blob: 86b2bcb093777a4d4fcf3f06ecc71c855ff5b88e [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 {
23 const u8 *proto_id;
24 const u8 *llid;
25 const u8 *plid;
26 const u8 *reason;
27 const u8 *pmk;
28};
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
75 /* remove optional PMK at end */
76 if (len >= 16) {
77 len -= 16;
78 mpm_ie->pmk = ie + len - 16;
79 }
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
104 /* plid, present for confirm, and possibly close */
105 if (len)
106 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];
215#ifdef CONFIG_IEEE80211N
216 u8 ht_capa_oper[2 + 26 + 2 + 22];
217#endif /* CONFIG_IEEE80211N */
218 u8 *pos, *cat;
219 u8 ie_len, add_plid = 0;
220 int ret;
221 int ampe = conf->security & MESH_CONF_SEC_AMPE;
222 size_t buf_len;
223
224 if (!sta)
225 return;
226
227 buf_len = 2 + /* capability info */
228 2 + /* AID */
229 2 + 8 + /* supported rates */
230 2 + (32 - 8) +
231 2 + 32 + /* mesh ID */
232 2 + 7 + /* mesh config */
233 2 + 23 + /* peering management */
234 2 + 96 + /* AMPE */
235 2 + 16; /* MIC */
236#ifdef CONFIG_IEEE80211N
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800237 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800238 buf_len += 2 + 26 + /* HT capabilities */
239 2 + 22; /* HT operation */
240 }
241#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800242 if (type != PLINK_CLOSE)
243 buf_len += conf->rsn_ie_len; /* RSN IE */
244
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800245 buf = wpabuf_alloc(buf_len);
246 if (!buf)
247 return;
248
249 cat = wpabuf_mhead_u8(buf);
250 wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
251 wpabuf_put_u8(buf, type);
252
253 if (type != PLINK_CLOSE) {
254 u8 info;
255
256 /* capability info */
257 wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
258
259 /* aid */
260 if (type == PLINK_CONFIRM)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800261 wpabuf_put_le16(buf, sta->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800262
263 /* IE: supp + ext. supp rates */
264 pos = hostapd_eid_supp_rates(bss, supp_rates);
265 pos = hostapd_eid_ext_supp_rates(bss, pos);
266 wpabuf_put_data(buf, supp_rates, pos - supp_rates);
267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800268 /* IE: RSN IE */
269 wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
270
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800271 /* IE: Mesh ID */
272 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
273 wpabuf_put_u8(buf, conf->meshid_len);
274 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
275
276 /* IE: mesh conf */
277 wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
278 wpabuf_put_u8(buf, 7);
279 wpabuf_put_u8(buf, conf->mesh_pp_id);
280 wpabuf_put_u8(buf, conf->mesh_pm_id);
281 wpabuf_put_u8(buf, conf->mesh_cc_id);
282 wpabuf_put_u8(buf, conf->mesh_sp_id);
283 wpabuf_put_u8(buf, conf->mesh_auth_id);
284 info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
285 /* TODO: Add Connected to Mesh Gate/AS subfields */
286 wpabuf_put_u8(buf, info);
287 /* always forwarding & accepting plinks for now */
288 wpabuf_put_u8(buf, 0x1 | 0x8);
289 } else { /* Peer closing frame */
290 /* IE: Mesh ID */
291 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
292 wpabuf_put_u8(buf, conf->meshid_len);
293 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
294 }
295
296 /* IE: Mesh Peering Management element */
297 ie_len = 4;
298 if (ampe)
299 ie_len += PMKID_LEN;
300 switch (type) {
301 case PLINK_OPEN:
302 break;
303 case PLINK_CONFIRM:
304 ie_len += 2;
305 add_plid = 1;
306 break;
307 case PLINK_CLOSE:
308 ie_len += 2;
309 add_plid = 1;
310 ie_len += 2; /* reason code */
311 break;
312 }
313
314 wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
315 wpabuf_put_u8(buf, ie_len);
316 /* peering protocol */
317 if (ampe)
318 wpabuf_put_le16(buf, 1);
319 else
320 wpabuf_put_le16(buf, 0);
321 wpabuf_put_le16(buf, sta->my_lid);
322 if (add_plid)
323 wpabuf_put_le16(buf, sta->peer_lid);
324 if (type == PLINK_CLOSE)
325 wpabuf_put_le16(buf, close_reason);
326 if (ampe) {
327 if (sta->sae == NULL) {
328 wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
329 goto fail;
330 }
331 mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
332 wpabuf_put(buf, PMKID_LEN));
333 }
334
335#ifdef CONFIG_IEEE80211N
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800336 if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800337 pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
338 pos = hostapd_eid_ht_operation(bss, pos);
339 wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
340 }
341#endif /* CONFIG_IEEE80211N */
342
343 if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
344 wpa_msg(wpa_s, MSG_INFO,
345 "Mesh MPM: failed to add AMPE and MIC IE");
346 goto fail;
347 }
348
349 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
350 sta->addr, wpa_s->own_addr, wpa_s->own_addr,
351 wpabuf_head(buf), wpabuf_len(buf), 0);
352 if (ret < 0)
353 wpa_msg(wpa_s, MSG_INFO,
354 "Mesh MPM: failed to send peering frame");
355
356fail:
357 wpabuf_free(buf);
358}
359
360
361/* configure peering state in ours and driver's station entry */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800362void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
363 struct sta_info *sta,
364 enum mesh_plink_state state)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800365{
366 struct hostapd_sta_add_params params;
367 int ret;
368
369 sta->plink_state = state;
370
371 os_memset(&params, 0, sizeof(params));
372 params.addr = sta->addr;
373 params.plink_state = state;
374 params.set = 1;
375
376 wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
377 MAC2STR(sta->addr), mplstate[state]);
378 ret = wpa_drv_sta_add(wpa_s, &params);
379 if (ret) {
380 wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
381 ": %d", MAC2STR(sta->addr), ret);
382 }
383}
384
385
386static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
387 struct sta_info *sta)
388{
389 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
390
391 eloop_cancel_timeout(plink_timer, wpa_s, sta);
392
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800393 ap_free_sta(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800394}
395
396
397static void plink_timer(void *eloop_ctx, void *user_data)
398{
399 struct wpa_supplicant *wpa_s = eloop_ctx;
400 struct sta_info *sta = user_data;
401 u16 reason = 0;
402 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
403
404 switch (sta->plink_state) {
405 case PLINK_OPEN_RCVD:
406 case PLINK_OPEN_SENT:
407 /* retry timer */
408 if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
409 eloop_register_timeout(
410 conf->dot11MeshRetryTimeout / 1000,
411 (conf->dot11MeshRetryTimeout % 1000) * 1000,
412 plink_timer, wpa_s, sta);
413 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
414 sta->mpm_retries++;
415 break;
416 }
417 reason = WLAN_REASON_MESH_MAX_RETRIES;
418 /* fall through on else */
419
420 case PLINK_CNF_RCVD:
421 /* confirm timer */
422 if (!reason)
423 reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800424 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800425 eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
426 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
427 plink_timer, wpa_s, sta);
428 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
429 break;
430 case PLINK_HOLDING:
431 /* holding timer */
432 mesh_mpm_fsm_restart(wpa_s, sta);
433 break;
434 default:
435 break;
436 }
437}
438
439
440/* initiate peering with station */
441static void
442mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
443 enum mesh_plink_state next_state)
444{
445 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
446
447 eloop_cancel_timeout(plink_timer, wpa_s, sta);
448 eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
449 (conf->dot11MeshRetryTimeout % 1000) * 1000,
450 plink_timer, wpa_s, sta);
451 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
452 wpa_mesh_set_plink_state(wpa_s, sta, next_state);
453}
454
455
456int mesh_mpm_plink_close(struct hostapd_data *hapd,
457 struct sta_info *sta, void *ctx)
458{
459 struct wpa_supplicant *wpa_s = ctx;
460 int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
461
462 if (sta) {
463 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
464 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
465 wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
466 MAC2STR(sta->addr));
467 eloop_cancel_timeout(plink_timer, wpa_s, sta);
468 return 0;
469 }
470
471 return 1;
472}
473
474
475void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
476{
477 struct hostapd_data *hapd = ifmsh->bss[0];
478
479 /* notify peers we're leaving */
480 ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
481
482 hapd->num_plinks = 0;
483 hostapd_free_stas(hapd);
484}
485
486
487/* for mesh_rsn to indicate this peer has completed authentication, and we're
488 * ready to start AMPE */
489void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
490{
491 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
492 struct hostapd_sta_add_params params;
493 struct sta_info *sta;
494 int ret;
495
496 sta = ap_get_sta(data, addr);
497 if (!sta) {
498 wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
499 return;
500 }
501
502 /* TODO: Should do nothing if this STA is already authenticated, but
503 * the AP code already sets this flag. */
504 sta->flags |= WLAN_STA_AUTH;
505
506 mesh_rsn_init_ampe_sta(wpa_s, sta);
507
508 os_memset(&params, 0, sizeof(params));
509 params.addr = sta->addr;
510 params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
511 params.set = 1;
512
513 wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
514 MAC2STR(sta->addr));
515 ret = wpa_drv_sta_add(wpa_s, &params);
516 if (ret) {
517 wpa_msg(wpa_s, MSG_ERROR,
518 "Driver failed to set " MACSTR ": %d",
519 MAC2STR(sta->addr), ret);
520 }
521
522 if (!sta->my_lid)
523 mesh_mpm_init_link(wpa_s, sta);
524
525 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
526}
527
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800528/*
529 * Initialize a sta_info structure for a peer and upload it into the driver
530 * in preparation for beginning authentication or peering. This is done when a
531 * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
532 * received from the peer for the first time.
533 */
534static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
535 const u8 *addr,
536 struct ieee802_11_elems *elems)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800537{
538 struct hostapd_sta_add_params params;
539 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
540 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
541 struct sta_info *sta;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800542 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800543
544 sta = ap_get_sta(data, addr);
545 if (!sta) {
546 sta = ap_sta_add(data, addr);
547 if (!sta)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800548 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800549 }
550
551 /* initialize sta */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800552 if (copy_supp_rates(wpa_s, sta, elems)) {
553 ap_free_sta(data, sta);
554 return NULL;
555 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800556
557 mesh_mpm_init_link(wpa_s, sta);
558
559#ifdef CONFIG_IEEE80211N
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700560 copy_sta_ht_capab(data, sta, elems->ht_capabilities);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800561 update_ht_state(data, sta);
562#endif /* CONFIG_IEEE80211N */
563
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800564 if (hostapd_get_aid(data, sta) < 0) {
565 wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
566 ap_free_sta(data, sta);
567 return NULL;
568 }
569
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800570 /* insert into driver */
571 os_memset(&params, 0, sizeof(params));
572 params.supp_rates = sta->supported_rates;
573 params.supp_rates_len = sta->supported_rates_len;
574 params.addr = addr;
575 params.plink_state = sta->plink_state;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800576 params.aid = sta->aid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577 params.listen_interval = 100;
578 params.ht_capabilities = sta->ht_capabilities;
579 params.flags |= WPA_STA_WMM;
580 params.flags_mask |= WPA_STA_AUTHENTICATED;
581 if (conf->security == MESH_CONF_SEC_NONE) {
582 params.flags |= WPA_STA_AUTHORIZED;
583 params.flags |= WPA_STA_AUTHENTICATED;
584 } else {
585 sta->flags |= WLAN_STA_MFP;
586 params.flags |= WPA_STA_MFP;
587 }
588
589 ret = wpa_drv_sta_add(wpa_s, &params);
590 if (ret) {
591 wpa_msg(wpa_s, MSG_ERROR,
592 "Driver failed to insert " MACSTR ": %d",
593 MAC2STR(addr), ret);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800594 ap_free_sta(data, sta);
595 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800596 }
597
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800598 return sta;
599}
600
601
602void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
603 struct ieee802_11_elems *elems)
604{
605 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
606 struct hostapd_data *data = wpa_s->ifmsh->bss[0];
607 struct sta_info *sta;
608 struct wpa_ssid *ssid = wpa_s->current_ssid;
609
610 sta = mesh_mpm_add_peer(wpa_s, addr, elems);
611 if (!sta)
612 return;
613
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800614 if (ssid && ssid->no_auto_peer) {
615 wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
616 MACSTR " because of no_auto_peer", MAC2STR(addr));
617 if (data->mesh_pending_auth) {
618 struct os_reltime age;
619 const struct ieee80211_mgmt *mgmt;
620 struct hostapd_frame_info fi;
621
622 mgmt = wpabuf_head(data->mesh_pending_auth);
623 os_reltime_age(&data->mesh_pending_auth_time, &age);
624 if (age.sec < 2 &&
625 os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
626 wpa_printf(MSG_DEBUG,
627 "mesh: Process pending Authentication frame from %u.%06u seconds ago",
628 (unsigned int) age.sec,
629 (unsigned int) age.usec);
630 os_memset(&fi, 0, sizeof(fi));
631 ieee802_11_mgmt(
632 data,
633 wpabuf_head(data->mesh_pending_auth),
634 wpabuf_len(data->mesh_pending_auth),
635 &fi);
636 }
637 wpabuf_free(data->mesh_pending_auth);
638 data->mesh_pending_auth = NULL;
639 }
640 return;
641 }
642
643 if (conf->security == MESH_CONF_SEC_NONE)
644 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
645 else
646 mesh_rsn_auth_sae_sta(wpa_s, sta);
647}
648
649
650void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
651{
652 struct hostapd_frame_info fi;
653
654 os_memset(&fi, 0, sizeof(fi));
655 fi.datarate = rx_mgmt->datarate;
656 fi.ssi_signal = rx_mgmt->ssi_signal;
657 ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
658 rx_mgmt->frame_len, &fi);
659}
660
661
662static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
663 struct sta_info *sta)
664{
665 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
666 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
667 u8 seq[6] = {};
668
669 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
670 MAC2STR(sta->addr));
671
672 if (conf->security & MESH_CONF_SEC_AMPE) {
673 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
674 seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
675 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
676 seq, sizeof(seq),
677 sta->mgtk, sizeof(sta->mgtk));
678 wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
679 seq, sizeof(seq),
680 sta->mgtk, sizeof(sta->mgtk));
681
682 wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
683 wpa_hexdump_key(MSG_DEBUG, "mgtk:",
684 sta->mgtk, sizeof(sta->mgtk));
685 }
686
687 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
688 hapd->num_plinks++;
689
690 sta->flags |= WLAN_STA_ASSOC;
691
692 eloop_cancel_timeout(plink_timer, wpa_s, sta);
693
694 /* Send ctrl event */
695 wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
696 MAC2STR(sta->addr));
697}
698
699
700static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
701 enum plink_event event)
702{
703 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
704 struct mesh_conf *conf = wpa_s->ifmsh->mconf;
705 u16 reason = 0;
706
707 wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
708 MAC2STR(sta->addr), mplstate[sta->plink_state],
709 mplevent[event]);
710
711 switch (sta->plink_state) {
712 case PLINK_LISTEN:
713 switch (event) {
714 case CLS_ACPT:
715 mesh_mpm_fsm_restart(wpa_s, sta);
716 break;
717 case OPN_ACPT:
718 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
719 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
720 0);
721 break;
722 default:
723 break;
724 }
725 break;
726 case PLINK_OPEN_SENT:
727 switch (event) {
728 case OPN_RJCT:
729 case CNF_RJCT:
730 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
731 /* fall-through */
732 case CLS_ACPT:
733 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
734 if (!reason)
735 reason = WLAN_REASON_MESH_CLOSE_RCVD;
736 eloop_register_timeout(
737 conf->dot11MeshHoldingTimeout / 1000,
738 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
739 plink_timer, wpa_s, sta);
740 mesh_mpm_send_plink_action(wpa_s, sta,
741 PLINK_CLOSE, reason);
742 break;
743 case OPN_ACPT:
744 /* retry timer is left untouched */
745 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
746 mesh_mpm_send_plink_action(wpa_s, sta,
747 PLINK_CONFIRM, 0);
748 break;
749 case CNF_ACPT:
750 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
751 eloop_register_timeout(
752 conf->dot11MeshConfirmTimeout / 1000,
753 (conf->dot11MeshConfirmTimeout % 1000) * 1000,
754 plink_timer, wpa_s, sta);
755 break;
756 default:
757 break;
758 }
759 break;
760 case PLINK_OPEN_RCVD:
761 switch (event) {
762 case OPN_RJCT:
763 case CNF_RJCT:
764 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
765 /* fall-through */
766 case CLS_ACPT:
767 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
768 if (!reason)
769 reason = WLAN_REASON_MESH_CLOSE_RCVD;
770 eloop_register_timeout(
771 conf->dot11MeshHoldingTimeout / 1000,
772 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
773 plink_timer, wpa_s, sta);
774 sta->mpm_close_reason = reason;
775 mesh_mpm_send_plink_action(wpa_s, sta,
776 PLINK_CLOSE, reason);
777 break;
778 case OPN_ACPT:
779 mesh_mpm_send_plink_action(wpa_s, sta,
780 PLINK_CONFIRM, 0);
781 break;
782 case CNF_ACPT:
783 if (conf->security & MESH_CONF_SEC_AMPE)
784 mesh_rsn_derive_mtk(wpa_s, sta);
785 mesh_mpm_plink_estab(wpa_s, sta);
786 break;
787 default:
788 break;
789 }
790 break;
791 case PLINK_CNF_RCVD:
792 switch (event) {
793 case OPN_RJCT:
794 case CNF_RJCT:
795 reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
796 /* fall-through */
797 case CLS_ACPT:
798 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
799 if (!reason)
800 reason = WLAN_REASON_MESH_CLOSE_RCVD;
801 eloop_register_timeout(
802 conf->dot11MeshHoldingTimeout / 1000,
803 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
804 plink_timer, wpa_s, sta);
805 sta->mpm_close_reason = reason;
806 mesh_mpm_send_plink_action(wpa_s, sta,
807 PLINK_CLOSE, reason);
808 break;
809 case OPN_ACPT:
810 mesh_mpm_plink_estab(wpa_s, sta);
811 mesh_mpm_send_plink_action(wpa_s, sta,
812 PLINK_CONFIRM, 0);
813 break;
814 default:
815 break;
816 }
817 break;
818 case PLINK_ESTAB:
819 switch (event) {
820 case CLS_ACPT:
821 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
822 reason = WLAN_REASON_MESH_CLOSE_RCVD;
823
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
830 wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
831 " closed with reason %d",
832 MAC2STR(sta->addr), reason);
833
834 wpa_msg_ctrl(wpa_s, MSG_INFO,
835 MESH_PEER_DISCONNECTED MACSTR,
836 MAC2STR(sta->addr));
837
838 hapd->num_plinks--;
839
840 mesh_mpm_send_plink_action(wpa_s, sta,
841 PLINK_CLOSE, reason);
842 break;
843 case OPN_ACPT:
844 mesh_mpm_send_plink_action(wpa_s, sta,
845 PLINK_CONFIRM, 0);
846 break;
847 default:
848 break;
849 }
850 break;
851 case PLINK_HOLDING:
852 switch (event) {
853 case CLS_ACPT:
854 mesh_mpm_fsm_restart(wpa_s, sta);
855 break;
856 case OPN_ACPT:
857 case CNF_ACPT:
858 case OPN_RJCT:
859 case CNF_RJCT:
860 reason = sta->mpm_close_reason;
861 mesh_mpm_send_plink_action(wpa_s, sta,
862 PLINK_CLOSE, reason);
863 break;
864 default:
865 break;
866 }
867 break;
868 default:
869 wpa_msg(wpa_s, MSG_DEBUG,
870 "Unsupported MPM event %s for state %s",
871 mplevent[event], mplstate[sta->plink_state]);
872 break;
873 }
874}
875
876
877void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
878 const struct ieee80211_mgmt *mgmt, size_t len)
879{
880 u8 action_field;
881 struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
882 struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
883 struct sta_info *sta;
884 u16 plid = 0, llid = 0;
885 enum plink_event event;
886 struct ieee802_11_elems elems;
887 struct mesh_peer_mgmt_ie peer_mgmt_ie;
888 const u8 *ies;
889 size_t ie_len;
890 int ret;
891
892 if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
893 return;
894
895 action_field = mgmt->u.action.u.slf_prot_action.action;
896 if (action_field != PLINK_OPEN &&
897 action_field != PLINK_CONFIRM &&
898 action_field != PLINK_CLOSE)
899 return;
900
901 ies = mgmt->u.action.u.slf_prot_action.variable;
902 ie_len = (const u8 *) mgmt + len -
903 mgmt->u.action.u.slf_prot_action.variable;
904
905 /* at least expect mesh id and peering mgmt */
906 if (ie_len < 2 + 2) {
907 wpa_printf(MSG_DEBUG,
908 "MPM: Ignore too short action frame %u ie_len %u",
909 action_field, (unsigned int) ie_len);
910 return;
911 }
912 wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
913
914 if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
915 wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
916 WPA_GET_LE16(ies));
917 ies += 2; /* capability */
918 ie_len -= 2;
919 }
920 if (action_field == PLINK_CONFIRM) {
921 wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
922 ies += 2; /* aid */
923 ie_len -= 2;
924 }
925
926 /* check for mesh peering, mesh id and mesh config IEs */
927 if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
928 wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
929 return;
930 }
931 if (!elems.peer_mgmt) {
932 wpa_printf(MSG_DEBUG,
933 "MPM: No Mesh Peering Management element");
934 return;
935 }
936 if (action_field != PLINK_CLOSE) {
937 if (!elems.mesh_id || !elems.mesh_config) {
938 wpa_printf(MSG_DEBUG,
939 "MPM: No Mesh ID or Mesh Configuration element");
940 return;
941 }
942
943 if (!matches_local(wpa_s, &elems)) {
944 wpa_printf(MSG_DEBUG,
945 "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
946 return;
947 }
948 }
949
950 ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
951 elems.peer_mgmt,
952 elems.peer_mgmt_len,
953 &peer_mgmt_ie);
954 if (ret) {
955 wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
956 return;
957 }
958
959 /* the sender's llid is our plid and vice-versa */
960 plid = WPA_GET_LE16(peer_mgmt_ie.llid);
961 if (peer_mgmt_ie.plid)
962 llid = WPA_GET_LE16(peer_mgmt_ie.plid);
963 wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
964
965 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800966
967 /*
968 * If this is an open frame from an unknown STA, and this is an
969 * open mesh, then go ahead and add the peer before proceeding.
970 */
971 if (!sta && action_field == PLINK_OPEN &&
972 !(mconf->security & MESH_CONF_SEC_AMPE))
973 sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
974
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800975 if (!sta) {
976 wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
977 return;
978 }
979
980#ifdef CONFIG_SAE
981 /* peer is in sae_accepted? */
982 if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
983 wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
984 return;
985 }
986#endif /* CONFIG_SAE */
987
988 if (!sta->my_lid)
989 mesh_mpm_init_link(wpa_s, sta);
990
991 if ((mconf->security & MESH_CONF_SEC_AMPE) &&
992 mesh_rsn_process_ampe(wpa_s, sta, &elems,
993 &mgmt->u.action.category,
994 ies, ie_len)) {
995 wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
996 return;
997 }
998
999 if (sta->plink_state == PLINK_BLOCKED) {
1000 wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
1001 return;
1002 }
1003
1004 /* Now we will figure out the appropriate event... */
1005 switch (action_field) {
1006 case PLINK_OPEN:
1007 if (plink_free_count(hapd) == 0) {
1008 event = OPN_IGNR;
1009 wpa_printf(MSG_INFO,
1010 "MPM: Peer link num over quota(%d)",
1011 hapd->max_plinks);
1012 } else if (sta->peer_lid && sta->peer_lid != plid) {
1013 event = OPN_IGNR;
1014 } else {
1015 sta->peer_lid = plid;
1016 event = OPN_ACPT;
1017 }
1018 break;
1019 case PLINK_CONFIRM:
1020 if (plink_free_count(hapd) == 0) {
1021 event = CNF_IGNR;
1022 wpa_printf(MSG_INFO,
1023 "MPM: Peer link num over quota(%d)",
1024 hapd->max_plinks);
1025 } else if (sta->my_lid != llid ||
1026 (sta->peer_lid && sta->peer_lid != plid)) {
1027 event = CNF_IGNR;
1028 } else {
1029 if (!sta->peer_lid)
1030 sta->peer_lid = plid;
1031 event = CNF_ACPT;
1032 }
1033 break;
1034 case PLINK_CLOSE:
1035 if (sta->plink_state == PLINK_ESTAB)
1036 /* Do not check for llid or plid. This does not
1037 * follow the standard but since multiple plinks
1038 * per cand are not supported, it is necessary in
1039 * order to avoid a livelock when MP A sees an
1040 * establish peer link to MP B but MP B does not
1041 * see it. This can be caused by a timeout in
1042 * B's peer link establishment or B being
1043 * restarted.
1044 */
1045 event = CLS_ACPT;
1046 else if (sta->peer_lid != plid)
1047 event = CLS_IGNR;
1048 else if (peer_mgmt_ie.plid && sta->my_lid != llid)
1049 event = CLS_IGNR;
1050 else
1051 event = CLS_ACPT;
1052 break;
1053 default:
1054 /*
1055 * This cannot be hit due to the action_field check above, but
1056 * compilers may not be able to figure that out and can warn
1057 * about uninitialized event below.
1058 */
1059 return;
1060 }
1061 mesh_mpm_fsm(wpa_s, sta, event);
1062}
1063
1064
1065/* called by ap_free_sta */
1066void mesh_mpm_free_sta(struct sta_info *sta)
1067{
1068 eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
1069 eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
1070}