1 /*
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>
19 #include <linux/slab.h>
20 #include <linux/export.h>
21 #include "core.h"
22 
23 static u_int	*debug;
24 
25 static 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 
33 static struct mISDN_sock_list	data_sockets = {
34 	.lock = __RW_LOCK_UNLOCKED(data_sockets.lock)
35 };
36 
37 static struct mISDN_sock_list	base_sockets = {
38 	.lock = __RW_LOCK_UNLOCKED(base_sockets.lock)
39 };
40 
41 #define L2_HEADER_LEN	4
42 
43 static inline struct sk_buff *
_l2_alloc_skb(unsigned int len,gfp_t gfp_mask)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 
54 static void
mISDN_sock_link(struct mISDN_sock_list * l,struct sock * sk)55 mISDN_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 
mISDN_sock_unlink(struct mISDN_sock_list * l,struct sock * sk)62 static 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 
69 static int
mISDN_send(struct mISDNchannel * ch,struct sk_buff * skb)70 mISDN_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 
87 static int
mISDN_ctrl(struct mISDNchannel * ch,u_int cmd,void * arg)88 mISDN_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 
103 static inline void
mISDN_sock_cmsg(struct sock * sk,struct msghdr * msg,struct sk_buff * skb)104 mISDN_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 
114 static int
mISDN_sock_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)115 mISDN_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
116 		   int flags)
117 {
118 	struct sk_buff		*skb;
119 	struct sock		*sk = sock->sk;
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_name) {
138 		DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
139 
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 		msg->msg_namelen = sizeof(*maddr);
153 	}
154 
155 	copied = skb->len + MISDN_HEADER_LEN;
156 	if (len < copied) {
157 		if (flags & MSG_PEEK)
158 			refcount_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),
164 	       MISDN_HEADER_LEN);
165 
166 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
167 
168 	mISDN_sock_cmsg(sk, msg, skb);
169 
170 	skb_free_datagram(sk, skb);
171 
172 	return err ? : copied;
173 }
174 
175 static int
mISDN_sock_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)176 mISDN_sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
177 {
178 	struct sock		*sk = sock->sk;
179 	struct sk_buff		*skb;
180 	int			err = -ENOMEM;
181 
182 	if (*debug & DEBUG_SOCKET)
183 		printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
184 		       __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
185 		       sk->sk_protocol);
186 
187 	if (msg->msg_flags & MSG_OOB)
188 		return -EOPNOTSUPP;
189 
190 	if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE))
191 		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 
205 	if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
206 		err = -EFAULT;
207 		goto done;
208 	}
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 */
215 		DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
216 		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))
220 			mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
221 	}
222 
223 	if (*debug & DEBUG_SOCKET)
224 		printk(KERN_DEBUG "%s: ID:%x\n",
225 		       __func__, mISDN_HEAD_ID(skb));
226 
227 	err = -ENODEV;
228 	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 	}
237 
238 done:
239 	if (skb)
240 		kfree_skb(skb);
241 	release_sock(sk);
242 	return err;
243 }
244 
245 static int
data_sock_release(struct socket * sock)246 data_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 
288 static int
data_sock_ioctl_bound(struct sock * sk,unsigned int cmd,void __user * p)289 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
290 {
291 	struct mISDN_ctrl_req	cq;
292 	int			err = -EINVAL, val[2];
293 	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,
308 						 &_pms(sk)->dev->bchannels, list) {
309 				if (bchan->nr == cq.channel) {
310 					err = bchan->ctrl(bchan,
311 							  CONTROL_CHANNEL, &cq);
312 					break;
313 				}
314 			}
315 		} else
316 			err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
317 						    CONTROL_CHANNEL, &cq);
318 		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 		}
328 		val[0] = cmd;
329 		if (get_user(val[1], (int __user *)p)) {
330 			err = -EFAULT;
331 			break;
332 		}
333 		err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
334 						  CONTROL_CHANNEL, val);
335 		break;
336 	case IMHOLD_L1:
337 		if (sk->sk_protocol != ISDN_P_LAPD_NT
338 		    && sk->sk_protocol != ISDN_P_LAPD_TE) {
339 			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,
348 						  CONTROL_CHANNEL, val);
349 		break;
350 	default:
351 		err = -EINVAL;
352 		break;
353 	}
354 done:
355 	release_sock(sk);
356 	return err;
357 }
358 
359 static int
data_sock_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)360 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
361 {
362 	int			err = 0, id;
363 	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 
389 			memset(&di, 0, sizeof(di));
390 			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,
395 			       sizeof(di.channelmap));
396 			di.nrbchan = dev->nrbchan;
397 			strscpy(di.name, dev_name(&dev->dev), sizeof(di.name));
398 			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,
406 						    (void __user *)arg);
407 		else
408 			err = -ENOTCONN;
409 	}
410 	return err;
411 }
412 
data_sock_setsockopt(struct socket * sock,int level,int optname,char __user * optval,unsigned int len)413 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
414 				char __user *optval, unsigned int len)
415 {
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,
421 		       level, optname, optval, len);
422 
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 
data_sock_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)445 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
446 				char __user *optval, int __user *optlen)
447 {
448 	struct sock *sk = sock->sk;
449 	int len, opt;
450 
451 	if (get_user(len, optlen))
452 		return -EFAULT;
453 
454 	if (len != sizeof(char))
455 		return -EINVAL;
456 
457 	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 
474 static int
data_sock_bind(struct socket * sock,struct sockaddr * addr,int addr_len)475 data_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;
479 	struct sock *csk;
480 	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 	}
500 
501 	if (sk->sk_protocol < ISDN_P_B_START) {
502 		read_lock_bh(&data_sockets.lock);
503 		sk_for_each(csk, &data_sockets.head) {
504 			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)
511 			    == IS_ISDN_P_TE(sk->sk_protocol))
512 				continue;
513 			read_unlock_bh(&data_sockets.lock);
514 			err = -EBUSY;
515 			goto done;
516 		}
517 		read_unlock_bh(&data_sockets.lock);
518 	}
519 
520 	_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,
530 				     sk->sk_protocol, maddr);
531 		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,
537 				      sk->sk_protocol, maddr);
538 		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,
546 				     sk->sk_protocol, maddr);
547 		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 
556 done:
557 	release_sock(sk);
558 	return err;
559 }
560 
561 static int
data_sock_getname(struct socket * sock,struct sockaddr * addr,int peer)562 data_sock_getname(struct socket *sock, struct sockaddr *addr,
563 		  int peer)
564 {
565 	struct sockaddr_mISDN	*maddr = (struct sockaddr_mISDN *) addr;
566 	struct sock		*sk = sock->sk;
567 
568 	if (!_pms(sk)->dev)
569 		return -EBADFD;
570 
571 	lock_sock(sk);
572 
573 	maddr->family = AF_ISDN;
574 	maddr->dev = _pms(sk)->dev->id;
575 	maddr->channel = _pms(sk)->ch.nr;
576 	maddr->sapi = _pms(sk)->ch.addr & 0xff;
577 	maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
578 	release_sock(sk);
579 	return sizeof(*maddr);
580 }
581 
582 static const struct proto_ops data_sock_ops = {
583 	.family		= PF_ISDN,
584 	.owner		= THIS_MODULE,
585 	.release	= data_sock_release,
586 	.ioctl		= data_sock_ioctl,
587 	.bind		= data_sock_bind,
588 	.getname	= data_sock_getname,
589 	.sendmsg	= mISDN_sock_sendmsg,
590 	.recvmsg	= mISDN_sock_recvmsg,
591 	.poll		= datagram_poll,
592 	.listen		= sock_no_listen,
593 	.shutdown	= sock_no_shutdown,
594 	.setsockopt	= data_sock_setsockopt,
595 	.getsockopt	= data_sock_getsockopt,
596 	.connect	= sock_no_connect,
597 	.socketpair	= sock_no_socketpair,
598 	.accept		= sock_no_accept,
599 	.mmap		= sock_no_mmap
600 };
601 
602 static int
data_sock_create(struct net * net,struct socket * sock,int protocol,int kern)603 data_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
604 {
605 	struct sock *sk;
606 
607 	if (sock->type != SOCK_DGRAM)
608 		return -ESOCKTNOSUPPORT;
609 
610 	sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
611 	if (!sk)
612 		return -ENOMEM;
613 
614 	sock_init_data(sock, sk);
615 
616 	sock->ops = &data_sock_ops;
617 	sock->state = SS_UNCONNECTED;
618 	sock_reset_flag(sk, SOCK_ZAPPED);
619 
620 	sk->sk_protocol = protocol;
621 	sk->sk_state    = MISDN_OPEN;
622 	mISDN_sock_link(&data_sockets, sk);
623 
624 	return 0;
625 }
626 
627 static int
base_sock_release(struct socket * sock)628 base_sock_release(struct socket *sock)
629 {
630 	struct sock *sk = sock->sk;
631 
632 	printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
633 	if (!sk)
634 		return 0;
635 
636 	mISDN_sock_unlink(&base_sockets, sk);
637 	sock_orphan(sk);
638 	sock_put(sk);
639 
640 	return 0;
641 }
642 
643 static int
base_sock_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)644 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
645 {
646 	int			err = 0, id;
647 	struct mISDNdevice	*dev;
648 	struct mISDNversion	ver;
649 
650 	switch (cmd) {
651 	case IMGETVERSION:
652 		ver.major = MISDN_MAJOR_VERSION;
653 		ver.minor = MISDN_MINOR_VERSION;
654 		ver.release = MISDN_RELEASE;
655 		if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
656 			err = -EFAULT;
657 		break;
658 	case IMGETCOUNT:
659 		id = get_mdevice_count();
660 		if (put_user(id, (int __user *)arg))
661 			err = -EFAULT;
662 		break;
663 	case IMGETDEVINFO:
664 		if (get_user(id, (int __user *)arg)) {
665 			err = -EFAULT;
666 			break;
667 		}
668 		dev = get_mdevice(id);
669 		if (dev) {
670 			struct mISDN_devinfo di;
671 
672 			memset(&di, 0, sizeof(di));
673 			di.id = dev->id;
674 			di.Dprotocols = dev->Dprotocols;
675 			di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
676 			di.protocol = dev->D.protocol;
677 			memcpy(di.channelmap, dev->channelmap,
678 			       sizeof(di.channelmap));
679 			di.nrbchan = dev->nrbchan;
680 			strscpy(di.name, dev_name(&dev->dev), sizeof(di.name));
681 			if (copy_to_user((void __user *)arg, &di, sizeof(di)))
682 				err = -EFAULT;
683 		} else
684 			err = -ENODEV;
685 		break;
686 	case IMSETDEVNAME:
687 	{
688 		struct mISDN_devrename dn;
689 		if (copy_from_user(&dn, (void __user *)arg,
690 				   sizeof(dn))) {
691 			err = -EFAULT;
692 			break;
693 		}
694 		dn.name[sizeof(dn.name) - 1] = '\0';
695 		dev = get_mdevice(dn.id);
696 		if (dev)
697 			err = device_rename(&dev->dev, dn.name);
698 		else
699 			err = -ENODEV;
700 	}
701 	break;
702 	default:
703 		err = -EINVAL;
704 	}
705 	return err;
706 }
707 
708 static int
base_sock_bind(struct socket * sock,struct sockaddr * addr,int addr_len)709 base_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 (addr_len < sizeof(struct sockaddr_mISDN))
716 		return -EINVAL;
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 
735 done:
736 	release_sock(sk);
737 	return err;
738 }
739 
740 static 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 	.listen		= sock_no_listen,
750 	.shutdown	= sock_no_shutdown,
751 	.setsockopt	= sock_no_setsockopt,
752 	.getsockopt	= sock_no_getsockopt,
753 	.connect	= sock_no_connect,
754 	.socketpair	= sock_no_socketpair,
755 	.accept		= sock_no_accept,
756 	.mmap		= sock_no_mmap
757 };
758 
759 
760 static int
base_sock_create(struct net * net,struct socket * sock,int protocol,int kern)761 base_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
762 {
763 	struct sock *sk;
764 
765 	if (sock->type != SOCK_RAW)
766 		return -ESOCKTNOSUPPORT;
767 	if (!capable(CAP_NET_RAW))
768 		return -EPERM;
769 
770 	sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
771 	if (!sk)
772 		return -ENOMEM;
773 
774 	sock_init_data(sock, sk);
775 	sock->ops = &base_sock_ops;
776 	sock->state = SS_UNCONNECTED;
777 	sock_reset_flag(sk, SOCK_ZAPPED);
778 	sk->sk_protocol = protocol;
779 	sk->sk_state    = MISDN_OPEN;
780 	mISDN_sock_link(&base_sockets, sk);
781 
782 	return 0;
783 }
784 
785 static int
mISDN_sock_create(struct net * net,struct socket * sock,int proto,int kern)786 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
787 {
788 	int err = -EPROTONOSUPPORT;
789 
790 	switch (proto) {
791 	case ISDN_P_BASE:
792 		err = base_sock_create(net, sock, proto, kern);
793 		break;
794 	case ISDN_P_TE_S0:
795 	case ISDN_P_NT_S0:
796 	case ISDN_P_TE_E1:
797 	case ISDN_P_NT_E1:
798 	case ISDN_P_LAPD_TE:
799 	case ISDN_P_LAPD_NT:
800 	case ISDN_P_B_RAW:
801 	case ISDN_P_B_HDLC:
802 	case ISDN_P_B_X75SLP:
803 	case ISDN_P_B_L2DTMF:
804 	case ISDN_P_B_L2DSP:
805 	case ISDN_P_B_L2DSPHDLC:
806 		err = data_sock_create(net, sock, proto, kern);
807 		break;
808 	default:
809 		return err;
810 	}
811 
812 	return err;
813 }
814 
815 static const struct net_proto_family mISDN_sock_family_ops = {
816 	.owner  = THIS_MODULE,
817 	.family = PF_ISDN,
818 	.create = mISDN_sock_create,
819 };
820 
821 int
misdn_sock_init(u_int * deb)822 misdn_sock_init(u_int *deb)
823 {
824 	int err;
825 
826 	debug = deb;
827 	err = sock_register(&mISDN_sock_family_ops);
828 	if (err)
829 		printk(KERN_ERR "%s: error(%d)\n", __func__, err);
830 	return err;
831 }
832 
833 void
misdn_sock_cleanup(void)834 misdn_sock_cleanup(void)
835 {
836 	sock_unregister(PF_ISDN);
837 }
838