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 
29 #include <linux/types.h>
30 #include <linux/list.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/skbuff.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <net/sock.h>
38 #include <asm/ioctls.h>
39 #include <linux/kmod.h>
40 
41 #include <net/bluetooth/bluetooth.h>
42 
43 #define VERSION "2.16"
44 
45 /* Bluetooth sockets */
46 #define BT_MAX_PROTO	8
47 static const struct net_proto_family *bt_proto[BT_MAX_PROTO];
48 static DEFINE_RWLOCK(bt_proto_lock);
49 
50 static struct lock_class_key bt_lock_key[BT_MAX_PROTO];
51 static const char *const bt_key_strings[BT_MAX_PROTO] = {
52 	"sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP",
53 	"sk_lock-AF_BLUETOOTH-BTPROTO_HCI",
54 	"sk_lock-AF_BLUETOOTH-BTPROTO_SCO",
55 	"sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM",
56 	"sk_lock-AF_BLUETOOTH-BTPROTO_BNEP",
57 	"sk_lock-AF_BLUETOOTH-BTPROTO_CMTP",
58 	"sk_lock-AF_BLUETOOTH-BTPROTO_HIDP",
59 	"sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP",
60 };
61 
62 static struct lock_class_key bt_slock_key[BT_MAX_PROTO];
63 static const char *const bt_slock_key_strings[BT_MAX_PROTO] = {
64 	"slock-AF_BLUETOOTH-BTPROTO_L2CAP",
65 	"slock-AF_BLUETOOTH-BTPROTO_HCI",
66 	"slock-AF_BLUETOOTH-BTPROTO_SCO",
67 	"slock-AF_BLUETOOTH-BTPROTO_RFCOMM",
68 	"slock-AF_BLUETOOTH-BTPROTO_BNEP",
69 	"slock-AF_BLUETOOTH-BTPROTO_CMTP",
70 	"slock-AF_BLUETOOTH-BTPROTO_HIDP",
71 	"slock-AF_BLUETOOTH-BTPROTO_AVDTP",
72 };
73 
bt_sock_reclassify_lock(struct sock * sk,int proto)74 void bt_sock_reclassify_lock(struct sock *sk, int proto)
75 {
76 	BUG_ON(!sk);
77 	BUG_ON(sock_owned_by_user(sk));
78 
79 	sock_lock_init_class_and_name(sk,
80 			bt_slock_key_strings[proto], &bt_slock_key[proto],
81 				bt_key_strings[proto], &bt_lock_key[proto]);
82 }
83 EXPORT_SYMBOL(bt_sock_reclassify_lock);
84 
bt_sock_register(int proto,const struct net_proto_family * ops)85 int bt_sock_register(int proto, const struct net_proto_family *ops)
86 {
87 	int err = 0;
88 
89 	if (proto < 0 || proto >= BT_MAX_PROTO)
90 		return -EINVAL;
91 
92 	write_lock(&bt_proto_lock);
93 
94 	if (bt_proto[proto])
95 		err = -EEXIST;
96 	else
97 		bt_proto[proto] = ops;
98 
99 	write_unlock(&bt_proto_lock);
100 
101 	return err;
102 }
103 EXPORT_SYMBOL(bt_sock_register);
104 
bt_sock_unregister(int proto)105 int bt_sock_unregister(int proto)
106 {
107 	int err = 0;
108 
109 	if (proto < 0 || proto >= BT_MAX_PROTO)
110 		return -EINVAL;
111 
112 	write_lock(&bt_proto_lock);
113 
114 	if (!bt_proto[proto])
115 		err = -ENOENT;
116 	else
117 		bt_proto[proto] = NULL;
118 
119 	write_unlock(&bt_proto_lock);
120 
121 	return err;
122 }
123 EXPORT_SYMBOL(bt_sock_unregister);
124 
bt_sock_create(struct net * net,struct socket * sock,int proto,int kern)125 static int bt_sock_create(struct net *net, struct socket *sock, int proto,
126 			  int kern)
127 {
128 	int err;
129 
130 	if (net != &init_net)
131 		return -EAFNOSUPPORT;
132 
133 	if (proto < 0 || proto >= BT_MAX_PROTO)
134 		return -EINVAL;
135 
136 	if (!bt_proto[proto])
137 		request_module("bt-proto-%d", proto);
138 
139 	err = -EPROTONOSUPPORT;
140 
141 	read_lock(&bt_proto_lock);
142 
143 	if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
144 		err = bt_proto[proto]->create(net, sock, proto, kern);
145 		if (!err)
146 			bt_sock_reclassify_lock(sock->sk, proto);
147 		module_put(bt_proto[proto]->owner);
148 	}
149 
150 	read_unlock(&bt_proto_lock);
151 
152 	return err;
153 }
154 
bt_sock_link(struct bt_sock_list * l,struct sock * sk)155 void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
156 {
157 	write_lock(&l->lock);
158 	sk_add_node(sk, &l->head);
159 	write_unlock(&l->lock);
160 }
161 EXPORT_SYMBOL(bt_sock_link);
162 
bt_sock_unlink(struct bt_sock_list * l,struct sock * sk)163 void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
164 {
165 	write_lock(&l->lock);
166 	sk_del_node_init(sk);
167 	write_unlock(&l->lock);
168 }
169 EXPORT_SYMBOL(bt_sock_unlink);
170 
bt_accept_enqueue(struct sock * parent,struct sock * sk)171 void bt_accept_enqueue(struct sock *parent, struct sock *sk)
172 {
173 	BT_DBG("parent %p, sk %p", parent, sk);
174 
175 	sock_hold(sk);
176 	list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
177 	bt_sk(sk)->parent = parent;
178 	parent->sk_ack_backlog++;
179 }
180 EXPORT_SYMBOL(bt_accept_enqueue);
181 
bt_accept_unlink(struct sock * sk)182 void bt_accept_unlink(struct sock *sk)
183 {
184 	BT_DBG("sk %p state %d", sk, sk->sk_state);
185 
186 	list_del_init(&bt_sk(sk)->accept_q);
187 	bt_sk(sk)->parent->sk_ack_backlog--;
188 	bt_sk(sk)->parent = NULL;
189 	sock_put(sk);
190 }
191 EXPORT_SYMBOL(bt_accept_unlink);
192 
bt_accept_dequeue(struct sock * parent,struct socket * newsock)193 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
194 {
195 	struct list_head *p, *n;
196 	struct sock *sk;
197 
198 	BT_DBG("parent %p", parent);
199 
200 	list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
201 		sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
202 
203 		lock_sock(sk);
204 
205 		/* FIXME: Is this check still needed */
206 		if (sk->sk_state == BT_CLOSED) {
207 			release_sock(sk);
208 			bt_accept_unlink(sk);
209 			continue;
210 		}
211 
212 		if (sk->sk_state == BT_CONNECTED || !newsock ||
213 						bt_sk(parent)->defer_setup) {
214 			bt_accept_unlink(sk);
215 			if (newsock)
216 				sock_graft(sk, newsock);
217 
218 			release_sock(sk);
219 			return sk;
220 		}
221 
222 		release_sock(sk);
223 	}
224 
225 	return NULL;
226 }
227 EXPORT_SYMBOL(bt_accept_dequeue);
228 
bt_sock_recvmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t len,int flags)229 int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
230 				struct msghdr *msg, size_t len, int flags)
231 {
232 	int noblock = flags & MSG_DONTWAIT;
233 	struct sock *sk = sock->sk;
234 	struct sk_buff *skb;
235 	size_t copied;
236 	int err;
237 
238 	BT_DBG("sock %p sk %p len %zu", sock, sk, len);
239 
240 	if (flags & (MSG_OOB))
241 		return -EOPNOTSUPP;
242 
243 	skb = skb_recv_datagram(sk, flags, noblock, &err);
244 	if (!skb) {
245 		if (sk->sk_shutdown & RCV_SHUTDOWN)
246 			return 0;
247 		return err;
248 	}
249 
250 	copied = skb->len;
251 	if (len < copied) {
252 		msg->msg_flags |= MSG_TRUNC;
253 		copied = len;
254 	}
255 
256 	skb_reset_transport_header(skb);
257 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
258 	if (err == 0)
259 		sock_recv_ts_and_drops(msg, sk, skb);
260 
261 	skb_free_datagram(sk, skb);
262 
263 	return err ? : copied;
264 }
265 EXPORT_SYMBOL(bt_sock_recvmsg);
266 
bt_sock_data_wait(struct sock * sk,long timeo)267 static long bt_sock_data_wait(struct sock *sk, long timeo)
268 {
269 	DECLARE_WAITQUEUE(wait, current);
270 
271 	add_wait_queue(sk_sleep(sk), &wait);
272 	for (;;) {
273 		set_current_state(TASK_INTERRUPTIBLE);
274 
275 		if (!skb_queue_empty(&sk->sk_receive_queue))
276 			break;
277 
278 		if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
279 			break;
280 
281 		if (signal_pending(current) || !timeo)
282 			break;
283 
284 		set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
285 		release_sock(sk);
286 		timeo = schedule_timeout(timeo);
287 		lock_sock(sk);
288 		clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
289 	}
290 
291 	__set_current_state(TASK_RUNNING);
292 	remove_wait_queue(sk_sleep(sk), &wait);
293 	return timeo;
294 }
295 
bt_sock_stream_recvmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t size,int flags)296 int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
297 			       struct msghdr *msg, size_t size, int flags)
298 {
299 	struct sock *sk = sock->sk;
300 	int err = 0;
301 	size_t target, copied = 0;
302 	long timeo;
303 
304 	if (flags & MSG_OOB)
305 		return -EOPNOTSUPP;
306 
307 	BT_DBG("sk %p size %zu", sk, size);
308 
309 	lock_sock(sk);
310 
311 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
312 	timeo  = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
313 
314 	do {
315 		struct sk_buff *skb;
316 		int chunk;
317 
318 		skb = skb_dequeue(&sk->sk_receive_queue);
319 		if (!skb) {
320 			if (copied >= target)
321 				break;
322 
323 			err = sock_error(sk);
324 			if (err)
325 				break;
326 			if (sk->sk_shutdown & RCV_SHUTDOWN)
327 				break;
328 
329 			err = -EAGAIN;
330 			if (!timeo)
331 				break;
332 
333 			timeo = bt_sock_data_wait(sk, timeo);
334 
335 			if (signal_pending(current)) {
336 				err = sock_intr_errno(timeo);
337 				goto out;
338 			}
339 			continue;
340 		}
341 
342 		chunk = min_t(unsigned int, skb->len, size);
343 		if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, chunk)) {
344 			skb_queue_head(&sk->sk_receive_queue, skb);
345 			if (!copied)
346 				copied = -EFAULT;
347 			break;
348 		}
349 		copied += chunk;
350 		size   -= chunk;
351 
352 		sock_recv_ts_and_drops(msg, sk, skb);
353 
354 		if (!(flags & MSG_PEEK)) {
355 			int skb_len = skb_headlen(skb);
356 
357 			if (chunk <= skb_len) {
358 				__skb_pull(skb, chunk);
359 			} else {
360 				struct sk_buff *frag;
361 
362 				__skb_pull(skb, skb_len);
363 				chunk -= skb_len;
364 
365 				skb_walk_frags(skb, frag) {
366 					if (chunk <= frag->len) {
367 						/* Pulling partial data */
368 						skb->len -= chunk;
369 						skb->data_len -= chunk;
370 						__skb_pull(frag, chunk);
371 						break;
372 					} else if (frag->len) {
373 						/* Pulling all frag data */
374 						chunk -= frag->len;
375 						skb->len -= frag->len;
376 						skb->data_len -= frag->len;
377 						__skb_pull(frag, frag->len);
378 					}
379 				}
380 			}
381 
382 			if (skb->len) {
383 				skb_queue_head(&sk->sk_receive_queue, skb);
384 				break;
385 			}
386 			kfree_skb(skb);
387 
388 		} else {
389 			/* put message back and return */
390 			skb_queue_head(&sk->sk_receive_queue, skb);
391 			break;
392 		}
393 	} while (size);
394 
395 out:
396 	release_sock(sk);
397 	return copied ? : err;
398 }
399 EXPORT_SYMBOL(bt_sock_stream_recvmsg);
400 
bt_accept_poll(struct sock * parent)401 static inline unsigned int bt_accept_poll(struct sock *parent)
402 {
403 	struct list_head *p, *n;
404 	struct sock *sk;
405 
406 	list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
407 		sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
408 		if (sk->sk_state == BT_CONNECTED ||
409 					(bt_sk(parent)->defer_setup &&
410 						sk->sk_state == BT_CONNECT2))
411 			return POLLIN | POLLRDNORM;
412 	}
413 
414 	return 0;
415 }
416 
bt_sock_poll(struct file * file,struct socket * sock,poll_table * wait)417 unsigned int bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait)
418 {
419 	struct sock *sk = sock->sk;
420 	unsigned int mask = 0;
421 
422 	BT_DBG("sock %p, sk %p", sock, sk);
423 
424 	poll_wait(file, sk_sleep(sk), wait);
425 
426 	if (sk->sk_state == BT_LISTEN)
427 		return bt_accept_poll(sk);
428 
429 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
430 		mask |= POLLERR;
431 
432 	if (sk->sk_shutdown & RCV_SHUTDOWN)
433 		mask |= POLLRDHUP | POLLIN | POLLRDNORM;
434 
435 	if (sk->sk_shutdown == SHUTDOWN_MASK)
436 		mask |= POLLHUP;
437 
438 	if (!skb_queue_empty(&sk->sk_receive_queue))
439 		mask |= POLLIN | POLLRDNORM;
440 
441 	if (sk->sk_state == BT_CLOSED)
442 		mask |= POLLHUP;
443 
444 	if (sk->sk_state == BT_CONNECT ||
445 			sk->sk_state == BT_CONNECT2 ||
446 			sk->sk_state == BT_CONFIG)
447 		return mask;
448 
449 	if (!bt_sk(sk)->suspended && sock_writeable(sk))
450 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
451 	else
452 		set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
453 
454 	return mask;
455 }
456 EXPORT_SYMBOL(bt_sock_poll);
457 
bt_sock_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)458 int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
459 {
460 	struct sock *sk = sock->sk;
461 	struct sk_buff *skb;
462 	long amount;
463 	int err;
464 
465 	BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
466 
467 	switch (cmd) {
468 	case TIOCOUTQ:
469 		if (sk->sk_state == BT_LISTEN)
470 			return -EINVAL;
471 
472 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
473 		if (amount < 0)
474 			amount = 0;
475 		err = put_user(amount, (int __user *) arg);
476 		break;
477 
478 	case TIOCINQ:
479 		if (sk->sk_state == BT_LISTEN)
480 			return -EINVAL;
481 
482 		lock_sock(sk);
483 		skb = skb_peek(&sk->sk_receive_queue);
484 		amount = skb ? skb->len : 0;
485 		release_sock(sk);
486 		err = put_user(amount, (int __user *) arg);
487 		break;
488 
489 	case SIOCGSTAMP:
490 		err = sock_get_timestamp(sk, (struct timeval __user *) arg);
491 		break;
492 
493 	case SIOCGSTAMPNS:
494 		err = sock_get_timestampns(sk, (struct timespec __user *) arg);
495 		break;
496 
497 	default:
498 		err = -ENOIOCTLCMD;
499 		break;
500 	}
501 
502 	return err;
503 }
504 EXPORT_SYMBOL(bt_sock_ioctl);
505 
bt_sock_wait_state(struct sock * sk,int state,unsigned long timeo)506 int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
507 {
508 	DECLARE_WAITQUEUE(wait, current);
509 	int err = 0;
510 
511 	BT_DBG("sk %p", sk);
512 
513 	add_wait_queue(sk_sleep(sk), &wait);
514 	set_current_state(TASK_INTERRUPTIBLE);
515 	while (sk->sk_state != state) {
516 		if (!timeo) {
517 			err = -EINPROGRESS;
518 			break;
519 		}
520 
521 		if (signal_pending(current)) {
522 			err = sock_intr_errno(timeo);
523 			break;
524 		}
525 
526 		release_sock(sk);
527 		timeo = schedule_timeout(timeo);
528 		lock_sock(sk);
529 		set_current_state(TASK_INTERRUPTIBLE);
530 
531 		err = sock_error(sk);
532 		if (err)
533 			break;
534 	}
535 	__set_current_state(TASK_RUNNING);
536 	remove_wait_queue(sk_sleep(sk), &wait);
537 	return err;
538 }
539 EXPORT_SYMBOL(bt_sock_wait_state);
540 
541 static struct net_proto_family bt_sock_family_ops = {
542 	.owner	= THIS_MODULE,
543 	.family	= PF_BLUETOOTH,
544 	.create	= bt_sock_create,
545 };
546 
bt_init(void)547 static int __init bt_init(void)
548 {
549 	int err;
550 
551 	BT_INFO("Core ver %s", VERSION);
552 
553 	err = bt_sysfs_init();
554 	if (err < 0)
555 		return err;
556 
557 	err = sock_register(&bt_sock_family_ops);
558 	if (err < 0) {
559 		bt_sysfs_cleanup();
560 		return err;
561 	}
562 
563 	BT_INFO("HCI device and connection manager initialized");
564 
565 	err = hci_sock_init();
566 	if (err < 0)
567 		goto error;
568 
569 	err = l2cap_init();
570 	if (err < 0)
571 		goto sock_err;
572 
573 	err = sco_init();
574 	if (err < 0) {
575 		l2cap_exit();
576 		goto sock_err;
577 	}
578 
579 	return 0;
580 
581 sock_err:
582 	hci_sock_cleanup();
583 
584 error:
585 	sock_unregister(PF_BLUETOOTH);
586 	bt_sysfs_cleanup();
587 
588 	return err;
589 }
590 
bt_exit(void)591 static void __exit bt_exit(void)
592 {
593 
594 	sco_exit();
595 
596 	l2cap_exit();
597 
598 	hci_sock_cleanup();
599 
600 	sock_unregister(PF_BLUETOOTH);
601 
602 	bt_sysfs_cleanup();
603 }
604 
605 subsys_initcall(bt_init);
606 module_exit(bt_exit);
607 
608 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
609 MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
610 MODULE_VERSION(VERSION);
611 MODULE_LICENSE("GPL");
612 MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);
613