ieee802_11_shared.c 12 KB

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