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
;
396 di
.Dprotocols
= dev
->Dprotocols
;
397 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
398 di
.protocol
= dev
->D
.protocol
;
399 memcpy(di
.channelmap
, dev
->channelmap
,
400 sizeof(di
.channelmap
));
401 di
.nrbchan
= dev
->nrbchan
;
402 strcpy(di
.name
, dev_name(&dev
->dev
));
403 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
409 if (sk
->sk_state
== MISDN_BOUND
)
410 err
= data_sock_ioctl_bound(sk
, cmd
,
418 static int data_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
419 char __user
*optval
, unsigned int len
)
421 struct sock
*sk
= sock
->sk
;
422 int err
= 0, opt
= 0;
424 if (*debug
& DEBUG_SOCKET
)
425 printk(KERN_DEBUG
"%s(%p, %d, %x, %p, %d)\n", __func__
, sock
,
426 level
, optname
, optval
, len
);
431 case MISDN_TIME_STAMP
:
432 if (get_user(opt
, (int __user
*)optval
)) {
438 _pms(sk
)->cmask
|= MISDN_TIME_STAMP
;
440 _pms(sk
)->cmask
&= ~MISDN_TIME_STAMP
;
450 static int data_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
451 char __user
*optval
, int __user
*optlen
)
453 struct sock
*sk
= sock
->sk
;
456 if (get_user(len
, optlen
))
460 case MISDN_TIME_STAMP
:
461 if (_pms(sk
)->cmask
& MISDN_TIME_STAMP
)
466 if (put_user(opt
, optval
))
477 data_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
479 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
480 struct sock
*sk
= sock
->sk
;
481 struct hlist_node
*node
;
485 if (*debug
& DEBUG_SOCKET
)
486 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
487 if (addr_len
!= sizeof(struct sockaddr_mISDN
))
489 if (!maddr
|| maddr
->family
!= AF_ISDN
)
498 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
499 if (!_pms(sk
)->dev
) {
504 if (sk
->sk_protocol
< ISDN_P_B_START
) {
505 read_lock_bh(&data_sockets
.lock
);
506 sk_for_each(csk
, node
, &data_sockets
.head
) {
509 if (_pms(csk
)->dev
!= _pms(sk
)->dev
)
511 if (csk
->sk_protocol
>= ISDN_P_B_START
)
513 if (IS_ISDN_P_TE(csk
->sk_protocol
)
514 == IS_ISDN_P_TE(sk
->sk_protocol
))
516 read_unlock_bh(&data_sockets
.lock
);
520 read_unlock_bh(&data_sockets
.lock
);
523 _pms(sk
)->ch
.send
= mISDN_send
;
524 _pms(sk
)->ch
.ctrl
= mISDN_ctrl
;
526 switch (sk
->sk_protocol
) {
531 mISDN_sock_unlink(&data_sockets
, sk
);
532 err
= connect_layer1(_pms(sk
)->dev
, &_pms(sk
)->ch
,
533 sk
->sk_protocol
, maddr
);
535 mISDN_sock_link(&data_sockets
, sk
);
539 err
= create_l2entity(_pms(sk
)->dev
, &_pms(sk
)->ch
,
540 sk
->sk_protocol
, maddr
);
544 case ISDN_P_B_X75SLP
:
545 case ISDN_P_B_L2DTMF
:
547 case ISDN_P_B_L2DSPHDLC
:
548 err
= connect_Bstack(_pms(sk
)->dev
, &_pms(sk
)->ch
,
549 sk
->sk_protocol
, maddr
);
552 err
= -EPROTONOSUPPORT
;
556 sk
->sk_state
= MISDN_BOUND
;
557 _pms(sk
)->ch
.protocol
= sk
->sk_protocol
;
565 data_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
566 int *addr_len
, int peer
)
568 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
569 struct sock
*sk
= sock
->sk
;
576 *addr_len
= sizeof(*maddr
);
577 maddr
->dev
= _pms(sk
)->dev
->id
;
578 maddr
->channel
= _pms(sk
)->ch
.nr
;
579 maddr
->sapi
= _pms(sk
)->ch
.addr
& 0xff;
580 maddr
->tei
= (_pms(sk
)->ch
.addr
>> 8) & 0xff;
585 static const struct proto_ops data_sock_ops
= {
587 .owner
= THIS_MODULE
,
588 .release
= data_sock_release
,
589 .ioctl
= data_sock_ioctl
,
590 .bind
= data_sock_bind
,
591 .getname
= data_sock_getname
,
592 .sendmsg
= mISDN_sock_sendmsg
,
593 .recvmsg
= mISDN_sock_recvmsg
,
594 .poll
= datagram_poll
,
595 .listen
= sock_no_listen
,
596 .shutdown
= sock_no_shutdown
,
597 .setsockopt
= data_sock_setsockopt
,
598 .getsockopt
= data_sock_getsockopt
,
599 .connect
= sock_no_connect
,
600 .socketpair
= sock_no_socketpair
,
601 .accept
= sock_no_accept
,
606 data_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
610 if (sock
->type
!= SOCK_DGRAM
)
611 return -ESOCKTNOSUPPORT
;
613 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
);
617 sock_init_data(sock
, sk
);
619 sock
->ops
= &data_sock_ops
;
620 sock
->state
= SS_UNCONNECTED
;
621 sock_reset_flag(sk
, SOCK_ZAPPED
);
623 sk
->sk_protocol
= protocol
;
624 sk
->sk_state
= MISDN_OPEN
;
625 mISDN_sock_link(&data_sockets
, sk
);
631 base_sock_release(struct socket
*sock
)
633 struct sock
*sk
= sock
->sk
;
635 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
639 mISDN_sock_unlink(&base_sockets
, sk
);
647 base_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
650 struct mISDNdevice
*dev
;
651 struct mISDNversion ver
;
655 ver
.major
= MISDN_MAJOR_VERSION
;
656 ver
.minor
= MISDN_MINOR_VERSION
;
657 ver
.release
= MISDN_RELEASE
;
658 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
662 id
= get_mdevice_count();
663 if (put_user(id
, (int __user
*)arg
))
667 if (get_user(id
, (int __user
*)arg
)) {
671 dev
= get_mdevice(id
);
673 struct mISDN_devinfo di
;
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
)))
690 struct mISDN_devrename dn
;
691 if (copy_from_user(&dn
, (void __user
*)arg
,
696 dev
= get_mdevice(dn
.id
);
698 err
= device_rename(&dev
->dev
, dn
.name
);
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
;
716 if (!maddr
|| maddr
->family
!= AF_ISDN
)
726 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
727 if (!_pms(sk
)->dev
) {
731 sk
->sk_state
= MISDN_BOUND
;
738 static const struct proto_ops base_sock_ops
= {
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
,
760 base_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
764 if (sock
->type
!= SOCK_RAW
)
765 return -ESOCKTNOSUPPORT
;
767 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
);
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
);
783 mISDN_sock_create(struct net
*net
, struct socket
*sock
, int proto
, int kern
)
785 int err
= -EPROTONOSUPPORT
;
789 err
= base_sock_create(net
, sock
, proto
);
799 case ISDN_P_B_X75SLP
:
800 case ISDN_P_B_L2DTMF
:
802 case ISDN_P_B_L2DSPHDLC
:
803 err
= data_sock_create(net
, sock
, proto
);
812 static const struct net_proto_family mISDN_sock_family_ops
= {
813 .owner
= THIS_MODULE
,
815 .create
= mISDN_sock_create
,
819 misdn_sock_init(u_int
*deb
)
824 err
= sock_register(&mISDN_sock_family_ops
);
826 printk(KERN_ERR
"%s: error(%d)\n", __func__
, err
);
831 misdn_sock_cleanup(void)
833 sock_unregister(PF_ISDN
);