1 /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
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>
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 */
23 #include <asm/uaccess.h>
24 #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 */
36 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
38 #define DPRINTK(format,args...)
41 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
42 DEFINE_RWLOCK(vcc_sklist_lock);
44 static void __vcc_insert_socket(struct sock *sk)
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);
53 void vcc_insert_socket(struct sock *sk)
55 write_lock_irq(&vcc_sklist_lock);
56 __vcc_insert_socket(sk);
57 write_unlock_irq(&vcc_sklist_lock);
60 static void vcc_remove_socket(struct sock *sk)
62 write_lock_irq(&vcc_sklist_lock);
64 write_unlock_irq(&vcc_sklist_lock);
68 static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
71 struct sock *sk = sk_atm(vcc);
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,
79 while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
80 DPRINTK("AlTx %d += %d\n", atomic_read(&sk->sk_wmem_alloc),
82 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
87 EXPORT_SYMBOL(vcc_hash);
88 EXPORT_SYMBOL(vcc_sklist_lock);
89 EXPORT_SYMBOL(vcc_insert_socket);
91 static void vcc_sock_destruct(struct sock *sk)
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));
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));
100 static void vcc_def_wakeup(struct sock *sk)
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);
108 static inline int vcc_writable(struct sock *sk)
110 struct atm_vcc *vcc = atm_sk(sk);
112 return (vcc->qos.txtp.max_sdu +
113 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
116 static void vcc_write_space(struct sock *sk)
118 read_lock(&sk->sk_callback_lock);
120 if (vcc_writable(sk)) {
121 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
122 wake_up_interruptible(sk->sk_sleep);
124 sk_wake_async(sk, 2, POLL_OUT);
127 read_unlock(&sk->sk_callback_lock);
130 static struct proto vcc_proto = {
132 .owner = THIS_MODULE,
133 .obj_size = sizeof(struct atm_vcc),
136 int vcc_create(struct socket *sock, int protocol, int family)
142 if (sock->type == SOCK_STREAM)
144 sk = sk_alloc(family, GFP_KERNEL, &vcc_proto, 1);
147 sock_init_data(sock, sk);
148 sk->sk_state_change = vcc_def_wakeup;
149 sk->sk_write_space = vcc_write_space;
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);
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;
168 static void vcc_destroy_socket(struct sock *sk)
170 struct atm_vcc *vcc = atm_sk(sk);
173 set_bit(ATM_VF_CLOSE, &vcc->flags);
174 clear_bit(ATM_VF_READY, &vcc->flags);
176 if (vcc->dev->ops->close)
177 vcc->dev->ops->close(vcc);
179 vcc->push(vcc, NULL); /* atmarpd has no push */
181 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
182 atm_return(vcc,skb->truesize);
186 module_put(vcc->dev->ops->owner);
187 atm_dev_put(vcc->dev);
190 vcc_remove_socket(sk);
194 int vcc_release(struct socket *sock)
196 struct sock *sk = sock->sk;
200 vcc_destroy_socket(sock->sk);
209 void vcc_release_async(struct atm_vcc *vcc, int reply)
211 struct sock *sk = sk_atm(vcc);
213 set_bit(ATM_VF_CLOSE, &vcc->flags);
214 sk->sk_shutdown |= RCV_SHUTDOWN;
216 clear_bit(ATM_VF_WAITING, &vcc->flags);
217 sk->sk_state_change(sk);
221 EXPORT_SYMBOL(vcc_release_async);
224 void atm_dev_release_vccs(struct atm_dev *dev)
228 write_lock_irq(&vcc_sklist_lock);
229 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
230 struct hlist_head *head = &vcc_hash[i];
231 struct hlist_node *node, *tmp;
235 sk_for_each_safe(s, node, tmp, head) {
237 if (vcc->dev == dev) {
238 vcc_release_async(vcc, -EPIPE);
243 write_unlock_irq(&vcc_sklist_lock);
247 static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
251 if (!tp->traffic_class) return 0;
254 max_sdu = ATM_CELL_SIZE-1;
257 max_sdu = ATM_MAX_AAL34_PDU;
260 printk(KERN_WARNING "ATM: AAL problems ... "
264 max_sdu = ATM_MAX_AAL5_PDU;
266 if (!tp->max_sdu) tp->max_sdu = max_sdu;
267 else if (tp->max_sdu > max_sdu) return -EINVAL;
268 if (!tp->max_cdv) tp->max_cdv = ATM_MAX_CDV;
273 static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
275 struct hlist_head *head = &vcc_hash[vci &
276 (VCC_HTABLE_SIZE - 1)];
277 struct hlist_node *node;
279 struct atm_vcc *walk;
281 sk_for_each(s, node, head) {
283 if (walk->dev != vcc->dev)
285 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
286 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
287 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
288 (walk->qos.rxtp.traffic_class != ATM_NONE &&
289 vcc->qos.rxtp.traffic_class != ATM_NONE)))
293 /* allow VCCs with same VPI/VCI iff they don't collide on
294 TX/RX (but we may refuse such sharing for other reasons,
295 e.g. if protocol requires to have both channels) */
301 static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
303 static short p; /* poor man's per-device cache */
309 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
310 err = check_ci(vcc, *vpi, *vci);
313 /* last scan may have left values out of bounds for current device */
314 if (*vpi != ATM_VPI_ANY)
316 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
318 if (*vci != ATM_VCI_ANY)
320 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
325 if (!check_ci(vcc, p, c)) {
330 if (*vci == ATM_VCI_ANY) {
332 if (c >= 1 << vcc->dev->ci_range.vci_bits)
335 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
336 *vpi == ATM_VPI_ANY) {
338 if (p >= 1 << vcc->dev->ci_range.vpi_bits) p = 0;
341 while (old_p != p || old_c != c);
346 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
349 struct sock *sk = sk_atm(vcc);
352 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
353 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
354 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
356 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
359 if (!try_module_get(dev->ops->owner))
362 write_lock_irq(&vcc_sklist_lock);
363 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
364 (error = find_ci(vcc, &vpi, &vci))) {
365 write_unlock_irq(&vcc_sklist_lock);
366 goto fail_module_put;
370 __vcc_insert_socket(sk);
371 write_unlock_irq(&vcc_sklist_lock);
372 switch (vcc->qos.aal) {
374 error = atm_init_aal0(vcc);
375 vcc->stats = &dev->stats.aal0;
378 error = atm_init_aal34(vcc);
379 vcc->stats = &dev->stats.aal34;
382 /* ATM_AAL5 is also used in the "0 for default" case */
383 vcc->qos.aal = ATM_AAL5;
386 error = atm_init_aal5(vcc);
387 vcc->stats = &dev->stats.aal5;
392 if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
393 if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
396 DPRINTK("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
397 DPRINTK(" TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
398 vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
399 DPRINTK(" RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
400 vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
402 if (dev->ops->open) {
403 if ((error = dev->ops->open(vcc)))
409 vcc_remove_socket(sk);
411 module_put(dev->ops->owner);
412 /* ensure we get dev module ref count correct */
418 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
421 struct atm_vcc *vcc = ATM_SD(sock);
424 DPRINTK("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
425 if (sock->state == SS_CONNECTED)
427 if (sock->state != SS_UNCONNECTED)
432 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
433 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
435 if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
437 DPRINTK("vcc_connect (TX: cl %d,bw %d-%d,sdu %d; "
438 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
439 vcc->qos.txtp.traffic_class,vcc->qos.txtp.min_pcr,
440 vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu,
441 vcc->qos.rxtp.traffic_class,vcc->qos.rxtp.min_pcr,
442 vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu,
443 vcc->qos.aal == ATM_AAL5 ? "" : vcc->qos.aal == ATM_AAL0 ? "" :
444 " ??? code ",vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
445 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
447 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
448 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
450 if (likely(itf != ATM_ITF_ANY)) {
451 dev = try_then_request_module(atm_dev_lookup(itf), "atm-device-%d", itf);
454 down(&atm_dev_mutex);
455 if (!list_empty(&atm_devs)) {
456 dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
463 error = __vcc_connect(vcc, dev, vpi, vci);
468 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
469 set_bit(ATM_VF_PARTIAL,&vcc->flags);
470 if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
471 sock->state = SS_CONNECTED;
476 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
477 size_t size, int flags)
479 struct sock *sk = sock->sk;
482 int copied, error = -EINVAL;
484 if (sock->state != SS_CONNECTED)
486 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
489 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
490 test_bit(ATM_VF_CLOSE,&vcc->flags) ||
491 !test_bit(ATM_VF_READY, &vcc->flags))
494 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
501 msg->msg_flags |= MSG_TRUNC;
504 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
507 sock_recv_timestamp(msg, sk, skb);
508 DPRINTK("RcvM %d -= %d\n", atomic_read(&sk->rmem_alloc), skb->truesize);
509 atm_return(vcc, skb->truesize);
510 skb_free_datagram(sk, skb);
515 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
518 struct sock *sk = sock->sk;
523 const void __user *buff;
527 if (sock->state != SS_CONNECTED) {
535 if (m->msg_iovlen != 1) {
536 error = -ENOSYS; /* fix this later @@@ */
539 buff = m->msg_iov->iov_base;
540 size = m->msg_iov->iov_len;
542 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
543 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
544 !test_bit(ATM_VF_READY, &vcc->flags)) {
546 send_sig(SIGPIPE, current, 0);
553 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
558 eff = (size+3) & ~3; /* align to word boundary */
559 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
561 while (!(skb = alloc_tx(vcc,eff))) {
562 if (m->msg_flags & MSG_DONTWAIT) {
567 if (signal_pending(current)) {
568 error = -ERESTARTSYS;
571 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
572 test_bit(ATM_VF_CLOSE,&vcc->flags) ||
573 !test_bit(ATM_VF_READY,&vcc->flags)) {
575 send_sig(SIGPIPE, current, 0);
578 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
580 finish_wait(sk->sk_sleep, &wait);
583 skb->dev = NULL; /* for paths shared with net_device interfaces */
584 ATM_SKB(skb)->atm_options = vcc->atm_options;
585 if (copy_from_user(skb_put(skb,size),buff,size)) {
590 if (eff != size) memset(skb->data+size,0,eff-size);
591 error = vcc->dev->ops->send(vcc,skb);
592 error = error ? error : size;
599 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
601 struct sock *sk = sock->sk;
605 poll_wait(file, sk->sk_sleep, wait);
610 /* exceptional events */
614 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
615 test_bit(ATM_VF_CLOSE, &vcc->flags))
619 if (!skb_queue_empty(&sk->sk_receive_queue))
620 mask |= POLLIN | POLLRDNORM;
623 if (sock->state == SS_CONNECTING &&
624 test_bit(ATM_VF_WAITING, &vcc->flags))
627 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
629 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
635 static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
640 * Don't let the QoS change the already connected AAL type nor the
643 if (qos->aal != vcc->qos.aal ||
644 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
645 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
647 error = adjust_tp(&qos->txtp,qos->aal);
648 if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
649 if (error) return error;
650 if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
651 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
652 return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
653 return svc_change_qos(vcc,qos);
657 static int check_tp(struct atm_trafprm *tp)
659 /* @@@ Should be merged with adjust_tp */
660 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
661 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
662 !tp->max_pcr) return -EINVAL;
663 if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
664 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
665 tp->min_pcr > tp->max_pcr) return -EINVAL;
667 * We allow pcr to be outside [min_pcr,max_pcr], because later
668 * adjustment may still push it in the valid range.
674 static int check_qos(struct atm_qos *qos)
678 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
680 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
681 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
682 qos->txtp.traffic_class != ATM_ANYCLASS &&
683 qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
684 error = check_tp(&qos->txtp);
685 if (error) return error;
686 return check_tp(&qos->rxtp);
689 int vcc_setsockopt(struct socket *sock, int level, int optname,
690 char __user *optval, int optlen)
696 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
705 if (copy_from_user(&qos,optval,sizeof(qos)))
707 error = check_qos(&qos);
708 if (error) return error;
709 if (sock->state == SS_CONNECTED)
710 return atm_change_qos(vcc,&qos);
711 if (sock->state != SS_UNCONNECTED)
714 set_bit(ATM_VF_HASQOS,&vcc->flags);
718 if (get_user(value,(unsigned long __user *)optval))
720 if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
721 else vcc->atm_options &= ~ATM_ATMOPT_CLP;
724 if (level == SOL_SOCKET) return -EINVAL;
727 if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
728 return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
732 int vcc_getsockopt(struct socket *sock, int level, int optname,
733 char __user *optval, int __user *optlen)
738 if (get_user(len, optlen))
740 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
746 if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
748 return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
751 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
752 0,(unsigned long __user *)optval) ? -EFAULT : 0;
755 struct sockaddr_atmpvc pvc;
758 !test_bit(ATM_VF_ADDR,&vcc->flags))
760 pvc.sap_family = AF_ATMPVC;
761 pvc.sap_addr.itf = vcc->dev->number;
762 pvc.sap_addr.vpi = vcc->vpi;
763 pvc.sap_addr.vci = vcc->vci;
764 return copy_to_user(optval,&pvc,sizeof(pvc)) ?
768 if (level == SOL_SOCKET) return -EINVAL;
771 if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
772 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
775 static int __init atm_init(void)
779 if ((error = proto_register(&vcc_proto, 0)) < 0)
782 if ((error = atmpvc_init()) < 0) {
783 printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
784 goto out_unregister_vcc_proto;
786 if ((error = atmsvc_init()) < 0) {
787 printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
788 goto out_atmpvc_exit;
790 if ((error = atm_proc_init()) < 0) {
791 printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
792 goto out_atmsvc_exit;
800 out_unregister_vcc_proto:
801 proto_unregister(&vcc_proto);
805 static void __exit atm_exit(void)
810 proto_unregister(&vcc_proto);
813 module_init(atm_init);
814 module_exit(atm_exit);
816 MODULE_LICENSE("GPL");
817 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
818 MODULE_ALIAS_NETPROTO(PF_ATMSVC);