gas_serv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Generic advertisement service (GAS) server
  3. * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "common/gas.h"
  12. #include "utils/eloop.h"
  13. #include "hostapd.h"
  14. #include "ap_config.h"
  15. #include "ap_drv_ops.h"
  16. #include "sta_info.h"
  17. #include "gas_serv.h"
  18. static struct gas_dialog_info *
  19. gas_dialog_create(struct hostapd_data *hapd, const u8 *addr, u8 dialog_token)
  20. {
  21. struct sta_info *sta;
  22. struct gas_dialog_info *dia = NULL;
  23. int i, j;
  24. sta = ap_get_sta(hapd, addr);
  25. if (!sta) {
  26. /*
  27. * We need a STA entry to be able to maintain state for
  28. * the GAS query.
  29. */
  30. wpa_printf(MSG_DEBUG, "ANQP: Add a temporary STA entry for "
  31. "GAS query");
  32. sta = ap_sta_add(hapd, addr);
  33. if (!sta) {
  34. wpa_printf(MSG_DEBUG, "Failed to add STA " MACSTR
  35. " for GAS query", MAC2STR(addr));
  36. return NULL;
  37. }
  38. sta->flags |= WLAN_STA_GAS;
  39. /*
  40. * The default inactivity is 300 seconds. We don't need
  41. * it to be that long.
  42. */
  43. ap_sta_session_timeout(hapd, sta, 5);
  44. }
  45. if (sta->gas_dialog == NULL) {
  46. sta->gas_dialog = os_zalloc(GAS_DIALOG_MAX *
  47. sizeof(struct gas_dialog_info));
  48. if (sta->gas_dialog == NULL)
  49. return NULL;
  50. }
  51. for (i = sta->gas_dialog_next, j = 0; j < GAS_DIALOG_MAX; i++, j++) {
  52. if (i == GAS_DIALOG_MAX)
  53. i = 0;
  54. if (sta->gas_dialog[i].valid)
  55. continue;
  56. dia = &sta->gas_dialog[i];
  57. dia->valid = 1;
  58. dia->index = i;
  59. dia->dialog_token = dialog_token;
  60. sta->gas_dialog_next = (++i == GAS_DIALOG_MAX) ? 0 : i;
  61. return dia;
  62. }
  63. wpa_msg(hapd->msg_ctx, MSG_ERROR, "ANQP: Could not create dialog for "
  64. MACSTR " dialog_token %u. Consider increasing "
  65. "GAS_DIALOG_MAX.", MAC2STR(addr), dialog_token);
  66. return NULL;
  67. }
  68. struct gas_dialog_info *
  69. gas_serv_dialog_find(struct hostapd_data *hapd, const u8 *addr,
  70. u8 dialog_token)
  71. {
  72. struct sta_info *sta;
  73. int i;
  74. sta = ap_get_sta(hapd, addr);
  75. if (!sta) {
  76. wpa_printf(MSG_DEBUG, "ANQP: could not find STA " MACSTR,
  77. MAC2STR(addr));
  78. return NULL;
  79. }
  80. for (i = 0; sta->gas_dialog && i < GAS_DIALOG_MAX; i++) {
  81. if (sta->gas_dialog[i].dialog_token != dialog_token ||
  82. !sta->gas_dialog[i].valid)
  83. continue;
  84. return &sta->gas_dialog[i];
  85. }
  86. wpa_printf(MSG_DEBUG, "ANQP: Could not find dialog for "
  87. MACSTR " dialog_token %u", MAC2STR(addr), dialog_token);
  88. return NULL;
  89. }
  90. void gas_serv_dialog_clear(struct gas_dialog_info *dia)
  91. {
  92. wpabuf_free(dia->sd_resp);
  93. os_memset(dia, 0, sizeof(*dia));
  94. }
  95. static void gas_serv_free_dialogs(struct hostapd_data *hapd,
  96. const u8 *sta_addr)
  97. {
  98. struct sta_info *sta;
  99. int i;
  100. sta = ap_get_sta(hapd, sta_addr);
  101. if (sta == NULL || sta->gas_dialog == NULL)
  102. return;
  103. for (i = 0; i < GAS_DIALOG_MAX; i++) {
  104. if (sta->gas_dialog[i].valid)
  105. return;
  106. }
  107. os_free(sta->gas_dialog);
  108. sta->gas_dialog = NULL;
  109. }
  110. static void anqp_add_hs_capab_list(struct hostapd_data *hapd,
  111. struct wpabuf *buf)
  112. {
  113. u8 *len;
  114. len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  115. wpabuf_put_be24(buf, OUI_WFA);
  116. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  117. wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
  118. wpabuf_put_u8(buf, 0); /* Reserved */
  119. wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
  120. if (hapd->conf->hs20_operating_class)
  121. wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
  122. gas_anqp_set_element_len(buf, len);
  123. }
  124. static void anqp_add_capab_list(struct hostapd_data *hapd,
  125. struct wpabuf *buf)
  126. {
  127. u8 *len;
  128. len = gas_anqp_add_element(buf, ANQP_CAPABILITY_LIST);
  129. wpabuf_put_le16(buf, ANQP_CAPABILITY_LIST);
  130. if (hapd->conf->venue_name)
  131. wpabuf_put_le16(buf, ANQP_VENUE_NAME);
  132. if (hapd->conf->network_auth_type)
  133. wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
  134. if (hapd->conf->roaming_consortium)
  135. wpabuf_put_le16(buf, ANQP_ROAMING_CONSORTIUM);
  136. if (hapd->conf->ipaddr_type_configured)
  137. wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
  138. if (hapd->conf->anqp_3gpp_cell_net)
  139. wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
  140. if (hapd->conf->domain_name)
  141. wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
  142. anqp_add_hs_capab_list(hapd, buf);
  143. gas_anqp_set_element_len(buf, len);
  144. }
  145. static void anqp_add_venue_name(struct hostapd_data *hapd, struct wpabuf *buf)
  146. {
  147. if (hapd->conf->venue_name) {
  148. u8 *len;
  149. unsigned int i;
  150. len = gas_anqp_add_element(buf, ANQP_VENUE_NAME);
  151. wpabuf_put_u8(buf, hapd->conf->venue_group);
  152. wpabuf_put_u8(buf, hapd->conf->venue_type);
  153. for (i = 0; i < hapd->conf->venue_name_count; i++) {
  154. struct hostapd_venue_name *vn;
  155. vn = &hapd->conf->venue_name[i];
  156. wpabuf_put_u8(buf, 3 + vn->name_len);
  157. wpabuf_put_data(buf, vn->lang, 3);
  158. wpabuf_put_data(buf, vn->name, vn->name_len);
  159. }
  160. gas_anqp_set_element_len(buf, len);
  161. }
  162. }
  163. static void anqp_add_network_auth_type(struct hostapd_data *hapd,
  164. struct wpabuf *buf)
  165. {
  166. if (hapd->conf->network_auth_type) {
  167. wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
  168. wpabuf_put_le16(buf, hapd->conf->network_auth_type_len);
  169. wpabuf_put_data(buf, hapd->conf->network_auth_type,
  170. hapd->conf->network_auth_type_len);
  171. }
  172. }
  173. static void anqp_add_roaming_consortium(struct hostapd_data *hapd,
  174. struct wpabuf *buf)
  175. {
  176. unsigned int i;
  177. u8 *len;
  178. len = gas_anqp_add_element(buf, ANQP_ROAMING_CONSORTIUM);
  179. for (i = 0; i < hapd->conf->roaming_consortium_count; i++) {
  180. struct hostapd_roaming_consortium *rc;
  181. rc = &hapd->conf->roaming_consortium[i];
  182. wpabuf_put_u8(buf, rc->len);
  183. wpabuf_put_data(buf, rc->oi, rc->len);
  184. }
  185. gas_anqp_set_element_len(buf, len);
  186. }
  187. static void anqp_add_ip_addr_type_availability(struct hostapd_data *hapd,
  188. struct wpabuf *buf)
  189. {
  190. if (hapd->conf->ipaddr_type_configured) {
  191. wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
  192. wpabuf_put_le16(buf, 1);
  193. wpabuf_put_u8(buf, hapd->conf->ipaddr_type_availability);
  194. }
  195. }
  196. static void anqp_add_3gpp_cellular_network(struct hostapd_data *hapd,
  197. struct wpabuf *buf)
  198. {
  199. if (hapd->conf->anqp_3gpp_cell_net) {
  200. wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
  201. wpabuf_put_le16(buf,
  202. hapd->conf->anqp_3gpp_cell_net_len);
  203. wpabuf_put_data(buf, hapd->conf->anqp_3gpp_cell_net,
  204. hapd->conf->anqp_3gpp_cell_net_len);
  205. }
  206. }
  207. static void anqp_add_domain_name(struct hostapd_data *hapd, struct wpabuf *buf)
  208. {
  209. if (hapd->conf->domain_name) {
  210. wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
  211. wpabuf_put_le16(buf, hapd->conf->domain_name_len);
  212. wpabuf_put_data(buf, hapd->conf->domain_name,
  213. hapd->conf->domain_name_len);
  214. }
  215. }
  216. static void anqp_add_operating_class(struct hostapd_data *hapd,
  217. struct wpabuf *buf)
  218. {
  219. if (hapd->conf->hs20_operating_class) {
  220. u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  221. wpabuf_put_be24(buf, OUI_WFA);
  222. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  223. wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
  224. wpabuf_put_u8(buf, 0); /* Reserved */
  225. wpabuf_put_data(buf, hapd->conf->hs20_operating_class,
  226. hapd->conf->hs20_operating_class_len);
  227. gas_anqp_set_element_len(buf, len);
  228. }
  229. }
  230. static struct wpabuf *
  231. gas_serv_build_gas_resp_payload(struct hostapd_data *hapd,
  232. unsigned int request,
  233. struct gas_dialog_info *di)
  234. {
  235. struct wpabuf *buf;
  236. buf = wpabuf_alloc(1400);
  237. if (buf == NULL)
  238. return NULL;
  239. if (request & ANQP_REQ_CAPABILITY_LIST)
  240. anqp_add_capab_list(hapd, buf);
  241. if (request & ANQP_REQ_VENUE_NAME)
  242. anqp_add_venue_name(hapd, buf);
  243. if (request & ANQP_REQ_NETWORK_AUTH_TYPE)
  244. anqp_add_network_auth_type(hapd, buf);
  245. if (request & ANQP_REQ_ROAMING_CONSORTIUM)
  246. anqp_add_roaming_consortium(hapd, buf);
  247. if (request & ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY)
  248. anqp_add_ip_addr_type_availability(hapd, buf);
  249. if (request & ANQP_REQ_3GPP_CELLULAR_NETWORK)
  250. anqp_add_3gpp_cellular_network(hapd, buf);
  251. if (request & ANQP_REQ_DOMAIN_NAME)
  252. anqp_add_domain_name(hapd, buf);
  253. if (request & ANQP_REQ_HS_CAPABILITY_LIST)
  254. anqp_add_hs_capab_list(hapd, buf);
  255. if (request & ANQP_REQ_OPERATING_CLASS)
  256. anqp_add_operating_class(hapd, buf);
  257. return buf;
  258. }
  259. static void gas_serv_clear_cached_ies(void *eloop_data, void *user_ctx)
  260. {
  261. struct gas_dialog_info *dia = eloop_data;
  262. wpa_printf(MSG_DEBUG, "GAS: Timeout triggered, clearing dialog for "
  263. "dialog token %d", dia->dialog_token);
  264. gas_serv_dialog_clear(dia);
  265. }
  266. struct anqp_query_info {
  267. unsigned int request;
  268. unsigned int remote_request;
  269. const void *param;
  270. u32 param_arg;
  271. u16 remote_delay;
  272. };
  273. static void set_anqp_req(unsigned int bit, const char *name, int local,
  274. unsigned int remote, u16 remote_delay,
  275. struct anqp_query_info *qi)
  276. {
  277. qi->request |= bit;
  278. if (local) {
  279. wpa_printf(MSG_DEBUG, "ANQP: %s (local)", name);
  280. } else if (bit & remote) {
  281. wpa_printf(MSG_DEBUG, "ANQP: %s (remote)", name);
  282. qi->remote_request |= bit;
  283. if (remote_delay > qi->remote_delay)
  284. qi->remote_delay = remote_delay;
  285. } else {
  286. wpa_printf(MSG_DEBUG, "ANQP: %s not available", name);
  287. }
  288. }
  289. static void rx_anqp_query_list_id(struct hostapd_data *hapd, u16 info_id,
  290. struct anqp_query_info *qi)
  291. {
  292. switch (info_id) {
  293. case ANQP_CAPABILITY_LIST:
  294. set_anqp_req(ANQP_REQ_CAPABILITY_LIST, "Capability List", 1, 0,
  295. 0, qi);
  296. break;
  297. case ANQP_VENUE_NAME:
  298. set_anqp_req(ANQP_REQ_VENUE_NAME, "Venue Name",
  299. hapd->conf->venue_name != NULL, 0, 0, qi);
  300. break;
  301. case ANQP_NETWORK_AUTH_TYPE:
  302. set_anqp_req(ANQP_REQ_NETWORK_AUTH_TYPE, "Network Auth Type",
  303. hapd->conf->network_auth_type != NULL,
  304. 0, 0, qi);
  305. break;
  306. case ANQP_ROAMING_CONSORTIUM:
  307. set_anqp_req(ANQP_REQ_ROAMING_CONSORTIUM, "Roaming Consortium",
  308. hapd->conf->roaming_consortium != NULL, 0, 0, qi);
  309. break;
  310. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  311. set_anqp_req(ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY,
  312. "IP Addr Type Availability",
  313. hapd->conf->ipaddr_type_configured,
  314. 0, 0, qi);
  315. break;
  316. case ANQP_3GPP_CELLULAR_NETWORK:
  317. set_anqp_req(ANQP_REQ_3GPP_CELLULAR_NETWORK,
  318. "3GPP Cellular Network",
  319. hapd->conf->anqp_3gpp_cell_net != NULL,
  320. 0, 0, qi);
  321. break;
  322. case ANQP_DOMAIN_NAME:
  323. set_anqp_req(ANQP_REQ_DOMAIN_NAME, "Domain Name",
  324. hapd->conf->domain_name != NULL,
  325. 0, 0, qi);
  326. break;
  327. default:
  328. wpa_printf(MSG_DEBUG, "ANQP: Unsupported Info Id %u",
  329. info_id);
  330. break;
  331. }
  332. }
  333. static void rx_anqp_query_list(struct hostapd_data *hapd,
  334. const u8 *pos, const u8 *end,
  335. struct anqp_query_info *qi)
  336. {
  337. wpa_printf(MSG_DEBUG, "ANQP: %u Info IDs requested in Query list",
  338. (unsigned int) (end - pos) / 2);
  339. while (pos + 2 <= end) {
  340. rx_anqp_query_list_id(hapd, WPA_GET_LE16(pos), qi);
  341. pos += 2;
  342. }
  343. }
  344. static void rx_anqp_hs_query_list(struct hostapd_data *hapd, u8 subtype,
  345. struct anqp_query_info *qi)
  346. {
  347. switch (subtype) {
  348. case HS20_STYPE_CAPABILITY_LIST:
  349. set_anqp_req(ANQP_REQ_HS_CAPABILITY_LIST, "HS Capability List",
  350. 1, 0, 0, qi);
  351. break;
  352. case HS20_STYPE_OPERATING_CLASS:
  353. set_anqp_req(ANQP_REQ_OPERATING_CLASS, "Operating Class",
  354. hapd->conf->hs20_operating_class != NULL,
  355. 0, 0, qi);
  356. break;
  357. default:
  358. wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 subtype %u",
  359. subtype);
  360. break;
  361. }
  362. }
  363. static void rx_anqp_vendor_specific(struct hostapd_data *hapd,
  364. const u8 *pos, const u8 *end,
  365. struct anqp_query_info *qi)
  366. {
  367. u32 oui;
  368. u8 subtype;
  369. if (pos + 4 > end) {
  370. wpa_printf(MSG_DEBUG, "ANQP: Too short vendor specific ANQP "
  371. "Query element");
  372. return;
  373. }
  374. oui = WPA_GET_BE24(pos);
  375. pos += 3;
  376. if (oui != OUI_WFA) {
  377. wpa_printf(MSG_DEBUG, "ANQP: Unsupported vendor OUI %06x",
  378. oui);
  379. return;
  380. }
  381. if (*pos != HS20_ANQP_OUI_TYPE) {
  382. wpa_printf(MSG_DEBUG, "ANQP: Unsupported WFA vendor type %u",
  383. *pos);
  384. return;
  385. }
  386. pos++;
  387. if (pos + 1 >= end)
  388. return;
  389. subtype = *pos++;
  390. pos++; /* Reserved */
  391. switch (subtype) {
  392. case HS20_STYPE_QUERY_LIST:
  393. wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Query List");
  394. while (pos < end) {
  395. rx_anqp_hs_query_list(hapd, *pos, qi);
  396. pos++;
  397. }
  398. break;
  399. default:
  400. wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 query subtype "
  401. "%u", subtype);
  402. break;
  403. }
  404. }
  405. static void gas_serv_req_local_processing(struct hostapd_data *hapd,
  406. const u8 *sa, u8 dialog_token,
  407. struct anqp_query_info *qi)
  408. {
  409. struct wpabuf *buf, *tx_buf;
  410. buf = gas_serv_build_gas_resp_payload(hapd, qi->request, NULL);
  411. wpa_hexdump_buf(MSG_MSGDUMP, "ANQP: Locally generated ANQP responses",
  412. buf);
  413. if (!buf)
  414. return;
  415. if (wpabuf_len(buf) > hapd->gas_frag_limit ||
  416. hapd->conf->gas_comeback_delay) {
  417. struct gas_dialog_info *di;
  418. u16 comeback_delay = 1;
  419. if (hapd->conf->gas_comeback_delay) {
  420. /* Testing - allow overriding of the delay value */
  421. comeback_delay = hapd->conf->gas_comeback_delay;
  422. }
  423. wpa_printf(MSG_DEBUG, "ANQP: Too long response to fit in "
  424. "initial response - use GAS comeback");
  425. di = gas_dialog_create(hapd, sa, dialog_token);
  426. if (!di) {
  427. wpa_printf(MSG_INFO, "ANQP: Could not create dialog "
  428. "for " MACSTR " (dialog token %u)",
  429. MAC2STR(sa), dialog_token);
  430. wpabuf_free(buf);
  431. return;
  432. }
  433. di->sd_resp = buf;
  434. di->sd_resp_pos = 0;
  435. tx_buf = gas_anqp_build_initial_resp_buf(
  436. dialog_token, WLAN_STATUS_SUCCESS, comeback_delay,
  437. NULL);
  438. } else {
  439. wpa_printf(MSG_DEBUG, "ANQP: Initial response (no comeback)");
  440. tx_buf = gas_anqp_build_initial_resp_buf(
  441. dialog_token, WLAN_STATUS_SUCCESS, 0, buf);
  442. wpabuf_free(buf);
  443. }
  444. if (!tx_buf)
  445. return;
  446. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  447. wpabuf_head(tx_buf), wpabuf_len(tx_buf));
  448. wpabuf_free(tx_buf);
  449. }
  450. static void gas_serv_rx_gas_initial_req(struct hostapd_data *hapd,
  451. const u8 *sa,
  452. const u8 *data, size_t len)
  453. {
  454. const u8 *pos = data;
  455. const u8 *end = data + len;
  456. const u8 *next;
  457. u8 dialog_token;
  458. u16 slen;
  459. struct anqp_query_info qi;
  460. const u8 *adv_proto;
  461. if (len < 1 + 2)
  462. return;
  463. os_memset(&qi, 0, sizeof(qi));
  464. dialog_token = *pos++;
  465. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  466. "GAS: GAS Initial Request from " MACSTR " (dialog token %u) ",
  467. MAC2STR(sa), dialog_token);
  468. if (*pos != WLAN_EID_ADV_PROTO) {
  469. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  470. "GAS: Unexpected IE in GAS Initial Request: %u", *pos);
  471. return;
  472. }
  473. adv_proto = pos++;
  474. slen = *pos++;
  475. next = pos + slen;
  476. if (next > end || slen < 2) {
  477. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  478. "GAS: Invalid IE in GAS Initial Request");
  479. return;
  480. }
  481. pos++; /* skip QueryRespLenLimit and PAME-BI */
  482. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  483. struct wpabuf *buf;
  484. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  485. "GAS: Unsupported GAS advertisement protocol id %u",
  486. *pos);
  487. if (sa[0] & 0x01)
  488. return; /* Invalid source address - drop silently */
  489. buf = gas_build_initial_resp(
  490. dialog_token, WLAN_STATUS_GAS_ADV_PROTO_NOT_SUPPORTED,
  491. 0, 2 + slen + 2);
  492. if (buf == NULL)
  493. return;
  494. wpabuf_put_data(buf, adv_proto, 2 + slen);
  495. wpabuf_put_le16(buf, 0); /* Query Response Length */
  496. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  497. wpabuf_head(buf), wpabuf_len(buf));
  498. wpabuf_free(buf);
  499. return;
  500. }
  501. pos = next;
  502. /* Query Request */
  503. if (pos + 2 > end)
  504. return;
  505. slen = WPA_GET_LE16(pos);
  506. pos += 2;
  507. if (pos + slen > end)
  508. return;
  509. end = pos + slen;
  510. /* ANQP Query Request */
  511. while (pos < end) {
  512. u16 info_id, elen;
  513. if (pos + 4 > end)
  514. return;
  515. info_id = WPA_GET_LE16(pos);
  516. pos += 2;
  517. elen = WPA_GET_LE16(pos);
  518. pos += 2;
  519. if (pos + elen > end) {
  520. wpa_printf(MSG_DEBUG, "ANQP: Invalid Query Request");
  521. return;
  522. }
  523. switch (info_id) {
  524. case ANQP_QUERY_LIST:
  525. rx_anqp_query_list(hapd, pos, pos + elen, &qi);
  526. break;
  527. case ANQP_VENDOR_SPECIFIC:
  528. rx_anqp_vendor_specific(hapd, pos, pos + elen, &qi);
  529. break;
  530. default:
  531. wpa_printf(MSG_DEBUG, "ANQP: Unsupported Query "
  532. "Request element %u", info_id);
  533. break;
  534. }
  535. pos += elen;
  536. }
  537. gas_serv_req_local_processing(hapd, sa, dialog_token, &qi);
  538. }
  539. void gas_serv_tx_gas_response(struct hostapd_data *hapd, const u8 *dst,
  540. struct gas_dialog_info *dialog)
  541. {
  542. struct wpabuf *buf, *tx_buf;
  543. u8 dialog_token = dialog->dialog_token;
  544. size_t frag_len;
  545. if (dialog->sd_resp == NULL) {
  546. buf = gas_serv_build_gas_resp_payload(hapd,
  547. dialog->all_requested,
  548. dialog);
  549. wpa_hexdump_buf(MSG_MSGDUMP, "ANQP: Generated ANQP responses",
  550. buf);
  551. if (!buf)
  552. goto tx_gas_response_done;
  553. dialog->sd_resp = buf;
  554. dialog->sd_resp_pos = 0;
  555. }
  556. frag_len = wpabuf_len(dialog->sd_resp) - dialog->sd_resp_pos;
  557. if (frag_len > hapd->gas_frag_limit || dialog->comeback_delay ||
  558. hapd->conf->gas_comeback_delay) {
  559. u16 comeback_delay_tus = dialog->comeback_delay +
  560. GAS_SERV_COMEBACK_DELAY_FUDGE;
  561. u32 comeback_delay_secs, comeback_delay_usecs;
  562. if (hapd->conf->gas_comeback_delay) {
  563. /* Testing - allow overriding of the delay value */
  564. comeback_delay_tus = hapd->conf->gas_comeback_delay;
  565. }
  566. wpa_printf(MSG_DEBUG, "GAS: Response frag_len %u (frag limit "
  567. "%u) and comeback delay %u, "
  568. "requesting comebacks", (unsigned int) frag_len,
  569. (unsigned int) hapd->gas_frag_limit,
  570. dialog->comeback_delay);
  571. tx_buf = gas_anqp_build_initial_resp_buf(dialog_token,
  572. WLAN_STATUS_SUCCESS,
  573. comeback_delay_tus,
  574. NULL);
  575. if (tx_buf) {
  576. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  577. "GAS: Tx GAS Initial Resp (comeback = 10TU)");
  578. hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
  579. dst,
  580. wpabuf_head(tx_buf),
  581. wpabuf_len(tx_buf));
  582. }
  583. wpabuf_free(tx_buf);
  584. /* start a timer of 1.5 * comeback-delay */
  585. comeback_delay_tus = comeback_delay_tus +
  586. (comeback_delay_tus / 2);
  587. comeback_delay_secs = (comeback_delay_tus * 1024) / 1000000;
  588. comeback_delay_usecs = (comeback_delay_tus * 1024) -
  589. (comeback_delay_secs * 1000000);
  590. eloop_register_timeout(comeback_delay_secs,
  591. comeback_delay_usecs,
  592. gas_serv_clear_cached_ies, dialog,
  593. NULL);
  594. goto tx_gas_response_done;
  595. }
  596. buf = wpabuf_alloc_copy(wpabuf_head_u8(dialog->sd_resp) +
  597. dialog->sd_resp_pos, frag_len);
  598. if (buf == NULL) {
  599. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Buffer allocation "
  600. "failed");
  601. goto tx_gas_response_done;
  602. }
  603. tx_buf = gas_anqp_build_initial_resp_buf(dialog_token,
  604. WLAN_STATUS_SUCCESS, 0, buf);
  605. wpabuf_free(buf);
  606. if (tx_buf == NULL)
  607. goto tx_gas_response_done;
  608. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Tx GAS Initial "
  609. "Response (frag_id %d frag_len %d)",
  610. dialog->sd_frag_id, (int) frag_len);
  611. dialog->sd_frag_id++;
  612. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, dst,
  613. wpabuf_head(tx_buf), wpabuf_len(tx_buf));
  614. wpabuf_free(tx_buf);
  615. tx_gas_response_done:
  616. gas_serv_clear_cached_ies(dialog, NULL);
  617. }
  618. static void gas_serv_rx_gas_comeback_req(struct hostapd_data *hapd,
  619. const u8 *sa,
  620. const u8 *data, size_t len)
  621. {
  622. struct gas_dialog_info *dialog;
  623. struct wpabuf *buf, *tx_buf;
  624. u8 dialog_token;
  625. size_t frag_len;
  626. int more = 0;
  627. wpa_hexdump(MSG_DEBUG, "GAS: RX GAS Comeback Request", data, len);
  628. if (len < 1)
  629. return;
  630. dialog_token = *data;
  631. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Dialog Token: %u",
  632. dialog_token);
  633. dialog = gas_serv_dialog_find(hapd, sa, dialog_token);
  634. if (!dialog) {
  635. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: No pending SD "
  636. "response fragment for " MACSTR " dialog token %u",
  637. MAC2STR(sa), dialog_token);
  638. if (sa[0] & 0x01)
  639. return; /* Invalid source address - drop silently */
  640. tx_buf = gas_anqp_build_comeback_resp_buf(
  641. dialog_token, WLAN_STATUS_NO_OUTSTANDING_GAS_REQ, 0, 0,
  642. 0, NULL);
  643. if (tx_buf == NULL)
  644. return;
  645. goto send_resp;
  646. }
  647. if (dialog->sd_resp == NULL) {
  648. wpa_printf(MSG_DEBUG, "GAS: Remote request 0x%x received 0x%x",
  649. dialog->requested, dialog->received);
  650. if ((dialog->requested & dialog->received) !=
  651. dialog->requested) {
  652. wpa_printf(MSG_DEBUG, "GAS: Did not receive response "
  653. "from remote processing");
  654. gas_serv_dialog_clear(dialog);
  655. tx_buf = gas_anqp_build_comeback_resp_buf(
  656. dialog_token,
  657. WLAN_STATUS_GAS_RESP_NOT_RECEIVED, 0, 0, 0,
  658. NULL);
  659. if (tx_buf == NULL)
  660. return;
  661. goto send_resp;
  662. }
  663. buf = gas_serv_build_gas_resp_payload(hapd,
  664. dialog->all_requested,
  665. dialog);
  666. wpa_hexdump_buf(MSG_MSGDUMP, "ANQP: Generated ANQP responses",
  667. buf);
  668. if (!buf)
  669. goto rx_gas_comeback_req_done;
  670. dialog->sd_resp = buf;
  671. dialog->sd_resp_pos = 0;
  672. }
  673. frag_len = wpabuf_len(dialog->sd_resp) - dialog->sd_resp_pos;
  674. if (frag_len > hapd->gas_frag_limit) {
  675. frag_len = hapd->gas_frag_limit;
  676. more = 1;
  677. }
  678. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: resp frag_len %u",
  679. (unsigned int) frag_len);
  680. buf = wpabuf_alloc_copy(wpabuf_head_u8(dialog->sd_resp) +
  681. dialog->sd_resp_pos, frag_len);
  682. if (buf == NULL) {
  683. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Failed to allocate "
  684. "buffer");
  685. goto rx_gas_comeback_req_done;
  686. }
  687. tx_buf = gas_anqp_build_comeback_resp_buf(dialog_token,
  688. WLAN_STATUS_SUCCESS,
  689. dialog->sd_frag_id,
  690. more, 0, buf);
  691. wpabuf_free(buf);
  692. if (tx_buf == NULL)
  693. goto rx_gas_comeback_req_done;
  694. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Tx GAS Comeback Response "
  695. "(frag_id %d more=%d frag_len=%d)",
  696. dialog->sd_frag_id, more, (int) frag_len);
  697. dialog->sd_frag_id++;
  698. dialog->sd_resp_pos += frag_len;
  699. if (more) {
  700. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: %d more bytes remain "
  701. "to be sent",
  702. (int) (wpabuf_len(dialog->sd_resp) -
  703. dialog->sd_resp_pos));
  704. } else {
  705. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: All fragments of "
  706. "SD response sent");
  707. gas_serv_dialog_clear(dialog);
  708. gas_serv_free_dialogs(hapd, sa);
  709. }
  710. send_resp:
  711. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  712. wpabuf_head(tx_buf), wpabuf_len(tx_buf));
  713. wpabuf_free(tx_buf);
  714. return;
  715. rx_gas_comeback_req_done:
  716. gas_serv_clear_cached_ies(dialog, NULL);
  717. }
  718. static void gas_serv_rx_public_action(void *ctx, const u8 *buf, size_t len,
  719. int freq)
  720. {
  721. struct hostapd_data *hapd = ctx;
  722. const struct ieee80211_mgmt *mgmt;
  723. size_t hdr_len;
  724. const u8 *sa, *data;
  725. mgmt = (const struct ieee80211_mgmt *) buf;
  726. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  727. if (hdr_len > len)
  728. return;
  729. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
  730. return;
  731. sa = mgmt->sa;
  732. len -= hdr_len;
  733. data = &mgmt->u.action.u.public_action.action;
  734. switch (data[0]) {
  735. case WLAN_PA_GAS_INITIAL_REQ:
  736. gas_serv_rx_gas_initial_req(hapd, sa, data + 1, len - 1);
  737. break;
  738. case WLAN_PA_GAS_COMEBACK_REQ:
  739. gas_serv_rx_gas_comeback_req(hapd, sa, data + 1, len - 1);
  740. break;
  741. }
  742. }
  743. int gas_serv_init(struct hostapd_data *hapd)
  744. {
  745. hapd->public_action_cb = gas_serv_rx_public_action;
  746. hapd->public_action_cb_ctx = hapd;
  747. hapd->gas_frag_limit = 1400;
  748. if (hapd->conf->gas_frag_limit > 0)
  749. hapd->gas_frag_limit = hapd->conf->gas_frag_limit;
  750. return 0;
  751. }
  752. void gas_serv_deinit(struct hostapd_data *hapd)
  753. {
  754. }