ieee802_11_shared.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "hostapd.h"
  12. #include "sta_info.h"
  13. #include "ap_config.h"
  14. #include "ap_drv_ops.h"
  15. #include "ieee802_11.h"
  16. #ifdef CONFIG_IEEE80211W
  17. u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
  18. struct sta_info *sta, u8 *eid)
  19. {
  20. u8 *pos = eid;
  21. u32 timeout, tu;
  22. struct os_reltime now, passed;
  23. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  24. *pos++ = 5;
  25. *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
  26. os_get_reltime(&now);
  27. os_reltime_sub(&now, &sta->sa_query_start, &passed);
  28. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  29. if (hapd->conf->assoc_sa_query_max_timeout > tu)
  30. timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
  31. else
  32. timeout = 0;
  33. if (timeout < hapd->conf->assoc_sa_query_max_timeout)
  34. timeout++; /* add some extra time for local timers */
  35. WPA_PUT_LE32(pos, timeout);
  36. pos += 4;
  37. return pos;
  38. }
  39. /* MLME-SAQuery.request */
  40. void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
  41. const u8 *addr, const u8 *trans_id)
  42. {
  43. struct ieee80211_mgmt mgmt;
  44. u8 *end;
  45. wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
  46. MACSTR, MAC2STR(addr));
  47. wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
  48. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  49. os_memset(&mgmt, 0, sizeof(mgmt));
  50. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  51. WLAN_FC_STYPE_ACTION);
  52. os_memcpy(mgmt.da, addr, ETH_ALEN);
  53. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  54. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  55. mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
  56. mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
  57. os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
  58. WLAN_SA_QUERY_TR_ID_LEN);
  59. end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
  60. if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0)
  61. wpa_printf(MSG_INFO, "ieee802_11_send_sa_query_req: send failed");
  62. }
  63. static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd,
  64. const u8 *sa, const u8 *trans_id)
  65. {
  66. struct sta_info *sta;
  67. struct ieee80211_mgmt resp;
  68. u8 *end;
  69. wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
  70. MACSTR, MAC2STR(sa));
  71. wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
  72. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  73. sta = ap_get_sta(hapd, sa);
  74. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  75. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
  76. "from unassociated STA " MACSTR, MAC2STR(sa));
  77. return;
  78. }
  79. wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
  80. MACSTR, MAC2STR(sa));
  81. os_memset(&resp, 0, sizeof(resp));
  82. resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  83. WLAN_FC_STYPE_ACTION);
  84. os_memcpy(resp.da, sa, ETH_ALEN);
  85. os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
  86. os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
  87. resp.u.action.category = WLAN_ACTION_SA_QUERY;
  88. resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
  89. os_memcpy(resp.u.action.u.sa_query_req.trans_id, trans_id,
  90. WLAN_SA_QUERY_TR_ID_LEN);
  91. end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
  92. if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp, 0) < 0)
  93. wpa_printf(MSG_INFO, "ieee80211_mgmt_sa_query_request: send failed");
  94. }
  95. void ieee802_11_sa_query_action(struct hostapd_data *hapd, const u8 *sa,
  96. const u8 action_type, const u8 *trans_id)
  97. {
  98. struct sta_info *sta;
  99. int i;
  100. if (action_type == WLAN_SA_QUERY_REQUEST) {
  101. ieee802_11_send_sa_query_resp(hapd, sa, trans_id);
  102. return;
  103. }
  104. if (action_type != WLAN_SA_QUERY_RESPONSE) {
  105. wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
  106. "Action %d", action_type);
  107. return;
  108. }
  109. wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
  110. MACSTR, MAC2STR(sa));
  111. wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
  112. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  113. /* MLME-SAQuery.confirm */
  114. sta = ap_get_sta(hapd, sa);
  115. if (sta == NULL || sta->sa_query_trans_id == NULL) {
  116. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
  117. "pending SA Query request found");
  118. return;
  119. }
  120. for (i = 0; i < sta->sa_query_count; i++) {
  121. if (os_memcmp(sta->sa_query_trans_id +
  122. i * WLAN_SA_QUERY_TR_ID_LEN,
  123. trans_id, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  124. break;
  125. }
  126. if (i >= sta->sa_query_count) {
  127. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
  128. "transaction identifier found");
  129. return;
  130. }
  131. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  132. HOSTAPD_LEVEL_DEBUG,
  133. "Reply to pending SA Query received");
  134. ap_sta_stop_sa_query(hapd, sta);
  135. }
  136. #endif /* CONFIG_IEEE80211W */
  137. static void hostapd_ext_capab_byte(struct hostapd_data *hapd, u8 *pos, int idx)
  138. {
  139. *pos = 0x00;
  140. switch (idx) {
  141. case 0: /* Bits 0-7 */
  142. if (hapd->iconf->obss_interval)
  143. *pos |= 0x01; /* Bit 0 - Coexistence management */
  144. if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)
  145. *pos |= 0x04; /* Bit 2 - Extended Channel Switching */
  146. break;
  147. case 1: /* Bits 8-15 */
  148. if (hapd->conf->proxy_arp)
  149. *pos |= 0x10; /* Bit 12 - Proxy ARP */
  150. break;
  151. case 2: /* Bits 16-23 */
  152. if (hapd->conf->wnm_sleep_mode)
  153. *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
  154. if (hapd->conf->bss_transition)
  155. *pos |= 0x08; /* Bit 19 - BSS Transition */
  156. break;
  157. case 3: /* Bits 24-31 */
  158. #ifdef CONFIG_WNM
  159. *pos |= 0x02; /* Bit 25 - SSID List */
  160. #endif /* CONFIG_WNM */
  161. if (hapd->conf->time_advertisement == 2)
  162. *pos |= 0x08; /* Bit 27 - UTC TSF Offset */
  163. if (hapd->conf->interworking)
  164. *pos |= 0x80; /* Bit 31 - Interworking */
  165. break;
  166. case 4: /* Bits 32-39 */
  167. if (hapd->conf->qos_map_set_len)
  168. *pos |= 0x01; /* Bit 32 - QoS Map */
  169. if (hapd->conf->tdls & TDLS_PROHIBIT)
  170. *pos |= 0x40; /* Bit 38 - TDLS Prohibited */
  171. if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) {
  172. /* Bit 39 - TDLS Channel Switching Prohibited */
  173. *pos |= 0x80;
  174. }
  175. break;
  176. case 5: /* Bits 40-47 */
  177. #ifdef CONFIG_HS20
  178. if (hapd->conf->hs20)
  179. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  180. #endif /* CONFIG_HS20 */
  181. #ifdef CONFIG_MBO
  182. if (hapd->conf->mbo_enabled)
  183. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  184. #endif /* CONFIG_MBO */
  185. break;
  186. case 6: /* Bits 48-55 */
  187. if (hapd->conf->ssid.utf8_ssid)
  188. *pos |= 0x01; /* Bit 48 - UTF-8 SSID */
  189. break;
  190. }
  191. }
  192. u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
  193. {
  194. u8 *pos = eid;
  195. u8 len = 0, i;
  196. if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH))
  197. len = 5;
  198. if (len < 4 && hapd->conf->interworking)
  199. len = 4;
  200. if (len < 3 && hapd->conf->wnm_sleep_mode)
  201. len = 3;
  202. if (len < 1 && hapd->iconf->obss_interval)
  203. len = 1;
  204. if (len < 7 && hapd->conf->ssid.utf8_ssid)
  205. len = 7;
  206. #ifdef CONFIG_WNM
  207. if (len < 4)
  208. len = 4;
  209. #endif /* CONFIG_WNM */
  210. #ifdef CONFIG_HS20
  211. if (hapd->conf->hs20 && len < 6)
  212. len = 6;
  213. #endif /* CONFIG_HS20 */
  214. #ifdef CONFIG_MBO
  215. if (hapd->conf->mbo_enabled && len < 6)
  216. len = 6;
  217. #endif /* CONFIG_MBO */
  218. if (len < hapd->iface->extended_capa_len)
  219. len = hapd->iface->extended_capa_len;
  220. if (len == 0)
  221. return eid;
  222. *pos++ = WLAN_EID_EXT_CAPAB;
  223. *pos++ = len;
  224. for (i = 0; i < len; i++, pos++) {
  225. hostapd_ext_capab_byte(hapd, pos, i);
  226. if (i < hapd->iface->extended_capa_len) {
  227. *pos &= ~hapd->iface->extended_capa_mask[i];
  228. *pos |= hapd->iface->extended_capa[i];
  229. }
  230. }
  231. while (len > 0 && eid[1 + len] == 0) {
  232. len--;
  233. eid[1] = len;
  234. }
  235. if (len == 0)
  236. return eid;
  237. return eid + 2 + len;
  238. }
  239. u8 * hostapd_eid_qos_map_set(struct hostapd_data *hapd, u8 *eid)
  240. {
  241. u8 *pos = eid;
  242. u8 len = hapd->conf->qos_map_set_len;
  243. if (!len)
  244. return eid;
  245. *pos++ = WLAN_EID_QOS_MAP_SET;
  246. *pos++ = len;
  247. os_memcpy(pos, hapd->conf->qos_map_set, len);
  248. pos += len;
  249. return pos;
  250. }
  251. u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
  252. {
  253. u8 *pos = eid;
  254. #ifdef CONFIG_INTERWORKING
  255. u8 *len;
  256. if (!hapd->conf->interworking)
  257. return eid;
  258. *pos++ = WLAN_EID_INTERWORKING;
  259. len = pos++;
  260. *pos = hapd->conf->access_network_type;
  261. if (hapd->conf->internet)
  262. *pos |= INTERWORKING_ANO_INTERNET;
  263. if (hapd->conf->asra)
  264. *pos |= INTERWORKING_ANO_ASRA;
  265. if (hapd->conf->esr)
  266. *pos |= INTERWORKING_ANO_ESR;
  267. if (hapd->conf->uesa)
  268. *pos |= INTERWORKING_ANO_UESA;
  269. pos++;
  270. if (hapd->conf->venue_info_set) {
  271. *pos++ = hapd->conf->venue_group;
  272. *pos++ = hapd->conf->venue_type;
  273. }
  274. if (!is_zero_ether_addr(hapd->conf->hessid)) {
  275. os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
  276. pos += ETH_ALEN;
  277. }
  278. *len = pos - len - 1;
  279. #endif /* CONFIG_INTERWORKING */
  280. return pos;
  281. }
  282. u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
  283. {
  284. u8 *pos = eid;
  285. #ifdef CONFIG_INTERWORKING
  286. /* TODO: Separate configuration for ANQP? */
  287. if (!hapd->conf->interworking)
  288. return eid;
  289. *pos++ = WLAN_EID_ADV_PROTO;
  290. *pos++ = 2;
  291. *pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
  292. *pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
  293. #endif /* CONFIG_INTERWORKING */
  294. return pos;
  295. }
  296. u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
  297. {
  298. u8 *pos = eid;
  299. #ifdef CONFIG_INTERWORKING
  300. u8 *len;
  301. unsigned int i, count;
  302. if (!hapd->conf->interworking ||
  303. hapd->conf->roaming_consortium == NULL ||
  304. hapd->conf->roaming_consortium_count == 0)
  305. return eid;
  306. *pos++ = WLAN_EID_ROAMING_CONSORTIUM;
  307. len = pos++;
  308. /* Number of ANQP OIs (in addition to the max 3 listed here) */
  309. if (hapd->conf->roaming_consortium_count > 3 + 255)
  310. *pos++ = 255;
  311. else if (hapd->conf->roaming_consortium_count > 3)
  312. *pos++ = hapd->conf->roaming_consortium_count - 3;
  313. else
  314. *pos++ = 0;
  315. /* OU #1 and #2 Lengths */
  316. *pos = hapd->conf->roaming_consortium[0].len;
  317. if (hapd->conf->roaming_consortium_count > 1)
  318. *pos |= hapd->conf->roaming_consortium[1].len << 4;
  319. pos++;
  320. if (hapd->conf->roaming_consortium_count > 3)
  321. count = 3;
  322. else
  323. count = hapd->conf->roaming_consortium_count;
  324. for (i = 0; i < count; i++) {
  325. os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
  326. hapd->conf->roaming_consortium[i].len);
  327. pos += hapd->conf->roaming_consortium[i].len;
  328. }
  329. *len = pos - len - 1;
  330. #endif /* CONFIG_INTERWORKING */
  331. return pos;
  332. }
  333. u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
  334. {
  335. if (hapd->conf->time_advertisement != 2)
  336. return eid;
  337. if (hapd->time_adv == NULL &&
  338. hostapd_update_time_adv(hapd) < 0)
  339. return eid;
  340. if (hapd->time_adv == NULL)
  341. return eid;
  342. os_memcpy(eid, wpabuf_head(hapd->time_adv),
  343. wpabuf_len(hapd->time_adv));
  344. eid += wpabuf_len(hapd->time_adv);
  345. return eid;
  346. }
  347. u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
  348. {
  349. size_t len;
  350. if (hapd->conf->time_advertisement != 2)
  351. return eid;
  352. len = os_strlen(hapd->conf->time_zone);
  353. *eid++ = WLAN_EID_TIME_ZONE;
  354. *eid++ = len;
  355. os_memcpy(eid, hapd->conf->time_zone, len);
  356. eid += len;
  357. return eid;
  358. }
  359. int hostapd_update_time_adv(struct hostapd_data *hapd)
  360. {
  361. const int elen = 2 + 1 + 10 + 5 + 1;
  362. struct os_time t;
  363. struct os_tm tm;
  364. u8 *pos;
  365. if (hapd->conf->time_advertisement != 2)
  366. return 0;
  367. if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
  368. return -1;
  369. if (!hapd->time_adv) {
  370. hapd->time_adv = wpabuf_alloc(elen);
  371. if (hapd->time_adv == NULL)
  372. return -1;
  373. pos = wpabuf_put(hapd->time_adv, elen);
  374. } else
  375. pos = wpabuf_mhead_u8(hapd->time_adv);
  376. *pos++ = WLAN_EID_TIME_ADVERTISEMENT;
  377. *pos++ = 1 + 10 + 5 + 1;
  378. *pos++ = 2; /* UTC time at which the TSF timer is 0 */
  379. /* Time Value at TSF 0 */
  380. /* FIX: need to calculate this based on the current TSF value */
  381. WPA_PUT_LE16(pos, tm.year); /* Year */
  382. pos += 2;
  383. *pos++ = tm.month; /* Month */
  384. *pos++ = tm.day; /* Day of month */
  385. *pos++ = tm.hour; /* Hours */
  386. *pos++ = tm.min; /* Minutes */
  387. *pos++ = tm.sec; /* Seconds */
  388. WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
  389. pos += 2;
  390. *pos++ = 0; /* Reserved */
  391. /* Time Error */
  392. /* TODO: fill in an estimate on the error */
  393. *pos++ = 0;
  394. *pos++ = 0;
  395. *pos++ = 0;
  396. *pos++ = 0;
  397. *pos++ = 0;
  398. *pos++ = hapd->time_update_counter++;
  399. return 0;
  400. }
  401. u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid)
  402. {
  403. u8 *pos = eid;
  404. #ifdef CONFIG_WNM
  405. if (hapd->conf->ap_max_inactivity > 0) {
  406. unsigned int val;
  407. *pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD;
  408. *pos++ = 3;
  409. val = hapd->conf->ap_max_inactivity;
  410. if (val > 68000)
  411. val = 68000;
  412. val *= 1000;
  413. val /= 1024;
  414. if (val == 0)
  415. val = 1;
  416. if (val > 65535)
  417. val = 65535;
  418. WPA_PUT_LE16(pos, val);
  419. pos += 2;
  420. *pos++ = 0x00; /* TODO: Protected Keep-Alive Required */
  421. }
  422. #endif /* CONFIG_WNM */
  423. return pos;
  424. }
  425. #ifdef CONFIG_MBO
  426. u8 * hostapd_eid_mbo(struct hostapd_data *hapd, u8 *eid, size_t len)
  427. {
  428. u8 mbo[6], *mbo_pos = mbo;
  429. u8 *pos = eid;
  430. if (!hapd->conf->mbo_enabled)
  431. return eid;
  432. *mbo_pos++ = MBO_ATTR_ID_AP_CAPA_IND;
  433. *mbo_pos++ = 1;
  434. /* Not Cellular aware */
  435. *mbo_pos++ = 0;
  436. if (hapd->mbo_assoc_disallow) {
  437. *mbo_pos++ = MBO_ATTR_ID_ASSOC_DISALLOW;
  438. *mbo_pos++ = 1;
  439. *mbo_pos++ = hapd->mbo_assoc_disallow;
  440. }
  441. pos += mbo_add_ie(pos, len, mbo, mbo_pos - mbo);
  442. return pos;
  443. }
  444. u8 hostapd_mbo_ie_len(struct hostapd_data *hapd)
  445. {
  446. if (!hapd->conf->mbo_enabled)
  447. return 0;
  448. /*
  449. * MBO IE header (6) + Capability Indication attribute (3) +
  450. * Association Disallowed attribute (3) = 12
  451. */
  452. return 6 + 3 + (hapd->mbo_assoc_disallow ? 3 : 0);
  453. }
  454. #endif /* CONFIG_MBO */
  455. void ap_copy_sta_supp_op_classes(struct sta_info *sta,
  456. const u8 *supp_op_classes,
  457. size_t supp_op_classes_len)
  458. {
  459. if (!supp_op_classes)
  460. return;
  461. os_free(sta->supp_op_classes);
  462. sta->supp_op_classes = os_malloc(1 + supp_op_classes_len);
  463. if (!sta->supp_op_classes)
  464. return;
  465. sta->supp_op_classes[0] = supp_op_classes_len;
  466. os_memcpy(sta->supp_op_classes + 1, supp_op_classes,
  467. supp_op_classes_len);
  468. }