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/module.h>
7 #include <linux/kmod.h>
8 #include <linux/net.h> /* struct socket, struct proto_ops */
9 #include <linux/atm.h> /* ATM stuff */
10 #include <linux/atmdev.h>
11 #include <linux/socket.h> /* SOL_SOCKET */
12 #include <linux/errno.h> /* error codes */
13 #include <linux/capability.h>
15 #include <linux/sched.h>
16 #include <linux/time.h> /* struct timeval */
17 #include <linux/skbuff.h>
18 #include <linux/bitops.h>
19 #include <linux/init.h>
20 #include <net/sock.h> /* struct sock */
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
27 #include "resources.h" /* atm_find_dev */
28 #include "common.h" /* prototypes */
29 #include "protocols.h" /* atm_init_<transport> */
30 #include "addr.h" /* address registry */
31 #include "signaling.h" /* for WAITING and sigd_attach */
35 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
37 #define DPRINTK(format,args...)
40 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
41 DEFINE_RWLOCK(vcc_sklist_lock);
43 static void __vcc_insert_socket(struct sock *sk)
45 struct atm_vcc *vcc = atm_sk(sk);
46 struct hlist_head *head = &vcc_hash[vcc->vci &
47 (VCC_HTABLE_SIZE - 1)];
48 sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
49 sk_add_node(sk, head);
52 void vcc_insert_socket(struct sock *sk)
54 write_lock_irq(&vcc_sklist_lock);
55 __vcc_insert_socket(sk);
56 write_unlock_irq(&vcc_sklist_lock);
59 static void vcc_remove_socket(struct sock *sk)
61 write_lock_irq(&vcc_sklist_lock);
63 write_unlock_irq(&vcc_sklist_lock);
67 static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
70 struct sock *sk = sk_atm(vcc);
72 if (atomic_read(&sk->sk_wmem_alloc) && !atm_may_send(vcc, size)) {
73 DPRINTK("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
74 atomic_read(&sk->sk_wmem_alloc), size,
78 while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
79 DPRINTK("AlTx %d += %d\n", atomic_read(&sk->sk_wmem_alloc),
81 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
86 EXPORT_SYMBOL(vcc_hash);
87 EXPORT_SYMBOL(vcc_sklist_lock);
88 EXPORT_SYMBOL(vcc_insert_socket);
90 static void vcc_sock_destruct(struct sock *sk)
92 if (atomic_read(&sk->sk_rmem_alloc))
93 printk(KERN_DEBUG "vcc_sock_destruct: rmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_rmem_alloc));
95 if (atomic_read(&sk->sk_wmem_alloc))
96 printk(KERN_DEBUG "vcc_sock_destruct: wmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_wmem_alloc));
99 static void vcc_def_wakeup(struct sock *sk)
101 read_lock(&sk->sk_callback_lock);
102 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
103 wake_up(sk->sk_sleep);
104 read_unlock(&sk->sk_callback_lock);
107 static inline int vcc_writable(struct sock *sk)
109 struct atm_vcc *vcc = atm_sk(sk);
111 return (vcc->qos.txtp.max_sdu +
112 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
115 static void vcc_write_space(struct sock *sk)
117 read_lock(&sk->sk_callback_lock);
119 if (vcc_writable(sk)) {
120 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
121 wake_up_interruptible(sk->sk_sleep);
123 sk_wake_async(sk, 2, POLL_OUT);
126 read_unlock(&sk->sk_callback_lock);
129 static struct proto vcc_proto = {
131 .owner = THIS_MODULE,
132 .obj_size = sizeof(struct atm_vcc),
135 int vcc_create(struct socket *sock, int protocol, int family)
141 if (sock->type == SOCK_STREAM)
143 sk = sk_alloc(family, GFP_KERNEL, &vcc_proto, 1);
146 sock_init_data(sock, sk);
147 sk->sk_state_change = vcc_def_wakeup;
148 sk->sk_write_space = vcc_write_space;
152 memset(&vcc->local,0,sizeof(struct sockaddr_atmsvc));
153 memset(&vcc->remote,0,sizeof(struct sockaddr_atmsvc));
154 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
155 atomic_set(&sk->sk_wmem_alloc, 0);
156 atomic_set(&sk->sk_rmem_alloc, 0);
159 vcc->push_oam = NULL;
160 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
161 vcc->atm_options = vcc->aal_options = 0;
162 sk->sk_destruct = vcc_sock_destruct;
167 static void vcc_destroy_socket(struct sock *sk)
169 struct atm_vcc *vcc = atm_sk(sk);
172 set_bit(ATM_VF_CLOSE, &vcc->flags);
173 clear_bit(ATM_VF_READY, &vcc->flags);
175 if (vcc->dev->ops->close)
176 vcc->dev->ops->close(vcc);
178 vcc->push(vcc, NULL); /* atmarpd has no push */
180 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
181 atm_return(vcc,skb->truesize);
185 module_put(vcc->dev->ops->owner);
186 atm_dev_put(vcc->dev);
189 vcc_remove_socket(sk);
193 int vcc_release(struct socket *sock)
195 struct sock *sk = sock->sk;
199 vcc_destroy_socket(sock->sk);
208 void vcc_release_async(struct atm_vcc *vcc, int reply)
210 struct sock *sk = sk_atm(vcc);
212 set_bit(ATM_VF_CLOSE, &vcc->flags);
213 sk->sk_shutdown |= RCV_SHUTDOWN;
215 clear_bit(ATM_VF_WAITING, &vcc->flags);
216 sk->sk_state_change(sk);
220 EXPORT_SYMBOL(vcc_release_async);
223 void atm_dev_release_vccs(struct atm_dev *dev)
227 write_lock_irq(&vcc_sklist_lock);
228 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
229 struct hlist_head *head = &vcc_hash[i];
230 struct hlist_node *node, *tmp;
234 sk_for_each_safe(s, node, tmp, head) {
236 if (vcc->dev == dev) {
237 vcc_release_async(vcc, -EPIPE);
242 write_unlock_irq(&vcc_sklist_lock);
246 static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
250 if (!tp->traffic_class) return 0;
253 max_sdu = ATM_CELL_SIZE-1;
256 max_sdu = ATM_MAX_AAL34_PDU;
259 printk(KERN_WARNING "ATM: AAL problems ... "
263 max_sdu = ATM_MAX_AAL5_PDU;
265 if (!tp->max_sdu) tp->max_sdu = max_sdu;
266 else if (tp->max_sdu > max_sdu) return -EINVAL;
267 if (!tp->max_cdv) tp->max_cdv = ATM_MAX_CDV;
272 static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
274 struct hlist_head *head = &vcc_hash[vci &
275 (VCC_HTABLE_SIZE - 1)];
276 struct hlist_node *node;
278 struct atm_vcc *walk;
280 sk_for_each(s, node, head) {
282 if (walk->dev != vcc->dev)
284 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
285 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
286 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
287 (walk->qos.rxtp.traffic_class != ATM_NONE &&
288 vcc->qos.rxtp.traffic_class != ATM_NONE)))
292 /* allow VCCs with same VPI/VCI iff they don't collide on
293 TX/RX (but we may refuse such sharing for other reasons,
294 e.g. if protocol requires to have both channels) */
300 static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
302 static short p; /* poor man's per-device cache */
308 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
309 err = check_ci(vcc, *vpi, *vci);
312 /* last scan may have left values out of bounds for current device */
313 if (*vpi != ATM_VPI_ANY)
315 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
317 if (*vci != ATM_VCI_ANY)
319 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
324 if (!check_ci(vcc, p, c)) {
329 if (*vci == ATM_VCI_ANY) {
331 if (c >= 1 << vcc->dev->ci_range.vci_bits)
334 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
335 *vpi == ATM_VPI_ANY) {
337 if (p >= 1 << vcc->dev->ci_range.vpi_bits) p = 0;
340 while (old_p != p || old_c != c);
345 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
348 struct sock *sk = sk_atm(vcc);
351 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
352 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
353 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
355 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
358 if (!try_module_get(dev->ops->owner))
361 write_lock_irq(&vcc_sklist_lock);
362 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
363 (error = find_ci(vcc, &vpi, &vci))) {
364 write_unlock_irq(&vcc_sklist_lock);
365 goto fail_module_put;
369 __vcc_insert_socket(sk);
370 write_unlock_irq(&vcc_sklist_lock);
371 switch (vcc->qos.aal) {
373 error = atm_init_aal0(vcc);
374 vcc->stats = &dev->stats.aal0;
377 error = atm_init_aal34(vcc);
378 vcc->stats = &dev->stats.aal34;
381 /* ATM_AAL5 is also used in the "0 for default" case */
382 vcc->qos.aal = ATM_AAL5;
385 error = atm_init_aal5(vcc);
386 vcc->stats = &dev->stats.aal5;
391 if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
392 if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
395 DPRINTK("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
396 DPRINTK(" TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
397 vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
398 DPRINTK(" RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
399 vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
401 if (dev->ops->open) {
402 if ((error = dev->ops->open(vcc)))
408 vcc_remove_socket(sk);
410 module_put(dev->ops->owner);
411 /* ensure we get dev module ref count correct */
417 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
420 struct atm_vcc *vcc = ATM_SD(sock);
423 DPRINTK("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
424 if (sock->state == SS_CONNECTED)
426 if (sock->state != SS_UNCONNECTED)
431 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
432 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
434 if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
436 DPRINTK("vcc_connect (TX: cl %d,bw %d-%d,sdu %d; "
437 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
438 vcc->qos.txtp.traffic_class,vcc->qos.txtp.min_pcr,
439 vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu,
440 vcc->qos.rxtp.traffic_class,vcc->qos.rxtp.min_pcr,
441 vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu,
442 vcc->qos.aal == ATM_AAL5 ? "" : vcc->qos.aal == ATM_AAL0 ? "" :
443 " ??? code ",vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
444 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
446 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
447 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
449 if (likely(itf != ATM_ITF_ANY)) {
450 dev = try_then_request_module(atm_dev_lookup(itf), "atm-device-%d", itf);
453 mutex_lock(&atm_dev_mutex);
454 if (!list_empty(&atm_devs)) {
455 dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
458 mutex_unlock(&atm_dev_mutex);
462 error = __vcc_connect(vcc, dev, vpi, vci);
467 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
468 set_bit(ATM_VF_PARTIAL,&vcc->flags);
469 if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
470 sock->state = SS_CONNECTED;
475 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
476 size_t size, int flags)
478 struct sock *sk = sock->sk;
481 int copied, error = -EINVAL;
483 if (sock->state != SS_CONNECTED)
485 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
488 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
489 test_bit(ATM_VF_CLOSE,&vcc->flags) ||
490 !test_bit(ATM_VF_READY, &vcc->flags))
493 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
500 msg->msg_flags |= MSG_TRUNC;
503 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
506 sock_recv_timestamp(msg, sk, skb);
507 DPRINTK("RcvM %d -= %d\n", atomic_read(&sk->rmem_alloc), skb->truesize);
508 atm_return(vcc, skb->truesize);
509 skb_free_datagram(sk, skb);
514 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
517 struct sock *sk = sock->sk;
522 const void __user *buff;
526 if (sock->state != SS_CONNECTED) {
534 if (m->msg_iovlen != 1) {
535 error = -ENOSYS; /* fix this later @@@ */
538 buff = m->msg_iov->iov_base;
539 size = m->msg_iov->iov_len;
541 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
542 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
543 !test_bit(ATM_VF_READY, &vcc->flags)) {
545 send_sig(SIGPIPE, current, 0);
552 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
557 eff = (size+3) & ~3; /* align to word boundary */
558 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
560 while (!(skb = alloc_tx(vcc,eff))) {
561 if (m->msg_flags & MSG_DONTWAIT) {
566 if (signal_pending(current)) {
567 error = -ERESTARTSYS;
570 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
571 test_bit(ATM_VF_CLOSE,&vcc->flags) ||
572 !test_bit(ATM_VF_READY,&vcc->flags)) {
574 send_sig(SIGPIPE, current, 0);
577 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
579 finish_wait(sk->sk_sleep, &wait);
582 skb->dev = NULL; /* for paths shared with net_device interfaces */
583 ATM_SKB(skb)->atm_options = vcc->atm_options;
584 if (copy_from_user(skb_put(skb,size),buff,size)) {
589 if (eff != size) memset(skb->data+size,0,eff-size);
590 error = vcc->dev->ops->send(vcc,skb);
591 error = error ? error : size;
598 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
600 struct sock *sk = sock->sk;
604 poll_wait(file, sk->sk_sleep, wait);
609 /* exceptional events */
613 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
614 test_bit(ATM_VF_CLOSE, &vcc->flags))
618 if (!skb_queue_empty(&sk->sk_receive_queue))
619 mask |= POLLIN | POLLRDNORM;
622 if (sock->state == SS_CONNECTING &&
623 test_bit(ATM_VF_WAITING, &vcc->flags))
626 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
628 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
634 static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
639 * Don't let the QoS change the already connected AAL type nor the
642 if (qos->aal != vcc->qos.aal ||
643 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
644 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
646 error = adjust_tp(&qos->txtp,qos->aal);
647 if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
648 if (error) return error;
649 if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
650 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
651 return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
652 return svc_change_qos(vcc,qos);
656 static int check_tp(struct atm_trafprm *tp)
658 /* @@@ Should be merged with adjust_tp */
659 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
660 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
661 !tp->max_pcr) return -EINVAL;
662 if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
663 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
664 tp->min_pcr > tp->max_pcr) return -EINVAL;
666 * We allow pcr to be outside [min_pcr,max_pcr], because later
667 * adjustment may still push it in the valid range.
673 static int check_qos(struct atm_qos *qos)
677 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
679 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
680 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
681 qos->txtp.traffic_class != ATM_ANYCLASS &&
682 qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
683 error = check_tp(&qos->txtp);
684 if (error) return error;
685 return check_tp(&qos->rxtp);
688 int vcc_setsockopt(struct socket *sock, int level, int optname,
689 char __user *optval, int optlen)
695 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
704 if (copy_from_user(&qos,optval,sizeof(qos)))
706 error = check_qos(&qos);
707 if (error) return error;
708 if (sock->state == SS_CONNECTED)
709 return atm_change_qos(vcc,&qos);
710 if (sock->state != SS_UNCONNECTED)
713 set_bit(ATM_VF_HASQOS,&vcc->flags);
717 if (get_user(value,(unsigned long __user *)optval))
719 if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
720 else vcc->atm_options &= ~ATM_ATMOPT_CLP;
723 if (level == SOL_SOCKET) return -EINVAL;
726 if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
727 return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
731 int vcc_getsockopt(struct socket *sock, int level, int optname,
732 char __user *optval, int __user *optlen)
737 if (get_user(len, optlen))
739 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
745 if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
747 return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
750 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
751 0,(unsigned long __user *)optval) ? -EFAULT : 0;
754 struct sockaddr_atmpvc pvc;
757 !test_bit(ATM_VF_ADDR,&vcc->flags))
759 pvc.sap_family = AF_ATMPVC;
760 pvc.sap_addr.itf = vcc->dev->number;
761 pvc.sap_addr.vpi = vcc->vpi;
762 pvc.sap_addr.vci = vcc->vci;
763 return copy_to_user(optval,&pvc,sizeof(pvc)) ?
767 if (level == SOL_SOCKET) return -EINVAL;
770 if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
771 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
774 static int __init atm_init(void)
778 if ((error = proto_register(&vcc_proto, 0)) < 0)
781 if ((error = atmpvc_init()) < 0) {
782 printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
783 goto out_unregister_vcc_proto;
785 if ((error = atmsvc_init()) < 0) {
786 printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
787 goto out_atmpvc_exit;
789 if ((error = atm_proc_init()) < 0) {
790 printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
791 goto out_atmsvc_exit;
793 if ((error = atm_sysfs_init()) < 0) {
794 printk(KERN_ERR "atm_sysfs_init() failed with %d\n",error);
795 goto out_atmproc_exit;
805 out_unregister_vcc_proto:
806 proto_unregister(&vcc_proto);
810 static void __exit atm_exit(void)
816 proto_unregister(&vcc_proto);
819 module_init(atm_init);
820 module_exit(atm_exit);
822 MODULE_LICENSE("GPL");
823 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
824 MODULE_ALIAS_NETPROTO(PF_ATMSVC);