xref: /linux/drivers/isdn/mISDN/socket.c (revision 93d90ad708b8da6efc0e487b66111aa9db7f70c7)
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 		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 			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_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 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 
183 	if (*debug & DEBUG_SOCKET)
184 		printk(KERN_DEBUG "%s: len %d flags %x ch %d proto %x\n",
185 		       __func__, (int)len, msg->msg_flags, _pms(sk)->ch.nr,
186 		       sk->sk_protocol);
187 
188 	if (msg->msg_flags & MSG_OOB)
189 		return -EOPNOTSUPP;
190 
191 	if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE))
192 		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_from_msg(skb_put(skb, len), msg, len)) {
207 		err = -EFAULT;
208 		goto done;
209 	}
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 */
216 		DECLARE_SOCKADDR(struct sockaddr_mISDN *, maddr, msg->msg_name);
217 		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))
221 			mISDN_HEAD_ID(skb) = _pms(sk)->ch.nr;
222 	}
223 
224 	if (*debug & DEBUG_SOCKET)
225 		printk(KERN_DEBUG "%s: ID:%x\n",
226 		       __func__, mISDN_HEAD_ID(skb));
227 
228 	err = -ENODEV;
229 	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 	}
238 
239 done:
240 	if (skb)
241 		kfree_skb(skb);
242 	release_sock(sk);
243 	return err;
244 }
245 
246 static int
247 data_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 
289 static int
290 data_sock_ioctl_bound(struct sock *sk, unsigned int cmd, void __user *p)
291 {
292 	struct mISDN_ctrl_req	cq;
293 	int			err = -EINVAL, val[2];
294 	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,
309 						 &_pms(sk)->dev->bchannels, list) {
310 				if (bchan->nr == cq.channel) {
311 					err = bchan->ctrl(bchan,
312 							  CONTROL_CHANNEL, &cq);
313 					break;
314 				}
315 			}
316 		} else
317 			err = _pms(sk)->dev->D.ctrl(&_pms(sk)->dev->D,
318 						    CONTROL_CHANNEL, &cq);
319 		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 		}
329 		val[0] = cmd;
330 		if (get_user(val[1], (int __user *)p)) {
331 			err = -EFAULT;
332 			break;
333 		}
334 		err = _pms(sk)->dev->teimgr->ctrl(_pms(sk)->dev->teimgr,
335 						  CONTROL_CHANNEL, val);
336 		break;
337 	case IMHOLD_L1:
338 		if (sk->sk_protocol != ISDN_P_LAPD_NT
339 		    && sk->sk_protocol != ISDN_P_LAPD_TE) {
340 			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,
349 						  CONTROL_CHANNEL, val);
350 		break;
351 	default:
352 		err = -EINVAL;
353 		break;
354 	}
355 done:
356 	release_sock(sk);
357 	return err;
358 }
359 
360 static int
361 data_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
362 {
363 	int			err = 0, id;
364 	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 
390 			memset(&di, 0, sizeof(di));
391 			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,
396 			       sizeof(di.channelmap));
397 			di.nrbchan = dev->nrbchan;
398 			strcpy(di.name, dev_name(&dev->dev));
399 			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,
407 						    (void __user *)arg);
408 		else
409 			err = -ENOTCONN;
410 	}
411 	return err;
412 }
413 
414 static int data_sock_setsockopt(struct socket *sock, int level, int optname,
415 				char __user *optval, unsigned int len)
416 {
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,
422 		       level, optname, optval, len);
423 
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 
446 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
447 				char __user *optval, int __user *optlen)
448 {
449 	struct sock *sk = sock->sk;
450 	int len, opt;
451 
452 	if (get_user(len, optlen))
453 		return -EFAULT;
454 
455 	if (len != sizeof(char))
456 		return -EINVAL;
457 
458 	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 
475 static int
476 data_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;
480 	struct sock *csk;
481 	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 	}
501 
502 	if (sk->sk_protocol < ISDN_P_B_START) {
503 		read_lock_bh(&data_sockets.lock);
504 		sk_for_each(csk, &data_sockets.head) {
505 			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)
512 			    == IS_ISDN_P_TE(sk->sk_protocol))
513 				continue;
514 			read_unlock_bh(&data_sockets.lock);
515 			err = -EBUSY;
516 			goto done;
517 		}
518 		read_unlock_bh(&data_sockets.lock);
519 	}
520 
521 	_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,
531 				     sk->sk_protocol, maddr);
532 		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,
538 				      sk->sk_protocol, maddr);
539 		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,
547 				     sk->sk_protocol, maddr);
548 		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 
557 done:
558 	release_sock(sk);
559 	return err;
560 }
561 
562 static int
563 data_sock_getname(struct socket *sock, struct sockaddr *addr,
564 		  int *addr_len, int peer)
565 {
566 	struct sockaddr_mISDN	*maddr = (struct sockaddr_mISDN *) addr;
567 	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);
575 	maddr->family = AF_ISDN;
576 	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 
584 static 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 
604 static int
605 data_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 
629 static int
630 base_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 
645 static int
646 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
647 {
648 	int			err = 0, id;
649 	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 
674 			memset(&di, 0, sizeof(di));
675 			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,
680 			       sizeof(di.channelmap));
681 			di.nrbchan = dev->nrbchan;
682 			strcpy(di.name, dev_name(&dev->dev));
683 			if (copy_to_user((void __user *)arg, &di, sizeof(di)))
684 				err = -EFAULT;
685 		} else
686 			err = -ENODEV;
687 		break;
688 	case IMSETDEVNAME:
689 	{
690 		struct mISDN_devrename dn;
691 		if (copy_from_user(&dn, (void __user *)arg,
692 				   sizeof(dn))) {
693 			err = -EFAULT;
694 			break;
695 		}
696 		dev = get_mdevice(dn.id);
697 		if (dev)
698 			err = device_rename(&dev->dev, dn.name);
699 		else
700 			err = -ENODEV;
701 	}
702 	break;
703 	default:
704 		err = -EINVAL;
705 	}
706 	return err;
707 }
708 
709 static int
710 base_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 
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 	.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 
759 static int
760 base_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 
782 static int
783 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
784 {
785 	int err = -EPROTONOSUPPORT;
786 
787 	switch (proto) {
788 	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 
812 static const struct net_proto_family mISDN_sock_family_ops = {
813 	.owner  = THIS_MODULE,
814 	.family = PF_ISDN,
815 	.create = mISDN_sock_create,
816 };
817 
818 int
819 misdn_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 
830 void
831 misdn_sock_cleanup(void)
832 {
833 	sock_unregister(PF_ISDN);
834 }
835