blob: 42c1aaaeb0beafb96cdc6eca36ad9da2c112e943 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidtcce06662013-11-04 18:44:24 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080015#include "common/hw_features_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070018#include "eap_server/tncs.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080019#include "eapol_auth/eapol_auth_sm.h"
20#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080021#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "hostapd.h"
23#include "authsrv.h"
24#include "sta_info.h"
25#include "accounting.h"
26#include "ap_list.h"
27#include "beacon.h"
28#include "iapp.h"
29#include "ieee802_1x.h"
30#include "ieee802_11_auth.h"
31#include "vlan_init.h"
32#include "wpa_auth.h"
33#include "wps_hostapd.h"
34#include "hw_features.h"
35#include "wpa_auth_glue.h"
36#include "ap_drv_ops.h"
37#include "ap_config.h"
38#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070039#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070040#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070041#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080042#include "bss_load.h"
43#include "x_snoop.h"
44#include "dhcp_snoop.h"
45#include "ndisc_snoop.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070046#include "neighbor_db.h"
47#include "rrm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49
Dmitry Shmidt04949592012-07-19 12:16:46 -070050static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070052static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080053static int setup_interface2(struct hostapd_iface *iface);
54static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
Dmitry Shmidt04949592012-07-19 12:16:46 -070057int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
58 int (*cb)(struct hostapd_iface *iface,
59 void *ctx), void *ctx)
60{
61 size_t i;
62 int ret;
63
64 for (i = 0; i < interfaces->count; i++) {
65 ret = cb(interfaces->iface[i], ctx);
66 if (ret)
67 return ret;
68 }
69
70 return 0;
71}
72
73
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074static void hostapd_reload_bss(struct hostapd_data *hapd)
75{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080076 struct hostapd_ssid *ssid;
77
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078#ifndef CONFIG_NO_RADIUS
79 radius_client_reconfig(hapd->radius, hapd->conf->radius);
80#endif /* CONFIG_NO_RADIUS */
81
Dmitry Shmidtcce06662013-11-04 18:44:24 -080082 ssid = &hapd->conf->ssid;
83 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
84 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
85 /*
86 * Force PSK to be derived again since SSID or passphrase may
87 * have changed.
88 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -080089 hostapd_config_clear_wpa_psk(&hapd->conf->ssid.wpa_psk);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080090 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 if (hostapd_setup_wpa_psk(hapd->conf)) {
92 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
93 "after reloading configuration");
94 }
95
96 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
97 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
98 else
99 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
100
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800101 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800103 if (hapd->wpa_auth)
104 wpa_init_keys(hapd->wpa_auth);
105 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 const u8 *wpa_ie;
107 size_t wpa_ie_len;
108 hostapd_reconfig_wpa(hapd);
109 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
110 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
111 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
112 "the kernel driver.");
113 } else if (hapd->wpa_auth) {
114 wpa_deinit(hapd->wpa_auth);
115 hapd->wpa_auth = NULL;
116 hostapd_set_privacy(hapd, 0);
117 hostapd_setup_encryption(hapd->conf->iface, hapd);
118 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
119 }
120
121 ieee802_11_set_beacon(hapd);
122 hostapd_update_wps(hapd);
123
124 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700125 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 hapd->conf->ssid.ssid_len)) {
127 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
128 /* try to continue */
129 }
130 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
131}
132
133
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700134static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136 size_t j;
137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138 /*
139 * Deauthenticate all stations since the new configuration may not
140 * allow them to use the BSS anymore.
141 */
142 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700143 hostapd_flush_old_stations(iface->bss[j],
144 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700145 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146
147#ifndef CONFIG_NO_RADIUS
148 /* TODO: update dynamic data based on changed configuration
149 * items (e.g., open/close sockets, etc.) */
150 radius_client_flush(iface->bss[j]->radius, 0);
151#endif /* CONFIG_NO_RADIUS */
152 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700153}
154
155
156int hostapd_reload_config(struct hostapd_iface *iface)
157{
158 struct hostapd_data *hapd = iface->bss[0];
159 struct hostapd_config *newconf, *oldconf;
160 size_t j;
161
162 if (iface->config_fname == NULL) {
163 /* Only in-memory config in use - assume it has been updated */
164 hostapd_clear_old(iface);
165 for (j = 0; j < iface->num_bss; j++)
166 hostapd_reload_bss(iface->bss[j]);
167 return 0;
168 }
169
170 if (iface->interfaces == NULL ||
171 iface->interfaces->config_read_cb == NULL)
172 return -1;
173 newconf = iface->interfaces->config_read_cb(iface->config_fname);
174 if (newconf == NULL)
175 return -1;
176
177 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178
179 oldconf = hapd->iconf;
180 iface->conf = newconf;
181
182 for (j = 0; j < iface->num_bss; j++) {
183 hapd = iface->bss[j];
184 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700185 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700186 hapd->iconf->acs = oldconf->acs;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700187 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700188 hapd->iconf->ieee80211n = oldconf->ieee80211n;
189 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
190 hapd->iconf->ht_capab = oldconf->ht_capab;
191 hapd->iconf->vht_capab = oldconf->vht_capab;
192 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
193 hapd->iconf->vht_oper_centr_freq_seg0_idx =
194 oldconf->vht_oper_centr_freq_seg0_idx;
195 hapd->iconf->vht_oper_centr_freq_seg1_idx =
196 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800197 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 hostapd_reload_bss(hapd);
199 }
200
201 hostapd_config_free(oldconf);
202
203
204 return 0;
205}
206
207
208static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
209 char *ifname)
210{
211 int i;
212
213 for (i = 0; i < NUM_WEP_KEYS; i++) {
214 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
215 0, NULL, 0, NULL, 0)) {
216 wpa_printf(MSG_DEBUG, "Failed to clear default "
217 "encryption keys (ifname=%s keyidx=%d)",
218 ifname, i);
219 }
220 }
221#ifdef CONFIG_IEEE80211W
222 if (hapd->conf->ieee80211w) {
223 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
224 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
225 NULL, i, 0, NULL,
226 0, NULL, 0)) {
227 wpa_printf(MSG_DEBUG, "Failed to clear "
228 "default mgmt encryption keys "
229 "(ifname=%s keyidx=%d)", ifname, i);
230 }
231 }
232 }
233#endif /* CONFIG_IEEE80211W */
234}
235
236
237static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
238{
239 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
240 return 0;
241}
242
243
244static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
245{
246 int errors = 0, idx;
247 struct hostapd_ssid *ssid = &hapd->conf->ssid;
248
249 idx = ssid->wep.idx;
250 if (ssid->wep.default_len &&
251 hostapd_drv_set_key(hapd->conf->iface,
252 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
253 1, NULL, 0, ssid->wep.key[idx],
254 ssid->wep.len[idx])) {
255 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
256 errors++;
257 }
258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 return errors;
260}
261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262
Dmitry Shmidt04949592012-07-19 12:16:46 -0700263static void hostapd_free_hapd_data(struct hostapd_data *hapd)
264{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800265 os_free(hapd->probereq_cb);
266 hapd->probereq_cb = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800267 hapd->num_probereq_cb = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800268
269#ifdef CONFIG_P2P
270 wpabuf_free(hapd->p2p_beacon_ie);
271 hapd->p2p_beacon_ie = NULL;
272 wpabuf_free(hapd->p2p_probe_resp_ie);
273 hapd->p2p_probe_resp_ie = NULL;
274#endif /* CONFIG_P2P */
275
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800276 if (!hapd->started) {
277 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
278 __func__, hapd->conf->iface);
279 return;
280 }
281 hapd->started = 0;
282
Dmitry Shmidt54605472013-11-08 11:10:19 -0800283 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 iapp_deinit(hapd->iapp);
285 hapd->iapp = NULL;
286 accounting_deinit(hapd);
287 hostapd_deinit_wpa(hapd);
288 vlan_deinit(hapd);
289 hostapd_acl_deinit(hapd);
290#ifndef CONFIG_NO_RADIUS
291 radius_client_deinit(hapd->radius);
292 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700293 radius_das_deinit(hapd->radius_das);
294 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295#endif /* CONFIG_NO_RADIUS */
296
297 hostapd_deinit_wps(hapd);
298
299 authsrv_deinit(hapd);
300
Dmitry Shmidt71757432014-06-02 13:50:35 -0700301 if (hapd->interface_added) {
302 hapd->interface_added = 0;
303 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
304 wpa_printf(MSG_WARNING,
305 "Failed to remove BSS interface %s",
306 hapd->conf->iface);
307 hapd->interface_added = 1;
308 } else {
309 /*
310 * Since this was a dynamically added interface, the
311 * driver wrapper may have removed its internal instance
312 * and hapd->drv_priv is not valid anymore.
313 */
314 hapd->drv_priv = NULL;
315 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 }
317
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700319
320#ifdef CONFIG_INTERWORKING
321 gas_serv_deinit(hapd);
322#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800323
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800324 bss_load_update_deinit(hapd);
325 ndisc_snoop_deinit(hapd);
326 dhcp_snoop_deinit(hapd);
327 x_snoop_deinit(hapd);
328
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800329#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700330 bin_clear_free(hapd->tmp_eap_user.identity,
331 hapd->tmp_eap_user.identity_len);
332 bin_clear_free(hapd->tmp_eap_user.password,
333 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800334#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800335
336#ifdef CONFIG_MESH
337 wpabuf_free(hapd->mesh_pending_auth);
338 hapd->mesh_pending_auth = NULL;
339#endif /* CONFIG_MESH */
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700340
341 hostapd_clean_rrm(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700342}
343
344
345/**
346 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
347 * @hapd: Pointer to BSS data
348 *
349 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800350 * Most of the modules that are initialized in hostapd_setup_bss() are
351 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700352 */
353static void hostapd_cleanup(struct hostapd_data *hapd)
354{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800355 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
356 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700357 if (hapd->iface->interfaces &&
358 hapd->iface->interfaces->ctrl_iface_deinit)
359 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700360 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361}
362
363
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364static void sta_track_deinit(struct hostapd_iface *iface)
365{
366 struct hostapd_sta_info *info;
367
368 if (!iface->num_sta_seen)
369 return;
370
371 while ((info = dl_list_first(&iface->sta_seen, struct hostapd_sta_info,
372 list))) {
373 dl_list_del(&info->list);
374 iface->num_sta_seen--;
375 os_free(info);
376 }
377}
378
379
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
381{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800382 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800383#ifdef CONFIG_IEEE80211N
384#ifdef NEED_AP_MLME
385 hostapd_stop_setup_timers(iface);
386#endif /* NEED_AP_MLME */
387#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700388 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
389 iface->hw_features = NULL;
390 os_free(iface->current_rates);
391 iface->current_rates = NULL;
392 os_free(iface->basic_rates);
393 iface->basic_rates = NULL;
394 ap_list_deinit(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800395 sta_track_deinit(iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700396}
397
398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399/**
400 * hostapd_cleanup_iface - Complete per-interface cleanup
401 * @iface: Pointer to interface data
402 *
403 * This function is called after per-BSS data structures are deinitialized
404 * with hostapd_cleanup().
405 */
406static void hostapd_cleanup_iface(struct hostapd_iface *iface)
407{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800408 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800409 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
410
Dmitry Shmidt04949592012-07-19 12:16:46 -0700411 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 hostapd_config_free(iface->conf);
413 iface->conf = NULL;
414
415 os_free(iface->config_fname);
416 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800417 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 os_free(iface);
419}
420
421
Dmitry Shmidt04949592012-07-19 12:16:46 -0700422static void hostapd_clear_wep(struct hostapd_data *hapd)
423{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800424 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700425 hostapd_set_privacy(hapd, 0);
426 hostapd_broadcast_wep_clear(hapd);
427 }
428}
429
430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
432{
433 int i;
434
435 hostapd_broadcast_wep_set(hapd);
436
437 if (hapd->conf->ssid.wep.default_len) {
438 hostapd_set_privacy(hapd, 1);
439 return 0;
440 }
441
Jouni Malinen75ecf522011-06-27 15:19:46 -0700442 /*
443 * When IEEE 802.1X is not enabled, the driver may need to know how to
444 * set authentication algorithms for static WEP.
445 */
446 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 for (i = 0; i < 4; i++) {
449 if (hapd->conf->ssid.wep.key[i] &&
450 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
451 i == hapd->conf->ssid.wep.idx, NULL, 0,
452 hapd->conf->ssid.wep.key[i],
453 hapd->conf->ssid.wep.len[i])) {
454 wpa_printf(MSG_WARNING, "Could not set WEP "
455 "encryption.");
456 return -1;
457 }
458 if (hapd->conf->ssid.wep.key[i] &&
459 i == hapd->conf->ssid.wep.idx)
460 hostapd_set_privacy(hapd, 1);
461 }
462
463 return 0;
464}
465
466
Dmitry Shmidt04949592012-07-19 12:16:46 -0700467static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468{
469 int ret = 0;
470 u8 addr[ETH_ALEN];
471
472 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
473 return 0;
474
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800475 if (!hapd->iface->driver_ap_teardown) {
476 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
477 "Flushing old station entries");
478
479 if (hostapd_flush(hapd)) {
480 wpa_msg(hapd->msg_ctx, MSG_WARNING,
481 "Could not connect to kernel driver");
482 ret = -1;
483 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800485 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700487 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 hostapd_free_stas(hapd);
489
490 return ret;
491}
492
493
Dmitry Shmidt71757432014-06-02 13:50:35 -0700494static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
495{
496 hostapd_free_stas(hapd);
497 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
498 hostapd_clear_wep(hapd);
499}
500
501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502/**
503 * hostapd_validate_bssid_configuration - Validate BSSID configuration
504 * @iface: Pointer to interface data
505 * Returns: 0 on success, -1 on failure
506 *
507 * This function is used to validate that the configured BSSIDs are valid.
508 */
509static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
510{
511 u8 mask[ETH_ALEN] = { 0 };
512 struct hostapd_data *hapd = iface->bss[0];
513 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 int auto_addr = 0;
515
516 if (hostapd_drv_none(hapd))
517 return 0;
518
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800519 if (iface->conf->use_driver_iface_addr)
520 return 0;
521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
523
524 /* Determine the bits necessary to cover the number of BSSIDs. */
525 for (i--; i; i >>= 1)
526 bits++;
527
528 /* Determine the bits necessary to any configured BSSIDs,
529 if they are higher than the number of BSSIDs. */
530 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800531 if (is_zero_ether_addr(iface->conf->bss[j]->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 if (j)
533 auto_addr++;
534 continue;
535 }
536
537 for (i = 0; i < ETH_ALEN; i++) {
538 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800539 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 hapd->own_addr[i];
541 }
542 }
543
544 if (!auto_addr)
545 goto skip_mask_ext;
546
547 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
548 ;
549 j = 0;
550 if (i < ETH_ALEN) {
551 j = (5 - i) * 8;
552
553 while (mask[i] != 0) {
554 mask[i] >>= 1;
555 j++;
556 }
557 }
558
559 if (bits < j)
560 bits = j;
561
562 if (bits > 40) {
563 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
564 bits);
565 return -1;
566 }
567
568 os_memset(mask, 0xff, ETH_ALEN);
569 j = bits / 8;
570 for (i = 5; i > 5 - j; i--)
571 mask[i] = 0;
572 j = bits % 8;
573 while (j--)
574 mask[i] <<= 1;
575
576skip_mask_ext:
577 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
578 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 if (!auto_addr)
581 return 0;
582
583 for (i = 0; i < ETH_ALEN; i++) {
584 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
585 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
586 " for start address " MACSTR ".",
587 MAC2STR(mask), MAC2STR(hapd->own_addr));
588 wpa_printf(MSG_ERROR, "Start address must be the "
589 "first address in the block (i.e., addr "
590 "AND mask == addr).");
591 return -1;
592 }
593 }
594
595 return 0;
596}
597
598
599static int mac_in_conf(struct hostapd_config *conf, const void *a)
600{
601 size_t i;
602
603 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800604 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 return 1;
606 }
607 }
608
609 return 0;
610}
611
612
Dmitry Shmidt04949592012-07-19 12:16:46 -0700613#ifndef CONFIG_NO_RADIUS
614
615static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
616 struct radius_das_attrs *attr)
617{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800618 if (attr->nas_identifier &&
619 (!hapd->conf->nas_identifier ||
620 os_strlen(hapd->conf->nas_identifier) !=
621 attr->nas_identifier_len ||
622 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
623 attr->nas_identifier_len) != 0)) {
624 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
625 return 1;
626 }
627
628 if (attr->nas_ip_addr &&
629 (hapd->conf->own_ip_addr.af != AF_INET ||
630 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
631 0)) {
632 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
633 return 1;
634 }
635
636#ifdef CONFIG_IPV6
637 if (attr->nas_ipv6_addr &&
638 (hapd->conf->own_ip_addr.af != AF_INET6 ||
639 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
640 != 0)) {
641 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
642 return 1;
643 }
644#endif /* CONFIG_IPV6 */
645
Dmitry Shmidt04949592012-07-19 12:16:46 -0700646 return 0;
647}
648
649
650static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800651 struct radius_das_attrs *attr,
652 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700653{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800654 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800656 int num_attr = 0;
657 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700658
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800659 *multi = 0;
660
661 for (sta = hapd->sta_list; sta; sta = sta->next)
662 sta->radius_das_match = 1;
663
664 if (attr->sta_addr) {
665 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700666 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800667 if (!sta) {
668 wpa_printf(MSG_DEBUG,
669 "RADIUS DAS: No Calling-Station-Id match");
670 return NULL;
671 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700672
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800673 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700674 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800675 if (sta != selected)
676 sta->radius_das_match = 0;
677 }
678 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
679 }
680
681 if (attr->acct_session_id) {
682 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800683 if (attr->acct_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800684 wpa_printf(MSG_DEBUG,
685 "RADIUS DAS: Acct-Session-Id cannot match");
686 return NULL;
687 }
688 count = 0;
689
690 for (sta = hapd->sta_list; sta; sta = sta->next) {
691 if (!sta->radius_das_match)
692 continue;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800693 os_snprintf(buf, sizeof(buf), "%016llX",
694 (unsigned long long) sta->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800695 if (os_memcmp(attr->acct_session_id, buf, 16) != 0)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800696 sta->radius_das_match = 0;
697 else
698 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700699 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800700
701 if (count == 0) {
702 wpa_printf(MSG_DEBUG,
703 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
704 return NULL;
705 }
706 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700707 }
708
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800709 if (attr->acct_multi_session_id) {
710 num_attr++;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800711 if (attr->acct_multi_session_id_len != 16) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800712 wpa_printf(MSG_DEBUG,
713 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
714 return NULL;
715 }
716 count = 0;
717
718 for (sta = hapd->sta_list; sta; sta = sta->next) {
719 if (!sta->radius_das_match)
720 continue;
721 if (!sta->eapol_sm ||
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800722 !sta->eapol_sm->acct_multi_session_id) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800723 sta->radius_das_match = 0;
724 continue;
725 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800726 os_snprintf(buf, sizeof(buf), "%016llX",
727 (unsigned long long)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800728 sta->eapol_sm->acct_multi_session_id);
729 if (os_memcmp(attr->acct_multi_session_id, buf, 16) !=
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800730 0)
731 sta->radius_das_match = 0;
732 else
733 count++;
734 }
735
736 if (count == 0) {
737 wpa_printf(MSG_DEBUG,
738 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
739 return NULL;
740 }
741 wpa_printf(MSG_DEBUG,
742 "RADIUS DAS: Acct-Multi-Session-Id match");
743 }
744
745 if (attr->cui) {
746 num_attr++;
747 count = 0;
748
Dmitry Shmidt04949592012-07-19 12:16:46 -0700749 for (sta = hapd->sta_list; sta; sta = sta->next) {
750 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800751
752 if (!sta->radius_das_match)
753 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700754 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800755 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700756 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800757 attr->cui_len) != 0)
758 sta->radius_das_match = 0;
759 else
760 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700761 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800762
763 if (count == 0) {
764 wpa_printf(MSG_DEBUG,
765 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
766 return NULL;
767 }
768 wpa_printf(MSG_DEBUG,
769 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700770 }
771
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800772 if (attr->user_name) {
773 num_attr++;
774 count = 0;
775
Dmitry Shmidt04949592012-07-19 12:16:46 -0700776 for (sta = hapd->sta_list; sta; sta = sta->next) {
777 u8 *identity;
778 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800779
780 if (!sta->radius_das_match)
781 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700782 identity = ieee802_1x_get_identity(sta->eapol_sm,
783 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800784 if (!identity ||
785 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800787 != 0)
788 sta->radius_das_match = 0;
789 else
790 count++;
791 }
792
793 if (count == 0) {
794 wpa_printf(MSG_DEBUG,
795 "RADIUS DAS: No matches remaining after User-Name check");
796 return NULL;
797 }
798 wpa_printf(MSG_DEBUG,
799 "RADIUS DAS: User-Name match");
800 }
801
802 if (num_attr == 0) {
803 /*
804 * In theory, we could match all current associations, but it
805 * seems safer to just reject requests that do not include any
806 * session identification attributes.
807 */
808 wpa_printf(MSG_DEBUG,
809 "RADIUS DAS: No session identification attributes included");
810 return NULL;
811 }
812
813 selected = NULL;
814 for (sta = hapd->sta_list; sta; sta = sta->next) {
815 if (sta->radius_das_match) {
816 if (selected) {
817 *multi = 1;
818 return NULL;
819 }
820 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700821 }
822 }
823
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800824 return selected;
825}
826
827
828static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
829 struct radius_das_attrs *attr)
830{
831 if (!hapd->wpa_auth)
832 return -1;
833 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700834}
835
836
837static enum radius_das_res
838hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
839{
840 struct hostapd_data *hapd = ctx;
841 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800842 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700843
844 if (hostapd_das_nas_mismatch(hapd, attr))
845 return RADIUS_DAS_NAS_MISMATCH;
846
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800847 sta = hostapd_das_find_sta(hapd, attr, &multi);
848 if (sta == NULL) {
849 if (multi) {
850 wpa_printf(MSG_DEBUG,
851 "RADIUS DAS: Multiple sessions match - not supported");
852 return RADIUS_DAS_MULTI_SESSION_MATCH;
853 }
854 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
855 wpa_printf(MSG_DEBUG,
856 "RADIUS DAS: PMKSA cache entry matched");
857 return RADIUS_DAS_SUCCESS;
858 }
859 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800861 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700862
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800863 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
864 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800865 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
866
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867 hostapd_drv_sta_deauth(hapd, sta->addr,
868 WLAN_REASON_PREV_AUTH_NOT_VALID);
869 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
870
871 return RADIUS_DAS_SUCCESS;
872}
873
874#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875
876
877/**
878 * hostapd_setup_bss - Per-BSS setup (initialization)
879 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800880 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
881 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 *
883 * This function is used to initialize all per-BSS data structures and
884 * resources. This gets called in a loop for each BSS when an interface is
885 * initialized. Most of the modules that are initialized here will be
886 * deinitialized in hostapd_cleanup().
887 */
888static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
889{
890 struct hostapd_bss_config *conf = hapd->conf;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700891 u8 ssid[SSID_MAX_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 int ssid_len, set_ssid;
893 char force_ifname[IFNAMSIZ];
894 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800895 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896
Dmitry Shmidt54605472013-11-08 11:10:19 -0800897 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700898 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800899
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700900#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700901 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700902 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
903 return -1;
904 }
905#endif /* EAP_SERVER_TNC */
906
Dmitry Shmidt54605472013-11-08 11:10:19 -0800907 if (hapd->started) {
908 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700909 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800910 return -1;
911 }
912 hapd->started = 1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700913 dl_list_init(&hapd->nr_db);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800914
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800915 if (!first || first == -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800916 u8 *addr = hapd->own_addr;
917
918 if (!is_zero_ether_addr(conf->bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700920 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921
922 if (hostapd_mac_comp(hapd->own_addr,
923 hapd->iface->bss[0]->own_addr) ==
924 0) {
925 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
926 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700927 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 return -1;
929 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800930 } else if (hapd->iconf->use_driver_iface_addr) {
931 addr = NULL;
932 } else {
933 /* Allocate the next available BSSID. */
934 do {
935 inc_byte_array(hapd->own_addr, ETH_ALEN);
936 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 }
938
Dmitry Shmidt54605472013-11-08 11:10:19 -0800939 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800941 conf->iface, addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700943 conf->bridge[0] ? conf->bridge : NULL,
944 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
946 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800947 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 return -1;
949 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800950
951 if (!addr)
952 os_memcpy(hapd->own_addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 }
954
955 if (conf->wmm_enabled < 0)
956 conf->wmm_enabled = hapd->iconf->ieee80211n;
957
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800958#ifdef CONFIG_IEEE80211R
959 if (is_zero_ether_addr(conf->r1_key_holder))
960 os_memcpy(conf->r1_key_holder, hapd->own_addr, ETH_ALEN);
961#endif /* CONFIG_IEEE80211R */
962
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800963#ifdef CONFIG_MESH
964 if (hapd->iface->mconf == NULL)
965 flush_old_stations = 0;
966#endif /* CONFIG_MESH */
967
968 if (flush_old_stations)
969 hostapd_flush_old_stations(hapd,
970 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 hostapd_set_privacy(hapd, 0);
972
973 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700974 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 return -1;
976
977 /*
978 * Fetch the SSID from the system and use it or,
979 * if one was specified in the config file, verify they
980 * match.
981 */
982 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
983 if (ssid_len < 0) {
984 wpa_printf(MSG_ERROR, "Could not read SSID from system");
985 return -1;
986 }
987 if (conf->ssid.ssid_set) {
988 /*
989 * If SSID is specified in the config file and it differs
990 * from what is being used then force installation of the
991 * new SSID.
992 */
993 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
994 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
995 } else {
996 /*
997 * No SSID in the config file; just use the one we got
998 * from the system.
999 */
1000 set_ssid = 0;
1001 conf->ssid.ssid_len = ssid_len;
1002 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003 }
1004
1005 if (!hostapd_drv_none(hapd)) {
1006 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001007 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001008 conf->iface, MAC2STR(hapd->own_addr),
1009 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 }
1011
1012 if (hostapd_setup_wpa_psk(conf)) {
1013 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1014 return -1;
1015 }
1016
1017 /* Set SSID for the kernel driver (to be used in beacon and probe
1018 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001019 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 conf->ssid.ssid_len)) {
1021 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1022 return -1;
1023 }
1024
Dmitry Shmidt818ea482014-03-10 13:15:21 -07001025 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 conf->radius->msg_dumps = 1;
1027#ifndef CONFIG_NO_RADIUS
1028 hapd->radius = radius_client_init(hapd, conf->radius);
1029 if (hapd->radius == NULL) {
1030 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1031 return -1;
1032 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001033
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001034 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001035 struct radius_das_conf das_conf;
1036 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001037 das_conf.port = conf->radius_das_port;
1038 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001039 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001040 conf->radius_das_shared_secret_len;
1041 das_conf.client_addr = &conf->radius_das_client_addr;
1042 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001043 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001044 conf->radius_das_require_event_timestamp;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001045 das_conf.ctx = hapd;
1046 das_conf.disconnect = hostapd_das_disconnect;
1047 hapd->radius_das = radius_das_init(&das_conf);
1048 if (hapd->radius_das == NULL) {
1049 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1050 "failed.");
1051 return -1;
1052 }
1053 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054#endif /* CONFIG_NO_RADIUS */
1055
1056 if (hostapd_acl_init(hapd)) {
1057 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1058 return -1;
1059 }
1060 if (hostapd_init_wps(hapd, conf))
1061 return -1;
1062
1063 if (authsrv_init(hapd) < 0)
1064 return -1;
1065
1066 if (ieee802_1x_init(hapd)) {
1067 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1068 return -1;
1069 }
1070
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001071 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 return -1;
1073
1074 if (accounting_init(hapd)) {
1075 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1076 return -1;
1077 }
1078
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001079 if (conf->ieee802_11f &&
1080 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1082 "failed.");
1083 return -1;
1084 }
1085
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086#ifdef CONFIG_INTERWORKING
1087 if (gas_serv_init(hapd)) {
1088 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1089 return -1;
1090 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001091
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001092 if (conf->qos_map_set_len &&
1093 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1094 conf->qos_map_set_len)) {
1095 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 return -1;
1097 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001098#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001100 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1101 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1102 return -1;
1103 }
1104
1105 if (conf->proxy_arp) {
1106 if (x_snoop_init(hapd)) {
1107 wpa_printf(MSG_ERROR,
1108 "Generic snooping infrastructure initialization failed");
1109 return -1;
1110 }
1111
1112 if (dhcp_snoop_init(hapd)) {
1113 wpa_printf(MSG_ERROR,
1114 "DHCP snooping initialization failed");
1115 return -1;
1116 }
1117
1118 if (ndisc_snoop_init(hapd)) {
1119 wpa_printf(MSG_ERROR,
1120 "Neighbor Discovery snooping initialization failed");
1121 return -1;
1122 }
1123 }
1124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1126 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1127 return -1;
1128 }
1129
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001130 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001131 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001133 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1134 return -1;
1135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 if (hapd->driver && hapd->driver->set_operstate)
1137 hapd->driver->set_operstate(hapd->drv_priv, 1);
1138
1139 return 0;
1140}
1141
1142
1143static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1144{
1145 struct hostapd_data *hapd = iface->bss[0];
1146 int i;
1147 struct hostapd_tx_queue_params *p;
1148
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001149#ifdef CONFIG_MESH
1150 if (iface->mconf == NULL)
1151 return;
1152#endif /* CONFIG_MESH */
1153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 for (i = 0; i < NUM_TX_QUEUES; i++) {
1155 p = &iface->conf->tx_queue[i];
1156
1157 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1158 p->cwmax, p->burst)) {
1159 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1160 "parameters for queue %d.", i);
1161 /* Continue anyway */
1162 }
1163 }
1164}
1165
1166
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001167static int hostapd_set_acl_list(struct hostapd_data *hapd,
1168 struct mac_acl_entry *mac_acl,
1169 int n_entries, u8 accept_acl)
1170{
1171 struct hostapd_acl_params *acl_params;
1172 int i, err;
1173
1174 acl_params = os_zalloc(sizeof(*acl_params) +
1175 (n_entries * sizeof(acl_params->mac_acl[0])));
1176 if (!acl_params)
1177 return -ENOMEM;
1178
1179 for (i = 0; i < n_entries; i++)
1180 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1181 ETH_ALEN);
1182
1183 acl_params->acl_policy = accept_acl;
1184 acl_params->num_mac_acl = n_entries;
1185
1186 err = hostapd_drv_set_acl(hapd, acl_params);
1187
1188 os_free(acl_params);
1189
1190 return err;
1191}
1192
1193
1194static void hostapd_set_acl(struct hostapd_data *hapd)
1195{
1196 struct hostapd_config *conf = hapd->iconf;
1197 int err;
1198 u8 accept_acl;
1199
1200 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1201 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001202
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001203 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001204 accept_acl = 1;
1205 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1206 conf->bss[0]->num_accept_mac,
1207 accept_acl);
1208 if (err) {
1209 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1210 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001211 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001212 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001213 accept_acl = 0;
1214 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1215 conf->bss[0]->num_deny_mac,
1216 accept_acl);
1217 if (err) {
1218 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1219 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001220 }
1221 }
1222}
1223
1224
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001225static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1226{
1227 if (!hapd->iface->interfaces ||
1228 !hapd->iface->interfaces->ctrl_iface_init)
1229 return 0;
1230
1231 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1232 wpa_printf(MSG_ERROR,
1233 "Failed to setup control interface for %s",
1234 hapd->conf->iface);
1235 return -1;
1236 }
1237
1238 return 0;
1239}
1240
1241
1242static int start_ctrl_iface(struct hostapd_iface *iface)
1243{
1244 size_t i;
1245
1246 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1247 return 0;
1248
1249 for (i = 0; i < iface->num_bss; i++) {
1250 struct hostapd_data *hapd = iface->bss[i];
1251 if (iface->interfaces->ctrl_iface_init(hapd)) {
1252 wpa_printf(MSG_ERROR,
1253 "Failed to setup control interface for %s",
1254 hapd->conf->iface);
1255 return -1;
1256 }
1257 }
1258
1259 return 0;
1260}
1261
1262
1263static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1264{
1265 struct hostapd_iface *iface = eloop_ctx;
1266
1267 if (!iface->wait_channel_update) {
1268 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1269 return;
1270 }
1271
1272 /*
1273 * It is possible that the existing channel list is acceptable, so try
1274 * to proceed.
1275 */
1276 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1277 setup_interface2(iface);
1278}
1279
1280
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001281void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001282{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001283 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001284 return;
1285
1286 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1287 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1288 setup_interface2(iface);
1289}
1290
1291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292static int setup_interface(struct hostapd_iface *iface)
1293{
1294 struct hostapd_data *hapd = iface->bss[0];
1295 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001296
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001297 /*
1298 * It is possible that setup_interface() is called after the interface
1299 * was disabled etc., in which case driver_ap_teardown is possibly set
1300 * to 1. Clear it here so any other key/station deletion, which is not
1301 * part of a teardown flow, would also call the relevant driver
1302 * callbacks.
1303 */
1304 iface->driver_ap_teardown = 0;
1305
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001306 if (!iface->phy[0]) {
1307 const char *phy = hostapd_drv_get_radio_name(hapd);
1308 if (phy) {
1309 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1310 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1311 }
1312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313
1314 /*
1315 * Make sure that all BSSes get configured with a pointer to the same
1316 * driver interface.
1317 */
1318 for (i = 1; i < iface->num_bss; i++) {
1319 iface->bss[i]->driver = hapd->driver;
1320 iface->bss[i]->drv_priv = hapd->drv_priv;
1321 }
1322
1323 if (hostapd_validate_bssid_configuration(iface))
1324 return -1;
1325
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001326 /*
1327 * Initialize control interfaces early to allow external monitoring of
1328 * channel setup operations that may take considerable amount of time
1329 * especially for DFS cases.
1330 */
1331 if (start_ctrl_iface(iface))
1332 return -1;
1333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001335 char country[4], previous_country[4];
1336
1337 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1338 if (hostapd_get_country(hapd, previous_country) < 0)
1339 previous_country[0] = '\0';
1340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 os_memcpy(country, hapd->iconf->country, 3);
1342 country[3] = '\0';
1343 if (hostapd_set_country(hapd, country) < 0) {
1344 wpa_printf(MSG_ERROR, "Failed to set country code");
1345 return -1;
1346 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001347
1348 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1349 previous_country, country);
1350
1351 if (os_strncmp(previous_country, country, 2) != 0) {
1352 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1353 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001354 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001355 channel_list_update_timeout,
1356 iface, NULL);
1357 return 0;
1358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 }
1360
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001361 return setup_interface2(iface);
1362}
1363
1364
1365static int setup_interface2(struct hostapd_iface *iface)
1366{
1367 iface->wait_channel_update = 0;
1368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 if (hostapd_get_hw_features(iface)) {
1370 /* Not all drivers support this yet, so continue without hw
1371 * feature data. */
1372 } else {
1373 int ret = hostapd_select_hw_mode(iface);
1374 if (ret < 0) {
1375 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1376 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001377 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001379 if (ret == 1) {
1380 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1381 return 0;
1382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 ret = hostapd_check_ht_capab(iface);
1384 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001385 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 if (ret == 1) {
1387 wpa_printf(MSG_DEBUG, "Interface initialization will "
1388 "be completed in a callback");
1389 return 0;
1390 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001391
1392 if (iface->conf->ieee80211h)
1393 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 }
1395 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001396
1397fail:
1398 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1399 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1400 if (iface->interfaces && iface->interfaces->terminate_on_error)
1401 eloop_terminate();
1402 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403}
1404
1405
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001406#ifdef CONFIG_FST
1407
1408static const u8 * fst_hostapd_get_bssid_cb(void *ctx)
1409{
1410 struct hostapd_data *hapd = ctx;
1411
1412 return hapd->own_addr;
1413}
1414
1415
1416static void fst_hostapd_get_channel_info_cb(void *ctx,
1417 enum hostapd_hw_mode *hw_mode,
1418 u8 *channel)
1419{
1420 struct hostapd_data *hapd = ctx;
1421
1422 *hw_mode = ieee80211_freq_to_chan(hapd->iface->freq, channel);
1423}
1424
1425
1426static void fst_hostapd_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
1427{
1428 struct hostapd_data *hapd = ctx;
1429
1430 if (hapd->iface->fst_ies != fst_ies) {
1431 hapd->iface->fst_ies = fst_ies;
1432 if (ieee802_11_set_beacon(hapd))
1433 wpa_printf(MSG_WARNING, "FST: Cannot set beacon");
1434 }
1435}
1436
1437
1438static int fst_hostapd_send_action_cb(void *ctx, const u8 *da,
1439 struct wpabuf *buf)
1440{
1441 struct hostapd_data *hapd = ctx;
1442
1443 return hostapd_drv_send_action(hapd, hapd->iface->freq, 0, da,
1444 wpabuf_head(buf), wpabuf_len(buf));
1445}
1446
1447
1448static const struct wpabuf * fst_hostapd_get_mb_ie_cb(void *ctx, const u8 *addr)
1449{
1450 struct hostapd_data *hapd = ctx;
1451 struct sta_info *sta = ap_get_sta(hapd, addr);
1452
1453 return sta ? sta->mb_ies : NULL;
1454}
1455
1456
1457static void fst_hostapd_update_mb_ie_cb(void *ctx, const u8 *addr,
1458 const u8 *buf, size_t size)
1459{
1460 struct hostapd_data *hapd = ctx;
1461 struct sta_info *sta = ap_get_sta(hapd, addr);
1462
1463 if (sta) {
1464 struct mb_ies_info info;
1465
1466 if (!mb_ies_info_by_ies(&info, buf, size)) {
1467 wpabuf_free(sta->mb_ies);
1468 sta->mb_ies = mb_ies_by_info(&info);
1469 }
1470 }
1471}
1472
1473
1474static const u8 * fst_hostapd_get_sta(struct fst_get_peer_ctx **get_ctx,
1475 Boolean mb_only)
1476{
1477 struct sta_info *s = (struct sta_info *) *get_ctx;
1478
1479 if (mb_only) {
1480 for (; s && !s->mb_ies; s = s->next)
1481 ;
1482 }
1483
1484 if (s) {
1485 *get_ctx = (struct fst_get_peer_ctx *) s->next;
1486
1487 return s->addr;
1488 }
1489
1490 *get_ctx = NULL;
1491 return NULL;
1492}
1493
1494
1495static const u8 * fst_hostapd_get_peer_first(void *ctx,
1496 struct fst_get_peer_ctx **get_ctx,
1497 Boolean mb_only)
1498{
1499 struct hostapd_data *hapd = ctx;
1500
1501 *get_ctx = (struct fst_get_peer_ctx *) hapd->sta_list;
1502
1503 return fst_hostapd_get_sta(get_ctx, mb_only);
1504}
1505
1506
1507static const u8 * fst_hostapd_get_peer_next(void *ctx,
1508 struct fst_get_peer_ctx **get_ctx,
1509 Boolean mb_only)
1510{
1511 return fst_hostapd_get_sta(get_ctx, mb_only);
1512}
1513
1514
1515void fst_hostapd_fill_iface_obj(struct hostapd_data *hapd,
1516 struct fst_wpa_obj *iface_obj)
1517{
1518 iface_obj->ctx = hapd;
1519 iface_obj->get_bssid = fst_hostapd_get_bssid_cb;
1520 iface_obj->get_channel_info = fst_hostapd_get_channel_info_cb;
1521 iface_obj->set_ies = fst_hostapd_set_ies_cb;
1522 iface_obj->send_action = fst_hostapd_send_action_cb;
1523 iface_obj->get_mb_ie = fst_hostapd_get_mb_ie_cb;
1524 iface_obj->update_mb_ie = fst_hostapd_update_mb_ie_cb;
1525 iface_obj->get_peer_first = fst_hostapd_get_peer_first;
1526 iface_obj->get_peer_next = fst_hostapd_get_peer_next;
1527}
1528
1529#endif /* CONFIG_FST */
1530
1531
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001532#ifdef NEED_AP_MLME
1533static enum nr_chan_width hostapd_get_nr_chan_width(struct hostapd_data *hapd,
1534 int ht, int vht)
1535{
1536 if (!ht && !vht)
1537 return NR_CHAN_WIDTH_20;
1538 if (!hapd->iconf->secondary_channel)
1539 return NR_CHAN_WIDTH_20;
1540 if (!vht || hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_USE_HT)
1541 return NR_CHAN_WIDTH_40;
1542 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80MHZ)
1543 return NR_CHAN_WIDTH_80;
1544 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_160MHZ)
1545 return NR_CHAN_WIDTH_160;
1546 if (hapd->iconf->vht_oper_chwidth == VHT_CHANWIDTH_80P80MHZ)
1547 return NR_CHAN_WIDTH_80P80;
1548 return NR_CHAN_WIDTH_20;
1549}
1550#endif /* NEED_AP_MLME */
1551
1552
1553static void hostapd_set_own_neighbor_report(struct hostapd_data *hapd)
1554{
1555#ifdef NEED_AP_MLME
1556 u16 capab = hostapd_own_capab_info(hapd);
1557 int ht = hapd->iconf->ieee80211n && !hapd->conf->disable_11n;
1558 int vht = hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac;
1559 struct wpa_ssid_value ssid;
1560 u8 channel, op_class;
1561 int center_freq1 = 0, center_freq2 = 0;
1562 enum nr_chan_width width;
1563 u32 bssid_info;
1564 struct wpabuf *nr;
1565
1566 if (!(hapd->conf->radio_measurements[0] &
1567 WLAN_RRM_CAPS_NEIGHBOR_REPORT))
1568 return;
1569
1570 bssid_info = 3; /* AP is reachable */
1571 bssid_info |= NEI_REP_BSSID_INFO_SECURITY; /* "same as the AP" */
1572 bssid_info |= NEI_REP_BSSID_INFO_KEY_SCOPE; /* "same as the AP" */
1573
1574 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT)
1575 bssid_info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
1576
1577 bssid_info |= NEI_REP_BSSID_INFO_RM; /* RRM is supported */
1578
1579 if (hapd->conf->wmm_enabled) {
1580 bssid_info |= NEI_REP_BSSID_INFO_QOS;
1581
1582 if (hapd->conf->wmm_uapsd &&
1583 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
1584 bssid_info |= NEI_REP_BSSID_INFO_APSD;
1585 }
1586
1587 if (ht) {
1588 bssid_info |= NEI_REP_BSSID_INFO_HT |
1589 NEI_REP_BSSID_INFO_DELAYED_BA;
1590
1591 /* VHT bit added in IEEE P802.11-REVmc/D4.3 */
1592 if (vht)
1593 bssid_info |= NEI_REP_BSSID_INFO_VHT;
1594 }
1595
1596 /* TODO: Set NEI_REP_BSSID_INFO_MOBILITY_DOMAIN if MDE is set */
1597
1598 ieee80211_freq_to_channel_ext(hapd->iface->freq,
1599 hapd->iconf->secondary_channel,
1600 hapd->iconf->vht_oper_chwidth,
1601 &op_class, &channel);
1602 width = hostapd_get_nr_chan_width(hapd, ht, vht);
1603 if (vht) {
1604 center_freq1 = ieee80211_chan_to_freq(
1605 NULL, op_class,
1606 hapd->iconf->vht_oper_centr_freq_seg0_idx);
1607 if (width == NR_CHAN_WIDTH_80P80)
1608 center_freq2 = ieee80211_chan_to_freq(
1609 NULL, op_class,
1610 hapd->iconf->vht_oper_centr_freq_seg1_idx);
1611 } else if (ht) {
1612 center_freq1 = hapd->iface->freq +
1613 10 * hapd->iconf->secondary_channel;
1614 }
1615
1616 ssid.ssid_len = hapd->conf->ssid.ssid_len;
1617 os_memcpy(ssid.ssid, hapd->conf->ssid.ssid, ssid.ssid_len);
1618
1619 /*
1620 * Neighbor Report element size = BSSID + BSSID info + op_class + chan +
1621 * phy type + wide bandwidth channel subelement.
1622 */
1623 nr = wpabuf_alloc(ETH_ALEN + 4 + 1 + 1 + 1 + 5);
1624 if (!nr)
1625 return;
1626
1627 wpabuf_put_data(nr, hapd->own_addr, ETH_ALEN);
1628 wpabuf_put_le32(nr, bssid_info);
1629 wpabuf_put_u8(nr, op_class);
1630 wpabuf_put_u8(nr, channel);
1631 wpabuf_put_u8(nr, ieee80211_get_phy_type(hapd->iface->freq, ht, vht));
1632
1633 /*
1634 * Wide Bandwidth Channel subelement may be needed to allow the
1635 * receiving STA to send packets to the AP. See IEEE P802.11-REVmc/D5.0
1636 * Figure 9-301.
1637 */
1638 wpabuf_put_u8(nr, WNM_NEIGHBOR_WIDE_BW_CHAN);
1639 wpabuf_put_u8(nr, 3);
1640 wpabuf_put_u8(nr, width);
1641 wpabuf_put_u8(nr, center_freq1);
1642 wpabuf_put_u8(nr, center_freq2);
1643
1644 hostapd_neighbor_set(hapd, hapd->own_addr, &ssid, nr, hapd->iconf->lci,
1645 hapd->iconf->civic);
1646
1647 wpabuf_free(nr);
1648#endif /* NEED_AP_MLME */
1649}
1650
1651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001652static int hostapd_setup_interface_complete_sync(struct hostapd_iface *iface,
1653 int err)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654{
1655 struct hostapd_data *hapd = iface->bss[0];
1656 size_t j;
1657 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001658 int delay_apply_cfg = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001659 int res_dfs_offload = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001661 if (err)
1662 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663
1664 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001665 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001666#ifdef NEED_AP_MLME
1667 int res;
1668#endif /* NEED_AP_MLME */
1669
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001670 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1672 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001673 hostapd_hw_mode_txt(iface->conf->hw_mode),
1674 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675
Dmitry Shmidt051af732013-10-22 13:52:46 -07001676#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001677 /* Handle DFS only if it is not offloaded to the driver */
1678 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1679 /* Check DFS */
1680 res = hostapd_handle_dfs(iface);
1681 if (res <= 0) {
1682 if (res < 0)
1683 goto fail;
1684 return res;
1685 }
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001686 } else {
1687 /* If DFS is offloaded to the driver */
1688 res_dfs_offload = hostapd_handle_dfs_offload(iface);
1689 if (res_dfs_offload <= 0) {
1690 if (res_dfs_offload < 0)
1691 goto fail;
1692 } else {
1693 wpa_printf(MSG_DEBUG,
1694 "Proceed with AP/channel setup");
1695 /*
1696 * If this is a DFS channel, move to completing
1697 * AP setup.
1698 */
1699 if (res_dfs_offload == 1)
1700 goto dfs_offload;
1701 /* Otherwise fall through. */
1702 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001703 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001704#endif /* NEED_AP_MLME */
1705
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001706#ifdef CONFIG_MESH
1707 if (iface->mconf != NULL) {
1708 wpa_printf(MSG_DEBUG,
1709 "%s: Mesh configuration will be applied while joining the mesh network",
1710 iface->bss[0]->conf->iface);
1711 delay_apply_cfg = 1;
1712 }
1713#endif /* CONFIG_MESH */
1714
1715 if (!delay_apply_cfg &&
1716 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 hapd->iconf->channel,
1718 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001719 hapd->iconf->ieee80211ac,
1720 hapd->iconf->secondary_channel,
1721 hapd->iconf->vht_oper_chwidth,
1722 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1723 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 wpa_printf(MSG_ERROR, "Could not set channel for "
1725 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001726 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 }
1728 }
1729
1730 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001731 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1733 "table.");
1734 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1735 HOSTAPD_LEVEL_WARNING,
1736 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001737 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 }
1739 }
1740
1741 if (hapd->iconf->rts_threshold > -1 &&
1742 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1743 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1744 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001745 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 }
1747
1748 if (hapd->iconf->fragm_threshold > -1 &&
1749 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1750 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1751 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001752 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 }
1754
1755 prev_addr = hapd->own_addr;
1756
1757 for (j = 0; j < iface->num_bss; j++) {
1758 hapd = iface->bss[j];
1759 if (j)
1760 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001761 if (hostapd_setup_bss(hapd, j == 0)) {
1762 do {
1763 hapd = iface->bss[j];
1764 hostapd_bss_deinit_no_free(hapd);
1765 hostapd_free_hapd_data(hapd);
1766 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001767 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001768 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001769 if (is_zero_ether_addr(hapd->conf->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 prev_addr = hapd->own_addr;
1771 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001772 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
1774 hostapd_tx_queue_params(iface);
1775
1776 ap_list_init(iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001777 dl_list_init(&iface->sta_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001779 hostapd_set_acl(hapd);
1780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 if (hostapd_driver_commit(hapd) < 0) {
1782 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1783 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001784 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785 }
1786
Jouni Malinen87fd2792011-05-16 18:35:42 +03001787 /*
1788 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1789 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1790 * mode), the interface is up only after driver_commit, so initialize
1791 * WPS after driver_commit.
1792 */
1793 for (j = 0; j < iface->num_bss; j++) {
1794 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001795 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001796 }
1797
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001798 if ((iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
1799 !res_dfs_offload) {
1800 /*
1801 * If freq is DFS, and DFS is offloaded to the driver, then wait
1802 * for CAC to complete.
1803 */
1804 wpa_printf(MSG_DEBUG, "%s: Wait for CAC to complete", __func__);
1805 return res_dfs_offload;
1806 }
1807
1808#ifdef NEED_AP_MLME
1809dfs_offload:
1810#endif /* NEED_AP_MLME */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001811
1812#ifdef CONFIG_FST
1813 if (hapd->iconf->fst_cfg.group_id[0]) {
1814 struct fst_wpa_obj iface_obj;
1815
1816 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
1817 iface->fst = fst_attach(hapd->conf->iface, hapd->own_addr,
1818 &iface_obj, &hapd->iconf->fst_cfg);
1819 if (!iface->fst) {
1820 wpa_printf(MSG_ERROR, "Could not attach to FST %s",
1821 hapd->iconf->fst_cfg.group_id);
1822 goto fail;
1823 }
1824 }
1825#endif /* CONFIG_FST */
1826
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001827 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1828 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 if (hapd->setup_complete_cb)
1830 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1831
1832 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1833 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001834 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1835 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001837 for (j = 0; j < iface->num_bss; j++)
1838 hostapd_set_own_neighbor_report(iface->bss[j]);
1839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001841
1842fail:
1843 wpa_printf(MSG_ERROR, "Interface initialization failed");
1844 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1845 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001846#ifdef CONFIG_FST
1847 if (iface->fst) {
1848 fst_detach(iface->fst);
1849 iface->fst = NULL;
1850 }
1851#endif /* CONFIG_FST */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001852 if (iface->interfaces && iface->interfaces->terminate_on_error)
1853 eloop_terminate();
1854 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855}
1856
1857
1858/**
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001859 * hostapd_setup_interface_complete - Complete interface setup
1860 *
1861 * This function is called when previous steps in the interface setup has been
1862 * completed. This can also start operations, e.g., DFS, that will require
1863 * additional processing before interface is ready to be enabled. Such
1864 * operations will call this function from eloop callbacks when finished.
1865 */
1866int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1867{
1868 struct hapd_interfaces *interfaces = iface->interfaces;
1869 struct hostapd_data *hapd = iface->bss[0];
1870 unsigned int i;
1871 int not_ready_in_sync_ifaces = 0;
1872
1873 if (!iface->need_to_start_in_sync)
1874 return hostapd_setup_interface_complete_sync(iface, err);
1875
1876 if (err) {
1877 wpa_printf(MSG_ERROR, "Interface initialization failed");
1878 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1879 iface->need_to_start_in_sync = 0;
1880 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1881 if (interfaces && interfaces->terminate_on_error)
1882 eloop_terminate();
1883 return -1;
1884 }
1885
1886 if (iface->ready_to_start_in_sync) {
1887 /* Already in ready and waiting. should never happpen */
1888 return 0;
1889 }
1890
1891 for (i = 0; i < interfaces->count; i++) {
1892 if (interfaces->iface[i]->need_to_start_in_sync &&
1893 !interfaces->iface[i]->ready_to_start_in_sync)
1894 not_ready_in_sync_ifaces++;
1895 }
1896
1897 /*
1898 * Check if this is the last interface, if yes then start all the other
1899 * waiting interfaces. If not, add this interface to the waiting list.
1900 */
1901 if (not_ready_in_sync_ifaces > 1 && iface->state == HAPD_IFACE_DFS) {
1902 /*
1903 * If this interface went through CAC, do not synchronize, just
1904 * start immediately.
1905 */
1906 iface->need_to_start_in_sync = 0;
1907 wpa_printf(MSG_INFO,
1908 "%s: Finished CAC - bypass sync and start interface",
1909 iface->bss[0]->conf->iface);
1910 return hostapd_setup_interface_complete_sync(iface, err);
1911 }
1912
1913 if (not_ready_in_sync_ifaces > 1) {
1914 /* need to wait as there are other interfaces still coming up */
1915 iface->ready_to_start_in_sync = 1;
1916 wpa_printf(MSG_INFO,
1917 "%s: Interface waiting to sync with other interfaces",
1918 iface->bss[0]->conf->iface);
1919 return 0;
1920 }
1921
1922 wpa_printf(MSG_INFO,
1923 "%s: Last interface to sync - starting all interfaces",
1924 iface->bss[0]->conf->iface);
1925 iface->need_to_start_in_sync = 0;
1926 hostapd_setup_interface_complete_sync(iface, err);
1927 for (i = 0; i < interfaces->count; i++) {
1928 if (interfaces->iface[i]->need_to_start_in_sync &&
1929 interfaces->iface[i]->ready_to_start_in_sync) {
1930 hostapd_setup_interface_complete_sync(
1931 interfaces->iface[i], 0);
1932 /* Only once the interfaces are sync started */
1933 interfaces->iface[i]->need_to_start_in_sync = 0;
1934 }
1935 }
1936
1937 return 0;
1938}
1939
1940
1941/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 * hostapd_setup_interface - Setup of an interface
1943 * @iface: Pointer to interface data.
1944 * Returns: 0 on success, -1 on failure
1945 *
1946 * Initializes the driver interface, validates the configuration,
1947 * and sets driver parameters based on the configuration.
1948 * Flushes old stations, sets the channel, encryption,
1949 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001950 *
1951 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1952 * or DFS operations, this function returns 0 before such operations have been
1953 * completed. The pending operations are registered into eloop and will be
1954 * completed from eloop callbacks. Those callbacks end up calling
1955 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 */
1957int hostapd_setup_interface(struct hostapd_iface *iface)
1958{
1959 int ret;
1960
1961 ret = setup_interface(iface);
1962 if (ret) {
1963 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1964 iface->bss[0]->conf->iface);
1965 return -1;
1966 }
1967
1968 return 0;
1969}
1970
1971
1972/**
1973 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1974 * @hapd_iface: Pointer to interface data
1975 * @conf: Pointer to per-interface configuration
1976 * @bss: Pointer to per-BSS configuration for this BSS
1977 * Returns: Pointer to allocated BSS data
1978 *
1979 * This function is used to allocate per-BSS data structure. This data will be
1980 * freed after hostapd_cleanup() is called for it during interface
1981 * deinitialization.
1982 */
1983struct hostapd_data *
1984hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1985 struct hostapd_config *conf,
1986 struct hostapd_bss_config *bss)
1987{
1988 struct hostapd_data *hapd;
1989
1990 hapd = os_zalloc(sizeof(*hapd));
1991 if (hapd == NULL)
1992 return NULL;
1993
1994 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1995 hapd->iconf = conf;
1996 hapd->conf = bss;
1997 hapd->iface = hapd_iface;
1998 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001999 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002000 dl_list_init(&hapd->ctrl_dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001
2002 return hapd;
2003}
2004
2005
Dmitry Shmidt54605472013-11-08 11:10:19 -08002006static void hostapd_bss_deinit(struct hostapd_data *hapd)
2007{
2008 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
2009 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002010 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002011 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002012 hostapd_cleanup(hapd);
2013}
2014
2015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016void hostapd_interface_deinit(struct hostapd_iface *iface)
2017{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002018 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019
Dmitry Shmidt54605472013-11-08 11:10:19 -08002020 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 if (iface == NULL)
2022 return;
2023
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07002024 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
2025
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002026#ifdef CONFIG_IEEE80211N
2027#ifdef NEED_AP_MLME
2028 hostapd_stop_setup_timers(iface);
2029 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
2030#endif /* NEED_AP_MLME */
2031#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002032 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
2033 iface->wait_channel_update = 0;
2034
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002035#ifdef CONFIG_FST
2036 if (iface->fst) {
2037 fst_detach(iface->fst);
2038 iface->fst = NULL;
2039 }
2040#endif /* CONFIG_FST */
2041
Dmitry Shmidt54605472013-11-08 11:10:19 -08002042 for (j = iface->num_bss - 1; j >= 0; j--)
2043 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044}
2045
2046
2047void hostapd_interface_free(struct hostapd_iface *iface)
2048{
2049 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002050 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
2051 for (j = 0; j < iface->num_bss; j++) {
2052 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
2053 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002055 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 hostapd_cleanup_iface(iface);
2057}
2058
2059
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002060/**
2061 * hostapd_init - Allocate and initialize per-interface data
2062 * @config_file: Path to the configuration file
2063 * Returns: Pointer to the allocated interface data or %NULL on failure
2064 *
2065 * This function is used to allocate main data structures for per-interface
2066 * data. The allocated data buffer will be freed by calling
2067 * hostapd_cleanup_iface().
2068 */
2069struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
2070 const char *config_file)
2071{
2072 struct hostapd_iface *hapd_iface = NULL;
2073 struct hostapd_config *conf = NULL;
2074 struct hostapd_data *hapd;
2075 size_t i;
2076
2077 hapd_iface = os_zalloc(sizeof(*hapd_iface));
2078 if (hapd_iface == NULL)
2079 goto fail;
2080
2081 hapd_iface->config_fname = os_strdup(config_file);
2082 if (hapd_iface->config_fname == NULL)
2083 goto fail;
2084
2085 conf = interfaces->config_read_cb(hapd_iface->config_fname);
2086 if (conf == NULL)
2087 goto fail;
2088 hapd_iface->conf = conf;
2089
2090 hapd_iface->num_bss = conf->num_bss;
2091 hapd_iface->bss = os_calloc(conf->num_bss,
2092 sizeof(struct hostapd_data *));
2093 if (hapd_iface->bss == NULL)
2094 goto fail;
2095
2096 for (i = 0; i < conf->num_bss; i++) {
2097 hapd = hapd_iface->bss[i] =
2098 hostapd_alloc_bss_data(hapd_iface, conf,
2099 conf->bss[i]);
2100 if (hapd == NULL)
2101 goto fail;
2102 hapd->msg_ctx = hapd;
2103 }
2104
2105 return hapd_iface;
2106
2107fail:
2108 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
2109 config_file);
2110 if (conf)
2111 hostapd_config_free(conf);
2112 if (hapd_iface) {
2113 os_free(hapd_iface->config_fname);
2114 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002115 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2116 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002117 os_free(hapd_iface);
2118 }
2119 return NULL;
2120}
2121
2122
2123static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
2124{
2125 size_t i, j;
2126
2127 for (i = 0; i < interfaces->count; i++) {
2128 struct hostapd_iface *iface = interfaces->iface[i];
2129 for (j = 0; j < iface->num_bss; j++) {
2130 struct hostapd_data *hapd = iface->bss[j];
2131 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2132 return 1;
2133 }
2134 }
2135
2136 return 0;
2137}
2138
2139
2140/**
2141 * hostapd_interface_init_bss - Read configuration file and init BSS data
2142 *
2143 * This function is used to parse configuration file for a BSS. This BSS is
2144 * added to an existing interface sharing the same radio (if any) or a new
2145 * interface is created if this is the first interface on a radio. This
2146 * allocate memory for the BSS. No actual driver operations are started.
2147 *
2148 * This is similar to hostapd_interface_init(), but for a case where the
2149 * configuration is used to add a single BSS instead of all BSSes for a radio.
2150 */
2151struct hostapd_iface *
2152hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
2153 const char *config_fname, int debug)
2154{
2155 struct hostapd_iface *new_iface = NULL, *iface = NULL;
2156 struct hostapd_data *hapd;
2157 int k;
2158 size_t i, bss_idx;
2159
2160 if (!phy || !*phy)
2161 return NULL;
2162
2163 for (i = 0; i < interfaces->count; i++) {
2164 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
2165 iface = interfaces->iface[i];
2166 break;
2167 }
2168 }
2169
2170 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
2171 config_fname, phy, iface ? "" : " --> new PHY");
2172 if (iface) {
2173 struct hostapd_config *conf;
2174 struct hostapd_bss_config **tmp_conf;
2175 struct hostapd_data **tmp_bss;
2176 struct hostapd_bss_config *bss;
2177 const char *ifname;
2178
2179 /* Add new BSS to existing iface */
2180 conf = interfaces->config_read_cb(config_fname);
2181 if (conf == NULL)
2182 return NULL;
2183 if (conf->num_bss > 1) {
2184 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
2185 hostapd_config_free(conf);
2186 return NULL;
2187 }
2188
2189 ifname = conf->bss[0]->iface;
2190 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
2191 wpa_printf(MSG_ERROR,
2192 "Interface name %s already in use", ifname);
2193 hostapd_config_free(conf);
2194 return NULL;
2195 }
2196
2197 tmp_conf = os_realloc_array(
2198 iface->conf->bss, iface->conf->num_bss + 1,
2199 sizeof(struct hostapd_bss_config *));
2200 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
2201 sizeof(struct hostapd_data *));
2202 if (tmp_bss)
2203 iface->bss = tmp_bss;
2204 if (tmp_conf) {
2205 iface->conf->bss = tmp_conf;
2206 iface->conf->last_bss = tmp_conf[0];
2207 }
2208 if (tmp_bss == NULL || tmp_conf == NULL) {
2209 hostapd_config_free(conf);
2210 return NULL;
2211 }
2212 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
2213 iface->conf->num_bss++;
2214
2215 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
2216 if (hapd == NULL) {
2217 iface->conf->num_bss--;
2218 hostapd_config_free(conf);
2219 return NULL;
2220 }
2221 iface->conf->last_bss = bss;
2222 iface->bss[iface->num_bss] = hapd;
2223 hapd->msg_ctx = hapd;
2224
2225 bss_idx = iface->num_bss++;
2226 conf->num_bss--;
2227 conf->bss[0] = NULL;
2228 hostapd_config_free(conf);
2229 } else {
2230 /* Add a new iface with the first BSS */
2231 new_iface = iface = hostapd_init(interfaces, config_fname);
2232 if (!iface)
2233 return NULL;
2234 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
2235 iface->interfaces = interfaces;
2236 bss_idx = 0;
2237 }
2238
2239 for (k = 0; k < debug; k++) {
2240 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
2241 iface->bss[bss_idx]->conf->logger_stdout_level--;
2242 }
2243
2244 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
2245 !hostapd_drv_none(iface->bss[bss_idx])) {
2246 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
2247 config_fname);
2248 if (new_iface)
2249 hostapd_interface_deinit_free(new_iface);
2250 return NULL;
2251 }
2252
2253 return iface;
2254}
2255
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002256
2257void hostapd_interface_deinit_free(struct hostapd_iface *iface)
2258{
2259 const struct wpa_driver_ops *driver;
2260 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002261
2262 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002263 if (iface == NULL)
2264 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002265 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
2266 __func__, (unsigned int) iface->num_bss,
2267 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002268 driver = iface->bss[0]->driver;
2269 drv_priv = iface->bss[0]->drv_priv;
2270 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08002271 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2272 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002273 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002274 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07002275 iface->bss[0]->drv_priv = NULL;
2276 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002277 hostapd_interface_free(iface);
2278}
2279
2280
Dmitry Shmidt15907092014-03-25 10:42:57 -07002281static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
2282 void *drv_priv,
2283 struct hostapd_iface *hapd_iface)
2284{
2285 size_t j;
2286
2287 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
2288 __func__, driver, drv_priv);
2289 if (driver && driver->hapd_deinit && drv_priv) {
2290 driver->hapd_deinit(drv_priv);
2291 for (j = 0; j < hapd_iface->num_bss; j++) {
2292 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
2293 __func__, (int) j,
2294 hapd_iface->bss[j]->drv_priv);
2295 if (hapd_iface->bss[j]->drv_priv == drv_priv)
2296 hapd_iface->bss[j]->drv_priv = NULL;
2297 }
2298 }
2299}
2300
2301
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002302int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
2303{
Dmitry Shmidt71757432014-06-02 13:50:35 -07002304 size_t j;
2305
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002306 if (hapd_iface->bss[0]->drv_priv != NULL) {
2307 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002308 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002309 return -1;
2310 }
2311
2312 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002313 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002314
Dmitry Shmidt71757432014-06-02 13:50:35 -07002315 for (j = 0; j < hapd_iface->num_bss; j++)
2316 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002317 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
2318 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
2319 return -1;
2320 }
2321
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002322 if (hapd_iface->interfaces == NULL ||
2323 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002324 hapd_iface->interfaces->driver_init(hapd_iface))
2325 return -1;
2326
2327 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07002328 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
2329 hapd_iface->bss[0]->drv_priv,
2330 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002331 return -1;
2332 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002333
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002334 return 0;
2335}
2336
2337
2338int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
2339{
2340 size_t j;
2341
2342 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002343 hapd_iface->conf->bss[0]->iface);
2344 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07002345 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002346 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002347 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
2348 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002349 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002350 hostapd_clear_old(hapd_iface);
2351 for (j = 0; j < hapd_iface->num_bss; j++)
2352 hostapd_reload_bss(hapd_iface->bss[j]);
2353
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002354 return 0;
2355}
2356
2357
2358int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
2359{
2360 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002361 const struct wpa_driver_ops *driver;
2362 void *drv_priv;
2363
2364 if (hapd_iface == NULL)
2365 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002366
2367 if (hapd_iface->bss[0]->drv_priv == NULL) {
2368 wpa_printf(MSG_INFO, "Interface %s already disabled",
2369 hapd_iface->conf->bss[0]->iface);
2370 return -1;
2371 }
2372
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002373 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002374 driver = hapd_iface->bss[0]->driver;
2375 drv_priv = hapd_iface->bss[0]->drv_priv;
2376
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002377 hapd_iface->driver_ap_teardown =
2378 !!(hapd_iface->drv_flags &
2379 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2380
2381 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002382 for (j = 0; j < hapd_iface->num_bss; j++) {
2383 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07002384 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002385 hostapd_free_hapd_data(hapd);
2386 }
2387
Dmitry Shmidt15907092014-03-25 10:42:57 -07002388 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002389
2390 /* From hostapd_cleanup_iface: These were initialized in
2391 * hostapd_setup_interface and hostapd_setup_interface_complete
2392 */
2393 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002394
Dmitry Shmidt56052862013-10-04 10:23:25 -07002395 wpa_printf(MSG_DEBUG, "Interface %s disabled",
2396 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002397 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002398 return 0;
2399}
2400
2401
2402static struct hostapd_iface *
2403hostapd_iface_alloc(struct hapd_interfaces *interfaces)
2404{
2405 struct hostapd_iface **iface, *hapd_iface;
2406
2407 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
2408 sizeof(struct hostapd_iface *));
2409 if (iface == NULL)
2410 return NULL;
2411 interfaces->iface = iface;
2412 hapd_iface = interfaces->iface[interfaces->count] =
2413 os_zalloc(sizeof(*hapd_iface));
2414 if (hapd_iface == NULL) {
2415 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2416 "the interface", __func__);
2417 return NULL;
2418 }
2419 interfaces->count++;
2420 hapd_iface->interfaces = interfaces;
2421
2422 return hapd_iface;
2423}
2424
2425
2426static struct hostapd_config *
2427hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002428 const char *ctrl_iface, const char *driver)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002429{
2430 struct hostapd_bss_config *bss;
2431 struct hostapd_config *conf;
2432
2433 /* Allocates memory for bss and conf */
2434 conf = hostapd_config_defaults();
2435 if (conf == NULL) {
2436 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2437 "configuration", __func__);
2438 return NULL;
2439 }
2440
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002441 if (driver) {
2442 int j;
2443
2444 for (j = 0; wpa_drivers[j]; j++) {
2445 if (os_strcmp(driver, wpa_drivers[j]->name) == 0) {
2446 conf->driver = wpa_drivers[j];
2447 goto skip;
2448 }
2449 }
2450
2451 wpa_printf(MSG_ERROR,
2452 "Invalid/unknown driver '%s' - registering the default driver",
2453 driver);
2454 }
2455
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002456 conf->driver = wpa_drivers[0];
2457 if (conf->driver == NULL) {
2458 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2459 hostapd_config_free(conf);
2460 return NULL;
2461 }
2462
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002463skip:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002464 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002465
2466 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2467 bss->ctrl_interface = os_strdup(ctrl_iface);
2468 if (bss->ctrl_interface == NULL) {
2469 hostapd_config_free(conf);
2470 return NULL;
2471 }
2472
2473 /* Reading configuration file skipped, will be done in SET!
2474 * From reading the configuration till the end has to be done in
2475 * SET
2476 */
2477 return conf;
2478}
2479
2480
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002481static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2482 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002483{
2484 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002485 struct hostapd_data *hapd;
2486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002487 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002488 sizeof(struct hostapd_data *));
2489 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002490 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002491
2492 for (i = 0; i < conf->num_bss; i++) {
2493 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002494 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002495 if (hapd == NULL) {
2496 while (i > 0) {
2497 i--;
2498 os_free(hapd_iface->bss[i]);
2499 hapd_iface->bss[i] = NULL;
2500 }
2501 os_free(hapd_iface->bss);
2502 hapd_iface->bss = NULL;
2503 return -1;
2504 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002505 hapd->msg_ctx = hapd;
2506 }
2507
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002508 hapd_iface->conf = conf;
2509 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002510
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002511 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002512}
2513
2514
2515int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2516{
2517 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002518 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2519 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002520 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002521 size_t i, j;
2522 const char *conf_file = NULL, *phy_name = NULL;
2523
2524 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2525 char *pos;
2526 phy_name = buf + 11;
2527 pos = os_strchr(phy_name, ':');
2528 if (!pos)
2529 return -1;
2530 *pos++ = '\0';
2531 conf_file = pos;
2532 if (!os_strlen(conf_file))
2533 return -1;
2534
2535 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2536 conf_file, 0);
2537 if (!hapd_iface)
2538 return -1;
2539 for (j = 0; j < interfaces->count; j++) {
2540 if (interfaces->iface[j] == hapd_iface)
2541 break;
2542 }
2543 if (j == interfaces->count) {
2544 struct hostapd_iface **tmp;
2545 tmp = os_realloc_array(interfaces->iface,
2546 interfaces->count + 1,
2547 sizeof(struct hostapd_iface *));
2548 if (!tmp) {
2549 hostapd_interface_deinit_free(hapd_iface);
2550 return -1;
2551 }
2552 interfaces->iface = tmp;
2553 interfaces->iface[interfaces->count++] = hapd_iface;
2554 new_iface = hapd_iface;
2555 }
2556
2557 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002558 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002559 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002560
2561 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002562 hostapd_deinit_driver(
2563 hapd_iface->bss[0]->driver,
2564 hapd_iface->bss[0]->drv_priv,
2565 hapd_iface);
2566 goto fail;
2567 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002568 } else {
2569 /* Assign new BSS with bss[0]'s driver info */
2570 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2571 hapd->driver = hapd_iface->bss[0]->driver;
2572 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2573 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2574 ETH_ALEN);
2575
2576 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002577 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2578 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002579 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002580 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002581 hapd_iface->conf->num_bss--;
2582 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002583 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2584 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002585 hostapd_config_free_bss(hapd->conf);
2586 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002587 os_free(hapd);
2588 return -1;
2589 }
2590 }
2591 return 0;
2592 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002593
2594 ptr = os_strchr(buf, ' ');
2595 if (ptr == NULL)
2596 return -1;
2597 *ptr++ = '\0';
2598
Dmitry Shmidt56052862013-10-04 10:23:25 -07002599 if (os_strncmp(ptr, "config=", 7) == 0)
2600 conf_file = ptr + 7;
2601
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002602 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002603 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002604 buf)) {
2605 wpa_printf(MSG_INFO, "Cannot add interface - it "
2606 "already exists");
2607 return -1;
2608 }
2609 }
2610
2611 hapd_iface = hostapd_iface_alloc(interfaces);
2612 if (hapd_iface == NULL) {
2613 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2614 "for interface", __func__);
2615 goto fail;
2616 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002617 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002618
Dmitry Shmidt56052862013-10-04 10:23:25 -07002619 if (conf_file && interfaces->config_read_cb) {
2620 conf = interfaces->config_read_cb(conf_file);
2621 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002622 os_strlcpy(conf->bss[0]->iface, buf,
2623 sizeof(conf->bss[0]->iface));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002624 } else {
2625 char *driver = os_strchr(ptr, ' ');
2626
2627 if (driver)
2628 *driver++ = '\0';
2629 conf = hostapd_config_alloc(interfaces, buf, ptr, driver);
2630 }
2631
Dmitry Shmidt56052862013-10-04 10:23:25 -07002632 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002633 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2634 "for configuration", __func__);
2635 goto fail;
2636 }
2637
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002638 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002639 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2640 "for hostapd", __func__);
2641 goto fail;
2642 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002643 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002644
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002645 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002646 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002647
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002648 wpa_printf(MSG_INFO, "Add interface '%s'",
2649 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002650
2651 return 0;
2652
2653fail:
2654 if (conf)
2655 hostapd_config_free(conf);
2656 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002657 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002658 for (i = 0; i < hapd_iface->num_bss; i++) {
2659 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002660 if (!hapd)
2661 continue;
2662 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002663 hapd_iface->interfaces->ctrl_iface_deinit)
2664 hapd_iface->interfaces->
2665 ctrl_iface_deinit(hapd);
2666 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2667 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002668 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002669 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002670 os_free(hapd);
2671 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002672 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002673 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002674 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002675 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002676 if (new_iface) {
2677 interfaces->count--;
2678 interfaces->iface[interfaces->count] = NULL;
2679 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002680 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002681 }
2682 return -1;
2683}
2684
2685
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002686static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2687{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002688 size_t i;
2689
Dmitry Shmidt54605472013-11-08 11:10:19 -08002690 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002691
Dmitry Shmidt54605472013-11-08 11:10:19 -08002692 /* Remove hostapd_data only if it has already been initialized */
2693 if (idx < iface->num_bss) {
2694 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002695
Dmitry Shmidt54605472013-11-08 11:10:19 -08002696 hostapd_bss_deinit(hapd);
2697 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2698 __func__, hapd, hapd->conf->iface);
2699 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002700 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002701 os_free(hapd);
2702
2703 iface->num_bss--;
2704
2705 for (i = idx; i < iface->num_bss; i++)
2706 iface->bss[i] = iface->bss[i + 1];
2707 } else {
2708 hostapd_config_free_bss(iface->conf->bss[idx]);
2709 iface->conf->bss[idx] = NULL;
2710 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002711
2712 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002713 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002714 iface->conf->bss[i] = iface->conf->bss[i + 1];
2715
2716 return 0;
2717}
2718
2719
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2721{
2722 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002723 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002724
2725 for (i = 0; i < interfaces->count; i++) {
2726 hapd_iface = interfaces->iface[i];
2727 if (hapd_iface == NULL)
2728 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002729 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002731 hapd_iface->driver_ap_teardown =
2732 !!(hapd_iface->drv_flags &
2733 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2734
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002735 hostapd_interface_deinit_free(hapd_iface);
2736 k = i;
2737 while (k < (interfaces->count - 1)) {
2738 interfaces->iface[k] =
2739 interfaces->iface[k + 1];
2740 k++;
2741 }
2742 interfaces->count--;
2743 return 0;
2744 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002745
2746 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002747 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2748 hapd_iface->driver_ap_teardown =
2749 !(hapd_iface->drv_flags &
2750 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002751 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002752 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002753 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002754 }
2755 return -1;
2756}
2757
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759/**
2760 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2761 * @hapd: Pointer to BSS data
2762 * @sta: Pointer to the associated STA data
2763 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2764 *
2765 * This function will be called whenever a station associates with the AP. It
2766 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2767 * from drv_callbacks.c based on driver events for drivers that take care of
2768 * management frames (IEEE 802.11 authentication and association) internally.
2769 */
2770void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2771 int reassoc)
2772{
2773 if (hapd->tkip_countermeasures) {
2774 hostapd_drv_sta_deauth(hapd, sta->addr,
2775 WLAN_REASON_MICHAEL_MIC_FAILURE);
2776 return;
2777 }
2778
2779 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002780 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781
2782 /* IEEE 802.11F (IAPP) */
2783 if (hapd->conf->ieee802_11f)
2784 iapp_new_station(hapd->iapp, sta);
2785
2786#ifdef CONFIG_P2P
2787 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2788 sta->no_p2p_set = 1;
2789 hapd->num_sta_no_p2p++;
2790 if (hapd->num_sta_no_p2p == 1)
2791 hostapd_p2p_non_p2p_sta_connected(hapd);
2792 }
2793#endif /* CONFIG_P2P */
2794
2795 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2796 * IEEE 802.1X/WPA code will start accounting after the station has
2797 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002798 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2799 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002800 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002802 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803
2804 /* Start IEEE 802.1X authentication process for new stations */
2805 ieee802_1x_new_station(hapd, sta);
2806 if (reassoc) {
2807 if (sta->auth_alg != WLAN_AUTH_FT &&
2808 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2809 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2810 } else
2811 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002812
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002813 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002814 wpa_printf(MSG_DEBUG,
2815 "%s: %s: reschedule ap_handle_timer timeout for "
2816 MACSTR " (%d seconds - ap_max_inactivity)",
2817 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002818 hapd->conf->ap_max_inactivity);
2819 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2820 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2821 ap_handle_timer, hapd, sta);
2822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002824
2825
2826const char * hostapd_state_text(enum hostapd_iface_state s)
2827{
2828 switch (s) {
2829 case HAPD_IFACE_UNINITIALIZED:
2830 return "UNINITIALIZED";
2831 case HAPD_IFACE_DISABLED:
2832 return "DISABLED";
2833 case HAPD_IFACE_COUNTRY_UPDATE:
2834 return "COUNTRY_UPDATE";
2835 case HAPD_IFACE_ACS:
2836 return "ACS";
2837 case HAPD_IFACE_HT_SCAN:
2838 return "HT_SCAN";
2839 case HAPD_IFACE_DFS:
2840 return "DFS";
2841 case HAPD_IFACE_ENABLED:
2842 return "ENABLED";
2843 }
2844
2845 return "UNKNOWN";
2846}
2847
2848
2849void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2850{
2851 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2852 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2853 hostapd_state_text(s));
2854 iface->state = s;
2855}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002856
2857
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002858int hostapd_csa_in_progress(struct hostapd_iface *iface)
2859{
2860 unsigned int i;
2861
2862 for (i = 0; i < iface->num_bss; i++)
2863 if (iface->bss[i]->csa_in_progress)
2864 return 1;
2865 return 0;
2866}
2867
2868
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002869#ifdef NEED_AP_MLME
2870
2871static void free_beacon_data(struct beacon_data *beacon)
2872{
2873 os_free(beacon->head);
2874 beacon->head = NULL;
2875 os_free(beacon->tail);
2876 beacon->tail = NULL;
2877 os_free(beacon->probe_resp);
2878 beacon->probe_resp = NULL;
2879 os_free(beacon->beacon_ies);
2880 beacon->beacon_ies = NULL;
2881 os_free(beacon->proberesp_ies);
2882 beacon->proberesp_ies = NULL;
2883 os_free(beacon->assocresp_ies);
2884 beacon->assocresp_ies = NULL;
2885}
2886
2887
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002888static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002889 struct beacon_data *beacon)
2890{
2891 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2892 struct wpa_driver_ap_params params;
2893 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002894
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002895 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002896 ret = ieee802_11_build_ap_params(hapd, &params);
2897 if (ret < 0)
2898 return ret;
2899
2900 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2901 &proberesp_extra,
2902 &assocresp_extra);
2903 if (ret)
2904 goto free_ap_params;
2905
2906 ret = -1;
2907 beacon->head = os_malloc(params.head_len);
2908 if (!beacon->head)
2909 goto free_ap_extra_ies;
2910
2911 os_memcpy(beacon->head, params.head, params.head_len);
2912 beacon->head_len = params.head_len;
2913
2914 beacon->tail = os_malloc(params.tail_len);
2915 if (!beacon->tail)
2916 goto free_beacon;
2917
2918 os_memcpy(beacon->tail, params.tail, params.tail_len);
2919 beacon->tail_len = params.tail_len;
2920
2921 if (params.proberesp != NULL) {
2922 beacon->probe_resp = os_malloc(params.proberesp_len);
2923 if (!beacon->probe_resp)
2924 goto free_beacon;
2925
2926 os_memcpy(beacon->probe_resp, params.proberesp,
2927 params.proberesp_len);
2928 beacon->probe_resp_len = params.proberesp_len;
2929 }
2930
2931 /* copy the extra ies */
2932 if (beacon_extra) {
2933 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2934 if (!beacon->beacon_ies)
2935 goto free_beacon;
2936
2937 os_memcpy(beacon->beacon_ies,
2938 beacon_extra->buf, wpabuf_len(beacon_extra));
2939 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2940 }
2941
2942 if (proberesp_extra) {
2943 beacon->proberesp_ies =
2944 os_malloc(wpabuf_len(proberesp_extra));
2945 if (!beacon->proberesp_ies)
2946 goto free_beacon;
2947
2948 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2949 wpabuf_len(proberesp_extra));
2950 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2951 }
2952
2953 if (assocresp_extra) {
2954 beacon->assocresp_ies =
2955 os_malloc(wpabuf_len(assocresp_extra));
2956 if (!beacon->assocresp_ies)
2957 goto free_beacon;
2958
2959 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2960 wpabuf_len(assocresp_extra));
2961 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2962 }
2963
2964 ret = 0;
2965free_beacon:
2966 /* if the function fails, the caller should not free beacon data */
2967 if (ret)
2968 free_beacon_data(beacon);
2969
2970free_ap_extra_ies:
2971 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2972 assocresp_extra);
2973free_ap_params:
2974 ieee802_11_free_ap_params(&params);
2975 return ret;
2976}
2977
2978
2979/*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002980 * TODO: This flow currently supports only changing channel and width within
2981 * the same hw_mode. Any other changes to MAC parameters or provided settings
2982 * are not supported.
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002983 */
2984static int hostapd_change_config_freq(struct hostapd_data *hapd,
2985 struct hostapd_config *conf,
2986 struct hostapd_freq_params *params,
2987 struct hostapd_freq_params *old_params)
2988{
2989 int channel;
2990
2991 if (!params->channel) {
2992 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002993 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002994 }
2995
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002996 channel = params->channel;
2997 if (!channel)
2998 return -1;
2999
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003000 /* if a pointer to old_params is provided we save previous state */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003001 if (old_params &&
3002 hostapd_set_freq_params(old_params, conf->hw_mode,
3003 hostapd_hw_get_freq(hapd, conf->channel),
3004 conf->channel, conf->ieee80211n,
3005 conf->ieee80211ac,
3006 conf->secondary_channel,
3007 conf->vht_oper_chwidth,
3008 conf->vht_oper_centr_freq_seg0_idx,
3009 conf->vht_oper_centr_freq_seg1_idx,
3010 conf->vht_capab))
3011 return -1;
3012
3013 switch (params->bandwidth) {
3014 case 0:
3015 case 20:
3016 case 40:
3017 conf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
3018 break;
3019 case 80:
3020 if (params->center_freq2)
3021 conf->vht_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
3022 else
3023 conf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
3024 break;
3025 case 160:
3026 conf->vht_oper_chwidth = VHT_CHANWIDTH_160MHZ;
3027 break;
3028 default:
3029 return -1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003030 }
3031
3032 conf->channel = channel;
3033 conf->ieee80211n = params->ht_enabled;
3034 conf->secondary_channel = params->sec_channel_offset;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003035 ieee80211_freq_to_chan(params->center_freq1,
3036 &conf->vht_oper_centr_freq_seg0_idx);
3037 ieee80211_freq_to_chan(params->center_freq2,
3038 &conf->vht_oper_centr_freq_seg1_idx);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003039
3040 /* TODO: maybe call here hostapd_config_check here? */
3041
3042 return 0;
3043}
3044
3045
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003046static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003047 struct csa_settings *settings)
3048{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003049 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003050 struct hostapd_freq_params old_freq;
3051 int ret;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003052 u8 chan, vht_bandwidth;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003053
3054 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003055 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003056 return -1;
3057
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003058 switch (settings->freq_params.bandwidth) {
3059 case 80:
3060 if (settings->freq_params.center_freq2)
3061 vht_bandwidth = VHT_CHANWIDTH_80P80MHZ;
3062 else
3063 vht_bandwidth = VHT_CHANWIDTH_80MHZ;
3064 break;
3065 case 160:
3066 vht_bandwidth = VHT_CHANWIDTH_160MHZ;
3067 break;
3068 default:
3069 vht_bandwidth = VHT_CHANWIDTH_USE_HT;
3070 break;
3071 }
3072
3073 if (ieee80211_freq_to_channel_ext(
3074 settings->freq_params.freq,
3075 settings->freq_params.sec_channel_offset,
3076 vht_bandwidth,
3077 &hapd->iface->cs_oper_class,
3078 &chan) == NUM_HOSTAPD_MODES) {
3079 wpa_printf(MSG_DEBUG,
3080 "invalid frequency for channel switch (freq=%d, sec_channel_offset=%d, vht_enabled=%d)",
3081 settings->freq_params.freq,
3082 settings->freq_params.sec_channel_offset,
3083 settings->freq_params.vht_enabled);
3084 return -1;
3085 }
3086
3087 settings->freq_params.channel = chan;
3088
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003089 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
3090 &settings->freq_params,
3091 &old_freq);
3092 if (ret)
3093 return ret;
3094
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003095 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003096
3097 /* change back the configuration */
3098 hostapd_change_config_freq(iface->bss[0], iface->conf,
3099 &old_freq, NULL);
3100
3101 if (ret)
3102 return ret;
3103
3104 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003105 hapd->cs_freq_params = settings->freq_params;
3106 hapd->cs_count = settings->cs_count;
3107 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003108
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003109 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003110 if (ret) {
3111 free_beacon_data(&settings->beacon_after);
3112 return ret;
3113 }
3114
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003115 settings->counter_offset_beacon[0] = hapd->cs_c_off_beacon;
3116 settings->counter_offset_presp[0] = hapd->cs_c_off_proberesp;
3117 settings->counter_offset_beacon[1] = hapd->cs_c_off_ecsa_beacon;
3118 settings->counter_offset_presp[1] = hapd->cs_c_off_ecsa_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003119
3120 return 0;
3121}
3122
3123
3124void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
3125{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003126 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
3127 hapd->cs_count = 0;
3128 hapd->cs_block_tx = 0;
3129 hapd->cs_c_off_beacon = 0;
3130 hapd->cs_c_off_proberesp = 0;
3131 hapd->csa_in_progress = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003132 hapd->cs_c_off_ecsa_beacon = 0;
3133 hapd->cs_c_off_ecsa_proberesp = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003134}
3135
3136
3137int hostapd_switch_channel(struct hostapd_data *hapd,
3138 struct csa_settings *settings)
3139{
3140 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003141
3142 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
3143 wpa_printf(MSG_INFO, "CSA is not supported");
3144 return -1;
3145 }
3146
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003147 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003148 if (ret)
3149 return ret;
3150
3151 ret = hostapd_drv_switch_channel(hapd, settings);
3152 free_beacon_data(&settings->beacon_csa);
3153 free_beacon_data(&settings->beacon_after);
3154
3155 if (ret) {
3156 /* if we failed, clean cs parameters */
3157 hostapd_cleanup_cs_params(hapd);
3158 return ret;
3159 }
3160
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003161 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003162 return 0;
3163}
3164
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003165
3166void
3167hostapd_switch_channel_fallback(struct hostapd_iface *iface,
3168 const struct hostapd_freq_params *freq_params)
3169{
3170 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
3171 unsigned int i;
3172
3173 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
3174
3175 if (freq_params->center_freq1)
3176 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
3177 if (freq_params->center_freq2)
3178 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
3179
3180 switch (freq_params->bandwidth) {
3181 case 0:
3182 case 20:
3183 case 40:
3184 vht_bw = VHT_CHANWIDTH_USE_HT;
3185 break;
3186 case 80:
3187 if (freq_params->center_freq2)
3188 vht_bw = VHT_CHANWIDTH_80P80MHZ;
3189 else
3190 vht_bw = VHT_CHANWIDTH_80MHZ;
3191 break;
3192 case 160:
3193 vht_bw = VHT_CHANWIDTH_160MHZ;
3194 break;
3195 default:
3196 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
3197 freq_params->bandwidth);
3198 break;
3199 }
3200
3201 iface->freq = freq_params->freq;
3202 iface->conf->channel = freq_params->channel;
3203 iface->conf->secondary_channel = freq_params->sec_channel_offset;
3204 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
3205 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
3206 iface->conf->vht_oper_chwidth = vht_bw;
3207 iface->conf->ieee80211n = freq_params->ht_enabled;
3208 iface->conf->ieee80211ac = freq_params->vht_enabled;
3209
3210 /*
3211 * cs_params must not be cleared earlier because the freq_params
3212 * argument may actually point to one of these.
3213 */
3214 for (i = 0; i < iface->num_bss; i++)
3215 hostapd_cleanup_cs_params(iface->bss[i]);
3216
3217 hostapd_disable_iface(iface);
3218 hostapd_enable_iface(iface);
3219}
3220
Dmitry Shmidte4663042016-04-04 10:07:49 -07003221#endif /* NEED_AP_MLME */
3222
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003223
3224struct hostapd_data * hostapd_get_iface(struct hapd_interfaces *interfaces,
3225 const char *ifname)
3226{
3227 size_t i, j;
3228
3229 for (i = 0; i < interfaces->count; i++) {
3230 struct hostapd_iface *iface = interfaces->iface[i];
3231
3232 for (j = 0; j < iface->num_bss; j++) {
3233 struct hostapd_data *hapd = iface->bss[j];
3234
3235 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3236 return hapd;
3237 }
3238 }
3239
3240 return NULL;
3241}
3242
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003243
3244void hostapd_periodic_iface(struct hostapd_iface *iface)
3245{
3246 size_t i;
3247
3248 ap_list_timer(iface);
3249
3250 for (i = 0; i < iface->num_bss; i++) {
3251 struct hostapd_data *hapd = iface->bss[i];
3252
3253 if (!hapd->started)
3254 continue;
3255
3256#ifndef CONFIG_NO_RADIUS
3257 hostapd_acl_expire(hapd);
3258#endif /* CONFIG_NO_RADIUS */
3259 }
3260}