xref: /freebsd/contrib/wpa/src/drivers/driver_common.c (revision 190cef3d52236565eb22e18b33e9e865ec634aa3)
1 /*
2  * Common driver-related functions
3  * Copyright (c) 2003-2011, 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 
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 
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(STKSTART);
39 	E2S(TDLS);
40 	E2S(FT_RESPONSE);
41 	E2S(IBSS_RSN_START);
42 	E2S(AUTH);
43 	E2S(DEAUTH);
44 	E2S(ASSOC_REJECT);
45 	E2S(AUTH_TIMED_OUT);
46 	E2S(ASSOC_TIMED_OUT);
47 	E2S(WPS_BUTTON_PUSHED);
48 	E2S(TX_STATUS);
49 	E2S(RX_FROM_UNKNOWN);
50 	E2S(RX_MGMT);
51 	E2S(REMAIN_ON_CHANNEL);
52 	E2S(CANCEL_REMAIN_ON_CHANNEL);
53 	E2S(RX_PROBE_REQ);
54 	E2S(NEW_STA);
55 	E2S(EAPOL_RX);
56 	E2S(SIGNAL_CHANGE);
57 	E2S(INTERFACE_ENABLED);
58 	E2S(INTERFACE_DISABLED);
59 	E2S(CHANNEL_LIST_CHANGED);
60 	E2S(INTERFACE_UNAVAILABLE);
61 	E2S(BEST_CHANNEL);
62 	E2S(UNPROT_DEAUTH);
63 	E2S(UNPROT_DISASSOC);
64 	E2S(STATION_LOW_ACK);
65 	E2S(IBSS_PEER_LOST);
66 	E2S(DRIVER_GTK_REKEY);
67 	E2S(SCHED_SCAN_STOPPED);
68 	E2S(DRIVER_CLIENT_POLL_OK);
69 	E2S(EAPOL_TX_STATUS);
70 	E2S(CH_SWITCH);
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 	}
85 
86 	return "UNKNOWN";
87 #undef E2S
88 }
89 
90 
91 const char * channel_width_to_string(enum chan_width width)
92 {
93 	switch (width) {
94 	case CHAN_WIDTH_20_NOHT:
95 		return "20 MHz (no HT)";
96 	case CHAN_WIDTH_20:
97 		return "20 MHz";
98 	case CHAN_WIDTH_40:
99 		return "40 MHz";
100 	case CHAN_WIDTH_80:
101 		return "80 MHz";
102 	case CHAN_WIDTH_80P80:
103 		return "80+80 MHz";
104 	case CHAN_WIDTH_160:
105 		return "160 MHz";
106 	default:
107 		return "unknown";
108 	}
109 }
110 
111 
112 int ht_supported(const struct hostapd_hw_modes *mode)
113 {
114 	if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
115 		/*
116 		 * The driver did not indicate whether it supports HT. Assume
117 		 * it does to avoid connection issues.
118 		 */
119 		return 1;
120 	}
121 
122 	/*
123 	 * IEEE Std 802.11n-2009 20.1.1:
124 	 * An HT non-AP STA shall support all EQM rates for one spatial stream.
125 	 */
126 	return mode->mcs_set[0] == 0xff;
127 }
128 
129 
130 int vht_supported(const struct hostapd_hw_modes *mode)
131 {
132 	if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
133 		/*
134 		 * The driver did not indicate whether it supports VHT. Assume
135 		 * it does to avoid connection issues.
136 		 */
137 		return 1;
138 	}
139 
140 	/*
141 	 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
142 	 * TODO: Verify if this complies with the standard
143 	 */
144 	return (mode->vht_mcs_set[0] & 0x3) != 3;
145 }
146 
147 
148 static int wpa_check_wowlan_trigger(const char *start, const char *trigger,
149 				    int capa_trigger, u8 *param_trigger)
150 {
151 	if (os_strcmp(start, trigger) != 0)
152 		return 0;
153 	if (!capa_trigger)
154 		return 0;
155 
156 	*param_trigger = 1;
157 	return 1;
158 }
159 
160 
161 struct wowlan_triggers *
162 wpa_get_wowlan_triggers(const char *wowlan_triggers,
163 			const struct wpa_driver_capa *capa)
164 {
165 	struct wowlan_triggers *triggers;
166 	char *start, *end, *buf;
167 	int last;
168 
169 	if (!wowlan_triggers)
170 		return NULL;
171 
172 	buf = os_strdup(wowlan_triggers);
173 	if (buf == NULL)
174 		return NULL;
175 
176 	triggers = os_zalloc(sizeof(*triggers));
177 	if (triggers == NULL)
178 		goto out;
179 
180 #define CHECK_TRIGGER(trigger) \
181 	wpa_check_wowlan_trigger(start, #trigger,			\
182 				  capa->wowlan_triggers.trigger,	\
183 				  &triggers->trigger)
184 
185 	start = buf;
186 	while (*start != '\0') {
187 		while (isblank((unsigned char) *start))
188 			start++;
189 		if (*start == '\0')
190 			break;
191 		end = start;
192 		while (!isblank((unsigned char) *end) && *end != '\0')
193 			end++;
194 		last = *end == '\0';
195 		*end = '\0';
196 
197 		if (!CHECK_TRIGGER(any) &&
198 		    !CHECK_TRIGGER(disconnect) &&
199 		    !CHECK_TRIGGER(magic_pkt) &&
200 		    !CHECK_TRIGGER(gtk_rekey_failure) &&
201 		    !CHECK_TRIGGER(eap_identity_req) &&
202 		    !CHECK_TRIGGER(four_way_handshake) &&
203 		    !CHECK_TRIGGER(rfkill_release)) {
204 			wpa_printf(MSG_DEBUG,
205 				   "Unknown/unsupported wowlan trigger '%s'",
206 				   start);
207 			os_free(triggers);
208 			triggers = NULL;
209 			goto out;
210 		}
211 
212 		if (last)
213 			break;
214 		start = end + 1;
215 	}
216 #undef CHECK_TRIGGER
217 
218 out:
219 	os_free(buf);
220 	return triggers;
221 }
222 
223 
224 const char * driver_flag_to_string(u64 flag)
225 {
226 #define DF2S(x) case WPA_DRIVER_FLAGS_ ## x: return #x
227 	switch (flag) {
228 	DF2S(DRIVER_IE);
229 	DF2S(SET_KEYS_AFTER_ASSOC);
230 	DF2S(DFS_OFFLOAD);
231 	DF2S(4WAY_HANDSHAKE);
232 	DF2S(WIRED);
233 	DF2S(SME);
234 	DF2S(AP);
235 	DF2S(SET_KEYS_AFTER_ASSOC_DONE);
236 	DF2S(HT_2040_COEX);
237 	DF2S(P2P_CONCURRENT);
238 	DF2S(P2P_DEDICATED_INTERFACE);
239 	DF2S(P2P_CAPABLE);
240 	DF2S(AP_TEARDOWN_SUPPORT);
241 	DF2S(P2P_MGMT_AND_NON_P2P);
242 	DF2S(SANE_ERROR_CODES);
243 	DF2S(OFFCHANNEL_TX);
244 	DF2S(EAPOL_TX_STATUS);
245 	DF2S(DEAUTH_TX_STATUS);
246 	DF2S(BSS_SELECTION);
247 	DF2S(TDLS_SUPPORT);
248 	DF2S(TDLS_EXTERNAL_SETUP);
249 	DF2S(PROBE_RESP_OFFLOAD);
250 	DF2S(AP_UAPSD);
251 	DF2S(INACTIVITY_TIMER);
252 	DF2S(AP_MLME);
253 	DF2S(SAE);
254 	DF2S(OBSS_SCAN);
255 	DF2S(IBSS);
256 	DF2S(RADAR);
257 	DF2S(DEDICATED_P2P_DEVICE);
258 	DF2S(QOS_MAPPING);
259 	DF2S(AP_CSA);
260 	DF2S(MESH);
261 	DF2S(ACS_OFFLOAD);
262 	DF2S(KEY_MGMT_OFFLOAD);
263 	DF2S(TDLS_CHANNEL_SWITCH);
264 	DF2S(HT_IBSS);
265 	DF2S(VHT_IBSS);
266 	DF2S(SUPPORT_HW_MODE_ANY);
267 	DF2S(OFFCHANNEL_SIMULTANEOUS);
268 	DF2S(FULL_AP_CLIENT_STATE);
269 	DF2S(P2P_LISTEN_OFFLOAD);
270 	}
271 	return "UNKNOWN";
272 #undef DF2S
273 }
274