xref: /openbmc/linux/net/bluetooth/af_bluetooth.c (revision 7a846d3c43b0b6d04300be9ba666b102b57a391a)
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 <linux/debugfs.h>
29 #include <linux/stringify.h>
30 #include <linux/sched/signal.h>
31 
32 #include <asm/ioctls.h>
33 
34 #include <net/bluetooth/bluetooth.h>
35 #include <linux/proc_fs.h>
36 
37 #include "leds.h"
38 #include "selftest.h"
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 
69 void bt_sock_reclassify_lock(struct sock *sk, int proto)
70 {
71 	BUG_ON(!sk);
72 	BUG_ON(!sock_allow_reclassification(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 
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 
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 static int bt_sock_create(struct net *net, struct socket *sock, int proto,
112 			  int kern)
113 {
114 	int err;
115 
116 	if (net != &init_net)
117 		return -EAFNOSUPPORT;
118 
119 	if (proto < 0 || proto >= BT_MAX_PROTO)
120 		return -EINVAL;
121 
122 	if (!bt_proto[proto])
123 		request_module("bt-proto-%d", proto);
124 
125 	err = -EPROTONOSUPPORT;
126 
127 	read_lock(&bt_proto_lock);
128 
129 	if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
130 		err = bt_proto[proto]->create(net, sock, proto, kern);
131 		if (!err)
132 			bt_sock_reclassify_lock(sock->sk, proto);
133 		module_put(bt_proto[proto]->owner);
134 	}
135 
136 	read_unlock(&bt_proto_lock);
137 
138 	return err;
139 }
140 
141 void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
142 {
143 	write_lock(&l->lock);
144 	sk_add_node(sk, &l->head);
145 	write_unlock(&l->lock);
146 }
147 EXPORT_SYMBOL(bt_sock_link);
148 
149 void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
150 {
151 	write_lock(&l->lock);
152 	sk_del_node_init(sk);
153 	write_unlock(&l->lock);
154 }
155 EXPORT_SYMBOL(bt_sock_unlink);
156 
157 void bt_accept_enqueue(struct sock *parent, struct sock *sk)
158 {
159 	BT_DBG("parent %p, sk %p", parent, sk);
160 
161 	sock_hold(sk);
162 	lock_sock(sk);
163 	list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
164 	bt_sk(sk)->parent = parent;
165 	release_sock(sk);
166 	parent->sk_ack_backlog++;
167 }
168 EXPORT_SYMBOL(bt_accept_enqueue);
169 
170 /* Calling function must hold the sk lock.
171  * bt_sk(sk)->parent must be non-NULL meaning sk is in the parent list.
172  */
173 void bt_accept_unlink(struct sock *sk)
174 {
175 	BT_DBG("sk %p state %d", sk, sk->sk_state);
176 
177 	list_del_init(&bt_sk(sk)->accept_q);
178 	bt_sk(sk)->parent->sk_ack_backlog--;
179 	bt_sk(sk)->parent = NULL;
180 	sock_put(sk);
181 }
182 EXPORT_SYMBOL(bt_accept_unlink);
183 
184 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
185 {
186 	struct bt_sock *s, *n;
187 	struct sock *sk;
188 
189 	BT_DBG("parent %p", parent);
190 
191 restart:
192 	list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) {
193 		sk = (struct sock *)s;
194 
195 		/* Prevent early freeing of sk due to unlink and sock_kill */
196 		sock_hold(sk);
197 		lock_sock(sk);
198 
199 		/* Check sk has not already been unlinked via
200 		 * bt_accept_unlink() due to serialisation caused by sk locking
201 		 */
202 		if (!bt_sk(sk)->parent) {
203 			BT_DBG("sk %p, already unlinked", sk);
204 			release_sock(sk);
205 			sock_put(sk);
206 
207 			/* Restart the loop as sk is no longer in the list
208 			 * and also avoid a potential infinite loop because
209 			 * list_for_each_entry_safe() is not thread safe.
210 			 */
211 			goto restart;
212 		}
213 
214 		/* sk is safely in the parent list so reduce reference count */
215 		sock_put(sk);
216 
217 		/* FIXME: Is this check still needed */
218 		if (sk->sk_state == BT_CLOSED) {
219 			bt_accept_unlink(sk);
220 			release_sock(sk);
221 			continue;
222 		}
223 
224 		if (sk->sk_state == BT_CONNECTED || !newsock ||
225 		    test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) {
226 			bt_accept_unlink(sk);
227 			if (newsock)
228 				sock_graft(sk, newsock);
229 
230 			release_sock(sk);
231 			return sk;
232 		}
233 
234 		release_sock(sk);
235 	}
236 
237 	return NULL;
238 }
239 EXPORT_SYMBOL(bt_accept_dequeue);
240 
241 int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
242 		    int flags)
243 {
244 	int noblock = flags & MSG_DONTWAIT;
245 	struct sock *sk = sock->sk;
246 	struct sk_buff *skb;
247 	size_t copied;
248 	size_t skblen;
249 	int err;
250 
251 	BT_DBG("sock %p sk %p len %zu", sock, sk, len);
252 
253 	if (flags & MSG_OOB)
254 		return -EOPNOTSUPP;
255 
256 	skb = skb_recv_datagram(sk, flags, noblock, &err);
257 	if (!skb) {
258 		if (sk->sk_shutdown & RCV_SHUTDOWN)
259 			return 0;
260 
261 		return err;
262 	}
263 
264 	skblen = skb->len;
265 	copied = skb->len;
266 	if (len < copied) {
267 		msg->msg_flags |= MSG_TRUNC;
268 		copied = len;
269 	}
270 
271 	skb_reset_transport_header(skb);
272 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
273 	if (err == 0) {
274 		sock_recv_ts_and_drops(msg, sk, skb);
275 
276 		if (msg->msg_name && bt_sk(sk)->skb_msg_name)
277 			bt_sk(sk)->skb_msg_name(skb, msg->msg_name,
278 						&msg->msg_namelen);
279 	}
280 
281 	skb_free_datagram(sk, skb);
282 
283 	if (flags & MSG_TRUNC)
284 		copied = skblen;
285 
286 	return err ? : copied;
287 }
288 EXPORT_SYMBOL(bt_sock_recvmsg);
289 
290 static long bt_sock_data_wait(struct sock *sk, long timeo)
291 {
292 	DECLARE_WAITQUEUE(wait, current);
293 
294 	add_wait_queue(sk_sleep(sk), &wait);
295 	for (;;) {
296 		set_current_state(TASK_INTERRUPTIBLE);
297 
298 		if (!skb_queue_empty(&sk->sk_receive_queue))
299 			break;
300 
301 		if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
302 			break;
303 
304 		if (signal_pending(current) || !timeo)
305 			break;
306 
307 		sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
308 		release_sock(sk);
309 		timeo = schedule_timeout(timeo);
310 		lock_sock(sk);
311 		sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
312 	}
313 
314 	__set_current_state(TASK_RUNNING);
315 	remove_wait_queue(sk_sleep(sk), &wait);
316 	return timeo;
317 }
318 
319 int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
320 			   size_t size, int flags)
321 {
322 	struct sock *sk = sock->sk;
323 	int err = 0;
324 	size_t target, copied = 0;
325 	long timeo;
326 
327 	if (flags & MSG_OOB)
328 		return -EOPNOTSUPP;
329 
330 	BT_DBG("sk %p size %zu", sk, size);
331 
332 	lock_sock(sk);
333 
334 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
335 	timeo  = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
336 
337 	do {
338 		struct sk_buff *skb;
339 		int chunk;
340 
341 		skb = skb_dequeue(&sk->sk_receive_queue);
342 		if (!skb) {
343 			if (copied >= target)
344 				break;
345 
346 			err = sock_error(sk);
347 			if (err)
348 				break;
349 			if (sk->sk_shutdown & RCV_SHUTDOWN)
350 				break;
351 
352 			err = -EAGAIN;
353 			if (!timeo)
354 				break;
355 
356 			timeo = bt_sock_data_wait(sk, timeo);
357 
358 			if (signal_pending(current)) {
359 				err = sock_intr_errno(timeo);
360 				goto out;
361 			}
362 			continue;
363 		}
364 
365 		chunk = min_t(unsigned int, skb->len, size);
366 		if (skb_copy_datagram_msg(skb, 0, msg, chunk)) {
367 			skb_queue_head(&sk->sk_receive_queue, skb);
368 			if (!copied)
369 				copied = -EFAULT;
370 			break;
371 		}
372 		copied += chunk;
373 		size   -= chunk;
374 
375 		sock_recv_ts_and_drops(msg, sk, skb);
376 
377 		if (!(flags & MSG_PEEK)) {
378 			int skb_len = skb_headlen(skb);
379 
380 			if (chunk <= skb_len) {
381 				__skb_pull(skb, chunk);
382 			} else {
383 				struct sk_buff *frag;
384 
385 				__skb_pull(skb, skb_len);
386 				chunk -= skb_len;
387 
388 				skb_walk_frags(skb, frag) {
389 					if (chunk <= frag->len) {
390 						/* Pulling partial data */
391 						skb->len -= chunk;
392 						skb->data_len -= chunk;
393 						__skb_pull(frag, chunk);
394 						break;
395 					} else if (frag->len) {
396 						/* Pulling all frag data */
397 						chunk -= frag->len;
398 						skb->len -= frag->len;
399 						skb->data_len -= frag->len;
400 						__skb_pull(frag, frag->len);
401 					}
402 				}
403 			}
404 
405 			if (skb->len) {
406 				skb_queue_head(&sk->sk_receive_queue, skb);
407 				break;
408 			}
409 			kfree_skb(skb);
410 
411 		} else {
412 			/* put message back and return */
413 			skb_queue_head(&sk->sk_receive_queue, skb);
414 			break;
415 		}
416 	} while (size);
417 
418 out:
419 	release_sock(sk);
420 	return copied ? : err;
421 }
422 EXPORT_SYMBOL(bt_sock_stream_recvmsg);
423 
424 static inline __poll_t bt_accept_poll(struct sock *parent)
425 {
426 	struct bt_sock *s, *n;
427 	struct sock *sk;
428 
429 	list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) {
430 		sk = (struct sock *)s;
431 		if (sk->sk_state == BT_CONNECTED ||
432 		    (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) &&
433 		     sk->sk_state == BT_CONNECT2))
434 			return EPOLLIN | EPOLLRDNORM;
435 	}
436 
437 	return 0;
438 }
439 
440 __poll_t bt_sock_poll_mask(struct socket *sock, __poll_t events)
441 {
442 	struct sock *sk = sock->sk;
443 	__poll_t mask = 0;
444 
445 	BT_DBG("sock %p, sk %p", sock, sk);
446 
447 	if (sk->sk_state == BT_LISTEN)
448 		return bt_accept_poll(sk);
449 
450 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
451 		mask |= EPOLLERR |
452 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
453 
454 	if (sk->sk_shutdown & RCV_SHUTDOWN)
455 		mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
456 
457 	if (sk->sk_shutdown == SHUTDOWN_MASK)
458 		mask |= EPOLLHUP;
459 
460 	if (!skb_queue_empty(&sk->sk_receive_queue))
461 		mask |= EPOLLIN | EPOLLRDNORM;
462 
463 	if (sk->sk_state == BT_CLOSED)
464 		mask |= EPOLLHUP;
465 
466 	if (sk->sk_state == BT_CONNECT ||
467 			sk->sk_state == BT_CONNECT2 ||
468 			sk->sk_state == BT_CONFIG)
469 		return mask;
470 
471 	if (!test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags) && sock_writeable(sk))
472 		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
473 	else
474 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
475 
476 	return mask;
477 }
478 EXPORT_SYMBOL(bt_sock_poll_mask);
479 
480 int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
481 {
482 	struct sock *sk = sock->sk;
483 	struct sk_buff *skb;
484 	long amount;
485 	int err;
486 
487 	BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
488 
489 	switch (cmd) {
490 	case TIOCOUTQ:
491 		if (sk->sk_state == BT_LISTEN)
492 			return -EINVAL;
493 
494 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
495 		if (amount < 0)
496 			amount = 0;
497 		err = put_user(amount, (int __user *) arg);
498 		break;
499 
500 	case TIOCINQ:
501 		if (sk->sk_state == BT_LISTEN)
502 			return -EINVAL;
503 
504 		lock_sock(sk);
505 		skb = skb_peek(&sk->sk_receive_queue);
506 		amount = skb ? skb->len : 0;
507 		release_sock(sk);
508 		err = put_user(amount, (int __user *) arg);
509 		break;
510 
511 	case SIOCGSTAMP:
512 		err = sock_get_timestamp(sk, (struct timeval __user *) arg);
513 		break;
514 
515 	case SIOCGSTAMPNS:
516 		err = sock_get_timestampns(sk, (struct timespec __user *) arg);
517 		break;
518 
519 	default:
520 		err = -ENOIOCTLCMD;
521 		break;
522 	}
523 
524 	return err;
525 }
526 EXPORT_SYMBOL(bt_sock_ioctl);
527 
528 /* This function expects the sk lock to be held when called */
529 int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
530 {
531 	DECLARE_WAITQUEUE(wait, current);
532 	int err = 0;
533 
534 	BT_DBG("sk %p", sk);
535 
536 	add_wait_queue(sk_sleep(sk), &wait);
537 	set_current_state(TASK_INTERRUPTIBLE);
538 	while (sk->sk_state != state) {
539 		if (!timeo) {
540 			err = -EINPROGRESS;
541 			break;
542 		}
543 
544 		if (signal_pending(current)) {
545 			err = sock_intr_errno(timeo);
546 			break;
547 		}
548 
549 		release_sock(sk);
550 		timeo = schedule_timeout(timeo);
551 		lock_sock(sk);
552 		set_current_state(TASK_INTERRUPTIBLE);
553 
554 		err = sock_error(sk);
555 		if (err)
556 			break;
557 	}
558 	__set_current_state(TASK_RUNNING);
559 	remove_wait_queue(sk_sleep(sk), &wait);
560 	return err;
561 }
562 EXPORT_SYMBOL(bt_sock_wait_state);
563 
564 /* This function expects the sk lock to be held when called */
565 int bt_sock_wait_ready(struct sock *sk, unsigned long flags)
566 {
567 	DECLARE_WAITQUEUE(wait, current);
568 	unsigned long timeo;
569 	int err = 0;
570 
571 	BT_DBG("sk %p", sk);
572 
573 	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
574 
575 	add_wait_queue(sk_sleep(sk), &wait);
576 	set_current_state(TASK_INTERRUPTIBLE);
577 	while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) {
578 		if (!timeo) {
579 			err = -EAGAIN;
580 			break;
581 		}
582 
583 		if (signal_pending(current)) {
584 			err = sock_intr_errno(timeo);
585 			break;
586 		}
587 
588 		release_sock(sk);
589 		timeo = schedule_timeout(timeo);
590 		lock_sock(sk);
591 		set_current_state(TASK_INTERRUPTIBLE);
592 
593 		err = sock_error(sk);
594 		if (err)
595 			break;
596 	}
597 	__set_current_state(TASK_RUNNING);
598 	remove_wait_queue(sk_sleep(sk), &wait);
599 
600 	return err;
601 }
602 EXPORT_SYMBOL(bt_sock_wait_ready);
603 
604 #ifdef CONFIG_PROC_FS
605 static void *bt_seq_start(struct seq_file *seq, loff_t *pos)
606 	__acquires(seq->private->l->lock)
607 {
608 	struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
609 
610 	read_lock(&l->lock);
611 	return seq_hlist_start_head(&l->head, *pos);
612 }
613 
614 static void *bt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
615 {
616 	struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
617 
618 	return seq_hlist_next(v, &l->head, pos);
619 }
620 
621 static void bt_seq_stop(struct seq_file *seq, void *v)
622 	__releases(seq->private->l->lock)
623 {
624 	struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
625 
626 	read_unlock(&l->lock);
627 }
628 
629 static int bt_seq_show(struct seq_file *seq, void *v)
630 {
631 	struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
632 
633 	if (v == SEQ_START_TOKEN) {
634 		seq_puts(seq ,"sk               RefCnt Rmem   Wmem   User   Inode  Parent");
635 
636 		if (l->custom_seq_show) {
637 			seq_putc(seq, ' ');
638 			l->custom_seq_show(seq, v);
639 		}
640 
641 		seq_putc(seq, '\n');
642 	} else {
643 		struct sock *sk = sk_entry(v);
644 		struct bt_sock *bt = bt_sk(sk);
645 
646 		seq_printf(seq,
647 			   "%pK %-6d %-6u %-6u %-6u %-6lu %-6lu",
648 			   sk,
649 			   refcount_read(&sk->sk_refcnt),
650 			   sk_rmem_alloc_get(sk),
651 			   sk_wmem_alloc_get(sk),
652 			   from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
653 			   sock_i_ino(sk),
654 			   bt->parent? sock_i_ino(bt->parent): 0LU);
655 
656 		if (l->custom_seq_show) {
657 			seq_putc(seq, ' ');
658 			l->custom_seq_show(seq, v);
659 		}
660 
661 		seq_putc(seq, '\n');
662 	}
663 	return 0;
664 }
665 
666 static const struct seq_operations bt_seq_ops = {
667 	.start = bt_seq_start,
668 	.next  = bt_seq_next,
669 	.stop  = bt_seq_stop,
670 	.show  = bt_seq_show,
671 };
672 
673 int bt_procfs_init(struct net *net, const char *name,
674 		   struct bt_sock_list *sk_list,
675 		   int (* seq_show)(struct seq_file *, void *))
676 {
677 	sk_list->custom_seq_show = seq_show;
678 
679 	if (!proc_create_seq_data(name, 0, net->proc_net, &bt_seq_ops, sk_list))
680 		return -ENOMEM;
681 	return 0;
682 }
683 
684 void bt_procfs_cleanup(struct net *net, const char *name)
685 {
686 	remove_proc_entry(name, net->proc_net);
687 }
688 #else
689 int bt_procfs_init(struct net *net, const char *name,
690 		   struct bt_sock_list *sk_list,
691 		   int (* seq_show)(struct seq_file *, void *))
692 {
693 	return 0;
694 }
695 
696 void bt_procfs_cleanup(struct net *net, const char *name)
697 {
698 }
699 #endif
700 EXPORT_SYMBOL(bt_procfs_init);
701 EXPORT_SYMBOL(bt_procfs_cleanup);
702 
703 static const struct net_proto_family bt_sock_family_ops = {
704 	.owner	= THIS_MODULE,
705 	.family	= PF_BLUETOOTH,
706 	.create	= bt_sock_create,
707 };
708 
709 struct dentry *bt_debugfs;
710 EXPORT_SYMBOL_GPL(bt_debugfs);
711 
712 #define VERSION __stringify(BT_SUBSYS_VERSION) "." \
713 		__stringify(BT_SUBSYS_REVISION)
714 
715 static int __init bt_init(void)
716 {
717 	int err;
718 
719 	sock_skb_cb_check_size(sizeof(struct bt_skb_cb));
720 
721 	BT_INFO("Core ver %s", VERSION);
722 
723 	err = bt_selftest();
724 	if (err < 0)
725 		return err;
726 
727 	bt_debugfs = debugfs_create_dir("bluetooth", NULL);
728 
729 	bt_leds_init();
730 
731 	err = bt_sysfs_init();
732 	if (err < 0)
733 		return err;
734 
735 	err = sock_register(&bt_sock_family_ops);
736 	if (err)
737 		goto cleanup_sysfs;
738 
739 	BT_INFO("HCI device and connection manager initialized");
740 
741 	err = hci_sock_init();
742 	if (err)
743 		goto unregister_socket;
744 
745 	err = l2cap_init();
746 	if (err)
747 		goto cleanup_socket;
748 
749 	err = sco_init();
750 	if (err)
751 		goto cleanup_cap;
752 
753 	err = mgmt_init();
754 	if (err)
755 		goto cleanup_sco;
756 
757 	return 0;
758 
759 cleanup_sco:
760 	sco_exit();
761 cleanup_cap:
762 	l2cap_exit();
763 cleanup_socket:
764 	hci_sock_cleanup();
765 unregister_socket:
766 	sock_unregister(PF_BLUETOOTH);
767 cleanup_sysfs:
768 	bt_sysfs_cleanup();
769 	return err;
770 }
771 
772 static void __exit bt_exit(void)
773 {
774 	mgmt_exit();
775 
776 	sco_exit();
777 
778 	l2cap_exit();
779 
780 	hci_sock_cleanup();
781 
782 	sock_unregister(PF_BLUETOOTH);
783 
784 	bt_sysfs_cleanup();
785 
786 	bt_leds_cleanup();
787 
788 	debugfs_remove_recursive(bt_debugfs);
789 }
790 
791 subsys_initcall(bt_init);
792 module_exit(bt_exit);
793 
794 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
795 MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
796 MODULE_VERSION(VERSION);
797 MODULE_LICENSE("GPL");
798 MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);
799