blob: bdf51d1d2c191b6f79af3058c8c23f88d52e022a [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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 Hideaki8e87d142007-02-09 23:24:33 +090015 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 Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI sockets. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080030#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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 Holtmann767c5eb2007-09-09 08:39:34 +020040#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/socket.h>
42#include <linux/ioctl.h>
43#include <net/sock.h>
44
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/unaligned.h>
47
48#include <net/bluetooth/bluetooth.h>
49#include <net/bluetooth/hci_core.h>
Marcel Holtmanncd82e612012-02-20 20:34:38 +010050#include <net/bluetooth/hci_mon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Marcel Holtmanncd82e612012-02-20 20:34:38 +010052static atomic_t monitor_promisc = ATOMIC_INIT(0);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* ----- HCI socket interface ----- */
55
56static 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 */
62static struct hci_sec_filter hci_sec_filter = {
63 /* Packet types */
64 0x10,
65 /* Events */
Marcel Holtmanndd7f5522005-10-28 19:20:53 +020066 { 0x1000d9fe, 0x0000b00c },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* Commands */
68 {
69 { 0x0 },
70 /* OGF_LINK_CTL */
Marcel Holtmann7c631a62007-09-09 08:39:43 +020071 { 0xbe000006, 0x00000001, 0x00000000, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* OGF_LINK_POLICY */
Marcel Holtmann7c631a62007-09-09 08:39:43 +020073 { 0x00005200, 0x00000000, 0x00000000, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /* OGF_HOST_CTL */
Marcel Holtmann7c631a62007-09-09 08:39:43 +020075 { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* OGF_INFO_PARAM */
Marcel Holtmann7c631a62007-09-09 08:39:43 +020077 { 0x000002be, 0x00000000, 0x00000000, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /* OGF_STATUS_PARAM */
Marcel Holtmann7c631a62007-09-09 08:39:43 +020079 { 0x000000ea, 0x00000000, 0x00000000, 0x00 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81};
82
83static struct bt_sock_list hci_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070084 .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
87/* Send frame to RAW socket */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +010088void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct sock *sk;
91 struct hlist_node *node;
Marcel Holtmanne0edf372012-02-20 14:50:36 +010092 struct sk_buff *skb_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 BT_DBG("hdev %p len %d", hdev, skb->len);
95
96 read_lock(&hci_sk_list.lock);
Marcel Holtmann470fe1b2012-02-20 14:50:30 +010097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 sk_for_each(sk, node, &hci_sk_list.head) {
99 struct hci_filter *flt;
100 struct sk_buff *nskb;
101
102 if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
103 continue;
104
105 /* Don't send frame to the socket it came from */
106 if (skb->sk == sk)
107 continue;
108
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100109 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW)
Johan Hedberga40c4062010-12-08 00:21:07 +0200110 continue;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Apply filter */
113 flt = &hci_pi(sk)->filter;
114
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700115 if (!test_bit((bt_cb(skb)->pkt_type == HCI_VENDOR_PKT) ?
Gustavo Padovan3bb3c752012-05-17 00:36:22 -0300116 0 : (bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS),
117 &flt->type_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 continue;
119
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700120 if (bt_cb(skb)->pkt_type == HCI_EVENT_PKT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 register int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
122
123 if (!hci_test_bit(evt, &flt->event_mask))
124 continue;
125
David S. Miller4498c802006-11-21 16:17:41 -0800126 if (flt->opcode &&
127 ((evt == HCI_EV_CMD_COMPLETE &&
128 flt->opcode !=
Al Viro905f3ed2006-12-13 00:35:01 -0800129 get_unaligned((__le16 *)(skb->data + 3))) ||
David S. Miller4498c802006-11-21 16:17:41 -0800130 (evt == HCI_EV_CMD_STATUS &&
131 flt->opcode !=
Al Viro905f3ed2006-12-13 00:35:01 -0800132 get_unaligned((__le16 *)(skb->data + 4)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 continue;
134 }
135
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100136 if (!skb_copy) {
137 /* Create a private copy with headroom */
138 skb_copy = __pskb_copy(skb, 1, GFP_ATOMIC);
139 if (!skb_copy)
140 continue;
141
142 /* Put type byte before the data */
143 memcpy(skb_push(skb_copy, 1), &bt_cb(skb)->pkt_type, 1);
144 }
145
146 nskb = skb_clone(skb_copy, GFP_ATOMIC);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200147 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 continue;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (sock_queue_rcv_skb(sk, nskb))
151 kfree_skb(nskb);
152 }
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100153
154 read_unlock(&hci_sk_list.lock);
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100155
156 kfree_skb(skb_copy);
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100157}
158
159/* Send frame to control socket */
160void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk)
161{
162 struct sock *sk;
163 struct hlist_node *node;
164
165 BT_DBG("len %d", skb->len);
166
167 read_lock(&hci_sk_list.lock);
168
169 sk_for_each(sk, node, &hci_sk_list.head) {
170 struct sk_buff *nskb;
171
172 /* Skip the original socket */
173 if (sk == skip_sk)
174 continue;
175
176 if (sk->sk_state != BT_BOUND)
177 continue;
178
179 if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
180 continue;
181
182 nskb = skb_clone(skb, GFP_ATOMIC);
183 if (!nskb)
184 continue;
185
186 if (sock_queue_rcv_skb(sk, nskb))
187 kfree_skb(nskb);
188 }
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 read_unlock(&hci_sk_list.lock);
191}
192
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100193/* Send frame to monitor socket */
194void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
195{
196 struct sock *sk;
197 struct hlist_node *node;
198 struct sk_buff *skb_copy = NULL;
199 __le16 opcode;
200
201 if (!atomic_read(&monitor_promisc))
202 return;
203
204 BT_DBG("hdev %p len %d", hdev, skb->len);
205
206 switch (bt_cb(skb)->pkt_type) {
207 case HCI_COMMAND_PKT:
208 opcode = __constant_cpu_to_le16(HCI_MON_COMMAND_PKT);
209 break;
210 case HCI_EVENT_PKT:
211 opcode = __constant_cpu_to_le16(HCI_MON_EVENT_PKT);
212 break;
213 case HCI_ACLDATA_PKT:
214 if (bt_cb(skb)->incoming)
215 opcode = __constant_cpu_to_le16(HCI_MON_ACL_RX_PKT);
216 else
217 opcode = __constant_cpu_to_le16(HCI_MON_ACL_TX_PKT);
218 break;
219 case HCI_SCODATA_PKT:
220 if (bt_cb(skb)->incoming)
221 opcode = __constant_cpu_to_le16(HCI_MON_SCO_RX_PKT);
222 else
223 opcode = __constant_cpu_to_le16(HCI_MON_SCO_TX_PKT);
224 break;
225 default:
226 return;
227 }
228
229 read_lock(&hci_sk_list.lock);
230
231 sk_for_each(sk, node, &hci_sk_list.head) {
232 struct sk_buff *nskb;
233
234 if (sk->sk_state != BT_BOUND)
235 continue;
236
237 if (hci_pi(sk)->channel != HCI_CHANNEL_MONITOR)
238 continue;
239
240 if (!skb_copy) {
241 struct hci_mon_hdr *hdr;
242
243 /* Create a private copy with headroom */
244 skb_copy = __pskb_copy(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC);
245 if (!skb_copy)
246 continue;
247
248 /* Put header before the data */
249 hdr = (void *) skb_push(skb_copy, HCI_MON_HDR_SIZE);
250 hdr->opcode = opcode;
251 hdr->index = cpu_to_le16(hdev->id);
252 hdr->len = cpu_to_le16(skb->len);
253 }
254
255 nskb = skb_clone(skb_copy, GFP_ATOMIC);
256 if (!nskb)
257 continue;
258
259 if (sock_queue_rcv_skb(sk, nskb))
260 kfree_skb(nskb);
261 }
262
263 read_unlock(&hci_sk_list.lock);
264
265 kfree_skb(skb_copy);
266}
267
268static void send_monitor_event(struct sk_buff *skb)
269{
270 struct sock *sk;
271 struct hlist_node *node;
272
273 BT_DBG("len %d", skb->len);
274
275 read_lock(&hci_sk_list.lock);
276
277 sk_for_each(sk, node, &hci_sk_list.head) {
278 struct sk_buff *nskb;
279
280 if (sk->sk_state != BT_BOUND)
281 continue;
282
283 if (hci_pi(sk)->channel != HCI_CHANNEL_MONITOR)
284 continue;
285
286 nskb = skb_clone(skb, GFP_ATOMIC);
287 if (!nskb)
288 continue;
289
290 if (sock_queue_rcv_skb(sk, nskb))
291 kfree_skb(nskb);
292 }
293
294 read_unlock(&hci_sk_list.lock);
295}
296
297static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
298{
299 struct hci_mon_hdr *hdr;
300 struct hci_mon_new_index *ni;
301 struct sk_buff *skb;
302 __le16 opcode;
303
304 switch (event) {
305 case HCI_DEV_REG:
306 skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
307 if (!skb)
308 return NULL;
309
310 ni = (void *) skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
311 ni->type = hdev->dev_type;
312 ni->bus = hdev->bus;
313 bacpy(&ni->bdaddr, &hdev->bdaddr);
314 memcpy(ni->name, hdev->name, 8);
315
316 opcode = __constant_cpu_to_le16(HCI_MON_NEW_INDEX);
317 break;
318
319 case HCI_DEV_UNREG:
320 skb = bt_skb_alloc(0, GFP_ATOMIC);
321 if (!skb)
322 return NULL;
323
324 opcode = __constant_cpu_to_le16(HCI_MON_DEL_INDEX);
325 break;
326
327 default:
328 return NULL;
329 }
330
331 __net_timestamp(skb);
332
333 hdr = (void *) skb_push(skb, HCI_MON_HDR_SIZE);
334 hdr->opcode = opcode;
335 hdr->index = cpu_to_le16(hdev->id);
336 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
337
338 return skb;
339}
340
341static void send_monitor_replay(struct sock *sk)
342{
343 struct hci_dev *hdev;
344
345 read_lock(&hci_dev_list_lock);
346
347 list_for_each_entry(hdev, &hci_dev_list, list) {
348 struct sk_buff *skb;
349
350 skb = create_monitor_event(hdev, HCI_DEV_REG);
351 if (!skb)
352 continue;
353
354 if (sock_queue_rcv_skb(sk, skb))
355 kfree_skb(skb);
356 }
357
358 read_unlock(&hci_dev_list_lock);
359}
360
Marcel Holtmann040030e2012-02-20 14:50:37 +0100361/* Generate internal stack event */
362static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
363{
364 struct hci_event_hdr *hdr;
365 struct hci_ev_stack_internal *ev;
366 struct sk_buff *skb;
367
368 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
369 if (!skb)
370 return;
371
372 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
373 hdr->evt = HCI_EV_STACK_INTERNAL;
374 hdr->plen = sizeof(*ev) + dlen;
375
376 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
377 ev->type = type;
378 memcpy(ev->data, data, dlen);
379
380 bt_cb(skb)->incoming = 1;
381 __net_timestamp(skb);
382
383 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
384 skb->dev = (void *) hdev;
385 hci_send_to_sock(hdev, skb);
386 kfree_skb(skb);
387}
388
389void hci_sock_dev_event(struct hci_dev *hdev, int event)
390{
391 struct hci_ev_si_device ev;
392
393 BT_DBG("hdev %s event %d", hdev->name, event);
394
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100395 /* Send event to monitor */
396 if (atomic_read(&monitor_promisc)) {
397 struct sk_buff *skb;
398
399 skb = create_monitor_event(hdev, event);
400 if (skb) {
401 send_monitor_event(skb);
402 kfree_skb(skb);
403 }
404 }
405
Marcel Holtmann040030e2012-02-20 14:50:37 +0100406 /* Send event to sockets */
407 ev.event = event;
408 ev.dev_id = hdev->id;
409 hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
410
411 if (event == HCI_DEV_UNREG) {
412 struct sock *sk;
413 struct hlist_node *node;
414
415 /* Detach sockets from device */
416 read_lock(&hci_sk_list.lock);
417 sk_for_each(sk, node, &hci_sk_list.head) {
418 bh_lock_sock_nested(sk);
419 if (hci_pi(sk)->hdev == hdev) {
420 hci_pi(sk)->hdev = NULL;
421 sk->sk_err = EPIPE;
422 sk->sk_state = BT_OPEN;
423 sk->sk_state_change(sk);
424
425 hci_dev_put(hdev);
426 }
427 bh_unlock_sock(sk);
428 }
429 read_unlock(&hci_sk_list.lock);
430 }
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int hci_sock_release(struct socket *sock)
434{
435 struct sock *sk = sock->sk;
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100436 struct hci_dev *hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 BT_DBG("sock %p sk %p", sock, sk);
439
440 if (!sk)
441 return 0;
442
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100443 hdev = hci_pi(sk)->hdev;
444
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100445 if (hci_pi(sk)->channel == HCI_CHANNEL_MONITOR)
446 atomic_dec(&monitor_promisc);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 bt_sock_unlink(&hci_sk_list, sk);
449
450 if (hdev) {
451 atomic_dec(&hdev->promisc);
452 hci_dev_put(hdev);
453 }
454
455 sock_orphan(sk);
456
457 skb_queue_purge(&sk->sk_receive_queue);
458 skb_queue_purge(&sk->sk_write_queue);
459
460 sock_put(sk);
461 return 0;
462}
463
Antti Julkub2a66aa2011-06-15 12:01:14 +0300464static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
Johan Hedbergf0358562010-05-18 13:20:32 +0200465{
466 bdaddr_t bdaddr;
Antti Julku5e762442011-08-25 16:48:02 +0300467 int err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200468
469 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
470 return -EFAULT;
471
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300472 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300473
Johan Hedberg88c1fe42012-02-09 15:56:11 +0200474 err = hci_blacklist_add(hdev, &bdaddr, 0);
Antti Julku5e762442011-08-25 16:48:02 +0300475
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300476 hci_dev_unlock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300477
478 return err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200479}
480
Antti Julkub2a66aa2011-06-15 12:01:14 +0300481static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
Johan Hedbergf0358562010-05-18 13:20:32 +0200482{
483 bdaddr_t bdaddr;
Antti Julku5e762442011-08-25 16:48:02 +0300484 int err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200485
486 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
487 return -EFAULT;
488
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300489 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300490
Johan Hedberg88c1fe42012-02-09 15:56:11 +0200491 err = hci_blacklist_del(hdev, &bdaddr, 0);
Antti Julku5e762442011-08-25 16:48:02 +0300492
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300493 hci_dev_unlock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300494
495 return err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200496}
497
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900498/* Ioctls that require bound socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static inline int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
500{
501 struct hci_dev *hdev = hci_pi(sk)->hdev;
502
503 if (!hdev)
504 return -EBADFD;
505
506 switch (cmd) {
507 case HCISETRAW:
508 if (!capable(CAP_NET_ADMIN))
509 return -EACCES;
510
511 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
512 return -EPERM;
513
514 if (arg)
515 set_bit(HCI_RAW, &hdev->flags);
516 else
517 clear_bit(HCI_RAW, &hdev->flags);
518
519 return 0;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 case HCIGETCONNINFO:
Marcel Holtmann40be4922008-07-14 20:13:50 +0200522 return hci_get_conn_info(hdev, (void __user *) arg);
523
524 case HCIGETAUTHINFO:
525 return hci_get_auth_info(hdev, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Johan Hedbergf0358562010-05-18 13:20:32 +0200527 case HCIBLOCKADDR:
528 if (!capable(CAP_NET_ADMIN))
529 return -EACCES;
Antti Julkub2a66aa2011-06-15 12:01:14 +0300530 return hci_sock_blacklist_add(hdev, (void __user *) arg);
Johan Hedbergf0358562010-05-18 13:20:32 +0200531
532 case HCIUNBLOCKADDR:
533 if (!capable(CAP_NET_ADMIN))
534 return -EACCES;
Antti Julkub2a66aa2011-06-15 12:01:14 +0300535 return hci_sock_blacklist_del(hdev, (void __user *) arg);
Johan Hedbergf0358562010-05-18 13:20:32 +0200536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 default:
538 if (hdev->ioctl)
539 return hdev->ioctl(hdev, cmd, arg);
540 return -EINVAL;
541 }
542}
543
544static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
545{
546 struct sock *sk = sock->sk;
Marcel Holtmann40be4922008-07-14 20:13:50 +0200547 void __user *argp = (void __user *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int err;
549
550 BT_DBG("cmd %x arg %lx", cmd, arg);
551
552 switch (cmd) {
553 case HCIGETDEVLIST:
554 return hci_get_dev_list(argp);
555
556 case HCIGETDEVINFO:
557 return hci_get_dev_info(argp);
558
559 case HCIGETCONNLIST:
560 return hci_get_conn_list(argp);
561
562 case HCIDEVUP:
563 if (!capable(CAP_NET_ADMIN))
564 return -EACCES;
565 return hci_dev_open(arg);
566
567 case HCIDEVDOWN:
568 if (!capable(CAP_NET_ADMIN))
569 return -EACCES;
570 return hci_dev_close(arg);
571
572 case HCIDEVRESET:
573 if (!capable(CAP_NET_ADMIN))
574 return -EACCES;
575 return hci_dev_reset(arg);
576
577 case HCIDEVRESTAT:
578 if (!capable(CAP_NET_ADMIN))
579 return -EACCES;
580 return hci_dev_reset_stat(arg);
581
582 case HCISETSCAN:
583 case HCISETAUTH:
584 case HCISETENCRYPT:
585 case HCISETPTYPE:
586 case HCISETLINKPOL:
587 case HCISETLINKMODE:
588 case HCISETACLMTU:
589 case HCISETSCOMTU:
590 if (!capable(CAP_NET_ADMIN))
591 return -EACCES;
592 return hci_dev_cmd(cmd, argp);
593
594 case HCIINQUIRY:
595 return hci_inquiry(argp);
596
597 default:
598 lock_sock(sk);
599 err = hci_sock_bound_ioctl(sk, cmd, arg);
600 release_sock(sk);
601 return err;
602 }
603}
604
605static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
606{
Johan Hedberg03811012010-12-08 00:21:06 +0200607 struct sockaddr_hci haddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct sock *sk = sock->sk;
609 struct hci_dev *hdev = NULL;
Johan Hedberg03811012010-12-08 00:21:06 +0200610 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 BT_DBG("sock %p sk %p", sock, sk);
613
Johan Hedberg03811012010-12-08 00:21:06 +0200614 if (!addr)
615 return -EINVAL;
616
617 memset(&haddr, 0, sizeof(haddr));
618 len = min_t(unsigned int, sizeof(haddr), addr_len);
619 memcpy(&haddr, addr, len);
620
621 if (haddr.hci_family != AF_BLUETOOTH)
622 return -EINVAL;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 lock_sock(sk);
625
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100626 if (sk->sk_state == BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 err = -EALREADY;
628 goto done;
629 }
630
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100631 switch (haddr.hci_channel) {
632 case HCI_CHANNEL_RAW:
633 if (hci_pi(sk)->hdev) {
634 err = -EALREADY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto done;
636 }
637
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100638 if (haddr.hci_dev != HCI_DEV_NONE) {
639 hdev = hci_dev_get(haddr.hci_dev);
640 if (!hdev) {
641 err = -ENODEV;
642 goto done;
643 }
644
645 atomic_inc(&hdev->promisc);
646 }
647
648 hci_pi(sk)->hdev = hdev;
649 break;
650
651 case HCI_CHANNEL_CONTROL:
Marcel Holtmann4b95a242012-02-20 21:24:37 +0100652 if (haddr.hci_dev != HCI_DEV_NONE) {
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100653 err = -EINVAL;
654 goto done;
655 }
656
Marcel Holtmann801f13b2012-02-20 20:54:10 +0100657 if (!capable(CAP_NET_ADMIN)) {
658 err = -EPERM;
659 goto done;
660 }
661
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100662 break;
663
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100664 case HCI_CHANNEL_MONITOR:
665 if (haddr.hci_dev != HCI_DEV_NONE) {
666 err = -EINVAL;
667 goto done;
668 }
669
670 if (!capable(CAP_NET_RAW)) {
671 err = -EPERM;
672 goto done;
673 }
674
675 send_monitor_replay(sk);
676
677 atomic_inc(&monitor_promisc);
678 break;
679
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100680 default:
681 err = -EINVAL;
682 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +0100685
Johan Hedberg03811012010-12-08 00:21:06 +0200686 hci_pi(sk)->channel = haddr.hci_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 sk->sk_state = BT_BOUND;
688
689done:
690 release_sock(sk);
691 return err;
692}
693
694static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
695{
696 struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
697 struct sock *sk = sock->sk;
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100698 struct hci_dev *hdev = hci_pi(sk)->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 BT_DBG("sock %p sk %p", sock, sk);
701
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100702 if (!hdev)
703 return -EBADFD;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 lock_sock(sk);
706
707 *addr_len = sizeof(*haddr);
708 haddr->hci_family = AF_BLUETOOTH;
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100709 haddr->hci_dev = hdev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 release_sock(sk);
712 return 0;
713}
714
715static inline void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
716{
717 __u32 mask = hci_pi(sk)->cmsg_mask;
718
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700719 if (mask & HCI_CMSG_DIR) {
720 int incoming = bt_cb(skb)->incoming;
721 put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming), &incoming);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700724 if (mask & HCI_CMSG_TSTAMP) {
Johann Felix Sodenf6e623a2010-02-15 22:23:48 +0100725#ifdef CONFIG_COMPAT
726 struct compat_timeval ctv;
727#endif
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700728 struct timeval tv;
Marcel Holtmann767c5eb2007-09-09 08:39:34 +0200729 void *data;
730 int len;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700731
732 skb_get_timestamp(skb, &tv);
Marcel Holtmann767c5eb2007-09-09 08:39:34 +0200733
David S. Miller1da97f82007-09-12 14:10:58 +0200734 data = &tv;
735 len = sizeof(tv);
736#ifdef CONFIG_COMPAT
H. J. Luda88cea2012-02-10 14:12:15 -0800737 if (!COMPAT_USE_64BIT_TIME &&
738 (msg->msg_flags & MSG_CMSG_COMPAT)) {
Marcel Holtmann767c5eb2007-09-09 08:39:34 +0200739 ctv.tv_sec = tv.tv_sec;
740 ctv.tv_usec = tv.tv_usec;
741 data = &ctv;
742 len = sizeof(ctv);
Marcel Holtmann767c5eb2007-09-09 08:39:34 +0200743 }
David S. Miller1da97f82007-09-12 14:10:58 +0200744#endif
Marcel Holtmann767c5eb2007-09-09 08:39:34 +0200745
746 put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900749
750static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
Gustavo Padovan3bb3c752012-05-17 00:36:22 -0300751 struct msghdr *msg, size_t len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 int noblock = flags & MSG_DONTWAIT;
754 struct sock *sk = sock->sk;
755 struct sk_buff *skb;
756 int copied, err;
757
758 BT_DBG("sock %p, sk %p", sock, sk);
759
760 if (flags & (MSG_OOB))
761 return -EOPNOTSUPP;
762
763 if (sk->sk_state == BT_CLOSED)
764 return 0;
765
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200766 skb = skb_recv_datagram(sk, flags, noblock, &err);
767 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return err;
769
770 msg->msg_namelen = 0;
771
772 copied = skb->len;
773 if (len < copied) {
774 msg->msg_flags |= MSG_TRUNC;
775 copied = len;
776 }
777
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300778 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
780
Marcel Holtmann3a208622012-02-20 14:50:34 +0100781 switch (hci_pi(sk)->channel) {
782 case HCI_CHANNEL_RAW:
783 hci_sock_cmsg(sk, msg, skb);
784 break;
Marcel Holtmann97e0bde2012-02-22 13:49:28 +0100785 case HCI_CHANNEL_CONTROL:
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100786 case HCI_CHANNEL_MONITOR:
787 sock_recv_timestamp(msg, sk, skb);
788 break;
Marcel Holtmann3a208622012-02-20 14:50:34 +0100789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 skb_free_datagram(sk, skb);
792
793 return err ? : copied;
794}
795
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900796static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct msghdr *msg, size_t len)
798{
799 struct sock *sk = sock->sk;
800 struct hci_dev *hdev;
801 struct sk_buff *skb;
802 int err;
803
804 BT_DBG("sock %p sk %p", sock, sk);
805
806 if (msg->msg_flags & MSG_OOB)
807 return -EOPNOTSUPP;
808
809 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
810 return -EINVAL;
811
812 if (len < 4 || len > HCI_MAX_FRAME_SIZE)
813 return -EINVAL;
814
815 lock_sock(sk);
816
Johan Hedberg03811012010-12-08 00:21:06 +0200817 switch (hci_pi(sk)->channel) {
818 case HCI_CHANNEL_RAW:
819 break;
820 case HCI_CHANNEL_CONTROL:
821 err = mgmt_control(sk, msg, len);
822 goto done;
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100823 case HCI_CHANNEL_MONITOR:
824 err = -EOPNOTSUPP;
825 goto done;
Johan Hedberg03811012010-12-08 00:21:06 +0200826 default:
827 err = -EINVAL;
828 goto done;
829 }
830
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200831 hdev = hci_pi(sk)->hdev;
832 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 err = -EBADFD;
834 goto done;
835 }
836
Marcel Holtmann7e21add2009-11-18 01:05:00 +0100837 if (!test_bit(HCI_UP, &hdev->flags)) {
838 err = -ENETDOWN;
839 goto done;
840 }
841
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200842 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
843 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto done;
845
846 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
847 err = -EFAULT;
848 goto drop;
849 }
850
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700851 bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 skb_pull(skb, 1);
853 skb->dev = (void *) hdev;
854
Marcel Holtmann0d48d932005-08-09 20:30:28 -0700855 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
Harvey Harrison83985312008-05-02 16:25:46 -0700856 u16 opcode = get_unaligned_le16(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 u16 ogf = hci_opcode_ogf(opcode);
858 u16 ocf = hci_opcode_ocf(opcode);
859
860 if (((ogf > HCI_SFLT_MAX_OGF) ||
Gustavo Padovan3bb3c752012-05-17 00:36:22 -0300861 !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
862 &hci_sec_filter.ocf_mask[ogf])) &&
863 !capable(CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 err = -EPERM;
865 goto drop;
866 }
867
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200868 if (test_bit(HCI_RAW, &hdev->flags) || (ogf == 0x3f)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 skb_queue_tail(&hdev->raw_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200870 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } else {
872 skb_queue_tail(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -0200873 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 } else {
876 if (!capable(CAP_NET_RAW)) {
877 err = -EPERM;
878 goto drop;
879 }
880
881 skb_queue_tail(&hdev->raw_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200882 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
885 err = len;
886
887done:
888 release_sock(sk);
889 return err;
890
891drop:
892 kfree_skb(skb);
893 goto done;
894}
895
David S. Millerb7058842009-09-30 16:12:20 -0700896static int hci_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 struct hci_ufilter uf = { .opcode = 0 };
899 struct sock *sk = sock->sk;
900 int err = 0, opt = 0;
901
902 BT_DBG("sk %p, opt %d", sk, optname);
903
904 lock_sock(sk);
905
Marcel Holtmann2f39cdb2012-02-20 14:50:32 +0100906 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
907 err = -EINVAL;
908 goto done;
909 }
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 switch (optname) {
912 case HCI_DATA_DIR:
913 if (get_user(opt, (int __user *)optval)) {
914 err = -EFAULT;
915 break;
916 }
917
918 if (opt)
919 hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
920 else
921 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
922 break;
923
924 case HCI_TIME_STAMP:
925 if (get_user(opt, (int __user *)optval)) {
926 err = -EFAULT;
927 break;
928 }
929
930 if (opt)
931 hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
932 else
933 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
934 break;
935
936 case HCI_FILTER:
Marcel Holtmann0878b662007-05-05 00:35:59 +0200937 {
938 struct hci_filter *f = &hci_pi(sk)->filter;
939
940 uf.type_mask = f->type_mask;
941 uf.opcode = f->opcode;
942 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
943 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
944 }
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 len = min_t(unsigned int, len, sizeof(uf));
947 if (copy_from_user(&uf, optval, len)) {
948 err = -EFAULT;
949 break;
950 }
951
952 if (!capable(CAP_NET_RAW)) {
953 uf.type_mask &= hci_sec_filter.type_mask;
954 uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
955 uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
956 }
957
958 {
959 struct hci_filter *f = &hci_pi(sk)->filter;
960
961 f->type_mask = uf.type_mask;
962 f->opcode = uf.opcode;
963 *((u32 *) f->event_mask + 0) = uf.event_mask[0];
964 *((u32 *) f->event_mask + 1) = uf.event_mask[1];
965 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 default:
969 err = -ENOPROTOOPT;
970 break;
971 }
972
Marcel Holtmann2f39cdb2012-02-20 14:50:32 +0100973done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 release_sock(sk);
975 return err;
976}
977
978static int hci_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
979{
980 struct hci_ufilter uf;
981 struct sock *sk = sock->sk;
Marcel Holtmanncedc5462012-02-20 14:50:33 +0100982 int len, opt, err = 0;
983
984 BT_DBG("sk %p, opt %d", sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (get_user(len, optlen))
987 return -EFAULT;
988
Marcel Holtmanncedc5462012-02-20 14:50:33 +0100989 lock_sock(sk);
990
991 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
992 err = -EINVAL;
993 goto done;
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 switch (optname) {
997 case HCI_DATA_DIR:
998 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
999 opt = 1;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001000 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 opt = 0;
1002
1003 if (put_user(opt, optval))
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001004 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006
1007 case HCI_TIME_STAMP:
1008 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
1009 opt = 1;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001010 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 opt = 0;
1012
1013 if (put_user(opt, optval))
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001014 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
1016
1017 case HCI_FILTER:
1018 {
1019 struct hci_filter *f = &hci_pi(sk)->filter;
1020
1021 uf.type_mask = f->type_mask;
1022 uf.opcode = f->opcode;
1023 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
1024 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
1025 }
1026
1027 len = min_t(unsigned int, len, sizeof(uf));
1028 if (copy_to_user(optval, &uf, len))
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001029 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031
1032 default:
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001033 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035 }
1036
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001037done:
1038 release_sock(sk);
1039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001042static const struct proto_ops hci_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .family = PF_BLUETOOTH,
1044 .owner = THIS_MODULE,
1045 .release = hci_sock_release,
1046 .bind = hci_sock_bind,
1047 .getname = hci_sock_getname,
1048 .sendmsg = hci_sock_sendmsg,
1049 .recvmsg = hci_sock_recvmsg,
1050 .ioctl = hci_sock_ioctl,
1051 .poll = datagram_poll,
1052 .listen = sock_no_listen,
1053 .shutdown = sock_no_shutdown,
1054 .setsockopt = hci_sock_setsockopt,
1055 .getsockopt = hci_sock_getsockopt,
1056 .connect = sock_no_connect,
1057 .socketpair = sock_no_socketpair,
1058 .accept = sock_no_accept,
1059 .mmap = sock_no_mmap
1060};
1061
1062static struct proto hci_sk_proto = {
1063 .name = "HCI",
1064 .owner = THIS_MODULE,
1065 .obj_size = sizeof(struct hci_pinfo)
1066};
1067
Eric Paris3f378b62009-11-05 22:18:14 -08001068static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
1069 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct sock *sk;
1072
1073 BT_DBG("sock %p", sock);
1074
1075 if (sock->type != SOCK_RAW)
1076 return -ESOCKTNOSUPPORT;
1077
1078 sock->ops = &hci_sock_ops;
1079
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001080 sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!sk)
1082 return -ENOMEM;
1083
1084 sock_init_data(sock, sk);
1085
1086 sock_reset_flag(sk, SOCK_ZAPPED);
1087
1088 sk->sk_protocol = protocol;
1089
1090 sock->state = SS_UNCONNECTED;
1091 sk->sk_state = BT_OPEN;
1092
1093 bt_sock_link(&hci_sk_list, sk);
1094 return 0;
1095}
1096
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001097static const struct net_proto_family hci_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 .family = PF_BLUETOOTH,
1099 .owner = THIS_MODULE,
1100 .create = hci_sock_create,
1101};
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103int __init hci_sock_init(void)
1104{
1105 int err;
1106
1107 err = proto_register(&hci_sk_proto, 0);
1108 if (err < 0)
1109 return err;
1110
1111 err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
1112 if (err < 0)
1113 goto error;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 BT_INFO("HCI socket layer initialized");
1116
1117 return 0;
1118
1119error:
1120 BT_ERR("HCI socket registration failed");
1121 proto_unregister(&hci_sk_proto);
1122 return err;
1123}
1124
Anand Gadiyarb7440a142011-02-22 12:43:09 +05301125void hci_sock_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 if (bt_sock_unregister(BTPROTO_HCI) < 0)
1128 BT_ERR("HCI socket unregistration failed");
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 proto_unregister(&hci_sk_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}