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 (os_memcmp(sta->addr, addr, ETH_ALEN) == 0)
24 			return sta;
25 	}
26 
27 	return NULL;
28 }
29 
30 
sta_get(struct wlantest_bss * bss,const u8 * addr)31 struct wlantest_sta * sta_get(struct wlantest_bss *bss, const u8 *addr)
32 {
33 	struct wlantest_sta *sta;
34 
35 	if (addr[0] & 0x01)
36 		return NULL; /* Skip group addressed frames */
37 
38 	sta = sta_find(bss, addr);
39 	if (sta)
40 		return sta;
41 
42 	sta = os_zalloc(sizeof(*sta));
43 	if (sta == NULL)
44 		return NULL;
45 	os_memset(sta->seq_ctrl_to_sta, 0xff, sizeof(sta->seq_ctrl_to_sta));
46 	os_memset(sta->seq_ctrl_to_ap, 0xff, sizeof(sta->seq_ctrl_to_ap));
47 	sta->bss = bss;
48 	os_memcpy(sta->addr, addr, ETH_ALEN);
49 	dl_list_add(&bss->sta, &sta->list);
50 	wpa_printf(MSG_DEBUG, "Discovered new STA " MACSTR " in BSS " MACSTR,
51 		   MAC2STR(sta->addr), MAC2STR(bss->bssid));
52 	return sta;
53 }
54 
55 
sta_deinit(struct wlantest_sta * sta)56 void sta_deinit(struct wlantest_sta *sta)
57 {
58 	dl_list_del(&sta->list);
59 	os_free(sta->assocreq_ies);
60 	os_free(sta);
61 }
62 
63 
sta_update_assoc(struct wlantest_sta * sta,struct ieee802_11_elems * elems)64 void sta_update_assoc(struct wlantest_sta *sta, struct ieee802_11_elems *elems)
65 {
66 	struct wpa_ie_data data;
67 	struct wlantest_bss *bss = sta->bss;
68 
69 	if (elems->wpa_ie && !bss->wpaie[0] &&
70 	    (bss->beacon_seen || bss->proberesp_seen)) {
71 		wpa_printf(MSG_INFO, "WPA IE included in Association Request "
72 			   "frame from " MACSTR " even though BSS does not "
73 			   "use WPA - ignore IE",
74 			   MAC2STR(sta->addr));
75 		elems->wpa_ie = NULL;
76 	}
77 
78 	if (elems->rsn_ie && !bss->rsnie[0] &&
79 	    (bss->beacon_seen || bss->proberesp_seen)) {
80 		wpa_printf(MSG_INFO, "RSN IE included in Association Request "
81 			   "frame from " MACSTR " even though BSS does not "
82 			   "use RSN - ignore IE",
83 			   MAC2STR(sta->addr));
84 		elems->rsn_ie = NULL;
85 	}
86 
87 	if (elems->osen && !bss->osenie[0] &&
88 	    (bss->beacon_seen || bss->proberesp_seen)) {
89 		wpa_printf(MSG_INFO, "OSEN IE included in Association Request "
90 			   "frame from " MACSTR " even though BSS does not "
91 			   "use OSEN - ignore IE",
92 			   MAC2STR(sta->addr));
93 		elems->osen = NULL;
94 	}
95 
96 	if (elems->wpa_ie && elems->rsn_ie) {
97 		wpa_printf(MSG_INFO, "Both WPA IE and RSN IE included in "
98 			   "Association Request frame from " MACSTR,
99 			   MAC2STR(sta->addr));
100 	}
101 
102 	if (elems->rsn_ie) {
103 		wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
104 			    elems->rsn_ie_len + 2);
105 		os_memcpy(sta->rsnie, elems->rsn_ie - 2,
106 			  elems->rsn_ie_len + 2);
107 		if (wpa_parse_wpa_ie_rsn(sta->rsnie, 2 + sta->rsnie[1], &data)
108 		    < 0) {
109 			wpa_printf(MSG_INFO, "Failed to parse RSN IE from "
110 				   MACSTR, MAC2STR(sta->addr));
111 		}
112 	} else if (elems->wpa_ie) {
113 		wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
114 			    elems->wpa_ie_len + 2);
115 		os_memcpy(sta->rsnie, elems->wpa_ie - 2,
116 			  elems->wpa_ie_len + 2);
117 		if (wpa_parse_wpa_ie_wpa(sta->rsnie, 2 + sta->rsnie[1], &data)
118 		    < 0) {
119 			wpa_printf(MSG_INFO, "Failed to parse WPA IE from "
120 				   MACSTR, MAC2STR(sta->addr));
121 		}
122 	} else if (elems->osen) {
123 		wpa_hexdump(MSG_DEBUG, "OSEN IE", elems->osen - 2,
124 			    elems->osen_len + 2);
125 		os_memcpy(sta->osenie, elems->osen - 2, elems->osen_len + 2);
126 		sta->proto = WPA_PROTO_OSEN;
127 		sta->pairwise_cipher = WPA_CIPHER_CCMP;
128 		sta->key_mgmt = WPA_KEY_MGMT_OSEN;
129 		sta->rsn_capab = 0;
130 		goto skip_rsn_wpa;
131 	} else {
132 		sta->rsnie[0] = 0;
133 		sta->proto = 0;
134 		sta->pairwise_cipher = 0;
135 		sta->key_mgmt = 0;
136 		sta->rsn_capab = 0;
137 		if (sta->assocreq_capab_info & WLAN_CAPABILITY_PRIVACY)
138 			sta->pairwise_cipher = WPA_CIPHER_WEP40;
139 		goto skip_rsn_wpa;
140 	}
141 
142 	sta->proto = data.proto;
143 	sta->pairwise_cipher = data.pairwise_cipher;
144 	sta->key_mgmt = data.key_mgmt;
145 	sta->rsn_capab = data.capabilities;
146 	if (bss->proto && (sta->proto & bss->proto) == 0) {
147 		wpa_printf(MSG_INFO, "Mismatch in WPA/WPA2 proto: STA "
148 			   MACSTR " 0x%x  BSS " MACSTR " 0x%x",
149 			   MAC2STR(sta->addr), sta->proto,
150 			   MAC2STR(bss->bssid), bss->proto);
151 	}
152 	if (bss->pairwise_cipher &&
153 	    (sta->pairwise_cipher & bss->pairwise_cipher) == 0) {
154 		wpa_printf(MSG_INFO, "Mismatch in pairwise cipher: STA "
155 			   MACSTR " 0x%x  BSS " MACSTR " 0x%x",
156 			   MAC2STR(sta->addr), sta->pairwise_cipher,
157 			   MAC2STR(bss->bssid), bss->pairwise_cipher);
158 	}
159 	if (sta->proto && data.group_cipher != bss->group_cipher &&
160 	    bss->ies_set) {
161 		wpa_printf(MSG_INFO, "Mismatch in group cipher: STA "
162 			   MACSTR " 0x%x != BSS " MACSTR " 0x%x",
163 			   MAC2STR(sta->addr), data.group_cipher,
164 			   MAC2STR(bss->bssid), bss->group_cipher);
165 	}
166 	if ((bss->rsn_capab & WPA_CAPABILITY_MFPR) &&
167 	    !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) {
168 		wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate "
169 			   "without MFP to BSS " MACSTR " that advertises "
170 			   "MFPR", MAC2STR(sta->addr), MAC2STR(bss->bssid));
171 	}
172 	if ((sta->rsn_capab & WPA_CAPABILITY_OCVC) &&
173 	    !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) {
174 		wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate "
175 			   "without MFP to BSS " MACSTR " while supporting "
176 			   "OCV", MAC2STR(sta->addr), MAC2STR(bss->bssid));
177 	}
178 
179 skip_rsn_wpa:
180 	wpa_printf(MSG_INFO, "STA " MACSTR
181 		   " proto=%s%s%s%s"
182 		   "pairwise=%s%s%s%s%s%s%s"
183 		   "key_mgmt=%s%s%s%s%s%s%s%s%s%s%s%s%s%s"
184 		   "rsn_capab=%s%s%s%s%s%s%s%s%s%s",
185 		   MAC2STR(sta->addr),
186 		   sta->proto == 0 ? "OPEN " : "",
187 		   sta->proto & WPA_PROTO_WPA ? "WPA " : "",
188 		   sta->proto & WPA_PROTO_RSN ? "WPA2 " : "",
189 		   sta->proto & WPA_PROTO_OSEN ? "OSEN " : "",
190 		   sta->pairwise_cipher == 0 ? "N/A " : "",
191 		   sta->pairwise_cipher & WPA_CIPHER_NONE ? "NONE " : "",
192 		   sta->pairwise_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
193 		   sta->pairwise_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
194 		   bss->pairwise_cipher & WPA_CIPHER_CCMP_256 ? "CCMP-256 " :
195 		   "",
196 		   bss->pairwise_cipher & WPA_CIPHER_GCMP ? "GCMP " : "",
197 		   bss->pairwise_cipher & WPA_CIPHER_GCMP_256 ? "GCMP-256 " :
198 		   "",
199 		   sta->key_mgmt == 0 ? "N/A " : "",
200 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X ? "EAP " : "",
201 		   sta->key_mgmt & WPA_KEY_MGMT_PSK ? "PSK " : "",
202 		   sta->key_mgmt & WPA_KEY_MGMT_WPA_NONE ? "WPA-NONE " : "",
203 		   sta->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X ? "FT-EAP " : "",
204 		   sta->key_mgmt & WPA_KEY_MGMT_FT_PSK ? "FT-PSK " : "",
205 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256 ?
206 		   "EAP-SHA256 " : "",
207 		   sta->key_mgmt & WPA_KEY_MGMT_PSK_SHA256 ?
208 		   "PSK-SHA256 " : "",
209 		   sta->key_mgmt & WPA_KEY_MGMT_OWE ? "OWE " : "",
210 		   sta->key_mgmt & WPA_KEY_MGMT_PASN ? "PASN " : "",
211 		   sta->key_mgmt & WPA_KEY_MGMT_OSEN ? "OSEN " : "",
212 		   sta->key_mgmt & WPA_KEY_MGMT_DPP ? "DPP " : "",
213 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B ?
214 		   "EAP-SUITE-B " : "",
215 		   sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ?
216 		   "EAP-SUITE-B-192 " : "",
217 		   sta->rsn_capab & WPA_CAPABILITY_PREAUTH ? "PREAUTH " : "",
218 		   sta->rsn_capab & WPA_CAPABILITY_NO_PAIRWISE ?
219 		   "NO_PAIRWISE " : "",
220 		   sta->rsn_capab & WPA_CAPABILITY_MFPR ? "MFPR " : "",
221 		   sta->rsn_capab & WPA_CAPABILITY_MFPC ? "MFPC " : "",
222 		   sta->rsn_capab & WPA_CAPABILITY_PEERKEY_ENABLED ?
223 		   "PEERKEY " : "",
224 		   sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_CAPABLE ?
225 		   "SPP-A-MSDU-CAPAB " : "",
226 		   sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_REQUIRED ?
227 		   "SPP-A-MSDU-REQUIRED " : "",
228 		   sta->rsn_capab & WPA_CAPABILITY_PBAC ? "PBAC " : "",
229 		   sta->rsn_capab & WPA_CAPABILITY_OCVC ? "OCVC " : "",
230 		   sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST ?
231 		   "ExtKeyID " : "");
232 }
233