11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds Copyright (C) 2000-2001 Qualcomm Incorporated
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
61da177e4SLinus Torvalds
71da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as
91da177e4SLinus Torvalds published by the Free Software Foundation;
101da177e4SLinus Torvalds
111da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
121da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
141da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
151da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
161da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
171da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
181da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
211da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
221da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED.
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds /* Bluetooth HCI sockets. */
267a6038b3SArnd Bergmann #include <linux/compat.h>
278c520a59SGustavo Padovan #include <linux/export.h>
28787b306cSJohannes Berg #include <linux/utsname.h>
2970ecce91SMarcel Holtmann #include <linux/sched.h>
301da177e4SLinus Torvalds #include <asm/unaligned.h>
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
331da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
34cd82e61cSMarcel Holtmann #include <net/bluetooth/hci_mon.h>
35fa4335d7SJohan Hedberg #include <net/bluetooth/mgmt.h>
36fa4335d7SJohan Hedberg
37fa4335d7SJohan Hedberg #include "mgmt_util.h"
381da177e4SLinus Torvalds
39801c1e8dSJohan Hedberg static LIST_HEAD(mgmt_chan_list);
40801c1e8dSJohan Hedberg static DEFINE_MUTEX(mgmt_chan_list_lock);
41801c1e8dSJohan Hedberg
4270ecce91SMarcel Holtmann static DEFINE_IDA(sock_cookie_ida);
4370ecce91SMarcel Holtmann
44cd82e61cSMarcel Holtmann static atomic_t monitor_promisc = ATOMIC_INIT(0);
45cd82e61cSMarcel Holtmann
461da177e4SLinus Torvalds /* ----- HCI socket interface ----- */
471da177e4SLinus Torvalds
48863def58SMarcel Holtmann /* Socket info */
49863def58SMarcel Holtmann #define hci_pi(sk) ((struct hci_pinfo *) sk)
50863def58SMarcel Holtmann
51863def58SMarcel Holtmann struct hci_pinfo {
52863def58SMarcel Holtmann struct bt_sock bt;
53863def58SMarcel Holtmann struct hci_dev *hdev;
54863def58SMarcel Holtmann struct hci_filter filter;
5532929e1fSAlain Michaud __u8 cmsg_mask;
56863def58SMarcel Holtmann unsigned short channel;
576befc644SMarcel Holtmann unsigned long flags;
5870ecce91SMarcel Holtmann __u32 cookie;
5970ecce91SMarcel Holtmann char comm[TASK_COMM_LEN];
6009572fcaSLuiz Augusto von Dentz __u16 mtu;
61863def58SMarcel Holtmann };
62863def58SMarcel Holtmann
hci_hdev_from_sock(struct sock * sk)63e0448092STetsuo Handa static struct hci_dev *hci_hdev_from_sock(struct sock *sk)
64e0448092STetsuo Handa {
65e0448092STetsuo Handa struct hci_dev *hdev = hci_pi(sk)->hdev;
66e0448092STetsuo Handa
67e0448092STetsuo Handa if (!hdev)
68e0448092STetsuo Handa return ERR_PTR(-EBADFD);
69e0448092STetsuo Handa if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
70e0448092STetsuo Handa return ERR_PTR(-EPIPE);
71e0448092STetsuo Handa return hdev;
72e0448092STetsuo Handa }
73e0448092STetsuo Handa
hci_sock_set_flag(struct sock * sk,int nr)746befc644SMarcel Holtmann void hci_sock_set_flag(struct sock *sk, int nr)
756befc644SMarcel Holtmann {
766befc644SMarcel Holtmann set_bit(nr, &hci_pi(sk)->flags);
776befc644SMarcel Holtmann }
786befc644SMarcel Holtmann
hci_sock_clear_flag(struct sock * sk,int nr)796befc644SMarcel Holtmann void hci_sock_clear_flag(struct sock *sk, int nr)
806befc644SMarcel Holtmann {
816befc644SMarcel Holtmann clear_bit(nr, &hci_pi(sk)->flags);
826befc644SMarcel Holtmann }
836befc644SMarcel Holtmann
hci_sock_test_flag(struct sock * sk,int nr)84c85be545SMarcel Holtmann int hci_sock_test_flag(struct sock *sk, int nr)
85c85be545SMarcel Holtmann {
86c85be545SMarcel Holtmann return test_bit(nr, &hci_pi(sk)->flags);
87c85be545SMarcel Holtmann }
88c85be545SMarcel Holtmann
hci_sock_get_channel(struct sock * sk)89d0f172b1SJohan Hedberg unsigned short hci_sock_get_channel(struct sock *sk)
90d0f172b1SJohan Hedberg {
91d0f172b1SJohan Hedberg return hci_pi(sk)->channel;
92d0f172b1SJohan Hedberg }
93d0f172b1SJohan Hedberg
hci_sock_get_cookie(struct sock * sk)9470ecce91SMarcel Holtmann u32 hci_sock_get_cookie(struct sock *sk)
9570ecce91SMarcel Holtmann {
9670ecce91SMarcel Holtmann return hci_pi(sk)->cookie;
9770ecce91SMarcel Holtmann }
9870ecce91SMarcel Holtmann
hci_sock_gen_cookie(struct sock * sk)99df1cb87aSMarcel Holtmann static bool hci_sock_gen_cookie(struct sock *sk)
100df1cb87aSMarcel Holtmann {
101df1cb87aSMarcel Holtmann int id = hci_pi(sk)->cookie;
102df1cb87aSMarcel Holtmann
103df1cb87aSMarcel Holtmann if (!id) {
1040a8af30aSChristophe JAILLET id = ida_alloc_min(&sock_cookie_ida, 1, GFP_KERNEL);
105df1cb87aSMarcel Holtmann if (id < 0)
106df1cb87aSMarcel Holtmann id = 0xffffffff;
107df1cb87aSMarcel Holtmann
108df1cb87aSMarcel Holtmann hci_pi(sk)->cookie = id;
109df1cb87aSMarcel Holtmann get_task_comm(hci_pi(sk)->comm, current);
110df1cb87aSMarcel Holtmann return true;
111df1cb87aSMarcel Holtmann }
112df1cb87aSMarcel Holtmann
113df1cb87aSMarcel Holtmann return false;
114df1cb87aSMarcel Holtmann }
115df1cb87aSMarcel Holtmann
hci_sock_free_cookie(struct sock * sk)116df1cb87aSMarcel Holtmann static void hci_sock_free_cookie(struct sock *sk)
117df1cb87aSMarcel Holtmann {
118df1cb87aSMarcel Holtmann int id = hci_pi(sk)->cookie;
119df1cb87aSMarcel Holtmann
120df1cb87aSMarcel Holtmann if (id) {
121df1cb87aSMarcel Holtmann hci_pi(sk)->cookie = 0xffffffff;
1220a8af30aSChristophe JAILLET ida_free(&sock_cookie_ida, id);
123df1cb87aSMarcel Holtmann }
124df1cb87aSMarcel Holtmann }
125df1cb87aSMarcel Holtmann
hci_test_bit(int nr,const void * addr)1269391976aSJiri Slaby static inline int hci_test_bit(int nr, const void *addr)
1271da177e4SLinus Torvalds {
1289391976aSJiri Slaby return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds /* Security filter */
1323ad254f7SMarcel Holtmann #define HCI_SFLT_MAX_OGF 5
1333ad254f7SMarcel Holtmann
1343ad254f7SMarcel Holtmann struct hci_sec_filter {
1353ad254f7SMarcel Holtmann __u32 type_mask;
1363ad254f7SMarcel Holtmann __u32 event_mask[2];
1373ad254f7SMarcel Holtmann __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
1383ad254f7SMarcel Holtmann };
1393ad254f7SMarcel Holtmann
1407e67c112SMarcel Holtmann static const struct hci_sec_filter hci_sec_filter = {
1411da177e4SLinus Torvalds /* Packet types */
1421da177e4SLinus Torvalds 0x10,
1431da177e4SLinus Torvalds /* Events */
144dd7f5527SMarcel Holtmann { 0x1000d9fe, 0x0000b00c },
1451da177e4SLinus Torvalds /* Commands */
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds { 0x0 },
1481da177e4SLinus Torvalds /* OGF_LINK_CTL */
1497c631a67SMarcel Holtmann { 0xbe000006, 0x00000001, 0x00000000, 0x00 },
1501da177e4SLinus Torvalds /* OGF_LINK_POLICY */
1517c631a67SMarcel Holtmann { 0x00005200, 0x00000000, 0x00000000, 0x00 },
1521da177e4SLinus Torvalds /* OGF_HOST_CTL */
1537c631a67SMarcel Holtmann { 0xaab00200, 0x2b402aaa, 0x05220154, 0x00 },
1541da177e4SLinus Torvalds /* OGF_INFO_PARAM */
1557c631a67SMarcel Holtmann { 0x000002be, 0x00000000, 0x00000000, 0x00 },
1561da177e4SLinus Torvalds /* OGF_STATUS_PARAM */
1577c631a67SMarcel Holtmann { 0x000000ea, 0x00000000, 0x00000000, 0x00 }
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds };
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds static struct bt_sock_list hci_sk_list = {
162d5fb2962SRobert P. J. Day .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
1631da177e4SLinus Torvalds };
1641da177e4SLinus Torvalds
is_filtered_packet(struct sock * sk,struct sk_buff * skb)165f81fe64fSMarcel Holtmann static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
166f81fe64fSMarcel Holtmann {
167f81fe64fSMarcel Holtmann struct hci_filter *flt;
168f81fe64fSMarcel Holtmann int flt_type, flt_event;
169f81fe64fSMarcel Holtmann
170f81fe64fSMarcel Holtmann /* Apply filter */
171f81fe64fSMarcel Holtmann flt = &hci_pi(sk)->filter;
172f81fe64fSMarcel Holtmann
173d79f34e3SMarcel Holtmann flt_type = hci_skb_pkt_type(skb) & HCI_FLT_TYPE_BITS;
174f81fe64fSMarcel Holtmann
175f81fe64fSMarcel Holtmann if (!test_bit(flt_type, &flt->type_mask))
176f81fe64fSMarcel Holtmann return true;
177f81fe64fSMarcel Holtmann
178f81fe64fSMarcel Holtmann /* Extra filter for event packets only */
179d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT)
180f81fe64fSMarcel Holtmann return false;
181f81fe64fSMarcel Holtmann
182f81fe64fSMarcel Holtmann flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
183f81fe64fSMarcel Holtmann
184f81fe64fSMarcel Holtmann if (!hci_test_bit(flt_event, &flt->event_mask))
185f81fe64fSMarcel Holtmann return true;
186f81fe64fSMarcel Holtmann
187f81fe64fSMarcel Holtmann /* Check filter only when opcode is set */
188f81fe64fSMarcel Holtmann if (!flt->opcode)
189f81fe64fSMarcel Holtmann return false;
190f81fe64fSMarcel Holtmann
191f81fe64fSMarcel Holtmann if (flt_event == HCI_EV_CMD_COMPLETE &&
192f81fe64fSMarcel Holtmann flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
193f81fe64fSMarcel Holtmann return true;
194f81fe64fSMarcel Holtmann
195f81fe64fSMarcel Holtmann if (flt_event == HCI_EV_CMD_STATUS &&
196f81fe64fSMarcel Holtmann flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
197f81fe64fSMarcel Holtmann return true;
198f81fe64fSMarcel Holtmann
199f81fe64fSMarcel Holtmann return false;
200f81fe64fSMarcel Holtmann }
201f81fe64fSMarcel Holtmann
2021da177e4SLinus Torvalds /* Send frame to RAW socket */
hci_send_to_sock(struct hci_dev * hdev,struct sk_buff * skb)203470fe1b5SMarcel Holtmann void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds struct sock *sk;
206e0edf373SMarcel Holtmann struct sk_buff *skb_copy = NULL;
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds BT_DBG("hdev %p len %d", hdev, skb->len);
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvalds read_lock(&hci_sk_list.lock);
211470fe1b5SMarcel Holtmann
212b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) {
2131da177e4SLinus Torvalds struct sk_buff *nskb;
2141da177e4SLinus Torvalds
2151da177e4SLinus Torvalds if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
2161da177e4SLinus Torvalds continue;
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds /* Don't send frame to the socket it came from */
2191da177e4SLinus Torvalds if (skb->sk == sk)
2201da177e4SLinus Torvalds continue;
2211da177e4SLinus Torvalds
22223500189SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
223d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
224d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
225d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
226cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
227cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT)
228bb77543eSMarcel Holtmann continue;
229f81fe64fSMarcel Holtmann if (is_filtered_packet(sk, skb))
2301da177e4SLinus Torvalds continue;
23123500189SMarcel Holtmann } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
23223500189SMarcel Holtmann if (!bt_cb(skb)->incoming)
23323500189SMarcel Holtmann continue;
234d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
235d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
236cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
237cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT)
23823500189SMarcel Holtmann continue;
23923500189SMarcel Holtmann } else {
24023500189SMarcel Holtmann /* Don't send frame to other channel types */
24123500189SMarcel Holtmann continue;
24223500189SMarcel Holtmann }
2431da177e4SLinus Torvalds
244e0edf373SMarcel Holtmann if (!skb_copy) {
245e0edf373SMarcel Holtmann /* Create a private copy with headroom */
246bad93e9dSOctavian Purdila skb_copy = __pskb_copy_fclone(skb, 1, GFP_ATOMIC, true);
247e0edf373SMarcel Holtmann if (!skb_copy)
2481da177e4SLinus Torvalds continue;
2491da177e4SLinus Torvalds
2501da177e4SLinus Torvalds /* Put type byte before the data */
251d79f34e3SMarcel Holtmann memcpy(skb_push(skb_copy, 1), &hci_skb_pkt_type(skb), 1);
252e0edf373SMarcel Holtmann }
253e0edf373SMarcel Holtmann
254e0edf373SMarcel Holtmann nskb = skb_clone(skb_copy, GFP_ATOMIC);
255e0edf373SMarcel Holtmann if (!nskb)
256e0edf373SMarcel Holtmann continue;
2571da177e4SLinus Torvalds
2581da177e4SLinus Torvalds if (sock_queue_rcv_skb(sk, nskb))
2591da177e4SLinus Torvalds kfree_skb(nskb);
2601da177e4SLinus Torvalds }
261470fe1b5SMarcel Holtmann
262470fe1b5SMarcel Holtmann read_unlock(&hci_sk_list.lock);
263e0edf373SMarcel Holtmann
264e0edf373SMarcel Holtmann kfree_skb(skb_copy);
265470fe1b5SMarcel Holtmann }
266470fe1b5SMarcel Holtmann
hci_sock_copy_creds(struct sock * sk,struct sk_buff * skb)26769ae5065SLuiz Augusto von Dentz static void hci_sock_copy_creds(struct sock *sk, struct sk_buff *skb)
26869ae5065SLuiz Augusto von Dentz {
26969ae5065SLuiz Augusto von Dentz struct scm_creds *creds;
27069ae5065SLuiz Augusto von Dentz
27169ae5065SLuiz Augusto von Dentz if (!sk || WARN_ON(!skb))
27269ae5065SLuiz Augusto von Dentz return;
27369ae5065SLuiz Augusto von Dentz
27469ae5065SLuiz Augusto von Dentz creds = &bt_cb(skb)->creds;
27569ae5065SLuiz Augusto von Dentz
27669ae5065SLuiz Augusto von Dentz /* Check if peer credentials is set */
27769ae5065SLuiz Augusto von Dentz if (!sk->sk_peer_pid) {
27869ae5065SLuiz Augusto von Dentz /* Check if parent peer credentials is set */
27969ae5065SLuiz Augusto von Dentz if (bt_sk(sk)->parent && bt_sk(sk)->parent->sk_peer_pid)
28069ae5065SLuiz Augusto von Dentz sk = bt_sk(sk)->parent;
28169ae5065SLuiz Augusto von Dentz else
28269ae5065SLuiz Augusto von Dentz return;
28369ae5065SLuiz Augusto von Dentz }
28469ae5065SLuiz Augusto von Dentz
28569ae5065SLuiz Augusto von Dentz /* Check if scm_creds already set */
28669ae5065SLuiz Augusto von Dentz if (creds->pid == pid_vnr(sk->sk_peer_pid))
28769ae5065SLuiz Augusto von Dentz return;
28869ae5065SLuiz Augusto von Dentz
28969ae5065SLuiz Augusto von Dentz memset(creds, 0, sizeof(*creds));
29069ae5065SLuiz Augusto von Dentz
29169ae5065SLuiz Augusto von Dentz creds->pid = pid_vnr(sk->sk_peer_pid);
29269ae5065SLuiz Augusto von Dentz if (sk->sk_peer_cred) {
29369ae5065SLuiz Augusto von Dentz creds->uid = sk->sk_peer_cred->uid;
29469ae5065SLuiz Augusto von Dentz creds->gid = sk->sk_peer_cred->gid;
29569ae5065SLuiz Augusto von Dentz }
29669ae5065SLuiz Augusto von Dentz }
29769ae5065SLuiz Augusto von Dentz
hci_skb_clone(struct sk_buff * skb)29869ae5065SLuiz Augusto von Dentz static struct sk_buff *hci_skb_clone(struct sk_buff *skb)
29969ae5065SLuiz Augusto von Dentz {
30069ae5065SLuiz Augusto von Dentz struct sk_buff *nskb;
30169ae5065SLuiz Augusto von Dentz
30269ae5065SLuiz Augusto von Dentz if (!skb)
30369ae5065SLuiz Augusto von Dentz return NULL;
30469ae5065SLuiz Augusto von Dentz
30569ae5065SLuiz Augusto von Dentz nskb = skb_clone(skb, GFP_ATOMIC);
30669ae5065SLuiz Augusto von Dentz if (!nskb)
30769ae5065SLuiz Augusto von Dentz return NULL;
30869ae5065SLuiz Augusto von Dentz
30969ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(skb->sk, nskb);
31069ae5065SLuiz Augusto von Dentz
31169ae5065SLuiz Augusto von Dentz return nskb;
31269ae5065SLuiz Augusto von Dentz }
31369ae5065SLuiz Augusto von Dentz
3147129069eSJohan Hedberg /* Send frame to sockets with specific channel */
__hci_send_to_channel(unsigned short channel,struct sk_buff * skb,int flag,struct sock * skip_sk)315a9ee77afSSebastian Andrzej Siewior static void __hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
316c08b1a1dSMarcel Holtmann int flag, struct sock *skip_sk)
317470fe1b5SMarcel Holtmann {
318470fe1b5SMarcel Holtmann struct sock *sk;
319470fe1b5SMarcel Holtmann
3207129069eSJohan Hedberg BT_DBG("channel %u len %d", channel, skb->len);
321470fe1b5SMarcel Holtmann
322b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) {
323470fe1b5SMarcel Holtmann struct sk_buff *nskb;
324470fe1b5SMarcel Holtmann
325c08b1a1dSMarcel Holtmann /* Ignore socket without the flag set */
326c85be545SMarcel Holtmann if (!hci_sock_test_flag(sk, flag))
327c08b1a1dSMarcel Holtmann continue;
328c08b1a1dSMarcel Holtmann
329470fe1b5SMarcel Holtmann /* Skip the original socket */
330470fe1b5SMarcel Holtmann if (sk == skip_sk)
331470fe1b5SMarcel Holtmann continue;
332470fe1b5SMarcel Holtmann
333470fe1b5SMarcel Holtmann if (sk->sk_state != BT_BOUND)
334470fe1b5SMarcel Holtmann continue;
335470fe1b5SMarcel Holtmann
3367129069eSJohan Hedberg if (hci_pi(sk)->channel != channel)
337d7f72f61SMarcel Holtmann continue;
338d7f72f61SMarcel Holtmann
33969ae5065SLuiz Augusto von Dentz nskb = hci_skb_clone(skb);
340d7f72f61SMarcel Holtmann if (!nskb)
341d7f72f61SMarcel Holtmann continue;
342d7f72f61SMarcel Holtmann
343d7f72f61SMarcel Holtmann if (sock_queue_rcv_skb(sk, nskb))
344d7f72f61SMarcel Holtmann kfree_skb(nskb);
345d7f72f61SMarcel Holtmann }
346d7f72f61SMarcel Holtmann
347a9ee77afSSebastian Andrzej Siewior }
348a9ee77afSSebastian Andrzej Siewior
hci_send_to_channel(unsigned short channel,struct sk_buff * skb,int flag,struct sock * skip_sk)349a9ee77afSSebastian Andrzej Siewior void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
350a9ee77afSSebastian Andrzej Siewior int flag, struct sock *skip_sk)
351a9ee77afSSebastian Andrzej Siewior {
352a9ee77afSSebastian Andrzej Siewior read_lock(&hci_sk_list.lock);
353a9ee77afSSebastian Andrzej Siewior __hci_send_to_channel(channel, skb, flag, skip_sk);
354d7f72f61SMarcel Holtmann read_unlock(&hci_sk_list.lock);
355d7f72f61SMarcel Holtmann }
356d7f72f61SMarcel Holtmann
357cd82e61cSMarcel Holtmann /* Send frame to monitor socket */
hci_send_to_monitor(struct hci_dev * hdev,struct sk_buff * skb)358cd82e61cSMarcel Holtmann void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
359cd82e61cSMarcel Holtmann {
360cd82e61cSMarcel Holtmann struct sk_buff *skb_copy = NULL;
3612b531294SMarcel Holtmann struct hci_mon_hdr *hdr;
362cd82e61cSMarcel Holtmann __le16 opcode;
363cd82e61cSMarcel Holtmann
364cd82e61cSMarcel Holtmann if (!atomic_read(&monitor_promisc))
365cd82e61cSMarcel Holtmann return;
366cd82e61cSMarcel Holtmann
367cd82e61cSMarcel Holtmann BT_DBG("hdev %p len %d", hdev, skb->len);
368cd82e61cSMarcel Holtmann
369d79f34e3SMarcel Holtmann switch (hci_skb_pkt_type(skb)) {
370cd82e61cSMarcel Holtmann case HCI_COMMAND_PKT:
371dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_COMMAND_PKT);
372cd82e61cSMarcel Holtmann break;
373cd82e61cSMarcel Holtmann case HCI_EVENT_PKT:
374dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
375cd82e61cSMarcel Holtmann break;
376cd82e61cSMarcel Holtmann case HCI_ACLDATA_PKT:
377cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming)
378dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
379cd82e61cSMarcel Holtmann else
380dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
381cd82e61cSMarcel Holtmann break;
382cd82e61cSMarcel Holtmann case HCI_SCODATA_PKT:
383cd82e61cSMarcel Holtmann if (bt_cb(skb)->incoming)
384dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
385cd82e61cSMarcel Holtmann else
386dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
387cd82e61cSMarcel Holtmann break;
388f9a619dbSLuiz Augusto von Dentz case HCI_ISODATA_PKT:
389f9a619dbSLuiz Augusto von Dentz if (bt_cb(skb)->incoming)
390f9a619dbSLuiz Augusto von Dentz opcode = cpu_to_le16(HCI_MON_ISO_RX_PKT);
391f9a619dbSLuiz Augusto von Dentz else
392f9a619dbSLuiz Augusto von Dentz opcode = cpu_to_le16(HCI_MON_ISO_TX_PKT);
393f9a619dbSLuiz Augusto von Dentz break;
394e875ff84SMarcel Holtmann case HCI_DIAG_PKT:
395e875ff84SMarcel Holtmann opcode = cpu_to_le16(HCI_MON_VENDOR_DIAG);
396e875ff84SMarcel Holtmann break;
397cd82e61cSMarcel Holtmann default:
398cd82e61cSMarcel Holtmann return;
399cd82e61cSMarcel Holtmann }
400cd82e61cSMarcel Holtmann
4012b531294SMarcel Holtmann /* Create a private copy with headroom */
4022b531294SMarcel Holtmann skb_copy = __pskb_copy_fclone(skb, HCI_MON_HDR_SIZE, GFP_ATOMIC, true);
4032b531294SMarcel Holtmann if (!skb_copy)
4042b531294SMarcel Holtmann return;
4052b531294SMarcel Holtmann
40669ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(skb->sk, skb_copy);
40769ae5065SLuiz Augusto von Dentz
4082b531294SMarcel Holtmann /* Put header before the data */
409d58ff351SJohannes Berg hdr = skb_push(skb_copy, HCI_MON_HDR_SIZE);
4102b531294SMarcel Holtmann hdr->opcode = opcode;
4112b531294SMarcel Holtmann hdr->index = cpu_to_le16(hdev->id);
4122b531294SMarcel Holtmann hdr->len = cpu_to_le16(skb->len);
4132b531294SMarcel Holtmann
414c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
415c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
416cd82e61cSMarcel Holtmann kfree_skb(skb_copy);
417cd82e61cSMarcel Holtmann }
418cd82e61cSMarcel Holtmann
hci_send_monitor_ctrl_event(struct hci_dev * hdev,u16 event,void * data,u16 data_len,ktime_t tstamp,int flag,struct sock * skip_sk)41938ceaa00SMarcel Holtmann void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
42038ceaa00SMarcel Holtmann void *data, u16 data_len, ktime_t tstamp,
42138ceaa00SMarcel Holtmann int flag, struct sock *skip_sk)
42238ceaa00SMarcel Holtmann {
42338ceaa00SMarcel Holtmann struct sock *sk;
42438ceaa00SMarcel Holtmann __le16 index;
42538ceaa00SMarcel Holtmann
42638ceaa00SMarcel Holtmann if (hdev)
42738ceaa00SMarcel Holtmann index = cpu_to_le16(hdev->id);
42838ceaa00SMarcel Holtmann else
42938ceaa00SMarcel Holtmann index = cpu_to_le16(MGMT_INDEX_NONE);
43038ceaa00SMarcel Holtmann
43138ceaa00SMarcel Holtmann read_lock(&hci_sk_list.lock);
43238ceaa00SMarcel Holtmann
43338ceaa00SMarcel Holtmann sk_for_each(sk, &hci_sk_list.head) {
43438ceaa00SMarcel Holtmann struct hci_mon_hdr *hdr;
43538ceaa00SMarcel Holtmann struct sk_buff *skb;
43638ceaa00SMarcel Holtmann
43738ceaa00SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
43838ceaa00SMarcel Holtmann continue;
43938ceaa00SMarcel Holtmann
44038ceaa00SMarcel Holtmann /* Ignore socket without the flag set */
44138ceaa00SMarcel Holtmann if (!hci_sock_test_flag(sk, flag))
44238ceaa00SMarcel Holtmann continue;
44338ceaa00SMarcel Holtmann
44438ceaa00SMarcel Holtmann /* Skip the original socket */
44538ceaa00SMarcel Holtmann if (sk == skip_sk)
44638ceaa00SMarcel Holtmann continue;
44738ceaa00SMarcel Holtmann
44838ceaa00SMarcel Holtmann skb = bt_skb_alloc(6 + data_len, GFP_ATOMIC);
44938ceaa00SMarcel Holtmann if (!skb)
45038ceaa00SMarcel Holtmann continue;
45138ceaa00SMarcel Holtmann
45238ceaa00SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
45338ceaa00SMarcel Holtmann put_unaligned_le16(event, skb_put(skb, 2));
45438ceaa00SMarcel Holtmann
45538ceaa00SMarcel Holtmann if (data)
45659ae1d12SJohannes Berg skb_put_data(skb, data, data_len);
45738ceaa00SMarcel Holtmann
45838ceaa00SMarcel Holtmann skb->tstamp = tstamp;
45938ceaa00SMarcel Holtmann
460d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE);
46138ceaa00SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
46238ceaa00SMarcel Holtmann hdr->index = index;
46338ceaa00SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
46438ceaa00SMarcel Holtmann
465a9ee77afSSebastian Andrzej Siewior __hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
46638ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
46738ceaa00SMarcel Holtmann kfree_skb(skb);
46838ceaa00SMarcel Holtmann }
46938ceaa00SMarcel Holtmann
47038ceaa00SMarcel Holtmann read_unlock(&hci_sk_list.lock);
47138ceaa00SMarcel Holtmann }
47238ceaa00SMarcel Holtmann
create_monitor_event(struct hci_dev * hdev,int event)473cd82e61cSMarcel Holtmann static struct sk_buff *create_monitor_event(struct hci_dev *hdev, int event)
474cd82e61cSMarcel Holtmann {
475cd82e61cSMarcel Holtmann struct hci_mon_hdr *hdr;
476cd82e61cSMarcel Holtmann struct hci_mon_new_index *ni;
4776c566dd5SMarcel Holtmann struct hci_mon_index_info *ii;
478cd82e61cSMarcel Holtmann struct sk_buff *skb;
479cd82e61cSMarcel Holtmann __le16 opcode;
480cd82e61cSMarcel Holtmann
481cd82e61cSMarcel Holtmann switch (event) {
482cd82e61cSMarcel Holtmann case HCI_DEV_REG:
483cd82e61cSMarcel Holtmann skb = bt_skb_alloc(HCI_MON_NEW_INDEX_SIZE, GFP_ATOMIC);
484cd82e61cSMarcel Holtmann if (!skb)
485cd82e61cSMarcel Holtmann return NULL;
486cd82e61cSMarcel Holtmann
4874df864c1SJohannes Berg ni = skb_put(skb, HCI_MON_NEW_INDEX_SIZE);
4885af2e235SLuiz Augusto von Dentz ni->type = 0x00; /* Old hdev->dev_type */
489cd82e61cSMarcel Holtmann ni->bus = hdev->bus;
490cd82e61cSMarcel Holtmann bacpy(&ni->bdaddr, &hdev->bdaddr);
491cb3871b1SKees Cook memcpy_and_pad(ni->name, sizeof(ni->name), hdev->name,
492cb3871b1SKees Cook strnlen(hdev->name, sizeof(ni->name)), '\0');
493cd82e61cSMarcel Holtmann
494dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_NEW_INDEX);
495cd82e61cSMarcel Holtmann break;
496cd82e61cSMarcel Holtmann
497cd82e61cSMarcel Holtmann case HCI_DEV_UNREG:
498cd82e61cSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC);
499cd82e61cSMarcel Holtmann if (!skb)
500cd82e61cSMarcel Holtmann return NULL;
501cd82e61cSMarcel Holtmann
502dcf4adbfSJoe Perches opcode = cpu_to_le16(HCI_MON_DEL_INDEX);
503cd82e61cSMarcel Holtmann break;
504cd82e61cSMarcel Holtmann
505e131d74aSMarcel Holtmann case HCI_DEV_SETUP:
506e131d74aSMarcel Holtmann if (hdev->manufacturer == 0xffff)
507e131d74aSMarcel Holtmann return NULL;
50819186c7bSGustavo A. R. Silva fallthrough;
509e131d74aSMarcel Holtmann
5106c566dd5SMarcel Holtmann case HCI_DEV_UP:
5116c566dd5SMarcel Holtmann skb = bt_skb_alloc(HCI_MON_INDEX_INFO_SIZE, GFP_ATOMIC);
5126c566dd5SMarcel Holtmann if (!skb)
5136c566dd5SMarcel Holtmann return NULL;
5146c566dd5SMarcel Holtmann
5154df864c1SJohannes Berg ii = skb_put(skb, HCI_MON_INDEX_INFO_SIZE);
5166c566dd5SMarcel Holtmann bacpy(&ii->bdaddr, &hdev->bdaddr);
5176c566dd5SMarcel Holtmann ii->manufacturer = cpu_to_le16(hdev->manufacturer);
5186c566dd5SMarcel Holtmann
5196c566dd5SMarcel Holtmann opcode = cpu_to_le16(HCI_MON_INDEX_INFO);
5206c566dd5SMarcel Holtmann break;
5216c566dd5SMarcel Holtmann
52222db3cbcSMarcel Holtmann case HCI_DEV_OPEN:
52322db3cbcSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC);
52422db3cbcSMarcel Holtmann if (!skb)
52522db3cbcSMarcel Holtmann return NULL;
52622db3cbcSMarcel Holtmann
52722db3cbcSMarcel Holtmann opcode = cpu_to_le16(HCI_MON_OPEN_INDEX);
52822db3cbcSMarcel Holtmann break;
52922db3cbcSMarcel Holtmann
53022db3cbcSMarcel Holtmann case HCI_DEV_CLOSE:
53122db3cbcSMarcel Holtmann skb = bt_skb_alloc(0, GFP_ATOMIC);
53222db3cbcSMarcel Holtmann if (!skb)
53322db3cbcSMarcel Holtmann return NULL;
53422db3cbcSMarcel Holtmann
53522db3cbcSMarcel Holtmann opcode = cpu_to_le16(HCI_MON_CLOSE_INDEX);
53622db3cbcSMarcel Holtmann break;
53722db3cbcSMarcel Holtmann
538cd82e61cSMarcel Holtmann default:
539cd82e61cSMarcel Holtmann return NULL;
540cd82e61cSMarcel Holtmann }
541cd82e61cSMarcel Holtmann
542cd82e61cSMarcel Holtmann __net_timestamp(skb);
543cd82e61cSMarcel Holtmann
544d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE);
545cd82e61cSMarcel Holtmann hdr->opcode = opcode;
546cd82e61cSMarcel Holtmann hdr->index = cpu_to_le16(hdev->id);
547cd82e61cSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
548cd82e61cSMarcel Holtmann
549cd82e61cSMarcel Holtmann return skb;
550cd82e61cSMarcel Holtmann }
551cd82e61cSMarcel Holtmann
create_monitor_ctrl_open(struct sock * sk)552249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_open(struct sock *sk)
553249fa169SMarcel Holtmann {
554249fa169SMarcel Holtmann struct hci_mon_hdr *hdr;
555249fa169SMarcel Holtmann struct sk_buff *skb;
556d0bef1d2SMarcel Holtmann u16 format;
557249fa169SMarcel Holtmann u8 ver[3];
558249fa169SMarcel Holtmann u32 flags;
559249fa169SMarcel Holtmann
5600ef2c42fSMarcel Holtmann /* No message needed when cookie is not present */
5610ef2c42fSMarcel Holtmann if (!hci_pi(sk)->cookie)
5620ef2c42fSMarcel Holtmann return NULL;
5630ef2c42fSMarcel Holtmann
564d0bef1d2SMarcel Holtmann switch (hci_pi(sk)->channel) {
565f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW:
566f81f5b2dSMarcel Holtmann format = 0x0000;
567f81f5b2dSMarcel Holtmann ver[0] = BT_SUBSYS_VERSION;
568f81f5b2dSMarcel Holtmann put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
569f81f5b2dSMarcel Holtmann break;
570aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER:
571aa1638ddSMarcel Holtmann format = 0x0001;
572aa1638ddSMarcel Holtmann ver[0] = BT_SUBSYS_VERSION;
573aa1638ddSMarcel Holtmann put_unaligned_le16(BT_SUBSYS_REVISION, ver + 1);
574aa1638ddSMarcel Holtmann break;
575d0bef1d2SMarcel Holtmann case HCI_CHANNEL_CONTROL:
576d0bef1d2SMarcel Holtmann format = 0x0002;
577d0bef1d2SMarcel Holtmann mgmt_fill_version_info(ver);
578d0bef1d2SMarcel Holtmann break;
579d0bef1d2SMarcel Holtmann default:
580d0bef1d2SMarcel Holtmann /* No message for unsupported format */
581d0bef1d2SMarcel Holtmann return NULL;
582d0bef1d2SMarcel Holtmann }
583d0bef1d2SMarcel Holtmann
584249fa169SMarcel Holtmann skb = bt_skb_alloc(14 + TASK_COMM_LEN, GFP_ATOMIC);
585249fa169SMarcel Holtmann if (!skb)
586249fa169SMarcel Holtmann return NULL;
587249fa169SMarcel Holtmann
58869ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb);
58969ae5065SLuiz Augusto von Dentz
590249fa169SMarcel Holtmann flags = hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) ? 0x1 : 0x0;
591249fa169SMarcel Holtmann
592249fa169SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
593249fa169SMarcel Holtmann put_unaligned_le16(format, skb_put(skb, 2));
59459ae1d12SJohannes Berg skb_put_data(skb, ver, sizeof(ver));
595249fa169SMarcel Holtmann put_unaligned_le32(flags, skb_put(skb, 4));
596634fef61SJohannes Berg skb_put_u8(skb, TASK_COMM_LEN);
59759ae1d12SJohannes Berg skb_put_data(skb, hci_pi(sk)->comm, TASK_COMM_LEN);
598249fa169SMarcel Holtmann
599249fa169SMarcel Holtmann __net_timestamp(skb);
600249fa169SMarcel Holtmann
601d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE);
602249fa169SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_OPEN);
6030ef2c42fSMarcel Holtmann if (hci_pi(sk)->hdev)
6040ef2c42fSMarcel Holtmann hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
6050ef2c42fSMarcel Holtmann else
606249fa169SMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE);
607249fa169SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
608249fa169SMarcel Holtmann
609249fa169SMarcel Holtmann return skb;
610249fa169SMarcel Holtmann }
611249fa169SMarcel Holtmann
create_monitor_ctrl_close(struct sock * sk)612249fa169SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_close(struct sock *sk)
613249fa169SMarcel Holtmann {
614249fa169SMarcel Holtmann struct hci_mon_hdr *hdr;
615249fa169SMarcel Holtmann struct sk_buff *skb;
616249fa169SMarcel Holtmann
6170ef2c42fSMarcel Holtmann /* No message needed when cookie is not present */
6180ef2c42fSMarcel Holtmann if (!hci_pi(sk)->cookie)
6190ef2c42fSMarcel Holtmann return NULL;
6200ef2c42fSMarcel Holtmann
621d0bef1d2SMarcel Holtmann switch (hci_pi(sk)->channel) {
622f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW:
623aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER:
624d0bef1d2SMarcel Holtmann case HCI_CHANNEL_CONTROL:
625d0bef1d2SMarcel Holtmann break;
626d0bef1d2SMarcel Holtmann default:
627d0bef1d2SMarcel Holtmann /* No message for unsupported format */
628d0bef1d2SMarcel Holtmann return NULL;
629d0bef1d2SMarcel Holtmann }
630d0bef1d2SMarcel Holtmann
631249fa169SMarcel Holtmann skb = bt_skb_alloc(4, GFP_ATOMIC);
632249fa169SMarcel Holtmann if (!skb)
633249fa169SMarcel Holtmann return NULL;
634249fa169SMarcel Holtmann
63569ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb);
63669ae5065SLuiz Augusto von Dentz
637249fa169SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
638249fa169SMarcel Holtmann
639249fa169SMarcel Holtmann __net_timestamp(skb);
640249fa169SMarcel Holtmann
641d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE);
642249fa169SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_CLOSE);
6430ef2c42fSMarcel Holtmann if (hci_pi(sk)->hdev)
6440ef2c42fSMarcel Holtmann hdr->index = cpu_to_le16(hci_pi(sk)->hdev->id);
6450ef2c42fSMarcel Holtmann else
646249fa169SMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE);
647249fa169SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
648249fa169SMarcel Holtmann
649249fa169SMarcel Holtmann return skb;
650249fa169SMarcel Holtmann }
651249fa169SMarcel Holtmann
create_monitor_ctrl_command(struct sock * sk,u16 index,u16 opcode,u16 len,const void * buf)65238ceaa00SMarcel Holtmann static struct sk_buff *create_monitor_ctrl_command(struct sock *sk, u16 index,
65338ceaa00SMarcel Holtmann u16 opcode, u16 len,
65438ceaa00SMarcel Holtmann const void *buf)
65538ceaa00SMarcel Holtmann {
65638ceaa00SMarcel Holtmann struct hci_mon_hdr *hdr;
65738ceaa00SMarcel Holtmann struct sk_buff *skb;
65838ceaa00SMarcel Holtmann
65938ceaa00SMarcel Holtmann skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
66038ceaa00SMarcel Holtmann if (!skb)
66138ceaa00SMarcel Holtmann return NULL;
66238ceaa00SMarcel Holtmann
66369ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb);
66469ae5065SLuiz Augusto von Dentz
66538ceaa00SMarcel Holtmann put_unaligned_le32(hci_pi(sk)->cookie, skb_put(skb, 4));
66638ceaa00SMarcel Holtmann put_unaligned_le16(opcode, skb_put(skb, 2));
66738ceaa00SMarcel Holtmann
66838ceaa00SMarcel Holtmann if (buf)
66959ae1d12SJohannes Berg skb_put_data(skb, buf, len);
67038ceaa00SMarcel Holtmann
67138ceaa00SMarcel Holtmann __net_timestamp(skb);
67238ceaa00SMarcel Holtmann
673d58ff351SJohannes Berg hdr = skb_push(skb, HCI_MON_HDR_SIZE);
67438ceaa00SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_CTRL_COMMAND);
67538ceaa00SMarcel Holtmann hdr->index = cpu_to_le16(index);
67638ceaa00SMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
67738ceaa00SMarcel Holtmann
67838ceaa00SMarcel Holtmann return skb;
67938ceaa00SMarcel Holtmann }
68038ceaa00SMarcel Holtmann
681787b306cSJohannes Berg static void __printf(2, 3)
send_monitor_note(struct sock * sk,const char * fmt,...)682787b306cSJohannes Berg send_monitor_note(struct sock *sk, const char *fmt, ...)
683dd31506dSMarcel Holtmann {
684787b306cSJohannes Berg size_t len;
685dd31506dSMarcel Holtmann struct hci_mon_hdr *hdr;
686dd31506dSMarcel Holtmann struct sk_buff *skb;
687787b306cSJohannes Berg va_list args;
688787b306cSJohannes Berg
689787b306cSJohannes Berg va_start(args, fmt);
690787b306cSJohannes Berg len = vsnprintf(NULL, 0, fmt, args);
691787b306cSJohannes Berg va_end(args);
692dd31506dSMarcel Holtmann
693dd31506dSMarcel Holtmann skb = bt_skb_alloc(len + 1, GFP_ATOMIC);
694dd31506dSMarcel Holtmann if (!skb)
695dd31506dSMarcel Holtmann return;
696dd31506dSMarcel Holtmann
69769ae5065SLuiz Augusto von Dentz hci_sock_copy_creds(sk, skb);
69869ae5065SLuiz Augusto von Dentz
699787b306cSJohannes Berg va_start(args, fmt);
700787b306cSJohannes Berg vsprintf(skb_put(skb, len), fmt, args);
7014df864c1SJohannes Berg *(u8 *)skb_put(skb, 1) = 0;
702787b306cSJohannes Berg va_end(args);
703dd31506dSMarcel Holtmann
704dd31506dSMarcel Holtmann __net_timestamp(skb);
705dd31506dSMarcel Holtmann
706dd31506dSMarcel Holtmann hdr = (void *)skb_push(skb, HCI_MON_HDR_SIZE);
707dd31506dSMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_SYSTEM_NOTE);
708dd31506dSMarcel Holtmann hdr->index = cpu_to_le16(HCI_DEV_NONE);
709dd31506dSMarcel Holtmann hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
710dd31506dSMarcel Holtmann
711dd31506dSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb))
712dd31506dSMarcel Holtmann kfree_skb(skb);
713dd31506dSMarcel Holtmann }
714dd31506dSMarcel Holtmann
send_monitor_replay(struct sock * sk)715cd82e61cSMarcel Holtmann static void send_monitor_replay(struct sock *sk)
716cd82e61cSMarcel Holtmann {
717cd82e61cSMarcel Holtmann struct hci_dev *hdev;
718cd82e61cSMarcel Holtmann
719cd82e61cSMarcel Holtmann read_lock(&hci_dev_list_lock);
720cd82e61cSMarcel Holtmann
721cd82e61cSMarcel Holtmann list_for_each_entry(hdev, &hci_dev_list, list) {
722cd82e61cSMarcel Holtmann struct sk_buff *skb;
723cd82e61cSMarcel Holtmann
724cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_REG);
725cd82e61cSMarcel Holtmann if (!skb)
726cd82e61cSMarcel Holtmann continue;
727cd82e61cSMarcel Holtmann
728cd82e61cSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb))
729cd82e61cSMarcel Holtmann kfree_skb(skb);
73022db3cbcSMarcel Holtmann
73122db3cbcSMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags))
73222db3cbcSMarcel Holtmann continue;
73322db3cbcSMarcel Holtmann
73422db3cbcSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_OPEN);
73522db3cbcSMarcel Holtmann if (!skb)
73622db3cbcSMarcel Holtmann continue;
73722db3cbcSMarcel Holtmann
73822db3cbcSMarcel Holtmann if (sock_queue_rcv_skb(sk, skb))
73922db3cbcSMarcel Holtmann kfree_skb(skb);
7406c566dd5SMarcel Holtmann
741e131d74aSMarcel Holtmann if (test_bit(HCI_UP, &hdev->flags))
7426c566dd5SMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_UP);
743e131d74aSMarcel Holtmann else if (hci_dev_test_flag(hdev, HCI_SETUP))
744e131d74aSMarcel Holtmann skb = create_monitor_event(hdev, HCI_DEV_SETUP);
745e131d74aSMarcel Holtmann else
746e131d74aSMarcel Holtmann skb = NULL;
7476c566dd5SMarcel Holtmann
748e131d74aSMarcel Holtmann if (skb) {
7496c566dd5SMarcel Holtmann if (sock_queue_rcv_skb(sk, skb))
7506c566dd5SMarcel Holtmann kfree_skb(skb);
751cd82e61cSMarcel Holtmann }
752e131d74aSMarcel Holtmann }
753cd82e61cSMarcel Holtmann
754cd82e61cSMarcel Holtmann read_unlock(&hci_dev_list_lock);
755cd82e61cSMarcel Holtmann }
756cd82e61cSMarcel Holtmann
send_monitor_control_replay(struct sock * mon_sk)757249fa169SMarcel Holtmann static void send_monitor_control_replay(struct sock *mon_sk)
758249fa169SMarcel Holtmann {
759249fa169SMarcel Holtmann struct sock *sk;
760249fa169SMarcel Holtmann
761249fa169SMarcel Holtmann read_lock(&hci_sk_list.lock);
762249fa169SMarcel Holtmann
763249fa169SMarcel Holtmann sk_for_each(sk, &hci_sk_list.head) {
764249fa169SMarcel Holtmann struct sk_buff *skb;
765249fa169SMarcel Holtmann
766249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk);
767249fa169SMarcel Holtmann if (!skb)
768249fa169SMarcel Holtmann continue;
769249fa169SMarcel Holtmann
770249fa169SMarcel Holtmann if (sock_queue_rcv_skb(mon_sk, skb))
771249fa169SMarcel Holtmann kfree_skb(skb);
772249fa169SMarcel Holtmann }
773249fa169SMarcel Holtmann
774249fa169SMarcel Holtmann read_unlock(&hci_sk_list.lock);
775249fa169SMarcel Holtmann }
776249fa169SMarcel Holtmann
777040030efSMarcel Holtmann /* Generate internal stack event */
hci_si_event(struct hci_dev * hdev,int type,int dlen,void * data)778040030efSMarcel Holtmann static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
779040030efSMarcel Holtmann {
780040030efSMarcel Holtmann struct hci_event_hdr *hdr;
781040030efSMarcel Holtmann struct hci_ev_stack_internal *ev;
782040030efSMarcel Holtmann struct sk_buff *skb;
783040030efSMarcel Holtmann
784040030efSMarcel Holtmann skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
785040030efSMarcel Holtmann if (!skb)
786040030efSMarcel Holtmann return;
787040030efSMarcel Holtmann
7884df864c1SJohannes Berg hdr = skb_put(skb, HCI_EVENT_HDR_SIZE);
789040030efSMarcel Holtmann hdr->evt = HCI_EV_STACK_INTERNAL;
790040030efSMarcel Holtmann hdr->plen = sizeof(*ev) + dlen;
791040030efSMarcel Holtmann
7924df864c1SJohannes Berg ev = skb_put(skb, sizeof(*ev) + dlen);
793040030efSMarcel Holtmann ev->type = type;
794040030efSMarcel Holtmann memcpy(ev->data, data, dlen);
795040030efSMarcel Holtmann
796040030efSMarcel Holtmann bt_cb(skb)->incoming = 1;
797040030efSMarcel Holtmann __net_timestamp(skb);
798040030efSMarcel Holtmann
799d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
800040030efSMarcel Holtmann hci_send_to_sock(hdev, skb);
801040030efSMarcel Holtmann kfree_skb(skb);
802040030efSMarcel Holtmann }
803040030efSMarcel Holtmann
hci_sock_dev_event(struct hci_dev * hdev,int event)804040030efSMarcel Holtmann void hci_sock_dev_event(struct hci_dev *hdev, int event)
805040030efSMarcel Holtmann {
806040030efSMarcel Holtmann BT_DBG("hdev %s event %d", hdev->name, event);
807040030efSMarcel Holtmann
808cd82e61cSMarcel Holtmann if (atomic_read(&monitor_promisc)) {
809cd82e61cSMarcel Holtmann struct sk_buff *skb;
810cd82e61cSMarcel Holtmann
811ed1b28a4SMarcel Holtmann /* Send event to monitor */
812cd82e61cSMarcel Holtmann skb = create_monitor_event(hdev, event);
813cd82e61cSMarcel Holtmann if (skb) {
814c08b1a1dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
815c08b1a1dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
816cd82e61cSMarcel Holtmann kfree_skb(skb);
817cd82e61cSMarcel Holtmann }
818cd82e61cSMarcel Holtmann }
819cd82e61cSMarcel Holtmann
820ed1b28a4SMarcel Holtmann if (event <= HCI_DEV_DOWN) {
821ed1b28a4SMarcel Holtmann struct hci_ev_si_device ev;
822ed1b28a4SMarcel Holtmann
823040030efSMarcel Holtmann /* Send event to sockets */
824040030efSMarcel Holtmann ev.event = event;
825040030efSMarcel Holtmann ev.dev_id = hdev->id;
826040030efSMarcel Holtmann hci_si_event(NULL, HCI_EV_SI_DEVICE, sizeof(ev), &ev);
827ed1b28a4SMarcel Holtmann }
828040030efSMarcel Holtmann
829040030efSMarcel Holtmann if (event == HCI_DEV_UNREG) {
830040030efSMarcel Holtmann struct sock *sk;
831040030efSMarcel Holtmann
832e0448092STetsuo Handa /* Wake up sockets using this dead device */
833040030efSMarcel Holtmann read_lock(&hci_sk_list.lock);
834b67bfe0dSSasha Levin sk_for_each(sk, &hci_sk_list.head) {
835040030efSMarcel Holtmann if (hci_pi(sk)->hdev == hdev) {
836040030efSMarcel Holtmann sk->sk_err = EPIPE;
837040030efSMarcel Holtmann sk->sk_state_change(sk);
838040030efSMarcel Holtmann }
839040030efSMarcel Holtmann }
840040030efSMarcel Holtmann read_unlock(&hci_sk_list.lock);
841040030efSMarcel Holtmann }
842040030efSMarcel Holtmann }
843040030efSMarcel Holtmann
__hci_mgmt_chan_find(unsigned short channel)844801c1e8dSJohan Hedberg static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
845801c1e8dSJohan Hedberg {
846801c1e8dSJohan Hedberg struct hci_mgmt_chan *c;
847801c1e8dSJohan Hedberg
848801c1e8dSJohan Hedberg list_for_each_entry(c, &mgmt_chan_list, list) {
849801c1e8dSJohan Hedberg if (c->channel == channel)
850801c1e8dSJohan Hedberg return c;
851801c1e8dSJohan Hedberg }
852801c1e8dSJohan Hedberg
853801c1e8dSJohan Hedberg return NULL;
854801c1e8dSJohan Hedberg }
855801c1e8dSJohan Hedberg
hci_mgmt_chan_find(unsigned short channel)856801c1e8dSJohan Hedberg static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
857801c1e8dSJohan Hedberg {
858801c1e8dSJohan Hedberg struct hci_mgmt_chan *c;
859801c1e8dSJohan Hedberg
860801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock);
861801c1e8dSJohan Hedberg c = __hci_mgmt_chan_find(channel);
862801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock);
863801c1e8dSJohan Hedberg
864801c1e8dSJohan Hedberg return c;
865801c1e8dSJohan Hedberg }
866801c1e8dSJohan Hedberg
hci_mgmt_chan_register(struct hci_mgmt_chan * c)867801c1e8dSJohan Hedberg int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
868801c1e8dSJohan Hedberg {
869801c1e8dSJohan Hedberg if (c->channel < HCI_CHANNEL_CONTROL)
870801c1e8dSJohan Hedberg return -EINVAL;
871801c1e8dSJohan Hedberg
872801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock);
873801c1e8dSJohan Hedberg if (__hci_mgmt_chan_find(c->channel)) {
874801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock);
875801c1e8dSJohan Hedberg return -EALREADY;
876801c1e8dSJohan Hedberg }
877801c1e8dSJohan Hedberg
878801c1e8dSJohan Hedberg list_add_tail(&c->list, &mgmt_chan_list);
879801c1e8dSJohan Hedberg
880801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock);
881801c1e8dSJohan Hedberg
882801c1e8dSJohan Hedberg return 0;
883801c1e8dSJohan Hedberg }
884801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_register);
885801c1e8dSJohan Hedberg
hci_mgmt_chan_unregister(struct hci_mgmt_chan * c)886801c1e8dSJohan Hedberg void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
887801c1e8dSJohan Hedberg {
888801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock);
889801c1e8dSJohan Hedberg list_del(&c->list);
890801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock);
891801c1e8dSJohan Hedberg }
892801c1e8dSJohan Hedberg EXPORT_SYMBOL(hci_mgmt_chan_unregister);
893801c1e8dSJohan Hedberg
hci_sock_release(struct socket * sock)8941da177e4SLinus Torvalds static int hci_sock_release(struct socket *sock)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds struct sock *sk = sock->sk;
8977b005bd3SMarcel Holtmann struct hci_dev *hdev;
898249fa169SMarcel Holtmann struct sk_buff *skb;
8991da177e4SLinus Torvalds
9001da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk);
9011da177e4SLinus Torvalds
9021da177e4SLinus Torvalds if (!sk)
9031da177e4SLinus Torvalds return 0;
9041da177e4SLinus Torvalds
90511eb85ecSDan Carpenter lock_sock(sk);
90611eb85ecSDan Carpenter
90770ecce91SMarcel Holtmann switch (hci_pi(sk)->channel) {
90870ecce91SMarcel Holtmann case HCI_CHANNEL_MONITOR:
909cd82e61cSMarcel Holtmann atomic_dec(&monitor_promisc);
91070ecce91SMarcel Holtmann break;
911f81f5b2dSMarcel Holtmann case HCI_CHANNEL_RAW:
912aa1638ddSMarcel Holtmann case HCI_CHANNEL_USER:
91370ecce91SMarcel Holtmann case HCI_CHANNEL_CONTROL:
914249fa169SMarcel Holtmann /* Send event to monitor */
915249fa169SMarcel Holtmann skb = create_monitor_ctrl_close(sk);
916249fa169SMarcel Holtmann if (skb) {
917249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
918249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
919249fa169SMarcel Holtmann kfree_skb(skb);
920249fa169SMarcel Holtmann }
921249fa169SMarcel Holtmann
922df1cb87aSMarcel Holtmann hci_sock_free_cookie(sk);
92370ecce91SMarcel Holtmann break;
92470ecce91SMarcel Holtmann }
925cd82e61cSMarcel Holtmann
9261da177e4SLinus Torvalds bt_sock_unlink(&hci_sk_list, sk);
9271da177e4SLinus Torvalds
928e20a2e9cSMyungho Jung hdev = hci_pi(sk)->hdev;
9291da177e4SLinus Torvalds if (hdev) {
9300acef50bSAbhishek Pandit-Subedi if (hci_pi(sk)->channel == HCI_CHANNEL_USER &&
9310acef50bSAbhishek Pandit-Subedi !hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
9329332ef9dSMasahiro Yamada /* When releasing a user channel exclusive access,
9336b3cc1dbSSimon Fels * call hci_dev_do_close directly instead of calling
9346b3cc1dbSSimon Fels * hci_dev_close to ensure the exclusive access will
9356b3cc1dbSSimon Fels * be released and the controller brought back down.
9366b3cc1dbSSimon Fels *
9376b3cc1dbSSimon Fels * The checking of HCI_AUTO_OFF is not needed in this
9386b3cc1dbSSimon Fels * case since it will have been cleared already when
9396b3cc1dbSSimon Fels * opening the user channel.
9400acef50bSAbhishek Pandit-Subedi *
9410acef50bSAbhishek Pandit-Subedi * Make sure to also check that we haven't already
9420acef50bSAbhishek Pandit-Subedi * unregistered since all the cleanup will have already
9430acef50bSAbhishek Pandit-Subedi * been complete and hdev will get released when we put
9440acef50bSAbhishek Pandit-Subedi * below.
9456b3cc1dbSSimon Fels */
9466b3cc1dbSSimon Fels hci_dev_do_close(hdev);
9479380f9eaSLoic Poulain hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
9489380f9eaSLoic Poulain mgmt_index_added(hdev);
94923500189SMarcel Holtmann }
95023500189SMarcel Holtmann
9511da177e4SLinus Torvalds atomic_dec(&hdev->promisc);
9521da177e4SLinus Torvalds hci_dev_put(hdev);
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds
9551da177e4SLinus Torvalds sock_orphan(sk);
95611eb85ecSDan Carpenter release_sock(sk);
9571da177e4SLinus Torvalds sock_put(sk);
9581da177e4SLinus Torvalds return 0;
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds
hci_sock_reject_list_add(struct hci_dev * hdev,void __user * arg)9613d4f9c00SArchie Pusaka static int hci_sock_reject_list_add(struct hci_dev *hdev, void __user *arg)
962f0358568SJohan Hedberg {
963f0358568SJohan Hedberg bdaddr_t bdaddr;
9645e762444SAntti Julku int err;
965f0358568SJohan Hedberg
966f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
967f0358568SJohan Hedberg return -EFAULT;
968f0358568SJohan Hedberg
96909fd0de5SGustavo F. Padovan hci_dev_lock(hdev);
9705e762444SAntti Julku
9713d4f9c00SArchie Pusaka err = hci_bdaddr_list_add(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9725e762444SAntti Julku
97309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev);
9745e762444SAntti Julku
9755e762444SAntti Julku return err;
976f0358568SJohan Hedberg }
977f0358568SJohan Hedberg
hci_sock_reject_list_del(struct hci_dev * hdev,void __user * arg)9783d4f9c00SArchie Pusaka static int hci_sock_reject_list_del(struct hci_dev *hdev, void __user *arg)
979f0358568SJohan Hedberg {
980f0358568SJohan Hedberg bdaddr_t bdaddr;
9815e762444SAntti Julku int err;
982f0358568SJohan Hedberg
983f0358568SJohan Hedberg if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
984f0358568SJohan Hedberg return -EFAULT;
985f0358568SJohan Hedberg
98609fd0de5SGustavo F. Padovan hci_dev_lock(hdev);
9875e762444SAntti Julku
9883d4f9c00SArchie Pusaka err = hci_bdaddr_list_del(&hdev->reject_list, &bdaddr, BDADDR_BREDR);
9895e762444SAntti Julku
99009fd0de5SGustavo F. Padovan hci_dev_unlock(hdev);
9915e762444SAntti Julku
9925e762444SAntti Julku return err;
993f0358568SJohan Hedberg }
994f0358568SJohan Hedberg
9951da177e4SLinus Torvalds /* Ioctls that require bound socket */
hci_sock_bound_ioctl(struct sock * sk,unsigned int cmd,unsigned long arg)9966039aa73SGustavo Padovan static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
9976039aa73SGustavo Padovan unsigned long arg)
9981da177e4SLinus Torvalds {
999e0448092STetsuo Handa struct hci_dev *hdev = hci_hdev_from_sock(sk);
10001da177e4SLinus Torvalds
1001e0448092STetsuo Handa if (IS_ERR(hdev))
1002e0448092STetsuo Handa return PTR_ERR(hdev);
10031da177e4SLinus Torvalds
1004d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
10050736cfa8SMarcel Holtmann return -EBUSY;
10060736cfa8SMarcel Holtmann
1007d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
1008fee746b0SMarcel Holtmann return -EOPNOTSUPP;
1009fee746b0SMarcel Holtmann
10101da177e4SLinus Torvalds switch (cmd) {
10111da177e4SLinus Torvalds case HCISETRAW:
10121da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
1013bf5b30b8SZhao Hongjiang return -EPERM;
1014db596681SMarcel Holtmann return -EOPNOTSUPP;
10151da177e4SLinus Torvalds
10161da177e4SLinus Torvalds case HCIGETCONNINFO:
10171da177e4SLinus Torvalds return hci_get_conn_info(hdev, (void __user *)arg);
10181da177e4SLinus Torvalds
101940be492fSMarcel Holtmann case HCIGETAUTHINFO:
102040be492fSMarcel Holtmann return hci_get_auth_info(hdev, (void __user *)arg);
102140be492fSMarcel Holtmann
1022f0358568SJohan Hedberg case HCIBLOCKADDR:
1023f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN))
1024bf5b30b8SZhao Hongjiang return -EPERM;
10253d4f9c00SArchie Pusaka return hci_sock_reject_list_add(hdev, (void __user *)arg);
1026f0358568SJohan Hedberg
1027f0358568SJohan Hedberg case HCIUNBLOCKADDR:
1028f0358568SJohan Hedberg if (!capable(CAP_NET_ADMIN))
1029bf5b30b8SZhao Hongjiang return -EPERM;
10303d4f9c00SArchie Pusaka return hci_sock_reject_list_del(hdev, (void __user *)arg);
10310736cfa8SMarcel Holtmann }
1032f0358568SJohan Hedberg
1033324d36edSMarcel Holtmann return -ENOIOCTLCMD;
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds
hci_sock_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)10368fc9ced3SGustavo Padovan static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
10378fc9ced3SGustavo Padovan unsigned long arg)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds void __user *argp = (void __user *)arg;
10400736cfa8SMarcel Holtmann struct sock *sk = sock->sk;
10411da177e4SLinus Torvalds int err;
10421da177e4SLinus Torvalds
10431da177e4SLinus Torvalds BT_DBG("cmd %x arg %lx", cmd, arg);
10441da177e4SLinus Torvalds
1045000c2fa2SRuihan Li /* Make sure the cmd is valid before doing anything */
1046000c2fa2SRuihan Li switch (cmd) {
1047000c2fa2SRuihan Li case HCIGETDEVLIST:
1048000c2fa2SRuihan Li case HCIGETDEVINFO:
1049000c2fa2SRuihan Li case HCIGETCONNLIST:
1050000c2fa2SRuihan Li case HCIDEVUP:
1051000c2fa2SRuihan Li case HCIDEVDOWN:
1052000c2fa2SRuihan Li case HCIDEVRESET:
1053000c2fa2SRuihan Li case HCIDEVRESTAT:
1054000c2fa2SRuihan Li case HCISETSCAN:
1055000c2fa2SRuihan Li case HCISETAUTH:
1056000c2fa2SRuihan Li case HCISETENCRYPT:
1057000c2fa2SRuihan Li case HCISETPTYPE:
1058000c2fa2SRuihan Li case HCISETLINKPOL:
1059000c2fa2SRuihan Li case HCISETLINKMODE:
1060000c2fa2SRuihan Li case HCISETACLMTU:
1061000c2fa2SRuihan Li case HCISETSCOMTU:
1062000c2fa2SRuihan Li case HCIINQUIRY:
1063000c2fa2SRuihan Li case HCISETRAW:
1064000c2fa2SRuihan Li case HCIGETCONNINFO:
1065000c2fa2SRuihan Li case HCIGETAUTHINFO:
1066000c2fa2SRuihan Li case HCIBLOCKADDR:
1067000c2fa2SRuihan Li case HCIUNBLOCKADDR:
1068000c2fa2SRuihan Li break;
1069000c2fa2SRuihan Li default:
1070000c2fa2SRuihan Li return -ENOIOCTLCMD;
1071000c2fa2SRuihan Li }
1072000c2fa2SRuihan Li
1073c1c4f956SMarcel Holtmann lock_sock(sk);
1074c1c4f956SMarcel Holtmann
1075c1c4f956SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1076c1c4f956SMarcel Holtmann err = -EBADFD;
1077c1c4f956SMarcel Holtmann goto done;
1078c1c4f956SMarcel Holtmann }
1079c1c4f956SMarcel Holtmann
1080f81f5b2dSMarcel Holtmann /* When calling an ioctl on an unbound raw socket, then ensure
1081f81f5b2dSMarcel Holtmann * that the monitor gets informed. Ensure that the resulting event
1082f81f5b2dSMarcel Holtmann * is only send once by checking if the cookie exists or not. The
1083f81f5b2dSMarcel Holtmann * socket cookie will be only ever generated once for the lifetime
1084f81f5b2dSMarcel Holtmann * of a given socket.
1085f81f5b2dSMarcel Holtmann */
1086f81f5b2dSMarcel Holtmann if (hci_sock_gen_cookie(sk)) {
1087f81f5b2dSMarcel Holtmann struct sk_buff *skb;
1088f81f5b2dSMarcel Holtmann
108925c150acSRuihan Li /* Perform careful checks before setting the HCI_SOCK_TRUSTED
109025c150acSRuihan Li * flag. Make sure that not only the current task but also
109125c150acSRuihan Li * the socket opener has the required capability, since
109225c150acSRuihan Li * privileged programs can be tricked into making ioctl calls
109325c150acSRuihan Li * on HCI sockets, and the socket should not be marked as
109425c150acSRuihan Li * trusted simply because the ioctl caller is privileged.
109525c150acSRuihan Li */
109625c150acSRuihan Li if (sk_capable(sk, CAP_NET_ADMIN))
1097f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1098f81f5b2dSMarcel Holtmann
1099f81f5b2dSMarcel Holtmann /* Send event to monitor */
1100f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk);
1101f81f5b2dSMarcel Holtmann if (skb) {
1102f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1103f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1104f81f5b2dSMarcel Holtmann kfree_skb(skb);
1105f81f5b2dSMarcel Holtmann }
1106f81f5b2dSMarcel Holtmann }
1107f81f5b2dSMarcel Holtmann
1108c1c4f956SMarcel Holtmann release_sock(sk);
1109c1c4f956SMarcel Holtmann
11101da177e4SLinus Torvalds switch (cmd) {
11111da177e4SLinus Torvalds case HCIGETDEVLIST:
11121da177e4SLinus Torvalds return hci_get_dev_list(argp);
11131da177e4SLinus Torvalds
11141da177e4SLinus Torvalds case HCIGETDEVINFO:
11151da177e4SLinus Torvalds return hci_get_dev_info(argp);
11161da177e4SLinus Torvalds
11171da177e4SLinus Torvalds case HCIGETCONNLIST:
11181da177e4SLinus Torvalds return hci_get_conn_list(argp);
11191da177e4SLinus Torvalds
11201da177e4SLinus Torvalds case HCIDEVUP:
11211da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
1122bf5b30b8SZhao Hongjiang return -EPERM;
11231da177e4SLinus Torvalds return hci_dev_open(arg);
11241da177e4SLinus Torvalds
11251da177e4SLinus Torvalds case HCIDEVDOWN:
11261da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
1127bf5b30b8SZhao Hongjiang return -EPERM;
11281da177e4SLinus Torvalds return hci_dev_close(arg);
11291da177e4SLinus Torvalds
11301da177e4SLinus Torvalds case HCIDEVRESET:
11311da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
1132bf5b30b8SZhao Hongjiang return -EPERM;
11331da177e4SLinus Torvalds return hci_dev_reset(arg);
11341da177e4SLinus Torvalds
11351da177e4SLinus Torvalds case HCIDEVRESTAT:
11361da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
1137bf5b30b8SZhao Hongjiang return -EPERM;
11381da177e4SLinus Torvalds return hci_dev_reset_stat(arg);
11391da177e4SLinus Torvalds
11401da177e4SLinus Torvalds case HCISETSCAN:
11411da177e4SLinus Torvalds case HCISETAUTH:
11421da177e4SLinus Torvalds case HCISETENCRYPT:
11431da177e4SLinus Torvalds case HCISETPTYPE:
11441da177e4SLinus Torvalds case HCISETLINKPOL:
11451da177e4SLinus Torvalds case HCISETLINKMODE:
11461da177e4SLinus Torvalds case HCISETACLMTU:
11471da177e4SLinus Torvalds case HCISETSCOMTU:
11481da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
1149bf5b30b8SZhao Hongjiang return -EPERM;
11501da177e4SLinus Torvalds return hci_dev_cmd(cmd, argp);
11511da177e4SLinus Torvalds
11521da177e4SLinus Torvalds case HCIINQUIRY:
11531da177e4SLinus Torvalds return hci_inquiry(argp);
1154c1c4f956SMarcel Holtmann }
11551da177e4SLinus Torvalds
11561da177e4SLinus Torvalds lock_sock(sk);
1157c1c4f956SMarcel Holtmann
11581da177e4SLinus Torvalds err = hci_sock_bound_ioctl(sk, cmd, arg);
1159c1c4f956SMarcel Holtmann
1160c1c4f956SMarcel Holtmann done:
11611da177e4SLinus Torvalds release_sock(sk);
11621da177e4SLinus Torvalds return err;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds
11657a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
hci_sock_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)11667a6038b3SArnd Bergmann static int hci_sock_compat_ioctl(struct socket *sock, unsigned int cmd,
11677a6038b3SArnd Bergmann unsigned long arg)
11687a6038b3SArnd Bergmann {
11697a6038b3SArnd Bergmann switch (cmd) {
11707a6038b3SArnd Bergmann case HCIDEVUP:
11717a6038b3SArnd Bergmann case HCIDEVDOWN:
11727a6038b3SArnd Bergmann case HCIDEVRESET:
11737a6038b3SArnd Bergmann case HCIDEVRESTAT:
11747a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, arg);
11757a6038b3SArnd Bergmann }
11767a6038b3SArnd Bergmann
11777a6038b3SArnd Bergmann return hci_sock_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
11787a6038b3SArnd Bergmann }
11797a6038b3SArnd Bergmann #endif
11807a6038b3SArnd Bergmann
hci_sock_bind(struct socket * sock,struct sockaddr * addr,int addr_len)11818fc9ced3SGustavo Padovan static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
11828fc9ced3SGustavo Padovan int addr_len)
11831da177e4SLinus Torvalds {
11840381101fSJohan Hedberg struct sockaddr_hci haddr;
11851da177e4SLinus Torvalds struct sock *sk = sock->sk;
11861da177e4SLinus Torvalds struct hci_dev *hdev = NULL;
1187f4cdbb3fSMarcel Holtmann struct sk_buff *skb;
11880381101fSJohan Hedberg int len, err = 0;
11891da177e4SLinus Torvalds
11901da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk);
11911da177e4SLinus Torvalds
11920381101fSJohan Hedberg if (!addr)
11930381101fSJohan Hedberg return -EINVAL;
11940381101fSJohan Hedberg
11950381101fSJohan Hedberg memset(&haddr, 0, sizeof(haddr));
11960381101fSJohan Hedberg len = min_t(unsigned int, sizeof(haddr), addr_len);
11970381101fSJohan Hedberg memcpy(&haddr, addr, len);
11980381101fSJohan Hedberg
11990381101fSJohan Hedberg if (haddr.hci_family != AF_BLUETOOTH)
12000381101fSJohan Hedberg return -EINVAL;
12010381101fSJohan Hedberg
12021da177e4SLinus Torvalds lock_sock(sk);
12031da177e4SLinus Torvalds
1204e0448092STetsuo Handa /* Allow detaching from dead device and attaching to alive device, if
1205e0448092STetsuo Handa * the caller wants to re-bind (instead of close) this socket in
1206e0448092STetsuo Handa * response to hci_sock_dev_event(HCI_DEV_UNREG) notification.
1207e0448092STetsuo Handa */
1208e0448092STetsuo Handa hdev = hci_pi(sk)->hdev;
1209e0448092STetsuo Handa if (hdev && hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1210e0448092STetsuo Handa hci_pi(sk)->hdev = NULL;
1211e0448092STetsuo Handa sk->sk_state = BT_OPEN;
1212e0448092STetsuo Handa hci_dev_put(hdev);
1213e0448092STetsuo Handa }
1214e0448092STetsuo Handa hdev = NULL;
1215e0448092STetsuo Handa
12167cc2ade2SMarcel Holtmann if (sk->sk_state == BT_BOUND) {
12177cc2ade2SMarcel Holtmann err = -EALREADY;
12187cc2ade2SMarcel Holtmann goto done;
12197cc2ade2SMarcel Holtmann }
12207cc2ade2SMarcel Holtmann
12217cc2ade2SMarcel Holtmann switch (haddr.hci_channel) {
12227cc2ade2SMarcel Holtmann case HCI_CHANNEL_RAW:
12237cc2ade2SMarcel Holtmann if (hci_pi(sk)->hdev) {
12241da177e4SLinus Torvalds err = -EALREADY;
12251da177e4SLinus Torvalds goto done;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds
12280381101fSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) {
12290381101fSJohan Hedberg hdev = hci_dev_get(haddr.hci_dev);
123070f23020SAndrei Emeltchenko if (!hdev) {
12311da177e4SLinus Torvalds err = -ENODEV;
12321da177e4SLinus Torvalds goto done;
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds
12351da177e4SLinus Torvalds atomic_inc(&hdev->promisc);
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds
12385a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel;
1239f81f5b2dSMarcel Holtmann
1240f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) {
1241f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been assigned,
1242f4cdbb3fSMarcel Holtmann * then there has been already an ioctl issued against
124391641b79SZheng Yongjun * an unbound socket and with that triggered an open
1244f4cdbb3fSMarcel Holtmann * notification. Send a close notification first to
1245f4cdbb3fSMarcel Holtmann * allow the state transition to bounded.
1246f81f5b2dSMarcel Holtmann */
1247f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk);
1248f4cdbb3fSMarcel Holtmann if (skb) {
1249f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1250f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1251f4cdbb3fSMarcel Holtmann kfree_skb(skb);
1252f4cdbb3fSMarcel Holtmann }
1253f4cdbb3fSMarcel Holtmann }
1254f81f5b2dSMarcel Holtmann
1255f81f5b2dSMarcel Holtmann if (capable(CAP_NET_ADMIN))
1256f81f5b2dSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1257f81f5b2dSMarcel Holtmann
1258f4cdbb3fSMarcel Holtmann hci_pi(sk)->hdev = hdev;
1259f4cdbb3fSMarcel Holtmann
1260f81f5b2dSMarcel Holtmann /* Send event to monitor */
1261f81f5b2dSMarcel Holtmann skb = create_monitor_ctrl_open(sk);
1262f81f5b2dSMarcel Holtmann if (skb) {
1263f81f5b2dSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1264f81f5b2dSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1265f81f5b2dSMarcel Holtmann kfree_skb(skb);
1266f81f5b2dSMarcel Holtmann }
12677cc2ade2SMarcel Holtmann break;
12687cc2ade2SMarcel Holtmann
126923500189SMarcel Holtmann case HCI_CHANNEL_USER:
127023500189SMarcel Holtmann if (hci_pi(sk)->hdev) {
127123500189SMarcel Holtmann err = -EALREADY;
127223500189SMarcel Holtmann goto done;
127323500189SMarcel Holtmann }
127423500189SMarcel Holtmann
127523500189SMarcel Holtmann if (haddr.hci_dev == HCI_DEV_NONE) {
127623500189SMarcel Holtmann err = -EINVAL;
127723500189SMarcel Holtmann goto done;
127823500189SMarcel Holtmann }
127923500189SMarcel Holtmann
128010a8b86fSMarcel Holtmann if (!capable(CAP_NET_ADMIN)) {
128123500189SMarcel Holtmann err = -EPERM;
128223500189SMarcel Holtmann goto done;
128323500189SMarcel Holtmann }
128423500189SMarcel Holtmann
128523500189SMarcel Holtmann hdev = hci_dev_get(haddr.hci_dev);
128623500189SMarcel Holtmann if (!hdev) {
128723500189SMarcel Holtmann err = -ENODEV;
128823500189SMarcel Holtmann goto done;
128923500189SMarcel Holtmann }
129023500189SMarcel Holtmann
1291781f899fSMarcel Holtmann if (test_bit(HCI_INIT, &hdev->flags) ||
1292d7a5a11dSMarcel Holtmann hci_dev_test_flag(hdev, HCI_SETUP) ||
1293781f899fSMarcel Holtmann hci_dev_test_flag(hdev, HCI_CONFIG) ||
1294781f899fSMarcel Holtmann (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1295781f899fSMarcel Holtmann test_bit(HCI_UP, &hdev->flags))) {
129623500189SMarcel Holtmann err = -EBUSY;
129723500189SMarcel Holtmann hci_dev_put(hdev);
129823500189SMarcel Holtmann goto done;
129923500189SMarcel Holtmann }
130023500189SMarcel Holtmann
1301238be788SMarcel Holtmann if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
130223500189SMarcel Holtmann err = -EUSERS;
130323500189SMarcel Holtmann hci_dev_put(hdev);
130423500189SMarcel Holtmann goto done;
130523500189SMarcel Holtmann }
130623500189SMarcel Holtmann
130723500189SMarcel Holtmann mgmt_index_removed(hdev);
130823500189SMarcel Holtmann
130923500189SMarcel Holtmann err = hci_dev_open(hdev->id);
131023500189SMarcel Holtmann if (err) {
1311781f899fSMarcel Holtmann if (err == -EALREADY) {
1312781f899fSMarcel Holtmann /* In case the transport is already up and
1313781f899fSMarcel Holtmann * running, clear the error here.
1314781f899fSMarcel Holtmann *
13159332ef9dSMasahiro Yamada * This can happen when opening a user
1316781f899fSMarcel Holtmann * channel and HCI_AUTO_OFF grace period
1317781f899fSMarcel Holtmann * is still active.
1318781f899fSMarcel Holtmann */
1319781f899fSMarcel Holtmann err = 0;
1320781f899fSMarcel Holtmann } else {
1321a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
1322c6521401SMarcel Holtmann mgmt_index_added(hdev);
132323500189SMarcel Holtmann hci_dev_put(hdev);
132423500189SMarcel Holtmann goto done;
132523500189SMarcel Holtmann }
1326781f899fSMarcel Holtmann }
132723500189SMarcel Holtmann
13285a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel;
1329aa1638ddSMarcel Holtmann
1330aa1638ddSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) {
1331aa1638ddSMarcel Holtmann /* In the case when a cookie has already been assigned,
1332aa1638ddSMarcel Holtmann * this socket will transition from a raw socket into
13339332ef9dSMasahiro Yamada * a user channel socket. For a clean transition, send
1334aa1638ddSMarcel Holtmann * the close notification first.
1335aa1638ddSMarcel Holtmann */
1336aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_close(sk);
1337aa1638ddSMarcel Holtmann if (skb) {
1338aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1339aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1340aa1638ddSMarcel Holtmann kfree_skb(skb);
1341aa1638ddSMarcel Holtmann }
1342aa1638ddSMarcel Holtmann }
1343aa1638ddSMarcel Holtmann
1344aa1638ddSMarcel Holtmann /* The user channel is restricted to CAP_NET_ADMIN
1345aa1638ddSMarcel Holtmann * capabilities and with that implicitly trusted.
1346aa1638ddSMarcel Holtmann */
1347aa1638ddSMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
1348aa1638ddSMarcel Holtmann
134923500189SMarcel Holtmann hci_pi(sk)->hdev = hdev;
13505a6d2cf5SMarcel Holtmann
1351aa1638ddSMarcel Holtmann /* Send event to monitor */
1352aa1638ddSMarcel Holtmann skb = create_monitor_ctrl_open(sk);
1353aa1638ddSMarcel Holtmann if (skb) {
1354aa1638ddSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1355aa1638ddSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1356aa1638ddSMarcel Holtmann kfree_skb(skb);
1357aa1638ddSMarcel Holtmann }
1358aa1638ddSMarcel Holtmann
13595a6d2cf5SMarcel Holtmann atomic_inc(&hdev->promisc);
136023500189SMarcel Holtmann break;
136123500189SMarcel Holtmann
1362cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR:
1363cd82e61cSMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) {
1364cd82e61cSMarcel Holtmann err = -EINVAL;
1365cd82e61cSMarcel Holtmann goto done;
1366cd82e61cSMarcel Holtmann }
1367cd82e61cSMarcel Holtmann
1368cd82e61cSMarcel Holtmann if (!capable(CAP_NET_RAW)) {
1369cd82e61cSMarcel Holtmann err = -EPERM;
1370cd82e61cSMarcel Holtmann goto done;
1371cd82e61cSMarcel Holtmann }
1372cd82e61cSMarcel Holtmann
13735a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel;
13745a6d2cf5SMarcel Holtmann
137550ebc055SMarcel Holtmann /* The monitor interface is restricted to CAP_NET_RAW
137650ebc055SMarcel Holtmann * capabilities and with that implicitly trusted.
137750ebc055SMarcel Holtmann */
137850ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
137950ebc055SMarcel Holtmann
1380787b306cSJohannes Berg send_monitor_note(sk, "Linux version %s (%s)",
1381787b306cSJohannes Berg init_utsname()->release,
1382787b306cSJohannes Berg init_utsname()->machine);
13839e8305b3SMarcel Holtmann send_monitor_note(sk, "Bluetooth subsystem version %u.%u",
13849e8305b3SMarcel Holtmann BT_SUBSYS_VERSION, BT_SUBSYS_REVISION);
1385cd82e61cSMarcel Holtmann send_monitor_replay(sk);
1386249fa169SMarcel Holtmann send_monitor_control_replay(sk);
1387cd82e61cSMarcel Holtmann
1388cd82e61cSMarcel Holtmann atomic_inc(&monitor_promisc);
1389cd82e61cSMarcel Holtmann break;
1390cd82e61cSMarcel Holtmann
1391ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING:
1392ac714949SMarcel Holtmann if (haddr.hci_dev != HCI_DEV_NONE) {
1393ac714949SMarcel Holtmann err = -EINVAL;
1394ac714949SMarcel Holtmann goto done;
1395ac714949SMarcel Holtmann }
1396ac714949SMarcel Holtmann
1397ac714949SMarcel Holtmann if (!capable(CAP_NET_ADMIN)) {
1398ac714949SMarcel Holtmann err = -EPERM;
1399ac714949SMarcel Holtmann goto done;
1400ac714949SMarcel Holtmann }
14015a6d2cf5SMarcel Holtmann
14025a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel;
1403ac714949SMarcel Holtmann break;
1404ac714949SMarcel Holtmann
14057cc2ade2SMarcel Holtmann default:
1406801c1e8dSJohan Hedberg if (!hci_mgmt_chan_find(haddr.hci_channel)) {
14077cc2ade2SMarcel Holtmann err = -EINVAL;
14087cc2ade2SMarcel Holtmann goto done;
14097cc2ade2SMarcel Holtmann }
14107cc2ade2SMarcel Holtmann
1411801c1e8dSJohan Hedberg if (haddr.hci_dev != HCI_DEV_NONE) {
1412801c1e8dSJohan Hedberg err = -EINVAL;
1413801c1e8dSJohan Hedberg goto done;
1414801c1e8dSJohan Hedberg }
1415801c1e8dSJohan Hedberg
14161195fbb8SMarcel Holtmann /* Users with CAP_NET_ADMIN capabilities are allowed
14171195fbb8SMarcel Holtmann * access to all management commands and events. For
14181195fbb8SMarcel Holtmann * untrusted users the interface is restricted and
14191195fbb8SMarcel Holtmann * also only untrusted events are sent.
142050ebc055SMarcel Holtmann */
14211195fbb8SMarcel Holtmann if (capable(CAP_NET_ADMIN))
142250ebc055SMarcel Holtmann hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
142350ebc055SMarcel Holtmann
14245a6d2cf5SMarcel Holtmann hci_pi(sk)->channel = haddr.hci_channel;
14255a6d2cf5SMarcel Holtmann
1426f9207338SMarcel Holtmann /* At the moment the index and unconfigured index events
1427f9207338SMarcel Holtmann * are enabled unconditionally. Setting them on each
1428f9207338SMarcel Holtmann * socket when binding keeps this functionality. They
1429f9207338SMarcel Holtmann * however might be cleared later and then sending of these
1430f9207338SMarcel Holtmann * events will be disabled, but that is then intentional.
1431f6b7712eSMarcel Holtmann *
1432f6b7712eSMarcel Holtmann * This also enables generic events that are safe to be
1433f6b7712eSMarcel Holtmann * received by untrusted users. Example for such events
1434f6b7712eSMarcel Holtmann * are changes to settings, class of device, name etc.
1435f9207338SMarcel Holtmann */
14365a6d2cf5SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_CONTROL) {
1437f4cdbb3fSMarcel Holtmann if (!hci_sock_gen_cookie(sk)) {
1438f4cdbb3fSMarcel Holtmann /* In the case when a cookie has already been
143991641b79SZheng Yongjun * assigned, this socket will transition from
1440f4cdbb3fSMarcel Holtmann * a raw socket into a control socket. To
144191641b79SZheng Yongjun * allow for a clean transition, send the
1442f4cdbb3fSMarcel Holtmann * close notification first.
1443f4cdbb3fSMarcel Holtmann */
1444f4cdbb3fSMarcel Holtmann skb = create_monitor_ctrl_close(sk);
1445f4cdbb3fSMarcel Holtmann if (skb) {
1446f4cdbb3fSMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1447f4cdbb3fSMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1448f4cdbb3fSMarcel Holtmann kfree_skb(skb);
1449f4cdbb3fSMarcel Holtmann }
1450f4cdbb3fSMarcel Holtmann }
145170ecce91SMarcel Holtmann
1452249fa169SMarcel Holtmann /* Send event to monitor */
1453249fa169SMarcel Holtmann skb = create_monitor_ctrl_open(sk);
1454249fa169SMarcel Holtmann if (skb) {
1455249fa169SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
1456249fa169SMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
1457249fa169SMarcel Holtmann kfree_skb(skb);
1458249fa169SMarcel Holtmann }
1459249fa169SMarcel Holtmann
1460f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
1461f9207338SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
14625504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_OPTION_EVENTS);
14635504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_SETTING_EVENTS);
14645504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
14655504c3a3SMarcel Holtmann hci_sock_set_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1466f9207338SMarcel Holtmann }
1467801c1e8dSJohan Hedberg break;
1468801c1e8dSJohan Hedberg }
1469801c1e8dSJohan Hedberg
147009572fcaSLuiz Augusto von Dentz /* Default MTU to HCI_MAX_FRAME_SIZE if not set */
147109572fcaSLuiz Augusto von Dentz if (!hci_pi(sk)->mtu)
147209572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = HCI_MAX_FRAME_SIZE;
147309572fcaSLuiz Augusto von Dentz
14741da177e4SLinus Torvalds sk->sk_state = BT_BOUND;
14751da177e4SLinus Torvalds
14761da177e4SLinus Torvalds done:
14771da177e4SLinus Torvalds release_sock(sk);
14781da177e4SLinus Torvalds return err;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds
hci_sock_getname(struct socket * sock,struct sockaddr * addr,int peer)14818fc9ced3SGustavo Padovan static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
14829b2c45d4SDenys Vlasenko int peer)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds struct sockaddr_hci *haddr = (struct sockaddr_hci *)addr;
14851da177e4SLinus Torvalds struct sock *sk = sock->sk;
14869d4b68b2SMarcel Holtmann struct hci_dev *hdev;
14879d4b68b2SMarcel Holtmann int err = 0;
14881da177e4SLinus Torvalds
14891da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk);
14901da177e4SLinus Torvalds
149106f43cbcSMarcel Holtmann if (peer)
149206f43cbcSMarcel Holtmann return -EOPNOTSUPP;
149306f43cbcSMarcel Holtmann
14941da177e4SLinus Torvalds lock_sock(sk);
14951da177e4SLinus Torvalds
1496e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk);
1497e0448092STetsuo Handa if (IS_ERR(hdev)) {
1498e0448092STetsuo Handa err = PTR_ERR(hdev);
14999d4b68b2SMarcel Holtmann goto done;
15009d4b68b2SMarcel Holtmann }
15019d4b68b2SMarcel Holtmann
15021da177e4SLinus Torvalds haddr->hci_family = AF_BLUETOOTH;
15037b005bd3SMarcel Holtmann haddr->hci_dev = hdev->id;
15049d4b68b2SMarcel Holtmann haddr->hci_channel= hci_pi(sk)->channel;
15059b2c45d4SDenys Vlasenko err = sizeof(*haddr);
15061da177e4SLinus Torvalds
15079d4b68b2SMarcel Holtmann done:
15081da177e4SLinus Torvalds release_sock(sk);
15099d4b68b2SMarcel Holtmann return err;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds
hci_sock_cmsg(struct sock * sk,struct msghdr * msg,struct sk_buff * skb)15126039aa73SGustavo Padovan static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
15136039aa73SGustavo Padovan struct sk_buff *skb)
15141da177e4SLinus Torvalds {
151532929e1fSAlain Michaud __u8 mask = hci_pi(sk)->cmsg_mask;
15161da177e4SLinus Torvalds
15170d48d939SMarcel Holtmann if (mask & HCI_CMSG_DIR) {
15180d48d939SMarcel Holtmann int incoming = bt_cb(skb)->incoming;
15198fc9ced3SGustavo Padovan put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
15208fc9ced3SGustavo Padovan &incoming);
15210d48d939SMarcel Holtmann }
15221da177e4SLinus Torvalds
1523a61bbcf2SPatrick McHardy if (mask & HCI_CMSG_TSTAMP) {
1524f6e623a6SJohann Felix Soden #ifdef CONFIG_COMPAT
152513c6ee2aSDeepa Dinamani struct old_timeval32 ctv;
1526f6e623a6SJohann Felix Soden #endif
152713c6ee2aSDeepa Dinamani struct __kernel_old_timeval tv;
1528767c5eb5SMarcel Holtmann void *data;
1529767c5eb5SMarcel Holtmann int len;
1530a61bbcf2SPatrick McHardy
1531a61bbcf2SPatrick McHardy skb_get_timestamp(skb, &tv);
1532767c5eb5SMarcel Holtmann
15331da97f83SDavid S. Miller data = &tv;
15341da97f83SDavid S. Miller len = sizeof(tv);
15351da97f83SDavid S. Miller #ifdef CONFIG_COMPAT
1536da88cea1SH. J. Lu if (!COMPAT_USE_64BIT_TIME &&
1537da88cea1SH. J. Lu (msg->msg_flags & MSG_CMSG_COMPAT)) {
1538767c5eb5SMarcel Holtmann ctv.tv_sec = tv.tv_sec;
1539767c5eb5SMarcel Holtmann ctv.tv_usec = tv.tv_usec;
1540767c5eb5SMarcel Holtmann data = &ctv;
1541767c5eb5SMarcel Holtmann len = sizeof(ctv);
1542767c5eb5SMarcel Holtmann }
15431da97f83SDavid S. Miller #endif
1544767c5eb5SMarcel Holtmann
1545767c5eb5SMarcel Holtmann put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, len, data);
1546a61bbcf2SPatrick McHardy }
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds
hci_sock_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)15498528d3f7SMarcel Holtmann static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg,
15508528d3f7SMarcel Holtmann size_t len, int flags)
15511da177e4SLinus Torvalds {
155269ae5065SLuiz Augusto von Dentz struct scm_cookie scm;
15531da177e4SLinus Torvalds struct sock *sk = sock->sk;
15541da177e4SLinus Torvalds struct sk_buff *skb;
15551da177e4SLinus Torvalds int copied, err;
155683871f8cSDenis Kenzior unsigned int skblen;
15571da177e4SLinus Torvalds
15581da177e4SLinus Torvalds BT_DBG("sock %p, sk %p", sock, sk);
15591da177e4SLinus Torvalds
1560d94a6104SMarcel Holtmann if (flags & MSG_OOB)
15611da177e4SLinus Torvalds return -EOPNOTSUPP;
15621da177e4SLinus Torvalds
1563ac714949SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_LOGGING)
1564ac714949SMarcel Holtmann return -EOPNOTSUPP;
1565ac714949SMarcel Holtmann
15661da177e4SLinus Torvalds if (sk->sk_state == BT_CLOSED)
15671da177e4SLinus Torvalds return 0;
15681da177e4SLinus Torvalds
1569f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &err);
157070f23020SAndrei Emeltchenko if (!skb)
15711da177e4SLinus Torvalds return err;
15721da177e4SLinus Torvalds
157383871f8cSDenis Kenzior skblen = skb->len;
15741da177e4SLinus Torvalds copied = skb->len;
15751da177e4SLinus Torvalds if (len < copied) {
15761da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC;
15771da177e4SLinus Torvalds copied = len;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds
1580badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb);
158151f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, copied);
15821da177e4SLinus Torvalds
15833a208627SMarcel Holtmann switch (hci_pi(sk)->channel) {
15843a208627SMarcel Holtmann case HCI_CHANNEL_RAW:
15851da177e4SLinus Torvalds hci_sock_cmsg(sk, msg, skb);
15863a208627SMarcel Holtmann break;
158723500189SMarcel Holtmann case HCI_CHANNEL_USER:
1588cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR:
1589cd82e61cSMarcel Holtmann sock_recv_timestamp(msg, sk, skb);
1590cd82e61cSMarcel Holtmann break;
1591801c1e8dSJohan Hedberg default:
1592801c1e8dSJohan Hedberg if (hci_mgmt_chan_find(hci_pi(sk)->channel))
1593801c1e8dSJohan Hedberg sock_recv_timestamp(msg, sk, skb);
1594801c1e8dSJohan Hedberg break;
15953a208627SMarcel Holtmann }
15961da177e4SLinus Torvalds
159769ae5065SLuiz Augusto von Dentz memset(&scm, 0, sizeof(scm));
159869ae5065SLuiz Augusto von Dentz scm.creds = bt_cb(skb)->creds;
159969ae5065SLuiz Augusto von Dentz
16001da177e4SLinus Torvalds skb_free_datagram(sk, skb);
16011da177e4SLinus Torvalds
16024f34228bSLuiz Augusto von Dentz if (flags & MSG_TRUNC)
160383871f8cSDenis Kenzior copied = skblen;
160483871f8cSDenis Kenzior
160569ae5065SLuiz Augusto von Dentz scm_recv(sock, msg, &scm, flags);
160669ae5065SLuiz Augusto von Dentz
16071da177e4SLinus Torvalds return err ? : copied;
16081da177e4SLinus Torvalds }
16091da177e4SLinus Torvalds
hci_mgmt_cmd(struct hci_mgmt_chan * chan,struct sock * sk,struct sk_buff * skb)161064ba2eb3SLuiz Augusto von Dentz static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
161164ba2eb3SLuiz Augusto von Dentz struct sk_buff *skb)
1612fa4335d7SJohan Hedberg {
1613fa4335d7SJohan Hedberg u8 *cp;
1614fa4335d7SJohan Hedberg struct mgmt_hdr *hdr;
1615fa4335d7SJohan Hedberg u16 opcode, index, len;
1616fa4335d7SJohan Hedberg struct hci_dev *hdev = NULL;
1617fa4335d7SJohan Hedberg const struct hci_mgmt_handler *handler;
1618fa4335d7SJohan Hedberg bool var_len, no_hdev;
1619fa4335d7SJohan Hedberg int err;
1620fa4335d7SJohan Hedberg
162164ba2eb3SLuiz Augusto von Dentz BT_DBG("got %d bytes", skb->len);
1622fa4335d7SJohan Hedberg
162364ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr))
1624fa4335d7SJohan Hedberg return -EINVAL;
1625fa4335d7SJohan Hedberg
162664ba2eb3SLuiz Augusto von Dentz hdr = (void *)skb->data;
1627fa4335d7SJohan Hedberg opcode = __le16_to_cpu(hdr->opcode);
1628fa4335d7SJohan Hedberg index = __le16_to_cpu(hdr->index);
1629fa4335d7SJohan Hedberg len = __le16_to_cpu(hdr->len);
1630fa4335d7SJohan Hedberg
163164ba2eb3SLuiz Augusto von Dentz if (len != skb->len - sizeof(*hdr)) {
1632fa4335d7SJohan Hedberg err = -EINVAL;
1633fa4335d7SJohan Hedberg goto done;
1634fa4335d7SJohan Hedberg }
1635fa4335d7SJohan Hedberg
163638ceaa00SMarcel Holtmann if (chan->channel == HCI_CHANNEL_CONTROL) {
163764ba2eb3SLuiz Augusto von Dentz struct sk_buff *cmd;
163838ceaa00SMarcel Holtmann
163938ceaa00SMarcel Holtmann /* Send event to monitor */
164064ba2eb3SLuiz Augusto von Dentz cmd = create_monitor_ctrl_command(sk, index, opcode, len,
164164ba2eb3SLuiz Augusto von Dentz skb->data + sizeof(*hdr));
164264ba2eb3SLuiz Augusto von Dentz if (cmd) {
164364ba2eb3SLuiz Augusto von Dentz hci_send_to_channel(HCI_CHANNEL_MONITOR, cmd,
164438ceaa00SMarcel Holtmann HCI_SOCK_TRUSTED, NULL);
164564ba2eb3SLuiz Augusto von Dentz kfree_skb(cmd);
164638ceaa00SMarcel Holtmann }
164738ceaa00SMarcel Holtmann }
164838ceaa00SMarcel Holtmann
1649fa4335d7SJohan Hedberg if (opcode >= chan->handler_count ||
1650fa4335d7SJohan Hedberg chan->handlers[opcode].func == NULL) {
1651fa4335d7SJohan Hedberg BT_DBG("Unknown op %u", opcode);
1652fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1653fa4335d7SJohan Hedberg MGMT_STATUS_UNKNOWN_COMMAND);
1654fa4335d7SJohan Hedberg goto done;
1655fa4335d7SJohan Hedberg }
1656fa4335d7SJohan Hedberg
1657fa4335d7SJohan Hedberg handler = &chan->handlers[opcode];
1658fa4335d7SJohan Hedberg
1659fa4335d7SJohan Hedberg if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1660fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1661fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1662fa4335d7SJohan Hedberg MGMT_STATUS_PERMISSION_DENIED);
1663fa4335d7SJohan Hedberg goto done;
1664fa4335d7SJohan Hedberg }
1665fa4335d7SJohan Hedberg
1666fa4335d7SJohan Hedberg if (index != MGMT_INDEX_NONE) {
1667fa4335d7SJohan Hedberg hdev = hci_dev_get(index);
1668fa4335d7SJohan Hedberg if (!hdev) {
1669fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1670fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX);
1671fa4335d7SJohan Hedberg goto done;
1672fa4335d7SJohan Hedberg }
1673fa4335d7SJohan Hedberg
1674fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1675fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_CONFIG) ||
1676fa4335d7SJohan Hedberg hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1677fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1678fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX);
1679fa4335d7SJohan Hedberg goto done;
1680fa4335d7SJohan Hedberg }
1681fa4335d7SJohan Hedberg
1682fa4335d7SJohan Hedberg if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1683fa4335d7SJohan Hedberg !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1684fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1685fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX);
1686fa4335d7SJohan Hedberg goto done;
1687fa4335d7SJohan Hedberg }
1688fa4335d7SJohan Hedberg }
1689fa4335d7SJohan Hedberg
1690d5cc6626SMarcel Holtmann if (!(handler->flags & HCI_MGMT_HDEV_OPTIONAL)) {
1691fa4335d7SJohan Hedberg no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1692fa4335d7SJohan Hedberg if (no_hdev != !hdev) {
1693fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1694fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_INDEX);
1695fa4335d7SJohan Hedberg goto done;
1696fa4335d7SJohan Hedberg }
1697d5cc6626SMarcel Holtmann }
1698fa4335d7SJohan Hedberg
1699fa4335d7SJohan Hedberg var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1700fa4335d7SJohan Hedberg if ((var_len && len < handler->data_len) ||
1701fa4335d7SJohan Hedberg (!var_len && len != handler->data_len)) {
1702fa4335d7SJohan Hedberg err = mgmt_cmd_status(sk, index, opcode,
1703fa4335d7SJohan Hedberg MGMT_STATUS_INVALID_PARAMS);
1704fa4335d7SJohan Hedberg goto done;
1705fa4335d7SJohan Hedberg }
1706fa4335d7SJohan Hedberg
1707fa4335d7SJohan Hedberg if (hdev && chan->hdev_init)
1708fa4335d7SJohan Hedberg chan->hdev_init(sk, hdev);
1709fa4335d7SJohan Hedberg
171064ba2eb3SLuiz Augusto von Dentz cp = skb->data + sizeof(*hdr);
1711fa4335d7SJohan Hedberg
1712fa4335d7SJohan Hedberg err = handler->func(sk, hdev, cp, len);
1713fa4335d7SJohan Hedberg if (err < 0)
1714fa4335d7SJohan Hedberg goto done;
1715fa4335d7SJohan Hedberg
171664ba2eb3SLuiz Augusto von Dentz err = skb->len;
1717fa4335d7SJohan Hedberg
1718fa4335d7SJohan Hedberg done:
1719fa4335d7SJohan Hedberg if (hdev)
1720fa4335d7SJohan Hedberg hci_dev_put(hdev);
1721fa4335d7SJohan Hedberg
1722fa4335d7SJohan Hedberg return err;
1723fa4335d7SJohan Hedberg }
1724fa4335d7SJohan Hedberg
hci_logging_frame(struct sock * sk,struct sk_buff * skb,unsigned int flags)172564ba2eb3SLuiz Augusto von Dentz static int hci_logging_frame(struct sock *sk, struct sk_buff *skb,
172664ba2eb3SLuiz Augusto von Dentz unsigned int flags)
1727ac714949SMarcel Holtmann {
1728ac714949SMarcel Holtmann struct hci_mon_hdr *hdr;
1729ac714949SMarcel Holtmann struct hci_dev *hdev;
1730ac714949SMarcel Holtmann u16 index;
1731ac714949SMarcel Holtmann int err;
1732ac714949SMarcel Holtmann
1733ac714949SMarcel Holtmann /* The logging frame consists at minimum of the standard header,
1734ac714949SMarcel Holtmann * the priority byte, the ident length byte and at least one string
1735ac714949SMarcel Holtmann * terminator NUL byte. Anything shorter are invalid packets.
1736ac714949SMarcel Holtmann */
173764ba2eb3SLuiz Augusto von Dentz if (skb->len < sizeof(*hdr) + 3)
1738ac714949SMarcel Holtmann return -EINVAL;
1739ac714949SMarcel Holtmann
1740ac714949SMarcel Holtmann hdr = (void *)skb->data;
1741ac714949SMarcel Holtmann
174264ba2eb3SLuiz Augusto von Dentz if (__le16_to_cpu(hdr->len) != skb->len - sizeof(*hdr))
174364ba2eb3SLuiz Augusto von Dentz return -EINVAL;
1744ac714949SMarcel Holtmann
1745ac714949SMarcel Holtmann if (__le16_to_cpu(hdr->opcode) == 0x0000) {
1746ac714949SMarcel Holtmann __u8 priority = skb->data[sizeof(*hdr)];
1747ac714949SMarcel Holtmann __u8 ident_len = skb->data[sizeof(*hdr) + 1];
1748ac714949SMarcel Holtmann
1749ac714949SMarcel Holtmann /* Only the priorities 0-7 are valid and with that any other
1750ac714949SMarcel Holtmann * value results in an invalid packet.
1751ac714949SMarcel Holtmann *
1752ac714949SMarcel Holtmann * The priority byte is followed by an ident length byte and
1753ac714949SMarcel Holtmann * the NUL terminated ident string. Check that the ident
1754ac714949SMarcel Holtmann * length is not overflowing the packet and also that the
1755ac714949SMarcel Holtmann * ident string itself is NUL terminated. In case the ident
1756ac714949SMarcel Holtmann * length is zero, the length value actually doubles as NUL
1757ac714949SMarcel Holtmann * terminator identifier.
1758ac714949SMarcel Holtmann *
1759ac714949SMarcel Holtmann * The message follows the ident string (if present) and
1760ac714949SMarcel Holtmann * must be NUL terminated. Otherwise it is not a valid packet.
1761ac714949SMarcel Holtmann */
176264ba2eb3SLuiz Augusto von Dentz if (priority > 7 || skb->data[skb->len - 1] != 0x00 ||
176364ba2eb3SLuiz Augusto von Dentz ident_len > skb->len - sizeof(*hdr) - 3 ||
176464ba2eb3SLuiz Augusto von Dentz skb->data[sizeof(*hdr) + ident_len + 1] != 0x00)
176564ba2eb3SLuiz Augusto von Dentz return -EINVAL;
1766ac714949SMarcel Holtmann } else {
176764ba2eb3SLuiz Augusto von Dentz return -EINVAL;
1768ac714949SMarcel Holtmann }
1769ac714949SMarcel Holtmann
1770ac714949SMarcel Holtmann index = __le16_to_cpu(hdr->index);
1771ac714949SMarcel Holtmann
1772ac714949SMarcel Holtmann if (index != MGMT_INDEX_NONE) {
1773ac714949SMarcel Holtmann hdev = hci_dev_get(index);
177464ba2eb3SLuiz Augusto von Dentz if (!hdev)
177564ba2eb3SLuiz Augusto von Dentz return -ENODEV;
1776ac714949SMarcel Holtmann } else {
1777ac714949SMarcel Holtmann hdev = NULL;
1778ac714949SMarcel Holtmann }
1779ac714949SMarcel Holtmann
1780ac714949SMarcel Holtmann hdr->opcode = cpu_to_le16(HCI_MON_USER_LOGGING);
1781ac714949SMarcel Holtmann
1782ac714949SMarcel Holtmann hci_send_to_channel(HCI_CHANNEL_MONITOR, skb, HCI_SOCK_TRUSTED, NULL);
178364ba2eb3SLuiz Augusto von Dentz err = skb->len;
1784ac714949SMarcel Holtmann
1785ac714949SMarcel Holtmann if (hdev)
1786ac714949SMarcel Holtmann hci_dev_put(hdev);
1787ac714949SMarcel Holtmann
1788ac714949SMarcel Holtmann return err;
1789ac714949SMarcel Holtmann }
1790ac714949SMarcel Holtmann
hci_sock_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)17911b784140SYing Xue static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
17921b784140SYing Xue size_t len)
17931da177e4SLinus Torvalds {
17941da177e4SLinus Torvalds struct sock *sk = sock->sk;
1795801c1e8dSJohan Hedberg struct hci_mgmt_chan *chan;
17961da177e4SLinus Torvalds struct hci_dev *hdev;
17971da177e4SLinus Torvalds struct sk_buff *skb;
17981da177e4SLinus Torvalds int err;
17990b59e272STetsuo Handa const unsigned int flags = msg->msg_flags;
18001da177e4SLinus Torvalds
18011da177e4SLinus Torvalds BT_DBG("sock %p sk %p", sock, sk);
18021da177e4SLinus Torvalds
18030b59e272STetsuo Handa if (flags & MSG_OOB)
18041da177e4SLinus Torvalds return -EOPNOTSUPP;
18051da177e4SLinus Torvalds
18060b59e272STetsuo Handa if (flags & ~(MSG_DONTWAIT | MSG_NOSIGNAL | MSG_ERRQUEUE | MSG_CMSG_COMPAT))
18071da177e4SLinus Torvalds return -EINVAL;
18081da177e4SLinus Torvalds
180909572fcaSLuiz Augusto von Dentz if (len < 4 || len > hci_pi(sk)->mtu)
18101da177e4SLinus Torvalds return -EINVAL;
18111da177e4SLinus Torvalds
181264ba2eb3SLuiz Augusto von Dentz skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
181364ba2eb3SLuiz Augusto von Dentz if (IS_ERR(skb))
181464ba2eb3SLuiz Augusto von Dentz return PTR_ERR(skb);
18150b59e272STetsuo Handa
18161da177e4SLinus Torvalds lock_sock(sk);
18171da177e4SLinus Torvalds
18180381101fSJohan Hedberg switch (hci_pi(sk)->channel) {
18190381101fSJohan Hedberg case HCI_CHANNEL_RAW:
182023500189SMarcel Holtmann case HCI_CHANNEL_USER:
18210381101fSJohan Hedberg break;
1822cd82e61cSMarcel Holtmann case HCI_CHANNEL_MONITOR:
1823cd82e61cSMarcel Holtmann err = -EOPNOTSUPP;
182464ba2eb3SLuiz Augusto von Dentz goto drop;
1825ac714949SMarcel Holtmann case HCI_CHANNEL_LOGGING:
182664ba2eb3SLuiz Augusto von Dentz err = hci_logging_frame(sk, skb, flags);
182764ba2eb3SLuiz Augusto von Dentz goto drop;
18280381101fSJohan Hedberg default:
1829801c1e8dSJohan Hedberg mutex_lock(&mgmt_chan_list_lock);
1830801c1e8dSJohan Hedberg chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1831801c1e8dSJohan Hedberg if (chan)
183264ba2eb3SLuiz Augusto von Dentz err = hci_mgmt_cmd(chan, sk, skb);
1833801c1e8dSJohan Hedberg else
18340381101fSJohan Hedberg err = -EINVAL;
1835801c1e8dSJohan Hedberg
1836801c1e8dSJohan Hedberg mutex_unlock(&mgmt_chan_list_lock);
183764ba2eb3SLuiz Augusto von Dentz goto drop;
18380381101fSJohan Hedberg }
18390381101fSJohan Hedberg
1840e0448092STetsuo Handa hdev = hci_hdev_from_sock(sk);
1841e0448092STetsuo Handa if (IS_ERR(hdev)) {
1842e0448092STetsuo Handa err = PTR_ERR(hdev);
184364ba2eb3SLuiz Augusto von Dentz goto drop;
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds
18467e21addcSMarcel Holtmann if (!test_bit(HCI_UP, &hdev->flags)) {
18477e21addcSMarcel Holtmann err = -ENETDOWN;
184864ba2eb3SLuiz Augusto von Dentz goto drop;
18497e21addcSMarcel Holtmann }
18507e21addcSMarcel Holtmann
18518528d3f7SMarcel Holtmann hci_skb_pkt_type(skb) = skb->data[0];
18521da177e4SLinus Torvalds skb_pull(skb, 1);
18531da177e4SLinus Torvalds
18541bc5ad16SMarcel Holtmann if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
18551bc5ad16SMarcel Holtmann /* No permission check is needed for user channel
18561bc5ad16SMarcel Holtmann * since that gets enforced when binding the socket.
18571bc5ad16SMarcel Holtmann *
18581bc5ad16SMarcel Holtmann * However check that the packet type is valid.
18591bc5ad16SMarcel Holtmann */
1860d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT &&
1861d79f34e3SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1862cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1863cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
18641bc5ad16SMarcel Holtmann err = -EINVAL;
18651bc5ad16SMarcel Holtmann goto drop;
18661bc5ad16SMarcel Holtmann }
18671bc5ad16SMarcel Holtmann
18681bc5ad16SMarcel Holtmann skb_queue_tail(&hdev->raw_q, skb);
18691bc5ad16SMarcel Holtmann queue_work(hdev->workqueue, &hdev->tx_work);
1870d79f34e3SMarcel Holtmann } else if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
187183985319SHarvey Harrison u16 opcode = get_unaligned_le16(skb->data);
18721da177e4SLinus Torvalds u16 ogf = hci_opcode_ogf(opcode);
18731da177e4SLinus Torvalds u16 ocf = hci_opcode_ocf(opcode);
18741da177e4SLinus Torvalds
18751da177e4SLinus Torvalds if (((ogf > HCI_SFLT_MAX_OGF) ||
18763bb3c755SGustavo Padovan !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
18773bb3c755SGustavo Padovan &hci_sec_filter.ocf_mask[ogf])) &&
18781da177e4SLinus Torvalds !capable(CAP_NET_RAW)) {
18791da177e4SLinus Torvalds err = -EPERM;
18801da177e4SLinus Torvalds goto drop;
18811da177e4SLinus Torvalds }
18821da177e4SLinus Torvalds
18831982162bSMarcel Holtmann /* Since the opcode has already been extracted here, store
18841982162bSMarcel Holtmann * a copy of the value for later use by the drivers.
18851982162bSMarcel Holtmann */
18861982162bSMarcel Holtmann hci_skb_opcode(skb) = opcode;
18871982162bSMarcel Holtmann
1888fee746b0SMarcel Holtmann if (ogf == 0x3f) {
18891da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb);
18903eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work);
18911da177e4SLinus Torvalds } else {
189249c922bbSStephen Hemminger /* Stand-alone HCI commands must be flagged as
189311714b3dSJohan Hedberg * single-command requests.
189411714b3dSJohan Hedberg */
189544d27137SJohan Hedberg bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
189611714b3dSJohan Hedberg
18971da177e4SLinus Torvalds skb_queue_tail(&hdev->cmd_q, skb);
1898c347b765SGustavo F. Padovan queue_work(hdev->workqueue, &hdev->cmd_work);
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds } else {
19011da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) {
19021da177e4SLinus Torvalds err = -EPERM;
19031da177e4SLinus Torvalds goto drop;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds
1906d79f34e3SMarcel Holtmann if (hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
1907cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
1908cc974003SMarcel Holtmann hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
1909bb77543eSMarcel Holtmann err = -EINVAL;
1910bb77543eSMarcel Holtmann goto drop;
1911bb77543eSMarcel Holtmann }
1912bb77543eSMarcel Holtmann
19131da177e4SLinus Torvalds skb_queue_tail(&hdev->raw_q, skb);
19143eff45eaSGustavo F. Padovan queue_work(hdev->workqueue, &hdev->tx_work);
19151da177e4SLinus Torvalds }
19161da177e4SLinus Torvalds
19171da177e4SLinus Torvalds err = len;
19181da177e4SLinus Torvalds
19191da177e4SLinus Torvalds done:
19201da177e4SLinus Torvalds release_sock(sk);
19211da177e4SLinus Torvalds return err;
19221da177e4SLinus Torvalds
19231da177e4SLinus Torvalds drop:
19241da177e4SLinus Torvalds kfree_skb(skb);
19251da177e4SLinus Torvalds goto done;
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds
hci_sock_setsockopt_old(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int len)192809572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt_old(struct socket *sock, int level, int optname,
1929a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int len)
19301da177e4SLinus Torvalds {
19311da177e4SLinus Torvalds struct hci_ufilter uf = { .opcode = 0 };
19321da177e4SLinus Torvalds struct sock *sk = sock->sk;
19331da177e4SLinus Torvalds int err = 0, opt = 0;
19341da177e4SLinus Torvalds
19351da177e4SLinus Torvalds BT_DBG("sk %p, opt %d", sk, optname);
19361da177e4SLinus Torvalds
19371da177e4SLinus Torvalds lock_sock(sk);
19381da177e4SLinus Torvalds
19392f39cdb7SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
1940c2371e80SMarcel Holtmann err = -EBADFD;
19412f39cdb7SMarcel Holtmann goto done;
19422f39cdb7SMarcel Holtmann }
19432f39cdb7SMarcel Holtmann
19441da177e4SLinus Torvalds switch (optname) {
19451da177e4SLinus Torvalds case HCI_DATA_DIR:
1946*0c18a640SLuiz Augusto von Dentz err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, len);
1947*0c18a640SLuiz Augusto von Dentz if (err)
19481da177e4SLinus Torvalds break;
19491da177e4SLinus Torvalds
19501da177e4SLinus Torvalds if (opt)
19511da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
19521da177e4SLinus Torvalds else
19531da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
19541da177e4SLinus Torvalds break;
19551da177e4SLinus Torvalds
19561da177e4SLinus Torvalds case HCI_TIME_STAMP:
1957*0c18a640SLuiz Augusto von Dentz err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, len);
1958*0c18a640SLuiz Augusto von Dentz if (err)
19591da177e4SLinus Torvalds break;
19601da177e4SLinus Torvalds
19611da177e4SLinus Torvalds if (opt)
19621da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
19631da177e4SLinus Torvalds else
19641da177e4SLinus Torvalds hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
19651da177e4SLinus Torvalds break;
19661da177e4SLinus Torvalds
19671da177e4SLinus Torvalds case HCI_FILTER:
19680878b666SMarcel Holtmann {
19690878b666SMarcel Holtmann struct hci_filter *f = &hci_pi(sk)->filter;
19700878b666SMarcel Holtmann
19710878b666SMarcel Holtmann uf.type_mask = f->type_mask;
19720878b666SMarcel Holtmann uf.opcode = f->opcode;
19730878b666SMarcel Holtmann uf.event_mask[0] = *((u32 *) f->event_mask + 0);
19740878b666SMarcel Holtmann uf.event_mask[1] = *((u32 *) f->event_mask + 1);
19750878b666SMarcel Holtmann }
19760878b666SMarcel Holtmann
1977*0c18a640SLuiz Augusto von Dentz err = bt_copy_from_sockptr(&uf, sizeof(uf), optval, len);
1978*0c18a640SLuiz Augusto von Dentz if (err)
19791da177e4SLinus Torvalds break;
19801da177e4SLinus Torvalds
19811da177e4SLinus Torvalds if (!capable(CAP_NET_RAW)) {
19821da177e4SLinus Torvalds uf.type_mask &= hci_sec_filter.type_mask;
19831da177e4SLinus Torvalds uf.event_mask[0] &= *((u32 *) hci_sec_filter.event_mask + 0);
19841da177e4SLinus Torvalds uf.event_mask[1] &= *((u32 *) hci_sec_filter.event_mask + 1);
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds
19871da177e4SLinus Torvalds {
19881da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter;
19891da177e4SLinus Torvalds
19901da177e4SLinus Torvalds f->type_mask = uf.type_mask;
19911da177e4SLinus Torvalds f->opcode = uf.opcode;
19921da177e4SLinus Torvalds *((u32 *) f->event_mask + 0) = uf.event_mask[0];
19931da177e4SLinus Torvalds *((u32 *) f->event_mask + 1) = uf.event_mask[1];
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds break;
19961da177e4SLinus Torvalds
19971da177e4SLinus Torvalds default:
19981da177e4SLinus Torvalds err = -ENOPROTOOPT;
19991da177e4SLinus Torvalds break;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds
20022f39cdb7SMarcel Holtmann done:
20031da177e4SLinus Torvalds release_sock(sk);
20041da177e4SLinus Torvalds return err;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds
hci_sock_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int len)200709572fcaSLuiz Augusto von Dentz static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
200809572fcaSLuiz Augusto von Dentz sockptr_t optval, unsigned int len)
200909572fcaSLuiz Augusto von Dentz {
201009572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk;
2011b9f9dbadSDan Carpenter int err = 0;
2012b9f9dbadSDan Carpenter u16 opt;
201309572fcaSLuiz Augusto von Dentz
201409572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname);
201509572fcaSLuiz Augusto von Dentz
201609572fcaSLuiz Augusto von Dentz if (level == SOL_HCI)
201709572fcaSLuiz Augusto von Dentz return hci_sock_setsockopt_old(sock, level, optname, optval,
201809572fcaSLuiz Augusto von Dentz len);
201909572fcaSLuiz Augusto von Dentz
202009572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH)
202109572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT;
202209572fcaSLuiz Augusto von Dentz
202309572fcaSLuiz Augusto von Dentz lock_sock(sk);
202409572fcaSLuiz Augusto von Dentz
202509572fcaSLuiz Augusto von Dentz switch (optname) {
202609572fcaSLuiz Augusto von Dentz case BT_SNDMTU:
202709572fcaSLuiz Augusto von Dentz case BT_RCVMTU:
202809572fcaSLuiz Augusto von Dentz switch (hci_pi(sk)->channel) {
202909572fcaSLuiz Augusto von Dentz /* Don't allow changing MTU for channels that are meant for HCI
203009572fcaSLuiz Augusto von Dentz * traffic only.
203109572fcaSLuiz Augusto von Dentz */
203209572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_RAW:
203309572fcaSLuiz Augusto von Dentz case HCI_CHANNEL_USER:
203409572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT;
203509572fcaSLuiz Augusto von Dentz goto done;
203609572fcaSLuiz Augusto von Dentz }
203709572fcaSLuiz Augusto von Dentz
2038*0c18a640SLuiz Augusto von Dentz err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, len);
2039*0c18a640SLuiz Augusto von Dentz if (err)
204009572fcaSLuiz Augusto von Dentz break;
204109572fcaSLuiz Augusto von Dentz
204209572fcaSLuiz Augusto von Dentz hci_pi(sk)->mtu = opt;
204309572fcaSLuiz Augusto von Dentz break;
204409572fcaSLuiz Augusto von Dentz
204509572fcaSLuiz Augusto von Dentz default:
204609572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT;
204709572fcaSLuiz Augusto von Dentz break;
204809572fcaSLuiz Augusto von Dentz }
204909572fcaSLuiz Augusto von Dentz
205009572fcaSLuiz Augusto von Dentz done:
205109572fcaSLuiz Augusto von Dentz release_sock(sk);
205209572fcaSLuiz Augusto von Dentz return err;
205309572fcaSLuiz Augusto von Dentz }
205409572fcaSLuiz Augusto von Dentz
hci_sock_getsockopt_old(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)205509572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
20568fc9ced3SGustavo Padovan char __user *optval, int __user *optlen)
20571da177e4SLinus Torvalds {
20581da177e4SLinus Torvalds struct hci_ufilter uf;
20591da177e4SLinus Torvalds struct sock *sk = sock->sk;
2060cedc5469SMarcel Holtmann int len, opt, err = 0;
2061cedc5469SMarcel Holtmann
2062cedc5469SMarcel Holtmann BT_DBG("sk %p, opt %d", sk, optname);
20631da177e4SLinus Torvalds
20641da177e4SLinus Torvalds if (get_user(len, optlen))
20651da177e4SLinus Torvalds return -EFAULT;
20661da177e4SLinus Torvalds
2067cedc5469SMarcel Holtmann lock_sock(sk);
2068cedc5469SMarcel Holtmann
2069cedc5469SMarcel Holtmann if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
2070c2371e80SMarcel Holtmann err = -EBADFD;
2071cedc5469SMarcel Holtmann goto done;
2072cedc5469SMarcel Holtmann }
2073cedc5469SMarcel Holtmann
20741da177e4SLinus Torvalds switch (optname) {
20751da177e4SLinus Torvalds case HCI_DATA_DIR:
20761da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_DIR)
20771da177e4SLinus Torvalds opt = 1;
20781da177e4SLinus Torvalds else
20791da177e4SLinus Torvalds opt = 0;
20801da177e4SLinus Torvalds
20811da177e4SLinus Torvalds if (put_user(opt, optval))
2082cedc5469SMarcel Holtmann err = -EFAULT;
20831da177e4SLinus Torvalds break;
20841da177e4SLinus Torvalds
20851da177e4SLinus Torvalds case HCI_TIME_STAMP:
20861da177e4SLinus Torvalds if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
20871da177e4SLinus Torvalds opt = 1;
20881da177e4SLinus Torvalds else
20891da177e4SLinus Torvalds opt = 0;
20901da177e4SLinus Torvalds
20911da177e4SLinus Torvalds if (put_user(opt, optval))
2092cedc5469SMarcel Holtmann err = -EFAULT;
20931da177e4SLinus Torvalds break;
20941da177e4SLinus Torvalds
20951da177e4SLinus Torvalds case HCI_FILTER:
20961da177e4SLinus Torvalds {
20971da177e4SLinus Torvalds struct hci_filter *f = &hci_pi(sk)->filter;
20981da177e4SLinus Torvalds
2099e15ca9a0SMathias Krause memset(&uf, 0, sizeof(uf));
21001da177e4SLinus Torvalds uf.type_mask = f->type_mask;
21011da177e4SLinus Torvalds uf.opcode = f->opcode;
21021da177e4SLinus Torvalds uf.event_mask[0] = *((u32 *) f->event_mask + 0);
21031da177e4SLinus Torvalds uf.event_mask[1] = *((u32 *) f->event_mask + 1);
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds
21061da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(uf));
21071da177e4SLinus Torvalds if (copy_to_user(optval, &uf, len))
2108cedc5469SMarcel Holtmann err = -EFAULT;
21091da177e4SLinus Torvalds break;
21101da177e4SLinus Torvalds
21111da177e4SLinus Torvalds default:
2112cedc5469SMarcel Holtmann err = -ENOPROTOOPT;
21131da177e4SLinus Torvalds break;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds
2116cedc5469SMarcel Holtmann done:
2117cedc5469SMarcel Holtmann release_sock(sk);
2118cedc5469SMarcel Holtmann return err;
21191da177e4SLinus Torvalds }
21201da177e4SLinus Torvalds
hci_sock_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)212109572fcaSLuiz Augusto von Dentz static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
212209572fcaSLuiz Augusto von Dentz char __user *optval, int __user *optlen)
212309572fcaSLuiz Augusto von Dentz {
212409572fcaSLuiz Augusto von Dentz struct sock *sk = sock->sk;
212509572fcaSLuiz Augusto von Dentz int err = 0;
212609572fcaSLuiz Augusto von Dentz
212709572fcaSLuiz Augusto von Dentz BT_DBG("sk %p, opt %d", sk, optname);
212809572fcaSLuiz Augusto von Dentz
212909572fcaSLuiz Augusto von Dentz if (level == SOL_HCI)
213009572fcaSLuiz Augusto von Dentz return hci_sock_getsockopt_old(sock, level, optname, optval,
213109572fcaSLuiz Augusto von Dentz optlen);
213209572fcaSLuiz Augusto von Dentz
213309572fcaSLuiz Augusto von Dentz if (level != SOL_BLUETOOTH)
213409572fcaSLuiz Augusto von Dentz return -ENOPROTOOPT;
213509572fcaSLuiz Augusto von Dentz
213609572fcaSLuiz Augusto von Dentz lock_sock(sk);
213709572fcaSLuiz Augusto von Dentz
213809572fcaSLuiz Augusto von Dentz switch (optname) {
213909572fcaSLuiz Augusto von Dentz case BT_SNDMTU:
214009572fcaSLuiz Augusto von Dentz case BT_RCVMTU:
214109572fcaSLuiz Augusto von Dentz if (put_user(hci_pi(sk)->mtu, (u16 __user *)optval))
214209572fcaSLuiz Augusto von Dentz err = -EFAULT;
214309572fcaSLuiz Augusto von Dentz break;
214409572fcaSLuiz Augusto von Dentz
214509572fcaSLuiz Augusto von Dentz default:
214609572fcaSLuiz Augusto von Dentz err = -ENOPROTOOPT;
214709572fcaSLuiz Augusto von Dentz break;
214809572fcaSLuiz Augusto von Dentz }
214909572fcaSLuiz Augusto von Dentz
215009572fcaSLuiz Augusto von Dentz release_sock(sk);
215109572fcaSLuiz Augusto von Dentz return err;
215209572fcaSLuiz Augusto von Dentz }
215309572fcaSLuiz Augusto von Dentz
hci_sock_destruct(struct sock * sk)2154709fca50SNguyen Dinh Phi static void hci_sock_destruct(struct sock *sk)
2155709fca50SNguyen Dinh Phi {
2156b338d917SBrian Gix mgmt_cleanup(sk);
2157709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_receive_queue);
2158709fca50SNguyen Dinh Phi skb_queue_purge(&sk->sk_write_queue);
2159709fca50SNguyen Dinh Phi }
2160709fca50SNguyen Dinh Phi
216190ddc4f0SEric Dumazet static const struct proto_ops hci_sock_ops = {
21621da177e4SLinus Torvalds .family = PF_BLUETOOTH,
21631da177e4SLinus Torvalds .owner = THIS_MODULE,
21641da177e4SLinus Torvalds .release = hci_sock_release,
21651da177e4SLinus Torvalds .bind = hci_sock_bind,
21661da177e4SLinus Torvalds .getname = hci_sock_getname,
21671da177e4SLinus Torvalds .sendmsg = hci_sock_sendmsg,
21681da177e4SLinus Torvalds .recvmsg = hci_sock_recvmsg,
21691da177e4SLinus Torvalds .ioctl = hci_sock_ioctl,
21707a6038b3SArnd Bergmann #ifdef CONFIG_COMPAT
21717a6038b3SArnd Bergmann .compat_ioctl = hci_sock_compat_ioctl,
21727a6038b3SArnd Bergmann #endif
2173a11e1d43SLinus Torvalds .poll = datagram_poll,
21741da177e4SLinus Torvalds .listen = sock_no_listen,
21751da177e4SLinus Torvalds .shutdown = sock_no_shutdown,
21761da177e4SLinus Torvalds .setsockopt = hci_sock_setsockopt,
21771da177e4SLinus Torvalds .getsockopt = hci_sock_getsockopt,
21781da177e4SLinus Torvalds .connect = sock_no_connect,
21791da177e4SLinus Torvalds .socketpair = sock_no_socketpair,
21801da177e4SLinus Torvalds .accept = sock_no_accept,
21811da177e4SLinus Torvalds .mmap = sock_no_mmap
21821da177e4SLinus Torvalds };
21831da177e4SLinus Torvalds
21841da177e4SLinus Torvalds static struct proto hci_sk_proto = {
21851da177e4SLinus Torvalds .name = "HCI",
21861da177e4SLinus Torvalds .owner = THIS_MODULE,
21871da177e4SLinus Torvalds .obj_size = sizeof(struct hci_pinfo)
21881da177e4SLinus Torvalds };
21891da177e4SLinus Torvalds
hci_sock_create(struct net * net,struct socket * sock,int protocol,int kern)21903f378b68SEric Paris static int hci_sock_create(struct net *net, struct socket *sock, int protocol,
21913f378b68SEric Paris int kern)
21921da177e4SLinus Torvalds {
21931da177e4SLinus Torvalds struct sock *sk;
21941da177e4SLinus Torvalds
21951da177e4SLinus Torvalds BT_DBG("sock %p", sock);
21961da177e4SLinus Torvalds
21971da177e4SLinus Torvalds if (sock->type != SOCK_RAW)
21981da177e4SLinus Torvalds return -ESOCKTNOSUPPORT;
21991da177e4SLinus Torvalds
22001da177e4SLinus Torvalds sock->ops = &hci_sock_ops;
22011da177e4SLinus Torvalds
22026bfa273eSLuiz Augusto von Dentz sk = bt_sock_alloc(net, sock, &hci_sk_proto, protocol, GFP_ATOMIC,
22036bfa273eSLuiz Augusto von Dentz kern);
22041da177e4SLinus Torvalds if (!sk)
22051da177e4SLinus Torvalds return -ENOMEM;
22061da177e4SLinus Torvalds
22071da177e4SLinus Torvalds sock->state = SS_UNCONNECTED;
2208709fca50SNguyen Dinh Phi sk->sk_destruct = hci_sock_destruct;
22091da177e4SLinus Torvalds
22101da177e4SLinus Torvalds bt_sock_link(&hci_sk_list, sk);
22111da177e4SLinus Torvalds return 0;
22121da177e4SLinus Torvalds }
22131da177e4SLinus Torvalds
2214ec1b4cf7SStephen Hemminger static const struct net_proto_family hci_sock_family_ops = {
22151da177e4SLinus Torvalds .family = PF_BLUETOOTH,
22161da177e4SLinus Torvalds .owner = THIS_MODULE,
22171da177e4SLinus Torvalds .create = hci_sock_create,
22181da177e4SLinus Torvalds };
22191da177e4SLinus Torvalds
hci_sock_init(void)22201da177e4SLinus Torvalds int __init hci_sock_init(void)
22211da177e4SLinus Torvalds {
22221da177e4SLinus Torvalds int err;
22231da177e4SLinus Torvalds
2224b0a8e282SMarcel Holtmann BUILD_BUG_ON(sizeof(struct sockaddr_hci) > sizeof(struct sockaddr));
2225b0a8e282SMarcel Holtmann
22261da177e4SLinus Torvalds err = proto_register(&hci_sk_proto, 0);
22271da177e4SLinus Torvalds if (err < 0)
22281da177e4SLinus Torvalds return err;
22291da177e4SLinus Torvalds
22301da177e4SLinus Torvalds err = bt_sock_register(BTPROTO_HCI, &hci_sock_family_ops);
2231f7c86637SMasatake YAMATO if (err < 0) {
2232f7c86637SMasatake YAMATO BT_ERR("HCI socket registration failed");
22331da177e4SLinus Torvalds goto error;
2234f7c86637SMasatake YAMATO }
2235f7c86637SMasatake YAMATO
2236b0316615SAl Viro err = bt_procfs_init(&init_net, "hci", &hci_sk_list, NULL);
2237f7c86637SMasatake YAMATO if (err < 0) {
2238f7c86637SMasatake YAMATO BT_ERR("Failed to create HCI proc file");
2239f7c86637SMasatake YAMATO bt_sock_unregister(BTPROTO_HCI);
2240f7c86637SMasatake YAMATO goto error;
2241f7c86637SMasatake YAMATO }
22421da177e4SLinus Torvalds
22431da177e4SLinus Torvalds BT_INFO("HCI socket layer initialized");
22441da177e4SLinus Torvalds
22451da177e4SLinus Torvalds return 0;
22461da177e4SLinus Torvalds
22471da177e4SLinus Torvalds error:
22481da177e4SLinus Torvalds proto_unregister(&hci_sk_proto);
22491da177e4SLinus Torvalds return err;
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds
hci_sock_cleanup(void)2252b7440a14SAnand Gadiyar void hci_sock_cleanup(void)
22531da177e4SLinus Torvalds {
2254f7c86637SMasatake YAMATO bt_procfs_cleanup(&init_net, "hci");
22555e9d7f86SDavid Herrmann bt_sock_unregister(BTPROTO_HCI);
22561da177e4SLinus Torvalds proto_unregister(&hci_sk_proto);
22571da177e4SLinus Torvalds }
2258