blob: 0d29b5a6356d729a6ceb987bccc8c2dbe6779823 [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
Ying Xue1b784142015-03-02 15:37:48 +0800115mISDN_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
116 int flags)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200117{
118 struct sk_buff *skb;
119 struct sock *sk = sock->sk;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200120
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",
Joe Perches475be4d2012-02-19 19:52:38 -0800125 __func__, (int)len, flags, _pms(sk)->ch.nr,
126 sk->sk_protocol);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200127 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
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +0100137 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100138 DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +0100139
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200140 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 }
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +0100152 msg->msg_namelen = sizeof(*maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200153 }
154
155 copied = skb->len + MISDN_HEADER_LEN;
156 if (len < copied) {
157 if (flags & MSG_PEEK)
158 atomic_dec(&skb->users);
159 else
160 skb_queue_head(&sk->sk_receive_queue, skb);
161 return -ENOSPC;
162 }
163 memcpy(skb_push(skb, MISDN_HEADER_LEN), mISDN_HEAD_P(skb),
Joe Perches475be4d2012-02-19 19:52:38 -0800164 MISDN_HEADER_LEN);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200165
David S. Miller51f3d022014-11-05 16:46:40 -0500166 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200167
168 mISDN_sock_cmsg(sk, msg, skb);
169
170 skb_free_datagram(sk, skb);
171
172 return err ? : copied;
173}
174
175static int
Ying Xue1b784142015-03-02 15:37:48 +0800176mISDN_sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200177{
178 struct sock *sk = sock->sk;
179 struct sk_buff *skb;
180 int err = -ENOMEM;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200181
182 if (*debug & DEBUG_SOCKET)
183 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800184 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
185 sk->sk_protocol);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200186
187 if (msg->msg_flags & MSG_OOB)
188 return -EOPNOTSUPP;
189
Joe Perches475be4d2012-02-19 19:52:38 -0800190 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200191 return -EINVAL;
192
193 if (len < MISDN_HEADER_LEN)
194 return -EINVAL;
195
196 if (sk->sk_state != MISDN_BOUND)
197 return -EBADFD;
198
199 lock_sock(sk);
200
201 skb = _l2_alloc_skb(len, GFP_KERNEL);
202 if (!skb)
203 goto done;
204
Al Viro6ce8e9c2014-04-06 21:25:44 -0400205 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200206 err = -EFAULT;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000207 goto done;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200208 }
209
210 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
211 skb_pull(skb, MISDN_HEADER_LEN);
212
213 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
214 /* if we have a address, we use it */
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100215 DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200216 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))
Karsten Keileac74af2009-05-22 11:04:56 +0000220 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200221 }
222
223 if (*debug & DEBUG_SOCKET)
224 printk(KERN_DEBUG "%s: ID:%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800225 __func__, mISDN_HEAD_ID(skb));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200226
227 err = -ENODEV;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000228 if (!_pms(sk)->ch.peer)
229 goto done;
230 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
231 if (err)
232 goto done;
233 else {
234 skb = NULL;
235 err = len;
236 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200237
238done:
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000239 if (skb)
240 kfree_skb(skb);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200241 release_sock(sk);
242 return err;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200243}
244
245static int
246data_sock_release(struct socket *sock)
247{
248 struct sock *sk = sock->sk;
249
250 if (*debug & DEBUG_SOCKET)
251 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
252 if (!sk)
253 return 0;
254 switch (sk->sk_protocol) {
255 case ISDN_P_TE_S0:
256 case ISDN_P_NT_S0:
257 case ISDN_P_TE_E1:
258 case ISDN_P_NT_E1:
259 if (sk->sk_state == MISDN_BOUND)
260 delete_channel(&_pms(sk)->ch);
261 else
262 mISDN_sock_unlink(&data_sockets, sk);
263 break;
264 case ISDN_P_LAPD_TE:
265 case ISDN_P_LAPD_NT:
266 case ISDN_P_B_RAW:
267 case ISDN_P_B_HDLC:
268 case ISDN_P_B_X75SLP:
269 case ISDN_P_B_L2DTMF:
270 case ISDN_P_B_L2DSP:
271 case ISDN_P_B_L2DSPHDLC:
272 delete_channel(&_pms(sk)->ch);
273 mISDN_sock_unlink(&data_sockets, sk);
274 break;
275 }
276
277 lock_sock(sk);
278
279 sock_orphan(sk);
280 skb_queue_purge(&sk->sk_receive_queue);
281
282 release_sock(sk);
283 sock_put(sk);
284
285 return 0;
286}
287
288static int
289data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
290{
291 struct mISDN_ctrl_req cq;
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000292 int err = -EINVAL, val[2];
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200293 struct mISDNchannel *bchan, *next;
294
295 lock_sock(sk);
296 if (!_pms(sk)->dev) {
297 err = -ENODEV;
298 goto done;
299 }
300 switch (cmd) {
301 case IMCTRLREQ:
302 if (copy_from_user(&cq, p, sizeof(cq))) {
303 err = -EFAULT;
304 break;
305 }
306 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
307 list_for_each_entry_safe(bchan, next,
Joe Perches475be4d2012-02-19 19:52:38 -0800308 &_pms(sk)->dev->bchannels, list) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200309 if (bchan->nr == cq.channel) {
310 err = bchan->ctrl(bchan,
Joe Perches475be4d2012-02-19 19:52:38 -0800311 CONTROL_CHANNEL, &cq);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200312 break;
313 }
314 }
315 } else
316 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
Joe Perches475be4d2012-02-19 19:52:38 -0800317 CONTROL_CHANNEL, &cq);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200318 if (err)
319 break;
320 if (copy_to_user(p, &cq, sizeof(cq)))
321 err = -EFAULT;
322 break;
323 case IMCLEAR_L2:
324 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
325 err = -EINVAL;
326 break;
327 }
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000328 val[0] = cmd;
329 if (get_user(val[1], (int __user *)p)) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200330 err = -EFAULT;
331 break;
332 }
333 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
Joe Perches475be4d2012-02-19 19:52:38 -0800334 CONTROL_CHANNEL, val);
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000335 break;
336 case IMHOLD_L1:
337 if (sk->sk_protocol != ISDN_P_LAPD_NT
Joe Perches475be4d2012-02-19 19:52:38 -0800338 && sk->sk_protocol != ISDN_P_LAPD_TE) {
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000339 err = -EINVAL;
340 break;
341 }
342 val[0] = cmd;
343 if (get_user(val[1], (int __user *)p)) {
344 err = -EFAULT;
345 break;
346 }
347 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
Joe Perches475be4d2012-02-19 19:52:38 -0800348 CONTROL_CHANNEL, val);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200349 break;
350 default:
351 err = -EINVAL;
352 break;
353 }
354done:
355 release_sock(sk);
356 return err;
357}
358
359static int
360data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
361{
Joe Perches475be4d2012-02-19 19:52:38 -0800362 int err = 0, id;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200363 struct sock *sk = sock->sk;
364 struct mISDNdevice *dev;
365 struct mISDNversion ver;
366
367 switch (cmd) {
368 case IMGETVERSION:
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)))
373 err = -EFAULT;
374 break;
375 case IMGETCOUNT:
376 id = get_mdevice_count();
377 if (put_user(id, (int __user *)arg))
378 err = -EFAULT;
379 break;
380 case IMGETDEVINFO:
381 if (get_user(id, (int __user *)arg)) {
382 err = -EFAULT;
383 break;
384 }
385 dev = get_mdevice(id);
386 if (dev) {
387 struct mISDN_devinfo di;
388
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000389 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200390 di.id = dev->id;
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800395 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200396 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200397 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200398 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
399 err = -EFAULT;
400 } else
401 err = -ENODEV;
402 break;
403 default:
404 if (sk->sk_state == MISDN_BOUND)
405 err = data_sock_ioctl_bound(sk, cmd,
Joe Perches475be4d2012-02-19 19:52:38 -0800406 (void __user *)arg);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200407 else
408 err = -ENOTCONN;
409 }
410 return err;
411}
412
413static int data_sock_setsockopt(struct socket *sock, int level, int optname,
Joe Perches475be4d2012-02-19 19:52:38 -0800414 char __user *optval, unsigned int len)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200415{
416 struct sock *sk = sock->sk;
417 int err = 0, opt = 0;
418
419 if (*debug & DEBUG_SOCKET)
420 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
Joe Perches475be4d2012-02-19 19:52:38 -0800421 level, optname, optval, len);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200422
423 lock_sock(sk);
424
425 switch (optname) {
426 case MISDN_TIME_STAMP:
427 if (get_user(opt, (int __user *)optval)) {
428 err = -EFAULT;
429 break;
430 }
431
432 if (opt)
433 _pms(sk)->cmask |= MISDN_TIME_STAMP;
434 else
435 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
436 break;
437 default:
438 err = -ENOPROTOOPT;
439 break;
440 }
441 release_sock(sk);
442 return err;
443}
444
445static int data_sock_getsockopt(struct socket *sock, int level, int optname,
Joe Perches475be4d2012-02-19 19:52:38 -0800446 char __user *optval, int __user *optlen)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200447{
448 struct sock *sk = sock->sk;
449 int len, opt;
450
451 if (get_user(len, optlen))
452 return -EFAULT;
453
David S. Miller81b424d2011-04-17 16:48:53 -0700454 if (len != sizeof(char))
455 return -EINVAL;
456
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200457 switch (optname) {
458 case MISDN_TIME_STAMP:
459 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
460 opt = 1;
461 else
462 opt = 0;
463
464 if (put_user(opt, optval))
465 return -EFAULT;
466 break;
467 default:
468 return -ENOPROTOOPT;
469 }
470
471 return 0;
472}
473
474static int
475data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
476{
477 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
478 struct sock *sk = sock->sk;
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200479 struct sock *csk;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200480 int err = 0;
481
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))
485 return -EINVAL;
486 if (!maddr || maddr->family != AF_ISDN)
487 return -EINVAL;
488
489 lock_sock(sk);
490
491 if (_pms(sk)->dev) {
492 err = -EALREADY;
493 goto done;
494 }
495 _pms(sk)->dev = get_mdevice(maddr->dev);
496 if (!_pms(sk)->dev) {
497 err = -ENODEV;
498 goto done;
499 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200500
Andreas Eversberg9a812552008-09-14 14:42:18 +0200501 if (sk->sk_protocol < ISDN_P_B_START) {
502 read_lock_bh(&data_sockets.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800503 sk_for_each(csk, &data_sockets.head) {
Andreas Eversberg9a812552008-09-14 14:42:18 +0200504 if (sk == csk)
505 continue;
506 if (_pms(csk)->dev != _pms(sk)->dev)
507 continue;
508 if (csk->sk_protocol >= ISDN_P_B_START)
509 continue;
510 if (IS_ISDN_P_TE(csk->sk_protocol)
Joe Perches475be4d2012-02-19 19:52:38 -0800511 == IS_ISDN_P_TE(sk->sk_protocol))
Andreas Eversberg9a812552008-09-14 14:42:18 +0200512 continue;
513 read_unlock_bh(&data_sockets.lock);
514 err = -EBUSY;
515 goto done;
516 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200517 read_unlock_bh(&data_sockets.lock);
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200518 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200519
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200520 _pms(sk)->ch.send = mISDN_send;
521 _pms(sk)->ch.ctrl = mISDN_ctrl;
522
523 switch (sk->sk_protocol) {
524 case ISDN_P_TE_S0:
525 case ISDN_P_NT_S0:
526 case ISDN_P_TE_E1:
527 case ISDN_P_NT_E1:
528 mISDN_sock_unlink(&data_sockets, sk);
529 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800530 sk->sk_protocol, maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200531 if (err)
532 mISDN_sock_link(&data_sockets, sk);
533 break;
534 case ISDN_P_LAPD_TE:
535 case ISDN_P_LAPD_NT:
536 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800537 sk->sk_protocol, maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200538 break;
539 case ISDN_P_B_RAW:
540 case ISDN_P_B_HDLC:
541 case ISDN_P_B_X75SLP:
542 case ISDN_P_B_L2DTMF:
543 case ISDN_P_B_L2DSP:
544 case ISDN_P_B_L2DSPHDLC:
545 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800546 sk->sk_protocol, maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200547 break;
548 default:
549 err = -EPROTONOSUPPORT;
550 }
551 if (err)
552 goto done;
553 sk->sk_state = MISDN_BOUND;
554 _pms(sk)->ch.protocol = sk->sk_protocol;
555
556done:
557 release_sock(sk);
558 return err;
559}
560
561static int
562data_sock_getname(struct socket *sock, struct sockaddr *addr,
Joe Perches475be4d2012-02-19 19:52:38 -0800563 int *addr_len, int peer)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200564{
Joe Perches475be4d2012-02-19 19:52:38 -0800565 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200566 struct sock *sk = sock->sk;
567
568 if (!_pms(sk)->dev)
569 return -EBADFD;
570
571 lock_sock(sk);
572
573 *addr_len = sizeof(*maddr);
Dan Carpenterd9152092013-04-22 20:24:14 +0000574 maddr->family = AF_ISDN;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200575 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;
579 release_sock(sk);
580 return 0;
581}
582
583static const struct proto_ops data_sock_ops = {
584 .family = PF_ISDN,
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,
600 .mmap = sock_no_mmap
601};
602
603static int
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500604data_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200605{
606 struct sock *sk;
607
608 if (sock->type != SOCK_DGRAM)
609 return -ESOCKTNOSUPPORT;
610
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500611 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200612 if (!sk)
613 return -ENOMEM;
614
615 sock_init_data(sock, sk);
616
617 sock->ops = &data_sock_ops;
618 sock->state = SS_UNCONNECTED;
619 sock_reset_flag(sk, SOCK_ZAPPED);
620
621 sk->sk_protocol = protocol;
622 sk->sk_state = MISDN_OPEN;
623 mISDN_sock_link(&data_sockets, sk);
624
625 return 0;
626}
627
628static int
629base_sock_release(struct socket *sock)
630{
631 struct sock *sk = sock->sk;
632
633 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
634 if (!sk)
635 return 0;
636
637 mISDN_sock_unlink(&base_sockets, sk);
638 sock_orphan(sk);
639 sock_put(sk);
640
641 return 0;
642}
643
644static int
645base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
646{
Joe Perches475be4d2012-02-19 19:52:38 -0800647 int err = 0, id;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200648 struct mISDNdevice *dev;
649 struct mISDNversion ver;
650
651 switch (cmd) {
652 case IMGETVERSION:
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)))
657 err = -EFAULT;
658 break;
659 case IMGETCOUNT:
660 id = get_mdevice_count();
661 if (put_user(id, (int __user *)arg))
662 err = -EFAULT;
663 break;
664 case IMGETDEVINFO:
665 if (get_user(id, (int __user *)arg)) {
666 err = -EFAULT;
667 break;
668 }
669 dev = get_mdevice(id);
670 if (dev) {
671 struct mISDN_devinfo di;
672
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000673 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200674 di.id = dev->id;
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800679 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200680 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200681 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200682 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
683 err = -EFAULT;
684 } else
685 err = -ENODEV;
686 break;
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200687 case IMSETDEVNAME:
Joe Perches475be4d2012-02-19 19:52:38 -0800688 {
689 struct mISDN_devrename dn;
690 if (copy_from_user(&dn, (void __user *)arg,
691 sizeof(dn))) {
692 err = -EFAULT;
693 break;
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200694 }
Joe Perches475be4d2012-02-19 19:52:38 -0800695 dev = get_mdevice(dn.id);
696 if (dev)
697 err = device_rename(&dev->dev, dn.name);
698 else
699 err = -ENODEV;
700 }
701 break;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200702 default:
703 err = -EINVAL;
704 }
705 return err;
706}
707
708static int
709base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
710{
711 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
712 struct sock *sk = sock->sk;
713 int err = 0;
714
715 if (!maddr || maddr->family != AF_ISDN)
716 return -EINVAL;
717
718 lock_sock(sk);
719
720 if (_pms(sk)->dev) {
721 err = -EALREADY;
722 goto done;
723 }
724
725 _pms(sk)->dev = get_mdevice(maddr->dev);
726 if (!_pms(sk)->dev) {
727 err = -ENODEV;
728 goto done;
729 }
730 sk->sk_state = MISDN_BOUND;
731
732done:
733 release_sock(sk);
734 return err;
735}
736
737static const struct proto_ops base_sock_ops = {
738 .family = PF_ISDN,
739 .owner = THIS_MODULE,
740 .release = base_sock_release,
741 .ioctl = base_sock_ioctl,
742 .bind = base_sock_bind,
743 .getname = sock_no_getname,
744 .sendmsg = sock_no_sendmsg,
745 .recvmsg = sock_no_recvmsg,
746 .poll = sock_no_poll,
747 .listen = sock_no_listen,
748 .shutdown = sock_no_shutdown,
749 .setsockopt = sock_no_setsockopt,
750 .getsockopt = sock_no_getsockopt,
751 .connect = sock_no_connect,
752 .socketpair = sock_no_socketpair,
753 .accept = sock_no_accept,
754 .mmap = sock_no_mmap
755};
756
757
758static int
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500759base_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200760{
761 struct sock *sk;
762
763 if (sock->type != SOCK_RAW)
764 return -ESOCKTNOSUPPORT;
765
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500766 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200767 if (!sk)
768 return -ENOMEM;
769
770 sock_init_data(sock, sk);
771 sock->ops = &base_sock_ops;
772 sock->state = SS_UNCONNECTED;
773 sock_reset_flag(sk, SOCK_ZAPPED);
774 sk->sk_protocol = protocol;
775 sk->sk_state = MISDN_OPEN;
776 mISDN_sock_link(&base_sockets, sk);
777
778 return 0;
779}
780
781static int
Eric Paris3f378b62009-11-05 22:18:14 -0800782mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200783{
784 int err = -EPROTONOSUPPORT;
785
Joe Perches475be4d2012-02-19 19:52:38 -0800786 switch (proto) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200787 case ISDN_P_BASE:
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500788 err = base_sock_create(net, sock, proto, kern);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200789 break;
790 case ISDN_P_TE_S0:
791 case ISDN_P_NT_S0:
792 case ISDN_P_TE_E1:
793 case ISDN_P_NT_E1:
794 case ISDN_P_LAPD_TE:
795 case ISDN_P_LAPD_NT:
796 case ISDN_P_B_RAW:
797 case ISDN_P_B_HDLC:
798 case ISDN_P_B_X75SLP:
799 case ISDN_P_B_L2DTMF:
800 case ISDN_P_B_L2DSP:
801 case ISDN_P_B_L2DSPHDLC:
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500802 err = data_sock_create(net, sock, proto, kern);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200803 break;
804 default:
805 return err;
806 }
807
808 return err;
809}
810
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000811static const struct net_proto_family mISDN_sock_family_ops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200812 .owner = THIS_MODULE,
813 .family = PF_ISDN,
814 .create = mISDN_sock_create,
815};
816
817int
818misdn_sock_init(u_int *deb)
819{
820 int err;
821
822 debug = deb;
823 err = sock_register(&mISDN_sock_family_ops);
824 if (err)
825 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
826 return err;
827}
828
829void
830misdn_sock_cleanup(void)
831{
832 sock_unregister(PF_ISDN);
833}