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 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;
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
;
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
,
188 if (msg
->msg_flags
& MSG_OOB
)
191 if (msg
->msg_flags
& ~(MSG_DONTWAIT
| MSG_NOSIGNAL
| MSG_ERRQUEUE
))
194 if (len
< MISDN_HEADER_LEN
)
197 if (sk
->sk_state
!= MISDN_BOUND
)
202 skb
= _l2_alloc_skb(len
, GFP_KERNEL
);
206 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
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
));
229 if (!_pms(sk
)->ch
.peer
)
231 err
= _pms(sk
)->ch
.recv(_pms(sk
)->ch
.peer
, skb
);
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
);
255 switch (sk
->sk_protocol
) {
260 if (sk
->sk_state
== MISDN_BOUND
)
261 delete_channel(&_pms(sk
)->ch
);
263 mISDN_sock_unlink(&data_sockets
, sk
);
269 case ISDN_P_B_X75SLP
:
270 case ISDN_P_B_L2DTMF
:
272 case ISDN_P_B_L2DSPHDLC
:
273 delete_channel(&_pms(sk
)->ch
);
274 mISDN_sock_unlink(&data_sockets
, sk
);
281 skb_queue_purge(&sk
->sk_receive_queue
);
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
;
297 if (!_pms(sk
)->dev
) {
303 if (copy_from_user(&cq
, p
, sizeof(cq
))) {
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
);
317 err
= _pms(sk
)->dev
->D
.ctrl(&_pms(sk
)->dev
->D
,
318 CONTROL_CHANNEL
, &cq
);
321 if (copy_to_user(p
, &cq
, sizeof(cq
)))
325 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
) {
330 if (get_user(val
[1], (int __user
*)p
)) {
334 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
335 CONTROL_CHANNEL
, val
);
338 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
339 && sk
->sk_protocol
!= ISDN_P_LAPD_TE
) {
344 if (get_user(val
[1], (int __user
*)p
)) {
348 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
349 CONTROL_CHANNEL
, val
);
361 data_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
364 struct sock
*sk
= sock
->sk
;
365 struct mISDNdevice
*dev
;
366 struct mISDNversion ver
;
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
)))
377 id
= get_mdevice_count();
378 if (put_user(id
, (int __user
*)arg
))
382 if (get_user(id
, (int __user
*)arg
)) {
386 dev
= get_mdevice(id
);
388 struct mISDN_devinfo di
;
390 memset(&di
, 0, sizeof(di
));
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
)))
405 if (sk
->sk_state
== MISDN_BOUND
)
406 err
= data_sock_ioctl_bound(sk
, cmd
,
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
);
427 case MISDN_TIME_STAMP
:
428 if (get_user(opt
, (int __user
*)optval
)) {
434 _pms(sk
)->cmask
|= MISDN_TIME_STAMP
;
436 _pms(sk
)->cmask
&= ~MISDN_TIME_STAMP
;
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
;
452 if (get_user(len
, optlen
))
455 if (len
!= sizeof(char))
459 case MISDN_TIME_STAMP
:
460 if (_pms(sk
)->cmask
& MISDN_TIME_STAMP
)
465 if (put_user(opt
, optval
))
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
;
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
))
487 if (!maddr
|| maddr
->family
!= AF_ISDN
)
496 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
497 if (!_pms(sk
)->dev
) {
502 if (sk
->sk_protocol
< ISDN_P_B_START
) {
503 read_lock_bh(&data_sockets
.lock
);
504 sk_for_each(csk
, &data_sockets
.head
) {
507 if (_pms(csk
)->dev
!= _pms(sk
)->dev
)
509 if (csk
->sk_protocol
>= ISDN_P_B_START
)
511 if (IS_ISDN_P_TE(csk
->sk_protocol
)
512 == IS_ISDN_P_TE(sk
->sk_protocol
))
514 read_unlock_bh(&data_sockets
.lock
);
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
) {
529 mISDN_sock_unlink(&data_sockets
, sk
);
530 err
= connect_layer1(_pms(sk
)->dev
, &_pms(sk
)->ch
,
531 sk
->sk_protocol
, maddr
);
533 mISDN_sock_link(&data_sockets
, sk
);
537 err
= create_l2entity(_pms(sk
)->dev
, &_pms(sk
)->ch
,
538 sk
->sk_protocol
, maddr
);
542 case ISDN_P_B_X75SLP
:
543 case ISDN_P_B_L2DTMF
:
545 case ISDN_P_B_L2DSPHDLC
:
546 err
= connect_Bstack(_pms(sk
)->dev
, &_pms(sk
)->ch
,
547 sk
->sk_protocol
, maddr
);
550 err
= -EPROTONOSUPPORT
;
554 sk
->sk_state
= MISDN_BOUND
;
555 _pms(sk
)->ch
.protocol
= sk
->sk_protocol
;
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
;
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;
584 static const struct proto_ops data_sock_ops
= {
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
,
605 data_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
609 if (sock
->type
!= SOCK_DGRAM
)
610 return -ESOCKTNOSUPPORT
;
612 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
);
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
);
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
);
638 mISDN_sock_unlink(&base_sockets
, sk
);
646 base_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
649 struct mISDNdevice
*dev
;
650 struct mISDNversion ver
;
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
)))
661 id
= get_mdevice_count();
662 if (put_user(id
, (int __user
*)arg
))
666 if (get_user(id
, (int __user
*)arg
)) {
670 dev
= get_mdevice(id
);
672 struct mISDN_devinfo di
;
674 memset(&di
, 0, sizeof(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
);