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