beacon.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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-2012, Jouni Malinen <j@w1.fi>
  6. *
  7. * This software may be distributed under the terms of the BSD license.
  8. * See README for more details.
  9. */
  10. #include "utils/includes.h"
  11. #ifndef CONFIG_NATIVE_WINDOWS
  12. #include "utils/common.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/hw_features_common.h"
  16. #include "wps/wps_defs.h"
  17. #include "p2p/p2p.h"
  18. #include "hostapd.h"
  19. #include "ieee802_11.h"
  20. #include "wpa_auth.h"
  21. #include "wmm.h"
  22. #include "ap_config.h"
  23. #include "sta_info.h"
  24. #include "p2p_hostapd.h"
  25. #include "ap_drv_ops.h"
  26. #include "beacon.h"
  27. #include "hs20.h"
  28. #include "dfs.h"
  29. #ifdef NEED_AP_MLME
  30. static u8 * hostapd_eid_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
  31. size_t len)
  32. {
  33. if (!hapd->conf->radio_measurements || len < 2 + 4)
  34. return eid;
  35. *eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
  36. *eid++ = 5;
  37. *eid++ = (hapd->conf->radio_measurements & BIT(0)) ?
  38. WLAN_RRM_CAPS_NEIGHBOR_REPORT : 0x00;
  39. *eid++ = 0x00;
  40. *eid++ = 0x00;
  41. *eid++ = 0x00;
  42. *eid++ = 0x00;
  43. return eid;
  44. }
  45. static u8 * hostapd_eid_bss_load(struct hostapd_data *hapd, u8 *eid, size_t len)
  46. {
  47. if (len < 2 + 5)
  48. return eid;
  49. #ifdef CONFIG_TESTING_OPTIONS
  50. if (hapd->conf->bss_load_test_set) {
  51. *eid++ = WLAN_EID_BSS_LOAD;
  52. *eid++ = 5;
  53. os_memcpy(eid, hapd->conf->bss_load_test, 5);
  54. eid += 5;
  55. return eid;
  56. }
  57. #endif /* CONFIG_TESTING_OPTIONS */
  58. if (hapd->conf->bss_load_update_period) {
  59. *eid++ = WLAN_EID_BSS_LOAD;
  60. *eid++ = 5;
  61. WPA_PUT_LE16(eid, hapd->num_sta);
  62. eid += 2;
  63. *eid++ = hapd->iface->channel_utilization;
  64. WPA_PUT_LE16(eid, 0); /* no available admission capabity */
  65. eid += 2;
  66. }
  67. return eid;
  68. }
  69. static u8 ieee802_11_erp_info(struct hostapd_data *hapd)
  70. {
  71. u8 erp = 0;
  72. if (hapd->iface->current_mode == NULL ||
  73. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  74. return 0;
  75. if (hapd->iface->olbc)
  76. erp |= ERP_INFO_USE_PROTECTION;
  77. if (hapd->iface->num_sta_non_erp > 0) {
  78. erp |= ERP_INFO_NON_ERP_PRESENT |
  79. ERP_INFO_USE_PROTECTION;
  80. }
  81. if (hapd->iface->num_sta_no_short_preamble > 0 ||
  82. hapd->iconf->preamble == LONG_PREAMBLE)
  83. erp |= ERP_INFO_BARKER_PREAMBLE_MODE;
  84. return erp;
  85. }
  86. static u8 * hostapd_eid_ds_params(struct hostapd_data *hapd, u8 *eid)
  87. {
  88. *eid++ = WLAN_EID_DS_PARAMS;
  89. *eid++ = 1;
  90. *eid++ = hapd->iconf->channel;
  91. return eid;
  92. }
  93. static u8 * hostapd_eid_erp_info(struct hostapd_data *hapd, u8 *eid)
  94. {
  95. if (hapd->iface->current_mode == NULL ||
  96. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
  97. return eid;
  98. /* Set NonERP_present and use_protection bits if there
  99. * are any associated NonERP stations. */
  100. /* TODO: use_protection bit can be set to zero even if
  101. * there are NonERP stations present. This optimization
  102. * might be useful if NonERP stations are "quiet".
  103. * See 802.11g/D6 E-1 for recommended practice.
  104. * In addition, Non ERP present might be set, if AP detects Non ERP
  105. * operation on other APs. */
  106. /* Add ERP Information element */
  107. *eid++ = WLAN_EID_ERP_INFO;
  108. *eid++ = 1;
  109. *eid++ = ieee802_11_erp_info(hapd);
  110. return eid;
  111. }
  112. static u8 * hostapd_eid_pwr_constraint(struct hostapd_data *hapd, u8 *eid)
  113. {
  114. u8 *pos = eid;
  115. u8 local_pwr_constraint = 0;
  116. int dfs;
  117. if (hapd->iface->current_mode == NULL ||
  118. hapd->iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A)
  119. return eid;
  120. /* Let host drivers add this IE if DFS support is offloaded */
  121. if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)
  122. return eid;
  123. /*
  124. * There is no DFS support and power constraint was not directly
  125. * requested by config option.
  126. */
  127. if (!hapd->iconf->ieee80211h &&
  128. hapd->iconf->local_pwr_constraint == -1)
  129. return eid;
  130. /* Check if DFS is required by regulatory. */
  131. dfs = hostapd_is_dfs_required(hapd->iface);
  132. if (dfs < 0) {
  133. wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
  134. dfs);
  135. dfs = 0;
  136. }
  137. if (dfs == 0 && hapd->iconf->local_pwr_constraint == -1)
  138. return eid;
  139. /*
  140. * ieee80211h (DFS) is enabled so Power Constraint element shall
  141. * be added when running on DFS channel whenever local_pwr_constraint
  142. * is configured or not. In order to meet regulations when TPC is not
  143. * implemented using a transmit power that is below the legal maximum
  144. * (including any mitigation factor) should help. In this case,
  145. * indicate 3 dB below maximum allowed transmit power.
  146. */
  147. if (hapd->iconf->local_pwr_constraint == -1)
  148. local_pwr_constraint = 3;
  149. /*
  150. * A STA that is not an AP shall use a transmit power less than or
  151. * equal to the local maximum transmit power level for the channel.
  152. * The local maximum transmit power can be calculated from the formula:
  153. * local max TX pwr = max TX pwr - local pwr constraint
  154. * Where max TX pwr is maximum transmit power level specified for
  155. * channel in Country element and local pwr constraint is specified
  156. * for channel in this Power Constraint element.
  157. */
  158. /* Element ID */
  159. *pos++ = WLAN_EID_PWR_CONSTRAINT;
  160. /* Length */
  161. *pos++ = 1;
  162. /* Local Power Constraint */
  163. if (local_pwr_constraint)
  164. *pos++ = local_pwr_constraint;
  165. else
  166. *pos++ = hapd->iconf->local_pwr_constraint;
  167. return pos;
  168. }
  169. static u8 * hostapd_eid_country_add(u8 *pos, u8 *end, int chan_spacing,
  170. struct hostapd_channel_data *start,
  171. struct hostapd_channel_data *prev)
  172. {
  173. if (end - pos < 3)
  174. return pos;
  175. /* first channel number */
  176. *pos++ = start->chan;
  177. /* number of channels */
  178. *pos++ = (prev->chan - start->chan) / chan_spacing + 1;
  179. /* maximum transmit power level */
  180. *pos++ = start->max_tx_power;
  181. return pos;
  182. }
  183. static u8 * hostapd_eid_country(struct hostapd_data *hapd, u8 *eid,
  184. int max_len)
  185. {
  186. u8 *pos = eid;
  187. u8 *end = eid + max_len;
  188. int i;
  189. struct hostapd_hw_modes *mode;
  190. struct hostapd_channel_data *start, *prev;
  191. int chan_spacing = 1;
  192. if (!hapd->iconf->ieee80211d || max_len < 6 ||
  193. hapd->iface->current_mode == NULL)
  194. return eid;
  195. *pos++ = WLAN_EID_COUNTRY;
  196. pos++; /* length will be set later */
  197. os_memcpy(pos, hapd->iconf->country, 3); /* e.g., 'US ' */
  198. pos += 3;
  199. mode = hapd->iface->current_mode;
  200. if (mode->mode == HOSTAPD_MODE_IEEE80211A)
  201. chan_spacing = 4;
  202. start = prev = NULL;
  203. for (i = 0; i < mode->num_channels; i++) {
  204. struct hostapd_channel_data *chan = &mode->channels[i];
  205. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  206. continue;
  207. if (start && prev &&
  208. prev->chan + chan_spacing == chan->chan &&
  209. start->max_tx_power == chan->max_tx_power) {
  210. prev = chan;
  211. continue; /* can use same entry */
  212. }
  213. if (start && prev) {
  214. pos = hostapd_eid_country_add(pos, end, chan_spacing,
  215. start, prev);
  216. start = NULL;
  217. }
  218. /* Start new group */
  219. start = prev = chan;
  220. }
  221. if (start) {
  222. pos = hostapd_eid_country_add(pos, end, chan_spacing,
  223. start, prev);
  224. }
  225. if ((pos - eid) & 1) {
  226. if (end - pos < 1)
  227. return eid;
  228. *pos++ = 0; /* pad for 16-bit alignment */
  229. }
  230. eid[1] = (pos - eid) - 2;
  231. return pos;
  232. }
  233. static u8 * hostapd_eid_wpa(struct hostapd_data *hapd, u8 *eid, size_t len)
  234. {
  235. const u8 *ie;
  236. size_t ielen;
  237. ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
  238. if (ie == NULL || ielen > len)
  239. return eid;
  240. os_memcpy(eid, ie, ielen);
  241. return eid + ielen;
  242. }
  243. static u8 * hostapd_eid_csa(struct hostapd_data *hapd, u8 *eid)
  244. {
  245. u8 chan;
  246. if (!hapd->cs_freq_params.freq)
  247. return eid;
  248. if (ieee80211_freq_to_chan(hapd->cs_freq_params.freq, &chan) ==
  249. NUM_HOSTAPD_MODES)
  250. return eid;
  251. *eid++ = WLAN_EID_CHANNEL_SWITCH;
  252. *eid++ = 3;
  253. *eid++ = hapd->cs_block_tx;
  254. *eid++ = chan;
  255. *eid++ = hapd->cs_count;
  256. return eid;
  257. }
  258. static u8 * hostapd_eid_secondary_channel(struct hostapd_data *hapd, u8 *eid)
  259. {
  260. u8 sec_ch;
  261. if (!hapd->cs_freq_params.sec_channel_offset)
  262. return eid;
  263. if (hapd->cs_freq_params.sec_channel_offset == -1)
  264. sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW;
  265. else if (hapd->cs_freq_params.sec_channel_offset == 1)
  266. sec_ch = HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE;
  267. else
  268. return eid;
  269. *eid++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
  270. *eid++ = 1;
  271. *eid++ = sec_ch;
  272. return eid;
  273. }
  274. static u8 * hostapd_add_csa_elems(struct hostapd_data *hapd, u8 *pos,
  275. u8 *start, unsigned int *csa_counter_off)
  276. {
  277. u8 *old_pos = pos;
  278. if (!csa_counter_off)
  279. return pos;
  280. *csa_counter_off = 0;
  281. pos = hostapd_eid_csa(hapd, pos);
  282. if (pos != old_pos) {
  283. /* save an offset to the counter - should be last byte */
  284. *csa_counter_off = pos - start - 1;
  285. pos = hostapd_eid_secondary_channel(hapd, pos);
  286. }
  287. return pos;
  288. }
  289. static u8 * hostapd_gen_probe_resp(struct hostapd_data *hapd,
  290. const struct ieee80211_mgmt *req,
  291. int is_p2p, size_t *resp_len)
  292. {
  293. struct ieee80211_mgmt *resp;
  294. u8 *pos, *epos;
  295. size_t buflen;
  296. #define MAX_PROBERESP_LEN 768
  297. buflen = MAX_PROBERESP_LEN;
  298. #ifdef CONFIG_WPS
  299. if (hapd->wps_probe_resp_ie)
  300. buflen += wpabuf_len(hapd->wps_probe_resp_ie);
  301. #endif /* CONFIG_WPS */
  302. #ifdef CONFIG_P2P
  303. if (hapd->p2p_probe_resp_ie)
  304. buflen += wpabuf_len(hapd->p2p_probe_resp_ie);
  305. #endif /* CONFIG_P2P */
  306. if (hapd->conf->vendor_elements)
  307. buflen += wpabuf_len(hapd->conf->vendor_elements);
  308. if (hapd->conf->vendor_vht) {
  309. buflen += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
  310. 2 + sizeof(struct ieee80211_vht_operation);
  311. }
  312. resp = os_zalloc(buflen);
  313. if (resp == NULL)
  314. return NULL;
  315. epos = ((u8 *) resp) + MAX_PROBERESP_LEN;
  316. resp->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  317. WLAN_FC_STYPE_PROBE_RESP);
  318. if (req)
  319. os_memcpy(resp->da, req->sa, ETH_ALEN);
  320. os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
  321. os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
  322. resp->u.probe_resp.beacon_int =
  323. host_to_le16(hapd->iconf->beacon_int);
  324. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  325. resp->u.probe_resp.capab_info =
  326. host_to_le16(hostapd_own_capab_info(hapd));
  327. pos = resp->u.probe_resp.variable;
  328. *pos++ = WLAN_EID_SSID;
  329. *pos++ = hapd->conf->ssid.ssid_len;
  330. os_memcpy(pos, hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len);
  331. pos += hapd->conf->ssid.ssid_len;
  332. /* Supported rates */
  333. pos = hostapd_eid_supp_rates(hapd, pos);
  334. /* DS Params */
  335. pos = hostapd_eid_ds_params(hapd, pos);
  336. pos = hostapd_eid_country(hapd, pos, epos - pos);
  337. /* Power Constraint element */
  338. pos = hostapd_eid_pwr_constraint(hapd, pos);
  339. /* ERP Information element */
  340. pos = hostapd_eid_erp_info(hapd, pos);
  341. /* Extended supported rates */
  342. pos = hostapd_eid_ext_supp_rates(hapd, pos);
  343. /* RSN, MDIE, WPA */
  344. pos = hostapd_eid_wpa(hapd, pos, epos - pos);
  345. pos = hostapd_eid_bss_load(hapd, pos, epos - pos);
  346. pos = hostapd_eid_rm_enabled_capab(hapd, pos, epos - pos);
  347. #ifdef CONFIG_IEEE80211N
  348. pos = hostapd_eid_ht_capabilities(hapd, pos);
  349. pos = hostapd_eid_ht_operation(hapd, pos);
  350. #endif /* CONFIG_IEEE80211N */
  351. pos = hostapd_eid_ext_capab(hapd, pos);
  352. pos = hostapd_eid_time_adv(hapd, pos);
  353. pos = hostapd_eid_time_zone(hapd, pos);
  354. pos = hostapd_eid_interworking(hapd, pos);
  355. pos = hostapd_eid_adv_proto(hapd, pos);
  356. pos = hostapd_eid_roaming_consortium(hapd, pos);
  357. pos = hostapd_add_csa_elems(hapd, pos, (u8 *)resp,
  358. &hapd->cs_c_off_proberesp);
  359. #ifdef CONFIG_IEEE80211AC
  360. if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
  361. pos = hostapd_eid_vht_capabilities(hapd, pos);
  362. pos = hostapd_eid_vht_operation(hapd, pos);
  363. }
  364. if (hapd->conf->vendor_vht)
  365. pos = hostapd_eid_vendor_vht(hapd, pos);
  366. #endif /* CONFIG_IEEE80211AC */
  367. /* Wi-Fi Alliance WMM */
  368. pos = hostapd_eid_wmm(hapd, pos);
  369. #ifdef CONFIG_WPS
  370. if (hapd->conf->wps_state && hapd->wps_probe_resp_ie) {
  371. os_memcpy(pos, wpabuf_head(hapd->wps_probe_resp_ie),
  372. wpabuf_len(hapd->wps_probe_resp_ie));
  373. pos += wpabuf_len(hapd->wps_probe_resp_ie);
  374. }
  375. #endif /* CONFIG_WPS */
  376. #ifdef CONFIG_P2P
  377. if ((hapd->conf->p2p & P2P_ENABLED) && is_p2p &&
  378. hapd->p2p_probe_resp_ie) {
  379. os_memcpy(pos, wpabuf_head(hapd->p2p_probe_resp_ie),
  380. wpabuf_len(hapd->p2p_probe_resp_ie));
  381. pos += wpabuf_len(hapd->p2p_probe_resp_ie);
  382. }
  383. #endif /* CONFIG_P2P */
  384. #ifdef CONFIG_P2P_MANAGER
  385. if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
  386. P2P_MANAGE)
  387. pos = hostapd_eid_p2p_manage(hapd, pos);
  388. #endif /* CONFIG_P2P_MANAGER */
  389. #ifdef CONFIG_HS20
  390. pos = hostapd_eid_hs20_indication(hapd, pos);
  391. pos = hostapd_eid_osen(hapd, pos);
  392. #endif /* CONFIG_HS20 */
  393. if (hapd->conf->vendor_elements) {
  394. os_memcpy(pos, wpabuf_head(hapd->conf->vendor_elements),
  395. wpabuf_len(hapd->conf->vendor_elements));
  396. pos += wpabuf_len(hapd->conf->vendor_elements);
  397. }
  398. *resp_len = pos - (u8 *) resp;
  399. return (u8 *) resp;
  400. }
  401. enum ssid_match_result {
  402. NO_SSID_MATCH,
  403. EXACT_SSID_MATCH,
  404. WILDCARD_SSID_MATCH
  405. };
  406. static enum ssid_match_result ssid_match(struct hostapd_data *hapd,
  407. const u8 *ssid, size_t ssid_len,
  408. const u8 *ssid_list,
  409. size_t ssid_list_len)
  410. {
  411. const u8 *pos, *end;
  412. int wildcard = 0;
  413. if (ssid_len == 0)
  414. wildcard = 1;
  415. if (ssid_len == hapd->conf->ssid.ssid_len &&
  416. os_memcmp(ssid, hapd->conf->ssid.ssid, ssid_len) == 0)
  417. return EXACT_SSID_MATCH;
  418. if (ssid_list == NULL)
  419. return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
  420. pos = ssid_list;
  421. end = ssid_list + ssid_list_len;
  422. while (pos + 1 <= end) {
  423. if (pos + 2 + pos[1] > end)
  424. break;
  425. if (pos[1] == 0)
  426. wildcard = 1;
  427. if (pos[1] == hapd->conf->ssid.ssid_len &&
  428. os_memcmp(pos + 2, hapd->conf->ssid.ssid, pos[1]) == 0)
  429. return EXACT_SSID_MATCH;
  430. pos += 2 + pos[1];
  431. }
  432. return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
  433. }
  434. void handle_probe_req(struct hostapd_data *hapd,
  435. const struct ieee80211_mgmt *mgmt, size_t len,
  436. int ssi_signal)
  437. {
  438. u8 *resp;
  439. struct ieee802_11_elems elems;
  440. const u8 *ie;
  441. size_t ie_len;
  442. size_t i, resp_len;
  443. int noack;
  444. enum ssid_match_result res;
  445. ie = mgmt->u.probe_req.variable;
  446. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req))
  447. return;
  448. ie_len = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.probe_req));
  449. for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++)
  450. if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
  451. mgmt->sa, mgmt->da, mgmt->bssid,
  452. ie, ie_len, ssi_signal) > 0)
  453. return;
  454. if (!hapd->iconf->send_probe_response)
  455. return;
  456. if (ieee802_11_parse_elems(ie, ie_len, &elems, 0) == ParseFailed) {
  457. wpa_printf(MSG_DEBUG, "Could not parse ProbeReq from " MACSTR,
  458. MAC2STR(mgmt->sa));
  459. return;
  460. }
  461. if ((!elems.ssid || !elems.supp_rates)) {
  462. wpa_printf(MSG_DEBUG, "STA " MACSTR " sent probe request "
  463. "without SSID or supported rates element",
  464. MAC2STR(mgmt->sa));
  465. return;
  466. }
  467. /*
  468. * No need to reply if the Probe Request frame was sent on an adjacent
  469. * channel. IEEE Std 802.11-2012 describes this as a requirement for an
  470. * AP with dot11RadioMeasurementActivated set to true, but strictly
  471. * speaking does not allow such ignoring of Probe Request frames if
  472. * dot11RadioMeasurementActivated is false. Anyway, this can help reduce
  473. * number of unnecessary Probe Response frames for cases where the STA
  474. * is less likely to see them (Probe Request frame sent on a
  475. * neighboring, but partially overlapping, channel).
  476. */
  477. if (elems.ds_params && elems.ds_params_len == 1 &&
  478. hapd->iface->current_mode &&
  479. (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G ||
  480. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B) &&
  481. hapd->iconf->channel != elems.ds_params[0]) {
  482. wpa_printf(MSG_DEBUG,
  483. "Ignore Probe Request due to DS Params mismatch: chan=%u != ds.chan=%u",
  484. hapd->iconf->channel, elems.ds_params[0]);
  485. return;
  486. }
  487. #ifdef CONFIG_P2P
  488. if (hapd->p2p && elems.wps_ie) {
  489. struct wpabuf *wps;
  490. wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
  491. if (wps && !p2p_group_match_dev_type(hapd->p2p_group, wps)) {
  492. wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
  493. "due to mismatch with Requested Device "
  494. "Type");
  495. wpabuf_free(wps);
  496. return;
  497. }
  498. wpabuf_free(wps);
  499. }
  500. if (hapd->p2p && elems.p2p) {
  501. struct wpabuf *p2p;
  502. p2p = ieee802_11_vendor_ie_concat(ie, ie_len, P2P_IE_VENDOR_TYPE);
  503. if (p2p && !p2p_group_match_dev_id(hapd->p2p_group, p2p)) {
  504. wpa_printf(MSG_MSGDUMP, "P2P: Ignore Probe Request "
  505. "due to mismatch with Device ID");
  506. wpabuf_free(p2p);
  507. return;
  508. }
  509. wpabuf_free(p2p);
  510. }
  511. #endif /* CONFIG_P2P */
  512. if (hapd->conf->ignore_broadcast_ssid && elems.ssid_len == 0 &&
  513. elems.ssid_list_len == 0) {
  514. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR " for "
  515. "broadcast SSID ignored", MAC2STR(mgmt->sa));
  516. return;
  517. }
  518. #ifdef CONFIG_P2P
  519. if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
  520. elems.ssid_len == P2P_WILDCARD_SSID_LEN &&
  521. os_memcmp(elems.ssid, P2P_WILDCARD_SSID,
  522. P2P_WILDCARD_SSID_LEN) == 0) {
  523. /* Process P2P Wildcard SSID like Wildcard SSID */
  524. elems.ssid_len = 0;
  525. }
  526. #endif /* CONFIG_P2P */
  527. res = ssid_match(hapd, elems.ssid, elems.ssid_len,
  528. elems.ssid_list, elems.ssid_list_len);
  529. if (res == NO_SSID_MATCH) {
  530. if (!(mgmt->da[0] & 0x01)) {
  531. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  532. " for foreign SSID '%s' (DA " MACSTR ")%s",
  533. MAC2STR(mgmt->sa),
  534. wpa_ssid_txt(elems.ssid, elems.ssid_len),
  535. MAC2STR(mgmt->da),
  536. elems.ssid_list ? " (SSID list)" : "");
  537. }
  538. return;
  539. }
  540. #ifdef CONFIG_INTERWORKING
  541. if (hapd->conf->interworking &&
  542. elems.interworking && elems.interworking_len >= 1) {
  543. u8 ant = elems.interworking[0] & 0x0f;
  544. if (ant != INTERWORKING_ANT_WILDCARD &&
  545. ant != hapd->conf->access_network_type) {
  546. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  547. " for mismatching ANT %u ignored",
  548. MAC2STR(mgmt->sa), ant);
  549. return;
  550. }
  551. }
  552. if (hapd->conf->interworking && elems.interworking &&
  553. (elems.interworking_len == 7 || elems.interworking_len == 9)) {
  554. const u8 *hessid;
  555. if (elems.interworking_len == 7)
  556. hessid = elems.interworking + 1;
  557. else
  558. hessid = elems.interworking + 1 + 2;
  559. if (!is_broadcast_ether_addr(hessid) &&
  560. os_memcmp(hessid, hapd->conf->hessid, ETH_ALEN) != 0) {
  561. wpa_printf(MSG_MSGDUMP, "Probe Request from " MACSTR
  562. " for mismatching HESSID " MACSTR
  563. " ignored",
  564. MAC2STR(mgmt->sa), MAC2STR(hessid));
  565. return;
  566. }
  567. }
  568. #endif /* CONFIG_INTERWORKING */
  569. #ifdef CONFIG_P2P
  570. if ((hapd->conf->p2p & P2P_GROUP_OWNER) &&
  571. supp_rates_11b_only(&elems)) {
  572. /* Indicates support for 11b rates only */
  573. wpa_printf(MSG_EXCESSIVE, "P2P: Ignore Probe Request from "
  574. MACSTR " with only 802.11b rates",
  575. MAC2STR(mgmt->sa));
  576. return;
  577. }
  578. #endif /* CONFIG_P2P */
  579. /* TODO: verify that supp_rates contains at least one matching rate
  580. * with AP configuration */
  581. #ifdef CONFIG_TESTING_OPTIONS
  582. if (hapd->iconf->ignore_probe_probability > 0.0 &&
  583. drand48() < hapd->iconf->ignore_probe_probability) {
  584. wpa_printf(MSG_INFO,
  585. "TESTING: ignoring probe request from " MACSTR,
  586. MAC2STR(mgmt->sa));
  587. return;
  588. }
  589. #endif /* CONFIG_TESTING_OPTIONS */
  590. resp = hostapd_gen_probe_resp(hapd, mgmt, elems.p2p != NULL,
  591. &resp_len);
  592. if (resp == NULL)
  593. return;
  594. /*
  595. * If this is a broadcast probe request, apply no ack policy to avoid
  596. * excessive retries.
  597. */
  598. noack = !!(res == WILDCARD_SSID_MATCH &&
  599. is_broadcast_ether_addr(mgmt->da));
  600. if (hostapd_drv_send_mlme(hapd, resp, resp_len, noack) < 0)
  601. wpa_printf(MSG_INFO, "handle_probe_req: send failed");
  602. os_free(resp);
  603. wpa_printf(MSG_EXCESSIVE, "STA " MACSTR " sent probe request for %s "
  604. "SSID", MAC2STR(mgmt->sa),
  605. elems.ssid_len == 0 ? "broadcast" : "our");
  606. }
  607. static u8 * hostapd_probe_resp_offloads(struct hostapd_data *hapd,
  608. size_t *resp_len)
  609. {
  610. /* check probe response offloading caps and print warnings */
  611. if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD))
  612. return NULL;
  613. #ifdef CONFIG_WPS
  614. if (hapd->conf->wps_state && hapd->wps_probe_resp_ie &&
  615. (!(hapd->iface->probe_resp_offloads &
  616. (WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS |
  617. WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2))))
  618. wpa_printf(MSG_WARNING, "Device is trying to offload WPS "
  619. "Probe Response while not supporting this");
  620. #endif /* CONFIG_WPS */
  621. #ifdef CONFIG_P2P
  622. if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_probe_resp_ie &&
  623. !(hapd->iface->probe_resp_offloads &
  624. WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P))
  625. wpa_printf(MSG_WARNING, "Device is trying to offload P2P "
  626. "Probe Response while not supporting this");
  627. #endif /* CONFIG_P2P */
  628. if (hapd->conf->interworking &&
  629. !(hapd->iface->probe_resp_offloads &
  630. WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING))
  631. wpa_printf(MSG_WARNING, "Device is trying to offload "
  632. "Interworking Probe Response while not supporting "
  633. "this");
  634. /* Generate a Probe Response template for the non-P2P case */
  635. return hostapd_gen_probe_resp(hapd, NULL, 0, resp_len);
  636. }
  637. #endif /* NEED_AP_MLME */
  638. int ieee802_11_build_ap_params(struct hostapd_data *hapd,
  639. struct wpa_driver_ap_params *params)
  640. {
  641. struct ieee80211_mgmt *head = NULL;
  642. u8 *tail = NULL;
  643. size_t head_len = 0, tail_len = 0;
  644. u8 *resp = NULL;
  645. size_t resp_len = 0;
  646. #ifdef NEED_AP_MLME
  647. u16 capab_info;
  648. u8 *pos, *tailpos;
  649. #define BEACON_HEAD_BUF_SIZE 256
  650. #define BEACON_TAIL_BUF_SIZE 512
  651. head = os_zalloc(BEACON_HEAD_BUF_SIZE);
  652. tail_len = BEACON_TAIL_BUF_SIZE;
  653. #ifdef CONFIG_WPS
  654. if (hapd->conf->wps_state && hapd->wps_beacon_ie)
  655. tail_len += wpabuf_len(hapd->wps_beacon_ie);
  656. #endif /* CONFIG_WPS */
  657. #ifdef CONFIG_P2P
  658. if (hapd->p2p_beacon_ie)
  659. tail_len += wpabuf_len(hapd->p2p_beacon_ie);
  660. #endif /* CONFIG_P2P */
  661. if (hapd->conf->vendor_elements)
  662. tail_len += wpabuf_len(hapd->conf->vendor_elements);
  663. #ifdef CONFIG_IEEE80211AC
  664. if (hapd->conf->vendor_vht) {
  665. tail_len += 5 + 2 + sizeof(struct ieee80211_vht_capabilities) +
  666. 2 + sizeof(struct ieee80211_vht_operation);
  667. }
  668. #endif /* CONFIG_IEEE80211AC */
  669. tailpos = tail = os_malloc(tail_len);
  670. if (head == NULL || tail == NULL) {
  671. wpa_printf(MSG_ERROR, "Failed to set beacon data");
  672. os_free(head);
  673. os_free(tail);
  674. return -1;
  675. }
  676. head->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  677. WLAN_FC_STYPE_BEACON);
  678. head->duration = host_to_le16(0);
  679. os_memset(head->da, 0xff, ETH_ALEN);
  680. os_memcpy(head->sa, hapd->own_addr, ETH_ALEN);
  681. os_memcpy(head->bssid, hapd->own_addr, ETH_ALEN);
  682. head->u.beacon.beacon_int =
  683. host_to_le16(hapd->iconf->beacon_int);
  684. /* hardware or low-level driver will setup seq_ctrl and timestamp */
  685. capab_info = hostapd_own_capab_info(hapd);
  686. head->u.beacon.capab_info = host_to_le16(capab_info);
  687. pos = &head->u.beacon.variable[0];
  688. /* SSID */
  689. *pos++ = WLAN_EID_SSID;
  690. if (hapd->conf->ignore_broadcast_ssid == 2) {
  691. /* clear the data, but keep the correct length of the SSID */
  692. *pos++ = hapd->conf->ssid.ssid_len;
  693. os_memset(pos, 0, hapd->conf->ssid.ssid_len);
  694. pos += hapd->conf->ssid.ssid_len;
  695. } else if (hapd->conf->ignore_broadcast_ssid) {
  696. *pos++ = 0; /* empty SSID */
  697. } else {
  698. *pos++ = hapd->conf->ssid.ssid_len;
  699. os_memcpy(pos, hapd->conf->ssid.ssid,
  700. hapd->conf->ssid.ssid_len);
  701. pos += hapd->conf->ssid.ssid_len;
  702. }
  703. /* Supported rates */
  704. pos = hostapd_eid_supp_rates(hapd, pos);
  705. /* DS Params */
  706. pos = hostapd_eid_ds_params(hapd, pos);
  707. head_len = pos - (u8 *) head;
  708. tailpos = hostapd_eid_country(hapd, tailpos,
  709. tail + BEACON_TAIL_BUF_SIZE - tailpos);
  710. /* Power Constraint element */
  711. tailpos = hostapd_eid_pwr_constraint(hapd, tailpos);
  712. /* ERP Information element */
  713. tailpos = hostapd_eid_erp_info(hapd, tailpos);
  714. /* Extended supported rates */
  715. tailpos = hostapd_eid_ext_supp_rates(hapd, tailpos);
  716. /* RSN, MDIE, WPA */
  717. tailpos = hostapd_eid_wpa(hapd, tailpos, tail + BEACON_TAIL_BUF_SIZE -
  718. tailpos);
  719. tailpos = hostapd_eid_rm_enabled_capab(hapd, tailpos,
  720. tail + BEACON_TAIL_BUF_SIZE -
  721. tailpos);
  722. tailpos = hostapd_eid_bss_load(hapd, tailpos,
  723. tail + BEACON_TAIL_BUF_SIZE - tailpos);
  724. #ifdef CONFIG_IEEE80211N
  725. tailpos = hostapd_eid_ht_capabilities(hapd, tailpos);
  726. tailpos = hostapd_eid_ht_operation(hapd, tailpos);
  727. #endif /* CONFIG_IEEE80211N */
  728. tailpos = hostapd_eid_ext_capab(hapd, tailpos);
  729. /*
  730. * TODO: Time Advertisement element should only be included in some
  731. * DTIM Beacon frames.
  732. */
  733. tailpos = hostapd_eid_time_adv(hapd, tailpos);
  734. tailpos = hostapd_eid_interworking(hapd, tailpos);
  735. tailpos = hostapd_eid_adv_proto(hapd, tailpos);
  736. tailpos = hostapd_eid_roaming_consortium(hapd, tailpos);
  737. tailpos = hostapd_add_csa_elems(hapd, tailpos, tail,
  738. &hapd->cs_c_off_beacon);
  739. #ifdef CONFIG_IEEE80211AC
  740. if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
  741. tailpos = hostapd_eid_vht_capabilities(hapd, tailpos);
  742. tailpos = hostapd_eid_vht_operation(hapd, tailpos);
  743. }
  744. if (hapd->conf->vendor_vht)
  745. tailpos = hostapd_eid_vendor_vht(hapd, tailpos);
  746. #endif /* CONFIG_IEEE80211AC */
  747. /* Wi-Fi Alliance WMM */
  748. tailpos = hostapd_eid_wmm(hapd, tailpos);
  749. #ifdef CONFIG_WPS
  750. if (hapd->conf->wps_state && hapd->wps_beacon_ie) {
  751. os_memcpy(tailpos, wpabuf_head(hapd->wps_beacon_ie),
  752. wpabuf_len(hapd->wps_beacon_ie));
  753. tailpos += wpabuf_len(hapd->wps_beacon_ie);
  754. }
  755. #endif /* CONFIG_WPS */
  756. #ifdef CONFIG_P2P
  757. if ((hapd->conf->p2p & P2P_ENABLED) && hapd->p2p_beacon_ie) {
  758. os_memcpy(tailpos, wpabuf_head(hapd->p2p_beacon_ie),
  759. wpabuf_len(hapd->p2p_beacon_ie));
  760. tailpos += wpabuf_len(hapd->p2p_beacon_ie);
  761. }
  762. #endif /* CONFIG_P2P */
  763. #ifdef CONFIG_P2P_MANAGER
  764. if ((hapd->conf->p2p & (P2P_MANAGE | P2P_ENABLED | P2P_GROUP_OWNER)) ==
  765. P2P_MANAGE)
  766. tailpos = hostapd_eid_p2p_manage(hapd, tailpos);
  767. #endif /* CONFIG_P2P_MANAGER */
  768. #ifdef CONFIG_HS20
  769. tailpos = hostapd_eid_hs20_indication(hapd, tailpos);
  770. tailpos = hostapd_eid_osen(hapd, tailpos);
  771. #endif /* CONFIG_HS20 */
  772. if (hapd->conf->vendor_elements) {
  773. os_memcpy(tailpos, wpabuf_head(hapd->conf->vendor_elements),
  774. wpabuf_len(hapd->conf->vendor_elements));
  775. tailpos += wpabuf_len(hapd->conf->vendor_elements);
  776. }
  777. tail_len = tailpos > tail ? tailpos - tail : 0;
  778. resp = hostapd_probe_resp_offloads(hapd, &resp_len);
  779. #endif /* NEED_AP_MLME */
  780. os_memset(params, 0, sizeof(*params));
  781. params->head = (u8 *) head;
  782. params->head_len = head_len;
  783. params->tail = tail;
  784. params->tail_len = tail_len;
  785. params->proberesp = resp;
  786. params->proberesp_len = resp_len;
  787. params->dtim_period = hapd->conf->dtim_period;
  788. params->beacon_int = hapd->iconf->beacon_int;
  789. params->basic_rates = hapd->iface->basic_rates;
  790. params->ssid = hapd->conf->ssid.ssid;
  791. params->ssid_len = hapd->conf->ssid.ssid_len;
  792. params->pairwise_ciphers = hapd->conf->wpa_pairwise |
  793. hapd->conf->rsn_pairwise;
  794. params->group_cipher = hapd->conf->wpa_group;
  795. params->key_mgmt_suites = hapd->conf->wpa_key_mgmt;
  796. params->auth_algs = hapd->conf->auth_algs;
  797. params->wpa_version = hapd->conf->wpa;
  798. params->privacy = hapd->conf->ssid.wep.keys_set || hapd->conf->wpa ||
  799. (hapd->conf->ieee802_1x &&
  800. (hapd->conf->default_wep_key_len ||
  801. hapd->conf->individual_wep_key_len));
  802. switch (hapd->conf->ignore_broadcast_ssid) {
  803. case 0:
  804. params->hide_ssid = NO_SSID_HIDING;
  805. break;
  806. case 1:
  807. params->hide_ssid = HIDDEN_SSID_ZERO_LEN;
  808. break;
  809. case 2:
  810. params->hide_ssid = HIDDEN_SSID_ZERO_CONTENTS;
  811. break;
  812. }
  813. params->isolate = hapd->conf->isolate;
  814. params->smps_mode = hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_MASK;
  815. #ifdef NEED_AP_MLME
  816. params->cts_protect = !!(ieee802_11_erp_info(hapd) &
  817. ERP_INFO_USE_PROTECTION);
  818. params->preamble = hapd->iface->num_sta_no_short_preamble == 0 &&
  819. hapd->iconf->preamble == SHORT_PREAMBLE;
  820. if (hapd->iface->current_mode &&
  821. hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
  822. params->short_slot_time =
  823. hapd->iface->num_sta_no_short_slot_time > 0 ? 0 : 1;
  824. else
  825. params->short_slot_time = -1;
  826. if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n)
  827. params->ht_opmode = -1;
  828. else
  829. params->ht_opmode = hapd->iface->ht_op_mode;
  830. #endif /* NEED_AP_MLME */
  831. params->interworking = hapd->conf->interworking;
  832. if (hapd->conf->interworking &&
  833. !is_zero_ether_addr(hapd->conf->hessid))
  834. params->hessid = hapd->conf->hessid;
  835. params->access_network_type = hapd->conf->access_network_type;
  836. params->ap_max_inactivity = hapd->conf->ap_max_inactivity;
  837. #ifdef CONFIG_P2P
  838. params->p2p_go_ctwindow = hapd->iconf->p2p_go_ctwindow;
  839. #endif /* CONFIG_P2P */
  840. #ifdef CONFIG_HS20
  841. params->disable_dgaf = hapd->conf->disable_dgaf;
  842. if (hapd->conf->osen) {
  843. params->privacy = 1;
  844. params->osen = 1;
  845. }
  846. #endif /* CONFIG_HS20 */
  847. return 0;
  848. }
  849. void ieee802_11_free_ap_params(struct wpa_driver_ap_params *params)
  850. {
  851. os_free(params->tail);
  852. params->tail = NULL;
  853. os_free(params->head);
  854. params->head = NULL;
  855. os_free(params->proberesp);
  856. params->proberesp = NULL;
  857. }
  858. int ieee802_11_set_beacon(struct hostapd_data *hapd)
  859. {
  860. struct wpa_driver_ap_params params;
  861. struct hostapd_freq_params freq;
  862. struct hostapd_iface *iface = hapd->iface;
  863. struct hostapd_config *iconf = iface->conf;
  864. struct wpabuf *beacon, *proberesp, *assocresp;
  865. int res, ret = -1;
  866. if (hapd->csa_in_progress) {
  867. wpa_printf(MSG_ERROR, "Cannot set beacons during CSA period");
  868. return -1;
  869. }
  870. hapd->beacon_set_done = 1;
  871. if (ieee802_11_build_ap_params(hapd, &params) < 0)
  872. return -1;
  873. if (hostapd_build_ap_extra_ies(hapd, &beacon, &proberesp, &assocresp) <
  874. 0)
  875. goto fail;
  876. params.beacon_ies = beacon;
  877. params.proberesp_ies = proberesp;
  878. params.assocresp_ies = assocresp;
  879. params.reenable = hapd->reenable_beacon;
  880. hapd->reenable_beacon = 0;
  881. if (iface->current_mode &&
  882. hostapd_set_freq_params(&freq, iconf->hw_mode, iface->freq,
  883. iconf->channel, iconf->ieee80211n,
  884. iconf->ieee80211ac,
  885. iconf->secondary_channel,
  886. iconf->vht_oper_chwidth,
  887. iconf->vht_oper_centr_freq_seg0_idx,
  888. iconf->vht_oper_centr_freq_seg1_idx,
  889. iface->current_mode->vht_capab) == 0)
  890. params.freq = &freq;
  891. res = hostapd_drv_set_ap(hapd, &params);
  892. hostapd_free_ap_extra_ies(hapd, beacon, proberesp, assocresp);
  893. if (res)
  894. wpa_printf(MSG_ERROR, "Failed to set beacon parameters");
  895. else
  896. ret = 0;
  897. fail:
  898. ieee802_11_free_ap_params(&params);
  899. return ret;
  900. }
  901. int ieee802_11_set_beacons(struct hostapd_iface *iface)
  902. {
  903. size_t i;
  904. int ret = 0;
  905. for (i = 0; i < iface->num_bss; i++) {
  906. if (iface->bss[i]->started &&
  907. ieee802_11_set_beacon(iface->bss[i]) < 0)
  908. ret = -1;
  909. }
  910. return ret;
  911. }
  912. /* only update beacons if started */
  913. int ieee802_11_update_beacons(struct hostapd_iface *iface)
  914. {
  915. size_t i;
  916. int ret = 0;
  917. for (i = 0; i < iface->num_bss; i++) {
  918. if (iface->bss[i]->beacon_set_done && iface->bss[i]->started &&
  919. ieee802_11_set_beacon(iface->bss[i]) < 0)
  920. ret = -1;
  921. }
  922. return ret;
  923. }
  924. #endif /* CONFIG_NATIVE_WINDOWS */