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