]>
Commit | Line | Data |
---|---|---|
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 | |
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 | ||
ee3b84be JM |
55 | static void rx_data_eth(struct wlantest *wt, const u8 *bssid, |
56 | const u8 *sta_addr, const u8 *dst, const u8 *src, | |
244c9303 JM |
57 | u16 ethertype, const u8 *data, size_t len, int prot, |
58 | const u8 *peer_addr) | |
32234bba | 59 | { |
ee3b84be JM |
60 | switch (ethertype) { |
61 | case ETH_P_PAE: | |
6d014ffc | 62 | rx_data_eapol(wt, bssid, sta_addr, dst, src, data, len, prot); |
ee3b84be JM |
63 | break; |
64 | case ETH_P_IP: | |
244c9303 JM |
65 | rx_data_ip(wt, bssid, sta_addr, dst, src, data, len, |
66 | peer_addr); | |
ee3b84be | 67 | break; |
b39f5834 JM |
68 | case 0x890d: |
69 | rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len); | |
70 | break; | |
ee3b84be | 71 | } |
32234bba JM |
72 | } |
73 | ||
74 | ||
ee3b84be JM |
75 | static void rx_data_process(struct wlantest *wt, const u8 *bssid, |
76 | const u8 *sta_addr, | |
77 | const u8 *dst, const u8 *src, | |
244c9303 JM |
78 | const u8 *data, size_t len, int prot, |
79 | const u8 *peer_addr) | |
32234bba | 80 | { |
4bc82fc7 JM |
81 | if (len == 0) |
82 | return; | |
83 | ||
32234bba | 84 | if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) { |
ee3b84be | 85 | rx_data_eth(wt, bssid, sta_addr, dst, src, |
244c9303 JM |
86 | WPA_GET_BE16(data + 6), data + 8, len - 8, prot, |
87 | peer_addr); | |
32234bba JM |
88 | return; |
89 | } | |
90 | ||
4bc82fc7 | 91 | wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len); |
32234bba JM |
92 | } |
93 | ||
94 | ||
a0530dff JM |
95 | static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher, |
96 | const struct ieee80211_hdr *hdr, | |
97 | const u8 *data, size_t data_len, size_t *decrypted_len) | |
98 | { | |
99 | struct wlantest_ptk *ptk; | |
100 | u8 *decrypted; | |
101 | int prev_level = wpa_debug_level; | |
102 | ||
103 | wpa_debug_level = MSG_WARNING; | |
104 | dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) { | |
eb2223e0 | 105 | unsigned int tk_len = ptk->ptk_len - 32; |
a0530dff JM |
106 | decrypted = NULL; |
107 | if ((pairwise_cipher == WPA_CIPHER_CCMP || | |
eb2223e0 | 108 | pairwise_cipher == 0) && tk_len == 16) { |
98cd3d1c | 109 | decrypted = ccmp_decrypt(ptk->ptk.tk, hdr, data, |
a0530dff | 110 | data_len, decrypted_len); |
eb2223e0 AKP |
111 | } else if ((pairwise_cipher == WPA_CIPHER_CCMP_256 || |
112 | pairwise_cipher == 0) && tk_len == 32) { | |
98cd3d1c | 113 | decrypted = ccmp_256_decrypt(ptk->ptk.tk, hdr, data, |
eb2223e0 AKP |
114 | data_len, decrypted_len); |
115 | } else if ((pairwise_cipher == WPA_CIPHER_GCMP || | |
116 | pairwise_cipher == WPA_CIPHER_GCMP_256 || | |
117 | pairwise_cipher == 0) && | |
118 | (tk_len == 16 || tk_len == 32)) { | |
98cd3d1c | 119 | decrypted = gcmp_decrypt(ptk->ptk.tk, tk_len, hdr, |
eb2223e0 AKP |
120 | data, data_len, decrypted_len); |
121 | } else if ((pairwise_cipher == WPA_CIPHER_TKIP || | |
122 | pairwise_cipher == 0) && tk_len == 32) { | |
98cd3d1c | 123 | decrypted = tkip_decrypt(ptk->ptk.tk, hdr, data, |
a0530dff JM |
124 | data_len, decrypted_len); |
125 | } | |
126 | if (decrypted) { | |
127 | wpa_debug_level = prev_level; | |
128 | add_note(wt, MSG_DEBUG, "Found PTK match from list of all known PTKs"); | |
129 | return decrypted; | |
130 | } | |
131 | } | |
132 | wpa_debug_level = prev_level; | |
133 | ||
134 | return NULL; | |
135 | } | |
136 | ||
137 | ||
d318c534 JM |
138 | static void rx_data_bss_prot_group(struct wlantest *wt, |
139 | const struct ieee80211_hdr *hdr, | |
e45f2e8a | 140 | size_t hdrlen, |
d318c534 JM |
141 | const u8 *qos, const u8 *dst, const u8 *src, |
142 | const u8 *data, size_t len) | |
143 | { | |
144 | struct wlantest_bss *bss; | |
145 | int keyid; | |
eb2223e0 | 146 | u8 *decrypted = NULL; |
07d0a5be | 147 | size_t dlen; |
2edd5c23 | 148 | u8 pn[6]; |
4158b80e | 149 | int replay = 0; |
d318c534 JM |
150 | |
151 | bss = bss_get(wt, hdr->addr2); | |
152 | if (bss == NULL) | |
153 | return; | |
154 | if (len < 4) { | |
e4d99217 | 155 | add_note(wt, MSG_INFO, "Too short group addressed data frame"); |
d318c534 JM |
156 | return; |
157 | } | |
158 | ||
20062114 JM |
159 | if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) && |
160 | !(data[3] & 0x20)) { | |
e4d99217 JM |
161 | add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from " |
162 | MACSTR " did not have ExtIV bit set to 1", | |
163 | MAC2STR(bss->bssid)); | |
164 | return; | |
20062114 JM |
165 | } |
166 | ||
16b8b6ea JM |
167 | if (bss->group_cipher == WPA_CIPHER_TKIP) { |
168 | if (data[3] & 0x1f) { | |
e4d99217 JM |
169 | add_note(wt, MSG_INFO, "TKIP frame from " MACSTR |
170 | " used non-zero reserved bit", | |
171 | MAC2STR(bss->bssid)); | |
16b8b6ea JM |
172 | } |
173 | if (data[1] != ((data[0] | 0x20) & 0x7f)) { | |
e4d99217 JM |
174 | add_note(wt, MSG_INFO, "TKIP frame from " MACSTR |
175 | " used incorrect WEPSeed[1] (was 0x%x, " | |
176 | "expected 0x%x)", | |
177 | MAC2STR(bss->bssid), data[1], | |
178 | (data[0] | 0x20) & 0x7f); | |
16b8b6ea JM |
179 | } |
180 | } else if (bss->group_cipher == WPA_CIPHER_CCMP) { | |
181 | if (data[2] != 0 || (data[3] & 0x1f) != 0) { | |
e4d99217 JM |
182 | add_note(wt, MSG_INFO, "CCMP frame from " MACSTR |
183 | " used non-zero reserved bit", | |
184 | MAC2STR(bss->bssid)); | |
16b8b6ea JM |
185 | } |
186 | } | |
187 | ||
d318c534 | 188 | keyid = data[3] >> 6; |
2e479416 JM |
189 | if (bss->gtk_len[keyid] == 0 && bss->group_cipher != WPA_CIPHER_WEP40) |
190 | { | |
e4d99217 JM |
191 | add_note(wt, MSG_MSGDUMP, "No GTK known to decrypt the frame " |
192 | "(A2=" MACSTR " KeyID=%d)", | |
193 | MAC2STR(hdr->addr2), keyid); | |
d318c534 JM |
194 | return; |
195 | } | |
196 | ||
4dac8453 JM |
197 | if (bss->group_cipher == WPA_CIPHER_TKIP) |
198 | tkip_get_pn(pn, data); | |
2e479416 JM |
199 | else if (bss->group_cipher == WPA_CIPHER_WEP40) |
200 | goto skip_replay_det; | |
4dac8453 JM |
201 | else |
202 | ccmp_get_pn(pn, data); | |
2edd5c23 | 203 | if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) { |
01b397dd | 204 | u16 seq_ctrl = le_to_host16(hdr->seq_ctrl); |
e4d99217 | 205 | add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR |
3a3cb8ee | 206 | " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s", |
e4d99217 JM |
207 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), |
208 | MAC2STR(hdr->addr3), | |
209 | WLAN_GET_SEQ_SEQ(seq_ctrl), | |
3a3cb8ee AKP |
210 | WLAN_GET_SEQ_FRAG(seq_ctrl), |
211 | (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ? | |
212 | " Retry" : ""); | |
2edd5c23 JM |
213 | wpa_hexdump(MSG_INFO, "RX PN", pn, 6); |
214 | wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6); | |
4158b80e | 215 | replay = 1; |
2edd5c23 JM |
216 | } |
217 | ||
2e479416 | 218 | skip_replay_det: |
2924b0eb JM |
219 | if (bss->group_cipher == WPA_CIPHER_TKIP) |
220 | decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len, | |
221 | &dlen); | |
2e479416 JM |
222 | else if (bss->group_cipher == WPA_CIPHER_WEP40) |
223 | decrypted = wep_decrypt(wt, hdr, data, len, &dlen); | |
eb2223e0 | 224 | else if (bss->group_cipher == WPA_CIPHER_CCMP) |
2924b0eb JM |
225 | decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len, |
226 | &dlen); | |
eb2223e0 AKP |
227 | else if (bss->group_cipher == WPA_CIPHER_CCMP_256) |
228 | decrypted = ccmp_256_decrypt(bss->gtk[keyid], hdr, data, len, | |
229 | &dlen); | |
230 | else if (bss->group_cipher == WPA_CIPHER_GCMP || | |
231 | bss->group_cipher == WPA_CIPHER_GCMP_256) | |
232 | decrypted = gcmp_decrypt(bss->gtk[keyid], bss->gtk_len[keyid], | |
233 | hdr, data, len, &dlen); | |
234 | ||
2edd5c23 | 235 | if (decrypted) { |
ee3b84be | 236 | rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted, |
244c9303 | 237 | dlen, 1, NULL); |
4158b80e JM |
238 | if (!replay) |
239 | os_memcpy(bss->rsc[keyid], pn, 6); | |
e45f2e8a | 240 | write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen, |
64f45d07 | 241 | decrypted, dlen); |
e4d99217 JM |
242 | } else |
243 | add_note(wt, MSG_DEBUG, "Failed to decrypt frame"); | |
07d0a5be | 244 | os_free(decrypted); |
d318c534 JM |
245 | } |
246 | ||
247 | ||
32234bba | 248 | static void rx_data_bss_prot(struct wlantest *wt, |
e45f2e8a JM |
249 | const struct ieee80211_hdr *hdr, size_t hdrlen, |
250 | const u8 *qos, const u8 *dst, const u8 *src, | |
251 | const u8 *data, size_t len) | |
32234bba | 252 | { |
aab66128 | 253 | struct wlantest_bss *bss, *bss2; |
b39f5834 | 254 | struct wlantest_sta *sta, *sta2; |
d318c534 JM |
255 | int keyid; |
256 | u16 fc = le_to_host16(hdr->frame_control); | |
257 | u8 *decrypted; | |
258 | size_t dlen; | |
2edd5c23 JM |
259 | int tid; |
260 | u8 pn[6], *rsc; | |
ace4e460 | 261 | struct wlantest_tdls *tdls = NULL, *found; |
b39f5834 | 262 | const u8 *tk = NULL; |
a0530dff JM |
263 | int ptk_iter_done = 0; |
264 | int try_ptk_iter = 0; | |
4158b80e | 265 | int replay = 0; |
d318c534 JM |
266 | |
267 | if (hdr->addr1[0] & 0x01) { | |
e45f2e8a JM |
268 | rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src, |
269 | data, len); | |
d318c534 JM |
270 | return; |
271 | } | |
272 | ||
e45f2e8a JM |
273 | if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == |
274 | (WLAN_FC_TODS | WLAN_FC_FROMDS)) { | |
275 | bss = bss_find(wt, hdr->addr1); | |
276 | if (bss) { | |
277 | sta = sta_find(bss, hdr->addr2); | |
aab66128 | 278 | if (sta) { |
e45f2e8a JM |
279 | sta->counters[ |
280 | WLANTEST_STA_COUNTER_PROT_DATA_TX]++; | |
97302b39 JM |
281 | } |
282 | if (!sta || !sta->ptk_set) { | |
aab66128 JM |
283 | bss2 = bss_find(wt, hdr->addr2); |
284 | if (bss2) { | |
97302b39 JM |
285 | sta2 = sta_find(bss2, hdr->addr1); |
286 | if (sta2 && (!sta || sta2->ptk_set)) { | |
aab66128 | 287 | bss = bss2; |
97302b39 JM |
288 | sta = sta2; |
289 | } | |
aab66128 JM |
290 | } |
291 | } | |
e45f2e8a JM |
292 | } else { |
293 | bss = bss_find(wt, hdr->addr2); | |
294 | if (!bss) | |
295 | return; | |
296 | sta = sta_find(bss, hdr->addr1); | |
297 | } | |
298 | } else if (fc & WLAN_FC_TODS) { | |
d318c534 JM |
299 | bss = bss_get(wt, hdr->addr1); |
300 | if (bss == NULL) | |
301 | return; | |
302 | sta = sta_get(bss, hdr->addr2); | |
0a9ddd92 JM |
303 | if (sta) |
304 | sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++; | |
b39f5834 | 305 | } else if (fc & WLAN_FC_FROMDS) { |
d318c534 JM |
306 | bss = bss_get(wt, hdr->addr2); |
307 | if (bss == NULL) | |
308 | return; | |
309 | sta = sta_get(bss, hdr->addr1); | |
b39f5834 JM |
310 | } else { |
311 | bss = bss_get(wt, hdr->addr3); | |
312 | if (bss == NULL) | |
313 | return; | |
314 | sta = sta_find(bss, hdr->addr2); | |
315 | sta2 = sta_find(bss, hdr->addr1); | |
316 | if (sta == NULL || sta2 == NULL) | |
317 | return; | |
ace4e460 | 318 | found = NULL; |
b39f5834 JM |
319 | dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) |
320 | { | |
321 | if ((tdls->init == sta && tdls->resp == sta2) || | |
322 | (tdls->init == sta2 && tdls->resp == sta)) { | |
ace4e460 JM |
323 | found = tdls; |
324 | if (tdls->link_up) | |
325 | break; | |
b39f5834 JM |
326 | } |
327 | } | |
ace4e460 JM |
328 | if (found) { |
329 | if (!found->link_up) | |
330 | add_note(wt, MSG_DEBUG, | |
331 | "TDLS: Link not up, but Data " | |
332 | "frame seen"); | |
333 | tk = found->tpk.tk; | |
334 | tdls = found; | |
335 | } | |
d318c534 | 336 | } |
2e479416 JM |
337 | if ((sta == NULL || |
338 | (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) && | |
339 | tk == NULL) { | |
e4d99217 | 340 | add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame"); |
a0530dff JM |
341 | if (dl_list_empty(&wt->ptk)) |
342 | return; | |
343 | try_ptk_iter = 1; | |
d318c534 JM |
344 | } |
345 | ||
346 | if (len < 4) { | |
e4d99217 | 347 | add_note(wt, MSG_INFO, "Too short encrypted data frame"); |
d318c534 JM |
348 | return; |
349 | } | |
350 | ||
99d7c1de JM |
351 | if (sta == NULL) |
352 | return; | |
20062114 JM |
353 | if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) && |
354 | !(data[3] & 0x20)) { | |
e4d99217 JM |
355 | add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from " |
356 | MACSTR " did not have ExtIV bit set to 1", | |
357 | MAC2STR(src)); | |
358 | return; | |
20062114 JM |
359 | } |
360 | ||
b39f5834 | 361 | if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) { |
16b8b6ea | 362 | if (data[3] & 0x1f) { |
e4d99217 JM |
363 | add_note(wt, MSG_INFO, "TKIP frame from " MACSTR |
364 | " used non-zero reserved bit", | |
365 | MAC2STR(hdr->addr2)); | |
16b8b6ea JM |
366 | } |
367 | if (data[1] != ((data[0] | 0x20) & 0x7f)) { | |
e4d99217 JM |
368 | add_note(wt, MSG_INFO, "TKIP frame from " MACSTR |
369 | " used incorrect WEPSeed[1] (was 0x%x, " | |
370 | "expected 0x%x)", | |
371 | MAC2STR(hdr->addr2), data[1], | |
372 | (data[0] | 0x20) & 0x7f); | |
16b8b6ea | 373 | } |
b39f5834 | 374 | } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) { |
16b8b6ea | 375 | if (data[2] != 0 || (data[3] & 0x1f) != 0) { |
e4d99217 JM |
376 | add_note(wt, MSG_INFO, "CCMP frame from " MACSTR |
377 | " used non-zero reserved bit", | |
378 | MAC2STR(hdr->addr2)); | |
16b8b6ea JM |
379 | } |
380 | } | |
381 | ||
d318c534 JM |
382 | keyid = data[3] >> 6; |
383 | if (keyid != 0) { | |
e4d99217 JM |
384 | add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in " |
385 | "individually addressed Data frame from " MACSTR, | |
386 | keyid, MAC2STR(hdr->addr2)); | |
d318c534 JM |
387 | } |
388 | ||
99d7c1de | 389 | if (qos) { |
2edd5c23 | 390 | tid = qos[0] & 0x0f; |
99d7c1de JM |
391 | if (fc & WLAN_FC_TODS) |
392 | sta->tx_tid[tid]++; | |
393 | else | |
394 | sta->rx_tid[tid]++; | |
395 | } else { | |
2edd5c23 | 396 | tid = 0; |
99d7c1de JM |
397 | if (fc & WLAN_FC_TODS) |
398 | sta->tx_tid[16]++; | |
399 | else | |
400 | sta->rx_tid[16]++; | |
401 | } | |
b39f5834 JM |
402 | if (tk) { |
403 | if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0) | |
404 | rsc = tdls->rsc_init[tid]; | |
405 | else | |
406 | rsc = tdls->rsc_resp[tid]; | |
aab66128 JM |
407 | } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == |
408 | (WLAN_FC_TODS | WLAN_FC_FROMDS)) { | |
409 | if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0) | |
410 | rsc = sta->rsc_tods[tid]; | |
411 | else | |
412 | rsc = sta->rsc_fromds[tid]; | |
b39f5834 | 413 | } else if (fc & WLAN_FC_TODS) |
2edd5c23 JM |
414 | rsc = sta->rsc_tods[tid]; |
415 | else | |
416 | rsc = sta->rsc_fromds[tid]; | |
417 | ||
418 | ||
b39f5834 | 419 | if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) |
4dac8453 | 420 | tkip_get_pn(pn, data); |
2e479416 JM |
421 | else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) |
422 | goto skip_replay_det; | |
4dac8453 JM |
423 | else |
424 | ccmp_get_pn(pn, data); | |
2edd5c23 | 425 | if (os_memcmp(pn, rsc, 6) <= 0) { |
01b397dd | 426 | u16 seq_ctrl = le_to_host16(hdr->seq_ctrl); |
e4d99217 | 427 | add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR |
3a3cb8ee | 428 | " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s", |
e4d99217 JM |
429 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), |
430 | MAC2STR(hdr->addr3), | |
431 | WLAN_GET_SEQ_SEQ(seq_ctrl), | |
3a3cb8ee AKP |
432 | WLAN_GET_SEQ_FRAG(seq_ctrl), |
433 | (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ? | |
434 | " Retry" : ""); | |
2edd5c23 JM |
435 | wpa_hexdump(MSG_INFO, "RX PN", pn, 6); |
436 | wpa_hexdump(MSG_INFO, "RSC", rsc, 6); | |
4158b80e | 437 | replay = 1; |
2edd5c23 JM |
438 | } |
439 | ||
2e479416 | 440 | skip_replay_det: |
eb2223e0 AKP |
441 | if (tk) { |
442 | if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) | |
443 | decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen); | |
444 | else if (sta->pairwise_cipher == WPA_CIPHER_GCMP || | |
445 | sta->pairwise_cipher == WPA_CIPHER_GCMP_256) | |
5420bcf4 | 446 | decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data, |
eb2223e0 AKP |
447 | len, &dlen); |
448 | else | |
449 | decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen); | |
450 | } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) { | |
98cd3d1c | 451 | decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen); |
eb2223e0 | 452 | } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) { |
2e479416 | 453 | decrypted = wep_decrypt(wt, hdr, data, len, &dlen); |
eb2223e0 AKP |
454 | } else if (sta->ptk_set) { |
455 | if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) | |
98cd3d1c | 456 | decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data, |
eb2223e0 AKP |
457 | len, &dlen); |
458 | else if (sta->pairwise_cipher == WPA_CIPHER_GCMP || | |
459 | sta->pairwise_cipher == WPA_CIPHER_GCMP_256) | |
5420bcf4 | 460 | decrypted = gcmp_decrypt(sta->ptk.tk, sta->ptk.tk_len, |
eb2223e0 AKP |
461 | hdr, data, len, &dlen); |
462 | else | |
98cd3d1c | 463 | decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len, |
eb2223e0 AKP |
464 | &dlen); |
465 | } else { | |
a0530dff JM |
466 | decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data, |
467 | len, &dlen); | |
468 | ptk_iter_done = 1; | |
469 | } | |
470 | if (!decrypted && !ptk_iter_done) { | |
471 | decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data, | |
472 | len, &dlen); | |
473 | if (decrypted) { | |
474 | add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs"); | |
475 | } | |
476 | } | |
2edd5c23 | 477 | if (decrypted) { |
244c9303 | 478 | u16 fc = le_to_host16(hdr->frame_control); |
9559a7f8 | 479 | const u8 *peer_addr = NULL; |
244c9303 JM |
480 | if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS))) |
481 | peer_addr = hdr->addr1; | |
4158b80e JM |
482 | if (!replay) |
483 | os_memcpy(rsc, pn, 6); | |
ee3b84be | 484 | rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted, |
244c9303 | 485 | dlen, 1, peer_addr); |
e45f2e8a | 486 | write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen, |
64f45d07 | 487 | decrypted, dlen); |
b3c43c3c JM |
488 | } else { |
489 | if (!try_ptk_iter) | |
490 | add_note(wt, MSG_DEBUG, "Failed to decrypt frame"); | |
491 | ||
492 | /* Assume the frame was corrupted and there was no FCS to check. | |
493 | * Allow retry of this particular frame to be processed so that | |
494 | * it could end up getting decrypted if it was received without | |
495 | * corruption. */ | |
496 | sta->allow_duplicate = 1; | |
497 | } | |
d318c534 | 498 | os_free(decrypted); |
32234bba JM |
499 | } |
500 | ||
501 | ||
502 | static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr, | |
e45f2e8a JM |
503 | size_t hdrlen, const u8 *qos, const u8 *dst, |
504 | const u8 *src, const u8 *data, size_t len) | |
32234bba JM |
505 | { |
506 | u16 fc = le_to_host16(hdr->frame_control); | |
507 | int prot = !!(fc & WLAN_FC_ISWEP); | |
508 | ||
509 | if (qos) { | |
510 | u8 ack = (qos[0] & 0x60) >> 5; | |
511 | wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR | |
512 | " len=%u%s tid=%u%s%s", | |
513 | MAC2STR(src), MAC2STR(dst), (unsigned int) len, | |
514 | prot ? " Prot" : "", qos[0] & 0x0f, | |
515 | (qos[0] & 0x10) ? " EOSP" : "", | |
516 | ack == 0 ? "" : | |
517 | (ack == 1 ? " NoAck" : | |
518 | (ack == 2 ? " NoExpAck" : " BA"))); | |
519 | } else { | |
520 | wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR | |
521 | " len=%u%s", | |
522 | MAC2STR(src), MAC2STR(dst), (unsigned int) len, | |
523 | prot ? " Prot" : ""); | |
524 | } | |
525 | ||
526 | if (prot) | |
e45f2e8a | 527 | rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len); |
ee3b84be | 528 | else { |
244c9303 | 529 | const u8 *bssid, *sta_addr, *peer_addr; |
99d7c1de JM |
530 | struct wlantest_bss *bss; |
531 | ||
ee3b84be JM |
532 | if (fc & WLAN_FC_TODS) { |
533 | bssid = hdr->addr1; | |
534 | sta_addr = hdr->addr2; | |
244c9303 JM |
535 | peer_addr = NULL; |
536 | } else if (fc & WLAN_FC_FROMDS) { | |
ee3b84be JM |
537 | bssid = hdr->addr2; |
538 | sta_addr = hdr->addr1; | |
244c9303 JM |
539 | peer_addr = NULL; |
540 | } else { | |
541 | bssid = hdr->addr3; | |
542 | sta_addr = hdr->addr2; | |
543 | peer_addr = hdr->addr1; | |
ee3b84be | 544 | } |
99d7c1de JM |
545 | |
546 | bss = bss_get(wt, bssid); | |
547 | if (bss) { | |
548 | struct wlantest_sta *sta = sta_get(bss, sta_addr); | |
549 | ||
550 | if (sta) { | |
551 | if (qos) { | |
552 | int tid = qos[0] & 0x0f; | |
553 | if (fc & WLAN_FC_TODS) | |
554 | sta->tx_tid[tid]++; | |
555 | else | |
556 | sta->rx_tid[tid]++; | |
557 | } else { | |
558 | if (fc & WLAN_FC_TODS) | |
559 | sta->tx_tid[16]++; | |
560 | else | |
561 | sta->rx_tid[16]++; | |
562 | } | |
563 | } | |
564 | } | |
565 | ||
244c9303 JM |
566 | rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0, |
567 | peer_addr); | |
ee3b84be | 568 | } |
32234bba JM |
569 | } |
570 | ||
571 | ||
0e42fff3 JM |
572 | static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid, |
573 | const u8 *sta1_addr, | |
574 | const u8 *sta2_addr) | |
575 | { | |
576 | struct wlantest_bss *bss; | |
577 | struct wlantest_sta *sta1, *sta2; | |
ace4e460 | 578 | struct wlantest_tdls *tdls, *found = NULL; |
0e42fff3 JM |
579 | |
580 | bss = bss_find(wt, bssid); | |
581 | if (bss == NULL) | |
582 | return NULL; | |
583 | sta1 = sta_find(bss, sta1_addr); | |
584 | if (sta1 == NULL) | |
585 | return NULL; | |
586 | sta2 = sta_find(bss, sta2_addr); | |
587 | if (sta2 == NULL) | |
588 | return NULL; | |
589 | ||
590 | dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) { | |
591 | if ((tdls->init == sta1 && tdls->resp == sta2) || | |
ace4e460 JM |
592 | (tdls->init == sta2 && tdls->resp == sta1)) { |
593 | found = tdls; | |
594 | if (tdls->link_up) | |
595 | break; | |
596 | } | |
0e42fff3 JM |
597 | } |
598 | ||
ace4e460 | 599 | return found; |
0e42fff3 JM |
600 | } |
601 | ||
602 | ||
603 | static void add_direct_link(struct wlantest *wt, const u8 *bssid, | |
604 | const u8 *sta1_addr, const u8 *sta2_addr) | |
605 | { | |
606 | struct wlantest_tdls *tdls; | |
607 | ||
608 | tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr); | |
609 | if (tdls == NULL) | |
610 | return; | |
611 | ||
612 | if (tdls->link_up) | |
613 | tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++; | |
614 | else | |
615 | tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++; | |
616 | } | |
617 | ||
618 | ||
619 | static void add_ap_path(struct wlantest *wt, const u8 *bssid, | |
620 | const u8 *sta1_addr, const u8 *sta2_addr) | |
621 | { | |
622 | struct wlantest_tdls *tdls; | |
623 | ||
624 | tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr); | |
625 | if (tdls == NULL) | |
626 | return; | |
627 | ||
628 | if (tdls->link_up) | |
629 | tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++; | |
630 | else | |
631 | tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++; | |
632 | } | |
633 | ||
634 | ||
2d73f0a8 JM |
635 | void rx_data(struct wlantest *wt, const u8 *data, size_t len) |
636 | { | |
637 | const struct ieee80211_hdr *hdr; | |
32234bba JM |
638 | u16 fc, stype; |
639 | size_t hdrlen; | |
640 | const u8 *qos = NULL; | |
2d73f0a8 JM |
641 | |
642 | if (len < 24) | |
643 | return; | |
644 | ||
645 | hdr = (const struct ieee80211_hdr *) data; | |
646 | fc = le_to_host16(hdr->frame_control); | |
32234bba JM |
647 | stype = WLAN_FC_GET_STYPE(fc); |
648 | hdrlen = 24; | |
649 | if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == | |
650 | (WLAN_FC_TODS | WLAN_FC_FROMDS)) | |
651 | hdrlen += ETH_ALEN; | |
652 | if (stype & 0x08) { | |
653 | qos = data + hdrlen; | |
654 | hdrlen += 2; | |
655 | } | |
656 | if (len < hdrlen) | |
657 | return; | |
2d73f0a8 JM |
658 | wt->rx_data++; |
659 | ||
660 | switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) { | |
661 | case 0: | |
662 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA=" | |
663 | MACSTR " BSSID=" MACSTR, | |
664 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
665 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
666 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
667 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
668 | MAC2STR(hdr->addr3)); | |
0e42fff3 | 669 | add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2); |
e45f2e8a | 670 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2, |
b39f5834 | 671 | data + hdrlen, len - hdrlen); |
2d73f0a8 JM |
672 | break; |
673 | case WLAN_FC_FROMDS: | |
674 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR | |
675 | " BSSID=" MACSTR " SA=" MACSTR, | |
676 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
677 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
678 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
679 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
680 | MAC2STR(hdr->addr3)); | |
0e42fff3 | 681 | add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3); |
e45f2e8a | 682 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3, |
32234bba | 683 | data + hdrlen, len - hdrlen); |
2d73f0a8 JM |
684 | break; |
685 | case WLAN_FC_TODS: | |
686 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR | |
687 | " SA=" MACSTR " DA=" MACSTR, | |
688 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
689 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
690 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
691 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
692 | MAC2STR(hdr->addr3)); | |
0e42fff3 | 693 | add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2); |
e45f2e8a | 694 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2, |
32234bba | 695 | data + hdrlen, len - hdrlen); |
2d73f0a8 JM |
696 | break; |
697 | case WLAN_FC_TODS | WLAN_FC_FROMDS: | |
698 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA=" | |
699 | MACSTR " DA=" MACSTR " SA=" MACSTR, | |
700 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
701 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
702 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
703 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
704 | MAC2STR(hdr->addr3), | |
705 | MAC2STR((const u8 *) (hdr + 1))); | |
e45f2e8a JM |
706 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2, |
707 | data + hdrlen, len - hdrlen); | |
2d73f0a8 JM |
708 | break; |
709 | } | |
710 | } |