interworking.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  1. /*
  2. * Interworking (IEEE 802.11u)
  3. * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
  4. * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/gas.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "utils/pcsc_funcs.h"
  15. #include "utils/eloop.h"
  16. #include "drivers/driver.h"
  17. #include "eap_common/eap_defs.h"
  18. #include "eap_peer/eap.h"
  19. #include "eap_peer/eap_methods.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "config.h"
  24. #include "config_ssid.h"
  25. #include "bss.h"
  26. #include "scan.h"
  27. #include "notify.h"
  28. #include "gas_query.h"
  29. #include "hs20_supplicant.h"
  30. #include "interworking.h"
  31. #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
  32. #define INTERWORKING_3GPP
  33. #else
  34. #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
  35. #define INTERWORKING_3GPP
  36. #else
  37. #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
  38. #define INTERWORKING_3GPP
  39. #endif
  40. #endif
  41. #endif
  42. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  43. static struct wpa_cred * interworking_credentials_available_realm(
  44. struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
  45. static struct wpa_cred * interworking_credentials_available_3gpp(
  46. struct wpa_supplicant *wpa_s, struct wpa_bss *bss);
  47. static void interworking_reconnect(struct wpa_supplicant *wpa_s)
  48. {
  49. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  50. wpa_supplicant_cancel_sched_scan(wpa_s);
  51. wpa_supplicant_deauthenticate(wpa_s,
  52. WLAN_REASON_DEAUTH_LEAVING);
  53. }
  54. wpa_s->disconnected = 0;
  55. wpa_s->reassociate = 1;
  56. if (wpa_supplicant_fast_associate(wpa_s) >= 0)
  57. return;
  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 cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
  92. {
  93. struct wpa_cred *cred;
  94. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  95. if (cred->roaming_consortium_len)
  96. return 1;
  97. if (cred->required_roaming_consortium_len)
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
  103. {
  104. struct wpa_cred *cred;
  105. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  106. if (cred->pcsc || cred->imsi)
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
  112. {
  113. struct wpa_cred *cred;
  114. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  115. if (cred->pcsc || cred->imsi)
  116. continue;
  117. if (!cred->eap_method)
  118. return 1;
  119. if (cred->realm && cred->roaming_consortium_len == 0)
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static int cred_with_domain(struct wpa_supplicant *wpa_s)
  125. {
  126. struct wpa_cred *cred;
  127. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  128. if (cred->domain || cred->pcsc || cred->imsi ||
  129. cred->roaming_partner)
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. static int additional_roaming_consortiums(struct wpa_bss *bss)
  135. {
  136. const u8 *ie;
  137. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  138. if (ie == NULL || ie[1] == 0)
  139. return 0;
  140. return ie[2]; /* Number of ANQP OIs */
  141. }
  142. static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
  143. {
  144. struct wpa_supplicant *wpa_s = eloop_ctx;
  145. interworking_next_anqp_fetch(wpa_s);
  146. }
  147. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  148. struct wpa_bss *bss)
  149. {
  150. struct wpabuf *buf;
  151. int ret = 0;
  152. int res;
  153. u16 info_ids[8];
  154. size_t num_info_ids = 0;
  155. struct wpabuf *extra = NULL;
  156. int all = wpa_s->fetch_all_anqp;
  157. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  158. MAC2STR(bss->bssid));
  159. wpa_s->interworking_gas_bss = bss;
  160. info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
  161. if (all) {
  162. info_ids[num_info_ids++] = ANQP_VENUE_NAME;
  163. info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
  164. }
  165. if (all || (cred_with_roaming_consortium(wpa_s) &&
  166. additional_roaming_consortiums(bss)))
  167. info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
  168. if (all)
  169. info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
  170. if (all || cred_with_nai_realm(wpa_s))
  171. info_ids[num_info_ids++] = ANQP_NAI_REALM;
  172. if (all || cred_with_3gpp(wpa_s))
  173. info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
  174. if (all || cred_with_domain(wpa_s))
  175. info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
  176. wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
  177. (u8 *) info_ids, num_info_ids * 2);
  178. #ifdef CONFIG_HS20
  179. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  180. u8 *len_pos;
  181. extra = wpabuf_alloc(100);
  182. if (!extra)
  183. return -1;
  184. len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
  185. wpabuf_put_be24(extra, OUI_WFA);
  186. wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
  187. wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
  188. wpabuf_put_u8(extra, 0); /* Reserved */
  189. wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
  190. if (all) {
  191. wpabuf_put_u8(extra,
  192. HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  193. wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
  194. wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
  195. wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
  196. }
  197. gas_anqp_set_element_len(extra, len_pos);
  198. }
  199. #endif /* CONFIG_HS20 */
  200. buf = anqp_build_req(info_ids, num_info_ids, extra);
  201. wpabuf_free(extra);
  202. if (buf == NULL)
  203. return -1;
  204. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  205. interworking_anqp_resp_cb, wpa_s);
  206. if (res < 0) {
  207. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  208. wpabuf_free(buf);
  209. ret = -1;
  210. eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
  211. NULL);
  212. } else
  213. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  214. "%u", res);
  215. return ret;
  216. }
  217. struct nai_realm_eap {
  218. u8 method;
  219. u8 inner_method;
  220. enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
  221. u8 cred_type;
  222. u8 tunneled_cred_type;
  223. };
  224. struct nai_realm {
  225. u8 encoding;
  226. char *realm;
  227. u8 eap_count;
  228. struct nai_realm_eap *eap;
  229. };
  230. static void nai_realm_free(struct nai_realm *realms, u16 count)
  231. {
  232. u16 i;
  233. if (realms == NULL)
  234. return;
  235. for (i = 0; i < count; i++) {
  236. os_free(realms[i].eap);
  237. os_free(realms[i].realm);
  238. }
  239. os_free(realms);
  240. }
  241. static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
  242. const u8 *end)
  243. {
  244. u8 elen, auth_count, a;
  245. const u8 *e_end;
  246. if (pos + 3 > end) {
  247. wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
  248. return NULL;
  249. }
  250. elen = *pos++;
  251. if (pos + elen > end || elen < 2) {
  252. wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
  253. return NULL;
  254. }
  255. e_end = pos + elen;
  256. e->method = *pos++;
  257. auth_count = *pos++;
  258. wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
  259. elen, e->method, auth_count);
  260. for (a = 0; a < auth_count; a++) {
  261. u8 id, len;
  262. if (pos + 2 > end || pos + 2 + pos[1] > end) {
  263. wpa_printf(MSG_DEBUG, "No room for Authentication "
  264. "Parameter subfield");
  265. return NULL;
  266. }
  267. id = *pos++;
  268. len = *pos++;
  269. switch (id) {
  270. case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
  271. if (len < 1)
  272. break;
  273. e->inner_non_eap = *pos;
  274. if (e->method != EAP_TYPE_TTLS)
  275. break;
  276. switch (*pos) {
  277. case NAI_REALM_INNER_NON_EAP_PAP:
  278. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
  279. break;
  280. case NAI_REALM_INNER_NON_EAP_CHAP:
  281. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
  282. break;
  283. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  284. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
  285. break;
  286. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  287. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
  288. break;
  289. }
  290. break;
  291. case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
  292. if (len < 1)
  293. break;
  294. e->inner_method = *pos;
  295. wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
  296. e->inner_method);
  297. break;
  298. case NAI_REALM_EAP_AUTH_CRED_TYPE:
  299. if (len < 1)
  300. break;
  301. e->cred_type = *pos;
  302. wpa_printf(MSG_DEBUG, "Credential Type: %u",
  303. e->cred_type);
  304. break;
  305. case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
  306. if (len < 1)
  307. break;
  308. e->tunneled_cred_type = *pos;
  309. wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
  310. "Type: %u", e->tunneled_cred_type);
  311. break;
  312. default:
  313. wpa_printf(MSG_DEBUG, "Unsupported Authentication "
  314. "Parameter: id=%u len=%u", id, len);
  315. wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
  316. "Value", pos, len);
  317. break;
  318. }
  319. pos += len;
  320. }
  321. return e_end;
  322. }
  323. static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
  324. const u8 *end)
  325. {
  326. u16 len;
  327. const u8 *f_end;
  328. u8 realm_len, e;
  329. if (end - pos < 4) {
  330. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  331. "fixed fields");
  332. return NULL;
  333. }
  334. len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
  335. pos += 2;
  336. if (pos + len > end || len < 3) {
  337. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  338. "(len=%u; left=%u)",
  339. len, (unsigned int) (end - pos));
  340. return NULL;
  341. }
  342. f_end = pos + len;
  343. r->encoding = *pos++;
  344. realm_len = *pos++;
  345. if (pos + realm_len > f_end) {
  346. wpa_printf(MSG_DEBUG, "No room for NAI Realm "
  347. "(len=%u; left=%u)",
  348. realm_len, (unsigned int) (f_end - pos));
  349. return NULL;
  350. }
  351. wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
  352. r->realm = dup_binstr(pos, realm_len);
  353. if (r->realm == NULL)
  354. return NULL;
  355. pos += realm_len;
  356. if (pos + 1 > f_end) {
  357. wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
  358. return NULL;
  359. }
  360. r->eap_count = *pos++;
  361. wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
  362. if (pos + r->eap_count * 3 > f_end) {
  363. wpa_printf(MSG_DEBUG, "No room for EAP Methods");
  364. return NULL;
  365. }
  366. r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
  367. if (r->eap == NULL)
  368. return NULL;
  369. for (e = 0; e < r->eap_count; e++) {
  370. pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
  371. if (pos == NULL)
  372. return NULL;
  373. }
  374. return f_end;
  375. }
  376. static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
  377. {
  378. struct nai_realm *realm;
  379. const u8 *pos, *end;
  380. u16 i, num;
  381. if (anqp == NULL || wpabuf_len(anqp) < 2)
  382. return NULL;
  383. pos = wpabuf_head_u8(anqp);
  384. end = pos + wpabuf_len(anqp);
  385. num = WPA_GET_LE16(pos);
  386. wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
  387. pos += 2;
  388. if (num * 5 > end - pos) {
  389. wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
  390. "enough data (%u octets) for that many realms",
  391. num, (unsigned int) (end - pos));
  392. return NULL;
  393. }
  394. realm = os_calloc(num, sizeof(struct nai_realm));
  395. if (realm == NULL)
  396. return NULL;
  397. for (i = 0; i < num; i++) {
  398. pos = nai_realm_parse_realm(&realm[i], pos, end);
  399. if (pos == NULL) {
  400. nai_realm_free(realm, num);
  401. return NULL;
  402. }
  403. }
  404. *count = num;
  405. return realm;
  406. }
  407. static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
  408. {
  409. char *tmp, *pos, *end;
  410. int match = 0;
  411. if (realm->realm == NULL || home_realm == NULL)
  412. return 0;
  413. if (os_strchr(realm->realm, ';') == NULL)
  414. return os_strcasecmp(realm->realm, home_realm) == 0;
  415. tmp = os_strdup(realm->realm);
  416. if (tmp == NULL)
  417. return 0;
  418. pos = tmp;
  419. while (*pos) {
  420. end = os_strchr(pos, ';');
  421. if (end)
  422. *end = '\0';
  423. if (os_strcasecmp(pos, home_realm) == 0) {
  424. match = 1;
  425. break;
  426. }
  427. if (end == NULL)
  428. break;
  429. pos = end + 1;
  430. }
  431. os_free(tmp);
  432. return match;
  433. }
  434. static int nai_realm_cred_username(struct nai_realm_eap *eap)
  435. {
  436. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  437. return 0; /* method not supported */
  438. if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
  439. eap->method != EAP_TYPE_FAST) {
  440. /* Only tunneled methods with username/password supported */
  441. return 0;
  442. }
  443. if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
  444. if (eap->inner_method &&
  445. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  446. return 0;
  447. if (!eap->inner_method &&
  448. eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
  449. return 0;
  450. }
  451. if (eap->method == EAP_TYPE_TTLS) {
  452. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  453. return 1; /* Assume TTLS/MSCHAPv2 is used */
  454. if (eap->inner_method &&
  455. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  456. return 0;
  457. if (eap->inner_non_eap &&
  458. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  459. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  460. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  461. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  462. return 0;
  463. }
  464. if (eap->inner_method &&
  465. eap->inner_method != EAP_TYPE_GTC &&
  466. eap->inner_method != EAP_TYPE_MSCHAPV2)
  467. return 0;
  468. return 1;
  469. }
  470. static int nai_realm_cred_cert(struct nai_realm_eap *eap)
  471. {
  472. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  473. return 0; /* method not supported */
  474. if (eap->method != EAP_TYPE_TLS) {
  475. /* Only EAP-TLS supported for credential authentication */
  476. return 0;
  477. }
  478. return 1;
  479. }
  480. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  481. struct nai_realm *realm)
  482. {
  483. u8 e;
  484. if (cred == NULL ||
  485. cred->username == NULL ||
  486. cred->username[0] == '\0' ||
  487. ((cred->password == NULL ||
  488. cred->password[0] == '\0') &&
  489. (cred->private_key == NULL ||
  490. cred->private_key[0] == '\0')))
  491. return NULL;
  492. for (e = 0; e < realm->eap_count; e++) {
  493. struct nai_realm_eap *eap = &realm->eap[e];
  494. if (cred->password && cred->password[0] &&
  495. nai_realm_cred_username(eap))
  496. return eap;
  497. if (cred->private_key && cred->private_key[0] &&
  498. nai_realm_cred_cert(eap))
  499. return eap;
  500. }
  501. return NULL;
  502. }
  503. #ifdef INTERWORKING_3GPP
  504. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  505. {
  506. u8 plmn[3], plmn2[3];
  507. const u8 *pos, *end;
  508. u8 udhl;
  509. /*
  510. * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
  511. * operator is allowed to include only two digits of the MNC, so allow
  512. * matches based on both two and three digit MNC assumptions. Since some
  513. * SIM/USIM cards may not expose MNC length conveniently, we may be
  514. * provided the default MNC length 3 here and as such, checking with MNC
  515. * length 2 is justifiable even though 3GPP TS 24.234 does not mention
  516. * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
  517. * with otherwise matching values would not be good idea in general, so
  518. * this should not result in selecting incorrect networks.
  519. */
  520. /* Match with 3 digit MNC */
  521. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  522. plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
  523. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  524. /* Match with 2 digit MNC */
  525. plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  526. plmn2[1] = (imsi[2] - '0') | 0xf0;
  527. plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  528. if (anqp == NULL)
  529. return 0;
  530. pos = wpabuf_head_u8(anqp);
  531. end = pos + wpabuf_len(anqp);
  532. if (pos + 2 > end)
  533. return 0;
  534. if (*pos != 0) {
  535. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  536. return 0;
  537. }
  538. pos++;
  539. udhl = *pos++;
  540. if (pos + udhl > end) {
  541. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  542. return 0;
  543. }
  544. end = pos + udhl;
  545. wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
  546. plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
  547. imsi, mnc_len);
  548. while (pos + 2 <= end) {
  549. u8 iei, len;
  550. const u8 *l_end;
  551. iei = *pos++;
  552. len = *pos++ & 0x7f;
  553. if (pos + len > end)
  554. break;
  555. l_end = pos + len;
  556. if (iei == 0 && len > 0) {
  557. /* PLMN List */
  558. u8 num, i;
  559. wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
  560. pos, len);
  561. num = *pos++;
  562. for (i = 0; i < num; i++) {
  563. if (pos + 3 > l_end)
  564. break;
  565. if (os_memcmp(pos, plmn, 3) == 0 ||
  566. os_memcmp(pos, plmn2, 3) == 0)
  567. return 1; /* Found matching PLMN */
  568. pos += 3;
  569. }
  570. } else {
  571. wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
  572. pos, len);
  573. }
  574. pos = l_end;
  575. }
  576. return 0;
  577. }
  578. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  579. size_t mnc_len, char prefix)
  580. {
  581. const char *sep, *msin;
  582. char *end, *pos;
  583. size_t msin_len, plmn_len;
  584. /*
  585. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  586. * Root NAI:
  587. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  588. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  589. */
  590. if (imsi == NULL || os_strlen(imsi) > 16) {
  591. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  592. return -1;
  593. }
  594. sep = os_strchr(imsi, '-');
  595. if (sep) {
  596. plmn_len = sep - imsi;
  597. msin = sep + 1;
  598. } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
  599. plmn_len = 3 + mnc_len;
  600. msin = imsi + plmn_len;
  601. } else
  602. return -1;
  603. if (plmn_len != 5 && plmn_len != 6)
  604. return -1;
  605. msin_len = os_strlen(msin);
  606. pos = nai;
  607. end = nai + nai_len;
  608. if (prefix)
  609. *pos++ = prefix;
  610. os_memcpy(pos, imsi, plmn_len);
  611. pos += plmn_len;
  612. os_memcpy(pos, msin, msin_len);
  613. pos += msin_len;
  614. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  615. if (plmn_len == 5) {
  616. *pos++ = '0';
  617. *pos++ = imsi[3];
  618. *pos++ = imsi[4];
  619. } else {
  620. *pos++ = imsi[3];
  621. *pos++ = imsi[4];
  622. *pos++ = imsi[5];
  623. }
  624. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  625. imsi[0], imsi[1], imsi[2]);
  626. return 0;
  627. }
  628. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  629. {
  630. char nai[100];
  631. if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
  632. return -1;
  633. return wpa_config_set_quoted(ssid, "identity", nai);
  634. }
  635. #endif /* INTERWORKING_3GPP */
  636. static int already_connected(struct wpa_supplicant *wpa_s,
  637. struct wpa_cred *cred, struct wpa_bss *bss)
  638. {
  639. struct wpa_ssid *ssid;
  640. if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
  641. return 0;
  642. ssid = wpa_s->current_ssid;
  643. if (ssid->parent_cred != cred)
  644. return 0;
  645. if (ssid->ssid_len != bss->ssid_len ||
  646. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  647. return 0;
  648. return 1;
  649. }
  650. static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
  651. struct wpa_cred *cred,
  652. struct wpa_bss *bss)
  653. {
  654. struct wpa_ssid *ssid;
  655. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  656. if (ssid->parent_cred != cred)
  657. continue;
  658. if (ssid->ssid_len != bss->ssid_len ||
  659. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  660. continue;
  661. break;
  662. }
  663. if (ssid == NULL)
  664. return;
  665. wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
  666. if (ssid == wpa_s->current_ssid) {
  667. wpa_sm_set_config(wpa_s->wpa, NULL);
  668. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  669. wpa_supplicant_deauthenticate(wpa_s,
  670. WLAN_REASON_DEAUTH_LEAVING);
  671. }
  672. wpas_notify_network_removed(wpa_s, ssid);
  673. wpa_config_remove_network(wpa_s->conf, ssid->id);
  674. }
  675. static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
  676. struct wpa_ssid *ssid)
  677. {
  678. if (wpa_config_set(ssid, "key_mgmt",
  679. wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
  680. "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
  681. return -1;
  682. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  683. return -1;
  684. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  685. return -1;
  686. return 0;
  687. }
  688. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  689. struct wpa_cred *cred,
  690. struct wpa_bss *bss)
  691. {
  692. #ifdef INTERWORKING_3GPP
  693. struct wpa_ssid *ssid;
  694. int eap_type;
  695. int res;
  696. char prefix;
  697. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  698. return -1;
  699. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  700. MAC2STR(bss->bssid));
  701. if (already_connected(wpa_s, cred, bss)) {
  702. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  703. MAC2STR(bss->bssid));
  704. return 0;
  705. }
  706. remove_duplicate_network(wpa_s, cred, bss);
  707. ssid = wpa_config_add_network(wpa_s->conf);
  708. if (ssid == NULL)
  709. return -1;
  710. ssid->parent_cred = cred;
  711. wpas_notify_network_added(wpa_s, ssid);
  712. wpa_config_set_network_defaults(ssid);
  713. ssid->priority = cred->priority;
  714. ssid->temporary = 1;
  715. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  716. if (ssid->ssid == NULL)
  717. goto fail;
  718. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  719. ssid->ssid_len = bss->ssid_len;
  720. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  721. goto fail;
  722. eap_type = EAP_TYPE_SIM;
  723. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  724. eap_type = EAP_TYPE_AKA;
  725. if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
  726. if (cred->eap_method[0].method == EAP_TYPE_SIM ||
  727. cred->eap_method[0].method == EAP_TYPE_AKA ||
  728. cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
  729. eap_type = cred->eap_method[0].method;
  730. }
  731. switch (eap_type) {
  732. case EAP_TYPE_SIM:
  733. prefix = '1';
  734. res = wpa_config_set(ssid, "eap", "SIM", 0);
  735. break;
  736. case EAP_TYPE_AKA:
  737. prefix = '0';
  738. res = wpa_config_set(ssid, "eap", "AKA", 0);
  739. break;
  740. case EAP_TYPE_AKA_PRIME:
  741. prefix = '6';
  742. res = wpa_config_set(ssid, "eap", "AKA'", 0);
  743. break;
  744. default:
  745. res = -1;
  746. break;
  747. }
  748. if (res < 0) {
  749. wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
  750. eap_type);
  751. goto fail;
  752. }
  753. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
  754. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  755. goto fail;
  756. }
  757. if (cred->milenage && cred->milenage[0]) {
  758. if (wpa_config_set_quoted(ssid, "password",
  759. cred->milenage) < 0)
  760. goto fail;
  761. } else if (cred->pcsc) {
  762. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  763. goto fail;
  764. if (wpa_s->conf->pcsc_pin &&
  765. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  766. < 0)
  767. goto fail;
  768. }
  769. if (cred->password && cred->password[0] &&
  770. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  771. goto fail;
  772. wpa_s->next_ssid = ssid;
  773. wpa_config_update_prio_list(wpa_s->conf);
  774. interworking_reconnect(wpa_s);
  775. return 0;
  776. fail:
  777. wpas_notify_network_removed(wpa_s, ssid);
  778. wpa_config_remove_network(wpa_s->conf, ssid->id);
  779. #endif /* INTERWORKING_3GPP */
  780. return -1;
  781. }
  782. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  783. size_t rc_len)
  784. {
  785. const u8 *pos, *end;
  786. u8 lens;
  787. if (ie == NULL)
  788. return 0;
  789. pos = ie + 2;
  790. end = ie + 2 + ie[1];
  791. /* Roaming Consortium element:
  792. * Number of ANQP OIs
  793. * OI #1 and #2 lengths
  794. * OI #1, [OI #2], [OI #3]
  795. */
  796. if (pos + 2 > end)
  797. return 0;
  798. pos++; /* skip Number of ANQP OIs */
  799. lens = *pos++;
  800. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  801. return 0;
  802. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  803. return 1;
  804. pos += lens & 0x0f;
  805. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  806. return 1;
  807. pos += lens >> 4;
  808. if (pos < end && (size_t) (end - pos) == rc_len &&
  809. os_memcmp(pos, rc_id, rc_len) == 0)
  810. return 1;
  811. return 0;
  812. }
  813. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  814. const u8 *rc_id, size_t rc_len)
  815. {
  816. const u8 *pos, *end;
  817. u8 len;
  818. if (anqp == NULL)
  819. return 0;
  820. pos = wpabuf_head(anqp);
  821. end = pos + wpabuf_len(anqp);
  822. /* Set of <OI Length, OI> duples */
  823. while (pos < end) {
  824. len = *pos++;
  825. if (pos + len > end)
  826. break;
  827. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  828. return 1;
  829. pos += len;
  830. }
  831. return 0;
  832. }
  833. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  834. const u8 *rc_id, size_t rc_len)
  835. {
  836. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  837. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  838. }
  839. static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
  840. {
  841. const u8 *ie;
  842. if (cred->required_roaming_consortium_len == 0)
  843. return 0;
  844. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  845. if (ie == NULL &&
  846. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  847. return 1;
  848. return !roaming_consortium_match(ie,
  849. bss->anqp ?
  850. bss->anqp->roaming_consortium : NULL,
  851. cred->required_roaming_consortium,
  852. cred->required_roaming_consortium_len);
  853. }
  854. static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
  855. {
  856. size_t i;
  857. if (!cred->excluded_ssid)
  858. return 0;
  859. for (i = 0; i < cred->num_excluded_ssid; i++) {
  860. struct excluded_ssid *e = &cred->excluded_ssid[i];
  861. if (bss->ssid_len == e->ssid_len &&
  862. os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
  863. return 1;
  864. }
  865. return 0;
  866. }
  867. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  868. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  869. {
  870. struct wpa_cred *cred, *selected = NULL;
  871. const u8 *ie;
  872. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  873. if (ie == NULL &&
  874. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  875. return NULL;
  876. if (wpa_s->conf->cred == NULL)
  877. return NULL;
  878. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  879. if (cred->roaming_consortium_len == 0)
  880. continue;
  881. if (!roaming_consortium_match(ie,
  882. bss->anqp ?
  883. bss->anqp->roaming_consortium :
  884. NULL,
  885. cred->roaming_consortium,
  886. cred->roaming_consortium_len))
  887. continue;
  888. if (cred_excluded_ssid(cred, bss))
  889. continue;
  890. if (cred_no_required_oi_match(cred, bss))
  891. continue;
  892. if (selected == NULL ||
  893. selected->priority < cred->priority)
  894. selected = cred;
  895. }
  896. return selected;
  897. }
  898. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  899. struct wpa_cred *cred, int ttls)
  900. {
  901. if (cred->eap_method) {
  902. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  903. cred->eap_method->method == EAP_TYPE_TTLS;
  904. os_free(ssid->eap.eap_methods);
  905. ssid->eap.eap_methods =
  906. os_malloc(sizeof(struct eap_method_type) * 2);
  907. if (ssid->eap.eap_methods == NULL)
  908. return -1;
  909. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  910. sizeof(*cred->eap_method));
  911. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  912. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  913. }
  914. if (ttls && cred->username && cred->username[0]) {
  915. const char *pos;
  916. char *anon;
  917. /* Use anonymous NAI in Phase 1 */
  918. pos = os_strchr(cred->username, '@');
  919. if (pos) {
  920. size_t buflen = 9 + os_strlen(pos) + 1;
  921. anon = os_malloc(buflen);
  922. if (anon == NULL)
  923. return -1;
  924. os_snprintf(anon, buflen, "anonymous%s", pos);
  925. } else if (cred->realm) {
  926. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  927. anon = os_malloc(buflen);
  928. if (anon == NULL)
  929. return -1;
  930. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  931. } else {
  932. anon = os_strdup("anonymous");
  933. if (anon == NULL)
  934. return -1;
  935. }
  936. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  937. 0) {
  938. os_free(anon);
  939. return -1;
  940. }
  941. os_free(anon);
  942. }
  943. if (cred->username && cred->username[0] &&
  944. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  945. return -1;
  946. if (cred->password && cred->password[0]) {
  947. if (cred->ext_password &&
  948. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  949. return -1;
  950. if (!cred->ext_password &&
  951. wpa_config_set_quoted(ssid, "password", cred->password) <
  952. 0)
  953. return -1;
  954. }
  955. if (cred->client_cert && cred->client_cert[0] &&
  956. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  957. return -1;
  958. #ifdef ANDROID
  959. if (cred->private_key &&
  960. os_strncmp(cred->private_key, "keystore://", 11) == 0) {
  961. /* Use OpenSSL engine configuration for Android keystore */
  962. if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
  963. wpa_config_set_quoted(ssid, "key_id",
  964. cred->private_key + 11) < 0 ||
  965. wpa_config_set(ssid, "engine", "1", 0) < 0)
  966. return -1;
  967. } else
  968. #endif /* ANDROID */
  969. if (cred->private_key && cred->private_key[0] &&
  970. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  971. return -1;
  972. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  973. wpa_config_set_quoted(ssid, "private_key_passwd",
  974. cred->private_key_passwd) < 0)
  975. return -1;
  976. if (cred->phase1) {
  977. os_free(ssid->eap.phase1);
  978. ssid->eap.phase1 = os_strdup(cred->phase1);
  979. }
  980. if (cred->phase2) {
  981. os_free(ssid->eap.phase2);
  982. ssid->eap.phase2 = os_strdup(cred->phase2);
  983. }
  984. if (cred->ca_cert && cred->ca_cert[0] &&
  985. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  986. return -1;
  987. if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
  988. wpa_config_set_quoted(ssid, "domain_suffix_match",
  989. cred->domain_suffix_match) < 0)
  990. return -1;
  991. return 0;
  992. }
  993. static int interworking_connect_roaming_consortium(
  994. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  995. struct wpa_bss *bss)
  996. {
  997. struct wpa_ssid *ssid;
  998. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  999. "roaming consortium match", MAC2STR(bss->bssid));
  1000. if (already_connected(wpa_s, cred, bss)) {
  1001. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1002. MAC2STR(bss->bssid));
  1003. return 0;
  1004. }
  1005. remove_duplicate_network(wpa_s, cred, bss);
  1006. ssid = wpa_config_add_network(wpa_s->conf);
  1007. if (ssid == NULL)
  1008. return -1;
  1009. ssid->parent_cred = cred;
  1010. wpas_notify_network_added(wpa_s, ssid);
  1011. wpa_config_set_network_defaults(ssid);
  1012. ssid->priority = cred->priority;
  1013. ssid->temporary = 1;
  1014. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  1015. if (ssid->ssid == NULL)
  1016. goto fail;
  1017. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  1018. ssid->ssid_len = bss->ssid_len;
  1019. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1020. goto fail;
  1021. if (cred->eap_method == NULL) {
  1022. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  1023. "credential using roaming consortium");
  1024. goto fail;
  1025. }
  1026. if (interworking_set_eap_params(
  1027. ssid, cred,
  1028. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  1029. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  1030. goto fail;
  1031. wpa_s->next_ssid = ssid;
  1032. wpa_config_update_prio_list(wpa_s->conf);
  1033. interworking_reconnect(wpa_s);
  1034. return 0;
  1035. fail:
  1036. wpas_notify_network_removed(wpa_s, ssid);
  1037. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1038. return -1;
  1039. }
  1040. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1041. {
  1042. struct wpa_cred *cred, *cred_rc, *cred_3gpp;
  1043. struct wpa_ssid *ssid;
  1044. struct nai_realm *realm;
  1045. struct nai_realm_eap *eap = NULL;
  1046. u16 count, i;
  1047. char buf[100];
  1048. if (wpa_s->conf->cred == NULL || bss == NULL)
  1049. return -1;
  1050. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1051. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1052. wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
  1053. MACSTR, MAC2STR(bss->bssid));
  1054. return -1;
  1055. }
  1056. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1057. /*
  1058. * We currently support only HS 2.0 networks and those are
  1059. * required to use WPA2-Enterprise.
  1060. */
  1061. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  1062. "RSN");
  1063. return -1;
  1064. }
  1065. cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
  1066. bss);
  1067. if (cred_rc) {
  1068. wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
  1069. "consortium matching credential priority %d",
  1070. cred_rc->priority);
  1071. }
  1072. cred = interworking_credentials_available_realm(wpa_s, bss);
  1073. if (cred) {
  1074. wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
  1075. "matching credential priority %d",
  1076. cred->priority);
  1077. }
  1078. cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
  1079. if (cred_3gpp) {
  1080. wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
  1081. "credential priority %d", cred_3gpp->priority);
  1082. }
  1083. if (cred_rc &&
  1084. (cred == NULL || cred_rc->priority >= cred->priority) &&
  1085. (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
  1086. return interworking_connect_roaming_consortium(wpa_s, cred_rc,
  1087. bss);
  1088. if (cred_3gpp &&
  1089. (cred == NULL || cred_3gpp->priority >= cred->priority)) {
  1090. return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
  1091. }
  1092. if (cred == NULL) {
  1093. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1094. "found for " MACSTR, MAC2STR(bss->bssid));
  1095. return -1;
  1096. }
  1097. realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
  1098. &count);
  1099. if (realm == NULL) {
  1100. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1101. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1102. return -1;
  1103. }
  1104. for (i = 0; i < count; i++) {
  1105. if (!nai_realm_match(&realm[i], cred->realm))
  1106. continue;
  1107. eap = nai_realm_find_eap(cred, &realm[i]);
  1108. if (eap)
  1109. break;
  1110. }
  1111. if (!eap) {
  1112. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1113. "and EAP method found for " MACSTR,
  1114. MAC2STR(bss->bssid));
  1115. nai_realm_free(realm, count);
  1116. return -1;
  1117. }
  1118. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  1119. MAC2STR(bss->bssid));
  1120. if (already_connected(wpa_s, cred, bss)) {
  1121. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1122. MAC2STR(bss->bssid));
  1123. nai_realm_free(realm, count);
  1124. return 0;
  1125. }
  1126. remove_duplicate_network(wpa_s, cred, bss);
  1127. ssid = wpa_config_add_network(wpa_s->conf);
  1128. if (ssid == NULL) {
  1129. nai_realm_free(realm, count);
  1130. return -1;
  1131. }
  1132. ssid->parent_cred = cred;
  1133. wpas_notify_network_added(wpa_s, ssid);
  1134. wpa_config_set_network_defaults(ssid);
  1135. ssid->priority = cred->priority;
  1136. ssid->temporary = 1;
  1137. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  1138. if (ssid->ssid == NULL)
  1139. goto fail;
  1140. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  1141. ssid->ssid_len = bss->ssid_len;
  1142. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1143. goto fail;
  1144. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  1145. eap->method), 0) < 0)
  1146. goto fail;
  1147. switch (eap->method) {
  1148. case EAP_TYPE_TTLS:
  1149. if (eap->inner_method) {
  1150. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  1151. eap_get_name(EAP_VENDOR_IETF,
  1152. eap->inner_method));
  1153. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1154. goto fail;
  1155. break;
  1156. }
  1157. switch (eap->inner_non_eap) {
  1158. case NAI_REALM_INNER_NON_EAP_PAP:
  1159. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  1160. 0)
  1161. goto fail;
  1162. break;
  1163. case NAI_REALM_INNER_NON_EAP_CHAP:
  1164. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1165. < 0)
  1166. goto fail;
  1167. break;
  1168. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1169. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1170. 0) < 0)
  1171. goto fail;
  1172. break;
  1173. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1174. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1175. 0) < 0)
  1176. goto fail;
  1177. break;
  1178. default:
  1179. /* EAP params were not set - assume TTLS/MSCHAPv2 */
  1180. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1181. 0) < 0)
  1182. goto fail;
  1183. break;
  1184. }
  1185. break;
  1186. case EAP_TYPE_PEAP:
  1187. case EAP_TYPE_FAST:
  1188. if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
  1189. 0) < 0)
  1190. goto fail;
  1191. if (wpa_config_set(ssid, "pac_file",
  1192. "\"blob://pac_interworking\"", 0) < 0)
  1193. goto fail;
  1194. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1195. eap_get_name(EAP_VENDOR_IETF,
  1196. eap->inner_method ?
  1197. eap->inner_method :
  1198. EAP_TYPE_MSCHAPV2));
  1199. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1200. goto fail;
  1201. break;
  1202. case EAP_TYPE_TLS:
  1203. break;
  1204. }
  1205. if (interworking_set_eap_params(ssid, cred,
  1206. eap->method == EAP_TYPE_TTLS) < 0)
  1207. goto fail;
  1208. nai_realm_free(realm, count);
  1209. wpa_s->next_ssid = ssid;
  1210. wpa_config_update_prio_list(wpa_s->conf);
  1211. interworking_reconnect(wpa_s);
  1212. return 0;
  1213. fail:
  1214. wpas_notify_network_removed(wpa_s, ssid);
  1215. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1216. nai_realm_free(realm, count);
  1217. return -1;
  1218. }
  1219. static struct wpa_cred * interworking_credentials_available_3gpp(
  1220. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1221. {
  1222. struct wpa_cred *selected = NULL;
  1223. #ifdef INTERWORKING_3GPP
  1224. struct wpa_cred *cred;
  1225. int ret;
  1226. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  1227. return NULL;
  1228. #ifdef CONFIG_EAP_PROXY
  1229. if (!wpa_s->imsi[0]) {
  1230. size_t len;
  1231. wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
  1232. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
  1233. wpa_s->imsi,
  1234. &len);
  1235. if (wpa_s->mnc_len > 0) {
  1236. wpa_s->imsi[len] = '\0';
  1237. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  1238. wpa_s->imsi, wpa_s->mnc_len);
  1239. } else {
  1240. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  1241. }
  1242. }
  1243. #endif /* CONFIG_EAP_PROXY */
  1244. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1245. char *sep;
  1246. const char *imsi;
  1247. int mnc_len;
  1248. char imsi_buf[16];
  1249. size_t msin_len;
  1250. #ifdef PCSC_FUNCS
  1251. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1252. wpa_s->imsi[0]) {
  1253. imsi = wpa_s->imsi;
  1254. mnc_len = wpa_s->mnc_len;
  1255. goto compare;
  1256. }
  1257. #endif /* PCSC_FUNCS */
  1258. #ifdef CONFIG_EAP_PROXY
  1259. if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1260. imsi = wpa_s->imsi;
  1261. mnc_len = wpa_s->mnc_len;
  1262. goto compare;
  1263. }
  1264. #endif /* CONFIG_EAP_PROXY */
  1265. if (cred->imsi == NULL || !cred->imsi[0] ||
  1266. (!wpa_s->conf->external_sim &&
  1267. (cred->milenage == NULL || !cred->milenage[0])))
  1268. continue;
  1269. sep = os_strchr(cred->imsi, '-');
  1270. if (sep == NULL ||
  1271. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1272. continue;
  1273. mnc_len = sep - cred->imsi - 3;
  1274. os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
  1275. sep++;
  1276. msin_len = os_strlen(cred->imsi);
  1277. if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
  1278. msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
  1279. os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
  1280. imsi_buf[3 + mnc_len + msin_len] = '\0';
  1281. imsi = imsi_buf;
  1282. #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
  1283. compare:
  1284. #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
  1285. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1286. MACSTR, MAC2STR(bss->bssid));
  1287. ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
  1288. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1289. if (ret) {
  1290. if (cred_excluded_ssid(cred, bss))
  1291. continue;
  1292. if (cred_no_required_oi_match(cred, bss))
  1293. continue;
  1294. if (selected == NULL ||
  1295. selected->priority < cred->priority)
  1296. selected = cred;
  1297. }
  1298. }
  1299. #endif /* INTERWORKING_3GPP */
  1300. return selected;
  1301. }
  1302. static struct wpa_cred * interworking_credentials_available_realm(
  1303. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1304. {
  1305. struct wpa_cred *cred, *selected = NULL;
  1306. struct nai_realm *realm;
  1307. u16 count, i;
  1308. if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
  1309. return NULL;
  1310. if (wpa_s->conf->cred == NULL)
  1311. return NULL;
  1312. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1313. MACSTR, MAC2STR(bss->bssid));
  1314. realm = nai_realm_parse(bss->anqp->nai_realm, &count);
  1315. if (realm == NULL) {
  1316. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1317. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1318. return NULL;
  1319. }
  1320. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1321. if (cred->realm == NULL)
  1322. continue;
  1323. for (i = 0; i < count; i++) {
  1324. if (!nai_realm_match(&realm[i], cred->realm))
  1325. continue;
  1326. if (nai_realm_find_eap(cred, &realm[i])) {
  1327. if (cred_excluded_ssid(cred, bss))
  1328. continue;
  1329. if (cred_no_required_oi_match(cred, bss))
  1330. continue;
  1331. if (selected == NULL ||
  1332. selected->priority < cred->priority)
  1333. selected = cred;
  1334. break;
  1335. }
  1336. }
  1337. }
  1338. nai_realm_free(realm, count);
  1339. return selected;
  1340. }
  1341. static struct wpa_cred * interworking_credentials_available(
  1342. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1343. {
  1344. struct wpa_cred *cred, *cred2;
  1345. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1346. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1347. wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
  1348. MACSTR, MAC2STR(bss->bssid));
  1349. return NULL;
  1350. }
  1351. cred = interworking_credentials_available_realm(wpa_s, bss);
  1352. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1353. if (cred && cred2 && cred2->priority >= cred->priority)
  1354. cred = cred2;
  1355. if (!cred)
  1356. cred = cred2;
  1357. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1358. bss);
  1359. if (cred && cred2 && cred2->priority >= cred->priority)
  1360. cred = cred2;
  1361. if (!cred)
  1362. cred = cred2;
  1363. return cred;
  1364. }
  1365. static int domain_name_list_contains(struct wpabuf *domain_names,
  1366. const char *domain, int exact_match)
  1367. {
  1368. const u8 *pos, *end;
  1369. size_t len;
  1370. len = os_strlen(domain);
  1371. pos = wpabuf_head(domain_names);
  1372. end = pos + wpabuf_len(domain_names);
  1373. while (pos + 1 < end) {
  1374. if (pos + 1 + pos[0] > end)
  1375. break;
  1376. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1377. pos + 1, pos[0]);
  1378. if (pos[0] == len &&
  1379. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1380. return 1;
  1381. if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
  1382. const char *ap = (const char *) (pos + 1);
  1383. int offset = pos[0] - len;
  1384. if (os_strncasecmp(domain, ap + offset, len) == 0)
  1385. return 1;
  1386. }
  1387. pos += 1 + pos[0];
  1388. }
  1389. return 0;
  1390. }
  1391. int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
  1392. struct wpa_cred *cred,
  1393. struct wpabuf *domain_names)
  1394. {
  1395. size_t i;
  1396. int ret = -1;
  1397. #ifdef INTERWORKING_3GPP
  1398. char nai[100], *realm;
  1399. char *imsi = NULL;
  1400. int mnc_len = 0;
  1401. if (cred->imsi)
  1402. imsi = cred->imsi;
  1403. #ifdef CONFIG_PCSC
  1404. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1405. wpa_s->scard && wpa_s->imsi[0]) {
  1406. imsi = wpa_s->imsi;
  1407. mnc_len = wpa_s->mnc_len;
  1408. }
  1409. #endif /* CONFIG_PCSC */
  1410. #ifdef CONFIG_EAP_PROXY
  1411. else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1412. imsi = wpa_s->imsi;
  1413. mnc_len = wpa_s->mnc_len;
  1414. }
  1415. #endif /* CONFIG_EAP_PROXY */
  1416. if (domain_names &&
  1417. imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
  1418. realm = os_strchr(nai, '@');
  1419. if (realm)
  1420. realm++;
  1421. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1422. "with SIM/USIM domain %s", realm);
  1423. if (realm &&
  1424. domain_name_list_contains(domain_names, realm, 1))
  1425. return 1;
  1426. if (realm)
  1427. ret = 0;
  1428. }
  1429. #endif /* INTERWORKING_3GPP */
  1430. if (domain_names == NULL || cred->domain == NULL)
  1431. return ret;
  1432. for (i = 0; i < cred->num_domain; i++) {
  1433. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1434. "home SP FQDN %s", cred->domain[i]);
  1435. if (domain_name_list_contains(domain_names, cred->domain[i], 1))
  1436. return 1;
  1437. }
  1438. return 0;
  1439. }
  1440. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1441. struct wpabuf *domain_names)
  1442. {
  1443. struct wpa_cred *cred;
  1444. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1445. return -1;
  1446. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1447. int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
  1448. if (res)
  1449. return res;
  1450. }
  1451. return 0;
  1452. }
  1453. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1454. {
  1455. struct wpa_bss *bss;
  1456. struct wpa_ssid *ssid;
  1457. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1458. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1459. if (wpas_network_disabled(wpa_s, ssid) ||
  1460. ssid->mode != WPAS_MODE_INFRA)
  1461. continue;
  1462. if (ssid->ssid_len != bss->ssid_len ||
  1463. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1464. 0)
  1465. continue;
  1466. /*
  1467. * TODO: Consider more accurate matching of security
  1468. * configuration similarly to what is done in events.c
  1469. */
  1470. return 1;
  1471. }
  1472. }
  1473. return 0;
  1474. }
  1475. static int roaming_partner_match(struct wpa_supplicant *wpa_s,
  1476. struct roaming_partner *partner,
  1477. struct wpabuf *domain_names)
  1478. {
  1479. if (!domain_name_list_contains(domain_names, partner->fqdn,
  1480. partner->exact_match))
  1481. return 0;
  1482. /* TODO: match Country */
  1483. return 1;
  1484. }
  1485. static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  1486. struct wpa_bss *bss)
  1487. {
  1488. size_t i;
  1489. if (bss->anqp == NULL || bss->anqp->domain_name == NULL)
  1490. return 128; /* cannot check preference with domain name */
  1491. if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
  1492. return 0; /* max preference for home SP network */
  1493. for (i = 0; i < cred->num_roaming_partner; i++) {
  1494. if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
  1495. bss->anqp->domain_name))
  1496. return cred->roaming_partner[i].priority;
  1497. }
  1498. return 128;
  1499. }
  1500. static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
  1501. struct wpa_bss *selected,
  1502. struct wpa_cred *cred)
  1503. {
  1504. struct wpa_bss *bss;
  1505. u8 best_prio, prio;
  1506. /*
  1507. * Check if any other BSS is operated by a more preferred roaming
  1508. * partner.
  1509. */
  1510. best_prio = roaming_prio(wpa_s, cred, selected);
  1511. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1512. if (bss == selected)
  1513. continue;
  1514. cred = interworking_credentials_available(wpa_s, bss);
  1515. if (!cred)
  1516. continue;
  1517. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
  1518. continue;
  1519. prio = roaming_prio(wpa_s, cred, bss);
  1520. if (prio < best_prio) {
  1521. best_prio = prio;
  1522. selected = bss;
  1523. }
  1524. }
  1525. return selected;
  1526. }
  1527. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1528. {
  1529. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1530. int selected_prio = -999999, selected_home_prio = -999999;
  1531. unsigned int count = 0;
  1532. const char *type;
  1533. int res;
  1534. struct wpa_cred *cred, *selected_cred = NULL;
  1535. struct wpa_cred *selected_home_cred = NULL;
  1536. wpa_s->network_select = 0;
  1537. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1538. cred = interworking_credentials_available(wpa_s, bss);
  1539. if (!cred)
  1540. continue;
  1541. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1542. /*
  1543. * We currently support only HS 2.0 networks and those
  1544. * are required to use WPA2-Enterprise.
  1545. */
  1546. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1547. "with " MACSTR " but network does not use "
  1548. "RSN", MAC2STR(bss->bssid));
  1549. continue;
  1550. }
  1551. count++;
  1552. res = interworking_home_sp(wpa_s, bss->anqp ?
  1553. bss->anqp->domain_name : NULL);
  1554. if (res > 0)
  1555. type = "home";
  1556. else if (res == 0)
  1557. type = "roaming";
  1558. else
  1559. type = "unknown";
  1560. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1561. MAC2STR(bss->bssid), type);
  1562. if (wpa_s->auto_select ||
  1563. (wpa_s->conf->auto_interworking &&
  1564. wpa_s->auto_network_select)) {
  1565. if (selected == NULL ||
  1566. cred->priority > selected_prio) {
  1567. selected = bss;
  1568. selected_prio = cred->priority;
  1569. selected_cred = cred;
  1570. }
  1571. if (res > 0 &&
  1572. (selected_home == NULL ||
  1573. cred->priority > selected_home_prio)) {
  1574. selected_home = bss;
  1575. selected_home_prio = cred->priority;
  1576. selected_home_cred = cred;
  1577. }
  1578. }
  1579. }
  1580. if (selected_home && selected_home != selected &&
  1581. selected_home_prio >= selected_prio) {
  1582. /* Prefer network operated by the Home SP */
  1583. selected = selected_home;
  1584. selected_cred = selected_home_cred;
  1585. }
  1586. if (count == 0) {
  1587. /*
  1588. * No matching network was found based on configured
  1589. * credentials. Check whether any of the enabled network blocks
  1590. * have matching APs.
  1591. */
  1592. if (interworking_find_network_match(wpa_s)) {
  1593. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1594. "match for enabled network configurations");
  1595. if (wpa_s->auto_select)
  1596. interworking_reconnect(wpa_s);
  1597. return;
  1598. }
  1599. if (wpa_s->auto_network_select) {
  1600. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1601. "scanning after ANQP fetch");
  1602. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  1603. 0);
  1604. return;
  1605. }
  1606. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1607. "with matching credentials found");
  1608. }
  1609. if (selected) {
  1610. selected = pick_best_roaming_partner(wpa_s, selected,
  1611. selected_cred);
  1612. interworking_connect(wpa_s, selected);
  1613. }
  1614. }
  1615. static struct wpa_bss_anqp *
  1616. interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1617. {
  1618. struct wpa_bss *other;
  1619. if (is_zero_ether_addr(bss->hessid))
  1620. return NULL; /* Cannot be in the same homegenous ESS */
  1621. dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
  1622. if (other == bss)
  1623. continue;
  1624. if (other->anqp == NULL)
  1625. continue;
  1626. if (other->anqp->roaming_consortium == NULL &&
  1627. other->anqp->nai_realm == NULL &&
  1628. other->anqp->anqp_3gpp == NULL &&
  1629. other->anqp->domain_name == NULL)
  1630. continue;
  1631. if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
  1632. continue;
  1633. if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
  1634. continue;
  1635. if (bss->ssid_len != other->ssid_len ||
  1636. os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
  1637. continue;
  1638. wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
  1639. "already fetched BSSID " MACSTR " and " MACSTR,
  1640. MAC2STR(other->bssid), MAC2STR(bss->bssid));
  1641. other->anqp->users++;
  1642. return other->anqp;
  1643. }
  1644. return NULL;
  1645. }
  1646. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1647. {
  1648. struct wpa_bss *bss;
  1649. int found = 0;
  1650. const u8 *ie;
  1651. if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
  1652. return;
  1653. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1654. if (!(bss->caps & IEEE80211_CAP_ESS))
  1655. continue;
  1656. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1657. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1658. continue; /* AP does not support Interworking */
  1659. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1660. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
  1661. continue; /* Disallowed BSS */
  1662. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1663. if (bss->anqp == NULL) {
  1664. bss->anqp = interworking_match_anqp_info(wpa_s,
  1665. bss);
  1666. if (bss->anqp) {
  1667. /* Shared data already fetched */
  1668. continue;
  1669. }
  1670. bss->anqp = wpa_bss_anqp_alloc();
  1671. if (bss->anqp == NULL)
  1672. break;
  1673. }
  1674. found++;
  1675. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1676. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1677. MACSTR, MAC2STR(bss->bssid));
  1678. interworking_anqp_send_req(wpa_s, bss);
  1679. break;
  1680. }
  1681. }
  1682. if (found == 0) {
  1683. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1684. wpa_s->fetch_anqp_in_progress = 0;
  1685. if (wpa_s->network_select)
  1686. interworking_select_network(wpa_s);
  1687. }
  1688. }
  1689. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1690. {
  1691. struct wpa_bss *bss;
  1692. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1693. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1694. wpa_s->fetch_anqp_in_progress = 1;
  1695. interworking_next_anqp_fetch(wpa_s);
  1696. }
  1697. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1698. {
  1699. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1700. return 0;
  1701. wpa_s->network_select = 0;
  1702. wpa_s->fetch_all_anqp = 1;
  1703. interworking_start_fetch_anqp(wpa_s);
  1704. return 0;
  1705. }
  1706. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1707. {
  1708. if (!wpa_s->fetch_anqp_in_progress)
  1709. return;
  1710. wpa_s->fetch_anqp_in_progress = 0;
  1711. }
  1712. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1713. u16 info_ids[], size_t num_ids)
  1714. {
  1715. struct wpabuf *buf;
  1716. int ret = 0;
  1717. int freq;
  1718. struct wpa_bss *bss;
  1719. int res;
  1720. freq = wpa_s->assoc_freq;
  1721. bss = wpa_bss_get_bssid(wpa_s, dst);
  1722. if (bss) {
  1723. wpa_bss_anqp_unshare_alloc(bss);
  1724. freq = bss->freq;
  1725. }
  1726. if (freq <= 0)
  1727. return -1;
  1728. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1729. MAC2STR(dst), (unsigned int) num_ids);
  1730. buf = anqp_build_req(info_ids, num_ids, NULL);
  1731. if (buf == NULL)
  1732. return -1;
  1733. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1734. if (res < 0) {
  1735. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1736. wpabuf_free(buf);
  1737. ret = -1;
  1738. } else
  1739. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1740. "%u", res);
  1741. return ret;
  1742. }
  1743. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1744. struct wpa_bss *bss, const u8 *sa,
  1745. u16 info_id,
  1746. const u8 *data, size_t slen)
  1747. {
  1748. const u8 *pos = data;
  1749. struct wpa_bss_anqp *anqp = NULL;
  1750. #ifdef CONFIG_HS20
  1751. u8 type;
  1752. #endif /* CONFIG_HS20 */
  1753. if (bss)
  1754. anqp = bss->anqp;
  1755. switch (info_id) {
  1756. case ANQP_CAPABILITY_LIST:
  1757. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1758. " ANQP Capability list", MAC2STR(sa));
  1759. break;
  1760. case ANQP_VENUE_NAME:
  1761. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1762. " Venue Name", MAC2STR(sa));
  1763. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1764. if (anqp) {
  1765. wpabuf_free(anqp->venue_name);
  1766. anqp->venue_name = wpabuf_alloc_copy(pos, slen);
  1767. }
  1768. break;
  1769. case ANQP_NETWORK_AUTH_TYPE:
  1770. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1771. " Network Authentication Type information",
  1772. MAC2STR(sa));
  1773. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1774. "Type", pos, slen);
  1775. if (anqp) {
  1776. wpabuf_free(anqp->network_auth_type);
  1777. anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
  1778. }
  1779. break;
  1780. case ANQP_ROAMING_CONSORTIUM:
  1781. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1782. " Roaming Consortium list", MAC2STR(sa));
  1783. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1784. pos, slen);
  1785. if (anqp) {
  1786. wpabuf_free(anqp->roaming_consortium);
  1787. anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
  1788. }
  1789. break;
  1790. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1791. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1792. " IP Address Type Availability information",
  1793. MAC2STR(sa));
  1794. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1795. pos, slen);
  1796. if (anqp) {
  1797. wpabuf_free(anqp->ip_addr_type_availability);
  1798. anqp->ip_addr_type_availability =
  1799. wpabuf_alloc_copy(pos, slen);
  1800. }
  1801. break;
  1802. case ANQP_NAI_REALM:
  1803. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1804. " NAI Realm list", MAC2STR(sa));
  1805. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1806. if (anqp) {
  1807. wpabuf_free(anqp->nai_realm);
  1808. anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
  1809. }
  1810. break;
  1811. case ANQP_3GPP_CELLULAR_NETWORK:
  1812. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1813. " 3GPP Cellular Network information", MAC2STR(sa));
  1814. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1815. pos, slen);
  1816. if (anqp) {
  1817. wpabuf_free(anqp->anqp_3gpp);
  1818. anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1819. }
  1820. break;
  1821. case ANQP_DOMAIN_NAME:
  1822. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1823. " Domain Name list", MAC2STR(sa));
  1824. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1825. if (anqp) {
  1826. wpabuf_free(anqp->domain_name);
  1827. anqp->domain_name = wpabuf_alloc_copy(pos, slen);
  1828. }
  1829. break;
  1830. case ANQP_VENDOR_SPECIFIC:
  1831. if (slen < 3)
  1832. return;
  1833. switch (WPA_GET_BE24(pos)) {
  1834. #ifdef CONFIG_HS20
  1835. case OUI_WFA:
  1836. pos += 3;
  1837. slen -= 3;
  1838. if (slen < 1)
  1839. return;
  1840. type = *pos++;
  1841. slen--;
  1842. switch (type) {
  1843. case HS20_ANQP_OUI_TYPE:
  1844. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1845. slen);
  1846. break;
  1847. default:
  1848. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1849. "vendor type %u", type);
  1850. break;
  1851. }
  1852. break;
  1853. #endif /* CONFIG_HS20 */
  1854. default:
  1855. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1856. "vendor-specific ANQP OUI %06x",
  1857. WPA_GET_BE24(pos));
  1858. return;
  1859. }
  1860. break;
  1861. default:
  1862. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1863. "%u", info_id);
  1864. break;
  1865. }
  1866. }
  1867. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1868. enum gas_query_result result,
  1869. const struct wpabuf *adv_proto,
  1870. const struct wpabuf *resp, u16 status_code)
  1871. {
  1872. struct wpa_supplicant *wpa_s = ctx;
  1873. const u8 *pos;
  1874. const u8 *end;
  1875. u16 info_id;
  1876. u16 slen;
  1877. struct wpa_bss *bss = NULL, *tmp;
  1878. if (result != GAS_QUERY_SUCCESS)
  1879. return;
  1880. pos = wpabuf_head(adv_proto);
  1881. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1882. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1883. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1884. "Protocol in response");
  1885. return;
  1886. }
  1887. /*
  1888. * If possible, select the BSS entry based on which BSS entry was used
  1889. * for the request. This can help in cases where multiple BSS entries
  1890. * may exist for the same AP.
  1891. */
  1892. dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
  1893. if (tmp == wpa_s->interworking_gas_bss &&
  1894. os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
  1895. bss = tmp;
  1896. break;
  1897. }
  1898. }
  1899. if (bss == NULL)
  1900. bss = wpa_bss_get_bssid(wpa_s, dst);
  1901. pos = wpabuf_head(resp);
  1902. end = pos + wpabuf_len(resp);
  1903. while (pos < end) {
  1904. if (pos + 4 > end) {
  1905. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1906. break;
  1907. }
  1908. info_id = WPA_GET_LE16(pos);
  1909. pos += 2;
  1910. slen = WPA_GET_LE16(pos);
  1911. pos += 2;
  1912. if (pos + slen > end) {
  1913. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1914. "for Info ID %u", info_id);
  1915. break;
  1916. }
  1917. interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
  1918. slen);
  1919. pos += slen;
  1920. }
  1921. }
  1922. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1923. struct wpa_scan_results *scan_res)
  1924. {
  1925. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1926. "ANQP fetch");
  1927. interworking_start_fetch_anqp(wpa_s);
  1928. }
  1929. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
  1930. int *freqs)
  1931. {
  1932. interworking_stop_fetch_anqp(wpa_s);
  1933. wpa_s->network_select = 1;
  1934. wpa_s->auto_network_select = 0;
  1935. wpa_s->auto_select = !!auto_select;
  1936. wpa_s->fetch_all_anqp = 0;
  1937. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1938. "selection");
  1939. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1940. wpa_s->normal_scans = 0;
  1941. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1942. os_free(wpa_s->manual_scan_freqs);
  1943. wpa_s->manual_scan_freqs = freqs;
  1944. wpa_s->after_wps = 0;
  1945. wpa_s->known_wps_freq = 0;
  1946. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1947. return 0;
  1948. }
  1949. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1950. enum gas_query_result result,
  1951. const struct wpabuf *adv_proto,
  1952. const struct wpabuf *resp, u16 status_code)
  1953. {
  1954. struct wpa_supplicant *wpa_s = ctx;
  1955. struct wpabuf *n;
  1956. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  1957. " dialog_token=%d status_code=%d resp_len=%d",
  1958. MAC2STR(addr), dialog_token, status_code,
  1959. resp ? (int) wpabuf_len(resp) : -1);
  1960. if (!resp)
  1961. return;
  1962. n = wpabuf_dup(resp);
  1963. if (n == NULL)
  1964. return;
  1965. wpabuf_free(wpa_s->prev_gas_resp);
  1966. wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
  1967. os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
  1968. wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
  1969. wpa_s->last_gas_resp = n;
  1970. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  1971. wpa_s->last_gas_dialog_token = dialog_token;
  1972. }
  1973. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1974. const struct wpabuf *adv_proto,
  1975. const struct wpabuf *query)
  1976. {
  1977. struct wpabuf *buf;
  1978. int ret = 0;
  1979. int freq;
  1980. struct wpa_bss *bss;
  1981. int res;
  1982. size_t len;
  1983. u8 query_resp_len_limit = 0, pame_bi = 0;
  1984. freq = wpa_s->assoc_freq;
  1985. bss = wpa_bss_get_bssid(wpa_s, dst);
  1986. if (bss)
  1987. freq = bss->freq;
  1988. if (freq <= 0)
  1989. return -1;
  1990. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  1991. MAC2STR(dst), freq);
  1992. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  1993. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  1994. len = 3 + wpabuf_len(adv_proto) + 2;
  1995. if (query)
  1996. len += wpabuf_len(query);
  1997. buf = gas_build_initial_req(0, len);
  1998. if (buf == NULL)
  1999. return -1;
  2000. /* Advertisement Protocol IE */
  2001. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  2002. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  2003. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  2004. (pame_bi ? 0x80 : 0));
  2005. wpabuf_put_buf(buf, adv_proto);
  2006. /* GAS Query */
  2007. if (query) {
  2008. wpabuf_put_le16(buf, wpabuf_len(query));
  2009. wpabuf_put_buf(buf, query);
  2010. } else
  2011. wpabuf_put_le16(buf, 0);
  2012. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  2013. if (res < 0) {
  2014. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  2015. wpabuf_free(buf);
  2016. ret = -1;
  2017. } else
  2018. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  2019. "%u", res);
  2020. return ret;
  2021. }