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