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_hashent = 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 vcc_remove_socket(sk); /* no more receive */
183 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
184 atm_return(vcc,skb->truesize);
188 module_put(vcc->dev->ops->owner);
189 atm_dev_put(vcc->dev);
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 static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
228 if (!tp->traffic_class) return 0;
231 max_sdu = ATM_CELL_SIZE-1;
234 max_sdu = ATM_MAX_AAL34_PDU;
237 printk(KERN_WARNING "ATM: AAL problems ... "
241 max_sdu = ATM_MAX_AAL5_PDU;
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;
250 static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
252 struct hlist_head *head = &vcc_hash[vci &
253 (VCC_HTABLE_SIZE - 1)];
254 struct hlist_node *node;
256 struct atm_vcc *walk;
258 sk_for_each(s, node, head) {
260 if (walk->dev != vcc->dev)
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)))
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) */
278 static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
280 static short p; /* poor man's per-device cache */
286 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
287 err = check_ci(vcc, *vpi, *vci);
290 /* last scan may have left values out of bounds for current device */
291 if (*vpi != ATM_VPI_ANY)
293 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
295 if (*vci != ATM_VCI_ANY)
297 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
302 if (!check_ci(vcc, p, c)) {
307 if (*vci == ATM_VCI_ANY) {
309 if (c >= 1 << vcc->dev->ci_range.vci_bits)
312 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
313 *vpi == ATM_VPI_ANY) {
315 if (p >= 1 << vcc->dev->ci_range.vpi_bits) p = 0;
318 while (old_p != p || old_c != c);
323 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
326 struct sock *sk = sk_atm(vcc);
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))
333 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
336 if (!try_module_get(dev->ops->owner))
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;
346 __vcc_insert_socket(sk);
347 write_unlock_irq(&vcc_sklist_lock);
348 switch (vcc->qos.aal) {
350 error = atm_init_aal0(vcc);
351 vcc->stats = &dev->stats.aal0;
354 error = atm_init_aal34(vcc);
355 vcc->stats = &dev->stats.aal34;
358 /* ATM_AAL5 is also used in the "0 for default" case */
359 vcc->qos.aal = ATM_AAL5;
362 error = atm_init_aal5(vcc);
363 vcc->stats = &dev->stats.aal5;
368 if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
369 if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
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);
378 if (dev->ops->open) {
379 if ((error = dev->ops->open(vcc)))
385 vcc_remove_socket(sk);
387 module_put(dev->ops->owner);
388 /* ensure we get dev module ref count correct */
394 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
397 struct atm_vcc *vcc = ATM_SD(sock);
400 DPRINTK("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
401 if (sock->state == SS_CONNECTED)
403 if (sock->state != SS_UNCONNECTED)
408 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
409 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
411 if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
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))
423 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
424 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
426 if (itf != ATM_ITF_ANY) {
427 dev = atm_dev_lookup(itf);
430 error = __vcc_connect(vcc, dev, vpi, vci);
436 struct list_head *p, *next;
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);
443 spin_unlock(&atm_dev_lock);
444 if (!__vcc_connect(vcc, dev, vpi, vci))
448 spin_lock(&atm_dev_lock);
450 spin_unlock(&atm_dev_lock);
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;
462 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
463 size_t size, int flags)
465 struct sock *sk = sock->sk;
468 int copied, error = -EINVAL;
470 if (sock->state != SS_CONNECTED)
472 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
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))
480 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
487 msg->msg_flags |= MSG_TRUNC;
490 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
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);
501 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
504 struct sock *sk = sock->sk;
509 const void __user *buff;
513 if (sock->state != SS_CONNECTED) {
521 if (m->msg_iovlen != 1) {
522 error = -ENOSYS; /* fix this later @@@ */
525 buff = m->msg_iov->iov_base;
526 size = m->msg_iov->iov_len;
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)) {
532 send_sig(SIGPIPE, current, 0);
539 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
544 eff = (size+3) & ~3; /* align to word boundary */
545 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
547 while (!(skb = alloc_tx(vcc,eff))) {
548 if (m->msg_flags & MSG_DONTWAIT) {
553 if (signal_pending(current)) {
554 error = -ERESTARTSYS;
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)) {
561 send_sig(SIGPIPE, current, 0);
564 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
566 finish_wait(sk->sk_sleep, &wait);
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)) {
576 if (eff != size) memset(skb->data+size,0,eff-size);
577 error = vcc->dev->ops->send(vcc,skb);
578 error = error ? error : size;
585 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
587 struct sock *sk = sock->sk;
591 poll_wait(file, sk->sk_sleep, wait);
596 /* exceptional events */
600 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
601 test_bit(ATM_VF_CLOSE, &vcc->flags))
605 if (!skb_queue_empty(&sk->sk_receive_queue))
606 mask |= POLLIN | POLLRDNORM;
609 if (sock->state == SS_CONNECTING &&
610 test_bit(ATM_VF_WAITING, &vcc->flags))
613 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
615 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
621 static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
626 * Don't let the QoS change the already connected AAL type nor the
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)
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);
643 static int check_tp(struct atm_trafprm *tp)
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;
653 * We allow pcr to be outside [min_pcr,max_pcr], because later
654 * adjustment may still push it in the valid range.
660 static int check_qos(struct atm_qos *qos)
664 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
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);
675 int vcc_setsockopt(struct socket *sock, int level, int optname,
676 char __user *optval, int optlen)
682 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
691 if (copy_from_user(&qos,optval,sizeof(qos)))
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)
700 set_bit(ATM_VF_HASQOS,&vcc->flags);
704 if (get_user(value,(unsigned long __user *)optval))
706 if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
707 else vcc->atm_options &= ~ATM_ATMOPT_CLP;
710 if (level == SOL_SOCKET) return -EINVAL;
713 if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
714 return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
718 int vcc_getsockopt(struct socket *sock, int level, int optname,
719 char __user *optval, int __user *optlen)
724 if (get_user(len, optlen))
726 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
732 if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
734 return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
737 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
738 0,(unsigned long __user *)optval) ? -EFAULT : 0;
741 struct sockaddr_atmpvc pvc;
744 !test_bit(ATM_VF_ADDR,&vcc->flags))
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)) ?
754 if (level == SOL_SOCKET) return -EINVAL;
757 if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
758 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
761 static int __init atm_init(void)
765 if ((error = proto_register(&vcc_proto, 0)) < 0)
768 if ((error = atmpvc_init()) < 0) {
769 printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
770 goto out_unregister_vcc_proto;
772 if ((error = atmsvc_init()) < 0) {
773 printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
774 goto out_atmpvc_exit;
776 if ((error = atm_proc_init()) < 0) {
777 printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
778 goto out_atmsvc_exit;
786 out_unregister_vcc_proto:
787 proto_unregister(&vcc_proto);
791 static void __exit atm_exit(void)
796 proto_unregister(&vcc_proto);
799 module_init(atm_init);
800 module_exit(atm_exit);
802 MODULE_LICENSE("GPL");
803 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
804 MODULE_ALIAS_NETPROTO(PF_ATMSVC);