• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4 
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.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    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24 
25 /* Bluetooth address family and sockets. */
26 
27 #include <linux/module.h>
28 #include <asm/ioctls.h>
29 
30 #include <net/bluetooth/bluetooth.h>
31 #include <linux/proc_fs.h>
32 
33 #ifndef CONFIG_BT_SOCK_DEBUG
34 #undef  BT_DBG
35 #define BT_DBG(D...)
36 #endif
37 
38 #define VERSION "2.16"
39 
40 /* Bluetooth sockets */
41 #define BT_MAX_PROTO	8
42 static const struct net_proto_family *bt_proto[BT_MAX_PROTO];
43 static DEFINE_RWLOCK(bt_proto_lock);
44 
45 static struct lock_class_key bt_lock_key[BT_MAX_PROTO];
46 static const char *const bt_key_strings[BT_MAX_PROTO] = {
47 	"sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP",
48 	"sk_lock-AF_BLUETOOTH-BTPROTO_HCI",
49 	"sk_lock-AF_BLUETOOTH-BTPROTO_SCO",
50 	"sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM",
51 	"sk_lock-AF_BLUETOOTH-BTPROTO_BNEP",
52 	"sk_lock-AF_BLUETOOTH-BTPROTO_CMTP",
53 	"sk_lock-AF_BLUETOOTH-BTPROTO_HIDP",
54 	"sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP",
55 };
56 
57 static struct lock_class_key bt_slock_key[BT_MAX_PROTO];
58 static const char *const bt_slock_key_strings[BT_MAX_PROTO] = {
59 	"slock-AF_BLUETOOTH-BTPROTO_L2CAP",
60 	"slock-AF_BLUETOOTH-BTPROTO_HCI",
61 	"slock-AF_BLUETOOTH-BTPROTO_SCO",
62 	"slock-AF_BLUETOOTH-BTPROTO_RFCOMM",
63 	"slock-AF_BLUETOOTH-BTPROTO_BNEP",
64 	"slock-AF_BLUETOOTH-BTPROTO_CMTP",
65 	"slock-AF_BLUETOOTH-BTPROTO_HIDP",
66 	"slock-AF_BLUETOOTH-BTPROTO_AVDTP",
67 };
68 
bt_sock_reclassify_lock(struct sock * sk,int proto)69 void bt_sock_reclassify_lock(struct sock *sk, int proto)
70 {
71 	BUG_ON(!sk);
72 	BUG_ON(sock_owned_by_user(sk));
73 
74 	sock_lock_init_class_and_name(sk,
75 			bt_slock_key_strings[proto], &bt_slock_key[proto],
76 				bt_key_strings[proto], &bt_lock_key[proto]);
77 }
78 EXPORT_SYMBOL(bt_sock_reclassify_lock);
79 
bt_sock_register(int proto,const struct net_proto_family * ops)80 int bt_sock_register(int proto, const struct net_proto_family *ops)
81 {
82 	int err = 0;
83 
84 	if (proto < 0 || proto >= BT_MAX_PROTO)
85 		return -EINVAL;
86 
87 	write_lock(&bt_proto_lock);
88 
89 	if (bt_proto[proto])
90 		err = -EEXIST;
91 	else
92 		bt_proto[proto] = ops;
93 
94 	write_unlock(&bt_proto_lock);
95 
96 	return err;
97 }
98 EXPORT_SYMBOL(bt_sock_register);
99 
bt_sock_unregister(int proto)100 void bt_sock_unregister(int proto)
101 {
102 	if (proto < 0 || proto >= BT_MAX_PROTO)
103 		return;
104 
105 	write_lock(&bt_proto_lock);
106 	bt_proto[proto] = NULL;
107 	write_unlock(&bt_proto_lock);
108 }
109 EXPORT_SYMBOL(bt_sock_unregister);
110 
111 #ifdef CONFIG_PARANOID_NETWORK
current_has_bt_admin(void)112 static inline int current_has_bt_admin(void)
113 {
114 	return !current_euid();
115 }
116 
current_has_bt(void)117 static inline int current_has_bt(void)
118 {
119 	return current_has_bt_admin();
120 }
121 # else
current_has_bt_admin(void)122 static inline int current_has_bt_admin(void)
123 {
124 	return 1;
125 }
126 
current_has_bt(void)127 static inline int current_has_bt(void)
128 {
129 	return 1;
130 }
131 #endif
132 
bt_sock_create(struct net * net,struct socket * sock,int proto,int kern)133 static int bt_sock_create(struct net *net, struct socket *sock, int proto,
134 			  int kern)
135 {
136 	int err;
137 
138 	if (proto == BTPROTO_RFCOMM || proto == BTPROTO_SCO ||
139 			proto == BTPROTO_L2CAP) {
140 		if (!current_has_bt())
141 			return -EPERM;
142 	} else if (!current_has_bt_admin())
143 		return -EPERM;
144 
145 	if (net != &init_net)
146 		return -EAFNOSUPPORT;
147 
148 	if (proto < 0 || proto >= BT_MAX_PROTO)
149 		return -EINVAL;
150 
151 	if (!bt_proto[proto])
152 		request_module("bt-proto-%d", proto);
153 
154 	err = -EPROTONOSUPPORT;
155 
156 	read_lock(&bt_proto_lock);
157 
158 	if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
159 		err = bt_proto[proto]->create(net, sock, proto, kern);
160 		if (!err)
161 			bt_sock_reclassify_lock(sock->sk, proto);
162 		module_put(bt_proto[proto]->owner);
163 	}
164 
165 	read_unlock(&bt_proto_lock);
166 
167 	return err;
168 }
169 
bt_sock_link(struct bt_sock_list * l,struct sock * sk)170 void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
171 {
172 	write_lock(&l->lock);
173 	sk_add_node(sk, &l->head);
174 	write_unlock(&l->lock);
175 }
176 EXPORT_SYMBOL(bt_sock_link);
177 
bt_sock_unlink(struct bt_sock_list * l,struct sock * sk)178 void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
179 {
180 	write_lock(&l->lock);
181 	sk_del_node_init(sk);
182 	write_unlock(&l->lock);
183 }
184 EXPORT_SYMBOL(bt_sock_unlink);
185 
bt_accept_enqueue(struct sock * parent,struct sock * sk)186 void bt_accept_enqueue(struct sock *parent, struct sock *sk)
187 {
188 	BT_DBG("parent %p, sk %p", parent, sk);
189 
190 	sock_hold(sk);
191 	list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
192 	bt_sk(sk)->parent = parent;
193 	parent->sk_ack_backlog++;
194 }
195 EXPORT_SYMBOL(bt_accept_enqueue);
196 
bt_accept_unlink(struct sock * sk)197 void bt_accept_unlink(struct sock *sk)
198 {
199 	BT_DBG("sk %p state %d", sk, sk->sk_state);
200 
201 	list_del_init(&bt_sk(sk)->accept_q);
202 	bt_sk(sk)->parent->sk_ack_backlog--;
203 	bt_sk(sk)->parent = NULL;
204 	sock_put(sk);
205 }
206 EXPORT_SYMBOL(bt_accept_unlink);
207 
bt_accept_dequeue(struct sock * parent,struct socket * newsock)208 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
209 {
210 	struct list_head *p, *n;
211 	struct sock *sk;
212 
213 	BT_DBG("parent %p", parent);
214 
215 	list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
216 		sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
217 
218 		lock_sock(sk);
219 
220 		/* FIXME: Is this check still needed */
221 		if (sk->sk_state == BT_CLOSED) {
222 			release_sock(sk);
223 			bt_accept_unlink(sk);
224 			continue;
225 		}
226 
227 		if (sk->sk_state == BT_CONNECTED || !newsock ||
228 		    test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) {
229 			bt_accept_unlink(sk);
230 			if (newsock)
231 				sock_graft(sk, newsock);
232 
233 			release_sock(sk);
234 			return sk;
235 		}
236 
237 		release_sock(sk);
238 	}
239 
240 	return NULL;
241 }
242 EXPORT_SYMBOL(bt_accept_dequeue);
243 
bt_sock_recvmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t len,int flags)244 int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
245 				struct msghdr *msg, size_t len, int flags)
246 {
247 	int noblock = flags & MSG_DONTWAIT;
248 	struct sock *sk = sock->sk;
249 	struct sk_buff *skb;
250 	size_t copied;
251 	int err;
252 
253 	BT_DBG("sock %p sk %p len %zu", sock, sk, len);
254 
255 	if (flags & (MSG_OOB))
256 		return -EOPNOTSUPP;
257 
258 	msg->msg_namelen = 0;
259 
260 	skb = skb_recv_datagram(sk, flags, noblock, &err);
261 	if (!skb) {
262 		if (sk->sk_shutdown & RCV_SHUTDOWN)
263 			return 0;
264 		return err;
265 	}
266 
267 	copied = skb->len;
268 	if (len < copied) {
269 		msg->msg_flags |= MSG_TRUNC;
270 		copied = len;
271 	}
272 
273 	skb_reset_transport_header(skb);
274 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
275 	if (err == 0)
276 		sock_recv_ts_and_drops(msg, sk, skb);
277 
278 	skb_free_datagram(sk, skb);
279 
280 	return err ? : copied;
281 }
282 EXPORT_SYMBOL(bt_sock_recvmsg);
283 
bt_sock_data_wait(struct sock * sk,long timeo)284 static long bt_sock_data_wait(struct sock *sk, long timeo)
285 {
286 	DECLARE_WAITQUEUE(wait, current);
287 
288 	add_wait_queue(sk_sleep(sk), &wait);
289 	for (;;) {
290 		set_current_state(TASK_INTERRUPTIBLE);
291 
292 		if (!skb_queue_empty(&sk->sk_receive_queue))
293 			break;
294 
295 		if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
296 			break;
297 
298 		if (signal_pending(current) || !timeo)
299 			break;
300 
301 		set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
302 		release_sock(sk);
303 		timeo = schedule_timeout(timeo);
304 		lock_sock(sk);
305 		clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
306 	}
307 
308 	__set_current_state(TASK_RUNNING);
309 	remove_wait_queue(sk_sleep(sk), &wait);
310 	return timeo;
311 }
312 
bt_sock_stream_recvmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t size,int flags)313 int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
314 			       struct msghdr *msg, size_t size, int flags)
315 {
316 	struct sock *sk = sock->sk;
317 	int err = 0;
318 	size_t target, copied = 0;
319 	long timeo;
320 
321 	if (flags & MSG_OOB)
322 		return -EOPNOTSUPP;
323 
324 	msg->msg_namelen = 0;
325 
326 	BT_DBG("sk %p size %zu", sk, size);
327 
328 	lock_sock(sk);
329 
330 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
331 	timeo  = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
332 
333 	do {
334 		struct sk_buff *skb;
335 		int chunk;
336 
337 		skb = skb_dequeue(&sk->sk_receive_queue);
338 		if (!skb) {
339 			if (copied >= target)
340 				break;
341 
342 			err = sock_error(sk);
343 			if (err)
344 				break;
345 			if (sk->sk_shutdown & RCV_SHUTDOWN)
346 				break;
347 
348 			err = -EAGAIN;
349 			if (!timeo)
350 				break;
351 
352 			timeo = bt_sock_data_wait(sk, timeo);
353 
354 			if (signal_pending(current)) {
355 				err = sock_intr_errno(timeo);
356 				goto out;
357 			}
358 			continue;
359 		}
360 
361 		chunk = min_t(unsigned int, skb->len, size);
362 		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, chunk)) {
363 			skb_queue_head(&sk->sk_receive_queue, skb);
364 			if (!copied)
365 				copied = -EFAULT;
366 			break;
367 		}
368 		copied += chunk;
369 		size   -= chunk;
370 
371 		sock_recv_ts_and_drops(msg, sk, skb);
372 
373 		if (!(flags & MSG_PEEK)) {
374 			int skb_len = skb_headlen(skb);
375 
376 			if (chunk <= skb_len) {
377 				__skb_pull(skb, chunk);
378 			} else {
379 				struct sk_buff *frag;
380 
381 				__skb_pull(skb, skb_len);
382 				chunk -= skb_len;
383 
384 				skb_walk_frags(skb, frag) {
385 					if (chunk <= frag->len) {
386 						/* Pulling partial data */
387 						skb->len -= chunk;
388 						skb->data_len -= chunk;
389 						__skb_pull(frag, chunk);
390 						break;
391 					} else if (frag->len) {
392 						/* Pulling all frag data */
393 						chunk -= frag->len;
394 						skb->len -= frag->len;
395 						skb->data_len -= frag->len;
396 						__skb_pull(frag, frag->len);
397 					}
398 				}
399 			}
400 
401 			if (skb->len) {
402 				skb_queue_head(&sk->sk_receive_queue, skb);
403 				break;
404 			}
405 			kfree_skb(skb);
406 
407 		} else {
408 			/* put message back and return */
409 			skb_queue_head(&sk->sk_receive_queue, skb);
410 			break;
411 		}
412 	} while (size);
413 
414 out:
415 	release_sock(sk);
416 	return copied ? : err;
417 }
418 EXPORT_SYMBOL(bt_sock_stream_recvmsg);
419 
bt_accept_poll(struct sock * parent)420 static inline unsigned int bt_accept_poll(struct sock *parent)
421 {
422 	struct list_head *p, *n;
423 	struct sock *sk;
424 
425 	list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
426 		sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
427 		if (sk->sk_state == BT_CONNECTED ||
428 		    (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) &&
429 		     sk->sk_state == BT_CONNECT2))
430 			return POLLIN | POLLRDNORM;
431 	}
432 
433 	return 0;
434 }
435 
bt_sock_poll(struct file * file,struct socket * sock,poll_table * wait)436 unsigned int bt_sock_poll(struct file *file, struct socket *sock,
437 			  poll_table *wait)
438 {
439 	struct sock *sk = sock->sk;
440 	unsigned int mask = 0;
441 
442 	BT_DBG("sock %p, sk %p", sock, sk);
443 
444 	poll_wait(file, sk_sleep(sk), wait);
445 
446 	if (sk->sk_state == BT_LISTEN)
447 		return bt_accept_poll(sk);
448 
449 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
450 		mask |= POLLERR |
451 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
452 
453 	if (sk->sk_shutdown & RCV_SHUTDOWN)
454 		mask |= POLLRDHUP | POLLIN | POLLRDNORM;
455 
456 	if (sk->sk_shutdown == SHUTDOWN_MASK)
457 		mask |= POLLHUP;
458 
459 	if (!skb_queue_empty(&sk->sk_receive_queue))
460 		mask |= POLLIN | POLLRDNORM;
461 
462 	if (sk->sk_state == BT_CLOSED)
463 		mask |= POLLHUP;
464 
465 	if (sk->sk_state == BT_CONNECT ||
466 			sk->sk_state == BT_CONNECT2 ||
467 			sk->sk_state == BT_CONFIG)
468 		return mask;
469 
470 	if (!test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags) && sock_writeable(sk))
471 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
472 	else
473 		set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
474 
475 	return mask;
476 }
477 EXPORT_SYMBOL(bt_sock_poll);
478 
bt_sock_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)479 int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
480 {
481 	struct sock *sk = sock->sk;
482 	struct sk_buff *skb;
483 	long amount;
484 	int err;
485 
486 	BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
487 
488 	switch (cmd) {
489 	case TIOCOUTQ:
490 		if (sk->sk_state == BT_LISTEN)
491 			return -EINVAL;
492 
493 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
494 		if (amount < 0)
495 			amount = 0;
496 		err = put_user(amount, (int __user *) arg);
497 		break;
498 
499 	case TIOCINQ:
500 		if (sk->sk_state == BT_LISTEN)
501 			return -EINVAL;
502 
503 		lock_sock(sk);
504 		skb = skb_peek(&sk->sk_receive_queue);
505 		amount = skb ? skb->len : 0;
506 		release_sock(sk);
507 		err = put_user(amount, (int __user *) arg);
508 		break;
509 
510 	case SIOCGSTAMP:
511 		err = sock_get_timestamp(sk, (struct timeval __user *) arg);
512 		break;
513 
514 	case SIOCGSTAMPNS:
515 		err = sock_get_timestampns(sk, (struct timespec __user *) arg);
516 		break;
517 
518 	default:
519 		err = -ENOIOCTLCMD;
520 		break;
521 	}
522 
523 	return err;
524 }
525 EXPORT_SYMBOL(bt_sock_ioctl);
526 
bt_sock_wait_state(struct sock * sk,int state,unsigned long timeo)527 int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
528 {
529 	DECLARE_WAITQUEUE(wait, current);
530 	int err = 0;
531 
532 	BT_DBG("sk %p", sk);
533 
534 	add_wait_queue(sk_sleep(sk), &wait);
535 	set_current_state(TASK_INTERRUPTIBLE);
536 	while (sk->sk_state != state) {
537 		if (!timeo) {
538 			err = -EINPROGRESS;
539 			break;
540 		}
541 
542 		if (signal_pending(current)) {
543 			err = sock_intr_errno(timeo);
544 			break;
545 		}
546 
547 		release_sock(sk);
548 		timeo = schedule_timeout(timeo);
549 		lock_sock(sk);
550 		set_current_state(TASK_INTERRUPTIBLE);
551 
552 		err = sock_error(sk);
553 		if (err)
554 			break;
555 	}
556 	__set_current_state(TASK_RUNNING);
557 	remove_wait_queue(sk_sleep(sk), &wait);
558 	return err;
559 }
560 EXPORT_SYMBOL(bt_sock_wait_state);
561 
562 #ifdef CONFIG_PROC_FS
563 struct bt_seq_state {
564 	struct bt_sock_list *l;
565 };
566 
bt_seq_start(struct seq_file * seq,loff_t * pos)567 static void *bt_seq_start(struct seq_file *seq, loff_t *pos)
568 	__acquires(seq->private->l->lock)
569 {
570 	struct bt_seq_state *s = seq->private;
571 	struct bt_sock_list *l = s->l;
572 
573 	read_lock(&l->lock);
574 	return seq_hlist_start_head(&l->head, *pos);
575 }
576 
bt_seq_next(struct seq_file * seq,void * v,loff_t * pos)577 static void *bt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
578 {
579 	struct bt_seq_state *s = seq->private;
580 	struct bt_sock_list *l = s->l;
581 
582 	return seq_hlist_next(v, &l->head, pos);
583 }
584 
bt_seq_stop(struct seq_file * seq,void * v)585 static void bt_seq_stop(struct seq_file *seq, void *v)
586 	__releases(seq->private->l->lock)
587 {
588 	struct bt_seq_state *s = seq->private;
589 	struct bt_sock_list *l = s->l;
590 
591 	read_unlock(&l->lock);
592 }
593 
bt_seq_show(struct seq_file * seq,void * v)594 static int bt_seq_show(struct seq_file *seq, void *v)
595 {
596 	struct bt_seq_state *s = seq->private;
597 	struct bt_sock_list *l = s->l;
598 
599 	if (v == SEQ_START_TOKEN) {
600 		seq_puts(seq ,"sk               RefCnt Rmem   Wmem   User   Inode  Src Dst Parent");
601 
602 		if (l->custom_seq_show) {
603 			seq_putc(seq, ' ');
604 			l->custom_seq_show(seq, v);
605 		}
606 
607 		seq_putc(seq, '\n');
608 	} else {
609 		struct sock *sk = sk_entry(v);
610 		struct bt_sock *bt = bt_sk(sk);
611 
612 		seq_printf(seq,
613 			   "%pK %-6d %-6u %-6u %-6u %-6lu %pMR %pMR %-6lu",
614 			   sk,
615 			   atomic_read(&sk->sk_refcnt),
616 			   sk_rmem_alloc_get(sk),
617 			   sk_wmem_alloc_get(sk),
618 			   from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
619 			   sock_i_ino(sk),
620 			   &bt->src,
621 			   &bt->dst,
622 			   bt->parent? sock_i_ino(bt->parent): 0LU);
623 
624 		if (l->custom_seq_show) {
625 			seq_putc(seq, ' ');
626 			l->custom_seq_show(seq, v);
627 		}
628 
629 		seq_putc(seq, '\n');
630 	}
631 	return 0;
632 }
633 
634 static struct seq_operations bt_seq_ops = {
635 	.start = bt_seq_start,
636 	.next  = bt_seq_next,
637 	.stop  = bt_seq_stop,
638 	.show  = bt_seq_show,
639 };
640 
bt_seq_open(struct inode * inode,struct file * file)641 static int bt_seq_open(struct inode *inode, struct file *file)
642 {
643 	struct bt_sock_list *sk_list;
644 	struct bt_seq_state *s;
645 
646 	sk_list = PDE_DATA(inode);
647 	s = __seq_open_private(file, &bt_seq_ops,
648 			       sizeof(struct bt_seq_state));
649 	if (!s)
650 		return -ENOMEM;
651 
652 	s->l = sk_list;
653 	return 0;
654 }
655 
656 static const struct file_operations bt_fops = {
657 	.open = bt_seq_open,
658 	.read = seq_read,
659 	.llseek = seq_lseek,
660 	.release = seq_release_private
661 };
662 
bt_procfs_init(struct net * net,const char * name,struct bt_sock_list * sk_list,int (* seq_show)(struct seq_file *,void *))663 int bt_procfs_init(struct net *net, const char *name,
664 		   struct bt_sock_list* sk_list,
665 		   int (* seq_show)(struct seq_file *, void *))
666 {
667 	sk_list->custom_seq_show = seq_show;
668 
669 	if (!proc_create_data(name, 0, net->proc_net, &bt_fops, sk_list))
670 		return -ENOMEM;
671 	return 0;
672 }
673 
bt_procfs_cleanup(struct net * net,const char * name)674 void bt_procfs_cleanup(struct net *net, const char *name)
675 {
676 	remove_proc_entry(name, net->proc_net);
677 }
678 #else
bt_procfs_init(struct net * net,const char * name,struct bt_sock_list * sk_list,int (* seq_show)(struct seq_file *,void *))679 int bt_procfs_init(struct net *net, const char *name,
680 		   struct bt_sock_list* sk_list,
681 		   int (* seq_show)(struct seq_file *, void *))
682 {
683 	return 0;
684 }
685 
bt_procfs_cleanup(struct net * net,const char * name)686 void bt_procfs_cleanup(struct net *net, const char *name)
687 {
688 }
689 #endif
690 EXPORT_SYMBOL(bt_procfs_init);
691 EXPORT_SYMBOL(bt_procfs_cleanup);
692 
693 static struct net_proto_family bt_sock_family_ops = {
694 	.owner	= THIS_MODULE,
695 	.family	= PF_BLUETOOTH,
696 	.create	= bt_sock_create,
697 };
698 
bt_init(void)699 static int __init bt_init(void)
700 {
701 	int err;
702 
703 	BT_INFO("Core ver %s", VERSION);
704 
705 	err = bt_sysfs_init();
706 	if (err < 0)
707 		return err;
708 
709 	err = sock_register(&bt_sock_family_ops);
710 	if (err < 0) {
711 		bt_sysfs_cleanup();
712 		return err;
713 	}
714 
715 	BT_INFO("HCI device and connection manager initialized");
716 
717 	err = hci_sock_init();
718 	if (err < 0)
719 		goto error;
720 
721 	err = l2cap_init();
722 	if (err < 0)
723 		goto sock_err;
724 
725 	err = sco_init();
726 	if (err < 0) {
727 		l2cap_exit();
728 		goto sock_err;
729 	}
730 
731 	return 0;
732 
733 sock_err:
734 	hci_sock_cleanup();
735 
736 error:
737 	sock_unregister(PF_BLUETOOTH);
738 	bt_sysfs_cleanup();
739 
740 	return err;
741 }
742 
bt_exit(void)743 static void __exit bt_exit(void)
744 {
745 
746 	sco_exit();
747 
748 	l2cap_exit();
749 
750 	hci_sock_cleanup();
751 
752 	sock_unregister(PF_BLUETOOTH);
753 
754 	bt_sysfs_cleanup();
755 }
756 
757 subsys_initcall(bt_init);
758 module_exit(bt_exit);
759 
760 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
761 MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
762 MODULE_VERSION(VERSION);
763 MODULE_LICENSE("GPL");
764 MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);
765