]>
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 | { |
d318c534 | 253 | struct wlantest_bss *bss; |
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); | |
278 | if (sta) | |
279 | sta->counters[ | |
280 | WLANTEST_STA_COUNTER_PROT_DATA_TX]++; | |
281 | } else { | |
282 | bss = bss_find(wt, hdr->addr2); | |
283 | if (!bss) | |
284 | return; | |
285 | sta = sta_find(bss, hdr->addr1); | |
286 | } | |
287 | } else if (fc & WLAN_FC_TODS) { | |
d318c534 JM |
288 | bss = bss_get(wt, hdr->addr1); |
289 | if (bss == NULL) | |
290 | return; | |
291 | sta = sta_get(bss, hdr->addr2); | |
0a9ddd92 JM |
292 | if (sta) |
293 | sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++; | |
b39f5834 | 294 | } else if (fc & WLAN_FC_FROMDS) { |
d318c534 JM |
295 | bss = bss_get(wt, hdr->addr2); |
296 | if (bss == NULL) | |
297 | return; | |
298 | sta = sta_get(bss, hdr->addr1); | |
b39f5834 JM |
299 | } else { |
300 | bss = bss_get(wt, hdr->addr3); | |
301 | if (bss == NULL) | |
302 | return; | |
303 | sta = sta_find(bss, hdr->addr2); | |
304 | sta2 = sta_find(bss, hdr->addr1); | |
305 | if (sta == NULL || sta2 == NULL) | |
306 | return; | |
ace4e460 | 307 | found = NULL; |
b39f5834 JM |
308 | dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) |
309 | { | |
310 | if ((tdls->init == sta && tdls->resp == sta2) || | |
311 | (tdls->init == sta2 && tdls->resp == sta)) { | |
ace4e460 JM |
312 | found = tdls; |
313 | if (tdls->link_up) | |
314 | break; | |
b39f5834 JM |
315 | } |
316 | } | |
ace4e460 JM |
317 | if (found) { |
318 | if (!found->link_up) | |
319 | add_note(wt, MSG_DEBUG, | |
320 | "TDLS: Link not up, but Data " | |
321 | "frame seen"); | |
322 | tk = found->tpk.tk; | |
323 | tdls = found; | |
324 | } | |
d318c534 | 325 | } |
2e479416 JM |
326 | if ((sta == NULL || |
327 | (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) && | |
328 | tk == NULL) { | |
e4d99217 | 329 | add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame"); |
a0530dff JM |
330 | if (dl_list_empty(&wt->ptk)) |
331 | return; | |
332 | try_ptk_iter = 1; | |
d318c534 JM |
333 | } |
334 | ||
335 | if (len < 4) { | |
e4d99217 | 336 | add_note(wt, MSG_INFO, "Too short encrypted data frame"); |
d318c534 JM |
337 | return; |
338 | } | |
339 | ||
99d7c1de JM |
340 | if (sta == NULL) |
341 | return; | |
20062114 JM |
342 | if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) && |
343 | !(data[3] & 0x20)) { | |
e4d99217 JM |
344 | add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from " |
345 | MACSTR " did not have ExtIV bit set to 1", | |
346 | MAC2STR(src)); | |
347 | return; | |
20062114 JM |
348 | } |
349 | ||
b39f5834 | 350 | if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) { |
16b8b6ea | 351 | if (data[3] & 0x1f) { |
e4d99217 JM |
352 | add_note(wt, MSG_INFO, "TKIP frame from " MACSTR |
353 | " used non-zero reserved bit", | |
354 | MAC2STR(hdr->addr2)); | |
16b8b6ea JM |
355 | } |
356 | if (data[1] != ((data[0] | 0x20) & 0x7f)) { | |
e4d99217 JM |
357 | add_note(wt, MSG_INFO, "TKIP frame from " MACSTR |
358 | " used incorrect WEPSeed[1] (was 0x%x, " | |
359 | "expected 0x%x)", | |
360 | MAC2STR(hdr->addr2), data[1], | |
361 | (data[0] | 0x20) & 0x7f); | |
16b8b6ea | 362 | } |
b39f5834 | 363 | } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) { |
16b8b6ea | 364 | if (data[2] != 0 || (data[3] & 0x1f) != 0) { |
e4d99217 JM |
365 | add_note(wt, MSG_INFO, "CCMP frame from " MACSTR |
366 | " used non-zero reserved bit", | |
367 | MAC2STR(hdr->addr2)); | |
16b8b6ea JM |
368 | } |
369 | } | |
370 | ||
d318c534 JM |
371 | keyid = data[3] >> 6; |
372 | if (keyid != 0) { | |
e4d99217 JM |
373 | add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in " |
374 | "individually addressed Data frame from " MACSTR, | |
375 | keyid, MAC2STR(hdr->addr2)); | |
d318c534 JM |
376 | } |
377 | ||
99d7c1de | 378 | if (qos) { |
2edd5c23 | 379 | tid = qos[0] & 0x0f; |
99d7c1de JM |
380 | if (fc & WLAN_FC_TODS) |
381 | sta->tx_tid[tid]++; | |
382 | else | |
383 | sta->rx_tid[tid]++; | |
384 | } else { | |
2edd5c23 | 385 | tid = 0; |
99d7c1de JM |
386 | if (fc & WLAN_FC_TODS) |
387 | sta->tx_tid[16]++; | |
388 | else | |
389 | sta->rx_tid[16]++; | |
390 | } | |
b39f5834 JM |
391 | if (tk) { |
392 | if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0) | |
393 | rsc = tdls->rsc_init[tid]; | |
394 | else | |
395 | rsc = tdls->rsc_resp[tid]; | |
396 | } else if (fc & WLAN_FC_TODS) | |
2edd5c23 JM |
397 | rsc = sta->rsc_tods[tid]; |
398 | else | |
399 | rsc = sta->rsc_fromds[tid]; | |
400 | ||
401 | ||
b39f5834 | 402 | if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) |
4dac8453 | 403 | tkip_get_pn(pn, data); |
2e479416 JM |
404 | else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) |
405 | goto skip_replay_det; | |
4dac8453 JM |
406 | else |
407 | ccmp_get_pn(pn, data); | |
2edd5c23 | 408 | if (os_memcmp(pn, rsc, 6) <= 0) { |
01b397dd | 409 | u16 seq_ctrl = le_to_host16(hdr->seq_ctrl); |
e4d99217 | 410 | add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR |
3a3cb8ee | 411 | " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s", |
e4d99217 JM |
412 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), |
413 | MAC2STR(hdr->addr3), | |
414 | WLAN_GET_SEQ_SEQ(seq_ctrl), | |
3a3cb8ee AKP |
415 | WLAN_GET_SEQ_FRAG(seq_ctrl), |
416 | (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ? | |
417 | " Retry" : ""); | |
2edd5c23 JM |
418 | wpa_hexdump(MSG_INFO, "RX PN", pn, 6); |
419 | wpa_hexdump(MSG_INFO, "RSC", rsc, 6); | |
4158b80e | 420 | replay = 1; |
2edd5c23 JM |
421 | } |
422 | ||
2e479416 | 423 | skip_replay_det: |
eb2223e0 AKP |
424 | if (tk) { |
425 | if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) | |
426 | decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen); | |
427 | else if (sta->pairwise_cipher == WPA_CIPHER_GCMP || | |
428 | sta->pairwise_cipher == WPA_CIPHER_GCMP_256) | |
429 | decrypted = gcmp_decrypt(tk, sta->tk_len, hdr, data, | |
430 | len, &dlen); | |
431 | else | |
432 | decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen); | |
433 | } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) { | |
98cd3d1c | 434 | decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen); |
eb2223e0 | 435 | } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) { |
2e479416 | 436 | decrypted = wep_decrypt(wt, hdr, data, len, &dlen); |
eb2223e0 AKP |
437 | } else if (sta->ptk_set) { |
438 | if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) | |
98cd3d1c | 439 | decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data, |
eb2223e0 AKP |
440 | len, &dlen); |
441 | else if (sta->pairwise_cipher == WPA_CIPHER_GCMP || | |
442 | sta->pairwise_cipher == WPA_CIPHER_GCMP_256) | |
98cd3d1c | 443 | decrypted = gcmp_decrypt(sta->ptk.tk, sta->tk_len, |
eb2223e0 AKP |
444 | hdr, data, len, &dlen); |
445 | else | |
98cd3d1c | 446 | decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len, |
eb2223e0 AKP |
447 | &dlen); |
448 | } else { | |
a0530dff JM |
449 | decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data, |
450 | len, &dlen); | |
451 | ptk_iter_done = 1; | |
452 | } | |
453 | if (!decrypted && !ptk_iter_done) { | |
454 | decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data, | |
455 | len, &dlen); | |
456 | if (decrypted) { | |
457 | add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs"); | |
458 | } | |
459 | } | |
2edd5c23 | 460 | if (decrypted) { |
244c9303 | 461 | u16 fc = le_to_host16(hdr->frame_control); |
9559a7f8 | 462 | const u8 *peer_addr = NULL; |
244c9303 JM |
463 | if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS))) |
464 | peer_addr = hdr->addr1; | |
4158b80e JM |
465 | if (!replay) |
466 | os_memcpy(rsc, pn, 6); | |
ee3b84be | 467 | rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted, |
244c9303 | 468 | dlen, 1, peer_addr); |
e45f2e8a | 469 | write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen, |
64f45d07 | 470 | decrypted, dlen); |
a0530dff | 471 | } else if (!try_ptk_iter) |
e4d99217 | 472 | add_note(wt, MSG_DEBUG, "Failed to decrypt frame"); |
d318c534 | 473 | os_free(decrypted); |
32234bba JM |
474 | } |
475 | ||
476 | ||
477 | static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr, | |
e45f2e8a JM |
478 | size_t hdrlen, const u8 *qos, const u8 *dst, |
479 | const u8 *src, const u8 *data, size_t len) | |
32234bba JM |
480 | { |
481 | u16 fc = le_to_host16(hdr->frame_control); | |
482 | int prot = !!(fc & WLAN_FC_ISWEP); | |
483 | ||
484 | if (qos) { | |
485 | u8 ack = (qos[0] & 0x60) >> 5; | |
486 | wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR | |
487 | " len=%u%s tid=%u%s%s", | |
488 | MAC2STR(src), MAC2STR(dst), (unsigned int) len, | |
489 | prot ? " Prot" : "", qos[0] & 0x0f, | |
490 | (qos[0] & 0x10) ? " EOSP" : "", | |
491 | ack == 0 ? "" : | |
492 | (ack == 1 ? " NoAck" : | |
493 | (ack == 2 ? " NoExpAck" : " BA"))); | |
494 | } else { | |
495 | wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR | |
496 | " len=%u%s", | |
497 | MAC2STR(src), MAC2STR(dst), (unsigned int) len, | |
498 | prot ? " Prot" : ""); | |
499 | } | |
500 | ||
501 | if (prot) | |
e45f2e8a | 502 | rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len); |
ee3b84be | 503 | else { |
244c9303 | 504 | const u8 *bssid, *sta_addr, *peer_addr; |
99d7c1de JM |
505 | struct wlantest_bss *bss; |
506 | ||
ee3b84be JM |
507 | if (fc & WLAN_FC_TODS) { |
508 | bssid = hdr->addr1; | |
509 | sta_addr = hdr->addr2; | |
244c9303 JM |
510 | peer_addr = NULL; |
511 | } else if (fc & WLAN_FC_FROMDS) { | |
ee3b84be JM |
512 | bssid = hdr->addr2; |
513 | sta_addr = hdr->addr1; | |
244c9303 JM |
514 | peer_addr = NULL; |
515 | } else { | |
516 | bssid = hdr->addr3; | |
517 | sta_addr = hdr->addr2; | |
518 | peer_addr = hdr->addr1; | |
ee3b84be | 519 | } |
99d7c1de JM |
520 | |
521 | bss = bss_get(wt, bssid); | |
522 | if (bss) { | |
523 | struct wlantest_sta *sta = sta_get(bss, sta_addr); | |
524 | ||
525 | if (sta) { | |
526 | if (qos) { | |
527 | int tid = qos[0] & 0x0f; | |
528 | if (fc & WLAN_FC_TODS) | |
529 | sta->tx_tid[tid]++; | |
530 | else | |
531 | sta->rx_tid[tid]++; | |
532 | } else { | |
533 | if (fc & WLAN_FC_TODS) | |
534 | sta->tx_tid[16]++; | |
535 | else | |
536 | sta->rx_tid[16]++; | |
537 | } | |
538 | } | |
539 | } | |
540 | ||
244c9303 JM |
541 | rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0, |
542 | peer_addr); | |
ee3b84be | 543 | } |
32234bba JM |
544 | } |
545 | ||
546 | ||
0e42fff3 JM |
547 | static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid, |
548 | const u8 *sta1_addr, | |
549 | const u8 *sta2_addr) | |
550 | { | |
551 | struct wlantest_bss *bss; | |
552 | struct wlantest_sta *sta1, *sta2; | |
ace4e460 | 553 | struct wlantest_tdls *tdls, *found = NULL; |
0e42fff3 JM |
554 | |
555 | bss = bss_find(wt, bssid); | |
556 | if (bss == NULL) | |
557 | return NULL; | |
558 | sta1 = sta_find(bss, sta1_addr); | |
559 | if (sta1 == NULL) | |
560 | return NULL; | |
561 | sta2 = sta_find(bss, sta2_addr); | |
562 | if (sta2 == NULL) | |
563 | return NULL; | |
564 | ||
565 | dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) { | |
566 | if ((tdls->init == sta1 && tdls->resp == sta2) || | |
ace4e460 JM |
567 | (tdls->init == sta2 && tdls->resp == sta1)) { |
568 | found = tdls; | |
569 | if (tdls->link_up) | |
570 | break; | |
571 | } | |
0e42fff3 JM |
572 | } |
573 | ||
ace4e460 | 574 | return found; |
0e42fff3 JM |
575 | } |
576 | ||
577 | ||
578 | static void add_direct_link(struct wlantest *wt, const u8 *bssid, | |
579 | const u8 *sta1_addr, const u8 *sta2_addr) | |
580 | { | |
581 | struct wlantest_tdls *tdls; | |
582 | ||
583 | tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr); | |
584 | if (tdls == NULL) | |
585 | return; | |
586 | ||
587 | if (tdls->link_up) | |
588 | tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++; | |
589 | else | |
590 | tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++; | |
591 | } | |
592 | ||
593 | ||
594 | static void add_ap_path(struct wlantest *wt, const u8 *bssid, | |
595 | const u8 *sta1_addr, const u8 *sta2_addr) | |
596 | { | |
597 | struct wlantest_tdls *tdls; | |
598 | ||
599 | tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr); | |
600 | if (tdls == NULL) | |
601 | return; | |
602 | ||
603 | if (tdls->link_up) | |
604 | tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++; | |
605 | else | |
606 | tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++; | |
607 | } | |
608 | ||
609 | ||
2d73f0a8 JM |
610 | void rx_data(struct wlantest *wt, const u8 *data, size_t len) |
611 | { | |
612 | const struct ieee80211_hdr *hdr; | |
32234bba JM |
613 | u16 fc, stype; |
614 | size_t hdrlen; | |
615 | const u8 *qos = NULL; | |
2d73f0a8 JM |
616 | |
617 | if (len < 24) | |
618 | return; | |
619 | ||
620 | hdr = (const struct ieee80211_hdr *) data; | |
621 | fc = le_to_host16(hdr->frame_control); | |
32234bba JM |
622 | stype = WLAN_FC_GET_STYPE(fc); |
623 | hdrlen = 24; | |
624 | if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) == | |
625 | (WLAN_FC_TODS | WLAN_FC_FROMDS)) | |
626 | hdrlen += ETH_ALEN; | |
627 | if (stype & 0x08) { | |
628 | qos = data + hdrlen; | |
629 | hdrlen += 2; | |
630 | } | |
631 | if (len < hdrlen) | |
632 | return; | |
2d73f0a8 JM |
633 | wt->rx_data++; |
634 | ||
635 | switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) { | |
636 | case 0: | |
637 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA=" | |
638 | MACSTR " BSSID=" MACSTR, | |
639 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
640 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
641 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
642 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
643 | MAC2STR(hdr->addr3)); | |
0e42fff3 | 644 | add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2); |
e45f2e8a | 645 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2, |
b39f5834 | 646 | data + hdrlen, len - hdrlen); |
2d73f0a8 JM |
647 | break; |
648 | case WLAN_FC_FROMDS: | |
649 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR | |
650 | " BSSID=" MACSTR " SA=" MACSTR, | |
651 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
652 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
653 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
654 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
655 | MAC2STR(hdr->addr3)); | |
0e42fff3 | 656 | add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3); |
e45f2e8a | 657 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3, |
32234bba | 658 | data + hdrlen, len - hdrlen); |
2d73f0a8 JM |
659 | break; |
660 | case WLAN_FC_TODS: | |
661 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR | |
662 | " SA=" MACSTR " DA=" MACSTR, | |
663 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
664 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
665 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
666 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
667 | MAC2STR(hdr->addr3)); | |
0e42fff3 | 668 | add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2); |
e45f2e8a | 669 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2, |
32234bba | 670 | data + hdrlen, len - hdrlen); |
2d73f0a8 JM |
671 | break; |
672 | case WLAN_FC_TODS | WLAN_FC_FROMDS: | |
673 | wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA=" | |
674 | MACSTR " DA=" MACSTR " SA=" MACSTR, | |
675 | data_stype(WLAN_FC_GET_STYPE(fc)), | |
676 | fc & WLAN_FC_PWRMGT ? " PwrMgt" : "", | |
677 | fc & WLAN_FC_ISWEP ? " Prot" : "", | |
678 | MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), | |
679 | MAC2STR(hdr->addr3), | |
680 | MAC2STR((const u8 *) (hdr + 1))); | |
e45f2e8a JM |
681 | rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2, |
682 | data + hdrlen, len - hdrlen); | |
2d73f0a8 JM |
683 | break; |
684 | } | |
685 | } |