Commit | Line | Data |
---|---|---|
8e87d142 | 1 | /* |
1da177e4 | 2 | BlueZ - Bluetooth protocol stack for Linux |
2d0a0346 | 3 | Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. |
1da177e4 LT |
4 | |
5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License version 2 as | |
9 | published by the Free Software Foundation; | |
10 | ||
11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. | |
14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY | |
8e87d142 YH |
15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
1da177e4 LT |
18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 | ||
8e87d142 YH |
20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
22 | SOFTWARE IS DISCLAIMED. |
23 | */ | |
24 | ||
25 | /* Bluetooth HCI connection handling. */ | |
26 | ||
1da177e4 LT |
27 | #include <linux/module.h> |
28 | ||
29 | #include <linux/types.h> | |
30 | #include <linux/errno.h> | |
31 | #include <linux/kernel.h> | |
1da177e4 LT |
32 | #include <linux/slab.h> |
33 | #include <linux/poll.h> | |
34 | #include <linux/fcntl.h> | |
35 | #include <linux/init.h> | |
36 | #include <linux/skbuff.h> | |
37 | #include <linux/interrupt.h> | |
38 | #include <linux/notifier.h> | |
39 | #include <net/sock.h> | |
40 | ||
41 | #include <asm/system.h> | |
70f23020 | 42 | #include <linux/uaccess.h> |
1da177e4 LT |
43 | #include <asm/unaligned.h> |
44 | ||
45 | #include <net/bluetooth/bluetooth.h> | |
46 | #include <net/bluetooth/hci_core.h> | |
47 | ||
fcd89c09 VT |
48 | static void hci_le_connect(struct hci_conn *conn) |
49 | { | |
50 | struct hci_dev *hdev = conn->hdev; | |
51 | struct hci_cp_le_create_conn cp; | |
52 | ||
53 | conn->state = BT_CONNECT; | |
54 | conn->out = 1; | |
b92a6223 | 55 | conn->link_mode |= HCI_LM_MASTER; |
fcd89c09 VT |
56 | |
57 | memset(&cp, 0, sizeof(cp)); | |
58 | cp.scan_interval = cpu_to_le16(0x0004); | |
59 | cp.scan_window = cpu_to_le16(0x0004); | |
60 | bacpy(&cp.peer_addr, &conn->dst); | |
61 | cp.conn_interval_min = cpu_to_le16(0x0008); | |
62 | cp.conn_interval_max = cpu_to_le16(0x0100); | |
63 | cp.supervision_timeout = cpu_to_le16(0x0064); | |
64 | cp.min_ce_len = cpu_to_le16(0x0001); | |
65 | cp.max_ce_len = cpu_to_le16(0x0001); | |
66 | ||
67 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); | |
68 | } | |
69 | ||
70 | static void hci_le_connect_cancel(struct hci_conn *conn) | |
71 | { | |
72 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); | |
73 | } | |
74 | ||
4c67bc74 | 75 | void hci_acl_connect(struct hci_conn *conn) |
1da177e4 LT |
76 | { |
77 | struct hci_dev *hdev = conn->hdev; | |
78 | struct inquiry_entry *ie; | |
79 | struct hci_cp_create_conn cp; | |
80 | ||
81 | BT_DBG("%p", conn); | |
82 | ||
83 | conn->state = BT_CONNECT; | |
a8746417 MH |
84 | conn->out = 1; |
85 | ||
1da177e4 LT |
86 | conn->link_mode = HCI_LM_MASTER; |
87 | ||
4c67bc74 MH |
88 | conn->attempt++; |
89 | ||
e4e8e37c MH |
90 | conn->link_policy = hdev->link_policy; |
91 | ||
1da177e4 LT |
92 | memset(&cp, 0, sizeof(cp)); |
93 | bacpy(&cp.bdaddr, &conn->dst); | |
94 | cp.pscan_rep_mode = 0x02; | |
95 | ||
70f23020 AE |
96 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
97 | if (ie) { | |
41a96212 MH |
98 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
99 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; | |
100 | cp.pscan_mode = ie->data.pscan_mode; | |
101 | cp.clock_offset = ie->data.clock_offset | | |
102 | cpu_to_le16(0x8000); | |
103 | } | |
104 | ||
1da177e4 | 105 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
41a96212 | 106 | conn->ssp_mode = ie->data.ssp_mode; |
1da177e4 LT |
107 | } |
108 | ||
a8746417 | 109 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 110 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
b6a0dc82 | 111 | cp.role_switch = 0x01; |
1da177e4 | 112 | else |
b6a0dc82 | 113 | cp.role_switch = 0x00; |
4c67bc74 | 114 | |
a9de9248 | 115 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
1da177e4 LT |
116 | } |
117 | ||
6ac59344 MH |
118 | static void hci_acl_connect_cancel(struct hci_conn *conn) |
119 | { | |
120 | struct hci_cp_create_conn_cancel cp; | |
121 | ||
122 | BT_DBG("%p", conn); | |
123 | ||
124 | if (conn->hdev->hci_ver < 2) | |
125 | return; | |
126 | ||
127 | bacpy(&cp.bdaddr, &conn->dst); | |
a9de9248 | 128 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
6ac59344 MH |
129 | } |
130 | ||
1da177e4 LT |
131 | void hci_acl_disconn(struct hci_conn *conn, __u8 reason) |
132 | { | |
133 | struct hci_cp_disconnect cp; | |
134 | ||
135 | BT_DBG("%p", conn); | |
136 | ||
137 | conn->state = BT_DISCONN; | |
138 | ||
aca3192c | 139 | cp.handle = cpu_to_le16(conn->handle); |
1da177e4 | 140 | cp.reason = reason; |
a9de9248 | 141 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
1da177e4 LT |
142 | } |
143 | ||
144 | void hci_add_sco(struct hci_conn *conn, __u16 handle) | |
145 | { | |
146 | struct hci_dev *hdev = conn->hdev; | |
147 | struct hci_cp_add_sco cp; | |
148 | ||
149 | BT_DBG("%p", conn); | |
150 | ||
151 | conn->state = BT_CONNECT; | |
152 | conn->out = 1; | |
153 | ||
efc7688b MH |
154 | conn->attempt++; |
155 | ||
aca3192c | 156 | cp.handle = cpu_to_le16(handle); |
a8746417 | 157 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 158 | |
a9de9248 | 159 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
1da177e4 LT |
160 | } |
161 | ||
b6a0dc82 MH |
162 | void hci_setup_sync(struct hci_conn *conn, __u16 handle) |
163 | { | |
164 | struct hci_dev *hdev = conn->hdev; | |
165 | struct hci_cp_setup_sync_conn cp; | |
166 | ||
167 | BT_DBG("%p", conn); | |
168 | ||
169 | conn->state = BT_CONNECT; | |
170 | conn->out = 1; | |
171 | ||
efc7688b MH |
172 | conn->attempt++; |
173 | ||
b6a0dc82 | 174 | cp.handle = cpu_to_le16(handle); |
a8746417 | 175 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
b6a0dc82 MH |
176 | |
177 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); | |
178 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); | |
179 | cp.max_latency = cpu_to_le16(0xffff); | |
180 | cp.voice_setting = cpu_to_le16(hdev->voice_setting); | |
181 | cp.retrans_effort = 0xff; | |
182 | ||
183 | hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp); | |
184 | } | |
185 | ||
2ce603eb CT |
186 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
187 | u16 latency, u16 to_multiplier) | |
188 | { | |
189 | struct hci_cp_le_conn_update cp; | |
190 | struct hci_dev *hdev = conn->hdev; | |
191 | ||
192 | memset(&cp, 0, sizeof(cp)); | |
193 | ||
194 | cp.handle = cpu_to_le16(conn->handle); | |
195 | cp.conn_interval_min = cpu_to_le16(min); | |
196 | cp.conn_interval_max = cpu_to_le16(max); | |
197 | cp.conn_latency = cpu_to_le16(latency); | |
198 | cp.supervision_timeout = cpu_to_le16(to_multiplier); | |
199 | cp.min_ce_len = cpu_to_le16(0x0001); | |
200 | cp.max_ce_len = cpu_to_le16(0x0001); | |
201 | ||
202 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); | |
203 | } | |
204 | EXPORT_SYMBOL(hci_le_conn_update); | |
205 | ||
e73439d8 MH |
206 | /* Device _must_ be locked */ |
207 | void hci_sco_setup(struct hci_conn *conn, __u8 status) | |
208 | { | |
209 | struct hci_conn *sco = conn->link; | |
210 | ||
211 | BT_DBG("%p", conn); | |
212 | ||
213 | if (!sco) | |
214 | return; | |
215 | ||
216 | if (!status) { | |
217 | if (lmp_esco_capable(conn->hdev)) | |
218 | hci_setup_sync(sco, conn->handle); | |
219 | else | |
220 | hci_add_sco(sco, conn->handle); | |
221 | } else { | |
222 | hci_proto_connect_cfm(sco, status); | |
223 | hci_conn_del(sco); | |
224 | } | |
225 | } | |
226 | ||
1da177e4 LT |
227 | static void hci_conn_timeout(unsigned long arg) |
228 | { | |
04837f64 MH |
229 | struct hci_conn *conn = (void *) arg; |
230 | struct hci_dev *hdev = conn->hdev; | |
2950f21a | 231 | __u8 reason; |
1da177e4 LT |
232 | |
233 | BT_DBG("conn %p state %d", conn, conn->state); | |
234 | ||
235 | if (atomic_read(&conn->refcnt)) | |
236 | return; | |
237 | ||
238 | hci_dev_lock(hdev); | |
6ac59344 MH |
239 | |
240 | switch (conn->state) { | |
241 | case BT_CONNECT: | |
769be974 | 242 | case BT_CONNECT2: |
fcd89c09 VT |
243 | if (conn->out) { |
244 | if (conn->type == ACL_LINK) | |
245 | hci_acl_connect_cancel(conn); | |
246 | else if (conn->type == LE_LINK) | |
247 | hci_le_connect_cancel(conn); | |
248 | } | |
6ac59344 | 249 | break; |
769be974 | 250 | case BT_CONFIG: |
8e87d142 | 251 | case BT_CONNECTED: |
2950f21a MH |
252 | reason = hci_proto_disconn_ind(conn); |
253 | hci_acl_disconn(conn, reason); | |
6ac59344 MH |
254 | break; |
255 | default: | |
1da177e4 | 256 | conn->state = BT_CLOSED; |
6ac59344 MH |
257 | break; |
258 | } | |
259 | ||
1da177e4 | 260 | hci_dev_unlock(hdev); |
1da177e4 LT |
261 | } |
262 | ||
04837f64 | 263 | static void hci_conn_idle(unsigned long arg) |
1da177e4 | 264 | { |
04837f64 MH |
265 | struct hci_conn *conn = (void *) arg; |
266 | ||
267 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
268 | ||
269 | hci_conn_enter_sniff_mode(conn); | |
1da177e4 LT |
270 | } |
271 | ||
272 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) | |
273 | { | |
274 | struct hci_conn *conn; | |
275 | ||
276 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); | |
277 | ||
04837f64 MH |
278 | conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC); |
279 | if (!conn) | |
1da177e4 | 280 | return NULL; |
1da177e4 LT |
281 | |
282 | bacpy(&conn->dst, dst); | |
a8746417 MH |
283 | conn->hdev = hdev; |
284 | conn->type = type; | |
285 | conn->mode = HCI_CM_ACTIVE; | |
286 | conn->state = BT_OPEN; | |
93f19c9f | 287 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
17fa4b9d | 288 | conn->io_capability = hdev->io_capability; |
1da177e4 | 289 | |
04837f64 | 290 | conn->power_save = 1; |
052b30b0 | 291 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
04837f64 | 292 | |
a8746417 MH |
293 | switch (type) { |
294 | case ACL_LINK: | |
295 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; | |
296 | break; | |
297 | case SCO_LINK: | |
298 | if (lmp_esco_capable(hdev)) | |
efc7688b MH |
299 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
300 | (hdev->esco_type & EDR_ESCO_MASK); | |
a8746417 MH |
301 | else |
302 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; | |
303 | break; | |
304 | case ESCO_LINK: | |
efc7688b | 305 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
a8746417 MH |
306 | break; |
307 | } | |
308 | ||
1da177e4 | 309 | skb_queue_head_init(&conn->data_q); |
04837f64 | 310 | |
b24b8a24 PE |
311 | setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn); |
312 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); | |
1da177e4 LT |
313 | |
314 | atomic_set(&conn->refcnt, 0); | |
315 | ||
316 | hci_dev_hold(hdev); | |
317 | ||
318 | tasklet_disable(&hdev->tx_task); | |
319 | ||
320 | hci_conn_hash_add(hdev, conn); | |
321 | if (hdev->notify) | |
322 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); | |
323 | ||
9eba32b8 MH |
324 | atomic_set(&conn->devref, 0); |
325 | ||
a67e899c MH |
326 | hci_conn_init_sysfs(conn); |
327 | ||
1da177e4 LT |
328 | tasklet_enable(&hdev->tx_task); |
329 | ||
330 | return conn; | |
331 | } | |
332 | ||
333 | int hci_conn_del(struct hci_conn *conn) | |
334 | { | |
335 | struct hci_dev *hdev = conn->hdev; | |
336 | ||
337 | BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle); | |
338 | ||
04837f64 MH |
339 | del_timer(&conn->idle_timer); |
340 | ||
341 | del_timer(&conn->disc_timer); | |
1da177e4 | 342 | |
5b7f9909 | 343 | if (conn->type == ACL_LINK) { |
1da177e4 LT |
344 | struct hci_conn *sco = conn->link; |
345 | if (sco) | |
346 | sco->link = NULL; | |
347 | ||
348 | /* Unacked frames */ | |
349 | hdev->acl_cnt += conn->sent; | |
6ed58ec5 VT |
350 | } else if (conn->type == LE_LINK) { |
351 | if (hdev->le_pkts) | |
352 | hdev->le_cnt += conn->sent; | |
353 | else | |
354 | hdev->acl_cnt += conn->sent; | |
5b7f9909 MH |
355 | } else { |
356 | struct hci_conn *acl = conn->link; | |
357 | if (acl) { | |
358 | acl->link = NULL; | |
359 | hci_conn_put(acl); | |
360 | } | |
1da177e4 LT |
361 | } |
362 | ||
363 | tasklet_disable(&hdev->tx_task); | |
7d0db0a3 | 364 | |
1da177e4 LT |
365 | hci_conn_hash_del(hdev, conn); |
366 | if (hdev->notify) | |
367 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); | |
7d0db0a3 | 368 | |
1da177e4 | 369 | tasklet_enable(&hdev->tx_task); |
7d0db0a3 | 370 | |
1da177e4 | 371 | skb_queue_purge(&conn->data_q); |
1da177e4 | 372 | |
9eba32b8 | 373 | hci_conn_put_device(conn); |
2ae9a6be | 374 | |
384943ec MH |
375 | hci_dev_put(hdev); |
376 | ||
1da177e4 LT |
377 | return 0; |
378 | } | |
379 | ||
380 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) | |
381 | { | |
382 | int use_src = bacmp(src, BDADDR_ANY); | |
383 | struct hci_dev *hdev = NULL; | |
384 | struct list_head *p; | |
385 | ||
386 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); | |
387 | ||
388 | read_lock_bh(&hci_dev_list_lock); | |
389 | ||
390 | list_for_each(p, &hci_dev_list) { | |
391 | struct hci_dev *d = list_entry(p, struct hci_dev, list); | |
392 | ||
393 | if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags)) | |
394 | continue; | |
395 | ||
8e87d142 | 396 | /* Simple routing: |
1da177e4 LT |
397 | * No source address - find interface with bdaddr != dst |
398 | * Source address - find interface with bdaddr == src | |
399 | */ | |
400 | ||
401 | if (use_src) { | |
402 | if (!bacmp(&d->bdaddr, src)) { | |
403 | hdev = d; break; | |
404 | } | |
405 | } else { | |
406 | if (bacmp(&d->bdaddr, dst)) { | |
407 | hdev = d; break; | |
408 | } | |
409 | } | |
410 | } | |
411 | ||
412 | if (hdev) | |
413 | hdev = hci_dev_hold(hdev); | |
414 | ||
415 | read_unlock_bh(&hci_dev_list_lock); | |
416 | return hdev; | |
417 | } | |
418 | EXPORT_SYMBOL(hci_get_route); | |
419 | ||
fcd89c09 | 420 | /* Create SCO, ACL or LE connection. |
1da177e4 | 421 | * Device _must_ be locked */ |
8c1b2355 | 422 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
423 | { |
424 | struct hci_conn *acl; | |
5b7f9909 | 425 | struct hci_conn *sco; |
fcd89c09 | 426 | struct hci_conn *le; |
1da177e4 LT |
427 | |
428 | BT_DBG("%s dst %s", hdev->name, batostr(dst)); | |
429 | ||
fcd89c09 VT |
430 | if (type == LE_LINK) { |
431 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); | |
432 | if (!le) | |
433 | le = hci_conn_add(hdev, LE_LINK, dst); | |
434 | if (!le) | |
435 | return NULL; | |
436 | if (le->state == BT_OPEN) | |
437 | hci_le_connect(le); | |
438 | ||
439 | hci_conn_hold(le); | |
440 | ||
441 | return le; | |
442 | } | |
443 | ||
70f23020 AE |
444 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
445 | if (!acl) { | |
446 | acl = hci_conn_add(hdev, ACL_LINK, dst); | |
447 | if (!acl) | |
1da177e4 LT |
448 | return NULL; |
449 | } | |
450 | ||
451 | hci_conn_hold(acl); | |
452 | ||
09ab6f4c | 453 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
765c2a96 JH |
454 | acl->sec_level = BT_SECURITY_LOW; |
455 | acl->pending_sec_level = sec_level; | |
09ab6f4c | 456 | acl->auth_type = auth_type; |
1da177e4 | 457 | hci_acl_connect(acl); |
09ab6f4c | 458 | } |
1da177e4 | 459 | |
5b7f9909 MH |
460 | if (type == ACL_LINK) |
461 | return acl; | |
1da177e4 | 462 | |
70f23020 AE |
463 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
464 | if (!sco) { | |
465 | sco = hci_conn_add(hdev, type, dst); | |
466 | if (!sco) { | |
5b7f9909 MH |
467 | hci_conn_put(acl); |
468 | return NULL; | |
1da177e4 | 469 | } |
5b7f9909 | 470 | } |
1da177e4 | 471 | |
5b7f9909 MH |
472 | acl->link = sco; |
473 | sco->link = acl; | |
1da177e4 | 474 | |
5b7f9909 | 475 | hci_conn_hold(sco); |
1da177e4 | 476 | |
5b7f9909 | 477 | if (acl->state == BT_CONNECTED && |
b6a0dc82 | 478 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
c390216b NP |
479 | acl->power_save = 1; |
480 | hci_conn_enter_active_mode(acl); | |
481 | ||
e73439d8 MH |
482 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) { |
483 | /* defer SCO setup until mode change completed */ | |
484 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend); | |
485 | return sco; | |
486 | } | |
487 | ||
488 | hci_sco_setup(acl, 0x00); | |
b6a0dc82 | 489 | } |
5b7f9909 MH |
490 | |
491 | return sco; | |
1da177e4 LT |
492 | } |
493 | EXPORT_SYMBOL(hci_connect); | |
494 | ||
e7c29cb1 MH |
495 | /* Check link security requirement */ |
496 | int hci_conn_check_link_mode(struct hci_conn *conn) | |
497 | { | |
498 | BT_DBG("conn %p", conn); | |
499 | ||
500 | if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 && | |
501 | !(conn->link_mode & HCI_LM_ENCRYPT)) | |
502 | return 0; | |
503 | ||
504 | return 1; | |
505 | } | |
506 | EXPORT_SYMBOL(hci_conn_check_link_mode); | |
507 | ||
1da177e4 | 508 | /* Authenticate remote device */ |
0684e5f9 | 509 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
510 | { |
511 | BT_DBG("conn %p", conn); | |
512 | ||
765c2a96 JH |
513 | if (conn->pending_sec_level > sec_level) |
514 | sec_level = conn->pending_sec_level; | |
515 | ||
96a31833 | 516 | if (sec_level > conn->sec_level) |
765c2a96 | 517 | conn->pending_sec_level = sec_level; |
96a31833 | 518 | else if (conn->link_mode & HCI_LM_AUTH) |
1da177e4 LT |
519 | return 1; |
520 | ||
65cf686e JH |
521 | /* Make sure we preserve an existing MITM requirement*/ |
522 | auth_type |= (conn->auth_type & 0x01); | |
523 | ||
96a31833 MH |
524 | conn->auth_type = auth_type; |
525 | ||
1da177e4 LT |
526 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
527 | struct hci_cp_auth_requested cp; | |
aca3192c | 528 | cp.handle = cpu_to_le16(conn->handle); |
40be492f MH |
529 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
530 | sizeof(cp), &cp); | |
1da177e4 | 531 | } |
8c1b2355 | 532 | |
1da177e4 LT |
533 | return 0; |
534 | } | |
1da177e4 | 535 | |
8c1b2355 | 536 | /* Enable security */ |
0684e5f9 | 537 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 LT |
538 | { |
539 | BT_DBG("conn %p", conn); | |
540 | ||
8c1b2355 MH |
541 | if (sec_level == BT_SECURITY_SDP) |
542 | return 1; | |
543 | ||
3fdca1e1 MH |
544 | if (sec_level == BT_SECURITY_LOW && |
545 | (!conn->ssp_mode || !conn->hdev->ssp_mode)) | |
546 | return 1; | |
8c1b2355 | 547 | |
1da177e4 | 548 | if (conn->link_mode & HCI_LM_ENCRYPT) |
0684e5f9 | 549 | return hci_conn_auth(conn, sec_level, auth_type); |
1da177e4 LT |
550 | |
551 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) | |
552 | return 0; | |
553 | ||
0684e5f9 | 554 | if (hci_conn_auth(conn, sec_level, auth_type)) { |
1da177e4 | 555 | struct hci_cp_set_conn_encrypt cp; |
aca3192c | 556 | cp.handle = cpu_to_le16(conn->handle); |
8e87d142 | 557 | cp.encrypt = 1; |
40be492f MH |
558 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, |
559 | sizeof(cp), &cp); | |
1da177e4 | 560 | } |
8c1b2355 | 561 | |
1da177e4 LT |
562 | return 0; |
563 | } | |
8c1b2355 | 564 | EXPORT_SYMBOL(hci_conn_security); |
1da177e4 LT |
565 | |
566 | /* Change link key */ | |
567 | int hci_conn_change_link_key(struct hci_conn *conn) | |
568 | { | |
569 | BT_DBG("conn %p", conn); | |
570 | ||
571 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { | |
572 | struct hci_cp_change_conn_link_key cp; | |
aca3192c | 573 | cp.handle = cpu_to_le16(conn->handle); |
40be492f MH |
574 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
575 | sizeof(cp), &cp); | |
1da177e4 | 576 | } |
8c1b2355 | 577 | |
1da177e4 LT |
578 | return 0; |
579 | } | |
580 | EXPORT_SYMBOL(hci_conn_change_link_key); | |
581 | ||
582 | /* Switch role */ | |
8c1b2355 | 583 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
1da177e4 LT |
584 | { |
585 | BT_DBG("conn %p", conn); | |
586 | ||
587 | if (!role && conn->link_mode & HCI_LM_MASTER) | |
588 | return 1; | |
589 | ||
590 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) { | |
591 | struct hci_cp_switch_role cp; | |
592 | bacpy(&cp.bdaddr, &conn->dst); | |
593 | cp.role = role; | |
a9de9248 | 594 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
1da177e4 | 595 | } |
8c1b2355 | 596 | |
1da177e4 LT |
597 | return 0; |
598 | } | |
599 | EXPORT_SYMBOL(hci_conn_switch_role); | |
600 | ||
04837f64 MH |
601 | /* Enter active mode */ |
602 | void hci_conn_enter_active_mode(struct hci_conn *conn) | |
603 | { | |
604 | struct hci_dev *hdev = conn->hdev; | |
605 | ||
606 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
607 | ||
608 | if (test_bit(HCI_RAW, &hdev->flags)) | |
609 | return; | |
610 | ||
611 | if (conn->mode != HCI_CM_SNIFF || !conn->power_save) | |
612 | goto timer; | |
613 | ||
614 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { | |
615 | struct hci_cp_exit_sniff_mode cp; | |
aca3192c | 616 | cp.handle = cpu_to_le16(conn->handle); |
a9de9248 | 617 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
618 | } |
619 | ||
620 | timer: | |
621 | if (hdev->idle_timeout > 0) | |
622 | mod_timer(&conn->idle_timer, | |
623 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); | |
624 | } | |
625 | ||
626 | /* Enter sniff mode */ | |
627 | void hci_conn_enter_sniff_mode(struct hci_conn *conn) | |
628 | { | |
629 | struct hci_dev *hdev = conn->hdev; | |
630 | ||
631 | BT_DBG("conn %p mode %d", conn, conn->mode); | |
632 | ||
633 | if (test_bit(HCI_RAW, &hdev->flags)) | |
634 | return; | |
635 | ||
636 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) | |
637 | return; | |
638 | ||
639 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) | |
640 | return; | |
641 | ||
642 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { | |
643 | struct hci_cp_sniff_subrate cp; | |
aca3192c YH |
644 | cp.handle = cpu_to_le16(conn->handle); |
645 | cp.max_latency = cpu_to_le16(0); | |
646 | cp.min_remote_timeout = cpu_to_le16(0); | |
647 | cp.min_local_timeout = cpu_to_le16(0); | |
a9de9248 | 648 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
04837f64 MH |
649 | } |
650 | ||
651 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { | |
652 | struct hci_cp_sniff_mode cp; | |
aca3192c YH |
653 | cp.handle = cpu_to_le16(conn->handle); |
654 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); | |
655 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); | |
656 | cp.attempt = cpu_to_le16(4); | |
657 | cp.timeout = cpu_to_le16(1); | |
a9de9248 | 658 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
659 | } |
660 | } | |
661 | ||
1da177e4 LT |
662 | /* Drop all connection on the device */ |
663 | void hci_conn_hash_flush(struct hci_dev *hdev) | |
664 | { | |
665 | struct hci_conn_hash *h = &hdev->conn_hash; | |
666 | struct list_head *p; | |
667 | ||
668 | BT_DBG("hdev %s", hdev->name); | |
669 | ||
670 | p = h->list.next; | |
671 | while (p != &h->list) { | |
672 | struct hci_conn *c; | |
673 | ||
674 | c = list_entry(p, struct hci_conn, list); | |
675 | p = p->next; | |
676 | ||
677 | c->state = BT_CLOSED; | |
678 | ||
2950f21a | 679 | hci_proto_disconn_cfm(c, 0x16); |
1da177e4 LT |
680 | hci_conn_del(c); |
681 | } | |
682 | } | |
683 | ||
a9de9248 MH |
684 | /* Check pending connect attempts */ |
685 | void hci_conn_check_pending(struct hci_dev *hdev) | |
686 | { | |
687 | struct hci_conn *conn; | |
688 | ||
689 | BT_DBG("hdev %s", hdev->name); | |
690 | ||
691 | hci_dev_lock(hdev); | |
692 | ||
693 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); | |
694 | if (conn) | |
695 | hci_acl_connect(conn); | |
696 | ||
697 | hci_dev_unlock(hdev); | |
698 | } | |
699 | ||
9eba32b8 MH |
700 | void hci_conn_hold_device(struct hci_conn *conn) |
701 | { | |
702 | atomic_inc(&conn->devref); | |
703 | } | |
704 | EXPORT_SYMBOL(hci_conn_hold_device); | |
705 | ||
706 | void hci_conn_put_device(struct hci_conn *conn) | |
707 | { | |
708 | if (atomic_dec_and_test(&conn->devref)) | |
709 | hci_conn_del_sysfs(conn); | |
710 | } | |
711 | EXPORT_SYMBOL(hci_conn_put_device); | |
712 | ||
1da177e4 LT |
713 | int hci_get_conn_list(void __user *arg) |
714 | { | |
715 | struct hci_conn_list_req req, *cl; | |
716 | struct hci_conn_info *ci; | |
717 | struct hci_dev *hdev; | |
718 | struct list_head *p; | |
719 | int n = 0, size, err; | |
720 | ||
721 | if (copy_from_user(&req, arg, sizeof(req))) | |
722 | return -EFAULT; | |
723 | ||
724 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) | |
725 | return -EINVAL; | |
726 | ||
727 | size = sizeof(req) + req.conn_num * sizeof(*ci); | |
728 | ||
70f23020 AE |
729 | cl = kmalloc(size, GFP_KERNEL); |
730 | if (!cl) | |
1da177e4 LT |
731 | return -ENOMEM; |
732 | ||
70f23020 AE |
733 | hdev = hci_dev_get(req.dev_id); |
734 | if (!hdev) { | |
1da177e4 LT |
735 | kfree(cl); |
736 | return -ENODEV; | |
737 | } | |
738 | ||
739 | ci = cl->conn_info; | |
740 | ||
741 | hci_dev_lock_bh(hdev); | |
742 | list_for_each(p, &hdev->conn_hash.list) { | |
743 | register struct hci_conn *c; | |
744 | c = list_entry(p, struct hci_conn, list); | |
745 | ||
746 | bacpy(&(ci + n)->bdaddr, &c->dst); | |
747 | (ci + n)->handle = c->handle; | |
748 | (ci + n)->type = c->type; | |
749 | (ci + n)->out = c->out; | |
750 | (ci + n)->state = c->state; | |
751 | (ci + n)->link_mode = c->link_mode; | |
752 | if (++n >= req.conn_num) | |
753 | break; | |
754 | } | |
755 | hci_dev_unlock_bh(hdev); | |
756 | ||
757 | cl->dev_id = hdev->id; | |
758 | cl->conn_num = n; | |
759 | size = sizeof(req) + n * sizeof(*ci); | |
760 | ||
761 | hci_dev_put(hdev); | |
762 | ||
763 | err = copy_to_user(arg, cl, size); | |
764 | kfree(cl); | |
765 | ||
766 | return err ? -EFAULT : 0; | |
767 | } | |
768 | ||
769 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) | |
770 | { | |
771 | struct hci_conn_info_req req; | |
772 | struct hci_conn_info ci; | |
773 | struct hci_conn *conn; | |
774 | char __user *ptr = arg + sizeof(req); | |
775 | ||
776 | if (copy_from_user(&req, arg, sizeof(req))) | |
777 | return -EFAULT; | |
778 | ||
779 | hci_dev_lock_bh(hdev); | |
780 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); | |
781 | if (conn) { | |
782 | bacpy(&ci.bdaddr, &conn->dst); | |
783 | ci.handle = conn->handle; | |
784 | ci.type = conn->type; | |
785 | ci.out = conn->out; | |
786 | ci.state = conn->state; | |
787 | ci.link_mode = conn->link_mode; | |
788 | } | |
789 | hci_dev_unlock_bh(hdev); | |
790 | ||
791 | if (!conn) | |
792 | return -ENOENT; | |
793 | ||
794 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; | |
795 | } | |
40be492f MH |
796 | |
797 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) | |
798 | { | |
799 | struct hci_auth_info_req req; | |
800 | struct hci_conn *conn; | |
801 | ||
802 | if (copy_from_user(&req, arg, sizeof(req))) | |
803 | return -EFAULT; | |
804 | ||
805 | hci_dev_lock_bh(hdev); | |
806 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); | |
807 | if (conn) | |
808 | req.type = conn->auth_type; | |
809 | hci_dev_unlock_bh(hdev); | |
810 | ||
811 | if (!conn) | |
812 | return -ENOENT; | |
813 | ||
814 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; | |
815 | } |