blob: c88a6007e643c473dc99f5417c67dcde5a77586d [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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030027#include <linux/export.h>
Johannes Berg787b3062016-01-06 14:38:40 +010028#include <linux/utsname.h>
Marcel Holtmann70ecce92016-08-27 20:23:38 +020029#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/unaligned.h>
31
32#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h>
Marcel Holtmanncd82e612012-02-20 20:34:38 +010034#include <net/bluetooth/hci_mon.h>
Johan Hedbergfa4335d2015-03-17 13:48:50 +020035#include <net/bluetooth/mgmt.h>
36
37#include "mgmt_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Johan Hedberg801c1e82015-03-06 21:08:50 +020039static LIST_HEAD(mgmt_chan_list);
40static DEFINE_MUTEX(mgmt_chan_list_lock);
41
Marcel Holtmann70ecce92016-08-27 20:23:38 +020042static DEFINE_IDA(sock_cookie_ida);
43
Marcel Holtmanncd82e612012-02-20 20:34:38 +010044static atomic_t monitor_promisc = ATOMIC_INIT(0);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* ----- HCI socket interface ----- */
47
Marcel Holtmann863def52014-07-11 05:41:00 +020048/* Socket info */
49#define hci_pi(sk) ((struct hci_pinfo *) sk)
50
51struct hci_pinfo {
52 struct bt_sock bt;
53 struct hci_dev *hdev;
54 struct hci_filter filter;
55 __u32 cmsg_mask;
56 unsigned short channel;
Marcel Holtmann6befc642015-03-14 19:27:53 -070057 unsigned long flags;
Marcel Holtmann70ecce92016-08-27 20:23:38 +020058 __u32 cookie;
59 char comm[TASK_COMM_LEN];
Marcel Holtmann863def52014-07-11 05:41:00 +020060};
61
Marcel Holtmann6befc642015-03-14 19:27:53 -070062void hci_sock_set_flag(struct sock *sk, int nr)
63{
64 set_bit(nr, &hci_pi(sk)->flags);
65}
66
67void hci_sock_clear_flag(struct sock *sk, int nr)
68{
69 clear_bit(nr, &hci_pi(sk)->flags);
70}
71
Marcel Holtmannc85be542015-03-14 19:28:00 -070072int hci_sock_test_flag(struct sock *sk, int nr)
73{
74 return test_bit(nr, &hci_pi(sk)->flags);
75}
76
Johan Hedbergd0f172b2015-03-17 13:48:46 +020077unsigned short hci_sock_get_channel(struct sock *sk)
78{
79 return hci_pi(sk)->channel;
80}
81
Marcel Holtmann70ecce92016-08-27 20:23:38 +020082u32 hci_sock_get_cookie(struct sock *sk)
83{
84 return hci_pi(sk)->cookie;
85}
86
Marcel Holtmanndf1cb872016-08-30 05:00:34 +020087static bool hci_sock_gen_cookie(struct sock *sk)
88{
89 int id = hci_pi(sk)->cookie;
90
91 if (!id) {
92 id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
93 if (id < 0)
94 id = 0xffffffff;
95
96 hci_pi(sk)->cookie = id;
97 get_task_comm(hci_pi(sk)->comm, current);
98 return true;
99 }
100
101 return false;
102}
103
104static void hci_sock_free_cookie(struct sock *sk)
105{
106 int id = hci_pi(sk)->cookie;
107
108 if (id) {
109 hci_pi(sk)->cookie = 0xffffffff;
110 ida_simple_remove(&sock_cookie_ida, id);
111 }
112}
113
Jiri Slaby93919762015-02-19 15:20:43 +0100114static inline int hci_test_bit(int nr, const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Jiri Slaby93919762015-02-19 15:20:43 +0100116 return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119/* Security filter */
Marcel Holtmann3ad254f2014-07-11 05:36:39 +0200120#define HCI_SFLT_MAX_OGF 5
121
122struct hci_sec_filter {
123 __u32 type_mask;
124 __u32 event_mask[2];
125 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
126};
127
Marcel Holtmann7e67c112014-07-11 05:36:40 +0200128static const struct hci_sec_filter hci_sec_filter = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* Packet types */
130 0x10,
131 /* Events */
Marcel Holtmanndd7f5522005-10-28 19:20:53 +0200132 { 0x1000d9fe, 0x0000b00c },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* Commands */
134 {
135 { 0x0 },
136 /* OGF_LINK_CTL */
Marcel Holtmann7c631a62007-09-09 08:39:43 +0200137 { 0xbe000006, 0x00000001, 0x00000000, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* OGF_LINK_POLICY */
Marcel Holtmann7c631a62007-09-09 08:39:43 +0200139 { 0x00005200, 0x00000000, 0x00000000, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* OGF_HOST_CTL */
Marcel Holtmann7c631a62007-09-09 08:39:43 +0200141 { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* OGF_INFO_PARAM */
Marcel Holtmann7c631a62007-09-09 08:39:43 +0200143 { 0x000002be, 0x00000000, 0x00000000, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* OGF_STATUS_PARAM */
Marcel Holtmann7c631a62007-09-09 08:39:43 +0200145 { 0x000000ea, 0x00000000, 0x00000000, 0x00 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147};
148
149static struct bt_sock_list hci_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -0700150 .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Marcel Holtmannf81fe642013-08-25 23:25:15 -0700153static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
154{
155 struct hci_filter *flt;
156 int flt_type, flt_event;
157
158 /* Apply filter */
159 flt = &hci_pi(sk)->filter;
160
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100161 flt_type = hci_skb_pkt_type(skb) & HCI_FLT_TYPE_BITS;
Marcel Holtmannf81fe642013-08-25 23:25:15 -0700162
163 if (!test_bit(flt_type, &flt->type_mask))
164 return true;
165
166 /* Extra filter for event packets only */
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100167 if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT)
Marcel Holtmannf81fe642013-08-25 23:25:15 -0700168 return false;
169
170 flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
171
172 if (!hci_test_bit(flt_event, &flt->event_mask))
173 return true;
174
175 /* Check filter only when opcode is set */
176 if (!flt->opcode)
177 return false;
178
179 if (flt_event == HCI_EV_CMD_COMPLETE &&
180 flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
181 return true;
182
183 if (flt_event == HCI_EV_CMD_STATUS &&
184 flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
185 return true;
186
187 return false;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* Send frame to RAW socket */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100191void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct sock *sk;
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100194 struct sk_buff *skb_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 BT_DBG("hdev %p len %d", hdev, skb->len);
197
198 read_lock(&hci_sk_list.lock);
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100199
Sasha Levinb67bfe02013-02-27 17:06:00 -0800200 sk_for_each(sk, &hci_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct sk_buff *nskb;
202
203 if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
204 continue;
205
206 /* Don't send frame to the socket it came from */
207 if (skb->sk == sk)
208 continue;
209
Marcel Holtmann23500182013-08-26 21:40:52 -0700210 if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100211 if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
212 hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
213 hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
214 hci_skb_pkt_type(skb) != HCI_SCODATA_PKT)
Marcel Holtmannbb775432015-10-09 16:13:50 +0200215 continue;
Marcel Holtmann23500182013-08-26 21:40:52 -0700216 if (is_filtered_packet(sk, skb))
217 continue;
218 } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
219 if (!bt_cb(skb)->incoming)
220 continue;
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100221 if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
222 hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
223 hci_skb_pkt_type(skb) != HCI_SCODATA_PKT)
Marcel Holtmann23500182013-08-26 21:40:52 -0700224 continue;
225 } else {
226 /* Don't send frame to other channel types */
Johan Hedberga40c4062010-12-08 00:21:07 +0200227 continue;
Marcel Holtmann23500182013-08-26 21:40:52 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100230 if (!skb_copy) {
231 /* Create a private copy with headroom */
Octavian Purdilabad93e92014-06-12 01:36:26 +0300232 skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true);
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100233 if (!skb_copy)
234 continue;
235
236 /* Put type byte before the data */
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100237 memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1);
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100238 }
239
240 nskb = skb_clone(skb_copy, GFP_ATOMIC);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200241 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 continue;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (sock_queue_rcv_skb(sk, nskb))
245 kfree_skb(nskb);
246 }
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100247
248 read_unlock(&hci_sk_list.lock);
Marcel Holtmanne0edf372012-02-20 14:50:36 +0100249
250 kfree_skb(skb_copy);
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100251}
252
Johan Hedberg71290692015-02-20 13:26:23 +0200253/* Send frame to sockets with specific channel */
254void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700255 int flag, struct sock *skip_sk)
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100256{
257 struct sock *sk;
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100258
Johan Hedberg71290692015-02-20 13:26:23 +0200259 BT_DBG("channel %u len %d", channel, skb->len);
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100260
261 read_lock(&hci_sk_list.lock);
262
Sasha Levinb67bfe02013-02-27 17:06:00 -0800263 sk_for_each(sk, &hci_sk_list.head) {
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100264 struct sk_buff *nskb;
265
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700266 /* Ignore socket without the flag set */
Marcel Holtmannc85be542015-03-14 19:28:00 -0700267 if (!hci_sock_test_flag(sk, flag))
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700268 continue;
269
Marcel Holtmann470fe1b2012-02-20 14:50:30 +0100270 /* Skip the original socket */
271 if (sk == skip_sk)
272 continue;
273
274 if (sk->sk_state != BT_BOUND)
275 continue;
276
Johan Hedberg71290692015-02-20 13:26:23 +0200277 if (hci_pi(sk)->channel != channel)
Marcel Holtmannd7f72f62015-01-11 19:33:32 -0800278 continue;
279
280 nskb = skb_clone(skb, GFP_ATOMIC);
281 if (!nskb)
282 continue;
283
284 if (sock_queue_rcv_skb(sk, nskb))
285 kfree_skb(nskb);
286 }
287
288 read_unlock(&hci_sk_list.lock);
289}
290
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100291/* Send frame to monitor socket */
292void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
293{
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100294 struct sk_buff *skb_copy = NULL;
Marcel Holtmann2b531292015-01-11 19:33:31 -0800295 struct hci_mon_hdr *hdr;
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100296 __le16 opcode;
297
298 if (!atomic_read(&monitor_promisc))
299 return;
300
301 BT_DBG("hdev %p len %d", hdev, skb->len);
302
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100303 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100304 case HCI_COMMAND_PKT:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700305 opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100306 break;
307 case HCI_EVENT_PKT:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700308 opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100309 break;
310 case HCI_ACLDATA_PKT:
311 if (bt_cb(skb)->incoming)
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700312 opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100313 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700314 opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100315 break;
316 case HCI_SCODATA_PKT:
317 if (bt_cb(skb)->incoming)
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700318 opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100319 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700320 opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100321 break;
Marcel Holtmanne875ff82015-10-07 16:38:35 +0200322 case HCI_DIAG_PKT:
323 opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG);
324 break;
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100325 default:
326 return;
327 }
328
Marcel Holtmann2b531292015-01-11 19:33:31 -0800329 /* Create a private copy with headroom */
330 skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
331 if (!skb_copy)
332 return;
333
334 /* Put header before the data */
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100335 hdr = (void *)skb_push(skb_copy, HCI_MON_HDR_SIZE);
Marcel Holtmann2b531292015-01-11 19:33:31 -0800336 hdr->opcode = opcode;
337 hdr->index = cpu_to_le16(hdev->id);
338 hdr->len = cpu_to_le16(skb->len);
339
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700340 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
341 HCI_SOCK_TRUSTED, NULL);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100342 kfree_skb(skb_copy);
343}
344
Marcel Holtmann38ceaa02016-08-27 20:23:41 +0200345void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
346 void *data, u16 data_len, ktime_t tstamp,
347 int flag, struct sock *skip_sk)
348{
349 struct sock *sk;
350 __le16 index;
351
352 if (hdev)
353 index = cpu_to_le16(hdev->id);
354 else
355 index = cpu_to_le16(MGMT_INDEX_NONE);
356
357 read_lock(&hci_sk_list.lock);
358
359 sk_for_each(sk, &hci_sk_list.head) {
360 struct hci_mon_hdr *hdr;
361 struct sk_buff *skb;
362
363 if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
364 continue;
365
366 /* Ignore socket without the flag set */
367 if (!hci_sock_test_flag(sk, flag))
368 continue;
369
370 /* Skip the original socket */
371 if (sk == skip_sk)
372 continue;
373
374 skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC);
375 if (!skb)
376 continue;
377
378 put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
379 put_unaligned_le16(event, skb_put(skb, 2));
380
381 if (data)
382 memcpy(skb_put(skb, data_len), data, data_len);
383
384 skb->tstamp = tstamp;
385
386 hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
387 hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
388 hdr->index = index;
389 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
390
391 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
392 HCI_SOCK_TRUSTED, NULL);
393 kfree_skb(skb);
394 }
395
396 read_unlock(&hci_sk_list.lock);
397}
398
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100399static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
400{
401 struct hci_mon_hdr *hdr;
402 struct hci_mon_new_index *ni;
Marcel Holtmann6c566dd2015-10-07 15:32:13 +0200403 struct hci_mon_index_info *ii;
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100404 struct sk_buff *skb;
405 __le16 opcode;
406
407 switch (event) {
408 case HCI_DEV_REG:
409 skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
410 if (!skb)
411 return NULL;
412
Marcel Holtmann6c566dd2015-10-07 15:32:13 +0200413 ni = (void *)skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100414 ni->type = hdev->dev_type;
415 ni->bus = hdev->bus;
416 bacpy(&ni->bdaddr, &hdev->bdaddr);
417 memcpy(ni->name, hdev->name, 8);
418
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700419 opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100420 break;
421
422 case HCI_DEV_UNREG:
423 skb = bt_skb_alloc(0, GFP_ATOMIC);
424 if (!skb)
425 return NULL;
426
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700427 opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100428 break;
429
Marcel Holtmanne131d742015-10-20 02:30:47 +0200430 case HCI_DEV_SETUP:
431 if (hdev->manufacturer == 0xffff)
432 return NULL;
433
434 /* fall through */
435
Marcel Holtmann6c566dd2015-10-07 15:32:13 +0200436 case HCI_DEV_UP:
437 skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC);
438 if (!skb)
439 return NULL;
440
441 ii = (void *)skb_put(skb, HCI_MON_INDEX_INFO_SIZE);
442 bacpy(&ii->bdaddr, &hdev->bdaddr);
443 ii->manufacturer = cpu_to_le16(hdev->manufacturer);
444
445 opcode = cpu_to_le16(HCI_MON_INDEX_INFO);
446 break;
447
Marcel Holtmann22db3cbc2015-10-04 23:34:03 +0200448 case HCI_DEV_OPEN:
449 skb = bt_skb_alloc(0, GFP_ATOMIC);
450 if (!skb)
451 return NULL;
452
453 opcode = cpu_to_le16(HCI_MON_OPEN_INDEX);
454 break;
455
456 case HCI_DEV_CLOSE:
457 skb = bt_skb_alloc(0, GFP_ATOMIC);
458 if (!skb)
459 return NULL;
460
461 opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX);
462 break;
463
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100464 default:
465 return NULL;
466 }
467
468 __net_timestamp(skb);
469
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100470 hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100471 hdr->opcode = opcode;
472 hdr->index = cpu_to_le16(hdev->id);
473 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
474
475 return skb;
476}
477
Marcel Holtmann249fa162016-08-27 20:23:40 +0200478static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
479{
480 struct hci_mon_hdr *hdr;
481 struct sk_buff *skb;
Marcel Holtmannd0bef1d2016-08-30 05:00:38 +0200482 u16 format;
Marcel Holtmann249fa162016-08-27 20:23:40 +0200483 u8 ver[3];
484 u32 flags;
485
Marcel Holtmann0ef2c422016-08-30 05:00:36 +0200486 /* No message needed when cookie is not present */
487 if (!hci_pi(sk)->cookie)
488 return NULL;
489
Marcel Holtmannd0bef1d2016-08-30 05:00:38 +0200490 switch (hci_pi(sk)->channel) {
Marcel Holtmannf81f5b22016-08-30 05:00:39 +0200491 case HCI_CHANNEL_RAW:
492 format = 0x0000;
493 ver[0] = BT_SUBSYS_VERSION;
494 put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
495 break;
Marcel Holtmannaa1638d2016-09-01 19:48:28 +0200496 case HCI_CHANNEL_USER:
497 format = 0x0001;
498 ver[0] = BT_SUBSYS_VERSION;
499 put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
500 break;
Marcel Holtmannd0bef1d2016-08-30 05:00:38 +0200501 case HCI_CHANNEL_CONTROL:
502 format = 0x0002;
503 mgmt_fill_version_info(ver);
504 break;
505 default:
506 /* No message for unsupported format */
507 return NULL;
508 }
509
Marcel Holtmann249fa162016-08-27 20:23:40 +0200510 skb = bt_skb_alloc(14 + TASK_COMM_LEN , GFP_ATOMIC);
511 if (!skb)
512 return NULL;
513
Marcel Holtmann249fa162016-08-27 20:23:40 +0200514 flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
515
516 put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
517 put_unaligned_le16(format, skb_put(skb, 2));
518 memcpy(skb_put(skb, sizeof(ver)), ver, sizeof(ver));
519 put_unaligned_le32(flags, skb_put(skb, 4));
520 *skb_put(skb, 1) = TASK_COMM_LEN;
521 memcpy(skb_put(skb, TASK_COMM_LEN), hci_pi(sk)->comm, TASK_COMM_LEN);
522
523 __net_timestamp(skb);
524
525 hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
526 hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
Marcel Holtmann0ef2c422016-08-30 05:00:36 +0200527 if (hci_pi(sk)->hdev)
528 hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
529 else
530 hdr->index = cpu_to_le16(HCI_DEV_NONE);
Marcel Holtmann249fa162016-08-27 20:23:40 +0200531 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
532
533 return skb;
534}
535
536static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
537{
538 struct hci_mon_hdr *hdr;
539 struct sk_buff *skb;
540
Marcel Holtmann0ef2c422016-08-30 05:00:36 +0200541 /* No message needed when cookie is not present */
542 if (!hci_pi(sk)->cookie)
543 return NULL;
544
Marcel Holtmannd0bef1d2016-08-30 05:00:38 +0200545 switch (hci_pi(sk)->channel) {
Marcel Holtmannf81f5b22016-08-30 05:00:39 +0200546 case HCI_CHANNEL_RAW:
Marcel Holtmannaa1638d2016-09-01 19:48:28 +0200547 case HCI_CHANNEL_USER:
Marcel Holtmannd0bef1d2016-08-30 05:00:38 +0200548 case HCI_CHANNEL_CONTROL:
549 break;
550 default:
551 /* No message for unsupported format */
552 return NULL;
553 }
554
Marcel Holtmann249fa162016-08-27 20:23:40 +0200555 skb = bt_skb_alloc(4, GFP_ATOMIC);
556 if (!skb)
557 return NULL;
558
559 put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
560
561 __net_timestamp(skb);
562
563 hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
564 hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
Marcel Holtmann0ef2c422016-08-30 05:00:36 +0200565 if (hci_pi(sk)->hdev)
566 hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
567 else
568 hdr->index = cpu_to_le16(HCI_DEV_NONE);
Marcel Holtmann249fa162016-08-27 20:23:40 +0200569 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
570
571 return skb;
572}
573
Marcel Holtmann38ceaa02016-08-27 20:23:41 +0200574static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index,
575 u16 opcode, u16 len,
576 const void *buf)
577{
578 struct hci_mon_hdr *hdr;
579 struct sk_buff *skb;
580
581 skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
582 if (!skb)
583 return NULL;
584
585 put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
586 put_unaligned_le16(opcode, skb_put(skb, 2));
587
588 if (buf)
589 memcpy(skb_put(skb, len), buf, len);
590
591 __net_timestamp(skb);
592
593 hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
594 hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND);
595 hdr->index = cpu_to_le16(index);
596 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
597
598 return skb;
599}
600
Johannes Berg787b3062016-01-06 14:38:40 +0100601static void __printf(2, 3)
602send_monitor_note(struct sock *sk, const char *fmt, ...)
Marcel Holtmanndd315062015-11-08 07:47:12 +0100603{
Johannes Berg787b3062016-01-06 14:38:40 +0100604 size_t len;
Marcel Holtmanndd315062015-11-08 07:47:12 +0100605 struct hci_mon_hdr *hdr;
606 struct sk_buff *skb;
Johannes Berg787b3062016-01-06 14:38:40 +0100607 va_list args;
608
609 va_start(args, fmt);
610 len = vsnprintf(NULL, 0, fmt, args);
611 va_end(args);
Marcel Holtmanndd315062015-11-08 07:47:12 +0100612
613 skb = bt_skb_alloc(len + 1, GFP_ATOMIC);
614 if (!skb)
615 return;
616
Johannes Berg787b3062016-01-06 14:38:40 +0100617 va_start(args, fmt);
618 vsprintf(skb_put(skb, len), fmt, args);
619 *skb_put(skb, 1) = 0;
620 va_end(args);
Marcel Holtmanndd315062015-11-08 07:47:12 +0100621
622 __net_timestamp(skb);
623
624 hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
625 hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE);
626 hdr->index = cpu_to_le16(HCI_DEV_NONE);
627 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
628
629 if (sock_queue_rcv_skb(sk, skb))
630 kfree_skb(skb);
631}
632
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100633static void send_monitor_replay(struct sock *sk)
634{
635 struct hci_dev *hdev;
636
637 read_lock(&hci_dev_list_lock);
638
639 list_for_each_entry(hdev, &hci_dev_list, list) {
640 struct sk_buff *skb;
641
642 skb = create_monitor_event(hdev, HCI_DEV_REG);
643 if (!skb)
644 continue;
645
646 if (sock_queue_rcv_skb(sk, skb))
647 kfree_skb(skb);
Marcel Holtmann22db3cbc2015-10-04 23:34:03 +0200648
649 if (!test_bit(HCI_RUNNING, &hdev->flags))
650 continue;
651
652 skb = create_monitor_event(hdev, HCI_DEV_OPEN);
653 if (!skb)
654 continue;
655
656 if (sock_queue_rcv_skb(sk, skb))
657 kfree_skb(skb);
Marcel Holtmann6c566dd2015-10-07 15:32:13 +0200658
Marcel Holtmanne131d742015-10-20 02:30:47 +0200659 if (test_bit(HCI_UP, &hdev->flags))
660 skb = create_monitor_event(hdev, HCI_DEV_UP);
661 else if (hci_dev_test_flag(hdev, HCI_SETUP))
662 skb = create_monitor_event(hdev, HCI_DEV_SETUP);
663 else
664 skb = NULL;
Marcel Holtmann6c566dd2015-10-07 15:32:13 +0200665
Marcel Holtmanne131d742015-10-20 02:30:47 +0200666 if (skb) {
667 if (sock_queue_rcv_skb(sk, skb))
668 kfree_skb(skb);
669 }
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100670 }
671
672 read_unlock(&hci_dev_list_lock);
673}
674
Marcel Holtmann249fa162016-08-27 20:23:40 +0200675static void send_monitor_control_replay(struct sock *mon_sk)
676{
677 struct sock *sk;
678
679 read_lock(&hci_sk_list.lock);
680
681 sk_for_each(sk, &hci_sk_list.head) {
682 struct sk_buff *skb;
683
Marcel Holtmann249fa162016-08-27 20:23:40 +0200684 skb = create_monitor_ctrl_open(sk);
685 if (!skb)
686 continue;
687
688 if (sock_queue_rcv_skb(mon_sk, skb))
689 kfree_skb(skb);
690 }
691
692 read_unlock(&hci_sk_list.lock);
693}
694
Marcel Holtmann040030e2012-02-20 14:50:37 +0100695/* Generate internal stack event */
696static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
697{
698 struct hci_event_hdr *hdr;
699 struct hci_ev_stack_internal *ev;
700 struct sk_buff *skb;
701
702 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
703 if (!skb)
704 return;
705
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100706 hdr = (void *)skb_put(skb, HCI_EVENT_HDR_SIZE);
Marcel Holtmann040030e2012-02-20 14:50:37 +0100707 hdr->evt = HCI_EV_STACK_INTERNAL;
708 hdr->plen = sizeof(*ev) + dlen;
709
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100710 ev = (void *)skb_put(skb, sizeof(*ev) + dlen);
Marcel Holtmann040030e2012-02-20 14:50:37 +0100711 ev->type = type;
712 memcpy(ev->data, data, dlen);
713
714 bt_cb(skb)->incoming = 1;
715 __net_timestamp(skb);
716
Marcel Holtmannd79f34e2015-11-05 07:10:00 +0100717 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmann040030e2012-02-20 14:50:37 +0100718 hci_send_to_sock(hdev, skb);
719 kfree_skb(skb);
720}
721
722void hci_sock_dev_event(struct hci_dev *hdev, int event)
723{
Marcel Holtmann040030e2012-02-20 14:50:37 +0100724 BT_DBG("hdev %s event %d", hdev->name, event);
725
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100726 if (atomic_read(&monitor_promisc)) {
727 struct sk_buff *skb;
728
Marcel Holtmanned1b28a2015-10-04 23:33:59 +0200729 /* Send event to monitor */
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100730 skb = create_monitor_event(hdev, event);
731 if (skb) {
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700732 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
733 HCI_SOCK_TRUSTED, NULL);
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100734 kfree_skb(skb);
735 }
736 }
737
Marcel Holtmanned1b28a2015-10-04 23:33:59 +0200738 if (event <= HCI_DEV_DOWN) {
739 struct hci_ev_si_device ev;
740
741 /* Send event to sockets */
742 ev.event = event;
743 ev.dev_id = hdev->id;
744 hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
745 }
Marcel Holtmann040030e2012-02-20 14:50:37 +0100746
747 if (event == HCI_DEV_UNREG) {
748 struct sock *sk;
Marcel Holtmann040030e2012-02-20 14:50:37 +0100749
750 /* Detach sockets from device */
751 read_lock(&hci_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800752 sk_for_each(sk, &hci_sk_list.head) {
Marcel Holtmann040030e2012-02-20 14:50:37 +0100753 bh_lock_sock_nested(sk);
754 if (hci_pi(sk)->hdev == hdev) {
755 hci_pi(sk)->hdev = NULL;
756 sk->sk_err = EPIPE;
757 sk->sk_state = BT_OPEN;
758 sk->sk_state_change(sk);
759
760 hci_dev_put(hdev);
761 }
762 bh_unlock_sock(sk);
763 }
764 read_unlock(&hci_sk_list.lock);
765 }
766}
767
Johan Hedberg801c1e82015-03-06 21:08:50 +0200768static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
769{
770 struct hci_mgmt_chan *c;
771
772 list_for_each_entry(c, &mgmt_chan_list, list) {
773 if (c->channel == channel)
774 return c;
775 }
776
777 return NULL;
778}
779
780static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
781{
782 struct hci_mgmt_chan *c;
783
784 mutex_lock(&mgmt_chan_list_lock);
785 c = __hci_mgmt_chan_find(channel);
786 mutex_unlock(&mgmt_chan_list_lock);
787
788 return c;
789}
790
791int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
792{
793 if (c->channel < HCI_CHANNEL_CONTROL)
794 return -EINVAL;
795
796 mutex_lock(&mgmt_chan_list_lock);
797 if (__hci_mgmt_chan_find(c->channel)) {
798 mutex_unlock(&mgmt_chan_list_lock);
799 return -EALREADY;
800 }
801
802 list_add_tail(&c->list, &mgmt_chan_list);
803
804 mutex_unlock(&mgmt_chan_list_lock);
805
806 return 0;
807}
808EXPORT_SYMBOL(hci_mgmt_chan_register);
809
810void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
811{
812 mutex_lock(&mgmt_chan_list_lock);
813 list_del(&c->list);
814 mutex_unlock(&mgmt_chan_list_lock);
815}
816EXPORT_SYMBOL(hci_mgmt_chan_unregister);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818static int hci_sock_release(struct socket *sock)
819{
820 struct sock *sk = sock->sk;
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100821 struct hci_dev *hdev;
Marcel Holtmann249fa162016-08-27 20:23:40 +0200822 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 BT_DBG("sock %p sk %p", sock, sk);
825
826 if (!sk)
827 return 0;
828
Marcel Holtmann7b005bd2006-02-13 11:40:03 +0100829 hdev = hci_pi(sk)->hdev;
830
Marcel Holtmann70ecce92016-08-27 20:23:38 +0200831 switch (hci_pi(sk)->channel) {
832 case HCI_CHANNEL_MONITOR:
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100833 atomic_dec(&monitor_promisc);
Marcel Holtmann70ecce92016-08-27 20:23:38 +0200834 break;
Marcel Holtmannf81f5b22016-08-30 05:00:39 +0200835 case HCI_CHANNEL_RAW:
Marcel Holtmannaa1638d2016-09-01 19:48:28 +0200836 case HCI_CHANNEL_USER:
Marcel Holtmann70ecce92016-08-27 20:23:38 +0200837 case HCI_CHANNEL_CONTROL:
Marcel Holtmann249fa162016-08-27 20:23:40 +0200838 /* Send event to monitor */
839 skb = create_monitor_ctrl_close(sk);
840 if (skb) {
841 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
842 HCI_SOCK_TRUSTED, NULL);
843 kfree_skb(skb);
844 }
845
Marcel Holtmanndf1cb872016-08-30 05:00:34 +0200846 hci_sock_free_cookie(sk);
Marcel Holtmann70ecce92016-08-27 20:23:38 +0200847 break;
848 }
Marcel Holtmanncd82e612012-02-20 20:34:38 +0100849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 bt_sock_unlink(&hci_sk_list, sk);
851
852 if (hdev) {
Marcel Holtmann23500182013-08-26 21:40:52 -0700853 if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
Simon Fels6b3cc1d2015-09-02 12:10:12 +0200854 /* When releasing an user channel exclusive access,
855 * call hci_dev_do_close directly instead of calling
856 * hci_dev_close to ensure the exclusive access will
857 * be released and the controller brought back down.
858 *
859 * The checking of HCI_AUTO_OFF is not needed in this
860 * case since it will have been cleared already when
861 * opening the user channel.
862 */
863 hci_dev_do_close(hdev);
Loic Poulain9380f9e2015-05-21 16:46:41 +0200864 hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
865 mgmt_index_added(hdev);
Marcel Holtmann23500182013-08-26 21:40:52 -0700866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 atomic_dec(&hdev->promisc);
869 hci_dev_put(hdev);
870 }
871
872 sock_orphan(sk);
873
874 skb_queue_purge(&sk->sk_receive_queue);
875 skb_queue_purge(&sk->sk_write_queue);
876
877 sock_put(sk);
878 return 0;
879}
880
Antti Julkub2a66aa2011-06-15 12:01:14 +0300881static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
Johan Hedbergf0358562010-05-18 13:20:32 +0200882{
883 bdaddr_t bdaddr;
Antti Julku5e762442011-08-25 16:48:02 +0300884 int err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200885
886 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
887 return -EFAULT;
888
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300889 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300890
Johan Hedbergdcc36c12014-07-09 12:59:13 +0300891 err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
Antti Julku5e762442011-08-25 16:48:02 +0300892
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300893 hci_dev_unlock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300894
895 return err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200896}
897
Antti Julkub2a66aa2011-06-15 12:01:14 +0300898static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
Johan Hedbergf0358562010-05-18 13:20:32 +0200899{
900 bdaddr_t bdaddr;
Antti Julku5e762442011-08-25 16:48:02 +0300901 int err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200902
903 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
904 return -EFAULT;
905
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300906 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300907
Johan Hedbergdcc36c12014-07-09 12:59:13 +0300908 err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
Antti Julku5e762442011-08-25 16:48:02 +0300909
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300910 hci_dev_unlock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +0300911
912 return err;
Johan Hedbergf0358562010-05-18 13:20:32 +0200913}
914
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900915/* Ioctls that require bound socket */
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300916static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
917 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct hci_dev *hdev = hci_pi(sk)->hdev;
920
921 if (!hdev)
922 return -EBADFD;
923
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700924 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700925 return -EBUSY;
926
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700927 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmannfee746b2014-06-29 12:13:05 +0200928 return -EOPNOTSUPP;
929
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200930 if (hdev->dev_type != HCI_PRIMARY)
Marcel Holtmann5b69bef52013-10-10 10:02:08 -0700931 return -EOPNOTSUPP;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 switch (cmd) {
934 case HCISETRAW:
935 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +0000936 return -EPERM;
Marcel Holtmanndb596682014-04-16 20:04:38 -0700937 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case HCIGETCONNINFO:
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100940 return hci_get_conn_info(hdev, (void __user *)arg);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200941
942 case HCIGETAUTHINFO:
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100943 return hci_get_auth_info(hdev, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Johan Hedbergf0358562010-05-18 13:20:32 +0200945 case HCIBLOCKADDR:
946 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +0000947 return -EPERM;
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100948 return hci_sock_blacklist_add(hdev, (void __user *)arg);
Johan Hedbergf0358562010-05-18 13:20:32 +0200949
950 case HCIUNBLOCKADDR:
951 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +0000952 return -EPERM;
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100953 return hci_sock_blacklist_del(hdev, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700955
Marcel Holtmann324d36e2013-10-10 10:50:06 -0700956 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300959static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
960 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Marcel Holtmann8528d3f2015-11-08 07:47:11 +0100962 void __user *argp = (void __user *)arg;
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700963 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int err;
965
966 BT_DBG("cmd %x arg %lx", cmd, arg);
967
Marcel Holtmannc1c4f952013-08-26 09:39:55 -0700968 lock_sock(sk);
969
970 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
971 err = -EBADFD;
972 goto done;
973 }
974
Marcel Holtmannf81f5b22016-08-30 05:00:39 +0200975 /* When calling an ioctl on an unbound raw socket, then ensure
976 * that the monitor gets informed. Ensure that the resulting event
977 * is only send once by checking if the cookie exists or not. The
978 * socket cookie will be only ever generated once for the lifetime
979 * of a given socket.
980 */
981 if (hci_sock_gen_cookie(sk)) {
982 struct sk_buff *skb;
983
984 if (capable(CAP_NET_ADMIN))
985 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
986
987 /* Send event to monitor */
988 skb = create_monitor_ctrl_open(sk);
989 if (skb) {
990 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
991 HCI_SOCK_TRUSTED, NULL);
992 kfree_skb(skb);
993 }
994 }
995
Marcel Holtmannc1c4f952013-08-26 09:39:55 -0700996 release_sock(sk);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 switch (cmd) {
999 case HCIGETDEVLIST:
1000 return hci_get_dev_list(argp);
1001
1002 case HCIGETDEVINFO:
1003 return hci_get_dev_info(argp);
1004
1005 case HCIGETCONNLIST:
1006 return hci_get_conn_list(argp);
1007
1008 case HCIDEVUP:
1009 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001010 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return hci_dev_open(arg);
1012
1013 case HCIDEVDOWN:
1014 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001015 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return hci_dev_close(arg);
1017
1018 case HCIDEVRESET:
1019 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001020 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return hci_dev_reset(arg);
1022
1023 case HCIDEVRESTAT:
1024 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001025 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return hci_dev_reset_stat(arg);
1027
1028 case HCISETSCAN:
1029 case HCISETAUTH:
1030 case HCISETENCRYPT:
1031 case HCISETPTYPE:
1032 case HCISETLINKPOL:
1033 case HCISETLINKMODE:
1034 case HCISETACLMTU:
1035 case HCISETSCOMTU:
1036 if (!capable(CAP_NET_ADMIN))
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001037 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return hci_dev_cmd(cmd, argp);
1039
1040 case HCIINQUIRY:
1041 return hci_inquiry(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Marcel Holtmannc1c4f952013-08-26 09:39:55 -07001043
1044 lock_sock(sk);
1045
1046 err = hci_sock_bound_ioctl(sk, cmd, arg);
1047
1048done:
1049 release_sock(sk);
1050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001053static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
1054 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Johan Hedberg03811012010-12-08 00:21:06 +02001056 struct sockaddr_hci haddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct sock *sk = sock->sk;
1058 struct hci_dev *hdev = NULL;
Marcel Holtmannf4cdbb32016-08-30 05:00:40 +02001059 struct sk_buff *skb;
Johan Hedberg03811012010-12-08 00:21:06 +02001060 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 BT_DBG("sock %p sk %p", sock, sk);
1063
Johan Hedberg03811012010-12-08 00:21:06 +02001064 if (!addr)
1065 return -EINVAL;
1066
1067 memset(&haddr, 0, sizeof(haddr));
1068 len = min_t(unsigned int, sizeof(haddr), addr_len);
1069 memcpy(&haddr, addr, len);
1070
1071 if (haddr.hci_family != AF_BLUETOOTH)
1072 return -EINVAL;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 lock_sock(sk);
1075
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +01001076 if (sk->sk_state == BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 err = -EALREADY;
1078 goto done;
1079 }
1080
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +01001081 switch (haddr.hci_channel) {
1082 case HCI_CHANNEL_RAW:
1083 if (hci_pi(sk)->hdev) {
1084 err = -EALREADY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto done;
1086 }
1087
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +01001088 if (haddr.hci_dev != HCI_DEV_NONE) {
1089 hdev = hci_dev_get(haddr.hci_dev);
1090 if (!hdev) {
1091 err = -ENODEV;
1092 goto done;
1093 }
1094
1095 atomic_inc(&hdev->promisc);
1096 }
1097
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001098 hci_pi(sk)->channel = haddr.hci_channel;
Marcel Holtmannf81f5b22016-08-30 05:00:39 +02001099
Marcel Holtmannf4cdbb32016-08-30 05:00:40 +02001100 if (!hci_sock_gen_cookie(sk)) {
1101 /* In the case when a cookie has already been assigned,
1102 * then there has been already an ioctl issued against
1103 * an unbound socket and with that triggerd an open
1104 * notification. Send a close notification first to
1105 * allow the state transition to bounded.
1106 */
1107 skb = create_monitor_ctrl_close(sk);
Marcel Holtmannf81f5b22016-08-30 05:00:39 +02001108 if (skb) {
1109 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1110 HCI_SOCK_TRUSTED, NULL);
1111 kfree_skb(skb);
1112 }
1113 }
Marcel Holtmannf4cdbb32016-08-30 05:00:40 +02001114
1115 if (capable(CAP_NET_ADMIN))
1116 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1117
1118 hci_pi(sk)->hdev = hdev;
1119
1120 /* Send event to monitor */
1121 skb = create_monitor_ctrl_open(sk);
1122 if (skb) {
1123 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1124 HCI_SOCK_TRUSTED, NULL);
1125 kfree_skb(skb);
1126 }
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +01001127 break;
1128
Marcel Holtmann23500182013-08-26 21:40:52 -07001129 case HCI_CHANNEL_USER:
1130 if (hci_pi(sk)->hdev) {
1131 err = -EALREADY;
1132 goto done;
1133 }
1134
1135 if (haddr.hci_dev == HCI_DEV_NONE) {
1136 err = -EINVAL;
1137 goto done;
1138 }
1139
Marcel Holtmann10a8b862013-10-01 22:59:24 -07001140 if (!capable(CAP_NET_ADMIN)) {
Marcel Holtmann23500182013-08-26 21:40:52 -07001141 err = -EPERM;
1142 goto done;
1143 }
1144
1145 hdev = hci_dev_get(haddr.hci_dev);
1146 if (!hdev) {
1147 err = -ENODEV;
1148 goto done;
1149 }
1150
Marcel Holtmann781f8992015-06-06 06:06:49 +02001151 if (test_bit(HCI_INIT, &hdev->flags) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001152 hci_dev_test_flag(hdev, HCI_SETUP) ||
Marcel Holtmann781f8992015-06-06 06:06:49 +02001153 hci_dev_test_flag(hdev, HCI_CONFIG) ||
1154 (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1155 test_bit(HCI_UP, &hdev->flags))) {
Marcel Holtmann23500182013-08-26 21:40:52 -07001156 err = -EBUSY;
1157 hci_dev_put(hdev);
1158 goto done;
1159 }
1160
Marcel Holtmann238be782015-03-13 02:11:06 -07001161 if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
Marcel Holtmann23500182013-08-26 21:40:52 -07001162 err = -EUSERS;
1163 hci_dev_put(hdev);
1164 goto done;
1165 }
1166
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02001167 mgmt_index_removed(hdev);
Marcel Holtmann23500182013-08-26 21:40:52 -07001168
1169 err = hci_dev_open(hdev->id);
1170 if (err) {
Marcel Holtmann781f8992015-06-06 06:06:49 +02001171 if (err == -EALREADY) {
1172 /* In case the transport is already up and
1173 * running, clear the error here.
1174 *
1175 * This can happen when opening an user
1176 * channel and HCI_AUTO_OFF grace period
1177 * is still active.
1178 */
1179 err = 0;
1180 } else {
1181 hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1182 mgmt_index_added(hdev);
1183 hci_dev_put(hdev);
1184 goto done;
1185 }
Marcel Holtmann23500182013-08-26 21:40:52 -07001186 }
1187
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001188 hci_pi(sk)->channel = haddr.hci_channel;
Marcel Holtmannaa1638d2016-09-01 19:48:28 +02001189
1190 if (!hci_sock_gen_cookie(sk)) {
1191 /* In the case when a cookie has already been assigned,
1192 * this socket will transition from a raw socket into
1193 * an user channel socket. For a clean transition, send
1194 * the close notification first.
1195 */
1196 skb = create_monitor_ctrl_close(sk);
1197 if (skb) {
1198 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1199 HCI_SOCK_TRUSTED, NULL);
1200 kfree_skb(skb);
1201 }
1202 }
1203
1204 /* The user channel is restricted to CAP_NET_ADMIN
1205 * capabilities and with that implicitly trusted.
1206 */
1207 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1208
Marcel Holtmann23500182013-08-26 21:40:52 -07001209 hci_pi(sk)->hdev = hdev;
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001210
Marcel Holtmannaa1638d2016-09-01 19:48:28 +02001211 /* Send event to monitor */
1212 skb = create_monitor_ctrl_open(sk);
1213 if (skb) {
1214 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1215 HCI_SOCK_TRUSTED, NULL);
1216 kfree_skb(skb);
1217 }
1218
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001219 atomic_inc(&hdev->promisc);
Marcel Holtmann23500182013-08-26 21:40:52 -07001220 break;
1221
Marcel Holtmanncd82e612012-02-20 20:34:38 +01001222 case HCI_CHANNEL_MONITOR:
1223 if (haddr.hci_dev != HCI_DEV_NONE) {
1224 err = -EINVAL;
1225 goto done;
1226 }
1227
1228 if (!capable(CAP_NET_RAW)) {
1229 err = -EPERM;
1230 goto done;
1231 }
1232
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001233 hci_pi(sk)->channel = haddr.hci_channel;
1234
Marcel Holtmann50ebc052015-03-14 19:27:58 -07001235 /* The monitor interface is restricted to CAP_NET_RAW
1236 * capabilities and with that implicitly trusted.
1237 */
1238 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1239
Johannes Berg787b3062016-01-06 14:38:40 +01001240 send_monitor_note(sk, "Linux version %s (%s)",
1241 init_utsname()->release,
1242 init_utsname()->machine);
Marcel Holtmann9e8305b2016-08-30 05:00:35 +02001243 send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
1244 BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
Marcel Holtmanncd82e612012-02-20 20:34:38 +01001245 send_monitor_replay(sk);
Marcel Holtmann249fa162016-08-27 20:23:40 +02001246 send_monitor_control_replay(sk);
Marcel Holtmanncd82e612012-02-20 20:34:38 +01001247
1248 atomic_inc(&monitor_promisc);
1249 break;
1250
Marcel Holtmannac714942015-11-08 07:47:13 +01001251 case HCI_CHANNEL_LOGGING:
1252 if (haddr.hci_dev != HCI_DEV_NONE) {
1253 err = -EINVAL;
1254 goto done;
1255 }
1256
1257 if (!capable(CAP_NET_ADMIN)) {
1258 err = -EPERM;
1259 goto done;
1260 }
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001261
1262 hci_pi(sk)->channel = haddr.hci_channel;
Marcel Holtmannac714942015-11-08 07:47:13 +01001263 break;
1264
Marcel Holtmann7cc2ade2012-02-20 14:50:35 +01001265 default:
Johan Hedberg801c1e82015-03-06 21:08:50 +02001266 if (!hci_mgmt_chan_find(haddr.hci_channel)) {
1267 err = -EINVAL;
1268 goto done;
1269 }
1270
1271 if (haddr.hci_dev != HCI_DEV_NONE) {
1272 err = -EINVAL;
1273 goto done;
1274 }
1275
Marcel Holtmann1195fbb2015-03-14 19:28:04 -07001276 /* Users with CAP_NET_ADMIN capabilities are allowed
1277 * access to all management commands and events. For
1278 * untrusted users the interface is restricted and
1279 * also only untrusted events are sent.
Marcel Holtmann50ebc052015-03-14 19:27:58 -07001280 */
Marcel Holtmann1195fbb2015-03-14 19:28:04 -07001281 if (capable(CAP_NET_ADMIN))
1282 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
Marcel Holtmann50ebc052015-03-14 19:27:58 -07001283
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001284 hci_pi(sk)->channel = haddr.hci_channel;
1285
Marcel Holtmannf9207332015-03-14 19:27:55 -07001286 /* At the moment the index and unconfigured index events
1287 * are enabled unconditionally. Setting them on each
1288 * socket when binding keeps this functionality. They
1289 * however might be cleared later and then sending of these
1290 * events will be disabled, but that is then intentional.
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001291 *
1292 * This also enables generic events that are safe to be
1293 * received by untrusted users. Example for such events
1294 * are changes to settings, class of device, name etc.
Marcel Holtmannf9207332015-03-14 19:27:55 -07001295 */
Marcel Holtmann5a6d2cf2016-08-30 05:00:37 +02001296 if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
Marcel Holtmannf4cdbb32016-08-30 05:00:40 +02001297 if (!hci_sock_gen_cookie(sk)) {
1298 /* In the case when a cookie has already been
1299 * assigned, this socket will transtion from
1300 * a raw socket into a control socket. To
1301 * allow for a clean transtion, send the
1302 * close notification first.
1303 */
1304 skb = create_monitor_ctrl_close(sk);
1305 if (skb) {
1306 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1307 HCI_SOCK_TRUSTED, NULL);
1308 kfree_skb(skb);
1309 }
1310 }
Marcel Holtmann70ecce92016-08-27 20:23:38 +02001311
Marcel Holtmann249fa162016-08-27 20:23:40 +02001312 /* Send event to monitor */
1313 skb = create_monitor_ctrl_open(sk);
1314 if (skb) {
1315 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1316 HCI_SOCK_TRUSTED, NULL);
1317 kfree_skb(skb);
1318 }
1319
Marcel Holtmannf9207332015-03-14 19:27:55 -07001320 hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1321 hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02001322 hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
1323 hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
1324 hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
1325 hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
Marcel Holtmannf9207332015-03-14 19:27:55 -07001326 }
Johan Hedberg801c1e82015-03-06 21:08:50 +02001327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 sk->sk_state = BT_BOUND;
1331
1332done:
1333 release_sock(sk);
1334 return err;
1335}
1336
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001337static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
1338 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Marcel Holtmann8528d3f2015-11-08 07:47:11 +01001340 struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct sock *sk = sock->sk;
Marcel Holtmann9d4b68b2013-08-26 00:20:37 -07001342 struct hci_dev *hdev;
1343 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 BT_DBG("sock %p sk %p", sock, sk);
1346
Marcel Holtmann06f43cb2013-08-26 00:06:30 -07001347 if (peer)
1348 return -EOPNOTSUPP;
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 lock_sock(sk);
1351
Marcel Holtmann9d4b68b2013-08-26 00:20:37 -07001352 hdev = hci_pi(sk)->hdev;
1353 if (!hdev) {
1354 err = -EBADFD;
1355 goto done;
1356 }
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *addr_len = sizeof(*haddr);
1359 haddr->hci_family = AF_BLUETOOTH;
Marcel Holtmann7b005bd2006-02-13 11:40:03 +01001360 haddr->hci_dev = hdev->id;
Marcel Holtmann9d4b68b2013-08-26 00:20:37 -07001361 haddr->hci_channel= hci_pi(sk)->channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Marcel Holtmann9d4b68b2013-08-26 00:20:37 -07001363done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 release_sock(sk);
Marcel Holtmann9d4b68b2013-08-26 00:20:37 -07001365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001368static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
1369 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 __u32 mask = hci_pi(sk)->cmsg_mask;
1372
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001373 if (mask & HCI_CMSG_DIR) {
1374 int incoming = bt_cb(skb)->incoming;
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001375 put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
1376 &incoming);
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001379 if (mask & HCI_CMSG_TSTAMP) {
Johann Felix Sodenf6e623a2010-02-15 22:23:48 +01001380#ifdef CONFIG_COMPAT
1381 struct compat_timeval ctv;
1382#endif
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001383 struct timeval tv;
Marcel Holtmann767c5eb2007-09-09 08:39:34 +02001384 void *data;
1385 int len;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001386
1387 skb_get_timestamp(skb, &tv);
Marcel Holtmann767c5eb2007-09-09 08:39:34 +02001388
David S. Miller1da97f82007-09-12 14:10:58 +02001389 data = &tv;
1390 len = sizeof(tv);
1391#ifdef CONFIG_COMPAT
H. J. Luda88cea2012-02-10 14:12:15 -08001392 if (!COMPAT_USE_64BIT_TIME &&
1393 (msg->msg_flags & MSG_CMSG_COMPAT)) {
Marcel Holtmann767c5eb2007-09-09 08:39:34 +02001394 ctv.tv_sec = tv.tv_sec;
1395 ctv.tv_usec = tv.tv_usec;
1396 data = &ctv;
1397 len = sizeof(ctv);
Marcel Holtmann767c5eb2007-09-09 08:39:34 +02001398 }
David S. Miller1da97f82007-09-12 14:10:58 +02001399#endif
Marcel Holtmann767c5eb2007-09-09 08:39:34 +02001400
1401 put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001404
Marcel Holtmann8528d3f2015-11-08 07:47:11 +01001405static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1406 size_t len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 int noblock = flags & MSG_DONTWAIT;
1409 struct sock *sk = sock->sk;
1410 struct sk_buff *skb;
1411 int copied, err;
Denis Kenzior83871f82016-06-27 11:01:13 -05001412 unsigned int skblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 BT_DBG("sock %p, sk %p", sock, sk);
1415
Marcel Holtmannd94a6102015-10-25 22:45:18 +01001416 if (flags & MSG_OOB)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EOPNOTSUPP;
1418
Marcel Holtmannac714942015-11-08 07:47:13 +01001419 if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1420 return -EOPNOTSUPP;
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (sk->sk_state == BT_CLOSED)
1423 return 0;
1424
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001425 skb = skb_recv_datagram(sk, flags, noblock, &err);
1426 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return err;
1428
Denis Kenzior83871f82016-06-27 11:01:13 -05001429 skblen = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 copied = skb->len;
1431 if (len < copied) {
1432 msg->msg_flags |= MSG_TRUNC;
1433 copied = len;
1434 }
1435
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001436 skb_reset_transport_header(skb);
David S. Miller51f3d022014-11-05 16:46:40 -05001437 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Marcel Holtmann3a208622012-02-20 14:50:34 +01001439 switch (hci_pi(sk)->channel) {
1440 case HCI_CHANNEL_RAW:
1441 hci_sock_cmsg(sk, msg, skb);
1442 break;
Marcel Holtmann23500182013-08-26 21:40:52 -07001443 case HCI_CHANNEL_USER:
Marcel Holtmanncd82e612012-02-20 20:34:38 +01001444 case HCI_CHANNEL_MONITOR:
1445 sock_recv_timestamp(msg, sk, skb);
1446 break;
Johan Hedberg801c1e82015-03-06 21:08:50 +02001447 default:
1448 if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1449 sock_recv_timestamp(msg, sk, skb);
1450 break;
Marcel Holtmann3a208622012-02-20 14:50:34 +01001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 skb_free_datagram(sk, skb);
1454
Luiz Augusto von Dentz4f342282016-08-15 16:02:20 +03001455 if (flags & MSG_TRUNC)
Denis Kenzior83871f82016-06-27 11:01:13 -05001456 copied = skblen;
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return err ? : copied;
1459}
1460
Johan Hedbergfa4335d2015-03-17 13:48:50 +02001461static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
1462 struct msghdr *msg, size_t msglen)
1463{
1464 void *buf;
1465 u8 *cp;
1466 struct mgmt_hdr *hdr;
1467 u16 opcode, index, len;
1468 struct hci_dev *hdev = NULL;
1469 const struct hci_mgmt_handler *handler;
1470 bool var_len, no_hdev;
1471 int err;
1472
1473 BT_DBG("got %zu bytes", msglen);
1474
1475 if (msglen < sizeof(*hdr))
1476 return -EINVAL;
1477
1478 buf = kmalloc(msglen, GFP_KERNEL);
1479 if (!buf)
1480 return -ENOMEM;
1481
1482 if (memcpy_from_msg(buf, msg, msglen)) {
1483 err = -EFAULT;
1484 goto done;
1485 }
1486
1487 hdr = buf;
1488 opcode = __le16_to_cpu(hdr->opcode);
1489 index = __le16_to_cpu(hdr->index);
1490 len = __le16_to_cpu(hdr->len);
1491
1492 if (len != msglen - sizeof(*hdr)) {
1493 err = -EINVAL;
1494 goto done;
1495 }
1496
Marcel Holtmann38ceaa02016-08-27 20:23:41 +02001497 if (chan->channel == HCI_CHANNEL_CONTROL) {
1498 struct sk_buff *skb;
1499
1500 /* Send event to monitor */
1501 skb = create_monitor_ctrl_command(sk, index, opcode, len,
1502 buf + sizeof(*hdr));
1503 if (skb) {
1504 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1505 HCI_SOCK_TRUSTED, NULL);
1506 kfree_skb(skb);
1507 }
1508 }
1509
Johan Hedbergfa4335d2015-03-17 13:48:50 +02001510 if (opcode >= chan->handler_count ||
1511 chan->handlers[opcode].func == NULL) {
1512 BT_DBG("Unknown op %u", opcode);
1513 err = mgmt_cmd_status(sk, index, opcode,
1514 MGMT_STATUS_UNKNOWN_COMMAND);
1515 goto done;
1516 }
1517
1518 handler = &chan->handlers[opcode];
1519
1520 if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1521 !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1522 err = mgmt_cmd_status(sk, index, opcode,
1523 MGMT_STATUS_PERMISSION_DENIED);
1524 goto done;
1525 }
1526
1527 if (index != MGMT_INDEX_NONE) {
1528 hdev = hci_dev_get(index);
1529 if (!hdev) {
1530 err = mgmt_cmd_status(sk, index, opcode,
1531 MGMT_STATUS_INVALID_INDEX);
1532 goto done;
1533 }
1534
1535 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1536 hci_dev_test_flag(hdev, HCI_CONFIG) ||
1537 hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1538 err = mgmt_cmd_status(sk, index, opcode,
1539 MGMT_STATUS_INVALID_INDEX);
1540 goto done;
1541 }
1542
1543 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1544 !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1545 err = mgmt_cmd_status(sk, index, opcode,
1546 MGMT_STATUS_INVALID_INDEX);
1547 goto done;
1548 }
1549 }
1550
1551 no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1552 if (no_hdev != !hdev) {
1553 err = mgmt_cmd_status(sk, index, opcode,
1554 MGMT_STATUS_INVALID_INDEX);
1555 goto done;
1556 }
1557
1558 var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1559 if ((var_len && len < handler->data_len) ||
1560 (!var_len && len != handler->data_len)) {
1561 err = mgmt_cmd_status(sk, index, opcode,
1562 MGMT_STATUS_INVALID_PARAMS);
1563 goto done;
1564 }
1565
1566 if (hdev && chan->hdev_init)
1567 chan->hdev_init(sk, hdev);
1568
1569 cp = buf + sizeof(*hdr);
1570
1571 err = handler->func(sk, hdev, cp, len);
1572 if (err < 0)
1573 goto done;
1574
1575 err = msglen;
1576
1577done:
1578 if (hdev)
1579 hci_dev_put(hdev);
1580
1581 kfree(buf);
1582 return err;
1583}
1584
Marcel Holtmannac714942015-11-08 07:47:13 +01001585static int hci_logging_frame(struct sock *sk, struct msghdr *msg, int len)
1586{
1587 struct hci_mon_hdr *hdr;
1588 struct sk_buff *skb;
1589 struct hci_dev *hdev;
1590 u16 index;
1591 int err;
1592
1593 /* The logging frame consists at minimum of the standard header,
1594 * the priority byte, the ident length byte and at least one string
1595 * terminator NUL byte. Anything shorter are invalid packets.
1596 */
1597 if (len < sizeof(*hdr) + 3)
1598 return -EINVAL;
1599
1600 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
1601 if (!skb)
1602 return err;
1603
1604 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1605 err = -EFAULT;
1606 goto drop;
1607 }
1608
1609 hdr = (void *)skb->data;
1610
1611 if (__le16_to_cpu(hdr->len) != len - sizeof(*hdr)) {
1612 err = -EINVAL;
1613 goto drop;
1614 }
1615
1616 if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1617 __u8 priority = skb->data[sizeof(*hdr)];
1618 __u8 ident_len = skb->data[sizeof(*hdr) + 1];
1619
1620 /* Only the priorities 0-7 are valid and with that any other
1621 * value results in an invalid packet.
1622 *
1623 * The priority byte is followed by an ident length byte and
1624 * the NUL terminated ident string. Check that the ident
1625 * length is not overflowing the packet and also that the
1626 * ident string itself is NUL terminated. In case the ident
1627 * length is zero, the length value actually doubles as NUL
1628 * terminator identifier.
1629 *
1630 * The message follows the ident string (if present) and
1631 * must be NUL terminated. Otherwise it is not a valid packet.
1632 */
1633 if (priority > 7 || skb->data[len - 1] != 0x00 ||
1634 ident_len > len - sizeof(*hdr) - 3 ||
1635 skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) {
1636 err = -EINVAL;
1637 goto drop;
1638 }
1639 } else {
1640 err = -EINVAL;
1641 goto drop;
1642 }
1643
1644 index = __le16_to_cpu(hdr->index);
1645
1646 if (index != MGMT_INDEX_NONE) {
1647 hdev = hci_dev_get(index);
1648 if (!hdev) {
1649 err = -ENODEV;
1650 goto drop;
1651 }
1652 } else {
1653 hdev = NULL;
1654 }
1655
1656 hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1657
1658 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
1659 err = len;
1660
1661 if (hdev)
1662 hci_dev_put(hdev);
1663
1664drop:
1665 kfree_skb(skb);
1666 return err;
1667}
1668
Ying Xue1b784142015-03-02 15:37:48 +08001669static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1670 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct sock *sk = sock->sk;
Johan Hedberg801c1e82015-03-06 21:08:50 +02001673 struct hci_mgmt_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct hci_dev *hdev;
1675 struct sk_buff *skb;
1676 int err;
1677
1678 BT_DBG("sock %p sk %p", sock, sk);
1679
1680 if (msg->msg_flags & MSG_OOB)
1681 return -EOPNOTSUPP;
1682
Szymon Janc518ca842017-04-24 18:25:04 -07001683 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE|
1684 MSG_CMSG_COMPAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return -EINVAL;
1686
1687 if (len < 4 || len > HCI_MAX_FRAME_SIZE)
1688 return -EINVAL;
1689
1690 lock_sock(sk);
1691
Johan Hedberg03811012010-12-08 00:21:06 +02001692 switch (hci_pi(sk)->channel) {
1693 case HCI_CHANNEL_RAW:
Marcel Holtmann23500182013-08-26 21:40:52 -07001694 case HCI_CHANNEL_USER:
Johan Hedberg03811012010-12-08 00:21:06 +02001695 break;
Marcel Holtmanncd82e612012-02-20 20:34:38 +01001696 case HCI_CHANNEL_MONITOR:
1697 err = -EOPNOTSUPP;
1698 goto done;
Marcel Holtmannac714942015-11-08 07:47:13 +01001699 case HCI_CHANNEL_LOGGING:
1700 err = hci_logging_frame(sk, msg, len);
1701 goto done;
Johan Hedberg03811012010-12-08 00:21:06 +02001702 default:
Johan Hedberg801c1e82015-03-06 21:08:50 +02001703 mutex_lock(&mgmt_chan_list_lock);
1704 chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1705 if (chan)
Johan Hedbergfa4335d2015-03-17 13:48:50 +02001706 err = hci_mgmt_cmd(chan, sk, msg, len);
Johan Hedberg801c1e82015-03-06 21:08:50 +02001707 else
1708 err = -EINVAL;
1709
1710 mutex_unlock(&mgmt_chan_list_lock);
Johan Hedberg03811012010-12-08 00:21:06 +02001711 goto done;
1712 }
1713
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001714 hdev = hci_pi(sk)->hdev;
1715 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 err = -EBADFD;
1717 goto done;
1718 }
1719
Marcel Holtmann7e21add2009-11-18 01:05:00 +01001720 if (!test_bit(HCI_UP, &hdev->flags)) {
1721 err = -ENETDOWN;
1722 goto done;
1723 }
1724
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001725 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
1726 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto done;
1728
Al Viro6ce8e9c2014-04-06 21:25:44 -04001729 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 err = -EFAULT;
1731 goto drop;
1732 }
1733
Marcel Holtmann8528d3f2015-11-08 07:47:11 +01001734 hci_skb_pkt_type(skb) = skb->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 skb_pull(skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Marcel Holtmann1bc5ad12013-12-17 03:21:25 -08001737 if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
1738 /* No permission check is needed for user channel
1739 * since that gets enforced when binding the socket.
1740 *
1741 * However check that the packet type is valid.
1742 */
Marcel Holtmannd79f34e2015-11-05 07:10:00 +01001743 if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1744 hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1745 hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
Marcel Holtmann1bc5ad12013-12-17 03:21:25 -08001746 err = -EINVAL;
1747 goto drop;
1748 }
1749
1750 skb_queue_tail(&hdev->raw_q, skb);
1751 queue_work(hdev->workqueue, &hdev->tx_work);
Marcel Holtmannd79f34e2015-11-05 07:10:00 +01001752 } else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
Harvey Harrison83985312008-05-02 16:25:46 -07001753 u16 opcode = get_unaligned_le16(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 u16 ogf = hci_opcode_ogf(opcode);
1755 u16 ocf = hci_opcode_ocf(opcode);
1756
1757 if (((ogf > HCI_SFLT_MAX_OGF) ||
Gustavo Padovan3bb3c752012-05-17 00:36:22 -03001758 !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
1759 &hci_sec_filter.ocf_mask[ogf])) &&
1760 !capable(CAP_NET_RAW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 err = -EPERM;
1762 goto drop;
1763 }
1764
Marcel Holtmann19821622015-11-06 07:42:20 +01001765 /* Since the opcode has already been extracted here, store
1766 * a copy of the value for later use by the drivers.
1767 */
1768 hci_skb_opcode(skb) = opcode;
1769
Marcel Holtmannfee746b2014-06-29 12:13:05 +02001770 if (ogf == 0x3f) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 skb_queue_tail(&hdev->raw_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02001772 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 } else {
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001774 /* Stand-alone HCI commands must be flagged as
Johan Hedberg11714b32013-03-05 20:37:47 +02001775 * single-command requests.
1776 */
Johan Hedberg44d27132015-11-05 09:31:40 +02001777 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
Johan Hedberg11714b32013-03-05 20:37:47 +02001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 skb_queue_tail(&hdev->cmd_q, skb);
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02001780 queue_work(hdev->workqueue, &hdev->cmd_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782 } else {
1783 if (!capable(CAP_NET_RAW)) {
1784 err = -EPERM;
1785 goto drop;
1786 }
1787
Marcel Holtmannd79f34e2015-11-05 07:10:00 +01001788 if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1789 hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
Marcel Holtmannbb775432015-10-09 16:13:50 +02001790 err = -EINVAL;
1791 goto drop;
1792 }
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 skb_queue_tail(&hdev->raw_q, skb);
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02001795 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 err = len;
1799
1800done:
1801 release_sock(sk);
1802 return err;
1803
1804drop:
1805 kfree_skb(skb);
1806 goto done;
1807}
1808
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001809static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
1810 char __user *optval, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct hci_ufilter uf = { .opcode = 0 };
1813 struct sock *sk = sock->sk;
1814 int err = 0, opt = 0;
1815
1816 BT_DBG("sk %p, opt %d", sk, optname);
1817
Marcel Holtmann47b0f572016-08-27 20:23:37 +02001818 if (level != SOL_HCI)
1819 return -ENOPROTOOPT;
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 lock_sock(sk);
1822
Marcel Holtmann2f39cdb2012-02-20 14:50:32 +01001823 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
Marcel Holtmannc2371e82013-08-26 09:29:39 -07001824 err = -EBADFD;
Marcel Holtmann2f39cdb2012-02-20 14:50:32 +01001825 goto done;
1826 }
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 switch (optname) {
1829 case HCI_DATA_DIR:
1830 if (get_user(opt, (int __user *)optval)) {
1831 err = -EFAULT;
1832 break;
1833 }
1834
1835 if (opt)
1836 hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
1837 else
1838 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
1839 break;
1840
1841 case HCI_TIME_STAMP:
1842 if (get_user(opt, (int __user *)optval)) {
1843 err = -EFAULT;
1844 break;
1845 }
1846
1847 if (opt)
1848 hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
1849 else
1850 hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
1851 break;
1852
1853 case HCI_FILTER:
Marcel Holtmann0878b662007-05-05 00:35:59 +02001854 {
1855 struct hci_filter *f = &hci_pi(sk)->filter;
1856
1857 uf.type_mask = f->type_mask;
1858 uf.opcode = f->opcode;
1859 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
1860 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
1861 }
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 len = min_t(unsigned int, len, sizeof(uf));
1864 if (copy_from_user(&uf, optval, len)) {
1865 err = -EFAULT;
1866 break;
1867 }
1868
1869 if (!capable(CAP_NET_RAW)) {
1870 uf.type_mask &= hci_sec_filter.type_mask;
1871 uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
1872 uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
1873 }
1874
1875 {
1876 struct hci_filter *f = &hci_pi(sk)->filter;
1877
1878 f->type_mask = uf.type_mask;
1879 f->opcode = uf.opcode;
1880 *((u32 *) f->event_mask + 0) = uf.event_mask[0];
1881 *((u32 *) f->event_mask + 1) = uf.event_mask[1];
1882 }
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 default:
1886 err = -ENOPROTOOPT;
1887 break;
1888 }
1889
Marcel Holtmann2f39cdb2012-02-20 14:50:32 +01001890done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 release_sock(sk);
1892 return err;
1893}
1894
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001895static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
1896 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct hci_ufilter uf;
1899 struct sock *sk = sock->sk;
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001900 int len, opt, err = 0;
1901
1902 BT_DBG("sk %p, opt %d", sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Marcel Holtmann47b0f572016-08-27 20:23:37 +02001904 if (level != SOL_HCI)
1905 return -ENOPROTOOPT;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (get_user(len, optlen))
1908 return -EFAULT;
1909
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001910 lock_sock(sk);
1911
1912 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
Marcel Holtmannc2371e82013-08-26 09:29:39 -07001913 err = -EBADFD;
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001914 goto done;
1915 }
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 switch (optname) {
1918 case HCI_DATA_DIR:
1919 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
1920 opt = 1;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001921 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 opt = 0;
1923
1924 if (put_user(opt, optval))
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001925 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927
1928 case HCI_TIME_STAMP:
1929 if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
1930 opt = 1;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001931 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 opt = 0;
1933
1934 if (put_user(opt, optval))
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001935 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 break;
1937
1938 case HCI_FILTER:
1939 {
1940 struct hci_filter *f = &hci_pi(sk)->filter;
1941
Mathias Krausee15ca9a2012-08-15 11:31:46 +00001942 memset(&uf, 0, sizeof(uf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 uf.type_mask = f->type_mask;
1944 uf.opcode = f->opcode;
1945 uf.event_mask[0] = *((u32 *) f->event_mask + 0);
1946 uf.event_mask[1] = *((u32 *) f->event_mask + 1);
1947 }
1948
1949 len = min_t(unsigned int, len, sizeof(uf));
1950 if (copy_to_user(optval, &uf, len))
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001951 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
1953
1954 default:
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001955 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
1957 }
1958
Marcel Holtmanncedc5462012-02-20 14:50:33 +01001959done:
1960 release_sock(sk);
1961 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001964static const struct proto_ops hci_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 .family = PF_BLUETOOTH,
1966 .owner = THIS_MODULE,
1967 .release = hci_sock_release,
1968 .bind = hci_sock_bind,
1969 .getname = hci_sock_getname,
1970 .sendmsg = hci_sock_sendmsg,
1971 .recvmsg = hci_sock_recvmsg,
1972 .ioctl = hci_sock_ioctl,
1973 .poll = datagram_poll,
1974 .listen = sock_no_listen,
1975 .shutdown = sock_no_shutdown,
1976 .setsockopt = hci_sock_setsockopt,
1977 .getsockopt = hci_sock_getsockopt,
1978 .connect = sock_no_connect,
1979 .socketpair = sock_no_socketpair,
1980 .accept = sock_no_accept,
1981 .mmap = sock_no_mmap
1982};
1983
1984static struct proto hci_sk_proto = {
1985 .name = "HCI",
1986 .owner = THIS_MODULE,
1987 .obj_size = sizeof(struct hci_pinfo)
1988};
1989
Eric Paris3f378b62009-11-05 22:18:14 -08001990static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
1991 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct sock *sk;
1994
1995 BT_DBG("sock %p", sock);
1996
1997 if (sock->type != SOCK_RAW)
1998 return -ESOCKTNOSUPPORT;
1999
2000 sock->ops = &hci_sock_ops;
2001
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05002002 sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (!sk)
2004 return -ENOMEM;
2005
2006 sock_init_data(sock, sk);
2007
2008 sock_reset_flag(sk, SOCK_ZAPPED);
2009
2010 sk->sk_protocol = protocol;
2011
2012 sock->state = SS_UNCONNECTED;
2013 sk->sk_state = BT_OPEN;
2014
2015 bt_sock_link(&hci_sk_list, sk);
2016 return 0;
2017}
2018
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002019static const struct net_proto_family hci_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 .family = PF_BLUETOOTH,
2021 .owner = THIS_MODULE,
2022 .create = hci_sock_create,
2023};
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025int __init hci_sock_init(void)
2026{
2027 int err;
2028
Marcel Holtmannb0a8e282015-01-11 15:18:17 -08002029 BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 err = proto_register(&hci_sk_proto, 0);
2032 if (err < 0)
2033 return err;
2034
2035 err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
Masatake YAMATOf7c86632012-07-26 01:28:36 +09002036 if (err < 0) {
2037 BT_ERR("HCI socket registration failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto error;
Masatake YAMATOf7c86632012-07-26 01:28:36 +09002039 }
2040
Al Virob0316612013-04-04 19:14:33 -04002041 err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
Masatake YAMATOf7c86632012-07-26 01:28:36 +09002042 if (err < 0) {
2043 BT_ERR("Failed to create HCI proc file");
2044 bt_sock_unregister(BTPROTO_HCI);
2045 goto error;
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 BT_INFO("HCI socket layer initialized");
2049
2050 return 0;
2051
2052error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 proto_unregister(&hci_sk_proto);
2054 return err;
2055}
2056
Anand Gadiyarb7440a142011-02-22 12:43:09 +05302057void hci_sock_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Masatake YAMATOf7c86632012-07-26 01:28:36 +09002059 bt_procfs_cleanup(&init_net, "hci");
David Herrmann5e9d7f82013-02-24 19:36:51 +01002060 bt_sock_unregister(BTPROTO_HCI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 proto_unregister(&hci_sk_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}