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