xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision a11e1d432b51f63ba698d044441284a661f01144)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds    Copyright (C) 2000-2001 Qualcomm Incorporated
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds    This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds    it under the terms of the GNU General Public License version 2 as
91da177e4SLinus Torvalds    published by the Free Software Foundation;
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
121da177e4SLinus Torvalds    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131da177e4SLinus Torvalds    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
141da177e4SLinus Torvalds    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
151da177e4SLinus Torvalds    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
161da177e4SLinus Torvalds    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
171da177e4SLinus Torvalds    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
181da177e4SLinus Torvalds    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
211da177e4SLinus Torvalds    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
221da177e4SLinus Torvalds    SOFTWARE IS DISCLAIMED.
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* Bluetooth HCI sockets. */
261da177e4SLinus Torvalds 
278c520a59SGustavo Padovan #include <linux/export.h>
28787b306cSJohannes Berg #include <linux/utsname.h>
2970ecce91SMarcel Holtmann #include <linux/sched.h>
301da177e4SLinus Torvalds #include <asm/unaligned.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
331da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
34cd82e61cSMarcel Holtmann #include <net/bluetooth/hci_mon.h>
35fa4335d7SJohan Hedberg #include <net/bluetooth/mgmt.h>
36fa4335d7SJohan Hedberg 
37fa4335d7SJohan Hedberg #include "mgmt_util.h"
381da177e4SLinus Torvalds 
39801c1e8dSJohan Hedberg static LIST_HEAD(mgmt_chan_list);
40801c1e8dSJohan Hedberg static DEFINE_MUTEX(mgmt_chan_list_lock);
41801c1e8dSJohan Hedberg 
4270ecce91SMarcel Holtmann static DEFINE_IDA(sock_cookie_ida);
4370ecce91SMarcel Holtmann 
44cd82e61cSMarcel Holtmann static atomic_t monitor_promisc = ATOMIC_INIT(0);
45cd82e61cSMarcel Holtmann 
461da177e4SLinus Torvalds /* ----- HCI socket interface ----- */
471da177e4SLinus Torvalds 
48863def58SMarcel Holtmann /* Socket info */
49863def58SMarcel Holtmann #define hci_pi(sk) ((struct hci_pinfo *) sk)
50863def58SMarcel Holtmann 
51863def58SMarcel Holtmann struct hci_pinfo {
52863def58SMarcel Holtmann 	struct bt_sock    bt;
53863def58SMarcel Holtmann 	struct hci_dev    *hdev;
54863def58SMarcel Holtmann 	struct hci_filter filter;
55863def58SMarcel Holtmann 	__u32             cmsg_mask;
56863def58SMarcel Holtmann 	unsigned short    channel;
576befc644SMarcel Holtmann 	unsigned long     flags;
5870ecce91SMarcel Holtmann 	__u32             cookie;
5970ecce91SMarcel Holtmann 	char              comm[TASK_COMM_LEN];
60863def58SMarcel Holtmann };
61863def58SMarcel Holtmann 
626befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr)
636befc644SMarcel Holtmann {
646befc644SMarcel Holtmann 	set_bit(nr, &hci_pi(sk)->flags);
656befc644SMarcel Holtmann }
666befc644SMarcel Holtmann 
676befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr)
686befc644SMarcel Holtmann {
696befc644SMarcel Holtmann 	clear_bit(nr, &hci_pi(sk)->flags);
706befc644SMarcel Holtmann }
716befc644SMarcel Holtmann 
72c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr)
73c85be545SMarcel Holtmann {
74c85be545SMarcel Holtmann 	return test_bit(nr, &hci_pi(sk)->flags);
75c85be545SMarcel Holtmann }
76c85be545SMarcel Holtmann 
77d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk)
78d0f172b1SJohan Hedberg {
79d0f172b1SJohan Hedberg 	return hci_pi(sk)->channel;
80d0f172b1SJohan Hedberg }
81d0f172b1SJohan Hedberg 
8270ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk)
8370ecce91SMarcel Holtmann {
8470ecce91SMarcel Holtmann 	return hci_pi(sk)->cookie;
8570ecce91SMarcel Holtmann }
8670ecce91SMarcel Holtmann 
87df1cb87aSMarcel Holtmann static bool hci_sock_gen_cookie(struct sock *sk)
88df1cb87aSMarcel Holtmann {
89df1cb87aSMarcel Holtmann 	int id = hci_pi(sk)->cookie;
90df1cb87aSMarcel Holtmann 
91df1cb87aSMarcel Holtmann 	if (!id) {
92df1cb87aSMarcel Holtmann 		id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
93df1cb87aSMarcel Holtmann 		if (id < 0)
94df1cb87aSMarcel Holtmann 			id = 0xffffffff;
95df1cb87aSMarcel Holtmann 
96df1cb87aSMarcel Holtmann 		hci_pi(sk)->cookie = id;
97df1cb87aSMarcel Holtmann 		get_task_comm(hci_pi(sk)->comm, current);
98df1cb87aSMarcel Holtmann 		return true;
99df1cb87aSMarcel Holtmann 	}
100df1cb87aSMarcel Holtmann 
101df1cb87aSMarcel Holtmann 	return false;
102df1cb87aSMarcel Holtmann }
103df1cb87aSMarcel Holtmann 
104df1cb87aSMarcel Holtmann static void hci_sock_free_cookie(struct sock *sk)
105df1cb87aSMarcel Holtmann {
106df1cb87aSMarcel Holtmann 	int id = hci_pi(sk)->cookie;
107df1cb87aSMarcel Holtmann 
108df1cb87aSMarcel Holtmann 	if (id) {
109df1cb87aSMarcel Holtmann 		hci_pi(sk)->cookie = 0xffffffff;
110df1cb87aSMarcel Holtmann 		ida_simple_remove(&sock_cookie_ida, id);
111df1cb87aSMarcel Holtmann 	}
112df1cb87aSMarcel Holtmann }
113df1cb87aSMarcel Holtmann 
1149391976aSJiri Slaby static inline int hci_test_bit(int nr, const void *addr)
1151da177e4SLinus Torvalds {
1169391976aSJiri Slaby 	return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* Security filter */
1203ad254f7SMarcel Holtmann #define HCI_SFLT_MAX_OGF  5
1213ad254f7SMarcel Holtmann 
1223ad254f7SMarcel Holtmann struct hci_sec_filter {
1233ad254f7SMarcel Holtmann 	__u32 type_mask;
1243ad254f7SMarcel Holtmann 	__u32 event_mask[2];
1253ad254f7SMarcel Holtmann 	__u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
1263ad254f7SMarcel Holtmann };
1273ad254f7SMarcel Holtmann 
1287e67c112SMarcel Holtmann static const struct hci_sec_filter hci_sec_filter = {
1291da177e4SLinus Torvalds 	/* Packet types */
1301da177e4SLinus Torvalds 	0x10,
1311da177e4SLinus Torvalds 	/* Events */
132dd7f5527SMarcel Holtmann 	{ 0x1000d9fe, 0x0000b00c },
1331da177e4SLinus Torvalds 	/* Commands */
1341da177e4SLinus Torvalds 	{
1351da177e4SLinus Torvalds 		{ 0x0 },
1361da177e4SLinus Torvalds 		/* OGF_LINK_CTL */
1377c631a67SMarcel Holtmann 		{ 0xbe000006, 0x00000001, 0x00000000, 0x00 },
1381da177e4SLinus Torvalds 		/* OGF_LINK_POLICY */
1397c631a67SMarcel Holtmann 		{ 0x00005200, 0x00000000, 0x00000000, 0x00 },
1401da177e4SLinus Torvalds 		/* OGF_HOST_CTL */
1417c631a67SMarcel Holtmann 		{ 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
1421da177e4SLinus Torvalds 		/* OGF_INFO_PARAM */
1437c631a67SMarcel Holtmann 		{ 0x000002be, 0x00000000, 0x00000000, 0x00 },
1441da177e4SLinus Torvalds 		/* OGF_STATUS_PARAM */
1457c631a67SMarcel Holtmann 		{ 0x000000ea, 0x00000000, 0x00000000, 0x00 }
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds };
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static struct bt_sock_list hci_sk_list = {
150d5fb2962SRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
1511da177e4SLinus Torvalds };
1521da177e4SLinus Torvalds 
153f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
154f81fe64fSMarcel Holtmann {
155f81fe64fSMarcel Holtmann 	struct hci_filter *flt;
156f81fe64fSMarcel Holtmann 	int flt_type, flt_event;
157f81fe64fSMarcel Holtmann 
158f81fe64fSMarcel Holtmann 	/* Apply filter */
159f81fe64fSMarcel Holtmann 	flt = &hci_pi(sk)->filter;
160f81fe64fSMarcel Holtmann 
161d79f34e3SMarcel Holtmann 	flt_type = hci_skb_pkt_type(skb) & HCI_FLT_TYPE_BITS;
162f81fe64fSMarcel Holtmann 
163f81fe64fSMarcel Holtmann 	if (!test_bit(flt_type, &flt->type_mask))
164f81fe64fSMarcel Holtmann 		return true;
165f81fe64fSMarcel Holtmann 
166f81fe64fSMarcel Holtmann 	/* Extra filter for event packets only */
167d79f34e3SMarcel Holtmann 	if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT)
168f81fe64fSMarcel Holtmann 		return false;
169f81fe64fSMarcel Holtmann 
170f81fe64fSMarcel Holtmann 	flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
171f81fe64fSMarcel Holtmann 
172f81fe64fSMarcel Holtmann 	if (!hci_test_bit(flt_event, &flt->event_mask))
173f81fe64fSMarcel Holtmann 		return true;
174f81fe64fSMarcel Holtmann 
175f81fe64fSMarcel Holtmann 	/* Check filter only when opcode is set */
176f81fe64fSMarcel Holtmann 	if (!flt->opcode)
177f81fe64fSMarcel Holtmann 		return false;
178f81fe64fSMarcel Holtmann 
179f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_COMPLETE &&
180f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
181f81fe64fSMarcel Holtmann 		return true;
182f81fe64fSMarcel Holtmann 
183f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_STATUS &&
184f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
185f81fe64fSMarcel Holtmann 		return true;
186f81fe64fSMarcel Holtmann 
187f81fe64fSMarcel Holtmann 	return false;
188f81fe64fSMarcel Holtmann }
189f81fe64fSMarcel Holtmann 
1901da177e4SLinus Torvalds /* Send frame to RAW socket */
191470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	struct sock *sk;
194e0edf373SMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	BT_DBG("hdev %p len %d", hdev, skb->len);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	read_lock(&hci_sk_list.lock);
199470fe1b5SMarcel Holtmann 
200b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
2011da177e4SLinus Torvalds 		struct sk_buff *nskb;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 		if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
2041da177e4SLinus Torvalds 			continue;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		/* Don't send frame to the socket it came from */
2071da177e4SLinus Torvalds 		if (skb->sk == sk)
2081da177e4SLinus Torvalds 			continue;
2091da177e4SLinus Torvalds 
21023500189SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
211d79f34e3SMarcel Holtmann 			if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
212d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
213d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
214d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT)
215bb77543eSMarcel Holtmann 				continue;
216f81fe64fSMarcel Holtmann 			if (is_filtered_packet(sk, skb))
2171da177e4SLinus Torvalds 				continue;
21823500189SMarcel Holtmann 		} else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
21923500189SMarcel Holtmann 			if (!bt_cb(skb)->incoming)
22023500189SMarcel Holtmann 				continue;
221d79f34e3SMarcel Holtmann 			if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
222d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
223d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT)
22423500189SMarcel Holtmann 				continue;
22523500189SMarcel Holtmann 		} else {
22623500189SMarcel Holtmann 			/* Don't send frame to other channel types */
22723500189SMarcel Holtmann 			continue;
22823500189SMarcel Holtmann 		}
2291da177e4SLinus Torvalds 
230e0edf373SMarcel Holtmann 		if (!skb_copy) {
231e0edf373SMarcel Holtmann 			/* Create a private copy with headroom */
232bad93e9dSOctavian Purdila 			skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true);
233e0edf373SMarcel Holtmann 			if (!skb_copy)
2341da177e4SLinus Torvalds 				continue;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 			/* Put type byte before the data */
237d79f34e3SMarcel Holtmann 			memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1);
238e0edf373SMarcel Holtmann 		}
239e0edf373SMarcel Holtmann 
240e0edf373SMarcel Holtmann 		nskb = skb_clone(skb_copy, GFP_ATOMIC);
241e0edf373SMarcel Holtmann 		if (!nskb)
242e0edf373SMarcel Holtmann 			continue;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 		if (sock_queue_rcv_skb(sk, nskb))
2451da177e4SLinus Torvalds 			kfree_skb(nskb);
2461da177e4SLinus Torvalds 	}
247470fe1b5SMarcel Holtmann 
248470fe1b5SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
249e0edf373SMarcel Holtmann 
250e0edf373SMarcel Holtmann 	kfree_skb(skb_copy);
251470fe1b5SMarcel Holtmann }
252470fe1b5SMarcel Holtmann 
2537129069eSJohan Hedberg /* Send frame to sockets with specific channel */
254a9ee77afSSebastian Andrzej Siewior static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
255c08b1a1dSMarcel Holtmann 				  int flag, struct sock *skip_sk)
256470fe1b5SMarcel Holtmann {
257470fe1b5SMarcel Holtmann 	struct sock *sk;
258470fe1b5SMarcel Holtmann 
2597129069eSJohan Hedberg 	BT_DBG("channel %u len %d", channel, skb->len);
260470fe1b5SMarcel Holtmann 
261b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
262470fe1b5SMarcel Holtmann 		struct sk_buff *nskb;
263470fe1b5SMarcel Holtmann 
264c08b1a1dSMarcel Holtmann 		/* Ignore socket without the flag set */
265c85be545SMarcel Holtmann 		if (!hci_sock_test_flag(sk, flag))
266c08b1a1dSMarcel Holtmann 			continue;
267c08b1a1dSMarcel Holtmann 
268470fe1b5SMarcel Holtmann 		/* Skip the original socket */
269470fe1b5SMarcel Holtmann 		if (sk == skip_sk)
270470fe1b5SMarcel Holtmann 			continue;
271470fe1b5SMarcel Holtmann 
272470fe1b5SMarcel Holtmann 		if (sk->sk_state != BT_BOUND)
273470fe1b5SMarcel Holtmann 			continue;
274470fe1b5SMarcel Holtmann 
2757129069eSJohan Hedberg 		if (hci_pi(sk)->channel != channel)
276d7f72f61SMarcel Holtmann 			continue;
277d7f72f61SMarcel Holtmann 
278d7f72f61SMarcel Holtmann 		nskb = skb_clone(skb, GFP_ATOMIC);
279d7f72f61SMarcel Holtmann 		if (!nskb)
280d7f72f61SMarcel Holtmann 			continue;
281d7f72f61SMarcel Holtmann 
282d7f72f61SMarcel Holtmann 		if (sock_queue_rcv_skb(sk, nskb))
283d7f72f61SMarcel Holtmann 			kfree_skb(nskb);
284d7f72f61SMarcel Holtmann 	}
285d7f72f61SMarcel Holtmann 
286a9ee77afSSebastian Andrzej Siewior }
287a9ee77afSSebastian Andrzej Siewior 
288a9ee77afSSebastian Andrzej Siewior void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
289a9ee77afSSebastian Andrzej Siewior 			 int flag, struct sock *skip_sk)
290a9ee77afSSebastian Andrzej Siewior {
291a9ee77afSSebastian Andrzej Siewior 	read_lock(&hci_sk_list.lock);
292a9ee77afSSebastian Andrzej Siewior 	__hci_send_to_channel(channel, skb, flag, skip_sk);
293d7f72f61SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
294d7f72f61SMarcel Holtmann }
295d7f72f61SMarcel Holtmann 
296cd82e61cSMarcel Holtmann /* Send frame to monitor socket */
297cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
298cd82e61cSMarcel Holtmann {
299cd82e61cSMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
3002b531294SMarcel Holtmann 	struct hci_mon_hdr *hdr;
301cd82e61cSMarcel Holtmann 	__le16 opcode;
302cd82e61cSMarcel Holtmann 
303cd82e61cSMarcel Holtmann 	if (!atomic_read(&monitor_promisc))
304cd82e61cSMarcel Holtmann 		return;
305cd82e61cSMarcel Holtmann 
306cd82e61cSMarcel Holtmann 	BT_DBG("hdev %p len %d", hdev, skb->len);
307cd82e61cSMarcel Holtmann 
308d79f34e3SMarcel Holtmann 	switch (hci_skb_pkt_type(skb)) {
309cd82e61cSMarcel Holtmann 	case HCI_COMMAND_PKT:
310dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
311cd82e61cSMarcel Holtmann 		break;
312cd82e61cSMarcel Holtmann 	case HCI_EVENT_PKT:
313dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
314cd82e61cSMarcel Holtmann 		break;
315cd82e61cSMarcel Holtmann 	case HCI_ACLDATA_PKT:
316cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
317dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
318cd82e61cSMarcel Holtmann 		else
319dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
320cd82e61cSMarcel Holtmann 		break;
321cd82e61cSMarcel Holtmann 	case HCI_SCODATA_PKT:
322cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
323dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
324cd82e61cSMarcel Holtmann 		else
325dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
326cd82e61cSMarcel Holtmann 		break;
327e875ff84SMarcel Holtmann 	case HCI_DIAG_PKT:
328e875ff84SMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG);
329e875ff84SMarcel Holtmann 		break;
330cd82e61cSMarcel Holtmann 	default:
331cd82e61cSMarcel Holtmann 		return;
332cd82e61cSMarcel Holtmann 	}
333cd82e61cSMarcel Holtmann 
3342b531294SMarcel Holtmann 	/* Create a private copy with headroom */
3352b531294SMarcel Holtmann 	skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
3362b531294SMarcel Holtmann 	if (!skb_copy)
3372b531294SMarcel Holtmann 		return;
3382b531294SMarcel Holtmann 
3392b531294SMarcel Holtmann 	/* Put header before the data */
340d58ff351SJohannes Berg 	hdr = skb_push(skb_copy, HCI_MON_HDR_SIZE);
3412b531294SMarcel Holtmann 	hdr->opcode = opcode;
3422b531294SMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
3432b531294SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len);
3442b531294SMarcel Holtmann 
345c08b1a1dSMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
346c08b1a1dSMarcel Holtmann 			    HCI_SOCK_TRUSTED, NULL);
347cd82e61cSMarcel Holtmann 	kfree_skb(skb_copy);
348cd82e61cSMarcel Holtmann }
349cd82e61cSMarcel Holtmann 
35038ceaa00SMarcel Holtmann void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
35138ceaa00SMarcel Holtmann 				 void *data, u16 data_len, ktime_t tstamp,
35238ceaa00SMarcel Holtmann 				 int flag, struct sock *skip_sk)
35338ceaa00SMarcel Holtmann {
35438ceaa00SMarcel Holtmann 	struct sock *sk;
35538ceaa00SMarcel Holtmann 	__le16 index;
35638ceaa00SMarcel Holtmann 
35738ceaa00SMarcel Holtmann 	if (hdev)
35838ceaa00SMarcel Holtmann 		index = cpu_to_le16(hdev->id);
35938ceaa00SMarcel Holtmann 	else
36038ceaa00SMarcel Holtmann 		index = cpu_to_le16(MGMT_INDEX_NONE);
36138ceaa00SMarcel Holtmann 
36238ceaa00SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
36338ceaa00SMarcel Holtmann 
36438ceaa00SMarcel Holtmann 	sk_for_each(sk, &hci_sk_list.head) {
36538ceaa00SMarcel Holtmann 		struct hci_mon_hdr *hdr;
36638ceaa00SMarcel Holtmann 		struct sk_buff *skb;
36738ceaa00SMarcel Holtmann 
36838ceaa00SMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
36938ceaa00SMarcel Holtmann 			continue;
37038ceaa00SMarcel Holtmann 
37138ceaa00SMarcel Holtmann 		/* Ignore socket without the flag set */
37238ceaa00SMarcel Holtmann 		if (!hci_sock_test_flag(sk, flag))
37338ceaa00SMarcel Holtmann 			continue;
37438ceaa00SMarcel Holtmann 
37538ceaa00SMarcel Holtmann 		/* Skip the original socket */
37638ceaa00SMarcel Holtmann 		if (sk == skip_sk)
37738ceaa00SMarcel Holtmann 			continue;
37838ceaa00SMarcel Holtmann 
37938ceaa00SMarcel Holtmann 		skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC);
38038ceaa00SMarcel Holtmann 		if (!skb)
38138ceaa00SMarcel Holtmann 			continue;
38238ceaa00SMarcel Holtmann 
38338ceaa00SMarcel Holtmann 		put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
38438ceaa00SMarcel Holtmann 		put_unaligned_le16(event, skb_put(skb, 2));
38538ceaa00SMarcel Holtmann 
38638ceaa00SMarcel Holtmann 		if (data)
38759ae1d12SJohannes Berg 			skb_put_data(skb, data, data_len);
38838ceaa00SMarcel Holtmann 
38938ceaa00SMarcel Holtmann 		skb->tstamp = tstamp;
39038ceaa00SMarcel Holtmann 
391d58ff351SJohannes Berg 		hdr = skb_push(skb, HCI_MON_HDR_SIZE);
39238ceaa00SMarcel Holtmann 		hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
39338ceaa00SMarcel Holtmann 		hdr->index = index;
39438ceaa00SMarcel Holtmann 		hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
39538ceaa00SMarcel Holtmann 
396a9ee77afSSebastian Andrzej Siewior 		__hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
39738ceaa00SMarcel Holtmann 				      HCI_SOCK_TRUSTED, NULL);
39838ceaa00SMarcel Holtmann 		kfree_skb(skb);
39938ceaa00SMarcel Holtmann 	}
40038ceaa00SMarcel Holtmann 
40138ceaa00SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
40238ceaa00SMarcel Holtmann }
40338ceaa00SMarcel Holtmann 
404cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
405cd82e61cSMarcel Holtmann {
406cd82e61cSMarcel Holtmann 	struct hci_mon_hdr *hdr;
407cd82e61cSMarcel Holtmann 	struct hci_mon_new_index *ni;
4086c566dd5SMarcel Holtmann 	struct hci_mon_index_info *ii;
409cd82e61cSMarcel Holtmann 	struct sk_buff *skb;
410cd82e61cSMarcel Holtmann 	__le16 opcode;
411cd82e61cSMarcel Holtmann 
412cd82e61cSMarcel Holtmann 	switch (event) {
413cd82e61cSMarcel Holtmann 	case HCI_DEV_REG:
414cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
415cd82e61cSMarcel Holtmann 		if (!skb)
416cd82e61cSMarcel Holtmann 			return NULL;
417cd82e61cSMarcel Holtmann 
4184df864c1SJohannes Berg 		ni = skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
419cd82e61cSMarcel Holtmann 		ni->type = hdev->dev_type;
420cd82e61cSMarcel Holtmann 		ni->bus = hdev->bus;
421cd82e61cSMarcel Holtmann 		bacpy(&ni->bdaddr, &hdev->bdaddr);
422cd82e61cSMarcel Holtmann 		memcpy(ni->name, hdev->name, 8);
423cd82e61cSMarcel Holtmann 
424dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
425cd82e61cSMarcel Holtmann 		break;
426cd82e61cSMarcel Holtmann 
427cd82e61cSMarcel Holtmann 	case HCI_DEV_UNREG:
428cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
429cd82e61cSMarcel Holtmann 		if (!skb)
430cd82e61cSMarcel Holtmann 			return NULL;
431cd82e61cSMarcel Holtmann 
432dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
433cd82e61cSMarcel Holtmann 		break;
434cd82e61cSMarcel Holtmann 
435e131d74aSMarcel Holtmann 	case HCI_DEV_SETUP:
436e131d74aSMarcel Holtmann 		if (hdev->manufacturer == 0xffff)
437e131d74aSMarcel Holtmann 			return NULL;
438e131d74aSMarcel Holtmann 
439e131d74aSMarcel Holtmann 		/* fall through */
440e131d74aSMarcel Holtmann 
4416c566dd5SMarcel Holtmann 	case HCI_DEV_UP:
4426c566dd5SMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC);
4436c566dd5SMarcel Holtmann 		if (!skb)
4446c566dd5SMarcel Holtmann 			return NULL;
4456c566dd5SMarcel Holtmann 
4464df864c1SJohannes Berg 		ii = skb_put(skb, HCI_MON_INDEX_INFO_SIZE);
4476c566dd5SMarcel Holtmann 		bacpy(&ii->bdaddr, &hdev->bdaddr);
4486c566dd5SMarcel Holtmann 		ii->manufacturer = cpu_to_le16(hdev->manufacturer);
4496c566dd5SMarcel Holtmann 
4506c566dd5SMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_INDEX_INFO);
4516c566dd5SMarcel Holtmann 		break;
4526c566dd5SMarcel Holtmann 
45322db3cbcSMarcel Holtmann 	case HCI_DEV_OPEN:
45422db3cbcSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
45522db3cbcSMarcel Holtmann 		if (!skb)
45622db3cbcSMarcel Holtmann 			return NULL;
45722db3cbcSMarcel Holtmann 
45822db3cbcSMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_OPEN_INDEX);
45922db3cbcSMarcel Holtmann 		break;
46022db3cbcSMarcel Holtmann 
46122db3cbcSMarcel Holtmann 	case HCI_DEV_CLOSE:
46222db3cbcSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
46322db3cbcSMarcel Holtmann 		if (!skb)
46422db3cbcSMarcel Holtmann 			return NULL;
46522db3cbcSMarcel Holtmann 
46622db3cbcSMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX);
46722db3cbcSMarcel Holtmann 		break;
46822db3cbcSMarcel Holtmann 
469cd82e61cSMarcel Holtmann 	default:
470cd82e61cSMarcel Holtmann 		return NULL;
471cd82e61cSMarcel Holtmann 	}
472cd82e61cSMarcel Holtmann 
473cd82e61cSMarcel Holtmann 	__net_timestamp(skb);
474cd82e61cSMarcel Holtmann 
475d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
476cd82e61cSMarcel Holtmann 	hdr->opcode = opcode;
477cd82e61cSMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
478cd82e61cSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
479cd82e61cSMarcel Holtmann 
480cd82e61cSMarcel Holtmann 	return skb;
481cd82e61cSMarcel Holtmann }
482cd82e61cSMarcel Holtmann 
483249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
484249fa169SMarcel Holtmann {
485249fa169SMarcel Holtmann 	struct hci_mon_hdr *hdr;
486249fa169SMarcel Holtmann 	struct sk_buff *skb;
487d0bef1d2SMarcel Holtmann 	u16 format;
488249fa169SMarcel Holtmann 	u8 ver[3];
489249fa169SMarcel Holtmann 	u32 flags;
490249fa169SMarcel Holtmann 
4910ef2c42fSMarcel Holtmann 	/* No message needed when cookie is not present */
4920ef2c42fSMarcel Holtmann 	if (!hci_pi(sk)->cookie)
4930ef2c42fSMarcel Holtmann 		return NULL;
4940ef2c42fSMarcel Holtmann 
495d0bef1d2SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
496f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
497f81f5b2dSMarcel Holtmann 		format = 0x0000;
498f81f5b2dSMarcel Holtmann 		ver[0] = BT_SUBSYS_VERSION;
499f81f5b2dSMarcel Holtmann 		put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
500f81f5b2dSMarcel Holtmann 		break;
501aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
502aa1638ddSMarcel Holtmann 		format = 0x0001;
503aa1638ddSMarcel Holtmann 		ver[0] = BT_SUBSYS_VERSION;
504aa1638ddSMarcel Holtmann 		put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
505aa1638ddSMarcel Holtmann 		break;
506d0bef1d2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
507d0bef1d2SMarcel Holtmann 		format = 0x0002;
508d0bef1d2SMarcel Holtmann 		mgmt_fill_version_info(ver);
509d0bef1d2SMarcel Holtmann 		break;
510d0bef1d2SMarcel Holtmann 	default:
511d0bef1d2SMarcel Holtmann 		/* No message for unsupported format */
512d0bef1d2SMarcel Holtmann 		return NULL;
513d0bef1d2SMarcel Holtmann 	}
514d0bef1d2SMarcel Holtmann 
515249fa169SMarcel Holtmann 	skb = bt_skb_alloc(14 + TASK_COMM_LEN , GFP_ATOMIC);
516249fa169SMarcel Holtmann 	if (!skb)
517249fa169SMarcel Holtmann 		return NULL;
518249fa169SMarcel Holtmann 
519249fa169SMarcel Holtmann 	flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
520249fa169SMarcel Holtmann 
521249fa169SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
522249fa169SMarcel Holtmann 	put_unaligned_le16(format, skb_put(skb, 2));
52359ae1d12SJohannes Berg 	skb_put_data(skb, ver, sizeof(ver));
524249fa169SMarcel Holtmann 	put_unaligned_le32(flags, skb_put(skb, 4));
525634fef61SJohannes Berg 	skb_put_u8(skb, TASK_COMM_LEN);
52659ae1d12SJohannes Berg 	skb_put_data(skb, hci_pi(sk)->comm, TASK_COMM_LEN);
527249fa169SMarcel Holtmann 
528249fa169SMarcel Holtmann 	__net_timestamp(skb);
529249fa169SMarcel Holtmann 
530d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
531249fa169SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
5320ef2c42fSMarcel Holtmann 	if (hci_pi(sk)->hdev)
5330ef2c42fSMarcel Holtmann 		hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
5340ef2c42fSMarcel Holtmann 	else
535249fa169SMarcel Holtmann 		hdr->index = cpu_to_le16(HCI_DEV_NONE);
536249fa169SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
537249fa169SMarcel Holtmann 
538249fa169SMarcel Holtmann 	return skb;
539249fa169SMarcel Holtmann }
540249fa169SMarcel Holtmann 
541249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
542249fa169SMarcel Holtmann {
543249fa169SMarcel Holtmann 	struct hci_mon_hdr *hdr;
544249fa169SMarcel Holtmann 	struct sk_buff *skb;
545249fa169SMarcel Holtmann 
5460ef2c42fSMarcel Holtmann 	/* No message needed when cookie is not present */
5470ef2c42fSMarcel Holtmann 	if (!hci_pi(sk)->cookie)
5480ef2c42fSMarcel Holtmann 		return NULL;
5490ef2c42fSMarcel Holtmann 
550d0bef1d2SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
551f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
552aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
553d0bef1d2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
554d0bef1d2SMarcel Holtmann 		break;
555d0bef1d2SMarcel Holtmann 	default:
556d0bef1d2SMarcel Holtmann 		/* No message for unsupported format */
557d0bef1d2SMarcel Holtmann 		return NULL;
558d0bef1d2SMarcel Holtmann 	}
559d0bef1d2SMarcel Holtmann 
560249fa169SMarcel Holtmann 	skb = bt_skb_alloc(4, GFP_ATOMIC);
561249fa169SMarcel Holtmann 	if (!skb)
562249fa169SMarcel Holtmann 		return NULL;
563249fa169SMarcel Holtmann 
564249fa169SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
565249fa169SMarcel Holtmann 
566249fa169SMarcel Holtmann 	__net_timestamp(skb);
567249fa169SMarcel Holtmann 
568d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
569249fa169SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
5700ef2c42fSMarcel Holtmann 	if (hci_pi(sk)->hdev)
5710ef2c42fSMarcel Holtmann 		hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
5720ef2c42fSMarcel Holtmann 	else
573249fa169SMarcel Holtmann 		hdr->index = cpu_to_le16(HCI_DEV_NONE);
574249fa169SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
575249fa169SMarcel Holtmann 
576249fa169SMarcel Holtmann 	return skb;
577249fa169SMarcel Holtmann }
578249fa169SMarcel Holtmann 
57938ceaa00SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index,
58038ceaa00SMarcel Holtmann 						   u16 opcode, u16 len,
58138ceaa00SMarcel Holtmann 						   const void *buf)
58238ceaa00SMarcel Holtmann {
58338ceaa00SMarcel Holtmann 	struct hci_mon_hdr *hdr;
58438ceaa00SMarcel Holtmann 	struct sk_buff *skb;
58538ceaa00SMarcel Holtmann 
58638ceaa00SMarcel Holtmann 	skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
58738ceaa00SMarcel Holtmann 	if (!skb)
58838ceaa00SMarcel Holtmann 		return NULL;
58938ceaa00SMarcel Holtmann 
59038ceaa00SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
59138ceaa00SMarcel Holtmann 	put_unaligned_le16(opcode, skb_put(skb, 2));
59238ceaa00SMarcel Holtmann 
59338ceaa00SMarcel Holtmann 	if (buf)
59459ae1d12SJohannes Berg 		skb_put_data(skb, buf, len);
59538ceaa00SMarcel Holtmann 
59638ceaa00SMarcel Holtmann 	__net_timestamp(skb);
59738ceaa00SMarcel Holtmann 
598d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
59938ceaa00SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND);
60038ceaa00SMarcel Holtmann 	hdr->index = cpu_to_le16(index);
60138ceaa00SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
60238ceaa00SMarcel Holtmann 
60338ceaa00SMarcel Holtmann 	return skb;
60438ceaa00SMarcel Holtmann }
60538ceaa00SMarcel Holtmann 
606787b306cSJohannes Berg static void __printf(2, 3)
607787b306cSJohannes Berg send_monitor_note(struct sock *sk, const char *fmt, ...)
608dd31506dSMarcel Holtmann {
609787b306cSJohannes Berg 	size_t len;
610dd31506dSMarcel Holtmann 	struct hci_mon_hdr *hdr;
611dd31506dSMarcel Holtmann 	struct sk_buff *skb;
612787b306cSJohannes Berg 	va_list args;
613787b306cSJohannes Berg 
614787b306cSJohannes Berg 	va_start(args, fmt);
615787b306cSJohannes Berg 	len = vsnprintf(NULL, 0, fmt, args);
616787b306cSJohannes Berg 	va_end(args);
617dd31506dSMarcel Holtmann 
618dd31506dSMarcel Holtmann 	skb = bt_skb_alloc(len + 1, GFP_ATOMIC);
619dd31506dSMarcel Holtmann 	if (!skb)
620dd31506dSMarcel Holtmann 		return;
621dd31506dSMarcel Holtmann 
622787b306cSJohannes Berg 	va_start(args, fmt);
623787b306cSJohannes Berg 	vsprintf(skb_put(skb, len), fmt, args);
6244df864c1SJohannes Berg 	*(u8 *)skb_put(skb, 1) = 0;
625787b306cSJohannes Berg 	va_end(args);
626dd31506dSMarcel Holtmann 
627dd31506dSMarcel Holtmann 	__net_timestamp(skb);
628dd31506dSMarcel Holtmann 
629dd31506dSMarcel Holtmann 	hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
630dd31506dSMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE);
631dd31506dSMarcel Holtmann 	hdr->index = cpu_to_le16(HCI_DEV_NONE);
632dd31506dSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
633dd31506dSMarcel Holtmann 
634dd31506dSMarcel Holtmann 	if (sock_queue_rcv_skb(sk, skb))
635dd31506dSMarcel Holtmann 		kfree_skb(skb);
636dd31506dSMarcel Holtmann }
637dd31506dSMarcel Holtmann 
638cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk)
639cd82e61cSMarcel Holtmann {
640cd82e61cSMarcel Holtmann 	struct hci_dev *hdev;
641cd82e61cSMarcel Holtmann 
642cd82e61cSMarcel Holtmann 	read_lock(&hci_dev_list_lock);
643cd82e61cSMarcel Holtmann 
644cd82e61cSMarcel Holtmann 	list_for_each_entry(hdev, &hci_dev_list, list) {
645cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
646cd82e61cSMarcel Holtmann 
647cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_REG);
648cd82e61cSMarcel Holtmann 		if (!skb)
649cd82e61cSMarcel Holtmann 			continue;
650cd82e61cSMarcel Holtmann 
651cd82e61cSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
652cd82e61cSMarcel Holtmann 			kfree_skb(skb);
65322db3cbcSMarcel Holtmann 
65422db3cbcSMarcel Holtmann 		if (!test_bit(HCI_RUNNING, &hdev->flags))
65522db3cbcSMarcel Holtmann 			continue;
65622db3cbcSMarcel Holtmann 
65722db3cbcSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_OPEN);
65822db3cbcSMarcel Holtmann 		if (!skb)
65922db3cbcSMarcel Holtmann 			continue;
66022db3cbcSMarcel Holtmann 
66122db3cbcSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
66222db3cbcSMarcel Holtmann 			kfree_skb(skb);
6636c566dd5SMarcel Holtmann 
664e131d74aSMarcel Holtmann 		if (test_bit(HCI_UP, &hdev->flags))
6656c566dd5SMarcel Holtmann 			skb = create_monitor_event(hdev, HCI_DEV_UP);
666e131d74aSMarcel Holtmann 		else if (hci_dev_test_flag(hdev, HCI_SETUP))
667e131d74aSMarcel Holtmann 			skb = create_monitor_event(hdev, HCI_DEV_SETUP);
668e131d74aSMarcel Holtmann 		else
669e131d74aSMarcel Holtmann 			skb = NULL;
6706c566dd5SMarcel Holtmann 
671e131d74aSMarcel Holtmann 		if (skb) {
6726c566dd5SMarcel Holtmann 			if (sock_queue_rcv_skb(sk, skb))
6736c566dd5SMarcel Holtmann 				kfree_skb(skb);
674cd82e61cSMarcel Holtmann 		}
675e131d74aSMarcel Holtmann 	}
676cd82e61cSMarcel Holtmann 
677cd82e61cSMarcel Holtmann 	read_unlock(&hci_dev_list_lock);
678cd82e61cSMarcel Holtmann }
679cd82e61cSMarcel Holtmann 
680249fa169SMarcel Holtmann static void send_monitor_control_replay(struct sock *mon_sk)
681249fa169SMarcel Holtmann {
682249fa169SMarcel Holtmann 	struct sock *sk;
683249fa169SMarcel Holtmann 
684249fa169SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
685249fa169SMarcel Holtmann 
686249fa169SMarcel Holtmann 	sk_for_each(sk, &hci_sk_list.head) {
687249fa169SMarcel Holtmann 		struct sk_buff *skb;
688249fa169SMarcel Holtmann 
689249fa169SMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
690249fa169SMarcel Holtmann 		if (!skb)
691249fa169SMarcel Holtmann 			continue;
692249fa169SMarcel Holtmann 
693249fa169SMarcel Holtmann 		if (sock_queue_rcv_skb(mon_sk, skb))
694249fa169SMarcel Holtmann 			kfree_skb(skb);
695249fa169SMarcel Holtmann 	}
696249fa169SMarcel Holtmann 
697249fa169SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
698249fa169SMarcel Holtmann }
699249fa169SMarcel Holtmann 
700040030efSMarcel Holtmann /* Generate internal stack event */
701040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
702040030efSMarcel Holtmann {
703040030efSMarcel Holtmann 	struct hci_event_hdr *hdr;
704040030efSMarcel Holtmann 	struct hci_ev_stack_internal *ev;
705040030efSMarcel Holtmann 	struct sk_buff *skb;
706040030efSMarcel Holtmann 
707040030efSMarcel Holtmann 	skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
708040030efSMarcel Holtmann 	if (!skb)
709040030efSMarcel Holtmann 		return;
710040030efSMarcel Holtmann 
7114df864c1SJohannes Berg 	hdr = skb_put(skb, HCI_EVENT_HDR_SIZE);
712040030efSMarcel Holtmann 	hdr->evt  = HCI_EV_STACK_INTERNAL;
713040030efSMarcel Holtmann 	hdr->plen = sizeof(*ev) + dlen;
714040030efSMarcel Holtmann 
7154df864c1SJohannes Berg 	ev = skb_put(skb, sizeof(*ev) + dlen);
716040030efSMarcel Holtmann 	ev->type = type;
717040030efSMarcel Holtmann 	memcpy(ev->data, data, dlen);
718040030efSMarcel Holtmann 
719040030efSMarcel Holtmann 	bt_cb(skb)->incoming = 1;
720040030efSMarcel Holtmann 	__net_timestamp(skb);
721040030efSMarcel Holtmann 
722d79f34e3SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
723040030efSMarcel Holtmann 	hci_send_to_sock(hdev, skb);
724040030efSMarcel Holtmann 	kfree_skb(skb);
725040030efSMarcel Holtmann }
726040030efSMarcel Holtmann 
727040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event)
728040030efSMarcel Holtmann {
729040030efSMarcel Holtmann 	BT_DBG("hdev %s event %d", hdev->name, event);
730040030efSMarcel Holtmann 
731cd82e61cSMarcel Holtmann 	if (atomic_read(&monitor_promisc)) {
732cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
733cd82e61cSMarcel Holtmann 
734ed1b28a4SMarcel Holtmann 		/* Send event to monitor */
735cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, event);
736cd82e61cSMarcel Holtmann 		if (skb) {
737c08b1a1dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
738c08b1a1dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
739cd82e61cSMarcel Holtmann 			kfree_skb(skb);
740cd82e61cSMarcel Holtmann 		}
741cd82e61cSMarcel Holtmann 	}
742cd82e61cSMarcel Holtmann 
743ed1b28a4SMarcel Holtmann 	if (event <= HCI_DEV_DOWN) {
744ed1b28a4SMarcel Holtmann 		struct hci_ev_si_device ev;
745ed1b28a4SMarcel Holtmann 
746040030efSMarcel Holtmann 		/* Send event to sockets */
747040030efSMarcel Holtmann 		ev.event  = event;
748040030efSMarcel Holtmann 		ev.dev_id = hdev->id;
749040030efSMarcel Holtmann 		hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
750ed1b28a4SMarcel Holtmann 	}
751040030efSMarcel Holtmann 
752040030efSMarcel Holtmann 	if (event == HCI_DEV_UNREG) {
753040030efSMarcel Holtmann 		struct sock *sk;
754040030efSMarcel Holtmann 
755040030efSMarcel Holtmann 		/* Detach sockets from device */
756040030efSMarcel Holtmann 		read_lock(&hci_sk_list.lock);
757b67bfe0dSSasha Levin 		sk_for_each(sk, &hci_sk_list.head) {
758040030efSMarcel Holtmann 			bh_lock_sock_nested(sk);
759040030efSMarcel Holtmann 			if (hci_pi(sk)->hdev == hdev) {
760040030efSMarcel Holtmann 				hci_pi(sk)->hdev = NULL;
761040030efSMarcel Holtmann 				sk->sk_err = EPIPE;
762040030efSMarcel Holtmann 				sk->sk_state = BT_OPEN;
763040030efSMarcel Holtmann 				sk->sk_state_change(sk);
764040030efSMarcel Holtmann 
765040030efSMarcel Holtmann 				hci_dev_put(hdev);
766040030efSMarcel Holtmann 			}
767040030efSMarcel Holtmann 			bh_unlock_sock(sk);
768040030efSMarcel Holtmann 		}
769040030efSMarcel Holtmann 		read_unlock(&hci_sk_list.lock);
770040030efSMarcel Holtmann 	}
771040030efSMarcel Holtmann }
772040030efSMarcel Holtmann 
773801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
774801c1e8dSJohan Hedberg {
775801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *c;
776801c1e8dSJohan Hedberg 
777801c1e8dSJohan Hedberg 	list_for_each_entry(c, &mgmt_chan_list, list) {
778801c1e8dSJohan Hedberg 		if (c->channel == channel)
779801c1e8dSJohan Hedberg 			return c;
780801c1e8dSJohan Hedberg 	}
781801c1e8dSJohan Hedberg 
782801c1e8dSJohan Hedberg 	return NULL;
783801c1e8dSJohan Hedberg }
784801c1e8dSJohan Hedberg 
785801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
786801c1e8dSJohan Hedberg {
787801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *c;
788801c1e8dSJohan Hedberg 
789801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
790801c1e8dSJohan Hedberg 	c = __hci_mgmt_chan_find(channel);
791801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
792801c1e8dSJohan Hedberg 
793801c1e8dSJohan Hedberg 	return c;
794801c1e8dSJohan Hedberg }
795801c1e8dSJohan Hedberg 
796801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
797801c1e8dSJohan Hedberg {
798801c1e8dSJohan Hedberg 	if (c->channel < HCI_CHANNEL_CONTROL)
799801c1e8dSJohan Hedberg 		return -EINVAL;
800801c1e8dSJohan Hedberg 
801801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
802801c1e8dSJohan Hedberg 	if (__hci_mgmt_chan_find(c->channel)) {
803801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
804801c1e8dSJohan Hedberg 		return -EALREADY;
805801c1e8dSJohan Hedberg 	}
806801c1e8dSJohan Hedberg 
807801c1e8dSJohan Hedberg 	list_add_tail(&c->list, &mgmt_chan_list);
808801c1e8dSJohan Hedberg 
809801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
810801c1e8dSJohan Hedberg 
811801c1e8dSJohan Hedberg 	return 0;
812801c1e8dSJohan Hedberg }
813801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register);
814801c1e8dSJohan Hedberg 
815801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
816801c1e8dSJohan Hedberg {
817801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
818801c1e8dSJohan Hedberg 	list_del(&c->list);
819801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
820801c1e8dSJohan Hedberg }
821801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister);
822801c1e8dSJohan Hedberg 
8231da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8267b005bd3SMarcel Holtmann 	struct hci_dev *hdev;
827249fa169SMarcel Holtmann 	struct sk_buff *skb;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	if (!sk)
8321da177e4SLinus Torvalds 		return 0;
8331da177e4SLinus Torvalds 
8347b005bd3SMarcel Holtmann 	hdev = hci_pi(sk)->hdev;
8357b005bd3SMarcel Holtmann 
83670ecce91SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
83770ecce91SMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
838cd82e61cSMarcel Holtmann 		atomic_dec(&monitor_promisc);
83970ecce91SMarcel Holtmann 		break;
840f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
841aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
84270ecce91SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
843249fa169SMarcel Holtmann 		/* Send event to monitor */
844249fa169SMarcel Holtmann 		skb = create_monitor_ctrl_close(sk);
845249fa169SMarcel Holtmann 		if (skb) {
846249fa169SMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
847249fa169SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
848249fa169SMarcel Holtmann 			kfree_skb(skb);
849249fa169SMarcel Holtmann 		}
850249fa169SMarcel Holtmann 
851df1cb87aSMarcel Holtmann 		hci_sock_free_cookie(sk);
85270ecce91SMarcel Holtmann 		break;
85370ecce91SMarcel Holtmann 	}
854cd82e61cSMarcel Holtmann 
8551da177e4SLinus Torvalds 	bt_sock_unlink(&hci_sk_list, sk);
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	if (hdev) {
85823500189SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
8599332ef9dSMasahiro Yamada 			/* When releasing a user channel exclusive access,
8606b3cc1dbSSimon Fels 			 * call hci_dev_do_close directly instead of calling
8616b3cc1dbSSimon Fels 			 * hci_dev_close to ensure the exclusive access will
8626b3cc1dbSSimon Fels 			 * be released and the controller brought back down.
8636b3cc1dbSSimon Fels 			 *
8646b3cc1dbSSimon Fels 			 * The checking of HCI_AUTO_OFF is not needed in this
8656b3cc1dbSSimon Fels 			 * case since it will have been cleared already when
8666b3cc1dbSSimon Fels 			 * opening the user channel.
8676b3cc1dbSSimon Fels 			 */
8686b3cc1dbSSimon Fels 			hci_dev_do_close(hdev);
8699380f9eaSLoic Poulain 			hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
8709380f9eaSLoic Poulain 			mgmt_index_added(hdev);
87123500189SMarcel Holtmann 		}
87223500189SMarcel Holtmann 
8731da177e4SLinus Torvalds 		atomic_dec(&hdev->promisc);
8741da177e4SLinus Torvalds 		hci_dev_put(hdev);
8751da177e4SLinus Torvalds 	}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	sock_orphan(sk);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	skb_queue_purge(&sk->sk_receive_queue);
8801da177e4SLinus Torvalds 	skb_queue_purge(&sk->sk_write_queue);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	sock_put(sk);
8831da177e4SLinus Torvalds 	return 0;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
886b2a66aadSAntti Julku static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
887f0358568SJohan Hedberg {
888f0358568SJohan Hedberg 	bdaddr_t bdaddr;
8895e762444SAntti Julku 	int err;
890f0358568SJohan Hedberg 
891f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
892f0358568SJohan Hedberg 		return -EFAULT;
893f0358568SJohan Hedberg 
89409fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
8955e762444SAntti Julku 
896dcc36c16SJohan Hedberg 	err = hci_bdaddr_list_add(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
8975e762444SAntti Julku 
89809fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
8995e762444SAntti Julku 
9005e762444SAntti Julku 	return err;
901f0358568SJohan Hedberg }
902f0358568SJohan Hedberg 
903b2a66aadSAntti Julku static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
904f0358568SJohan Hedberg {
905f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9065e762444SAntti Julku 	int err;
907f0358568SJohan Hedberg 
908f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
909f0358568SJohan Hedberg 		return -EFAULT;
910f0358568SJohan Hedberg 
91109fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9125e762444SAntti Julku 
913dcc36c16SJohan Hedberg 	err = hci_bdaddr_list_del(&hdev->blacklist, &bdaddr, BDADDR_BREDR);
9145e762444SAntti Julku 
91509fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9165e762444SAntti Julku 
9175e762444SAntti Julku 	return err;
918f0358568SJohan Hedberg }
919f0358568SJohan Hedberg 
9201da177e4SLinus Torvalds /* Ioctls that require bound socket */
9216039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
9226039aa73SGustavo Padovan 				unsigned long arg)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds 	struct hci_dev *hdev = hci_pi(sk)->hdev;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	if (!hdev)
9271da177e4SLinus Torvalds 		return -EBADFD;
9281da177e4SLinus Torvalds 
929d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
9300736cfa8SMarcel Holtmann 		return -EBUSY;
9310736cfa8SMarcel Holtmann 
932d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
933fee746b0SMarcel Holtmann 		return -EOPNOTSUPP;
934fee746b0SMarcel Holtmann 
935ca8bee5dSMarcel Holtmann 	if (hdev->dev_type != HCI_PRIMARY)
9365b69bef5SMarcel Holtmann 		return -EOPNOTSUPP;
9375b69bef5SMarcel Holtmann 
9381da177e4SLinus Torvalds 	switch (cmd) {
9391da177e4SLinus Torvalds 	case HCISETRAW:
9401da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
941bf5b30b8SZhao Hongjiang 			return -EPERM;
942db596681SMarcel Holtmann 		return -EOPNOTSUPP;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	case HCIGETCONNINFO:
9451da177e4SLinus Torvalds 		return hci_get_conn_info(hdev, (void __user *)arg);
9461da177e4SLinus Torvalds 
94740be492fSMarcel Holtmann 	case HCIGETAUTHINFO:
94840be492fSMarcel Holtmann 		return hci_get_auth_info(hdev, (void __user *)arg);
94940be492fSMarcel Holtmann 
950f0358568SJohan Hedberg 	case HCIBLOCKADDR:
951f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
952bf5b30b8SZhao Hongjiang 			return -EPERM;
953b2a66aadSAntti Julku 		return hci_sock_blacklist_add(hdev, (void __user *)arg);
954f0358568SJohan Hedberg 
955f0358568SJohan Hedberg 	case HCIUNBLOCKADDR:
956f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
957bf5b30b8SZhao Hongjiang 			return -EPERM;
958b2a66aadSAntti Julku 		return hci_sock_blacklist_del(hdev, (void __user *)arg);
9590736cfa8SMarcel Holtmann 	}
960f0358568SJohan Hedberg 
961324d36edSMarcel Holtmann 	return -ENOIOCTLCMD;
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9648fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
9658fc9ced3SGustavo Padovan 			  unsigned long arg)
9661da177e4SLinus Torvalds {
9671da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
9680736cfa8SMarcel Holtmann 	struct sock *sk = sock->sk;
9691da177e4SLinus Torvalds 	int err;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	BT_DBG("cmd %x arg %lx", cmd, arg);
9721da177e4SLinus Torvalds 
973c1c4f956SMarcel Holtmann 	lock_sock(sk);
974c1c4f956SMarcel Holtmann 
975c1c4f956SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
976c1c4f956SMarcel Holtmann 		err = -EBADFD;
977c1c4f956SMarcel Holtmann 		goto done;
978c1c4f956SMarcel Holtmann 	}
979c1c4f956SMarcel Holtmann 
980f81f5b2dSMarcel Holtmann 	/* When calling an ioctl on an unbound raw socket, then ensure
981f81f5b2dSMarcel Holtmann 	 * that the monitor gets informed. Ensure that the resulting event
982f81f5b2dSMarcel Holtmann 	 * is only send once by checking if the cookie exists or not. The
983f81f5b2dSMarcel Holtmann 	 * socket cookie will be only ever generated once for the lifetime
984f81f5b2dSMarcel Holtmann 	 * of a given socket.
985f81f5b2dSMarcel Holtmann 	 */
986f81f5b2dSMarcel Holtmann 	if (hci_sock_gen_cookie(sk)) {
987f81f5b2dSMarcel Holtmann 		struct sk_buff *skb;
988f81f5b2dSMarcel Holtmann 
989f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
990f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
991f81f5b2dSMarcel Holtmann 
992f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
993f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
994f81f5b2dSMarcel Holtmann 		if (skb) {
995f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
996f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
997f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
998f81f5b2dSMarcel Holtmann 		}
999f81f5b2dSMarcel Holtmann 	}
1000f81f5b2dSMarcel Holtmann 
1001c1c4f956SMarcel Holtmann 	release_sock(sk);
1002c1c4f956SMarcel Holtmann 
10031da177e4SLinus Torvalds 	switch (cmd) {
10041da177e4SLinus Torvalds 	case HCIGETDEVLIST:
10051da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	case HCIGETDEVINFO:
10081da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	case HCIGETCONNLIST:
10111da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	case HCIDEVUP:
10141da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1015bf5b30b8SZhao Hongjiang 			return -EPERM;
10161da177e4SLinus Torvalds 		return hci_dev_open(arg);
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	case HCIDEVDOWN:
10191da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1020bf5b30b8SZhao Hongjiang 			return -EPERM;
10211da177e4SLinus Torvalds 		return hci_dev_close(arg);
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	case HCIDEVRESET:
10241da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1025bf5b30b8SZhao Hongjiang 			return -EPERM;
10261da177e4SLinus Torvalds 		return hci_dev_reset(arg);
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	case HCIDEVRESTAT:
10291da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1030bf5b30b8SZhao Hongjiang 			return -EPERM;
10311da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	case HCISETSCAN:
10341da177e4SLinus Torvalds 	case HCISETAUTH:
10351da177e4SLinus Torvalds 	case HCISETENCRYPT:
10361da177e4SLinus Torvalds 	case HCISETPTYPE:
10371da177e4SLinus Torvalds 	case HCISETLINKPOL:
10381da177e4SLinus Torvalds 	case HCISETLINKMODE:
10391da177e4SLinus Torvalds 	case HCISETACLMTU:
10401da177e4SLinus Torvalds 	case HCISETSCOMTU:
10411da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1042bf5b30b8SZhao Hongjiang 			return -EPERM;
10431da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	case HCIINQUIRY:
10461da177e4SLinus Torvalds 		return hci_inquiry(argp);
1047c1c4f956SMarcel Holtmann 	}
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	lock_sock(sk);
1050c1c4f956SMarcel Holtmann 
10511da177e4SLinus Torvalds 	err = hci_sock_bound_ioctl(sk, cmd, arg);
1052c1c4f956SMarcel Holtmann 
1053c1c4f956SMarcel Holtmann done:
10541da177e4SLinus Torvalds 	release_sock(sk);
10551da177e4SLinus Torvalds 	return err;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10588fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
10598fc9ced3SGustavo Padovan 			 int addr_len)
10601da177e4SLinus Torvalds {
10610381101fSJohan Hedberg 	struct sockaddr_hci haddr;
10621da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10631da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
1064f4cdbb3fSMarcel Holtmann 	struct sk_buff *skb;
10650381101fSJohan Hedberg 	int len, err = 0;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
10681da177e4SLinus Torvalds 
10690381101fSJohan Hedberg 	if (!addr)
10700381101fSJohan Hedberg 		return -EINVAL;
10710381101fSJohan Hedberg 
10720381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
10730381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
10740381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
10750381101fSJohan Hedberg 
10760381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
10770381101fSJohan Hedberg 		return -EINVAL;
10780381101fSJohan Hedberg 
10791da177e4SLinus Torvalds 	lock_sock(sk);
10801da177e4SLinus Torvalds 
10817cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
10827cc2ade2SMarcel Holtmann 		err = -EALREADY;
10837cc2ade2SMarcel Holtmann 		goto done;
10847cc2ade2SMarcel Holtmann 	}
10857cc2ade2SMarcel Holtmann 
10867cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
10877cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
10887cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
10891da177e4SLinus Torvalds 			err = -EALREADY;
10901da177e4SLinus Torvalds 			goto done;
10911da177e4SLinus Torvalds 		}
10921da177e4SLinus Torvalds 
10930381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
10940381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
109570f23020SAndrei Emeltchenko 			if (!hdev) {
10961da177e4SLinus Torvalds 				err = -ENODEV;
10971da177e4SLinus Torvalds 				goto done;
10981da177e4SLinus Torvalds 			}
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
11011da177e4SLinus Torvalds 		}
11021da177e4SLinus Torvalds 
11035a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1104f81f5b2dSMarcel Holtmann 
1105f4cdbb3fSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1106f4cdbb3fSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1107f4cdbb3fSMarcel Holtmann 			 * then there has been already an ioctl issued against
1108f4cdbb3fSMarcel Holtmann 			 * an unbound socket and with that triggerd an open
1109f4cdbb3fSMarcel Holtmann 			 * notification. Send a close notification first to
1110f4cdbb3fSMarcel Holtmann 			 * allow the state transition to bounded.
1111f81f5b2dSMarcel Holtmann 			 */
1112f4cdbb3fSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1113f4cdbb3fSMarcel Holtmann 			if (skb) {
1114f4cdbb3fSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1115f4cdbb3fSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1116f4cdbb3fSMarcel Holtmann 				kfree_skb(skb);
1117f4cdbb3fSMarcel Holtmann 			}
1118f4cdbb3fSMarcel Holtmann 		}
1119f81f5b2dSMarcel Holtmann 
1120f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1121f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1122f81f5b2dSMarcel Holtmann 
1123f4cdbb3fSMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
1124f4cdbb3fSMarcel Holtmann 
1125f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1126f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1127f81f5b2dSMarcel Holtmann 		if (skb) {
1128f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1129f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1130f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1131f81f5b2dSMarcel Holtmann 		}
11327cc2ade2SMarcel Holtmann 		break;
11337cc2ade2SMarcel Holtmann 
113423500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
113523500189SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
113623500189SMarcel Holtmann 			err = -EALREADY;
113723500189SMarcel Holtmann 			goto done;
113823500189SMarcel Holtmann 		}
113923500189SMarcel Holtmann 
114023500189SMarcel Holtmann 		if (haddr.hci_dev == HCI_DEV_NONE) {
114123500189SMarcel Holtmann 			err = -EINVAL;
114223500189SMarcel Holtmann 			goto done;
114323500189SMarcel Holtmann 		}
114423500189SMarcel Holtmann 
114510a8b86fSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
114623500189SMarcel Holtmann 			err = -EPERM;
114723500189SMarcel Holtmann 			goto done;
114823500189SMarcel Holtmann 		}
114923500189SMarcel Holtmann 
115023500189SMarcel Holtmann 		hdev = hci_dev_get(haddr.hci_dev);
115123500189SMarcel Holtmann 		if (!hdev) {
115223500189SMarcel Holtmann 			err = -ENODEV;
115323500189SMarcel Holtmann 			goto done;
115423500189SMarcel Holtmann 		}
115523500189SMarcel Holtmann 
1156781f899fSMarcel Holtmann 		if (test_bit(HCI_INIT, &hdev->flags) ||
1157d7a5a11dSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_SETUP) ||
1158781f899fSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1159781f899fSMarcel Holtmann 		    (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1160781f899fSMarcel Holtmann 		     test_bit(HCI_UP, &hdev->flags))) {
116123500189SMarcel Holtmann 			err = -EBUSY;
116223500189SMarcel Holtmann 			hci_dev_put(hdev);
116323500189SMarcel Holtmann 			goto done;
116423500189SMarcel Holtmann 		}
116523500189SMarcel Holtmann 
1166238be788SMarcel Holtmann 		if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
116723500189SMarcel Holtmann 			err = -EUSERS;
116823500189SMarcel Holtmann 			hci_dev_put(hdev);
116923500189SMarcel Holtmann 			goto done;
117023500189SMarcel Holtmann 		}
117123500189SMarcel Holtmann 
117223500189SMarcel Holtmann 		mgmt_index_removed(hdev);
117323500189SMarcel Holtmann 
117423500189SMarcel Holtmann 		err = hci_dev_open(hdev->id);
117523500189SMarcel Holtmann 		if (err) {
1176781f899fSMarcel Holtmann 			if (err == -EALREADY) {
1177781f899fSMarcel Holtmann 				/* In case the transport is already up and
1178781f899fSMarcel Holtmann 				 * running, clear the error here.
1179781f899fSMarcel Holtmann 				 *
11809332ef9dSMasahiro Yamada 				 * This can happen when opening a user
1181781f899fSMarcel Holtmann 				 * channel and HCI_AUTO_OFF grace period
1182781f899fSMarcel Holtmann 				 * is still active.
1183781f899fSMarcel Holtmann 				 */
1184781f899fSMarcel Holtmann 				err = 0;
1185781f899fSMarcel Holtmann 			} else {
1186a358dc11SMarcel Holtmann 				hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1187c6521401SMarcel Holtmann 				mgmt_index_added(hdev);
118823500189SMarcel Holtmann 				hci_dev_put(hdev);
118923500189SMarcel Holtmann 				goto done;
119023500189SMarcel Holtmann 			}
1191781f899fSMarcel Holtmann 		}
119223500189SMarcel Holtmann 
11935a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1194aa1638ddSMarcel Holtmann 
1195aa1638ddSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1196aa1638ddSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1197aa1638ddSMarcel Holtmann 			 * this socket will transition from a raw socket into
11989332ef9dSMasahiro Yamada 			 * a user channel socket. For a clean transition, send
1199aa1638ddSMarcel Holtmann 			 * the close notification first.
1200aa1638ddSMarcel Holtmann 			 */
1201aa1638ddSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1202aa1638ddSMarcel Holtmann 			if (skb) {
1203aa1638ddSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1204aa1638ddSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1205aa1638ddSMarcel Holtmann 				kfree_skb(skb);
1206aa1638ddSMarcel Holtmann 			}
1207aa1638ddSMarcel Holtmann 		}
1208aa1638ddSMarcel Holtmann 
1209aa1638ddSMarcel Holtmann 		/* The user channel is restricted to CAP_NET_ADMIN
1210aa1638ddSMarcel Holtmann 		 * capabilities and with that implicitly trusted.
1211aa1638ddSMarcel Holtmann 		 */
1212aa1638ddSMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1213aa1638ddSMarcel Holtmann 
121423500189SMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
12155a6d2cf5SMarcel Holtmann 
1216aa1638ddSMarcel Holtmann 		/* Send event to monitor */
1217aa1638ddSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1218aa1638ddSMarcel Holtmann 		if (skb) {
1219aa1638ddSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1220aa1638ddSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1221aa1638ddSMarcel Holtmann 			kfree_skb(skb);
1222aa1638ddSMarcel Holtmann 		}
1223aa1638ddSMarcel Holtmann 
12245a6d2cf5SMarcel Holtmann 		atomic_inc(&hdev->promisc);
122523500189SMarcel Holtmann 		break;
122623500189SMarcel Holtmann 
1227cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1228cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1229cd82e61cSMarcel Holtmann 			err = -EINVAL;
1230cd82e61cSMarcel Holtmann 			goto done;
1231cd82e61cSMarcel Holtmann 		}
1232cd82e61cSMarcel Holtmann 
1233cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
1234cd82e61cSMarcel Holtmann 			err = -EPERM;
1235cd82e61cSMarcel Holtmann 			goto done;
1236cd82e61cSMarcel Holtmann 		}
1237cd82e61cSMarcel Holtmann 
12385a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
12395a6d2cf5SMarcel Holtmann 
124050ebc055SMarcel Holtmann 		/* The monitor interface is restricted to CAP_NET_RAW
124150ebc055SMarcel Holtmann 		 * capabilities and with that implicitly trusted.
124250ebc055SMarcel Holtmann 		 */
124350ebc055SMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
124450ebc055SMarcel Holtmann 
1245787b306cSJohannes Berg 		send_monitor_note(sk, "Linux version %s (%s)",
1246787b306cSJohannes Berg 				  init_utsname()->release,
1247787b306cSJohannes Berg 				  init_utsname()->machine);
12489e8305b3SMarcel Holtmann 		send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
12499e8305b3SMarcel Holtmann 				  BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
1250cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
1251249fa169SMarcel Holtmann 		send_monitor_control_replay(sk);
1252cd82e61cSMarcel Holtmann 
1253cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
1254cd82e61cSMarcel Holtmann 		break;
1255cd82e61cSMarcel Holtmann 
1256ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
1257ac714949SMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1258ac714949SMarcel Holtmann 			err = -EINVAL;
1259ac714949SMarcel Holtmann 			goto done;
1260ac714949SMarcel Holtmann 		}
1261ac714949SMarcel Holtmann 
1262ac714949SMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
1263ac714949SMarcel Holtmann 			err = -EPERM;
1264ac714949SMarcel Holtmann 			goto done;
1265ac714949SMarcel Holtmann 		}
12665a6d2cf5SMarcel Holtmann 
12675a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1268ac714949SMarcel Holtmann 		break;
1269ac714949SMarcel Holtmann 
12707cc2ade2SMarcel Holtmann 	default:
1271801c1e8dSJohan Hedberg 		if (!hci_mgmt_chan_find(haddr.hci_channel)) {
12727cc2ade2SMarcel Holtmann 			err = -EINVAL;
12737cc2ade2SMarcel Holtmann 			goto done;
12747cc2ade2SMarcel Holtmann 		}
12757cc2ade2SMarcel Holtmann 
1276801c1e8dSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
1277801c1e8dSJohan Hedberg 			err = -EINVAL;
1278801c1e8dSJohan Hedberg 			goto done;
1279801c1e8dSJohan Hedberg 		}
1280801c1e8dSJohan Hedberg 
12811195fbb8SMarcel Holtmann 		/* Users with CAP_NET_ADMIN capabilities are allowed
12821195fbb8SMarcel Holtmann 		 * access to all management commands and events. For
12831195fbb8SMarcel Holtmann 		 * untrusted users the interface is restricted and
12841195fbb8SMarcel Holtmann 		 * also only untrusted events are sent.
128550ebc055SMarcel Holtmann 		 */
12861195fbb8SMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
128750ebc055SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
128850ebc055SMarcel Holtmann 
12895a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
12905a6d2cf5SMarcel Holtmann 
1291f9207338SMarcel Holtmann 		/* At the moment the index and unconfigured index events
1292f9207338SMarcel Holtmann 		 * are enabled unconditionally. Setting them on each
1293f9207338SMarcel Holtmann 		 * socket when binding keeps this functionality. They
1294f9207338SMarcel Holtmann 		 * however might be cleared later and then sending of these
1295f9207338SMarcel Holtmann 		 * events will be disabled, but that is then intentional.
1296f6b7712eSMarcel Holtmann 		 *
1297f6b7712eSMarcel Holtmann 		 * This also enables generic events that are safe to be
1298f6b7712eSMarcel Holtmann 		 * received by untrusted users. Example for such events
1299f6b7712eSMarcel Holtmann 		 * are changes to settings, class of device, name etc.
1300f9207338SMarcel Holtmann 		 */
13015a6d2cf5SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
1302f4cdbb3fSMarcel Holtmann 			if (!hci_sock_gen_cookie(sk)) {
1303f4cdbb3fSMarcel Holtmann 				/* In the case when a cookie has already been
1304f4cdbb3fSMarcel Holtmann 				 * assigned, this socket will transtion from
1305f4cdbb3fSMarcel Holtmann 				 * a raw socket into a control socket. To
1306f4cdbb3fSMarcel Holtmann 				 * allow for a clean transtion, send the
1307f4cdbb3fSMarcel Holtmann 				 * close notification first.
1308f4cdbb3fSMarcel Holtmann 				 */
1309f4cdbb3fSMarcel Holtmann 				skb = create_monitor_ctrl_close(sk);
1310f4cdbb3fSMarcel Holtmann 				if (skb) {
1311f4cdbb3fSMarcel Holtmann 					hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1312f4cdbb3fSMarcel Holtmann 							    HCI_SOCK_TRUSTED, NULL);
1313f4cdbb3fSMarcel Holtmann 					kfree_skb(skb);
1314f4cdbb3fSMarcel Holtmann 				}
1315f4cdbb3fSMarcel Holtmann 			}
131670ecce91SMarcel Holtmann 
1317249fa169SMarcel Holtmann 			/* Send event to monitor */
1318249fa169SMarcel Holtmann 			skb = create_monitor_ctrl_open(sk);
1319249fa169SMarcel Holtmann 			if (skb) {
1320249fa169SMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1321249fa169SMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1322249fa169SMarcel Holtmann 				kfree_skb(skb);
1323249fa169SMarcel Holtmann 			}
1324249fa169SMarcel Holtmann 
1325f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1326f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
13275504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
13285504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
13295504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
13305504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1331f9207338SMarcel Holtmann 		}
1332801c1e8dSJohan Hedberg 		break;
1333801c1e8dSJohan Hedberg 	}
1334801c1e8dSJohan Hedberg 
13351da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds done:
13381da177e4SLinus Torvalds 	release_sock(sk);
13391da177e4SLinus Torvalds 	return err;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13428fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
13439b2c45d4SDenys Vlasenko 			    int peer)
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
13461da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13479d4b68b2SMarcel Holtmann 	struct hci_dev *hdev;
13489d4b68b2SMarcel Holtmann 	int err = 0;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
13511da177e4SLinus Torvalds 
135206f43cbcSMarcel Holtmann 	if (peer)
135306f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
135406f43cbcSMarcel Holtmann 
13551da177e4SLinus Torvalds 	lock_sock(sk);
13561da177e4SLinus Torvalds 
13579d4b68b2SMarcel Holtmann 	hdev = hci_pi(sk)->hdev;
13589d4b68b2SMarcel Holtmann 	if (!hdev) {
13599d4b68b2SMarcel Holtmann 		err = -EBADFD;
13609d4b68b2SMarcel Holtmann 		goto done;
13619d4b68b2SMarcel Holtmann 	}
13629d4b68b2SMarcel Holtmann 
13631da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
13647b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
13659d4b68b2SMarcel Holtmann 	haddr->hci_channel= hci_pi(sk)->channel;
13669b2c45d4SDenys Vlasenko 	err = sizeof(*haddr);
13671da177e4SLinus Torvalds 
13689d4b68b2SMarcel Holtmann done:
13691da177e4SLinus Torvalds 	release_sock(sk);
13709d4b68b2SMarcel Holtmann 	return err;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
13736039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
13746039aa73SGustavo Padovan 			  struct sk_buff *skb)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	__u32 mask = hci_pi(sk)->cmsg_mask;
13771da177e4SLinus Torvalds 
13780d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
13790d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
13808fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
13818fc9ced3SGustavo Padovan 			 &incoming);
13820d48d939SMarcel Holtmann 	}
13831da177e4SLinus Torvalds 
1384a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
1385f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
1386f6e623a6SJohann Felix Soden 		struct compat_timeval ctv;
1387f6e623a6SJohann Felix Soden #endif
1388a61bbcf2SPatrick McHardy 		struct timeval tv;
1389767c5eb5SMarcel Holtmann 		void *data;
1390767c5eb5SMarcel Holtmann 		int len;
1391a61bbcf2SPatrick McHardy 
1392a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
1393767c5eb5SMarcel Holtmann 
13941da97f83SDavid S. Miller 		data = &tv;
13951da97f83SDavid S. Miller 		len = sizeof(tv);
13961da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
1397da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
1398da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
1399767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
1400767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
1401767c5eb5SMarcel Holtmann 			data = &ctv;
1402767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
1403767c5eb5SMarcel Holtmann 		}
14041da97f83SDavid S. Miller #endif
1405767c5eb5SMarcel Holtmann 
1406767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
1407a61bbcf2SPatrick McHardy 	}
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14108528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
14118528d3f7SMarcel Holtmann 			    size_t len, int flags)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	int noblock = flags & MSG_DONTWAIT;
14141da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
14151da177e4SLinus Torvalds 	struct sk_buff *skb;
14161da177e4SLinus Torvalds 	int copied, err;
141783871f8cSDenis Kenzior 	unsigned int skblen;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
14201da177e4SLinus Torvalds 
1421d94a6104SMarcel Holtmann 	if (flags & MSG_OOB)
14221da177e4SLinus Torvalds 		return -EOPNOTSUPP;
14231da177e4SLinus Torvalds 
1424ac714949SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1425ac714949SMarcel Holtmann 		return -EOPNOTSUPP;
1426ac714949SMarcel Holtmann 
14271da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
14281da177e4SLinus Torvalds 		return 0;
14291da177e4SLinus Torvalds 
143070f23020SAndrei Emeltchenko 	skb = skb_recv_datagram(sk, flags, noblock, &err);
143170f23020SAndrei Emeltchenko 	if (!skb)
14321da177e4SLinus Torvalds 		return err;
14331da177e4SLinus Torvalds 
143483871f8cSDenis Kenzior 	skblen = skb->len;
14351da177e4SLinus Torvalds 	copied = skb->len;
14361da177e4SLinus Torvalds 	if (len < copied) {
14371da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
14381da177e4SLinus Torvalds 		copied = len;
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds 
1441badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
144251f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
14431da177e4SLinus Torvalds 
14443a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
14453a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
14461da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
14473a208627SMarcel Holtmann 		break;
144823500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
1449cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1450cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
1451cd82e61cSMarcel Holtmann 		break;
1452801c1e8dSJohan Hedberg 	default:
1453801c1e8dSJohan Hedberg 		if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1454801c1e8dSJohan Hedberg 			sock_recv_timestamp(msg, sk, skb);
1455801c1e8dSJohan Hedberg 		break;
14563a208627SMarcel Holtmann 	}
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
14591da177e4SLinus Torvalds 
14604f34228bSLuiz Augusto von Dentz 	if (flags & MSG_TRUNC)
146183871f8cSDenis Kenzior 		copied = skblen;
146283871f8cSDenis Kenzior 
14631da177e4SLinus Torvalds 	return err ? : copied;
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
1466fa4335d7SJohan Hedberg static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
1467fa4335d7SJohan Hedberg 			struct msghdr *msg, size_t msglen)
1468fa4335d7SJohan Hedberg {
1469fa4335d7SJohan Hedberg 	void *buf;
1470fa4335d7SJohan Hedberg 	u8 *cp;
1471fa4335d7SJohan Hedberg 	struct mgmt_hdr *hdr;
1472fa4335d7SJohan Hedberg 	u16 opcode, index, len;
1473fa4335d7SJohan Hedberg 	struct hci_dev *hdev = NULL;
1474fa4335d7SJohan Hedberg 	const struct hci_mgmt_handler *handler;
1475fa4335d7SJohan Hedberg 	bool var_len, no_hdev;
1476fa4335d7SJohan Hedberg 	int err;
1477fa4335d7SJohan Hedberg 
1478fa4335d7SJohan Hedberg 	BT_DBG("got %zu bytes", msglen);
1479fa4335d7SJohan Hedberg 
1480fa4335d7SJohan Hedberg 	if (msglen < sizeof(*hdr))
1481fa4335d7SJohan Hedberg 		return -EINVAL;
1482fa4335d7SJohan Hedberg 
1483fa4335d7SJohan Hedberg 	buf = kmalloc(msglen, GFP_KERNEL);
1484fa4335d7SJohan Hedberg 	if (!buf)
1485fa4335d7SJohan Hedberg 		return -ENOMEM;
1486fa4335d7SJohan Hedberg 
1487fa4335d7SJohan Hedberg 	if (memcpy_from_msg(buf, msg, msglen)) {
1488fa4335d7SJohan Hedberg 		err = -EFAULT;
1489fa4335d7SJohan Hedberg 		goto done;
1490fa4335d7SJohan Hedberg 	}
1491fa4335d7SJohan Hedberg 
1492fa4335d7SJohan Hedberg 	hdr = buf;
1493fa4335d7SJohan Hedberg 	opcode = __le16_to_cpu(hdr->opcode);
1494fa4335d7SJohan Hedberg 	index = __le16_to_cpu(hdr->index);
1495fa4335d7SJohan Hedberg 	len = __le16_to_cpu(hdr->len);
1496fa4335d7SJohan Hedberg 
1497fa4335d7SJohan Hedberg 	if (len != msglen - sizeof(*hdr)) {
1498fa4335d7SJohan Hedberg 		err = -EINVAL;
1499fa4335d7SJohan Hedberg 		goto done;
1500fa4335d7SJohan Hedberg 	}
1501fa4335d7SJohan Hedberg 
150238ceaa00SMarcel Holtmann 	if (chan->channel == HCI_CHANNEL_CONTROL) {
150338ceaa00SMarcel Holtmann 		struct sk_buff *skb;
150438ceaa00SMarcel Holtmann 
150538ceaa00SMarcel Holtmann 		/* Send event to monitor */
150638ceaa00SMarcel Holtmann 		skb = create_monitor_ctrl_command(sk, index, opcode, len,
150738ceaa00SMarcel Holtmann 						  buf + sizeof(*hdr));
150838ceaa00SMarcel Holtmann 		if (skb) {
150938ceaa00SMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
151038ceaa00SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
151138ceaa00SMarcel Holtmann 			kfree_skb(skb);
151238ceaa00SMarcel Holtmann 		}
151338ceaa00SMarcel Holtmann 	}
151438ceaa00SMarcel Holtmann 
1515fa4335d7SJohan Hedberg 	if (opcode >= chan->handler_count ||
1516fa4335d7SJohan Hedberg 	    chan->handlers[opcode].func == NULL) {
1517fa4335d7SJohan Hedberg 		BT_DBG("Unknown op %u", opcode);
1518fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1519fa4335d7SJohan Hedberg 				      MGMT_STATUS_UNKNOWN_COMMAND);
1520fa4335d7SJohan Hedberg 		goto done;
1521fa4335d7SJohan Hedberg 	}
1522fa4335d7SJohan Hedberg 
1523fa4335d7SJohan Hedberg 	handler = &chan->handlers[opcode];
1524fa4335d7SJohan Hedberg 
1525fa4335d7SJohan Hedberg 	if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1526fa4335d7SJohan Hedberg 	    !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1527fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1528fa4335d7SJohan Hedberg 				      MGMT_STATUS_PERMISSION_DENIED);
1529fa4335d7SJohan Hedberg 		goto done;
1530fa4335d7SJohan Hedberg 	}
1531fa4335d7SJohan Hedberg 
1532fa4335d7SJohan Hedberg 	if (index != MGMT_INDEX_NONE) {
1533fa4335d7SJohan Hedberg 		hdev = hci_dev_get(index);
1534fa4335d7SJohan Hedberg 		if (!hdev) {
1535fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1536fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1537fa4335d7SJohan Hedberg 			goto done;
1538fa4335d7SJohan Hedberg 		}
1539fa4335d7SJohan Hedberg 
1540fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1541fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1542fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1543fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1544fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1545fa4335d7SJohan Hedberg 			goto done;
1546fa4335d7SJohan Hedberg 		}
1547fa4335d7SJohan Hedberg 
1548fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1549fa4335d7SJohan Hedberg 		    !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1550fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1551fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1552fa4335d7SJohan Hedberg 			goto done;
1553fa4335d7SJohan Hedberg 		}
1554fa4335d7SJohan Hedberg 	}
1555fa4335d7SJohan Hedberg 
1556fa4335d7SJohan Hedberg 	no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1557fa4335d7SJohan Hedberg 	if (no_hdev != !hdev) {
1558fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1559fa4335d7SJohan Hedberg 				      MGMT_STATUS_INVALID_INDEX);
1560fa4335d7SJohan Hedberg 		goto done;
1561fa4335d7SJohan Hedberg 	}
1562fa4335d7SJohan Hedberg 
1563fa4335d7SJohan Hedberg 	var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1564fa4335d7SJohan Hedberg 	if ((var_len && len < handler->data_len) ||
1565fa4335d7SJohan Hedberg 	    (!var_len && len != handler->data_len)) {
1566fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1567fa4335d7SJohan Hedberg 				      MGMT_STATUS_INVALID_PARAMS);
1568fa4335d7SJohan Hedberg 		goto done;
1569fa4335d7SJohan Hedberg 	}
1570fa4335d7SJohan Hedberg 
1571fa4335d7SJohan Hedberg 	if (hdev && chan->hdev_init)
1572fa4335d7SJohan Hedberg 		chan->hdev_init(sk, hdev);
1573fa4335d7SJohan Hedberg 
1574fa4335d7SJohan Hedberg 	cp = buf + sizeof(*hdr);
1575fa4335d7SJohan Hedberg 
1576fa4335d7SJohan Hedberg 	err = handler->func(sk, hdev, cp, len);
1577fa4335d7SJohan Hedberg 	if (err < 0)
1578fa4335d7SJohan Hedberg 		goto done;
1579fa4335d7SJohan Hedberg 
1580fa4335d7SJohan Hedberg 	err = msglen;
1581fa4335d7SJohan Hedberg 
1582fa4335d7SJohan Hedberg done:
1583fa4335d7SJohan Hedberg 	if (hdev)
1584fa4335d7SJohan Hedberg 		hci_dev_put(hdev);
1585fa4335d7SJohan Hedberg 
1586fa4335d7SJohan Hedberg 	kfree(buf);
1587fa4335d7SJohan Hedberg 	return err;
1588fa4335d7SJohan Hedberg }
1589fa4335d7SJohan Hedberg 
1590ac714949SMarcel Holtmann static int hci_logging_frame(struct sock *sk, struct msghdr *msg, int len)
1591ac714949SMarcel Holtmann {
1592ac714949SMarcel Holtmann 	struct hci_mon_hdr *hdr;
1593ac714949SMarcel Holtmann 	struct sk_buff *skb;
1594ac714949SMarcel Holtmann 	struct hci_dev *hdev;
1595ac714949SMarcel Holtmann 	u16 index;
1596ac714949SMarcel Holtmann 	int err;
1597ac714949SMarcel Holtmann 
1598ac714949SMarcel Holtmann 	/* The logging frame consists at minimum of the standard header,
1599ac714949SMarcel Holtmann 	 * the priority byte, the ident length byte and at least one string
1600ac714949SMarcel Holtmann 	 * terminator NUL byte. Anything shorter are invalid packets.
1601ac714949SMarcel Holtmann 	 */
1602ac714949SMarcel Holtmann 	if (len < sizeof(*hdr) + 3)
1603ac714949SMarcel Holtmann 		return -EINVAL;
1604ac714949SMarcel Holtmann 
1605ac714949SMarcel Holtmann 	skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
1606ac714949SMarcel Holtmann 	if (!skb)
1607ac714949SMarcel Holtmann 		return err;
1608ac714949SMarcel Holtmann 
1609ac714949SMarcel Holtmann 	if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1610ac714949SMarcel Holtmann 		err = -EFAULT;
1611ac714949SMarcel Holtmann 		goto drop;
1612ac714949SMarcel Holtmann 	}
1613ac714949SMarcel Holtmann 
1614ac714949SMarcel Holtmann 	hdr = (void *)skb->data;
1615ac714949SMarcel Holtmann 
1616ac714949SMarcel Holtmann 	if (__le16_to_cpu(hdr->len) != len - sizeof(*hdr)) {
1617ac714949SMarcel Holtmann 		err = -EINVAL;
1618ac714949SMarcel Holtmann 		goto drop;
1619ac714949SMarcel Holtmann 	}
1620ac714949SMarcel Holtmann 
1621ac714949SMarcel Holtmann 	if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1622ac714949SMarcel Holtmann 		__u8 priority = skb->data[sizeof(*hdr)];
1623ac714949SMarcel Holtmann 		__u8 ident_len = skb->data[sizeof(*hdr) + 1];
1624ac714949SMarcel Holtmann 
1625ac714949SMarcel Holtmann 		/* Only the priorities 0-7 are valid and with that any other
1626ac714949SMarcel Holtmann 		 * value results in an invalid packet.
1627ac714949SMarcel Holtmann 		 *
1628ac714949SMarcel Holtmann 		 * The priority byte is followed by an ident length byte and
1629ac714949SMarcel Holtmann 		 * the NUL terminated ident string. Check that the ident
1630ac714949SMarcel Holtmann 		 * length is not overflowing the packet and also that the
1631ac714949SMarcel Holtmann 		 * ident string itself is NUL terminated. In case the ident
1632ac714949SMarcel Holtmann 		 * length is zero, the length value actually doubles as NUL
1633ac714949SMarcel Holtmann 		 * terminator identifier.
1634ac714949SMarcel Holtmann 		 *
1635ac714949SMarcel Holtmann 		 * The message follows the ident string (if present) and
1636ac714949SMarcel Holtmann 		 * must be NUL terminated. Otherwise it is not a valid packet.
1637ac714949SMarcel Holtmann 		 */
1638ac714949SMarcel Holtmann 		if (priority > 7 || skb->data[len - 1] != 0x00 ||
1639ac714949SMarcel Holtmann 		    ident_len > len - sizeof(*hdr) - 3 ||
1640ac714949SMarcel Holtmann 		    skb->data[sizeof(*hdr) + ident_len + 1] != 0x00) {
1641ac714949SMarcel Holtmann 			err = -EINVAL;
1642ac714949SMarcel Holtmann 			goto drop;
1643ac714949SMarcel Holtmann 		}
1644ac714949SMarcel Holtmann 	} else {
1645ac714949SMarcel Holtmann 		err = -EINVAL;
1646ac714949SMarcel Holtmann 		goto drop;
1647ac714949SMarcel Holtmann 	}
1648ac714949SMarcel Holtmann 
1649ac714949SMarcel Holtmann 	index = __le16_to_cpu(hdr->index);
1650ac714949SMarcel Holtmann 
1651ac714949SMarcel Holtmann 	if (index != MGMT_INDEX_NONE) {
1652ac714949SMarcel Holtmann 		hdev = hci_dev_get(index);
1653ac714949SMarcel Holtmann 		if (!hdev) {
1654ac714949SMarcel Holtmann 			err = -ENODEV;
1655ac714949SMarcel Holtmann 			goto drop;
1656ac714949SMarcel Holtmann 		}
1657ac714949SMarcel Holtmann 	} else {
1658ac714949SMarcel Holtmann 		hdev = NULL;
1659ac714949SMarcel Holtmann 	}
1660ac714949SMarcel Holtmann 
1661ac714949SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1662ac714949SMarcel Holtmann 
1663ac714949SMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
1664ac714949SMarcel Holtmann 	err = len;
1665ac714949SMarcel Holtmann 
1666ac714949SMarcel Holtmann 	if (hdev)
1667ac714949SMarcel Holtmann 		hci_dev_put(hdev);
1668ac714949SMarcel Holtmann 
1669ac714949SMarcel Holtmann drop:
1670ac714949SMarcel Holtmann 	kfree_skb(skb);
1671ac714949SMarcel Holtmann 	return err;
1672ac714949SMarcel Holtmann }
1673ac714949SMarcel Holtmann 
16741b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
16751b784140SYing Xue 			    size_t len)
16761da177e4SLinus Torvalds {
16771da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1678801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *chan;
16791da177e4SLinus Torvalds 	struct hci_dev *hdev;
16801da177e4SLinus Torvalds 	struct sk_buff *skb;
16811da177e4SLinus Torvalds 	int err;
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
16861da177e4SLinus Torvalds 		return -EOPNOTSUPP;
16871da177e4SLinus Torvalds 
1688ab89f0bdSSzymon Janc 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE|
1689ab89f0bdSSzymon Janc 			       MSG_CMSG_COMPAT))
16901da177e4SLinus Torvalds 		return -EINVAL;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	if (len < 4 || len > HCI_MAX_FRAME_SIZE)
16931da177e4SLinus Torvalds 		return -EINVAL;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	lock_sock(sk);
16961da177e4SLinus Torvalds 
16970381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
16980381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
169923500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
17000381101fSJohan Hedberg 		break;
1701cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1702cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
1703cd82e61cSMarcel Holtmann 		goto done;
1704ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
1705ac714949SMarcel Holtmann 		err = hci_logging_frame(sk, msg, len);
1706ac714949SMarcel Holtmann 		goto done;
17070381101fSJohan Hedberg 	default:
1708801c1e8dSJohan Hedberg 		mutex_lock(&mgmt_chan_list_lock);
1709801c1e8dSJohan Hedberg 		chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1710801c1e8dSJohan Hedberg 		if (chan)
1711fa4335d7SJohan Hedberg 			err = hci_mgmt_cmd(chan, sk, msg, len);
1712801c1e8dSJohan Hedberg 		else
17130381101fSJohan Hedberg 			err = -EINVAL;
1714801c1e8dSJohan Hedberg 
1715801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
17160381101fSJohan Hedberg 		goto done;
17170381101fSJohan Hedberg 	}
17180381101fSJohan Hedberg 
171970f23020SAndrei Emeltchenko 	hdev = hci_pi(sk)->hdev;
172070f23020SAndrei Emeltchenko 	if (!hdev) {
17211da177e4SLinus Torvalds 		err = -EBADFD;
17221da177e4SLinus Torvalds 		goto done;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 
17257e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
17267e21addcSMarcel Holtmann 		err = -ENETDOWN;
17277e21addcSMarcel Holtmann 		goto done;
17287e21addcSMarcel Holtmann 	}
17297e21addcSMarcel Holtmann 
173070f23020SAndrei Emeltchenko 	skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
173170f23020SAndrei Emeltchenko 	if (!skb)
17321da177e4SLinus Torvalds 		goto done;
17331da177e4SLinus Torvalds 
17346ce8e9ceSAl Viro 	if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
17351da177e4SLinus Torvalds 		err = -EFAULT;
17361da177e4SLinus Torvalds 		goto drop;
17371da177e4SLinus Torvalds 	}
17381da177e4SLinus Torvalds 
17398528d3f7SMarcel Holtmann 	hci_skb_pkt_type(skb) = skb->data[0];
17401da177e4SLinus Torvalds 	skb_pull(skb, 1);
17411da177e4SLinus Torvalds 
17421bc5ad16SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
17431bc5ad16SMarcel Holtmann 		/* No permission check is needed for user channel
17441bc5ad16SMarcel Holtmann 		 * since that gets enforced when binding the socket.
17451bc5ad16SMarcel Holtmann 		 *
17461bc5ad16SMarcel Holtmann 		 * However check that the packet type is valid.
17471bc5ad16SMarcel Holtmann 		 */
1748d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1749d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1750d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
17511bc5ad16SMarcel Holtmann 			err = -EINVAL;
17521bc5ad16SMarcel Holtmann 			goto drop;
17531bc5ad16SMarcel Holtmann 		}
17541bc5ad16SMarcel Holtmann 
17551bc5ad16SMarcel Holtmann 		skb_queue_tail(&hdev->raw_q, skb);
17561bc5ad16SMarcel Holtmann 		queue_work(hdev->workqueue, &hdev->tx_work);
1757d79f34e3SMarcel Holtmann 	} else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
175883985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
17591da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
17601da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
17633bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
17643bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
17651da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
17661da177e4SLinus Torvalds 			err = -EPERM;
17671da177e4SLinus Torvalds 			goto drop;
17681da177e4SLinus Torvalds 		}
17691da177e4SLinus Torvalds 
17701982162bSMarcel Holtmann 		/* Since the opcode has already been extracted here, store
17711982162bSMarcel Holtmann 		 * a copy of the value for later use by the drivers.
17721982162bSMarcel Holtmann 		 */
17731982162bSMarcel Holtmann 		hci_skb_opcode(skb) = opcode;
17741982162bSMarcel Holtmann 
1775fee746b0SMarcel Holtmann 		if (ogf == 0x3f) {
17761da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
17773eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
17781da177e4SLinus Torvalds 		} else {
177949c922bbSStephen Hemminger 			/* Stand-alone HCI commands must be flagged as
178011714b3dSJohan Hedberg 			 * single-command requests.
178111714b3dSJohan Hedberg 			 */
178244d27137SJohan Hedberg 			bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
178311714b3dSJohan Hedberg 
17841da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
1785c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
17861da177e4SLinus Torvalds 		}
17871da177e4SLinus Torvalds 	} else {
17881da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
17891da177e4SLinus Torvalds 			err = -EPERM;
17901da177e4SLinus Torvalds 			goto drop;
17911da177e4SLinus Torvalds 		}
17921da177e4SLinus Torvalds 
1793d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1794d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
1795bb77543eSMarcel Holtmann 			err = -EINVAL;
1796bb77543eSMarcel Holtmann 			goto drop;
1797bb77543eSMarcel Holtmann 		}
1798bb77543eSMarcel Holtmann 
17991da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
18003eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	err = len;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds done:
18061da177e4SLinus Torvalds 	release_sock(sk);
18071da177e4SLinus Torvalds 	return err;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds drop:
18101da177e4SLinus Torvalds 	kfree_skb(skb);
18111da177e4SLinus Torvalds 	goto done;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
18148fc9ced3SGustavo Padovan static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
18158fc9ced3SGustavo Padovan 			       char __user *optval, unsigned int len)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
18181da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
18191da177e4SLinus Torvalds 	int err = 0, opt = 0;
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
18221da177e4SLinus Torvalds 
182347b0f573SMarcel Holtmann 	if (level != SOL_HCI)
182447b0f573SMarcel Holtmann 		return -ENOPROTOOPT;
182547b0f573SMarcel Holtmann 
18261da177e4SLinus Torvalds 	lock_sock(sk);
18271da177e4SLinus Torvalds 
18282f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1829c2371e80SMarcel Holtmann 		err = -EBADFD;
18302f39cdb7SMarcel Holtmann 		goto done;
18312f39cdb7SMarcel Holtmann 	}
18322f39cdb7SMarcel Holtmann 
18331da177e4SLinus Torvalds 	switch (optname) {
18341da177e4SLinus Torvalds 	case HCI_DATA_DIR:
18351da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
18361da177e4SLinus Torvalds 			err = -EFAULT;
18371da177e4SLinus Torvalds 			break;
18381da177e4SLinus Torvalds 		}
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 		if (opt)
18411da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
18421da177e4SLinus Torvalds 		else
18431da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
18441da177e4SLinus Torvalds 		break;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
18471da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
18481da177e4SLinus Torvalds 			err = -EFAULT;
18491da177e4SLinus Torvalds 			break;
18501da177e4SLinus Torvalds 		}
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 		if (opt)
18531da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
18541da177e4SLinus Torvalds 		else
18551da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
18561da177e4SLinus Torvalds 		break;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	case HCI_FILTER:
18590878b666SMarcel Holtmann 		{
18600878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
18610878b666SMarcel Holtmann 
18620878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
18630878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
18640878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
18650878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
18660878b666SMarcel Holtmann 		}
18670878b666SMarcel Holtmann 
18681da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
18691da177e4SLinus Torvalds 		if (copy_from_user(&uf, optval, len)) {
18701da177e4SLinus Torvalds 			err = -EFAULT;
18711da177e4SLinus Torvalds 			break;
18721da177e4SLinus Torvalds 		}
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
18751da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
18761da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
18771da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
18781da177e4SLinus Torvalds 		}
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 		{
18811da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
18841da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
18851da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
18861da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
18871da177e4SLinus Torvalds 		}
18881da177e4SLinus Torvalds 		break;
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	default:
18911da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
18921da177e4SLinus Torvalds 		break;
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 
18952f39cdb7SMarcel Holtmann done:
18961da177e4SLinus Torvalds 	release_sock(sk);
18971da177e4SLinus Torvalds 	return err;
18981da177e4SLinus Torvalds }
18991da177e4SLinus Torvalds 
19008fc9ced3SGustavo Padovan static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
19018fc9ced3SGustavo Padovan 			       char __user *optval, int __user *optlen)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds 	struct hci_ufilter uf;
19041da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1905cedc5469SMarcel Holtmann 	int len, opt, err = 0;
1906cedc5469SMarcel Holtmann 
1907cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
19081da177e4SLinus Torvalds 
190947b0f573SMarcel Holtmann 	if (level != SOL_HCI)
191047b0f573SMarcel Holtmann 		return -ENOPROTOOPT;
191147b0f573SMarcel Holtmann 
19121da177e4SLinus Torvalds 	if (get_user(len, optlen))
19131da177e4SLinus Torvalds 		return -EFAULT;
19141da177e4SLinus Torvalds 
1915cedc5469SMarcel Holtmann 	lock_sock(sk);
1916cedc5469SMarcel Holtmann 
1917cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1918c2371e80SMarcel Holtmann 		err = -EBADFD;
1919cedc5469SMarcel Holtmann 		goto done;
1920cedc5469SMarcel Holtmann 	}
1921cedc5469SMarcel Holtmann 
19221da177e4SLinus Torvalds 	switch (optname) {
19231da177e4SLinus Torvalds 	case HCI_DATA_DIR:
19241da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
19251da177e4SLinus Torvalds 			opt = 1;
19261da177e4SLinus Torvalds 		else
19271da177e4SLinus Torvalds 			opt = 0;
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 		if (put_user(opt, optval))
1930cedc5469SMarcel Holtmann 			err = -EFAULT;
19311da177e4SLinus Torvalds 		break;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
19341da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
19351da177e4SLinus Torvalds 			opt = 1;
19361da177e4SLinus Torvalds 		else
19371da177e4SLinus Torvalds 			opt = 0;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 		if (put_user(opt, optval))
1940cedc5469SMarcel Holtmann 			err = -EFAULT;
19411da177e4SLinus Torvalds 		break;
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	case HCI_FILTER:
19441da177e4SLinus Torvalds 		{
19451da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
19461da177e4SLinus Torvalds 
1947e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
19481da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
19491da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
19501da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
19511da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
19521da177e4SLinus Torvalds 		}
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
19551da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
1956cedc5469SMarcel Holtmann 			err = -EFAULT;
19571da177e4SLinus Torvalds 		break;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	default:
1960cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
19611da177e4SLinus Torvalds 		break;
19621da177e4SLinus Torvalds 	}
19631da177e4SLinus Torvalds 
1964cedc5469SMarcel Holtmann done:
1965cedc5469SMarcel Holtmann 	release_sock(sk);
1966cedc5469SMarcel Holtmann 	return err;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
196990ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
19701da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
19711da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
19721da177e4SLinus Torvalds 	.release	= hci_sock_release,
19731da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
19741da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
19751da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
19761da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
19771da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
1978*a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
19791da177e4SLinus Torvalds 	.listen		= sock_no_listen,
19801da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
19811da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
19821da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
19831da177e4SLinus Torvalds 	.connect	= sock_no_connect,
19841da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
19851da177e4SLinus Torvalds 	.accept		= sock_no_accept,
19861da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
19871da177e4SLinus Torvalds };
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds static struct proto hci_sk_proto = {
19901da177e4SLinus Torvalds 	.name		= "HCI",
19911da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
19921da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
19931da177e4SLinus Torvalds };
19941da177e4SLinus Torvalds 
19953f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
19963f378b68SEric Paris 			   int kern)
19971da177e4SLinus Torvalds {
19981da177e4SLinus Torvalds 	struct sock *sk;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
20031da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
20061da177e4SLinus Torvalds 
200711aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
20081da177e4SLinus Torvalds 	if (!sk)
20091da177e4SLinus Torvalds 		return -ENOMEM;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	sock_init_data(sock, sk);
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
20181da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
20211da177e4SLinus Torvalds 	return 0;
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
2024ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
20251da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
20261da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
20271da177e4SLinus Torvalds 	.create	= hci_sock_create,
20281da177e4SLinus Torvalds };
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds int __init hci_sock_init(void)
20311da177e4SLinus Torvalds {
20321da177e4SLinus Torvalds 	int err;
20331da177e4SLinus Torvalds 
2034b0a8e282SMarcel Holtmann 	BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2035b0a8e282SMarcel Holtmann 
20361da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
20371da177e4SLinus Torvalds 	if (err < 0)
20381da177e4SLinus Torvalds 		return err;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
2041f7c86637SMasatake YAMATO 	if (err < 0) {
2042f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
20431da177e4SLinus Torvalds 		goto error;
2044f7c86637SMasatake YAMATO 	}
2045f7c86637SMasatake YAMATO 
2046b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
2047f7c86637SMasatake YAMATO 	if (err < 0) {
2048f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
2049f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
2050f7c86637SMasatake YAMATO 		goto error;
2051f7c86637SMasatake YAMATO 	}
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	return 0;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds error:
20581da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
20591da177e4SLinus Torvalds 	return err;
20601da177e4SLinus Torvalds }
20611da177e4SLinus Torvalds 
2062b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
20631da177e4SLinus Torvalds {
2064f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
20655e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
20661da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
20671da177e4SLinus Torvalds }
2068