interworking.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /*
  2. * Interworking (IEEE 802.11u)
  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 "common/wpa_ctrl.h"
  13. #include "utils/pcsc_funcs.h"
  14. #include "drivers/driver.h"
  15. #include "eap_common/eap_defs.h"
  16. #include "eap_peer/eap.h"
  17. #include "eap_peer/eap_methods.h"
  18. #include "wpa_supplicant_i.h"
  19. #include "config.h"
  20. #include "config_ssid.h"
  21. #include "bss.h"
  22. #include "scan.h"
  23. #include "notify.h"
  24. #include "gas_query.h"
  25. #include "hs20_supplicant.h"
  26. #include "interworking.h"
  27. #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
  28. #define INTERWORKING_3GPP
  29. #else
  30. #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
  31. #define INTERWORKING_3GPP
  32. #else
  33. #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
  34. #define INTERWORKING_3GPP
  35. #endif
  36. #endif
  37. #endif
  38. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  39. static void interworking_reconnect(struct wpa_supplicant *wpa_s)
  40. {
  41. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  42. wpa_supplicant_cancel_sched_scan(wpa_s);
  43. wpa_supplicant_deauthenticate(wpa_s,
  44. WLAN_REASON_DEAUTH_LEAVING);
  45. }
  46. wpa_s->disconnected = 0;
  47. wpa_s->reassociate = 1;
  48. if (wpa_s->last_scan_res_used > 0) {
  49. struct os_time now;
  50. os_get_time(&now);
  51. if (now.sec - wpa_s->last_scan.sec <= 5) {
  52. wpa_printf(MSG_DEBUG, "Interworking: Old scan results "
  53. "are fresh - connect without new scan");
  54. if (wpas_select_network_from_last_scan(wpa_s) == 0)
  55. return;
  56. }
  57. }
  58. wpa_supplicant_req_scan(wpa_s, 0, 0);
  59. }
  60. static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
  61. struct wpabuf *extra)
  62. {
  63. struct wpabuf *buf;
  64. size_t i;
  65. u8 *len_pos;
  66. buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
  67. (extra ? wpabuf_len(extra) : 0));
  68. if (buf == NULL)
  69. return NULL;
  70. len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
  71. for (i = 0; i < num_ids; i++)
  72. wpabuf_put_le16(buf, info_ids[i]);
  73. gas_anqp_set_element_len(buf, len_pos);
  74. if (extra)
  75. wpabuf_put_buf(buf, extra);
  76. gas_anqp_set_len(buf);
  77. return buf;
  78. }
  79. static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
  80. u8 dialog_token,
  81. enum gas_query_result result,
  82. const struct wpabuf *adv_proto,
  83. const struct wpabuf *resp,
  84. u16 status_code)
  85. {
  86. struct wpa_supplicant *wpa_s = ctx;
  87. anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
  88. status_code);
  89. interworking_next_anqp_fetch(wpa_s);
  90. }
  91. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  92. struct wpa_bss *bss)
  93. {
  94. struct wpabuf *buf;
  95. int ret = 0;
  96. int res;
  97. u16 info_ids[] = {
  98. ANQP_CAPABILITY_LIST,
  99. ANQP_VENUE_NAME,
  100. ANQP_NETWORK_AUTH_TYPE,
  101. ANQP_ROAMING_CONSORTIUM,
  102. ANQP_IP_ADDR_TYPE_AVAILABILITY,
  103. ANQP_NAI_REALM,
  104. ANQP_3GPP_CELLULAR_NETWORK,
  105. ANQP_DOMAIN_NAME
  106. };
  107. struct wpabuf *extra = NULL;
  108. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  109. MAC2STR(bss->bssid));
  110. #ifdef CONFIG_HS20
  111. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  112. u8 *len_pos;
  113. extra = wpabuf_alloc(100);
  114. if (!extra)
  115. return -1;
  116. len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
  117. wpabuf_put_be24(extra, OUI_WFA);
  118. wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
  119. wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
  120. wpabuf_put_u8(extra, 0); /* Reserved */
  121. wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
  122. wpabuf_put_u8(extra, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  123. wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
  124. wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
  125. wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
  126. gas_anqp_set_element_len(extra, len_pos);
  127. }
  128. #endif /* CONFIG_HS20 */
  129. buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
  130. extra);
  131. wpabuf_free(extra);
  132. if (buf == NULL)
  133. return -1;
  134. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  135. interworking_anqp_resp_cb, wpa_s);
  136. if (res < 0) {
  137. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  138. ret = -1;
  139. } else
  140. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  141. "%u", res);
  142. wpabuf_free(buf);
  143. return ret;
  144. }
  145. struct nai_realm_eap {
  146. u8 method;
  147. u8 inner_method;
  148. enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
  149. u8 cred_type;
  150. u8 tunneled_cred_type;
  151. };
  152. struct nai_realm {
  153. u8 encoding;
  154. char *realm;
  155. u8 eap_count;
  156. struct nai_realm_eap *eap;
  157. };
  158. static void nai_realm_free(struct nai_realm *realms, u16 count)
  159. {
  160. u16 i;
  161. if (realms == NULL)
  162. return;
  163. for (i = 0; i < count; i++) {
  164. os_free(realms[i].eap);
  165. os_free(realms[i].realm);
  166. }
  167. os_free(realms);
  168. }
  169. static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
  170. const u8 *end)
  171. {
  172. u8 elen, auth_count, a;
  173. const u8 *e_end;
  174. if (pos + 3 > end) {
  175. wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
  176. return NULL;
  177. }
  178. elen = *pos++;
  179. if (pos + elen > end || elen < 2) {
  180. wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
  181. return NULL;
  182. }
  183. e_end = pos + elen;
  184. e->method = *pos++;
  185. auth_count = *pos++;
  186. wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
  187. elen, e->method, auth_count);
  188. for (a = 0; a < auth_count; a++) {
  189. u8 id, len;
  190. if (pos + 2 > end || pos + 2 + pos[1] > end) {
  191. wpa_printf(MSG_DEBUG, "No room for Authentication "
  192. "Parameter subfield");
  193. return NULL;
  194. }
  195. id = *pos++;
  196. len = *pos++;
  197. switch (id) {
  198. case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
  199. if (len < 1)
  200. break;
  201. e->inner_non_eap = *pos;
  202. if (e->method != EAP_TYPE_TTLS)
  203. break;
  204. switch (*pos) {
  205. case NAI_REALM_INNER_NON_EAP_PAP:
  206. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
  207. break;
  208. case NAI_REALM_INNER_NON_EAP_CHAP:
  209. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
  210. break;
  211. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  212. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
  213. break;
  214. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  215. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
  216. break;
  217. }
  218. break;
  219. case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
  220. if (len < 1)
  221. break;
  222. e->inner_method = *pos;
  223. wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
  224. e->inner_method);
  225. break;
  226. case NAI_REALM_EAP_AUTH_CRED_TYPE:
  227. if (len < 1)
  228. break;
  229. e->cred_type = *pos;
  230. wpa_printf(MSG_DEBUG, "Credential Type: %u",
  231. e->cred_type);
  232. break;
  233. case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
  234. if (len < 1)
  235. break;
  236. e->tunneled_cred_type = *pos;
  237. wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
  238. "Type: %u", e->tunneled_cred_type);
  239. break;
  240. default:
  241. wpa_printf(MSG_DEBUG, "Unsupported Authentication "
  242. "Parameter: id=%u len=%u", id, len);
  243. wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
  244. "Value", pos, len);
  245. break;
  246. }
  247. pos += len;
  248. }
  249. return e_end;
  250. }
  251. static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
  252. const u8 *end)
  253. {
  254. u16 len;
  255. const u8 *f_end;
  256. u8 realm_len, e;
  257. if (end - pos < 4) {
  258. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  259. "fixed fields");
  260. return NULL;
  261. }
  262. len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
  263. pos += 2;
  264. if (pos + len > end || len < 3) {
  265. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  266. "(len=%u; left=%u)",
  267. len, (unsigned int) (end - pos));
  268. return NULL;
  269. }
  270. f_end = pos + len;
  271. r->encoding = *pos++;
  272. realm_len = *pos++;
  273. if (pos + realm_len > f_end) {
  274. wpa_printf(MSG_DEBUG, "No room for NAI Realm "
  275. "(len=%u; left=%u)",
  276. realm_len, (unsigned int) (f_end - pos));
  277. return NULL;
  278. }
  279. wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
  280. r->realm = os_malloc(realm_len + 1);
  281. if (r->realm == NULL)
  282. return NULL;
  283. os_memcpy(r->realm, pos, realm_len);
  284. r->realm[realm_len] = '\0';
  285. pos += realm_len;
  286. if (pos + 1 > f_end) {
  287. wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
  288. return NULL;
  289. }
  290. r->eap_count = *pos++;
  291. wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
  292. if (pos + r->eap_count * 3 > f_end) {
  293. wpa_printf(MSG_DEBUG, "No room for EAP Methods");
  294. return NULL;
  295. }
  296. r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
  297. if (r->eap == NULL)
  298. return NULL;
  299. for (e = 0; e < r->eap_count; e++) {
  300. pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
  301. if (pos == NULL)
  302. return NULL;
  303. }
  304. return f_end;
  305. }
  306. static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
  307. {
  308. struct nai_realm *realm;
  309. const u8 *pos, *end;
  310. u16 i, num;
  311. if (anqp == NULL || wpabuf_len(anqp) < 2)
  312. return NULL;
  313. pos = wpabuf_head_u8(anqp);
  314. end = pos + wpabuf_len(anqp);
  315. num = WPA_GET_LE16(pos);
  316. wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
  317. pos += 2;
  318. if (num * 5 > end - pos) {
  319. wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
  320. "enough data (%u octets) for that many realms",
  321. num, (unsigned int) (end - pos));
  322. return NULL;
  323. }
  324. realm = os_calloc(num, sizeof(struct nai_realm));
  325. if (realm == NULL)
  326. return NULL;
  327. for (i = 0; i < num; i++) {
  328. pos = nai_realm_parse_realm(&realm[i], pos, end);
  329. if (pos == NULL) {
  330. nai_realm_free(realm, num);
  331. return NULL;
  332. }
  333. }
  334. *count = num;
  335. return realm;
  336. }
  337. static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
  338. {
  339. char *tmp, *pos, *end;
  340. int match = 0;
  341. if (realm->realm == NULL || home_realm == NULL)
  342. return 0;
  343. if (os_strchr(realm->realm, ';') == NULL)
  344. return os_strcasecmp(realm->realm, home_realm) == 0;
  345. tmp = os_strdup(realm->realm);
  346. if (tmp == NULL)
  347. return 0;
  348. pos = tmp;
  349. while (*pos) {
  350. end = os_strchr(pos, ';');
  351. if (end)
  352. *end = '\0';
  353. if (os_strcasecmp(pos, home_realm) == 0) {
  354. match = 1;
  355. break;
  356. }
  357. if (end == NULL)
  358. break;
  359. pos = end + 1;
  360. }
  361. os_free(tmp);
  362. return match;
  363. }
  364. static int nai_realm_cred_username(struct nai_realm_eap *eap)
  365. {
  366. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  367. return 0; /* method not supported */
  368. if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
  369. /* Only tunneled methods with username/password supported */
  370. return 0;
  371. }
  372. if (eap->method == EAP_TYPE_PEAP &&
  373. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  374. return 0;
  375. if (eap->method == EAP_TYPE_TTLS) {
  376. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  377. return 0;
  378. if (eap->inner_method &&
  379. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  380. return 0;
  381. if (eap->inner_non_eap &&
  382. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  383. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  384. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  385. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  386. return 0;
  387. }
  388. if (eap->inner_method &&
  389. eap->inner_method != EAP_TYPE_GTC &&
  390. eap->inner_method != EAP_TYPE_MSCHAPV2)
  391. return 0;
  392. return 1;
  393. }
  394. static int nai_realm_cred_cert(struct nai_realm_eap *eap)
  395. {
  396. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  397. return 0; /* method not supported */
  398. if (eap->method != EAP_TYPE_TLS) {
  399. /* Only EAP-TLS supported for credential authentication */
  400. return 0;
  401. }
  402. return 1;
  403. }
  404. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  405. struct nai_realm *realm)
  406. {
  407. u8 e;
  408. if (cred == NULL ||
  409. cred->username == NULL ||
  410. cred->username[0] == '\0' ||
  411. ((cred->password == NULL ||
  412. cred->password[0] == '\0') &&
  413. (cred->private_key == NULL ||
  414. cred->private_key[0] == '\0')))
  415. return NULL;
  416. for (e = 0; e < realm->eap_count; e++) {
  417. struct nai_realm_eap *eap = &realm->eap[e];
  418. if (cred->password && cred->password[0] &&
  419. nai_realm_cred_username(eap))
  420. return eap;
  421. if (cred->private_key && cred->private_key[0] &&
  422. nai_realm_cred_cert(eap))
  423. return eap;
  424. }
  425. return NULL;
  426. }
  427. #ifdef INTERWORKING_3GPP
  428. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  429. {
  430. u8 plmn[3];
  431. const u8 *pos, *end;
  432. u8 udhl;
  433. /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
  434. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  435. plmn[1] = imsi[2] - '0';
  436. /* default to MNC length 3 if unknown */
  437. if (mnc_len != 2)
  438. plmn[1] |= (imsi[5] - '0') << 4;
  439. else
  440. plmn[1] |= 0xf0;
  441. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  442. if (anqp == NULL)
  443. return 0;
  444. pos = wpabuf_head_u8(anqp);
  445. end = pos + wpabuf_len(anqp);
  446. if (pos + 2 > end)
  447. return 0;
  448. if (*pos != 0) {
  449. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  450. return 0;
  451. }
  452. pos++;
  453. udhl = *pos++;
  454. if (pos + udhl > end) {
  455. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  456. return 0;
  457. }
  458. end = pos + udhl;
  459. while (pos + 2 <= end) {
  460. u8 iei, len;
  461. const u8 *l_end;
  462. iei = *pos++;
  463. len = *pos++ & 0x7f;
  464. if (pos + len > end)
  465. break;
  466. l_end = pos + len;
  467. if (iei == 0 && len > 0) {
  468. /* PLMN List */
  469. u8 num, i;
  470. num = *pos++;
  471. for (i = 0; i < num; i++) {
  472. if (pos + 3 > end)
  473. break;
  474. if (os_memcmp(pos, plmn, 3) == 0)
  475. return 1; /* Found matching PLMN */
  476. pos += 3;
  477. }
  478. }
  479. pos = l_end;
  480. }
  481. return 0;
  482. }
  483. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  484. size_t mnc_len, char prefix)
  485. {
  486. const char *sep, *msin;
  487. char *end, *pos;
  488. size_t msin_len, plmn_len;
  489. /*
  490. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  491. * Root NAI:
  492. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  493. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  494. */
  495. if (imsi == NULL || os_strlen(imsi) > 16) {
  496. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  497. return -1;
  498. }
  499. sep = os_strchr(imsi, '-');
  500. if (sep) {
  501. plmn_len = sep - imsi;
  502. msin = sep + 1;
  503. } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
  504. plmn_len = 3 + mnc_len;
  505. msin = imsi + plmn_len;
  506. } else
  507. return -1;
  508. if (plmn_len != 5 && plmn_len != 6)
  509. return -1;
  510. msin_len = os_strlen(msin);
  511. pos = nai;
  512. end = nai + nai_len;
  513. if (prefix)
  514. *pos++ = prefix;
  515. os_memcpy(pos, imsi, plmn_len);
  516. pos += plmn_len;
  517. os_memcpy(pos, msin, msin_len);
  518. pos += msin_len;
  519. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  520. if (plmn_len == 5) {
  521. *pos++ = '0';
  522. *pos++ = imsi[3];
  523. *pos++ = imsi[4];
  524. } else {
  525. *pos++ = imsi[3];
  526. *pos++ = imsi[4];
  527. *pos++ = imsi[5];
  528. }
  529. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  530. imsi[0], imsi[1], imsi[2]);
  531. return 0;
  532. }
  533. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  534. {
  535. char nai[100];
  536. if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
  537. return -1;
  538. return wpa_config_set_quoted(ssid, "identity", nai);
  539. }
  540. #endif /* INTERWORKING_3GPP */
  541. static int interworking_set_hs20_params(struct wpa_ssid *ssid)
  542. {
  543. if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
  544. return -1;
  545. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  546. return -1;
  547. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  548. return -1;
  549. return 0;
  550. }
  551. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  552. struct wpa_bss *bss)
  553. {
  554. #ifdef INTERWORKING_3GPP
  555. struct wpa_cred *cred;
  556. struct wpa_ssid *ssid;
  557. const u8 *ie;
  558. if (bss->anqp_3gpp == NULL)
  559. return -1;
  560. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  561. char *sep;
  562. const char *imsi;
  563. int mnc_len;
  564. #ifdef PCSC_FUNCS
  565. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  566. wpa_s->imsi[0]) {
  567. imsi = wpa_s->imsi;
  568. mnc_len = wpa_s->mnc_len;
  569. goto compare;
  570. }
  571. #endif /* PCSC_FUNCS */
  572. if (cred->imsi == NULL || !cred->imsi[0] ||
  573. cred->milenage == NULL || !cred->milenage[0])
  574. continue;
  575. sep = os_strchr(cred->imsi, '-');
  576. if (sep == NULL ||
  577. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  578. continue;
  579. mnc_len = sep - cred->imsi - 3;
  580. imsi = cred->imsi;
  581. #ifdef PCSC_FUNCS
  582. compare:
  583. #endif /* PCSC_FUNCS */
  584. if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
  585. break;
  586. }
  587. if (cred == NULL)
  588. return -1;
  589. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  590. if (ie == NULL)
  591. return -1;
  592. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  593. MAC2STR(bss->bssid));
  594. ssid = wpa_config_add_network(wpa_s->conf);
  595. if (ssid == NULL)
  596. return -1;
  597. wpas_notify_network_added(wpa_s, ssid);
  598. wpa_config_set_network_defaults(ssid);
  599. ssid->priority = cred->priority;
  600. ssid->temporary = 1;
  601. ssid->ssid = os_zalloc(ie[1] + 1);
  602. if (ssid->ssid == NULL)
  603. goto fail;
  604. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  605. ssid->ssid_len = ie[1];
  606. if (interworking_set_hs20_params(ssid) < 0)
  607. goto fail;
  608. /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
  609. if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
  610. wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
  611. goto fail;
  612. }
  613. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  614. wpa_config_set(ssid, "eap", "AKA", 0);
  615. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
  616. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  617. goto fail;
  618. }
  619. if (cred->milenage && cred->milenage[0]) {
  620. if (wpa_config_set_quoted(ssid, "password",
  621. cred->milenage) < 0)
  622. goto fail;
  623. } else if (cred->pcsc) {
  624. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  625. goto fail;
  626. if (wpa_s->conf->pcsc_pin &&
  627. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  628. < 0)
  629. goto fail;
  630. }
  631. if (cred->password && cred->password[0] &&
  632. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  633. goto fail;
  634. wpa_config_update_prio_list(wpa_s->conf);
  635. interworking_reconnect(wpa_s);
  636. return 0;
  637. fail:
  638. wpas_notify_network_removed(wpa_s, ssid);
  639. wpa_config_remove_network(wpa_s->conf, ssid->id);
  640. #endif /* INTERWORKING_3GPP */
  641. return -1;
  642. }
  643. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  644. size_t rc_len)
  645. {
  646. const u8 *pos, *end;
  647. u8 lens;
  648. if (ie == NULL)
  649. return 0;
  650. pos = ie + 2;
  651. end = ie + 2 + ie[1];
  652. /* Roaming Consortium element:
  653. * Number of ANQP OIs
  654. * OI #1 and #2 lengths
  655. * OI #1, [OI #2], [OI #3]
  656. */
  657. if (pos + 2 > end)
  658. return 0;
  659. pos++; /* skip Number of ANQP OIs */
  660. lens = *pos++;
  661. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  662. return 0;
  663. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  664. return 1;
  665. pos += lens & 0x0f;
  666. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  667. return 1;
  668. pos += lens >> 4;
  669. if (pos < end && (size_t) (end - pos) == rc_len &&
  670. os_memcmp(pos, rc_id, rc_len) == 0)
  671. return 1;
  672. return 0;
  673. }
  674. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  675. const u8 *rc_id, size_t rc_len)
  676. {
  677. const u8 *pos, *end;
  678. u8 len;
  679. if (anqp == NULL)
  680. return 0;
  681. pos = wpabuf_head(anqp);
  682. end = pos + wpabuf_len(anqp);
  683. /* Set of <OI Length, OI> duples */
  684. while (pos < end) {
  685. len = *pos++;
  686. if (pos + len > end)
  687. break;
  688. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  689. return 1;
  690. pos += len;
  691. }
  692. return 0;
  693. }
  694. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  695. const u8 *rc_id, size_t rc_len)
  696. {
  697. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  698. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  699. }
  700. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  701. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  702. {
  703. struct wpa_cred *cred, *selected = NULL;
  704. const u8 *ie;
  705. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  706. if (ie == NULL && bss->anqp_roaming_consortium == NULL)
  707. return NULL;
  708. if (wpa_s->conf->cred == NULL)
  709. return NULL;
  710. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  711. if (cred->roaming_consortium_len == 0)
  712. continue;
  713. if (!roaming_consortium_match(ie, bss->anqp_roaming_consortium,
  714. cred->roaming_consortium,
  715. cred->roaming_consortium_len))
  716. continue;
  717. if (selected == NULL ||
  718. selected->priority < cred->priority)
  719. selected = cred;
  720. }
  721. return selected;
  722. }
  723. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  724. struct wpa_cred *cred, int ttls)
  725. {
  726. if (cred->eap_method) {
  727. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  728. cred->eap_method->method == EAP_TYPE_TTLS;
  729. os_free(ssid->eap.eap_methods);
  730. ssid->eap.eap_methods =
  731. os_malloc(sizeof(struct eap_method_type) * 2);
  732. if (ssid->eap.eap_methods == NULL)
  733. return -1;
  734. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  735. sizeof(*cred->eap_method));
  736. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  737. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  738. }
  739. if (ttls && cred->username && cred->username[0]) {
  740. const char *pos;
  741. char *anon;
  742. /* Use anonymous NAI in Phase 1 */
  743. pos = os_strchr(cred->username, '@');
  744. if (pos) {
  745. size_t buflen = 9 + os_strlen(pos) + 1;
  746. anon = os_malloc(buflen);
  747. if (anon == NULL)
  748. return -1;
  749. os_snprintf(anon, buflen, "anonymous%s", pos);
  750. } else if (cred->realm) {
  751. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  752. anon = os_malloc(buflen);
  753. if (anon == NULL)
  754. return -1;
  755. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  756. } else {
  757. anon = os_strdup("anonymous");
  758. if (anon == NULL)
  759. return -1;
  760. }
  761. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  762. 0) {
  763. os_free(anon);
  764. return -1;
  765. }
  766. os_free(anon);
  767. }
  768. if (cred->username && cred->username[0] &&
  769. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  770. return -1;
  771. if (cred->password && cred->password[0]) {
  772. if (cred->ext_password &&
  773. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  774. return -1;
  775. if (!cred->ext_password &&
  776. wpa_config_set_quoted(ssid, "password", cred->password) <
  777. 0)
  778. return -1;
  779. }
  780. if (cred->client_cert && cred->client_cert[0] &&
  781. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  782. return -1;
  783. if (cred->private_key && cred->private_key[0] &&
  784. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  785. return -1;
  786. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  787. wpa_config_set_quoted(ssid, "private_key_passwd",
  788. cred->private_key_passwd) < 0)
  789. return -1;
  790. if (cred->phase1) {
  791. os_free(ssid->eap.phase1);
  792. ssid->eap.phase1 = os_strdup(cred->phase1);
  793. }
  794. if (cred->phase2) {
  795. os_free(ssid->eap.phase2);
  796. ssid->eap.phase2 = os_strdup(cred->phase2);
  797. }
  798. if (cred->ca_cert && cred->ca_cert[0] &&
  799. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  800. return -1;
  801. return 0;
  802. }
  803. static int interworking_connect_roaming_consortium(
  804. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  805. struct wpa_bss *bss, const u8 *ssid_ie)
  806. {
  807. struct wpa_ssid *ssid;
  808. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  809. "roaming consortium match", MAC2STR(bss->bssid));
  810. ssid = wpa_config_add_network(wpa_s->conf);
  811. if (ssid == NULL)
  812. return -1;
  813. wpas_notify_network_added(wpa_s, ssid);
  814. wpa_config_set_network_defaults(ssid);
  815. ssid->priority = cred->priority;
  816. ssid->temporary = 1;
  817. ssid->ssid = os_zalloc(ssid_ie[1] + 1);
  818. if (ssid->ssid == NULL)
  819. goto fail;
  820. os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
  821. ssid->ssid_len = ssid_ie[1];
  822. if (interworking_set_hs20_params(ssid) < 0)
  823. goto fail;
  824. if (cred->eap_method == NULL) {
  825. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  826. "credential using roaming consortium");
  827. goto fail;
  828. }
  829. if (interworking_set_eap_params(
  830. ssid, cred,
  831. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  832. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  833. goto fail;
  834. wpa_config_update_prio_list(wpa_s->conf);
  835. interworking_reconnect(wpa_s);
  836. return 0;
  837. fail:
  838. wpas_notify_network_removed(wpa_s, ssid);
  839. wpa_config_remove_network(wpa_s->conf, ssid->id);
  840. return -1;
  841. }
  842. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  843. {
  844. struct wpa_cred *cred;
  845. struct wpa_ssid *ssid;
  846. struct nai_realm *realm;
  847. struct nai_realm_eap *eap = NULL;
  848. u16 count, i;
  849. char buf[100];
  850. const u8 *ie;
  851. if (wpa_s->conf->cred == NULL || bss == NULL)
  852. return -1;
  853. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  854. if (ie == NULL || ie[1] == 0) {
  855. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  856. MACSTR, MAC2STR(bss->bssid));
  857. return -1;
  858. }
  859. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  860. /*
  861. * We currently support only HS 2.0 networks and those are
  862. * required to use WPA2-Enterprise.
  863. */
  864. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  865. "RSN");
  866. return -1;
  867. }
  868. cred = interworking_credentials_available_roaming_consortium(wpa_s,
  869. bss);
  870. if (cred)
  871. return interworking_connect_roaming_consortium(wpa_s, cred,
  872. bss, ie);
  873. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  874. if (realm == NULL) {
  875. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  876. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  877. count = 0;
  878. }
  879. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  880. for (i = 0; i < count; i++) {
  881. if (!nai_realm_match(&realm[i], cred->realm))
  882. continue;
  883. eap = nai_realm_find_eap(cred, &realm[i]);
  884. if (eap)
  885. break;
  886. }
  887. if (eap)
  888. break;
  889. }
  890. if (!eap) {
  891. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  892. if (realm)
  893. nai_realm_free(realm, count);
  894. return 0;
  895. }
  896. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  897. "and EAP method found for " MACSTR,
  898. MAC2STR(bss->bssid));
  899. nai_realm_free(realm, count);
  900. return -1;
  901. }
  902. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  903. MAC2STR(bss->bssid));
  904. ssid = wpa_config_add_network(wpa_s->conf);
  905. if (ssid == NULL) {
  906. nai_realm_free(realm, count);
  907. return -1;
  908. }
  909. wpas_notify_network_added(wpa_s, ssid);
  910. wpa_config_set_network_defaults(ssid);
  911. ssid->priority = cred->priority;
  912. ssid->temporary = 1;
  913. ssid->ssid = os_zalloc(ie[1] + 1);
  914. if (ssid->ssid == NULL)
  915. goto fail;
  916. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  917. ssid->ssid_len = ie[1];
  918. if (interworking_set_hs20_params(ssid) < 0)
  919. goto fail;
  920. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  921. eap->method), 0) < 0)
  922. goto fail;
  923. switch (eap->method) {
  924. case EAP_TYPE_TTLS:
  925. if (eap->inner_method) {
  926. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  927. eap_get_name(EAP_VENDOR_IETF,
  928. eap->inner_method));
  929. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  930. goto fail;
  931. break;
  932. }
  933. switch (eap->inner_non_eap) {
  934. case NAI_REALM_INNER_NON_EAP_PAP:
  935. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  936. 0)
  937. goto fail;
  938. break;
  939. case NAI_REALM_INNER_NON_EAP_CHAP:
  940. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  941. < 0)
  942. goto fail;
  943. break;
  944. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  945. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  946. 0) < 0)
  947. goto fail;
  948. break;
  949. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  950. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  951. 0) < 0)
  952. goto fail;
  953. break;
  954. }
  955. break;
  956. case EAP_TYPE_PEAP:
  957. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  958. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  959. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  960. goto fail;
  961. break;
  962. case EAP_TYPE_TLS:
  963. break;
  964. }
  965. if (interworking_set_eap_params(ssid, cred,
  966. eap->method == EAP_TYPE_TTLS) < 0)
  967. goto fail;
  968. nai_realm_free(realm, count);
  969. wpa_config_update_prio_list(wpa_s->conf);
  970. interworking_reconnect(wpa_s);
  971. return 0;
  972. fail:
  973. wpas_notify_network_removed(wpa_s, ssid);
  974. wpa_config_remove_network(wpa_s->conf, ssid->id);
  975. nai_realm_free(realm, count);
  976. return -1;
  977. }
  978. static struct wpa_cred * interworking_credentials_available_3gpp(
  979. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  980. {
  981. struct wpa_cred *cred, *selected = NULL;
  982. int ret;
  983. #ifdef INTERWORKING_3GPP
  984. if (bss->anqp_3gpp == NULL)
  985. return NULL;
  986. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  987. char *sep;
  988. const char *imsi;
  989. int mnc_len;
  990. #ifdef PCSC_FUNCS
  991. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  992. wpa_s->imsi[0]) {
  993. imsi = wpa_s->imsi;
  994. mnc_len = wpa_s->mnc_len;
  995. goto compare;
  996. }
  997. #endif /* PCSC_FUNCS */
  998. if (cred->imsi == NULL || !cred->imsi[0] ||
  999. cred->milenage == NULL || !cred->milenage[0])
  1000. continue;
  1001. sep = os_strchr(cred->imsi, '-');
  1002. if (sep == NULL ||
  1003. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1004. continue;
  1005. mnc_len = sep - cred->imsi - 3;
  1006. imsi = cred->imsi;
  1007. #ifdef PCSC_FUNCS
  1008. compare:
  1009. #endif /* PCSC_FUNCS */
  1010. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1011. MACSTR, MAC2STR(bss->bssid));
  1012. ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
  1013. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1014. if (ret) {
  1015. if (selected == NULL ||
  1016. selected->priority < cred->priority)
  1017. selected = cred;
  1018. }
  1019. }
  1020. #endif /* INTERWORKING_3GPP */
  1021. return selected;
  1022. }
  1023. static struct wpa_cred * interworking_credentials_available_realm(
  1024. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1025. {
  1026. struct wpa_cred *cred, *selected = NULL;
  1027. struct nai_realm *realm;
  1028. u16 count, i;
  1029. if (bss->anqp_nai_realm == NULL)
  1030. return NULL;
  1031. if (wpa_s->conf->cred == NULL)
  1032. return NULL;
  1033. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1034. MACSTR, MAC2STR(bss->bssid));
  1035. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  1036. if (realm == NULL) {
  1037. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1038. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1039. return NULL;
  1040. }
  1041. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1042. if (cred->realm == NULL)
  1043. continue;
  1044. for (i = 0; i < count; i++) {
  1045. if (!nai_realm_match(&realm[i], cred->realm))
  1046. continue;
  1047. if (nai_realm_find_eap(cred, &realm[i])) {
  1048. if (selected == NULL ||
  1049. selected->priority < cred->priority)
  1050. selected = cred;
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. nai_realm_free(realm, count);
  1056. return selected;
  1057. }
  1058. static struct wpa_cred * interworking_credentials_available(
  1059. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1060. {
  1061. struct wpa_cred *cred, *cred2;
  1062. cred = interworking_credentials_available_realm(wpa_s, bss);
  1063. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1064. if (cred && cred2 && cred2->priority >= cred->priority)
  1065. cred = cred2;
  1066. if (!cred)
  1067. cred = cred2;
  1068. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1069. bss);
  1070. if (cred && cred2 && cred2->priority >= cred->priority)
  1071. cred = cred2;
  1072. if (!cred)
  1073. cred = cred2;
  1074. return cred;
  1075. }
  1076. static int domain_name_list_contains(struct wpabuf *domain_names,
  1077. const char *domain)
  1078. {
  1079. const u8 *pos, *end;
  1080. size_t len;
  1081. len = os_strlen(domain);
  1082. pos = wpabuf_head(domain_names);
  1083. end = pos + wpabuf_len(domain_names);
  1084. while (pos + 1 < end) {
  1085. if (pos + 1 + pos[0] > end)
  1086. break;
  1087. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1088. pos + 1, pos[0]);
  1089. if (pos[0] == len &&
  1090. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1091. return 1;
  1092. pos += 1 + pos[0];
  1093. }
  1094. return 0;
  1095. }
  1096. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1097. struct wpabuf *domain_names)
  1098. {
  1099. struct wpa_cred *cred;
  1100. #ifdef INTERWORKING_3GPP
  1101. char nai[100], *realm;
  1102. #endif /* INTERWORKING_3GPP */
  1103. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1104. return -1;
  1105. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1106. #ifdef INTERWORKING_3GPP
  1107. char *imsi = NULL;
  1108. int mnc_len = 0;
  1109. if (cred->imsi)
  1110. imsi = cred->imsi;
  1111. #ifdef CONFIG_PCSC
  1112. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1113. wpa_s->scard && wpa_s->imsi[0]) {
  1114. imsi = wpa_s->imsi;
  1115. mnc_len = wpa_s->mnc_len;
  1116. }
  1117. #endif /* CONFIG_PCSC */
  1118. if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0)
  1119. == 0) {
  1120. realm = os_strchr(nai, '@');
  1121. if (realm)
  1122. realm++;
  1123. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1124. "with SIM/USIM domain %s", realm);
  1125. if (realm &&
  1126. domain_name_list_contains(domain_names, realm))
  1127. return 1;
  1128. }
  1129. #endif /* INTERWORKING_3GPP */
  1130. if (cred->domain == NULL)
  1131. continue;
  1132. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1133. "home SP FQDN %s", cred->domain);
  1134. if (domain_name_list_contains(domain_names, cred->domain))
  1135. return 1;
  1136. }
  1137. return 0;
  1138. }
  1139. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1140. {
  1141. struct wpa_bss *bss;
  1142. struct wpa_ssid *ssid;
  1143. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1144. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1145. if (wpas_network_disabled(wpa_s, ssid) ||
  1146. ssid->mode != WPAS_MODE_INFRA)
  1147. continue;
  1148. if (ssid->ssid_len != bss->ssid_len ||
  1149. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1150. 0)
  1151. continue;
  1152. /*
  1153. * TODO: Consider more accurate matching of security
  1154. * configuration similarly to what is done in events.c
  1155. */
  1156. return 1;
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1162. {
  1163. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1164. int selected_prio = -999999, selected_home_prio = -999999;
  1165. unsigned int count = 0;
  1166. const char *type;
  1167. int res;
  1168. struct wpa_cred *cred;
  1169. wpa_s->network_select = 0;
  1170. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1171. cred = interworking_credentials_available(wpa_s, bss);
  1172. if (!cred)
  1173. continue;
  1174. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1175. /*
  1176. * We currently support only HS 2.0 networks and those
  1177. * are required to use WPA2-Enterprise.
  1178. */
  1179. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1180. "with " MACSTR " but network does not use "
  1181. "RSN", MAC2STR(bss->bssid));
  1182. continue;
  1183. }
  1184. count++;
  1185. res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
  1186. if (res > 0)
  1187. type = "home";
  1188. else if (res == 0)
  1189. type = "roaming";
  1190. else
  1191. type = "unknown";
  1192. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1193. MAC2STR(bss->bssid), type);
  1194. if (wpa_s->auto_select ||
  1195. (wpa_s->conf->auto_interworking &&
  1196. wpa_s->auto_network_select)) {
  1197. if (selected == NULL ||
  1198. cred->priority > selected_prio) {
  1199. selected = bss;
  1200. selected_prio = cred->priority;
  1201. }
  1202. if (res > 0 &&
  1203. (selected_home == NULL ||
  1204. cred->priority > selected_home_prio)) {
  1205. selected_home = bss;
  1206. selected_home_prio = cred->priority;
  1207. }
  1208. }
  1209. }
  1210. if (selected_home && selected_home != selected &&
  1211. selected_home_prio >= selected_prio) {
  1212. /* Prefer network operated by the Home SP */
  1213. selected = selected_home;
  1214. }
  1215. if (count == 0) {
  1216. /*
  1217. * No matching network was found based on configured
  1218. * credentials. Check whether any of the enabled network blocks
  1219. * have matching APs.
  1220. */
  1221. if (interworking_find_network_match(wpa_s)) {
  1222. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1223. "match for enabled network configurations");
  1224. if (wpa_s->auto_select)
  1225. interworking_reconnect(wpa_s);
  1226. return;
  1227. }
  1228. if (wpa_s->auto_network_select) {
  1229. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1230. "scanning after ANQP fetch");
  1231. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  1232. 0);
  1233. return;
  1234. }
  1235. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1236. "with matching credentials found");
  1237. }
  1238. if (selected)
  1239. interworking_connect(wpa_s, selected);
  1240. }
  1241. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1242. {
  1243. struct wpa_bss *bss;
  1244. int found = 0;
  1245. const u8 *ie;
  1246. if (!wpa_s->fetch_anqp_in_progress)
  1247. return;
  1248. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1249. if (!(bss->caps & IEEE80211_CAP_ESS))
  1250. continue;
  1251. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1252. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1253. continue; /* AP does not support Interworking */
  1254. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1255. found++;
  1256. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1257. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1258. MACSTR, MAC2STR(bss->bssid));
  1259. interworking_anqp_send_req(wpa_s, bss);
  1260. break;
  1261. }
  1262. }
  1263. if (found == 0) {
  1264. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1265. wpa_s->fetch_anqp_in_progress = 0;
  1266. if (wpa_s->network_select)
  1267. interworking_select_network(wpa_s);
  1268. }
  1269. }
  1270. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1271. {
  1272. struct wpa_bss *bss;
  1273. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1274. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1275. wpa_s->fetch_anqp_in_progress = 1;
  1276. interworking_next_anqp_fetch(wpa_s);
  1277. }
  1278. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1279. {
  1280. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1281. return 0;
  1282. wpa_s->network_select = 0;
  1283. interworking_start_fetch_anqp(wpa_s);
  1284. return 0;
  1285. }
  1286. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1287. {
  1288. if (!wpa_s->fetch_anqp_in_progress)
  1289. return;
  1290. wpa_s->fetch_anqp_in_progress = 0;
  1291. }
  1292. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1293. u16 info_ids[], size_t num_ids)
  1294. {
  1295. struct wpabuf *buf;
  1296. int ret = 0;
  1297. int freq;
  1298. struct wpa_bss *bss;
  1299. int res;
  1300. freq = wpa_s->assoc_freq;
  1301. bss = wpa_bss_get_bssid(wpa_s, dst);
  1302. if (bss)
  1303. freq = bss->freq;
  1304. if (freq <= 0)
  1305. return -1;
  1306. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1307. MAC2STR(dst), (unsigned int) num_ids);
  1308. buf = anqp_build_req(info_ids, num_ids, NULL);
  1309. if (buf == NULL)
  1310. return -1;
  1311. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1312. if (res < 0) {
  1313. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1314. ret = -1;
  1315. } else
  1316. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1317. "%u", res);
  1318. wpabuf_free(buf);
  1319. return ret;
  1320. }
  1321. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1322. const u8 *sa, u16 info_id,
  1323. const u8 *data, size_t slen)
  1324. {
  1325. const u8 *pos = data;
  1326. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1327. #ifdef CONFIG_HS20
  1328. u8 type;
  1329. #endif /* CONFIG_HS20 */
  1330. switch (info_id) {
  1331. case ANQP_CAPABILITY_LIST:
  1332. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1333. " ANQP Capability list", MAC2STR(sa));
  1334. break;
  1335. case ANQP_VENUE_NAME:
  1336. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1337. " Venue Name", MAC2STR(sa));
  1338. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1339. if (bss) {
  1340. wpabuf_free(bss->anqp_venue_name);
  1341. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  1342. }
  1343. break;
  1344. case ANQP_NETWORK_AUTH_TYPE:
  1345. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1346. " Network Authentication Type information",
  1347. MAC2STR(sa));
  1348. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1349. "Type", pos, slen);
  1350. if (bss) {
  1351. wpabuf_free(bss->anqp_network_auth_type);
  1352. bss->anqp_network_auth_type =
  1353. wpabuf_alloc_copy(pos, slen);
  1354. }
  1355. break;
  1356. case ANQP_ROAMING_CONSORTIUM:
  1357. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1358. " Roaming Consortium list", MAC2STR(sa));
  1359. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1360. pos, slen);
  1361. if (bss) {
  1362. wpabuf_free(bss->anqp_roaming_consortium);
  1363. bss->anqp_roaming_consortium =
  1364. wpabuf_alloc_copy(pos, slen);
  1365. }
  1366. break;
  1367. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1368. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1369. " IP Address Type Availability information",
  1370. MAC2STR(sa));
  1371. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1372. pos, slen);
  1373. if (bss) {
  1374. wpabuf_free(bss->anqp_ip_addr_type_availability);
  1375. bss->anqp_ip_addr_type_availability =
  1376. wpabuf_alloc_copy(pos, slen);
  1377. }
  1378. break;
  1379. case ANQP_NAI_REALM:
  1380. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1381. " NAI Realm list", MAC2STR(sa));
  1382. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1383. if (bss) {
  1384. wpabuf_free(bss->anqp_nai_realm);
  1385. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  1386. }
  1387. break;
  1388. case ANQP_3GPP_CELLULAR_NETWORK:
  1389. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1390. " 3GPP Cellular Network information", MAC2STR(sa));
  1391. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1392. pos, slen);
  1393. if (bss) {
  1394. wpabuf_free(bss->anqp_3gpp);
  1395. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1396. }
  1397. break;
  1398. case ANQP_DOMAIN_NAME:
  1399. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1400. " Domain Name list", MAC2STR(sa));
  1401. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1402. if (bss) {
  1403. wpabuf_free(bss->anqp_domain_name);
  1404. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  1405. }
  1406. break;
  1407. case ANQP_VENDOR_SPECIFIC:
  1408. if (slen < 3)
  1409. return;
  1410. switch (WPA_GET_BE24(pos)) {
  1411. #ifdef CONFIG_HS20
  1412. case OUI_WFA:
  1413. pos += 3;
  1414. slen -= 3;
  1415. if (slen < 1)
  1416. return;
  1417. type = *pos++;
  1418. slen--;
  1419. switch (type) {
  1420. case HS20_ANQP_OUI_TYPE:
  1421. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1422. slen);
  1423. break;
  1424. default:
  1425. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1426. "vendor type %u", type);
  1427. break;
  1428. }
  1429. break;
  1430. #endif /* CONFIG_HS20 */
  1431. default:
  1432. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1433. "vendor-specific ANQP OUI %06x",
  1434. WPA_GET_BE24(pos));
  1435. return;
  1436. }
  1437. break;
  1438. default:
  1439. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1440. "%u", info_id);
  1441. break;
  1442. }
  1443. }
  1444. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1445. enum gas_query_result result,
  1446. const struct wpabuf *adv_proto,
  1447. const struct wpabuf *resp, u16 status_code)
  1448. {
  1449. struct wpa_supplicant *wpa_s = ctx;
  1450. const u8 *pos;
  1451. const u8 *end;
  1452. u16 info_id;
  1453. u16 slen;
  1454. if (result != GAS_QUERY_SUCCESS)
  1455. return;
  1456. pos = wpabuf_head(adv_proto);
  1457. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1458. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1459. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1460. "Protocol in response");
  1461. return;
  1462. }
  1463. pos = wpabuf_head(resp);
  1464. end = pos + wpabuf_len(resp);
  1465. while (pos < end) {
  1466. if (pos + 4 > end) {
  1467. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1468. break;
  1469. }
  1470. info_id = WPA_GET_LE16(pos);
  1471. pos += 2;
  1472. slen = WPA_GET_LE16(pos);
  1473. pos += 2;
  1474. if (pos + slen > end) {
  1475. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1476. "for Info ID %u", info_id);
  1477. break;
  1478. }
  1479. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1480. slen);
  1481. pos += slen;
  1482. }
  1483. }
  1484. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1485. struct wpa_scan_results *scan_res)
  1486. {
  1487. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1488. "ANQP fetch");
  1489. interworking_start_fetch_anqp(wpa_s);
  1490. }
  1491. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1492. {
  1493. interworking_stop_fetch_anqp(wpa_s);
  1494. wpa_s->network_select = 1;
  1495. wpa_s->auto_network_select = 0;
  1496. wpa_s->auto_select = !!auto_select;
  1497. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1498. "selection");
  1499. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1500. wpa_s->scan_req = 2;
  1501. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1502. return 0;
  1503. }
  1504. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1505. enum gas_query_result result,
  1506. const struct wpabuf *adv_proto,
  1507. const struct wpabuf *resp, u16 status_code)
  1508. {
  1509. struct wpa_supplicant *wpa_s = ctx;
  1510. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  1511. " dialog_token=%d status_code=%d resp_len=%d",
  1512. MAC2STR(addr), dialog_token, status_code,
  1513. resp ? (int) wpabuf_len(resp) : -1);
  1514. if (!resp)
  1515. return;
  1516. wpabuf_free(wpa_s->last_gas_resp);
  1517. wpa_s->last_gas_resp = wpabuf_dup(resp);
  1518. if (wpa_s->last_gas_resp == NULL)
  1519. return;
  1520. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  1521. wpa_s->last_gas_dialog_token = dialog_token;
  1522. }
  1523. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1524. const struct wpabuf *adv_proto,
  1525. const struct wpabuf *query)
  1526. {
  1527. struct wpabuf *buf;
  1528. int ret = 0;
  1529. int freq;
  1530. struct wpa_bss *bss;
  1531. int res;
  1532. size_t len;
  1533. u8 query_resp_len_limit = 0, pame_bi = 0;
  1534. freq = wpa_s->assoc_freq;
  1535. bss = wpa_bss_get_bssid(wpa_s, dst);
  1536. if (bss)
  1537. freq = bss->freq;
  1538. if (freq <= 0)
  1539. return -1;
  1540. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  1541. MAC2STR(dst), freq);
  1542. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  1543. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  1544. len = 3 + wpabuf_len(adv_proto) + 2;
  1545. if (query)
  1546. len += wpabuf_len(query);
  1547. buf = gas_build_initial_req(0, len);
  1548. if (buf == NULL)
  1549. return -1;
  1550. /* Advertisement Protocol IE */
  1551. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1552. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  1553. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  1554. (pame_bi ? 0x80 : 0));
  1555. wpabuf_put_buf(buf, adv_proto);
  1556. /* GAS Query */
  1557. if (query) {
  1558. wpabuf_put_le16(buf, wpabuf_len(query));
  1559. wpabuf_put_buf(buf, query);
  1560. } else
  1561. wpabuf_put_le16(buf, 0);
  1562. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  1563. if (res < 0) {
  1564. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  1565. ret = -1;
  1566. } else
  1567. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  1568. "%u", res);
  1569. wpabuf_free(buf);
  1570. return ret;
  1571. }