aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/smp.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/bluetooth/smp.c')
-rw-r--r--net/bluetooth/smp.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
new file mode 100644
index 000000000000..aa0434f4aa1a
--- /dev/null
+++ b/net/bluetooth/smp.c
@@ -0,0 +1,146 @@
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
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#include <net/bluetooth/bluetooth.h>
24#include <net/bluetooth/hci_core.h>
25#include <net/bluetooth/l2cap.h>
26#include <net/bluetooth/smp.h>
27
28static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
29 u16 dlen, void *data)
30{
31 struct sk_buff *skb;
32 struct l2cap_hdr *lh;
33 int len;
34
35 len = L2CAP_HDR_SIZE + sizeof(code) + dlen;
36
37 if (len > conn->mtu)
38 return NULL;
39
40 skb = bt_skb_alloc(len, GFP_ATOMIC);
41 if (!skb)
42 return NULL;
43
44 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
45 lh->len = cpu_to_le16(sizeof(code) + dlen);
46 lh->cid = cpu_to_le16(L2CAP_CID_SMP);
47
48 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code));
49
50 memcpy(skb_put(skb, dlen), data, dlen);
51
52 return skb;
53}
54
55static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
56{
57 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
58
59 BT_DBG("code 0x%2.2x", code);
60
61 if (!skb)
62 return;
63
64 hci_send_acl(conn->hcon, skb, 0);
65}
66
67int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
68{
69 __u8 authreq;
70
71 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, conn->hcon, sec_level);
72
73 switch (sec_level) {
74 case BT_SECURITY_MEDIUM:
75 /* Encrypted, no MITM protection */
76 authreq = HCI_AT_NO_BONDING_MITM;
77 break;
78
79 case BT_SECURITY_HIGH:
80 /* Bonding, MITM protection */
81 authreq = HCI_AT_GENERAL_BONDING_MITM;
82 break;
83
84 case BT_SECURITY_LOW:
85 default:
86 return 1;
87 }
88
89 if (conn->hcon->link_mode & HCI_LM_MASTER) {
90 struct smp_cmd_pairing cp;
91 cp.io_capability = 0x00;
92 cp.oob_flag = 0x00;
93 cp.max_key_size = 16;
94 cp.init_key_dist = 0x00;
95 cp.resp_key_dist = 0x00;
96 cp.auth_req = authreq;
97 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
98 } else {
99 struct smp_cmd_security_req cp;
100 cp.auth_req = authreq;
101 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
102 }
103
104 return 0;
105}
106
107int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
108{
109 __u8 code = skb->data[0];
110 __u8 reason;
111 int err = 0;
112
113 skb_pull(skb, sizeof(code));
114
115 switch (code) {
116 case SMP_CMD_PAIRING_REQ:
117 reason = SMP_PAIRING_NOTSUPP;
118 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
119 &reason);
120 err = -EOPNOTSUPP;
121 break;
122
123 case SMP_CMD_PAIRING_FAIL:
124 break;
125
126 case SMP_CMD_PAIRING_RSP:
127 case SMP_CMD_PAIRING_CONFIRM:
128 case SMP_CMD_PAIRING_RANDOM:
129 case SMP_CMD_ENCRYPT_INFO:
130 case SMP_CMD_MASTER_IDENT:
131 case SMP_CMD_IDENT_INFO:
132 case SMP_CMD_IDENT_ADDR_INFO:
133 case SMP_CMD_SIGN_INFO:
134 case SMP_CMD_SECURITY_REQ:
135 default:
136 BT_DBG("Unknown command code 0x%2.2x", code);
137
138 reason = SMP_CMD_NOTSUPP;
139 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
140 &reason);
141 err = -EOPNOTSUPP;
142 }
143
144 kfree_skb(skb);
145 return err;
146}