]> git.ipfire.org Git - thirdparty/hostap.git/blame - wlantest/rx_data.c
wlantest: Do not report decryption keys when checking only zero TK
[thirdparty/hostap.git] / wlantest / rx_data.c
CommitLineData
2d73f0a8
JM
1/*
2 * Received Data frame processing
98cd3d1c 3 * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
2d73f0a8 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
2d73f0a8
JM
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
a9dd29d3 12#include "common/defs.h"
2d73f0a8
JM
13#include "common/ieee802_11_defs.h"
14#include "wlantest.h"
a0530dff 15
2d73f0a8
JM
16
17static 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
722c7d19
JM
55static 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
60static 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
ee3b84be
JM
78static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
79 const u8 *sta_addr, const u8 *dst, const u8 *src,
244c9303
JM
80 u16 ethertype, const u8 *data, size_t len, int prot,
81 const u8 *peer_addr)
32234bba 82{
ee3b84be
JM
83 switch (ethertype) {
84 case ETH_P_PAE:
6d014ffc 85 rx_data_eapol(wt, bssid, sta_addr, dst, src, data, len, prot);
ee3b84be
JM
86 break;
87 case ETH_P_IP:
244c9303
JM
88 rx_data_ip(wt, bssid, sta_addr, dst, src, data, len,
89 peer_addr);
ee3b84be 90 break;
b39f5834
JM
91 case 0x890d:
92 rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len);
93 break;
722c7d19
JM
94 case ETH_P_8021Q:
95 rx_data_vlan(wt, bssid, sta_addr, dst, src, data, len, prot,
96 peer_addr);
97 break;
ee3b84be 98 }
32234bba
JM
99}
100
101
ee3b84be
JM
102static void rx_data_process(struct wlantest *wt, const u8 *bssid,
103 const u8 *sta_addr,
104 const u8 *dst, const u8 *src,
244c9303
JM
105 const u8 *data, size_t len, int prot,
106 const u8 *peer_addr)
32234bba 107{
4bc82fc7
JM
108 if (len == 0)
109 return;
110
32234bba 111 if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
ee3b84be 112 rx_data_eth(wt, bssid, sta_addr, dst, src,
244c9303
JM
113 WPA_GET_BE16(data + 6), data + 8, len - 8, prot,
114 peer_addr);
32234bba
JM
115 return;
116 }
117
4bc82fc7 118 wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
32234bba
JM
119}
120
121
f5849f1c
JM
122static 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
f5f7286b
JM
135static 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
a0530dff 167static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher,
f5849f1c 168 const struct ieee80211_hdr *hdr, int keyid,
a0530dff
JM
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) {
f5f7286b
JM
177 decrypted = try_ptk(pairwise_cipher, &ptk->ptk, hdr,
178 data, data_len, decrypted_len);
a0530dff
JM
179 if (decrypted) {
180 wpa_debug_level = prev_level;
f5849f1c
JM
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);
a0530dff
JM
185 return decrypted;
186 }
187 }
188 wpa_debug_level = prev_level;
189
190 return NULL;
191}
192
193
80d41221
JM
194static 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
d318c534
JM
211static void rx_data_bss_prot_group(struct wlantest *wt,
212 const struct ieee80211_hdr *hdr,
e45f2e8a 213 size_t hdrlen,
d318c534
JM
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;
eb2223e0 219 u8 *decrypted = NULL;
07d0a5be 220 size_t dlen;
2edd5c23 221 u8 pn[6];
4158b80e 222 int replay = 0;
d318c534
JM
223
224 bss = bss_get(wt, hdr->addr2);
225 if (bss == NULL)
226 return;
227 if (len < 4) {
e4d99217 228 add_note(wt, MSG_INFO, "Too short group addressed data frame");
d318c534
JM
229 return;
230 }
231
20062114
JM
232 if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
233 !(data[3] & 0x20)) {
e4d99217
JM
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;
20062114
JM
238 }
239
16b8b6ea
JM
240 if (bss->group_cipher == WPA_CIPHER_TKIP) {
241 if (data[3] & 0x1f) {
e4d99217
JM
242 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
243 " used non-zero reserved bit",
244 MAC2STR(bss->bssid));
16b8b6ea
JM
245 }
246 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
e4d99217
JM
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);
16b8b6ea
JM
252 }
253 } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
254 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
e4d99217
JM
255 add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
256 " used non-zero reserved bit",
257 MAC2STR(bss->bssid));
16b8b6ea
JM
258 }
259 }
260
80d41221 261 check_plaintext_prot(wt, hdr, data, len);
d318c534 262 keyid = data[3] >> 6;
2e479416
JM
263 if (bss->gtk_len[keyid] == 0 && bss->group_cipher != WPA_CIPHER_WEP40)
264 {
e4d99217
JM
265 add_note(wt, MSG_MSGDUMP, "No GTK known to decrypt the frame "
266 "(A2=" MACSTR " KeyID=%d)",
267 MAC2STR(hdr->addr2), keyid);
d318c534
JM
268 return;
269 }
270
4dac8453
JM
271 if (bss->group_cipher == WPA_CIPHER_TKIP)
272 tkip_get_pn(pn, data);
2e479416
JM
273 else if (bss->group_cipher == WPA_CIPHER_WEP40)
274 goto skip_replay_det;
4dac8453
JM
275 else
276 ccmp_get_pn(pn, data);
2edd5c23 277 if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
01b397dd 278 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
f5849f1c
JM
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",
e4d99217
JM
286 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
287 MAC2STR(hdr->addr3),
288 WLAN_GET_SEQ_SEQ(seq_ctrl),
3a3cb8ee
AKP
289 WLAN_GET_SEQ_FRAG(seq_ctrl),
290 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
f5849f1c
JM
291 " Retry" : "",
292 keyid, pn_hex, rsc_hex);
2edd5c23
JM
293 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
294 wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
4158b80e 295 replay = 1;
2edd5c23
JM
296 }
297
2e479416 298skip_replay_det:
2924b0eb
JM
299 if (bss->group_cipher == WPA_CIPHER_TKIP)
300 decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
301 &dlen);
2e479416
JM
302 else if (bss->group_cipher == WPA_CIPHER_WEP40)
303 decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
eb2223e0 304 else if (bss->group_cipher == WPA_CIPHER_CCMP)
2924b0eb
JM
305 decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
306 &dlen);
eb2223e0
AKP
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
2edd5c23 315 if (decrypted) {
f5849f1c
JM
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);
ee3b84be 321 rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
244c9303 322 dlen, 1, NULL);
4158b80e
JM
323 if (!replay)
324 os_memcpy(bss->rsc[keyid], pn, 6);
e45f2e8a 325 write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
64f45d07 326 decrypted, dlen);
e4d99217 327 } else
0dc58cfa 328 add_note(wt, MSG_DEBUG, "Failed to decrypt frame (group)");
07d0a5be 329 os_free(decrypted);
d318c534
JM
330}
331
332
32234bba 333static void rx_data_bss_prot(struct wlantest *wt,
e45f2e8a
JM
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)
32234bba 337{
aab66128 338 struct wlantest_bss *bss, *bss2;
b39f5834 339 struct wlantest_sta *sta, *sta2;
d318c534
JM
340 int keyid;
341 u16 fc = le_to_host16(hdr->frame_control);
8e467e3c 342 u8 *decrypted = NULL;
d318c534 343 size_t dlen;
2edd5c23 344 int tid;
8e467e3c 345 u8 pn[6], *rsc = NULL;
ace4e460 346 struct wlantest_tdls *tdls = NULL, *found;
b39f5834 347 const u8 *tk = NULL;
a0530dff
JM
348 int ptk_iter_done = 0;
349 int try_ptk_iter = 0;
4158b80e 350 int replay = 0;
0dc58cfa 351 int only_zero_tk = 0;
d318c534
JM
352
353 if (hdr->addr1[0] & 0x01) {
e45f2e8a
JM
354 rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src,
355 data, len);
d318c534
JM
356 return;
357 }
358
e45f2e8a
JM
359 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
360 (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
361 bss = bss_find(wt, hdr->addr1);
362 if (bss) {
363 sta = sta_find(bss, hdr->addr2);
aab66128 364 if (sta) {
e45f2e8a
JM
365 sta->counters[
366 WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
97302b39
JM
367 }
368 if (!sta || !sta->ptk_set) {
aab66128
JM
369 bss2 = bss_find(wt, hdr->addr2);
370 if (bss2) {
97302b39
JM
371 sta2 = sta_find(bss2, hdr->addr1);
372 if (sta2 && (!sta || sta2->ptk_set)) {
aab66128 373 bss = bss2;
97302b39
JM
374 sta = sta2;
375 }
aab66128
JM
376 }
377 }
e45f2e8a
JM
378 } else {
379 bss = bss_find(wt, hdr->addr2);
380 if (!bss)
381 return;
382 sta = sta_find(bss, hdr->addr1);
383 }
384 } else if (fc & WLAN_FC_TODS) {
d318c534
JM
385 bss = bss_get(wt, hdr->addr1);
386 if (bss == NULL)
387 return;
388 sta = sta_get(bss, hdr->addr2);
0a9ddd92
JM
389 if (sta)
390 sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
b39f5834 391 } else if (fc & WLAN_FC_FROMDS) {
d318c534
JM
392 bss = bss_get(wt, hdr->addr2);
393 if (bss == NULL)
394 return;
395 sta = sta_get(bss, hdr->addr1);
b39f5834
JM
396 } else {
397 bss = bss_get(wt, hdr->addr3);
398 if (bss == NULL)
399 return;
400 sta = sta_find(bss, hdr->addr2);
401 sta2 = sta_find(bss, hdr->addr1);
402 if (sta == NULL || sta2 == NULL)
403 return;
ace4e460 404 found = NULL;
b39f5834
JM
405 dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
406 {
407 if ((tdls->init == sta && tdls->resp == sta2) ||
408 (tdls->init == sta2 && tdls->resp == sta)) {
ace4e460
JM
409 found = tdls;
410 if (tdls->link_up)
411 break;
b39f5834
JM
412 }
413 }
ace4e460
JM
414 if (found) {
415 if (!found->link_up)
416 add_note(wt, MSG_DEBUG,
417 "TDLS: Link not up, but Data "
418 "frame seen");
419 tk = found->tpk.tk;
420 tdls = found;
421 }
d318c534 422 }
80d41221 423 check_plaintext_prot(wt, hdr, data, len);
2e479416
JM
424 if ((sta == NULL ||
425 (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
426 tk == NULL) {
e4d99217 427 add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
8e467e3c
JM
428 if (dl_list_empty(&wt->ptk)) {
429 if (len >= 4 && sta) {
430 keyid = data[3] >> 6;
0dc58cfa 431 only_zero_tk = 1;
8e467e3c
JM
432 goto check_zero_tk;
433 }
a0530dff 434 return;
8e467e3c
JM
435 }
436
a0530dff 437 try_ptk_iter = 1;
d318c534
JM
438 }
439
440 if (len < 4) {
e4d99217 441 add_note(wt, MSG_INFO, "Too short encrypted data frame");
d318c534
JM
442 return;
443 }
444
99d7c1de
JM
445 if (sta == NULL)
446 return;
20062114
JM
447 if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
448 !(data[3] & 0x20)) {
e4d99217
JM
449 add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
450 MACSTR " did not have ExtIV bit set to 1",
451 MAC2STR(src));
452 return;
20062114
JM
453 }
454
b39f5834 455 if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
16b8b6ea 456 if (data[3] & 0x1f) {
e4d99217
JM
457 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
458 " used non-zero reserved bit",
459 MAC2STR(hdr->addr2));
16b8b6ea
JM
460 }
461 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
e4d99217
JM
462 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
463 " used incorrect WEPSeed[1] (was 0x%x, "
464 "expected 0x%x)",
465 MAC2STR(hdr->addr2), data[1],
466 (data[0] | 0x20) & 0x7f);
16b8b6ea 467 }
b39f5834 468 } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
16b8b6ea 469 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
e4d99217
JM
470 add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
471 " used non-zero reserved bit",
472 MAC2STR(hdr->addr2));
16b8b6ea
JM
473 }
474 }
475
d318c534 476 keyid = data[3] >> 6;
6ea7a152
AW
477 if (keyid != 0 &&
478 (!(sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST) ||
479 !(bss->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST) ||
480 keyid != 1)) {
481 add_note(wt, MSG_INFO,
482 "Unexpected KeyID %d in individually addressed Data frame from "
483 MACSTR,
e4d99217 484 keyid, MAC2STR(hdr->addr2));
d318c534
JM
485 }
486
99d7c1de 487 if (qos) {
2edd5c23 488 tid = qos[0] & 0x0f;
99d7c1de
JM
489 if (fc & WLAN_FC_TODS)
490 sta->tx_tid[tid]++;
491 else
492 sta->rx_tid[tid]++;
493 } else {
2edd5c23 494 tid = 0;
99d7c1de
JM
495 if (fc & WLAN_FC_TODS)
496 sta->tx_tid[16]++;
497 else
498 sta->rx_tid[16]++;
499 }
b39f5834
JM
500 if (tk) {
501 if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
502 rsc = tdls->rsc_init[tid];
503 else
504 rsc = tdls->rsc_resp[tid];
aab66128
JM
505 } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
506 (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
507 if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0)
508 rsc = sta->rsc_tods[tid];
509 else
510 rsc = sta->rsc_fromds[tid];
b39f5834 511 } else if (fc & WLAN_FC_TODS)
2edd5c23
JM
512 rsc = sta->rsc_tods[tid];
513 else
514 rsc = sta->rsc_fromds[tid];
515
516
b39f5834 517 if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
4dac8453 518 tkip_get_pn(pn, data);
2e479416
JM
519 else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
520 goto skip_replay_det;
4dac8453
JM
521 else
522 ccmp_get_pn(pn, data);
2edd5c23 523 if (os_memcmp(pn, rsc, 6) <= 0) {
01b397dd 524 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
f5849f1c
JM
525 char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
526
527 wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
528 wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), rsc, 6);
529 add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
530 " A2=" MACSTR " A3=" MACSTR
531 " seq=%u frag=%u%s keyid=%d tid=%d %s<=%s",
e4d99217
JM
532 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
533 MAC2STR(hdr->addr3),
534 WLAN_GET_SEQ_SEQ(seq_ctrl),
3a3cb8ee
AKP
535 WLAN_GET_SEQ_FRAG(seq_ctrl),
536 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
f5849f1c
JM
537 " Retry" : "",
538 keyid, tid, pn_hex, rsc_hex);
2edd5c23
JM
539 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
540 wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
4158b80e 541 replay = 1;
2edd5c23
JM
542 }
543
2e479416 544skip_replay_det:
eb2223e0 545 if (tk) {
f5849f1c 546 if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) {
eb2223e0 547 decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
f5849f1c
JM
548 write_decrypted_note(wt, decrypted, tk, 32, keyid);
549 } else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
550 sta->pairwise_cipher == WPA_CIPHER_GCMP_256) {
5420bcf4 551 decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data,
eb2223e0 552 len, &dlen);
f5849f1c
JM
553 write_decrypted_note(wt, decrypted, tk, sta->ptk.tk_len,
554 keyid);
555 } else {
eb2223e0 556 decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
f5849f1c
JM
557 write_decrypted_note(wt, decrypted, tk, 16, keyid);
558 }
eb2223e0 559 } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
98cd3d1c 560 decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen);
f5849f1c 561 write_decrypted_note(wt, decrypted, sta->ptk.tk, 32, keyid);
eb2223e0 562 } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
2e479416 563 decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
eb2223e0
AKP
564 } else if (sta->ptk_set) {
565 if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
98cd3d1c 566 decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data,
eb2223e0
AKP
567 len, &dlen);
568 else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
569 sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
5420bcf4 570 decrypted = gcmp_decrypt(sta->ptk.tk, sta->ptk.tk_len,
eb2223e0
AKP
571 hdr, data, len, &dlen);
572 else
98cd3d1c 573 decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len,
eb2223e0 574 &dlen);
f5849f1c
JM
575 write_decrypted_note(wt, decrypted, sta->ptk.tk,
576 sta->ptk.tk_len, keyid);
eb2223e0 577 } else {
f5849f1c
JM
578 decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
579 data, len, &dlen);
a0530dff
JM
580 ptk_iter_done = 1;
581 }
582 if (!decrypted && !ptk_iter_done) {
f5849f1c
JM
583 decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
584 data, len, &dlen);
a0530dff
JM
585 if (decrypted) {
586 add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
587 }
588 }
8e467e3c 589check_zero_tk:
f5f7286b
JM
590 if (!decrypted) {
591 struct wpa_ptk zero_ptk;
8e467e3c 592 int old_debug_level = wpa_debug_level;
f5f7286b
JM
593
594 os_memset(&zero_ptk, 0, sizeof(zero_ptk));
595 zero_ptk.tk_len = wpa_cipher_key_len(sta->pairwise_cipher);
8e467e3c 596 wpa_debug_level = MSG_ERROR;
f5f7286b
JM
597 decrypted = try_ptk(sta->pairwise_cipher, &zero_ptk, hdr,
598 data, len, &dlen);
8e467e3c 599 wpa_debug_level = old_debug_level;
f5f7286b
JM
600 if (decrypted) {
601 add_note(wt, MSG_DEBUG,
602 "Frame was encrypted with zero TK");
8e467e3c
JM
603 wpa_printf(MSG_INFO, "Zero TK used in frame #%u: A2="
604 MACSTR " seq=%u",
605 wt->frame_num, MAC2STR(hdr->addr2),
606 WLAN_GET_SEQ_SEQ(
607 le_to_host16(hdr->seq_ctrl)));
f5f7286b
JM
608 write_decrypted_note(wt, decrypted, zero_ptk.tk,
609 zero_ptk.tk_len, keyid);
610 }
611 }
2edd5c23 612 if (decrypted) {
244c9303 613 u16 fc = le_to_host16(hdr->frame_control);
9559a7f8 614 const u8 *peer_addr = NULL;
244c9303
JM
615 if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
616 peer_addr = hdr->addr1;
8e467e3c 617 if (!replay && rsc)
4158b80e 618 os_memcpy(rsc, pn, 6);
ee3b84be 619 rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
244c9303 620 dlen, 1, peer_addr);
e45f2e8a 621 write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
64f45d07 622 decrypted, dlen);
b3c43c3c 623 } else {
0dc58cfa 624 if (!try_ptk_iter && !only_zero_tk)
b3c43c3c
JM
625 add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
626
627 /* Assume the frame was corrupted and there was no FCS to check.
628 * Allow retry of this particular frame to be processed so that
629 * it could end up getting decrypted if it was received without
630 * corruption. */
631 sta->allow_duplicate = 1;
632 }
d318c534 633 os_free(decrypted);
32234bba
JM
634}
635
636
637static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
e45f2e8a
JM
638 size_t hdrlen, const u8 *qos, const u8 *dst,
639 const u8 *src, const u8 *data, size_t len)
32234bba
JM
640{
641 u16 fc = le_to_host16(hdr->frame_control);
642 int prot = !!(fc & WLAN_FC_ISWEP);
643
644 if (qos) {
645 u8 ack = (qos[0] & 0x60) >> 5;
646 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
647 " len=%u%s tid=%u%s%s",
648 MAC2STR(src), MAC2STR(dst), (unsigned int) len,
649 prot ? " Prot" : "", qos[0] & 0x0f,
650 (qos[0] & 0x10) ? " EOSP" : "",
651 ack == 0 ? "" :
652 (ack == 1 ? " NoAck" :
653 (ack == 2 ? " NoExpAck" : " BA")));
654 } else {
655 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
656 " len=%u%s",
657 MAC2STR(src), MAC2STR(dst), (unsigned int) len,
658 prot ? " Prot" : "");
659 }
660
661 if (prot)
e45f2e8a 662 rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
ee3b84be 663 else {
244c9303 664 const u8 *bssid, *sta_addr, *peer_addr;
99d7c1de
JM
665 struct wlantest_bss *bss;
666
ee3b84be
JM
667 if (fc & WLAN_FC_TODS) {
668 bssid = hdr->addr1;
669 sta_addr = hdr->addr2;
244c9303
JM
670 peer_addr = NULL;
671 } else if (fc & WLAN_FC_FROMDS) {
ee3b84be
JM
672 bssid = hdr->addr2;
673 sta_addr = hdr->addr1;
244c9303
JM
674 peer_addr = NULL;
675 } else {
676 bssid = hdr->addr3;
677 sta_addr = hdr->addr2;
678 peer_addr = hdr->addr1;
ee3b84be 679 }
99d7c1de
JM
680
681 bss = bss_get(wt, bssid);
682 if (bss) {
683 struct wlantest_sta *sta = sta_get(bss, sta_addr);
684
685 if (sta) {
686 if (qos) {
687 int tid = qos[0] & 0x0f;
688 if (fc & WLAN_FC_TODS)
689 sta->tx_tid[tid]++;
690 else
691 sta->rx_tid[tid]++;
692 } else {
693 if (fc & WLAN_FC_TODS)
694 sta->tx_tid[16]++;
695 else
696 sta->rx_tid[16]++;
697 }
698 }
699 }
700
244c9303
JM
701 rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0,
702 peer_addr);
ee3b84be 703 }
32234bba
JM
704}
705
706
0e42fff3
JM
707static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
708 const u8 *sta1_addr,
709 const u8 *sta2_addr)
710{
711 struct wlantest_bss *bss;
712 struct wlantest_sta *sta1, *sta2;
ace4e460 713 struct wlantest_tdls *tdls, *found = NULL;
0e42fff3
JM
714
715 bss = bss_find(wt, bssid);
716 if (bss == NULL)
717 return NULL;
718 sta1 = sta_find(bss, sta1_addr);
719 if (sta1 == NULL)
720 return NULL;
721 sta2 = sta_find(bss, sta2_addr);
722 if (sta2 == NULL)
723 return NULL;
724
725 dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
726 if ((tdls->init == sta1 && tdls->resp == sta2) ||
ace4e460
JM
727 (tdls->init == sta2 && tdls->resp == sta1)) {
728 found = tdls;
729 if (tdls->link_up)
730 break;
731 }
0e42fff3
JM
732 }
733
ace4e460 734 return found;
0e42fff3
JM
735}
736
737
738static void add_direct_link(struct wlantest *wt, const u8 *bssid,
739 const u8 *sta1_addr, const u8 *sta2_addr)
740{
741 struct wlantest_tdls *tdls;
742
743 tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
744 if (tdls == NULL)
745 return;
746
747 if (tdls->link_up)
748 tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
749 else
750 tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
751}
752
753
754static void add_ap_path(struct wlantest *wt, const u8 *bssid,
755 const u8 *sta1_addr, const u8 *sta2_addr)
756{
757 struct wlantest_tdls *tdls;
758
759 tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
760 if (tdls == NULL)
761 return;
762
763 if (tdls->link_up)
764 tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
765 else
766 tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
767}
768
769
2d73f0a8
JM
770void rx_data(struct wlantest *wt, const u8 *data, size_t len)
771{
772 const struct ieee80211_hdr *hdr;
32234bba
JM
773 u16 fc, stype;
774 size_t hdrlen;
775 const u8 *qos = NULL;
2d73f0a8
JM
776
777 if (len < 24)
778 return;
779
780 hdr = (const struct ieee80211_hdr *) data;
781 fc = le_to_host16(hdr->frame_control);
32234bba
JM
782 stype = WLAN_FC_GET_STYPE(fc);
783 hdrlen = 24;
784 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
785 (WLAN_FC_TODS | WLAN_FC_FROMDS))
786 hdrlen += ETH_ALEN;
787 if (stype & 0x08) {
788 qos = data + hdrlen;
789 hdrlen += 2;
790 }
791 if (len < hdrlen)
792 return;
2d73f0a8
JM
793 wt->rx_data++;
794
795 switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
796 case 0:
797 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
798 MACSTR " BSSID=" MACSTR,
799 data_stype(WLAN_FC_GET_STYPE(fc)),
800 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
801 fc & WLAN_FC_ISWEP ? " Prot" : "",
802 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
803 MAC2STR(hdr->addr3));
0e42fff3 804 add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
e45f2e8a 805 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
b39f5834 806 data + hdrlen, len - hdrlen);
2d73f0a8
JM
807 break;
808 case WLAN_FC_FROMDS:
809 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
810 " BSSID=" MACSTR " SA=" MACSTR,
811 data_stype(WLAN_FC_GET_STYPE(fc)),
812 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
813 fc & WLAN_FC_ISWEP ? " Prot" : "",
814 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
815 MAC2STR(hdr->addr3));
0e42fff3 816 add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
e45f2e8a 817 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
32234bba 818 data + hdrlen, len - hdrlen);
2d73f0a8
JM
819 break;
820 case WLAN_FC_TODS:
821 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
822 " SA=" MACSTR " DA=" MACSTR,
823 data_stype(WLAN_FC_GET_STYPE(fc)),
824 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
825 fc & WLAN_FC_ISWEP ? " Prot" : "",
826 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
827 MAC2STR(hdr->addr3));
0e42fff3 828 add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
e45f2e8a 829 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
32234bba 830 data + hdrlen, len - hdrlen);
2d73f0a8
JM
831 break;
832 case WLAN_FC_TODS | WLAN_FC_FROMDS:
833 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
834 MACSTR " DA=" MACSTR " SA=" MACSTR,
835 data_stype(WLAN_FC_GET_STYPE(fc)),
836 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
837 fc & WLAN_FC_ISWEP ? " Prot" : "",
838 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
839 MAC2STR(hdr->addr3),
840 MAC2STR((const u8 *) (hdr + 1)));
e45f2e8a
JM
841 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
842 data + hdrlen, len - hdrlen);
2d73f0a8
JM
843 break;
844 }
845}