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>
25 static struct proto mISDN_proto
= {
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
)
48 skb
= alloc_skb(len
+ L2_HEADER_LEN
, gfp_mask
);
50 skb_reserve(skb
, L2_HEADER_LEN
);
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
);
66 write_unlock_bh(&l
->lock
);
70 mISDN_send(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
72 struct mISDN_sock
*msk
;
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
)
81 err
= sock_queue_rcv_skb(&msk
->sk
, skb
);
83 printk(KERN_WARNING
"%s: error %d\n", __func__
, err
);
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
);
97 msk
->sk
.sk_state
= MISDN_CLOSED
;
104 mISDN_sock_cmsg(struct sock
*sk
, struct msghdr
*msg
, struct sk_buff
*skb
)
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
);
115 mISDN_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
116 struct msghdr
*msg
, size_t len
, int flags
)
119 struct sock
*sk
= sock
->sk
;
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
);
138 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;
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
;
157 if (flags
& MSG_PEEK
)
158 atomic_dec(&skb
->users
);
160 skb_queue_head(&sk
->sk_receive_queue
, skb
);
163 memcpy(skb_push(skb
, MISDN_HEADER_LEN
), mISDN_HEAD_P(skb
),
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
;
176 mISDN_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
177 struct msghdr
*msg
, size_t len
)
179 struct sock
*sk
= sock
->sk
;
182 struct sockaddr_mISDN
*maddr
;
184 if (*debug
& DEBUG_SOCKET
)
185 printk(KERN_DEBUG
"%s: len %d flags %x ch %d proto %x\n",
186 __func__
, (int)len
, msg
->msg_flags
, _pms(sk
)->ch
.nr
,
189 if (msg
->msg_flags
& MSG_OOB
)
192 if (msg
->msg_flags
& ~(MSG_DONTWAIT
| MSG_NOSIGNAL
| MSG_ERRQUEUE
))
195 if (len
< MISDN_HEADER_LEN
)
198 if (sk
->sk_state
!= MISDN_BOUND
)
203 skb
= _l2_alloc_skb(len
, GFP_KERNEL
);
207 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
212 memcpy(mISDN_HEAD_P(skb
), skb
->data
, MISDN_HEADER_LEN
);
213 skb_pull(skb
, MISDN_HEADER_LEN
);
215 if (msg
->msg_namelen
>= sizeof(struct sockaddr_mISDN
)) {
216 /* if we have a address, we use it */
217 maddr
= (struct sockaddr_mISDN
*)msg
->msg_name
;
218 mISDN_HEAD_ID(skb
) = maddr
->channel
;
219 } else { /* use default for L2 messages */
220 if ((sk
->sk_protocol
== ISDN_P_LAPD_TE
) ||
221 (sk
->sk_protocol
== ISDN_P_LAPD_NT
))
222 mISDN_HEAD_ID(skb
) = _pms(sk
)->ch
.nr
;
225 if (*debug
& DEBUG_SOCKET
)
226 printk(KERN_DEBUG
"%s: ID:%x\n",
227 __func__
, mISDN_HEAD_ID(skb
));
230 if (!_pms(sk
)->ch
.peer
)
232 err
= _pms(sk
)->ch
.recv(_pms(sk
)->ch
.peer
, skb
);
248 data_sock_release(struct socket
*sock
)
250 struct sock
*sk
= sock
->sk
;
252 if (*debug
& DEBUG_SOCKET
)
253 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
256 switch (sk
->sk_protocol
) {
261 if (sk
->sk_state
== MISDN_BOUND
)
262 delete_channel(&_pms(sk
)->ch
);
264 mISDN_sock_unlink(&data_sockets
, sk
);
270 case ISDN_P_B_X75SLP
:
271 case ISDN_P_B_L2DTMF
:
273 case ISDN_P_B_L2DSPHDLC
:
274 delete_channel(&_pms(sk
)->ch
);
275 mISDN_sock_unlink(&data_sockets
, sk
);
282 skb_queue_purge(&sk
->sk_receive_queue
);
291 data_sock_ioctl_bound(struct sock
*sk
, unsigned int cmd
, void __user
*p
)
293 struct mISDN_ctrl_req cq
;
294 int err
= -EINVAL
, val
[2];
295 struct mISDNchannel
*bchan
, *next
;
298 if (!_pms(sk
)->dev
) {
304 if (copy_from_user(&cq
, p
, sizeof(cq
))) {
308 if ((sk
->sk_protocol
& ~ISDN_P_B_MASK
) == ISDN_P_B_START
) {
309 list_for_each_entry_safe(bchan
, next
,
310 &_pms(sk
)->dev
->bchannels
, list
) {
311 if (bchan
->nr
== cq
.channel
) {
312 err
= bchan
->ctrl(bchan
,
313 CONTROL_CHANNEL
, &cq
);
318 err
= _pms(sk
)->dev
->D
.ctrl(&_pms(sk
)->dev
->D
,
319 CONTROL_CHANNEL
, &cq
);
322 if (copy_to_user(p
, &cq
, sizeof(cq
)))
326 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
) {
331 if (get_user(val
[1], (int __user
*)p
)) {
335 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
336 CONTROL_CHANNEL
, val
);
339 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
340 && sk
->sk_protocol
!= ISDN_P_LAPD_TE
) {
345 if (get_user(val
[1], (int __user
*)p
)) {
349 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
350 CONTROL_CHANNEL
, val
);
362 data_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
365 struct sock
*sk
= sock
->sk
;
366 struct mISDNdevice
*dev
;
367 struct mISDNversion ver
;
371 ver
.major
= MISDN_MAJOR_VERSION
;
372 ver
.minor
= MISDN_MINOR_VERSION
;
373 ver
.release
= MISDN_RELEASE
;
374 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
378 id
= get_mdevice_count();
379 if (put_user(id
, (int __user
*)arg
))
383 if (get_user(id
, (int __user
*)arg
)) {
387 dev
= get_mdevice(id
);
389 struct mISDN_devinfo di
;
391 memset(&di
, 0, sizeof(di
));
393 di
.Dprotocols
= dev
->Dprotocols
;
394 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
395 di
.protocol
= dev
->D
.protocol
;
396 memcpy(di
.channelmap
, dev
->channelmap
,
397 sizeof(di
.channelmap
));
398 di
.nrbchan
= dev
->nrbchan
;
399 strcpy(di
.name
, dev_name(&dev
->dev
));
400 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
406 if (sk
->sk_state
== MISDN_BOUND
)
407 err
= data_sock_ioctl_bound(sk
, cmd
,
415 static int data_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
416 char __user
*optval
, unsigned int len
)
418 struct sock
*sk
= sock
->sk
;
419 int err
= 0, opt
= 0;
421 if (*debug
& DEBUG_SOCKET
)
422 printk(KERN_DEBUG
"%s(%p, %d, %x, %p, %d)\n", __func__
, sock
,
423 level
, optname
, optval
, len
);
428 case MISDN_TIME_STAMP
:
429 if (get_user(opt
, (int __user
*)optval
)) {
435 _pms(sk
)->cmask
|= MISDN_TIME_STAMP
;
437 _pms(sk
)->cmask
&= ~MISDN_TIME_STAMP
;
447 static int data_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
448 char __user
*optval
, int __user
*optlen
)
450 struct sock
*sk
= sock
->sk
;
453 if (get_user(len
, optlen
))
456 if (len
!= sizeof(char))
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
;
484 if (*debug
& DEBUG_SOCKET
)
485 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
486 if (addr_len
!= sizeof(struct sockaddr_mISDN
))
488 if (!maddr
|| maddr
->family
!= AF_ISDN
)
497 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
498 if (!_pms(sk
)->dev
) {
503 if (sk
->sk_protocol
< ISDN_P_B_START
) {
504 read_lock_bh(&data_sockets
.lock
);
505 sk_for_each(csk
, &data_sockets
.head
) {
508 if (_pms(csk
)->dev
!= _pms(sk
)->dev
)
510 if (csk
->sk_protocol
>= ISDN_P_B_START
)
512 if (IS_ISDN_P_TE(csk
->sk_protocol
)
513 == IS_ISDN_P_TE(sk
->sk_protocol
))
515 read_unlock_bh(&data_sockets
.lock
);
519 read_unlock_bh(&data_sockets
.lock
);
522 _pms(sk
)->ch
.send
= mISDN_send
;
523 _pms(sk
)->ch
.ctrl
= mISDN_ctrl
;
525 switch (sk
->sk_protocol
) {
530 mISDN_sock_unlink(&data_sockets
, sk
);
531 err
= connect_layer1(_pms(sk
)->dev
, &_pms(sk
)->ch
,
532 sk
->sk_protocol
, maddr
);
534 mISDN_sock_link(&data_sockets
, sk
);
538 err
= create_l2entity(_pms(sk
)->dev
, &_pms(sk
)->ch
,
539 sk
->sk_protocol
, maddr
);
543 case ISDN_P_B_X75SLP
:
544 case ISDN_P_B_L2DTMF
:
546 case ISDN_P_B_L2DSPHDLC
:
547 err
= connect_Bstack(_pms(sk
)->dev
, &_pms(sk
)->ch
,
548 sk
->sk_protocol
, maddr
);
551 err
= -EPROTONOSUPPORT
;
555 sk
->sk_state
= MISDN_BOUND
;
556 _pms(sk
)->ch
.protocol
= sk
->sk_protocol
;
564 data_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
565 int *addr_len
, int peer
)
567 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
568 struct sock
*sk
= sock
->sk
;
575 *addr_len
= sizeof(*maddr
);
576 maddr
->family
= AF_ISDN
;
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
;
675 memset(&di
, 0, sizeof(di
));
677 di
.Dprotocols
= dev
->Dprotocols
;
678 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
679 di
.protocol
= dev
->D
.protocol
;
680 memcpy(di
.channelmap
, dev
->channelmap
,
681 sizeof(di
.channelmap
));
682 di
.nrbchan
= dev
->nrbchan
;
683 strcpy(di
.name
, dev_name(&dev
->dev
));
684 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
691 struct mISDN_devrename dn
;
692 if (copy_from_user(&dn
, (void __user
*)arg
,
697 dev
= get_mdevice(dn
.id
);
699 err
= device_rename(&dev
->dev
, dn
.name
);
711 base_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
713 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
714 struct sock
*sk
= sock
->sk
;
717 if (!maddr
|| maddr
->family
!= AF_ISDN
)
727 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
728 if (!_pms(sk
)->dev
) {
732 sk
->sk_state
= MISDN_BOUND
;
739 static const struct proto_ops base_sock_ops
= {
741 .owner
= THIS_MODULE
,
742 .release
= base_sock_release
,
743 .ioctl
= base_sock_ioctl
,
744 .bind
= base_sock_bind
,
745 .getname
= sock_no_getname
,
746 .sendmsg
= sock_no_sendmsg
,
747 .recvmsg
= sock_no_recvmsg
,
748 .poll
= sock_no_poll
,
749 .listen
= sock_no_listen
,
750 .shutdown
= sock_no_shutdown
,
751 .setsockopt
= sock_no_setsockopt
,
752 .getsockopt
= sock_no_getsockopt
,
753 .connect
= sock_no_connect
,
754 .socketpair
= sock_no_socketpair
,
755 .accept
= sock_no_accept
,
761 base_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
765 if (sock
->type
!= SOCK_RAW
)
766 return -ESOCKTNOSUPPORT
;
768 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
);
772 sock_init_data(sock
, sk
);
773 sock
->ops
= &base_sock_ops
;
774 sock
->state
= SS_UNCONNECTED
;
775 sock_reset_flag(sk
, SOCK_ZAPPED
);
776 sk
->sk_protocol
= protocol
;
777 sk
->sk_state
= MISDN_OPEN
;
778 mISDN_sock_link(&base_sockets
, sk
);
784 mISDN_sock_create(struct net
*net
, struct socket
*sock
, int proto
, int kern
)
786 int err
= -EPROTONOSUPPORT
;
790 err
= base_sock_create(net
, sock
, proto
);
800 case ISDN_P_B_X75SLP
:
801 case ISDN_P_B_L2DTMF
:
803 case ISDN_P_B_L2DSPHDLC
:
804 err
= data_sock_create(net
, sock
, proto
);
813 static const struct net_proto_family mISDN_sock_family_ops
= {
814 .owner
= THIS_MODULE
,
816 .create
= mISDN_sock_create
,
820 misdn_sock_init(u_int
*deb
)
825 err
= sock_register(&mISDN_sock_family_ops
);
827 printk(KERN_ERR
"%s: error(%d)\n", __func__
, err
);
832 misdn_sock_cleanup(void)
834 sock_unregister(PF_ISDN
);