beacon.c 27 KB

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