xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision 0736cfa8e5bb7ee1d7b7d28aabe634fd3f85cb92)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds    Copyright (C) 2000-2001 Qualcomm Incorporated
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds    This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds    it under the terms of the GNU General Public License version 2 as
91da177e4SLinus Torvalds    published by the Free Software Foundation;
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
121da177e4SLinus Torvalds    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131da177e4SLinus Torvalds    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
141da177e4SLinus Torvalds    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
151da177e4SLinus Torvalds    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
161da177e4SLinus Torvalds    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
171da177e4SLinus Torvalds    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
181da177e4SLinus Torvalds    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
211da177e4SLinus Torvalds    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
221da177e4SLinus Torvalds    SOFTWARE IS DISCLAIMED.
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* Bluetooth HCI sockets. */
261da177e4SLinus Torvalds 
278c520a59SGustavo Padovan #include <linux/export.h>
281da177e4SLinus Torvalds #include <asm/unaligned.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
311da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
32cd82e61cSMarcel Holtmann #include <net/bluetooth/hci_mon.h>
331da177e4SLinus Torvalds 
34cd82e61cSMarcel Holtmann static atomic_t monitor_promisc = ATOMIC_INIT(0);
35cd82e61cSMarcel Holtmann 
361da177e4SLinus Torvalds /* ----- HCI socket interface ----- */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static inline int hci_test_bit(int nr, void *addr)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* Security filter */
441da177e4SLinus Torvalds static struct hci_sec_filter hci_sec_filter = {
451da177e4SLinus Torvalds 	/* Packet types */
461da177e4SLinus Torvalds 	0x10,
471da177e4SLinus Torvalds 	/* Events */
48dd7f5527SMarcel Holtmann 	{ 0x1000d9fe, 0x0000b00c },
491da177e4SLinus Torvalds 	/* Commands */
501da177e4SLinus Torvalds 	{
511da177e4SLinus Torvalds 		{ 0x0 },
521da177e4SLinus Torvalds 		/* OGF_LINK_CTL */
537c631a67SMarcel Holtmann 		{ 0xbe000006, 0x00000001, 0x00000000, 0x00 },
541da177e4SLinus Torvalds 		/* OGF_LINK_POLICY */
557c631a67SMarcel Holtmann 		{ 0x00005200, 0x00000000, 0x00000000, 0x00 },
561da177e4SLinus Torvalds 		/* OGF_HOST_CTL */
577c631a67SMarcel Holtmann 		{ 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
581da177e4SLinus Torvalds 		/* OGF_INFO_PARAM */
597c631a67SMarcel Holtmann 		{ 0x000002be, 0x00000000, 0x00000000, 0x00 },
601da177e4SLinus Torvalds 		/* OGF_STATUS_PARAM */
617c631a67SMarcel Holtmann 		{ 0x000000ea, 0x00000000, 0x00000000, 0x00 }
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static struct bt_sock_list hci_sk_list = {
66d5fb2962SRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
69f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
70f81fe64fSMarcel Holtmann {
71f81fe64fSMarcel Holtmann 	struct hci_filter *flt;
72f81fe64fSMarcel Holtmann 	int flt_type, flt_event;
73f81fe64fSMarcel Holtmann 
74f81fe64fSMarcel Holtmann 	/* Apply filter */
75f81fe64fSMarcel Holtmann 	flt = &hci_pi(sk)->filter;
76f81fe64fSMarcel Holtmann 
77f81fe64fSMarcel Holtmann 	if (bt_cb(skb)->pkt_type == HCI_VENDOR_PKT)
78f81fe64fSMarcel Holtmann 		flt_type = 0;
79f81fe64fSMarcel Holtmann 	else
80f81fe64fSMarcel Holtmann 		flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS;
81f81fe64fSMarcel Holtmann 
82f81fe64fSMarcel Holtmann 	if (!test_bit(flt_type, &flt->type_mask))
83f81fe64fSMarcel Holtmann 		return true;
84f81fe64fSMarcel Holtmann 
85f81fe64fSMarcel Holtmann 	/* Extra filter for event packets only */
86f81fe64fSMarcel Holtmann 	if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT)
87f81fe64fSMarcel Holtmann 		return false;
88f81fe64fSMarcel Holtmann 
89f81fe64fSMarcel Holtmann 	flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
90f81fe64fSMarcel Holtmann 
91f81fe64fSMarcel Holtmann 	if (!hci_test_bit(flt_event, &flt->event_mask))
92f81fe64fSMarcel Holtmann 		return true;
93f81fe64fSMarcel Holtmann 
94f81fe64fSMarcel Holtmann 	/* Check filter only when opcode is set */
95f81fe64fSMarcel Holtmann 	if (!flt->opcode)
96f81fe64fSMarcel Holtmann 		return false;
97f81fe64fSMarcel Holtmann 
98f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_COMPLETE &&
99f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
100f81fe64fSMarcel Holtmann 		return true;
101f81fe64fSMarcel Holtmann 
102f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_STATUS &&
103f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
104f81fe64fSMarcel Holtmann 		return true;
105f81fe64fSMarcel Holtmann 
106f81fe64fSMarcel Holtmann 	return false;
107f81fe64fSMarcel Holtmann }
108f81fe64fSMarcel Holtmann 
1091da177e4SLinus Torvalds /* Send frame to RAW socket */
110470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	struct sock *sk;
113e0edf373SMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	BT_DBG("hdev %p len %d", hdev, skb->len);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	read_lock(&hci_sk_list.lock);
118470fe1b5SMarcel Holtmann 
119b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
1201da177e4SLinus Torvalds 		struct sk_buff *nskb;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 		if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
1231da177e4SLinus Torvalds 			continue;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 		/* Don't send frame to the socket it came from */
1261da177e4SLinus Torvalds 		if (skb->sk == sk)
1271da177e4SLinus Torvalds 			continue;
1281da177e4SLinus Torvalds 
129470fe1b5SMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_RAW)
130a40c406cSJohan Hedberg 			continue;
131a40c406cSJohan Hedberg 
132f81fe64fSMarcel Holtmann 		if (is_filtered_packet(sk, skb))
1331da177e4SLinus Torvalds 			continue;
1341da177e4SLinus Torvalds 
135e0edf373SMarcel Holtmann 		if (!skb_copy) {
136e0edf373SMarcel Holtmann 			/* Create a private copy with headroom */
137e0edf373SMarcel Holtmann 			skb_copy = __pskb_copy(skb, 1, GFP_ATOMIC);
138e0edf373SMarcel Holtmann 			if (!skb_copy)
1391da177e4SLinus Torvalds 				continue;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 			/* Put type byte before the data */
142e0edf373SMarcel Holtmann 			memcpy(skb_push(skb_copy, 1), &bt_cb(skb)->pkt_type, 1);
143e0edf373SMarcel Holtmann 		}
144e0edf373SMarcel Holtmann 
145e0edf373SMarcel Holtmann 		nskb = skb_clone(skb_copy, GFP_ATOMIC);
146e0edf373SMarcel Holtmann 		if (!nskb)
147e0edf373SMarcel Holtmann 			continue;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		if (sock_queue_rcv_skb(sk, nskb))
1501da177e4SLinus Torvalds 			kfree_skb(nskb);
1511da177e4SLinus Torvalds 	}
152470fe1b5SMarcel Holtmann 
153470fe1b5SMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
154e0edf373SMarcel Holtmann 
155e0edf373SMarcel Holtmann 	kfree_skb(skb_copy);
156470fe1b5SMarcel Holtmann }
157470fe1b5SMarcel Holtmann 
158470fe1b5SMarcel Holtmann /* Send frame to control socket */
159470fe1b5SMarcel Holtmann void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk)
160470fe1b5SMarcel Holtmann {
161470fe1b5SMarcel Holtmann 	struct sock *sk;
162470fe1b5SMarcel Holtmann 
163470fe1b5SMarcel Holtmann 	BT_DBG("len %d", skb->len);
164470fe1b5SMarcel Holtmann 
165470fe1b5SMarcel Holtmann 	read_lock(&hci_sk_list.lock);
166470fe1b5SMarcel Holtmann 
167b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
168470fe1b5SMarcel Holtmann 		struct sk_buff *nskb;
169470fe1b5SMarcel Holtmann 
170470fe1b5SMarcel Holtmann 		/* Skip the original socket */
171470fe1b5SMarcel Holtmann 		if (sk == skip_sk)
172470fe1b5SMarcel Holtmann 			continue;
173470fe1b5SMarcel Holtmann 
174470fe1b5SMarcel Holtmann 		if (sk->sk_state != BT_BOUND)
175470fe1b5SMarcel Holtmann 			continue;
176470fe1b5SMarcel Holtmann 
177470fe1b5SMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
178470fe1b5SMarcel Holtmann 			continue;
179470fe1b5SMarcel Holtmann 
180470fe1b5SMarcel Holtmann 		nskb = skb_clone(skb, GFP_ATOMIC);
181470fe1b5SMarcel Holtmann 		if (!nskb)
182470fe1b5SMarcel Holtmann 			continue;
183470fe1b5SMarcel Holtmann 
184470fe1b5SMarcel Holtmann 		if (sock_queue_rcv_skb(sk, nskb))
185470fe1b5SMarcel Holtmann 			kfree_skb(nskb);
186470fe1b5SMarcel Holtmann 	}
187470fe1b5SMarcel Holtmann 
1881da177e4SLinus Torvalds 	read_unlock(&hci_sk_list.lock);
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191cd82e61cSMarcel Holtmann /* Send frame to monitor socket */
192cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
193cd82e61cSMarcel Holtmann {
194cd82e61cSMarcel Holtmann 	struct sock *sk;
195cd82e61cSMarcel Holtmann 	struct sk_buff *skb_copy = NULL;
196cd82e61cSMarcel Holtmann 	__le16 opcode;
197cd82e61cSMarcel Holtmann 
198cd82e61cSMarcel Holtmann 	if (!atomic_read(&monitor_promisc))
199cd82e61cSMarcel Holtmann 		return;
200cd82e61cSMarcel Holtmann 
201cd82e61cSMarcel Holtmann 	BT_DBG("hdev %p len %d", hdev, skb->len);
202cd82e61cSMarcel Holtmann 
203cd82e61cSMarcel Holtmann 	switch (bt_cb(skb)->pkt_type) {
204cd82e61cSMarcel Holtmann 	case HCI_COMMAND_PKT:
205cd82e61cSMarcel Holtmann 		opcode = __constant_cpu_to_le16(HCI_MON_COMMAND_PKT);
206cd82e61cSMarcel Holtmann 		break;
207cd82e61cSMarcel Holtmann 	case HCI_EVENT_PKT:
208cd82e61cSMarcel Holtmann 		opcode = __constant_cpu_to_le16(HCI_MON_EVENT_PKT);
209cd82e61cSMarcel Holtmann 		break;
210cd82e61cSMarcel Holtmann 	case HCI_ACLDATA_PKT:
211cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
212cd82e61cSMarcel Holtmann 			opcode = __constant_cpu_to_le16(HCI_MON_ACL_RX_PKT);
213cd82e61cSMarcel Holtmann 		else
214cd82e61cSMarcel Holtmann 			opcode = __constant_cpu_to_le16(HCI_MON_ACL_TX_PKT);
215cd82e61cSMarcel Holtmann 		break;
216cd82e61cSMarcel Holtmann 	case HCI_SCODATA_PKT:
217cd82e61cSMarcel Holtmann 		if (bt_cb(skb)->incoming)
218cd82e61cSMarcel Holtmann 			opcode = __constant_cpu_to_le16(HCI_MON_SCO_RX_PKT);
219cd82e61cSMarcel Holtmann 		else
220cd82e61cSMarcel Holtmann 			opcode = __constant_cpu_to_le16(HCI_MON_SCO_TX_PKT);
221cd82e61cSMarcel Holtmann 		break;
222cd82e61cSMarcel Holtmann 	default:
223cd82e61cSMarcel Holtmann 		return;
224cd82e61cSMarcel Holtmann 	}
225cd82e61cSMarcel Holtmann 
226cd82e61cSMarcel Holtmann 	read_lock(&hci_sk_list.lock);
227cd82e61cSMarcel Holtmann 
228b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
229cd82e61cSMarcel Holtmann 		struct sk_buff *nskb;
230cd82e61cSMarcel Holtmann 
231cd82e61cSMarcel Holtmann 		if (sk->sk_state != BT_BOUND)
232cd82e61cSMarcel Holtmann 			continue;
233cd82e61cSMarcel Holtmann 
234cd82e61cSMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_MONITOR)
235cd82e61cSMarcel Holtmann 			continue;
236cd82e61cSMarcel Holtmann 
237cd82e61cSMarcel Holtmann 		if (!skb_copy) {
238cd82e61cSMarcel Holtmann 			struct hci_mon_hdr *hdr;
239cd82e61cSMarcel Holtmann 
240cd82e61cSMarcel Holtmann 			/* Create a private copy with headroom */
2418fc9ced3SGustavo Padovan 			skb_copy = __pskb_copy(skb, HCI_MON_HDR_SIZE,
2428fc9ced3SGustavo Padovan 					       GFP_ATOMIC);
243cd82e61cSMarcel Holtmann 			if (!skb_copy)
244cd82e61cSMarcel Holtmann 				continue;
245cd82e61cSMarcel Holtmann 
246cd82e61cSMarcel Holtmann 			/* Put header before the data */
247cd82e61cSMarcel Holtmann 			hdr = (void *) skb_push(skb_copy, HCI_MON_HDR_SIZE);
248cd82e61cSMarcel Holtmann 			hdr->opcode = opcode;
249cd82e61cSMarcel Holtmann 			hdr->index = cpu_to_le16(hdev->id);
250cd82e61cSMarcel Holtmann 			hdr->len = cpu_to_le16(skb->len);
251cd82e61cSMarcel Holtmann 		}
252cd82e61cSMarcel Holtmann 
253cd82e61cSMarcel Holtmann 		nskb = skb_clone(skb_copy, GFP_ATOMIC);
254cd82e61cSMarcel Holtmann 		if (!nskb)
255cd82e61cSMarcel Holtmann 			continue;
256cd82e61cSMarcel Holtmann 
257cd82e61cSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, nskb))
258cd82e61cSMarcel Holtmann 			kfree_skb(nskb);
259cd82e61cSMarcel Holtmann 	}
260cd82e61cSMarcel Holtmann 
261cd82e61cSMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
262cd82e61cSMarcel Holtmann 
263cd82e61cSMarcel Holtmann 	kfree_skb(skb_copy);
264cd82e61cSMarcel Holtmann }
265cd82e61cSMarcel Holtmann 
266cd82e61cSMarcel Holtmann static void send_monitor_event(struct sk_buff *skb)
267cd82e61cSMarcel Holtmann {
268cd82e61cSMarcel Holtmann 	struct sock *sk;
269cd82e61cSMarcel Holtmann 
270cd82e61cSMarcel Holtmann 	BT_DBG("len %d", skb->len);
271cd82e61cSMarcel Holtmann 
272cd82e61cSMarcel Holtmann 	read_lock(&hci_sk_list.lock);
273cd82e61cSMarcel Holtmann 
274b67bfe0dSSasha Levin 	sk_for_each(sk, &hci_sk_list.head) {
275cd82e61cSMarcel Holtmann 		struct sk_buff *nskb;
276cd82e61cSMarcel Holtmann 
277cd82e61cSMarcel Holtmann 		if (sk->sk_state != BT_BOUND)
278cd82e61cSMarcel Holtmann 			continue;
279cd82e61cSMarcel Holtmann 
280cd82e61cSMarcel Holtmann 		if (hci_pi(sk)->channel != HCI_CHANNEL_MONITOR)
281cd82e61cSMarcel Holtmann 			continue;
282cd82e61cSMarcel Holtmann 
283cd82e61cSMarcel Holtmann 		nskb = skb_clone(skb, GFP_ATOMIC);
284cd82e61cSMarcel Holtmann 		if (!nskb)
285cd82e61cSMarcel Holtmann 			continue;
286cd82e61cSMarcel Holtmann 
287cd82e61cSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, nskb))
288cd82e61cSMarcel Holtmann 			kfree_skb(nskb);
289cd82e61cSMarcel Holtmann 	}
290cd82e61cSMarcel Holtmann 
291cd82e61cSMarcel Holtmann 	read_unlock(&hci_sk_list.lock);
292cd82e61cSMarcel Holtmann }
293cd82e61cSMarcel Holtmann 
294cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
295cd82e61cSMarcel Holtmann {
296cd82e61cSMarcel Holtmann 	struct hci_mon_hdr *hdr;
297cd82e61cSMarcel Holtmann 	struct hci_mon_new_index *ni;
298cd82e61cSMarcel Holtmann 	struct sk_buff *skb;
299cd82e61cSMarcel Holtmann 	__le16 opcode;
300cd82e61cSMarcel Holtmann 
301cd82e61cSMarcel Holtmann 	switch (event) {
302cd82e61cSMarcel Holtmann 	case HCI_DEV_REG:
303cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
304cd82e61cSMarcel Holtmann 		if (!skb)
305cd82e61cSMarcel Holtmann 			return NULL;
306cd82e61cSMarcel Holtmann 
307cd82e61cSMarcel Holtmann 		ni = (void *) skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
308cd82e61cSMarcel Holtmann 		ni->type = hdev->dev_type;
309cd82e61cSMarcel Holtmann 		ni->bus = hdev->bus;
310cd82e61cSMarcel Holtmann 		bacpy(&ni->bdaddr, &hdev->bdaddr);
311cd82e61cSMarcel Holtmann 		memcpy(ni->name, hdev->name, 8);
312cd82e61cSMarcel Holtmann 
313cd82e61cSMarcel Holtmann 		opcode = __constant_cpu_to_le16(HCI_MON_NEW_INDEX);
314cd82e61cSMarcel Holtmann 		break;
315cd82e61cSMarcel Holtmann 
316cd82e61cSMarcel Holtmann 	case HCI_DEV_UNREG:
317cd82e61cSMarcel Holtmann 		skb = bt_skb_alloc(0, GFP_ATOMIC);
318cd82e61cSMarcel Holtmann 		if (!skb)
319cd82e61cSMarcel Holtmann 			return NULL;
320cd82e61cSMarcel Holtmann 
321cd82e61cSMarcel Holtmann 		opcode = __constant_cpu_to_le16(HCI_MON_DEL_INDEX);
322cd82e61cSMarcel Holtmann 		break;
323cd82e61cSMarcel Holtmann 
324cd82e61cSMarcel Holtmann 	default:
325cd82e61cSMarcel Holtmann 		return NULL;
326cd82e61cSMarcel Holtmann 	}
327cd82e61cSMarcel Holtmann 
328cd82e61cSMarcel Holtmann 	__net_timestamp(skb);
329cd82e61cSMarcel Holtmann 
330cd82e61cSMarcel Holtmann 	hdr = (void *) skb_push(skb, HCI_MON_HDR_SIZE);
331cd82e61cSMarcel Holtmann 	hdr->opcode = opcode;
332cd82e61cSMarcel Holtmann 	hdr->index = cpu_to_le16(hdev->id);
333cd82e61cSMarcel Holtmann 	hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
334cd82e61cSMarcel Holtmann 
335cd82e61cSMarcel Holtmann 	return skb;
336cd82e61cSMarcel Holtmann }
337cd82e61cSMarcel Holtmann 
338cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk)
339cd82e61cSMarcel Holtmann {
340cd82e61cSMarcel Holtmann 	struct hci_dev *hdev;
341cd82e61cSMarcel Holtmann 
342cd82e61cSMarcel Holtmann 	read_lock(&hci_dev_list_lock);
343cd82e61cSMarcel Holtmann 
344cd82e61cSMarcel Holtmann 	list_for_each_entry(hdev, &hci_dev_list, list) {
345cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
346cd82e61cSMarcel Holtmann 
347cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, HCI_DEV_REG);
348cd82e61cSMarcel Holtmann 		if (!skb)
349cd82e61cSMarcel Holtmann 			continue;
350cd82e61cSMarcel Holtmann 
351cd82e61cSMarcel Holtmann 		if (sock_queue_rcv_skb(sk, skb))
352cd82e61cSMarcel Holtmann 			kfree_skb(skb);
353cd82e61cSMarcel Holtmann 	}
354cd82e61cSMarcel Holtmann 
355cd82e61cSMarcel Holtmann 	read_unlock(&hci_dev_list_lock);
356cd82e61cSMarcel Holtmann }
357cd82e61cSMarcel Holtmann 
358040030efSMarcel Holtmann /* Generate internal stack event */
359040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
360040030efSMarcel Holtmann {
361040030efSMarcel Holtmann 	struct hci_event_hdr *hdr;
362040030efSMarcel Holtmann 	struct hci_ev_stack_internal *ev;
363040030efSMarcel Holtmann 	struct sk_buff *skb;
364040030efSMarcel Holtmann 
365040030efSMarcel Holtmann 	skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
366040030efSMarcel Holtmann 	if (!skb)
367040030efSMarcel Holtmann 		return;
368040030efSMarcel Holtmann 
369040030efSMarcel Holtmann 	hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
370040030efSMarcel Holtmann 	hdr->evt  = HCI_EV_STACK_INTERNAL;
371040030efSMarcel Holtmann 	hdr->plen = sizeof(*ev) + dlen;
372040030efSMarcel Holtmann 
373040030efSMarcel Holtmann 	ev  = (void *) skb_put(skb, sizeof(*ev) + dlen);
374040030efSMarcel Holtmann 	ev->type = type;
375040030efSMarcel Holtmann 	memcpy(ev->data, data, dlen);
376040030efSMarcel Holtmann 
377040030efSMarcel Holtmann 	bt_cb(skb)->incoming = 1;
378040030efSMarcel Holtmann 	__net_timestamp(skb);
379040030efSMarcel Holtmann 
380040030efSMarcel Holtmann 	bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
381040030efSMarcel Holtmann 	skb->dev = (void *) hdev;
382040030efSMarcel Holtmann 	hci_send_to_sock(hdev, skb);
383040030efSMarcel Holtmann 	kfree_skb(skb);
384040030efSMarcel Holtmann }
385040030efSMarcel Holtmann 
386040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event)
387040030efSMarcel Holtmann {
388040030efSMarcel Holtmann 	struct hci_ev_si_device ev;
389040030efSMarcel Holtmann 
390040030efSMarcel Holtmann 	BT_DBG("hdev %s event %d", hdev->name, event);
391040030efSMarcel Holtmann 
392cd82e61cSMarcel Holtmann 	/* Send event to monitor */
393cd82e61cSMarcel Holtmann 	if (atomic_read(&monitor_promisc)) {
394cd82e61cSMarcel Holtmann 		struct sk_buff *skb;
395cd82e61cSMarcel Holtmann 
396cd82e61cSMarcel Holtmann 		skb = create_monitor_event(hdev, event);
397cd82e61cSMarcel Holtmann 		if (skb) {
398cd82e61cSMarcel Holtmann 			send_monitor_event(skb);
399cd82e61cSMarcel Holtmann 			kfree_skb(skb);
400cd82e61cSMarcel Holtmann 		}
401cd82e61cSMarcel Holtmann 	}
402cd82e61cSMarcel Holtmann 
403040030efSMarcel Holtmann 	/* Send event to sockets */
404040030efSMarcel Holtmann 	ev.event  = event;
405040030efSMarcel Holtmann 	ev.dev_id = hdev->id;
406040030efSMarcel Holtmann 	hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
407040030efSMarcel Holtmann 
408040030efSMarcel Holtmann 	if (event == HCI_DEV_UNREG) {
409040030efSMarcel Holtmann 		struct sock *sk;
410040030efSMarcel Holtmann 
411040030efSMarcel Holtmann 		/* Detach sockets from device */
412040030efSMarcel Holtmann 		read_lock(&hci_sk_list.lock);
413b67bfe0dSSasha Levin 		sk_for_each(sk, &hci_sk_list.head) {
414040030efSMarcel Holtmann 			bh_lock_sock_nested(sk);
415040030efSMarcel Holtmann 			if (hci_pi(sk)->hdev == hdev) {
416040030efSMarcel Holtmann 				hci_pi(sk)->hdev = NULL;
417040030efSMarcel Holtmann 				sk->sk_err = EPIPE;
418040030efSMarcel Holtmann 				sk->sk_state = BT_OPEN;
419040030efSMarcel Holtmann 				sk->sk_state_change(sk);
420040030efSMarcel Holtmann 
421040030efSMarcel Holtmann 				hci_dev_put(hdev);
422040030efSMarcel Holtmann 			}
423040030efSMarcel Holtmann 			bh_unlock_sock(sk);
424040030efSMarcel Holtmann 		}
425040030efSMarcel Holtmann 		read_unlock(&hci_sk_list.lock);
426040030efSMarcel Holtmann 	}
427040030efSMarcel Holtmann }
428040030efSMarcel Holtmann 
4291da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4327b005bd3SMarcel Holtmann 	struct hci_dev *hdev;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (!sk)
4371da177e4SLinus Torvalds 		return 0;
4381da177e4SLinus Torvalds 
4397b005bd3SMarcel Holtmann 	hdev = hci_pi(sk)->hdev;
4407b005bd3SMarcel Holtmann 
441cd82e61cSMarcel Holtmann 	if (hci_pi(sk)->channel == HCI_CHANNEL_MONITOR)
442cd82e61cSMarcel Holtmann 		atomic_dec(&monitor_promisc);
443cd82e61cSMarcel Holtmann 
4441da177e4SLinus Torvalds 	bt_sock_unlink(&hci_sk_list, sk);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (hdev) {
4471da177e4SLinus Torvalds 		atomic_dec(&hdev->promisc);
4481da177e4SLinus Torvalds 		hci_dev_put(hdev);
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	sock_orphan(sk);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	skb_queue_purge(&sk->sk_receive_queue);
4541da177e4SLinus Torvalds 	skb_queue_purge(&sk->sk_write_queue);
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	sock_put(sk);
4571da177e4SLinus Torvalds 	return 0;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
460b2a66aadSAntti Julku static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
461f0358568SJohan Hedberg {
462f0358568SJohan Hedberg 	bdaddr_t bdaddr;
4635e762444SAntti Julku 	int err;
464f0358568SJohan Hedberg 
465f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
466f0358568SJohan Hedberg 		return -EFAULT;
467f0358568SJohan Hedberg 
46809fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
4695e762444SAntti Julku 
47088c1fe4bSJohan Hedberg 	err = hci_blacklist_add(hdev, &bdaddr, 0);
4715e762444SAntti Julku 
47209fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
4735e762444SAntti Julku 
4745e762444SAntti Julku 	return err;
475f0358568SJohan Hedberg }
476f0358568SJohan Hedberg 
477b2a66aadSAntti Julku static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
478f0358568SJohan Hedberg {
479f0358568SJohan Hedberg 	bdaddr_t bdaddr;
4805e762444SAntti Julku 	int err;
481f0358568SJohan Hedberg 
482f0358568SJohan Hedberg 	if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
483f0358568SJohan Hedberg 		return -EFAULT;
484f0358568SJohan Hedberg 
48509fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
4865e762444SAntti Julku 
48788c1fe4bSJohan Hedberg 	err = hci_blacklist_del(hdev, &bdaddr, 0);
4885e762444SAntti Julku 
48909fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
4905e762444SAntti Julku 
4915e762444SAntti Julku 	return err;
492f0358568SJohan Hedberg }
493f0358568SJohan Hedberg 
4941da177e4SLinus Torvalds /* Ioctls that require bound socket */
4956039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
4966039aa73SGustavo Padovan 				unsigned long arg)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	struct hci_dev *hdev = hci_pi(sk)->hdev;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	if (!hdev)
5011da177e4SLinus Torvalds 		return -EBADFD;
5021da177e4SLinus Torvalds 
503*0736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
504*0736cfa8SMarcel Holtmann 		return -EBUSY;
505*0736cfa8SMarcel Holtmann 
5061da177e4SLinus Torvalds 	switch (cmd) {
5071da177e4SLinus Torvalds 	case HCISETRAW:
5081da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
509bf5b30b8SZhao Hongjiang 			return -EPERM;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
5121da177e4SLinus Torvalds 			return -EPERM;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		if (arg)
5151da177e4SLinus Torvalds 			set_bit(HCI_RAW, &hdev->flags);
5161da177e4SLinus Torvalds 		else
5171da177e4SLinus Torvalds 			clear_bit(HCI_RAW, &hdev->flags);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 		return 0;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	case HCIGETCONNINFO:
5221da177e4SLinus Torvalds 		return hci_get_conn_info(hdev, (void __user *) arg);
5231da177e4SLinus Torvalds 
52440be492fSMarcel Holtmann 	case HCIGETAUTHINFO:
52540be492fSMarcel Holtmann 		return hci_get_auth_info(hdev, (void __user *) arg);
52640be492fSMarcel Holtmann 
527f0358568SJohan Hedberg 	case HCIBLOCKADDR:
528f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
529bf5b30b8SZhao Hongjiang 			return -EPERM;
530b2a66aadSAntti Julku 		return hci_sock_blacklist_add(hdev, (void __user *) arg);
531f0358568SJohan Hedberg 
532f0358568SJohan Hedberg 	case HCIUNBLOCKADDR:
533f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
534bf5b30b8SZhao Hongjiang 			return -EPERM;
535b2a66aadSAntti Julku 		return hci_sock_blacklist_del(hdev, (void __user *) arg);
536*0736cfa8SMarcel Holtmann 	}
537f0358568SJohan Hedberg 
5381da177e4SLinus Torvalds 	if (hdev->ioctl)
5391da177e4SLinus Torvalds 		return hdev->ioctl(hdev, cmd, arg);
540*0736cfa8SMarcel Holtmann 
5411da177e4SLinus Torvalds 	return -EINVAL;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5448fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
5458fc9ced3SGustavo Padovan 			  unsigned long arg)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	void __user *argp = (void __user *) arg;
548*0736cfa8SMarcel Holtmann 	struct sock *sk = sock->sk;
5491da177e4SLinus Torvalds 	int err;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	BT_DBG("cmd %x arg %lx", cmd, arg);
5521da177e4SLinus Torvalds 
553c1c4f956SMarcel Holtmann 	lock_sock(sk);
554c1c4f956SMarcel Holtmann 
555c1c4f956SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
556c1c4f956SMarcel Holtmann 		err = -EBADFD;
557c1c4f956SMarcel Holtmann 		goto done;
558c1c4f956SMarcel Holtmann 	}
559c1c4f956SMarcel Holtmann 
560c1c4f956SMarcel Holtmann 	release_sock(sk);
561c1c4f956SMarcel Holtmann 
5621da177e4SLinus Torvalds 	switch (cmd) {
5631da177e4SLinus Torvalds 	case HCIGETDEVLIST:
5641da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	case HCIGETDEVINFO:
5671da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	case HCIGETCONNLIST:
5701da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	case HCIDEVUP:
5731da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
574bf5b30b8SZhao Hongjiang 			return -EPERM;
5751da177e4SLinus Torvalds 		return hci_dev_open(arg);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	case HCIDEVDOWN:
5781da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
579bf5b30b8SZhao Hongjiang 			return -EPERM;
5801da177e4SLinus Torvalds 		return hci_dev_close(arg);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	case HCIDEVRESET:
5831da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
584bf5b30b8SZhao Hongjiang 			return -EPERM;
5851da177e4SLinus Torvalds 		return hci_dev_reset(arg);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	case HCIDEVRESTAT:
5881da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
589bf5b30b8SZhao Hongjiang 			return -EPERM;
5901da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	case HCISETSCAN:
5931da177e4SLinus Torvalds 	case HCISETAUTH:
5941da177e4SLinus Torvalds 	case HCISETENCRYPT:
5951da177e4SLinus Torvalds 	case HCISETPTYPE:
5961da177e4SLinus Torvalds 	case HCISETLINKPOL:
5971da177e4SLinus Torvalds 	case HCISETLINKMODE:
5981da177e4SLinus Torvalds 	case HCISETACLMTU:
5991da177e4SLinus Torvalds 	case HCISETSCOMTU:
6001da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
601bf5b30b8SZhao Hongjiang 			return -EPERM;
6021da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	case HCIINQUIRY:
6051da177e4SLinus Torvalds 		return hci_inquiry(argp);
606c1c4f956SMarcel Holtmann 	}
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	lock_sock(sk);
609c1c4f956SMarcel Holtmann 
6101da177e4SLinus Torvalds 	err = hci_sock_bound_ioctl(sk, cmd, arg);
611c1c4f956SMarcel Holtmann 
612c1c4f956SMarcel Holtmann done:
6131da177e4SLinus Torvalds 	release_sock(sk);
6141da177e4SLinus Torvalds 	return err;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6178fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
6188fc9ced3SGustavo Padovan 			 int addr_len)
6191da177e4SLinus Torvalds {
6200381101fSJohan Hedberg 	struct sockaddr_hci haddr;
6211da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6221da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
6230381101fSJohan Hedberg 	int len, err = 0;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
6261da177e4SLinus Torvalds 
6270381101fSJohan Hedberg 	if (!addr)
6280381101fSJohan Hedberg 		return -EINVAL;
6290381101fSJohan Hedberg 
6300381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
6310381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
6320381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
6330381101fSJohan Hedberg 
6340381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
6350381101fSJohan Hedberg 		return -EINVAL;
6360381101fSJohan Hedberg 
6371da177e4SLinus Torvalds 	lock_sock(sk);
6381da177e4SLinus Torvalds 
6397cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
6407cc2ade2SMarcel Holtmann 		err = -EALREADY;
6417cc2ade2SMarcel Holtmann 		goto done;
6427cc2ade2SMarcel Holtmann 	}
6437cc2ade2SMarcel Holtmann 
6447cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
6457cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
6467cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
6471da177e4SLinus Torvalds 			err = -EALREADY;
6481da177e4SLinus Torvalds 			goto done;
6491da177e4SLinus Torvalds 		}
6501da177e4SLinus Torvalds 
6510381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
6520381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
65370f23020SAndrei Emeltchenko 			if (!hdev) {
6541da177e4SLinus Torvalds 				err = -ENODEV;
6551da177e4SLinus Torvalds 				goto done;
6561da177e4SLinus Torvalds 			}
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
6591da177e4SLinus Torvalds 		}
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 		hci_pi(sk)->hdev = hdev;
6627cc2ade2SMarcel Holtmann 		break;
6637cc2ade2SMarcel Holtmann 
6647cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
6654b95a24cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
6667cc2ade2SMarcel Holtmann 			err = -EINVAL;
6677cc2ade2SMarcel Holtmann 			goto done;
6687cc2ade2SMarcel Holtmann 		}
6697cc2ade2SMarcel Holtmann 
670801f13bdSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
671801f13bdSMarcel Holtmann 			err = -EPERM;
672801f13bdSMarcel Holtmann 			goto done;
673801f13bdSMarcel Holtmann 		}
674801f13bdSMarcel Holtmann 
6757cc2ade2SMarcel Holtmann 		break;
6767cc2ade2SMarcel Holtmann 
677cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
678cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
679cd82e61cSMarcel Holtmann 			err = -EINVAL;
680cd82e61cSMarcel Holtmann 			goto done;
681cd82e61cSMarcel Holtmann 		}
682cd82e61cSMarcel Holtmann 
683cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
684cd82e61cSMarcel Holtmann 			err = -EPERM;
685cd82e61cSMarcel Holtmann 			goto done;
686cd82e61cSMarcel Holtmann 		}
687cd82e61cSMarcel Holtmann 
688cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
689cd82e61cSMarcel Holtmann 
690cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
691cd82e61cSMarcel Holtmann 		break;
692cd82e61cSMarcel Holtmann 
6937cc2ade2SMarcel Holtmann 	default:
6947cc2ade2SMarcel Holtmann 		err = -EINVAL;
6957cc2ade2SMarcel Holtmann 		goto done;
6967cc2ade2SMarcel Holtmann 	}
6977cc2ade2SMarcel Holtmann 
6987cc2ade2SMarcel Holtmann 
6997cc2ade2SMarcel Holtmann 	hci_pi(sk)->channel = haddr.hci_channel;
7001da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds done:
7031da177e4SLinus Torvalds 	release_sock(sk);
7041da177e4SLinus Torvalds 	return err;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7078fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
7088fc9ced3SGustavo Padovan 			    int *addr_len, int peer)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
7111da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7129d4b68b2SMarcel Holtmann 	struct hci_dev *hdev;
7139d4b68b2SMarcel Holtmann 	int err = 0;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
7161da177e4SLinus Torvalds 
71706f43cbcSMarcel Holtmann 	if (peer)
71806f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
71906f43cbcSMarcel Holtmann 
7201da177e4SLinus Torvalds 	lock_sock(sk);
7211da177e4SLinus Torvalds 
7229d4b68b2SMarcel Holtmann 	hdev = hci_pi(sk)->hdev;
7239d4b68b2SMarcel Holtmann 	if (!hdev) {
7249d4b68b2SMarcel Holtmann 		err = -EBADFD;
7259d4b68b2SMarcel Holtmann 		goto done;
7269d4b68b2SMarcel Holtmann 	}
7279d4b68b2SMarcel Holtmann 
7281da177e4SLinus Torvalds 	*addr_len = sizeof(*haddr);
7291da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
7307b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
7319d4b68b2SMarcel Holtmann 	haddr->hci_channel= hci_pi(sk)->channel;
7321da177e4SLinus Torvalds 
7339d4b68b2SMarcel Holtmann done:
7341da177e4SLinus Torvalds 	release_sock(sk);
7359d4b68b2SMarcel Holtmann 	return err;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7386039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
7396039aa73SGustavo Padovan 			  struct sk_buff *skb)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	__u32 mask = hci_pi(sk)->cmsg_mask;
7421da177e4SLinus Torvalds 
7430d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
7440d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
7458fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
7468fc9ced3SGustavo Padovan 			 &incoming);
7470d48d939SMarcel Holtmann 	}
7481da177e4SLinus Torvalds 
749a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
750f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
751f6e623a6SJohann Felix Soden 		struct compat_timeval ctv;
752f6e623a6SJohann Felix Soden #endif
753a61bbcf2SPatrick McHardy 		struct timeval tv;
754767c5eb5SMarcel Holtmann 		void *data;
755767c5eb5SMarcel Holtmann 		int len;
756a61bbcf2SPatrick McHardy 
757a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
758767c5eb5SMarcel Holtmann 
7591da97f83SDavid S. Miller 		data = &tv;
7601da97f83SDavid S. Miller 		len = sizeof(tv);
7611da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
762da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
763da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
764767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
765767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
766767c5eb5SMarcel Holtmann 			data = &ctv;
767767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
768767c5eb5SMarcel Holtmann 		}
7691da97f83SDavid S. Miller #endif
770767c5eb5SMarcel Holtmann 
771767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
772a61bbcf2SPatrick McHardy 	}
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
7761da177e4SLinus Torvalds 			    struct msghdr *msg, size_t len, int flags)
7771da177e4SLinus Torvalds {
7781da177e4SLinus Torvalds 	int noblock = flags & MSG_DONTWAIT;
7791da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7801da177e4SLinus Torvalds 	struct sk_buff *skb;
7811da177e4SLinus Torvalds 	int copied, err;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	if (flags & (MSG_OOB))
7861da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
7891da177e4SLinus Torvalds 		return 0;
7901da177e4SLinus Torvalds 
79170f23020SAndrei Emeltchenko 	skb = skb_recv_datagram(sk, flags, noblock, &err);
79270f23020SAndrei Emeltchenko 	if (!skb)
7931da177e4SLinus Torvalds 		return err;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	msg->msg_namelen = 0;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	copied = skb->len;
7981da177e4SLinus Torvalds 	if (len < copied) {
7991da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
8001da177e4SLinus Torvalds 		copied = len;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 
803badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
8041da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
8051da177e4SLinus Torvalds 
8063a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
8073a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
8081da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
8093a208627SMarcel Holtmann 		break;
81097e0bdebSMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
811cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
812cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
813cd82e61cSMarcel Holtmann 		break;
8143a208627SMarcel Holtmann 	}
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	return err ? : copied;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
8221da177e4SLinus Torvalds 			    struct msghdr *msg, size_t len)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8251da177e4SLinus Torvalds 	struct hci_dev *hdev;
8261da177e4SLinus Torvalds 	struct sk_buff *skb;
8271da177e4SLinus Torvalds 	int err;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
8321da177e4SLinus Torvalds 		return -EOPNOTSUPP;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
8351da177e4SLinus Torvalds 		return -EINVAL;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (len < 4 || len > HCI_MAX_FRAME_SIZE)
8381da177e4SLinus Torvalds 		return -EINVAL;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	lock_sock(sk);
8411da177e4SLinus Torvalds 
8420381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
8430381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
8440381101fSJohan Hedberg 		break;
8450381101fSJohan Hedberg 	case HCI_CHANNEL_CONTROL:
8460381101fSJohan Hedberg 		err = mgmt_control(sk, msg, len);
8470381101fSJohan Hedberg 		goto done;
848cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
849cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
850cd82e61cSMarcel Holtmann 		goto done;
8510381101fSJohan Hedberg 	default:
8520381101fSJohan Hedberg 		err = -EINVAL;
8530381101fSJohan Hedberg 		goto done;
8540381101fSJohan Hedberg 	}
8550381101fSJohan Hedberg 
85670f23020SAndrei Emeltchenko 	hdev = hci_pi(sk)->hdev;
85770f23020SAndrei Emeltchenko 	if (!hdev) {
8581da177e4SLinus Torvalds 		err = -EBADFD;
8591da177e4SLinus Torvalds 		goto done;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8627e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
8637e21addcSMarcel Holtmann 		err = -ENETDOWN;
8647e21addcSMarcel Holtmann 		goto done;
8657e21addcSMarcel Holtmann 	}
8667e21addcSMarcel Holtmann 
86770f23020SAndrei Emeltchenko 	skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
86870f23020SAndrei Emeltchenko 	if (!skb)
8691da177e4SLinus Torvalds 		goto done;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
8721da177e4SLinus Torvalds 		err = -EFAULT;
8731da177e4SLinus Torvalds 		goto drop;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
8760d48d939SMarcel Holtmann 	bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
8771da177e4SLinus Torvalds 	skb_pull(skb, 1);
8781da177e4SLinus Torvalds 	skb->dev = (void *) hdev;
8791da177e4SLinus Torvalds 
8800d48d939SMarcel Holtmann 	if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
88183985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
8821da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
8831da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
8863bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
8873bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
8881da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
8891da177e4SLinus Torvalds 			err = -EPERM;
8901da177e4SLinus Torvalds 			goto drop;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 
893a9de9248SMarcel Holtmann 		if (test_bit(HCI_RAW, &hdev->flags) || (ogf == 0x3f)) {
8941da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
8953eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
8961da177e4SLinus Torvalds 		} else {
89711714b3dSJohan Hedberg 			/* Stand-alone HCI commands must be flaged as
89811714b3dSJohan Hedberg 			 * single-command requests.
89911714b3dSJohan Hedberg 			 */
90011714b3dSJohan Hedberg 			bt_cb(skb)->req.start = true;
90111714b3dSJohan Hedberg 
9021da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
903c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
9041da177e4SLinus Torvalds 		}
9051da177e4SLinus Torvalds 	} else {
9061da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
9071da177e4SLinus Torvalds 			err = -EPERM;
9081da177e4SLinus Torvalds 			goto drop;
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
9123eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	err = len;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds done:
9181da177e4SLinus Torvalds 	release_sock(sk);
9191da177e4SLinus Torvalds 	return err;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds drop:
9221da177e4SLinus Torvalds 	kfree_skb(skb);
9231da177e4SLinus Torvalds 	goto done;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
9268fc9ced3SGustavo Padovan static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
9278fc9ced3SGustavo Padovan 			       char __user *optval, unsigned int len)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
9301da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9311da177e4SLinus Torvalds 	int err = 0, opt = 0;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	lock_sock(sk);
9361da177e4SLinus Torvalds 
9372f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
938c2371e80SMarcel Holtmann 		err = -EBADFD;
9392f39cdb7SMarcel Holtmann 		goto done;
9402f39cdb7SMarcel Holtmann 	}
9412f39cdb7SMarcel Holtmann 
9421da177e4SLinus Torvalds 	switch (optname) {
9431da177e4SLinus Torvalds 	case HCI_DATA_DIR:
9441da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
9451da177e4SLinus Torvalds 			err = -EFAULT;
9461da177e4SLinus Torvalds 			break;
9471da177e4SLinus Torvalds 		}
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 		if (opt)
9501da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
9511da177e4SLinus Torvalds 		else
9521da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
9531da177e4SLinus Torvalds 		break;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
9561da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
9571da177e4SLinus Torvalds 			err = -EFAULT;
9581da177e4SLinus Torvalds 			break;
9591da177e4SLinus Torvalds 		}
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 		if (opt)
9621da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
9631da177e4SLinus Torvalds 		else
9641da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
9651da177e4SLinus Torvalds 		break;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	case HCI_FILTER:
9680878b666SMarcel Holtmann 		{
9690878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
9700878b666SMarcel Holtmann 
9710878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
9720878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
9730878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
9740878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
9750878b666SMarcel Holtmann 		}
9760878b666SMarcel Holtmann 
9771da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
9781da177e4SLinus Torvalds 		if (copy_from_user(&uf, optval, len)) {
9791da177e4SLinus Torvalds 			err = -EFAULT;
9801da177e4SLinus Torvalds 			break;
9811da177e4SLinus Torvalds 		}
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
9841da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
9851da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
9861da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
9871da177e4SLinus Torvalds 		}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 		{
9901da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
9931da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
9941da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
9951da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
9961da177e4SLinus Torvalds 		}
9971da177e4SLinus Torvalds 		break;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	default:
10001da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
10011da177e4SLinus Torvalds 		break;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10042f39cdb7SMarcel Holtmann done:
10051da177e4SLinus Torvalds 	release_sock(sk);
10061da177e4SLinus Torvalds 	return err;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10098fc9ced3SGustavo Padovan static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
10108fc9ced3SGustavo Padovan 			       char __user *optval, int __user *optlen)
10111da177e4SLinus Torvalds {
10121da177e4SLinus Torvalds 	struct hci_ufilter uf;
10131da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1014cedc5469SMarcel Holtmann 	int len, opt, err = 0;
1015cedc5469SMarcel Holtmann 
1016cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	if (get_user(len, optlen))
10191da177e4SLinus Torvalds 		return -EFAULT;
10201da177e4SLinus Torvalds 
1021cedc5469SMarcel Holtmann 	lock_sock(sk);
1022cedc5469SMarcel Holtmann 
1023cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1024c2371e80SMarcel Holtmann 		err = -EBADFD;
1025cedc5469SMarcel Holtmann 		goto done;
1026cedc5469SMarcel Holtmann 	}
1027cedc5469SMarcel Holtmann 
10281da177e4SLinus Torvalds 	switch (optname) {
10291da177e4SLinus Torvalds 	case HCI_DATA_DIR:
10301da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
10311da177e4SLinus Torvalds 			opt = 1;
10321da177e4SLinus Torvalds 		else
10331da177e4SLinus Torvalds 			opt = 0;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		if (put_user(opt, optval))
1036cedc5469SMarcel Holtmann 			err = -EFAULT;
10371da177e4SLinus Torvalds 		break;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
10401da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
10411da177e4SLinus Torvalds 			opt = 1;
10421da177e4SLinus Torvalds 		else
10431da177e4SLinus Torvalds 			opt = 0;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 		if (put_user(opt, optval))
1046cedc5469SMarcel Holtmann 			err = -EFAULT;
10471da177e4SLinus Torvalds 		break;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	case HCI_FILTER:
10501da177e4SLinus Torvalds 		{
10511da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
10521da177e4SLinus Torvalds 
1053e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
10541da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
10551da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
10561da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
10571da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
10611da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
1062cedc5469SMarcel Holtmann 			err = -EFAULT;
10631da177e4SLinus Torvalds 		break;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	default:
1066cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
10671da177e4SLinus Torvalds 		break;
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 
1070cedc5469SMarcel Holtmann done:
1071cedc5469SMarcel Holtmann 	release_sock(sk);
1072cedc5469SMarcel Holtmann 	return err;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
107590ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
10761da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
10771da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10781da177e4SLinus Torvalds 	.release	= hci_sock_release,
10791da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
10801da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
10811da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
10821da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
10831da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
10841da177e4SLinus Torvalds 	.poll		= datagram_poll,
10851da177e4SLinus Torvalds 	.listen		= sock_no_listen,
10861da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
10871da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
10881da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
10891da177e4SLinus Torvalds 	.connect	= sock_no_connect,
10901da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
10911da177e4SLinus Torvalds 	.accept		= sock_no_accept,
10921da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
10931da177e4SLinus Torvalds };
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds static struct proto hci_sk_proto = {
10961da177e4SLinus Torvalds 	.name		= "HCI",
10971da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10981da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
10991da177e4SLinus Torvalds };
11001da177e4SLinus Torvalds 
11013f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
11023f378b68SEric Paris 			   int kern)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	struct sock *sk;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
11091da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
11121da177e4SLinus Torvalds 
11136257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto);
11141da177e4SLinus Torvalds 	if (!sk)
11151da177e4SLinus Torvalds 		return -ENOMEM;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	sock_init_data(sock, sk);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
11241da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
11271da177e4SLinus Torvalds 	return 0;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
1130ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
11311da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
11321da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
11331da177e4SLinus Torvalds 	.create	= hci_sock_create,
11341da177e4SLinus Torvalds };
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds int __init hci_sock_init(void)
11371da177e4SLinus Torvalds {
11381da177e4SLinus Torvalds 	int err;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
11411da177e4SLinus Torvalds 	if (err < 0)
11421da177e4SLinus Torvalds 		return err;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
1145f7c86637SMasatake YAMATO 	if (err < 0) {
1146f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
11471da177e4SLinus Torvalds 		goto error;
1148f7c86637SMasatake YAMATO 	}
1149f7c86637SMasatake YAMATO 
1150b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
1151f7c86637SMasatake YAMATO 	if (err < 0) {
1152f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
1153f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
1154f7c86637SMasatake YAMATO 		goto error;
1155f7c86637SMasatake YAMATO 	}
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	return 0;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds error:
11621da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
11631da177e4SLinus Torvalds 	return err;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
1166b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
11671da177e4SLinus Torvalds {
1168f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
11695e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
11701da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
11711da177e4SLinus Torvalds }
1172