beacon.c

Go to the documentation of this file.
00001 
00019 #include "includes.h"
00020 
00021 #ifndef CONFIG_NATIVE_WINDOWS
00022 
00023 #include "hostapd.h"
00024 #include "ieee802_11.h"
00025 #include "wpa.h"
00026 #include "wme.h"
00027 #include "beacon.h"
00028 #include "hw_features.h"
00029 #include "driver_i.h"
00030 #include "sta_info.h"
00031 #include "wps_hostapd.h"
00032 
00033 
00034 static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
00035 {
00036         u8 erp = 0;
00037 
00038         if (hapd->iface->current_mode == NULL ||
00039             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
00040                 return 0;
00041 
00042         switch (hapd->iconf->cts_protection_type) {
00043         case CTS_PROTECTION_FORCE_ENABLED:
00044                 erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
00045                 break;
00046         case CTS_PROTECTION_FORCE_DISABLED:
00047                 erp = 0;
00048                 break;
00049         case CTS_PROTECTION_AUTOMATIC:
00050                 if (hapd->iface->olbc)
00051                         erp |= ERP_INFO_USE_PROTECTION;
00052                 /* continue */
00053         case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
00054                 if (hapd->iface->num_sta_non_erp > 0) {
00055                         erp |= ERP_INFO_NON_ERP_PRESENT |
00056                                 ERP_INFO_USE_PROTECTION;
00057                 }
00058                 break;
00059         }
00060         if (hapd->iface->num_sta_no_short_preamble > 0)
00061                 erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
00062 
00063         return erp;
00064 }
00065 
00066 
00067 static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
00068 {
00069         *eid++ = WLAN_EID_DS_PARAMS;
00070         *eid++ = 1;
00071         *eid++ = hapd->iconf->channel;
00072         return eid;
00073 }
00074 
00075 
00076 static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
00077 {
00078         if (hapd->iface->current_mode == NULL ||
00079             hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
00080                 return eid;
00081 
00082         /* Set NonERP_present and use_protection bits if there
00083          * are any associated NonERP stations. */
00084         /* TODO: use_protection bit can be set to zero even if
00085          * there are NonERP stations present. This optimization
00086          * might be useful if NonERP stations are "quiet".
00087          * See 802.11g/D6 E-1 for recommended practice.
00088          * In addition, Non ERP present might be set, if AP detects Non ERP
00089          * operation on other APs. */
00090 
00091         /* Add ERP Information element */
00092         *eid++ = WLAN_EID_ERP_INFO;
00093         *eid++ = 1;
00094         *eid++ = ieee802_11_erp_info(hapd);
00095 
00096         return eid;
00097 }
00098 
00099 
00100 static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
00101                                     struct hostapd_channel_data *start,
00102                                     struct hostapd_channel_data *prev)
00103 {
00104         if (end - pos < 3)
00105                 return pos;
00106 
00107         /* first channel number */
00108         *pos++ = start->chan;
00109         /* number of channels */
00110         *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
00111         /* maximum transmit power level */
00112         *pos++ = start->max_tx_power;
00113 
00114         return pos;
00115 }
00116 
00117 
00118 static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
00119                                 int max_len)
00120 {
00121         u8 *pos = eid;
00122         u8 *end = eid + max_len;
00123         int i;
00124         struct hostapd_hw_modes *mode;
00125         struct hostapd_channel_data *start, *prev;
00126         int chan_spacing = 1;
00127 
00128         if (!hapd->iconf->ieee80211d || max_len < 6 ||
00129             hapd->iface->current_mode == NULL)
00130                 return eid;
00131 
00132         *pos++ = WLAN_EID_COUNTRY;
00133         pos++; /* length will be set later */
00134         os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
00135         pos += 3;
00136 
00137         mode = hapd->iface->current_mode;
00138         if (mode->mode == HOSTAPD_MODE_IEEE80211A)
00139                 chan_spacing = 4;
00140 
00141         start = prev = NULL;
00142         for (i = 0; i < mode->num_channels; i++) {
00143                 struct hostapd_channel_data *chan = &mode->channels[i];
00144                 if (chan->flag & HOSTAPD_CHAN_DISABLED)
00145                         continue;
00146                 if (start && prev &&
00147                     prev->chan + chan_spacing == chan->chan &&
00148                     start->max_tx_power == chan->max_tx_power) {
00149                         prev = chan;
00150                         continue; /* can use same entry */
00151                 }
00152 
00153                 if (start) {
00154                         pos = hostapd_eid_country_add(pos, end, chan_spacing,
00155                                                       start, prev);
00156                         start = NULL;
00157                 }
00158 
00159                 /* Start new group */
00160                 start = prev = chan;
00161         }
00162 
00163         if (start) {
00164                 pos = hostapd_eid_country_add(pos, end, chan_spacing,
00165                                               start, prev);
00166         }
00167 
00168         if ((pos - eid) & 1) {
00169                 if (end - pos < 1)
00170                         return eid;
00171                 *pos++ = 0; /* pad for 16-bit alignment */
00172         }
00173 
00174         eid[1] = (pos - eid) - 2;
00175 
00176         return pos;
00177 }
00178 
00179 
00180 static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
00181                             struct sta_info *sta)
00182 {
00183         const u8 *ie;
00184         size_t ielen;
00185 
00186         ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
00187         if (ie == NULL || ielen > len)
00188                 return eid;
00189 
00190         os_memcpy(eid, ie, ielen);
00191         return eid + ielen;
00192 }
00193 
00194 
00195 void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
00196                       size_t len)
00197 {
00198         struct ieee80211_mgmt *resp;
00199         struct ieee802_11_elems elems;
00200         char *ssid;
00201         u8 *pos, *epos, *ie;
00202         size_t ssid_len, ie_len;
00203         struct sta_info *sta = NULL;
00204 
00205         ie = mgmt->u.probe_req.variable;
00206         ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
00207 
00208         hostapd_probe_req_rx(hapd, mgmt->sa, ie, ie_len);
00209 
00210         if (!hapd->iconf->send_probe_response)
00211                 return;
00212 
00213         if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
00214                 wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
00215                            MAC2STR(mgmt->sa));
00216                 return;
00217         }
00218 
00219         ssid = NULL;
00220         ssid_len = 0;
00221 
00222         if ((!elems.ssid || !elems.supp_rates)) {
00223                 wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
00224                            "without SSID or supported rates element",
00225                            MAC2STR(mgmt->sa));
00226                 return;
00227         }
00228 
00229         if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
00230                 wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
00231                            "broadcast SSID ignored", MAC2STR(mgmt->sa));
00232                 return;
00233         }
00234 
00235         sta = ap_get_sta(hapd, mgmt->sa);
00236 
00237         if (elems.ssid_len == 0 ||
00238             (elems.ssid_len == hapd->conf->ssid.ssid_len &&
00239              os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
00240              0)) {
00241                 ssid = hapd->conf->ssid.ssid;
00242                 ssid_len = hapd->conf->ssid.ssid_len;
00243                 if (sta)
00244                         sta->ssid_probe = &hapd->conf->ssid;
00245         }
00246 
00247         if (!ssid) {
00248                 if (!(mgmt->da[0] & 0x01)) {
00249                         char ssid_txt[33];
00250                         ieee802_11_print_ssid(ssid_txt, elems.ssid,
00251                                               elems.ssid_len);
00252                         wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
00253                                    " for foreign SSID '%s'",
00254                                    MAC2STR(mgmt->sa), ssid_txt);
00255                 }
00256                 return;
00257         }
00258 
00259         /* TODO: verify that supp_rates contains at least one matching rate
00260          * with AP configuration */
00261 #define MAX_PROBERESP_LEN 768
00262         resp = os_zalloc(MAX_PROBERESP_LEN);
00263         if (resp == NULL)
00264                 return;
00265         epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
00266 
00267         resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
00268                                            WLAN_FC_STYPE_PROBE_RESP);
00269         os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
00270         os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
00271 
00272         os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
00273         resp->u.probe_resp.beacon_int =
00274                 host_to_le16(hapd->iconf->beacon_int);
00275 
00276         /* hardware or low-level driver will setup seq_ctrl and timestamp */
00277         resp->u.probe_resp.capab_info =
00278                 host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
00279 
00280         pos = resp->u.probe_resp.variable;
00281         *pos++ = WLAN_EID_SSID;
00282         *pos++ = ssid_len;
00283         os_memcpy(pos, ssid, ssid_len);
00284         pos += ssid_len;
00285 
00286         /* Supported rates */
00287         pos = hostapd_eid_supp_rates(hapd, pos);
00288 
00289         /* DS Params */
00290         pos = hostapd_eid_ds_params(hapd, pos);
00291 
00292         pos = hostapd_eid_country(hapd, pos, epos - pos);
00293 
00294         /* ERP Information element */
00295         pos = hostapd_eid_erp_info(hapd, pos);
00296 
00297         /* Extended supported rates */
00298         pos = hostapd_eid_ext_supp_rates(hapd, pos);
00299 
00300         pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
00301 
00302         /* Wi-Fi Alliance WMM */
00303         pos = hostapd_eid_wmm(hapd, pos);
00304 
00305         pos = hostapd_eid_ht_capabilities_info(hapd, pos);
00306         pos = hostapd_eid_ht_operation(hapd, pos);
00307 
00308 #ifdef CONFIG_WPS
00309         if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
00310                 os_memcpy(pos, hapd->wps_probe_resp_ie,
00311                           hapd->wps_probe_resp_ie_len);
00312                 pos += hapd->wps_probe_resp_ie_len;
00313         }
00314 #endif /* CONFIG_WPS */
00315 
00316         if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
00317                 perror("handle_probe_req: send");
00318 
00319         os_free(resp);
00320 
00321         wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
00322                    "SSID", MAC2STR(mgmt->sa),
00323                    elems.ssid_len == 0 ? "broadcast" : "our");
00324 }
00325 
00326 
00327 void ieee802_11_set_beacon(struct hostapd_data *hapd)
00328 {
00329         struct ieee80211_mgmt *head;
00330         u8 *pos, *tail, *tailpos;
00331         int preamble;
00332         u16 capab_info;
00333         size_t head_len, tail_len;
00334         int cts_protection = ((ieee802_11_erp_info(hapd) &
00335                               ERP_INFO_USE_PROTECTION) ? 1 : 0);
00336 
00337 #define BEACON_HEAD_BUF_SIZE 256
00338 #define BEACON_TAIL_BUF_SIZE 512
00339         head = os_zalloc(BEACON_HEAD_BUF_SIZE);
00340         tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
00341         if (head == NULL || tail == NULL) {
00342                 wpa_printf(MSG_ERROR, "Failed to set beacon data");
00343                 os_free(head);
00344                 os_free(tail);
00345                 return;
00346         }
00347 
00348         head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
00349                                            WLAN_FC_STYPE_BEACON);
00350         head->duration = host_to_le16(0);
00351         os_memset(head->da, 0xff, ETH_ALEN);
00352 
00353         os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
00354         os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
00355         head->u.beacon.beacon_int =
00356                 host_to_le16(hapd->iconf->beacon_int);
00357 
00358         /* hardware or low-level driver will setup seq_ctrl and timestamp */
00359         capab_info = hostapd_own_capab_info(hapd, NULL, 0);
00360         head->u.beacon.capab_info = host_to_le16(capab_info);
00361         pos = &head->u.beacon.variable[0];
00362 
00363         /* SSID */
00364         *pos++ = WLAN_EID_SSID;
00365         if (hapd->conf->ignore_broadcast_ssid == 2) {
00366                 /* clear the data, but keep the correct length of the SSID */
00367                 *pos++ = hapd->conf->ssid.ssid_len;
00368                 os_memset(pos, 0, hapd->conf->ssid.ssid_len);
00369                 pos += hapd->conf->ssid.ssid_len;
00370         } else if (hapd->conf->ignore_broadcast_ssid) {
00371                 *pos++ = 0; /* empty SSID */
00372         } else {
00373                 *pos++ = hapd->conf->ssid.ssid_len;
00374                 os_memcpy(pos, hapd->conf->ssid.ssid,
00375                           hapd->conf->ssid.ssid_len);
00376                 pos += hapd->conf->ssid.ssid_len;
00377         }
00378 
00379         /* Supported rates */
00380         pos = hostapd_eid_supp_rates(hapd, pos);
00381 
00382         /* DS Params */
00383         pos = hostapd_eid_ds_params(hapd, pos);
00384 
00385         head_len = pos - (u8 *) head;
00386 
00387         tailpos = hostapd_eid_country(hapd, tailpos,
00388                                       tail + BEACON_TAIL_BUF_SIZE - tailpos);
00389 
00390         /* ERP Information element */
00391         tailpos = hostapd_eid_erp_info(hapd, tailpos);
00392 
00393         /* Extended supported rates */
00394         tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
00395 
00396         tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
00397                                   tailpos, NULL);
00398 
00399         /* Wi-Fi Alliance WMM */
00400         tailpos = hostapd_eid_wmm(hapd, tailpos);
00401 
00402 #ifdef CONFIG_IEEE80211N
00403         if (hapd->iconf->ieee80211n) {
00404                 u8 *ht_capab, *ht_oper;
00405                 ht_capab = tailpos;
00406                 tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
00407 
00408                 ht_oper = tailpos;
00409                 tailpos = hostapd_eid_ht_operation(hapd, tailpos);
00410 
00411                 if (tailpos > ht_oper && ht_oper > ht_capab &&
00412                     hostapd_set_ht_params(hapd->conf->iface, hapd,
00413                                           ht_capab + 2, ht_capab[1],
00414                                           ht_oper + 2, ht_oper[1])) {
00415                         wpa_printf(MSG_ERROR, "Could not set HT capabilities "
00416                                    "for kernel driver");
00417                 }
00418         }
00419 #endif /* CONFIG_IEEE80211N */
00420 
00421 #ifdef CONFIG_WPS
00422         if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
00423                 os_memcpy(tailpos, hapd->wps_beacon_ie,
00424                           hapd->wps_beacon_ie_len);
00425                 tailpos += hapd->wps_beacon_ie_len;
00426         }
00427 #endif /* CONFIG_WPS */
00428 
00429         tail_len = tailpos > tail ? tailpos - tail : 0;
00430 
00431         if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
00432                                tail, tail_len, hapd->conf->dtim_period))
00433                 wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
00434                            "period");
00435 
00436         os_free(tail);
00437         os_free(head);
00438 
00439         if (hostapd_set_cts_protect(hapd, cts_protection))
00440                 wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
00441                            "driver");
00442 
00443         if (hapd->iface->current_mode &&
00444             hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
00445             hostapd_set_short_slot_time(hapd,
00446                                         hapd->iface->num_sta_no_short_slot_time
00447                                         > 0 ? 0 : 1))
00448                 wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
00449                            "in kernel driver");
00450 
00451         if (hapd->iface->num_sta_no_short_preamble == 0 &&
00452             hapd->iconf->preamble == SHORT_PREAMBLE)
00453                 preamble = SHORT_PREAMBLE;
00454         else
00455                 preamble = LONG_PREAMBLE;
00456         if (hostapd_set_preamble(hapd, preamble))
00457                 wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
00458                            "driver");
00459 }
00460 
00461 
00462 void ieee802_11_set_beacons(struct hostapd_iface *iface)
00463 {
00464         size_t i;
00465         for (i = 0; i < iface->num_bss; i++)
00466                 ieee802_11_set_beacon(iface->bss[i]);
00467 }
00468 
00469 #endif /* CONFIG_NATIVE_WINDOWS */
00470 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:47 2009 for hostapd by  doxygen 1.6.1