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