beacon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_i.h"
  26. #include "sta_info.h"
  27. #include "wps_hostapd.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_add(u8 *pos, u8 *end, int chan_spacing,
  83. struct hostapd_channel_data *start,
  84. struct hostapd_channel_data *prev)
  85. {
  86. if (end - pos < 3)
  87. return pos;
  88. /* first channel number */
  89. *pos++ = start->chan;
  90. /* number of channels */
  91. *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
  92. /* maximum transmit power level */
  93. *pos++ = start->max_tx_power;
  94. return pos;
  95. }
  96. static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
  97. int max_len)
  98. {
  99. u8 *pos = eid;
  100. u8 *end = eid + max_len;
  101. int i;
  102. struct hostapd_hw_modes *mode;
  103. struct hostapd_channel_data *start, *prev;
  104. int chan_spacing = 1;
  105. if (!hapd->iconf->ieee80211d || max_len < 6 ||
  106. hapd->iface->current_mode == NULL)
  107. return eid;
  108. *pos++ = WLAN_EID_COUNTRY;
  109. pos++; /* length will be set later */
  110. os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
  111. pos += 3;
  112. mode = hapd->iface->current_mode;
  113. if (mode->mode == HOSTAPD_MODE_IEEE80211A)
  114. chan_spacing = 4;
  115. start = prev = NULL;
  116. for (i = 0; i < mode->num_channels; i++) {
  117. struct hostapd_channel_data *chan = &mode->channels[i];
  118. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  119. continue;
  120. if (start && prev &&
  121. prev->chan + chan_spacing == chan->chan &&
  122. start->max_tx_power == chan->max_tx_power) {
  123. prev = chan;
  124. continue; /* can use same entry */
  125. }
  126. if (start) {
  127. pos = hostapd_eid_country_add(pos, end, chan_spacing,
  128. start, prev);
  129. start = NULL;
  130. }
  131. /* Start new group */
  132. start = prev = chan;
  133. }
  134. if (start) {
  135. pos = hostapd_eid_country_add(pos, end, chan_spacing,
  136. start, prev);
  137. }
  138. if ((pos - eid) & 1) {
  139. if (end - pos < 1)
  140. return eid;
  141. *pos++ = 0; /* pad for 16-bit alignment */
  142. }
  143. eid[1] = (pos - eid) - 2;
  144. return pos;
  145. }
  146. static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
  147. struct sta_info *sta)
  148. {
  149. const u8 *ie;
  150. size_t ielen;
  151. ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
  152. if (ie == NULL || ielen > len)
  153. return eid;
  154. os_memcpy(eid, ie, ielen);
  155. return eid + ielen;
  156. }
  157. void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  158. size_t len)
  159. {
  160. struct ieee80211_mgmt *resp;
  161. struct ieee802_11_elems elems;
  162. char *ssid;
  163. u8 *pos, *epos, *ie;
  164. size_t ssid_len, ie_len;
  165. struct sta_info *sta = NULL;
  166. ie = mgmt->u.probe_req.variable;
  167. ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
  168. hostapd_probe_req_rx(hapd, mgmt->sa, ie, ie_len);
  169. if (!hapd->iconf->send_probe_response)
  170. return;
  171. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  172. wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
  173. MAC2STR(mgmt->sa));
  174. return;
  175. }
  176. ssid = NULL;
  177. ssid_len = 0;
  178. if ((!elems.ssid || !elems.supp_rates)) {
  179. wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
  180. "without SSID or supported rates element",
  181. MAC2STR(mgmt->sa));
  182. return;
  183. }
  184. if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
  185. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
  186. "broadcast SSID ignored", MAC2STR(mgmt->sa));
  187. return;
  188. }
  189. sta = ap_get_sta(hapd, mgmt->sa);
  190. if (elems.ssid_len == 0 ||
  191. (elems.ssid_len == hapd->conf->ssid.ssid_len &&
  192. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
  193. 0)) {
  194. ssid = hapd->conf->ssid.ssid;
  195. ssid_len = hapd->conf->ssid.ssid_len;
  196. if (sta)
  197. sta->ssid_probe = &hapd->conf->ssid;
  198. }
  199. if (!ssid) {
  200. if (!(mgmt->da[0] & 0x01)) {
  201. char ssid_txt[33];
  202. ieee802_11_print_ssid(ssid_txt, elems.ssid,
  203. elems.ssid_len);
  204. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  205. " for foreign SSID '%s'",
  206. MAC2STR(mgmt->sa), ssid_txt);
  207. }
  208. return;
  209. }
  210. /* TODO: verify that supp_rates contains at least one matching rate
  211. * with AP configuration */
  212. #define MAX_PROBERESP_LEN 768
  213. resp = os_zalloc(MAX_PROBERESP_LEN);
  214. if (resp == NULL)
  215. return;
  216. epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
  217. resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  218. WLAN_FC_STYPE_PROBE_RESP);
  219. os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
  220. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  221. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  222. resp->u.probe_resp.beacon_int =
  223. host_to_le16(hapd->iconf->beacon_int);
  224. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  225. resp->u.probe_resp.capab_info =
  226. host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
  227. pos = resp->u.probe_resp.variable;
  228. *pos++ = WLAN_EID_SSID;
  229. *pos++ = ssid_len;
  230. os_memcpy(pos, ssid, ssid_len);
  231. pos += ssid_len;
  232. /* Supported rates */
  233. pos = hostapd_eid_supp_rates(hapd, pos);
  234. /* DS Params */
  235. pos = hostapd_eid_ds_params(hapd, pos);
  236. pos = hostapd_eid_country(hapd, pos, epos - pos);
  237. /* ERP Information element */
  238. pos = hostapd_eid_erp_info(hapd, pos);
  239. /* Extended supported rates */
  240. pos = hostapd_eid_ext_supp_rates(hapd, pos);
  241. pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
  242. /* Wi-Fi Alliance WMM */
  243. pos = hostapd_eid_wmm(hapd, pos);
  244. pos = hostapd_eid_ht_capabilities_info(hapd, pos);
  245. pos = hostapd_eid_ht_operation(hapd, pos);
  246. #ifdef CONFIG_WPS
  247. if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
  248. os_memcpy(pos, hapd->wps_probe_resp_ie,
  249. hapd->wps_probe_resp_ie_len);
  250. pos += hapd->wps_probe_resp_ie_len;
  251. }
  252. #endif /* CONFIG_WPS */
  253. if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp) < 0)
  254. perror("handle_probe_req: send");
  255. os_free(resp);
  256. wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
  257. "SSID", MAC2STR(mgmt->sa),
  258. elems.ssid_len == 0 ? "broadcast" : "our");
  259. }
  260. void ieee802_11_set_beacon(struct hostapd_data *hapd)
  261. {
  262. struct ieee80211_mgmt *head;
  263. u8 *pos, *tail, *tailpos;
  264. int preamble;
  265. u16 capab_info;
  266. size_t head_len, tail_len;
  267. int cts_protection = ((ieee802_11_erp_info(hapd) &
  268. ERP_INFO_USE_PROTECTION) ? 1 : 0);
  269. #define BEACON_HEAD_BUF_SIZE 256
  270. #define BEACON_TAIL_BUF_SIZE 512
  271. head = os_zalloc(BEACON_HEAD_BUF_SIZE);
  272. tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
  273. if (head == NULL || tail == NULL) {
  274. wpa_printf(MSG_ERROR, "Failed to set beacon data");
  275. os_free(head);
  276. os_free(tail);
  277. return;
  278. }
  279. head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  280. WLAN_FC_STYPE_BEACON);
  281. head->duration = host_to_le16(0);
  282. os_memset(head->da, 0xff, ETH_ALEN);
  283. os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
  284. os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
  285. head->u.beacon.beacon_int =
  286. host_to_le16(hapd->iconf->beacon_int);
  287. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  288. capab_info = hostapd_own_capab_info(hapd, NULL, 0);
  289. head->u.beacon.capab_info = host_to_le16(capab_info);
  290. pos = &head->u.beacon.variable[0];
  291. /* SSID */
  292. *pos++ = WLAN_EID_SSID;
  293. if (hapd->conf->ignore_broadcast_ssid == 2) {
  294. /* clear the data, but keep the correct length of the SSID */
  295. *pos++ = hapd->conf->ssid.ssid_len;
  296. os_memset(pos, 0, hapd->conf->ssid.ssid_len);
  297. pos += hapd->conf->ssid.ssid_len;
  298. } else if (hapd->conf->ignore_broadcast_ssid) {
  299. *pos++ = 0; /* empty SSID */
  300. } else {
  301. *pos++ = hapd->conf->ssid.ssid_len;
  302. os_memcpy(pos, hapd->conf->ssid.ssid,
  303. hapd->conf->ssid.ssid_len);
  304. pos += hapd->conf->ssid.ssid_len;
  305. }
  306. /* Supported rates */
  307. pos = hostapd_eid_supp_rates(hapd, pos);
  308. /* DS Params */
  309. pos = hostapd_eid_ds_params(hapd, pos);
  310. head_len = pos - (u8 *) head;
  311. tailpos = hostapd_eid_country(hapd, tailpos,
  312. tail + BEACON_TAIL_BUF_SIZE - tailpos);
  313. /* ERP Information element */
  314. tailpos = hostapd_eid_erp_info(hapd, tailpos);
  315. /* Extended supported rates */
  316. tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
  317. tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
  318. tailpos, NULL);
  319. /* Wi-Fi Alliance WMM */
  320. tailpos = hostapd_eid_wmm(hapd, tailpos);
  321. #ifdef CONFIG_IEEE80211N
  322. if (hapd->iconf->ieee80211n) {
  323. u8 *ht_capab, *ht_oper;
  324. ht_capab = tailpos;
  325. tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
  326. ht_oper = tailpos;
  327. tailpos = hostapd_eid_ht_operation(hapd, tailpos);
  328. if (tailpos > ht_oper && ht_oper > ht_capab &&
  329. hostapd_set_ht_params(hapd->conf->iface, hapd,
  330. ht_capab + 2, ht_capab[1],
  331. ht_oper + 2, ht_oper[1])) {
  332. wpa_printf(MSG_ERROR, "Could not set HT capabilities "
  333. "for kernel driver");
  334. }
  335. }
  336. #endif /* CONFIG_IEEE80211N */
  337. #ifdef CONFIG_WPS
  338. if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
  339. os_memcpy(tailpos, hapd->wps_beacon_ie,
  340. hapd->wps_beacon_ie_len);
  341. tailpos += hapd->wps_beacon_ie_len;
  342. }
  343. #endif /* CONFIG_WPS */
  344. tail_len = tailpos > tail ? tailpos - tail : 0;
  345. if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
  346. tail, tail_len, hapd->conf->dtim_period,
  347. hapd->iconf->beacon_int))
  348. wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
  349. "period");
  350. os_free(tail);
  351. os_free(head);
  352. if (hostapd_set_cts_protect(hapd, cts_protection))
  353. wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
  354. "driver");
  355. if (hapd->iface->current_mode &&
  356. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  357. hostapd_set_short_slot_time(hapd,
  358. hapd->iface->num_sta_no_short_slot_time
  359. > 0 ? 0 : 1))
  360. wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
  361. "in kernel driver");
  362. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  363. hapd->iconf->preamble == SHORT_PREAMBLE)
  364. preamble = SHORT_PREAMBLE;
  365. else
  366. preamble = LONG_PREAMBLE;
  367. if (hostapd_set_preamble(hapd, preamble))
  368. wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
  369. "driver");
  370. }
  371. void ieee802_11_set_beacons(struct hostapd_iface *iface)
  372. {
  373. size_t i;
  374. for (i = 0; i < iface->num_bss; i++)
  375. ieee802_11_set_beacon(iface->bss[i]);
  376. }
  377. #endif /* CONFIG_NATIVE_WINDOWS */