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