interworking.c 56 KB

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