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