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
))
460 if (len
!= sizeof(char))
464 case MISDN_TIME_STAMP
:
465 if (_pms(sk
)->cmask
& MISDN_TIME_STAMP
)
470 if (put_user(opt
, optval
))
481 data_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
483 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
484 struct sock
*sk
= sock
->sk
;
485 struct hlist_node
*node
;
489 if (*debug
& DEBUG_SOCKET
)
490 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
491 if (addr_len
!= sizeof(struct sockaddr_mISDN
))
493 if (!maddr
|| maddr
->family
!= AF_ISDN
)
502 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
503 if (!_pms(sk
)->dev
) {
508 if (sk
->sk_protocol
< ISDN_P_B_START
) {
509 read_lock_bh(&data_sockets
.lock
);
510 sk_for_each(csk
, node
, &data_sockets
.head
) {
513 if (_pms(csk
)->dev
!= _pms(sk
)->dev
)
515 if (csk
->sk_protocol
>= ISDN_P_B_START
)
517 if (IS_ISDN_P_TE(csk
->sk_protocol
)
518 == IS_ISDN_P_TE(sk
->sk_protocol
))
520 read_unlock_bh(&data_sockets
.lock
);
524 read_unlock_bh(&data_sockets
.lock
);
527 _pms(sk
)->ch
.send
= mISDN_send
;
528 _pms(sk
)->ch
.ctrl
= mISDN_ctrl
;
530 switch (sk
->sk_protocol
) {
535 mISDN_sock_unlink(&data_sockets
, sk
);
536 err
= connect_layer1(_pms(sk
)->dev
, &_pms(sk
)->ch
,
537 sk
->sk_protocol
, maddr
);
539 mISDN_sock_link(&data_sockets
, sk
);
543 err
= create_l2entity(_pms(sk
)->dev
, &_pms(sk
)->ch
,
544 sk
->sk_protocol
, maddr
);
548 case ISDN_P_B_X75SLP
:
549 case ISDN_P_B_L2DTMF
:
551 case ISDN_P_B_L2DSPHDLC
:
552 err
= connect_Bstack(_pms(sk
)->dev
, &_pms(sk
)->ch
,
553 sk
->sk_protocol
, maddr
);
556 err
= -EPROTONOSUPPORT
;
560 sk
->sk_state
= MISDN_BOUND
;
561 _pms(sk
)->ch
.protocol
= sk
->sk_protocol
;
569 data_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
570 int *addr_len
, int peer
)
572 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
573 struct sock
*sk
= sock
->sk
;
580 *addr_len
= sizeof(*maddr
);
581 maddr
->dev
= _pms(sk
)->dev
->id
;
582 maddr
->channel
= _pms(sk
)->ch
.nr
;
583 maddr
->sapi
= _pms(sk
)->ch
.addr
& 0xff;
584 maddr
->tei
= (_pms(sk
)->ch
.addr
>> 8) & 0xff;
589 static const struct proto_ops data_sock_ops
= {
591 .owner
= THIS_MODULE
,
592 .release
= data_sock_release
,
593 .ioctl
= data_sock_ioctl
,
594 .bind
= data_sock_bind
,
595 .getname
= data_sock_getname
,
596 .sendmsg
= mISDN_sock_sendmsg
,
597 .recvmsg
= mISDN_sock_recvmsg
,
598 .poll
= datagram_poll
,
599 .listen
= sock_no_listen
,
600 .shutdown
= sock_no_shutdown
,
601 .setsockopt
= data_sock_setsockopt
,
602 .getsockopt
= data_sock_getsockopt
,
603 .connect
= sock_no_connect
,
604 .socketpair
= sock_no_socketpair
,
605 .accept
= sock_no_accept
,
610 data_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
614 if (sock
->type
!= SOCK_DGRAM
)
615 return -ESOCKTNOSUPPORT
;
617 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
);
621 sock_init_data(sock
, sk
);
623 sock
->ops
= &data_sock_ops
;
624 sock
->state
= SS_UNCONNECTED
;
625 sock_reset_flag(sk
, SOCK_ZAPPED
);
627 sk
->sk_protocol
= protocol
;
628 sk
->sk_state
= MISDN_OPEN
;
629 mISDN_sock_link(&data_sockets
, sk
);
635 base_sock_release(struct socket
*sock
)
637 struct sock
*sk
= sock
->sk
;
639 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
643 mISDN_sock_unlink(&base_sockets
, sk
);
651 base_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
654 struct mISDNdevice
*dev
;
655 struct mISDNversion ver
;
659 ver
.major
= MISDN_MAJOR_VERSION
;
660 ver
.minor
= MISDN_MINOR_VERSION
;
661 ver
.release
= MISDN_RELEASE
;
662 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
666 id
= get_mdevice_count();
667 if (put_user(id
, (int __user
*)arg
))
671 if (get_user(id
, (int __user
*)arg
)) {
675 dev
= get_mdevice(id
);
677 struct mISDN_devinfo di
;
679 memset(&di
, 0, sizeof(di
));
681 di
.Dprotocols
= dev
->Dprotocols
;
682 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
683 di
.protocol
= dev
->D
.protocol
;
684 memcpy(di
.channelmap
, dev
->channelmap
,
685 sizeof(di
.channelmap
));
686 di
.nrbchan
= dev
->nrbchan
;
687 strcpy(di
.name
, dev_name(&dev
->dev
));
688 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
695 struct mISDN_devrename dn
;
696 if (copy_from_user(&dn
, (void __user
*)arg
,
701 dev
= get_mdevice(dn
.id
);
703 err
= device_rename(&dev
->dev
, dn
.name
);
715 base_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
717 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
718 struct sock
*sk
= sock
->sk
;
721 if (!maddr
|| maddr
->family
!= AF_ISDN
)
731 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
732 if (!_pms(sk
)->dev
) {
736 sk
->sk_state
= MISDN_BOUND
;
743 static const struct proto_ops base_sock_ops
= {
745 .owner
= THIS_MODULE
,
746 .release
= base_sock_release
,
747 .ioctl
= base_sock_ioctl
,
748 .bind
= base_sock_bind
,
749 .getname
= sock_no_getname
,
750 .sendmsg
= sock_no_sendmsg
,
751 .recvmsg
= sock_no_recvmsg
,
752 .poll
= sock_no_poll
,
753 .listen
= sock_no_listen
,
754 .shutdown
= sock_no_shutdown
,
755 .setsockopt
= sock_no_setsockopt
,
756 .getsockopt
= sock_no_getsockopt
,
757 .connect
= sock_no_connect
,
758 .socketpair
= sock_no_socketpair
,
759 .accept
= sock_no_accept
,
765 base_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
769 if (sock
->type
!= SOCK_RAW
)
770 return -ESOCKTNOSUPPORT
;
772 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
);
776 sock_init_data(sock
, sk
);
777 sock
->ops
= &base_sock_ops
;
778 sock
->state
= SS_UNCONNECTED
;
779 sock_reset_flag(sk
, SOCK_ZAPPED
);
780 sk
->sk_protocol
= protocol
;
781 sk
->sk_state
= MISDN_OPEN
;
782 mISDN_sock_link(&base_sockets
, sk
);
788 mISDN_sock_create(struct net
*net
, struct socket
*sock
, int proto
, int kern
)
790 int err
= -EPROTONOSUPPORT
;
794 err
= base_sock_create(net
, sock
, proto
);
804 case ISDN_P_B_X75SLP
:
805 case ISDN_P_B_L2DTMF
:
807 case ISDN_P_B_L2DSPHDLC
:
808 err
= data_sock_create(net
, sock
, proto
);
817 static const struct net_proto_family mISDN_sock_family_ops
= {
818 .owner
= THIS_MODULE
,
820 .create
= mISDN_sock_create
,
824 misdn_sock_init(u_int
*deb
)
829 err
= sock_register(&mISDN_sock_family_ops
);
831 printk(KERN_ERR
"%s: error(%d)\n", __func__
, err
);
836 misdn_sock_cleanup(void)
838 sock_unregister(PF_ISDN
);