xref: /linux/net/bluetooth/hci_conn.c (revision f3be0c984ecbcb82b0bec408022c4ef738cb3843)
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4    Copyright 2023-2024 NXP
5 
6    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License version 2 as
10    published by the Free Software Foundation;
11 
12    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 
21    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23    SOFTWARE IS DISCLAIMED.
24 */
25 
26 /* Bluetooth HCI connection handling. */
27 
28 #include <linux/export.h>
29 #include <linux/debugfs.h>
30 #include <linux/errqueue.h>
31 
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/l2cap.h>
35 #include <net/bluetooth/iso.h>
36 #include <net/bluetooth/mgmt.h>
37 
38 #include "smp.h"
39 #include "eir.h"
40 
41 struct sco_param {
42 	u16 pkt_type;
43 	u16 max_latency;
44 	u8  retrans_effort;
45 };
46 
47 struct conn_handle_t {
48 	struct hci_conn *conn;
49 	__u16 handle;
50 };
51 
52 static const struct sco_param esco_param_cvsd[] = {
53 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a,	0x01 }, /* S3 */
54 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007,	0x01 }, /* S2 */
55 	{ EDR_ESCO_MASK | ESCO_EV3,   0x0007,	0x01 }, /* S1 */
56 	{ EDR_ESCO_MASK | ESCO_HV3,   0xffff,	0x01 }, /* D1 */
57 	{ EDR_ESCO_MASK | ESCO_HV1,   0xffff,	0x01 }, /* D0 */
58 };
59 
60 static const struct sco_param sco_param_cvsd[] = {
61 	{ EDR_ESCO_MASK | ESCO_HV3,   0xffff,	0xff }, /* D1 */
62 	{ EDR_ESCO_MASK | ESCO_HV1,   0xffff,	0xff }, /* D0 */
63 };
64 
65 static const struct sco_param esco_param_msbc[] = {
66 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d,	0x02 }, /* T2 */
67 	{ EDR_ESCO_MASK | ESCO_EV3,   0x0008,	0x02 }, /* T1 */
68 };
69 
70 /* This function requires the caller holds hdev->lock */
hci_connect_le_scan_cleanup(struct hci_conn * conn,u8 status)71 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status)
72 {
73 	struct hci_conn_params *params;
74 	struct hci_dev *hdev = conn->hdev;
75 	struct smp_irk *irk;
76 	bdaddr_t *bdaddr;
77 	u8 bdaddr_type;
78 
79 	bdaddr = &conn->dst;
80 	bdaddr_type = conn->dst_type;
81 
82 	/* Check if we need to convert to identity address */
83 	irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
84 	if (irk) {
85 		bdaddr = &irk->bdaddr;
86 		bdaddr_type = irk->addr_type;
87 	}
88 
89 	params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
90 					   bdaddr_type);
91 	if (!params)
92 		return;
93 
94 	if (params->conn) {
95 		hci_conn_drop(params->conn);
96 		hci_conn_put(params->conn);
97 		params->conn = NULL;
98 	}
99 
100 	if (!params->explicit_connect)
101 		return;
102 
103 	/* If the status indicates successful cancellation of
104 	 * the attempt (i.e. Unknown Connection Id) there's no point of
105 	 * notifying failure since we'll go back to keep trying to
106 	 * connect. The only exception is explicit connect requests
107 	 * where a timeout + cancel does indicate an actual failure.
108 	 */
109 	if (status && status != HCI_ERROR_UNKNOWN_CONN_ID)
110 		mgmt_connect_failed(hdev, conn, status);
111 
112 	/* The connection attempt was doing scan for new RPA, and is
113 	 * in scan phase. If params are not associated with any other
114 	 * autoconnect action, remove them completely. If they are, just unmark
115 	 * them as waiting for connection, by clearing explicit_connect field.
116 	 */
117 	params->explicit_connect = false;
118 
119 	hci_pend_le_list_del_init(params);
120 
121 	switch (params->auto_connect) {
122 	case HCI_AUTO_CONN_EXPLICIT:
123 		hci_conn_params_del(hdev, bdaddr, bdaddr_type);
124 		/* return instead of break to avoid duplicate scan update */
125 		return;
126 	case HCI_AUTO_CONN_DIRECT:
127 	case HCI_AUTO_CONN_ALWAYS:
128 		hci_pend_le_list_add(params, &hdev->pend_le_conns);
129 		break;
130 	case HCI_AUTO_CONN_REPORT:
131 		hci_pend_le_list_add(params, &hdev->pend_le_reports);
132 		break;
133 	default:
134 		break;
135 	}
136 
137 	hci_update_passive_scan(hdev);
138 }
139 
hci_conn_cleanup(struct hci_conn * conn)140 static void hci_conn_cleanup(struct hci_conn *conn)
141 {
142 	struct hci_dev *hdev = conn->hdev;
143 
144 	if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
145 		hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
146 
147 	if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
148 		hci_remove_link_key(hdev, &conn->dst);
149 
150 	hci_chan_list_flush(conn);
151 
152 	if (HCI_CONN_HANDLE_UNSET(conn->handle))
153 		ida_free(&hdev->unset_handle_ida, conn->handle);
154 
155 	if (conn->cleanup)
156 		conn->cleanup(conn);
157 
158 	if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
159 		switch (conn->setting & SCO_AIRMODE_MASK) {
160 		case SCO_AIRMODE_CVSD:
161 		case SCO_AIRMODE_TRANSP:
162 			if (hdev->notify)
163 				hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
164 			break;
165 		}
166 	} else {
167 		if (hdev->notify)
168 			hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
169 	}
170 
171 	debugfs_remove_recursive(conn->debugfs);
172 
173 	hci_conn_del_sysfs(conn);
174 
175 	hci_dev_put(hdev);
176 }
177 
hci_disconnect(struct hci_conn * conn,__u8 reason)178 int hci_disconnect(struct hci_conn *conn, __u8 reason)
179 {
180 	BT_DBG("hcon %p", conn);
181 
182 	/* When we are central of an established connection and it enters
183 	 * the disconnect timeout, then go ahead and try to read the
184 	 * current clock offset.  Processing of the result is done
185 	 * within the event handling and hci_clock_offset_evt function.
186 	 */
187 	if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
188 	    (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
189 		struct hci_dev *hdev = conn->hdev;
190 		struct hci_cp_read_clock_offset clkoff_cp;
191 
192 		clkoff_cp.handle = cpu_to_le16(conn->handle);
193 		hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
194 			     &clkoff_cp);
195 	}
196 
197 	return hci_abort_conn(conn, reason);
198 }
199 
hci_add_sco(struct hci_conn * conn,__u16 handle)200 static void hci_add_sco(struct hci_conn *conn, __u16 handle)
201 {
202 	struct hci_dev *hdev = conn->hdev;
203 	struct hci_cp_add_sco cp;
204 
205 	BT_DBG("hcon %p", conn);
206 
207 	conn->state = BT_CONNECT;
208 	conn->out = true;
209 
210 	conn->attempt++;
211 
212 	cp.handle   = cpu_to_le16(handle);
213 	cp.pkt_type = cpu_to_le16(conn->pkt_type);
214 
215 	hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
216 }
217 
find_next_esco_param(struct hci_conn * conn,const struct sco_param * esco_param,int size)218 static bool find_next_esco_param(struct hci_conn *conn,
219 				 const struct sco_param *esco_param, int size)
220 {
221 	if (!conn->parent)
222 		return false;
223 
224 	for (; conn->attempt <= size; conn->attempt++) {
225 		if (lmp_esco_2m_capable(conn->parent) ||
226 		    (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
227 			break;
228 		BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
229 		       conn, conn->attempt);
230 	}
231 
232 	return conn->attempt <= size;
233 }
234 
configure_datapath_sync(struct hci_dev * hdev,struct bt_codec * codec)235 static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec)
236 {
237 	int err;
238 	__u8 vnd_len, *vnd_data = NULL;
239 	struct hci_op_configure_data_path *cmd = NULL;
240 
241 	/* Do not take below 2 checks as error since the 1st means user do not
242 	 * want to use HFP offload mode and the 2nd means the vendor controller
243 	 * do not need to send below HCI command for offload mode.
244 	 */
245 	if (!codec->data_path || !hdev->get_codec_config_data)
246 		return 0;
247 
248 	err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len,
249 					  &vnd_data);
250 	if (err < 0)
251 		goto error;
252 
253 	cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL);
254 	if (!cmd) {
255 		err = -ENOMEM;
256 		goto error;
257 	}
258 
259 	err = hdev->get_data_path_id(hdev, &cmd->data_path_id);
260 	if (err < 0)
261 		goto error;
262 
263 	cmd->vnd_len = vnd_len;
264 	memcpy(cmd->vnd_data, vnd_data, vnd_len);
265 
266 	cmd->direction = 0x00;
267 	__hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
268 			      sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT);
269 
270 	cmd->direction = 0x01;
271 	err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
272 				    sizeof(*cmd) + vnd_len, cmd,
273 				    HCI_CMD_TIMEOUT);
274 error:
275 
276 	kfree(cmd);
277 	kfree(vnd_data);
278 	return err;
279 }
280 
hci_enhanced_setup_sync(struct hci_dev * hdev,void * data)281 static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data)
282 {
283 	struct conn_handle_t *conn_handle = data;
284 	struct hci_conn *conn = conn_handle->conn;
285 	__u16 handle = conn_handle->handle;
286 	struct hci_cp_enhanced_setup_sync_conn cp;
287 	const struct sco_param *param;
288 
289 	kfree(conn_handle);
290 
291 	if (!hci_conn_valid(hdev, conn))
292 		return -ECANCELED;
293 
294 	bt_dev_dbg(hdev, "hcon %p", conn);
295 
296 	configure_datapath_sync(hdev, &conn->codec);
297 
298 	conn->state = BT_CONNECT;
299 	conn->out = true;
300 
301 	conn->attempt++;
302 
303 	memset(&cp, 0x00, sizeof(cp));
304 
305 	cp.handle   = cpu_to_le16(handle);
306 
307 	cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
308 	cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
309 
310 	switch (conn->codec.id) {
311 	case BT_CODEC_MSBC:
312 		if (!find_next_esco_param(conn, esco_param_msbc,
313 					  ARRAY_SIZE(esco_param_msbc)))
314 			return -EINVAL;
315 
316 		param = &esco_param_msbc[conn->attempt - 1];
317 		cp.tx_coding_format.id = 0x05;
318 		cp.rx_coding_format.id = 0x05;
319 		cp.tx_codec_frame_size = __cpu_to_le16(60);
320 		cp.rx_codec_frame_size = __cpu_to_le16(60);
321 		cp.in_bandwidth = __cpu_to_le32(32000);
322 		cp.out_bandwidth = __cpu_to_le32(32000);
323 		cp.in_coding_format.id = 0x04;
324 		cp.out_coding_format.id = 0x04;
325 		cp.in_coded_data_size = __cpu_to_le16(16);
326 		cp.out_coded_data_size = __cpu_to_le16(16);
327 		cp.in_pcm_data_format = 2;
328 		cp.out_pcm_data_format = 2;
329 		cp.in_pcm_sample_payload_msb_pos = 0;
330 		cp.out_pcm_sample_payload_msb_pos = 0;
331 		cp.in_data_path = conn->codec.data_path;
332 		cp.out_data_path = conn->codec.data_path;
333 		cp.in_transport_unit_size = 1;
334 		cp.out_transport_unit_size = 1;
335 		break;
336 
337 	case BT_CODEC_TRANSPARENT:
338 		if (!find_next_esco_param(conn, esco_param_msbc,
339 					  ARRAY_SIZE(esco_param_msbc)))
340 			return -EINVAL;
341 
342 		param = &esco_param_msbc[conn->attempt - 1];
343 		cp.tx_coding_format.id = 0x03;
344 		cp.rx_coding_format.id = 0x03;
345 		cp.tx_codec_frame_size = __cpu_to_le16(60);
346 		cp.rx_codec_frame_size = __cpu_to_le16(60);
347 		cp.in_bandwidth = __cpu_to_le32(0x1f40);
348 		cp.out_bandwidth = __cpu_to_le32(0x1f40);
349 		cp.in_coding_format.id = 0x03;
350 		cp.out_coding_format.id = 0x03;
351 		cp.in_coded_data_size = __cpu_to_le16(16);
352 		cp.out_coded_data_size = __cpu_to_le16(16);
353 		cp.in_pcm_data_format = 2;
354 		cp.out_pcm_data_format = 2;
355 		cp.in_pcm_sample_payload_msb_pos = 0;
356 		cp.out_pcm_sample_payload_msb_pos = 0;
357 		cp.in_data_path = conn->codec.data_path;
358 		cp.out_data_path = conn->codec.data_path;
359 		cp.in_transport_unit_size = 1;
360 		cp.out_transport_unit_size = 1;
361 		break;
362 
363 	case BT_CODEC_CVSD:
364 		if (conn->parent && lmp_esco_capable(conn->parent)) {
365 			if (!find_next_esco_param(conn, esco_param_cvsd,
366 						  ARRAY_SIZE(esco_param_cvsd)))
367 				return -EINVAL;
368 			param = &esco_param_cvsd[conn->attempt - 1];
369 		} else {
370 			if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
371 				return -EINVAL;
372 			param = &sco_param_cvsd[conn->attempt - 1];
373 		}
374 		cp.tx_coding_format.id = 2;
375 		cp.rx_coding_format.id = 2;
376 		cp.tx_codec_frame_size = __cpu_to_le16(60);
377 		cp.rx_codec_frame_size = __cpu_to_le16(60);
378 		cp.in_bandwidth = __cpu_to_le32(16000);
379 		cp.out_bandwidth = __cpu_to_le32(16000);
380 		cp.in_coding_format.id = 4;
381 		cp.out_coding_format.id = 4;
382 		cp.in_coded_data_size = __cpu_to_le16(16);
383 		cp.out_coded_data_size = __cpu_to_le16(16);
384 		cp.in_pcm_data_format = 2;
385 		cp.out_pcm_data_format = 2;
386 		cp.in_pcm_sample_payload_msb_pos = 0;
387 		cp.out_pcm_sample_payload_msb_pos = 0;
388 		cp.in_data_path = conn->codec.data_path;
389 		cp.out_data_path = conn->codec.data_path;
390 		cp.in_transport_unit_size = 16;
391 		cp.out_transport_unit_size = 16;
392 		break;
393 	default:
394 		return -EINVAL;
395 	}
396 
397 	cp.retrans_effort = param->retrans_effort;
398 	cp.pkt_type = __cpu_to_le16(param->pkt_type);
399 	cp.max_latency = __cpu_to_le16(param->max_latency);
400 
401 	if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
402 		return -EIO;
403 
404 	return 0;
405 }
406 
hci_setup_sync_conn(struct hci_conn * conn,__u16 handle)407 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
408 {
409 	struct hci_dev *hdev = conn->hdev;
410 	struct hci_cp_setup_sync_conn cp;
411 	const struct sco_param *param;
412 
413 	bt_dev_dbg(hdev, "hcon %p", conn);
414 
415 	conn->state = BT_CONNECT;
416 	conn->out = true;
417 
418 	conn->attempt++;
419 
420 	cp.handle   = cpu_to_le16(handle);
421 
422 	cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
423 	cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
424 	cp.voice_setting  = cpu_to_le16(conn->setting);
425 
426 	switch (conn->setting & SCO_AIRMODE_MASK) {
427 	case SCO_AIRMODE_TRANSP:
428 		if (!find_next_esco_param(conn, esco_param_msbc,
429 					  ARRAY_SIZE(esco_param_msbc)))
430 			return false;
431 		param = &esco_param_msbc[conn->attempt - 1];
432 		break;
433 	case SCO_AIRMODE_CVSD:
434 		if (conn->parent && lmp_esco_capable(conn->parent)) {
435 			if (!find_next_esco_param(conn, esco_param_cvsd,
436 						  ARRAY_SIZE(esco_param_cvsd)))
437 				return false;
438 			param = &esco_param_cvsd[conn->attempt - 1];
439 		} else {
440 			if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
441 				return false;
442 			param = &sco_param_cvsd[conn->attempt - 1];
443 		}
444 		break;
445 	default:
446 		return false;
447 	}
448 
449 	cp.retrans_effort = param->retrans_effort;
450 	cp.pkt_type = __cpu_to_le16(param->pkt_type);
451 	cp.max_latency = __cpu_to_le16(param->max_latency);
452 
453 	if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
454 		return false;
455 
456 	return true;
457 }
458 
hci_setup_sync(struct hci_conn * conn,__u16 handle)459 bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
460 {
461 	int result;
462 	struct conn_handle_t *conn_handle;
463 
464 	if (enhanced_sync_conn_capable(conn->hdev)) {
465 		conn_handle = kzalloc_obj(*conn_handle);
466 
467 		if (!conn_handle)
468 			return false;
469 
470 		conn_handle->conn = conn;
471 		conn_handle->handle = handle;
472 		result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync,
473 					    conn_handle, NULL);
474 		if (result < 0)
475 			kfree(conn_handle);
476 
477 		return result == 0;
478 	}
479 
480 	return hci_setup_sync_conn(conn, handle);
481 }
482 
483 struct le_conn_update_data {
484 	struct hci_conn *conn;
485 	u16	min;
486 	u16	max;
487 	u16	latency;
488 	u16	to_multiplier;
489 };
490 
le_conn_update_sync(struct hci_dev * hdev,void * data)491 static int le_conn_update_sync(struct hci_dev *hdev, void *data)
492 {
493 	struct le_conn_update_data *d = data;
494 	struct hci_conn *conn = d->conn;
495 	struct hci_conn_params *params;
496 	struct hci_cp_le_conn_update cp;
497 	u16 timeout;
498 	u8 store_hint;
499 	int err;
500 
501 	/* Verify connection is still alive and read conn fields under
502 	 * the same lock to prevent a concurrent disconnect from freeing
503 	 * or reusing the connection while we build the HCI command.
504 	 */
505 	hci_dev_lock(hdev);
506 
507 	if (!hci_conn_valid(hdev, conn)) {
508 		hci_dev_unlock(hdev);
509 		return -ECANCELED;
510 	}
511 
512 	memset(&cp, 0, sizeof(cp));
513 	cp.handle		= cpu_to_le16(conn->handle);
514 	cp.conn_interval_min	= cpu_to_le16(d->min);
515 	cp.conn_interval_max	= cpu_to_le16(d->max);
516 	cp.conn_latency		= cpu_to_le16(d->latency);
517 	cp.supervision_timeout	= cpu_to_le16(d->to_multiplier);
518 	cp.min_ce_len		= cpu_to_le16(0x0000);
519 	cp.max_ce_len		= cpu_to_le16(0x0000);
520 	timeout			= conn->conn_timeout;
521 
522 	hci_dev_unlock(hdev);
523 
524 	err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE,
525 				       sizeof(cp), &cp,
526 				       HCI_EV_LE_CONN_UPDATE_COMPLETE,
527 				       timeout, NULL);
528 	if (err)
529 		return err;
530 
531 	/* Update stored connection parameters after the controller has
532 	 * confirmed the update via the LE Connection Update Complete event.
533 	 */
534 	hci_dev_lock(hdev);
535 
536 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
537 	if (params) {
538 		params->conn_min_interval = d->min;
539 		params->conn_max_interval = d->max;
540 		params->conn_latency = d->latency;
541 		params->supervision_timeout = d->to_multiplier;
542 		store_hint = 0x01;
543 	} else {
544 		store_hint = 0x00;
545 	}
546 
547 	hci_dev_unlock(hdev);
548 
549 	mgmt_new_conn_param(hdev, &conn->dst, conn->dst_type, store_hint,
550 			    d->min, d->max, d->latency, d->to_multiplier);
551 
552 	return 0;
553 }
554 
le_conn_update_complete(struct hci_dev * hdev,void * data,int err)555 static void le_conn_update_complete(struct hci_dev *hdev, void *data, int err)
556 {
557 	struct le_conn_update_data *d = data;
558 
559 	hci_conn_put(d->conn);
560 	kfree(d);
561 }
562 
hci_le_conn_update(struct hci_conn * conn,u16 min,u16 max,u16 latency,u16 to_multiplier)563 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
564 			u16 to_multiplier)
565 {
566 	struct le_conn_update_data *d;
567 
568 	d = kzalloc_obj(*d);
569 	if (!d)
570 		return;
571 
572 	hci_conn_get(conn);
573 	d->conn = conn;
574 	d->min = min;
575 	d->max = max;
576 	d->latency = latency;
577 	d->to_multiplier = to_multiplier;
578 
579 	if (hci_cmd_sync_queue(conn->hdev, le_conn_update_sync, d,
580 			       le_conn_update_complete) < 0) {
581 		hci_conn_put(conn);
582 		kfree(d);
583 	}
584 }
585 
hci_le_start_enc(struct hci_conn * conn,__le16 ediv,__le64 rand,__u8 ltk[16],__u8 key_size)586 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
587 		      __u8 ltk[16], __u8 key_size)
588 {
589 	struct hci_dev *hdev = conn->hdev;
590 	struct hci_cp_le_start_enc cp;
591 
592 	BT_DBG("hcon %p", conn);
593 
594 	memset(&cp, 0, sizeof(cp));
595 
596 	cp.handle = cpu_to_le16(conn->handle);
597 	cp.rand = rand;
598 	cp.ediv = ediv;
599 	memcpy(cp.ltk, ltk, key_size);
600 
601 	hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
602 }
603 
604 /* Device _must_ be locked */
hci_sco_setup(struct hci_conn * conn,__u8 status)605 void hci_sco_setup(struct hci_conn *conn, __u8 status)
606 {
607 	struct hci_link *link;
608 
609 	link = list_first_entry_or_null(&conn->link_list, struct hci_link, list);
610 	if (!link || !link->conn)
611 		return;
612 
613 	BT_DBG("hcon %p", conn);
614 
615 	if (!status) {
616 		if (lmp_esco_capable(conn->hdev))
617 			hci_setup_sync(link->conn, conn->handle);
618 		else
619 			hci_add_sco(link->conn, conn->handle);
620 	} else {
621 		hci_connect_cfm(link->conn, status);
622 		hci_conn_del(link->conn);
623 	}
624 }
625 
hci_conn_timeout(struct work_struct * work)626 static void hci_conn_timeout(struct work_struct *work)
627 {
628 	struct hci_conn *conn = container_of(work, struct hci_conn,
629 					     disc_work.work);
630 	int refcnt = atomic_read(&conn->refcnt);
631 
632 	BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
633 
634 	WARN_ON(refcnt < 0);
635 
636 	/* FIXME: It was observed that in pairing failed scenario, refcnt
637 	 * drops below 0. Probably this is because l2cap_conn_del calls
638 	 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
639 	 * dropped. After that loop hci_chan_del is called which also drops
640 	 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
641 	 * otherwise drop it.
642 	 */
643 	if (refcnt > 0)
644 		return;
645 
646 	hci_abort_conn(conn, hci_proto_disconn_ind(conn));
647 }
648 
649 /* Enter sniff mode */
hci_conn_idle(struct work_struct * work)650 static void hci_conn_idle(struct work_struct *work)
651 {
652 	struct hci_conn *conn = container_of(work, struct hci_conn,
653 					     idle_work.work);
654 	struct hci_dev *hdev = conn->hdev;
655 
656 	BT_DBG("hcon %p mode %d", conn, conn->mode);
657 
658 	if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
659 		return;
660 
661 	if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
662 		return;
663 
664 	if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
665 		struct hci_cp_sniff_subrate cp;
666 		cp.handle             = cpu_to_le16(conn->handle);
667 		cp.max_latency        = cpu_to_le16(0);
668 		cp.min_remote_timeout = cpu_to_le16(0);
669 		cp.min_local_timeout  = cpu_to_le16(0);
670 		hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
671 	}
672 
673 	if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
674 		struct hci_cp_sniff_mode cp;
675 		cp.handle       = cpu_to_le16(conn->handle);
676 		cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
677 		cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
678 		cp.attempt      = cpu_to_le16(4);
679 		cp.timeout      = cpu_to_le16(1);
680 		hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
681 	}
682 }
683 
hci_conn_auto_accept(struct work_struct * work)684 static void hci_conn_auto_accept(struct work_struct *work)
685 {
686 	struct hci_conn *conn = container_of(work, struct hci_conn,
687 					     auto_accept_work.work);
688 
689 	hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
690 		     &conn->dst);
691 }
692 
le_disable_advertising(struct hci_dev * hdev)693 static void le_disable_advertising(struct hci_dev *hdev)
694 {
695 	if (ext_adv_capable(hdev)) {
696 		struct hci_cp_le_set_ext_adv_enable cp;
697 
698 		cp.enable = 0x00;
699 		cp.num_of_sets = 0x00;
700 
701 		hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
702 			     &cp);
703 	} else {
704 		u8 enable = 0x00;
705 		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
706 			     &enable);
707 	}
708 }
709 
le_conn_timeout(struct work_struct * work)710 static void le_conn_timeout(struct work_struct *work)
711 {
712 	struct hci_conn *conn = container_of(work, struct hci_conn,
713 					     le_conn_timeout.work);
714 	struct hci_dev *hdev = conn->hdev;
715 
716 	BT_DBG("");
717 
718 	/* We could end up here due to having done directed advertising,
719 	 * so clean up the state if necessary. This should however only
720 	 * happen with broken hardware or if low duty cycle was used
721 	 * (which doesn't have a timeout of its own).
722 	 */
723 	if (conn->role == HCI_ROLE_SLAVE) {
724 		/* Disable LE Advertising */
725 		le_disable_advertising(hdev);
726 		hci_dev_lock(hdev);
727 		hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
728 		hci_dev_unlock(hdev);
729 		return;
730 	}
731 
732 	hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
733 }
734 
735 struct iso_list_data {
736 	union {
737 		u8  cig;
738 		u8  big;
739 	};
740 	union {
741 		u8  cis;
742 		u8  bis;
743 		u16 sync_handle;
744 	};
745 	int count;
746 	bool big_term;
747 	bool pa_sync_term;
748 	bool big_sync_term;
749 };
750 
bis_list(struct hci_conn * conn,void * data)751 static void bis_list(struct hci_conn *conn, void *data)
752 {
753 	struct iso_list_data *d = data;
754 
755 	/* Skip if not broadcast/ANY address */
756 	if (bacmp(&conn->dst, BDADDR_ANY))
757 		return;
758 
759 	if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET ||
760 	    d->bis != conn->iso_qos.bcast.bis)
761 		return;
762 
763 	d->count++;
764 }
765 
terminate_big_sync(struct hci_dev * hdev,void * data)766 static int terminate_big_sync(struct hci_dev *hdev, void *data)
767 {
768 	struct iso_list_data *d = data;
769 
770 	bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
771 
772 	hci_disable_per_advertising_sync(hdev, d->bis);
773 	hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
774 
775 	/* Only terminate BIG if it has been created */
776 	if (!d->big_term)
777 		return 0;
778 
779 	return hci_le_terminate_big_sync(hdev, d->big,
780 					 HCI_ERROR_LOCAL_HOST_TERM);
781 }
782 
terminate_big_destroy(struct hci_dev * hdev,void * data,int err)783 static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
784 {
785 	kfree(data);
786 }
787 
hci_le_terminate_big(struct hci_dev * hdev,struct hci_conn * conn)788 static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn)
789 {
790 	struct iso_list_data *d;
791 	int ret;
792 
793 	bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big,
794 		   conn->iso_qos.bcast.bis);
795 
796 	d = kzalloc_obj(*d);
797 	if (!d)
798 		return -ENOMEM;
799 
800 	d->big = conn->iso_qos.bcast.big;
801 	d->bis = conn->iso_qos.bcast.bis;
802 	d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags);
803 
804 	ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d,
805 				 terminate_big_destroy);
806 	if (ret)
807 		kfree(d);
808 
809 	return ret;
810 }
811 
big_terminate_sync(struct hci_dev * hdev,void * data)812 static int big_terminate_sync(struct hci_dev *hdev, void *data)
813 {
814 	struct iso_list_data *d = data;
815 
816 	bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big,
817 		   d->sync_handle);
818 
819 	if (d->big_sync_term)
820 		hci_le_big_terminate_sync(hdev, d->big);
821 
822 	if (d->pa_sync_term)
823 		return hci_le_pa_terminate_sync(hdev, d->sync_handle);
824 
825 	return 0;
826 }
827 
find_bis(struct hci_conn * conn,void * data)828 static void find_bis(struct hci_conn *conn, void *data)
829 {
830 	struct iso_list_data *d = data;
831 
832 	/* Ignore if BIG doesn't match */
833 	if (d->big != conn->iso_qos.bcast.big)
834 		return;
835 
836 	d->count++;
837 }
838 
hci_le_big_terminate(struct hci_dev * hdev,struct hci_conn * conn)839 static int hci_le_big_terminate(struct hci_dev *hdev, struct hci_conn *conn)
840 {
841 	struct iso_list_data *d;
842 	int ret;
843 
844 	bt_dev_dbg(hdev, "hcon %p big 0x%2.2x sync_handle 0x%4.4x", conn,
845 		   conn->iso_qos.bcast.big, conn->sync_handle);
846 
847 	d = kzalloc_obj(*d);
848 	if (!d)
849 		return -ENOMEM;
850 
851 	d->big = conn->iso_qos.bcast.big;
852 	d->sync_handle = conn->sync_handle;
853 
854 	if (conn->type == PA_LINK &&
855 	    test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags)) {
856 		hci_conn_hash_list_flag(hdev, find_bis, PA_LINK,
857 					HCI_CONN_PA_SYNC, d);
858 
859 		if (!d->count)
860 			d->pa_sync_term = true;
861 
862 		d->count = 0;
863 	}
864 
865 	if (test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags)) {
866 		hci_conn_hash_list_flag(hdev, find_bis, BIS_LINK,
867 					HCI_CONN_BIG_SYNC, d);
868 
869 		if (!d->count)
870 			d->big_sync_term = true;
871 	}
872 
873 	if (!d->pa_sync_term && !d->big_sync_term) {
874 		kfree(d);
875 		return 0;
876 	}
877 
878 	ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d,
879 				 terminate_big_destroy);
880 	if (ret)
881 		kfree(d);
882 
883 	return ret;
884 }
885 
886 /* Cleanup BIS connection
887  *
888  * Detects if there any BIS left connected in a BIG
889  * broadcaster: Remove advertising instance and terminate BIG.
890  * broadcaster receiver: Terminate BIG sync and terminate PA sync.
891  */
bis_cleanup(struct hci_conn * conn)892 static void bis_cleanup(struct hci_conn *conn)
893 {
894 	struct hci_dev *hdev = conn->hdev;
895 	struct hci_conn *bis;
896 
897 	bt_dev_dbg(hdev, "conn %p", conn);
898 
899 	if (conn->role == HCI_ROLE_MASTER) {
900 		if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags))
901 			return;
902 
903 		/* Check if ISO connection is a BIS and terminate advertising
904 		 * set and BIG if there are no other connections using it.
905 		 */
906 		bis = hci_conn_hash_lookup_big_state(hdev,
907 						     conn->iso_qos.bcast.big,
908 						     BT_CONNECTED,
909 						     HCI_ROLE_MASTER);
910 		if (bis)
911 			return;
912 
913 		bis = hci_conn_hash_lookup_big_state(hdev,
914 						     conn->iso_qos.bcast.big,
915 						     BT_CONNECT,
916 						     HCI_ROLE_MASTER);
917 		if (bis)
918 			return;
919 
920 		bis = hci_conn_hash_lookup_big_state(hdev,
921 						     conn->iso_qos.bcast.big,
922 						     BT_OPEN,
923 						     HCI_ROLE_MASTER);
924 		if (bis)
925 			return;
926 
927 		hci_le_terminate_big(hdev, conn);
928 	} else {
929 		hci_le_big_terminate(hdev, conn);
930 	}
931 }
932 
remove_cig_sync(struct hci_dev * hdev,void * data)933 static int remove_cig_sync(struct hci_dev *hdev, void *data)
934 {
935 	u8 handle = PTR_UINT(data);
936 
937 	return hci_le_remove_cig_sync(hdev, handle);
938 }
939 
hci_le_remove_cig(struct hci_dev * hdev,u8 handle)940 static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
941 {
942 	bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
943 
944 	return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle),
945 				  NULL);
946 }
947 
find_cis(struct hci_conn * conn,void * data)948 static void find_cis(struct hci_conn *conn, void *data)
949 {
950 	struct iso_list_data *d = data;
951 
952 	/* Ignore broadcast or if CIG don't match */
953 	if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig)
954 		return;
955 
956 	d->count++;
957 }
958 
959 /* Cleanup CIS connection:
960  *
961  * Detects if there any CIS left connected in a CIG and remove it.
962  */
cis_cleanup(struct hci_conn * conn)963 static void cis_cleanup(struct hci_conn *conn)
964 {
965 	struct hci_dev *hdev = conn->hdev;
966 	struct iso_list_data d;
967 
968 	if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET)
969 		return;
970 
971 	memset(&d, 0, sizeof(d));
972 	d.cig = conn->iso_qos.ucast.cig;
973 
974 	/* Check if ISO connection is a CIS and remove CIG if there are
975 	 * no other connections using it.
976 	 */
977 	hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_BOUND, &d);
978 	hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECT,
979 				 &d);
980 	hci_conn_hash_list_state(hdev, find_cis, CIS_LINK, BT_CONNECTED,
981 				 &d);
982 	if (d.count)
983 		return;
984 
985 	hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig);
986 }
987 
hci_conn_hash_alloc_unset(struct hci_dev * hdev)988 static int hci_conn_hash_alloc_unset(struct hci_dev *hdev)
989 {
990 	return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1,
991 			       U16_MAX, GFP_ATOMIC);
992 }
993 
__hci_conn_add(struct hci_dev * hdev,int type,bdaddr_t * dst,u8 dst_type,u8 role,u16 handle)994 static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type,
995 				       bdaddr_t *dst, u8 dst_type,
996 				       u8 role, u16 handle)
997 {
998 	struct hci_conn *conn;
999 	struct smp_irk *irk = NULL;
1000 
1001 	switch (type) {
1002 	case ACL_LINK:
1003 		if (!hdev->acl_mtu)
1004 			return ERR_PTR(-ECONNREFUSED);
1005 		break;
1006 	case CIS_LINK:
1007 	case BIS_LINK:
1008 	case PA_LINK:
1009 		if (!hdev->iso_mtu)
1010 			return ERR_PTR(-ECONNREFUSED);
1011 		irk = hci_get_irk(hdev, dst, dst_type);
1012 		break;
1013 	case LE_LINK:
1014 		if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
1015 			return ERR_PTR(-ECONNREFUSED);
1016 		if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU)
1017 			return ERR_PTR(-ECONNREFUSED);
1018 		irk = hci_get_irk(hdev, dst, dst_type);
1019 		break;
1020 	case SCO_LINK:
1021 	case ESCO_LINK:
1022 		if (!hdev->sco_pkts)
1023 			/* Controller does not support SCO or eSCO over HCI */
1024 			return ERR_PTR(-ECONNREFUSED);
1025 		break;
1026 	default:
1027 		return ERR_PTR(-ECONNREFUSED);
1028 	}
1029 
1030 	bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle);
1031 
1032 	conn = kzalloc_obj(*conn);
1033 	if (!conn)
1034 		return ERR_PTR(-ENOMEM);
1035 
1036 	/* If and IRK exists use its identity address */
1037 	if (!irk) {
1038 		bacpy(&conn->dst, dst);
1039 		conn->dst_type = dst_type;
1040 	} else {
1041 		bacpy(&conn->dst, &irk->bdaddr);
1042 		conn->dst_type = irk->addr_type;
1043 	}
1044 
1045 	bacpy(&conn->src, &hdev->bdaddr);
1046 	conn->handle = handle;
1047 	conn->hdev  = hdev;
1048 	conn->type  = type;
1049 	conn->role  = role;
1050 	conn->mode  = HCI_CM_ACTIVE;
1051 	conn->state = BT_OPEN;
1052 	conn->auth_type = HCI_AT_GENERAL_BONDING;
1053 	conn->io_capability = hdev->io_capability;
1054 	conn->remote_auth = 0xff;
1055 	conn->key_type = 0xff;
1056 	conn->rssi = HCI_RSSI_INVALID;
1057 	conn->tx_power = HCI_TX_POWER_INVALID;
1058 	conn->max_tx_power = HCI_TX_POWER_INVALID;
1059 	conn->sync_handle = HCI_SYNC_HANDLE_INVALID;
1060 	conn->sid = HCI_SID_INVALID;
1061 
1062 	set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
1063 	conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1064 
1065 	/* Set Default Authenticated payload timeout to 30s */
1066 	conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
1067 
1068 	if (conn->role == HCI_ROLE_MASTER)
1069 		conn->out = true;
1070 
1071 	switch (type) {
1072 	case ACL_LINK:
1073 		conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
1074 		conn->link_policy = hdev->link_policy;
1075 		conn->mtu = hdev->acl_mtu;
1076 		break;
1077 	case LE_LINK:
1078 		/* conn->src should reflect the local identity address */
1079 		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
1080 		conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
1081 		/* Use the controller supported PHYS as default until the
1082 		 * remote features are resolved.
1083 		 */
1084 		conn->le_tx_def_phys = hdev->le_tx_def_phys;
1085 		conn->le_rx_def_phys = hdev->le_tx_def_phys;
1086 		break;
1087 	case CIS_LINK:
1088 		/* conn->src should reflect the local identity address */
1089 		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
1090 
1091 		if (conn->role == HCI_ROLE_MASTER)
1092 			conn->cleanup = cis_cleanup;
1093 
1094 		conn->mtu = hdev->iso_mtu;
1095 		break;
1096 	case PA_LINK:
1097 	case BIS_LINK:
1098 		/* conn->src should reflect the local identity address */
1099 		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
1100 		conn->cleanup = bis_cleanup;
1101 		conn->mtu = hdev->iso_mtu;
1102 		break;
1103 	case SCO_LINK:
1104 		if (lmp_esco_capable(hdev))
1105 			conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
1106 					(hdev->esco_type & EDR_ESCO_MASK);
1107 		else
1108 			conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
1109 
1110 		conn->mtu = hdev->sco_mtu;
1111 		break;
1112 	case ESCO_LINK:
1113 		conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
1114 		conn->mtu = hdev->sco_mtu;
1115 		break;
1116 	}
1117 
1118 	skb_queue_head_init(&conn->data_q);
1119 	skb_queue_head_init(&conn->tx_q.queue);
1120 
1121 	INIT_LIST_HEAD(&conn->chan_list);
1122 	INIT_LIST_HEAD(&conn->link_list);
1123 
1124 	INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
1125 	INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
1126 	INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
1127 	INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
1128 
1129 	atomic_set(&conn->refcnt, 0);
1130 
1131 	hci_dev_hold(hdev);
1132 
1133 	hci_conn_hash_add(hdev, conn);
1134 
1135 	/* The SCO and eSCO connections will only be notified when their
1136 	 * setup has been completed. This is different to ACL links which
1137 	 * can be notified right away.
1138 	 */
1139 	if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
1140 		if (hdev->notify)
1141 			hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
1142 	}
1143 
1144 	hci_conn_init_sysfs(conn);
1145 	return conn;
1146 }
1147 
hci_conn_add_unset(struct hci_dev * hdev,int type,bdaddr_t * dst,u8 dst_type,u8 role)1148 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1149 				    bdaddr_t *dst, u8 dst_type, u8 role)
1150 {
1151 	int handle;
1152 
1153 	bt_dev_dbg(hdev, "dst %pMR", dst);
1154 
1155 	handle = hci_conn_hash_alloc_unset(hdev);
1156 	if (unlikely(handle < 0))
1157 		return ERR_PTR(-ECONNREFUSED);
1158 
1159 	return __hci_conn_add(hdev, type, dst, dst_type, role, handle);
1160 }
1161 
hci_conn_add(struct hci_dev * hdev,int type,bdaddr_t * dst,u8 dst_type,u8 role,u16 handle)1162 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1163 			      u8 dst_type, u8 role, u16 handle)
1164 {
1165 	if (handle > HCI_CONN_HANDLE_MAX)
1166 		return ERR_PTR(-EINVAL);
1167 
1168 	return __hci_conn_add(hdev, type, dst, dst_type, role, handle);
1169 }
1170 
hci_conn_cleanup_child(struct hci_conn * conn,u8 reason)1171 static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason)
1172 {
1173 	if (!reason)
1174 		reason = HCI_ERROR_REMOTE_USER_TERM;
1175 
1176 	/* Due to race, SCO/ISO conn might be not established yet at this point,
1177 	 * and nothing else will clean it up. In other cases it is done via HCI
1178 	 * events.
1179 	 */
1180 	switch (conn->type) {
1181 	case SCO_LINK:
1182 	case ESCO_LINK:
1183 		if (HCI_CONN_HANDLE_UNSET(conn->handle))
1184 			hci_conn_failed(conn, reason);
1185 		break;
1186 	case CIS_LINK:
1187 	case BIS_LINK:
1188 	case PA_LINK:
1189 		if ((conn->state != BT_CONNECTED &&
1190 		    !test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) ||
1191 		    test_bit(HCI_CONN_BIG_CREATED, &conn->flags))
1192 			hci_conn_failed(conn, reason);
1193 		break;
1194 	}
1195 }
1196 
hci_conn_unlink(struct hci_conn * conn)1197 static void hci_conn_unlink(struct hci_conn *conn)
1198 {
1199 	struct hci_dev *hdev = conn->hdev;
1200 
1201 	bt_dev_dbg(hdev, "hcon %p", conn);
1202 
1203 	if (!conn->parent) {
1204 		struct hci_link *link, *t;
1205 
1206 		list_for_each_entry_safe(link, t, &conn->link_list, list) {
1207 			struct hci_conn *child = link->conn;
1208 
1209 			hci_conn_unlink(child);
1210 
1211 			/* If hdev is down it means
1212 			 * hci_dev_close_sync/hci_conn_hash_flush is in progress
1213 			 * and links don't need to be cleanup as all connections
1214 			 * would be cleanup.
1215 			 */
1216 			if (!test_bit(HCI_UP, &hdev->flags))
1217 				continue;
1218 
1219 			hci_conn_cleanup_child(child, conn->abort_reason);
1220 		}
1221 
1222 		return;
1223 	}
1224 
1225 	if (!conn->link)
1226 		return;
1227 
1228 	list_del_rcu(&conn->link->list);
1229 	synchronize_rcu();
1230 
1231 	hci_conn_drop(conn->parent);
1232 	hci_conn_put(conn->parent);
1233 	conn->parent = NULL;
1234 
1235 	kfree(conn->link);
1236 	conn->link = NULL;
1237 }
1238 
hci_conn_del(struct hci_conn * conn)1239 void hci_conn_del(struct hci_conn *conn)
1240 {
1241 	struct hci_dev *hdev = conn->hdev;
1242 
1243 	BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
1244 
1245 	hci_conn_unlink(conn);
1246 
1247 	disable_delayed_work_sync(&conn->disc_work);
1248 	disable_delayed_work_sync(&conn->auto_accept_work);
1249 	disable_delayed_work_sync(&conn->idle_work);
1250 
1251 	/* Remove the connection from the list so unacked logic can detect when
1252 	 * a certain pool is not being utilized.
1253 	 */
1254 	hci_conn_hash_del(hdev, conn);
1255 
1256 	/* Handle unacked frames:
1257 	 *
1258 	 * - In case there are no connection, or if restoring the buffers
1259 	 *   considered in transist would overflow, restore all buffers to the
1260 	 *   pool.
1261 	 * - Otherwise restore just the buffers considered in transit for the
1262 	 *   hci_conn
1263 	 */
1264 	switch (conn->type) {
1265 	case ACL_LINK:
1266 		if (!hci_conn_num(hdev, ACL_LINK) ||
1267 		    hdev->acl_cnt + conn->sent > hdev->acl_pkts)
1268 			hdev->acl_cnt = hdev->acl_pkts;
1269 		else
1270 			hdev->acl_cnt += conn->sent;
1271 		break;
1272 	case LE_LINK:
1273 		cancel_delayed_work(&conn->le_conn_timeout);
1274 
1275 		if (hdev->le_pkts) {
1276 			if (!hci_conn_num(hdev, LE_LINK) ||
1277 			    hdev->le_cnt + conn->sent > hdev->le_pkts)
1278 				hdev->le_cnt = hdev->le_pkts;
1279 			else
1280 				hdev->le_cnt += conn->sent;
1281 		} else {
1282 			if ((!hci_conn_num(hdev, LE_LINK) &&
1283 			     !hci_conn_num(hdev, ACL_LINK)) ||
1284 			    hdev->acl_cnt + conn->sent > hdev->acl_pkts)
1285 				hdev->acl_cnt = hdev->acl_pkts;
1286 			else
1287 				hdev->acl_cnt += conn->sent;
1288 		}
1289 		break;
1290 	case CIS_LINK:
1291 	case BIS_LINK:
1292 	case PA_LINK:
1293 		if (!hci_iso_count(hdev) ||
1294 		    hdev->iso_cnt + conn->sent > hdev->iso_pkts)
1295 			hdev->iso_cnt = hdev->iso_pkts;
1296 		else
1297 			hdev->iso_cnt += conn->sent;
1298 		break;
1299 	}
1300 
1301 	skb_queue_purge(&conn->data_q);
1302 	skb_queue_purge(&conn->tx_q.queue);
1303 
1304 	/* Remove the connection from the list and cleanup its remaining
1305 	 * state. This is a separate function since for some cases like
1306 	 * BT_CONNECT_SCAN we *only* want the cleanup part without the
1307 	 * rest of hci_conn_del.
1308 	 */
1309 	hci_conn_cleanup(conn);
1310 
1311 	/* Dequeue callbacks using connection pointer as data */
1312 	hci_cmd_sync_dequeue(hdev, NULL, conn, NULL);
1313 }
1314 
hci_get_route(bdaddr_t * dst,bdaddr_t * src,uint8_t src_type)1315 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
1316 {
1317 	int use_src = bacmp(src, BDADDR_ANY);
1318 	struct hci_dev *hdev = NULL, *d;
1319 
1320 	BT_DBG("%pMR -> %pMR", src, dst);
1321 
1322 	read_lock(&hci_dev_list_lock);
1323 
1324 	list_for_each_entry(d, &hci_dev_list, list) {
1325 		if (!test_bit(HCI_UP, &d->flags) ||
1326 		    hci_dev_test_flag(d, HCI_USER_CHANNEL))
1327 			continue;
1328 
1329 		/* Simple routing:
1330 		 *   No source address - find interface with bdaddr != dst
1331 		 *   Source address    - find interface with bdaddr == src
1332 		 */
1333 
1334 		if (use_src) {
1335 			bdaddr_t id_addr;
1336 			u8 id_addr_type;
1337 
1338 			if (src_type == BDADDR_BREDR) {
1339 				if (!lmp_bredr_capable(d))
1340 					continue;
1341 				bacpy(&id_addr, &d->bdaddr);
1342 				id_addr_type = BDADDR_BREDR;
1343 			} else {
1344 				if (!lmp_le_capable(d))
1345 					continue;
1346 
1347 				hci_copy_identity_address(d, &id_addr,
1348 							  &id_addr_type);
1349 
1350 				/* Convert from HCI to three-value type */
1351 				if (id_addr_type == ADDR_LE_DEV_PUBLIC)
1352 					id_addr_type = BDADDR_LE_PUBLIC;
1353 				else
1354 					id_addr_type = BDADDR_LE_RANDOM;
1355 			}
1356 
1357 			if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
1358 				hdev = d; break;
1359 			}
1360 		} else {
1361 			if (bacmp(&d->bdaddr, dst)) {
1362 				hdev = d; break;
1363 			}
1364 		}
1365 	}
1366 
1367 	if (hdev)
1368 		hdev = hci_dev_hold(hdev);
1369 
1370 	read_unlock(&hci_dev_list_lock);
1371 	return hdev;
1372 }
1373 EXPORT_SYMBOL(hci_get_route);
1374 
1375 /* This function requires the caller holds hdev->lock */
hci_le_conn_failed(struct hci_conn * conn,u8 status)1376 static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
1377 {
1378 	struct hci_dev *hdev = conn->hdev;
1379 
1380 	hci_connect_le_scan_cleanup(conn, status);
1381 
1382 	/* Enable advertising in case this was a failed connection
1383 	 * attempt as a peripheral.
1384 	 */
1385 	hci_enable_advertising(hdev);
1386 }
1387 
1388 /* This function requires the caller holds hdev->lock */
hci_conn_failed(struct hci_conn * conn,u8 status)1389 void hci_conn_failed(struct hci_conn *conn, u8 status)
1390 {
1391 	struct hci_dev *hdev = conn->hdev;
1392 
1393 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
1394 
1395 	switch (conn->type) {
1396 	case LE_LINK:
1397 		hci_le_conn_failed(conn, status);
1398 		break;
1399 	case ACL_LINK:
1400 		mgmt_connect_failed(hdev, conn, status);
1401 		break;
1402 	}
1403 
1404 	/* In case of BIG/PA sync failed, clear conn flags so that
1405 	 * the conns will be correctly cleaned up by ISO layer
1406 	 */
1407 	test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags);
1408 	test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags);
1409 
1410 	conn->state = BT_CLOSED;
1411 	hci_connect_cfm(conn, status);
1412 	hci_conn_del(conn);
1413 }
1414 
1415 /* This function requires the caller holds hdev->lock */
hci_conn_set_handle(struct hci_conn * conn,u16 handle)1416 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
1417 {
1418 	struct hci_dev *hdev = conn->hdev;
1419 
1420 	bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle);
1421 
1422 	if (conn->handle == handle)
1423 		return 0;
1424 
1425 	if (handle > HCI_CONN_HANDLE_MAX) {
1426 		bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x",
1427 			   handle, HCI_CONN_HANDLE_MAX);
1428 		return HCI_ERROR_INVALID_PARAMETERS;
1429 	}
1430 
1431 	/* If abort_reason has been sent it means the connection is being
1432 	 * aborted and the handle shall not be changed.
1433 	 */
1434 	if (conn->abort_reason)
1435 		return conn->abort_reason;
1436 
1437 	if (HCI_CONN_HANDLE_UNSET(conn->handle))
1438 		ida_free(&hdev->unset_handle_ida, conn->handle);
1439 
1440 	conn->handle = handle;
1441 
1442 	return 0;
1443 }
1444 
hci_connect_le(struct hci_dev * hdev,bdaddr_t * dst,u8 dst_type,bool dst_resolved,u8 sec_level,u16 conn_timeout,u8 role,u8 phy,u8 sec_phy)1445 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1446 				u8 dst_type, bool dst_resolved, u8 sec_level,
1447 				u16 conn_timeout, u8 role, u8 phy, u8 sec_phy)
1448 {
1449 	struct hci_conn *conn;
1450 	struct smp_irk *irk;
1451 	int err;
1452 
1453 	/* Let's make sure that le is enabled.*/
1454 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1455 		if (lmp_le_capable(hdev))
1456 			return ERR_PTR(-ECONNREFUSED);
1457 
1458 		return ERR_PTR(-EOPNOTSUPP);
1459 	}
1460 
1461 	/* Since the controller supports only one LE connection attempt at a
1462 	 * time, we return -EBUSY if there is any connection attempt running.
1463 	 */
1464 	if (hci_lookup_le_connect(hdev))
1465 		return ERR_PTR(-EBUSY);
1466 
1467 	/* If there's already a connection object but it's not in
1468 	 * scanning state it means it must already be established, in
1469 	 * which case we can't do anything else except report a failure
1470 	 * to connect.
1471 	 */
1472 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1473 	if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
1474 		return ERR_PTR(-EBUSY);
1475 	}
1476 
1477 	/* Check if the destination address has been resolved by the controller
1478 	 * since if it did then the identity address shall be used.
1479 	 */
1480 	if (!dst_resolved) {
1481 		/* When given an identity address with existing identity
1482 		 * resolving key, the connection needs to be established
1483 		 * to a resolvable random address.
1484 		 *
1485 		 * Storing the resolvable random address is required here
1486 		 * to handle connection failures. The address will later
1487 		 * be resolved back into the original identity address
1488 		 * from the connect request.
1489 		 */
1490 		irk = hci_find_irk_by_addr(hdev, dst, dst_type);
1491 		if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
1492 			dst = &irk->rpa;
1493 			dst_type = ADDR_LE_DEV_RANDOM;
1494 		}
1495 	}
1496 
1497 	if (conn) {
1498 		bacpy(&conn->dst, dst);
1499 	} else {
1500 		conn = hci_conn_add_unset(hdev, LE_LINK, dst, dst_type, role);
1501 		if (IS_ERR(conn))
1502 			return conn;
1503 		hci_conn_hold(conn);
1504 		conn->pending_sec_level = sec_level;
1505 	}
1506 
1507 	conn->sec_level = BT_SECURITY_LOW;
1508 	conn->conn_timeout = conn_timeout;
1509 	conn->le_adv_phy = phy;
1510 	conn->le_adv_sec_phy = sec_phy;
1511 
1512 	err = hci_connect_le_sync(hdev, conn);
1513 	if (err) {
1514 		hci_conn_del(conn);
1515 		return ERR_PTR(err);
1516 	}
1517 
1518 	return conn;
1519 }
1520 
is_connected(struct hci_dev * hdev,bdaddr_t * addr,u8 type)1521 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1522 {
1523 	struct hci_conn *conn;
1524 
1525 	conn = hci_conn_hash_lookup_le(hdev, addr, type);
1526 	if (!conn)
1527 		return false;
1528 
1529 	if (conn->state != BT_CONNECTED)
1530 		return false;
1531 
1532 	return true;
1533 }
1534 
1535 /* This function requires the caller holds hdev->lock */
hci_explicit_conn_params_set(struct hci_dev * hdev,bdaddr_t * addr,u8 addr_type)1536 static int hci_explicit_conn_params_set(struct hci_dev *hdev,
1537 					bdaddr_t *addr, u8 addr_type)
1538 {
1539 	struct hci_conn_params *params;
1540 
1541 	if (is_connected(hdev, addr, addr_type))
1542 		return -EISCONN;
1543 
1544 	params = hci_conn_params_lookup(hdev, addr, addr_type);
1545 	if (!params) {
1546 		params = hci_conn_params_add(hdev, addr, addr_type);
1547 		if (!params)
1548 			return -ENOMEM;
1549 
1550 		/* If we created new params, mark them to be deleted in
1551 		 * hci_connect_le_scan_cleanup. It's different case than
1552 		 * existing disabled params, those will stay after cleanup.
1553 		 */
1554 		params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1555 	}
1556 
1557 	/* We're trying to connect, so make sure params are at pend_le_conns */
1558 	if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
1559 	    params->auto_connect == HCI_AUTO_CONN_REPORT ||
1560 	    params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
1561 		hci_pend_le_list_del_init(params);
1562 		hci_pend_le_list_add(params, &hdev->pend_le_conns);
1563 	}
1564 
1565 	params->explicit_connect = true;
1566 
1567 	BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1568 	       params->auto_connect);
1569 
1570 	return 0;
1571 }
1572 
qos_set_big(struct hci_dev * hdev,struct bt_iso_qos * qos)1573 static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos)
1574 {
1575 	struct hci_conn *conn;
1576 	u8  big;
1577 
1578 	/* Allocate a BIG if not set */
1579 	if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) {
1580 		for (big = 0x00; big < 0xef; big++) {
1581 
1582 			conn = hci_conn_hash_lookup_big(hdev, big);
1583 			if (!conn)
1584 				break;
1585 		}
1586 
1587 		if (big == 0xef)
1588 			return -EADDRNOTAVAIL;
1589 
1590 		/* Update BIG */
1591 		qos->bcast.big = big;
1592 	}
1593 
1594 	return 0;
1595 }
1596 
qos_set_bis(struct hci_dev * hdev,struct bt_iso_qos * qos)1597 static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos)
1598 {
1599 	struct hci_conn *conn;
1600 	u8  bis;
1601 
1602 	/* Allocate BIS if not set */
1603 	if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) {
1604 		if (qos->bcast.big != BT_ISO_QOS_BIG_UNSET) {
1605 			conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1606 
1607 			if (conn) {
1608 				/* If the BIG handle is already matched to an advertising
1609 				 * handle, do not allocate a new one.
1610 				 */
1611 				qos->bcast.bis = conn->iso_qos.bcast.bis;
1612 				return 0;
1613 			}
1614 		}
1615 
1616 		/* Find an unused adv set to advertise BIS, skip instance 0x00
1617 		 * since it is reserved as general purpose set.
1618 		 */
1619 		for (bis = 0x01; bis < hdev->le_num_of_adv_sets;
1620 		     bis++) {
1621 
1622 			conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis);
1623 			if (!conn)
1624 				break;
1625 		}
1626 
1627 		if (bis == hdev->le_num_of_adv_sets)
1628 			return -EADDRNOTAVAIL;
1629 
1630 		/* Update BIS */
1631 		qos->bcast.bis = bis;
1632 	}
1633 
1634 	return 0;
1635 }
1636 
1637 /* This function requires the caller holds hdev->lock */
hci_add_bis(struct hci_dev * hdev,bdaddr_t * dst,__u8 sid,struct bt_iso_qos * qos,__u8 base_len,__u8 * base,u16 timeout)1638 static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst,
1639 				    __u8 sid, struct bt_iso_qos *qos,
1640 				    __u8 base_len, __u8 *base, u16 timeout)
1641 {
1642 	struct hci_conn *conn;
1643 	int err;
1644 
1645 	/* Let's make sure that le is enabled.*/
1646 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1647 		if (lmp_le_capable(hdev))
1648 			return ERR_PTR(-ECONNREFUSED);
1649 		return ERR_PTR(-EOPNOTSUPP);
1650 	}
1651 
1652 	err = qos_set_big(hdev, qos);
1653 	if (err)
1654 		return ERR_PTR(err);
1655 
1656 	err = qos_set_bis(hdev, qos);
1657 	if (err)
1658 		return ERR_PTR(err);
1659 
1660 	/* Check if the LE Create BIG command has already been sent */
1661 	conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big,
1662 						qos->bcast.big);
1663 	if (conn)
1664 		return ERR_PTR(-EADDRINUSE);
1665 
1666 	/* Check BIS settings against other bound BISes, since all
1667 	 * BISes in a BIG must have the same value for all parameters
1668 	 */
1669 	conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1670 
1671 	if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) ||
1672 		     base_len != conn->le_per_adv_data_len ||
1673 		     memcmp(conn->le_per_adv_data, base, base_len)))
1674 		return ERR_PTR(-EADDRINUSE);
1675 
1676 	conn = hci_conn_add_unset(hdev, BIS_LINK, dst, 0, HCI_ROLE_MASTER);
1677 	if (IS_ERR(conn))
1678 		return conn;
1679 
1680 	conn->state = BT_CONNECT;
1681 	conn->sid = sid;
1682 	conn->conn_timeout = timeout;
1683 
1684 	hci_conn_hold(conn);
1685 	return conn;
1686 }
1687 
1688 /* This function requires the caller holds hdev->lock */
hci_connect_le_scan(struct hci_dev * hdev,bdaddr_t * dst,u8 dst_type,u8 sec_level,u16 conn_timeout,enum conn_reasons conn_reason)1689 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1690 				     u8 dst_type, u8 sec_level,
1691 				     u16 conn_timeout,
1692 				     enum conn_reasons conn_reason)
1693 {
1694 	struct hci_conn *conn;
1695 
1696 	/* Let's make sure that le is enabled.*/
1697 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1698 		if (lmp_le_capable(hdev))
1699 			return ERR_PTR(-ECONNREFUSED);
1700 
1701 		return ERR_PTR(-EOPNOTSUPP);
1702 	}
1703 
1704 	/* Some devices send ATT messages as soon as the physical link is
1705 	 * established. To be able to handle these ATT messages, the user-
1706 	 * space first establishes the connection and then starts the pairing
1707 	 * process.
1708 	 *
1709 	 * So if a hci_conn object already exists for the following connection
1710 	 * attempt, we simply update pending_sec_level and auth_type fields
1711 	 * and return the object found.
1712 	 */
1713 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1714 	if (conn) {
1715 		if (conn->pending_sec_level < sec_level)
1716 			conn->pending_sec_level = sec_level;
1717 		goto done;
1718 	}
1719 
1720 	BT_DBG("requesting refresh of dst_addr");
1721 
1722 	conn = hci_conn_add_unset(hdev, LE_LINK, dst, dst_type,
1723 				  HCI_ROLE_MASTER);
1724 	if (IS_ERR(conn))
1725 		return conn;
1726 
1727 	if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1728 		hci_conn_del(conn);
1729 		return ERR_PTR(-EBUSY);
1730 	}
1731 
1732 	conn->state = BT_CONNECT;
1733 	set_bit(HCI_CONN_SCANNING, &conn->flags);
1734 	conn->sec_level = BT_SECURITY_LOW;
1735 	conn->pending_sec_level = sec_level;
1736 	conn->conn_timeout = conn_timeout;
1737 	conn->conn_reason = conn_reason;
1738 
1739 	hci_update_passive_scan(hdev);
1740 
1741 done:
1742 	hci_conn_hold(conn);
1743 	return conn;
1744 }
1745 
hci_connect_acl(struct hci_dev * hdev,bdaddr_t * dst,u8 sec_level,u8 auth_type,enum conn_reasons conn_reason,u16 timeout)1746 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1747 				 u8 sec_level, u8 auth_type,
1748 				 enum conn_reasons conn_reason, u16 timeout)
1749 {
1750 	struct hci_conn *acl;
1751 
1752 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1753 		if (lmp_bredr_capable(hdev))
1754 			return ERR_PTR(-ECONNREFUSED);
1755 
1756 		return ERR_PTR(-EOPNOTSUPP);
1757 	}
1758 
1759 	/* Reject outgoing connection to device with same BD ADDR against
1760 	 * CVE-2020-26555
1761 	 */
1762 	if (!bacmp(&hdev->bdaddr, dst)) {
1763 		bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
1764 			   dst);
1765 		return ERR_PTR(-ECONNREFUSED);
1766 	}
1767 
1768 	acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1769 	if (!acl) {
1770 		acl = hci_conn_add_unset(hdev, ACL_LINK, dst, 0,
1771 					 HCI_ROLE_MASTER);
1772 		if (IS_ERR(acl))
1773 			return acl;
1774 	}
1775 
1776 	hci_conn_hold(acl);
1777 
1778 	acl->conn_reason = conn_reason;
1779 	if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
1780 		int err;
1781 
1782 		acl->sec_level = BT_SECURITY_LOW;
1783 		acl->pending_sec_level = sec_level;
1784 		acl->auth_type = auth_type;
1785 		acl->conn_timeout = timeout;
1786 
1787 		err = hci_connect_acl_sync(hdev, acl);
1788 		if (err) {
1789 			hci_conn_del(acl);
1790 			return ERR_PTR(err);
1791 		}
1792 	}
1793 
1794 	return acl;
1795 }
1796 
hci_conn_link(struct hci_conn * parent,struct hci_conn * conn)1797 static struct hci_link *hci_conn_link(struct hci_conn *parent,
1798 				      struct hci_conn *conn)
1799 {
1800 	struct hci_dev *hdev = parent->hdev;
1801 	struct hci_link *link;
1802 
1803 	bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn);
1804 
1805 	if (conn->link)
1806 		return conn->link;
1807 
1808 	if (conn->parent)
1809 		return NULL;
1810 
1811 	link = kzalloc_obj(*link);
1812 	if (!link)
1813 		return NULL;
1814 
1815 	link->conn = hci_conn_hold(conn);
1816 	conn->link = link;
1817 	conn->parent = hci_conn_get(parent);
1818 
1819 	/* Use list_add_tail_rcu append to the list */
1820 	list_add_tail_rcu(&link->list, &parent->link_list);
1821 
1822 	return link;
1823 }
1824 
hci_connect_sco(struct hci_dev * hdev,int type,bdaddr_t * dst,__u16 setting,struct bt_codec * codec,u16 timeout)1825 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1826 				 __u16 setting, struct bt_codec *codec,
1827 				 u16 timeout)
1828 {
1829 	struct hci_conn *acl;
1830 	struct hci_conn *sco;
1831 	struct hci_link *link;
1832 
1833 	acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
1834 			      CONN_REASON_SCO_CONNECT, timeout);
1835 	if (IS_ERR(acl))
1836 		return acl;
1837 
1838 	sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1839 	if (!sco) {
1840 		sco = hci_conn_add_unset(hdev, type, dst, 0, HCI_ROLE_MASTER);
1841 		if (IS_ERR(sco)) {
1842 			hci_conn_drop(acl);
1843 			return sco;
1844 		}
1845 	}
1846 
1847 	link = hci_conn_link(acl, sco);
1848 	if (!link) {
1849 		hci_conn_drop(acl);
1850 		hci_conn_drop(sco);
1851 		return ERR_PTR(-ENOLINK);
1852 	}
1853 
1854 	sco->setting = setting;
1855 	sco->codec = *codec;
1856 
1857 	if (acl->state == BT_CONNECTED &&
1858 	    (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
1859 		set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
1860 		hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
1861 
1862 		if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
1863 			/* defer SCO setup until mode change completed */
1864 			set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
1865 			return sco;
1866 		}
1867 
1868 		hci_sco_setup(acl, 0x00);
1869 	}
1870 
1871 	return sco;
1872 }
1873 
hci_le_create_big(struct hci_conn * conn,struct bt_iso_qos * qos)1874 static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos)
1875 {
1876 	struct hci_dev *hdev = conn->hdev;
1877 	struct hci_cp_le_create_big cp;
1878 	struct iso_list_data data;
1879 
1880 	memset(&cp, 0, sizeof(cp));
1881 
1882 	data.big = qos->bcast.big;
1883 	data.bis = qos->bcast.bis;
1884 	data.count = 0;
1885 
1886 	/* Create a BIS for each bound connection */
1887 	hci_conn_hash_list_state(hdev, bis_list, BIS_LINK,
1888 				 BT_BOUND, &data);
1889 
1890 	cp.handle = qos->bcast.big;
1891 	cp.adv_handle = qos->bcast.bis;
1892 	cp.num_bis  = data.count;
1893 	hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
1894 	cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
1895 	cp.bis.latency =  cpu_to_le16(qos->bcast.out.latency);
1896 	cp.bis.rtn  = qos->bcast.out.rtn;
1897 	cp.bis.phy  = qos->bcast.out.phys;
1898 	cp.bis.packing = qos->bcast.packing;
1899 	cp.bis.framing = qos->bcast.framing;
1900 	cp.bis.encryption = qos->bcast.encryption;
1901 	memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
1902 
1903 	return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp);
1904 }
1905 
set_cig_params_sync(struct hci_dev * hdev,void * data)1906 static int set_cig_params_sync(struct hci_dev *hdev, void *data)
1907 {
1908 	DEFINE_FLEX(struct hci_cp_le_set_cig_params, pdu, cis, num_cis, 0x1f);
1909 	u8 cig_id = PTR_UINT(data);
1910 	struct hci_conn *conn;
1911 	struct bt_iso_qos *qos;
1912 	u8 aux_num_cis = 0;
1913 	u8 cis_id;
1914 
1915 	hci_dev_lock(hdev);
1916 
1917 	conn = hci_conn_hash_lookup_cig(hdev, cig_id);
1918 	if (!conn) {
1919 		hci_dev_unlock(hdev);
1920 		return 0;
1921 	}
1922 
1923 	qos = &conn->iso_qos;
1924 	pdu->cig_id = cig_id;
1925 	hci_cpu_to_le24(qos->ucast.out.interval, pdu->c_interval);
1926 	hci_cpu_to_le24(qos->ucast.in.interval, pdu->p_interval);
1927 	pdu->sca = qos->ucast.sca;
1928 	pdu->packing = qos->ucast.packing;
1929 	pdu->framing = qos->ucast.framing;
1930 	pdu->c_latency = cpu_to_le16(qos->ucast.out.latency);
1931 	pdu->p_latency = cpu_to_le16(qos->ucast.in.latency);
1932 
1933 	/* Reprogram all CIS(s) with the same CIG, valid range are:
1934 	 * num_cis: 0x00 to 0x1F
1935 	 * cis_id: 0x00 to 0xEF
1936 	 */
1937 	for (cis_id = 0x00; cis_id < 0xf0 &&
1938 	     aux_num_cis < pdu->num_cis; cis_id++) {
1939 		struct hci_cis_params *cis;
1940 
1941 		conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id);
1942 		if (!conn)
1943 			continue;
1944 
1945 		qos = &conn->iso_qos;
1946 
1947 		cis = &pdu->cis[aux_num_cis++];
1948 		cis->cis_id = cis_id;
1949 		cis->c_sdu  = cpu_to_le16(conn->iso_qos.ucast.out.sdu);
1950 		cis->p_sdu  = cpu_to_le16(conn->iso_qos.ucast.in.sdu);
1951 		cis->c_phys = qos->ucast.out.phys ? qos->ucast.out.phys :
1952 			      qos->ucast.in.phys;
1953 		cis->p_phys = qos->ucast.in.phys ? qos->ucast.in.phys :
1954 			      qos->ucast.out.phys;
1955 		cis->c_rtn  = qos->ucast.out.rtn;
1956 		cis->p_rtn  = qos->ucast.in.rtn;
1957 	}
1958 	pdu->num_cis = aux_num_cis;
1959 
1960 	hci_dev_unlock(hdev);
1961 
1962 	if (!pdu->num_cis)
1963 		return 0;
1964 
1965 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS,
1966 				     struct_size(pdu, cis, pdu->num_cis),
1967 				     pdu, HCI_CMD_TIMEOUT);
1968 }
1969 
hci_le_set_cig_params(struct hci_conn * conn,struct bt_iso_qos * qos)1970 static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
1971 {
1972 	struct hci_dev *hdev = conn->hdev;
1973 	struct iso_list_data data;
1974 
1975 	memset(&data, 0, sizeof(data));
1976 
1977 	/* Allocate first still reconfigurable CIG if not set */
1978 	if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) {
1979 		for (data.cig = 0x00; data.cig < 0xf0; data.cig++) {
1980 			data.count = 0;
1981 
1982 			hci_conn_hash_list_state(hdev, find_cis, CIS_LINK,
1983 						 BT_CONNECT, &data);
1984 			if (data.count)
1985 				continue;
1986 
1987 			hci_conn_hash_list_state(hdev, find_cis, CIS_LINK,
1988 						 BT_CONNECTED, &data);
1989 			if (!data.count)
1990 				break;
1991 		}
1992 
1993 		if (data.cig == 0xf0)
1994 			return false;
1995 
1996 		/* Update CIG */
1997 		qos->ucast.cig = data.cig;
1998 	}
1999 
2000 	if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) {
2001 		if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig,
2002 					     qos->ucast.cis))
2003 			return false;
2004 		goto done;
2005 	}
2006 
2007 	/* Allocate first available CIS if not set */
2008 	for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0;
2009 	     data.cis++) {
2010 		if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig,
2011 					      data.cis)) {
2012 			/* Update CIS */
2013 			qos->ucast.cis = data.cis;
2014 			break;
2015 		}
2016 	}
2017 
2018 	if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET)
2019 		return false;
2020 
2021 done:
2022 	conn->iso_qos = *qos;
2023 
2024 	if (hci_cmd_sync_queue(hdev, set_cig_params_sync,
2025 			       UINT_PTR(qos->ucast.cig), NULL) < 0)
2026 		return false;
2027 
2028 	return true;
2029 }
2030 
hci_bind_cis(struct hci_dev * hdev,bdaddr_t * dst,__u8 dst_type,struct bt_iso_qos * qos,u16 timeout)2031 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
2032 			      __u8 dst_type, struct bt_iso_qos *qos,
2033 			      u16 timeout)
2034 {
2035 	struct hci_conn *cis;
2036 
2037 	cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig,
2038 				       qos->ucast.cis);
2039 	if (!cis) {
2040 		cis = hci_conn_add_unset(hdev, CIS_LINK, dst, dst_type,
2041 					 HCI_ROLE_MASTER);
2042 		if (IS_ERR(cis))
2043 			return cis;
2044 		cis->cleanup = cis_cleanup;
2045 		cis->dst_type = dst_type;
2046 		cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET;
2047 		cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET;
2048 		cis->conn_timeout = timeout;
2049 	}
2050 
2051 	if (cis->state == BT_CONNECTED)
2052 		return cis;
2053 
2054 	/* Check if CIS has been set and the settings matches */
2055 	if (cis->state == BT_BOUND &&
2056 	    !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
2057 		return cis;
2058 
2059 	/* Update LINK PHYs according to QoS preference */
2060 	cis->le_tx_phy = qos->ucast.out.phys;
2061 	cis->le_rx_phy = qos->ucast.in.phys;
2062 
2063 	/* If output interval is not set use the input interval as it cannot be
2064 	 * 0x000000.
2065 	 */
2066 	if (!qos->ucast.out.interval)
2067 		qos->ucast.out.interval = qos->ucast.in.interval;
2068 
2069 	/* If input interval is not set use the output interval as it cannot be
2070 	 * 0x000000.
2071 	 */
2072 	if (!qos->ucast.in.interval)
2073 		qos->ucast.in.interval = qos->ucast.out.interval;
2074 
2075 	/* If output latency is not set use the input latency as it cannot be
2076 	 * 0x0000.
2077 	 */
2078 	if (!qos->ucast.out.latency)
2079 		qos->ucast.out.latency = qos->ucast.in.latency;
2080 
2081 	/* If input latency is not set use the output latency as it cannot be
2082 	 * 0x0000.
2083 	 */
2084 	if (!qos->ucast.in.latency)
2085 		qos->ucast.in.latency = qos->ucast.out.latency;
2086 
2087 	if (!hci_le_set_cig_params(cis, qos)) {
2088 		hci_conn_drop(cis);
2089 		return ERR_PTR(-EINVAL);
2090 	}
2091 
2092 	hci_conn_hold(cis);
2093 	cis->state = BT_BOUND;
2094 
2095 	return cis;
2096 }
2097 
hci_iso_setup_path(struct hci_conn * conn)2098 bool hci_iso_setup_path(struct hci_conn *conn)
2099 {
2100 	struct hci_dev *hdev = conn->hdev;
2101 	struct hci_cp_le_setup_iso_path cmd;
2102 
2103 	memset(&cmd, 0, sizeof(cmd));
2104 
2105 	if (conn->iso_qos.ucast.out.sdu) {
2106 		cmd.handle = cpu_to_le16(conn->handle);
2107 		cmd.direction = 0x00; /* Input (Host to Controller) */
2108 		cmd.path = 0x00; /* HCI path if enabled */
2109 		cmd.codec = 0x03; /* Transparent Data */
2110 
2111 		if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
2112 				 &cmd) < 0)
2113 			return false;
2114 	}
2115 
2116 	if (conn->iso_qos.ucast.in.sdu) {
2117 		cmd.handle = cpu_to_le16(conn->handle);
2118 		cmd.direction = 0x01; /* Output (Controller to Host) */
2119 		cmd.path = 0x00; /* HCI path if enabled */
2120 		cmd.codec = 0x03; /* Transparent Data */
2121 
2122 		if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
2123 				 &cmd) < 0)
2124 			return false;
2125 	}
2126 
2127 	return true;
2128 }
2129 
hci_conn_check_create_cis(struct hci_conn * conn)2130 int hci_conn_check_create_cis(struct hci_conn *conn)
2131 {
2132 	if (conn->type != CIS_LINK)
2133 		return -EINVAL;
2134 
2135 	if (!conn->parent || conn->parent->state != BT_CONNECTED ||
2136 	    conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle))
2137 		return 1;
2138 
2139 	return 0;
2140 }
2141 
hci_create_cis_sync(struct hci_dev * hdev,void * data)2142 static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
2143 {
2144 	return hci_le_create_cis_sync(hdev);
2145 }
2146 
hci_le_create_cis_pending(struct hci_dev * hdev)2147 int hci_le_create_cis_pending(struct hci_dev *hdev)
2148 {
2149 	struct hci_conn *conn;
2150 	bool pending = false;
2151 
2152 	rcu_read_lock();
2153 
2154 	list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
2155 		if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) {
2156 			rcu_read_unlock();
2157 			return -EBUSY;
2158 		}
2159 
2160 		if (!hci_conn_check_create_cis(conn))
2161 			pending = true;
2162 	}
2163 
2164 	rcu_read_unlock();
2165 
2166 	if (!pending)
2167 		return 0;
2168 
2169 	/* Queue Create CIS */
2170 	return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL);
2171 }
2172 
hci_iso_qos_setup(struct hci_dev * hdev,struct hci_conn * conn,struct bt_iso_io_qos * qos,__u8 phys)2173 static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
2174 			      struct bt_iso_io_qos *qos, __u8 phys)
2175 {
2176 	/* Only set MTU if PHY is enabled */
2177 	if (!qos->sdu && qos->phys)
2178 		qos->sdu = conn->mtu;
2179 
2180 	/* Use the same PHY as ACL if set to any */
2181 	if (qos->phys == BT_ISO_PHY_ANY)
2182 		qos->phys = phys;
2183 
2184 	/* Use LE ACL connection interval if not set */
2185 	if (!qos->interval)
2186 		/* ACL interval unit in 1.25 ms to us */
2187 		qos->interval = conn->le_conn_interval * 1250;
2188 
2189 	/* Use LE ACL connection latency if not set */
2190 	if (!qos->latency)
2191 		qos->latency = conn->le_conn_latency;
2192 }
2193 
create_big_sync(struct hci_dev * hdev,void * data)2194 static int create_big_sync(struct hci_dev *hdev, void *data)
2195 {
2196 	struct hci_conn *conn = data;
2197 	struct bt_iso_qos *qos = &conn->iso_qos;
2198 	u16 interval, sync_interval = 0;
2199 	u32 flags = 0;
2200 	int err;
2201 
2202 	if (!hci_conn_valid(hdev, conn))
2203 		return -ECANCELED;
2204 
2205 	if (qos->bcast.out.phys == BIT(1))
2206 		flags |= MGMT_ADV_FLAG_SEC_2M;
2207 
2208 	/* Align intervals */
2209 	interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor;
2210 
2211 	if (qos->bcast.bis)
2212 		sync_interval = interval * 4;
2213 
2214 	err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->sid,
2215 				     conn->le_per_adv_data_len,
2216 				     conn->le_per_adv_data, flags, interval,
2217 				     interval, sync_interval);
2218 	if (err)
2219 		return err;
2220 
2221 	return hci_le_create_big(conn, &conn->iso_qos);
2222 }
2223 
hci_pa_create_sync(struct hci_dev * hdev,bdaddr_t * dst,__u8 dst_type,__u8 sid,struct bt_iso_qos * qos)2224 struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
2225 				    __u8 dst_type, __u8 sid,
2226 				    struct bt_iso_qos *qos)
2227 {
2228 	struct hci_conn *conn;
2229 
2230 	bt_dev_dbg(hdev, "dst %pMR type %d sid %d", dst, dst_type, sid);
2231 
2232 	conn = hci_conn_add_unset(hdev, PA_LINK, dst, dst_type, HCI_ROLE_SLAVE);
2233 	if (IS_ERR(conn))
2234 		return conn;
2235 
2236 	conn->iso_qos = *qos;
2237 	conn->sid = sid;
2238 	conn->state = BT_LISTEN;
2239 	conn->conn_timeout = msecs_to_jiffies(qos->bcast.sync_timeout * 10);
2240 
2241 	hci_conn_hold(conn);
2242 
2243 	hci_connect_pa_sync(hdev, conn);
2244 
2245 	return conn;
2246 }
2247 
hci_conn_big_create_sync(struct hci_dev * hdev,struct hci_conn * hcon,struct bt_iso_qos * qos,__u16 sync_handle,__u8 num_bis,__u8 bis[])2248 int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
2249 			     struct bt_iso_qos *qos, __u16 sync_handle,
2250 			     __u8 num_bis, __u8 bis[])
2251 {
2252 	int err;
2253 
2254 	if (num_bis < 0x01 || num_bis > ISO_MAX_NUM_BIS)
2255 		return -EINVAL;
2256 
2257 	err = qos_set_big(hdev, qos);
2258 	if (err)
2259 		return err;
2260 
2261 	if (hcon) {
2262 		/* Update hcon QoS */
2263 		hcon->iso_qos = *qos;
2264 
2265 		hcon->num_bis = num_bis;
2266 		memcpy(hcon->bis, bis, num_bis);
2267 		hcon->conn_timeout = msecs_to_jiffies(qos->bcast.timeout * 10);
2268 	}
2269 
2270 	return hci_connect_big_sync(hdev, hcon);
2271 }
2272 
create_big_complete(struct hci_dev * hdev,void * data,int err)2273 static void create_big_complete(struct hci_dev *hdev, void *data, int err)
2274 {
2275 	struct hci_conn *conn = data;
2276 
2277 	bt_dev_dbg(hdev, "conn %p", conn);
2278 
2279 	if (err == -ECANCELED)
2280 		goto done;
2281 
2282 	hci_dev_lock(hdev);
2283 
2284 	if (!hci_conn_valid(hdev, conn))
2285 		goto unlock;
2286 
2287 	if (err) {
2288 		bt_dev_err(hdev, "Unable to create BIG: %d", err);
2289 		hci_connect_cfm(conn, err);
2290 		hci_conn_del(conn);
2291 	}
2292 
2293 unlock:
2294 	hci_dev_unlock(hdev);
2295 done:
2296 	hci_conn_put(conn);
2297 }
2298 
hci_bind_bis(struct hci_dev * hdev,bdaddr_t * dst,__u8 sid,struct bt_iso_qos * qos,__u8 base_len,__u8 * base,u16 timeout)2299 struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid,
2300 			      struct bt_iso_qos *qos,
2301 			      __u8 base_len, __u8 *base, u16 timeout)
2302 {
2303 	struct hci_conn *conn;
2304 	struct hci_conn *parent;
2305 	__u8 eir[HCI_MAX_PER_AD_LENGTH];
2306 	struct hci_link *link;
2307 
2308 	/* Look for any BIS that is open for rebinding */
2309 	conn = hci_conn_hash_lookup_big_state(hdev, qos->bcast.big, BT_OPEN,
2310 					      HCI_ROLE_MASTER);
2311 	if (conn) {
2312 		memcpy(qos, &conn->iso_qos, sizeof(*qos));
2313 		conn->state = BT_CONNECTED;
2314 		return conn;
2315 	}
2316 
2317 	if (base_len && base)
2318 		base_len = eir_append_service_data(eir, 0,  0x1851,
2319 						   base, base_len);
2320 
2321 	/* We need hci_conn object using the BDADDR_ANY as dst */
2322 	conn = hci_add_bis(hdev, dst, sid, qos, base_len, eir, timeout);
2323 	if (IS_ERR(conn))
2324 		return conn;
2325 
2326 	/* Update LINK PHYs according to QoS preference */
2327 	conn->le_tx_def_phys = qos->bcast.out.phys;
2328 
2329 	/* Add Basic Announcement into Peridic Adv Data if BASE is set */
2330 	if (base_len && base) {
2331 		memcpy(conn->le_per_adv_data,  eir, sizeof(eir));
2332 		conn->le_per_adv_data_len = base_len;
2333 	}
2334 
2335 	hci_iso_qos_setup(hdev, conn, &qos->bcast.out,
2336 			  conn->le_tx_def_phys ? conn->le_tx_def_phys :
2337 			  hdev->le_tx_def_phys);
2338 
2339 	conn->iso_qos = *qos;
2340 	conn->state = BT_BOUND;
2341 
2342 	/* Link BISes together */
2343 	parent = hci_conn_hash_lookup_big(hdev,
2344 					  conn->iso_qos.bcast.big);
2345 	if (parent && parent != conn) {
2346 		link = hci_conn_link(parent, conn);
2347 		hci_conn_drop(conn);
2348 		if (!link)
2349 			return ERR_PTR(-ENOLINK);
2350 	}
2351 
2352 	return conn;
2353 }
2354 
hci_past_bis(struct hci_conn * conn,bdaddr_t * dst,__u8 dst_type)2355 int hci_past_bis(struct hci_conn *conn, bdaddr_t *dst, __u8 dst_type)
2356 {
2357 	struct hci_conn *le;
2358 
2359 	/* Lookup existing LE connection to rebind to */
2360 	le = hci_conn_hash_lookup_le(conn->hdev, dst, dst_type);
2361 	if (!le)
2362 		return -EINVAL;
2363 
2364 	return hci_past_sync(conn, le);
2365 }
2366 
bis_mark_per_adv(struct hci_conn * conn,void * data)2367 static void bis_mark_per_adv(struct hci_conn *conn, void *data)
2368 {
2369 	struct iso_list_data *d = data;
2370 
2371 	/* Skip if not broadcast/ANY address */
2372 	if (bacmp(&conn->dst, BDADDR_ANY))
2373 		return;
2374 
2375 	if (d->big != conn->iso_qos.bcast.big ||
2376 	    d->bis == BT_ISO_QOS_BIS_UNSET ||
2377 	    d->bis != conn->iso_qos.bcast.bis)
2378 		return;
2379 
2380 	set_bit(HCI_CONN_PER_ADV, &conn->flags);
2381 }
2382 
hci_connect_bis(struct hci_dev * hdev,bdaddr_t * dst,__u8 dst_type,__u8 sid,struct bt_iso_qos * qos,__u8 base_len,__u8 * base,u16 timeout)2383 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
2384 				 __u8 dst_type, __u8 sid,
2385 				 struct bt_iso_qos *qos,
2386 				 __u8 base_len, __u8 *base, u16 timeout)
2387 {
2388 	struct hci_conn *conn;
2389 	int err;
2390 	struct iso_list_data data;
2391 
2392 	conn = hci_bind_bis(hdev, dst, sid, qos, base_len, base, timeout);
2393 	if (IS_ERR(conn))
2394 		return conn;
2395 
2396 	if (conn->state == BT_CONNECTED)
2397 		return conn;
2398 
2399 	/* Check if SID needs to be allocated then search for the first
2400 	 * available.
2401 	 */
2402 	if (conn->sid == HCI_SID_INVALID) {
2403 		u8 sid;
2404 
2405 		for (sid = 0; sid <= 0x0f; sid++) {
2406 			if (!hci_find_adv_sid(hdev, sid)) {
2407 				conn->sid = sid;
2408 				break;
2409 			}
2410 		}
2411 	}
2412 
2413 	data.big = qos->bcast.big;
2414 	data.bis = qos->bcast.bis;
2415 
2416 	/* Set HCI_CONN_PER_ADV for all bound connections, to mark that
2417 	 * the start periodic advertising and create BIG commands have
2418 	 * been queued
2419 	 */
2420 	hci_conn_hash_list_state(hdev, bis_mark_per_adv, BIS_LINK,
2421 				 BT_BOUND, &data);
2422 
2423 	/* Queue start periodic advertising and create BIG */
2424 	err = hci_cmd_sync_queue(hdev, create_big_sync, hci_conn_get(conn),
2425 				 create_big_complete);
2426 	if (err < 0) {
2427 		hci_conn_drop(conn);
2428 		hci_conn_put(conn);
2429 		return ERR_PTR(err);
2430 	}
2431 
2432 	return conn;
2433 }
2434 
hci_connect_cis(struct hci_dev * hdev,bdaddr_t * dst,__u8 dst_type,struct bt_iso_qos * qos,u16 timeout)2435 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
2436 				 __u8 dst_type, struct bt_iso_qos *qos,
2437 				 u16 timeout)
2438 {
2439 	struct hci_conn *le;
2440 	struct hci_conn *cis;
2441 	struct hci_link *link;
2442 
2443 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2444 		le = hci_connect_le(hdev, dst, dst_type, false,
2445 				    BT_SECURITY_LOW,
2446 				    HCI_LE_CONN_TIMEOUT,
2447 				    HCI_ROLE_SLAVE, 0, 0);
2448 	else
2449 		le = hci_connect_le_scan(hdev, dst, dst_type,
2450 					 BT_SECURITY_LOW,
2451 					 HCI_LE_CONN_TIMEOUT,
2452 					 CONN_REASON_ISO_CONNECT);
2453 	if (IS_ERR(le))
2454 		return le;
2455 
2456 	hci_iso_qos_setup(hdev, le, &qos->ucast.out,
2457 			  le->le_tx_def_phys ? le->le_tx_def_phys :
2458 			  hdev->le_tx_def_phys);
2459 	hci_iso_qos_setup(hdev, le, &qos->ucast.in,
2460 			  le->le_rx_def_phys ? le->le_rx_def_phys :
2461 			  hdev->le_rx_def_phys);
2462 
2463 	cis = hci_bind_cis(hdev, dst, dst_type, qos, timeout);
2464 	if (IS_ERR(cis)) {
2465 		hci_conn_drop(le);
2466 		return cis;
2467 	}
2468 
2469 	link = hci_conn_link(le, cis);
2470 	hci_conn_drop(cis);
2471 	if (!link) {
2472 		hci_conn_drop(le);
2473 		return ERR_PTR(-ENOLINK);
2474 	}
2475 
2476 	cis->state = BT_CONNECT;
2477 
2478 	hci_le_create_cis_pending(hdev);
2479 
2480 	return cis;
2481 }
2482 
2483 /* Check link security requirement */
hci_conn_check_link_mode(struct hci_conn * conn)2484 int hci_conn_check_link_mode(struct hci_conn *conn)
2485 {
2486 	BT_DBG("hcon %p", conn);
2487 
2488 	/* In Secure Connections Only mode, it is required that Secure
2489 	 * Connections is used and the link is encrypted with AES-CCM
2490 	 * using a P-256 authenticated combination key.
2491 	 */
2492 	if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
2493 		if (!hci_conn_sc_enabled(conn) ||
2494 		    !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2495 		    conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
2496 			return 0;
2497 	}
2498 
2499 	 /* AES encryption is required for Level 4:
2500 	  *
2501 	  * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
2502 	  * page 1319:
2503 	  *
2504 	  * 128-bit equivalent strength for link and encryption keys
2505 	  * required using FIPS approved algorithms (E0 not allowed,
2506 	  * SAFER+ not allowed, and P-192 not allowed; encryption key
2507 	  * not shortened)
2508 	  */
2509 	if (conn->sec_level == BT_SECURITY_FIPS &&
2510 	    !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
2511 		bt_dev_err(conn->hdev,
2512 			   "Invalid security: Missing AES-CCM usage");
2513 		return 0;
2514 	}
2515 
2516 	if (hci_conn_ssp_enabled(conn) &&
2517 	    !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2518 		return 0;
2519 
2520 	return 1;
2521 }
2522 
2523 /* Authenticate remote device */
hci_conn_auth(struct hci_conn * conn,__u8 sec_level,__u8 auth_type)2524 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
2525 {
2526 	BT_DBG("hcon %p", conn);
2527 
2528 	if (conn->pending_sec_level > sec_level)
2529 		sec_level = conn->pending_sec_level;
2530 
2531 	if (sec_level > conn->sec_level)
2532 		conn->pending_sec_level = sec_level;
2533 	else if (test_bit(HCI_CONN_AUTH, &conn->flags))
2534 		return 1;
2535 
2536 	/* Make sure we preserve an existing MITM requirement*/
2537 	auth_type |= (conn->auth_type & 0x01);
2538 
2539 	conn->auth_type = auth_type;
2540 
2541 	if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
2542 		struct hci_cp_auth_requested cp;
2543 
2544 		cp.handle = cpu_to_le16(conn->handle);
2545 		hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
2546 			     sizeof(cp), &cp);
2547 
2548 		/* Set the ENCRYPT_PEND to trigger encryption after
2549 		 * authentication.
2550 		 */
2551 		if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2552 			set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2553 	}
2554 
2555 	return 0;
2556 }
2557 
2558 /* Encrypt the link */
hci_conn_encrypt(struct hci_conn * conn)2559 static void hci_conn_encrypt(struct hci_conn *conn)
2560 {
2561 	BT_DBG("hcon %p", conn);
2562 
2563 	if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
2564 		struct hci_cp_set_conn_encrypt cp;
2565 		cp.handle  = cpu_to_le16(conn->handle);
2566 		cp.encrypt = 0x01;
2567 		hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
2568 			     &cp);
2569 	}
2570 }
2571 
2572 /* Enable security */
hci_conn_security(struct hci_conn * conn,__u8 sec_level,__u8 auth_type,bool initiator)2573 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
2574 		      bool initiator)
2575 {
2576 	BT_DBG("hcon %p", conn);
2577 
2578 	if (conn->type == LE_LINK)
2579 		return smp_conn_security(conn, sec_level);
2580 
2581 	/* For sdp we don't need the link key. */
2582 	if (sec_level == BT_SECURITY_SDP)
2583 		return 1;
2584 
2585 	/* For non 2.1 devices and low security level we don't need the link
2586 	   key. */
2587 	if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
2588 		return 1;
2589 
2590 	/* For other security levels we need the link key. */
2591 	if (!test_bit(HCI_CONN_AUTH, &conn->flags))
2592 		goto auth;
2593 
2594 	switch (conn->key_type) {
2595 	case HCI_LK_AUTH_COMBINATION_P256:
2596 		/* An authenticated FIPS approved combination key has
2597 		 * sufficient security for security level 4 or lower.
2598 		 */
2599 		if (sec_level <= BT_SECURITY_FIPS)
2600 			goto encrypt;
2601 		break;
2602 	case HCI_LK_AUTH_COMBINATION_P192:
2603 		/* An authenticated combination key has sufficient security for
2604 		 * security level 3 or lower.
2605 		 */
2606 		if (sec_level <= BT_SECURITY_HIGH)
2607 			goto encrypt;
2608 		break;
2609 	case HCI_LK_UNAUTH_COMBINATION_P192:
2610 	case HCI_LK_UNAUTH_COMBINATION_P256:
2611 		/* An unauthenticated combination key has sufficient security
2612 		 * for security level 2 or lower.
2613 		 */
2614 		if (sec_level <= BT_SECURITY_MEDIUM)
2615 			goto encrypt;
2616 		break;
2617 	case HCI_LK_COMBINATION:
2618 		/* A combination key has always sufficient security for the
2619 		 * security levels 2 or lower. High security level requires the
2620 		 * combination key is generated using maximum PIN code length
2621 		 * (16). For pre 2.1 units.
2622 		 */
2623 		if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16)
2624 			goto encrypt;
2625 		break;
2626 	default:
2627 		break;
2628 	}
2629 
2630 auth:
2631 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
2632 		return 0;
2633 
2634 	if (initiator)
2635 		set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2636 
2637 	if (!hci_conn_auth(conn, sec_level, auth_type))
2638 		return 0;
2639 
2640 encrypt:
2641 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
2642 		/* Ensure that the encryption key size has been read,
2643 		 * otherwise stall the upper layer responses.
2644 		 */
2645 		if (!conn->enc_key_size)
2646 			return 0;
2647 
2648 		/* Nothing else needed, all requirements are met */
2649 		return 1;
2650 	}
2651 
2652 	hci_conn_encrypt(conn);
2653 	return 0;
2654 }
2655 EXPORT_SYMBOL(hci_conn_security);
2656 
2657 /* Check secure link requirement */
hci_conn_check_secure(struct hci_conn * conn,__u8 sec_level)2658 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
2659 {
2660 	BT_DBG("hcon %p", conn);
2661 
2662 	/* Accept if non-secure or higher security level is required */
2663 	if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
2664 		return 1;
2665 
2666 	/* Accept if secure or higher security level is already present */
2667 	if (conn->sec_level == BT_SECURITY_HIGH ||
2668 	    conn->sec_level == BT_SECURITY_FIPS)
2669 		return 1;
2670 
2671 	/* Reject not secure link */
2672 	return 0;
2673 }
2674 EXPORT_SYMBOL(hci_conn_check_secure);
2675 
2676 /* Switch role */
hci_conn_switch_role(struct hci_conn * conn,__u8 role)2677 int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
2678 {
2679 	BT_DBG("hcon %p", conn);
2680 
2681 	if (role == conn->role)
2682 		return 1;
2683 
2684 	if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
2685 		struct hci_cp_switch_role cp;
2686 		bacpy(&cp.bdaddr, &conn->dst);
2687 		cp.role = role;
2688 		hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
2689 	}
2690 
2691 	return 0;
2692 }
2693 EXPORT_SYMBOL(hci_conn_switch_role);
2694 
2695 /* Enter active mode */
hci_conn_enter_active_mode(struct hci_conn * conn,__u8 force_active)2696 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
2697 {
2698 	struct hci_dev *hdev = conn->hdev;
2699 
2700 	BT_DBG("hcon %p mode %d", conn, conn->mode);
2701 
2702 	if (conn->mode != HCI_CM_SNIFF)
2703 		goto timer;
2704 
2705 	if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
2706 		goto timer;
2707 
2708 	if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
2709 		struct hci_cp_exit_sniff_mode cp;
2710 		cp.handle = cpu_to_le16(conn->handle);
2711 		hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
2712 	}
2713 
2714 timer:
2715 	if (hdev->idle_timeout > 0)
2716 		mod_delayed_work(hdev->workqueue, &conn->idle_work,
2717 				 msecs_to_jiffies(hdev->idle_timeout));
2718 }
2719 
2720 /* Drop all connection on the device */
hci_conn_hash_flush(struct hci_dev * hdev)2721 void hci_conn_hash_flush(struct hci_dev *hdev)
2722 {
2723 	struct list_head *head = &hdev->conn_hash.list;
2724 	struct hci_conn *conn;
2725 
2726 	BT_DBG("hdev %s", hdev->name);
2727 
2728 	/* We should not traverse the list here, because hci_conn_del
2729 	 * can remove extra links, which may cause the list traversal
2730 	 * to hit items that have already been released.
2731 	 */
2732 	while ((conn = list_first_entry_or_null(head,
2733 						struct hci_conn,
2734 						list)) != NULL) {
2735 		conn->state = BT_CLOSED;
2736 		hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM);
2737 		hci_conn_del(conn);
2738 	}
2739 }
2740 
get_link_mode(struct hci_conn * conn)2741 static u32 get_link_mode(struct hci_conn *conn)
2742 {
2743 	u32 link_mode = 0;
2744 
2745 	if (conn->role == HCI_ROLE_MASTER)
2746 		link_mode |= HCI_LM_MASTER;
2747 
2748 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2749 		link_mode |= HCI_LM_ENCRYPT;
2750 
2751 	if (test_bit(HCI_CONN_AUTH, &conn->flags))
2752 		link_mode |= HCI_LM_AUTH;
2753 
2754 	if (test_bit(HCI_CONN_SECURE, &conn->flags))
2755 		link_mode |= HCI_LM_SECURE;
2756 
2757 	if (test_bit(HCI_CONN_FIPS, &conn->flags))
2758 		link_mode |= HCI_LM_FIPS;
2759 
2760 	return link_mode;
2761 }
2762 
hci_get_conn_list(void __user * arg)2763 int hci_get_conn_list(void __user *arg)
2764 {
2765 	struct hci_conn *c;
2766 	struct hci_conn_list_req req, *cl;
2767 	struct hci_conn_info *ci;
2768 	struct hci_dev *hdev;
2769 	int n = 0, size, err;
2770 
2771 	if (copy_from_user(&req, arg, sizeof(req)))
2772 		return -EFAULT;
2773 
2774 	if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
2775 		return -EINVAL;
2776 
2777 	size = sizeof(req) + req.conn_num * sizeof(*ci);
2778 
2779 	cl = kmalloc(size, GFP_KERNEL);
2780 	if (!cl)
2781 		return -ENOMEM;
2782 
2783 	hdev = hci_dev_get(req.dev_id);
2784 	if (!hdev) {
2785 		kfree(cl);
2786 		return -ENODEV;
2787 	}
2788 
2789 	ci = cl->conn_info;
2790 
2791 	hci_dev_lock(hdev);
2792 	list_for_each_entry(c, &hdev->conn_hash.list, list) {
2793 		bacpy(&(ci + n)->bdaddr, &c->dst);
2794 		(ci + n)->handle = c->handle;
2795 		(ci + n)->type  = c->type;
2796 		(ci + n)->out   = c->out;
2797 		(ci + n)->state = c->state;
2798 		(ci + n)->link_mode = get_link_mode(c);
2799 		if (++n >= req.conn_num)
2800 			break;
2801 	}
2802 	hci_dev_unlock(hdev);
2803 
2804 	cl->dev_id = hdev->id;
2805 	cl->conn_num = n;
2806 	size = sizeof(req) + n * sizeof(*ci);
2807 
2808 	hci_dev_put(hdev);
2809 
2810 	err = copy_to_user(arg, cl, size);
2811 	kfree(cl);
2812 
2813 	return err ? -EFAULT : 0;
2814 }
2815 
hci_get_conn_info(struct hci_dev * hdev,void __user * arg)2816 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
2817 {
2818 	struct hci_conn_info_req req;
2819 	struct hci_conn_info ci;
2820 	struct hci_conn *conn;
2821 	char __user *ptr = arg + sizeof(req);
2822 
2823 	if (copy_from_user(&req, arg, sizeof(req)))
2824 		return -EFAULT;
2825 
2826 	hci_dev_lock(hdev);
2827 	conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
2828 	if (conn) {
2829 		bacpy(&ci.bdaddr, &conn->dst);
2830 		ci.handle = conn->handle;
2831 		ci.type  = conn->type;
2832 		ci.out   = conn->out;
2833 		ci.state = conn->state;
2834 		ci.link_mode = get_link_mode(conn);
2835 	}
2836 	hci_dev_unlock(hdev);
2837 
2838 	if (!conn)
2839 		return -ENOENT;
2840 
2841 	return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
2842 }
2843 
hci_get_auth_info(struct hci_dev * hdev,void __user * arg)2844 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
2845 {
2846 	struct hci_auth_info_req req;
2847 	struct hci_conn *conn;
2848 
2849 	if (copy_from_user(&req, arg, sizeof(req)))
2850 		return -EFAULT;
2851 
2852 	hci_dev_lock(hdev);
2853 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
2854 	if (conn)
2855 		req.type = conn->auth_type;
2856 	hci_dev_unlock(hdev);
2857 
2858 	if (!conn)
2859 		return -ENOENT;
2860 
2861 	return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
2862 }
2863 
hci_chan_create(struct hci_conn * conn)2864 struct hci_chan *hci_chan_create(struct hci_conn *conn)
2865 {
2866 	struct hci_dev *hdev = conn->hdev;
2867 	struct hci_chan *chan;
2868 
2869 	BT_DBG("%s hcon %p", hdev->name, conn);
2870 
2871 	if (test_bit(HCI_CONN_DROP, &conn->flags)) {
2872 		BT_DBG("Refusing to create new hci_chan");
2873 		return NULL;
2874 	}
2875 
2876 	chan = kzalloc_obj(*chan);
2877 	if (!chan)
2878 		return NULL;
2879 
2880 	chan->conn = hci_conn_get(conn);
2881 	skb_queue_head_init(&chan->data_q);
2882 	chan->state = BT_CONNECTED;
2883 
2884 	list_add_rcu(&chan->list, &conn->chan_list);
2885 
2886 	return chan;
2887 }
2888 
hci_chan_del(struct hci_chan * chan)2889 void hci_chan_del(struct hci_chan *chan)
2890 {
2891 	struct hci_conn *conn = chan->conn;
2892 	struct hci_dev *hdev = conn->hdev;
2893 
2894 	BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
2895 
2896 	list_del_rcu(&chan->list);
2897 
2898 	synchronize_rcu();
2899 
2900 	/* Prevent new hci_chan's to be created for this hci_conn */
2901 	set_bit(HCI_CONN_DROP, &conn->flags);
2902 
2903 	hci_conn_put(conn);
2904 
2905 	skb_queue_purge(&chan->data_q);
2906 	kfree(chan);
2907 }
2908 
hci_chan_list_flush(struct hci_conn * conn)2909 void hci_chan_list_flush(struct hci_conn *conn)
2910 {
2911 	struct hci_chan *chan, *n;
2912 
2913 	BT_DBG("hcon %p", conn);
2914 
2915 	list_for_each_entry_safe(chan, n, &conn->chan_list, list)
2916 		hci_chan_del(chan);
2917 }
2918 
__hci_chan_lookup_handle(struct hci_conn * hcon,__u16 handle)2919 static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
2920 						 __u16 handle)
2921 {
2922 	struct hci_chan *hchan;
2923 
2924 	list_for_each_entry(hchan, &hcon->chan_list, list) {
2925 		if (hchan->handle == handle)
2926 			return hchan;
2927 	}
2928 
2929 	return NULL;
2930 }
2931 
hci_chan_lookup_handle(struct hci_dev * hdev,__u16 handle)2932 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
2933 {
2934 	struct hci_conn_hash *h = &hdev->conn_hash;
2935 	struct hci_conn *hcon;
2936 	struct hci_chan *hchan = NULL;
2937 
2938 	rcu_read_lock();
2939 
2940 	list_for_each_entry_rcu(hcon, &h->list, list) {
2941 		hchan = __hci_chan_lookup_handle(hcon, handle);
2942 		if (hchan)
2943 			break;
2944 	}
2945 
2946 	rcu_read_unlock();
2947 
2948 	return hchan;
2949 }
2950 
hci_conn_get_phy(struct hci_conn * conn)2951 u32 hci_conn_get_phy(struct hci_conn *conn)
2952 {
2953 	u32 phys = 0;
2954 
2955 	/* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
2956 	 * Table 6.2: Packets defined for synchronous, asynchronous, and
2957 	 * CPB logical transport types.
2958 	 */
2959 	switch (conn->type) {
2960 	case SCO_LINK:
2961 		/* SCO logical transport (1 Mb/s):
2962 		 * HV1, HV2, HV3 and DV.
2963 		 */
2964 		phys |= BT_PHY_BR_1M_1SLOT;
2965 
2966 		break;
2967 
2968 	case ACL_LINK:
2969 		/* ACL logical transport (1 Mb/s) ptt=0:
2970 		 * DH1, DM3, DH3, DM5 and DH5.
2971 		 */
2972 		phys |= BT_PHY_BR_1M_1SLOT;
2973 
2974 		if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
2975 			phys |= BT_PHY_BR_1M_3SLOT;
2976 
2977 		if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
2978 			phys |= BT_PHY_BR_1M_5SLOT;
2979 
2980 		/* ACL logical transport (2 Mb/s) ptt=1:
2981 		 * 2-DH1, 2-DH3 and 2-DH5.
2982 		 */
2983 		if (!(conn->pkt_type & HCI_2DH1))
2984 			phys |= BT_PHY_EDR_2M_1SLOT;
2985 
2986 		if (!(conn->pkt_type & HCI_2DH3))
2987 			phys |= BT_PHY_EDR_2M_3SLOT;
2988 
2989 		if (!(conn->pkt_type & HCI_2DH5))
2990 			phys |= BT_PHY_EDR_2M_5SLOT;
2991 
2992 		/* ACL logical transport (3 Mb/s) ptt=1:
2993 		 * 3-DH1, 3-DH3 and 3-DH5.
2994 		 */
2995 		if (!(conn->pkt_type & HCI_3DH1))
2996 			phys |= BT_PHY_EDR_3M_1SLOT;
2997 
2998 		if (!(conn->pkt_type & HCI_3DH3))
2999 			phys |= BT_PHY_EDR_3M_3SLOT;
3000 
3001 		if (!(conn->pkt_type & HCI_3DH5))
3002 			phys |= BT_PHY_EDR_3M_5SLOT;
3003 
3004 		break;
3005 
3006 	case ESCO_LINK:
3007 		/* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
3008 		phys |= BT_PHY_BR_1M_1SLOT;
3009 
3010 		if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
3011 			phys |= BT_PHY_BR_1M_3SLOT;
3012 
3013 		/* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
3014 		if (!(conn->pkt_type & ESCO_2EV3))
3015 			phys |= BT_PHY_EDR_2M_1SLOT;
3016 
3017 		if (!(conn->pkt_type & ESCO_2EV5))
3018 			phys |= BT_PHY_EDR_2M_3SLOT;
3019 
3020 		/* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
3021 		if (!(conn->pkt_type & ESCO_3EV3))
3022 			phys |= BT_PHY_EDR_3M_1SLOT;
3023 
3024 		if (!(conn->pkt_type & ESCO_3EV5))
3025 			phys |= BT_PHY_EDR_3M_3SLOT;
3026 
3027 		break;
3028 
3029 	case LE_LINK:
3030 		if (conn->le_tx_def_phys & HCI_LE_SET_PHY_1M)
3031 			phys |= BT_PHY_LE_1M_TX;
3032 
3033 		if (conn->le_rx_def_phys & HCI_LE_SET_PHY_1M)
3034 			phys |= BT_PHY_LE_1M_RX;
3035 
3036 		if (conn->le_tx_def_phys & HCI_LE_SET_PHY_2M)
3037 			phys |= BT_PHY_LE_2M_TX;
3038 
3039 		if (conn->le_rx_def_phys & HCI_LE_SET_PHY_2M)
3040 			phys |= BT_PHY_LE_2M_RX;
3041 
3042 		if (conn->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
3043 			phys |= BT_PHY_LE_CODED_TX;
3044 
3045 		if (conn->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
3046 			phys |= BT_PHY_LE_CODED_RX;
3047 
3048 		break;
3049 	}
3050 
3051 	return phys;
3052 }
3053 
bt_phy_pkt_type(struct hci_conn * conn,u32 phys)3054 static u16 bt_phy_pkt_type(struct hci_conn *conn, u32 phys)
3055 {
3056 	u16 pkt_type = conn->pkt_type;
3057 
3058 	if (phys & BT_PHY_BR_1M_3SLOT)
3059 		pkt_type |= HCI_DM3 | HCI_DH3;
3060 	else
3061 		pkt_type &= ~(HCI_DM3 | HCI_DH3);
3062 
3063 	if (phys & BT_PHY_BR_1M_5SLOT)
3064 		pkt_type |= HCI_DM5 | HCI_DH5;
3065 	else
3066 		pkt_type &= ~(HCI_DM5 | HCI_DH5);
3067 
3068 	if (phys & BT_PHY_EDR_2M_1SLOT)
3069 		pkt_type &= ~HCI_2DH1;
3070 	else
3071 		pkt_type |= HCI_2DH1;
3072 
3073 	if (phys & BT_PHY_EDR_2M_3SLOT)
3074 		pkt_type &= ~HCI_2DH3;
3075 	else
3076 		pkt_type |= HCI_2DH3;
3077 
3078 	if (phys & BT_PHY_EDR_2M_5SLOT)
3079 		pkt_type &= ~HCI_2DH5;
3080 	else
3081 		pkt_type |= HCI_2DH5;
3082 
3083 	if (phys & BT_PHY_EDR_3M_1SLOT)
3084 		pkt_type &= ~HCI_3DH1;
3085 	else
3086 		pkt_type |= HCI_3DH1;
3087 
3088 	if (phys & BT_PHY_EDR_3M_3SLOT)
3089 		pkt_type &= ~HCI_3DH3;
3090 	else
3091 		pkt_type |= HCI_3DH3;
3092 
3093 	if (phys & BT_PHY_EDR_3M_5SLOT)
3094 		pkt_type &= ~HCI_3DH5;
3095 	else
3096 		pkt_type |= HCI_3DH5;
3097 
3098 	return pkt_type;
3099 }
3100 
bt_phy_le_phy(u32 phys,u8 * tx_phys,u8 * rx_phys)3101 static int bt_phy_le_phy(u32 phys, u8 *tx_phys, u8 *rx_phys)
3102 {
3103 	if (!tx_phys || !rx_phys)
3104 		return -EINVAL;
3105 
3106 	*tx_phys = 0;
3107 	*rx_phys = 0;
3108 
3109 	if (phys & BT_PHY_LE_1M_TX)
3110 		*tx_phys |= HCI_LE_SET_PHY_1M;
3111 
3112 	if (phys & BT_PHY_LE_1M_RX)
3113 		*rx_phys |= HCI_LE_SET_PHY_1M;
3114 
3115 	if (phys & BT_PHY_LE_2M_TX)
3116 		*tx_phys |= HCI_LE_SET_PHY_2M;
3117 
3118 	if (phys & BT_PHY_LE_2M_RX)
3119 		*rx_phys |= HCI_LE_SET_PHY_2M;
3120 
3121 	if (phys & BT_PHY_LE_CODED_TX)
3122 		*tx_phys |= HCI_LE_SET_PHY_CODED;
3123 
3124 	if (phys & BT_PHY_LE_CODED_RX)
3125 		*rx_phys |= HCI_LE_SET_PHY_CODED;
3126 
3127 	return 0;
3128 }
3129 
hci_conn_set_phy(struct hci_conn * conn,u32 phys)3130 int hci_conn_set_phy(struct hci_conn *conn, u32 phys)
3131 {
3132 	u8 tx_phys, rx_phys;
3133 
3134 	switch (conn->type) {
3135 	case SCO_LINK:
3136 	case ESCO_LINK:
3137 		return -EINVAL;
3138 	case ACL_LINK:
3139 		/* Only allow setting BR/EDR PHYs if link type is ACL */
3140 		if (phys & ~BT_PHY_BREDR_MASK)
3141 			return -EINVAL;
3142 
3143 		return hci_acl_change_pkt_type(conn,
3144 					       bt_phy_pkt_type(conn, phys));
3145 	case LE_LINK:
3146 		/* Only allow setting LE PHYs if link type is LE */
3147 		if (phys & ~BT_PHY_LE_MASK)
3148 			return -EINVAL;
3149 
3150 		if (bt_phy_le_phy(phys, &tx_phys, &rx_phys))
3151 			return -EINVAL;
3152 
3153 		return hci_le_set_phy(conn, tx_phys, rx_phys);
3154 	default:
3155 		return -EINVAL;
3156 	}
3157 }
3158 
abort_conn_sync(struct hci_dev * hdev,void * data)3159 static int abort_conn_sync(struct hci_dev *hdev, void *data)
3160 {
3161 	struct hci_conn *conn = data;
3162 
3163 	if (!hci_conn_valid(hdev, conn))
3164 		return -ECANCELED;
3165 
3166 	return hci_abort_conn_sync(hdev, conn, conn->abort_reason);
3167 }
3168 
hci_abort_conn(struct hci_conn * conn,u8 reason)3169 int hci_abort_conn(struct hci_conn *conn, u8 reason)
3170 {
3171 	struct hci_dev *hdev = conn->hdev;
3172 	int err;
3173 
3174 	/* If abort_reason has already been set it means the connection is
3175 	 * already being aborted so don't attempt to overwrite it.
3176 	 */
3177 	if (conn->abort_reason)
3178 		return 0;
3179 
3180 	bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason);
3181 
3182 	conn->abort_reason = reason;
3183 
3184 	/* If the connection is pending check the command opcode since that
3185 	 * might be blocking on hci_cmd_sync_work while waiting its respective
3186 	 * event so we need to hci_cmd_sync_cancel to cancel it.
3187 	 *
3188 	 * hci_connect_le serializes the connection attempts so only one
3189 	 * connection can be in BT_CONNECT at time.
3190 	 */
3191 	if (conn->state == BT_CONNECT && READ_ONCE(hdev->req_status) == HCI_REQ_PEND) {
3192 		switch (hci_skb_event(hdev->sent_cmd)) {
3193 		case HCI_EV_CONN_COMPLETE:
3194 		case HCI_EV_LE_CONN_COMPLETE:
3195 		case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
3196 		case HCI_EVT_LE_CIS_ESTABLISHED:
3197 			hci_cmd_sync_cancel(hdev, ECANCELED);
3198 			break;
3199 		}
3200 	/* Cancel connect attempt if still queued/pending */
3201 	} else if (!hci_cancel_connect_sync(hdev, conn)) {
3202 		return 0;
3203 	}
3204 
3205 	/* Run immediately if on cmd_sync_work since this may be called
3206 	 * as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does
3207 	 * already queue its callback on cmd_sync_work.
3208 	 */
3209 	err = hci_cmd_sync_run_once(hdev, abort_conn_sync, conn, NULL);
3210 	return (err == -EEXIST) ? 0 : err;
3211 }
3212 
hci_setup_tx_timestamp(struct sk_buff * skb,size_t key_offset,const struct sockcm_cookie * sockc)3213 void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
3214 			    const struct sockcm_cookie *sockc)
3215 {
3216 	struct sock *sk = skb ? skb->sk : NULL;
3217 	int key;
3218 
3219 	/* This shall be called on a single skb of those generated by user
3220 	 * sendmsg(), and only when the sendmsg() does not return error to
3221 	 * user. This is required for keeping the tskey that increments here in
3222 	 * sync with possible sendmsg() counting by user.
3223 	 *
3224 	 * Stream sockets shall set key_offset to sendmsg() length in bytes
3225 	 * and call with the last fragment, others to 1 and first fragment.
3226 	 */
3227 
3228 	if (!skb || !sockc || !sk || !key_offset)
3229 		return;
3230 
3231 	sock_tx_timestamp(sk, sockc, &skb_shinfo(skb)->tx_flags);
3232 
3233 	if (sk->sk_type == SOCK_STREAM)
3234 		key = atomic_add_return(key_offset, &sk->sk_tskey);
3235 
3236 	if (sockc->tsflags & SOF_TIMESTAMPING_OPT_ID &&
3237 	    sockc->tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK) {
3238 		if (sockc->tsflags & SOCKCM_FLAG_TS_OPT_ID) {
3239 			skb_shinfo(skb)->tskey = sockc->ts_opt_id;
3240 		} else {
3241 			if (sk->sk_type != SOCK_STREAM)
3242 				key = atomic_inc_return(&sk->sk_tskey);
3243 			skb_shinfo(skb)->tskey = key - 1;
3244 		}
3245 	}
3246 }
3247 
hci_conn_tx_queue(struct hci_conn * conn,struct sk_buff * skb)3248 void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb)
3249 {
3250 	struct tx_queue *comp = &conn->tx_q;
3251 	bool track = false;
3252 
3253 	/* Emit SND now, ie. just before sending to driver */
3254 	if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
3255 		__skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SND);
3256 
3257 	/* COMPLETION tstamp is emitted for tracked skb later in Number of
3258 	 * Completed Packets event. Available only for flow controlled cases.
3259 	 *
3260 	 * TODO: SCO support without flowctl (needs to be done in drivers)
3261 	 */
3262 	switch (conn->type) {
3263 	case CIS_LINK:
3264 	case BIS_LINK:
3265 	case PA_LINK:
3266 	case ACL_LINK:
3267 	case LE_LINK:
3268 		break;
3269 	case SCO_LINK:
3270 	case ESCO_LINK:
3271 		if (!hci_dev_test_flag(conn->hdev, HCI_SCO_FLOWCTL))
3272 			return;
3273 		break;
3274 	default:
3275 		return;
3276 	}
3277 
3278 	if (skb->sk && (skb_shinfo(skb)->tx_flags & SKBTX_COMPLETION_TSTAMP))
3279 		track = true;
3280 
3281 	/* If nothing is tracked, just count extra skbs at the queue head */
3282 	if (!track && !comp->tracked) {
3283 		comp->extra++;
3284 		return;
3285 	}
3286 
3287 	if (track) {
3288 		skb = skb_clone_sk(skb);
3289 		if (!skb)
3290 			goto count_only;
3291 
3292 		comp->tracked++;
3293 	} else {
3294 		skb = skb_clone(skb, GFP_KERNEL);
3295 		if (!skb)
3296 			goto count_only;
3297 	}
3298 
3299 	skb_queue_tail(&comp->queue, skb);
3300 	return;
3301 
3302 count_only:
3303 	/* Stop tracking skbs, and only count. This will not emit timestamps for
3304 	 * the packets, but if we get here something is more seriously wrong.
3305 	 */
3306 	comp->tracked = 0;
3307 	comp->extra += skb_queue_len(&comp->queue) + 1;
3308 	skb_queue_purge(&comp->queue);
3309 }
3310 
hci_conn_tx_dequeue(struct hci_conn * conn)3311 void hci_conn_tx_dequeue(struct hci_conn *conn)
3312 {
3313 	struct tx_queue *comp = &conn->tx_q;
3314 	struct sk_buff *skb;
3315 
3316 	/* If there are tracked skbs, the counted extra go before dequeuing real
3317 	 * skbs, to keep ordering. When nothing is tracked, the ordering doesn't
3318 	 * matter so dequeue real skbs first to get rid of them ASAP.
3319 	 */
3320 	if (comp->extra && (comp->tracked || skb_queue_empty(&comp->queue))) {
3321 		comp->extra--;
3322 		return;
3323 	}
3324 
3325 	skb = skb_dequeue(&comp->queue);
3326 	if (!skb)
3327 		return;
3328 
3329 	if (skb->sk) {
3330 		comp->tracked--;
3331 		__skb_tstamp_tx(skb, NULL, NULL, skb->sk,
3332 				SCM_TSTAMP_COMPLETION);
3333 	}
3334 
3335 	kfree_skb(skb);
3336 }
3337 
hci_conn_key_enc_size(struct hci_conn * conn)3338 u8 *hci_conn_key_enc_size(struct hci_conn *conn)
3339 {
3340 	if (conn->type == ACL_LINK) {
3341 		struct link_key *key;
3342 
3343 		key = hci_find_link_key(conn->hdev, &conn->dst);
3344 		if (!key)
3345 			return NULL;
3346 
3347 		return &key->pin_len;
3348 	} else if (conn->type == LE_LINK) {
3349 		struct smp_ltk *ltk;
3350 
3351 		ltk = hci_find_ltk(conn->hdev, &conn->dst, conn->dst_type,
3352 				   conn->role);
3353 		if (!ltk)
3354 			return NULL;
3355 
3356 		return &ltk->enc_size;
3357 	}
3358 
3359 	return NULL;
3360 }
3361 
hci_ethtool_ts_info(unsigned int index,int sk_proto,struct kernel_ethtool_ts_info * info)3362 int hci_ethtool_ts_info(unsigned int index, int sk_proto,
3363 			struct kernel_ethtool_ts_info *info)
3364 {
3365 	struct hci_dev *hdev;
3366 
3367 	hdev = hci_dev_get(index);
3368 	if (!hdev)
3369 		return -ENODEV;
3370 
3371 	info->so_timestamping =
3372 		SOF_TIMESTAMPING_RX_SOFTWARE |
3373 		SOF_TIMESTAMPING_SOFTWARE;
3374 	info->phc_index = -1;
3375 	info->tx_types = BIT(HWTSTAMP_TX_OFF);
3376 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE);
3377 
3378 	switch (sk_proto) {
3379 	case BTPROTO_ISO:
3380 	case BTPROTO_L2CAP:
3381 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
3382 		info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION;
3383 		break;
3384 	case BTPROTO_SCO:
3385 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
3386 		if (hci_dev_test_flag(hdev, HCI_SCO_FLOWCTL))
3387 			info->so_timestamping |= SOF_TIMESTAMPING_TX_COMPLETION;
3388 		break;
3389 	}
3390 
3391 	hci_dev_put(hdev);
3392 	return 0;
3393 }
3394