3 * Author Karsten Keil <kkeil@novell.com>
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/mISDNif.h>
19 #include <linux/slab.h>
20 #include <linux/export.h>
25 static struct proto mISDN_proto = {
28 .obj_size = sizeof(struct mISDN_sock)
31 #define _pms(sk) ((struct mISDN_sock *)sk)
33 static struct mISDN_sock_list data_sockets = {
34 .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
37 static struct mISDN_sock_list base_sockets = {
38 .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
41 #define L2_HEADER_LEN 4
43 static inline struct sk_buff *
44 _l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
48 skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
50 skb_reserve(skb, L2_HEADER_LEN);
55 mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
57 write_lock_bh(&l->lock);
58 sk_add_node(sk, &l->head);
59 write_unlock_bh(&l->lock);
62 static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
64 write_lock_bh(&l->lock);
66 write_unlock_bh(&l->lock);
70 mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
72 struct mISDN_sock *msk;
75 msk = container_of(ch, struct mISDN_sock, ch);
76 if (*debug & DEBUG_SOCKET)
77 printk(KERN_DEBUG "%s len %d %p\n", __func__, skb->len, skb);
78 if (msk->sk.sk_state == MISDN_CLOSED)
81 err = sock_queue_rcv_skb(&msk->sk, skb);
83 printk(KERN_WARNING "%s: error %d\n", __func__, err);
88 mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
90 struct mISDN_sock *msk;
92 msk = container_of(ch, struct mISDN_sock, ch);
93 if (*debug & DEBUG_SOCKET)
94 printk(KERN_DEBUG "%s(%p, %x, %p)\n", __func__, ch, cmd, arg);
97 msk->sk.sk_state = MISDN_CLOSED;
104 mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
108 if (_pms(sk)->cmask & MISDN_TIME_STAMP) {
109 skb_get_timestamp(skb, &tv);
110 put_cmsg(msg, SOL_MISDN, MISDN_TIME_STAMP, sizeof(tv), &tv);
115 mISDN_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
119 struct sock *sk = sock->sk;
123 if (*debug & DEBUG_SOCKET)
124 printk(KERN_DEBUG "%s: len %d, flags %x ch.nr %d, proto %x\n",
125 __func__, (int)len, flags, _pms(sk)->ch.nr,
127 if (flags & (MSG_OOB))
130 if (sk->sk_state == MISDN_CLOSED)
133 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
138 DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
140 maddr->family = AF_ISDN;
141 maddr->dev = _pms(sk)->dev->id;
142 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
143 (sk->sk_protocol == ISDN_P_LAPD_NT)) {
144 maddr->channel = (mISDN_HEAD_ID(skb) >> 16) & 0xff;
145 maddr->tei = (mISDN_HEAD_ID(skb) >> 8) & 0xff;
146 maddr->sapi = mISDN_HEAD_ID(skb) & 0xff;
148 maddr->channel = _pms(sk)->ch.nr;
149 maddr->sapi = _pms(sk)->ch.addr & 0xFF;
150 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xFF;
152 msg->msg_namelen = sizeof(*maddr);
155 copied = skb->len + MISDN_HEADER_LEN;
157 if (flags & MSG_PEEK)
158 refcount_dec(&skb->users);
160 skb_queue_head(&sk->sk_receive_queue, skb);
163 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
166 err = skb_copy_datagram_msg(skb, 0, msg, copied);
168 mISDN_sock_cmsg(sk, msg, skb);
170 skb_free_datagram(sk, skb);
172 return err ? : copied;
176 mISDN_sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
178 struct sock *sk = sock->sk;
182 if (*debug & DEBUG_SOCKET)
183 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
184 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
187 if (msg->msg_flags & MSG_OOB)
190 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE))
193 if (len < MISDN_HEADER_LEN)
196 if (sk->sk_state != MISDN_BOUND)
201 skb = _l2_alloc_skb(len, GFP_KERNEL);
205 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
210 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
211 skb_pull(skb, MISDN_HEADER_LEN);
213 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
214 /* if we have a address, we use it */
215 DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
216 mISDN_HEAD_ID(skb) = maddr->channel;
217 } else { /* use default for L2 messages */
218 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
219 (sk->sk_protocol == ISDN_P_LAPD_NT))
220 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
223 if (*debug & DEBUG_SOCKET)
224 printk(KERN_DEBUG "%s: ID:%x\n",
225 __func__, mISDN_HEAD_ID(skb));
228 if (!_pms(sk)->ch.peer)
230 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
246 data_sock_release(struct socket *sock)
248 struct sock *sk = sock->sk;
250 if (*debug & DEBUG_SOCKET)
251 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
254 switch (sk->sk_protocol) {
259 if (sk->sk_state == MISDN_BOUND)
260 delete_channel(&_pms(sk)->ch);
262 mISDN_sock_unlink(&data_sockets, sk);
268 case ISDN_P_B_X75SLP:
269 case ISDN_P_B_L2DTMF:
271 case ISDN_P_B_L2DSPHDLC:
272 delete_channel(&_pms(sk)->ch);
273 mISDN_sock_unlink(&data_sockets, sk);
280 skb_queue_purge(&sk->sk_receive_queue);
289 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
291 struct mISDN_ctrl_req cq;
292 int err = -EINVAL, val[2];
293 struct mISDNchannel *bchan, *next;
296 if (!_pms(sk)->dev) {
302 if (copy_from_user(&cq, p, sizeof(cq))) {
306 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
307 list_for_each_entry_safe(bchan, next,
308 &_pms(sk)->dev->bchannels, list) {
309 if (bchan->nr == cq.channel) {
310 err = bchan->ctrl(bchan,
311 CONTROL_CHANNEL, &cq);
316 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
317 CONTROL_CHANNEL, &cq);
320 if (copy_to_user(p, &cq, sizeof(cq)))
324 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
329 if (get_user(val[1], (int __user *)p)) {
333 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
334 CONTROL_CHANNEL, val);
337 if (sk->sk_protocol != ISDN_P_LAPD_NT
338 && sk->sk_protocol != ISDN_P_LAPD_TE) {
343 if (get_user(val[1], (int __user *)p)) {
347 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
348 CONTROL_CHANNEL, val);
360 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
363 struct sock *sk = sock->sk;
364 struct mISDNdevice *dev;
365 struct mISDNversion ver;
369 ver.major = MISDN_MAJOR_VERSION;
370 ver.minor = MISDN_MINOR_VERSION;
371 ver.release = MISDN_RELEASE;
372 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
376 id = get_mdevice_count();
377 if (put_user(id, (int __user *)arg))
381 if (get_user(id, (int __user *)arg)) {
385 dev = get_mdevice(id);
387 struct mISDN_devinfo di;
389 memset(&di, 0, sizeof(di));
391 di.Dprotocols = dev->Dprotocols;
392 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
393 di.protocol = dev->D.protocol;
394 memcpy(di.channelmap, dev->channelmap,
395 sizeof(di.channelmap));
396 di.nrbchan = dev->nrbchan;
397 strscpy(di.name, dev_name(&dev->dev), sizeof(di.name));
398 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
404 if (sk->sk_state == MISDN_BOUND)
405 err = data_sock_ioctl_bound(sk, cmd,
413 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
414 char __user *optval, unsigned int len)
416 struct sock *sk = sock->sk;
417 int err = 0, opt = 0;
419 if (*debug & DEBUG_SOCKET)
420 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
421 level, optname, optval, len);
426 case MISDN_TIME_STAMP:
427 if (get_user(opt, (int __user *)optval)) {
433 _pms(sk)->cmask |= MISDN_TIME_STAMP;
435 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
445 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
446 char __user *optval, int __user *optlen)
448 struct sock *sk = sock->sk;
451 if (get_user(len, optlen))
454 if (len != sizeof(char))
458 case MISDN_TIME_STAMP:
459 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
464 if (put_user(opt, optval))
475 data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
477 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
478 struct sock *sk = sock->sk;
482 if (*debug & DEBUG_SOCKET)
483 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
484 if (addr_len != sizeof(struct sockaddr_mISDN))
486 if (!maddr || maddr->family != AF_ISDN)
495 _pms(sk)->dev = get_mdevice(maddr->dev);
496 if (!_pms(sk)->dev) {
501 if (sk->sk_protocol < ISDN_P_B_START) {
502 read_lock_bh(&data_sockets.lock);
503 sk_for_each(csk, &data_sockets.head) {
506 if (_pms(csk)->dev != _pms(sk)->dev)
508 if (csk->sk_protocol >= ISDN_P_B_START)
510 if (IS_ISDN_P_TE(csk->sk_protocol)
511 == IS_ISDN_P_TE(sk->sk_protocol))
513 read_unlock_bh(&data_sockets.lock);
517 read_unlock_bh(&data_sockets.lock);
520 _pms(sk)->ch.send = mISDN_send;
521 _pms(sk)->ch.ctrl = mISDN_ctrl;
523 switch (sk->sk_protocol) {
528 mISDN_sock_unlink(&data_sockets, sk);
529 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
530 sk->sk_protocol, maddr);
532 mISDN_sock_link(&data_sockets, sk);
536 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
537 sk->sk_protocol, maddr);
541 case ISDN_P_B_X75SLP:
542 case ISDN_P_B_L2DTMF:
544 case ISDN_P_B_L2DSPHDLC:
545 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
546 sk->sk_protocol, maddr);
549 err = -EPROTONOSUPPORT;
553 sk->sk_state = MISDN_BOUND;
554 _pms(sk)->ch.protocol = sk->sk_protocol;
562 data_sock_getname(struct socket *sock, struct sockaddr *addr,
565 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
566 struct sock *sk = sock->sk;
573 maddr->family = AF_ISDN;
574 maddr->dev = _pms(sk)->dev->id;
575 maddr->channel = _pms(sk)->ch.nr;
576 maddr->sapi = _pms(sk)->ch.addr & 0xff;
577 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
579 return sizeof(*maddr);
582 static const struct proto_ops data_sock_ops = {
584 .owner = THIS_MODULE,
585 .release = data_sock_release,
586 .ioctl = data_sock_ioctl,
587 .bind = data_sock_bind,
588 .getname = data_sock_getname,
589 .sendmsg = mISDN_sock_sendmsg,
590 .recvmsg = mISDN_sock_recvmsg,
591 .poll = datagram_poll,
592 .listen = sock_no_listen,
593 .shutdown = sock_no_shutdown,
594 .setsockopt = data_sock_setsockopt,
595 .getsockopt = data_sock_getsockopt,
596 .connect = sock_no_connect,
597 .socketpair = sock_no_socketpair,
598 .accept = sock_no_accept,
603 data_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
607 if (sock->type != SOCK_DGRAM)
608 return -ESOCKTNOSUPPORT;
610 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
614 sock_init_data(sock, sk);
616 sock->ops = &data_sock_ops;
617 sock->state = SS_UNCONNECTED;
618 sock_reset_flag(sk, SOCK_ZAPPED);
620 sk->sk_protocol = protocol;
621 sk->sk_state = MISDN_OPEN;
622 mISDN_sock_link(&data_sockets, sk);
628 base_sock_release(struct socket *sock)
630 struct sock *sk = sock->sk;
632 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
636 mISDN_sock_unlink(&base_sockets, sk);
644 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
647 struct mISDNdevice *dev;
648 struct mISDNversion ver;
652 ver.major = MISDN_MAJOR_VERSION;
653 ver.minor = MISDN_MINOR_VERSION;
654 ver.release = MISDN_RELEASE;
655 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
659 id = get_mdevice_count();
660 if (put_user(id, (int __user *)arg))
664 if (get_user(id, (int __user *)arg)) {
668 dev = get_mdevice(id);
670 struct mISDN_devinfo di;
672 memset(&di, 0, sizeof(di));
674 di.Dprotocols = dev->Dprotocols;
675 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
676 di.protocol = dev->D.protocol;
677 memcpy(di.channelmap, dev->channelmap,
678 sizeof(di.channelmap));
679 di.nrbchan = dev->nrbchan;
680 strscpy(di.name, dev_name(&dev->dev), sizeof(di.name));
681 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
688 struct mISDN_devrename dn;
689 if (copy_from_user(&dn, (void __user *)arg,
694 dn.name[sizeof(dn.name) - 1] = '\0';
695 dev = get_mdevice(dn.id);
697 err = device_rename(&dev->dev, dn.name);
709 base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
711 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
712 struct sock *sk = sock->sk;
715 if (addr_len < sizeof(struct sockaddr_mISDN))
718 if (!maddr || maddr->family != AF_ISDN)
728 _pms(sk)->dev = get_mdevice(maddr->dev);
729 if (!_pms(sk)->dev) {
733 sk->sk_state = MISDN_BOUND;
740 static const struct proto_ops base_sock_ops = {
742 .owner = THIS_MODULE,
743 .release = base_sock_release,
744 .ioctl = base_sock_ioctl,
745 .bind = base_sock_bind,
746 .getname = sock_no_getname,
747 .sendmsg = sock_no_sendmsg,
748 .recvmsg = sock_no_recvmsg,
749 .listen = sock_no_listen,
750 .shutdown = sock_no_shutdown,
751 .setsockopt = sock_no_setsockopt,
752 .getsockopt = sock_no_getsockopt,
753 .connect = sock_no_connect,
754 .socketpair = sock_no_socketpair,
755 .accept = sock_no_accept,
761 base_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
765 if (sock->type != SOCK_RAW)
766 return -ESOCKTNOSUPPORT;
767 if (!capable(CAP_NET_RAW))
770 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
774 sock_init_data(sock, sk);
775 sock->ops = &base_sock_ops;
776 sock->state = SS_UNCONNECTED;
777 sock_reset_flag(sk, SOCK_ZAPPED);
778 sk->sk_protocol = protocol;
779 sk->sk_state = MISDN_OPEN;
780 mISDN_sock_link(&base_sockets, sk);
786 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
788 int err = -EPROTONOSUPPORT;
792 err = base_sock_create(net, sock, proto, kern);
802 case ISDN_P_B_X75SLP:
803 case ISDN_P_B_L2DTMF:
805 case ISDN_P_B_L2DSPHDLC:
806 err = data_sock_create(net, sock, proto, kern);
815 static const struct net_proto_family mISDN_sock_family_ops = {
816 .owner = THIS_MODULE,
818 .create = mISDN_sock_create,
822 misdn_sock_init(u_int *deb)
827 err = sock_register(&mISDN_sock_family_ops);
829 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
834 misdn_sock_cleanup(void)
836 sock_unregister(PF_ISDN);