blob: 7446d8b4282d58cee32d435c930c23b1b43092a4 [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
2 *
3 * Author Karsten Keil <kkeil@novell.com>
4 *
5 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
6 *
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.
10 *
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.
15 *
16 */
17
18#include <linux/mISDNif.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020020#include "core.h"
21
Hannes Ederdfa96ec2008-12-12 21:13:45 -080022static u_int *debug;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020023
24static struct proto mISDN_proto = {
25 .name = "misdn",
26 .owner = THIS_MODULE,
27 .obj_size = sizeof(struct mISDN_sock)
28};
29
30#define _pms(sk) ((struct mISDN_sock *)sk)
31
32static struct mISDN_sock_list data_sockets = {
33 .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
34};
35
36static struct mISDN_sock_list base_sockets = {
37 .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
38};
39
40#define L2_HEADER_LEN 4
41
42static inline struct sk_buff *
43_l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
44{
45 struct sk_buff *skb;
46
47 skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
48 if (likely(skb))
49 skb_reserve(skb, L2_HEADER_LEN);
50 return skb;
51}
52
53static void
54mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
55{
56 write_lock_bh(&l->lock);
57 sk_add_node(sk, &l->head);
58 write_unlock_bh(&l->lock);
59}
60
61static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
62{
63 write_lock_bh(&l->lock);
64 sk_del_node_init(sk);
65 write_unlock_bh(&l->lock);
66}
67
68static int
69mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
70{
71 struct mISDN_sock *msk;
72 int err;
73
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)
78 return -EUNATCH;
79 __net_timestamp(skb);
80 err = sock_queue_rcv_skb(&msk->sk, skb);
81 if (err)
82 printk(KERN_WARNING "%s: error %d\n", __func__, err);
83 return err;
84}
85
86static int
87mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
88{
89 struct mISDN_sock *msk;
90
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);
94 switch (cmd) {
95 case CLOSE_CHANNEL:
96 msk->sk.sk_state = MISDN_CLOSED;
97 break;
98 }
99 return 0;
100}
101
102static inline void
103mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
104{
105 struct timeval tv;
106
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);
110 }
111}
112
113static int
114mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
115 struct msghdr *msg, size_t len, int flags)
116{
117 struct sk_buff *skb;
118 struct sock *sk = sock->sk;
119 struct sockaddr_mISDN *maddr;
120
121 int copied, err;
122
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,
126 sk->sk_protocol);
127 if (flags & (MSG_OOB))
128 return -EOPNOTSUPP;
129
130 if (sk->sk_state == MISDN_CLOSED)
131 return 0;
132
133 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
134 if (!skb)
135 return err;
136
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;
147 } else {
148 maddr->channel = _pms(sk)->ch.nr;
149 maddr->sapi = _pms(sk)->ch.addr & 0xFF;
150 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xFF;
151 }
152 } else {
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;
157 }
158
159 copied = skb->len + MISDN_HEADER_LEN;
160 if (len < copied) {
161 if (flags & MSG_PEEK)
162 atomic_dec(&skb->users);
163 else
164 skb_queue_head(&sk->sk_receive_queue, skb);
165 return -ENOSPC;
166 }
167 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
168 MISDN_HEADER_LEN);
169
170 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
171
172 mISDN_sock_cmsg(sk, msg, skb);
173
174 skb_free_datagram(sk, skb);
175
176 return err ? : copied;
177}
178
179static int
180mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
181 struct msghdr *msg, size_t len)
182{
183 struct sock *sk = sock->sk;
184 struct sk_buff *skb;
185 int err = -ENOMEM;
186 struct sockaddr_mISDN *maddr;
187
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,
191 sk->sk_protocol);
192
193 if (msg->msg_flags & MSG_OOB)
194 return -EOPNOTSUPP;
195
196 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
197 return -EINVAL;
198
199 if (len < MISDN_HEADER_LEN)
200 return -EINVAL;
201
202 if (sk->sk_state != MISDN_BOUND)
203 return -EBADFD;
204
205 lock_sock(sk);
206
207 skb = _l2_alloc_skb(len, GFP_KERNEL);
208 if (!skb)
209 goto done;
210
211 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
212 err = -EFAULT;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000213 goto done;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200214 }
215
216 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
217 skb_pull(skb, MISDN_HEADER_LEN);
218
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))
Karsten Keileac74af2009-05-22 11:04:56 +0000226 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200227 }
228
229 if (*debug & DEBUG_SOCKET)
230 printk(KERN_DEBUG "%s: ID:%x\n",
231 __func__, mISDN_HEAD_ID(skb));
232
233 err = -ENODEV;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000234 if (!_pms(sk)->ch.peer)
235 goto done;
236 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
237 if (err)
238 goto done;
239 else {
240 skb = NULL;
241 err = len;
242 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200243
244done:
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000245 if (skb)
246 kfree_skb(skb);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200247 release_sock(sk);
248 return err;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200249}
250
251static int
252data_sock_release(struct socket *sock)
253{
254 struct sock *sk = sock->sk;
255
256 if (*debug & DEBUG_SOCKET)
257 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
258 if (!sk)
259 return 0;
260 switch (sk->sk_protocol) {
261 case ISDN_P_TE_S0:
262 case ISDN_P_NT_S0:
263 case ISDN_P_TE_E1:
264 case ISDN_P_NT_E1:
265 if (sk->sk_state == MISDN_BOUND)
266 delete_channel(&_pms(sk)->ch);
267 else
268 mISDN_sock_unlink(&data_sockets, sk);
269 break;
270 case ISDN_P_LAPD_TE:
271 case ISDN_P_LAPD_NT:
272 case ISDN_P_B_RAW:
273 case ISDN_P_B_HDLC:
274 case ISDN_P_B_X75SLP:
275 case ISDN_P_B_L2DTMF:
276 case ISDN_P_B_L2DSP:
277 case ISDN_P_B_L2DSPHDLC:
278 delete_channel(&_pms(sk)->ch);
279 mISDN_sock_unlink(&data_sockets, sk);
280 break;
281 }
282
283 lock_sock(sk);
284
285 sock_orphan(sk);
286 skb_queue_purge(&sk->sk_receive_queue);
287
288 release_sock(sk);
289 sock_put(sk);
290
291 return 0;
292}
293
294static int
295data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
296{
297 struct mISDN_ctrl_req cq;
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000298 int err = -EINVAL, val[2];
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200299 struct mISDNchannel *bchan, *next;
300
301 lock_sock(sk);
302 if (!_pms(sk)->dev) {
303 err = -ENODEV;
304 goto done;
305 }
306 switch (cmd) {
307 case IMCTRLREQ:
308 if (copy_from_user(&cq, p, sizeof(cq))) {
309 err = -EFAULT;
310 break;
311 }
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);
318 break;
319 }
320 }
321 } else
322 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
323 CONTROL_CHANNEL, &cq);
324 if (err)
325 break;
326 if (copy_to_user(p, &cq, sizeof(cq)))
327 err = -EFAULT;
328 break;
329 case IMCLEAR_L2:
330 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
331 err = -EINVAL;
332 break;
333 }
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000334 val[0] = cmd;
335 if (get_user(val[1], (int __user *)p)) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200336 err = -EFAULT;
337 break;
338 }
339 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000340 CONTROL_CHANNEL, val);
341 break;
342 case IMHOLD_L1:
343 if (sk->sk_protocol != ISDN_P_LAPD_NT
344 && sk->sk_protocol != ISDN_P_LAPD_TE) {
345 err = -EINVAL;
346 break;
347 }
348 val[0] = cmd;
349 if (get_user(val[1], (int __user *)p)) {
350 err = -EFAULT;
351 break;
352 }
353 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
354 CONTROL_CHANNEL, val);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200355 break;
356 default:
357 err = -EINVAL;
358 break;
359 }
360done:
361 release_sock(sk);
362 return err;
363}
364
365static int
366data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
367{
368 int err = 0, id;
369 struct sock *sk = sock->sk;
370 struct mISDNdevice *dev;
371 struct mISDNversion ver;
372
373 switch (cmd) {
374 case IMGETVERSION:
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)))
379 err = -EFAULT;
380 break;
381 case IMGETCOUNT:
382 id = get_mdevice_count();
383 if (put_user(id, (int __user *)arg))
384 err = -EFAULT;
385 break;
386 case IMGETDEVINFO:
387 if (get_user(id, (int __user *)arg)) {
388 err = -EFAULT;
389 break;
390 }
391 dev = get_mdevice(id);
392 if (dev) {
393 struct mISDN_devinfo di;
394
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000395 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200396 di.id = dev->id;
397 di.Dprotocols = dev->Dprotocols;
398 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
399 di.protocol = dev->D.protocol;
400 memcpy(di.channelmap, dev->channelmap,
Karsten Keilff4cc1d2008-07-30 18:26:58 +0200401 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200402 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200403 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200404 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
405 err = -EFAULT;
406 } else
407 err = -ENODEV;
408 break;
409 default:
410 if (sk->sk_state == MISDN_BOUND)
411 err = data_sock_ioctl_bound(sk, cmd,
412 (void __user *)arg);
413 else
414 err = -ENOTCONN;
415 }
416 return err;
417}
418
419static int data_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700420 char __user *optval, unsigned int len)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200421{
422 struct sock *sk = sock->sk;
423 int err = 0, opt = 0;
424
425 if (*debug & DEBUG_SOCKET)
426 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
427 level, optname, optval, len);
428
429 lock_sock(sk);
430
431 switch (optname) {
432 case MISDN_TIME_STAMP:
433 if (get_user(opt, (int __user *)optval)) {
434 err = -EFAULT;
435 break;
436 }
437
438 if (opt)
439 _pms(sk)->cmask |= MISDN_TIME_STAMP;
440 else
441 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
442 break;
443 default:
444 err = -ENOPROTOOPT;
445 break;
446 }
447 release_sock(sk);
448 return err;
449}
450
451static int data_sock_getsockopt(struct socket *sock, int level, int optname,
452 char __user *optval, int __user *optlen)
453{
454 struct sock *sk = sock->sk;
455 int len, opt;
456
457 if (get_user(len, optlen))
458 return -EFAULT;
459
460 switch (optname) {
461 case MISDN_TIME_STAMP:
462 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
463 opt = 1;
464 else
465 opt = 0;
466
467 if (put_user(opt, optval))
468 return -EFAULT;
469 break;
470 default:
471 return -ENOPROTOOPT;
472 }
473
474 return 0;
475}
476
477static int
478data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
479{
480 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
481 struct sock *sk = sock->sk;
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200482 struct hlist_node *node;
483 struct sock *csk;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200484 int err = 0;
485
486 if (*debug & DEBUG_SOCKET)
487 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
488 if (addr_len != sizeof(struct sockaddr_mISDN))
489 return -EINVAL;
490 if (!maddr || maddr->family != AF_ISDN)
491 return -EINVAL;
492
493 lock_sock(sk);
494
495 if (_pms(sk)->dev) {
496 err = -EALREADY;
497 goto done;
498 }
499 _pms(sk)->dev = get_mdevice(maddr->dev);
500 if (!_pms(sk)->dev) {
501 err = -ENODEV;
502 goto done;
503 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200504
Andreas Eversberg9a812552008-09-14 14:42:18 +0200505 if (sk->sk_protocol < ISDN_P_B_START) {
506 read_lock_bh(&data_sockets.lock);
507 sk_for_each(csk, node, &data_sockets.head) {
508 if (sk == csk)
509 continue;
510 if (_pms(csk)->dev != _pms(sk)->dev)
511 continue;
512 if (csk->sk_protocol >= ISDN_P_B_START)
513 continue;
514 if (IS_ISDN_P_TE(csk->sk_protocol)
515 == IS_ISDN_P_TE(sk->sk_protocol))
516 continue;
517 read_unlock_bh(&data_sockets.lock);
518 err = -EBUSY;
519 goto done;
520 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200521 read_unlock_bh(&data_sockets.lock);
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200522 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200523
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200524 _pms(sk)->ch.send = mISDN_send;
525 _pms(sk)->ch.ctrl = mISDN_ctrl;
526
527 switch (sk->sk_protocol) {
528 case ISDN_P_TE_S0:
529 case ISDN_P_NT_S0:
530 case ISDN_P_TE_E1:
531 case ISDN_P_NT_E1:
532 mISDN_sock_unlink(&data_sockets, sk);
533 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
534 sk->sk_protocol, maddr);
535 if (err)
536 mISDN_sock_link(&data_sockets, sk);
537 break;
538 case ISDN_P_LAPD_TE:
539 case ISDN_P_LAPD_NT:
540 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
541 sk->sk_protocol, maddr);
542 break;
543 case ISDN_P_B_RAW:
544 case ISDN_P_B_HDLC:
545 case ISDN_P_B_X75SLP:
546 case ISDN_P_B_L2DTMF:
547 case ISDN_P_B_L2DSP:
548 case ISDN_P_B_L2DSPHDLC:
549 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
550 sk->sk_protocol, maddr);
551 break;
552 default:
553 err = -EPROTONOSUPPORT;
554 }
555 if (err)
556 goto done;
557 sk->sk_state = MISDN_BOUND;
558 _pms(sk)->ch.protocol = sk->sk_protocol;
559
560done:
561 release_sock(sk);
562 return err;
563}
564
565static int
566data_sock_getname(struct socket *sock, struct sockaddr *addr,
567 int *addr_len, int peer)
568{
569 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
570 struct sock *sk = sock->sk;
571
572 if (!_pms(sk)->dev)
573 return -EBADFD;
574
575 lock_sock(sk);
576
577 *addr_len = sizeof(*maddr);
578 maddr->dev = _pms(sk)->dev->id;
579 maddr->channel = _pms(sk)->ch.nr;
580 maddr->sapi = _pms(sk)->ch.addr & 0xff;
581 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
582 release_sock(sk);
583 return 0;
584}
585
586static const struct proto_ops data_sock_ops = {
587 .family = PF_ISDN,
588 .owner = THIS_MODULE,
589 .release = data_sock_release,
590 .ioctl = data_sock_ioctl,
591 .bind = data_sock_bind,
592 .getname = data_sock_getname,
593 .sendmsg = mISDN_sock_sendmsg,
594 .recvmsg = mISDN_sock_recvmsg,
595 .poll = datagram_poll,
596 .listen = sock_no_listen,
597 .shutdown = sock_no_shutdown,
598 .setsockopt = data_sock_setsockopt,
599 .getsockopt = data_sock_getsockopt,
600 .connect = sock_no_connect,
601 .socketpair = sock_no_socketpair,
602 .accept = sock_no_accept,
603 .mmap = sock_no_mmap
604};
605
606static int
607data_sock_create(struct net *net, struct socket *sock, int protocol)
608{
609 struct sock *sk;
610
611 if (sock->type != SOCK_DGRAM)
612 return -ESOCKTNOSUPPORT;
613
614 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
615 if (!sk)
616 return -ENOMEM;
617
618 sock_init_data(sock, sk);
619
620 sock->ops = &data_sock_ops;
621 sock->state = SS_UNCONNECTED;
622 sock_reset_flag(sk, SOCK_ZAPPED);
623
624 sk->sk_protocol = protocol;
625 sk->sk_state = MISDN_OPEN;
626 mISDN_sock_link(&data_sockets, sk);
627
628 return 0;
629}
630
631static int
632base_sock_release(struct socket *sock)
633{
634 struct sock *sk = sock->sk;
635
636 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
637 if (!sk)
638 return 0;
639
640 mISDN_sock_unlink(&base_sockets, sk);
641 sock_orphan(sk);
642 sock_put(sk);
643
644 return 0;
645}
646
647static int
648base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
649{
650 int err = 0, id;
651 struct mISDNdevice *dev;
652 struct mISDNversion ver;
653
654 switch (cmd) {
655 case IMGETVERSION:
656 ver.major = MISDN_MAJOR_VERSION;
657 ver.minor = MISDN_MINOR_VERSION;
658 ver.release = MISDN_RELEASE;
659 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
660 err = -EFAULT;
661 break;
662 case IMGETCOUNT:
663 id = get_mdevice_count();
664 if (put_user(id, (int __user *)arg))
665 err = -EFAULT;
666 break;
667 case IMGETDEVINFO:
668 if (get_user(id, (int __user *)arg)) {
669 err = -EFAULT;
670 break;
671 }
672 dev = get_mdevice(id);
673 if (dev) {
674 struct mISDN_devinfo di;
675
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000676 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200677 di.id = dev->id;
678 di.Dprotocols = dev->Dprotocols;
679 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
680 di.protocol = dev->D.protocol;
681 memcpy(di.channelmap, dev->channelmap,
Karsten Keilff4cc1d2008-07-30 18:26:58 +0200682 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200683 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200684 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200685 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
686 err = -EFAULT;
687 } else
688 err = -ENODEV;
689 break;
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200690 case IMSETDEVNAME:
691 {
692 struct mISDN_devrename dn;
693 if (copy_from_user(&dn, (void __user *)arg,
694 sizeof(dn))) {
695 err = -EFAULT;
696 break;
697 }
698 dev = get_mdevice(dn.id);
699 if (dev)
Matthias Urlichs837468d2008-08-16 00:04:33 +0200700 err = device_rename(&dev->dev, dn.name);
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200701 else
702 err = -ENODEV;
703 }
704 break;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200705 default:
706 err = -EINVAL;
707 }
708 return err;
709}
710
711static int
712base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
713{
714 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
715 struct sock *sk = sock->sk;
716 int err = 0;
717
718 if (!maddr || maddr->family != AF_ISDN)
719 return -EINVAL;
720
721 lock_sock(sk);
722
723 if (_pms(sk)->dev) {
724 err = -EALREADY;
725 goto done;
726 }
727
728 _pms(sk)->dev = get_mdevice(maddr->dev);
729 if (!_pms(sk)->dev) {
730 err = -ENODEV;
731 goto done;
732 }
733 sk->sk_state = MISDN_BOUND;
734
735done:
736 release_sock(sk);
737 return err;
738}
739
740static const struct proto_ops base_sock_ops = {
741 .family = PF_ISDN,
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,
757 .mmap = sock_no_mmap
758};
759
760
761static int
762base_sock_create(struct net *net, struct socket *sock, int protocol)
763{
764 struct sock *sk;
765
766 if (sock->type != SOCK_RAW)
767 return -ESOCKTNOSUPPORT;
768
769 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
770 if (!sk)
771 return -ENOMEM;
772
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);
780
781 return 0;
782}
783
784static int
Eric Paris3f378b62009-11-05 22:18:14 -0800785mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200786{
787 int err = -EPROTONOSUPPORT;
788
789 switch (proto) {
790 case ISDN_P_BASE:
791 err = base_sock_create(net, sock, proto);
792 break;
793 case ISDN_P_TE_S0:
794 case ISDN_P_NT_S0:
795 case ISDN_P_TE_E1:
796 case ISDN_P_NT_E1:
797 case ISDN_P_LAPD_TE:
798 case ISDN_P_LAPD_NT:
799 case ISDN_P_B_RAW:
800 case ISDN_P_B_HDLC:
801 case ISDN_P_B_X75SLP:
802 case ISDN_P_B_L2DTMF:
803 case ISDN_P_B_L2DSP:
804 case ISDN_P_B_L2DSPHDLC:
805 err = data_sock_create(net, sock, proto);
806 break;
807 default:
808 return err;
809 }
810
811 return err;
812}
813
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000814static const struct net_proto_family mISDN_sock_family_ops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200815 .owner = THIS_MODULE,
816 .family = PF_ISDN,
817 .create = mISDN_sock_create,
818};
819
820int
821misdn_sock_init(u_int *deb)
822{
823 int err;
824
825 debug = deb;
826 err = sock_register(&mISDN_sock_family_ops);
827 if (err)
828 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
829 return err;
830}
831
832void
833misdn_sock_cleanup(void)
834{
835 sock_unregister(PF_ISDN);
836}
837