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