Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / isdn / mISDN / socket.c
blob1be82284cf9dfaa8ab5f5078bed78c3aec095ed3
1 /*
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>
21 #include "core.h"
23 static u_int *debug;
25 static struct proto mISDN_proto = {
26 .name = "misdn",
27 .owner = THIS_MODULE,
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)
46 struct sk_buff *skb;
48 skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
49 if (likely(skb))
50 skb_reserve(skb, L2_HEADER_LEN);
51 return skb;
54 static void
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);
65 sk_del_node_init(sk);
66 write_unlock_bh(&l->lock);
69 static int
70 mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
72 struct mISDN_sock *msk;
73 int err;
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)
79 return -EUNATCH;
80 __net_timestamp(skb);
81 err = sock_queue_rcv_skb(&msk->sk, skb);
82 if (err)
83 printk(KERN_WARNING "%s: error %d\n", __func__, err);
84 return err;
87 static int
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);
95 switch (cmd) {
96 case CLOSE_CHANNEL:
97 msk->sk.sk_state = MISDN_CLOSED;
98 break;
100 return 0;
103 static inline void
104 mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
106 struct timeval tv;
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);
114 static int
115 mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
116 struct msghdr *msg, size_t len, int flags)
118 struct sk_buff *skb;
119 struct sock *sk = sock->sk;
121 int copied, err;
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,
126 sk->sk_protocol);
127 if (flags & (MSG_OOB))
128 return -EOPNOTSUPP;
130 if (sk->sk_state == MISDN_CLOSED)
131 return 0;
133 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
134 if (!skb)
135 return err;
137 if (msg->msg_name) {
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;
147 } else {
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;
156 if (len < copied) {
157 if (flags & MSG_PEEK)
158 atomic_dec(&skb->users);
159 else
160 skb_queue_head(&sk->sk_receive_queue, skb);
161 return -ENOSPC;
163 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
164 MISDN_HEADER_LEN);
166 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
168 mISDN_sock_cmsg(sk, msg, skb);
170 skb_free_datagram(sk, skb);
172 return err ? : copied;
175 static int
176 mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
177 struct msghdr *msg, size_t len)
179 struct sock *sk = sock->sk;
180 struct sk_buff *skb;
181 int err = -ENOMEM;
183 if (*debug & DEBUG_SOCKET)
184 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
185 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
186 sk->sk_protocol);
188 if (msg->msg_flags & MSG_OOB)
189 return -EOPNOTSUPP;
191 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE))
192 return -EINVAL;
194 if (len < MISDN_HEADER_LEN)
195 return -EINVAL;
197 if (sk->sk_state != MISDN_BOUND)
198 return -EBADFD;
200 lock_sock(sk);
202 skb = _l2_alloc_skb(len, GFP_KERNEL);
203 if (!skb)
204 goto done;
206 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
207 err = -EFAULT;
208 goto done;
211 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
212 skb_pull(skb, MISDN_HEADER_LEN);
214 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
215 /* if we have a address, we use it */
216 DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
217 mISDN_HEAD_ID(skb) = maddr->channel;
218 } else { /* use default for L2 messages */
219 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
220 (sk->sk_protocol == ISDN_P_LAPD_NT))
221 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
224 if (*debug & DEBUG_SOCKET)
225 printk(KERN_DEBUG "%s: ID:%x\n",
226 __func__, mISDN_HEAD_ID(skb));
228 err = -ENODEV;
229 if (!_pms(sk)->ch.peer)
230 goto done;
231 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
232 if (err)
233 goto done;
234 else {
235 skb = NULL;
236 err = len;
239 done:
240 if (skb)
241 kfree_skb(skb);
242 release_sock(sk);
243 return err;
246 static int
247 data_sock_release(struct socket *sock)
249 struct sock *sk = sock->sk;
251 if (*debug & DEBUG_SOCKET)
252 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
253 if (!sk)
254 return 0;
255 switch (sk->sk_protocol) {
256 case ISDN_P_TE_S0:
257 case ISDN_P_NT_S0:
258 case ISDN_P_TE_E1:
259 case ISDN_P_NT_E1:
260 if (sk->sk_state == MISDN_BOUND)
261 delete_channel(&_pms(sk)->ch);
262 else
263 mISDN_sock_unlink(&data_sockets, sk);
264 break;
265 case ISDN_P_LAPD_TE:
266 case ISDN_P_LAPD_NT:
267 case ISDN_P_B_RAW:
268 case ISDN_P_B_HDLC:
269 case ISDN_P_B_X75SLP:
270 case ISDN_P_B_L2DTMF:
271 case ISDN_P_B_L2DSP:
272 case ISDN_P_B_L2DSPHDLC:
273 delete_channel(&_pms(sk)->ch);
274 mISDN_sock_unlink(&data_sockets, sk);
275 break;
278 lock_sock(sk);
280 sock_orphan(sk);
281 skb_queue_purge(&sk->sk_receive_queue);
283 release_sock(sk);
284 sock_put(sk);
286 return 0;
289 static int
290 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
292 struct mISDN_ctrl_req cq;
293 int err = -EINVAL, val[2];
294 struct mISDNchannel *bchan, *next;
296 lock_sock(sk);
297 if (!_pms(sk)->dev) {
298 err = -ENODEV;
299 goto done;
301 switch (cmd) {
302 case IMCTRLREQ:
303 if (copy_from_user(&cq, p, sizeof(cq))) {
304 err = -EFAULT;
305 break;
307 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
308 list_for_each_entry_safe(bchan, next,
309 &_pms(sk)->dev->bchannels, list) {
310 if (bchan->nr == cq.channel) {
311 err = bchan->ctrl(bchan,
312 CONTROL_CHANNEL, &cq);
313 break;
316 } else
317 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
318 CONTROL_CHANNEL, &cq);
319 if (err)
320 break;
321 if (copy_to_user(p, &cq, sizeof(cq)))
322 err = -EFAULT;
323 break;
324 case IMCLEAR_L2:
325 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
326 err = -EINVAL;
327 break;
329 val[0] = cmd;
330 if (get_user(val[1], (int __user *)p)) {
331 err = -EFAULT;
332 break;
334 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
335 CONTROL_CHANNEL, val);
336 break;
337 case IMHOLD_L1:
338 if (sk->sk_protocol != ISDN_P_LAPD_NT
339 && sk->sk_protocol != ISDN_P_LAPD_TE) {
340 err = -EINVAL;
341 break;
343 val[0] = cmd;
344 if (get_user(val[1], (int __user *)p)) {
345 err = -EFAULT;
346 break;
348 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
349 CONTROL_CHANNEL, val);
350 break;
351 default:
352 err = -EINVAL;
353 break;
355 done:
356 release_sock(sk);
357 return err;
360 static int
361 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
363 int err = 0, id;
364 struct sock *sk = sock->sk;
365 struct mISDNdevice *dev;
366 struct mISDNversion ver;
368 switch (cmd) {
369 case IMGETVERSION:
370 ver.major = MISDN_MAJOR_VERSION;
371 ver.minor = MISDN_MINOR_VERSION;
372 ver.release = MISDN_RELEASE;
373 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
374 err = -EFAULT;
375 break;
376 case IMGETCOUNT:
377 id = get_mdevice_count();
378 if (put_user(id, (int __user *)arg))
379 err = -EFAULT;
380 break;
381 case IMGETDEVINFO:
382 if (get_user(id, (int __user *)arg)) {
383 err = -EFAULT;
384 break;
386 dev = get_mdevice(id);
387 if (dev) {
388 struct mISDN_devinfo di;
390 memset(&di, 0, sizeof(di));
391 di.id = dev->id;
392 di.Dprotocols = dev->Dprotocols;
393 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
394 di.protocol = dev->D.protocol;
395 memcpy(di.channelmap, dev->channelmap,
396 sizeof(di.channelmap));
397 di.nrbchan = dev->nrbchan;
398 strcpy(di.name, dev_name(&dev->dev));
399 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
400 err = -EFAULT;
401 } else
402 err = -ENODEV;
403 break;
404 default:
405 if (sk->sk_state == MISDN_BOUND)
406 err = data_sock_ioctl_bound(sk, cmd,
407 (void __user *)arg);
408 else
409 err = -ENOTCONN;
411 return err;
414 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
415 char __user *optval, unsigned int len)
417 struct sock *sk = sock->sk;
418 int err = 0, opt = 0;
420 if (*debug & DEBUG_SOCKET)
421 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
422 level, optname, optval, len);
424 lock_sock(sk);
426 switch (optname) {
427 case MISDN_TIME_STAMP:
428 if (get_user(opt, (int __user *)optval)) {
429 err = -EFAULT;
430 break;
433 if (opt)
434 _pms(sk)->cmask |= MISDN_TIME_STAMP;
435 else
436 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
437 break;
438 default:
439 err = -ENOPROTOOPT;
440 break;
442 release_sock(sk);
443 return err;
446 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
447 char __user *optval, int __user *optlen)
449 struct sock *sk = sock->sk;
450 int len, opt;
452 if (get_user(len, optlen))
453 return -EFAULT;
455 if (len != sizeof(char))
456 return -EINVAL;
458 switch (optname) {
459 case MISDN_TIME_STAMP:
460 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
461 opt = 1;
462 else
463 opt = 0;
465 if (put_user(opt, optval))
466 return -EFAULT;
467 break;
468 default:
469 return -ENOPROTOOPT;
472 return 0;
475 static int
476 data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
478 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
479 struct sock *sk = sock->sk;
480 struct sock *csk;
481 int err = 0;
483 if (*debug & DEBUG_SOCKET)
484 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
485 if (addr_len != sizeof(struct sockaddr_mISDN))
486 return -EINVAL;
487 if (!maddr || maddr->family != AF_ISDN)
488 return -EINVAL;
490 lock_sock(sk);
492 if (_pms(sk)->dev) {
493 err = -EALREADY;
494 goto done;
496 _pms(sk)->dev = get_mdevice(maddr->dev);
497 if (!_pms(sk)->dev) {
498 err = -ENODEV;
499 goto done;
502 if (sk->sk_protocol < ISDN_P_B_START) {
503 read_lock_bh(&data_sockets.lock);
504 sk_for_each(csk, &data_sockets.head) {
505 if (sk == csk)
506 continue;
507 if (_pms(csk)->dev != _pms(sk)->dev)
508 continue;
509 if (csk->sk_protocol >= ISDN_P_B_START)
510 continue;
511 if (IS_ISDN_P_TE(csk->sk_protocol)
512 == IS_ISDN_P_TE(sk->sk_protocol))
513 continue;
514 read_unlock_bh(&data_sockets.lock);
515 err = -EBUSY;
516 goto done;
518 read_unlock_bh(&data_sockets.lock);
521 _pms(sk)->ch.send = mISDN_send;
522 _pms(sk)->ch.ctrl = mISDN_ctrl;
524 switch (sk->sk_protocol) {
525 case ISDN_P_TE_S0:
526 case ISDN_P_NT_S0:
527 case ISDN_P_TE_E1:
528 case ISDN_P_NT_E1:
529 mISDN_sock_unlink(&data_sockets, sk);
530 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
531 sk->sk_protocol, maddr);
532 if (err)
533 mISDN_sock_link(&data_sockets, sk);
534 break;
535 case ISDN_P_LAPD_TE:
536 case ISDN_P_LAPD_NT:
537 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
538 sk->sk_protocol, maddr);
539 break;
540 case ISDN_P_B_RAW:
541 case ISDN_P_B_HDLC:
542 case ISDN_P_B_X75SLP:
543 case ISDN_P_B_L2DTMF:
544 case ISDN_P_B_L2DSP:
545 case ISDN_P_B_L2DSPHDLC:
546 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
547 sk->sk_protocol, maddr);
548 break;
549 default:
550 err = -EPROTONOSUPPORT;
552 if (err)
553 goto done;
554 sk->sk_state = MISDN_BOUND;
555 _pms(sk)->ch.protocol = sk->sk_protocol;
557 done:
558 release_sock(sk);
559 return err;
562 static int
563 data_sock_getname(struct socket *sock, struct sockaddr *addr,
564 int *addr_len, int peer)
566 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
567 struct sock *sk = sock->sk;
569 if (!_pms(sk)->dev)
570 return -EBADFD;
572 lock_sock(sk);
574 *addr_len = sizeof(*maddr);
575 maddr->family = AF_ISDN;
576 maddr->dev = _pms(sk)->dev->id;
577 maddr->channel = _pms(sk)->ch.nr;
578 maddr->sapi = _pms(sk)->ch.addr & 0xff;
579 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
580 release_sock(sk);
581 return 0;
584 static const struct proto_ops data_sock_ops = {
585 .family = PF_ISDN,
586 .owner = THIS_MODULE,
587 .release = data_sock_release,
588 .ioctl = data_sock_ioctl,
589 .bind = data_sock_bind,
590 .getname = data_sock_getname,
591 .sendmsg = mISDN_sock_sendmsg,
592 .recvmsg = mISDN_sock_recvmsg,
593 .poll = datagram_poll,
594 .listen = sock_no_listen,
595 .shutdown = sock_no_shutdown,
596 .setsockopt = data_sock_setsockopt,
597 .getsockopt = data_sock_getsockopt,
598 .connect = sock_no_connect,
599 .socketpair = sock_no_socketpair,
600 .accept = sock_no_accept,
601 .mmap = sock_no_mmap
604 static int
605 data_sock_create(struct net *net, struct socket *sock, int protocol)
607 struct sock *sk;
609 if (sock->type != SOCK_DGRAM)
610 return -ESOCKTNOSUPPORT;
612 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
613 if (!sk)
614 return -ENOMEM;
616 sock_init_data(sock, sk);
618 sock->ops = &data_sock_ops;
619 sock->state = SS_UNCONNECTED;
620 sock_reset_flag(sk, SOCK_ZAPPED);
622 sk->sk_protocol = protocol;
623 sk->sk_state = MISDN_OPEN;
624 mISDN_sock_link(&data_sockets, sk);
626 return 0;
629 static int
630 base_sock_release(struct socket *sock)
632 struct sock *sk = sock->sk;
634 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
635 if (!sk)
636 return 0;
638 mISDN_sock_unlink(&base_sockets, sk);
639 sock_orphan(sk);
640 sock_put(sk);
642 return 0;
645 static int
646 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
648 int err = 0, id;
649 struct mISDNdevice *dev;
650 struct mISDNversion ver;
652 switch (cmd) {
653 case IMGETVERSION:
654 ver.major = MISDN_MAJOR_VERSION;
655 ver.minor = MISDN_MINOR_VERSION;
656 ver.release = MISDN_RELEASE;
657 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
658 err = -EFAULT;
659 break;
660 case IMGETCOUNT:
661 id = get_mdevice_count();
662 if (put_user(id, (int __user *)arg))
663 err = -EFAULT;
664 break;
665 case IMGETDEVINFO:
666 if (get_user(id, (int __user *)arg)) {
667 err = -EFAULT;
668 break;
670 dev = get_mdevice(id);
671 if (dev) {
672 struct mISDN_devinfo di;
674 memset(&di, 0, sizeof(di));
675 di.id = dev->id;
676 di.Dprotocols = dev->Dprotocols;
677 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
678 di.protocol = dev->D.protocol;
679 memcpy(di.channelmap, dev->channelmap,
680 sizeof(di.channelmap));
681 di.nrbchan = dev->nrbchan;
682 strcpy(di.name, dev_name(&dev->dev));
683 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
684 err = -EFAULT;
685 } else
686 err = -ENODEV;
687 break;
688 case IMSETDEVNAME:
690 struct mISDN_devrename dn;
691 if (copy_from_user(&dn, (void __user *)arg,
692 sizeof(dn))) {
693 err = -EFAULT;
694 break;
696 dev = get_mdevice(dn.id);
697 if (dev)
698 err = device_rename(&dev->dev, dn.name);
699 else
700 err = -ENODEV;
702 break;
703 default:
704 err = -EINVAL;
706 return err;
709 static int
710 base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
712 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
713 struct sock *sk = sock->sk;
714 int err = 0;
716 if (!maddr || maddr->family != AF_ISDN)
717 return -EINVAL;
719 lock_sock(sk);
721 if (_pms(sk)->dev) {
722 err = -EALREADY;
723 goto done;
726 _pms(sk)->dev = get_mdevice(maddr->dev);
727 if (!_pms(sk)->dev) {
728 err = -ENODEV;
729 goto done;
731 sk->sk_state = MISDN_BOUND;
733 done:
734 release_sock(sk);
735 return err;
738 static const struct proto_ops base_sock_ops = {
739 .family = PF_ISDN,
740 .owner = THIS_MODULE,
741 .release = base_sock_release,
742 .ioctl = base_sock_ioctl,
743 .bind = base_sock_bind,
744 .getname = sock_no_getname,
745 .sendmsg = sock_no_sendmsg,
746 .recvmsg = sock_no_recvmsg,
747 .poll = sock_no_poll,
748 .listen = sock_no_listen,
749 .shutdown = sock_no_shutdown,
750 .setsockopt = sock_no_setsockopt,
751 .getsockopt = sock_no_getsockopt,
752 .connect = sock_no_connect,
753 .socketpair = sock_no_socketpair,
754 .accept = sock_no_accept,
755 .mmap = sock_no_mmap
759 static int
760 base_sock_create(struct net *net, struct socket *sock, int protocol)
762 struct sock *sk;
764 if (sock->type != SOCK_RAW)
765 return -ESOCKTNOSUPPORT;
767 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
768 if (!sk)
769 return -ENOMEM;
771 sock_init_data(sock, sk);
772 sock->ops = &base_sock_ops;
773 sock->state = SS_UNCONNECTED;
774 sock_reset_flag(sk, SOCK_ZAPPED);
775 sk->sk_protocol = protocol;
776 sk->sk_state = MISDN_OPEN;
777 mISDN_sock_link(&base_sockets, sk);
779 return 0;
782 static int
783 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
785 int err = -EPROTONOSUPPORT;
787 switch (proto) {
788 case ISDN_P_BASE:
789 err = base_sock_create(net, sock, proto);
790 break;
791 case ISDN_P_TE_S0:
792 case ISDN_P_NT_S0:
793 case ISDN_P_TE_E1:
794 case ISDN_P_NT_E1:
795 case ISDN_P_LAPD_TE:
796 case ISDN_P_LAPD_NT:
797 case ISDN_P_B_RAW:
798 case ISDN_P_B_HDLC:
799 case ISDN_P_B_X75SLP:
800 case ISDN_P_B_L2DTMF:
801 case ISDN_P_B_L2DSP:
802 case ISDN_P_B_L2DSPHDLC:
803 err = data_sock_create(net, sock, proto);
804 break;
805 default:
806 return err;
809 return err;
812 static const struct net_proto_family mISDN_sock_family_ops = {
813 .owner = THIS_MODULE,
814 .family = PF_ISDN,
815 .create = mISDN_sock_create,
819 misdn_sock_init(u_int *deb)
821 int err;
823 debug = deb;
824 err = sock_register(&mISDN_sock_family_ops);
825 if (err)
826 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
827 return err;
830 void
831 misdn_sock_cleanup(void)
833 sock_unregister(PF_ISDN);