beacon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 "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(struct hostapd_data *hapd, u8 *eid,
  83. int max_len)
  84. {
  85. u8 *pos = eid;
  86. if (!hapd->iconf->ieee80211d || 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_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
  98. struct sta_info *sta)
  99. {
  100. const u8 *ie;
  101. size_t ielen;
  102. ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
  103. if (ie == NULL || ielen > len)
  104. return eid;
  105. os_memcpy(eid, ie, ielen);
  106. return eid + ielen;
  107. }
  108. void handle_probe_req(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  109. size_t len)
  110. {
  111. struct ieee80211_mgmt *resp;
  112. struct ieee802_11_elems elems;
  113. char *ssid;
  114. u8 *pos, *epos, *ie;
  115. size_t ssid_len, ie_len;
  116. struct sta_info *sta = NULL;
  117. ie = mgmt->u.probe_req.variable;
  118. ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
  119. hostapd_wps_probe_req_rx(hapd, mgmt->sa, ie, ie_len);
  120. if (!hapd->iconf->send_probe_response)
  121. return;
  122. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  123. wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
  124. MAC2STR(mgmt->sa));
  125. return;
  126. }
  127. ssid = NULL;
  128. ssid_len = 0;
  129. if ((!elems.ssid || !elems.supp_rates)) {
  130. wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
  131. "without SSID or supported rates element",
  132. MAC2STR(mgmt->sa));
  133. return;
  134. }
  135. if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
  136. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
  137. "broadcast SSID ignored", MAC2STR(mgmt->sa));
  138. return;
  139. }
  140. sta = ap_get_sta(hapd, mgmt->sa);
  141. if (elems.ssid_len == 0 ||
  142. (elems.ssid_len == hapd->conf->ssid.ssid_len &&
  143. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
  144. 0)) {
  145. ssid = hapd->conf->ssid.ssid;
  146. ssid_len = hapd->conf->ssid.ssid_len;
  147. if (sta)
  148. sta->ssid_probe = &hapd->conf->ssid;
  149. }
  150. if (!ssid) {
  151. if (!(mgmt->da[0] & 0x01)) {
  152. char ssid_txt[33];
  153. ieee802_11_print_ssid(ssid_txt, elems.ssid,
  154. elems.ssid_len);
  155. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  156. " for foreign SSID '%s'",
  157. MAC2STR(mgmt->sa), ssid_txt);
  158. }
  159. return;
  160. }
  161. /* TODO: verify that supp_rates contains at least one matching rate
  162. * with AP configuration */
  163. #define MAX_PROBERESP_LEN 768
  164. resp = os_zalloc(MAX_PROBERESP_LEN);
  165. if (resp == NULL)
  166. return;
  167. epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
  168. resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  169. WLAN_FC_STYPE_PROBE_RESP);
  170. os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
  171. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  172. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  173. resp->u.probe_resp.beacon_int =
  174. host_to_le16(hapd->iconf->beacon_int);
  175. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  176. resp->u.probe_resp.capab_info =
  177. host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
  178. pos = resp->u.probe_resp.variable;
  179. *pos++ = WLAN_EID_SSID;
  180. *pos++ = ssid_len;
  181. os_memcpy(pos, ssid, ssid_len);
  182. pos += ssid_len;
  183. /* Supported rates */
  184. pos = hostapd_eid_supp_rates(hapd, pos);
  185. /* DS Params */
  186. pos = hostapd_eid_ds_params(hapd, pos);
  187. pos = hostapd_eid_country(hapd, pos, epos - pos);
  188. /* ERP Information element */
  189. pos = hostapd_eid_erp_info(hapd, pos);
  190. /* Extended supported rates */
  191. pos = hostapd_eid_ext_supp_rates(hapd, pos);
  192. pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
  193. /* Wi-Fi Wireless Multimedia Extensions */
  194. pos = hostapd_eid_wme(hapd, pos);
  195. pos = hostapd_eid_ht_capabilities_info(hapd, pos);
  196. pos = hostapd_eid_ht_operation(hapd, pos);
  197. #ifdef CONFIG_WPS
  198. if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
  199. os_memcpy(pos, hapd->wps_probe_resp_ie,
  200. hapd->wps_probe_resp_ie_len);
  201. pos += hapd->wps_probe_resp_ie_len;
  202. }
  203. #endif /* CONFIG_WPS */
  204. if (hostapd_send_mgmt_frame(hapd, resp, pos - (u8 *) resp, 0) < 0)
  205. perror("handle_probe_req: send");
  206. os_free(resp);
  207. wpa_printf(MSG_MSGDUMP, "STA " MACSTR " sent probe request for %s "
  208. "SSID", MAC2STR(mgmt->sa),
  209. elems.ssid_len == 0 ? "broadcast" : "our");
  210. }
  211. void ieee802_11_set_beacon(struct hostapd_data *hapd)
  212. {
  213. struct ieee80211_mgmt *head;
  214. u8 *pos, *tail, *tailpos;
  215. int preamble;
  216. u16 capab_info;
  217. size_t head_len, tail_len;
  218. int cts_protection = ((ieee802_11_erp_info(hapd) &
  219. ERP_INFO_USE_PROTECTION) ? 1 : 0);
  220. #define BEACON_HEAD_BUF_SIZE 256
  221. #define BEACON_TAIL_BUF_SIZE 512
  222. head = os_zalloc(BEACON_HEAD_BUF_SIZE);
  223. tailpos = tail = os_malloc(BEACON_TAIL_BUF_SIZE);
  224. if (head == NULL || tail == NULL) {
  225. wpa_printf(MSG_ERROR, "Failed to set beacon data");
  226. os_free(head);
  227. os_free(tail);
  228. return;
  229. }
  230. head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  231. WLAN_FC_STYPE_BEACON);
  232. head->duration = host_to_le16(0);
  233. os_memset(head->da, 0xff, ETH_ALEN);
  234. os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
  235. os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
  236. head->u.beacon.beacon_int =
  237. host_to_le16(hapd->iconf->beacon_int);
  238. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  239. capab_info = hostapd_own_capab_info(hapd, NULL, 0);
  240. head->u.beacon.capab_info = host_to_le16(capab_info);
  241. pos = &head->u.beacon.variable[0];
  242. /* SSID */
  243. *pos++ = WLAN_EID_SSID;
  244. if (hapd->conf->ignore_broadcast_ssid == 2) {
  245. /* clear the data, but keep the correct length of the SSID */
  246. *pos++ = hapd->conf->ssid.ssid_len;
  247. os_memset(pos, 0, hapd->conf->ssid.ssid_len);
  248. pos += hapd->conf->ssid.ssid_len;
  249. } else if (hapd->conf->ignore_broadcast_ssid) {
  250. *pos++ = 0; /* empty SSID */
  251. } else {
  252. *pos++ = hapd->conf->ssid.ssid_len;
  253. os_memcpy(pos, hapd->conf->ssid.ssid,
  254. hapd->conf->ssid.ssid_len);
  255. pos += hapd->conf->ssid.ssid_len;
  256. }
  257. /* Supported rates */
  258. pos = hostapd_eid_supp_rates(hapd, pos);
  259. /* DS Params */
  260. pos = hostapd_eid_ds_params(hapd, pos);
  261. head_len = pos - (u8 *) head;
  262. tailpos = hostapd_eid_country(hapd, tailpos,
  263. tail + BEACON_TAIL_BUF_SIZE - tailpos);
  264. /* ERP Information element */
  265. tailpos = hostapd_eid_erp_info(hapd, tailpos);
  266. /* Extended supported rates */
  267. tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
  268. tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
  269. tailpos, NULL);
  270. /* Wi-Fi Wireless Multimedia Extensions */
  271. tailpos = hostapd_eid_wme(hapd, tailpos);
  272. #ifdef CONFIG_IEEE80211N
  273. if (hapd->iconf->ieee80211n) {
  274. u8 *start;
  275. start = tailpos;
  276. tailpos = hostapd_eid_ht_capabilities_info(hapd, tailpos);
  277. if (hostapd_set_ht_capability(hapd->conf->iface, hapd,
  278. start + 2)) {
  279. wpa_printf(MSG_ERROR, "Could not set HT capabilities "
  280. "for kernel driver");
  281. }
  282. start = tailpos;
  283. tailpos = hostapd_eid_ht_operation(hapd, tailpos);
  284. if (hostapd_set_ht_operation(hapd->conf->iface, hapd,
  285. start + 2))
  286. wpa_printf(MSG_ERROR, "Could not set HT operation for "
  287. "kernel driver");
  288. }
  289. #endif /* CONFIG_IEEE80211N */
  290. #ifdef CONFIG_WPS
  291. if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
  292. os_memcpy(tailpos, hapd->wps_beacon_ie,
  293. hapd->wps_beacon_ie_len);
  294. tailpos += hapd->wps_beacon_ie_len;
  295. }
  296. #endif /* CONFIG_WPS */
  297. tail_len = tailpos > tail ? tailpos - tail : 0;
  298. if (hostapd_set_beacon(hapd->conf->iface, hapd, (u8 *) head, head_len,
  299. tail, tail_len))
  300. wpa_printf(MSG_ERROR, "Failed to set beacon head/tail");
  301. os_free(tail);
  302. os_free(head);
  303. if (hostapd_set_cts_protect(hapd, cts_protection))
  304. wpa_printf(MSG_ERROR, "Failed to set CTS protect in kernel "
  305. "driver");
  306. if (hapd->iface->current_mode &&
  307. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
  308. hostapd_set_short_slot_time(hapd,
  309. hapd->iface->num_sta_no_short_slot_time
  310. > 0 ? 0 : 1))
  311. wpa_printf(MSG_ERROR, "Failed to set Short Slot Time option "
  312. "in kernel driver");
  313. if (hapd->iface->num_sta_no_short_preamble == 0 &&
  314. hapd->iconf->preamble == SHORT_PREAMBLE)
  315. preamble = SHORT_PREAMBLE;
  316. else
  317. preamble = LONG_PREAMBLE;
  318. if (hostapd_set_preamble(hapd, preamble))
  319. wpa_printf(MSG_ERROR, "Could not set preamble for kernel "
  320. "driver");
  321. }
  322. void ieee802_11_set_beacons(struct hostapd_iface *iface)
  323. {
  324. size_t i;
  325. for (i = 0; i < iface->num_bss; i++)
  326. ieee802_11_set_beacon(iface->bss[i]);
  327. }
  328. #endif /* CONFIG_NATIVE_WINDOWS */