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