xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision 000c2fa2c144c499c881a101819cf1936a1f7cf2)
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) {
8720acef50bSAbhishek Pandit-Subedi 		if (hci_pi(sk)->channel == HCI_CHANNEL_USER &&
8730acef50bSAbhishek Pandit-Subedi 		    !hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
8749332ef9dSMasahiro Yamada 			/* When releasing a user channel exclusive access,
8756b3cc1dbSSimon Fels 			 * call hci_dev_do_close directly instead of calling
8766b3cc1dbSSimon Fels 			 * hci_dev_close to ensure the exclusive access will
8776b3cc1dbSSimon Fels 			 * be released and the controller brought back down.
8786b3cc1dbSSimon Fels 			 *
8796b3cc1dbSSimon Fels 			 * The checking of HCI_AUTO_OFF is not needed in this
8806b3cc1dbSSimon Fels 			 * case since it will have been cleared already when
8816b3cc1dbSSimon Fels 			 * opening the user channel.
8820acef50bSAbhishek Pandit-Subedi 			 *
8830acef50bSAbhishek Pandit-Subedi 			 * Make sure to also check that we haven't already
8840acef50bSAbhishek Pandit-Subedi 			 * unregistered since all the cleanup will have already
8850acef50bSAbhishek Pandit-Subedi 			 * been complete and hdev will get released when we put
8860acef50bSAbhishek Pandit-Subedi 			 * below.
8876b3cc1dbSSimon Fels 			 */
8886b3cc1dbSSimon Fels 			hci_dev_do_close(hdev);
8899380f9eaSLoic Poulain 			hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
8909380f9eaSLoic Poulain 			mgmt_index_added(hdev);
89123500189SMarcel Holtmann 		}
89223500189SMarcel Holtmann 
8931da177e4SLinus Torvalds 		atomic_dec(&hdev->promisc);
8941da177e4SLinus Torvalds 		hci_dev_put(hdev);
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	sock_orphan(sk);
89811eb85ecSDan Carpenter 	release_sock(sk);
8991da177e4SLinus Torvalds 	sock_put(sk);
9001da177e4SLinus Torvalds 	return 0;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9033d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg)
904f0358568SJohan Hedberg {
905f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9065e762444SAntti Julku 	int err;
907f0358568SJohan Hedberg 
908f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
909f0358568SJohan Hedberg 		return -EFAULT;
910f0358568SJohan Hedberg 
91109fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9125e762444SAntti Julku 
9133d4f9c00SArchie Pusaka 	err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9145e762444SAntti Julku 
91509fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9165e762444SAntti Julku 
9175e762444SAntti Julku 	return err;
918f0358568SJohan Hedberg }
919f0358568SJohan Hedberg 
9203d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg)
921f0358568SJohan Hedberg {
922f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9235e762444SAntti Julku 	int err;
924f0358568SJohan Hedberg 
925f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
926f0358568SJohan Hedberg 		return -EFAULT;
927f0358568SJohan Hedberg 
92809fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9295e762444SAntti Julku 
9303d4f9c00SArchie Pusaka 	err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9315e762444SAntti Julku 
93209fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9335e762444SAntti Julku 
9345e762444SAntti Julku 	return err;
935f0358568SJohan Hedberg }
936f0358568SJohan Hedberg 
9371da177e4SLinus Torvalds /* Ioctls that require bound socket */
9386039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
9396039aa73SGustavo Padovan 				unsigned long arg)
9401da177e4SLinus Torvalds {
941e0448092STetsuo Handa 	struct hci_dev *hdev = hci_hdev_from_sock(sk);
9421da177e4SLinus Torvalds 
943e0448092STetsuo Handa 	if (IS_ERR(hdev))
944e0448092STetsuo Handa 		return PTR_ERR(hdev);
9451da177e4SLinus Torvalds 
946d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
9470736cfa8SMarcel Holtmann 		return -EBUSY;
9480736cfa8SMarcel Holtmann 
949d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
950fee746b0SMarcel Holtmann 		return -EOPNOTSUPP;
951fee746b0SMarcel Holtmann 
952ca8bee5dSMarcel Holtmann 	if (hdev->dev_type != HCI_PRIMARY)
9535b69bef5SMarcel Holtmann 		return -EOPNOTSUPP;
9545b69bef5SMarcel Holtmann 
9551da177e4SLinus Torvalds 	switch (cmd) {
9561da177e4SLinus Torvalds 	case HCISETRAW:
9571da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
958bf5b30b8SZhao Hongjiang 			return -EPERM;
959db596681SMarcel Holtmann 		return -EOPNOTSUPP;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	case HCIGETCONNINFO:
9621da177e4SLinus Torvalds 		return hci_get_conn_info(hdev, (void __user *)arg);
9631da177e4SLinus Torvalds 
96440be492fSMarcel Holtmann 	case HCIGETAUTHINFO:
96540be492fSMarcel Holtmann 		return hci_get_auth_info(hdev, (void __user *)arg);
96640be492fSMarcel Holtmann 
967f0358568SJohan Hedberg 	case HCIBLOCKADDR:
968f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
969bf5b30b8SZhao Hongjiang 			return -EPERM;
9703d4f9c00SArchie Pusaka 		return hci_sock_reject_list_add(hdev, (void __user *)arg);
971f0358568SJohan Hedberg 
972f0358568SJohan Hedberg 	case HCIUNBLOCKADDR:
973f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
974bf5b30b8SZhao Hongjiang 			return -EPERM;
9753d4f9c00SArchie Pusaka 		return hci_sock_reject_list_del(hdev, (void __user *)arg);
9760736cfa8SMarcel Holtmann 	}
977f0358568SJohan Hedberg 
978324d36edSMarcel Holtmann 	return -ENOIOCTLCMD;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9818fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
9828fc9ced3SGustavo Padovan 			  unsigned long arg)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
9850736cfa8SMarcel Holtmann 	struct sock *sk = sock->sk;
9861da177e4SLinus Torvalds 	int err;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	BT_DBG("cmd %x arg %lx", cmd, arg);
9891da177e4SLinus Torvalds 
990*000c2fa2SRuihan Li 	/* Make sure the cmd is valid before doing anything */
991*000c2fa2SRuihan Li 	switch (cmd) {
992*000c2fa2SRuihan Li 	case HCIGETDEVLIST:
993*000c2fa2SRuihan Li 	case HCIGETDEVINFO:
994*000c2fa2SRuihan Li 	case HCIGETCONNLIST:
995*000c2fa2SRuihan Li 	case HCIDEVUP:
996*000c2fa2SRuihan Li 	case HCIDEVDOWN:
997*000c2fa2SRuihan Li 	case HCIDEVRESET:
998*000c2fa2SRuihan Li 	case HCIDEVRESTAT:
999*000c2fa2SRuihan Li 	case HCISETSCAN:
1000*000c2fa2SRuihan Li 	case HCISETAUTH:
1001*000c2fa2SRuihan Li 	case HCISETENCRYPT:
1002*000c2fa2SRuihan Li 	case HCISETPTYPE:
1003*000c2fa2SRuihan Li 	case HCISETLINKPOL:
1004*000c2fa2SRuihan Li 	case HCISETLINKMODE:
1005*000c2fa2SRuihan Li 	case HCISETACLMTU:
1006*000c2fa2SRuihan Li 	case HCISETSCOMTU:
1007*000c2fa2SRuihan Li 	case HCIINQUIRY:
1008*000c2fa2SRuihan Li 	case HCISETRAW:
1009*000c2fa2SRuihan Li 	case HCIGETCONNINFO:
1010*000c2fa2SRuihan Li 	case HCIGETAUTHINFO:
1011*000c2fa2SRuihan Li 	case HCIBLOCKADDR:
1012*000c2fa2SRuihan Li 	case HCIUNBLOCKADDR:
1013*000c2fa2SRuihan Li 		break;
1014*000c2fa2SRuihan Li 	default:
1015*000c2fa2SRuihan Li 		return -ENOIOCTLCMD;
1016*000c2fa2SRuihan Li 	}
1017*000c2fa2SRuihan Li 
1018c1c4f956SMarcel Holtmann 	lock_sock(sk);
1019c1c4f956SMarcel Holtmann 
1020c1c4f956SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1021c1c4f956SMarcel Holtmann 		err = -EBADFD;
1022c1c4f956SMarcel Holtmann 		goto done;
1023c1c4f956SMarcel Holtmann 	}
1024c1c4f956SMarcel Holtmann 
1025f81f5b2dSMarcel Holtmann 	/* When calling an ioctl on an unbound raw socket, then ensure
1026f81f5b2dSMarcel Holtmann 	 * that the monitor gets informed. Ensure that the resulting event
1027f81f5b2dSMarcel Holtmann 	 * is only send once by checking if the cookie exists or not. The
1028f81f5b2dSMarcel Holtmann 	 * socket cookie will be only ever generated once for the lifetime
1029f81f5b2dSMarcel Holtmann 	 * of a given socket.
1030f81f5b2dSMarcel Holtmann 	 */
1031f81f5b2dSMarcel Holtmann 	if (hci_sock_gen_cookie(sk)) {
1032f81f5b2dSMarcel Holtmann 		struct sk_buff *skb;
1033f81f5b2dSMarcel Holtmann 
1034f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1035f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1036f81f5b2dSMarcel Holtmann 
1037f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1038f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1039f81f5b2dSMarcel Holtmann 		if (skb) {
1040f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1041f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1042f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1043f81f5b2dSMarcel Holtmann 		}
1044f81f5b2dSMarcel Holtmann 	}
1045f81f5b2dSMarcel Holtmann 
1046c1c4f956SMarcel Holtmann 	release_sock(sk);
1047c1c4f956SMarcel Holtmann 
10481da177e4SLinus Torvalds 	switch (cmd) {
10491da177e4SLinus Torvalds 	case HCIGETDEVLIST:
10501da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	case HCIGETDEVINFO:
10531da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	case HCIGETCONNLIST:
10561da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	case HCIDEVUP:
10591da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1060bf5b30b8SZhao Hongjiang 			return -EPERM;
10611da177e4SLinus Torvalds 		return hci_dev_open(arg);
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	case HCIDEVDOWN:
10641da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1065bf5b30b8SZhao Hongjiang 			return -EPERM;
10661da177e4SLinus Torvalds 		return hci_dev_close(arg);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	case HCIDEVRESET:
10691da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1070bf5b30b8SZhao Hongjiang 			return -EPERM;
10711da177e4SLinus Torvalds 		return hci_dev_reset(arg);
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	case HCIDEVRESTAT:
10741da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1075bf5b30b8SZhao Hongjiang 			return -EPERM;
10761da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	case HCISETSCAN:
10791da177e4SLinus Torvalds 	case HCISETAUTH:
10801da177e4SLinus Torvalds 	case HCISETENCRYPT:
10811da177e4SLinus Torvalds 	case HCISETPTYPE:
10821da177e4SLinus Torvalds 	case HCISETLINKPOL:
10831da177e4SLinus Torvalds 	case HCISETLINKMODE:
10841da177e4SLinus Torvalds 	case HCISETACLMTU:
10851da177e4SLinus Torvalds 	case HCISETSCOMTU:
10861da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1087bf5b30b8SZhao Hongjiang 			return -EPERM;
10881da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	case HCIINQUIRY:
10911da177e4SLinus Torvalds 		return hci_inquiry(argp);
1092c1c4f956SMarcel Holtmann 	}
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	lock_sock(sk);
1095c1c4f956SMarcel Holtmann 
10961da177e4SLinus Torvalds 	err = hci_sock_bound_ioctl(sk, cmd, arg);
1097c1c4f956SMarcel Holtmann 
1098c1c4f956SMarcel Holtmann done:
10991da177e4SLinus Torvalds 	release_sock(sk);
11001da177e4SLinus Torvalds 	return err;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11037a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
11047a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd,
11057a6038b3SArnd Bergmann 				 unsigned long arg)
11067a6038b3SArnd Bergmann {
11077a6038b3SArnd Bergmann 	switch (cmd) {
11087a6038b3SArnd Bergmann 	case HCIDEVUP:
11097a6038b3SArnd Bergmann 	case HCIDEVDOWN:
11107a6038b3SArnd Bergmann 	case HCIDEVRESET:
11117a6038b3SArnd Bergmann 	case HCIDEVRESTAT:
11127a6038b3SArnd Bergmann 		return hci_sock_ioctl(sock, cmd, arg);
11137a6038b3SArnd Bergmann 	}
11147a6038b3SArnd Bergmann 
11157a6038b3SArnd Bergmann 	return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
11167a6038b3SArnd Bergmann }
11177a6038b3SArnd Bergmann #endif
11187a6038b3SArnd Bergmann 
11198fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
11208fc9ced3SGustavo Padovan 			 int addr_len)
11211da177e4SLinus Torvalds {
11220381101fSJohan Hedberg 	struct sockaddr_hci haddr;
11231da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
11241da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
1125f4cdbb3fSMarcel Holtmann 	struct sk_buff *skb;
11260381101fSJohan Hedberg 	int len, err = 0;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
11291da177e4SLinus Torvalds 
11300381101fSJohan Hedberg 	if (!addr)
11310381101fSJohan Hedberg 		return -EINVAL;
11320381101fSJohan Hedberg 
11330381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
11340381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
11350381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
11360381101fSJohan Hedberg 
11370381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
11380381101fSJohan Hedberg 		return -EINVAL;
11390381101fSJohan Hedberg 
11401da177e4SLinus Torvalds 	lock_sock(sk);
11411da177e4SLinus Torvalds 
1142e0448092STetsuo Handa 	/* Allow detaching from dead device and attaching to alive device, if
1143e0448092STetsuo Handa 	 * the caller wants to re-bind (instead of close) this socket in
1144e0448092STetsuo Handa 	 * response to hci_sock_dev_event(HCI_DEV_UNREG) notification.
1145e0448092STetsuo Handa 	 */
1146e0448092STetsuo Handa 	hdev = hci_pi(sk)->hdev;
1147e0448092STetsuo Handa 	if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1148e0448092STetsuo Handa 		hci_pi(sk)->hdev = NULL;
1149e0448092STetsuo Handa 		sk->sk_state = BT_OPEN;
1150e0448092STetsuo Handa 		hci_dev_put(hdev);
1151e0448092STetsuo Handa 	}
1152e0448092STetsuo Handa 	hdev = NULL;
1153e0448092STetsuo Handa 
11547cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
11557cc2ade2SMarcel Holtmann 		err = -EALREADY;
11567cc2ade2SMarcel Holtmann 		goto done;
11577cc2ade2SMarcel Holtmann 	}
11587cc2ade2SMarcel Holtmann 
11597cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
11607cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
11617cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
11621da177e4SLinus Torvalds 			err = -EALREADY;
11631da177e4SLinus Torvalds 			goto done;
11641da177e4SLinus Torvalds 		}
11651da177e4SLinus Torvalds 
11660381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
11670381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
116870f23020SAndrei Emeltchenko 			if (!hdev) {
11691da177e4SLinus Torvalds 				err = -ENODEV;
11701da177e4SLinus Torvalds 				goto done;
11711da177e4SLinus Torvalds 			}
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
11741da177e4SLinus Torvalds 		}
11751da177e4SLinus Torvalds 
11765a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1177f81f5b2dSMarcel Holtmann 
1178f4cdbb3fSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1179f4cdbb3fSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1180f4cdbb3fSMarcel Holtmann 			 * then there has been already an ioctl issued against
118191641b79SZheng Yongjun 			 * an unbound socket and with that triggered an open
1182f4cdbb3fSMarcel Holtmann 			 * notification. Send a close notification first to
1183f4cdbb3fSMarcel Holtmann 			 * allow the state transition to bounded.
1184f81f5b2dSMarcel Holtmann 			 */
1185f4cdbb3fSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1186f4cdbb3fSMarcel Holtmann 			if (skb) {
1187f4cdbb3fSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1188f4cdbb3fSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1189f4cdbb3fSMarcel Holtmann 				kfree_skb(skb);
1190f4cdbb3fSMarcel Holtmann 			}
1191f4cdbb3fSMarcel Holtmann 		}
1192f81f5b2dSMarcel Holtmann 
1193f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1194f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1195f81f5b2dSMarcel Holtmann 
1196f4cdbb3fSMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
1197f4cdbb3fSMarcel Holtmann 
1198f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1199f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1200f81f5b2dSMarcel Holtmann 		if (skb) {
1201f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1202f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1203f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1204f81f5b2dSMarcel Holtmann 		}
12057cc2ade2SMarcel Holtmann 		break;
12067cc2ade2SMarcel Holtmann 
120723500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
120823500189SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
120923500189SMarcel Holtmann 			err = -EALREADY;
121023500189SMarcel Holtmann 			goto done;
121123500189SMarcel Holtmann 		}
121223500189SMarcel Holtmann 
121323500189SMarcel Holtmann 		if (haddr.hci_dev == HCI_DEV_NONE) {
121423500189SMarcel Holtmann 			err = -EINVAL;
121523500189SMarcel Holtmann 			goto done;
121623500189SMarcel Holtmann 		}
121723500189SMarcel Holtmann 
121810a8b86fSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
121923500189SMarcel Holtmann 			err = -EPERM;
122023500189SMarcel Holtmann 			goto done;
122123500189SMarcel Holtmann 		}
122223500189SMarcel Holtmann 
122323500189SMarcel Holtmann 		hdev = hci_dev_get(haddr.hci_dev);
122423500189SMarcel Holtmann 		if (!hdev) {
122523500189SMarcel Holtmann 			err = -ENODEV;
122623500189SMarcel Holtmann 			goto done;
122723500189SMarcel Holtmann 		}
122823500189SMarcel Holtmann 
1229781f899fSMarcel Holtmann 		if (test_bit(HCI_INIT, &hdev->flags) ||
1230d7a5a11dSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_SETUP) ||
1231781f899fSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1232781f899fSMarcel Holtmann 		    (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1233781f899fSMarcel Holtmann 		     test_bit(HCI_UP, &hdev->flags))) {
123423500189SMarcel Holtmann 			err = -EBUSY;
123523500189SMarcel Holtmann 			hci_dev_put(hdev);
123623500189SMarcel Holtmann 			goto done;
123723500189SMarcel Holtmann 		}
123823500189SMarcel Holtmann 
1239238be788SMarcel Holtmann 		if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
124023500189SMarcel Holtmann 			err = -EUSERS;
124123500189SMarcel Holtmann 			hci_dev_put(hdev);
124223500189SMarcel Holtmann 			goto done;
124323500189SMarcel Holtmann 		}
124423500189SMarcel Holtmann 
124523500189SMarcel Holtmann 		mgmt_index_removed(hdev);
124623500189SMarcel Holtmann 
124723500189SMarcel Holtmann 		err = hci_dev_open(hdev->id);
124823500189SMarcel Holtmann 		if (err) {
1249781f899fSMarcel Holtmann 			if (err == -EALREADY) {
1250781f899fSMarcel Holtmann 				/* In case the transport is already up and
1251781f899fSMarcel Holtmann 				 * running, clear the error here.
1252781f899fSMarcel Holtmann 				 *
12539332ef9dSMasahiro Yamada 				 * This can happen when opening a user
1254781f899fSMarcel Holtmann 				 * channel and HCI_AUTO_OFF grace period
1255781f899fSMarcel Holtmann 				 * is still active.
1256781f899fSMarcel Holtmann 				 */
1257781f899fSMarcel Holtmann 				err = 0;
1258781f899fSMarcel Holtmann 			} else {
1259a358dc11SMarcel Holtmann 				hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1260c6521401SMarcel Holtmann 				mgmt_index_added(hdev);
126123500189SMarcel Holtmann 				hci_dev_put(hdev);
126223500189SMarcel Holtmann 				goto done;
126323500189SMarcel Holtmann 			}
1264781f899fSMarcel Holtmann 		}
126523500189SMarcel Holtmann 
12665a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1267aa1638ddSMarcel Holtmann 
1268aa1638ddSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1269aa1638ddSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1270aa1638ddSMarcel Holtmann 			 * this socket will transition from a raw socket into
12719332ef9dSMasahiro Yamada 			 * a user channel socket. For a clean transition, send
1272aa1638ddSMarcel Holtmann 			 * the close notification first.
1273aa1638ddSMarcel Holtmann 			 */
1274aa1638ddSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1275aa1638ddSMarcel Holtmann 			if (skb) {
1276aa1638ddSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1277aa1638ddSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1278aa1638ddSMarcel Holtmann 				kfree_skb(skb);
1279aa1638ddSMarcel Holtmann 			}
1280aa1638ddSMarcel Holtmann 		}
1281aa1638ddSMarcel Holtmann 
1282aa1638ddSMarcel Holtmann 		/* The user channel is restricted to CAP_NET_ADMIN
1283aa1638ddSMarcel Holtmann 		 * capabilities and with that implicitly trusted.
1284aa1638ddSMarcel Holtmann 		 */
1285aa1638ddSMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1286aa1638ddSMarcel Holtmann 
128723500189SMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
12885a6d2cf5SMarcel Holtmann 
1289aa1638ddSMarcel Holtmann 		/* Send event to monitor */
1290aa1638ddSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1291aa1638ddSMarcel Holtmann 		if (skb) {
1292aa1638ddSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1293aa1638ddSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1294aa1638ddSMarcel Holtmann 			kfree_skb(skb);
1295aa1638ddSMarcel Holtmann 		}
1296aa1638ddSMarcel Holtmann 
12975a6d2cf5SMarcel Holtmann 		atomic_inc(&hdev->promisc);
129823500189SMarcel Holtmann 		break;
129923500189SMarcel Holtmann 
1300cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1301cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1302cd82e61cSMarcel Holtmann 			err = -EINVAL;
1303cd82e61cSMarcel Holtmann 			goto done;
1304cd82e61cSMarcel Holtmann 		}
1305cd82e61cSMarcel Holtmann 
1306cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
1307cd82e61cSMarcel Holtmann 			err = -EPERM;
1308cd82e61cSMarcel Holtmann 			goto done;
1309cd82e61cSMarcel Holtmann 		}
1310cd82e61cSMarcel Holtmann 
13115a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
13125a6d2cf5SMarcel Holtmann 
131350ebc055SMarcel Holtmann 		/* The monitor interface is restricted to CAP_NET_RAW
131450ebc055SMarcel Holtmann 		 * capabilities and with that implicitly trusted.
131550ebc055SMarcel Holtmann 		 */
131650ebc055SMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
131750ebc055SMarcel Holtmann 
1318787b306cSJohannes Berg 		send_monitor_note(sk, "Linux version %s (%s)",
1319787b306cSJohannes Berg 				  init_utsname()->release,
1320787b306cSJohannes Berg 				  init_utsname()->machine);
13219e8305b3SMarcel Holtmann 		send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
13229e8305b3SMarcel Holtmann 				  BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
1323cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
1324249fa169SMarcel Holtmann 		send_monitor_control_replay(sk);
1325cd82e61cSMarcel Holtmann 
1326cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
1327cd82e61cSMarcel Holtmann 		break;
1328cd82e61cSMarcel Holtmann 
1329ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
1330ac714949SMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1331ac714949SMarcel Holtmann 			err = -EINVAL;
1332ac714949SMarcel Holtmann 			goto done;
1333ac714949SMarcel Holtmann 		}
1334ac714949SMarcel Holtmann 
1335ac714949SMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
1336ac714949SMarcel Holtmann 			err = -EPERM;
1337ac714949SMarcel Holtmann 			goto done;
1338ac714949SMarcel Holtmann 		}
13395a6d2cf5SMarcel Holtmann 
13405a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1341ac714949SMarcel Holtmann 		break;
1342ac714949SMarcel Holtmann 
13437cc2ade2SMarcel Holtmann 	default:
1344801c1e8dSJohan Hedberg 		if (!hci_mgmt_chan_find(haddr.hci_channel)) {
13457cc2ade2SMarcel Holtmann 			err = -EINVAL;
13467cc2ade2SMarcel Holtmann 			goto done;
13477cc2ade2SMarcel Holtmann 		}
13487cc2ade2SMarcel Holtmann 
1349801c1e8dSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
1350801c1e8dSJohan Hedberg 			err = -EINVAL;
1351801c1e8dSJohan Hedberg 			goto done;
1352801c1e8dSJohan Hedberg 		}
1353801c1e8dSJohan Hedberg 
13541195fbb8SMarcel Holtmann 		/* Users with CAP_NET_ADMIN capabilities are allowed
13551195fbb8SMarcel Holtmann 		 * access to all management commands and events. For
13561195fbb8SMarcel Holtmann 		 * untrusted users the interface is restricted and
13571195fbb8SMarcel Holtmann 		 * also only untrusted events are sent.
135850ebc055SMarcel Holtmann 		 */
13591195fbb8SMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
136050ebc055SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
136150ebc055SMarcel Holtmann 
13625a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
13635a6d2cf5SMarcel Holtmann 
1364f9207338SMarcel Holtmann 		/* At the moment the index and unconfigured index events
1365f9207338SMarcel Holtmann 		 * are enabled unconditionally. Setting them on each
1366f9207338SMarcel Holtmann 		 * socket when binding keeps this functionality. They
1367f9207338SMarcel Holtmann 		 * however might be cleared later and then sending of these
1368f9207338SMarcel Holtmann 		 * events will be disabled, but that is then intentional.
1369f6b7712eSMarcel Holtmann 		 *
1370f6b7712eSMarcel Holtmann 		 * This also enables generic events that are safe to be
1371f6b7712eSMarcel Holtmann 		 * received by untrusted users. Example for such events
1372f6b7712eSMarcel Holtmann 		 * are changes to settings, class of device, name etc.
1373f9207338SMarcel Holtmann 		 */
13745a6d2cf5SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
1375f4cdbb3fSMarcel Holtmann 			if (!hci_sock_gen_cookie(sk)) {
1376f4cdbb3fSMarcel Holtmann 				/* In the case when a cookie has already been
137791641b79SZheng Yongjun 				 * assigned, this socket will transition from
1378f4cdbb3fSMarcel Holtmann 				 * a raw socket into a control socket. To
137991641b79SZheng Yongjun 				 * allow for a clean transition, send the
1380f4cdbb3fSMarcel Holtmann 				 * close notification first.
1381f4cdbb3fSMarcel Holtmann 				 */
1382f4cdbb3fSMarcel Holtmann 				skb = create_monitor_ctrl_close(sk);
1383f4cdbb3fSMarcel Holtmann 				if (skb) {
1384f4cdbb3fSMarcel Holtmann 					hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1385f4cdbb3fSMarcel Holtmann 							    HCI_SOCK_TRUSTED, NULL);
1386f4cdbb3fSMarcel Holtmann 					kfree_skb(skb);
1387f4cdbb3fSMarcel Holtmann 				}
1388f4cdbb3fSMarcel Holtmann 			}
138970ecce91SMarcel Holtmann 
1390249fa169SMarcel Holtmann 			/* Send event to monitor */
1391249fa169SMarcel Holtmann 			skb = create_monitor_ctrl_open(sk);
1392249fa169SMarcel Holtmann 			if (skb) {
1393249fa169SMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1394249fa169SMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1395249fa169SMarcel Holtmann 				kfree_skb(skb);
1396249fa169SMarcel Holtmann 			}
1397249fa169SMarcel Holtmann 
1398f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1399f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
14005504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
14015504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
14025504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
14035504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1404f9207338SMarcel Holtmann 		}
1405801c1e8dSJohan Hedberg 		break;
1406801c1e8dSJohan Hedberg 	}
1407801c1e8dSJohan Hedberg 
140809572fcaSLuiz Augusto von Dentz 	/* Default MTU to HCI_MAX_FRAME_SIZE if not set */
140909572fcaSLuiz Augusto von Dentz 	if (!hci_pi(sk)->mtu)
141009572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE;
141109572fcaSLuiz Augusto von Dentz 
14121da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds done:
14151da177e4SLinus Torvalds 	release_sock(sk);
14161da177e4SLinus Torvalds 	return err;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14198fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
14209b2c45d4SDenys Vlasenko 			    int peer)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
14231da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
14249d4b68b2SMarcel Holtmann 	struct hci_dev *hdev;
14259d4b68b2SMarcel Holtmann 	int err = 0;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
14281da177e4SLinus Torvalds 
142906f43cbcSMarcel Holtmann 	if (peer)
143006f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
143106f43cbcSMarcel Holtmann 
14321da177e4SLinus Torvalds 	lock_sock(sk);
14331da177e4SLinus Torvalds 
1434e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1435e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1436e0448092STetsuo Handa 		err = PTR_ERR(hdev);
14379d4b68b2SMarcel Holtmann 		goto done;
14389d4b68b2SMarcel Holtmann 	}
14399d4b68b2SMarcel Holtmann 
14401da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
14417b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
14429d4b68b2SMarcel Holtmann 	haddr->hci_channel= hci_pi(sk)->channel;
14439b2c45d4SDenys Vlasenko 	err = sizeof(*haddr);
14441da177e4SLinus Torvalds 
14459d4b68b2SMarcel Holtmann done:
14461da177e4SLinus Torvalds 	release_sock(sk);
14479d4b68b2SMarcel Holtmann 	return err;
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds 
14506039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
14516039aa73SGustavo Padovan 			  struct sk_buff *skb)
14521da177e4SLinus Torvalds {
145332929e1fSAlain Michaud 	__u8 mask = hci_pi(sk)->cmsg_mask;
14541da177e4SLinus Torvalds 
14550d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
14560d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
14578fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
14588fc9ced3SGustavo Padovan 			 &incoming);
14590d48d939SMarcel Holtmann 	}
14601da177e4SLinus Torvalds 
1461a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
1462f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
146313c6ee2aSDeepa Dinamani 		struct old_timeval32 ctv;
1464f6e623a6SJohann Felix Soden #endif
146513c6ee2aSDeepa Dinamani 		struct __kernel_old_timeval tv;
1466767c5eb5SMarcel Holtmann 		void *data;
1467767c5eb5SMarcel Holtmann 		int len;
1468a61bbcf2SPatrick McHardy 
1469a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
1470767c5eb5SMarcel Holtmann 
14711da97f83SDavid S. Miller 		data = &tv;
14721da97f83SDavid S. Miller 		len = sizeof(tv);
14731da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
1474da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
1475da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
1476767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
1477767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
1478767c5eb5SMarcel Holtmann 			data = &ctv;
1479767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
1480767c5eb5SMarcel Holtmann 		}
14811da97f83SDavid S. Miller #endif
1482767c5eb5SMarcel Holtmann 
1483767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
1484a61bbcf2SPatrick McHardy 	}
14851da177e4SLinus Torvalds }
14861da177e4SLinus Torvalds 
14878528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
14888528d3f7SMarcel Holtmann 			    size_t len, int flags)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
14911da177e4SLinus Torvalds 	struct sk_buff *skb;
14921da177e4SLinus Torvalds 	int copied, err;
149383871f8cSDenis Kenzior 	unsigned int skblen;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
14961da177e4SLinus Torvalds 
1497d94a6104SMarcel Holtmann 	if (flags & MSG_OOB)
14981da177e4SLinus Torvalds 		return -EOPNOTSUPP;
14991da177e4SLinus Torvalds 
1500ac714949SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1501ac714949SMarcel Holtmann 		return -EOPNOTSUPP;
1502ac714949SMarcel Holtmann 
15031da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
15041da177e4SLinus Torvalds 		return 0;
15051da177e4SLinus Torvalds 
1506f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
150770f23020SAndrei Emeltchenko 	if (!skb)
15081da177e4SLinus Torvalds 		return err;
15091da177e4SLinus Torvalds 
151083871f8cSDenis Kenzior 	skblen = skb->len;
15111da177e4SLinus Torvalds 	copied = skb->len;
15121da177e4SLinus Torvalds 	if (len < copied) {
15131da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
15141da177e4SLinus Torvalds 		copied = len;
15151da177e4SLinus Torvalds 	}
15161da177e4SLinus Torvalds 
1517badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
151851f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
15191da177e4SLinus Torvalds 
15203a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
15213a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
15221da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
15233a208627SMarcel Holtmann 		break;
152423500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
1525cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1526cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
1527cd82e61cSMarcel Holtmann 		break;
1528801c1e8dSJohan Hedberg 	default:
1529801c1e8dSJohan Hedberg 		if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1530801c1e8dSJohan Hedberg 			sock_recv_timestamp(msg, sk, skb);
1531801c1e8dSJohan Hedberg 		break;
15323a208627SMarcel Holtmann 	}
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
15351da177e4SLinus Torvalds 
15364f34228bSLuiz Augusto von Dentz 	if (flags & MSG_TRUNC)
153783871f8cSDenis Kenzior 		copied = skblen;
153883871f8cSDenis Kenzior 
15391da177e4SLinus Torvalds 	return err ? : copied;
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
154264ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
154364ba2eb3SLuiz Augusto von Dentz 			struct sk_buff *skb)
1544fa4335d7SJohan Hedberg {
1545fa4335d7SJohan Hedberg 	u8 *cp;
1546fa4335d7SJohan Hedberg 	struct mgmt_hdr *hdr;
1547fa4335d7SJohan Hedberg 	u16 opcode, index, len;
1548fa4335d7SJohan Hedberg 	struct hci_dev *hdev = NULL;
1549fa4335d7SJohan Hedberg 	const struct hci_mgmt_handler *handler;
1550fa4335d7SJohan Hedberg 	bool var_len, no_hdev;
1551fa4335d7SJohan Hedberg 	int err;
1552fa4335d7SJohan Hedberg 
155364ba2eb3SLuiz Augusto von Dentz 	BT_DBG("got %d bytes", skb->len);
1554fa4335d7SJohan Hedberg 
155564ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr))
1556fa4335d7SJohan Hedberg 		return -EINVAL;
1557fa4335d7SJohan Hedberg 
155864ba2eb3SLuiz Augusto von Dentz 	hdr = (void *)skb->data;
1559fa4335d7SJohan Hedberg 	opcode = __le16_to_cpu(hdr->opcode);
1560fa4335d7SJohan Hedberg 	index = __le16_to_cpu(hdr->index);
1561fa4335d7SJohan Hedberg 	len = __le16_to_cpu(hdr->len);
1562fa4335d7SJohan Hedberg 
156364ba2eb3SLuiz Augusto von Dentz 	if (len != skb->len - sizeof(*hdr)) {
1564fa4335d7SJohan Hedberg 		err = -EINVAL;
1565fa4335d7SJohan Hedberg 		goto done;
1566fa4335d7SJohan Hedberg 	}
1567fa4335d7SJohan Hedberg 
156838ceaa00SMarcel Holtmann 	if (chan->channel == HCI_CHANNEL_CONTROL) {
156964ba2eb3SLuiz Augusto von Dentz 		struct sk_buff *cmd;
157038ceaa00SMarcel Holtmann 
157138ceaa00SMarcel Holtmann 		/* Send event to monitor */
157264ba2eb3SLuiz Augusto von Dentz 		cmd = create_monitor_ctrl_command(sk, index, opcode, len,
157364ba2eb3SLuiz Augusto von Dentz 						  skb->data + sizeof(*hdr));
157464ba2eb3SLuiz Augusto von Dentz 		if (cmd) {
157564ba2eb3SLuiz Augusto von Dentz 			hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd,
157638ceaa00SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
157764ba2eb3SLuiz Augusto von Dentz 			kfree_skb(cmd);
157838ceaa00SMarcel Holtmann 		}
157938ceaa00SMarcel Holtmann 	}
158038ceaa00SMarcel Holtmann 
1581fa4335d7SJohan Hedberg 	if (opcode >= chan->handler_count ||
1582fa4335d7SJohan Hedberg 	    chan->handlers[opcode].func == NULL) {
1583fa4335d7SJohan Hedberg 		BT_DBG("Unknown op %u", opcode);
1584fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1585fa4335d7SJohan Hedberg 				      MGMT_STATUS_UNKNOWN_COMMAND);
1586fa4335d7SJohan Hedberg 		goto done;
1587fa4335d7SJohan Hedberg 	}
1588fa4335d7SJohan Hedberg 
1589fa4335d7SJohan Hedberg 	handler = &chan->handlers[opcode];
1590fa4335d7SJohan Hedberg 
1591fa4335d7SJohan Hedberg 	if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1592fa4335d7SJohan Hedberg 	    !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1593fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1594fa4335d7SJohan Hedberg 				      MGMT_STATUS_PERMISSION_DENIED);
1595fa4335d7SJohan Hedberg 		goto done;
1596fa4335d7SJohan Hedberg 	}
1597fa4335d7SJohan Hedberg 
1598fa4335d7SJohan Hedberg 	if (index != MGMT_INDEX_NONE) {
1599fa4335d7SJohan Hedberg 		hdev = hci_dev_get(index);
1600fa4335d7SJohan Hedberg 		if (!hdev) {
1601fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1602fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1603fa4335d7SJohan Hedberg 			goto done;
1604fa4335d7SJohan Hedberg 		}
1605fa4335d7SJohan Hedberg 
1606fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1607fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1608fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1609fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1610fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1611fa4335d7SJohan Hedberg 			goto done;
1612fa4335d7SJohan Hedberg 		}
1613fa4335d7SJohan Hedberg 
1614fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1615fa4335d7SJohan Hedberg 		    !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1616fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1617fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1618fa4335d7SJohan Hedberg 			goto done;
1619fa4335d7SJohan Hedberg 		}
1620fa4335d7SJohan Hedberg 	}
1621fa4335d7SJohan Hedberg 
1622d5cc6626SMarcel Holtmann 	if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) {
1623fa4335d7SJohan Hedberg 		no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1624fa4335d7SJohan Hedberg 		if (no_hdev != !hdev) {
1625fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1626fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1627fa4335d7SJohan Hedberg 			goto done;
1628fa4335d7SJohan Hedberg 		}
1629d5cc6626SMarcel Holtmann 	}
1630fa4335d7SJohan Hedberg 
1631fa4335d7SJohan Hedberg 	var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1632fa4335d7SJohan Hedberg 	if ((var_len && len < handler->data_len) ||
1633fa4335d7SJohan Hedberg 	    (!var_len && len != handler->data_len)) {
1634fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1635fa4335d7SJohan Hedberg 				      MGMT_STATUS_INVALID_PARAMS);
1636fa4335d7SJohan Hedberg 		goto done;
1637fa4335d7SJohan Hedberg 	}
1638fa4335d7SJohan Hedberg 
1639fa4335d7SJohan Hedberg 	if (hdev && chan->hdev_init)
1640fa4335d7SJohan Hedberg 		chan->hdev_init(sk, hdev);
1641fa4335d7SJohan Hedberg 
164264ba2eb3SLuiz Augusto von Dentz 	cp = skb->data + sizeof(*hdr);
1643fa4335d7SJohan Hedberg 
1644fa4335d7SJohan Hedberg 	err = handler->func(sk, hdev, cp, len);
1645fa4335d7SJohan Hedberg 	if (err < 0)
1646fa4335d7SJohan Hedberg 		goto done;
1647fa4335d7SJohan Hedberg 
164864ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1649fa4335d7SJohan Hedberg 
1650fa4335d7SJohan Hedberg done:
1651fa4335d7SJohan Hedberg 	if (hdev)
1652fa4335d7SJohan Hedberg 		hci_dev_put(hdev);
1653fa4335d7SJohan Hedberg 
1654fa4335d7SJohan Hedberg 	return err;
1655fa4335d7SJohan Hedberg }
1656fa4335d7SJohan Hedberg 
165764ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb,
165864ba2eb3SLuiz Augusto von Dentz 			     unsigned int flags)
1659ac714949SMarcel Holtmann {
1660ac714949SMarcel Holtmann 	struct hci_mon_hdr *hdr;
1661ac714949SMarcel Holtmann 	struct hci_dev *hdev;
1662ac714949SMarcel Holtmann 	u16 index;
1663ac714949SMarcel Holtmann 	int err;
1664ac714949SMarcel Holtmann 
1665ac714949SMarcel Holtmann 	/* The logging frame consists at minimum of the standard header,
1666ac714949SMarcel Holtmann 	 * the priority byte, the ident length byte and at least one string
1667ac714949SMarcel Holtmann 	 * terminator NUL byte. Anything shorter are invalid packets.
1668ac714949SMarcel Holtmann 	 */
166964ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr) + 3)
1670ac714949SMarcel Holtmann 		return -EINVAL;
1671ac714949SMarcel Holtmann 
1672ac714949SMarcel Holtmann 	hdr = (void *)skb->data;
1673ac714949SMarcel Holtmann 
167464ba2eb3SLuiz Augusto von Dentz 	if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr))
167564ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1676ac714949SMarcel Holtmann 
1677ac714949SMarcel Holtmann 	if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1678ac714949SMarcel Holtmann 		__u8 priority = skb->data[sizeof(*hdr)];
1679ac714949SMarcel Holtmann 		__u8 ident_len = skb->data[sizeof(*hdr) + 1];
1680ac714949SMarcel Holtmann 
1681ac714949SMarcel Holtmann 		/* Only the priorities 0-7 are valid and with that any other
1682ac714949SMarcel Holtmann 		 * value results in an invalid packet.
1683ac714949SMarcel Holtmann 		 *
1684ac714949SMarcel Holtmann 		 * The priority byte is followed by an ident length byte and
1685ac714949SMarcel Holtmann 		 * the NUL terminated ident string. Check that the ident
1686ac714949SMarcel Holtmann 		 * length is not overflowing the packet and also that the
1687ac714949SMarcel Holtmann 		 * ident string itself is NUL terminated. In case the ident
1688ac714949SMarcel Holtmann 		 * length is zero, the length value actually doubles as NUL
1689ac714949SMarcel Holtmann 		 * terminator identifier.
1690ac714949SMarcel Holtmann 		 *
1691ac714949SMarcel Holtmann 		 * The message follows the ident string (if present) and
1692ac714949SMarcel Holtmann 		 * must be NUL terminated. Otherwise it is not a valid packet.
1693ac714949SMarcel Holtmann 		 */
169464ba2eb3SLuiz Augusto von Dentz 		if (priority > 7 || skb->data[skb->len - 1] != 0x00 ||
169564ba2eb3SLuiz Augusto von Dentz 		    ident_len > skb->len - sizeof(*hdr) - 3 ||
169664ba2eb3SLuiz Augusto von Dentz 		    skb->data[sizeof(*hdr) + ident_len + 1] != 0x00)
169764ba2eb3SLuiz Augusto von Dentz 			return -EINVAL;
1698ac714949SMarcel Holtmann 	} else {
169964ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1700ac714949SMarcel Holtmann 	}
1701ac714949SMarcel Holtmann 
1702ac714949SMarcel Holtmann 	index = __le16_to_cpu(hdr->index);
1703ac714949SMarcel Holtmann 
1704ac714949SMarcel Holtmann 	if (index != MGMT_INDEX_NONE) {
1705ac714949SMarcel Holtmann 		hdev = hci_dev_get(index);
170664ba2eb3SLuiz Augusto von Dentz 		if (!hdev)
170764ba2eb3SLuiz Augusto von Dentz 			return -ENODEV;
1708ac714949SMarcel Holtmann 	} else {
1709ac714949SMarcel Holtmann 		hdev = NULL;
1710ac714949SMarcel Holtmann 	}
1711ac714949SMarcel Holtmann 
1712ac714949SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1713ac714949SMarcel Holtmann 
1714ac714949SMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
171564ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1716ac714949SMarcel Holtmann 
1717ac714949SMarcel Holtmann 	if (hdev)
1718ac714949SMarcel Holtmann 		hci_dev_put(hdev);
1719ac714949SMarcel Holtmann 
1720ac714949SMarcel Holtmann 	return err;
1721ac714949SMarcel Holtmann }
1722ac714949SMarcel Holtmann 
17231b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
17241b784140SYing Xue 			    size_t len)
17251da177e4SLinus Torvalds {
17261da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1727801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *chan;
17281da177e4SLinus Torvalds 	struct hci_dev *hdev;
17291da177e4SLinus Torvalds 	struct sk_buff *skb;
17301da177e4SLinus Torvalds 	int err;
17310b59e272STetsuo Handa 	const unsigned int flags = msg->msg_flags;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
17341da177e4SLinus Torvalds 
17350b59e272STetsuo Handa 	if (flags & MSG_OOB)
17361da177e4SLinus Torvalds 		return -EOPNOTSUPP;
17371da177e4SLinus Torvalds 
17380b59e272STetsuo Handa 	if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
17391da177e4SLinus Torvalds 		return -EINVAL;
17401da177e4SLinus Torvalds 
174109572fcaSLuiz Augusto von Dentz 	if (len < 4 || len > hci_pi(sk)->mtu)
17421da177e4SLinus Torvalds 		return -EINVAL;
17431da177e4SLinus Torvalds 
174464ba2eb3SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
174564ba2eb3SLuiz Augusto von Dentz 	if (IS_ERR(skb))
174664ba2eb3SLuiz Augusto von Dentz 		return PTR_ERR(skb);
17470b59e272STetsuo Handa 
17481da177e4SLinus Torvalds 	lock_sock(sk);
17491da177e4SLinus Torvalds 
17500381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
17510381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
175223500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
17530381101fSJohan Hedberg 		break;
1754cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1755cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
175664ba2eb3SLuiz Augusto von Dentz 		goto drop;
1757ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
175864ba2eb3SLuiz Augusto von Dentz 		err = hci_logging_frame(sk, skb, flags);
175964ba2eb3SLuiz Augusto von Dentz 		goto drop;
17600381101fSJohan Hedberg 	default:
1761801c1e8dSJohan Hedberg 		mutex_lock(&mgmt_chan_list_lock);
1762801c1e8dSJohan Hedberg 		chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1763801c1e8dSJohan Hedberg 		if (chan)
176464ba2eb3SLuiz Augusto von Dentz 			err = hci_mgmt_cmd(chan, sk, skb);
1765801c1e8dSJohan Hedberg 		else
17660381101fSJohan Hedberg 			err = -EINVAL;
1767801c1e8dSJohan Hedberg 
1768801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
176964ba2eb3SLuiz Augusto von Dentz 		goto drop;
17700381101fSJohan Hedberg 	}
17710381101fSJohan Hedberg 
1772e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1773e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1774e0448092STetsuo Handa 		err = PTR_ERR(hdev);
177564ba2eb3SLuiz Augusto von Dentz 		goto drop;
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 
17787e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
17797e21addcSMarcel Holtmann 		err = -ENETDOWN;
178064ba2eb3SLuiz Augusto von Dentz 		goto drop;
17817e21addcSMarcel Holtmann 	}
17827e21addcSMarcel Holtmann 
17838528d3f7SMarcel Holtmann 	hci_skb_pkt_type(skb) = skb->data[0];
17841da177e4SLinus Torvalds 	skb_pull(skb, 1);
17851da177e4SLinus Torvalds 
17861bc5ad16SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
17871bc5ad16SMarcel Holtmann 		/* No permission check is needed for user channel
17881bc5ad16SMarcel Holtmann 		 * since that gets enforced when binding the socket.
17891bc5ad16SMarcel Holtmann 		 *
17901bc5ad16SMarcel Holtmann 		 * However check that the packet type is valid.
17911bc5ad16SMarcel Holtmann 		 */
1792d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1793d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1794cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1795cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
17961bc5ad16SMarcel Holtmann 			err = -EINVAL;
17971bc5ad16SMarcel Holtmann 			goto drop;
17981bc5ad16SMarcel Holtmann 		}
17991bc5ad16SMarcel Holtmann 
18001bc5ad16SMarcel Holtmann 		skb_queue_tail(&hdev->raw_q, skb);
18011bc5ad16SMarcel Holtmann 		queue_work(hdev->workqueue, &hdev->tx_work);
1802d79f34e3SMarcel Holtmann 	} else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
180383985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
18041da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
18051da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
18083bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
18093bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
18101da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
18111da177e4SLinus Torvalds 			err = -EPERM;
18121da177e4SLinus Torvalds 			goto drop;
18131da177e4SLinus Torvalds 		}
18141da177e4SLinus Torvalds 
18151982162bSMarcel Holtmann 		/* Since the opcode has already been extracted here, store
18161982162bSMarcel Holtmann 		 * a copy of the value for later use by the drivers.
18171982162bSMarcel Holtmann 		 */
18181982162bSMarcel Holtmann 		hci_skb_opcode(skb) = opcode;
18191982162bSMarcel Holtmann 
1820fee746b0SMarcel Holtmann 		if (ogf == 0x3f) {
18211da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
18223eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
18231da177e4SLinus Torvalds 		} else {
182449c922bbSStephen Hemminger 			/* Stand-alone HCI commands must be flagged as
182511714b3dSJohan Hedberg 			 * single-command requests.
182611714b3dSJohan Hedberg 			 */
182744d27137SJohan Hedberg 			bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
182811714b3dSJohan Hedberg 
18291da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
1830c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
18311da177e4SLinus Torvalds 		}
18321da177e4SLinus Torvalds 	} else {
18331da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
18341da177e4SLinus Torvalds 			err = -EPERM;
18351da177e4SLinus Torvalds 			goto drop;
18361da177e4SLinus Torvalds 		}
18371da177e4SLinus Torvalds 
1838d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1839cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1840cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
1841bb77543eSMarcel Holtmann 			err = -EINVAL;
1842bb77543eSMarcel Holtmann 			goto drop;
1843bb77543eSMarcel Holtmann 		}
1844bb77543eSMarcel Holtmann 
18451da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
18463eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
18471da177e4SLinus Torvalds 	}
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	err = len;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds done:
18521da177e4SLinus Torvalds 	release_sock(sk);
18531da177e4SLinus Torvalds 	return err;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds drop:
18561da177e4SLinus Torvalds 	kfree_skb(skb);
18571da177e4SLinus Torvalds 	goto done;
18581da177e4SLinus Torvalds }
18591da177e4SLinus Torvalds 
186009572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname,
1861a7b75c5aSChristoph Hellwig 				   sockptr_t optval, unsigned int len)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
18641da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
18651da177e4SLinus Torvalds 	int err = 0, opt = 0;
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	lock_sock(sk);
18701da177e4SLinus Torvalds 
18712f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1872c2371e80SMarcel Holtmann 		err = -EBADFD;
18732f39cdb7SMarcel Holtmann 		goto done;
18742f39cdb7SMarcel Holtmann 	}
18752f39cdb7SMarcel Holtmann 
18761da177e4SLinus Torvalds 	switch (optname) {
18771da177e4SLinus Torvalds 	case HCI_DATA_DIR:
1878a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
18791da177e4SLinus Torvalds 			err = -EFAULT;
18801da177e4SLinus Torvalds 			break;
18811da177e4SLinus Torvalds 		}
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 		if (opt)
18841da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
18851da177e4SLinus Torvalds 		else
18861da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
18871da177e4SLinus Torvalds 		break;
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
1890a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
18911da177e4SLinus Torvalds 			err = -EFAULT;
18921da177e4SLinus Torvalds 			break;
18931da177e4SLinus Torvalds 		}
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 		if (opt)
18961da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
18971da177e4SLinus Torvalds 		else
18981da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
18991da177e4SLinus Torvalds 		break;
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 	case HCI_FILTER:
19020878b666SMarcel Holtmann 		{
19030878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
19040878b666SMarcel Holtmann 
19050878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
19060878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
19070878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
19080878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
19090878b666SMarcel Holtmann 		}
19100878b666SMarcel Holtmann 
19111da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
1912a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&uf, optval, len)) {
19131da177e4SLinus Torvalds 			err = -EFAULT;
19141da177e4SLinus Torvalds 			break;
19151da177e4SLinus Torvalds 		}
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
19181da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
19191da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
19201da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
19211da177e4SLinus Torvalds 		}
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 		{
19241da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
19271da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
19281da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
19291da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
19301da177e4SLinus Torvalds 		}
19311da177e4SLinus Torvalds 		break;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	default:
19341da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
19351da177e4SLinus Torvalds 		break;
19361da177e4SLinus Torvalds 	}
19371da177e4SLinus Torvalds 
19382f39cdb7SMarcel Holtmann done:
19391da177e4SLinus Torvalds 	release_sock(sk);
19401da177e4SLinus Torvalds 	return err;
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
194309572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
194409572fcaSLuiz Augusto von Dentz 			       sockptr_t optval, unsigned int len)
194509572fcaSLuiz Augusto von Dentz {
194609572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1947b9f9dbadSDan Carpenter 	int err = 0;
1948b9f9dbadSDan Carpenter 	u16 opt;
194909572fcaSLuiz Augusto von Dentz 
195009572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
195109572fcaSLuiz Augusto von Dentz 
195209572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
195309572fcaSLuiz Augusto von Dentz 		return hci_sock_setsockopt_old(sock, level, optname, optval,
195409572fcaSLuiz Augusto von Dentz 					       len);
195509572fcaSLuiz Augusto von Dentz 
195609572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
195709572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
195809572fcaSLuiz Augusto von Dentz 
195909572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
196009572fcaSLuiz Augusto von Dentz 
196109572fcaSLuiz Augusto von Dentz 	switch (optname) {
196209572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
196309572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
196409572fcaSLuiz Augusto von Dentz 		switch (hci_pi(sk)->channel) {
196509572fcaSLuiz Augusto von Dentz 		/* Don't allow changing MTU for channels that are meant for HCI
196609572fcaSLuiz Augusto von Dentz 		 * traffic only.
196709572fcaSLuiz Augusto von Dentz 		 */
196809572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_RAW:
196909572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_USER:
197009572fcaSLuiz Augusto von Dentz 			err = -ENOPROTOOPT;
197109572fcaSLuiz Augusto von Dentz 			goto done;
197209572fcaSLuiz Augusto von Dentz 		}
197309572fcaSLuiz Augusto von Dentz 
1974b9f9dbadSDan Carpenter 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
197509572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
197609572fcaSLuiz Augusto von Dentz 			break;
197709572fcaSLuiz Augusto von Dentz 		}
197809572fcaSLuiz Augusto von Dentz 
197909572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = opt;
198009572fcaSLuiz Augusto von Dentz 		break;
198109572fcaSLuiz Augusto von Dentz 
198209572fcaSLuiz Augusto von Dentz 	default:
198309572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
198409572fcaSLuiz Augusto von Dentz 		break;
198509572fcaSLuiz Augusto von Dentz 	}
198609572fcaSLuiz Augusto von Dentz 
198709572fcaSLuiz Augusto von Dentz done:
198809572fcaSLuiz Augusto von Dentz 	release_sock(sk);
198909572fcaSLuiz Augusto von Dentz 	return err;
199009572fcaSLuiz Augusto von Dentz }
199109572fcaSLuiz Augusto von Dentz 
199209572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
19938fc9ced3SGustavo Padovan 				   char __user *optval, int __user *optlen)
19941da177e4SLinus Torvalds {
19951da177e4SLinus Torvalds 	struct hci_ufilter uf;
19961da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1997cedc5469SMarcel Holtmann 	int len, opt, err = 0;
1998cedc5469SMarcel Holtmann 
1999cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	if (get_user(len, optlen))
20021da177e4SLinus Torvalds 		return -EFAULT;
20031da177e4SLinus Torvalds 
2004cedc5469SMarcel Holtmann 	lock_sock(sk);
2005cedc5469SMarcel Holtmann 
2006cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
2007c2371e80SMarcel Holtmann 		err = -EBADFD;
2008cedc5469SMarcel Holtmann 		goto done;
2009cedc5469SMarcel Holtmann 	}
2010cedc5469SMarcel Holtmann 
20111da177e4SLinus Torvalds 	switch (optname) {
20121da177e4SLinus Torvalds 	case HCI_DATA_DIR:
20131da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
20141da177e4SLinus Torvalds 			opt = 1;
20151da177e4SLinus Torvalds 		else
20161da177e4SLinus Torvalds 			opt = 0;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 		if (put_user(opt, optval))
2019cedc5469SMarcel Holtmann 			err = -EFAULT;
20201da177e4SLinus Torvalds 		break;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
20231da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
20241da177e4SLinus Torvalds 			opt = 1;
20251da177e4SLinus Torvalds 		else
20261da177e4SLinus Torvalds 			opt = 0;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 		if (put_user(opt, optval))
2029cedc5469SMarcel Holtmann 			err = -EFAULT;
20301da177e4SLinus Torvalds 		break;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	case HCI_FILTER:
20331da177e4SLinus Torvalds 		{
20341da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
20351da177e4SLinus Torvalds 
2036e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
20371da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
20381da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
20391da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
20401da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
20411da177e4SLinus Torvalds 		}
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
20441da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
2045cedc5469SMarcel Holtmann 			err = -EFAULT;
20461da177e4SLinus Torvalds 		break;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	default:
2049cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
20501da177e4SLinus Torvalds 		break;
20511da177e4SLinus Torvalds 	}
20521da177e4SLinus Torvalds 
2053cedc5469SMarcel Holtmann done:
2054cedc5469SMarcel Holtmann 	release_sock(sk);
2055cedc5469SMarcel Holtmann 	return err;
20561da177e4SLinus Torvalds }
20571da177e4SLinus Torvalds 
205809572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
205909572fcaSLuiz Augusto von Dentz 			       char __user *optval, int __user *optlen)
206009572fcaSLuiz Augusto von Dentz {
206109572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
206209572fcaSLuiz Augusto von Dentz 	int err = 0;
206309572fcaSLuiz Augusto von Dentz 
206409572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
206509572fcaSLuiz Augusto von Dentz 
206609572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
206709572fcaSLuiz Augusto von Dentz 		return hci_sock_getsockopt_old(sock, level, optname, optval,
206809572fcaSLuiz Augusto von Dentz 					       optlen);
206909572fcaSLuiz Augusto von Dentz 
207009572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
207109572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
207209572fcaSLuiz Augusto von Dentz 
207309572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
207409572fcaSLuiz Augusto von Dentz 
207509572fcaSLuiz Augusto von Dentz 	switch (optname) {
207609572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
207709572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
207809572fcaSLuiz Augusto von Dentz 		if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval))
207909572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
208009572fcaSLuiz Augusto von Dentz 		break;
208109572fcaSLuiz Augusto von Dentz 
208209572fcaSLuiz Augusto von Dentz 	default:
208309572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
208409572fcaSLuiz Augusto von Dentz 		break;
208509572fcaSLuiz Augusto von Dentz 	}
208609572fcaSLuiz Augusto von Dentz 
208709572fcaSLuiz Augusto von Dentz 	release_sock(sk);
208809572fcaSLuiz Augusto von Dentz 	return err;
208909572fcaSLuiz Augusto von Dentz }
209009572fcaSLuiz Augusto von Dentz 
2091709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk)
2092709fca50SNguyen Dinh Phi {
2093b338d917SBrian Gix 	mgmt_cleanup(sk);
2094709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_receive_queue);
2095709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_write_queue);
2096709fca50SNguyen Dinh Phi }
2097709fca50SNguyen Dinh Phi 
209890ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
20991da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
21001da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21011da177e4SLinus Torvalds 	.release	= hci_sock_release,
21021da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
21031da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
21041da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
21051da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
21061da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
21077a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
21087a6038b3SArnd Bergmann 	.compat_ioctl	= hci_sock_compat_ioctl,
21097a6038b3SArnd Bergmann #endif
2110a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
21111da177e4SLinus Torvalds 	.listen		= sock_no_listen,
21121da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
21131da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
21141da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
21151da177e4SLinus Torvalds 	.connect	= sock_no_connect,
21161da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
21171da177e4SLinus Torvalds 	.accept		= sock_no_accept,
21181da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
21191da177e4SLinus Torvalds };
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds static struct proto hci_sk_proto = {
21221da177e4SLinus Torvalds 	.name		= "HCI",
21231da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21241da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
21251da177e4SLinus Torvalds };
21261da177e4SLinus Torvalds 
21273f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
21283f378b68SEric Paris 			   int kern)
21291da177e4SLinus Torvalds {
21301da177e4SLinus Torvalds 	struct sock *sk;
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
21351da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
21381da177e4SLinus Torvalds 
213911aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
21401da177e4SLinus Torvalds 	if (!sk)
21411da177e4SLinus Torvalds 		return -ENOMEM;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	sock_init_data(sock, sk);
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
21501da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
2151709fca50SNguyen Dinh Phi 	sk->sk_destruct = hci_sock_destruct;
21521da177e4SLinus Torvalds 
21531da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
21541da177e4SLinus Torvalds 	return 0;
21551da177e4SLinus Torvalds }
21561da177e4SLinus Torvalds 
2157ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
21581da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
21591da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
21601da177e4SLinus Torvalds 	.create	= hci_sock_create,
21611da177e4SLinus Torvalds };
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds int __init hci_sock_init(void)
21641da177e4SLinus Torvalds {
21651da177e4SLinus Torvalds 	int err;
21661da177e4SLinus Torvalds 
2167b0a8e282SMarcel Holtmann 	BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2168b0a8e282SMarcel Holtmann 
21691da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
21701da177e4SLinus Torvalds 	if (err < 0)
21711da177e4SLinus Torvalds 		return err;
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
2174f7c86637SMasatake YAMATO 	if (err < 0) {
2175f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
21761da177e4SLinus Torvalds 		goto error;
2177f7c86637SMasatake YAMATO 	}
2178f7c86637SMasatake YAMATO 
2179b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
2180f7c86637SMasatake YAMATO 	if (err < 0) {
2181f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
2182f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
2183f7c86637SMasatake YAMATO 		goto error;
2184f7c86637SMasatake YAMATO 	}
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	return 0;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds error:
21911da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
21921da177e4SLinus Torvalds 	return err;
21931da177e4SLinus Torvalds }
21941da177e4SLinus Torvalds 
2195b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
21961da177e4SLinus Torvalds {
2197f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
21985e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
21991da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
22001da177e4SLinus Torvalds }
2201