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,
86 hdev = bt_cb(skb)->mgmt.hdev;
91 /* Send just the data, without headers, to the monitor */
92 if (channel == HCI_CHANNEL_CONTROL)
93 hci_send_monitor_ctrl_event(hdev, bt_cb(skb)->mgmt.opcode,
95 skb_get_ktime(skb), flag, skip_sk);
97 hdr = skb_push(skb, sizeof(*hdr));
98 hdr->opcode = cpu_to_le16(bt_cb(skb)->mgmt.opcode);
100 hdr->index = cpu_to_le16(hdev->id);
102 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
103 hdr->len = cpu_to_le16(len);
105 hci_send_to_channel(channel, skb, flag, skip_sk);
111 int mgmt_send_event(u16 event, struct hci_dev *hdev, unsigned short channel,
112 void *data, u16 data_len, int flag, struct sock *skip_sk)
116 skb = mgmt_alloc_skb(hdev, event, data_len);
121 skb_put_data(skb, data, data_len);
123 return mgmt_send_event_skb(channel, skb, flag, skip_sk);
126 int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
128 struct sk_buff *skb, *mskb;
129 struct mgmt_hdr *hdr;
130 struct mgmt_ev_cmd_status *ev;
133 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
135 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
139 hdr = skb_put(skb, sizeof(*hdr));
141 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
142 hdr->index = cpu_to_le16(index);
143 hdr->len = cpu_to_le16(sizeof(*ev));
145 ev = skb_put(skb, sizeof(*ev));
147 ev->opcode = cpu_to_le16(cmd);
149 mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
150 MGMT_EV_CMD_STATUS, sizeof(*ev), ev);
152 skb->tstamp = mskb->tstamp;
154 __net_timestamp(skb);
156 err = sock_queue_rcv_skb(sk, skb);
161 hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
162 HCI_SOCK_TRUSTED, NULL);
169 int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
170 void *rp, size_t rp_len)
172 struct sk_buff *skb, *mskb;
173 struct mgmt_hdr *hdr;
174 struct mgmt_ev_cmd_complete *ev;
177 BT_DBG("sock %p", sk);
179 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
183 hdr = skb_put(skb, sizeof(*hdr));
185 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
186 hdr->index = cpu_to_le16(index);
187 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
189 ev = skb_put(skb, sizeof(*ev) + rp_len);
190 ev->opcode = cpu_to_le16(cmd);
194 memcpy(ev->data, rp, rp_len);
196 mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
197 MGMT_EV_CMD_COMPLETE,
198 sizeof(*ev) + rp_len, ev);
200 skb->tstamp = mskb->tstamp;
202 __net_timestamp(skb);
204 err = sock_queue_rcv_skb(sk, skb);
209 hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
210 HCI_SOCK_TRUSTED, NULL);
217 struct mgmt_pending_cmd *mgmt_pending_find(unsigned short channel, u16 opcode,
218 struct hci_dev *hdev)
220 struct mgmt_pending_cmd *cmd;
222 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
223 if (hci_sock_get_channel(cmd->sk) != channel)
225 if (cmd->opcode == opcode)
232 struct mgmt_pending_cmd *mgmt_pending_find_data(unsigned short channel,
234 struct hci_dev *hdev,
237 struct mgmt_pending_cmd *cmd;
239 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
240 if (cmd->user_data != data)
242 if (cmd->opcode == opcode)
249 void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
250 void (*cb)(struct mgmt_pending_cmd *cmd, void *data),
253 struct mgmt_pending_cmd *cmd, *tmp;
255 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
256 if (opcode > 0 && cmd->opcode != opcode)
263 struct mgmt_pending_cmd *mgmt_pending_new(struct sock *sk, u16 opcode,
264 struct hci_dev *hdev,
267 struct mgmt_pending_cmd *cmd;
269 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
273 cmd->opcode = opcode;
274 cmd->index = hdev->id;
276 cmd->param = kmemdup(data, len, GFP_KERNEL);
282 cmd->param_len = len;
290 struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
291 struct hci_dev *hdev,
294 struct mgmt_pending_cmd *cmd;
296 cmd = mgmt_pending_new(sk, opcode, hdev, data, len);
300 list_add_tail(&cmd->list, &hdev->mgmt_pending);
305 void mgmt_pending_free(struct mgmt_pending_cmd *cmd)
312 void mgmt_pending_remove(struct mgmt_pending_cmd *cmd)
314 list_del(&cmd->list);
315 mgmt_pending_free(cmd);
318 void mgmt_mesh_foreach(struct hci_dev *hdev,
319 void (*cb)(struct mgmt_mesh_tx *mesh_tx, void *data),
320 void *data, struct sock *sk)
322 struct mgmt_mesh_tx *mesh_tx, *tmp;
324 list_for_each_entry_safe(mesh_tx, tmp, &hdev->mgmt_pending, list) {
325 if (!sk || mesh_tx->sk == sk)
330 struct mgmt_mesh_tx *mgmt_mesh_next(struct hci_dev *hdev, struct sock *sk)
332 struct mgmt_mesh_tx *mesh_tx;
334 if (list_empty(&hdev->mesh_pending))
337 list_for_each_entry(mesh_tx, &hdev->mesh_pending, list) {
338 if (!sk || mesh_tx->sk == sk)
345 struct mgmt_mesh_tx *mgmt_mesh_find(struct hci_dev *hdev, u8 handle)
347 struct mgmt_mesh_tx *mesh_tx;
349 if (list_empty(&hdev->mesh_pending))
352 list_for_each_entry(mesh_tx, &hdev->mesh_pending, list) {
353 if (mesh_tx->handle == handle)
360 struct mgmt_mesh_tx *mgmt_mesh_add(struct sock *sk, struct hci_dev *hdev,
363 struct mgmt_mesh_tx *mesh_tx;
365 mesh_tx = kzalloc(sizeof(*mesh_tx), GFP_KERNEL);
369 hdev->mesh_send_ref++;
370 if (!hdev->mesh_send_ref)
371 hdev->mesh_send_ref++;
373 mesh_tx->handle = hdev->mesh_send_ref;
374 mesh_tx->index = hdev->id;
375 memcpy(mesh_tx->param, data, len);
376 mesh_tx->param_len = len;
380 list_add_tail(&mesh_tx->list, &hdev->mesh_pending);
385 void mgmt_mesh_remove(struct mgmt_mesh_tx *mesh_tx)
387 list_del(&mesh_tx->list);
388 sock_put(mesh_tx->sk);