]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/wpa/src/ap/hostapd.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / wpa / src / ap / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2012, 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 "radius/radius_client.h"
15 #include "radius/radius_das.h"
16 #include "drivers/driver.h"
17 #include "hostapd.h"
18 #include "authsrv.h"
19 #include "sta_info.h"
20 #include "accounting.h"
21 #include "ap_list.h"
22 #include "beacon.h"
23 #include "iapp.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11_auth.h"
26 #include "vlan_init.h"
27 #include "wpa_auth.h"
28 #include "wps_hostapd.h"
29 #include "hw_features.h"
30 #include "wpa_auth_glue.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "p2p_hostapd.h"
34 #include "gas_serv.h"
35
36
37 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
38 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
39 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
40
41 extern int wpa_debug_level;
42 extern struct wpa_driver_ops *wpa_drivers[];
43
44
45 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
46                                int (*cb)(struct hostapd_iface *iface,
47                                          void *ctx), void *ctx)
48 {
49         size_t i;
50         int ret;
51
52         for (i = 0; i < interfaces->count; i++) {
53                 ret = cb(interfaces->iface[i], ctx);
54                 if (ret)
55                         return ret;
56         }
57
58         return 0;
59 }
60
61
62 static void hostapd_reload_bss(struct hostapd_data *hapd)
63 {
64 #ifndef CONFIG_NO_RADIUS
65         radius_client_reconfig(hapd->radius, hapd->conf->radius);
66 #endif /* CONFIG_NO_RADIUS */
67
68         if (hostapd_setup_wpa_psk(hapd->conf)) {
69                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
70                            "after reloading configuration");
71         }
72
73         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
74                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
75         else
76                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
77
78         if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
79                 hostapd_setup_wpa(hapd);
80                 if (hapd->wpa_auth)
81                         wpa_init_keys(hapd->wpa_auth);
82         } else if (hapd->conf->wpa) {
83                 const u8 *wpa_ie;
84                 size_t wpa_ie_len;
85                 hostapd_reconfig_wpa(hapd);
86                 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
87                 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
88                         wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
89                                    "the kernel driver.");
90         } else if (hapd->wpa_auth) {
91                 wpa_deinit(hapd->wpa_auth);
92                 hapd->wpa_auth = NULL;
93                 hostapd_set_privacy(hapd, 0);
94                 hostapd_setup_encryption(hapd->conf->iface, hapd);
95                 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
96         }
97
98         ieee802_11_set_beacon(hapd);
99         hostapd_update_wps(hapd);
100
101         if (hapd->conf->ssid.ssid_set &&
102             hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
103                              hapd->conf->ssid.ssid_len)) {
104                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
105                 /* try to continue */
106         }
107         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
108 }
109
110
111 int hostapd_reload_config(struct hostapd_iface *iface)
112 {
113         struct hostapd_data *hapd = iface->bss[0];
114         struct hostapd_config *newconf, *oldconf;
115         size_t j;
116
117         if (iface->interfaces == NULL ||
118             iface->interfaces->config_read_cb == NULL)
119                 return -1;
120         newconf = iface->interfaces->config_read_cb(iface->config_fname);
121         if (newconf == NULL)
122                 return -1;
123
124         /*
125          * Deauthenticate all stations since the new configuration may not
126          * allow them to use the BSS anymore.
127          */
128         for (j = 0; j < iface->num_bss; j++) {
129                 hostapd_flush_old_stations(iface->bss[j],
130                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
131                 hostapd_broadcast_wep_clear(iface->bss[j]);
132
133 #ifndef CONFIG_NO_RADIUS
134                 /* TODO: update dynamic data based on changed configuration
135                  * items (e.g., open/close sockets, etc.) */
136                 radius_client_flush(iface->bss[j]->radius, 0);
137 #endif /* CONFIG_NO_RADIUS */
138         }
139
140         oldconf = hapd->iconf;
141         iface->conf = newconf;
142
143         for (j = 0; j < iface->num_bss; j++) {
144                 hapd = iface->bss[j];
145                 hapd->iconf = newconf;
146                 hapd->conf = &newconf->bss[j];
147                 hostapd_reload_bss(hapd);
148         }
149
150         hostapd_config_free(oldconf);
151
152
153         return 0;
154 }
155
156
157 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
158                                               char *ifname)
159 {
160         int i;
161
162         for (i = 0; i < NUM_WEP_KEYS; i++) {
163                 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
164                                         0, NULL, 0, NULL, 0)) {
165                         wpa_printf(MSG_DEBUG, "Failed to clear default "
166                                    "encryption keys (ifname=%s keyidx=%d)",
167                                    ifname, i);
168                 }
169         }
170 #ifdef CONFIG_IEEE80211W
171         if (hapd->conf->ieee80211w) {
172                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
173                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
174                                                 NULL, i, 0, NULL,
175                                                 0, NULL, 0)) {
176                                 wpa_printf(MSG_DEBUG, "Failed to clear "
177                                            "default mgmt encryption keys "
178                                            "(ifname=%s keyidx=%d)", ifname, i);
179                         }
180                 }
181         }
182 #endif /* CONFIG_IEEE80211W */
183 }
184
185
186 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
187 {
188         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
189         return 0;
190 }
191
192
193 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
194 {
195         int errors = 0, idx;
196         struct hostapd_ssid *ssid = &hapd->conf->ssid;
197
198         idx = ssid->wep.idx;
199         if (ssid->wep.default_len &&
200             hostapd_drv_set_key(hapd->conf->iface,
201                                 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
202                                 1, NULL, 0, ssid->wep.key[idx],
203                                 ssid->wep.len[idx])) {
204                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
205                 errors++;
206         }
207
208         if (ssid->dyn_vlan_keys) {
209                 size_t i;
210                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
211                         const char *ifname;
212                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
213                         if (key == NULL)
214                                 continue;
215                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
216                                                             i);
217                         if (ifname == NULL)
218                                 continue;
219
220                         idx = key->idx;
221                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
222                                                 broadcast_ether_addr, idx, 1,
223                                                 NULL, 0, key->key[idx],
224                                                 key->len[idx])) {
225                                 wpa_printf(MSG_WARNING, "Could not set "
226                                            "dynamic VLAN WEP encryption.");
227                                 errors++;
228                         }
229                 }
230         }
231
232         return errors;
233 }
234
235
236 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
237 {
238         iapp_deinit(hapd->iapp);
239         hapd->iapp = NULL;
240         accounting_deinit(hapd);
241         hostapd_deinit_wpa(hapd);
242         vlan_deinit(hapd);
243         hostapd_acl_deinit(hapd);
244 #ifndef CONFIG_NO_RADIUS
245         radius_client_deinit(hapd->radius);
246         hapd->radius = NULL;
247         radius_das_deinit(hapd->radius_das);
248         hapd->radius_das = NULL;
249 #endif /* CONFIG_NO_RADIUS */
250
251         hostapd_deinit_wps(hapd);
252
253         authsrv_deinit(hapd);
254
255         if (hapd->interface_added &&
256             hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
257                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
258                            hapd->conf->iface);
259         }
260
261         os_free(hapd->probereq_cb);
262         hapd->probereq_cb = NULL;
263
264 #ifdef CONFIG_P2P
265         wpabuf_free(hapd->p2p_beacon_ie);
266         hapd->p2p_beacon_ie = NULL;
267         wpabuf_free(hapd->p2p_probe_resp_ie);
268         hapd->p2p_probe_resp_ie = NULL;
269 #endif /* CONFIG_P2P */
270
271         wpabuf_free(hapd->time_adv);
272
273 #ifdef CONFIG_INTERWORKING
274         gas_serv_deinit(hapd);
275 #endif /* CONFIG_INTERWORKING */
276
277 #ifdef CONFIG_SQLITE
278         os_free(hapd->tmp_eap_user.identity);
279         os_free(hapd->tmp_eap_user.password);
280 #endif /* CONFIG_SQLITE */
281 }
282
283
284 /**
285  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
286  * @hapd: Pointer to BSS data
287  *
288  * This function is used to free all per-BSS data structures and resources.
289  * This gets called in a loop for each BSS between calls to
290  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
291  * is deinitialized. Most of the modules that are initialized in
292  * hostapd_setup_bss() are deinitialized here.
293  */
294 static void hostapd_cleanup(struct hostapd_data *hapd)
295 {
296         if (hapd->iface->interfaces &&
297             hapd->iface->interfaces->ctrl_iface_deinit)
298                 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
299         hostapd_free_hapd_data(hapd);
300 }
301
302
303 /**
304  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
305  * @iface: Pointer to interface data
306  *
307  * This function is called before per-BSS data structures are deinitialized
308  * with hostapd_cleanup().
309  */
310 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
311 {
312 }
313
314
315 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
316 {
317         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
318         iface->hw_features = NULL;
319         os_free(iface->current_rates);
320         iface->current_rates = NULL;
321         os_free(iface->basic_rates);
322         iface->basic_rates = NULL;
323         ap_list_deinit(iface);
324 }
325
326
327 /**
328  * hostapd_cleanup_iface - Complete per-interface cleanup
329  * @iface: Pointer to interface data
330  *
331  * This function is called after per-BSS data structures are deinitialized
332  * with hostapd_cleanup().
333  */
334 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
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         os_free(iface);
343 }
344
345
346 static void hostapd_clear_wep(struct hostapd_data *hapd)
347 {
348         if (hapd->drv_priv) {
349                 hostapd_set_privacy(hapd, 0);
350                 hostapd_broadcast_wep_clear(hapd);
351         }
352 }
353
354
355 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
356 {
357         int i;
358
359         hostapd_broadcast_wep_set(hapd);
360
361         if (hapd->conf->ssid.wep.default_len) {
362                 hostapd_set_privacy(hapd, 1);
363                 return 0;
364         }
365
366         /*
367          * When IEEE 802.1X is not enabled, the driver may need to know how to
368          * set authentication algorithms for static WEP.
369          */
370         hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
371
372         for (i = 0; i < 4; i++) {
373                 if (hapd->conf->ssid.wep.key[i] &&
374                     hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
375                                         i == hapd->conf->ssid.wep.idx, NULL, 0,
376                                         hapd->conf->ssid.wep.key[i],
377                                         hapd->conf->ssid.wep.len[i])) {
378                         wpa_printf(MSG_WARNING, "Could not set WEP "
379                                    "encryption.");
380                         return -1;
381                 }
382                 if (hapd->conf->ssid.wep.key[i] &&
383                     i == hapd->conf->ssid.wep.idx)
384                         hostapd_set_privacy(hapd, 1);
385         }
386
387         return 0;
388 }
389
390
391 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
392 {
393         int ret = 0;
394         u8 addr[ETH_ALEN];
395
396         if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
397                 return 0;
398
399         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
400         if (hostapd_flush(hapd)) {
401                 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
402                         "kernel driver");
403                 ret = -1;
404         }
405         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
406         os_memset(addr, 0xff, ETH_ALEN);
407         hostapd_drv_sta_deauth(hapd, addr, reason);
408         hostapd_free_stas(hapd);
409
410         return ret;
411 }
412
413
414 /**
415  * hostapd_validate_bssid_configuration - Validate BSSID configuration
416  * @iface: Pointer to interface data
417  * Returns: 0 on success, -1 on failure
418  *
419  * This function is used to validate that the configured BSSIDs are valid.
420  */
421 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
422 {
423         u8 mask[ETH_ALEN] = { 0 };
424         struct hostapd_data *hapd = iface->bss[0];
425         unsigned int i = iface->conf->num_bss, bits = 0, j;
426         int auto_addr = 0;
427
428         if (hostapd_drv_none(hapd))
429                 return 0;
430
431         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
432
433         /* Determine the bits necessary to cover the number of BSSIDs. */
434         for (i--; i; i >>= 1)
435                 bits++;
436
437         /* Determine the bits necessary to any configured BSSIDs,
438            if they are higher than the number of BSSIDs. */
439         for (j = 0; j < iface->conf->num_bss; j++) {
440                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
441                         if (j)
442                                 auto_addr++;
443                         continue;
444                 }
445
446                 for (i = 0; i < ETH_ALEN; i++) {
447                         mask[i] |=
448                                 iface->conf->bss[j].bssid[i] ^
449                                 hapd->own_addr[i];
450                 }
451         }
452
453         if (!auto_addr)
454                 goto skip_mask_ext;
455
456         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
457                 ;
458         j = 0;
459         if (i < ETH_ALEN) {
460                 j = (5 - i) * 8;
461
462                 while (mask[i] != 0) {
463                         mask[i] >>= 1;
464                         j++;
465                 }
466         }
467
468         if (bits < j)
469                 bits = j;
470
471         if (bits > 40) {
472                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
473                            bits);
474                 return -1;
475         }
476
477         os_memset(mask, 0xff, ETH_ALEN);
478         j = bits / 8;
479         for (i = 5; i > 5 - j; i--)
480                 mask[i] = 0;
481         j = bits % 8;
482         while (j--)
483                 mask[i] <<= 1;
484
485 skip_mask_ext:
486         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
487                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
488
489         if (!auto_addr)
490                 return 0;
491
492         for (i = 0; i < ETH_ALEN; i++) {
493                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
494                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
495                                    " for start address " MACSTR ".",
496                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
497                         wpa_printf(MSG_ERROR, "Start address must be the "
498                                    "first address in the block (i.e., addr "
499                                    "AND mask == addr).");
500                         return -1;
501                 }
502         }
503
504         return 0;
505 }
506
507
508 static int mac_in_conf(struct hostapd_config *conf, const void *a)
509 {
510         size_t i;
511
512         for (i = 0; i < conf->num_bss; i++) {
513                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
514                         return 1;
515                 }
516         }
517
518         return 0;
519 }
520
521
522 #ifndef CONFIG_NO_RADIUS
523
524 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
525                                     struct radius_das_attrs *attr)
526 {
527         /* TODO */
528         return 0;
529 }
530
531
532 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
533                                               struct radius_das_attrs *attr)
534 {
535         struct sta_info *sta = NULL;
536         char buf[128];
537
538         if (attr->sta_addr)
539                 sta = ap_get_sta(hapd, attr->sta_addr);
540
541         if (sta == NULL && attr->acct_session_id &&
542             attr->acct_session_id_len == 17) {
543                 for (sta = hapd->sta_list; sta; sta = sta->next) {
544                         os_snprintf(buf, sizeof(buf), "%08X-%08X",
545                                     sta->acct_session_id_hi,
546                                     sta->acct_session_id_lo);
547                         if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
548                                 break;
549                 }
550         }
551
552         if (sta == NULL && attr->cui) {
553                 for (sta = hapd->sta_list; sta; sta = sta->next) {
554                         struct wpabuf *cui;
555                         cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
556                         if (cui && wpabuf_len(cui) == attr->cui_len &&
557                             os_memcmp(wpabuf_head(cui), attr->cui,
558                                       attr->cui_len) == 0)
559                                 break;
560                 }
561         }
562
563         if (sta == NULL && attr->user_name) {
564                 for (sta = hapd->sta_list; sta; sta = sta->next) {
565                         u8 *identity;
566                         size_t identity_len;
567                         identity = ieee802_1x_get_identity(sta->eapol_sm,
568                                                            &identity_len);
569                         if (identity &&
570                             identity_len == attr->user_name_len &&
571                             os_memcmp(identity, attr->user_name, identity_len)
572                             == 0)
573                                 break;
574                 }
575         }
576
577         return sta;
578 }
579
580
581 static enum radius_das_res
582 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
583 {
584         struct hostapd_data *hapd = ctx;
585         struct sta_info *sta;
586
587         if (hostapd_das_nas_mismatch(hapd, attr))
588                 return RADIUS_DAS_NAS_MISMATCH;
589
590         sta = hostapd_das_find_sta(hapd, attr);
591         if (sta == NULL)
592                 return RADIUS_DAS_SESSION_NOT_FOUND;
593
594         hostapd_drv_sta_deauth(hapd, sta->addr,
595                                WLAN_REASON_PREV_AUTH_NOT_VALID);
596         ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
597
598         return RADIUS_DAS_SUCCESS;
599 }
600
601 #endif /* CONFIG_NO_RADIUS */
602
603
604 /**
605  * hostapd_setup_bss - Per-BSS setup (initialization)
606  * @hapd: Pointer to BSS data
607  * @first: Whether this BSS is the first BSS of an interface
608  *
609  * This function is used to initialize all per-BSS data structures and
610  * resources. This gets called in a loop for each BSS when an interface is
611  * initialized. Most of the modules that are initialized here will be
612  * deinitialized in hostapd_cleanup().
613  */
614 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
615 {
616         struct hostapd_bss_config *conf = hapd->conf;
617         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
618         int ssid_len, set_ssid;
619         char force_ifname[IFNAMSIZ];
620         u8 if_addr[ETH_ALEN];
621
622         if (!first) {
623                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
624                         /* Allocate the next available BSSID. */
625                         do {
626                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
627                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
628                 } else {
629                         /* Allocate the configured BSSID. */
630                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
631
632                         if (hostapd_mac_comp(hapd->own_addr,
633                                              hapd->iface->bss[0]->own_addr) ==
634                             0) {
635                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
636                                            "BSSID set to the MAC address of "
637                                            "the radio", hapd->conf->iface);
638                                 return -1;
639                         }
640                 }
641
642                 hapd->interface_added = 1;
643                 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
644                                    hapd->conf->iface, hapd->own_addr, hapd,
645                                    &hapd->drv_priv, force_ifname, if_addr,
646                                    hapd->conf->bridge[0] ? hapd->conf->bridge :
647                                    NULL)) {
648                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
649                                    MACSTR ")", MAC2STR(hapd->own_addr));
650                         return -1;
651                 }
652         }
653
654         if (conf->wmm_enabled < 0)
655                 conf->wmm_enabled = hapd->iconf->ieee80211n;
656
657         hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
658         hostapd_set_privacy(hapd, 0);
659
660         hostapd_broadcast_wep_clear(hapd);
661         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
662                 return -1;
663
664         /*
665          * Fetch the SSID from the system and use it or,
666          * if one was specified in the config file, verify they
667          * match.
668          */
669         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
670         if (ssid_len < 0) {
671                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
672                 return -1;
673         }
674         if (conf->ssid.ssid_set) {
675                 /*
676                  * If SSID is specified in the config file and it differs
677                  * from what is being used then force installation of the
678                  * new SSID.
679                  */
680                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
681                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
682         } else {
683                 /*
684                  * No SSID in the config file; just use the one we got
685                  * from the system.
686                  */
687                 set_ssid = 0;
688                 conf->ssid.ssid_len = ssid_len;
689                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
690         }
691
692         if (!hostapd_drv_none(hapd)) {
693                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
694                            " and ssid \"%s\"",
695                            hapd->conf->iface, MAC2STR(hapd->own_addr),
696                            wpa_ssid_txt(hapd->conf->ssid.ssid,
697                                         hapd->conf->ssid.ssid_len));
698         }
699
700         if (hostapd_setup_wpa_psk(conf)) {
701                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
702                 return -1;
703         }
704
705         /* Set SSID for the kernel driver (to be used in beacon and probe
706          * response frames) */
707         if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
708                                          conf->ssid.ssid_len)) {
709                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
710                 return -1;
711         }
712
713         if (wpa_debug_level == MSG_MSGDUMP)
714                 conf->radius->msg_dumps = 1;
715 #ifndef CONFIG_NO_RADIUS
716         hapd->radius = radius_client_init(hapd, conf->radius);
717         if (hapd->radius == NULL) {
718                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
719                 return -1;
720         }
721
722         if (hapd->conf->radius_das_port) {
723                 struct radius_das_conf das_conf;
724                 os_memset(&das_conf, 0, sizeof(das_conf));
725                 das_conf.port = hapd->conf->radius_das_port;
726                 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
727                 das_conf.shared_secret_len =
728                         hapd->conf->radius_das_shared_secret_len;
729                 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
730                 das_conf.time_window = hapd->conf->radius_das_time_window;
731                 das_conf.require_event_timestamp =
732                         hapd->conf->radius_das_require_event_timestamp;
733                 das_conf.ctx = hapd;
734                 das_conf.disconnect = hostapd_das_disconnect;
735                 hapd->radius_das = radius_das_init(&das_conf);
736                 if (hapd->radius_das == NULL) {
737                         wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
738                                    "failed.");
739                         return -1;
740                 }
741         }
742 #endif /* CONFIG_NO_RADIUS */
743
744         if (hostapd_acl_init(hapd)) {
745                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
746                 return -1;
747         }
748         if (hostapd_init_wps(hapd, conf))
749                 return -1;
750
751         if (authsrv_init(hapd) < 0)
752                 return -1;
753
754         if (ieee802_1x_init(hapd)) {
755                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
756                 return -1;
757         }
758
759         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
760                 return -1;
761
762         if (accounting_init(hapd)) {
763                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
764                 return -1;
765         }
766
767         if (hapd->conf->ieee802_11f &&
768             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
769                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
770                            "failed.");
771                 return -1;
772         }
773
774 #ifdef CONFIG_INTERWORKING
775         if (gas_serv_init(hapd)) {
776                 wpa_printf(MSG_ERROR, "GAS server initialization failed");
777                 return -1;
778         }
779 #endif /* CONFIG_INTERWORKING */
780
781         if (hapd->iface->interfaces &&
782             hapd->iface->interfaces->ctrl_iface_init &&
783             hapd->iface->interfaces->ctrl_iface_init(hapd)) {
784                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
785                 return -1;
786         }
787
788         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
789                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
790                 return -1;
791         }
792
793         ieee802_11_set_beacon(hapd);
794
795         if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
796                 return -1;
797
798         if (hapd->driver && hapd->driver->set_operstate)
799                 hapd->driver->set_operstate(hapd->drv_priv, 1);
800
801         return 0;
802 }
803
804
805 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
806 {
807         struct hostapd_data *hapd = iface->bss[0];
808         int i;
809         struct hostapd_tx_queue_params *p;
810
811         for (i = 0; i < NUM_TX_QUEUES; i++) {
812                 p = &iface->conf->tx_queue[i];
813
814                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
815                                                 p->cwmax, p->burst)) {
816                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
817                                    "parameters for queue %d.", i);
818                         /* Continue anyway */
819                 }
820         }
821 }
822
823
824 static int setup_interface(struct hostapd_iface *iface)
825 {
826         struct hostapd_data *hapd = iface->bss[0];
827         size_t i;
828         char country[4];
829
830         /*
831          * Make sure that all BSSes get configured with a pointer to the same
832          * driver interface.
833          */
834         for (i = 1; i < iface->num_bss; i++) {
835                 iface->bss[i]->driver = hapd->driver;
836                 iface->bss[i]->drv_priv = hapd->drv_priv;
837         }
838
839         if (hostapd_validate_bssid_configuration(iface))
840                 return -1;
841
842         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
843                 os_memcpy(country, hapd->iconf->country, 3);
844                 country[3] = '\0';
845                 if (hostapd_set_country(hapd, country) < 0) {
846                         wpa_printf(MSG_ERROR, "Failed to set country code");
847                         return -1;
848                 }
849         }
850
851         if (hostapd_get_hw_features(iface)) {
852                 /* Not all drivers support this yet, so continue without hw
853                  * feature data. */
854         } else {
855                 int ret = hostapd_select_hw_mode(iface);
856                 if (ret < 0) {
857                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
858                                    "channel. (%d)", ret);
859                         return -1;
860                 }
861                 ret = hostapd_check_ht_capab(iface);
862                 if (ret < 0)
863                         return -1;
864                 if (ret == 1) {
865                         wpa_printf(MSG_DEBUG, "Interface initialization will "
866                                    "be completed in a callback");
867                         return 0;
868                 }
869         }
870         return hostapd_setup_interface_complete(iface, 0);
871 }
872
873
874 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
875 {
876         struct hostapd_data *hapd = iface->bss[0];
877         size_t j;
878         u8 *prev_addr;
879
880         if (err) {
881                 wpa_printf(MSG_ERROR, "Interface initialization failed");
882                 eloop_terminate();
883                 return -1;
884         }
885
886         wpa_printf(MSG_DEBUG, "Completing interface initialization");
887         if (hapd->iconf->channel) {
888                 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
889                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
890                            "Frequency: %d MHz",
891                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
892                            hapd->iconf->channel, iface->freq);
893
894                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
895                                      hapd->iconf->channel,
896                                      hapd->iconf->ieee80211n,
897                                      hapd->iconf->secondary_channel)) {
898                         wpa_printf(MSG_ERROR, "Could not set channel for "
899                                    "kernel driver");
900                         return -1;
901                 }
902         }
903
904         if (iface->current_mode) {
905                 if (hostapd_prepare_rates(iface, iface->current_mode)) {
906                         wpa_printf(MSG_ERROR, "Failed to prepare rates "
907                                    "table.");
908                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
909                                        HOSTAPD_LEVEL_WARNING,
910                                        "Failed to prepare rates table.");
911                         return -1;
912                 }
913         }
914
915         if (hapd->iconf->rts_threshold > -1 &&
916             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
917                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
918                            "kernel driver");
919                 return -1;
920         }
921
922         if (hapd->iconf->fragm_threshold > -1 &&
923             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
924                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
925                            "for kernel driver");
926                 return -1;
927         }
928
929         prev_addr = hapd->own_addr;
930
931         for (j = 0; j < iface->num_bss; j++) {
932                 hapd = iface->bss[j];
933                 if (j)
934                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
935                 if (hostapd_setup_bss(hapd, j == 0))
936                         return -1;
937                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
938                         prev_addr = hapd->own_addr;
939         }
940
941         hostapd_tx_queue_params(iface);
942
943         ap_list_init(iface);
944
945         if (hostapd_driver_commit(hapd) < 0) {
946                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
947                            "configuration", __func__);
948                 return -1;
949         }
950
951         /*
952          * WPS UPnP module can be initialized only when the "upnp_iface" is up.
953          * If "interface" and "upnp_iface" are the same (e.g., non-bridge
954          * mode), the interface is up only after driver_commit, so initialize
955          * WPS after driver_commit.
956          */
957         for (j = 0; j < iface->num_bss; j++) {
958                 if (hostapd_init_wps_complete(iface->bss[j]))
959                         return -1;
960         }
961
962         if (hapd->setup_complete_cb)
963                 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
964
965         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
966                    iface->bss[0]->conf->iface);
967
968         return 0;
969 }
970
971
972 /**
973  * hostapd_setup_interface - Setup of an interface
974  * @iface: Pointer to interface data.
975  * Returns: 0 on success, -1 on failure
976  *
977  * Initializes the driver interface, validates the configuration,
978  * and sets driver parameters based on the configuration.
979  * Flushes old stations, sets the channel, encryption,
980  * beacons, and WDS links based on the configuration.
981  */
982 int hostapd_setup_interface(struct hostapd_iface *iface)
983 {
984         int ret;
985
986         ret = setup_interface(iface);
987         if (ret) {
988                 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
989                            iface->bss[0]->conf->iface);
990                 return -1;
991         }
992
993         return 0;
994 }
995
996
997 /**
998  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
999  * @hapd_iface: Pointer to interface data
1000  * @conf: Pointer to per-interface configuration
1001  * @bss: Pointer to per-BSS configuration for this BSS
1002  * Returns: Pointer to allocated BSS data
1003  *
1004  * This function is used to allocate per-BSS data structure. This data will be
1005  * freed after hostapd_cleanup() is called for it during interface
1006  * deinitialization.
1007  */
1008 struct hostapd_data *
1009 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1010                        struct hostapd_config *conf,
1011                        struct hostapd_bss_config *bss)
1012 {
1013         struct hostapd_data *hapd;
1014
1015         hapd = os_zalloc(sizeof(*hapd));
1016         if (hapd == NULL)
1017                 return NULL;
1018
1019         hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1020         hapd->iconf = conf;
1021         hapd->conf = bss;
1022         hapd->iface = hapd_iface;
1023         hapd->driver = hapd->iconf->driver;
1024         hapd->ctrl_sock = -1;
1025
1026         return hapd;
1027 }
1028
1029
1030 void hostapd_interface_deinit(struct hostapd_iface *iface)
1031 {
1032         size_t j;
1033
1034         if (iface == NULL)
1035                 return;
1036
1037         hostapd_cleanup_iface_pre(iface);
1038         for (j = 0; j < iface->num_bss; j++) {
1039                 struct hostapd_data *hapd = iface->bss[j];
1040                 hostapd_free_stas(hapd);
1041                 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1042                 hostapd_clear_wep(hapd);
1043                 hostapd_cleanup(hapd);
1044         }
1045 }
1046
1047
1048 void hostapd_interface_free(struct hostapd_iface *iface)
1049 {
1050         size_t j;
1051         for (j = 0; j < iface->num_bss; j++)
1052                 os_free(iface->bss[j]);
1053         hostapd_cleanup_iface(iface);
1054 }
1055
1056
1057 #ifdef HOSTAPD
1058
1059 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1060 {
1061         const struct wpa_driver_ops *driver;
1062         void *drv_priv;
1063         if (iface == NULL)
1064                 return;
1065         driver = iface->bss[0]->driver;
1066         drv_priv = iface->bss[0]->drv_priv;
1067         hostapd_interface_deinit(iface);
1068         if (driver && driver->hapd_deinit && drv_priv)
1069                 driver->hapd_deinit(drv_priv);
1070         hostapd_interface_free(iface);
1071 }
1072
1073
1074 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1075 {
1076         if (hapd_iface->bss[0]->drv_priv != NULL) {
1077                 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1078                            hapd_iface->conf->bss[0].iface);
1079                 return -1;
1080         }
1081
1082         wpa_printf(MSG_DEBUG, "Enable interface %s",
1083                    hapd_iface->conf->bss[0].iface);
1084
1085         if (hapd_iface->interfaces == NULL ||
1086             hapd_iface->interfaces->driver_init == NULL ||
1087             hapd_iface->interfaces->driver_init(hapd_iface) ||
1088             hostapd_setup_interface(hapd_iface)) {
1089                 hostapd_interface_deinit_free(hapd_iface);
1090                 return -1;
1091         }
1092         return 0;
1093 }
1094
1095
1096 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1097 {
1098         size_t j;
1099
1100         wpa_printf(MSG_DEBUG, "Reload interface %s",
1101                    hapd_iface->conf->bss[0].iface);
1102         for (j = 0; j < hapd_iface->num_bss; j++) {
1103                 hostapd_flush_old_stations(hapd_iface->bss[j],
1104                                            WLAN_REASON_PREV_AUTH_NOT_VALID);
1105
1106 #ifndef CONFIG_NO_RADIUS
1107                 /* TODO: update dynamic data based on changed configuration
1108                  * items (e.g., open/close sockets, etc.) */
1109                 radius_client_flush(hapd_iface->bss[j]->radius, 0);
1110 #endif  /* CONFIG_NO_RADIUS */
1111
1112                 hostapd_reload_bss(hapd_iface->bss[j]);
1113         }
1114         return 0;
1115 }
1116
1117
1118 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1119 {
1120         size_t j;
1121         struct hostapd_bss_config *bss;
1122         const struct wpa_driver_ops *driver;
1123         void *drv_priv;
1124
1125         if (hapd_iface == NULL)
1126                 return -1;
1127         bss = hapd_iface->bss[0]->conf;
1128         driver = hapd_iface->bss[0]->driver;
1129         drv_priv = hapd_iface->bss[0]->drv_priv;
1130
1131         /* whatever hostapd_interface_deinit does */
1132         for (j = 0; j < hapd_iface->num_bss; j++) {
1133                 struct hostapd_data *hapd = hapd_iface->bss[j];
1134                 hostapd_free_stas(hapd);
1135                 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1136                 hostapd_clear_wep(hapd);
1137                 hostapd_free_hapd_data(hapd);
1138         }
1139
1140         if (driver && driver->hapd_deinit && drv_priv) {
1141                 driver->hapd_deinit(drv_priv);
1142                 hapd_iface->bss[0]->drv_priv = NULL;
1143         }
1144
1145         /* From hostapd_cleanup_iface: These were initialized in
1146          * hostapd_setup_interface and hostapd_setup_interface_complete
1147          */
1148         hostapd_cleanup_iface_partial(hapd_iface);
1149         bss->wpa = 0;
1150         bss->wpa_key_mgmt = -1;
1151         bss->wpa_pairwise = -1;
1152
1153         wpa_printf(MSG_DEBUG, "Interface %s disabled", bss->iface);
1154         return 0;
1155 }
1156
1157
1158 static struct hostapd_iface *
1159 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1160 {
1161         struct hostapd_iface **iface, *hapd_iface;
1162
1163         iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1164                                  sizeof(struct hostapd_iface *));
1165         if (iface == NULL)
1166                 return NULL;
1167         interfaces->iface = iface;
1168         hapd_iface = interfaces->iface[interfaces->count] =
1169                 os_zalloc(sizeof(*hapd_iface));
1170         if (hapd_iface == NULL) {
1171                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1172                            "the interface", __func__);
1173                 return NULL;
1174         }
1175         interfaces->count++;
1176         hapd_iface->interfaces = interfaces;
1177
1178         return hapd_iface;
1179 }
1180
1181
1182 static struct hostapd_config *
1183 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1184                      const char *ctrl_iface)
1185 {
1186         struct hostapd_bss_config *bss;
1187         struct hostapd_config *conf;
1188
1189         /* Allocates memory for bss and conf */
1190         conf = hostapd_config_defaults();
1191         if (conf == NULL) {
1192                  wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1193                                 "configuration", __func__);
1194                 return NULL;
1195         }
1196
1197         conf->driver = wpa_drivers[0];
1198         if (conf->driver == NULL) {
1199                 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1200                 hostapd_config_free(conf);
1201                 return NULL;
1202         }
1203
1204         bss = conf->last_bss = conf->bss;
1205
1206         os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1207         bss->ctrl_interface = os_strdup(ctrl_iface);
1208         if (bss->ctrl_interface == NULL) {
1209                 hostapd_config_free(conf);
1210                 return NULL;
1211         }
1212
1213         /* Reading configuration file skipped, will be done in SET!
1214          * From reading the configuration till the end has to be done in
1215          * SET
1216          */
1217         return conf;
1218 }
1219
1220
1221 static struct hostapd_iface * hostapd_data_alloc(
1222         struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1223 {
1224         size_t i;
1225         struct hostapd_iface *hapd_iface =
1226                 interfaces->iface[interfaces->count - 1];
1227         struct hostapd_data *hapd;
1228
1229         hapd_iface->conf = conf;
1230         hapd_iface->num_bss = conf->num_bss;
1231
1232         hapd_iface->bss = os_zalloc(conf->num_bss *
1233                                     sizeof(struct hostapd_data *));
1234         if (hapd_iface->bss == NULL)
1235                 return NULL;
1236
1237         for (i = 0; i < conf->num_bss; i++) {
1238                 hapd = hapd_iface->bss[i] =
1239                         hostapd_alloc_bss_data(hapd_iface, conf,
1240                                                &conf->bss[i]);
1241                 if (hapd == NULL)
1242                         return NULL;
1243                 hapd->msg_ctx = hapd;
1244         }
1245
1246         hapd_iface->interfaces = interfaces;
1247
1248         return hapd_iface;
1249 }
1250
1251
1252 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1253 {
1254         struct hostapd_config *conf = NULL;
1255         struct hostapd_iface *hapd_iface = NULL;
1256         char *ptr;
1257         size_t i;
1258
1259         ptr = os_strchr(buf, ' ');
1260         if (ptr == NULL)
1261                 return -1;
1262         *ptr++ = '\0';
1263
1264         for (i = 0; i < interfaces->count; i++) {
1265                 if (!os_strcmp(interfaces->iface[i]->conf->bss[0].iface,
1266                                buf)) {
1267                         wpa_printf(MSG_INFO, "Cannot add interface - it "
1268                                    "already exists");
1269                         return -1;
1270                 }
1271         }
1272
1273         hapd_iface = hostapd_iface_alloc(interfaces);
1274         if (hapd_iface == NULL) {
1275                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1276                            "for interface", __func__);
1277                 goto fail;
1278         }
1279
1280         conf = hostapd_config_alloc(interfaces, buf, ptr);
1281         if (conf == NULL) {
1282                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1283                            "for configuration", __func__);
1284                 goto fail;
1285         }
1286
1287         hapd_iface = hostapd_data_alloc(interfaces, conf);
1288         if (hapd_iface == NULL) {
1289                 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1290                            "for hostapd", __func__);
1291                 goto fail;
1292         }
1293
1294         if (hapd_iface->interfaces &&
1295             hapd_iface->interfaces->ctrl_iface_init &&
1296             hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1297                 wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1298                            "interface", __func__);
1299                 goto fail;
1300         }
1301         wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0].iface);
1302
1303         return 0;
1304
1305 fail:
1306         if (conf)
1307                 hostapd_config_free(conf);
1308         if (hapd_iface) {
1309                 os_free(hapd_iface->bss[interfaces->count]);
1310                 os_free(hapd_iface);
1311         }
1312         return -1;
1313 }
1314
1315
1316 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1317 {
1318         struct hostapd_iface *hapd_iface;
1319         size_t i, k = 0;
1320
1321         for (i = 0; i < interfaces->count; i++) {
1322                 hapd_iface = interfaces->iface[i];
1323                 if (hapd_iface == NULL)
1324                         return -1;
1325                 if (!os_strcmp(hapd_iface->conf->bss[0].iface, buf)) {
1326                         wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1327                         hostapd_interface_deinit_free(hapd_iface);
1328                         k = i;
1329                         while (k < (interfaces->count - 1)) {
1330                                 interfaces->iface[k] =
1331                                         interfaces->iface[k + 1];
1332                                 k++;
1333                         }
1334                         interfaces->count--;
1335                         return 0;
1336                 }
1337         }
1338         return -1;
1339 }
1340
1341 #endif /* HOSTAPD */
1342
1343
1344 /**
1345  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1346  * @hapd: Pointer to BSS data
1347  * @sta: Pointer to the associated STA data
1348  * @reassoc: 1 to indicate this was a re-association; 0 = first association
1349  *
1350  * This function will be called whenever a station associates with the AP. It
1351  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1352  * from drv_callbacks.c based on driver events for drivers that take care of
1353  * management frames (IEEE 802.11 authentication and association) internally.
1354  */
1355 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1356                            int reassoc)
1357 {
1358         if (hapd->tkip_countermeasures) {
1359                 hostapd_drv_sta_deauth(hapd, sta->addr,
1360                                        WLAN_REASON_MICHAEL_MIC_FAILURE);
1361                 return;
1362         }
1363
1364         hostapd_prune_associations(hapd, sta->addr);
1365
1366         /* IEEE 802.11F (IAPP) */
1367         if (hapd->conf->ieee802_11f)
1368                 iapp_new_station(hapd->iapp, sta);
1369
1370 #ifdef CONFIG_P2P
1371         if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1372                 sta->no_p2p_set = 1;
1373                 hapd->num_sta_no_p2p++;
1374                 if (hapd->num_sta_no_p2p == 1)
1375                         hostapd_p2p_non_p2p_sta_connected(hapd);
1376         }
1377 #endif /* CONFIG_P2P */
1378
1379         /* Start accounting here, if IEEE 802.1X and WPA are not used.
1380          * IEEE 802.1X/WPA code will start accounting after the station has
1381          * been authorized. */
1382         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1383                 os_get_time(&sta->connected_time);
1384                 accounting_sta_start(hapd, sta);
1385         }
1386
1387         /* Start IEEE 802.1X authentication process for new stations */
1388         ieee802_1x_new_station(hapd, sta);
1389         if (reassoc) {
1390                 if (sta->auth_alg != WLAN_AUTH_FT &&
1391                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1392                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1393         } else
1394                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1395
1396         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1397                    "for " MACSTR " (%d seconds - ap_max_inactivity)",
1398                    __func__, MAC2STR(sta->addr),
1399                    hapd->conf->ap_max_inactivity);
1400         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1401         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1402                                ap_handle_timer, hapd, sta);
1403 }