blob: 4caa55ce5871d57d274ce45d0b7d141006f7e49e [file] [log] [blame]
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001/*
2 * WPA Supplicant - Basic mesh peer management
3 * Copyright (c) 2013-2014, cozybit, Inc. All rights reserved.
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 "common/ieee802_11_defs.h"
14#include "ap/hostapd.h"
15#include "ap/sta_info.h"
16#include "ap/ieee802_11.h"
17#include "wpa_supplicant_i.h"
18#include "driver_i.h"
19#include "mesh_mpm.h"
20#include "mesh_rsn.h"
21
22struct mesh_peer_mgmt_ie {
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -080023 const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
24 const u8 *llid; /* Local Link ID (2 octets) */
25 const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
26 const u8 *reason; /* Reason Code (conditional, 2 octets) */
27 const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080028};
29
30static void plink_timer(void *eloop_ctx, void *user_data);
31
32
33enum plink_event {
34 PLINK_UNDEFINED,
35 OPN_ACPT,
36 OPN_RJCT,
37 OPN_IGNR,
38 CNF_ACPT,
39 CNF_RJCT,
40 CNF_IGNR,
41 CLS_ACPT,
42 CLS_IGNR
43};
44
45static const char * const mplstate[] = {
46 [PLINK_LISTEN] = "LISTEN",
47 [PLINK_OPEN_SENT] = "OPEN_SENT",
48 [PLINK_OPEN_RCVD] = "OPEN_RCVD",
49 [PLINK_CNF_RCVD] = "CNF_RCVD",
50 [PLINK_ESTAB] = "ESTAB",
51 [PLINK_HOLDING] = "HOLDING",
52 [PLINK_BLOCKED] = "BLOCKED"
53};
54
55static const char * const mplevent[] = {
56 [PLINK_UNDEFINED] = "UNDEFINED",
57 [OPN_ACPT] = "OPN_ACPT",
58 [OPN_RJCT] = "OPN_RJCT",
59 [OPN_IGNR] = "OPN_IGNR",
60 [CNF_ACPT] = "CNF_ACPT",
61 [CNF_RJCT] = "CNF_RJCT",
62 [CNF_IGNR] = "CNF_IGNR",
63 [CLS_ACPT] = "CLS_ACPT",
64 [CLS_IGNR] = "CLS_IGNR"
65};
66
67
68static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
69 u8 action_field,
70 const u8 *ie, size_t len,
71 struct mesh_peer_mgmt_ie *mpm_ie)
72{
73 os_memset(mpm_ie, 0, sizeof(*mpm_ie));
74
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -080075 /* Remove optional Chosen PMK field at end */
76 if (len >= SAE_PMKID_LEN) {
77 mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
78 len -= SAE_PMKID_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080079 }
80
81 if ((action_field == PLINK_OPEN && len != 4) ||
82 (action_field == PLINK_CONFIRM && len != 6) ||
83 (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
84 wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
85 return -1;
86 }
87
88 /* required fields */
89 if (len < 4)
90 return -1;
91 mpm_ie->proto_id = ie;
92 mpm_ie->llid = ie + 2;
93 ie += 4;
94 len -= 4;
95
96 /* close reason is always present at end for close */
97 if (action_field == PLINK_CLOSE) {
98 if (len < 2)
99 return -1;
100 mpm_ie->reason = ie + len - 2;
101 len -= 2;
102 }
103
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800104 /* Peer Link ID, present for confirm, and possibly close */
105 if (len >= 2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800106 mpm_ie->plid = ie;
107
108 return 0;
109}
110
111
112static int plink_free_count(struct hostapd_data *hapd)
113{
114 if (hapd->max_plinks > hapd->num_plinks)
115 return hapd->max_plinks - hapd->num_plinks;
116 return 0;
117}
118
119
120static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
121 struct sta_info *sta,
122 struct ieee802_11_elems *elems)
123{
124 if (!elems->supp_rates) {
125 wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
126 MAC2STR(sta->addr));
127 return WLAN_STATUS_UNSPECIFIED_FAILURE;
128 }
129
130 if (elems->supp_rates_len + elems->ext_supp_rates_len >
131 sizeof(sta->supported_rates)) {
132 wpa_msg(wpa_s, MSG_ERROR,
133 "Invalid supported rates element length " MACSTR
134 " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
135 elems->ext_supp_rates_len);
136 return WLAN_STATUS_UNSPECIFIED_FAILURE;
137 }
138
139 sta->supported_rates_len = merge_byte_arrays(
140 sta->supported_rates, sizeof(sta->supported_rates),
141 elems->supp_rates, elems->supp_rates_len,
142 elems->ext_supp_rates, elems->ext_supp_rates_len);
143
144 return WLAN_STATUS_SUCCESS;
145}
146
147
148/* return true if elems from a neighbor match this MBSS */
149static Boolean matches_local(struct wpa_supplicant *wpa_s,
150 struct ieee802_11_elems *elems)
151{
152 struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
153
154 if (elems->mesh_config_len < 5)
155 return FALSE;
156
157 return (mconf->meshid_len == elems->mesh_id_len &&
158 os_memcmp(mconf->meshid, elems->mesh_id,
159 elems->mesh_id_len) == 0 &&
160 mconf->mesh_pp_id == elems->mesh_config[0] &&
161 mconf->mesh_pm_id == elems->mesh_config[1] &&
162 mconf->mesh_cc_id == elems->mesh_config[2] &&
163 mconf->mesh_sp_id == elems->mesh_config[3] &&
164 mconf->mesh_auth_id == elems->mesh_config[4]);
165}
166
167
168/* check if local link id is already used with another peer */
169static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
170{
171 struct sta_info *sta;
172 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
173
174 for (sta = hapd->sta_list; sta; sta = sta->next) {
175 if (sta->my_lid == llid)
176 return TRUE;
177 }
178
179 return FALSE;
180}
181
182
183/* generate an llid for a link and set to initial state */
184static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
185 struct sta_info *sta)
186{
187 u16 llid;
188
189 do {
190 if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
191 continue;
192 } while (!llid || llid_in_use(wpa_s, llid));
193
194 sta->my_lid = llid;
195 sta->peer_lid = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800196
197 /*
198 * We do not use wpa_mesh_set_plink_state() here because there is no
199 * entry in kernel yet.
200 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800201 sta->plink_state = PLINK_LISTEN;
202}
203
204
205static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
206 struct sta_info *sta,
207 enum plink_action_field type,
208 u16 close_reason)
209{
210 struct wpabuf *buf;
211 struct hostapd_iface *ifmsh = wpa_s->ifmsh;
212 struct hostapd_data *bss = ifmsh->bss[0];
213 struct mesh_conf *conf = ifmsh->mconf;
214 u8 supp_rates[2 + 2 + 32];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800215 u8 *pos, *cat;
216 u8 ie_len, add_plid = 0;
217 int ret;
218 int ampe = conf->security & MESH_CONF_SEC_AMPE;
219 size_t buf_len;
220
221 if (!sta)
222 return;
223
224 buf_len = 2 + /* capability info */
225 2 + /* AID */
226 2 + 8 + /* supported rates */
227 2 + (32 - 8) +
228 2 + 32 + /* mesh ID */
229 2 + 7 + /* mesh config */
230 2 + 23 + /* peering management */
231 2 + 96 + /* AMPE */
232 2 + 16; /* MIC */
233#ifdef CONFIG_IEEE80211N
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800234 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800235 buf_len += 2 + 26 + /* HT capabilities */
236 2 + 22; /* HT operation */
237 }
238#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800239#ifdef CONFIG_IEEE80211AC
240 if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
241 buf_len += 2 + 12 + /* VHT Capabilities */
242 2 + 5; /* VHT Operation */
243 }
244#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800245 if (type != PLINK_CLOSE)
246 buf_len += conf->rsn_ie_len; /* RSN IE */
247
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800248 buf = wpabuf_alloc(buf_len);
249 if (!buf)
250 return;
251
252 cat = wpabuf_mhead_u8(buf);
253 wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
254 wpabuf_put_u8(buf, type);
255
256 if (type != PLINK_CLOSE) {
257 u8 info;
258
259 /* capability info */
260 wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
261
262 /* aid */
263 if (type == PLINK_CONFIRM)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800264 wpabuf_put_le16(buf, sta->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800265
266 /* IE: supp + ext. supp rates */
267 pos = hostapd_eid_supp_rates(bss, supp_rates);
268 pos = hostapd_eid_ext_supp_rates(bss, pos);
269 wpabuf_put_data(buf, supp_rates, pos - supp_rates);
270
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800271 /* IE: RSN IE */
272 wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
273
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800274 /* IE: Mesh ID */
275 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
276 wpabuf_put_u8(buf, conf->meshid_len);
277 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
278
279 /* IE: mesh conf */
280 wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
281 wpabuf_put_u8(buf, 7);
282 wpabuf_put_u8(buf, conf->mesh_pp_id);
283 wpabuf_put_u8(buf, conf->mesh_pm_id);
284 wpabuf_put_u8(buf, conf->mesh_cc_id);
285 wpabuf_put_u8(buf, conf->mesh_sp_id);
286 wpabuf_put_u8(buf, conf->mesh_auth_id);
287 info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
288 /* TODO: Add Connected to Mesh Gate/AS subfields */
289 wpabuf_put_u8(buf, info);
290 /* always forwarding & accepting plinks for now */
291 wpabuf_put_u8(buf, 0x1 | 0x8);
292 } else { /* Peer closing frame */
293 /* IE: Mesh ID */
294 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
295 wpabuf_put_u8(buf, conf->meshid_len);
296 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
297 }
298
299 /* IE: Mesh Peering Management element */
300 ie_len = 4;
301 if (ampe)
302 ie_len += PMKID_LEN;
303 switch (type) {
304 case PLINK_OPEN:
305 break;
306 case PLINK_CONFIRM:
307 ie_len += 2;
308 add_plid = 1;
309 break;
310 case PLINK_CLOSE:
311 ie_len += 2;
312 add_plid = 1;
313 ie_len += 2; /* reason code */
314 break;
315 }
316
317 wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
318 wpabuf_put_u8(buf, ie_len);
319 /* peering protocol */
320 if (ampe)
321 wpabuf_put_le16(buf, 1);
322 else
323 wpabuf_put_le16(buf, 0);
324 wpabuf_put_le16(buf, sta->my_lid);
325 if (add_plid)
326 wpabuf_put_le16(buf, sta->peer_lid);
327 if (type == PLINK_CLOSE)
328 wpabuf_put_le16(buf, close_reason);
329 if (ampe) {
330 if (sta->sae == NULL) {
331 wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
332 goto fail;
333 }
334 mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
335 wpabuf_put(buf, PMKID_LEN));
336 }
337
338#ifdef CONFIG_IEEE80211N
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800339 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800340 u8 ht_capa_oper[2 + 26 + 2 + 22];
341
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800342 pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
343 pos = hostapd_eid_ht_operation(bss, pos);
344 wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
345 }
346#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800347#ifdef CONFIG_IEEE80211AC
348 if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
349 u8 vht_capa_oper[2 + 12 + 2 + 5];
350
351 pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper);
352 pos = hostapd_eid_vht_operation(bss, pos);
353 wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
354 }
355#endif /* CONFIG_IEEE80211AC */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800356
357 if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
358 wpa_msg(wpa_s, MSG_INFO,
359 "Mesh MPM: failed to add AMPE and MIC IE");
360 goto fail;
361 }
362
363 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
364 sta->addr, wpa_s->own_addr, wpa_s->own_addr,
365 wpabuf_head(buf), wpabuf_len(buf), 0);
366 if (ret < 0)
367 wpa_msg(wpa_s, MSG_INFO,
368 "Mesh MPM: failed to send peering frame");
369
370fail:
371 wpabuf_free(buf);
372}
373
374
375/* configure peering state in ours and driver's station entry */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800376void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
377 struct sta_info *sta,
378 enum mesh_plink_state state)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379{
380 struct hostapd_sta_add_params params;
381 int ret;
382
383 sta->plink_state = state;
384
385 os_memset(&params, 0, sizeof(params));
386 params.addr = sta->addr;
387 params.plink_state = state;
388 params.set = 1;
389
390 wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
391 MAC2STR(sta->addr), mplstate[state]);
392 ret = wpa_drv_sta_add(wpa_s, &params);
393 if (ret) {
394 wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
395 ": %d", MAC2STR(sta->addr), ret);
396 }
397}
398
399
400static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
401 struct sta_info *sta)
402{
403 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
404
405 eloop_cancel_timeout(plink_timer, wpa_s, sta);
406
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800407 ap_free_sta(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408}
409
410
411static void plink_timer(void *eloop_ctx, void *user_data)
412{
413 struct wpa_supplicant *wpa_s = eloop_ctx;
414 struct sta_info *sta = user_data;
415 u16 reason = 0;
416 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
417
418 switch (sta->plink_state) {
419 case PLINK_OPEN_RCVD:
420 case PLINK_OPEN_SENT:
421 /* retry timer */
422 if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
423 eloop_register_timeout(
424 conf->dot11MeshRetryTimeout / 1000,
425 (conf->dot11MeshRetryTimeout % 1000) * 1000,
426 plink_timer, wpa_s, sta);
427 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
428 sta->mpm_retries++;
429 break;
430 }
431 reason = WLAN_REASON_MESH_MAX_RETRIES;
432 /* fall through on else */
433
434 case PLINK_CNF_RCVD:
435 /* confirm timer */
436 if (!reason)
437 reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800438 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800439 eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
440 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
441 plink_timer, wpa_s, sta);
442 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
443 break;
444 case PLINK_HOLDING:
445 /* holding timer */
446 mesh_mpm_fsm_restart(wpa_s, sta);
447 break;
448 default:
449 break;
450 }
451}
452
453
454/* initiate peering with station */
455static void
456mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
457 enum mesh_plink_state next_state)
458{
459 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
460
461 eloop_cancel_timeout(plink_timer, wpa_s, sta);
462 eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
463 (conf->dot11MeshRetryTimeout % 1000) * 1000,
464 plink_timer, wpa_s, sta);
465 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
466 wpa_mesh_set_plink_state(wpa_s, sta, next_state);
467}
468
469
470int mesh_mpm_plink_close(struct hostapd_data *hapd,
471 struct sta_info *sta, void *ctx)
472{
473 struct wpa_supplicant *wpa_s = ctx;
474 int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
475
476 if (sta) {
477 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
478 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
479 wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
480 MAC2STR(sta->addr));
481 eloop_cancel_timeout(plink_timer, wpa_s, sta);
482 return 0;
483 }
484
485 return 1;
486}
487
488
489void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
490{
491 struct hostapd_data *hapd = ifmsh->bss[0];
492
493 /* notify peers we're leaving */
494 ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
495
496 hapd->num_plinks = 0;
497 hostapd_free_stas(hapd);
498}
499
500
501/* for mesh_rsn to indicate this peer has completed authentication, and we're
502 * ready to start AMPE */
503void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
504{
505 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
506 struct hostapd_sta_add_params params;
507 struct sta_info *sta;
508 int ret;
509
510 sta = ap_get_sta(data, addr);
511 if (!sta) {
512 wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
513 return;
514 }
515
516 /* TODO: Should do nothing if this STA is already authenticated, but
517 * the AP code already sets this flag. */
518 sta->flags |= WLAN_STA_AUTH;
519
520 mesh_rsn_init_ampe_sta(wpa_s, sta);
521
522 os_memset(&params, 0, sizeof(params));
523 params.addr = sta->addr;
524 params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
525 params.set = 1;
526
527 wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
528 MAC2STR(sta->addr));
529 ret = wpa_drv_sta_add(wpa_s, &params);
530 if (ret) {
531 wpa_msg(wpa_s, MSG_ERROR,
532 "Driver failed to set " MACSTR ": %d",
533 MAC2STR(sta->addr), ret);
534 }
535
536 if (!sta->my_lid)
537 mesh_mpm_init_link(wpa_s, sta);
538
539 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
540}
541
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800542/*
543 * Initialize a sta_info structure for a peer and upload it into the driver
544 * in preparation for beginning authentication or peering. This is done when a
545 * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
546 * received from the peer for the first time.
547 */
548static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
549 const u8 *addr,
550 struct ieee802_11_elems *elems)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800551{
552 struct hostapd_sta_add_params params;
553 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
554 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
555 struct sta_info *sta;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800556 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800557
558 sta = ap_get_sta(data, addr);
559 if (!sta) {
560 sta = ap_sta_add(data, addr);
561 if (!sta)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800562 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800563 }
564
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800565 /* Set WMM by default since Mesh STAs are QoS STAs */
566 sta->flags |= WLAN_STA_WMM;
567
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800568 /* initialize sta */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800569 if (copy_supp_rates(wpa_s, sta, elems)) {
570 ap_free_sta(data, sta);
571 return NULL;
572 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800573
574 mesh_mpm_init_link(wpa_s, sta);
575
576#ifdef CONFIG_IEEE80211N
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700577 copy_sta_ht_capab(data, sta, elems->ht_capabilities);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 update_ht_state(data, sta);
579#endif /* CONFIG_IEEE80211N */
580
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800581#ifdef CONFIG_IEEE80211AC
582 copy_sta_vht_capab(data, sta, elems->vht_capabilities);
583 set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
584#endif /* CONFIG_IEEE80211AC */
585
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800586 if (hostapd_get_aid(data, sta) < 0) {
587 wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
588 ap_free_sta(data, sta);
589 return NULL;
590 }
591
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800592 /* insert into driver */
593 os_memset(&params, 0, sizeof(params));
594 params.supp_rates = sta->supported_rates;
595 params.supp_rates_len = sta->supported_rates_len;
596 params.addr = addr;
597 params.plink_state = sta->plink_state;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800598 params.aid = sta->aid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800599 params.listen_interval = 100;
600 params.ht_capabilities = sta->ht_capabilities;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800601 params.vht_capabilities = sta->vht_capabilities;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800602 params.flags |= WPA_STA_WMM;
603 params.flags_mask |= WPA_STA_AUTHENTICATED;
604 if (conf->security == MESH_CONF_SEC_NONE) {
605 params.flags |= WPA_STA_AUTHORIZED;
606 params.flags |= WPA_STA_AUTHENTICATED;
607 } else {
608 sta->flags |= WLAN_STA_MFP;
609 params.flags |= WPA_STA_MFP;
610 }
611
612 ret = wpa_drv_sta_add(wpa_s, &params);
613 if (ret) {
614 wpa_msg(wpa_s, MSG_ERROR,
615 "Driver failed to insert " MACSTR ": %d",
616 MAC2STR(addr), ret);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800617 ap_free_sta(data, sta);
618 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800619 }
620
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800621 return sta;
622}
623
624
625void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
626 struct ieee802_11_elems *elems)
627{
628 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
629 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
630 struct sta_info *sta;
631 struct wpa_ssid *ssid = wpa_s->current_ssid;
632
633 sta = mesh_mpm_add_peer(wpa_s, addr, elems);
634 if (!sta)
635 return;
636
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800637 if (ssid && ssid->no_auto_peer) {
638 wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
639 MACSTR " because of no_auto_peer", MAC2STR(addr));
640 if (data->mesh_pending_auth) {
641 struct os_reltime age;
642 const struct ieee80211_mgmt *mgmt;
643 struct hostapd_frame_info fi;
644
645 mgmt = wpabuf_head(data->mesh_pending_auth);
646 os_reltime_age(&data->mesh_pending_auth_time, &age);
647 if (age.sec < 2 &&
648 os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
649 wpa_printf(MSG_DEBUG,
650 "mesh: Process pending Authentication frame from %u.%06u seconds ago",
651 (unsigned int) age.sec,
652 (unsigned int) age.usec);
653 os_memset(&fi, 0, sizeof(fi));
654 ieee802_11_mgmt(
655 data,
656 wpabuf_head(data->mesh_pending_auth),
657 wpabuf_len(data->mesh_pending_auth),
658 &fi);
659 }
660 wpabuf_free(data->mesh_pending_auth);
661 data->mesh_pending_auth = NULL;
662 }
663 return;
664 }
665
666 if (conf->security == MESH_CONF_SEC_NONE)
667 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
668 else
669 mesh_rsn_auth_sae_sta(wpa_s, sta);
670}
671
672
673void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
674{
675 struct hostapd_frame_info fi;
676
677 os_memset(&fi, 0, sizeof(fi));
678 fi.datarate = rx_mgmt->datarate;
679 fi.ssi_signal = rx_mgmt->ssi_signal;
680 ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
681 rx_mgmt->frame_len, &fi);
682}
683
684
685static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
686 struct sta_info *sta)
687{
688 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
689 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
690 u8 seq[6] = {};
691
692 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
693 MAC2STR(sta->addr));
694
695 if (conf->security & MESH_CONF_SEC_AMPE) {
696 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
697 seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
698 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
699 seq, sizeof(seq),
700 sta->mgtk, sizeof(sta->mgtk));
701 wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
702 seq, sizeof(seq),
703 sta->mgtk, sizeof(sta->mgtk));
704
705 wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
706 wpa_hexdump_key(MSG_DEBUG, "mgtk:",
707 sta->mgtk, sizeof(sta->mgtk));
708 }
709
710 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
711 hapd->num_plinks++;
712
713 sta->flags |= WLAN_STA_ASSOC;
714
715 eloop_cancel_timeout(plink_timer, wpa_s, sta);
716
717 /* Send ctrl event */
718 wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
719 MAC2STR(sta->addr));
720}
721
722
723static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
724 enum plink_event event)
725{
726 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
727 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
728 u16 reason = 0;
729
730 wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
731 MAC2STR(sta->addr), mplstate[sta->plink_state],
732 mplevent[event]);
733
734 switch (sta->plink_state) {
735 case PLINK_LISTEN:
736 switch (event) {
737 case CLS_ACPT:
738 mesh_mpm_fsm_restart(wpa_s, sta);
739 break;
740 case OPN_ACPT:
741 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
742 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
743 0);
744 break;
745 default:
746 break;
747 }
748 break;
749 case PLINK_OPEN_SENT:
750 switch (event) {
751 case OPN_RJCT:
752 case CNF_RJCT:
753 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
754 /* fall-through */
755 case CLS_ACPT:
756 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
757 if (!reason)
758 reason = WLAN_REASON_MESH_CLOSE_RCVD;
759 eloop_register_timeout(
760 conf->dot11MeshHoldingTimeout / 1000,
761 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
762 plink_timer, wpa_s, sta);
763 mesh_mpm_send_plink_action(wpa_s, sta,
764 PLINK_CLOSE, reason);
765 break;
766 case OPN_ACPT:
767 /* retry timer is left untouched */
768 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
769 mesh_mpm_send_plink_action(wpa_s, sta,
770 PLINK_CONFIRM, 0);
771 break;
772 case CNF_ACPT:
773 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
774 eloop_register_timeout(
775 conf->dot11MeshConfirmTimeout / 1000,
776 (conf->dot11MeshConfirmTimeout % 1000) * 1000,
777 plink_timer, wpa_s, sta);
778 break;
779 default:
780 break;
781 }
782 break;
783 case PLINK_OPEN_RCVD:
784 switch (event) {
785 case OPN_RJCT:
786 case CNF_RJCT:
787 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
788 /* fall-through */
789 case CLS_ACPT:
790 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
791 if (!reason)
792 reason = WLAN_REASON_MESH_CLOSE_RCVD;
793 eloop_register_timeout(
794 conf->dot11MeshHoldingTimeout / 1000,
795 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
796 plink_timer, wpa_s, sta);
797 sta->mpm_close_reason = reason;
798 mesh_mpm_send_plink_action(wpa_s, sta,
799 PLINK_CLOSE, reason);
800 break;
801 case OPN_ACPT:
802 mesh_mpm_send_plink_action(wpa_s, sta,
803 PLINK_CONFIRM, 0);
804 break;
805 case CNF_ACPT:
806 if (conf->security & MESH_CONF_SEC_AMPE)
807 mesh_rsn_derive_mtk(wpa_s, sta);
808 mesh_mpm_plink_estab(wpa_s, sta);
809 break;
810 default:
811 break;
812 }
813 break;
814 case PLINK_CNF_RCVD:
815 switch (event) {
816 case OPN_RJCT:
817 case CNF_RJCT:
818 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
819 /* fall-through */
820 case CLS_ACPT:
821 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
822 if (!reason)
823 reason = WLAN_REASON_MESH_CLOSE_RCVD;
824 eloop_register_timeout(
825 conf->dot11MeshHoldingTimeout / 1000,
826 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
827 plink_timer, wpa_s, sta);
828 sta->mpm_close_reason = reason;
829 mesh_mpm_send_plink_action(wpa_s, sta,
830 PLINK_CLOSE, reason);
831 break;
832 case OPN_ACPT:
833 mesh_mpm_plink_estab(wpa_s, sta);
834 mesh_mpm_send_plink_action(wpa_s, sta,
835 PLINK_CONFIRM, 0);
836 break;
837 default:
838 break;
839 }
840 break;
841 case PLINK_ESTAB:
842 switch (event) {
843 case CLS_ACPT:
844 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
845 reason = WLAN_REASON_MESH_CLOSE_RCVD;
846
847 eloop_register_timeout(
848 conf->dot11MeshHoldingTimeout / 1000,
849 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
850 plink_timer, wpa_s, sta);
851 sta->mpm_close_reason = reason;
852
853 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
854 " closed with reason %d",
855 MAC2STR(sta->addr), reason);
856
857 wpa_msg_ctrl(wpa_s, MSG_INFO,
858 MESH_PEER_DISCONNECTED MACSTR,
859 MAC2STR(sta->addr));
860
861 hapd->num_plinks--;
862
863 mesh_mpm_send_plink_action(wpa_s, sta,
864 PLINK_CLOSE, reason);
865 break;
866 case OPN_ACPT:
867 mesh_mpm_send_plink_action(wpa_s, sta,
868 PLINK_CONFIRM, 0);
869 break;
870 default:
871 break;
872 }
873 break;
874 case PLINK_HOLDING:
875 switch (event) {
876 case CLS_ACPT:
877 mesh_mpm_fsm_restart(wpa_s, sta);
878 break;
879 case OPN_ACPT:
880 case CNF_ACPT:
881 case OPN_RJCT:
882 case CNF_RJCT:
883 reason = sta->mpm_close_reason;
884 mesh_mpm_send_plink_action(wpa_s, sta,
885 PLINK_CLOSE, reason);
886 break;
887 default:
888 break;
889 }
890 break;
891 default:
892 wpa_msg(wpa_s, MSG_DEBUG,
893 "Unsupported MPM event %s for state %s",
894 mplevent[event], mplstate[sta->plink_state]);
895 break;
896 }
897}
898
899
900void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
901 const struct ieee80211_mgmt *mgmt, size_t len)
902{
903 u8 action_field;
904 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
905 struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
906 struct sta_info *sta;
907 u16 plid = 0, llid = 0;
908 enum plink_event event;
909 struct ieee802_11_elems elems;
910 struct mesh_peer_mgmt_ie peer_mgmt_ie;
911 const u8 *ies;
912 size_t ie_len;
913 int ret;
914
915 if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
916 return;
917
918 action_field = mgmt->u.action.u.slf_prot_action.action;
919 if (action_field != PLINK_OPEN &&
920 action_field != PLINK_CONFIRM &&
921 action_field != PLINK_CLOSE)
922 return;
923
924 ies = mgmt->u.action.u.slf_prot_action.variable;
925 ie_len = (const u8 *) mgmt + len -
926 mgmt->u.action.u.slf_prot_action.variable;
927
928 /* at least expect mesh id and peering mgmt */
929 if (ie_len < 2 + 2) {
930 wpa_printf(MSG_DEBUG,
931 "MPM: Ignore too short action frame %u ie_len %u",
932 action_field, (unsigned int) ie_len);
933 return;
934 }
935 wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
936
937 if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
938 wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
939 WPA_GET_LE16(ies));
940 ies += 2; /* capability */
941 ie_len -= 2;
942 }
943 if (action_field == PLINK_CONFIRM) {
944 wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
945 ies += 2; /* aid */
946 ie_len -= 2;
947 }
948
949 /* check for mesh peering, mesh id and mesh config IEs */
950 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
951 wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
952 return;
953 }
954 if (!elems.peer_mgmt) {
955 wpa_printf(MSG_DEBUG,
956 "MPM: No Mesh Peering Management element");
957 return;
958 }
959 if (action_field != PLINK_CLOSE) {
960 if (!elems.mesh_id || !elems.mesh_config) {
961 wpa_printf(MSG_DEBUG,
962 "MPM: No Mesh ID or Mesh Configuration element");
963 return;
964 }
965
966 if (!matches_local(wpa_s, &elems)) {
967 wpa_printf(MSG_DEBUG,
968 "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
969 return;
970 }
971 }
972
973 ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
974 elems.peer_mgmt,
975 elems.peer_mgmt_len,
976 &peer_mgmt_ie);
977 if (ret) {
978 wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
979 return;
980 }
981
982 /* the sender's llid is our plid and vice-versa */
983 plid = WPA_GET_LE16(peer_mgmt_ie.llid);
984 if (peer_mgmt_ie.plid)
985 llid = WPA_GET_LE16(peer_mgmt_ie.plid);
986 wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
987
988 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800989
990 /*
991 * If this is an open frame from an unknown STA, and this is an
992 * open mesh, then go ahead and add the peer before proceeding.
993 */
994 if (!sta && action_field == PLINK_OPEN &&
995 !(mconf->security & MESH_CONF_SEC_AMPE))
996 sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800998 if (!sta) {
999 wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
1000 return;
1001 }
1002
1003#ifdef CONFIG_SAE
1004 /* peer is in sae_accepted? */
1005 if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
1006 wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
1007 return;
1008 }
1009#endif /* CONFIG_SAE */
1010
1011 if (!sta->my_lid)
1012 mesh_mpm_init_link(wpa_s, sta);
1013
1014 if ((mconf->security & MESH_CONF_SEC_AMPE) &&
1015 mesh_rsn_process_ampe(wpa_s, sta, &elems,
1016 &mgmt->u.action.category,
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08001017 peer_mgmt_ie.chosen_pmk,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001018 ies, ie_len)) {
1019 wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
1020 return;
1021 }
1022
1023 if (sta->plink_state == PLINK_BLOCKED) {
1024 wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
1025 return;
1026 }
1027
1028 /* Now we will figure out the appropriate event... */
1029 switch (action_field) {
1030 case PLINK_OPEN:
1031 if (plink_free_count(hapd) == 0) {
1032 event = OPN_IGNR;
1033 wpa_printf(MSG_INFO,
1034 "MPM: Peer link num over quota(%d)",
1035 hapd->max_plinks);
1036 } else if (sta->peer_lid && sta->peer_lid != plid) {
1037 event = OPN_IGNR;
1038 } else {
1039 sta->peer_lid = plid;
1040 event = OPN_ACPT;
1041 }
1042 break;
1043 case PLINK_CONFIRM:
1044 if (plink_free_count(hapd) == 0) {
1045 event = CNF_IGNR;
1046 wpa_printf(MSG_INFO,
1047 "MPM: Peer link num over quota(%d)",
1048 hapd->max_plinks);
1049 } else if (sta->my_lid != llid ||
1050 (sta->peer_lid && sta->peer_lid != plid)) {
1051 event = CNF_IGNR;
1052 } else {
1053 if (!sta->peer_lid)
1054 sta->peer_lid = plid;
1055 event = CNF_ACPT;
1056 }
1057 break;
1058 case PLINK_CLOSE:
1059 if (sta->plink_state == PLINK_ESTAB)
1060 /* Do not check for llid or plid. This does not
1061 * follow the standard but since multiple plinks
1062 * per cand are not supported, it is necessary in
1063 * order to avoid a livelock when MP A sees an
1064 * establish peer link to MP B but MP B does not
1065 * see it. This can be caused by a timeout in
1066 * B's peer link establishment or B being
1067 * restarted.
1068 */
1069 event = CLS_ACPT;
1070 else if (sta->peer_lid != plid)
1071 event = CLS_IGNR;
1072 else if (peer_mgmt_ie.plid && sta->my_lid != llid)
1073 event = CLS_IGNR;
1074 else
1075 event = CLS_ACPT;
1076 break;
1077 default:
1078 /*
1079 * This cannot be hit due to the action_field check above, but
1080 * compilers may not be able to figure that out and can warn
1081 * about uninitialized event below.
1082 */
1083 return;
1084 }
1085 mesh_mpm_fsm(wpa_s, sta, event);
1086}
1087
1088
1089/* called by ap_free_sta */
1090void mesh_mpm_free_sta(struct sta_info *sta)
1091{
1092 eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
1093 eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
1094}