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 socket
*sock
, struct msghdr
*msg
, size_t len
,
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 refcount_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_msg(skb
, 0, msg
, copied
);
168 mISDN_sock_cmsg(sk
, msg
, skb
);
170 skb_free_datagram(sk
, skb
);
172 return err
? : copied
;
176 mISDN_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
178 struct sock
*sk
= sock
->sk
;
182 if (*debug
& DEBUG_SOCKET
)
183 printk(KERN_DEBUG
"%s: len %d flags %x ch %d proto %x\n",
184 __func__
, (int)len
, msg
->msg_flags
, _pms(sk
)->ch
.nr
,
187 if (msg
->msg_flags
& MSG_OOB
)
190 if (msg
->msg_flags
& ~(MSG_DONTWAIT
| MSG_NOSIGNAL
| MSG_ERRQUEUE
))
193 if (len
< MISDN_HEADER_LEN
)
196 if (sk
->sk_state
!= MISDN_BOUND
)
201 skb
= _l2_alloc_skb(len
, GFP_KERNEL
);
205 if (memcpy_from_msg(skb_put(skb
, len
), msg
, len
)) {
210 memcpy(mISDN_HEAD_P(skb
), skb
->data
, MISDN_HEADER_LEN
);
211 skb_pull(skb
, MISDN_HEADER_LEN
);
213 if (msg
->msg_namelen
>= sizeof(struct sockaddr_mISDN
)) {
214 /* if we have a address, we use it */
215 DECLARE_SOCKADDR(struct sockaddr_mISDN
*, maddr
, msg
->msg_name
);
216 mISDN_HEAD_ID(skb
) = maddr
->channel
;
217 } else { /* use default for L2 messages */
218 if ((sk
->sk_protocol
== ISDN_P_LAPD_TE
) ||
219 (sk
->sk_protocol
== ISDN_P_LAPD_NT
))
220 mISDN_HEAD_ID(skb
) = _pms(sk
)->ch
.nr
;
223 if (*debug
& DEBUG_SOCKET
)
224 printk(KERN_DEBUG
"%s: ID:%x\n",
225 __func__
, mISDN_HEAD_ID(skb
));
228 if (!_pms(sk
)->ch
.peer
)
230 err
= _pms(sk
)->ch
.recv(_pms(sk
)->ch
.peer
, skb
);
246 data_sock_release(struct socket
*sock
)
248 struct sock
*sk
= sock
->sk
;
250 if (*debug
& DEBUG_SOCKET
)
251 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
254 switch (sk
->sk_protocol
) {
259 if (sk
->sk_state
== MISDN_BOUND
)
260 delete_channel(&_pms(sk
)->ch
);
262 mISDN_sock_unlink(&data_sockets
, sk
);
268 case ISDN_P_B_X75SLP
:
269 case ISDN_P_B_L2DTMF
:
271 case ISDN_P_B_L2DSPHDLC
:
272 delete_channel(&_pms(sk
)->ch
);
273 mISDN_sock_unlink(&data_sockets
, sk
);
280 skb_queue_purge(&sk
->sk_receive_queue
);
289 data_sock_ioctl_bound(struct sock
*sk
, unsigned int cmd
, void __user
*p
)
291 struct mISDN_ctrl_req cq
;
292 int err
= -EINVAL
, val
[2];
293 struct mISDNchannel
*bchan
, *next
;
296 if (!_pms(sk
)->dev
) {
302 if (copy_from_user(&cq
, p
, sizeof(cq
))) {
306 if ((sk
->sk_protocol
& ~ISDN_P_B_MASK
) == ISDN_P_B_START
) {
307 list_for_each_entry_safe(bchan
, next
,
308 &_pms(sk
)->dev
->bchannels
, list
) {
309 if (bchan
->nr
== cq
.channel
) {
310 err
= bchan
->ctrl(bchan
,
311 CONTROL_CHANNEL
, &cq
);
316 err
= _pms(sk
)->dev
->D
.ctrl(&_pms(sk
)->dev
->D
,
317 CONTROL_CHANNEL
, &cq
);
320 if (copy_to_user(p
, &cq
, sizeof(cq
)))
324 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
) {
329 if (get_user(val
[1], (int __user
*)p
)) {
333 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
334 CONTROL_CHANNEL
, val
);
337 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
338 && sk
->sk_protocol
!= ISDN_P_LAPD_TE
) {
343 if (get_user(val
[1], (int __user
*)p
)) {
347 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
348 CONTROL_CHANNEL
, val
);
360 data_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
363 struct sock
*sk
= sock
->sk
;
364 struct mISDNdevice
*dev
;
365 struct mISDNversion ver
;
369 ver
.major
= MISDN_MAJOR_VERSION
;
370 ver
.minor
= MISDN_MINOR_VERSION
;
371 ver
.release
= MISDN_RELEASE
;
372 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
376 id
= get_mdevice_count();
377 if (put_user(id
, (int __user
*)arg
))
381 if (get_user(id
, (int __user
*)arg
)) {
385 dev
= get_mdevice(id
);
387 struct mISDN_devinfo di
;
389 memset(&di
, 0, sizeof(di
));
391 di
.Dprotocols
= dev
->Dprotocols
;
392 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
393 di
.protocol
= dev
->D
.protocol
;
394 memcpy(di
.channelmap
, dev
->channelmap
,
395 sizeof(di
.channelmap
));
396 di
.nrbchan
= dev
->nrbchan
;
397 strcpy(di
.name
, dev_name(&dev
->dev
));
398 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
404 if (sk
->sk_state
== MISDN_BOUND
)
405 err
= data_sock_ioctl_bound(sk
, cmd
,
413 static int data_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
414 char __user
*optval
, unsigned int len
)
416 struct sock
*sk
= sock
->sk
;
417 int err
= 0, opt
= 0;
419 if (*debug
& DEBUG_SOCKET
)
420 printk(KERN_DEBUG
"%s(%p, %d, %x, %p, %d)\n", __func__
, sock
,
421 level
, optname
, optval
, len
);
426 case MISDN_TIME_STAMP
:
427 if (get_user(opt
, (int __user
*)optval
)) {
433 _pms(sk
)->cmask
|= MISDN_TIME_STAMP
;
435 _pms(sk
)->cmask
&= ~MISDN_TIME_STAMP
;
445 static int data_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
446 char __user
*optval
, int __user
*optlen
)
448 struct sock
*sk
= sock
->sk
;
451 if (get_user(len
, optlen
))
454 if (len
!= sizeof(char))
458 case MISDN_TIME_STAMP
:
459 if (_pms(sk
)->cmask
& MISDN_TIME_STAMP
)
464 if (put_user(opt
, optval
))
475 data_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
477 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
478 struct sock
*sk
= sock
->sk
;
482 if (*debug
& DEBUG_SOCKET
)
483 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
484 if (addr_len
!= sizeof(struct sockaddr_mISDN
))
486 if (!maddr
|| maddr
->family
!= AF_ISDN
)
495 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
496 if (!_pms(sk
)->dev
) {
501 if (sk
->sk_protocol
< ISDN_P_B_START
) {
502 read_lock_bh(&data_sockets
.lock
);
503 sk_for_each(csk
, &data_sockets
.head
) {
506 if (_pms(csk
)->dev
!= _pms(sk
)->dev
)
508 if (csk
->sk_protocol
>= ISDN_P_B_START
)
510 if (IS_ISDN_P_TE(csk
->sk_protocol
)
511 == IS_ISDN_P_TE(sk
->sk_protocol
))
513 read_unlock_bh(&data_sockets
.lock
);
517 read_unlock_bh(&data_sockets
.lock
);
520 _pms(sk
)->ch
.send
= mISDN_send
;
521 _pms(sk
)->ch
.ctrl
= mISDN_ctrl
;
523 switch (sk
->sk_protocol
) {
528 mISDN_sock_unlink(&data_sockets
, sk
);
529 err
= connect_layer1(_pms(sk
)->dev
, &_pms(sk
)->ch
,
530 sk
->sk_protocol
, maddr
);
532 mISDN_sock_link(&data_sockets
, sk
);
536 err
= create_l2entity(_pms(sk
)->dev
, &_pms(sk
)->ch
,
537 sk
->sk_protocol
, maddr
);
541 case ISDN_P_B_X75SLP
:
542 case ISDN_P_B_L2DTMF
:
544 case ISDN_P_B_L2DSPHDLC
:
545 err
= connect_Bstack(_pms(sk
)->dev
, &_pms(sk
)->ch
,
546 sk
->sk_protocol
, maddr
);
549 err
= -EPROTONOSUPPORT
;
553 sk
->sk_state
= MISDN_BOUND
;
554 _pms(sk
)->ch
.protocol
= sk
->sk_protocol
;
562 data_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
563 int *addr_len
, int peer
)
565 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
566 struct sock
*sk
= sock
->sk
;
573 *addr_len
= sizeof(*maddr
);
574 maddr
->family
= AF_ISDN
;
575 maddr
->dev
= _pms(sk
)->dev
->id
;
576 maddr
->channel
= _pms(sk
)->ch
.nr
;
577 maddr
->sapi
= _pms(sk
)->ch
.addr
& 0xff;
578 maddr
->tei
= (_pms(sk
)->ch
.addr
>> 8) & 0xff;
583 static const struct proto_ops data_sock_ops
= {
585 .owner
= THIS_MODULE
,
586 .release
= data_sock_release
,
587 .ioctl
= data_sock_ioctl
,
588 .bind
= data_sock_bind
,
589 .getname
= data_sock_getname
,
590 .sendmsg
= mISDN_sock_sendmsg
,
591 .recvmsg
= mISDN_sock_recvmsg
,
592 .poll
= datagram_poll
,
593 .listen
= sock_no_listen
,
594 .shutdown
= sock_no_shutdown
,
595 .setsockopt
= data_sock_setsockopt
,
596 .getsockopt
= data_sock_getsockopt
,
597 .connect
= sock_no_connect
,
598 .socketpair
= sock_no_socketpair
,
599 .accept
= sock_no_accept
,
604 data_sock_create(struct net
*net
, struct socket
*sock
, int protocol
, int kern
)
608 if (sock
->type
!= SOCK_DGRAM
)
609 return -ESOCKTNOSUPPORT
;
611 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
, kern
);
615 sock_init_data(sock
, sk
);
617 sock
->ops
= &data_sock_ops
;
618 sock
->state
= SS_UNCONNECTED
;
619 sock_reset_flag(sk
, SOCK_ZAPPED
);
621 sk
->sk_protocol
= protocol
;
622 sk
->sk_state
= MISDN_OPEN
;
623 mISDN_sock_link(&data_sockets
, sk
);
629 base_sock_release(struct socket
*sock
)
631 struct sock
*sk
= sock
->sk
;
633 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
637 mISDN_sock_unlink(&base_sockets
, sk
);
645 base_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
648 struct mISDNdevice
*dev
;
649 struct mISDNversion ver
;
653 ver
.major
= MISDN_MAJOR_VERSION
;
654 ver
.minor
= MISDN_MINOR_VERSION
;
655 ver
.release
= MISDN_RELEASE
;
656 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
660 id
= get_mdevice_count();
661 if (put_user(id
, (int __user
*)arg
))
665 if (get_user(id
, (int __user
*)arg
)) {
669 dev
= get_mdevice(id
);
671 struct mISDN_devinfo di
;
673 memset(&di
, 0, sizeof(di
));
675 di
.Dprotocols
= dev
->Dprotocols
;
676 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
677 di
.protocol
= dev
->D
.protocol
;
678 memcpy(di
.channelmap
, dev
->channelmap
,
679 sizeof(di
.channelmap
));
680 di
.nrbchan
= dev
->nrbchan
;
681 strcpy(di
.name
, dev_name(&dev
->dev
));
682 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
689 struct mISDN_devrename dn
;
690 if (copy_from_user(&dn
, (void __user
*)arg
,
695 dev
= get_mdevice(dn
.id
);
697 err
= device_rename(&dev
->dev
, dn
.name
);
709 base_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
711 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
712 struct sock
*sk
= sock
->sk
;
715 if (!maddr
|| maddr
->family
!= AF_ISDN
)
718 if (addr_len
< sizeof(struct sockaddr_mISDN
))
728 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
729 if (!_pms(sk
)->dev
) {
733 sk
->sk_state
= MISDN_BOUND
;
740 static const struct proto_ops base_sock_ops
= {
742 .owner
= THIS_MODULE
,
743 .release
= base_sock_release
,
744 .ioctl
= base_sock_ioctl
,
745 .bind
= base_sock_bind
,
746 .getname
= sock_no_getname
,
747 .sendmsg
= sock_no_sendmsg
,
748 .recvmsg
= sock_no_recvmsg
,
749 .poll
= sock_no_poll
,
750 .listen
= sock_no_listen
,
751 .shutdown
= sock_no_shutdown
,
752 .setsockopt
= sock_no_setsockopt
,
753 .getsockopt
= sock_no_getsockopt
,
754 .connect
= sock_no_connect
,
755 .socketpair
= sock_no_socketpair
,
756 .accept
= sock_no_accept
,
762 base_sock_create(struct net
*net
, struct socket
*sock
, int protocol
, int kern
)
766 if (sock
->type
!= SOCK_RAW
)
767 return -ESOCKTNOSUPPORT
;
769 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
, kern
);
773 sock_init_data(sock
, sk
);
774 sock
->ops
= &base_sock_ops
;
775 sock
->state
= SS_UNCONNECTED
;
776 sock_reset_flag(sk
, SOCK_ZAPPED
);
777 sk
->sk_protocol
= protocol
;
778 sk
->sk_state
= MISDN_OPEN
;
779 mISDN_sock_link(&base_sockets
, sk
);
785 mISDN_sock_create(struct net
*net
, struct socket
*sock
, int proto
, int kern
)
787 int err
= -EPROTONOSUPPORT
;
791 err
= base_sock_create(net
, sock
, proto
, kern
);
801 case ISDN_P_B_X75SLP
:
802 case ISDN_P_B_L2DTMF
:
804 case ISDN_P_B_L2DSPHDLC
:
805 err
= data_sock_create(net
, sock
, proto
, kern
);
814 static const struct net_proto_family mISDN_sock_family_ops
= {
815 .owner
= THIS_MODULE
,
817 .create
= mISDN_sock_create
,
821 misdn_sock_init(u_int
*deb
)
826 err
= sock_register(&mISDN_sock_family_ops
);
828 printk(KERN_ERR
"%s: error(%d)\n", __func__
, err
);
833 misdn_sock_cleanup(void)
835 sock_unregister(PF_ISDN
);