bluetooth: hid: make Android code conditional
[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 static inline void bt_sock_reclassify_lock(struct socket *sock, int proto)
84 {
85         struct sock *sk = sock->sk;
86
87         if (!sk)
88                 return;
89
90         BUG_ON(sock_owned_by_user(sk));
91
92         sock_lock_init_class_and_name(sk,
93                         bt_slock_key_strings[proto], &bt_slock_key[proto],
94                                 bt_key_strings[proto], &bt_lock_key[proto]);
95 }
96
97 int bt_sock_register(int proto, const struct net_proto_family *ops)
98 {
99         int err = 0;
100
101         if (proto < 0 || proto >= BT_MAX_PROTO)
102                 return -EINVAL;
103
104         write_lock(&bt_proto_lock);
105
106         if (bt_proto[proto])
107                 err = -EEXIST;
108         else
109                 bt_proto[proto] = ops;
110
111         write_unlock(&bt_proto_lock);
112
113         return err;
114 }
115 EXPORT_SYMBOL(bt_sock_register);
116
117 int bt_sock_unregister(int proto)
118 {
119         int err = 0;
120
121         if (proto < 0 || proto >= BT_MAX_PROTO)
122                 return -EINVAL;
123
124         write_lock(&bt_proto_lock);
125
126         if (!bt_proto[proto])
127                 err = -ENOENT;
128         else
129                 bt_proto[proto] = NULL;
130
131         write_unlock(&bt_proto_lock);
132
133         return err;
134 }
135 EXPORT_SYMBOL(bt_sock_unregister);
136
137 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
138 static inline int current_has_bt_admin(void)
139 {
140         return (!current_euid() || in_egroup_p(AID_NET_BT_ADMIN));
141 }
142
143 static inline int current_has_bt(void)
144 {
145         return (current_has_bt_admin() || in_egroup_p(AID_NET_BT));
146 }
147 # else
148 static inline int current_has_bt_admin(void)
149 {
150         return 1;
151 }
152
153 static inline int current_has_bt(void)
154 {
155         return 1;
156 }
157 #endif
158
159 static int bt_sock_create(struct net *net, struct socket *sock, int proto,
160                           int kern)
161 {
162         int err;
163
164         if (proto == BTPROTO_RFCOMM || proto == BTPROTO_SCO ||
165                         proto == BTPROTO_L2CAP) {
166                 if (!current_has_bt())
167                         return -EPERM;
168         } else if (!current_has_bt_admin())
169                 return -EPERM;
170
171         if (net != &init_net)
172                 return -EAFNOSUPPORT;
173
174         if (proto < 0 || proto >= BT_MAX_PROTO)
175                 return -EINVAL;
176
177         if (!bt_proto[proto])
178                 request_module("bt-proto-%d", proto);
179
180         err = -EPROTONOSUPPORT;
181
182         read_lock(&bt_proto_lock);
183
184         if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
185                 err = bt_proto[proto]->create(net, sock, proto, kern);
186                 bt_sock_reclassify_lock(sock, proto);
187                 module_put(bt_proto[proto]->owner);
188         }
189
190         read_unlock(&bt_proto_lock);
191
192         return err;
193 }
194
195 void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
196 {
197         write_lock_bh(&l->lock);
198         sk_add_node(sk, &l->head);
199         write_unlock_bh(&l->lock);
200 }
201 EXPORT_SYMBOL(bt_sock_link);
202
203 void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
204 {
205         write_lock_bh(&l->lock);
206         sk_del_node_init(sk);
207         write_unlock_bh(&l->lock);
208 }
209 EXPORT_SYMBOL(bt_sock_unlink);
210
211 void bt_accept_enqueue(struct sock *parent, struct sock *sk)
212 {
213         BT_DBG("parent %p, sk %p", parent, sk);
214
215         sock_hold(sk);
216         list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
217         bt_sk(sk)->parent = parent;
218         parent->sk_ack_backlog++;
219 }
220 EXPORT_SYMBOL(bt_accept_enqueue);
221
222 void bt_accept_unlink(struct sock *sk)
223 {
224         BT_DBG("sk %p state %d", sk, sk->sk_state);
225
226         list_del_init(&bt_sk(sk)->accept_q);
227         bt_sk(sk)->parent->sk_ack_backlog--;
228         bt_sk(sk)->parent = NULL;
229         sock_put(sk);
230 }
231 EXPORT_SYMBOL(bt_accept_unlink);
232
233 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
234 {
235         struct list_head *p, *n;
236         struct sock *sk;
237
238         BT_DBG("parent %p", parent);
239
240         local_bh_disable();
241         list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
242                 sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
243
244                 bh_lock_sock(sk);
245
246                 /* FIXME: Is this check still needed */
247                 if (sk->sk_state == BT_CLOSED) {
248                         bh_unlock_sock(sk);
249                         bt_accept_unlink(sk);
250                         continue;
251                 }
252
253                 if (sk->sk_state == BT_CONNECTED || !newsock ||
254                                                 bt_sk(parent)->defer_setup) {
255                         bt_accept_unlink(sk);
256                         if (newsock)
257                                 sock_graft(sk, newsock);
258
259                         bh_unlock_sock(sk);
260                         local_bh_enable();
261                         return sk;
262                 }
263
264                 bh_unlock_sock(sk);
265         }
266         local_bh_enable();
267
268         return NULL;
269 }
270 EXPORT_SYMBOL(bt_accept_dequeue);
271
272 int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
273                                 struct msghdr *msg, size_t len, int flags)
274 {
275         int noblock = flags & MSG_DONTWAIT;
276         struct sock *sk = sock->sk;
277         struct sk_buff *skb;
278         size_t copied;
279         int err;
280
281         BT_DBG("sock %p sk %p len %zu", sock, sk, len);
282
283         if (flags & (MSG_OOB))
284                 return -EOPNOTSUPP;
285
286         skb = skb_recv_datagram(sk, flags, noblock, &err);
287         if (!skb) {
288                 if (sk->sk_shutdown & RCV_SHUTDOWN)
289                         return 0;
290                 return err;
291         }
292
293         msg->msg_namelen = 0;
294
295         copied = skb->len;
296         if (len < copied) {
297                 msg->msg_flags |= MSG_TRUNC;
298                 copied = len;
299         }
300
301         skb_reset_transport_header(skb);
302         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
303         if (err == 0)
304                 sock_recv_ts_and_drops(msg, sk, skb);
305
306         skb_free_datagram(sk, skb);
307
308         return err ? : copied;
309 }
310 EXPORT_SYMBOL(bt_sock_recvmsg);
311
312 static long bt_sock_data_wait(struct sock *sk, long timeo)
313 {
314         DECLARE_WAITQUEUE(wait, current);
315
316         add_wait_queue(sk_sleep(sk), &wait);
317         for (;;) {
318                 set_current_state(TASK_INTERRUPTIBLE);
319
320                 if (!skb_queue_empty(&sk->sk_receive_queue))
321                         break;
322
323                 if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
324                         break;
325
326                 if (signal_pending(current) || !timeo)
327                         break;
328
329                 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
330                 release_sock(sk);
331                 timeo = schedule_timeout(timeo);
332                 lock_sock(sk);
333                 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
334         }
335
336         __set_current_state(TASK_RUNNING);
337         remove_wait_queue(sk_sleep(sk), &wait);
338         return timeo;
339 }
340
341 int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
342                                struct msghdr *msg, size_t size, int flags)
343 {
344         struct sock *sk = sock->sk;
345         int err = 0;
346         size_t target, copied = 0;
347         long timeo;
348
349         if (flags & MSG_OOB)
350                 return -EOPNOTSUPP;
351
352         msg->msg_namelen = 0;
353
354         BT_DBG("sk %p size %zu", sk, size);
355
356         lock_sock(sk);
357
358         target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
359         timeo  = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
360
361         do {
362                 struct sk_buff *skb;
363                 int chunk;
364
365                 skb = skb_dequeue(&sk->sk_receive_queue);
366                 if (!skb) {
367                         if (copied >= target)
368                                 break;
369
370                         err = sock_error(sk);
371                         if (err)
372                                 break;
373                         if (sk->sk_shutdown & RCV_SHUTDOWN)
374                                 break;
375
376                         err = -EAGAIN;
377                         if (!timeo)
378                                 break;
379
380                         timeo = bt_sock_data_wait(sk, timeo);
381
382                         if (signal_pending(current)) {
383                                 err = sock_intr_errno(timeo);
384                                 goto out;
385                         }
386                         continue;
387                 }
388
389                 chunk = min_t(unsigned int, skb->len, size);
390                 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
391                         skb_queue_head(&sk->sk_receive_queue, skb);
392                         if (!copied)
393                                 copied = -EFAULT;
394                         break;
395                 }
396                 copied += chunk;
397                 size   -= chunk;
398
399                 sock_recv_ts_and_drops(msg, sk, skb);
400
401                 if (!(flags & MSG_PEEK)) {
402                         skb_pull(skb, chunk);
403                         if (skb->len) {
404                                 skb_queue_head(&sk->sk_receive_queue, skb);
405                                 break;
406                         }
407                         kfree_skb(skb);
408
409                 } else {
410                         /* put message back and return */
411                         skb_queue_head(&sk->sk_receive_queue, skb);
412                         break;
413                 }
414         } while (size);
415
416 out:
417         release_sock(sk);
418         return copied ? : err;
419 }
420 EXPORT_SYMBOL(bt_sock_stream_recvmsg);
421
422 static inline unsigned int bt_accept_poll(struct sock *parent)
423 {
424         struct list_head *p, *n;
425         struct sock *sk;
426
427         list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
428                 sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
429                 if (sk->sk_state == BT_CONNECTED ||
430                                         (bt_sk(parent)->defer_setup &&
431                                                 sk->sk_state == BT_CONNECT2))
432                         return POLLIN | POLLRDNORM;
433         }
434
435         return 0;
436 }
437
438 unsigned int bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait)
439 {
440         struct sock *sk = sock->sk;
441         unsigned int mask = 0;
442
443         BT_DBG("sock %p, sk %p", sock, sk);
444
445         poll_wait(file, sk_sleep(sk), wait);
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 |= POLLERR;
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 (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
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
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 static struct net_proto_family bt_sock_family_ops = {
563         .owner  = THIS_MODULE,
564         .family = PF_BLUETOOTH,
565         .create = bt_sock_create,
566 };
567
568 static int __init bt_init(void)
569 {
570         int err;
571
572         BT_INFO("Core ver %s", VERSION);
573
574         err = bt_sysfs_init();
575         if (err < 0)
576                 return err;
577
578         err = sock_register(&bt_sock_family_ops);
579         if (err < 0) {
580                 bt_sysfs_cleanup();
581                 return err;
582         }
583
584         BT_INFO("HCI device and connection manager initialized");
585
586         err = hci_sock_init();
587         if (err < 0)
588                 goto error;
589
590         err = l2cap_init();
591         if (err < 0)
592                 goto sock_err;
593
594         err = sco_init();
595         if (err < 0) {
596                 l2cap_exit();
597                 goto sock_err;
598         }
599
600         return 0;
601
602 sock_err:
603         hci_sock_cleanup();
604
605 error:
606         sock_unregister(PF_BLUETOOTH);
607         bt_sysfs_cleanup();
608
609         return err;
610 }
611
612 static void __exit bt_exit(void)
613 {
614
615         sco_exit();
616
617         l2cap_exit();
618
619         hci_sock_cleanup();
620
621         sock_unregister(PF_BLUETOOTH);
622
623         bt_sysfs_cleanup();
624 }
625
626 subsys_initcall(bt_init);
627 module_exit(bt_exit);
628
629 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
630 MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
631 MODULE_VERSION(VERSION);
632 MODULE_LICENSE("GPL");
633 MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);