1 // SPDX-License-Identifier: GPL-2.0-only
4 * Author Karsten Keil <kkeil@novell.com>
6 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
9 #include <linux/mISDNif.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
16 static struct proto mISDN_proto
= {
19 .obj_size
= sizeof(struct mISDN_sock
)
22 #define _pms(sk) ((struct mISDN_sock *)sk)
24 static struct mISDN_sock_list data_sockets
= {
25 .lock
= __RW_LOCK_UNLOCKED(data_sockets
.lock
)
28 static struct mISDN_sock_list base_sockets
= {
29 .lock
= __RW_LOCK_UNLOCKED(base_sockets
.lock
)
32 #define L2_HEADER_LEN 4
34 static inline struct sk_buff
*
35 _l2_alloc_skb(unsigned int len
, gfp_t gfp_mask
)
39 skb
= alloc_skb(len
+ L2_HEADER_LEN
, gfp_mask
);
41 skb_reserve(skb
, L2_HEADER_LEN
);
46 mISDN_sock_link(struct mISDN_sock_list
*l
, struct sock
*sk
)
48 write_lock_bh(&l
->lock
);
49 sk_add_node(sk
, &l
->head
);
50 write_unlock_bh(&l
->lock
);
53 static void mISDN_sock_unlink(struct mISDN_sock_list
*l
, struct sock
*sk
)
55 write_lock_bh(&l
->lock
);
57 write_unlock_bh(&l
->lock
);
61 mISDN_send(struct mISDNchannel
*ch
, struct sk_buff
*skb
)
63 struct mISDN_sock
*msk
;
66 msk
= container_of(ch
, struct mISDN_sock
, ch
);
67 if (*debug
& DEBUG_SOCKET
)
68 printk(KERN_DEBUG
"%s len %d %p\n", __func__
, skb
->len
, skb
);
69 if (msk
->sk
.sk_state
== MISDN_CLOSED
)
72 err
= sock_queue_rcv_skb(&msk
->sk
, skb
);
74 printk(KERN_WARNING
"%s: error %d\n", __func__
, err
);
79 mISDN_ctrl(struct mISDNchannel
*ch
, u_int cmd
, void *arg
)
81 struct mISDN_sock
*msk
;
83 msk
= container_of(ch
, struct mISDN_sock
, ch
);
84 if (*debug
& DEBUG_SOCKET
)
85 printk(KERN_DEBUG
"%s(%p, %x, %p)\n", __func__
, ch
, cmd
, arg
);
88 msk
->sk
.sk_state
= MISDN_CLOSED
;
95 mISDN_sock_cmsg(struct sock
*sk
, struct msghdr
*msg
, struct sk_buff
*skb
)
97 struct __kernel_old_timeval tv
;
99 if (_pms(sk
)->cmask
& MISDN_TIME_STAMP
) {
100 skb_get_timestamp(skb
, &tv
);
101 put_cmsg(msg
, SOL_MISDN
, MISDN_TIME_STAMP
, sizeof(tv
), &tv
);
106 mISDN_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
110 struct sock
*sk
= sock
->sk
;
114 if (*debug
& DEBUG_SOCKET
)
115 printk(KERN_DEBUG
"%s: len %d, flags %x ch.nr %d, proto %x\n",
116 __func__
, (int)len
, flags
, _pms(sk
)->ch
.nr
,
118 if (flags
& (MSG_OOB
))
121 if (sk
->sk_state
== MISDN_CLOSED
)
124 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
129 DECLARE_SOCKADDR(struct sockaddr_mISDN
*, maddr
, msg
->msg_name
);
131 maddr
->family
= AF_ISDN
;
132 maddr
->dev
= _pms(sk
)->dev
->id
;
133 if ((sk
->sk_protocol
== ISDN_P_LAPD_TE
) ||
134 (sk
->sk_protocol
== ISDN_P_LAPD_NT
)) {
135 maddr
->channel
= (mISDN_HEAD_ID(skb
) >> 16) & 0xff;
136 maddr
->tei
= (mISDN_HEAD_ID(skb
) >> 8) & 0xff;
137 maddr
->sapi
= mISDN_HEAD_ID(skb
) & 0xff;
139 maddr
->channel
= _pms(sk
)->ch
.nr
;
140 maddr
->sapi
= _pms(sk
)->ch
.addr
& 0xFF;
141 maddr
->tei
= (_pms(sk
)->ch
.addr
>> 8) & 0xFF;
143 msg
->msg_namelen
= sizeof(*maddr
);
146 copied
= skb
->len
+ MISDN_HEADER_LEN
;
148 if (flags
& MSG_PEEK
)
149 refcount_dec(&skb
->users
);
151 skb_queue_head(&sk
->sk_receive_queue
, skb
);
154 memcpy(skb_push(skb
, MISDN_HEADER_LEN
), mISDN_HEAD_P(skb
),
157 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
159 mISDN_sock_cmsg(sk
, msg
, skb
);
161 skb_free_datagram(sk
, skb
);
163 return err
? : copied
;
167 mISDN_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
169 struct sock
*sk
= sock
->sk
;
173 if (*debug
& DEBUG_SOCKET
)
174 printk(KERN_DEBUG
"%s: len %d flags %x ch %d proto %x\n",
175 __func__
, (int)len
, msg
->msg_flags
, _pms(sk
)->ch
.nr
,
178 if (msg
->msg_flags
& MSG_OOB
)
181 if (msg
->msg_flags
& ~(MSG_DONTWAIT
| MSG_NOSIGNAL
| MSG_ERRQUEUE
))
184 if (len
< MISDN_HEADER_LEN
)
187 if (sk
->sk_state
!= MISDN_BOUND
)
192 skb
= _l2_alloc_skb(len
, GFP_KERNEL
);
196 if (memcpy_from_msg(skb_put(skb
, len
), msg
, len
)) {
201 memcpy(mISDN_HEAD_P(skb
), skb
->data
, MISDN_HEADER_LEN
);
202 skb_pull(skb
, MISDN_HEADER_LEN
);
204 if (msg
->msg_namelen
>= sizeof(struct sockaddr_mISDN
)) {
205 /* if we have a address, we use it */
206 DECLARE_SOCKADDR(struct sockaddr_mISDN
*, maddr
, msg
->msg_name
);
207 mISDN_HEAD_ID(skb
) = maddr
->channel
;
208 } else { /* use default for L2 messages */
209 if ((sk
->sk_protocol
== ISDN_P_LAPD_TE
) ||
210 (sk
->sk_protocol
== ISDN_P_LAPD_NT
))
211 mISDN_HEAD_ID(skb
) = _pms(sk
)->ch
.nr
;
214 if (*debug
& DEBUG_SOCKET
)
215 printk(KERN_DEBUG
"%s: ID:%x\n",
216 __func__
, mISDN_HEAD_ID(skb
));
219 if (!_pms(sk
)->ch
.peer
)
221 err
= _pms(sk
)->ch
.recv(_pms(sk
)->ch
.peer
, skb
);
236 data_sock_release(struct socket
*sock
)
238 struct sock
*sk
= sock
->sk
;
240 if (*debug
& DEBUG_SOCKET
)
241 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
244 switch (sk
->sk_protocol
) {
249 if (sk
->sk_state
== MISDN_BOUND
)
250 delete_channel(&_pms(sk
)->ch
);
252 mISDN_sock_unlink(&data_sockets
, sk
);
258 case ISDN_P_B_X75SLP
:
259 case ISDN_P_B_L2DTMF
:
261 case ISDN_P_B_L2DSPHDLC
:
262 delete_channel(&_pms(sk
)->ch
);
263 mISDN_sock_unlink(&data_sockets
, sk
);
270 skb_queue_purge(&sk
->sk_receive_queue
);
279 data_sock_ioctl_bound(struct sock
*sk
, unsigned int cmd
, void __user
*p
)
281 struct mISDN_ctrl_req cq
;
282 int err
= -EINVAL
, val
[2];
283 struct mISDNchannel
*bchan
, *next
;
286 if (!_pms(sk
)->dev
) {
292 if (copy_from_user(&cq
, p
, sizeof(cq
))) {
296 if ((sk
->sk_protocol
& ~ISDN_P_B_MASK
) == ISDN_P_B_START
) {
297 list_for_each_entry_safe(bchan
, next
,
298 &_pms(sk
)->dev
->bchannels
, list
) {
299 if (bchan
->nr
== cq
.channel
) {
300 err
= bchan
->ctrl(bchan
,
301 CONTROL_CHANNEL
, &cq
);
306 err
= _pms(sk
)->dev
->D
.ctrl(&_pms(sk
)->dev
->D
,
307 CONTROL_CHANNEL
, &cq
);
310 if (copy_to_user(p
, &cq
, sizeof(cq
)))
314 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
) {
319 if (get_user(val
[1], (int __user
*)p
)) {
323 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
324 CONTROL_CHANNEL
, val
);
327 if (sk
->sk_protocol
!= ISDN_P_LAPD_NT
328 && sk
->sk_protocol
!= ISDN_P_LAPD_TE
) {
333 if (get_user(val
[1], (int __user
*)p
)) {
337 err
= _pms(sk
)->dev
->teimgr
->ctrl(_pms(sk
)->dev
->teimgr
,
338 CONTROL_CHANNEL
, val
);
350 data_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
353 struct sock
*sk
= sock
->sk
;
354 struct mISDNdevice
*dev
;
355 struct mISDNversion ver
;
359 ver
.major
= MISDN_MAJOR_VERSION
;
360 ver
.minor
= MISDN_MINOR_VERSION
;
361 ver
.release
= MISDN_RELEASE
;
362 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
366 id
= get_mdevice_count();
367 if (put_user(id
, (int __user
*)arg
))
371 if (get_user(id
, (int __user
*)arg
)) {
375 dev
= get_mdevice(id
);
377 struct mISDN_devinfo di
;
379 memset(&di
, 0, sizeof(di
));
381 di
.Dprotocols
= dev
->Dprotocols
;
382 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
383 di
.protocol
= dev
->D
.protocol
;
384 memcpy(di
.channelmap
, dev
->channelmap
,
385 sizeof(di
.channelmap
));
386 di
.nrbchan
= dev
->nrbchan
;
387 strscpy(di
.name
, dev_name(&dev
->dev
), sizeof(di
.name
));
388 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
394 if (sk
->sk_state
== MISDN_BOUND
)
395 err
= data_sock_ioctl_bound(sk
, cmd
,
403 static int data_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
404 sockptr_t optval
, unsigned int len
)
406 struct sock
*sk
= sock
->sk
;
407 int err
= 0, opt
= 0;
409 if (*debug
& DEBUG_SOCKET
)
410 printk(KERN_DEBUG
"%s(%p, %d, %x, optval, %d)\n", __func__
, sock
,
411 level
, optname
, len
);
416 case MISDN_TIME_STAMP
:
417 if (copy_from_sockptr(&opt
, optval
, sizeof(int))) {
423 _pms(sk
)->cmask
|= MISDN_TIME_STAMP
;
425 _pms(sk
)->cmask
&= ~MISDN_TIME_STAMP
;
435 static int data_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
436 char __user
*optval
, int __user
*optlen
)
438 struct sock
*sk
= sock
->sk
;
441 if (get_user(len
, optlen
))
444 if (len
!= sizeof(char))
448 case MISDN_TIME_STAMP
:
449 if (_pms(sk
)->cmask
& MISDN_TIME_STAMP
)
454 if (put_user(opt
, optval
))
465 data_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
467 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
468 struct sock
*sk
= sock
->sk
;
472 if (*debug
& DEBUG_SOCKET
)
473 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
474 if (addr_len
!= sizeof(struct sockaddr_mISDN
))
476 if (!maddr
|| maddr
->family
!= AF_ISDN
)
485 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
486 if (!_pms(sk
)->dev
) {
491 if (sk
->sk_protocol
< ISDN_P_B_START
) {
492 read_lock_bh(&data_sockets
.lock
);
493 sk_for_each(csk
, &data_sockets
.head
) {
496 if (_pms(csk
)->dev
!= _pms(sk
)->dev
)
498 if (csk
->sk_protocol
>= ISDN_P_B_START
)
500 if (IS_ISDN_P_TE(csk
->sk_protocol
)
501 == IS_ISDN_P_TE(sk
->sk_protocol
))
503 read_unlock_bh(&data_sockets
.lock
);
507 read_unlock_bh(&data_sockets
.lock
);
510 _pms(sk
)->ch
.send
= mISDN_send
;
511 _pms(sk
)->ch
.ctrl
= mISDN_ctrl
;
513 switch (sk
->sk_protocol
) {
518 mISDN_sock_unlink(&data_sockets
, sk
);
519 err
= connect_layer1(_pms(sk
)->dev
, &_pms(sk
)->ch
,
520 sk
->sk_protocol
, maddr
);
522 mISDN_sock_link(&data_sockets
, sk
);
526 err
= create_l2entity(_pms(sk
)->dev
, &_pms(sk
)->ch
,
527 sk
->sk_protocol
, maddr
);
531 case ISDN_P_B_X75SLP
:
532 case ISDN_P_B_L2DTMF
:
534 case ISDN_P_B_L2DSPHDLC
:
535 err
= connect_Bstack(_pms(sk
)->dev
, &_pms(sk
)->ch
,
536 sk
->sk_protocol
, maddr
);
539 err
= -EPROTONOSUPPORT
;
543 sk
->sk_state
= MISDN_BOUND
;
544 _pms(sk
)->ch
.protocol
= sk
->sk_protocol
;
552 data_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
555 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
556 struct sock
*sk
= sock
->sk
;
563 maddr
->family
= AF_ISDN
;
564 maddr
->dev
= _pms(sk
)->dev
->id
;
565 maddr
->channel
= _pms(sk
)->ch
.nr
;
566 maddr
->sapi
= _pms(sk
)->ch
.addr
& 0xff;
567 maddr
->tei
= (_pms(sk
)->ch
.addr
>> 8) & 0xff;
569 return sizeof(*maddr
);
572 static const struct proto_ops data_sock_ops
= {
574 .owner
= THIS_MODULE
,
575 .release
= data_sock_release
,
576 .ioctl
= data_sock_ioctl
,
577 .bind
= data_sock_bind
,
578 .getname
= data_sock_getname
,
579 .sendmsg
= mISDN_sock_sendmsg
,
580 .recvmsg
= mISDN_sock_recvmsg
,
581 .poll
= datagram_poll
,
582 .listen
= sock_no_listen
,
583 .shutdown
= sock_no_shutdown
,
584 .setsockopt
= data_sock_setsockopt
,
585 .getsockopt
= data_sock_getsockopt
,
586 .connect
= sock_no_connect
,
587 .socketpair
= sock_no_socketpair
,
588 .accept
= sock_no_accept
,
593 data_sock_create(struct net
*net
, struct socket
*sock
, int protocol
, int kern
)
597 if (sock
->type
!= SOCK_DGRAM
)
598 return -ESOCKTNOSUPPORT
;
600 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
, kern
);
604 sock_init_data(sock
, sk
);
606 sock
->ops
= &data_sock_ops
;
607 sock
->state
= SS_UNCONNECTED
;
608 sock_reset_flag(sk
, SOCK_ZAPPED
);
610 sk
->sk_protocol
= protocol
;
611 sk
->sk_state
= MISDN_OPEN
;
612 mISDN_sock_link(&data_sockets
, sk
);
618 base_sock_release(struct socket
*sock
)
620 struct sock
*sk
= sock
->sk
;
622 printk(KERN_DEBUG
"%s(%p) sk=%p\n", __func__
, sock
, sk
);
626 mISDN_sock_unlink(&base_sockets
, sk
);
634 base_sock_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
637 struct mISDNdevice
*dev
;
638 struct mISDNversion ver
;
642 ver
.major
= MISDN_MAJOR_VERSION
;
643 ver
.minor
= MISDN_MINOR_VERSION
;
644 ver
.release
= MISDN_RELEASE
;
645 if (copy_to_user((void __user
*)arg
, &ver
, sizeof(ver
)))
649 id
= get_mdevice_count();
650 if (put_user(id
, (int __user
*)arg
))
654 if (get_user(id
, (int __user
*)arg
)) {
658 dev
= get_mdevice(id
);
660 struct mISDN_devinfo di
;
662 memset(&di
, 0, sizeof(di
));
664 di
.Dprotocols
= dev
->Dprotocols
;
665 di
.Bprotocols
= dev
->Bprotocols
| get_all_Bprotocols();
666 di
.protocol
= dev
->D
.protocol
;
667 memcpy(di
.channelmap
, dev
->channelmap
,
668 sizeof(di
.channelmap
));
669 di
.nrbchan
= dev
->nrbchan
;
670 strscpy(di
.name
, dev_name(&dev
->dev
), sizeof(di
.name
));
671 if (copy_to_user((void __user
*)arg
, &di
, sizeof(di
)))
678 struct mISDN_devrename dn
;
679 if (copy_from_user(&dn
, (void __user
*)arg
,
684 dn
.name
[sizeof(dn
.name
) - 1] = '\0';
685 dev
= get_mdevice(dn
.id
);
687 err
= device_rename(&dev
->dev
, dn
.name
);
699 base_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
701 struct sockaddr_mISDN
*maddr
= (struct sockaddr_mISDN
*) addr
;
702 struct sock
*sk
= sock
->sk
;
705 if (addr_len
< sizeof(struct sockaddr_mISDN
))
708 if (!maddr
|| maddr
->family
!= AF_ISDN
)
718 _pms(sk
)->dev
= get_mdevice(maddr
->dev
);
719 if (!_pms(sk
)->dev
) {
723 sk
->sk_state
= MISDN_BOUND
;
730 static const struct proto_ops base_sock_ops
= {
732 .owner
= THIS_MODULE
,
733 .release
= base_sock_release
,
734 .ioctl
= base_sock_ioctl
,
735 .bind
= base_sock_bind
,
736 .getname
= sock_no_getname
,
737 .sendmsg
= sock_no_sendmsg
,
738 .recvmsg
= sock_no_recvmsg
,
739 .listen
= sock_no_listen
,
740 .shutdown
= sock_no_shutdown
,
741 .connect
= sock_no_connect
,
742 .socketpair
= sock_no_socketpair
,
743 .accept
= sock_no_accept
,
749 base_sock_create(struct net
*net
, struct socket
*sock
, int protocol
, int kern
)
753 if (sock
->type
!= SOCK_RAW
)
754 return -ESOCKTNOSUPPORT
;
755 if (!capable(CAP_NET_RAW
))
758 sk
= sk_alloc(net
, PF_ISDN
, GFP_KERNEL
, &mISDN_proto
, kern
);
762 sock_init_data(sock
, sk
);
763 sock
->ops
= &base_sock_ops
;
764 sock
->state
= SS_UNCONNECTED
;
765 sock_reset_flag(sk
, SOCK_ZAPPED
);
766 sk
->sk_protocol
= protocol
;
767 sk
->sk_state
= MISDN_OPEN
;
768 mISDN_sock_link(&base_sockets
, sk
);
774 mISDN_sock_create(struct net
*net
, struct socket
*sock
, int proto
, int kern
)
776 int err
= -EPROTONOSUPPORT
;
780 err
= base_sock_create(net
, sock
, proto
, kern
);
790 case ISDN_P_B_X75SLP
:
791 case ISDN_P_B_L2DTMF
:
793 case ISDN_P_B_L2DSPHDLC
:
794 err
= data_sock_create(net
, sock
, proto
, kern
);
803 static const struct net_proto_family mISDN_sock_family_ops
= {
804 .owner
= THIS_MODULE
,
806 .create
= mISDN_sock_create
,
810 misdn_sock_init(u_int
*deb
)
815 err
= sock_register(&mISDN_sock_family_ops
);
817 printk(KERN_ERR
"%s: error(%d)\n", __func__
, err
);
822 misdn_sock_cleanup(void)
824 sock_unregister(PF_ISDN
);