mesh: Add more debug information to MPM Action frame processing
[mech_eap.git] / wpa_supplicant / mesh_mpm.c
1 /*
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
22 struct 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
30 static void plink_timer(void *eloop_ctx, void *user_data);
31
32
33 enum 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
45 static 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
55 static 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
68 static 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
112 static 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
120 static 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 */
149 static 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 */
169 static 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 */
184 static 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;
196         sta->plink_state = PLINK_LISTEN;
197 }
198
199
200 static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
201                                        struct sta_info *sta,
202                                        enum plink_action_field type,
203                                        u16 close_reason)
204 {
205         struct wpabuf *buf;
206         struct hostapd_iface *ifmsh = wpa_s->ifmsh;
207         struct hostapd_data *bss = ifmsh->bss[0];
208         struct mesh_conf *conf = ifmsh->mconf;
209         u8 supp_rates[2 + 2 + 32];
210 #ifdef CONFIG_IEEE80211N
211         u8 ht_capa_oper[2 + 26 + 2 + 22];
212 #endif /* CONFIG_IEEE80211N */
213         u8 *pos, *cat;
214         u8 ie_len, add_plid = 0;
215         int ret;
216         int ampe = conf->security & MESH_CONF_SEC_AMPE;
217         size_t buf_len;
218
219         if (!sta)
220                 return;
221
222         buf_len = 2 +      /* capability info */
223                   2 +      /* AID */
224                   2 + 8 +  /* supported rates */
225                   2 + (32 - 8) +
226                   2 + 32 + /* mesh ID */
227                   2 + 7 +  /* mesh config */
228                   2 + 23 + /* peering management */
229                   2 + 96 + /* AMPE */
230                   2 + 16;  /* MIC */
231 #ifdef CONFIG_IEEE80211N
232         if (type != PLINK_CLOSE &&
233             wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
234                 buf_len += 2 + 26 + /* HT capabilities */
235                            2 + 22;  /* HT operation */
236         }
237 #endif /* CONFIG_IEEE80211N */
238         buf = wpabuf_alloc(buf_len);
239         if (!buf)
240                 return;
241
242         cat = wpabuf_mhead_u8(buf);
243         wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
244         wpabuf_put_u8(buf, type);
245
246         if (type != PLINK_CLOSE) {
247                 /* capability info */
248                 wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
249
250                 /* aid */
251                 if (type == PLINK_CONFIRM)
252                         wpabuf_put_le16(buf, sta->peer_lid);
253
254                 /* IE: supp + ext. supp rates */
255                 pos = hostapd_eid_supp_rates(bss, supp_rates);
256                 pos = hostapd_eid_ext_supp_rates(bss, pos);
257                 wpabuf_put_data(buf, supp_rates, pos - supp_rates);
258
259                 /* IE: Mesh ID */
260                 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
261                 wpabuf_put_u8(buf, conf->meshid_len);
262                 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
263
264                 /* IE: mesh conf */
265                 wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
266                 wpabuf_put_u8(buf, 7);
267                 wpabuf_put_u8(buf, conf->mesh_pp_id);
268                 wpabuf_put_u8(buf, conf->mesh_pm_id);
269                 wpabuf_put_u8(buf, conf->mesh_cc_id);
270                 wpabuf_put_u8(buf, conf->mesh_sp_id);
271                 wpabuf_put_u8(buf, conf->mesh_auth_id);
272                 /* TODO: formation info */
273                 wpabuf_put_u8(buf, 0);
274                 /* always forwarding & accepting plinks for now */
275                 wpabuf_put_u8(buf, 0x1 | 0x8);
276         } else {        /* Peer closing frame */
277                 /* IE: Mesh ID */
278                 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
279                 wpabuf_put_u8(buf, conf->meshid_len);
280                 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
281         }
282
283         /* IE: Mesh Peering Management element */
284         ie_len = 4;
285         if (ampe)
286                 ie_len += PMKID_LEN;
287         switch (type) {
288         case PLINK_OPEN:
289                 break;
290         case PLINK_CONFIRM:
291                 ie_len += 2;
292                 add_plid = 1;
293                 break;
294         case PLINK_CLOSE:
295                 ie_len += 2;
296                 add_plid = 1;
297                 ie_len += 2; /* reason code */
298                 break;
299         }
300
301         wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
302         wpabuf_put_u8(buf, ie_len);
303         /* peering protocol */
304         if (ampe)
305                 wpabuf_put_le16(buf, 1);
306         else
307                 wpabuf_put_le16(buf, 0);
308         wpabuf_put_le16(buf, sta->my_lid);
309         if (add_plid)
310                 wpabuf_put_le16(buf, sta->peer_lid);
311         if (type == PLINK_CLOSE)
312                 wpabuf_put_le16(buf, close_reason);
313         if (ampe)
314                 mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
315                                    wpabuf_put(buf, PMKID_LEN));
316
317 #ifdef CONFIG_IEEE80211N
318         if (type != PLINK_CLOSE &&
319             wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
320                 pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
321                 pos = hostapd_eid_ht_operation(bss, pos);
322                 wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
323         }
324 #endif /* CONFIG_IEEE80211N */
325
326         if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
327                 wpa_msg(wpa_s, MSG_INFO,
328                         "Mesh MPM: failed to add AMPE and MIC IE");
329                 goto fail;
330         }
331
332         ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
333                                   sta->addr, wpa_s->own_addr, wpa_s->own_addr,
334                                   wpabuf_head(buf), wpabuf_len(buf), 0);
335         if (ret < 0)
336                 wpa_msg(wpa_s, MSG_INFO,
337                         "Mesh MPM: failed to send peering frame");
338
339 fail:
340         wpabuf_free(buf);
341 }
342
343
344 /* configure peering state in ours and driver's station entry */
345 static void
346 wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s, struct sta_info *sta,
347                          enum mesh_plink_state state)
348 {
349         struct hostapd_sta_add_params params;
350         int ret;
351
352         sta->plink_state = state;
353
354         os_memset(&params, 0, sizeof(params));
355         params.addr = sta->addr;
356         params.plink_state = state;
357         params.set = 1;
358
359         wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
360                 MAC2STR(sta->addr), mplstate[state]);
361         ret = wpa_drv_sta_add(wpa_s, &params);
362         if (ret) {
363                 wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
364                         ": %d", MAC2STR(sta->addr), ret);
365         }
366 }
367
368
369 static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
370                                  struct sta_info *sta)
371 {
372         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
373
374         eloop_cancel_timeout(plink_timer, wpa_s, sta);
375
376         if (sta->mpm_close_reason == WLAN_REASON_MESH_CLOSE_RCVD) {
377                 ap_free_sta(hapd, sta);
378                 return;
379         }
380
381         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_LISTEN);
382         sta->my_lid = sta->peer_lid = sta->mpm_close_reason = 0;
383         sta->mpm_retries = 0;
384 }
385
386
387 static void plink_timer(void *eloop_ctx, void *user_data)
388 {
389         struct wpa_supplicant *wpa_s = eloop_ctx;
390         struct sta_info *sta = user_data;
391         u16 reason = 0;
392         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
393
394         switch (sta->plink_state) {
395         case PLINK_OPEN_RCVD:
396         case PLINK_OPEN_SENT:
397                 /* retry timer */
398                 if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
399                         eloop_register_timeout(
400                                 conf->dot11MeshRetryTimeout / 1000,
401                                 (conf->dot11MeshRetryTimeout % 1000) * 1000,
402                                 plink_timer, wpa_s, sta);
403                         mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
404                         sta->mpm_retries++;
405                         break;
406                 }
407                 reason = WLAN_REASON_MESH_MAX_RETRIES;
408                 /* fall through on else */
409
410         case PLINK_CNF_RCVD:
411                 /* confirm timer */
412                 if (!reason)
413                         reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
414                 sta->plink_state = PLINK_HOLDING;
415                 eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
416                         (conf->dot11MeshHoldingTimeout % 1000) * 1000,
417                         plink_timer, wpa_s, sta);
418                 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
419                 break;
420         case PLINK_HOLDING:
421                 /* holding timer */
422                 mesh_mpm_fsm_restart(wpa_s, sta);
423                 break;
424         default:
425                 break;
426         }
427 }
428
429
430 /* initiate peering with station */
431 static void
432 mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
433                     enum mesh_plink_state next_state)
434 {
435         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
436
437         eloop_cancel_timeout(plink_timer, wpa_s, sta);
438         eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
439                                (conf->dot11MeshRetryTimeout % 1000) * 1000,
440                                plink_timer, wpa_s, sta);
441         mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
442         wpa_mesh_set_plink_state(wpa_s, sta, next_state);
443 }
444
445
446 int mesh_mpm_plink_close(struct hostapd_data *hapd,
447                          struct sta_info *sta, void *ctx)
448 {
449         struct wpa_supplicant *wpa_s = ctx;
450         int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
451
452         if (sta) {
453                 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
454                 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
455                 wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
456                            MAC2STR(sta->addr));
457                 eloop_cancel_timeout(plink_timer, wpa_s, sta);
458                 return 0;
459         }
460
461         return 1;
462 }
463
464
465 void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
466 {
467         struct hostapd_data *hapd = ifmsh->bss[0];
468
469         /* notify peers we're leaving */
470         ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
471
472         hapd->num_plinks = 0;
473         hostapd_free_stas(hapd);
474 }
475
476
477 /* for mesh_rsn to indicate this peer has completed authentication, and we're
478  * ready to start AMPE */
479 void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
480 {
481         struct hostapd_data *data = wpa_s->ifmsh->bss[0];
482         struct hostapd_sta_add_params params;
483         struct sta_info *sta;
484         int ret;
485
486         sta = ap_get_sta(data, addr);
487         if (!sta) {
488                 wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
489                 return;
490         }
491
492         /* TODO: Should do nothing if this STA is already authenticated, but
493          * the AP code already sets this flag. */
494         sta->flags |= WLAN_STA_AUTH;
495
496         mesh_rsn_init_ampe_sta(wpa_s, sta);
497
498         os_memset(&params, 0, sizeof(params));
499         params.addr = sta->addr;
500         params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
501         params.set = 1;
502
503         wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
504                 MAC2STR(sta->addr));
505         ret = wpa_drv_sta_add(wpa_s, &params);
506         if (ret) {
507                 wpa_msg(wpa_s, MSG_ERROR,
508                         "Driver failed to set " MACSTR ": %d",
509                         MAC2STR(sta->addr), ret);
510         }
511
512         if (!sta->my_lid)
513                 mesh_mpm_init_link(wpa_s, sta);
514
515         mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
516 }
517
518
519 void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
520                             struct ieee802_11_elems *elems)
521 {
522         struct hostapd_sta_add_params params;
523         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
524         struct hostapd_data *data = wpa_s->ifmsh->bss[0];
525         struct sta_info *sta;
526         struct wpa_ssid *ssid = wpa_s->current_ssid;
527         int ret = 0;
528
529         sta = ap_get_sta(data, addr);
530         if (!sta) {
531                 sta = ap_sta_add(data, addr);
532                 if (!sta)
533                         return;
534         }
535
536         /* initialize sta */
537         if (copy_supp_rates(wpa_s, sta, elems))
538                 return;
539
540         mesh_mpm_init_link(wpa_s, sta);
541
542 #ifdef CONFIG_IEEE80211N
543         copy_sta_ht_capab(data, sta, elems->ht_capabilities,
544                         elems->ht_capabilities_len);
545         update_ht_state(data, sta);
546 #endif /* CONFIG_IEEE80211N */
547
548         /* insert into driver */
549         os_memset(&params, 0, sizeof(params));
550         params.supp_rates = sta->supported_rates;
551         params.supp_rates_len = sta->supported_rates_len;
552         params.addr = addr;
553         params.plink_state = sta->plink_state;
554         params.aid = sta->peer_lid;
555         params.listen_interval = 100;
556         params.ht_capabilities = sta->ht_capabilities;
557         params.flags |= WPA_STA_WMM;
558         params.flags_mask |= WPA_STA_AUTHENTICATED;
559         if (conf->security == MESH_CONF_SEC_NONE) {
560                 params.flags |= WPA_STA_AUTHORIZED;
561                 params.flags |= WPA_STA_AUTHENTICATED;
562         } else {
563                 sta->flags |= WLAN_STA_MFP;
564                 params.flags |= WPA_STA_MFP;
565         }
566
567         ret = wpa_drv_sta_add(wpa_s, &params);
568         if (ret) {
569                 wpa_msg(wpa_s, MSG_ERROR,
570                         "Driver failed to insert " MACSTR ": %d",
571                         MAC2STR(addr), ret);
572                 return;
573         }
574
575         if (ssid && ssid->no_auto_peer) {
576                 wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
577                         MACSTR " because of no_auto_peer", MAC2STR(addr));
578                 return;
579         }
580
581         if (conf->security == MESH_CONF_SEC_NONE)
582                 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
583         else
584                 mesh_rsn_auth_sae_sta(wpa_s, sta);
585 }
586
587
588 void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
589 {
590         struct hostapd_frame_info fi;
591
592         os_memset(&fi, 0, sizeof(fi));
593         fi.datarate = rx_mgmt->datarate;
594         fi.ssi_signal = rx_mgmt->ssi_signal;
595         ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
596                         rx_mgmt->frame_len, &fi);
597 }
598
599
600 static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
601                                  struct sta_info *sta)
602 {
603         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
604         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
605         u8 seq[6] = {};
606
607         wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
608                 MAC2STR(sta->addr));
609
610         if (conf->security & MESH_CONF_SEC_AMPE) {
611                 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
612                                 seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
613                 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
614                                 seq, sizeof(seq),
615                                 sta->mgtk, sizeof(sta->mgtk));
616                 wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
617                                 seq, sizeof(seq),
618                                 sta->mgtk, sizeof(sta->mgtk));
619
620                 wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
621                 wpa_hexdump_key(MSG_DEBUG, "mgtk:",
622                                 sta->mgtk, sizeof(sta->mgtk));
623         }
624
625         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
626         hapd->num_plinks++;
627
628         sta->flags |= WLAN_STA_ASSOC;
629
630         eloop_cancel_timeout(plink_timer, wpa_s, sta);
631
632         /* Send ctrl event */
633         wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
634                      MAC2STR(sta->addr));
635 }
636
637
638 static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
639                          enum plink_event event)
640 {
641         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
642         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
643         u16 reason = 0;
644
645         wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
646                 MAC2STR(sta->addr), mplstate[sta->plink_state],
647                 mplevent[event]);
648
649         switch (sta->plink_state) {
650         case PLINK_LISTEN:
651                 switch (event) {
652                 case CLS_ACPT:
653                         mesh_mpm_fsm_restart(wpa_s, sta);
654                         break;
655                 case OPN_ACPT:
656                         mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
657                         mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
658                                                    0);
659                         break;
660                 default:
661                         break;
662                 }
663                 break;
664         case PLINK_OPEN_SENT:
665                 switch (event) {
666                 case OPN_RJCT:
667                 case CNF_RJCT:
668                         reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
669                         /* fall-through */
670                 case CLS_ACPT:
671                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
672                         if (!reason)
673                                 reason = WLAN_REASON_MESH_CLOSE_RCVD;
674                         eloop_register_timeout(
675                                 conf->dot11MeshHoldingTimeout / 1000,
676                                 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
677                                 plink_timer, wpa_s, sta);
678                         mesh_mpm_send_plink_action(wpa_s, sta,
679                                                    PLINK_CLOSE, reason);
680                         break;
681                 case OPN_ACPT:
682                         /* retry timer is left untouched */
683                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
684                         mesh_mpm_send_plink_action(wpa_s, sta,
685                                                    PLINK_CONFIRM, 0);
686                         break;
687                 case CNF_ACPT:
688                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
689                         eloop_register_timeout(
690                                 conf->dot11MeshConfirmTimeout / 1000,
691                                 (conf->dot11MeshConfirmTimeout % 1000) * 1000,
692                                 plink_timer, wpa_s, sta);
693                         break;
694                 default:
695                         break;
696                 }
697                 break;
698         case PLINK_OPEN_RCVD:
699                 switch (event) {
700                 case OPN_RJCT:
701                 case CNF_RJCT:
702                         reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
703                         /* fall-through */
704                 case CLS_ACPT:
705                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
706                         if (!reason)
707                                 reason = WLAN_REASON_MESH_CLOSE_RCVD;
708                         eloop_register_timeout(
709                                 conf->dot11MeshHoldingTimeout / 1000,
710                                 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
711                                 plink_timer, wpa_s, sta);
712                         sta->mpm_close_reason = reason;
713                         mesh_mpm_send_plink_action(wpa_s, sta,
714                                                    PLINK_CLOSE, reason);
715                         break;
716                 case OPN_ACPT:
717                         mesh_mpm_send_plink_action(wpa_s, sta,
718                                                    PLINK_CONFIRM, 0);
719                         break;
720                 case CNF_ACPT:
721                         if (conf->security & MESH_CONF_SEC_AMPE)
722                                 mesh_rsn_derive_mtk(wpa_s, sta);
723                         mesh_mpm_plink_estab(wpa_s, sta);
724                         break;
725                 default:
726                         break;
727                 }
728                 break;
729         case PLINK_CNF_RCVD:
730                 switch (event) {
731                 case OPN_RJCT:
732                 case CNF_RJCT:
733                         reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
734                         /* fall-through */
735                 case CLS_ACPT:
736                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
737                         if (!reason)
738                                 reason = WLAN_REASON_MESH_CLOSE_RCVD;
739                         eloop_register_timeout(
740                                 conf->dot11MeshHoldingTimeout / 1000,
741                                 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
742                                 plink_timer, wpa_s, sta);
743                         sta->mpm_close_reason = reason;
744                         mesh_mpm_send_plink_action(wpa_s, sta,
745                                                    PLINK_CLOSE, reason);
746                         break;
747                 case OPN_ACPT:
748                         mesh_mpm_plink_estab(wpa_s, sta);
749                         mesh_mpm_send_plink_action(wpa_s, sta,
750                                                    PLINK_CONFIRM, 0);
751                         break;
752                 default:
753                         break;
754                 }
755                 break;
756         case PLINK_ESTAB:
757                 switch (event) {
758                 case CLS_ACPT:
759                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
760                         reason = WLAN_REASON_MESH_CLOSE_RCVD;
761
762                         eloop_register_timeout(
763                                 conf->dot11MeshHoldingTimeout / 1000,
764                                 (conf->dot11MeshHoldingTimeout % 1000) * 1000,
765                                 plink_timer, wpa_s, sta);
766                         sta->mpm_close_reason = reason;
767
768                         wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
769                                 " closed with reason %d",
770                                 MAC2STR(sta->addr), reason);
771
772                         wpa_msg_ctrl(wpa_s, MSG_INFO,
773                                      MESH_PEER_DISCONNECTED MACSTR,
774                                      MAC2STR(sta->addr));
775
776                         hapd->num_plinks--;
777
778                         mesh_mpm_send_plink_action(wpa_s, sta,
779                                                    PLINK_CLOSE, reason);
780                         break;
781                 case OPN_ACPT:
782                         mesh_mpm_send_plink_action(wpa_s, sta,
783                                                    PLINK_CONFIRM, 0);
784                         break;
785                 default:
786                         break;
787                 }
788                 break;
789         case PLINK_HOLDING:
790                 switch (event) {
791                 case CLS_ACPT:
792                         mesh_mpm_fsm_restart(wpa_s, sta);
793                         break;
794                 case OPN_ACPT:
795                 case CNF_ACPT:
796                 case OPN_RJCT:
797                 case CNF_RJCT:
798                         reason = sta->mpm_close_reason;
799                         mesh_mpm_send_plink_action(wpa_s, sta,
800                                                    PLINK_CLOSE, reason);
801                         break;
802                 default:
803                         break;
804                 }
805                 break;
806         default:
807                 wpa_msg(wpa_s, MSG_DEBUG,
808                         "Unsupported MPM event %s for state %s",
809                         mplevent[event], mplstate[sta->plink_state]);
810                 break;
811         }
812 }
813
814
815 void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
816                         const struct ieee80211_mgmt *mgmt, size_t len)
817 {
818         u8 action_field;
819         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
820         struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
821         struct sta_info *sta;
822         u16 plid = 0, llid = 0;
823         enum plink_event event;
824         struct ieee802_11_elems elems;
825         struct mesh_peer_mgmt_ie peer_mgmt_ie;
826         const u8 *ies;
827         size_t ie_len;
828         int ret;
829
830         if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
831                 return;
832
833         action_field = mgmt->u.action.u.slf_prot_action.action;
834         if (action_field != PLINK_OPEN &&
835             action_field != PLINK_CONFIRM &&
836             action_field != PLINK_CLOSE)
837                 return;
838
839         ies = mgmt->u.action.u.slf_prot_action.variable;
840         ie_len = (const u8 *) mgmt + len -
841                 mgmt->u.action.u.slf_prot_action.variable;
842
843         /* at least expect mesh id and peering mgmt */
844         if (ie_len < 2 + 2) {
845                 wpa_printf(MSG_DEBUG,
846                            "MPM: Ignore too short action frame %u ie_len %u",
847                            action_field, (unsigned int) ie_len);
848                 return;
849         }
850         wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
851
852         if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
853                 wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
854                            WPA_GET_LE16(ies));
855                 ies += 2;       /* capability */
856                 ie_len -= 2;
857         }
858         if (action_field == PLINK_CONFIRM) {
859                 wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
860                 ies += 2;       /* aid */
861                 ie_len -= 2;
862         }
863
864         /* check for mesh peering, mesh id and mesh config IEs */
865         if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
866                 wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
867                 return;
868         }
869         if (!elems.peer_mgmt) {
870                 wpa_printf(MSG_DEBUG,
871                            "MPM: No Mesh Peering Management element");
872                 return;
873         }
874         if (action_field != PLINK_CLOSE) {
875                 if (!elems.mesh_id || !elems.mesh_config) {
876                         wpa_printf(MSG_DEBUG,
877                                    "MPM: No Mesh ID or Mesh Configuration element");
878                         return;
879                 }
880
881                 if (!matches_local(wpa_s, &elems)) {
882                         wpa_printf(MSG_DEBUG,
883                                    "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
884                         return;
885                 }
886         }
887
888         ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
889                                        elems.peer_mgmt,
890                                        elems.peer_mgmt_len,
891                                        &peer_mgmt_ie);
892         if (ret) {
893                 wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
894                 return;
895         }
896
897         /* the sender's llid is our plid and vice-versa */
898         plid = WPA_GET_LE16(peer_mgmt_ie.llid);
899         if (peer_mgmt_ie.plid)
900                 llid = WPA_GET_LE16(peer_mgmt_ie.plid);
901         wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
902
903         sta = ap_get_sta(hapd, mgmt->sa);
904         if (!sta) {
905                 wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
906                 return;
907         }
908
909 #ifdef CONFIG_SAE
910         /* peer is in sae_accepted? */
911         if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
912                 wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
913                 return;
914         }
915 #endif /* CONFIG_SAE */
916
917         if (!sta->my_lid)
918                 mesh_mpm_init_link(wpa_s, sta);
919
920         if ((mconf->security & MESH_CONF_SEC_AMPE) &&
921             mesh_rsn_process_ampe(wpa_s, sta, &elems,
922                                   &mgmt->u.action.category,
923                                   ies, ie_len)) {
924                 wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
925                 return;
926         }
927
928         if (sta->plink_state == PLINK_BLOCKED) {
929                 wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
930                 return;
931         }
932
933         /* Now we will figure out the appropriate event... */
934         switch (action_field) {
935         case PLINK_OPEN:
936                 if (!plink_free_count(hapd) ||
937                     (sta->peer_lid && sta->peer_lid != plid)) {
938                         event = OPN_IGNR;
939                 } else {
940                         sta->peer_lid = plid;
941                         event = OPN_ACPT;
942                 }
943                 break;
944         case PLINK_CONFIRM:
945                 if (!plink_free_count(hapd) ||
946                     sta->my_lid != llid ||
947                     (sta->peer_lid && sta->peer_lid != plid)) {
948                         event = CNF_IGNR;
949                 } else {
950                         if (!sta->peer_lid)
951                                 sta->peer_lid = plid;
952                         event = CNF_ACPT;
953                 }
954                 break;
955         case PLINK_CLOSE:
956                 if (sta->plink_state == PLINK_ESTAB)
957                         /* Do not check for llid or plid. This does not
958                          * follow the standard but since multiple plinks
959                          * per cand are not supported, it is necessary in
960                          * order to avoid a livelock when MP A sees an
961                          * establish peer link to MP B but MP B does not
962                          * see it. This can be caused by a timeout in
963                          * B's peer link establishment or B being
964                          * restarted.
965                          */
966                         event = CLS_ACPT;
967                 else if (sta->peer_lid != plid)
968                         event = CLS_IGNR;
969                 else if (peer_mgmt_ie.plid && sta->my_lid != llid)
970                         event = CLS_IGNR;
971                 else
972                         event = CLS_ACPT;
973                 break;
974         }
975         mesh_mpm_fsm(wpa_s, sta, event);
976 }
977
978
979 /* called by ap_free_sta */
980 void mesh_mpm_free_sta(struct sta_info *sta)
981 {
982         eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
983         eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
984 }