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