Interworking: Add GAS server support for AP mode
[mech_eap.git] / src / ap / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2009, 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 "drivers/driver.h"
16 #include "hostapd.h"
17 #include "authsrv.h"
18 #include "sta_info.h"
19 #include "accounting.h"
20 #include "ap_list.h"
21 #include "beacon.h"
22 #include "iapp.h"
23 #include "ieee802_1x.h"
24 #include "ieee802_11_auth.h"
25 #include "vlan_init.h"
26 #include "wpa_auth.h"
27 #include "wps_hostapd.h"
28 #include "hw_features.h"
29 #include "wpa_auth_glue.h"
30 #include "ap_drv_ops.h"
31 #include "ap_config.h"
32 #include "p2p_hostapd.h"
33 #include "gas_serv.h"
34
35
36 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
37 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
38 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
39
40 extern int wpa_debug_level;
41
42
43 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
44                                int (*cb)(struct hostapd_iface *iface,
45                                          void *ctx), void *ctx)
46 {
47         size_t i;
48         int ret;
49
50         for (i = 0; i < interfaces->count; i++) {
51                 ret = cb(interfaces->iface[i], ctx);
52                 if (ret)
53                         return ret;
54         }
55
56         return 0;
57 }
58
59
60 static void hostapd_reload_bss(struct hostapd_data *hapd)
61 {
62 #ifndef CONFIG_NO_RADIUS
63         radius_client_reconfig(hapd->radius, hapd->conf->radius);
64 #endif /* CONFIG_NO_RADIUS */
65
66         if (hostapd_setup_wpa_psk(hapd->conf)) {
67                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
68                            "after reloading configuration");
69         }
70
71         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
72                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
73         else
74                 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
75
76         if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
77                 hostapd_setup_wpa(hapd);
78                 if (hapd->wpa_auth)
79                         wpa_init_keys(hapd->wpa_auth);
80         } else if (hapd->conf->wpa) {
81                 const u8 *wpa_ie;
82                 size_t wpa_ie_len;
83                 hostapd_reconfig_wpa(hapd);
84                 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
85                 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
86                         wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
87                                    "the kernel driver.");
88         } else if (hapd->wpa_auth) {
89                 wpa_deinit(hapd->wpa_auth);
90                 hapd->wpa_auth = NULL;
91                 hostapd_set_privacy(hapd, 0);
92                 hostapd_setup_encryption(hapd->conf->iface, hapd);
93                 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
94         }
95
96         ieee802_11_set_beacon(hapd);
97         hostapd_update_wps(hapd);
98
99         if (hapd->conf->ssid.ssid_set &&
100             hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
101                              hapd->conf->ssid.ssid_len)) {
102                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
103                 /* try to continue */
104         }
105         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
106 }
107
108
109 int hostapd_reload_config(struct hostapd_iface *iface)
110 {
111         struct hostapd_data *hapd = iface->bss[0];
112         struct hostapd_config *newconf, *oldconf;
113         size_t j;
114
115         if (iface->config_read_cb == NULL)
116                 return -1;
117         newconf = iface->config_read_cb(iface->config_fname);
118         if (newconf == NULL)
119                 return -1;
120
121         /*
122          * Deauthenticate all stations since the new configuration may not
123          * allow them to use the BSS anymore.
124          */
125         for (j = 0; j < iface->num_bss; j++) {
126                 hostapd_flush_old_stations(iface->bss[j]);
127                 hostapd_broadcast_wep_clear(iface->bss[j]);
128
129 #ifndef CONFIG_NO_RADIUS
130                 /* TODO: update dynamic data based on changed configuration
131                  * items (e.g., open/close sockets, etc.) */
132                 radius_client_flush(iface->bss[j]->radius, 0);
133 #endif /* CONFIG_NO_RADIUS */
134         }
135
136         oldconf = hapd->iconf;
137         iface->conf = newconf;
138
139         for (j = 0; j < iface->num_bss; j++) {
140                 hapd = iface->bss[j];
141                 hapd->iconf = newconf;
142                 hapd->conf = &newconf->bss[j];
143                 hostapd_reload_bss(hapd);
144         }
145
146         hostapd_config_free(oldconf);
147
148
149         return 0;
150 }
151
152
153 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
154                                               char *ifname)
155 {
156         int i;
157
158         for (i = 0; i < NUM_WEP_KEYS; i++) {
159                 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
160                                         0, NULL, 0, NULL, 0)) {
161                         wpa_printf(MSG_DEBUG, "Failed to clear default "
162                                    "encryption keys (ifname=%s keyidx=%d)",
163                                    ifname, i);
164                 }
165         }
166 #ifdef CONFIG_IEEE80211W
167         if (hapd->conf->ieee80211w) {
168                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
169                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
170                                                 NULL, i, 0, NULL,
171                                                 0, NULL, 0)) {
172                                 wpa_printf(MSG_DEBUG, "Failed to clear "
173                                            "default mgmt encryption keys "
174                                            "(ifname=%s keyidx=%d)", ifname, i);
175                         }
176                 }
177         }
178 #endif /* CONFIG_IEEE80211W */
179 }
180
181
182 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
183 {
184         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
185         return 0;
186 }
187
188
189 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
190 {
191         int errors = 0, idx;
192         struct hostapd_ssid *ssid = &hapd->conf->ssid;
193
194         idx = ssid->wep.idx;
195         if (ssid->wep.default_len &&
196             hostapd_drv_set_key(hapd->conf->iface,
197                                 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
198                                 1, NULL, 0, ssid->wep.key[idx],
199                                 ssid->wep.len[idx])) {
200                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
201                 errors++;
202         }
203
204         if (ssid->dyn_vlan_keys) {
205                 size_t i;
206                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
207                         const char *ifname;
208                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
209                         if (key == NULL)
210                                 continue;
211                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
212                                                             i);
213                         if (ifname == NULL)
214                                 continue;
215
216                         idx = key->idx;
217                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
218                                                 broadcast_ether_addr, idx, 1,
219                                                 NULL, 0, key->key[idx],
220                                                 key->len[idx])) {
221                                 wpa_printf(MSG_WARNING, "Could not set "
222                                            "dynamic VLAN WEP encryption.");
223                                 errors++;
224                         }
225                 }
226         }
227
228         return errors;
229 }
230
231
232 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
233 {
234         iapp_deinit(hapd->iapp);
235         hapd->iapp = NULL;
236         accounting_deinit(hapd);
237         hostapd_deinit_wpa(hapd);
238         vlan_deinit(hapd);
239         hostapd_acl_deinit(hapd);
240 #ifndef CONFIG_NO_RADIUS
241         radius_client_deinit(hapd->radius);
242         hapd->radius = NULL;
243 #endif /* CONFIG_NO_RADIUS */
244
245         hostapd_deinit_wps(hapd);
246
247         authsrv_deinit(hapd);
248
249         if (hapd->interface_added &&
250             hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
251                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
252                            hapd->conf->iface);
253         }
254
255         os_free(hapd->probereq_cb);
256         hapd->probereq_cb = NULL;
257
258 #ifdef CONFIG_P2P
259         wpabuf_free(hapd->p2p_beacon_ie);
260         hapd->p2p_beacon_ie = NULL;
261         wpabuf_free(hapd->p2p_probe_resp_ie);
262         hapd->p2p_probe_resp_ie = NULL;
263 #endif /* CONFIG_P2P */
264
265         wpabuf_free(hapd->time_adv);
266
267 #ifdef CONFIG_INTERWORKING
268         gas_serv_deinit(hapd);
269 #endif /* CONFIG_INTERWORKING */
270 }
271
272
273 /**
274  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
275  * @hapd: Pointer to BSS data
276  *
277  * This function is used to free all per-BSS data structures and resources.
278  * This gets called in a loop for each BSS between calls to
279  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
280  * is deinitialized. Most of the modules that are initialized in
281  * hostapd_setup_bss() are deinitialized here.
282  */
283 static void hostapd_cleanup(struct hostapd_data *hapd)
284 {
285         if (hapd->iface->ctrl_iface_deinit)
286                 hapd->iface->ctrl_iface_deinit(hapd);
287         hostapd_free_hapd_data(hapd);
288 }
289
290
291 /**
292  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
293  * @iface: Pointer to interface data
294  *
295  * This function is called before per-BSS data structures are deinitialized
296  * with hostapd_cleanup().
297  */
298 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
299 {
300 }
301
302
303 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
304 {
305         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
306         iface->hw_features = NULL;
307         os_free(iface->current_rates);
308         iface->current_rates = NULL;
309         os_free(iface->basic_rates);
310         iface->basic_rates = NULL;
311         ap_list_deinit(iface);
312 }
313
314
315 /**
316  * hostapd_cleanup_iface - Complete per-interface cleanup
317  * @iface: Pointer to interface data
318  *
319  * This function is called after per-BSS data structures are deinitialized
320  * with hostapd_cleanup().
321  */
322 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
323 {
324         hostapd_cleanup_iface_partial(iface);
325         hostapd_config_free(iface->conf);
326         iface->conf = NULL;
327
328         os_free(iface->config_fname);
329         os_free(iface->bss);
330         os_free(iface);
331 }
332
333
334 static void hostapd_clear_wep(struct hostapd_data *hapd)
335 {
336         if (hapd->drv_priv) {
337                 hostapd_set_privacy(hapd, 0);
338                 hostapd_broadcast_wep_clear(hapd);
339         }
340 }
341
342
343 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
344 {
345         int i;
346
347         hostapd_broadcast_wep_set(hapd);
348
349         if (hapd->conf->ssid.wep.default_len) {
350                 hostapd_set_privacy(hapd, 1);
351                 return 0;
352         }
353
354         /*
355          * When IEEE 802.1X is not enabled, the driver may need to know how to
356          * set authentication algorithms for static WEP.
357          */
358         hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
359
360         for (i = 0; i < 4; i++) {
361                 if (hapd->conf->ssid.wep.key[i] &&
362                     hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
363                                         i == hapd->conf->ssid.wep.idx, NULL, 0,
364                                         hapd->conf->ssid.wep.key[i],
365                                         hapd->conf->ssid.wep.len[i])) {
366                         wpa_printf(MSG_WARNING, "Could not set WEP "
367                                    "encryption.");
368                         return -1;
369                 }
370                 if (hapd->conf->ssid.wep.key[i] &&
371                     i == hapd->conf->ssid.wep.idx)
372                         hostapd_set_privacy(hapd, 1);
373         }
374
375         return 0;
376 }
377
378
379 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
380 {
381         int ret = 0;
382         u8 addr[ETH_ALEN];
383
384         if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
385                 return 0;
386
387         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
388         if (hostapd_flush(hapd)) {
389                 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
390                         "kernel driver");
391                 ret = -1;
392         }
393         wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
394         os_memset(addr, 0xff, ETH_ALEN);
395         hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
396         hostapd_free_stas(hapd);
397
398         return ret;
399 }
400
401
402 /**
403  * hostapd_validate_bssid_configuration - Validate BSSID configuration
404  * @iface: Pointer to interface data
405  * Returns: 0 on success, -1 on failure
406  *
407  * This function is used to validate that the configured BSSIDs are valid.
408  */
409 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
410 {
411         u8 mask[ETH_ALEN] = { 0 };
412         struct hostapd_data *hapd = iface->bss[0];
413         unsigned int i = iface->conf->num_bss, bits = 0, j;
414         int auto_addr = 0;
415
416         if (hostapd_drv_none(hapd))
417                 return 0;
418
419         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
420
421         /* Determine the bits necessary to cover the number of BSSIDs. */
422         for (i--; i; i >>= 1)
423                 bits++;
424
425         /* Determine the bits necessary to any configured BSSIDs,
426            if they are higher than the number of BSSIDs. */
427         for (j = 0; j < iface->conf->num_bss; j++) {
428                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
429                         if (j)
430                                 auto_addr++;
431                         continue;
432                 }
433
434                 for (i = 0; i < ETH_ALEN; i++) {
435                         mask[i] |=
436                                 iface->conf->bss[j].bssid[i] ^
437                                 hapd->own_addr[i];
438                 }
439         }
440
441         if (!auto_addr)
442                 goto skip_mask_ext;
443
444         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
445                 ;
446         j = 0;
447         if (i < ETH_ALEN) {
448                 j = (5 - i) * 8;
449
450                 while (mask[i] != 0) {
451                         mask[i] >>= 1;
452                         j++;
453                 }
454         }
455
456         if (bits < j)
457                 bits = j;
458
459         if (bits > 40) {
460                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
461                            bits);
462                 return -1;
463         }
464
465         os_memset(mask, 0xff, ETH_ALEN);
466         j = bits / 8;
467         for (i = 5; i > 5 - j; i--)
468                 mask[i] = 0;
469         j = bits % 8;
470         while (j--)
471                 mask[i] <<= 1;
472
473 skip_mask_ext:
474         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
475                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
476
477         if (!auto_addr)
478                 return 0;
479
480         for (i = 0; i < ETH_ALEN; i++) {
481                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
482                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
483                                    " for start address " MACSTR ".",
484                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
485                         wpa_printf(MSG_ERROR, "Start address must be the "
486                                    "first address in the block (i.e., addr "
487                                    "AND mask == addr).");
488                         return -1;
489                 }
490         }
491
492         return 0;
493 }
494
495
496 static int mac_in_conf(struct hostapd_config *conf, const void *a)
497 {
498         size_t i;
499
500         for (i = 0; i < conf->num_bss; i++) {
501                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
502                         return 1;
503                 }
504         }
505
506         return 0;
507 }
508
509
510
511
512 /**
513  * hostapd_setup_bss - Per-BSS setup (initialization)
514  * @hapd: Pointer to BSS data
515  * @first: Whether this BSS is the first BSS of an interface
516  *
517  * This function is used to initialize all per-BSS data structures and
518  * resources. This gets called in a loop for each BSS when an interface is
519  * initialized. Most of the modules that are initialized here will be
520  * deinitialized in hostapd_cleanup().
521  */
522 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
523 {
524         struct hostapd_bss_config *conf = hapd->conf;
525         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
526         int ssid_len, set_ssid;
527         char force_ifname[IFNAMSIZ];
528         u8 if_addr[ETH_ALEN];
529
530         if (!first) {
531                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
532                         /* Allocate the next available BSSID. */
533                         do {
534                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
535                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
536                 } else {
537                         /* Allocate the configured BSSID. */
538                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
539
540                         if (hostapd_mac_comp(hapd->own_addr,
541                                              hapd->iface->bss[0]->own_addr) ==
542                             0) {
543                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
544                                            "BSSID set to the MAC address of "
545                                            "the radio", hapd->conf->iface);
546                                 return -1;
547                         }
548                 }
549
550                 hapd->interface_added = 1;
551                 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
552                                    hapd->conf->iface, hapd->own_addr, hapd,
553                                    &hapd->drv_priv, force_ifname, if_addr,
554                                    hapd->conf->bridge[0] ? hapd->conf->bridge :
555                                    NULL)) {
556                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
557                                    MACSTR ")", MAC2STR(hapd->own_addr));
558                         return -1;
559                 }
560         }
561
562         if (conf->wmm_enabled < 0)
563                 conf->wmm_enabled = hapd->iconf->ieee80211n;
564
565         hostapd_flush_old_stations(hapd);
566         hostapd_set_privacy(hapd, 0);
567
568         hostapd_broadcast_wep_clear(hapd);
569         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
570                 return -1;
571
572         /*
573          * Fetch the SSID from the system and use it or,
574          * if one was specified in the config file, verify they
575          * match.
576          */
577         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
578         if (ssid_len < 0) {
579                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
580                 return -1;
581         }
582         if (conf->ssid.ssid_set) {
583                 /*
584                  * If SSID is specified in the config file and it differs
585                  * from what is being used then force installation of the
586                  * new SSID.
587                  */
588                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
589                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
590         } else {
591                 /*
592                  * No SSID in the config file; just use the one we got
593                  * from the system.
594                  */
595                 set_ssid = 0;
596                 conf->ssid.ssid_len = ssid_len;
597                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
598                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
599         }
600
601         if (!hostapd_drv_none(hapd)) {
602                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
603                            " and ssid '%s'",
604                            hapd->conf->iface, MAC2STR(hapd->own_addr),
605                            hapd->conf->ssid.ssid);
606         }
607
608         if (hostapd_setup_wpa_psk(conf)) {
609                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
610                 return -1;
611         }
612
613         /* Set SSID for the kernel driver (to be used in beacon and probe
614          * response frames) */
615         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
616                                          conf->ssid.ssid_len)) {
617                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
618                 return -1;
619         }
620
621         if (wpa_debug_level == MSG_MSGDUMP)
622                 conf->radius->msg_dumps = 1;
623 #ifndef CONFIG_NO_RADIUS
624         hapd->radius = radius_client_init(hapd, conf->radius);
625         if (hapd->radius == NULL) {
626                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
627                 return -1;
628         }
629 #endif /* CONFIG_NO_RADIUS */
630
631         if (hostapd_acl_init(hapd)) {
632                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
633                 return -1;
634         }
635         if (hostapd_init_wps(hapd, conf))
636                 return -1;
637
638         if (authsrv_init(hapd) < 0)
639                 return -1;
640
641         if (ieee802_1x_init(hapd)) {
642                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
643                 return -1;
644         }
645
646         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
647                 return -1;
648
649         if (accounting_init(hapd)) {
650                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
651                 return -1;
652         }
653
654         if (hapd->conf->ieee802_11f &&
655             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
656                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
657                            "failed.");
658                 return -1;
659         }
660
661 #ifdef CONFIG_INTERWORKING
662         if (gas_serv_init(hapd)) {
663                 wpa_printf(MSG_ERROR, "GAS server initialization failed");
664                 return -1;
665         }
666 #endif /* CONFIG_INTERWORKING */
667
668         if (hapd->iface->ctrl_iface_init &&
669             hapd->iface->ctrl_iface_init(hapd)) {
670                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
671                 return -1;
672         }
673
674         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
675                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
676                 return -1;
677         }
678
679         ieee802_11_set_beacon(hapd);
680
681         if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
682                 return -1;
683
684         if (hapd->driver && hapd->driver->set_operstate)
685                 hapd->driver->set_operstate(hapd->drv_priv, 1);
686
687         return 0;
688 }
689
690
691 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
692 {
693         struct hostapd_data *hapd = iface->bss[0];
694         int i;
695         struct hostapd_tx_queue_params *p;
696
697         for (i = 0; i < NUM_TX_QUEUES; i++) {
698                 p = &iface->conf->tx_queue[i];
699
700                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
701                                                 p->cwmax, p->burst)) {
702                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
703                                    "parameters for queue %d.", i);
704                         /* Continue anyway */
705                 }
706         }
707 }
708
709
710 static int setup_interface(struct hostapd_iface *iface)
711 {
712         struct hostapd_data *hapd = iface->bss[0];
713         size_t i;
714         char country[4];
715
716         /*
717          * Make sure that all BSSes get configured with a pointer to the same
718          * driver interface.
719          */
720         for (i = 1; i < iface->num_bss; i++) {
721                 iface->bss[i]->driver = hapd->driver;
722                 iface->bss[i]->drv_priv = hapd->drv_priv;
723         }
724
725         if (hostapd_validate_bssid_configuration(iface))
726                 return -1;
727
728         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
729                 os_memcpy(country, hapd->iconf->country, 3);
730                 country[3] = '\0';
731                 if (hostapd_set_country(hapd, country) < 0) {
732                         wpa_printf(MSG_ERROR, "Failed to set country code");
733                         return -1;
734                 }
735         }
736
737         if (hostapd_get_hw_features(iface)) {
738                 /* Not all drivers support this yet, so continue without hw
739                  * feature data. */
740         } else {
741                 int ret = hostapd_select_hw_mode(iface);
742                 if (ret < 0) {
743                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
744                                    "channel. (%d)", ret);
745                         return -1;
746                 }
747                 ret = hostapd_check_ht_capab(iface);
748                 if (ret < 0)
749                         return -1;
750                 if (ret == 1) {
751                         wpa_printf(MSG_DEBUG, "Interface initialization will "
752                                    "be completed in a callback");
753                         return 0;
754                 }
755         }
756         return hostapd_setup_interface_complete(iface, 0);
757 }
758
759
760 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
761 {
762         struct hostapd_data *hapd = iface->bss[0];
763         size_t j;
764         u8 *prev_addr;
765
766         if (err) {
767                 wpa_printf(MSG_ERROR, "Interface initialization failed");
768                 eloop_terminate();
769                 return -1;
770         }
771
772         wpa_printf(MSG_DEBUG, "Completing interface initialization");
773         if (hapd->iconf->channel) {
774                 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
775                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
776                            "Frequency: %d MHz",
777                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
778                            hapd->iconf->channel, iface->freq);
779
780                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
781                                      hapd->iconf->channel,
782                                      hapd->iconf->ieee80211n,
783                                      hapd->iconf->secondary_channel)) {
784                         wpa_printf(MSG_ERROR, "Could not set channel for "
785                                    "kernel driver");
786                         return -1;
787                 }
788         }
789
790         if (iface->current_mode) {
791                 if (hostapd_prepare_rates(iface, iface->current_mode)) {
792                         wpa_printf(MSG_ERROR, "Failed to prepare rates "
793                                    "table.");
794                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
795                                        HOSTAPD_LEVEL_WARNING,
796                                        "Failed to prepare rates table.");
797                         return -1;
798                 }
799         }
800
801         if (hapd->iconf->rts_threshold > -1 &&
802             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
803                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
804                            "kernel driver");
805                 return -1;
806         }
807
808         if (hapd->iconf->fragm_threshold > -1 &&
809             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
810                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
811                            "for kernel driver");
812                 return -1;
813         }
814
815         prev_addr = hapd->own_addr;
816
817         for (j = 0; j < iface->num_bss; j++) {
818                 hapd = iface->bss[j];
819                 if (j)
820                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
821                 if (hostapd_setup_bss(hapd, j == 0))
822                         return -1;
823                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
824                         prev_addr = hapd->own_addr;
825         }
826
827         hostapd_tx_queue_params(iface);
828
829         ap_list_init(iface);
830
831         if (hostapd_driver_commit(hapd) < 0) {
832                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
833                            "configuration", __func__);
834                 return -1;
835         }
836
837         /*
838          * WPS UPnP module can be initialized only when the "upnp_iface" is up.
839          * If "interface" and "upnp_iface" are the same (e.g., non-bridge
840          * mode), the interface is up only after driver_commit, so initialize
841          * WPS after driver_commit.
842          */
843         for (j = 0; j < iface->num_bss; j++) {
844                 if (hostapd_init_wps_complete(iface->bss[j]))
845                         return -1;
846         }
847
848         if (hapd->setup_complete_cb)
849                 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
850
851         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
852                    iface->bss[0]->conf->iface);
853
854         return 0;
855 }
856
857
858 /**
859  * hostapd_setup_interface - Setup of an interface
860  * @iface: Pointer to interface data.
861  * Returns: 0 on success, -1 on failure
862  *
863  * Initializes the driver interface, validates the configuration,
864  * and sets driver parameters based on the configuration.
865  * Flushes old stations, sets the channel, encryption,
866  * beacons, and WDS links based on the configuration.
867  */
868 int hostapd_setup_interface(struct hostapd_iface *iface)
869 {
870         int ret;
871
872         ret = setup_interface(iface);
873         if (ret) {
874                 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
875                            iface->bss[0]->conf->iface);
876                 return -1;
877         }
878
879         return 0;
880 }
881
882
883 /**
884  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
885  * @hapd_iface: Pointer to interface data
886  * @conf: Pointer to per-interface configuration
887  * @bss: Pointer to per-BSS configuration for this BSS
888  * Returns: Pointer to allocated BSS data
889  *
890  * This function is used to allocate per-BSS data structure. This data will be
891  * freed after hostapd_cleanup() is called for it during interface
892  * deinitialization.
893  */
894 struct hostapd_data *
895 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
896                        struct hostapd_config *conf,
897                        struct hostapd_bss_config *bss)
898 {
899         struct hostapd_data *hapd;
900
901         hapd = os_zalloc(sizeof(*hapd));
902         if (hapd == NULL)
903                 return NULL;
904
905         hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
906         hapd->iconf = conf;
907         hapd->conf = bss;
908         hapd->iface = hapd_iface;
909         hapd->driver = hapd->iconf->driver;
910         hapd->ctrl_sock = -1;
911
912         return hapd;
913 }
914
915
916 void hostapd_interface_deinit(struct hostapd_iface *iface)
917 {
918         size_t j;
919
920         if (iface == NULL)
921                 return;
922
923         hostapd_cleanup_iface_pre(iface);
924         for (j = 0; j < iface->num_bss; j++) {
925                 struct hostapd_data *hapd = iface->bss[j];
926                 hostapd_free_stas(hapd);
927                 hostapd_flush_old_stations(hapd);
928                 hostapd_clear_wep(hapd);
929                 hostapd_cleanup(hapd);
930         }
931 }
932
933
934 void hostapd_interface_free(struct hostapd_iface *iface)
935 {
936         size_t j;
937         for (j = 0; j < iface->num_bss; j++)
938                 os_free(iface->bss[j]);
939         hostapd_cleanup_iface(iface);
940 }
941
942
943 /**
944  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
945  * @hapd: Pointer to BSS data
946  * @sta: Pointer to the associated STA data
947  * @reassoc: 1 to indicate this was a re-association; 0 = first association
948  *
949  * This function will be called whenever a station associates with the AP. It
950  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
951  * from drv_callbacks.c based on driver events for drivers that take care of
952  * management frames (IEEE 802.11 authentication and association) internally.
953  */
954 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
955                            int reassoc)
956 {
957         if (hapd->tkip_countermeasures) {
958                 hostapd_drv_sta_deauth(hapd, sta->addr,
959                                        WLAN_REASON_MICHAEL_MIC_FAILURE);
960                 return;
961         }
962
963         hostapd_prune_associations(hapd, sta->addr);
964
965         /* IEEE 802.11F (IAPP) */
966         if (hapd->conf->ieee802_11f)
967                 iapp_new_station(hapd->iapp, sta);
968
969 #ifdef CONFIG_P2P
970         if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
971                 sta->no_p2p_set = 1;
972                 hapd->num_sta_no_p2p++;
973                 if (hapd->num_sta_no_p2p == 1)
974                         hostapd_p2p_non_p2p_sta_connected(hapd);
975         }
976 #endif /* CONFIG_P2P */
977
978         /* Start accounting here, if IEEE 802.1X and WPA are not used.
979          * IEEE 802.1X/WPA code will start accounting after the station has
980          * been authorized. */
981         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
982                 accounting_sta_start(hapd, sta);
983
984         /* Start IEEE 802.1X authentication process for new stations */
985         ieee802_1x_new_station(hapd, sta);
986         if (reassoc) {
987                 if (sta->auth_alg != WLAN_AUTH_FT &&
988                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
989                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
990         } else
991                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
992 }