blob: b641503c7360eec018ce3898cbfeee6254bf3df5 [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 Shmidt8d520ff2011-05-09 14:06:53 -070015#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070016#include "radius/radius_das.h"
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070017#include "eap_server/tncs.h"
Dmitry Shmidt2f74e362015-01-21 13:19:05 -080018#include "eapol_auth/eapol_auth_sm.h"
19#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "hostapd.h"
21#include "authsrv.h"
22#include "sta_info.h"
23#include "accounting.h"
24#include "ap_list.h"
25#include "beacon.h"
26#include "iapp.h"
27#include "ieee802_1x.h"
28#include "ieee802_11_auth.h"
29#include "vlan_init.h"
30#include "wpa_auth.h"
31#include "wps_hostapd.h"
32#include "hw_features.h"
33#include "wpa_auth_glue.h"
34#include "ap_drv_ops.h"
35#include "ap_config.h"
36#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070037#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070038#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070039#include "ieee802_11.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080040#include "bss_load.h"
41#include "x_snoop.h"
42#include "dhcp_snoop.h"
43#include "ndisc_snoop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45
Dmitry Shmidt04949592012-07-19 12:16:46 -070046static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070048static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080049static int setup_interface2(struct hostapd_iface *iface);
50static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
Dmitry Shmidt04949592012-07-19 12:16:46 -070053int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
54 int (*cb)(struct hostapd_iface *iface,
55 void *ctx), void *ctx)
56{
57 size_t i;
58 int ret;
59
60 for (i = 0; i < interfaces->count; i++) {
61 ret = cb(interfaces->iface[i], ctx);
62 if (ret)
63 return ret;
64 }
65
66 return 0;
67}
68
69
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070static void hostapd_reload_bss(struct hostapd_data *hapd)
71{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080072 struct hostapd_ssid *ssid;
73
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074#ifndef CONFIG_NO_RADIUS
75 radius_client_reconfig(hapd->radius, hapd->conf->radius);
76#endif /* CONFIG_NO_RADIUS */
77
Dmitry Shmidtcce06662013-11-04 18:44:24 -080078 ssid = &hapd->conf->ssid;
79 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
80 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
81 /*
82 * Force PSK to be derived again since SSID or passphrase may
83 * have changed.
84 */
85 os_free(ssid->wpa_psk);
86 ssid->wpa_psk = NULL;
87 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 if (hostapd_setup_wpa_psk(hapd->conf)) {
89 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
90 "after reloading configuration");
91 }
92
93 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
94 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
95 else
96 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
97
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080098 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800100 if (hapd->wpa_auth)
101 wpa_init_keys(hapd->wpa_auth);
102 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 const u8 *wpa_ie;
104 size_t wpa_ie_len;
105 hostapd_reconfig_wpa(hapd);
106 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
107 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
108 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
109 "the kernel driver.");
110 } else if (hapd->wpa_auth) {
111 wpa_deinit(hapd->wpa_auth);
112 hapd->wpa_auth = NULL;
113 hostapd_set_privacy(hapd, 0);
114 hostapd_setup_encryption(hapd->conf->iface, hapd);
115 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
116 }
117
118 ieee802_11_set_beacon(hapd);
119 hostapd_update_wps(hapd);
120
121 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700122 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 hapd->conf->ssid.ssid_len)) {
124 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
125 /* try to continue */
126 }
127 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
128}
129
130
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700131static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 size_t j;
134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 /*
136 * Deauthenticate all stations since the new configuration may not
137 * allow them to use the BSS anymore.
138 */
139 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700140 hostapd_flush_old_stations(iface->bss[j],
141 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700142 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143
144#ifndef CONFIG_NO_RADIUS
145 /* TODO: update dynamic data based on changed configuration
146 * items (e.g., open/close sockets, etc.) */
147 radius_client_flush(iface->bss[j]->radius, 0);
148#endif /* CONFIG_NO_RADIUS */
149 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700150}
151
152
153int hostapd_reload_config(struct hostapd_iface *iface)
154{
155 struct hostapd_data *hapd = iface->bss[0];
156 struct hostapd_config *newconf, *oldconf;
157 size_t j;
158
159 if (iface->config_fname == NULL) {
160 /* Only in-memory config in use - assume it has been updated */
161 hostapd_clear_old(iface);
162 for (j = 0; j < iface->num_bss; j++)
163 hostapd_reload_bss(iface->bss[j]);
164 return 0;
165 }
166
167 if (iface->interfaces == NULL ||
168 iface->interfaces->config_read_cb == NULL)
169 return -1;
170 newconf = iface->interfaces->config_read_cb(iface->config_fname);
171 if (newconf == NULL)
172 return -1;
173
174 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175
176 oldconf = hapd->iconf;
177 iface->conf = newconf;
178
179 for (j = 0; j < iface->num_bss; j++) {
180 hapd = iface->bss[j];
181 hapd->iconf = newconf;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700182 hapd->iconf->channel = oldconf->channel;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700183 hapd->iconf->secondary_channel = oldconf->secondary_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700184 hapd->iconf->ieee80211n = oldconf->ieee80211n;
185 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
186 hapd->iconf->ht_capab = oldconf->ht_capab;
187 hapd->iconf->vht_capab = oldconf->vht_capab;
188 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
189 hapd->iconf->vht_oper_centr_freq_seg0_idx =
190 oldconf->vht_oper_centr_freq_seg0_idx;
191 hapd->iconf->vht_oper_centr_freq_seg1_idx =
192 oldconf->vht_oper_centr_freq_seg1_idx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800193 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 hostapd_reload_bss(hapd);
195 }
196
197 hostapd_config_free(oldconf);
198
199
200 return 0;
201}
202
203
204static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
205 char *ifname)
206{
207 int i;
208
209 for (i = 0; i < NUM_WEP_KEYS; i++) {
210 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
211 0, NULL, 0, NULL, 0)) {
212 wpa_printf(MSG_DEBUG, "Failed to clear default "
213 "encryption keys (ifname=%s keyidx=%d)",
214 ifname, i);
215 }
216 }
217#ifdef CONFIG_IEEE80211W
218 if (hapd->conf->ieee80211w) {
219 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
220 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
221 NULL, i, 0, NULL,
222 0, NULL, 0)) {
223 wpa_printf(MSG_DEBUG, "Failed to clear "
224 "default mgmt encryption keys "
225 "(ifname=%s keyidx=%d)", ifname, i);
226 }
227 }
228 }
229#endif /* CONFIG_IEEE80211W */
230}
231
232
233static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
234{
235 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
236 return 0;
237}
238
239
240static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
241{
242 int errors = 0, idx;
243 struct hostapd_ssid *ssid = &hapd->conf->ssid;
244
245 idx = ssid->wep.idx;
246 if (ssid->wep.default_len &&
247 hostapd_drv_set_key(hapd->conf->iface,
248 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
249 1, NULL, 0, ssid->wep.key[idx],
250 ssid->wep.len[idx])) {
251 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
252 errors++;
253 }
254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 return errors;
256}
257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258
Dmitry Shmidt04949592012-07-19 12:16:46 -0700259static void hostapd_free_hapd_data(struct hostapd_data *hapd)
260{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800261 os_free(hapd->probereq_cb);
262 hapd->probereq_cb = NULL;
263
264#ifdef CONFIG_P2P
265 wpabuf_free(hapd->p2p_beacon_ie);
266 hapd->p2p_beacon_ie = NULL;
267 wpabuf_free(hapd->p2p_probe_resp_ie);
268 hapd->p2p_probe_resp_ie = NULL;
269#endif /* CONFIG_P2P */
270
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800271 if (!hapd->started) {
272 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
273 __func__, hapd->conf->iface);
274 return;
275 }
276 hapd->started = 0;
277
Dmitry Shmidt54605472013-11-08 11:10:19 -0800278 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 iapp_deinit(hapd->iapp);
280 hapd->iapp = NULL;
281 accounting_deinit(hapd);
282 hostapd_deinit_wpa(hapd);
283 vlan_deinit(hapd);
284 hostapd_acl_deinit(hapd);
285#ifndef CONFIG_NO_RADIUS
286 radius_client_deinit(hapd->radius);
287 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700288 radius_das_deinit(hapd->radius_das);
289 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290#endif /* CONFIG_NO_RADIUS */
291
292 hostapd_deinit_wps(hapd);
293
294 authsrv_deinit(hapd);
295
Dmitry Shmidt71757432014-06-02 13:50:35 -0700296 if (hapd->interface_added) {
297 hapd->interface_added = 0;
298 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
299 wpa_printf(MSG_WARNING,
300 "Failed to remove BSS interface %s",
301 hapd->conf->iface);
302 hapd->interface_added = 1;
303 } else {
304 /*
305 * Since this was a dynamically added interface, the
306 * driver wrapper may have removed its internal instance
307 * and hapd->drv_priv is not valid anymore.
308 */
309 hapd->drv_priv = NULL;
310 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 }
312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700314
315#ifdef CONFIG_INTERWORKING
316 gas_serv_deinit(hapd);
317#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800319 bss_load_update_deinit(hapd);
320 ndisc_snoop_deinit(hapd);
321 dhcp_snoop_deinit(hapd);
322 x_snoop_deinit(hapd);
323
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800324#ifdef CONFIG_SQLITE
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700325 bin_clear_free(hapd->tmp_eap_user.identity,
326 hapd->tmp_eap_user.identity_len);
327 bin_clear_free(hapd->tmp_eap_user.password,
328 hapd->tmp_eap_user.password_len);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800329#endif /* CONFIG_SQLITE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800330
331#ifdef CONFIG_MESH
332 wpabuf_free(hapd->mesh_pending_auth);
333 hapd->mesh_pending_auth = NULL;
334#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335}
336
337
338/**
339 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
340 * @hapd: Pointer to BSS data
341 *
342 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800343 * Most of the modules that are initialized in hostapd_setup_bss() are
344 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700345 */
346static void hostapd_cleanup(struct hostapd_data *hapd)
347{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800348 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
349 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700350 if (hapd->iface->interfaces &&
351 hapd->iface->interfaces->ctrl_iface_deinit)
352 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354}
355
356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
358{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800359 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700360 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
361 iface->hw_features = NULL;
362 os_free(iface->current_rates);
363 iface->current_rates = NULL;
364 os_free(iface->basic_rates);
365 iface->basic_rates = NULL;
366 ap_list_deinit(iface);
367}
368
369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370/**
371 * hostapd_cleanup_iface - Complete per-interface cleanup
372 * @iface: Pointer to interface data
373 *
374 * This function is called after per-BSS data structures are deinitialized
375 * with hostapd_cleanup().
376 */
377static void hostapd_cleanup_iface(struct hostapd_iface *iface)
378{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800379 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800380 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
381
Dmitry Shmidt04949592012-07-19 12:16:46 -0700382 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 hostapd_config_free(iface->conf);
384 iface->conf = NULL;
385
386 os_free(iface->config_fname);
387 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800388 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 os_free(iface);
390}
391
392
Dmitry Shmidt04949592012-07-19 12:16:46 -0700393static void hostapd_clear_wep(struct hostapd_data *hapd)
394{
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800395 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700396 hostapd_set_privacy(hapd, 0);
397 hostapd_broadcast_wep_clear(hapd);
398 }
399}
400
401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
403{
404 int i;
405
406 hostapd_broadcast_wep_set(hapd);
407
408 if (hapd->conf->ssid.wep.default_len) {
409 hostapd_set_privacy(hapd, 1);
410 return 0;
411 }
412
Jouni Malinen75ecf522011-06-27 15:19:46 -0700413 /*
414 * When IEEE 802.1X is not enabled, the driver may need to know how to
415 * set authentication algorithms for static WEP.
416 */
417 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 for (i = 0; i < 4; i++) {
420 if (hapd->conf->ssid.wep.key[i] &&
421 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
422 i == hapd->conf->ssid.wep.idx, NULL, 0,
423 hapd->conf->ssid.wep.key[i],
424 hapd->conf->ssid.wep.len[i])) {
425 wpa_printf(MSG_WARNING, "Could not set WEP "
426 "encryption.");
427 return -1;
428 }
429 if (hapd->conf->ssid.wep.key[i] &&
430 i == hapd->conf->ssid.wep.idx)
431 hostapd_set_privacy(hapd, 1);
432 }
433
434 return 0;
435}
436
437
Dmitry Shmidt04949592012-07-19 12:16:46 -0700438static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439{
440 int ret = 0;
441 u8 addr[ETH_ALEN];
442
443 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
444 return 0;
445
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800446 if (!hapd->iface->driver_ap_teardown) {
447 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
448 "Flushing old station entries");
449
450 if (hostapd_flush(hapd)) {
451 wpa_msg(hapd->msg_ctx, MSG_WARNING,
452 "Could not connect to kernel driver");
453 ret = -1;
454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800456 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 hostapd_free_stas(hapd);
460
461 return ret;
462}
463
464
Dmitry Shmidt71757432014-06-02 13:50:35 -0700465static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
466{
467 hostapd_free_stas(hapd);
468 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
469 hostapd_clear_wep(hapd);
470}
471
472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473/**
474 * hostapd_validate_bssid_configuration - Validate BSSID configuration
475 * @iface: Pointer to interface data
476 * Returns: 0 on success, -1 on failure
477 *
478 * This function is used to validate that the configured BSSIDs are valid.
479 */
480static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
481{
482 u8 mask[ETH_ALEN] = { 0 };
483 struct hostapd_data *hapd = iface->bss[0];
484 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 int auto_addr = 0;
486
487 if (hostapd_drv_none(hapd))
488 return 0;
489
490 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
491
492 /* Determine the bits necessary to cover the number of BSSIDs. */
493 for (i--; i; i >>= 1)
494 bits++;
495
496 /* Determine the bits necessary to any configured BSSIDs,
497 if they are higher than the number of BSSIDs. */
498 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800499 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 if (j)
501 auto_addr++;
502 continue;
503 }
504
505 for (i = 0; i < ETH_ALEN; i++) {
506 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800507 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 hapd->own_addr[i];
509 }
510 }
511
512 if (!auto_addr)
513 goto skip_mask_ext;
514
515 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
516 ;
517 j = 0;
518 if (i < ETH_ALEN) {
519 j = (5 - i) * 8;
520
521 while (mask[i] != 0) {
522 mask[i] >>= 1;
523 j++;
524 }
525 }
526
527 if (bits < j)
528 bits = j;
529
530 if (bits > 40) {
531 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
532 bits);
533 return -1;
534 }
535
536 os_memset(mask, 0xff, ETH_ALEN);
537 j = bits / 8;
538 for (i = 5; i > 5 - j; i--)
539 mask[i] = 0;
540 j = bits % 8;
541 while (j--)
542 mask[i] <<= 1;
543
544skip_mask_ext:
545 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
546 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 if (!auto_addr)
549 return 0;
550
551 for (i = 0; i < ETH_ALEN; i++) {
552 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
553 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
554 " for start address " MACSTR ".",
555 MAC2STR(mask), MAC2STR(hapd->own_addr));
556 wpa_printf(MSG_ERROR, "Start address must be the "
557 "first address in the block (i.e., addr "
558 "AND mask == addr).");
559 return -1;
560 }
561 }
562
563 return 0;
564}
565
566
567static int mac_in_conf(struct hostapd_config *conf, const void *a)
568{
569 size_t i;
570
571 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800572 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 return 1;
574 }
575 }
576
577 return 0;
578}
579
580
Dmitry Shmidt04949592012-07-19 12:16:46 -0700581#ifndef CONFIG_NO_RADIUS
582
583static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
584 struct radius_das_attrs *attr)
585{
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800586 if (attr->nas_identifier &&
587 (!hapd->conf->nas_identifier ||
588 os_strlen(hapd->conf->nas_identifier) !=
589 attr->nas_identifier_len ||
590 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
591 attr->nas_identifier_len) != 0)) {
592 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
593 return 1;
594 }
595
596 if (attr->nas_ip_addr &&
597 (hapd->conf->own_ip_addr.af != AF_INET ||
598 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
599 0)) {
600 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
601 return 1;
602 }
603
604#ifdef CONFIG_IPV6
605 if (attr->nas_ipv6_addr &&
606 (hapd->conf->own_ip_addr.af != AF_INET6 ||
607 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
608 != 0)) {
609 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
610 return 1;
611 }
612#endif /* CONFIG_IPV6 */
613
Dmitry Shmidt04949592012-07-19 12:16:46 -0700614 return 0;
615}
616
617
618static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800619 struct radius_das_attrs *attr,
620 int *multi)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700621{
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800622 struct sta_info *selected, *sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700623 char buf[128];
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800624 int num_attr = 0;
625 int count;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700626
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800627 *multi = 0;
628
629 for (sta = hapd->sta_list; sta; sta = sta->next)
630 sta->radius_das_match = 1;
631
632 if (attr->sta_addr) {
633 num_attr++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700634 sta = ap_get_sta(hapd, attr->sta_addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800635 if (!sta) {
636 wpa_printf(MSG_DEBUG,
637 "RADIUS DAS: No Calling-Station-Id match");
638 return NULL;
639 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700640
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800641 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700642 for (sta = hapd->sta_list; sta; sta = sta->next) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800643 if (sta != selected)
644 sta->radius_das_match = 0;
645 }
646 wpa_printf(MSG_DEBUG, "RADIUS DAS: Calling-Station-Id match");
647 }
648
649 if (attr->acct_session_id) {
650 num_attr++;
651 if (attr->acct_session_id_len != 17) {
652 wpa_printf(MSG_DEBUG,
653 "RADIUS DAS: Acct-Session-Id cannot match");
654 return NULL;
655 }
656 count = 0;
657
658 for (sta = hapd->sta_list; sta; sta = sta->next) {
659 if (!sta->radius_das_match)
660 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700661 os_snprintf(buf, sizeof(buf), "%08X-%08X",
662 sta->acct_session_id_hi,
663 sta->acct_session_id_lo);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800664 if (os_memcmp(attr->acct_session_id, buf, 17) != 0)
665 sta->radius_das_match = 0;
666 else
667 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700668 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800669
670 if (count == 0) {
671 wpa_printf(MSG_DEBUG,
672 "RADIUS DAS: No matches remaining after Acct-Session-Id check");
673 return NULL;
674 }
675 wpa_printf(MSG_DEBUG, "RADIUS DAS: Acct-Session-Id match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700676 }
677
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800678 if (attr->acct_multi_session_id) {
679 num_attr++;
680 if (attr->acct_multi_session_id_len != 17) {
681 wpa_printf(MSG_DEBUG,
682 "RADIUS DAS: Acct-Multi-Session-Id cannot match");
683 return NULL;
684 }
685 count = 0;
686
687 for (sta = hapd->sta_list; sta; sta = sta->next) {
688 if (!sta->radius_das_match)
689 continue;
690 if (!sta->eapol_sm ||
691 !sta->eapol_sm->acct_multi_session_id_hi) {
692 sta->radius_das_match = 0;
693 continue;
694 }
695 os_snprintf(buf, sizeof(buf), "%08X+%08X",
696 sta->eapol_sm->acct_multi_session_id_hi,
697 sta->eapol_sm->acct_multi_session_id_lo);
698 if (os_memcmp(attr->acct_multi_session_id, buf, 17) !=
699 0)
700 sta->radius_das_match = 0;
701 else
702 count++;
703 }
704
705 if (count == 0) {
706 wpa_printf(MSG_DEBUG,
707 "RADIUS DAS: No matches remaining after Acct-Multi-Session-Id check");
708 return NULL;
709 }
710 wpa_printf(MSG_DEBUG,
711 "RADIUS DAS: Acct-Multi-Session-Id match");
712 }
713
714 if (attr->cui) {
715 num_attr++;
716 count = 0;
717
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718 for (sta = hapd->sta_list; sta; sta = sta->next) {
719 struct wpabuf *cui;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800720
721 if (!sta->radius_das_match)
722 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700723 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800724 if (!cui || wpabuf_len(cui) != attr->cui_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700725 os_memcmp(wpabuf_head(cui), attr->cui,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800726 attr->cui_len) != 0)
727 sta->radius_das_match = 0;
728 else
729 count++;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700730 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800731
732 if (count == 0) {
733 wpa_printf(MSG_DEBUG,
734 "RADIUS DAS: No matches remaining after Chargeable-User-Identity check");
735 return NULL;
736 }
737 wpa_printf(MSG_DEBUG,
738 "RADIUS DAS: Chargeable-User-Identity match");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700739 }
740
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800741 if (attr->user_name) {
742 num_attr++;
743 count = 0;
744
Dmitry Shmidt04949592012-07-19 12:16:46 -0700745 for (sta = hapd->sta_list; sta; sta = sta->next) {
746 u8 *identity;
747 size_t identity_len;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800748
749 if (!sta->radius_das_match)
750 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700751 identity = ieee802_1x_get_identity(sta->eapol_sm,
752 &identity_len);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800753 if (!identity ||
754 identity_len != attr->user_name_len ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700755 os_memcmp(identity, attr->user_name, identity_len)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800756 != 0)
757 sta->radius_das_match = 0;
758 else
759 count++;
760 }
761
762 if (count == 0) {
763 wpa_printf(MSG_DEBUG,
764 "RADIUS DAS: No matches remaining after User-Name check");
765 return NULL;
766 }
767 wpa_printf(MSG_DEBUG,
768 "RADIUS DAS: User-Name match");
769 }
770
771 if (num_attr == 0) {
772 /*
773 * In theory, we could match all current associations, but it
774 * seems safer to just reject requests that do not include any
775 * session identification attributes.
776 */
777 wpa_printf(MSG_DEBUG,
778 "RADIUS DAS: No session identification attributes included");
779 return NULL;
780 }
781
782 selected = NULL;
783 for (sta = hapd->sta_list; sta; sta = sta->next) {
784 if (sta->radius_das_match) {
785 if (selected) {
786 *multi = 1;
787 return NULL;
788 }
789 selected = sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700790 }
791 }
792
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800793 return selected;
794}
795
796
797static int hostapd_das_disconnect_pmksa(struct hostapd_data *hapd,
798 struct radius_das_attrs *attr)
799{
800 if (!hapd->wpa_auth)
801 return -1;
802 return wpa_auth_radius_das_disconnect_pmksa(hapd->wpa_auth, attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700803}
804
805
806static enum radius_das_res
807hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
808{
809 struct hostapd_data *hapd = ctx;
810 struct sta_info *sta;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800811 int multi;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700812
813 if (hostapd_das_nas_mismatch(hapd, attr))
814 return RADIUS_DAS_NAS_MISMATCH;
815
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800816 sta = hostapd_das_find_sta(hapd, attr, &multi);
817 if (sta == NULL) {
818 if (multi) {
819 wpa_printf(MSG_DEBUG,
820 "RADIUS DAS: Multiple sessions match - not supported");
821 return RADIUS_DAS_MULTI_SESSION_MATCH;
822 }
823 if (hostapd_das_disconnect_pmksa(hapd, attr) == 0) {
824 wpa_printf(MSG_DEBUG,
825 "RADIUS DAS: PMKSA cache entry matched");
826 return RADIUS_DAS_SUCCESS;
827 }
828 wpa_printf(MSG_DEBUG, "RADIUS DAS: No matching session found");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700829 return RADIUS_DAS_SESSION_NOT_FOUND;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800830 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700831
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800832 wpa_printf(MSG_DEBUG, "RADIUS DAS: Found a matching session " MACSTR
833 " - disconnecting", MAC2STR(sta->addr));
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -0800834 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
835
Dmitry Shmidt04949592012-07-19 12:16:46 -0700836 hostapd_drv_sta_deauth(hapd, sta->addr,
837 WLAN_REASON_PREV_AUTH_NOT_VALID);
838 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
839
840 return RADIUS_DAS_SUCCESS;
841}
842
843#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844
845
846/**
847 * hostapd_setup_bss - Per-BSS setup (initialization)
848 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800849 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
850 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 *
852 * This function is used to initialize all per-BSS data structures and
853 * resources. This gets called in a loop for each BSS when an interface is
854 * initialized. Most of the modules that are initialized here will be
855 * deinitialized in hostapd_cleanup().
856 */
857static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
858{
859 struct hostapd_bss_config *conf = hapd->conf;
860 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
861 int ssid_len, set_ssid;
862 char force_ifname[IFNAMSIZ];
863 u8 if_addr[ETH_ALEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800864 int flush_old_stations = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865
Dmitry Shmidt54605472013-11-08 11:10:19 -0800866 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700867 __func__, hapd, conf->iface, first);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800868
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700869#ifdef EAP_SERVER_TNC
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700870 if (conf->tnc && tncs_global_init() < 0) {
Dmitry Shmidt50b691d2014-05-21 14:01:45 -0700871 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
872 return -1;
873 }
874#endif /* EAP_SERVER_TNC */
875
Dmitry Shmidt54605472013-11-08 11:10:19 -0800876 if (hapd->started) {
877 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700878 __func__, conf->iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800879 return -1;
880 }
881 hapd->started = 1;
882
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800883 if (!first || first == -1) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700884 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 /* Allocate the next available BSSID. */
886 do {
887 inc_byte_array(hapd->own_addr, ETH_ALEN);
888 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
889 } else {
890 /* Allocate the configured BSSID. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700891 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892
893 if (hostapd_mac_comp(hapd->own_addr,
894 hapd->iface->bss[0]->own_addr) ==
895 0) {
896 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
897 "BSSID set to the MAC address of "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700898 "the radio", conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 return -1;
900 }
901 }
902
Dmitry Shmidt54605472013-11-08 11:10:19 -0800903 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700905 conf->iface, hapd->own_addr, hapd,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 &hapd->drv_priv, force_ifname, if_addr,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700907 conf->bridge[0] ? conf->bridge : NULL,
908 first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
910 MACSTR ")", MAC2STR(hapd->own_addr));
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -0800911 hapd->interface_added = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 return -1;
913 }
914 }
915
916 if (conf->wmm_enabled < 0)
917 conf->wmm_enabled = hapd->iconf->ieee80211n;
918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800919#ifdef CONFIG_MESH
920 if (hapd->iface->mconf == NULL)
921 flush_old_stations = 0;
922#endif /* CONFIG_MESH */
923
924 if (flush_old_stations)
925 hostapd_flush_old_stations(hapd,
926 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 hostapd_set_privacy(hapd, 0);
928
929 hostapd_broadcast_wep_clear(hapd);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700930 if (hostapd_setup_encryption(conf->iface, hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 return -1;
932
933 /*
934 * Fetch the SSID from the system and use it or,
935 * if one was specified in the config file, verify they
936 * match.
937 */
938 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
939 if (ssid_len < 0) {
940 wpa_printf(MSG_ERROR, "Could not read SSID from system");
941 return -1;
942 }
943 if (conf->ssid.ssid_set) {
944 /*
945 * If SSID is specified in the config file and it differs
946 * from what is being used then force installation of the
947 * new SSID.
948 */
949 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
950 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
951 } else {
952 /*
953 * No SSID in the config file; just use the one we got
954 * from the system.
955 */
956 set_ssid = 0;
957 conf->ssid.ssid_len = ssid_len;
958 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 }
960
961 if (!hostapd_drv_none(hapd)) {
962 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 " and ssid \"%s\"",
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700964 conf->iface, MAC2STR(hapd->own_addr),
965 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 }
967
968 if (hostapd_setup_wpa_psk(conf)) {
969 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
970 return -1;
971 }
972
973 /* Set SSID for the kernel driver (to be used in beacon and probe
974 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700975 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 conf->ssid.ssid_len)) {
977 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
978 return -1;
979 }
980
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700981 if (wpa_debug_level <= MSG_MSGDUMP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 conf->radius->msg_dumps = 1;
983#ifndef CONFIG_NO_RADIUS
984 hapd->radius = radius_client_init(hapd, conf->radius);
985 if (hapd->radius == NULL) {
986 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
987 return -1;
988 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700990 if (conf->radius_das_port) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700991 struct radius_das_conf das_conf;
992 os_memset(&das_conf, 0, sizeof(das_conf));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700993 das_conf.port = conf->radius_das_port;
994 das_conf.shared_secret = conf->radius_das_shared_secret;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700995 das_conf.shared_secret_len =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700996 conf->radius_das_shared_secret_len;
997 das_conf.client_addr = &conf->radius_das_client_addr;
998 das_conf.time_window = conf->radius_das_time_window;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700999 das_conf.require_event_timestamp =
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001000 conf->radius_das_require_event_timestamp;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001001 das_conf.ctx = hapd;
1002 das_conf.disconnect = hostapd_das_disconnect;
1003 hapd->radius_das = radius_das_init(&das_conf);
1004 if (hapd->radius_das == NULL) {
1005 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
1006 "failed.");
1007 return -1;
1008 }
1009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010#endif /* CONFIG_NO_RADIUS */
1011
1012 if (hostapd_acl_init(hapd)) {
1013 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1014 return -1;
1015 }
1016 if (hostapd_init_wps(hapd, conf))
1017 return -1;
1018
1019 if (authsrv_init(hapd) < 0)
1020 return -1;
1021
1022 if (ieee802_1x_init(hapd)) {
1023 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1024 return -1;
1025 }
1026
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001027 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 return -1;
1029
1030 if (accounting_init(hapd)) {
1031 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1032 return -1;
1033 }
1034
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001035 if (conf->ieee802_11f &&
1036 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1038 "failed.");
1039 return -1;
1040 }
1041
Dmitry Shmidt04949592012-07-19 12:16:46 -07001042#ifdef CONFIG_INTERWORKING
1043 if (gas_serv_init(hapd)) {
1044 wpa_printf(MSG_ERROR, "GAS server initialization failed");
1045 return -1;
1046 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001048 if (conf->qos_map_set_len &&
1049 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
1050 conf->qos_map_set_len)) {
1051 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 return -1;
1053 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001054#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001056 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
1057 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
1058 return -1;
1059 }
1060
1061 if (conf->proxy_arp) {
1062 if (x_snoop_init(hapd)) {
1063 wpa_printf(MSG_ERROR,
1064 "Generic snooping infrastructure initialization failed");
1065 return -1;
1066 }
1067
1068 if (dhcp_snoop_init(hapd)) {
1069 wpa_printf(MSG_ERROR,
1070 "DHCP snooping initialization failed");
1071 return -1;
1072 }
1073
1074 if (ndisc_snoop_init(hapd)) {
1075 wpa_printf(MSG_ERROR,
1076 "Neighbor Discovery snooping initialization failed");
1077 return -1;
1078 }
1079 }
1080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1082 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1083 return -1;
1084 }
1085
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001086 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001087 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001089 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
1090 return -1;
1091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 if (hapd->driver && hapd->driver->set_operstate)
1093 hapd->driver->set_operstate(hapd->drv_priv, 1);
1094
1095 return 0;
1096}
1097
1098
1099static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1100{
1101 struct hostapd_data *hapd = iface->bss[0];
1102 int i;
1103 struct hostapd_tx_queue_params *p;
1104
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001105#ifdef CONFIG_MESH
1106 if (iface->mconf == NULL)
1107 return;
1108#endif /* CONFIG_MESH */
1109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 for (i = 0; i < NUM_TX_QUEUES; i++) {
1111 p = &iface->conf->tx_queue[i];
1112
1113 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1114 p->cwmax, p->burst)) {
1115 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1116 "parameters for queue %d.", i);
1117 /* Continue anyway */
1118 }
1119 }
1120}
1121
1122
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001123static int hostapd_set_acl_list(struct hostapd_data *hapd,
1124 struct mac_acl_entry *mac_acl,
1125 int n_entries, u8 accept_acl)
1126{
1127 struct hostapd_acl_params *acl_params;
1128 int i, err;
1129
1130 acl_params = os_zalloc(sizeof(*acl_params) +
1131 (n_entries * sizeof(acl_params->mac_acl[0])));
1132 if (!acl_params)
1133 return -ENOMEM;
1134
1135 for (i = 0; i < n_entries; i++)
1136 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
1137 ETH_ALEN);
1138
1139 acl_params->acl_policy = accept_acl;
1140 acl_params->num_mac_acl = n_entries;
1141
1142 err = hostapd_drv_set_acl(hapd, acl_params);
1143
1144 os_free(acl_params);
1145
1146 return err;
1147}
1148
1149
1150static void hostapd_set_acl(struct hostapd_data *hapd)
1151{
1152 struct hostapd_config *conf = hapd->iconf;
1153 int err;
1154 u8 accept_acl;
1155
1156 if (hapd->iface->drv_max_acl_mac_addrs == 0)
1157 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001158
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001159 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001160 accept_acl = 1;
1161 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
1162 conf->bss[0]->num_accept_mac,
1163 accept_acl);
1164 if (err) {
1165 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
1166 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001167 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001168 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001169 accept_acl = 0;
1170 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
1171 conf->bss[0]->num_deny_mac,
1172 accept_acl);
1173 if (err) {
1174 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
1175 return;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001176 }
1177 }
1178}
1179
1180
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001181static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1182{
1183 if (!hapd->iface->interfaces ||
1184 !hapd->iface->interfaces->ctrl_iface_init)
1185 return 0;
1186
1187 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1188 wpa_printf(MSG_ERROR,
1189 "Failed to setup control interface for %s",
1190 hapd->conf->iface);
1191 return -1;
1192 }
1193
1194 return 0;
1195}
1196
1197
1198static int start_ctrl_iface(struct hostapd_iface *iface)
1199{
1200 size_t i;
1201
1202 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1203 return 0;
1204
1205 for (i = 0; i < iface->num_bss; i++) {
1206 struct hostapd_data *hapd = iface->bss[i];
1207 if (iface->interfaces->ctrl_iface_init(hapd)) {
1208 wpa_printf(MSG_ERROR,
1209 "Failed to setup control interface for %s",
1210 hapd->conf->iface);
1211 return -1;
1212 }
1213 }
1214
1215 return 0;
1216}
1217
1218
1219static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1220{
1221 struct hostapd_iface *iface = eloop_ctx;
1222
1223 if (!iface->wait_channel_update) {
1224 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1225 return;
1226 }
1227
1228 /*
1229 * It is possible that the existing channel list is acceptable, so try
1230 * to proceed.
1231 */
1232 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1233 setup_interface2(iface);
1234}
1235
1236
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001237void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001238{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001239 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001240 return;
1241
1242 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1243 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1244 setup_interface2(iface);
1245}
1246
1247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248static int setup_interface(struct hostapd_iface *iface)
1249{
1250 struct hostapd_data *hapd = iface->bss[0];
1251 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001252
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001253 /*
1254 * It is possible that setup_interface() is called after the interface
1255 * was disabled etc., in which case driver_ap_teardown is possibly set
1256 * to 1. Clear it here so any other key/station deletion, which is not
1257 * part of a teardown flow, would also call the relevant driver
1258 * callbacks.
1259 */
1260 iface->driver_ap_teardown = 0;
1261
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001262 if (!iface->phy[0]) {
1263 const char *phy = hostapd_drv_get_radio_name(hapd);
1264 if (phy) {
1265 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1266 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1267 }
1268 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269
1270 /*
1271 * Make sure that all BSSes get configured with a pointer to the same
1272 * driver interface.
1273 */
1274 for (i = 1; i < iface->num_bss; i++) {
1275 iface->bss[i]->driver = hapd->driver;
1276 iface->bss[i]->drv_priv = hapd->drv_priv;
1277 }
1278
1279 if (hostapd_validate_bssid_configuration(iface))
1280 return -1;
1281
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001282 /*
1283 * Initialize control interfaces early to allow external monitoring of
1284 * channel setup operations that may take considerable amount of time
1285 * especially for DFS cases.
1286 */
1287 if (start_ctrl_iface(iface))
1288 return -1;
1289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001291 char country[4], previous_country[4];
1292
1293 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1294 if (hostapd_get_country(hapd, previous_country) < 0)
1295 previous_country[0] = '\0';
1296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 os_memcpy(country, hapd->iconf->country, 3);
1298 country[3] = '\0';
1299 if (hostapd_set_country(hapd, country) < 0) {
1300 wpa_printf(MSG_ERROR, "Failed to set country code");
1301 return -1;
1302 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001303
1304 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1305 previous_country, country);
1306
1307 if (os_strncmp(previous_country, country, 2) != 0) {
1308 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1309 iface->wait_channel_update = 1;
Dmitry Shmidt97672262014-02-03 13:02:54 -08001310 eloop_register_timeout(5, 0,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001311 channel_list_update_timeout,
1312 iface, NULL);
1313 return 0;
1314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 }
1316
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001317 return setup_interface2(iface);
1318}
1319
1320
1321static int setup_interface2(struct hostapd_iface *iface)
1322{
1323 iface->wait_channel_update = 0;
1324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 if (hostapd_get_hw_features(iface)) {
1326 /* Not all drivers support this yet, so continue without hw
1327 * feature data. */
1328 } else {
1329 int ret = hostapd_select_hw_mode(iface);
1330 if (ret < 0) {
1331 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1332 "channel. (%d)", ret);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001333 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001335 if (ret == 1) {
1336 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1337 return 0;
1338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 ret = hostapd_check_ht_capab(iface);
1340 if (ret < 0)
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001341 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 if (ret == 1) {
1343 wpa_printf(MSG_DEBUG, "Interface initialization will "
1344 "be completed in a callback");
1345 return 0;
1346 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001347
1348 if (iface->conf->ieee80211h)
1349 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 }
1351 return hostapd_setup_interface_complete(iface, 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001352
1353fail:
1354 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1355 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1356 if (iface->interfaces && iface->interfaces->terminate_on_error)
1357 eloop_terminate();
1358 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359}
1360
1361
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001362/**
1363 * hostapd_setup_interface_complete - Complete interface setup
1364 *
1365 * This function is called when previous steps in the interface setup has been
1366 * completed. This can also start operations, e.g., DFS, that will require
1367 * additional processing before interface is ready to be enabled. Such
1368 * operations will call this function from eloop callbacks when finished.
1369 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1371{
1372 struct hostapd_data *hapd = iface->bss[0];
1373 size_t j;
1374 u8 *prev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001375 int delay_apply_cfg = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001377 if (err)
1378 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379
1380 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001381 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001382#ifdef NEED_AP_MLME
1383 int res;
1384#endif /* NEED_AP_MLME */
1385
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001386 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1388 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001389 hostapd_hw_mode_txt(iface->conf->hw_mode),
1390 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
Dmitry Shmidt051af732013-10-22 13:52:46 -07001392#ifdef NEED_AP_MLME
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001393 /* Handle DFS only if it is not offloaded to the driver */
1394 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1395 /* Check DFS */
1396 res = hostapd_handle_dfs(iface);
1397 if (res <= 0) {
1398 if (res < 0)
1399 goto fail;
1400 return res;
1401 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001402 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001403#endif /* NEED_AP_MLME */
1404
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001405#ifdef CONFIG_MESH
1406 if (iface->mconf != NULL) {
1407 wpa_printf(MSG_DEBUG,
1408 "%s: Mesh configuration will be applied while joining the mesh network",
1409 iface->bss[0]->conf->iface);
1410 delay_apply_cfg = 1;
1411 }
1412#endif /* CONFIG_MESH */
1413
1414 if (!delay_apply_cfg &&
1415 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 hapd->iconf->channel,
1417 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001418 hapd->iconf->ieee80211ac,
1419 hapd->iconf->secondary_channel,
1420 hapd->iconf->vht_oper_chwidth,
1421 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1422 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 wpa_printf(MSG_ERROR, "Could not set channel for "
1424 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001425 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 }
1427 }
1428
1429 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001430 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1432 "table.");
1433 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1434 HOSTAPD_LEVEL_WARNING,
1435 "Failed to prepare rates table.");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001436 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 }
1438 }
1439
1440 if (hapd->iconf->rts_threshold > -1 &&
1441 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1442 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1443 "kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001444 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 }
1446
1447 if (hapd->iconf->fragm_threshold > -1 &&
1448 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1449 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1450 "for kernel driver");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001451 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 }
1453
1454 prev_addr = hapd->own_addr;
1455
1456 for (j = 0; j < iface->num_bss; j++) {
1457 hapd = iface->bss[j];
1458 if (j)
1459 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001460 if (hostapd_setup_bss(hapd, j == 0)) {
1461 do {
1462 hapd = iface->bss[j];
1463 hostapd_bss_deinit_no_free(hapd);
1464 hostapd_free_hapd_data(hapd);
1465 } while (j-- > 0);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001466 goto fail;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1469 prev_addr = hapd->own_addr;
1470 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001471 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472
1473 hostapd_tx_queue_params(iface);
1474
1475 ap_list_init(iface);
1476
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001477 hostapd_set_acl(hapd);
1478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 if (hostapd_driver_commit(hapd) < 0) {
1480 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1481 "configuration", __func__);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001482 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 }
1484
Jouni Malinen87fd2792011-05-16 18:35:42 +03001485 /*
1486 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1487 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1488 * mode), the interface is up only after driver_commit, so initialize
1489 * WPS after driver_commit.
1490 */
1491 for (j = 0; j < iface->num_bss; j++) {
1492 if (hostapd_init_wps_complete(iface->bss[j]))
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001493 goto fail;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001494 }
1495
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001496 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1497 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 if (hapd->setup_complete_cb)
1499 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1500
1501 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1502 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001503 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1504 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
1506 return 0;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001507
1508fail:
1509 wpa_printf(MSG_ERROR, "Interface initialization failed");
1510 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1511 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1512 if (iface->interfaces && iface->interfaces->terminate_on_error)
1513 eloop_terminate();
1514 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515}
1516
1517
1518/**
1519 * hostapd_setup_interface - Setup of an interface
1520 * @iface: Pointer to interface data.
1521 * Returns: 0 on success, -1 on failure
1522 *
1523 * Initializes the driver interface, validates the configuration,
1524 * and sets driver parameters based on the configuration.
1525 * Flushes old stations, sets the channel, encryption,
1526 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001527 *
1528 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1529 * or DFS operations, this function returns 0 before such operations have been
1530 * completed. The pending operations are registered into eloop and will be
1531 * completed from eloop callbacks. Those callbacks end up calling
1532 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 */
1534int hostapd_setup_interface(struct hostapd_iface *iface)
1535{
1536 int ret;
1537
1538 ret = setup_interface(iface);
1539 if (ret) {
1540 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1541 iface->bss[0]->conf->iface);
1542 return -1;
1543 }
1544
1545 return 0;
1546}
1547
1548
1549/**
1550 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1551 * @hapd_iface: Pointer to interface data
1552 * @conf: Pointer to per-interface configuration
1553 * @bss: Pointer to per-BSS configuration for this BSS
1554 * Returns: Pointer to allocated BSS data
1555 *
1556 * This function is used to allocate per-BSS data structure. This data will be
1557 * freed after hostapd_cleanup() is called for it during interface
1558 * deinitialization.
1559 */
1560struct hostapd_data *
1561hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1562 struct hostapd_config *conf,
1563 struct hostapd_bss_config *bss)
1564{
1565 struct hostapd_data *hapd;
1566
1567 hapd = os_zalloc(sizeof(*hapd));
1568 if (hapd == NULL)
1569 return NULL;
1570
1571 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1572 hapd->iconf = conf;
1573 hapd->conf = bss;
1574 hapd->iface = hapd_iface;
1575 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001576 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577
1578 return hapd;
1579}
1580
1581
Dmitry Shmidt54605472013-11-08 11:10:19 -08001582static void hostapd_bss_deinit(struct hostapd_data *hapd)
1583{
1584 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1585 hapd->conf->iface);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001586 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001587 hostapd_cleanup(hapd);
1588}
1589
1590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591void hostapd_interface_deinit(struct hostapd_iface *iface)
1592{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001593 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594
Dmitry Shmidt54605472013-11-08 11:10:19 -08001595 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596 if (iface == NULL)
1597 return;
1598
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001599#ifdef CONFIG_IEEE80211N
1600#ifdef NEED_AP_MLME
1601 hostapd_stop_setup_timers(iface);
1602 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1603#endif /* NEED_AP_MLME */
1604#endif /* CONFIG_IEEE80211N */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001605 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1606 iface->wait_channel_update = 0;
1607
Dmitry Shmidt54605472013-11-08 11:10:19 -08001608 for (j = iface->num_bss - 1; j >= 0; j--)
1609 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610}
1611
1612
1613void hostapd_interface_free(struct hostapd_iface *iface)
1614{
1615 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001616 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1617 for (j = 0; j < iface->num_bss; j++) {
1618 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1619 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001621 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 hostapd_cleanup_iface(iface);
1623}
1624
1625
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001626/**
1627 * hostapd_init - Allocate and initialize per-interface data
1628 * @config_file: Path to the configuration file
1629 * Returns: Pointer to the allocated interface data or %NULL on failure
1630 *
1631 * This function is used to allocate main data structures for per-interface
1632 * data. The allocated data buffer will be freed by calling
1633 * hostapd_cleanup_iface().
1634 */
1635struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1636 const char *config_file)
1637{
1638 struct hostapd_iface *hapd_iface = NULL;
1639 struct hostapd_config *conf = NULL;
1640 struct hostapd_data *hapd;
1641 size_t i;
1642
1643 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1644 if (hapd_iface == NULL)
1645 goto fail;
1646
1647 hapd_iface->config_fname = os_strdup(config_file);
1648 if (hapd_iface->config_fname == NULL)
1649 goto fail;
1650
1651 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1652 if (conf == NULL)
1653 goto fail;
1654 hapd_iface->conf = conf;
1655
1656 hapd_iface->num_bss = conf->num_bss;
1657 hapd_iface->bss = os_calloc(conf->num_bss,
1658 sizeof(struct hostapd_data *));
1659 if (hapd_iface->bss == NULL)
1660 goto fail;
1661
1662 for (i = 0; i < conf->num_bss; i++) {
1663 hapd = hapd_iface->bss[i] =
1664 hostapd_alloc_bss_data(hapd_iface, conf,
1665 conf->bss[i]);
1666 if (hapd == NULL)
1667 goto fail;
1668 hapd->msg_ctx = hapd;
1669 }
1670
1671 return hapd_iface;
1672
1673fail:
1674 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1675 config_file);
1676 if (conf)
1677 hostapd_config_free(conf);
1678 if (hapd_iface) {
1679 os_free(hapd_iface->config_fname);
1680 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001681 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1682 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001683 os_free(hapd_iface);
1684 }
1685 return NULL;
1686}
1687
1688
1689static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1690{
1691 size_t i, j;
1692
1693 for (i = 0; i < interfaces->count; i++) {
1694 struct hostapd_iface *iface = interfaces->iface[i];
1695 for (j = 0; j < iface->num_bss; j++) {
1696 struct hostapd_data *hapd = iface->bss[j];
1697 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1698 return 1;
1699 }
1700 }
1701
1702 return 0;
1703}
1704
1705
1706/**
1707 * hostapd_interface_init_bss - Read configuration file and init BSS data
1708 *
1709 * This function is used to parse configuration file for a BSS. This BSS is
1710 * added to an existing interface sharing the same radio (if any) or a new
1711 * interface is created if this is the first interface on a radio. This
1712 * allocate memory for the BSS. No actual driver operations are started.
1713 *
1714 * This is similar to hostapd_interface_init(), but for a case where the
1715 * configuration is used to add a single BSS instead of all BSSes for a radio.
1716 */
1717struct hostapd_iface *
1718hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1719 const char *config_fname, int debug)
1720{
1721 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1722 struct hostapd_data *hapd;
1723 int k;
1724 size_t i, bss_idx;
1725
1726 if (!phy || !*phy)
1727 return NULL;
1728
1729 for (i = 0; i < interfaces->count; i++) {
1730 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1731 iface = interfaces->iface[i];
1732 break;
1733 }
1734 }
1735
1736 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1737 config_fname, phy, iface ? "" : " --> new PHY");
1738 if (iface) {
1739 struct hostapd_config *conf;
1740 struct hostapd_bss_config **tmp_conf;
1741 struct hostapd_data **tmp_bss;
1742 struct hostapd_bss_config *bss;
1743 const char *ifname;
1744
1745 /* Add new BSS to existing iface */
1746 conf = interfaces->config_read_cb(config_fname);
1747 if (conf == NULL)
1748 return NULL;
1749 if (conf->num_bss > 1) {
1750 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1751 hostapd_config_free(conf);
1752 return NULL;
1753 }
1754
1755 ifname = conf->bss[0]->iface;
1756 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1757 wpa_printf(MSG_ERROR,
1758 "Interface name %s already in use", ifname);
1759 hostapd_config_free(conf);
1760 return NULL;
1761 }
1762
1763 tmp_conf = os_realloc_array(
1764 iface->conf->bss, iface->conf->num_bss + 1,
1765 sizeof(struct hostapd_bss_config *));
1766 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1767 sizeof(struct hostapd_data *));
1768 if (tmp_bss)
1769 iface->bss = tmp_bss;
1770 if (tmp_conf) {
1771 iface->conf->bss = tmp_conf;
1772 iface->conf->last_bss = tmp_conf[0];
1773 }
1774 if (tmp_bss == NULL || tmp_conf == NULL) {
1775 hostapd_config_free(conf);
1776 return NULL;
1777 }
1778 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1779 iface->conf->num_bss++;
1780
1781 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1782 if (hapd == NULL) {
1783 iface->conf->num_bss--;
1784 hostapd_config_free(conf);
1785 return NULL;
1786 }
1787 iface->conf->last_bss = bss;
1788 iface->bss[iface->num_bss] = hapd;
1789 hapd->msg_ctx = hapd;
1790
1791 bss_idx = iface->num_bss++;
1792 conf->num_bss--;
1793 conf->bss[0] = NULL;
1794 hostapd_config_free(conf);
1795 } else {
1796 /* Add a new iface with the first BSS */
1797 new_iface = iface = hostapd_init(interfaces, config_fname);
1798 if (!iface)
1799 return NULL;
1800 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1801 iface->interfaces = interfaces;
1802 bss_idx = 0;
1803 }
1804
1805 for (k = 0; k < debug; k++) {
1806 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1807 iface->bss[bss_idx]->conf->logger_stdout_level--;
1808 }
1809
1810 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1811 !hostapd_drv_none(iface->bss[bss_idx])) {
1812 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1813 config_fname);
1814 if (new_iface)
1815 hostapd_interface_deinit_free(new_iface);
1816 return NULL;
1817 }
1818
1819 return iface;
1820}
1821
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001822
1823void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1824{
1825 const struct wpa_driver_ops *driver;
1826 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001827
1828 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001829 if (iface == NULL)
1830 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001831 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1832 __func__, (unsigned int) iface->num_bss,
1833 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001834 driver = iface->bss[0]->driver;
1835 drv_priv = iface->bss[0]->drv_priv;
1836 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001837 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1838 __func__, driver, drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001839 if (driver && driver->hapd_deinit && drv_priv) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001840 driver->hapd_deinit(drv_priv);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001841 iface->bss[0]->drv_priv = NULL;
1842 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001843 hostapd_interface_free(iface);
1844}
1845
1846
Dmitry Shmidt15907092014-03-25 10:42:57 -07001847static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1848 void *drv_priv,
1849 struct hostapd_iface *hapd_iface)
1850{
1851 size_t j;
1852
1853 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1854 __func__, driver, drv_priv);
1855 if (driver && driver->hapd_deinit && drv_priv) {
1856 driver->hapd_deinit(drv_priv);
1857 for (j = 0; j < hapd_iface->num_bss; j++) {
1858 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1859 __func__, (int) j,
1860 hapd_iface->bss[j]->drv_priv);
1861 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1862 hapd_iface->bss[j]->drv_priv = NULL;
1863 }
1864 }
1865}
1866
1867
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001868int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1869{
Dmitry Shmidt71757432014-06-02 13:50:35 -07001870 size_t j;
1871
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001872 if (hapd_iface->bss[0]->drv_priv != NULL) {
1873 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001874 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001875 return -1;
1876 }
1877
1878 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001879 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001880
Dmitry Shmidt71757432014-06-02 13:50:35 -07001881 for (j = 0; j < hapd_iface->num_bss; j++)
1882 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001883 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1884 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1885 return -1;
1886 }
1887
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001888 if (hapd_iface->interfaces == NULL ||
1889 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001890 hapd_iface->interfaces->driver_init(hapd_iface))
1891 return -1;
1892
1893 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt15907092014-03-25 10:42:57 -07001894 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1895 hapd_iface->bss[0]->drv_priv,
1896 hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001897 return -1;
1898 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001899
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001900 return 0;
1901}
1902
1903
1904int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1905{
1906 size_t j;
1907
1908 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001909 hapd_iface->conf->bss[0]->iface);
1910 for (j = 0; j < hapd_iface->num_bss; j++)
Dmitry Shmidt71757432014-06-02 13:50:35 -07001911 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001912 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001913 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1914 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001915 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001916 hostapd_clear_old(hapd_iface);
1917 for (j = 0; j < hapd_iface->num_bss; j++)
1918 hostapd_reload_bss(hapd_iface->bss[j]);
1919
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001920 return 0;
1921}
1922
1923
1924int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1925{
1926 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001927 const struct wpa_driver_ops *driver;
1928 void *drv_priv;
1929
1930 if (hapd_iface == NULL)
1931 return -1;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001932
1933 if (hapd_iface->bss[0]->drv_priv == NULL) {
1934 wpa_printf(MSG_INFO, "Interface %s already disabled",
1935 hapd_iface->conf->bss[0]->iface);
1936 return -1;
1937 }
1938
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001939 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001940 driver = hapd_iface->bss[0]->driver;
1941 drv_priv = hapd_iface->bss[0]->drv_priv;
1942
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001943 hapd_iface->driver_ap_teardown =
1944 !!(hapd_iface->drv_flags &
1945 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1946
1947 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001948 for (j = 0; j < hapd_iface->num_bss; j++) {
1949 struct hostapd_data *hapd = hapd_iface->bss[j];
Dmitry Shmidt71757432014-06-02 13:50:35 -07001950 hostapd_bss_deinit_no_free(hapd);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001951 hostapd_free_hapd_data(hapd);
1952 }
1953
Dmitry Shmidt15907092014-03-25 10:42:57 -07001954 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001955
1956 /* From hostapd_cleanup_iface: These were initialized in
1957 * hostapd_setup_interface and hostapd_setup_interface_complete
1958 */
1959 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001960
Dmitry Shmidt56052862013-10-04 10:23:25 -07001961 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1962 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001963 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001964 return 0;
1965}
1966
1967
1968static struct hostapd_iface *
1969hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1970{
1971 struct hostapd_iface **iface, *hapd_iface;
1972
1973 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1974 sizeof(struct hostapd_iface *));
1975 if (iface == NULL)
1976 return NULL;
1977 interfaces->iface = iface;
1978 hapd_iface = interfaces->iface[interfaces->count] =
1979 os_zalloc(sizeof(*hapd_iface));
1980 if (hapd_iface == NULL) {
1981 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1982 "the interface", __func__);
1983 return NULL;
1984 }
1985 interfaces->count++;
1986 hapd_iface->interfaces = interfaces;
1987
1988 return hapd_iface;
1989}
1990
1991
1992static struct hostapd_config *
1993hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1994 const char *ctrl_iface)
1995{
1996 struct hostapd_bss_config *bss;
1997 struct hostapd_config *conf;
1998
1999 /* Allocates memory for bss and conf */
2000 conf = hostapd_config_defaults();
2001 if (conf == NULL) {
2002 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
2003 "configuration", __func__);
2004 return NULL;
2005 }
2006
2007 conf->driver = wpa_drivers[0];
2008 if (conf->driver == NULL) {
2009 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
2010 hostapd_config_free(conf);
2011 return NULL;
2012 }
2013
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002014 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002015
2016 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
2017 bss->ctrl_interface = os_strdup(ctrl_iface);
2018 if (bss->ctrl_interface == NULL) {
2019 hostapd_config_free(conf);
2020 return NULL;
2021 }
2022
2023 /* Reading configuration file skipped, will be done in SET!
2024 * From reading the configuration till the end has to be done in
2025 * SET
2026 */
2027 return conf;
2028}
2029
2030
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002031static int hostapd_data_alloc(struct hostapd_iface *hapd_iface,
2032 struct hostapd_config *conf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002033{
2034 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002035 struct hostapd_data *hapd;
2036
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002037 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002038 sizeof(struct hostapd_data *));
2039 if (hapd_iface->bss == NULL)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002040 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002041
2042 for (i = 0; i < conf->num_bss; i++) {
2043 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002044 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002045 if (hapd == NULL) {
2046 while (i > 0) {
2047 i--;
2048 os_free(hapd_iface->bss[i]);
2049 hapd_iface->bss[i] = NULL;
2050 }
2051 os_free(hapd_iface->bss);
2052 hapd_iface->bss = NULL;
2053 return -1;
2054 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002055 hapd->msg_ctx = hapd;
2056 }
2057
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002058 hapd_iface->conf = conf;
2059 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002060
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002061 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002062}
2063
2064
2065int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
2066{
2067 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002068 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
2069 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002070 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002071 size_t i, j;
2072 const char *conf_file = NULL, *phy_name = NULL;
2073
2074 if (os_strncmp(buf, "bss_config=", 11) == 0) {
2075 char *pos;
2076 phy_name = buf + 11;
2077 pos = os_strchr(phy_name, ':');
2078 if (!pos)
2079 return -1;
2080 *pos++ = '\0';
2081 conf_file = pos;
2082 if (!os_strlen(conf_file))
2083 return -1;
2084
2085 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
2086 conf_file, 0);
2087 if (!hapd_iface)
2088 return -1;
2089 for (j = 0; j < interfaces->count; j++) {
2090 if (interfaces->iface[j] == hapd_iface)
2091 break;
2092 }
2093 if (j == interfaces->count) {
2094 struct hostapd_iface **tmp;
2095 tmp = os_realloc_array(interfaces->iface,
2096 interfaces->count + 1,
2097 sizeof(struct hostapd_iface *));
2098 if (!tmp) {
2099 hostapd_interface_deinit_free(hapd_iface);
2100 return -1;
2101 }
2102 interfaces->iface = tmp;
2103 interfaces->iface[interfaces->count++] = hapd_iface;
2104 new_iface = hapd_iface;
2105 }
2106
2107 if (new_iface) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002108 if (interfaces->driver_init(hapd_iface))
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002109 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002110
2111 if (hostapd_setup_interface(hapd_iface)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002112 hostapd_deinit_driver(
2113 hapd_iface->bss[0]->driver,
2114 hapd_iface->bss[0]->drv_priv,
2115 hapd_iface);
2116 goto fail;
2117 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002118 } else {
2119 /* Assign new BSS with bss[0]'s driver info */
2120 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
2121 hapd->driver = hapd_iface->bss[0]->driver;
2122 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
2123 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
2124 ETH_ALEN);
2125
2126 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08002127 (hapd_iface->state == HAPD_IFACE_ENABLED &&
2128 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002129 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002130 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002131 hapd_iface->conf->num_bss--;
2132 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002133 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
2134 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002135 hostapd_config_free_bss(hapd->conf);
2136 hapd->conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002137 os_free(hapd);
2138 return -1;
2139 }
2140 }
2141 return 0;
2142 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002143
2144 ptr = os_strchr(buf, ' ');
2145 if (ptr == NULL)
2146 return -1;
2147 *ptr++ = '\0';
2148
Dmitry Shmidt56052862013-10-04 10:23:25 -07002149 if (os_strncmp(ptr, "config=", 7) == 0)
2150 conf_file = ptr + 7;
2151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002152 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002153 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002154 buf)) {
2155 wpa_printf(MSG_INFO, "Cannot add interface - it "
2156 "already exists");
2157 return -1;
2158 }
2159 }
2160
2161 hapd_iface = hostapd_iface_alloc(interfaces);
2162 if (hapd_iface == NULL) {
2163 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2164 "for interface", __func__);
2165 goto fail;
2166 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002167 new_iface = hapd_iface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002168
Dmitry Shmidt56052862013-10-04 10:23:25 -07002169 if (conf_file && interfaces->config_read_cb) {
2170 conf = interfaces->config_read_cb(conf_file);
2171 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002172 os_strlcpy(conf->bss[0]->iface, buf,
2173 sizeof(conf->bss[0]->iface));
Dmitry Shmidt56052862013-10-04 10:23:25 -07002174 } else
2175 conf = hostapd_config_alloc(interfaces, buf, ptr);
2176 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002177 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2178 "for configuration", __func__);
2179 goto fail;
2180 }
2181
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002182 if (hostapd_data_alloc(hapd_iface, conf) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002183 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
2184 "for hostapd", __func__);
2185 goto fail;
2186 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002187 conf = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002188
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002189 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002190 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002191
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002192 wpa_printf(MSG_INFO, "Add interface '%s'",
2193 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002194
2195 return 0;
2196
2197fail:
2198 if (conf)
2199 hostapd_config_free(conf);
2200 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002201 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002202 for (i = 0; i < hapd_iface->num_bss; i++) {
2203 hapd = hapd_iface->bss[i];
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002204 if (!hapd)
2205 continue;
2206 if (hapd_iface->interfaces &&
Dmitry Shmidt54605472013-11-08 11:10:19 -08002207 hapd_iface->interfaces->ctrl_iface_deinit)
2208 hapd_iface->interfaces->
2209 ctrl_iface_deinit(hapd);
2210 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2211 __func__, hapd_iface->bss[i],
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002212 hapd->conf->iface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002213 hostapd_cleanup(hapd);
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08002214 os_free(hapd);
2215 hapd_iface->bss[i] = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002216 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002217 os_free(hapd_iface->bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002218 hapd_iface->bss = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002219 }
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002220 if (new_iface) {
2221 interfaces->count--;
2222 interfaces->iface[interfaces->count] = NULL;
2223 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002224 hostapd_cleanup_iface(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002225 }
2226 return -1;
2227}
2228
2229
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002230static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2231{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002232 size_t i;
2233
Dmitry Shmidt54605472013-11-08 11:10:19 -08002234 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002235
Dmitry Shmidt54605472013-11-08 11:10:19 -08002236 /* Remove hostapd_data only if it has already been initialized */
2237 if (idx < iface->num_bss) {
2238 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002239
Dmitry Shmidt54605472013-11-08 11:10:19 -08002240 hostapd_bss_deinit(hapd);
2241 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2242 __func__, hapd, hapd->conf->iface);
2243 hostapd_config_free_bss(hapd->conf);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002244 hapd->conf = NULL;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002245 os_free(hapd);
2246
2247 iface->num_bss--;
2248
2249 for (i = idx; i < iface->num_bss; i++)
2250 iface->bss[i] = iface->bss[i + 1];
2251 } else {
2252 hostapd_config_free_bss(iface->conf->bss[idx]);
2253 iface->conf->bss[idx] = NULL;
2254 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002255
2256 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002257 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002258 iface->conf->bss[i] = iface->conf->bss[i + 1];
2259
2260 return 0;
2261}
2262
2263
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002264int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2265{
2266 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002267 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002268
2269 for (i = 0; i < interfaces->count; i++) {
2270 hapd_iface = interfaces->iface[i];
2271 if (hapd_iface == NULL)
2272 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08002273 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002274 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002275 hapd_iface->driver_ap_teardown =
2276 !!(hapd_iface->drv_flags &
2277 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2278
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002279 hostapd_interface_deinit_free(hapd_iface);
2280 k = i;
2281 while (k < (interfaces->count - 1)) {
2282 interfaces->iface[k] =
2283 interfaces->iface[k + 1];
2284 k++;
2285 }
2286 interfaces->count--;
2287 return 0;
2288 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002289
2290 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002291 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2292 hapd_iface->driver_ap_teardown =
2293 !(hapd_iface->drv_flags &
2294 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002295 return hostapd_remove_bss(hapd_iface, j);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002296 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002297 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002298 }
2299 return -1;
2300}
2301
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303/**
2304 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2305 * @hapd: Pointer to BSS data
2306 * @sta: Pointer to the associated STA data
2307 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2308 *
2309 * This function will be called whenever a station associates with the AP. It
2310 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2311 * from drv_callbacks.c based on driver events for drivers that take care of
2312 * management frames (IEEE 802.11 authentication and association) internally.
2313 */
2314void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2315 int reassoc)
2316{
2317 if (hapd->tkip_countermeasures) {
2318 hostapd_drv_sta_deauth(hapd, sta->addr,
2319 WLAN_REASON_MICHAEL_MIC_FAILURE);
2320 return;
2321 }
2322
2323 hostapd_prune_associations(hapd, sta->addr);
2324
2325 /* IEEE 802.11F (IAPP) */
2326 if (hapd->conf->ieee802_11f)
2327 iapp_new_station(hapd->iapp, sta);
2328
2329#ifdef CONFIG_P2P
2330 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2331 sta->no_p2p_set = 1;
2332 hapd->num_sta_no_p2p++;
2333 if (hapd->num_sta_no_p2p == 1)
2334 hostapd_p2p_non_p2p_sta_connected(hapd);
2335 }
2336#endif /* CONFIG_P2P */
2337
2338 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2339 * IEEE 802.1X/WPA code will start accounting after the station has
2340 * been authorized. */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08002341 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2342 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002343 os_get_reltime(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346
2347 /* Start IEEE 802.1X authentication process for new stations */
2348 ieee802_1x_new_station(hapd, sta);
2349 if (reassoc) {
2350 if (sta->auth_alg != WLAN_AUTH_FT &&
2351 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2352 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2353 } else
2354 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002355
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002356 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2357 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2358 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2359 __func__, MAC2STR(sta->addr),
2360 hapd->conf->ap_max_inactivity);
2361 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2362 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2363 ap_handle_timer, hapd, sta);
2364 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002366
2367
2368const char * hostapd_state_text(enum hostapd_iface_state s)
2369{
2370 switch (s) {
2371 case HAPD_IFACE_UNINITIALIZED:
2372 return "UNINITIALIZED";
2373 case HAPD_IFACE_DISABLED:
2374 return "DISABLED";
2375 case HAPD_IFACE_COUNTRY_UPDATE:
2376 return "COUNTRY_UPDATE";
2377 case HAPD_IFACE_ACS:
2378 return "ACS";
2379 case HAPD_IFACE_HT_SCAN:
2380 return "HT_SCAN";
2381 case HAPD_IFACE_DFS:
2382 return "DFS";
2383 case HAPD_IFACE_ENABLED:
2384 return "ENABLED";
2385 }
2386
2387 return "UNKNOWN";
2388}
2389
2390
2391void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2392{
2393 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2394 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2395 hostapd_state_text(s));
2396 iface->state = s;
2397}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002398
2399
2400#ifdef NEED_AP_MLME
2401
2402static void free_beacon_data(struct beacon_data *beacon)
2403{
2404 os_free(beacon->head);
2405 beacon->head = NULL;
2406 os_free(beacon->tail);
2407 beacon->tail = NULL;
2408 os_free(beacon->probe_resp);
2409 beacon->probe_resp = NULL;
2410 os_free(beacon->beacon_ies);
2411 beacon->beacon_ies = NULL;
2412 os_free(beacon->proberesp_ies);
2413 beacon->proberesp_ies = NULL;
2414 os_free(beacon->assocresp_ies);
2415 beacon->assocresp_ies = NULL;
2416}
2417
2418
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002419static int hostapd_build_beacon_data(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002420 struct beacon_data *beacon)
2421{
2422 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2423 struct wpa_driver_ap_params params;
2424 int ret;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002425
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002426 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002427 ret = ieee802_11_build_ap_params(hapd, &params);
2428 if (ret < 0)
2429 return ret;
2430
2431 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2432 &proberesp_extra,
2433 &assocresp_extra);
2434 if (ret)
2435 goto free_ap_params;
2436
2437 ret = -1;
2438 beacon->head = os_malloc(params.head_len);
2439 if (!beacon->head)
2440 goto free_ap_extra_ies;
2441
2442 os_memcpy(beacon->head, params.head, params.head_len);
2443 beacon->head_len = params.head_len;
2444
2445 beacon->tail = os_malloc(params.tail_len);
2446 if (!beacon->tail)
2447 goto free_beacon;
2448
2449 os_memcpy(beacon->tail, params.tail, params.tail_len);
2450 beacon->tail_len = params.tail_len;
2451
2452 if (params.proberesp != NULL) {
2453 beacon->probe_resp = os_malloc(params.proberesp_len);
2454 if (!beacon->probe_resp)
2455 goto free_beacon;
2456
2457 os_memcpy(beacon->probe_resp, params.proberesp,
2458 params.proberesp_len);
2459 beacon->probe_resp_len = params.proberesp_len;
2460 }
2461
2462 /* copy the extra ies */
2463 if (beacon_extra) {
2464 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2465 if (!beacon->beacon_ies)
2466 goto free_beacon;
2467
2468 os_memcpy(beacon->beacon_ies,
2469 beacon_extra->buf, wpabuf_len(beacon_extra));
2470 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2471 }
2472
2473 if (proberesp_extra) {
2474 beacon->proberesp_ies =
2475 os_malloc(wpabuf_len(proberesp_extra));
2476 if (!beacon->proberesp_ies)
2477 goto free_beacon;
2478
2479 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2480 wpabuf_len(proberesp_extra));
2481 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2482 }
2483
2484 if (assocresp_extra) {
2485 beacon->assocresp_ies =
2486 os_malloc(wpabuf_len(assocresp_extra));
2487 if (!beacon->assocresp_ies)
2488 goto free_beacon;
2489
2490 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2491 wpabuf_len(assocresp_extra));
2492 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2493 }
2494
2495 ret = 0;
2496free_beacon:
2497 /* if the function fails, the caller should not free beacon data */
2498 if (ret)
2499 free_beacon_data(beacon);
2500
2501free_ap_extra_ies:
2502 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2503 assocresp_extra);
2504free_ap_params:
2505 ieee802_11_free_ap_params(&params);
2506 return ret;
2507}
2508
2509
2510/*
2511 * TODO: This flow currently supports only changing frequency within the
2512 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2513 * width) are not supported.
2514 */
2515static int hostapd_change_config_freq(struct hostapd_data *hapd,
2516 struct hostapd_config *conf,
2517 struct hostapd_freq_params *params,
2518 struct hostapd_freq_params *old_params)
2519{
2520 int channel;
2521
2522 if (!params->channel) {
2523 /* check if the new channel is supported by hw */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002524 params->channel = hostapd_hw_get_channel(hapd, params->freq);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002525 }
2526
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002527 channel = params->channel;
2528 if (!channel)
2529 return -1;
2530
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002531 /* if a pointer to old_params is provided we save previous state */
2532 if (old_params) {
2533 old_params->channel = conf->channel;
2534 old_params->ht_enabled = conf->ieee80211n;
2535 old_params->sec_channel_offset = conf->secondary_channel;
2536 }
2537
2538 conf->channel = channel;
2539 conf->ieee80211n = params->ht_enabled;
2540 conf->secondary_channel = params->sec_channel_offset;
2541
2542 /* TODO: maybe call here hostapd_config_check here? */
2543
2544 return 0;
2545}
2546
2547
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002548static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002549 struct csa_settings *settings)
2550{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002551 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002552 struct hostapd_freq_params old_freq;
2553 int ret;
2554
2555 os_memset(&old_freq, 0, sizeof(old_freq));
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002556 if (!iface || !iface->freq || hapd->csa_in_progress)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002557 return -1;
2558
2559 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2560 &settings->freq_params,
2561 &old_freq);
2562 if (ret)
2563 return ret;
2564
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002565 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002566
2567 /* change back the configuration */
2568 hostapd_change_config_freq(iface->bss[0], iface->conf,
2569 &old_freq, NULL);
2570
2571 if (ret)
2572 return ret;
2573
2574 /* set channel switch parameters for csa ie */
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002575 hapd->cs_freq_params = settings->freq_params;
2576 hapd->cs_count = settings->cs_count;
2577 hapd->cs_block_tx = settings->block_tx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002578
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002579 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002580 if (ret) {
2581 free_beacon_data(&settings->beacon_after);
2582 return ret;
2583 }
2584
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002585 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2586 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002587
2588 return 0;
2589}
2590
2591
2592void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2593{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002594 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2595 hapd->cs_count = 0;
2596 hapd->cs_block_tx = 0;
2597 hapd->cs_c_off_beacon = 0;
2598 hapd->cs_c_off_proberesp = 0;
2599 hapd->csa_in_progress = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002600}
2601
2602
2603int hostapd_switch_channel(struct hostapd_data *hapd,
2604 struct csa_settings *settings)
2605{
2606 int ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002607
2608 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2609 wpa_printf(MSG_INFO, "CSA is not supported");
2610 return -1;
2611 }
2612
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002613 ret = hostapd_fill_csa_settings(hapd, settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002614 if (ret)
2615 return ret;
2616
2617 ret = hostapd_drv_switch_channel(hapd, settings);
2618 free_beacon_data(&settings->beacon_csa);
2619 free_beacon_data(&settings->beacon_after);
2620
2621 if (ret) {
2622 /* if we failed, clean cs parameters */
2623 hostapd_cleanup_cs_params(hapd);
2624 return ret;
2625 }
2626
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002627 hapd->csa_in_progress = 1;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002628 return 0;
2629}
2630
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002631
2632void
2633hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2634 const struct hostapd_freq_params *freq_params)
2635{
2636 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2637 unsigned int i;
2638
2639 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2640
2641 if (freq_params->center_freq1)
2642 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2643 if (freq_params->center_freq2)
2644 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2645
2646 switch (freq_params->bandwidth) {
2647 case 0:
2648 case 20:
2649 case 40:
2650 vht_bw = VHT_CHANWIDTH_USE_HT;
2651 break;
2652 case 80:
2653 if (freq_params->center_freq2)
2654 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2655 else
2656 vht_bw = VHT_CHANWIDTH_80MHZ;
2657 break;
2658 case 160:
2659 vht_bw = VHT_CHANWIDTH_160MHZ;
2660 break;
2661 default:
2662 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2663 freq_params->bandwidth);
2664 break;
2665 }
2666
2667 iface->freq = freq_params->freq;
2668 iface->conf->channel = freq_params->channel;
2669 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2670 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2671 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2672 iface->conf->vht_oper_chwidth = vht_bw;
2673 iface->conf->ieee80211n = freq_params->ht_enabled;
2674 iface->conf->ieee80211ac = freq_params->vht_enabled;
2675
2676 /*
2677 * cs_params must not be cleared earlier because the freq_params
2678 * argument may actually point to one of these.
2679 */
2680 for (i = 0; i < iface->num_bss; i++)
2681 hostapd_cleanup_cs_params(iface->bss[i]);
2682
2683 hostapd_disable_iface(iface);
2684 hostapd_enable_iface(iface);
2685}
2686
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002687#endif /* NEED_AP_MLME */