]>
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 | ||
8c520a59 | 27 | #include <linux/export.h> |
1da177e4 LT |
28 | |
29 | #include <net/bluetooth/bluetooth.h> | |
30 | #include <net/bluetooth/hci_core.h> | |
9740e49d | 31 | #include <net/bluetooth/a2mp.h> |
d8343f12 | 32 | #include <net/bluetooth/smp.h> |
1da177e4 | 33 | |
2dea632f FD |
34 | struct sco_param { |
35 | u16 pkt_type; | |
36 | u16 max_latency; | |
37 | }; | |
38 | ||
39 | static const struct sco_param sco_param_cvsd[] = { | |
40 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a }, /* S3 */ | |
41 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007 }, /* S2 */ | |
42 | { EDR_ESCO_MASK | ESCO_EV3, 0x0007 }, /* S1 */ | |
43 | { EDR_ESCO_MASK | ESCO_HV3, 0xffff }, /* D1 */ | |
44 | { EDR_ESCO_MASK | ESCO_HV1, 0xffff }, /* D0 */ | |
45 | }; | |
46 | ||
47 | static const struct sco_param sco_param_wideband[] = { | |
48 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d }, /* T2 */ | |
49 | { EDR_ESCO_MASK | ESCO_EV3, 0x0008 }, /* T1 */ | |
50 | }; | |
51 | ||
1aef8669 | 52 | static void hci_le_create_connection(struct hci_conn *conn) |
fcd89c09 VT |
53 | { |
54 | struct hci_dev *hdev = conn->hdev; | |
55 | struct hci_cp_le_create_conn cp; | |
56 | ||
57 | conn->state = BT_CONNECT; | |
a0c808b3 | 58 | conn->out = true; |
b92a6223 | 59 | conn->link_mode |= HCI_LM_MASTER; |
7b5c0d52 | 60 | conn->sec_level = BT_SECURITY_LOW; |
fcd89c09 VT |
61 | |
62 | memset(&cp, 0, sizeof(cp)); | |
82781e63 AE |
63 | cp.scan_interval = __constant_cpu_to_le16(0x0060); |
64 | cp.scan_window = __constant_cpu_to_le16(0x0030); | |
fcd89c09 | 65 | bacpy(&cp.peer_addr, &conn->dst); |
6d3ce0e7 | 66 | cp.peer_addr_type = conn->dst_type; |
82781e63 AE |
67 | cp.conn_interval_min = __constant_cpu_to_le16(0x0028); |
68 | cp.conn_interval_max = __constant_cpu_to_le16(0x0038); | |
69 | cp.supervision_timeout = __constant_cpu_to_le16(0x002a); | |
70 | cp.min_ce_len = __constant_cpu_to_le16(0x0000); | |
71 | cp.max_ce_len = __constant_cpu_to_le16(0x0000); | |
fcd89c09 VT |
72 | |
73 | hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); | |
74 | } | |
75 | ||
1aef8669 | 76 | static void hci_le_create_connection_cancel(struct hci_conn *conn) |
fcd89c09 VT |
77 | { |
78 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); | |
79 | } | |
80 | ||
1aef8669 | 81 | static void hci_acl_create_connection(struct hci_conn *conn) |
1da177e4 LT |
82 | { |
83 | struct hci_dev *hdev = conn->hdev; | |
84 | struct inquiry_entry *ie; | |
85 | struct hci_cp_create_conn cp; | |
86 | ||
42d2d87c | 87 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
88 | |
89 | conn->state = BT_CONNECT; | |
a0c808b3 | 90 | conn->out = true; |
a8746417 | 91 | |
1da177e4 LT |
92 | conn->link_mode = HCI_LM_MASTER; |
93 | ||
4c67bc74 MH |
94 | conn->attempt++; |
95 | ||
e4e8e37c MH |
96 | conn->link_policy = hdev->link_policy; |
97 | ||
1da177e4 LT |
98 | memset(&cp, 0, sizeof(cp)); |
99 | bacpy(&cp.bdaddr, &conn->dst); | |
100 | cp.pscan_rep_mode = 0x02; | |
101 | ||
70f23020 AE |
102 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
103 | if (ie) { | |
41a96212 MH |
104 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
105 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; | |
106 | cp.pscan_mode = ie->data.pscan_mode; | |
107 | cp.clock_offset = ie->data.clock_offset | | |
82781e63 | 108 | __constant_cpu_to_le16(0x8000); |
41a96212 MH |
109 | } |
110 | ||
1da177e4 | 111 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
58a681ef JH |
112 | if (ie->data.ssp_mode > 0) |
113 | set_bit(HCI_CONN_SSP_ENABLED, &conn->flags); | |
1da177e4 LT |
114 | } |
115 | ||
a8746417 | 116 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 117 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
b6a0dc82 | 118 | cp.role_switch = 0x01; |
1da177e4 | 119 | else |
b6a0dc82 | 120 | cp.role_switch = 0x00; |
4c67bc74 | 121 | |
a9de9248 | 122 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
1da177e4 LT |
123 | } |
124 | ||
1aef8669 | 125 | static void hci_acl_create_connection_cancel(struct hci_conn *conn) |
6ac59344 MH |
126 | { |
127 | struct hci_cp_create_conn_cancel cp; | |
128 | ||
38b3fef1 | 129 | BT_DBG("hcon %p", conn); |
6ac59344 | 130 | |
d095c1eb | 131 | if (conn->hdev->hci_ver < BLUETOOTH_VER_1_2) |
6ac59344 MH |
132 | return; |
133 | ||
134 | bacpy(&cp.bdaddr, &conn->dst); | |
a9de9248 | 135 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
6ac59344 MH |
136 | } |
137 | ||
93796fa6 CT |
138 | static void hci_reject_sco(struct hci_conn *conn) |
139 | { | |
140 | struct hci_cp_reject_sync_conn_req cp; | |
141 | ||
142 | cp.reason = HCI_ERROR_REMOTE_USER_TERM; | |
143 | bacpy(&cp.bdaddr, &conn->dst); | |
144 | ||
145 | hci_send_cmd(conn->hdev, HCI_OP_REJECT_SYNC_CONN_REQ, sizeof(cp), &cp); | |
146 | } | |
147 | ||
bed71748 | 148 | void hci_disconnect(struct hci_conn *conn, __u8 reason) |
1da177e4 LT |
149 | { |
150 | struct hci_cp_disconnect cp; | |
151 | ||
38b3fef1 | 152 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
153 | |
154 | conn->state = BT_DISCONN; | |
155 | ||
aca3192c | 156 | cp.handle = cpu_to_le16(conn->handle); |
1da177e4 | 157 | cp.reason = reason; |
a9de9248 | 158 | hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
1da177e4 LT |
159 | } |
160 | ||
53502d69 AE |
161 | static void hci_amp_disconn(struct hci_conn *conn, __u8 reason) |
162 | { | |
163 | struct hci_cp_disconn_phy_link cp; | |
164 | ||
165 | BT_DBG("hcon %p", conn); | |
166 | ||
167 | conn->state = BT_DISCONN; | |
168 | ||
169 | cp.phy_handle = HCI_PHY_HANDLE(conn->handle); | |
170 | cp.reason = reason; | |
171 | hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHY_LINK, | |
172 | sizeof(cp), &cp); | |
173 | } | |
174 | ||
57f5d0d1 | 175 | static void hci_add_sco(struct hci_conn *conn, __u16 handle) |
1da177e4 LT |
176 | { |
177 | struct hci_dev *hdev = conn->hdev; | |
178 | struct hci_cp_add_sco cp; | |
179 | ||
38b3fef1 | 180 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
181 | |
182 | conn->state = BT_CONNECT; | |
a0c808b3 | 183 | conn->out = true; |
1da177e4 | 184 | |
efc7688b MH |
185 | conn->attempt++; |
186 | ||
aca3192c | 187 | cp.handle = cpu_to_le16(handle); |
a8746417 | 188 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
1da177e4 | 189 | |
a9de9248 | 190 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
1da177e4 LT |
191 | } |
192 | ||
2dea632f | 193 | bool hci_setup_sync(struct hci_conn *conn, __u16 handle) |
b6a0dc82 MH |
194 | { |
195 | struct hci_dev *hdev = conn->hdev; | |
196 | struct hci_cp_setup_sync_conn cp; | |
2dea632f | 197 | const struct sco_param *param; |
b6a0dc82 | 198 | |
38b3fef1 | 199 | BT_DBG("hcon %p", conn); |
b6a0dc82 MH |
200 | |
201 | conn->state = BT_CONNECT; | |
a0c808b3 | 202 | conn->out = true; |
b6a0dc82 | 203 | |
efc7688b MH |
204 | conn->attempt++; |
205 | ||
b6a0dc82 | 206 | cp.handle = cpu_to_le16(handle); |
b6a0dc82 | 207 | |
82781e63 AE |
208 | cp.tx_bandwidth = __constant_cpu_to_le32(0x00001f40); |
209 | cp.rx_bandwidth = __constant_cpu_to_le32(0x00001f40); | |
10c62ddc FD |
210 | cp.voice_setting = cpu_to_le16(conn->setting); |
211 | ||
212 | switch (conn->setting & SCO_AIRMODE_MASK) { | |
213 | case SCO_AIRMODE_TRANSP: | |
2dea632f FD |
214 | if (conn->attempt > ARRAY_SIZE(sco_param_wideband)) |
215 | return false; | |
10c62ddc | 216 | cp.retrans_effort = 0x02; |
2dea632f | 217 | param = &sco_param_wideband[conn->attempt - 1]; |
10c62ddc FD |
218 | break; |
219 | case SCO_AIRMODE_CVSD: | |
2dea632f FD |
220 | if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) |
221 | return false; | |
222 | cp.retrans_effort = 0x01; | |
223 | param = &sco_param_cvsd[conn->attempt - 1]; | |
10c62ddc | 224 | break; |
2dea632f FD |
225 | default: |
226 | return false; | |
10c62ddc | 227 | } |
b6a0dc82 | 228 | |
2dea632f FD |
229 | cp.pkt_type = __cpu_to_le16(param->pkt_type); |
230 | cp.max_latency = __cpu_to_le16(param->max_latency); | |
231 | ||
232 | if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) | |
233 | return false; | |
234 | ||
235 | return true; | |
b6a0dc82 MH |
236 | } |
237 | ||
2ce603eb | 238 | void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, |
5974e4c4 | 239 | u16 latency, u16 to_multiplier) |
2ce603eb CT |
240 | { |
241 | struct hci_cp_le_conn_update cp; | |
242 | struct hci_dev *hdev = conn->hdev; | |
243 | ||
244 | memset(&cp, 0, sizeof(cp)); | |
245 | ||
246 | cp.handle = cpu_to_le16(conn->handle); | |
247 | cp.conn_interval_min = cpu_to_le16(min); | |
248 | cp.conn_interval_max = cpu_to_le16(max); | |
249 | cp.conn_latency = cpu_to_le16(latency); | |
250 | cp.supervision_timeout = cpu_to_le16(to_multiplier); | |
82781e63 AE |
251 | cp.min_ce_len = __constant_cpu_to_le16(0x0001); |
252 | cp.max_ce_len = __constant_cpu_to_le16(0x0001); | |
2ce603eb CT |
253 | |
254 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); | |
255 | } | |
2ce603eb | 256 | |
a7a595f6 | 257 | void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8], |
5974e4c4 | 258 | __u8 ltk[16]) |
a7a595f6 VCG |
259 | { |
260 | struct hci_dev *hdev = conn->hdev; | |
261 | struct hci_cp_le_start_enc cp; | |
262 | ||
38b3fef1 | 263 | BT_DBG("hcon %p", conn); |
a7a595f6 VCG |
264 | |
265 | memset(&cp, 0, sizeof(cp)); | |
266 | ||
267 | cp.handle = cpu_to_le16(conn->handle); | |
268 | memcpy(cp.ltk, ltk, sizeof(cp.ltk)); | |
269 | cp.ediv = ediv; | |
51beabdf | 270 | memcpy(cp.rand, rand, sizeof(cp.rand)); |
a7a595f6 VCG |
271 | |
272 | hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); | |
273 | } | |
a7a595f6 | 274 | |
e73439d8 MH |
275 | /* Device _must_ be locked */ |
276 | void hci_sco_setup(struct hci_conn *conn, __u8 status) | |
277 | { | |
278 | struct hci_conn *sco = conn->link; | |
279 | ||
e73439d8 MH |
280 | if (!sco) |
281 | return; | |
282 | ||
38b3fef1 AE |
283 | BT_DBG("hcon %p", conn); |
284 | ||
e73439d8 MH |
285 | if (!status) { |
286 | if (lmp_esco_capable(conn->hdev)) | |
287 | hci_setup_sync(sco, conn->handle); | |
288 | else | |
289 | hci_add_sco(sco, conn->handle); | |
290 | } else { | |
291 | hci_proto_connect_cfm(sco, status); | |
292 | hci_conn_del(sco); | |
293 | } | |
294 | } | |
295 | ||
53502d69 AE |
296 | static void hci_conn_disconnect(struct hci_conn *conn) |
297 | { | |
298 | __u8 reason = hci_proto_disconn_ind(conn); | |
299 | ||
300 | switch (conn->type) { | |
53502d69 AE |
301 | case AMP_LINK: |
302 | hci_amp_disconn(conn, reason); | |
303 | break; | |
4c02e2d4 | 304 | default: |
bed71748 | 305 | hci_disconnect(conn, reason); |
4c02e2d4 | 306 | break; |
53502d69 AE |
307 | } |
308 | } | |
309 | ||
19c40e3b | 310 | static void hci_conn_timeout(struct work_struct *work) |
1da177e4 | 311 | { |
19c40e3b | 312 | struct hci_conn *conn = container_of(work, struct hci_conn, |
5974e4c4 | 313 | disc_work.work); |
1da177e4 | 314 | |
38b3fef1 | 315 | BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); |
1da177e4 LT |
316 | |
317 | if (atomic_read(&conn->refcnt)) | |
318 | return; | |
319 | ||
6ac59344 MH |
320 | switch (conn->state) { |
321 | case BT_CONNECT: | |
769be974 | 322 | case BT_CONNECT2: |
fcd89c09 VT |
323 | if (conn->out) { |
324 | if (conn->type == ACL_LINK) | |
1aef8669 | 325 | hci_acl_create_connection_cancel(conn); |
fcd89c09 | 326 | else if (conn->type == LE_LINK) |
1aef8669 | 327 | hci_le_create_connection_cancel(conn); |
93796fa6 CT |
328 | } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { |
329 | hci_reject_sco(conn); | |
fcd89c09 | 330 | } |
6ac59344 | 331 | break; |
769be974 | 332 | case BT_CONFIG: |
8e87d142 | 333 | case BT_CONNECTED: |
53502d69 | 334 | hci_conn_disconnect(conn); |
6ac59344 MH |
335 | break; |
336 | default: | |
1da177e4 | 337 | conn->state = BT_CLOSED; |
6ac59344 MH |
338 | break; |
339 | } | |
1da177e4 LT |
340 | } |
341 | ||
416dc94b GP |
342 | /* Enter sniff mode */ |
343 | static void hci_conn_enter_sniff_mode(struct hci_conn *conn) | |
344 | { | |
345 | struct hci_dev *hdev = conn->hdev; | |
346 | ||
38b3fef1 | 347 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
416dc94b GP |
348 | |
349 | if (test_bit(HCI_RAW, &hdev->flags)) | |
350 | return; | |
351 | ||
352 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) | |
353 | return; | |
354 | ||
355 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) | |
356 | return; | |
357 | ||
358 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { | |
359 | struct hci_cp_sniff_subrate cp; | |
360 | cp.handle = cpu_to_le16(conn->handle); | |
82781e63 AE |
361 | cp.max_latency = __constant_cpu_to_le16(0); |
362 | cp.min_remote_timeout = __constant_cpu_to_le16(0); | |
363 | cp.min_local_timeout = __constant_cpu_to_le16(0); | |
416dc94b GP |
364 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
365 | } | |
366 | ||
51a8efd7 | 367 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
416dc94b GP |
368 | struct hci_cp_sniff_mode cp; |
369 | cp.handle = cpu_to_le16(conn->handle); | |
370 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); | |
371 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); | |
82781e63 AE |
372 | cp.attempt = __constant_cpu_to_le16(4); |
373 | cp.timeout = __constant_cpu_to_le16(1); | |
416dc94b GP |
374 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
375 | } | |
376 | } | |
377 | ||
04837f64 | 378 | static void hci_conn_idle(unsigned long arg) |
1da177e4 | 379 | { |
04837f64 MH |
380 | struct hci_conn *conn = (void *) arg; |
381 | ||
38b3fef1 | 382 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
04837f64 MH |
383 | |
384 | hci_conn_enter_sniff_mode(conn); | |
1da177e4 LT |
385 | } |
386 | ||
9f61656a JH |
387 | static void hci_conn_auto_accept(unsigned long arg) |
388 | { | |
389 | struct hci_conn *conn = (void *) arg; | |
390 | struct hci_dev *hdev = conn->hdev; | |
391 | ||
9f61656a | 392 | hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), |
5974e4c4 | 393 | &conn->dst); |
9f61656a JH |
394 | } |
395 | ||
1da177e4 LT |
396 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst) |
397 | { | |
398 | struct hci_conn *conn; | |
399 | ||
6ed93dc6 | 400 | BT_DBG("%s dst %pMR", hdev->name, dst); |
1da177e4 | 401 | |
cb601d7e | 402 | conn = kzalloc(sizeof(struct hci_conn), GFP_KERNEL); |
04837f64 | 403 | if (!conn) |
1da177e4 | 404 | return NULL; |
1da177e4 LT |
405 | |
406 | bacpy(&conn->dst, dst); | |
a8746417 MH |
407 | conn->hdev = hdev; |
408 | conn->type = type; | |
409 | conn->mode = HCI_CM_ACTIVE; | |
410 | conn->state = BT_OPEN; | |
93f19c9f | 411 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
17fa4b9d | 412 | conn->io_capability = hdev->io_capability; |
a9583556 | 413 | conn->remote_auth = 0xff; |
13d39315 | 414 | conn->key_type = 0xff; |
1da177e4 | 415 | |
58a681ef | 416 | set_bit(HCI_CONN_POWER_SAVE, &conn->flags); |
052b30b0 | 417 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
04837f64 | 418 | |
a8746417 MH |
419 | switch (type) { |
420 | case ACL_LINK: | |
421 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; | |
422 | break; | |
423 | case SCO_LINK: | |
424 | if (lmp_esco_capable(hdev)) | |
efc7688b MH |
425 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
426 | (hdev->esco_type & EDR_ESCO_MASK); | |
a8746417 MH |
427 | else |
428 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; | |
429 | break; | |
430 | case ESCO_LINK: | |
efc7688b | 431 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
a8746417 MH |
432 | break; |
433 | } | |
434 | ||
1da177e4 | 435 | skb_queue_head_init(&conn->data_q); |
04837f64 | 436 | |
70c1f20b | 437 | INIT_LIST_HEAD(&conn->chan_list); |
73d80deb | 438 | |
19c40e3b | 439 | INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); |
b24b8a24 | 440 | setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); |
9f61656a | 441 | setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept, |
5974e4c4 | 442 | (unsigned long) conn); |
1da177e4 LT |
443 | |
444 | atomic_set(&conn->refcnt, 0); | |
445 | ||
446 | hci_dev_hold(hdev); | |
447 | ||
1da177e4 | 448 | hci_conn_hash_add(hdev, conn); |
3c54711c | 449 | if (hdev->notify) |
1da177e4 LT |
450 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
451 | ||
a67e899c MH |
452 | hci_conn_init_sysfs(conn); |
453 | ||
1da177e4 LT |
454 | return conn; |
455 | } | |
456 | ||
457 | int hci_conn_del(struct hci_conn *conn) | |
458 | { | |
459 | struct hci_dev *hdev = conn->hdev; | |
460 | ||
38b3fef1 | 461 | BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); |
1da177e4 | 462 | |
04837f64 MH |
463 | del_timer(&conn->idle_timer); |
464 | ||
19c40e3b | 465 | cancel_delayed_work_sync(&conn->disc_work); |
1da177e4 | 466 | |
9f61656a JH |
467 | del_timer(&conn->auto_accept_timer); |
468 | ||
5b7f9909 | 469 | if (conn->type == ACL_LINK) { |
1da177e4 LT |
470 | struct hci_conn *sco = conn->link; |
471 | if (sco) | |
472 | sco->link = NULL; | |
473 | ||
474 | /* Unacked frames */ | |
475 | hdev->acl_cnt += conn->sent; | |
6ed58ec5 VT |
476 | } else if (conn->type == LE_LINK) { |
477 | if (hdev->le_pkts) | |
478 | hdev->le_cnt += conn->sent; | |
479 | else | |
480 | hdev->acl_cnt += conn->sent; | |
5b7f9909 MH |
481 | } else { |
482 | struct hci_conn *acl = conn->link; | |
483 | if (acl) { | |
484 | acl->link = NULL; | |
76a68ba0 | 485 | hci_conn_drop(acl); |
5b7f9909 | 486 | } |
1da177e4 LT |
487 | } |
488 | ||
2c33c06a | 489 | hci_chan_list_flush(conn); |
73d80deb | 490 | |
9740e49d AE |
491 | if (conn->amp_mgr) |
492 | amp_mgr_put(conn->amp_mgr); | |
493 | ||
1da177e4 | 494 | hci_conn_hash_del(hdev, conn); |
3c54711c | 495 | if (hdev->notify) |
1da177e4 | 496 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
7d0db0a3 | 497 | |
1da177e4 | 498 | skb_queue_purge(&conn->data_q); |
1da177e4 | 499 | |
fc225c3f | 500 | hci_conn_del_sysfs(conn); |
2ae9a6be | 501 | |
384943ec MH |
502 | hci_dev_put(hdev); |
503 | ||
8d12356f | 504 | hci_conn_put(conn); |
163f4dab | 505 | |
1da177e4 LT |
506 | return 0; |
507 | } | |
508 | ||
509 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) | |
510 | { | |
511 | int use_src = bacmp(src, BDADDR_ANY); | |
8035ded4 | 512 | struct hci_dev *hdev = NULL, *d; |
1da177e4 | 513 | |
6ed93dc6 | 514 | BT_DBG("%pMR -> %pMR", src, dst); |
1da177e4 | 515 | |
f20d09d5 | 516 | read_lock(&hci_dev_list_lock); |
1da177e4 | 517 | |
8035ded4 | 518 | list_for_each_entry(d, &hci_dev_list, list) { |
8fc9ced3 | 519 | if (!test_bit(HCI_UP, &d->flags) || |
d300fa9b | 520 | test_bit(HCI_RAW, &d->flags) || |
af750e94 | 521 | test_bit(HCI_USER_CHANNEL, &d->dev_flags) || |
d300fa9b | 522 | d->dev_type != HCI_BREDR) |
1da177e4 LT |
523 | continue; |
524 | ||
8e87d142 | 525 | /* Simple routing: |
1da177e4 LT |
526 | * No source address - find interface with bdaddr != dst |
527 | * Source address - find interface with bdaddr == src | |
528 | */ | |
529 | ||
530 | if (use_src) { | |
531 | if (!bacmp(&d->bdaddr, src)) { | |
532 | hdev = d; break; | |
533 | } | |
534 | } else { | |
535 | if (bacmp(&d->bdaddr, dst)) { | |
536 | hdev = d; break; | |
537 | } | |
538 | } | |
539 | } | |
540 | ||
541 | if (hdev) | |
542 | hdev = hci_dev_hold(hdev); | |
543 | ||
f20d09d5 | 544 | read_unlock(&hci_dev_list_lock); |
1da177e4 LT |
545 | return hdev; |
546 | } | |
547 | EXPORT_SYMBOL(hci_get_route); | |
548 | ||
d04aef4c VCG |
549 | static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, |
550 | u8 dst_type, u8 sec_level, u8 auth_type) | |
1da177e4 | 551 | { |
fcd89c09 | 552 | struct hci_conn *le; |
1da177e4 | 553 | |
f1550478 JH |
554 | if (test_bit(HCI_LE_PERIPHERAL, &hdev->flags)) |
555 | return ERR_PTR(-ENOTSUPP); | |
556 | ||
d04aef4c VCG |
557 | le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
558 | if (!le) { | |
559 | le = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); | |
560 | if (le) | |
561 | return ERR_PTR(-EBUSY); | |
1da177e4 | 562 | |
d04aef4c VCG |
563 | le = hci_conn_add(hdev, LE_LINK, dst); |
564 | if (!le) | |
565 | return ERR_PTR(-ENOMEM); | |
dfc94dbd | 566 | |
d04aef4c VCG |
567 | le->dst_type = bdaddr_to_le(dst_type); |
568 | hci_le_create_connection(le); | |
569 | } | |
9f0caeb1 | 570 | |
d04aef4c VCG |
571 | le->pending_sec_level = sec_level; |
572 | le->auth_type = auth_type; | |
eda42b50 | 573 | |
d04aef4c | 574 | hci_conn_hold(le); |
fcd89c09 | 575 | |
d04aef4c VCG |
576 | return le; |
577 | } | |
fcd89c09 | 578 | |
db474275 VCG |
579 | static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, |
580 | u8 sec_level, u8 auth_type) | |
1da177e4 LT |
581 | { |
582 | struct hci_conn *acl; | |
fcd89c09 | 583 | |
56f87901 JH |
584 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
585 | return ERR_PTR(-ENOTSUPP); | |
586 | ||
70f23020 AE |
587 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
588 | if (!acl) { | |
589 | acl = hci_conn_add(hdev, ACL_LINK, dst); | |
590 | if (!acl) | |
48c7aba9 | 591 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
592 | } |
593 | ||
594 | hci_conn_hold(acl); | |
595 | ||
09ab6f4c | 596 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
765c2a96 JH |
597 | acl->sec_level = BT_SECURITY_LOW; |
598 | acl->pending_sec_level = sec_level; | |
09ab6f4c | 599 | acl->auth_type = auth_type; |
1aef8669 | 600 | hci_acl_create_connection(acl); |
09ab6f4c | 601 | } |
1da177e4 | 602 | |
db474275 VCG |
603 | return acl; |
604 | } | |
605 | ||
10c62ddc FD |
606 | struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, |
607 | __u16 setting) | |
db474275 VCG |
608 | { |
609 | struct hci_conn *acl; | |
610 | struct hci_conn *sco; | |
611 | ||
e660ed6c | 612 | acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); |
db474275 | 613 | if (IS_ERR(acl)) |
5b7f9909 | 614 | return acl; |
1da177e4 | 615 | |
70f23020 AE |
616 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
617 | if (!sco) { | |
618 | sco = hci_conn_add(hdev, type, dst); | |
619 | if (!sco) { | |
76a68ba0 | 620 | hci_conn_drop(acl); |
48c7aba9 | 621 | return ERR_PTR(-ENOMEM); |
1da177e4 | 622 | } |
5b7f9909 | 623 | } |
1da177e4 | 624 | |
5b7f9909 MH |
625 | acl->link = sco; |
626 | sco->link = acl; | |
1da177e4 | 627 | |
5b7f9909 | 628 | hci_conn_hold(sco); |
1da177e4 | 629 | |
10c62ddc FD |
630 | sco->setting = setting; |
631 | ||
5b7f9909 | 632 | if (acl->state == BT_CONNECTED && |
5974e4c4 | 633 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
58a681ef | 634 | set_bit(HCI_CONN_POWER_SAVE, &acl->flags); |
14b12d0b | 635 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
c390216b | 636 | |
51a8efd7 | 637 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { |
e73439d8 | 638 | /* defer SCO setup until mode change completed */ |
51a8efd7 | 639 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); |
e73439d8 MH |
640 | return sco; |
641 | } | |
642 | ||
643 | hci_sco_setup(acl, 0x00); | |
b6a0dc82 | 644 | } |
5b7f9909 MH |
645 | |
646 | return sco; | |
1da177e4 | 647 | } |
1da177e4 | 648 | |
b7d839bf VCG |
649 | /* Create SCO, ACL or LE connection. */ |
650 | struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, | |
651 | __u8 dst_type, __u8 sec_level, __u8 auth_type) | |
652 | { | |
6ed93dc6 | 653 | BT_DBG("%s dst %pMR type 0x%x", hdev->name, dst, type); |
b7d839bf | 654 | |
4cd2d983 VCG |
655 | switch (type) { |
656 | case LE_LINK: | |
b7d839bf | 657 | return hci_connect_le(hdev, dst, dst_type, sec_level, auth_type); |
4cd2d983 | 658 | case ACL_LINK: |
b7d839bf | 659 | return hci_connect_acl(hdev, dst, sec_level, auth_type); |
4cd2d983 | 660 | } |
b7d839bf | 661 | |
4cd2d983 | 662 | return ERR_PTR(-EINVAL); |
b7d839bf VCG |
663 | } |
664 | ||
e7c29cb1 MH |
665 | /* Check link security requirement */ |
666 | int hci_conn_check_link_mode(struct hci_conn *conn) | |
667 | { | |
38b3fef1 | 668 | BT_DBG("hcon %p", conn); |
e7c29cb1 | 669 | |
aa64a8b5 | 670 | if (hci_conn_ssp_enabled(conn) && !(conn->link_mode & HCI_LM_ENCRYPT)) |
e7c29cb1 MH |
671 | return 0; |
672 | ||
673 | return 1; | |
674 | } | |
e7c29cb1 | 675 | |
1da177e4 | 676 | /* Authenticate remote device */ |
0684e5f9 | 677 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 | 678 | { |
38b3fef1 | 679 | BT_DBG("hcon %p", conn); |
1da177e4 | 680 | |
765c2a96 JH |
681 | if (conn->pending_sec_level > sec_level) |
682 | sec_level = conn->pending_sec_level; | |
683 | ||
96a31833 | 684 | if (sec_level > conn->sec_level) |
765c2a96 | 685 | conn->pending_sec_level = sec_level; |
96a31833 | 686 | else if (conn->link_mode & HCI_LM_AUTH) |
1da177e4 LT |
687 | return 1; |
688 | ||
65cf686e JH |
689 | /* Make sure we preserve an existing MITM requirement*/ |
690 | auth_type |= (conn->auth_type & 0x01); | |
691 | ||
96a31833 MH |
692 | conn->auth_type = auth_type; |
693 | ||
51a8efd7 | 694 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
1da177e4 | 695 | struct hci_cp_auth_requested cp; |
b7d05bad PH |
696 | |
697 | /* encrypt must be pending if auth is also pending */ | |
698 | set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); | |
699 | ||
aca3192c | 700 | cp.handle = cpu_to_le16(conn->handle); |
40be492f | 701 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
5974e4c4 | 702 | sizeof(cp), &cp); |
19f8def0 | 703 | if (conn->key_type != 0xff) |
51a8efd7 | 704 | set_bit(HCI_CONN_REAUTH_PEND, &conn->flags); |
1da177e4 | 705 | } |
8c1b2355 | 706 | |
1da177e4 LT |
707 | return 0; |
708 | } | |
1da177e4 | 709 | |
13d39315 WR |
710 | /* Encrypt the the link */ |
711 | static void hci_conn_encrypt(struct hci_conn *conn) | |
712 | { | |
38b3fef1 | 713 | BT_DBG("hcon %p", conn); |
13d39315 | 714 | |
51a8efd7 | 715 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { |
13d39315 WR |
716 | struct hci_cp_set_conn_encrypt cp; |
717 | cp.handle = cpu_to_le16(conn->handle); | |
718 | cp.encrypt = 0x01; | |
719 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), | |
5974e4c4 | 720 | &cp); |
13d39315 WR |
721 | } |
722 | } | |
723 | ||
8c1b2355 | 724 | /* Enable security */ |
0684e5f9 | 725 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
1da177e4 | 726 | { |
38b3fef1 | 727 | BT_DBG("hcon %p", conn); |
1da177e4 | 728 | |
d8343f12 VCG |
729 | if (conn->type == LE_LINK) |
730 | return smp_conn_security(conn, sec_level); | |
731 | ||
13d39315 | 732 | /* For sdp we don't need the link key. */ |
8c1b2355 MH |
733 | if (sec_level == BT_SECURITY_SDP) |
734 | return 1; | |
735 | ||
13d39315 WR |
736 | /* For non 2.1 devices and low security level we don't need the link |
737 | key. */ | |
aa64a8b5 | 738 | if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) |
3fdca1e1 | 739 | return 1; |
8c1b2355 | 740 | |
13d39315 WR |
741 | /* For other security levels we need the link key. */ |
742 | if (!(conn->link_mode & HCI_LM_AUTH)) | |
743 | goto auth; | |
744 | ||
745 | /* An authenticated combination key has sufficient security for any | |
746 | security level. */ | |
747 | if (conn->key_type == HCI_LK_AUTH_COMBINATION) | |
748 | goto encrypt; | |
749 | ||
750 | /* An unauthenticated combination key has sufficient security for | |
751 | security level 1 and 2. */ | |
752 | if (conn->key_type == HCI_LK_UNAUTH_COMBINATION && | |
5974e4c4 | 753 | (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW)) |
13d39315 WR |
754 | goto encrypt; |
755 | ||
756 | /* A combination key has always sufficient security for the security | |
757 | levels 1 or 2. High security level requires the combination key | |
758 | is generated using maximum PIN code length (16). | |
759 | For pre 2.1 units. */ | |
760 | if (conn->key_type == HCI_LK_COMBINATION && | |
5974e4c4 | 761 | (sec_level != BT_SECURITY_HIGH || conn->pin_length == 16)) |
13d39315 WR |
762 | goto encrypt; |
763 | ||
764 | auth: | |
51a8efd7 | 765 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) |
1da177e4 LT |
766 | return 0; |
767 | ||
6fdf658c LAD |
768 | if (!hci_conn_auth(conn, sec_level, auth_type)) |
769 | return 0; | |
13d39315 WR |
770 | |
771 | encrypt: | |
772 | if (conn->link_mode & HCI_LM_ENCRYPT) | |
773 | return 1; | |
8c1b2355 | 774 | |
13d39315 | 775 | hci_conn_encrypt(conn); |
1da177e4 LT |
776 | return 0; |
777 | } | |
8c1b2355 | 778 | EXPORT_SYMBOL(hci_conn_security); |
1da177e4 | 779 | |
b3b1b061 WR |
780 | /* Check secure link requirement */ |
781 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) | |
782 | { | |
38b3fef1 | 783 | BT_DBG("hcon %p", conn); |
b3b1b061 WR |
784 | |
785 | if (sec_level != BT_SECURITY_HIGH) | |
786 | return 1; /* Accept if non-secure is required */ | |
787 | ||
ef4177e2 | 788 | if (conn->sec_level == BT_SECURITY_HIGH) |
b3b1b061 WR |
789 | return 1; |
790 | ||
791 | return 0; /* Reject not secure link */ | |
792 | } | |
793 | EXPORT_SYMBOL(hci_conn_check_secure); | |
794 | ||
1da177e4 LT |
795 | /* Change link key */ |
796 | int hci_conn_change_link_key(struct hci_conn *conn) | |
797 | { | |
38b3fef1 | 798 | BT_DBG("hcon %p", conn); |
1da177e4 | 799 | |
51a8efd7 | 800 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
1da177e4 | 801 | struct hci_cp_change_conn_link_key cp; |
aca3192c | 802 | cp.handle = cpu_to_le16(conn->handle); |
40be492f | 803 | hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY, |
5974e4c4 | 804 | sizeof(cp), &cp); |
1da177e4 | 805 | } |
8c1b2355 | 806 | |
1da177e4 LT |
807 | return 0; |
808 | } | |
1da177e4 LT |
809 | |
810 | /* Switch role */ | |
8c1b2355 | 811 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
1da177e4 | 812 | { |
38b3fef1 | 813 | BT_DBG("hcon %p", conn); |
1da177e4 LT |
814 | |
815 | if (!role && conn->link_mode & HCI_LM_MASTER) | |
816 | return 1; | |
817 | ||
51a8efd7 | 818 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { |
1da177e4 LT |
819 | struct hci_cp_switch_role cp; |
820 | bacpy(&cp.bdaddr, &conn->dst); | |
821 | cp.role = role; | |
a9de9248 | 822 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
1da177e4 | 823 | } |
8c1b2355 | 824 | |
1da177e4 LT |
825 | return 0; |
826 | } | |
827 | EXPORT_SYMBOL(hci_conn_switch_role); | |
828 | ||
04837f64 | 829 | /* Enter active mode */ |
14b12d0b | 830 | void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) |
04837f64 MH |
831 | { |
832 | struct hci_dev *hdev = conn->hdev; | |
833 | ||
38b3fef1 | 834 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
04837f64 MH |
835 | |
836 | if (test_bit(HCI_RAW, &hdev->flags)) | |
837 | return; | |
838 | ||
14b12d0b JG |
839 | if (conn->mode != HCI_CM_SNIFF) |
840 | goto timer; | |
841 | ||
58a681ef | 842 | if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) |
04837f64 MH |
843 | goto timer; |
844 | ||
51a8efd7 | 845 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
04837f64 | 846 | struct hci_cp_exit_sniff_mode cp; |
aca3192c | 847 | cp.handle = cpu_to_le16(conn->handle); |
a9de9248 | 848 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
04837f64 MH |
849 | } |
850 | ||
851 | timer: | |
852 | if (hdev->idle_timeout > 0) | |
853 | mod_timer(&conn->idle_timer, | |
5974e4c4 | 854 | jiffies + msecs_to_jiffies(hdev->idle_timeout)); |
04837f64 MH |
855 | } |
856 | ||
1da177e4 LT |
857 | /* Drop all connection on the device */ |
858 | void hci_conn_hash_flush(struct hci_dev *hdev) | |
859 | { | |
860 | struct hci_conn_hash *h = &hdev->conn_hash; | |
3c4e0df0 | 861 | struct hci_conn *c, *n; |
1da177e4 LT |
862 | |
863 | BT_DBG("hdev %s", hdev->name); | |
864 | ||
3c4e0df0 | 865 | list_for_each_entry_safe(c, n, &h->list, list) { |
1da177e4 LT |
866 | c->state = BT_CLOSED; |
867 | ||
9f5a0d7b | 868 | hci_proto_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); |
1da177e4 LT |
869 | hci_conn_del(c); |
870 | } | |
871 | } | |
872 | ||
a9de9248 MH |
873 | /* Check pending connect attempts */ |
874 | void hci_conn_check_pending(struct hci_dev *hdev) | |
875 | { | |
876 | struct hci_conn *conn; | |
877 | ||
878 | BT_DBG("hdev %s", hdev->name); | |
879 | ||
880 | hci_dev_lock(hdev); | |
881 | ||
882 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); | |
883 | if (conn) | |
1aef8669 | 884 | hci_acl_create_connection(conn); |
a9de9248 MH |
885 | |
886 | hci_dev_unlock(hdev); | |
887 | } | |
888 | ||
1da177e4 LT |
889 | int hci_get_conn_list(void __user *arg) |
890 | { | |
fc5fef61 | 891 | struct hci_conn *c; |
1da177e4 LT |
892 | struct hci_conn_list_req req, *cl; |
893 | struct hci_conn_info *ci; | |
894 | struct hci_dev *hdev; | |
1da177e4 LT |
895 | int n = 0, size, err; |
896 | ||
897 | if (copy_from_user(&req, arg, sizeof(req))) | |
898 | return -EFAULT; | |
899 | ||
900 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) | |
901 | return -EINVAL; | |
902 | ||
903 | size = sizeof(req) + req.conn_num * sizeof(*ci); | |
904 | ||
70f23020 AE |
905 | cl = kmalloc(size, GFP_KERNEL); |
906 | if (!cl) | |
1da177e4 LT |
907 | return -ENOMEM; |
908 | ||
70f23020 AE |
909 | hdev = hci_dev_get(req.dev_id); |
910 | if (!hdev) { | |
1da177e4 LT |
911 | kfree(cl); |
912 | return -ENODEV; | |
913 | } | |
914 | ||
915 | ci = cl->conn_info; | |
916 | ||
09fd0de5 | 917 | hci_dev_lock(hdev); |
8035ded4 | 918 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
1da177e4 LT |
919 | bacpy(&(ci + n)->bdaddr, &c->dst); |
920 | (ci + n)->handle = c->handle; | |
921 | (ci + n)->type = c->type; | |
922 | (ci + n)->out = c->out; | |
923 | (ci + n)->state = c->state; | |
924 | (ci + n)->link_mode = c->link_mode; | |
925 | if (++n >= req.conn_num) | |
926 | break; | |
927 | } | |
09fd0de5 | 928 | hci_dev_unlock(hdev); |
1da177e4 LT |
929 | |
930 | cl->dev_id = hdev->id; | |
931 | cl->conn_num = n; | |
932 | size = sizeof(req) + n * sizeof(*ci); | |
933 | ||
934 | hci_dev_put(hdev); | |
935 | ||
936 | err = copy_to_user(arg, cl, size); | |
937 | kfree(cl); | |
938 | ||
939 | return err ? -EFAULT : 0; | |
940 | } | |
941 | ||
942 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) | |
943 | { | |
944 | struct hci_conn_info_req req; | |
945 | struct hci_conn_info ci; | |
946 | struct hci_conn *conn; | |
947 | char __user *ptr = arg + sizeof(req); | |
948 | ||
949 | if (copy_from_user(&req, arg, sizeof(req))) | |
950 | return -EFAULT; | |
951 | ||
09fd0de5 | 952 | hci_dev_lock(hdev); |
1da177e4 LT |
953 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
954 | if (conn) { | |
955 | bacpy(&ci.bdaddr, &conn->dst); | |
956 | ci.handle = conn->handle; | |
957 | ci.type = conn->type; | |
958 | ci.out = conn->out; | |
959 | ci.state = conn->state; | |
960 | ci.link_mode = conn->link_mode; | |
961 | } | |
09fd0de5 | 962 | hci_dev_unlock(hdev); |
1da177e4 LT |
963 | |
964 | if (!conn) | |
965 | return -ENOENT; | |
966 | ||
967 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; | |
968 | } | |
40be492f MH |
969 | |
970 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) | |
971 | { | |
972 | struct hci_auth_info_req req; | |
973 | struct hci_conn *conn; | |
974 | ||
975 | if (copy_from_user(&req, arg, sizeof(req))) | |
976 | return -EFAULT; | |
977 | ||
09fd0de5 | 978 | hci_dev_lock(hdev); |
40be492f MH |
979 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
980 | if (conn) | |
981 | req.type = conn->auth_type; | |
09fd0de5 | 982 | hci_dev_unlock(hdev); |
40be492f MH |
983 | |
984 | if (!conn) | |
985 | return -ENOENT; | |
986 | ||
987 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; | |
988 | } | |
73d80deb LAD |
989 | |
990 | struct hci_chan *hci_chan_create(struct hci_conn *conn) | |
991 | { | |
992 | struct hci_dev *hdev = conn->hdev; | |
993 | struct hci_chan *chan; | |
994 | ||
38b3fef1 | 995 | BT_DBG("%s hcon %p", hdev->name, conn); |
73d80deb | 996 | |
75d7735c | 997 | chan = kzalloc(sizeof(struct hci_chan), GFP_KERNEL); |
73d80deb LAD |
998 | if (!chan) |
999 | return NULL; | |
1000 | ||
1001 | chan->conn = conn; | |
1002 | skb_queue_head_init(&chan->data_q); | |
168df8e5 | 1003 | chan->state = BT_CONNECTED; |
73d80deb | 1004 | |
8192edef | 1005 | list_add_rcu(&chan->list, &conn->chan_list); |
73d80deb LAD |
1006 | |
1007 | return chan; | |
1008 | } | |
1009 | ||
9472007c | 1010 | void hci_chan_del(struct hci_chan *chan) |
73d80deb LAD |
1011 | { |
1012 | struct hci_conn *conn = chan->conn; | |
1013 | struct hci_dev *hdev = conn->hdev; | |
1014 | ||
38b3fef1 | 1015 | BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); |
73d80deb | 1016 | |
8192edef GP |
1017 | list_del_rcu(&chan->list); |
1018 | ||
1019 | synchronize_rcu(); | |
73d80deb | 1020 | |
76a68ba0 | 1021 | hci_conn_drop(conn); |
e9b02748 | 1022 | |
73d80deb LAD |
1023 | skb_queue_purge(&chan->data_q); |
1024 | kfree(chan); | |
73d80deb LAD |
1025 | } |
1026 | ||
2c33c06a | 1027 | void hci_chan_list_flush(struct hci_conn *conn) |
73d80deb | 1028 | { |
2a5a5ec6 | 1029 | struct hci_chan *chan, *n; |
73d80deb | 1030 | |
38b3fef1 | 1031 | BT_DBG("hcon %p", conn); |
73d80deb | 1032 | |
2a5a5ec6 | 1033 | list_for_each_entry_safe(chan, n, &conn->chan_list, list) |
73d80deb LAD |
1034 | hci_chan_del(chan); |
1035 | } | |
42c4e53e AE |
1036 | |
1037 | static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon, | |
1038 | __u16 handle) | |
1039 | { | |
1040 | struct hci_chan *hchan; | |
1041 | ||
1042 | list_for_each_entry(hchan, &hcon->chan_list, list) { | |
1043 | if (hchan->handle == handle) | |
1044 | return hchan; | |
1045 | } | |
1046 | ||
1047 | return NULL; | |
1048 | } | |
1049 | ||
1050 | struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle) | |
1051 | { | |
1052 | struct hci_conn_hash *h = &hdev->conn_hash; | |
1053 | struct hci_conn *hcon; | |
1054 | struct hci_chan *hchan = NULL; | |
1055 | ||
1056 | rcu_read_lock(); | |
1057 | ||
1058 | list_for_each_entry_rcu(hcon, &h->list, list) { | |
1059 | hchan = __hci_chan_lookup_handle(hcon, handle); | |
1060 | if (hchan) | |
1061 | break; | |
1062 | } | |
1063 | ||
1064 | rcu_read_unlock(); | |
1065 | ||
1066 | return hchan; | |
1067 | } |