Skip to content

Commit b139553

Browse files
mcchou-crjhedberg
authored andcommitted
Bluetooth: Add handler of MGMT_OP_ADD_ADV_PATTERNS_MONITOR
This adds the request handler of MGMT_OP_ADD_ADV_PATTERNS_MONITOR command. Note that the controller-based monitoring is not yet in place. This tracks the content of the monitor without sending HCI traffic, so the request returns immediately. The following manual test was performed. - Issue btmgmt advmon-add with valid and invalid inputs. - Issue btmgmt advmon-add more the allowed number of monitors. Signed-off-by: Miao-chen Chou <mcchou@chromium.org> Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent e5e1e7f commit b139553

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

‎include/net/bluetooth/hci_core.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,8 @@ int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
12811281
void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired);
12821282

12831283
void hci_adv_monitors_clear(struct hci_dev *hdev);
1284+
void hci_free_adv_monitor(struct adv_monitor *monitor);
1285+
int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
12841286

12851287
void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
12861288

‎net/bluetooth/hci_core.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,9 +2998,49 @@ int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags,
29982998
/* This function requires the caller holds hdev->lock */
29992999
void hci_adv_monitors_clear(struct hci_dev *hdev)
30003000
{
3001+
struct adv_monitor *monitor;
3002+
int handle;
3003+
3004+
idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle)
3005+
hci_free_adv_monitor(monitor);
3006+
30013007
idr_destroy(&hdev->adv_monitors_idr);
30023008
}
30033009

3010+
void hci_free_adv_monitor(struct adv_monitor *monitor)
3011+
{
3012+
struct adv_pattern *pattern;
3013+
struct adv_pattern *tmp;
3014+
3015+
if (!monitor)
3016+
return;
3017+
3018+
list_for_each_entry_safe(pattern, tmp, &monitor->patterns, list)
3019+
kfree(pattern);
3020+
3021+
kfree(monitor);
3022+
}
3023+
3024+
/* This function requires the caller holds hdev->lock */
3025+
int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
3026+
{
3027+
int min, max, handle;
3028+
3029+
if (!monitor)
3030+
return -EINVAL;
3031+
3032+
min = HCI_MIN_ADV_MONITOR_HANDLE;
3033+
max = HCI_MIN_ADV_MONITOR_HANDLE + HCI_MAX_ADV_MONITOR_NUM_HANDLES;
3034+
handle = idr_alloc(&hdev->adv_monitors_idr, monitor, min, max,
3035+
GFP_KERNEL);
3036+
if (handle < 0)
3037+
return handle;
3038+
3039+
hdev->adv_monitors_cnt++;
3040+
monitor->handle = handle;
3041+
return 0;
3042+
}
3043+
30043044
struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list,
30053045
bdaddr_t *bdaddr, u8 type)
30063046
{

‎net/bluetooth/mgmt.c‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ static const u16 mgmt_commands[] = {
120120
MGMT_OP_GET_DEVICE_FLAGS,
121121
MGMT_OP_SET_DEVICE_FLAGS,
122122
MGMT_OP_READ_ADV_MONITOR_FEATURES,
123+
MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
123124
};
124125

125126
static const u16 mgmt_events[] = {
@@ -4020,6 +4021,103 @@ static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
40204021
MGMT_STATUS_SUCCESS, rp, rp_size);
40214022
}
40224023

4024+
static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
4025+
void *data, u16 len)
4026+
{
4027+
struct mgmt_cp_add_adv_patterns_monitor *cp = data;
4028+
struct mgmt_rp_add_adv_patterns_monitor rp;
4029+
struct adv_monitor *m = NULL;
4030+
struct adv_pattern *p = NULL;
4031+
__u8 cp_ofst = 0, cp_len = 0;
4032+
unsigned int mp_cnt = 0;
4033+
int err, i;
4034+
4035+
BT_DBG("request for %s", hdev->name);
4036+
4037+
if (len <= sizeof(*cp) || cp->pattern_count == 0) {
4038+
err = mgmt_cmd_status(sk, hdev->id,
4039+
MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4040+
MGMT_STATUS_INVALID_PARAMS);
4041+
goto failed;
4042+
}
4043+
4044+
m = kmalloc(sizeof(*m), GFP_KERNEL);
4045+
if (!m) {
4046+
err = -ENOMEM;
4047+
goto failed;
4048+
}
4049+
4050+
INIT_LIST_HEAD(&m->patterns);
4051+
m->active = false;
4052+
4053+
for (i = 0; i < cp->pattern_count; i++) {
4054+
if (++mp_cnt > HCI_MAX_ADV_MONITOR_NUM_PATTERNS) {
4055+
err = mgmt_cmd_status(sk, hdev->id,
4056+
MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4057+
MGMT_STATUS_INVALID_PARAMS);
4058+
goto failed;
4059+
}
4060+
4061+
cp_ofst = cp->patterns[i].offset;
4062+
cp_len = cp->patterns[i].length;
4063+
if (cp_ofst >= HCI_MAX_AD_LENGTH ||
4064+
cp_len > HCI_MAX_AD_LENGTH ||
4065+
(cp_ofst + cp_len) > HCI_MAX_AD_LENGTH) {
4066+
err = mgmt_cmd_status(sk, hdev->id,
4067+
MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4068+
MGMT_STATUS_INVALID_PARAMS);
4069+
goto failed;
4070+
}
4071+
4072+
p = kmalloc(sizeof(*p), GFP_KERNEL);
4073+
if (!p) {
4074+
err = -ENOMEM;
4075+
goto failed;
4076+
}
4077+
4078+
p->ad_type = cp->patterns[i].ad_type;
4079+
p->offset = cp->patterns[i].offset;
4080+
p->length = cp->patterns[i].length;
4081+
memcpy(p->value, cp->patterns[i].value, p->length);
4082+
4083+
INIT_LIST_HEAD(&p->list);
4084+
list_add(&p->list, &m->patterns);
4085+
}
4086+
4087+
if (mp_cnt != cp->pattern_count) {
4088+
err = mgmt_cmd_status(sk, hdev->id,
4089+
MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4090+
MGMT_STATUS_INVALID_PARAMS);
4091+
goto failed;
4092+
}
4093+
4094+
hci_dev_lock(hdev);
4095+
4096+
err = hci_add_adv_monitor(hdev, m);
4097+
if (err) {
4098+
if (err == -ENOSPC) {
4099+
mgmt_cmd_status(sk, hdev->id,
4100+
MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4101+
MGMT_STATUS_NO_RESOURCES);
4102+
}
4103+
goto unlock;
4104+
}
4105+
4106+
hci_dev_unlock(hdev);
4107+
4108+
rp.monitor_handle = cpu_to_le16(m->handle);
4109+
4110+
return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4111+
MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
4112+
4113+
unlock:
4114+
hci_dev_unlock(hdev);
4115+
4116+
failed:
4117+
hci_free_adv_monitor(m);
4118+
return err;
4119+
}
4120+
40234121
static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
40244122
u16 opcode, struct sk_buff *skb)
40254123
{
@@ -7489,6 +7587,8 @@ static const struct hci_mgmt_handler mgmt_handlers[] = {
74897587
{ get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE },
74907588
{ set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE },
74917589
{ read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE },
7590+
{ add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE,
7591+
HCI_MGMT_VAR_LEN },
74927592
};
74937593

74947594
void mgmt_index_added(struct hci_dev *hdev)

0 commit comments

Comments
 (0)