]> git.ipfire.org Git - thirdparty/hostap.git/blob - wlantest/rx_data.c
d86a5886c3bc5db5d91916336cc201c65575e9b6
[thirdparty/hostap.git] / wlantest / rx_data.c
1 /*
2 * Received Data 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 "wlantest.h"
15
16
17 static const char * data_stype(u16 stype)
18 {
19 switch (stype) {
20 case WLAN_FC_STYPE_DATA:
21 return "DATA";
22 case WLAN_FC_STYPE_DATA_CFACK:
23 return "DATA-CFACK";
24 case WLAN_FC_STYPE_DATA_CFPOLL:
25 return "DATA-CFPOLL";
26 case WLAN_FC_STYPE_DATA_CFACKPOLL:
27 return "DATA-CFACKPOLL";
28 case WLAN_FC_STYPE_NULLFUNC:
29 return "NULLFUNC";
30 case WLAN_FC_STYPE_CFACK:
31 return "CFACK";
32 case WLAN_FC_STYPE_CFPOLL:
33 return "CFPOLL";
34 case WLAN_FC_STYPE_CFACKPOLL:
35 return "CFACKPOLL";
36 case WLAN_FC_STYPE_QOS_DATA:
37 return "QOSDATA";
38 case WLAN_FC_STYPE_QOS_DATA_CFACK:
39 return "QOSDATA-CFACK";
40 case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
41 return "QOSDATA-CFPOLL";
42 case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
43 return "QOSDATA-CFACKPOLL";
44 case WLAN_FC_STYPE_QOS_NULL:
45 return "QOS-NULL";
46 case WLAN_FC_STYPE_QOS_CFPOLL:
47 return "QOS-CFPOLL";
48 case WLAN_FC_STYPE_QOS_CFACKPOLL:
49 return "QOS-CFACKPOLL";
50 }
51 return "??";
52 }
53
54
55 static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
56 const u8 *sta_addr, const u8 *dst, const u8 *src,
57 u16 ethertype, const u8 *data, size_t len, int prot,
58 const u8 *peer_addr);
59
60 static void rx_data_vlan(struct wlantest *wt, const u8 *bssid,
61 const u8 *sta_addr, const u8 *dst, const u8 *src,
62 const u8 *data, size_t len, int prot,
63 const u8 *peer_addr)
64 {
65 u16 tag;
66
67 if (len < 4)
68 return;
69 tag = WPA_GET_BE16(data);
70 wpa_printf(MSG_MSGDUMP, "VLAN tag: Priority=%u ID=%u",
71 tag >> 12, tag & 0x0ffff);
72 /* ignore VLAN information and process the original frame */
73 rx_data_eth(wt, bssid, sta_addr, dst, src, WPA_GET_BE16(data + 2),
74 data + 4, len - 4, prot, peer_addr);
75 }
76
77
78 static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
79 const u8 *sta_addr, const u8 *dst, const u8 *src,
80 u16 ethertype, const u8 *data, size_t len, int prot,
81 const u8 *peer_addr)
82 {
83 switch (ethertype) {
84 case ETH_P_PAE:
85 rx_data_eapol(wt, bssid, sta_addr, dst, src, data, len, prot);
86 break;
87 case ETH_P_IP:
88 rx_data_ip(wt, bssid, sta_addr, dst, src, data, len,
89 peer_addr);
90 break;
91 case 0x890d:
92 rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len);
93 break;
94 case ETH_P_8021Q:
95 rx_data_vlan(wt, bssid, sta_addr, dst, src, data, len, prot,
96 peer_addr);
97 break;
98 }
99 }
100
101
102 static void rx_data_process(struct wlantest *wt, const u8 *bssid,
103 const u8 *sta_addr,
104 const u8 *dst, const u8 *src,
105 const u8 *data, size_t len, int prot,
106 const u8 *peer_addr)
107 {
108 if (len == 0)
109 return;
110
111 if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
112 rx_data_eth(wt, bssid, sta_addr, dst, src,
113 WPA_GET_BE16(data + 6), data + 8, len - 8, prot,
114 peer_addr);
115 return;
116 }
117
118 wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
119 }
120
121
122 static void write_decrypted_note(struct wlantest *wt, const u8 *decrypted,
123 const u8 *tk, size_t tk_len, int keyid)
124 {
125 char tk_hex[65];
126
127 if (!decrypted)
128 return;
129
130 wpa_snprintf_hex(tk_hex, sizeof(tk_hex), tk, tk_len);
131 add_note(wt, MSG_EXCESSIVE, "TK[%d] %s", keyid, tk_hex);
132 }
133
134
135 static u8 * try_ptk(int pairwise_cipher, struct wpa_ptk *ptk,
136 const struct ieee80211_hdr *hdr,
137 const u8 *data, size_t data_len, size_t *decrypted_len)
138 {
139 u8 *decrypted;
140 unsigned int tk_len = ptk->tk_len;
141
142 decrypted = NULL;
143 if ((pairwise_cipher == WPA_CIPHER_CCMP ||
144 pairwise_cipher == 0) && tk_len == 16) {
145 decrypted = ccmp_decrypt(ptk->tk, hdr, data,
146 data_len, decrypted_len);
147 } else if ((pairwise_cipher == WPA_CIPHER_CCMP_256 ||
148 pairwise_cipher == 0) && tk_len == 32) {
149 decrypted = ccmp_256_decrypt(ptk->tk, hdr, data,
150 data_len, decrypted_len);
151 } else if ((pairwise_cipher == WPA_CIPHER_GCMP ||
152 pairwise_cipher == WPA_CIPHER_GCMP_256 ||
153 pairwise_cipher == 0) &&
154 (tk_len == 16 || tk_len == 32)) {
155 decrypted = gcmp_decrypt(ptk->tk, tk_len, hdr,
156 data, data_len, decrypted_len);
157 } else if ((pairwise_cipher == WPA_CIPHER_TKIP ||
158 pairwise_cipher == 0) && tk_len == 32) {
159 decrypted = tkip_decrypt(ptk->tk, hdr, data, data_len,
160 decrypted_len);
161 }
162
163 return decrypted;
164 }
165
166
167 static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher,
168 const struct ieee80211_hdr *hdr, int keyid,
169 const u8 *data, size_t data_len, size_t *decrypted_len)
170 {
171 struct wlantest_ptk *ptk;
172 u8 *decrypted;
173 int prev_level = wpa_debug_level;
174
175 wpa_debug_level = MSG_WARNING;
176 dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) {
177 decrypted = try_ptk(pairwise_cipher, &ptk->ptk, hdr,
178 data, data_len, decrypted_len);
179 if (decrypted) {
180 wpa_debug_level = prev_level;
181 add_note(wt, MSG_DEBUG,
182 "Found PTK match from list of all known PTKs");
183 write_decrypted_note(wt, decrypted, ptk->ptk.tk,
184 ptk->ptk.tk_len, keyid);
185 return decrypted;
186 }
187 }
188 wpa_debug_level = prev_level;
189
190 return NULL;
191 }
192
193
194 static void check_plaintext_prot(struct wlantest *wt,
195 const struct ieee80211_hdr *hdr,
196 const u8 *data, size_t len)
197 {
198 if (len < 8 + 3 || data[8] != 0xaa || data[9] != 0xaa ||
199 data[10] != 0x03)
200 return;
201
202 add_note(wt, MSG_DEBUG,
203 "Plaintext payload in protected frame");
204 wpa_printf(MSG_INFO, "Plaintext payload in protected frame #%u: A2="
205 MACSTR " seq=%u",
206 wt->frame_num, MAC2STR(hdr->addr2),
207 WLAN_GET_SEQ_SEQ(le_to_host16(hdr->seq_ctrl)));
208 }
209
210
211 static void rx_data_bss_prot_group(struct wlantest *wt,
212 const struct ieee80211_hdr *hdr,
213 size_t hdrlen,
214 const u8 *qos, const u8 *dst, const u8 *src,
215 const u8 *data, size_t len)
216 {
217 struct wlantest_bss *bss;
218 int keyid;
219 u8 *decrypted = NULL;
220 size_t dlen;
221 u8 pn[6];
222 int replay = 0;
223
224 bss = bss_get(wt, hdr->addr2);
225 if (bss == NULL)
226 return;
227 if (len < 4) {
228 add_note(wt, MSG_INFO, "Too short group addressed data frame");
229 return;
230 }
231
232 if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
233 !(data[3] & 0x20)) {
234 add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
235 MACSTR " did not have ExtIV bit set to 1",
236 MAC2STR(bss->bssid));
237 return;
238 }
239
240 if (bss->group_cipher == WPA_CIPHER_TKIP) {
241 if (data[3] & 0x1f) {
242 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
243 " used non-zero reserved bit",
244 MAC2STR(bss->bssid));
245 }
246 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
247 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
248 " used incorrect WEPSeed[1] (was 0x%x, "
249 "expected 0x%x)",
250 MAC2STR(bss->bssid), data[1],
251 (data[0] | 0x20) & 0x7f);
252 }
253 } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
254 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
255 add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
256 " used non-zero reserved bit",
257 MAC2STR(bss->bssid));
258 }
259 }
260
261 check_plaintext_prot(wt, hdr, data, len);
262 keyid = data[3] >> 6;
263 if (bss->gtk_len[keyid] == 0 && bss->group_cipher != WPA_CIPHER_WEP40)
264 {
265 add_note(wt, MSG_MSGDUMP, "No GTK known to decrypt the frame "
266 "(A2=" MACSTR " KeyID=%d)",
267 MAC2STR(hdr->addr2), keyid);
268 return;
269 }
270
271 if (bss->group_cipher == WPA_CIPHER_TKIP)
272 tkip_get_pn(pn, data);
273 else if (bss->group_cipher == WPA_CIPHER_WEP40)
274 goto skip_replay_det;
275 else
276 ccmp_get_pn(pn, data);
277 if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
278 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
279 char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
280
281 wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
282 wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), bss->rsc[keyid], 6);
283 add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
284 " A2=" MACSTR " A3=" MACSTR
285 " seq=%u frag=%u%s keyid=%d %s<=%s",
286 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
287 MAC2STR(hdr->addr3),
288 WLAN_GET_SEQ_SEQ(seq_ctrl),
289 WLAN_GET_SEQ_FRAG(seq_ctrl),
290 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
291 " Retry" : "",
292 keyid, pn_hex, rsc_hex);
293 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
294 wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
295 replay = 1;
296 }
297
298 skip_replay_det:
299 if (bss->group_cipher == WPA_CIPHER_TKIP)
300 decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
301 &dlen);
302 else if (bss->group_cipher == WPA_CIPHER_WEP40)
303 decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
304 else if (bss->group_cipher == WPA_CIPHER_CCMP)
305 decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
306 &dlen);
307 else if (bss->group_cipher == WPA_CIPHER_CCMP_256)
308 decrypted = ccmp_256_decrypt(bss->gtk[keyid], hdr, data, len,
309 &dlen);
310 else if (bss->group_cipher == WPA_CIPHER_GCMP ||
311 bss->group_cipher == WPA_CIPHER_GCMP_256)
312 decrypted = gcmp_decrypt(bss->gtk[keyid], bss->gtk_len[keyid],
313 hdr, data, len, &dlen);
314
315 if (decrypted) {
316 char gtk[65];
317
318 wpa_snprintf_hex(gtk, sizeof(gtk), bss->gtk[keyid],
319 bss->gtk_len[keyid]);
320 add_note(wt, MSG_EXCESSIVE, "GTK[%d] %s", keyid, gtk);
321 rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
322 dlen, 1, NULL);
323 if (!replay)
324 os_memcpy(bss->rsc[keyid], pn, 6);
325 write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
326 decrypted, dlen);
327 } else
328 add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
329 os_free(decrypted);
330 }
331
332
333 static void rx_data_bss_prot(struct wlantest *wt,
334 const struct ieee80211_hdr *hdr, size_t hdrlen,
335 const u8 *qos, const u8 *dst, const u8 *src,
336 const u8 *data, size_t len)
337 {
338 struct wlantest_bss *bss, *bss2;
339 struct wlantest_sta *sta, *sta2;
340 int keyid;
341 u16 fc = le_to_host16(hdr->frame_control);
342 u8 *decrypted;
343 size_t dlen;
344 int tid;
345 u8 pn[6], *rsc;
346 struct wlantest_tdls *tdls = NULL, *found;
347 const u8 *tk = NULL;
348 int ptk_iter_done = 0;
349 int try_ptk_iter = 0;
350 int replay = 0;
351
352 if (hdr->addr1[0] & 0x01) {
353 rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src,
354 data, len);
355 return;
356 }
357
358 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
359 (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
360 bss = bss_find(wt, hdr->addr1);
361 if (bss) {
362 sta = sta_find(bss, hdr->addr2);
363 if (sta) {
364 sta->counters[
365 WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
366 }
367 if (!sta || !sta->ptk_set) {
368 bss2 = bss_find(wt, hdr->addr2);
369 if (bss2) {
370 sta2 = sta_find(bss2, hdr->addr1);
371 if (sta2 && (!sta || sta2->ptk_set)) {
372 bss = bss2;
373 sta = sta2;
374 }
375 }
376 }
377 } else {
378 bss = bss_find(wt, hdr->addr2);
379 if (!bss)
380 return;
381 sta = sta_find(bss, hdr->addr1);
382 }
383 } else if (fc & WLAN_FC_TODS) {
384 bss = bss_get(wt, hdr->addr1);
385 if (bss == NULL)
386 return;
387 sta = sta_get(bss, hdr->addr2);
388 if (sta)
389 sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
390 } else if (fc & WLAN_FC_FROMDS) {
391 bss = bss_get(wt, hdr->addr2);
392 if (bss == NULL)
393 return;
394 sta = sta_get(bss, hdr->addr1);
395 } else {
396 bss = bss_get(wt, hdr->addr3);
397 if (bss == NULL)
398 return;
399 sta = sta_find(bss, hdr->addr2);
400 sta2 = sta_find(bss, hdr->addr1);
401 if (sta == NULL || sta2 == NULL)
402 return;
403 found = NULL;
404 dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
405 {
406 if ((tdls->init == sta && tdls->resp == sta2) ||
407 (tdls->init == sta2 && tdls->resp == sta)) {
408 found = tdls;
409 if (tdls->link_up)
410 break;
411 }
412 }
413 if (found) {
414 if (!found->link_up)
415 add_note(wt, MSG_DEBUG,
416 "TDLS: Link not up, but Data "
417 "frame seen");
418 tk = found->tpk.tk;
419 tdls = found;
420 }
421 }
422 check_plaintext_prot(wt, hdr, data, len);
423 if ((sta == NULL ||
424 (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
425 tk == NULL) {
426 add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
427 if (dl_list_empty(&wt->ptk))
428 return;
429 try_ptk_iter = 1;
430 }
431
432 if (len < 4) {
433 add_note(wt, MSG_INFO, "Too short encrypted data frame");
434 return;
435 }
436
437 if (sta == NULL)
438 return;
439 if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
440 !(data[3] & 0x20)) {
441 add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
442 MACSTR " did not have ExtIV bit set to 1",
443 MAC2STR(src));
444 return;
445 }
446
447 if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
448 if (data[3] & 0x1f) {
449 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
450 " used non-zero reserved bit",
451 MAC2STR(hdr->addr2));
452 }
453 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
454 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
455 " used incorrect WEPSeed[1] (was 0x%x, "
456 "expected 0x%x)",
457 MAC2STR(hdr->addr2), data[1],
458 (data[0] | 0x20) & 0x7f);
459 }
460 } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
461 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
462 add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
463 " used non-zero reserved bit",
464 MAC2STR(hdr->addr2));
465 }
466 }
467
468 keyid = data[3] >> 6;
469 if (keyid != 0) {
470 add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
471 "individually addressed Data frame from " MACSTR,
472 keyid, MAC2STR(hdr->addr2));
473 }
474
475 if (qos) {
476 tid = qos[0] & 0x0f;
477 if (fc & WLAN_FC_TODS)
478 sta->tx_tid[tid]++;
479 else
480 sta->rx_tid[tid]++;
481 } else {
482 tid = 0;
483 if (fc & WLAN_FC_TODS)
484 sta->tx_tid[16]++;
485 else
486 sta->rx_tid[16]++;
487 }
488 if (tk) {
489 if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
490 rsc = tdls->rsc_init[tid];
491 else
492 rsc = tdls->rsc_resp[tid];
493 } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
494 (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
495 if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0)
496 rsc = sta->rsc_tods[tid];
497 else
498 rsc = sta->rsc_fromds[tid];
499 } else if (fc & WLAN_FC_TODS)
500 rsc = sta->rsc_tods[tid];
501 else
502 rsc = sta->rsc_fromds[tid];
503
504
505 if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
506 tkip_get_pn(pn, data);
507 else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
508 goto skip_replay_det;
509 else
510 ccmp_get_pn(pn, data);
511 if (os_memcmp(pn, rsc, 6) <= 0) {
512 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
513 char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
514
515 wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
516 wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), rsc, 6);
517 add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
518 " A2=" MACSTR " A3=" MACSTR
519 " seq=%u frag=%u%s keyid=%d tid=%d %s<=%s",
520 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
521 MAC2STR(hdr->addr3),
522 WLAN_GET_SEQ_SEQ(seq_ctrl),
523 WLAN_GET_SEQ_FRAG(seq_ctrl),
524 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
525 " Retry" : "",
526 keyid, tid, pn_hex, rsc_hex);
527 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
528 wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
529 replay = 1;
530 }
531
532 skip_replay_det:
533 if (tk) {
534 if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) {
535 decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
536 write_decrypted_note(wt, decrypted, tk, 32, keyid);
537 } else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
538 sta->pairwise_cipher == WPA_CIPHER_GCMP_256) {
539 decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data,
540 len, &dlen);
541 write_decrypted_note(wt, decrypted, tk, sta->ptk.tk_len,
542 keyid);
543 } else {
544 decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
545 write_decrypted_note(wt, decrypted, tk, 16, keyid);
546 }
547 } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
548 decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen);
549 write_decrypted_note(wt, decrypted, sta->ptk.tk, 32, keyid);
550 } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
551 decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
552 } else if (sta->ptk_set) {
553 if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
554 decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data,
555 len, &dlen);
556 else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
557 sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
558 decrypted = gcmp_decrypt(sta->ptk.tk, sta->ptk.tk_len,
559 hdr, data, len, &dlen);
560 else
561 decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len,
562 &dlen);
563 write_decrypted_note(wt, decrypted, sta->ptk.tk,
564 sta->ptk.tk_len, keyid);
565 } else {
566 decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
567 data, len, &dlen);
568 ptk_iter_done = 1;
569 }
570 if (!decrypted && !ptk_iter_done) {
571 decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
572 data, len, &dlen);
573 if (decrypted) {
574 add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
575 }
576 }
577 if (!decrypted) {
578 struct wpa_ptk zero_ptk;
579
580 os_memset(&zero_ptk, 0, sizeof(zero_ptk));
581 zero_ptk.tk_len = wpa_cipher_key_len(sta->pairwise_cipher);
582 decrypted = try_ptk(sta->pairwise_cipher, &zero_ptk, hdr,
583 data, len, &dlen);
584 if (decrypted) {
585 add_note(wt, MSG_DEBUG,
586 "Frame was encrypted with zero TK");
587 write_decrypted_note(wt, decrypted, zero_ptk.tk,
588 zero_ptk.tk_len, keyid);
589 }
590 }
591 if (decrypted) {
592 u16 fc = le_to_host16(hdr->frame_control);
593 const u8 *peer_addr = NULL;
594 if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
595 peer_addr = hdr->addr1;
596 if (!replay)
597 os_memcpy(rsc, pn, 6);
598 rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
599 dlen, 1, peer_addr);
600 write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
601 decrypted, dlen);
602 } else {
603 if (!try_ptk_iter)
604 add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
605
606 /* Assume the frame was corrupted and there was no FCS to check.
607 * Allow retry of this particular frame to be processed so that
608 * it could end up getting decrypted if it was received without
609 * corruption. */
610 sta->allow_duplicate = 1;
611 }
612 os_free(decrypted);
613 }
614
615
616 static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
617 size_t hdrlen, const u8 *qos, const u8 *dst,
618 const u8 *src, const u8 *data, size_t len)
619 {
620 u16 fc = le_to_host16(hdr->frame_control);
621 int prot = !!(fc & WLAN_FC_ISWEP);
622
623 if (qos) {
624 u8 ack = (qos[0] & 0x60) >> 5;
625 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
626 " len=%u%s tid=%u%s%s",
627 MAC2STR(src), MAC2STR(dst), (unsigned int) len,
628 prot ? " Prot" : "", qos[0] & 0x0f,
629 (qos[0] & 0x10) ? " EOSP" : "",
630 ack == 0 ? "" :
631 (ack == 1 ? " NoAck" :
632 (ack == 2 ? " NoExpAck" : " BA")));
633 } else {
634 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
635 " len=%u%s",
636 MAC2STR(src), MAC2STR(dst), (unsigned int) len,
637 prot ? " Prot" : "");
638 }
639
640 if (prot)
641 rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
642 else {
643 const u8 *bssid, *sta_addr, *peer_addr;
644 struct wlantest_bss *bss;
645
646 if (fc & WLAN_FC_TODS) {
647 bssid = hdr->addr1;
648 sta_addr = hdr->addr2;
649 peer_addr = NULL;
650 } else if (fc & WLAN_FC_FROMDS) {
651 bssid = hdr->addr2;
652 sta_addr = hdr->addr1;
653 peer_addr = NULL;
654 } else {
655 bssid = hdr->addr3;
656 sta_addr = hdr->addr2;
657 peer_addr = hdr->addr1;
658 }
659
660 bss = bss_get(wt, bssid);
661 if (bss) {
662 struct wlantest_sta *sta = sta_get(bss, sta_addr);
663
664 if (sta) {
665 if (qos) {
666 int tid = qos[0] & 0x0f;
667 if (fc & WLAN_FC_TODS)
668 sta->tx_tid[tid]++;
669 else
670 sta->rx_tid[tid]++;
671 } else {
672 if (fc & WLAN_FC_TODS)
673 sta->tx_tid[16]++;
674 else
675 sta->rx_tid[16]++;
676 }
677 }
678 }
679
680 rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0,
681 peer_addr);
682 }
683 }
684
685
686 static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
687 const u8 *sta1_addr,
688 const u8 *sta2_addr)
689 {
690 struct wlantest_bss *bss;
691 struct wlantest_sta *sta1, *sta2;
692 struct wlantest_tdls *tdls, *found = NULL;
693
694 bss = bss_find(wt, bssid);
695 if (bss == NULL)
696 return NULL;
697 sta1 = sta_find(bss, sta1_addr);
698 if (sta1 == NULL)
699 return NULL;
700 sta2 = sta_find(bss, sta2_addr);
701 if (sta2 == NULL)
702 return NULL;
703
704 dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
705 if ((tdls->init == sta1 && tdls->resp == sta2) ||
706 (tdls->init == sta2 && tdls->resp == sta1)) {
707 found = tdls;
708 if (tdls->link_up)
709 break;
710 }
711 }
712
713 return found;
714 }
715
716
717 static void add_direct_link(struct wlantest *wt, const u8 *bssid,
718 const u8 *sta1_addr, const u8 *sta2_addr)
719 {
720 struct wlantest_tdls *tdls;
721
722 tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
723 if (tdls == NULL)
724 return;
725
726 if (tdls->link_up)
727 tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
728 else
729 tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
730 }
731
732
733 static void add_ap_path(struct wlantest *wt, const u8 *bssid,
734 const u8 *sta1_addr, const u8 *sta2_addr)
735 {
736 struct wlantest_tdls *tdls;
737
738 tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
739 if (tdls == NULL)
740 return;
741
742 if (tdls->link_up)
743 tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
744 else
745 tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
746 }
747
748
749 void rx_data(struct wlantest *wt, const u8 *data, size_t len)
750 {
751 const struct ieee80211_hdr *hdr;
752 u16 fc, stype;
753 size_t hdrlen;
754 const u8 *qos = NULL;
755
756 if (len < 24)
757 return;
758
759 hdr = (const struct ieee80211_hdr *) data;
760 fc = le_to_host16(hdr->frame_control);
761 stype = WLAN_FC_GET_STYPE(fc);
762 hdrlen = 24;
763 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
764 (WLAN_FC_TODS | WLAN_FC_FROMDS))
765 hdrlen += ETH_ALEN;
766 if (stype & 0x08) {
767 qos = data + hdrlen;
768 hdrlen += 2;
769 }
770 if (len < hdrlen)
771 return;
772 wt->rx_data++;
773
774 switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
775 case 0:
776 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
777 MACSTR " BSSID=" MACSTR,
778 data_stype(WLAN_FC_GET_STYPE(fc)),
779 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
780 fc & WLAN_FC_ISWEP ? " Prot" : "",
781 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
782 MAC2STR(hdr->addr3));
783 add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
784 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
785 data + hdrlen, len - hdrlen);
786 break;
787 case WLAN_FC_FROMDS:
788 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
789 " BSSID=" MACSTR " SA=" MACSTR,
790 data_stype(WLAN_FC_GET_STYPE(fc)),
791 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
792 fc & WLAN_FC_ISWEP ? " Prot" : "",
793 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
794 MAC2STR(hdr->addr3));
795 add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
796 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
797 data + hdrlen, len - hdrlen);
798 break;
799 case WLAN_FC_TODS:
800 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
801 " SA=" MACSTR " DA=" MACSTR,
802 data_stype(WLAN_FC_GET_STYPE(fc)),
803 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
804 fc & WLAN_FC_ISWEP ? " Prot" : "",
805 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
806 MAC2STR(hdr->addr3));
807 add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
808 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
809 data + hdrlen, len - hdrlen);
810 break;
811 case WLAN_FC_TODS | WLAN_FC_FROMDS:
812 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
813 MACSTR " DA=" MACSTR " SA=" MACSTR,
814 data_stype(WLAN_FC_GET_STYPE(fc)),
815 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
816 fc & WLAN_FC_ISWEP ? " Prot" : "",
817 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
818 MAC2STR(hdr->addr3),
819 MAC2STR((const u8 *) (hdr + 1)));
820 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
821 data + hdrlen, len - hdrlen);
822 break;
823 }
824 }