blob: 575ef2ad27eab95e5a42a9da349daf8bcd717c3a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Initialization and configuration
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2002-2012, 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"
14#include "radius/radius_client.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070015#include "radius/radius_das.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "drivers/driver.h"
17#include "hostapd.h"
18#include "authsrv.h"
19#include "sta_info.h"
20#include "accounting.h"
21#include "ap_list.h"
22#include "beacon.h"
23#include "iapp.h"
24#include "ieee802_1x.h"
25#include "ieee802_11_auth.h"
26#include "vlan_init.h"
27#include "wpa_auth.h"
28#include "wps_hostapd.h"
29#include "hw_features.h"
30#include "wpa_auth_glue.h"
31#include "ap_drv_ops.h"
32#include "ap_config.h"
33#include "p2p_hostapd.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070034#include "gas_serv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035
36
Dmitry Shmidt04949592012-07-19 12:16:46 -070037static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070039static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41extern int wpa_debug_level;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070042extern struct wpa_driver_ops *wpa_drivers[];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043
44
Dmitry Shmidt04949592012-07-19 12:16:46 -070045int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
46 int (*cb)(struct hostapd_iface *iface,
47 void *ctx), void *ctx)
48{
49 size_t i;
50 int ret;
51
52 for (i = 0; i < interfaces->count; i++) {
53 ret = cb(interfaces->iface[i], ctx);
54 if (ret)
55 return ret;
56 }
57
58 return 0;
59}
60
61
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062static void hostapd_reload_bss(struct hostapd_data *hapd)
63{
64#ifndef CONFIG_NO_RADIUS
65 radius_client_reconfig(hapd->radius, hapd->conf->radius);
66#endif /* CONFIG_NO_RADIUS */
67
68 if (hostapd_setup_wpa_psk(hapd->conf)) {
69 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
70 "after reloading configuration");
71 }
72
73 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
74 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
75 else
76 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
77
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080078 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 hostapd_setup_wpa(hapd);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080080 if (hapd->wpa_auth)
81 wpa_init_keys(hapd->wpa_auth);
82 } else if (hapd->conf->wpa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 const u8 *wpa_ie;
84 size_t wpa_ie_len;
85 hostapd_reconfig_wpa(hapd);
86 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
87 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
88 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
89 "the kernel driver.");
90 } else if (hapd->wpa_auth) {
91 wpa_deinit(hapd->wpa_auth);
92 hapd->wpa_auth = NULL;
93 hostapd_set_privacy(hapd, 0);
94 hostapd_setup_encryption(hapd->conf->iface, hapd);
95 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
96 }
97
98 ieee802_11_set_beacon(hapd);
99 hostapd_update_wps(hapd);
100
101 if (hapd->conf->ssid.ssid_set &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700102 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 hapd->conf->ssid.ssid_len)) {
104 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
105 /* try to continue */
106 }
107 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
108}
109
110
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700111static void hostapd_clear_old(struct hostapd_iface *iface)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 size_t j;
114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 /*
116 * Deauthenticate all stations since the new configuration may not
117 * allow them to use the BSS anymore.
118 */
119 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120 hostapd_flush_old_stations(iface->bss[j],
121 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700122 hostapd_broadcast_wep_clear(iface->bss[j]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123
124#ifndef CONFIG_NO_RADIUS
125 /* TODO: update dynamic data based on changed configuration
126 * items (e.g., open/close sockets, etc.) */
127 radius_client_flush(iface->bss[j]->radius, 0);
128#endif /* CONFIG_NO_RADIUS */
129 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700130}
131
132
133int hostapd_reload_config(struct hostapd_iface *iface)
134{
135 struct hostapd_data *hapd = iface->bss[0];
136 struct hostapd_config *newconf, *oldconf;
137 size_t j;
138
139 if (iface->config_fname == NULL) {
140 /* Only in-memory config in use - assume it has been updated */
141 hostapd_clear_old(iface);
142 for (j = 0; j < iface->num_bss; j++)
143 hostapd_reload_bss(iface->bss[j]);
144 return 0;
145 }
146
147 if (iface->interfaces == NULL ||
148 iface->interfaces->config_read_cb == NULL)
149 return -1;
150 newconf = iface->interfaces->config_read_cb(iface->config_fname);
151 if (newconf == NULL)
152 return -1;
153
154 hostapd_clear_old(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156 oldconf = hapd->iconf;
157 iface->conf = newconf;
158
159 for (j = 0; j < iface->num_bss; j++) {
160 hapd = iface->bss[j];
161 hapd->iconf = newconf;
162 hapd->conf = &newconf->bss[j];
163 hostapd_reload_bss(hapd);
164 }
165
166 hostapd_config_free(oldconf);
167
168
169 return 0;
170}
171
172
173static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
174 char *ifname)
175{
176 int i;
177
178 for (i = 0; i < NUM_WEP_KEYS; i++) {
179 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
180 0, NULL, 0, NULL, 0)) {
181 wpa_printf(MSG_DEBUG, "Failed to clear default "
182 "encryption keys (ifname=%s keyidx=%d)",
183 ifname, i);
184 }
185 }
186#ifdef CONFIG_IEEE80211W
187 if (hapd->conf->ieee80211w) {
188 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
189 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
190 NULL, i, 0, NULL,
191 0, NULL, 0)) {
192 wpa_printf(MSG_DEBUG, "Failed to clear "
193 "default mgmt encryption keys "
194 "(ifname=%s keyidx=%d)", ifname, i);
195 }
196 }
197 }
198#endif /* CONFIG_IEEE80211W */
199}
200
201
202static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
203{
204 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
205 return 0;
206}
207
208
209static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
210{
211 int errors = 0, idx;
212 struct hostapd_ssid *ssid = &hapd->conf->ssid;
213
214 idx = ssid->wep.idx;
215 if (ssid->wep.default_len &&
216 hostapd_drv_set_key(hapd->conf->iface,
217 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
218 1, NULL, 0, ssid->wep.key[idx],
219 ssid->wep.len[idx])) {
220 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
221 errors++;
222 }
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 return errors;
225}
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227
Dmitry Shmidt04949592012-07-19 12:16:46 -0700228static void hostapd_free_hapd_data(struct hostapd_data *hapd)
229{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 iapp_deinit(hapd->iapp);
231 hapd->iapp = NULL;
232 accounting_deinit(hapd);
233 hostapd_deinit_wpa(hapd);
234 vlan_deinit(hapd);
235 hostapd_acl_deinit(hapd);
236#ifndef CONFIG_NO_RADIUS
237 radius_client_deinit(hapd->radius);
238 hapd->radius = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 radius_das_deinit(hapd->radius_das);
240 hapd->radius_das = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241#endif /* CONFIG_NO_RADIUS */
242
243 hostapd_deinit_wps(hapd);
244
245 authsrv_deinit(hapd);
246
247 if (hapd->interface_added &&
248 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
249 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
250 hapd->conf->iface);
251 }
252
253 os_free(hapd->probereq_cb);
254 hapd->probereq_cb = NULL;
255
256#ifdef CONFIG_P2P
257 wpabuf_free(hapd->p2p_beacon_ie);
258 hapd->p2p_beacon_ie = NULL;
259 wpabuf_free(hapd->p2p_probe_resp_ie);
260 hapd->p2p_probe_resp_ie = NULL;
261#endif /* CONFIG_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800262
263 wpabuf_free(hapd->time_adv);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700264
265#ifdef CONFIG_INTERWORKING
266 gas_serv_deinit(hapd);
267#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800268
269#ifdef CONFIG_SQLITE
270 os_free(hapd->tmp_eap_user.identity);
271 os_free(hapd->tmp_eap_user.password);
272#endif /* CONFIG_SQLITE */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273}
274
275
276/**
277 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
278 * @hapd: Pointer to BSS data
279 *
280 * This function is used to free all per-BSS data structures and resources.
281 * This gets called in a loop for each BSS between calls to
282 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
283 * is deinitialized. Most of the modules that are initialized in
284 * hostapd_setup_bss() are deinitialized here.
285 */
286static void hostapd_cleanup(struct hostapd_data *hapd)
287{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700288 if (hapd->iface->interfaces &&
289 hapd->iface->interfaces->ctrl_iface_deinit)
290 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700291 hostapd_free_hapd_data(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292}
293
294
295/**
296 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
297 * @iface: Pointer to interface data
298 *
299 * This function is called before per-BSS data structures are deinitialized
300 * with hostapd_cleanup().
301 */
302static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
303{
304}
305
306
Dmitry Shmidt04949592012-07-19 12:16:46 -0700307static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
308{
309 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
310 iface->hw_features = NULL;
311 os_free(iface->current_rates);
312 iface->current_rates = NULL;
313 os_free(iface->basic_rates);
314 iface->basic_rates = NULL;
315 ap_list_deinit(iface);
316}
317
318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319/**
320 * hostapd_cleanup_iface - Complete per-interface cleanup
321 * @iface: Pointer to interface data
322 *
323 * This function is called after per-BSS data structures are deinitialized
324 * with hostapd_cleanup().
325 */
326static void hostapd_cleanup_iface(struct hostapd_iface *iface)
327{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700328 hostapd_cleanup_iface_partial(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 hostapd_config_free(iface->conf);
330 iface->conf = NULL;
331
332 os_free(iface->config_fname);
333 os_free(iface->bss);
334 os_free(iface);
335}
336
337
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338static void hostapd_clear_wep(struct hostapd_data *hapd)
339{
340 if (hapd->drv_priv) {
341 hostapd_set_privacy(hapd, 0);
342 hostapd_broadcast_wep_clear(hapd);
343 }
344}
345
346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
348{
349 int i;
350
351 hostapd_broadcast_wep_set(hapd);
352
353 if (hapd->conf->ssid.wep.default_len) {
354 hostapd_set_privacy(hapd, 1);
355 return 0;
356 }
357
Jouni Malinen75ecf522011-06-27 15:19:46 -0700358 /*
359 * When IEEE 802.1X is not enabled, the driver may need to know how to
360 * set authentication algorithms for static WEP.
361 */
362 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 for (i = 0; i < 4; i++) {
365 if (hapd->conf->ssid.wep.key[i] &&
366 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
367 i == hapd->conf->ssid.wep.idx, NULL, 0,
368 hapd->conf->ssid.wep.key[i],
369 hapd->conf->ssid.wep.len[i])) {
370 wpa_printf(MSG_WARNING, "Could not set WEP "
371 "encryption.");
372 return -1;
373 }
374 if (hapd->conf->ssid.wep.key[i] &&
375 i == hapd->conf->ssid.wep.idx)
376 hostapd_set_privacy(hapd, 1);
377 }
378
379 return 0;
380}
381
382
Dmitry Shmidt04949592012-07-19 12:16:46 -0700383static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384{
385 int ret = 0;
386 u8 addr[ETH_ALEN];
387
388 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
389 return 0;
390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 if (hostapd_flush(hapd)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800393 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
394 "kernel driver");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 ret = -1;
396 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800397 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398 os_memset(addr, 0xff, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700399 hostapd_drv_sta_deauth(hapd, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 hostapd_free_stas(hapd);
401
402 return ret;
403}
404
405
406/**
407 * hostapd_validate_bssid_configuration - Validate BSSID configuration
408 * @iface: Pointer to interface data
409 * Returns: 0 on success, -1 on failure
410 *
411 * This function is used to validate that the configured BSSIDs are valid.
412 */
413static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
414{
415 u8 mask[ETH_ALEN] = { 0 };
416 struct hostapd_data *hapd = iface->bss[0];
417 unsigned int i = iface->conf->num_bss, bits = 0, j;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 int auto_addr = 0;
419
420 if (hostapd_drv_none(hapd))
421 return 0;
422
423 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
424
425 /* Determine the bits necessary to cover the number of BSSIDs. */
426 for (i--; i; i >>= 1)
427 bits++;
428
429 /* Determine the bits necessary to any configured BSSIDs,
430 if they are higher than the number of BSSIDs. */
431 for (j = 0; j < iface->conf->num_bss; j++) {
432 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
433 if (j)
434 auto_addr++;
435 continue;
436 }
437
438 for (i = 0; i < ETH_ALEN; i++) {
439 mask[i] |=
440 iface->conf->bss[j].bssid[i] ^
441 hapd->own_addr[i];
442 }
443 }
444
445 if (!auto_addr)
446 goto skip_mask_ext;
447
448 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
449 ;
450 j = 0;
451 if (i < ETH_ALEN) {
452 j = (5 - i) * 8;
453
454 while (mask[i] != 0) {
455 mask[i] >>= 1;
456 j++;
457 }
458 }
459
460 if (bits < j)
461 bits = j;
462
463 if (bits > 40) {
464 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
465 bits);
466 return -1;
467 }
468
469 os_memset(mask, 0xff, ETH_ALEN);
470 j = bits / 8;
471 for (i = 5; i > 5 - j; i--)
472 mask[i] = 0;
473 j = bits % 8;
474 while (j--)
475 mask[i] <<= 1;
476
477skip_mask_ext:
478 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
479 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 if (!auto_addr)
482 return 0;
483
484 for (i = 0; i < ETH_ALEN; i++) {
485 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
486 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
487 " for start address " MACSTR ".",
488 MAC2STR(mask), MAC2STR(hapd->own_addr));
489 wpa_printf(MSG_ERROR, "Start address must be the "
490 "first address in the block (i.e., addr "
491 "AND mask == addr).");
492 return -1;
493 }
494 }
495
496 return 0;
497}
498
499
500static int mac_in_conf(struct hostapd_config *conf, const void *a)
501{
502 size_t i;
503
504 for (i = 0; i < conf->num_bss; i++) {
505 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
506 return 1;
507 }
508 }
509
510 return 0;
511}
512
513
Dmitry Shmidt04949592012-07-19 12:16:46 -0700514#ifndef CONFIG_NO_RADIUS
515
516static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
517 struct radius_das_attrs *attr)
518{
519 /* TODO */
520 return 0;
521}
522
523
524static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
525 struct radius_das_attrs *attr)
526{
527 struct sta_info *sta = NULL;
528 char buf[128];
529
530 if (attr->sta_addr)
531 sta = ap_get_sta(hapd, attr->sta_addr);
532
533 if (sta == NULL && attr->acct_session_id &&
534 attr->acct_session_id_len == 17) {
535 for (sta = hapd->sta_list; sta; sta = sta->next) {
536 os_snprintf(buf, sizeof(buf), "%08X-%08X",
537 sta->acct_session_id_hi,
538 sta->acct_session_id_lo);
539 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
540 break;
541 }
542 }
543
544 if (sta == NULL && attr->cui) {
545 for (sta = hapd->sta_list; sta; sta = sta->next) {
546 struct wpabuf *cui;
547 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
548 if (cui && wpabuf_len(cui) == attr->cui_len &&
549 os_memcmp(wpabuf_head(cui), attr->cui,
550 attr->cui_len) == 0)
551 break;
552 }
553 }
554
555 if (sta == NULL && attr->user_name) {
556 for (sta = hapd->sta_list; sta; sta = sta->next) {
557 u8 *identity;
558 size_t identity_len;
559 identity = ieee802_1x_get_identity(sta->eapol_sm,
560 &identity_len);
561 if (identity &&
562 identity_len == attr->user_name_len &&
563 os_memcmp(identity, attr->user_name, identity_len)
564 == 0)
565 break;
566 }
567 }
568
569 return sta;
570}
571
572
573static enum radius_das_res
574hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
575{
576 struct hostapd_data *hapd = ctx;
577 struct sta_info *sta;
578
579 if (hostapd_das_nas_mismatch(hapd, attr))
580 return RADIUS_DAS_NAS_MISMATCH;
581
582 sta = hostapd_das_find_sta(hapd, attr);
583 if (sta == NULL)
584 return RADIUS_DAS_SESSION_NOT_FOUND;
585
586 hostapd_drv_sta_deauth(hapd, sta->addr,
587 WLAN_REASON_PREV_AUTH_NOT_VALID);
588 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
589
590 return RADIUS_DAS_SUCCESS;
591}
592
593#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594
595
596/**
597 * hostapd_setup_bss - Per-BSS setup (initialization)
598 * @hapd: Pointer to BSS data
599 * @first: Whether this BSS is the first BSS of an interface
600 *
601 * This function is used to initialize all per-BSS data structures and
602 * resources. This gets called in a loop for each BSS when an interface is
603 * initialized. Most of the modules that are initialized here will be
604 * deinitialized in hostapd_cleanup().
605 */
606static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
607{
608 struct hostapd_bss_config *conf = hapd->conf;
609 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
610 int ssid_len, set_ssid;
611 char force_ifname[IFNAMSIZ];
612 u8 if_addr[ETH_ALEN];
613
614 if (!first) {
615 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
616 /* Allocate the next available BSSID. */
617 do {
618 inc_byte_array(hapd->own_addr, ETH_ALEN);
619 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
620 } else {
621 /* Allocate the configured BSSID. */
622 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
623
624 if (hostapd_mac_comp(hapd->own_addr,
625 hapd->iface->bss[0]->own_addr) ==
626 0) {
627 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
628 "BSSID set to the MAC address of "
629 "the radio", hapd->conf->iface);
630 return -1;
631 }
632 }
633
634 hapd->interface_added = 1;
635 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
636 hapd->conf->iface, hapd->own_addr, hapd,
637 &hapd->drv_priv, force_ifname, if_addr,
638 hapd->conf->bridge[0] ? hapd->conf->bridge :
639 NULL)) {
640 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
641 MACSTR ")", MAC2STR(hapd->own_addr));
642 return -1;
643 }
644 }
645
646 if (conf->wmm_enabled < 0)
647 conf->wmm_enabled = hapd->iconf->ieee80211n;
648
Dmitry Shmidt04949592012-07-19 12:16:46 -0700649 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 hostapd_set_privacy(hapd, 0);
651
652 hostapd_broadcast_wep_clear(hapd);
653 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
654 return -1;
655
656 /*
657 * Fetch the SSID from the system and use it or,
658 * if one was specified in the config file, verify they
659 * match.
660 */
661 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
662 if (ssid_len < 0) {
663 wpa_printf(MSG_ERROR, "Could not read SSID from system");
664 return -1;
665 }
666 if (conf->ssid.ssid_set) {
667 /*
668 * If SSID is specified in the config file and it differs
669 * from what is being used then force installation of the
670 * new SSID.
671 */
672 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
673 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
674 } else {
675 /*
676 * No SSID in the config file; just use the one we got
677 * from the system.
678 */
679 set_ssid = 0;
680 conf->ssid.ssid_len = ssid_len;
681 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 }
683
684 if (!hostapd_drv_none(hapd)) {
685 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700686 " and ssid \"%s\"",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 hapd->conf->iface, MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700688 wpa_ssid_txt(hapd->conf->ssid.ssid,
689 hapd->conf->ssid.ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 }
691
692 if (hostapd_setup_wpa_psk(conf)) {
693 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
694 return -1;
695 }
696
697 /* Set SSID for the kernel driver (to be used in beacon and probe
698 * response frames) */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700699 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 conf->ssid.ssid_len)) {
701 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
702 return -1;
703 }
704
705 if (wpa_debug_level == MSG_MSGDUMP)
706 conf->radius->msg_dumps = 1;
707#ifndef CONFIG_NO_RADIUS
708 hapd->radius = radius_client_init(hapd, conf->radius);
709 if (hapd->radius == NULL) {
710 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
711 return -1;
712 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700713
714 if (hapd->conf->radius_das_port) {
715 struct radius_das_conf das_conf;
716 os_memset(&das_conf, 0, sizeof(das_conf));
717 das_conf.port = hapd->conf->radius_das_port;
718 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
719 das_conf.shared_secret_len =
720 hapd->conf->radius_das_shared_secret_len;
721 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
722 das_conf.time_window = hapd->conf->radius_das_time_window;
723 das_conf.require_event_timestamp =
724 hapd->conf->radius_das_require_event_timestamp;
725 das_conf.ctx = hapd;
726 das_conf.disconnect = hostapd_das_disconnect;
727 hapd->radius_das = radius_das_init(&das_conf);
728 if (hapd->radius_das == NULL) {
729 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
730 "failed.");
731 return -1;
732 }
733 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734#endif /* CONFIG_NO_RADIUS */
735
736 if (hostapd_acl_init(hapd)) {
737 wpa_printf(MSG_ERROR, "ACL initialization failed.");
738 return -1;
739 }
740 if (hostapd_init_wps(hapd, conf))
741 return -1;
742
743 if (authsrv_init(hapd) < 0)
744 return -1;
745
746 if (ieee802_1x_init(hapd)) {
747 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
748 return -1;
749 }
750
751 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
752 return -1;
753
754 if (accounting_init(hapd)) {
755 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
756 return -1;
757 }
758
759 if (hapd->conf->ieee802_11f &&
760 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
761 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
762 "failed.");
763 return -1;
764 }
765
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766#ifdef CONFIG_INTERWORKING
767 if (gas_serv_init(hapd)) {
768 wpa_printf(MSG_ERROR, "GAS server initialization failed");
769 return -1;
770 }
771#endif /* CONFIG_INTERWORKING */
772
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700773 if (hapd->iface->interfaces &&
774 hapd->iface->interfaces->ctrl_iface_init &&
775 hapd->iface->interfaces->ctrl_iface_init(hapd)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 wpa_printf(MSG_ERROR, "Failed to setup control interface");
777 return -1;
778 }
779
780 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
781 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
782 return -1;
783 }
784
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700785 if (!hapd->conf->start_disabled)
786 ieee802_11_set_beacon(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
789 return -1;
790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 if (hapd->driver && hapd->driver->set_operstate)
792 hapd->driver->set_operstate(hapd->drv_priv, 1);
793
794 return 0;
795}
796
797
798static void hostapd_tx_queue_params(struct hostapd_iface *iface)
799{
800 struct hostapd_data *hapd = iface->bss[0];
801 int i;
802 struct hostapd_tx_queue_params *p;
803
804 for (i = 0; i < NUM_TX_QUEUES; i++) {
805 p = &iface->conf->tx_queue[i];
806
807 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
808 p->cwmax, p->burst)) {
809 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
810 "parameters for queue %d.", i);
811 /* Continue anyway */
812 }
813 }
814}
815
816
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700817static int hostapd_set_acl_list(struct hostapd_data *hapd,
818 struct mac_acl_entry *mac_acl,
819 int n_entries, u8 accept_acl)
820{
821 struct hostapd_acl_params *acl_params;
822 int i, err;
823
824 acl_params = os_zalloc(sizeof(*acl_params) +
825 (n_entries * sizeof(acl_params->mac_acl[0])));
826 if (!acl_params)
827 return -ENOMEM;
828
829 for (i = 0; i < n_entries; i++)
830 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
831 ETH_ALEN);
832
833 acl_params->acl_policy = accept_acl;
834 acl_params->num_mac_acl = n_entries;
835
836 err = hostapd_drv_set_acl(hapd, acl_params);
837
838 os_free(acl_params);
839
840 return err;
841}
842
843
844static void hostapd_set_acl(struct hostapd_data *hapd)
845{
846 struct hostapd_config *conf = hapd->iconf;
847 int err;
848 u8 accept_acl;
849
850 if (hapd->iface->drv_max_acl_mac_addrs == 0)
851 return;
852 if (!(conf->bss->num_accept_mac || conf->bss->num_deny_mac))
853 return;
854
855 if (conf->bss->macaddr_acl == DENY_UNLESS_ACCEPTED) {
856 if (conf->bss->num_accept_mac) {
857 accept_acl = 1;
858 err = hostapd_set_acl_list(hapd, conf->bss->accept_mac,
859 conf->bss->num_accept_mac,
860 accept_acl);
861 if (err) {
862 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
863 return;
864 }
865 } else {
866 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
867 }
868 } else if (conf->bss->macaddr_acl == ACCEPT_UNLESS_DENIED) {
869 if (conf->bss->num_deny_mac) {
870 accept_acl = 0;
871 err = hostapd_set_acl_list(hapd, conf->bss->deny_mac,
872 conf->bss->num_deny_mac,
873 accept_acl);
874 if (err) {
875 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
876 return;
877 }
878 } else {
879 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
880 }
881 }
882}
883
884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885static int setup_interface(struct hostapd_iface *iface)
886{
887 struct hostapd_data *hapd = iface->bss[0];
888 size_t i;
889 char country[4];
890
891 /*
892 * Make sure that all BSSes get configured with a pointer to the same
893 * driver interface.
894 */
895 for (i = 1; i < iface->num_bss; i++) {
896 iface->bss[i]->driver = hapd->driver;
897 iface->bss[i]->drv_priv = hapd->drv_priv;
898 }
899
900 if (hostapd_validate_bssid_configuration(iface))
901 return -1;
902
903 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
904 os_memcpy(country, hapd->iconf->country, 3);
905 country[3] = '\0';
906 if (hostapd_set_country(hapd, country) < 0) {
907 wpa_printf(MSG_ERROR, "Failed to set country code");
908 return -1;
909 }
910 }
911
912 if (hostapd_get_hw_features(iface)) {
913 /* Not all drivers support this yet, so continue without hw
914 * feature data. */
915 } else {
916 int ret = hostapd_select_hw_mode(iface);
917 if (ret < 0) {
918 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
919 "channel. (%d)", ret);
920 return -1;
921 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700922 if (ret == 1) {
923 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
924 return 0;
925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 ret = hostapd_check_ht_capab(iface);
927 if (ret < 0)
928 return -1;
929 if (ret == 1) {
930 wpa_printf(MSG_DEBUG, "Interface initialization will "
931 "be completed in a callback");
932 return 0;
933 }
934 }
935 return hostapd_setup_interface_complete(iface, 0);
936}
937
938
939int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
940{
941 struct hostapd_data *hapd = iface->bss[0];
942 size_t j;
943 u8 *prev_addr;
944
945 if (err) {
946 wpa_printf(MSG_ERROR, "Interface initialization failed");
947 eloop_terminate();
948 return -1;
949 }
950
951 wpa_printf(MSG_DEBUG, "Completing interface initialization");
952 if (hapd->iconf->channel) {
953 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
954 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
955 "Frequency: %d MHz",
956 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
957 hapd->iconf->channel, iface->freq);
958
959 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
960 hapd->iconf->channel,
961 hapd->iconf->ieee80211n,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800962 hapd->iconf->ieee80211ac,
963 hapd->iconf->secondary_channel,
964 hapd->iconf->vht_oper_chwidth,
965 hapd->iconf->vht_oper_centr_freq_seg0_idx,
966 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 wpa_printf(MSG_ERROR, "Could not set channel for "
968 "kernel driver");
969 return -1;
970 }
971 }
972
973 if (iface->current_mode) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800974 if (hostapd_prepare_rates(iface, iface->current_mode)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 wpa_printf(MSG_ERROR, "Failed to prepare rates "
976 "table.");
977 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
978 HOSTAPD_LEVEL_WARNING,
979 "Failed to prepare rates table.");
980 return -1;
981 }
982 }
983
984 if (hapd->iconf->rts_threshold > -1 &&
985 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
986 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
987 "kernel driver");
988 return -1;
989 }
990
991 if (hapd->iconf->fragm_threshold > -1 &&
992 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
993 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
994 "for kernel driver");
995 return -1;
996 }
997
998 prev_addr = hapd->own_addr;
999
1000 for (j = 0; j < iface->num_bss; j++) {
1001 hapd = iface->bss[j];
1002 if (j)
1003 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1004 if (hostapd_setup_bss(hapd, j == 0))
1005 return -1;
1006 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1007 prev_addr = hapd->own_addr;
1008 }
1009
1010 hostapd_tx_queue_params(iface);
1011
1012 ap_list_init(iface);
1013
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07001014 hostapd_set_acl(hapd);
1015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 if (hostapd_driver_commit(hapd) < 0) {
1017 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1018 "configuration", __func__);
1019 return -1;
1020 }
1021
Jouni Malinen87fd2792011-05-16 18:35:42 +03001022 /*
1023 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1024 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1025 * mode), the interface is up only after driver_commit, so initialize
1026 * WPS after driver_commit.
1027 */
1028 for (j = 0; j < iface->num_bss; j++) {
1029 if (hostapd_init_wps_complete(iface->bss[j]))
1030 return -1;
1031 }
1032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 if (hapd->setup_complete_cb)
1034 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1035
1036 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1037 iface->bss[0]->conf->iface);
1038
1039 return 0;
1040}
1041
1042
1043/**
1044 * hostapd_setup_interface - Setup of an interface
1045 * @iface: Pointer to interface data.
1046 * Returns: 0 on success, -1 on failure
1047 *
1048 * Initializes the driver interface, validates the configuration,
1049 * and sets driver parameters based on the configuration.
1050 * Flushes old stations, sets the channel, encryption,
1051 * beacons, and WDS links based on the configuration.
1052 */
1053int hostapd_setup_interface(struct hostapd_iface *iface)
1054{
1055 int ret;
1056
1057 ret = setup_interface(iface);
1058 if (ret) {
1059 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1060 iface->bss[0]->conf->iface);
1061 return -1;
1062 }
1063
1064 return 0;
1065}
1066
1067
1068/**
1069 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1070 * @hapd_iface: Pointer to interface data
1071 * @conf: Pointer to per-interface configuration
1072 * @bss: Pointer to per-BSS configuration for this BSS
1073 * Returns: Pointer to allocated BSS data
1074 *
1075 * This function is used to allocate per-BSS data structure. This data will be
1076 * freed after hostapd_cleanup() is called for it during interface
1077 * deinitialization.
1078 */
1079struct hostapd_data *
1080hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1081 struct hostapd_config *conf,
1082 struct hostapd_bss_config *bss)
1083{
1084 struct hostapd_data *hapd;
1085
1086 hapd = os_zalloc(sizeof(*hapd));
1087 if (hapd == NULL)
1088 return NULL;
1089
1090 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1091 hapd->iconf = conf;
1092 hapd->conf = bss;
1093 hapd->iface = hapd_iface;
1094 hapd->driver = hapd->iconf->driver;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001095 hapd->ctrl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096
1097 return hapd;
1098}
1099
1100
1101void hostapd_interface_deinit(struct hostapd_iface *iface)
1102{
1103 size_t j;
1104
1105 if (iface == NULL)
1106 return;
1107
1108 hostapd_cleanup_iface_pre(iface);
1109 for (j = 0; j < iface->num_bss; j++) {
1110 struct hostapd_data *hapd = iface->bss[j];
1111 hostapd_free_stas(hapd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001112 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1113 hostapd_clear_wep(hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 hostapd_cleanup(hapd);
1115 }
1116}
1117
1118
1119void hostapd_interface_free(struct hostapd_iface *iface)
1120{
1121 size_t j;
1122 for (j = 0; j < iface->num_bss; j++)
1123 os_free(iface->bss[j]);
1124 hostapd_cleanup_iface(iface);
1125}
1126
1127
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001128#ifdef HOSTAPD
1129
1130void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1131{
1132 const struct wpa_driver_ops *driver;
1133 void *drv_priv;
1134 if (iface == NULL)
1135 return;
1136 driver = iface->bss[0]->driver;
1137 drv_priv = iface->bss[0]->drv_priv;
1138 hostapd_interface_deinit(iface);
1139 if (driver && driver->hapd_deinit && drv_priv)
1140 driver->hapd_deinit(drv_priv);
1141 hostapd_interface_free(iface);
1142}
1143
1144
1145int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1146{
1147 if (hapd_iface->bss[0]->drv_priv != NULL) {
1148 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1149 hapd_iface->conf->bss[0].iface);
1150 return -1;
1151 }
1152
1153 wpa_printf(MSG_DEBUG, "Enable interface %s",
1154 hapd_iface->conf->bss[0].iface);
1155
1156 if (hapd_iface->interfaces == NULL ||
1157 hapd_iface->interfaces->driver_init == NULL ||
1158 hapd_iface->interfaces->driver_init(hapd_iface) ||
1159 hostapd_setup_interface(hapd_iface)) {
1160 hostapd_interface_deinit_free(hapd_iface);
1161 return -1;
1162 }
1163 return 0;
1164}
1165
1166
1167int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1168{
1169 size_t j;
1170
1171 wpa_printf(MSG_DEBUG, "Reload interface %s",
1172 hapd_iface->conf->bss[0].iface);
1173 for (j = 0; j < hapd_iface->num_bss; j++) {
1174 hostapd_flush_old_stations(hapd_iface->bss[j],
1175 WLAN_REASON_PREV_AUTH_NOT_VALID);
1176
1177#ifndef CONFIG_NO_RADIUS
1178 /* TODO: update dynamic data based on changed configuration
1179 * items (e.g., open/close sockets, etc.) */
1180 radius_client_flush(hapd_iface->bss[j]->radius, 0);
1181#endif /* CONFIG_NO_RADIUS */
1182
1183 hostapd_reload_bss(hapd_iface->bss[j]);
1184 }
1185 return 0;
1186}
1187
1188
1189int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1190{
1191 size_t j;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001192 const struct wpa_driver_ops *driver;
1193 void *drv_priv;
1194
1195 if (hapd_iface == NULL)
1196 return -1;
1197 driver = hapd_iface->bss[0]->driver;
1198 drv_priv = hapd_iface->bss[0]->drv_priv;
1199
1200 /* whatever hostapd_interface_deinit does */
1201 for (j = 0; j < hapd_iface->num_bss; j++) {
1202 struct hostapd_data *hapd = hapd_iface->bss[j];
1203 hostapd_free_stas(hapd);
1204 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1205 hostapd_clear_wep(hapd);
1206 hostapd_free_hapd_data(hapd);
1207 }
1208
1209 if (driver && driver->hapd_deinit && drv_priv) {
1210 driver->hapd_deinit(drv_priv);
1211 hapd_iface->bss[0]->drv_priv = NULL;
1212 }
1213
1214 /* From hostapd_cleanup_iface: These were initialized in
1215 * hostapd_setup_interface and hostapd_setup_interface_complete
1216 */
1217 hostapd_cleanup_iface_partial(hapd_iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001218
Dmitry Shmidt56052862013-10-04 10:23:25 -07001219 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1220 hapd_iface->bss[0]->conf->iface);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001221 return 0;
1222}
1223
1224
1225static struct hostapd_iface *
1226hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1227{
1228 struct hostapd_iface **iface, *hapd_iface;
1229
1230 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1231 sizeof(struct hostapd_iface *));
1232 if (iface == NULL)
1233 return NULL;
1234 interfaces->iface = iface;
1235 hapd_iface = interfaces->iface[interfaces->count] =
1236 os_zalloc(sizeof(*hapd_iface));
1237 if (hapd_iface == NULL) {
1238 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1239 "the interface", __func__);
1240 return NULL;
1241 }
1242 interfaces->count++;
1243 hapd_iface->interfaces = interfaces;
1244
1245 return hapd_iface;
1246}
1247
1248
1249static struct hostapd_config *
1250hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1251 const char *ctrl_iface)
1252{
1253 struct hostapd_bss_config *bss;
1254 struct hostapd_config *conf;
1255
1256 /* Allocates memory for bss and conf */
1257 conf = hostapd_config_defaults();
1258 if (conf == NULL) {
1259 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1260 "configuration", __func__);
1261 return NULL;
1262 }
1263
1264 conf->driver = wpa_drivers[0];
1265 if (conf->driver == NULL) {
1266 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1267 hostapd_config_free(conf);
1268 return NULL;
1269 }
1270
1271 bss = conf->last_bss = conf->bss;
1272
1273 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1274 bss->ctrl_interface = os_strdup(ctrl_iface);
1275 if (bss->ctrl_interface == NULL) {
1276 hostapd_config_free(conf);
1277 return NULL;
1278 }
1279
1280 /* Reading configuration file skipped, will be done in SET!
1281 * From reading the configuration till the end has to be done in
1282 * SET
1283 */
1284 return conf;
1285}
1286
1287
1288static struct hostapd_iface * hostapd_data_alloc(
1289 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1290{
1291 size_t i;
1292 struct hostapd_iface *hapd_iface =
1293 interfaces->iface[interfaces->count - 1];
1294 struct hostapd_data *hapd;
1295
1296 hapd_iface->conf = conf;
1297 hapd_iface->num_bss = conf->num_bss;
1298
1299 hapd_iface->bss = os_zalloc(conf->num_bss *
1300 sizeof(struct hostapd_data *));
1301 if (hapd_iface->bss == NULL)
1302 return NULL;
1303
1304 for (i = 0; i < conf->num_bss; i++) {
1305 hapd = hapd_iface->bss[i] =
1306 hostapd_alloc_bss_data(hapd_iface, conf,
1307 &conf->bss[i]);
1308 if (hapd == NULL)
1309 return NULL;
1310 hapd->msg_ctx = hapd;
1311 }
1312
1313 hapd_iface->interfaces = interfaces;
1314
1315 return hapd_iface;
1316}
1317
1318
1319int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1320{
1321 struct hostapd_config *conf = NULL;
1322 struct hostapd_iface *hapd_iface = NULL;
1323 char *ptr;
1324 size_t i;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001325 const char *conf_file = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001326
1327 ptr = os_strchr(buf, ' ');
1328 if (ptr == NULL)
1329 return -1;
1330 *ptr++ = '\0';
1331
Dmitry Shmidt56052862013-10-04 10:23:25 -07001332 if (os_strncmp(ptr, "config=", 7) == 0)
1333 conf_file = ptr + 7;
1334
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001335 for (i = 0; i < interfaces->count; i++) {
1336 if (!os_strcmp(interfaces->iface[i]->conf->bss[0].iface,
1337 buf)) {
1338 wpa_printf(MSG_INFO, "Cannot add interface - it "
1339 "already exists");
1340 return -1;
1341 }
1342 }
1343
1344 hapd_iface = hostapd_iface_alloc(interfaces);
1345 if (hapd_iface == NULL) {
1346 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1347 "for interface", __func__);
1348 goto fail;
1349 }
1350
Dmitry Shmidt56052862013-10-04 10:23:25 -07001351 if (conf_file && interfaces->config_read_cb) {
1352 conf = interfaces->config_read_cb(conf_file);
1353 if (conf && conf->bss)
1354 os_strlcpy(conf->bss->iface, buf,
1355 sizeof(conf->bss->iface));
1356 } else
1357 conf = hostapd_config_alloc(interfaces, buf, ptr);
1358 if (conf == NULL || conf->bss == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001359 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1360 "for configuration", __func__);
1361 goto fail;
1362 }
1363
1364 hapd_iface = hostapd_data_alloc(interfaces, conf);
1365 if (hapd_iface == NULL) {
1366 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1367 "for hostapd", __func__);
1368 goto fail;
1369 }
1370
1371 if (hapd_iface->interfaces &&
1372 hapd_iface->interfaces->ctrl_iface_init &&
1373 hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1374 wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1375 "interface", __func__);
1376 goto fail;
1377 }
1378 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0].iface);
1379
1380 return 0;
1381
1382fail:
1383 if (conf)
1384 hostapd_config_free(conf);
1385 if (hapd_iface) {
1386 os_free(hapd_iface->bss[interfaces->count]);
1387 os_free(hapd_iface);
1388 }
1389 return -1;
1390}
1391
1392
1393int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1394{
1395 struct hostapd_iface *hapd_iface;
1396 size_t i, k = 0;
1397
1398 for (i = 0; i < interfaces->count; i++) {
1399 hapd_iface = interfaces->iface[i];
1400 if (hapd_iface == NULL)
1401 return -1;
1402 if (!os_strcmp(hapd_iface->conf->bss[0].iface, buf)) {
1403 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1404 hostapd_interface_deinit_free(hapd_iface);
1405 k = i;
1406 while (k < (interfaces->count - 1)) {
1407 interfaces->iface[k] =
1408 interfaces->iface[k + 1];
1409 k++;
1410 }
1411 interfaces->count--;
1412 return 0;
1413 }
1414 }
1415 return -1;
1416}
1417
1418#endif /* HOSTAPD */
1419
1420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421/**
1422 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1423 * @hapd: Pointer to BSS data
1424 * @sta: Pointer to the associated STA data
1425 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1426 *
1427 * This function will be called whenever a station associates with the AP. It
1428 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1429 * from drv_callbacks.c based on driver events for drivers that take care of
1430 * management frames (IEEE 802.11 authentication and association) internally.
1431 */
1432void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1433 int reassoc)
1434{
1435 if (hapd->tkip_countermeasures) {
1436 hostapd_drv_sta_deauth(hapd, sta->addr,
1437 WLAN_REASON_MICHAEL_MIC_FAILURE);
1438 return;
1439 }
1440
1441 hostapd_prune_associations(hapd, sta->addr);
1442
1443 /* IEEE 802.11F (IAPP) */
1444 if (hapd->conf->ieee802_11f)
1445 iapp_new_station(hapd->iapp, sta);
1446
1447#ifdef CONFIG_P2P
1448 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1449 sta->no_p2p_set = 1;
1450 hapd->num_sta_no_p2p++;
1451 if (hapd->num_sta_no_p2p == 1)
1452 hostapd_p2p_non_p2p_sta_connected(hapd);
1453 }
1454#endif /* CONFIG_P2P */
1455
1456 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1457 * IEEE 802.1X/WPA code will start accounting after the station has
1458 * been authorized. */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001459 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1460 os_get_time(&sta->connected_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 accounting_sta_start(hapd, sta);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463
1464 /* Start IEEE 802.1X authentication process for new stations */
1465 ieee802_1x_new_station(hapd, sta);
1466 if (reassoc) {
1467 if (sta->auth_alg != WLAN_AUTH_FT &&
1468 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1469 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1470 } else
1471 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001472
1473 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1474 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1475 __func__, MAC2STR(sta->addr),
1476 hapd->conf->ap_max_inactivity);
1477 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1478 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1479 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480}