2 BlueZ - Bluetooth protocol stack for Linux
4 Copyright (C) 2015 Intel Corporation
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
24 #include <asm/unaligned.h>
26 #include <net/bluetooth/bluetooth.h>
27 #include <net/bluetooth/hci_core.h>
28 #include <net/bluetooth/hci_mon.h>
29 #include <net/bluetooth/mgmt.h>
31 #include "mgmt_util.h"
33 static struct sk_buff *create_monitor_ctrl_event(__le16 index, u32 cookie,
34 u16 opcode, u16 len, void *buf)
36 struct hci_mon_hdr *hdr;
39 skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
43 put_unaligned_le32(cookie, skb_put(skb, 4));
44 put_unaligned_le16(opcode, skb_put(skb, 2));
47 skb_put_data(skb, buf, len);
51 hdr = skb_push(skb, HCI_MON_HDR_SIZE);
52 hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
54 hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
59 struct sk_buff *mgmt_alloc_skb(struct hci_dev *hdev, u16 opcode,
64 skb = alloc_skb(sizeof(struct mgmt_hdr) + size, GFP_KERNEL);
68 skb_reserve(skb, sizeof(struct mgmt_hdr));
69 bt_cb(skb)->mgmt.hdev = hdev;
70 bt_cb(skb)->mgmt.opcode = opcode;
75 int mgmt_send_event_skb(unsigned short channel, struct sk_buff *skb, int flag,
85 hdev = bt_cb(skb)->mgmt.hdev;
90 /* Send just the data, without headers, to the monitor */
91 if (channel == HCI_CHANNEL_CONTROL)
92 hci_send_monitor_ctrl_event(hdev, bt_cb(skb)->mgmt.opcode,
94 skb_get_ktime(skb), flag, skip_sk);
96 hdr = skb_push(skb, sizeof(*hdr));
97 hdr->opcode = cpu_to_le16(bt_cb(skb)->mgmt.opcode);
99 hdr->index = cpu_to_le16(hdev->id);
101 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
102 hdr->len = cpu_to_le16(len);
104 hci_send_to_channel(channel, skb, flag, skip_sk);
110 int mgmt_send_event(u16 event, struct hci_dev *hdev, unsigned short channel,
111 void *data, u16 data_len, int flag, struct sock *skip_sk)
115 skb = mgmt_alloc_skb(hdev, event, data_len);
120 skb_put_data(skb, data, data_len);
122 return mgmt_send_event_skb(channel, skb, flag, skip_sk);
125 int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
127 struct sk_buff *skb, *mskb;
128 struct mgmt_hdr *hdr;
129 struct mgmt_ev_cmd_status *ev;
132 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
134 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
138 hdr = skb_put(skb, sizeof(*hdr));
140 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
141 hdr->index = cpu_to_le16(index);
142 hdr->len = cpu_to_le16(sizeof(*ev));
144 ev = skb_put(skb, sizeof(*ev));
146 ev->opcode = cpu_to_le16(cmd);
148 mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
149 MGMT_EV_CMD_STATUS, sizeof(*ev), ev);
151 skb->tstamp = mskb->tstamp;
153 __net_timestamp(skb);
155 err = sock_queue_rcv_skb(sk, skb);
160 hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
161 HCI_SOCK_TRUSTED, NULL);
168 int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
169 void *rp, size_t rp_len)
171 struct sk_buff *skb, *mskb;
172 struct mgmt_hdr *hdr;
173 struct mgmt_ev_cmd_complete *ev;
176 BT_DBG("sock %p", sk);
178 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
182 hdr = skb_put(skb, sizeof(*hdr));
184 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
185 hdr->index = cpu_to_le16(index);
186 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
188 ev = skb_put(skb, sizeof(*ev) + rp_len);
189 ev->opcode = cpu_to_le16(cmd);
193 memcpy(ev->data, rp, rp_len);
195 mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
196 MGMT_EV_CMD_COMPLETE,
197 sizeof(*ev) + rp_len, ev);
199 skb->tstamp = mskb->tstamp;
201 __net_timestamp(skb);
203 err = sock_queue_rcv_skb(sk, skb);
208 hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
209 HCI_SOCK_TRUSTED, NULL);
216 struct mgmt_pending_cmd *mgmt_pending_find(unsigned short channel, u16 opcode,
217 struct hci_dev *hdev)
219 struct mgmt_pending_cmd *cmd;
221 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
222 if (hci_sock_get_channel(cmd->sk) != channel)
224 if (cmd->opcode == opcode)
231 struct mgmt_pending_cmd *mgmt_pending_find_data(unsigned short channel,
233 struct hci_dev *hdev,
236 struct mgmt_pending_cmd *cmd;
238 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
239 if (cmd->user_data != data)
241 if (cmd->opcode == opcode)
248 void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
249 void (*cb)(struct mgmt_pending_cmd *cmd, void *data),
252 struct mgmt_pending_cmd *cmd, *tmp;
254 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
255 if (opcode > 0 && cmd->opcode != opcode)
262 struct mgmt_pending_cmd *mgmt_pending_new(struct sock *sk, u16 opcode,
263 struct hci_dev *hdev,
266 struct mgmt_pending_cmd *cmd;
268 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
272 cmd->opcode = opcode;
273 cmd->index = hdev->id;
275 cmd->param = kmemdup(data, len, GFP_KERNEL);
281 cmd->param_len = len;
289 struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
290 struct hci_dev *hdev,
293 struct mgmt_pending_cmd *cmd;
295 cmd = mgmt_pending_new(sk, opcode, hdev, data, len);
299 list_add(&cmd->list, &hdev->mgmt_pending);
304 void mgmt_pending_free(struct mgmt_pending_cmd *cmd)
311 void mgmt_pending_remove(struct mgmt_pending_cmd *cmd)
313 list_del(&cmd->list);
314 mgmt_pending_free(cmd);