4d16b40ec7ce425f78ca54a6b3f4b6755b4f1b43
[linux-2.6.git] / net / bluetooth / hci_sock.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 HCI sockets. */
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/major.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/fcntl.h>
38 #include <linux/init.h>
39 #include <linux/skbuff.h>
40 #include <linux/workqueue.h>
41 #include <linux/interrupt.h>
42 #include <linux/socket.h>
43 #include <linux/ioctl.h>
44 #include <net/sock.h>
45
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48 #include <asm/unaligned.h>
49
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52
53 #ifndef CONFIG_BT_HCI_SOCK_DEBUG
54 #undef  BT_DBG
55 #define BT_DBG(D...)
56 #endif
57
58 /* ----- HCI socket interface ----- */
59
60 static inline int hci_test_bit(int nr, void *addr)
61 {
62         return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
63 }
64
65 /* Security filter */
66 static struct hci_sec_filter hci_sec_filter = {
67         /* Packet types */
68         0x10,
69         /* Events */
70         { 0x1000d9fe, 0x0000300c },
71         /* Commands */
72         {
73                 { 0x0 },
74                 /* OGF_LINK_CTL */
75                 { 0xbe000006, 0x00000001, 0x0000, 0x00 },
76                 /* OGF_LINK_POLICY */
77                 { 0x00005200, 0x00000000, 0x0000, 0x00 },
78                 /* OGF_HOST_CTL */
79                 { 0xaab00200, 0x2b402aaa, 0x0154, 0x00 },
80                 /* OGF_INFO_PARAM */
81                 { 0x000002be, 0x00000000, 0x0000, 0x00 },
82                 /* OGF_STATUS_PARAM */
83                 { 0x000000ea, 0x00000000, 0x0000, 0x00 }
84         }
85 };
86
87 static struct bt_sock_list hci_sk_list = {
88         .lock = RW_LOCK_UNLOCKED
89 };
90
91 /* Send frame to RAW socket */
92 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
93 {
94         struct sock *sk;
95         struct hlist_node *node;
96
97         BT_DBG("hdev %p len %d", hdev, skb->len);
98
99         read_lock(&hci_sk_list.lock);
100         sk_for_each(sk, node, &hci_sk_list.head) {
101                 struct hci_filter *flt;
102                 struct sk_buff *nskb;
103
104                 if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
105                         continue;
106
107                 /* Don't send frame to the socket it came from */
108                 if (skb->sk == sk)
109                         continue;
110
111                 /* Apply filter */
112                 flt = &hci_pi(sk)->filter;
113
114                 if (!test_bit((skb->pkt_type & HCI_FLT_TYPE_BITS), &flt->type_mask))
115                         continue;
116
117                 if (skb->pkt_type == HCI_EVENT_PKT) {
118                         register int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
119
120                         if (!hci_test_bit(evt, &flt->event_mask))
121                                 continue;
122
123                         if (flt->opcode && ((evt == HCI_EV_CMD_COMPLETE && 
124                                         flt->opcode != *(__u16 *)(skb->data + 3)) ||
125                                         (evt == HCI_EV_CMD_STATUS && 
126                                         flt->opcode != *(__u16 *)(skb->data + 4))))
127                                 continue;
128                 }
129
130                 if (!(nskb = skb_clone(skb, GFP_ATOMIC)))
131                         continue;
132
133                 /* Put type byte before the data */
134                 memcpy(skb_push(nskb, 1), &nskb->pkt_type, 1);
135
136                 if (sock_queue_rcv_skb(sk, nskb))
137                         kfree_skb(nskb);
138         }
139         read_unlock(&hci_sk_list.lock);
140 }
141
142 static int hci_sock_release(struct socket *sock)
143 {
144         struct sock *sk = sock->sk;
145         struct hci_dev *hdev = hci_pi(sk)->hdev;
146
147         BT_DBG("sock %p sk %p", sock, sk);
148
149         if (!sk)
150                 return 0;
151
152         bt_sock_unlink(&hci_sk_list, sk);
153
154         if (hdev) {
155                 atomic_dec(&hdev->promisc);
156                 hci_dev_put(hdev);
157         }
158
159         sock_orphan(sk);
160
161         skb_queue_purge(&sk->sk_receive_queue);
162         skb_queue_purge(&sk->sk_write_queue);
163
164         sock_put(sk);
165         return 0;
166 }
167
168 /* Ioctls that require bound socket */ 
169 static inline int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
170 {
171         struct hci_dev *hdev = hci_pi(sk)->hdev;
172
173         if (!hdev)
174                 return -EBADFD;
175
176         switch (cmd) {
177         case HCISETRAW:
178                 if (!capable(CAP_NET_ADMIN))
179                         return -EACCES;
180
181                 if (arg)
182                         set_bit(HCI_RAW, &hdev->flags);
183                 else
184                         clear_bit(HCI_RAW, &hdev->flags);
185
186                 return 0;
187
188         case HCIGETCONNINFO:
189                 return hci_get_conn_info(hdev, arg);
190
191         default:
192                 if (hdev->ioctl)
193                         return hdev->ioctl(hdev, cmd, arg);
194                 return -EINVAL;
195         }
196 }
197
198 static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
199 {
200         struct sock *sk = sock->sk;
201         int err;
202
203         BT_DBG("cmd %x arg %lx", cmd, arg);
204
205         switch (cmd) {
206         case HCIGETDEVLIST:
207                 return hci_get_dev_list(arg);
208
209         case HCIGETDEVINFO:
210                 return hci_get_dev_info(arg);
211
212         case HCIGETCONNLIST:
213                 return hci_get_conn_list(arg);
214
215         case HCIDEVUP:
216                 if (!capable(CAP_NET_ADMIN))
217                         return -EACCES;
218                 return hci_dev_open(arg);
219
220         case HCIDEVDOWN:
221                 if (!capable(CAP_NET_ADMIN))
222                         return -EACCES;
223                 return hci_dev_close(arg);
224
225         case HCIDEVRESET:
226                 if (!capable(CAP_NET_ADMIN))
227                         return -EACCES;
228                 return hci_dev_reset(arg);
229
230         case HCIDEVRESTAT:
231                 if (!capable(CAP_NET_ADMIN))
232                         return -EACCES;
233                 return hci_dev_reset_stat(arg);
234
235         case HCISETSCAN:
236         case HCISETAUTH:
237         case HCISETENCRYPT:
238         case HCISETPTYPE:
239         case HCISETLINKPOL:
240         case HCISETLINKMODE:
241         case HCISETACLMTU:
242         case HCISETSCOMTU:
243                 if (!capable(CAP_NET_ADMIN))
244                         return -EACCES;
245                 return hci_dev_cmd(cmd, arg);
246
247         case HCIINQUIRY:
248                 return hci_inquiry(arg);
249
250         default:
251                 lock_sock(sk);
252                 err = hci_sock_bound_ioctl(sk, cmd, arg);
253                 release_sock(sk);
254                 return err;
255         }
256 }
257
258 static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
259 {
260         struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
261         struct sock *sk = sock->sk;
262         struct hci_dev *hdev = NULL;
263         int err = 0;
264
265         BT_DBG("sock %p sk %p", sock, sk);
266
267         if (!haddr || haddr->hci_family != AF_BLUETOOTH)
268                 return -EINVAL;
269
270         lock_sock(sk);
271
272         if (hci_pi(sk)->hdev) {
273                 err = -EALREADY;
274                 goto done;
275         }
276
277         if (haddr->hci_dev != HCI_DEV_NONE) {
278                 if (!(hdev = hci_dev_get(haddr->hci_dev))) {
279                         err = -ENODEV;
280                         goto done;
281                 }
282
283                 atomic_inc(&hdev->promisc);
284         }
285
286         hci_pi(sk)->hdev = hdev;
287         sk->sk_state = BT_BOUND;
288
289 done:
290         release_sock(sk);
291         return err;
292 }
293
294 static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
295 {
296         struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
297         struct sock *sk = sock->sk;
298
299         BT_DBG("sock %p sk %p", sock, sk);
300
301         lock_sock(sk);
302
303         *addr_len = sizeof(*haddr);
304         haddr->hci_family = AF_BLUETOOTH;
305         haddr->hci_dev    = hci_pi(sk)->hdev->id;
306
307         release_sock(sk);
308         return 0;
309 }
310
311 static inline void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
312 {
313         __u32 mask = hci_pi(sk)->cmsg_mask;
314
315         if (mask & HCI_CMSG_DIR)
316                 put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(int), &bt_cb(skb)->incoming);
317
318         if (mask & HCI_CMSG_TSTAMP)
319                 put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, sizeof(skb->stamp), &skb->stamp);
320 }
321  
322 static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 
323                                 struct msghdr *msg, size_t len, int flags)
324 {
325         int noblock = flags & MSG_DONTWAIT;
326         struct sock *sk = sock->sk;
327         struct sk_buff *skb;
328         int copied, err;
329
330         BT_DBG("sock %p, sk %p", sock, sk);
331
332         if (flags & (MSG_OOB))
333                 return -EOPNOTSUPP;
334
335         if (sk->sk_state == BT_CLOSED)
336                 return 0;
337
338         if (!(skb = skb_recv_datagram(sk, flags, noblock, &err)))
339                 return err;
340
341         msg->msg_namelen = 0;
342
343         copied = skb->len;
344         if (len < copied) {
345                 msg->msg_flags |= MSG_TRUNC;
346                 copied = len;
347         }
348
349         skb->h.raw = skb->data;
350         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
351
352         hci_sock_cmsg(sk, msg, skb);
353
354         skb_free_datagram(sk, skb);
355
356         return err ? : copied;
357 }
358
359 static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 
360                             struct msghdr *msg, size_t len)
361 {
362         struct sock *sk = sock->sk;
363         struct hci_dev *hdev;
364         struct sk_buff *skb;
365         int err;
366
367         BT_DBG("sock %p sk %p", sock, sk);
368
369         if (msg->msg_flags & MSG_OOB)
370                 return -EOPNOTSUPP;
371
372         if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
373                 return -EINVAL;
374
375         if (len < 4 || len > HCI_MAX_FRAME_SIZE)
376                 return -EINVAL;
377
378         lock_sock(sk);
379
380         if (!(hdev = hci_pi(sk)->hdev)) {
381                 err = -EBADFD;
382                 goto done;
383         }
384
385         if (!(skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err)))
386                 goto done;
387
388         if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
389                 err = -EFAULT;
390                 goto drop;
391         }
392
393         skb->pkt_type = *((unsigned char *) skb->data);
394         skb_pull(skb, 1);
395         skb->dev = (void *) hdev;
396
397         if (skb->pkt_type == HCI_COMMAND_PKT) {
398                 u16 opcode = __le16_to_cpu(get_unaligned((u16 *)skb->data));
399                 u16 ogf = hci_opcode_ogf(opcode);
400                 u16 ocf = hci_opcode_ocf(opcode);
401
402                 if (((ogf > HCI_SFLT_MAX_OGF) ||
403                                 !hci_test_bit(ocf & HCI_FLT_OCF_BITS, &hci_sec_filter.ocf_mask[ogf])) &&
404                                         !capable(CAP_NET_RAW)) {
405                         err = -EPERM;
406                         goto drop;
407                 }
408
409                 if (test_bit(HCI_RAW, &hdev->flags) || (ogf == OGF_VENDOR_CMD)) {
410                         skb_queue_tail(&hdev->raw_q, skb);
411                         hci_sched_tx(hdev);
412                 } else {
413                         skb_queue_tail(&hdev->cmd_q, skb);
414                         hci_sched_cmd(hdev);
415                 }
416         } else {
417                 if (!capable(CAP_NET_RAW)) {
418                         err = -EPERM;
419                         goto drop;
420                 }
421
422                 skb_queue_tail(&hdev->raw_q, skb);
423                 hci_sched_tx(hdev);
424         }
425
426         err = len;
427
428 done:
429         release_sock(sk);
430         return err;
431
432 drop:
433         kfree_skb(skb);
434         goto done;
435 }
436
437 int hci_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int len)
438 {
439         struct hci_ufilter uf = { .opcode = 0 };
440         struct sock *sk = sock->sk;
441         int err = 0, opt = 0;
442
443         BT_DBG("sk %p, opt %d", sk, optname);
444
445         lock_sock(sk);
446
447         switch (optname) {
448         case HCI_DATA_DIR:
449                 if (get_user(opt, (int __user *)optval)) {
450                         err = -EFAULT;
451                         break;
452                 }
453
454                 if (opt)
455                         hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
456                 else
457                         hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
458                 break;
459
460         case HCI_TIME_STAMP:
461                 if (get_user(opt, (int __user *)optval)) {
462                         err = -EFAULT;
463                         break;
464                 }
465
466                 if (opt)
467                         hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
468                 else
469                         hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
470                 break;
471
472         case HCI_FILTER:
473                 len = min_t(unsigned int, len, sizeof(uf));
474                 if (copy_from_user(&uf, optval, len)) {
475                         err = -EFAULT;
476                         break;
477                 }
478
479                 if (!capable(CAP_NET_RAW)) {
480                         uf.type_mask &= hci_sec_filter.type_mask;
481                         uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
482                         uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
483                 }
484
485                 {
486                         struct hci_filter *f = &hci_pi(sk)->filter;
487
488                         f->type_mask = uf.type_mask;
489                         f->opcode    = uf.opcode;
490                         *((u32 *) f->event_mask + 0) = uf.event_mask[0];
491                         *((u32 *) f->event_mask + 1) = uf.event_mask[1];
492                 }
493                 break; 
494
495         default:
496                 err = -ENOPROTOOPT;
497                 break;
498         }
499
500         release_sock(sk);
501         return err;
502 }
503
504 int hci_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
505 {
506         struct hci_ufilter uf;
507         struct sock *sk = sock->sk;
508         int len, opt; 
509
510         if (get_user(len, optlen))
511                 return -EFAULT;
512
513         switch (optname) {
514         case HCI_DATA_DIR:
515                 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
516                         opt = 1;
517                 else 
518                         opt = 0;
519
520                 if (put_user(opt, optval))
521                         return -EFAULT;
522                 break;
523
524         case HCI_TIME_STAMP:
525                 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
526                         opt = 1;
527                 else 
528                         opt = 0;
529
530                 if (put_user(opt, optval))
531                         return -EFAULT;
532                 break;
533
534         case HCI_FILTER:
535                 {
536                         struct hci_filter *f = &hci_pi(sk)->filter;
537
538                         uf.type_mask = f->type_mask;
539                         uf.opcode    = f->opcode;
540                         uf.event_mask[0] = *((u32 *) f->event_mask + 0);
541                         uf.event_mask[1] = *((u32 *) f->event_mask + 1);
542                 }
543
544                 len = min_t(unsigned int, len, sizeof(uf));
545                 if (copy_to_user(optval, &uf, len))
546                         return -EFAULT;
547                 break;
548
549         default:
550                 return -ENOPROTOOPT;
551                 break;
552         }
553
554         return 0;
555 }
556
557 struct proto_ops hci_sock_ops = {
558         .family         = PF_BLUETOOTH,
559         .owner          = THIS_MODULE,
560         .release        = hci_sock_release,
561         .bind           = hci_sock_bind,
562         .getname        = hci_sock_getname,
563         .sendmsg        = hci_sock_sendmsg,
564         .recvmsg        = hci_sock_recvmsg,
565         .ioctl          = hci_sock_ioctl,
566         .poll           = datagram_poll,
567         .listen         = sock_no_listen,
568         .shutdown       = sock_no_shutdown,
569         .setsockopt     = hci_sock_setsockopt,
570         .getsockopt     = hci_sock_getsockopt,
571         .connect        = sock_no_connect,
572         .socketpair     = sock_no_socketpair,
573         .accept         = sock_no_accept,
574         .mmap           = sock_no_mmap
575 };
576
577 static int hci_sock_create(struct socket *sock, int protocol)
578 {
579         struct sock *sk;
580
581         BT_DBG("sock %p", sock);
582
583         if (sock->type != SOCK_RAW)
584                 return -ESOCKTNOSUPPORT;
585
586         sock->ops = &hci_sock_ops;
587
588         sk = bt_sock_alloc(sock, protocol, sizeof(struct hci_pinfo), GFP_KERNEL);
589         if (!sk)
590                 return -ENOMEM;
591
592         sk_set_owner(sk, THIS_MODULE);
593
594         sock->state = SS_UNCONNECTED;
595         sk->sk_state = BT_OPEN;
596
597         bt_sock_link(&hci_sk_list, sk);
598         return 0;
599 }
600
601 static int hci_sock_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
602 {
603         struct hci_dev *hdev = (struct hci_dev *) ptr;
604         struct hci_ev_si_device ev;
605
606         BT_DBG("hdev %s event %ld", hdev->name, event);
607
608         /* Send event to sockets */
609         ev.event  = event;
610         ev.dev_id = hdev->id;
611         hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
612
613         if (event == HCI_DEV_UNREG) {
614                 struct sock *sk;
615                 struct hlist_node *node;
616
617                 /* Detach sockets from device */
618                 read_lock(&hci_sk_list.lock);
619                 sk_for_each(sk, node, &hci_sk_list.head) {
620                         bh_lock_sock(sk);
621                         if (hci_pi(sk)->hdev == hdev) {
622                                 hci_pi(sk)->hdev = NULL;
623                                 sk->sk_err = EPIPE;
624                                 sk->sk_state = BT_OPEN;
625                                 sk->sk_state_change(sk);
626
627                                 hci_dev_put(hdev);
628                         }
629                         bh_unlock_sock(sk);
630                 }
631                 read_unlock(&hci_sk_list.lock);
632         }
633
634         return NOTIFY_DONE;
635 }
636
637 struct net_proto_family hci_sock_family_ops = {
638         .family = PF_BLUETOOTH,
639         .owner  = THIS_MODULE,
640         .create = hci_sock_create,
641 };
642
643 struct notifier_block hci_sock_nblock = {
644         .notifier_call = hci_sock_dev_event
645 };
646
647 int __init hci_sock_init(void)
648 {
649         if (bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops)) {
650                 BT_ERR("HCI socket registration failed");
651                 return -EPROTO;
652         }
653
654         hci_register_notifier(&hci_sock_nblock);
655
656         BT_INFO("HCI socket layer initialized");
657
658         return 0;
659 }
660
661 int __exit hci_sock_cleanup(void)
662 {
663         if (bt_sock_unregister(BTPROTO_HCI))
664                 BT_ERR("HCI socket unregistration failed");
665
666         hci_unregister_notifier(&hci_sock_nblock);
667         return 0;
668 }