xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision 06f43cbc4d61922d5a14c28909f02ae9c7fc5283)
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 
5031da177e4SLinus Torvalds 	switch (cmd) {
5041da177e4SLinus Torvalds 	case HCISETRAW:
5051da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
506bf5b30b8SZhao Hongjiang 			return -EPERM;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 		if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
5091da177e4SLinus Torvalds 			return -EPERM;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		if (arg)
5121da177e4SLinus Torvalds 			set_bit(HCI_RAW, &hdev->flags);
5131da177e4SLinus Torvalds 		else
5141da177e4SLinus Torvalds 			clear_bit(HCI_RAW, &hdev->flags);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		return 0;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	case HCIGETCONNINFO:
5191da177e4SLinus Torvalds 		return hci_get_conn_info(hdev, (void __user *) arg);
5201da177e4SLinus Torvalds 
52140be492fSMarcel Holtmann 	case HCIGETAUTHINFO:
52240be492fSMarcel Holtmann 		return hci_get_auth_info(hdev, (void __user *) arg);
52340be492fSMarcel Holtmann 
524f0358568SJohan Hedberg 	case HCIBLOCKADDR:
525f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
526bf5b30b8SZhao Hongjiang 			return -EPERM;
527b2a66aadSAntti Julku 		return hci_sock_blacklist_add(hdev, (void __user *) arg);
528f0358568SJohan Hedberg 
529f0358568SJohan Hedberg 	case HCIUNBLOCKADDR:
530f0358568SJohan Hedberg 		if (!capable(CAP_NET_ADMIN))
531bf5b30b8SZhao Hongjiang 			return -EPERM;
532b2a66aadSAntti Julku 		return hci_sock_blacklist_del(hdev, (void __user *) arg);
533f0358568SJohan Hedberg 
5341da177e4SLinus Torvalds 	default:
5351da177e4SLinus Torvalds 		if (hdev->ioctl)
5361da177e4SLinus Torvalds 			return hdev->ioctl(hdev, cmd, arg);
5371da177e4SLinus Torvalds 		return -EINVAL;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5418fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
5428fc9ced3SGustavo Padovan 			  unsigned long arg)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
5451da177e4SLinus Torvalds 	void __user *argp = (void __user *) arg;
5461da177e4SLinus Torvalds 	int err;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	BT_DBG("cmd %x arg %lx", cmd, arg);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	switch (cmd) {
5511da177e4SLinus Torvalds 	case HCIGETDEVLIST:
5521da177e4SLinus Torvalds 		return hci_get_dev_list(argp);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	case HCIGETDEVINFO:
5551da177e4SLinus Torvalds 		return hci_get_dev_info(argp);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	case HCIGETCONNLIST:
5581da177e4SLinus Torvalds 		return hci_get_conn_list(argp);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	case HCIDEVUP:
5611da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
562bf5b30b8SZhao Hongjiang 			return -EPERM;
5631da177e4SLinus Torvalds 		return hci_dev_open(arg);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	case HCIDEVDOWN:
5661da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
567bf5b30b8SZhao Hongjiang 			return -EPERM;
5681da177e4SLinus Torvalds 		return hci_dev_close(arg);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	case HCIDEVRESET:
5711da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
572bf5b30b8SZhao Hongjiang 			return -EPERM;
5731da177e4SLinus Torvalds 		return hci_dev_reset(arg);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	case HCIDEVRESTAT:
5761da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
577bf5b30b8SZhao Hongjiang 			return -EPERM;
5781da177e4SLinus Torvalds 		return hci_dev_reset_stat(arg);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	case HCISETSCAN:
5811da177e4SLinus Torvalds 	case HCISETAUTH:
5821da177e4SLinus Torvalds 	case HCISETENCRYPT:
5831da177e4SLinus Torvalds 	case HCISETPTYPE:
5841da177e4SLinus Torvalds 	case HCISETLINKPOL:
5851da177e4SLinus Torvalds 	case HCISETLINKMODE:
5861da177e4SLinus Torvalds 	case HCISETACLMTU:
5871da177e4SLinus Torvalds 	case HCISETSCOMTU:
5881da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
589bf5b30b8SZhao Hongjiang 			return -EPERM;
5901da177e4SLinus Torvalds 		return hci_dev_cmd(cmd, argp);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	case HCIINQUIRY:
5931da177e4SLinus Torvalds 		return hci_inquiry(argp);
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	default:
5961da177e4SLinus Torvalds 		lock_sock(sk);
5971da177e4SLinus Torvalds 		err = hci_sock_bound_ioctl(sk, cmd, arg);
5981da177e4SLinus Torvalds 		release_sock(sk);
5991da177e4SLinus Torvalds 		return err;
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6038fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
6048fc9ced3SGustavo Padovan 			 int addr_len)
6051da177e4SLinus Torvalds {
6060381101fSJohan Hedberg 	struct sockaddr_hci haddr;
6071da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6081da177e4SLinus Torvalds 	struct hci_dev *hdev = NULL;
6090381101fSJohan Hedberg 	int len, err = 0;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
6121da177e4SLinus Torvalds 
6130381101fSJohan Hedberg 	if (!addr)
6140381101fSJohan Hedberg 		return -EINVAL;
6150381101fSJohan Hedberg 
6160381101fSJohan Hedberg 	memset(&haddr, 0, sizeof(haddr));
6170381101fSJohan Hedberg 	len = min_t(unsigned int, sizeof(haddr), addr_len);
6180381101fSJohan Hedberg 	memcpy(&haddr, addr, len);
6190381101fSJohan Hedberg 
6200381101fSJohan Hedberg 	if (haddr.hci_family != AF_BLUETOOTH)
6210381101fSJohan Hedberg 		return -EINVAL;
6220381101fSJohan Hedberg 
6231da177e4SLinus Torvalds 	lock_sock(sk);
6241da177e4SLinus Torvalds 
6257cc2ade2SMarcel Holtmann 	if (sk->sk_state == BT_BOUND) {
6267cc2ade2SMarcel Holtmann 		err = -EALREADY;
6277cc2ade2SMarcel Holtmann 		goto done;
6287cc2ade2SMarcel Holtmann 	}
6297cc2ade2SMarcel Holtmann 
6307cc2ade2SMarcel Holtmann 	switch (haddr.hci_channel) {
6317cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_RAW:
6327cc2ade2SMarcel Holtmann 		if (hci_pi(sk)->hdev) {
6331da177e4SLinus Torvalds 			err = -EALREADY;
6341da177e4SLinus Torvalds 			goto done;
6351da177e4SLinus Torvalds 		}
6361da177e4SLinus Torvalds 
6370381101fSJohan Hedberg 		if (haddr.hci_dev != HCI_DEV_NONE) {
6380381101fSJohan Hedberg 			hdev = hci_dev_get(haddr.hci_dev);
63970f23020SAndrei Emeltchenko 			if (!hdev) {
6401da177e4SLinus Torvalds 				err = -ENODEV;
6411da177e4SLinus Torvalds 				goto done;
6421da177e4SLinus Torvalds 			}
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 			atomic_inc(&hdev->promisc);
6451da177e4SLinus Torvalds 		}
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 		hci_pi(sk)->hdev = hdev;
6487cc2ade2SMarcel Holtmann 		break;
6497cc2ade2SMarcel Holtmann 
6507cc2ade2SMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
6514b95a24cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
6527cc2ade2SMarcel Holtmann 			err = -EINVAL;
6537cc2ade2SMarcel Holtmann 			goto done;
6547cc2ade2SMarcel Holtmann 		}
6557cc2ade2SMarcel Holtmann 
656801f13bdSMarcel Holtmann 		if (!capable(CAP_NET_ADMIN)) {
657801f13bdSMarcel Holtmann 			err = -EPERM;
658801f13bdSMarcel Holtmann 			goto done;
659801f13bdSMarcel Holtmann 		}
660801f13bdSMarcel Holtmann 
6617cc2ade2SMarcel Holtmann 		break;
6627cc2ade2SMarcel Holtmann 
663cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
664cd82e61cSMarcel Holtmann 		if (haddr.hci_dev != HCI_DEV_NONE) {
665cd82e61cSMarcel Holtmann 			err = -EINVAL;
666cd82e61cSMarcel Holtmann 			goto done;
667cd82e61cSMarcel Holtmann 		}
668cd82e61cSMarcel Holtmann 
669cd82e61cSMarcel Holtmann 		if (!capable(CAP_NET_RAW)) {
670cd82e61cSMarcel Holtmann 			err = -EPERM;
671cd82e61cSMarcel Holtmann 			goto done;
672cd82e61cSMarcel Holtmann 		}
673cd82e61cSMarcel Holtmann 
674cd82e61cSMarcel Holtmann 		send_monitor_replay(sk);
675cd82e61cSMarcel Holtmann 
676cd82e61cSMarcel Holtmann 		atomic_inc(&monitor_promisc);
677cd82e61cSMarcel Holtmann 		break;
678cd82e61cSMarcel Holtmann 
6797cc2ade2SMarcel Holtmann 	default:
6807cc2ade2SMarcel Holtmann 		err = -EINVAL;
6817cc2ade2SMarcel Holtmann 		goto done;
6827cc2ade2SMarcel Holtmann 	}
6837cc2ade2SMarcel Holtmann 
6847cc2ade2SMarcel Holtmann 
6857cc2ade2SMarcel Holtmann 	hci_pi(sk)->channel = haddr.hci_channel;
6861da177e4SLinus Torvalds 	sk->sk_state = BT_BOUND;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds done:
6891da177e4SLinus Torvalds 	release_sock(sk);
6901da177e4SLinus Torvalds 	return err;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6938fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
6948fc9ced3SGustavo Padovan 			    int *addr_len, int peer)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
6971da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6987b005bd3SMarcel Holtmann 	struct hci_dev *hdev = hci_pi(sk)->hdev;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
7011da177e4SLinus Torvalds 
702*06f43cbcSMarcel Holtmann 	if (peer)
703*06f43cbcSMarcel Holtmann 		return -EOPNOTSUPP;
704*06f43cbcSMarcel Holtmann 
7057b005bd3SMarcel Holtmann 	if (!hdev)
7067b005bd3SMarcel Holtmann 		return -EBADFD;
7077b005bd3SMarcel Holtmann 
7081da177e4SLinus Torvalds 	lock_sock(sk);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	*addr_len = sizeof(*haddr);
7111da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
7127b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
7133f68ba07SMathias Krause 	haddr->hci_channel= 0;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	release_sock(sk);
7161da177e4SLinus Torvalds 	return 0;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7196039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
7206039aa73SGustavo Padovan 			  struct sk_buff *skb)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	__u32 mask = hci_pi(sk)->cmsg_mask;
7231da177e4SLinus Torvalds 
7240d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
7250d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
7268fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
7278fc9ced3SGustavo Padovan 			 &incoming);
7280d48d939SMarcel Holtmann 	}
7291da177e4SLinus Torvalds 
730a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
731f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
732f6e623a6SJohann Felix Soden 		struct compat_timeval ctv;
733f6e623a6SJohann Felix Soden #endif
734a61bbcf2SPatrick McHardy 		struct timeval tv;
735767c5eb5SMarcel Holtmann 		void *data;
736767c5eb5SMarcel Holtmann 		int len;
737a61bbcf2SPatrick McHardy 
738a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
739767c5eb5SMarcel Holtmann 
7401da97f83SDavid S. Miller 		data = &tv;
7411da97f83SDavid S. Miller 		len = sizeof(tv);
7421da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
743da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
744da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
745767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
746767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
747767c5eb5SMarcel Holtmann 			data = &ctv;
748767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
749767c5eb5SMarcel Holtmann 		}
7501da97f83SDavid S. Miller #endif
751767c5eb5SMarcel Holtmann 
752767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
753a61bbcf2SPatrick McHardy 	}
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
7571da177e4SLinus Torvalds 			    struct msghdr *msg, size_t len, int flags)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	int noblock = flags & MSG_DONTWAIT;
7601da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7611da177e4SLinus Torvalds 	struct sk_buff *skb;
7621da177e4SLinus Torvalds 	int copied, err;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	if (flags & (MSG_OOB))
7671da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
7701da177e4SLinus Torvalds 		return 0;
7711da177e4SLinus Torvalds 
77270f23020SAndrei Emeltchenko 	skb = skb_recv_datagram(sk, flags, noblock, &err);
77370f23020SAndrei Emeltchenko 	if (!skb)
7741da177e4SLinus Torvalds 		return err;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	msg->msg_namelen = 0;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	copied = skb->len;
7791da177e4SLinus Torvalds 	if (len < copied) {
7801da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
7811da177e4SLinus Torvalds 		copied = len;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
784badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
7851da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
7861da177e4SLinus Torvalds 
7873a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
7883a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
7891da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
7903a208627SMarcel Holtmann 		break;
79197e0bdebSMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
792cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
793cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
794cd82e61cSMarcel Holtmann 		break;
7953a208627SMarcel Holtmann 	}
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	return err ? : copied;
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
8031da177e4SLinus Torvalds 			    struct msghdr *msg, size_t len)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8061da177e4SLinus Torvalds 	struct hci_dev *hdev;
8071da177e4SLinus Torvalds 	struct sk_buff *skb;
8081da177e4SLinus Torvalds 	int err;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
8131da177e4SLinus Torvalds 		return -EOPNOTSUPP;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
8161da177e4SLinus Torvalds 		return -EINVAL;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	if (len < 4 || len > HCI_MAX_FRAME_SIZE)
8191da177e4SLinus Torvalds 		return -EINVAL;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	lock_sock(sk);
8221da177e4SLinus Torvalds 
8230381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
8240381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
8250381101fSJohan Hedberg 		break;
8260381101fSJohan Hedberg 	case HCI_CHANNEL_CONTROL:
8270381101fSJohan Hedberg 		err = mgmt_control(sk, msg, len);
8280381101fSJohan Hedberg 		goto done;
829cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
830cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
831cd82e61cSMarcel Holtmann 		goto done;
8320381101fSJohan Hedberg 	default:
8330381101fSJohan Hedberg 		err = -EINVAL;
8340381101fSJohan Hedberg 		goto done;
8350381101fSJohan Hedberg 	}
8360381101fSJohan Hedberg 
83770f23020SAndrei Emeltchenko 	hdev = hci_pi(sk)->hdev;
83870f23020SAndrei Emeltchenko 	if (!hdev) {
8391da177e4SLinus Torvalds 		err = -EBADFD;
8401da177e4SLinus Torvalds 		goto done;
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 
8437e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
8447e21addcSMarcel Holtmann 		err = -ENETDOWN;
8457e21addcSMarcel Holtmann 		goto done;
8467e21addcSMarcel Holtmann 	}
8477e21addcSMarcel Holtmann 
84870f23020SAndrei Emeltchenko 	skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
84970f23020SAndrei Emeltchenko 	if (!skb)
8501da177e4SLinus Torvalds 		goto done;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
8531da177e4SLinus Torvalds 		err = -EFAULT;
8541da177e4SLinus Torvalds 		goto drop;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 
8570d48d939SMarcel Holtmann 	bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
8581da177e4SLinus Torvalds 	skb_pull(skb, 1);
8591da177e4SLinus Torvalds 	skb->dev = (void *) hdev;
8601da177e4SLinus Torvalds 
8610d48d939SMarcel Holtmann 	if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
86283985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
8631da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
8641da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
8673bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
8683bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
8691da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
8701da177e4SLinus Torvalds 			err = -EPERM;
8711da177e4SLinus Torvalds 			goto drop;
8721da177e4SLinus Torvalds 		}
8731da177e4SLinus Torvalds 
874a9de9248SMarcel Holtmann 		if (test_bit(HCI_RAW, &hdev->flags) || (ogf == 0x3f)) {
8751da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
8763eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
8771da177e4SLinus Torvalds 		} else {
87811714b3dSJohan Hedberg 			/* Stand-alone HCI commands must be flaged as
87911714b3dSJohan Hedberg 			 * single-command requests.
88011714b3dSJohan Hedberg 			 */
88111714b3dSJohan Hedberg 			bt_cb(skb)->req.start = true;
88211714b3dSJohan Hedberg 
8831da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
884c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
8851da177e4SLinus Torvalds 		}
8861da177e4SLinus Torvalds 	} else {
8871da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
8881da177e4SLinus Torvalds 			err = -EPERM;
8891da177e4SLinus Torvalds 			goto drop;
8901da177e4SLinus Torvalds 		}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
8933eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	err = len;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds done:
8991da177e4SLinus Torvalds 	release_sock(sk);
9001da177e4SLinus Torvalds 	return err;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds drop:
9031da177e4SLinus Torvalds 	kfree_skb(skb);
9041da177e4SLinus Torvalds 	goto done;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9078fc9ced3SGustavo Padovan static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
9088fc9ced3SGustavo Padovan 			       char __user *optval, unsigned int len)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
9111da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9121da177e4SLinus Torvalds 	int err = 0, opt = 0;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	lock_sock(sk);
9171da177e4SLinus Torvalds 
9182f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
9192f39cdb7SMarcel Holtmann 		err = -EINVAL;
9202f39cdb7SMarcel Holtmann 		goto done;
9212f39cdb7SMarcel Holtmann 	}
9222f39cdb7SMarcel Holtmann 
9231da177e4SLinus Torvalds 	switch (optname) {
9241da177e4SLinus Torvalds 	case HCI_DATA_DIR:
9251da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
9261da177e4SLinus Torvalds 			err = -EFAULT;
9271da177e4SLinus Torvalds 			break;
9281da177e4SLinus Torvalds 		}
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 		if (opt)
9311da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
9321da177e4SLinus Torvalds 		else
9331da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
9341da177e4SLinus Torvalds 		break;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
9371da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
9381da177e4SLinus Torvalds 			err = -EFAULT;
9391da177e4SLinus Torvalds 			break;
9401da177e4SLinus Torvalds 		}
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 		if (opt)
9431da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
9441da177e4SLinus Torvalds 		else
9451da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
9461da177e4SLinus Torvalds 		break;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	case HCI_FILTER:
9490878b666SMarcel Holtmann 		{
9500878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
9510878b666SMarcel Holtmann 
9520878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
9530878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
9540878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
9550878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
9560878b666SMarcel Holtmann 		}
9570878b666SMarcel Holtmann 
9581da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
9591da177e4SLinus Torvalds 		if (copy_from_user(&uf, optval, len)) {
9601da177e4SLinus Torvalds 			err = -EFAULT;
9611da177e4SLinus Torvalds 			break;
9621da177e4SLinus Torvalds 		}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
9651da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
9661da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
9671da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
9681da177e4SLinus Torvalds 		}
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		{
9711da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
9741da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
9751da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
9761da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
9771da177e4SLinus Torvalds 		}
9781da177e4SLinus Torvalds 		break;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	default:
9811da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
9821da177e4SLinus Torvalds 		break;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 
9852f39cdb7SMarcel Holtmann done:
9861da177e4SLinus Torvalds 	release_sock(sk);
9871da177e4SLinus Torvalds 	return err;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9908fc9ced3SGustavo Padovan static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
9918fc9ced3SGustavo Padovan 			       char __user *optval, int __user *optlen)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	struct hci_ufilter uf;
9941da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
995cedc5469SMarcel Holtmann 	int len, opt, err = 0;
996cedc5469SMarcel Holtmann 
997cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	if (get_user(len, optlen))
10001da177e4SLinus Torvalds 		return -EFAULT;
10011da177e4SLinus Torvalds 
1002cedc5469SMarcel Holtmann 	lock_sock(sk);
1003cedc5469SMarcel Holtmann 
1004cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1005cedc5469SMarcel Holtmann 		err = -EINVAL;
1006cedc5469SMarcel Holtmann 		goto done;
1007cedc5469SMarcel Holtmann 	}
1008cedc5469SMarcel Holtmann 
10091da177e4SLinus Torvalds 	switch (optname) {
10101da177e4SLinus Torvalds 	case HCI_DATA_DIR:
10111da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
10121da177e4SLinus Torvalds 			opt = 1;
10131da177e4SLinus Torvalds 		else
10141da177e4SLinus Torvalds 			opt = 0;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 		if (put_user(opt, optval))
1017cedc5469SMarcel Holtmann 			err = -EFAULT;
10181da177e4SLinus Torvalds 		break;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
10211da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
10221da177e4SLinus Torvalds 			opt = 1;
10231da177e4SLinus Torvalds 		else
10241da177e4SLinus Torvalds 			opt = 0;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 		if (put_user(opt, optval))
1027cedc5469SMarcel Holtmann 			err = -EFAULT;
10281da177e4SLinus Torvalds 		break;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	case HCI_FILTER:
10311da177e4SLinus Torvalds 		{
10321da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
10331da177e4SLinus Torvalds 
1034e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
10351da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
10361da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
10371da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
10381da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
10391da177e4SLinus Torvalds 		}
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
10421da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
1043cedc5469SMarcel Holtmann 			err = -EFAULT;
10441da177e4SLinus Torvalds 		break;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	default:
1047cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
10481da177e4SLinus Torvalds 		break;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
1051cedc5469SMarcel Holtmann done:
1052cedc5469SMarcel Holtmann 	release_sock(sk);
1053cedc5469SMarcel Holtmann 	return err;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
105690ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
10571da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
10581da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10591da177e4SLinus Torvalds 	.release	= hci_sock_release,
10601da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
10611da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
10621da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
10631da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
10641da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
10651da177e4SLinus Torvalds 	.poll		= datagram_poll,
10661da177e4SLinus Torvalds 	.listen		= sock_no_listen,
10671da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
10681da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
10691da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
10701da177e4SLinus Torvalds 	.connect	= sock_no_connect,
10711da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
10721da177e4SLinus Torvalds 	.accept		= sock_no_accept,
10731da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
10741da177e4SLinus Torvalds };
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds static struct proto hci_sk_proto = {
10771da177e4SLinus Torvalds 	.name		= "HCI",
10781da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10791da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
10801da177e4SLinus Torvalds };
10811da177e4SLinus Torvalds 
10823f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
10833f378b68SEric Paris 			   int kern)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	struct sock *sk;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
10901da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
10931da177e4SLinus Torvalds 
10946257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto);
10951da177e4SLinus Torvalds 	if (!sk)
10961da177e4SLinus Torvalds 		return -ENOMEM;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	sock_init_data(sock, sk);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
11051da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
11081da177e4SLinus Torvalds 	return 0;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
1111ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
11121da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
11131da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
11141da177e4SLinus Torvalds 	.create	= hci_sock_create,
11151da177e4SLinus Torvalds };
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds int __init hci_sock_init(void)
11181da177e4SLinus Torvalds {
11191da177e4SLinus Torvalds 	int err;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
11221da177e4SLinus Torvalds 	if (err < 0)
11231da177e4SLinus Torvalds 		return err;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
1126f7c86637SMasatake YAMATO 	if (err < 0) {
1127f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
11281da177e4SLinus Torvalds 		goto error;
1129f7c86637SMasatake YAMATO 	}
1130f7c86637SMasatake YAMATO 
1131b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
1132f7c86637SMasatake YAMATO 	if (err < 0) {
1133f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
1134f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
1135f7c86637SMasatake YAMATO 		goto error;
1136f7c86637SMasatake YAMATO 	}
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	return 0;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds error:
11431da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
11441da177e4SLinus Torvalds 	return err;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
1147b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
11481da177e4SLinus Torvalds {
1149f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
11505e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
11511da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
11521da177e4SLinus Torvalds }
1153