AP: Add support for Proxy ARP, DHCP snooping mechanism
[mech_eap.git] / src / ap / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2014, 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 "eap_server/tncs.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 #include "ieee802_11.h"
38 #include "bss_load.h"
39 #include "dhcp_snoop.h"
40
41
42 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
43 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
44 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
45 static int setup_interface2(struct hostapd_iface *iface);
46 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
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->conf->osen) && 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->iconf->channel = oldconf->channel;
179                 hapd->iconf->secondary_channel = oldconf->secondary_channel;
180                 hapd->iconf->ieee80211n = oldconf->ieee80211n;
181                 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
182                 hapd->iconf->ht_capab = oldconf->ht_capab;
183                 hapd->iconf->vht_capab = oldconf->vht_capab;
184                 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
185                 hapd->iconf->vht_oper_centr_freq_seg0_idx =
186                         oldconf->vht_oper_centr_freq_seg0_idx;
187                 hapd->iconf->vht_oper_centr_freq_seg1_idx =
188                         oldconf->vht_oper_centr_freq_seg1_idx;
189                 hapd->conf = newconf->bss[j];
190                 hostapd_reload_bss(hapd);
191         }
192
193         hostapd_config_free(oldconf);
194
195
196         return 0;
197 }
198
199
200 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
201                                               char *ifname)
202 {
203         int i;
204
205         for (i = 0; i < NUM_WEP_KEYS; i++) {
206                 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
207                                         0, NULL, 0, NULL, 0)) {
208                         wpa_printf(MSG_DEBUG, "Failed to clear default "
209                                    "encryption keys (ifname=%s keyidx=%d)",
210                                    ifname, i);
211                 }
212         }
213 #ifdef CONFIG_IEEE80211W
214         if (hapd->conf->ieee80211w) {
215                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
216                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
217                                                 NULL, i, 0, NULL,
218                                                 0, NULL, 0)) {
219                                 wpa_printf(MSG_DEBUG, "Failed to clear "
220                                            "default mgmt encryption keys "
221                                            "(ifname=%s keyidx=%d)", ifname, i);
222                         }
223                 }
224         }
225 #endif /* CONFIG_IEEE80211W */
226 }
227
228
229 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
230 {
231         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
232         return 0;
233 }
234
235
236 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
237 {
238         int errors = 0, idx;
239         struct hostapd_ssid *ssid = &hapd->conf->ssid;
240
241         idx = ssid->wep.idx;
242         if (ssid->wep.default_len &&
243             hostapd_drv_set_key(hapd->conf->iface,
244                                 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
245                                 1, NULL, 0, ssid->wep.key[idx],
246                                 ssid->wep.len[idx])) {
247                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
248                 errors++;
249         }
250
251         return errors;
252 }
253
254
255 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
256 {
257         if (!hapd->started) {
258                 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
259                            __func__, hapd->conf->iface);
260                 return;
261         }
262         hapd->started = 0;
263
264         wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
265         iapp_deinit(hapd->iapp);
266         hapd->iapp = NULL;
267         accounting_deinit(hapd);
268         hostapd_deinit_wpa(hapd);
269         vlan_deinit(hapd);
270         hostapd_acl_deinit(hapd);
271 #ifndef CONFIG_NO_RADIUS
272         radius_client_deinit(hapd->radius);
273         hapd->radius = NULL;
274         radius_das_deinit(hapd->radius_das);
275         hapd->radius_das = NULL;
276 #endif /* CONFIG_NO_RADIUS */
277
278         hostapd_deinit_wps(hapd);
279
280         authsrv_deinit(hapd);
281
282         if (hapd->interface_added) {
283                 hapd->interface_added = 0;
284                 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
285                         wpa_printf(MSG_WARNING,
286                                    "Failed to remove BSS interface %s",
287                                    hapd->conf->iface);
288                         hapd->interface_added = 1;
289                 } else {
290                         /*
291                          * Since this was a dynamically added interface, the
292                          * driver wrapper may have removed its internal instance
293                          * and hapd->drv_priv is not valid anymore.
294                          */
295                         hapd->drv_priv = NULL;
296                 }
297         }
298
299         os_free(hapd->probereq_cb);
300         hapd->probereq_cb = NULL;
301
302 #ifdef CONFIG_P2P
303         wpabuf_free(hapd->p2p_beacon_ie);
304         hapd->p2p_beacon_ie = NULL;
305         wpabuf_free(hapd->p2p_probe_resp_ie);
306         hapd->p2p_probe_resp_ie = NULL;
307 #endif /* CONFIG_P2P */
308
309         wpabuf_free(hapd->time_adv);
310
311 #ifdef CONFIG_INTERWORKING
312         gas_serv_deinit(hapd);
313 #endif /* CONFIG_INTERWORKING */
314
315         bss_load_update_deinit(hapd);
316         dhcp_snoop_deinit(hapd);
317
318 #ifdef CONFIG_SQLITE
319         bin_clear_free(hapd->tmp_eap_user.identity,
320                        hapd->tmp_eap_user.identity_len);
321         bin_clear_free(hapd->tmp_eap_user.password,
322                        hapd->tmp_eap_user.password_len);
323 #endif /* CONFIG_SQLITE */
324 }
325
326
327 /**
328  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
329  * @hapd: Pointer to BSS data
330  *
331  * This function is used to free all per-BSS data structures and resources.
332  * Most of the modules that are initialized in hostapd_setup_bss() are
333  * deinitialized here.
334  */
335 static void hostapd_cleanup(struct hostapd_data *hapd)
336 {
337         wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
338                    hapd->conf->iface);
339         if (hapd->iface->interfaces &&
340             hapd->iface->interfaces->ctrl_iface_deinit)
341                 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
342         hostapd_free_hapd_data(hapd);
343 }
344
345
346 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
347 {
348         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
349         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
350         iface->hw_features = NULL;
351         os_free(iface->current_rates);
352         iface->current_rates = NULL;
353         os_free(iface->basic_rates);
354         iface->basic_rates = NULL;
355         ap_list_deinit(iface);
356 }
357
358
359 /**
360  * hostapd_cleanup_iface - Complete per-interface cleanup
361  * @iface: Pointer to interface data
362  *
363  * This function is called after per-BSS data structures are deinitialized
364  * with hostapd_cleanup().
365  */
366 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
367 {
368         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
369         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
370
371         hostapd_cleanup_iface_partial(iface);
372         hostapd_config_free(iface->conf);
373         iface->conf = NULL;
374
375         os_free(iface->config_fname);
376         os_free(iface->bss);
377         wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
378         os_free(iface);
379 }
380
381
382 static void hostapd_clear_wep(struct hostapd_data *hapd)
383 {
384         if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
385                 hostapd_set_privacy(hapd, 0);
386                 hostapd_broadcast_wep_clear(hapd);
387         }
388 }
389
390
391 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
392 {
393         int i;
394
395         hostapd_broadcast_wep_set(hapd);
396
397         if (hapd->conf->ssid.wep.default_len) {
398                 hostapd_set_privacy(hapd, 1);
399                 return 0;
400         }
401
402         /*
403          * When IEEE 802.1X is not enabled, the driver may need to know how to
404          * set authentication algorithms for static WEP.
405          */
406         hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
407
408         for (i = 0; i < 4; i++) {
409                 if (hapd->conf->ssid.wep.key[i] &&
410                     hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
411                                         i == hapd->conf->ssid.wep.idx, NULL, 0,
412                                         hapd->conf->ssid.wep.key[i],
413                                         hapd->conf->ssid.wep.len[i])) {
414                         wpa_printf(MSG_WARNING, "Could not set WEP "
415                                    "encryption.");
416                         return -1;
417                 }
418                 if (hapd->conf->ssid.wep.key[i] &&
419                     i == hapd->conf->ssid.wep.idx)
420                         hostapd_set_privacy(hapd, 1);
421         }
422
423         return 0;
424 }
425
426
427 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
428 {
429         int ret = 0;
430         u8 addr[ETH_ALEN];
431
432         if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
433                 return 0;
434
435         if (!hapd->iface->driver_ap_teardown) {
436                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
437                         "Flushing old station entries");
438
439                 if (hostapd_flush(hapd)) {
440                         wpa_msg(hapd->msg_ctx, MSG_WARNING,
441                                 "Could not connect to kernel driver");
442                         ret = -1;
443                 }
444         }
445         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
446         os_memset(addr, 0xff, ETH_ALEN);
447         hostapd_drv_sta_deauth(hapd, addr, reason);
448         hostapd_free_stas(hapd);
449
450         return ret;
451 }
452
453
454 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
455 {
456         hostapd_free_stas(hapd);
457         hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
458         hostapd_clear_wep(hapd);
459 }
460
461
462 /**
463  * hostapd_validate_bssid_configuration - Validate BSSID configuration
464  * @iface: Pointer to interface data
465  * Returns: 0 on success, -1 on failure
466  *
467  * This function is used to validate that the configured BSSIDs are valid.
468  */
469 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
470 {
471         u8 mask[ETH_ALEN] = { 0 };
472         struct hostapd_data *hapd = iface->bss[0];
473         unsigned int i = iface->conf->num_bss, bits = 0, j;
474         int auto_addr = 0;
475
476         if (hostapd_drv_none(hapd))
477                 return 0;
478
479         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
480
481         /* Determine the bits necessary to cover the number of BSSIDs. */
482         for (i--; i; i >>= 1)
483                 bits++;
484
485         /* Determine the bits necessary to any configured BSSIDs,
486            if they are higher than the number of BSSIDs. */
487         for (j = 0; j < iface->conf->num_bss; j++) {
488                 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
489                         if (j)
490                                 auto_addr++;
491                         continue;
492                 }
493
494                 for (i = 0; i < ETH_ALEN; i++) {
495                         mask[i] |=
496                                 iface->conf->bss[j]->bssid[i] ^
497                                 hapd->own_addr[i];
498                 }
499         }
500
501         if (!auto_addr)
502                 goto skip_mask_ext;
503
504         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
505                 ;
506         j = 0;
507         if (i < ETH_ALEN) {
508                 j = (5 - i) * 8;
509
510                 while (mask[i] != 0) {
511                         mask[i] >>= 1;
512                         j++;
513                 }
514         }
515
516         if (bits < j)
517                 bits = j;
518
519         if (bits > 40) {
520                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
521                            bits);
522                 return -1;
523         }
524
525         os_memset(mask, 0xff, ETH_ALEN);
526         j = bits / 8;
527         for (i = 5; i > 5 - j; i--)
528                 mask[i] = 0;
529         j = bits % 8;
530         while (j--)
531                 mask[i] <<= 1;
532
533 skip_mask_ext:
534         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
535                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
536
537         if (!auto_addr)
538                 return 0;
539
540         for (i = 0; i < ETH_ALEN; i++) {
541                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
542                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
543                                    " for start address " MACSTR ".",
544                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
545                         wpa_printf(MSG_ERROR, "Start address must be the "
546                                    "first address in the block (i.e., addr "
547                                    "AND mask == addr).");
548                         return -1;
549                 }
550         }
551
552         return 0;
553 }
554
555
556 static int mac_in_conf(struct hostapd_config *conf, const void *a)
557 {
558         size_t i;
559
560         for (i = 0; i < conf->num_bss; i++) {
561                 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
562                         return 1;
563                 }
564         }
565
566         return 0;
567 }
568
569
570 #ifndef CONFIG_NO_RADIUS
571
572 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
573                                     struct radius_das_attrs *attr)
574 {
575         if (attr->nas_identifier &&
576             (!hapd->conf->nas_identifier ||
577              os_strlen(hapd->conf->nas_identifier) !=
578              attr->nas_identifier_len ||
579              os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
580                        attr->nas_identifier_len) != 0)) {
581                 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
582                 return 1;
583         }
584
585         if (attr->nas_ip_addr &&
586             (hapd->conf->own_ip_addr.af != AF_INET ||
587              os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
588              0)) {
589                 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
590                 return 1;
591         }
592
593 #ifdef CONFIG_IPV6
594         if (attr->nas_ipv6_addr &&
595             (hapd->conf->own_ip_addr.af != AF_INET6 ||
596              os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
597              != 0)) {
598                 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
599                 return 1;
600         }
601 #endif /* CONFIG_IPV6 */
602
603         return 0;
604 }
605
606
607 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
608                                               struct radius_das_attrs *attr)
609 {
610         struct sta_info *sta = NULL;
611         char buf[128];
612
613         if (attr->sta_addr)
614                 sta = ap_get_sta(hapd, attr->sta_addr);
615
616         if (sta == NULL && attr->acct_session_id &&
617             attr->acct_session_id_len == 17) {
618                 for (sta = hapd->sta_list; sta; sta = sta->next) {
619                         os_snprintf(buf, sizeof(buf), "%08X-%08X",
620                                     sta->acct_session_id_hi,
621                                     sta->acct_session_id_lo);
622                         if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
623                                 break;
624                 }
625         }
626
627         if (sta == NULL && attr->cui) {
628                 for (sta = hapd->sta_list; sta; sta = sta->next) {
629                         struct wpabuf *cui;
630                         cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
631                         if (cui && wpabuf_len(cui) == attr->cui_len &&
632                             os_memcmp(wpabuf_head(cui), attr->cui,
633                                       attr->cui_len) == 0)
634                                 break;
635                 }
636         }
637
638         if (sta == NULL && attr->user_name) {
639                 for (sta = hapd->sta_list; sta; sta = sta->next) {
640                         u8 *identity;
641                         size_t identity_len;
642                         identity = ieee802_1x_get_identity(sta->eapol_sm,
643                                                            &identity_len);
644                         if (identity &&
645                             identity_len == attr->user_name_len &&
646                             os_memcmp(identity, attr->user_name, identity_len)
647                             == 0)
648                                 break;
649                 }
650         }
651
652         return sta;
653 }
654
655
656 static enum radius_das_res
657 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
658 {
659         struct hostapd_data *hapd = ctx;
660         struct sta_info *sta;
661
662         if (hostapd_das_nas_mismatch(hapd, attr))
663                 return RADIUS_DAS_NAS_MISMATCH;
664
665         sta = hostapd_das_find_sta(hapd, attr);
666         if (sta == NULL)
667                 return RADIUS_DAS_SESSION_NOT_FOUND;
668
669         wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
670
671         hostapd_drv_sta_deauth(hapd, sta->addr,
672                                WLAN_REASON_PREV_AUTH_NOT_VALID);
673         ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
674
675         return RADIUS_DAS_SUCCESS;
676 }
677
678 #endif /* CONFIG_NO_RADIUS */
679
680
681 /**
682  * hostapd_setup_bss - Per-BSS setup (initialization)
683  * @hapd: Pointer to BSS data
684  * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
685  *      but interface may exist
686  *
687  * This function is used to initialize all per-BSS data structures and
688  * resources. This gets called in a loop for each BSS when an interface is
689  * initialized. Most of the modules that are initialized here will be
690  * deinitialized in hostapd_cleanup().
691  */
692 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
693 {
694         struct hostapd_bss_config *conf = hapd->conf;
695         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
696         int ssid_len, set_ssid;
697         char force_ifname[IFNAMSIZ];
698         u8 if_addr[ETH_ALEN];
699         int flush_old_stations = 1;
700
701         wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
702                    __func__, hapd, conf->iface, first);
703
704 #ifdef EAP_SERVER_TNC
705         if (conf->tnc && tncs_global_init() < 0) {
706                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
707                 return -1;
708         }
709 #endif /* EAP_SERVER_TNC */
710
711         if (hapd->started) {
712                 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
713                            __func__, conf->iface);
714                 return -1;
715         }
716         hapd->started = 1;
717
718         if (!first || first == -1) {
719                 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
720                         /* Allocate the next available BSSID. */
721                         do {
722                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
723                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
724                 } else {
725                         /* Allocate the configured BSSID. */
726                         os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
727
728                         if (hostapd_mac_comp(hapd->own_addr,
729                                              hapd->iface->bss[0]->own_addr) ==
730                             0) {
731                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
732                                            "BSSID set to the MAC address of "
733                                            "the radio", conf->iface);
734                                 return -1;
735                         }
736                 }
737
738                 hapd->interface_added = 1;
739                 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
740                                    conf->iface, hapd->own_addr, hapd,
741                                    &hapd->drv_priv, force_ifname, if_addr,
742                                    conf->bridge[0] ? conf->bridge : NULL,
743                                    first == -1)) {
744                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
745                                    MACSTR ")", MAC2STR(hapd->own_addr));
746                         hapd->interface_added = 0;
747                         return -1;
748                 }
749         }
750
751         if (conf->wmm_enabled < 0)
752                 conf->wmm_enabled = hapd->iconf->ieee80211n;
753
754 #ifdef CONFIG_MESH
755         if (hapd->iface->mconf == NULL)
756                 flush_old_stations = 0;
757 #endif /* CONFIG_MESH */
758
759         if (flush_old_stations)
760                 hostapd_flush_old_stations(hapd,
761                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
762         hostapd_set_privacy(hapd, 0);
763
764         hostapd_broadcast_wep_clear(hapd);
765         if (hostapd_setup_encryption(conf->iface, hapd))
766                 return -1;
767
768         /*
769          * Fetch the SSID from the system and use it or,
770          * if one was specified in the config file, verify they
771          * match.
772          */
773         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
774         if (ssid_len < 0) {
775                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
776                 return -1;
777         }
778         if (conf->ssid.ssid_set) {
779                 /*
780                  * If SSID is specified in the config file and it differs
781                  * from what is being used then force installation of the
782                  * new SSID.
783                  */
784                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
785                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
786         } else {
787                 /*
788                  * No SSID in the config file; just use the one we got
789                  * from the system.
790                  */
791                 set_ssid = 0;
792                 conf->ssid.ssid_len = ssid_len;
793                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
794         }
795
796         if (!hostapd_drv_none(hapd)) {
797                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
798                            " and ssid \"%s\"",
799                            conf->iface, MAC2STR(hapd->own_addr),
800                            wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
801         }
802
803         if (hostapd_setup_wpa_psk(conf)) {
804                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
805                 return -1;
806         }
807
808         /* Set SSID for the kernel driver (to be used in beacon and probe
809          * response frames) */
810         if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
811                                          conf->ssid.ssid_len)) {
812                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
813                 return -1;
814         }
815
816         if (wpa_debug_level <= MSG_MSGDUMP)
817                 conf->radius->msg_dumps = 1;
818 #ifndef CONFIG_NO_RADIUS
819         hapd->radius = radius_client_init(hapd, conf->radius);
820         if (hapd->radius == NULL) {
821                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
822                 return -1;
823         }
824
825         if (conf->radius_das_port) {
826                 struct radius_das_conf das_conf;
827                 os_memset(&das_conf, 0, sizeof(das_conf));
828                 das_conf.port = conf->radius_das_port;
829                 das_conf.shared_secret = conf->radius_das_shared_secret;
830                 das_conf.shared_secret_len =
831                         conf->radius_das_shared_secret_len;
832                 das_conf.client_addr = &conf->radius_das_client_addr;
833                 das_conf.time_window = conf->radius_das_time_window;
834                 das_conf.require_event_timestamp =
835                         conf->radius_das_require_event_timestamp;
836                 das_conf.ctx = hapd;
837                 das_conf.disconnect = hostapd_das_disconnect;
838                 hapd->radius_das = radius_das_init(&das_conf);
839                 if (hapd->radius_das == NULL) {
840                         wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
841                                    "failed.");
842                         return -1;
843                 }
844         }
845 #endif /* CONFIG_NO_RADIUS */
846
847         if (hostapd_acl_init(hapd)) {
848                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
849                 return -1;
850         }
851         if (hostapd_init_wps(hapd, conf))
852                 return -1;
853
854         if (authsrv_init(hapd) < 0)
855                 return -1;
856
857         if (ieee802_1x_init(hapd)) {
858                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
859                 return -1;
860         }
861
862         if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
863                 return -1;
864
865         if (accounting_init(hapd)) {
866                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
867                 return -1;
868         }
869
870         if (conf->ieee802_11f &&
871             (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
872                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
873                            "failed.");
874                 return -1;
875         }
876
877 #ifdef CONFIG_INTERWORKING
878         if (gas_serv_init(hapd)) {
879                 wpa_printf(MSG_ERROR, "GAS server initialization failed");
880                 return -1;
881         }
882
883         if (conf->qos_map_set_len &&
884             hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
885                                     conf->qos_map_set_len)) {
886                 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
887                 return -1;
888         }
889 #endif /* CONFIG_INTERWORKING */
890
891         if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
892                 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
893                 return -1;
894         }
895
896         if (conf->proxy_arp && dhcp_snoop_init(hapd)) {
897                 wpa_printf(MSG_ERROR, "DHCP snooping initialization failed");
898                 return -1;
899         }
900
901         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
902                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
903                 return -1;
904         }
905
906         if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
907                 return -1;
908
909         if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
910                 return -1;
911
912         if (hapd->driver && hapd->driver->set_operstate)
913                 hapd->driver->set_operstate(hapd->drv_priv, 1);
914
915         return 0;
916 }
917
918
919 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
920 {
921         struct hostapd_data *hapd = iface->bss[0];
922         int i;
923         struct hostapd_tx_queue_params *p;
924
925 #ifdef CONFIG_MESH
926         if (iface->mconf == NULL)
927                 return;
928 #endif /* CONFIG_MESH */
929
930         for (i = 0; i < NUM_TX_QUEUES; i++) {
931                 p = &iface->conf->tx_queue[i];
932
933                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
934                                                 p->cwmax, p->burst)) {
935                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
936                                    "parameters for queue %d.", i);
937                         /* Continue anyway */
938                 }
939         }
940 }
941
942
943 static int hostapd_set_acl_list(struct hostapd_data *hapd,
944                                 struct mac_acl_entry *mac_acl,
945                                 int n_entries, u8 accept_acl)
946 {
947         struct hostapd_acl_params *acl_params;
948         int i, err;
949
950         acl_params = os_zalloc(sizeof(*acl_params) +
951                                (n_entries * sizeof(acl_params->mac_acl[0])));
952         if (!acl_params)
953                 return -ENOMEM;
954
955         for (i = 0; i < n_entries; i++)
956                 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
957                           ETH_ALEN);
958
959         acl_params->acl_policy = accept_acl;
960         acl_params->num_mac_acl = n_entries;
961
962         err = hostapd_drv_set_acl(hapd, acl_params);
963
964         os_free(acl_params);
965
966         return err;
967 }
968
969
970 static void hostapd_set_acl(struct hostapd_data *hapd)
971 {
972         struct hostapd_config *conf = hapd->iconf;
973         int err;
974         u8 accept_acl;
975
976         if (hapd->iface->drv_max_acl_mac_addrs == 0)
977                 return;
978
979         if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
980                 accept_acl = 1;
981                 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
982                                            conf->bss[0]->num_accept_mac,
983                                            accept_acl);
984                 if (err) {
985                         wpa_printf(MSG_DEBUG, "Failed to set accept acl");
986                         return;
987                 }
988         } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
989                 accept_acl = 0;
990                 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
991                                            conf->bss[0]->num_deny_mac,
992                                            accept_acl);
993                 if (err) {
994                         wpa_printf(MSG_DEBUG, "Failed to set deny acl");
995                         return;
996                 }
997         }
998 }
999
1000
1001 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
1002 {
1003         if (!hapd->iface->interfaces ||
1004             !hapd->iface->interfaces->ctrl_iface_init)
1005                 return 0;
1006
1007         if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1008                 wpa_printf(MSG_ERROR,
1009                            "Failed to setup control interface for %s",
1010                            hapd->conf->iface);
1011                 return -1;
1012         }
1013
1014         return 0;
1015 }
1016
1017
1018 static int start_ctrl_iface(struct hostapd_iface *iface)
1019 {
1020         size_t i;
1021
1022         if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1023                 return 0;
1024
1025         for (i = 0; i < iface->num_bss; i++) {
1026                 struct hostapd_data *hapd = iface->bss[i];
1027                 if (iface->interfaces->ctrl_iface_init(hapd)) {
1028                         wpa_printf(MSG_ERROR,
1029                                    "Failed to setup control interface for %s",
1030                                    hapd->conf->iface);
1031                         return -1;
1032                 }
1033         }
1034
1035         return 0;
1036 }
1037
1038
1039 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1040 {
1041         struct hostapd_iface *iface = eloop_ctx;
1042
1043         if (!iface->wait_channel_update) {
1044                 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1045                 return;
1046         }
1047
1048         /*
1049          * It is possible that the existing channel list is acceptable, so try
1050          * to proceed.
1051          */
1052         wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1053         setup_interface2(iface);
1054 }
1055
1056
1057 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1058 {
1059         if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1060                 return;
1061
1062         wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1063         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1064         setup_interface2(iface);
1065 }
1066
1067
1068 static int setup_interface(struct hostapd_iface *iface)
1069 {
1070         struct hostapd_data *hapd = iface->bss[0];
1071         size_t i;
1072
1073         /*
1074          * It is possible that setup_interface() is called after the interface
1075          * was disabled etc., in which case driver_ap_teardown is possibly set
1076          * to 1. Clear it here so any other key/station deletion, which is not
1077          * part of a teardown flow, would also call the relevant driver
1078          * callbacks.
1079          */
1080         iface->driver_ap_teardown = 0;
1081
1082         if (!iface->phy[0]) {
1083                 const char *phy = hostapd_drv_get_radio_name(hapd);
1084                 if (phy) {
1085                         wpa_printf(MSG_DEBUG, "phy: %s", phy);
1086                         os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1087                 }
1088         }
1089
1090         /*
1091          * Make sure that all BSSes get configured with a pointer to the same
1092          * driver interface.
1093          */
1094         for (i = 1; i < iface->num_bss; i++) {
1095                 iface->bss[i]->driver = hapd->driver;
1096                 iface->bss[i]->drv_priv = hapd->drv_priv;
1097         }
1098
1099         if (hostapd_validate_bssid_configuration(iface))
1100                 return -1;
1101
1102         /*
1103          * Initialize control interfaces early to allow external monitoring of
1104          * channel setup operations that may take considerable amount of time
1105          * especially for DFS cases.
1106          */
1107         if (start_ctrl_iface(iface))
1108                 return -1;
1109
1110         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1111                 char country[4], previous_country[4];
1112
1113                 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1114                 if (hostapd_get_country(hapd, previous_country) < 0)
1115                         previous_country[0] = '\0';
1116
1117                 os_memcpy(country, hapd->iconf->country, 3);
1118                 country[3] = '\0';
1119                 if (hostapd_set_country(hapd, country) < 0) {
1120                         wpa_printf(MSG_ERROR, "Failed to set country code");
1121                         return -1;
1122                 }
1123
1124                 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1125                            previous_country, country);
1126
1127                 if (os_strncmp(previous_country, country, 2) != 0) {
1128                         wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1129                         iface->wait_channel_update = 1;
1130                         eloop_register_timeout(5, 0,
1131                                                channel_list_update_timeout,
1132                                                iface, NULL);
1133                         return 0;
1134                 }
1135         }
1136
1137         return setup_interface2(iface);
1138 }
1139
1140
1141 static int setup_interface2(struct hostapd_iface *iface)
1142 {
1143         iface->wait_channel_update = 0;
1144
1145         if (hostapd_get_hw_features(iface)) {
1146                 /* Not all drivers support this yet, so continue without hw
1147                  * feature data. */
1148         } else {
1149                 int ret = hostapd_select_hw_mode(iface);
1150                 if (ret < 0) {
1151                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1152                                    "channel. (%d)", ret);
1153                         goto fail;
1154                 }
1155                 if (ret == 1) {
1156                         wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1157                         return 0;
1158                 }
1159                 ret = hostapd_check_ht_capab(iface);
1160                 if (ret < 0)
1161                         goto fail;
1162                 if (ret == 1) {
1163                         wpa_printf(MSG_DEBUG, "Interface initialization will "
1164                                    "be completed in a callback");
1165                         return 0;
1166                 }
1167
1168                 if (iface->conf->ieee80211h)
1169                         wpa_printf(MSG_DEBUG, "DFS support is enabled");
1170         }
1171         return hostapd_setup_interface_complete(iface, 0);
1172
1173 fail:
1174         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1175         wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1176         if (iface->interfaces && iface->interfaces->terminate_on_error)
1177                 eloop_terminate();
1178         return -1;
1179 }
1180
1181
1182 /**
1183  * hostapd_setup_interface_complete - Complete interface setup
1184  *
1185  * This function is called when previous steps in the interface setup has been
1186  * completed. This can also start operations, e.g., DFS, that will require
1187  * additional processing before interface is ready to be enabled. Such
1188  * operations will call this function from eloop callbacks when finished.
1189  */
1190 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1191 {
1192         struct hostapd_data *hapd = iface->bss[0];
1193         size_t j;
1194         u8 *prev_addr;
1195         int delay_apply_cfg = 0;
1196
1197         if (err)
1198                 goto fail;
1199
1200         wpa_printf(MSG_DEBUG, "Completing interface initialization");
1201         if (iface->conf->channel) {
1202 #ifdef NEED_AP_MLME
1203                 int res;
1204 #endif /* NEED_AP_MLME */
1205
1206                 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1207                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1208                            "Frequency: %d MHz",
1209                            hostapd_hw_mode_txt(iface->conf->hw_mode),
1210                            iface->conf->channel, iface->freq);
1211
1212 #ifdef NEED_AP_MLME
1213                 /* Handle DFS only if it is not offloaded to the driver */
1214                 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1215                         /* Check DFS */
1216                         res = hostapd_handle_dfs(iface);
1217                         if (res <= 0) {
1218                                 if (res < 0)
1219                                         goto fail;
1220                                 return res;
1221                         }
1222                 }
1223 #endif /* NEED_AP_MLME */
1224
1225 #ifdef CONFIG_MESH
1226                 if (iface->mconf != NULL) {
1227                         wpa_printf(MSG_DEBUG,
1228                                    "%s: Mesh configuration will be applied while joining the mesh network",
1229                                    iface->bss[0]->conf->iface);
1230                         delay_apply_cfg = 1;
1231                 }
1232 #endif /* CONFIG_MESH */
1233
1234                 if (!delay_apply_cfg &&
1235                     hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1236                                      hapd->iconf->channel,
1237                                      hapd->iconf->ieee80211n,
1238                                      hapd->iconf->ieee80211ac,
1239                                      hapd->iconf->secondary_channel,
1240                                      hapd->iconf->vht_oper_chwidth,
1241                                      hapd->iconf->vht_oper_centr_freq_seg0_idx,
1242                                      hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1243                         wpa_printf(MSG_ERROR, "Could not set channel for "
1244                                    "kernel driver");
1245                         goto fail;
1246                 }
1247         }
1248
1249         if (iface->current_mode) {
1250                 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1251                         wpa_printf(MSG_ERROR, "Failed to prepare rates "
1252                                    "table.");
1253                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1254                                        HOSTAPD_LEVEL_WARNING,
1255                                        "Failed to prepare rates table.");
1256                         goto fail;
1257                 }
1258         }
1259
1260         if (hapd->iconf->rts_threshold > -1 &&
1261             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1262                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1263                            "kernel driver");
1264                 goto fail;
1265         }
1266
1267         if (hapd->iconf->fragm_threshold > -1 &&
1268             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1269                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1270                            "for kernel driver");
1271                 goto fail;
1272         }
1273
1274         prev_addr = hapd->own_addr;
1275
1276         for (j = 0; j < iface->num_bss; j++) {
1277                 hapd = iface->bss[j];
1278                 if (j)
1279                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1280                 if (hostapd_setup_bss(hapd, j == 0)) {
1281                         do {
1282                                 hapd = iface->bss[j];
1283                                 hostapd_bss_deinit_no_free(hapd);
1284                                 hostapd_free_hapd_data(hapd);
1285                         } while (j-- > 0);
1286                         goto fail;
1287                 }
1288                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1289                         prev_addr = hapd->own_addr;
1290         }
1291         hapd = iface->bss[0];
1292
1293         hostapd_tx_queue_params(iface);
1294
1295         ap_list_init(iface);
1296
1297         hostapd_set_acl(hapd);
1298
1299         if (hostapd_driver_commit(hapd) < 0) {
1300                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1301                            "configuration", __func__);
1302                 goto fail;
1303         }
1304
1305         /*
1306          * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1307          * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1308          * mode), the interface is up only after driver_commit, so initialize
1309          * WPS after driver_commit.
1310          */
1311         for (j = 0; j < iface->num_bss; j++) {
1312                 if (hostapd_init_wps_complete(iface->bss[j]))
1313                         goto fail;
1314         }
1315
1316         hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1317         wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1318         if (hapd->setup_complete_cb)
1319                 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1320
1321         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1322                    iface->bss[0]->conf->iface);
1323         if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1324                 iface->interfaces->terminate_on_error--;
1325
1326         return 0;
1327
1328 fail:
1329         wpa_printf(MSG_ERROR, "Interface initialization failed");
1330         hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1331         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1332         if (iface->interfaces && iface->interfaces->terminate_on_error)
1333                 eloop_terminate();
1334         return -1;
1335 }
1336
1337
1338 /**
1339  * hostapd_setup_interface - Setup of an interface
1340  * @iface: Pointer to interface data.
1341  * Returns: 0 on success, -1 on failure
1342  *
1343  * Initializes the driver interface, validates the configuration,
1344  * and sets driver parameters based on the configuration.
1345  * Flushes old stations, sets the channel, encryption,
1346  * beacons, and WDS links based on the configuration.
1347  *
1348  * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1349  * or DFS operations, this function returns 0 before such operations have been
1350  * completed. The pending operations are registered into eloop and will be
1351  * completed from eloop callbacks. Those callbacks end up calling
1352  * hostapd_setup_interface_complete() once setup has been completed.
1353  */
1354 int hostapd_setup_interface(struct hostapd_iface *iface)
1355 {
1356         int ret;
1357
1358         ret = setup_interface(iface);
1359         if (ret) {
1360                 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1361                            iface->bss[0]->conf->iface);
1362                 return -1;
1363         }
1364
1365         return 0;
1366 }
1367
1368
1369 /**
1370  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1371  * @hapd_iface: Pointer to interface data
1372  * @conf: Pointer to per-interface configuration
1373  * @bss: Pointer to per-BSS configuration for this BSS
1374  * Returns: Pointer to allocated BSS data
1375  *
1376  * This function is used to allocate per-BSS data structure. This data will be
1377  * freed after hostapd_cleanup() is called for it during interface
1378  * deinitialization.
1379  */
1380 struct hostapd_data *
1381 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1382                        struct hostapd_config *conf,
1383                        struct hostapd_bss_config *bss)
1384 {
1385         struct hostapd_data *hapd;
1386
1387         hapd = os_zalloc(sizeof(*hapd));
1388         if (hapd == NULL)
1389                 return NULL;
1390
1391         hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1392         hapd->iconf = conf;
1393         hapd->conf = bss;
1394         hapd->iface = hapd_iface;
1395         hapd->driver = hapd->iconf->driver;
1396         hapd->ctrl_sock = -1;
1397
1398         return hapd;
1399 }
1400
1401
1402 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1403 {
1404         wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1405                    hapd->conf->iface);
1406         hostapd_bss_deinit_no_free(hapd);
1407         hostapd_cleanup(hapd);
1408 }
1409
1410
1411 void hostapd_interface_deinit(struct hostapd_iface *iface)
1412 {
1413         int j;
1414
1415         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1416         if (iface == NULL)
1417                 return;
1418
1419 #ifdef CONFIG_IEEE80211N
1420 #ifdef NEED_AP_MLME
1421         hostapd_stop_setup_timers(iface);
1422         eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1423 #endif /* NEED_AP_MLME */
1424 #endif /* CONFIG_IEEE80211N */
1425         eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1426         iface->wait_channel_update = 0;
1427
1428         for (j = iface->num_bss - 1; j >= 0; j--)
1429                 hostapd_bss_deinit(iface->bss[j]);
1430 }
1431
1432
1433 void hostapd_interface_free(struct hostapd_iface *iface)
1434 {
1435         size_t j;
1436         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1437         for (j = 0; j < iface->num_bss; j++) {
1438                 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1439                            __func__, iface->bss[j]);
1440                 os_free(iface->bss[j]);
1441         }
1442         hostapd_cleanup_iface(iface);
1443 }
1444
1445
1446 /**
1447  * hostapd_init - Allocate and initialize per-interface data
1448  * @config_file: Path to the configuration file
1449  * Returns: Pointer to the allocated interface data or %NULL on failure
1450  *
1451  * This function is used to allocate main data structures for per-interface
1452  * data. The allocated data buffer will be freed by calling
1453  * hostapd_cleanup_iface().
1454  */
1455 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1456                                     const char *config_file)
1457 {
1458         struct hostapd_iface *hapd_iface = NULL;
1459         struct hostapd_config *conf = NULL;
1460         struct hostapd_data *hapd;
1461         size_t i;
1462
1463         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1464         if (hapd_iface == NULL)
1465                 goto fail;
1466
1467         hapd_iface->config_fname = os_strdup(config_file);
1468         if (hapd_iface->config_fname == NULL)
1469                 goto fail;
1470
1471         conf = interfaces->config_read_cb(hapd_iface->config_fname);
1472         if (conf == NULL)
1473                 goto fail;
1474         hapd_iface->conf = conf;
1475
1476         hapd_iface->num_bss = conf->num_bss;
1477         hapd_iface->bss = os_calloc(conf->num_bss,
1478                                     sizeof(struct hostapd_data *));
1479         if (hapd_iface->bss == NULL)
1480                 goto fail;
1481
1482         for (i = 0; i < conf->num_bss; i++) {
1483                 hapd = hapd_iface->bss[i] =
1484                         hostapd_alloc_bss_data(hapd_iface, conf,
1485                                                conf->bss[i]);
1486                 if (hapd == NULL)
1487                         goto fail;
1488                 hapd->msg_ctx = hapd;
1489         }
1490
1491         return hapd_iface;
1492
1493 fail:
1494         wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1495                    config_file);
1496         if (conf)
1497                 hostapd_config_free(conf);
1498         if (hapd_iface) {
1499                 os_free(hapd_iface->config_fname);
1500                 os_free(hapd_iface->bss);
1501                 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1502                            __func__, hapd_iface);
1503                 os_free(hapd_iface);
1504         }
1505         return NULL;
1506 }
1507
1508
1509 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1510 {
1511         size_t i, j;
1512
1513         for (i = 0; i < interfaces->count; i++) {
1514                 struct hostapd_iface *iface = interfaces->iface[i];
1515                 for (j = 0; j < iface->num_bss; j++) {
1516                         struct hostapd_data *hapd = iface->bss[j];
1517                         if (os_strcmp(ifname, hapd->conf->iface) == 0)
1518                                 return 1;
1519                 }
1520         }
1521
1522         return 0;
1523 }
1524
1525
1526 /**
1527  * hostapd_interface_init_bss - Read configuration file and init BSS data
1528  *
1529  * This function is used to parse configuration file for a BSS. This BSS is
1530  * added to an existing interface sharing the same radio (if any) or a new
1531  * interface is created if this is the first interface on a radio. This
1532  * allocate memory for the BSS. No actual driver operations are started.
1533  *
1534  * This is similar to hostapd_interface_init(), but for a case where the
1535  * configuration is used to add a single BSS instead of all BSSes for a radio.
1536  */
1537 struct hostapd_iface *
1538 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1539                            const char *config_fname, int debug)
1540 {
1541         struct hostapd_iface *new_iface = NULL, *iface = NULL;
1542         struct hostapd_data *hapd;
1543         int k;
1544         size_t i, bss_idx;
1545
1546         if (!phy || !*phy)
1547                 return NULL;
1548
1549         for (i = 0; i < interfaces->count; i++) {
1550                 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1551                         iface = interfaces->iface[i];
1552                         break;
1553                 }
1554         }
1555
1556         wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1557                    config_fname, phy, iface ? "" : " --> new PHY");
1558         if (iface) {
1559                 struct hostapd_config *conf;
1560                 struct hostapd_bss_config **tmp_conf;
1561                 struct hostapd_data **tmp_bss;
1562                 struct hostapd_bss_config *bss;
1563                 const char *ifname;
1564
1565                 /* Add new BSS to existing iface */
1566                 conf = interfaces->config_read_cb(config_fname);
1567                 if (conf == NULL)
1568                         return NULL;
1569                 if (conf->num_bss > 1) {
1570                         wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1571                         hostapd_config_free(conf);
1572                         return NULL;
1573                 }
1574
1575                 ifname = conf->bss[0]->iface;
1576                 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1577                         wpa_printf(MSG_ERROR,
1578                                    "Interface name %s already in use", ifname);
1579                         hostapd_config_free(conf);
1580                         return NULL;
1581                 }
1582
1583                 tmp_conf = os_realloc_array(
1584                         iface->conf->bss, iface->conf->num_bss + 1,
1585                         sizeof(struct hostapd_bss_config *));
1586                 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1587                                            sizeof(struct hostapd_data *));
1588                 if (tmp_bss)
1589                         iface->bss = tmp_bss;
1590                 if (tmp_conf) {
1591                         iface->conf->bss = tmp_conf;
1592                         iface->conf->last_bss = tmp_conf[0];
1593                 }
1594                 if (tmp_bss == NULL || tmp_conf == NULL) {
1595                         hostapd_config_free(conf);
1596                         return NULL;
1597                 }
1598                 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1599                 iface->conf->num_bss++;
1600
1601                 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1602                 if (hapd == NULL) {
1603                         iface->conf->num_bss--;
1604                         hostapd_config_free(conf);
1605                         return NULL;
1606                 }
1607                 iface->conf->last_bss = bss;
1608                 iface->bss[iface->num_bss] = hapd;
1609                 hapd->msg_ctx = hapd;
1610
1611                 bss_idx = iface->num_bss++;
1612                 conf->num_bss--;
1613                 conf->bss[0] = NULL;
1614                 hostapd_config_free(conf);
1615         } else {
1616                 /* Add a new iface with the first BSS */
1617                 new_iface = iface = hostapd_init(interfaces, config_fname);
1618                 if (!iface)
1619                         return NULL;
1620                 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1621                 iface->interfaces = interfaces;
1622                 bss_idx = 0;
1623         }
1624
1625         for (k = 0; k < debug; k++) {
1626                 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1627                         iface->bss[bss_idx]->conf->logger_stdout_level--;
1628         }
1629
1630         if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1631             !hostapd_drv_none(iface->bss[bss_idx])) {
1632                 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1633                            config_fname);
1634                 if (new_iface)
1635                         hostapd_interface_deinit_free(new_iface);
1636                 return NULL;
1637         }
1638
1639         return iface;
1640 }
1641
1642
1643 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1644 {
1645         const struct wpa_driver_ops *driver;
1646         void *drv_priv;
1647
1648         wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1649         if (iface == NULL)
1650                 return;
1651         wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1652                    __func__, (unsigned int) iface->num_bss,
1653                    (unsigned int) iface->conf->num_bss);
1654         driver = iface->bss[0]->driver;
1655         drv_priv = iface->bss[0]->drv_priv;
1656         hostapd_interface_deinit(iface);
1657         wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1658                    __func__, driver, drv_priv);
1659         if (driver && driver->hapd_deinit && drv_priv) {
1660                 driver->hapd_deinit(drv_priv);
1661                 iface->bss[0]->drv_priv = NULL;
1662         }
1663         hostapd_interface_free(iface);
1664 }
1665
1666
1667 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1668                                   void *drv_priv,
1669                                   struct hostapd_iface *hapd_iface)
1670 {
1671         size_t j;
1672
1673         wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1674                    __func__, driver, drv_priv);
1675         if (driver && driver->hapd_deinit && drv_priv) {
1676                 driver->hapd_deinit(drv_priv);
1677                 for (j = 0; j < hapd_iface->num_bss; j++) {
1678                         wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1679                                    __func__, (int) j,
1680                                    hapd_iface->bss[j]->drv_priv);
1681                         if (hapd_iface->bss[j]->drv_priv == drv_priv)
1682                                 hapd_iface->bss[j]->drv_priv = NULL;
1683                 }
1684         }
1685 }
1686
1687
1688 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1689 {
1690         size_t j;
1691
1692         if (hapd_iface->bss[0]->drv_priv != NULL) {
1693                 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1694                            hapd_iface->conf->bss[0]->iface);
1695                 return -1;
1696         }
1697
1698         wpa_printf(MSG_DEBUG, "Enable interface %s",
1699                    hapd_iface->conf->bss[0]->iface);
1700
1701         for (j = 0; j < hapd_iface->num_bss; j++)
1702                 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1703         if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1704                 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1705                 return -1;
1706         }
1707
1708         if (hapd_iface->interfaces == NULL ||
1709             hapd_iface->interfaces->driver_init == NULL ||
1710             hapd_iface->interfaces->driver_init(hapd_iface))
1711                 return -1;
1712
1713         if (hostapd_setup_interface(hapd_iface)) {
1714                 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1715                                       hapd_iface->bss[0]->drv_priv,
1716                                       hapd_iface);
1717                 return -1;
1718         }
1719
1720         return 0;
1721 }
1722
1723
1724 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1725 {
1726         size_t j;
1727
1728         wpa_printf(MSG_DEBUG, "Reload interface %s",
1729                    hapd_iface->conf->bss[0]->iface);
1730         for (j = 0; j < hapd_iface->num_bss; j++)
1731                 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1732         if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1733                 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1734                 return -1;
1735         }
1736         hostapd_clear_old(hapd_iface);
1737         for (j = 0; j < hapd_iface->num_bss; j++)
1738                 hostapd_reload_bss(hapd_iface->bss[j]);
1739
1740         return 0;
1741 }
1742
1743
1744 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1745 {
1746         size_t j;
1747         const struct wpa_driver_ops *driver;
1748         void *drv_priv;
1749
1750         if (hapd_iface == NULL)
1751                 return -1;
1752
1753         if (hapd_iface->bss[0]->drv_priv == NULL) {
1754                 wpa_printf(MSG_INFO, "Interface %s already disabled",
1755                            hapd_iface->conf->bss[0]->iface);
1756                 return -1;
1757         }
1758
1759         wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1760         driver = hapd_iface->bss[0]->driver;
1761         drv_priv = hapd_iface->bss[0]->drv_priv;
1762
1763         hapd_iface->driver_ap_teardown =
1764                 !!(hapd_iface->drv_flags &
1765                    WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1766
1767         /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1768         for (j = 0; j < hapd_iface->num_bss; j++) {
1769                 struct hostapd_data *hapd = hapd_iface->bss[j];
1770                 hostapd_bss_deinit_no_free(hapd);
1771                 hostapd_free_hapd_data(hapd);
1772         }
1773
1774         hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1775
1776         /* From hostapd_cleanup_iface: These were initialized in
1777          * hostapd_setup_interface and hostapd_setup_interface_complete
1778          */
1779         hostapd_cleanup_iface_partial(hapd_iface);
1780
1781         wpa_printf(MSG_DEBUG, "Interface %s disabled",
1782                    hapd_iface->bss[0]->conf->iface);
1783         hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1784         return 0;
1785 }
1786
1787
1788 static struct hostapd_iface *
1789 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1790 {
1791         struct hostapd_iface **iface, *hapd_iface;
1792
1793         iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1794                                  sizeof(struct hostapd_iface *));
1795         if (iface == NULL)
1796                 return NULL;
1797         interfaces->iface = iface;
1798         hapd_iface = interfaces->iface[interfaces->count] =
1799                 os_zalloc(sizeof(*hapd_iface));
1800         if (hapd_iface == NULL) {
1801                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1802                            "the interface", __func__);
1803                 return NULL;
1804         }
1805         interfaces->count++;
1806         hapd_iface->interfaces = interfaces;
1807
1808         return hapd_iface;
1809 }
1810
1811
1812 static struct hostapd_config *
1813 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1814                      const char *ctrl_iface)
1815 {
1816         struct hostapd_bss_config *bss;
1817         struct hostapd_config *conf;
1818
1819         /* Allocates memory for bss and conf */
1820         conf = hostapd_config_defaults();
1821         if (conf == NULL) {
1822                  wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1823                                 "configuration", __func__);
1824                 return NULL;
1825         }
1826
1827         conf->driver = wpa_drivers[0];
1828         if (conf->driver == NULL) {
1829                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1830                 hostapd_config_free(conf);
1831                 return NULL;
1832         }
1833
1834         bss = conf->last_bss = conf->bss[0];
1835
1836         os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1837         bss->ctrl_interface = os_strdup(ctrl_iface);
1838         if (bss->ctrl_interface == NULL) {
1839                 hostapd_config_free(conf);
1840                 return NULL;
1841         }
1842
1843         /* Reading configuration file skipped, will be done in SET!
1844          * From reading the configuration till the end has to be done in
1845          * SET
1846          */
1847         return conf;
1848 }
1849
1850
1851 static struct hostapd_iface * hostapd_data_alloc(
1852         struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1853 {
1854         size_t i;
1855         struct hostapd_iface *hapd_iface =
1856                 interfaces->iface[interfaces->count - 1];
1857         struct hostapd_data *hapd;
1858
1859         hapd_iface->conf = conf;
1860         hapd_iface->num_bss = conf->num_bss;
1861
1862         hapd_iface->bss = os_zalloc(conf->num_bss *
1863                                     sizeof(struct hostapd_data *));
1864         if (hapd_iface->bss == NULL)
1865                 return NULL;
1866
1867         for (i = 0; i < conf->num_bss; i++) {
1868                 hapd = hapd_iface->bss[i] =
1869                         hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1870                 if (hapd == NULL)
1871                         return NULL;
1872                 hapd->msg_ctx = hapd;
1873         }
1874
1875         hapd_iface->interfaces = interfaces;
1876
1877         return hapd_iface;
1878 }
1879
1880
1881 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1882 {
1883         struct hostapd_config *conf = NULL;
1884         struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1885         struct hostapd_data *hapd;
1886         char *ptr;
1887         size_t i, j;
1888         const char *conf_file = NULL, *phy_name = NULL;
1889
1890         if (os_strncmp(buf, "bss_config=", 11) == 0) {
1891                 char *pos;
1892                 phy_name = buf + 11;
1893                 pos = os_strchr(phy_name, ':');
1894                 if (!pos)
1895                         return -1;
1896                 *pos++ = '\0';
1897                 conf_file = pos;
1898                 if (!os_strlen(conf_file))
1899                         return -1;
1900
1901                 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1902                                                         conf_file, 0);
1903                 if (!hapd_iface)
1904                         return -1;
1905                 for (j = 0; j < interfaces->count; j++) {
1906                         if (interfaces->iface[j] == hapd_iface)
1907                                 break;
1908                 }
1909                 if (j == interfaces->count) {
1910                         struct hostapd_iface **tmp;
1911                         tmp = os_realloc_array(interfaces->iface,
1912                                                interfaces->count + 1,
1913                                                sizeof(struct hostapd_iface *));
1914                         if (!tmp) {
1915                                 hostapd_interface_deinit_free(hapd_iface);
1916                                 return -1;
1917                         }
1918                         interfaces->iface = tmp;
1919                         interfaces->iface[interfaces->count++] = hapd_iface;
1920                         new_iface = hapd_iface;
1921                 }
1922
1923                 if (new_iface) {
1924                         if (interfaces->driver_init(hapd_iface) ||
1925                             hostapd_setup_interface(hapd_iface)) {
1926                                 interfaces->count--;
1927                                 goto fail;
1928                         }
1929                 } else {
1930                         /* Assign new BSS with bss[0]'s driver info */
1931                         hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1932                         hapd->driver = hapd_iface->bss[0]->driver;
1933                         hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1934                         os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1935                                   ETH_ALEN);
1936
1937                         if (start_ctrl_iface_bss(hapd) < 0 ||
1938                             (hapd_iface->state == HAPD_IFACE_ENABLED &&
1939                              hostapd_setup_bss(hapd, -1))) {
1940                                 hostapd_cleanup(hapd);
1941                                 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1942                                 hapd_iface->conf->num_bss--;
1943                                 hapd_iface->num_bss--;
1944                                 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1945                                            __func__, hapd, hapd->conf->iface);
1946                                 os_free(hapd);
1947                                 return -1;
1948                         }
1949                 }
1950                 return 0;
1951         }
1952
1953         ptr = os_strchr(buf, ' ');
1954         if (ptr == NULL)
1955                 return -1;
1956         *ptr++ = '\0';
1957
1958         if (os_strncmp(ptr, "config=", 7) == 0)
1959                 conf_file = ptr + 7;
1960
1961         for (i = 0; i < interfaces->count; i++) {
1962                 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1963                                buf)) {
1964                         wpa_printf(MSG_INFO, "Cannot add interface - it "
1965                                    "already exists");
1966                         return -1;
1967                 }
1968         }
1969
1970         hapd_iface = hostapd_iface_alloc(interfaces);
1971         if (hapd_iface == NULL) {
1972                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1973                            "for interface", __func__);
1974                 goto fail;
1975         }
1976
1977         if (conf_file && interfaces->config_read_cb) {
1978                 conf = interfaces->config_read_cb(conf_file);
1979                 if (conf && conf->bss)
1980                         os_strlcpy(conf->bss[0]->iface, buf,
1981                                    sizeof(conf->bss[0]->iface));
1982         } else
1983                 conf = hostapd_config_alloc(interfaces, buf, ptr);
1984         if (conf == NULL || conf->bss == NULL) {
1985                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1986                            "for configuration", __func__);
1987                 goto fail;
1988         }
1989
1990         hapd_iface = hostapd_data_alloc(interfaces, conf);
1991         if (hapd_iface == NULL) {
1992                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1993                            "for hostapd", __func__);
1994                 goto fail;
1995         }
1996
1997         if (start_ctrl_iface(hapd_iface) < 0)
1998                 goto fail;
1999
2000         wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
2001
2002         return 0;
2003
2004 fail:
2005         if (conf)
2006                 hostapd_config_free(conf);
2007         if (hapd_iface) {
2008                 if (hapd_iface->bss) {
2009                         for (i = 0; i < hapd_iface->num_bss; i++) {
2010                                 hapd = hapd_iface->bss[i];
2011                                 if (!hapd)
2012                                         continue;
2013                                 if (hapd_iface->interfaces &&
2014                                     hapd_iface->interfaces->ctrl_iface_deinit)
2015                                         hapd_iface->interfaces->
2016                                                 ctrl_iface_deinit(hapd);
2017                                 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2018                                            __func__, hapd_iface->bss[i],
2019                                            hapd->conf->iface);
2020                                 os_free(hapd);
2021                                 hapd_iface->bss[i] = NULL;
2022                         }
2023                         os_free(hapd_iface->bss);
2024                 }
2025                 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2026                            __func__, hapd_iface);
2027                 os_free(hapd_iface);
2028         }
2029         return -1;
2030 }
2031
2032
2033 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2034 {
2035         size_t i;
2036
2037         wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2038
2039         /* Remove hostapd_data only if it has already been initialized */
2040         if (idx < iface->num_bss) {
2041                 struct hostapd_data *hapd = iface->bss[idx];
2042
2043                 hostapd_bss_deinit(hapd);
2044                 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2045                            __func__, hapd, hapd->conf->iface);
2046                 hostapd_config_free_bss(hapd->conf);
2047                 os_free(hapd);
2048
2049                 iface->num_bss--;
2050
2051                 for (i = idx; i < iface->num_bss; i++)
2052                         iface->bss[i] = iface->bss[i + 1];
2053         } else {
2054                 hostapd_config_free_bss(iface->conf->bss[idx]);
2055                 iface->conf->bss[idx] = NULL;
2056         }
2057
2058         iface->conf->num_bss--;
2059         for (i = idx; i < iface->conf->num_bss; i++)
2060                 iface->conf->bss[i] = iface->conf->bss[i + 1];
2061
2062         return 0;
2063 }
2064
2065
2066 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2067 {
2068         struct hostapd_iface *hapd_iface;
2069         size_t i, j, k = 0;
2070
2071         for (i = 0; i < interfaces->count; i++) {
2072                 hapd_iface = interfaces->iface[i];
2073                 if (hapd_iface == NULL)
2074                         return -1;
2075                 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2076                         wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2077                         hapd_iface->driver_ap_teardown =
2078                                 !!(hapd_iface->drv_flags &
2079                                    WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2080
2081                         hostapd_interface_deinit_free(hapd_iface);
2082                         k = i;
2083                         while (k < (interfaces->count - 1)) {
2084                                 interfaces->iface[k] =
2085                                         interfaces->iface[k + 1];
2086                                 k++;
2087                         }
2088                         interfaces->count--;
2089                         return 0;
2090                 }
2091
2092                 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2093                         if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2094                                 hapd_iface->driver_ap_teardown =
2095                                         !(hapd_iface->drv_flags &
2096                                           WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2097                                 return hostapd_remove_bss(hapd_iface, j);
2098                         }
2099                 }
2100         }
2101         return -1;
2102 }
2103
2104
2105 /**
2106  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2107  * @hapd: Pointer to BSS data
2108  * @sta: Pointer to the associated STA data
2109  * @reassoc: 1 to indicate this was a re-association; 0 = first association
2110  *
2111  * This function will be called whenever a station associates with the AP. It
2112  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2113  * from drv_callbacks.c based on driver events for drivers that take care of
2114  * management frames (IEEE 802.11 authentication and association) internally.
2115  */
2116 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2117                            int reassoc)
2118 {
2119         if (hapd->tkip_countermeasures) {
2120                 hostapd_drv_sta_deauth(hapd, sta->addr,
2121                                        WLAN_REASON_MICHAEL_MIC_FAILURE);
2122                 return;
2123         }
2124
2125         hostapd_prune_associations(hapd, sta->addr);
2126
2127         /* IEEE 802.11F (IAPP) */
2128         if (hapd->conf->ieee802_11f)
2129                 iapp_new_station(hapd->iapp, sta);
2130
2131 #ifdef CONFIG_P2P
2132         if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2133                 sta->no_p2p_set = 1;
2134                 hapd->num_sta_no_p2p++;
2135                 if (hapd->num_sta_no_p2p == 1)
2136                         hostapd_p2p_non_p2p_sta_connected(hapd);
2137         }
2138 #endif /* CONFIG_P2P */
2139
2140         /* Start accounting here, if IEEE 802.1X and WPA are not used.
2141          * IEEE 802.1X/WPA code will start accounting after the station has
2142          * been authorized. */
2143         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2144                 ap_sta_set_authorized(hapd, sta, 1);
2145                 os_get_reltime(&sta->connected_time);
2146                 accounting_sta_start(hapd, sta);
2147         }
2148
2149         /* Start IEEE 802.1X authentication process for new stations */
2150         ieee802_1x_new_station(hapd, sta);
2151         if (reassoc) {
2152                 if (sta->auth_alg != WLAN_AUTH_FT &&
2153                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2154                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2155         } else
2156                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2157
2158         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2159                 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2160                            "for " MACSTR " (%d seconds - ap_max_inactivity)",
2161                            __func__, MAC2STR(sta->addr),
2162                            hapd->conf->ap_max_inactivity);
2163                 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2164                 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2165                                        ap_handle_timer, hapd, sta);
2166         }
2167 }
2168
2169
2170 const char * hostapd_state_text(enum hostapd_iface_state s)
2171 {
2172         switch (s) {
2173         case HAPD_IFACE_UNINITIALIZED:
2174                 return "UNINITIALIZED";
2175         case HAPD_IFACE_DISABLED:
2176                 return "DISABLED";
2177         case HAPD_IFACE_COUNTRY_UPDATE:
2178                 return "COUNTRY_UPDATE";
2179         case HAPD_IFACE_ACS:
2180                 return "ACS";
2181         case HAPD_IFACE_HT_SCAN:
2182                 return "HT_SCAN";
2183         case HAPD_IFACE_DFS:
2184                 return "DFS";
2185         case HAPD_IFACE_ENABLED:
2186                 return "ENABLED";
2187         }
2188
2189         return "UNKNOWN";
2190 }
2191
2192
2193 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2194 {
2195         wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2196                    iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2197                    hostapd_state_text(s));
2198         iface->state = s;
2199 }
2200
2201
2202 #ifdef NEED_AP_MLME
2203
2204 static void free_beacon_data(struct beacon_data *beacon)
2205 {
2206         os_free(beacon->head);
2207         beacon->head = NULL;
2208         os_free(beacon->tail);
2209         beacon->tail = NULL;
2210         os_free(beacon->probe_resp);
2211         beacon->probe_resp = NULL;
2212         os_free(beacon->beacon_ies);
2213         beacon->beacon_ies = NULL;
2214         os_free(beacon->proberesp_ies);
2215         beacon->proberesp_ies = NULL;
2216         os_free(beacon->assocresp_ies);
2217         beacon->assocresp_ies = NULL;
2218 }
2219
2220
2221 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2222                                      struct beacon_data *beacon)
2223 {
2224         struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2225         struct wpa_driver_ap_params params;
2226         int ret;
2227
2228         os_memset(beacon, 0, sizeof(*beacon));
2229         ret = ieee802_11_build_ap_params(hapd, &params);
2230         if (ret < 0)
2231                 return ret;
2232
2233         ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2234                                          &proberesp_extra,
2235                                          &assocresp_extra);
2236         if (ret)
2237                 goto free_ap_params;
2238
2239         ret = -1;
2240         beacon->head = os_malloc(params.head_len);
2241         if (!beacon->head)
2242                 goto free_ap_extra_ies;
2243
2244         os_memcpy(beacon->head, params.head, params.head_len);
2245         beacon->head_len = params.head_len;
2246
2247         beacon->tail = os_malloc(params.tail_len);
2248         if (!beacon->tail)
2249                 goto free_beacon;
2250
2251         os_memcpy(beacon->tail, params.tail, params.tail_len);
2252         beacon->tail_len = params.tail_len;
2253
2254         if (params.proberesp != NULL) {
2255                 beacon->probe_resp = os_malloc(params.proberesp_len);
2256                 if (!beacon->probe_resp)
2257                         goto free_beacon;
2258
2259                 os_memcpy(beacon->probe_resp, params.proberesp,
2260                           params.proberesp_len);
2261                 beacon->probe_resp_len = params.proberesp_len;
2262         }
2263
2264         /* copy the extra ies */
2265         if (beacon_extra) {
2266                 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2267                 if (!beacon->beacon_ies)
2268                         goto free_beacon;
2269
2270                 os_memcpy(beacon->beacon_ies,
2271                           beacon_extra->buf, wpabuf_len(beacon_extra));
2272                 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2273         }
2274
2275         if (proberesp_extra) {
2276                 beacon->proberesp_ies =
2277                         os_malloc(wpabuf_len(proberesp_extra));
2278                 if (!beacon->proberesp_ies)
2279                         goto free_beacon;
2280
2281                 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2282                           wpabuf_len(proberesp_extra));
2283                 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2284         }
2285
2286         if (assocresp_extra) {
2287                 beacon->assocresp_ies =
2288                         os_malloc(wpabuf_len(assocresp_extra));
2289                 if (!beacon->assocresp_ies)
2290                         goto free_beacon;
2291
2292                 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2293                           wpabuf_len(assocresp_extra));
2294                 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2295         }
2296
2297         ret = 0;
2298 free_beacon:
2299         /* if the function fails, the caller should not free beacon data */
2300         if (ret)
2301                 free_beacon_data(beacon);
2302
2303 free_ap_extra_ies:
2304         hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2305                                   assocresp_extra);
2306 free_ap_params:
2307         ieee802_11_free_ap_params(&params);
2308         return ret;
2309 }
2310
2311
2312 /*
2313  * TODO: This flow currently supports only changing frequency within the
2314  * same hw_mode. Any other changes to MAC parameters or provided settings (even
2315  * width) are not supported.
2316  */
2317 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2318                                       struct hostapd_config *conf,
2319                                       struct hostapd_freq_params *params,
2320                                       struct hostapd_freq_params *old_params)
2321 {
2322         int channel;
2323
2324         if (!params->channel) {
2325                 /* check if the new channel is supported by hw */
2326                 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2327         }
2328
2329         channel = params->channel;
2330         if (!channel)
2331                 return -1;
2332
2333         /* if a pointer to old_params is provided we save previous state */
2334         if (old_params) {
2335                 old_params->channel = conf->channel;
2336                 old_params->ht_enabled = conf->ieee80211n;
2337                 old_params->sec_channel_offset = conf->secondary_channel;
2338         }
2339
2340         conf->channel = channel;
2341         conf->ieee80211n = params->ht_enabled;
2342         conf->secondary_channel = params->sec_channel_offset;
2343
2344         /* TODO: maybe call here hostapd_config_check here? */
2345
2346         return 0;
2347 }
2348
2349
2350 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2351                                      struct csa_settings *settings)
2352 {
2353         struct hostapd_iface *iface = hapd->iface;
2354         struct hostapd_freq_params old_freq;
2355         int ret;
2356
2357         os_memset(&old_freq, 0, sizeof(old_freq));
2358         if (!iface || !iface->freq || hapd->csa_in_progress)
2359                 return -1;
2360
2361         ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2362                                          &settings->freq_params,
2363                                          &old_freq);
2364         if (ret)
2365                 return ret;
2366
2367         ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2368
2369         /* change back the configuration */
2370         hostapd_change_config_freq(iface->bss[0], iface->conf,
2371                                    &old_freq, NULL);
2372
2373         if (ret)
2374                 return ret;
2375
2376         /* set channel switch parameters for csa ie */
2377         hapd->cs_freq_params = settings->freq_params;
2378         hapd->cs_count = settings->cs_count;
2379         hapd->cs_block_tx = settings->block_tx;
2380
2381         ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2382         if (ret) {
2383                 free_beacon_data(&settings->beacon_after);
2384                 return ret;
2385         }
2386
2387         settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2388         settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2389
2390         return 0;
2391 }
2392
2393
2394 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2395 {
2396         os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2397         hapd->cs_count = 0;
2398         hapd->cs_block_tx = 0;
2399         hapd->cs_c_off_beacon = 0;
2400         hapd->cs_c_off_proberesp = 0;
2401         hapd->csa_in_progress = 0;
2402 }
2403
2404
2405 int hostapd_switch_channel(struct hostapd_data *hapd,
2406                            struct csa_settings *settings)
2407 {
2408         int ret;
2409
2410         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2411                 wpa_printf(MSG_INFO, "CSA is not supported");
2412                 return -1;
2413         }
2414
2415         ret = hostapd_fill_csa_settings(hapd, settings);
2416         if (ret)
2417                 return ret;
2418
2419         ret = hostapd_drv_switch_channel(hapd, settings);
2420         free_beacon_data(&settings->beacon_csa);
2421         free_beacon_data(&settings->beacon_after);
2422
2423         if (ret) {
2424                 /* if we failed, clean cs parameters */
2425                 hostapd_cleanup_cs_params(hapd);
2426                 return ret;
2427         }
2428
2429         hapd->csa_in_progress = 1;
2430         return 0;
2431 }
2432
2433
2434 void
2435 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2436                                 const struct hostapd_freq_params *freq_params)
2437 {
2438         int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2439         unsigned int i;
2440
2441         wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2442
2443         if (freq_params->center_freq1)
2444                 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2445         if (freq_params->center_freq2)
2446                 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2447
2448         switch (freq_params->bandwidth) {
2449         case 0:
2450         case 20:
2451         case 40:
2452                 vht_bw = VHT_CHANWIDTH_USE_HT;
2453                 break;
2454         case 80:
2455                 if (freq_params->center_freq2)
2456                         vht_bw = VHT_CHANWIDTH_80P80MHZ;
2457                 else
2458                         vht_bw = VHT_CHANWIDTH_80MHZ;
2459                 break;
2460         case 160:
2461                 vht_bw = VHT_CHANWIDTH_160MHZ;
2462                 break;
2463         default:
2464                 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2465                            freq_params->bandwidth);
2466                 break;
2467         }
2468
2469         iface->freq = freq_params->freq;
2470         iface->conf->channel = freq_params->channel;
2471         iface->conf->secondary_channel = freq_params->sec_channel_offset;
2472         iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2473         iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2474         iface->conf->vht_oper_chwidth = vht_bw;
2475         iface->conf->ieee80211n = freq_params->ht_enabled;
2476         iface->conf->ieee80211ac = freq_params->vht_enabled;
2477
2478         /*
2479          * cs_params must not be cleared earlier because the freq_params
2480          * argument may actually point to one of these.
2481          */
2482         for (i = 0; i < iface->num_bss; i++)
2483                 hostapd_cleanup_cs_params(iface->bss[i]);
2484
2485         hostapd_disable_iface(iface);
2486         hostapd_enable_iface(iface);
2487 }
2488
2489 #endif /* NEED_AP_MLME */