beacon.c 11 KB

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