Simplify Timeout Interval element parsing
[mech_eap.git] / wlantest / rx_mgmt.c
1 /*
2  * Received Management frame processing
3  * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
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 "common/defs.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "crypto/aes_wrap.h"
16 #include "wlantest.h"
17
18
19 static const char * mgmt_stype(u16 stype)
20 {
21         switch (stype) {
22         case WLAN_FC_STYPE_ASSOC_REQ:
23                 return "ASSOC-REQ";
24         case WLAN_FC_STYPE_ASSOC_RESP:
25                 return "ASSOC-RESP";
26         case WLAN_FC_STYPE_REASSOC_REQ:
27                 return "REASSOC-REQ";
28         case WLAN_FC_STYPE_REASSOC_RESP:
29                 return "REASSOC-RESP";
30         case WLAN_FC_STYPE_PROBE_REQ:
31                 return "PROBE-REQ";
32         case WLAN_FC_STYPE_PROBE_RESP:
33                 return "PROBE-RESP";
34         case WLAN_FC_STYPE_BEACON:
35                 return "BEACON";
36         case WLAN_FC_STYPE_ATIM:
37                 return "ATIM";
38         case WLAN_FC_STYPE_DISASSOC:
39                 return "DISASSOC";
40         case WLAN_FC_STYPE_AUTH:
41                 return "AUTH";
42         case WLAN_FC_STYPE_DEAUTH:
43                 return "DEAUTH";
44         case WLAN_FC_STYPE_ACTION:
45                 return "ACTION";
46         }
47         return "??";
48 }
49
50
51 static void rx_mgmt_beacon(struct wlantest *wt, const u8 *data, size_t len)
52 {
53         const struct ieee80211_mgmt *mgmt;
54         struct wlantest_bss *bss;
55         struct ieee802_11_elems elems;
56         size_t offset;
57
58         mgmt = (const struct ieee80211_mgmt *) data;
59         offset = mgmt->u.beacon.variable - data;
60         if (len < offset)
61                 return;
62         bss = bss_get(wt, mgmt->bssid);
63         if (bss == NULL)
64                 return;
65         if (bss->proberesp_seen)
66                 return; /* do not override with Beacon data */
67         bss->capab_info = le_to_host16(mgmt->u.beacon.capab_info);
68         if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - offset,
69                                    &elems, 0) == ParseFailed) {
70                 if (bss->parse_error_reported)
71                         return;
72                 add_note(wt, MSG_INFO, "Invalid IEs in a Beacon frame from "
73                          MACSTR, MAC2STR(mgmt->sa));
74                 bss->parse_error_reported = 1;
75                 return;
76         }
77
78         bss_update(wt, bss, &elems);
79 }
80
81
82 static void rx_mgmt_probe_resp(struct wlantest *wt, const u8 *data, size_t len)
83 {
84         const struct ieee80211_mgmt *mgmt;
85         struct wlantest_bss *bss;
86         struct ieee802_11_elems elems;
87         size_t offset;
88
89         mgmt = (const struct ieee80211_mgmt *) data;
90         offset = mgmt->u.probe_resp.variable - data;
91         if (len < offset)
92                 return;
93         bss = bss_get(wt, mgmt->bssid);
94         if (bss == NULL)
95                 return;
96
97         bss->counters[WLANTEST_BSS_COUNTER_PROBE_RESPONSE]++;
98         bss->capab_info = le_to_host16(mgmt->u.probe_resp.capab_info);
99         if (ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - offset,
100                                    &elems, 0) == ParseFailed) {
101                 if (bss->parse_error_reported)
102                         return;
103                 add_note(wt, MSG_INFO, "Invalid IEs in a Probe Response frame "
104                          "from " MACSTR, MAC2STR(mgmt->sa));
105                 bss->parse_error_reported = 1;
106                 return;
107         }
108
109         bss_update(wt, bss, &elems);
110 }
111
112
113 static void rx_mgmt_auth(struct wlantest *wt, const u8 *data, size_t len)
114 {
115         const struct ieee80211_mgmt *mgmt;
116         struct wlantest_bss *bss;
117         struct wlantest_sta *sta;
118         u16 alg, trans, status;
119
120         mgmt = (const struct ieee80211_mgmt *) data;
121         bss = bss_get(wt, mgmt->bssid);
122         if (bss == NULL)
123                 return;
124         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
125                 sta = sta_get(bss, mgmt->da);
126         else
127                 sta = sta_get(bss, mgmt->sa);
128         if (sta == NULL)
129                 return;
130
131         if (len < 24 + 6) {
132                 add_note(wt, MSG_INFO, "Too short Authentication frame from "
133                          MACSTR, MAC2STR(mgmt->sa));
134                 return;
135         }
136
137         alg = le_to_host16(mgmt->u.auth.auth_alg);
138         trans = le_to_host16(mgmt->u.auth.auth_transaction);
139         status = le_to_host16(mgmt->u.auth.status_code);
140
141         wpa_printf(MSG_DEBUG, "AUTH " MACSTR " -> " MACSTR
142                    " (alg=%u trans=%u status=%u)",
143                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da), alg, trans, status);
144
145         if (alg == 0 && trans == 2 && status == 0) {
146                 if (sta->state == STATE1) {
147                         add_note(wt, MSG_DEBUG, "STA " MACSTR
148                                  " moved to State 2 with " MACSTR,
149                                  MAC2STR(sta->addr), MAC2STR(bss->bssid));
150                         sta->state = STATE2;
151                 }
152         }
153
154         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
155                 sta->counters[WLANTEST_STA_COUNTER_AUTH_RX]++;
156         else
157                 sta->counters[WLANTEST_STA_COUNTER_AUTH_TX]++;
158 }
159
160
161 static void deauth_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
162 {
163         struct wlantest_sta *sta;
164         dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
165                 if (sta->state == STATE1)
166                         continue;
167                 add_note(wt, MSG_DEBUG, "STA " MACSTR
168                          " moved to State 1 with " MACSTR,
169                          MAC2STR(sta->addr), MAC2STR(bss->bssid));
170                 sta->state = STATE1;
171         }
172 }
173
174
175 static void tdls_link_down(struct wlantest *wt, struct wlantest_bss *bss,
176                            struct wlantest_sta *sta)
177 {
178         struct wlantest_tdls *tdls;
179         dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
180                 if ((tdls->init == sta || tdls->resp == sta) && tdls->link_up)
181                 {
182                         add_note(wt, MSG_DEBUG, "TDLS: Set link down based on "
183                                  "STA deauth/disassoc");
184                         tdls->link_up = 0;
185                 }
186         }
187 }
188
189
190 static void rx_mgmt_deauth(struct wlantest *wt, const u8 *data, size_t len,
191                            int valid)
192 {
193         const struct ieee80211_mgmt *mgmt;
194         struct wlantest_bss *bss;
195         struct wlantest_sta *sta;
196         u16 fc, reason;
197
198         mgmt = (const struct ieee80211_mgmt *) data;
199         bss = bss_get(wt, mgmt->bssid);
200         if (bss == NULL)
201                 return;
202         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
203                 sta = sta_get(bss, mgmt->da);
204         else
205                 sta = sta_get(bss, mgmt->sa);
206
207         if (len < 24 + 2) {
208                 add_note(wt, MSG_INFO, "Too short Deauthentication frame from "
209                          MACSTR, MAC2STR(mgmt->sa));
210                 return;
211         }
212
213         reason = le_to_host16(mgmt->u.deauth.reason_code);
214         wpa_printf(MSG_DEBUG, "DEAUTH " MACSTR " -> " MACSTR
215                    " (reason=%u) (valid=%d)",
216                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
217                    reason, valid);
218         wpa_hexdump(MSG_MSGDUMP, "DEAUTH payload", data + 24, len - 24);
219
220         if (sta == NULL) {
221                 if (valid && mgmt->da[0] == 0xff)
222                         deauth_all_stas(wt, bss);
223                 return;
224         }
225
226         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
227                 sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_RX :
228                               WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX]++;
229                 if (sta->pwrmgt && !sta->pspoll)
230                         sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_ASLEEP]++;
231                 else
232                         sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_AWAKE]++;
233
234                 fc = le_to_host16(mgmt->frame_control);
235                 if (!(fc & WLAN_FC_ISWEP) && reason == 6)
236                         sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_RC6]++;
237                 else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
238                         sta->counters[WLANTEST_STA_COUNTER_DEAUTH_RX_RC7]++;
239         } else
240                 sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DEAUTH_TX :
241                               WLANTEST_STA_COUNTER_INVALID_DEAUTH_TX]++;
242
243         if (!valid) {
244                 add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
245                          "since Disassociation frame was not protected "
246                          "correctly", MAC2STR(sta->addr));
247                 return;
248         }
249
250         if (sta->state != STATE1) {
251                 add_note(wt, MSG_DEBUG, "STA " MACSTR
252                          " moved to State 1 with " MACSTR,
253                          MAC2STR(sta->addr), MAC2STR(bss->bssid));
254                 sta->state = STATE1;
255         }
256         tdls_link_down(wt, bss, sta);
257 }
258
259
260 static void rx_mgmt_assoc_req(struct wlantest *wt, const u8 *data, size_t len)
261 {
262         const struct ieee80211_mgmt *mgmt;
263         struct wlantest_bss *bss;
264         struct wlantest_sta *sta;
265         struct ieee802_11_elems elems;
266
267         mgmt = (const struct ieee80211_mgmt *) data;
268         bss = bss_get(wt, mgmt->bssid);
269         if (bss == NULL)
270                 return;
271         sta = sta_get(bss, mgmt->sa);
272         if (sta == NULL)
273                 return;
274
275         if (len < 24 + 4) {
276                 add_note(wt, MSG_INFO, "Too short Association Request frame "
277                          "from " MACSTR, MAC2STR(mgmt->sa));
278                 return;
279         }
280
281         wpa_printf(MSG_DEBUG, "ASSOCREQ " MACSTR " -> " MACSTR
282                    " (capab=0x%x listen_int=%u)",
283                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
284                    le_to_host16(mgmt->u.assoc_req.capab_info),
285                    le_to_host16(mgmt->u.assoc_req.listen_interval));
286
287         sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX]++;
288
289         if (ieee802_11_parse_elems(mgmt->u.assoc_req.variable,
290                                    len - (mgmt->u.assoc_req.variable - data),
291                                    &elems, 0) == ParseFailed) {
292                 add_note(wt, MSG_INFO, "Invalid IEs in Association Request "
293                          "frame from " MACSTR, MAC2STR(mgmt->sa));
294                 return;
295         }
296
297         sta->assocreq_capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
298         sta->assocreq_listen_int =
299                 le_to_host16(mgmt->u.assoc_req.listen_interval);
300         os_free(sta->assocreq_ies);
301         sta->assocreq_ies_len = len - (mgmt->u.assoc_req.variable - data);
302         sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
303         if (sta->assocreq_ies)
304                 os_memcpy(sta->assocreq_ies, mgmt->u.assoc_req.variable,
305                           sta->assocreq_ies_len);
306
307         sta_update_assoc(sta, &elems);
308 }
309
310
311 static void rx_mgmt_assoc_resp(struct wlantest *wt, const u8 *data, size_t len)
312 {
313         const struct ieee80211_mgmt *mgmt;
314         struct wlantest_bss *bss;
315         struct wlantest_sta *sta;
316         u16 capab, status, aid;
317
318         mgmt = (const struct ieee80211_mgmt *) data;
319         bss = bss_get(wt, mgmt->bssid);
320         if (bss == NULL)
321                 return;
322         sta = sta_get(bss, mgmt->da);
323         if (sta == NULL)
324                 return;
325
326         if (len < 24 + 6) {
327                 add_note(wt, MSG_INFO, "Too short Association Response frame "
328                          "from " MACSTR, MAC2STR(mgmt->sa));
329                 return;
330         }
331
332         capab = le_to_host16(mgmt->u.assoc_resp.capab_info);
333         status = le_to_host16(mgmt->u.assoc_resp.status_code);
334         aid = le_to_host16(mgmt->u.assoc_resp.aid);
335
336         wpa_printf(MSG_DEBUG, "ASSOCRESP " MACSTR " -> " MACSTR
337                    " (capab=0x%x status=%u aid=%u)",
338                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
339                    aid & 0x3fff);
340
341         if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
342                 struct ieee802_11_elems elems;
343                 const u8 *ies = mgmt->u.assoc_resp.variable;
344                 size_t ies_len = len - (mgmt->u.assoc_resp.variable - data);
345                 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
346                     ParseFailed) {
347                         add_note(wt, MSG_INFO, "Failed to parse IEs in "
348                                  "AssocResp from " MACSTR,
349                                  MAC2STR(mgmt->sa));
350                 } else if (elems.timeout_int == NULL ||
351                            elems.timeout_int[0] !=
352                            WLAN_TIMEOUT_ASSOC_COMEBACK) {
353                         add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
354                                  "with Assoc Comeback time in AssocResp "
355                                  "(status=30) from " MACSTR,
356                                  MAC2STR(mgmt->sa));
357                 } else {
358                         sta->counters[
359                                 WLANTEST_STA_COUNTER_ASSOCRESP_COMEBACK]++;
360                 }
361         }
362
363         if (status)
364                 return;
365
366         if ((aid & 0xc000) != 0xc000) {
367                 add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
368                          "in Association Response from " MACSTR,
369                          MAC2STR(mgmt->sa));
370         }
371         sta->aid = aid & 0xc000;
372
373         if (sta->state < STATE2) {
374                 add_note(wt, MSG_DEBUG,
375                          "STA " MACSTR " was not in State 2 when "
376                          "getting associated", MAC2STR(sta->addr));
377         }
378
379         if (sta->state < STATE3) {
380                 add_note(wt, MSG_DEBUG, "STA " MACSTR
381                          " moved to State 3 with " MACSTR,
382                          MAC2STR(sta->addr), MAC2STR(bss->bssid));
383                 sta->state = STATE3;
384         }
385 }
386
387
388 static void rx_mgmt_reassoc_req(struct wlantest *wt, const u8 *data,
389                                 size_t len)
390 {
391         const struct ieee80211_mgmt *mgmt;
392         struct wlantest_bss *bss;
393         struct wlantest_sta *sta;
394         struct ieee802_11_elems elems;
395
396         mgmt = (const struct ieee80211_mgmt *) data;
397         bss = bss_get(wt, mgmt->bssid);
398         if (bss == NULL)
399                 return;
400         sta = sta_get(bss, mgmt->sa);
401         if (sta == NULL)
402                 return;
403
404         if (len < 24 + 4 + ETH_ALEN) {
405                 add_note(wt, MSG_INFO, "Too short Reassociation Request frame "
406                          "from " MACSTR, MAC2STR(mgmt->sa));
407                 return;
408         }
409
410         wpa_printf(MSG_DEBUG, "REASSOCREQ " MACSTR " -> " MACSTR
411                    " (capab=0x%x listen_int=%u current_ap=" MACSTR ")",
412                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
413                    le_to_host16(mgmt->u.reassoc_req.capab_info),
414                    le_to_host16(mgmt->u.reassoc_req.listen_interval),
415                    MAC2STR(mgmt->u.reassoc_req.current_ap));
416
417         sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX]++;
418
419         if (ieee802_11_parse_elems(mgmt->u.reassoc_req.variable,
420                                    len - (mgmt->u.reassoc_req.variable - data),
421                                    &elems, 0) == ParseFailed) {
422                 add_note(wt, MSG_INFO, "Invalid IEs in Reassociation Request "
423                          "frame from " MACSTR, MAC2STR(mgmt->sa));
424                 return;
425         }
426
427         sta->assocreq_capab_info =
428                 le_to_host16(mgmt->u.reassoc_req.capab_info);
429         sta->assocreq_listen_int =
430                 le_to_host16(mgmt->u.reassoc_req.listen_interval);
431         os_free(sta->assocreq_ies);
432         sta->assocreq_ies_len = len - (mgmt->u.reassoc_req.variable - data);
433         sta->assocreq_ies = os_malloc(sta->assocreq_ies_len);
434         if (sta->assocreq_ies)
435                 os_memcpy(sta->assocreq_ies, mgmt->u.reassoc_req.variable,
436                           sta->assocreq_ies_len);
437
438         sta_update_assoc(sta, &elems);
439 }
440
441
442 static void rx_mgmt_reassoc_resp(struct wlantest *wt, const u8 *data,
443                                  size_t len)
444 {
445         const struct ieee80211_mgmt *mgmt;
446         struct wlantest_bss *bss;
447         struct wlantest_sta *sta;
448         u16 capab, status, aid;
449
450         mgmt = (const struct ieee80211_mgmt *) data;
451         bss = bss_get(wt, mgmt->bssid);
452         if (bss == NULL)
453                 return;
454         sta = sta_get(bss, mgmt->da);
455         if (sta == NULL)
456                 return;
457
458         if (len < 24 + 6) {
459                 add_note(wt, MSG_INFO, "Too short Reassociation Response frame "
460                          "from " MACSTR, MAC2STR(mgmt->sa));
461                 return;
462         }
463
464         capab = le_to_host16(mgmt->u.reassoc_resp.capab_info);
465         status = le_to_host16(mgmt->u.reassoc_resp.status_code);
466         aid = le_to_host16(mgmt->u.reassoc_resp.aid);
467
468         wpa_printf(MSG_DEBUG, "REASSOCRESP " MACSTR " -> " MACSTR
469                    " (capab=0x%x status=%u aid=%u)",
470                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da), capab, status,
471                    aid & 0x3fff);
472
473         if (status == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY) {
474                 struct ieee802_11_elems elems;
475                 const u8 *ies = mgmt->u.reassoc_resp.variable;
476                 size_t ies_len = len - (mgmt->u.reassoc_resp.variable - data);
477                 if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
478                     ParseFailed) {
479                         add_note(wt, MSG_INFO, "Failed to parse IEs in "
480                                  "ReassocResp from " MACSTR,
481                                  MAC2STR(mgmt->sa));
482                 } else if (elems.timeout_int == NULL ||
483                            elems.timeout_int[0] !=
484                            WLAN_TIMEOUT_ASSOC_COMEBACK) {
485                         add_note(wt, MSG_INFO, "No valid Timeout Interval IE "
486                                  "with Assoc Comeback time in ReassocResp "
487                                  "(status=30) from " MACSTR,
488                                  MAC2STR(mgmt->sa));
489                 } else {
490                         sta->counters[
491                                 WLANTEST_STA_COUNTER_REASSOCRESP_COMEBACK]++;
492                 }
493         }
494
495         if (status)
496                 return;
497
498         if ((aid & 0xc000) != 0xc000) {
499                 add_note(wt, MSG_DEBUG, "Two MSBs of the AID were not set to 1 "
500                          "in Reassociation Response from " MACSTR,
501                          MAC2STR(mgmt->sa));
502         }
503         sta->aid = aid & 0xc000;
504
505         if (sta->state < STATE2) {
506                 add_note(wt, MSG_DEBUG,
507                          "STA " MACSTR " was not in State 2 when "
508                          "getting associated", MAC2STR(sta->addr));
509         }
510
511         if (sta->state < STATE3) {
512                 add_note(wt, MSG_DEBUG, "STA " MACSTR
513                          " moved to State 3 with " MACSTR,
514                          MAC2STR(sta->addr), MAC2STR(bss->bssid));
515                 sta->state = STATE3;
516         }
517 }
518
519
520 static void disassoc_all_stas(struct wlantest *wt, struct wlantest_bss *bss)
521 {
522         struct wlantest_sta *sta;
523         dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
524                 if (sta->state <= STATE2)
525                         continue;
526                 add_note(wt, MSG_DEBUG, "STA " MACSTR
527                          " moved to State 2 with " MACSTR,
528                          MAC2STR(sta->addr), MAC2STR(bss->bssid));
529                 sta->state = STATE2;
530         }
531 }
532
533
534 static void rx_mgmt_disassoc(struct wlantest *wt, const u8 *data, size_t len,
535                              int valid)
536 {
537         const struct ieee80211_mgmt *mgmt;
538         struct wlantest_bss *bss;
539         struct wlantest_sta *sta;
540         u16 fc, reason;
541
542         mgmt = (const struct ieee80211_mgmt *) data;
543         bss = bss_get(wt, mgmt->bssid);
544         if (bss == NULL)
545                 return;
546         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
547                 sta = sta_get(bss, mgmt->da);
548         else
549                 sta = sta_get(bss, mgmt->sa);
550
551         if (len < 24 + 2) {
552                 add_note(wt, MSG_INFO, "Too short Disassociation frame from "
553                          MACSTR, MAC2STR(mgmt->sa));
554                 return;
555         }
556
557         reason = le_to_host16(mgmt->u.disassoc.reason_code);
558         wpa_printf(MSG_DEBUG, "DISASSOC " MACSTR " -> " MACSTR
559                    " (reason=%u) (valid=%d)",
560                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
561                    reason, valid);
562         wpa_hexdump(MSG_MSGDUMP, "DISASSOC payload", data + 24, len - 24);
563
564         if (sta == NULL) {
565                 if (valid && mgmt->da[0] == 0xff)
566                         disassoc_all_stas(wt, bss);
567                 return;
568         }
569
570         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
571                 sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_RX :
572                               WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX]++;
573                 if (sta->pwrmgt && !sta->pspoll)
574                         sta->counters[
575                                 WLANTEST_STA_COUNTER_DISASSOC_RX_ASLEEP]++;
576                 else
577                         sta->counters[
578                                 WLANTEST_STA_COUNTER_DISASSOC_RX_AWAKE]++;
579
580                 fc = le_to_host16(mgmt->frame_control);
581                 if (!(fc & WLAN_FC_ISWEP) && reason == 6)
582                         sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC6]++;
583                 else if (!(fc & WLAN_FC_ISWEP) && reason == 7)
584                         sta->counters[WLANTEST_STA_COUNTER_DISASSOC_RX_RC7]++;
585         } else
586                 sta->counters[valid ? WLANTEST_STA_COUNTER_VALID_DISASSOC_TX :
587                               WLANTEST_STA_COUNTER_INVALID_DISASSOC_TX]++;
588
589         if (!valid) {
590                 add_note(wt, MSG_INFO, "Do not change STA " MACSTR " State "
591                          "since Disassociation frame was not protected "
592                          "correctly", MAC2STR(sta->addr));
593                 return;
594         }
595
596         if (sta->state < STATE2) {
597                 add_note(wt, MSG_DEBUG,
598                          "STA " MACSTR " was not in State 2 or 3 "
599                          "when getting disassociated", MAC2STR(sta->addr));
600         }
601
602         if (sta->state > STATE2) {
603                 add_note(wt, MSG_DEBUG, "STA " MACSTR
604                          " moved to State 2 with " MACSTR,
605                          MAC2STR(sta->addr), MAC2STR(bss->bssid));
606                 sta->state = STATE2;
607         }
608         tdls_link_down(wt, bss, sta);
609 }
610
611
612 static void rx_mgmt_action_sa_query_req(struct wlantest *wt,
613                                         struct wlantest_sta *sta,
614                                         const struct ieee80211_mgmt *mgmt,
615                                         size_t len, int valid)
616 {
617         const u8 *rx_id;
618         u8 *id;
619
620         rx_id = (const u8 *) mgmt->u.action.u.sa_query_req.trans_id;
621         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
622                 id = sta->ap_sa_query_tr;
623         else
624                 id = sta->sta_sa_query_tr;
625         add_note(wt, MSG_INFO, "SA Query Request " MACSTR " -> " MACSTR
626                  " (trans_id=%02x%02x)%s",
627                  MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
628                  valid ? "" : " (invalid protection)");
629         os_memcpy(id, mgmt->u.action.u.sa_query_req.trans_id, 2);
630         if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
631                 sta->counters[valid ?
632                               WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_TX :
633                               WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_TX]++;
634         else
635                 sta->counters[valid ?
636                               WLANTEST_STA_COUNTER_VALID_SAQUERYREQ_RX :
637                               WLANTEST_STA_COUNTER_INVALID_SAQUERYREQ_RX]++;
638 }
639
640
641 static void rx_mgmt_action_sa_query_resp(struct wlantest *wt,
642                                          struct wlantest_sta *sta,
643                                          const struct ieee80211_mgmt *mgmt,
644                                          size_t len, int valid)
645 {
646         const u8 *rx_id;
647         u8 *id;
648         int match;
649
650         rx_id = (const u8 *) mgmt->u.action.u.sa_query_resp.trans_id;
651         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
652                 id = sta->sta_sa_query_tr;
653         else
654                 id = sta->ap_sa_query_tr;
655         match = os_memcmp(rx_id, id, 2) == 0;
656         add_note(wt, MSG_INFO, "SA Query Response " MACSTR " -> " MACSTR
657                  " (trans_id=%02x%02x; %s)%s",
658                  MAC2STR(mgmt->sa), MAC2STR(mgmt->da), rx_id[0], rx_id[1],
659                  match ? "match" : "mismatch",
660                  valid ? "" : " (invalid protection)");
661         if (os_memcmp(mgmt->sa, sta->addr, ETH_ALEN) == 0)
662                 sta->counters[(valid && match) ?
663                               WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_TX :
664                               WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_TX]++;
665         else
666                 sta->counters[(valid && match) ?
667                               WLANTEST_STA_COUNTER_VALID_SAQUERYRESP_RX :
668                               WLANTEST_STA_COUNTER_INVALID_SAQUERYRESP_RX]++;
669 }
670
671
672 static void rx_mgmt_action_sa_query(struct wlantest *wt,
673                                     struct wlantest_sta *sta,
674                                     const struct ieee80211_mgmt *mgmt,
675                                     size_t len, int valid)
676 {
677         if (len < 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
678                 add_note(wt, MSG_INFO, "Too short SA Query frame from " MACSTR,
679                          MAC2STR(mgmt->sa));
680                 return;
681         }
682
683         if (len > 24 + 2 + WLAN_SA_QUERY_TR_ID_LEN) {
684                 size_t elen = len - (24 + 2 + WLAN_SA_QUERY_TR_ID_LEN);
685                 add_note(wt, MSG_INFO, "Unexpected %u octets of extra data at "
686                          "the end of SA Query frame from " MACSTR,
687                          (unsigned) elen, MAC2STR(mgmt->sa));
688                 wpa_hexdump(MSG_INFO, "SA Query extra data",
689                             ((const u8 *) mgmt) + len - elen, elen);
690         }
691
692         switch (mgmt->u.action.u.sa_query_req.action) {
693         case WLAN_SA_QUERY_REQUEST:
694                 rx_mgmt_action_sa_query_req(wt, sta, mgmt, len, valid);
695                 break;
696         case WLAN_SA_QUERY_RESPONSE:
697                 rx_mgmt_action_sa_query_resp(wt, sta, mgmt, len, valid);
698                 break;
699         default:
700                 add_note(wt, MSG_INFO, "Unexpected SA Query action value %u "
701                          "from " MACSTR,
702                          mgmt->u.action.u.sa_query_req.action,
703                          MAC2STR(mgmt->sa));
704         }
705 }
706
707
708 static void rx_mgmt_action(struct wlantest *wt, const u8 *data, size_t len,
709                            int valid)
710 {
711         const struct ieee80211_mgmt *mgmt;
712         struct wlantest_bss *bss;
713         struct wlantest_sta *sta;
714
715         mgmt = (const struct ieee80211_mgmt *) data;
716         if (mgmt->da[0] & 0x01) {
717                 add_note(wt, MSG_DEBUG, "Group addressed Action frame: DA="
718                          MACSTR " SA=" MACSTR " BSSID=" MACSTR
719                          " category=%u",
720                          MAC2STR(mgmt->da), MAC2STR(mgmt->sa),
721                          MAC2STR(mgmt->bssid), mgmt->u.action.category);
722                 return; /* Ignore group addressed Action frames for now */
723         }
724         bss = bss_get(wt, mgmt->bssid);
725         if (bss == NULL)
726                 return;
727         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
728                 sta = sta_get(bss, mgmt->da);
729         else
730                 sta = sta_get(bss, mgmt->sa);
731         if (sta == NULL)
732                 return;
733
734         if (len < 24 + 1) {
735                 add_note(wt, MSG_INFO, "Too short Action frame from " MACSTR,
736                          MAC2STR(mgmt->sa));
737                 return;
738         }
739
740         wpa_printf(MSG_DEBUG, "ACTION " MACSTR " -> " MACSTR
741                    " (category=%u) (valid=%d)",
742                    MAC2STR(mgmt->sa), MAC2STR(mgmt->da),
743                    mgmt->u.action.category, valid);
744         wpa_hexdump(MSG_MSGDUMP, "ACTION payload", data + 24, len - 24);
745
746         if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
747             sta->state < STATE3) {
748                 add_note(wt, MSG_INFO, "Action frame sent when STA is not in "
749                          "State 3 (SA=" MACSTR " DATA=" MACSTR ")",
750                          MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
751         }
752
753         switch (mgmt->u.action.category) {
754         case WLAN_ACTION_SA_QUERY:
755                 rx_mgmt_action_sa_query(wt, sta, mgmt, len, valid);
756                 break;
757         }
758 }
759
760
761 static int check_mmie_mic(unsigned int mgmt_group_cipher,
762                           const u8 *igtk, size_t igtk_len,
763                           const u8 *data, size_t len)
764 {
765         u8 *buf;
766         u8 mic[16];
767         u16 fc;
768         const struct ieee80211_hdr *hdr;
769         int ret, mic_len;
770
771         if (!mgmt_group_cipher || igtk_len < 16)
772                 return -1;
773         mic_len = mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
774
775         if (len < 24 || len - 24 < mic_len)
776                 return -1;
777
778         buf = os_malloc(len + 20 - 24);
779         if (buf == NULL)
780                 return -1;
781
782         /* BIP AAD: FC(masked) A1 A2 A3 */
783         hdr = (const struct ieee80211_hdr *) data;
784         fc = le_to_host16(hdr->frame_control);
785         fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
786         WPA_PUT_LE16(buf, fc);
787         os_memcpy(buf + 2, hdr->addr1, 3 * ETH_ALEN);
788
789         /* Frame body with MMIE MIC masked to zero */
790         os_memcpy(buf + 20, data + 24, len - 24 - mic_len);
791         os_memset(buf + 20 + len - 24 - mic_len, 0, mic_len);
792
793         wpa_hexdump(MSG_MSGDUMP, "BIP: AAD|Body(masked)", buf, len + 20 - 24);
794         /* MIC = L(AES-128-CMAC(AAD || Frame Body(masked)), 0, 64) */
795         if (mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
796                 ret = omac1_aes_128(igtk, buf, len + 20 - 24, mic);
797         } else if (mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256) {
798                 ret = omac1_aes_256(igtk, buf, len + 20 - 24, mic);
799         } else if (mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
800                  mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256) {
801                 u8 nonce[12], *npos;
802                 const u8 *ipn;
803
804                 ipn = data + len - mic_len - 6;
805
806                 /* Nonce: A2 | IPN */
807                 os_memcpy(nonce, hdr->addr2, ETH_ALEN);
808                 npos = nonce + ETH_ALEN;
809                 *npos++ = ipn[5];
810                 *npos++ = ipn[4];
811                 *npos++ = ipn[3];
812                 *npos++ = ipn[2];
813                 *npos++ = ipn[1];
814                 *npos++ = ipn[0];
815
816                 ret = aes_gmac(igtk, igtk_len, nonce, sizeof(nonce),
817                                buf, len + 20 - 24, mic);
818         } else {
819                 ret = -1;
820         }
821         if (ret < 0) {
822                 os_free(buf);
823                 return -1;
824         }
825
826         os_free(buf);
827
828         if (os_memcmp(data + len - mic_len, mic, mic_len) != 0)
829                 return -1;
830
831         return 0;
832 }
833
834
835 static int check_bip(struct wlantest *wt, const u8 *data, size_t len)
836 {
837         const struct ieee80211_mgmt *mgmt;
838         u16 fc, stype;
839         const u8 *mmie;
840         u16 keyid;
841         struct wlantest_bss *bss;
842         size_t mic_len;
843
844         mgmt = (const struct ieee80211_mgmt *) data;
845         fc = le_to_host16(mgmt->frame_control);
846         stype = WLAN_FC_GET_STYPE(fc);
847
848         if (stype == WLAN_FC_STYPE_ACTION) {
849                 if (len < 24 + 1)
850                         return 0;
851                 if (mgmt->u.action.category == WLAN_ACTION_PUBLIC)
852                         return 0; /* Not a robust management frame */
853         }
854
855         bss = bss_get(wt, mgmt->bssid);
856         if (bss == NULL)
857                 return 0; /* No key known yet */
858
859         mic_len = bss->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ? 8 : 16;
860
861         if (len < 24 + 10 + mic_len ||
862             data[len - (10 + mic_len)] != WLAN_EID_MMIE ||
863             data[len - (10 + mic_len - 1)] != 8 + mic_len) {
864                 /* No MMIE */
865                 if (bss->rsn_capab & WPA_CAPABILITY_MFPC) {
866                         add_note(wt, MSG_INFO, "Robust group-addressed "
867                                  "management frame sent without BIP by "
868                                  MACSTR, MAC2STR(mgmt->sa));
869                         bss->counters[WLANTEST_BSS_COUNTER_MISSING_BIP_MMIE]++;
870                         return -1;
871                 }
872                 return 0;
873         }
874
875         mmie = data + len - (8 + mic_len);
876         keyid = WPA_GET_LE16(mmie);
877         if (keyid & 0xf000) {
878                 add_note(wt, MSG_INFO, "MMIE KeyID reserved bits not zero "
879                          "(%04x) from " MACSTR, keyid, MAC2STR(mgmt->sa));
880                 keyid &= 0x0fff;
881         }
882         if (keyid < 4 || keyid > 5) {
883                 add_note(wt, MSG_INFO, "Unexpected MMIE KeyID %u from " MACSTR,
884                          keyid, MAC2STR(mgmt->sa));
885                 bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
886                 return 0;
887         }
888         wpa_printf(MSG_DEBUG, "MMIE KeyID %u", keyid);
889         wpa_hexdump(MSG_MSGDUMP, "MMIE IPN", mmie + 2, 6);
890         wpa_hexdump(MSG_MSGDUMP, "MMIE MIC", mmie + 8, mic_len);
891
892         if (!bss->igtk_len[keyid]) {
893                 add_note(wt, MSG_DEBUG, "No IGTK known to validate BIP frame");
894                 return 0;
895         }
896
897         if (os_memcmp(mmie + 2, bss->ipn[keyid], 6) <= 0) {
898                 add_note(wt, MSG_INFO, "BIP replay detected: SA=" MACSTR,
899                          MAC2STR(mgmt->sa));
900                 wpa_hexdump(MSG_INFO, "RX IPN", mmie + 2, 6);
901                 wpa_hexdump(MSG_INFO, "Last RX IPN", bss->ipn[keyid], 6);
902         }
903
904         if (check_mmie_mic(bss->mgmt_group_cipher, bss->igtk[keyid],
905                            bss->igtk_len[keyid], data, len) < 0) {
906                 add_note(wt, MSG_INFO, "Invalid MMIE MIC in a frame from "
907                          MACSTR, MAC2STR(mgmt->sa));
908                 bss->counters[WLANTEST_BSS_COUNTER_INVALID_BIP_MMIE]++;
909                 return -1;
910         }
911
912         add_note(wt, MSG_DEBUG, "Valid MMIE MIC");
913         os_memcpy(bss->ipn[keyid], mmie + 2, 6);
914         bss->counters[WLANTEST_BSS_COUNTER_VALID_BIP_MMIE]++;
915
916         if (stype == WLAN_FC_STYPE_DEAUTH)
917                 bss->counters[WLANTEST_BSS_COUNTER_BIP_DEAUTH]++;
918         else if (stype == WLAN_FC_STYPE_DISASSOC)
919                 bss->counters[WLANTEST_BSS_COUNTER_BIP_DISASSOC]++;
920
921         return 0;
922 }
923
924
925 static u8 * mgmt_ccmp_decrypt(struct wlantest *wt, const u8 *data, size_t len,
926                               size_t *dlen)
927 {
928         struct wlantest_bss *bss;
929         struct wlantest_sta *sta;
930         const struct ieee80211_hdr *hdr;
931         int keyid;
932         u8 *decrypted, *frame = NULL;
933         u8 pn[6], *rsc;
934
935         hdr = (const struct ieee80211_hdr *) data;
936         bss = bss_get(wt, hdr->addr3);
937         if (bss == NULL)
938                 return NULL;
939         if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
940                 sta = sta_get(bss, hdr->addr2);
941         else
942                 sta = sta_get(bss, hdr->addr1);
943         if (sta == NULL || !sta->ptk_set) {
944                 add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
945                 return NULL;
946         }
947
948         if (len < 24 + 4)
949                 return NULL;
950
951         if (!(data[24 + 3] & 0x20)) {
952                 add_note(wt, MSG_INFO, "Expected CCMP frame from " MACSTR
953                          " did not have ExtIV bit set to 1",
954                          MAC2STR(hdr->addr2));
955                 return NULL;
956         }
957
958         if (data[24 + 2] != 0 || (data[24 + 3] & 0x1f) != 0) {
959                 add_note(wt, MSG_INFO, "CCMP mgmt frame from " MACSTR " used "
960                          "non-zero reserved bit", MAC2STR(hdr->addr2));
961         }
962
963         keyid = data[24 + 3] >> 6;
964         if (keyid != 0) {
965                 add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
966                          "individually addressed Management frame from "
967                          MACSTR, keyid, MAC2STR(hdr->addr2));
968         }
969
970         if (os_memcmp(hdr->addr1, hdr->addr3, ETH_ALEN) == 0)
971                 rsc = sta->rsc_tods[16];
972         else
973                 rsc = sta->rsc_fromds[16];
974
975         ccmp_get_pn(pn, data + 24);
976         if (os_memcmp(pn, rsc, 6) <= 0) {
977                 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
978                 add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
979                          " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
980                          MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
981                          MAC2STR(hdr->addr3),
982                          WLAN_GET_SEQ_SEQ(seq_ctrl),
983                          WLAN_GET_SEQ_FRAG(seq_ctrl),
984                          (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
985                          " Retry" : "");
986                 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
987                 wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
988         }
989
990         decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data + 24, len - 24, dlen);
991         if (decrypted) {
992                 os_memcpy(rsc, pn, 6);
993                 frame = os_malloc(24 + *dlen);
994                 if (frame) {
995                         os_memcpy(frame, data, 24);
996                         os_memcpy(frame + 24, decrypted, *dlen);
997                         *dlen += 24;
998                 }
999         }
1000
1001         os_free(decrypted);
1002
1003         return frame;
1004 }
1005
1006
1007 static int check_mgmt_ccmp(struct wlantest *wt, const u8 *data, size_t len)
1008 {
1009         const struct ieee80211_mgmt *mgmt;
1010         u16 fc;
1011         struct wlantest_bss *bss;
1012         struct wlantest_sta *sta;
1013
1014         mgmt = (const struct ieee80211_mgmt *) data;
1015         fc = le_to_host16(mgmt->frame_control);
1016
1017         if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
1018                 if (len > 24 &&
1019                     mgmt->u.action.category == WLAN_ACTION_PUBLIC)
1020                         return 0; /* Not a robust management frame */
1021         }
1022
1023         bss = bss_get(wt, mgmt->bssid);
1024         if (bss == NULL)
1025                 return 0;
1026         if (os_memcmp(mgmt->da, mgmt->bssid, ETH_ALEN) == 0)
1027                 sta = sta_get(bss, mgmt->sa);
1028         else
1029                 sta = sta_get(bss, mgmt->da);
1030         if (sta == NULL)
1031                 return 0;
1032
1033         if ((sta->rsn_capab & WPA_CAPABILITY_MFPC) &&
1034             (sta->state == STATE3 ||
1035              WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION)) {
1036                 add_note(wt, MSG_INFO, "Robust individually-addressed "
1037                          "management frame sent without CCMP by "
1038                          MACSTR, MAC2STR(mgmt->sa));
1039                 return -1;
1040         }
1041
1042         return 0;
1043 }
1044
1045
1046 void rx_mgmt(struct wlantest *wt, const u8 *data, size_t len)
1047 {
1048         const struct ieee80211_hdr *hdr;
1049         u16 fc, stype;
1050         int valid = 1;
1051         u8 *decrypted = NULL;
1052         size_t dlen;
1053
1054         if (len < 24)
1055                 return;
1056
1057         hdr = (const struct ieee80211_hdr *) data;
1058         fc = le_to_host16(hdr->frame_control);
1059         wt->rx_mgmt++;
1060         stype = WLAN_FC_GET_STYPE(fc);
1061
1062         if ((hdr->addr1[0] & 0x01) &&
1063             (stype == WLAN_FC_STYPE_DEAUTH ||
1064              stype == WLAN_FC_STYPE_DISASSOC ||
1065              stype == WLAN_FC_STYPE_ACTION)) {
1066                 if (check_bip(wt, data, len) < 0)
1067                         valid = 0;
1068         }
1069
1070         wpa_printf((stype == WLAN_FC_STYPE_BEACON ||
1071                     stype == WLAN_FC_STYPE_PROBE_RESP ||
1072                     stype == WLAN_FC_STYPE_PROBE_REQ) ?
1073                    MSG_EXCESSIVE : MSG_MSGDUMP,
1074                    "MGMT %s%s%s DA=" MACSTR " SA=" MACSTR " BSSID=" MACSTR,
1075                    mgmt_stype(stype),
1076                    fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
1077                    fc & WLAN_FC_ISWEP ? " Prot" : "",
1078                    MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1079                    MAC2STR(hdr->addr3));
1080
1081         if ((fc & WLAN_FC_ISWEP) &&
1082             !(hdr->addr1[0] & 0x01) &&
1083             (stype == WLAN_FC_STYPE_DEAUTH ||
1084              stype == WLAN_FC_STYPE_DISASSOC ||
1085              stype == WLAN_FC_STYPE_ACTION)) {
1086                 decrypted = mgmt_ccmp_decrypt(wt, data, len, &dlen);
1087                 if (decrypted) {
1088                         write_pcap_decrypted(wt, decrypted, dlen, NULL, 0);
1089                         data = decrypted;
1090                         len = dlen;
1091                 } else
1092                         valid = 0;
1093         }
1094
1095         if (!(fc & WLAN_FC_ISWEP) &&
1096             !(hdr->addr1[0] & 0x01) &&
1097             (stype == WLAN_FC_STYPE_DEAUTH ||
1098              stype == WLAN_FC_STYPE_DISASSOC ||
1099              stype == WLAN_FC_STYPE_ACTION)) {
1100                 if (check_mgmt_ccmp(wt, data, len) < 0)
1101                         valid = 0;
1102         }
1103
1104         switch (stype) {
1105         case WLAN_FC_STYPE_BEACON:
1106                 rx_mgmt_beacon(wt, data, len);
1107                 break;
1108         case WLAN_FC_STYPE_PROBE_RESP:
1109                 rx_mgmt_probe_resp(wt, data, len);
1110                 break;
1111         case WLAN_FC_STYPE_AUTH:
1112                 rx_mgmt_auth(wt, data, len);
1113                 break;
1114         case WLAN_FC_STYPE_DEAUTH:
1115                 rx_mgmt_deauth(wt, data, len, valid);
1116                 break;
1117         case WLAN_FC_STYPE_ASSOC_REQ:
1118                 rx_mgmt_assoc_req(wt, data, len);
1119                 break;
1120         case WLAN_FC_STYPE_ASSOC_RESP:
1121                 rx_mgmt_assoc_resp(wt, data, len);
1122                 break;
1123         case WLAN_FC_STYPE_REASSOC_REQ:
1124                 rx_mgmt_reassoc_req(wt, data, len);
1125                 break;
1126         case WLAN_FC_STYPE_REASSOC_RESP:
1127                 rx_mgmt_reassoc_resp(wt, data, len);
1128                 break;
1129         case WLAN_FC_STYPE_DISASSOC:
1130                 rx_mgmt_disassoc(wt, data, len, valid);
1131                 break;
1132         case WLAN_FC_STYPE_ACTION:
1133                 rx_mgmt_action(wt, data, len, valid);
1134                 break;
1135         }
1136
1137         os_free(decrypted);
1138
1139         wt->last_mgmt_valid = valid;
1140 }
1141
1142
1143 static void rx_mgmt_deauth_ack(struct wlantest *wt,
1144                                const struct ieee80211_hdr *hdr)
1145 {
1146         const struct ieee80211_mgmt *mgmt;
1147         struct wlantest_bss *bss;
1148         struct wlantest_sta *sta;
1149
1150         mgmt = (const struct ieee80211_mgmt *) hdr;
1151         bss = bss_get(wt, mgmt->bssid);
1152         if (bss == NULL)
1153                 return;
1154         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
1155                 sta = sta_get(bss, mgmt->da);
1156         else
1157                 sta = sta_get(bss, mgmt->sa);
1158         if (sta == NULL)
1159                 return;
1160
1161         add_note(wt, MSG_DEBUG, "DEAUTH from " MACSTR " acknowledged by "
1162                  MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
1163         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
1164                 int c;
1165                 c = wt->last_mgmt_valid ?
1166                         WLANTEST_STA_COUNTER_VALID_DEAUTH_RX_ACK :
1167                         WLANTEST_STA_COUNTER_INVALID_DEAUTH_RX_ACK;
1168                 sta->counters[c]++;
1169         }
1170 }
1171
1172
1173 static void rx_mgmt_disassoc_ack(struct wlantest *wt,
1174                                  const struct ieee80211_hdr *hdr)
1175 {
1176         const struct ieee80211_mgmt *mgmt;
1177         struct wlantest_bss *bss;
1178         struct wlantest_sta *sta;
1179
1180         mgmt = (const struct ieee80211_mgmt *) hdr;
1181         bss = bss_get(wt, mgmt->bssid);
1182         if (bss == NULL)
1183                 return;
1184         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0)
1185                 sta = sta_get(bss, mgmt->da);
1186         else
1187                 sta = sta_get(bss, mgmt->sa);
1188         if (sta == NULL)
1189                 return;
1190
1191         add_note(wt, MSG_DEBUG, "DISASSOC from " MACSTR " acknowledged by "
1192                  MACSTR, MAC2STR(mgmt->sa), MAC2STR(mgmt->da));
1193         if (os_memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN) == 0) {
1194                 int c;
1195                 c = wt->last_mgmt_valid ?
1196                         WLANTEST_STA_COUNTER_VALID_DISASSOC_RX_ACK :
1197                         WLANTEST_STA_COUNTER_INVALID_DISASSOC_RX_ACK;
1198                 sta->counters[c]++;
1199         }
1200 }
1201
1202
1203 void rx_mgmt_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
1204 {
1205         u16 fc, stype;
1206         fc = le_to_host16(hdr->frame_control);
1207         stype = WLAN_FC_GET_STYPE(fc);
1208
1209         wpa_printf(MSG_MSGDUMP, "MGMT ACK: stype=%u a1=" MACSTR " a2=" MACSTR
1210                    " a3=" MACSTR,
1211                    stype, MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
1212                    MAC2STR(hdr->addr3));
1213
1214         switch (stype) {
1215         case WLAN_FC_STYPE_DEAUTH:
1216                 rx_mgmt_deauth_ack(wt, hdr);
1217                 break;
1218         case WLAN_FC_STYPE_DISASSOC:
1219                 rx_mgmt_disassoc_ack(wt, hdr);
1220                 break;
1221         }
1222 }