Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame^] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2010 Nokia Corporation |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
| 23 | /* Bluetooth HCI Management interface */ |
| 24 | |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/unaligned.h> |
| 27 | |
| 28 | #include <net/bluetooth/bluetooth.h> |
| 29 | #include <net/bluetooth/hci_core.h> |
| 30 | #include <net/bluetooth/mgmt.h> |
| 31 | |
| 32 | static void cmd_status(struct sock *sk, u16 cmd, u8 status) |
| 33 | { |
| 34 | struct sk_buff *skb; |
| 35 | struct mgmt_hdr *hdr; |
| 36 | struct mgmt_ev_cmd_status *ev; |
| 37 | |
| 38 | BT_DBG("sock %p", sk); |
| 39 | |
| 40 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC); |
| 41 | if (!skb) |
| 42 | return; |
| 43 | |
| 44 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 45 | |
| 46 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
| 47 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 48 | |
| 49 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 50 | ev->status = status; |
| 51 | put_unaligned_le16(cmd, &ev->opcode); |
| 52 | |
| 53 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 54 | kfree_skb(skb); |
| 55 | } |
| 56 | |
| 57 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 58 | { |
| 59 | unsigned char *buf; |
| 60 | struct mgmt_hdr *hdr; |
| 61 | u16 opcode, len; |
| 62 | int err; |
| 63 | |
| 64 | BT_DBG("got %zu bytes", msglen); |
| 65 | |
| 66 | if (msglen < sizeof(*hdr)) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | buf = kmalloc(msglen, GFP_ATOMIC); |
| 70 | if (!buf) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 74 | err = -EFAULT; |
| 75 | goto done; |
| 76 | } |
| 77 | |
| 78 | hdr = (struct mgmt_hdr *) buf; |
| 79 | opcode = get_unaligned_le16(&hdr->opcode); |
| 80 | len = get_unaligned_le16(&hdr->len); |
| 81 | |
| 82 | if (len != msglen - sizeof(*hdr)) { |
| 83 | err = -EINVAL; |
| 84 | goto done; |
| 85 | } |
| 86 | |
| 87 | switch (opcode) { |
| 88 | default: |
| 89 | BT_DBG("Unknown op %u", opcode); |
| 90 | cmd_status(sk, opcode, 0x01); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | err = msglen; |
| 95 | |
| 96 | done: |
| 97 | kfree(buf); |
| 98 | return err; |
| 99 | } |