xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision 69ae5065061c53ded4f49e821646b9bc60ab302a)
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 
267*69ae5065SLuiz Augusto von Dentz static void hci_sock_copy_creds(struct sock *sk, struct sk_buff *skb)
268*69ae5065SLuiz Augusto von Dentz {
269*69ae5065SLuiz Augusto von Dentz 	struct scm_creds *creds;
270*69ae5065SLuiz Augusto von Dentz 
271*69ae5065SLuiz Augusto von Dentz 	if (!sk || WARN_ON(!skb))
272*69ae5065SLuiz Augusto von Dentz 		return;
273*69ae5065SLuiz Augusto von Dentz 
274*69ae5065SLuiz Augusto von Dentz 	creds = &bt_cb(skb)->creds;
275*69ae5065SLuiz Augusto von Dentz 
276*69ae5065SLuiz Augusto von Dentz 	/* Check if peer credentials is set */
277*69ae5065SLuiz Augusto von Dentz 	if (!sk->sk_peer_pid) {
278*69ae5065SLuiz Augusto von Dentz 		/* Check if parent peer credentials is set */
279*69ae5065SLuiz Augusto von Dentz 		if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid)
280*69ae5065SLuiz Augusto von Dentz 			sk = bt_sk(sk)->parent;
281*69ae5065SLuiz Augusto von Dentz 		else
282*69ae5065SLuiz Augusto von Dentz 			return;
283*69ae5065SLuiz Augusto von Dentz 	}
284*69ae5065SLuiz Augusto von Dentz 
285*69ae5065SLuiz Augusto von Dentz 	/* Check if scm_creds already set */
286*69ae5065SLuiz Augusto von Dentz 	if (creds->pid == pid_vnr(sk->sk_peer_pid))
287*69ae5065SLuiz Augusto von Dentz 		return;
288*69ae5065SLuiz Augusto von Dentz 
289*69ae5065SLuiz Augusto von Dentz 	memset(creds, 0, sizeof(*creds));
290*69ae5065SLuiz Augusto von Dentz 
291*69ae5065SLuiz Augusto von Dentz 	creds->pid = pid_vnr(sk->sk_peer_pid);
292*69ae5065SLuiz Augusto von Dentz 	if (sk->sk_peer_cred) {
293*69ae5065SLuiz Augusto von Dentz 		creds->uid = sk->sk_peer_cred->uid;
294*69ae5065SLuiz Augusto von Dentz 		creds->gid = sk->sk_peer_cred->gid;
295*69ae5065SLuiz Augusto von Dentz 	}
296*69ae5065SLuiz Augusto von Dentz }
297*69ae5065SLuiz Augusto von Dentz 
298*69ae5065SLuiz Augusto von Dentz static struct sk_buff *hci_skb_clone(struct sk_buff *skb)
299*69ae5065SLuiz Augusto von Dentz {
300*69ae5065SLuiz Augusto von Dentz 	struct sk_buff *nskb;
301*69ae5065SLuiz Augusto von Dentz 
302*69ae5065SLuiz Augusto von Dentz 	if (!skb)
303*69ae5065SLuiz Augusto von Dentz 		return NULL;
304*69ae5065SLuiz Augusto von Dentz 
305*69ae5065SLuiz Augusto von Dentz 	nskb = skb_clone(skb, GFP_ATOMIC);
306*69ae5065SLuiz Augusto von Dentz 	if (!nskb)
307*69ae5065SLuiz Augusto von Dentz 		return NULL;
308*69ae5065SLuiz Augusto von Dentz 
309*69ae5065SLuiz Augusto von Dentz 	hci_sock_copy_creds(skb->sk, nskb);
310*69ae5065SLuiz Augusto von Dentz 
311*69ae5065SLuiz Augusto von Dentz 	return nskb;
312*69ae5065SLuiz Augusto von Dentz }
313*69ae5065SLuiz Augusto von Dentz 
3147129069eSJohan Hedberg /* Send frame to sockets with specific channel */
315a9ee77afSSebastian Andrzej Siewior static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
316c08b1a1dSMarcel Holtmann 				  int flag, struct sock *skip_sk)
317470fe1b5SMarcel Holtmann {
318470fe1b5SMarcel Holtmann 	struct sock *sk;
319470fe1b5SMarcel Holtmann 
3207129069eSJohan Hedberg 	BT_DBG("channel %u len %d", channel, skb->len);
321470fe1b5SMarcel Holtmann 
322b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
323470fe1b5SMarcel Holtmann 		struct sk_buff *nskb;
324470fe1b5SMarcel Holtmann 
325c08b1a1dSMarcel Holtmann 		/* Ignore socket without the flag set */
326c85be545SMarcel Holtmann 		if (!hci_sock_test_flag(sk, flag))
327c08b1a1dSMarcel Holtmann 			continue;
328c08b1a1dSMarcel Holtmann 
329470fe1b5SMarcel Holtmann 		/* Skip the original socket */
330470fe1b5SMarcel Holtmann 		if (sk == skip_sk)
331470fe1b5SMarcel Holtmann 			continue;
332470fe1b5SMarcel Holtmann 
333470fe1b5SMarcel Holtmann 		if (sk->sk_state != BT_BOUND)
334470fe1b5SMarcel Holtmann 			continue;
335470fe1b5SMarcel Holtmann 
3367129069eSJohan Hedberg 		if (hci_pi(sk)->channel != channel)
337d7f72f61SMarcel Holtmann 			continue;
338d7f72f61SMarcel Holtmann 
339*69ae5065SLuiz Augusto von Dentz 		nskb = hci_skb_clone(skb);
340d7f72f61SMarcel Holtmann 		if (!nskb)
341d7f72f61SMarcel Holtmann 			continue;
342d7f72f61SMarcel Holtmann 
343d7f72f61SMarcel Holtmann 		if (sock_queue_rcv_skb(sk, nskb))
344d7f72f61SMarcel Holtmann 			kfree_skb(nskb);
345d7f72f61SMarcel Holtmann 	}
346d7f72f61SMarcel Holtmann 
347a9ee77afSSebastian Andrzej Siewior }
348a9ee77afSSebastian Andrzej Siewior 
349a9ee77afSSebastian Andrzej Siewior void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
350a9ee77afSSebastian Andrzej Siewior 			 int flag, struct sock *skip_sk)
351a9ee77afSSebastian Andrzej Siewior {
352a9ee77afSSebastian Andrzej Siewior 	read_lock(&hci_sk_list.lock);
353a9ee77afSSebastian Andrzej Siewior 	__hci_send_to_channel(channel, skb, flag, skip_sk);
354d7f72f61SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
355d7f72f61SMarcel Holtmann }
356d7f72f61SMarcel Holtmann 
357cd82e61cSMarcel Holtmann /* Send frame to monitor socket */
358cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
359cd82e61cSMarcel Holtmann {
360cd82e61cSMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
3612b531294SMarcel Holtmann 	struct hci_mon_hdr *hdr;
362cd82e61cSMarcel Holtmann 	__le16 opcode;
363cd82e61cSMarcel Holtmann 
364cd82e61cSMarcel Holtmann 	if (!atomic_read(&monitor_promisc))
365cd82e61cSMarcel Holtmann 		return;
366cd82e61cSMarcel Holtmann 
367cd82e61cSMarcel Holtmann 	BT_DBG("hdev %p len %d", hdev, skb->len);
368cd82e61cSMarcel Holtmann 
369d79f34e3SMarcel Holtmann 	switch (hci_skb_pkt_type(skb)) {
370cd82e61cSMarcel Holtmann 	case HCI_COMMAND_PKT:
371dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
372cd82e61cSMarcel Holtmann 		break;
373cd82e61cSMarcel Holtmann 	case HCI_EVENT_PKT:
374dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
375cd82e61cSMarcel Holtmann 		break;
376cd82e61cSMarcel Holtmann 	case HCI_ACLDATA_PKT:
377cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
378dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
379cd82e61cSMarcel Holtmann 		else
380dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
381cd82e61cSMarcel Holtmann 		break;
382cd82e61cSMarcel Holtmann 	case HCI_SCODATA_PKT:
383cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
384dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
385cd82e61cSMarcel Holtmann 		else
386dcf4adbfSJoe Perches 			opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
387cd82e61cSMarcel Holtmann 		break;
388f9a619dbSLuiz Augusto von Dentz 	case HCI_ISODATA_PKT:
389f9a619dbSLuiz Augusto von Dentz 		if (bt_cb(skb)->incoming)
390f9a619dbSLuiz Augusto von Dentz 			opcode = cpu_to_le16(HCI_MON_ISO_RX_PKT);
391f9a619dbSLuiz Augusto von Dentz 		else
392f9a619dbSLuiz Augusto von Dentz 			opcode = cpu_to_le16(HCI_MON_ISO_TX_PKT);
393f9a619dbSLuiz Augusto von Dentz 		break;
394e875ff84SMarcel Holtmann 	case HCI_DIAG_PKT:
395e875ff84SMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG);
396e875ff84SMarcel Holtmann 		break;
397cd82e61cSMarcel Holtmann 	default:
398cd82e61cSMarcel Holtmann 		return;
399cd82e61cSMarcel Holtmann 	}
400cd82e61cSMarcel Holtmann 
4012b531294SMarcel Holtmann 	/* Create a private copy with headroom */
4022b531294SMarcel Holtmann 	skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
4032b531294SMarcel Holtmann 	if (!skb_copy)
4042b531294SMarcel Holtmann 		return;
4052b531294SMarcel Holtmann 
406*69ae5065SLuiz Augusto von Dentz 	hci_sock_copy_creds(skb->sk, skb_copy);
407*69ae5065SLuiz Augusto von Dentz 
4082b531294SMarcel Holtmann 	/* Put header before the data */
409d58ff351SJohannes Berg 	hdr = skb_push(skb_copy, HCI_MON_HDR_SIZE);
4102b531294SMarcel Holtmann 	hdr->opcode = opcode;
4112b531294SMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
4122b531294SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len);
4132b531294SMarcel Holtmann 
414c08b1a1dSMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
415c08b1a1dSMarcel Holtmann 			    HCI_SOCK_TRUSTED, NULL);
416cd82e61cSMarcel Holtmann 	kfree_skb(skb_copy);
417cd82e61cSMarcel Holtmann }
418cd82e61cSMarcel Holtmann 
41938ceaa00SMarcel Holtmann void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
42038ceaa00SMarcel Holtmann 				 void *data, u16 data_len, ktime_t tstamp,
42138ceaa00SMarcel Holtmann 				 int flag, struct sock *skip_sk)
42238ceaa00SMarcel Holtmann {
42338ceaa00SMarcel Holtmann 	struct sock *sk;
42438ceaa00SMarcel Holtmann 	__le16 index;
42538ceaa00SMarcel Holtmann 
42638ceaa00SMarcel Holtmann 	if (hdev)
42738ceaa00SMarcel Holtmann 		index = cpu_to_le16(hdev->id);
42838ceaa00SMarcel Holtmann 	else
42938ceaa00SMarcel Holtmann 		index = cpu_to_le16(MGMT_INDEX_NONE);
43038ceaa00SMarcel Holtmann 
43138ceaa00SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
43238ceaa00SMarcel Holtmann 
43338ceaa00SMarcel Holtmann 	sk_for_each(sk, &hci_sk_list.head) {
43438ceaa00SMarcel Holtmann 		struct hci_mon_hdr *hdr;
43538ceaa00SMarcel Holtmann 		struct sk_buff *skb;
43638ceaa00SMarcel Holtmann 
43738ceaa00SMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
43838ceaa00SMarcel Holtmann 			continue;
43938ceaa00SMarcel Holtmann 
44038ceaa00SMarcel Holtmann 		/* Ignore socket without the flag set */
44138ceaa00SMarcel Holtmann 		if (!hci_sock_test_flag(sk, flag))
44238ceaa00SMarcel Holtmann 			continue;
44338ceaa00SMarcel Holtmann 
44438ceaa00SMarcel Holtmann 		/* Skip the original socket */
44538ceaa00SMarcel Holtmann 		if (sk == skip_sk)
44638ceaa00SMarcel Holtmann 			continue;
44738ceaa00SMarcel Holtmann 
44838ceaa00SMarcel Holtmann 		skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC);
44938ceaa00SMarcel Holtmann 		if (!skb)
45038ceaa00SMarcel Holtmann 			continue;
45138ceaa00SMarcel Holtmann 
45238ceaa00SMarcel Holtmann 		put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
45338ceaa00SMarcel Holtmann 		put_unaligned_le16(event, skb_put(skb, 2));
45438ceaa00SMarcel Holtmann 
45538ceaa00SMarcel Holtmann 		if (data)
45659ae1d12SJohannes Berg 			skb_put_data(skb, data, data_len);
45738ceaa00SMarcel Holtmann 
45838ceaa00SMarcel Holtmann 		skb->tstamp = tstamp;
45938ceaa00SMarcel Holtmann 
460d58ff351SJohannes Berg 		hdr = skb_push(skb, HCI_MON_HDR_SIZE);
46138ceaa00SMarcel Holtmann 		hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
46238ceaa00SMarcel Holtmann 		hdr->index = index;
46338ceaa00SMarcel Holtmann 		hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
46438ceaa00SMarcel Holtmann 
465a9ee77afSSebastian Andrzej Siewior 		__hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
46638ceaa00SMarcel Holtmann 				      HCI_SOCK_TRUSTED, NULL);
46738ceaa00SMarcel Holtmann 		kfree_skb(skb);
46838ceaa00SMarcel Holtmann 	}
46938ceaa00SMarcel Holtmann 
47038ceaa00SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
47138ceaa00SMarcel Holtmann }
47238ceaa00SMarcel Holtmann 
473cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
474cd82e61cSMarcel Holtmann {
475cd82e61cSMarcel Holtmann 	struct hci_mon_hdr *hdr;
476cd82e61cSMarcel Holtmann 	struct hci_mon_new_index *ni;
4776c566dd5SMarcel Holtmann 	struct hci_mon_index_info *ii;
478cd82e61cSMarcel Holtmann 	struct sk_buff *skb;
479cd82e61cSMarcel Holtmann 	__le16 opcode;
480cd82e61cSMarcel Holtmann 
481cd82e61cSMarcel Holtmann 	switch (event) {
482cd82e61cSMarcel Holtmann 	case HCI_DEV_REG:
483cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
484cd82e61cSMarcel Holtmann 		if (!skb)
485cd82e61cSMarcel Holtmann 			return NULL;
486cd82e61cSMarcel Holtmann 
4874df864c1SJohannes Berg 		ni = skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
488cd82e61cSMarcel Holtmann 		ni->type = hdev->dev_type;
489cd82e61cSMarcel Holtmann 		ni->bus = hdev->bus;
490cd82e61cSMarcel Holtmann 		bacpy(&ni->bdaddr, &hdev->bdaddr);
491cd82e61cSMarcel Holtmann 		memcpy(ni->name, hdev->name, 8);
492cd82e61cSMarcel Holtmann 
493dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
494cd82e61cSMarcel Holtmann 		break;
495cd82e61cSMarcel Holtmann 
496cd82e61cSMarcel Holtmann 	case HCI_DEV_UNREG:
497cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
498cd82e61cSMarcel Holtmann 		if (!skb)
499cd82e61cSMarcel Holtmann 			return NULL;
500cd82e61cSMarcel Holtmann 
501dcf4adbfSJoe Perches 		opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
502cd82e61cSMarcel Holtmann 		break;
503cd82e61cSMarcel Holtmann 
504e131d74aSMarcel Holtmann 	case HCI_DEV_SETUP:
505e131d74aSMarcel Holtmann 		if (hdev->manufacturer == 0xffff)
506e131d74aSMarcel Holtmann 			return NULL;
50719186c7bSGustavo A. R. Silva 		fallthrough;
508e131d74aSMarcel Holtmann 
5096c566dd5SMarcel Holtmann 	case HCI_DEV_UP:
5106c566dd5SMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC);
5116c566dd5SMarcel Holtmann 		if (!skb)
5126c566dd5SMarcel Holtmann 			return NULL;
5136c566dd5SMarcel Holtmann 
5144df864c1SJohannes Berg 		ii = skb_put(skb, HCI_MON_INDEX_INFO_SIZE);
5156c566dd5SMarcel Holtmann 		bacpy(&ii->bdaddr, &hdev->bdaddr);
5166c566dd5SMarcel Holtmann 		ii->manufacturer = cpu_to_le16(hdev->manufacturer);
5176c566dd5SMarcel Holtmann 
5186c566dd5SMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_INDEX_INFO);
5196c566dd5SMarcel Holtmann 		break;
5206c566dd5SMarcel Holtmann 
52122db3cbcSMarcel Holtmann 	case HCI_DEV_OPEN:
52222db3cbcSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
52322db3cbcSMarcel Holtmann 		if (!skb)
52422db3cbcSMarcel Holtmann 			return NULL;
52522db3cbcSMarcel Holtmann 
52622db3cbcSMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_OPEN_INDEX);
52722db3cbcSMarcel Holtmann 		break;
52822db3cbcSMarcel Holtmann 
52922db3cbcSMarcel Holtmann 	case HCI_DEV_CLOSE:
53022db3cbcSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
53122db3cbcSMarcel Holtmann 		if (!skb)
53222db3cbcSMarcel Holtmann 			return NULL;
53322db3cbcSMarcel Holtmann 
53422db3cbcSMarcel Holtmann 		opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX);
53522db3cbcSMarcel Holtmann 		break;
53622db3cbcSMarcel Holtmann 
537cd82e61cSMarcel Holtmann 	default:
538cd82e61cSMarcel Holtmann 		return NULL;
539cd82e61cSMarcel Holtmann 	}
540cd82e61cSMarcel Holtmann 
541cd82e61cSMarcel Holtmann 	__net_timestamp(skb);
542cd82e61cSMarcel Holtmann 
543d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
544cd82e61cSMarcel Holtmann 	hdr->opcode = opcode;
545cd82e61cSMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
546cd82e61cSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
547cd82e61cSMarcel Holtmann 
548cd82e61cSMarcel Holtmann 	return skb;
549cd82e61cSMarcel Holtmann }
550cd82e61cSMarcel Holtmann 
551249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
552249fa169SMarcel Holtmann {
553249fa169SMarcel Holtmann 	struct hci_mon_hdr *hdr;
554249fa169SMarcel Holtmann 	struct sk_buff *skb;
555d0bef1d2SMarcel Holtmann 	u16 format;
556249fa169SMarcel Holtmann 	u8 ver[3];
557249fa169SMarcel Holtmann 	u32 flags;
558249fa169SMarcel Holtmann 
5590ef2c42fSMarcel Holtmann 	/* No message needed when cookie is not present */
5600ef2c42fSMarcel Holtmann 	if (!hci_pi(sk)->cookie)
5610ef2c42fSMarcel Holtmann 		return NULL;
5620ef2c42fSMarcel Holtmann 
563d0bef1d2SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
564f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
565f81f5b2dSMarcel Holtmann 		format = 0x0000;
566f81f5b2dSMarcel Holtmann 		ver[0] = BT_SUBSYS_VERSION;
567f81f5b2dSMarcel Holtmann 		put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
568f81f5b2dSMarcel Holtmann 		break;
569aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
570aa1638ddSMarcel Holtmann 		format = 0x0001;
571aa1638ddSMarcel Holtmann 		ver[0] = BT_SUBSYS_VERSION;
572aa1638ddSMarcel Holtmann 		put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
573aa1638ddSMarcel Holtmann 		break;
574d0bef1d2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
575d0bef1d2SMarcel Holtmann 		format = 0x0002;
576d0bef1d2SMarcel Holtmann 		mgmt_fill_version_info(ver);
577d0bef1d2SMarcel Holtmann 		break;
578d0bef1d2SMarcel Holtmann 	default:
579d0bef1d2SMarcel Holtmann 		/* No message for unsupported format */
580d0bef1d2SMarcel Holtmann 		return NULL;
581d0bef1d2SMarcel Holtmann 	}
582d0bef1d2SMarcel Holtmann 
583249fa169SMarcel Holtmann 	skb = bt_skb_alloc(14 + TASK_COMM_LEN, GFP_ATOMIC);
584249fa169SMarcel Holtmann 	if (!skb)
585249fa169SMarcel Holtmann 		return NULL;
586249fa169SMarcel Holtmann 
587*69ae5065SLuiz Augusto von Dentz 	hci_sock_copy_creds(sk, skb);
588*69ae5065SLuiz Augusto von Dentz 
589249fa169SMarcel Holtmann 	flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
590249fa169SMarcel Holtmann 
591249fa169SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
592249fa169SMarcel Holtmann 	put_unaligned_le16(format, skb_put(skb, 2));
59359ae1d12SJohannes Berg 	skb_put_data(skb, ver, sizeof(ver));
594249fa169SMarcel Holtmann 	put_unaligned_le32(flags, skb_put(skb, 4));
595634fef61SJohannes Berg 	skb_put_u8(skb, TASK_COMM_LEN);
59659ae1d12SJohannes Berg 	skb_put_data(skb, hci_pi(sk)->comm, TASK_COMM_LEN);
597249fa169SMarcel Holtmann 
598249fa169SMarcel Holtmann 	__net_timestamp(skb);
599249fa169SMarcel Holtmann 
600d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
601249fa169SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
6020ef2c42fSMarcel Holtmann 	if (hci_pi(sk)->hdev)
6030ef2c42fSMarcel Holtmann 		hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
6040ef2c42fSMarcel Holtmann 	else
605249fa169SMarcel Holtmann 		hdr->index = cpu_to_le16(HCI_DEV_NONE);
606249fa169SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
607249fa169SMarcel Holtmann 
608249fa169SMarcel Holtmann 	return skb;
609249fa169SMarcel Holtmann }
610249fa169SMarcel Holtmann 
611249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
612249fa169SMarcel Holtmann {
613249fa169SMarcel Holtmann 	struct hci_mon_hdr *hdr;
614249fa169SMarcel Holtmann 	struct sk_buff *skb;
615249fa169SMarcel Holtmann 
6160ef2c42fSMarcel Holtmann 	/* No message needed when cookie is not present */
6170ef2c42fSMarcel Holtmann 	if (!hci_pi(sk)->cookie)
6180ef2c42fSMarcel Holtmann 		return NULL;
6190ef2c42fSMarcel Holtmann 
620d0bef1d2SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
621f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
622aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
623d0bef1d2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
624d0bef1d2SMarcel Holtmann 		break;
625d0bef1d2SMarcel Holtmann 	default:
626d0bef1d2SMarcel Holtmann 		/* No message for unsupported format */
627d0bef1d2SMarcel Holtmann 		return NULL;
628d0bef1d2SMarcel Holtmann 	}
629d0bef1d2SMarcel Holtmann 
630249fa169SMarcel Holtmann 	skb = bt_skb_alloc(4, GFP_ATOMIC);
631249fa169SMarcel Holtmann 	if (!skb)
632249fa169SMarcel Holtmann 		return NULL;
633249fa169SMarcel Holtmann 
634*69ae5065SLuiz Augusto von Dentz 	hci_sock_copy_creds(sk, skb);
635*69ae5065SLuiz Augusto von Dentz 
636249fa169SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
637249fa169SMarcel Holtmann 
638249fa169SMarcel Holtmann 	__net_timestamp(skb);
639249fa169SMarcel Holtmann 
640d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
641249fa169SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
6420ef2c42fSMarcel Holtmann 	if (hci_pi(sk)->hdev)
6430ef2c42fSMarcel Holtmann 		hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
6440ef2c42fSMarcel Holtmann 	else
645249fa169SMarcel Holtmann 		hdr->index = cpu_to_le16(HCI_DEV_NONE);
646249fa169SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
647249fa169SMarcel Holtmann 
648249fa169SMarcel Holtmann 	return skb;
649249fa169SMarcel Holtmann }
650249fa169SMarcel Holtmann 
65138ceaa00SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index,
65238ceaa00SMarcel Holtmann 						   u16 opcode, u16 len,
65338ceaa00SMarcel Holtmann 						   const void *buf)
65438ceaa00SMarcel Holtmann {
65538ceaa00SMarcel Holtmann 	struct hci_mon_hdr *hdr;
65638ceaa00SMarcel Holtmann 	struct sk_buff *skb;
65738ceaa00SMarcel Holtmann 
65838ceaa00SMarcel Holtmann 	skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
65938ceaa00SMarcel Holtmann 	if (!skb)
66038ceaa00SMarcel Holtmann 		return NULL;
66138ceaa00SMarcel Holtmann 
662*69ae5065SLuiz Augusto von Dentz 	hci_sock_copy_creds(sk, skb);
663*69ae5065SLuiz Augusto von Dentz 
66438ceaa00SMarcel Holtmann 	put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
66538ceaa00SMarcel Holtmann 	put_unaligned_le16(opcode, skb_put(skb, 2));
66638ceaa00SMarcel Holtmann 
66738ceaa00SMarcel Holtmann 	if (buf)
66859ae1d12SJohannes Berg 		skb_put_data(skb, buf, len);
66938ceaa00SMarcel Holtmann 
67038ceaa00SMarcel Holtmann 	__net_timestamp(skb);
67138ceaa00SMarcel Holtmann 
672d58ff351SJohannes Berg 	hdr = skb_push(skb, HCI_MON_HDR_SIZE);
67338ceaa00SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND);
67438ceaa00SMarcel Holtmann 	hdr->index = cpu_to_le16(index);
67538ceaa00SMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
67638ceaa00SMarcel Holtmann 
67738ceaa00SMarcel Holtmann 	return skb;
67838ceaa00SMarcel Holtmann }
67938ceaa00SMarcel Holtmann 
680787b306cSJohannes Berg static void __printf(2, 3)
681787b306cSJohannes Berg send_monitor_note(struct sock *sk, const char *fmt, ...)
682dd31506dSMarcel Holtmann {
683787b306cSJohannes Berg 	size_t len;
684dd31506dSMarcel Holtmann 	struct hci_mon_hdr *hdr;
685dd31506dSMarcel Holtmann 	struct sk_buff *skb;
686787b306cSJohannes Berg 	va_list args;
687787b306cSJohannes Berg 
688787b306cSJohannes Berg 	va_start(args, fmt);
689787b306cSJohannes Berg 	len = vsnprintf(NULL, 0, fmt, args);
690787b306cSJohannes Berg 	va_end(args);
691dd31506dSMarcel Holtmann 
692dd31506dSMarcel Holtmann 	skb = bt_skb_alloc(len + 1, GFP_ATOMIC);
693dd31506dSMarcel Holtmann 	if (!skb)
694dd31506dSMarcel Holtmann 		return;
695dd31506dSMarcel Holtmann 
696*69ae5065SLuiz Augusto von Dentz 	hci_sock_copy_creds(sk, skb);
697*69ae5065SLuiz Augusto von Dentz 
698787b306cSJohannes Berg 	va_start(args, fmt);
699787b306cSJohannes Berg 	vsprintf(skb_put(skb, len), fmt, args);
7004df864c1SJohannes Berg 	*(u8 *)skb_put(skb, 1) = 0;
701787b306cSJohannes Berg 	va_end(args);
702dd31506dSMarcel Holtmann 
703dd31506dSMarcel Holtmann 	__net_timestamp(skb);
704dd31506dSMarcel Holtmann 
705dd31506dSMarcel Holtmann 	hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
706dd31506dSMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE);
707dd31506dSMarcel Holtmann 	hdr->index = cpu_to_le16(HCI_DEV_NONE);
708dd31506dSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
709dd31506dSMarcel Holtmann 
710dd31506dSMarcel Holtmann 	if (sock_queue_rcv_skb(sk, skb))
711dd31506dSMarcel Holtmann 		kfree_skb(skb);
712dd31506dSMarcel Holtmann }
713dd31506dSMarcel Holtmann 
714cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk)
715cd82e61cSMarcel Holtmann {
716cd82e61cSMarcel Holtmann 	struct hci_dev *hdev;
717cd82e61cSMarcel Holtmann 
718cd82e61cSMarcel Holtmann 	read_lock(&hci_dev_list_lock);
719cd82e61cSMarcel Holtmann 
720cd82e61cSMarcel Holtmann 	list_for_each_entry(hdev, &hci_dev_list, list) {
721cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
722cd82e61cSMarcel Holtmann 
723cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_REG);
724cd82e61cSMarcel Holtmann 		if (!skb)
725cd82e61cSMarcel Holtmann 			continue;
726cd82e61cSMarcel Holtmann 
727cd82e61cSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
728cd82e61cSMarcel Holtmann 			kfree_skb(skb);
72922db3cbcSMarcel Holtmann 
73022db3cbcSMarcel Holtmann 		if (!test_bit(HCI_RUNNING, &hdev->flags))
73122db3cbcSMarcel Holtmann 			continue;
73222db3cbcSMarcel Holtmann 
73322db3cbcSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_OPEN);
73422db3cbcSMarcel Holtmann 		if (!skb)
73522db3cbcSMarcel Holtmann 			continue;
73622db3cbcSMarcel Holtmann 
73722db3cbcSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
73822db3cbcSMarcel Holtmann 			kfree_skb(skb);
7396c566dd5SMarcel Holtmann 
740e131d74aSMarcel Holtmann 		if (test_bit(HCI_UP, &hdev->flags))
7416c566dd5SMarcel Holtmann 			skb = create_monitor_event(hdev, HCI_DEV_UP);
742e131d74aSMarcel Holtmann 		else if (hci_dev_test_flag(hdev, HCI_SETUP))
743e131d74aSMarcel Holtmann 			skb = create_monitor_event(hdev, HCI_DEV_SETUP);
744e131d74aSMarcel Holtmann 		else
745e131d74aSMarcel Holtmann 			skb = NULL;
7466c566dd5SMarcel Holtmann 
747e131d74aSMarcel Holtmann 		if (skb) {
7486c566dd5SMarcel Holtmann 			if (sock_queue_rcv_skb(sk, skb))
7496c566dd5SMarcel Holtmann 				kfree_skb(skb);
750cd82e61cSMarcel Holtmann 		}
751e131d74aSMarcel Holtmann 	}
752cd82e61cSMarcel Holtmann 
753cd82e61cSMarcel Holtmann 	read_unlock(&hci_dev_list_lock);
754cd82e61cSMarcel Holtmann }
755cd82e61cSMarcel Holtmann 
756249fa169SMarcel Holtmann static void send_monitor_control_replay(struct sock *mon_sk)
757249fa169SMarcel Holtmann {
758249fa169SMarcel Holtmann 	struct sock *sk;
759249fa169SMarcel Holtmann 
760249fa169SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
761249fa169SMarcel Holtmann 
762249fa169SMarcel Holtmann 	sk_for_each(sk, &hci_sk_list.head) {
763249fa169SMarcel Holtmann 		struct sk_buff *skb;
764249fa169SMarcel Holtmann 
765249fa169SMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
766249fa169SMarcel Holtmann 		if (!skb)
767249fa169SMarcel Holtmann 			continue;
768249fa169SMarcel Holtmann 
769249fa169SMarcel Holtmann 		if (sock_queue_rcv_skb(mon_sk, skb))
770249fa169SMarcel Holtmann 			kfree_skb(skb);
771249fa169SMarcel Holtmann 	}
772249fa169SMarcel Holtmann 
773249fa169SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
774249fa169SMarcel Holtmann }
775249fa169SMarcel Holtmann 
776040030efSMarcel Holtmann /* Generate internal stack event */
777040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
778040030efSMarcel Holtmann {
779040030efSMarcel Holtmann 	struct hci_event_hdr *hdr;
780040030efSMarcel Holtmann 	struct hci_ev_stack_internal *ev;
781040030efSMarcel Holtmann 	struct sk_buff *skb;
782040030efSMarcel Holtmann 
783040030efSMarcel Holtmann 	skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
784040030efSMarcel Holtmann 	if (!skb)
785040030efSMarcel Holtmann 		return;
786040030efSMarcel Holtmann 
7874df864c1SJohannes Berg 	hdr = skb_put(skb, HCI_EVENT_HDR_SIZE);
788040030efSMarcel Holtmann 	hdr->evt  = HCI_EV_STACK_INTERNAL;
789040030efSMarcel Holtmann 	hdr->plen = sizeof(*ev) + dlen;
790040030efSMarcel Holtmann 
7914df864c1SJohannes Berg 	ev = skb_put(skb, sizeof(*ev) + dlen);
792040030efSMarcel Holtmann 	ev->type = type;
793040030efSMarcel Holtmann 	memcpy(ev->data, data, dlen);
794040030efSMarcel Holtmann 
795040030efSMarcel Holtmann 	bt_cb(skb)->incoming = 1;
796040030efSMarcel Holtmann 	__net_timestamp(skb);
797040030efSMarcel Holtmann 
798d79f34e3SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
799040030efSMarcel Holtmann 	hci_send_to_sock(hdev, skb);
800040030efSMarcel Holtmann 	kfree_skb(skb);
801040030efSMarcel Holtmann }
802040030efSMarcel Holtmann 
803040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event)
804040030efSMarcel Holtmann {
805040030efSMarcel Holtmann 	BT_DBG("hdev %s event %d", hdev->name, event);
806040030efSMarcel Holtmann 
807cd82e61cSMarcel Holtmann 	if (atomic_read(&monitor_promisc)) {
808cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
809cd82e61cSMarcel Holtmann 
810ed1b28a4SMarcel Holtmann 		/* Send event to monitor */
811cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, event);
812cd82e61cSMarcel Holtmann 		if (skb) {
813c08b1a1dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
814c08b1a1dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
815cd82e61cSMarcel Holtmann 			kfree_skb(skb);
816cd82e61cSMarcel Holtmann 		}
817cd82e61cSMarcel Holtmann 	}
818cd82e61cSMarcel Holtmann 
819ed1b28a4SMarcel Holtmann 	if (event <= HCI_DEV_DOWN) {
820ed1b28a4SMarcel Holtmann 		struct hci_ev_si_device ev;
821ed1b28a4SMarcel Holtmann 
822040030efSMarcel Holtmann 		/* Send event to sockets */
823040030efSMarcel Holtmann 		ev.event  = event;
824040030efSMarcel Holtmann 		ev.dev_id = hdev->id;
825040030efSMarcel Holtmann 		hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
826ed1b28a4SMarcel Holtmann 	}
827040030efSMarcel Holtmann 
828040030efSMarcel Holtmann 	if (event == HCI_DEV_UNREG) {
829040030efSMarcel Holtmann 		struct sock *sk;
830040030efSMarcel Holtmann 
831e0448092STetsuo Handa 		/* Wake up sockets using this dead device */
832040030efSMarcel Holtmann 		read_lock(&hci_sk_list.lock);
833b67bfe0dSSasha Levin 		sk_for_each(sk, &hci_sk_list.head) {
834040030efSMarcel Holtmann 			if (hci_pi(sk)->hdev == hdev) {
835040030efSMarcel Holtmann 				sk->sk_err = EPIPE;
836040030efSMarcel Holtmann 				sk->sk_state_change(sk);
837040030efSMarcel Holtmann 			}
838040030efSMarcel Holtmann 		}
839040030efSMarcel Holtmann 		read_unlock(&hci_sk_list.lock);
840040030efSMarcel Holtmann 	}
841040030efSMarcel Holtmann }
842040030efSMarcel Holtmann 
843801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
844801c1e8dSJohan Hedberg {
845801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *c;
846801c1e8dSJohan Hedberg 
847801c1e8dSJohan Hedberg 	list_for_each_entry(c, &mgmt_chan_list, list) {
848801c1e8dSJohan Hedberg 		if (c->channel == channel)
849801c1e8dSJohan Hedberg 			return c;
850801c1e8dSJohan Hedberg 	}
851801c1e8dSJohan Hedberg 
852801c1e8dSJohan Hedberg 	return NULL;
853801c1e8dSJohan Hedberg }
854801c1e8dSJohan Hedberg 
855801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
856801c1e8dSJohan Hedberg {
857801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *c;
858801c1e8dSJohan Hedberg 
859801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
860801c1e8dSJohan Hedberg 	c = __hci_mgmt_chan_find(channel);
861801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
862801c1e8dSJohan Hedberg 
863801c1e8dSJohan Hedberg 	return c;
864801c1e8dSJohan Hedberg }
865801c1e8dSJohan Hedberg 
866801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
867801c1e8dSJohan Hedberg {
868801c1e8dSJohan Hedberg 	if (c->channel < HCI_CHANNEL_CONTROL)
869801c1e8dSJohan Hedberg 		return -EINVAL;
870801c1e8dSJohan Hedberg 
871801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
872801c1e8dSJohan Hedberg 	if (__hci_mgmt_chan_find(c->channel)) {
873801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
874801c1e8dSJohan Hedberg 		return -EALREADY;
875801c1e8dSJohan Hedberg 	}
876801c1e8dSJohan Hedberg 
877801c1e8dSJohan Hedberg 	list_add_tail(&c->list, &mgmt_chan_list);
878801c1e8dSJohan Hedberg 
879801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
880801c1e8dSJohan Hedberg 
881801c1e8dSJohan Hedberg 	return 0;
882801c1e8dSJohan Hedberg }
883801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register);
884801c1e8dSJohan Hedberg 
885801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
886801c1e8dSJohan Hedberg {
887801c1e8dSJohan Hedberg 	mutex_lock(&mgmt_chan_list_lock);
888801c1e8dSJohan Hedberg 	list_del(&c->list);
889801c1e8dSJohan Hedberg 	mutex_unlock(&mgmt_chan_list_lock);
890801c1e8dSJohan Hedberg }
891801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister);
892801c1e8dSJohan Hedberg 
8931da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock)
8941da177e4SLinus Torvalds {
8951da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8967b005bd3SMarcel Holtmann 	struct hci_dev *hdev;
897249fa169SMarcel Holtmann 	struct sk_buff *skb;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	if (!sk)
9021da177e4SLinus Torvalds 		return 0;
9031da177e4SLinus Torvalds 
90411eb85ecSDan Carpenter 	lock_sock(sk);
90511eb85ecSDan Carpenter 
90670ecce91SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
90770ecce91SMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
908cd82e61cSMarcel Holtmann 		atomic_dec(&monitor_promisc);
90970ecce91SMarcel Holtmann 		break;
910f81f5b2dSMarcel Holtmann 	case HCI_CHANNEL_RAW:
911aa1638ddSMarcel Holtmann 	case HCI_CHANNEL_USER:
91270ecce91SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
913249fa169SMarcel Holtmann 		/* Send event to monitor */
914249fa169SMarcel Holtmann 		skb = create_monitor_ctrl_close(sk);
915249fa169SMarcel Holtmann 		if (skb) {
916249fa169SMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
917249fa169SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
918249fa169SMarcel Holtmann 			kfree_skb(skb);
919249fa169SMarcel Holtmann 		}
920249fa169SMarcel Holtmann 
921df1cb87aSMarcel Holtmann 		hci_sock_free_cookie(sk);
92270ecce91SMarcel Holtmann 		break;
92370ecce91SMarcel Holtmann 	}
924cd82e61cSMarcel Holtmann 
9251da177e4SLinus Torvalds 	bt_sock_unlink(&hci_sk_list, sk);
9261da177e4SLinus Torvalds 
927e20a2e9cSMyungho Jung 	hdev = hci_pi(sk)->hdev;
9281da177e4SLinus Torvalds 	if (hdev) {
9290acef50bSAbhishek Pandit-Subedi 		if (hci_pi(sk)->channel == HCI_CHANNEL_USER &&
9300acef50bSAbhishek Pandit-Subedi 		    !hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
9319332ef9dSMasahiro Yamada 			/* When releasing a user channel exclusive access,
9326b3cc1dbSSimon Fels 			 * call hci_dev_do_close directly instead of calling
9336b3cc1dbSSimon Fels 			 * hci_dev_close to ensure the exclusive access will
9346b3cc1dbSSimon Fels 			 * be released and the controller brought back down.
9356b3cc1dbSSimon Fels 			 *
9366b3cc1dbSSimon Fels 			 * The checking of HCI_AUTO_OFF is not needed in this
9376b3cc1dbSSimon Fels 			 * case since it will have been cleared already when
9386b3cc1dbSSimon Fels 			 * opening the user channel.
9390acef50bSAbhishek Pandit-Subedi 			 *
9400acef50bSAbhishek Pandit-Subedi 			 * Make sure to also check that we haven't already
9410acef50bSAbhishek Pandit-Subedi 			 * unregistered since all the cleanup will have already
9420acef50bSAbhishek Pandit-Subedi 			 * been complete and hdev will get released when we put
9430acef50bSAbhishek Pandit-Subedi 			 * below.
9446b3cc1dbSSimon Fels 			 */
9456b3cc1dbSSimon Fels 			hci_dev_do_close(hdev);
9469380f9eaSLoic Poulain 			hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
9479380f9eaSLoic Poulain 			mgmt_index_added(hdev);
94823500189SMarcel Holtmann 		}
94923500189SMarcel Holtmann 
9501da177e4SLinus Torvalds 		atomic_dec(&hdev->promisc);
9511da177e4SLinus Torvalds 		hci_dev_put(hdev);
9521da177e4SLinus Torvalds 	}
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	sock_orphan(sk);
95511eb85ecSDan Carpenter 	release_sock(sk);
9561da177e4SLinus Torvalds 	sock_put(sk);
9571da177e4SLinus Torvalds 	return 0;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9603d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg)
961f0358568SJohan Hedberg {
962f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9635e762444SAntti Julku 	int err;
964f0358568SJohan Hedberg 
965f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
966f0358568SJohan Hedberg 		return -EFAULT;
967f0358568SJohan Hedberg 
96809fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9695e762444SAntti Julku 
9703d4f9c00SArchie Pusaka 	err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9715e762444SAntti Julku 
97209fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9735e762444SAntti Julku 
9745e762444SAntti Julku 	return err;
975f0358568SJohan Hedberg }
976f0358568SJohan Hedberg 
9773d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg)
978f0358568SJohan Hedberg {
979f0358568SJohan Hedberg 	bdaddr_t bdaddr;
9805e762444SAntti Julku 	int err;
981f0358568SJohan Hedberg 
982f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
983f0358568SJohan Hedberg 		return -EFAULT;
984f0358568SJohan Hedberg 
98509fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
9865e762444SAntti Julku 
9873d4f9c00SArchie Pusaka 	err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9885e762444SAntti Julku 
98909fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
9905e762444SAntti Julku 
9915e762444SAntti Julku 	return err;
992f0358568SJohan Hedberg }
993f0358568SJohan Hedberg 
9941da177e4SLinus Torvalds /* Ioctls that require bound socket */
9956039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
9966039aa73SGustavo Padovan 				unsigned long arg)
9971da177e4SLinus Torvalds {
998e0448092STetsuo Handa 	struct hci_dev *hdev = hci_hdev_from_sock(sk);
9991da177e4SLinus Torvalds 
1000e0448092STetsuo Handa 	if (IS_ERR(hdev))
1001e0448092STetsuo Handa 		return PTR_ERR(hdev);
10021da177e4SLinus Torvalds 
1003d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
10040736cfa8SMarcel Holtmann 		return -EBUSY;
10050736cfa8SMarcel Holtmann 
1006d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
1007fee746b0SMarcel Holtmann 		return -EOPNOTSUPP;
1008fee746b0SMarcel Holtmann 
1009ca8bee5dSMarcel Holtmann 	if (hdev->dev_type != HCI_PRIMARY)
10105b69bef5SMarcel Holtmann 		return -EOPNOTSUPP;
10115b69bef5SMarcel Holtmann 
10121da177e4SLinus Torvalds 	switch (cmd) {
10131da177e4SLinus Torvalds 	case HCISETRAW:
10141da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1015bf5b30b8SZhao Hongjiang 			return -EPERM;
1016db596681SMarcel Holtmann 		return -EOPNOTSUPP;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	case HCIGETCONNINFO:
10191da177e4SLinus Torvalds 		return hci_get_conn_info(hdev, (void __user *)arg);
10201da177e4SLinus Torvalds 
102140be492fSMarcel Holtmann 	case HCIGETAUTHINFO:
102240be492fSMarcel Holtmann 		return hci_get_auth_info(hdev, (void __user *)arg);
102340be492fSMarcel Holtmann 
1024f0358568SJohan Hedberg 	case HCIBLOCKADDR:
1025f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
1026bf5b30b8SZhao Hongjiang 			return -EPERM;
10273d4f9c00SArchie Pusaka 		return hci_sock_reject_list_add(hdev, (void __user *)arg);
1028f0358568SJohan Hedberg 
1029f0358568SJohan Hedberg 	case HCIUNBLOCKADDR:
1030f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
1031bf5b30b8SZhao Hongjiang 			return -EPERM;
10323d4f9c00SArchie Pusaka 		return hci_sock_reject_list_del(hdev, (void __user *)arg);
10330736cfa8SMarcel Holtmann 	}
1034f0358568SJohan Hedberg 
1035324d36edSMarcel Holtmann 	return -ENOIOCTLCMD;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10388fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
10398fc9ced3SGustavo Padovan 			  unsigned long arg)
10401da177e4SLinus Torvalds {
10411da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
10420736cfa8SMarcel Holtmann 	struct sock *sk = sock->sk;
10431da177e4SLinus Torvalds 	int err;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	BT_DBG("cmd %x arg %lx", cmd, arg);
10461da177e4SLinus Torvalds 
1047000c2fa2SRuihan Li 	/* Make sure the cmd is valid before doing anything */
1048000c2fa2SRuihan Li 	switch (cmd) {
1049000c2fa2SRuihan Li 	case HCIGETDEVLIST:
1050000c2fa2SRuihan Li 	case HCIGETDEVINFO:
1051000c2fa2SRuihan Li 	case HCIGETCONNLIST:
1052000c2fa2SRuihan Li 	case HCIDEVUP:
1053000c2fa2SRuihan Li 	case HCIDEVDOWN:
1054000c2fa2SRuihan Li 	case HCIDEVRESET:
1055000c2fa2SRuihan Li 	case HCIDEVRESTAT:
1056000c2fa2SRuihan Li 	case HCISETSCAN:
1057000c2fa2SRuihan Li 	case HCISETAUTH:
1058000c2fa2SRuihan Li 	case HCISETENCRYPT:
1059000c2fa2SRuihan Li 	case HCISETPTYPE:
1060000c2fa2SRuihan Li 	case HCISETLINKPOL:
1061000c2fa2SRuihan Li 	case HCISETLINKMODE:
1062000c2fa2SRuihan Li 	case HCISETACLMTU:
1063000c2fa2SRuihan Li 	case HCISETSCOMTU:
1064000c2fa2SRuihan Li 	case HCIINQUIRY:
1065000c2fa2SRuihan Li 	case HCISETRAW:
1066000c2fa2SRuihan Li 	case HCIGETCONNINFO:
1067000c2fa2SRuihan Li 	case HCIGETAUTHINFO:
1068000c2fa2SRuihan Li 	case HCIBLOCKADDR:
1069000c2fa2SRuihan Li 	case HCIUNBLOCKADDR:
1070000c2fa2SRuihan Li 		break;
1071000c2fa2SRuihan Li 	default:
1072000c2fa2SRuihan Li 		return -ENOIOCTLCMD;
1073000c2fa2SRuihan Li 	}
1074000c2fa2SRuihan Li 
1075c1c4f956SMarcel Holtmann 	lock_sock(sk);
1076c1c4f956SMarcel Holtmann 
1077c1c4f956SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1078c1c4f956SMarcel Holtmann 		err = -EBADFD;
1079c1c4f956SMarcel Holtmann 		goto done;
1080c1c4f956SMarcel Holtmann 	}
1081c1c4f956SMarcel Holtmann 
1082f81f5b2dSMarcel Holtmann 	/* When calling an ioctl on an unbound raw socket, then ensure
1083f81f5b2dSMarcel Holtmann 	 * that the monitor gets informed. Ensure that the resulting event
1084f81f5b2dSMarcel Holtmann 	 * is only send once by checking if the cookie exists or not. The
1085f81f5b2dSMarcel Holtmann 	 * socket cookie will be only ever generated once for the lifetime
1086f81f5b2dSMarcel Holtmann 	 * of a given socket.
1087f81f5b2dSMarcel Holtmann 	 */
1088f81f5b2dSMarcel Holtmann 	if (hci_sock_gen_cookie(sk)) {
1089f81f5b2dSMarcel Holtmann 		struct sk_buff *skb;
1090f81f5b2dSMarcel Holtmann 
109125c150acSRuihan Li 		/* Perform careful checks before setting the HCI_SOCK_TRUSTED
109225c150acSRuihan Li 		 * flag. Make sure that not only the current task but also
109325c150acSRuihan Li 		 * the socket opener has the required capability, since
109425c150acSRuihan Li 		 * privileged programs can be tricked into making ioctl calls
109525c150acSRuihan Li 		 * on HCI sockets, and the socket should not be marked as
109625c150acSRuihan Li 		 * trusted simply because the ioctl caller is privileged.
109725c150acSRuihan Li 		 */
109825c150acSRuihan Li 		if (sk_capable(sk, CAP_NET_ADMIN))
1099f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1100f81f5b2dSMarcel Holtmann 
1101f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1102f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1103f81f5b2dSMarcel Holtmann 		if (skb) {
1104f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1105f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1106f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1107f81f5b2dSMarcel Holtmann 		}
1108f81f5b2dSMarcel Holtmann 	}
1109f81f5b2dSMarcel Holtmann 
1110c1c4f956SMarcel Holtmann 	release_sock(sk);
1111c1c4f956SMarcel Holtmann 
11121da177e4SLinus Torvalds 	switch (cmd) {
11131da177e4SLinus Torvalds 	case HCIGETDEVLIST:
11141da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	case HCIGETDEVINFO:
11171da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	case HCIGETCONNLIST:
11201da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	case HCIDEVUP:
11231da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1124bf5b30b8SZhao Hongjiang 			return -EPERM;
11251da177e4SLinus Torvalds 		return hci_dev_open(arg);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	case HCIDEVDOWN:
11281da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1129bf5b30b8SZhao Hongjiang 			return -EPERM;
11301da177e4SLinus Torvalds 		return hci_dev_close(arg);
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	case HCIDEVRESET:
11331da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1134bf5b30b8SZhao Hongjiang 			return -EPERM;
11351da177e4SLinus Torvalds 		return hci_dev_reset(arg);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	case HCIDEVRESTAT:
11381da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1139bf5b30b8SZhao Hongjiang 			return -EPERM;
11401da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	case HCISETSCAN:
11431da177e4SLinus Torvalds 	case HCISETAUTH:
11441da177e4SLinus Torvalds 	case HCISETENCRYPT:
11451da177e4SLinus Torvalds 	case HCISETPTYPE:
11461da177e4SLinus Torvalds 	case HCISETLINKPOL:
11471da177e4SLinus Torvalds 	case HCISETLINKMODE:
11481da177e4SLinus Torvalds 	case HCISETACLMTU:
11491da177e4SLinus Torvalds 	case HCISETSCOMTU:
11501da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
1151bf5b30b8SZhao Hongjiang 			return -EPERM;
11521da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	case HCIINQUIRY:
11551da177e4SLinus Torvalds 		return hci_inquiry(argp);
1156c1c4f956SMarcel Holtmann 	}
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	lock_sock(sk);
1159c1c4f956SMarcel Holtmann 
11601da177e4SLinus Torvalds 	err = hci_sock_bound_ioctl(sk, cmd, arg);
1161c1c4f956SMarcel Holtmann 
1162c1c4f956SMarcel Holtmann done:
11631da177e4SLinus Torvalds 	release_sock(sk);
11641da177e4SLinus Torvalds 	return err;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11677a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
11687a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd,
11697a6038b3SArnd Bergmann 				 unsigned long arg)
11707a6038b3SArnd Bergmann {
11717a6038b3SArnd Bergmann 	switch (cmd) {
11727a6038b3SArnd Bergmann 	case HCIDEVUP:
11737a6038b3SArnd Bergmann 	case HCIDEVDOWN:
11747a6038b3SArnd Bergmann 	case HCIDEVRESET:
11757a6038b3SArnd Bergmann 	case HCIDEVRESTAT:
11767a6038b3SArnd Bergmann 		return hci_sock_ioctl(sock, cmd, arg);
11777a6038b3SArnd Bergmann 	}
11787a6038b3SArnd Bergmann 
11797a6038b3SArnd Bergmann 	return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
11807a6038b3SArnd Bergmann }
11817a6038b3SArnd Bergmann #endif
11827a6038b3SArnd Bergmann 
11838fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
11848fc9ced3SGustavo Padovan 			 int addr_len)
11851da177e4SLinus Torvalds {
11860381101fSJohan Hedberg 	struct sockaddr_hci haddr;
11871da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
11881da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
1189f4cdbb3fSMarcel Holtmann 	struct sk_buff *skb;
11900381101fSJohan Hedberg 	int len, err = 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
11931da177e4SLinus Torvalds 
11940381101fSJohan Hedberg 	if (!addr)
11950381101fSJohan Hedberg 		return -EINVAL;
11960381101fSJohan Hedberg 
11970381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
11980381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
11990381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
12000381101fSJohan Hedberg 
12010381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
12020381101fSJohan Hedberg 		return -EINVAL;
12030381101fSJohan Hedberg 
12041da177e4SLinus Torvalds 	lock_sock(sk);
12051da177e4SLinus Torvalds 
1206e0448092STetsuo Handa 	/* Allow detaching from dead device and attaching to alive device, if
1207e0448092STetsuo Handa 	 * the caller wants to re-bind (instead of close) this socket in
1208e0448092STetsuo Handa 	 * response to hci_sock_dev_event(HCI_DEV_UNREG) notification.
1209e0448092STetsuo Handa 	 */
1210e0448092STetsuo Handa 	hdev = hci_pi(sk)->hdev;
1211e0448092STetsuo Handa 	if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1212e0448092STetsuo Handa 		hci_pi(sk)->hdev = NULL;
1213e0448092STetsuo Handa 		sk->sk_state = BT_OPEN;
1214e0448092STetsuo Handa 		hci_dev_put(hdev);
1215e0448092STetsuo Handa 	}
1216e0448092STetsuo Handa 	hdev = NULL;
1217e0448092STetsuo Handa 
12187cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
12197cc2ade2SMarcel Holtmann 		err = -EALREADY;
12207cc2ade2SMarcel Holtmann 		goto done;
12217cc2ade2SMarcel Holtmann 	}
12227cc2ade2SMarcel Holtmann 
12237cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
12247cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
12257cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
12261da177e4SLinus Torvalds 			err = -EALREADY;
12271da177e4SLinus Torvalds 			goto done;
12281da177e4SLinus Torvalds 		}
12291da177e4SLinus Torvalds 
12300381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
12310381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
123270f23020SAndrei Emeltchenko 			if (!hdev) {
12331da177e4SLinus Torvalds 				err = -ENODEV;
12341da177e4SLinus Torvalds 				goto done;
12351da177e4SLinus Torvalds 			}
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds 
12405a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1241f81f5b2dSMarcel Holtmann 
1242f4cdbb3fSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1243f4cdbb3fSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1244f4cdbb3fSMarcel Holtmann 			 * then there has been already an ioctl issued against
124591641b79SZheng Yongjun 			 * an unbound socket and with that triggered an open
1246f4cdbb3fSMarcel Holtmann 			 * notification. Send a close notification first to
1247f4cdbb3fSMarcel Holtmann 			 * allow the state transition to bounded.
1248f81f5b2dSMarcel Holtmann 			 */
1249f4cdbb3fSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1250f4cdbb3fSMarcel Holtmann 			if (skb) {
1251f4cdbb3fSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1252f4cdbb3fSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1253f4cdbb3fSMarcel Holtmann 				kfree_skb(skb);
1254f4cdbb3fSMarcel Holtmann 			}
1255f4cdbb3fSMarcel Holtmann 		}
1256f81f5b2dSMarcel Holtmann 
1257f81f5b2dSMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
1258f81f5b2dSMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1259f81f5b2dSMarcel Holtmann 
1260f4cdbb3fSMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
1261f4cdbb3fSMarcel Holtmann 
1262f81f5b2dSMarcel Holtmann 		/* Send event to monitor */
1263f81f5b2dSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1264f81f5b2dSMarcel Holtmann 		if (skb) {
1265f81f5b2dSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1266f81f5b2dSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1267f81f5b2dSMarcel Holtmann 			kfree_skb(skb);
1268f81f5b2dSMarcel Holtmann 		}
12697cc2ade2SMarcel Holtmann 		break;
12707cc2ade2SMarcel Holtmann 
127123500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
127223500189SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
127323500189SMarcel Holtmann 			err = -EALREADY;
127423500189SMarcel Holtmann 			goto done;
127523500189SMarcel Holtmann 		}
127623500189SMarcel Holtmann 
127723500189SMarcel Holtmann 		if (haddr.hci_dev == HCI_DEV_NONE) {
127823500189SMarcel Holtmann 			err = -EINVAL;
127923500189SMarcel Holtmann 			goto done;
128023500189SMarcel Holtmann 		}
128123500189SMarcel Holtmann 
128210a8b86fSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
128323500189SMarcel Holtmann 			err = -EPERM;
128423500189SMarcel Holtmann 			goto done;
128523500189SMarcel Holtmann 		}
128623500189SMarcel Holtmann 
128723500189SMarcel Holtmann 		hdev = hci_dev_get(haddr.hci_dev);
128823500189SMarcel Holtmann 		if (!hdev) {
128923500189SMarcel Holtmann 			err = -ENODEV;
129023500189SMarcel Holtmann 			goto done;
129123500189SMarcel Holtmann 		}
129223500189SMarcel Holtmann 
1293781f899fSMarcel Holtmann 		if (test_bit(HCI_INIT, &hdev->flags) ||
1294d7a5a11dSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_SETUP) ||
1295781f899fSMarcel Holtmann 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1296781f899fSMarcel Holtmann 		    (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1297781f899fSMarcel Holtmann 		     test_bit(HCI_UP, &hdev->flags))) {
129823500189SMarcel Holtmann 			err = -EBUSY;
129923500189SMarcel Holtmann 			hci_dev_put(hdev);
130023500189SMarcel Holtmann 			goto done;
130123500189SMarcel Holtmann 		}
130223500189SMarcel Holtmann 
1303238be788SMarcel Holtmann 		if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
130423500189SMarcel Holtmann 			err = -EUSERS;
130523500189SMarcel Holtmann 			hci_dev_put(hdev);
130623500189SMarcel Holtmann 			goto done;
130723500189SMarcel Holtmann 		}
130823500189SMarcel Holtmann 
130923500189SMarcel Holtmann 		mgmt_index_removed(hdev);
131023500189SMarcel Holtmann 
131123500189SMarcel Holtmann 		err = hci_dev_open(hdev->id);
131223500189SMarcel Holtmann 		if (err) {
1313781f899fSMarcel Holtmann 			if (err == -EALREADY) {
1314781f899fSMarcel Holtmann 				/* In case the transport is already up and
1315781f899fSMarcel Holtmann 				 * running, clear the error here.
1316781f899fSMarcel Holtmann 				 *
13179332ef9dSMasahiro Yamada 				 * This can happen when opening a user
1318781f899fSMarcel Holtmann 				 * channel and HCI_AUTO_OFF grace period
1319781f899fSMarcel Holtmann 				 * is still active.
1320781f899fSMarcel Holtmann 				 */
1321781f899fSMarcel Holtmann 				err = 0;
1322781f899fSMarcel Holtmann 			} else {
1323a358dc11SMarcel Holtmann 				hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1324c6521401SMarcel Holtmann 				mgmt_index_added(hdev);
132523500189SMarcel Holtmann 				hci_dev_put(hdev);
132623500189SMarcel Holtmann 				goto done;
132723500189SMarcel Holtmann 			}
1328781f899fSMarcel Holtmann 		}
132923500189SMarcel Holtmann 
13305a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1331aa1638ddSMarcel Holtmann 
1332aa1638ddSMarcel Holtmann 		if (!hci_sock_gen_cookie(sk)) {
1333aa1638ddSMarcel Holtmann 			/* In the case when a cookie has already been assigned,
1334aa1638ddSMarcel Holtmann 			 * this socket will transition from a raw socket into
13359332ef9dSMasahiro Yamada 			 * a user channel socket. For a clean transition, send
1336aa1638ddSMarcel Holtmann 			 * the close notification first.
1337aa1638ddSMarcel Holtmann 			 */
1338aa1638ddSMarcel Holtmann 			skb = create_monitor_ctrl_close(sk);
1339aa1638ddSMarcel Holtmann 			if (skb) {
1340aa1638ddSMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1341aa1638ddSMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1342aa1638ddSMarcel Holtmann 				kfree_skb(skb);
1343aa1638ddSMarcel Holtmann 			}
1344aa1638ddSMarcel Holtmann 		}
1345aa1638ddSMarcel Holtmann 
1346aa1638ddSMarcel Holtmann 		/* The user channel is restricted to CAP_NET_ADMIN
1347aa1638ddSMarcel Holtmann 		 * capabilities and with that implicitly trusted.
1348aa1638ddSMarcel Holtmann 		 */
1349aa1638ddSMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1350aa1638ddSMarcel Holtmann 
135123500189SMarcel Holtmann 		hci_pi(sk)->hdev = hdev;
13525a6d2cf5SMarcel Holtmann 
1353aa1638ddSMarcel Holtmann 		/* Send event to monitor */
1354aa1638ddSMarcel Holtmann 		skb = create_monitor_ctrl_open(sk);
1355aa1638ddSMarcel Holtmann 		if (skb) {
1356aa1638ddSMarcel Holtmann 			hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1357aa1638ddSMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
1358aa1638ddSMarcel Holtmann 			kfree_skb(skb);
1359aa1638ddSMarcel Holtmann 		}
1360aa1638ddSMarcel Holtmann 
13615a6d2cf5SMarcel Holtmann 		atomic_inc(&hdev->promisc);
136223500189SMarcel Holtmann 		break;
136323500189SMarcel Holtmann 
1364cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1365cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1366cd82e61cSMarcel Holtmann 			err = -EINVAL;
1367cd82e61cSMarcel Holtmann 			goto done;
1368cd82e61cSMarcel Holtmann 		}
1369cd82e61cSMarcel Holtmann 
1370cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
1371cd82e61cSMarcel Holtmann 			err = -EPERM;
1372cd82e61cSMarcel Holtmann 			goto done;
1373cd82e61cSMarcel Holtmann 		}
1374cd82e61cSMarcel Holtmann 
13755a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
13765a6d2cf5SMarcel Holtmann 
137750ebc055SMarcel Holtmann 		/* The monitor interface is restricted to CAP_NET_RAW
137850ebc055SMarcel Holtmann 		 * capabilities and with that implicitly trusted.
137950ebc055SMarcel Holtmann 		 */
138050ebc055SMarcel Holtmann 		hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
138150ebc055SMarcel Holtmann 
1382787b306cSJohannes Berg 		send_monitor_note(sk, "Linux version %s (%s)",
1383787b306cSJohannes Berg 				  init_utsname()->release,
1384787b306cSJohannes Berg 				  init_utsname()->machine);
13859e8305b3SMarcel Holtmann 		send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
13869e8305b3SMarcel Holtmann 				  BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
1387cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
1388249fa169SMarcel Holtmann 		send_monitor_control_replay(sk);
1389cd82e61cSMarcel Holtmann 
1390cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
1391cd82e61cSMarcel Holtmann 		break;
1392cd82e61cSMarcel Holtmann 
1393ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
1394ac714949SMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
1395ac714949SMarcel Holtmann 			err = -EINVAL;
1396ac714949SMarcel Holtmann 			goto done;
1397ac714949SMarcel Holtmann 		}
1398ac714949SMarcel Holtmann 
1399ac714949SMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
1400ac714949SMarcel Holtmann 			err = -EPERM;
1401ac714949SMarcel Holtmann 			goto done;
1402ac714949SMarcel Holtmann 		}
14035a6d2cf5SMarcel Holtmann 
14045a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
1405ac714949SMarcel Holtmann 		break;
1406ac714949SMarcel Holtmann 
14077cc2ade2SMarcel Holtmann 	default:
1408801c1e8dSJohan Hedberg 		if (!hci_mgmt_chan_find(haddr.hci_channel)) {
14097cc2ade2SMarcel Holtmann 			err = -EINVAL;
14107cc2ade2SMarcel Holtmann 			goto done;
14117cc2ade2SMarcel Holtmann 		}
14127cc2ade2SMarcel Holtmann 
1413801c1e8dSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
1414801c1e8dSJohan Hedberg 			err = -EINVAL;
1415801c1e8dSJohan Hedberg 			goto done;
1416801c1e8dSJohan Hedberg 		}
1417801c1e8dSJohan Hedberg 
14181195fbb8SMarcel Holtmann 		/* Users with CAP_NET_ADMIN capabilities are allowed
14191195fbb8SMarcel Holtmann 		 * access to all management commands and events. For
14201195fbb8SMarcel Holtmann 		 * untrusted users the interface is restricted and
14211195fbb8SMarcel Holtmann 		 * also only untrusted events are sent.
142250ebc055SMarcel Holtmann 		 */
14231195fbb8SMarcel Holtmann 		if (capable(CAP_NET_ADMIN))
142450ebc055SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
142550ebc055SMarcel Holtmann 
14265a6d2cf5SMarcel Holtmann 		hci_pi(sk)->channel = haddr.hci_channel;
14275a6d2cf5SMarcel Holtmann 
1428f9207338SMarcel Holtmann 		/* At the moment the index and unconfigured index events
1429f9207338SMarcel Holtmann 		 * are enabled unconditionally. Setting them on each
1430f9207338SMarcel Holtmann 		 * socket when binding keeps this functionality. They
1431f9207338SMarcel Holtmann 		 * however might be cleared later and then sending of these
1432f9207338SMarcel Holtmann 		 * events will be disabled, but that is then intentional.
1433f6b7712eSMarcel Holtmann 		 *
1434f6b7712eSMarcel Holtmann 		 * This also enables generic events that are safe to be
1435f6b7712eSMarcel Holtmann 		 * received by untrusted users. Example for such events
1436f6b7712eSMarcel Holtmann 		 * are changes to settings, class of device, name etc.
1437f9207338SMarcel Holtmann 		 */
14385a6d2cf5SMarcel Holtmann 		if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
1439f4cdbb3fSMarcel Holtmann 			if (!hci_sock_gen_cookie(sk)) {
1440f4cdbb3fSMarcel Holtmann 				/* In the case when a cookie has already been
144191641b79SZheng Yongjun 				 * assigned, this socket will transition from
1442f4cdbb3fSMarcel Holtmann 				 * a raw socket into a control socket. To
144391641b79SZheng Yongjun 				 * allow for a clean transition, send the
1444f4cdbb3fSMarcel Holtmann 				 * close notification first.
1445f4cdbb3fSMarcel Holtmann 				 */
1446f4cdbb3fSMarcel Holtmann 				skb = create_monitor_ctrl_close(sk);
1447f4cdbb3fSMarcel Holtmann 				if (skb) {
1448f4cdbb3fSMarcel Holtmann 					hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1449f4cdbb3fSMarcel Holtmann 							    HCI_SOCK_TRUSTED, NULL);
1450f4cdbb3fSMarcel Holtmann 					kfree_skb(skb);
1451f4cdbb3fSMarcel Holtmann 				}
1452f4cdbb3fSMarcel Holtmann 			}
145370ecce91SMarcel Holtmann 
1454249fa169SMarcel Holtmann 			/* Send event to monitor */
1455249fa169SMarcel Holtmann 			skb = create_monitor_ctrl_open(sk);
1456249fa169SMarcel Holtmann 			if (skb) {
1457249fa169SMarcel Holtmann 				hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1458249fa169SMarcel Holtmann 						    HCI_SOCK_TRUSTED, NULL);
1459249fa169SMarcel Holtmann 				kfree_skb(skb);
1460249fa169SMarcel Holtmann 			}
1461249fa169SMarcel Holtmann 
1462f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1463f9207338SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
14645504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
14655504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
14665504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
14675504c3a3SMarcel Holtmann 			hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1468f9207338SMarcel Holtmann 		}
1469801c1e8dSJohan Hedberg 		break;
1470801c1e8dSJohan Hedberg 	}
1471801c1e8dSJohan Hedberg 
147209572fcaSLuiz Augusto von Dentz 	/* Default MTU to HCI_MAX_FRAME_SIZE if not set */
147309572fcaSLuiz Augusto von Dentz 	if (!hci_pi(sk)->mtu)
147409572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE;
147509572fcaSLuiz Augusto von Dentz 
14761da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds done:
14791da177e4SLinus Torvalds 	release_sock(sk);
14801da177e4SLinus Torvalds 	return err;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14838fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
14849b2c45d4SDenys Vlasenko 			    int peer)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
14871da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
14889d4b68b2SMarcel Holtmann 	struct hci_dev *hdev;
14899d4b68b2SMarcel Holtmann 	int err = 0;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
14921da177e4SLinus Torvalds 
149306f43cbcSMarcel Holtmann 	if (peer)
149406f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
149506f43cbcSMarcel Holtmann 
14961da177e4SLinus Torvalds 	lock_sock(sk);
14971da177e4SLinus Torvalds 
1498e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1499e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1500e0448092STetsuo Handa 		err = PTR_ERR(hdev);
15019d4b68b2SMarcel Holtmann 		goto done;
15029d4b68b2SMarcel Holtmann 	}
15039d4b68b2SMarcel Holtmann 
15041da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
15057b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
15069d4b68b2SMarcel Holtmann 	haddr->hci_channel= hci_pi(sk)->channel;
15079b2c45d4SDenys Vlasenko 	err = sizeof(*haddr);
15081da177e4SLinus Torvalds 
15099d4b68b2SMarcel Holtmann done:
15101da177e4SLinus Torvalds 	release_sock(sk);
15119d4b68b2SMarcel Holtmann 	return err;
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds 
15146039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
15156039aa73SGustavo Padovan 			  struct sk_buff *skb)
15161da177e4SLinus Torvalds {
151732929e1fSAlain Michaud 	__u8 mask = hci_pi(sk)->cmsg_mask;
15181da177e4SLinus Torvalds 
15190d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
15200d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
15218fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
15228fc9ced3SGustavo Padovan 			 &incoming);
15230d48d939SMarcel Holtmann 	}
15241da177e4SLinus Torvalds 
1525a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
1526f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
152713c6ee2aSDeepa Dinamani 		struct old_timeval32 ctv;
1528f6e623a6SJohann Felix Soden #endif
152913c6ee2aSDeepa Dinamani 		struct __kernel_old_timeval tv;
1530767c5eb5SMarcel Holtmann 		void *data;
1531767c5eb5SMarcel Holtmann 		int len;
1532a61bbcf2SPatrick McHardy 
1533a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
1534767c5eb5SMarcel Holtmann 
15351da97f83SDavid S. Miller 		data = &tv;
15361da97f83SDavid S. Miller 		len = sizeof(tv);
15371da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
1538da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
1539da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
1540767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
1541767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
1542767c5eb5SMarcel Holtmann 			data = &ctv;
1543767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
1544767c5eb5SMarcel Holtmann 		}
15451da97f83SDavid S. Miller #endif
1546767c5eb5SMarcel Holtmann 
1547767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
1548a61bbcf2SPatrick McHardy 	}
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
15518528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
15528528d3f7SMarcel Holtmann 			    size_t len, int flags)
15531da177e4SLinus Torvalds {
1554*69ae5065SLuiz Augusto von Dentz 	struct scm_cookie scm;
15551da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
15561da177e4SLinus Torvalds 	struct sk_buff *skb;
15571da177e4SLinus Torvalds 	int copied, err;
155883871f8cSDenis Kenzior 	unsigned int skblen;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
15611da177e4SLinus Torvalds 
1562d94a6104SMarcel Holtmann 	if (flags & MSG_OOB)
15631da177e4SLinus Torvalds 		return -EOPNOTSUPP;
15641da177e4SLinus Torvalds 
1565ac714949SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1566ac714949SMarcel Holtmann 		return -EOPNOTSUPP;
1567ac714949SMarcel Holtmann 
15681da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
15691da177e4SLinus Torvalds 		return 0;
15701da177e4SLinus Torvalds 
1571f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
157270f23020SAndrei Emeltchenko 	if (!skb)
15731da177e4SLinus Torvalds 		return err;
15741da177e4SLinus Torvalds 
157583871f8cSDenis Kenzior 	skblen = skb->len;
15761da177e4SLinus Torvalds 	copied = skb->len;
15771da177e4SLinus Torvalds 	if (len < copied) {
15781da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
15791da177e4SLinus Torvalds 		copied = len;
15801da177e4SLinus Torvalds 	}
15811da177e4SLinus Torvalds 
1582badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
158351f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
15841da177e4SLinus Torvalds 
15853a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
15863a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
15871da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
15883a208627SMarcel Holtmann 		break;
158923500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
1590cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1591cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
1592cd82e61cSMarcel Holtmann 		break;
1593801c1e8dSJohan Hedberg 	default:
1594801c1e8dSJohan Hedberg 		if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1595801c1e8dSJohan Hedberg 			sock_recv_timestamp(msg, sk, skb);
1596801c1e8dSJohan Hedberg 		break;
15973a208627SMarcel Holtmann 	}
15981da177e4SLinus Torvalds 
1599*69ae5065SLuiz Augusto von Dentz 	memset(&scm, 0, sizeof(scm));
1600*69ae5065SLuiz Augusto von Dentz 	scm.creds = bt_cb(skb)->creds;
1601*69ae5065SLuiz Augusto von Dentz 
16021da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
16031da177e4SLinus Torvalds 
16044f34228bSLuiz Augusto von Dentz 	if (flags & MSG_TRUNC)
160583871f8cSDenis Kenzior 		copied = skblen;
160683871f8cSDenis Kenzior 
1607*69ae5065SLuiz Augusto von Dentz 	scm_recv(sock, msg, &scm, flags);
1608*69ae5065SLuiz Augusto von Dentz 
16091da177e4SLinus Torvalds 	return err ? : copied;
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
161264ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
161364ba2eb3SLuiz Augusto von Dentz 			struct sk_buff *skb)
1614fa4335d7SJohan Hedberg {
1615fa4335d7SJohan Hedberg 	u8 *cp;
1616fa4335d7SJohan Hedberg 	struct mgmt_hdr *hdr;
1617fa4335d7SJohan Hedberg 	u16 opcode, index, len;
1618fa4335d7SJohan Hedberg 	struct hci_dev *hdev = NULL;
1619fa4335d7SJohan Hedberg 	const struct hci_mgmt_handler *handler;
1620fa4335d7SJohan Hedberg 	bool var_len, no_hdev;
1621fa4335d7SJohan Hedberg 	int err;
1622fa4335d7SJohan Hedberg 
162364ba2eb3SLuiz Augusto von Dentz 	BT_DBG("got %d bytes", skb->len);
1624fa4335d7SJohan Hedberg 
162564ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr))
1626fa4335d7SJohan Hedberg 		return -EINVAL;
1627fa4335d7SJohan Hedberg 
162864ba2eb3SLuiz Augusto von Dentz 	hdr = (void *)skb->data;
1629fa4335d7SJohan Hedberg 	opcode = __le16_to_cpu(hdr->opcode);
1630fa4335d7SJohan Hedberg 	index = __le16_to_cpu(hdr->index);
1631fa4335d7SJohan Hedberg 	len = __le16_to_cpu(hdr->len);
1632fa4335d7SJohan Hedberg 
163364ba2eb3SLuiz Augusto von Dentz 	if (len != skb->len - sizeof(*hdr)) {
1634fa4335d7SJohan Hedberg 		err = -EINVAL;
1635fa4335d7SJohan Hedberg 		goto done;
1636fa4335d7SJohan Hedberg 	}
1637fa4335d7SJohan Hedberg 
163838ceaa00SMarcel Holtmann 	if (chan->channel == HCI_CHANNEL_CONTROL) {
163964ba2eb3SLuiz Augusto von Dentz 		struct sk_buff *cmd;
164038ceaa00SMarcel Holtmann 
164138ceaa00SMarcel Holtmann 		/* Send event to monitor */
164264ba2eb3SLuiz Augusto von Dentz 		cmd = create_monitor_ctrl_command(sk, index, opcode, len,
164364ba2eb3SLuiz Augusto von Dentz 						  skb->data + sizeof(*hdr));
164464ba2eb3SLuiz Augusto von Dentz 		if (cmd) {
164564ba2eb3SLuiz Augusto von Dentz 			hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd,
164638ceaa00SMarcel Holtmann 					    HCI_SOCK_TRUSTED, NULL);
164764ba2eb3SLuiz Augusto von Dentz 			kfree_skb(cmd);
164838ceaa00SMarcel Holtmann 		}
164938ceaa00SMarcel Holtmann 	}
165038ceaa00SMarcel Holtmann 
1651fa4335d7SJohan Hedberg 	if (opcode >= chan->handler_count ||
1652fa4335d7SJohan Hedberg 	    chan->handlers[opcode].func == NULL) {
1653fa4335d7SJohan Hedberg 		BT_DBG("Unknown op %u", opcode);
1654fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1655fa4335d7SJohan Hedberg 				      MGMT_STATUS_UNKNOWN_COMMAND);
1656fa4335d7SJohan Hedberg 		goto done;
1657fa4335d7SJohan Hedberg 	}
1658fa4335d7SJohan Hedberg 
1659fa4335d7SJohan Hedberg 	handler = &chan->handlers[opcode];
1660fa4335d7SJohan Hedberg 
1661fa4335d7SJohan Hedberg 	if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1662fa4335d7SJohan Hedberg 	    !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1663fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1664fa4335d7SJohan Hedberg 				      MGMT_STATUS_PERMISSION_DENIED);
1665fa4335d7SJohan Hedberg 		goto done;
1666fa4335d7SJohan Hedberg 	}
1667fa4335d7SJohan Hedberg 
1668fa4335d7SJohan Hedberg 	if (index != MGMT_INDEX_NONE) {
1669fa4335d7SJohan Hedberg 		hdev = hci_dev_get(index);
1670fa4335d7SJohan Hedberg 		if (!hdev) {
1671fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1672fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1673fa4335d7SJohan Hedberg 			goto done;
1674fa4335d7SJohan Hedberg 		}
1675fa4335d7SJohan Hedberg 
1676fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1677fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_CONFIG) ||
1678fa4335d7SJohan Hedberg 		    hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1679fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1680fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1681fa4335d7SJohan Hedberg 			goto done;
1682fa4335d7SJohan Hedberg 		}
1683fa4335d7SJohan Hedberg 
1684fa4335d7SJohan Hedberg 		if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1685fa4335d7SJohan Hedberg 		    !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1686fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1687fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1688fa4335d7SJohan Hedberg 			goto done;
1689fa4335d7SJohan Hedberg 		}
1690fa4335d7SJohan Hedberg 	}
1691fa4335d7SJohan Hedberg 
1692d5cc6626SMarcel Holtmann 	if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) {
1693fa4335d7SJohan Hedberg 		no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1694fa4335d7SJohan Hedberg 		if (no_hdev != !hdev) {
1695fa4335d7SJohan Hedberg 			err = mgmt_cmd_status(sk, index, opcode,
1696fa4335d7SJohan Hedberg 					      MGMT_STATUS_INVALID_INDEX);
1697fa4335d7SJohan Hedberg 			goto done;
1698fa4335d7SJohan Hedberg 		}
1699d5cc6626SMarcel Holtmann 	}
1700fa4335d7SJohan Hedberg 
1701fa4335d7SJohan Hedberg 	var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1702fa4335d7SJohan Hedberg 	if ((var_len && len < handler->data_len) ||
1703fa4335d7SJohan Hedberg 	    (!var_len && len != handler->data_len)) {
1704fa4335d7SJohan Hedberg 		err = mgmt_cmd_status(sk, index, opcode,
1705fa4335d7SJohan Hedberg 				      MGMT_STATUS_INVALID_PARAMS);
1706fa4335d7SJohan Hedberg 		goto done;
1707fa4335d7SJohan Hedberg 	}
1708fa4335d7SJohan Hedberg 
1709fa4335d7SJohan Hedberg 	if (hdev && chan->hdev_init)
1710fa4335d7SJohan Hedberg 		chan->hdev_init(sk, hdev);
1711fa4335d7SJohan Hedberg 
171264ba2eb3SLuiz Augusto von Dentz 	cp = skb->data + sizeof(*hdr);
1713fa4335d7SJohan Hedberg 
1714fa4335d7SJohan Hedberg 	err = handler->func(sk, hdev, cp, len);
1715fa4335d7SJohan Hedberg 	if (err < 0)
1716fa4335d7SJohan Hedberg 		goto done;
1717fa4335d7SJohan Hedberg 
171864ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1719fa4335d7SJohan Hedberg 
1720fa4335d7SJohan Hedberg done:
1721fa4335d7SJohan Hedberg 	if (hdev)
1722fa4335d7SJohan Hedberg 		hci_dev_put(hdev);
1723fa4335d7SJohan Hedberg 
1724fa4335d7SJohan Hedberg 	return err;
1725fa4335d7SJohan Hedberg }
1726fa4335d7SJohan Hedberg 
172764ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb,
172864ba2eb3SLuiz Augusto von Dentz 			     unsigned int flags)
1729ac714949SMarcel Holtmann {
1730ac714949SMarcel Holtmann 	struct hci_mon_hdr *hdr;
1731ac714949SMarcel Holtmann 	struct hci_dev *hdev;
1732ac714949SMarcel Holtmann 	u16 index;
1733ac714949SMarcel Holtmann 	int err;
1734ac714949SMarcel Holtmann 
1735ac714949SMarcel Holtmann 	/* The logging frame consists at minimum of the standard header,
1736ac714949SMarcel Holtmann 	 * the priority byte, the ident length byte and at least one string
1737ac714949SMarcel Holtmann 	 * terminator NUL byte. Anything shorter are invalid packets.
1738ac714949SMarcel Holtmann 	 */
173964ba2eb3SLuiz Augusto von Dentz 	if (skb->len < sizeof(*hdr) + 3)
1740ac714949SMarcel Holtmann 		return -EINVAL;
1741ac714949SMarcel Holtmann 
1742ac714949SMarcel Holtmann 	hdr = (void *)skb->data;
1743ac714949SMarcel Holtmann 
174464ba2eb3SLuiz Augusto von Dentz 	if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr))
174564ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1746ac714949SMarcel Holtmann 
1747ac714949SMarcel Holtmann 	if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1748ac714949SMarcel Holtmann 		__u8 priority = skb->data[sizeof(*hdr)];
1749ac714949SMarcel Holtmann 		__u8 ident_len = skb->data[sizeof(*hdr) + 1];
1750ac714949SMarcel Holtmann 
1751ac714949SMarcel Holtmann 		/* Only the priorities 0-7 are valid and with that any other
1752ac714949SMarcel Holtmann 		 * value results in an invalid packet.
1753ac714949SMarcel Holtmann 		 *
1754ac714949SMarcel Holtmann 		 * The priority byte is followed by an ident length byte and
1755ac714949SMarcel Holtmann 		 * the NUL terminated ident string. Check that the ident
1756ac714949SMarcel Holtmann 		 * length is not overflowing the packet and also that the
1757ac714949SMarcel Holtmann 		 * ident string itself is NUL terminated. In case the ident
1758ac714949SMarcel Holtmann 		 * length is zero, the length value actually doubles as NUL
1759ac714949SMarcel Holtmann 		 * terminator identifier.
1760ac714949SMarcel Holtmann 		 *
1761ac714949SMarcel Holtmann 		 * The message follows the ident string (if present) and
1762ac714949SMarcel Holtmann 		 * must be NUL terminated. Otherwise it is not a valid packet.
1763ac714949SMarcel Holtmann 		 */
176464ba2eb3SLuiz Augusto von Dentz 		if (priority > 7 || skb->data[skb->len - 1] != 0x00 ||
176564ba2eb3SLuiz Augusto von Dentz 		    ident_len > skb->len - sizeof(*hdr) - 3 ||
176664ba2eb3SLuiz Augusto von Dentz 		    skb->data[sizeof(*hdr) + ident_len + 1] != 0x00)
176764ba2eb3SLuiz Augusto von Dentz 			return -EINVAL;
1768ac714949SMarcel Holtmann 	} else {
176964ba2eb3SLuiz Augusto von Dentz 		return -EINVAL;
1770ac714949SMarcel Holtmann 	}
1771ac714949SMarcel Holtmann 
1772ac714949SMarcel Holtmann 	index = __le16_to_cpu(hdr->index);
1773ac714949SMarcel Holtmann 
1774ac714949SMarcel Holtmann 	if (index != MGMT_INDEX_NONE) {
1775ac714949SMarcel Holtmann 		hdev = hci_dev_get(index);
177664ba2eb3SLuiz Augusto von Dentz 		if (!hdev)
177764ba2eb3SLuiz Augusto von Dentz 			return -ENODEV;
1778ac714949SMarcel Holtmann 	} else {
1779ac714949SMarcel Holtmann 		hdev = NULL;
1780ac714949SMarcel Holtmann 	}
1781ac714949SMarcel Holtmann 
1782ac714949SMarcel Holtmann 	hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1783ac714949SMarcel Holtmann 
1784ac714949SMarcel Holtmann 	hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
178564ba2eb3SLuiz Augusto von Dentz 	err = skb->len;
1786ac714949SMarcel Holtmann 
1787ac714949SMarcel Holtmann 	if (hdev)
1788ac714949SMarcel Holtmann 		hci_dev_put(hdev);
1789ac714949SMarcel Holtmann 
1790ac714949SMarcel Holtmann 	return err;
1791ac714949SMarcel Holtmann }
1792ac714949SMarcel Holtmann 
17931b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
17941b784140SYing Xue 			    size_t len)
17951da177e4SLinus Torvalds {
17961da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1797801c1e8dSJohan Hedberg 	struct hci_mgmt_chan *chan;
17981da177e4SLinus Torvalds 	struct hci_dev *hdev;
17991da177e4SLinus Torvalds 	struct sk_buff *skb;
18001da177e4SLinus Torvalds 	int err;
18010b59e272STetsuo Handa 	const unsigned int flags = msg->msg_flags;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
18041da177e4SLinus Torvalds 
18050b59e272STetsuo Handa 	if (flags & MSG_OOB)
18061da177e4SLinus Torvalds 		return -EOPNOTSUPP;
18071da177e4SLinus Torvalds 
18080b59e272STetsuo Handa 	if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
18091da177e4SLinus Torvalds 		return -EINVAL;
18101da177e4SLinus Torvalds 
181109572fcaSLuiz Augusto von Dentz 	if (len < 4 || len > hci_pi(sk)->mtu)
18121da177e4SLinus Torvalds 		return -EINVAL;
18131da177e4SLinus Torvalds 
181464ba2eb3SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
181564ba2eb3SLuiz Augusto von Dentz 	if (IS_ERR(skb))
181664ba2eb3SLuiz Augusto von Dentz 		return PTR_ERR(skb);
18170b59e272STetsuo Handa 
18181da177e4SLinus Torvalds 	lock_sock(sk);
18191da177e4SLinus Torvalds 
18200381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
18210381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
182223500189SMarcel Holtmann 	case HCI_CHANNEL_USER:
18230381101fSJohan Hedberg 		break;
1824cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
1825cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
182664ba2eb3SLuiz Augusto von Dentz 		goto drop;
1827ac714949SMarcel Holtmann 	case HCI_CHANNEL_LOGGING:
182864ba2eb3SLuiz Augusto von Dentz 		err = hci_logging_frame(sk, skb, flags);
182964ba2eb3SLuiz Augusto von Dentz 		goto drop;
18300381101fSJohan Hedberg 	default:
1831801c1e8dSJohan Hedberg 		mutex_lock(&mgmt_chan_list_lock);
1832801c1e8dSJohan Hedberg 		chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1833801c1e8dSJohan Hedberg 		if (chan)
183464ba2eb3SLuiz Augusto von Dentz 			err = hci_mgmt_cmd(chan, sk, skb);
1835801c1e8dSJohan Hedberg 		else
18360381101fSJohan Hedberg 			err = -EINVAL;
1837801c1e8dSJohan Hedberg 
1838801c1e8dSJohan Hedberg 		mutex_unlock(&mgmt_chan_list_lock);
183964ba2eb3SLuiz Augusto von Dentz 		goto drop;
18400381101fSJohan Hedberg 	}
18410381101fSJohan Hedberg 
1842e0448092STetsuo Handa 	hdev = hci_hdev_from_sock(sk);
1843e0448092STetsuo Handa 	if (IS_ERR(hdev)) {
1844e0448092STetsuo Handa 		err = PTR_ERR(hdev);
184564ba2eb3SLuiz Augusto von Dentz 		goto drop;
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 
18487e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
18497e21addcSMarcel Holtmann 		err = -ENETDOWN;
185064ba2eb3SLuiz Augusto von Dentz 		goto drop;
18517e21addcSMarcel Holtmann 	}
18527e21addcSMarcel Holtmann 
18538528d3f7SMarcel Holtmann 	hci_skb_pkt_type(skb) = skb->data[0];
18541da177e4SLinus Torvalds 	skb_pull(skb, 1);
18551da177e4SLinus Torvalds 
18561bc5ad16SMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
18571bc5ad16SMarcel Holtmann 		/* No permission check is needed for user channel
18581bc5ad16SMarcel Holtmann 		 * since that gets enforced when binding the socket.
18591bc5ad16SMarcel Holtmann 		 *
18601bc5ad16SMarcel Holtmann 		 * However check that the packet type is valid.
18611bc5ad16SMarcel Holtmann 		 */
1862d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1863d79f34e3SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1864cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1865cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
18661bc5ad16SMarcel Holtmann 			err = -EINVAL;
18671bc5ad16SMarcel Holtmann 			goto drop;
18681bc5ad16SMarcel Holtmann 		}
18691bc5ad16SMarcel Holtmann 
18701bc5ad16SMarcel Holtmann 		skb_queue_tail(&hdev->raw_q, skb);
18711bc5ad16SMarcel Holtmann 		queue_work(hdev->workqueue, &hdev->tx_work);
1872d79f34e3SMarcel Holtmann 	} else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
187383985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
18741da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
18751da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
18783bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
18793bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
18801da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
18811da177e4SLinus Torvalds 			err = -EPERM;
18821da177e4SLinus Torvalds 			goto drop;
18831da177e4SLinus Torvalds 		}
18841da177e4SLinus Torvalds 
18851982162bSMarcel Holtmann 		/* Since the opcode has already been extracted here, store
18861982162bSMarcel Holtmann 		 * a copy of the value for later use by the drivers.
18871982162bSMarcel Holtmann 		 */
18881982162bSMarcel Holtmann 		hci_skb_opcode(skb) = opcode;
18891982162bSMarcel Holtmann 
1890fee746b0SMarcel Holtmann 		if (ogf == 0x3f) {
18911da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
18923eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
18931da177e4SLinus Torvalds 		} else {
189449c922bbSStephen Hemminger 			/* Stand-alone HCI commands must be flagged as
189511714b3dSJohan Hedberg 			 * single-command requests.
189611714b3dSJohan Hedberg 			 */
189744d27137SJohan Hedberg 			bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
189811714b3dSJohan Hedberg 
18991da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
1900c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
19011da177e4SLinus Torvalds 		}
19021da177e4SLinus Torvalds 	} else {
19031da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
19041da177e4SLinus Torvalds 			err = -EPERM;
19051da177e4SLinus Torvalds 			goto drop;
19061da177e4SLinus Torvalds 		}
19071da177e4SLinus Torvalds 
1908d79f34e3SMarcel Holtmann 		if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1909cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1910cc974003SMarcel Holtmann 		    hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
1911bb77543eSMarcel Holtmann 			err = -EINVAL;
1912bb77543eSMarcel Holtmann 			goto drop;
1913bb77543eSMarcel Holtmann 		}
1914bb77543eSMarcel Holtmann 
19151da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
19163eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	err = len;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds done:
19221da177e4SLinus Torvalds 	release_sock(sk);
19231da177e4SLinus Torvalds 	return err;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds drop:
19261da177e4SLinus Torvalds 	kfree_skb(skb);
19271da177e4SLinus Torvalds 	goto done;
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
193009572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname,
1931a7b75c5aSChristoph Hellwig 				   sockptr_t optval, unsigned int len)
19321da177e4SLinus Torvalds {
19331da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
19341da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
19351da177e4SLinus Torvalds 	int err = 0, opt = 0;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	lock_sock(sk);
19401da177e4SLinus Torvalds 
19412f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1942c2371e80SMarcel Holtmann 		err = -EBADFD;
19432f39cdb7SMarcel Holtmann 		goto done;
19442f39cdb7SMarcel Holtmann 	}
19452f39cdb7SMarcel Holtmann 
19461da177e4SLinus Torvalds 	switch (optname) {
19471da177e4SLinus Torvalds 	case HCI_DATA_DIR:
1948a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
19491da177e4SLinus Torvalds 			err = -EFAULT;
19501da177e4SLinus Torvalds 			break;
19511da177e4SLinus Torvalds 		}
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 		if (opt)
19541da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
19551da177e4SLinus Torvalds 		else
19561da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
19571da177e4SLinus Torvalds 		break;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
1960a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
19611da177e4SLinus Torvalds 			err = -EFAULT;
19621da177e4SLinus Torvalds 			break;
19631da177e4SLinus Torvalds 		}
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 		if (opt)
19661da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
19671da177e4SLinus Torvalds 		else
19681da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
19691da177e4SLinus Torvalds 		break;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	case HCI_FILTER:
19720878b666SMarcel Holtmann 		{
19730878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
19740878b666SMarcel Holtmann 
19750878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
19760878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
19770878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
19780878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
19790878b666SMarcel Holtmann 		}
19800878b666SMarcel Holtmann 
19811da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
1982a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&uf, optval, len)) {
19831da177e4SLinus Torvalds 			err = -EFAULT;
19841da177e4SLinus Torvalds 			break;
19851da177e4SLinus Torvalds 		}
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
19881da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
19891da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
19901da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
19911da177e4SLinus Torvalds 		}
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 		{
19941da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
19971da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
19981da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
19991da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
20001da177e4SLinus Torvalds 		}
20011da177e4SLinus Torvalds 		break;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	default:
20041da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
20051da177e4SLinus Torvalds 		break;
20061da177e4SLinus Torvalds 	}
20071da177e4SLinus Torvalds 
20082f39cdb7SMarcel Holtmann done:
20091da177e4SLinus Torvalds 	release_sock(sk);
20101da177e4SLinus Torvalds 	return err;
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
201309572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
201409572fcaSLuiz Augusto von Dentz 			       sockptr_t optval, unsigned int len)
201509572fcaSLuiz Augusto von Dentz {
201609572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
2017b9f9dbadSDan Carpenter 	int err = 0;
2018b9f9dbadSDan Carpenter 	u16 opt;
201909572fcaSLuiz Augusto von Dentz 
202009572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
202109572fcaSLuiz Augusto von Dentz 
202209572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
202309572fcaSLuiz Augusto von Dentz 		return hci_sock_setsockopt_old(sock, level, optname, optval,
202409572fcaSLuiz Augusto von Dentz 					       len);
202509572fcaSLuiz Augusto von Dentz 
202609572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
202709572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
202809572fcaSLuiz Augusto von Dentz 
202909572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
203009572fcaSLuiz Augusto von Dentz 
203109572fcaSLuiz Augusto von Dentz 	switch (optname) {
203209572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
203309572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
203409572fcaSLuiz Augusto von Dentz 		switch (hci_pi(sk)->channel) {
203509572fcaSLuiz Augusto von Dentz 		/* Don't allow changing MTU for channels that are meant for HCI
203609572fcaSLuiz Augusto von Dentz 		 * traffic only.
203709572fcaSLuiz Augusto von Dentz 		 */
203809572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_RAW:
203909572fcaSLuiz Augusto von Dentz 		case HCI_CHANNEL_USER:
204009572fcaSLuiz Augusto von Dentz 			err = -ENOPROTOOPT;
204109572fcaSLuiz Augusto von Dentz 			goto done;
204209572fcaSLuiz Augusto von Dentz 		}
204309572fcaSLuiz Augusto von Dentz 
2044b9f9dbadSDan Carpenter 		if (copy_from_sockptr(&opt, optval, sizeof(opt))) {
204509572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
204609572fcaSLuiz Augusto von Dentz 			break;
204709572fcaSLuiz Augusto von Dentz 		}
204809572fcaSLuiz Augusto von Dentz 
204909572fcaSLuiz Augusto von Dentz 		hci_pi(sk)->mtu = opt;
205009572fcaSLuiz Augusto von Dentz 		break;
205109572fcaSLuiz Augusto von Dentz 
205209572fcaSLuiz Augusto von Dentz 	default:
205309572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
205409572fcaSLuiz Augusto von Dentz 		break;
205509572fcaSLuiz Augusto von Dentz 	}
205609572fcaSLuiz Augusto von Dentz 
205709572fcaSLuiz Augusto von Dentz done:
205809572fcaSLuiz Augusto von Dentz 	release_sock(sk);
205909572fcaSLuiz Augusto von Dentz 	return err;
206009572fcaSLuiz Augusto von Dentz }
206109572fcaSLuiz Augusto von Dentz 
206209572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
20638fc9ced3SGustavo Padovan 				   char __user *optval, int __user *optlen)
20641da177e4SLinus Torvalds {
20651da177e4SLinus Torvalds 	struct hci_ufilter uf;
20661da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
2067cedc5469SMarcel Holtmann 	int len, opt, err = 0;
2068cedc5469SMarcel Holtmann 
2069cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	if (get_user(len, optlen))
20721da177e4SLinus Torvalds 		return -EFAULT;
20731da177e4SLinus Torvalds 
2074cedc5469SMarcel Holtmann 	lock_sock(sk);
2075cedc5469SMarcel Holtmann 
2076cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
2077c2371e80SMarcel Holtmann 		err = -EBADFD;
2078cedc5469SMarcel Holtmann 		goto done;
2079cedc5469SMarcel Holtmann 	}
2080cedc5469SMarcel Holtmann 
20811da177e4SLinus Torvalds 	switch (optname) {
20821da177e4SLinus Torvalds 	case HCI_DATA_DIR:
20831da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
20841da177e4SLinus Torvalds 			opt = 1;
20851da177e4SLinus Torvalds 		else
20861da177e4SLinus Torvalds 			opt = 0;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 		if (put_user(opt, optval))
2089cedc5469SMarcel Holtmann 			err = -EFAULT;
20901da177e4SLinus Torvalds 		break;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
20931da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
20941da177e4SLinus Torvalds 			opt = 1;
20951da177e4SLinus Torvalds 		else
20961da177e4SLinus Torvalds 			opt = 0;
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 		if (put_user(opt, optval))
2099cedc5469SMarcel Holtmann 			err = -EFAULT;
21001da177e4SLinus Torvalds 		break;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	case HCI_FILTER:
21031da177e4SLinus Torvalds 		{
21041da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
21051da177e4SLinus Torvalds 
2106e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
21071da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
21081da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
21091da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
21101da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
21111da177e4SLinus Torvalds 		}
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
21141da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
2115cedc5469SMarcel Holtmann 			err = -EFAULT;
21161da177e4SLinus Torvalds 		break;
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	default:
2119cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
21201da177e4SLinus Torvalds 		break;
21211da177e4SLinus Torvalds 	}
21221da177e4SLinus Torvalds 
2123cedc5469SMarcel Holtmann done:
2124cedc5469SMarcel Holtmann 	release_sock(sk);
2125cedc5469SMarcel Holtmann 	return err;
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
212809572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
212909572fcaSLuiz Augusto von Dentz 			       char __user *optval, int __user *optlen)
213009572fcaSLuiz Augusto von Dentz {
213109572fcaSLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
213209572fcaSLuiz Augusto von Dentz 	int err = 0;
213309572fcaSLuiz Augusto von Dentz 
213409572fcaSLuiz Augusto von Dentz 	BT_DBG("sk %p, opt %d", sk, optname);
213509572fcaSLuiz Augusto von Dentz 
213609572fcaSLuiz Augusto von Dentz 	if (level == SOL_HCI)
213709572fcaSLuiz Augusto von Dentz 		return hci_sock_getsockopt_old(sock, level, optname, optval,
213809572fcaSLuiz Augusto von Dentz 					       optlen);
213909572fcaSLuiz Augusto von Dentz 
214009572fcaSLuiz Augusto von Dentz 	if (level != SOL_BLUETOOTH)
214109572fcaSLuiz Augusto von Dentz 		return -ENOPROTOOPT;
214209572fcaSLuiz Augusto von Dentz 
214309572fcaSLuiz Augusto von Dentz 	lock_sock(sk);
214409572fcaSLuiz Augusto von Dentz 
214509572fcaSLuiz Augusto von Dentz 	switch (optname) {
214609572fcaSLuiz Augusto von Dentz 	case BT_SNDMTU:
214709572fcaSLuiz Augusto von Dentz 	case BT_RCVMTU:
214809572fcaSLuiz Augusto von Dentz 		if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval))
214909572fcaSLuiz Augusto von Dentz 			err = -EFAULT;
215009572fcaSLuiz Augusto von Dentz 		break;
215109572fcaSLuiz Augusto von Dentz 
215209572fcaSLuiz Augusto von Dentz 	default:
215309572fcaSLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
215409572fcaSLuiz Augusto von Dentz 		break;
215509572fcaSLuiz Augusto von Dentz 	}
215609572fcaSLuiz Augusto von Dentz 
215709572fcaSLuiz Augusto von Dentz 	release_sock(sk);
215809572fcaSLuiz Augusto von Dentz 	return err;
215909572fcaSLuiz Augusto von Dentz }
216009572fcaSLuiz Augusto von Dentz 
2161709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk)
2162709fca50SNguyen Dinh Phi {
2163b338d917SBrian Gix 	mgmt_cleanup(sk);
2164709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_receive_queue);
2165709fca50SNguyen Dinh Phi 	skb_queue_purge(&sk->sk_write_queue);
2166709fca50SNguyen Dinh Phi }
2167709fca50SNguyen Dinh Phi 
216890ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
21691da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
21701da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21711da177e4SLinus Torvalds 	.release	= hci_sock_release,
21721da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
21731da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
21741da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
21751da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
21761da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
21777a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
21787a6038b3SArnd Bergmann 	.compat_ioctl	= hci_sock_compat_ioctl,
21797a6038b3SArnd Bergmann #endif
2180a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
21811da177e4SLinus Torvalds 	.listen		= sock_no_listen,
21821da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
21831da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
21841da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
21851da177e4SLinus Torvalds 	.connect	= sock_no_connect,
21861da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
21871da177e4SLinus Torvalds 	.accept		= sock_no_accept,
21881da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
21891da177e4SLinus Torvalds };
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds static struct proto hci_sk_proto = {
21921da177e4SLinus Torvalds 	.name		= "HCI",
21931da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21941da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
21951da177e4SLinus Torvalds };
21961da177e4SLinus Torvalds 
21973f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
21983f378b68SEric Paris 			   int kern)
21991da177e4SLinus Torvalds {
22001da177e4SLinus Torvalds 	struct sock *sk;
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
22051da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
22081da177e4SLinus Torvalds 
22096bfa273eSLuiz Augusto von Dentz 	sk = bt_sock_alloc(net, sock, &hci_sk_proto, protocol, GFP_ATOMIC,
22106bfa273eSLuiz Augusto von Dentz 			   kern);
22111da177e4SLinus Torvalds 	if (!sk)
22121da177e4SLinus Torvalds 		return -ENOMEM;
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
2215709fca50SNguyen Dinh Phi 	sk->sk_destruct = hci_sock_destruct;
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
22181da177e4SLinus Torvalds 	return 0;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
2221ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
22221da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
22231da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
22241da177e4SLinus Torvalds 	.create	= hci_sock_create,
22251da177e4SLinus Torvalds };
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds int __init hci_sock_init(void)
22281da177e4SLinus Torvalds {
22291da177e4SLinus Torvalds 	int err;
22301da177e4SLinus Torvalds 
2231b0a8e282SMarcel Holtmann 	BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2232b0a8e282SMarcel Holtmann 
22331da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
22341da177e4SLinus Torvalds 	if (err < 0)
22351da177e4SLinus Torvalds 		return err;
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
2238f7c86637SMasatake YAMATO 	if (err < 0) {
2239f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
22401da177e4SLinus Torvalds 		goto error;
2241f7c86637SMasatake YAMATO 	}
2242f7c86637SMasatake YAMATO 
2243b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
2244f7c86637SMasatake YAMATO 	if (err < 0) {
2245f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
2246f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
2247f7c86637SMasatake YAMATO 		goto error;
2248f7c86637SMasatake YAMATO 	}
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	return 0;
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds error:
22551da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
22561da177e4SLinus Torvalds 	return err;
22571da177e4SLinus Torvalds }
22581da177e4SLinus Torvalds 
2259b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
22601da177e4SLinus Torvalds {
2261f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
22625e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
22631da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
22641da177e4SLinus Torvalds }
2265