xref: /openbmc/linux/drivers/isdn/mISDN/socket.c (revision 2fa49589)
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 *
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
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 
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
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
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
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
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
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 	kfree_skb(skb);
240 	release_sock(sk);
241 	return err;
242 }
243 
244 static int
245 data_sock_release(struct socket *sock)
246 {
247 	struct sock *sk = sock->sk;
248 
249 	if (*debug & DEBUG_SOCKET)
250 		printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
251 	if (!sk)
252 		return 0;
253 	switch (sk->sk_protocol) {
254 	case ISDN_P_TE_S0:
255 	case ISDN_P_NT_S0:
256 	case ISDN_P_TE_E1:
257 	case ISDN_P_NT_E1:
258 		if (sk->sk_state == MISDN_BOUND)
259 			delete_channel(&_pms(sk)->ch);
260 		else
261 			mISDN_sock_unlink(&data_sockets, sk);
262 		break;
263 	case ISDN_P_LAPD_TE:
264 	case ISDN_P_LAPD_NT:
265 	case ISDN_P_B_RAW:
266 	case ISDN_P_B_HDLC:
267 	case ISDN_P_B_X75SLP:
268 	case ISDN_P_B_L2DTMF:
269 	case ISDN_P_B_L2DSP:
270 	case ISDN_P_B_L2DSPHDLC:
271 		delete_channel(&_pms(sk)->ch);
272 		mISDN_sock_unlink(&data_sockets, sk);
273 		break;
274 	}
275 
276 	lock_sock(sk);
277 
278 	sock_orphan(sk);
279 	skb_queue_purge(&sk->sk_receive_queue);
280 
281 	release_sock(sk);
282 	sock_put(sk);
283 
284 	return 0;
285 }
286 
287 static int
288 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
289 {
290 	struct mISDN_ctrl_req	cq;
291 	int			err = -EINVAL, val[2];
292 	struct mISDNchannel	*bchan, *next;
293 
294 	lock_sock(sk);
295 	if (!_pms(sk)->dev) {
296 		err = -ENODEV;
297 		goto done;
298 	}
299 	switch (cmd) {
300 	case IMCTRLREQ:
301 		if (copy_from_user(&cq, p, sizeof(cq))) {
302 			err = -EFAULT;
303 			break;
304 		}
305 		if ((sk->sk_protocol & ~ISDN_P_B_MASK) == ISDN_P_B_START) {
306 			list_for_each_entry_safe(bchan, next,
307 						 &_pms(sk)->dev->bchannels, list) {
308 				if (bchan->nr == cq.channel) {
309 					err = bchan->ctrl(bchan,
310 							  CONTROL_CHANNEL, &cq);
311 					break;
312 				}
313 			}
314 		} else
315 			err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
316 						    CONTROL_CHANNEL, &cq);
317 		if (err)
318 			break;
319 		if (copy_to_user(p, &cq, sizeof(cq)))
320 			err = -EFAULT;
321 		break;
322 	case IMCLEAR_L2:
323 		if (sk->sk_protocol != ISDN_P_LAPD_NT) {
324 			err = -EINVAL;
325 			break;
326 		}
327 		val[0] = cmd;
328 		if (get_user(val[1], (int __user *)p)) {
329 			err = -EFAULT;
330 			break;
331 		}
332 		err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
333 						  CONTROL_CHANNEL, val);
334 		break;
335 	case IMHOLD_L1:
336 		if (sk->sk_protocol != ISDN_P_LAPD_NT
337 		    && sk->sk_protocol != ISDN_P_LAPD_TE) {
338 			err = -EINVAL;
339 			break;
340 		}
341 		val[0] = cmd;
342 		if (get_user(val[1], (int __user *)p)) {
343 			err = -EFAULT;
344 			break;
345 		}
346 		err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
347 						  CONTROL_CHANNEL, val);
348 		break;
349 	default:
350 		err = -EINVAL;
351 		break;
352 	}
353 done:
354 	release_sock(sk);
355 	return err;
356 }
357 
358 static int
359 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
360 {
361 	int			err = 0, id;
362 	struct sock		*sk = sock->sk;
363 	struct mISDNdevice	*dev;
364 	struct mISDNversion	ver;
365 
366 	switch (cmd) {
367 	case IMGETVERSION:
368 		ver.major = MISDN_MAJOR_VERSION;
369 		ver.minor = MISDN_MINOR_VERSION;
370 		ver.release = MISDN_RELEASE;
371 		if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
372 			err = -EFAULT;
373 		break;
374 	case IMGETCOUNT:
375 		id = get_mdevice_count();
376 		if (put_user(id, (int __user *)arg))
377 			err = -EFAULT;
378 		break;
379 	case IMGETDEVINFO:
380 		if (get_user(id, (int __user *)arg)) {
381 			err = -EFAULT;
382 			break;
383 		}
384 		dev = get_mdevice(id);
385 		if (dev) {
386 			struct mISDN_devinfo di;
387 
388 			memset(&di, 0, sizeof(di));
389 			di.id = dev->id;
390 			di.Dprotocols = dev->Dprotocols;
391 			di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
392 			di.protocol = dev->D.protocol;
393 			memcpy(di.channelmap, dev->channelmap,
394 			       sizeof(di.channelmap));
395 			di.nrbchan = dev->nrbchan;
396 			strcpy(di.name, dev_name(&dev->dev));
397 			if (copy_to_user((void __user *)arg, &di, sizeof(di)))
398 				err = -EFAULT;
399 		} else
400 			err = -ENODEV;
401 		break;
402 	default:
403 		if (sk->sk_state == MISDN_BOUND)
404 			err = data_sock_ioctl_bound(sk, cmd,
405 						    (void __user *)arg);
406 		else
407 			err = -ENOTCONN;
408 	}
409 	return err;
410 }
411 
412 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
413 				char __user *optval, unsigned int len)
414 {
415 	struct sock *sk = sock->sk;
416 	int err = 0, opt = 0;
417 
418 	if (*debug & DEBUG_SOCKET)
419 		printk(KERN_DEBUG "%s(%p, %d, %x, %p, %d)\n", __func__, sock,
420 		       level, optname, optval, len);
421 
422 	lock_sock(sk);
423 
424 	switch (optname) {
425 	case MISDN_TIME_STAMP:
426 		if (get_user(opt, (int __user *)optval)) {
427 			err = -EFAULT;
428 			break;
429 		}
430 
431 		if (opt)
432 			_pms(sk)->cmask |= MISDN_TIME_STAMP;
433 		else
434 			_pms(sk)->cmask &= ~MISDN_TIME_STAMP;
435 		break;
436 	default:
437 		err = -ENOPROTOOPT;
438 		break;
439 	}
440 	release_sock(sk);
441 	return err;
442 }
443 
444 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
445 				char __user *optval, int __user *optlen)
446 {
447 	struct sock *sk = sock->sk;
448 	int len, opt;
449 
450 	if (get_user(len, optlen))
451 		return -EFAULT;
452 
453 	if (len != sizeof(char))
454 		return -EINVAL;
455 
456 	switch (optname) {
457 	case MISDN_TIME_STAMP:
458 		if (_pms(sk)->cmask & MISDN_TIME_STAMP)
459 			opt = 1;
460 		else
461 			opt = 0;
462 
463 		if (put_user(opt, optval))
464 			return -EFAULT;
465 		break;
466 	default:
467 		return -ENOPROTOOPT;
468 	}
469 
470 	return 0;
471 }
472 
473 static int
474 data_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
475 {
476 	struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
477 	struct sock *sk = sock->sk;
478 	struct sock *csk;
479 	int err = 0;
480 
481 	if (*debug & DEBUG_SOCKET)
482 		printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
483 	if (addr_len != sizeof(struct sockaddr_mISDN))
484 		return -EINVAL;
485 	if (!maddr || maddr->family != AF_ISDN)
486 		return -EINVAL;
487 
488 	lock_sock(sk);
489 
490 	if (_pms(sk)->dev) {
491 		err = -EALREADY;
492 		goto done;
493 	}
494 	_pms(sk)->dev = get_mdevice(maddr->dev);
495 	if (!_pms(sk)->dev) {
496 		err = -ENODEV;
497 		goto done;
498 	}
499 
500 	if (sk->sk_protocol < ISDN_P_B_START) {
501 		read_lock_bh(&data_sockets.lock);
502 		sk_for_each(csk, &data_sockets.head) {
503 			if (sk == csk)
504 				continue;
505 			if (_pms(csk)->dev != _pms(sk)->dev)
506 				continue;
507 			if (csk->sk_protocol >= ISDN_P_B_START)
508 				continue;
509 			if (IS_ISDN_P_TE(csk->sk_protocol)
510 			    == IS_ISDN_P_TE(sk->sk_protocol))
511 				continue;
512 			read_unlock_bh(&data_sockets.lock);
513 			err = -EBUSY;
514 			goto done;
515 		}
516 		read_unlock_bh(&data_sockets.lock);
517 	}
518 
519 	_pms(sk)->ch.send = mISDN_send;
520 	_pms(sk)->ch.ctrl = mISDN_ctrl;
521 
522 	switch (sk->sk_protocol) {
523 	case ISDN_P_TE_S0:
524 	case ISDN_P_NT_S0:
525 	case ISDN_P_TE_E1:
526 	case ISDN_P_NT_E1:
527 		mISDN_sock_unlink(&data_sockets, sk);
528 		err = connect_layer1(_pms(sk)->dev, &_pms(sk)->ch,
529 				     sk->sk_protocol, maddr);
530 		if (err)
531 			mISDN_sock_link(&data_sockets, sk);
532 		break;
533 	case ISDN_P_LAPD_TE:
534 	case ISDN_P_LAPD_NT:
535 		err = create_l2entity(_pms(sk)->dev, &_pms(sk)->ch,
536 				      sk->sk_protocol, maddr);
537 		break;
538 	case ISDN_P_B_RAW:
539 	case ISDN_P_B_HDLC:
540 	case ISDN_P_B_X75SLP:
541 	case ISDN_P_B_L2DTMF:
542 	case ISDN_P_B_L2DSP:
543 	case ISDN_P_B_L2DSPHDLC:
544 		err = connect_Bstack(_pms(sk)->dev, &_pms(sk)->ch,
545 				     sk->sk_protocol, maddr);
546 		break;
547 	default:
548 		err = -EPROTONOSUPPORT;
549 	}
550 	if (err)
551 		goto done;
552 	sk->sk_state = MISDN_BOUND;
553 	_pms(sk)->ch.protocol = sk->sk_protocol;
554 
555 done:
556 	release_sock(sk);
557 	return err;
558 }
559 
560 static int
561 data_sock_getname(struct socket *sock, struct sockaddr *addr,
562 		  int peer)
563 {
564 	struct sockaddr_mISDN	*maddr = (struct sockaddr_mISDN *) addr;
565 	struct sock		*sk = sock->sk;
566 
567 	if (!_pms(sk)->dev)
568 		return -EBADFD;
569 
570 	lock_sock(sk);
571 
572 	maddr->family = AF_ISDN;
573 	maddr->dev = _pms(sk)->dev->id;
574 	maddr->channel = _pms(sk)->ch.nr;
575 	maddr->sapi = _pms(sk)->ch.addr & 0xff;
576 	maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
577 	release_sock(sk);
578 	return sizeof(*maddr);
579 }
580 
581 static const struct proto_ops data_sock_ops = {
582 	.family		= PF_ISDN,
583 	.owner		= THIS_MODULE,
584 	.release	= data_sock_release,
585 	.ioctl		= data_sock_ioctl,
586 	.bind		= data_sock_bind,
587 	.getname	= data_sock_getname,
588 	.sendmsg	= mISDN_sock_sendmsg,
589 	.recvmsg	= mISDN_sock_recvmsg,
590 	.poll		= datagram_poll,
591 	.listen		= sock_no_listen,
592 	.shutdown	= sock_no_shutdown,
593 	.setsockopt	= data_sock_setsockopt,
594 	.getsockopt	= data_sock_getsockopt,
595 	.connect	= sock_no_connect,
596 	.socketpair	= sock_no_socketpair,
597 	.accept		= sock_no_accept,
598 	.mmap		= sock_no_mmap
599 };
600 
601 static int
602 data_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
603 {
604 	struct sock *sk;
605 
606 	if (sock->type != SOCK_DGRAM)
607 		return -ESOCKTNOSUPPORT;
608 
609 	sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
610 	if (!sk)
611 		return -ENOMEM;
612 
613 	sock_init_data(sock, sk);
614 
615 	sock->ops = &data_sock_ops;
616 	sock->state = SS_UNCONNECTED;
617 	sock_reset_flag(sk, SOCK_ZAPPED);
618 
619 	sk->sk_protocol = protocol;
620 	sk->sk_state    = MISDN_OPEN;
621 	mISDN_sock_link(&data_sockets, sk);
622 
623 	return 0;
624 }
625 
626 static int
627 base_sock_release(struct socket *sock)
628 {
629 	struct sock *sk = sock->sk;
630 
631 	printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
632 	if (!sk)
633 		return 0;
634 
635 	mISDN_sock_unlink(&base_sockets, sk);
636 	sock_orphan(sk);
637 	sock_put(sk);
638 
639 	return 0;
640 }
641 
642 static int
643 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
644 {
645 	int			err = 0, id;
646 	struct mISDNdevice	*dev;
647 	struct mISDNversion	ver;
648 
649 	switch (cmd) {
650 	case IMGETVERSION:
651 		ver.major = MISDN_MAJOR_VERSION;
652 		ver.minor = MISDN_MINOR_VERSION;
653 		ver.release = MISDN_RELEASE;
654 		if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
655 			err = -EFAULT;
656 		break;
657 	case IMGETCOUNT:
658 		id = get_mdevice_count();
659 		if (put_user(id, (int __user *)arg))
660 			err = -EFAULT;
661 		break;
662 	case IMGETDEVINFO:
663 		if (get_user(id, (int __user *)arg)) {
664 			err = -EFAULT;
665 			break;
666 		}
667 		dev = get_mdevice(id);
668 		if (dev) {
669 			struct mISDN_devinfo di;
670 
671 			memset(&di, 0, sizeof(di));
672 			di.id = dev->id;
673 			di.Dprotocols = dev->Dprotocols;
674 			di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
675 			di.protocol = dev->D.protocol;
676 			memcpy(di.channelmap, dev->channelmap,
677 			       sizeof(di.channelmap));
678 			di.nrbchan = dev->nrbchan;
679 			strcpy(di.name, dev_name(&dev->dev));
680 			if (copy_to_user((void __user *)arg, &di, sizeof(di)))
681 				err = -EFAULT;
682 		} else
683 			err = -ENODEV;
684 		break;
685 	case IMSETDEVNAME:
686 	{
687 		struct mISDN_devrename dn;
688 		if (copy_from_user(&dn, (void __user *)arg,
689 				   sizeof(dn))) {
690 			err = -EFAULT;
691 			break;
692 		}
693 		dev = get_mdevice(dn.id);
694 		if (dev)
695 			err = device_rename(&dev->dev, dn.name);
696 		else
697 			err = -ENODEV;
698 	}
699 	break;
700 	default:
701 		err = -EINVAL;
702 	}
703 	return err;
704 }
705 
706 static int
707 base_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
708 {
709 	struct sockaddr_mISDN *maddr = (struct sockaddr_mISDN *) addr;
710 	struct sock *sk = sock->sk;
711 	int err = 0;
712 
713 	if (!maddr || maddr->family != AF_ISDN)
714 		return -EINVAL;
715 
716 	if (addr_len < sizeof(struct sockaddr_mISDN))
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 
733 done:
734 	release_sock(sk);
735 	return err;
736 }
737 
738 static 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 	.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 
758 static int
759 base_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
760 {
761 	struct sock *sk;
762 
763 	if (sock->type != SOCK_RAW)
764 		return -ESOCKTNOSUPPORT;
765 
766 	sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
767 	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 
781 static int
782 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
783 {
784 	int err = -EPROTONOSUPPORT;
785 
786 	switch (proto) {
787 	case ISDN_P_BASE:
788 		err = base_sock_create(net, sock, proto, kern);
789 		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:
802 		err = data_sock_create(net, sock, proto, kern);
803 		break;
804 	default:
805 		return err;
806 	}
807 
808 	return err;
809 }
810 
811 static const struct net_proto_family mISDN_sock_family_ops = {
812 	.owner  = THIS_MODULE,
813 	.family = PF_ISDN,
814 	.create = mISDN_sock_create,
815 };
816 
817 int
818 misdn_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 
829 void
830 misdn_sock_cleanup(void)
831 {
832 	sock_unregister(PF_ISDN);
833 }
834