xref: /linux/drivers/isdn/mISDN/socket.c (revision 80d443e8876602be2c130f79c4de81e12e2a700d)
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 			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 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
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
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
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 			strcpy(di.name, dev_name(&dev->dev));
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 
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 
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
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
562 data_sock_getname(struct socket *sock, struct sockaddr *addr,
563 		  int *addr_len, 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 	*addr_len = sizeof(*maddr);
574 	maddr->family = AF_ISDN;
575 	maddr->dev = _pms(sk)->dev->id;
576 	maddr->channel = _pms(sk)->ch.nr;
577 	maddr->sapi = _pms(sk)->ch.addr & 0xff;
578 	maddr->tei = (_pms(sk)->ch.addr >> 8) & 0xff;
579 	release_sock(sk);
580 	return 0;
581 }
582 
583 static const struct proto_ops data_sock_ops = {
584 	.family		= PF_ISDN,
585 	.owner		= THIS_MODULE,
586 	.release	= data_sock_release,
587 	.ioctl		= data_sock_ioctl,
588 	.bind		= data_sock_bind,
589 	.getname	= data_sock_getname,
590 	.sendmsg	= mISDN_sock_sendmsg,
591 	.recvmsg	= mISDN_sock_recvmsg,
592 	.poll		= datagram_poll,
593 	.listen		= sock_no_listen,
594 	.shutdown	= sock_no_shutdown,
595 	.setsockopt	= data_sock_setsockopt,
596 	.getsockopt	= data_sock_getsockopt,
597 	.connect	= sock_no_connect,
598 	.socketpair	= sock_no_socketpair,
599 	.accept		= sock_no_accept,
600 	.mmap		= sock_no_mmap
601 };
602 
603 static int
604 data_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
605 {
606 	struct sock *sk;
607 
608 	if (sock->type != SOCK_DGRAM)
609 		return -ESOCKTNOSUPPORT;
610 
611 	sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
612 	if (!sk)
613 		return -ENOMEM;
614 
615 	sock_init_data(sock, sk);
616 
617 	sock->ops = &data_sock_ops;
618 	sock->state = SS_UNCONNECTED;
619 	sock_reset_flag(sk, SOCK_ZAPPED);
620 
621 	sk->sk_protocol = protocol;
622 	sk->sk_state    = MISDN_OPEN;
623 	mISDN_sock_link(&data_sockets, sk);
624 
625 	return 0;
626 }
627 
628 static int
629 base_sock_release(struct socket *sock)
630 {
631 	struct sock *sk = sock->sk;
632 
633 	printk(KERN_DEBUG "%s(%p) sk=%p\n", __func__, sock, sk);
634 	if (!sk)
635 		return 0;
636 
637 	mISDN_sock_unlink(&base_sockets, sk);
638 	sock_orphan(sk);
639 	sock_put(sk);
640 
641 	return 0;
642 }
643 
644 static int
645 base_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
646 {
647 	int			err = 0, id;
648 	struct mISDNdevice	*dev;
649 	struct mISDNversion	ver;
650 
651 	switch (cmd) {
652 	case IMGETVERSION:
653 		ver.major = MISDN_MAJOR_VERSION;
654 		ver.minor = MISDN_MINOR_VERSION;
655 		ver.release = MISDN_RELEASE;
656 		if (copy_to_user((void __user *)arg, &ver, sizeof(ver)))
657 			err = -EFAULT;
658 		break;
659 	case IMGETCOUNT:
660 		id = get_mdevice_count();
661 		if (put_user(id, (int __user *)arg))
662 			err = -EFAULT;
663 		break;
664 	case IMGETDEVINFO:
665 		if (get_user(id, (int __user *)arg)) {
666 			err = -EFAULT;
667 			break;
668 		}
669 		dev = get_mdevice(id);
670 		if (dev) {
671 			struct mISDN_devinfo di;
672 
673 			memset(&di, 0, sizeof(di));
674 			di.id = dev->id;
675 			di.Dprotocols = dev->Dprotocols;
676 			di.Bprotocols = dev->Bprotocols | get_all_Bprotocols();
677 			di.protocol = dev->D.protocol;
678 			memcpy(di.channelmap, dev->channelmap,
679 			       sizeof(di.channelmap));
680 			di.nrbchan = dev->nrbchan;
681 			strcpy(di.name, dev_name(&dev->dev));
682 			if (copy_to_user((void __user *)arg, &di, sizeof(di)))
683 				err = -EFAULT;
684 		} else
685 			err = -ENODEV;
686 		break;
687 	case IMSETDEVNAME:
688 	{
689 		struct mISDN_devrename dn;
690 		if (copy_from_user(&dn, (void __user *)arg,
691 				   sizeof(dn))) {
692 			err = -EFAULT;
693 			break;
694 		}
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
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 (!maddr || maddr->family != AF_ISDN)
716 		return -EINVAL;
717 
718 	if (addr_len < sizeof(struct sockaddr_mISDN))
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 	.poll		= sock_no_poll,
750 	.listen		= sock_no_listen,
751 	.shutdown	= sock_no_shutdown,
752 	.setsockopt	= sock_no_setsockopt,
753 	.getsockopt	= sock_no_getsockopt,
754 	.connect	= sock_no_connect,
755 	.socketpair	= sock_no_socketpair,
756 	.accept		= sock_no_accept,
757 	.mmap		= sock_no_mmap
758 };
759 
760 
761 static int
762 base_sock_create(struct net *net, struct socket *sock, int protocol, int kern)
763 {
764 	struct sock *sk;
765 
766 	if (sock->type != SOCK_RAW)
767 		return -ESOCKTNOSUPPORT;
768 
769 	sk = sk_alloc(net, PF_ISDN, GFP_KERNEL, &mISDN_proto, kern);
770 	if (!sk)
771 		return -ENOMEM;
772 
773 	sock_init_data(sock, sk);
774 	sock->ops = &base_sock_ops;
775 	sock->state = SS_UNCONNECTED;
776 	sock_reset_flag(sk, SOCK_ZAPPED);
777 	sk->sk_protocol = protocol;
778 	sk->sk_state    = MISDN_OPEN;
779 	mISDN_sock_link(&base_sockets, sk);
780 
781 	return 0;
782 }
783 
784 static int
785 mISDN_sock_create(struct net *net, struct socket *sock, int proto, int kern)
786 {
787 	int err = -EPROTONOSUPPORT;
788 
789 	switch (proto) {
790 	case ISDN_P_BASE:
791 		err = base_sock_create(net, sock, proto, kern);
792 		break;
793 	case ISDN_P_TE_S0:
794 	case ISDN_P_NT_S0:
795 	case ISDN_P_TE_E1:
796 	case ISDN_P_NT_E1:
797 	case ISDN_P_LAPD_TE:
798 	case ISDN_P_LAPD_NT:
799 	case ISDN_P_B_RAW:
800 	case ISDN_P_B_HDLC:
801 	case ISDN_P_B_X75SLP:
802 	case ISDN_P_B_L2DTMF:
803 	case ISDN_P_B_L2DSP:
804 	case ISDN_P_B_L2DSPHDLC:
805 		err = data_sock_create(net, sock, proto, kern);
806 		break;
807 	default:
808 		return err;
809 	}
810 
811 	return err;
812 }
813 
814 static const struct net_proto_family mISDN_sock_family_ops = {
815 	.owner  = THIS_MODULE,
816 	.family = PF_ISDN,
817 	.create = mISDN_sock_create,
818 };
819 
820 int
821 misdn_sock_init(u_int *deb)
822 {
823 	int err;
824 
825 	debug = deb;
826 	err = sock_register(&mISDN_sock_family_ops);
827 	if (err)
828 		printk(KERN_ERR "%s: error(%d)\n", __func__, err);
829 	return err;
830 }
831 
832 void
833 misdn_sock_cleanup(void)
834 {
835 	sock_unregister(PF_ISDN);
836 }
837