mesh: Enable mesh HT mode
[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 /* TODO make configurable */
23 #define dot11MeshMaxRetries 10
24 #define dot11MeshRetryTimeout 1
25 #define dot11MeshConfirmTimeout 1
26 #define dot11MeshHoldingTimeout 1
27
28 struct mesh_peer_mgmt_ie {
29         const u8 *proto_id;
30         const u8 *llid;
31         const u8 *plid;
32         const u8 *reason;
33         const u8 *pmk;
34 };
35
36 static void plink_timer(void *eloop_ctx, void *user_data);
37
38
39 enum plink_event {
40         PLINK_UNDEFINED,
41         OPN_ACPT,
42         OPN_RJCT,
43         OPN_IGNR,
44         CNF_ACPT,
45         CNF_RJCT,
46         CNF_IGNR,
47         CLS_ACPT,
48         CLS_IGNR
49 };
50
51 static const char * const mplstate[] = {
52         [PLINK_LISTEN] = "LISTEN",
53         [PLINK_OPEN_SENT] = "OPEN_SENT",
54         [PLINK_OPEN_RCVD] = "OPEN_RCVD",
55         [PLINK_CNF_RCVD] = "CNF_RCVD",
56         [PLINK_ESTAB] = "ESTAB",
57         [PLINK_HOLDING] = "HOLDING",
58         [PLINK_BLOCKED] = "BLOCKED"
59 };
60
61 static const char * const mplevent[] = {
62         [PLINK_UNDEFINED] = "UNDEFINED",
63         [OPN_ACPT] = "OPN_ACPT",
64         [OPN_RJCT] = "OPN_RJCT",
65         [OPN_IGNR] = "OPN_IGNR",
66         [CNF_ACPT] = "CNF_ACPT",
67         [CNF_RJCT] = "CNF_RJCT",
68         [CNF_IGNR] = "CNF_IGNR",
69         [CLS_ACPT] = "CLS_ACPT",
70         [CLS_IGNR] = "CLS_IGNR"
71 };
72
73
74 static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
75                                     u8 action_field,
76                                     const u8 *ie, size_t len,
77                                     struct mesh_peer_mgmt_ie *mpm_ie)
78 {
79         os_memset(mpm_ie, 0, sizeof(*mpm_ie));
80
81         /* remove optional PMK at end */
82         if (len >= 16) {
83                 len -= 16;
84                 mpm_ie->pmk = ie + len - 16;
85         }
86
87         if ((action_field == PLINK_OPEN && len != 4) ||
88             (action_field == PLINK_CONFIRM && len != 6) ||
89             (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
90                 wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
91                 return -1;
92         }
93
94         /* required fields */
95         if (len < 4)
96                 return -1;
97         mpm_ie->proto_id = ie;
98         mpm_ie->llid = ie + 2;
99         ie += 4;
100         len -= 4;
101
102         /* close reason is always present at end for close */
103         if (action_field == PLINK_CLOSE) {
104                 if (len < 2)
105                         return -1;
106                 mpm_ie->reason = ie + len - 2;
107                 len -= 2;
108         }
109
110         /* plid, present for confirm, and possibly close */
111         if (len)
112                 mpm_ie->plid = ie;
113
114         return 0;
115 }
116
117
118 static int plink_free_count(struct hostapd_data *hapd)
119 {
120         if (hapd->max_plinks > hapd->num_plinks)
121                 return hapd->max_plinks - hapd->num_plinks;
122         return 0;
123 }
124
125
126 static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
127                            struct sta_info *sta,
128                            struct ieee802_11_elems *elems)
129 {
130         if (!elems->supp_rates) {
131                 wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
132                         MAC2STR(sta->addr));
133                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
134         }
135
136         if (elems->supp_rates_len + elems->ext_supp_rates_len >
137             sizeof(sta->supported_rates)) {
138                 wpa_msg(wpa_s, MSG_ERROR,
139                         "Invalid supported rates element length " MACSTR
140                         " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
141                         elems->ext_supp_rates_len);
142                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
143         }
144
145         sta->supported_rates_len = merge_byte_arrays(
146                 sta->supported_rates, sizeof(sta->supported_rates),
147                 elems->supp_rates, elems->supp_rates_len,
148                 elems->ext_supp_rates, elems->ext_supp_rates_len);
149
150         return WLAN_STATUS_SUCCESS;
151 }
152
153
154 /* return true if elems from a neighbor match this MBSS */
155 static Boolean matches_local(struct wpa_supplicant *wpa_s,
156                              struct ieee802_11_elems *elems)
157 {
158         struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
159
160         if (elems->mesh_config_len < 5)
161                 return FALSE;
162
163         return (mconf->meshid_len == elems->mesh_id_len &&
164                 os_memcmp(mconf->meshid, elems->mesh_id,
165                           elems->mesh_id_len) == 0 &&
166                 mconf->mesh_pp_id == elems->mesh_config[0] &&
167                 mconf->mesh_pm_id == elems->mesh_config[1] &&
168                 mconf->mesh_cc_id == elems->mesh_config[2] &&
169                 mconf->mesh_sp_id == elems->mesh_config[3] &&
170                 mconf->mesh_auth_id == elems->mesh_config[4]);
171 }
172
173
174 /* check if local link id is already used with another peer */
175 static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
176 {
177         struct sta_info *sta;
178         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
179
180         for (sta = hapd->sta_list; sta; sta = sta->next) {
181                 if (sta->my_lid == llid)
182                         return TRUE;
183         }
184
185         return FALSE;
186 }
187
188
189 /* generate an llid for a link and set to initial state */
190 static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
191                                struct sta_info *sta)
192 {
193         u16 llid;
194
195         do {
196                 if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
197                         continue;
198         } while (!llid || llid_in_use(wpa_s, llid));
199
200         sta->my_lid = llid;
201         sta->peer_lid = 0;
202         sta->plink_state = PLINK_LISTEN;
203 }
204
205
206 static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
207                                        struct sta_info *sta,
208                                        enum plink_action_field type,
209                                        u16 close_reason)
210 {
211         struct wpabuf *buf;
212         struct hostapd_iface *ifmsh = wpa_s->ifmsh;
213         struct hostapd_data *bss = ifmsh->bss[0];
214         struct mesh_conf *conf = ifmsh->mconf;
215         u8 supp_rates[2 + 2 + 32];
216 #ifdef CONFIG_IEEE80211N
217         u8 ht_capa_oper[2 + 26 + 2 + 22];
218 #endif /* CONFIG_IEEE80211N */
219         u8 *pos, *cat;
220         u8 ie_len, add_plid = 0;
221         int ret;
222         int ampe = conf->security & MESH_CONF_SEC_AMPE;
223
224         if (!sta)
225                 return;
226
227         buf = wpabuf_alloc(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 + 26 + /* HT capabilities */
234                            2 + 22 + /* HT operation */
235                            2 + 23 + /* peering management */
236                            2 + 96 + /* AMPE */
237                            2 + 16); /* MIC */
238         if (!buf)
239                 return;
240
241         cat = wpabuf_mhead_u8(buf);
242         wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
243         wpabuf_put_u8(buf, type);
244
245         if (type != PLINK_CLOSE) {
246                 /* capability info */
247                 wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
248
249                 /* aid */
250                 if (type == PLINK_CONFIRM)
251                         wpabuf_put_le16(buf, sta->peer_lid);
252
253                 /* IE: supp + ext. supp rates */
254                 pos = hostapd_eid_supp_rates(bss, supp_rates);
255                 pos = hostapd_eid_ext_supp_rates(bss, pos);
256                 wpabuf_put_data(buf, supp_rates, pos - supp_rates);
257
258                 /* IE: Mesh ID */
259                 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
260                 wpabuf_put_u8(buf, conf->meshid_len);
261                 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
262
263                 /* IE: mesh conf */
264                 wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
265                 wpabuf_put_u8(buf, 7);
266                 wpabuf_put_u8(buf, conf->mesh_pp_id);
267                 wpabuf_put_u8(buf, conf->mesh_pm_id);
268                 wpabuf_put_u8(buf, conf->mesh_cc_id);
269                 wpabuf_put_u8(buf, conf->mesh_sp_id);
270                 wpabuf_put_u8(buf, conf->mesh_auth_id);
271                 /* TODO: formation info */
272                 wpabuf_put_u8(buf, 0);
273                 /* always forwarding & accepting plinks for now */
274                 wpabuf_put_u8(buf, 0x1 | 0x8);
275         } else {        /* Peer closing frame */
276                 /* IE: Mesh ID */
277                 wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
278                 wpabuf_put_u8(buf, conf->meshid_len);
279                 wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
280         }
281
282         /* IE: Mesh Peering Management element */
283         ie_len = 4;
284         if (ampe)
285                 ie_len += PMKID_LEN;
286         switch (type) {
287         case PLINK_OPEN:
288                 break;
289         case PLINK_CONFIRM:
290                 ie_len += 2;
291                 add_plid = 1;
292                 break;
293         case PLINK_CLOSE:
294                 ie_len += 2;
295                 add_plid = 1;
296                 ie_len += 2; /* reason code */
297                 break;
298         }
299
300         wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
301         wpabuf_put_u8(buf, ie_len);
302         /* peering protocol */
303         if (ampe)
304                 wpabuf_put_le16(buf, 1);
305         else
306                 wpabuf_put_le16(buf, 0);
307         wpabuf_put_le16(buf, sta->my_lid);
308         if (add_plid)
309                 wpabuf_put_le16(buf, sta->peer_lid);
310         if (type == PLINK_CLOSE)
311                 wpabuf_put_le16(buf, close_reason);
312         if (ampe)
313                 mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
314                                    wpabuf_put(buf, PMKID_LEN));
315
316 #ifdef CONFIG_IEEE80211N
317         if (type != PLINK_CLOSE &&
318             wpa_s->current_ssid->mesh_ht_mode != CHAN_NO_HT) {
319                 pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
320                 pos = hostapd_eid_ht_operation(bss, pos);
321                 wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
322         }
323 #endif /* CONFIG_IEEE80211N */
324
325         if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
326                 wpa_msg(wpa_s, MSG_INFO,
327                         "Mesh MPM: failed to add AMPE and MIC IE");
328                 goto fail;
329         }
330
331         ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
332                                   sta->addr, wpa_s->own_addr, wpa_s->own_addr,
333                                   wpabuf_head(buf), wpabuf_len(buf), 0);
334         if (ret < 0)
335                 wpa_msg(wpa_s, MSG_INFO,
336                         "Mesh MPM: failed to send peering frame");
337
338 fail:
339         wpabuf_free(buf);
340 }
341
342
343 /* configure peering state in ours and driver's station entry */
344 static void
345 wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s, struct sta_info *sta,
346                          enum mesh_plink_state state)
347 {
348         struct hostapd_sta_add_params params;
349         int ret;
350
351         sta->plink_state = state;
352
353         os_memset(&params, 0, sizeof(params));
354         params.addr = sta->addr;
355         params.plink_state = state;
356         params.set = 1;
357
358         wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
359                 MAC2STR(sta->addr), mplstate[state]);
360         ret = wpa_drv_sta_add(wpa_s, &params);
361         if (ret) {
362                 wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
363                         ": %d", MAC2STR(sta->addr), ret);
364         }
365 }
366
367
368 static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
369                                  struct sta_info *sta)
370 {
371         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
372
373         eloop_cancel_timeout(plink_timer, wpa_s, sta);
374
375         if (sta->mpm_close_reason == WLAN_REASON_MESH_CLOSE_RCVD) {
376                 ap_free_sta(hapd, sta);
377                 return;
378         }
379
380         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_LISTEN);
381         sta->my_lid = sta->peer_lid = sta->mpm_close_reason = 0;
382         sta->mpm_retries = 0;
383 }
384
385
386 static void plink_timer(void *eloop_ctx, void *user_data)
387 {
388         struct wpa_supplicant *wpa_s = eloop_ctx;
389         struct sta_info *sta = user_data;
390         u16 reason = 0;
391
392         switch (sta->plink_state) {
393         case PLINK_OPEN_RCVD:
394         case PLINK_OPEN_SENT:
395                 /* retry timer */
396                 if (sta->mpm_retries < dot11MeshMaxRetries) {
397                         eloop_register_timeout(dot11MeshRetryTimeout, 0,
398                                                plink_timer, wpa_s, sta);
399                         mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
400                         sta->mpm_retries++;
401                         break;
402                 }
403                 reason = WLAN_REASON_MESH_MAX_RETRIES;
404                 /* fall through on else */
405
406         case PLINK_CNF_RCVD:
407                 /* confirm timer */
408                 if (!reason)
409                         reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
410                 sta->plink_state = PLINK_HOLDING;
411                 eloop_register_timeout(dot11MeshHoldingTimeout, 0,
412                                        plink_timer, wpa_s, sta);
413                 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
414                 break;
415         case PLINK_HOLDING:
416                 /* holding timer */
417                 mesh_mpm_fsm_restart(wpa_s, sta);
418                 break;
419         default:
420                 break;
421         }
422 }
423
424
425 /* initiate peering with station */
426 static void
427 mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
428                     enum mesh_plink_state next_state)
429 {
430         eloop_cancel_timeout(plink_timer, wpa_s, sta);
431         eloop_register_timeout(dot11MeshRetryTimeout, 0, plink_timer,
432                                wpa_s, sta);
433         mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
434         wpa_mesh_set_plink_state(wpa_s, sta, next_state);
435 }
436
437
438 int mesh_mpm_plink_close(struct hostapd_data *hapd,
439                          struct sta_info *sta, void *ctx)
440 {
441         struct wpa_supplicant *wpa_s = ctx;
442         int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
443
444         if (sta) {
445                 wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
446                 mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
447                 wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
448                            MAC2STR(sta->addr));
449                 eloop_cancel_timeout(plink_timer, wpa_s, sta);
450                 return 0;
451         }
452
453         return 1;
454 }
455
456
457 void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
458 {
459         struct hostapd_data *hapd = ifmsh->bss[0];
460
461         /* notify peers we're leaving */
462         ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
463
464         hapd->num_plinks = 0;
465         hostapd_free_stas(hapd);
466 }
467
468
469 /* for mesh_rsn to indicate this peer has completed authentication, and we're
470  * ready to start AMPE */
471 void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
472 {
473         struct hostapd_data *data = wpa_s->ifmsh->bss[0];
474         struct hostapd_sta_add_params params;
475         struct sta_info *sta;
476         int ret;
477
478         sta = ap_get_sta(data, addr);
479         if (!sta) {
480                 wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
481                 return;
482         }
483
484         /* TODO: Should do nothing if this STA is already authenticated, but
485          * the AP code already sets this flag. */
486         sta->flags |= WLAN_STA_AUTH;
487
488         mesh_rsn_init_ampe_sta(wpa_s, sta);
489
490         os_memset(&params, 0, sizeof(params));
491         params.addr = sta->addr;
492         params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
493         params.set = 1;
494
495         wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
496                 MAC2STR(sta->addr));
497         ret = wpa_drv_sta_add(wpa_s, &params);
498         if (ret) {
499                 wpa_msg(wpa_s, MSG_ERROR,
500                         "Driver failed to set " MACSTR ": %d",
501                         MAC2STR(sta->addr), ret);
502         }
503
504         if (!sta->my_lid)
505                 mesh_mpm_init_link(wpa_s, sta);
506
507         mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
508 }
509
510
511 void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
512                             struct ieee802_11_elems *elems)
513 {
514         struct hostapd_sta_add_params params;
515         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
516         struct hostapd_data *data = wpa_s->ifmsh->bss[0];
517         struct sta_info *sta;
518         struct wpa_ssid *ssid = wpa_s->current_ssid;
519         int ret = 0;
520
521         sta = ap_get_sta(data, addr);
522         if (!sta) {
523                 sta = ap_sta_add(data, addr);
524                 if (!sta)
525                         return;
526         }
527
528         /* initialize sta */
529         if (copy_supp_rates(wpa_s, sta, elems))
530                 return;
531
532         mesh_mpm_init_link(wpa_s, sta);
533
534 #ifdef CONFIG_IEEE80211N
535         copy_sta_ht_capab(data, sta, elems->ht_capabilities,
536                         elems->ht_capabilities_len);
537         update_ht_state(data, sta);
538 #endif /* CONFIG_IEEE80211N */
539
540         /* insert into driver */
541         os_memset(&params, 0, sizeof(params));
542         params.supp_rates = sta->supported_rates;
543         params.supp_rates_len = sta->supported_rates_len;
544         params.addr = addr;
545         params.plink_state = sta->plink_state;
546         params.aid = sta->peer_lid;
547         params.listen_interval = 100;
548         params.ht_capabilities = sta->ht_capabilities;
549         params.flags |= WPA_STA_WMM;
550         params.flags_mask |= WPA_STA_AUTHENTICATED;
551         if (conf->security == MESH_CONF_SEC_NONE) {
552                 params.flags |= WPA_STA_AUTHORIZED;
553                 params.flags |= WPA_STA_AUTHENTICATED;
554         } else {
555                 sta->flags |= WLAN_STA_MFP;
556                 params.flags |= WPA_STA_MFP;
557         }
558
559         ret = wpa_drv_sta_add(wpa_s, &params);
560         if (ret) {
561                 wpa_msg(wpa_s, MSG_ERROR,
562                         "Driver failed to insert " MACSTR ": %d",
563                         MAC2STR(addr), ret);
564                 return;
565         }
566
567         if (ssid && ssid->no_auto_peer) {
568                 wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
569                         MACSTR " because of no_auto_peer", MAC2STR(addr));
570                 return;
571         }
572
573         if (conf->security == MESH_CONF_SEC_NONE)
574                 mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
575         else
576                 mesh_rsn_auth_sae_sta(wpa_s, sta);
577 }
578
579
580 void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
581 {
582         struct hostapd_frame_info fi;
583
584         os_memset(&fi, 0, sizeof(fi));
585         fi.datarate = rx_mgmt->datarate;
586         fi.ssi_signal = rx_mgmt->ssi_signal;
587         ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
588                         rx_mgmt->frame_len, &fi);
589 }
590
591
592 static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
593                                  struct sta_info *sta)
594 {
595         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
596         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
597         u8 seq[6] = {};
598
599         wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
600                 MAC2STR(sta->addr));
601
602         if (conf->security & MESH_CONF_SEC_AMPE) {
603                 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
604                                 seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
605                 wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
606                                 seq, sizeof(seq),
607                                 sta->mgtk, sizeof(sta->mgtk));
608                 wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
609                                 seq, sizeof(seq),
610                                 sta->mgtk, sizeof(sta->mgtk));
611
612                 wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
613                 wpa_hexdump_key(MSG_DEBUG, "mgtk:",
614                                 sta->mgtk, sizeof(sta->mgtk));
615         }
616
617         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
618         hapd->num_plinks++;
619
620         sta->flags |= WLAN_STA_ASSOC;
621
622         eloop_cancel_timeout(plink_timer, wpa_s, sta);
623
624         /* Send ctrl event */
625         wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
626                      MAC2STR(sta->addr));
627 }
628
629
630 static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
631                          enum plink_event event)
632 {
633         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
634         struct mesh_conf *conf = wpa_s->ifmsh->mconf;
635         u16 reason = 0;
636
637         wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
638                 MAC2STR(sta->addr), mplstate[sta->plink_state],
639                 mplevent[event]);
640
641         switch (sta->plink_state) {
642         case PLINK_LISTEN:
643                 switch (event) {
644                 case CLS_ACPT:
645                         mesh_mpm_fsm_restart(wpa_s, sta);
646                         break;
647                 case OPN_ACPT:
648                         mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
649                         mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
650                                                    0);
651                         break;
652                 default:
653                         break;
654                 }
655                 break;
656         case PLINK_OPEN_SENT:
657                 switch (event) {
658                 case OPN_RJCT:
659                 case CNF_RJCT:
660                         reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
661                         /* fall-through */
662                 case CLS_ACPT:
663                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
664                         if (!reason)
665                                 reason = WLAN_REASON_MESH_CLOSE_RCVD;
666                         eloop_register_timeout(dot11MeshHoldingTimeout, 0,
667                                                plink_timer, wpa_s, sta);
668                         mesh_mpm_send_plink_action(wpa_s, sta,
669                                                    PLINK_CLOSE, reason);
670                         break;
671                 case OPN_ACPT:
672                         /* retry timer is left untouched */
673                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
674                         mesh_mpm_send_plink_action(wpa_s, sta,
675                                                    PLINK_CONFIRM, 0);
676                         break;
677                 case CNF_ACPT:
678                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
679                         eloop_register_timeout(dot11MeshConfirmTimeout, 0,
680                                                plink_timer, wpa_s, sta);
681                         break;
682                 default:
683                         break;
684                 }
685                 break;
686         case PLINK_OPEN_RCVD:
687                 switch (event) {
688                 case OPN_RJCT:
689                 case CNF_RJCT:
690                         reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
691                         /* fall-through */
692                 case CLS_ACPT:
693                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
694                         if (!reason)
695                                 reason = WLAN_REASON_MESH_CLOSE_RCVD;
696                         eloop_register_timeout(dot11MeshHoldingTimeout, 0,
697                                                plink_timer, wpa_s, sta);
698                         sta->mpm_close_reason = reason;
699                         mesh_mpm_send_plink_action(wpa_s, sta,
700                                                    PLINK_CLOSE, reason);
701                         break;
702                 case OPN_ACPT:
703                         mesh_mpm_send_plink_action(wpa_s, sta,
704                                                    PLINK_CONFIRM, 0);
705                         break;
706                 case CNF_ACPT:
707                         if (conf->security & MESH_CONF_SEC_AMPE)
708                                 mesh_rsn_derive_mtk(wpa_s, sta);
709                         mesh_mpm_plink_estab(wpa_s, sta);
710                         break;
711                 default:
712                         break;
713                 }
714                 break;
715         case PLINK_CNF_RCVD:
716                 switch (event) {
717                 case OPN_RJCT:
718                 case CNF_RJCT:
719                         reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
720                         /* fall-through */
721                 case CLS_ACPT:
722                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
723                         if (!reason)
724                                 reason = WLAN_REASON_MESH_CLOSE_RCVD;
725                         eloop_register_timeout(dot11MeshHoldingTimeout, 0,
726                                                plink_timer, wpa_s, sta);
727                         sta->mpm_close_reason = reason;
728                         mesh_mpm_send_plink_action(wpa_s, sta,
729                                                    PLINK_CLOSE, reason);
730                         break;
731                 case OPN_ACPT:
732                         mesh_mpm_plink_estab(wpa_s, sta);
733                         mesh_mpm_send_plink_action(wpa_s, sta,
734                                                    PLINK_CONFIRM, 0);
735                         break;
736                 default:
737                         break;
738                 }
739                 break;
740         case PLINK_ESTAB:
741                 switch (event) {
742                 case CLS_ACPT:
743                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
744                         reason = WLAN_REASON_MESH_CLOSE_RCVD;
745
746                         eloop_register_timeout(dot11MeshHoldingTimeout, 0,
747                                                plink_timer, wpa_s, sta);
748                         sta->mpm_close_reason = reason;
749
750                         wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
751                                 " closed with reason %d",
752                                 MAC2STR(sta->addr), reason);
753
754                         wpa_msg_ctrl(wpa_s, MSG_INFO,
755                                      MESH_PEER_DISCONNECTED MACSTR,
756                                      MAC2STR(sta->addr));
757
758                         hapd->num_plinks--;
759
760                         mesh_mpm_send_plink_action(wpa_s, sta,
761                                                    PLINK_CLOSE, reason);
762                         break;
763                 case OPN_ACPT:
764                         mesh_mpm_send_plink_action(wpa_s, sta,
765                                                    PLINK_CONFIRM, 0);
766                         break;
767                 default:
768                         break;
769                 }
770                 break;
771         case PLINK_HOLDING:
772                 switch (event) {
773                 case CLS_ACPT:
774                         mesh_mpm_fsm_restart(wpa_s, sta);
775                         break;
776                 case OPN_ACPT:
777                 case CNF_ACPT:
778                 case OPN_RJCT:
779                 case CNF_RJCT:
780                         reason = sta->mpm_close_reason;
781                         mesh_mpm_send_plink_action(wpa_s, sta,
782                                                    PLINK_CLOSE, reason);
783                         break;
784                 default:
785                         break;
786                 }
787                 break;
788         default:
789                 wpa_msg(wpa_s, MSG_DEBUG,
790                         "Unsupported MPM event %s for state %s",
791                         mplevent[event], mplstate[sta->plink_state]);
792                 break;
793         }
794 }
795
796
797 void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
798                         const struct ieee80211_mgmt *mgmt, size_t len)
799 {
800         u8 action_field;
801         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
802         struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
803         struct sta_info *sta;
804         u16 plid = 0, llid = 0;
805         enum plink_event event;
806         struct ieee802_11_elems elems;
807         struct mesh_peer_mgmt_ie peer_mgmt_ie;
808         const u8 *ies;
809         size_t ie_len;
810         int ret;
811
812         if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
813                 return;
814
815         action_field = mgmt->u.action.u.slf_prot_action.action;
816
817         ies = mgmt->u.action.u.slf_prot_action.variable;
818         ie_len = (const u8 *) mgmt + len -
819                 mgmt->u.action.u.slf_prot_action.variable;
820
821         /* at least expect mesh id and peering mgmt */
822         if (ie_len < 2 + 2)
823                 return;
824
825         if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
826                 ies += 2;       /* capability */
827                 ie_len -= 2;
828         }
829         if (action_field == PLINK_CONFIRM) {
830                 ies += 2;       /* aid */
831                 ie_len -= 2;
832         }
833
834         /* check for mesh peering, mesh id and mesh config IEs */
835         if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed)
836                 return;
837         if (!elems.peer_mgmt)
838                 return;
839         if ((action_field != PLINK_CLOSE) &&
840             (!elems.mesh_id || !elems.mesh_config))
841                 return;
842
843         if (action_field != PLINK_CLOSE && !matches_local(wpa_s, &elems))
844                 return;
845
846         ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
847                                        elems.peer_mgmt,
848                                        elems.peer_mgmt_len,
849                                        &peer_mgmt_ie);
850         if (ret)
851                 return;
852
853         /* the sender's llid is our plid and vice-versa */
854         plid = WPA_GET_LE16(peer_mgmt_ie.llid);
855         if (peer_mgmt_ie.plid)
856                 llid = WPA_GET_LE16(peer_mgmt_ie.plid);
857
858         sta = ap_get_sta(hapd, mgmt->sa);
859         if (!sta)
860                 return;
861
862 #ifdef CONFIG_SAE
863         /* peer is in sae_accepted? */
864         if (sta->sae && sta->sae->state != SAE_ACCEPTED)
865                 return;
866 #endif /* CONFIG_SAE */
867
868         if (!sta->my_lid)
869                 mesh_mpm_init_link(wpa_s, sta);
870
871         if (mconf->security & MESH_CONF_SEC_AMPE)
872                 if (mesh_rsn_process_ampe(wpa_s, sta, &elems,
873                                           &mgmt->u.action.category,
874                                           ies, ie_len))
875                         return;
876
877         if (sta->plink_state == PLINK_BLOCKED)
878                 return;
879
880         /* Now we will figure out the appropriate event... */
881         switch (action_field) {
882         case PLINK_OPEN:
883                 if (!plink_free_count(hapd) ||
884                     (sta->peer_lid && sta->peer_lid != plid)) {
885                         event = OPN_IGNR;
886                 } else {
887                         sta->peer_lid = plid;
888                         event = OPN_ACPT;
889                 }
890                 break;
891         case PLINK_CONFIRM:
892                 if (!plink_free_count(hapd) ||
893                     sta->my_lid != llid ||
894                     (sta->peer_lid && sta->peer_lid != plid)) {
895                         event = CNF_IGNR;
896                 } else {
897                         if (!sta->peer_lid)
898                                 sta->peer_lid = plid;
899                         event = CNF_ACPT;
900                 }
901                 break;
902         case PLINK_CLOSE:
903                 if (sta->plink_state == PLINK_ESTAB)
904                         /* Do not check for llid or plid. This does not
905                          * follow the standard but since multiple plinks
906                          * per cand are not supported, it is necessary in
907                          * order to avoid a livelock when MP A sees an
908                          * establish peer link to MP B but MP B does not
909                          * see it. This can be caused by a timeout in
910                          * B's peer link establishment or B being
911                          * restarted.
912                          */
913                         event = CLS_ACPT;
914                 else if (sta->peer_lid != plid)
915                         event = CLS_IGNR;
916                 else if (peer_mgmt_ie.plid && sta->my_lid != llid)
917                         event = CLS_IGNR;
918                 else
919                         event = CLS_ACPT;
920                 break;
921         default:
922                 wpa_msg(wpa_s, MSG_ERROR,
923                         "Mesh plink: unknown frame subtype");
924                 return;
925         }
926         mesh_mpm_fsm(wpa_s, sta, event);
927 }
928
929
930 /* called by ap_free_sta */
931 void mesh_mpm_free_sta(struct sta_info *sta)
932 {
933         eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
934         eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
935 }