Remove direct driver calls from vlan_init.c
[libeap.git] / hostapd / beacon.c
index 05dcbb5..a796cc3 100644 (file)
@@ -2,8 +2,7 @@
  * hostapd / IEEE 802.11 Management: Beacon and Probe Request/Response
  * Copyright (c) 2002-2004, Instant802 Networks, Inc.
  * Copyright (c) 2005-2006, Devicescape Software, Inc.
- * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
- * Copyright (c) 2007-2008, Intel Corporation
+ * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 
 #ifndef CONFIG_NATIVE_WINDOWS
 
+#include "common.h"
 #include "hostapd.h"
 #include "ieee802_11.h"
 #include "wpa.h"
 #include "wme.h"
 #include "beacon.h"
 #include "hw_features.h"
-#include "driver.h"
+#include "drivers/driver.h"
+#include "config.h"
 #include "sta_info.h"
-#include "ieee802_11h.h"
 
 
 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
@@ -96,13 +96,36 @@ static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
 }
 
 
+static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
+                                   struct hostapd_channel_data *start,
+                                   struct hostapd_channel_data *prev)
+{
+       if (end - pos < 3)
+               return pos;
+
+       /* first channel number */
+       *pos++ = start->chan;
+       /* number of channels */
+       *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
+       /* maximum transmit power level */
+       *pos++ = start->max_tx_power;
+
+       return pos;
+}
+
+
 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
                                int max_len)
 {
        u8 *pos = eid;
-
-       if ((!hapd->iconf->ieee80211d && !hapd->iface->dfs_enable) ||
-           max_len < 6)
+       u8 *end = eid + max_len;
+       int i;
+       struct hostapd_hw_modes *mode;
+       struct hostapd_channel_data *start, *prev;
+       int chan_spacing = 1;
+
+       if (!hapd->iconf->ieee80211d || max_len < 6 ||
+           hapd->iface->current_mode == NULL)
                return eid;
 
        *pos++ = WLAN_EID_COUNTRY;
@@ -110,51 +133,46 @@ static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
        os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
        pos += 3;
 
-       if ((pos - eid) & 1)
-               *pos++ = 0; /* pad for 16-bit alignment */
-
-       eid[1] = (pos - eid) - 2;
-
-       return pos;
-}
-
-
-static u8 * hostapd_eid_power_constraint(struct hostapd_data *hapd, u8 *eid)
+       mode = hapd->iface->current_mode;
+       if (mode->mode == HOSTAPD_MODE_IEEE80211A)
+               chan_spacing = 4;
+
+       start = prev = NULL;
+       for (i = 0; i < mode->num_channels; i++) {
+               struct hostapd_channel_data *chan = &mode->channels[i];
+               if (chan->flag & HOSTAPD_CHAN_DISABLED)
+                       continue;
+               if (start && prev &&
+                   prev->chan + chan_spacing == chan->chan &&
+                   start->max_tx_power == chan->max_tx_power) {
+                       prev = chan;
+                       continue; /* can use same entry */
+               }
 
-{
-       if (!hapd->iface->dfs_enable)
-               return eid;
-       *eid++ = WLAN_EID_PWR_CONSTRAINT;
-       *eid++ = 1;
-       *eid++ = hapd->iface->pwr_const;
-       return eid;
-}
+               if (start) {
+                       pos = hostapd_eid_country_add(pos, end, chan_spacing,
+                                                     start, prev);
+                       start = NULL;
+               }
 
+               /* Start new group */
+               start = prev = chan;
+       }
 
-static u8 * hostapd_eid_tpc_report(struct hostapd_data *hapd, u8 *eid)
+       if (start) {
+               pos = hostapd_eid_country_add(pos, end, chan_spacing,
+                                             start, prev);
+       }
 
-{
-       if (!hapd->iface->dfs_enable)
-               return eid;
-       *eid++ = WLAN_EID_TPC_REPORT;
-       *eid++ = 2;
-       *eid++ = hapd->iface->tx_power; /* TX POWER */
-       *eid++ = 0; /* Link Margin */
-       return eid;
-}
+       if ((pos - eid) & 1) {
+               if (end - pos < 1)
+                       return eid;
+               *pos++ = 0; /* pad for 16-bit alignment */
+       }
 
-static u8 * hostapd_eid_channel_switch(struct hostapd_data *hapd, u8 *eid)
+       eid[1] = (pos - eid) - 2;
 
-{
-       if (!hapd->iface->dfs_enable || !hapd->iface->channel_switch)
-               return eid;
-       *eid++ = WLAN_EID_CHANNEL_SWITCH;
-       *eid++ = 3;
-       *eid++ = CHAN_SWITCH_MODE_QUIET;
-       *eid++ = hapd->iface->channel_switch; /* New channel */
-       /* 0 - very soon; 1 - before next TBTT; num - after num beacons */
-       *eid++ = 0;
-       return eid;
+       return pos;
 }
 
 
@@ -173,24 +191,29 @@ static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
 }
 
 
-void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
-                     size_t len)
+void handle_probe_req(struct hostapd_data *hapd,
+                     const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct ieee80211_mgmt *resp;
        struct ieee802_11_elems elems;
        char *ssid;
-       u8 *pos, *epos, *ie;
+       u8 *pos, *epos;
+       const u8 *ie;
        size_t ssid_len, ie_len;
        struct sta_info *sta = NULL;
+       size_t i;
 
        ie = mgmt->u.probe_req.variable;
        ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
 
+       for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
+               hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
+                                       mgmt->sa, ie, ie_len);
+
        if (!hapd->iconf->send_probe_response)
                return;
 
-       if (ieee802_11_parse_elems(hapd, ie, ie_len, &elems, 0) == ParseFailed)
-       {
+       if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
                wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
                           MAC2STR(mgmt->sa));
                return;
@@ -271,9 +294,6 @@ void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
 
        pos = hostapd_eid_country(hapd, pos, epos - pos);
 
-       pos = hostapd_eid_power_constraint(hapd, pos);
-       pos = hostapd_eid_tpc_report(hapd, pos);
-
        /* ERP Information element */
        pos = hostapd_eid_erp_info(hapd, pos);
 
@@ -282,18 +302,23 @@ void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
 
        pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
 
-       /* Wi-Fi Wireless Multimedia Extensions */
-       if (hapd->conf->wme_enabled)
-               pos = hostapd_eid_wme(hapd, pos);
+       /* Wi-Fi Alliance WMM */
+       pos = hostapd_eid_wmm(hapd, pos);
 
 #ifdef CONFIG_IEEE80211N
-       if (hapd->conf->ieee80211n) {
-               pos = hostapd_eid_ht_capabilities_info(hapd, pos);
-               pos = hostapd_eid_ht_operation(hapd, pos);
-       }
+       pos = hostapd_eid_ht_capabilities(hapd, pos);
+       pos = hostapd_eid_ht_operation(hapd, pos);
 #endif /* CONFIG_IEEE80211N */
 
-       if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
+#ifdef CONFIG_WPS
+       if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
+               os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
+                         wpabuf_len(hapd->wps_probe_resp_ie));
+               pos += wpabuf_len(hapd->wps_probe_resp_ie);
+       }
+#endif /* CONFIG_WPS */
+
+       if (hapd->drv.send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
                perror("handle_probe_req: send");
 
        os_free(resp);
@@ -308,11 +333,8 @@ void ieee802_11_set_beacon(struct hostapd_data *hapd)
 {
        struct ieee80211_mgmt *head;
        u8 *pos, *tail, *tailpos;
-       int preamble;
        u16 capab_info;
        size_t head_len, tail_len;
-       int cts_protection = ((ieee802_11_erp_info(hapd) &
-                             ERP_INFO_USE_PROTECTION) ? 1 : 0);
 
 #define BEACON_HEAD_BUF_SIZE 256
 #define BEACON_TAIL_BUF_SIZE 512
@@ -367,10 +389,6 @@ void ieee802_11_set_beacon(struct hostapd_data *hapd)
        tailpos = hostapd_eid_country(hapd, tailpos,
                                      tail + BEACON_TAIL_BUF_SIZE - tailpos);
 
-       tailpos = hostapd_eid_power_constraint(hapd, tailpos);
-       tailpos = hostapd_eid_channel_switch(hapd, tailpos);
-       tailpos = hostapd_eid_tpc_report(hapd, tailpos);
-
        /* ERP Information element */
        tailpos = hostapd_eid_erp_info(hapd, tailpos);
 
@@ -380,59 +398,36 @@ void ieee802_11_set_beacon(struct hostapd_data *hapd)
        tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
                                  tailpos, NULL);
 
-       /* Wi-Fi Wireless Multimedia Extensions */
-       if (hapd->conf->wme_enabled)
-               tailpos = hostapd_eid_wme(hapd, tailpos);
+       /* Wi-Fi Alliance WMM */
+       tailpos = hostapd_eid_wmm(hapd, tailpos);
 
 #ifdef CONFIG_IEEE80211N
-       if (hapd->conf->ieee80211n) {
-               u8 *start;
-               start = tailpos;
-               tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
-               if (hostapd_set_ht_capability(hapd->conf->iface, hapd,
-                                             start + 2)) {
-                       wpa_printf(MSG_ERROR, "Could not set HT capabilities "
-                                  "for kernel driver");
-               }
+       tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
+       tailpos = hostapd_eid_ht_operation(hapd, tailpos);
+#endif /* CONFIG_IEEE80211N */
 
-               start = tailpos;
-               tailpos = hostapd_eid_ht_operation(hapd, tailpos);
-               if (hostapd_set_ht_operation(hapd->conf->iface, hapd,
-                                            start + 2))
-                       wpa_printf(MSG_ERROR, "Could not set HT operation for "
-                                  "kernel driver");
+#ifdef CONFIG_WPS
+       if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
+               os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
+                         wpabuf_len(hapd->wps_beacon_ie));
+               tailpos += wpabuf_len(hapd->wps_beacon_ie);
        }
-#endif /* CONFIG_IEEE80211N */
+#endif /* CONFIG_WPS */
 
        tail_len = tailpos > tail ? tailpos - tail : 0;
 
-       if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
-                              tail, tail_len))
-               wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
+       if (hapd->drv.set_beacon(hapd->conf->iface, hapd,
+                                (u8 *) head, head_len,
+                                tail, tail_len, hapd->conf->dtim_period,
+                                hapd->iconf->beacon_int))
+               wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
+                          "period");
 
        os_free(tail);
        os_free(head);
 
-       if (hostapd_set_cts_protect(hapd, cts_protection))
-               wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
-                          "driver");
-
-       if (hapd->iface->current_mode &&
-           hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
-           hostapd_set_short_slot_time(hapd,
-                                       hapd->iface->num_sta_no_short_slot_time
-                                       > 0 ? 0 : 1))
-               wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
-                          "in kernel driver");
-
-       if (hapd->iface->num_sta_no_short_preamble == 0 &&
-           hapd->iconf->preamble == SHORT_PREAMBLE)
-               preamble = SHORT_PREAMBLE;
-       else
-               preamble = LONG_PREAMBLE;
-       if (hostapd_set_preamble(hapd, preamble))
-               wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
-                          "driver");
+       hapd->drv.set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
+                                         ERP_INFO_USE_PROTECTION));
 }