ieee802_11_shared.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * hostapd / IEEE 802.11 Management
  3. * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "hostapd.h"
  18. #include "sta_info.h"
  19. #include "ap_config.h"
  20. #include "ap_drv_ops.h"
  21. #include "ieee802_11.h"
  22. #ifdef CONFIG_IEEE80211W
  23. u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
  24. struct sta_info *sta, u8 *eid)
  25. {
  26. u8 *pos = eid;
  27. u32 timeout, tu;
  28. struct os_time now, passed;
  29. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  30. *pos++ = 5;
  31. *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
  32. os_get_time(&now);
  33. os_time_sub(&now, &sta->sa_query_start, &passed);
  34. tu = (passed.sec * 1000000 + passed.usec) / 1024;
  35. if (hapd->conf->assoc_sa_query_max_timeout > tu)
  36. timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
  37. else
  38. timeout = 0;
  39. if (timeout < hapd->conf->assoc_sa_query_max_timeout)
  40. timeout++; /* add some extra time for local timers */
  41. WPA_PUT_LE32(pos, timeout);
  42. pos += 4;
  43. return pos;
  44. }
  45. /* MLME-SAQuery.request */
  46. void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
  47. const u8 *addr, const u8 *trans_id)
  48. {
  49. struct ieee80211_mgmt mgmt;
  50. u8 *end;
  51. wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
  52. MACSTR, MAC2STR(addr));
  53. wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
  54. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  55. os_memset(&mgmt, 0, sizeof(mgmt));
  56. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  57. WLAN_FC_STYPE_ACTION);
  58. os_memcpy(mgmt.da, addr, ETH_ALEN);
  59. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  60. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  61. mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
  62. mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
  63. os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
  64. WLAN_SA_QUERY_TR_ID_LEN);
  65. end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
  66. if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
  67. perror("ieee802_11_send_sa_query_req: send");
  68. }
  69. static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd,
  70. const u8 *sa, const u8 *trans_id)
  71. {
  72. struct sta_info *sta;
  73. struct ieee80211_mgmt resp;
  74. u8 *end;
  75. wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
  76. MACSTR, MAC2STR(sa));
  77. wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
  78. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  79. sta = ap_get_sta(hapd, sa);
  80. if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
  81. wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
  82. "from unassociated STA " MACSTR, MAC2STR(sa));
  83. return;
  84. }
  85. wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
  86. MACSTR, MAC2STR(sa));
  87. os_memset(&resp, 0, sizeof(resp));
  88. resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  89. WLAN_FC_STYPE_ACTION);
  90. os_memcpy(resp.da, sa, ETH_ALEN);
  91. os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
  92. os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
  93. resp.u.action.category = WLAN_ACTION_SA_QUERY;
  94. resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
  95. os_memcpy(resp.u.action.u.sa_query_req.trans_id, trans_id,
  96. WLAN_SA_QUERY_TR_ID_LEN);
  97. end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
  98. if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp) < 0)
  99. perror("ieee80211_mgmt_sa_query_request: send");
  100. }
  101. void ieee802_11_sa_query_action(struct hostapd_data *hapd, const u8 *sa,
  102. const u8 action_type, const u8 *trans_id)
  103. {
  104. struct sta_info *sta;
  105. int i;
  106. if (action_type == WLAN_SA_QUERY_REQUEST) {
  107. ieee802_11_send_sa_query_resp(hapd, sa, trans_id);
  108. return;
  109. }
  110. if (action_type != WLAN_SA_QUERY_RESPONSE) {
  111. wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
  112. "Action %d", action_type);
  113. return;
  114. }
  115. wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
  116. MACSTR, MAC2STR(sa));
  117. wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
  118. trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  119. /* MLME-SAQuery.confirm */
  120. sta = ap_get_sta(hapd, sa);
  121. if (sta == NULL || sta->sa_query_trans_id == NULL) {
  122. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
  123. "pending SA Query request found");
  124. return;
  125. }
  126. for (i = 0; i < sta->sa_query_count; i++) {
  127. if (os_memcmp(sta->sa_query_trans_id +
  128. i * WLAN_SA_QUERY_TR_ID_LEN,
  129. trans_id, WLAN_SA_QUERY_TR_ID_LEN) == 0)
  130. break;
  131. }
  132. if (i >= sta->sa_query_count) {
  133. wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
  134. "transaction identifier found");
  135. return;
  136. }
  137. hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
  138. HOSTAPD_LEVEL_DEBUG,
  139. "Reply to pending SA Query received");
  140. ap_sta_stop_sa_query(hapd, sta);
  141. }
  142. #endif /* CONFIG_IEEE80211W */
  143. u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
  144. {
  145. u8 *pos = eid;
  146. u8 len = 0;
  147. if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH))
  148. len = 5;
  149. if (len < 4 && hapd->conf->interworking)
  150. len = 4;
  151. if (len == 0)
  152. return eid;
  153. *pos++ = WLAN_EID_EXT_CAPAB;
  154. *pos++ = len;
  155. *pos++ = 0x00;
  156. *pos++ = 0x00;
  157. *pos++ = 0x00;
  158. *pos = 0x00;
  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. pos++;
  164. if (len < 5)
  165. return pos;
  166. *pos = 0x00;
  167. if (hapd->conf->tdls & TDLS_PROHIBIT)
  168. *pos |= 0x40; /* Bit 38 - TDLS Prohibited */
  169. if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH)
  170. *pos |= 0x80; /* Bit 39 - TDLS Channel Switching Prohibited */
  171. pos++;
  172. return pos;
  173. }
  174. u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
  175. {
  176. u8 *pos = eid;
  177. #ifdef CONFIG_INTERWORKING
  178. u8 *len;
  179. if (!hapd->conf->interworking)
  180. return eid;
  181. *pos++ = WLAN_EID_INTERWORKING;
  182. len = pos++;
  183. *pos = hapd->conf->access_network_type;
  184. if (hapd->conf->internet)
  185. *pos |= INTERWORKING_ANO_INTERNET;
  186. if (hapd->conf->asra)
  187. *pos |= INTERWORKING_ANO_ASRA;
  188. if (hapd->conf->esr)
  189. *pos |= INTERWORKING_ANO_ESR;
  190. if (hapd->conf->uesa)
  191. *pos |= INTERWORKING_ANO_UESA;
  192. pos++;
  193. if (hapd->conf->venue_info_set) {
  194. *pos++ = hapd->conf->venue_group;
  195. *pos++ = hapd->conf->venue_type;
  196. }
  197. if (!is_zero_ether_addr(hapd->conf->hessid)) {
  198. os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
  199. pos += ETH_ALEN;
  200. }
  201. *len = pos - len - 1;
  202. #endif /* CONFIG_INTERWORKING */
  203. return pos;
  204. }
  205. u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
  206. {
  207. u8 *pos = eid;
  208. #ifdef CONFIG_INTERWORKING
  209. /* TODO: Separate configuration for ANQP? */
  210. if (!hapd->conf->interworking)
  211. return eid;
  212. *pos++ = WLAN_EID_ADV_PROTO;
  213. *pos++ = 2;
  214. *pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
  215. *pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
  216. #endif /* CONFIG_INTERWORKING */
  217. return pos;
  218. }
  219. u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
  220. {
  221. u8 *pos = eid;
  222. #ifdef CONFIG_INTERWORKING
  223. u8 *len;
  224. unsigned int i, count;
  225. if (!hapd->conf->interworking ||
  226. hapd->conf->roaming_consortium == NULL ||
  227. hapd->conf->roaming_consortium_count == 0)
  228. return eid;
  229. *pos++ = WLAN_EID_ROAMING_CONSORTIUM;
  230. len = pos++;
  231. /* Number of ANQP OIs (in addition to the max 3 listed here) */
  232. if (hapd->conf->roaming_consortium_count > 3 + 255)
  233. *pos++ = 255;
  234. else if (hapd->conf->roaming_consortium_count > 3)
  235. *pos++ = hapd->conf->roaming_consortium_count - 3;
  236. else
  237. *pos++ = 0;
  238. /* OU #1 and #2 Lengths */
  239. *pos = hapd->conf->roaming_consortium[0].len;
  240. if (hapd->conf->roaming_consortium_count > 1)
  241. *pos |= hapd->conf->roaming_consortium[1].len << 4;
  242. pos++;
  243. if (hapd->conf->roaming_consortium_count > 3)
  244. count = 3;
  245. else
  246. count = hapd->conf->roaming_consortium_count;
  247. for (i = 0; i < count; i++) {
  248. os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
  249. hapd->conf->roaming_consortium[i].len);
  250. pos += hapd->conf->roaming_consortium[i].len;
  251. }
  252. *len = pos - len - 1;
  253. #endif /* CONFIG_INTERWORKING */
  254. return pos;
  255. }
  256. u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
  257. {
  258. if (hapd->conf->time_advertisement != 2)
  259. return eid;
  260. if (hapd->time_adv == NULL &&
  261. hostapd_update_time_adv(hapd) < 0)
  262. return eid;
  263. if (hapd->time_adv == NULL)
  264. return eid;
  265. os_memcpy(eid, wpabuf_head(hapd->time_adv),
  266. wpabuf_len(hapd->time_adv));
  267. eid += wpabuf_len(hapd->time_adv);
  268. return eid;
  269. }
  270. u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
  271. {
  272. size_t len;
  273. if (hapd->conf->time_advertisement != 2)
  274. return eid;
  275. len = os_strlen(hapd->conf->time_zone);
  276. *eid++ = WLAN_EID_TIME_ZONE;
  277. *eid++ = len;
  278. os_memcpy(eid, hapd->conf->time_zone, len);
  279. eid += len;
  280. return eid;
  281. }
  282. int hostapd_update_time_adv(struct hostapd_data *hapd)
  283. {
  284. const int elen = 2 + 1 + 10 + 5 + 1;
  285. struct os_time t;
  286. struct os_tm tm;
  287. u8 *pos;
  288. if (hapd->conf->time_advertisement != 2)
  289. return 0;
  290. if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
  291. return -1;
  292. if (!hapd->time_adv) {
  293. hapd->time_adv = wpabuf_alloc(elen);
  294. if (hapd->time_adv == NULL)
  295. return -1;
  296. pos = wpabuf_put(hapd->time_adv, elen);
  297. } else
  298. pos = wpabuf_mhead_u8(hapd->time_adv);
  299. *pos++ = WLAN_EID_TIME_ADVERTISEMENT;
  300. *pos++ = 1 + 10 + 5 + 1;
  301. *pos++ = 2; /* UTC time at which the TSF timer is 0 */
  302. /* Time Value at TSF 0 */
  303. /* FIX: need to calculate this based on the current TSF value */
  304. WPA_PUT_LE16(pos, tm.year); /* Year */
  305. pos += 2;
  306. *pos++ = tm.month; /* Month */
  307. *pos++ = tm.day; /* Day of month */
  308. *pos++ = tm.hour; /* Hours */
  309. *pos++ = tm.min; /* Minutes */
  310. *pos++ = tm.sec; /* Seconds */
  311. WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
  312. pos += 2;
  313. *pos++ = 0; /* Reserved */
  314. /* Time Error */
  315. /* TODO: fill in an estimate on the error */
  316. *pos++ = 0;
  317. *pos++ = 0;
  318. *pos++ = 0;
  319. *pos++ = 0;
  320. *pos++ = 0;
  321. *pos++ = hapd->time_update_counter++;
  322. return 0;
  323. }