blob: 3e24c0bf18e78a266e6f9a85710983e2ec0a195b [file] [log] [blame]
Johan Hedberg03811012010-12-08 00:21:06 +02001/*
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
Johan Hedberg02d98122010-12-13 21:07:04 +020032#define MGMT_VERSION 0
33#define MGMT_REVISION 1
34
35static int read_version(struct sock *sk)
36{
37 struct sk_buff *skb;
38 struct mgmt_hdr *hdr;
39 struct mgmt_ev_cmd_complete *ev;
40 struct mgmt_rp_read_version *rp;
41
42 BT_DBG("sock %p", sk);
43
44 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + sizeof(*rp), GFP_ATOMIC);
45 if (!skb)
46 return -ENOMEM;
47
48 hdr = (void *) skb_put(skb, sizeof(*hdr));
49 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
50 hdr->len = cpu_to_le16(sizeof(*ev) + sizeof(*rp));
51
52 ev = (void *) skb_put(skb, sizeof(*ev));
53 put_unaligned_le16(MGMT_OP_READ_VERSION, &ev->opcode);
54
55 rp = (void *) skb_put(skb, sizeof(*rp));
56 rp->version = MGMT_VERSION;
57 put_unaligned_le16(MGMT_REVISION, &rp->revision);
58
59 if (sock_queue_rcv_skb(sk, skb) < 0)
60 kfree_skb(skb);
61
62 return 0;
63}
64
Johan Hedberge41d8b42010-12-13 21:07:03 +020065static int cmd_status(struct sock *sk, u16 cmd, u8 status)
Johan Hedberg03811012010-12-08 00:21:06 +020066{
67 struct sk_buff *skb;
68 struct mgmt_hdr *hdr;
69 struct mgmt_ev_cmd_status *ev;
70
71 BT_DBG("sock %p", sk);
72
73 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC);
74 if (!skb)
Johan Hedberge41d8b42010-12-13 21:07:03 +020075 return -ENOMEM;
Johan Hedberg03811012010-12-08 00:21:06 +020076
77 hdr = (void *) skb_put(skb, sizeof(*hdr));
78
79 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
80 hdr->len = cpu_to_le16(sizeof(*ev));
81
82 ev = (void *) skb_put(skb, sizeof(*ev));
83 ev->status = status;
84 put_unaligned_le16(cmd, &ev->opcode);
85
86 if (sock_queue_rcv_skb(sk, skb) < 0)
87 kfree_skb(skb);
Johan Hedberge41d8b42010-12-13 21:07:03 +020088
89 return 0;
Johan Hedberg03811012010-12-08 00:21:06 +020090}
91
92int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
93{
94 unsigned char *buf;
95 struct mgmt_hdr *hdr;
96 u16 opcode, len;
97 int err;
98
99 BT_DBG("got %zu bytes", msglen);
100
101 if (msglen < sizeof(*hdr))
102 return -EINVAL;
103
104 buf = kmalloc(msglen, GFP_ATOMIC);
105 if (!buf)
106 return -ENOMEM;
107
108 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
109 err = -EFAULT;
110 goto done;
111 }
112
113 hdr = (struct mgmt_hdr *) buf;
114 opcode = get_unaligned_le16(&hdr->opcode);
115 len = get_unaligned_le16(&hdr->len);
116
117 if (len != msglen - sizeof(*hdr)) {
118 err = -EINVAL;
119 goto done;
120 }
121
122 switch (opcode) {
Johan Hedberg02d98122010-12-13 21:07:04 +0200123 case MGMT_OP_READ_VERSION:
124 err = read_version(sk);
125 break;
Johan Hedberg03811012010-12-08 00:21:06 +0200126 default:
127 BT_DBG("Unknown op %u", opcode);
Johan Hedberge41d8b42010-12-13 21:07:03 +0200128 err = cmd_status(sk, opcode, 0x01);
Johan Hedberg03811012010-12-08 00:21:06 +0200129 break;
130 }
131
Johan Hedberge41d8b42010-12-13 21:07:03 +0200132 if (err < 0)
133 goto done;
134
Johan Hedberg03811012010-12-08 00:21:06 +0200135 err = msglen;
136
137done:
138 kfree(buf);
139 return err;
140}