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>
23 static struct proto mISDN_proto = {
26 .obj_size = sizeof(struct mISDN_sock)
29 #define _pms(sk) ((struct mISDN_sock *)sk)
31 static struct mISDN_sock_list data_sockets = {
32 .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
35 static struct mISDN_sock_list base_sockets = {
36 .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
39 #define L2_HEADER_LEN 4
41 static inline struct sk_buff *
42 _l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
46 skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
48 skb_reserve(skb, L2_HEADER_LEN);
53 mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
55 write_lock_bh(&l->lock);
56 sk_add_node(sk, &l->head);
57 write_unlock_bh(&l->lock);
60 static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
62 write_lock_bh(&l->lock);
64 write_unlock_bh(&l->lock);
68 mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
70 struct mISDN_sock *msk;
73 msk = container_of(ch, struct mISDN_sock, ch);
74 if (*debug & DEBUG_SOCKET)
75 printk(KERN_DEBUG "%s len %d %p\n", __func__, skb->len, skb);
76 if (msk->sk.sk_state == MISDN_CLOSED)
79 err = sock_queue_rcv_skb(&msk->sk, skb);
81 printk(KERN_WARNING "%s: error %d\n", __func__, err);
86 mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
88 struct mISDN_sock *msk;
90 msk = container_of(ch, struct mISDN_sock, ch);
91 if (*debug & DEBUG_SOCKET)
92 printk(KERN_DEBUG "%s(%p, %x, %p)\n", __func__, ch, cmd, arg);
95 msk->sk.sk_state = MISDN_CLOSED;
102 mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
106 if (_pms(sk)->cmask & MISDN_TIME_STAMP) {
107 skb_get_timestamp(skb, &tv);
108 put_cmsg(msg, SOL_MISDN, MISDN_TIME_STAMP, sizeof(tv), &tv);
113 mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
114 struct msghdr *msg, size_t len, int flags)
117 struct sock *sk = sock->sk;
118 struct sockaddr_mISDN *maddr;
122 if (*debug & DEBUG_SOCKET)
123 printk(KERN_DEBUG "%s: len %d, flags %x ch.nr %d, proto %x\n",
124 __func__, (int)len, flags, _pms(sk)->ch.nr,
126 if (flags & (MSG_OOB))
129 if (sk->sk_state == MISDN_CLOSED)
132 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
136 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
137 msg->msg_namelen = sizeof(struct sockaddr_mISDN);
138 maddr = (struct sockaddr_mISDN *)msg->msg_name;
139 maddr->family = AF_ISDN;
140 maddr->dev = _pms(sk)->dev->id;
141 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
142 (sk->sk_protocol == ISDN_P_LAPD_NT)) {
143 maddr->channel = (mISDN_HEAD_ID(skb) >> 16) & 0xff;
144 maddr->tei = (mISDN_HEAD_ID(skb) >> 8) & 0xff;
145 maddr->sapi = mISDN_HEAD_ID(skb) & 0xff;
147 maddr->channel = _pms(sk)->ch.nr;
148 maddr->sapi = _pms(sk)->ch.addr & 0xFF;
149 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xFF;
152 if (msg->msg_namelen)
153 printk(KERN_WARNING "%s: too small namelen %d\n",
154 __func__, msg->msg_namelen);
155 msg->msg_namelen = 0;
158 copied = skb->len + MISDN_HEADER_LEN;
160 if (flags & MSG_PEEK)
161 atomic_dec(&skb->users);
163 skb_queue_head(&sk->sk_receive_queue, skb);
166 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
169 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
171 mISDN_sock_cmsg(sk, msg, skb);
173 skb_free_datagram(sk, skb);
175 return err ? : copied;
179 mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
180 struct msghdr *msg, size_t len)
182 struct sock *sk = sock->sk;
185 struct sockaddr_mISDN *maddr;
187 if (*debug & DEBUG_SOCKET)
188 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
189 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
192 if (msg->msg_flags & MSG_OOB)
195 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
198 if (len < MISDN_HEADER_LEN)
201 if (sk->sk_state != MISDN_BOUND)
206 skb = _l2_alloc_skb(len, GFP_KERNEL);
210 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
215 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
216 skb_pull(skb, MISDN_HEADER_LEN);
218 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
219 /* if we have a address, we use it */
220 maddr = (struct sockaddr_mISDN *)msg->msg_name;
221 mISDN_HEAD_ID(skb) = maddr->channel;
222 } else { /* use default for L2 messages */
223 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
224 (sk->sk_protocol == ISDN_P_LAPD_NT))
225 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
228 if (*debug & DEBUG_SOCKET)
229 printk(KERN_DEBUG "%s: ID:%x\n",
230 __func__, mISDN_HEAD_ID(skb));
233 if (!_pms(sk)->ch.peer ||
234 (err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb)))
249 data_sock_release(struct socket *sock)
251 struct sock *sk = sock->sk;
253 if (*debug & DEBUG_SOCKET)
254 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
257 switch (sk->sk_protocol) {
262 if (sk->sk_state == MISDN_BOUND)
263 delete_channel(&_pms(sk)->ch);
265 mISDN_sock_unlink(&data_sockets, sk);
271 case ISDN_P_B_X75SLP:
272 case ISDN_P_B_L2DTMF:
274 case ISDN_P_B_L2DSPHDLC:
275 delete_channel(&_pms(sk)->ch);
276 mISDN_sock_unlink(&data_sockets, sk);
283 skb_queue_purge(&sk->sk_receive_queue);
292 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
294 struct mISDN_ctrl_req cq;
295 int err = -EINVAL, val;
296 struct mISDNchannel *bchan, *next;
299 if (!_pms(sk)->dev) {
305 if (copy_from_user(&cq, p, sizeof(cq))) {
309 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
310 list_for_each_entry_safe(bchan, next,
311 &_pms(sk)->dev->bchannels, list) {
312 if (bchan->nr == cq.channel) {
313 err = bchan->ctrl(bchan,
314 CONTROL_CHANNEL, &cq);
319 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
320 CONTROL_CHANNEL, &cq);
323 if (copy_to_user(p, &cq, sizeof(cq)))
327 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
331 if (get_user(val, (int __user *)p)) {
335 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
336 CONTROL_CHANNEL, &val);
348 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
351 struct sock *sk = sock->sk;
352 struct mISDNdevice *dev;
353 struct mISDNversion ver;
357 ver.major = MISDN_MAJOR_VERSION;
358 ver.minor = MISDN_MINOR_VERSION;
359 ver.release = MISDN_RELEASE;
360 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
364 id = get_mdevice_count();
365 if (put_user(id, (int __user *)arg))
369 if (get_user(id, (int __user *)arg)) {
373 dev = get_mdevice(id);
375 struct mISDN_devinfo di;
378 di.Dprotocols = dev->Dprotocols;
379 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
380 di.protocol = dev->D.protocol;
381 memcpy(di.channelmap, dev->channelmap,
382 sizeof(di.channelmap));
383 di.nrbchan = dev->nrbchan;
384 strcpy(di.name, dev_name(&dev->dev));
385 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
391 if (sk->sk_state == MISDN_BOUND)
392 err = data_sock_ioctl_bound(sk, cmd,
400 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
401 char __user *optval, int len)
403 struct sock *sk = sock->sk;
404 int err = 0, opt = 0;
406 if (*debug & DEBUG_SOCKET)
407 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
408 level, optname, optval, len);
413 case MISDN_TIME_STAMP:
414 if (get_user(opt, (int __user *)optval)) {
420 _pms(sk)->cmask |= MISDN_TIME_STAMP;
422 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
432 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
433 char __user *optval, int __user *optlen)
435 struct sock *sk = sock->sk;
438 if (get_user(len, optlen))
442 case MISDN_TIME_STAMP:
443 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
448 if (put_user(opt, optval))
459 data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
461 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
462 struct sock *sk = sock->sk;
463 struct hlist_node *node;
467 if (*debug & DEBUG_SOCKET)
468 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
469 if (addr_len != sizeof(struct sockaddr_mISDN))
471 if (!maddr || maddr->family != AF_ISDN)
480 _pms(sk)->dev = get_mdevice(maddr->dev);
481 if (!_pms(sk)->dev) {
486 if (sk->sk_protocol < ISDN_P_B_START) {
487 read_lock_bh(&data_sockets.lock);
488 sk_for_each(csk, node, &data_sockets.head) {
491 if (_pms(csk)->dev != _pms(sk)->dev)
493 if (csk->sk_protocol >= ISDN_P_B_START)
495 if (IS_ISDN_P_TE(csk->sk_protocol)
496 == IS_ISDN_P_TE(sk->sk_protocol))
498 read_unlock_bh(&data_sockets.lock);
502 read_unlock_bh(&data_sockets.lock);
505 _pms(sk)->ch.send = mISDN_send;
506 _pms(sk)->ch.ctrl = mISDN_ctrl;
508 switch (sk->sk_protocol) {
513 mISDN_sock_unlink(&data_sockets, sk);
514 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
515 sk->sk_protocol, maddr);
517 mISDN_sock_link(&data_sockets, sk);
521 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
522 sk->sk_protocol, maddr);
526 case ISDN_P_B_X75SLP:
527 case ISDN_P_B_L2DTMF:
529 case ISDN_P_B_L2DSPHDLC:
530 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
531 sk->sk_protocol, maddr);
534 err = -EPROTONOSUPPORT;
538 sk->sk_state = MISDN_BOUND;
539 _pms(sk)->ch.protocol = sk->sk_protocol;
547 data_sock_getname(struct socket *sock, struct sockaddr *addr,
548 int *addr_len, int peer)
550 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
551 struct sock *sk = sock->sk;
558 *addr_len = sizeof(*maddr);
559 maddr->dev = _pms(sk)->dev->id;
560 maddr->channel = _pms(sk)->ch.nr;
561 maddr->sapi = _pms(sk)->ch.addr & 0xff;
562 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
567 static const struct proto_ops data_sock_ops = {
569 .owner = THIS_MODULE,
570 .release = data_sock_release,
571 .ioctl = data_sock_ioctl,
572 .bind = data_sock_bind,
573 .getname = data_sock_getname,
574 .sendmsg = mISDN_sock_sendmsg,
575 .recvmsg = mISDN_sock_recvmsg,
576 .poll = datagram_poll,
577 .listen = sock_no_listen,
578 .shutdown = sock_no_shutdown,
579 .setsockopt = data_sock_setsockopt,
580 .getsockopt = data_sock_getsockopt,
581 .connect = sock_no_connect,
582 .socketpair = sock_no_socketpair,
583 .accept = sock_no_accept,
588 data_sock_create(struct net *net, struct socket *sock, int protocol)
592 if (sock->type != SOCK_DGRAM)
593 return -ESOCKTNOSUPPORT;
595 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
599 sock_init_data(sock, sk);
601 sock->ops = &data_sock_ops;
602 sock->state = SS_UNCONNECTED;
603 sock_reset_flag(sk, SOCK_ZAPPED);
605 sk->sk_protocol = protocol;
606 sk->sk_state = MISDN_OPEN;
607 mISDN_sock_link(&data_sockets, sk);
613 base_sock_release(struct socket *sock)
615 struct sock *sk = sock->sk;
617 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
621 mISDN_sock_unlink(&base_sockets, sk);
629 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
632 struct mISDNdevice *dev;
633 struct mISDNversion ver;
637 ver.major = MISDN_MAJOR_VERSION;
638 ver.minor = MISDN_MINOR_VERSION;
639 ver.release = MISDN_RELEASE;
640 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
644 id = get_mdevice_count();
645 if (put_user(id, (int __user *)arg))
649 if (get_user(id, (int __user *)arg)) {
653 dev = get_mdevice(id);
655 struct mISDN_devinfo di;
658 di.Dprotocols = dev->Dprotocols;
659 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
660 di.protocol = dev->D.protocol;
661 memcpy(di.channelmap, dev->channelmap,
662 sizeof(di.channelmap));
663 di.nrbchan = dev->nrbchan;
664 strcpy(di.name, dev_name(&dev->dev));
665 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
672 struct mISDN_devrename dn;
673 if (copy_from_user(&dn, (void __user *)arg,
678 dev = get_mdevice(dn.id);
680 err = device_rename(&dev->dev, dn.name);
692 base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
694 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
695 struct sock *sk = sock->sk;
698 if (!maddr || maddr->family != AF_ISDN)
708 _pms(sk)->dev = get_mdevice(maddr->dev);
709 if (!_pms(sk)->dev) {
713 sk->sk_state = MISDN_BOUND;
720 static const struct proto_ops base_sock_ops = {
722 .owner = THIS_MODULE,
723 .release = base_sock_release,
724 .ioctl = base_sock_ioctl,
725 .bind = base_sock_bind,
726 .getname = sock_no_getname,
727 .sendmsg = sock_no_sendmsg,
728 .recvmsg = sock_no_recvmsg,
729 .poll = sock_no_poll,
730 .listen = sock_no_listen,
731 .shutdown = sock_no_shutdown,
732 .setsockopt = sock_no_setsockopt,
733 .getsockopt = sock_no_getsockopt,
734 .connect = sock_no_connect,
735 .socketpair = sock_no_socketpair,
736 .accept = sock_no_accept,
742 base_sock_create(struct net *net, struct socket *sock, int protocol)
746 if (sock->type != SOCK_RAW)
747 return -ESOCKTNOSUPPORT;
749 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
753 sock_init_data(sock, sk);
754 sock->ops = &base_sock_ops;
755 sock->state = SS_UNCONNECTED;
756 sock_reset_flag(sk, SOCK_ZAPPED);
757 sk->sk_protocol = protocol;
758 sk->sk_state = MISDN_OPEN;
759 mISDN_sock_link(&base_sockets, sk);
765 mISDN_sock_create(struct net *net, struct socket *sock, int proto)
767 int err = -EPROTONOSUPPORT;
771 err = base_sock_create(net, sock, proto);
781 case ISDN_P_B_X75SLP:
782 case ISDN_P_B_L2DTMF:
784 case ISDN_P_B_L2DSPHDLC:
785 err = data_sock_create(net, sock, proto);
795 net_proto_family mISDN_sock_family_ops = {
796 .owner = THIS_MODULE,
798 .create = mISDN_sock_create,
802 misdn_sock_init(u_int *deb)
807 err = sock_register(&mISDN_sock_family_ops);
809 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
814 misdn_sock_cleanup(void)
816 sock_unregister(PF_ISDN);