[NET] link_watch: Eliminate potential delay on wrap-around
[linux-2.6] / net / atm / common.c
1 /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
2
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
6 #include <linux/module.h>
7 #include <linux/kmod.h>
8 #include <linux/net.h>          /* struct socket, struct proto_ops */
9 #include <linux/atm.h>          /* ATM stuff */
10 #include <linux/atmdev.h>
11 #include <linux/socket.h>       /* SOL_SOCKET */
12 #include <linux/errno.h>        /* error codes */
13 #include <linux/capability.h>
14 #include <linux/mm.h>
15 #include <linux/sched.h>
16 #include <linux/time.h>         /* struct timeval */
17 #include <linux/skbuff.h>
18 #include <linux/bitops.h>
19 #include <linux/init.h>
20 #include <net/sock.h>           /* struct sock */
21
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
24 #include <asm/poll.h>
25
26
27 #include "resources.h"          /* atm_find_dev */
28 #include "common.h"             /* prototypes */
29 #include "protocols.h"          /* atm_init_<transport> */
30 #include "addr.h"               /* address registry */
31 #include "signaling.h"          /* for WAITING and sigd_attach */
32
33
34 #if 0
35 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
36 #else
37 #define DPRINTK(format,args...)
38 #endif
39
40 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
41 DEFINE_RWLOCK(vcc_sklist_lock);
42
43 static void __vcc_insert_socket(struct sock *sk)
44 {
45         struct atm_vcc *vcc = atm_sk(sk);
46         struct hlist_head *head = &vcc_hash[vcc->vci &
47                                         (VCC_HTABLE_SIZE - 1)];
48         sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
49         sk_add_node(sk, head);
50 }
51
52 void vcc_insert_socket(struct sock *sk)
53 {
54         write_lock_irq(&vcc_sklist_lock);
55         __vcc_insert_socket(sk);
56         write_unlock_irq(&vcc_sklist_lock);
57 }
58
59 static void vcc_remove_socket(struct sock *sk)
60 {
61         write_lock_irq(&vcc_sklist_lock);
62         sk_del_node_init(sk);
63         write_unlock_irq(&vcc_sklist_lock);
64 }
65
66
67 static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
68 {
69         struct sk_buff *skb;
70         struct sock *sk = sk_atm(vcc);
71
72         if (atomic_read(&sk->sk_wmem_alloc) && !atm_may_send(vcc, size)) {
73                 DPRINTK("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
74                         atomic_read(&sk->sk_wmem_alloc), size,
75                         sk->sk_sndbuf);
76                 return NULL;
77         }
78         while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
79         DPRINTK("AlTx %d += %d\n", atomic_read(&sk->sk_wmem_alloc),
80                 skb->truesize);
81         atomic_add(skb->truesize, &sk->sk_wmem_alloc);
82         return skb;
83 }
84
85
86 EXPORT_SYMBOL(vcc_hash);
87 EXPORT_SYMBOL(vcc_sklist_lock);
88 EXPORT_SYMBOL(vcc_insert_socket);
89
90 static void vcc_sock_destruct(struct sock *sk)
91 {
92         if (atomic_read(&sk->sk_rmem_alloc))
93                 printk(KERN_DEBUG "vcc_sock_destruct: rmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_rmem_alloc));
94
95         if (atomic_read(&sk->sk_wmem_alloc))
96                 printk(KERN_DEBUG "vcc_sock_destruct: wmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_wmem_alloc));
97 }
98
99 static void vcc_def_wakeup(struct sock *sk)
100 {
101         read_lock(&sk->sk_callback_lock);
102         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
103                 wake_up(sk->sk_sleep);
104         read_unlock(&sk->sk_callback_lock);
105 }
106
107 static inline int vcc_writable(struct sock *sk)
108 {
109         struct atm_vcc *vcc = atm_sk(sk);
110
111         return (vcc->qos.txtp.max_sdu +
112                 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
113 }
114
115 static void vcc_write_space(struct sock *sk)
116 {
117         read_lock(&sk->sk_callback_lock);
118
119         if (vcc_writable(sk)) {
120                 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
121                         wake_up_interruptible(sk->sk_sleep);
122
123                 sk_wake_async(sk, 2, POLL_OUT);
124         }
125
126         read_unlock(&sk->sk_callback_lock);
127 }
128
129 static struct proto vcc_proto = {
130         .name     = "VCC",
131         .owner    = THIS_MODULE,
132         .obj_size = sizeof(struct atm_vcc),
133 };
134
135 int vcc_create(struct socket *sock, int protocol, int family)
136 {
137         struct sock *sk;
138         struct atm_vcc *vcc;
139
140         sock->sk = NULL;
141         if (sock->type == SOCK_STREAM)
142                 return -EINVAL;
143         sk = sk_alloc(family, GFP_KERNEL, &vcc_proto, 1);
144         if (!sk)
145                 return -ENOMEM;
146         sock_init_data(sock, sk);
147         sk->sk_state_change = vcc_def_wakeup;
148         sk->sk_write_space = vcc_write_space;
149
150         vcc = atm_sk(sk);
151         vcc->dev = NULL;
152         memset(&vcc->local,0,sizeof(struct sockaddr_atmsvc));
153         memset(&vcc->remote,0,sizeof(struct sockaddr_atmsvc));
154         vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
155         atomic_set(&sk->sk_wmem_alloc, 0);
156         atomic_set(&sk->sk_rmem_alloc, 0);
157         vcc->push = NULL;
158         vcc->pop = NULL;
159         vcc->push_oam = NULL;
160         vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
161         vcc->atm_options = vcc->aal_options = 0;
162         sk->sk_destruct = vcc_sock_destruct;
163         return 0;
164 }
165
166
167 static void vcc_destroy_socket(struct sock *sk)
168 {
169         struct atm_vcc *vcc = atm_sk(sk);
170         struct sk_buff *skb;
171
172         set_bit(ATM_VF_CLOSE, &vcc->flags);
173         clear_bit(ATM_VF_READY, &vcc->flags);
174         if (vcc->dev) {
175                 if (vcc->dev->ops->close)
176                         vcc->dev->ops->close(vcc);
177                 if (vcc->push)
178                         vcc->push(vcc, NULL); /* atmarpd has no push */
179
180                 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
181                         atm_return(vcc,skb->truesize);
182                         kfree_skb(skb);
183                 }
184
185                 module_put(vcc->dev->ops->owner);
186                 atm_dev_put(vcc->dev);
187         }
188
189         vcc_remove_socket(sk);
190 }
191
192
193 int vcc_release(struct socket *sock)
194 {
195         struct sock *sk = sock->sk;
196
197         if (sk) {
198                 lock_sock(sk);
199                 vcc_destroy_socket(sock->sk);
200                 release_sock(sk);
201                 sock_put(sk);
202         }
203
204         return 0;
205 }
206
207
208 void vcc_release_async(struct atm_vcc *vcc, int reply)
209 {
210         struct sock *sk = sk_atm(vcc);
211
212         set_bit(ATM_VF_CLOSE, &vcc->flags);
213         sk->sk_shutdown |= RCV_SHUTDOWN;
214         sk->sk_err = -reply;
215         clear_bit(ATM_VF_WAITING, &vcc->flags);
216         sk->sk_state_change(sk);
217 }
218
219
220 EXPORT_SYMBOL(vcc_release_async);
221
222
223 void atm_dev_release_vccs(struct atm_dev *dev)
224 {
225         int i;
226
227         write_lock_irq(&vcc_sklist_lock);
228         for (i = 0; i < VCC_HTABLE_SIZE; i++) {
229                 struct hlist_head *head = &vcc_hash[i];
230                 struct hlist_node *node, *tmp;
231                 struct sock *s;
232                 struct atm_vcc *vcc;
233
234                 sk_for_each_safe(s, node, tmp, head) {
235                         vcc = atm_sk(s);
236                         if (vcc->dev == dev) {
237                                 vcc_release_async(vcc, -EPIPE);
238                                 sk_del_node_init(s);
239                         }
240                 }
241         }
242         write_unlock_irq(&vcc_sklist_lock);
243 }
244
245
246 static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
247 {
248         int max_sdu;
249
250         if (!tp->traffic_class) return 0;
251         switch (aal) {
252                 case ATM_AAL0:
253                         max_sdu = ATM_CELL_SIZE-1;
254                         break;
255                 case ATM_AAL34:
256                         max_sdu = ATM_MAX_AAL34_PDU;
257                         break;
258                 default:
259                         printk(KERN_WARNING "ATM: AAL problems ... "
260                             "(%d)\n",aal);
261                         /* fall through */
262                 case ATM_AAL5:
263                         max_sdu = ATM_MAX_AAL5_PDU;
264         }
265         if (!tp->max_sdu) tp->max_sdu = max_sdu;
266         else if (tp->max_sdu > max_sdu) return -EINVAL;
267         if (!tp->max_cdv) tp->max_cdv = ATM_MAX_CDV;
268         return 0;
269 }
270
271
272 static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
273 {
274         struct hlist_head *head = &vcc_hash[vci &
275                                         (VCC_HTABLE_SIZE - 1)];
276         struct hlist_node *node;
277         struct sock *s;
278         struct atm_vcc *walk;
279
280         sk_for_each(s, node, head) {
281                 walk = atm_sk(s);
282                 if (walk->dev != vcc->dev)
283                         continue;
284                 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
285                     walk->vci == vci && ((walk->qos.txtp.traffic_class !=
286                     ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
287                     (walk->qos.rxtp.traffic_class != ATM_NONE &&
288                     vcc->qos.rxtp.traffic_class != ATM_NONE)))
289                         return -EADDRINUSE;
290         }
291
292         /* allow VCCs with same VPI/VCI iff they don't collide on
293            TX/RX (but we may refuse such sharing for other reasons,
294            e.g. if protocol requires to have both channels) */
295
296         return 0;
297 }
298
299
300 static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
301 {
302         static short p;        /* poor man's per-device cache */
303         static int c;
304         short old_p;
305         int old_c;
306         int err;
307
308         if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
309                 err = check_ci(vcc, *vpi, *vci);
310                 return err;
311         }
312         /* last scan may have left values out of bounds for current device */
313         if (*vpi != ATM_VPI_ANY)
314                 p = *vpi;
315         else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
316                 p = 0;
317         if (*vci != ATM_VCI_ANY)
318                 c = *vci;
319         else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
320                         c = ATM_NOT_RSV_VCI;
321         old_p = p;
322         old_c = c;
323         do {
324                 if (!check_ci(vcc, p, c)) {
325                         *vpi = p;
326                         *vci = c;
327                         return 0;
328                 }
329                 if (*vci == ATM_VCI_ANY) {
330                         c++;
331                         if (c >= 1 << vcc->dev->ci_range.vci_bits)
332                                 c = ATM_NOT_RSV_VCI;
333                 }
334                 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
335                     *vpi == ATM_VPI_ANY) {
336                         p++;
337                         if (p >= 1 << vcc->dev->ci_range.vpi_bits) p = 0;
338                 }
339         }
340         while (old_p != p || old_c != c);
341         return -EADDRINUSE;
342 }
343
344
345 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
346                          int vci)
347 {
348         struct sock *sk = sk_atm(vcc);
349         int error;
350
351         if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
352             vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
353             vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
354                 return -EINVAL;
355         if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
356                 return -EPERM;
357         error = -ENODEV;
358         if (!try_module_get(dev->ops->owner))
359                 return error;
360         vcc->dev = dev;
361         write_lock_irq(&vcc_sklist_lock);
362         if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
363             (error = find_ci(vcc, &vpi, &vci))) {
364                 write_unlock_irq(&vcc_sklist_lock);
365                 goto fail_module_put;
366         }
367         vcc->vpi = vpi;
368         vcc->vci = vci;
369         __vcc_insert_socket(sk);
370         write_unlock_irq(&vcc_sklist_lock);
371         switch (vcc->qos.aal) {
372                 case ATM_AAL0:
373                         error = atm_init_aal0(vcc);
374                         vcc->stats = &dev->stats.aal0;
375                         break;
376                 case ATM_AAL34:
377                         error = atm_init_aal34(vcc);
378                         vcc->stats = &dev->stats.aal34;
379                         break;
380                 case ATM_NO_AAL:
381                         /* ATM_AAL5 is also used in the "0 for default" case */
382                         vcc->qos.aal = ATM_AAL5;
383                         /* fall through */
384                 case ATM_AAL5:
385                         error = atm_init_aal5(vcc);
386                         vcc->stats = &dev->stats.aal5;
387                         break;
388                 default:
389                         error = -EPROTOTYPE;
390         }
391         if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
392         if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
393         if (error)
394                 goto fail;
395         DPRINTK("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
396         DPRINTK("  TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
397             vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
398         DPRINTK("  RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
399             vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
400
401         if (dev->ops->open) {
402                 if ((error = dev->ops->open(vcc)))
403                         goto fail;
404         }
405         return 0;
406
407 fail:
408         vcc_remove_socket(sk);
409 fail_module_put:
410         module_put(dev->ops->owner);
411         /* ensure we get dev module ref count correct */
412         vcc->dev = NULL;
413         return error;
414 }
415
416
417 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
418 {
419         struct atm_dev *dev;
420         struct atm_vcc *vcc = ATM_SD(sock);
421         int error;
422
423         DPRINTK("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
424         if (sock->state == SS_CONNECTED)
425                 return -EISCONN;
426         if (sock->state != SS_UNCONNECTED)
427                 return -EINVAL;
428         if (!(vpi || vci))
429                 return -EINVAL;
430
431         if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
432                 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
433         else
434                 if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
435                         return -EINVAL;
436         DPRINTK("vcc_connect (TX: cl %d,bw %d-%d,sdu %d; "
437             "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
438             vcc->qos.txtp.traffic_class,vcc->qos.txtp.min_pcr,
439             vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu,
440             vcc->qos.rxtp.traffic_class,vcc->qos.rxtp.min_pcr,
441             vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu,
442             vcc->qos.aal == ATM_AAL5 ? "" : vcc->qos.aal == ATM_AAL0 ? "" :
443             " ??? code ",vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
444         if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
445                 return -EBADFD;
446         if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
447             vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
448                 return -EINVAL;
449         if (likely(itf != ATM_ITF_ANY)) {
450                 dev = try_then_request_module(atm_dev_lookup(itf), "atm-device-%d", itf);
451         } else {
452                 dev = NULL;
453                 mutex_lock(&atm_dev_mutex);
454                 if (!list_empty(&atm_devs)) {
455                         dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
456                         atm_dev_hold(dev);
457                 }
458                 mutex_unlock(&atm_dev_mutex);
459         }
460         if (!dev)
461                 return -ENODEV;
462         error = __vcc_connect(vcc, dev, vpi, vci);
463         if (error) {
464                 atm_dev_put(dev);
465                 return error;
466         }
467         if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
468                 set_bit(ATM_VF_PARTIAL,&vcc->flags);
469         if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
470                 sock->state = SS_CONNECTED;
471         return 0;
472 }
473
474
475 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
476                 size_t size, int flags)
477 {
478         struct sock *sk = sock->sk;
479         struct atm_vcc *vcc;
480         struct sk_buff *skb;
481         int copied, error = -EINVAL;
482
483         if (sock->state != SS_CONNECTED)
484                 return -ENOTCONN;
485         if (flags & ~MSG_DONTWAIT)              /* only handle MSG_DONTWAIT */
486                 return -EOPNOTSUPP;
487         vcc = ATM_SD(sock);
488         if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
489             test_bit(ATM_VF_CLOSE,&vcc->flags) ||
490             !test_bit(ATM_VF_READY, &vcc->flags))
491                 return 0;
492
493         skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
494         if (!skb)
495                 return error;
496
497         copied = skb->len;
498         if (copied > size) {
499                 copied = size;
500                 msg->msg_flags |= MSG_TRUNC;
501         }
502
503         error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
504         if (error)
505                 return error;
506         sock_recv_timestamp(msg, sk, skb);
507         DPRINTK("RcvM %d -= %d\n", atomic_read(&sk->rmem_alloc), skb->truesize);
508         atm_return(vcc, skb->truesize);
509         skb_free_datagram(sk, skb);
510         return copied;
511 }
512
513
514 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
515                 size_t total_len)
516 {
517         struct sock *sk = sock->sk;
518         DEFINE_WAIT(wait);
519         struct atm_vcc *vcc;
520         struct sk_buff *skb;
521         int eff,error;
522         const void __user *buff;
523         int size;
524
525         lock_sock(sk);
526         if (sock->state != SS_CONNECTED) {
527                 error = -ENOTCONN;
528                 goto out;
529         }
530         if (m->msg_name) {
531                 error = -EISCONN;
532                 goto out;
533         }
534         if (m->msg_iovlen != 1) {
535                 error = -ENOSYS; /* fix this later @@@ */
536                 goto out;
537         }
538         buff = m->msg_iov->iov_base;
539         size = m->msg_iov->iov_len;
540         vcc = ATM_SD(sock);
541         if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
542             test_bit(ATM_VF_CLOSE, &vcc->flags) ||
543             !test_bit(ATM_VF_READY, &vcc->flags)) {
544                 error = -EPIPE;
545                 send_sig(SIGPIPE, current, 0);
546                 goto out;
547         }
548         if (!size) {
549                 error = 0;
550                 goto out;
551         }
552         if (size < 0 || size > vcc->qos.txtp.max_sdu) {
553                 error = -EMSGSIZE;
554                 goto out;
555         }
556
557         eff = (size+3) & ~3; /* align to word boundary */
558         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
559         error = 0;
560         while (!(skb = alloc_tx(vcc,eff))) {
561                 if (m->msg_flags & MSG_DONTWAIT) {
562                         error = -EAGAIN;
563                         break;
564                 }
565                 schedule();
566                 if (signal_pending(current)) {
567                         error = -ERESTARTSYS;
568                         break;
569                 }
570                 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
571                     test_bit(ATM_VF_CLOSE,&vcc->flags) ||
572                     !test_bit(ATM_VF_READY,&vcc->flags)) {
573                         error = -EPIPE;
574                         send_sig(SIGPIPE, current, 0);
575                         break;
576                 }
577                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
578         }
579         finish_wait(sk->sk_sleep, &wait);
580         if (error)
581                 goto out;
582         skb->dev = NULL; /* for paths shared with net_device interfaces */
583         ATM_SKB(skb)->atm_options = vcc->atm_options;
584         if (copy_from_user(skb_put(skb,size),buff,size)) {
585                 kfree_skb(skb);
586                 error = -EFAULT;
587                 goto out;
588         }
589         if (eff != size) memset(skb->data+size,0,eff-size);
590         error = vcc->dev->ops->send(vcc,skb);
591         error = error ? error : size;
592 out:
593         release_sock(sk);
594         return error;
595 }
596
597
598 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
599 {
600         struct sock *sk = sock->sk;
601         struct atm_vcc *vcc;
602         unsigned int mask;
603
604         poll_wait(file, sk->sk_sleep, wait);
605         mask = 0;
606
607         vcc = ATM_SD(sock);
608
609         /* exceptional events */
610         if (sk->sk_err)
611                 mask = POLLERR;
612
613         if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
614             test_bit(ATM_VF_CLOSE, &vcc->flags))
615                 mask |= POLLHUP;
616
617         /* readable? */
618         if (!skb_queue_empty(&sk->sk_receive_queue))
619                 mask |= POLLIN | POLLRDNORM;
620
621         /* writable? */
622         if (sock->state == SS_CONNECTING &&
623             test_bit(ATM_VF_WAITING, &vcc->flags))
624                 return mask;
625
626         if (vcc->qos.txtp.traffic_class != ATM_NONE &&
627             vcc_writable(sk))
628                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
629
630         return mask;
631 }
632
633
634 static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
635 {
636         int error;
637
638         /*
639          * Don't let the QoS change the already connected AAL type nor the
640          * traffic class.
641          */
642         if (qos->aal != vcc->qos.aal ||
643             qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
644             qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
645                 return -EINVAL;
646         error = adjust_tp(&qos->txtp,qos->aal);
647         if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
648         if (error) return error;
649         if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
650         if (sk_atm(vcc)->sk_family == AF_ATMPVC)
651                 return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
652         return svc_change_qos(vcc,qos);
653 }
654
655
656 static int check_tp(struct atm_trafprm *tp)
657 {
658         /* @@@ Should be merged with adjust_tp */
659         if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
660         if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
661             !tp->max_pcr) return -EINVAL;
662         if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
663         if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
664             tp->min_pcr > tp->max_pcr) return -EINVAL;
665         /*
666          * We allow pcr to be outside [min_pcr,max_pcr], because later
667          * adjustment may still push it in the valid range.
668          */
669         return 0;
670 }
671
672
673 static int check_qos(struct atm_qos *qos)
674 {
675         int error;
676
677         if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
678                 return -EINVAL;
679         if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
680             qos->txtp.traffic_class && qos->rxtp.traffic_class &&
681             qos->txtp.traffic_class != ATM_ANYCLASS &&
682             qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
683         error = check_tp(&qos->txtp);
684         if (error) return error;
685         return check_tp(&qos->rxtp);
686 }
687
688 int vcc_setsockopt(struct socket *sock, int level, int optname,
689                    char __user *optval, int optlen)
690 {
691         struct atm_vcc *vcc;
692         unsigned long value;
693         int error;
694
695         if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
696                 return -EINVAL;
697
698         vcc = ATM_SD(sock);
699         switch (optname) {
700                 case SO_ATMQOS:
701                         {
702                                 struct atm_qos qos;
703
704                                 if (copy_from_user(&qos,optval,sizeof(qos)))
705                                         return -EFAULT;
706                                 error = check_qos(&qos);
707                                 if (error) return error;
708                                 if (sock->state == SS_CONNECTED)
709                                         return atm_change_qos(vcc,&qos);
710                                 if (sock->state != SS_UNCONNECTED)
711                                         return -EBADFD;
712                                 vcc->qos = qos;
713                                 set_bit(ATM_VF_HASQOS,&vcc->flags);
714                                 return 0;
715                         }
716                 case SO_SETCLP:
717                         if (get_user(value,(unsigned long __user *)optval))
718                                 return -EFAULT;
719                         if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
720                         else vcc->atm_options &= ~ATM_ATMOPT_CLP;
721                         return 0;
722                 default:
723                         if (level == SOL_SOCKET) return -EINVAL;
724                         break;
725         }
726         if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
727         return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
728 }
729
730
731 int vcc_getsockopt(struct socket *sock, int level, int optname,
732                    char __user *optval, int __user *optlen)
733 {
734         struct atm_vcc *vcc;
735         int len;
736
737         if (get_user(len, optlen))
738                 return -EFAULT;
739         if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
740                 return -EINVAL;
741
742         vcc = ATM_SD(sock);
743         switch (optname) {
744                 case SO_ATMQOS:
745                         if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
746                                 return -EINVAL;
747                         return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
748                             -EFAULT : 0;
749                 case SO_SETCLP:
750                         return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
751                           0,(unsigned long __user *)optval) ? -EFAULT : 0;
752                 case SO_ATMPVC:
753                         {
754                                 struct sockaddr_atmpvc pvc;
755
756                                 if (!vcc->dev ||
757                                     !test_bit(ATM_VF_ADDR,&vcc->flags))
758                                         return -ENOTCONN;
759                                 pvc.sap_family = AF_ATMPVC;
760                                 pvc.sap_addr.itf = vcc->dev->number;
761                                 pvc.sap_addr.vpi = vcc->vpi;
762                                 pvc.sap_addr.vci = vcc->vci;
763                                 return copy_to_user(optval,&pvc,sizeof(pvc)) ?
764                                     -EFAULT : 0;
765                         }
766                 default:
767                         if (level == SOL_SOCKET) return -EINVAL;
768                         break;
769         }
770         if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
771         return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
772 }
773
774 static int __init atm_init(void)
775 {
776         int error;
777
778         if ((error = proto_register(&vcc_proto, 0)) < 0)
779                 goto out;
780
781         if ((error = atmpvc_init()) < 0) {
782                 printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
783                 goto out_unregister_vcc_proto;
784         }
785         if ((error = atmsvc_init()) < 0) {
786                 printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
787                 goto out_atmpvc_exit;
788         }
789         if ((error = atm_proc_init()) < 0) {
790                 printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
791                 goto out_atmsvc_exit;
792         }
793         if ((error = atm_sysfs_init()) < 0) {
794                 printk(KERN_ERR "atm_sysfs_init() failed with %d\n",error);
795                 goto out_atmproc_exit;
796         }
797 out:
798         return error;
799 out_atmproc_exit:
800         atm_proc_exit();
801 out_atmsvc_exit:
802         atmsvc_exit();
803 out_atmpvc_exit:
804         atmsvc_exit();
805 out_unregister_vcc_proto:
806         proto_unregister(&vcc_proto);
807         goto out;
808 }
809
810 static void __exit atm_exit(void)
811 {
812         atm_proc_exit();
813         atm_sysfs_exit();
814         atmsvc_exit();
815         atmpvc_exit();
816         proto_unregister(&vcc_proto);
817 }
818
819 subsys_initcall(atm_init);
820
821 module_exit(atm_exit);
822
823 MODULE_LICENSE("GPL");
824 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
825 MODULE_ALIAS_NETPROTO(PF_ATMSVC);