interworking.c 56 KB

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