m68k: amiga - Zorro bus modalias support
[linux/fpc-iii.git] / net / atm / common.c
blob97ed94aa0cbceb3623b91e918fd886f353793aab
1 /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
5 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
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 <linux/slab.h>
22 #include <net/sock.h> /* struct sock */
23 #include <linux/uaccess.h>
24 #include <linux/poll.h>
26 #include <asm/atomic.h>
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 */
34 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
35 EXPORT_SYMBOL(vcc_hash);
37 DEFINE_RWLOCK(vcc_sklist_lock);
38 EXPORT_SYMBOL(vcc_sklist_lock);
40 static void __vcc_insert_socket(struct sock *sk)
42 struct atm_vcc *vcc = atm_sk(sk);
43 struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
44 sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
45 sk_add_node(sk, head);
48 void vcc_insert_socket(struct sock *sk)
50 write_lock_irq(&vcc_sklist_lock);
51 __vcc_insert_socket(sk);
52 write_unlock_irq(&vcc_sklist_lock);
54 EXPORT_SYMBOL(vcc_insert_socket);
56 static void vcc_remove_socket(struct sock *sk)
58 write_lock_irq(&vcc_sklist_lock);
59 sk_del_node_init(sk);
60 write_unlock_irq(&vcc_sklist_lock);
63 static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
65 struct sk_buff *skb;
66 struct sock *sk = sk_atm(vcc);
68 if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
69 pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
70 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
71 return NULL;
73 while (!(skb = alloc_skb(size, GFP_KERNEL)))
74 schedule();
75 pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
76 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
77 return skb;
80 static void vcc_sock_destruct(struct sock *sk)
82 if (atomic_read(&sk->sk_rmem_alloc))
83 printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
84 __func__, atomic_read(&sk->sk_rmem_alloc));
86 if (atomic_read(&sk->sk_wmem_alloc))
87 printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
88 __func__, atomic_read(&sk->sk_wmem_alloc));
91 static void vcc_def_wakeup(struct sock *sk)
93 read_lock(&sk->sk_callback_lock);
94 if (sk_has_sleeper(sk))
95 wake_up(sk->sk_sleep);
96 read_unlock(&sk->sk_callback_lock);
99 static inline int vcc_writable(struct sock *sk)
101 struct atm_vcc *vcc = atm_sk(sk);
103 return (vcc->qos.txtp.max_sdu +
104 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
107 static void vcc_write_space(struct sock *sk)
109 read_lock(&sk->sk_callback_lock);
111 if (vcc_writable(sk)) {
112 if (sk_has_sleeper(sk))
113 wake_up_interruptible(sk->sk_sleep);
115 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
118 read_unlock(&sk->sk_callback_lock);
121 static struct proto vcc_proto = {
122 .name = "VCC",
123 .owner = THIS_MODULE,
124 .obj_size = sizeof(struct atm_vcc),
127 int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
129 struct sock *sk;
130 struct atm_vcc *vcc;
132 sock->sk = NULL;
133 if (sock->type == SOCK_STREAM)
134 return -EINVAL;
135 sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
136 if (!sk)
137 return -ENOMEM;
138 sock_init_data(sock, sk);
139 sk->sk_state_change = vcc_def_wakeup;
140 sk->sk_write_space = vcc_write_space;
142 vcc = atm_sk(sk);
143 vcc->dev = NULL;
144 memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
145 memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
146 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
147 atomic_set(&sk->sk_wmem_alloc, 1);
148 atomic_set(&sk->sk_rmem_alloc, 0);
149 vcc->push = NULL;
150 vcc->pop = NULL;
151 vcc->push_oam = NULL;
152 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
153 vcc->atm_options = vcc->aal_options = 0;
154 sk->sk_destruct = vcc_sock_destruct;
155 return 0;
158 static void vcc_destroy_socket(struct sock *sk)
160 struct atm_vcc *vcc = atm_sk(sk);
161 struct sk_buff *skb;
163 set_bit(ATM_VF_CLOSE, &vcc->flags);
164 clear_bit(ATM_VF_READY, &vcc->flags);
165 if (vcc->dev) {
166 if (vcc->dev->ops->close)
167 vcc->dev->ops->close(vcc);
168 if (vcc->push)
169 vcc->push(vcc, NULL); /* atmarpd has no push */
171 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
172 atm_return(vcc, skb->truesize);
173 kfree_skb(skb);
176 module_put(vcc->dev->ops->owner);
177 atm_dev_put(vcc->dev);
180 vcc_remove_socket(sk);
183 int vcc_release(struct socket *sock)
185 struct sock *sk = sock->sk;
187 if (sk) {
188 lock_sock(sk);
189 vcc_destroy_socket(sock->sk);
190 release_sock(sk);
191 sock_put(sk);
194 return 0;
197 void vcc_release_async(struct atm_vcc *vcc, int reply)
199 struct sock *sk = sk_atm(vcc);
201 set_bit(ATM_VF_CLOSE, &vcc->flags);
202 sk->sk_shutdown |= RCV_SHUTDOWN;
203 sk->sk_err = -reply;
204 clear_bit(ATM_VF_WAITING, &vcc->flags);
205 sk->sk_state_change(sk);
207 EXPORT_SYMBOL(vcc_release_async);
210 void atm_dev_release_vccs(struct atm_dev *dev)
212 int i;
214 write_lock_irq(&vcc_sklist_lock);
215 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
216 struct hlist_head *head = &vcc_hash[i];
217 struct hlist_node *node, *tmp;
218 struct sock *s;
219 struct atm_vcc *vcc;
221 sk_for_each_safe(s, node, tmp, head) {
222 vcc = atm_sk(s);
223 if (vcc->dev == dev) {
224 vcc_release_async(vcc, -EPIPE);
225 sk_del_node_init(s);
229 write_unlock_irq(&vcc_sklist_lock);
232 static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
234 int max_sdu;
236 if (!tp->traffic_class)
237 return 0;
238 switch (aal) {
239 case ATM_AAL0:
240 max_sdu = ATM_CELL_SIZE-1;
241 break;
242 case ATM_AAL34:
243 max_sdu = ATM_MAX_AAL34_PDU;
244 break;
245 default:
246 pr_warning("AAL problems ... (%d)\n", aal);
247 /* fall through */
248 case ATM_AAL5:
249 max_sdu = ATM_MAX_AAL5_PDU;
251 if (!tp->max_sdu)
252 tp->max_sdu = max_sdu;
253 else if (tp->max_sdu > max_sdu)
254 return -EINVAL;
255 if (!tp->max_cdv)
256 tp->max_cdv = ATM_MAX_CDV;
257 return 0;
260 static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
262 struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
263 struct hlist_node *node;
264 struct sock *s;
265 struct atm_vcc *walk;
267 sk_for_each(s, node, head) {
268 walk = atm_sk(s);
269 if (walk->dev != vcc->dev)
270 continue;
271 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
272 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
273 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
274 (walk->qos.rxtp.traffic_class != ATM_NONE &&
275 vcc->qos.rxtp.traffic_class != ATM_NONE)))
276 return -EADDRINUSE;
279 /* allow VCCs with same VPI/VCI iff they don't collide on
280 TX/RX (but we may refuse such sharing for other reasons,
281 e.g. if protocol requires to have both channels) */
283 return 0;
286 static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
288 static short p; /* poor man's per-device cache */
289 static int c;
290 short old_p;
291 int old_c;
292 int err;
294 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
295 err = check_ci(vcc, *vpi, *vci);
296 return err;
298 /* last scan may have left values out of bounds for current device */
299 if (*vpi != ATM_VPI_ANY)
300 p = *vpi;
301 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
302 p = 0;
303 if (*vci != ATM_VCI_ANY)
304 c = *vci;
305 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
306 c = ATM_NOT_RSV_VCI;
307 old_p = p;
308 old_c = c;
309 do {
310 if (!check_ci(vcc, p, c)) {
311 *vpi = p;
312 *vci = c;
313 return 0;
315 if (*vci == ATM_VCI_ANY) {
316 c++;
317 if (c >= 1 << vcc->dev->ci_range.vci_bits)
318 c = ATM_NOT_RSV_VCI;
320 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
321 *vpi == ATM_VPI_ANY) {
322 p++;
323 if (p >= 1 << vcc->dev->ci_range.vpi_bits)
324 p = 0;
326 } while (old_p != p || old_c != c);
327 return -EADDRINUSE;
330 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
331 int vci)
333 struct sock *sk = sk_atm(vcc);
334 int error;
336 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
337 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
338 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
339 return -EINVAL;
340 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
341 return -EPERM;
342 error = -ENODEV;
343 if (!try_module_get(dev->ops->owner))
344 return error;
345 vcc->dev = dev;
346 write_lock_irq(&vcc_sklist_lock);
347 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
348 (error = find_ci(vcc, &vpi, &vci))) {
349 write_unlock_irq(&vcc_sklist_lock);
350 goto fail_module_put;
352 vcc->vpi = vpi;
353 vcc->vci = vci;
354 __vcc_insert_socket(sk);
355 write_unlock_irq(&vcc_sklist_lock);
356 switch (vcc->qos.aal) {
357 case ATM_AAL0:
358 error = atm_init_aal0(vcc);
359 vcc->stats = &dev->stats.aal0;
360 break;
361 case ATM_AAL34:
362 error = atm_init_aal34(vcc);
363 vcc->stats = &dev->stats.aal34;
364 break;
365 case ATM_NO_AAL:
366 /* ATM_AAL5 is also used in the "0 for default" case */
367 vcc->qos.aal = ATM_AAL5;
368 /* fall through */
369 case ATM_AAL5:
370 error = atm_init_aal5(vcc);
371 vcc->stats = &dev->stats.aal5;
372 break;
373 default:
374 error = -EPROTOTYPE;
376 if (!error)
377 error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
378 if (!error)
379 error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
380 if (error)
381 goto fail;
382 pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
383 pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
384 vcc->qos.txtp.traffic_class,
385 vcc->qos.txtp.min_pcr,
386 vcc->qos.txtp.max_pcr,
387 vcc->qos.txtp.max_sdu);
388 pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
389 vcc->qos.rxtp.traffic_class,
390 vcc->qos.rxtp.min_pcr,
391 vcc->qos.rxtp.max_pcr,
392 vcc->qos.rxtp.max_sdu);
394 if (dev->ops->open) {
395 error = dev->ops->open(vcc);
396 if (error)
397 goto fail;
399 return 0;
401 fail:
402 vcc_remove_socket(sk);
403 fail_module_put:
404 module_put(dev->ops->owner);
405 /* ensure we get dev module ref count correct */
406 vcc->dev = NULL;
407 return error;
410 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
412 struct atm_dev *dev;
413 struct atm_vcc *vcc = ATM_SD(sock);
414 int error;
416 pr_debug("(vpi %d, vci %d)\n", vpi, vci);
417 if (sock->state == SS_CONNECTED)
418 return -EISCONN;
419 if (sock->state != SS_UNCONNECTED)
420 return -EINVAL;
421 if (!(vpi || vci))
422 return -EINVAL;
424 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
425 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
426 else
427 if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
428 return -EINVAL;
429 pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
430 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
431 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
432 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
433 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
434 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
435 vcc->qos.aal == ATM_AAL5 ? "" :
436 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
437 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
438 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
439 return -EBADFD;
440 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
441 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
442 return -EINVAL;
443 if (likely(itf != ATM_ITF_ANY)) {
444 dev = try_then_request_module(atm_dev_lookup(itf),
445 "atm-device-%d", itf);
446 } else {
447 dev = NULL;
448 mutex_lock(&atm_dev_mutex);
449 if (!list_empty(&atm_devs)) {
450 dev = list_entry(atm_devs.next,
451 struct atm_dev, dev_list);
452 atm_dev_hold(dev);
454 mutex_unlock(&atm_dev_mutex);
456 if (!dev)
457 return -ENODEV;
458 error = __vcc_connect(vcc, dev, vpi, vci);
459 if (error) {
460 atm_dev_put(dev);
461 return error;
463 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
464 set_bit(ATM_VF_PARTIAL, &vcc->flags);
465 if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
466 sock->state = SS_CONNECTED;
467 return 0;
470 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
471 size_t size, int flags)
473 struct sock *sk = sock->sk;
474 struct atm_vcc *vcc;
475 struct sk_buff *skb;
476 int copied, error = -EINVAL;
478 if (sock->state != SS_CONNECTED)
479 return -ENOTCONN;
480 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
481 return -EOPNOTSUPP;
482 vcc = ATM_SD(sock);
483 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
484 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
485 !test_bit(ATM_VF_READY, &vcc->flags))
486 return 0;
488 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
489 if (!skb)
490 return error;
492 copied = skb->len;
493 if (copied > size) {
494 copied = size;
495 msg->msg_flags |= MSG_TRUNC;
498 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
499 if (error)
500 return error;
501 sock_recv_ts_and_drops(msg, sk, skb);
502 pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
503 atm_return(vcc, skb->truesize);
504 skb_free_datagram(sk, skb);
505 return copied;
508 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
509 size_t total_len)
511 struct sock *sk = sock->sk;
512 DEFINE_WAIT(wait);
513 struct atm_vcc *vcc;
514 struct sk_buff *skb;
515 int eff, error;
516 const void __user *buff;
517 int size;
519 lock_sock(sk);
520 if (sock->state != SS_CONNECTED) {
521 error = -ENOTCONN;
522 goto out;
524 if (m->msg_name) {
525 error = -EISCONN;
526 goto out;
528 if (m->msg_iovlen != 1) {
529 error = -ENOSYS; /* fix this later @@@ */
530 goto out;
532 buff = m->msg_iov->iov_base;
533 size = m->msg_iov->iov_len;
534 vcc = ATM_SD(sock);
535 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
536 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
537 !test_bit(ATM_VF_READY, &vcc->flags)) {
538 error = -EPIPE;
539 send_sig(SIGPIPE, current, 0);
540 goto out;
542 if (!size) {
543 error = 0;
544 goto out;
546 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
547 error = -EMSGSIZE;
548 goto out;
551 eff = (size+3) & ~3; /* align to word boundary */
552 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
553 error = 0;
554 while (!(skb = alloc_tx(vcc, eff))) {
555 if (m->msg_flags & MSG_DONTWAIT) {
556 error = -EAGAIN;
557 break;
559 schedule();
560 if (signal_pending(current)) {
561 error = -ERESTARTSYS;
562 break;
564 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
565 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
566 !test_bit(ATM_VF_READY, &vcc->flags)) {
567 error = -EPIPE;
568 send_sig(SIGPIPE, current, 0);
569 break;
571 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
573 finish_wait(sk->sk_sleep, &wait);
574 if (error)
575 goto out;
576 skb->dev = NULL; /* for paths shared with net_device interfaces */
577 ATM_SKB(skb)->atm_options = vcc->atm_options;
578 if (copy_from_user(skb_put(skb, size), buff, size)) {
579 kfree_skb(skb);
580 error = -EFAULT;
581 goto out;
583 if (eff != size)
584 memset(skb->data + size, 0, eff-size);
585 error = vcc->dev->ops->send(vcc, skb);
586 error = error ? error : size;
587 out:
588 release_sock(sk);
589 return error;
592 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
594 struct sock *sk = sock->sk;
595 struct atm_vcc *vcc;
596 unsigned int mask;
598 sock_poll_wait(file, sk->sk_sleep, wait);
599 mask = 0;
601 vcc = ATM_SD(sock);
603 /* exceptional events */
604 if (sk->sk_err)
605 mask = POLLERR;
607 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
608 test_bit(ATM_VF_CLOSE, &vcc->flags))
609 mask |= POLLHUP;
611 /* readable? */
612 if (!skb_queue_empty(&sk->sk_receive_queue))
613 mask |= POLLIN | POLLRDNORM;
615 /* writable? */
616 if (sock->state == SS_CONNECTING &&
617 test_bit(ATM_VF_WAITING, &vcc->flags))
618 return mask;
620 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
621 vcc_writable(sk))
622 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
624 return mask;
627 static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
629 int error;
632 * Don't let the QoS change the already connected AAL type nor the
633 * traffic class.
635 if (qos->aal != vcc->qos.aal ||
636 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
637 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
638 return -EINVAL;
639 error = adjust_tp(&qos->txtp, qos->aal);
640 if (!error)
641 error = adjust_tp(&qos->rxtp, qos->aal);
642 if (error)
643 return error;
644 if (!vcc->dev->ops->change_qos)
645 return -EOPNOTSUPP;
646 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
647 return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
648 return svc_change_qos(vcc, qos);
651 static int check_tp(const struct atm_trafprm *tp)
653 /* @@@ Should be merged with adjust_tp */
654 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
655 return 0;
656 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
657 !tp->max_pcr)
658 return -EINVAL;
659 if (tp->min_pcr == ATM_MAX_PCR)
660 return -EINVAL;
661 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
662 tp->min_pcr > tp->max_pcr)
663 return -EINVAL;
665 * We allow pcr to be outside [min_pcr,max_pcr], because later
666 * adjustment may still push it in the valid range.
668 return 0;
671 static int check_qos(const struct atm_qos *qos)
673 int error;
675 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
676 return -EINVAL;
677 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
678 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
679 qos->txtp.traffic_class != ATM_ANYCLASS &&
680 qos->rxtp.traffic_class != ATM_ANYCLASS)
681 return -EINVAL;
682 error = check_tp(&qos->txtp);
683 if (error)
684 return error;
685 return check_tp(&qos->rxtp);
688 int vcc_setsockopt(struct socket *sock, int level, int optname,
689 char __user *optval, unsigned int optlen)
691 struct atm_vcc *vcc;
692 unsigned long value;
693 int error;
695 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
696 return -EINVAL;
698 vcc = ATM_SD(sock);
699 switch (optname) {
700 case SO_ATMQOS:
702 struct atm_qos qos;
704 if (copy_from_user(&qos, optval, sizeof(qos)))
705 return -EFAULT;
706 error = check_qos(&qos);
707 if (error)
708 return error;
709 if (sock->state == SS_CONNECTED)
710 return atm_change_qos(vcc, &qos);
711 if (sock->state != SS_UNCONNECTED)
712 return -EBADFD;
713 vcc->qos = qos;
714 set_bit(ATM_VF_HASQOS, &vcc->flags);
715 return 0;
717 case SO_SETCLP:
718 if (get_user(value, (unsigned long __user *)optval))
719 return -EFAULT;
720 if (value)
721 vcc->atm_options |= ATM_ATMOPT_CLP;
722 else
723 vcc->atm_options &= ~ATM_ATMOPT_CLP;
724 return 0;
725 default:
726 if (level == SOL_SOCKET)
727 return -EINVAL;
728 break;
730 if (!vcc->dev || !vcc->dev->ops->setsockopt)
731 return -EINVAL;
732 return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
735 int vcc_getsockopt(struct socket *sock, int level, int optname,
736 char __user *optval, int __user *optlen)
738 struct atm_vcc *vcc;
739 int len;
741 if (get_user(len, optlen))
742 return -EFAULT;
743 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
744 return -EINVAL;
746 vcc = ATM_SD(sock);
747 switch (optname) {
748 case SO_ATMQOS:
749 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
750 return -EINVAL;
751 return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
752 ? -EFAULT : 0;
753 case SO_SETCLP:
754 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
755 (unsigned long __user *)optval) ? -EFAULT : 0;
756 case SO_ATMPVC:
758 struct sockaddr_atmpvc pvc;
760 if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
761 return -ENOTCONN;
762 pvc.sap_family = AF_ATMPVC;
763 pvc.sap_addr.itf = vcc->dev->number;
764 pvc.sap_addr.vpi = vcc->vpi;
765 pvc.sap_addr.vci = vcc->vci;
766 return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
768 default:
769 if (level == SOL_SOCKET)
770 return -EINVAL;
771 break;
773 if (!vcc->dev || !vcc->dev->ops->getsockopt)
774 return -EINVAL;
775 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
778 static int __init atm_init(void)
780 int error;
782 error = proto_register(&vcc_proto, 0);
783 if (error < 0)
784 goto out;
785 error = atmpvc_init();
786 if (error < 0) {
787 pr_err("atmpvc_init() failed with %d\n", error);
788 goto out_unregister_vcc_proto;
790 error = atmsvc_init();
791 if (error < 0) {
792 pr_err("atmsvc_init() failed with %d\n", error);
793 goto out_atmpvc_exit;
795 error = atm_proc_init();
796 if (error < 0) {
797 pr_err("atm_proc_init() failed with %d\n", error);
798 goto out_atmsvc_exit;
800 error = atm_sysfs_init();
801 if (error < 0) {
802 pr_err("atm_sysfs_init() failed with %d\n", error);
803 goto out_atmproc_exit;
805 out:
806 return error;
807 out_atmproc_exit:
808 atm_proc_exit();
809 out_atmsvc_exit:
810 atmsvc_exit();
811 out_atmpvc_exit:
812 atmsvc_exit();
813 out_unregister_vcc_proto:
814 proto_unregister(&vcc_proto);
815 goto out;
818 static void __exit atm_exit(void)
820 atm_proc_exit();
821 atm_sysfs_exit();
822 atmsvc_exit();
823 atmpvc_exit();
824 proto_unregister(&vcc_proto);
827 subsys_initcall(atm_init);
829 module_exit(atm_exit);
831 MODULE_LICENSE("GPL");
832 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
833 MODULE_ALIAS_NETPROTO(PF_ATMSVC);