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 
data_stype(u16 stype)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 
rx_data_vlan(struct wlantest * wt,const u8 * bssid,const u8 * sta_addr,const u8 * dst,const u8 * src,const u8 * data,size_t len,int prot,const u8 * peer_addr)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 
rx_data_eth(struct wlantest * wt,const u8 * bssid,const u8 * sta_addr,const u8 * dst,const u8 * src,u16 ethertype,const u8 * data,size_t len,int prot,const u8 * peer_addr)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 
rx_data_process(struct wlantest * wt,struct wlantest_bss * bss,const u8 * bssid,const u8 * sta_addr,const u8 * dst,const u8 * src,const u8 * data,size_t len,int prot,const u8 * peer_addr,const u8 * qos)102 static void rx_data_process(struct wlantest *wt, struct wlantest_bss *bss,
103 			    const u8 *bssid,
104 			    const u8 *sta_addr,
105 			    const u8 *dst, const u8 *src,
106 			    const u8 *data, size_t len, int prot,
107 			    const u8 *peer_addr, const u8 *qos)
108 {
109 	if (len == 0)
110 		return;
111 
112 	if (bss && bss->mesh && qos && !(qos[0] & BIT(7)) &&
113 	    (qos[1] & BIT(0))) {
114 		u8 addr_ext_mode;
115 		size_t mesh_control_len = 6;
116 
117 		/* Skip Mesh Control field if this is not an A-MSDU */
118 		if (len < mesh_control_len) {
119 			wpa_printf(MSG_DEBUG,
120 				   "Not enough room for Mesh Control field");
121 			return;
122 		}
123 
124 		addr_ext_mode = data[0] & 0x03;
125 		if (addr_ext_mode == 3) {
126 			wpa_printf(MSG_DEBUG,
127 				   "Reserved Mesh Control :: Address Extension Mode");
128 			return;
129 		}
130 
131 		mesh_control_len += addr_ext_mode * ETH_ALEN;
132 		if (len < mesh_control_len) {
133 			wpa_printf(MSG_DEBUG,
134 				   "Not enough room for Mesh Address Extension");
135 			return;
136 		}
137 
138 		len -= mesh_control_len;
139 		data += mesh_control_len;
140 	}
141 
142 	if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
143 		rx_data_eth(wt, bssid, sta_addr, dst, src,
144 			    WPA_GET_BE16(data + 6), data + 8, len - 8, prot,
145 			    peer_addr);
146 		return;
147 	}
148 
149 	wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
150 }
151 
152 
try_ptk(struct wlantest * wt,int pairwise_cipher,struct wpa_ptk * ptk,const struct ieee80211_hdr * hdr,const u8 * data,size_t data_len,size_t * decrypted_len)153 static u8 * try_ptk(struct wlantest *wt, int pairwise_cipher,
154 		    struct wpa_ptk *ptk, const struct ieee80211_hdr *hdr,
155 		    const u8 *data, size_t data_len, size_t *decrypted_len)
156 {
157 	u8 *decrypted;
158 	unsigned int tk_len = ptk->tk_len;
159 
160 	decrypted = NULL;
161 	if ((pairwise_cipher == WPA_CIPHER_CCMP ||
162 	     pairwise_cipher == 0) && tk_len == 16) {
163 		decrypted = ccmp_decrypt(ptk->tk, hdr, data,
164 					 data_len, decrypted_len);
165 	} else if ((pairwise_cipher == WPA_CIPHER_CCMP_256 ||
166 		    pairwise_cipher == 0) && tk_len == 32) {
167 		decrypted = ccmp_256_decrypt(ptk->tk, hdr, data,
168 					     data_len, decrypted_len);
169 	} else if ((pairwise_cipher == WPA_CIPHER_GCMP ||
170 		    pairwise_cipher == WPA_CIPHER_GCMP_256 ||
171 		    pairwise_cipher == 0) &&
172 		   (tk_len == 16 || tk_len == 32)) {
173 		decrypted = gcmp_decrypt(ptk->tk, tk_len, hdr,
174 					 data, data_len, decrypted_len);
175 	} else if ((pairwise_cipher == WPA_CIPHER_TKIP ||
176 		    pairwise_cipher == 0) && tk_len == 32) {
177 		enum michael_mic_result mic_res;
178 
179 		decrypted = tkip_decrypt(ptk->tk, hdr, data, data_len,
180 					 decrypted_len, &mic_res,
181 					 &wt->tkip_frag);
182 		if (decrypted && mic_res == MICHAEL_MIC_INCORRECT)
183 			add_note(wt, MSG_INFO, "Invalid Michael MIC");
184 		else if (decrypted && mic_res == MICHAEL_MIC_NOT_VERIFIED)
185 			add_note(wt, MSG_DEBUG, "Michael MIC not verified");
186 	}
187 
188 	return decrypted;
189 }
190 
191 
try_all_ptk(struct wlantest * wt,int pairwise_cipher,const struct ieee80211_hdr * hdr,int keyid,const u8 * data,size_t data_len,size_t * decrypted_len)192 static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher,
193 			const struct ieee80211_hdr *hdr, int keyid,
194 			const u8 *data, size_t data_len, size_t *decrypted_len)
195 {
196 	struct wlantest_ptk *ptk;
197 	u8 *decrypted;
198 	int prev_level = wpa_debug_level;
199 
200 	wpa_debug_level = MSG_WARNING;
201 	dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) {
202 		decrypted = try_ptk(wt, pairwise_cipher, &ptk->ptk, hdr,
203 				    data, data_len, decrypted_len);
204 		if (decrypted) {
205 			wpa_debug_level = prev_level;
206 			add_note(wt, MSG_DEBUG,
207 				 "Found PTK match from list of all known PTKs");
208 			write_decrypted_note(wt, decrypted, ptk->ptk.tk,
209 					     ptk->ptk.tk_len, keyid);
210 			return decrypted;
211 		}
212 	}
213 	wpa_debug_level = prev_level;
214 
215 	return NULL;
216 }
217 
218 
check_plaintext_prot(struct wlantest * wt,const struct ieee80211_hdr * hdr,const u8 * data,size_t len)219 static void check_plaintext_prot(struct wlantest *wt,
220 				 const struct ieee80211_hdr *hdr,
221 				 const u8 *data, size_t len)
222 {
223 	if (len < 8 + 3 || data[8] != 0xaa || data[9] != 0xaa ||
224 	    data[10] != 0x03)
225 		return;
226 
227 	add_note(wt, MSG_DEBUG,
228 		 "Plaintext payload in protected frame");
229 	wpa_printf(MSG_INFO, "Plaintext payload in protected frame #%u: A2="
230 		   MACSTR " seq=%u",
231 		   wt->frame_num, MAC2STR(hdr->addr2),
232 		   WLAN_GET_SEQ_SEQ(le_to_host16(hdr->seq_ctrl)));
233 }
234 
235 
rx_data_bss_prot_group(struct wlantest * wt,const struct ieee80211_hdr * hdr,size_t hdrlen,const u8 * qos,const u8 * dst,const u8 * src,const u8 * data,size_t len)236 static void rx_data_bss_prot_group(struct wlantest *wt,
237 				   const struct ieee80211_hdr *hdr,
238 				   size_t hdrlen,
239 				   const u8 *qos, const u8 *dst, const u8 *src,
240 				   const u8 *data, size_t len)
241 {
242 	struct wlantest_bss *bss;
243 	int keyid;
244 	u8 *decrypted = NULL;
245 	size_t dlen;
246 	u8 pn[6];
247 	int replay = 0;
248 
249 	bss = bss_get(wt, hdr->addr2);
250 	if (bss == NULL)
251 		return;
252 	if (len < 4) {
253 		add_note(wt, MSG_INFO, "Too short group addressed data frame");
254 		return;
255 	}
256 
257 	if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
258 	    !(data[3] & 0x20)) {
259 		add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
260 			 MACSTR " did not have ExtIV bit set to 1",
261 			 MAC2STR(bss->bssid));
262 		return;
263 	}
264 
265 	if (bss->group_cipher == WPA_CIPHER_TKIP) {
266 		if (data[3] & 0x1f) {
267 			add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
268 				 " used non-zero reserved bit",
269 				 MAC2STR(bss->bssid));
270 		}
271 		if (data[1] != ((data[0] | 0x20) & 0x7f)) {
272 			add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
273 				 " used incorrect WEPSeed[1] (was 0x%x, "
274 				 "expected 0x%x)",
275 				 MAC2STR(bss->bssid), data[1],
276 				 (data[0] | 0x20) & 0x7f);
277 		}
278 	} else if (bss->group_cipher == WPA_CIPHER_CCMP) {
279 		if (data[2] != 0 || (data[3] & 0x1f) != 0) {
280 			add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
281 				 " used non-zero reserved bit",
282 				 MAC2STR(bss->bssid));
283 		}
284 	}
285 
286 	check_plaintext_prot(wt, hdr, data, len);
287 	keyid = data[3] >> 6;
288 	if (bss->gtk_len[keyid] == 0 &&
289 	    (bss->group_cipher != WPA_CIPHER_WEP40 ||
290 	     dl_list_empty(&wt->wep))) {
291 		decrypted = try_all_ptk(wt, bss->group_cipher, hdr, keyid,
292 					data, len, &dlen);
293 		if (decrypted)
294 			goto process;
295 		add_note(wt, MSG_MSGDUMP,
296 			 "No GTK known to decrypt the frame (A2=" MACSTR
297 			 " KeyID=%d)",
298 			 MAC2STR(hdr->addr2), keyid);
299 		return;
300 	}
301 
302 	if (bss->group_cipher == WPA_CIPHER_TKIP)
303 		tkip_get_pn(pn, data);
304 	else if (bss->group_cipher == WPA_CIPHER_WEP40)
305 		goto skip_replay_det;
306 	else
307 		ccmp_get_pn(pn, data);
308 	if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
309 		u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
310 		char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
311 
312 		wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
313 		wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), bss->rsc[keyid], 6);
314 		add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
315 			 " A2=" MACSTR " A3=" MACSTR
316 			 " seq=%u frag=%u%s keyid=%d #%u %s<=%s",
317 			 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
318 			 MAC2STR(hdr->addr3),
319 			 WLAN_GET_SEQ_SEQ(seq_ctrl),
320 			 WLAN_GET_SEQ_FRAG(seq_ctrl),
321 			 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
322 			 " Retry" : "",
323 			 keyid, wt->frame_num, pn_hex, rsc_hex);
324 		replay = 1;
325 	}
326 
327 skip_replay_det:
328 	if (bss->group_cipher == WPA_CIPHER_TKIP) {
329 		enum michael_mic_result mic_res;
330 
331 		decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
332 					 &dlen, &mic_res, &wt->tkip_frag);
333 		if (decrypted && mic_res == MICHAEL_MIC_INCORRECT)
334 			add_note(wt, MSG_INFO, "Invalid Michael MIC");
335 		else if (decrypted && mic_res == MICHAEL_MIC_NOT_VERIFIED)
336 			add_note(wt, MSG_DEBUG, "Michael MIC not verified");
337 	} else if (bss->group_cipher == WPA_CIPHER_WEP40) {
338 		decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
339 	} else if (bss->group_cipher == WPA_CIPHER_CCMP) {
340 		decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
341 					 &dlen);
342 	} else if (bss->group_cipher == WPA_CIPHER_CCMP_256) {
343 		decrypted = ccmp_256_decrypt(bss->gtk[keyid], hdr, data, len,
344 					     &dlen);
345 	} else if (bss->group_cipher == WPA_CIPHER_GCMP ||
346 		   bss->group_cipher == WPA_CIPHER_GCMP_256) {
347 		decrypted = gcmp_decrypt(bss->gtk[keyid], bss->gtk_len[keyid],
348 					 hdr, data, len, &dlen);
349 	}
350 
351 	if (decrypted) {
352 		char gtk[65];
353 
354 		wpa_snprintf_hex(gtk, sizeof(gtk), bss->gtk[keyid],
355 				 bss->gtk_len[keyid]);
356 		add_note(wt, MSG_EXCESSIVE, "GTK[%d] %s", keyid, gtk);
357 	process:
358 		rx_data_process(wt, bss, bss->bssid, NULL, dst, src, decrypted,
359 				dlen, 1, NULL, qos);
360 		if (!replay)
361 			os_memcpy(bss->rsc[keyid], pn, 6);
362 		write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
363 				     decrypted, dlen);
364 	} else {
365 		wpa_printf(MSG_DEBUG, "Failed to decrypt frame (group) #%u A2="
366 			   MACSTR " seq=%u",
367 			   wt->frame_num, MAC2STR(hdr->addr2),
368 			   WLAN_GET_SEQ_SEQ(le_to_host16(hdr->seq_ctrl)));
369 		add_note(wt, MSG_DEBUG, "Failed to decrypt frame (group)");
370 	}
371 	os_free(decrypted);
372 }
373 
374 
try_ptk_decrypt(struct wlantest * wt,struct wlantest_sta * sta,const struct ieee80211_hdr * hdr,int keyid,const u8 * data,size_t len,const u8 * tk,size_t tk_len,size_t * dlen)375 static u8 * try_ptk_decrypt(struct wlantest *wt, struct wlantest_sta *sta,
376 			    const struct ieee80211_hdr *hdr, int keyid,
377 			    const u8 *data, size_t len,
378 			    const u8 *tk, size_t tk_len, size_t *dlen)
379 {
380 	u8 *decrypted = NULL;
381 
382 	if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
383 		decrypted = ccmp_256_decrypt(tk, hdr, data, len, dlen);
384 	else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
385 		 sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
386 		decrypted = gcmp_decrypt(tk, tk_len, hdr, data, len, dlen);
387 	else
388 		decrypted = ccmp_decrypt(tk, hdr, data, len, dlen);
389 	write_decrypted_note(wt, decrypted, tk, tk_len, keyid);
390 
391 	return decrypted;
392 }
393 
394 
rx_data_bss_prot(struct wlantest * wt,const struct ieee80211_hdr * hdr,size_t hdrlen,const u8 * qos,const u8 * dst,const u8 * src,const u8 * data,size_t len)395 static void rx_data_bss_prot(struct wlantest *wt,
396 			     const struct ieee80211_hdr *hdr, size_t hdrlen,
397 			     const u8 *qos, const u8 *dst, const u8 *src,
398 			     const u8 *data, size_t len)
399 {
400 	struct wlantest_bss *bss, *bss2;
401 	struct wlantest_sta *sta, *sta2;
402 	int keyid;
403 	u16 fc = le_to_host16(hdr->frame_control);
404 	u8 *decrypted = NULL;
405 	size_t dlen;
406 	int tid;
407 	u8 pn[6], *rsc = NULL;
408 	struct wlantest_tdls *tdls = NULL, *found;
409 	const u8 *tk = NULL;
410 	int ptk_iter_done = 0;
411 	int try_ptk_iter = 0;
412 	int replay = 0;
413 	int only_zero_tk = 0;
414 	u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
415 
416 	if (hdr->addr1[0] & 0x01) {
417 		rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src,
418 				       data, len);
419 		return;
420 	}
421 
422 	if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
423 	    (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
424 		bss = bss_find(wt, hdr->addr1);
425 		if (bss) {
426 			sta = sta_find(bss, hdr->addr2);
427 			if (sta) {
428 				sta->counters[
429 					WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
430 			}
431 			if (!sta || !sta->ptk_set) {
432 				bss2 = bss_find(wt, hdr->addr2);
433 				if (bss2) {
434 					sta2 = sta_find(bss2, hdr->addr1);
435 					if (sta2 && (!sta || sta2->ptk_set)) {
436 						bss = bss2;
437 						sta = sta2;
438 					}
439 				}
440 			}
441 		} else {
442 			bss = bss_find(wt, hdr->addr2);
443 			if (!bss)
444 				return;
445 			sta = sta_find(bss, hdr->addr1);
446 		}
447 	} else if (fc & WLAN_FC_TODS) {
448 		bss = bss_get(wt, hdr->addr1);
449 		if (bss == NULL)
450 			return;
451 		sta = sta_get(bss, hdr->addr2);
452 		if (sta)
453 			sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
454 	} else if (fc & WLAN_FC_FROMDS) {
455 		bss = bss_get(wt, hdr->addr2);
456 		if (bss == NULL)
457 			return;
458 		sta = sta_get(bss, hdr->addr1);
459 	} else {
460 		bss = bss_get(wt, hdr->addr3);
461 		if (bss == NULL)
462 			return;
463 		sta = sta_find(bss, hdr->addr2);
464 		sta2 = sta_find(bss, hdr->addr1);
465 		if (sta == NULL || sta2 == NULL)
466 			return;
467 		found = NULL;
468 		dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
469 		{
470 			if ((tdls->init == sta && tdls->resp == sta2) ||
471 			    (tdls->init == sta2 && tdls->resp == sta)) {
472 				found = tdls;
473 				if (tdls->link_up)
474 					break;
475 			}
476 		}
477 		if (found) {
478 			if (!found->link_up)
479 				add_note(wt, MSG_DEBUG,
480 					 "TDLS: Link not up, but Data "
481 					 "frame seen");
482 			tk = found->tpk.tk;
483 			tdls = found;
484 		}
485 	}
486 	check_plaintext_prot(wt, hdr, data, len);
487 	if ((sta == NULL ||
488 	     (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
489 	    tk == NULL) {
490 		add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
491 		if (dl_list_empty(&wt->ptk)) {
492 			if (len >= 4 && sta) {
493 				keyid = data[3] >> 6;
494 				only_zero_tk = 1;
495 				goto check_zero_tk;
496 			}
497 			return;
498 		}
499 
500 		try_ptk_iter = 1;
501 	}
502 
503 	if (len < 4) {
504 		add_note(wt, MSG_INFO, "Too short encrypted data frame");
505 		return;
506 	}
507 
508 	if (sta == NULL)
509 		return;
510 	if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
511 	    !(data[3] & 0x20)) {
512 		add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
513 			 MACSTR " did not have ExtIV bit set to 1",
514 			 MAC2STR(src));
515 		return;
516 	}
517 
518 	if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
519 		if (data[3] & 0x1f) {
520 			add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
521 				 " used non-zero reserved bit",
522 				 MAC2STR(hdr->addr2));
523 		}
524 		if (data[1] != ((data[0] | 0x20) & 0x7f)) {
525 			add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
526 				 " used incorrect WEPSeed[1] (was 0x%x, "
527 				 "expected 0x%x)",
528 				 MAC2STR(hdr->addr2), data[1],
529 				 (data[0] | 0x20) & 0x7f);
530 		}
531 	} else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
532 		if (data[2] != 0 || (data[3] & 0x1f) != 0) {
533 			add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
534 				 " used non-zero reserved bit",
535 				 MAC2STR(hdr->addr2));
536 		}
537 	}
538 
539 	keyid = data[3] >> 6;
540 	if (keyid != 0 &&
541 	    (!(sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST) ||
542 	     !(bss->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST) ||
543 	     keyid != 1)) {
544 		add_note(wt, MSG_INFO,
545 			 "Unexpected KeyID %d in individually addressed Data frame from "
546 			 MACSTR,
547 			 keyid, MAC2STR(hdr->addr2));
548 	}
549 
550 	if (qos) {
551 		tid = qos[0] & 0x0f;
552 		if (fc & WLAN_FC_TODS)
553 			sta->tx_tid[tid]++;
554 		else
555 			sta->rx_tid[tid]++;
556 	} else {
557 		tid = 0;
558 		if (fc & WLAN_FC_TODS)
559 			sta->tx_tid[16]++;
560 		else
561 			sta->rx_tid[16]++;
562 	}
563 	if (tk) {
564 		if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
565 			rsc = tdls->rsc_init[tid];
566 		else
567 			rsc = tdls->rsc_resp[tid];
568 	} else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
569 		   (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
570 		if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0)
571 			rsc = sta->rsc_tods[tid];
572 		else
573 			rsc = sta->rsc_fromds[tid];
574 	} else if (fc & WLAN_FC_TODS)
575 		rsc = sta->rsc_tods[tid];
576 	else
577 		rsc = sta->rsc_fromds[tid];
578 
579 
580 	if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
581 		tkip_get_pn(pn, data);
582 	else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
583 		goto skip_replay_det;
584 	else
585 		ccmp_get_pn(pn, data);
586 	if (os_memcmp(pn, rsc, 6) <= 0) {
587 		char pn_hex[6 * 2 + 1], rsc_hex[6 * 2 + 1];
588 
589 		wpa_snprintf_hex(pn_hex, sizeof(pn_hex), pn, 6);
590 		wpa_snprintf_hex(rsc_hex, sizeof(rsc_hex), rsc, 6);
591 		add_note(wt, MSG_INFO, "replay detected: A1=" MACSTR
592 			 " A2=" MACSTR " A3=" MACSTR
593 			 " seq=%u frag=%u%s keyid=%d tid=%d #%u %s<=%s",
594 			 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
595 			 MAC2STR(hdr->addr3),
596 			 WLAN_GET_SEQ_SEQ(seq_ctrl),
597 			 WLAN_GET_SEQ_FRAG(seq_ctrl),
598 			 (le_to_host16(hdr->frame_control) &  WLAN_FC_RETRY) ?
599 			 " Retry" : "",
600 			 keyid, tid, wt->frame_num, pn_hex, rsc_hex);
601 		replay = 1;
602 	}
603 
604 skip_replay_det:
605 	if (tk) {
606 		if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256) {
607 			decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
608 			write_decrypted_note(wt, decrypted, tk, 32, keyid);
609 		} else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
610 			   sta->pairwise_cipher == WPA_CIPHER_GCMP_256) {
611 			decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data,
612 						 len, &dlen);
613 			write_decrypted_note(wt, decrypted, tk, sta->ptk.tk_len,
614 					     keyid);
615 		} else {
616 			decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
617 			write_decrypted_note(wt, decrypted, tk, 16, keyid);
618 		}
619 	} else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
620 		enum michael_mic_result mic_res;
621 
622 		decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen,
623 					 &mic_res, &wt->tkip_frag);
624 		if (decrypted && mic_res == MICHAEL_MIC_INCORRECT)
625 			add_note(wt, MSG_INFO, "Invalid Michael MIC");
626 		else if (decrypted && mic_res == MICHAEL_MIC_NOT_VERIFIED)
627 			add_note(wt, MSG_DEBUG, "Michael MIC not verified");
628 		write_decrypted_note(wt, decrypted, sta->ptk.tk, 32, keyid);
629 	} else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
630 		decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
631 	} else if (sta->ptk_set) {
632 		decrypted = try_ptk_decrypt(wt, sta, hdr, keyid, data, len,
633 					    sta->ptk.tk, sta->ptk.tk_len,
634 					    &dlen);
635 	} else {
636 		decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
637 					data, len, &dlen);
638 		ptk_iter_done = 1;
639 	}
640 	if (!decrypted && !ptk_iter_done) {
641 		decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, keyid,
642 					data, len, &dlen);
643 		if (decrypted) {
644 			add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
645 		}
646 	}
647 check_zero_tk:
648 	if (!decrypted) {
649 		struct wpa_ptk zero_ptk;
650 		int old_debug_level = wpa_debug_level;
651 
652 		os_memset(&zero_ptk, 0, sizeof(zero_ptk));
653 		zero_ptk.tk_len = wpa_cipher_key_len(sta->pairwise_cipher);
654 		wpa_debug_level = MSG_ERROR;
655 		decrypted = try_ptk(wt, sta->pairwise_cipher, &zero_ptk, hdr,
656 				    data, len, &dlen);
657 		wpa_debug_level = old_debug_level;
658 		if (decrypted) {
659 			add_note(wt, MSG_DEBUG,
660 				 "Frame was encrypted with zero TK");
661 			wpa_printf(MSG_INFO, "Zero TK used in frame #%u: A2="
662 				   MACSTR " seq=%u",
663 				   wt->frame_num, MAC2STR(hdr->addr2),
664 				   WLAN_GET_SEQ_SEQ(
665 					   le_to_host16(hdr->seq_ctrl)));
666 			write_decrypted_note(wt, decrypted, zero_ptk.tk,
667 					     zero_ptk.tk_len, keyid);
668 		}
669 	}
670 	if (decrypted) {
671 		u16 fc = le_to_host16(hdr->frame_control);
672 		const u8 *peer_addr = NULL;
673 		if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
674 			peer_addr = hdr->addr1;
675 		if (!replay && rsc)
676 			os_memcpy(rsc, pn, 6);
677 		rx_data_process(wt, bss, bss->bssid, sta->addr, dst, src,
678 				decrypted, dlen, 1, peer_addr, qos);
679 		write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
680 				     decrypted, dlen);
681 	} else if (sta->tptk_set) {
682 		/* Check whether TPTK has a matching TK that could be used to
683 		 * decrypt the frame. That could happen if EAPOL-Key msg 4/4
684 		 * was missing in the capture and this was PTK rekeying. */
685 		decrypted = try_ptk_decrypt(wt, sta, hdr, keyid, data, len,
686 					    sta->tptk.tk, sta->tptk.tk_len,
687 					    &dlen);
688 		if (decrypted) {
689 			add_note(wt, MSG_DEBUG,
690 				 "Update PTK (rekeying; no valid EAPOL-Key msg 4/4 seen)");
691 			os_memcpy(&sta->ptk, &sta->tptk, sizeof(sta->ptk));
692 			sta->ptk_set = 1;
693 			sta->tptk_set = 0;
694 			os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
695 			os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
696 		}
697 	} else {
698 		if (!try_ptk_iter && !only_zero_tk) {
699 			wpa_printf(MSG_DEBUG,
700 				   "Failed to decrypt frame #%u A2=" MACSTR
701 				   " seq=%u",
702 				   wt->frame_num, MAC2STR(hdr->addr2),
703 				   WLAN_GET_SEQ_SEQ(seq_ctrl));
704 			add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
705 		}
706 
707 		/* Assume the frame was corrupted and there was no FCS to check.
708 		 * Allow retry of this particular frame to be processed so that
709 		 * it could end up getting decrypted if it was received without
710 		 * corruption. */
711 		sta->allow_duplicate = 1;
712 	}
713 	os_free(decrypted);
714 }
715 
716 
rx_data_bss(struct wlantest * wt,const struct ieee80211_hdr * hdr,size_t hdrlen,const u8 * qos,const u8 * dst,const u8 * src,const u8 * data,size_t len)717 static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
718 			size_t hdrlen, const u8 *qos, const u8 *dst,
719 			const u8 *src, const u8 *data, size_t len)
720 {
721 	u16 fc = le_to_host16(hdr->frame_control);
722 	int prot = !!(fc & WLAN_FC_ISWEP);
723 
724 	if (qos) {
725 		u8 ack = (qos[0] & 0x60) >> 5;
726 		wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
727 			   " len=%u%s tid=%u%s%s",
728 			   MAC2STR(src), MAC2STR(dst), (unsigned int) len,
729 			   prot ? " Prot" : "", qos[0] & 0x0f,
730 			   (qos[0] & 0x10) ? " EOSP" : "",
731 			   ack == 0 ? "" :
732 			   (ack == 1 ? " NoAck" :
733 			    (ack == 2 ? " NoExpAck" : " BA")));
734 	} else {
735 		wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
736 			   " len=%u%s",
737 			   MAC2STR(src), MAC2STR(dst), (unsigned int) len,
738 			   prot ? " Prot" : "");
739 	}
740 
741 	if (prot)
742 		rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
743 	else {
744 		const u8 *bssid, *sta_addr, *peer_addr;
745 		struct wlantest_bss *bss;
746 
747 		if (fc & WLAN_FC_TODS) {
748 			bssid = hdr->addr1;
749 			sta_addr = hdr->addr2;
750 			peer_addr = NULL;
751 		} else if (fc & WLAN_FC_FROMDS) {
752 			bssid = hdr->addr2;
753 			sta_addr = hdr->addr1;
754 			peer_addr = NULL;
755 		} else {
756 			bssid = hdr->addr3;
757 			sta_addr = hdr->addr2;
758 			peer_addr = hdr->addr1;
759 		}
760 
761 		bss = bss_get(wt, bssid);
762 		if (bss) {
763 			struct wlantest_sta *sta = sta_get(bss, sta_addr);
764 
765 			if (sta) {
766 				if (qos) {
767 					int tid = qos[0] & 0x0f;
768 					if (fc & WLAN_FC_TODS)
769 						sta->tx_tid[tid]++;
770 					else
771 						sta->rx_tid[tid]++;
772 				} else {
773 					if (fc & WLAN_FC_TODS)
774 						sta->tx_tid[16]++;
775 					else
776 						sta->rx_tid[16]++;
777 				}
778 			}
779 		}
780 
781 		rx_data_process(wt, bss, bssid, sta_addr, dst, src, data, len,
782 				0, peer_addr, qos);
783 	}
784 }
785 
786 
get_tdls(struct wlantest * wt,const u8 * bssid,const u8 * sta1_addr,const u8 * sta2_addr)787 static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
788 				       const u8 *sta1_addr,
789 				       const u8 *sta2_addr)
790 {
791 	struct wlantest_bss *bss;
792 	struct wlantest_sta *sta1, *sta2;
793 	struct wlantest_tdls *tdls, *found = NULL;
794 
795 	bss = bss_find(wt, bssid);
796 	if (bss == NULL)
797 		return NULL;
798 	sta1 = sta_find(bss, sta1_addr);
799 	if (sta1 == NULL)
800 		return NULL;
801 	sta2 = sta_find(bss, sta2_addr);
802 	if (sta2 == NULL)
803 		return NULL;
804 
805 	dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
806 		if ((tdls->init == sta1 && tdls->resp == sta2) ||
807 		    (tdls->init == sta2 && tdls->resp == sta1)) {
808 			found = tdls;
809 			if (tdls->link_up)
810 				break;
811 		}
812 	}
813 
814 	return found;
815 }
816 
817 
add_direct_link(struct wlantest * wt,const u8 * bssid,const u8 * sta1_addr,const u8 * sta2_addr)818 static void add_direct_link(struct wlantest *wt, const u8 *bssid,
819 			    const u8 *sta1_addr, const u8 *sta2_addr)
820 {
821 	struct wlantest_tdls *tdls;
822 
823 	tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
824 	if (tdls == NULL)
825 		return;
826 
827 	if (tdls->link_up)
828 		tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
829 	else
830 		tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
831 }
832 
833 
add_ap_path(struct wlantest * wt,const u8 * bssid,const u8 * sta1_addr,const u8 * sta2_addr)834 static void add_ap_path(struct wlantest *wt, const u8 *bssid,
835 			const u8 *sta1_addr, const u8 *sta2_addr)
836 {
837 	struct wlantest_tdls *tdls;
838 
839 	tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
840 	if (tdls == NULL)
841 		return;
842 
843 	if (tdls->link_up)
844 		tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
845 	else
846 		tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
847 }
848 
849 
rx_data(struct wlantest * wt,const u8 * data,size_t len)850 void rx_data(struct wlantest *wt, const u8 *data, size_t len)
851 {
852 	const struct ieee80211_hdr *hdr;
853 	u16 fc, stype;
854 	size_t hdrlen;
855 	const u8 *qos = NULL;
856 
857 	if (len < 24)
858 		return;
859 
860 	hdr = (const struct ieee80211_hdr *) data;
861 	fc = le_to_host16(hdr->frame_control);
862 	stype = WLAN_FC_GET_STYPE(fc);
863 	hdrlen = 24;
864 	if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
865 	    (WLAN_FC_TODS | WLAN_FC_FROMDS))
866 		hdrlen += ETH_ALEN;
867 	if (stype & 0x08) {
868 		qos = data + hdrlen;
869 		hdrlen += 2;
870 	}
871 	if ((fc & WLAN_FC_HTC) && (stype & 0x08))
872 		hdrlen += 4; /* HT Control field */
873 	if (len < hdrlen)
874 		return;
875 	wt->rx_data++;
876 
877 	switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
878 	case 0:
879 		wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
880 			   MACSTR " BSSID=" MACSTR,
881 			   data_stype(WLAN_FC_GET_STYPE(fc)),
882 			   fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
883 			   fc & WLAN_FC_ISWEP ? " Prot" : "",
884 			   MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
885 			   MAC2STR(hdr->addr3));
886 		add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
887 		rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
888 			    data + hdrlen, len - hdrlen);
889 		break;
890 	case WLAN_FC_FROMDS:
891 		wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
892 			   " BSSID=" MACSTR " SA=" MACSTR,
893 			   data_stype(WLAN_FC_GET_STYPE(fc)),
894 			   fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
895 			   fc & WLAN_FC_ISWEP ? " Prot" : "",
896 			   MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
897 			   MAC2STR(hdr->addr3));
898 		add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
899 		rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
900 			    data + hdrlen, len - hdrlen);
901 		break;
902 	case WLAN_FC_TODS:
903 		wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
904 			   " SA=" MACSTR " DA=" MACSTR,
905 			   data_stype(WLAN_FC_GET_STYPE(fc)),
906 			   fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
907 			   fc & WLAN_FC_ISWEP ? " Prot" : "",
908 			   MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
909 			   MAC2STR(hdr->addr3));
910 		add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
911 		rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
912 			    data + hdrlen, len - hdrlen);
913 		break;
914 	case WLAN_FC_TODS | WLAN_FC_FROMDS:
915 		wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
916 			   MACSTR " DA=" MACSTR " SA=" MACSTR,
917 			   data_stype(WLAN_FC_GET_STYPE(fc)),
918 			   fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
919 			   fc & WLAN_FC_ISWEP ? " Prot" : "",
920 			   MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
921 			   MAC2STR(hdr->addr3),
922 			   MAC2STR((const u8 *) (hdr + 1)));
923 		rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
924 			    data + hdrlen, len - hdrlen);
925 		break;
926 	}
927 }
928