xref: /openbmc/linux/net/bluetooth/hci_sock.c (revision f81fe64f3d3bc76c1a8d3edb80b54219a60ff291)
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 
69*f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
70*f81fe64fSMarcel Holtmann {
71*f81fe64fSMarcel Holtmann 	struct hci_filter *flt;
72*f81fe64fSMarcel Holtmann 	int flt_type, flt_event;
73*f81fe64fSMarcel Holtmann 
74*f81fe64fSMarcel Holtmann 	/* Apply filter */
75*f81fe64fSMarcel Holtmann 	flt = &hci_pi(sk)->filter;
76*f81fe64fSMarcel Holtmann 
77*f81fe64fSMarcel Holtmann 	if (bt_cb(skb)->pkt_type == HCI_VENDOR_PKT)
78*f81fe64fSMarcel Holtmann 		flt_type = 0;
79*f81fe64fSMarcel Holtmann 	else
80*f81fe64fSMarcel Holtmann 		flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS;
81*f81fe64fSMarcel Holtmann 
82*f81fe64fSMarcel Holtmann 	if (!test_bit(flt_type, &flt->type_mask))
83*f81fe64fSMarcel Holtmann 		return true;
84*f81fe64fSMarcel Holtmann 
85*f81fe64fSMarcel Holtmann 	/* Extra filter for event packets only */
86*f81fe64fSMarcel Holtmann 	if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT)
87*f81fe64fSMarcel Holtmann 		return false;
88*f81fe64fSMarcel Holtmann 
89*f81fe64fSMarcel Holtmann 	flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
90*f81fe64fSMarcel Holtmann 
91*f81fe64fSMarcel Holtmann 	if (!hci_test_bit(flt_event, &flt->event_mask))
92*f81fe64fSMarcel Holtmann 		return true;
93*f81fe64fSMarcel Holtmann 
94*f81fe64fSMarcel Holtmann 	/* Check filter only when opcode is set */
95*f81fe64fSMarcel Holtmann 	if (!flt->opcode)
96*f81fe64fSMarcel Holtmann 		return false;
97*f81fe64fSMarcel Holtmann 
98*f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_COMPLETE &&
99*f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
100*f81fe64fSMarcel Holtmann 		return true;
101*f81fe64fSMarcel Holtmann 
102*f81fe64fSMarcel Holtmann 	if (flt_event == HCI_EV_CMD_STATUS &&
103*f81fe64fSMarcel Holtmann 	    flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
104*f81fe64fSMarcel Holtmann 		return true;
105*f81fe64fSMarcel Holtmann 
106*f81fe64fSMarcel Holtmann 	return false;
107*f81fe64fSMarcel Holtmann }
108*f81fe64fSMarcel 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 
132*f81fe64fSMarcel 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 
7027b005bd3SMarcel Holtmann 	if (!hdev)
7037b005bd3SMarcel Holtmann 		return -EBADFD;
7047b005bd3SMarcel Holtmann 
7051da177e4SLinus Torvalds 	lock_sock(sk);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	*addr_len = sizeof(*haddr);
7081da177e4SLinus Torvalds 	haddr->hci_family = AF_BLUETOOTH;
7097b005bd3SMarcel Holtmann 	haddr->hci_dev    = hdev->id;
7103f68ba07SMathias Krause 	haddr->hci_channel= 0;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	release_sock(sk);
7131da177e4SLinus Torvalds 	return 0;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7166039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
7176039aa73SGustavo Padovan 			  struct sk_buff *skb)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	__u32 mask = hci_pi(sk)->cmsg_mask;
7201da177e4SLinus Torvalds 
7210d48d939SMarcel Holtmann 	if (mask & HCI_CMSG_DIR) {
7220d48d939SMarcel Holtmann 		int incoming = bt_cb(skb)->incoming;
7238fc9ced3SGustavo Padovan 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
7248fc9ced3SGustavo Padovan 			 &incoming);
7250d48d939SMarcel Holtmann 	}
7261da177e4SLinus Torvalds 
727a61bbcf2SPatrick McHardy 	if (mask & HCI_CMSG_TSTAMP) {
728f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
729f6e623a6SJohann Felix Soden 		struct compat_timeval ctv;
730f6e623a6SJohann Felix Soden #endif
731a61bbcf2SPatrick McHardy 		struct timeval tv;
732767c5eb5SMarcel Holtmann 		void *data;
733767c5eb5SMarcel Holtmann 		int len;
734a61bbcf2SPatrick McHardy 
735a61bbcf2SPatrick McHardy 		skb_get_timestamp(skb, &tv);
736767c5eb5SMarcel Holtmann 
7371da97f83SDavid S. Miller 		data = &tv;
7381da97f83SDavid S. Miller 		len = sizeof(tv);
7391da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
740da88cea1SH. J. Lu 		if (!COMPAT_USE_64BIT_TIME &&
741da88cea1SH. J. Lu 		    (msg->msg_flags & MSG_CMSG_COMPAT)) {
742767c5eb5SMarcel Holtmann 			ctv.tv_sec = tv.tv_sec;
743767c5eb5SMarcel Holtmann 			ctv.tv_usec = tv.tv_usec;
744767c5eb5SMarcel Holtmann 			data = &ctv;
745767c5eb5SMarcel Holtmann 			len = sizeof(ctv);
746767c5eb5SMarcel Holtmann 		}
7471da97f83SDavid S. Miller #endif
748767c5eb5SMarcel Holtmann 
749767c5eb5SMarcel Holtmann 		put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
750a61bbcf2SPatrick McHardy 	}
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
7541da177e4SLinus Torvalds 			    struct msghdr *msg, size_t len, int flags)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	int noblock = flags & MSG_DONTWAIT;
7571da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7581da177e4SLinus Torvalds 	struct sk_buff *skb;
7591da177e4SLinus Torvalds 	int copied, err;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	BT_DBG("sock %p, sk %p", sock, sk);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	if (flags & (MSG_OOB))
7641da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	if (sk->sk_state == BT_CLOSED)
7671da177e4SLinus Torvalds 		return 0;
7681da177e4SLinus Torvalds 
76970f23020SAndrei Emeltchenko 	skb = skb_recv_datagram(sk, flags, noblock, &err);
77070f23020SAndrei Emeltchenko 	if (!skb)
7711da177e4SLinus Torvalds 		return err;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	msg->msg_namelen = 0;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	copied = skb->len;
7761da177e4SLinus Torvalds 	if (len < copied) {
7771da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
7781da177e4SLinus Torvalds 		copied = len;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
781badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
7821da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
7831da177e4SLinus Torvalds 
7843a208627SMarcel Holtmann 	switch (hci_pi(sk)->channel) {
7853a208627SMarcel Holtmann 	case HCI_CHANNEL_RAW:
7861da177e4SLinus Torvalds 		hci_sock_cmsg(sk, msg, skb);
7873a208627SMarcel Holtmann 		break;
78897e0bdebSMarcel Holtmann 	case HCI_CHANNEL_CONTROL:
789cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
790cd82e61cSMarcel Holtmann 		sock_recv_timestamp(msg, sk, skb);
791cd82e61cSMarcel Holtmann 		break;
7923a208627SMarcel Holtmann 	}
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	return err ? : copied;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
8001da177e4SLinus Torvalds 			    struct msghdr *msg, size_t len)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8031da177e4SLinus Torvalds 	struct hci_dev *hdev;
8041da177e4SLinus Torvalds 	struct sk_buff *skb;
8051da177e4SLinus Torvalds 	int err;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	BT_DBG("sock %p sk %p", sock, sk);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
8101da177e4SLinus Torvalds 		return -EOPNOTSUPP;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
8131da177e4SLinus Torvalds 		return -EINVAL;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	if (len < 4 || len > HCI_MAX_FRAME_SIZE)
8161da177e4SLinus Torvalds 		return -EINVAL;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	lock_sock(sk);
8191da177e4SLinus Torvalds 
8200381101fSJohan Hedberg 	switch (hci_pi(sk)->channel) {
8210381101fSJohan Hedberg 	case HCI_CHANNEL_RAW:
8220381101fSJohan Hedberg 		break;
8230381101fSJohan Hedberg 	case HCI_CHANNEL_CONTROL:
8240381101fSJohan Hedberg 		err = mgmt_control(sk, msg, len);
8250381101fSJohan Hedberg 		goto done;
826cd82e61cSMarcel Holtmann 	case HCI_CHANNEL_MONITOR:
827cd82e61cSMarcel Holtmann 		err = -EOPNOTSUPP;
828cd82e61cSMarcel Holtmann 		goto done;
8290381101fSJohan Hedberg 	default:
8300381101fSJohan Hedberg 		err = -EINVAL;
8310381101fSJohan Hedberg 		goto done;
8320381101fSJohan Hedberg 	}
8330381101fSJohan Hedberg 
83470f23020SAndrei Emeltchenko 	hdev = hci_pi(sk)->hdev;
83570f23020SAndrei Emeltchenko 	if (!hdev) {
8361da177e4SLinus Torvalds 		err = -EBADFD;
8371da177e4SLinus Torvalds 		goto done;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8407e21addcSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
8417e21addcSMarcel Holtmann 		err = -ENETDOWN;
8427e21addcSMarcel Holtmann 		goto done;
8437e21addcSMarcel Holtmann 	}
8447e21addcSMarcel Holtmann 
84570f23020SAndrei Emeltchenko 	skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
84670f23020SAndrei Emeltchenko 	if (!skb)
8471da177e4SLinus Torvalds 		goto done;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
8501da177e4SLinus Torvalds 		err = -EFAULT;
8511da177e4SLinus Torvalds 		goto drop;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 
8540d48d939SMarcel Holtmann 	bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
8551da177e4SLinus Torvalds 	skb_pull(skb, 1);
8561da177e4SLinus Torvalds 	skb->dev = (void *) hdev;
8571da177e4SLinus Torvalds 
8580d48d939SMarcel Holtmann 	if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
85983985319SHarvey Harrison 		u16 opcode = get_unaligned_le16(skb->data);
8601da177e4SLinus Torvalds 		u16 ogf = hci_opcode_ogf(opcode);
8611da177e4SLinus Torvalds 		u16 ocf = hci_opcode_ocf(opcode);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		if (((ogf > HCI_SFLT_MAX_OGF) ||
8643bb3c755SGustavo Padovan 		     !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
8653bb3c755SGustavo Padovan 				   &hci_sec_filter.ocf_mask[ogf])) &&
8661da177e4SLinus Torvalds 		    !capable(CAP_NET_RAW)) {
8671da177e4SLinus Torvalds 			err = -EPERM;
8681da177e4SLinus Torvalds 			goto drop;
8691da177e4SLinus Torvalds 		}
8701da177e4SLinus Torvalds 
871a9de9248SMarcel Holtmann 		if (test_bit(HCI_RAW, &hdev->flags) || (ogf == 0x3f)) {
8721da177e4SLinus Torvalds 			skb_queue_tail(&hdev->raw_q, skb);
8733eff45eaSGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->tx_work);
8741da177e4SLinus Torvalds 		} else {
87511714b3dSJohan Hedberg 			/* Stand-alone HCI commands must be flaged as
87611714b3dSJohan Hedberg 			 * single-command requests.
87711714b3dSJohan Hedberg 			 */
87811714b3dSJohan Hedberg 			bt_cb(skb)->req.start = true;
87911714b3dSJohan Hedberg 
8801da177e4SLinus Torvalds 			skb_queue_tail(&hdev->cmd_q, skb);
881c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
8821da177e4SLinus Torvalds 		}
8831da177e4SLinus Torvalds 	} else {
8841da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
8851da177e4SLinus Torvalds 			err = -EPERM;
8861da177e4SLinus Torvalds 			goto drop;
8871da177e4SLinus Torvalds 		}
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		skb_queue_tail(&hdev->raw_q, skb);
8903eff45eaSGustavo F. Padovan 		queue_work(hdev->workqueue, &hdev->tx_work);
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	err = len;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds done:
8961da177e4SLinus Torvalds 	release_sock(sk);
8971da177e4SLinus Torvalds 	return err;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds drop:
9001da177e4SLinus Torvalds 	kfree_skb(skb);
9011da177e4SLinus Torvalds 	goto done;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
9048fc9ced3SGustavo Padovan static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
9058fc9ced3SGustavo Padovan 			       char __user *optval, unsigned int len)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	struct hci_ufilter uf = { .opcode = 0 };
9081da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9091da177e4SLinus Torvalds 	int err = 0, opt = 0;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	BT_DBG("sk %p, opt %d", sk, optname);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	lock_sock(sk);
9141da177e4SLinus Torvalds 
9152f39cdb7SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
9162f39cdb7SMarcel Holtmann 		err = -EINVAL;
9172f39cdb7SMarcel Holtmann 		goto done;
9182f39cdb7SMarcel Holtmann 	}
9192f39cdb7SMarcel Holtmann 
9201da177e4SLinus Torvalds 	switch (optname) {
9211da177e4SLinus Torvalds 	case HCI_DATA_DIR:
9221da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
9231da177e4SLinus Torvalds 			err = -EFAULT;
9241da177e4SLinus Torvalds 			break;
9251da177e4SLinus Torvalds 		}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 		if (opt)
9281da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
9291da177e4SLinus Torvalds 		else
9301da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
9311da177e4SLinus Torvalds 		break;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
9341da177e4SLinus Torvalds 		if (get_user(opt, (int __user *)optval)) {
9351da177e4SLinus Torvalds 			err = -EFAULT;
9361da177e4SLinus Torvalds 			break;
9371da177e4SLinus Torvalds 		}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 		if (opt)
9401da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
9411da177e4SLinus Torvalds 		else
9421da177e4SLinus Torvalds 			hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
9431da177e4SLinus Torvalds 		break;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	case HCI_FILTER:
9460878b666SMarcel Holtmann 		{
9470878b666SMarcel Holtmann 			struct hci_filter *f = &hci_pi(sk)->filter;
9480878b666SMarcel Holtmann 
9490878b666SMarcel Holtmann 			uf.type_mask = f->type_mask;
9500878b666SMarcel Holtmann 			uf.opcode    = f->opcode;
9510878b666SMarcel Holtmann 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
9520878b666SMarcel Holtmann 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
9530878b666SMarcel Holtmann 		}
9540878b666SMarcel Holtmann 
9551da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
9561da177e4SLinus Torvalds 		if (copy_from_user(&uf, optval, len)) {
9571da177e4SLinus Torvalds 			err = -EFAULT;
9581da177e4SLinus Torvalds 			break;
9591da177e4SLinus Torvalds 		}
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 		if (!capable(CAP_NET_RAW)) {
9621da177e4SLinus Torvalds 			uf.type_mask &= hci_sec_filter.type_mask;
9631da177e4SLinus Torvalds 			uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
9641da177e4SLinus Torvalds 			uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
9651da177e4SLinus Torvalds 		}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 		{
9681da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 			f->type_mask = uf.type_mask;
9711da177e4SLinus Torvalds 			f->opcode    = uf.opcode;
9721da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 0) = uf.event_mask[0];
9731da177e4SLinus Torvalds 			*((u32 *) f->event_mask + 1) = uf.event_mask[1];
9741da177e4SLinus Torvalds 		}
9751da177e4SLinus Torvalds 		break;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	default:
9781da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
9791da177e4SLinus Torvalds 		break;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 
9822f39cdb7SMarcel Holtmann done:
9831da177e4SLinus Torvalds 	release_sock(sk);
9841da177e4SLinus Torvalds 	return err;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
9878fc9ced3SGustavo Padovan static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
9888fc9ced3SGustavo Padovan 			       char __user *optval, int __user *optlen)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	struct hci_ufilter uf;
9911da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
992cedc5469SMarcel Holtmann 	int len, opt, err = 0;
993cedc5469SMarcel Holtmann 
994cedc5469SMarcel Holtmann 	BT_DBG("sk %p, opt %d", sk, optname);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (get_user(len, optlen))
9971da177e4SLinus Torvalds 		return -EFAULT;
9981da177e4SLinus Torvalds 
999cedc5469SMarcel Holtmann 	lock_sock(sk);
1000cedc5469SMarcel Holtmann 
1001cedc5469SMarcel Holtmann 	if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1002cedc5469SMarcel Holtmann 		err = -EINVAL;
1003cedc5469SMarcel Holtmann 		goto done;
1004cedc5469SMarcel Holtmann 	}
1005cedc5469SMarcel Holtmann 
10061da177e4SLinus Torvalds 	switch (optname) {
10071da177e4SLinus Torvalds 	case HCI_DATA_DIR:
10081da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
10091da177e4SLinus Torvalds 			opt = 1;
10101da177e4SLinus Torvalds 		else
10111da177e4SLinus Torvalds 			opt = 0;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 		if (put_user(opt, optval))
1014cedc5469SMarcel Holtmann 			err = -EFAULT;
10151da177e4SLinus Torvalds 		break;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	case HCI_TIME_STAMP:
10181da177e4SLinus Torvalds 		if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
10191da177e4SLinus Torvalds 			opt = 1;
10201da177e4SLinus Torvalds 		else
10211da177e4SLinus Torvalds 			opt = 0;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 		if (put_user(opt, optval))
1024cedc5469SMarcel Holtmann 			err = -EFAULT;
10251da177e4SLinus Torvalds 		break;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	case HCI_FILTER:
10281da177e4SLinus Torvalds 		{
10291da177e4SLinus Torvalds 			struct hci_filter *f = &hci_pi(sk)->filter;
10301da177e4SLinus Torvalds 
1031e15ca9a0SMathias Krause 			memset(&uf, 0, sizeof(uf));
10321da177e4SLinus Torvalds 			uf.type_mask = f->type_mask;
10331da177e4SLinus Torvalds 			uf.opcode    = f->opcode;
10341da177e4SLinus Torvalds 			uf.event_mask[0] = *((u32 *) f->event_mask + 0);
10351da177e4SLinus Torvalds 			uf.event_mask[1] = *((u32 *) f->event_mask + 1);
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 		len = min_t(unsigned int, len, sizeof(uf));
10391da177e4SLinus Torvalds 		if (copy_to_user(optval, &uf, len))
1040cedc5469SMarcel Holtmann 			err = -EFAULT;
10411da177e4SLinus Torvalds 		break;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	default:
1044cedc5469SMarcel Holtmann 		err = -ENOPROTOOPT;
10451da177e4SLinus Torvalds 		break;
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 
1048cedc5469SMarcel Holtmann done:
1049cedc5469SMarcel Holtmann 	release_sock(sk);
1050cedc5469SMarcel Holtmann 	return err;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
105390ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
10541da177e4SLinus Torvalds 	.family		= PF_BLUETOOTH,
10551da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10561da177e4SLinus Torvalds 	.release	= hci_sock_release,
10571da177e4SLinus Torvalds 	.bind		= hci_sock_bind,
10581da177e4SLinus Torvalds 	.getname	= hci_sock_getname,
10591da177e4SLinus Torvalds 	.sendmsg	= hci_sock_sendmsg,
10601da177e4SLinus Torvalds 	.recvmsg	= hci_sock_recvmsg,
10611da177e4SLinus Torvalds 	.ioctl		= hci_sock_ioctl,
10621da177e4SLinus Torvalds 	.poll		= datagram_poll,
10631da177e4SLinus Torvalds 	.listen		= sock_no_listen,
10641da177e4SLinus Torvalds 	.shutdown	= sock_no_shutdown,
10651da177e4SLinus Torvalds 	.setsockopt	= hci_sock_setsockopt,
10661da177e4SLinus Torvalds 	.getsockopt	= hci_sock_getsockopt,
10671da177e4SLinus Torvalds 	.connect	= sock_no_connect,
10681da177e4SLinus Torvalds 	.socketpair	= sock_no_socketpair,
10691da177e4SLinus Torvalds 	.accept		= sock_no_accept,
10701da177e4SLinus Torvalds 	.mmap		= sock_no_mmap
10711da177e4SLinus Torvalds };
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static struct proto hci_sk_proto = {
10741da177e4SLinus Torvalds 	.name		= "HCI",
10751da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
10761da177e4SLinus Torvalds 	.obj_size	= sizeof(struct hci_pinfo)
10771da177e4SLinus Torvalds };
10781da177e4SLinus Torvalds 
10793f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
10803f378b68SEric Paris 			   int kern)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	struct sock *sk;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	BT_DBG("sock %p", sock);
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	if (sock->type != SOCK_RAW)
10871da177e4SLinus Torvalds 		return -ESOCKTNOSUPPORT;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	sock->ops = &hci_sock_ops;
10901da177e4SLinus Torvalds 
10916257ff21SPavel Emelyanov 	sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hci_sk_proto);
10921da177e4SLinus Torvalds 	if (!sk)
10931da177e4SLinus Torvalds 		return -ENOMEM;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	sock_init_data(sock, sk);
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
11021da177e4SLinus Torvalds 	sk->sk_state = BT_OPEN;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	bt_sock_link(&hci_sk_list, sk);
11051da177e4SLinus Torvalds 	return 0;
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
1108ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
11091da177e4SLinus Torvalds 	.family	= PF_BLUETOOTH,
11101da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
11111da177e4SLinus Torvalds 	.create	= hci_sock_create,
11121da177e4SLinus Torvalds };
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds int __init hci_sock_init(void)
11151da177e4SLinus Torvalds {
11161da177e4SLinus Torvalds 	int err;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	err = proto_register(&hci_sk_proto, 0);
11191da177e4SLinus Torvalds 	if (err < 0)
11201da177e4SLinus Torvalds 		return err;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
1123f7c86637SMasatake YAMATO 	if (err < 0) {
1124f7c86637SMasatake YAMATO 		BT_ERR("HCI socket registration failed");
11251da177e4SLinus Torvalds 		goto error;
1126f7c86637SMasatake YAMATO 	}
1127f7c86637SMasatake YAMATO 
1128b0316615SAl Viro 	err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
1129f7c86637SMasatake YAMATO 	if (err < 0) {
1130f7c86637SMasatake YAMATO 		BT_ERR("Failed to create HCI proc file");
1131f7c86637SMasatake YAMATO 		bt_sock_unregister(BTPROTO_HCI);
1132f7c86637SMasatake YAMATO 		goto error;
1133f7c86637SMasatake YAMATO 	}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	BT_INFO("HCI socket layer initialized");
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	return 0;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds error:
11401da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
11411da177e4SLinus Torvalds 	return err;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
1144b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
11451da177e4SLinus Torvalds {
1146f7c86637SMasatake YAMATO 	bt_procfs_cleanup(&init_net, "hci");
11475e9d7f86SDavid Herrmann 	bt_sock_unregister(BTPROTO_HCI);
11481da177e4SLinus Torvalds 	proto_unregister(&hci_sk_proto);
11491da177e4SLinus Torvalds }
1150