1 /*
2  * STA list
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 "common/ieee802_11_common.h"
15 #include "wlantest.h"
16 
17 
sta_find(struct wlantest_bss * bss,const u8 * addr)18 struct wlantest_sta * sta_find(struct wlantest_bss *bss, const u8 *addr)
19 {
20 	struct wlantest_sta *sta;
21 
22 	dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
23 		if (ether_addr_equal(sta->addr, addr))
24 			return sta;
25 	}
26 
27 	return NULL;
28 }
29 
30 
sta_find_mlo(struct wlantest * wt,struct wlantest_bss * bss,const u8 * addr)31 struct wlantest_sta * sta_find_mlo(struct wlantest *wt,
32 				   struct wlantest_bss *bss, const u8 *addr)
33 {
34 	struct wlantest_sta *sta;
35 	struct wlantest_bss *obss;
36 	int link_id;
37 
38 	dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
39 		if (ether_addr_equal(sta->addr, addr))
40 			return sta;
41 		if (ether_addr_equal(sta->mld_mac_addr, addr))
42 			return sta;
43 	}
44 
45 	if (is_zero_ether_addr(addr))
46 		return NULL;
47 
48 	dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) {
49 		for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
50 			if (ether_addr_equal(sta->link_addr[link_id], addr))
51 				return sta;
52 		}
53 	}
54 
55 	dl_list_for_each(obss, &wt->bss, struct wlantest_bss, list) {
56 		if (obss == bss)
57 			continue;
58 		if (!is_zero_ether_addr(bss->mld_mac_addr) &&
59 		    !ether_addr_equal(obss->mld_mac_addr, bss->mld_mac_addr))
60 			continue;
61 		dl_list_for_each(sta, &obss->sta, struct wlantest_sta, list) {
62 			if (ether_addr_equal(sta->addr, addr))
63 				return sta;
64 			if (ether_addr_equal(sta->mld_mac_addr, addr))
65 				return sta;
66 			for (link_id = 0; link_id < MAX_NUM_MLD_LINKS;
67 			     link_id++) {
68 				if (ether_addr_equal(sta->link_addr[link_id],
69 						     addr))
70 					return sta;
71 			}
72 		}
73 	}
74 
75 	return NULL;
76 }
77 
78 
sta_get(struct wlantest_bss * bss,const u8 * addr)79 struct wlantest_sta * sta_get(struct wlantest_bss *bss, const u8 *addr)
80 {
81 	struct wlantest_sta *sta;
82 
83 	if (addr[0] & 0x01)
84 		return NULL; /* Skip group addressed frames */
85 
86 	sta = sta_find(bss, addr);
87 	if (sta)
88 		return sta;
89 
90 	sta = os_zalloc(sizeof(*sta));
91 	if (sta == NULL)
92 		return NULL;
93 	os_memset(sta->seq_ctrl_to_sta, 0xff, sizeof(sta->seq_ctrl_to_sta));
94 	os_memset(sta->seq_ctrl_to_ap, 0xff, sizeof(sta->seq_ctrl_to_ap));
95 	sta->bss = bss;
96 	os_memcpy(sta->addr, addr, ETH_ALEN);
97 	dl_list_add(&bss->sta, &sta->list);
98 	wpa_printf(MSG_DEBUG, "Discovered new STA " MACSTR " in BSS " MACSTR
99 		   " (MLD " MACSTR ")",
100 		   MAC2STR(sta->addr),
101 		   MAC2STR(bss->bssid), MAC2STR(bss->mld_mac_addr));
102 	return sta;
103 }
104 
105 
sta_deinit(struct wlantest_sta * sta)106 void sta_deinit(struct wlantest_sta *sta)
107 {
108 	dl_list_del(&sta->list);
109 	os_free(sta->assocreq_ies);
110 	os_free(sta);
111 }
112 
113 
sta_update_assoc_ml(struct wlantest_sta * sta,struct ieee802_11_elems * elems)114 static void sta_update_assoc_ml(struct wlantest_sta *sta,
115 				struct ieee802_11_elems *elems)
116 {
117 	const u8 *mld_addr;
118 
119 	if (!elems->basic_mle)
120 		return;
121 
122 	mld_addr = get_basic_mle_mld_addr(elems->basic_mle,
123 					  elems->basic_mle_len);
124 	if (!mld_addr) {
125 		wpa_printf(MSG_INFO, "MLO: Invalid Basic Multi-Link element");
126 		return;
127 	}
128 
129 	wpa_printf(MSG_DEBUG, "STA MLD Address: " MACSTR, MAC2STR(mld_addr));
130 	os_memcpy(sta->mld_mac_addr, mld_addr, ETH_ALEN);
131 }
132 
133 
sta_update_assoc(struct wlantest_sta * sta,struct ieee802_11_elems * elems)134 void sta_update_assoc(struct wlantest_sta *sta, struct ieee802_11_elems *elems)
135 {
136 	struct wpa_ie_data data;
137 	struct wlantest_bss *bss = sta->bss;
138 
139 	if (elems->wpa_ie && !bss->wpaie[0] &&
140 	    (bss->beacon_seen || bss->proberesp_seen)) {
141 		wpa_printf(MSG_INFO, "WPA IE included in Association Request "
142 			   "frame from " MACSTR " even though BSS does not "
143 			   "use WPA - ignore IE",
144 			   MAC2STR(sta->addr));
145 		elems->wpa_ie = NULL;
146 	}
147 
148 	if (elems->rsn_ie && !bss->rsnie[0] &&
149 	    (bss->beacon_seen || bss->proberesp_seen)) {
150 		wpa_printf(MSG_INFO, "RSN IE included in Association Request "
151 			   "frame from " MACSTR " even though BSS does not "
152 			   "use RSN - ignore IE",
153 			   MAC2STR(sta->addr));
154 		elems->rsn_ie = NULL;
155 	}
156 
157 	if (elems->osen && !bss->osenie[0] &&
158 	    (bss->beacon_seen || bss->proberesp_seen)) {
159 		wpa_printf(MSG_INFO, "OSEN IE included in Association Request "
160 			   "frame from " MACSTR " even though BSS does not "
161 			   "use OSEN - ignore IE",
162 			   MAC2STR(sta->addr));
163 		elems->osen = NULL;
164 	}
165 
166 	if (elems->wpa_ie && elems->rsn_ie) {
167 		wpa_printf(MSG_INFO, "Both WPA IE and RSN IE included in "
168 			   "Association Request frame from " MACSTR,
169 			   MAC2STR(sta->addr));
170 	}
171 
172 	if (elems->rsn_ie) {
173 		wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
174 			    elems->rsn_ie_len + 2);
175 		os_memcpy(sta->rsnie, elems->rsn_ie - 2,
176 			  elems->rsn_ie_len + 2);
177 		if (wpa_parse_wpa_ie_rsn(sta->rsnie, 2 + sta->rsnie[1], &data)
178 		    < 0) {
179 			wpa_printf(MSG_INFO, "Failed to parse RSN IE from "
180 				   MACSTR, MAC2STR(sta->addr));
181 		}
182 	} else if (elems->wpa_ie) {
183 		wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
184 			    elems->wpa_ie_len + 2);
185 		os_memcpy(sta->rsnie, elems->wpa_ie - 2,
186 			  elems->wpa_ie_len + 2);
187 		if (wpa_parse_wpa_ie_wpa(sta->rsnie, 2 + sta->rsnie[1], &data)
188 		    < 0) {
189 			wpa_printf(MSG_INFO, "Failed to parse WPA IE from "
190 				   MACSTR, MAC2STR(sta->addr));
191 		}
192 	} else if (elems->osen) {
193 		wpa_hexdump(MSG_DEBUG, "OSEN IE", elems->osen - 2,
194 			    elems->osen_len + 2);
195 		os_memcpy(sta->osenie, elems->osen - 2, elems->osen_len + 2);
196 		sta->proto = WPA_PROTO_OSEN;
197 		sta->pairwise_cipher = WPA_CIPHER_CCMP;
198 		sta->key_mgmt = WPA_KEY_MGMT_OSEN;
199 		sta->rsn_capab = 0;
200 		goto skip_rsn_wpa;
201 	} else {
202 		sta->rsnie[0] = 0;
203 		sta->proto = 0;
204 		sta->pairwise_cipher = 0;
205 		sta->key_mgmt = 0;
206 		sta->rsn_capab = 0;
207 		if (sta->assocreq_capab_info & WLAN_CAPABILITY_PRIVACY)
208 			sta->pairwise_cipher = WPA_CIPHER_WEP40;
209 		goto skip_rsn_wpa;
210 	}
211 
212 	sta->proto = data.proto;
213 	sta->pairwise_cipher = data.pairwise_cipher;
214 	sta->key_mgmt = data.key_mgmt;
215 	sta->rsn_capab = data.capabilities;
216 	if (bss->proto && (sta->proto & bss->proto) == 0) {
217 		wpa_printf(MSG_INFO, "Mismatch in WPA/WPA2 proto: STA "
218 			   MACSTR " 0x%x  BSS " MACSTR " 0x%x",
219 			   MAC2STR(sta->addr), sta->proto,
220 			   MAC2STR(bss->bssid), bss->proto);
221 	}
222 	if (bss->pairwise_cipher &&
223 	    (sta->pairwise_cipher & bss->pairwise_cipher) == 0) {
224 		wpa_printf(MSG_INFO, "Mismatch in pairwise cipher: STA "
225 			   MACSTR " 0x%x  BSS " MACSTR " 0x%x",
226 			   MAC2STR(sta->addr), sta->pairwise_cipher,
227 			   MAC2STR(bss->bssid), bss->pairwise_cipher);
228 	}
229 	if (sta->proto && data.group_cipher != bss->group_cipher &&
230 	    bss->ies_set) {
231 		wpa_printf(MSG_INFO, "Mismatch in group cipher: STA "
232 			   MACSTR " 0x%x != BSS " MACSTR " 0x%x",
233 			   MAC2STR(sta->addr), data.group_cipher,
234 			   MAC2STR(bss->bssid), bss->group_cipher);
235 	}
236 	if ((bss->rsn_capab & WPA_CAPABILITY_MFPR) &&
237 	    !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) {
238 		wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate "
239 			   "without MFP to BSS " MACSTR " that advertises "
240 			   "MFPR", MAC2STR(sta->addr), MAC2STR(bss->bssid));
241 	}
242 	if ((sta->rsn_capab & WPA_CAPABILITY_OCVC) &&
243 	    !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) {
244 		wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate "
245 			   "without MFP to BSS " MACSTR " while supporting "
246 			   "OCV", MAC2STR(sta->addr), MAC2STR(bss->bssid));
247 	}
248 
249 skip_rsn_wpa:
250 	wpa_printf(MSG_INFO, "STA " MACSTR
251 		   " proto=%s%s%s%s"
252 		   "pairwise=%s%s%s%s%s%s%s"
253 		   "key_mgmt=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
254 		   "rsn_capab=%s%s%s%s%s%s%s%s%s%s",
255 		   MAC2STR(sta->addr),
256 		   sta->proto == 0 ? "OPEN " : "",
257 		   sta->proto & WPA_PROTO_WPA ? "WPA " : "",
258 		   sta->proto & WPA_PROTO_RSN ? "WPA2 " : "",
259 		   sta->proto & WPA_PROTO_OSEN ? "OSEN " : "",
260 		   sta->pairwise_cipher == 0 ? "N/A " : "",
261 		   sta->pairwise_cipher & WPA_CIPHER_NONE ? "NONE " : "",
262 		   sta->pairwise_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
263 		   sta->pairwise_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
264 		   bss->pairwise_cipher & WPA_CIPHER_CCMP_256 ? "CCMP-256 " :
265 		   "",
266 		   bss->pairwise_cipher & WPA_CIPHER_GCMP ? "GCMP " : "",
267 		   bss->pairwise_cipher & WPA_CIPHER_GCMP_256 ? "GCMP-256 " :
268 		   "",
269 		   sta->key_mgmt == 0 ? "N/A " : "",
270 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X ? "EAP " : "",
271 		   sta->key_mgmt & WPA_KEY_MGMT_PSK ? "PSK " : "",
272 		   sta->key_mgmt & WPA_KEY_MGMT_WPA_NONE ? "WPA-NONE " : "",
273 		   sta->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X ? "FT-EAP " : "",
274 		   sta->key_mgmt & WPA_KEY_MGMT_FT_PSK ? "FT-PSK " : "",
275 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256 ?
276 		   "EAP-SHA256 " : "",
277 		   sta->key_mgmt & WPA_KEY_MGMT_PSK_SHA256 ?
278 		   "PSK-SHA256 " : "",
279 		   sta->key_mgmt & WPA_KEY_MGMT_OWE ? "OWE " : "",
280 		   sta->key_mgmt & WPA_KEY_MGMT_PASN ? "PASN " : "",
281 		   sta->key_mgmt & WPA_KEY_MGMT_OSEN ? "OSEN " : "",
282 		   sta->key_mgmt & WPA_KEY_MGMT_DPP ? "DPP " : "",
283 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B ?
284 		   "EAP-SUITE-B " : "",
285 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ?
286 		   "EAP-SUITE-B-192 " : "",
287 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384 ?
288 		   "EAP-SHA384 " : "",
289 		   sta->rsn_capab & WPA_CAPABILITY_PREAUTH ? "PREAUTH " : "",
290 		   sta->rsn_capab & WPA_CAPABILITY_NO_PAIRWISE ?
291 		   "NO_PAIRWISE " : "",
292 		   sta->rsn_capab & WPA_CAPABILITY_MFPR ? "MFPR " : "",
293 		   sta->rsn_capab & WPA_CAPABILITY_MFPC ? "MFPC " : "",
294 		   sta->rsn_capab & WPA_CAPABILITY_PEERKEY_ENABLED ?
295 		   "PEERKEY " : "",
296 		   sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_CAPABLE ?
297 		   "SPP-A-MSDU-CAPAB " : "",
298 		   sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_REQUIRED ?
299 		   "SPP-A-MSDU-REQUIRED " : "",
300 		   sta->rsn_capab & WPA_CAPABILITY_PBAC ? "PBAC " : "",
301 		   sta->rsn_capab & WPA_CAPABILITY_OCVC ? "OCVC " : "",
302 		   sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST ?
303 		   "ExtKeyID " : "");
304 
305 	sta_update_assoc_ml(sta, elems);
306 }
307 
308 
sta_copy_ptk(struct wlantest_sta * sta,struct wpa_ptk * ptk)309 static void sta_copy_ptk(struct wlantest_sta *sta, struct wpa_ptk *ptk)
310 {
311 	os_memcpy(&sta->ptk, ptk, sizeof(*ptk));
312 	sta->ptk_set = 1;
313 	os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
314 	os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
315 }
316 
317 
sta_new_ptk(struct wlantest * wt,struct wlantest_sta * sta,struct wpa_ptk * ptk)318 void sta_new_ptk(struct wlantest *wt, struct wlantest_sta *sta,
319 		 struct wpa_ptk *ptk)
320 {
321 	struct wlantest_bss *bss;
322 	struct wlantest_sta *osta;
323 
324 	add_note(wt, MSG_DEBUG, "Derived new PTK");
325 	sta_copy_ptk(sta, ptk);
326 	wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, sta->ptk.kck_len);
327 	wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, sta->ptk.kek_len);
328 	wpa_hexdump(MSG_DEBUG, "PTK:TK", sta->ptk.tk, sta->ptk.tk_len);
329 
330 	dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
331 		dl_list_for_each(osta, &bss->sta, struct wlantest_sta, list) {
332 			bool match = false;
333 			int link_id;
334 
335 			if (osta == sta)
336 				continue;
337 			if (ether_addr_equal(sta->addr, osta->addr))
338 				match = true;
339 			for (link_id = 0; !match && link_id < MAX_NUM_MLD_LINKS;
340 			     link_id++) {
341 				if (ether_addr_equal(osta->link_addr[link_id],
342 						     sta->addr))
343 					match = true;
344 			}
345 
346 			if (!match)
347 				continue;
348 			if (!ether_addr_equal(sta->bss->mld_mac_addr,
349 					      osta->bss->mld_mac_addr))
350 				continue;
351 			wpa_printf(MSG_DEBUG,
352 				   "Add PTK to another MLO STA entry " MACSTR
353 				   " (MLD " MACSTR " --> " MACSTR ") in BSS "
354 				   MACSTR " (MLD " MACSTR " --> " MACSTR ")",
355 				   MAC2STR(osta->addr),
356 				   MAC2STR(osta->mld_mac_addr),
357 				   MAC2STR(sta->mld_mac_addr),
358 				   MAC2STR(bss->bssid),
359 				   MAC2STR(bss->mld_mac_addr),
360 				   MAC2STR(sta->bss->mld_mac_addr));
361 			sta_copy_ptk(osta, ptk);
362 			os_memcpy(osta->mld_mac_addr, sta->mld_mac_addr,
363 				  ETH_ALEN);
364 		}
365 	}
366 }
367