YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI sockets. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 30 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/errno.h> |
| 32 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 40 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/socket.h> |
| 42 | #include <linux/ioctl.h> |
| 43 | #include <net/sock.h> |
| 44 | |
| 45 | #include <asm/system.h> |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 46 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <asm/unaligned.h> |
| 48 | |
| 49 | #include <net/bluetooth/bluetooth.h> |
| 50 | #include <net/bluetooth/hci_core.h> |
| 51 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 52 | static bool enable_mgmt; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Marcel Holtmann | dd7f552 | 2005-10-28 19:20:53 +0200 | [diff] [blame] | 66 | { 0x1000d9fe, 0x0000b00c }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* Commands */ |
| 68 | { |
| 69 | { 0x0 }, |
| 70 | /* OGF_LINK_CTL */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 71 | { 0xbe000006, 0x00000001, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* OGF_LINK_POLICY */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 73 | { 0x00005200, 0x00000000, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* OGF_HOST_CTL */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 75 | { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* OGF_INFO_PARAM */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 77 | { 0x000002be, 0x00000000, 0x00000000, 0x00 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* OGF_STATUS_PARAM */ |
Marcel Holtmann | 7c631a6 | 2007-09-09 08:39:43 +0200 | [diff] [blame] | 79 | { 0x000000ea, 0x00000000, 0x00000000, 0x00 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
| 83 | static struct bt_sock_list hci_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 84 | .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* Send frame to RAW socket */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame^] | 88 | void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame^] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame^] | 108 | if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) |
Johan Hedberg | a40c406 | 2010-12-08 00:21:07 +0200 | [diff] [blame] | 109 | continue; |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* Apply filter */ |
| 112 | flt = &hci_pi(sk)->filter; |
| 113 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 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)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | continue; |
| 117 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 118 | if (bt_cb(skb)->pkt_type == HCI_EVENT_PKT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | register int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS); |
| 120 | |
| 121 | if (!hci_test_bit(evt, &flt->event_mask)) |
| 122 | continue; |
| 123 | |
David S. Miller | 4498c80 | 2006-11-21 16:17:41 -0800 | [diff] [blame] | 124 | if (flt->opcode && |
| 125 | ((evt == HCI_EV_CMD_COMPLETE && |
| 126 | flt->opcode != |
Al Viro | 905f3ed | 2006-12-13 00:35:01 -0800 | [diff] [blame] | 127 | get_unaligned((__le16 *)(skb->data + 3))) || |
David S. Miller | 4498c80 | 2006-11-21 16:17:41 -0800 | [diff] [blame] | 128 | (evt == HCI_EV_CMD_STATUS && |
| 129 | flt->opcode != |
Al Viro | 905f3ed | 2006-12-13 00:35:01 -0800 | [diff] [blame] | 130 | get_unaligned((__le16 *)(skb->data + 4))))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 134 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 135 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | continue; |
| 137 | |
| 138 | /* Put type byte before the data */ |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame^] | 139 | memcpy(skb_push(nskb, 1), &bt_cb(nskb)->pkt_type, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | if (sock_queue_rcv_skb(sk, nskb)) |
| 142 | kfree_skb(nskb); |
| 143 | } |
Marcel Holtmann | 470fe1b | 2012-02-20 14:50:30 +0100 | [diff] [blame^] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 185 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | BT_DBG("sock %p sk %p", sock, sk); |
| 188 | |
| 189 | if (!sk) |
| 190 | return 0; |
| 191 | |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 192 | hdev = hci_pi(sk)->hdev; |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 210 | static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg) |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 211 | { |
| 212 | bdaddr_t bdaddr; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 213 | int err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 214 | |
| 215 | if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) |
| 216 | return -EFAULT; |
| 217 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 218 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 219 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 220 | err = hci_blacklist_add(hdev, &bdaddr, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 221 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 222 | hci_dev_unlock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 223 | |
| 224 | return err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 227 | static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg) |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 228 | { |
| 229 | bdaddr_t bdaddr; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 230 | int err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 231 | |
| 232 | if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) |
| 233 | return -EFAULT; |
| 234 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 235 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 236 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 237 | err = hci_blacklist_del(hdev, &bdaddr, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 238 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 239 | hci_dev_unlock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 240 | |
| 241 | return err; |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 242 | } |
| 243 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 244 | /* Ioctls that require bound socket */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | case HCIGETCONNINFO: |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 268 | return hci_get_conn_info(hdev, (void __user *) arg); |
| 269 | |
| 270 | case HCIGETAUTHINFO: |
| 271 | return hci_get_auth_info(hdev, (void __user *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 273 | case HCIBLOCKADDR: |
| 274 | if (!capable(CAP_NET_ADMIN)) |
| 275 | return -EACCES; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 276 | return hci_sock_blacklist_add(hdev, (void __user *) arg); |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 277 | |
| 278 | case HCIUNBLOCKADDR: |
| 279 | if (!capable(CAP_NET_ADMIN)) |
| 280 | return -EACCES; |
Antti Julku | b2a66aa | 2011-06-15 12:01:14 +0300 | [diff] [blame] | 281 | return hci_sock_blacklist_del(hdev, (void __user *) arg); |
Johan Hedberg | f035856 | 2010-05-18 13:20:32 +0200 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 293 | void __user *argp = (void __user *) arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 353 | struct sockaddr_hci haddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | struct sock *sk = sock->sk; |
| 355 | struct hci_dev *hdev = NULL; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 356 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | BT_DBG("sock %p sk %p", sock, sk); |
| 359 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 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 | |
Gustavo F. Padovan | 17f9cc3 | 2010-12-22 23:00:34 -0200 | [diff] [blame] | 370 | if (haddr.hci_channel > HCI_CHANNEL_CONTROL) |
| 371 | return -EINVAL; |
| 372 | |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | lock_sock(sk); |
| 380 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 381 | if (sk->sk_state == BT_BOUND || hci_pi(sk)->hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | err = -EALREADY; |
| 383 | goto done; |
| 384 | } |
| 385 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 386 | if (haddr.hci_dev != HCI_DEV_NONE) { |
| 387 | hdev = hci_dev_get(haddr.hci_dev); |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 388 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | err = -ENODEV; |
| 390 | goto done; |
| 391 | } |
| 392 | |
| 393 | atomic_inc(&hdev->promisc); |
| 394 | } |
| 395 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 396 | hci_pi(sk)->channel = haddr.hci_channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 409 | struct hci_dev *hdev = hci_pi(sk)->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | BT_DBG("sock %p sk %p", sock, sk); |
| 412 | |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 413 | if (!hdev) |
| 414 | return -EBADFD; |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | lock_sock(sk); |
| 417 | |
| 418 | *addr_len = sizeof(*haddr); |
| 419 | haddr->hci_family = AF_BLUETOOTH; |
Marcel Holtmann | 7b005bd | 2006-02-13 11:40:03 +0100 | [diff] [blame] | 420 | haddr->hci_dev = hdev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 435 | if (mask & HCI_CMSG_TSTAMP) { |
Johann Felix Soden | f6e623a | 2010-02-15 22:23:48 +0100 | [diff] [blame] | 436 | #ifdef CONFIG_COMPAT |
| 437 | struct compat_timeval ctv; |
| 438 | #endif |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 439 | struct timeval tv; |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 440 | void *data; |
| 441 | int len; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 442 | |
| 443 | skb_get_timestamp(skb, &tv); |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 444 | |
David S. Miller | 1da97f8 | 2007-09-12 14:10:58 +0200 | [diff] [blame] | 445 | data = &tv; |
| 446 | len = sizeof(tv); |
| 447 | #ifdef CONFIG_COMPAT |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 448 | if (msg->msg_flags & MSG_CMSG_COMPAT) { |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 449 | ctv.tv_sec = tv.tv_sec; |
| 450 | ctv.tv_usec = tv.tv_usec; |
| 451 | data = &ctv; |
| 452 | len = sizeof(ctv); |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 453 | } |
David S. Miller | 1da97f8 | 2007-09-12 14:10:58 +0200 | [diff] [blame] | 454 | #endif |
Marcel Holtmann | 767c5eb | 2007-09-09 08:39:34 +0200 | [diff] [blame] | 455 | |
| 456 | put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data); |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 459 | |
| 460 | static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 476 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 477 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 488 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 490 | |
| 491 | hci_sock_cmsg(sk, msg, skb); |
| 492 | |
| 493 | skb_free_datagram(sk, skb); |
| 494 | |
| 495 | return err ? : copied; |
| 496 | } |
| 497 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 498 | static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | struct msghdr *msg, size_t len) |
| 500 | { |
| 501 | struct sock *sk = sock->sk; |
| 502 | struct hci_dev *hdev; |
| 503 | struct sk_buff *skb; |
| 504 | int err; |
| 505 | |
| 506 | BT_DBG("sock %p sk %p", sock, sk); |
| 507 | |
| 508 | if (msg->msg_flags & MSG_OOB) |
| 509 | return -EOPNOTSUPP; |
| 510 | |
| 511 | if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE)) |
| 512 | return -EINVAL; |
| 513 | |
| 514 | if (len < 4 || len > HCI_MAX_FRAME_SIZE) |
| 515 | return -EINVAL; |
| 516 | |
| 517 | lock_sock(sk); |
| 518 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 519 | switch (hci_pi(sk)->channel) { |
| 520 | case HCI_CHANNEL_RAW: |
| 521 | break; |
| 522 | case HCI_CHANNEL_CONTROL: |
| 523 | err = mgmt_control(sk, msg, len); |
| 524 | goto done; |
| 525 | default: |
| 526 | err = -EINVAL; |
| 527 | goto done; |
| 528 | } |
| 529 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 530 | hdev = hci_pi(sk)->hdev; |
| 531 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | err = -EBADFD; |
| 533 | goto done; |
| 534 | } |
| 535 | |
Marcel Holtmann | 7e21add | 2009-11-18 01:05:00 +0100 | [diff] [blame] | 536 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 537 | err = -ENETDOWN; |
| 538 | goto done; |
| 539 | } |
| 540 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 541 | skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); |
| 542 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | goto done; |
| 544 | |
| 545 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
| 546 | err = -EFAULT; |
| 547 | goto drop; |
| 548 | } |
| 549 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 550 | bt_cb(skb)->pkt_type = *((unsigned char *) skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | skb_pull(skb, 1); |
| 552 | skb->dev = (void *) hdev; |
| 553 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 554 | if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 555 | u16 opcode = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | u16 ogf = hci_opcode_ogf(opcode); |
| 557 | u16 ocf = hci_opcode_ocf(opcode); |
| 558 | |
| 559 | if (((ogf > HCI_SFLT_MAX_OGF) || |
| 560 | !hci_test_bit(ocf & HCI_FLT_OCF_BITS, &hci_sec_filter.ocf_mask[ogf])) && |
| 561 | !capable(CAP_NET_RAW)) { |
| 562 | err = -EPERM; |
| 563 | goto drop; |
| 564 | } |
| 565 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 566 | if (test_bit(HCI_RAW, &hdev->flags) || (ogf == 0x3f)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 568 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } else { |
| 570 | skb_queue_tail(&hdev->cmd_q, skb); |
Gustavo F. Padovan | c347b76 | 2011-12-14 23:53:47 -0200 | [diff] [blame] | 571 | queue_work(hdev->workqueue, &hdev->cmd_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | } else { |
| 574 | if (!capable(CAP_NET_RAW)) { |
| 575 | err = -EPERM; |
| 576 | goto drop; |
| 577 | } |
| 578 | |
| 579 | skb_queue_tail(&hdev->raw_q, skb); |
Gustavo F. Padovan | 3eff45e | 2011-12-15 00:50:02 -0200 | [diff] [blame] | 580 | queue_work(hdev->workqueue, &hdev->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | err = len; |
| 584 | |
| 585 | done: |
| 586 | release_sock(sk); |
| 587 | return err; |
| 588 | |
| 589 | drop: |
| 590 | kfree_skb(skb); |
| 591 | goto done; |
| 592 | } |
| 593 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 594 | static int hci_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
| 596 | struct hci_ufilter uf = { .opcode = 0 }; |
| 597 | struct sock *sk = sock->sk; |
| 598 | int err = 0, opt = 0; |
| 599 | |
| 600 | BT_DBG("sk %p, opt %d", sk, optname); |
| 601 | |
| 602 | lock_sock(sk); |
| 603 | |
| 604 | switch (optname) { |
| 605 | case HCI_DATA_DIR: |
| 606 | if (get_user(opt, (int __user *)optval)) { |
| 607 | err = -EFAULT; |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | if (opt) |
| 612 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR; |
| 613 | else |
| 614 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR; |
| 615 | break; |
| 616 | |
| 617 | case HCI_TIME_STAMP: |
| 618 | if (get_user(opt, (int __user *)optval)) { |
| 619 | err = -EFAULT; |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | if (opt) |
| 624 | hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP; |
| 625 | else |
| 626 | hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP; |
| 627 | break; |
| 628 | |
| 629 | case HCI_FILTER: |
Marcel Holtmann | 0878b66 | 2007-05-05 00:35:59 +0200 | [diff] [blame] | 630 | { |
| 631 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 632 | |
| 633 | uf.type_mask = f->type_mask; |
| 634 | uf.opcode = f->opcode; |
| 635 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 636 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 637 | } |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | len = min_t(unsigned int, len, sizeof(uf)); |
| 640 | if (copy_from_user(&uf, optval, len)) { |
| 641 | err = -EFAULT; |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | if (!capable(CAP_NET_RAW)) { |
| 646 | uf.type_mask &= hci_sec_filter.type_mask; |
| 647 | uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0); |
| 648 | uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1); |
| 649 | } |
| 650 | |
| 651 | { |
| 652 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 653 | |
| 654 | f->type_mask = uf.type_mask; |
| 655 | f->opcode = uf.opcode; |
| 656 | *((u32 *) f->event_mask + 0) = uf.event_mask[0]; |
| 657 | *((u32 *) f->event_mask + 1) = uf.event_mask[1]; |
| 658 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 659 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | default: |
| 662 | err = -ENOPROTOOPT; |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | release_sock(sk); |
| 667 | return err; |
| 668 | } |
| 669 | |
| 670 | static int hci_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 671 | { |
| 672 | struct hci_ufilter uf; |
| 673 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 674 | int len, opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | if (get_user(len, optlen)) |
| 677 | return -EFAULT; |
| 678 | |
| 679 | switch (optname) { |
| 680 | case HCI_DATA_DIR: |
| 681 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR) |
| 682 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 683 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | opt = 0; |
| 685 | |
| 686 | if (put_user(opt, optval)) |
| 687 | return -EFAULT; |
| 688 | break; |
| 689 | |
| 690 | case HCI_TIME_STAMP: |
| 691 | if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP) |
| 692 | opt = 1; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 693 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | opt = 0; |
| 695 | |
| 696 | if (put_user(opt, optval)) |
| 697 | return -EFAULT; |
| 698 | break; |
| 699 | |
| 700 | case HCI_FILTER: |
| 701 | { |
| 702 | struct hci_filter *f = &hci_pi(sk)->filter; |
| 703 | |
| 704 | uf.type_mask = f->type_mask; |
| 705 | uf.opcode = f->opcode; |
| 706 | uf.event_mask[0] = *((u32 *) f->event_mask + 0); |
| 707 | uf.event_mask[1] = *((u32 *) f->event_mask + 1); |
| 708 | } |
| 709 | |
| 710 | len = min_t(unsigned int, len, sizeof(uf)); |
| 711 | if (copy_to_user(optval, &uf, len)) |
| 712 | return -EFAULT; |
| 713 | break; |
| 714 | |
| 715 | default: |
| 716 | return -ENOPROTOOPT; |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 723 | static const struct proto_ops hci_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | .family = PF_BLUETOOTH, |
| 725 | .owner = THIS_MODULE, |
| 726 | .release = hci_sock_release, |
| 727 | .bind = hci_sock_bind, |
| 728 | .getname = hci_sock_getname, |
| 729 | .sendmsg = hci_sock_sendmsg, |
| 730 | .recvmsg = hci_sock_recvmsg, |
| 731 | .ioctl = hci_sock_ioctl, |
| 732 | .poll = datagram_poll, |
| 733 | .listen = sock_no_listen, |
| 734 | .shutdown = sock_no_shutdown, |
| 735 | .setsockopt = hci_sock_setsockopt, |
| 736 | .getsockopt = hci_sock_getsockopt, |
| 737 | .connect = sock_no_connect, |
| 738 | .socketpair = sock_no_socketpair, |
| 739 | .accept = sock_no_accept, |
| 740 | .mmap = sock_no_mmap |
| 741 | }; |
| 742 | |
| 743 | static struct proto hci_sk_proto = { |
| 744 | .name = "HCI", |
| 745 | .owner = THIS_MODULE, |
| 746 | .obj_size = sizeof(struct hci_pinfo) |
| 747 | }; |
| 748 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 749 | static int hci_sock_create(struct net *net, struct socket *sock, int protocol, |
| 750 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
| 752 | struct sock *sk; |
| 753 | |
| 754 | BT_DBG("sock %p", sock); |
| 755 | |
| 756 | if (sock->type != SOCK_RAW) |
| 757 | return -ESOCKTNOSUPPORT; |
| 758 | |
| 759 | sock->ops = &hci_sock_ops; |
| 760 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 761 | sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | if (!sk) |
| 763 | return -ENOMEM; |
| 764 | |
| 765 | sock_init_data(sock, sk); |
| 766 | |
| 767 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 768 | |
| 769 | sk->sk_protocol = protocol; |
| 770 | |
| 771 | sock->state = SS_UNCONNECTED; |
| 772 | sk->sk_state = BT_OPEN; |
| 773 | |
| 774 | bt_sock_link(&hci_sk_list, sk); |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static int hci_sock_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 779 | { |
| 780 | struct hci_dev *hdev = (struct hci_dev *) ptr; |
| 781 | struct hci_ev_si_device ev; |
| 782 | |
| 783 | BT_DBG("hdev %s event %ld", hdev->name, event); |
| 784 | |
| 785 | /* Send event to sockets */ |
| 786 | ev.event = event; |
| 787 | ev.dev_id = hdev->id; |
| 788 | hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev); |
| 789 | |
| 790 | if (event == HCI_DEV_UNREG) { |
| 791 | struct sock *sk; |
| 792 | struct hlist_node *node; |
| 793 | |
| 794 | /* Detach sockets from device */ |
| 795 | read_lock(&hci_sk_list.lock); |
| 796 | sk_for_each(sk, node, &hci_sk_list.head) { |
Satyam Sharma | 4ce61d1 | 2007-05-16 23:50:16 -0700 | [diff] [blame] | 797 | bh_lock_sock_nested(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (hci_pi(sk)->hdev == hdev) { |
| 799 | hci_pi(sk)->hdev = NULL; |
| 800 | sk->sk_err = EPIPE; |
| 801 | sk->sk_state = BT_OPEN; |
| 802 | sk->sk_state_change(sk); |
| 803 | |
| 804 | hci_dev_put(hdev); |
| 805 | } |
Satyam Sharma | 4ce61d1 | 2007-05-16 23:50:16 -0700 | [diff] [blame] | 806 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
| 808 | read_unlock(&hci_sk_list.lock); |
| 809 | } |
| 810 | |
| 811 | return NOTIFY_DONE; |
| 812 | } |
| 813 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 814 | static const struct net_proto_family hci_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | .family = PF_BLUETOOTH, |
| 816 | .owner = THIS_MODULE, |
| 817 | .create = hci_sock_create, |
| 818 | }; |
| 819 | |
| 820 | static struct notifier_block hci_sock_nblock = { |
| 821 | .notifier_call = hci_sock_dev_event |
| 822 | }; |
| 823 | |
| 824 | int __init hci_sock_init(void) |
| 825 | { |
| 826 | int err; |
| 827 | |
| 828 | err = proto_register(&hci_sk_proto, 0); |
| 829 | if (err < 0) |
| 830 | return err; |
| 831 | |
| 832 | err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops); |
| 833 | if (err < 0) |
| 834 | goto error; |
| 835 | |
| 836 | hci_register_notifier(&hci_sock_nblock); |
| 837 | |
| 838 | BT_INFO("HCI socket layer initialized"); |
| 839 | |
| 840 | return 0; |
| 841 | |
| 842 | error: |
| 843 | BT_ERR("HCI socket registration failed"); |
| 844 | proto_unregister(&hci_sk_proto); |
| 845 | return err; |
| 846 | } |
| 847 | |
Anand Gadiyar | b7440a14 | 2011-02-22 12:43:09 +0530 | [diff] [blame] | 848 | void hci_sock_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
| 850 | if (bt_sock_unregister(BTPROTO_HCI) < 0) |
| 851 | BT_ERR("HCI socket unregistration failed"); |
| 852 | |
| 853 | hci_unregister_notifier(&hci_sock_nblock); |
| 854 | |
| 855 | proto_unregister(&hci_sk_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 857 | |
| 858 | module_param(enable_mgmt, bool, 0644); |
| 859 | MODULE_PARM_DESC(enable_mgmt, "Enable Management interface"); |