ieee802_11_shared.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. break;
  145. case 1: /* Bits 8-15 */
  146. break;
  147. case 2: /* Bits 16-23 */
  148. if (hapd->conf->wnm_sleep_mode)
  149. *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
  150. if (hapd->conf->bss_transition)
  151. *pos |= 0x08; /* Bit 19 - BSS Transition */
  152. break;
  153. case 3: /* Bits 24-31 */
  154. #ifdef CONFIG_WNM
  155. *pos |= 0x02; /* Bit 25 - SSID List */
  156. #endif /* CONFIG_WNM */
  157. if (hapd->conf->time_advertisement == 2)
  158. *pos |= 0x08; /* Bit 27 - UTC TSF Offset */
  159. if (hapd->conf->interworking)
  160. *pos |= 0x80; /* Bit 31 - Interworking */
  161. break;
  162. case 4: /* Bits 32-39 */
  163. if (hapd->conf->qos_map_set_len)
  164. *pos |= 0x01; /* Bit 32 - QoS Map */
  165. if (hapd->conf->tdls & TDLS_PROHIBIT)
  166. *pos |= 0x40; /* Bit 38 - TDLS Prohibited */
  167. if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) {
  168. /* Bit 39 - TDLS Channel Switching Prohibited */
  169. *pos |= 0x80;
  170. }
  171. break;
  172. case 5: /* Bits 40-47 */
  173. #ifdef CONFIG_HS20
  174. if (hapd->conf->hs20)
  175. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  176. #endif /* CONFIG_HS20 */
  177. break;
  178. case 6: /* Bits 48-55 */
  179. if (hapd->conf->ssid.utf8_ssid)
  180. *pos |= 0x01; /* Bit 48 - UTF-8 SSID */
  181. break;
  182. }
  183. }
  184. u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
  185. {
  186. u8 *pos = eid;
  187. u8 len = 0, i;
  188. if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH))
  189. len = 5;
  190. if (len < 4 && hapd->conf->interworking)
  191. len = 4;
  192. if (len < 3 && hapd->conf->wnm_sleep_mode)
  193. len = 3;
  194. if (len < 1 && hapd->iconf->obss_interval)
  195. len = 1;
  196. if (len < 7 && hapd->conf->ssid.utf8_ssid)
  197. len = 7;
  198. #ifdef CONFIG_WNM
  199. if (len < 4)
  200. len = 4;
  201. #endif /* CONFIG_WNM */
  202. #ifdef CONFIG_HS20
  203. if (hapd->conf->hs20 && len < 6)
  204. len = 6;
  205. #endif /* CONFIG_HS20 */
  206. if (len < hapd->iface->extended_capa_len)
  207. len = hapd->iface->extended_capa_len;
  208. if (len == 0)
  209. return eid;
  210. *pos++ = WLAN_EID_EXT_CAPAB;
  211. *pos++ = len;
  212. for (i = 0; i < len; i++, pos++) {
  213. hostapd_ext_capab_byte(hapd, pos, i);
  214. if (i < hapd->iface->extended_capa_len) {
  215. *pos &= ~hapd->iface->extended_capa_mask[i];
  216. *pos |= hapd->iface->extended_capa[i];
  217. }
  218. }
  219. while (len > 0 && eid[1 + len] == 0) {
  220. len--;
  221. eid[1] = len;
  222. }
  223. if (len == 0)
  224. return eid;
  225. return eid + 2 + len;
  226. }
  227. u8 * hostapd_eid_qos_map_set(struct hostapd_data *hapd, u8 *eid)
  228. {
  229. u8 *pos = eid;
  230. u8 len = hapd->conf->qos_map_set_len;
  231. if (!len)
  232. return eid;
  233. *pos++ = WLAN_EID_QOS_MAP_SET;
  234. *pos++ = len;
  235. os_memcpy(pos, hapd->conf->qos_map_set, len);
  236. pos += len;
  237. return pos;
  238. }
  239. u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
  240. {
  241. u8 *pos = eid;
  242. #ifdef CONFIG_INTERWORKING
  243. u8 *len;
  244. if (!hapd->conf->interworking)
  245. return eid;
  246. *pos++ = WLAN_EID_INTERWORKING;
  247. len = pos++;
  248. *pos = hapd->conf->access_network_type;
  249. if (hapd->conf->internet)
  250. *pos |= INTERWORKING_ANO_INTERNET;
  251. if (hapd->conf->asra)
  252. *pos |= INTERWORKING_ANO_ASRA;
  253. if (hapd->conf->esr)
  254. *pos |= INTERWORKING_ANO_ESR;
  255. if (hapd->conf->uesa)
  256. *pos |= INTERWORKING_ANO_UESA;
  257. pos++;
  258. if (hapd->conf->venue_info_set) {
  259. *pos++ = hapd->conf->venue_group;
  260. *pos++ = hapd->conf->venue_type;
  261. }
  262. if (!is_zero_ether_addr(hapd->conf->hessid)) {
  263. os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
  264. pos += ETH_ALEN;
  265. }
  266. *len = pos - len - 1;
  267. #endif /* CONFIG_INTERWORKING */
  268. return pos;
  269. }
  270. u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
  271. {
  272. u8 *pos = eid;
  273. #ifdef CONFIG_INTERWORKING
  274. /* TODO: Separate configuration for ANQP? */
  275. if (!hapd->conf->interworking)
  276. return eid;
  277. *pos++ = WLAN_EID_ADV_PROTO;
  278. *pos++ = 2;
  279. *pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
  280. *pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
  281. #endif /* CONFIG_INTERWORKING */
  282. return pos;
  283. }
  284. u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
  285. {
  286. u8 *pos = eid;
  287. #ifdef CONFIG_INTERWORKING
  288. u8 *len;
  289. unsigned int i, count;
  290. if (!hapd->conf->interworking ||
  291. hapd->conf->roaming_consortium == NULL ||
  292. hapd->conf->roaming_consortium_count == 0)
  293. return eid;
  294. *pos++ = WLAN_EID_ROAMING_CONSORTIUM;
  295. len = pos++;
  296. /* Number of ANQP OIs (in addition to the max 3 listed here) */
  297. if (hapd->conf->roaming_consortium_count > 3 + 255)
  298. *pos++ = 255;
  299. else if (hapd->conf->roaming_consortium_count > 3)
  300. *pos++ = hapd->conf->roaming_consortium_count - 3;
  301. else
  302. *pos++ = 0;
  303. /* OU #1 and #2 Lengths */
  304. *pos = hapd->conf->roaming_consortium[0].len;
  305. if (hapd->conf->roaming_consortium_count > 1)
  306. *pos |= hapd->conf->roaming_consortium[1].len << 4;
  307. pos++;
  308. if (hapd->conf->roaming_consortium_count > 3)
  309. count = 3;
  310. else
  311. count = hapd->conf->roaming_consortium_count;
  312. for (i = 0; i < count; i++) {
  313. os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
  314. hapd->conf->roaming_consortium[i].len);
  315. pos += hapd->conf->roaming_consortium[i].len;
  316. }
  317. *len = pos - len - 1;
  318. #endif /* CONFIG_INTERWORKING */
  319. return pos;
  320. }
  321. u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
  322. {
  323. if (hapd->conf->time_advertisement != 2)
  324. return eid;
  325. if (hapd->time_adv == NULL &&
  326. hostapd_update_time_adv(hapd) < 0)
  327. return eid;
  328. if (hapd->time_adv == NULL)
  329. return eid;
  330. os_memcpy(eid, wpabuf_head(hapd->time_adv),
  331. wpabuf_len(hapd->time_adv));
  332. eid += wpabuf_len(hapd->time_adv);
  333. return eid;
  334. }
  335. u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
  336. {
  337. size_t len;
  338. if (hapd->conf->time_advertisement != 2)
  339. return eid;
  340. len = os_strlen(hapd->conf->time_zone);
  341. *eid++ = WLAN_EID_TIME_ZONE;
  342. *eid++ = len;
  343. os_memcpy(eid, hapd->conf->time_zone, len);
  344. eid += len;
  345. return eid;
  346. }
  347. int hostapd_update_time_adv(struct hostapd_data *hapd)
  348. {
  349. const int elen = 2 + 1 + 10 + 5 + 1;
  350. struct os_time t;
  351. struct os_tm tm;
  352. u8 *pos;
  353. if (hapd->conf->time_advertisement != 2)
  354. return 0;
  355. if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
  356. return -1;
  357. if (!hapd->time_adv) {
  358. hapd->time_adv = wpabuf_alloc(elen);
  359. if (hapd->time_adv == NULL)
  360. return -1;
  361. pos = wpabuf_put(hapd->time_adv, elen);
  362. } else
  363. pos = wpabuf_mhead_u8(hapd->time_adv);
  364. *pos++ = WLAN_EID_TIME_ADVERTISEMENT;
  365. *pos++ = 1 + 10 + 5 + 1;
  366. *pos++ = 2; /* UTC time at which the TSF timer is 0 */
  367. /* Time Value at TSF 0 */
  368. /* FIX: need to calculate this based on the current TSF value */
  369. WPA_PUT_LE16(pos, tm.year); /* Year */
  370. pos += 2;
  371. *pos++ = tm.month; /* Month */
  372. *pos++ = tm.day; /* Day of month */
  373. *pos++ = tm.hour; /* Hours */
  374. *pos++ = tm.min; /* Minutes */
  375. *pos++ = tm.sec; /* Seconds */
  376. WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
  377. pos += 2;
  378. *pos++ = 0; /* Reserved */
  379. /* Time Error */
  380. /* TODO: fill in an estimate on the error */
  381. *pos++ = 0;
  382. *pos++ = 0;
  383. *pos++ = 0;
  384. *pos++ = 0;
  385. *pos++ = 0;
  386. *pos++ = hapd->time_update_counter++;
  387. return 0;
  388. }
  389. u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid)
  390. {
  391. u8 *pos = eid;
  392. #ifdef CONFIG_WNM
  393. if (hapd->conf->ap_max_inactivity > 0) {
  394. unsigned int val;
  395. *pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD;
  396. *pos++ = 3;
  397. val = hapd->conf->ap_max_inactivity;
  398. if (val > 68000)
  399. val = 68000;
  400. val *= 1000;
  401. val /= 1024;
  402. if (val == 0)
  403. val = 1;
  404. if (val > 65535)
  405. val = 65535;
  406. WPA_PUT_LE16(pos, val);
  407. pos += 2;
  408. *pos++ = 0x00; /* TODO: Protected Keep-Alive Required */
  409. }
  410. #endif /* CONFIG_WNM */
  411. return pos;
  412. }