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->wpa_ie && elems->rsn_ie) {
158 		wpa_printf(MSG_INFO, "Both WPA IE and RSN IE included in "
159 			   "Association Request frame from " MACSTR,
160 			   MAC2STR(sta->addr));
161 	}
162 
163 	if (elems->rsn_ie) {
164 		wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
165 			    elems->rsn_ie_len + 2);
166 		os_memcpy(sta->rsnie, elems->rsn_ie - 2,
167 			  elems->rsn_ie_len + 2);
168 		if (wpa_parse_wpa_ie_rsn(sta->rsnie, 2 + sta->rsnie[1], &data)
169 		    < 0) {
170 			wpa_printf(MSG_INFO, "Failed to parse RSN IE from "
171 				   MACSTR, MAC2STR(sta->addr));
172 		}
173 	} else if (elems->wpa_ie) {
174 		wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
175 			    elems->wpa_ie_len + 2);
176 		os_memcpy(sta->rsnie, elems->wpa_ie - 2,
177 			  elems->wpa_ie_len + 2);
178 		if (wpa_parse_wpa_ie_wpa(sta->rsnie, 2 + sta->rsnie[1], &data)
179 		    < 0) {
180 			wpa_printf(MSG_INFO, "Failed to parse WPA IE from "
181 				   MACSTR, MAC2STR(sta->addr));
182 		}
183 	} else {
184 		sta->rsnie[0] = 0;
185 		sta->proto = 0;
186 		sta->pairwise_cipher = 0;
187 		sta->key_mgmt = 0;
188 		sta->rsn_capab = 0;
189 		if (sta->assocreq_capab_info & WLAN_CAPABILITY_PRIVACY)
190 			sta->pairwise_cipher = WPA_CIPHER_WEP40;
191 		goto skip_rsn_wpa;
192 	}
193 
194 	sta->proto = data.proto;
195 	sta->pairwise_cipher = data.pairwise_cipher;
196 	sta->key_mgmt = data.key_mgmt;
197 	sta->rsn_capab = data.capabilities;
198 	if (bss->proto && (sta->proto & bss->proto) == 0) {
199 		wpa_printf(MSG_INFO, "Mismatch in WPA/WPA2 proto: STA "
200 			   MACSTR " 0x%x  BSS " MACSTR " 0x%x",
201 			   MAC2STR(sta->addr), sta->proto,
202 			   MAC2STR(bss->bssid), bss->proto);
203 	}
204 	if (bss->pairwise_cipher &&
205 	    (sta->pairwise_cipher & bss->pairwise_cipher) == 0) {
206 		wpa_printf(MSG_INFO, "Mismatch in pairwise cipher: STA "
207 			   MACSTR " 0x%x  BSS " MACSTR " 0x%x",
208 			   MAC2STR(sta->addr), sta->pairwise_cipher,
209 			   MAC2STR(bss->bssid), bss->pairwise_cipher);
210 	}
211 	if (sta->proto && data.group_cipher != bss->group_cipher &&
212 	    bss->ies_set) {
213 		wpa_printf(MSG_INFO, "Mismatch in group cipher: STA "
214 			   MACSTR " 0x%x != BSS " MACSTR " 0x%x",
215 			   MAC2STR(sta->addr), data.group_cipher,
216 			   MAC2STR(bss->bssid), bss->group_cipher);
217 	}
218 	if ((bss->rsn_capab & WPA_CAPABILITY_MFPR) &&
219 	    !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) {
220 		wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate "
221 			   "without MFP to BSS " MACSTR " that advertises "
222 			   "MFPR", MAC2STR(sta->addr), MAC2STR(bss->bssid));
223 	}
224 	if ((sta->rsn_capab & WPA_CAPABILITY_OCVC) &&
225 	    !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) {
226 		wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate "
227 			   "without MFP to BSS " MACSTR " while supporting "
228 			   "OCV", MAC2STR(sta->addr), MAC2STR(bss->bssid));
229 	}
230 
231 skip_rsn_wpa:
232 	wpa_printf(MSG_INFO, "STA " MACSTR
233 		   " proto=%s%s%s"
234 		   "pairwise=%s%s%s%s%s%s%s"
235 		   "key_mgmt=%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
236 		   "rsn_capab=%s%s%s%s%s%s%s%s%s%s",
237 		   MAC2STR(sta->addr),
238 		   sta->proto == 0 ? "OPEN " : "",
239 		   sta->proto & WPA_PROTO_WPA ? "WPA " : "",
240 		   sta->proto & WPA_PROTO_RSN ? "WPA2 " : "",
241 		   sta->pairwise_cipher == 0 ? "N/A " : "",
242 		   sta->pairwise_cipher & WPA_CIPHER_NONE ? "NONE " : "",
243 		   sta->pairwise_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
244 		   sta->pairwise_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
245 		   bss->pairwise_cipher & WPA_CIPHER_CCMP_256 ? "CCMP-256 " :
246 		   "",
247 		   bss->pairwise_cipher & WPA_CIPHER_GCMP ? "GCMP " : "",
248 		   bss->pairwise_cipher & WPA_CIPHER_GCMP_256 ? "GCMP-256 " :
249 		   "",
250 		   sta->key_mgmt == 0 ? "N/A " : "",
251 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X ? "EAP " : "",
252 		   sta->key_mgmt & WPA_KEY_MGMT_PSK ? "PSK " : "",
253 		   sta->key_mgmt & WPA_KEY_MGMT_WPA_NONE ? "WPA-NONE " : "",
254 		   sta->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X ? "FT-EAP " : "",
255 		   sta->key_mgmt & WPA_KEY_MGMT_FT_PSK ? "FT-PSK " : "",
256 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256 ?
257 		   "EAP-SHA256 " : "",
258 		   sta->key_mgmt & WPA_KEY_MGMT_PSK_SHA256 ?
259 		   "PSK-SHA256 " : "",
260 		   sta->key_mgmt & WPA_KEY_MGMT_OWE ? "OWE " : "",
261 		   sta->key_mgmt & WPA_KEY_MGMT_PASN ? "PASN " : "",
262 		   sta->key_mgmt & WPA_KEY_MGMT_DPP ? "DPP " : "",
263 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B ?
264 		   "EAP-SUITE-B " : "",
265 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ?
266 		   "EAP-SUITE-B-192 " : "",
267 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384 ?
268 		   "EAP-SHA384 " : "",
269 		   sta->rsn_capab & WPA_CAPABILITY_PREAUTH ? "PREAUTH " : "",
270 		   sta->rsn_capab & WPA_CAPABILITY_NO_PAIRWISE ?
271 		   "NO_PAIRWISE " : "",
272 		   sta->rsn_capab & WPA_CAPABILITY_MFPR ? "MFPR " : "",
273 		   sta->rsn_capab & WPA_CAPABILITY_MFPC ? "MFPC " : "",
274 		   sta->rsn_capab & WPA_CAPABILITY_PEERKEY_ENABLED ?
275 		   "PEERKEY " : "",
276 		   sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_CAPABLE ?
277 		   "SPP-A-MSDU-CAPAB " : "",
278 		   sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_REQUIRED ?
279 		   "SPP-A-MSDU-REQUIRED " : "",
280 		   sta->rsn_capab & WPA_CAPABILITY_PBAC ? "PBAC " : "",
281 		   sta->rsn_capab & WPA_CAPABILITY_OCVC ? "OCVC " : "",
282 		   sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST ?
283 		   "ExtKeyID " : "");
284 
285 	sta_update_assoc_ml(sta, elems);
286 }
287 
288 
sta_copy_ptk(struct wlantest_sta * sta,struct wpa_ptk * ptk)289 static void sta_copy_ptk(struct wlantest_sta *sta, struct wpa_ptk *ptk)
290 {
291 	os_memcpy(&sta->ptk, ptk, sizeof(*ptk));
292 	sta->ptk_set = 1;
293 	os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
294 	os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
295 }
296 
297 
sta_new_ptk(struct wlantest * wt,struct wlantest_sta * sta,struct wpa_ptk * ptk)298 void sta_new_ptk(struct wlantest *wt, struct wlantest_sta *sta,
299 		 struct wpa_ptk *ptk)
300 {
301 	struct wlantest_bss *bss;
302 	struct wlantest_sta *osta;
303 
304 	add_note(wt, MSG_DEBUG, "Derived new PTK");
305 	sta_copy_ptk(sta, ptk);
306 	wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, sta->ptk.kck_len);
307 	wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, sta->ptk.kek_len);
308 	wpa_hexdump(MSG_DEBUG, "PTK:TK", sta->ptk.tk, sta->ptk.tk_len);
309 
310 	dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
311 		dl_list_for_each(osta, &bss->sta, struct wlantest_sta, list) {
312 			bool match = false;
313 			int link_id;
314 
315 			if (osta == sta)
316 				continue;
317 			if (ether_addr_equal(sta->addr, osta->addr))
318 				match = true;
319 			for (link_id = 0; !match && link_id < MAX_NUM_MLD_LINKS;
320 			     link_id++) {
321 				if (ether_addr_equal(osta->link_addr[link_id],
322 						     sta->addr))
323 					match = true;
324 			}
325 
326 			if (!match)
327 				continue;
328 			if (!ether_addr_equal(sta->bss->mld_mac_addr,
329 					      osta->bss->mld_mac_addr))
330 				continue;
331 			wpa_printf(MSG_DEBUG,
332 				   "Add PTK to another MLO STA entry " MACSTR
333 				   " (MLD " MACSTR " --> " MACSTR ") in BSS "
334 				   MACSTR " (MLD " MACSTR " --> " MACSTR ")",
335 				   MAC2STR(osta->addr),
336 				   MAC2STR(osta->mld_mac_addr),
337 				   MAC2STR(sta->mld_mac_addr),
338 				   MAC2STR(bss->bssid),
339 				   MAC2STR(bss->mld_mac_addr),
340 				   MAC2STR(sta->bss->mld_mac_addr));
341 			sta_copy_ptk(osta, ptk);
342 			os_memcpy(osta->mld_mac_addr, sta->mld_mac_addr,
343 				  ETH_ALEN);
344 		}
345 	}
346 }
347