xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision 0acef50ba3b5ee95bd1598ef3e4c19f065d04eed)
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) {
872*0acef50bSAbhishek Pandit-Subedi 		if (hci_pi(sk)->channel == HCI_CHANNEL_USER &&
873*0acef50bSAbhishek 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.
882*0acef50bSAbhishek Pandit-Subedi 			 *
883*0acef50bSAbhishek Pandit-Subedi 			 * Make sure to also check that we haven't already
884*0acef50bSAbhishek Pandit-Subedi 			 * unregistered since all the cleanup will have already
885*0acef50bSAbhishek Pandit-Subedi 			 * been complete and hdev will get released when we put
886*0acef50bSAbhishek 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 
990c1c4f956SMarcel Holtmann 	lock_sock(sk);
991c1c4f956SMarcel Holtmann 
992c1c4f956SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
993c1c4f956SMarcel Holtmann 		err = -EBADFD;
994c1c4f956SMarcel Holtmann 		goto done;
995c1c4f956SMarcel Holtmann 	}
996c1c4f956SMarcel Holtmann 
997f81f5b2dSMarcel Holtmann 	/* When calling an ioctl on an unbound raw socket, then ensure
998f81f5b2dSMarcel Holtmann 	 * that the monitor gets informed. Ensure that the resulting event
999f81f5b2dSMarcel Holtmann 	 * is only send once by checking if the cookie exists or not. The
1000f81f5b2dSMarcel Holtmann 	 * socket cookie will be only ever generated once for the lifetime
1001f81f5b2dSMarcel Holtmann 	 * of a given socket.
1002f81f5b2dSMarcel Holtmann 	 */
1003f81f5b2dSMarcel Holtmann 	if (hci_sock_gen_cookie(sk)) {
1004f81f5b2dSMarcel Holtmann 		struct sk_buff *skb;
1005f81f5b2dSMarcel Holtmann 
1006f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1007f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1008f81f5b2dSMarcel Holtmann 
1009f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1010f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1011f81f5b2dSMarcel Holtmann 		if (skb) {
1012f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1013f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1014f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1015f81f5b2dSMarcel Holtmann 		}
1016f81f5b2dSMarcel Holtmann 	}
1017f81f5b2dSMarcel Holtmann 
1018c1c4f956SMarcel Holtmann 	release_sock(sk);
1019c1c4f956SMarcel Holtmann 
10201da177e4SLinus Torvalds 	switch (cmd) {
10211da177e4SLinus Torvalds 	case HCIGETDEVLIST:
10221da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	case HCIGETDEVINFO:
10251da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	case HCIGETCONNLIST:
10281da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	case HCIDEVUP:
10311da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1032bf5b30b8SZhao Hongjiang 			return -EPERM;
10331da177e4SLinus Torvalds 		return hci_dev_open(arg);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	case HCIDEVDOWN:
10361da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1037bf5b30b8SZhao Hongjiang 			return -EPERM;
10381da177e4SLinus Torvalds 		return hci_dev_close(arg);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	case HCIDEVRESET:
10411da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1042bf5b30b8SZhao Hongjiang 			return -EPERM;
10431da177e4SLinus Torvalds 		return hci_dev_reset(arg);
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	case HCIDEVRESTAT:
10461da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1047bf5b30b8SZhao Hongjiang 			return -EPERM;
10481da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	case HCISETSCAN:
10511da177e4SLinus Torvalds 	case HCISETAUTH:
10521da177e4SLinus Torvalds 	case HCISETENCRYPT:
10531da177e4SLinus Torvalds 	case HCISETPTYPE:
10541da177e4SLinus Torvalds 	case HCISETLINKPOL:
10551da177e4SLinus Torvalds 	case HCISETLINKMODE:
10561da177e4SLinus Torvalds 	case HCISETACLMTU:
10571da177e4SLinus Torvalds 	case HCISETSCOMTU:
10581da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1059bf5b30b8SZhao Hongjiang 			return -EPERM;
10601da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	case HCIINQUIRY:
10631da177e4SLinus Torvalds 		return hci_inquiry(argp);
1064c1c4f956SMarcel Holtmann 	}
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	lock_sock(sk);
1067c1c4f956SMarcel Holtmann 
10681da177e4SLinus Torvalds 	err = hci_sock_bound_ioctl(sk, cmd, arg);
1069c1c4f956SMarcel Holtmann 
1070c1c4f956SMarcel Holtmann done:
10711da177e4SLinus Torvalds 	release_sock(sk);
10721da177e4SLinus Torvalds 	return err;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
10757a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
10767a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd,
10777a6038b3SArnd Bergmann 				 unsigned long arg)
10787a6038b3SArnd Bergmann {
10797a6038b3SArnd Bergmann 	switch (cmd) {
10807a6038b3SArnd Bergmann 	case HCIDEVUP:
10817a6038b3SArnd Bergmann 	case HCIDEVDOWN:
10827a6038b3SArnd Bergmann 	case HCIDEVRESET:
10837a6038b3SArnd Bergmann 	case HCIDEVRESTAT:
10847a6038b3SArnd Bergmann 		return hci_sock_ioctl(sock, cmd, arg);
10857a6038b3SArnd Bergmann 	}
10867a6038b3SArnd Bergmann 
10877a6038b3SArnd Bergmann 	return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
10887a6038b3SArnd Bergmann }
10897a6038b3SArnd Bergmann #endif
10907a6038b3SArnd Bergmann 
10918fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
10928fc9ced3SGustavo Padovan 			 int addr_len)
10931da177e4SLinus Torvalds {
10940381101fSJohan Hedberg 	struct sockaddr_hci haddr;
10951da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10961da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
1097f4cdbb3fSMarcel Holtmann 	struct sk_buff *skb;
10980381101fSJohan Hedberg 	int len, err = 0;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
11011da177e4SLinus Torvalds 
11020381101fSJohan Hedberg 	if (!addr)
11030381101fSJohan Hedberg 		return -EINVAL;
11040381101fSJohan Hedberg 
11050381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
11060381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
11070381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
11080381101fSJohan Hedberg 
11090381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
11100381101fSJohan Hedberg 		return -EINVAL;
11110381101fSJohan Hedberg 
11121da177e4SLinus Torvalds 	lock_sock(sk);
11131da177e4SLinus Torvalds 
1114e0448092STetsuo Handa 	/* Allow detaching from dead device and attaching to alive device, if
1115e0448092STetsuo Handa 	 * the caller wants to re-bind (instead of close) this socket in
1116e0448092STetsuo Handa 	 * response to hci_sock_dev_event(HCI_DEV_UNREG) notification.
1117e0448092STetsuo Handa 	 */
1118e0448092STetsuo Handa 	hdev = hci_pi(sk)->hdev;
1119e0448092STetsuo Handa 	if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1120e0448092STetsuo Handa 		hci_pi(sk)->hdev = NULL;
1121e0448092STetsuo Handa 		sk->sk_state = BT_OPEN;
1122e0448092STetsuo Handa 		hci_dev_put(hdev);
1123e0448092STetsuo Handa 	}
1124e0448092STetsuo Handa 	hdev = NULL;
1125e0448092STetsuo Handa 
11267cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
11277cc2ade2SMarcel Holtmann 		err = -EALREADY;
11287cc2ade2SMarcel Holtmann 		goto done;
11297cc2ade2SMarcel Holtmann 	}
11307cc2ade2SMarcel Holtmann 
11317cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
11327cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
11337cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
11341da177e4SLinus Torvalds 			err = -EALREADY;
11351da177e4SLinus Torvalds 			goto done;
11361da177e4SLinus Torvalds 		}
11371da177e4SLinus Torvalds 
11380381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
11390381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
114070f23020SAndrei Emeltchenko 			if (!hdev) {
11411da177e4SLinus Torvalds 				err = -ENODEV;
11421da177e4SLinus Torvalds 				goto done;
11431da177e4SLinus Torvalds 			}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
11461da177e4SLinus Torvalds 		}
11471da177e4SLinus Torvalds 
11485a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1149f81f5b2dSMarcel Holtmann 
1150f4cdbb3fSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1151f4cdbb3fSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1152f4cdbb3fSMarcel Holtmann 			 * then there has been already an ioctl issued against
115391641b79SZheng Yongjun 			 * an unbound socket and with that triggered an open
1154f4cdbb3fSMarcel Holtmann 			 * notification. Send a close notification first to
1155f4cdbb3fSMarcel Holtmann 			 * allow the state transition to bounded.
1156f81f5b2dSMarcel Holtmann 			 */
1157f4cdbb3fSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1158f4cdbb3fSMarcel Holtmann 			if (skb) {
1159f4cdbb3fSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1160f4cdbb3fSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1161f4cdbb3fSMarcel Holtmann 				kfree_skb(skb);
1162f4cdbb3fSMarcel Holtmann 			}
1163f4cdbb3fSMarcel Holtmann 		}
1164f81f5b2dSMarcel Holtmann 
1165f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1166f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1167f81f5b2dSMarcel Holtmann 
1168f4cdbb3fSMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
1169f4cdbb3fSMarcel Holtmann 
1170f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1171f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1172f81f5b2dSMarcel Holtmann 		if (skb) {
1173f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1174f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1175f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1176f81f5b2dSMarcel Holtmann 		}
11777cc2ade2SMarcel Holtmann 		break;
11787cc2ade2SMarcel Holtmann 
117923500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
118023500189SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
118123500189SMarcel Holtmann 			err = -EALREADY;
118223500189SMarcel Holtmann 			goto done;
118323500189SMarcel Holtmann 		}
118423500189SMarcel Holtmann 
118523500189SMarcel Holtmann 		if (haddr.hci_dev == HCI_DEV_NONE) {
118623500189SMarcel Holtmann 			err = -EINVAL;
118723500189SMarcel Holtmann 			goto done;
118823500189SMarcel Holtmann 		}
118923500189SMarcel Holtmann 
119010a8b86fSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
119123500189SMarcel Holtmann 			err = -EPERM;
119223500189SMarcel Holtmann 			goto done;
119323500189SMarcel Holtmann 		}
119423500189SMarcel Holtmann 
119523500189SMarcel Holtmann 		hdev = hci_dev_get(haddr.hci_dev);
119623500189SMarcel Holtmann 		if (!hdev) {
119723500189SMarcel Holtmann 			err = -ENODEV;
119823500189SMarcel Holtmann 			goto done;
119923500189SMarcel Holtmann 		}
120023500189SMarcel Holtmann 
1201781f899fSMarcel Holtmann 		if (test_bit(HCI_INIT, &hdev->flags) ||
1202d7a5a11dSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_SETUP) ||
1203781f899fSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1204781f899fSMarcel Holtmann 		    (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1205781f899fSMarcel Holtmann 		     test_bit(HCI_UP, &hdev->flags))) {
120623500189SMarcel Holtmann 			err = -EBUSY;
120723500189SMarcel Holtmann 			hci_dev_put(hdev);
120823500189SMarcel Holtmann 			goto done;
120923500189SMarcel Holtmann 		}
121023500189SMarcel Holtmann 
1211238be788SMarcel Holtmann 		if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
121223500189SMarcel Holtmann 			err = -EUSERS;
121323500189SMarcel Holtmann 			hci_dev_put(hdev);
121423500189SMarcel Holtmann 			goto done;
121523500189SMarcel Holtmann 		}
121623500189SMarcel Holtmann 
121723500189SMarcel Holtmann 		mgmt_index_removed(hdev);
121823500189SMarcel Holtmann 
121923500189SMarcel Holtmann 		err = hci_dev_open(hdev->id);
122023500189SMarcel Holtmann 		if (err) {
1221781f899fSMarcel Holtmann 			if (err == -EALREADY) {
1222781f899fSMarcel Holtmann 				/* In case the transport is already up and
1223781f899fSMarcel Holtmann 				 * running, clear the error here.
1224781f899fSMarcel Holtmann 				 *
12259332ef9dSMasahiro Yamada 				 * This can happen when opening a user
1226781f899fSMarcel Holtmann 				 * channel and HCI_AUTO_OFF grace period
1227781f899fSMarcel Holtmann 				 * is still active.
1228781f899fSMarcel Holtmann 				 */
1229781f899fSMarcel Holtmann 				err = 0;
1230781f899fSMarcel Holtmann 			} else {
1231a358dc11SMarcel Holtmann 				hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1232c6521401SMarcel Holtmann 				mgmt_index_added(hdev);
123323500189SMarcel Holtmann 				hci_dev_put(hdev);
123423500189SMarcel Holtmann 				goto done;
123523500189SMarcel Holtmann 			}
1236781f899fSMarcel Holtmann 		}
123723500189SMarcel Holtmann 
12385a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1239aa1638ddSMarcel Holtmann 
1240aa1638ddSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1241aa1638ddSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1242aa1638ddSMarcel Holtmann 			 * this socket will transition from a raw socket into
12439332ef9dSMasahiro Yamada 			 * a user channel socket. For a clean transition, send
1244aa1638ddSMarcel Holtmann 			 * the close notification first.
1245aa1638ddSMarcel Holtmann 			 */
1246aa1638ddSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1247aa1638ddSMarcel Holtmann 			if (skb) {
1248aa1638ddSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1249aa1638ddSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1250aa1638ddSMarcel Holtmann 				kfree_skb(skb);
1251aa1638ddSMarcel Holtmann 			}
1252aa1638ddSMarcel Holtmann 		}
1253aa1638ddSMarcel Holtmann 
1254aa1638ddSMarcel Holtmann 		/* The user channel is restricted to CAP_NET_ADMIN
1255aa1638ddSMarcel Holtmann 		 * capabilities and with that implicitly trusted.
1256aa1638ddSMarcel Holtmann 		 */
1257aa1638ddSMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1258aa1638ddSMarcel Holtmann 
125923500189SMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
12605a6d2cf5SMarcel Holtmann 
1261aa1638ddSMarcel Holtmann 		/* Send event to monitor */
1262aa1638ddSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1263aa1638ddSMarcel Holtmann 		if (skb) {
1264aa1638ddSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1265aa1638ddSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1266aa1638ddSMarcel Holtmann 			kfree_skb(skb);
1267aa1638ddSMarcel Holtmann 		}
1268aa1638ddSMarcel Holtmann 
12695a6d2cf5SMarcel Holtmann 		atomic_inc(&hdev->promisc);
127023500189SMarcel Holtmann 		break;
127123500189SMarcel Holtmann 
1272cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1273cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1274cd82e61cSMarcel Holtmann 			err = -EINVAL;
1275cd82e61cSMarcel Holtmann 			goto done;
1276cd82e61cSMarcel Holtmann 		}
1277cd82e61cSMarcel Holtmann 
1278cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
1279cd82e61cSMarcel Holtmann 			err = -EPERM;
1280cd82e61cSMarcel Holtmann 			goto done;
1281cd82e61cSMarcel Holtmann 		}
1282cd82e61cSMarcel Holtmann 
12835a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
12845a6d2cf5SMarcel Holtmann 
128550ebc055SMarcel Holtmann 		/* The monitor interface is restricted to CAP_NET_RAW
128650ebc055SMarcel Holtmann 		 * capabilities and with that implicitly trusted.
128750ebc055SMarcel Holtmann 		 */
128850ebc055SMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
128950ebc055SMarcel Holtmann 
1290787b306cSJohannes Berg 		send_monitor_note(sk, "Linux version %s (%s)",
1291787b306cSJohannes Berg 				  init_utsname()->release,
1292787b306cSJohannes Berg 				  init_utsname()->machine);
12939e8305b3SMarcel Holtmann 		send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
12949e8305b3SMarcel Holtmann 				  BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
1295cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
1296249fa169SMarcel Holtmann 		send_monitor_control_replay(sk);
1297cd82e61cSMarcel Holtmann 
1298cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
1299cd82e61cSMarcel Holtmann 		break;
1300cd82e61cSMarcel Holtmann 
1301ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
1302ac714949SMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1303ac714949SMarcel Holtmann 			err = -EINVAL;
1304ac714949SMarcel Holtmann 			goto done;
1305ac714949SMarcel Holtmann 		}
1306ac714949SMarcel Holtmann 
1307ac714949SMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
1308ac714949SMarcel Holtmann 			err = -EPERM;
1309ac714949SMarcel Holtmann 			goto done;
1310ac714949SMarcel Holtmann 		}
13115a6d2cf5SMarcel Holtmann 
13125a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1313ac714949SMarcel Holtmann 		break;
1314ac714949SMarcel Holtmann 
13157cc2ade2SMarcel Holtmann 	default:
1316801c1e8dSJohan Hedberg 		if (!hci_mgmt_chan_find(haddr.hci_channel)) {
13177cc2ade2SMarcel Holtmann 			err = -EINVAL;
13187cc2ade2SMarcel Holtmann 			goto done;
13197cc2ade2SMarcel Holtmann 		}
13207cc2ade2SMarcel Holtmann 
1321801c1e8dSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
1322801c1e8dSJohan Hedberg 			err = -EINVAL;
1323801c1e8dSJohan Hedberg 			goto done;
1324801c1e8dSJohan Hedberg 		}
1325801c1e8dSJohan Hedberg 
13261195fbb8SMarcel Holtmann 		/* Users with CAP_NET_ADMIN capabilities are allowed
13271195fbb8SMarcel Holtmann 		 * access to all management commands and events. For
13281195fbb8SMarcel Holtmann 		 * untrusted users the interface is restricted and
13291195fbb8SMarcel Holtmann 		 * also only untrusted events are sent.
133050ebc055SMarcel Holtmann 		 */
13311195fbb8SMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
133250ebc055SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
133350ebc055SMarcel Holtmann 
13345a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
13355a6d2cf5SMarcel Holtmann 
1336f9207338SMarcel Holtmann 		/* At the moment the index and unconfigured index events
1337f9207338SMarcel Holtmann 		 * are enabled unconditionally. Setting them on each
1338f9207338SMarcel Holtmann 		 * socket when binding keeps this functionality. They
1339f9207338SMarcel Holtmann 		 * however might be cleared later and then sending of these
1340f9207338SMarcel Holtmann 		 * events will be disabled, but that is then intentional.
1341f6b7712eSMarcel Holtmann 		 *
1342f6b7712eSMarcel Holtmann 		 * This also enables generic events that are safe to be
1343f6b7712eSMarcel Holtmann 		 * received by untrusted users. Example for such events
1344f6b7712eSMarcel Holtmann 		 * are changes to settings, class of device, name etc.
1345f9207338SMarcel Holtmann 		 */
13465a6d2cf5SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
1347f4cdbb3fSMarcel Holtmann 			if (!hci_sock_gen_cookie(sk)) {
1348f4cdbb3fSMarcel Holtmann 				/* In the case when a cookie has already been
134991641b79SZheng Yongjun 				 * assigned, this socket will transition from
1350f4cdbb3fSMarcel Holtmann 				 * a raw socket into a control socket. To
135191641b79SZheng Yongjun 				 * allow for a clean transition, send the
1352f4cdbb3fSMarcel Holtmann 				 * close notification first.
1353f4cdbb3fSMarcel Holtmann 				 */
1354f4cdbb3fSMarcel Holtmann 				skb = create_monitor_ctrl_close(sk);
1355f4cdbb3fSMarcel Holtmann 				if (skb) {
1356f4cdbb3fSMarcel Holtmann 					hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1357f4cdbb3fSMarcel Holtmann 							    HCI_SOCK_TRUSTED, NULL);
1358f4cdbb3fSMarcel Holtmann 					kfree_skb(skb);
1359f4cdbb3fSMarcel Holtmann 				}
1360f4cdbb3fSMarcel Holtmann 			}
136170ecce91SMarcel Holtmann 
1362249fa169SMarcel Holtmann 			/* Send event to monitor */
1363249fa169SMarcel Holtmann 			skb = create_monitor_ctrl_open(sk);
1364249fa169SMarcel Holtmann 			if (skb) {
1365249fa169SMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1366249fa169SMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1367249fa169SMarcel Holtmann 				kfree_skb(skb);
1368249fa169SMarcel Holtmann 			}
1369249fa169SMarcel Holtmann 
1370f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1371f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
13725504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
13735504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
13745504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
13755504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1376f9207338SMarcel Holtmann 		}
1377801c1e8dSJohan Hedberg 		break;
1378801c1e8dSJohan Hedberg 	}
1379801c1e8dSJohan Hedberg 
138009572fcaSLuiz Augusto von Dentz 	/* Default MTU to HCI_MAX_FRAME_SIZE if not set */
138109572fcaSLuiz Augusto von Dentz 	if (!hci_pi(sk)->mtu)
138209572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE;
138309572fcaSLuiz Augusto von Dentz 
13841da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds done:
13871da177e4SLinus Torvalds 	release_sock(sk);
13881da177e4SLinus Torvalds 	return err;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
13918fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
13929b2c45d4SDenys Vlasenko 			    int peer)
13931da177e4SLinus Torvalds {
13941da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
13951da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13969d4b68b2SMarcel Holtmann 	struct hci_dev *hdev;
13979d4b68b2SMarcel Holtmann 	int err = 0;
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
14001da177e4SLinus Torvalds 
140106f43cbcSMarcel Holtmann 	if (peer)
140206f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
140306f43cbcSMarcel Holtmann 
14041da177e4SLinus Torvalds 	lock_sock(sk);
14051da177e4SLinus Torvalds 
1406e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1407e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1408e0448092STetsuo Handa 		err = PTR_ERR(hdev);
14099d4b68b2SMarcel Holtmann 		goto done;
14109d4b68b2SMarcel Holtmann 	}
14119d4b68b2SMarcel Holtmann 
14121da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
14137b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
14149d4b68b2SMarcel Holtmann 	haddr->hci_channel= hci_pi(sk)->channel;
14159b2c45d4SDenys Vlasenko 	err = sizeof(*haddr);
14161da177e4SLinus Torvalds 
14179d4b68b2SMarcel Holtmann done:
14181da177e4SLinus Torvalds 	release_sock(sk);
14199d4b68b2SMarcel Holtmann 	return err;
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds 
14226039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
14236039aa73SGustavo Padovan 			  struct sk_buff *skb)
14241da177e4SLinus Torvalds {
142532929e1fSAlain Michaud 	__u8 mask = hci_pi(sk)->cmsg_mask;
14261da177e4SLinus Torvalds 
14270d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
14280d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
14298fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
14308fc9ced3SGustavo Padovan 			 &incoming);
14310d48d939SMarcel Holtmann 	}
14321da177e4SLinus Torvalds 
1433a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
1434f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
143513c6ee2aSDeepa Dinamani 		struct old_timeval32 ctv;
1436f6e623a6SJohann Felix Soden #endif
143713c6ee2aSDeepa Dinamani 		struct __kernel_old_timeval tv;
1438767c5eb5SMarcel Holtmann 		void *data;
1439767c5eb5SMarcel Holtmann 		int len;
1440a61bbcf2SPatrick McHardy 
1441a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
1442767c5eb5SMarcel Holtmann 
14431da97f83SDavid S. Miller 		data = &tv;
14441da97f83SDavid S. Miller 		len = sizeof(tv);
14451da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
1446da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
1447da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
1448767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
1449767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
1450767c5eb5SMarcel Holtmann 			data = &ctv;
1451767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
1452767c5eb5SMarcel Holtmann 		}
14531da97f83SDavid S. Miller #endif
1454767c5eb5SMarcel Holtmann 
1455767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
1456a61bbcf2SPatrick McHardy 	}
14571da177e4SLinus Torvalds }
14581da177e4SLinus Torvalds 
14598528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
14608528d3f7SMarcel Holtmann 			    size_t len, int flags)
14611da177e4SLinus Torvalds {
14621da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
14631da177e4SLinus Torvalds 	struct sk_buff *skb;
14641da177e4SLinus Torvalds 	int copied, err;
146583871f8cSDenis Kenzior 	unsigned int skblen;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
14681da177e4SLinus Torvalds 
1469d94a6104SMarcel Holtmann 	if (flags & MSG_OOB)
14701da177e4SLinus Torvalds 		return -EOPNOTSUPP;
14711da177e4SLinus Torvalds 
1472ac714949SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1473ac714949SMarcel Holtmann 		return -EOPNOTSUPP;
1474ac714949SMarcel Holtmann 
14751da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
14761da177e4SLinus Torvalds 		return 0;
14771da177e4SLinus Torvalds 
1478f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
147970f23020SAndrei Emeltchenko 	if (!skb)
14801da177e4SLinus Torvalds 		return err;
14811da177e4SLinus Torvalds 
148283871f8cSDenis Kenzior 	skblen = skb->len;
14831da177e4SLinus Torvalds 	copied = skb->len;
14841da177e4SLinus Torvalds 	if (len < copied) {
14851da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
14861da177e4SLinus Torvalds 		copied = len;
14871da177e4SLinus Torvalds 	}
14881da177e4SLinus Torvalds 
1489badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
149051f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
14911da177e4SLinus Torvalds 
14923a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
14933a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
14941da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
14953a208627SMarcel Holtmann 		break;
149623500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
1497cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1498cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
1499cd82e61cSMarcel Holtmann 		break;
1500801c1e8dSJohan Hedberg 	default:
1501801c1e8dSJohan Hedberg 		if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1502801c1e8dSJohan Hedberg 			sock_recv_timestamp(msg, sk, skb);
1503801c1e8dSJohan Hedberg 		break;
15043a208627SMarcel Holtmann 	}
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
15071da177e4SLinus Torvalds 
15084f34228bSLuiz Augusto von Dentz 	if (flags & MSG_TRUNC)
150983871f8cSDenis Kenzior 		copied = skblen;
151083871f8cSDenis Kenzior 
15111da177e4SLinus Torvalds 	return err ? : copied;
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds 
151464ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
151564ba2eb3SLuiz Augusto von Dentz 			struct sk_buff *skb)
1516fa4335d7SJohan Hedberg {
1517fa4335d7SJohan Hedberg 	u8 *cp;
1518fa4335d7SJohan Hedberg 	struct mgmt_hdr *hdr;
1519fa4335d7SJohan Hedberg 	u16 opcode, index, len;
1520fa4335d7SJohan Hedberg 	struct hci_dev *hdev = NULL;
1521fa4335d7SJohan Hedberg 	const struct hci_mgmt_handler *handler;
1522fa4335d7SJohan Hedberg 	bool var_len, no_hdev;
1523fa4335d7SJohan Hedberg 	int err;
1524fa4335d7SJohan Hedberg 
152564ba2eb3SLuiz Augusto von Dentz 	BT_DBG("got %d bytes", skb->len);
1526fa4335d7SJohan Hedberg 
152764ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr))
1528fa4335d7SJohan Hedberg 		return -EINVAL;
1529fa4335d7SJohan Hedberg 
153064ba2eb3SLuiz Augusto von Dentz 	hdr = (void *)skb->data;
1531fa4335d7SJohan Hedberg 	opcode = __le16_to_cpu(hdr->opcode);
1532fa4335d7SJohan Hedberg 	index = __le16_to_cpu(hdr->index);
1533fa4335d7SJohan Hedberg 	len = __le16_to_cpu(hdr->len);
1534fa4335d7SJohan Hedberg 
153564ba2eb3SLuiz Augusto von Dentz 	if (len != skb->len - sizeof(*hdr)) {
1536fa4335d7SJohan Hedberg 		err = -EINVAL;
1537fa4335d7SJohan Hedberg 		goto done;
1538fa4335d7SJohan Hedberg 	}
1539fa4335d7SJohan Hedberg 
154038ceaa00SMarcel Holtmann 	if (chan->channel == HCI_CHANNEL_CONTROL) {
154164ba2eb3SLuiz Augusto von Dentz 		struct sk_buff *cmd;
154238ceaa00SMarcel Holtmann 
154338ceaa00SMarcel Holtmann 		/* Send event to monitor */
154464ba2eb3SLuiz Augusto von Dentz 		cmd = create_monitor_ctrl_command(sk, index, opcode, len,
154564ba2eb3SLuiz Augusto von Dentz 						  skb->data + sizeof(*hdr));
154664ba2eb3SLuiz Augusto von Dentz 		if (cmd) {
154764ba2eb3SLuiz Augusto von Dentz 			hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd,
154838ceaa00SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
154964ba2eb3SLuiz Augusto von Dentz 			kfree_skb(cmd);
155038ceaa00SMarcel Holtmann 		}
155138ceaa00SMarcel Holtmann 	}
155238ceaa00SMarcel Holtmann 
1553fa4335d7SJohan Hedberg 	if (opcode >= chan->handler_count ||
1554fa4335d7SJohan Hedberg 	    chan->handlers[opcode].func == NULL) {
1555fa4335d7SJohan Hedberg 		BT_DBG("Unknown op %u", opcode);
1556fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1557fa4335d7SJohan Hedberg 				      MGMT_STATUS_UNKNOWN_COMMAND);
1558fa4335d7SJohan Hedberg 		goto done;
1559fa4335d7SJohan Hedberg 	}
1560fa4335d7SJohan Hedberg 
1561fa4335d7SJohan Hedberg 	handler = &chan->handlers[opcode];
1562fa4335d7SJohan Hedberg 
1563fa4335d7SJohan Hedberg 	if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1564fa4335d7SJohan Hedberg 	    !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1565fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1566fa4335d7SJohan Hedberg 				      MGMT_STATUS_PERMISSION_DENIED);
1567fa4335d7SJohan Hedberg 		goto done;
1568fa4335d7SJohan Hedberg 	}
1569fa4335d7SJohan Hedberg 
1570fa4335d7SJohan Hedberg 	if (index != MGMT_INDEX_NONE) {
1571fa4335d7SJohan Hedberg 		hdev = hci_dev_get(index);
1572fa4335d7SJohan Hedberg 		if (!hdev) {
1573fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1574fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1575fa4335d7SJohan Hedberg 			goto done;
1576fa4335d7SJohan Hedberg 		}
1577fa4335d7SJohan Hedberg 
1578fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1579fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1580fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1581fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1582fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1583fa4335d7SJohan Hedberg 			goto done;
1584fa4335d7SJohan Hedberg 		}
1585fa4335d7SJohan Hedberg 
1586fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1587fa4335d7SJohan Hedberg 		    !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1588fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1589fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1590fa4335d7SJohan Hedberg 			goto done;
1591fa4335d7SJohan Hedberg 		}
1592fa4335d7SJohan Hedberg 	}
1593fa4335d7SJohan Hedberg 
1594d5cc6626SMarcel Holtmann 	if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) {
1595fa4335d7SJohan Hedberg 		no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1596fa4335d7SJohan Hedberg 		if (no_hdev != !hdev) {
1597fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1598fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1599fa4335d7SJohan Hedberg 			goto done;
1600fa4335d7SJohan Hedberg 		}
1601d5cc6626SMarcel Holtmann 	}
1602fa4335d7SJohan Hedberg 
1603fa4335d7SJohan Hedberg 	var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1604fa4335d7SJohan Hedberg 	if ((var_len && len < handler->data_len) ||
1605fa4335d7SJohan Hedberg 	    (!var_len && len != handler->data_len)) {
1606fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1607fa4335d7SJohan Hedberg 				      MGMT_STATUS_INVALID_PARAMS);
1608fa4335d7SJohan Hedberg 		goto done;
1609fa4335d7SJohan Hedberg 	}
1610fa4335d7SJohan Hedberg 
1611fa4335d7SJohan Hedberg 	if (hdev && chan->hdev_init)
1612fa4335d7SJohan Hedberg 		chan->hdev_init(sk, hdev);
1613fa4335d7SJohan Hedberg 
161464ba2eb3SLuiz Augusto von Dentz 	cp = skb->data + sizeof(*hdr);
1615fa4335d7SJohan Hedberg 
1616fa4335d7SJohan Hedberg 	err = handler->func(sk, hdev, cp, len);
1617fa4335d7SJohan Hedberg 	if (err < 0)
1618fa4335d7SJohan Hedberg 		goto done;
1619fa4335d7SJohan Hedberg 
162064ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1621fa4335d7SJohan Hedberg 
1622fa4335d7SJohan Hedberg done:
1623fa4335d7SJohan Hedberg 	if (hdev)
1624fa4335d7SJohan Hedberg 		hci_dev_put(hdev);
1625fa4335d7SJohan Hedberg 
1626fa4335d7SJohan Hedberg 	return err;
1627fa4335d7SJohan Hedberg }
1628fa4335d7SJohan Hedberg 
162964ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb,
163064ba2eb3SLuiz Augusto von Dentz 			     unsigned int flags)
1631ac714949SMarcel Holtmann {
1632ac714949SMarcel Holtmann 	struct hci_mon_hdr *hdr;
1633ac714949SMarcel Holtmann 	struct hci_dev *hdev;
1634ac714949SMarcel Holtmann 	u16 index;
1635ac714949SMarcel Holtmann 	int err;
1636ac714949SMarcel Holtmann 
1637ac714949SMarcel Holtmann 	/* The logging frame consists at minimum of the standard header,
1638ac714949SMarcel Holtmann 	 * the priority byte, the ident length byte and at least one string
1639ac714949SMarcel Holtmann 	 * terminator NUL byte. Anything shorter are invalid packets.
1640ac714949SMarcel Holtmann 	 */
164164ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr) + 3)
1642ac714949SMarcel Holtmann 		return -EINVAL;
1643ac714949SMarcel Holtmann 
1644ac714949SMarcel Holtmann 	hdr = (void *)skb->data;
1645ac714949SMarcel Holtmann 
164664ba2eb3SLuiz Augusto von Dentz 	if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr))
164764ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1648ac714949SMarcel Holtmann 
1649ac714949SMarcel Holtmann 	if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1650ac714949SMarcel Holtmann 		__u8 priority = skb->data[sizeof(*hdr)];
1651ac714949SMarcel Holtmann 		__u8 ident_len = skb->data[sizeof(*hdr) + 1];
1652ac714949SMarcel Holtmann 
1653ac714949SMarcel Holtmann 		/* Only the priorities 0-7 are valid and with that any other
1654ac714949SMarcel Holtmann 		 * value results in an invalid packet.
1655ac714949SMarcel Holtmann 		 *
1656ac714949SMarcel Holtmann 		 * The priority byte is followed by an ident length byte and
1657ac714949SMarcel Holtmann 		 * the NUL terminated ident string. Check that the ident
1658ac714949SMarcel Holtmann 		 * length is not overflowing the packet and also that the
1659ac714949SMarcel Holtmann 		 * ident string itself is NUL terminated. In case the ident
1660ac714949SMarcel Holtmann 		 * length is zero, the length value actually doubles as NUL
1661ac714949SMarcel Holtmann 		 * terminator identifier.
1662ac714949SMarcel Holtmann 		 *
1663ac714949SMarcel Holtmann 		 * The message follows the ident string (if present) and
1664ac714949SMarcel Holtmann 		 * must be NUL terminated. Otherwise it is not a valid packet.
1665ac714949SMarcel Holtmann 		 */
166664ba2eb3SLuiz Augusto von Dentz 		if (priority > 7 || skb->data[skb->len - 1] != 0x00 ||
166764ba2eb3SLuiz Augusto von Dentz 		    ident_len > skb->len - sizeof(*hdr) - 3 ||
166864ba2eb3SLuiz Augusto von Dentz 		    skb->data[sizeof(*hdr) + ident_len + 1] != 0x00)
166964ba2eb3SLuiz Augusto von Dentz 			return -EINVAL;
1670ac714949SMarcel Holtmann 	} else {
167164ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1672ac714949SMarcel Holtmann 	}
1673ac714949SMarcel Holtmann 
1674ac714949SMarcel Holtmann 	index = __le16_to_cpu(hdr->index);
1675ac714949SMarcel Holtmann 
1676ac714949SMarcel Holtmann 	if (index != MGMT_INDEX_NONE) {
1677ac714949SMarcel Holtmann 		hdev = hci_dev_get(index);
167864ba2eb3SLuiz Augusto von Dentz 		if (!hdev)
167964ba2eb3SLuiz Augusto von Dentz 			return -ENODEV;
1680ac714949SMarcel Holtmann 	} else {
1681ac714949SMarcel Holtmann 		hdev = NULL;
1682ac714949SMarcel Holtmann 	}
1683ac714949SMarcel Holtmann 
1684ac714949SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1685ac714949SMarcel Holtmann 
1686ac714949SMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
168764ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1688ac714949SMarcel Holtmann 
1689ac714949SMarcel Holtmann 	if (hdev)
1690ac714949SMarcel Holtmann 		hci_dev_put(hdev);
1691ac714949SMarcel Holtmann 
1692ac714949SMarcel Holtmann 	return err;
1693ac714949SMarcel Holtmann }
1694ac714949SMarcel Holtmann 
16951b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
16961b784140SYing Xue 			    size_t len)
16971da177e4SLinus Torvalds {
16981da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1699801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *chan;
17001da177e4SLinus Torvalds 	struct hci_dev *hdev;
17011da177e4SLinus Torvalds 	struct sk_buff *skb;
17021da177e4SLinus Torvalds 	int err;
17030b59e272STetsuo Handa 	const unsigned int flags = msg->msg_flags;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
17061da177e4SLinus Torvalds 
17070b59e272STetsuo Handa 	if (flags & MSG_OOB)
17081da177e4SLinus Torvalds 		return -EOPNOTSUPP;
17091da177e4SLinus Torvalds 
17100b59e272STetsuo Handa 	if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
17111da177e4SLinus Torvalds 		return -EINVAL;
17121da177e4SLinus Torvalds 
171309572fcaSLuiz Augusto von Dentz 	if (len < 4 || len > hci_pi(sk)->mtu)
17141da177e4SLinus Torvalds 		return -EINVAL;
17151da177e4SLinus Torvalds 
171664ba2eb3SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
171764ba2eb3SLuiz Augusto von Dentz 	if (IS_ERR(skb))
171864ba2eb3SLuiz Augusto von Dentz 		return PTR_ERR(skb);
17190b59e272STetsuo Handa 
17201da177e4SLinus Torvalds 	lock_sock(sk);
17211da177e4SLinus Torvalds 
17220381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
17230381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
172423500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
17250381101fSJohan Hedberg 		break;
1726cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1727cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
172864ba2eb3SLuiz Augusto von Dentz 		goto drop;
1729ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
173064ba2eb3SLuiz Augusto von Dentz 		err = hci_logging_frame(sk, skb, flags);
173164ba2eb3SLuiz Augusto von Dentz 		goto drop;
17320381101fSJohan Hedberg 	default:
1733801c1e8dSJohan Hedberg 		mutex_lock(&mgmt_chan_list_lock);
1734801c1e8dSJohan Hedberg 		chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1735801c1e8dSJohan Hedberg 		if (chan)
173664ba2eb3SLuiz Augusto von Dentz 			err = hci_mgmt_cmd(chan, sk, skb);
1737801c1e8dSJohan Hedberg 		else
17380381101fSJohan Hedberg 			err = -EINVAL;
1739801c1e8dSJohan Hedberg 
1740801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
174164ba2eb3SLuiz Augusto von Dentz 		goto drop;
17420381101fSJohan Hedberg 	}
17430381101fSJohan Hedberg 
1744e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1745e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1746e0448092STetsuo Handa 		err = PTR_ERR(hdev);
174764ba2eb3SLuiz Augusto von Dentz 		goto drop;
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds 
17507e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
17517e21addcSMarcel Holtmann 		err = -ENETDOWN;
175264ba2eb3SLuiz Augusto von Dentz 		goto drop;
17537e21addcSMarcel Holtmann 	}
17547e21addcSMarcel Holtmann 
17558528d3f7SMarcel Holtmann 	hci_skb_pkt_type(skb) = skb->data[0];
17561da177e4SLinus Torvalds 	skb_pull(skb, 1);
17571da177e4SLinus Torvalds 
17581bc5ad16SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
17591bc5ad16SMarcel Holtmann 		/* No permission check is needed for user channel
17601bc5ad16SMarcel Holtmann 		 * since that gets enforced when binding the socket.
17611bc5ad16SMarcel Holtmann 		 *
17621bc5ad16SMarcel Holtmann 		 * However check that the packet type is valid.
17631bc5ad16SMarcel Holtmann 		 */
1764d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1765d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1766cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1767cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
17681bc5ad16SMarcel Holtmann 			err = -EINVAL;
17691bc5ad16SMarcel Holtmann 			goto drop;
17701bc5ad16SMarcel Holtmann 		}
17711bc5ad16SMarcel Holtmann 
17721bc5ad16SMarcel Holtmann 		skb_queue_tail(&hdev->raw_q, skb);
17731bc5ad16SMarcel Holtmann 		queue_work(hdev->workqueue, &hdev->tx_work);
1774d79f34e3SMarcel Holtmann 	} else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
177583985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
17761da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
17771da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
17803bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
17813bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
17821da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
17831da177e4SLinus Torvalds 			err = -EPERM;
17841da177e4SLinus Torvalds 			goto drop;
17851da177e4SLinus Torvalds 		}
17861da177e4SLinus Torvalds 
17871982162bSMarcel Holtmann 		/* Since the opcode has already been extracted here, store
17881982162bSMarcel Holtmann 		 * a copy of the value for later use by the drivers.
17891982162bSMarcel Holtmann 		 */
17901982162bSMarcel Holtmann 		hci_skb_opcode(skb) = opcode;
17911982162bSMarcel Holtmann 
1792fee746b0SMarcel Holtmann 		if (ogf == 0x3f) {
17931da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
17943eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
17951da177e4SLinus Torvalds 		} else {
179649c922bbSStephen Hemminger 			/* Stand-alone HCI commands must be flagged as
179711714b3dSJohan Hedberg 			 * single-command requests.
179811714b3dSJohan Hedberg 			 */
179944d27137SJohan Hedberg 			bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
180011714b3dSJohan Hedberg 
18011da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
1802c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
18031da177e4SLinus Torvalds 		}
18041da177e4SLinus Torvalds 	} else {
18051da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
18061da177e4SLinus Torvalds 			err = -EPERM;
18071da177e4SLinus Torvalds 			goto drop;
18081da177e4SLinus Torvalds 		}
18091da177e4SLinus Torvalds 
1810d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1811cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1812cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
1813bb77543eSMarcel Holtmann 			err = -EINVAL;
1814bb77543eSMarcel Holtmann 			goto drop;
1815bb77543eSMarcel Holtmann 		}
1816bb77543eSMarcel Holtmann 
18171da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
18183eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
18191da177e4SLinus Torvalds 	}
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	err = len;
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds done:
18241da177e4SLinus Torvalds 	release_sock(sk);
18251da177e4SLinus Torvalds 	return err;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds drop:
18281da177e4SLinus Torvalds 	kfree_skb(skb);
18291da177e4SLinus Torvalds 	goto done;
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds 
183209572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname,
1833a7b75c5aSChristoph Hellwig 				   sockptr_t optval, unsigned int len)
18341da177e4SLinus Torvalds {
18351da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
18361da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
18371da177e4SLinus Torvalds 	int err = 0, opt = 0;
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	lock_sock(sk);
18421da177e4SLinus Torvalds 
18432f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1844c2371e80SMarcel Holtmann 		err = -EBADFD;
18452f39cdb7SMarcel Holtmann 		goto done;
18462f39cdb7SMarcel Holtmann 	}
18472f39cdb7SMarcel Holtmann 
18481da177e4SLinus Torvalds 	switch (optname) {
18491da177e4SLinus Torvalds 	case HCI_DATA_DIR:
1850a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
18511da177e4SLinus Torvalds 			err = -EFAULT;
18521da177e4SLinus Torvalds 			break;
18531da177e4SLinus Torvalds 		}
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 		if (opt)
18561da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
18571da177e4SLinus Torvalds 		else
18581da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
18591da177e4SLinus Torvalds 		break;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
1862a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
18631da177e4SLinus Torvalds 			err = -EFAULT;
18641da177e4SLinus Torvalds 			break;
18651da177e4SLinus Torvalds 		}
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 		if (opt)
18681da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
18691da177e4SLinus Torvalds 		else
18701da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
18711da177e4SLinus Torvalds 		break;
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds 	case HCI_FILTER:
18740878b666SMarcel Holtmann 		{
18750878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
18760878b666SMarcel Holtmann 
18770878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
18780878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
18790878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
18800878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
18810878b666SMarcel Holtmann 		}
18820878b666SMarcel Holtmann 
18831da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
1884a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&uf, optval, len)) {
18851da177e4SLinus Torvalds 			err = -EFAULT;
18861da177e4SLinus Torvalds 			break;
18871da177e4SLinus Torvalds 		}
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
18901da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
18911da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
18921da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
18931da177e4SLinus Torvalds 		}
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 		{
18961da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
18991da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
19001da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
19011da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
19021da177e4SLinus Torvalds 		}
19031da177e4SLinus Torvalds 		break;
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	default:
19061da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
19071da177e4SLinus Torvalds 		break;
19081da177e4SLinus Torvalds 	}
19091da177e4SLinus Torvalds 
19102f39cdb7SMarcel Holtmann done:
19111da177e4SLinus Torvalds 	release_sock(sk);
19121da177e4SLinus Torvalds 	return err;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
191509572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
191609572fcaSLuiz Augusto von Dentz 			       sockptr_t optval, unsigned int len)
191709572fcaSLuiz Augusto von Dentz {
191809572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1919b9f9dbadSDan Carpenter 	int err = 0;
1920b9f9dbadSDan Carpenter 	u16 opt;
192109572fcaSLuiz Augusto von Dentz 
192209572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
192309572fcaSLuiz Augusto von Dentz 
192409572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
192509572fcaSLuiz Augusto von Dentz 		return hci_sock_setsockopt_old(sock, level, optname, optval,
192609572fcaSLuiz Augusto von Dentz 					       len);
192709572fcaSLuiz Augusto von Dentz 
192809572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
192909572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
193009572fcaSLuiz Augusto von Dentz 
193109572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
193209572fcaSLuiz Augusto von Dentz 
193309572fcaSLuiz Augusto von Dentz 	switch (optname) {
193409572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
193509572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
193609572fcaSLuiz Augusto von Dentz 		switch (hci_pi(sk)->channel) {
193709572fcaSLuiz Augusto von Dentz 		/* Don't allow changing MTU for channels that are meant for HCI
193809572fcaSLuiz Augusto von Dentz 		 * traffic only.
193909572fcaSLuiz Augusto von Dentz 		 */
194009572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_RAW:
194109572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_USER:
194209572fcaSLuiz Augusto von Dentz 			err = -ENOPROTOOPT;
194309572fcaSLuiz Augusto von Dentz 			goto done;
194409572fcaSLuiz Augusto von Dentz 		}
194509572fcaSLuiz Augusto von Dentz 
1946b9f9dbadSDan Carpenter 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
194709572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
194809572fcaSLuiz Augusto von Dentz 			break;
194909572fcaSLuiz Augusto von Dentz 		}
195009572fcaSLuiz Augusto von Dentz 
195109572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = opt;
195209572fcaSLuiz Augusto von Dentz 		break;
195309572fcaSLuiz Augusto von Dentz 
195409572fcaSLuiz Augusto von Dentz 	default:
195509572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
195609572fcaSLuiz Augusto von Dentz 		break;
195709572fcaSLuiz Augusto von Dentz 	}
195809572fcaSLuiz Augusto von Dentz 
195909572fcaSLuiz Augusto von Dentz done:
196009572fcaSLuiz Augusto von Dentz 	release_sock(sk);
196109572fcaSLuiz Augusto von Dentz 	return err;
196209572fcaSLuiz Augusto von Dentz }
196309572fcaSLuiz Augusto von Dentz 
196409572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
19658fc9ced3SGustavo Padovan 				   char __user *optval, int __user *optlen)
19661da177e4SLinus Torvalds {
19671da177e4SLinus Torvalds 	struct hci_ufilter uf;
19681da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1969cedc5469SMarcel Holtmann 	int len, opt, err = 0;
1970cedc5469SMarcel Holtmann 
1971cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	if (get_user(len, optlen))
19741da177e4SLinus Torvalds 		return -EFAULT;
19751da177e4SLinus Torvalds 
1976cedc5469SMarcel Holtmann 	lock_sock(sk);
1977cedc5469SMarcel Holtmann 
1978cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1979c2371e80SMarcel Holtmann 		err = -EBADFD;
1980cedc5469SMarcel Holtmann 		goto done;
1981cedc5469SMarcel Holtmann 	}
1982cedc5469SMarcel Holtmann 
19831da177e4SLinus Torvalds 	switch (optname) {
19841da177e4SLinus Torvalds 	case HCI_DATA_DIR:
19851da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
19861da177e4SLinus Torvalds 			opt = 1;
19871da177e4SLinus Torvalds 		else
19881da177e4SLinus Torvalds 			opt = 0;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 		if (put_user(opt, optval))
1991cedc5469SMarcel Holtmann 			err = -EFAULT;
19921da177e4SLinus Torvalds 		break;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
19951da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
19961da177e4SLinus Torvalds 			opt = 1;
19971da177e4SLinus Torvalds 		else
19981da177e4SLinus Torvalds 			opt = 0;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 		if (put_user(opt, optval))
2001cedc5469SMarcel Holtmann 			err = -EFAULT;
20021da177e4SLinus Torvalds 		break;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	case HCI_FILTER:
20051da177e4SLinus Torvalds 		{
20061da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
20071da177e4SLinus Torvalds 
2008e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
20091da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
20101da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
20111da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
20121da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
20131da177e4SLinus Torvalds 		}
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
20161da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
2017cedc5469SMarcel Holtmann 			err = -EFAULT;
20181da177e4SLinus Torvalds 		break;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	default:
2021cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
20221da177e4SLinus Torvalds 		break;
20231da177e4SLinus Torvalds 	}
20241da177e4SLinus Torvalds 
2025cedc5469SMarcel Holtmann done:
2026cedc5469SMarcel Holtmann 	release_sock(sk);
2027cedc5469SMarcel Holtmann 	return err;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
203009572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
203109572fcaSLuiz Augusto von Dentz 			       char __user *optval, int __user *optlen)
203209572fcaSLuiz Augusto von Dentz {
203309572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
203409572fcaSLuiz Augusto von Dentz 	int err = 0;
203509572fcaSLuiz Augusto von Dentz 
203609572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
203709572fcaSLuiz Augusto von Dentz 
203809572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
203909572fcaSLuiz Augusto von Dentz 		return hci_sock_getsockopt_old(sock, level, optname, optval,
204009572fcaSLuiz Augusto von Dentz 					       optlen);
204109572fcaSLuiz Augusto von Dentz 
204209572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
204309572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
204409572fcaSLuiz Augusto von Dentz 
204509572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
204609572fcaSLuiz Augusto von Dentz 
204709572fcaSLuiz Augusto von Dentz 	switch (optname) {
204809572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
204909572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
205009572fcaSLuiz Augusto von Dentz 		if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval))
205109572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
205209572fcaSLuiz Augusto von Dentz 		break;
205309572fcaSLuiz Augusto von Dentz 
205409572fcaSLuiz Augusto von Dentz 	default:
205509572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
205609572fcaSLuiz Augusto von Dentz 		break;
205709572fcaSLuiz Augusto von Dentz 	}
205809572fcaSLuiz Augusto von Dentz 
205909572fcaSLuiz Augusto von Dentz 	release_sock(sk);
206009572fcaSLuiz Augusto von Dentz 	return err;
206109572fcaSLuiz Augusto von Dentz }
206209572fcaSLuiz Augusto von Dentz 
2063709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk)
2064709fca50SNguyen Dinh Phi {
2065709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_receive_queue);
2066709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_write_queue);
2067709fca50SNguyen Dinh Phi }
2068709fca50SNguyen Dinh Phi 
206990ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
20701da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
20711da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
20721da177e4SLinus Torvalds 	.release	= hci_sock_release,
20731da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
20741da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
20751da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
20761da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
20771da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
20787a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
20797a6038b3SArnd Bergmann 	.compat_ioctl	= hci_sock_compat_ioctl,
20807a6038b3SArnd Bergmann #endif
2081a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
20821da177e4SLinus Torvalds 	.listen		= sock_no_listen,
20831da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
20841da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
20851da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
20861da177e4SLinus Torvalds 	.connect	= sock_no_connect,
20871da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
20881da177e4SLinus Torvalds 	.accept		= sock_no_accept,
20891da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
20901da177e4SLinus Torvalds };
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds static struct proto hci_sk_proto = {
20931da177e4SLinus Torvalds 	.name		= "HCI",
20941da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
20951da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
20961da177e4SLinus Torvalds };
20971da177e4SLinus Torvalds 
20983f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
20993f378b68SEric Paris 			   int kern)
21001da177e4SLinus Torvalds {
21011da177e4SLinus Torvalds 	struct sock *sk;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
21061da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
21091da177e4SLinus Torvalds 
211011aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto, kern);
21111da177e4SLinus Torvalds 	if (!sk)
21121da177e4SLinus Torvalds 		return -ENOMEM;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds 	sock_init_data(sock, sk);
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
21211da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
2122709fca50SNguyen Dinh Phi 	sk->sk_destruct = hci_sock_destruct;
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
21251da177e4SLinus Torvalds 	return 0;
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
2128ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
21291da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
21301da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
21311da177e4SLinus Torvalds 	.create	= hci_sock_create,
21321da177e4SLinus Torvalds };
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds int __init hci_sock_init(void)
21351da177e4SLinus Torvalds {
21361da177e4SLinus Torvalds 	int err;
21371da177e4SLinus Torvalds 
2138b0a8e282SMarcel Holtmann 	BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2139b0a8e282SMarcel Holtmann 
21401da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
21411da177e4SLinus Torvalds 	if (err < 0)
21421da177e4SLinus Torvalds 		return err;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
2145f7c86637SMasatake YAMATO 	if (err < 0) {
2146f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
21471da177e4SLinus Torvalds 		goto error;
2148f7c86637SMasatake YAMATO 	}
2149f7c86637SMasatake YAMATO 
2150b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
2151f7c86637SMasatake YAMATO 	if (err < 0) {
2152f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
2153f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
2154f7c86637SMasatake YAMATO 		goto error;
2155f7c86637SMasatake YAMATO 	}
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	return 0;
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds error:
21621da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
21631da177e4SLinus Torvalds 	return err;
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
2166b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
21671da177e4SLinus Torvalds {
2168f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
21695e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
21701da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
21711da177e4SLinus Torvalds }
2172