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