You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

beacon.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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-2009, 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 "utils/includes.h"
  17. #ifndef CONFIG_NATIVE_WINDOWS
  18. #include "utils/common.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/ieee802_11_common.h"
  21. #include "drivers/driver.h"
  22. #include "wps/wps_defs.h"
  23. #include "p2p/p2p.h"
  24. #include "hostapd.h"
  25. #include "ieee802_11.h"
  26. #include "wpa_auth.h"
  27. #include "wmm.h"
  28. #include "ap_config.h"
  29. #include "sta_info.h"
  30. #include "p2p_hostapd.h"
  31. #include "ap_drv_ops.h"
  32. #include "beacon.h"
  33. static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
  34. {
  35. u8 erp = 0;
  36. if (hapd->iface->current_mode == NULL ||
  37. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  38. return 0;
  39. switch (hapd->iconf->cts_protection_type) {
  40. case CTS_PROTECTION_FORCE_ENABLED:
  41. erp |= ERP_INFO_NON_ERP_PRESENT | ERP_INFO_USE_PROTECTION;
  42. break;
  43. case CTS_PROTECTION_FORCE_DISABLED:
  44. erp = 0;
  45. break;
  46. case CTS_PROTECTION_AUTOMATIC:
  47. if (hapd->iface->olbc)
  48. erp |= ERP_INFO_USE_PROTECTION;
  49. /* continue */
  50. case CTS_PROTECTION_AUTOMATIC_NO_OLBC:
  51. if (hapd->iface->num_sta_non_erp > 0) {
  52. erp |= ERP_INFO_NON_ERP_PRESENT |
  53. ERP_INFO_USE_PROTECTION;
  54. }
  55. break;
  56. }
  57. if (hapd->iface->num_sta_no_short_preamble > 0 ||
  58. hapd->iconf->preamble == LONG_PREAMBLE)
  59. erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
  60. return erp;
  61. }
  62. static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
  63. {
  64. *eid++ = WLAN_EID_DS_PARAMS;
  65. *eid++ = 1;
  66. *eid++ = hapd->iconf->channel;
  67. return eid;
  68. }
  69. static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
  70. {
  71. if (hapd->iface->current_mode == NULL ||
  72. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  73. return eid;
  74. /* Set NonERP_present and use_protection bits if there
  75. * are any associated NonERP stations. */
  76. /* TODO: use_protection bit can be set to zero even if
  77. * there are NonERP stations present. This optimization
  78. * might be useful if NonERP stations are "quiet".
  79. * See 802.11g/D6 E-1 for recommended practice.
  80. * In addition, Non ERP present might be set, if AP detects Non ERP
  81. * operation on other APs. */
  82. /* Add ERP Information element */
  83. *eid++ = WLAN_EID_ERP_INFO;
  84. *eid++ = 1;
  85. *eid++ = ieee802_11_erp_info(hapd);
  86. return eid;
  87. }
  88. static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
  89. struct hostapd_channel_data *start,
  90. struct hostapd_channel_data *prev)
  91. {
  92. if (end - pos < 3)
  93. return pos;
  94. /* first channel number */
  95. *pos++ = start->chan;
  96. /* number of channels */
  97. *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
  98. /* maximum transmit power level */
  99. *pos++ = start->max_tx_power;
  100. return pos;
  101. }
  102. static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
  103. int max_len)
  104. {
  105. u8 *pos = eid;
  106. u8 *end = eid + max_len;
  107. int i;
  108. struct hostapd_hw_modes *mode;
  109. struct hostapd_channel_data *start, *prev;
  110. int chan_spacing = 1;
  111. if (!hapd->iconf->ieee80211d || max_len < 6 ||
  112. hapd->iface->current_mode == NULL)
  113. return eid;
  114. *pos++ = WLAN_EID_COUNTRY;
  115. pos++; /* length will be set later */
  116. os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
  117. pos += 3;
  118. mode = hapd->iface->current_mode;
  119. if (mode->mode == HOSTAPD_MODE_IEEE80211A)
  120. chan_spacing = 4;
  121. start = prev = NULL;
  122. for (i = 0; i < mode->num_channels; i++) {
  123. struct hostapd_channel_data *chan = &mode->channels[i];
  124. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  125. continue;
  126. if (start && prev &&
  127. prev->chan + chan_spacing == chan->chan &&
  128. start->max_tx_power == chan->max_tx_power) {
  129. prev = chan;
  130. continue; /* can use same entry */
  131. }
  132. if (start) {
  133. pos = hostapd_eid_country_add(pos, end, chan_spacing,
  134. start, prev);
  135. start = NULL;
  136. }
  137. /* Start new group */
  138. start = prev = chan;
  139. }
  140. if (start) {
  141. pos = hostapd_eid_country_add(pos, end, chan_spacing,
  142. start, prev);
  143. }
  144. if ((pos - eid) & 1) {
  145. if (end - pos < 1)
  146. return eid;
  147. *pos++ = 0; /* pad for 16-bit alignment */
  148. }
  149. eid[1] = (pos - eid) - 2;
  150. return pos;
  151. }
  152. static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len,
  153. struct sta_info *sta)
  154. {
  155. const u8 *ie;
  156. size_t ielen;
  157. ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
  158. if (ie == NULL || ielen > len)
  159. return eid;
  160. os_memcpy(eid, ie, ielen);
  161. return eid + ielen;
  162. }
  163. void handle_probe_req(struct hostapd_data *hapd,
  164. const struct ieee80211_mgmt *mgmt, size_t len)
  165. {
  166. struct ieee80211_mgmt *resp;
  167. struct ieee802_11_elems elems;
  168. char *ssid;
  169. u8 *pos, *epos;
  170. const u8 *ie;
  171. size_t ssid_len, ie_len;
  172. struct sta_info *sta = NULL;
  173. size_t buflen;
  174. size_t i;
  175. ie = mgmt->u.probe_req.variable;
  176. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
  177. return;
  178. ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
  179. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
  180. if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  181. mgmt->sa, ie, ie_len) > 0)
  182. return;
  183. if (!hapd->iconf->send_probe_response)
  184. return;
  185. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  186. wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
  187. MAC2STR(mgmt->sa));
  188. return;
  189. }
  190. ssid = NULL;
  191. ssid_len = 0;
  192. if ((!elems.ssid || !elems.supp_rates)) {
  193. wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
  194. "without SSID or supported rates element",
  195. MAC2STR(mgmt->sa));
  196. return;
  197. }
  198. #ifdef CONFIG_P2P
  199. if (hapd->p2p && elems.wps_ie) {
  200. struct wpabuf *wps;
  201. wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  202. if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
  203. wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
  204. "due to mismatch with Requested Device "
  205. "Type");
  206. wpabuf_free(wps);
  207. return;
  208. }
  209. wpabuf_free(wps);
  210. }
  211. #endif /* CONFIG_P2P */
  212. if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0) {
  213. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
  214. "broadcast SSID ignored", MAC2STR(mgmt->sa));
  215. return;
  216. }
  217. sta = ap_get_sta(hapd, mgmt->sa);
  218. #ifdef CONFIG_P2P
  219. if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
  220. elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
  221. os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
  222. P2P_WILDCARD_SSID_LEN) == 0) {
  223. /* Process P2P Wildcard SSID like Wildcard SSID */
  224. elems.ssid_len = 0;
  225. }
  226. #endif /* CONFIG_P2P */
  227. if (elems.ssid_len == 0 ||
  228. (elems.ssid_len == hapd->conf->ssid.ssid_len &&
  229. os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) ==
  230. 0)) {
  231. ssid = hapd->conf->ssid.ssid;
  232. ssid_len = hapd->conf->ssid.ssid_len;
  233. if (sta)
  234. sta->ssid_probe = &hapd->conf->ssid;
  235. }
  236. if (!ssid) {
  237. if (!(mgmt->da[0] & 0x01)) {
  238. char ssid_txt[33];
  239. ieee802_11_print_ssid(ssid_txt, elems.ssid,
  240. elems.ssid_len);
  241. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  242. " for foreign SSID '%s' (DA " MACSTR ")",
  243. MAC2STR(mgmt->sa), ssid_txt,
  244. MAC2STR(mgmt->da));
  245. }
  246. return;
  247. }
  248. /* TODO: verify that supp_rates contains at least one matching rate
  249. * with AP configuration */
  250. #define MAX_PROBERESP_LEN 768
  251. buflen = MAX_PROBERESP_LEN;
  252. #ifdef CONFIG_WPS
  253. if (hapd->wps_probe_resp_ie)
  254. buflen += wpabuf_len(hapd->wps_probe_resp_ie);
  255. #endif /* CONFIG_WPS */
  256. #ifdef CONFIG_P2P
  257. if (hapd->p2p_probe_resp_ie)
  258. buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
  259. #endif /* CONFIG_P2P */
  260. resp = os_zalloc(buflen);
  261. if (resp == NULL)
  262. return;
  263. epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
  264. resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  265. WLAN_FC_STYPE_PROBE_RESP);
  266. os_memcpy(resp->da, mgmt->sa, ETH_ALEN);
  267. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  268. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  269. resp->u.probe_resp.beacon_int =
  270. host_to_le16(hapd->iconf->beacon_int);
  271. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  272. resp->u.probe_resp.capab_info =
  273. host_to_le16(hostapd_own_capab_info(hapd, sta, 1));
  274. pos = resp->u.probe_resp.variable;
  275. *pos++ = WLAN_EID_SSID;
  276. *pos++ = ssid_len;
  277. os_memcpy(pos, ssid, ssid_len);
  278. pos += ssid_len;
  279. /* Supported rates */
  280. pos = hostapd_eid_supp_rates(hapd, pos);
  281. /* DS Params */
  282. pos = hostapd_eid_ds_params(hapd, pos);
  283. pos = hostapd_eid_country(hapd, pos, epos - pos);
  284. /* ERP Information element */
  285. pos = hostapd_eid_erp_info(hapd, pos);
  286. /* Extended supported rates */
  287. pos = hostapd_eid_ext_supp_rates(hapd, pos);
  288. /* RSN, MDIE, WPA */
  289. pos = hostapd_eid_wpa(hapd, pos, epos - pos, sta);
  290. #ifdef CONFIG_IEEE80211N
  291. pos = hostapd_eid_ht_capabilities(hapd, pos);
  292. pos = hostapd_eid_ht_operation(hapd, pos);
  293. #endif /* CONFIG_IEEE80211N */
  294. pos = hostapd_eid_ext_capab(hapd, pos);
  295. /* Wi-Fi Alliance WMM */
  296. pos = hostapd_eid_wmm(hapd, pos);
  297. #ifdef CONFIG_WPS
  298. if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
  299. os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
  300. wpabuf_len(hapd->wps_probe_resp_ie));
  301. pos += wpabuf_len(hapd->wps_probe_resp_ie);
  302. }
  303. #endif /* CONFIG_WPS */
  304. #ifdef CONFIG_P2P
  305. if ((hapd->conf->p2p & P2P_ENABLED) && elems.p2p &&
  306. hapd->p2p_probe_resp_ie) {
  307. os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
  308. wpabuf_len(hapd->p2p_probe_resp_ie));
  309. pos += wpabuf_len(hapd->p2p_probe_resp_ie);
  310. }
  311. #endif /* CONFIG_P2P */
  312. #ifdef CONFIG_P2P_MANAGER
  313. if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
  314. P2P_MANAGE)
  315. pos = hostapd_eid_p2p_manage(hapd, pos);
  316. #endif /* CONFIG_P2P_MANAGER */
  317. if (hostapd_drv_send_mlme(hapd, resp, pos - (u8 *) resp) < 0)
  318. perror("handle_probe_req: send");
  319. os_free(resp);
  320. wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
  321. "SSID", MAC2STR(mgmt->sa),
  322. elems.ssid_len == 0 ? "broadcast" : "our");
  323. }
  324. void ieee802_11_set_beacon(struct hostapd_data *hapd)
  325. {
  326. struct ieee80211_mgmt *head;
  327. u8 *pos, *tail, *tailpos;
  328. u16 capab_info;
  329. size_t head_len, tail_len;
  330. #ifdef CONFIG_P2P
  331. if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) == P2P_ENABLED)
  332. goto no_beacon;
  333. #endif /* CONFIG_P2P */
  334. #define BEACON_HEAD_BUF_SIZE 256
  335. #define BEACON_TAIL_BUF_SIZE 512
  336. head = os_zalloc(BEACON_HEAD_BUF_SIZE);
  337. tail_len = BEACON_TAIL_BUF_SIZE;
  338. #ifdef CONFIG_WPS
  339. if (hapd->conf->wps_state && hapd->wps_beacon_ie)
  340. tail_len += wpabuf_len(hapd->wps_beacon_ie);
  341. #endif /* CONFIG_WPS */
  342. #ifdef CONFIG_P2P
  343. if (hapd->p2p_beacon_ie)
  344. tail_len += wpabuf_len(hapd->p2p_beacon_ie);
  345. #endif /* CONFIG_P2P */
  346. tailpos = tail = os_malloc(tail_len);
  347. if (head == NULL || tail == NULL) {
  348. wpa_printf(MSG_ERROR, "Failed to set beacon data");
  349. os_free(head);
  350. os_free(tail);
  351. return;
  352. }
  353. head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  354. WLAN_FC_STYPE_BEACON);
  355. head->duration = host_to_le16(0);
  356. os_memset(head->da, 0xff, ETH_ALEN);
  357. os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
  358. os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
  359. head->u.beacon.beacon_int =
  360. host_to_le16(hapd->iconf->beacon_int);
  361. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  362. capab_info = hostapd_own_capab_info(hapd, NULL, 0);
  363. head->u.beacon.capab_info = host_to_le16(capab_info);
  364. pos = &head->u.beacon.variable[0];
  365. /* SSID */
  366. *pos++ = WLAN_EID_SSID;
  367. if (hapd->conf->ignore_broadcast_ssid == 2) {
  368. /* clear the data, but keep the correct length of the SSID */
  369. *pos++ = hapd->conf->ssid.ssid_len;
  370. os_memset(pos, 0, hapd->conf->ssid.ssid_len);
  371. pos += hapd->conf->ssid.ssid_len;
  372. } else if (hapd->conf->ignore_broadcast_ssid) {
  373. *pos++ = 0; /* empty SSID */
  374. } else {
  375. *pos++ = hapd->conf->ssid.ssid_len;
  376. os_memcpy(pos, hapd->conf->ssid.ssid,
  377. hapd->conf->ssid.ssid_len);
  378. pos += hapd->conf->ssid.ssid_len;
  379. }
  380. /* Supported rates */
  381. pos = hostapd_eid_supp_rates(hapd, pos);
  382. /* DS Params */
  383. pos = hostapd_eid_ds_params(hapd, pos);
  384. head_len = pos - (u8 *) head;
  385. tailpos = hostapd_eid_country(hapd, tailpos,
  386. tail + BEACON_TAIL_BUF_SIZE - tailpos);
  387. /* ERP Information element */
  388. tailpos = hostapd_eid_erp_info(hapd, tailpos);
  389. /* Extended supported rates */
  390. tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
  391. /* RSN, MDIE, WPA */
  392. tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
  393. tailpos, NULL);
  394. #ifdef CONFIG_IEEE80211N
  395. tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
  396. tailpos = hostapd_eid_ht_operation(hapd, tailpos);
  397. //DRIVER_RTW ADD
  398. if(hapd->iconf->ieee80211n)
  399. hapd->conf->wmm_enabled = 1;
  400. #endif /* CONFIG_IEEE80211N */
  401. tailpos = hostapd_eid_ext_capab(hapd, tailpos);
  402. /* Wi-Fi Alliance WMM */
  403. tailpos = hostapd_eid_wmm(hapd, tailpos);
  404. #ifdef CONFIG_WPS
  405. if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
  406. os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
  407. wpabuf_len(hapd->wps_beacon_ie));
  408. tailpos += wpabuf_len(hapd->wps_beacon_ie);
  409. }
  410. #endif /* CONFIG_WPS */
  411. #ifdef CONFIG_P2P
  412. if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
  413. os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
  414. wpabuf_len(hapd->p2p_beacon_ie));
  415. tailpos += wpabuf_len(hapd->p2p_beacon_ie);
  416. }
  417. #endif /* CONFIG_P2P */
  418. #ifdef CONFIG_P2P_MANAGER
  419. if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
  420. P2P_MANAGE)
  421. tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
  422. #endif /* CONFIG_P2P_MANAGER */
  423. tail_len = tailpos > tail ? tailpos - tail : 0;
  424. if (hostapd_drv_set_beacon(hapd, (u8 *) head, head_len,
  425. tail, tail_len, hapd->conf->dtim_period,
  426. hapd->iconf->beacon_int))
  427. wpa_printf(MSG_ERROR, "Failed to set beacon head/tail or DTIM "
  428. "period");
  429. os_free(tail);
  430. os_free(head);
  431. #ifdef CONFIG_P2P
  432. no_beacon:
  433. #endif /* CONFIG_P2P */
  434. hostapd_set_bss_params(hapd, !!(ieee802_11_erp_info(hapd) &
  435. ERP_INFO_USE_PROTECTION));
  436. }
  437. void ieee802_11_set_beacons(struct hostapd_iface *iface)
  438. {
  439. size_t i;
  440. for (i = 0; i < iface->num_bss; i++)
  441. ieee802_11_set_beacon(iface->bss[i]);
  442. }
  443. #endif /* CONFIG_NATIVE_WINDOWS */