Bluetooth: Add HCI CMSG details only to raw sockets
[deliverable/linux.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/module.h>
28
29 #include <linux/types.h>
30 #include <linux/capability.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/skbuff.h>
38 #include <linux/workqueue.h>
39 #include <linux/interrupt.h>
40 #include <linux/compat.h>
41 #include <linux/socket.h>
42 #include <linux/ioctl.h>
43 #include <net/sock.h>
44
45 #include <asm/system.h>
46 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
48
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
51
52 static bool enable_mgmt;
53
54 /* ----- HCI socket interface ----- */
55
56 static inline int hci_test_bit(int nr, void *addr)
57 {
58 return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
59 }
60
61 /* Security filter */
62 static struct hci_sec_filter hci_sec_filter = {
63 /* Packet types */
64 0x10,
65 /* Events */
66 { 0x1000d9fe, 0x0000b00c },
67 /* Commands */
68 {
69 { 0x0 },
70 /* OGF_LINK_CTL */
71 { 0xbe000006, 0x00000001, 0x00000000, 0x00 },
72 /* OGF_LINK_POLICY */
73 { 0x00005200, 0x00000000, 0x00000000, 0x00 },
74 /* OGF_HOST_CTL */
75 { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
76 /* OGF_INFO_PARAM */
77 { 0x000002be, 0x00000000, 0x00000000, 0x00 },
78 /* OGF_STATUS_PARAM */
79 { 0x000000ea, 0x00000000, 0x00000000, 0x00 }
80 }
81 };
82
83 static struct bt_sock_list hci_sk_list = {
84 .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
85 };
86
87 /* Send frame to RAW socket */
88 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
89 {
90 struct sock *sk;
91 struct hlist_node *node;
92
93 BT_DBG("hdev %p len %d", hdev, skb->len);
94
95 read_lock(&hci_sk_list.lock);
96
97 sk_for_each(sk, node, &hci_sk_list.head) {
98 struct hci_filter *flt;
99 struct sk_buff *nskb;
100
101 if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
102 continue;
103
104 /* Don't send frame to the socket it came from */
105 if (skb->sk == sk)
106 continue;
107
108 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW)
109 continue;
110
111 /* Apply filter */
112 flt = &hci_pi(sk)->filter;
113
114 if (!test_bit((bt_cb(skb)->pkt_type == HCI_VENDOR_PKT) ?
115 0 : (bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS), &flt->type_mask))
116 continue;
117
118 if (bt_cb(skb)->pkt_type == HCI_EVENT_PKT) {
119 register int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
120
121 if (!hci_test_bit(evt, &flt->event_mask))
122 continue;
123
124 if (flt->opcode &&
125 ((evt == HCI_EV_CMD_COMPLETE &&
126 flt->opcode !=
127 get_unaligned((__le16 *)(skb->data + 3))) ||
128 (evt == HCI_EV_CMD_STATUS &&
129 flt->opcode !=
130 get_unaligned((__le16 *)(skb->data + 4)))))
131 continue;
132 }
133
134 nskb = skb_clone(skb, GFP_ATOMIC);
135 if (!nskb)
136 continue;
137
138 /* Put type byte before the data */
139 memcpy(skb_push(nskb, 1), &bt_cb(nskb)->pkt_type, 1);
140
141 if (sock_queue_rcv_skb(sk, nskb))
142 kfree_skb(nskb);
143 }
144
145 read_unlock(&hci_sk_list.lock);
146 }
147
148 /* Send frame to control socket */
149 void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk)
150 {
151 struct sock *sk;
152 struct hlist_node *node;
153
154 BT_DBG("len %d", skb->len);
155
156 read_lock(&hci_sk_list.lock);
157
158 sk_for_each(sk, node, &hci_sk_list.head) {
159 struct sk_buff *nskb;
160
161 /* Skip the original socket */
162 if (sk == skip_sk)
163 continue;
164
165 if (sk->sk_state != BT_BOUND)
166 continue;
167
168 if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
169 continue;
170
171 nskb = skb_clone(skb, GFP_ATOMIC);
172 if (!nskb)
173 continue;
174
175 if (sock_queue_rcv_skb(sk, nskb))
176 kfree_skb(nskb);
177 }
178
179 read_unlock(&hci_sk_list.lock);
180 }
181
182 static int hci_sock_release(struct socket *sock)
183 {
184 struct sock *sk = sock->sk;
185 struct hci_dev *hdev;
186
187 BT_DBG("sock %p sk %p", sock, sk);
188
189 if (!sk)
190 return 0;
191
192 hdev = hci_pi(sk)->hdev;
193
194 bt_sock_unlink(&hci_sk_list, sk);
195
196 if (hdev) {
197 atomic_dec(&hdev->promisc);
198 hci_dev_put(hdev);
199 }
200
201 sock_orphan(sk);
202
203 skb_queue_purge(&sk->sk_receive_queue);
204 skb_queue_purge(&sk->sk_write_queue);
205
206 sock_put(sk);
207 return 0;
208 }
209
210 static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
211 {
212 bdaddr_t bdaddr;
213 int err;
214
215 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
216 return -EFAULT;
217
218 hci_dev_lock(hdev);
219
220 err = hci_blacklist_add(hdev, &bdaddr, 0);
221
222 hci_dev_unlock(hdev);
223
224 return err;
225 }
226
227 static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
228 {
229 bdaddr_t bdaddr;
230 int err;
231
232 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
233 return -EFAULT;
234
235 hci_dev_lock(hdev);
236
237 err = hci_blacklist_del(hdev, &bdaddr, 0);
238
239 hci_dev_unlock(hdev);
240
241 return err;
242 }
243
244 /* Ioctls that require bound socket */
245 static inline int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
246 {
247 struct hci_dev *hdev = hci_pi(sk)->hdev;
248
249 if (!hdev)
250 return -EBADFD;
251
252 switch (cmd) {
253 case HCISETRAW:
254 if (!capable(CAP_NET_ADMIN))
255 return -EACCES;
256
257 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
258 return -EPERM;
259
260 if (arg)
261 set_bit(HCI_RAW, &hdev->flags);
262 else
263 clear_bit(HCI_RAW, &hdev->flags);
264
265 return 0;
266
267 case HCIGETCONNINFO:
268 return hci_get_conn_info(hdev, (void __user *) arg);
269
270 case HCIGETAUTHINFO:
271 return hci_get_auth_info(hdev, (void __user *) arg);
272
273 case HCIBLOCKADDR:
274 if (!capable(CAP_NET_ADMIN))
275 return -EACCES;
276 return hci_sock_blacklist_add(hdev, (void __user *) arg);
277
278 case HCIUNBLOCKADDR:
279 if (!capable(CAP_NET_ADMIN))
280 return -EACCES;
281 return hci_sock_blacklist_del(hdev, (void __user *) arg);
282
283 default:
284 if (hdev->ioctl)
285 return hdev->ioctl(hdev, cmd, arg);
286 return -EINVAL;
287 }
288 }
289
290 static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
291 {
292 struct sock *sk = sock->sk;
293 void __user *argp = (void __user *) arg;
294 int err;
295
296 BT_DBG("cmd %x arg %lx", cmd, arg);
297
298 switch (cmd) {
299 case HCIGETDEVLIST:
300 return hci_get_dev_list(argp);
301
302 case HCIGETDEVINFO:
303 return hci_get_dev_info(argp);
304
305 case HCIGETCONNLIST:
306 return hci_get_conn_list(argp);
307
308 case HCIDEVUP:
309 if (!capable(CAP_NET_ADMIN))
310 return -EACCES;
311 return hci_dev_open(arg);
312
313 case HCIDEVDOWN:
314 if (!capable(CAP_NET_ADMIN))
315 return -EACCES;
316 return hci_dev_close(arg);
317
318 case HCIDEVRESET:
319 if (!capable(CAP_NET_ADMIN))
320 return -EACCES;
321 return hci_dev_reset(arg);
322
323 case HCIDEVRESTAT:
324 if (!capable(CAP_NET_ADMIN))
325 return -EACCES;
326 return hci_dev_reset_stat(arg);
327
328 case HCISETSCAN:
329 case HCISETAUTH:
330 case HCISETENCRYPT:
331 case HCISETPTYPE:
332 case HCISETLINKPOL:
333 case HCISETLINKMODE:
334 case HCISETACLMTU:
335 case HCISETSCOMTU:
336 if (!capable(CAP_NET_ADMIN))
337 return -EACCES;
338 return hci_dev_cmd(cmd, argp);
339
340 case HCIINQUIRY:
341 return hci_inquiry(argp);
342
343 default:
344 lock_sock(sk);
345 err = hci_sock_bound_ioctl(sk, cmd, arg);
346 release_sock(sk);
347 return err;
348 }
349 }
350
351 static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
352 {
353 struct sockaddr_hci haddr;
354 struct sock *sk = sock->sk;
355 struct hci_dev *hdev = NULL;
356 int len, err = 0;
357
358 BT_DBG("sock %p sk %p", sock, sk);
359
360 if (!addr)
361 return -EINVAL;
362
363 memset(&haddr, 0, sizeof(haddr));
364 len = min_t(unsigned int, sizeof(haddr), addr_len);
365 memcpy(&haddr, addr, len);
366
367 if (haddr.hci_family != AF_BLUETOOTH)
368 return -EINVAL;
369
370 if (haddr.hci_channel > HCI_CHANNEL_CONTROL)
371 return -EINVAL;
372
373 if (haddr.hci_channel == HCI_CHANNEL_CONTROL) {
374 if (!enable_mgmt)
375 return -EINVAL;
376 set_bit(HCI_PI_MGMT_INIT, &hci_pi(sk)->flags);
377 }
378
379 lock_sock(sk);
380
381 if (sk->sk_state == BT_BOUND || hci_pi(sk)->hdev) {
382 err = -EALREADY;
383 goto done;
384 }
385
386 if (haddr.hci_dev != HCI_DEV_NONE) {
387 hdev = hci_dev_get(haddr.hci_dev);
388 if (!hdev) {
389 err = -ENODEV;
390 goto done;
391 }
392
393 atomic_inc(&hdev->promisc);
394 }
395
396 hci_pi(sk)->channel = haddr.hci_channel;
397 hci_pi(sk)->hdev = hdev;
398 sk->sk_state = BT_BOUND;
399
400 done:
401 release_sock(sk);
402 return err;
403 }
404
405 static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
406 {
407 struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
408 struct sock *sk = sock->sk;
409 struct hci_dev *hdev = hci_pi(sk)->hdev;
410
411 BT_DBG("sock %p sk %p", sock, sk);
412
413 if (!hdev)
414 return -EBADFD;
415
416 lock_sock(sk);
417
418 *addr_len = sizeof(*haddr);
419 haddr->hci_family = AF_BLUETOOTH;
420 haddr->hci_dev = hdev->id;
421
422 release_sock(sk);
423 return 0;
424 }
425
426 static inline void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
427 {
428 __u32 mask = hci_pi(sk)->cmsg_mask;
429
430 if (mask & HCI_CMSG_DIR) {
431 int incoming = bt_cb(skb)->incoming;
432 put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), &incoming);
433 }
434
435 if (mask & HCI_CMSG_TSTAMP) {
436 #ifdef CONFIG_COMPAT
437 struct compat_timeval ctv;
438 #endif
439 struct timeval tv;
440 void *data;
441 int len;
442
443 skb_get_timestamp(skb, &tv);
444
445 data = &tv;
446 len = sizeof(tv);
447 #ifdef CONFIG_COMPAT
448 if (msg->msg_flags & MSG_CMSG_COMPAT) {
449 ctv.tv_sec = tv.tv_sec;
450 ctv.tv_usec = tv.tv_usec;
451 data = &ctv;
452 len = sizeof(ctv);
453 }
454 #endif
455
456 put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
457 }
458 }
459
460 static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
461 struct msghdr *msg, size_t len, int flags)
462 {
463 int noblock = flags & MSG_DONTWAIT;
464 struct sock *sk = sock->sk;
465 struct sk_buff *skb;
466 int copied, err;
467
468 BT_DBG("sock %p, sk %p", sock, sk);
469
470 if (flags & (MSG_OOB))
471 return -EOPNOTSUPP;
472
473 if (sk->sk_state == BT_CLOSED)
474 return 0;
475
476 skb = skb_recv_datagram(sk, flags, noblock, &err);
477 if (!skb)
478 return err;
479
480 msg->msg_namelen = 0;
481
482 copied = skb->len;
483 if (len < copied) {
484 msg->msg_flags |= MSG_TRUNC;
485 copied = len;
486 }
487
488 skb_reset_transport_header(skb);
489 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
490
491 switch (hci_pi(sk)->channel) {
492 case HCI_CHANNEL_RAW:
493 hci_sock_cmsg(sk, msg, skb);
494 break;
495 }
496
497 skb_free_datagram(sk, skb);
498
499 return err ? : copied;
500 }
501
502 static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
503 struct msghdr *msg, size_t len)
504 {
505 struct sock *sk = sock->sk;
506 struct hci_dev *hdev;
507 struct sk_buff *skb;
508 int err;
509
510 BT_DBG("sock %p sk %p", sock, sk);
511
512 if (msg->msg_flags & MSG_OOB)
513 return -EOPNOTSUPP;
514
515 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
516 return -EINVAL;
517
518 if (len < 4 || len > HCI_MAX_FRAME_SIZE)
519 return -EINVAL;
520
521 lock_sock(sk);
522
523 switch (hci_pi(sk)->channel) {
524 case HCI_CHANNEL_RAW:
525 break;
526 case HCI_CHANNEL_CONTROL:
527 err = mgmt_control(sk, msg, len);
528 goto done;
529 default:
530 err = -EINVAL;
531 goto done;
532 }
533
534 hdev = hci_pi(sk)->hdev;
535 if (!hdev) {
536 err = -EBADFD;
537 goto done;
538 }
539
540 if (!test_bit(HCI_UP, &hdev->flags)) {
541 err = -ENETDOWN;
542 goto done;
543 }
544
545 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
546 if (!skb)
547 goto done;
548
549 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
550 err = -EFAULT;
551 goto drop;
552 }
553
554 bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
555 skb_pull(skb, 1);
556 skb->dev = (void *) hdev;
557
558 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
559 u16 opcode = get_unaligned_le16(skb->data);
560 u16 ogf = hci_opcode_ogf(opcode);
561 u16 ocf = hci_opcode_ocf(opcode);
562
563 if (((ogf > HCI_SFLT_MAX_OGF) ||
564 !hci_test_bit(ocf & HCI_FLT_OCF_BITS, &hci_sec_filter.ocf_mask[ogf])) &&
565 !capable(CAP_NET_RAW)) {
566 err = -EPERM;
567 goto drop;
568 }
569
570 if (test_bit(HCI_RAW, &hdev->flags) || (ogf == 0x3f)) {
571 skb_queue_tail(&hdev->raw_q, skb);
572 queue_work(hdev->workqueue, &hdev->tx_work);
573 } else {
574 skb_queue_tail(&hdev->cmd_q, skb);
575 queue_work(hdev->workqueue, &hdev->cmd_work);
576 }
577 } else {
578 if (!capable(CAP_NET_RAW)) {
579 err = -EPERM;
580 goto drop;
581 }
582
583 skb_queue_tail(&hdev->raw_q, skb);
584 queue_work(hdev->workqueue, &hdev->tx_work);
585 }
586
587 err = len;
588
589 done:
590 release_sock(sk);
591 return err;
592
593 drop:
594 kfree_skb(skb);
595 goto done;
596 }
597
598 static int hci_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int len)
599 {
600 struct hci_ufilter uf = { .opcode = 0 };
601 struct sock *sk = sock->sk;
602 int err = 0, opt = 0;
603
604 BT_DBG("sk %p, opt %d", sk, optname);
605
606 lock_sock(sk);
607
608 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
609 err = -EINVAL;
610 goto done;
611 }
612
613 switch (optname) {
614 case HCI_DATA_DIR:
615 if (get_user(opt, (int __user *)optval)) {
616 err = -EFAULT;
617 break;
618 }
619
620 if (opt)
621 hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
622 else
623 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
624 break;
625
626 case HCI_TIME_STAMP:
627 if (get_user(opt, (int __user *)optval)) {
628 err = -EFAULT;
629 break;
630 }
631
632 if (opt)
633 hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
634 else
635 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
636 break;
637
638 case HCI_FILTER:
639 {
640 struct hci_filter *f = &hci_pi(sk)->filter;
641
642 uf.type_mask = f->type_mask;
643 uf.opcode = f->opcode;
644 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
645 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
646 }
647
648 len = min_t(unsigned int, len, sizeof(uf));
649 if (copy_from_user(&uf, optval, len)) {
650 err = -EFAULT;
651 break;
652 }
653
654 if (!capable(CAP_NET_RAW)) {
655 uf.type_mask &= hci_sec_filter.type_mask;
656 uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
657 uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
658 }
659
660 {
661 struct hci_filter *f = &hci_pi(sk)->filter;
662
663 f->type_mask = uf.type_mask;
664 f->opcode = uf.opcode;
665 *((u32 *) f->event_mask + 0) = uf.event_mask[0];
666 *((u32 *) f->event_mask + 1) = uf.event_mask[1];
667 }
668 break;
669
670 default:
671 err = -ENOPROTOOPT;
672 break;
673 }
674
675 done:
676 release_sock(sk);
677 return err;
678 }
679
680 static int hci_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
681 {
682 struct hci_ufilter uf;
683 struct sock *sk = sock->sk;
684 int len, opt, err = 0;
685
686 BT_DBG("sk %p, opt %d", sk, optname);
687
688 if (get_user(len, optlen))
689 return -EFAULT;
690
691 lock_sock(sk);
692
693 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
694 err = -EINVAL;
695 goto done;
696 }
697
698 switch (optname) {
699 case HCI_DATA_DIR:
700 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
701 opt = 1;
702 else
703 opt = 0;
704
705 if (put_user(opt, optval))
706 err = -EFAULT;
707 break;
708
709 case HCI_TIME_STAMP:
710 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
711 opt = 1;
712 else
713 opt = 0;
714
715 if (put_user(opt, optval))
716 err = -EFAULT;
717 break;
718
719 case HCI_FILTER:
720 {
721 struct hci_filter *f = &hci_pi(sk)->filter;
722
723 uf.type_mask = f->type_mask;
724 uf.opcode = f->opcode;
725 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
726 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
727 }
728
729 len = min_t(unsigned int, len, sizeof(uf));
730 if (copy_to_user(optval, &uf, len))
731 err = -EFAULT;
732 break;
733
734 default:
735 err = -ENOPROTOOPT;
736 break;
737 }
738
739 done:
740 release_sock(sk);
741 return err;
742 }
743
744 static const struct proto_ops hci_sock_ops = {
745 .family = PF_BLUETOOTH,
746 .owner = THIS_MODULE,
747 .release = hci_sock_release,
748 .bind = hci_sock_bind,
749 .getname = hci_sock_getname,
750 .sendmsg = hci_sock_sendmsg,
751 .recvmsg = hci_sock_recvmsg,
752 .ioctl = hci_sock_ioctl,
753 .poll = datagram_poll,
754 .listen = sock_no_listen,
755 .shutdown = sock_no_shutdown,
756 .setsockopt = hci_sock_setsockopt,
757 .getsockopt = hci_sock_getsockopt,
758 .connect = sock_no_connect,
759 .socketpair = sock_no_socketpair,
760 .accept = sock_no_accept,
761 .mmap = sock_no_mmap
762 };
763
764 static struct proto hci_sk_proto = {
765 .name = "HCI",
766 .owner = THIS_MODULE,
767 .obj_size = sizeof(struct hci_pinfo)
768 };
769
770 static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
771 int kern)
772 {
773 struct sock *sk;
774
775 BT_DBG("sock %p", sock);
776
777 if (sock->type != SOCK_RAW)
778 return -ESOCKTNOSUPPORT;
779
780 sock->ops = &hci_sock_ops;
781
782 sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto);
783 if (!sk)
784 return -ENOMEM;
785
786 sock_init_data(sock, sk);
787
788 sock_reset_flag(sk, SOCK_ZAPPED);
789
790 sk->sk_protocol = protocol;
791
792 sock->state = SS_UNCONNECTED;
793 sk->sk_state = BT_OPEN;
794
795 bt_sock_link(&hci_sk_list, sk);
796 return 0;
797 }
798
799 static int hci_sock_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
800 {
801 struct hci_dev *hdev = (struct hci_dev *) ptr;
802 struct hci_ev_si_device ev;
803
804 BT_DBG("hdev %s event %ld", hdev->name, event);
805
806 /* Send event to sockets */
807 ev.event = event;
808 ev.dev_id = hdev->id;
809 hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
810
811 if (event == HCI_DEV_UNREG) {
812 struct sock *sk;
813 struct hlist_node *node;
814
815 /* Detach sockets from device */
816 read_lock(&hci_sk_list.lock);
817 sk_for_each(sk, node, &hci_sk_list.head) {
818 bh_lock_sock_nested(sk);
819 if (hci_pi(sk)->hdev == hdev) {
820 hci_pi(sk)->hdev = NULL;
821 sk->sk_err = EPIPE;
822 sk->sk_state = BT_OPEN;
823 sk->sk_state_change(sk);
824
825 hci_dev_put(hdev);
826 }
827 bh_unlock_sock(sk);
828 }
829 read_unlock(&hci_sk_list.lock);
830 }
831
832 return NOTIFY_DONE;
833 }
834
835 static const struct net_proto_family hci_sock_family_ops = {
836 .family = PF_BLUETOOTH,
837 .owner = THIS_MODULE,
838 .create = hci_sock_create,
839 };
840
841 static struct notifier_block hci_sock_nblock = {
842 .notifier_call = hci_sock_dev_event
843 };
844
845 int __init hci_sock_init(void)
846 {
847 int err;
848
849 err = proto_register(&hci_sk_proto, 0);
850 if (err < 0)
851 return err;
852
853 err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
854 if (err < 0)
855 goto error;
856
857 hci_register_notifier(&hci_sock_nblock);
858
859 BT_INFO("HCI socket layer initialized");
860
861 return 0;
862
863 error:
864 BT_ERR("HCI socket registration failed");
865 proto_unregister(&hci_sk_proto);
866 return err;
867 }
868
869 void hci_sock_cleanup(void)
870 {
871 if (bt_sock_unregister(BTPROTO_HCI) < 0)
872 BT_ERR("HCI socket unregistration failed");
873
874 hci_unregister_notifier(&hci_sock_nblock);
875
876 proto_unregister(&hci_sk_proto);
877 }
878
879 module_param(enable_mgmt, bool, 0644);
880 MODULE_PARM_DESC(enable_mgmt, "Enable Management interface");
This page took 0.054357 seconds and 6 git commands to generate.