blob: 7ea5489e79773d36bf74bef168e737380665b04c [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 Hedberge41d8b42010-12-13 21:07:03 +020032static int cmd_status(struct sock *sk, u16 cmd, u8 status)
Johan Hedberg03811012010-12-08 00:21:06 +020033{
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)
Johan Hedberge41d8b42010-12-13 21:07:03 +020042 return -ENOMEM;
Johan Hedberg03811012010-12-08 00:21:06 +020043
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);
Johan Hedberge41d8b42010-12-13 21:07:03 +020055
56 return 0;
Johan Hedberg03811012010-12-08 00:21:06 +020057}
58
59int 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);
Johan Hedberge41d8b42010-12-13 21:07:03 +020092 err = cmd_status(sk, opcode, 0x01);
Johan Hedberg03811012010-12-08 00:21:06 +020093 break;
94 }
95
Johan Hedberge41d8b42010-12-13 21:07:03 +020096 if (err < 0)
97 goto done;
98
Johan Hedberg03811012010-12-08 00:21:06 +020099 err = msglen;
100
101done:
102 kfree(buf);
103 return err;
104}