[ATM]: attempt to autoload atm drivers
[linux-2.6.git] / 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 (likely(itf != ATM_ITF_ANY)) {
427                 dev = try_then_request_module(atm_dev_lookup(itf), "atm-device-%d", itf);
428         } else {
429                 dev = NULL;
430                 spin_lock(&atm_dev_lock);
431                 if (!list_empty(&atm_devs)) {
432                         dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
433                         atm_dev_hold(dev);
434                 }
435                 spin_unlock(&atm_dev_lock);
436         }
437         if (!dev)
438                 return -ENODEV;
439         error = __vcc_connect(vcc, dev, vpi, vci);
440         if (error) {
441                 atm_dev_put(dev);
442                 return error;
443         }
444         if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
445                 set_bit(ATM_VF_PARTIAL,&vcc->flags);
446         if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
447                 sock->state = SS_CONNECTED;
448         return 0;
449 }
450
451
452 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
453                 size_t size, int flags)
454 {
455         struct sock *sk = sock->sk;
456         struct atm_vcc *vcc;
457         struct sk_buff *skb;
458         int copied, error = -EINVAL;
459
460         if (sock->state != SS_CONNECTED)
461                 return -ENOTCONN;
462         if (flags & ~MSG_DONTWAIT)              /* only handle MSG_DONTWAIT */
463                 return -EOPNOTSUPP;
464         vcc = ATM_SD(sock);
465         if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
466             test_bit(ATM_VF_CLOSE,&vcc->flags) ||
467             !test_bit(ATM_VF_READY, &vcc->flags))
468                 return 0;
469
470         skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
471         if (!skb)
472                 return error;
473
474         copied = skb->len; 
475         if (copied > size) {
476                 copied = size; 
477                 msg->msg_flags |= MSG_TRUNC;
478         }
479
480         error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
481         if (error)
482                 return error;
483         sock_recv_timestamp(msg, sk, skb);
484         DPRINTK("RcvM %d -= %d\n", atomic_read(&sk->rmem_alloc), skb->truesize);
485         atm_return(vcc, skb->truesize);
486         skb_free_datagram(sk, skb);
487         return copied;
488 }
489
490
491 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
492                 size_t total_len)
493 {
494         struct sock *sk = sock->sk;
495         DEFINE_WAIT(wait);
496         struct atm_vcc *vcc;
497         struct sk_buff *skb;
498         int eff,error;
499         const void __user *buff;
500         int size;
501
502         lock_sock(sk);
503         if (sock->state != SS_CONNECTED) {
504                 error = -ENOTCONN;
505                 goto out;
506         }
507         if (m->msg_name) {
508                 error = -EISCONN;
509                 goto out;
510         }
511         if (m->msg_iovlen != 1) {
512                 error = -ENOSYS; /* fix this later @@@ */
513                 goto out;
514         }
515         buff = m->msg_iov->iov_base;
516         size = m->msg_iov->iov_len;
517         vcc = ATM_SD(sock);
518         if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
519             test_bit(ATM_VF_CLOSE, &vcc->flags) ||
520             !test_bit(ATM_VF_READY, &vcc->flags)) {
521                 error = -EPIPE;
522                 send_sig(SIGPIPE, current, 0);
523                 goto out;
524         }
525         if (!size) {
526                 error = 0;
527                 goto out;
528         }
529         if (size < 0 || size > vcc->qos.txtp.max_sdu) {
530                 error = -EMSGSIZE;
531                 goto out;
532         }
533
534         eff = (size+3) & ~3; /* align to word boundary */
535         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
536         error = 0;
537         while (!(skb = alloc_tx(vcc,eff))) {
538                 if (m->msg_flags & MSG_DONTWAIT) {
539                         error = -EAGAIN;
540                         break;
541                 }
542                 schedule();
543                 if (signal_pending(current)) {
544                         error = -ERESTARTSYS;
545                         break;
546                 }
547                 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
548                     test_bit(ATM_VF_CLOSE,&vcc->flags) ||
549                     !test_bit(ATM_VF_READY,&vcc->flags)) {
550                         error = -EPIPE;
551                         send_sig(SIGPIPE, current, 0);
552                         break;
553                 }
554                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
555         }
556         finish_wait(sk->sk_sleep, &wait);
557         if (error)
558                 goto out;
559         skb->dev = NULL; /* for paths shared with net_device interfaces */
560         ATM_SKB(skb)->atm_options = vcc->atm_options;
561         if (copy_from_user(skb_put(skb,size),buff,size)) {
562                 kfree_skb(skb);
563                 error = -EFAULT;
564                 goto out;
565         }
566         if (eff != size) memset(skb->data+size,0,eff-size);
567         error = vcc->dev->ops->send(vcc,skb);
568         error = error ? error : size;
569 out:
570         release_sock(sk);
571         return error;
572 }
573
574
575 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
576 {
577         struct sock *sk = sock->sk;
578         struct atm_vcc *vcc;
579         unsigned int mask;
580
581         poll_wait(file, sk->sk_sleep, wait);
582         mask = 0;
583
584         vcc = ATM_SD(sock);
585
586         /* exceptional events */
587         if (sk->sk_err)
588                 mask = POLLERR;
589
590         if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
591             test_bit(ATM_VF_CLOSE, &vcc->flags))
592                 mask |= POLLHUP;
593
594         /* readable? */
595         if (!skb_queue_empty(&sk->sk_receive_queue))
596                 mask |= POLLIN | POLLRDNORM;
597
598         /* writable? */
599         if (sock->state == SS_CONNECTING &&
600             test_bit(ATM_VF_WAITING, &vcc->flags))
601                 return mask;
602
603         if (vcc->qos.txtp.traffic_class != ATM_NONE &&
604             vcc_writable(sk))
605                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
606
607         return mask;
608 }
609
610
611 static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
612 {
613         int error;
614
615         /*
616          * Don't let the QoS change the already connected AAL type nor the
617          * traffic class.
618          */
619         if (qos->aal != vcc->qos.aal ||
620             qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
621             qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
622                 return -EINVAL;
623         error = adjust_tp(&qos->txtp,qos->aal);
624         if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
625         if (error) return error;
626         if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
627         if (sk_atm(vcc)->sk_family == AF_ATMPVC)
628                 return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
629         return svc_change_qos(vcc,qos);
630 }
631
632
633 static int check_tp(struct atm_trafprm *tp)
634 {
635         /* @@@ Should be merged with adjust_tp */
636         if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
637         if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
638             !tp->max_pcr) return -EINVAL;
639         if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
640         if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
641             tp->min_pcr > tp->max_pcr) return -EINVAL;
642         /*
643          * We allow pcr to be outside [min_pcr,max_pcr], because later
644          * adjustment may still push it in the valid range.
645          */
646         return 0;
647 }
648
649
650 static int check_qos(struct atm_qos *qos)
651 {
652         int error;
653
654         if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
655                 return -EINVAL;
656         if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
657             qos->txtp.traffic_class && qos->rxtp.traffic_class &&
658             qos->txtp.traffic_class != ATM_ANYCLASS &&
659             qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
660         error = check_tp(&qos->txtp);
661         if (error) return error;
662         return check_tp(&qos->rxtp);
663 }
664
665 int vcc_setsockopt(struct socket *sock, int level, int optname,
666                    char __user *optval, int optlen)
667 {
668         struct atm_vcc *vcc;
669         unsigned long value;
670         int error;
671
672         if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
673                 return -EINVAL;
674
675         vcc = ATM_SD(sock);
676         switch (optname) {
677                 case SO_ATMQOS:
678                         {
679                                 struct atm_qos qos;
680
681                                 if (copy_from_user(&qos,optval,sizeof(qos)))
682                                         return -EFAULT;
683                                 error = check_qos(&qos);
684                                 if (error) return error;
685                                 if (sock->state == SS_CONNECTED)
686                                         return atm_change_qos(vcc,&qos);
687                                 if (sock->state != SS_UNCONNECTED)
688                                         return -EBADFD;
689                                 vcc->qos = qos;
690                                 set_bit(ATM_VF_HASQOS,&vcc->flags);
691                                 return 0;
692                         }
693                 case SO_SETCLP:
694                         if (get_user(value,(unsigned long __user *)optval))
695                                 return -EFAULT;
696                         if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
697                         else vcc->atm_options &= ~ATM_ATMOPT_CLP;
698                         return 0;
699                 default:
700                         if (level == SOL_SOCKET) return -EINVAL;
701                         break;
702         }
703         if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
704         return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
705 }
706
707
708 int vcc_getsockopt(struct socket *sock, int level, int optname,
709                    char __user *optval, int __user *optlen)
710 {
711         struct atm_vcc *vcc;
712         int len;
713
714         if (get_user(len, optlen))
715                 return -EFAULT;
716         if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
717                 return -EINVAL;
718
719         vcc = ATM_SD(sock);
720         switch (optname) {
721                 case SO_ATMQOS:
722                         if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
723                                 return -EINVAL;
724                         return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
725                             -EFAULT : 0;
726                 case SO_SETCLP:
727                         return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
728                           0,(unsigned long __user *)optval) ? -EFAULT : 0;
729                 case SO_ATMPVC:
730                         {
731                                 struct sockaddr_atmpvc pvc;
732
733                                 if (!vcc->dev ||
734                                     !test_bit(ATM_VF_ADDR,&vcc->flags))
735                                         return -ENOTCONN;
736                                 pvc.sap_family = AF_ATMPVC;
737                                 pvc.sap_addr.itf = vcc->dev->number;
738                                 pvc.sap_addr.vpi = vcc->vpi;
739                                 pvc.sap_addr.vci = vcc->vci;
740                                 return copy_to_user(optval,&pvc,sizeof(pvc)) ?
741                                     -EFAULT : 0;
742                         }
743                 default:
744                         if (level == SOL_SOCKET) return -EINVAL;
745                         break;
746         }
747         if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
748         return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
749 }
750
751 static int __init atm_init(void)
752 {
753         int error;
754
755         if ((error = proto_register(&vcc_proto, 0)) < 0)
756                 goto out;
757
758         if ((error = atmpvc_init()) < 0) {
759                 printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
760                 goto out_unregister_vcc_proto;
761         }
762         if ((error = atmsvc_init()) < 0) {
763                 printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
764                 goto out_atmpvc_exit;
765         }
766         if ((error = atm_proc_init()) < 0) {
767                 printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
768                 goto out_atmsvc_exit;
769         }
770 out:
771         return error;
772 out_atmsvc_exit:
773         atmsvc_exit();
774 out_atmpvc_exit:
775         atmsvc_exit();
776 out_unregister_vcc_proto:
777         proto_unregister(&vcc_proto);
778         goto out;
779 }
780
781 static void __exit atm_exit(void)
782 {
783         atm_proc_exit();
784         atmsvc_exit();
785         atmpvc_exit();
786         proto_unregister(&vcc_proto);
787 }
788
789 module_init(atm_init);
790 module_exit(atm_exit);
791
792 MODULE_LICENSE("GPL");
793 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
794 MODULE_ALIAS_NETPROTO(PF_ATMSVC);