1 /*
2 * NAN Discovery Engine
3 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/crc32.h"
14 #include "utils/list.h"
15 #include "crypto/crypto.h"
16 #include "crypto/sha256.h"
17 #include "ieee802_11_defs.h"
18 #include "nan/nan.h"
19 #include "nan_defs.h"
20 #include "nan_de.h"
21
22 const u8 nan_network_id[ETH_ALEN] =
23 { 0x51, 0x6f, 0x9a, 0x01, 0x00, 0x00 };
24
25 enum nan_de_service_type {
26 NAN_DE_PUBLISH,
27 NAN_DE_SUBSCRIBE,
28 };
29
30 const u8 p2p_network_id[ETH_ALEN] =
31 { 0x51, 0x6f, 0x9a, 0x02, 0x00, 0x00 };
32
33 static const u8 wildcard_bssid[ETH_ALEN] =
34 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
35
36 struct nan_de_service {
37 int id;
38 enum nan_de_service_type type;
39 char *service_name;
40 u8 service_id[NAN_SERVICE_ID_LEN];
41 struct nan_publish_params publish;
42 struct nan_subscribe_params subscribe;
43 enum nan_service_protocol_type srv_proto_type;
44 struct wpabuf *ssi;
45 struct wpabuf *elems;
46 struct os_reltime time_started;
47 struct os_reltime end_time;
48 struct os_reltime last_multicast;
49 struct os_reltime first_discovered;
50 bool needs_fsd;
51 unsigned int freq;
52 unsigned int default_freq;
53 int *freq_list;
54 u8 a3[ETH_ALEN];
55 bool a3_set;
56
57 /* Source MAC address for this service (optional) */
58 u8 forced_addr[ETH_ALEN];
59 bool forced_addr_set;
60
61 /* pauseState information for Publish function */
62 struct os_reltime pause_state_end;
63 u8 sel_peer_id;
64 u8 sel_peer_addr[ETH_ALEN];
65
66 /* Publish state - channel iteration */
67 bool in_multi_chan;
68 bool first_multi_chan;
69 int multi_chan_idx; /* index to freq_list[] */
70 struct os_reltime next_publish_state;
71 struct os_reltime next_publish_chan;
72 unsigned int next_publish_duration;
73 bool is_p2p;
74 bool is_pr;
75 bool listen_stopped;
76 bool sync;
77
78 /* Filters */
79 struct wpabuf *matching_filter_tx;
80 struct wpabuf *matching_filter_rx;
81
82 bool srf_include;
83 bool srf_type_bloom_filter;
84 u8 srf_bf_idx;
85 struct wpabuf *srf;
86 bool close_proximity;
87 bool gtk_required;
88 bool data_path;
89 bool security_required;
90
91 /* Bootstrapping methods */
92 u16 pbm;
93
94 /* For Publish - int_array of supported cipher suites */
95 int *cipher_suites_list;
96
97 /* Bitmap of NAN_CS_INFO_CAPA_* */
98 u8 security_capab;
99
100 /* PMKID list for this service */
101 struct dl_list pmkid_list;
102 };
103
104 #define NAN_DE_N_MIN 5
105 #define NAN_DE_N_MAX 10
106
107 #define NAN_DE_RSSI_CLOSE_PROXIMITY (-70) /* dBm */
108
109 struct nan_de_tracked_tx {
110 struct dl_list list;
111 u8 dst[ETH_ALEN];
112 u32 cookie;
113 u8 digest[SHA256_MAC_LEN];
114 bool with_wait;
115 };
116
117 enum nan_de_flush_tracked_tx_reason {
118 NAN_DE_FLUSH_TRACKED_TX_FLUSH_ALL,
119 NAN_DE_FLUSH_TRACKED_TX_WAIT_EXPIRED,
120 };
121
122 struct nan_de {
123 u8 nmi[ETH_ALEN];
124 u8 cluster_id[ETH_ALEN];
125 bool cluster_id_set;
126 bool offload;
127 bool ap;
128 unsigned int max_listen;
129 struct nan_callbacks cb;
130
131 struct nan_de_service *service[NAN_DE_MAX_SERVICE];
132 unsigned int num_service;
133
134 int next_handle;
135
136 unsigned int ext_listen_freq;
137 unsigned int listen_freq;
138 unsigned int tx_wait_status_freq;
139 unsigned int tx_wait_end_freq;
140
141 struct nan_de_cfg cfg;
142 struct os_reltime suspend_cycle_start;
143
144 int dw_freq;
145
146 /* RSSI threshold for close proximity, or zero if not limited */
147 int rssi_threshold;
148
149 /*
150 * List of transmit requests (struct nan_de_tracked_tx::list) for which
151 * the caller requested status indicating whether the frame was
152 * acknowledged
153 */
154 struct dl_list tracked_tx;
155
156 #ifdef CONFIG_TESTING_OPTIONS
157 /*
158 * When set, multicast follow-up SDFs will be sent as Protected Dual of
159 * Public Action frames. This can be used to test protection of NAN
160 * multicast Management frames.
161 */
162 bool tx_mcast_follow_up_prot;
163 #endif /* CONFIG_TESTING_OPTIONS */
164 };
165
166
nan_de_is_nan_network_id(const u8 * addr)167 bool nan_de_is_nan_network_id(const u8 *addr)
168 {
169 return ether_addr_equal(addr, nan_network_id);
170 }
171
172
nan_de_is_p2p_network_id(const u8 * addr)173 bool nan_de_is_p2p_network_id(const u8 *addr)
174 {
175 return ether_addr_equal(addr, p2p_network_id);
176 }
177
178
nan_de_init(const u8 * nmi,bool offload,bool ap,unsigned int max_listen,const struct nan_callbacks * cb)179 struct nan_de * nan_de_init(const u8 *nmi, bool offload, bool ap,
180 unsigned int max_listen,
181 const struct nan_callbacks *cb)
182 {
183 struct nan_de *de;
184
185 de = os_zalloc(sizeof(*de));
186 if (!de)
187 return NULL;
188
189 os_memcpy(de->nmi, nmi, ETH_ALEN);
190 de->offload = offload;
191 de->ap = ap;
192 de->max_listen = max_listen ? max_listen : 1000;
193 os_memcpy(&de->cb, cb, sizeof(*cb));
194
195 de->cfg.n_min = NAN_DE_N_MIN;
196 de->cfg.n_max = NAN_DE_N_MAX;
197
198 de->rssi_threshold = NAN_DE_RSSI_CLOSE_PROXIMITY;
199 dl_list_init(&de->tracked_tx);
200
201 return de;
202 }
203
204
nan_de_service_free(struct nan_de_service * srv)205 static void nan_de_service_free(struct nan_de_service *srv)
206 {
207 os_free(srv->service_name);
208 wpabuf_free(srv->ssi);
209 wpabuf_free(srv->elems);
210 wpabuf_free(srv->matching_filter_tx);
211 wpabuf_free(srv->matching_filter_rx);
212 wpabuf_free(srv->srf);
213 os_free(srv->freq_list);
214 os_free(srv->cipher_suites_list);
215 #ifdef CONFIG_NAN
216 nan_crypto_clear_pmkid_list(&srv->pmkid_list);
217 #endif /* CONFIG_NAN */
218 os_free(srv);
219 }
220
221
nan_de_service_deinit(struct nan_de * de,struct nan_de_service * srv,enum nan_de_reason reason)222 static void nan_de_service_deinit(struct nan_de *de, struct nan_de_service *srv,
223 enum nan_de_reason reason)
224 {
225 if (!srv)
226 return;
227 if (srv->type == NAN_DE_PUBLISH && de->cb.publish_terminated)
228 de->cb.publish_terminated(de->cb.ctx, srv->id, reason);
229 if (srv->type == NAN_DE_SUBSCRIBE && de->cb.subscribe_terminated)
230 de->cb.subscribe_terminated(de->cb.ctx, srv->id, reason);
231 nan_de_service_free(srv);
232 }
233
234
nan_de_flush_tracked_tx(struct nan_de * de,enum nan_de_flush_tracked_tx_reason reason)235 static void nan_de_flush_tracked_tx(struct nan_de *de,
236 enum nan_de_flush_tracked_tx_reason reason)
237 {
238 struct nan_de_tracked_tx *tx, *tmp;
239
240 dl_list_for_each_safe(tx, tmp, &de->tracked_tx,
241 struct nan_de_tracked_tx, list) {
242 if (reason == NAN_DE_FLUSH_TRACKED_TX_WAIT_EXPIRED &&
243 !tx->with_wait)
244 continue;
245
246 de->cb.transmit_req_status(de->cb.ctx, tx->cookie, false);
247 dl_list_del(&tx->list);
248 os_free(tx);
249 }
250 }
251
252
nan_de_clear_pending(struct nan_de * de)253 static void nan_de_clear_pending(struct nan_de *de)
254 {
255 nan_de_flush_tracked_tx(de, NAN_DE_FLUSH_TRACKED_TX_FLUSH_ALL);
256
257 de->listen_freq = 0;
258 de->tx_wait_status_freq = 0;
259 de->tx_wait_end_freq = 0;
260 }
261
262
nan_de_track_tx_digest(const u8 * data,size_t len,u8 * digest)263 static int nan_de_track_tx_digest(const u8 *data, size_t len, u8 *digest)
264 {
265 return sha256_vector(1, &data, &len, digest);
266 }
267
268
269 static struct nan_de_tracked_tx *
nan_de_add_tracked_tx(struct nan_de * de,const u8 * dst,bool with_wait,u32 cookie,const struct wpabuf * buf)270 nan_de_add_tracked_tx(struct nan_de *de, const u8 *dst, bool with_wait,
271 u32 cookie, const struct wpabuf *buf)
272 {
273 struct nan_de_tracked_tx *tx;
274 u8 digest[SHA256_MAC_LEN];
275
276 if (!de->cb.transmit_req_status) {
277 wpa_printf(MSG_DEBUG,
278 "NAN: No tx_status callback, cannot track Tx");
279 return NULL;
280 }
281
282 if (!cookie) {
283 wpa_printf(MSG_DEBUG, "NAN: Invalid cookie for Tx tracking");
284 return NULL;
285 }
286
287 if (nan_de_track_tx_digest(wpabuf_head(buf), wpabuf_len(buf), digest)) {
288 wpa_printf(MSG_INFO, "NAN: Failed to compute Tx digest");
289 return NULL;
290 }
291
292 dl_list_for_each(tx, &de->tracked_tx, struct nan_de_tracked_tx, list) {
293 if (!ether_addr_equal(tx->dst, dst))
294 continue;
295
296 if (tx->cookie == cookie) {
297 wpa_printf(MSG_DEBUG,
298 "NAN: Already tracking Tx cookie %u to "
299 MACSTR, cookie, MAC2STR(dst));
300 return NULL;
301 }
302
303 if (os_memcmp(tx->digest, digest, SHA256_MAC_LEN) == 0) {
304 wpa_printf(MSG_DEBUG,
305 "NAN: Already tracking identical payload to "
306 MACSTR " (cookie %u)",
307 MAC2STR(dst), tx->cookie);
308 return NULL;
309 }
310 }
311
312 tx = os_zalloc(sizeof(*tx));
313 if (!tx)
314 return NULL;
315
316 os_memcpy(tx->dst, dst, ETH_ALEN);
317 tx->cookie = cookie;
318 tx->with_wait = with_wait;
319 os_memcpy(tx->digest, digest, SHA256_MAC_LEN);
320
321 dl_list_add(&de->tracked_tx, &tx->list);
322
323 wpa_printf(MSG_DEBUG, "NAN: Track Tx cookie %u", tx->cookie);
324 wpa_hexdump(MSG_DEBUG, "NAN: Track Tx digest",
325 tx->digest, SHA256_MAC_LEN);
326
327 return tx;
328 }
329
330
nan_de_tx_status_match(struct nan_de * de,const u8 * data,size_t len,u8 acked)331 static void nan_de_tx_status_match(struct nan_de *de, const u8 *data,
332 size_t len, u8 acked)
333 {
334 struct nan_de_tracked_tx *tx;
335 const struct ieee80211_mgmt *mgmt =
336 (const struct ieee80211_mgmt *) data;
337 const u8 *pos = (const u8 *) &mgmt->u.action;
338 u8 digest[SHA256_MAC_LEN];
339
340 if (len <= offsetof(struct ieee80211_mgmt, u.action))
341 return;
342
343 len = data + len - pos;
344
345 if (nan_de_track_tx_digest(pos, len, digest))
346 return;
347
348 dl_list_for_each(tx, &de->tracked_tx,
349 struct nan_de_tracked_tx, list) {
350 if (!ether_addr_equal(tx->dst, mgmt->da) ||
351 os_memcmp(tx->digest, digest, SHA256_MAC_LEN) != 0)
352 continue;
353
354 wpa_printf(MSG_DEBUG, "NAN: Tx status for cookie=%u ack=%u",
355 tx->cookie, acked);
356
357 de->cb.transmit_req_status(de->cb.ctx, tx->cookie, acked);
358 dl_list_del(&tx->list);
359 os_free(tx);
360 return;
361 }
362 }
363
364
nan_de_flush(struct nan_de * de)365 void nan_de_flush(struct nan_de *de)
366 {
367 unsigned int i;
368
369 if (!de)
370 return;
371
372 for (i = 0; i < NAN_DE_MAX_SERVICE; i++) {
373 nan_de_service_deinit(de, de->service[i],
374 NAN_DE_REASON_USER_REQUEST);
375 de->service[i] = NULL;
376 }
377
378 de->num_service = 0;
379 nan_de_clear_pending(de);
380 }
381
382
nan_de_pause_state(struct nan_de_service * srv,const u8 * peer_addr,u8 peer_id)383 static void nan_de_pause_state(struct nan_de_service *srv, const u8 *peer_addr,
384 u8 peer_id)
385 {
386 wpa_printf(MSG_DEBUG, "NAN: Start pauseState");
387 os_get_reltime(&srv->pause_state_end);
388 srv->pause_state_end.sec += 60;
389 if (os_reltime_initialized(&srv->end_time) &&
390 os_reltime_before(&srv->end_time, &srv->pause_state_end))
391 srv->pause_state_end = srv->end_time;
392 os_memcpy(srv->sel_peer_addr, peer_addr, ETH_ALEN);
393 srv->sel_peer_id = peer_id;
394 }
395
396
nan_de_unpause_state(struct nan_de_service * srv)397 static void nan_de_unpause_state(struct nan_de_service *srv)
398 {
399 wpa_printf(MSG_DEBUG, "NAN: Stop pauseState");
400 srv->pause_state_end.sec = 0;
401 srv->pause_state_end.usec = 0;
402 os_memset(srv->sel_peer_addr, 0, ETH_ALEN);
403 srv->sel_peer_id = 0;
404 }
405
406
nan_de_alloc_sdf(struct nan_de * de,const u8 * dst,size_t len,enum nan_service_control_type type)407 static struct wpabuf * nan_de_alloc_sdf(struct nan_de *de, const u8 *dst,
408 size_t len,
409 enum nan_service_control_type type)
410 {
411 struct wpabuf *buf;
412 u8 category = WLAN_ACTION_PUBLIC;
413
414 if (de->cb.is_peer_paired && de->cb.is_peer_paired(de->cb.ctx, dst))
415 category = WLAN_ACTION_PROTECTED_DUAL;
416
417 #ifdef CONFIG_TESTING_OPTIONS
418 if (de->tx_mcast_follow_up_prot &&
419 is_multicast_ether_addr(dst) &&
420 type == NAN_SRV_CTRL_FOLLOW_UP) {
421 wpa_printf(MSG_DEBUG,
422 "NAN: Send multicast follow-up as protected");
423 category = WLAN_ACTION_PROTECTED_DUAL;
424 }
425 #endif /* CONFIG_TESTING_OPTIONS */
426
427 buf = wpabuf_alloc(2 + 4 + len);
428 if (buf) {
429 wpabuf_put_u8(buf, category);
430 wpabuf_put_u8(buf, WLAN_PA_VENDOR_SPECIFIC);
431 wpabuf_put_be32(buf, NAN_SDF_VENDOR_TYPE);
432 }
433
434 return buf;
435 }
436
437
nan_de_tx(struct nan_de * de,unsigned int freq,unsigned int wait_time,const u8 * dst,const u8 * src,const u8 * bssid,const struct wpabuf * buf,u32 * cookie)438 static int nan_de_tx(struct nan_de *de, unsigned int freq,
439 unsigned int wait_time,
440 const u8 *dst, const u8 *src, const u8 *bssid,
441 const struct wpabuf *buf, u32 *cookie)
442 {
443 struct nan_de_tracked_tx *tracked_tx = NULL;
444 int res;
445
446 if (!de->cb.tx)
447 return -1;
448
449 if (cookie) {
450 tracked_tx = nan_de_add_tracked_tx(de, dst, !!wait_time,
451 *cookie, buf);
452 if (!tracked_tx)
453 return -1;
454 }
455
456 res = de->cb.tx(de->cb.ctx, freq, wait_time, dst, src, bssid, buf);
457 if (res < 0) {
458 if (tracked_tx) {
459 dl_list_del(&tracked_tx->list);
460 os_free(tracked_tx);
461 }
462 return res;
463 }
464
465 de->tx_wait_status_freq = freq;
466 de->tx_wait_end_freq = wait_time ? freq : 0;
467
468 return res;
469 }
470
471
nan_buf_add_npba(const struct nan_de * de,const struct nan_de_service * srv,struct wpabuf * buf)472 static void nan_buf_add_npba(const struct nan_de *de,
473 const struct nan_de_service *srv,
474 struct wpabuf *buf)
475 {
476 u8 type_and_status = NAN_PBA_TYPE_ADVERTISE |
477 (NAN_PBA_STATUS_ACCEPTED << NAN_PBA_STATUS_POS);
478
479 wpa_printf(MSG_DEBUG, "NAN: Add NPBA");
480
481 wpabuf_put_u8(buf, NAN_ATTR_NPBA);
482 wpabuf_put_le16(buf, 5);
483
484 /* Dialog token is reserved (0) for advertise */
485 wpabuf_put_u8(buf, 0);
486 wpabuf_put_u8(buf, type_and_status);
487 wpabuf_put_u8(buf, NAN_REASON_RESERVED);
488 wpabuf_put_le16(buf, srv->pbm);
489 }
490
491
nan_de_tx_sdf(struct nan_de * de,struct nan_de_service * srv,unsigned int wait_time,enum nan_service_control_type type,const u8 * dst,const u8 * a3,u8 req_instance_id,const struct wpabuf * ssi,const struct wpabuf * attrs,u32 * cookie)492 static void nan_de_tx_sdf(struct nan_de *de, struct nan_de_service *srv,
493 unsigned int wait_time,
494 enum nan_service_control_type type,
495 const u8 *dst, const u8 *a3, u8 req_instance_id,
496 const struct wpabuf *ssi,
497 const struct wpabuf *attrs, u32 *cookie)
498 {
499 struct wpabuf *buf;
500 size_t len = 0, sda_len, sdea_len;
501 u8 ctrl = type;
502 u16 sdea_ctrl = 0;
503 const u8 *forced_addr;
504 size_t cs_num = int_array_len(srv->cipher_suites_list);
505
506 /* Service Descriptor attribute */
507 sda_len = NAN_SERVICE_ID_LEN + 1 + 1 + 1;
508 if (srv->matching_filter_tx && wpabuf_len(srv->matching_filter_tx)) {
509 sda_len += wpabuf_len(srv->matching_filter_tx) + 1;
510 ctrl |= NAN_SRV_CTRL_MATCHING_FILTER;
511 }
512
513 if (srv->srf && wpabuf_len(srv->srf)) {
514 /* SRF length + SRF control */
515 sda_len += 1 + 1 + wpabuf_len(srv->srf);
516 ctrl |= NAN_SRV_CTRL_RESP_FILTER;
517 }
518
519 if ((srv->type == NAN_DE_SUBSCRIBE || srv->type == NAN_DE_PUBLISH) &&
520 srv->close_proximity)
521 ctrl |= NAN_SRV_CTRL_DISCOVERY_RANGE_LIMITED;
522
523 len += NAN_ATTR_HDR_LEN + sda_len;
524
525 /* Service Descriptor Extension attribute */
526 sdea_len = 1 + 2;
527 if (ssi)
528 sdea_len += 2 + 4 + wpabuf_len(ssi);
529 len += NAN_ATTR_HDR_LEN + sdea_len;
530
531 /* Element Container attribute */
532 if (srv->elems)
533 len += NAN_ATTR_HDR_LEN + 1 + wpabuf_len(srv->elems);
534
535 /* NPBA (dialog token, type and status, reason, pbm) */
536 if (srv->pbm && type != NAN_SRV_CTRL_FOLLOW_UP)
537 len += NAN_ATTR_HDR_LEN + 1 + 1 + 1 + 2;
538
539 /* Reserve some additional space for extra attributes */
540 if (de->cb.add_extra_attrs)
541 len += 256;
542
543 len += attrs ? wpabuf_len(attrs) : 0;
544
545 /* Cipher Suite Information Attribute */
546 if (srv->type == NAN_DE_PUBLISH && srv->cipher_suites_list) {
547 len += NAN_ATTR_HDR_LEN + sizeof(struct nan_cipher_suite_info) +
548 cs_num * sizeof(struct nan_cipher_suite);
549 }
550
551 /* Security Context Information Attribute */
552 if (srv->type == NAN_DE_PUBLISH && !dl_list_empty(&srv->pmkid_list)) {
553 unsigned int list_len = dl_list_len(&srv->pmkid_list);
554
555 /* Each entry: sizeof(nan_sec_ctxt) + PMKID_LEN */
556 len += NAN_ATTR_HDR_LEN +
557 list_len * (sizeof(struct nan_sec_ctxt) + PMKID_LEN);
558 }
559
560 buf = nan_de_alloc_sdf(de, dst, len, type);
561 if (!buf)
562 return;
563
564 /* Service Descriptor attribute */
565 wpabuf_put_u8(buf, NAN_ATTR_SDA);
566 wpabuf_put_le16(buf, sda_len);
567 wpabuf_put_data(buf, srv->service_id, NAN_SERVICE_ID_LEN);
568 wpabuf_put_u8(buf, srv->id); /* Instance ID */
569 wpabuf_put_u8(buf, req_instance_id); /* Requestor Instance ID */
570 wpabuf_put_u8(buf, ctrl);
571
572 if (ctrl & NAN_SRV_CTRL_MATCHING_FILTER) {
573 wpabuf_put_u8(buf, wpabuf_len(srv->matching_filter_tx));
574 wpabuf_put_buf(buf, srv->matching_filter_tx);
575 }
576
577 if (ctrl & NAN_SRV_CTRL_RESP_FILTER) {
578 u8 srf_ctrl = 0;
579
580 if (srv->srf_type_bloom_filter)
581 srf_ctrl = NAN_SRF_CTRL_BF;
582
583 if (srv->srf_include)
584 srf_ctrl |= NAN_SRF_CTRL_INCLUDE;
585
586 srf_ctrl |= (srv->srf_bf_idx & NAN_SRF_CTRL_BF_IDX_MSK) <<
587 NAN_SRF_CTRL_BF_IDX_POS;
588 wpabuf_put_u8(buf, wpabuf_len(srv->srf) + 1);
589 wpabuf_put_u8(buf, srf_ctrl);
590 wpabuf_put_buf(buf, srv->srf);
591 }
592
593 /* Service Descriptor Extension attribute */
594 if (srv->type == NAN_DE_PUBLISH || ssi) {
595 if (srv->type == NAN_DE_PUBLISH) {
596 if (srv->publish.fsd)
597 sdea_ctrl |= NAN_SDEA_CTRL_FSD_REQ;
598 if (srv->publish.fsd_gas)
599 sdea_ctrl |= NAN_SDEA_CTRL_FSD_GAS;
600 if (srv->gtk_required)
601 sdea_ctrl |= NAN_SDEA_CTRL_GTK_REQ;
602 if (srv->data_path) {
603 sdea_ctrl |= NAN_SDEA_CTRL_DATA_PATH_REQ;
604 if (srv->cipher_suites_list &&
605 srv->security_required)
606 sdea_ctrl |= NAN_SDEA_CTRL_SECURITY_REQ;
607 }
608 }
609
610 if (sdea_ctrl || ssi) {
611 wpabuf_put_u8(buf, NAN_ATTR_SDEA);
612 wpabuf_put_le16(buf, sdea_len);
613 wpabuf_put_u8(buf, srv->id); /* Instance ID */
614 wpabuf_put_le16(buf, sdea_ctrl);
615 if (ssi) {
616 wpabuf_put_le16(buf, 4 + wpabuf_len(ssi));
617 wpabuf_put_be24(buf, OUI_WFA);
618 wpabuf_put_u8(buf, srv->srv_proto_type);
619 wpabuf_put_buf(buf, ssi);
620 }
621 }
622 }
623
624 /* Element Container attribute */
625 if (srv->elems) {
626 wpabuf_put_u8(buf, NAN_ATTR_ELEM_CONTAINER);
627 wpabuf_put_le16(buf, 1 + wpabuf_len(srv->elems));
628 wpabuf_put_u8(buf, 0); /* Map ID */
629 wpabuf_put_buf(buf, srv->elems);
630 }
631
632 /* Use per-service source address if configured, otherwise use NMI */
633 forced_addr = srv->forced_addr_set ? srv->forced_addr : de->nmi;
634
635 if (srv->pbm && type != NAN_SRV_CTRL_FOLLOW_UP)
636 nan_buf_add_npba(de, srv, buf);
637
638 if (de->cb.add_extra_attrs)
639 de->cb.add_extra_attrs(de->cb.ctx, buf);
640
641 if (attrs) {
642 wpa_printf(MSG_DEBUG, "NAN: Add extra NAN attributes");
643 wpabuf_put_buf(buf, attrs);
644 }
645
646 if (srv->type == NAN_DE_PUBLISH && srv->cipher_suites_list) {
647 size_t i;
648
649 wpabuf_put_u8(buf, NAN_ATTR_CSIA);
650 wpabuf_put_le16(buf, sizeof(struct nan_cipher_suite_info) +
651 cs_num * sizeof(struct nan_cipher_suite));
652 wpabuf_put_u8(buf, srv->security_capab);
653 for (i = 0; i < cs_num; i++) {
654 wpabuf_put_u8(buf, (u8) srv->cipher_suites_list[i]);
655 wpabuf_put_u8(buf, srv->id);
656 }
657 }
658
659 if (srv->type == NAN_DE_PUBLISH && !dl_list_empty(&srv->pmkid_list)) {
660 struct nan_de_pmkid *pmkid;
661 u8 *len_ptr;
662
663 wpabuf_put_u8(buf, NAN_ATTR_SCIA);
664 len_ptr = wpabuf_put(buf, 2); /* length filled later */
665
666 dl_list_for_each(pmkid, &srv->pmkid_list, struct nan_de_pmkid,
667 list) {
668 wpabuf_put_le16(buf, PMKID_LEN);
669 wpabuf_put_u8(buf, NAN_SEC_CTX_TYPE_ND_PMKID);
670 wpabuf_put_u8(buf, srv->id);
671 wpabuf_put_data(buf, pmkid->pmkid, PMKID_LEN);
672 }
673
674 WPA_PUT_LE16(len_ptr, (u8 *) wpabuf_put(buf, 0) - len_ptr - 2);
675 }
676
677 nan_de_tx(de, srv->sync ? 0 : srv->freq, srv->sync ? 0 : wait_time,
678 dst, forced_addr, a3, buf, cookie);
679 wpabuf_free(buf);
680 }
681
682
nan_de_time_to_next_chan_change(struct nan_de_service * srv)683 static int nan_de_time_to_next_chan_change(struct nan_de_service *srv)
684 {
685 struct os_reltime tmp, diff, now;
686
687 if (os_reltime_before(&srv->next_publish_state,
688 &srv->next_publish_chan))
689 tmp = srv->next_publish_state;
690 else if (srv->in_multi_chan)
691 tmp = srv->next_publish_chan;
692 else
693 tmp = srv->next_publish_state;
694
695 os_get_reltime(&now);
696 os_reltime_sub(&tmp, &now, &diff);
697 return os_reltime_in_ms(&diff);
698 }
699
700
nan_de_set_publish_times(struct nan_de_service * srv)701 static void nan_de_set_publish_times(struct nan_de_service *srv)
702 {
703 os_get_reltime(&srv->next_publish_state);
704 srv->next_publish_chan = srv->next_publish_state;
705 /* Swap single/multi channel state in N * 100 TU */
706 os_reltime_add_ms(&srv->next_publish_state,
707 srv->next_publish_duration * 1024 / 1000);
708
709 /* Swap channel in multi channel state after 150 ms */
710 os_reltime_add_ms(&srv->next_publish_chan, 150);
711 }
712
713
nan_de_check_chan_change(struct nan_de_service * srv)714 static void nan_de_check_chan_change(struct nan_de_service *srv)
715 {
716 if (srv->next_publish_duration) {
717 /* Update end times for the first operation of the publish
718 * iteration */
719 nan_de_set_publish_times(srv);
720 srv->next_publish_duration = 0;
721 } else if (srv->in_multi_chan) {
722 if (!os_reltime_initialized(&srv->pause_state_end)) {
723 srv->multi_chan_idx++;
724 if (srv->freq_list[srv->multi_chan_idx] == 0)
725 srv->multi_chan_idx = 0;
726 srv->freq = srv->freq_list[srv->multi_chan_idx];
727 wpa_printf(MSG_DEBUG,
728 "NAN: Publish multi-channel change to %u MHz",
729 srv->freq);
730 }
731 os_get_reltime(&srv->next_publish_chan);
732 os_reltime_add_ms(&srv->next_publish_chan, 150);
733 }
734 }
735
736
nan_de_tx_multicast(struct nan_de * de,struct nan_de_service * srv,u8 req_instance_id)737 static void nan_de_tx_multicast(struct nan_de *de, struct nan_de_service *srv,
738 u8 req_instance_id)
739 {
740 enum nan_service_control_type type;
741 unsigned int wait_time = 100;
742 const u8 *network_id;
743 const u8 *bssid;
744
745 if (srv->type == NAN_DE_PUBLISH) {
746 int ms;
747
748 type = NAN_SRV_CTRL_PUBLISH;
749
750 if (!srv->sync) {
751 nan_de_check_chan_change(srv);
752 ms = nan_de_time_to_next_chan_change(srv);
753 if (ms < 100)
754 ms = 100;
755 wait_time = ms;
756 }
757 } else if (srv->type == NAN_DE_SUBSCRIBE) {
758 type = NAN_SRV_CTRL_SUBSCRIBE;
759 } else {
760 return;
761 }
762
763 if (srv->is_p2p) {
764 network_id = p2p_network_id;
765 bssid = wildcard_bssid;
766 } else {
767 network_id = nan_network_id;
768 bssid = nan_network_id;
769 }
770
771 if (srv->sync) {
772 if (!de->cluster_id_set || !de->dw_freq) {
773 wpa_printf(MSG_DEBUG,
774 "NAN: Cluster ID or DW frequency are not set - skip sync TX");
775 return;
776 }
777
778 wait_time = 0;
779 bssid = de->cluster_id;
780 }
781
782 nan_de_tx_sdf(de, srv, wait_time, type, network_id, bssid,
783 req_instance_id, srv->ssi, NULL, NULL);
784 os_get_reltime(&srv->last_multicast);
785 }
786
787
nan_de_add_srv(struct nan_de * de,struct nan_de_service * srv)788 static void nan_de_add_srv(struct nan_de *de, struct nan_de_service *srv)
789 {
790 int ttl;
791
792 os_get_reltime(&srv->time_started);
793 ttl = srv->type == NAN_DE_PUBLISH ? srv->publish.ttl :
794 srv->subscribe.ttl;
795 if (ttl) {
796 srv->end_time = srv->time_started;
797 srv->end_time.sec += ttl;
798 }
799
800 de->service[srv->id - 1] = srv;
801 de->num_service++;
802 }
803
804
nan_de_del_srv(struct nan_de * de,struct nan_de_service * srv,enum nan_de_reason reason)805 static void nan_de_del_srv(struct nan_de *de, struct nan_de_service *srv,
806 enum nan_de_reason reason)
807 {
808 de->service[srv->id - 1] = NULL;
809 nan_de_service_deinit(de, srv, reason);
810 de->num_service--;
811 if (de->num_service == 0)
812 nan_de_clear_pending(de);
813 }
814
815
nan_de_srv_expired(struct nan_de_service * srv,struct os_reltime * now)816 static bool nan_de_srv_expired(struct nan_de_service *srv,
817 struct os_reltime *now)
818 {
819 if (os_reltime_initialized(&srv->end_time))
820 return os_reltime_before(&srv->end_time, now);
821
822 if (srv->type == NAN_DE_PUBLISH) {
823 /* Time out after one transmission (and wait for FSD) */
824 if (!os_reltime_initialized(&srv->last_multicast))
825 return false;
826 if (!srv->publish.fsd)
827 return true;
828 }
829
830 if (srv->type == NAN_DE_SUBSCRIBE) {
831 /* Time out after first DiscoveryResult event (and wait for
832 * FSD) */
833 if (!os_reltime_initialized(&srv->first_discovered))
834 return false;
835 if (!srv->needs_fsd)
836 return true;
837 }
838
839 return false;
840 }
841
842
nan_de_next_multicast(struct nan_de * de,struct nan_de_service * srv,struct os_reltime * now)843 static int nan_de_next_multicast(struct nan_de *de, struct nan_de_service *srv,
844 struct os_reltime *now)
845 {
846 unsigned int period;
847 struct os_reltime next, diff;
848
849 if (srv->type == NAN_DE_PUBLISH && !srv->publish.unsolicited)
850 return -1;
851 if (srv->type == NAN_DE_SUBSCRIBE && !srv->subscribe.active)
852 return -1;
853
854 if (!os_reltime_initialized(&srv->last_multicast))
855 return 0;
856
857 if (srv->type == NAN_DE_PUBLISH && srv->publish.ttl == 0)
858 return -1;
859
860 if (srv->type == NAN_DE_PUBLISH &&
861 os_reltime_initialized(&srv->pause_state_end))
862 return -1;
863
864 period = srv->type == NAN_DE_PUBLISH ?
865 srv->publish.announcement_period :
866 srv->subscribe.query_period;
867 if (period == 0)
868 period = 100;
869 next = srv->last_multicast;
870 os_reltime_add_ms(&next, period);
871
872 if (srv->type == NAN_DE_PUBLISH) {
873 if (!de->tx_wait_end_freq && srv->publish.unsolicited &&
874 os_reltime_before(&next, now))
875 return 0;
876 next = srv->next_publish_state;
877 }
878
879 if (os_reltime_before(&next, now))
880 return 0;
881
882 os_reltime_sub(&next, now, &diff);
883 return os_reltime_in_ms(&diff);
884 }
885
886
nan_de_srv_time_to_next(struct nan_de * de,struct nan_de_service * srv,struct os_reltime * now)887 static int nan_de_srv_time_to_next(struct nan_de *de,
888 struct nan_de_service *srv,
889 struct os_reltime *now)
890 {
891 struct os_reltime diff;
892 int next = -1, tmp;
893
894 if (os_reltime_initialized(&srv->end_time)) {
895 os_reltime_sub(&srv->end_time, now, &diff);
896 tmp = os_reltime_in_ms(&diff);
897 if (next == -1 || tmp < next)
898 next = tmp;
899 }
900
901 if (srv->type == NAN_DE_PUBLISH &&
902 srv->publish.fsd &&
903 os_reltime_initialized(&srv->pause_state_end)) {
904 os_reltime_sub(&srv->pause_state_end, now, &diff);
905 tmp = os_reltime_in_ms(&diff);
906 if (next == -1 || tmp < next)
907 next = tmp;
908 return next;
909 }
910
911 tmp = nan_de_next_multicast(de, srv, now);
912 if (tmp >= 0 && (next == -1 || tmp < next))
913 next = tmp;
914
915 if (srv->type == NAN_DE_PUBLISH &&
916 os_reltime_initialized(&srv->last_multicast)) {
917 /* Time out after one transmission (and wait for FSD) */
918 tmp = srv->publish.fsd ? 1000 : 100;
919 if (next == -1 || tmp < next)
920 next = tmp;
921 }
922
923 if (srv->type == NAN_DE_SUBSCRIBE &&
924 os_reltime_initialized(&srv->first_discovered)) {
925 /* Time out after first DiscoveryResult event (and wait for
926 * FSD) */
927 tmp = srv->needs_fsd ? 1000 : 100;
928 if (next == -1 || tmp < next)
929 next = tmp;
930 }
931
932 if (os_reltime_initialized(&srv->next_publish_state)) {
933 os_reltime_sub(&srv->next_publish_state, now, &diff);
934 if (diff.sec < 0 || (diff.sec == 0 && diff.usec < 0))
935 tmp = 0;
936 else
937 tmp = os_reltime_in_ms(&diff);
938 if (next == -1 || tmp < next)
939 next = tmp;
940 }
941
942 return next;
943 }
944
945
nan_de_start_new_publish_state(struct nan_de * de,struct nan_de_service * srv,bool force_single)946 static void nan_de_start_new_publish_state(struct nan_de *de,
947 struct nan_de_service *srv,
948 bool force_single)
949 {
950 unsigned int n;
951
952 if (srv->sync)
953 return;
954
955 if (force_single || !srv->freq_list || srv->freq_list[0] == 0)
956 srv->in_multi_chan = false;
957 else
958 srv->in_multi_chan = !srv->in_multi_chan;
959
960 /* Use same values for N and M. */
961 n = de->cfg.n_min + os_random() % (de->cfg.n_max - de->cfg.n_min);
962 srv->next_publish_duration = n * 100;
963
964 nan_de_set_publish_times(srv);
965
966 if (os_reltime_initialized(&srv->pause_state_end))
967 return;
968
969 if (srv->in_multi_chan && srv->freq_list && srv->freq_list[0]) {
970 if (!srv->first_multi_chan)
971 srv->multi_chan_idx++;
972 if (srv->freq_list[srv->multi_chan_idx] == 0)
973 srv->multi_chan_idx = 0;
974 srv->first_multi_chan = false;
975 srv->freq = srv->freq_list[srv->multi_chan_idx];
976 } else {
977 srv->freq = srv->default_freq;
978 }
979
980 wpa_printf(MSG_DEBUG,
981 "NAN: Publish in %s channel state for %u TU; starting with %u MHz",
982 srv->in_multi_chan ? "multi" : "single", n * 100, srv->freq);
983 }
984
985
nan_de_listen_duration(struct nan_de * de,struct nan_de_service * srv)986 static u32 nan_de_listen_duration(struct nan_de *de, struct nan_de_service *srv)
987 {
988 u32 duration = 1000;
989 u32 max_duration = de->max_listen;
990
991 /* Limit the listen duration based on the maximal 'N' value */
992 if (de->cfg.n_max && de->cfg.n_max * 100 < max_duration)
993 max_duration = de->cfg.n_max * 100;
994
995 if (srv->type == NAN_DE_PUBLISH) {
996 nan_de_check_chan_change(srv);
997 duration = nan_de_time_to_next_chan_change(srv);
998 if (duration < 150)
999 duration = 150;
1000 }
1001
1002 return MIN(duration, max_duration);
1003 }
1004
1005
nan_de_timer(void * eloop_ctx,void * timeout_ctx)1006 static void nan_de_timer(void *eloop_ctx, void *timeout_ctx)
1007 {
1008 struct nan_de *de = eloop_ctx;
1009 unsigned int i;
1010 int next = -1;
1011 bool started = false;
1012 struct os_reltime now;
1013
1014 os_get_reltime(&now);
1015
1016 /* Based on the USD specification, the device should always be either on
1017 * the default channel or one of the configured channels. However, to
1018 * allow operation of other interfaces, suspend the USD functionality
1019 * based on the cycle and suspend parameters. This would lower the
1020 * probability of service discovery, but would allow functionality of
1021 * other interfaces.
1022 */
1023 if (!de->listen_freq && de->cfg.cycle) {
1024 u32 diff_ms;
1025
1026 if (os_reltime_initialized(&de->suspend_cycle_start)) {
1027 struct os_reltime diff;
1028
1029 os_reltime_sub(&now, &de->suspend_cycle_start, &diff);
1030 diff_ms = os_reltime_in_ms(&diff);
1031 } else {
1032 /* We want to start a new cycle */
1033 diff_ms = de->cfg.cycle;
1034 }
1035
1036 if (diff_ms < de->cfg.suspend) {
1037 wpa_printf(MSG_DEBUG,
1038 "NAN: USD: Suspend in progress: diff_ms=%u",
1039 diff_ms);
1040
1041 /* Set the timer to fire at the end of the suspend */
1042 diff_ms = de->cfg.suspend - diff_ms;
1043 } else if (diff_ms >= de->cfg.cycle) {
1044 wpa_printf(MSG_DEBUG,
1045 "NAN: Suspend USD for %u ms (passed=%u ms)",
1046 de->cfg.suspend, diff_ms);
1047 de->suspend_cycle_start = now;
1048
1049 /* Set the timer to fire at the end of the suspend */
1050 diff_ms = de->cfg.suspend;
1051 } else {
1052 diff_ms = 0;
1053 }
1054
1055 if (diff_ms) {
1056 wpa_printf(MSG_DEBUG, "NAN: diff_ms=%u ms", diff_ms);
1057
1058 eloop_register_timeout(diff_ms / 1000,
1059 (diff_ms % 1000) * 1000,
1060 nan_de_timer, de, NULL);
1061 return;
1062 }
1063 }
1064
1065 for (i = 0; i < NAN_DE_MAX_SERVICE; i++) {
1066 struct nan_de_service *srv = de->service[i];
1067 int srv_next;
1068
1069 if (!srv)
1070 continue;
1071
1072 if (nan_de_srv_expired(srv, &now)) {
1073 wpa_printf(MSG_DEBUG, "NAN: Service id %d expired",
1074 srv->id);
1075 if (srv->type == NAN_DE_PUBLISH &&
1076 de->cb.offload_cancel_publish)
1077 de->cb.offload_cancel_publish(de->cb.ctx,
1078 srv->id);
1079 if (srv->type == NAN_DE_SUBSCRIBE &&
1080 de->cb.offload_cancel_subscribe)
1081 de->cb.offload_cancel_subscribe(de->cb.ctx,
1082 srv->id);
1083 nan_de_del_srv(de, srv, NAN_DE_REASON_TIMEOUT);
1084 continue;
1085 }
1086
1087 if (srv->sync)
1088 continue;
1089
1090 if (os_reltime_initialized(&srv->next_publish_state) &&
1091 os_reltime_before(&srv->next_publish_state, &now))
1092 nan_de_start_new_publish_state(de, srv, false);
1093
1094 if (srv->type == NAN_DE_PUBLISH &&
1095 os_reltime_initialized(&srv->pause_state_end) &&
1096 (os_reltime_before(&srv->pause_state_end, &now)))
1097 nan_de_unpause_state(srv);
1098
1099 srv_next = nan_de_srv_time_to_next(de, srv, &now);
1100 if (srv_next >= 0 && (next == -1 || srv_next < next))
1101 next = srv_next;
1102
1103 if (srv->type == NAN_DE_PUBLISH &&
1104 srv->publish.fsd &&
1105 os_reltime_initialized(&srv->pause_state_end) &&
1106 de->tx_wait_end_freq == 0 &&
1107 de->listen_freq == 0 && de->ext_listen_freq == 0) {
1108 struct os_reltime diff;
1109 int duration;
1110
1111 os_reltime_sub(&srv->pause_state_end, &now, &diff);
1112 duration = os_reltime_in_ms(&diff);
1113 if (duration < 0)
1114 continue;
1115 if (srv->listen_stopped) {
1116 wpa_printf(MSG_DEBUG,
1117 "NAN: Publisher listen stopped temporarily - do not start driver listen operation");
1118 continue;
1119 }
1120 if ((unsigned int) duration > de->max_listen)
1121 duration = de->max_listen;
1122 if (de->cb.listen(de->cb.ctx, srv->freq, duration,
1123 srv->forced_addr_set ?
1124 srv->forced_addr : NULL) == 0) {
1125 wpa_printf(MSG_DEBUG,
1126 "NAN: Publisher in pauseState - started listen on %u MHz",
1127 srv->freq);
1128 de->listen_freq = srv->freq;
1129 return;
1130 }
1131 }
1132
1133 if (srv_next == 0 && !started && !de->offload &&
1134 de->listen_freq == 0 && de->ext_listen_freq == 0 &&
1135 de->tx_wait_end_freq == 0 &&
1136 nan_de_next_multicast(de, srv, &now) == 0) {
1137 started = true;
1138 nan_de_tx_multicast(de, srv, 0);
1139 }
1140
1141 if (!started && !de->offload && de->cb.listen &&
1142 de->listen_freq == 0 && de->ext_listen_freq == 0 &&
1143 de->tx_wait_end_freq == 0 &&
1144 ((srv->type == NAN_DE_PUBLISH &&
1145 !srv->publish.unsolicited && srv->publish.solicited) ||
1146 (srv->type == NAN_DE_SUBSCRIBE &&
1147 !srv->subscribe.active))) {
1148 u32 duration;
1149
1150 if (srv->listen_stopped) {
1151 wpa_printf(MSG_DEBUG,
1152 "NAN: Listen stopped temporarily - do not start driver listen operation");
1153 continue;
1154 }
1155
1156 duration = nan_de_listen_duration(de, srv);
1157
1158 started = true;
1159 if (de->cb.listen(de->cb.ctx, srv->freq, duration,
1160 srv->forced_addr_set ?
1161 srv->forced_addr : NULL) == 0)
1162 de->listen_freq = srv->freq;
1163 }
1164
1165 }
1166
1167 if (next < 0)
1168 return;
1169
1170 if (next == 0)
1171 next = 1;
1172
1173 eloop_register_timeout(next / 1000, (next % 1000) * 1000, nan_de_timer,
1174 de, NULL);
1175 }
1176
1177
nan_de_run_timer(struct nan_de * de)1178 static void nan_de_run_timer(struct nan_de *de)
1179 {
1180 eloop_cancel_timeout(nan_de_timer, de, NULL);
1181 eloop_register_timeout(0, 0, nan_de_timer, de, NULL);
1182 }
1183
1184
nan_de_deinit(struct nan_de * de)1185 void nan_de_deinit(struct nan_de *de)
1186 {
1187 eloop_cancel_timeout(nan_de_timer, de, NULL);
1188 nan_de_flush(de);
1189 os_free(de);
1190 }
1191
1192
nan_de_listen_started(struct nan_de * de,unsigned int freq,unsigned int duration)1193 void nan_de_listen_started(struct nan_de *de, unsigned int freq,
1194 unsigned int duration)
1195 {
1196 if (freq != de->listen_freq)
1197 de->ext_listen_freq = freq;
1198 }
1199
1200
nan_de_listen_ended(struct nan_de * de,unsigned int freq)1201 void nan_de_listen_ended(struct nan_de *de, unsigned int freq)
1202 {
1203 if (freq == de->ext_listen_freq)
1204 de->ext_listen_freq = 0;
1205
1206 if (freq == de->listen_freq) {
1207 de->listen_freq = 0;
1208 nan_de_run_timer(de);
1209 }
1210 }
1211
1212
nan_de_update_nmi(struct nan_de * de,const u8 * nmi)1213 void nan_de_update_nmi(struct nan_de *de, const u8 *nmi)
1214 {
1215 if (de)
1216 os_memcpy(de->nmi, nmi, ETH_ALEN);
1217 }
1218
1219
nan_de_tx_status(struct nan_de * de,unsigned int freq,const u8 * dst,const u8 * data,size_t data_len,bool ack)1220 void nan_de_tx_status(struct nan_de *de, unsigned int freq, const u8 *dst,
1221 const u8 *data, size_t data_len, bool ack)
1222 {
1223 if (freq == de->tx_wait_status_freq)
1224 de->tx_wait_status_freq = 0;
1225
1226 nan_de_tx_status_match(de, data, data_len, ack);
1227 }
1228
1229
nan_de_tx_wait_ended(struct nan_de * de)1230 void nan_de_tx_wait_ended(struct nan_de *de)
1231 {
1232 if (de->tx_wait_end_freq)
1233 wpa_printf(MSG_DEBUG,
1234 "NAN: TX wait for response ended (freq=%u)",
1235 de->tx_wait_end_freq);
1236
1237 nan_de_flush_tracked_tx(de, NAN_DE_FLUSH_TRACKED_TX_WAIT_EXPIRED);
1238
1239 de->tx_wait_end_freq = 0;
1240 nan_de_run_timer(de);
1241 }
1242
1243
1244 static const u8 *
nan_de_get_attr(const u8 * buf,size_t len,enum nan_attr_id id,unsigned int skip)1245 nan_de_get_attr(const u8 *buf, size_t len, enum nan_attr_id id,
1246 unsigned int skip)
1247 {
1248 const u8 *pos = buf, *end = buf + len;
1249
1250 while (end - pos >= NAN_ATTR_HDR_LEN) {
1251 const u8 *attr = pos;
1252 u8 attr_id;
1253 u16 attr_len;
1254
1255 attr_id = *pos++;
1256 attr_len = WPA_GET_LE16(pos);
1257 pos += 2;
1258 if (attr_len > end - pos) {
1259 wpa_printf(MSG_DEBUG,
1260 "NAN: Truncated attribute %u (len %u; left %zu)",
1261 attr_id, attr_len, end - pos);
1262 break;
1263 }
1264
1265 if (attr_id == id) {
1266 if (skip == 0)
1267 return attr;
1268 skip--;
1269 }
1270
1271 pos += attr_len;
1272 }
1273
1274 return NULL;
1275 }
1276
1277
nan_de_get_sdea(const u8 * buf,size_t len,u8 instance_id,u16 * sdea_control,enum nan_service_protocol_type * srv_proto_type,const u8 ** ssi,size_t * ssi_len)1278 static void nan_de_get_sdea(const u8 *buf, size_t len, u8 instance_id,
1279 u16 *sdea_control,
1280 enum nan_service_protocol_type *srv_proto_type,
1281 const u8 **ssi, size_t *ssi_len)
1282 {
1283 unsigned int skip;
1284 const u8 *sdea, *end;
1285 u16 sdea_len;
1286
1287 for (skip = 0; ; skip++) {
1288 sdea = nan_de_get_attr(buf, len, NAN_ATTR_SDEA, skip);
1289 if (!sdea)
1290 break;
1291
1292 sdea++;
1293 sdea_len = WPA_GET_LE16(sdea);
1294 sdea += 2;
1295 if (sdea_len < 1 + 2)
1296 continue;
1297 end = sdea + sdea_len;
1298
1299 if (instance_id != *sdea++)
1300 continue; /* Mismatching Instance ID */
1301
1302 *sdea_control = WPA_GET_LE16(sdea);
1303 sdea += 2;
1304
1305 if (*sdea_control & NAN_SDEA_CTRL_RANGE_LIMIT) {
1306 if (end - sdea < 4)
1307 continue;
1308 sdea += 4;
1309 }
1310
1311 if (*sdea_control & NAN_SDEA_CTRL_SRV_UPD_INDIC) {
1312 if (end - sdea < 1)
1313 continue;
1314 sdea++;
1315 }
1316
1317 if (end - sdea >= 2) {
1318 u16 srv_info_len;
1319
1320 srv_info_len = WPA_GET_LE16(sdea);
1321 sdea += 2;
1322
1323 if (srv_info_len > end - sdea)
1324 continue;
1325
1326 if (srv_info_len >= 4 &&
1327 WPA_GET_BE24(sdea) == OUI_WFA) {
1328 *srv_proto_type = sdea[3];
1329 *ssi = sdea + 4;
1330 *ssi_len = srv_info_len - 4;
1331 }
1332 }
1333 }
1334 }
1335
1336
nan_de_parse_csia(const u8 * buf,size_t len,u8 instance_id,u8 * cipher_suites,unsigned int max_cipher_suites,u8 * capabilities)1337 static unsigned int nan_de_parse_csia(const u8 *buf, size_t len, u8 instance_id,
1338 u8 *cipher_suites,
1339 unsigned int max_cipher_suites,
1340 u8 *capabilities)
1341 {
1342 const u8 *csia, *pos, *end;
1343 u16 csia_len;
1344 unsigned int cs_count = 0;
1345 const struct nan_cipher_suite_info *cs_info;
1346
1347 csia = nan_de_get_attr(buf, len, NAN_ATTR_CSIA, 0);
1348 if (!csia)
1349 return 0;
1350
1351 csia++;
1352 csia_len = WPA_GET_LE16(csia);
1353 csia += 2;
1354
1355 if (csia_len < 1)
1356 return 0;
1357
1358 wpa_printf(MSG_DEBUG,
1359 "NAN: Parsing Cipher Suite Information attribute (len=%u)",
1360 csia_len);
1361
1362 cs_info = (const struct nan_cipher_suite_info *) csia;
1363
1364 if (capabilities)
1365 *capabilities = cs_info->capab;
1366
1367 pos = cs_info->cs;
1368 end = csia + csia_len;
1369
1370 /* Parse cipher suite list. Each entry is 2 bytes (csid + publish_id) */
1371 while (end - pos >= 2 && cs_count < max_cipher_suites) {
1372 u8 csid = *pos++;
1373 u8 publish_id = *pos++;
1374
1375 if (csid == NAN_CS_NONE || csid >= NAN_CS_MAX) {
1376 wpa_printf(MSG_DEBUG,
1377 "NAN: Invalid cipher suite ID %u for publish ID %u",
1378 csid, publish_id);
1379 continue;
1380 }
1381
1382 wpa_printf(MSG_DEBUG,
1383 "NAN: Cipher suite ID %u for publish ID %u",
1384 csid, publish_id);
1385
1386 /* Only include cipher suites for the matching publish ID */
1387 if (publish_id == instance_id) {
1388 cipher_suites[cs_count++] = csid;
1389 wpa_printf(MSG_DEBUG,
1390 "NAN: Added cipher suite %u for matching publish ID %u",
1391 csid, instance_id);
1392 }
1393 }
1394
1395 wpa_printf(MSG_DEBUG,
1396 "NAN: Parsed %u cipher suites from CSIA for publish ID %u",
1397 cs_count, instance_id);
1398
1399 return cs_count;
1400 }
1401
1402
nan_de_parse_scia(const u8 * buf,size_t len,u8 instance_id,u8 * pmkid_list,unsigned int max_pmkids)1403 static unsigned int nan_de_parse_scia(const u8 *buf, size_t len, u8 instance_id,
1404 u8 *pmkid_list, unsigned int max_pmkids)
1405 {
1406 const u8 *scia, *end;
1407 u16 scia_len;
1408 unsigned int pmkid_count = 0;
1409
1410 scia = nan_de_get_attr(buf, len, NAN_ATTR_SCIA, 0);
1411 if (!scia)
1412 return 0;
1413
1414 scia++;
1415 scia_len = WPA_GET_LE16(scia);
1416 scia += 2;
1417
1418 end = scia + scia_len;
1419
1420 wpa_printf(MSG_DEBUG,
1421 "NAN: Parsing Security Context Information attribute (len=%u)",
1422 scia_len);
1423
1424 /* Parse list of Security Context Identifiers */
1425 while ((size_t) (end - scia) >= sizeof(struct nan_sec_ctxt)) {
1426 const struct nan_sec_ctxt *sec_ctx =
1427 (const struct nan_sec_ctxt *) scia;
1428 u16 scid_len = le_to_host16(sec_ctx->len);
1429
1430 if (scid_len + sizeof(*sec_ctx) > (size_t) (end - scia)) {
1431 wpa_printf(MSG_DEBUG,
1432 "NAN: Invalid SCID length %u (remaining %zu)",
1433 scid_len, (size_t) (end - scia));
1434 break;
1435 }
1436
1437 /* Check if this is for our instance_id and is a PMKID type */
1438 if (sec_ctx->scid == NAN_SEC_CTX_TYPE_ND_PMKID &&
1439 sec_ctx->instance_id == instance_id) {
1440 if (scid_len == PMKID_LEN && pmkid_count < max_pmkids) {
1441 os_memcpy(&pmkid_list[pmkid_count * PMKID_LEN],
1442 sec_ctx->ctxt, PMKID_LEN);
1443 pmkid_count++;
1444 wpa_hexdump(MSG_DEBUG, "NAN: Parsed PMKID",
1445 sec_ctx->ctxt, PMKID_LEN);
1446 } else {
1447 wpa_printf(MSG_DEBUG,
1448 "NAN: Unexpected SCID length %u or max PMKIDs reached",
1449 scid_len);
1450 }
1451 }
1452
1453 scia += scid_len + sizeof(*sec_ctx);
1454 }
1455
1456 wpa_printf(MSG_DEBUG, "NAN: Parsed %u PMKIDs from SCIA", pmkid_count);
1457
1458 return pmkid_count;
1459 }
1460
1461
nan_de_process_elem_container(struct nan_de * de,const u8 * buf,size_t len,const u8 * peer_addr,unsigned int freq,bool p2p,bool pr)1462 static void nan_de_process_elem_container(struct nan_de *de, const u8 *buf,
1463 size_t len, const u8 *peer_addr,
1464 unsigned int freq, bool p2p, bool pr)
1465 {
1466 const u8 *elem;
1467 u16 elem_len;
1468
1469 elem = nan_de_get_attr(buf, len, NAN_ATTR_ELEM_CONTAINER, 0);
1470 if (!elem)
1471 return;
1472
1473 elem++;
1474 elem_len = WPA_GET_LE16(elem);
1475 elem += 2;
1476 /* Skip the attribute if there is not enough froom for an element. */
1477 if (elem_len < 1 + 2)
1478 return;
1479
1480 /* Skip Map ID */
1481 elem++;
1482 elem_len--;
1483
1484 if (p2p && de->cb.process_p2p_usd_elems)
1485 de->cb.process_p2p_usd_elems(de->cb.ctx, elem, elem_len,
1486 peer_addr, freq);
1487 if (pr && de->cb.process_pr_usd_elems)
1488 de->cb.process_pr_usd_elems(de->cb.ctx, elem, elem_len,
1489 peer_addr, freq);
1490 }
1491
1492
nan_de_parse_dcea(const u8 * buf,size_t len,bool * pairing_setup,bool * npk_nik_caching)1493 static void nan_de_parse_dcea(const u8 *buf, size_t len, bool *pairing_setup,
1494 bool *npk_nik_caching)
1495 {
1496 const u8 *dcea;
1497 u16 dcea_len;
1498
1499 *pairing_setup = false;
1500 *npk_nik_caching = false;
1501
1502 dcea = nan_de_get_attr(buf, len, NAN_ATTR_DCEA, 0);
1503 if (!dcea)
1504 return;
1505
1506 dcea_len = WPA_GET_LE16(dcea + 1);
1507 if (dcea_len < 2) {
1508 wpa_printf(MSG_DEBUG, "NAN: DCEA length=%u too short",
1509 dcea_len);
1510 return;
1511 }
1512
1513 *pairing_setup = !!(dcea[4] & NAN_DEV_CAPA_EXT_INFO_1_PAIRING_SETUP);
1514 *npk_nik_caching = !!(dcea[4] &
1515 NAN_DEV_CAPA_EXT_INFO_1_NPK_NIK_CACHING);
1516 }
1517
1518
nan_de_get_advertise_pbm(const u8 * buf,size_t len)1519 static u16 nan_de_get_advertise_pbm(const u8 *buf, size_t len)
1520 {
1521 const u8 *npba;
1522 u16 npba_len;
1523
1524 npba = nan_de_get_attr(buf, len, NAN_ATTR_NPBA, 0);
1525 if (!npba)
1526 return 0;
1527
1528 npba_len = WPA_GET_LE16(npba + 1);
1529 if (npba_len < 5) {
1530 wpa_printf(MSG_DEBUG, "NAN: Invalid NPBA length %u", npba_len);
1531 return 0;
1532 }
1533
1534 /* Skip the attribute ID and length */
1535 npba += NAN_ATTR_HDR_LEN;
1536 if ((npba[1] & NAN_PBA_TYPE_MASK) != NAN_PBA_TYPE_ADVERTISE)
1537 return 0;
1538
1539 return WPA_GET_LE16(npba + 3);
1540 }
1541
1542
nan_de_filter_match(struct nan_de_service * srv,const u8 * matching_filter,size_t matching_filter_len)1543 static bool nan_de_filter_match(struct nan_de_service *srv,
1544 const u8 *matching_filter,
1545 size_t matching_filter_len)
1546 {
1547 const u8 *spos, *spos_end, *ppos, *ppos_end;
1548 const u8 *publish_filter = NULL, *subscribe_filter = NULL;
1549 u8 publish_filter_len = 0, subscribe_filter_len = 0;
1550
1551 wpa_printf(MSG_DEBUG,
1552 "NAN: Check matching filter for service id %d type %d",
1553 srv->id, srv->type);
1554
1555 if (srv->type == NAN_DE_PUBLISH) {
1556 if (srv->matching_filter_rx) {
1557 publish_filter =
1558 wpabuf_head_u8(srv->matching_filter_rx);
1559 publish_filter_len =
1560 wpabuf_len(srv->matching_filter_rx);
1561 }
1562 subscribe_filter = matching_filter;
1563 subscribe_filter_len = matching_filter_len;
1564 } else if (srv->type == NAN_DE_SUBSCRIBE) {
1565 if (srv->matching_filter_rx) {
1566 subscribe_filter =
1567 wpabuf_head_u8(srv->matching_filter_rx);
1568 subscribe_filter_len =
1569 wpabuf_len(srv->matching_filter_rx);
1570 }
1571 publish_filter = matching_filter;
1572 publish_filter_len = matching_filter_len;
1573 } else {
1574 wpa_printf(MSG_DEBUG,
1575 "NAN: Unsupported service type %d for matching filter",
1576 srv->type);
1577 return false;
1578 }
1579
1580 if (!subscribe_filter)
1581 return true;
1582
1583 spos = subscribe_filter;
1584 spos_end = subscribe_filter + subscribe_filter_len;
1585
1586 ppos = publish_filter;
1587 ppos_end = publish_filter ? publish_filter + publish_filter_len : NULL;
1588
1589 wpa_hexdump(MSG_DEBUG, "NAN: subscribe filter",
1590 spos, spos_end - spos);
1591 if (ppos)
1592 wpa_hexdump(MSG_DEBUG, "NAN: publish filter",
1593 ppos, ppos_end - ppos);
1594
1595 while (spos < spos_end) {
1596 u8 slen, plen = 0;
1597
1598 slen = *spos++;
1599
1600 /* Invalid filter length - do not match */
1601 if (slen > spos_end - spos)
1602 return false;
1603
1604 /* Read publish filter */
1605 if (ppos && ppos < ppos_end) {
1606 plen = *ppos++;
1607 if (plen > ppos_end - ppos)
1608 return false;
1609 }
1610
1611 if (slen > 0) {
1612 if (!ppos)
1613 return false;
1614
1615 /* For non zero filters, compare */
1616 if (plen &&
1617 (plen != slen || os_memcmp(spos, ppos, plen) != 0))
1618 return false;
1619
1620 /* Filter matches */
1621 }
1622
1623 spos += slen;
1624
1625 /*
1626 * If ppos is NULL we can still have match if the subscribe
1627 * filter is <0><0>...
1628 */
1629 if (!ppos)
1630 continue;
1631
1632 ppos += plen;
1633
1634 /* Publish filter is over */
1635 if (ppos >= ppos_end && spos < spos_end)
1636 return false;
1637 }
1638
1639 return true;
1640 }
1641
1642
nan_de_rx_publish(struct nan_de * de,struct nan_de_service * srv,const u8 * peer_addr,const u8 * a3,u8 instance_id,const u8 * matching_filter,size_t matching_filter_len,u8 req_instance_id,u16 sdea_control,enum nan_service_protocol_type srv_proto_type,const u8 * ssi,size_t ssi_len,bool range_limit,int rssi,const u8 * buf,size_t buf_len)1643 static bool nan_de_rx_publish(struct nan_de *de, struct nan_de_service *srv,
1644 const u8 *peer_addr, const u8 *a3, u8 instance_id,
1645 const u8 *matching_filter,
1646 size_t matching_filter_len,
1647 u8 req_instance_id, u16 sdea_control,
1648 enum nan_service_protocol_type srv_proto_type,
1649 const u8 *ssi, size_t ssi_len,
1650 bool range_limit, int rssi,
1651 const u8 *buf, size_t buf_len)
1652 {
1653 struct nan_discovery_result res;
1654
1655 /* The SCIA can potentially contain a PMKID for each cipher suite */
1656 u8 pmkid_list[(NAN_CS_MAX - 1) * PMKID_LEN];
1657 unsigned int pmkid_count = 0;
1658 /* Cipher suites from CSIA */
1659 u8 cipher_suites[NAN_CS_MAX - 1];
1660 unsigned int cipher_suite_count = 0;
1661
1662 if (!nan_de_filter_match(srv, matching_filter, matching_filter_len))
1663 return false;
1664
1665 /* Skip USD logic */
1666 if (srv->sync)
1667 goto send_event;
1668
1669 if ((range_limit || srv->close_proximity) &&
1670 de->rssi_threshold && rssi) {
1671 if (rssi < de->rssi_threshold) {
1672 wpa_printf(MSG_DEBUG,
1673 "NAN: Discard SDA with RSSI %d below threshold %d",
1674 rssi, de->rssi_threshold);
1675 return false;
1676 }
1677 }
1678
1679 /* Subscribe function processing of a receive Publish message */
1680 if (!os_reltime_initialized(&srv->first_discovered)) {
1681 os_get_reltime(&srv->first_discovered);
1682 srv->needs_fsd = sdea_control & NAN_SDEA_CTRL_FSD_REQ;
1683 nan_de_run_timer(de);
1684 }
1685
1686 if (!de->offload && srv->subscribe.active && req_instance_id == 0) {
1687 /* Active subscriber replies with a Subscribe message if it
1688 * received a matching unsolicited Publish message. */
1689 nan_de_tx_multicast(de, srv, instance_id);
1690 }
1691
1692 if (!de->offload && !srv->subscribe.active && req_instance_id == 0) {
1693 /* Passive subscriber replies with a Follow-up message without
1694 * Service Specific Info field if it received a matching
1695 * unsolicited Publish message. */
1696 nan_de_transmit(de, srv->id, NULL, NULL, peer_addr,
1697 instance_id, NULL, NULL);
1698 }
1699
1700 send_event:
1701 os_memset(&res, 0, sizeof(res));
1702 if (buf && buf_len > 0) {
1703 /* Parse Cipher Suite Information Attribute */
1704 cipher_suite_count = nan_de_parse_csia(
1705 buf, buf_len, instance_id, cipher_suites,
1706 ARRAY_SIZE(cipher_suites), NULL);
1707
1708 /* Parse Security Context Information attribute */
1709 pmkid_count = nan_de_parse_scia(buf, buf_len, instance_id,
1710 pmkid_list,
1711 sizeof(pmkid_list) / PMKID_LEN);
1712
1713 /*
1714 * Parse Device Capability Extension attribute for pairing
1715 * setup and NPK/NIK caching support
1716 */
1717 nan_de_parse_dcea(buf, buf_len,
1718 &res.pairing_setup_supp,
1719 &res.npk_nik_caching_supp);
1720
1721 /* Get the bootstrapping methods */
1722 res.pbm = nan_de_get_advertise_pbm(buf, buf_len);
1723 }
1724
1725 res.subscribe_id = srv->id;
1726 res.srv_proto_type = srv_proto_type;
1727 res.ssi = ssi;
1728 res.ssi_len = ssi_len;
1729 res.peer_publish_id = instance_id;
1730 res.peer_addr = peer_addr;
1731 res.fsd = !!(sdea_control & NAN_SDEA_CTRL_FSD_REQ);
1732 res.fsd_gas = !!(sdea_control & NAN_SDEA_CTRL_FSD_GAS);
1733 res.data_path = !!(sdea_control & NAN_SDEA_CTRL_DATA_PATH_REQ);
1734 res.security_required = !!(sdea_control & NAN_SDEA_CTRL_SECURITY_REQ);
1735 res.cipher_suites = cipher_suite_count > 0 ? cipher_suites : NULL;
1736 res.n_cipher_suites = cipher_suite_count;
1737 res.pmkid_list = pmkid_count > 0 ? pmkid_list : NULL;
1738 res.pmkid_count = pmkid_count;
1739
1740 if (de->cb.discovery_result)
1741 de->cb.discovery_result(de->cb.ctx, &res);
1742
1743 return true;
1744 }
1745
1746
nan_de_rx_subscribe(struct nan_de * de,struct nan_de_service * srv,const u8 * peer_addr,const u8 * a3,u8 instance_id,const u8 * matching_filter,size_t matching_filter_len,enum nan_service_protocol_type srv_proto_type,const u8 * ssi,size_t ssi_len,bool range_limit,int rssi)1747 static bool nan_de_rx_subscribe(struct nan_de *de, struct nan_de_service *srv,
1748 const u8 *peer_addr, const u8 *a3,
1749 u8 instance_id,
1750 const u8 *matching_filter,
1751 size_t matching_filter_len,
1752 enum nan_service_protocol_type srv_proto_type,
1753 const u8 *ssi, size_t ssi_len,
1754 bool range_limit, int rssi)
1755 {
1756 const u8 *network_id;
1757
1758 /* Publish function processing of a receive Subscribe message */
1759
1760 if (!nan_de_filter_match(srv, matching_filter, matching_filter_len))
1761 return false;
1762
1763 if ((range_limit || srv->close_proximity) &&
1764 de->rssi_threshold && rssi) {
1765 if (rssi < de->rssi_threshold) {
1766 wpa_printf(MSG_DEBUG,
1767 "NAN: Discard SDA with RSSI %d below threshold %d",
1768 rssi, de->rssi_threshold);
1769 return false;
1770 }
1771 }
1772
1773 if (!srv->publish.solicited)
1774 return false;
1775
1776 if (os_reltime_initialized(&srv->pause_state_end) &&
1777 (!ether_addr_equal(peer_addr, srv->sel_peer_addr) ||
1778 instance_id != srv->sel_peer_id)) {
1779 wpa_printf(MSG_DEBUG,
1780 "NAN: In pauseState - ignore Subscribe message from another subscriber");
1781 return false;
1782 }
1783
1784 if (de->offload)
1785 goto offload;
1786
1787 /* Reply with a solicited Publish message */
1788
1789 if (srv->is_p2p)
1790 network_id = p2p_network_id;
1791 else
1792 network_id = nan_network_id;
1793
1794 if (srv->sync && de->cluster_id_set)
1795 a3 = de->cluster_id;
1796 else if (srv->publish.solicited_multicast || !a3)
1797 a3 = network_id;
1798 else if (srv->is_p2p)
1799 a3 = de->nmi;
1800
1801 nan_de_tx_sdf(de, srv, 100, NAN_SRV_CTRL_PUBLISH,
1802 srv->publish.solicited_multicast ?
1803 network_id : peer_addr, a3, instance_id, srv->ssi, NULL,
1804 NULL);
1805
1806 if (!srv->is_p2p && !srv->sync)
1807 nan_de_pause_state(srv, peer_addr, instance_id);
1808
1809 offload:
1810 if (!srv->publish.disable_events && de->cb.replied)
1811 de->cb.replied(de->cb.ctx, srv->id, peer_addr, instance_id,
1812 srv_proto_type, ssi, ssi_len);
1813
1814 return true;
1815 }
1816
1817
nan_de_rx_follow_up(struct nan_de * de,struct nan_de_service * srv,const u8 * peer_addr,const u8 * a3,u8 instance_id,const u8 * ssi,size_t ssi_len,const u8 * buf,size_t len)1818 static bool nan_de_rx_follow_up(struct nan_de *de, struct nan_de_service *srv,
1819 const u8 *peer_addr, const u8 *a3,
1820 u8 instance_id, const u8 *ssi, size_t ssi_len,
1821 const u8 *buf, size_t len)
1822 {
1823 /* Follow-up function processing of a receive Follow-up message for a
1824 * Subscribe or Publish instance */
1825
1826 if (srv->type == NAN_DE_PUBLISH &&
1827 os_reltime_initialized(&srv->pause_state_end) &&
1828 (!ether_addr_equal(peer_addr, srv->sel_peer_addr) ||
1829 instance_id != srv->sel_peer_id ||
1830 !ssi)) {
1831 wpa_printf(MSG_DEBUG,
1832 "NAN: In pauseState - ignore Follow-up message from another subscriber or without ssi");
1833 return false;
1834 }
1835
1836 if (srv->type == NAN_DE_PUBLISH && !ssi && !srv->sync)
1837 nan_de_pause_state(srv, peer_addr, instance_id);
1838
1839 os_memcpy(srv->a3, a3, ETH_ALEN);
1840 srv->a3_set = true;
1841
1842 if (de->cb.receive)
1843 de->cb.receive(de->cb.ctx, srv->id, instance_id, ssi, ssi_len,
1844 peer_addr, buf, len);
1845
1846 return true;
1847 }
1848
1849
nan_check_bloom_filter(const u8 * nmi,const u8 * bf,size_t bf_len,u8 bf_idx)1850 static bool nan_check_bloom_filter(const u8 *nmi, const u8 *bf,
1851 size_t bf_len, u8 bf_idx)
1852 {
1853 u8 a_j_x[1 + ETH_ALEN];
1854 int j;
1855 u32 crc;
1856
1857 for (j = 4 * bf_idx; j < 4 * (bf_idx + 1); j++) {
1858 a_j_x[0] = j;
1859 os_memcpy(&a_j_x[1], nmi, ETH_ALEN);
1860 crc = (~ieee80211_crc32(a_j_x, 1 + ETH_ALEN)) & 0xFFFF;
1861 crc %= bf_len * 8;
1862 if (!(bf[crc / 8] & BIT(crc % 8)))
1863 return false;
1864 }
1865
1866 return true;
1867 }
1868
1869
nan_srf_match(struct nan_de * de,const u8 * srf,size_t srf_len)1870 static bool nan_srf_match(struct nan_de *de, const u8 *srf, size_t srf_len)
1871 {
1872 u8 srf_ctrl;
1873 bool srf_type_bf;
1874 bool include;
1875 u8 srf_bf_idx;
1876
1877 if (srf_len < 1)
1878 return false;
1879
1880 srf_ctrl = *srf++;
1881 srf_len--;
1882
1883 srf_type_bf = !!(srf_ctrl & NAN_SRF_CTRL_BF);
1884 include = !!(srf_ctrl & NAN_SRF_CTRL_INCLUDE);
1885 srf_bf_idx = (srf_ctrl >> NAN_SRF_CTRL_BF_IDX_POS) &
1886 NAN_SRF_CTRL_BF_IDX_MSK;
1887
1888 if (srf_type_bf) {
1889 if (srf_len == 0)
1890 return false;
1891 if (nan_check_bloom_filter(de->nmi, srf, srf_len, srf_bf_idx))
1892 return include;
1893 } else {
1894 /* MAC Address filter */
1895 while (srf_len >= ETH_ALEN) {
1896 if (ether_addr_equal(srf, de->nmi))
1897 return include;
1898
1899 srf += ETH_ALEN;
1900 srf_len -= ETH_ALEN;
1901 }
1902 }
1903
1904 return !include;
1905 }
1906
1907
nan_de_rx_sda(struct nan_de * de,const u8 * peer_addr,const u8 * a3,unsigned int freq,const u8 * buf,size_t len,const u8 * sda,size_t sda_len,int rssi)1908 static bool nan_de_rx_sda(struct nan_de *de, const u8 *peer_addr, const u8 *a3,
1909 unsigned int freq, const u8 *buf, size_t len,
1910 const u8 *sda, size_t sda_len, int rssi)
1911 {
1912 const u8 *service_id;
1913 u8 instance_id, req_instance_id, ctrl;
1914 u16 sdea_control = 0;
1915 unsigned int i;
1916 enum nan_service_control_type type = 0;
1917 enum nan_service_protocol_type srv_proto_type = 0;
1918 const u8 *ssi = NULL;
1919 size_t ssi_len = 0;
1920 bool first = true;
1921 const u8 *end;
1922 const u8 *matching_filter = NULL;
1923 size_t matching_filter_len = 0;
1924 bool ret = false;
1925
1926 if (sda_len < NAN_SERVICE_ID_LEN + 1 + 1 + 1)
1927 return false;
1928 end = sda + sda_len;
1929
1930 service_id = sda;
1931 sda += NAN_SERVICE_ID_LEN;
1932 instance_id = *sda++;
1933 req_instance_id = *sda++;
1934 ctrl = *sda++;
1935 type = ctrl & NAN_SRV_CTRL_TYPE_MASK;
1936 wpa_printf(MSG_DEBUG,
1937 "NAN: SDA - Service ID %02x%02x%02x%02x%02x%02x Instance ID %u Requestor Instance ID %u Service Control 0x%x (Service Control Type %u)",
1938 MAC2STR(service_id), instance_id, req_instance_id,
1939 ctrl, type);
1940 if (type != NAN_SRV_CTRL_PUBLISH &&
1941 type != NAN_SRV_CTRL_SUBSCRIBE &&
1942 type != NAN_SRV_CTRL_FOLLOW_UP) {
1943 wpa_printf(MSG_DEBUG,
1944 "NAN: Discard SDF with unknown Service Control Type %u",
1945 type);
1946 return false;
1947 }
1948
1949 if (ctrl & NAN_SRV_CTRL_BINDING_BITMAP) {
1950 if (end - sda < 2)
1951 return false;
1952 sda += 2;
1953 }
1954
1955 if (ctrl & NAN_SRV_CTRL_MATCHING_FILTER) {
1956 u8 flen;
1957
1958 if (end - sda < 1)
1959 return false;
1960 flen = *sda++;
1961 if (end - sda < flen)
1962 return false;
1963 matching_filter = sda;
1964 matching_filter_len = flen;
1965 sda += flen;
1966 }
1967
1968 if (ctrl & NAN_SRV_CTRL_RESP_FILTER) {
1969 u8 flen;
1970
1971 if (end - sda < 1)
1972 return false;
1973 flen = *sda++;
1974 if (end - sda < flen)
1975 return false;
1976
1977 if (!nan_srf_match(de, sda, flen)) {
1978 wpa_printf(MSG_DEBUG,
1979 "NAN: Discard SDA with non-matching SRF");
1980 return false;
1981 }
1982
1983 sda += flen;
1984 }
1985
1986 if (ctrl & NAN_SRV_CTRL_SRV_INFO) {
1987 u8 flen;
1988
1989 if (end - sda < 1)
1990 return false;
1991 flen = *sda++;
1992 if (end - sda < flen)
1993 return false;
1994
1995 if (flen) {
1996 /* This case of SSI in SDA does not have an explicit
1997 * indication of a service protocol type unlike the
1998 * SDEA case. For now, leave srv_proto_type to 0 for
1999 * this SDA case since that is a reserved value for the
2000 * SDEA cases. */
2001 ssi = sda;
2002 ssi_len = flen;
2003 wpa_hexdump(MSG_MSGDUMP, "NAN: ssi", ssi, ssi_len);
2004 }
2005 sda += flen;
2006 }
2007
2008 for (i = 0; i < NAN_DE_MAX_SERVICE; i++) {
2009 struct nan_de_service *srv = de->service[i];
2010
2011 if (!srv)
2012 continue;
2013 if (os_memcmp(srv->service_id, service_id,
2014 NAN_SERVICE_ID_LEN) != 0)
2015 continue;
2016 if (type == NAN_SRV_CTRL_PUBLISH) {
2017 if (srv->type == NAN_DE_PUBLISH)
2018 continue;
2019 if (req_instance_id && srv->id != req_instance_id)
2020 continue;
2021 }
2022 if (type == NAN_SRV_CTRL_SUBSCRIBE &&
2023 srv->type == NAN_DE_SUBSCRIBE)
2024 continue;
2025 wpa_printf(MSG_DEBUG, "NAN: Received SDF matches service ID %u",
2026 i + 1);
2027
2028 if (first) {
2029 first = false;
2030 nan_de_get_sdea(buf, len, instance_id, &sdea_control,
2031 &srv_proto_type, &ssi, &ssi_len);
2032
2033 if (ssi) {
2034 wpa_printf(MSG_DEBUG,
2035 "NAN: Service Protocol Type %d",
2036 srv_proto_type);
2037 wpa_hexdump(MSG_MSGDUMP, "NAN: ssi",
2038 ssi, ssi_len);
2039 }
2040 nan_de_process_elem_container(de, buf, len, peer_addr,
2041 freq, srv->is_p2p,
2042 srv->is_pr);
2043 }
2044
2045 switch (type) {
2046 case NAN_SRV_CTRL_PUBLISH:
2047 ret |= nan_de_rx_publish(
2048 de, srv, peer_addr, a3, instance_id,
2049 matching_filter, matching_filter_len,
2050 req_instance_id, sdea_control, srv_proto_type,
2051 ssi, ssi_len,
2052 ctrl & NAN_SRV_CTRL_DISCOVERY_RANGE_LIMITED,
2053 rssi, buf, len);
2054 break;
2055 case NAN_SRV_CTRL_SUBSCRIBE:
2056 ret |= nan_de_rx_subscribe(
2057 de, srv, peer_addr, a3, instance_id,
2058 matching_filter, matching_filter_len,
2059 srv_proto_type, ssi, ssi_len,
2060 ctrl & NAN_SRV_CTRL_DISCOVERY_RANGE_LIMITED,
2061 rssi);
2062 break;
2063 case NAN_SRV_CTRL_FOLLOW_UP:
2064 ret |= nan_de_rx_follow_up(de, srv, peer_addr, a3,
2065 instance_id, ssi, ssi_len,
2066 buf, len);
2067 break;
2068 }
2069 }
2070
2071 return ret;
2072 }
2073
2074
nan_de_rx_sdf(struct nan_de * de,const u8 * peer_addr,const u8 * a3,unsigned int freq,const u8 * buf,size_t len,int rssi)2075 bool nan_de_rx_sdf(struct nan_de *de, const u8 *peer_addr, const u8 *a3,
2076 unsigned int freq, const u8 *buf, size_t len, int rssi)
2077 {
2078 const u8 *sda;
2079 u16 sda_len;
2080 unsigned int skip;
2081 bool ret = false;
2082
2083 if (!de->num_service)
2084 return false;
2085
2086 wpa_printf(MSG_DEBUG, "NAN: RX SDF from " MACSTR
2087 " freq=%u len=%zu rssi=%d",
2088 MAC2STR(peer_addr), freq, len, rssi);
2089
2090 wpa_hexdump(MSG_MSGDUMP, "NAN: SDF payload", buf, len);
2091
2092 for (skip = 0; ; skip++) {
2093 sda = nan_de_get_attr(buf, len, NAN_ATTR_SDA, skip);
2094 if (!sda)
2095 break;
2096
2097 sda++;
2098 sda_len = WPA_GET_LE16(sda);
2099 sda += 2;
2100 ret |= nan_de_rx_sda(de, peer_addr, a3, freq, buf, len,
2101 sda, sda_len, rssi);
2102 }
2103
2104 return ret;
2105 }
2106
2107
nan_de_get_handle(struct nan_de * de)2108 static int nan_de_get_handle(struct nan_de *de)
2109 {
2110 int i = de->next_handle;
2111
2112 if (de->num_service >= NAN_DE_MAX_SERVICE)
2113 goto fail;
2114
2115 do {
2116 if (!de->service[i]) {
2117 de->next_handle = (i + 1) % NAN_DE_MAX_SERVICE;
2118 return i + 1;
2119 }
2120 i = (i + 1) % NAN_DE_MAX_SERVICE;
2121 } while (i != de->next_handle);
2122
2123 fail:
2124 wpa_printf(MSG_DEBUG, "NAN: No more room for a new service");
2125 return -1;
2126 }
2127
2128
nan_de_derive_service_id(struct nan_de_service * srv)2129 static int nan_de_derive_service_id(struct nan_de_service *srv)
2130 {
2131 u8 hash[SHA256_MAC_LEN];
2132 char *name, *pos;
2133 int ret;
2134 const u8 *addr[1];
2135 size_t len[1];
2136
2137 name = os_strdup(srv->service_name);
2138 if (!name)
2139 return -1;
2140 pos = name;
2141 while (*pos) {
2142 *pos = tolower(*pos);
2143 pos++;
2144 }
2145
2146 addr[0] = (u8 *) name;
2147 len[0] = os_strlen(name);
2148 ret = sha256_vector(1, addr, len, hash);
2149 os_free(name);
2150 if (ret == 0)
2151 os_memcpy(srv->service_id, hash, NAN_SERVICE_ID_LEN);
2152
2153 return ret;
2154 }
2155
2156
nan_de_get_service_id(struct nan_de * de,int id)2157 const u8 * nan_de_get_service_id(struct nan_de *de, int id)
2158 {
2159 struct nan_de_service *srv;
2160
2161 if (id < 1 || id > NAN_DE_MAX_SERVICE)
2162 return NULL;
2163 srv = de->service[id - 1];
2164 if (!srv)
2165 return NULL;
2166 return srv->service_id;
2167 }
2168
2169
nan_de_publish(struct nan_de * de,const char * service_name,enum nan_service_protocol_type srv_proto_type,const struct wpabuf * ssi,const struct wpabuf * elems,struct nan_publish_params * params,bool p2p,const u8 * addr)2170 int nan_de_publish(struct nan_de *de, const char *service_name,
2171 enum nan_service_protocol_type srv_proto_type,
2172 const struct wpabuf *ssi, const struct wpabuf *elems,
2173 struct nan_publish_params *params, bool p2p,
2174 const u8 *addr)
2175 {
2176 int publish_id;
2177 struct nan_de_service *srv;
2178
2179 if (!service_name && !params->proximity_ranging) {
2180 wpa_printf(MSG_DEBUG, "NAN: Publish() - no service_name");
2181 return -1;
2182 }
2183
2184 if (!params->unsolicited && !params->solicited) {
2185 wpa_printf(MSG_INFO,
2186 "NAN: Publish() - both unsolicited and solicited disabled is invalid");
2187 return -1;
2188 }
2189
2190 if (params->proximity_ranging && params->solicited && !elems) {
2191 wpa_printf(MSG_INFO,
2192 "NAN: Unable to fetch proximity ranging params");
2193 return -1;
2194 }
2195
2196 if (params->sync && !de->cluster_id_set) {
2197 wpa_printf(MSG_DEBUG,
2198 "NAN: Publish() - can't publish sync, cluster id is not set");
2199 return -1;
2200 }
2201
2202 if (p2p && params->sync) {
2203 wpa_printf(MSG_DEBUG,
2204 "NAN: Publish() - P2P is not supported with sync");
2205 return -1;
2206 }
2207
2208 publish_id = nan_de_get_handle(de);
2209 if (publish_id < 1)
2210 return -1;
2211
2212 srv = os_zalloc(sizeof(*srv));
2213 if (!srv)
2214 return -1;
2215 srv->type = NAN_DE_PUBLISH;
2216 srv->freq = srv->default_freq = params->freq;
2217
2218 if (service_name) {
2219 srv->service_name = os_strdup(service_name);
2220 if (!srv->service_name)
2221 goto fail;
2222 }
2223
2224 if (params->proximity_ranging && !service_name)
2225 os_memset(srv->service_id, 0, NAN_SERVICE_ID_LEN);
2226 else if (nan_de_derive_service_id(srv) < 0)
2227 goto fail;
2228
2229 os_memcpy(&srv->publish, params, sizeof(*params));
2230
2231 if (params->freq_list) {
2232 size_t len;
2233
2234 len = (int_array_len(params->freq_list) + 1) * sizeof(int);
2235 srv->freq_list = os_memdup(params->freq_list, len);
2236 if (!srv->freq_list)
2237 goto fail;
2238 }
2239 srv->publish.freq_list = NULL;
2240
2241 srv->srv_proto_type = srv_proto_type;
2242 if (ssi) {
2243 srv->ssi = wpabuf_dup(ssi);
2244 if (!srv->ssi)
2245 goto fail;
2246 }
2247 if (elems) {
2248 srv->elems = wpabuf_dup(elems);
2249 if (!srv->elems)
2250 goto fail;
2251 }
2252
2253 if (params->match_filter_rx) {
2254 srv->matching_filter_rx =
2255 wpabuf_parse_bin(params->match_filter_rx);
2256 if (!srv->matching_filter_rx ||
2257 wpabuf_len(srv->matching_filter_rx) > 255) {
2258 wpa_printf(MSG_INFO,
2259 "NAN: Failed to parse RX matching filter");
2260 goto fail;
2261 }
2262 }
2263
2264 if (params->match_filter_tx) {
2265 srv->matching_filter_tx =
2266 wpabuf_parse_bin(params->match_filter_tx);
2267 if (!srv->matching_filter_tx ||
2268 wpabuf_len(srv->matching_filter_tx) > 255) {
2269 wpa_printf(MSG_INFO,
2270 "NAN: Failed to parse TX matching filter");
2271 goto fail;
2272 }
2273 }
2274
2275 srv->sync = params->sync;
2276
2277 if (addr && params->forced_addr) {
2278 os_memcpy(srv->forced_addr, addr, ETH_ALEN);
2279 srv->forced_addr_set = true;
2280 wpa_printf(MSG_DEBUG, "NAN: Using source address " MACSTR
2281 " for publish service", MAC2STR(srv->forced_addr));
2282 }
2283
2284 srv->security_capab = params->security_capab;
2285
2286 if (params->cipher_suites_list) {
2287 int i = 0;
2288
2289 while (params->cipher_suites_list[i] && i < NAN_CS_MAX) {
2290 if (params->cipher_suites_list[i] >= NAN_CS_MAX) {
2291 wpa_printf(MSG_DEBUG,
2292 "NAN: Invalid cipher suite %d in publish",
2293 params->cipher_suites_list[i]);
2294 goto fail;
2295 }
2296
2297 i++;
2298 }
2299
2300 srv->cipher_suites_list =
2301 int_array_dup(params->cipher_suites_list);
2302 if (!srv->cipher_suites_list)
2303 goto fail;
2304 }
2305
2306 dl_list_init(&srv->pmkid_list);
2307 #ifdef CONFIG_NAN
2308 if (nan_crypto_pmkid_list(&srv->pmkid_list, de->nmi, srv->service_id,
2309 srv->cipher_suites_list, params->nd_pmk) < 0)
2310 goto fail;
2311 #endif /* CONFIG_NAN */
2312
2313 /* Prepare for single and multi-channel states; starting with
2314 * single channel */
2315 srv->first_multi_chan = true;
2316 nan_de_start_new_publish_state(de, srv, true);
2317
2318 wpa_printf(MSG_DEBUG, "NAN: Assigned new publish handle %d for %s",
2319 publish_id, service_name ? service_name : "Ranging");
2320 srv->id = publish_id;
2321 srv->is_p2p = p2p;
2322 srv->is_pr = params->proximity_ranging && params->solicited;
2323 srv->close_proximity = params->close_proximity;
2324 srv->pbm = params->pbm;
2325 srv->gtk_required = params->gtk_required;
2326 srv->data_path = params->data_path;
2327 srv->security_required = params->security_required;
2328
2329 nan_de_add_srv(de, srv);
2330 nan_de_run_timer(de);
2331 return publish_id;
2332 fail:
2333 nan_de_service_free(srv);
2334 return -1;
2335 }
2336
2337
nan_de_cancel_publish(struct nan_de * de,int publish_id)2338 void nan_de_cancel_publish(struct nan_de *de, int publish_id)
2339 {
2340 struct nan_de_service *srv;
2341
2342 wpa_printf(MSG_DEBUG, "NAN: CancelPublish(publish_id=%d)", publish_id);
2343
2344 if (publish_id < 1 || publish_id > NAN_DE_MAX_SERVICE)
2345 return;
2346 srv = de->service[publish_id - 1];
2347 if (!srv || srv->type != NAN_DE_PUBLISH)
2348 return;
2349 nan_de_del_srv(de, srv, NAN_DE_REASON_USER_REQUEST);
2350 }
2351
2352
nan_de_update_publish(struct nan_de * de,int publish_id,const struct wpabuf * ssi)2353 int nan_de_update_publish(struct nan_de *de, int publish_id,
2354 const struct wpabuf *ssi)
2355 {
2356 struct nan_de_service *srv;
2357
2358 wpa_printf(MSG_DEBUG, "NAN: UpdatePublish(publish_id=%d)", publish_id);
2359
2360 if (publish_id < 1 || publish_id > NAN_DE_MAX_SERVICE)
2361 return -1;
2362 srv = de->service[publish_id - 1];
2363 if (!srv || srv->type != NAN_DE_PUBLISH)
2364 return -1;
2365
2366 wpabuf_free(srv->ssi);
2367 srv->ssi = NULL;
2368 if (!ssi)
2369 return 0;
2370 srv->ssi = wpabuf_dup(ssi);
2371 if (!srv->ssi)
2372 return -1;
2373 return 0;
2374 }
2375
2376
nan_de_unpause_publish(struct nan_de * de,int publish_id,u8 peer_instance_id,const u8 * peer_addr)2377 int nan_de_unpause_publish(struct nan_de *de, int publish_id,
2378 u8 peer_instance_id, const u8 *peer_addr)
2379 {
2380 struct nan_de_service *srv;
2381
2382 wpa_printf(MSG_DEBUG,
2383 "NAN: UnpausePublish(publish_id=%d, peer_instance_id=%d peer_addr="
2384 MACSTR ")",
2385 publish_id, peer_instance_id, MAC2STR(peer_addr));
2386
2387 if (publish_id < 1 || publish_id > NAN_DE_MAX_SERVICE)
2388 return -1;
2389 srv = de->service[publish_id - 1];
2390 if (!srv || srv->type != NAN_DE_PUBLISH)
2391 return -1;
2392
2393 if (srv->sel_peer_id != peer_instance_id ||
2394 !ether_addr_equal(peer_addr, srv->sel_peer_addr) ||
2395 !os_reltime_initialized(&srv->pause_state_end))
2396 return -1;
2397
2398 nan_de_unpause_state(srv);
2399 return 0;
2400 }
2401
2402
bloom_filter_add(u8 * bf,u8 bf_idx,u8 bf_len,const u8 * mac)2403 static void bloom_filter_add(u8 *bf, u8 bf_idx, u8 bf_len, const u8 *mac)
2404 {
2405 u8 a_j_x[1 + ETH_ALEN];
2406 int j;
2407 u32 crc;
2408
2409 for (j = 4 * bf_idx; j < 4 * (bf_idx + 1); j++) {
2410 a_j_x[0] = j;
2411 os_memcpy(&a_j_x[1], mac, ETH_ALEN);
2412 crc = (~ieee80211_crc32(a_j_x, 1 + ETH_ALEN)) & 0xFFFF;
2413 crc %= bf_len * 8;
2414 bf[crc / 8] |= 1 << (crc % 8);
2415 }
2416 }
2417
2418
nan_build_bloom_filter(const char * srf_mac_list,u8 srf_bf_len,u8 srf_bf_idx)2419 static struct wpabuf * nan_build_bloom_filter(const char *srf_mac_list,
2420 u8 srf_bf_len, u8 srf_bf_idx)
2421 {
2422 struct wpabuf *srf;
2423 int i, n;
2424 u8 mac[ETH_ALEN];
2425 u8 *bf;
2426
2427 if (srf_bf_idx > 3)
2428 return NULL;
2429
2430 if (os_strlen(srf_mac_list) % (ETH_ALEN * 2)) {
2431 wpa_printf(MSG_INFO,
2432 "NAN: Invalid SRF MAC list length %zu",
2433 os_strlen(srf_mac_list));
2434 return NULL;
2435 }
2436
2437 n = os_strlen(srf_mac_list) / (ETH_ALEN * 2);
2438
2439 srf = wpabuf_alloc(srf_bf_len);
2440 if (!srf)
2441 return NULL;
2442
2443 bf = wpabuf_put(srf, srf_bf_len);
2444
2445 for (i = 0; i < n; i++) {
2446 if (hexstr2bin(srf_mac_list + i * 2 * ETH_ALEN, mac, ETH_ALEN))
2447 {
2448 wpa_printf(MSG_INFO,
2449 "NAN: Invalid SRF MAC address %s",
2450 srf_mac_list + i * 2 * ETH_ALEN);
2451 goto out;
2452 }
2453
2454 bloom_filter_add(bf, srf_bf_idx, srf_bf_len, mac);
2455 }
2456
2457 return srf;
2458 out:
2459 wpabuf_free(srf);
2460 return NULL;
2461 }
2462
2463
nan_de_subscribe(struct nan_de * de,const char * service_name,enum nan_service_protocol_type srv_proto_type,const struct wpabuf * ssi,const struct wpabuf * elems,struct nan_subscribe_params * params,bool p2p,const u8 * addr)2464 int nan_de_subscribe(struct nan_de *de, const char *service_name,
2465 enum nan_service_protocol_type srv_proto_type,
2466 const struct wpabuf *ssi, const struct wpabuf *elems,
2467 struct nan_subscribe_params *params, bool p2p,
2468 const u8 *addr)
2469 {
2470 int subscribe_id;
2471 struct nan_de_service *srv;
2472
2473 if (!service_name && !params->proximity_ranging) {
2474 wpa_printf(MSG_DEBUG, "NAN: Subscribe() - no service_name");
2475 return -1;
2476 }
2477
2478 if (params->proximity_ranging && params->active && !elems) {
2479 wpa_printf(MSG_INFO,
2480 "NAN: Unable to fetch proximity ranging params");
2481 return -1;
2482 }
2483
2484 if (params->sync && !de->cluster_id_set) {
2485 wpa_printf(MSG_DEBUG,
2486 "NAN: Subscribe() - can't publish sync, cluster id is not set");
2487 return -1;
2488 }
2489
2490 if (p2p && params->sync) {
2491 wpa_printf(MSG_DEBUG,
2492 "NAN: Subscribe() - P2P is not supported with sync");
2493 return -1;
2494 }
2495
2496 subscribe_id = nan_de_get_handle(de);
2497 if (subscribe_id < 1)
2498 return -1;
2499
2500 srv = os_zalloc(sizeof(*srv));
2501 if (!srv)
2502 return -1;
2503 srv->type = NAN_DE_SUBSCRIBE;
2504 srv->freq = params->freq;
2505
2506 if (service_name) {
2507 srv->service_name = os_strdup(service_name);
2508 if (!srv->service_name)
2509 goto fail;
2510 }
2511
2512 if (params->proximity_ranging && !service_name)
2513 os_memset(srv->service_id, 0, NAN_SERVICE_ID_LEN);
2514 else if (nan_de_derive_service_id(srv) < 0)
2515 goto fail;
2516
2517 os_memcpy(&srv->subscribe, params, sizeof(*params));
2518
2519 if (params->freq_list) {
2520 size_t len;
2521
2522 len = (int_array_len(params->freq_list) + 1) * sizeof(int);
2523 srv->freq_list = os_memdup(params->freq_list, len);
2524 if (!srv->freq_list)
2525 goto fail;
2526 }
2527 srv->subscribe.freq_list = NULL;
2528
2529 srv->srv_proto_type = srv_proto_type;
2530 if (ssi) {
2531 srv->ssi = wpabuf_dup(ssi);
2532 if (!srv->ssi)
2533 goto fail;
2534 }
2535 if (elems) {
2536 srv->elems = wpabuf_dup(elems);
2537 if (!srv->elems)
2538 goto fail;
2539 }
2540
2541 if (params->match_filter_rx) {
2542 srv->matching_filter_rx =
2543 wpabuf_parse_bin(params->match_filter_rx);
2544 if (!srv->matching_filter_rx ||
2545 wpabuf_len(srv->matching_filter_rx) > 255) {
2546 wpa_printf(MSG_DEBUG,
2547 "NAN: Failed to parse RX matching filter");
2548 goto fail;
2549 }
2550 }
2551
2552 if (params->match_filter_tx) {
2553 srv->matching_filter_tx =
2554 wpabuf_parse_bin(params->match_filter_tx);
2555 if (!srv->matching_filter_tx ||
2556 wpabuf_len(srv->matching_filter_tx) > 255) {
2557 wpa_printf(MSG_DEBUG,
2558 "NAN: Failed to parse TX matching filter");
2559 goto fail;
2560 }
2561 }
2562
2563 if (params->srf_mac_list) {
2564 if (params->srf_bf_len) {
2565 srv->srf = nan_build_bloom_filter(params->srf_mac_list,
2566 params->srf_bf_len,
2567 params->srf_bf_idx);
2568 srv->srf_type_bloom_filter = true;
2569 srv->srf_bf_idx = params->srf_bf_idx;
2570 } else {
2571 srv->srf = wpabuf_parse_bin(params->srf_mac_list);
2572 if (wpabuf_len(srv->srf) % ETH_ALEN) {
2573 wpa_printf(MSG_INFO,
2574 "NAN: Invalid SRF MAC list length");
2575 goto fail;
2576 }
2577 }
2578
2579 if (!srv->srf || wpabuf_len(srv->srf) > 254) {
2580 wpa_printf(MSG_DEBUG,
2581 "NAN: Failed to parse SRF MAC list");
2582 goto fail;
2583 }
2584
2585 srv->srf_include = params->srf_include;
2586 }
2587
2588 if (addr && params->forced_addr) {
2589 os_memcpy(srv->forced_addr, addr, ETH_ALEN);
2590 srv->forced_addr_set = true;
2591 wpa_printf(MSG_DEBUG, "NAN: Using source address " MACSTR
2592 " for subscribe service", MAC2STR(srv->forced_addr));
2593 }
2594
2595 dl_list_init(&srv->pmkid_list);
2596
2597 wpa_printf(MSG_DEBUG, "NAN: Assigned new subscribe handle %d for %s",
2598 subscribe_id, service_name ? service_name : "Ranging");
2599 srv->id = subscribe_id;
2600 srv->is_p2p = p2p;
2601 srv->is_pr = params->proximity_ranging && params->active;
2602 srv->sync = params->sync;
2603 srv->close_proximity = params->close_proximity;
2604 srv->pbm = params->pbm;
2605 srv->gtk_required = params->gtk_required;
2606
2607 nan_de_add_srv(de, srv);
2608 nan_de_run_timer(de);
2609 return subscribe_id;
2610 fail:
2611 nan_de_service_free(srv);
2612 return -1;
2613 }
2614
2615
nan_de_cancel_subscribe(struct nan_de * de,int subscribe_id)2616 void nan_de_cancel_subscribe(struct nan_de *de, int subscribe_id)
2617 {
2618 struct nan_de_service *srv;
2619
2620 if (subscribe_id < 1 || subscribe_id > NAN_DE_MAX_SERVICE)
2621 return;
2622 srv = de->service[subscribe_id - 1];
2623 if (!srv || srv->type != NAN_DE_SUBSCRIBE)
2624 return;
2625 nan_de_del_srv(de, srv, NAN_DE_REASON_USER_REQUEST);
2626 }
2627
2628
nan_de_transmit(struct nan_de * de,int handle,const struct wpabuf * ssi,const struct wpabuf * elems,const u8 * peer_addr,u8 req_instance_id,const struct wpabuf * nan_attrs,u32 * cookie)2629 int nan_de_transmit(struct nan_de *de, int handle,
2630 const struct wpabuf *ssi, const struct wpabuf *elems,
2631 const u8 *peer_addr, u8 req_instance_id,
2632 const struct wpabuf *nan_attrs, u32 *cookie)
2633 {
2634 struct nan_de_service *srv;
2635 const u8 *a3;
2636 const u8 *network_id;
2637
2638 if (handle < 1 || handle > NAN_DE_MAX_SERVICE)
2639 return -1;
2640
2641 srv = de->service[handle - 1];
2642 if (!srv)
2643 return -1;
2644
2645 if (srv->sync && !de->cluster_id_set) {
2646 wpa_printf(MSG_DEBUG,
2647 "NAN: Cannot transmit Follow-up, cluster ID not set");
2648 return -1;
2649 }
2650
2651 if (srv->is_p2p)
2652 network_id = p2p_network_id;
2653 else if (srv->sync)
2654 network_id = de->cluster_id;
2655 else
2656 network_id = nan_network_id;
2657
2658 if (srv->a3_set)
2659 a3 = srv->a3;
2660 else
2661 a3 = network_id;
2662 nan_de_tx_sdf(de, srv, 100, NAN_SRV_CTRL_FOLLOW_UP,
2663 peer_addr, a3, req_instance_id, ssi, nan_attrs,
2664 cookie);
2665
2666 srv->listen_stopped = false;
2667 return 0;
2668 }
2669
2670
nan_de_stop_listen(struct nan_de * de,int handle)2671 int nan_de_stop_listen(struct nan_de *de, int handle)
2672 {
2673 struct nan_de_service *srv;
2674
2675 if (handle < 1 || handle > NAN_DE_MAX_SERVICE)
2676 return -1;
2677
2678 srv = de->service[handle - 1];
2679 if (!srv)
2680 return -1;
2681 srv->listen_stopped = true;
2682 return 0;
2683 }
2684
2685
nan_de_config(struct nan_de * de,struct nan_de_cfg * cfg)2686 int nan_de_config(struct nan_de *de, struct nan_de_cfg *cfg)
2687 {
2688 if (!de || !cfg)
2689 return -1;
2690
2691 /* No change in configuration */
2692 if (de->cfg.n_min == cfg->n_min && de->cfg.n_max == cfg->n_max &&
2693 de->cfg.cycle == cfg->cycle && de->cfg.suspend == cfg->suspend)
2694 return 0;
2695
2696 wpa_printf(MSG_DEBUG,
2697 "NAN: Configuring NAN DE: n=(%u, %u), suspend=%u, cycle=%u",
2698 cfg->n_min, cfg->n_max, cfg->suspend, cfg->cycle);
2699
2700 if (!cfg->n_min && !cfg->n_max) {
2701 cfg->n_min = NAN_DE_N_MIN;
2702 cfg->n_max = NAN_DE_N_MAX;
2703 } else if (cfg->n_min < 1 || cfg->n_max < cfg->n_min) {
2704 wpa_printf(MSG_DEBUG,
2705 "NAN: Invalid configuration parameters: N");
2706 return -1;
2707 }
2708
2709 if (((!!cfg->suspend) ^ (!!cfg->cycle)) ||
2710 (cfg->cycle && cfg->suspend >= cfg->cycle)) {
2711 wpa_printf(MSG_DEBUG,
2712 "NAN: Invalid configuration parameters: cycle");
2713 return -1;
2714 }
2715
2716 de->cfg = *cfg;
2717
2718 os_memset(&de->suspend_cycle_start, 0, sizeof(de->suspend_cycle_start));
2719
2720 if (!de->listen_freq)
2721 nan_de_run_timer(de);
2722
2723 return 0;
2724 }
2725
2726
nan_de_dw_trigger(struct nan_de * de,int freq)2727 void nan_de_dw_trigger(struct nan_de *de, int freq)
2728 {
2729 int i;
2730 struct os_reltime now;
2731
2732 de->dw_freq = freq;
2733
2734 if (!de->cluster_id_set) {
2735 wpa_printf(MSG_DEBUG, "NAN: Skip DW, cluster ID not set");
2736 return;
2737 }
2738
2739 os_get_reltime(&now);
2740 for (i = 0; i < NAN_DE_MAX_SERVICE; i++) {
2741 struct nan_de_service *srv = de->service[i];
2742
2743 if (!srv || !srv->sync)
2744 continue;
2745
2746 if (nan_de_srv_expired(srv, &now)) {
2747 nan_de_del_srv(de, srv, NAN_DE_REASON_TIMEOUT);
2748 continue;
2749 }
2750
2751 if ((srv->type == NAN_DE_PUBLISH &&
2752 srv->publish.unsolicited) ||
2753 (srv->type == NAN_DE_SUBSCRIBE && srv->subscribe.active)) {
2754 nan_de_tx_multicast(de, srv, 0);
2755 }
2756 }
2757 }
2758
2759
nan_de_set_cluster_id(struct nan_de * de,const u8 * cluster_id)2760 void nan_de_set_cluster_id(struct nan_de *de, const u8 *cluster_id)
2761 {
2762 if (cluster_id) {
2763 os_memcpy(de->cluster_id, cluster_id, ETH_ALEN);
2764 de->cluster_id_set = true;
2765 } else {
2766 de->cluster_id_set = false;
2767 }
2768 }
2769
2770
nan_de_is_valid_instance_id(struct nan_de * de,int handle,bool publish,u8 * service_id)2771 bool nan_de_is_valid_instance_id(struct nan_de *de, int handle,
2772 bool publish, u8 *service_id)
2773 {
2774 struct nan_de_service *srv;
2775
2776 if (handle < 1 || handle > NAN_DE_MAX_SERVICE)
2777 return false;
2778
2779 srv = de->service[handle - 1];
2780 if (!srv)
2781 return false;
2782
2783 if (publish && srv->type != NAN_DE_PUBLISH)
2784 return false;
2785 if (!publish && srv->type != NAN_DE_SUBSCRIBE)
2786 return false;
2787
2788 os_memcpy(service_id, srv->service_id, NAN_SERVICE_ID_LEN);
2789 return true;
2790 }
2791
2792
nan_de_get_service_bootstrap_methods(struct nan_de * de,int handle)2793 u16 nan_de_get_service_bootstrap_methods(struct nan_de *de, int handle)
2794 {
2795 struct nan_de_service *srv;
2796
2797 if (handle < 1 || handle > NAN_DE_MAX_SERVICE)
2798 return 0;
2799
2800 srv = de->service[handle - 1];
2801 if (!srv)
2802 return 0;
2803
2804 return srv->pbm;
2805 }
2806
2807
nan_de_service_supports_csid(struct nan_de * de,int handle,int csid)2808 bool nan_de_service_supports_csid(struct nan_de *de, int handle, int csid)
2809 {
2810 struct nan_de_service *srv;
2811
2812 if (handle < 1 || handle > NAN_DE_MAX_SERVICE)
2813 return false;
2814
2815 srv = de->service[handle - 1];
2816 if (!srv)
2817 return false;
2818
2819 /* If cipher_suites_list is not set, all CSIDs are allowed */
2820 if (!srv->cipher_suites_list)
2821 return true;
2822
2823 /* Open is allowed only if security is not required */
2824 if (csid == NAN_CS_NONE)
2825 return !srv->security_required;
2826
2827 /* Check if the CSID is in the service's cipher suite list */
2828 return int_array_includes(srv->cipher_suites_list, csid);
2829 }
2830
2831
nan_de_service_type2str(enum nan_de_service_type type)2832 static const char * nan_de_service_type2str(enum nan_de_service_type type)
2833 {
2834 switch (type) {
2835 case NAN_DE_PUBLISH:
2836 return "publish";
2837 case NAN_DE_SUBSCRIBE:
2838 return "subscribe";
2839 }
2840
2841 return "unknown";
2842 }
2843
2844
nan_de_get_status(struct nan_de * de,char * buf,size_t buflen)2845 int nan_de_get_status(struct nan_de *de, char *buf, size_t buflen)
2846 {
2847 char *pos, *end;
2848 unsigned int i;
2849 int ret;
2850
2851 if (!de)
2852 return -1;
2853
2854 pos = buf;
2855 end = buf + buflen;
2856
2857 ret = os_snprintf(pos, end - pos, "num_services=%u\n",
2858 de->num_service);
2859 if (os_snprintf_error(end - pos, ret))
2860 return pos - buf;
2861 pos += ret;
2862
2863 for (i = 0; i < NAN_DE_MAX_SERVICE; i++) {
2864 struct nan_de_service *srv = de->service[i];
2865
2866 if (!srv)
2867 continue;
2868
2869 ret = os_snprintf(pos, end - pos,
2870 "service=%u type=%s name=%s sync=%d\n",
2871 srv->id,
2872 nan_de_service_type2str(srv->type),
2873 srv->service_name ? srv->service_name : "",
2874 srv->sync);
2875 if (os_snprintf_error(end - pos, ret))
2876 return pos - buf;
2877 pos += ret;
2878 }
2879
2880 return pos - buf;
2881 }
2882
2883
2884 #ifdef CONFIG_TESTING_OPTIONS
2885
nan_de_set_tx_mcast_follow_up_prot(struct nan_de * de,bool prot)2886 void nan_de_set_tx_mcast_follow_up_prot(struct nan_de *de, bool prot)
2887 {
2888 wpa_printf(MSG_DEBUG,
2889 "NAN: Set tx_mcast_follow_up_dual_prot: %u->%u",
2890 de->tx_mcast_follow_up_prot, prot);
2891
2892 de->tx_mcast_follow_up_prot = prot;
2893 }
2894
2895 #endif /* CONFIG_TESTING_OPTIONS */
2896