blob: 1be82284cf9dfaa8ab5f5078bed78c3aec095ed3 [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,
Joe Perches475be4d2012-02-19 19:52:38 -0800116 struct msghdr *msg, size_t len, 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
166 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
167
168 mISDN_sock_cmsg(sk, msg, skb);
169
170 skb_free_datagram(sk, skb);
171
172 return err ? : copied;
173}
174
175static int
176mISDN_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Joe Perches475be4d2012-02-19 19:52:38 -0800177 struct msghdr *msg, size_t len)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200178{
179 struct sock *sk = sock->sk;
180 struct sk_buff *skb;
181 int err = -ENOMEM;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200182
183 if (*debug & DEBUG_SOCKET)
184 printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800185 __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
186 sk->sk_protocol);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200187
188 if (msg->msg_flags & MSG_OOB)
189 return -EOPNOTSUPP;
190
Joe Perches475be4d2012-02-19 19:52:38 -0800191 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200192 return -EINVAL;
193
194 if (len < MISDN_HEADER_LEN)
195 return -EINVAL;
196
197 if (sk->sk_state != MISDN_BOUND)
198 return -EBADFD;
199
200 lock_sock(sk);
201
202 skb = _l2_alloc_skb(len, GFP_KERNEL);
203 if (!skb)
204 goto done;
205
206 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
207 err = -EFAULT;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000208 goto done;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200209 }
210
211 memcpy(mISDN_HEAD_P(skb), skb->data, MISDN_HEADER_LEN);
212 skb_pull(skb, MISDN_HEADER_LEN);
213
214 if (msg->msg_namelen >= sizeof(struct sockaddr_mISDN)) {
215 /* if we have a address, we use it */
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100216 DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200217 mISDN_HEAD_ID(skb) = maddr->channel;
218 } else { /* use default for L2 messages */
219 if ((sk->sk_protocol == ISDN_P_LAPD_TE) ||
220 (sk->sk_protocol == ISDN_P_LAPD_NT))
Karsten Keileac74af2009-05-22 11:04:56 +0000221 mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200222 }
223
224 if (*debug & DEBUG_SOCKET)
225 printk(KERN_DEBUG "%s: ID:%x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800226 __func__, mISDN_HEAD_ID(skb));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200227
228 err = -ENODEV;
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000229 if (!_pms(sk)->ch.peer)
230 goto done;
231 err = _pms(sk)->ch.recv(_pms(sk)->ch.peer, skb);
232 if (err)
233 goto done;
234 else {
235 skb = NULL;
236 err = len;
237 }
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200238
239done:
Karsten Keil5df3b8b2009-05-22 11:04:52 +0000240 if (skb)
241 kfree_skb(skb);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200242 release_sock(sk);
243 return err;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200244}
245
246static int
247data_sock_release(struct socket *sock)
248{
249 struct sock *sk = sock->sk;
250
251 if (*debug & DEBUG_SOCKET)
252 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
253 if (!sk)
254 return 0;
255 switch (sk->sk_protocol) {
256 case ISDN_P_TE_S0:
257 case ISDN_P_NT_S0:
258 case ISDN_P_TE_E1:
259 case ISDN_P_NT_E1:
260 if (sk->sk_state == MISDN_BOUND)
261 delete_channel(&_pms(sk)->ch);
262 else
263 mISDN_sock_unlink(&data_sockets, sk);
264 break;
265 case ISDN_P_LAPD_TE:
266 case ISDN_P_LAPD_NT:
267 case ISDN_P_B_RAW:
268 case ISDN_P_B_HDLC:
269 case ISDN_P_B_X75SLP:
270 case ISDN_P_B_L2DTMF:
271 case ISDN_P_B_L2DSP:
272 case ISDN_P_B_L2DSPHDLC:
273 delete_channel(&_pms(sk)->ch);
274 mISDN_sock_unlink(&data_sockets, sk);
275 break;
276 }
277
278 lock_sock(sk);
279
280 sock_orphan(sk);
281 skb_queue_purge(&sk->sk_receive_queue);
282
283 release_sock(sk);
284 sock_put(sk);
285
286 return 0;
287}
288
289static int
290data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
291{
292 struct mISDN_ctrl_req cq;
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000293 int err = -EINVAL, val[2];
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200294 struct mISDNchannel *bchan, *next;
295
296 lock_sock(sk);
297 if (!_pms(sk)->dev) {
298 err = -ENODEV;
299 goto done;
300 }
301 switch (cmd) {
302 case IMCTRLREQ:
303 if (copy_from_user(&cq, p, sizeof(cq))) {
304 err = -EFAULT;
305 break;
306 }
307 if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
308 list_for_each_entry_safe(bchan, next,
Joe Perches475be4d2012-02-19 19:52:38 -0800309 &_pms(sk)->dev->bchannels, list) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200310 if (bchan->nr == cq.channel) {
311 err = bchan->ctrl(bchan,
Joe Perches475be4d2012-02-19 19:52:38 -0800312 CONTROL_CHANNEL, &cq);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200313 break;
314 }
315 }
316 } else
317 err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
Joe Perches475be4d2012-02-19 19:52:38 -0800318 CONTROL_CHANNEL, &cq);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200319 if (err)
320 break;
321 if (copy_to_user(p, &cq, sizeof(cq)))
322 err = -EFAULT;
323 break;
324 case IMCLEAR_L2:
325 if (sk->sk_protocol != ISDN_P_LAPD_NT) {
326 err = -EINVAL;
327 break;
328 }
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000329 val[0] = cmd;
330 if (get_user(val[1], (int __user *)p)) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200331 err = -EFAULT;
332 break;
333 }
334 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
Joe Perches475be4d2012-02-19 19:52:38 -0800335 CONTROL_CHANNEL, val);
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000336 break;
337 case IMHOLD_L1:
338 if (sk->sk_protocol != ISDN_P_LAPD_NT
Joe Perches475be4d2012-02-19 19:52:38 -0800339 && sk->sk_protocol != ISDN_P_LAPD_TE) {
Andreas Eversberge73f6b22009-05-22 11:04:48 +0000340 err = -EINVAL;
341 break;
342 }
343 val[0] = cmd;
344 if (get_user(val[1], (int __user *)p)) {
345 err = -EFAULT;
346 break;
347 }
348 err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
Joe Perches475be4d2012-02-19 19:52:38 -0800349 CONTROL_CHANNEL, val);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200350 break;
351 default:
352 err = -EINVAL;
353 break;
354 }
355done:
356 release_sock(sk);
357 return err;
358}
359
360static int
361data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
362{
Joe Perches475be4d2012-02-19 19:52:38 -0800363 int err = 0, id;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200364 struct sock *sk = sock->sk;
365 struct mISDNdevice *dev;
366 struct mISDNversion ver;
367
368 switch (cmd) {
369 case IMGETVERSION:
370 ver.major = MISDN_MAJOR_VERSION;
371 ver.minor = MISDN_MINOR_VERSION;
372 ver.release = MISDN_RELEASE;
373 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
374 err = -EFAULT;
375 break;
376 case IMGETCOUNT:
377 id = get_mdevice_count();
378 if (put_user(id, (int __user *)arg))
379 err = -EFAULT;
380 break;
381 case IMGETDEVINFO:
382 if (get_user(id, (int __user *)arg)) {
383 err = -EFAULT;
384 break;
385 }
386 dev = get_mdevice(id);
387 if (dev) {
388 struct mISDN_devinfo di;
389
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000390 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200391 di.id = dev->id;
392 di.Dprotocols = dev->Dprotocols;
393 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
394 di.protocol = dev->D.protocol;
395 memcpy(di.channelmap, dev->channelmap,
Joe Perches475be4d2012-02-19 19:52:38 -0800396 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200397 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200398 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200399 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
400 err = -EFAULT;
401 } else
402 err = -ENODEV;
403 break;
404 default:
405 if (sk->sk_state == MISDN_BOUND)
406 err = data_sock_ioctl_bound(sk, cmd,
Joe Perches475be4d2012-02-19 19:52:38 -0800407 (void __user *)arg);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200408 else
409 err = -ENOTCONN;
410 }
411 return err;
412}
413
414static int data_sock_setsockopt(struct socket *sock, int level, int optname,
Joe Perches475be4d2012-02-19 19:52:38 -0800415 char __user *optval, unsigned int len)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200416{
417 struct sock *sk = sock->sk;
418 int err = 0, opt = 0;
419
420 if (*debug & DEBUG_SOCKET)
421 printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
Joe Perches475be4d2012-02-19 19:52:38 -0800422 level, optname, optval, len);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200423
424 lock_sock(sk);
425
426 switch (optname) {
427 case MISDN_TIME_STAMP:
428 if (get_user(opt, (int __user *)optval)) {
429 err = -EFAULT;
430 break;
431 }
432
433 if (opt)
434 _pms(sk)->cmask |= MISDN_TIME_STAMP;
435 else
436 _pms(sk)->cmask &= ~MISDN_TIME_STAMP;
437 break;
438 default:
439 err = -ENOPROTOOPT;
440 break;
441 }
442 release_sock(sk);
443 return err;
444}
445
446static int data_sock_getsockopt(struct socket *sock, int level, int optname,
Joe Perches475be4d2012-02-19 19:52:38 -0800447 char __user *optval, int __user *optlen)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200448{
449 struct sock *sk = sock->sk;
450 int len, opt;
451
452 if (get_user(len, optlen))
453 return -EFAULT;
454
David S. Miller81b424d2011-04-17 16:48:53 -0700455 if (len != sizeof(char))
456 return -EINVAL;
457
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200458 switch (optname) {
459 case MISDN_TIME_STAMP:
460 if (_pms(sk)->cmask & MISDN_TIME_STAMP)
461 opt = 1;
462 else
463 opt = 0;
464
465 if (put_user(opt, optval))
466 return -EFAULT;
467 break;
468 default:
469 return -ENOPROTOOPT;
470 }
471
472 return 0;
473}
474
475static int
476data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
477{
478 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
479 struct sock *sk = sock->sk;
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200480 struct sock *csk;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200481 int err = 0;
482
483 if (*debug & DEBUG_SOCKET)
484 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
485 if (addr_len != sizeof(struct sockaddr_mISDN))
486 return -EINVAL;
487 if (!maddr || maddr->family != AF_ISDN)
488 return -EINVAL;
489
490 lock_sock(sk);
491
492 if (_pms(sk)->dev) {
493 err = -EALREADY;
494 goto done;
495 }
496 _pms(sk)->dev = get_mdevice(maddr->dev);
497 if (!_pms(sk)->dev) {
498 err = -ENODEV;
499 goto done;
500 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200501
Andreas Eversberg9a812552008-09-14 14:42:18 +0200502 if (sk->sk_protocol < ISDN_P_B_START) {
503 read_lock_bh(&data_sockets.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800504 sk_for_each(csk, &data_sockets.head) {
Andreas Eversberg9a812552008-09-14 14:42:18 +0200505 if (sk == csk)
506 continue;
507 if (_pms(csk)->dev != _pms(sk)->dev)
508 continue;
509 if (csk->sk_protocol >= ISDN_P_B_START)
510 continue;
511 if (IS_ISDN_P_TE(csk->sk_protocol)
Joe Perches475be4d2012-02-19 19:52:38 -0800512 == IS_ISDN_P_TE(sk->sk_protocol))
Andreas Eversberg9a812552008-09-14 14:42:18 +0200513 continue;
514 read_unlock_bh(&data_sockets.lock);
515 err = -EBUSY;
516 goto done;
517 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200518 read_unlock_bh(&data_sockets.lock);
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200519 }
Andreas Eversberg1b4d3312008-09-14 12:30:18 +0200520
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200521 _pms(sk)->ch.send = mISDN_send;
522 _pms(sk)->ch.ctrl = mISDN_ctrl;
523
524 switch (sk->sk_protocol) {
525 case ISDN_P_TE_S0:
526 case ISDN_P_NT_S0:
527 case ISDN_P_TE_E1:
528 case ISDN_P_NT_E1:
529 mISDN_sock_unlink(&data_sockets, sk);
530 err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800531 sk->sk_protocol, maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200532 if (err)
533 mISDN_sock_link(&data_sockets, sk);
534 break;
535 case ISDN_P_LAPD_TE:
536 case ISDN_P_LAPD_NT:
537 err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800538 sk->sk_protocol, maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200539 break;
540 case ISDN_P_B_RAW:
541 case ISDN_P_B_HDLC:
542 case ISDN_P_B_X75SLP:
543 case ISDN_P_B_L2DTMF:
544 case ISDN_P_B_L2DSP:
545 case ISDN_P_B_L2DSPHDLC:
546 err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
Joe Perches475be4d2012-02-19 19:52:38 -0800547 sk->sk_protocol, maddr);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200548 break;
549 default:
550 err = -EPROTONOSUPPORT;
551 }
552 if (err)
553 goto done;
554 sk->sk_state = MISDN_BOUND;
555 _pms(sk)->ch.protocol = sk->sk_protocol;
556
557done:
558 release_sock(sk);
559 return err;
560}
561
562static int
563data_sock_getname(struct socket *sock, struct sockaddr *addr,
Joe Perches475be4d2012-02-19 19:52:38 -0800564 int *addr_len, int peer)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200565{
Joe Perches475be4d2012-02-19 19:52:38 -0800566 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200567 struct sock *sk = sock->sk;
568
569 if (!_pms(sk)->dev)
570 return -EBADFD;
571
572 lock_sock(sk);
573
574 *addr_len = sizeof(*maddr);
Dan Carpenterd9152092013-04-22 20:24:14 +0000575 maddr->family = AF_ISDN;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200576 maddr->dev = _pms(sk)->dev->id;
577 maddr->channel = _pms(sk)->ch.nr;
578 maddr->sapi = _pms(sk)->ch.addr & 0xff;
579 maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
580 release_sock(sk);
581 return 0;
582}
583
584static const struct proto_ops data_sock_ops = {
585 .family = PF_ISDN,
586 .owner = THIS_MODULE,
587 .release = data_sock_release,
588 .ioctl = data_sock_ioctl,
589 .bind = data_sock_bind,
590 .getname = data_sock_getname,
591 .sendmsg = mISDN_sock_sendmsg,
592 .recvmsg = mISDN_sock_recvmsg,
593 .poll = datagram_poll,
594 .listen = sock_no_listen,
595 .shutdown = sock_no_shutdown,
596 .setsockopt = data_sock_setsockopt,
597 .getsockopt = data_sock_getsockopt,
598 .connect = sock_no_connect,
599 .socketpair = sock_no_socketpair,
600 .accept = sock_no_accept,
601 .mmap = sock_no_mmap
602};
603
604static int
605data_sock_create(struct net *net, struct socket *sock, int protocol)
606{
607 struct sock *sk;
608
609 if (sock->type != SOCK_DGRAM)
610 return -ESOCKTNOSUPPORT;
611
612 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
613 if (!sk)
614 return -ENOMEM;
615
616 sock_init_data(sock, sk);
617
618 sock->ops = &data_sock_ops;
619 sock->state = SS_UNCONNECTED;
620 sock_reset_flag(sk, SOCK_ZAPPED);
621
622 sk->sk_protocol = protocol;
623 sk->sk_state = MISDN_OPEN;
624 mISDN_sock_link(&data_sockets, sk);
625
626 return 0;
627}
628
629static int
630base_sock_release(struct socket *sock)
631{
632 struct sock *sk = sock->sk;
633
634 printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
635 if (!sk)
636 return 0;
637
638 mISDN_sock_unlink(&base_sockets, sk);
639 sock_orphan(sk);
640 sock_put(sk);
641
642 return 0;
643}
644
645static int
646base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
647{
Joe Perches475be4d2012-02-19 19:52:38 -0800648 int err = 0, id;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200649 struct mISDNdevice *dev;
650 struct mISDNversion ver;
651
652 switch (cmd) {
653 case IMGETVERSION:
654 ver.major = MISDN_MAJOR_VERSION;
655 ver.minor = MISDN_MINOR_VERSION;
656 ver.release = MISDN_RELEASE;
657 if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
658 err = -EFAULT;
659 break;
660 case IMGETCOUNT:
661 id = get_mdevice_count();
662 if (put_user(id, (int __user *)arg))
663 err = -EFAULT;
664 break;
665 case IMGETDEVINFO:
666 if (get_user(id, (int __user *)arg)) {
667 err = -EFAULT;
668 break;
669 }
670 dev = get_mdevice(id);
671 if (dev) {
672 struct mISDN_devinfo di;
673
Kulikov Vasiliyce384d92010-10-29 23:04:33 +0000674 memset(&di, 0, sizeof(di));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200675 di.id = dev->id;
676 di.Dprotocols = dev->Dprotocols;
677 di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
678 di.protocol = dev->D.protocol;
679 memcpy(di.channelmap, dev->channelmap,
Joe Perches475be4d2012-02-19 19:52:38 -0800680 sizeof(di.channelmap));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200681 di.nrbchan = dev->nrbchan;
Matthias Urlichs837468d2008-08-16 00:04:33 +0200682 strcpy(di.name, dev_name(&dev->dev));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200683 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
684 err = -EFAULT;
685 } else
686 err = -ENODEV;
687 break;
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200688 case IMSETDEVNAME:
Joe Perches475be4d2012-02-19 19:52:38 -0800689 {
690 struct mISDN_devrename dn;
691 if (copy_from_user(&dn, (void __user *)arg,
692 sizeof(dn))) {
693 err = -EFAULT;
694 break;
Matthias Urlichs8b6015f2008-08-12 10:12:09 +0200695 }
Joe Perches475be4d2012-02-19 19:52:38 -0800696 dev = get_mdevice(dn.id);
697 if (dev)
698 err = device_rename(&dev->dev, dn.name);
699 else
700 err = -ENODEV;
701 }
702 break;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200703 default:
704 err = -EINVAL;
705 }
706 return err;
707}
708
709static int
710base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
711{
712 struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
713 struct sock *sk = sock->sk;
714 int err = 0;
715
716 if (!maddr || maddr->family != AF_ISDN)
717 return -EINVAL;
718
719 lock_sock(sk);
720
721 if (_pms(sk)->dev) {
722 err = -EALREADY;
723 goto done;
724 }
725
726 _pms(sk)->dev = get_mdevice(maddr->dev);
727 if (!_pms(sk)->dev) {
728 err = -ENODEV;
729 goto done;
730 }
731 sk->sk_state = MISDN_BOUND;
732
733done:
734 release_sock(sk);
735 return err;
736}
737
738static const struct proto_ops base_sock_ops = {
739 .family = PF_ISDN,
740 .owner = THIS_MODULE,
741 .release = base_sock_release,
742 .ioctl = base_sock_ioctl,
743 .bind = base_sock_bind,
744 .getname = sock_no_getname,
745 .sendmsg = sock_no_sendmsg,
746 .recvmsg = sock_no_recvmsg,
747 .poll = sock_no_poll,
748 .listen = sock_no_listen,
749 .shutdown = sock_no_shutdown,
750 .setsockopt = sock_no_setsockopt,
751 .getsockopt = sock_no_getsockopt,
752 .connect = sock_no_connect,
753 .socketpair = sock_no_socketpair,
754 .accept = sock_no_accept,
755 .mmap = sock_no_mmap
756};
757
758
759static int
760base_sock_create(struct net *net, struct socket *sock, int protocol)
761{
762 struct sock *sk;
763
764 if (sock->type != SOCK_RAW)
765 return -ESOCKTNOSUPPORT;
766
767 sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto);
768 if (!sk)
769 return -ENOMEM;
770
771 sock_init_data(sock, sk);
772 sock->ops = &base_sock_ops;
773 sock->state = SS_UNCONNECTED;
774 sock_reset_flag(sk, SOCK_ZAPPED);
775 sk->sk_protocol = protocol;
776 sk->sk_state = MISDN_OPEN;
777 mISDN_sock_link(&base_sockets, sk);
778
779 return 0;
780}
781
782static int
Eric Paris3f378b62009-11-05 22:18:14 -0800783mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200784{
785 int err = -EPROTONOSUPPORT;
786
Joe Perches475be4d2012-02-19 19:52:38 -0800787 switch (proto) {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200788 case ISDN_P_BASE:
789 err = base_sock_create(net, sock, proto);
790 break;
791 case ISDN_P_TE_S0:
792 case ISDN_P_NT_S0:
793 case ISDN_P_TE_E1:
794 case ISDN_P_NT_E1:
795 case ISDN_P_LAPD_TE:
796 case ISDN_P_LAPD_NT:
797 case ISDN_P_B_RAW:
798 case ISDN_P_B_HDLC:
799 case ISDN_P_B_X75SLP:
800 case ISDN_P_B_L2DTMF:
801 case ISDN_P_B_L2DSP:
802 case ISDN_P_B_L2DSPHDLC:
803 err = data_sock_create(net, sock, proto);
804 break;
805 default:
806 return err;
807 }
808
809 return err;
810}
811
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000812static const struct net_proto_family mISDN_sock_family_ops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200813 .owner = THIS_MODULE,
814 .family = PF_ISDN,
815 .create = mISDN_sock_create,
816};
817
818int
819misdn_sock_init(u_int *deb)
820{
821 int err;
822
823 debug = deb;
824 err = sock_register(&mISDN_sock_family_ops);
825 if (err)
826 printk(KERN_ERR "%s: error(%d)\n", __func__, err);
827 return err;
828}
829
830void
831misdn_sock_cleanup(void)
832{
833 sock_unregister(PF_ISDN);
834}