interworking.c 63 KB

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