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>
24 static struct proto mISDN_proto = {
27 .obj_size = sizeof(struct mISDN_sock)
30 #define _pms(sk) ((struct mISDN_sock *)sk)
32 static struct mISDN_sock_list data_sockets = {
33 .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
36 static struct mISDN_sock_list base_sockets = {
37 .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
40 #define L2_HEADER_LEN 4
42 static inline struct sk_buff *
43 _l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
47 skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
49 skb_reserve(skb, L2_HEADER_LEN);
54 mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
56 write_lock_bh(&l->lock);
57 sk_add_node(sk, &l->head);
58 write_unlock_bh(&l->lock);
61 static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
63 write_lock_bh(&l->lock);
65 write_unlock_bh(&l->lock);
69 mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
71 struct mISDN_sock *msk;
74 msk = container_of(ch, struct mISDN_sock, ch);
75 if (*debug & DEBUG_SOCKET)
76 printk(KERN_DEBUG "%s len %d %p\n", __func__, skb->len, skb);
77 if (msk->sk.sk_state == MISDN_CLOSED)
80 err = sock_queue_rcv_skb(&msk->sk, skb);
82 printk(KERN_WARNING "%s: error %d\n", __func__, err);
87 mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
89 struct mISDN_sock *msk;
91 msk = container_of(ch, struct mISDN_sock, ch);
92 if (*debug & DEBUG_SOCKET)
93 printk(KERN_DEBUG "%s(%p, %x, %p)\n", __func__, ch, cmd, arg);
96 msk->sk.sk_state = MISDN_CLOSED;
103 mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
107 if (_pms(sk)->cmask & MISDN_TIME_STAMP) {
108 skb_get_timestamp(skb, &tv);
109 put_cmsg(msg, SOL_MISDN, MISDN_TIME_STAMP, sizeof(tv), &tv);
114 mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
115 struct msghdr *msg, size_t len, int flags)
118 struct sock *sk = sock->sk;
119 struct sockaddr_mISDN *maddr;
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);
137 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
138 msg->msg_namelen = sizeof(struct sockaddr_mISDN);
139 maddr = (struct sockaddr_mISDN *)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;
153 if (msg->msg_namelen)
154 printk(KERN_WARNING "%s: too small namelen %d\n",
155 __func__, msg->msg_namelen);
156 msg->msg_namelen = 0;
159 copied = skb->len + MISDN_HEADER_LEN;
161 if (flags & MSG_PEEK)
162 atomic_dec(&skb->users);
164 skb_queue_head(&sk->sk_receive_queue, skb);
167 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
170 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
172 mISDN_sock_cmsg(sk, msg, skb);
174 skb_free_datagram(sk, skb);
176 return err ? : copied;
180 mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
181 struct msghdr *msg, size_t len)
183 struct sock *sk = sock->sk;
186 struct sockaddr_mISDN *maddr;
188 if (*debug & DEBUG_SOCKET)
189 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
190 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
193 if (msg->msg_flags & MSG_OOB)
196 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
199 if (len < MISDN_HEADER_LEN)
202 if (sk->sk_state != MISDN_BOUND)
207 skb = _l2_alloc_skb(len, GFP_KERNEL);
211 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
216 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
217 skb_pull(skb, MISDN_HEADER_LEN);
219 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
220 /* if we have a address, we use it */
221 maddr = (struct sockaddr_mISDN *)msg->msg_name;
222 mISDN_HEAD_ID(skb) = maddr->channel;
223 } else { /* use default for L2 messages */
224 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
225 (sk->sk_protocol == ISDN_P_LAPD_NT))
226 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
229 if (*debug & DEBUG_SOCKET)
230 printk(KERN_DEBUG "%s: ID:%x\n",
231 __func__, mISDN_HEAD_ID(skb));
234 if (!_pms(sk)->ch.peer)
236 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
252 data_sock_release(struct socket *sock)
254 struct sock *sk = sock->sk;
256 if (*debug & DEBUG_SOCKET)
257 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
260 switch (sk->sk_protocol) {
265 if (sk->sk_state == MISDN_BOUND)
266 delete_channel(&_pms(sk)->ch);
268 mISDN_sock_unlink(&data_sockets, sk);
274 case ISDN_P_B_X75SLP:
275 case ISDN_P_B_L2DTMF:
277 case ISDN_P_B_L2DSPHDLC:
278 delete_channel(&_pms(sk)->ch);
279 mISDN_sock_unlink(&data_sockets, sk);
286 skb_queue_purge(&sk->sk_receive_queue);
295 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
297 struct mISDN_ctrl_req cq;
298 int err = -EINVAL, val[2];
299 struct mISDNchannel *bchan, *next;
302 if (!_pms(sk)->dev) {
308 if (copy_from_user(&cq, p, sizeof(cq))) {
312 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
313 list_for_each_entry_safe(bchan, next,
314 &_pms(sk)->dev->bchannels, list) {
315 if (bchan->nr == cq.channel) {
316 err = bchan->ctrl(bchan,
317 CONTROL_CHANNEL, &cq);
322 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
323 CONTROL_CHANNEL, &cq);
326 if (copy_to_user(p, &cq, sizeof(cq)))
330 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
335 if (get_user(val[1], (int __user *)p)) {
339 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
340 CONTROL_CHANNEL, val);
343 if (sk->sk_protocol != ISDN_P_LAPD_NT
344 && sk->sk_protocol != ISDN_P_LAPD_TE) {
349 if (get_user(val[1], (int __user *)p)) {
353 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
354 CONTROL_CHANNEL, val);
366 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
369 struct sock *sk = sock->sk;
370 struct mISDNdevice *dev;
371 struct mISDNversion ver;
375 ver.major = MISDN_MAJOR_VERSION;
376 ver.minor = MISDN_MINOR_VERSION;
377 ver.release = MISDN_RELEASE;
378 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
382 id = get_mdevice_count();
383 if (put_user(id, (int __user *)arg))
387 if (get_user(id, (int __user *)arg)) {
391 dev = get_mdevice(id);
393 struct mISDN_devinfo di;
395 memset(&di, 0, sizeof(di));
397 di.Dprotocols = dev->Dprotocols;
398 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
399 di.protocol = dev->D.protocol;
400 memcpy(di.channelmap, dev->channelmap,
401 sizeof(di.channelmap));
402 di.nrbchan = dev->nrbchan;
403 strcpy(di.name, dev_name(&dev->dev));
404 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
410 if (sk->sk_state == MISDN_BOUND)
411 err = data_sock_ioctl_bound(sk, cmd,
419 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
420 char __user *optval, unsigned int len)
422 struct sock *sk = sock->sk;
423 int err = 0, opt = 0;
425 if (*debug & DEBUG_SOCKET)
426 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
427 level, optname, optval, len);
432 case MISDN_TIME_STAMP:
433 if (get_user(opt, (int __user *)optval)) {
439 _pms(sk)->cmask |= MISDN_TIME_STAMP;
441 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
451 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
452 char __user *optval, int __user *optlen)
454 struct sock *sk = sock->sk;
457 if (get_user(len, optlen))
461 case MISDN_TIME_STAMP:
462 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
467 if (put_user(opt, optval))
478 data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
480 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
481 struct sock *sk = sock->sk;
482 struct hlist_node *node;
486 if (*debug & DEBUG_SOCKET)
487 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
488 if (addr_len != sizeof(struct sockaddr_mISDN))
490 if (!maddr || maddr->family != AF_ISDN)
499 _pms(sk)->dev = get_mdevice(maddr->dev);
500 if (!_pms(sk)->dev) {
505 if (sk->sk_protocol < ISDN_P_B_START) {
506 read_lock_bh(&data_sockets.lock);
507 sk_for_each(csk, node, &data_sockets.head) {
510 if (_pms(csk)->dev != _pms(sk)->dev)
512 if (csk->sk_protocol >= ISDN_P_B_START)
514 if (IS_ISDN_P_TE(csk->sk_protocol)
515 == IS_ISDN_P_TE(sk->sk_protocol))
517 read_unlock_bh(&data_sockets.lock);
521 read_unlock_bh(&data_sockets.lock);
524 _pms(sk)->ch.send = mISDN_send;
525 _pms(sk)->ch.ctrl = mISDN_ctrl;
527 switch (sk->sk_protocol) {
532 mISDN_sock_unlink(&data_sockets, sk);
533 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
534 sk->sk_protocol, maddr);
536 mISDN_sock_link(&data_sockets, sk);
540 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
541 sk->sk_protocol, maddr);
545 case ISDN_P_B_X75SLP:
546 case ISDN_P_B_L2DTMF:
548 case ISDN_P_B_L2DSPHDLC:
549 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
550 sk->sk_protocol, maddr);
553 err = -EPROTONOSUPPORT;
557 sk->sk_state = MISDN_BOUND;
558 _pms(sk)->ch.protocol = sk->sk_protocol;
566 data_sock_getname(struct socket *sock, struct sockaddr *addr,
567 int *addr_len, int peer)
569 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
570 struct sock *sk = sock->sk;
577 *addr_len = sizeof(*maddr);
578 maddr->dev = _pms(sk)->dev->id;
579 maddr->channel = _pms(sk)->ch.nr;
580 maddr->sapi = _pms(sk)->ch.addr & 0xff;
581 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
586 static const struct proto_ops data_sock_ops = {
588 .owner = THIS_MODULE,
589 .release = data_sock_release,
590 .ioctl = data_sock_ioctl,
591 .bind = data_sock_bind,
592 .getname = data_sock_getname,
593 .sendmsg = mISDN_sock_sendmsg,
594 .recvmsg = mISDN_sock_recvmsg,
595 .poll = datagram_poll,
596 .listen = sock_no_listen,
597 .shutdown = sock_no_shutdown,
598 .setsockopt = data_sock_setsockopt,
599 .getsockopt = data_sock_getsockopt,
600 .connect = sock_no_connect,
601 .socketpair = sock_no_socketpair,
602 .accept = sock_no_accept,
607 data_sock_create(struct net *net, struct socket *sock, int protocol)
611 if (sock->type != SOCK_DGRAM)
612 return -ESOCKTNOSUPPORT;
614 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
618 sock_init_data(sock, sk);
620 sock->ops = &data_sock_ops;
621 sock->state = SS_UNCONNECTED;
622 sock_reset_flag(sk, SOCK_ZAPPED);
624 sk->sk_protocol = protocol;
625 sk->sk_state = MISDN_OPEN;
626 mISDN_sock_link(&data_sockets, sk);
632 base_sock_release(struct socket *sock)
634 struct sock *sk = sock->sk;
636 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
640 mISDN_sock_unlink(&base_sockets, sk);
648 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
651 struct mISDNdevice *dev;
652 struct mISDNversion ver;
656 ver.major = MISDN_MAJOR_VERSION;
657 ver.minor = MISDN_MINOR_VERSION;
658 ver.release = MISDN_RELEASE;
659 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
663 id = get_mdevice_count();
664 if (put_user(id, (int __user *)arg))
668 if (get_user(id, (int __user *)arg)) {
672 dev = get_mdevice(id);
674 struct mISDN_devinfo di;
676 memset(&di, 0, sizeof(di));
678 di.Dprotocols = dev->Dprotocols;
679 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
680 di.protocol = dev->D.protocol;
681 memcpy(di.channelmap, dev->channelmap,
682 sizeof(di.channelmap));
683 di.nrbchan = dev->nrbchan;
684 strcpy(di.name, dev_name(&dev->dev));
685 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
692 struct mISDN_devrename dn;
693 if (copy_from_user(&dn, (void __user *)arg,
698 dev = get_mdevice(dn.id);
700 err = device_rename(&dev->dev, dn.name);
712 base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
714 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
715 struct sock *sk = sock->sk;
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 .poll = sock_no_poll,
750 .listen = sock_no_listen,
751 .shutdown = sock_no_shutdown,
752 .setsockopt = sock_no_setsockopt,
753 .getsockopt = sock_no_getsockopt,
754 .connect = sock_no_connect,
755 .socketpair = sock_no_socketpair,
756 .accept = sock_no_accept,
762 base_sock_create(struct net *net, struct socket *sock, int protocol)
766 if (sock->type != SOCK_RAW)
767 return -ESOCKTNOSUPPORT;
769 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
773 sock_init_data(sock, sk);
774 sock->ops = &base_sock_ops;
775 sock->state = SS_UNCONNECTED;
776 sock_reset_flag(sk, SOCK_ZAPPED);
777 sk->sk_protocol = protocol;
778 sk->sk_state = MISDN_OPEN;
779 mISDN_sock_link(&base_sockets, sk);
785 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
787 int err = -EPROTONOSUPPORT;
791 err = base_sock_create(net, sock, proto);
801 case ISDN_P_B_X75SLP:
802 case ISDN_P_B_L2DTMF:
804 case ISDN_P_B_L2DSPHDLC:
805 err = data_sock_create(net, sock, proto);
814 static const struct net_proto_family mISDN_sock_family_ops = {
815 .owner = THIS_MODULE,
817 .create = mISDN_sock_create,
821 misdn_sock_init(u_int *deb)
826 err = sock_register(&mISDN_sock_family_ops);
828 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
833 misdn_sock_cleanup(void)
835 sock_unregister(PF_ISDN);