gas_serv.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. /*
  2. * Generic advertisement service (GAS) server
  3. * Copyright (c) 2011-2014, 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 "common/wpa_ctrl.h"
  13. #include "utils/eloop.h"
  14. #include "hostapd.h"
  15. #include "ap_config.h"
  16. #include "ap_drv_ops.h"
  17. #include "dpp_hostapd.h"
  18. #include "sta_info.h"
  19. #include "gas_serv.h"
  20. #ifdef CONFIG_DPP
  21. static void gas_serv_write_dpp_adv_proto(struct wpabuf *buf)
  22. {
  23. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  24. wpabuf_put_u8(buf, 8); /* Length */
  25. wpabuf_put_u8(buf, 0x7f);
  26. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  27. wpabuf_put_u8(buf, 5);
  28. wpabuf_put_be24(buf, OUI_WFA);
  29. wpabuf_put_u8(buf, DPP_OUI_TYPE);
  30. wpabuf_put_u8(buf, 0x01);
  31. }
  32. #endif /* CONFIG_DPP */
  33. static void convert_to_protected_dual(struct wpabuf *msg)
  34. {
  35. u8 *categ = wpabuf_mhead_u8(msg);
  36. *categ = WLAN_ACTION_PROTECTED_DUAL;
  37. }
  38. static struct gas_dialog_info *
  39. gas_dialog_create(struct hostapd_data *hapd, const u8 *addr, u8 dialog_token)
  40. {
  41. struct sta_info *sta;
  42. struct gas_dialog_info *dia = NULL;
  43. int i, j;
  44. sta = ap_get_sta(hapd, addr);
  45. if (!sta) {
  46. /*
  47. * We need a STA entry to be able to maintain state for
  48. * the GAS query.
  49. */
  50. wpa_printf(MSG_DEBUG, "ANQP: Add a temporary STA entry for "
  51. "GAS query");
  52. sta = ap_sta_add(hapd, addr);
  53. if (!sta) {
  54. wpa_printf(MSG_DEBUG, "Failed to add STA " MACSTR
  55. " for GAS query", MAC2STR(addr));
  56. return NULL;
  57. }
  58. sta->flags |= WLAN_STA_GAS;
  59. /*
  60. * The default inactivity is 300 seconds. We don't need
  61. * it to be that long. Use five second timeout and increase this
  62. * with the comeback_delay for testing cases.
  63. */
  64. ap_sta_session_timeout(hapd, sta,
  65. hapd->conf->gas_comeback_delay / 1024 +
  66. 5);
  67. } else {
  68. ap_sta_replenish_timeout(hapd, sta, 5);
  69. }
  70. if (sta->gas_dialog == NULL) {
  71. sta->gas_dialog = os_calloc(GAS_DIALOG_MAX,
  72. sizeof(struct gas_dialog_info));
  73. if (sta->gas_dialog == NULL)
  74. return NULL;
  75. }
  76. for (i = sta->gas_dialog_next, j = 0; j < GAS_DIALOG_MAX; i++, j++) {
  77. if (i == GAS_DIALOG_MAX)
  78. i = 0;
  79. if (sta->gas_dialog[i].valid)
  80. continue;
  81. dia = &sta->gas_dialog[i];
  82. dia->valid = 1;
  83. dia->dialog_token = dialog_token;
  84. sta->gas_dialog_next = (++i == GAS_DIALOG_MAX) ? 0 : i;
  85. return dia;
  86. }
  87. wpa_msg(hapd->msg_ctx, MSG_ERROR, "ANQP: Could not create dialog for "
  88. MACSTR " dialog_token %u. Consider increasing "
  89. "GAS_DIALOG_MAX.", MAC2STR(addr), dialog_token);
  90. return NULL;
  91. }
  92. struct gas_dialog_info *
  93. gas_serv_dialog_find(struct hostapd_data *hapd, const u8 *addr,
  94. u8 dialog_token)
  95. {
  96. struct sta_info *sta;
  97. int i;
  98. sta = ap_get_sta(hapd, addr);
  99. if (!sta) {
  100. wpa_printf(MSG_DEBUG, "ANQP: could not find STA " MACSTR,
  101. MAC2STR(addr));
  102. return NULL;
  103. }
  104. for (i = 0; sta->gas_dialog && i < GAS_DIALOG_MAX; i++) {
  105. if (sta->gas_dialog[i].dialog_token != dialog_token ||
  106. !sta->gas_dialog[i].valid)
  107. continue;
  108. ap_sta_replenish_timeout(hapd, sta, 5);
  109. return &sta->gas_dialog[i];
  110. }
  111. wpa_printf(MSG_DEBUG, "ANQP: Could not find dialog for "
  112. MACSTR " dialog_token %u", MAC2STR(addr), dialog_token);
  113. return NULL;
  114. }
  115. void gas_serv_dialog_clear(struct gas_dialog_info *dia)
  116. {
  117. wpabuf_free(dia->sd_resp);
  118. os_memset(dia, 0, sizeof(*dia));
  119. }
  120. static void gas_serv_free_dialogs(struct hostapd_data *hapd,
  121. const u8 *sta_addr)
  122. {
  123. struct sta_info *sta;
  124. int i;
  125. sta = ap_get_sta(hapd, sta_addr);
  126. if (sta == NULL || sta->gas_dialog == NULL)
  127. return;
  128. for (i = 0; i < GAS_DIALOG_MAX; i++) {
  129. if (sta->gas_dialog[i].valid)
  130. return;
  131. }
  132. os_free(sta->gas_dialog);
  133. sta->gas_dialog = NULL;
  134. }
  135. #ifdef CONFIG_HS20
  136. static void anqp_add_hs_capab_list(struct hostapd_data *hapd,
  137. struct wpabuf *buf)
  138. {
  139. u8 *len;
  140. len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  141. wpabuf_put_be24(buf, OUI_WFA);
  142. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  143. wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
  144. wpabuf_put_u8(buf, 0); /* Reserved */
  145. wpabuf_put_u8(buf, HS20_STYPE_CAPABILITY_LIST);
  146. if (hapd->conf->hs20_oper_friendly_name)
  147. wpabuf_put_u8(buf, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  148. if (hapd->conf->hs20_wan_metrics)
  149. wpabuf_put_u8(buf, HS20_STYPE_WAN_METRICS);
  150. if (hapd->conf->hs20_connection_capability)
  151. wpabuf_put_u8(buf, HS20_STYPE_CONNECTION_CAPABILITY);
  152. if (hapd->conf->nai_realm_data)
  153. wpabuf_put_u8(buf, HS20_STYPE_NAI_HOME_REALM_QUERY);
  154. if (hapd->conf->hs20_operating_class)
  155. wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
  156. if (hapd->conf->hs20_osu_providers_count)
  157. wpabuf_put_u8(buf, HS20_STYPE_OSU_PROVIDERS_LIST);
  158. if (hapd->conf->hs20_icons_count)
  159. wpabuf_put_u8(buf, HS20_STYPE_ICON_REQUEST);
  160. gas_anqp_set_element_len(buf, len);
  161. }
  162. #endif /* CONFIG_HS20 */
  163. static struct anqp_element * get_anqp_elem(struct hostapd_data *hapd,
  164. u16 infoid)
  165. {
  166. struct anqp_element *elem;
  167. dl_list_for_each(elem, &hapd->conf->anqp_elem, struct anqp_element,
  168. list) {
  169. if (elem->infoid == infoid)
  170. return elem;
  171. }
  172. return NULL;
  173. }
  174. static void anqp_add_elem(struct hostapd_data *hapd, struct wpabuf *buf,
  175. u16 infoid)
  176. {
  177. struct anqp_element *elem;
  178. elem = get_anqp_elem(hapd, infoid);
  179. if (!elem)
  180. return;
  181. if (wpabuf_tailroom(buf) < 2 + 2 + wpabuf_len(elem->payload)) {
  182. wpa_printf(MSG_DEBUG, "ANQP: No room for InfoID %u payload",
  183. infoid);
  184. return;
  185. }
  186. wpabuf_put_le16(buf, infoid);
  187. wpabuf_put_le16(buf, wpabuf_len(elem->payload));
  188. wpabuf_put_buf(buf, elem->payload);
  189. }
  190. static int anqp_add_override(struct hostapd_data *hapd, struct wpabuf *buf,
  191. u16 infoid)
  192. {
  193. if (get_anqp_elem(hapd, infoid)) {
  194. anqp_add_elem(hapd, buf, infoid);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. static void anqp_add_capab_list(struct hostapd_data *hapd,
  200. struct wpabuf *buf)
  201. {
  202. u8 *len;
  203. u16 id;
  204. if (anqp_add_override(hapd, buf, ANQP_CAPABILITY_LIST))
  205. return;
  206. len = gas_anqp_add_element(buf, ANQP_CAPABILITY_LIST);
  207. wpabuf_put_le16(buf, ANQP_CAPABILITY_LIST);
  208. if (hapd->conf->venue_name || get_anqp_elem(hapd, ANQP_VENUE_NAME))
  209. wpabuf_put_le16(buf, ANQP_VENUE_NAME);
  210. if (get_anqp_elem(hapd, ANQP_EMERGENCY_CALL_NUMBER))
  211. wpabuf_put_le16(buf, ANQP_EMERGENCY_CALL_NUMBER);
  212. if (hapd->conf->network_auth_type ||
  213. get_anqp_elem(hapd, ANQP_NETWORK_AUTH_TYPE))
  214. wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
  215. if (hapd->conf->roaming_consortium ||
  216. get_anqp_elem(hapd, ANQP_ROAMING_CONSORTIUM))
  217. wpabuf_put_le16(buf, ANQP_ROAMING_CONSORTIUM);
  218. if (hapd->conf->ipaddr_type_configured ||
  219. get_anqp_elem(hapd, ANQP_IP_ADDR_TYPE_AVAILABILITY))
  220. wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
  221. if (hapd->conf->nai_realm_data ||
  222. get_anqp_elem(hapd, ANQP_NAI_REALM))
  223. wpabuf_put_le16(buf, ANQP_NAI_REALM);
  224. if (hapd->conf->anqp_3gpp_cell_net ||
  225. get_anqp_elem(hapd, ANQP_3GPP_CELLULAR_NETWORK))
  226. wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
  227. if (get_anqp_elem(hapd, ANQP_AP_GEOSPATIAL_LOCATION))
  228. wpabuf_put_le16(buf, ANQP_AP_GEOSPATIAL_LOCATION);
  229. if (get_anqp_elem(hapd, ANQP_AP_CIVIC_LOCATION))
  230. wpabuf_put_le16(buf, ANQP_AP_CIVIC_LOCATION);
  231. if (get_anqp_elem(hapd, ANQP_AP_LOCATION_PUBLIC_URI))
  232. wpabuf_put_le16(buf, ANQP_AP_LOCATION_PUBLIC_URI);
  233. if (hapd->conf->domain_name || get_anqp_elem(hapd, ANQP_DOMAIN_NAME))
  234. wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
  235. if (get_anqp_elem(hapd, ANQP_EMERGENCY_ALERT_URI))
  236. wpabuf_put_le16(buf, ANQP_EMERGENCY_ALERT_URI);
  237. if (get_anqp_elem(hapd, ANQP_TDLS_CAPABILITY))
  238. wpabuf_put_le16(buf, ANQP_TDLS_CAPABILITY);
  239. if (get_anqp_elem(hapd, ANQP_EMERGENCY_NAI))
  240. wpabuf_put_le16(buf, ANQP_EMERGENCY_NAI);
  241. if (get_anqp_elem(hapd, ANQP_NEIGHBOR_REPORT))
  242. wpabuf_put_le16(buf, ANQP_NEIGHBOR_REPORT);
  243. #ifdef CONFIG_FILS
  244. if (!dl_list_empty(&hapd->conf->fils_realms) ||
  245. get_anqp_elem(hapd, ANQP_FILS_REALM_INFO))
  246. wpabuf_put_le16(buf, ANQP_FILS_REALM_INFO);
  247. #endif /* CONFIG_FILS */
  248. if (get_anqp_elem(hapd, ANQP_CAG))
  249. wpabuf_put_le16(buf, ANQP_CAG);
  250. if (hapd->conf->venue_url || get_anqp_elem(hapd, ANQP_VENUE_URL))
  251. wpabuf_put_le16(buf, ANQP_VENUE_URL);
  252. if (get_anqp_elem(hapd, ANQP_ADVICE_OF_CHARGE))
  253. wpabuf_put_le16(buf, ANQP_ADVICE_OF_CHARGE);
  254. if (get_anqp_elem(hapd, ANQP_LOCAL_CONTENT))
  255. wpabuf_put_le16(buf, ANQP_LOCAL_CONTENT);
  256. for (id = 280; id < 300; id++) {
  257. if (get_anqp_elem(hapd, id))
  258. wpabuf_put_le16(buf, id);
  259. }
  260. #ifdef CONFIG_HS20
  261. anqp_add_hs_capab_list(hapd, buf);
  262. #endif /* CONFIG_HS20 */
  263. gas_anqp_set_element_len(buf, len);
  264. }
  265. static void anqp_add_venue_name(struct hostapd_data *hapd, struct wpabuf *buf)
  266. {
  267. if (anqp_add_override(hapd, buf, ANQP_VENUE_NAME))
  268. return;
  269. if (hapd->conf->venue_name) {
  270. u8 *len;
  271. unsigned int i;
  272. len = gas_anqp_add_element(buf, ANQP_VENUE_NAME);
  273. wpabuf_put_u8(buf, hapd->conf->venue_group);
  274. wpabuf_put_u8(buf, hapd->conf->venue_type);
  275. for (i = 0; i < hapd->conf->venue_name_count; i++) {
  276. struct hostapd_lang_string *vn;
  277. vn = &hapd->conf->venue_name[i];
  278. wpabuf_put_u8(buf, 3 + vn->name_len);
  279. wpabuf_put_data(buf, vn->lang, 3);
  280. wpabuf_put_data(buf, vn->name, vn->name_len);
  281. }
  282. gas_anqp_set_element_len(buf, len);
  283. }
  284. }
  285. static void anqp_add_venue_url(struct hostapd_data *hapd, struct wpabuf *buf)
  286. {
  287. if (anqp_add_override(hapd, buf, ANQP_VENUE_URL))
  288. return;
  289. if (hapd->conf->venue_url) {
  290. u8 *len;
  291. unsigned int i;
  292. len = gas_anqp_add_element(buf, ANQP_VENUE_URL);
  293. for (i = 0; i < hapd->conf->venue_url_count; i++) {
  294. struct hostapd_venue_url *url;
  295. url = &hapd->conf->venue_url[i];
  296. wpabuf_put_u8(buf, 1 + url->url_len);
  297. wpabuf_put_u8(buf, url->venue_number);
  298. wpabuf_put_data(buf, url->url, url->url_len);
  299. }
  300. gas_anqp_set_element_len(buf, len);
  301. }
  302. }
  303. static void anqp_add_network_auth_type(struct hostapd_data *hapd,
  304. struct wpabuf *buf)
  305. {
  306. if (anqp_add_override(hapd, buf, ANQP_NETWORK_AUTH_TYPE))
  307. return;
  308. if (hapd->conf->network_auth_type) {
  309. wpabuf_put_le16(buf, ANQP_NETWORK_AUTH_TYPE);
  310. wpabuf_put_le16(buf, hapd->conf->network_auth_type_len);
  311. wpabuf_put_data(buf, hapd->conf->network_auth_type,
  312. hapd->conf->network_auth_type_len);
  313. }
  314. }
  315. static void anqp_add_roaming_consortium(struct hostapd_data *hapd,
  316. struct wpabuf *buf)
  317. {
  318. unsigned int i;
  319. u8 *len;
  320. if (anqp_add_override(hapd, buf, ANQP_ROAMING_CONSORTIUM))
  321. return;
  322. len = gas_anqp_add_element(buf, ANQP_ROAMING_CONSORTIUM);
  323. for (i = 0; i < hapd->conf->roaming_consortium_count; i++) {
  324. struct hostapd_roaming_consortium *rc;
  325. rc = &hapd->conf->roaming_consortium[i];
  326. wpabuf_put_u8(buf, rc->len);
  327. wpabuf_put_data(buf, rc->oi, rc->len);
  328. }
  329. gas_anqp_set_element_len(buf, len);
  330. }
  331. static void anqp_add_ip_addr_type_availability(struct hostapd_data *hapd,
  332. struct wpabuf *buf)
  333. {
  334. if (anqp_add_override(hapd, buf, ANQP_IP_ADDR_TYPE_AVAILABILITY))
  335. return;
  336. if (hapd->conf->ipaddr_type_configured) {
  337. wpabuf_put_le16(buf, ANQP_IP_ADDR_TYPE_AVAILABILITY);
  338. wpabuf_put_le16(buf, 1);
  339. wpabuf_put_u8(buf, hapd->conf->ipaddr_type_availability);
  340. }
  341. }
  342. static void anqp_add_nai_realm_eap(struct wpabuf *buf,
  343. struct hostapd_nai_realm_data *realm)
  344. {
  345. unsigned int i, j;
  346. wpabuf_put_u8(buf, realm->eap_method_count);
  347. for (i = 0; i < realm->eap_method_count; i++) {
  348. struct hostapd_nai_realm_eap *eap = &realm->eap_method[i];
  349. wpabuf_put_u8(buf, 2 + (3 * eap->num_auths));
  350. wpabuf_put_u8(buf, eap->eap_method);
  351. wpabuf_put_u8(buf, eap->num_auths);
  352. for (j = 0; j < eap->num_auths; j++) {
  353. wpabuf_put_u8(buf, eap->auth_id[j]);
  354. wpabuf_put_u8(buf, 1);
  355. wpabuf_put_u8(buf, eap->auth_val[j]);
  356. }
  357. }
  358. }
  359. static void anqp_add_nai_realm_data(struct wpabuf *buf,
  360. struct hostapd_nai_realm_data *realm,
  361. unsigned int realm_idx)
  362. {
  363. u8 *realm_data_len;
  364. wpa_printf(MSG_DEBUG, "realm=%s, len=%d", realm->realm[realm_idx],
  365. (int) os_strlen(realm->realm[realm_idx]));
  366. realm_data_len = wpabuf_put(buf, 2);
  367. wpabuf_put_u8(buf, realm->encoding);
  368. wpabuf_put_u8(buf, os_strlen(realm->realm[realm_idx]));
  369. wpabuf_put_str(buf, realm->realm[realm_idx]);
  370. anqp_add_nai_realm_eap(buf, realm);
  371. gas_anqp_set_element_len(buf, realm_data_len);
  372. }
  373. static int hs20_add_nai_home_realm_matches(struct hostapd_data *hapd,
  374. struct wpabuf *buf,
  375. const u8 *home_realm,
  376. size_t home_realm_len)
  377. {
  378. unsigned int i, j, k;
  379. u8 num_realms, num_matching = 0, encoding, realm_len, *realm_list_len;
  380. struct hostapd_nai_realm_data *realm;
  381. const u8 *pos, *realm_name, *end;
  382. struct {
  383. unsigned int realm_data_idx;
  384. unsigned int realm_idx;
  385. } matches[10];
  386. pos = home_realm;
  387. end = pos + home_realm_len;
  388. if (end - pos < 1) {
  389. wpa_hexdump(MSG_DEBUG, "Too short NAI Home Realm Query",
  390. home_realm, home_realm_len);
  391. return -1;
  392. }
  393. num_realms = *pos++;
  394. for (i = 0; i < num_realms && num_matching < 10; i++) {
  395. if (end - pos < 2) {
  396. wpa_hexdump(MSG_DEBUG,
  397. "Truncated NAI Home Realm Query",
  398. home_realm, home_realm_len);
  399. return -1;
  400. }
  401. encoding = *pos++;
  402. realm_len = *pos++;
  403. if (realm_len > end - pos) {
  404. wpa_hexdump(MSG_DEBUG,
  405. "Truncated NAI Home Realm Query",
  406. home_realm, home_realm_len);
  407. return -1;
  408. }
  409. realm_name = pos;
  410. for (j = 0; j < hapd->conf->nai_realm_count &&
  411. num_matching < 10; j++) {
  412. const u8 *rpos, *rend;
  413. realm = &hapd->conf->nai_realm_data[j];
  414. if (encoding != realm->encoding)
  415. continue;
  416. rpos = realm_name;
  417. while (rpos < realm_name + realm_len &&
  418. num_matching < 10) {
  419. for (rend = rpos;
  420. rend < realm_name + realm_len; rend++) {
  421. if (*rend == ';')
  422. break;
  423. }
  424. for (k = 0; k < MAX_NAI_REALMS &&
  425. realm->realm[k] &&
  426. num_matching < 10; k++) {
  427. if ((int) os_strlen(realm->realm[k]) !=
  428. rend - rpos ||
  429. os_strncmp((char *) rpos,
  430. realm->realm[k],
  431. rend - rpos) != 0)
  432. continue;
  433. matches[num_matching].realm_data_idx =
  434. j;
  435. matches[num_matching].realm_idx = k;
  436. num_matching++;
  437. }
  438. rpos = rend + 1;
  439. }
  440. }
  441. pos += realm_len;
  442. }
  443. realm_list_len = gas_anqp_add_element(buf, ANQP_NAI_REALM);
  444. wpabuf_put_le16(buf, num_matching);
  445. /*
  446. * There are two ways to format. 1. each realm in a NAI Realm Data unit
  447. * 2. all realms that share the same EAP methods in a NAI Realm Data
  448. * unit. The first format is likely to be bigger in size than the
  449. * second, but may be easier to parse and process by the receiver.
  450. */
  451. for (i = 0; i < num_matching; i++) {
  452. wpa_printf(MSG_DEBUG, "realm_idx %d, realm_data_idx %d",
  453. matches[i].realm_data_idx, matches[i].realm_idx);
  454. realm = &hapd->conf->nai_realm_data[matches[i].realm_data_idx];
  455. anqp_add_nai_realm_data(buf, realm, matches[i].realm_idx);
  456. }
  457. gas_anqp_set_element_len(buf, realm_list_len);
  458. return 0;
  459. }
  460. static void anqp_add_nai_realm(struct hostapd_data *hapd, struct wpabuf *buf,
  461. const u8 *home_realm, size_t home_realm_len,
  462. int nai_realm, int nai_home_realm)
  463. {
  464. if (nai_realm && !nai_home_realm &&
  465. anqp_add_override(hapd, buf, ANQP_NAI_REALM))
  466. return;
  467. if (nai_realm && hapd->conf->nai_realm_data) {
  468. u8 *len;
  469. unsigned int i, j;
  470. len = gas_anqp_add_element(buf, ANQP_NAI_REALM);
  471. wpabuf_put_le16(buf, hapd->conf->nai_realm_count);
  472. for (i = 0; i < hapd->conf->nai_realm_count; i++) {
  473. u8 *realm_data_len, *realm_len;
  474. struct hostapd_nai_realm_data *realm;
  475. realm = &hapd->conf->nai_realm_data[i];
  476. realm_data_len = wpabuf_put(buf, 2);
  477. wpabuf_put_u8(buf, realm->encoding);
  478. realm_len = wpabuf_put(buf, 1);
  479. for (j = 0; realm->realm[j]; j++) {
  480. if (j > 0)
  481. wpabuf_put_u8(buf, ';');
  482. wpabuf_put_str(buf, realm->realm[j]);
  483. }
  484. *realm_len = (u8 *) wpabuf_put(buf, 0) - realm_len - 1;
  485. anqp_add_nai_realm_eap(buf, realm);
  486. gas_anqp_set_element_len(buf, realm_data_len);
  487. }
  488. gas_anqp_set_element_len(buf, len);
  489. } else if (nai_home_realm && hapd->conf->nai_realm_data && home_realm) {
  490. hs20_add_nai_home_realm_matches(hapd, buf, home_realm,
  491. home_realm_len);
  492. }
  493. }
  494. static void anqp_add_3gpp_cellular_network(struct hostapd_data *hapd,
  495. struct wpabuf *buf)
  496. {
  497. if (anqp_add_override(hapd, buf, ANQP_3GPP_CELLULAR_NETWORK))
  498. return;
  499. if (hapd->conf->anqp_3gpp_cell_net) {
  500. wpabuf_put_le16(buf, ANQP_3GPP_CELLULAR_NETWORK);
  501. wpabuf_put_le16(buf,
  502. hapd->conf->anqp_3gpp_cell_net_len);
  503. wpabuf_put_data(buf, hapd->conf->anqp_3gpp_cell_net,
  504. hapd->conf->anqp_3gpp_cell_net_len);
  505. }
  506. }
  507. static void anqp_add_domain_name(struct hostapd_data *hapd, struct wpabuf *buf)
  508. {
  509. if (anqp_add_override(hapd, buf, ANQP_DOMAIN_NAME))
  510. return;
  511. if (hapd->conf->domain_name) {
  512. wpabuf_put_le16(buf, ANQP_DOMAIN_NAME);
  513. wpabuf_put_le16(buf, hapd->conf->domain_name_len);
  514. wpabuf_put_data(buf, hapd->conf->domain_name,
  515. hapd->conf->domain_name_len);
  516. }
  517. }
  518. #ifdef CONFIG_FILS
  519. static void anqp_add_fils_realm_info(struct hostapd_data *hapd,
  520. struct wpabuf *buf)
  521. {
  522. size_t count;
  523. if (anqp_add_override(hapd, buf, ANQP_FILS_REALM_INFO))
  524. return;
  525. count = dl_list_len(&hapd->conf->fils_realms);
  526. if (count > 10000)
  527. count = 10000;
  528. if (count) {
  529. struct fils_realm *realm;
  530. wpabuf_put_le16(buf, ANQP_FILS_REALM_INFO);
  531. wpabuf_put_le16(buf, 2 * count);
  532. dl_list_for_each(realm, &hapd->conf->fils_realms,
  533. struct fils_realm, list) {
  534. if (count == 0)
  535. break;
  536. wpabuf_put_data(buf, realm->hash, 2);
  537. count--;
  538. }
  539. }
  540. }
  541. #endif /* CONFIG_FILS */
  542. #ifdef CONFIG_HS20
  543. static void anqp_add_operator_friendly_name(struct hostapd_data *hapd,
  544. struct wpabuf *buf)
  545. {
  546. if (hapd->conf->hs20_oper_friendly_name) {
  547. u8 *len;
  548. unsigned int i;
  549. len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  550. wpabuf_put_be24(buf, OUI_WFA);
  551. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  552. wpabuf_put_u8(buf, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  553. wpabuf_put_u8(buf, 0); /* Reserved */
  554. for (i = 0; i < hapd->conf->hs20_oper_friendly_name_count; i++)
  555. {
  556. struct hostapd_lang_string *vn;
  557. vn = &hapd->conf->hs20_oper_friendly_name[i];
  558. wpabuf_put_u8(buf, 3 + vn->name_len);
  559. wpabuf_put_data(buf, vn->lang, 3);
  560. wpabuf_put_data(buf, vn->name, vn->name_len);
  561. }
  562. gas_anqp_set_element_len(buf, len);
  563. }
  564. }
  565. static void anqp_add_wan_metrics(struct hostapd_data *hapd,
  566. struct wpabuf *buf)
  567. {
  568. if (hapd->conf->hs20_wan_metrics) {
  569. u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  570. wpabuf_put_be24(buf, OUI_WFA);
  571. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  572. wpabuf_put_u8(buf, HS20_STYPE_WAN_METRICS);
  573. wpabuf_put_u8(buf, 0); /* Reserved */
  574. wpabuf_put_data(buf, hapd->conf->hs20_wan_metrics, 13);
  575. gas_anqp_set_element_len(buf, len);
  576. }
  577. }
  578. static void anqp_add_connection_capability(struct hostapd_data *hapd,
  579. struct wpabuf *buf)
  580. {
  581. if (hapd->conf->hs20_connection_capability) {
  582. u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  583. wpabuf_put_be24(buf, OUI_WFA);
  584. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  585. wpabuf_put_u8(buf, HS20_STYPE_CONNECTION_CAPABILITY);
  586. wpabuf_put_u8(buf, 0); /* Reserved */
  587. wpabuf_put_data(buf, hapd->conf->hs20_connection_capability,
  588. hapd->conf->hs20_connection_capability_len);
  589. gas_anqp_set_element_len(buf, len);
  590. }
  591. }
  592. static void anqp_add_operating_class(struct hostapd_data *hapd,
  593. struct wpabuf *buf)
  594. {
  595. if (hapd->conf->hs20_operating_class) {
  596. u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  597. wpabuf_put_be24(buf, OUI_WFA);
  598. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  599. wpabuf_put_u8(buf, HS20_STYPE_OPERATING_CLASS);
  600. wpabuf_put_u8(buf, 0); /* Reserved */
  601. wpabuf_put_data(buf, hapd->conf->hs20_operating_class,
  602. hapd->conf->hs20_operating_class_len);
  603. gas_anqp_set_element_len(buf, len);
  604. }
  605. }
  606. static void anqp_add_osu_provider(struct wpabuf *buf,
  607. struct hostapd_bss_config *bss,
  608. struct hs20_osu_provider *p)
  609. {
  610. u8 *len, *len2, *count;
  611. unsigned int i;
  612. len = wpabuf_put(buf, 2); /* OSU Provider Length to be filled */
  613. /* OSU Friendly Name Duples */
  614. len2 = wpabuf_put(buf, 2);
  615. for (i = 0; i < p->friendly_name_count; i++) {
  616. struct hostapd_lang_string *s = &p->friendly_name[i];
  617. wpabuf_put_u8(buf, 3 + s->name_len);
  618. wpabuf_put_data(buf, s->lang, 3);
  619. wpabuf_put_data(buf, s->name, s->name_len);
  620. }
  621. WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
  622. /* OSU Server URI */
  623. if (p->server_uri) {
  624. wpabuf_put_u8(buf, os_strlen(p->server_uri));
  625. wpabuf_put_str(buf, p->server_uri);
  626. } else
  627. wpabuf_put_u8(buf, 0);
  628. /* OSU Method List */
  629. count = wpabuf_put(buf, 1);
  630. for (i = 0; p->method_list && p->method_list[i] >= 0; i++)
  631. wpabuf_put_u8(buf, p->method_list[i]);
  632. *count = i;
  633. /* Icons Available */
  634. len2 = wpabuf_put(buf, 2);
  635. for (i = 0; i < p->icons_count; i++) {
  636. size_t j;
  637. struct hs20_icon *icon = NULL;
  638. for (j = 0; j < bss->hs20_icons_count && !icon; j++) {
  639. if (os_strcmp(p->icons[i], bss->hs20_icons[j].name) ==
  640. 0)
  641. icon = &bss->hs20_icons[j];
  642. }
  643. if (!icon)
  644. continue; /* icon info not found */
  645. wpabuf_put_le16(buf, icon->width);
  646. wpabuf_put_le16(buf, icon->height);
  647. wpabuf_put_data(buf, icon->language, 3);
  648. wpabuf_put_u8(buf, os_strlen(icon->type));
  649. wpabuf_put_str(buf, icon->type);
  650. wpabuf_put_u8(buf, os_strlen(icon->name));
  651. wpabuf_put_str(buf, icon->name);
  652. }
  653. WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
  654. /* OSU_NAI */
  655. if (p->osu_nai) {
  656. wpabuf_put_u8(buf, os_strlen(p->osu_nai));
  657. wpabuf_put_str(buf, p->osu_nai);
  658. } else
  659. wpabuf_put_u8(buf, 0);
  660. /* OSU Service Description Duples */
  661. len2 = wpabuf_put(buf, 2);
  662. for (i = 0; i < p->service_desc_count; i++) {
  663. struct hostapd_lang_string *s = &p->service_desc[i];
  664. wpabuf_put_u8(buf, 3 + s->name_len);
  665. wpabuf_put_data(buf, s->lang, 3);
  666. wpabuf_put_data(buf, s->name, s->name_len);
  667. }
  668. WPA_PUT_LE16(len2, (u8 *) wpabuf_put(buf, 0) - len2 - 2);
  669. WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
  670. }
  671. static void anqp_add_osu_providers_list(struct hostapd_data *hapd,
  672. struct wpabuf *buf)
  673. {
  674. if (hapd->conf->hs20_osu_providers_count) {
  675. size_t i;
  676. u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  677. wpabuf_put_be24(buf, OUI_WFA);
  678. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  679. wpabuf_put_u8(buf, HS20_STYPE_OSU_PROVIDERS_LIST);
  680. wpabuf_put_u8(buf, 0); /* Reserved */
  681. /* OSU SSID */
  682. wpabuf_put_u8(buf, hapd->conf->osu_ssid_len);
  683. wpabuf_put_data(buf, hapd->conf->osu_ssid,
  684. hapd->conf->osu_ssid_len);
  685. /* Number of OSU Providers */
  686. wpabuf_put_u8(buf, hapd->conf->hs20_osu_providers_count);
  687. for (i = 0; i < hapd->conf->hs20_osu_providers_count; i++) {
  688. anqp_add_osu_provider(
  689. buf, hapd->conf,
  690. &hapd->conf->hs20_osu_providers[i]);
  691. }
  692. gas_anqp_set_element_len(buf, len);
  693. }
  694. }
  695. static void anqp_add_icon_binary_file(struct hostapd_data *hapd,
  696. struct wpabuf *buf,
  697. const u8 *name, size_t name_len)
  698. {
  699. struct hs20_icon *icon;
  700. size_t i;
  701. u8 *len;
  702. wpa_hexdump_ascii(MSG_DEBUG, "HS 2.0: Requested Icon Filename",
  703. name, name_len);
  704. for (i = 0; i < hapd->conf->hs20_icons_count; i++) {
  705. icon = &hapd->conf->hs20_icons[i];
  706. if (name_len == os_strlen(icon->name) &&
  707. os_memcmp(name, icon->name, name_len) == 0)
  708. break;
  709. }
  710. if (i < hapd->conf->hs20_icons_count)
  711. icon = &hapd->conf->hs20_icons[i];
  712. else
  713. icon = NULL;
  714. len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  715. wpabuf_put_be24(buf, OUI_WFA);
  716. wpabuf_put_u8(buf, HS20_ANQP_OUI_TYPE);
  717. wpabuf_put_u8(buf, HS20_STYPE_ICON_BINARY_FILE);
  718. wpabuf_put_u8(buf, 0); /* Reserved */
  719. if (icon) {
  720. char *data;
  721. size_t data_len;
  722. data = os_readfile(icon->file, &data_len);
  723. if (data == NULL || data_len > 65535) {
  724. wpabuf_put_u8(buf, 2); /* Download Status:
  725. * Unspecified file error */
  726. wpabuf_put_u8(buf, 0);
  727. wpabuf_put_le16(buf, 0);
  728. } else {
  729. wpabuf_put_u8(buf, 0); /* Download Status: Success */
  730. wpabuf_put_u8(buf, os_strlen(icon->type));
  731. wpabuf_put_str(buf, icon->type);
  732. wpabuf_put_le16(buf, data_len);
  733. wpabuf_put_data(buf, data, data_len);
  734. }
  735. os_free(data);
  736. } else {
  737. wpabuf_put_u8(buf, 1); /* Download Status: File not found */
  738. wpabuf_put_u8(buf, 0);
  739. wpabuf_put_le16(buf, 0);
  740. }
  741. gas_anqp_set_element_len(buf, len);
  742. }
  743. #endif /* CONFIG_HS20 */
  744. #ifdef CONFIG_MBO
  745. static void anqp_add_mbo_cell_data_conn_pref(struct hostapd_data *hapd,
  746. struct wpabuf *buf)
  747. {
  748. if (hapd->conf->mbo_cell_data_conn_pref >= 0) {
  749. u8 *len = gas_anqp_add_element(buf, ANQP_VENDOR_SPECIFIC);
  750. wpabuf_put_be24(buf, OUI_WFA);
  751. wpabuf_put_u8(buf, MBO_ANQP_OUI_TYPE);
  752. wpabuf_put_u8(buf, MBO_ANQP_SUBTYPE_CELL_CONN_PREF);
  753. wpabuf_put_u8(buf, hapd->conf->mbo_cell_data_conn_pref);
  754. gas_anqp_set_element_len(buf, len);
  755. }
  756. }
  757. #endif /* CONFIG_MBO */
  758. static size_t anqp_get_required_len(struct hostapd_data *hapd,
  759. const u16 *infoid,
  760. unsigned int num_infoid)
  761. {
  762. size_t len = 0;
  763. unsigned int i;
  764. for (i = 0; i < num_infoid; i++) {
  765. struct anqp_element *elem = get_anqp_elem(hapd, infoid[i]);
  766. if (elem)
  767. len += 2 + 2 + wpabuf_len(elem->payload);
  768. }
  769. return len;
  770. }
  771. static struct wpabuf *
  772. gas_serv_build_gas_resp_payload(struct hostapd_data *hapd,
  773. unsigned int request,
  774. const u8 *home_realm, size_t home_realm_len,
  775. const u8 *icon_name, size_t icon_name_len,
  776. const u16 *extra_req,
  777. unsigned int num_extra_req)
  778. {
  779. struct wpabuf *buf;
  780. size_t len;
  781. unsigned int i;
  782. len = 1400;
  783. if (request & (ANQP_REQ_NAI_REALM | ANQP_REQ_NAI_HOME_REALM))
  784. len += 1000;
  785. if (request & ANQP_REQ_ICON_REQUEST)
  786. len += 65536;
  787. #ifdef CONFIG_FILS
  788. if (request & ANQP_FILS_REALM_INFO)
  789. len += 2 * dl_list_len(&hapd->conf->fils_realms);
  790. #endif /* CONFIG_FILS */
  791. len += anqp_get_required_len(hapd, extra_req, num_extra_req);
  792. buf = wpabuf_alloc(len);
  793. if (buf == NULL)
  794. return NULL;
  795. if (request & ANQP_REQ_CAPABILITY_LIST)
  796. anqp_add_capab_list(hapd, buf);
  797. if (request & ANQP_REQ_VENUE_NAME)
  798. anqp_add_venue_name(hapd, buf);
  799. if (request & ANQP_REQ_EMERGENCY_CALL_NUMBER)
  800. anqp_add_elem(hapd, buf, ANQP_EMERGENCY_CALL_NUMBER);
  801. if (request & ANQP_REQ_NETWORK_AUTH_TYPE)
  802. anqp_add_network_auth_type(hapd, buf);
  803. if (request & ANQP_REQ_ROAMING_CONSORTIUM)
  804. anqp_add_roaming_consortium(hapd, buf);
  805. if (request & ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY)
  806. anqp_add_ip_addr_type_availability(hapd, buf);
  807. if (request & (ANQP_REQ_NAI_REALM | ANQP_REQ_NAI_HOME_REALM))
  808. anqp_add_nai_realm(hapd, buf, home_realm, home_realm_len,
  809. request & ANQP_REQ_NAI_REALM,
  810. request & ANQP_REQ_NAI_HOME_REALM);
  811. if (request & ANQP_REQ_3GPP_CELLULAR_NETWORK)
  812. anqp_add_3gpp_cellular_network(hapd, buf);
  813. if (request & ANQP_REQ_AP_GEOSPATIAL_LOCATION)
  814. anqp_add_elem(hapd, buf, ANQP_AP_GEOSPATIAL_LOCATION);
  815. if (request & ANQP_REQ_AP_CIVIC_LOCATION)
  816. anqp_add_elem(hapd, buf, ANQP_AP_CIVIC_LOCATION);
  817. if (request & ANQP_REQ_AP_LOCATION_PUBLIC_URI)
  818. anqp_add_elem(hapd, buf, ANQP_AP_LOCATION_PUBLIC_URI);
  819. if (request & ANQP_REQ_DOMAIN_NAME)
  820. anqp_add_domain_name(hapd, buf);
  821. if (request & ANQP_REQ_EMERGENCY_ALERT_URI)
  822. anqp_add_elem(hapd, buf, ANQP_EMERGENCY_ALERT_URI);
  823. if (request & ANQP_REQ_TDLS_CAPABILITY)
  824. anqp_add_elem(hapd, buf, ANQP_TDLS_CAPABILITY);
  825. if (request & ANQP_REQ_EMERGENCY_NAI)
  826. anqp_add_elem(hapd, buf, ANQP_EMERGENCY_NAI);
  827. for (i = 0; i < num_extra_req; i++) {
  828. #ifdef CONFIG_FILS
  829. if (extra_req[i] == ANQP_FILS_REALM_INFO) {
  830. anqp_add_fils_realm_info(hapd, buf);
  831. continue;
  832. }
  833. #endif /* CONFIG_FILS */
  834. if (extra_req[i] == ANQP_VENUE_URL) {
  835. anqp_add_venue_url(hapd, buf);
  836. continue;
  837. }
  838. anqp_add_elem(hapd, buf, extra_req[i]);
  839. }
  840. #ifdef CONFIG_HS20
  841. if (request & ANQP_REQ_HS_CAPABILITY_LIST)
  842. anqp_add_hs_capab_list(hapd, buf);
  843. if (request & ANQP_REQ_OPERATOR_FRIENDLY_NAME)
  844. anqp_add_operator_friendly_name(hapd, buf);
  845. if (request & ANQP_REQ_WAN_METRICS)
  846. anqp_add_wan_metrics(hapd, buf);
  847. if (request & ANQP_REQ_CONNECTION_CAPABILITY)
  848. anqp_add_connection_capability(hapd, buf);
  849. if (request & ANQP_REQ_OPERATING_CLASS)
  850. anqp_add_operating_class(hapd, buf);
  851. if (request & ANQP_REQ_OSU_PROVIDERS_LIST)
  852. anqp_add_osu_providers_list(hapd, buf);
  853. if (request & ANQP_REQ_ICON_REQUEST)
  854. anqp_add_icon_binary_file(hapd, buf, icon_name, icon_name_len);
  855. #endif /* CONFIG_HS20 */
  856. #ifdef CONFIG_MBO
  857. if (request & ANQP_REQ_MBO_CELL_DATA_CONN_PREF)
  858. anqp_add_mbo_cell_data_conn_pref(hapd, buf);
  859. #endif /* CONFIG_MBO */
  860. return buf;
  861. }
  862. #define ANQP_MAX_EXTRA_REQ 20
  863. struct anqp_query_info {
  864. unsigned int request;
  865. const u8 *home_realm_query;
  866. size_t home_realm_query_len;
  867. const u8 *icon_name;
  868. size_t icon_name_len;
  869. int p2p_sd;
  870. u16 extra_req[ANQP_MAX_EXTRA_REQ];
  871. unsigned int num_extra_req;
  872. };
  873. static void set_anqp_req(unsigned int bit, const char *name, int local,
  874. struct anqp_query_info *qi)
  875. {
  876. qi->request |= bit;
  877. if (local) {
  878. wpa_printf(MSG_DEBUG, "ANQP: %s (local)", name);
  879. } else {
  880. wpa_printf(MSG_DEBUG, "ANQP: %s not available", name);
  881. }
  882. }
  883. static void rx_anqp_query_list_id(struct hostapd_data *hapd, u16 info_id,
  884. struct anqp_query_info *qi)
  885. {
  886. switch (info_id) {
  887. case ANQP_CAPABILITY_LIST:
  888. set_anqp_req(ANQP_REQ_CAPABILITY_LIST, "Capability List", 1,
  889. qi);
  890. break;
  891. case ANQP_VENUE_NAME:
  892. set_anqp_req(ANQP_REQ_VENUE_NAME, "Venue Name",
  893. hapd->conf->venue_name != NULL, qi);
  894. break;
  895. case ANQP_EMERGENCY_CALL_NUMBER:
  896. set_anqp_req(ANQP_REQ_EMERGENCY_CALL_NUMBER,
  897. "Emergency Call Number",
  898. get_anqp_elem(hapd, info_id) != NULL, qi);
  899. break;
  900. case ANQP_NETWORK_AUTH_TYPE:
  901. set_anqp_req(ANQP_REQ_NETWORK_AUTH_TYPE, "Network Auth Type",
  902. hapd->conf->network_auth_type != NULL, qi);
  903. break;
  904. case ANQP_ROAMING_CONSORTIUM:
  905. set_anqp_req(ANQP_REQ_ROAMING_CONSORTIUM, "Roaming Consortium",
  906. hapd->conf->roaming_consortium != NULL, qi);
  907. break;
  908. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  909. set_anqp_req(ANQP_REQ_IP_ADDR_TYPE_AVAILABILITY,
  910. "IP Addr Type Availability",
  911. hapd->conf->ipaddr_type_configured, qi);
  912. break;
  913. case ANQP_NAI_REALM:
  914. set_anqp_req(ANQP_REQ_NAI_REALM, "NAI Realm",
  915. hapd->conf->nai_realm_data != NULL, qi);
  916. break;
  917. case ANQP_3GPP_CELLULAR_NETWORK:
  918. set_anqp_req(ANQP_REQ_3GPP_CELLULAR_NETWORK,
  919. "3GPP Cellular Network",
  920. hapd->conf->anqp_3gpp_cell_net != NULL, qi);
  921. break;
  922. case ANQP_AP_GEOSPATIAL_LOCATION:
  923. set_anqp_req(ANQP_REQ_AP_GEOSPATIAL_LOCATION,
  924. "AP Geospatial Location",
  925. get_anqp_elem(hapd, info_id) != NULL, qi);
  926. break;
  927. case ANQP_AP_CIVIC_LOCATION:
  928. set_anqp_req(ANQP_REQ_AP_CIVIC_LOCATION,
  929. "AP Civic Location",
  930. get_anqp_elem(hapd, info_id) != NULL, qi);
  931. break;
  932. case ANQP_AP_LOCATION_PUBLIC_URI:
  933. set_anqp_req(ANQP_REQ_AP_LOCATION_PUBLIC_URI,
  934. "AP Location Public URI",
  935. get_anqp_elem(hapd, info_id) != NULL, qi);
  936. break;
  937. case ANQP_DOMAIN_NAME:
  938. set_anqp_req(ANQP_REQ_DOMAIN_NAME, "Domain Name",
  939. hapd->conf->domain_name != NULL, qi);
  940. break;
  941. case ANQP_EMERGENCY_ALERT_URI:
  942. set_anqp_req(ANQP_REQ_EMERGENCY_ALERT_URI,
  943. "Emergency Alert URI",
  944. get_anqp_elem(hapd, info_id) != NULL, qi);
  945. break;
  946. case ANQP_TDLS_CAPABILITY:
  947. set_anqp_req(ANQP_REQ_TDLS_CAPABILITY,
  948. "TDLS Capability",
  949. get_anqp_elem(hapd, info_id) != NULL, qi);
  950. break;
  951. case ANQP_EMERGENCY_NAI:
  952. set_anqp_req(ANQP_REQ_EMERGENCY_NAI,
  953. "Emergency NAI",
  954. get_anqp_elem(hapd, info_id) != NULL, qi);
  955. break;
  956. default:
  957. #ifdef CONFIG_FILS
  958. if (info_id == ANQP_FILS_REALM_INFO &&
  959. !dl_list_empty(&hapd->conf->fils_realms)) {
  960. wpa_printf(MSG_DEBUG,
  961. "ANQP: FILS Realm Information (local)");
  962. } else
  963. #endif /* CONFIG_FILS */
  964. if (info_id == ANQP_VENUE_URL && hapd->conf->venue_url) {
  965. wpa_printf(MSG_DEBUG,
  966. "ANQP: Venue URL (local)");
  967. } else if (!get_anqp_elem(hapd, info_id)) {
  968. wpa_printf(MSG_DEBUG, "ANQP: Unsupported Info Id %u",
  969. info_id);
  970. break;
  971. }
  972. if (qi->num_extra_req == ANQP_MAX_EXTRA_REQ) {
  973. wpa_printf(MSG_DEBUG,
  974. "ANQP: No more room for extra requests - ignore Info Id %u",
  975. info_id);
  976. break;
  977. }
  978. wpa_printf(MSG_DEBUG, "ANQP: Info Id %u (local)", info_id);
  979. qi->extra_req[qi->num_extra_req] = info_id;
  980. qi->num_extra_req++;
  981. break;
  982. }
  983. }
  984. static void rx_anqp_query_list(struct hostapd_data *hapd,
  985. const u8 *pos, const u8 *end,
  986. struct anqp_query_info *qi)
  987. {
  988. wpa_printf(MSG_DEBUG, "ANQP: %u Info IDs requested in Query list",
  989. (unsigned int) (end - pos) / 2);
  990. while (end - pos >= 2) {
  991. rx_anqp_query_list_id(hapd, WPA_GET_LE16(pos), qi);
  992. pos += 2;
  993. }
  994. }
  995. #ifdef CONFIG_HS20
  996. static void rx_anqp_hs_query_list(struct hostapd_data *hapd, u8 subtype,
  997. struct anqp_query_info *qi)
  998. {
  999. switch (subtype) {
  1000. case HS20_STYPE_CAPABILITY_LIST:
  1001. set_anqp_req(ANQP_REQ_HS_CAPABILITY_LIST, "HS Capability List",
  1002. 1, qi);
  1003. break;
  1004. case HS20_STYPE_OPERATOR_FRIENDLY_NAME:
  1005. set_anqp_req(ANQP_REQ_OPERATOR_FRIENDLY_NAME,
  1006. "Operator Friendly Name",
  1007. hapd->conf->hs20_oper_friendly_name != NULL, qi);
  1008. break;
  1009. case HS20_STYPE_WAN_METRICS:
  1010. set_anqp_req(ANQP_REQ_WAN_METRICS, "WAN Metrics",
  1011. hapd->conf->hs20_wan_metrics != NULL, qi);
  1012. break;
  1013. case HS20_STYPE_CONNECTION_CAPABILITY:
  1014. set_anqp_req(ANQP_REQ_CONNECTION_CAPABILITY,
  1015. "Connection Capability",
  1016. hapd->conf->hs20_connection_capability != NULL,
  1017. qi);
  1018. break;
  1019. case HS20_STYPE_OPERATING_CLASS:
  1020. set_anqp_req(ANQP_REQ_OPERATING_CLASS, "Operating Class",
  1021. hapd->conf->hs20_operating_class != NULL, qi);
  1022. break;
  1023. case HS20_STYPE_OSU_PROVIDERS_LIST:
  1024. set_anqp_req(ANQP_REQ_OSU_PROVIDERS_LIST, "OSU Providers list",
  1025. hapd->conf->hs20_osu_providers_count, qi);
  1026. break;
  1027. default:
  1028. wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 subtype %u",
  1029. subtype);
  1030. break;
  1031. }
  1032. }
  1033. static void rx_anqp_hs_nai_home_realm(struct hostapd_data *hapd,
  1034. const u8 *pos, const u8 *end,
  1035. struct anqp_query_info *qi)
  1036. {
  1037. qi->request |= ANQP_REQ_NAI_HOME_REALM;
  1038. qi->home_realm_query = pos;
  1039. qi->home_realm_query_len = end - pos;
  1040. if (hapd->conf->nai_realm_data != NULL) {
  1041. wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 NAI Home Realm Query "
  1042. "(local)");
  1043. } else {
  1044. wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 NAI Home Realm Query not "
  1045. "available");
  1046. }
  1047. }
  1048. static void rx_anqp_hs_icon_request(struct hostapd_data *hapd,
  1049. const u8 *pos, const u8 *end,
  1050. struct anqp_query_info *qi)
  1051. {
  1052. qi->request |= ANQP_REQ_ICON_REQUEST;
  1053. qi->icon_name = pos;
  1054. qi->icon_name_len = end - pos;
  1055. if (hapd->conf->hs20_icons_count) {
  1056. wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Icon Request Query "
  1057. "(local)");
  1058. } else {
  1059. wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Icon Request Query not "
  1060. "available");
  1061. }
  1062. }
  1063. static void rx_anqp_vendor_specific_hs20(struct hostapd_data *hapd,
  1064. const u8 *pos, const u8 *end,
  1065. struct anqp_query_info *qi)
  1066. {
  1067. u8 subtype;
  1068. if (end - pos <= 1)
  1069. return;
  1070. subtype = *pos++;
  1071. pos++; /* Reserved */
  1072. switch (subtype) {
  1073. case HS20_STYPE_QUERY_LIST:
  1074. wpa_printf(MSG_DEBUG, "ANQP: HS 2.0 Query List");
  1075. while (pos < end) {
  1076. rx_anqp_hs_query_list(hapd, *pos, qi);
  1077. pos++;
  1078. }
  1079. break;
  1080. case HS20_STYPE_NAI_HOME_REALM_QUERY:
  1081. rx_anqp_hs_nai_home_realm(hapd, pos, end, qi);
  1082. break;
  1083. case HS20_STYPE_ICON_REQUEST:
  1084. rx_anqp_hs_icon_request(hapd, pos, end, qi);
  1085. break;
  1086. default:
  1087. wpa_printf(MSG_DEBUG, "ANQP: Unsupported HS 2.0 query subtype "
  1088. "%u", subtype);
  1089. break;
  1090. }
  1091. }
  1092. #endif /* CONFIG_HS20 */
  1093. #ifdef CONFIG_P2P
  1094. static void rx_anqp_vendor_specific_p2p(struct hostapd_data *hapd,
  1095. struct anqp_query_info *qi)
  1096. {
  1097. /*
  1098. * This is for P2P SD and will be taken care of by the P2P
  1099. * implementation. This query needs to be ignored in the generic
  1100. * GAS server to avoid duplicated response.
  1101. */
  1102. wpa_printf(MSG_DEBUG,
  1103. "ANQP: Ignore WFA vendor type %u (P2P SD) in generic GAS server",
  1104. P2P_OUI_TYPE);
  1105. qi->p2p_sd = 1;
  1106. return;
  1107. }
  1108. #endif /* CONFIG_P2P */
  1109. #ifdef CONFIG_MBO
  1110. static void rx_anqp_mbo_query_list(struct hostapd_data *hapd, u8 subtype,
  1111. struct anqp_query_info *qi)
  1112. {
  1113. switch (subtype) {
  1114. case MBO_ANQP_SUBTYPE_CELL_CONN_PREF:
  1115. set_anqp_req(ANQP_REQ_MBO_CELL_DATA_CONN_PREF,
  1116. "Cellular Data Connection Preference",
  1117. hapd->conf->mbo_cell_data_conn_pref >= 0, qi);
  1118. break;
  1119. default:
  1120. wpa_printf(MSG_DEBUG, "ANQP: Unsupported MBO subtype %u",
  1121. subtype);
  1122. break;
  1123. }
  1124. }
  1125. static void rx_anqp_vendor_specific_mbo(struct hostapd_data *hapd,
  1126. const u8 *pos, const u8 *end,
  1127. struct anqp_query_info *qi)
  1128. {
  1129. u8 subtype;
  1130. if (end - pos < 1)
  1131. return;
  1132. subtype = *pos++;
  1133. switch (subtype) {
  1134. case MBO_ANQP_SUBTYPE_QUERY_LIST:
  1135. wpa_printf(MSG_DEBUG, "ANQP: MBO Query List");
  1136. while (pos < end) {
  1137. rx_anqp_mbo_query_list(hapd, *pos, qi);
  1138. pos++;
  1139. }
  1140. break;
  1141. default:
  1142. wpa_printf(MSG_DEBUG, "ANQP: Unsupported MBO query subtype %u",
  1143. subtype);
  1144. break;
  1145. }
  1146. }
  1147. #endif /* CONFIG_MBO */
  1148. static void rx_anqp_vendor_specific(struct hostapd_data *hapd,
  1149. const u8 *pos, const u8 *end,
  1150. struct anqp_query_info *qi)
  1151. {
  1152. u32 oui;
  1153. if (end - pos < 4) {
  1154. wpa_printf(MSG_DEBUG, "ANQP: Too short vendor specific ANQP "
  1155. "Query element");
  1156. return;
  1157. }
  1158. oui = WPA_GET_BE24(pos);
  1159. pos += 3;
  1160. if (oui != OUI_WFA) {
  1161. wpa_printf(MSG_DEBUG, "ANQP: Unsupported vendor OUI %06x",
  1162. oui);
  1163. return;
  1164. }
  1165. switch (*pos) {
  1166. #ifdef CONFIG_P2P
  1167. case P2P_OUI_TYPE:
  1168. rx_anqp_vendor_specific_p2p(hapd, qi);
  1169. break;
  1170. #endif /* CONFIG_P2P */
  1171. #ifdef CONFIG_HS20
  1172. case HS20_ANQP_OUI_TYPE:
  1173. rx_anqp_vendor_specific_hs20(hapd, pos + 1, end, qi);
  1174. break;
  1175. #endif /* CONFIG_HS20 */
  1176. #ifdef CONFIG_MBO
  1177. case MBO_ANQP_OUI_TYPE:
  1178. rx_anqp_vendor_specific_mbo(hapd, pos + 1, end, qi);
  1179. break;
  1180. #endif /* CONFIG_MBO */
  1181. default:
  1182. wpa_printf(MSG_DEBUG, "ANQP: Unsupported WFA vendor type %u",
  1183. *pos);
  1184. break;
  1185. }
  1186. }
  1187. static void gas_serv_req_local_processing(struct hostapd_data *hapd,
  1188. const u8 *sa, u8 dialog_token,
  1189. struct anqp_query_info *qi, int prot,
  1190. int std_addr3)
  1191. {
  1192. struct wpabuf *buf, *tx_buf;
  1193. buf = gas_serv_build_gas_resp_payload(hapd, qi->request,
  1194. qi->home_realm_query,
  1195. qi->home_realm_query_len,
  1196. qi->icon_name, qi->icon_name_len,
  1197. qi->extra_req, qi->num_extra_req);
  1198. wpa_hexdump_buf(MSG_MSGDUMP, "ANQP: Locally generated ANQP responses",
  1199. buf);
  1200. if (!buf)
  1201. return;
  1202. #ifdef CONFIG_P2P
  1203. if (wpabuf_len(buf) == 0 && qi->p2p_sd) {
  1204. wpa_printf(MSG_DEBUG,
  1205. "ANQP: Do not send response to P2P SD from generic GAS service (P2P SD implementation will process this)");
  1206. wpabuf_free(buf);
  1207. return;
  1208. }
  1209. #endif /* CONFIG_P2P */
  1210. if (wpabuf_len(buf) > hapd->conf->gas_frag_limit ||
  1211. hapd->conf->gas_comeback_delay) {
  1212. struct gas_dialog_info *di;
  1213. u16 comeback_delay = 1;
  1214. if (hapd->conf->gas_comeback_delay) {
  1215. /* Testing - allow overriding of the delay value */
  1216. comeback_delay = hapd->conf->gas_comeback_delay;
  1217. }
  1218. wpa_printf(MSG_DEBUG, "ANQP: Too long response to fit in "
  1219. "initial response - use GAS comeback");
  1220. di = gas_dialog_create(hapd, sa, dialog_token);
  1221. if (!di) {
  1222. wpa_printf(MSG_INFO, "ANQP: Could not create dialog "
  1223. "for " MACSTR " (dialog token %u)",
  1224. MAC2STR(sa), dialog_token);
  1225. wpabuf_free(buf);
  1226. tx_buf = gas_anqp_build_initial_resp_buf(
  1227. dialog_token, WLAN_STATUS_UNSPECIFIED_FAILURE,
  1228. 0, NULL);
  1229. } else {
  1230. di->prot = prot;
  1231. di->sd_resp = buf;
  1232. di->sd_resp_pos = 0;
  1233. tx_buf = gas_anqp_build_initial_resp_buf(
  1234. dialog_token, WLAN_STATUS_SUCCESS,
  1235. comeback_delay, NULL);
  1236. }
  1237. } else {
  1238. wpa_printf(MSG_DEBUG, "ANQP: Initial response (no comeback)");
  1239. tx_buf = gas_anqp_build_initial_resp_buf(
  1240. dialog_token, WLAN_STATUS_SUCCESS, 0, buf);
  1241. wpabuf_free(buf);
  1242. }
  1243. if (!tx_buf)
  1244. return;
  1245. if (prot)
  1246. convert_to_protected_dual(tx_buf);
  1247. if (std_addr3)
  1248. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  1249. wpabuf_head(tx_buf),
  1250. wpabuf_len(tx_buf));
  1251. else
  1252. hostapd_drv_send_action_addr3_ap(hapd, hapd->iface->freq, 0, sa,
  1253. wpabuf_head(tx_buf),
  1254. wpabuf_len(tx_buf));
  1255. wpabuf_free(tx_buf);
  1256. }
  1257. #ifdef CONFIG_DPP
  1258. static void gas_serv_req_dpp_processing(struct hostapd_data *hapd,
  1259. const u8 *sa, u8 dialog_token,
  1260. int prot, struct wpabuf *buf)
  1261. {
  1262. struct wpabuf *tx_buf;
  1263. if (wpabuf_len(buf) > hapd->conf->gas_frag_limit ||
  1264. hapd->conf->gas_comeback_delay) {
  1265. struct gas_dialog_info *di;
  1266. u16 comeback_delay = 1;
  1267. if (hapd->conf->gas_comeback_delay) {
  1268. /* Testing - allow overriding of the delay value */
  1269. comeback_delay = hapd->conf->gas_comeback_delay;
  1270. }
  1271. wpa_printf(MSG_DEBUG,
  1272. "DPP: Too long response to fit in initial response - use GAS comeback");
  1273. di = gas_dialog_create(hapd, sa, dialog_token);
  1274. if (!di) {
  1275. wpa_printf(MSG_INFO, "DPP: Could not create dialog for "
  1276. MACSTR " (dialog token %u)",
  1277. MAC2STR(sa), dialog_token);
  1278. wpabuf_free(buf);
  1279. tx_buf = gas_build_initial_resp(
  1280. dialog_token, WLAN_STATUS_UNSPECIFIED_FAILURE,
  1281. 0, 10);
  1282. if (tx_buf)
  1283. gas_serv_write_dpp_adv_proto(tx_buf);
  1284. } else {
  1285. di->prot = prot;
  1286. di->sd_resp = buf;
  1287. di->sd_resp_pos = 0;
  1288. tx_buf = gas_build_initial_resp(
  1289. dialog_token, WLAN_STATUS_SUCCESS,
  1290. comeback_delay, 10);
  1291. if (tx_buf)
  1292. gas_serv_write_dpp_adv_proto(tx_buf);
  1293. }
  1294. } else {
  1295. wpa_printf(MSG_DEBUG,
  1296. "DPP: GAS Initial response (no comeback)");
  1297. tx_buf = gas_build_initial_resp(
  1298. dialog_token, WLAN_STATUS_SUCCESS, 0,
  1299. 10 + 2 + wpabuf_len(buf));
  1300. if (tx_buf) {
  1301. gas_serv_write_dpp_adv_proto(tx_buf);
  1302. wpabuf_put_le16(tx_buf, wpabuf_len(buf));
  1303. wpabuf_put_buf(tx_buf, buf);
  1304. hostapd_dpp_gas_status_handler(hapd, 1);
  1305. }
  1306. wpabuf_free(buf);
  1307. }
  1308. if (!tx_buf)
  1309. return;
  1310. if (prot)
  1311. convert_to_protected_dual(tx_buf);
  1312. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  1313. wpabuf_head(tx_buf),
  1314. wpabuf_len(tx_buf));
  1315. wpabuf_free(tx_buf);
  1316. }
  1317. #endif /* CONFIG_DPP */
  1318. static void gas_serv_rx_gas_initial_req(struct hostapd_data *hapd,
  1319. const u8 *sa,
  1320. const u8 *data, size_t len, int prot,
  1321. int std_addr3)
  1322. {
  1323. const u8 *pos = data;
  1324. const u8 *end = data + len;
  1325. const u8 *next;
  1326. u8 dialog_token;
  1327. u16 slen;
  1328. struct anqp_query_info qi;
  1329. const u8 *adv_proto;
  1330. #ifdef CONFIG_DPP
  1331. int dpp = 0;
  1332. #endif /* CONFIG_DPP */
  1333. if (len < 1 + 2)
  1334. return;
  1335. os_memset(&qi, 0, sizeof(qi));
  1336. dialog_token = *pos++;
  1337. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  1338. "GAS: GAS Initial Request from " MACSTR " (dialog token %u) ",
  1339. MAC2STR(sa), dialog_token);
  1340. if (*pos != WLAN_EID_ADV_PROTO) {
  1341. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  1342. "GAS: Unexpected IE in GAS Initial Request: %u", *pos);
  1343. return;
  1344. }
  1345. adv_proto = pos++;
  1346. slen = *pos++;
  1347. if (slen > end - pos || slen < 2) {
  1348. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  1349. "GAS: Invalid IE in GAS Initial Request");
  1350. return;
  1351. }
  1352. next = pos + slen;
  1353. pos++; /* skip QueryRespLenLimit and PAME-BI */
  1354. #ifdef CONFIG_DPP
  1355. if (slen == 8 && *pos == WLAN_EID_VENDOR_SPECIFIC &&
  1356. pos[1] == 5 && WPA_GET_BE24(&pos[2]) == OUI_WFA &&
  1357. pos[5] == DPP_OUI_TYPE && pos[6] == 0x01) {
  1358. wpa_printf(MSG_DEBUG, "DPP: Configuration Request");
  1359. dpp = 1;
  1360. } else
  1361. #endif /* CONFIG_DPP */
  1362. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1363. struct wpabuf *buf;
  1364. wpa_msg(hapd->msg_ctx, MSG_DEBUG,
  1365. "GAS: Unsupported GAS advertisement protocol id %u",
  1366. *pos);
  1367. if (sa[0] & 0x01)
  1368. return; /* Invalid source address - drop silently */
  1369. buf = gas_build_initial_resp(
  1370. dialog_token, WLAN_STATUS_GAS_ADV_PROTO_NOT_SUPPORTED,
  1371. 0, 2 + slen + 2);
  1372. if (buf == NULL)
  1373. return;
  1374. wpabuf_put_data(buf, adv_proto, 2 + slen);
  1375. wpabuf_put_le16(buf, 0); /* Query Response Length */
  1376. if (prot)
  1377. convert_to_protected_dual(buf);
  1378. if (std_addr3)
  1379. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  1380. wpabuf_head(buf),
  1381. wpabuf_len(buf));
  1382. else
  1383. hostapd_drv_send_action_addr3_ap(hapd,
  1384. hapd->iface->freq, 0,
  1385. sa, wpabuf_head(buf),
  1386. wpabuf_len(buf));
  1387. wpabuf_free(buf);
  1388. return;
  1389. }
  1390. pos = next;
  1391. /* Query Request */
  1392. if (end - pos < 2)
  1393. return;
  1394. slen = WPA_GET_LE16(pos);
  1395. pos += 2;
  1396. if (slen > end - pos)
  1397. return;
  1398. end = pos + slen;
  1399. #ifdef CONFIG_DPP
  1400. if (dpp) {
  1401. struct wpabuf *msg;
  1402. msg = hostapd_dpp_gas_req_handler(hapd, sa, pos, slen);
  1403. if (!msg)
  1404. return;
  1405. gas_serv_req_dpp_processing(hapd, sa, dialog_token, prot, msg);
  1406. return;
  1407. }
  1408. #endif /* CONFIG_DPP */
  1409. /* ANQP Query Request */
  1410. while (pos < end) {
  1411. u16 info_id, elen;
  1412. if (end - pos < 4)
  1413. return;
  1414. info_id = WPA_GET_LE16(pos);
  1415. pos += 2;
  1416. elen = WPA_GET_LE16(pos);
  1417. pos += 2;
  1418. if (elen > end - pos) {
  1419. wpa_printf(MSG_DEBUG, "ANQP: Invalid Query Request");
  1420. return;
  1421. }
  1422. switch (info_id) {
  1423. case ANQP_QUERY_LIST:
  1424. rx_anqp_query_list(hapd, pos, pos + elen, &qi);
  1425. break;
  1426. case ANQP_VENDOR_SPECIFIC:
  1427. rx_anqp_vendor_specific(hapd, pos, pos + elen, &qi);
  1428. break;
  1429. default:
  1430. wpa_printf(MSG_DEBUG, "ANQP: Unsupported Query "
  1431. "Request element %u", info_id);
  1432. break;
  1433. }
  1434. pos += elen;
  1435. }
  1436. gas_serv_req_local_processing(hapd, sa, dialog_token, &qi, prot,
  1437. std_addr3);
  1438. }
  1439. static void gas_serv_rx_gas_comeback_req(struct hostapd_data *hapd,
  1440. const u8 *sa,
  1441. const u8 *data, size_t len, int prot,
  1442. int std_addr3)
  1443. {
  1444. struct gas_dialog_info *dialog;
  1445. struct wpabuf *buf, *tx_buf;
  1446. u8 dialog_token;
  1447. size_t frag_len;
  1448. int more = 0;
  1449. wpa_hexdump(MSG_DEBUG, "GAS: RX GAS Comeback Request", data, len);
  1450. if (len < 1)
  1451. return;
  1452. dialog_token = *data;
  1453. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Dialog Token: %u",
  1454. dialog_token);
  1455. dialog = gas_serv_dialog_find(hapd, sa, dialog_token);
  1456. if (!dialog) {
  1457. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: No pending SD "
  1458. "response fragment for " MACSTR " dialog token %u",
  1459. MAC2STR(sa), dialog_token);
  1460. if (sa[0] & 0x01)
  1461. return; /* Invalid source address - drop silently */
  1462. tx_buf = gas_anqp_build_comeback_resp_buf(
  1463. dialog_token, WLAN_STATUS_NO_OUTSTANDING_GAS_REQ, 0, 0,
  1464. 0, NULL);
  1465. if (tx_buf == NULL)
  1466. return;
  1467. goto send_resp;
  1468. }
  1469. frag_len = wpabuf_len(dialog->sd_resp) - dialog->sd_resp_pos;
  1470. if (frag_len > hapd->conf->gas_frag_limit) {
  1471. frag_len = hapd->conf->gas_frag_limit;
  1472. more = 1;
  1473. }
  1474. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: resp frag_len %u",
  1475. (unsigned int) frag_len);
  1476. buf = wpabuf_alloc_copy(wpabuf_head_u8(dialog->sd_resp) +
  1477. dialog->sd_resp_pos, frag_len);
  1478. if (buf == NULL) {
  1479. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Failed to allocate "
  1480. "buffer");
  1481. gas_serv_dialog_clear(dialog);
  1482. return;
  1483. }
  1484. #ifdef CONFIG_DPP
  1485. if (dialog->dpp) {
  1486. tx_buf = gas_build_comeback_resp(dialog_token,
  1487. WLAN_STATUS_SUCCESS,
  1488. dialog->sd_frag_id, more, 0,
  1489. 10 + frag_len);
  1490. if (tx_buf) {
  1491. gas_serv_write_dpp_adv_proto(tx_buf);
  1492. wpabuf_put_buf(tx_buf, buf);
  1493. }
  1494. } else
  1495. #endif /* CONFIG_DPP */
  1496. tx_buf = gas_anqp_build_comeback_resp_buf(dialog_token,
  1497. WLAN_STATUS_SUCCESS,
  1498. dialog->sd_frag_id,
  1499. more, 0, buf);
  1500. wpabuf_free(buf);
  1501. if (tx_buf == NULL) {
  1502. gas_serv_dialog_clear(dialog);
  1503. return;
  1504. }
  1505. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: Tx GAS Comeback Response "
  1506. "(frag_id %d more=%d frag_len=%d)",
  1507. dialog->sd_frag_id, more, (int) frag_len);
  1508. dialog->sd_frag_id++;
  1509. dialog->sd_resp_pos += frag_len;
  1510. if (more) {
  1511. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: %d more bytes remain "
  1512. "to be sent",
  1513. (int) (wpabuf_len(dialog->sd_resp) -
  1514. dialog->sd_resp_pos));
  1515. } else {
  1516. wpa_msg(hapd->msg_ctx, MSG_DEBUG, "GAS: All fragments of "
  1517. "SD response sent");
  1518. #ifdef CONFIG_DPP
  1519. if (dialog->dpp)
  1520. hostapd_dpp_gas_status_handler(hapd, 1);
  1521. #endif /* CONFIG_DPP */
  1522. gas_serv_dialog_clear(dialog);
  1523. gas_serv_free_dialogs(hapd, sa);
  1524. }
  1525. send_resp:
  1526. if (prot)
  1527. convert_to_protected_dual(tx_buf);
  1528. if (std_addr3)
  1529. hostapd_drv_send_action(hapd, hapd->iface->freq, 0, sa,
  1530. wpabuf_head(tx_buf),
  1531. wpabuf_len(tx_buf));
  1532. else
  1533. hostapd_drv_send_action_addr3_ap(hapd, hapd->iface->freq, 0, sa,
  1534. wpabuf_head(tx_buf),
  1535. wpabuf_len(tx_buf));
  1536. wpabuf_free(tx_buf);
  1537. }
  1538. static void gas_serv_rx_public_action(void *ctx, const u8 *buf, size_t len,
  1539. int freq)
  1540. {
  1541. struct hostapd_data *hapd = ctx;
  1542. const struct ieee80211_mgmt *mgmt;
  1543. const u8 *sa, *data;
  1544. int prot, std_addr3;
  1545. mgmt = (const struct ieee80211_mgmt *) buf;
  1546. if (len < IEEE80211_HDRLEN + 2)
  1547. return;
  1548. if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
  1549. mgmt->u.action.category != WLAN_ACTION_PROTECTED_DUAL)
  1550. return;
  1551. /*
  1552. * Note: Public Action and Protected Dual of Public Action frames share
  1553. * the same payload structure, so it is fine to use definitions of
  1554. * Public Action frames to process both.
  1555. */
  1556. prot = mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL;
  1557. sa = mgmt->sa;
  1558. if (hapd->conf->gas_address3 == 1)
  1559. std_addr3 = 1;
  1560. else if (hapd->conf->gas_address3 == 2)
  1561. std_addr3 = 0;
  1562. else
  1563. std_addr3 = is_broadcast_ether_addr(mgmt->bssid);
  1564. len -= IEEE80211_HDRLEN + 1;
  1565. data = buf + IEEE80211_HDRLEN + 1;
  1566. switch (data[0]) {
  1567. case WLAN_PA_GAS_INITIAL_REQ:
  1568. gas_serv_rx_gas_initial_req(hapd, sa, data + 1, len - 1, prot,
  1569. std_addr3);
  1570. break;
  1571. case WLAN_PA_GAS_COMEBACK_REQ:
  1572. gas_serv_rx_gas_comeback_req(hapd, sa, data + 1, len - 1, prot,
  1573. std_addr3);
  1574. break;
  1575. }
  1576. }
  1577. int gas_serv_init(struct hostapd_data *hapd)
  1578. {
  1579. hapd->public_action_cb2 = gas_serv_rx_public_action;
  1580. hapd->public_action_cb2_ctx = hapd;
  1581. return 0;
  1582. }
  1583. void gas_serv_deinit(struct hostapd_data *hapd)
  1584. {
  1585. }