1 /*
2  * Common driver-related functions
3  * Copyright (c) 2003-2017, 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 "includes.h"
10 #include "utils/common.h"
11 #include "driver.h"
12 
wpa_scan_results_free(struct wpa_scan_results * res)13 void wpa_scan_results_free(struct wpa_scan_results *res)
14 {
15 	size_t i;
16 
17 	if (res == NULL)
18 		return;
19 
20 	for (i = 0; i < res->num; i++)
21 		os_free(res->res[i]);
22 	os_free(res->res);
23 	os_free(res);
24 }
25 
26 
event_to_string(enum wpa_event_type event)27 const char * event_to_string(enum wpa_event_type event)
28 {
29 #define E2S(n) case EVENT_ ## n: return #n
30 	switch (event) {
31 	E2S(ASSOC);
32 	E2S(DISASSOC);
33 	E2S(MICHAEL_MIC_FAILURE);
34 	E2S(SCAN_RESULTS);
35 	E2S(ASSOCINFO);
36 	E2S(INTERFACE_STATUS);
37 	E2S(PMKID_CANDIDATE);
38 	E2S(TDLS);
39 	E2S(FT_RESPONSE);
40 	E2S(IBSS_RSN_START);
41 	E2S(AUTH);
42 	E2S(DEAUTH);
43 	E2S(ASSOC_REJECT);
44 	E2S(AUTH_TIMED_OUT);
45 	E2S(ASSOC_TIMED_OUT);
46 	E2S(WPS_BUTTON_PUSHED);
47 	E2S(TX_STATUS);
48 	E2S(RX_FROM_UNKNOWN);
49 	E2S(RX_MGMT);
50 	E2S(REMAIN_ON_CHANNEL);
51 	E2S(CANCEL_REMAIN_ON_CHANNEL);
52 	E2S(RX_PROBE_REQ);
53 	E2S(NEW_STA);
54 	E2S(EAPOL_RX);
55 	E2S(SIGNAL_CHANGE);
56 	E2S(INTERFACE_ENABLED);
57 	E2S(INTERFACE_DISABLED);
58 	E2S(CHANNEL_LIST_CHANGED);
59 	E2S(INTERFACE_UNAVAILABLE);
60 	E2S(BEST_CHANNEL);
61 	E2S(UNPROT_DEAUTH);
62 	E2S(UNPROT_DISASSOC);
63 	E2S(STATION_LOW_ACK);
64 	E2S(IBSS_PEER_LOST);
65 	E2S(DRIVER_GTK_REKEY);
66 	E2S(SCHED_SCAN_STOPPED);
67 	E2S(DRIVER_CLIENT_POLL_OK);
68 	E2S(EAPOL_TX_STATUS);
69 	E2S(CH_SWITCH);
70 	E2S(CH_SWITCH_STARTED);
71 	E2S(WNM);
72 	E2S(CONNECT_FAILED_REASON);
73 	E2S(DFS_RADAR_DETECTED);
74 	E2S(DFS_CAC_FINISHED);
75 	E2S(DFS_CAC_ABORTED);
76 	E2S(DFS_NOP_FINISHED);
77 	E2S(SURVEY);
78 	E2S(SCAN_STARTED);
79 	E2S(AVOID_FREQUENCIES);
80 	E2S(NEW_PEER_CANDIDATE);
81 	E2S(ACS_CHANNEL_SELECTED);
82 	E2S(DFS_CAC_STARTED);
83 	E2S(P2P_LO_STOP);
84 	E2S(BEACON_LOSS);
85 	E2S(DFS_PRE_CAC_EXPIRED);
86 	E2S(EXTERNAL_AUTH);
87 	E2S(PORT_AUTHORIZED);
88 	E2S(STATION_OPMODE_CHANGED);
89 	E2S(INTERFACE_MAC_CHANGED);
90 	E2S(WDS_STA_INTERFACE_STATUS);
91 	E2S(UPDATE_DH);
92 	E2S(UNPROT_BEACON);
93 	E2S(TX_WAIT_EXPIRE);
94 	}
95 
96 	return "UNKNOWN";
97 #undef E2S
98 }
99 
100 
channel_width_to_string(enum chan_width width)101 const char * channel_width_to_string(enum chan_width width)
102 {
103 	switch (width) {
104 	case CHAN_WIDTH_20_NOHT:
105 		return "20 MHz (no HT)";
106 	case CHAN_WIDTH_20:
107 		return "20 MHz";
108 	case CHAN_WIDTH_40:
109 		return "40 MHz";
110 	case CHAN_WIDTH_80:
111 		return "80 MHz";
112 	case CHAN_WIDTH_80P80:
113 		return "80+80 MHz";
114 	case CHAN_WIDTH_160:
115 		return "160 MHz";
116 	default:
117 		return "unknown";
118 	}
119 }
120 
121 
channel_width_to_int(enum chan_width width)122 int channel_width_to_int(enum chan_width width)
123 {
124 	switch (width) {
125 	case CHAN_WIDTH_20_NOHT:
126 	case CHAN_WIDTH_20:
127 		return 20;
128 	case CHAN_WIDTH_40:
129 		return 40;
130 	case CHAN_WIDTH_80:
131 		return 80;
132 	case CHAN_WIDTH_80P80:
133 	case CHAN_WIDTH_160:
134 		return 160;
135 	default:
136 		return 0;
137 	}
138 }
139 
140 
ht_supported(const struct hostapd_hw_modes * mode)141 int ht_supported(const struct hostapd_hw_modes *mode)
142 {
143 	if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
144 		/*
145 		 * The driver did not indicate whether it supports HT. Assume
146 		 * it does to avoid connection issues.
147 		 */
148 		return 1;
149 	}
150 
151 	/*
152 	 * IEEE Std 802.11n-2009 20.1.1:
153 	 * An HT non-AP STA shall support all EQM rates for one spatial stream.
154 	 */
155 	return mode->mcs_set[0] == 0xff;
156 }
157 
158 
vht_supported(const struct hostapd_hw_modes * mode)159 int vht_supported(const struct hostapd_hw_modes *mode)
160 {
161 	if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
162 		/*
163 		 * The driver did not indicate whether it supports VHT. Assume
164 		 * it does to avoid connection issues.
165 		 */
166 		return 1;
167 	}
168 
169 	/*
170 	 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
171 	 * TODO: Verify if this complies with the standard
172 	 */
173 	return (mode->vht_mcs_set[0] & 0x3) != 3;
174 }
175 
176 
wpa_check_wowlan_trigger(const char * start,const char * trigger,int capa_trigger,u8 * param_trigger)177 static int wpa_check_wowlan_trigger(const char *start, const char *trigger,
178 				    int capa_trigger, u8 *param_trigger)
179 {
180 	if (os_strcmp(start, trigger) != 0)
181 		return 0;
182 	if (!capa_trigger)
183 		return 0;
184 
185 	*param_trigger = 1;
186 	return 1;
187 }
188 
189 
190 struct wowlan_triggers *
wpa_get_wowlan_triggers(const char * wowlan_triggers,const struct wpa_driver_capa * capa)191 wpa_get_wowlan_triggers(const char *wowlan_triggers,
192 			const struct wpa_driver_capa *capa)
193 {
194 	struct wowlan_triggers *triggers;
195 	char *start, *end, *buf;
196 	int last;
197 
198 	if (!wowlan_triggers)
199 		return NULL;
200 
201 	buf = os_strdup(wowlan_triggers);
202 	if (buf == NULL)
203 		return NULL;
204 
205 	triggers = os_zalloc(sizeof(*triggers));
206 	if (triggers == NULL)
207 		goto out;
208 
209 #define CHECK_TRIGGER(trigger) \
210 	wpa_check_wowlan_trigger(start, #trigger,			\
211 				  capa->wowlan_triggers.trigger,	\
212 				  &triggers->trigger)
213 
214 	start = buf;
215 	while (*start != '\0') {
216 		while (isblank((unsigned char) *start))
217 			start++;
218 		if (*start == '\0')
219 			break;
220 		end = start;
221 		while (!isblank((unsigned char) *end) && *end != '\0')
222 			end++;
223 		last = *end == '\0';
224 		*end = '\0';
225 
226 		if (!CHECK_TRIGGER(any) &&
227 		    !CHECK_TRIGGER(disconnect) &&
228 		    !CHECK_TRIGGER(magic_pkt) &&
229 		    !CHECK_TRIGGER(gtk_rekey_failure) &&
230 		    !CHECK_TRIGGER(eap_identity_req) &&
231 		    !CHECK_TRIGGER(four_way_handshake) &&
232 		    !CHECK_TRIGGER(rfkill_release)) {
233 			wpa_printf(MSG_DEBUG,
234 				   "Unknown/unsupported wowlan trigger '%s'",
235 				   start);
236 			os_free(triggers);
237 			triggers = NULL;
238 			goto out;
239 		}
240 
241 		if (last)
242 			break;
243 		start = end + 1;
244 	}
245 #undef CHECK_TRIGGER
246 
247 out:
248 	os_free(buf);
249 	return triggers;
250 }
251 
252 
driver_flag_to_string(u64 flag)253 const char * driver_flag_to_string(u64 flag)
254 {
255 #define DF2S(x) case WPA_DRIVER_FLAGS_ ## x: return #x
256 	switch (flag) {
257 	DF2S(DRIVER_IE);
258 	DF2S(SET_KEYS_AFTER_ASSOC);
259 	DF2S(DFS_OFFLOAD);
260 	DF2S(4WAY_HANDSHAKE_PSK);
261 	DF2S(4WAY_HANDSHAKE_8021X);
262 	DF2S(WIRED);
263 	DF2S(SME);
264 	DF2S(AP);
265 	DF2S(SET_KEYS_AFTER_ASSOC_DONE);
266 	DF2S(HT_2040_COEX);
267 	DF2S(P2P_CONCURRENT);
268 	DF2S(P2P_DEDICATED_INTERFACE);
269 	DF2S(P2P_CAPABLE);
270 	DF2S(AP_TEARDOWN_SUPPORT);
271 	DF2S(P2P_MGMT_AND_NON_P2P);
272 	DF2S(VALID_ERROR_CODES);
273 	DF2S(OFFCHANNEL_TX);
274 	DF2S(EAPOL_TX_STATUS);
275 	DF2S(DEAUTH_TX_STATUS);
276 	DF2S(BSS_SELECTION);
277 	DF2S(TDLS_SUPPORT);
278 	DF2S(TDLS_EXTERNAL_SETUP);
279 	DF2S(PROBE_RESP_OFFLOAD);
280 	DF2S(AP_UAPSD);
281 	DF2S(INACTIVITY_TIMER);
282 	DF2S(AP_MLME);
283 	DF2S(SAE);
284 	DF2S(OBSS_SCAN);
285 	DF2S(IBSS);
286 	DF2S(RADAR);
287 	DF2S(DEDICATED_P2P_DEVICE);
288 	DF2S(QOS_MAPPING);
289 	DF2S(AP_CSA);
290 	DF2S(MESH);
291 	DF2S(ACS_OFFLOAD);
292 	DF2S(KEY_MGMT_OFFLOAD);
293 	DF2S(TDLS_CHANNEL_SWITCH);
294 	DF2S(HT_IBSS);
295 	DF2S(VHT_IBSS);
296 	DF2S(SUPPORT_HW_MODE_ANY);
297 	DF2S(OFFCHANNEL_SIMULTANEOUS);
298 	DF2S(FULL_AP_CLIENT_STATE);
299 	DF2S(P2P_LISTEN_OFFLOAD);
300 	DF2S(SUPPORT_FILS);
301 	DF2S(BEACON_RATE_LEGACY);
302 	DF2S(BEACON_RATE_HT);
303 	DF2S(BEACON_RATE_VHT);
304 	DF2S(MGMT_TX_RANDOM_TA);
305 	DF2S(MGMT_TX_RANDOM_TA_CONNECTED);
306 	DF2S(SCHED_SCAN_RELATIVE_RSSI);
307 	DF2S(HE_CAPABILITIES);
308 	DF2S(FILS_SK_OFFLOAD);
309 	DF2S(OCE_STA);
310 	DF2S(OCE_AP);
311 	DF2S(OCE_STA_CFON);
312 	DF2S(MFP_OPTIONAL);
313 	DF2S(SELF_MANAGED_REGULATORY);
314 	DF2S(FTM_RESPONDER);
315 	DF2S(CONTROL_PORT);
316 	DF2S(VLAN_OFFLOAD);
317 	DF2S(UPDATE_FT_IES);
318 	DF2S(SAFE_PTK0_REKEYS);
319 	DF2S(BEACON_PROTECTION);
320 	DF2S(EXTENDED_KEY_ID);
321 	}
322 	return "UNKNOWN";
323 #undef DF2S
324 }
325 
326 
driver_flag2_to_string(u64 flag2)327 const char * driver_flag2_to_string(u64 flag2)
328 {
329 #define DF2S(x) case WPA_DRIVER_FLAGS2_ ## x: return #x
330 	switch (flag2) {
331 	DF2S(CONTROL_PORT_RX);
332 	DF2S(CONTROL_PORT_TX_STATUS);
333 	}
334 	return "UNKNOWN";
335 #undef DF2S
336 }
337