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