beacon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * hostapd / IEEE 802.11 Management: Beacon and Probe Request/Response
  3. * Copyright (c) 2002-2004, Instant802 Networks, Inc.
  4. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  5. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "includes.h"
  17. #ifndef CONFIG_NATIVE_WINDOWS
  18. #include "hostapd.h"
  19. #include "ieee802_11.h"
  20. #include "wpa.h"
  21. #include "wme.h"
  22. #include "beacon.h"
  23. #include "hw_features.h"
  24. #include "driver.h"
  25. #include "sta_info.h"
  26. #include "ieee802_11h.h"
  27. static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
  28. {
  29. u8 erp = 0;
  30. if (hapd->iface->current_mode == NULL ||
  31. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  32. return 0;
  33. switch (hapd->iconf->cts_protection_type) {
  34. case CTS_PROTECTION_FORCE_ENABLED:
  35. erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
  36. break;
  37. case CTS_PROTECTION_FORCE_DISABLED:
  38. erp = 0;
  39. break;
  40. case CTS_PROTECTION_AUTOMATIC:
  41. if (hapd->iface->olbc)
  42. erp |= ERP_INFO_USE_PROTECTION;
  43. /* continue */
  44. case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
  45. if (hapd->iface->num_sta_non_erp > 0) {
  46. erp |= ERP_INFO_NON_ERP_PRESENT |
  47. ERP_INFO_USE_PROTECTION;
  48. }
  49. break;
  50. }
  51. if (hapd->iface->num_sta_no_short_preamble > 0)
  52. erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
  53. return erp;
  54. }
  55. static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
  56. {
  57. *eid++ = WLAN_EID_DS_PARAMS;
  58. *eid++ = 1;
  59. *eid++ = hapd->iconf->channel;
  60. return eid;
  61. }
  62. static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
  63. {
  64. if (hapd->iface->current_mode == NULL ||
  65. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  66. return eid;
  67. /* Set NonERP_present and use_protection bits if there
  68. * are any associated NonERP stations. */
  69. /* TODO: use_protection bit can be set to zero even if
  70. * there are NonERP stations present. This optimization
  71. * might be useful if NonERP stations are "quiet".
  72. * See 802.11g/D6 E-1 for recommended practice.
  73. * In addition, Non ERP present might be set, if AP detects Non ERP
  74. * operation on other APs. */
  75. /* Add ERP Information element */
  76. *eid++ = WLAN_EID_ERP_INFO;
  77. *eid++ = 1;
  78. *eid++ = ieee802_11_erp_info(hapd);
  79. return eid;
  80. }
  81. static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
  82. int max_len)
  83. {
  84. u8 *pos = eid;
  85. if ((!hapd->iconf->ieee80211d && !hapd->iface->dfs_enable) ||
  86. max_len < 6)
  87. return eid;
  88. *pos++ = WLAN_EID_COUNTRY;
  89. pos++; /* length will be set later */
  90. os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
  91. pos += 3;
  92. if ((pos - eid) & 1)
  93. *pos++ = 0; /* pad for 16-bit alignment */
  94. eid[1] = (pos - eid) - 2;
  95. return pos;
  96. }
  97. static u8 * hostapd_eid_power_constraint(struct hostapd_data *hapd, u8 *eid)
  98. {
  99. if (!hapd->iface->dfs_enable)
  100. return eid;
  101. *eid++ = WLAN_EID_PWR_CONSTRAINT;
  102. *eid++ = 1;
  103. *eid++ = hapd->iface->pwr_const;
  104. return eid;
  105. }
  106. static u8 * hostapd_eid_tpc_report(struct hostapd_data *hapd, u8 *eid)
  107. {
  108. if (!hapd->iface->dfs_enable)
  109. return eid;
  110. *eid++ = WLAN_EID_TPC_REPORT;
  111. *eid++ = 2;
  112. *eid++ = hapd->iface->tx_power; /* TX POWER */
  113. *eid++ = 0; /* Link Margin */
  114. return eid;
  115. }
  116. static u8 * hostapd_eid_channel_switch(struct hostapd_data *hapd, u8 *eid)
  117. {
  118. if (!hapd->iface->dfs_enable || !hapd->iface->channel_switch)
  119. return eid;
  120. *eid++ = WLAN_EID_CHANNEL_SWITCH;
  121. *eid++ = 3;
  122. *eid++ = CHAN_SWITCH_MODE_QUIET;
  123. *eid++ = hapd->iface->channel_switch; /* New channel */
  124. /* 0 - very soon; 1 - before next TBTT; num - after num beacons */
  125. *eid++ = 0;
  126. return eid;
  127. }
  128. static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
  129. struct sta_info *sta)
  130. {
  131. const u8 *ie;
  132. size_t ielen;
  133. ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
  134. if (ie == NULL || ielen > len)
  135. return eid;
  136. os_memcpy(eid, ie, ielen);
  137. return eid + ielen;
  138. }
  139. void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  140. size_t len)
  141. {
  142. struct ieee80211_mgmt *resp;
  143. struct ieee802_11_elems elems;
  144. char *ssid;
  145. u8 *pos, *epos, *ie;
  146. size_t ssid_len, ie_len;
  147. struct sta_info *sta = NULL;
  148. ie = mgmt->u.probe_req.variable;
  149. ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
  150. if (!hapd->iconf->send_probe_response)
  151. return;
  152. if (ieee802_11_parse_elems(hapd, ie, ie_len, &elems, 0) == ParseFailed)
  153. {
  154. wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
  155. MAC2STR(mgmt->sa));
  156. return;
  157. }
  158. ssid = NULL;
  159. ssid_len = 0;
  160. if ((!elems.ssid || !elems.supp_rates)) {
  161. wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
  162. "without SSID or supported rates element",
  163. MAC2STR(mgmt->sa));
  164. return;
  165. }
  166. if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
  167. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
  168. "broadcast SSID ignored", MAC2STR(mgmt->sa));
  169. return;
  170. }
  171. sta = ap_get_sta(hapd, mgmt->sa);
  172. if (elems.ssid_len == 0 ||
  173. (elems.ssid_len == hapd->conf->ssid.ssid_len &&
  174. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
  175. 0)) {
  176. ssid = hapd->conf->ssid.ssid;
  177. ssid_len = hapd->conf->ssid.ssid_len;
  178. if (sta)
  179. sta->ssid_probe = &hapd->conf->ssid;
  180. }
  181. if (!ssid) {
  182. if (!(mgmt->da[0] & 0x01)) {
  183. char ssid_txt[33];
  184. ieee802_11_print_ssid(ssid_txt, elems.ssid,
  185. elems.ssid_len);
  186. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  187. " for foreign SSID '%s'",
  188. MAC2STR(mgmt->sa), ssid_txt);
  189. }
  190. return;
  191. }
  192. /* TODO: verify that supp_rates contains at least one matching rate
  193. * with AP configuration */
  194. #define MAX_PROBERESP_LEN 768
  195. resp = os_zalloc(MAX_PROBERESP_LEN);
  196. if (resp == NULL)
  197. return;
  198. epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
  199. resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  200. WLAN_FC_STYPE_PROBE_RESP);
  201. os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
  202. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  203. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  204. resp->u.probe_resp.beacon_int =
  205. host_to_le16(hapd->iconf->beacon_int);
  206. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  207. resp->u.probe_resp.capab_info =
  208. host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
  209. pos = resp->u.probe_resp.variable;
  210. *pos++ = WLAN_EID_SSID;
  211. *pos++ = ssid_len;
  212. os_memcpy(pos, ssid, ssid_len);
  213. pos += ssid_len;
  214. /* Supported rates */
  215. pos = hostapd_eid_supp_rates(hapd, pos);
  216. /* DS Params */
  217. pos = hostapd_eid_ds_params(hapd, pos);
  218. pos = hostapd_eid_country(hapd, pos, epos - pos);
  219. pos = hostapd_eid_power_constraint(hapd, pos);
  220. pos = hostapd_eid_tpc_report(hapd, pos);
  221. /* ERP Information element */
  222. pos = hostapd_eid_erp_info(hapd, pos);
  223. /* Extended supported rates */
  224. pos = hostapd_eid_ext_supp_rates(hapd, pos);
  225. pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
  226. /* Wi-Fi Wireless Multimedia Extensions */
  227. if (hapd->conf->wme_enabled)
  228. pos = hostapd_eid_wme(hapd, pos);
  229. if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
  230. perror("handle_probe_req: send");
  231. os_free(resp);
  232. wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
  233. "SSID", MAC2STR(mgmt->sa),
  234. elems.ssid_len == 0 ? "broadcast" : "our");
  235. }
  236. void ieee802_11_set_beacon(struct hostapd_data *hapd)
  237. {
  238. struct ieee80211_mgmt *head;
  239. u8 *pos, *tail, *tailpos;
  240. int preamble;
  241. u16 capab_info;
  242. size_t head_len, tail_len;
  243. int cts_protection = ((ieee802_11_erp_info(hapd) &
  244. ERP_INFO_USE_PROTECTION) ? 1 : 0);
  245. #define BEACON_HEAD_BUF_SIZE 256
  246. #define BEACON_TAIL_BUF_SIZE 512
  247. head = os_zalloc(BEACON_HEAD_BUF_SIZE);
  248. tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
  249. if (head == NULL || tail == NULL) {
  250. wpa_printf(MSG_ERROR, "Failed to set beacon data");
  251. os_free(head);
  252. os_free(tail);
  253. return;
  254. }
  255. head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  256. WLAN_FC_STYPE_BEACON);
  257. head->duration = host_to_le16(0);
  258. os_memset(head->da, 0xff, ETH_ALEN);
  259. os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
  260. os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
  261. head->u.beacon.beacon_int =
  262. host_to_le16(hapd->iconf->beacon_int);
  263. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  264. capab_info = hostapd_own_capab_info(hapd, NULL, 0);
  265. head->u.beacon.capab_info = host_to_le16(capab_info);
  266. pos = &head->u.beacon.variable[0];
  267. /* SSID */
  268. *pos++ = WLAN_EID_SSID;
  269. if (hapd->conf->ignore_broadcast_ssid == 2) {
  270. /* clear the data, but keep the correct length of the SSID */
  271. *pos++ = hapd->conf->ssid.ssid_len;
  272. os_memset(pos, 0, hapd->conf->ssid.ssid_len);
  273. pos += hapd->conf->ssid.ssid_len;
  274. } else if (hapd->conf->ignore_broadcast_ssid) {
  275. *pos++ = 0; /* empty SSID */
  276. } else {
  277. *pos++ = hapd->conf->ssid.ssid_len;
  278. os_memcpy(pos, hapd->conf->ssid.ssid,
  279. hapd->conf->ssid.ssid_len);
  280. pos += hapd->conf->ssid.ssid_len;
  281. }
  282. /* Supported rates */
  283. pos = hostapd_eid_supp_rates(hapd, pos);
  284. /* DS Params */
  285. pos = hostapd_eid_ds_params(hapd, pos);
  286. head_len = pos - (u8 *) head;
  287. tailpos = hostapd_eid_country(hapd, tailpos,
  288. tail + BEACON_TAIL_BUF_SIZE - tailpos);
  289. tailpos = hostapd_eid_power_constraint(hapd, tailpos);
  290. tailpos = hostapd_eid_channel_switch(hapd, tailpos);
  291. tailpos = hostapd_eid_tpc_report(hapd, tailpos);
  292. /* ERP Information element */
  293. tailpos = hostapd_eid_erp_info(hapd, tailpos);
  294. /* Extended supported rates */
  295. tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
  296. tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
  297. tailpos, NULL);
  298. /* Wi-Fi Wireless Multimedia Extensions */
  299. if (hapd->conf->wme_enabled)
  300. tailpos = hostapd_eid_wme(hapd, tailpos);
  301. tail_len = tailpos > tail ? tailpos - tail : 0;
  302. if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
  303. tail, tail_len))
  304. wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
  305. os_free(tail);
  306. os_free(head);
  307. if (hostapd_set_cts_protect(hapd, cts_protection))
  308. wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
  309. "driver");
  310. if (hapd->iface->current_mode &&
  311. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  312. hostapd_set_short_slot_time(hapd,
  313. hapd->iface->num_sta_no_short_slot_time
  314. > 0 ? 0 : 1))
  315. wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
  316. "in kernel driver");
  317. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  318. hapd->iconf->preamble == SHORT_PREAMBLE)
  319. preamble = SHORT_PREAMBLE;
  320. else
  321. preamble = LONG_PREAMBLE;
  322. if (hostapd_set_preamble(hapd, preamble))
  323. wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
  324. "driver");
  325. }
  326. void ieee802_11_set_beacons(struct hostapd_iface *iface)
  327. {
  328. size_t i;
  329. for (i = 0; i < iface->num_bss; i++)
  330. ieee802_11_set_beacon(iface->bss[i]);
  331. }
  332. #endif /* CONFIG_NATIVE_WINDOWS */