DFS: Convert hostapd_data use to hostapd_iface
[mech_eap.git] / src / ap / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2013, 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 "common/wpa_ctrl.h"
15 #include "radius/radius_client.h"
16 #include "radius/radius_das.h"
17 #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"
35 #include "gas_serv.h"
36 #include "dfs.h"
37
38
39 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
40 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
41 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
42 static int setup_interface2(struct hostapd_iface *iface);
43 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
44
45 extern int wpa_debug_level;
46 extern struct wpa_driver_ops *wpa_drivers[];
47
48
49 int 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
66 static void hostapd_reload_bss(struct hostapd_data *hapd)
67 {
68         struct hostapd_ssid *ssid;
69
70 #ifndef CONFIG_NO_RADIUS
71         radius_client_reconfig(hapd->radius, hapd->conf->radius);
72 #endif /* CONFIG_NO_RADIUS */
73
74         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         }
84         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
94         if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
95                 hostapd_setup_wpa(hapd);
96                 if (hapd->wpa_auth)
97                         wpa_init_keys(hapd->wpa_auth);
98         } else if (hapd->conf->wpa) {
99                 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 &&
118             hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
119                              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
127 static void hostapd_clear_old(struct hostapd_iface *iface)
128 {
129         size_t j;
130
131         /*
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++) {
136                 hostapd_flush_old_stations(iface->bss[j],
137                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
138                 hostapd_broadcast_wep_clear(iface->bss[j]);
139
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         }
146 }
147
148
149 int 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);
171
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;
178                 hapd->conf = newconf->bss[j];
179                 hostapd_reload_bss(hapd);
180         }
181
182         hostapd_config_free(oldconf);
183
184
185         return 0;
186 }
187
188
189 static 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
218 static 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
225 static 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
240         return errors;
241 }
242
243
244 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
245 {
246         iapp_deinit(hapd->iapp);
247         hapd->iapp = NULL;
248         accounting_deinit(hapd);
249         hostapd_deinit_wpa(hapd);
250         vlan_deinit(hapd);
251         hostapd_acl_deinit(hapd);
252 #ifndef CONFIG_NO_RADIUS
253         radius_client_deinit(hapd->radius);
254         hapd->radius = NULL;
255         radius_das_deinit(hapd->radius_das);
256         hapd->radius_das = NULL;
257 #endif /* CONFIG_NO_RADIUS */
258
259         hostapd_deinit_wps(hapd);
260
261         authsrv_deinit(hapd);
262
263         if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
264                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
265                            hapd->conf->iface);
266         }
267
268         os_free(hapd->probereq_cb);
269         hapd->probereq_cb = NULL;
270
271 #ifdef CONFIG_P2P
272         wpabuf_free(hapd->p2p_beacon_ie);
273         hapd->p2p_beacon_ie = NULL;
274         wpabuf_free(hapd->p2p_probe_resp_ie);
275         hapd->p2p_probe_resp_ie = NULL;
276 #endif /* CONFIG_P2P */
277
278         wpabuf_free(hapd->time_adv);
279
280 #ifdef CONFIG_INTERWORKING
281         gas_serv_deinit(hapd);
282 #endif /* CONFIG_INTERWORKING */
283
284 #ifdef CONFIG_SQLITE
285         os_free(hapd->tmp_eap_user.identity);
286         os_free(hapd->tmp_eap_user.password);
287 #endif /* CONFIG_SQLITE */
288 }
289
290
291 /**
292  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
293  * @hapd: Pointer to BSS data
294  *
295  * This function is used to free all per-BSS data structures and resources.
296  * This gets called in a loop for each BSS between calls to
297  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
298  * is deinitialized. Most of the modules that are initialized in
299  * hostapd_setup_bss() are deinitialized here.
300  */
301 static void hostapd_cleanup(struct hostapd_data *hapd)
302 {
303         if (hapd->iface->interfaces &&
304             hapd->iface->interfaces->ctrl_iface_deinit)
305                 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
306         hostapd_free_hapd_data(hapd);
307 }
308
309
310 /**
311  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
312  * @iface: Pointer to interface data
313  *
314  * This function is called before per-BSS data structures are deinitialized
315  * with hostapd_cleanup().
316  */
317 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
318 {
319 }
320
321
322 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
323 {
324         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
325         iface->hw_features = NULL;
326         os_free(iface->current_rates);
327         iface->current_rates = NULL;
328         os_free(iface->basic_rates);
329         iface->basic_rates = NULL;
330         ap_list_deinit(iface);
331 }
332
333
334 /**
335  * hostapd_cleanup_iface - Complete per-interface cleanup
336  * @iface: Pointer to interface data
337  *
338  * This function is called after per-BSS data structures are deinitialized
339  * with hostapd_cleanup().
340  */
341 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
342 {
343         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
344
345         hostapd_cleanup_iface_partial(iface);
346         hostapd_config_free(iface->conf);
347         iface->conf = NULL;
348
349         os_free(iface->config_fname);
350         os_free(iface->bss);
351         os_free(iface);
352 }
353
354
355 static 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
364 static 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
375         /*
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
381         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
400 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
401 {
402         int ret = 0;
403         u8 addr[ETH_ALEN];
404
405         if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
406                 return 0;
407
408         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
409         if (hostapd_flush(hapd)) {
410                 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
411                         "kernel driver");
412                 ret = -1;
413         }
414         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
415         os_memset(addr, 0xff, ETH_ALEN);
416         hostapd_drv_sta_deauth(hapd, addr, reason);
417         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  */
430 static 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;
435         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++) {
449                 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
450                         if (j)
451                                 auto_addr++;
452                         continue;
453                 }
454
455                 for (i = 0; i < ETH_ALEN; i++) {
456                         mask[i] |=
457                                 iface->conf->bss[j]->bssid[i] ^
458                                 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
494 skip_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
498         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
517 static 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++) {
522                 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
523                         return 1;
524                 }
525         }
526
527         return 0;
528 }
529
530
531 #ifndef CONFIG_NO_RADIUS
532
533 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
534                                     struct radius_das_attrs *attr)
535 {
536         /* TODO */
537         return 0;
538 }
539
540
541 static 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
590 static enum radius_das_res
591 hostapd_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 */
611
612
613 /**
614  * hostapd_setup_bss - Per-BSS setup (initialization)
615  * @hapd: Pointer to BSS data
616  * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
617  *      but interface may exist
618  *
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  */
624 static 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
632         if (!first || first == -1) {
633                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
634                         /* Allocate the next available BSSID. */
635                         do {
636                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
637                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
638                 } else {
639                         /* Allocate the configured BSSID. */
640                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
641
642                         if (hostapd_mac_comp(hapd->own_addr,
643                                              hapd->iface->bss[0]->own_addr) ==
644                             0) {
645                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
646                                            "BSSID set to the MAC address of "
647                                            "the radio", hapd->conf->iface);
648                                 return -1;
649                         }
650                 }
651
652                 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
653                                    hapd->conf->iface, hapd->own_addr, hapd,
654                                    &hapd->drv_priv, force_ifname, if_addr,
655                                    hapd->conf->bridge[0] ? hapd->conf->bridge :
656                                    NULL, first == -1)) {
657                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
658                                    MACSTR ")", MAC2STR(hapd->own_addr));
659                         return -1;
660                 }
661         }
662
663         if (conf->wmm_enabled < 0)
664                 conf->wmm_enabled = hapd->iconf->ieee80211n;
665
666         hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
667         hostapd_set_privacy(hapd, 0);
668
669         hostapd_broadcast_wep_clear(hapd);
670         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
671                 return -1;
672
673         /*
674          * Fetch the SSID from the system and use it or,
675          * if one was specified in the config file, verify they
676          * match.
677          */
678         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
679         if (ssid_len < 0) {
680                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
681                 return -1;
682         }
683         if (conf->ssid.ssid_set) {
684                 /*
685                  * If SSID is specified in the config file and it differs
686                  * from what is being used then force installation of the
687                  * new SSID.
688                  */
689                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
690                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
691         } else {
692                 /*
693                  * No SSID in the config file; just use the one we got
694                  * from the system.
695                  */
696                 set_ssid = 0;
697                 conf->ssid.ssid_len = ssid_len;
698                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
699         }
700
701         if (!hostapd_drv_none(hapd)) {
702                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
703                            " and ssid \"%s\"",
704                            hapd->conf->iface, MAC2STR(hapd->own_addr),
705                            wpa_ssid_txt(hapd->conf->ssid.ssid,
706                                         hapd->conf->ssid.ssid_len));
707         }
708
709         if (hostapd_setup_wpa_psk(conf)) {
710                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
711                 return -1;
712         }
713
714         /* Set SSID for the kernel driver (to be used in beacon and probe
715          * response frames) */
716         if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
717                                          conf->ssid.ssid_len)) {
718                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
719                 return -1;
720         }
721
722         if (wpa_debug_level == MSG_MSGDUMP)
723                 conf->radius->msg_dumps = 1;
724 #ifndef CONFIG_NO_RADIUS
725         hapd->radius = radius_client_init(hapd, conf->radius);
726         if (hapd->radius == NULL) {
727                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
728                 return -1;
729         }
730
731         if (hapd->conf->radius_das_port) {
732                 struct radius_das_conf das_conf;
733                 os_memset(&das_conf, 0, sizeof(das_conf));
734                 das_conf.port = hapd->conf->radius_das_port;
735                 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
736                 das_conf.shared_secret_len =
737                         hapd->conf->radius_das_shared_secret_len;
738                 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
739                 das_conf.time_window = hapd->conf->radius_das_time_window;
740                 das_conf.require_event_timestamp =
741                         hapd->conf->radius_das_require_event_timestamp;
742                 das_conf.ctx = hapd;
743                 das_conf.disconnect = hostapd_das_disconnect;
744                 hapd->radius_das = radius_das_init(&das_conf);
745                 if (hapd->radius_das == NULL) {
746                         wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
747                                    "failed.");
748                         return -1;
749                 }
750         }
751 #endif /* CONFIG_NO_RADIUS */
752
753         if (hostapd_acl_init(hapd)) {
754                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
755                 return -1;
756         }
757         if (hostapd_init_wps(hapd, conf))
758                 return -1;
759
760         if (authsrv_init(hapd) < 0)
761                 return -1;
762
763         if (ieee802_1x_init(hapd)) {
764                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
765                 return -1;
766         }
767
768         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
769                 return -1;
770
771         if (accounting_init(hapd)) {
772                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
773                 return -1;
774         }
775
776         if (hapd->conf->ieee802_11f &&
777             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
778                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
779                            "failed.");
780                 return -1;
781         }
782
783 #ifdef CONFIG_INTERWORKING
784         if (gas_serv_init(hapd)) {
785                 wpa_printf(MSG_ERROR, "GAS server initialization failed");
786                 return -1;
787         }
788 #endif /* CONFIG_INTERWORKING */
789
790         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
791                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
792                 return -1;
793         }
794
795         if (!hapd->conf->start_disabled)
796                 ieee802_11_set_beacon(hapd);
797
798         if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
799                 return -1;
800
801         if (hapd->driver && hapd->driver->set_operstate)
802                 hapd->driver->set_operstate(hapd->drv_priv, 1);
803
804         return 0;
805 }
806
807
808 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
809 {
810         struct hostapd_data *hapd = iface->bss[0];
811         int i;
812         struct hostapd_tx_queue_params *p;
813
814         for (i = 0; i < NUM_TX_QUEUES; i++) {
815                 p = &iface->conf->tx_queue[i];
816
817                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
818                                                 p->cwmax, p->burst)) {
819                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
820                                    "parameters for queue %d.", i);
821                         /* Continue anyway */
822                 }
823         }
824 }
825
826
827 static int hostapd_set_acl_list(struct hostapd_data *hapd,
828                                 struct mac_acl_entry *mac_acl,
829                                 int n_entries, u8 accept_acl)
830 {
831         struct hostapd_acl_params *acl_params;
832         int i, err;
833
834         acl_params = os_zalloc(sizeof(*acl_params) +
835                                (n_entries * sizeof(acl_params->mac_acl[0])));
836         if (!acl_params)
837                 return -ENOMEM;
838
839         for (i = 0; i < n_entries; i++)
840                 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
841                           ETH_ALEN);
842
843         acl_params->acl_policy = accept_acl;
844         acl_params->num_mac_acl = n_entries;
845
846         err = hostapd_drv_set_acl(hapd, acl_params);
847
848         os_free(acl_params);
849
850         return err;
851 }
852
853
854 static void hostapd_set_acl(struct hostapd_data *hapd)
855 {
856         struct hostapd_config *conf = hapd->iconf;
857         int err;
858         u8 accept_acl;
859
860         if (hapd->iface->drv_max_acl_mac_addrs == 0)
861                 return;
862         if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
863                 return;
864
865         if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
866                 if (conf->bss[0]->num_accept_mac) {
867                         accept_acl = 1;
868                         err = hostapd_set_acl_list(hapd,
869                                                    conf->bss[0]->accept_mac,
870                                                    conf->bss[0]->num_accept_mac,
871                                                    accept_acl);
872                         if (err) {
873                                 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
874                                 return;
875                         }
876                 } else {
877                         wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
878                 }
879         } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
880                 if (conf->bss[0]->num_deny_mac) {
881                         accept_acl = 0;
882                         err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
883                                                    conf->bss[0]->num_deny_mac,
884                                                    accept_acl);
885                         if (err) {
886                                 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
887                                 return;
888                         }
889                 } else {
890                         wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
891                 }
892         }
893 }
894
895
896 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
897 {
898         if (!hapd->iface->interfaces ||
899             !hapd->iface->interfaces->ctrl_iface_init)
900                 return 0;
901
902         if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
903                 wpa_printf(MSG_ERROR,
904                            "Failed to setup control interface for %s",
905                            hapd->conf->iface);
906                 return -1;
907         }
908
909         return 0;
910 }
911
912
913 static int start_ctrl_iface(struct hostapd_iface *iface)
914 {
915         size_t i;
916
917         if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
918                 return 0;
919
920         for (i = 0; i < iface->num_bss; i++) {
921                 struct hostapd_data *hapd = iface->bss[i];
922                 if (iface->interfaces->ctrl_iface_init(hapd)) {
923                         wpa_printf(MSG_ERROR,
924                                    "Failed to setup control interface for %s",
925                                    hapd->conf->iface);
926                         return -1;
927                 }
928         }
929
930         return 0;
931 }
932
933
934 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
935 {
936         struct hostapd_iface *iface = eloop_ctx;
937
938         if (!iface->wait_channel_update) {
939                 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
940                 return;
941         }
942
943         /*
944          * It is possible that the existing channel list is acceptable, so try
945          * to proceed.
946          */
947         wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
948         setup_interface2(iface);
949 }
950
951
952 void hostapd_channel_list_updated(struct hostapd_iface *iface)
953 {
954         if (!iface->wait_channel_update)
955                 return;
956
957         wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
958         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
959         setup_interface2(iface);
960 }
961
962
963 static int setup_interface(struct hostapd_iface *iface)
964 {
965         struct hostapd_data *hapd = iface->bss[0];
966         size_t i;
967
968         if (!iface->phy[0]) {
969                 const char *phy = hostapd_drv_get_radio_name(hapd);
970                 if (phy) {
971                         wpa_printf(MSG_DEBUG, "phy: %s", phy);
972                         os_strlcpy(iface->phy, phy, sizeof(iface->phy));
973                 }
974         }
975
976         /*
977          * Make sure that all BSSes get configured with a pointer to the same
978          * driver interface.
979          */
980         for (i = 1; i < iface->num_bss; i++) {
981                 iface->bss[i]->driver = hapd->driver;
982                 iface->bss[i]->drv_priv = hapd->drv_priv;
983         }
984
985         if (hostapd_validate_bssid_configuration(iface))
986                 return -1;
987
988         /*
989          * Initialize control interfaces early to allow external monitoring of
990          * channel setup operations that may take considerable amount of time
991          * especially for DFS cases.
992          */
993         if (start_ctrl_iface(iface))
994                 return -1;
995
996         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
997                 char country[4], previous_country[4];
998
999                 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1000                 if (hostapd_get_country(hapd, previous_country) < 0)
1001                         previous_country[0] = '\0';
1002
1003                 os_memcpy(country, hapd->iconf->country, 3);
1004                 country[3] = '\0';
1005                 if (hostapd_set_country(hapd, country) < 0) {
1006                         wpa_printf(MSG_ERROR, "Failed to set country code");
1007                         return -1;
1008                 }
1009
1010                 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1011                            previous_country, country);
1012
1013                 if (os_strncmp(previous_country, country, 2) != 0) {
1014                         wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1015                         iface->wait_channel_update = 1;
1016                         eloop_register_timeout(1, 0,
1017                                                channel_list_update_timeout,
1018                                                iface, NULL);
1019                         return 0;
1020                 }
1021         }
1022
1023         return setup_interface2(iface);
1024 }
1025
1026
1027 static int setup_interface2(struct hostapd_iface *iface)
1028 {
1029         iface->wait_channel_update = 0;
1030
1031         if (hostapd_get_hw_features(iface)) {
1032                 /* Not all drivers support this yet, so continue without hw
1033                  * feature data. */
1034         } else {
1035                 int ret = hostapd_select_hw_mode(iface);
1036                 if (ret < 0) {
1037                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1038                                    "channel. (%d)", ret);
1039                         return -1;
1040                 }
1041                 if (ret == 1) {
1042                         wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1043                         return 0;
1044                 }
1045                 ret = hostapd_check_ht_capab(iface);
1046                 if (ret < 0)
1047                         return -1;
1048                 if (ret == 1) {
1049                         wpa_printf(MSG_DEBUG, "Interface initialization will "
1050                                    "be completed in a callback");
1051                         return 0;
1052                 }
1053
1054                 if (iface->conf->ieee80211h)
1055                         wpa_printf(MSG_DEBUG, "DFS support is enabled");
1056         }
1057         return hostapd_setup_interface_complete(iface, 0);
1058 }
1059
1060
1061 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1062 {
1063         struct hostapd_data *hapd = iface->bss[0];
1064         size_t j;
1065         u8 *prev_addr;
1066
1067         if (err) {
1068                 wpa_printf(MSG_ERROR, "Interface initialization failed");
1069                 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1070                 eloop_terminate();
1071                 return -1;
1072         }
1073
1074         wpa_printf(MSG_DEBUG, "Completing interface initialization");
1075         if (iface->conf->channel) {
1076 #ifdef NEED_AP_MLME
1077                 int res;
1078 #endif /* NEED_AP_MLME */
1079
1080                 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1081                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1082                            "Frequency: %d MHz",
1083                            hostapd_hw_mode_txt(iface->conf->hw_mode),
1084                            iface->conf->channel, iface->freq);
1085
1086 #ifdef NEED_AP_MLME
1087                 /* Check DFS */
1088                 res = hostapd_handle_dfs(iface);
1089                 if (res <= 0)
1090                         return res;
1091 #endif /* NEED_AP_MLME */
1092
1093                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1094                                      hapd->iconf->channel,
1095                                      hapd->iconf->ieee80211n,
1096                                      hapd->iconf->ieee80211ac,
1097                                      hapd->iconf->secondary_channel,
1098                                      hapd->iconf->vht_oper_chwidth,
1099                                      hapd->iconf->vht_oper_centr_freq_seg0_idx,
1100                                      hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1101                         wpa_printf(MSG_ERROR, "Could not set channel for "
1102                                    "kernel driver");
1103                         return -1;
1104                 }
1105         }
1106
1107         if (iface->current_mode) {
1108                 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1109                         wpa_printf(MSG_ERROR, "Failed to prepare rates "
1110                                    "table.");
1111                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1112                                        HOSTAPD_LEVEL_WARNING,
1113                                        "Failed to prepare rates table.");
1114                         return -1;
1115                 }
1116         }
1117
1118         if (hapd->iconf->rts_threshold > -1 &&
1119             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1120                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1121                            "kernel driver");
1122                 return -1;
1123         }
1124
1125         if (hapd->iconf->fragm_threshold > -1 &&
1126             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1127                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1128                            "for kernel driver");
1129                 return -1;
1130         }
1131
1132         prev_addr = hapd->own_addr;
1133
1134         for (j = 0; j < iface->num_bss; j++) {
1135                 hapd = iface->bss[j];
1136                 if (j)
1137                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1138                 if (hostapd_setup_bss(hapd, j == 0))
1139                         return -1;
1140                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1141                         prev_addr = hapd->own_addr;
1142         }
1143         hapd = iface->bss[0];
1144
1145         hostapd_tx_queue_params(iface);
1146
1147         ap_list_init(iface);
1148
1149         hostapd_set_acl(hapd);
1150
1151         if (hostapd_driver_commit(hapd) < 0) {
1152                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1153                            "configuration", __func__);
1154                 return -1;
1155         }
1156
1157         /*
1158          * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1159          * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1160          * mode), the interface is up only after driver_commit, so initialize
1161          * WPS after driver_commit.
1162          */
1163         for (j = 0; j < iface->num_bss; j++) {
1164                 if (hostapd_init_wps_complete(iface->bss[j]))
1165                         return -1;
1166         }
1167
1168         hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1169         wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1170         if (hapd->setup_complete_cb)
1171                 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1172
1173         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1174                    iface->bss[0]->conf->iface);
1175
1176         return 0;
1177 }
1178
1179
1180 /**
1181  * hostapd_setup_interface - Setup of an interface
1182  * @iface: Pointer to interface data.
1183  * Returns: 0 on success, -1 on failure
1184  *
1185  * Initializes the driver interface, validates the configuration,
1186  * and sets driver parameters based on the configuration.
1187  * Flushes old stations, sets the channel, encryption,
1188  * beacons, and WDS links based on the configuration.
1189  */
1190 int hostapd_setup_interface(struct hostapd_iface *iface)
1191 {
1192         int ret;
1193
1194         ret = setup_interface(iface);
1195         if (ret) {
1196                 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1197                            iface->bss[0]->conf->iface);
1198                 return -1;
1199         }
1200
1201         return 0;
1202 }
1203
1204
1205 /**
1206  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1207  * @hapd_iface: Pointer to interface data
1208  * @conf: Pointer to per-interface configuration
1209  * @bss: Pointer to per-BSS configuration for this BSS
1210  * Returns: Pointer to allocated BSS data
1211  *
1212  * This function is used to allocate per-BSS data structure. This data will be
1213  * freed after hostapd_cleanup() is called for it during interface
1214  * deinitialization.
1215  */
1216 struct hostapd_data *
1217 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1218                        struct hostapd_config *conf,
1219                        struct hostapd_bss_config *bss)
1220 {
1221         struct hostapd_data *hapd;
1222
1223         hapd = os_zalloc(sizeof(*hapd));
1224         if (hapd == NULL)
1225                 return NULL;
1226
1227         hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1228         hapd->iconf = conf;
1229         hapd->conf = bss;
1230         hapd->iface = hapd_iface;
1231         hapd->driver = hapd->iconf->driver;
1232         hapd->ctrl_sock = -1;
1233
1234         return hapd;
1235 }
1236
1237
1238 void hostapd_interface_deinit(struct hostapd_iface *iface)
1239 {
1240         int j;
1241
1242         if (iface == NULL)
1243                 return;
1244
1245         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1246         iface->wait_channel_update = 0;
1247
1248         hostapd_cleanup_iface_pre(iface);
1249         for (j = iface->num_bss - 1; j >= 0; j--) {
1250                 struct hostapd_data *hapd = iface->bss[j];
1251                 hostapd_free_stas(hapd);
1252                 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1253                 hostapd_clear_wep(hapd);
1254                 hostapd_cleanup(hapd);
1255         }
1256 }
1257
1258
1259 void hostapd_interface_free(struct hostapd_iface *iface)
1260 {
1261         size_t j;
1262         for (j = 0; j < iface->num_bss; j++)
1263                 os_free(iface->bss[j]);
1264         hostapd_cleanup_iface(iface);
1265 }
1266
1267
1268 /**
1269  * hostapd_init - Allocate and initialize per-interface data
1270  * @config_file: Path to the configuration file
1271  * Returns: Pointer to the allocated interface data or %NULL on failure
1272  *
1273  * This function is used to allocate main data structures for per-interface
1274  * data. The allocated data buffer will be freed by calling
1275  * hostapd_cleanup_iface().
1276  */
1277 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1278                                     const char *config_file)
1279 {
1280         struct hostapd_iface *hapd_iface = NULL;
1281         struct hostapd_config *conf = NULL;
1282         struct hostapd_data *hapd;
1283         size_t i;
1284
1285         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1286         if (hapd_iface == NULL)
1287                 goto fail;
1288
1289         hapd_iface->config_fname = os_strdup(config_file);
1290         if (hapd_iface->config_fname == NULL)
1291                 goto fail;
1292
1293         conf = interfaces->config_read_cb(hapd_iface->config_fname);
1294         if (conf == NULL)
1295                 goto fail;
1296         hapd_iface->conf = conf;
1297
1298         hapd_iface->num_bss = conf->num_bss;
1299         hapd_iface->bss = os_calloc(conf->num_bss,
1300                                     sizeof(struct hostapd_data *));
1301         if (hapd_iface->bss == NULL)
1302                 goto fail;
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                         goto fail;
1310                 hapd->msg_ctx = hapd;
1311         }
1312
1313         return hapd_iface;
1314
1315 fail:
1316         wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1317                    config_file);
1318         if (conf)
1319                 hostapd_config_free(conf);
1320         if (hapd_iface) {
1321                 os_free(hapd_iface->config_fname);
1322                 os_free(hapd_iface->bss);
1323                 os_free(hapd_iface);
1324         }
1325         return NULL;
1326 }
1327
1328
1329 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1330 {
1331         size_t i, j;
1332
1333         for (i = 0; i < interfaces->count; i++) {
1334                 struct hostapd_iface *iface = interfaces->iface[i];
1335                 for (j = 0; j < iface->num_bss; j++) {
1336                         struct hostapd_data *hapd = iface->bss[j];
1337                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
1338                                 return 1;
1339                 }
1340         }
1341
1342         return 0;
1343 }
1344
1345
1346 struct hostapd_iface *
1347 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1348                            const char *config_fname, int debug)
1349 {
1350         struct hostapd_iface *new_iface = NULL, *iface = NULL;
1351         struct hostapd_data *hapd;
1352         int k;
1353         size_t i, bss_idx;
1354
1355         if (!phy || !*phy)
1356                 return NULL;
1357
1358         for (i = 0; i < interfaces->count; i++) {
1359                 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1360                         iface = interfaces->iface[i];
1361                         break;
1362                 }
1363         }
1364
1365         wpa_printf(MSG_ERROR, "Configuration file: %s (phy %s)%s",
1366                    config_fname, phy, iface ? "" : " --> new PHY");
1367         if (iface) {
1368                 struct hostapd_config *conf;
1369                 struct hostapd_bss_config **tmp_conf;
1370                 struct hostapd_data **tmp_bss;
1371                 struct hostapd_bss_config *bss;
1372                 const char *ifname;
1373
1374                 /* Add new BSS to existing iface */
1375                 conf = interfaces->config_read_cb(config_fname);
1376                 if (conf == NULL)
1377                         return NULL;
1378                 if (conf->num_bss > 1) {
1379                         wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1380                         hostapd_config_free(conf);
1381                         return NULL;
1382                 }
1383
1384                 ifname = conf->bss[0]->iface;
1385                 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1386                         wpa_printf(MSG_ERROR,
1387                                    "Interface name %s already in use", ifname);
1388                         hostapd_config_free(conf);
1389                         return NULL;
1390                 }
1391
1392                 tmp_conf = os_realloc_array(
1393                         iface->conf->bss, iface->conf->num_bss + 1,
1394                         sizeof(struct hostapd_bss_config *));
1395                 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1396                                            sizeof(struct hostapd_data *));
1397                 if (tmp_bss)
1398                         iface->bss = tmp_bss;
1399                 if (tmp_conf) {
1400                         iface->conf->bss = tmp_conf;
1401                         iface->conf->last_bss = tmp_conf[0];
1402                 }
1403                 if (tmp_bss == NULL || tmp_conf == NULL) {
1404                         hostapd_config_free(conf);
1405                         return NULL;
1406                 }
1407                 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1408                 iface->conf->num_bss++;
1409
1410                 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1411                 if (hapd == NULL) {
1412                         iface->conf->num_bss--;
1413                         hostapd_config_free(conf);
1414                         return NULL;
1415                 }
1416                 iface->conf->last_bss = bss;
1417                 iface->bss[iface->num_bss] = hapd;
1418                 hapd->msg_ctx = hapd;
1419
1420                 bss_idx = iface->num_bss++;
1421                 conf->num_bss--;
1422                 conf->bss[0] = NULL;
1423                 hostapd_config_free(conf);
1424         } else {
1425                 /* Add a new iface with the first BSS */
1426                 new_iface = iface = hostapd_init(interfaces, config_fname);
1427                 if (!iface)
1428                         return NULL;
1429                 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1430                 iface->interfaces = interfaces;
1431                 bss_idx = 0;
1432         }
1433
1434         for (k = 0; k < debug; k++) {
1435                 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1436                         iface->bss[bss_idx]->conf->logger_stdout_level--;
1437         }
1438
1439         if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1440             !hostapd_drv_none(iface->bss[bss_idx])) {
1441                 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1442                            config_fname);
1443                 if (new_iface)
1444                         hostapd_interface_deinit_free(new_iface);
1445                 return NULL;
1446         }
1447
1448         return iface;
1449 }
1450
1451
1452 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1453 {
1454         const struct wpa_driver_ops *driver;
1455         void *drv_priv;
1456         if (iface == NULL)
1457                 return;
1458         driver = iface->bss[0]->driver;
1459         drv_priv = iface->bss[0]->drv_priv;
1460         hostapd_interface_deinit(iface);
1461         if (driver && driver->hapd_deinit && drv_priv)
1462                 driver->hapd_deinit(drv_priv);
1463         hostapd_interface_free(iface);
1464 }
1465
1466
1467 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1468 {
1469         if (hapd_iface->bss[0]->drv_priv != NULL) {
1470                 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1471                            hapd_iface->conf->bss[0]->iface);
1472                 return -1;
1473         }
1474
1475         wpa_printf(MSG_DEBUG, "Enable interface %s",
1476                    hapd_iface->conf->bss[0]->iface);
1477
1478         if (hapd_iface->interfaces == NULL ||
1479             hapd_iface->interfaces->driver_init == NULL ||
1480             hapd_iface->interfaces->driver_init(hapd_iface))
1481                 return -1;
1482
1483         if (hostapd_setup_interface(hapd_iface)) {
1484                 const struct wpa_driver_ops *driver;
1485                 void *drv_priv;
1486
1487                 driver = hapd_iface->bss[0]->driver;
1488                 drv_priv = hapd_iface->bss[0]->drv_priv;
1489                 if (driver && driver->hapd_deinit && drv_priv) {
1490                         driver->hapd_deinit(drv_priv);
1491                         hapd_iface->bss[0]->drv_priv = NULL;
1492                 }
1493                 return -1;
1494         }
1495
1496         return 0;
1497 }
1498
1499
1500 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1501 {
1502         size_t j;
1503
1504         wpa_printf(MSG_DEBUG, "Reload interface %s",
1505                    hapd_iface->conf->bss[0]->iface);
1506         for (j = 0; j < hapd_iface->num_bss; j++)
1507                 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1508         if (hostapd_config_check(hapd_iface->conf) < 0) {
1509                 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1510                 return -1;
1511         }
1512         hostapd_clear_old(hapd_iface);
1513         for (j = 0; j < hapd_iface->num_bss; j++)
1514                 hostapd_reload_bss(hapd_iface->bss[j]);
1515
1516         return 0;
1517 }
1518
1519
1520 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1521 {
1522         size_t j;
1523         const struct wpa_driver_ops *driver;
1524         void *drv_priv;
1525
1526         if (hapd_iface == NULL)
1527                 return -1;
1528         wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1529         driver = hapd_iface->bss[0]->driver;
1530         drv_priv = hapd_iface->bss[0]->drv_priv;
1531
1532         /* whatever hostapd_interface_deinit does */
1533         for (j = 0; j < hapd_iface->num_bss; j++) {
1534                 struct hostapd_data *hapd = hapd_iface->bss[j];
1535                 hostapd_free_stas(hapd);
1536                 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1537                 hostapd_clear_wep(hapd);
1538                 hostapd_free_hapd_data(hapd);
1539         }
1540
1541         if (driver && driver->hapd_deinit && drv_priv) {
1542                 driver->hapd_deinit(drv_priv);
1543                 hapd_iface->bss[0]->drv_priv = NULL;
1544         }
1545
1546         /* From hostapd_cleanup_iface: These were initialized in
1547          * hostapd_setup_interface and hostapd_setup_interface_complete
1548          */
1549         hostapd_cleanup_iface_partial(hapd_iface);
1550
1551         wpa_printf(MSG_DEBUG, "Interface %s disabled",
1552                    hapd_iface->bss[0]->conf->iface);
1553         hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1554         return 0;
1555 }
1556
1557
1558 static struct hostapd_iface *
1559 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1560 {
1561         struct hostapd_iface **iface, *hapd_iface;
1562
1563         iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1564                                  sizeof(struct hostapd_iface *));
1565         if (iface == NULL)
1566                 return NULL;
1567         interfaces->iface = iface;
1568         hapd_iface = interfaces->iface[interfaces->count] =
1569                 os_zalloc(sizeof(*hapd_iface));
1570         if (hapd_iface == NULL) {
1571                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1572                            "the interface", __func__);
1573                 return NULL;
1574         }
1575         interfaces->count++;
1576         hapd_iface->interfaces = interfaces;
1577
1578         return hapd_iface;
1579 }
1580
1581
1582 static struct hostapd_config *
1583 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1584                      const char *ctrl_iface)
1585 {
1586         struct hostapd_bss_config *bss;
1587         struct hostapd_config *conf;
1588
1589         /* Allocates memory for bss and conf */
1590         conf = hostapd_config_defaults();
1591         if (conf == NULL) {
1592                  wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1593                                 "configuration", __func__);
1594                 return NULL;
1595         }
1596
1597         conf->driver = wpa_drivers[0];
1598         if (conf->driver == NULL) {
1599                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1600                 hostapd_config_free(conf);
1601                 return NULL;
1602         }
1603
1604         bss = conf->last_bss = conf->bss[0];
1605
1606         os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1607         bss->ctrl_interface = os_strdup(ctrl_iface);
1608         if (bss->ctrl_interface == NULL) {
1609                 hostapd_config_free(conf);
1610                 return NULL;
1611         }
1612
1613         /* Reading configuration file skipped, will be done in SET!
1614          * From reading the configuration till the end has to be done in
1615          * SET
1616          */
1617         return conf;
1618 }
1619
1620
1621 static struct hostapd_iface * hostapd_data_alloc(
1622         struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1623 {
1624         size_t i;
1625         struct hostapd_iface *hapd_iface =
1626                 interfaces->iface[interfaces->count - 1];
1627         struct hostapd_data *hapd;
1628
1629         hapd_iface->conf = conf;
1630         hapd_iface->num_bss = conf->num_bss;
1631
1632         hapd_iface->bss = os_zalloc(conf->num_bss *
1633                                     sizeof(struct hostapd_data *));
1634         if (hapd_iface->bss == NULL)
1635                 return NULL;
1636
1637         for (i = 0; i < conf->num_bss; i++) {
1638                 hapd = hapd_iface->bss[i] =
1639                         hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1640                 if (hapd == NULL)
1641                         return NULL;
1642                 hapd->msg_ctx = hapd;
1643         }
1644
1645         hapd_iface->interfaces = interfaces;
1646
1647         return hapd_iface;
1648 }
1649
1650
1651 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1652 {
1653         struct hostapd_config *conf = NULL;
1654         struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1655         struct hostapd_data *hapd;
1656         char *ptr;
1657         size_t i, j;
1658         const char *conf_file = NULL, *phy_name = NULL;
1659
1660         if (os_strncmp(buf, "bss_config=", 11) == 0) {
1661                 char *pos;
1662                 phy_name = buf + 11;
1663                 pos = os_strchr(phy_name, ':');
1664                 if (!pos)
1665                         return -1;
1666                 *pos++ = '\0';
1667                 conf_file = pos;
1668                 if (!os_strlen(conf_file))
1669                         return -1;
1670
1671                 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1672                                                         conf_file, 0);
1673                 if (!hapd_iface)
1674                         return -1;
1675                 for (j = 0; j < interfaces->count; j++) {
1676                         if (interfaces->iface[j] == hapd_iface)
1677                                 break;
1678                 }
1679                 if (j == interfaces->count) {
1680                         struct hostapd_iface **tmp;
1681                         tmp = os_realloc_array(interfaces->iface,
1682                                                interfaces->count + 1,
1683                                                sizeof(struct hostapd_iface *));
1684                         if (!tmp) {
1685                                 hostapd_interface_deinit_free(hapd_iface);
1686                                 return -1;
1687                         }
1688                         interfaces->iface = tmp;
1689                         interfaces->iface[interfaces->count++] = hapd_iface;
1690                         new_iface = hapd_iface;
1691                 }
1692
1693                 if (new_iface) {
1694                         if (interfaces->driver_init(hapd_iface) ||
1695                             hostapd_setup_interface(hapd_iface)) {
1696                                 interfaces->count--;
1697                                 goto fail;
1698                         }
1699                 } else {
1700                         /* Assign new BSS with bss[0]'s driver info */
1701                         hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1702                         hapd->driver = hapd_iface->bss[0]->driver;
1703                         hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1704                         os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1705                                   ETH_ALEN);
1706
1707                         if (start_ctrl_iface_bss(hapd) < 0 ||
1708                             hostapd_setup_bss(hapd, -1)) {
1709                                 hapd_iface->conf->num_bss--;
1710                                 hapd_iface->num_bss--;
1711                                 os_free(hapd);
1712                                 return -1;
1713                         }
1714                 }
1715                 return 0;
1716         }
1717
1718         ptr = os_strchr(buf, ' ');
1719         if (ptr == NULL)
1720                 return -1;
1721         *ptr++ = '\0';
1722
1723         if (os_strncmp(ptr, "config=", 7) == 0)
1724                 conf_file = ptr + 7;
1725
1726         for (i = 0; i < interfaces->count; i++) {
1727                 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1728                                buf)) {
1729                         wpa_printf(MSG_INFO, "Cannot add interface - it "
1730                                    "already exists");
1731                         return -1;
1732                 }
1733         }
1734
1735         hapd_iface = hostapd_iface_alloc(interfaces);
1736         if (hapd_iface == NULL) {
1737                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1738                            "for interface", __func__);
1739                 goto fail;
1740         }
1741
1742         if (conf_file && interfaces->config_read_cb) {
1743                 conf = interfaces->config_read_cb(conf_file);
1744                 if (conf && conf->bss)
1745                         os_strlcpy(conf->bss[0]->iface, buf,
1746                                    sizeof(conf->bss[0]->iface));
1747         } else
1748                 conf = hostapd_config_alloc(interfaces, buf, ptr);
1749         if (conf == NULL || conf->bss == NULL) {
1750                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1751                            "for configuration", __func__);
1752                 goto fail;
1753         }
1754
1755         hapd_iface = hostapd_data_alloc(interfaces, conf);
1756         if (hapd_iface == NULL) {
1757                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1758                            "for hostapd", __func__);
1759                 goto fail;
1760         }
1761
1762         if (hapd_iface->interfaces &&
1763             hapd_iface->interfaces->ctrl_iface_init &&
1764             hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1765                 wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1766                            "interface", __func__);
1767                 goto fail;
1768         }
1769         wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1770
1771         return 0;
1772
1773 fail:
1774         if (conf)
1775                 hostapd_config_free(conf);
1776         if (hapd_iface) {
1777                 if (hapd_iface->bss) {
1778                         for (i = 0; i < hapd_iface->num_bss; i++)
1779                                 os_free(hapd_iface->bss[i]);
1780                         os_free(hapd_iface->bss);
1781                 }
1782                 os_free(hapd_iface);
1783         }
1784         return -1;
1785 }
1786
1787
1788 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
1789 {
1790         struct hostapd_data *hapd;
1791         size_t i;
1792
1793         if (idx > iface->num_bss || idx > iface->conf->num_bss)
1794                 return -1;
1795         hapd = iface->bss[idx];
1796         wpa_printf(MSG_INFO, "Remove BSS '%s'", hapd->conf->iface);
1797
1798         hostapd_free_stas(hapd);
1799         hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1800         hostapd_clear_wep(hapd);
1801         hostapd_cleanup(hapd);
1802         hostapd_config_free_bss(hapd->conf);
1803         os_free(hapd);
1804
1805         iface->num_bss--;
1806         for (i = idx; i < iface->num_bss; i++)
1807                 iface->bss[i] = iface->bss[i + 1];
1808
1809         iface->conf->num_bss--;
1810         for (i = idx; i < iface->num_bss; i++)
1811                 iface->conf->bss[i] = iface->conf->bss[i + 1];
1812
1813         return 0;
1814 }
1815
1816
1817 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1818 {
1819         struct hostapd_iface *hapd_iface;
1820         size_t i, j, k = 0;
1821
1822         for (i = 0; i < interfaces->count; i++) {
1823                 hapd_iface = interfaces->iface[i];
1824                 if (hapd_iface == NULL)
1825                         return -1;
1826                 if (hapd_iface->conf->num_bss == 1 &&
1827                     !os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
1828                         wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1829                         hostapd_interface_deinit_free(hapd_iface);
1830                         k = i;
1831                         while (k < (interfaces->count - 1)) {
1832                                 interfaces->iface[k] =
1833                                         interfaces->iface[k + 1];
1834                                 k++;
1835                         }
1836                         interfaces->count--;
1837                         return 0;
1838                 }
1839
1840                 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
1841                         if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf))
1842                                 return hostapd_remove_bss(hapd_iface, j);
1843                 }
1844         }
1845         return -1;
1846 }
1847
1848
1849 /**
1850  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1851  * @hapd: Pointer to BSS data
1852  * @sta: Pointer to the associated STA data
1853  * @reassoc: 1 to indicate this was a re-association; 0 = first association
1854  *
1855  * This function will be called whenever a station associates with the AP. It
1856  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1857  * from drv_callbacks.c based on driver events for drivers that take care of
1858  * management frames (IEEE 802.11 authentication and association) internally.
1859  */
1860 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1861                            int reassoc)
1862 {
1863         if (hapd->tkip_countermeasures) {
1864                 hostapd_drv_sta_deauth(hapd, sta->addr,
1865                                        WLAN_REASON_MICHAEL_MIC_FAILURE);
1866                 return;
1867         }
1868
1869         hostapd_prune_associations(hapd, sta->addr);
1870
1871         /* IEEE 802.11F (IAPP) */
1872         if (hapd->conf->ieee802_11f)
1873                 iapp_new_station(hapd->iapp, sta);
1874
1875 #ifdef CONFIG_P2P
1876         if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1877                 sta->no_p2p_set = 1;
1878                 hapd->num_sta_no_p2p++;
1879                 if (hapd->num_sta_no_p2p == 1)
1880                         hostapd_p2p_non_p2p_sta_connected(hapd);
1881         }
1882 #endif /* CONFIG_P2P */
1883
1884         /* Start accounting here, if IEEE 802.1X and WPA are not used.
1885          * IEEE 802.1X/WPA code will start accounting after the station has
1886          * been authorized. */
1887         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1888                 os_get_time(&sta->connected_time);
1889                 accounting_sta_start(hapd, sta);
1890         }
1891
1892         /* Start IEEE 802.1X authentication process for new stations */
1893         ieee802_1x_new_station(hapd, sta);
1894         if (reassoc) {
1895                 if (sta->auth_alg != WLAN_AUTH_FT &&
1896                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1897                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1898         } else
1899                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1900
1901         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1902                    "for " MACSTR " (%d seconds - ap_max_inactivity)",
1903                    __func__, MAC2STR(sta->addr),
1904                    hapd->conf->ap_max_inactivity);
1905         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1906         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1907                                ap_handle_timer, hapd, sta);
1908 }
1909
1910
1911 const char * hostapd_state_text(enum hostapd_iface_state s)
1912 {
1913         switch (s) {
1914         case HAPD_IFACE_UNINITIALIZED:
1915                 return "UNINITIALIZED";
1916         case HAPD_IFACE_DISABLED:
1917                 return "DISABLED";
1918         case HAPD_IFACE_COUNTRY_UPDATE:
1919                 return "COUNTRY_UPDATE";
1920         case HAPD_IFACE_ACS:
1921                 return "ACS";
1922         case HAPD_IFACE_HT_SCAN:
1923                 return "HT_SCAN";
1924         case HAPD_IFACE_DFS:
1925                 return "DFS";
1926         case HAPD_IFACE_ENABLED:
1927                 return "ENABLED";
1928         }
1929
1930         return "UNKNOWN";
1931 }
1932
1933
1934 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
1935 {
1936         wpa_printf(MSG_INFO, "%s: interface state %s->%s",
1937                    iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
1938                    hostapd_state_text(s));
1939         iface->state = s;
1940 }