18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * Copyright (C) 2020 Google Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <net/bluetooth/bluetooth.h>
88c2ecf20Sopenharmony_ci#include <net/bluetooth/hci_core.h>
98c2ecf20Sopenharmony_ci#include <net/bluetooth/mgmt.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "mgmt_util.h"
128c2ecf20Sopenharmony_ci#include "mgmt_config.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define HDEV_PARAM_U16(_param_code_, _param_name_) \
158c2ecf20Sopenharmony_ci{ \
168c2ecf20Sopenharmony_ci	{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
178c2ecf20Sopenharmony_ci	{ cpu_to_le16(hdev->_param_name_) } \
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
218c2ecf20Sopenharmony_ci{ \
228c2ecf20Sopenharmony_ci	{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
238c2ecf20Sopenharmony_ci	{ cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
248c2ecf20Sopenharmony_ci}
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciint read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
278c2ecf20Sopenharmony_ci			   u16 data_len)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct {
308c2ecf20Sopenharmony_ci		struct mgmt_tlv entry;
318c2ecf20Sopenharmony_ci		union {
328c2ecf20Sopenharmony_ci			/* This is a simplification for now since all values
338c2ecf20Sopenharmony_ci			 * are 16 bits.  In the future, this code may need
348c2ecf20Sopenharmony_ci			 * refactoring to account for variable length values
358c2ecf20Sopenharmony_ci			 * and properly calculate the required buffer size.
368c2ecf20Sopenharmony_ci			 */
378c2ecf20Sopenharmony_ci			__le16 value;
388c2ecf20Sopenharmony_ci		};
398c2ecf20Sopenharmony_ci	} __packed params[] = {
408c2ecf20Sopenharmony_ci		/* Please see mgmt-api.txt for documentation of these values */
418c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0000, def_page_scan_type),
428c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0001, def_page_scan_int),
438c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0002, def_page_scan_window),
448c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0003, def_inq_scan_type),
458c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0004, def_inq_scan_int),
468c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0005, def_inq_scan_window),
478c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0006, def_br_lsto),
488c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0007, def_page_timeout),
498c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0008, sniff_min_interval),
508c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0009, sniff_max_interval),
518c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x000a, le_adv_min_interval),
528c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x000b, le_adv_max_interval),
538c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
548c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x000d, le_scan_interval),
558c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x000e, le_scan_window),
568c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
578c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
588c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
598c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
608c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
618c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
628c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0015, le_scan_int_connect),
638c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0016, le_scan_window_connect),
648c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0017, le_conn_min_interval),
658c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0018, le_conn_max_interval),
668c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x0019, le_conn_latency),
678c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x001a, le_supv_timeout),
688c2ecf20Sopenharmony_ci		HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
698c2ecf20Sopenharmony_ci						def_le_autoconnect_timeout),
708c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
718c2ecf20Sopenharmony_ci		HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
728c2ecf20Sopenharmony_ci	};
738c2ecf20Sopenharmony_ci	struct mgmt_rp_read_def_system_config *rp = (void *)params;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	bt_dev_dbg(hdev, "sock %p", sk);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return mgmt_cmd_complete(sk, hdev->id,
788c2ecf20Sopenharmony_ci				 MGMT_OP_READ_DEF_SYSTEM_CONFIG,
798c2ecf20Sopenharmony_ci				 0, rp, sizeof(params));
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define TO_TLV(x)		((struct mgmt_tlv *)(x))
838c2ecf20Sopenharmony_ci#define TLV_GET_LE16(tlv)	le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciint set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
868c2ecf20Sopenharmony_ci			  u16 data_len)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	u16 buffer_left = data_len;
898c2ecf20Sopenharmony_ci	u8 *buffer = data;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (buffer_left < sizeof(struct mgmt_tlv)) {
928c2ecf20Sopenharmony_ci		return mgmt_cmd_status(sk, hdev->id,
938c2ecf20Sopenharmony_ci				       MGMT_OP_SET_DEF_SYSTEM_CONFIG,
948c2ecf20Sopenharmony_ci				       MGMT_STATUS_INVALID_PARAMS);
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* First pass to validate the tlv */
988c2ecf20Sopenharmony_ci	while (buffer_left >= sizeof(struct mgmt_tlv)) {
998c2ecf20Sopenharmony_ci		const u8 len = TO_TLV(buffer)->length;
1008c2ecf20Sopenharmony_ci		const u16 exp_len = sizeof(struct mgmt_tlv) +
1018c2ecf20Sopenharmony_ci				    len;
1028c2ecf20Sopenharmony_ci		const u16 type = le16_to_cpu(TO_TLV(buffer)->type);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		if (buffer_left < exp_len) {
1058c2ecf20Sopenharmony_ci			bt_dev_warn(hdev, "invalid len left %d, exp >= %d",
1068c2ecf20Sopenharmony_ci				    buffer_left, exp_len);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci			return mgmt_cmd_status(sk, hdev->id,
1098c2ecf20Sopenharmony_ci					MGMT_OP_SET_DEF_SYSTEM_CONFIG,
1108c2ecf20Sopenharmony_ci					MGMT_STATUS_INVALID_PARAMS);
1118c2ecf20Sopenharmony_ci		}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci		/* Please see mgmt-api.txt for documentation of these values */
1148c2ecf20Sopenharmony_ci		switch (type) {
1158c2ecf20Sopenharmony_ci		case 0x0000:
1168c2ecf20Sopenharmony_ci		case 0x0001:
1178c2ecf20Sopenharmony_ci		case 0x0002:
1188c2ecf20Sopenharmony_ci		case 0x0003:
1198c2ecf20Sopenharmony_ci		case 0x0004:
1208c2ecf20Sopenharmony_ci		case 0x0005:
1218c2ecf20Sopenharmony_ci		case 0x0006:
1228c2ecf20Sopenharmony_ci		case 0x0007:
1238c2ecf20Sopenharmony_ci		case 0x0008:
1248c2ecf20Sopenharmony_ci		case 0x0009:
1258c2ecf20Sopenharmony_ci		case 0x000a:
1268c2ecf20Sopenharmony_ci		case 0x000b:
1278c2ecf20Sopenharmony_ci		case 0x000c:
1288c2ecf20Sopenharmony_ci		case 0x000d:
1298c2ecf20Sopenharmony_ci		case 0x000e:
1308c2ecf20Sopenharmony_ci		case 0x000f:
1318c2ecf20Sopenharmony_ci		case 0x0010:
1328c2ecf20Sopenharmony_ci		case 0x0011:
1338c2ecf20Sopenharmony_ci		case 0x0012:
1348c2ecf20Sopenharmony_ci		case 0x0013:
1358c2ecf20Sopenharmony_ci		case 0x0014:
1368c2ecf20Sopenharmony_ci		case 0x0015:
1378c2ecf20Sopenharmony_ci		case 0x0016:
1388c2ecf20Sopenharmony_ci		case 0x0017:
1398c2ecf20Sopenharmony_ci		case 0x0018:
1408c2ecf20Sopenharmony_ci		case 0x0019:
1418c2ecf20Sopenharmony_ci		case 0x001a:
1428c2ecf20Sopenharmony_ci		case 0x001b:
1438c2ecf20Sopenharmony_ci		case 0x001d:
1448c2ecf20Sopenharmony_ci		case 0x001e:
1458c2ecf20Sopenharmony_ci			if (len != sizeof(u16)) {
1468c2ecf20Sopenharmony_ci				bt_dev_warn(hdev, "invalid length %d, exp %zu for type %d",
1478c2ecf20Sopenharmony_ci					    len, sizeof(u16), type);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci				return mgmt_cmd_status(sk, hdev->id,
1508c2ecf20Sopenharmony_ci					MGMT_OP_SET_DEF_SYSTEM_CONFIG,
1518c2ecf20Sopenharmony_ci					MGMT_STATUS_INVALID_PARAMS);
1528c2ecf20Sopenharmony_ci			}
1538c2ecf20Sopenharmony_ci			break;
1548c2ecf20Sopenharmony_ci		default:
1558c2ecf20Sopenharmony_ci			bt_dev_warn(hdev, "unsupported parameter %u", type);
1568c2ecf20Sopenharmony_ci			break;
1578c2ecf20Sopenharmony_ci		}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		buffer_left -= exp_len;
1608c2ecf20Sopenharmony_ci		buffer += exp_len;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	buffer_left = data_len;
1648c2ecf20Sopenharmony_ci	buffer = data;
1658c2ecf20Sopenharmony_ci	while (buffer_left >= sizeof(struct mgmt_tlv)) {
1668c2ecf20Sopenharmony_ci		const u8 len = TO_TLV(buffer)->length;
1678c2ecf20Sopenharmony_ci		const u16 exp_len = sizeof(struct mgmt_tlv) +
1688c2ecf20Sopenharmony_ci				    len;
1698c2ecf20Sopenharmony_ci		const u16 type = le16_to_cpu(TO_TLV(buffer)->type);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		switch (type) {
1728c2ecf20Sopenharmony_ci		case 0x0000:
1738c2ecf20Sopenharmony_ci			hdev->def_page_scan_type = TLV_GET_LE16(buffer);
1748c2ecf20Sopenharmony_ci			break;
1758c2ecf20Sopenharmony_ci		case 0x0001:
1768c2ecf20Sopenharmony_ci			hdev->def_page_scan_int = TLV_GET_LE16(buffer);
1778c2ecf20Sopenharmony_ci			break;
1788c2ecf20Sopenharmony_ci		case 0x0002:
1798c2ecf20Sopenharmony_ci			hdev->def_page_scan_window = TLV_GET_LE16(buffer);
1808c2ecf20Sopenharmony_ci			break;
1818c2ecf20Sopenharmony_ci		case 0x0003:
1828c2ecf20Sopenharmony_ci			hdev->def_inq_scan_type = TLV_GET_LE16(buffer);
1838c2ecf20Sopenharmony_ci			break;
1848c2ecf20Sopenharmony_ci		case 0x0004:
1858c2ecf20Sopenharmony_ci			hdev->def_inq_scan_int = TLV_GET_LE16(buffer);
1868c2ecf20Sopenharmony_ci			break;
1878c2ecf20Sopenharmony_ci		case 0x0005:
1888c2ecf20Sopenharmony_ci			hdev->def_inq_scan_window = TLV_GET_LE16(buffer);
1898c2ecf20Sopenharmony_ci			break;
1908c2ecf20Sopenharmony_ci		case 0x0006:
1918c2ecf20Sopenharmony_ci			hdev->def_br_lsto = TLV_GET_LE16(buffer);
1928c2ecf20Sopenharmony_ci			break;
1938c2ecf20Sopenharmony_ci		case 0x0007:
1948c2ecf20Sopenharmony_ci			hdev->def_page_timeout = TLV_GET_LE16(buffer);
1958c2ecf20Sopenharmony_ci			break;
1968c2ecf20Sopenharmony_ci		case 0x0008:
1978c2ecf20Sopenharmony_ci			hdev->sniff_min_interval = TLV_GET_LE16(buffer);
1988c2ecf20Sopenharmony_ci			break;
1998c2ecf20Sopenharmony_ci		case 0x0009:
2008c2ecf20Sopenharmony_ci			hdev->sniff_max_interval = TLV_GET_LE16(buffer);
2018c2ecf20Sopenharmony_ci			break;
2028c2ecf20Sopenharmony_ci		case 0x000a:
2038c2ecf20Sopenharmony_ci			hdev->le_adv_min_interval = TLV_GET_LE16(buffer);
2048c2ecf20Sopenharmony_ci			break;
2058c2ecf20Sopenharmony_ci		case 0x000b:
2068c2ecf20Sopenharmony_ci			hdev->le_adv_max_interval = TLV_GET_LE16(buffer);
2078c2ecf20Sopenharmony_ci			break;
2088c2ecf20Sopenharmony_ci		case 0x000c:
2098c2ecf20Sopenharmony_ci			hdev->def_multi_adv_rotation_duration =
2108c2ecf20Sopenharmony_ci							   TLV_GET_LE16(buffer);
2118c2ecf20Sopenharmony_ci			break;
2128c2ecf20Sopenharmony_ci		case 0x000d:
2138c2ecf20Sopenharmony_ci			hdev->le_scan_interval = TLV_GET_LE16(buffer);
2148c2ecf20Sopenharmony_ci			break;
2158c2ecf20Sopenharmony_ci		case 0x000e:
2168c2ecf20Sopenharmony_ci			hdev->le_scan_window = TLV_GET_LE16(buffer);
2178c2ecf20Sopenharmony_ci			break;
2188c2ecf20Sopenharmony_ci		case 0x000f:
2198c2ecf20Sopenharmony_ci			hdev->le_scan_int_suspend = TLV_GET_LE16(buffer);
2208c2ecf20Sopenharmony_ci			break;
2218c2ecf20Sopenharmony_ci		case 0x0010:
2228c2ecf20Sopenharmony_ci			hdev->le_scan_window_suspend = TLV_GET_LE16(buffer);
2238c2ecf20Sopenharmony_ci			break;
2248c2ecf20Sopenharmony_ci		case 0x0011:
2258c2ecf20Sopenharmony_ci			hdev->le_scan_int_discovery = TLV_GET_LE16(buffer);
2268c2ecf20Sopenharmony_ci			break;
2278c2ecf20Sopenharmony_ci		case 0x00012:
2288c2ecf20Sopenharmony_ci			hdev->le_scan_window_discovery = TLV_GET_LE16(buffer);
2298c2ecf20Sopenharmony_ci			break;
2308c2ecf20Sopenharmony_ci		case 0x00013:
2318c2ecf20Sopenharmony_ci			hdev->le_scan_int_adv_monitor = TLV_GET_LE16(buffer);
2328c2ecf20Sopenharmony_ci			break;
2338c2ecf20Sopenharmony_ci		case 0x00014:
2348c2ecf20Sopenharmony_ci			hdev->le_scan_window_adv_monitor = TLV_GET_LE16(buffer);
2358c2ecf20Sopenharmony_ci			break;
2368c2ecf20Sopenharmony_ci		case 0x00015:
2378c2ecf20Sopenharmony_ci			hdev->le_scan_int_connect = TLV_GET_LE16(buffer);
2388c2ecf20Sopenharmony_ci			break;
2398c2ecf20Sopenharmony_ci		case 0x00016:
2408c2ecf20Sopenharmony_ci			hdev->le_scan_window_connect = TLV_GET_LE16(buffer);
2418c2ecf20Sopenharmony_ci			break;
2428c2ecf20Sopenharmony_ci		case 0x00017:
2438c2ecf20Sopenharmony_ci			hdev->le_conn_min_interval = TLV_GET_LE16(buffer);
2448c2ecf20Sopenharmony_ci			break;
2458c2ecf20Sopenharmony_ci		case 0x00018:
2468c2ecf20Sopenharmony_ci			hdev->le_conn_max_interval = TLV_GET_LE16(buffer);
2478c2ecf20Sopenharmony_ci			break;
2488c2ecf20Sopenharmony_ci		case 0x00019:
2498c2ecf20Sopenharmony_ci			hdev->le_conn_latency = TLV_GET_LE16(buffer);
2508c2ecf20Sopenharmony_ci			break;
2518c2ecf20Sopenharmony_ci		case 0x0001a:
2528c2ecf20Sopenharmony_ci			hdev->le_supv_timeout = TLV_GET_LE16(buffer);
2538c2ecf20Sopenharmony_ci			break;
2548c2ecf20Sopenharmony_ci		case 0x0001b:
2558c2ecf20Sopenharmony_ci			hdev->def_le_autoconnect_timeout =
2568c2ecf20Sopenharmony_ci					msecs_to_jiffies(TLV_GET_LE16(buffer));
2578c2ecf20Sopenharmony_ci			break;
2588c2ecf20Sopenharmony_ci		case 0x0001d:
2598c2ecf20Sopenharmony_ci			hdev->advmon_allowlist_duration = TLV_GET_LE16(buffer);
2608c2ecf20Sopenharmony_ci			break;
2618c2ecf20Sopenharmony_ci		case 0x0001e:
2628c2ecf20Sopenharmony_ci			hdev->advmon_no_filter_duration = TLV_GET_LE16(buffer);
2638c2ecf20Sopenharmony_ci			break;
2648c2ecf20Sopenharmony_ci		default:
2658c2ecf20Sopenharmony_ci			bt_dev_warn(hdev, "unsupported parameter %u", type);
2668c2ecf20Sopenharmony_ci			break;
2678c2ecf20Sopenharmony_ci		}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		buffer_left -= exp_len;
2708c2ecf20Sopenharmony_ci		buffer += exp_len;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return mgmt_cmd_complete(sk, hdev->id,
2748c2ecf20Sopenharmony_ci				 MGMT_OP_SET_DEF_SYSTEM_CONFIG, 0, NULL, 0);
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ciint read_def_runtime_config(struct sock *sk, struct hci_dev *hdev, void *data,
2788c2ecf20Sopenharmony_ci			    u16 data_len)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	bt_dev_dbg(hdev, "sock %p", sk);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return mgmt_cmd_complete(sk, hdev->id,
2838c2ecf20Sopenharmony_ci				 MGMT_OP_READ_DEF_RUNTIME_CONFIG, 0, NULL, 0);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ciint set_def_runtime_config(struct sock *sk, struct hci_dev *hdev, void *data,
2878c2ecf20Sopenharmony_ci			   u16 data_len)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	bt_dev_dbg(hdev, "sock %p", sk);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEF_SYSTEM_CONFIG,
2928c2ecf20Sopenharmony_ci			       MGMT_STATUS_INVALID_PARAMS);
2938c2ecf20Sopenharmony_ci}
294