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