1 /*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "radius/radius_client.h"
15 #include "drivers/driver.h"
16 #include "hostapd.h"
17 #include "authsrv.h"
18 #include "sta_info.h"
19 #include "accounting.h"
20 #include "ap_list.h"
21 #include "beacon.h"
22 #include "iapp.h"
23 #include "ieee802_1x.h"
24 #include "ieee802_11_auth.h"
25 #include "vlan_init.h"
26 #include "wpa_auth.h"
27 #include "wps_hostapd.h"
28 #include "hw_features.h"
29 #include "wpa_auth_glue.h"
30 #include "ap_drv_ops.h"
31 #include "ap_config.h"
32 #include "p2p_hostapd.h"
33
34
35 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
36 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
37 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
38
39 extern int wpa_debug_level;
40
41
hostapd_reload_bss(struct hostapd_data * hapd)42 static void hostapd_reload_bss(struct hostapd_data *hapd)
43 {
44 #ifndef CONFIG_NO_RADIUS
45 radius_client_reconfig(hapd->radius, hapd->conf->radius);
46 #endif /* CONFIG_NO_RADIUS */
47
48 if (hostapd_setup_wpa_psk(hapd->conf)) {
49 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
50 "after reloading configuration");
51 }
52
53 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
54 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
55 else
56 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
57
58 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
59 hostapd_setup_wpa(hapd);
60 if (hapd->wpa_auth)
61 wpa_init_keys(hapd->wpa_auth);
62 } else if (hapd->conf->wpa) {
63 const u8 *wpa_ie;
64 size_t wpa_ie_len;
65 hostapd_reconfig_wpa(hapd);
66 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
67 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
68 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
69 "the kernel driver.");
70 } else if (hapd->wpa_auth) {
71 wpa_deinit(hapd->wpa_auth);
72 hapd->wpa_auth = NULL;
73 hostapd_set_privacy(hapd, 0);
74 hostapd_setup_encryption(hapd->conf->iface, hapd);
75 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
76 }
77
78 ieee802_11_set_beacon(hapd);
79 hostapd_update_wps(hapd);
80
81 if (hapd->conf->ssid.ssid_set &&
82 hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
83 hapd->conf->ssid.ssid_len)) {
84 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
85 /* try to continue */
86 }
87 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
88 }
89
90
hostapd_reload_config(struct hostapd_iface * iface)91 int hostapd_reload_config(struct hostapd_iface *iface)
92 {
93 struct hostapd_data *hapd = iface->bss[0];
94 struct hostapd_config *newconf, *oldconf;
95 size_t j;
96
97 if (iface->config_read_cb == NULL)
98 return -1;
99 newconf = iface->config_read_cb(iface->config_fname);
100 if (newconf == NULL)
101 return -1;
102
103 /*
104 * Deauthenticate all stations since the new configuration may not
105 * allow them to use the BSS anymore.
106 */
107 for (j = 0; j < iface->num_bss; j++) {
108 hostapd_flush_old_stations(iface->bss[j]);
109 hostapd_broadcast_wep_clear(iface->bss[j]);
110
111 #ifndef CONFIG_NO_RADIUS
112 /* TODO: update dynamic data based on changed configuration
113 * items (e.g., open/close sockets, etc.) */
114 radius_client_flush(iface->bss[j]->radius, 0);
115 #endif /* CONFIG_NO_RADIUS */
116 }
117
118 oldconf = hapd->iconf;
119 iface->conf = newconf;
120
121 for (j = 0; j < iface->num_bss; j++) {
122 hapd = iface->bss[j];
123 hapd->iconf = newconf;
124 hapd->conf = &newconf->bss[j];
125 hostapd_reload_bss(hapd);
126 }
127
128 hostapd_config_free(oldconf);
129
130
131 return 0;
132 }
133
134
hostapd_broadcast_key_clear_iface(struct hostapd_data * hapd,char * ifname)135 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
136 char *ifname)
137 {
138 int i;
139
140 for (i = 0; i < NUM_WEP_KEYS; i++) {
141 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
142 0, NULL, 0, NULL, 0)) {
143 wpa_printf(MSG_DEBUG, "Failed to clear default "
144 "encryption keys (ifname=%s keyidx=%d)",
145 ifname, i);
146 }
147 }
148 #ifdef CONFIG_IEEE80211W
149 if (hapd->conf->ieee80211w) {
150 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
151 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
152 NULL, i, 0, NULL,
153 0, NULL, 0)) {
154 wpa_printf(MSG_DEBUG, "Failed to clear "
155 "default mgmt encryption keys "
156 "(ifname=%s keyidx=%d)", ifname, i);
157 }
158 }
159 }
160 #endif /* CONFIG_IEEE80211W */
161 }
162
163
hostapd_broadcast_wep_clear(struct hostapd_data * hapd)164 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
165 {
166 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
167 return 0;
168 }
169
170
hostapd_broadcast_wep_set(struct hostapd_data * hapd)171 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
172 {
173 int errors = 0, idx;
174 struct hostapd_ssid *ssid = &hapd->conf->ssid;
175
176 idx = ssid->wep.idx;
177 if (ssid->wep.default_len &&
178 hostapd_drv_set_key(hapd->conf->iface,
179 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
180 1, NULL, 0, ssid->wep.key[idx],
181 ssid->wep.len[idx])) {
182 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
183 errors++;
184 }
185
186 if (ssid->dyn_vlan_keys) {
187 size_t i;
188 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
189 const char *ifname;
190 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
191 if (key == NULL)
192 continue;
193 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
194 i);
195 if (ifname == NULL)
196 continue;
197
198 idx = key->idx;
199 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
200 broadcast_ether_addr, idx, 1,
201 NULL, 0, key->key[idx],
202 key->len[idx])) {
203 wpa_printf(MSG_WARNING, "Could not set "
204 "dynamic VLAN WEP encryption.");
205 errors++;
206 }
207 }
208 }
209
210 return errors;
211 }
212
213 /**
214 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
215 * @hapd: Pointer to BSS data
216 *
217 * This function is used to free all per-BSS data structures and resources.
218 * This gets called in a loop for each BSS between calls to
219 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
220 * is deinitialized. Most of the modules that are initialized in
221 * hostapd_setup_bss() are deinitialized here.
222 */
hostapd_cleanup(struct hostapd_data * hapd)223 static void hostapd_cleanup(struct hostapd_data *hapd)
224 {
225 if (hapd->iface->ctrl_iface_deinit)
226 hapd->iface->ctrl_iface_deinit(hapd);
227
228 iapp_deinit(hapd->iapp);
229 hapd->iapp = NULL;
230 accounting_deinit(hapd);
231 hostapd_deinit_wpa(hapd);
232 vlan_deinit(hapd);
233 hostapd_acl_deinit(hapd);
234 #ifndef CONFIG_NO_RADIUS
235 radius_client_deinit(hapd->radius);
236 hapd->radius = NULL;
237 #endif /* CONFIG_NO_RADIUS */
238
239 hostapd_deinit_wps(hapd);
240
241 authsrv_deinit(hapd);
242
243 if (hapd->interface_added &&
244 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
245 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
246 hapd->conf->iface);
247 }
248
249 os_free(hapd->probereq_cb);
250 hapd->probereq_cb = NULL;
251
252 #ifdef CONFIG_P2P
253 wpabuf_free(hapd->p2p_beacon_ie);
254 hapd->p2p_beacon_ie = NULL;
255 wpabuf_free(hapd->p2p_probe_resp_ie);
256 hapd->p2p_probe_resp_ie = NULL;
257 #endif /* CONFIG_P2P */
258
259 wpabuf_free(hapd->time_adv);
260 }
261
262
263 /**
264 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
265 * @iface: Pointer to interface data
266 *
267 * This function is called before per-BSS data structures are deinitialized
268 * with hostapd_cleanup().
269 */
hostapd_cleanup_iface_pre(struct hostapd_iface * iface)270 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
271 {
272 }
273
274
275 /**
276 * hostapd_cleanup_iface - Complete per-interface cleanup
277 * @iface: Pointer to interface data
278 *
279 * This function is called after per-BSS data structures are deinitialized
280 * with hostapd_cleanup().
281 */
hostapd_cleanup_iface(struct hostapd_iface * iface)282 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
283 {
284 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
285 iface->hw_features = NULL;
286 os_free(iface->current_rates);
287 iface->current_rates = NULL;
288 os_free(iface->basic_rates);
289 iface->basic_rates = NULL;
290 ap_list_deinit(iface);
291 hostapd_config_free(iface->conf);
292 iface->conf = NULL;
293
294 os_free(iface->config_fname);
295 os_free(iface->bss);
296 os_free(iface);
297 }
298
299
hostapd_setup_encryption(char * iface,struct hostapd_data * hapd)300 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
301 {
302 int i;
303
304 hostapd_broadcast_wep_set(hapd);
305
306 if (hapd->conf->ssid.wep.default_len) {
307 hostapd_set_privacy(hapd, 1);
308 return 0;
309 }
310
311 /*
312 * When IEEE 802.1X is not enabled, the driver may need to know how to
313 * set authentication algorithms for static WEP.
314 */
315 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
316
317 for (i = 0; i < 4; i++) {
318 if (hapd->conf->ssid.wep.key[i] &&
319 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
320 i == hapd->conf->ssid.wep.idx, NULL, 0,
321 hapd->conf->ssid.wep.key[i],
322 hapd->conf->ssid.wep.len[i])) {
323 wpa_printf(MSG_WARNING, "Could not set WEP "
324 "encryption.");
325 return -1;
326 }
327 if (hapd->conf->ssid.wep.key[i] &&
328 i == hapd->conf->ssid.wep.idx)
329 hostapd_set_privacy(hapd, 1);
330 }
331
332 return 0;
333 }
334
335
hostapd_flush_old_stations(struct hostapd_data * hapd)336 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
337 {
338 int ret = 0;
339 u8 addr[ETH_ALEN];
340
341 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
342 return 0;
343
344 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
345 if (hostapd_flush(hapd)) {
346 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
347 "kernel driver");
348 ret = -1;
349 }
350 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
351 os_memset(addr, 0xff, ETH_ALEN);
352 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
353 hostapd_free_stas(hapd);
354
355 return ret;
356 }
357
358
359 /**
360 * hostapd_validate_bssid_configuration - Validate BSSID configuration
361 * @iface: Pointer to interface data
362 * Returns: 0 on success, -1 on failure
363 *
364 * This function is used to validate that the configured BSSIDs are valid.
365 */
hostapd_validate_bssid_configuration(struct hostapd_iface * iface)366 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
367 {
368 u8 mask[ETH_ALEN] = { 0 };
369 struct hostapd_data *hapd = iface->bss[0];
370 unsigned int i = iface->conf->num_bss, bits = 0, j;
371 int auto_addr = 0;
372
373 if (hostapd_drv_none(hapd))
374 return 0;
375
376 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
377
378 /* Determine the bits necessary to cover the number of BSSIDs. */
379 for (i--; i; i >>= 1)
380 bits++;
381
382 /* Determine the bits necessary to any configured BSSIDs,
383 if they are higher than the number of BSSIDs. */
384 for (j = 0; j < iface->conf->num_bss; j++) {
385 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
386 if (j)
387 auto_addr++;
388 continue;
389 }
390
391 for (i = 0; i < ETH_ALEN; i++) {
392 mask[i] |=
393 iface->conf->bss[j].bssid[i] ^
394 hapd->own_addr[i];
395 }
396 }
397
398 if (!auto_addr)
399 goto skip_mask_ext;
400
401 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
402 ;
403 j = 0;
404 if (i < ETH_ALEN) {
405 j = (5 - i) * 8;
406
407 while (mask[i] != 0) {
408 mask[i] >>= 1;
409 j++;
410 }
411 }
412
413 if (bits < j)
414 bits = j;
415
416 if (bits > 40) {
417 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
418 bits);
419 return -1;
420 }
421
422 os_memset(mask, 0xff, ETH_ALEN);
423 j = bits / 8;
424 for (i = 5; i > 5 - j; i--)
425 mask[i] = 0;
426 j = bits % 8;
427 while (j--)
428 mask[i] <<= 1;
429
430 skip_mask_ext:
431 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
432 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
433
434 if (!auto_addr)
435 return 0;
436
437 for (i = 0; i < ETH_ALEN; i++) {
438 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
439 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
440 " for start address " MACSTR ".",
441 MAC2STR(mask), MAC2STR(hapd->own_addr));
442 wpa_printf(MSG_ERROR, "Start address must be the "
443 "first address in the block (i.e., addr "
444 "AND mask == addr).");
445 return -1;
446 }
447 }
448
449 return 0;
450 }
451
452
mac_in_conf(struct hostapd_config * conf,const void * a)453 static int mac_in_conf(struct hostapd_config *conf, const void *a)
454 {
455 size_t i;
456
457 for (i = 0; i < conf->num_bss; i++) {
458 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
459 return 1;
460 }
461 }
462
463 return 0;
464 }
465
466
467
468
469 /**
470 * hostapd_setup_bss - Per-BSS setup (initialization)
471 * @hapd: Pointer to BSS data
472 * @first: Whether this BSS is the first BSS of an interface
473 *
474 * This function is used to initialize all per-BSS data structures and
475 * resources. This gets called in a loop for each BSS when an interface is
476 * initialized. Most of the modules that are initialized here will be
477 * deinitialized in hostapd_cleanup().
478 */
hostapd_setup_bss(struct hostapd_data * hapd,int first)479 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
480 {
481 struct hostapd_bss_config *conf = hapd->conf;
482 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
483 int ssid_len, set_ssid;
484 char force_ifname[IFNAMSIZ];
485 u8 if_addr[ETH_ALEN];
486
487 if (!first) {
488 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
489 /* Allocate the next available BSSID. */
490 do {
491 inc_byte_array(hapd->own_addr, ETH_ALEN);
492 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
493 } else {
494 /* Allocate the configured BSSID. */
495 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
496
497 if (hostapd_mac_comp(hapd->own_addr,
498 hapd->iface->bss[0]->own_addr) ==
499 0) {
500 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
501 "BSSID set to the MAC address of "
502 "the radio", hapd->conf->iface);
503 return -1;
504 }
505 }
506
507 hapd->interface_added = 1;
508 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
509 hapd->conf->iface, hapd->own_addr, hapd,
510 &hapd->drv_priv, force_ifname, if_addr,
511 hapd->conf->bridge[0] ? hapd->conf->bridge :
512 NULL)) {
513 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
514 MACSTR ")", MAC2STR(hapd->own_addr));
515 return -1;
516 }
517 }
518
519 if (conf->wmm_enabled < 0)
520 conf->wmm_enabled = hapd->iconf->ieee80211n;
521
522 hostapd_flush_old_stations(hapd);
523 hostapd_set_privacy(hapd, 0);
524
525 hostapd_broadcast_wep_clear(hapd);
526 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
527 return -1;
528
529 /*
530 * Fetch the SSID from the system and use it or,
531 * if one was specified in the config file, verify they
532 * match.
533 */
534 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
535 if (ssid_len < 0) {
536 wpa_printf(MSG_ERROR, "Could not read SSID from system");
537 return -1;
538 }
539 if (conf->ssid.ssid_set) {
540 /*
541 * If SSID is specified in the config file and it differs
542 * from what is being used then force installation of the
543 * new SSID.
544 */
545 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
546 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
547 } else {
548 /*
549 * No SSID in the config file; just use the one we got
550 * from the system.
551 */
552 set_ssid = 0;
553 conf->ssid.ssid_len = ssid_len;
554 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
555 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
556 }
557
558 if (!hostapd_drv_none(hapd)) {
559 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
560 " and ssid '%s'",
561 hapd->conf->iface, MAC2STR(hapd->own_addr),
562 hapd->conf->ssid.ssid);
563 }
564
565 if (hostapd_setup_wpa_psk(conf)) {
566 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
567 return -1;
568 }
569
570 /* Set SSID for the kernel driver (to be used in beacon and probe
571 * response frames) */
572 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
573 conf->ssid.ssid_len)) {
574 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
575 return -1;
576 }
577
578 if (wpa_debug_level == MSG_MSGDUMP)
579 conf->radius->msg_dumps = 1;
580 #ifndef CONFIG_NO_RADIUS
581 hapd->radius = radius_client_init(hapd, conf->radius);
582 if (hapd->radius == NULL) {
583 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
584 return -1;
585 }
586 #endif /* CONFIG_NO_RADIUS */
587
588 if (hostapd_acl_init(hapd)) {
589 wpa_printf(MSG_ERROR, "ACL initialization failed.");
590 return -1;
591 }
592 if (hostapd_init_wps(hapd, conf))
593 return -1;
594
595 if (authsrv_init(hapd) < 0)
596 return -1;
597
598 if (ieee802_1x_init(hapd)) {
599 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
600 return -1;
601 }
602
603 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
604 return -1;
605
606 if (accounting_init(hapd)) {
607 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
608 return -1;
609 }
610
611 if (hapd->conf->ieee802_11f &&
612 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
613 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
614 "failed.");
615 return -1;
616 }
617
618 if (hapd->iface->ctrl_iface_init &&
619 hapd->iface->ctrl_iface_init(hapd)) {
620 wpa_printf(MSG_ERROR, "Failed to setup control interface");
621 return -1;
622 }
623
624 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
625 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
626 return -1;
627 }
628
629 ieee802_11_set_beacon(hapd);
630
631 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
632 return -1;
633
634 if (hapd->driver && hapd->driver->set_operstate)
635 hapd->driver->set_operstate(hapd->drv_priv, 1);
636
637 return 0;
638 }
639
640
hostapd_tx_queue_params(struct hostapd_iface * iface)641 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
642 {
643 struct hostapd_data *hapd = iface->bss[0];
644 int i;
645 struct hostapd_tx_queue_params *p;
646
647 for (i = 0; i < NUM_TX_QUEUES; i++) {
648 p = &iface->conf->tx_queue[i];
649
650 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
651 p->cwmax, p->burst)) {
652 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
653 "parameters for queue %d.", i);
654 /* Continue anyway */
655 }
656 }
657 }
658
659
setup_interface(struct hostapd_iface * iface)660 static int setup_interface(struct hostapd_iface *iface)
661 {
662 struct hostapd_data *hapd = iface->bss[0];
663 size_t i;
664 char country[4];
665
666 /*
667 * Make sure that all BSSes get configured with a pointer to the same
668 * driver interface.
669 */
670 for (i = 1; i < iface->num_bss; i++) {
671 iface->bss[i]->driver = hapd->driver;
672 iface->bss[i]->drv_priv = hapd->drv_priv;
673 }
674
675 if (hostapd_validate_bssid_configuration(iface))
676 return -1;
677
678 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
679 os_memcpy(country, hapd->iconf->country, 3);
680 country[3] = '\0';
681 if (hostapd_set_country(hapd, country) < 0) {
682 wpa_printf(MSG_ERROR, "Failed to set country code");
683 return -1;
684 }
685 }
686
687 if (hostapd_get_hw_features(iface)) {
688 /* Not all drivers support this yet, so continue without hw
689 * feature data. */
690 } else {
691 int ret = hostapd_select_hw_mode(iface);
692 if (ret < 0) {
693 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
694 "channel. (%d)", ret);
695 return -1;
696 }
697 ret = hostapd_check_ht_capab(iface);
698 if (ret < 0)
699 return -1;
700 if (ret == 1) {
701 wpa_printf(MSG_DEBUG, "Interface initialization will "
702 "be completed in a callback");
703 return 0;
704 }
705 }
706 return hostapd_setup_interface_complete(iface, 0);
707 }
708
709
hostapd_setup_interface_complete(struct hostapd_iface * iface,int err)710 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
711 {
712 struct hostapd_data *hapd = iface->bss[0];
713 size_t j;
714 u8 *prev_addr;
715
716 if (err) {
717 wpa_printf(MSG_ERROR, "Interface initialization failed");
718 eloop_terminate();
719 return -1;
720 }
721
722 wpa_printf(MSG_DEBUG, "Completing interface initialization");
723 if (hapd->iconf->channel) {
724 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
725 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
726 "Frequency: %d MHz",
727 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
728 hapd->iconf->channel, iface->freq);
729
730 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
731 hapd->iconf->channel,
732 hapd->iconf->ieee80211n,
733 hapd->iconf->secondary_channel)) {
734 wpa_printf(MSG_ERROR, "Could not set channel for "
735 "kernel driver");
736 return -1;
737 }
738 }
739
740 if (iface->current_mode) {
741 if (hostapd_prepare_rates(iface, iface->current_mode)) {
742 wpa_printf(MSG_ERROR, "Failed to prepare rates "
743 "table.");
744 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
745 HOSTAPD_LEVEL_WARNING,
746 "Failed to prepare rates table.");
747 return -1;
748 }
749 }
750
751 if (hapd->iconf->rts_threshold > -1 &&
752 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
753 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
754 "kernel driver");
755 return -1;
756 }
757
758 if (hapd->iconf->fragm_threshold > -1 &&
759 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
760 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
761 "for kernel driver");
762 return -1;
763 }
764
765 prev_addr = hapd->own_addr;
766
767 for (j = 0; j < iface->num_bss; j++) {
768 hapd = iface->bss[j];
769 if (j)
770 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
771 if (hostapd_setup_bss(hapd, j == 0))
772 return -1;
773 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
774 prev_addr = hapd->own_addr;
775 }
776
777 hostapd_tx_queue_params(iface);
778
779 ap_list_init(iface);
780
781 if (hostapd_driver_commit(hapd) < 0) {
782 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
783 "configuration", __func__);
784 return -1;
785 }
786
787 /*
788 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
789 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
790 * mode), the interface is up only after driver_commit, so initialize
791 * WPS after driver_commit.
792 */
793 for (j = 0; j < iface->num_bss; j++) {
794 if (hostapd_init_wps_complete(iface->bss[j]))
795 return -1;
796 }
797
798 if (hapd->setup_complete_cb)
799 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
800
801 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
802 iface->bss[0]->conf->iface);
803
804 return 0;
805 }
806
807
808 /**
809 * hostapd_setup_interface - Setup of an interface
810 * @iface: Pointer to interface data.
811 * Returns: 0 on success, -1 on failure
812 *
813 * Initializes the driver interface, validates the configuration,
814 * and sets driver parameters based on the configuration.
815 * Flushes old stations, sets the channel, encryption,
816 * beacons, and WDS links based on the configuration.
817 */
hostapd_setup_interface(struct hostapd_iface * iface)818 int hostapd_setup_interface(struct hostapd_iface *iface)
819 {
820 int ret;
821
822 ret = setup_interface(iface);
823 if (ret) {
824 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
825 iface->bss[0]->conf->iface);
826 return -1;
827 }
828
829 return 0;
830 }
831
832
833 /**
834 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
835 * @hapd_iface: Pointer to interface data
836 * @conf: Pointer to per-interface configuration
837 * @bss: Pointer to per-BSS configuration for this BSS
838 * Returns: Pointer to allocated BSS data
839 *
840 * This function is used to allocate per-BSS data structure. This data will be
841 * freed after hostapd_cleanup() is called for it during interface
842 * deinitialization.
843 */
844 struct hostapd_data *
hostapd_alloc_bss_data(struct hostapd_iface * hapd_iface,struct hostapd_config * conf,struct hostapd_bss_config * bss)845 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
846 struct hostapd_config *conf,
847 struct hostapd_bss_config *bss)
848 {
849 struct hostapd_data *hapd;
850
851 hapd = os_zalloc(sizeof(*hapd));
852 if (hapd == NULL)
853 return NULL;
854
855 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
856 hapd->iconf = conf;
857 hapd->conf = bss;
858 hapd->iface = hapd_iface;
859 hapd->driver = hapd->iconf->driver;
860
861 return hapd;
862 }
863
864
hostapd_interface_deinit(struct hostapd_iface * iface)865 void hostapd_interface_deinit(struct hostapd_iface *iface)
866 {
867 size_t j;
868
869 if (iface == NULL)
870 return;
871
872 hostapd_cleanup_iface_pre(iface);
873 for (j = 0; j < iface->num_bss; j++) {
874 struct hostapd_data *hapd = iface->bss[j];
875 hostapd_free_stas(hapd);
876 hostapd_flush_old_stations(hapd);
877 hostapd_cleanup(hapd);
878 }
879 }
880
881
hostapd_interface_free(struct hostapd_iface * iface)882 void hostapd_interface_free(struct hostapd_iface *iface)
883 {
884 size_t j;
885 for (j = 0; j < iface->num_bss; j++)
886 os_free(iface->bss[j]);
887 hostapd_cleanup_iface(iface);
888 }
889
890
891 /**
892 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
893 * @hapd: Pointer to BSS data
894 * @sta: Pointer to the associated STA data
895 * @reassoc: 1 to indicate this was a re-association; 0 = first association
896 *
897 * This function will be called whenever a station associates with the AP. It
898 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
899 * from drv_callbacks.c based on driver events for drivers that take care of
900 * management frames (IEEE 802.11 authentication and association) internally.
901 */
hostapd_new_assoc_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)902 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
903 int reassoc)
904 {
905 if (hapd->tkip_countermeasures) {
906 hostapd_drv_sta_deauth(hapd, sta->addr,
907 WLAN_REASON_MICHAEL_MIC_FAILURE);
908 return;
909 }
910
911 hostapd_prune_associations(hapd, sta->addr);
912
913 /* IEEE 802.11F (IAPP) */
914 if (hapd->conf->ieee802_11f)
915 iapp_new_station(hapd->iapp, sta);
916
917 #ifdef CONFIG_P2P
918 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
919 sta->no_p2p_set = 1;
920 hapd->num_sta_no_p2p++;
921 if (hapd->num_sta_no_p2p == 1)
922 hostapd_p2p_non_p2p_sta_connected(hapd);
923 }
924 #endif /* CONFIG_P2P */
925
926 /* Start accounting here, if IEEE 802.1X and WPA are not used.
927 * IEEE 802.1X/WPA code will start accounting after the station has
928 * been authorized. */
929 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
930 accounting_sta_start(hapd, sta);
931
932 /* Start IEEE 802.1X authentication process for new stations */
933 ieee802_1x_new_station(hapd, sta);
934 if (reassoc) {
935 if (sta->auth_alg != WLAN_AUTH_FT &&
936 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
937 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
938 } else
939 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
940 }
941