1 /*
2  * hostapd - Driver operations
3  * Copyright (c) 2009-2010, 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/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/hw_features_common.h"
15 #include "wps/wps.h"
16 #include "p2p/p2p.h"
17 #include "hostapd.h"
18 #include "ieee802_11.h"
19 #include "sta_info.h"
20 #include "ap_config.h"
21 #include "p2p_hostapd.h"
22 #include "hs20.h"
23 #include "wpa_auth.h"
24 #include "ap_drv_ops.h"
25 #ifdef CONFIG_VENDOR_EXT
26 #include "vendor_ext.h"
27 #endif
28 
hostapd_sta_flags_to_drv(u32 flags)29 u32 hostapd_sta_flags_to_drv(u32 flags)
30 {
31 	int res = 0;
32 	if (flags & WLAN_STA_AUTHORIZED)
33 		res |= WPA_STA_AUTHORIZED;
34 	if (flags & WLAN_STA_WMM)
35 		res |= WPA_STA_WMM;
36 	if (flags & WLAN_STA_SHORT_PREAMBLE)
37 		res |= WPA_STA_SHORT_PREAMBLE;
38 	if (flags & WLAN_STA_MFP)
39 		res |= WPA_STA_MFP;
40 	if (flags & WLAN_STA_AUTH)
41 		res |= WPA_STA_AUTHENTICATED;
42 	if (flags & WLAN_STA_ASSOC)
43 		res |= WPA_STA_ASSOCIATED;
44 	return res;
45 }
46 
47 
add_buf(struct wpabuf **dst, const struct wpabuf *src)48 static int add_buf(struct wpabuf **dst, const struct wpabuf *src)
49 {
50 	if (!src)
51 		return 0;
52 	if (wpabuf_resize(dst, wpabuf_len(src)) != 0)
53 		return -1;
54 	wpabuf_put_buf(*dst, src);
55 	return 0;
56 }
57 
58 
add_buf_data(struct wpabuf **dst, const u8 *data, size_t len)59 int add_buf_data(struct wpabuf **dst, const u8 *data, size_t len)
60 {
61 	if (!data || !len)
62 		return 0;
63 	if (wpabuf_resize(dst, len) != 0)
64 		return -1;
65 	wpabuf_put_data(*dst, data, len);
66 	return 0;
67 }
68 
69 
hostapd_build_ap_extra_ies(struct hostapd_data *hapd, struct wpabuf **beacon_ret, struct wpabuf **proberesp_ret, struct wpabuf **assocresp_ret)70 int hostapd_build_ap_extra_ies(struct hostapd_data *hapd,
71 			       struct wpabuf **beacon_ret,
72 			       struct wpabuf **proberesp_ret,
73 			       struct wpabuf **assocresp_ret)
74 {
75 	struct wpabuf *beacon = NULL, *proberesp = NULL, *assocresp = NULL;
76 	u8 buf[200], *pos;
77 
78 	*beacon_ret = *proberesp_ret = *assocresp_ret = NULL;
79 
80 	pos = buf;
81 	pos = hostapd_eid_time_adv(hapd, pos);
82 	if (add_buf_data(&beacon, buf, pos - buf) < 0)
83 		goto fail;
84 	pos = hostapd_eid_time_zone(hapd, pos);
85 	if (add_buf_data(&proberesp, buf, pos - buf) < 0)
86 		goto fail;
87 
88 	pos = buf;
89 	pos = hostapd_eid_ext_capab(hapd, pos);
90 	if (add_buf_data(&assocresp, buf, pos - buf) < 0)
91 		goto fail;
92 	pos = hostapd_eid_interworking(hapd, pos);
93 	pos = hostapd_eid_adv_proto(hapd, pos);
94 	pos = hostapd_eid_roaming_consortium(hapd, pos);
95 	if (add_buf_data(&beacon, buf, pos - buf) < 0 ||
96 	    add_buf_data(&proberesp, buf, pos - buf) < 0)
97 		goto fail;
98 
99 #ifdef CONFIG_FST
100 	if (add_buf(&beacon, hapd->iface->fst_ies) < 0 ||
101 	    add_buf(&proberesp, hapd->iface->fst_ies) < 0 ||
102 	    add_buf(&assocresp, hapd->iface->fst_ies) < 0)
103 		goto fail;
104 #endif /* CONFIG_FST */
105 
106 #ifdef CONFIG_FILS
107 	pos = hostapd_eid_fils_indic(hapd, buf, 0);
108 	if (add_buf_data(&beacon, buf, pos - buf) < 0 ||
109 	    add_buf_data(&proberesp, buf, pos - buf) < 0)
110 		goto fail;
111 #endif /* CONFIG_FILS */
112 
113 	pos = hostapd_eid_rsnxe(hapd, buf, sizeof(buf));
114 	if (add_buf_data(&assocresp, buf, pos - buf) < 0)
115 		goto fail;
116 
117 	if (add_buf(&beacon, hapd->wps_beacon_ie) < 0 ||
118 	    add_buf(&proberesp, hapd->wps_probe_resp_ie) < 0)
119 		goto fail;
120 
121 #ifdef CONFIG_P2P
122 	if (add_buf(&beacon, hapd->p2p_beacon_ie) < 0 ||
123 	    add_buf(&proberesp, hapd->p2p_probe_resp_ie) < 0)
124 		goto fail;
125 #endif /* CONFIG_P2P */
126 
127 #ifdef CONFIG_P2P_MANAGER
128 	if (hapd->conf->p2p & P2P_MANAGE) {
129 		if (wpabuf_resize(&beacon, 100) == 0) {
130 			u8 *start, *p;
131 			start = wpabuf_put(beacon, 0);
132 			p = hostapd_eid_p2p_manage(hapd, start);
133 			wpabuf_put(beacon, p - start);
134 		}
135 
136 		if (wpabuf_resize(&proberesp, 100) == 0) {
137 			u8 *start, *p;
138 			start = wpabuf_put(proberesp, 0);
139 			p = hostapd_eid_p2p_manage(hapd, start);
140 			wpabuf_put(proberesp, p - start);
141 		}
142 	}
143 #endif /* CONFIG_P2P_MANAGER */
144 
145 #ifdef CONFIG_WPS
146 	if (hapd->conf->wps_state) {
147 		struct wpabuf *a = wps_build_assoc_resp_ie();
148 		add_buf(&assocresp, a);
149 		wpabuf_free(a);
150 	}
151 #endif /* CONFIG_WPS */
152 
153 #ifdef CONFIG_P2P_MANAGER
154 	if (hapd->conf->p2p & P2P_MANAGE) {
155 		if (wpabuf_resize(&assocresp, 100) == 0) {
156 			u8 *start, *p;
157 			start = wpabuf_put(assocresp, 0);
158 			p = hostapd_eid_p2p_manage(hapd, start);
159 			wpabuf_put(assocresp, p - start);
160 		}
161 	}
162 #endif /* CONFIG_P2P_MANAGER */
163 
164 #ifdef CONFIG_WIFI_DISPLAY
165 	if (hapd->p2p_group) {
166 		struct wpabuf *a;
167 		a = p2p_group_assoc_resp_ie(hapd->p2p_group, P2P_SC_SUCCESS);
168 		add_buf(&assocresp, a);
169 		wpabuf_free(a);
170 	}
171 #endif /* CONFIG_WIFI_DISPLAY */
172 
173 #ifdef CONFIG_HS20
174 	pos = hostapd_eid_hs20_indication(hapd, buf);
175 	if (add_buf_data(&beacon, buf, pos - buf) < 0 ||
176 	    add_buf_data(&proberesp, buf, pos - buf) < 0)
177 		goto fail;
178 
179 	pos = hostapd_eid_osen(hapd, buf);
180 	if (add_buf_data(&beacon, buf, pos - buf) < 0 ||
181 	    add_buf_data(&proberesp, buf, pos - buf) < 0)
182 		goto fail;
183 #endif /* CONFIG_HS20 */
184 
185 #ifdef CONFIG_MBO
186 	if (hapd->conf->mbo_enabled ||
187 	    OCE_STA_CFON_ENABLED(hapd) || OCE_AP_ENABLED(hapd)) {
188 		pos = hostapd_eid_mbo(hapd, buf, sizeof(buf));
189 		if (add_buf_data(&beacon, buf, pos - buf) < 0 ||
190 		    add_buf_data(&proberesp, buf, pos - buf) < 0 ||
191 		    add_buf_data(&assocresp, buf, pos - buf) < 0)
192 			goto fail;
193 	}
194 #endif /* CONFIG_MBO */
195 
196 #ifdef CONFIG_OWE
197 	pos = hostapd_eid_owe_trans(hapd, buf, sizeof(buf));
198 	if (add_buf_data(&beacon, buf, pos - buf) < 0 ||
199 	    add_buf_data(&proberesp, buf, pos - buf) < 0)
200 		goto fail;
201 #endif /* CONFIG_OWE */
202 
203 	add_buf(&beacon, hapd->conf->vendor_elements);
204 	add_buf(&proberesp, hapd->conf->vendor_elements);
205 	add_buf(&assocresp, hapd->conf->assocresp_elements);
206 	*beacon_ret = beacon;
207 	*proberesp_ret = proberesp;
208 	*assocresp_ret = assocresp;
209 
210 	return 0;
211 
212 fail:
213 	wpabuf_free(beacon);
214 	wpabuf_free(proberesp);
215 	wpabuf_free(assocresp);
216 	return -1;
217 }
218 
219 
hostapd_free_ap_extra_ies(struct hostapd_data *hapd, struct wpabuf *beacon, struct wpabuf *proberesp, struct wpabuf *assocresp)220 void hostapd_free_ap_extra_ies(struct hostapd_data *hapd,
221 			       struct wpabuf *beacon,
222 			       struct wpabuf *proberesp,
223 			       struct wpabuf *assocresp)
224 {
225 	wpabuf_free(beacon);
226 	wpabuf_free(proberesp);
227 	wpabuf_free(assocresp);
228 }
229 
230 
hostapd_reset_ap_wps_ie(struct hostapd_data *hapd)231 int hostapd_reset_ap_wps_ie(struct hostapd_data *hapd)
232 {
233 	if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
234 		return 0;
235 
236 	return hapd->driver->set_ap_wps_ie(hapd->drv_priv, NULL, NULL, NULL);
237 }
238 
239 
hostapd_set_ap_wps_ie(struct hostapd_data *hapd)240 int hostapd_set_ap_wps_ie(struct hostapd_data *hapd)
241 {
242 	struct wpabuf *beacon, *proberesp, *assocresp;
243 	int ret;
244 
245 	if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
246 		return 0;
247 
248 	if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
249 	    0)
250 		return -1;
251 
252 	ret = hapd->driver->set_ap_wps_ie(hapd->drv_priv, beacon, proberesp,
253 					  assocresp);
254 
255 	hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
256 
257 	return ret;
258 }
259 
260 
hostapd_set_authorized(struct hostapd_data *hapd, struct sta_info *sta, int authorized)261 int hostapd_set_authorized(struct hostapd_data *hapd,
262 			   struct sta_info *sta, int authorized)
263 {
264 	if (authorized) {
265 		return hostapd_sta_set_flags(hapd, sta->addr,
266 					     hostapd_sta_flags_to_drv(
267 						     sta->flags),
268 					     WPA_STA_AUTHORIZED, ~0);
269 	}
270 
271 	return hostapd_sta_set_flags(hapd, sta->addr,
272 				     hostapd_sta_flags_to_drv(sta->flags),
273 				     0, ~WPA_STA_AUTHORIZED);
274 }
275 
276 
hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)277 int hostapd_set_sta_flags(struct hostapd_data *hapd, struct sta_info *sta)
278 {
279 	int set_flags, total_flags, flags_and, flags_or;
280 	total_flags = hostapd_sta_flags_to_drv(sta->flags);
281 	set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
282 	if (((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
283 	     sta->auth_alg == WLAN_AUTH_FT) &&
284 	    sta->flags & WLAN_STA_AUTHORIZED)
285 		set_flags |= WPA_STA_AUTHORIZED;
286 	flags_or = total_flags & set_flags;
287 	flags_and = total_flags | ~set_flags;
288 	return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
289 				     flags_or, flags_and);
290 }
291 
292 
hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname, int enabled)293 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
294 			      int enabled)
295 {
296 	struct wpa_bss_params params;
297 	os_memset(&params, 0, sizeof(params));
298 	params.ifname = ifname;
299 	params.enabled = enabled;
300 	if (enabled) {
301 		params.wpa = hapd->conf->wpa;
302 		params.ieee802_1x = hapd->conf->ieee802_1x;
303 		params.wpa_group = hapd->conf->wpa_group;
304 		if ((hapd->conf->wpa & (WPA_PROTO_WPA | WPA_PROTO_RSN)) ==
305 		    (WPA_PROTO_WPA | WPA_PROTO_RSN))
306 			params.wpa_pairwise = hapd->conf->wpa_pairwise |
307 				hapd->conf->rsn_pairwise;
308 		else if (hapd->conf->wpa & WPA_PROTO_RSN)
309 			params.wpa_pairwise = hapd->conf->rsn_pairwise;
310 		else if (hapd->conf->wpa & WPA_PROTO_WPA)
311 			params.wpa_pairwise = hapd->conf->wpa_pairwise;
312 		params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
313 		params.rsn_preauth = hapd->conf->rsn_preauth;
314 		params.ieee80211w = hapd->conf->ieee80211w;
315 	}
316 	return hostapd_set_ieee8021x(hapd, &params);
317 }
318 
319 
hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)320 int hostapd_vlan_if_add(struct hostapd_data *hapd, const char *ifname)
321 {
322 	char force_ifname[IFNAMSIZ];
323 	u8 if_addr[ETH_ALEN];
324 	return hostapd_if_add(hapd, WPA_IF_AP_VLAN, ifname, hapd->own_addr,
325 			      NULL, NULL, force_ifname, if_addr, NULL, 0);
326 }
327 
328 
hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)329 int hostapd_vlan_if_remove(struct hostapd_data *hapd, const char *ifname)
330 {
331 	return hostapd_if_remove(hapd, WPA_IF_AP_VLAN, ifname);
332 }
333 
334 
hostapd_set_wds_sta(struct hostapd_data *hapd, char *ifname_wds, const u8 *addr, int aid, int val)335 int hostapd_set_wds_sta(struct hostapd_data *hapd, char *ifname_wds,
336 			const u8 *addr, int aid, int val)
337 {
338 	const char *bridge = NULL;
339 
340 	if (hapd->driver == NULL || hapd->driver->set_wds_sta == NULL)
341 		return -1;
342 	if (hapd->conf->wds_bridge[0])
343 		bridge = hapd->conf->wds_bridge;
344 	else if (hapd->conf->bridge[0])
345 		bridge = hapd->conf->bridge;
346 	return hapd->driver->set_wds_sta(hapd->drv_priv, addr, aid, val,
347 					 bridge, ifname_wds);
348 }
349 
350 
hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr, u16 auth_alg)351 int hostapd_add_sta_node(struct hostapd_data *hapd, const u8 *addr,
352 			 u16 auth_alg)
353 {
354 	if (hapd->driver == NULL || hapd->driver->add_sta_node == NULL)
355 		return -EOPNOTSUPP;
356 	return hapd->driver->add_sta_node(hapd->drv_priv, addr, auth_alg);
357 }
358 
359 
hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr, u16 seq, u16 status, const u8 *ie, size_t len)360 int hostapd_sta_auth(struct hostapd_data *hapd, const u8 *addr,
361 		     u16 seq, u16 status, const u8 *ie, size_t len)
362 {
363 	struct wpa_driver_sta_auth_params params;
364 #ifdef CONFIG_FILS
365 	struct sta_info *sta;
366 #endif /* CONFIG_FILS */
367 
368 	if (hapd->driver == NULL || hapd->driver->sta_auth == NULL)
369 		return 0;
370 
371 	os_memset(&params, 0, sizeof(params));
372 
373 #ifdef CONFIG_FILS
374 	sta = ap_get_sta(hapd, addr);
375 	if (!sta) {
376 		wpa_printf(MSG_DEBUG, "Station " MACSTR_SEC
377 			   " not found for sta_auth processing",
378 			   MAC2STR_SEC(addr));
379 		return 0;
380 	}
381 
382 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
383 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
384 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
385 		params.fils_auth = 1;
386 		wpa_auth_get_fils_aead_params(sta->wpa_sm, params.fils_anonce,
387 					      params.fils_snonce,
388 					      params.fils_kek,
389 					      &params.fils_kek_len);
390 	}
391 #endif /* CONFIG_FILS */
392 
393 	params.own_addr = hapd->own_addr;
394 	params.addr = addr;
395 	params.seq = seq;
396 	params.status = status;
397 	params.ie = ie;
398 	params.len = len;
399 
400 	return hapd->driver->sta_auth(hapd->drv_priv, &params);
401 }
402 
403 
hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr, int reassoc, u16 status, const u8 *ie, size_t len)404 int hostapd_sta_assoc(struct hostapd_data *hapd, const u8 *addr,
405 		      int reassoc, u16 status, const u8 *ie, size_t len)
406 {
407 	if (hapd->driver == NULL || hapd->driver->sta_assoc == NULL)
408 		return 0;
409 	return hapd->driver->sta_assoc(hapd->drv_priv, hapd->own_addr, addr,
410 				       reassoc, status, ie, len);
411 }
412 
413 
hostapd_sta_add(struct hostapd_data *hapd, const u8 *addr, u16 aid, u16 capability, const u8 *supp_rates, size_t supp_rates_len, u16 listen_interval, const struct ieee80211_ht_capabilities *ht_capab, const struct ieee80211_vht_capabilities *vht_capab, const struct ieee80211_he_capabilities *he_capab, size_t he_capab_len, const struct ieee80211_he_6ghz_band_cap *he_6ghz_capab, u32 flags, u8 qosinfo, u8 vht_opmode, int supp_p2p_ps, int set)414 int hostapd_sta_add(struct hostapd_data *hapd,
415 		    const u8 *addr, u16 aid, u16 capability,
416 		    const u8 *supp_rates, size_t supp_rates_len,
417 		    u16 listen_interval,
418 		    const struct ieee80211_ht_capabilities *ht_capab,
419 		    const struct ieee80211_vht_capabilities *vht_capab,
420 		    const struct ieee80211_he_capabilities *he_capab,
421 		    size_t he_capab_len,
422 		    const struct ieee80211_he_6ghz_band_cap *he_6ghz_capab,
423 		    u32 flags, u8 qosinfo, u8 vht_opmode, int supp_p2p_ps,
424 		    int set)
425 {
426 	struct hostapd_sta_add_params params;
427 
428 	if (hapd->driver == NULL)
429 		return 0;
430 	if (hapd->driver->sta_add == NULL)
431 		return 0;
432 
433 	os_memset(&params, 0, sizeof(params));
434 	params.addr = addr;
435 	params.aid = aid;
436 	params.capability = capability;
437 	params.supp_rates = supp_rates;
438 	params.supp_rates_len = supp_rates_len;
439 	params.listen_interval = listen_interval;
440 	params.ht_capabilities = ht_capab;
441 	params.vht_capabilities = vht_capab;
442 	params.he_capab = he_capab;
443 	params.he_capab_len = he_capab_len;
444 	params.he_6ghz_capab = he_6ghz_capab;
445 	params.vht_opmode_enabled = !!(flags & WLAN_STA_VHT_OPMODE_ENABLED);
446 	params.vht_opmode = vht_opmode;
447 	params.flags = hostapd_sta_flags_to_drv(flags);
448 	params.qosinfo = qosinfo;
449 	params.support_p2p_ps = supp_p2p_ps;
450 	params.set = set;
451 	return hapd->driver->sta_add(hapd->drv_priv, &params);
452 }
453 
454 
hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr, u8 *tspec_ie, size_t tspec_ielen)455 int hostapd_add_tspec(struct hostapd_data *hapd, const u8 *addr,
456 		      u8 *tspec_ie, size_t tspec_ielen)
457 {
458 	if (hapd->driver == NULL || hapd->driver->add_tspec == NULL)
459 		return 0;
460 	return hapd->driver->add_tspec(hapd->drv_priv, addr, tspec_ie,
461 				       tspec_ielen);
462 }
463 
464 
hostapd_set_privacy(struct hostapd_data *hapd, int enabled)465 int hostapd_set_privacy(struct hostapd_data *hapd, int enabled)
466 {
467 	if (hapd->driver == NULL || hapd->driver->set_privacy == NULL)
468 		return 0;
469 	return hapd->driver->set_privacy(hapd->drv_priv, enabled);
470 }
471 
472 
hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem, size_t elem_len)473 int hostapd_set_generic_elem(struct hostapd_data *hapd, const u8 *elem,
474 			     size_t elem_len)
475 {
476 	if (hapd->driver == NULL || hapd->driver->set_generic_elem == NULL)
477 		return 0;
478 	return hapd->driver->set_generic_elem(hapd->drv_priv, elem, elem_len);
479 }
480 
481 
hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)482 int hostapd_get_ssid(struct hostapd_data *hapd, u8 *buf, size_t len)
483 {
484 	if (hapd->driver == NULL || hapd->driver->hapd_get_ssid == NULL)
485 		return 0;
486 	return hapd->driver->hapd_get_ssid(hapd->drv_priv, buf, len);
487 }
488 
489 
hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)490 int hostapd_set_ssid(struct hostapd_data *hapd, const u8 *buf, size_t len)
491 {
492 	if (hapd->driver == NULL || hapd->driver->hapd_set_ssid == NULL)
493 		return 0;
494 	return hapd->driver->hapd_set_ssid(hapd->drv_priv, buf, len);
495 }
496 
497 
hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type, const char *ifname, const u8 *addr, void *bss_ctx, void **drv_priv, char *force_ifname, u8 *if_addr, const char *bridge, int use_existing)498 int hostapd_if_add(struct hostapd_data *hapd, enum wpa_driver_if_type type,
499 		   const char *ifname, const u8 *addr, void *bss_ctx,
500 		   void **drv_priv, char *force_ifname, u8 *if_addr,
501 		   const char *bridge, int use_existing)
502 {
503 	if (hapd->driver == NULL || hapd->driver->if_add == NULL)
504 		return -1;
505 	return hapd->driver->if_add(hapd->drv_priv, type, ifname, addr,
506 				    bss_ctx, drv_priv, force_ifname, if_addr,
507 				    bridge, use_existing, 1);
508 }
509 
510 
hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type, const char *ifname)511 int hostapd_if_remove(struct hostapd_data *hapd, enum wpa_driver_if_type type,
512 		      const char *ifname)
513 {
514 	if (hapd->driver == NULL || hapd->drv_priv == NULL ||
515 	    hapd->driver->if_remove == NULL)
516 		return -1;
517 	return hapd->driver->if_remove(hapd->drv_priv, type, ifname);
518 }
519 
520 
hostapd_set_ieee8021x(struct hostapd_data *hapd, struct wpa_bss_params *params)521 int hostapd_set_ieee8021x(struct hostapd_data *hapd,
522 			  struct wpa_bss_params *params)
523 {
524 	if (hapd->driver == NULL || hapd->driver->set_ieee8021x == NULL)
525 		return 0;
526 	return hapd->driver->set_ieee8021x(hapd->drv_priv, params);
527 }
528 
529 
hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd, const u8 *addr, int idx, u8 *seq)530 int hostapd_get_seqnum(const char *ifname, struct hostapd_data *hapd,
531 		       const u8 *addr, int idx, u8 *seq)
532 {
533 	if (hapd->driver == NULL || hapd->driver->get_seqnum == NULL)
534 		return 0;
535 	return hapd->driver->get_seqnum(ifname, hapd->drv_priv, addr, idx,
536 					seq);
537 }
538 
539 
hostapd_flush(struct hostapd_data *hapd)540 int hostapd_flush(struct hostapd_data *hapd)
541 {
542 	if (hapd->driver == NULL || hapd->driver->flush == NULL)
543 		return 0;
544 	return hapd->driver->flush(hapd->drv_priv);
545 }
546 
547 
hostapd_set_freq(struct hostapd_data *hapd, enum hostapd_hw_mode mode, int freq, int channel, int edmg, u8 edmg_channel, int ht_enabled, int vht_enabled, int he_enabled, int sec_channel_offset, int oper_chwidth, int center_segment0, int center_segment1)548 int hostapd_set_freq(struct hostapd_data *hapd, enum hostapd_hw_mode mode,
549 		     int freq, int channel, int edmg, u8 edmg_channel,
550 		     int ht_enabled, int vht_enabled,
551 		     int he_enabled,
552 		     int sec_channel_offset, int oper_chwidth,
553 		     int center_segment0, int center_segment1)
554 {
555 	struct hostapd_freq_params data;
556 	struct hostapd_hw_modes *cmode = hapd->iface->current_mode;
557 
558 	if (hostapd_set_freq_params(&data, mode, freq, channel, edmg,
559 				    edmg_channel, ht_enabled,
560 				    vht_enabled, he_enabled, sec_channel_offset,
561 				    oper_chwidth,
562 				    center_segment0, center_segment1,
563 				    cmode ? cmode->vht_capab : 0,
564 				    cmode ?
565 				    &cmode->he_capab[IEEE80211_MODE_AP] : NULL))
566 		return -1;
567 
568 	if (hapd->driver == NULL)
569 		return 0;
570 	if (hapd->driver->set_freq == NULL)
571 		return 0;
572 	return hapd->driver->set_freq(hapd->drv_priv, &data);
573 }
574 
hostapd_set_rts(struct hostapd_data *hapd, int rts)575 int hostapd_set_rts(struct hostapd_data *hapd, int rts)
576 {
577 	if (hapd->driver == NULL || hapd->driver->set_rts == NULL)
578 		return 0;
579 	return hapd->driver->set_rts(hapd->drv_priv, rts);
580 }
581 
582 
hostapd_set_frag(struct hostapd_data *hapd, int frag)583 int hostapd_set_frag(struct hostapd_data *hapd, int frag)
584 {
585 	if (hapd->driver == NULL || hapd->driver->set_frag == NULL)
586 		return 0;
587 	return hapd->driver->set_frag(hapd->drv_priv, frag);
588 }
589 
590 
hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr, int total_flags, int flags_or, int flags_and)591 int hostapd_sta_set_flags(struct hostapd_data *hapd, u8 *addr,
592 			  int total_flags, int flags_or, int flags_and)
593 {
594 	if (!hapd->driver || !hapd->drv_priv || !hapd->driver->sta_set_flags)
595 		return 0;
596 	return hapd->driver->sta_set_flags(hapd->drv_priv, addr, total_flags,
597 					   flags_or, flags_and);
598 }
599 
600 
hostapd_sta_set_airtime_weight(struct hostapd_data *hapd, const u8 *addr, unsigned int weight)601 int hostapd_sta_set_airtime_weight(struct hostapd_data *hapd, const u8 *addr,
602 				   unsigned int weight)
603 {
604 	if (!hapd->driver || !hapd->driver->sta_set_airtime_weight)
605 		return 0;
606 	return hapd->driver->sta_set_airtime_weight(hapd->drv_priv, addr,
607 						    weight);
608 }
609 
610 
hostapd_set_country(struct hostapd_data *hapd, const char *country)611 int hostapd_set_country(struct hostapd_data *hapd, const char *country)
612 {
613 	if (hapd->driver == NULL ||
614 	    hapd->driver->set_country == NULL)
615 		return 0;
616 	return hapd->driver->set_country(hapd->drv_priv, country);
617 }
618 
619 
hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs, int cw_min, int cw_max, int burst_time)620 int hostapd_set_tx_queue_params(struct hostapd_data *hapd, int queue, int aifs,
621 				int cw_min, int cw_max, int burst_time)
622 {
623 	if (hapd->driver == NULL || hapd->driver->set_tx_queue_params == NULL)
624 		return 0;
625 	return hapd->driver->set_tx_queue_params(hapd->drv_priv, queue, aifs,
626 						 cw_min, cw_max, burst_time);
627 }
628 
629 
630 struct hostapd_hw_modes *
hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes, u16 *flags, u8 *dfs_domain)631 hostapd_get_hw_feature_data(struct hostapd_data *hapd, u16 *num_modes,
632 			    u16 *flags, u8 *dfs_domain)
633 {
634 	if (hapd->driver == NULL ||
635 	    hapd->driver->get_hw_feature_data == NULL)
636 		return NULL;
637 	return hapd->driver->get_hw_feature_data(hapd->drv_priv, num_modes,
638 						 flags, dfs_domain);
639 }
640 
641 
hostapd_driver_commit(struct hostapd_data *hapd)642 int hostapd_driver_commit(struct hostapd_data *hapd)
643 {
644 	if (hapd->driver == NULL || hapd->driver->commit == NULL)
645 		return 0;
646 	return hapd->driver->commit(hapd->drv_priv);
647 }
648 
649 
hostapd_drv_none(struct hostapd_data *hapd)650 int hostapd_drv_none(struct hostapd_data *hapd)
651 {
652 	return hapd->driver && os_strcmp(hapd->driver->name, "none") == 0;
653 }
654 
655 
hostapd_drv_nl80211(struct hostapd_data *hapd)656 bool hostapd_drv_nl80211(struct hostapd_data *hapd)
657 {
658 	return hapd->driver && os_strcmp(hapd->driver->name, "nl80211") == 0;
659 }
660 
661 
hostapd_driver_scan(struct hostapd_data *hapd, struct wpa_driver_scan_params *params)662 int hostapd_driver_scan(struct hostapd_data *hapd,
663 			struct wpa_driver_scan_params *params)
664 {
665 	if (hapd->driver && hapd->driver->scan2)
666 		return hapd->driver->scan2(hapd->drv_priv, params);
667 	return -1;
668 }
669 
670 
hostapd_driver_get_scan_results( struct hostapd_data *hapd)671 struct wpa_scan_results * hostapd_driver_get_scan_results(
672 	struct hostapd_data *hapd)
673 {
674 	if (hapd->driver && hapd->driver->get_scan_results2)
675 		return hapd->driver->get_scan_results2(hapd->drv_priv);
676 	return NULL;
677 }
678 
679 
hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start, int duration)680 int hostapd_driver_set_noa(struct hostapd_data *hapd, u8 count, int start,
681 			   int duration)
682 {
683 	if (hapd->driver && hapd->driver->set_noa)
684 		return hapd->driver->set_noa(hapd->drv_priv, count, start,
685 					     duration);
686 	return -1;
687 }
688 
689 
hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd, enum wpa_alg alg, const u8 *addr, int key_idx, int vlan_id, int set_tx, const u8 *seq, size_t seq_len, const u8 *key, size_t key_len, enum key_flag key_flag)690 int hostapd_drv_set_key(const char *ifname, struct hostapd_data *hapd,
691 			enum wpa_alg alg, const u8 *addr,
692 			int key_idx, int vlan_id, int set_tx,
693 			const u8 *seq, size_t seq_len,
694 			const u8 *key, size_t key_len, enum key_flag key_flag)
695 {
696 	struct wpa_driver_set_key_params params;
697 
698 	if (hapd->driver == NULL || hapd->driver->set_key == NULL)
699 		return 0;
700 
701 	os_memset(&params, 0, sizeof(params));
702 	params.ifname = ifname;
703 	params.alg = alg;
704 	params.addr = addr;
705 	params.key_idx = key_idx;
706 	params.set_tx = set_tx;
707 	params.seq = seq;
708 	params.seq_len = seq_len;
709 	params.key = key;
710 	params.key_len = key_len;
711 	params.vlan_id = vlan_id;
712 	params.key_flag = key_flag;
713 #ifdef CONFIG_MLD_PATCH
714 	params.link_id = -1;
715 #endif
716 	return hapd->driver->set_key(hapd->drv_priv, &params);
717 }
718 
719 
hostapd_drv_send_mlme(struct hostapd_data *hapd, const void *msg, size_t len, int noack, const u16 *csa_offs, size_t csa_offs_len, int no_encrypt)720 int hostapd_drv_send_mlme(struct hostapd_data *hapd,
721 			  const void *msg, size_t len, int noack,
722 			  const u16 *csa_offs, size_t csa_offs_len,
723 			  int no_encrypt)
724 {
725 	if (!hapd->driver || !hapd->driver->send_mlme || !hapd->drv_priv)
726 		return 0;
727 	return hapd->driver->send_mlme(hapd->drv_priv, msg, len, noack, 0,
728 				       csa_offs, csa_offs_len, no_encrypt, 0);
729 }
730 
731 
hostapd_drv_sta_deauth(struct hostapd_data *hapd, const u8 *addr, int reason)732 int hostapd_drv_sta_deauth(struct hostapd_data *hapd,
733 			   const u8 *addr, int reason)
734 {
735 	if (!hapd->driver || !hapd->driver->sta_deauth || !hapd->drv_priv)
736 		return 0;
737 	return hapd->driver->sta_deauth(hapd->drv_priv, hapd->own_addr, addr,
738 					reason);
739 }
740 
741 
hostapd_drv_sta_disassoc(struct hostapd_data *hapd, const u8 *addr, int reason)742 int hostapd_drv_sta_disassoc(struct hostapd_data *hapd,
743 			     const u8 *addr, int reason)
744 {
745 	if (!hapd->driver || !hapd->driver->sta_disassoc || !hapd->drv_priv)
746 		return 0;
747 	return hapd->driver->sta_disassoc(hapd->drv_priv, hapd->own_addr, addr,
748 					  reason);
749 }
750 
751 
hostapd_drv_wnm_oper(struct hostapd_data *hapd, enum wnm_oper oper, const u8 *peer, u8 *buf, u16 *buf_len)752 int hostapd_drv_wnm_oper(struct hostapd_data *hapd, enum wnm_oper oper,
753 			 const u8 *peer, u8 *buf, u16 *buf_len)
754 {
755 	if (hapd->driver == NULL || hapd->driver->wnm_oper == NULL)
756 		return -1;
757 	return hapd->driver->wnm_oper(hapd->drv_priv, oper, peer, buf,
758 				      buf_len);
759 }
760 
761 
hostapd_drv_send_action(struct hostapd_data *hapd, unsigned int freq, unsigned int wait, const u8 *dst, const u8 *data, size_t len)762 int hostapd_drv_send_action(struct hostapd_data *hapd, unsigned int freq,
763 			    unsigned int wait, const u8 *dst, const u8 *data,
764 			    size_t len)
765 {
766 	const u8 *bssid;
767 	const u8 wildcard_bssid[ETH_ALEN] = {
768 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
769 	};
770 
771 	if (!hapd->driver || !hapd->driver->send_action || !hapd->drv_priv)
772 		return 0;
773 	bssid = hapd->own_addr;
774 	if (!is_multicast_ether_addr(dst) &&
775 	    len > 0 && data[0] == WLAN_ACTION_PUBLIC) {
776 		struct sta_info *sta;
777 
778 		/*
779 		 * Public Action frames to a STA that is not a member of the BSS
780 		 * shall use wildcard BSSID value.
781 		 */
782 		sta = ap_get_sta(hapd, dst);
783 		if (!sta || !(sta->flags & WLAN_STA_ASSOC))
784 			bssid = wildcard_bssid;
785 	} else if (is_broadcast_ether_addr(dst) &&
786 		   len > 0 && data[0] == WLAN_ACTION_PUBLIC) {
787 		/*
788 		 * The only current use case of Public Action frames with
789 		 * broadcast destination address is DPP PKEX. That case is
790 		 * directing all devices and not just the STAs within the BSS,
791 		 * so have to use the wildcard BSSID value.
792 		 */
793 		bssid = wildcard_bssid;
794 	}
795 	return hapd->driver->send_action(hapd->drv_priv, freq, wait, dst,
796 					 hapd->own_addr, bssid, data, len, 0);
797 }
798 
799 
hostapd_drv_send_action_addr3_ap(struct hostapd_data *hapd, unsigned int freq, unsigned int wait, const u8 *dst, const u8 *data, size_t len)800 int hostapd_drv_send_action_addr3_ap(struct hostapd_data *hapd,
801 				     unsigned int freq,
802 				     unsigned int wait, const u8 *dst,
803 				     const u8 *data, size_t len)
804 {
805 	if (hapd->driver == NULL || hapd->driver->send_action == NULL)
806 		return 0;
807 	return hapd->driver->send_action(hapd->drv_priv, freq, wait, dst,
808 					 hapd->own_addr, hapd->own_addr, data,
809 					 len, 0);
810 }
811 
812 
hostapd_start_dfs_cac(struct hostapd_iface *iface, enum hostapd_hw_mode mode, int freq, int channel, int ht_enabled, int vht_enabled, int he_enabled, int sec_channel_offset, int oper_chwidth, int center_segment0, int center_segment1)813 int hostapd_start_dfs_cac(struct hostapd_iface *iface,
814 			  enum hostapd_hw_mode mode, int freq,
815 			  int channel, int ht_enabled, int vht_enabled,
816 			  int he_enabled,
817 			  int sec_channel_offset, int oper_chwidth,
818 			  int center_segment0, int center_segment1)
819 {
820 	struct hostapd_data *hapd = iface->bss[0];
821 	struct hostapd_freq_params data;
822 	int res;
823 	struct hostapd_hw_modes *cmode = iface->current_mode;
824 
825 	if (!hapd->driver || !hapd->driver->start_dfs_cac || !cmode)
826 		return 0;
827 
828 	if (!iface->conf->ieee80211h) {
829 		wpa_printf(MSG_ERROR, "Can't start DFS CAC, DFS functionality "
830 			   "is not enabled");
831 		return -1;
832 	}
833 
834 	if (hostapd_set_freq_params(&data, mode, freq, channel, 0, 0,
835 				    ht_enabled,
836 				    vht_enabled, he_enabled, sec_channel_offset,
837 				    oper_chwidth, center_segment0,
838 				    center_segment1,
839 				    cmode->vht_capab,
840 				    &cmode->he_capab[IEEE80211_MODE_AP])) {
841 		wpa_printf(MSG_ERROR, "Can't set freq params");
842 		return -1;
843 	}
844 
845 	res = hapd->driver->start_dfs_cac(hapd->drv_priv, &data);
846 	if (!res) {
847 		iface->cac_started = 1;
848 		os_get_reltime(&iface->dfs_cac_start);
849 	}
850 
851 	return res;
852 }
853 
854 
hostapd_drv_set_qos_map(struct hostapd_data *hapd, const u8 *qos_map_set, u8 qos_map_set_len)855 int hostapd_drv_set_qos_map(struct hostapd_data *hapd,
856 			    const u8 *qos_map_set, u8 qos_map_set_len)
857 {
858 	if (!hapd->driver || !hapd->driver->set_qos_map || !hapd->drv_priv)
859 		return 0;
860 	return hapd->driver->set_qos_map(hapd->drv_priv, qos_map_set,
861 					 qos_map_set_len);
862 }
863 
864 
hostapd_get_hw_mode_any_channels(struct hostapd_data *hapd, struct hostapd_hw_modes *mode, int acs_ch_list_all, int **freq_list)865 static void hostapd_get_hw_mode_any_channels(struct hostapd_data *hapd,
866 					     struct hostapd_hw_modes *mode,
867 					     int acs_ch_list_all,
868 					     int **freq_list)
869 {
870 	int i;
871 
872 	for (i = 0; i < mode->num_channels; i++) {
873 		struct hostapd_channel_data *chan = &mode->channels[i];
874 
875 		if (!acs_ch_list_all &&
876 		    (hapd->iface->conf->acs_freq_list.num &&
877 		     !freq_range_list_includes(
878 			     &hapd->iface->conf->acs_freq_list,
879 			     chan->freq)))
880 			continue;
881 		if (!acs_ch_list_all &&
882 		    (!hapd->iface->conf->acs_freq_list_present &&
883 		     hapd->iface->conf->acs_ch_list.num &&
884 		     !freq_range_list_includes(
885 			     &hapd->iface->conf->acs_ch_list,
886 			     chan->chan)))
887 			continue;
888 		if (is_6ghz_freq(chan->freq) &&
889 		    hapd->iface->conf->acs_exclude_6ghz_non_psc &&
890 		    !is_6ghz_psc_frequency(chan->freq))
891 			continue;
892 		if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
893 		    !(hapd->iface->conf->acs_exclude_dfs &&
894 		      (chan->flag & HOSTAPD_CHAN_RADAR)) &&
895 		    !(chan->max_tx_power < hapd->iface->conf->min_tx_power))
896 			int_array_add_unique(freq_list, chan->freq);
897 	}
898 }
899 
900 
hostapd_get_ext_capa(struct hostapd_iface *iface)901 void hostapd_get_ext_capa(struct hostapd_iface *iface)
902 {
903 	struct hostapd_data *hapd = iface->bss[0];
904 
905 	if (!hapd->driver || !hapd->driver->get_ext_capab)
906 		return;
907 
908 	hapd->driver->get_ext_capab(hapd->drv_priv, WPA_IF_AP_BSS,
909 				    &iface->extended_capa,
910 				    &iface->extended_capa_mask,
911 				    &iface->extended_capa_len);
912 }
913 
914 
hostapd_drv_do_acs(struct hostapd_data *hapd)915 int hostapd_drv_do_acs(struct hostapd_data *hapd)
916 {
917 	struct drv_acs_params params;
918 	int ret, i, acs_ch_list_all = 0;
919 	struct hostapd_hw_modes *mode;
920 	int *freq_list = NULL;
921 	enum hostapd_hw_mode selected_mode;
922 
923 	if (hapd->driver == NULL || hapd->driver->do_acs == NULL)
924 		return 0;
925 
926 	os_memset(&params, 0, sizeof(params));
927 	params.hw_mode = hapd->iface->conf->hw_mode;
928 
929 	/*
930 	 * If no chanlist config parameter is provided, include all enabled
931 	 * channels of the selected hw_mode.
932 	 */
933 	if (hapd->iface->conf->acs_freq_list_present)
934 		acs_ch_list_all = !hapd->iface->conf->acs_freq_list.num;
935 	else
936 		acs_ch_list_all = !hapd->iface->conf->acs_ch_list.num;
937 
938 	if (hapd->iface->current_mode)
939 		selected_mode = hapd->iface->current_mode->mode;
940 	else
941 		selected_mode = HOSTAPD_MODE_IEEE80211ANY;
942 
943 	for (i = 0; i < hapd->iface->num_hw_features; i++) {
944 		mode = &hapd->iface->hw_features[i];
945 		if (selected_mode != HOSTAPD_MODE_IEEE80211ANY &&
946 		    selected_mode != mode->mode)
947 			continue;
948 		hostapd_get_hw_mode_any_channels(hapd, mode, acs_ch_list_all,
949 						 &freq_list);
950 	}
951 
952 	params.freq_list = freq_list;
953 	params.edmg_enabled = hapd->iface->conf->enable_edmg;
954 
955 	params.ht_enabled = !!(hapd->iface->conf->ieee80211n);
956 	params.ht40_enabled = !!(hapd->iface->conf->ht_capab &
957 				 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET);
958 	params.vht_enabled = !!(hapd->iface->conf->ieee80211ac);
959 	params.ch_width = 20;
960 	if (hapd->iface->conf->ieee80211n && params.ht40_enabled)
961 		params.ch_width = 40;
962 
963 	/* Note: VHT20 is defined by combination of ht_capab & oper_chwidth
964 	 */
965 	if ((hapd->iface->conf->ieee80211ax ||
966 	     hapd->iface->conf->ieee80211ac) &&
967 	    params.ht40_enabled) {
968 		u8 oper_chwidth = hostapd_get_oper_chwidth(hapd->iface->conf);
969 
970 		if (oper_chwidth == CHANWIDTH_80MHZ)
971 			params.ch_width = 80;
972 		else if (oper_chwidth == CHANWIDTH_160MHZ ||
973 			 oper_chwidth == CHANWIDTH_80P80MHZ)
974 			params.ch_width = 160;
975 	}
976 
977 	if (hapd->iface->conf->op_class)
978 		params.ch_width = op_class_to_bandwidth(
979 			hapd->iface->conf->op_class);
980 	ret = hapd->driver->do_acs(hapd->drv_priv, &params);
981 	os_free(freq_list);
982 
983 	return ret;
984 }
985 
986 
hostapd_drv_update_dh_ie(struct hostapd_data *hapd, const u8 *peer, u16 reason_code, const u8 *ie, size_t ielen)987 int hostapd_drv_update_dh_ie(struct hostapd_data *hapd, const u8 *peer,
988 			     u16 reason_code, const u8 *ie, size_t ielen)
989 {
990 	if (!hapd->driver || !hapd->driver->update_dh_ie || !hapd->drv_priv)
991 		return 0;
992 	return hapd->driver->update_dh_ie(hapd->drv_priv, peer, reason_code,
993 					  ie, ielen);
994 }
995 
996 
hostapd_drv_dpp_listen(struct hostapd_data *hapd, bool enable)997 int hostapd_drv_dpp_listen(struct hostapd_data *hapd, bool enable)
998 {
999 	if (!hapd->driver || !hapd->driver->dpp_listen || !hapd->drv_priv)
1000 		return 0;
1001 	return hapd->driver->dpp_listen(hapd->drv_priv, enable);
1002 }
1003