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