]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/bluetooth/mgmt.c
7ea5489e79773d36bf74bef168e737380665b04c
[mv-sheeva.git] / net / bluetooth / mgmt.c
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 int 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 -ENOMEM;
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         return 0;
57 }
58
59 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
60 {
61         unsigned char *buf;
62         struct mgmt_hdr *hdr;
63         u16 opcode, len;
64         int err;
65
66         BT_DBG("got %zu bytes", msglen);
67
68         if (msglen < sizeof(*hdr))
69                 return -EINVAL;
70
71         buf = kmalloc(msglen, GFP_ATOMIC);
72         if (!buf)
73                 return -ENOMEM;
74
75         if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
76                 err = -EFAULT;
77                 goto done;
78         }
79
80         hdr = (struct mgmt_hdr *) buf;
81         opcode = get_unaligned_le16(&hdr->opcode);
82         len = get_unaligned_le16(&hdr->len);
83
84         if (len != msglen - sizeof(*hdr)) {
85                 err = -EINVAL;
86                 goto done;
87         }
88
89         switch (opcode) {
90         default:
91                 BT_DBG("Unknown op %u", opcode);
92                 err = cmd_status(sk, opcode, 0x01);
93                 break;
94         }
95
96         if (err < 0)
97                 goto done;
98
99         err = msglen;
100
101 done:
102         kfree(buf);
103         return err;
104 }