mgmt_util.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2015 Intel Corporation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <asm/unaligned.h>
  20. #include <net/bluetooth/bluetooth.h>
  21. #include <net/bluetooth/hci_core.h>
  22. #include <net/bluetooth/hci_mon.h>
  23. #include <net/bluetooth/mgmt.h>
  24. #include "mgmt_util.h"
  25. static struct sk_buff *create_monitor_ctrl_event(__le16 index, u32 cookie,
  26. u16 opcode, u16 len, void *buf)
  27. {
  28. struct hci_mon_hdr *hdr;
  29. struct sk_buff *skb;
  30. skb = bt_skb_alloc(6 + len, GFP_ATOMIC);
  31. if (!skb)
  32. return NULL;
  33. put_unaligned_le32(cookie, skb_put(skb, 4));
  34. put_unaligned_le16(opcode, skb_put(skb, 2));
  35. if (buf)
  36. skb_put_data(skb, buf, len);
  37. __net_timestamp(skb);
  38. hdr = skb_push(skb, HCI_MON_HDR_SIZE);
  39. hdr->opcode = cpu_to_le16(HCI_MON_CTRL_EVENT);
  40. hdr->index = index;
  41. hdr->len = cpu_to_le16(skb->len - HCI_MON_HDR_SIZE);
  42. return skb;
  43. }
  44. int mgmt_send_event(u16 event, struct hci_dev *hdev, unsigned short channel,
  45. void *data, u16 data_len, int flag, struct sock *skip_sk)
  46. {
  47. struct sk_buff *skb;
  48. struct mgmt_hdr *hdr;
  49. skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
  50. if (!skb)
  51. return -ENOMEM;
  52. hdr = skb_put(skb, sizeof(*hdr));
  53. hdr->opcode = cpu_to_le16(event);
  54. if (hdev)
  55. hdr->index = cpu_to_le16(hdev->id);
  56. else
  57. hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
  58. hdr->len = cpu_to_le16(data_len);
  59. if (data)
  60. skb_put_data(skb, data, data_len);
  61. /* Time stamp */
  62. __net_timestamp(skb);
  63. hci_send_to_channel(channel, skb, flag, skip_sk);
  64. if (channel == HCI_CHANNEL_CONTROL)
  65. hci_send_monitor_ctrl_event(hdev, event, data, data_len,
  66. skb_get_ktime(skb), flag, skip_sk);
  67. kfree_skb(skb);
  68. return 0;
  69. }
  70. int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
  71. {
  72. struct sk_buff *skb, *mskb;
  73. struct mgmt_hdr *hdr;
  74. struct mgmt_ev_cmd_status *ev;
  75. int err;
  76. BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
  77. skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
  78. if (!skb)
  79. return -ENOMEM;
  80. hdr = skb_put(skb, sizeof(*hdr));
  81. hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
  82. hdr->index = cpu_to_le16(index);
  83. hdr->len = cpu_to_le16(sizeof(*ev));
  84. ev = skb_put(skb, sizeof(*ev));
  85. ev->status = status;
  86. ev->opcode = cpu_to_le16(cmd);
  87. mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
  88. MGMT_EV_CMD_STATUS, sizeof(*ev), ev);
  89. if (mskb)
  90. skb->tstamp = mskb->tstamp;
  91. else
  92. __net_timestamp(skb);
  93. err = sock_queue_rcv_skb(sk, skb);
  94. if (err < 0)
  95. kfree_skb(skb);
  96. if (mskb) {
  97. hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
  98. HCI_SOCK_TRUSTED, NULL);
  99. kfree_skb(mskb);
  100. }
  101. return err;
  102. }
  103. int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
  104. void *rp, size_t rp_len)
  105. {
  106. struct sk_buff *skb, *mskb;
  107. struct mgmt_hdr *hdr;
  108. struct mgmt_ev_cmd_complete *ev;
  109. int err;
  110. BT_DBG("sock %p", sk);
  111. skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
  112. if (!skb)
  113. return -ENOMEM;
  114. hdr = skb_put(skb, sizeof(*hdr));
  115. hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
  116. hdr->index = cpu_to_le16(index);
  117. hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
  118. ev = skb_put(skb, sizeof(*ev) + rp_len);
  119. ev->opcode = cpu_to_le16(cmd);
  120. ev->status = status;
  121. if (rp)
  122. memcpy(ev->data, rp, rp_len);
  123. mskb = create_monitor_ctrl_event(hdr->index, hci_sock_get_cookie(sk),
  124. MGMT_EV_CMD_COMPLETE,
  125. sizeof(*ev) + rp_len, ev);
  126. if (mskb)
  127. skb->tstamp = mskb->tstamp;
  128. else
  129. __net_timestamp(skb);
  130. err = sock_queue_rcv_skb(sk, skb);
  131. if (err < 0)
  132. kfree_skb(skb);
  133. if (mskb) {
  134. hci_send_to_channel(HCI_CHANNEL_MONITOR, mskb,
  135. HCI_SOCK_TRUSTED, NULL);
  136. kfree_skb(mskb);
  137. }
  138. return err;
  139. }
  140. struct mgmt_pending_cmd *mgmt_pending_find(unsigned short channel, u16 opcode,
  141. struct hci_dev *hdev)
  142. {
  143. struct mgmt_pending_cmd *cmd;
  144. list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
  145. if (hci_sock_get_channel(cmd->sk) != channel)
  146. continue;
  147. if (cmd->opcode == opcode)
  148. return cmd;
  149. }
  150. return NULL;
  151. }
  152. struct mgmt_pending_cmd *mgmt_pending_find_data(unsigned short channel,
  153. u16 opcode,
  154. struct hci_dev *hdev,
  155. const void *data)
  156. {
  157. struct mgmt_pending_cmd *cmd;
  158. list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
  159. if (cmd->user_data != data)
  160. continue;
  161. if (cmd->opcode == opcode)
  162. return cmd;
  163. }
  164. return NULL;
  165. }
  166. void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
  167. void (*cb)(struct mgmt_pending_cmd *cmd, void *data),
  168. void *data)
  169. {
  170. struct mgmt_pending_cmd *cmd, *tmp;
  171. list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
  172. if (opcode > 0 && cmd->opcode != opcode)
  173. continue;
  174. cb(cmd, data);
  175. }
  176. }
  177. struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
  178. struct hci_dev *hdev,
  179. void *data, u16 len)
  180. {
  181. struct mgmt_pending_cmd *cmd;
  182. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  183. if (!cmd)
  184. return NULL;
  185. cmd->opcode = opcode;
  186. cmd->index = hdev->id;
  187. cmd->param = kmemdup(data, len, GFP_KERNEL);
  188. if (!cmd->param) {
  189. kfree(cmd);
  190. return NULL;
  191. }
  192. cmd->param_len = len;
  193. cmd->sk = sk;
  194. sock_hold(sk);
  195. list_add(&cmd->list, &hdev->mgmt_pending);
  196. return cmd;
  197. }
  198. void mgmt_pending_free(struct mgmt_pending_cmd *cmd)
  199. {
  200. sock_put(cmd->sk);
  201. kfree(cmd->param);
  202. kfree(cmd);
  203. }
  204. void mgmt_pending_remove(struct mgmt_pending_cmd *cmd)
  205. {
  206. list_del(&cmd->list);
  207. mgmt_pending_free(cmd);
  208. }