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