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 int mgmt_send_event(u16 event, struct hci_dev *hdev, unsigned short channel,
60 void *data, u16 data_len, int flag, struct sock *skip_sk)
65 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
69 hdr = skb_put(skb, sizeof(*hdr));
70 hdr->opcode = cpu_to_le16(event);
72 hdr->index = cpu_to_le16(hdev->id);
74 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
75 hdr->len = cpu_to_le16(data_len);
78 skb_put_data(skb, data, data_len);
83 hci_send_to_channel(channel, skb, flag, skip_sk);
85 if (channel == HCI_CHANNEL_CONTROL)
86 hci_send_monitor_ctrl_event(hdev, event, data, data_len,
87 skb_get_ktime(skb), flag, skip_sk);
93 int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
95 struct sk_buff *skb, *mskb;
97 struct mgmt_ev_cmd_status *ev;
100 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
102 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
106 hdr = skb_put(skb, sizeof(*hdr));
108 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
109 hdr->index = cpu_to_le16(index);
110 hdr->len = cpu_to_le16(sizeof(*ev));
112 ev = skb_put(skb, sizeof(*ev));
114 ev->opcode = cpu_to_le16(cmd);
116 mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
117 MGMT_EV_CMD_STATUS, sizeof(*ev), ev);
119 skb->tstamp = mskb->tstamp;
121 __net_timestamp(skb);
123 err = sock_queue_rcv_skb(sk, skb);
128 hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
129 HCI_SOCK_TRUSTED, NULL);
136 int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
137 void *rp, size_t rp_len)
139 struct sk_buff *skb, *mskb;
140 struct mgmt_hdr *hdr;
141 struct mgmt_ev_cmd_complete *ev;
144 BT_DBG("sock %p", sk);
146 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
150 hdr = skb_put(skb, sizeof(*hdr));
152 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
153 hdr->index = cpu_to_le16(index);
154 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
156 ev = skb_put(skb, sizeof(*ev) + rp_len);
157 ev->opcode = cpu_to_le16(cmd);
161 memcpy(ev->data, rp, rp_len);
163 mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
164 MGMT_EV_CMD_COMPLETE,
165 sizeof(*ev) + rp_len, ev);
167 skb->tstamp = mskb->tstamp;
169 __net_timestamp(skb);
171 err = sock_queue_rcv_skb(sk, skb);
176 hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
177 HCI_SOCK_TRUSTED, NULL);
184 struct mgmt_pending_cmd *mgmt_pending_find(unsigned short channel, u16 opcode,
185 struct hci_dev *hdev)
187 struct mgmt_pending_cmd *cmd;
189 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
190 if (hci_sock_get_channel(cmd->sk) != channel)
192 if (cmd->opcode == opcode)
199 struct mgmt_pending_cmd *mgmt_pending_find_data(unsigned short channel,
201 struct hci_dev *hdev,
204 struct mgmt_pending_cmd *cmd;
206 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
207 if (cmd->user_data != data)
209 if (cmd->opcode == opcode)
216 void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
217 void (*cb)(struct mgmt_pending_cmd *cmd, void *data),
220 struct mgmt_pending_cmd *cmd, *tmp;
222 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
223 if (opcode > 0 && cmd->opcode != opcode)
230 struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
231 struct hci_dev *hdev,
234 struct mgmt_pending_cmd *cmd;
236 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
240 cmd->opcode = opcode;
241 cmd->index = hdev->id;
243 cmd->param = kmemdup(data, len, GFP_KERNEL);
249 cmd->param_len = len;
254 list_add(&cmd->list, &hdev->mgmt_pending);
259 void mgmt_pending_free(struct mgmt_pending_cmd *cmd)
266 void mgmt_pending_remove(struct mgmt_pending_cmd *cmd)
268 list_del(&cmd->list);
269 mgmt_pending_free(cmd);