blob: 738ea8dd0adfdd113134eff884dfd6c62cebc494 [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>
Paul Gortmaker5d76fc22011-07-10 12:23:16 -040020#include <linux/export.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020021#include "core.h"
22
Hannes Ederdfa96ec2008-12-12 21:13:45 -080023static u_int *debug;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020024
25static struct proto mISDN_proto = {
26 .name = "misdn",
27 .owner = THIS_MODULE,
28 .obj_size = sizeof(struct mISDN_sock)
29};
30
31#define _pms(sk) ((struct mISDN_sock *)sk)
32
33static struct mISDN_sock_list data_sockets = {
34 .lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
35};
36
37static struct mISDN_sock_list base_sockets = {
38 .lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
39};
40
41#define L2_HEADER_LEN 4
42
43static inline struct sk_buff *
44_l2_alloc_skb(unsigned int len, gfp_t gfp_mask)
45{
46 struct sk_buff *skb;
47
48 skb = alloc_skb(len + L2_HEADER_LEN, gfp_mask);
49 if (likely(skb))
50 skb_reserve(skb, L2_HEADER_LEN);
51 return skb;
52}
53
54static void
55mISDN_sock_link(struct mISDN_sock_list *l, struct sock *sk)
56{
57 write_lock_bh(&l->lock);
58 sk_add_node(sk, &l->head);
59 write_unlock_bh(&l->lock);
60}
61
62static void mISDN_sock_unlink(struct mISDN_sock_list *l, struct sock *sk)
63{
64 write_lock_bh(&l->lock);
65 sk_del_node_init(sk);
66 write_unlock_bh(&l->lock);
67}
68
69static int
70mISDN_send(struct mISDNchannel *ch, struct sk_buff *skb)
71{
72 struct mISDN_sock *msk;
73 int err;
74
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)
79 return -EUNATCH;
80 __net_timestamp(skb);
81 err = sock_queue_rcv_skb(&msk->sk, skb);
82 if (err)
83 printk(KERN_WARNING "%s: error %d\n", __func__, err);
84 return err;
85}
86
87static int
88mISDN_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
89{
90 struct mISDN_sock *msk;
91
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);
95 switch (cmd) {
96 case CLOSE_CHANNEL:
97 msk->sk.sk_state = MISDN_CLOSED;
98 break;
99 }
100 return 0;
101}
102
103static inline void
104mISDN_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
105{
106 struct timeval tv;
107
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);
111 }
112}
113
114static int
115mISDN_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
116 struct msghdr *msg, size_t len, int flags)
117{
118 struct sk_buff *skb;
119 struct sock *sk = sock->sk;
120 struct sockaddr_mISDN *maddr;
121
122 int copied, err;
123
124 if (*debug & DEBUG_SOCKET)
125 printk(KERN_DEBUG "%s: len %d, flags %x ch.nr %d, proto %x\n",
126 __func__, (int)len, flags, _pms(sk)->ch.nr,
127 sk->sk_protocol);
128 if (flags & (MSG_OOB))
129 return -EOPNOTSUPP;
130
131 if (sk->sk_state == MISDN_CLOSED)
132 return 0;
133
134 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
135 if (!skb)
136 return err;
137
138 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
139 msg->msg_namelen = sizeof(struct sockaddr_mISDN);
140 maddr = (struct sockaddr_mISDN *)msg->msg_name;
141 maddr->family = AF_ISDN;
142 maddr->dev = _pms(sk)->dev->id;
143 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
144 (sk->sk_protocol == ISDN_P_LAPD_NT)) {
145 maddr->channel = (mISDN_HEAD_ID(skb) >> 16) & 0xff;
146 maddr->tei = (mISDN_HEAD_ID(skb) >> 8) & 0xff;
147 maddr->sapi = mISDN_HEAD_ID(skb) & 0xff;
148 } else {
149 maddr->channel = _pms(sk)->ch.nr;
150 maddr->sapi = _pms(sk)->ch.addr & 0xFF;
151 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xFF;
152 }
153 } else {
154 if (msg->msg_namelen)
155 printk(KERN_WARNING "%s: too small namelen %d\n",
156 __func__, msg->msg_namelen);
157 msg->msg_namelen = 0;
158 }
159
160 copied = skb->len + MISDN_HEADER_LEN;
161 if (len < copied) {
162 if (flags & MSG_PEEK)
163 atomic_dec(&skb->users);
164 else
165 skb_queue_head(&sk->sk_receive_queue, skb);
166 return -ENOSPC;
167 }
168 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
169 MISDN_HEADER_LEN);
170
171 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
172
173 mISDN_sock_cmsg(sk, msg, skb);
174
175 skb_free_datagram(sk, skb);
176
177 return err ? : copied;
178}
179
180static int
181mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
182 struct msghdr *msg, size_t len)
183{
184 struct sock *sk = sock->sk;
185 struct sk_buff *skb;
186 int err = -ENOMEM;
187 struct sockaddr_mISDN *maddr;
188
189 if (*debug & DEBUG_SOCKET)
190 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
191 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
192 sk->sk_protocol);
193
194 if (msg->msg_flags & MSG_OOB)
195 return -EOPNOTSUPP;
196
197 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
198 return -EINVAL;
199
200 if (len < MISDN_HEADER_LEN)
201 return -EINVAL;
202
203 if (sk->sk_state != MISDN_BOUND)
204 return -EBADFD;
205
206 lock_sock(sk);
207
208 skb = _l2_alloc_skb(len, GFP_KERNEL);
209 if (!skb)
210 goto done;
211
212 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
213 err = -EFAULT;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000214 goto done;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200215 }
216
217 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
218 skb_pull(skb, MISDN_HEADER_LEN);
219
220 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
221 /* if we have a address, we use it */
222 maddr = (struct sockaddr_mISDN *)msg->msg_name;
223 mISDN_HEAD_ID(skb) = maddr->channel;
224 } else { /* use default for L2 messages */
225 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
226 (sk->sk_protocol == ISDN_P_LAPD_NT))
Karsten Keileac74af2009-05-22 11:04:56 +0000227 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200228 }
229
230 if (*debug & DEBUG_SOCKET)
231 printk(KERN_DEBUG "%s: ID:%x\n",
232 __func__, mISDN_HEAD_ID(skb));
233
234 err = -ENODEV;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000235 if (!_pms(sk)->ch.peer)
236 goto done;
237 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
238 if (err)
239 goto done;
240 else {
241 skb = NULL;
242 err = len;
243 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200244
245done:
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000246 if (skb)
247 kfree_skb(skb);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200248 release_sock(sk);
249 return err;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200250}
251
252static int
253data_sock_release(struct socket *sock)
254{
255 struct sock *sk = sock->sk;
256
257 if (*debug & DEBUG_SOCKET)
258 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
259 if (!sk)
260 return 0;
261 switch (sk->sk_protocol) {
262 case ISDN_P_TE_S0:
263 case ISDN_P_NT_S0:
264 case ISDN_P_TE_E1:
265 case ISDN_P_NT_E1:
266 if (sk->sk_state == MISDN_BOUND)
267 delete_channel(&_pms(sk)->ch);
268 else
269 mISDN_sock_unlink(&data_sockets, sk);
270 break;
271 case ISDN_P_LAPD_TE:
272 case ISDN_P_LAPD_NT:
273 case ISDN_P_B_RAW:
274 case ISDN_P_B_HDLC:
275 case ISDN_P_B_X75SLP:
276 case ISDN_P_B_L2DTMF:
277 case ISDN_P_B_L2DSP:
278 case ISDN_P_B_L2DSPHDLC:
279 delete_channel(&_pms(sk)->ch);
280 mISDN_sock_unlink(&data_sockets, sk);
281 break;
282 }
283
284 lock_sock(sk);
285
286 sock_orphan(sk);
287 skb_queue_purge(&sk->sk_receive_queue);
288
289 release_sock(sk);
290 sock_put(sk);
291
292 return 0;
293}
294
295static int
296data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
297{
298 struct mISDN_ctrl_req cq;
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000299 int err = -EINVAL, val[2];
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200300 struct mISDNchannel *bchan, *next;
301
302 lock_sock(sk);
303 if (!_pms(sk)->dev) {
304 err = -ENODEV;
305 goto done;
306 }
307 switch (cmd) {
308 case IMCTRLREQ:
309 if (copy_from_user(&cq, p, sizeof(cq))) {
310 err = -EFAULT;
311 break;
312 }
313 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
314 list_for_each_entry_safe(bchan, next,
315 &_pms(sk)->dev->bchannels, list) {
316 if (bchan->nr == cq.channel) {
317 err = bchan->ctrl(bchan,
318 CONTROL_CHANNEL, &cq);
319 break;
320 }
321 }
322 } else
323 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
324 CONTROL_CHANNEL, &cq);
325 if (err)
326 break;
327 if (copy_to_user(p, &cq, sizeof(cq)))
328 err = -EFAULT;
329 break;
330 case IMCLEAR_L2:
331 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
332 err = -EINVAL;
333 break;
334 }
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000335 val[0] = cmd;
336 if (get_user(val[1], (int __user *)p)) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200337 err = -EFAULT;
338 break;
339 }
340 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000341 CONTROL_CHANNEL, val);
342 break;
343 case IMHOLD_L1:
344 if (sk->sk_protocol != ISDN_P_LAPD_NT
345 && sk->sk_protocol != ISDN_P_LAPD_TE) {
346 err = -EINVAL;
347 break;
348 }
349 val[0] = cmd;
350 if (get_user(val[1], (int __user *)p)) {
351 err = -EFAULT;
352 break;
353 }
354 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
355 CONTROL_CHANNEL, val);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200356 break;
357 default:
358 err = -EINVAL;
359 break;
360 }
361done:
362 release_sock(sk);
363 return err;
364}
365
366static int
367data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
368{
369 int err = 0, id;
370 struct sock *sk = sock->sk;
371 struct mISDNdevice *dev;
372 struct mISDNversion ver;
373
374 switch (cmd) {
375 case IMGETVERSION:
376 ver.major = MISDN_MAJOR_VERSION;
377 ver.minor = MISDN_MINOR_VERSION;
378 ver.release = MISDN_RELEASE;
379 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
380 err = -EFAULT;
381 break;
382 case IMGETCOUNT:
383 id = get_mdevice_count();
384 if (put_user(id, (int __user *)arg))
385 err = -EFAULT;
386 break;
387 case IMGETDEVINFO:
388 if (get_user(id, (int __user *)arg)) {
389 err = -EFAULT;
390 break;
391 }
392 dev = get_mdevice(id);
393 if (dev) {
394 struct mISDN_devinfo di;
395
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000396 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200397 di.id = dev->id;
398 di.Dprotocols = dev->Dprotocols;
399 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
400 di.protocol = dev->D.protocol;
401 memcpy(di.channelmap, dev->channelmap,
Karsten Keilff4cc1d2008-07-30 18:26:58 +0200402 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200403 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200404 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200405 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
406 err = -EFAULT;
407 } else
408 err = -ENODEV;
409 break;
410 default:
411 if (sk->sk_state == MISDN_BOUND)
412 err = data_sock_ioctl_bound(sk, cmd,
413 (void __user *)arg);
414 else
415 err = -ENOTCONN;
416 }
417 return err;
418}
419
420static int data_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700421 char __user *optval, unsigned int len)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200422{
423 struct sock *sk = sock->sk;
424 int err = 0, opt = 0;
425
426 if (*debug & DEBUG_SOCKET)
427 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
428 level, optname, optval, len);
429
430 lock_sock(sk);
431
432 switch (optname) {
433 case MISDN_TIME_STAMP:
434 if (get_user(opt, (int __user *)optval)) {
435 err = -EFAULT;
436 break;
437 }
438
439 if (opt)
440 _pms(sk)->cmask |= MISDN_TIME_STAMP;
441 else
442 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
443 break;
444 default:
445 err = -ENOPROTOOPT;
446 break;
447 }
448 release_sock(sk);
449 return err;
450}
451
452static int data_sock_getsockopt(struct socket *sock, int level, int optname,
453 char __user *optval, int __user *optlen)
454{
455 struct sock *sk = sock->sk;
456 int len, opt;
457
458 if (get_user(len, optlen))
459 return -EFAULT;
460
David S. Miller81b424d2011-04-17 16:48:53 -0700461 if (len != sizeof(char))
462 return -EINVAL;
463
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200464 switch (optname) {
465 case MISDN_TIME_STAMP:
466 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
467 opt = 1;
468 else
469 opt = 0;
470
471 if (put_user(opt, optval))
472 return -EFAULT;
473 break;
474 default:
475 return -ENOPROTOOPT;
476 }
477
478 return 0;
479}
480
481static int
482data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
483{
484 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
485 struct sock *sk = sock->sk;
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200486 struct hlist_node *node;
487 struct sock *csk;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200488 int err = 0;
489
490 if (*debug & DEBUG_SOCKET)
491 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
492 if (addr_len != sizeof(struct sockaddr_mISDN))
493 return -EINVAL;
494 if (!maddr || maddr->family != AF_ISDN)
495 return -EINVAL;
496
497 lock_sock(sk);
498
499 if (_pms(sk)->dev) {
500 err = -EALREADY;
501 goto done;
502 }
503 _pms(sk)->dev = get_mdevice(maddr->dev);
504 if (!_pms(sk)->dev) {
505 err = -ENODEV;
506 goto done;
507 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200508
Andreas Eversberg9a812552008-09-14 14:42:18 +0200509 if (sk->sk_protocol < ISDN_P_B_START) {
510 read_lock_bh(&data_sockets.lock);
511 sk_for_each(csk, node, &data_sockets.head) {
512 if (sk == csk)
513 continue;
514 if (_pms(csk)->dev != _pms(sk)->dev)
515 continue;
516 if (csk->sk_protocol >= ISDN_P_B_START)
517 continue;
518 if (IS_ISDN_P_TE(csk->sk_protocol)
519 == IS_ISDN_P_TE(sk->sk_protocol))
520 continue;
521 read_unlock_bh(&data_sockets.lock);
522 err = -EBUSY;
523 goto done;
524 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200525 read_unlock_bh(&data_sockets.lock);
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200526 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200527
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200528 _pms(sk)->ch.send = mISDN_send;
529 _pms(sk)->ch.ctrl = mISDN_ctrl;
530
531 switch (sk->sk_protocol) {
532 case ISDN_P_TE_S0:
533 case ISDN_P_NT_S0:
534 case ISDN_P_TE_E1:
535 case ISDN_P_NT_E1:
536 mISDN_sock_unlink(&data_sockets, sk);
537 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
538 sk->sk_protocol, maddr);
539 if (err)
540 mISDN_sock_link(&data_sockets, sk);
541 break;
542 case ISDN_P_LAPD_TE:
543 case ISDN_P_LAPD_NT:
544 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
545 sk->sk_protocol, maddr);
546 break;
547 case ISDN_P_B_RAW:
548 case ISDN_P_B_HDLC:
549 case ISDN_P_B_X75SLP:
550 case ISDN_P_B_L2DTMF:
551 case ISDN_P_B_L2DSP:
552 case ISDN_P_B_L2DSPHDLC:
553 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
554 sk->sk_protocol, maddr);
555 break;
556 default:
557 err = -EPROTONOSUPPORT;
558 }
559 if (err)
560 goto done;
561 sk->sk_state = MISDN_BOUND;
562 _pms(sk)->ch.protocol = sk->sk_protocol;
563
564done:
565 release_sock(sk);
566 return err;
567}
568
569static int
570data_sock_getname(struct socket *sock, struct sockaddr *addr,
571 int *addr_len, int peer)
572{
573 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
574 struct sock *sk = sock->sk;
575
576 if (!_pms(sk)->dev)
577 return -EBADFD;
578
579 lock_sock(sk);
580
581 *addr_len = sizeof(*maddr);
582 maddr->dev = _pms(sk)->dev->id;
583 maddr->channel = _pms(sk)->ch.nr;
584 maddr->sapi = _pms(sk)->ch.addr & 0xff;
585 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
586 release_sock(sk);
587 return 0;
588}
589
590static const struct proto_ops data_sock_ops = {
591 .family = PF_ISDN,
592 .owner = THIS_MODULE,
593 .release = data_sock_release,
594 .ioctl = data_sock_ioctl,
595 .bind = data_sock_bind,
596 .getname = data_sock_getname,
597 .sendmsg = mISDN_sock_sendmsg,
598 .recvmsg = mISDN_sock_recvmsg,
599 .poll = datagram_poll,
600 .listen = sock_no_listen,
601 .shutdown = sock_no_shutdown,
602 .setsockopt = data_sock_setsockopt,
603 .getsockopt = data_sock_getsockopt,
604 .connect = sock_no_connect,
605 .socketpair = sock_no_socketpair,
606 .accept = sock_no_accept,
607 .mmap = sock_no_mmap
608};
609
610static int
611data_sock_create(struct net *net, struct socket *sock, int protocol)
612{
613 struct sock *sk;
614
615 if (sock->type != SOCK_DGRAM)
616 return -ESOCKTNOSUPPORT;
617
618 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
619 if (!sk)
620 return -ENOMEM;
621
622 sock_init_data(sock, sk);
623
624 sock->ops = &data_sock_ops;
625 sock->state = SS_UNCONNECTED;
626 sock_reset_flag(sk, SOCK_ZAPPED);
627
628 sk->sk_protocol = protocol;
629 sk->sk_state = MISDN_OPEN;
630 mISDN_sock_link(&data_sockets, sk);
631
632 return 0;
633}
634
635static int
636base_sock_release(struct socket *sock)
637{
638 struct sock *sk = sock->sk;
639
640 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
641 if (!sk)
642 return 0;
643
644 mISDN_sock_unlink(&base_sockets, sk);
645 sock_orphan(sk);
646 sock_put(sk);
647
648 return 0;
649}
650
651static int
652base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
653{
654 int err = 0, id;
655 struct mISDNdevice *dev;
656 struct mISDNversion ver;
657
658 switch (cmd) {
659 case IMGETVERSION:
660 ver.major = MISDN_MAJOR_VERSION;
661 ver.minor = MISDN_MINOR_VERSION;
662 ver.release = MISDN_RELEASE;
663 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
664 err = -EFAULT;
665 break;
666 case IMGETCOUNT:
667 id = get_mdevice_count();
668 if (put_user(id, (int __user *)arg))
669 err = -EFAULT;
670 break;
671 case IMGETDEVINFO:
672 if (get_user(id, (int __user *)arg)) {
673 err = -EFAULT;
674 break;
675 }
676 dev = get_mdevice(id);
677 if (dev) {
678 struct mISDN_devinfo di;
679
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000680 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200681 di.id = dev->id;
682 di.Dprotocols = dev->Dprotocols;
683 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
684 di.protocol = dev->D.protocol;
685 memcpy(di.channelmap, dev->channelmap,
Karsten Keilff4cc1d2008-07-30 18:26:58 +0200686 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200687 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200688 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200689 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
690 err = -EFAULT;
691 } else
692 err = -ENODEV;
693 break;
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200694 case IMSETDEVNAME:
695 {
696 struct mISDN_devrename dn;
697 if (copy_from_user(&dn, (void __user *)arg,
698 sizeof(dn))) {
699 err = -EFAULT;
700 break;
701 }
702 dev = get_mdevice(dn.id);
703 if (dev)
Matthias Urlichs837468d2008-08-16 00:04:33 +0200704 err = device_rename(&dev->dev, dn.name);
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200705 else
706 err = -ENODEV;
707 }
708 break;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200709 default:
710 err = -EINVAL;
711 }
712 return err;
713}
714
715static int
716base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
717{
718 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
719 struct sock *sk = sock->sk;
720 int err = 0;
721
722 if (!maddr || maddr->family != AF_ISDN)
723 return -EINVAL;
724
725 lock_sock(sk);
726
727 if (_pms(sk)->dev) {
728 err = -EALREADY;
729 goto done;
730 }
731
732 _pms(sk)->dev = get_mdevice(maddr->dev);
733 if (!_pms(sk)->dev) {
734 err = -ENODEV;
735 goto done;
736 }
737 sk->sk_state = MISDN_BOUND;
738
739done:
740 release_sock(sk);
741 return err;
742}
743
744static const struct proto_ops base_sock_ops = {
745 .family = PF_ISDN,
746 .owner = THIS_MODULE,
747 .release = base_sock_release,
748 .ioctl = base_sock_ioctl,
749 .bind = base_sock_bind,
750 .getname = sock_no_getname,
751 .sendmsg = sock_no_sendmsg,
752 .recvmsg = sock_no_recvmsg,
753 .poll = sock_no_poll,
754 .listen = sock_no_listen,
755 .shutdown = sock_no_shutdown,
756 .setsockopt = sock_no_setsockopt,
757 .getsockopt = sock_no_getsockopt,
758 .connect = sock_no_connect,
759 .socketpair = sock_no_socketpair,
760 .accept = sock_no_accept,
761 .mmap = sock_no_mmap
762};
763
764
765static int
766base_sock_create(struct net *net, struct socket *sock, int protocol)
767{
768 struct sock *sk;
769
770 if (sock->type != SOCK_RAW)
771 return -ESOCKTNOSUPPORT;
772
773 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
774 if (!sk)
775 return -ENOMEM;
776
777 sock_init_data(sock, sk);
778 sock->ops = &base_sock_ops;
779 sock->state = SS_UNCONNECTED;
780 sock_reset_flag(sk, SOCK_ZAPPED);
781 sk->sk_protocol = protocol;
782 sk->sk_state = MISDN_OPEN;
783 mISDN_sock_link(&base_sockets, sk);
784
785 return 0;
786}
787
788static int
Eric Paris3f378b62009-11-05 22:18:14 -0800789mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200790{
791 int err = -EPROTONOSUPPORT;
792
793 switch (proto) {
794 case ISDN_P_BASE:
795 err = base_sock_create(net, sock, proto);
796 break;
797 case ISDN_P_TE_S0:
798 case ISDN_P_NT_S0:
799 case ISDN_P_TE_E1:
800 case ISDN_P_NT_E1:
801 case ISDN_P_LAPD_TE:
802 case ISDN_P_LAPD_NT:
803 case ISDN_P_B_RAW:
804 case ISDN_P_B_HDLC:
805 case ISDN_P_B_X75SLP:
806 case ISDN_P_B_L2DTMF:
807 case ISDN_P_B_L2DSP:
808 case ISDN_P_B_L2DSPHDLC:
809 err = data_sock_create(net, sock, proto);
810 break;
811 default:
812 return err;
813 }
814
815 return err;
816}
817
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000818static const struct net_proto_family mISDN_sock_family_ops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200819 .owner = THIS_MODULE,
820 .family = PF_ISDN,
821 .create = mISDN_sock_create,
822};
823
824int
825misdn_sock_init(u_int *deb)
826{
827 int err;
828
829 debug = deb;
830 err = sock_register(&mISDN_sock_family_ops);
831 if (err)
832 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
833 return err;
834}
835
836void
837misdn_sock_cleanup(void)
838{
839 sock_unregister(PF_ISDN);
840}
841