xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision 709fca500067524381e28a5f481882930eebac88)
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. */
267a6038b3SArnd Bergmann #include <linux/compat.h>
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;
5532929e1fSAlain Michaud 	__u8              cmsg_mask;
56863def58SMarcel Holtmann 	unsigned short    channel;
576befc644SMarcel Holtmann 	unsigned long     flags;
5870ecce91SMarcel Holtmann 	__u32             cookie;
5970ecce91SMarcel Holtmann 	char              comm[TASK_COMM_LEN];
6009572fcaSLuiz Augusto von Dentz 	__u16             mtu;
61863def58SMarcel Holtmann };
62863def58SMarcel Holtmann 
63e0448092STetsuo Handa static struct hci_dev *hci_hdev_from_sock(struct sock *sk)
64e0448092STetsuo Handa {
65e0448092STetsuo Handa 	struct hci_dev *hdev = hci_pi(sk)->hdev;
66e0448092STetsuo Handa 
67e0448092STetsuo Handa 	if (!hdev)
68e0448092STetsuo Handa 		return ERR_PTR(-EBADFD);
69e0448092STetsuo Handa 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
70e0448092STetsuo Handa 		return ERR_PTR(-EPIPE);
71e0448092STetsuo Handa 	return hdev;
72e0448092STetsuo Handa }
73e0448092STetsuo Handa 
746befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr)
756befc644SMarcel Holtmann {
766befc644SMarcel Holtmann 	set_bit(nr, &hci_pi(sk)->flags);
776befc644SMarcel Holtmann }
786befc644SMarcel Holtmann 
796befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr)
806befc644SMarcel Holtmann {
816befc644SMarcel Holtmann 	clear_bit(nr, &hci_pi(sk)->flags);
826befc644SMarcel Holtmann }
836befc644SMarcel Holtmann 
84c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr)
85c85be545SMarcel Holtmann {
86c85be545SMarcel Holtmann 	return test_bit(nr, &hci_pi(sk)->flags);
87c85be545SMarcel Holtmann }
88c85be545SMarcel Holtmann 
89d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk)
90d0f172b1SJohan Hedberg {
91d0f172b1SJohan Hedberg 	return hci_pi(sk)->channel;
92d0f172b1SJohan Hedberg }
93d0f172b1SJohan Hedberg 
9470ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk)
9570ecce91SMarcel Holtmann {
9670ecce91SMarcel Holtmann 	return hci_pi(sk)->cookie;
9770ecce91SMarcel Holtmann }
9870ecce91SMarcel Holtmann 
99df1cb87aSMarcel Holtmann static bool hci_sock_gen_cookie(struct sock *sk)
100df1cb87aSMarcel Holtmann {
101df1cb87aSMarcel Holtmann 	int id = hci_pi(sk)->cookie;
102df1cb87aSMarcel Holtmann 
103df1cb87aSMarcel Holtmann 	if (!id) {
104df1cb87aSMarcel Holtmann 		id = ida_simple_get(&sock_cookie_ida, 1, 0, GFP_KERNEL);
105df1cb87aSMarcel Holtmann 		if (id < 0)
106df1cb87aSMarcel Holtmann 			id = 0xffffffff;
107df1cb87aSMarcel Holtmann 
108df1cb87aSMarcel Holtmann 		hci_pi(sk)->cookie = id;
109df1cb87aSMarcel Holtmann 		get_task_comm(hci_pi(sk)->comm, current);
110df1cb87aSMarcel Holtmann 		return true;
111df1cb87aSMarcel Holtmann 	}
112df1cb87aSMarcel Holtmann 
113df1cb87aSMarcel Holtmann 	return false;
114df1cb87aSMarcel Holtmann }
115df1cb87aSMarcel Holtmann 
116df1cb87aSMarcel Holtmann static void hci_sock_free_cookie(struct sock *sk)
117df1cb87aSMarcel Holtmann {
118df1cb87aSMarcel Holtmann 	int id = hci_pi(sk)->cookie;
119df1cb87aSMarcel Holtmann 
120df1cb87aSMarcel Holtmann 	if (id) {
121df1cb87aSMarcel Holtmann 		hci_pi(sk)->cookie = 0xffffffff;
122df1cb87aSMarcel Holtmann 		ida_simple_remove(&sock_cookie_ida, id);
123df1cb87aSMarcel Holtmann 	}
124df1cb87aSMarcel Holtmann }
125df1cb87aSMarcel Holtmann 
1269391976aSJiri Slaby static inline int hci_test_bit(int nr, const void *addr)
1271da177e4SLinus Torvalds {
1289391976aSJiri Slaby 	return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /* Security filter */
1323ad254f7SMarcel Holtmann #define HCI_SFLT_MAX_OGF  5
1333ad254f7SMarcel Holtmann 
1343ad254f7SMarcel Holtmann struct hci_sec_filter {
1353ad254f7SMarcel Holtmann 	__u32 type_mask;
1363ad254f7SMarcel Holtmann 	__u32 event_mask[2];
1373ad254f7SMarcel Holtmann 	__u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
1383ad254f7SMarcel Holtmann };
1393ad254f7SMarcel Holtmann 
1407e67c112SMarcel Holtmann static const struct hci_sec_filter hci_sec_filter = {
1411da177e4SLinus Torvalds 	/* Packet types */
1421da177e4SLinus Torvalds 	0x10,
1431da177e4SLinus Torvalds 	/* Events */
144dd7f5527SMarcel Holtmann 	{ 0x1000d9fe, 0x0000b00c },
1451da177e4SLinus Torvalds 	/* Commands */
1461da177e4SLinus Torvalds 	{
1471da177e4SLinus Torvalds 		{ 0x0 },
1481da177e4SLinus Torvalds 		/* OGF_LINK_CTL */
1497c631a67SMarcel Holtmann 		{ 0xbe000006, 0x00000001, 0x00000000, 0x00 },
1501da177e4SLinus Torvalds 		/* OGF_LINK_POLICY */
1517c631a67SMarcel Holtmann 		{ 0x00005200, 0x00000000, 0x00000000, 0x00 },
1521da177e4SLinus Torvalds 		/* OGF_HOST_CTL */
1537c631a67SMarcel Holtmann 		{ 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
1541da177e4SLinus Torvalds 		/* OGF_INFO_PARAM */
1557c631a67SMarcel Holtmann 		{ 0x000002be, 0x00000000, 0x00000000, 0x00 },
1561da177e4SLinus Torvalds 		/* OGF_STATUS_PARAM */
1577c631a67SMarcel Holtmann 		{ 0x000000ea, 0x00000000, 0x00000000, 0x00 }
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds };
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static struct bt_sock_list hci_sk_list = {
162d5fb2962SRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
1631da177e4SLinus Torvalds };
1641da177e4SLinus Torvalds 
165f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
166f81fe64fSMarcel Holtmann {
167f81fe64fSMarcel Holtmann 	struct hci_filter *flt;
168f81fe64fSMarcel Holtmann 	int flt_type, flt_event;
169f81fe64fSMarcel Holtmann 
170f81fe64fSMarcel Holtmann 	/* Apply filter */
171f81fe64fSMarcel Holtmann 	flt = &hci_pi(sk)->filter;
172f81fe64fSMarcel Holtmann 
173d79f34e3SMarcel Holtmann 	flt_type = hci_skb_pkt_type(skb) & HCI_FLT_TYPE_BITS;
174f81fe64fSMarcel Holtmann 
175f81fe64fSMarcel Holtmann 	if (!test_bit(flt_type, &flt->type_mask))
176f81fe64fSMarcel Holtmann 		return true;
177f81fe64fSMarcel Holtmann 
178f81fe64fSMarcel Holtmann 	/* Extra filter for event packets only */
179d79f34e3SMarcel Holtmann 	if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT)
180f81fe64fSMarcel Holtmann 		return false;
181f81fe64fSMarcel Holtmann 
182f81fe64fSMarcel Holtmann 	flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
183f81fe64fSMarcel Holtmann 
184f81fe64fSMarcel Holtmann 	if (!hci_test_bit(flt_event, &flt->event_mask))
185f81fe64fSMarcel Holtmann 		return true;
186f81fe64fSMarcel Holtmann 
187f81fe64fSMarcel Holtmann 	/* Check filter only when opcode is set */
188f81fe64fSMarcel Holtmann 	if (!flt->opcode)
189f81fe64fSMarcel Holtmann 		return false;
190f81fe64fSMarcel Holtmann 
191f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_COMPLETE &&
192f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
193f81fe64fSMarcel Holtmann 		return true;
194f81fe64fSMarcel Holtmann 
195f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_STATUS &&
196f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
197f81fe64fSMarcel Holtmann 		return true;
198f81fe64fSMarcel Holtmann 
199f81fe64fSMarcel Holtmann 	return false;
200f81fe64fSMarcel Holtmann }
201f81fe64fSMarcel Holtmann 
2021da177e4SLinus Torvalds /* Send frame to RAW socket */
203470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct sock *sk;
206e0edf373SMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	BT_DBG("hdev %p len %d", hdev, skb->len);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	read_lock(&hci_sk_list.lock);
211470fe1b5SMarcel Holtmann 
212b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
2131da177e4SLinus Torvalds 		struct sk_buff *nskb;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 		if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
2161da177e4SLinus Torvalds 			continue;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 		/* Don't send frame to the socket it came from */
2191da177e4SLinus Torvalds 		if (skb->sk == sk)
2201da177e4SLinus Torvalds 			continue;
2211da177e4SLinus Torvalds 
22223500189SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
223d79f34e3SMarcel Holtmann 			if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
224d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
225d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
226cc974003SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
227cc974003SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT)
228bb77543eSMarcel Holtmann 				continue;
229f81fe64fSMarcel Holtmann 			if (is_filtered_packet(sk, skb))
2301da177e4SLinus Torvalds 				continue;
23123500189SMarcel Holtmann 		} else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
23223500189SMarcel Holtmann 			if (!bt_cb(skb)->incoming)
23323500189SMarcel Holtmann 				continue;
234d79f34e3SMarcel Holtmann 			if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
235d79f34e3SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
236cc974003SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
237cc974003SMarcel Holtmann 			    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT)
23823500189SMarcel Holtmann 				continue;
23923500189SMarcel Holtmann 		} else {
24023500189SMarcel Holtmann 			/* Don't send frame to other channel types */
24123500189SMarcel Holtmann 			continue;
24223500189SMarcel Holtmann 		}
2431da177e4SLinus Torvalds 
244e0edf373SMarcel Holtmann 		if (!skb_copy) {
245e0edf373SMarcel Holtmann 			/* Create a private copy with headroom */
246bad93e9dSOctavian Purdila 			skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true);
247e0edf373SMarcel Holtmann 			if (!skb_copy)
2481da177e4SLinus Torvalds 				continue;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 			/* Put type byte before the data */
251d79f34e3SMarcel Holtmann 			memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1);
252e0edf373SMarcel Holtmann 		}
253e0edf373SMarcel Holtmann 
254e0edf373SMarcel Holtmann 		nskb = skb_clone(skb_copy, GFP_ATOMIC);
255e0edf373SMarcel Holtmann 		if (!nskb)
256e0edf373SMarcel Holtmann 			continue;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 		if (sock_queue_rcv_skb(sk, nskb))
2591da177e4SLinus Torvalds 			kfree_skb(nskb);
2601da177e4SLinus Torvalds 	}
261470fe1b5SMarcel Holtmann 
262470fe1b5SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
263e0edf373SMarcel Holtmann 
264e0edf373SMarcel Holtmann 	kfree_skb(skb_copy);
265470fe1b5SMarcel Holtmann }
266470fe1b5SMarcel Holtmann 
2677129069eSJohan Hedberg /* Send frame to sockets with specific channel */
268a9ee77afSSebastian Andrzej Siewior static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
269c08b1a1dSMarcel Holtmann 				  int flag, struct sock *skip_sk)
270470fe1b5SMarcel Holtmann {
271470fe1b5SMarcel Holtmann 	struct sock *sk;
272470fe1b5SMarcel Holtmann 
2737129069eSJohan Hedberg 	BT_DBG("channel %u len %d", channel, skb->len);
274470fe1b5SMarcel Holtmann 
275b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
276470fe1b5SMarcel Holtmann 		struct sk_buff *nskb;
277470fe1b5SMarcel Holtmann 
278c08b1a1dSMarcel Holtmann 		/* Ignore socket without the flag set */
279c85be545SMarcel Holtmann 		if (!hci_sock_test_flag(sk, flag))
280c08b1a1dSMarcel Holtmann 			continue;
281c08b1a1dSMarcel Holtmann 
282470fe1b5SMarcel Holtmann 		/* Skip the original socket */
283470fe1b5SMarcel Holtmann 		if (sk == skip_sk)
284470fe1b5SMarcel Holtmann 			continue;
285470fe1b5SMarcel Holtmann 
286470fe1b5SMarcel Holtmann 		if (sk->sk_state != BT_BOUND)
287470fe1b5SMarcel Holtmann 			continue;
288470fe1b5SMarcel Holtmann 
2897129069eSJohan Hedberg 		if (hci_pi(sk)->channel != channel)
290d7f72f61SMarcel Holtmann 			continue;
291d7f72f61SMarcel Holtmann 
292d7f72f61SMarcel Holtmann 		nskb = skb_clone(skb, GFP_ATOMIC);
293d7f72f61SMarcel Holtmann 		if (!nskb)
294d7f72f61SMarcel Holtmann 			continue;
295d7f72f61SMarcel Holtmann 
296d7f72f61SMarcel Holtmann 		if (sock_queue_rcv_skb(sk, nskb))
297d7f72f61SMarcel Holtmann 			kfree_skb(nskb);
298d7f72f61SMarcel Holtmann 	}
299d7f72f61SMarcel Holtmann 
300a9ee77afSSebastian Andrzej Siewior }
301a9ee77afSSebastian Andrzej Siewior 
302a9ee77afSSebastian Andrzej Siewior void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
303a9ee77afSSebastian Andrzej Siewior 			 int flag, struct sock *skip_sk)
304a9ee77afSSebastian Andrzej Siewior {
305a9ee77afSSebastian Andrzej Siewior 	read_lock(&hci_sk_list.lock);
306a9ee77afSSebastian Andrzej Siewior 	__hci_send_to_channel(channel, skb, flag, skip_sk);
307d7f72f61SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
308d7f72f61SMarcel Holtmann }
309d7f72f61SMarcel Holtmann 
310cd82e61cSMarcel Holtmann /* Send frame to monitor socket */
311cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
312cd82e61cSMarcel Holtmann {
313cd82e61cSMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
3142b531294SMarcel Holtmann 	struct hci_mon_hdr *hdr;
315cd82e61cSMarcel Holtmann 	__le16 opcode;
316cd82e61cSMarcel Holtmann 
317cd82e61cSMarcel Holtmann 	if (!atomic_read(&monitor_promisc))
318cd82e61cSMarcel Holtmann 		return;
319cd82e61cSMarcel Holtmann 
320cd82e61cSMarcel Holtmann 	BT_DBG("hdev %p len %d", hdev, skb->len);
321cd82e61cSMarcel Holtmann 
322d79f34e3SMarcel Holtmann 	switch (hci_skb_pkt_type(skb)) {
323cd82e61cSMarcel Holtmann 	case HCI_COMMAND_PKT:
324dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
325cd82e61cSMarcel Holtmann 		break;
326cd82e61cSMarcel Holtmann 	case HCI_EVENT_PKT:
327dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
328cd82e61cSMarcel Holtmann 		break;
329cd82e61cSMarcel Holtmann 	case HCI_ACLDATA_PKT:
330cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
331dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
332cd82e61cSMarcel Holtmann 		else
333dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
334cd82e61cSMarcel Holtmann 		break;
335cd82e61cSMarcel Holtmann 	case HCI_SCODATA_PKT:
336cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
337dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
338cd82e61cSMarcel Holtmann 		else
339dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
340cd82e61cSMarcel Holtmann 		break;
341f9a619dbSLuiz Augusto von Dentz 	case HCI_ISODATA_PKT:
342f9a619dbSLuiz Augusto von Dentz 		if (bt_cb(skb)->incoming)
343f9a619dbSLuiz Augusto von Dentz 			opcode = cpu_to_le16(HCI_MON_ISO_RX_PKT);
344f9a619dbSLuiz Augusto von Dentz 		else
345f9a619dbSLuiz Augusto von Dentz 			opcode = cpu_to_le16(HCI_MON_ISO_TX_PKT);
346f9a619dbSLuiz Augusto von Dentz 		break;
347e875ff84SMarcel Holtmann 	case HCI_DIAG_PKT:
348e875ff84SMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG);
349e875ff84SMarcel Holtmann 		break;
350cd82e61cSMarcel Holtmann 	default:
351cd82e61cSMarcel Holtmann 		return;
352cd82e61cSMarcel Holtmann 	}
353cd82e61cSMarcel Holtmann 
3542b531294SMarcel Holtmann 	/* Create a private copy with headroom */
3552b531294SMarcel Holtmann 	skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
3562b531294SMarcel Holtmann 	if (!skb_copy)
3572b531294SMarcel Holtmann 		return;
3582b531294SMarcel Holtmann 
3592b531294SMarcel Holtmann 	/* Put header before the data */
360d58ff351SJohannes Berg 	hdr = skb_push(skb_copy, HCI_MON_HDR_SIZE);
3612b531294SMarcel Holtmann 	hdr->opcode = opcode;
3622b531294SMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
3632b531294SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len);
3642b531294SMarcel Holtmann 
365c08b1a1dSMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
366c08b1a1dSMarcel Holtmann 			    HCI_SOCK_TRUSTED, NULL);
367cd82e61cSMarcel Holtmann 	kfree_skb(skb_copy);
368cd82e61cSMarcel Holtmann }
369cd82e61cSMarcel Holtmann 
37038ceaa00SMarcel Holtmann void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
37138ceaa00SMarcel Holtmann 				 void *data, u16 data_len, ktime_t tstamp,
37238ceaa00SMarcel Holtmann 				 int flag, struct sock *skip_sk)
37338ceaa00SMarcel Holtmann {
37438ceaa00SMarcel Holtmann 	struct sock *sk;
37538ceaa00SMarcel Holtmann 	__le16 index;
37638ceaa00SMarcel Holtmann 
37738ceaa00SMarcel Holtmann 	if (hdev)
37838ceaa00SMarcel Holtmann 		index = cpu_to_le16(hdev->id);
37938ceaa00SMarcel Holtmann 	else
38038ceaa00SMarcel Holtmann 		index = cpu_to_le16(MGMT_INDEX_NONE);
38138ceaa00SMarcel Holtmann 
38238ceaa00SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
38338ceaa00SMarcel Holtmann 
38438ceaa00SMarcel Holtmann 	sk_for_each(sk, &hci_sk_list.head) {
38538ceaa00SMarcel Holtmann 		struct hci_mon_hdr *hdr;
38638ceaa00SMarcel Holtmann 		struct sk_buff *skb;
38738ceaa00SMarcel Holtmann 
38838ceaa00SMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
38938ceaa00SMarcel Holtmann 			continue;
39038ceaa00SMarcel Holtmann 
39138ceaa00SMarcel Holtmann 		/* Ignore socket without the flag set */
39238ceaa00SMarcel Holtmann 		if (!hci_sock_test_flag(sk, flag))
39338ceaa00SMarcel Holtmann 			continue;
39438ceaa00SMarcel Holtmann 
39538ceaa00SMarcel Holtmann 		/* Skip the original socket */
39638ceaa00SMarcel Holtmann 		if (sk == skip_sk)
39738ceaa00SMarcel Holtmann 			continue;
39838ceaa00SMarcel Holtmann 
39938ceaa00SMarcel Holtmann 		skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC);
40038ceaa00SMarcel Holtmann 		if (!skb)
40138ceaa00SMarcel Holtmann 			continue;
40238ceaa00SMarcel Holtmann 
40338ceaa00SMarcel Holtmann 		put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
40438ceaa00SMarcel Holtmann 		put_unaligned_le16(event, skb_put(skb, 2));
40538ceaa00SMarcel Holtmann 
40638ceaa00SMarcel Holtmann 		if (data)
40759ae1d12SJohannes Berg 			skb_put_data(skb, data, data_len);
40838ceaa00SMarcel Holtmann 
40938ceaa00SMarcel Holtmann 		skb->tstamp = tstamp;
41038ceaa00SMarcel Holtmann 
411d58ff351SJohannes Berg 		hdr = skb_push(skb, HCI_MON_HDR_SIZE);
41238ceaa00SMarcel Holtmann 		hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
41338ceaa00SMarcel Holtmann 		hdr->index = index;
41438ceaa00SMarcel Holtmann 		hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
41538ceaa00SMarcel Holtmann 
416a9ee77afSSebastian Andrzej Siewior 		__hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
41738ceaa00SMarcel Holtmann 				      HCI_SOCK_TRUSTED, NULL);
41838ceaa00SMarcel Holtmann 		kfree_skb(skb);
41938ceaa00SMarcel Holtmann 	}
42038ceaa00SMarcel Holtmann 
42138ceaa00SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
42238ceaa00SMarcel Holtmann }
42338ceaa00SMarcel Holtmann 
424cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
425cd82e61cSMarcel Holtmann {
426cd82e61cSMarcel Holtmann 	struct hci_mon_hdr *hdr;
427cd82e61cSMarcel Holtmann 	struct hci_mon_new_index *ni;
4286c566dd5SMarcel Holtmann 	struct hci_mon_index_info *ii;
429cd82e61cSMarcel Holtmann 	struct sk_buff *skb;
430cd82e61cSMarcel Holtmann 	__le16 opcode;
431cd82e61cSMarcel Holtmann 
432cd82e61cSMarcel Holtmann 	switch (event) {
433cd82e61cSMarcel Holtmann 	case HCI_DEV_REG:
434cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
435cd82e61cSMarcel Holtmann 		if (!skb)
436cd82e61cSMarcel Holtmann 			return NULL;
437cd82e61cSMarcel Holtmann 
4384df864c1SJohannes Berg 		ni = skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
439cd82e61cSMarcel Holtmann 		ni->type = hdev->dev_type;
440cd82e61cSMarcel Holtmann 		ni->bus = hdev->bus;
441cd82e61cSMarcel Holtmann 		bacpy(&ni->bdaddr, &hdev->bdaddr);
442cd82e61cSMarcel Holtmann 		memcpy(ni->name, hdev->name, 8);
443cd82e61cSMarcel Holtmann 
444dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
445cd82e61cSMarcel Holtmann 		break;
446cd82e61cSMarcel Holtmann 
447cd82e61cSMarcel Holtmann 	case HCI_DEV_UNREG:
448cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
449cd82e61cSMarcel Holtmann 		if (!skb)
450cd82e61cSMarcel Holtmann 			return NULL;
451cd82e61cSMarcel Holtmann 
452dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
453cd82e61cSMarcel Holtmann 		break;
454cd82e61cSMarcel Holtmann 
455e131d74aSMarcel Holtmann 	case HCI_DEV_SETUP:
456e131d74aSMarcel Holtmann 		if (hdev->manufacturer == 0xffff)
457e131d74aSMarcel Holtmann 			return NULL;
45819186c7bSGustavo A. R. Silva 		fallthrough;
459e131d74aSMarcel Holtmann 
4606c566dd5SMarcel Holtmann 	case HCI_DEV_UP:
4616c566dd5SMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC);
4626c566dd5SMarcel Holtmann 		if (!skb)
4636c566dd5SMarcel Holtmann 			return NULL;
4646c566dd5SMarcel Holtmann 
4654df864c1SJohannes Berg 		ii = skb_put(skb, HCI_MON_INDEX_INFO_SIZE);
4666c566dd5SMarcel Holtmann 		bacpy(&ii->bdaddr, &hdev->bdaddr);
4676c566dd5SMarcel Holtmann 		ii->manufacturer = cpu_to_le16(hdev->manufacturer);
4686c566dd5SMarcel Holtmann 
4696c566dd5SMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_INDEX_INFO);
4706c566dd5SMarcel Holtmann 		break;
4716c566dd5SMarcel Holtmann 
47222db3cbcSMarcel Holtmann 	case HCI_DEV_OPEN:
47322db3cbcSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
47422db3cbcSMarcel Holtmann 		if (!skb)
47522db3cbcSMarcel Holtmann 			return NULL;
47622db3cbcSMarcel Holtmann 
47722db3cbcSMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_OPEN_INDEX);
47822db3cbcSMarcel Holtmann 		break;
47922db3cbcSMarcel Holtmann 
48022db3cbcSMarcel Holtmann 	case HCI_DEV_CLOSE:
48122db3cbcSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
48222db3cbcSMarcel Holtmann 		if (!skb)
48322db3cbcSMarcel Holtmann 			return NULL;
48422db3cbcSMarcel Holtmann 
48522db3cbcSMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX);
48622db3cbcSMarcel Holtmann 		break;
48722db3cbcSMarcel Holtmann 
488cd82e61cSMarcel Holtmann 	default:
489cd82e61cSMarcel Holtmann 		return NULL;
490cd82e61cSMarcel Holtmann 	}
491cd82e61cSMarcel Holtmann 
492cd82e61cSMarcel Holtmann 	__net_timestamp(skb);
493cd82e61cSMarcel Holtmann 
494d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
495cd82e61cSMarcel Holtmann 	hdr->opcode = opcode;
496cd82e61cSMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
497cd82e61cSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
498cd82e61cSMarcel Holtmann 
499cd82e61cSMarcel Holtmann 	return skb;
500cd82e61cSMarcel Holtmann }
501cd82e61cSMarcel Holtmann 
502249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
503249fa169SMarcel Holtmann {
504249fa169SMarcel Holtmann 	struct hci_mon_hdr *hdr;
505249fa169SMarcel Holtmann 	struct sk_buff *skb;
506d0bef1d2SMarcel Holtmann 	u16 format;
507249fa169SMarcel Holtmann 	u8 ver[3];
508249fa169SMarcel Holtmann 	u32 flags;
509249fa169SMarcel Holtmann 
5100ef2c42fSMarcel Holtmann 	/* No message needed when cookie is not present */
5110ef2c42fSMarcel Holtmann 	if (!hci_pi(sk)->cookie)
5120ef2c42fSMarcel Holtmann 		return NULL;
5130ef2c42fSMarcel Holtmann 
514d0bef1d2SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
515f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
516f81f5b2dSMarcel Holtmann 		format = 0x0000;
517f81f5b2dSMarcel Holtmann 		ver[0] = BT_SUBSYS_VERSION;
518f81f5b2dSMarcel Holtmann 		put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
519f81f5b2dSMarcel Holtmann 		break;
520aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
521aa1638ddSMarcel Holtmann 		format = 0x0001;
522aa1638ddSMarcel Holtmann 		ver[0] = BT_SUBSYS_VERSION;
523aa1638ddSMarcel Holtmann 		put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
524aa1638ddSMarcel Holtmann 		break;
525d0bef1d2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
526d0bef1d2SMarcel Holtmann 		format = 0x0002;
527d0bef1d2SMarcel Holtmann 		mgmt_fill_version_info(ver);
528d0bef1d2SMarcel Holtmann 		break;
529d0bef1d2SMarcel Holtmann 	default:
530d0bef1d2SMarcel Holtmann 		/* No message for unsupported format */
531d0bef1d2SMarcel Holtmann 		return NULL;
532d0bef1d2SMarcel Holtmann 	}
533d0bef1d2SMarcel Holtmann 
534249fa169SMarcel Holtmann 	skb = bt_skb_alloc(14 + TASK_COMM_LEN , GFP_ATOMIC);
535249fa169SMarcel Holtmann 	if (!skb)
536249fa169SMarcel Holtmann 		return NULL;
537249fa169SMarcel Holtmann 
538249fa169SMarcel Holtmann 	flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
539249fa169SMarcel Holtmann 
540249fa169SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
541249fa169SMarcel Holtmann 	put_unaligned_le16(format, skb_put(skb, 2));
54259ae1d12SJohannes Berg 	skb_put_data(skb, ver, sizeof(ver));
543249fa169SMarcel Holtmann 	put_unaligned_le32(flags, skb_put(skb, 4));
544634fef61SJohannes Berg 	skb_put_u8(skb, TASK_COMM_LEN);
54559ae1d12SJohannes Berg 	skb_put_data(skb, hci_pi(sk)->comm, TASK_COMM_LEN);
546249fa169SMarcel Holtmann 
547249fa169SMarcel Holtmann 	__net_timestamp(skb);
548249fa169SMarcel Holtmann 
549d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
550249fa169SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
5510ef2c42fSMarcel Holtmann 	if (hci_pi(sk)->hdev)
5520ef2c42fSMarcel Holtmann 		hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
5530ef2c42fSMarcel Holtmann 	else
554249fa169SMarcel Holtmann 		hdr->index = cpu_to_le16(HCI_DEV_NONE);
555249fa169SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
556249fa169SMarcel Holtmann 
557249fa169SMarcel Holtmann 	return skb;
558249fa169SMarcel Holtmann }
559249fa169SMarcel Holtmann 
560249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
561249fa169SMarcel Holtmann {
562249fa169SMarcel Holtmann 	struct hci_mon_hdr *hdr;
563249fa169SMarcel Holtmann 	struct sk_buff *skb;
564249fa169SMarcel Holtmann 
5650ef2c42fSMarcel Holtmann 	/* No message needed when cookie is not present */
5660ef2c42fSMarcel Holtmann 	if (!hci_pi(sk)->cookie)
5670ef2c42fSMarcel Holtmann 		return NULL;
5680ef2c42fSMarcel Holtmann 
569d0bef1d2SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
570f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
571aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
572d0bef1d2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
573d0bef1d2SMarcel Holtmann 		break;
574d0bef1d2SMarcel Holtmann 	default:
575d0bef1d2SMarcel Holtmann 		/* No message for unsupported format */
576d0bef1d2SMarcel Holtmann 		return NULL;
577d0bef1d2SMarcel Holtmann 	}
578d0bef1d2SMarcel Holtmann 
579249fa169SMarcel Holtmann 	skb = bt_skb_alloc(4, GFP_ATOMIC);
580249fa169SMarcel Holtmann 	if (!skb)
581249fa169SMarcel Holtmann 		return NULL;
582249fa169SMarcel Holtmann 
583249fa169SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
584249fa169SMarcel Holtmann 
585249fa169SMarcel Holtmann 	__net_timestamp(skb);
586249fa169SMarcel Holtmann 
587d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
588249fa169SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
5890ef2c42fSMarcel Holtmann 	if (hci_pi(sk)->hdev)
5900ef2c42fSMarcel Holtmann 		hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
5910ef2c42fSMarcel Holtmann 	else
592249fa169SMarcel Holtmann 		hdr->index = cpu_to_le16(HCI_DEV_NONE);
593249fa169SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
594249fa169SMarcel Holtmann 
595249fa169SMarcel Holtmann 	return skb;
596249fa169SMarcel Holtmann }
597249fa169SMarcel Holtmann 
59838ceaa00SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index,
59938ceaa00SMarcel Holtmann 						   u16 opcode, u16 len,
60038ceaa00SMarcel Holtmann 						   const void *buf)
60138ceaa00SMarcel Holtmann {
60238ceaa00SMarcel Holtmann 	struct hci_mon_hdr *hdr;
60338ceaa00SMarcel Holtmann 	struct sk_buff *skb;
60438ceaa00SMarcel Holtmann 
60538ceaa00SMarcel Holtmann 	skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
60638ceaa00SMarcel Holtmann 	if (!skb)
60738ceaa00SMarcel Holtmann 		return NULL;
60838ceaa00SMarcel Holtmann 
60938ceaa00SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
61038ceaa00SMarcel Holtmann 	put_unaligned_le16(opcode, skb_put(skb, 2));
61138ceaa00SMarcel Holtmann 
61238ceaa00SMarcel Holtmann 	if (buf)
61359ae1d12SJohannes Berg 		skb_put_data(skb, buf, len);
61438ceaa00SMarcel Holtmann 
61538ceaa00SMarcel Holtmann 	__net_timestamp(skb);
61638ceaa00SMarcel Holtmann 
617d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
61838ceaa00SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND);
61938ceaa00SMarcel Holtmann 	hdr->index = cpu_to_le16(index);
62038ceaa00SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
62138ceaa00SMarcel Holtmann 
62238ceaa00SMarcel Holtmann 	return skb;
62338ceaa00SMarcel Holtmann }
62438ceaa00SMarcel Holtmann 
625787b306cSJohannes Berg static void __printf(2, 3)
626787b306cSJohannes Berg send_monitor_note(struct sock *sk, const char *fmt, ...)
627dd31506dSMarcel Holtmann {
628787b306cSJohannes Berg 	size_t len;
629dd31506dSMarcel Holtmann 	struct hci_mon_hdr *hdr;
630dd31506dSMarcel Holtmann 	struct sk_buff *skb;
631787b306cSJohannes Berg 	va_list args;
632787b306cSJohannes Berg 
633787b306cSJohannes Berg 	va_start(args, fmt);
634787b306cSJohannes Berg 	len = vsnprintf(NULL, 0, fmt, args);
635787b306cSJohannes Berg 	va_end(args);
636dd31506dSMarcel Holtmann 
637dd31506dSMarcel Holtmann 	skb = bt_skb_alloc(len + 1, GFP_ATOMIC);
638dd31506dSMarcel Holtmann 	if (!skb)
639dd31506dSMarcel Holtmann 		return;
640dd31506dSMarcel Holtmann 
641787b306cSJohannes Berg 	va_start(args, fmt);
642787b306cSJohannes Berg 	vsprintf(skb_put(skb, len), fmt, args);
6434df864c1SJohannes Berg 	*(u8 *)skb_put(skb, 1) = 0;
644787b306cSJohannes Berg 	va_end(args);
645dd31506dSMarcel Holtmann 
646dd31506dSMarcel Holtmann 	__net_timestamp(skb);
647dd31506dSMarcel Holtmann 
648dd31506dSMarcel Holtmann 	hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
649dd31506dSMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE);
650dd31506dSMarcel Holtmann 	hdr->index = cpu_to_le16(HCI_DEV_NONE);
651dd31506dSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
652dd31506dSMarcel Holtmann 
653dd31506dSMarcel Holtmann 	if (sock_queue_rcv_skb(sk, skb))
654dd31506dSMarcel Holtmann 		kfree_skb(skb);
655dd31506dSMarcel Holtmann }
656dd31506dSMarcel Holtmann 
657cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk)
658cd82e61cSMarcel Holtmann {
659cd82e61cSMarcel Holtmann 	struct hci_dev *hdev;
660cd82e61cSMarcel Holtmann 
661cd82e61cSMarcel Holtmann 	read_lock(&hci_dev_list_lock);
662cd82e61cSMarcel Holtmann 
663cd82e61cSMarcel Holtmann 	list_for_each_entry(hdev, &hci_dev_list, list) {
664cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
665cd82e61cSMarcel Holtmann 
666cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_REG);
667cd82e61cSMarcel Holtmann 		if (!skb)
668cd82e61cSMarcel Holtmann 			continue;
669cd82e61cSMarcel Holtmann 
670cd82e61cSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
671cd82e61cSMarcel Holtmann 			kfree_skb(skb);
67222db3cbcSMarcel Holtmann 
67322db3cbcSMarcel Holtmann 		if (!test_bit(HCI_RUNNING, &hdev->flags))
67422db3cbcSMarcel Holtmann 			continue;
67522db3cbcSMarcel Holtmann 
67622db3cbcSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_OPEN);
67722db3cbcSMarcel Holtmann 		if (!skb)
67822db3cbcSMarcel Holtmann 			continue;
67922db3cbcSMarcel Holtmann 
68022db3cbcSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
68122db3cbcSMarcel Holtmann 			kfree_skb(skb);
6826c566dd5SMarcel Holtmann 
683e131d74aSMarcel Holtmann 		if (test_bit(HCI_UP, &hdev->flags))
6846c566dd5SMarcel Holtmann 			skb = create_monitor_event(hdev, HCI_DEV_UP);
685e131d74aSMarcel Holtmann 		else if (hci_dev_test_flag(hdev, HCI_SETUP))
686e131d74aSMarcel Holtmann 			skb = create_monitor_event(hdev, HCI_DEV_SETUP);
687e131d74aSMarcel Holtmann 		else
688e131d74aSMarcel Holtmann 			skb = NULL;
6896c566dd5SMarcel Holtmann 
690e131d74aSMarcel Holtmann 		if (skb) {
6916c566dd5SMarcel Holtmann 			if (sock_queue_rcv_skb(sk, skb))
6926c566dd5SMarcel Holtmann 				kfree_skb(skb);
693cd82e61cSMarcel Holtmann 		}
694e131d74aSMarcel Holtmann 	}
695cd82e61cSMarcel Holtmann 
696cd82e61cSMarcel Holtmann 	read_unlock(&hci_dev_list_lock);
697cd82e61cSMarcel Holtmann }
698cd82e61cSMarcel Holtmann 
699249fa169SMarcel Holtmann static void send_monitor_control_replay(struct sock *mon_sk)
700249fa169SMarcel Holtmann {
701249fa169SMarcel Holtmann 	struct sock *sk;
702249fa169SMarcel Holtmann 
703249fa169SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
704249fa169SMarcel Holtmann 
705249fa169SMarcel Holtmann 	sk_for_each(sk, &hci_sk_list.head) {
706249fa169SMarcel Holtmann 		struct sk_buff *skb;
707249fa169SMarcel Holtmann 
708249fa169SMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
709249fa169SMarcel Holtmann 		if (!skb)
710249fa169SMarcel Holtmann 			continue;
711249fa169SMarcel Holtmann 
712249fa169SMarcel Holtmann 		if (sock_queue_rcv_skb(mon_sk, skb))
713249fa169SMarcel Holtmann 			kfree_skb(skb);
714249fa169SMarcel Holtmann 	}
715249fa169SMarcel Holtmann 
716249fa169SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
717249fa169SMarcel Holtmann }
718249fa169SMarcel Holtmann 
719040030efSMarcel Holtmann /* Generate internal stack event */
720040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
721040030efSMarcel Holtmann {
722040030efSMarcel Holtmann 	struct hci_event_hdr *hdr;
723040030efSMarcel Holtmann 	struct hci_ev_stack_internal *ev;
724040030efSMarcel Holtmann 	struct sk_buff *skb;
725040030efSMarcel Holtmann 
726040030efSMarcel Holtmann 	skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
727040030efSMarcel Holtmann 	if (!skb)
728040030efSMarcel Holtmann 		return;
729040030efSMarcel Holtmann 
7304df864c1SJohannes Berg 	hdr = skb_put(skb, HCI_EVENT_HDR_SIZE);
731040030efSMarcel Holtmann 	hdr->evt  = HCI_EV_STACK_INTERNAL;
732040030efSMarcel Holtmann 	hdr->plen = sizeof(*ev) + dlen;
733040030efSMarcel Holtmann 
7344df864c1SJohannes Berg 	ev = skb_put(skb, sizeof(*ev) + dlen);
735040030efSMarcel Holtmann 	ev->type = type;
736040030efSMarcel Holtmann 	memcpy(ev->data, data, dlen);
737040030efSMarcel Holtmann 
738040030efSMarcel Holtmann 	bt_cb(skb)->incoming = 1;
739040030efSMarcel Holtmann 	__net_timestamp(skb);
740040030efSMarcel Holtmann 
741d79f34e3SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
742040030efSMarcel Holtmann 	hci_send_to_sock(hdev, skb);
743040030efSMarcel Holtmann 	kfree_skb(skb);
744040030efSMarcel Holtmann }
745040030efSMarcel Holtmann 
746040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event)
747040030efSMarcel Holtmann {
748040030efSMarcel Holtmann 	BT_DBG("hdev %s event %d", hdev->name, event);
749040030efSMarcel Holtmann 
750cd82e61cSMarcel Holtmann 	if (atomic_read(&monitor_promisc)) {
751cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
752cd82e61cSMarcel Holtmann 
753ed1b28a4SMarcel Holtmann 		/* Send event to monitor */
754cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, event);
755cd82e61cSMarcel Holtmann 		if (skb) {
756c08b1a1dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
757c08b1a1dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
758cd82e61cSMarcel Holtmann 			kfree_skb(skb);
759cd82e61cSMarcel Holtmann 		}
760cd82e61cSMarcel Holtmann 	}
761cd82e61cSMarcel Holtmann 
762ed1b28a4SMarcel Holtmann 	if (event <= HCI_DEV_DOWN) {
763ed1b28a4SMarcel Holtmann 		struct hci_ev_si_device ev;
764ed1b28a4SMarcel Holtmann 
765040030efSMarcel Holtmann 		/* Send event to sockets */
766040030efSMarcel Holtmann 		ev.event  = event;
767040030efSMarcel Holtmann 		ev.dev_id = hdev->id;
768040030efSMarcel Holtmann 		hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
769ed1b28a4SMarcel Holtmann 	}
770040030efSMarcel Holtmann 
771040030efSMarcel Holtmann 	if (event == HCI_DEV_UNREG) {
772040030efSMarcel Holtmann 		struct sock *sk;
773040030efSMarcel Holtmann 
774e0448092STetsuo Handa 		/* Wake up sockets using this dead device */
775040030efSMarcel Holtmann 		read_lock(&hci_sk_list.lock);
776b67bfe0dSSasha Levin 		sk_for_each(sk, &hci_sk_list.head) {
777040030efSMarcel Holtmann 			if (hci_pi(sk)->hdev == hdev) {
778040030efSMarcel Holtmann 				sk->sk_err = EPIPE;
779040030efSMarcel Holtmann 				sk->sk_state_change(sk);
780040030efSMarcel Holtmann 			}
781040030efSMarcel Holtmann 		}
782040030efSMarcel Holtmann 		read_unlock(&hci_sk_list.lock);
783040030efSMarcel Holtmann 	}
784040030efSMarcel Holtmann }
785040030efSMarcel Holtmann 
786801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
787801c1e8dSJohan Hedberg {
788801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *c;
789801c1e8dSJohan Hedberg 
790801c1e8dSJohan Hedberg 	list_for_each_entry(c, &mgmt_chan_list, list) {
791801c1e8dSJohan Hedberg 		if (c->channel == channel)
792801c1e8dSJohan Hedberg 			return c;
793801c1e8dSJohan Hedberg 	}
794801c1e8dSJohan Hedberg 
795801c1e8dSJohan Hedberg 	return NULL;
796801c1e8dSJohan Hedberg }
797801c1e8dSJohan Hedberg 
798801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
799801c1e8dSJohan Hedberg {
800801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *c;
801801c1e8dSJohan Hedberg 
802801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
803801c1e8dSJohan Hedberg 	c = __hci_mgmt_chan_find(channel);
804801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
805801c1e8dSJohan Hedberg 
806801c1e8dSJohan Hedberg 	return c;
807801c1e8dSJohan Hedberg }
808801c1e8dSJohan Hedberg 
809801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
810801c1e8dSJohan Hedberg {
811801c1e8dSJohan Hedberg 	if (c->channel < HCI_CHANNEL_CONTROL)
812801c1e8dSJohan Hedberg 		return -EINVAL;
813801c1e8dSJohan Hedberg 
814801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
815801c1e8dSJohan Hedberg 	if (__hci_mgmt_chan_find(c->channel)) {
816801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
817801c1e8dSJohan Hedberg 		return -EALREADY;
818801c1e8dSJohan Hedberg 	}
819801c1e8dSJohan Hedberg 
820801c1e8dSJohan Hedberg 	list_add_tail(&c->list, &mgmt_chan_list);
821801c1e8dSJohan Hedberg 
822801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
823801c1e8dSJohan Hedberg 
824801c1e8dSJohan Hedberg 	return 0;
825801c1e8dSJohan Hedberg }
826801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register);
827801c1e8dSJohan Hedberg 
828801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
829801c1e8dSJohan Hedberg {
830801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
831801c1e8dSJohan Hedberg 	list_del(&c->list);
832801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
833801c1e8dSJohan Hedberg }
834801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister);
835801c1e8dSJohan Hedberg 
8361da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8397b005bd3SMarcel Holtmann 	struct hci_dev *hdev;
840249fa169SMarcel Holtmann 	struct sk_buff *skb;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	if (!sk)
8451da177e4SLinus Torvalds 		return 0;
8461da177e4SLinus Torvalds 
84711eb85ecSDan Carpenter 	lock_sock(sk);
84811eb85ecSDan Carpenter 
84970ecce91SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
85070ecce91SMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
851cd82e61cSMarcel Holtmann 		atomic_dec(&monitor_promisc);
85270ecce91SMarcel Holtmann 		break;
853f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
854aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
85570ecce91SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
856249fa169SMarcel Holtmann 		/* Send event to monitor */
857249fa169SMarcel Holtmann 		skb = create_monitor_ctrl_close(sk);
858249fa169SMarcel Holtmann 		if (skb) {
859249fa169SMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
860249fa169SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
861249fa169SMarcel Holtmann 			kfree_skb(skb);
862249fa169SMarcel Holtmann 		}
863249fa169SMarcel Holtmann 
864df1cb87aSMarcel Holtmann 		hci_sock_free_cookie(sk);
86570ecce91SMarcel Holtmann 		break;
86670ecce91SMarcel Holtmann 	}
867cd82e61cSMarcel Holtmann 
8681da177e4SLinus Torvalds 	bt_sock_unlink(&hci_sk_list, sk);
8691da177e4SLinus Torvalds 
870e20a2e9cSMyungho Jung 	hdev = hci_pi(sk)->hdev;
8711da177e4SLinus Torvalds 	if (hdev) {
87223500189SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
8739332ef9dSMasahiro Yamada 			/* When releasing a user channel exclusive access,
8746b3cc1dbSSimon Fels 			 * call hci_dev_do_close directly instead of calling
8756b3cc1dbSSimon Fels 			 * hci_dev_close to ensure the exclusive access will
8766b3cc1dbSSimon Fels 			 * be released and the controller brought back down.
8776b3cc1dbSSimon Fels 			 *
8786b3cc1dbSSimon Fels 			 * The checking of HCI_AUTO_OFF is not needed in this
8796b3cc1dbSSimon Fels 			 * case since it will have been cleared already when
8806b3cc1dbSSimon Fels 			 * opening the user channel.
8816b3cc1dbSSimon Fels 			 */
8826b3cc1dbSSimon Fels 			hci_dev_do_close(hdev);
8839380f9eaSLoic Poulain 			hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
8849380f9eaSLoic Poulain 			mgmt_index_added(hdev);
88523500189SMarcel Holtmann 		}
88623500189SMarcel Holtmann 
8871da177e4SLinus Torvalds 		atomic_dec(&hdev->promisc);
8881da177e4SLinus Torvalds 		hci_dev_put(hdev);
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	sock_orphan(sk);
89211eb85ecSDan Carpenter 	release_sock(sk);
8931da177e4SLinus Torvalds 	sock_put(sk);
8941da177e4SLinus Torvalds 	return 0;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8973d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg)
898f0358568SJohan Hedberg {
899f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9005e762444SAntti Julku 	int err;
901f0358568SJohan Hedberg 
902f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
903f0358568SJohan Hedberg 		return -EFAULT;
904f0358568SJohan Hedberg 
90509fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9065e762444SAntti Julku 
9073d4f9c00SArchie Pusaka 	err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9085e762444SAntti Julku 
90909fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9105e762444SAntti Julku 
9115e762444SAntti Julku 	return err;
912f0358568SJohan Hedberg }
913f0358568SJohan Hedberg 
9143d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg)
915f0358568SJohan Hedberg {
916f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9175e762444SAntti Julku 	int err;
918f0358568SJohan Hedberg 
919f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
920f0358568SJohan Hedberg 		return -EFAULT;
921f0358568SJohan Hedberg 
92209fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9235e762444SAntti Julku 
9243d4f9c00SArchie Pusaka 	err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9255e762444SAntti Julku 
92609fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9275e762444SAntti Julku 
9285e762444SAntti Julku 	return err;
929f0358568SJohan Hedberg }
930f0358568SJohan Hedberg 
9311da177e4SLinus Torvalds /* Ioctls that require bound socket */
9326039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
9336039aa73SGustavo Padovan 				unsigned long arg)
9341da177e4SLinus Torvalds {
935e0448092STetsuo Handa 	struct hci_dev *hdev = hci_hdev_from_sock(sk);
9361da177e4SLinus Torvalds 
937e0448092STetsuo Handa 	if (IS_ERR(hdev))
938e0448092STetsuo Handa 		return PTR_ERR(hdev);
9391da177e4SLinus Torvalds 
940d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
9410736cfa8SMarcel Holtmann 		return -EBUSY;
9420736cfa8SMarcel Holtmann 
943d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
944fee746b0SMarcel Holtmann 		return -EOPNOTSUPP;
945fee746b0SMarcel Holtmann 
946ca8bee5dSMarcel Holtmann 	if (hdev->dev_type != HCI_PRIMARY)
9475b69bef5SMarcel Holtmann 		return -EOPNOTSUPP;
9485b69bef5SMarcel Holtmann 
9491da177e4SLinus Torvalds 	switch (cmd) {
9501da177e4SLinus Torvalds 	case HCISETRAW:
9511da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
952bf5b30b8SZhao Hongjiang 			return -EPERM;
953db596681SMarcel Holtmann 		return -EOPNOTSUPP;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	case HCIGETCONNINFO:
9561da177e4SLinus Torvalds 		return hci_get_conn_info(hdev, (void __user *)arg);
9571da177e4SLinus Torvalds 
95840be492fSMarcel Holtmann 	case HCIGETAUTHINFO:
95940be492fSMarcel Holtmann 		return hci_get_auth_info(hdev, (void __user *)arg);
96040be492fSMarcel Holtmann 
961f0358568SJohan Hedberg 	case HCIBLOCKADDR:
962f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
963bf5b30b8SZhao Hongjiang 			return -EPERM;
9643d4f9c00SArchie Pusaka 		return hci_sock_reject_list_add(hdev, (void __user *)arg);
965f0358568SJohan Hedberg 
966f0358568SJohan Hedberg 	case HCIUNBLOCKADDR:
967f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
968bf5b30b8SZhao Hongjiang 			return -EPERM;
9693d4f9c00SArchie Pusaka 		return hci_sock_reject_list_del(hdev, (void __user *)arg);
9700736cfa8SMarcel Holtmann 	}
971f0358568SJohan Hedberg 
972324d36edSMarcel Holtmann 	return -ENOIOCTLCMD;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9758fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
9768fc9ced3SGustavo Padovan 			  unsigned long arg)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
9790736cfa8SMarcel Holtmann 	struct sock *sk = sock->sk;
9801da177e4SLinus Torvalds 	int err;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	BT_DBG("cmd %x arg %lx", cmd, arg);
9831da177e4SLinus Torvalds 
984c1c4f956SMarcel Holtmann 	lock_sock(sk);
985c1c4f956SMarcel Holtmann 
986c1c4f956SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
987c1c4f956SMarcel Holtmann 		err = -EBADFD;
988c1c4f956SMarcel Holtmann 		goto done;
989c1c4f956SMarcel Holtmann 	}
990c1c4f956SMarcel Holtmann 
991f81f5b2dSMarcel Holtmann 	/* When calling an ioctl on an unbound raw socket, then ensure
992f81f5b2dSMarcel Holtmann 	 * that the monitor gets informed. Ensure that the resulting event
993f81f5b2dSMarcel Holtmann 	 * is only send once by checking if the cookie exists or not. The
994f81f5b2dSMarcel Holtmann 	 * socket cookie will be only ever generated once for the lifetime
995f81f5b2dSMarcel Holtmann 	 * of a given socket.
996f81f5b2dSMarcel Holtmann 	 */
997f81f5b2dSMarcel Holtmann 	if (hci_sock_gen_cookie(sk)) {
998f81f5b2dSMarcel Holtmann 		struct sk_buff *skb;
999f81f5b2dSMarcel Holtmann 
1000f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1001f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1002f81f5b2dSMarcel Holtmann 
1003f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1004f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1005f81f5b2dSMarcel Holtmann 		if (skb) {
1006f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1007f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1008f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1009f81f5b2dSMarcel Holtmann 		}
1010f81f5b2dSMarcel Holtmann 	}
1011f81f5b2dSMarcel Holtmann 
1012c1c4f956SMarcel Holtmann 	release_sock(sk);
1013c1c4f956SMarcel Holtmann 
10141da177e4SLinus Torvalds 	switch (cmd) {
10151da177e4SLinus Torvalds 	case HCIGETDEVLIST:
10161da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	case HCIGETDEVINFO:
10191da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	case HCIGETCONNLIST:
10221da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	case HCIDEVUP:
10251da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1026bf5b30b8SZhao Hongjiang 			return -EPERM;
10271da177e4SLinus Torvalds 		return hci_dev_open(arg);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	case HCIDEVDOWN:
10301da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1031bf5b30b8SZhao Hongjiang 			return -EPERM;
10321da177e4SLinus Torvalds 		return hci_dev_close(arg);
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	case HCIDEVRESET:
10351da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1036bf5b30b8SZhao Hongjiang 			return -EPERM;
10371da177e4SLinus Torvalds 		return hci_dev_reset(arg);
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	case HCIDEVRESTAT:
10401da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1041bf5b30b8SZhao Hongjiang 			return -EPERM;
10421da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	case HCISETSCAN:
10451da177e4SLinus Torvalds 	case HCISETAUTH:
10461da177e4SLinus Torvalds 	case HCISETENCRYPT:
10471da177e4SLinus Torvalds 	case HCISETPTYPE:
10481da177e4SLinus Torvalds 	case HCISETLINKPOL:
10491da177e4SLinus Torvalds 	case HCISETLINKMODE:
10501da177e4SLinus Torvalds 	case HCISETACLMTU:
10511da177e4SLinus Torvalds 	case HCISETSCOMTU:
10521da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1053bf5b30b8SZhao Hongjiang 			return -EPERM;
10541da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	case HCIINQUIRY:
10571da177e4SLinus Torvalds 		return hci_inquiry(argp);
1058c1c4f956SMarcel Holtmann 	}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	lock_sock(sk);
1061c1c4f956SMarcel Holtmann 
10621da177e4SLinus Torvalds 	err = hci_sock_bound_ioctl(sk, cmd, arg);
1063c1c4f956SMarcel Holtmann 
1064c1c4f956SMarcel Holtmann done:
10651da177e4SLinus Torvalds 	release_sock(sk);
10661da177e4SLinus Torvalds 	return err;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10697a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
10707a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd,
10717a6038b3SArnd Bergmann 				 unsigned long arg)
10727a6038b3SArnd Bergmann {
10737a6038b3SArnd Bergmann 	switch (cmd) {
10747a6038b3SArnd Bergmann 	case HCIDEVUP:
10757a6038b3SArnd Bergmann 	case HCIDEVDOWN:
10767a6038b3SArnd Bergmann 	case HCIDEVRESET:
10777a6038b3SArnd Bergmann 	case HCIDEVRESTAT:
10787a6038b3SArnd Bergmann 		return hci_sock_ioctl(sock, cmd, arg);
10797a6038b3SArnd Bergmann 	}
10807a6038b3SArnd Bergmann 
10817a6038b3SArnd Bergmann 	return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
10827a6038b3SArnd Bergmann }
10837a6038b3SArnd Bergmann #endif
10847a6038b3SArnd Bergmann 
10858fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
10868fc9ced3SGustavo Padovan 			 int addr_len)
10871da177e4SLinus Torvalds {
10880381101fSJohan Hedberg 	struct sockaddr_hci haddr;
10891da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10901da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
1091f4cdbb3fSMarcel Holtmann 	struct sk_buff *skb;
10920381101fSJohan Hedberg 	int len, err = 0;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
10951da177e4SLinus Torvalds 
10960381101fSJohan Hedberg 	if (!addr)
10970381101fSJohan Hedberg 		return -EINVAL;
10980381101fSJohan Hedberg 
10990381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
11000381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
11010381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
11020381101fSJohan Hedberg 
11030381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
11040381101fSJohan Hedberg 		return -EINVAL;
11050381101fSJohan Hedberg 
11061da177e4SLinus Torvalds 	lock_sock(sk);
11071da177e4SLinus Torvalds 
1108e0448092STetsuo Handa 	/* Allow detaching from dead device and attaching to alive device, if
1109e0448092STetsuo Handa 	 * the caller wants to re-bind (instead of close) this socket in
1110e0448092STetsuo Handa 	 * response to hci_sock_dev_event(HCI_DEV_UNREG) notification.
1111e0448092STetsuo Handa 	 */
1112e0448092STetsuo Handa 	hdev = hci_pi(sk)->hdev;
1113e0448092STetsuo Handa 	if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1114e0448092STetsuo Handa 		hci_pi(sk)->hdev = NULL;
1115e0448092STetsuo Handa 		sk->sk_state = BT_OPEN;
1116e0448092STetsuo Handa 		hci_dev_put(hdev);
1117e0448092STetsuo Handa 	}
1118e0448092STetsuo Handa 	hdev = NULL;
1119e0448092STetsuo Handa 
11207cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
11217cc2ade2SMarcel Holtmann 		err = -EALREADY;
11227cc2ade2SMarcel Holtmann 		goto done;
11237cc2ade2SMarcel Holtmann 	}
11247cc2ade2SMarcel Holtmann 
11257cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
11267cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
11277cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
11281da177e4SLinus Torvalds 			err = -EALREADY;
11291da177e4SLinus Torvalds 			goto done;
11301da177e4SLinus Torvalds 		}
11311da177e4SLinus Torvalds 
11320381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
11330381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
113470f23020SAndrei Emeltchenko 			if (!hdev) {
11351da177e4SLinus Torvalds 				err = -ENODEV;
11361da177e4SLinus Torvalds 				goto done;
11371da177e4SLinus Torvalds 			}
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
11401da177e4SLinus Torvalds 		}
11411da177e4SLinus Torvalds 
11425a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1143f81f5b2dSMarcel Holtmann 
1144f4cdbb3fSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1145f4cdbb3fSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1146f4cdbb3fSMarcel Holtmann 			 * then there has been already an ioctl issued against
114791641b79SZheng Yongjun 			 * an unbound socket and with that triggered an open
1148f4cdbb3fSMarcel Holtmann 			 * notification. Send a close notification first to
1149f4cdbb3fSMarcel Holtmann 			 * allow the state transition to bounded.
1150f81f5b2dSMarcel Holtmann 			 */
1151f4cdbb3fSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1152f4cdbb3fSMarcel Holtmann 			if (skb) {
1153f4cdbb3fSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1154f4cdbb3fSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1155f4cdbb3fSMarcel Holtmann 				kfree_skb(skb);
1156f4cdbb3fSMarcel Holtmann 			}
1157f4cdbb3fSMarcel Holtmann 		}
1158f81f5b2dSMarcel Holtmann 
1159f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1160f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1161f81f5b2dSMarcel Holtmann 
1162f4cdbb3fSMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
1163f4cdbb3fSMarcel Holtmann 
1164f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1165f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1166f81f5b2dSMarcel Holtmann 		if (skb) {
1167f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1168f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1169f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1170f81f5b2dSMarcel Holtmann 		}
11717cc2ade2SMarcel Holtmann 		break;
11727cc2ade2SMarcel Holtmann 
117323500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
117423500189SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
117523500189SMarcel Holtmann 			err = -EALREADY;
117623500189SMarcel Holtmann 			goto done;
117723500189SMarcel Holtmann 		}
117823500189SMarcel Holtmann 
117923500189SMarcel Holtmann 		if (haddr.hci_dev == HCI_DEV_NONE) {
118023500189SMarcel Holtmann 			err = -EINVAL;
118123500189SMarcel Holtmann 			goto done;
118223500189SMarcel Holtmann 		}
118323500189SMarcel Holtmann 
118410a8b86fSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
118523500189SMarcel Holtmann 			err = -EPERM;
118623500189SMarcel Holtmann 			goto done;
118723500189SMarcel Holtmann 		}
118823500189SMarcel Holtmann 
118923500189SMarcel Holtmann 		hdev = hci_dev_get(haddr.hci_dev);
119023500189SMarcel Holtmann 		if (!hdev) {
119123500189SMarcel Holtmann 			err = -ENODEV;
119223500189SMarcel Holtmann 			goto done;
119323500189SMarcel Holtmann 		}
119423500189SMarcel Holtmann 
1195781f899fSMarcel Holtmann 		if (test_bit(HCI_INIT, &hdev->flags) ||
1196d7a5a11dSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_SETUP) ||
1197781f899fSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1198781f899fSMarcel Holtmann 		    (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1199781f899fSMarcel Holtmann 		     test_bit(HCI_UP, &hdev->flags))) {
120023500189SMarcel Holtmann 			err = -EBUSY;
120123500189SMarcel Holtmann 			hci_dev_put(hdev);
120223500189SMarcel Holtmann 			goto done;
120323500189SMarcel Holtmann 		}
120423500189SMarcel Holtmann 
1205238be788SMarcel Holtmann 		if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
120623500189SMarcel Holtmann 			err = -EUSERS;
120723500189SMarcel Holtmann 			hci_dev_put(hdev);
120823500189SMarcel Holtmann 			goto done;
120923500189SMarcel Holtmann 		}
121023500189SMarcel Holtmann 
121123500189SMarcel Holtmann 		mgmt_index_removed(hdev);
121223500189SMarcel Holtmann 
121323500189SMarcel Holtmann 		err = hci_dev_open(hdev->id);
121423500189SMarcel Holtmann 		if (err) {
1215781f899fSMarcel Holtmann 			if (err == -EALREADY) {
1216781f899fSMarcel Holtmann 				/* In case the transport is already up and
1217781f899fSMarcel Holtmann 				 * running, clear the error here.
1218781f899fSMarcel Holtmann 				 *
12199332ef9dSMasahiro Yamada 				 * This can happen when opening a user
1220781f899fSMarcel Holtmann 				 * channel and HCI_AUTO_OFF grace period
1221781f899fSMarcel Holtmann 				 * is still active.
1222781f899fSMarcel Holtmann 				 */
1223781f899fSMarcel Holtmann 				err = 0;
1224781f899fSMarcel Holtmann 			} else {
1225a358dc11SMarcel Holtmann 				hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1226c6521401SMarcel Holtmann 				mgmt_index_added(hdev);
122723500189SMarcel Holtmann 				hci_dev_put(hdev);
122823500189SMarcel Holtmann 				goto done;
122923500189SMarcel Holtmann 			}
1230781f899fSMarcel Holtmann 		}
123123500189SMarcel Holtmann 
12325a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1233aa1638ddSMarcel Holtmann 
1234aa1638ddSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1235aa1638ddSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1236aa1638ddSMarcel Holtmann 			 * this socket will transition from a raw socket into
12379332ef9dSMasahiro Yamada 			 * a user channel socket. For a clean transition, send
1238aa1638ddSMarcel Holtmann 			 * the close notification first.
1239aa1638ddSMarcel Holtmann 			 */
1240aa1638ddSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1241aa1638ddSMarcel Holtmann 			if (skb) {
1242aa1638ddSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1243aa1638ddSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1244aa1638ddSMarcel Holtmann 				kfree_skb(skb);
1245aa1638ddSMarcel Holtmann 			}
1246aa1638ddSMarcel Holtmann 		}
1247aa1638ddSMarcel Holtmann 
1248aa1638ddSMarcel Holtmann 		/* The user channel is restricted to CAP_NET_ADMIN
1249aa1638ddSMarcel Holtmann 		 * capabilities and with that implicitly trusted.
1250aa1638ddSMarcel Holtmann 		 */
1251aa1638ddSMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1252aa1638ddSMarcel Holtmann 
125323500189SMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
12545a6d2cf5SMarcel Holtmann 
1255aa1638ddSMarcel Holtmann 		/* Send event to monitor */
1256aa1638ddSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1257aa1638ddSMarcel Holtmann 		if (skb) {
1258aa1638ddSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1259aa1638ddSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1260aa1638ddSMarcel Holtmann 			kfree_skb(skb);
1261aa1638ddSMarcel Holtmann 		}
1262aa1638ddSMarcel Holtmann 
12635a6d2cf5SMarcel Holtmann 		atomic_inc(&hdev->promisc);
126423500189SMarcel Holtmann 		break;
126523500189SMarcel Holtmann 
1266cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1267cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1268cd82e61cSMarcel Holtmann 			err = -EINVAL;
1269cd82e61cSMarcel Holtmann 			goto done;
1270cd82e61cSMarcel Holtmann 		}
1271cd82e61cSMarcel Holtmann 
1272cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
1273cd82e61cSMarcel Holtmann 			err = -EPERM;
1274cd82e61cSMarcel Holtmann 			goto done;
1275cd82e61cSMarcel Holtmann 		}
1276cd82e61cSMarcel Holtmann 
12775a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
12785a6d2cf5SMarcel Holtmann 
127950ebc055SMarcel Holtmann 		/* The monitor interface is restricted to CAP_NET_RAW
128050ebc055SMarcel Holtmann 		 * capabilities and with that implicitly trusted.
128150ebc055SMarcel Holtmann 		 */
128250ebc055SMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
128350ebc055SMarcel Holtmann 
1284787b306cSJohannes Berg 		send_monitor_note(sk, "Linux version %s (%s)",
1285787b306cSJohannes Berg 				  init_utsname()->release,
1286787b306cSJohannes Berg 				  init_utsname()->machine);
12879e8305b3SMarcel Holtmann 		send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
12889e8305b3SMarcel Holtmann 				  BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
1289cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
1290249fa169SMarcel Holtmann 		send_monitor_control_replay(sk);
1291cd82e61cSMarcel Holtmann 
1292cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
1293cd82e61cSMarcel Holtmann 		break;
1294cd82e61cSMarcel Holtmann 
1295ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
1296ac714949SMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1297ac714949SMarcel Holtmann 			err = -EINVAL;
1298ac714949SMarcel Holtmann 			goto done;
1299ac714949SMarcel Holtmann 		}
1300ac714949SMarcel Holtmann 
1301ac714949SMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
1302ac714949SMarcel Holtmann 			err = -EPERM;
1303ac714949SMarcel Holtmann 			goto done;
1304ac714949SMarcel Holtmann 		}
13055a6d2cf5SMarcel Holtmann 
13065a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1307ac714949SMarcel Holtmann 		break;
1308ac714949SMarcel Holtmann 
13097cc2ade2SMarcel Holtmann 	default:
1310801c1e8dSJohan Hedberg 		if (!hci_mgmt_chan_find(haddr.hci_channel)) {
13117cc2ade2SMarcel Holtmann 			err = -EINVAL;
13127cc2ade2SMarcel Holtmann 			goto done;
13137cc2ade2SMarcel Holtmann 		}
13147cc2ade2SMarcel Holtmann 
1315801c1e8dSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
1316801c1e8dSJohan Hedberg 			err = -EINVAL;
1317801c1e8dSJohan Hedberg 			goto done;
1318801c1e8dSJohan Hedberg 		}
1319801c1e8dSJohan Hedberg 
13201195fbb8SMarcel Holtmann 		/* Users with CAP_NET_ADMIN capabilities are allowed
13211195fbb8SMarcel Holtmann 		 * access to all management commands and events. For
13221195fbb8SMarcel Holtmann 		 * untrusted users the interface is restricted and
13231195fbb8SMarcel Holtmann 		 * also only untrusted events are sent.
132450ebc055SMarcel Holtmann 		 */
13251195fbb8SMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
132650ebc055SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
132750ebc055SMarcel Holtmann 
13285a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
13295a6d2cf5SMarcel Holtmann 
1330f9207338SMarcel Holtmann 		/* At the moment the index and unconfigured index events
1331f9207338SMarcel Holtmann 		 * are enabled unconditionally. Setting them on each
1332f9207338SMarcel Holtmann 		 * socket when binding keeps this functionality. They
1333f9207338SMarcel Holtmann 		 * however might be cleared later and then sending of these
1334f9207338SMarcel Holtmann 		 * events will be disabled, but that is then intentional.
1335f6b7712eSMarcel Holtmann 		 *
1336f6b7712eSMarcel Holtmann 		 * This also enables generic events that are safe to be
1337f6b7712eSMarcel Holtmann 		 * received by untrusted users. Example for such events
1338f6b7712eSMarcel Holtmann 		 * are changes to settings, class of device, name etc.
1339f9207338SMarcel Holtmann 		 */
13405a6d2cf5SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
1341f4cdbb3fSMarcel Holtmann 			if (!hci_sock_gen_cookie(sk)) {
1342f4cdbb3fSMarcel Holtmann 				/* In the case when a cookie has already been
134391641b79SZheng Yongjun 				 * assigned, this socket will transition from
1344f4cdbb3fSMarcel Holtmann 				 * a raw socket into a control socket. To
134591641b79SZheng Yongjun 				 * allow for a clean transition, send the
1346f4cdbb3fSMarcel Holtmann 				 * close notification first.
1347f4cdbb3fSMarcel Holtmann 				 */
1348f4cdbb3fSMarcel Holtmann 				skb = create_monitor_ctrl_close(sk);
1349f4cdbb3fSMarcel Holtmann 				if (skb) {
1350f4cdbb3fSMarcel Holtmann 					hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1351f4cdbb3fSMarcel Holtmann 							    HCI_SOCK_TRUSTED, NULL);
1352f4cdbb3fSMarcel Holtmann 					kfree_skb(skb);
1353f4cdbb3fSMarcel Holtmann 				}
1354f4cdbb3fSMarcel Holtmann 			}
135570ecce91SMarcel Holtmann 
1356249fa169SMarcel Holtmann 			/* Send event to monitor */
1357249fa169SMarcel Holtmann 			skb = create_monitor_ctrl_open(sk);
1358249fa169SMarcel Holtmann 			if (skb) {
1359249fa169SMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1360249fa169SMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1361249fa169SMarcel Holtmann 				kfree_skb(skb);
1362249fa169SMarcel Holtmann 			}
1363249fa169SMarcel Holtmann 
1364f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1365f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
13665504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
13675504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
13685504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
13695504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1370f9207338SMarcel Holtmann 		}
1371801c1e8dSJohan Hedberg 		break;
1372801c1e8dSJohan Hedberg 	}
1373801c1e8dSJohan Hedberg 
137409572fcaSLuiz Augusto von Dentz 	/* Default MTU to HCI_MAX_FRAME_SIZE if not set */
137509572fcaSLuiz Augusto von Dentz 	if (!hci_pi(sk)->mtu)
137609572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE;
137709572fcaSLuiz Augusto von Dentz 
13781da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds done:
13811da177e4SLinus Torvalds 	release_sock(sk);
13821da177e4SLinus Torvalds 	return err;
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13858fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
13869b2c45d4SDenys Vlasenko 			    int peer)
13871da177e4SLinus Torvalds {
13881da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
13891da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13909d4b68b2SMarcel Holtmann 	struct hci_dev *hdev;
13919d4b68b2SMarcel Holtmann 	int err = 0;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
13941da177e4SLinus Torvalds 
139506f43cbcSMarcel Holtmann 	if (peer)
139606f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
139706f43cbcSMarcel Holtmann 
13981da177e4SLinus Torvalds 	lock_sock(sk);
13991da177e4SLinus Torvalds 
1400e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1401e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1402e0448092STetsuo Handa 		err = PTR_ERR(hdev);
14039d4b68b2SMarcel Holtmann 		goto done;
14049d4b68b2SMarcel Holtmann 	}
14059d4b68b2SMarcel Holtmann 
14061da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
14077b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
14089d4b68b2SMarcel Holtmann 	haddr->hci_channel= hci_pi(sk)->channel;
14099b2c45d4SDenys Vlasenko 	err = sizeof(*haddr);
14101da177e4SLinus Torvalds 
14119d4b68b2SMarcel Holtmann done:
14121da177e4SLinus Torvalds 	release_sock(sk);
14139d4b68b2SMarcel Holtmann 	return err;
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
14166039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
14176039aa73SGustavo Padovan 			  struct sk_buff *skb)
14181da177e4SLinus Torvalds {
141932929e1fSAlain Michaud 	__u8 mask = hci_pi(sk)->cmsg_mask;
14201da177e4SLinus Torvalds 
14210d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
14220d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
14238fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
14248fc9ced3SGustavo Padovan 			 &incoming);
14250d48d939SMarcel Holtmann 	}
14261da177e4SLinus Torvalds 
1427a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
1428f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
142913c6ee2aSDeepa Dinamani 		struct old_timeval32 ctv;
1430f6e623a6SJohann Felix Soden #endif
143113c6ee2aSDeepa Dinamani 		struct __kernel_old_timeval tv;
1432767c5eb5SMarcel Holtmann 		void *data;
1433767c5eb5SMarcel Holtmann 		int len;
1434a61bbcf2SPatrick McHardy 
1435a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
1436767c5eb5SMarcel Holtmann 
14371da97f83SDavid S. Miller 		data = &tv;
14381da97f83SDavid S. Miller 		len = sizeof(tv);
14391da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
1440da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
1441da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
1442767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
1443767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
1444767c5eb5SMarcel Holtmann 			data = &ctv;
1445767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
1446767c5eb5SMarcel Holtmann 		}
14471da97f83SDavid S. Miller #endif
1448767c5eb5SMarcel Holtmann 
1449767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
1450a61bbcf2SPatrick McHardy 	}
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14538528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
14548528d3f7SMarcel Holtmann 			    size_t len, int flags)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	int noblock = flags & MSG_DONTWAIT;
14571da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
14581da177e4SLinus Torvalds 	struct sk_buff *skb;
14591da177e4SLinus Torvalds 	int copied, err;
146083871f8cSDenis Kenzior 	unsigned int skblen;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
14631da177e4SLinus Torvalds 
1464d94a6104SMarcel Holtmann 	if (flags & MSG_OOB)
14651da177e4SLinus Torvalds 		return -EOPNOTSUPP;
14661da177e4SLinus Torvalds 
1467ac714949SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1468ac714949SMarcel Holtmann 		return -EOPNOTSUPP;
1469ac714949SMarcel Holtmann 
14701da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
14711da177e4SLinus Torvalds 		return 0;
14721da177e4SLinus Torvalds 
147370f23020SAndrei Emeltchenko 	skb = skb_recv_datagram(sk, flags, noblock, &err);
147470f23020SAndrei Emeltchenko 	if (!skb)
14751da177e4SLinus Torvalds 		return err;
14761da177e4SLinus Torvalds 
147783871f8cSDenis Kenzior 	skblen = skb->len;
14781da177e4SLinus Torvalds 	copied = skb->len;
14791da177e4SLinus Torvalds 	if (len < copied) {
14801da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
14811da177e4SLinus Torvalds 		copied = len;
14821da177e4SLinus Torvalds 	}
14831da177e4SLinus Torvalds 
1484badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
148551f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
14861da177e4SLinus Torvalds 
14873a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
14883a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
14891da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
14903a208627SMarcel Holtmann 		break;
149123500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
1492cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1493cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
1494cd82e61cSMarcel Holtmann 		break;
1495801c1e8dSJohan Hedberg 	default:
1496801c1e8dSJohan Hedberg 		if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1497801c1e8dSJohan Hedberg 			sock_recv_timestamp(msg, sk, skb);
1498801c1e8dSJohan Hedberg 		break;
14993a208627SMarcel Holtmann 	}
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
15021da177e4SLinus Torvalds 
15034f34228bSLuiz Augusto von Dentz 	if (flags & MSG_TRUNC)
150483871f8cSDenis Kenzior 		copied = skblen;
150583871f8cSDenis Kenzior 
15061da177e4SLinus Torvalds 	return err ? : copied;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
150964ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
151064ba2eb3SLuiz Augusto von Dentz 			struct sk_buff *skb)
1511fa4335d7SJohan Hedberg {
1512fa4335d7SJohan Hedberg 	u8 *cp;
1513fa4335d7SJohan Hedberg 	struct mgmt_hdr *hdr;
1514fa4335d7SJohan Hedberg 	u16 opcode, index, len;
1515fa4335d7SJohan Hedberg 	struct hci_dev *hdev = NULL;
1516fa4335d7SJohan Hedberg 	const struct hci_mgmt_handler *handler;
1517fa4335d7SJohan Hedberg 	bool var_len, no_hdev;
1518fa4335d7SJohan Hedberg 	int err;
1519fa4335d7SJohan Hedberg 
152064ba2eb3SLuiz Augusto von Dentz 	BT_DBG("got %d bytes", skb->len);
1521fa4335d7SJohan Hedberg 
152264ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr))
1523fa4335d7SJohan Hedberg 		return -EINVAL;
1524fa4335d7SJohan Hedberg 
152564ba2eb3SLuiz Augusto von Dentz 	hdr = (void *)skb->data;
1526fa4335d7SJohan Hedberg 	opcode = __le16_to_cpu(hdr->opcode);
1527fa4335d7SJohan Hedberg 	index = __le16_to_cpu(hdr->index);
1528fa4335d7SJohan Hedberg 	len = __le16_to_cpu(hdr->len);
1529fa4335d7SJohan Hedberg 
153064ba2eb3SLuiz Augusto von Dentz 	if (len != skb->len - sizeof(*hdr)) {
1531fa4335d7SJohan Hedberg 		err = -EINVAL;
1532fa4335d7SJohan Hedberg 		goto done;
1533fa4335d7SJohan Hedberg 	}
1534fa4335d7SJohan Hedberg 
153538ceaa00SMarcel Holtmann 	if (chan->channel == HCI_CHANNEL_CONTROL) {
153664ba2eb3SLuiz Augusto von Dentz 		struct sk_buff *cmd;
153738ceaa00SMarcel Holtmann 
153838ceaa00SMarcel Holtmann 		/* Send event to monitor */
153964ba2eb3SLuiz Augusto von Dentz 		cmd = create_monitor_ctrl_command(sk, index, opcode, len,
154064ba2eb3SLuiz Augusto von Dentz 						  skb->data + sizeof(*hdr));
154164ba2eb3SLuiz Augusto von Dentz 		if (cmd) {
154264ba2eb3SLuiz Augusto von Dentz 			hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd,
154338ceaa00SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
154464ba2eb3SLuiz Augusto von Dentz 			kfree_skb(cmd);
154538ceaa00SMarcel Holtmann 		}
154638ceaa00SMarcel Holtmann 	}
154738ceaa00SMarcel Holtmann 
1548fa4335d7SJohan Hedberg 	if (opcode >= chan->handler_count ||
1549fa4335d7SJohan Hedberg 	    chan->handlers[opcode].func == NULL) {
1550fa4335d7SJohan Hedberg 		BT_DBG("Unknown op %u", opcode);
1551fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1552fa4335d7SJohan Hedberg 				      MGMT_STATUS_UNKNOWN_COMMAND);
1553fa4335d7SJohan Hedberg 		goto done;
1554fa4335d7SJohan Hedberg 	}
1555fa4335d7SJohan Hedberg 
1556fa4335d7SJohan Hedberg 	handler = &chan->handlers[opcode];
1557fa4335d7SJohan Hedberg 
1558fa4335d7SJohan Hedberg 	if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1559fa4335d7SJohan Hedberg 	    !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1560fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1561fa4335d7SJohan Hedberg 				      MGMT_STATUS_PERMISSION_DENIED);
1562fa4335d7SJohan Hedberg 		goto done;
1563fa4335d7SJohan Hedberg 	}
1564fa4335d7SJohan Hedberg 
1565fa4335d7SJohan Hedberg 	if (index != MGMT_INDEX_NONE) {
1566fa4335d7SJohan Hedberg 		hdev = hci_dev_get(index);
1567fa4335d7SJohan Hedberg 		if (!hdev) {
1568fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1569fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1570fa4335d7SJohan Hedberg 			goto done;
1571fa4335d7SJohan Hedberg 		}
1572fa4335d7SJohan Hedberg 
1573fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1574fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1575fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1576fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1577fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1578fa4335d7SJohan Hedberg 			goto done;
1579fa4335d7SJohan Hedberg 		}
1580fa4335d7SJohan Hedberg 
1581fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1582fa4335d7SJohan Hedberg 		    !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1583fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1584fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1585fa4335d7SJohan Hedberg 			goto done;
1586fa4335d7SJohan Hedberg 		}
1587fa4335d7SJohan Hedberg 	}
1588fa4335d7SJohan Hedberg 
1589d5cc6626SMarcel Holtmann 	if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) {
1590fa4335d7SJohan Hedberg 		no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1591fa4335d7SJohan Hedberg 		if (no_hdev != !hdev) {
1592fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1593fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1594fa4335d7SJohan Hedberg 			goto done;
1595fa4335d7SJohan Hedberg 		}
1596d5cc6626SMarcel Holtmann 	}
1597fa4335d7SJohan Hedberg 
1598fa4335d7SJohan Hedberg 	var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1599fa4335d7SJohan Hedberg 	if ((var_len && len < handler->data_len) ||
1600fa4335d7SJohan Hedberg 	    (!var_len && len != handler->data_len)) {
1601fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1602fa4335d7SJohan Hedberg 				      MGMT_STATUS_INVALID_PARAMS);
1603fa4335d7SJohan Hedberg 		goto done;
1604fa4335d7SJohan Hedberg 	}
1605fa4335d7SJohan Hedberg 
1606fa4335d7SJohan Hedberg 	if (hdev && chan->hdev_init)
1607fa4335d7SJohan Hedberg 		chan->hdev_init(sk, hdev);
1608fa4335d7SJohan Hedberg 
160964ba2eb3SLuiz Augusto von Dentz 	cp = skb->data + sizeof(*hdr);
1610fa4335d7SJohan Hedberg 
1611fa4335d7SJohan Hedberg 	err = handler->func(sk, hdev, cp, len);
1612fa4335d7SJohan Hedberg 	if (err < 0)
1613fa4335d7SJohan Hedberg 		goto done;
1614fa4335d7SJohan Hedberg 
161564ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1616fa4335d7SJohan Hedberg 
1617fa4335d7SJohan Hedberg done:
1618fa4335d7SJohan Hedberg 	if (hdev)
1619fa4335d7SJohan Hedberg 		hci_dev_put(hdev);
1620fa4335d7SJohan Hedberg 
1621fa4335d7SJohan Hedberg 	return err;
1622fa4335d7SJohan Hedberg }
1623fa4335d7SJohan Hedberg 
162464ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb,
162564ba2eb3SLuiz Augusto von Dentz 			     unsigned int flags)
1626ac714949SMarcel Holtmann {
1627ac714949SMarcel Holtmann 	struct hci_mon_hdr *hdr;
1628ac714949SMarcel Holtmann 	struct hci_dev *hdev;
1629ac714949SMarcel Holtmann 	u16 index;
1630ac714949SMarcel Holtmann 	int err;
1631ac714949SMarcel Holtmann 
1632ac714949SMarcel Holtmann 	/* The logging frame consists at minimum of the standard header,
1633ac714949SMarcel Holtmann 	 * the priority byte, the ident length byte and at least one string
1634ac714949SMarcel Holtmann 	 * terminator NUL byte. Anything shorter are invalid packets.
1635ac714949SMarcel Holtmann 	 */
163664ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr) + 3)
1637ac714949SMarcel Holtmann 		return -EINVAL;
1638ac714949SMarcel Holtmann 
1639ac714949SMarcel Holtmann 	hdr = (void *)skb->data;
1640ac714949SMarcel Holtmann 
164164ba2eb3SLuiz Augusto von Dentz 	if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr))
164264ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1643ac714949SMarcel Holtmann 
1644ac714949SMarcel Holtmann 	if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1645ac714949SMarcel Holtmann 		__u8 priority = skb->data[sizeof(*hdr)];
1646ac714949SMarcel Holtmann 		__u8 ident_len = skb->data[sizeof(*hdr) + 1];
1647ac714949SMarcel Holtmann 
1648ac714949SMarcel Holtmann 		/* Only the priorities 0-7 are valid and with that any other
1649ac714949SMarcel Holtmann 		 * value results in an invalid packet.
1650ac714949SMarcel Holtmann 		 *
1651ac714949SMarcel Holtmann 		 * The priority byte is followed by an ident length byte and
1652ac714949SMarcel Holtmann 		 * the NUL terminated ident string. Check that the ident
1653ac714949SMarcel Holtmann 		 * length is not overflowing the packet and also that the
1654ac714949SMarcel Holtmann 		 * ident string itself is NUL terminated. In case the ident
1655ac714949SMarcel Holtmann 		 * length is zero, the length value actually doubles as NUL
1656ac714949SMarcel Holtmann 		 * terminator identifier.
1657ac714949SMarcel Holtmann 		 *
1658ac714949SMarcel Holtmann 		 * The message follows the ident string (if present) and
1659ac714949SMarcel Holtmann 		 * must be NUL terminated. Otherwise it is not a valid packet.
1660ac714949SMarcel Holtmann 		 */
166164ba2eb3SLuiz Augusto von Dentz 		if (priority > 7 || skb->data[skb->len - 1] != 0x00 ||
166264ba2eb3SLuiz Augusto von Dentz 		    ident_len > skb->len - sizeof(*hdr) - 3 ||
166364ba2eb3SLuiz Augusto von Dentz 		    skb->data[sizeof(*hdr) + ident_len + 1] != 0x00)
166464ba2eb3SLuiz Augusto von Dentz 			return -EINVAL;
1665ac714949SMarcel Holtmann 	} else {
166664ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1667ac714949SMarcel Holtmann 	}
1668ac714949SMarcel Holtmann 
1669ac714949SMarcel Holtmann 	index = __le16_to_cpu(hdr->index);
1670ac714949SMarcel Holtmann 
1671ac714949SMarcel Holtmann 	if (index != MGMT_INDEX_NONE) {
1672ac714949SMarcel Holtmann 		hdev = hci_dev_get(index);
167364ba2eb3SLuiz Augusto von Dentz 		if (!hdev)
167464ba2eb3SLuiz Augusto von Dentz 			return -ENODEV;
1675ac714949SMarcel Holtmann 	} else {
1676ac714949SMarcel Holtmann 		hdev = NULL;
1677ac714949SMarcel Holtmann 	}
1678ac714949SMarcel Holtmann 
1679ac714949SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1680ac714949SMarcel Holtmann 
1681ac714949SMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
168264ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1683ac714949SMarcel Holtmann 
1684ac714949SMarcel Holtmann 	if (hdev)
1685ac714949SMarcel Holtmann 		hci_dev_put(hdev);
1686ac714949SMarcel Holtmann 
1687ac714949SMarcel Holtmann 	return err;
1688ac714949SMarcel Holtmann }
1689ac714949SMarcel Holtmann 
16901b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
16911b784140SYing Xue 			    size_t len)
16921da177e4SLinus Torvalds {
16931da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1694801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *chan;
16951da177e4SLinus Torvalds 	struct hci_dev *hdev;
16961da177e4SLinus Torvalds 	struct sk_buff *skb;
16971da177e4SLinus Torvalds 	int err;
16980b59e272STetsuo Handa 	const unsigned int flags = msg->msg_flags;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
17011da177e4SLinus Torvalds 
17020b59e272STetsuo Handa 	if (flags & MSG_OOB)
17031da177e4SLinus Torvalds 		return -EOPNOTSUPP;
17041da177e4SLinus Torvalds 
17050b59e272STetsuo Handa 	if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
17061da177e4SLinus Torvalds 		return -EINVAL;
17071da177e4SLinus Torvalds 
170809572fcaSLuiz Augusto von Dentz 	if (len < 4 || len > hci_pi(sk)->mtu)
17091da177e4SLinus Torvalds 		return -EINVAL;
17101da177e4SLinus Torvalds 
171164ba2eb3SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
171264ba2eb3SLuiz Augusto von Dentz 	if (IS_ERR(skb))
171364ba2eb3SLuiz Augusto von Dentz 		return PTR_ERR(skb);
17140b59e272STetsuo Handa 
17151da177e4SLinus Torvalds 	lock_sock(sk);
17161da177e4SLinus Torvalds 
17170381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
17180381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
171923500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
17200381101fSJohan Hedberg 		break;
1721cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1722cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
172364ba2eb3SLuiz Augusto von Dentz 		goto drop;
1724ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
172564ba2eb3SLuiz Augusto von Dentz 		err = hci_logging_frame(sk, skb, flags);
172664ba2eb3SLuiz Augusto von Dentz 		goto drop;
17270381101fSJohan Hedberg 	default:
1728801c1e8dSJohan Hedberg 		mutex_lock(&mgmt_chan_list_lock);
1729801c1e8dSJohan Hedberg 		chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1730801c1e8dSJohan Hedberg 		if (chan)
173164ba2eb3SLuiz Augusto von Dentz 			err = hci_mgmt_cmd(chan, sk, skb);
1732801c1e8dSJohan Hedberg 		else
17330381101fSJohan Hedberg 			err = -EINVAL;
1734801c1e8dSJohan Hedberg 
1735801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
173664ba2eb3SLuiz Augusto von Dentz 		goto drop;
17370381101fSJohan Hedberg 	}
17380381101fSJohan Hedberg 
1739e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1740e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1741e0448092STetsuo Handa 		err = PTR_ERR(hdev);
174264ba2eb3SLuiz Augusto von Dentz 		goto drop;
17431da177e4SLinus Torvalds 	}
17441da177e4SLinus Torvalds 
17457e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
17467e21addcSMarcel Holtmann 		err = -ENETDOWN;
174764ba2eb3SLuiz Augusto von Dentz 		goto drop;
17487e21addcSMarcel Holtmann 	}
17497e21addcSMarcel Holtmann 
17508528d3f7SMarcel Holtmann 	hci_skb_pkt_type(skb) = skb->data[0];
17511da177e4SLinus Torvalds 	skb_pull(skb, 1);
17521da177e4SLinus Torvalds 
17531bc5ad16SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
17541bc5ad16SMarcel Holtmann 		/* No permission check is needed for user channel
17551bc5ad16SMarcel Holtmann 		 * since that gets enforced when binding the socket.
17561bc5ad16SMarcel Holtmann 		 *
17571bc5ad16SMarcel Holtmann 		 * However check that the packet type is valid.
17581bc5ad16SMarcel Holtmann 		 */
1759d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1760d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1761cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1762cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
17631bc5ad16SMarcel Holtmann 			err = -EINVAL;
17641bc5ad16SMarcel Holtmann 			goto drop;
17651bc5ad16SMarcel Holtmann 		}
17661bc5ad16SMarcel Holtmann 
17671bc5ad16SMarcel Holtmann 		skb_queue_tail(&hdev->raw_q, skb);
17681bc5ad16SMarcel Holtmann 		queue_work(hdev->workqueue, &hdev->tx_work);
1769d79f34e3SMarcel Holtmann 	} else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
177083985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
17711da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
17721da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
17753bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
17763bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
17771da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
17781da177e4SLinus Torvalds 			err = -EPERM;
17791da177e4SLinus Torvalds 			goto drop;
17801da177e4SLinus Torvalds 		}
17811da177e4SLinus Torvalds 
17821982162bSMarcel Holtmann 		/* Since the opcode has already been extracted here, store
17831982162bSMarcel Holtmann 		 * a copy of the value for later use by the drivers.
17841982162bSMarcel Holtmann 		 */
17851982162bSMarcel Holtmann 		hci_skb_opcode(skb) = opcode;
17861982162bSMarcel Holtmann 
1787fee746b0SMarcel Holtmann 		if (ogf == 0x3f) {
17881da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
17893eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
17901da177e4SLinus Torvalds 		} else {
179149c922bbSStephen Hemminger 			/* Stand-alone HCI commands must be flagged as
179211714b3dSJohan Hedberg 			 * single-command requests.
179311714b3dSJohan Hedberg 			 */
179444d27137SJohan Hedberg 			bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
179511714b3dSJohan Hedberg 
17961da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
1797c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
17981da177e4SLinus Torvalds 		}
17991da177e4SLinus Torvalds 	} else {
18001da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
18011da177e4SLinus Torvalds 			err = -EPERM;
18021da177e4SLinus Torvalds 			goto drop;
18031da177e4SLinus Torvalds 		}
18041da177e4SLinus Torvalds 
1805d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1806cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1807cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
1808bb77543eSMarcel Holtmann 			err = -EINVAL;
1809bb77543eSMarcel Holtmann 			goto drop;
1810bb77543eSMarcel Holtmann 		}
1811bb77543eSMarcel Holtmann 
18121da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
18133eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
18141da177e4SLinus Torvalds 	}
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	err = len;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds done:
18191da177e4SLinus Torvalds 	release_sock(sk);
18201da177e4SLinus Torvalds 	return err;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds drop:
18231da177e4SLinus Torvalds 	kfree_skb(skb);
18241da177e4SLinus Torvalds 	goto done;
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
182709572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname,
1828a7b75c5aSChristoph Hellwig 				   sockptr_t optval, unsigned int len)
18291da177e4SLinus Torvalds {
18301da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
18311da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
18321da177e4SLinus Torvalds 	int err = 0, opt = 0;
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	lock_sock(sk);
18371da177e4SLinus Torvalds 
18382f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1839c2371e80SMarcel Holtmann 		err = -EBADFD;
18402f39cdb7SMarcel Holtmann 		goto done;
18412f39cdb7SMarcel Holtmann 	}
18422f39cdb7SMarcel Holtmann 
18431da177e4SLinus Torvalds 	switch (optname) {
18441da177e4SLinus Torvalds 	case HCI_DATA_DIR:
1845a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
18461da177e4SLinus Torvalds 			err = -EFAULT;
18471da177e4SLinus Torvalds 			break;
18481da177e4SLinus Torvalds 		}
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 		if (opt)
18511da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
18521da177e4SLinus Torvalds 		else
18531da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
18541da177e4SLinus Torvalds 		break;
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
1857a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
18581da177e4SLinus Torvalds 			err = -EFAULT;
18591da177e4SLinus Torvalds 			break;
18601da177e4SLinus Torvalds 		}
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 		if (opt)
18631da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
18641da177e4SLinus Torvalds 		else
18651da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
18661da177e4SLinus Torvalds 		break;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	case HCI_FILTER:
18690878b666SMarcel Holtmann 		{
18700878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
18710878b666SMarcel Holtmann 
18720878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
18730878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
18740878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
18750878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
18760878b666SMarcel Holtmann 		}
18770878b666SMarcel Holtmann 
18781da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
1879a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&uf, optval, len)) {
18801da177e4SLinus Torvalds 			err = -EFAULT;
18811da177e4SLinus Torvalds 			break;
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
18851da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
18861da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
18871da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
18881da177e4SLinus Torvalds 		}
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 		{
18911da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
18941da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
18951da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
18961da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
18971da177e4SLinus Torvalds 		}
18981da177e4SLinus Torvalds 		break;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	default:
19011da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
19021da177e4SLinus Torvalds 		break;
19031da177e4SLinus Torvalds 	}
19041da177e4SLinus Torvalds 
19052f39cdb7SMarcel Holtmann done:
19061da177e4SLinus Torvalds 	release_sock(sk);
19071da177e4SLinus Torvalds 	return err;
19081da177e4SLinus Torvalds }
19091da177e4SLinus Torvalds 
191009572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
191109572fcaSLuiz Augusto von Dentz 			       sockptr_t optval, unsigned int len)
191209572fcaSLuiz Augusto von Dentz {
191309572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
191409572fcaSLuiz Augusto von Dentz 	int err = 0, opt = 0;
191509572fcaSLuiz Augusto von Dentz 
191609572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
191709572fcaSLuiz Augusto von Dentz 
191809572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
191909572fcaSLuiz Augusto von Dentz 		return hci_sock_setsockopt_old(sock, level, optname, optval,
192009572fcaSLuiz Augusto von Dentz 					       len);
192109572fcaSLuiz Augusto von Dentz 
192209572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
192309572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
192409572fcaSLuiz Augusto von Dentz 
192509572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
192609572fcaSLuiz Augusto von Dentz 
192709572fcaSLuiz Augusto von Dentz 	switch (optname) {
192809572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
192909572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
193009572fcaSLuiz Augusto von Dentz 		switch (hci_pi(sk)->channel) {
193109572fcaSLuiz Augusto von Dentz 		/* Don't allow changing MTU for channels that are meant for HCI
193209572fcaSLuiz Augusto von Dentz 		 * traffic only.
193309572fcaSLuiz Augusto von Dentz 		 */
193409572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_RAW:
193509572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_USER:
193609572fcaSLuiz Augusto von Dentz 			err = -ENOPROTOOPT;
193709572fcaSLuiz Augusto von Dentz 			goto done;
193809572fcaSLuiz Augusto von Dentz 		}
193909572fcaSLuiz Augusto von Dentz 
194009572fcaSLuiz Augusto von Dentz 		if (copy_from_sockptr(&opt, optval, sizeof(u16))) {
194109572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
194209572fcaSLuiz Augusto von Dentz 			break;
194309572fcaSLuiz Augusto von Dentz 		}
194409572fcaSLuiz Augusto von Dentz 
194509572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = opt;
194609572fcaSLuiz Augusto von Dentz 		break;
194709572fcaSLuiz Augusto von Dentz 
194809572fcaSLuiz Augusto von Dentz 	default:
194909572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
195009572fcaSLuiz Augusto von Dentz 		break;
195109572fcaSLuiz Augusto von Dentz 	}
195209572fcaSLuiz Augusto von Dentz 
195309572fcaSLuiz Augusto von Dentz done:
195409572fcaSLuiz Augusto von Dentz 	release_sock(sk);
195509572fcaSLuiz Augusto von Dentz 	return err;
195609572fcaSLuiz Augusto von Dentz }
195709572fcaSLuiz Augusto von Dentz 
195809572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
19598fc9ced3SGustavo Padovan 				   char __user *optval, int __user *optlen)
19601da177e4SLinus Torvalds {
19611da177e4SLinus Torvalds 	struct hci_ufilter uf;
19621da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1963cedc5469SMarcel Holtmann 	int len, opt, err = 0;
1964cedc5469SMarcel Holtmann 
1965cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	if (get_user(len, optlen))
19681da177e4SLinus Torvalds 		return -EFAULT;
19691da177e4SLinus Torvalds 
1970cedc5469SMarcel Holtmann 	lock_sock(sk);
1971cedc5469SMarcel Holtmann 
1972cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1973c2371e80SMarcel Holtmann 		err = -EBADFD;
1974cedc5469SMarcel Holtmann 		goto done;
1975cedc5469SMarcel Holtmann 	}
1976cedc5469SMarcel Holtmann 
19771da177e4SLinus Torvalds 	switch (optname) {
19781da177e4SLinus Torvalds 	case HCI_DATA_DIR:
19791da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
19801da177e4SLinus Torvalds 			opt = 1;
19811da177e4SLinus Torvalds 		else
19821da177e4SLinus Torvalds 			opt = 0;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 		if (put_user(opt, optval))
1985cedc5469SMarcel Holtmann 			err = -EFAULT;
19861da177e4SLinus Torvalds 		break;
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
19891da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
19901da177e4SLinus Torvalds 			opt = 1;
19911da177e4SLinus Torvalds 		else
19921da177e4SLinus Torvalds 			opt = 0;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 		if (put_user(opt, optval))
1995cedc5469SMarcel Holtmann 			err = -EFAULT;
19961da177e4SLinus Torvalds 		break;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	case HCI_FILTER:
19991da177e4SLinus Torvalds 		{
20001da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
20011da177e4SLinus Torvalds 
2002e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
20031da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
20041da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
20051da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
20061da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
20071da177e4SLinus Torvalds 		}
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
20101da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
2011cedc5469SMarcel Holtmann 			err = -EFAULT;
20121da177e4SLinus Torvalds 		break;
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	default:
2015cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
20161da177e4SLinus Torvalds 		break;
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 
2019cedc5469SMarcel Holtmann done:
2020cedc5469SMarcel Holtmann 	release_sock(sk);
2021cedc5469SMarcel Holtmann 	return err;
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
202409572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
202509572fcaSLuiz Augusto von Dentz 			       char __user *optval, int __user *optlen)
202609572fcaSLuiz Augusto von Dentz {
202709572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
202809572fcaSLuiz Augusto von Dentz 	int err = 0;
202909572fcaSLuiz Augusto von Dentz 
203009572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
203109572fcaSLuiz Augusto von Dentz 
203209572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
203309572fcaSLuiz Augusto von Dentz 		return hci_sock_getsockopt_old(sock, level, optname, optval,
203409572fcaSLuiz Augusto von Dentz 					       optlen);
203509572fcaSLuiz Augusto von Dentz 
203609572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
203709572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
203809572fcaSLuiz Augusto von Dentz 
203909572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
204009572fcaSLuiz Augusto von Dentz 
204109572fcaSLuiz Augusto von Dentz 	switch (optname) {
204209572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
204309572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
204409572fcaSLuiz Augusto von Dentz 		if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval))
204509572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
204609572fcaSLuiz Augusto von Dentz 		break;
204709572fcaSLuiz Augusto von Dentz 
204809572fcaSLuiz Augusto von Dentz 	default:
204909572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
205009572fcaSLuiz Augusto von Dentz 		break;
205109572fcaSLuiz Augusto von Dentz 	}
205209572fcaSLuiz Augusto von Dentz 
205309572fcaSLuiz Augusto von Dentz 	release_sock(sk);
205409572fcaSLuiz Augusto von Dentz 	return err;
205509572fcaSLuiz Augusto von Dentz }
205609572fcaSLuiz Augusto von Dentz 
2057*709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk)
2058*709fca50SNguyen Dinh Phi {
2059*709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_receive_queue);
2060*709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_write_queue);
2061*709fca50SNguyen Dinh Phi }
2062*709fca50SNguyen Dinh Phi 
206390ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
20641da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
20651da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
20661da177e4SLinus Torvalds 	.release	= hci_sock_release,
20671da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
20681da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
20691da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
20701da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
20711da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
20727a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
20737a6038b3SArnd Bergmann 	.compat_ioctl	= hci_sock_compat_ioctl,
20747a6038b3SArnd Bergmann #endif
2075a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
20761da177e4SLinus Torvalds 	.listen		= sock_no_listen,
20771da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
20781da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
20791da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
20801da177e4SLinus Torvalds 	.connect	= sock_no_connect,
20811da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
20821da177e4SLinus Torvalds 	.accept		= sock_no_accept,
20831da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
20841da177e4SLinus Torvalds };
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds static struct proto hci_sk_proto = {
20871da177e4SLinus Torvalds 	.name		= "HCI",
20881da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
20891da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
20901da177e4SLinus Torvalds };
20911da177e4SLinus Torvalds 
20923f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
20933f378b68SEric Paris 			   int kern)
20941da177e4SLinus Torvalds {
20951da177e4SLinus Torvalds 	struct sock *sk;
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
21001da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
21031da177e4SLinus Torvalds 
210411aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
21051da177e4SLinus Torvalds 	if (!sk)
21061da177e4SLinus Torvalds 		return -ENOMEM;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	sock_init_data(sock, sk);
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
21151da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
2116*709fca50SNguyen Dinh Phi 	sk->sk_destruct = hci_sock_destruct;
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
21191da177e4SLinus Torvalds 	return 0;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
2122ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
21231da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
21241da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
21251da177e4SLinus Torvalds 	.create	= hci_sock_create,
21261da177e4SLinus Torvalds };
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds int __init hci_sock_init(void)
21291da177e4SLinus Torvalds {
21301da177e4SLinus Torvalds 	int err;
21311da177e4SLinus Torvalds 
2132b0a8e282SMarcel Holtmann 	BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2133b0a8e282SMarcel Holtmann 
21341da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
21351da177e4SLinus Torvalds 	if (err < 0)
21361da177e4SLinus Torvalds 		return err;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
2139f7c86637SMasatake YAMATO 	if (err < 0) {
2140f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
21411da177e4SLinus Torvalds 		goto error;
2142f7c86637SMasatake YAMATO 	}
2143f7c86637SMasatake YAMATO 
2144b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
2145f7c86637SMasatake YAMATO 	if (err < 0) {
2146f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
2147f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
2148f7c86637SMasatake YAMATO 		goto error;
2149f7c86637SMasatake YAMATO 	}
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	return 0;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds error:
21561da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
21571da177e4SLinus Torvalds 	return err;
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
2160b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
21611da177e4SLinus Torvalds {
2162f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
21635e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
21641da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
21651da177e4SLinus Torvalds }
2166