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 */
33 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
34 DEFINE_RWLOCK(vcc_sklist_lock);
36 static void __vcc_insert_socket(struct sock *sk)
38 struct atm_vcc *vcc = atm_sk(sk);
39 struct hlist_head *head = &vcc_hash[vcc->vci &
40 (VCC_HTABLE_SIZE - 1)];
41 sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
42 sk_add_node(sk, head);
45 void vcc_insert_socket(struct sock *sk)
47 write_lock_irq(&vcc_sklist_lock);
48 __vcc_insert_socket(sk);
49 write_unlock_irq(&vcc_sklist_lock);
52 static void vcc_remove_socket(struct sock *sk)
54 write_lock_irq(&vcc_sklist_lock);
56 write_unlock_irq(&vcc_sklist_lock);
60 static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
63 struct sock *sk = sk_atm(vcc);
65 if (atomic_read(&sk->sk_wmem_alloc) && !atm_may_send(vcc, size)) {
66 pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
67 atomic_read(&sk->sk_wmem_alloc), size,
71 while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
72 pr_debug("AlTx %d += %d\n", atomic_read(&sk->sk_wmem_alloc),
74 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
79 EXPORT_SYMBOL(vcc_hash);
80 EXPORT_SYMBOL(vcc_sklist_lock);
81 EXPORT_SYMBOL(vcc_insert_socket);
83 static void vcc_sock_destruct(struct sock *sk)
85 if (atomic_read(&sk->sk_rmem_alloc))
86 printk(KERN_DEBUG "vcc_sock_destruct: rmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_rmem_alloc));
88 if (atomic_read(&sk->sk_wmem_alloc))
89 printk(KERN_DEBUG "vcc_sock_destruct: wmem leakage (%d bytes) detected.\n", atomic_read(&sk->sk_wmem_alloc));
92 static void vcc_def_wakeup(struct sock *sk)
94 read_lock(&sk->sk_callback_lock);
95 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
96 wake_up(sk->sk_sleep);
97 read_unlock(&sk->sk_callback_lock);
100 static inline int vcc_writable(struct sock *sk)
102 struct atm_vcc *vcc = atm_sk(sk);
104 return (vcc->qos.txtp.max_sdu +
105 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
108 static void vcc_write_space(struct sock *sk)
110 read_lock(&sk->sk_callback_lock);
112 if (vcc_writable(sk)) {
113 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
114 wake_up_interruptible(sk->sk_sleep);
116 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
119 read_unlock(&sk->sk_callback_lock);
122 static struct proto vcc_proto = {
124 .owner = THIS_MODULE,
125 .obj_size = sizeof(struct atm_vcc),
128 int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
134 if (sock->type == SOCK_STREAM)
136 sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
139 sock_init_data(sock, sk);
140 sk->sk_state_change = vcc_def_wakeup;
141 sk->sk_write_space = vcc_write_space;
145 memset(&vcc->local,0,sizeof(struct sockaddr_atmsvc));
146 memset(&vcc->remote,0,sizeof(struct sockaddr_atmsvc));
147 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
148 atomic_set(&sk->sk_wmem_alloc, 0);
149 atomic_set(&sk->sk_rmem_alloc, 0);
152 vcc->push_oam = NULL;
153 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
154 vcc->atm_options = vcc->aal_options = 0;
155 sk->sk_destruct = vcc_sock_destruct;
160 static void vcc_destroy_socket(struct sock *sk)
162 struct atm_vcc *vcc = atm_sk(sk);
165 set_bit(ATM_VF_CLOSE, &vcc->flags);
166 clear_bit(ATM_VF_READY, &vcc->flags);
168 if (vcc->dev->ops->close)
169 vcc->dev->ops->close(vcc);
171 vcc->push(vcc, NULL); /* atmarpd has no push */
173 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
174 atm_return(vcc,skb->truesize);
178 module_put(vcc->dev->ops->owner);
179 atm_dev_put(vcc->dev);
182 vcc_remove_socket(sk);
186 int vcc_release(struct socket *sock)
188 struct sock *sk = sock->sk;
192 vcc_destroy_socket(sock->sk);
201 void vcc_release_async(struct atm_vcc *vcc, int reply)
203 struct sock *sk = sk_atm(vcc);
205 set_bit(ATM_VF_CLOSE, &vcc->flags);
206 sk->sk_shutdown |= RCV_SHUTDOWN;
208 clear_bit(ATM_VF_WAITING, &vcc->flags);
209 sk->sk_state_change(sk);
213 EXPORT_SYMBOL(vcc_release_async);
216 void atm_dev_release_vccs(struct atm_dev *dev)
220 write_lock_irq(&vcc_sklist_lock);
221 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
222 struct hlist_head *head = &vcc_hash[i];
223 struct hlist_node *node, *tmp;
227 sk_for_each_safe(s, node, tmp, head) {
229 if (vcc->dev == dev) {
230 vcc_release_async(vcc, -EPIPE);
235 write_unlock_irq(&vcc_sklist_lock);
239 static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
243 if (!tp->traffic_class) return 0;
246 max_sdu = ATM_CELL_SIZE-1;
249 max_sdu = ATM_MAX_AAL34_PDU;
252 printk(KERN_WARNING "ATM: AAL problems ... "
256 max_sdu = ATM_MAX_AAL5_PDU;
258 if (!tp->max_sdu) tp->max_sdu = max_sdu;
259 else if (tp->max_sdu > max_sdu) return -EINVAL;
260 if (!tp->max_cdv) tp->max_cdv = ATM_MAX_CDV;
265 static int check_ci(struct atm_vcc *vcc, short vpi, int vci)
267 struct hlist_head *head = &vcc_hash[vci &
268 (VCC_HTABLE_SIZE - 1)];
269 struct hlist_node *node;
271 struct atm_vcc *walk;
273 sk_for_each(s, node, head) {
275 if (walk->dev != vcc->dev)
277 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
278 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
279 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
280 (walk->qos.rxtp.traffic_class != ATM_NONE &&
281 vcc->qos.rxtp.traffic_class != ATM_NONE)))
285 /* allow VCCs with same VPI/VCI iff they don't collide on
286 TX/RX (but we may refuse such sharing for other reasons,
287 e.g. if protocol requires to have both channels) */
293 static int find_ci(struct atm_vcc *vcc, short *vpi, int *vci)
295 static short p; /* poor man's per-device cache */
301 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
302 err = check_ci(vcc, *vpi, *vci);
305 /* last scan may have left values out of bounds for current device */
306 if (*vpi != ATM_VPI_ANY)
308 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
310 if (*vci != ATM_VCI_ANY)
312 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
317 if (!check_ci(vcc, p, c)) {
322 if (*vci == ATM_VCI_ANY) {
324 if (c >= 1 << vcc->dev->ci_range.vci_bits)
327 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
328 *vpi == ATM_VPI_ANY) {
330 if (p >= 1 << vcc->dev->ci_range.vpi_bits) p = 0;
333 while (old_p != p || old_c != c);
338 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
341 struct sock *sk = sk_atm(vcc);
344 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
345 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
346 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
348 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
351 if (!try_module_get(dev->ops->owner))
354 write_lock_irq(&vcc_sklist_lock);
355 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
356 (error = find_ci(vcc, &vpi, &vci))) {
357 write_unlock_irq(&vcc_sklist_lock);
358 goto fail_module_put;
362 __vcc_insert_socket(sk);
363 write_unlock_irq(&vcc_sklist_lock);
364 switch (vcc->qos.aal) {
366 error = atm_init_aal0(vcc);
367 vcc->stats = &dev->stats.aal0;
370 error = atm_init_aal34(vcc);
371 vcc->stats = &dev->stats.aal34;
374 /* ATM_AAL5 is also used in the "0 for default" case */
375 vcc->qos.aal = ATM_AAL5;
378 error = atm_init_aal5(vcc);
379 vcc->stats = &dev->stats.aal5;
384 if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
385 if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
388 pr_debug("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
389 pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
390 vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
391 pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
392 vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
394 if (dev->ops->open) {
395 if ((error = dev->ops->open(vcc)))
401 vcc_remove_socket(sk);
403 module_put(dev->ops->owner);
404 /* ensure we get dev module ref count correct */
410 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
413 struct atm_vcc *vcc = ATM_SD(sock);
416 pr_debug("vcc_connect (vpi %d, vci %d)\n",vpi,vci);
417 if (sock->state == SS_CONNECTED)
419 if (sock->state != SS_UNCONNECTED)
424 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
425 clear_bit(ATM_VF_PARTIAL,&vcc->flags);
427 if (test_bit(ATM_VF_PARTIAL,&vcc->flags))
429 pr_debug("vcc_connect (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 ? "" : vcc->qos.aal == ATM_AAL0 ? "" :
436 " ??? code ",vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
437 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
439 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
440 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
442 if (likely(itf != ATM_ITF_ANY)) {
443 dev = try_then_request_module(atm_dev_lookup(itf), "atm-device-%d", itf);
446 mutex_lock(&atm_dev_mutex);
447 if (!list_empty(&atm_devs)) {
448 dev = list_entry(atm_devs.next, struct atm_dev, dev_list);
451 mutex_unlock(&atm_dev_mutex);
455 error = __vcc_connect(vcc, dev, vpi, vci);
460 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
461 set_bit(ATM_VF_PARTIAL,&vcc->flags);
462 if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
463 sock->state = SS_CONNECTED;
468 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
469 size_t size, int flags)
471 struct sock *sk = sock->sk;
474 int copied, error = -EINVAL;
476 if (sock->state != SS_CONNECTED)
478 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
481 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
482 test_bit(ATM_VF_CLOSE,&vcc->flags) ||
483 !test_bit(ATM_VF_READY, &vcc->flags))
486 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
493 msg->msg_flags |= MSG_TRUNC;
496 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
499 sock_recv_timestamp(msg, sk, skb);
500 pr_debug("RcvM %d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
501 atm_return(vcc, skb->truesize);
502 skb_free_datagram(sk, skb);
507 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
510 struct sock *sk = sock->sk;
515 const void __user *buff;
519 if (sock->state != SS_CONNECTED) {
527 if (m->msg_iovlen != 1) {
528 error = -ENOSYS; /* fix this later @@@ */
531 buff = m->msg_iov->iov_base;
532 size = m->msg_iov->iov_len;
534 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
535 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
536 !test_bit(ATM_VF_READY, &vcc->flags)) {
538 send_sig(SIGPIPE, current, 0);
545 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
550 eff = (size+3) & ~3; /* align to word boundary */
551 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
553 while (!(skb = alloc_tx(vcc,eff))) {
554 if (m->msg_flags & MSG_DONTWAIT) {
559 if (signal_pending(current)) {
560 error = -ERESTARTSYS;
563 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
564 test_bit(ATM_VF_CLOSE,&vcc->flags) ||
565 !test_bit(ATM_VF_READY,&vcc->flags)) {
567 send_sig(SIGPIPE, current, 0);
570 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
572 finish_wait(sk->sk_sleep, &wait);
575 skb->dev = NULL; /* for paths shared with net_device interfaces */
576 ATM_SKB(skb)->atm_options = vcc->atm_options;
577 if (copy_from_user(skb_put(skb,size),buff,size)) {
582 if (eff != size) memset(skb->data+size,0,eff-size);
583 error = vcc->dev->ops->send(vcc,skb);
584 error = error ? error : size;
591 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
593 struct sock *sk = sock->sk;
597 poll_wait(file, sk->sk_sleep, wait);
602 /* exceptional events */
606 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
607 test_bit(ATM_VF_CLOSE, &vcc->flags))
611 if (!skb_queue_empty(&sk->sk_receive_queue))
612 mask |= POLLIN | POLLRDNORM;
615 if (sock->state == SS_CONNECTING &&
616 test_bit(ATM_VF_WAITING, &vcc->flags))
619 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
621 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
627 static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
632 * Don't let the QoS change the already connected AAL type nor the
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)
639 error = adjust_tp(&qos->txtp,qos->aal);
640 if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
641 if (error) return error;
642 if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
643 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
644 return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
645 return svc_change_qos(vcc,qos);
649 static int check_tp(struct atm_trafprm *tp)
651 /* @@@ Should be merged with adjust_tp */
652 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
653 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
654 !tp->max_pcr) return -EINVAL;
655 if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
656 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
657 tp->min_pcr > tp->max_pcr) return -EINVAL;
659 * We allow pcr to be outside [min_pcr,max_pcr], because later
660 * adjustment may still push it in the valid range.
666 static int check_qos(struct atm_qos *qos)
670 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
672 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
673 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
674 qos->txtp.traffic_class != ATM_ANYCLASS &&
675 qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
676 error = check_tp(&qos->txtp);
677 if (error) return error;
678 return check_tp(&qos->rxtp);
681 int vcc_setsockopt(struct socket *sock, int level, int optname,
682 char __user *optval, int optlen)
688 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
697 if (copy_from_user(&qos,optval,sizeof(qos)))
699 error = check_qos(&qos);
700 if (error) return error;
701 if (sock->state == SS_CONNECTED)
702 return atm_change_qos(vcc,&qos);
703 if (sock->state != SS_UNCONNECTED)
706 set_bit(ATM_VF_HASQOS,&vcc->flags);
710 if (get_user(value,(unsigned long __user *)optval))
712 if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
713 else vcc->atm_options &= ~ATM_ATMOPT_CLP;
716 if (level == SOL_SOCKET) return -EINVAL;
719 if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
720 return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
724 int vcc_getsockopt(struct socket *sock, int level, int optname,
725 char __user *optval, int __user *optlen)
730 if (get_user(len, optlen))
732 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
738 if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
740 return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
743 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
744 0,(unsigned long __user *)optval) ? -EFAULT : 0;
747 struct sockaddr_atmpvc pvc;
750 !test_bit(ATM_VF_ADDR,&vcc->flags))
752 pvc.sap_family = AF_ATMPVC;
753 pvc.sap_addr.itf = vcc->dev->number;
754 pvc.sap_addr.vpi = vcc->vpi;
755 pvc.sap_addr.vci = vcc->vci;
756 return copy_to_user(optval,&pvc,sizeof(pvc)) ?
760 if (level == SOL_SOCKET) return -EINVAL;
763 if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
764 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
767 static int __init atm_init(void)
771 if ((error = proto_register(&vcc_proto, 0)) < 0)
774 if ((error = atmpvc_init()) < 0) {
775 printk(KERN_ERR "atmpvc_init() failed with %d\n", error);
776 goto out_unregister_vcc_proto;
778 if ((error = atmsvc_init()) < 0) {
779 printk(KERN_ERR "atmsvc_init() failed with %d\n", error);
780 goto out_atmpvc_exit;
782 if ((error = atm_proc_init()) < 0) {
783 printk(KERN_ERR "atm_proc_init() failed with %d\n",error);
784 goto out_atmsvc_exit;
786 if ((error = atm_sysfs_init()) < 0) {
787 printk(KERN_ERR "atm_sysfs_init() failed with %d\n",error);
788 goto out_atmproc_exit;
798 out_unregister_vcc_proto:
799 proto_unregister(&vcc_proto);
803 static void __exit atm_exit(void)
809 proto_unregister(&vcc_proto);
812 subsys_initcall(atm_init);
814 module_exit(atm_exit);
816 MODULE_LICENSE("GPL");
817 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
818 MODULE_ALIAS_NETPROTO(PF_ATMSVC);