xref: /linux/net/bluetooth/eir.c (revision 056e065a6b6e01ab54bb9770c0d5a15350e571e2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * BlueZ - Bluetooth protocol stack for Linux
4  *
5  * Copyright (C) 2021 Intel Corporation
6  */
7 
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
10 #include <net/bluetooth/mgmt.h>
11 
12 #include "eir.h"
13 
14 #define PNP_INFO_SVCLASS_ID		0x1200
15 
16 u8 eir_append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
17 {
18 	size_t short_len;
19 	size_t complete_len;
20 
21 	/* no space left for name (+ type + len) */
22 	if ((max_adv_len(hdev) - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 2)
23 		return ad_len;
24 
25 	/* use complete name if present and fits */
26 	complete_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
27 	if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
28 		return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
29 				       hdev->dev_name, complete_len);
30 
31 	/* use short name if present */
32 	short_len = strnlen(hdev->short_name, sizeof(hdev->short_name));
33 	if (short_len)
34 		return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
35 				       hdev->short_name,
36 				       short_len);
37 
38 	/* use shortened full name if present, we already know that name
39 	 * is longer then HCI_MAX_SHORT_NAME_LENGTH
40 	 */
41 	if (complete_len)
42 		return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
43 				       hdev->dev_name,
44 				       HCI_MAX_SHORT_NAME_LENGTH);
45 
46 	return ad_len;
47 }
48 
49 u8 eir_append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
50 {
51 	return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
52 }
53 
54 u8 eir_append_service_data(u8 *eir, u16 eir_len, u16 uuid, u8 *data,
55 			   u8 data_len)
56 {
57 	eir[eir_len++] = sizeof(u8) + sizeof(uuid) + data_len;
58 	eir[eir_len++] = EIR_SERVICE_DATA;
59 	put_unaligned_le16(uuid, &eir[eir_len]);
60 	eir_len += sizeof(uuid);
61 	memcpy(&eir[eir_len], data, data_len);
62 	eir_len += data_len;
63 
64 	return eir_len;
65 }
66 
67 static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
68 {
69 	u8 *ptr = data, *uuids_start = NULL;
70 	struct bt_uuid *uuid;
71 
72 	if (len < 4)
73 		return ptr;
74 
75 	list_for_each_entry(uuid, &hdev->uuids, list) {
76 		u16 uuid16;
77 
78 		if (uuid->size != 16)
79 			continue;
80 
81 		uuid16 = get_unaligned_le16(&uuid->uuid[12]);
82 		if (uuid16 < 0x1100)
83 			continue;
84 
85 		if (uuid16 == PNP_INFO_SVCLASS_ID)
86 			continue;
87 
88 		if (!uuids_start) {
89 			uuids_start = ptr;
90 			uuids_start[0] = 1;
91 			uuids_start[1] = EIR_UUID16_ALL;
92 			ptr += 2;
93 		}
94 
95 		/* Stop if not enough space to put next UUID */
96 		if ((ptr - data) + sizeof(u16) > len) {
97 			uuids_start[1] = EIR_UUID16_SOME;
98 			break;
99 		}
100 
101 		*ptr++ = (uuid16 & 0x00ff);
102 		*ptr++ = (uuid16 & 0xff00) >> 8;
103 		uuids_start[0] += sizeof(uuid16);
104 	}
105 
106 	return ptr;
107 }
108 
109 static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
110 {
111 	u8 *ptr = data, *uuids_start = NULL;
112 	struct bt_uuid *uuid;
113 
114 	if (len < 6)
115 		return ptr;
116 
117 	list_for_each_entry(uuid, &hdev->uuids, list) {
118 		if (uuid->size != 32)
119 			continue;
120 
121 		if (!uuids_start) {
122 			uuids_start = ptr;
123 			uuids_start[0] = 1;
124 			uuids_start[1] = EIR_UUID32_ALL;
125 			ptr += 2;
126 		}
127 
128 		/* Stop if not enough space to put next UUID */
129 		if ((ptr - data) + sizeof(u32) > len) {
130 			uuids_start[1] = EIR_UUID32_SOME;
131 			break;
132 		}
133 
134 		memcpy(ptr, &uuid->uuid[12], sizeof(u32));
135 		ptr += sizeof(u32);
136 		uuids_start[0] += sizeof(u32);
137 	}
138 
139 	return ptr;
140 }
141 
142 static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
143 {
144 	u8 *ptr = data, *uuids_start = NULL;
145 	struct bt_uuid *uuid;
146 
147 	if (len < 18)
148 		return ptr;
149 
150 	list_for_each_entry(uuid, &hdev->uuids, list) {
151 		if (uuid->size != 128)
152 			continue;
153 
154 		if (!uuids_start) {
155 			uuids_start = ptr;
156 			uuids_start[0] = 1;
157 			uuids_start[1] = EIR_UUID128_ALL;
158 			ptr += 2;
159 		}
160 
161 		/* Stop if not enough space to put next UUID */
162 		if ((ptr - data) + 16 > len) {
163 			uuids_start[1] = EIR_UUID128_SOME;
164 			break;
165 		}
166 
167 		memcpy(ptr, uuid->uuid, 16);
168 		ptr += 16;
169 		uuids_start[0] += 16;
170 	}
171 
172 	return ptr;
173 }
174 
175 void eir_create(struct hci_dev *hdev, u8 *data)
176 {
177 	u8 *ptr = data;
178 	size_t name_len;
179 
180 	name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
181 
182 	if (name_len > 0) {
183 		/* EIR Data type */
184 		if (name_len > 48) {
185 			name_len = 48;
186 			ptr[1] = EIR_NAME_SHORT;
187 		} else {
188 			ptr[1] = EIR_NAME_COMPLETE;
189 		}
190 
191 		/* EIR Data length */
192 		ptr[0] = name_len + 1;
193 
194 		memcpy(ptr + 2, hdev->dev_name, name_len);
195 
196 		ptr += (name_len + 2);
197 	}
198 
199 	if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
200 		ptr[0] = 2;
201 		ptr[1] = EIR_TX_POWER;
202 		ptr[2] = (u8)hdev->inq_tx_power;
203 
204 		ptr += 3;
205 	}
206 
207 	if (hdev->devid_source > 0) {
208 		ptr[0] = 9;
209 		ptr[1] = EIR_DEVICE_ID;
210 
211 		put_unaligned_le16(hdev->devid_source, ptr + 2);
212 		put_unaligned_le16(hdev->devid_vendor, ptr + 4);
213 		put_unaligned_le16(hdev->devid_product, ptr + 6);
214 		put_unaligned_le16(hdev->devid_version, ptr + 8);
215 
216 		ptr += 10;
217 	}
218 
219 	ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
220 	ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
221 	ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
222 }
223 
224 u8 eir_create_per_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
225 {
226 	struct adv_info *adv = NULL;
227 	u8 ad_len = 0;
228 
229 	/* Return 0 when the current instance identifier is invalid. */
230 	if (instance) {
231 		adv = hci_find_adv_instance(hdev, instance);
232 		if (!adv)
233 			return 0;
234 	}
235 
236 	if (adv) {
237 		memcpy(ptr, adv->per_adv_data, adv->per_adv_data_len);
238 		ad_len += adv->per_adv_data_len;
239 		ptr += adv->per_adv_data_len;
240 	}
241 
242 	return ad_len;
243 }
244 
245 u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr, u8 size)
246 {
247 	struct adv_info *adv = NULL;
248 	u8 ad_len = 0, flags = 0;
249 	u32 instance_flags;
250 
251 	/* Return 0 when the current instance identifier is invalid. */
252 	if (instance) {
253 		adv = hci_find_adv_instance(hdev, instance);
254 		if (!adv)
255 			return 0;
256 	}
257 
258 	instance_flags = hci_adv_instance_flags(hdev, instance);
259 
260 	/* If instance already has the flags set skip adding it once
261 	 * again.
262 	 */
263 	if (adv && eir_get_data(adv->adv_data, adv->adv_data_len, EIR_FLAGS,
264 				NULL))
265 		goto skip_flags;
266 
267 	/* The Add Advertising command allows userspace to set both the general
268 	 * and limited discoverable flags.
269 	 */
270 	if (instance_flags & MGMT_ADV_FLAG_DISCOV)
271 		flags |= LE_AD_GENERAL;
272 
273 	if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
274 		flags |= LE_AD_LIMITED;
275 
276 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
277 		flags |= LE_AD_NO_BREDR;
278 
279 	if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
280 		/* If a discovery flag wasn't provided, simply use the global
281 		 * settings.
282 		 */
283 		if (!flags)
284 			flags |= mgmt_get_adv_discov_flags(hdev);
285 
286 		/* Only add the "Flags" if it fits together with the instance
287 		 * advertising data; drop it rather than overflow the buffer.
288 		 */
289 		if (flags &&
290 		    (ad_len + eir_precalc_len(1) +
291 		     (adv ? adv->adv_data_len : 0) <= size)) {
292 			ptr[0] = 0x02;
293 			ptr[1] = EIR_FLAGS;
294 			ptr[2] = flags;
295 
296 			ad_len += 3;
297 			ptr += 3;
298 		}
299 	}
300 
301 skip_flags:
302 	if (adv) {
303 		memcpy(ptr, adv->adv_data, adv->adv_data_len);
304 		ad_len += adv->adv_data_len;
305 		ptr += adv->adv_data_len;
306 	}
307 
308 	if (instance_flags & MGMT_ADV_FLAG_TX_POWER) {
309 		s8 adv_tx_power;
310 
311 		if (ext_adv_capable(hdev)) {
312 			if (adv)
313 				adv_tx_power = adv->tx_power;
314 			else
315 				adv_tx_power = hdev->adv_tx_power;
316 		} else {
317 			adv_tx_power = hdev->adv_tx_power;
318 		}
319 
320 		/* Provide Tx Power only if we can provide a valid value for it */
321 		if (adv_tx_power != HCI_TX_POWER_INVALID &&
322 		    (ad_len + eir_precalc_len(1) <= size)) {
323 			ptr[0] = 0x02;
324 			ptr[1] = EIR_TX_POWER;
325 			ptr[2] = (u8)adv_tx_power;
326 
327 			ad_len += 3;
328 			ptr += 3;
329 		}
330 	}
331 
332 	return ad_len;
333 }
334 
335 static u8 create_default_scan_rsp(struct hci_dev *hdev, u8 *ptr)
336 {
337 	u8 scan_rsp_len = 0;
338 
339 	if (hdev->appearance)
340 		scan_rsp_len = eir_append_appearance(hdev, ptr, scan_rsp_len);
341 
342 	return eir_append_local_name(hdev, ptr, scan_rsp_len);
343 }
344 
345 u8 eir_create_scan_rsp(struct hci_dev *hdev, u8 instance, u8 *ptr)
346 {
347 	struct adv_info *adv;
348 	u8 scan_rsp_len = 0;
349 
350 	if (!instance)
351 		return create_default_scan_rsp(hdev, ptr);
352 
353 	adv = hci_find_adv_instance(hdev, instance);
354 	if (!adv)
355 		return 0;
356 
357 	if ((adv->flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance)
358 		scan_rsp_len = eir_append_appearance(hdev, ptr, scan_rsp_len);
359 
360 	memcpy(&ptr[scan_rsp_len], adv->scan_rsp_data, adv->scan_rsp_len);
361 
362 	scan_rsp_len += adv->scan_rsp_len;
363 
364 	if (adv->flags & MGMT_ADV_FLAG_LOCAL_NAME)
365 		scan_rsp_len = eir_append_local_name(hdev, ptr, scan_rsp_len);
366 
367 	return scan_rsp_len;
368 }
369 
370 void *eir_get_service_data(u8 *eir, size_t eir_len, u16 uuid, size_t *len)
371 {
372 	size_t dlen;
373 
374 	while ((eir = eir_get_data(eir, eir_len, EIR_SERVICE_DATA, &dlen))) {
375 		u16 value = get_unaligned_le16(eir);
376 
377 		if (uuid == value) {
378 			if (len)
379 				*len = dlen - 2;
380 			return &eir[2];
381 		}
382 
383 		eir += dlen;
384 		eir_len -= dlen;
385 	}
386 
387 	return NULL;
388 }
389