blob: 492861e07a42eaf031094caacf384e4674fad0cb [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003 * Copyright (c) 2002-2013, 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 Shmidt8d520ff2011-05-09 14:06:53 -070017#include "drivers/driver.h"
18#include "hostapd.h"
19#include "authsrv.h"
20#include "sta_info.h"
21#include "accounting.h"
22#include "ap_list.h"
23#include "beacon.h"
24#include "iapp.h"
25#include "ieee802_1x.h"
26#include "ieee802_11_auth.h"
27#include "vlan_init.h"
28#include "wpa_auth.h"
29#include "wps_hostapd.h"
30#include "hw_features.h"
31#include "wpa_auth_glue.h"
32#include "ap_drv_ops.h"
33#include "ap_config.h"
34#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070035#include "gas_serv.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070036#include "dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037
38
Dmitry Shmidt04949592012-07-19 12:16:46 -070039static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070041static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidtcce06662013-11-04 18:44:24 -080042static int setup_interface2(struct hostapd_iface *iface);
43static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45extern int wpa_debug_level;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070046extern struct wpa_driver_ops *wpa_drivers[];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047
48
Dmitry Shmidt04949592012-07-19 12:16:46 -070049int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
50 int (*cb)(struct hostapd_iface *iface,
51 void *ctx), void *ctx)
52{
53 size_t i;
54 int ret;
55
56 for (i = 0; i < interfaces->count; i++) {
57 ret = cb(interfaces->iface[i], ctx);
58 if (ret)
59 return ret;
60 }
61
62 return 0;
63}
64
65
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066static void hostapd_reload_bss(struct hostapd_data *hapd)
67{
Dmitry Shmidtcce06662013-11-04 18:44:24 -080068 struct hostapd_ssid *ssid;
69
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070#ifndef CONFIG_NO_RADIUS
71 radius_client_reconfig(hapd->radius, hapd->conf->radius);
72#endif /* CONFIG_NO_RADIUS */
73
Dmitry Shmidtcce06662013-11-04 18:44:24 -080074 ssid = &hapd->conf->ssid;
75 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
76 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
77 /*
78 * Force PSK to be derived again since SSID or passphrase may
79 * have changed.
80 */
81 os_free(ssid->wpa_psk);
82 ssid->wpa_psk = NULL;
83 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 if (hostapd_setup_wpa_psk(hapd->conf)) {
85 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
86 "after reloading configuration");
87 }
88
89 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
90 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
91 else
92 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
93
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080096 if (hapd->wpa_auth)
97 wpa_init_keys(hapd->wpa_auth);
98 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 const u8 *wpa_ie;
100 size_t wpa_ie_len;
101 hostapd_reconfig_wpa(hapd);
102 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
103 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
104 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
105 "the kernel driver.");
106 } else if (hapd->wpa_auth) {
107 wpa_deinit(hapd->wpa_auth);
108 hapd->wpa_auth = NULL;
109 hostapd_set_privacy(hapd, 0);
110 hostapd_setup_encryption(hapd->conf->iface, hapd);
111 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
112 }
113
114 ieee802_11_set_beacon(hapd);
115 hostapd_update_wps(hapd);
116
117 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119 hapd->conf->ssid.ssid_len)) {
120 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
121 /* try to continue */
122 }
123 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
124}
125
126
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700127static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 size_t j;
130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 /*
132 * Deauthenticate all stations since the new configuration may not
133 * allow them to use the BSS anymore.
134 */
135 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700136 hostapd_flush_old_stations(iface->bss[j],
137 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700138 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139
140#ifndef CONFIG_NO_RADIUS
141 /* TODO: update dynamic data based on changed configuration
142 * items (e.g., open/close sockets, etc.) */
143 radius_client_flush(iface->bss[j]->radius, 0);
144#endif /* CONFIG_NO_RADIUS */
145 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700146}
147
148
149int hostapd_reload_config(struct hostapd_iface *iface)
150{
151 struct hostapd_data *hapd = iface->bss[0];
152 struct hostapd_config *newconf, *oldconf;
153 size_t j;
154
155 if (iface->config_fname == NULL) {
156 /* Only in-memory config in use - assume it has been updated */
157 hostapd_clear_old(iface);
158 for (j = 0; j < iface->num_bss; j++)
159 hostapd_reload_bss(iface->bss[j]);
160 return 0;
161 }
162
163 if (iface->interfaces == NULL ||
164 iface->interfaces->config_read_cb == NULL)
165 return -1;
166 newconf = iface->interfaces->config_read_cb(iface->config_fname);
167 if (newconf == NULL)
168 return -1;
169
170 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171
172 oldconf = hapd->iconf;
173 iface->conf = newconf;
174
175 for (j = 0; j < iface->num_bss; j++) {
176 hapd = iface->bss[j];
177 hapd->iconf = newconf;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800178 hapd->conf = newconf->bss[j];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 hostapd_reload_bss(hapd);
180 }
181
182 hostapd_config_free(oldconf);
183
184
185 return 0;
186}
187
188
189static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
190 char *ifname)
191{
192 int i;
193
194 for (i = 0; i < NUM_WEP_KEYS; i++) {
195 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
196 0, NULL, 0, NULL, 0)) {
197 wpa_printf(MSG_DEBUG, "Failed to clear default "
198 "encryption keys (ifname=%s keyidx=%d)",
199 ifname, i);
200 }
201 }
202#ifdef CONFIG_IEEE80211W
203 if (hapd->conf->ieee80211w) {
204 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
205 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
206 NULL, i, 0, NULL,
207 0, NULL, 0)) {
208 wpa_printf(MSG_DEBUG, "Failed to clear "
209 "default mgmt encryption keys "
210 "(ifname=%s keyidx=%d)", ifname, i);
211 }
212 }
213 }
214#endif /* CONFIG_IEEE80211W */
215}
216
217
218static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
219{
220 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
221 return 0;
222}
223
224
225static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
226{
227 int errors = 0, idx;
228 struct hostapd_ssid *ssid = &hapd->conf->ssid;
229
230 idx = ssid->wep.idx;
231 if (ssid->wep.default_len &&
232 hostapd_drv_set_key(hapd->conf->iface,
233 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
234 1, NULL, 0, ssid->wep.key[idx],
235 ssid->wep.len[idx])) {
236 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
237 errors++;
238 }
239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 return errors;
241}
242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243
Dmitry Shmidt04949592012-07-19 12:16:46 -0700244static void hostapd_free_hapd_data(struct hostapd_data *hapd)
245{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -0800246 if (!hapd->started) {
247 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
248 __func__, hapd->conf->iface);
249 return;
250 }
251 hapd->started = 0;
252
Dmitry Shmidt54605472013-11-08 11:10:19 -0800253 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 iapp_deinit(hapd->iapp);
255 hapd->iapp = NULL;
256 accounting_deinit(hapd);
257 hostapd_deinit_wpa(hapd);
258 vlan_deinit(hapd);
259 hostapd_acl_deinit(hapd);
260#ifndef CONFIG_NO_RADIUS
261 radius_client_deinit(hapd->radius);
262 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700263 radius_das_deinit(hapd->radius_das);
264 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265#endif /* CONFIG_NO_RADIUS */
266
267 hostapd_deinit_wps(hapd);
268
269 authsrv_deinit(hapd);
270
Dmitry Shmidt54605472013-11-08 11:10:19 -0800271 if (hapd->interface_added &&
272 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
274 hapd->conf->iface);
275 }
276
277 os_free(hapd->probereq_cb);
278 hapd->probereq_cb = NULL;
279
280#ifdef CONFIG_P2P
281 wpabuf_free(hapd->p2p_beacon_ie);
282 hapd->p2p_beacon_ie = NULL;
283 wpabuf_free(hapd->p2p_probe_resp_ie);
284 hapd->p2p_probe_resp_ie = NULL;
285#endif /* CONFIG_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286
287 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700288
289#ifdef CONFIG_INTERWORKING
290 gas_serv_deinit(hapd);
291#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800292
293#ifdef CONFIG_SQLITE
294 os_free(hapd->tmp_eap_user.identity);
295 os_free(hapd->tmp_eap_user.password);
296#endif /* CONFIG_SQLITE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700297}
298
299
300/**
301 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
302 * @hapd: Pointer to BSS data
303 *
304 * This function is used to free all per-BSS data structures and resources.
Dmitry Shmidt54605472013-11-08 11:10:19 -0800305 * Most of the modules that are initialized in hostapd_setup_bss() are
306 * deinitialized here.
Dmitry Shmidt04949592012-07-19 12:16:46 -0700307 */
308static void hostapd_cleanup(struct hostapd_data *hapd)
309{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800310 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
311 hapd->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700312 if (hapd->iface->interfaces &&
313 hapd->iface->interfaces->ctrl_iface_deinit)
314 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700315 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316}
317
318
Dmitry Shmidt04949592012-07-19 12:16:46 -0700319static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
320{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800321 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700322 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
323 iface->hw_features = NULL;
324 os_free(iface->current_rates);
325 iface->current_rates = NULL;
326 os_free(iface->basic_rates);
327 iface->basic_rates = NULL;
328 ap_list_deinit(iface);
329}
330
331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332/**
333 * hostapd_cleanup_iface - Complete per-interface cleanup
334 * @iface: Pointer to interface data
335 *
336 * This function is called after per-BSS data structures are deinitialized
337 * with hostapd_cleanup().
338 */
339static void hostapd_cleanup_iface(struct hostapd_iface *iface)
340{
Dmitry Shmidt54605472013-11-08 11:10:19 -0800341 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800342 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
343
Dmitry Shmidt04949592012-07-19 12:16:46 -0700344 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 hostapd_config_free(iface->conf);
346 iface->conf = NULL;
347
348 os_free(iface->config_fname);
349 os_free(iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -0800350 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 os_free(iface);
352}
353
354
Dmitry Shmidt04949592012-07-19 12:16:46 -0700355static void hostapd_clear_wep(struct hostapd_data *hapd)
356{
357 if (hapd->drv_priv) {
358 hostapd_set_privacy(hapd, 0);
359 hostapd_broadcast_wep_clear(hapd);
360 }
361}
362
363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
365{
366 int i;
367
368 hostapd_broadcast_wep_set(hapd);
369
370 if (hapd->conf->ssid.wep.default_len) {
371 hostapd_set_privacy(hapd, 1);
372 return 0;
373 }
374
Jouni Malinen75ecf522011-06-27 15:19:46 -0700375 /*
376 * When IEEE 802.1X is not enabled, the driver may need to know how to
377 * set authentication algorithms for static WEP.
378 */
379 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 for (i = 0; i < 4; i++) {
382 if (hapd->conf->ssid.wep.key[i] &&
383 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
384 i == hapd->conf->ssid.wep.idx, NULL, 0,
385 hapd->conf->ssid.wep.key[i],
386 hapd->conf->ssid.wep.len[i])) {
387 wpa_printf(MSG_WARNING, "Could not set WEP "
388 "encryption.");
389 return -1;
390 }
391 if (hapd->conf->ssid.wep.key[i] &&
392 i == hapd->conf->ssid.wep.idx)
393 hostapd_set_privacy(hapd, 1);
394 }
395
396 return 0;
397}
398
399
Dmitry Shmidt04949592012-07-19 12:16:46 -0700400static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401{
402 int ret = 0;
403 u8 addr[ETH_ALEN];
404
405 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
406 return 0;
407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800408 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 if (hostapd_flush(hapd)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800410 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
411 "kernel driver");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 ret = -1;
413 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700416 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 hostapd_free_stas(hapd);
418
419 return ret;
420}
421
422
423/**
424 * hostapd_validate_bssid_configuration - Validate BSSID configuration
425 * @iface: Pointer to interface data
426 * Returns: 0 on success, -1 on failure
427 *
428 * This function is used to validate that the configured BSSIDs are valid.
429 */
430static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
431{
432 u8 mask[ETH_ALEN] = { 0 };
433 struct hostapd_data *hapd = iface->bss[0];
434 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 int auto_addr = 0;
436
437 if (hostapd_drv_none(hapd))
438 return 0;
439
440 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
441
442 /* Determine the bits necessary to cover the number of BSSIDs. */
443 for (i--; i; i >>= 1)
444 bits++;
445
446 /* Determine the bits necessary to any configured BSSIDs,
447 if they are higher than the number of BSSIDs. */
448 for (j = 0; j < iface->conf->num_bss; j++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800449 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 if (j)
451 auto_addr++;
452 continue;
453 }
454
455 for (i = 0; i < ETH_ALEN; i++) {
456 mask[i] |=
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800457 iface->conf->bss[j]->bssid[i] ^
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 hapd->own_addr[i];
459 }
460 }
461
462 if (!auto_addr)
463 goto skip_mask_ext;
464
465 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
466 ;
467 j = 0;
468 if (i < ETH_ALEN) {
469 j = (5 - i) * 8;
470
471 while (mask[i] != 0) {
472 mask[i] >>= 1;
473 j++;
474 }
475 }
476
477 if (bits < j)
478 bits = j;
479
480 if (bits > 40) {
481 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
482 bits);
483 return -1;
484 }
485
486 os_memset(mask, 0xff, ETH_ALEN);
487 j = bits / 8;
488 for (i = 5; i > 5 - j; i--)
489 mask[i] = 0;
490 j = bits % 8;
491 while (j--)
492 mask[i] <<= 1;
493
494skip_mask_ext:
495 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
496 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 if (!auto_addr)
499 return 0;
500
501 for (i = 0; i < ETH_ALEN; i++) {
502 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
503 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
504 " for start address " MACSTR ".",
505 MAC2STR(mask), MAC2STR(hapd->own_addr));
506 wpa_printf(MSG_ERROR, "Start address must be the "
507 "first address in the block (i.e., addr "
508 "AND mask == addr).");
509 return -1;
510 }
511 }
512
513 return 0;
514}
515
516
517static int mac_in_conf(struct hostapd_config *conf, const void *a)
518{
519 size_t i;
520
521 for (i = 0; i < conf->num_bss; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800522 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 return 1;
524 }
525 }
526
527 return 0;
528}
529
530
Dmitry Shmidt04949592012-07-19 12:16:46 -0700531#ifndef CONFIG_NO_RADIUS
532
533static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
534 struct radius_das_attrs *attr)
535{
536 /* TODO */
537 return 0;
538}
539
540
541static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
542 struct radius_das_attrs *attr)
543{
544 struct sta_info *sta = NULL;
545 char buf[128];
546
547 if (attr->sta_addr)
548 sta = ap_get_sta(hapd, attr->sta_addr);
549
550 if (sta == NULL && attr->acct_session_id &&
551 attr->acct_session_id_len == 17) {
552 for (sta = hapd->sta_list; sta; sta = sta->next) {
553 os_snprintf(buf, sizeof(buf), "%08X-%08X",
554 sta->acct_session_id_hi,
555 sta->acct_session_id_lo);
556 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
557 break;
558 }
559 }
560
561 if (sta == NULL && attr->cui) {
562 for (sta = hapd->sta_list; sta; sta = sta->next) {
563 struct wpabuf *cui;
564 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
565 if (cui && wpabuf_len(cui) == attr->cui_len &&
566 os_memcmp(wpabuf_head(cui), attr->cui,
567 attr->cui_len) == 0)
568 break;
569 }
570 }
571
572 if (sta == NULL && attr->user_name) {
573 for (sta = hapd->sta_list; sta; sta = sta->next) {
574 u8 *identity;
575 size_t identity_len;
576 identity = ieee802_1x_get_identity(sta->eapol_sm,
577 &identity_len);
578 if (identity &&
579 identity_len == attr->user_name_len &&
580 os_memcmp(identity, attr->user_name, identity_len)
581 == 0)
582 break;
583 }
584 }
585
586 return sta;
587}
588
589
590static enum radius_das_res
591hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
592{
593 struct hostapd_data *hapd = ctx;
594 struct sta_info *sta;
595
596 if (hostapd_das_nas_mismatch(hapd, attr))
597 return RADIUS_DAS_NAS_MISMATCH;
598
599 sta = hostapd_das_find_sta(hapd, attr);
600 if (sta == NULL)
601 return RADIUS_DAS_SESSION_NOT_FOUND;
602
603 hostapd_drv_sta_deauth(hapd, sta->addr,
604 WLAN_REASON_PREV_AUTH_NOT_VALID);
605 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
606
607 return RADIUS_DAS_SUCCESS;
608}
609
610#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611
612
613/**
614 * hostapd_setup_bss - Per-BSS setup (initialization)
615 * @hapd: Pointer to BSS data
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800616 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
617 * but interface may exist
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 *
619 * This function is used to initialize all per-BSS data structures and
620 * resources. This gets called in a loop for each BSS when an interface is
621 * initialized. Most of the modules that are initialized here will be
622 * deinitialized in hostapd_cleanup().
623 */
624static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
625{
626 struct hostapd_bss_config *conf = hapd->conf;
627 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
628 int ssid_len, set_ssid;
629 char force_ifname[IFNAMSIZ];
630 u8 if_addr[ETH_ALEN];
631
Dmitry Shmidt54605472013-11-08 11:10:19 -0800632 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
633 __func__, hapd, hapd->conf->iface, first);
634
635 if (hapd->started) {
636 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
637 __func__, hapd->conf->iface);
638 return -1;
639 }
640 hapd->started = 1;
641
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800642 if (!first || first == -1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
644 /* Allocate the next available BSSID. */
645 do {
646 inc_byte_array(hapd->own_addr, ETH_ALEN);
647 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
648 } else {
649 /* Allocate the configured BSSID. */
650 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
651
652 if (hostapd_mac_comp(hapd->own_addr,
653 hapd->iface->bss[0]->own_addr) ==
654 0) {
655 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
656 "BSSID set to the MAC address of "
657 "the radio", hapd->conf->iface);
658 return -1;
659 }
660 }
661
Dmitry Shmidt54605472013-11-08 11:10:19 -0800662 hapd->interface_added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
664 hapd->conf->iface, hapd->own_addr, hapd,
665 &hapd->drv_priv, force_ifname, if_addr,
666 hapd->conf->bridge[0] ? hapd->conf->bridge :
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800667 NULL, first == -1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
669 MACSTR ")", MAC2STR(hapd->own_addr));
670 return -1;
671 }
672 }
673
674 if (conf->wmm_enabled < 0)
675 conf->wmm_enabled = hapd->iconf->ieee80211n;
676
Dmitry Shmidt04949592012-07-19 12:16:46 -0700677 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 hostapd_set_privacy(hapd, 0);
679
680 hostapd_broadcast_wep_clear(hapd);
681 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
682 return -1;
683
684 /*
685 * Fetch the SSID from the system and use it or,
686 * if one was specified in the config file, verify they
687 * match.
688 */
689 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
690 if (ssid_len < 0) {
691 wpa_printf(MSG_ERROR, "Could not read SSID from system");
692 return -1;
693 }
694 if (conf->ssid.ssid_set) {
695 /*
696 * If SSID is specified in the config file and it differs
697 * from what is being used then force installation of the
698 * new SSID.
699 */
700 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
701 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
702 } else {
703 /*
704 * No SSID in the config file; just use the one we got
705 * from the system.
706 */
707 set_ssid = 0;
708 conf->ssid.ssid_len = ssid_len;
709 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 }
711
712 if (!hostapd_drv_none(hapd)) {
713 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700714 " and ssid \"%s\"",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 hapd->conf->iface, MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700716 wpa_ssid_txt(hapd->conf->ssid.ssid,
717 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 }
719
720 if (hostapd_setup_wpa_psk(conf)) {
721 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
722 return -1;
723 }
724
725 /* Set SSID for the kernel driver (to be used in beacon and probe
726 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700727 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 conf->ssid.ssid_len)) {
729 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
730 return -1;
731 }
732
733 if (wpa_debug_level == MSG_MSGDUMP)
734 conf->radius->msg_dumps = 1;
735#ifndef CONFIG_NO_RADIUS
736 hapd->radius = radius_client_init(hapd, conf->radius);
737 if (hapd->radius == NULL) {
738 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
739 return -1;
740 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700741
742 if (hapd->conf->radius_das_port) {
743 struct radius_das_conf das_conf;
744 os_memset(&das_conf, 0, sizeof(das_conf));
745 das_conf.port = hapd->conf->radius_das_port;
746 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
747 das_conf.shared_secret_len =
748 hapd->conf->radius_das_shared_secret_len;
749 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
750 das_conf.time_window = hapd->conf->radius_das_time_window;
751 das_conf.require_event_timestamp =
752 hapd->conf->radius_das_require_event_timestamp;
753 das_conf.ctx = hapd;
754 das_conf.disconnect = hostapd_das_disconnect;
755 hapd->radius_das = radius_das_init(&das_conf);
756 if (hapd->radius_das == NULL) {
757 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
758 "failed.");
759 return -1;
760 }
761 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762#endif /* CONFIG_NO_RADIUS */
763
764 if (hostapd_acl_init(hapd)) {
765 wpa_printf(MSG_ERROR, "ACL initialization failed.");
766 return -1;
767 }
768 if (hostapd_init_wps(hapd, conf))
769 return -1;
770
771 if (authsrv_init(hapd) < 0)
772 return -1;
773
774 if (ieee802_1x_init(hapd)) {
775 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
776 return -1;
777 }
778
779 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
780 return -1;
781
782 if (accounting_init(hapd)) {
783 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
784 return -1;
785 }
786
787 if (hapd->conf->ieee802_11f &&
788 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
789 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
790 "failed.");
791 return -1;
792 }
793
Dmitry Shmidt04949592012-07-19 12:16:46 -0700794#ifdef CONFIG_INTERWORKING
795 if (gas_serv_init(hapd)) {
796 wpa_printf(MSG_ERROR, "GAS server initialization failed");
797 return -1;
798 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700799
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800800 if (conf->qos_map_set_len &&
801 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
802 conf->qos_map_set_len)) {
803 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 return -1;
805 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800806#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807
808 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
809 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
810 return -1;
811 }
812
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700813 if (!hapd->conf->start_disabled)
814 ieee802_11_set_beacon(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
817 return -1;
818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 if (hapd->driver && hapd->driver->set_operstate)
820 hapd->driver->set_operstate(hapd->drv_priv, 1);
821
822 return 0;
823}
824
825
826static void hostapd_tx_queue_params(struct hostapd_iface *iface)
827{
828 struct hostapd_data *hapd = iface->bss[0];
829 int i;
830 struct hostapd_tx_queue_params *p;
831
832 for (i = 0; i < NUM_TX_QUEUES; i++) {
833 p = &iface->conf->tx_queue[i];
834
835 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
836 p->cwmax, p->burst)) {
837 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
838 "parameters for queue %d.", i);
839 /* Continue anyway */
840 }
841 }
842}
843
844
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700845static int hostapd_set_acl_list(struct hostapd_data *hapd,
846 struct mac_acl_entry *mac_acl,
847 int n_entries, u8 accept_acl)
848{
849 struct hostapd_acl_params *acl_params;
850 int i, err;
851
852 acl_params = os_zalloc(sizeof(*acl_params) +
853 (n_entries * sizeof(acl_params->mac_acl[0])));
854 if (!acl_params)
855 return -ENOMEM;
856
857 for (i = 0; i < n_entries; i++)
858 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
859 ETH_ALEN);
860
861 acl_params->acl_policy = accept_acl;
862 acl_params->num_mac_acl = n_entries;
863
864 err = hostapd_drv_set_acl(hapd, acl_params);
865
866 os_free(acl_params);
867
868 return err;
869}
870
871
872static void hostapd_set_acl(struct hostapd_data *hapd)
873{
874 struct hostapd_config *conf = hapd->iconf;
875 int err;
876 u8 accept_acl;
877
878 if (hapd->iface->drv_max_acl_mac_addrs == 0)
879 return;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800880 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700881 return;
882
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800883 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
884 if (conf->bss[0]->num_accept_mac) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700885 accept_acl = 1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800886 err = hostapd_set_acl_list(hapd,
887 conf->bss[0]->accept_mac,
888 conf->bss[0]->num_accept_mac,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700889 accept_acl);
890 if (err) {
891 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
892 return;
893 }
894 } else {
895 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
896 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800897 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
898 if (conf->bss[0]->num_deny_mac) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700899 accept_acl = 0;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800900 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
901 conf->bss[0]->num_deny_mac,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700902 accept_acl);
903 if (err) {
904 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
905 return;
906 }
907 } else {
908 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
909 }
910 }
911}
912
913
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800914static int start_ctrl_iface_bss(struct hostapd_data *hapd)
915{
916 if (!hapd->iface->interfaces ||
917 !hapd->iface->interfaces->ctrl_iface_init)
918 return 0;
919
920 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
921 wpa_printf(MSG_ERROR,
922 "Failed to setup control interface for %s",
923 hapd->conf->iface);
924 return -1;
925 }
926
927 return 0;
928}
929
930
931static int start_ctrl_iface(struct hostapd_iface *iface)
932{
933 size_t i;
934
935 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
936 return 0;
937
938 for (i = 0; i < iface->num_bss; i++) {
939 struct hostapd_data *hapd = iface->bss[i];
940 if (iface->interfaces->ctrl_iface_init(hapd)) {
941 wpa_printf(MSG_ERROR,
942 "Failed to setup control interface for %s",
943 hapd->conf->iface);
944 return -1;
945 }
946 }
947
948 return 0;
949}
950
951
952static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
953{
954 struct hostapd_iface *iface = eloop_ctx;
955
956 if (!iface->wait_channel_update) {
957 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
958 return;
959 }
960
961 /*
962 * It is possible that the existing channel list is acceptable, so try
963 * to proceed.
964 */
965 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
966 setup_interface2(iface);
967}
968
969
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800970void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800971{
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800972 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800973 return;
974
975 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
976 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
977 setup_interface2(iface);
978}
979
980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981static int setup_interface(struct hostapd_iface *iface)
982{
983 struct hostapd_data *hapd = iface->bss[0];
984 size_t i;
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800985
986 if (!iface->phy[0]) {
987 const char *phy = hostapd_drv_get_radio_name(hapd);
988 if (phy) {
989 wpa_printf(MSG_DEBUG, "phy: %s", phy);
990 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
991 }
992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993
994 /*
995 * Make sure that all BSSes get configured with a pointer to the same
996 * driver interface.
997 */
998 for (i = 1; i < iface->num_bss; i++) {
999 iface->bss[i]->driver = hapd->driver;
1000 iface->bss[i]->drv_priv = hapd->drv_priv;
1001 }
1002
1003 if (hostapd_validate_bssid_configuration(iface))
1004 return -1;
1005
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001006 /*
1007 * Initialize control interfaces early to allow external monitoring of
1008 * channel setup operations that may take considerable amount of time
1009 * especially for DFS cases.
1010 */
1011 if (start_ctrl_iface(iface))
1012 return -1;
1013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001015 char country[4], previous_country[4];
1016
1017 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1018 if (hostapd_get_country(hapd, previous_country) < 0)
1019 previous_country[0] = '\0';
1020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 os_memcpy(country, hapd->iconf->country, 3);
1022 country[3] = '\0';
1023 if (hostapd_set_country(hapd, country) < 0) {
1024 wpa_printf(MSG_ERROR, "Failed to set country code");
1025 return -1;
1026 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001027
1028 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1029 previous_country, country);
1030
1031 if (os_strncmp(previous_country, country, 2) != 0) {
1032 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1033 iface->wait_channel_update = 1;
1034 eloop_register_timeout(1, 0,
1035 channel_list_update_timeout,
1036 iface, NULL);
1037 return 0;
1038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 }
1040
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001041 return setup_interface2(iface);
1042}
1043
1044
1045static int setup_interface2(struct hostapd_iface *iface)
1046{
1047 iface->wait_channel_update = 0;
1048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 if (hostapd_get_hw_features(iface)) {
1050 /* Not all drivers support this yet, so continue without hw
1051 * feature data. */
1052 } else {
1053 int ret = hostapd_select_hw_mode(iface);
1054 if (ret < 0) {
1055 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1056 "channel. (%d)", ret);
1057 return -1;
1058 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001059 if (ret == 1) {
1060 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1061 return 0;
1062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 ret = hostapd_check_ht_capab(iface);
1064 if (ret < 0)
1065 return -1;
1066 if (ret == 1) {
1067 wpa_printf(MSG_DEBUG, "Interface initialization will "
1068 "be completed in a callback");
1069 return 0;
1070 }
Dmitry Shmidt051af732013-10-22 13:52:46 -07001071
1072 if (iface->conf->ieee80211h)
1073 wpa_printf(MSG_DEBUG, "DFS support is enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 }
1075 return hostapd_setup_interface_complete(iface, 0);
1076}
1077
1078
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001079/**
1080 * hostapd_setup_interface_complete - Complete interface setup
1081 *
1082 * This function is called when previous steps in the interface setup has been
1083 * completed. This can also start operations, e.g., DFS, that will require
1084 * additional processing before interface is ready to be enabled. Such
1085 * operations will call this function from eloop callbacks when finished.
1086 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1088{
1089 struct hostapd_data *hapd = iface->bss[0];
1090 size_t j;
1091 u8 *prev_addr;
1092
1093 if (err) {
1094 wpa_printf(MSG_ERROR, "Interface initialization failed");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001095 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001096 if (iface->interfaces && iface->interfaces->terminate_on_error)
1097 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 return -1;
1099 }
1100
1101 wpa_printf(MSG_DEBUG, "Completing interface initialization");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001102 if (iface->conf->channel) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07001103#ifdef NEED_AP_MLME
1104 int res;
1105#endif /* NEED_AP_MLME */
1106
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001107 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1109 "Frequency: %d MHz",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001110 hostapd_hw_mode_txt(iface->conf->hw_mode),
1111 iface->conf->channel, iface->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
Dmitry Shmidt051af732013-10-22 13:52:46 -07001113#ifdef NEED_AP_MLME
1114 /* Check DFS */
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001115 res = hostapd_handle_dfs(iface);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001116 if (res <= 0)
1117 return res;
1118#endif /* NEED_AP_MLME */
1119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1121 hapd->iconf->channel,
1122 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001123 hapd->iconf->ieee80211ac,
1124 hapd->iconf->secondary_channel,
1125 hapd->iconf->vht_oper_chwidth,
1126 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1127 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 wpa_printf(MSG_ERROR, "Could not set channel for "
1129 "kernel driver");
1130 return -1;
1131 }
1132 }
1133
1134 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1137 "table.");
1138 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1139 HOSTAPD_LEVEL_WARNING,
1140 "Failed to prepare rates table.");
1141 return -1;
1142 }
1143 }
1144
1145 if (hapd->iconf->rts_threshold > -1 &&
1146 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1147 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1148 "kernel driver");
1149 return -1;
1150 }
1151
1152 if (hapd->iconf->fragm_threshold > -1 &&
1153 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1154 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1155 "for kernel driver");
1156 return -1;
1157 }
1158
1159 prev_addr = hapd->own_addr;
1160
1161 for (j = 0; j < iface->num_bss; j++) {
1162 hapd = iface->bss[j];
1163 if (j)
1164 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1165 if (hostapd_setup_bss(hapd, j == 0))
1166 return -1;
1167 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1168 prev_addr = hapd->own_addr;
1169 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001170 hapd = iface->bss[0];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171
1172 hostapd_tx_queue_params(iface);
1173
1174 ap_list_init(iface);
1175
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001176 hostapd_set_acl(hapd);
1177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 if (hostapd_driver_commit(hapd) < 0) {
1179 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1180 "configuration", __func__);
1181 return -1;
1182 }
1183
Jouni Malinen87fd2792011-05-16 18:35:42 +03001184 /*
1185 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1186 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1187 * mode), the interface is up only after driver_commit, so initialize
1188 * WPS after driver_commit.
1189 */
1190 for (j = 0; j < iface->num_bss; j++) {
1191 if (hostapd_init_wps_complete(iface->bss[j]))
1192 return -1;
1193 }
1194
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001195 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1196 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 if (hapd->setup_complete_cb)
1198 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1199
1200 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1201 iface->bss[0]->conf->iface);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08001202 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1203 iface->interfaces->terminate_on_error--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204
1205 return 0;
1206}
1207
1208
1209/**
1210 * hostapd_setup_interface - Setup of an interface
1211 * @iface: Pointer to interface data.
1212 * Returns: 0 on success, -1 on failure
1213 *
1214 * Initializes the driver interface, validates the configuration,
1215 * and sets driver parameters based on the configuration.
1216 * Flushes old stations, sets the channel, encryption,
1217 * beacons, and WDS links based on the configuration.
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001218 *
1219 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1220 * or DFS operations, this function returns 0 before such operations have been
1221 * completed. The pending operations are registered into eloop and will be
1222 * completed from eloop callbacks. Those callbacks end up calling
1223 * hostapd_setup_interface_complete() once setup has been completed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 */
1225int hostapd_setup_interface(struct hostapd_iface *iface)
1226{
1227 int ret;
1228
1229 ret = setup_interface(iface);
1230 if (ret) {
1231 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1232 iface->bss[0]->conf->iface);
1233 return -1;
1234 }
1235
1236 return 0;
1237}
1238
1239
1240/**
1241 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1242 * @hapd_iface: Pointer to interface data
1243 * @conf: Pointer to per-interface configuration
1244 * @bss: Pointer to per-BSS configuration for this BSS
1245 * Returns: Pointer to allocated BSS data
1246 *
1247 * This function is used to allocate per-BSS data structure. This data will be
1248 * freed after hostapd_cleanup() is called for it during interface
1249 * deinitialization.
1250 */
1251struct hostapd_data *
1252hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1253 struct hostapd_config *conf,
1254 struct hostapd_bss_config *bss)
1255{
1256 struct hostapd_data *hapd;
1257
1258 hapd = os_zalloc(sizeof(*hapd));
1259 if (hapd == NULL)
1260 return NULL;
1261
1262 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1263 hapd->iconf = conf;
1264 hapd->conf = bss;
1265 hapd->iface = hapd_iface;
1266 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001267 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268
1269 return hapd;
1270}
1271
1272
Dmitry Shmidt54605472013-11-08 11:10:19 -08001273static void hostapd_bss_deinit(struct hostapd_data *hapd)
1274{
1275 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1276 hapd->conf->iface);
1277 hostapd_free_stas(hapd);
1278 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1279 hostapd_clear_wep(hapd);
1280 hostapd_cleanup(hapd);
1281}
1282
1283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284void hostapd_interface_deinit(struct hostapd_iface *iface)
1285{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001286 int j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287
Dmitry Shmidt54605472013-11-08 11:10:19 -08001288 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 if (iface == NULL)
1290 return;
1291
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001292 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1293 iface->wait_channel_update = 0;
1294
Dmitry Shmidt54605472013-11-08 11:10:19 -08001295 for (j = iface->num_bss - 1; j >= 0; j--)
1296 hostapd_bss_deinit(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297}
1298
1299
1300void hostapd_interface_free(struct hostapd_iface *iface)
1301{
1302 size_t j;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001303 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1304 for (j = 0; j < iface->num_bss; j++) {
1305 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1306 __func__, iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 os_free(iface->bss[j]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 hostapd_cleanup_iface(iface);
1310}
1311
1312
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001313/**
1314 * hostapd_init - Allocate and initialize per-interface data
1315 * @config_file: Path to the configuration file
1316 * Returns: Pointer to the allocated interface data or %NULL on failure
1317 *
1318 * This function is used to allocate main data structures for per-interface
1319 * data. The allocated data buffer will be freed by calling
1320 * hostapd_cleanup_iface().
1321 */
1322struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1323 const char *config_file)
1324{
1325 struct hostapd_iface *hapd_iface = NULL;
1326 struct hostapd_config *conf = NULL;
1327 struct hostapd_data *hapd;
1328 size_t i;
1329
1330 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1331 if (hapd_iface == NULL)
1332 goto fail;
1333
1334 hapd_iface->config_fname = os_strdup(config_file);
1335 if (hapd_iface->config_fname == NULL)
1336 goto fail;
1337
1338 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1339 if (conf == NULL)
1340 goto fail;
1341 hapd_iface->conf = conf;
1342
1343 hapd_iface->num_bss = conf->num_bss;
1344 hapd_iface->bss = os_calloc(conf->num_bss,
1345 sizeof(struct hostapd_data *));
1346 if (hapd_iface->bss == NULL)
1347 goto fail;
1348
1349 for (i = 0; i < conf->num_bss; i++) {
1350 hapd = hapd_iface->bss[i] =
1351 hostapd_alloc_bss_data(hapd_iface, conf,
1352 conf->bss[i]);
1353 if (hapd == NULL)
1354 goto fail;
1355 hapd->msg_ctx = hapd;
1356 }
1357
1358 return hapd_iface;
1359
1360fail:
1361 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1362 config_file);
1363 if (conf)
1364 hostapd_config_free(conf);
1365 if (hapd_iface) {
1366 os_free(hapd_iface->config_fname);
1367 os_free(hapd_iface->bss);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001368 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1369 __func__, hapd_iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001370 os_free(hapd_iface);
1371 }
1372 return NULL;
1373}
1374
1375
1376static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1377{
1378 size_t i, j;
1379
1380 for (i = 0; i < interfaces->count; i++) {
1381 struct hostapd_iface *iface = interfaces->iface[i];
1382 for (j = 0; j < iface->num_bss; j++) {
1383 struct hostapd_data *hapd = iface->bss[j];
1384 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1385 return 1;
1386 }
1387 }
1388
1389 return 0;
1390}
1391
1392
1393/**
1394 * hostapd_interface_init_bss - Read configuration file and init BSS data
1395 *
1396 * This function is used to parse configuration file for a BSS. This BSS is
1397 * added to an existing interface sharing the same radio (if any) or a new
1398 * interface is created if this is the first interface on a radio. This
1399 * allocate memory for the BSS. No actual driver operations are started.
1400 *
1401 * This is similar to hostapd_interface_init(), but for a case where the
1402 * configuration is used to add a single BSS instead of all BSSes for a radio.
1403 */
1404struct hostapd_iface *
1405hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1406 const char *config_fname, int debug)
1407{
1408 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1409 struct hostapd_data *hapd;
1410 int k;
1411 size_t i, bss_idx;
1412
1413 if (!phy || !*phy)
1414 return NULL;
1415
1416 for (i = 0; i < interfaces->count; i++) {
1417 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1418 iface = interfaces->iface[i];
1419 break;
1420 }
1421 }
1422
1423 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1424 config_fname, phy, iface ? "" : " --> new PHY");
1425 if (iface) {
1426 struct hostapd_config *conf;
1427 struct hostapd_bss_config **tmp_conf;
1428 struct hostapd_data **tmp_bss;
1429 struct hostapd_bss_config *bss;
1430 const char *ifname;
1431
1432 /* Add new BSS to existing iface */
1433 conf = interfaces->config_read_cb(config_fname);
1434 if (conf == NULL)
1435 return NULL;
1436 if (conf->num_bss > 1) {
1437 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1438 hostapd_config_free(conf);
1439 return NULL;
1440 }
1441
1442 ifname = conf->bss[0]->iface;
1443 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1444 wpa_printf(MSG_ERROR,
1445 "Interface name %s already in use", ifname);
1446 hostapd_config_free(conf);
1447 return NULL;
1448 }
1449
1450 tmp_conf = os_realloc_array(
1451 iface->conf->bss, iface->conf->num_bss + 1,
1452 sizeof(struct hostapd_bss_config *));
1453 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1454 sizeof(struct hostapd_data *));
1455 if (tmp_bss)
1456 iface->bss = tmp_bss;
1457 if (tmp_conf) {
1458 iface->conf->bss = tmp_conf;
1459 iface->conf->last_bss = tmp_conf[0];
1460 }
1461 if (tmp_bss == NULL || tmp_conf == NULL) {
1462 hostapd_config_free(conf);
1463 return NULL;
1464 }
1465 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1466 iface->conf->num_bss++;
1467
1468 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1469 if (hapd == NULL) {
1470 iface->conf->num_bss--;
1471 hostapd_config_free(conf);
1472 return NULL;
1473 }
1474 iface->conf->last_bss = bss;
1475 iface->bss[iface->num_bss] = hapd;
1476 hapd->msg_ctx = hapd;
1477
1478 bss_idx = iface->num_bss++;
1479 conf->num_bss--;
1480 conf->bss[0] = NULL;
1481 hostapd_config_free(conf);
1482 } else {
1483 /* Add a new iface with the first BSS */
1484 new_iface = iface = hostapd_init(interfaces, config_fname);
1485 if (!iface)
1486 return NULL;
1487 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1488 iface->interfaces = interfaces;
1489 bss_idx = 0;
1490 }
1491
1492 for (k = 0; k < debug; k++) {
1493 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1494 iface->bss[bss_idx]->conf->logger_stdout_level--;
1495 }
1496
1497 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1498 !hostapd_drv_none(iface->bss[bss_idx])) {
1499 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1500 config_fname);
1501 if (new_iface)
1502 hostapd_interface_deinit_free(new_iface);
1503 return NULL;
1504 }
1505
1506 return iface;
1507}
1508
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001509
1510void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1511{
1512 const struct wpa_driver_ops *driver;
1513 void *drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001514
1515 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001516 if (iface == NULL)
1517 return;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001518 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1519 __func__, (unsigned int) iface->num_bss,
1520 (unsigned int) iface->conf->num_bss);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001521 driver = iface->bss[0]->driver;
1522 drv_priv = iface->bss[0]->drv_priv;
1523 hostapd_interface_deinit(iface);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001524 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1525 __func__, driver, drv_priv);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001526 if (driver && driver->hapd_deinit && drv_priv)
1527 driver->hapd_deinit(drv_priv);
1528 hostapd_interface_free(iface);
1529}
1530
1531
1532int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1533{
1534 if (hapd_iface->bss[0]->drv_priv != NULL) {
1535 wpa_printf(MSG_ERROR, "Interface %s already enabled",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001536 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001537 return -1;
1538 }
1539
1540 wpa_printf(MSG_DEBUG, "Enable interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001541 hapd_iface->conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001542
1543 if (hapd_iface->interfaces == NULL ||
1544 hapd_iface->interfaces->driver_init == NULL ||
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001545 hapd_iface->interfaces->driver_init(hapd_iface))
1546 return -1;
1547
1548 if (hostapd_setup_interface(hapd_iface)) {
1549 const struct wpa_driver_ops *driver;
1550 void *drv_priv;
1551
1552 driver = hapd_iface->bss[0]->driver;
1553 drv_priv = hapd_iface->bss[0]->drv_priv;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001554 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1555 __func__, driver, drv_priv);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556 if (driver && driver->hapd_deinit && drv_priv) {
1557 driver->hapd_deinit(drv_priv);
1558 hapd_iface->bss[0]->drv_priv = NULL;
1559 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001560 return -1;
1561 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001562
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001563 return 0;
1564}
1565
1566
1567int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1568{
1569 size_t j;
1570
1571 wpa_printf(MSG_DEBUG, "Reload interface %s",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001572 hapd_iface->conf->bss[0]->iface);
1573 for (j = 0; j < hapd_iface->num_bss; j++)
1574 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1575 if (hostapd_config_check(hapd_iface->conf) < 0) {
1576 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1577 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001578 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001579 hostapd_clear_old(hapd_iface);
1580 for (j = 0; j < hapd_iface->num_bss; j++)
1581 hostapd_reload_bss(hapd_iface->bss[j]);
1582
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001583 return 0;
1584}
1585
1586
1587int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1588{
1589 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001590 const struct wpa_driver_ops *driver;
1591 void *drv_priv;
1592
1593 if (hapd_iface == NULL)
1594 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001595 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001596 driver = hapd_iface->bss[0]->driver;
1597 drv_priv = hapd_iface->bss[0]->drv_priv;
1598
1599 /* whatever hostapd_interface_deinit does */
1600 for (j = 0; j < hapd_iface->num_bss; j++) {
1601 struct hostapd_data *hapd = hapd_iface->bss[j];
1602 hostapd_free_stas(hapd);
1603 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1604 hostapd_clear_wep(hapd);
1605 hostapd_free_hapd_data(hapd);
1606 }
1607
Dmitry Shmidt54605472013-11-08 11:10:19 -08001608 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1609 __func__, driver, drv_priv);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001610 if (driver && driver->hapd_deinit && drv_priv) {
1611 driver->hapd_deinit(drv_priv);
1612 hapd_iface->bss[0]->drv_priv = NULL;
1613 }
1614
1615 /* From hostapd_cleanup_iface: These were initialized in
1616 * hostapd_setup_interface and hostapd_setup_interface_complete
1617 */
1618 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001619
Dmitry Shmidt56052862013-10-04 10:23:25 -07001620 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1621 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001622 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001623 return 0;
1624}
1625
1626
1627static struct hostapd_iface *
1628hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1629{
1630 struct hostapd_iface **iface, *hapd_iface;
1631
1632 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1633 sizeof(struct hostapd_iface *));
1634 if (iface == NULL)
1635 return NULL;
1636 interfaces->iface = iface;
1637 hapd_iface = interfaces->iface[interfaces->count] =
1638 os_zalloc(sizeof(*hapd_iface));
1639 if (hapd_iface == NULL) {
1640 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1641 "the interface", __func__);
1642 return NULL;
1643 }
1644 interfaces->count++;
1645 hapd_iface->interfaces = interfaces;
1646
1647 return hapd_iface;
1648}
1649
1650
1651static struct hostapd_config *
1652hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1653 const char *ctrl_iface)
1654{
1655 struct hostapd_bss_config *bss;
1656 struct hostapd_config *conf;
1657
1658 /* Allocates memory for bss and conf */
1659 conf = hostapd_config_defaults();
1660 if (conf == NULL) {
1661 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1662 "configuration", __func__);
1663 return NULL;
1664 }
1665
1666 conf->driver = wpa_drivers[0];
1667 if (conf->driver == NULL) {
1668 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1669 hostapd_config_free(conf);
1670 return NULL;
1671 }
1672
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001673 bss = conf->last_bss = conf->bss[0];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001674
1675 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1676 bss->ctrl_interface = os_strdup(ctrl_iface);
1677 if (bss->ctrl_interface == NULL) {
1678 hostapd_config_free(conf);
1679 return NULL;
1680 }
1681
1682 /* Reading configuration file skipped, will be done in SET!
1683 * From reading the configuration till the end has to be done in
1684 * SET
1685 */
1686 return conf;
1687}
1688
1689
1690static struct hostapd_iface * hostapd_data_alloc(
1691 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1692{
1693 size_t i;
1694 struct hostapd_iface *hapd_iface =
1695 interfaces->iface[interfaces->count - 1];
1696 struct hostapd_data *hapd;
1697
1698 hapd_iface->conf = conf;
1699 hapd_iface->num_bss = conf->num_bss;
1700
1701 hapd_iface->bss = os_zalloc(conf->num_bss *
1702 sizeof(struct hostapd_data *));
1703 if (hapd_iface->bss == NULL)
1704 return NULL;
1705
1706 for (i = 0; i < conf->num_bss; i++) {
1707 hapd = hapd_iface->bss[i] =
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001708 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001709 if (hapd == NULL)
1710 return NULL;
1711 hapd->msg_ctx = hapd;
1712 }
1713
1714 hapd_iface->interfaces = interfaces;
1715
1716 return hapd_iface;
1717}
1718
1719
1720int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1721{
1722 struct hostapd_config *conf = NULL;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001723 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1724 struct hostapd_data *hapd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001725 char *ptr;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001726 size_t i, j;
1727 const char *conf_file = NULL, *phy_name = NULL;
1728
1729 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1730 char *pos;
1731 phy_name = buf + 11;
1732 pos = os_strchr(phy_name, ':');
1733 if (!pos)
1734 return -1;
1735 *pos++ = '\0';
1736 conf_file = pos;
1737 if (!os_strlen(conf_file))
1738 return -1;
1739
1740 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1741 conf_file, 0);
1742 if (!hapd_iface)
1743 return -1;
1744 for (j = 0; j < interfaces->count; j++) {
1745 if (interfaces->iface[j] == hapd_iface)
1746 break;
1747 }
1748 if (j == interfaces->count) {
1749 struct hostapd_iface **tmp;
1750 tmp = os_realloc_array(interfaces->iface,
1751 interfaces->count + 1,
1752 sizeof(struct hostapd_iface *));
1753 if (!tmp) {
1754 hostapd_interface_deinit_free(hapd_iface);
1755 return -1;
1756 }
1757 interfaces->iface = tmp;
1758 interfaces->iface[interfaces->count++] = hapd_iface;
1759 new_iface = hapd_iface;
1760 }
1761
1762 if (new_iface) {
1763 if (interfaces->driver_init(hapd_iface) ||
1764 hostapd_setup_interface(hapd_iface)) {
1765 interfaces->count--;
1766 goto fail;
1767 }
1768 } else {
1769 /* Assign new BSS with bss[0]'s driver info */
1770 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1771 hapd->driver = hapd_iface->bss[0]->driver;
1772 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1773 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1774 ETH_ALEN);
1775
1776 if (start_ctrl_iface_bss(hapd) < 0 ||
Dmitry Shmidt54605472013-11-08 11:10:19 -08001777 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1778 hostapd_setup_bss(hapd, -1))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001779 hapd_iface->conf->num_bss--;
1780 hapd_iface->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001781 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1782 __func__, hapd, hapd->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001783 os_free(hapd);
1784 return -1;
1785 }
1786 }
1787 return 0;
1788 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001789
1790 ptr = os_strchr(buf, ' ');
1791 if (ptr == NULL)
1792 return -1;
1793 *ptr++ = '\0';
1794
Dmitry Shmidt56052862013-10-04 10:23:25 -07001795 if (os_strncmp(ptr, "config=", 7) == 0)
1796 conf_file = ptr + 7;
1797
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001798 for (i = 0; i < interfaces->count; i++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001799 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001800 buf)) {
1801 wpa_printf(MSG_INFO, "Cannot add interface - it "
1802 "already exists");
1803 return -1;
1804 }
1805 }
1806
1807 hapd_iface = hostapd_iface_alloc(interfaces);
1808 if (hapd_iface == NULL) {
1809 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1810 "for interface", __func__);
1811 goto fail;
1812 }
1813
Dmitry Shmidt56052862013-10-04 10:23:25 -07001814 if (conf_file && interfaces->config_read_cb) {
1815 conf = interfaces->config_read_cb(conf_file);
1816 if (conf && conf->bss)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001817 os_strlcpy(conf->bss[0]->iface, buf,
1818 sizeof(conf->bss[0]->iface));
Dmitry Shmidt56052862013-10-04 10:23:25 -07001819 } else
1820 conf = hostapd_config_alloc(interfaces, buf, ptr);
1821 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001822 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1823 "for configuration", __func__);
1824 goto fail;
1825 }
1826
1827 hapd_iface = hostapd_data_alloc(interfaces, conf);
1828 if (hapd_iface == NULL) {
1829 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1830 "for hostapd", __func__);
1831 goto fail;
1832 }
1833
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001834 if (start_ctrl_iface(hapd_iface) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001835 goto fail;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001836
1837 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001838
1839 return 0;
1840
1841fail:
1842 if (conf)
1843 hostapd_config_free(conf);
1844 if (hapd_iface) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001845 if (hapd_iface->bss) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08001846 for (i = 0; i < hapd_iface->num_bss; i++) {
1847 hapd = hapd_iface->bss[i];
1848 if (hapd && hapd_iface->interfaces &&
1849 hapd_iface->interfaces->ctrl_iface_deinit)
1850 hapd_iface->interfaces->
1851 ctrl_iface_deinit(hapd);
1852 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1853 __func__, hapd_iface->bss[i],
1854 hapd_iface->bss[i]->conf->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001855 os_free(hapd_iface->bss[i]);
Dmitry Shmidt54605472013-11-08 11:10:19 -08001856 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001857 os_free(hapd_iface->bss);
1858 }
Dmitry Shmidt54605472013-11-08 11:10:19 -08001859 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1860 __func__, hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001861 os_free(hapd_iface);
1862 }
1863 return -1;
1864}
1865
1866
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001867static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1868{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001869 size_t i;
1870
Dmitry Shmidt54605472013-11-08 11:10:19 -08001871 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001872
Dmitry Shmidt54605472013-11-08 11:10:19 -08001873 /* Remove hostapd_data only if it has already been initialized */
1874 if (idx < iface->num_bss) {
1875 struct hostapd_data *hapd = iface->bss[idx];
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001876
Dmitry Shmidt54605472013-11-08 11:10:19 -08001877 hostapd_bss_deinit(hapd);
1878 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
1879 __func__, hapd, hapd->conf->iface);
1880 hostapd_config_free_bss(hapd->conf);
1881 os_free(hapd);
1882
1883 iface->num_bss--;
1884
1885 for (i = idx; i < iface->num_bss; i++)
1886 iface->bss[i] = iface->bss[i + 1];
1887 } else {
1888 hostapd_config_free_bss(iface->conf->bss[idx]);
1889 iface->conf->bss[idx] = NULL;
1890 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001891
1892 iface->conf->num_bss--;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001893 for (i = idx; i < iface->conf->num_bss; i++)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001894 iface->conf->bss[i] = iface->conf->bss[i + 1];
1895
1896 return 0;
1897}
1898
1899
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001900int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1901{
1902 struct hostapd_iface *hapd_iface;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001903 size_t i, j, k = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904
1905 for (i = 0; i < interfaces->count; i++) {
1906 hapd_iface = interfaces->iface[i];
1907 if (hapd_iface == NULL)
1908 return -1;
Dmitry Shmidt54605472013-11-08 11:10:19 -08001909 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001910 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1911 hostapd_interface_deinit_free(hapd_iface);
1912 k = i;
1913 while (k < (interfaces->count - 1)) {
1914 interfaces->iface[k] =
1915 interfaces->iface[k + 1];
1916 k++;
1917 }
1918 interfaces->count--;
1919 return 0;
1920 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001921
1922 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1923 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf))
1924 return hostapd_remove_bss(hapd_iface, j);
1925 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001926 }
1927 return -1;
1928}
1929
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931/**
1932 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1933 * @hapd: Pointer to BSS data
1934 * @sta: Pointer to the associated STA data
1935 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1936 *
1937 * This function will be called whenever a station associates with the AP. It
1938 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1939 * from drv_callbacks.c based on driver events for drivers that take care of
1940 * management frames (IEEE 802.11 authentication and association) internally.
1941 */
1942void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1943 int reassoc)
1944{
1945 if (hapd->tkip_countermeasures) {
1946 hostapd_drv_sta_deauth(hapd, sta->addr,
1947 WLAN_REASON_MICHAEL_MIC_FAILURE);
1948 return;
1949 }
1950
1951 hostapd_prune_associations(hapd, sta->addr);
1952
1953 /* IEEE 802.11F (IAPP) */
1954 if (hapd->conf->ieee802_11f)
1955 iapp_new_station(hapd->iapp, sta);
1956
1957#ifdef CONFIG_P2P
1958 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1959 sta->no_p2p_set = 1;
1960 hapd->num_sta_no_p2p++;
1961 if (hapd->num_sta_no_p2p == 1)
1962 hostapd_p2p_non_p2p_sta_connected(hapd);
1963 }
1964#endif /* CONFIG_P2P */
1965
1966 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1967 * IEEE 802.1X/WPA code will start accounting after the station has
1968 * been authorized. */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001969 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1970 os_get_time(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001972 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973
1974 /* Start IEEE 802.1X authentication process for new stations */
1975 ieee802_1x_new_station(hapd, sta);
1976 if (reassoc) {
1977 if (sta->auth_alg != WLAN_AUTH_FT &&
1978 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1979 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1980 } else
1981 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001982
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001983 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
1984 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1985 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1986 __func__, MAC2STR(sta->addr),
1987 hapd->conf->ap_max_inactivity);
1988 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1989 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1990 ap_handle_timer, hapd, sta);
1991 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992}
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001993
1994
1995const char * hostapd_state_text(enum hostapd_iface_state s)
1996{
1997 switch (s) {
1998 case HAPD_IFACE_UNINITIALIZED:
1999 return "UNINITIALIZED";
2000 case HAPD_IFACE_DISABLED:
2001 return "DISABLED";
2002 case HAPD_IFACE_COUNTRY_UPDATE:
2003 return "COUNTRY_UPDATE";
2004 case HAPD_IFACE_ACS:
2005 return "ACS";
2006 case HAPD_IFACE_HT_SCAN:
2007 return "HT_SCAN";
2008 case HAPD_IFACE_DFS:
2009 return "DFS";
2010 case HAPD_IFACE_ENABLED:
2011 return "ENABLED";
2012 }
2013
2014 return "UNKNOWN";
2015}
2016
2017
2018void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2019{
2020 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2021 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2022 hostapd_state_text(s));
2023 iface->state = s;
2024}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002025
2026
2027#ifdef NEED_AP_MLME
2028
2029static void free_beacon_data(struct beacon_data *beacon)
2030{
2031 os_free(beacon->head);
2032 beacon->head = NULL;
2033 os_free(beacon->tail);
2034 beacon->tail = NULL;
2035 os_free(beacon->probe_resp);
2036 beacon->probe_resp = NULL;
2037 os_free(beacon->beacon_ies);
2038 beacon->beacon_ies = NULL;
2039 os_free(beacon->proberesp_ies);
2040 beacon->proberesp_ies = NULL;
2041 os_free(beacon->assocresp_ies);
2042 beacon->assocresp_ies = NULL;
2043}
2044
2045
2046static int hostapd_build_beacon_data(struct hostapd_iface *iface,
2047 struct beacon_data *beacon)
2048{
2049 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2050 struct wpa_driver_ap_params params;
2051 int ret;
2052 struct hostapd_data *hapd = iface->bss[0];
2053
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002054 os_memset(beacon, 0, sizeof(*beacon));
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002055 ret = ieee802_11_build_ap_params(hapd, &params);
2056 if (ret < 0)
2057 return ret;
2058
2059 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2060 &proberesp_extra,
2061 &assocresp_extra);
2062 if (ret)
2063 goto free_ap_params;
2064
2065 ret = -1;
2066 beacon->head = os_malloc(params.head_len);
2067 if (!beacon->head)
2068 goto free_ap_extra_ies;
2069
2070 os_memcpy(beacon->head, params.head, params.head_len);
2071 beacon->head_len = params.head_len;
2072
2073 beacon->tail = os_malloc(params.tail_len);
2074 if (!beacon->tail)
2075 goto free_beacon;
2076
2077 os_memcpy(beacon->tail, params.tail, params.tail_len);
2078 beacon->tail_len = params.tail_len;
2079
2080 if (params.proberesp != NULL) {
2081 beacon->probe_resp = os_malloc(params.proberesp_len);
2082 if (!beacon->probe_resp)
2083 goto free_beacon;
2084
2085 os_memcpy(beacon->probe_resp, params.proberesp,
2086 params.proberesp_len);
2087 beacon->probe_resp_len = params.proberesp_len;
2088 }
2089
2090 /* copy the extra ies */
2091 if (beacon_extra) {
2092 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2093 if (!beacon->beacon_ies)
2094 goto free_beacon;
2095
2096 os_memcpy(beacon->beacon_ies,
2097 beacon_extra->buf, wpabuf_len(beacon_extra));
2098 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2099 }
2100
2101 if (proberesp_extra) {
2102 beacon->proberesp_ies =
2103 os_malloc(wpabuf_len(proberesp_extra));
2104 if (!beacon->proberesp_ies)
2105 goto free_beacon;
2106
2107 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2108 wpabuf_len(proberesp_extra));
2109 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2110 }
2111
2112 if (assocresp_extra) {
2113 beacon->assocresp_ies =
2114 os_malloc(wpabuf_len(assocresp_extra));
2115 if (!beacon->assocresp_ies)
2116 goto free_beacon;
2117
2118 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2119 wpabuf_len(assocresp_extra));
2120 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2121 }
2122
2123 ret = 0;
2124free_beacon:
2125 /* if the function fails, the caller should not free beacon data */
2126 if (ret)
2127 free_beacon_data(beacon);
2128
2129free_ap_extra_ies:
2130 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2131 assocresp_extra);
2132free_ap_params:
2133 ieee802_11_free_ap_params(&params);
2134 return ret;
2135}
2136
2137
2138/*
2139 * TODO: This flow currently supports only changing frequency within the
2140 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2141 * width) are not supported.
2142 */
2143static int hostapd_change_config_freq(struct hostapd_data *hapd,
2144 struct hostapd_config *conf,
2145 struct hostapd_freq_params *params,
2146 struct hostapd_freq_params *old_params)
2147{
2148 int channel;
2149
2150 if (!params->channel) {
2151 /* check if the new channel is supported by hw */
2152 channel = hostapd_hw_get_channel(hapd, params->freq);
2153 if (!channel)
2154 return -1;
2155 } else {
2156 channel = params->channel;
2157 }
2158
2159 /* if a pointer to old_params is provided we save previous state */
2160 if (old_params) {
2161 old_params->channel = conf->channel;
2162 old_params->ht_enabled = conf->ieee80211n;
2163 old_params->sec_channel_offset = conf->secondary_channel;
2164 }
2165
2166 conf->channel = channel;
2167 conf->ieee80211n = params->ht_enabled;
2168 conf->secondary_channel = params->sec_channel_offset;
2169
2170 /* TODO: maybe call here hostapd_config_check here? */
2171
2172 return 0;
2173}
2174
2175
2176static int hostapd_fill_csa_settings(struct hostapd_iface *iface,
2177 struct csa_settings *settings)
2178{
2179 struct hostapd_freq_params old_freq;
2180 int ret;
2181
2182 os_memset(&old_freq, 0, sizeof(old_freq));
2183 if (!iface || !iface->freq || iface->csa_in_progress)
2184 return -1;
2185
2186 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2187 &settings->freq_params,
2188 &old_freq);
2189 if (ret)
2190 return ret;
2191
2192 ret = hostapd_build_beacon_data(iface, &settings->beacon_after);
2193
2194 /* change back the configuration */
2195 hostapd_change_config_freq(iface->bss[0], iface->conf,
2196 &old_freq, NULL);
2197
2198 if (ret)
2199 return ret;
2200
2201 /* set channel switch parameters for csa ie */
2202 iface->cs_freq = settings->freq_params.freq;
2203 iface->cs_count = settings->cs_count;
2204 iface->cs_block_tx = settings->block_tx;
2205
2206 ret = hostapd_build_beacon_data(iface, &settings->beacon_csa);
2207 if (ret) {
2208 free_beacon_data(&settings->beacon_after);
2209 return ret;
2210 }
2211
2212 settings->counter_offset_beacon = iface->cs_c_off_beacon;
2213 settings->counter_offset_presp = iface->cs_c_off_proberesp;
2214
2215 return 0;
2216}
2217
2218
2219void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2220{
2221 hapd->iface->cs_freq = 0;
2222 hapd->iface->cs_count = 0;
2223 hapd->iface->cs_block_tx = 0;
2224 hapd->iface->cs_c_off_beacon = 0;
2225 hapd->iface->cs_c_off_proberesp = 0;
2226 hapd->iface->csa_in_progress = 0;
2227}
2228
2229
2230int hostapd_switch_channel(struct hostapd_data *hapd,
2231 struct csa_settings *settings)
2232{
2233 int ret;
2234 ret = hostapd_fill_csa_settings(hapd->iface, settings);
2235 if (ret)
2236 return ret;
2237
2238 ret = hostapd_drv_switch_channel(hapd, settings);
2239 free_beacon_data(&settings->beacon_csa);
2240 free_beacon_data(&settings->beacon_after);
2241
2242 if (ret) {
2243 /* if we failed, clean cs parameters */
2244 hostapd_cleanup_cs_params(hapd);
2245 return ret;
2246 }
2247
2248 hapd->iface->csa_in_progress = 1;
2249 return 0;
2250}
2251
2252#endif /* NEED_AP_MLME */