interworking.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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. /* Only tunneled methods with username/password supported */
  438. return 0;
  439. }
  440. if (eap->method == EAP_TYPE_PEAP) {
  441. if (eap->inner_method &&
  442. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  443. return 0;
  444. if (!eap->inner_method &&
  445. eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
  446. return 0;
  447. }
  448. if (eap->method == EAP_TYPE_TTLS) {
  449. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  450. return 1; /* Assume TTLS/MSCHAPv2 is used */
  451. if (eap->inner_method &&
  452. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  453. return 0;
  454. if (eap->inner_non_eap &&
  455. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  456. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  457. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  458. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  459. return 0;
  460. }
  461. if (eap->inner_method &&
  462. eap->inner_method != EAP_TYPE_GTC &&
  463. eap->inner_method != EAP_TYPE_MSCHAPV2)
  464. return 0;
  465. return 1;
  466. }
  467. static int nai_realm_cred_cert(struct nai_realm_eap *eap)
  468. {
  469. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  470. return 0; /* method not supported */
  471. if (eap->method != EAP_TYPE_TLS) {
  472. /* Only EAP-TLS supported for credential authentication */
  473. return 0;
  474. }
  475. return 1;
  476. }
  477. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  478. struct nai_realm *realm)
  479. {
  480. u8 e;
  481. if (cred == NULL ||
  482. cred->username == NULL ||
  483. cred->username[0] == '\0' ||
  484. ((cred->password == NULL ||
  485. cred->password[0] == '\0') &&
  486. (cred->private_key == NULL ||
  487. cred->private_key[0] == '\0')))
  488. return NULL;
  489. for (e = 0; e < realm->eap_count; e++) {
  490. struct nai_realm_eap *eap = &realm->eap[e];
  491. if (cred->password && cred->password[0] &&
  492. nai_realm_cred_username(eap))
  493. return eap;
  494. if (cred->private_key && cred->private_key[0] &&
  495. nai_realm_cred_cert(eap))
  496. return eap;
  497. }
  498. return NULL;
  499. }
  500. #ifdef INTERWORKING_3GPP
  501. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  502. {
  503. u8 plmn[3], plmn2[3];
  504. const u8 *pos, *end;
  505. u8 udhl;
  506. /*
  507. * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
  508. * operator is allowed to include only two digits of the MNC, so allow
  509. * matches based on both two and three digit MNC assumptions. Since some
  510. * SIM/USIM cards may not expose MNC length conveniently, we may be
  511. * provided the default MNC length 3 here and as such, checking with MNC
  512. * length 2 is justifiable even though 3GPP TS 24.234 does not mention
  513. * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
  514. * with otherwise matching values would not be good idea in general, so
  515. * this should not result in selecting incorrect networks.
  516. */
  517. /* Match with 3 digit MNC */
  518. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  519. plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
  520. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  521. /* Match with 2 digit MNC */
  522. plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  523. plmn2[1] = (imsi[2] - '0') | 0xf0;
  524. plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  525. if (anqp == NULL)
  526. return 0;
  527. pos = wpabuf_head_u8(anqp);
  528. end = pos + wpabuf_len(anqp);
  529. if (pos + 2 > end)
  530. return 0;
  531. if (*pos != 0) {
  532. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  533. return 0;
  534. }
  535. pos++;
  536. udhl = *pos++;
  537. if (pos + udhl > end) {
  538. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  539. return 0;
  540. }
  541. end = pos + udhl;
  542. wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
  543. plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
  544. imsi, mnc_len);
  545. while (pos + 2 <= end) {
  546. u8 iei, len;
  547. const u8 *l_end;
  548. iei = *pos++;
  549. len = *pos++ & 0x7f;
  550. if (pos + len > end)
  551. break;
  552. l_end = pos + len;
  553. if (iei == 0 && len > 0) {
  554. /* PLMN List */
  555. u8 num, i;
  556. wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
  557. pos, len);
  558. num = *pos++;
  559. for (i = 0; i < num; i++) {
  560. if (pos + 3 > l_end)
  561. break;
  562. if (os_memcmp(pos, plmn, 3) == 0 ||
  563. os_memcmp(pos, plmn2, 3) == 0)
  564. return 1; /* Found matching PLMN */
  565. pos += 3;
  566. }
  567. } else {
  568. wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
  569. pos, len);
  570. }
  571. pos = l_end;
  572. }
  573. return 0;
  574. }
  575. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  576. size_t mnc_len, char prefix)
  577. {
  578. const char *sep, *msin;
  579. char *end, *pos;
  580. size_t msin_len, plmn_len;
  581. /*
  582. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  583. * Root NAI:
  584. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  585. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  586. */
  587. if (imsi == NULL || os_strlen(imsi) > 16) {
  588. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  589. return -1;
  590. }
  591. sep = os_strchr(imsi, '-');
  592. if (sep) {
  593. plmn_len = sep - imsi;
  594. msin = sep + 1;
  595. } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
  596. plmn_len = 3 + mnc_len;
  597. msin = imsi + plmn_len;
  598. } else
  599. return -1;
  600. if (plmn_len != 5 && plmn_len != 6)
  601. return -1;
  602. msin_len = os_strlen(msin);
  603. pos = nai;
  604. end = nai + nai_len;
  605. if (prefix)
  606. *pos++ = prefix;
  607. os_memcpy(pos, imsi, plmn_len);
  608. pos += plmn_len;
  609. os_memcpy(pos, msin, msin_len);
  610. pos += msin_len;
  611. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  612. if (plmn_len == 5) {
  613. *pos++ = '0';
  614. *pos++ = imsi[3];
  615. *pos++ = imsi[4];
  616. } else {
  617. *pos++ = imsi[3];
  618. *pos++ = imsi[4];
  619. *pos++ = imsi[5];
  620. }
  621. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  622. imsi[0], imsi[1], imsi[2]);
  623. return 0;
  624. }
  625. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  626. {
  627. char nai[100];
  628. if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
  629. return -1;
  630. return wpa_config_set_quoted(ssid, "identity", nai);
  631. }
  632. #endif /* INTERWORKING_3GPP */
  633. static int already_connected(struct wpa_supplicant *wpa_s,
  634. struct wpa_cred *cred, struct wpa_bss *bss)
  635. {
  636. struct wpa_ssid *ssid;
  637. if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
  638. return 0;
  639. ssid = wpa_s->current_ssid;
  640. if (ssid->parent_cred != cred)
  641. return 0;
  642. if (ssid->ssid_len != bss->ssid_len ||
  643. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  644. return 0;
  645. return 1;
  646. }
  647. static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
  648. struct wpa_cred *cred,
  649. struct wpa_bss *bss)
  650. {
  651. struct wpa_ssid *ssid;
  652. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  653. if (ssid->parent_cred != cred)
  654. continue;
  655. if (ssid->ssid_len != bss->ssid_len ||
  656. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  657. continue;
  658. break;
  659. }
  660. if (ssid == NULL)
  661. return;
  662. wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
  663. if (ssid == wpa_s->current_ssid) {
  664. wpa_sm_set_config(wpa_s->wpa, NULL);
  665. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  666. wpa_supplicant_deauthenticate(wpa_s,
  667. WLAN_REASON_DEAUTH_LEAVING);
  668. }
  669. wpas_notify_network_removed(wpa_s, ssid);
  670. wpa_config_remove_network(wpa_s->conf, ssid->id);
  671. }
  672. static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
  673. struct wpa_ssid *ssid)
  674. {
  675. if (wpa_config_set(ssid, "key_mgmt",
  676. wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
  677. "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
  678. return -1;
  679. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  680. return -1;
  681. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  682. return -1;
  683. return 0;
  684. }
  685. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  686. struct wpa_cred *cred,
  687. struct wpa_bss *bss)
  688. {
  689. #ifdef INTERWORKING_3GPP
  690. struct wpa_ssid *ssid;
  691. const u8 *ie;
  692. int eap_type;
  693. int res;
  694. char prefix;
  695. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  696. return -1;
  697. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  698. if (ie == NULL)
  699. return -1;
  700. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  701. MAC2STR(bss->bssid));
  702. if (already_connected(wpa_s, cred, bss)) {
  703. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  704. MAC2STR(bss->bssid));
  705. return 0;
  706. }
  707. remove_duplicate_network(wpa_s, cred, bss);
  708. ssid = wpa_config_add_network(wpa_s->conf);
  709. if (ssid == NULL)
  710. return -1;
  711. ssid->parent_cred = cred;
  712. wpas_notify_network_added(wpa_s, ssid);
  713. wpa_config_set_network_defaults(ssid);
  714. ssid->priority = cred->priority;
  715. ssid->temporary = 1;
  716. ssid->ssid = os_zalloc(ie[1] + 1);
  717. if (ssid->ssid == NULL)
  718. goto fail;
  719. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  720. ssid->ssid_len = ie[1];
  721. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  722. goto fail;
  723. eap_type = EAP_TYPE_SIM;
  724. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  725. eap_type = EAP_TYPE_AKA;
  726. if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
  727. if (cred->eap_method[0].method == EAP_TYPE_SIM ||
  728. cred->eap_method[0].method == EAP_TYPE_AKA ||
  729. cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
  730. eap_type = cred->eap_method[0].method;
  731. }
  732. switch (eap_type) {
  733. case EAP_TYPE_SIM:
  734. prefix = '1';
  735. res = wpa_config_set(ssid, "eap", "SIM", 0);
  736. break;
  737. case EAP_TYPE_AKA:
  738. prefix = '0';
  739. res = wpa_config_set(ssid, "eap", "AKA", 0);
  740. break;
  741. case EAP_TYPE_AKA_PRIME:
  742. prefix = '6';
  743. res = wpa_config_set(ssid, "eap", "AKA'", 0);
  744. break;
  745. default:
  746. res = -1;
  747. break;
  748. }
  749. if (res < 0) {
  750. wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
  751. eap_type);
  752. goto fail;
  753. }
  754. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
  755. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  756. goto fail;
  757. }
  758. if (cred->milenage && cred->milenage[0]) {
  759. if (wpa_config_set_quoted(ssid, "password",
  760. cred->milenage) < 0)
  761. goto fail;
  762. } else if (cred->pcsc) {
  763. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  764. goto fail;
  765. if (wpa_s->conf->pcsc_pin &&
  766. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  767. < 0)
  768. goto fail;
  769. }
  770. if (cred->password && cred->password[0] &&
  771. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  772. goto fail;
  773. wpa_config_update_prio_list(wpa_s->conf);
  774. interworking_reconnect(wpa_s);
  775. return 0;
  776. fail:
  777. wpas_notify_network_removed(wpa_s, ssid);
  778. wpa_config_remove_network(wpa_s->conf, ssid->id);
  779. #endif /* INTERWORKING_3GPP */
  780. return -1;
  781. }
  782. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  783. size_t rc_len)
  784. {
  785. const u8 *pos, *end;
  786. u8 lens;
  787. if (ie == NULL)
  788. return 0;
  789. pos = ie + 2;
  790. end = ie + 2 + ie[1];
  791. /* Roaming Consortium element:
  792. * Number of ANQP OIs
  793. * OI #1 and #2 lengths
  794. * OI #1, [OI #2], [OI #3]
  795. */
  796. if (pos + 2 > end)
  797. return 0;
  798. pos++; /* skip Number of ANQP OIs */
  799. lens = *pos++;
  800. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  801. return 0;
  802. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  803. return 1;
  804. pos += lens & 0x0f;
  805. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  806. return 1;
  807. pos += lens >> 4;
  808. if (pos < end && (size_t) (end - pos) == rc_len &&
  809. os_memcmp(pos, rc_id, rc_len) == 0)
  810. return 1;
  811. return 0;
  812. }
  813. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  814. const u8 *rc_id, size_t rc_len)
  815. {
  816. const u8 *pos, *end;
  817. u8 len;
  818. if (anqp == NULL)
  819. return 0;
  820. pos = wpabuf_head(anqp);
  821. end = pos + wpabuf_len(anqp);
  822. /* Set of <OI Length, OI> duples */
  823. while (pos < end) {
  824. len = *pos++;
  825. if (pos + len > end)
  826. break;
  827. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  828. return 1;
  829. pos += len;
  830. }
  831. return 0;
  832. }
  833. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  834. const u8 *rc_id, size_t rc_len)
  835. {
  836. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  837. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  838. }
  839. static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
  840. {
  841. const u8 *ie;
  842. if (cred->required_roaming_consortium_len == 0)
  843. return 0;
  844. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  845. if (ie == NULL &&
  846. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  847. return 1;
  848. return !roaming_consortium_match(ie,
  849. bss->anqp ?
  850. bss->anqp->roaming_consortium : NULL,
  851. cred->required_roaming_consortium,
  852. cred->required_roaming_consortium_len);
  853. }
  854. static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
  855. {
  856. size_t i;
  857. if (!cred->excluded_ssid)
  858. return 0;
  859. for (i = 0; i < cred->num_excluded_ssid; i++) {
  860. struct excluded_ssid *e = &cred->excluded_ssid[i];
  861. if (bss->ssid_len == e->ssid_len &&
  862. os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
  863. return 1;
  864. }
  865. return 0;
  866. }
  867. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  868. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  869. {
  870. struct wpa_cred *cred, *selected = NULL;
  871. const u8 *ie;
  872. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  873. if (ie == NULL &&
  874. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  875. return NULL;
  876. if (wpa_s->conf->cred == NULL)
  877. return NULL;
  878. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  879. if (cred->roaming_consortium_len == 0)
  880. continue;
  881. if (!roaming_consortium_match(ie,
  882. bss->anqp ?
  883. bss->anqp->roaming_consortium :
  884. NULL,
  885. cred->roaming_consortium,
  886. cred->roaming_consortium_len))
  887. continue;
  888. if (cred_excluded_ssid(cred, bss))
  889. continue;
  890. if (cred_no_required_oi_match(cred, bss))
  891. continue;
  892. if (selected == NULL ||
  893. selected->priority < cred->priority)
  894. selected = cred;
  895. }
  896. return selected;
  897. }
  898. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  899. struct wpa_cred *cred, int ttls)
  900. {
  901. if (cred->eap_method) {
  902. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  903. cred->eap_method->method == EAP_TYPE_TTLS;
  904. os_free(ssid->eap.eap_methods);
  905. ssid->eap.eap_methods =
  906. os_malloc(sizeof(struct eap_method_type) * 2);
  907. if (ssid->eap.eap_methods == NULL)
  908. return -1;
  909. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  910. sizeof(*cred->eap_method));
  911. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  912. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  913. }
  914. if (ttls && cred->username && cred->username[0]) {
  915. const char *pos;
  916. char *anon;
  917. /* Use anonymous NAI in Phase 1 */
  918. pos = os_strchr(cred->username, '@');
  919. if (pos) {
  920. size_t buflen = 9 + os_strlen(pos) + 1;
  921. anon = os_malloc(buflen);
  922. if (anon == NULL)
  923. return -1;
  924. os_snprintf(anon, buflen, "anonymous%s", pos);
  925. } else if (cred->realm) {
  926. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  927. anon = os_malloc(buflen);
  928. if (anon == NULL)
  929. return -1;
  930. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  931. } else {
  932. anon = os_strdup("anonymous");
  933. if (anon == NULL)
  934. return -1;
  935. }
  936. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  937. 0) {
  938. os_free(anon);
  939. return -1;
  940. }
  941. os_free(anon);
  942. }
  943. if (cred->username && cred->username[0] &&
  944. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  945. return -1;
  946. if (cred->password && cred->password[0]) {
  947. if (cred->ext_password &&
  948. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  949. return -1;
  950. if (!cred->ext_password &&
  951. wpa_config_set_quoted(ssid, "password", cred->password) <
  952. 0)
  953. return -1;
  954. }
  955. if (cred->client_cert && cred->client_cert[0] &&
  956. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  957. return -1;
  958. #ifdef ANDROID
  959. if (cred->private_key &&
  960. os_strncmp(cred->private_key, "keystore://", 11) == 0) {
  961. /* Use OpenSSL engine configuration for Android keystore */
  962. if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
  963. wpa_config_set_quoted(ssid, "key_id",
  964. cred->private_key + 11) < 0 ||
  965. wpa_config_set(ssid, "engine", "1", 0) < 0)
  966. return -1;
  967. } else
  968. #endif /* ANDROID */
  969. if (cred->private_key && cred->private_key[0] &&
  970. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  971. return -1;
  972. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  973. wpa_config_set_quoted(ssid, "private_key_passwd",
  974. cred->private_key_passwd) < 0)
  975. return -1;
  976. if (cred->phase1) {
  977. os_free(ssid->eap.phase1);
  978. ssid->eap.phase1 = os_strdup(cred->phase1);
  979. }
  980. if (cred->phase2) {
  981. os_free(ssid->eap.phase2);
  982. ssid->eap.phase2 = os_strdup(cred->phase2);
  983. }
  984. if (cred->ca_cert && cred->ca_cert[0] &&
  985. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  986. return -1;
  987. if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
  988. wpa_config_set_quoted(ssid, "domain_suffix_match",
  989. cred->domain_suffix_match) < 0)
  990. return -1;
  991. return 0;
  992. }
  993. static int interworking_connect_roaming_consortium(
  994. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  995. struct wpa_bss *bss, const u8 *ssid_ie)
  996. {
  997. struct wpa_ssid *ssid;
  998. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  999. "roaming consortium match", MAC2STR(bss->bssid));
  1000. if (already_connected(wpa_s, cred, bss)) {
  1001. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1002. MAC2STR(bss->bssid));
  1003. return 0;
  1004. }
  1005. remove_duplicate_network(wpa_s, cred, bss);
  1006. ssid = wpa_config_add_network(wpa_s->conf);
  1007. if (ssid == NULL)
  1008. return -1;
  1009. ssid->parent_cred = cred;
  1010. wpas_notify_network_added(wpa_s, ssid);
  1011. wpa_config_set_network_defaults(ssid);
  1012. ssid->priority = cred->priority;
  1013. ssid->temporary = 1;
  1014. ssid->ssid = os_zalloc(ssid_ie[1] + 1);
  1015. if (ssid->ssid == NULL)
  1016. goto fail;
  1017. os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
  1018. ssid->ssid_len = ssid_ie[1];
  1019. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1020. goto fail;
  1021. if (cred->eap_method == NULL) {
  1022. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  1023. "credential using roaming consortium");
  1024. goto fail;
  1025. }
  1026. if (interworking_set_eap_params(
  1027. ssid, cred,
  1028. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  1029. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  1030. goto fail;
  1031. wpa_config_update_prio_list(wpa_s->conf);
  1032. interworking_reconnect(wpa_s);
  1033. return 0;
  1034. fail:
  1035. wpas_notify_network_removed(wpa_s, ssid);
  1036. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1037. return -1;
  1038. }
  1039. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1040. {
  1041. struct wpa_cred *cred, *cred_rc, *cred_3gpp;
  1042. struct wpa_ssid *ssid;
  1043. struct nai_realm *realm;
  1044. struct nai_realm_eap *eap = NULL;
  1045. u16 count, i;
  1046. char buf[100];
  1047. const u8 *ie;
  1048. if (wpa_s->conf->cred == NULL || bss == NULL)
  1049. return -1;
  1050. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1051. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1052. wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
  1053. MACSTR, MAC2STR(bss->bssid));
  1054. return -1;
  1055. }
  1056. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  1057. if (ie == NULL || ie[1] == 0) {
  1058. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  1059. MACSTR, MAC2STR(bss->bssid));
  1060. return -1;
  1061. }
  1062. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1063. /*
  1064. * We currently support only HS 2.0 networks and those are
  1065. * required to use WPA2-Enterprise.
  1066. */
  1067. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  1068. "RSN");
  1069. return -1;
  1070. }
  1071. cred_rc = interworking_credentials_available_roaming_consortium(wpa_s,
  1072. bss);
  1073. if (cred_rc) {
  1074. wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
  1075. "consortium matching credential priority %d",
  1076. cred_rc->priority);
  1077. }
  1078. cred = interworking_credentials_available_realm(wpa_s, bss);
  1079. if (cred) {
  1080. wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
  1081. "matching credential priority %d",
  1082. cred->priority);
  1083. }
  1084. cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss);
  1085. if (cred_3gpp) {
  1086. wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
  1087. "credential priority %d", cred_3gpp->priority);
  1088. }
  1089. if (cred_rc &&
  1090. (cred == NULL || cred_rc->priority >= cred->priority) &&
  1091. (cred_3gpp == NULL || cred_rc->priority >= cred_3gpp->priority))
  1092. return interworking_connect_roaming_consortium(wpa_s, cred_rc,
  1093. bss, ie);
  1094. if (cred_3gpp &&
  1095. (cred == NULL || cred_3gpp->priority >= cred->priority)) {
  1096. return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
  1097. }
  1098. if (cred == NULL) {
  1099. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1100. "found for " MACSTR, MAC2STR(bss->bssid));
  1101. return -1;
  1102. }
  1103. realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
  1104. &count);
  1105. if (realm == NULL) {
  1106. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1107. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1108. return -1;
  1109. }
  1110. for (i = 0; i < count; i++) {
  1111. if (!nai_realm_match(&realm[i], cred->realm))
  1112. continue;
  1113. eap = nai_realm_find_eap(cred, &realm[i]);
  1114. if (eap)
  1115. break;
  1116. }
  1117. if (!eap) {
  1118. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1119. "and EAP method found for " MACSTR,
  1120. MAC2STR(bss->bssid));
  1121. nai_realm_free(realm, count);
  1122. return -1;
  1123. }
  1124. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  1125. MAC2STR(bss->bssid));
  1126. if (already_connected(wpa_s, cred, bss)) {
  1127. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1128. MAC2STR(bss->bssid));
  1129. nai_realm_free(realm, count);
  1130. return 0;
  1131. }
  1132. remove_duplicate_network(wpa_s, cred, bss);
  1133. ssid = wpa_config_add_network(wpa_s->conf);
  1134. if (ssid == NULL) {
  1135. nai_realm_free(realm, count);
  1136. return -1;
  1137. }
  1138. ssid->parent_cred = cred;
  1139. wpas_notify_network_added(wpa_s, ssid);
  1140. wpa_config_set_network_defaults(ssid);
  1141. ssid->priority = cred->priority;
  1142. ssid->temporary = 1;
  1143. ssid->ssid = os_zalloc(ie[1] + 1);
  1144. if (ssid->ssid == NULL)
  1145. goto fail;
  1146. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  1147. ssid->ssid_len = ie[1];
  1148. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1149. goto fail;
  1150. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  1151. eap->method), 0) < 0)
  1152. goto fail;
  1153. switch (eap->method) {
  1154. case EAP_TYPE_TTLS:
  1155. if (eap->inner_method) {
  1156. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  1157. eap_get_name(EAP_VENDOR_IETF,
  1158. eap->inner_method));
  1159. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1160. goto fail;
  1161. break;
  1162. }
  1163. switch (eap->inner_non_eap) {
  1164. case NAI_REALM_INNER_NON_EAP_PAP:
  1165. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  1166. 0)
  1167. goto fail;
  1168. break;
  1169. case NAI_REALM_INNER_NON_EAP_CHAP:
  1170. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1171. < 0)
  1172. goto fail;
  1173. break;
  1174. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1175. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1176. 0) < 0)
  1177. goto fail;
  1178. break;
  1179. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1180. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1181. 0) < 0)
  1182. goto fail;
  1183. break;
  1184. default:
  1185. /* EAP params were not set - assume TTLS/MSCHAPv2 */
  1186. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1187. 0) < 0)
  1188. goto fail;
  1189. break;
  1190. }
  1191. break;
  1192. case EAP_TYPE_PEAP:
  1193. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1194. eap_get_name(EAP_VENDOR_IETF,
  1195. eap->inner_method ?
  1196. eap->inner_method :
  1197. EAP_TYPE_MSCHAPV2));
  1198. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1199. goto fail;
  1200. break;
  1201. case EAP_TYPE_TLS:
  1202. break;
  1203. }
  1204. if (interworking_set_eap_params(ssid, cred,
  1205. eap->method == EAP_TYPE_TTLS) < 0)
  1206. goto fail;
  1207. nai_realm_free(realm, count);
  1208. wpa_config_update_prio_list(wpa_s->conf);
  1209. interworking_reconnect(wpa_s);
  1210. return 0;
  1211. fail:
  1212. wpas_notify_network_removed(wpa_s, ssid);
  1213. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1214. nai_realm_free(realm, count);
  1215. return -1;
  1216. }
  1217. static struct wpa_cred * interworking_credentials_available_3gpp(
  1218. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1219. {
  1220. struct wpa_cred *selected = NULL;
  1221. #ifdef INTERWORKING_3GPP
  1222. struct wpa_cred *cred;
  1223. int ret;
  1224. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  1225. return NULL;
  1226. #ifdef CONFIG_EAP_PROXY
  1227. if (!wpa_s->imsi[0]) {
  1228. size_t len;
  1229. wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
  1230. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
  1231. wpa_s->imsi,
  1232. &len);
  1233. if (wpa_s->mnc_len > 0) {
  1234. wpa_s->imsi[len] = '\0';
  1235. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  1236. wpa_s->imsi, wpa_s->mnc_len);
  1237. } else {
  1238. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  1239. }
  1240. }
  1241. #endif /* CONFIG_EAP_PROXY */
  1242. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1243. char *sep;
  1244. const char *imsi;
  1245. int mnc_len;
  1246. char imsi_buf[16];
  1247. size_t msin_len;
  1248. #ifdef PCSC_FUNCS
  1249. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1250. wpa_s->imsi[0]) {
  1251. imsi = wpa_s->imsi;
  1252. mnc_len = wpa_s->mnc_len;
  1253. goto compare;
  1254. }
  1255. #endif /* PCSC_FUNCS */
  1256. #ifdef CONFIG_EAP_PROXY
  1257. if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1258. imsi = wpa_s->imsi;
  1259. mnc_len = wpa_s->mnc_len;
  1260. goto compare;
  1261. }
  1262. #endif /* CONFIG_EAP_PROXY */
  1263. if (cred->imsi == NULL || !cred->imsi[0] ||
  1264. (!wpa_s->conf->external_sim &&
  1265. (cred->milenage == NULL || !cred->milenage[0])))
  1266. continue;
  1267. sep = os_strchr(cred->imsi, '-');
  1268. if (sep == NULL ||
  1269. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1270. continue;
  1271. mnc_len = sep - cred->imsi - 3;
  1272. os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
  1273. sep++;
  1274. msin_len = os_strlen(cred->imsi);
  1275. if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
  1276. msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
  1277. os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
  1278. imsi_buf[3 + mnc_len + msin_len] = '\0';
  1279. imsi = imsi_buf;
  1280. #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
  1281. compare:
  1282. #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
  1283. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1284. MACSTR, MAC2STR(bss->bssid));
  1285. ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
  1286. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1287. if (ret) {
  1288. if (cred_excluded_ssid(cred, bss))
  1289. continue;
  1290. if (cred_no_required_oi_match(cred, bss))
  1291. continue;
  1292. if (selected == NULL ||
  1293. selected->priority < cred->priority)
  1294. selected = cred;
  1295. }
  1296. }
  1297. #endif /* INTERWORKING_3GPP */
  1298. return selected;
  1299. }
  1300. static struct wpa_cred * interworking_credentials_available_realm(
  1301. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1302. {
  1303. struct wpa_cred *cred, *selected = NULL;
  1304. struct nai_realm *realm;
  1305. u16 count, i;
  1306. if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
  1307. return NULL;
  1308. if (wpa_s->conf->cred == NULL)
  1309. return NULL;
  1310. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1311. MACSTR, MAC2STR(bss->bssid));
  1312. realm = nai_realm_parse(bss->anqp->nai_realm, &count);
  1313. if (realm == NULL) {
  1314. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1315. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1316. return NULL;
  1317. }
  1318. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1319. if (cred->realm == NULL)
  1320. continue;
  1321. for (i = 0; i < count; i++) {
  1322. if (!nai_realm_match(&realm[i], cred->realm))
  1323. continue;
  1324. if (nai_realm_find_eap(cred, &realm[i])) {
  1325. if (cred_excluded_ssid(cred, bss))
  1326. continue;
  1327. if (cred_no_required_oi_match(cred, bss))
  1328. continue;
  1329. if (selected == NULL ||
  1330. selected->priority < cred->priority)
  1331. selected = cred;
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. nai_realm_free(realm, count);
  1337. return selected;
  1338. }
  1339. static struct wpa_cred * interworking_credentials_available(
  1340. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1341. {
  1342. struct wpa_cred *cred, *cred2;
  1343. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1344. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1345. wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
  1346. MACSTR, MAC2STR(bss->bssid));
  1347. return NULL;
  1348. }
  1349. cred = interworking_credentials_available_realm(wpa_s, bss);
  1350. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1351. if (cred && cred2 && cred2->priority >= cred->priority)
  1352. cred = cred2;
  1353. if (!cred)
  1354. cred = cred2;
  1355. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1356. bss);
  1357. if (cred && cred2 && cred2->priority >= cred->priority)
  1358. cred = cred2;
  1359. if (!cred)
  1360. cred = cred2;
  1361. return cred;
  1362. }
  1363. static int domain_name_list_contains(struct wpabuf *domain_names,
  1364. const char *domain)
  1365. {
  1366. const u8 *pos, *end;
  1367. size_t len;
  1368. len = os_strlen(domain);
  1369. pos = wpabuf_head(domain_names);
  1370. end = pos + wpabuf_len(domain_names);
  1371. while (pos + 1 < end) {
  1372. if (pos + 1 + pos[0] > end)
  1373. break;
  1374. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1375. pos + 1, pos[0]);
  1376. if (pos[0] == len &&
  1377. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1378. return 1;
  1379. pos += 1 + pos[0];
  1380. }
  1381. return 0;
  1382. }
  1383. int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
  1384. struct wpa_cred *cred,
  1385. struct wpabuf *domain_names)
  1386. {
  1387. size_t i;
  1388. int ret = -1;
  1389. #ifdef INTERWORKING_3GPP
  1390. char nai[100], *realm;
  1391. char *imsi = NULL;
  1392. int mnc_len = 0;
  1393. if (cred->imsi)
  1394. imsi = cred->imsi;
  1395. #ifdef CONFIG_PCSC
  1396. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1397. wpa_s->scard && wpa_s->imsi[0]) {
  1398. imsi = wpa_s->imsi;
  1399. mnc_len = wpa_s->mnc_len;
  1400. }
  1401. #endif /* CONFIG_PCSC */
  1402. #ifdef CONFIG_EAP_PROXY
  1403. else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1404. imsi = wpa_s->imsi;
  1405. mnc_len = wpa_s->mnc_len;
  1406. }
  1407. #endif /* CONFIG_EAP_PROXY */
  1408. if (domain_names &&
  1409. imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
  1410. realm = os_strchr(nai, '@');
  1411. if (realm)
  1412. realm++;
  1413. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1414. "with SIM/USIM domain %s", realm);
  1415. if (realm &&
  1416. domain_name_list_contains(domain_names, realm))
  1417. return 1;
  1418. if (realm)
  1419. ret = 0;
  1420. }
  1421. #endif /* INTERWORKING_3GPP */
  1422. if (domain_names == NULL || cred->domain == NULL)
  1423. return ret;
  1424. for (i = 0; i < cred->num_domain; i++) {
  1425. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1426. "home SP FQDN %s", cred->domain[i]);
  1427. if (domain_name_list_contains(domain_names, cred->domain[i]))
  1428. return 1;
  1429. }
  1430. return 0;
  1431. }
  1432. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1433. struct wpabuf *domain_names)
  1434. {
  1435. struct wpa_cred *cred;
  1436. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1437. return -1;
  1438. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1439. int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
  1440. if (res)
  1441. return res;
  1442. }
  1443. return 0;
  1444. }
  1445. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1446. {
  1447. struct wpa_bss *bss;
  1448. struct wpa_ssid *ssid;
  1449. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1450. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1451. if (wpas_network_disabled(wpa_s, ssid) ||
  1452. ssid->mode != WPAS_MODE_INFRA)
  1453. continue;
  1454. if (ssid->ssid_len != bss->ssid_len ||
  1455. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1456. 0)
  1457. continue;
  1458. /*
  1459. * TODO: Consider more accurate matching of security
  1460. * configuration similarly to what is done in events.c
  1461. */
  1462. return 1;
  1463. }
  1464. }
  1465. return 0;
  1466. }
  1467. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1468. {
  1469. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1470. int selected_prio = -999999, selected_home_prio = -999999;
  1471. unsigned int count = 0;
  1472. const char *type;
  1473. int res;
  1474. struct wpa_cred *cred;
  1475. wpa_s->network_select = 0;
  1476. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1477. cred = interworking_credentials_available(wpa_s, bss);
  1478. if (!cred)
  1479. continue;
  1480. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1481. /*
  1482. * We currently support only HS 2.0 networks and those
  1483. * are required to use WPA2-Enterprise.
  1484. */
  1485. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1486. "with " MACSTR " but network does not use "
  1487. "RSN", MAC2STR(bss->bssid));
  1488. continue;
  1489. }
  1490. count++;
  1491. res = interworking_home_sp(wpa_s, bss->anqp ?
  1492. bss->anqp->domain_name : NULL);
  1493. if (res > 0)
  1494. type = "home";
  1495. else if (res == 0)
  1496. type = "roaming";
  1497. else
  1498. type = "unknown";
  1499. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1500. MAC2STR(bss->bssid), type);
  1501. if (wpa_s->auto_select ||
  1502. (wpa_s->conf->auto_interworking &&
  1503. wpa_s->auto_network_select)) {
  1504. if (selected == NULL ||
  1505. cred->priority > selected_prio) {
  1506. selected = bss;
  1507. selected_prio = cred->priority;
  1508. }
  1509. if (res > 0 &&
  1510. (selected_home == NULL ||
  1511. cred->priority > selected_home_prio)) {
  1512. selected_home = bss;
  1513. selected_home_prio = cred->priority;
  1514. }
  1515. }
  1516. }
  1517. if (selected_home && selected_home != selected &&
  1518. selected_home_prio >= selected_prio) {
  1519. /* Prefer network operated by the Home SP */
  1520. selected = selected_home;
  1521. }
  1522. if (count == 0) {
  1523. /*
  1524. * No matching network was found based on configured
  1525. * credentials. Check whether any of the enabled network blocks
  1526. * have matching APs.
  1527. */
  1528. if (interworking_find_network_match(wpa_s)) {
  1529. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1530. "match for enabled network configurations");
  1531. if (wpa_s->auto_select)
  1532. interworking_reconnect(wpa_s);
  1533. return;
  1534. }
  1535. if (wpa_s->auto_network_select) {
  1536. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1537. "scanning after ANQP fetch");
  1538. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  1539. 0);
  1540. return;
  1541. }
  1542. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1543. "with matching credentials found");
  1544. }
  1545. if (selected)
  1546. interworking_connect(wpa_s, selected);
  1547. }
  1548. static struct wpa_bss_anqp *
  1549. interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1550. {
  1551. struct wpa_bss *other;
  1552. if (is_zero_ether_addr(bss->hessid))
  1553. return NULL; /* Cannot be in the same homegenous ESS */
  1554. dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
  1555. if (other == bss)
  1556. continue;
  1557. if (other->anqp == NULL)
  1558. continue;
  1559. if (other->anqp->roaming_consortium == NULL &&
  1560. other->anqp->nai_realm == NULL &&
  1561. other->anqp->anqp_3gpp == NULL &&
  1562. other->anqp->domain_name == NULL)
  1563. continue;
  1564. if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
  1565. continue;
  1566. if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
  1567. continue;
  1568. if (bss->ssid_len != other->ssid_len ||
  1569. os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
  1570. continue;
  1571. wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
  1572. "already fetched BSSID " MACSTR " and " MACSTR,
  1573. MAC2STR(other->bssid), MAC2STR(bss->bssid));
  1574. other->anqp->users++;
  1575. return other->anqp;
  1576. }
  1577. return NULL;
  1578. }
  1579. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1580. {
  1581. struct wpa_bss *bss;
  1582. int found = 0;
  1583. const u8 *ie;
  1584. if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
  1585. return;
  1586. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1587. if (!(bss->caps & IEEE80211_CAP_ESS))
  1588. continue;
  1589. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1590. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1591. continue; /* AP does not support Interworking */
  1592. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1593. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
  1594. continue; /* Disallowed BSS */
  1595. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1596. if (bss->anqp == NULL) {
  1597. bss->anqp = interworking_match_anqp_info(wpa_s,
  1598. bss);
  1599. if (bss->anqp) {
  1600. /* Shared data already fetched */
  1601. continue;
  1602. }
  1603. bss->anqp = wpa_bss_anqp_alloc();
  1604. if (bss->anqp == NULL)
  1605. break;
  1606. }
  1607. found++;
  1608. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1609. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1610. MACSTR, MAC2STR(bss->bssid));
  1611. interworking_anqp_send_req(wpa_s, bss);
  1612. break;
  1613. }
  1614. }
  1615. if (found == 0) {
  1616. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1617. wpa_s->fetch_anqp_in_progress = 0;
  1618. if (wpa_s->network_select)
  1619. interworking_select_network(wpa_s);
  1620. }
  1621. }
  1622. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1623. {
  1624. struct wpa_bss *bss;
  1625. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1626. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1627. wpa_s->fetch_anqp_in_progress = 1;
  1628. interworking_next_anqp_fetch(wpa_s);
  1629. }
  1630. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1631. {
  1632. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1633. return 0;
  1634. wpa_s->network_select = 0;
  1635. wpa_s->fetch_all_anqp = 1;
  1636. interworking_start_fetch_anqp(wpa_s);
  1637. return 0;
  1638. }
  1639. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1640. {
  1641. if (!wpa_s->fetch_anqp_in_progress)
  1642. return;
  1643. wpa_s->fetch_anqp_in_progress = 0;
  1644. }
  1645. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1646. u16 info_ids[], size_t num_ids)
  1647. {
  1648. struct wpabuf *buf;
  1649. int ret = 0;
  1650. int freq;
  1651. struct wpa_bss *bss;
  1652. int res;
  1653. freq = wpa_s->assoc_freq;
  1654. bss = wpa_bss_get_bssid(wpa_s, dst);
  1655. if (bss) {
  1656. wpa_bss_anqp_unshare_alloc(bss);
  1657. freq = bss->freq;
  1658. }
  1659. if (freq <= 0)
  1660. return -1;
  1661. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1662. MAC2STR(dst), (unsigned int) num_ids);
  1663. buf = anqp_build_req(info_ids, num_ids, NULL);
  1664. if (buf == NULL)
  1665. return -1;
  1666. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1667. if (res < 0) {
  1668. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1669. wpabuf_free(buf);
  1670. ret = -1;
  1671. } else
  1672. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1673. "%u", res);
  1674. return ret;
  1675. }
  1676. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1677. struct wpa_bss *bss, const u8 *sa,
  1678. u16 info_id,
  1679. const u8 *data, size_t slen)
  1680. {
  1681. const u8 *pos = data;
  1682. struct wpa_bss_anqp *anqp = NULL;
  1683. #ifdef CONFIG_HS20
  1684. u8 type;
  1685. #endif /* CONFIG_HS20 */
  1686. if (bss)
  1687. anqp = bss->anqp;
  1688. switch (info_id) {
  1689. case ANQP_CAPABILITY_LIST:
  1690. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1691. " ANQP Capability list", MAC2STR(sa));
  1692. break;
  1693. case ANQP_VENUE_NAME:
  1694. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1695. " Venue Name", MAC2STR(sa));
  1696. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1697. if (anqp) {
  1698. wpabuf_free(anqp->venue_name);
  1699. anqp->venue_name = wpabuf_alloc_copy(pos, slen);
  1700. }
  1701. break;
  1702. case ANQP_NETWORK_AUTH_TYPE:
  1703. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1704. " Network Authentication Type information",
  1705. MAC2STR(sa));
  1706. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1707. "Type", pos, slen);
  1708. if (anqp) {
  1709. wpabuf_free(anqp->network_auth_type);
  1710. anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
  1711. }
  1712. break;
  1713. case ANQP_ROAMING_CONSORTIUM:
  1714. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1715. " Roaming Consortium list", MAC2STR(sa));
  1716. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1717. pos, slen);
  1718. if (anqp) {
  1719. wpabuf_free(anqp->roaming_consortium);
  1720. anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
  1721. }
  1722. break;
  1723. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1724. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1725. " IP Address Type Availability information",
  1726. MAC2STR(sa));
  1727. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1728. pos, slen);
  1729. if (anqp) {
  1730. wpabuf_free(anqp->ip_addr_type_availability);
  1731. anqp->ip_addr_type_availability =
  1732. wpabuf_alloc_copy(pos, slen);
  1733. }
  1734. break;
  1735. case ANQP_NAI_REALM:
  1736. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1737. " NAI Realm list", MAC2STR(sa));
  1738. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1739. if (anqp) {
  1740. wpabuf_free(anqp->nai_realm);
  1741. anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
  1742. }
  1743. break;
  1744. case ANQP_3GPP_CELLULAR_NETWORK:
  1745. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1746. " 3GPP Cellular Network information", MAC2STR(sa));
  1747. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1748. pos, slen);
  1749. if (anqp) {
  1750. wpabuf_free(anqp->anqp_3gpp);
  1751. anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1752. }
  1753. break;
  1754. case ANQP_DOMAIN_NAME:
  1755. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1756. " Domain Name list", MAC2STR(sa));
  1757. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1758. if (anqp) {
  1759. wpabuf_free(anqp->domain_name);
  1760. anqp->domain_name = wpabuf_alloc_copy(pos, slen);
  1761. }
  1762. break;
  1763. case ANQP_VENDOR_SPECIFIC:
  1764. if (slen < 3)
  1765. return;
  1766. switch (WPA_GET_BE24(pos)) {
  1767. #ifdef CONFIG_HS20
  1768. case OUI_WFA:
  1769. pos += 3;
  1770. slen -= 3;
  1771. if (slen < 1)
  1772. return;
  1773. type = *pos++;
  1774. slen--;
  1775. switch (type) {
  1776. case HS20_ANQP_OUI_TYPE:
  1777. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1778. slen);
  1779. break;
  1780. default:
  1781. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1782. "vendor type %u", type);
  1783. break;
  1784. }
  1785. break;
  1786. #endif /* CONFIG_HS20 */
  1787. default:
  1788. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1789. "vendor-specific ANQP OUI %06x",
  1790. WPA_GET_BE24(pos));
  1791. return;
  1792. }
  1793. break;
  1794. default:
  1795. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1796. "%u", info_id);
  1797. break;
  1798. }
  1799. }
  1800. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1801. enum gas_query_result result,
  1802. const struct wpabuf *adv_proto,
  1803. const struct wpabuf *resp, u16 status_code)
  1804. {
  1805. struct wpa_supplicant *wpa_s = ctx;
  1806. const u8 *pos;
  1807. const u8 *end;
  1808. u16 info_id;
  1809. u16 slen;
  1810. struct wpa_bss *bss = NULL, *tmp;
  1811. if (result != GAS_QUERY_SUCCESS)
  1812. return;
  1813. pos = wpabuf_head(adv_proto);
  1814. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1815. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1816. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1817. "Protocol in response");
  1818. return;
  1819. }
  1820. /*
  1821. * If possible, select the BSS entry based on which BSS entry was used
  1822. * for the request. This can help in cases where multiple BSS entries
  1823. * may exist for the same AP.
  1824. */
  1825. dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
  1826. if (tmp == wpa_s->interworking_gas_bss &&
  1827. os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
  1828. bss = tmp;
  1829. break;
  1830. }
  1831. }
  1832. if (bss == NULL)
  1833. bss = wpa_bss_get_bssid(wpa_s, dst);
  1834. pos = wpabuf_head(resp);
  1835. end = pos + wpabuf_len(resp);
  1836. while (pos < end) {
  1837. if (pos + 4 > end) {
  1838. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1839. break;
  1840. }
  1841. info_id = WPA_GET_LE16(pos);
  1842. pos += 2;
  1843. slen = WPA_GET_LE16(pos);
  1844. pos += 2;
  1845. if (pos + slen > end) {
  1846. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1847. "for Info ID %u", info_id);
  1848. break;
  1849. }
  1850. interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
  1851. slen);
  1852. pos += slen;
  1853. }
  1854. }
  1855. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1856. struct wpa_scan_results *scan_res)
  1857. {
  1858. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1859. "ANQP fetch");
  1860. interworking_start_fetch_anqp(wpa_s);
  1861. }
  1862. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1863. {
  1864. interworking_stop_fetch_anqp(wpa_s);
  1865. wpa_s->network_select = 1;
  1866. wpa_s->auto_network_select = 0;
  1867. wpa_s->auto_select = !!auto_select;
  1868. wpa_s->fetch_all_anqp = 0;
  1869. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1870. "selection");
  1871. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1872. wpa_s->normal_scans = 0;
  1873. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1874. wpa_s->after_wps = 0;
  1875. wpa_s->known_wps_freq = 0;
  1876. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1877. return 0;
  1878. }
  1879. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1880. enum gas_query_result result,
  1881. const struct wpabuf *adv_proto,
  1882. const struct wpabuf *resp, u16 status_code)
  1883. {
  1884. struct wpa_supplicant *wpa_s = ctx;
  1885. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  1886. " dialog_token=%d status_code=%d resp_len=%d",
  1887. MAC2STR(addr), dialog_token, status_code,
  1888. resp ? (int) wpabuf_len(resp) : -1);
  1889. if (!resp)
  1890. return;
  1891. wpabuf_free(wpa_s->last_gas_resp);
  1892. wpa_s->last_gas_resp = wpabuf_dup(resp);
  1893. if (wpa_s->last_gas_resp == NULL)
  1894. return;
  1895. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  1896. wpa_s->last_gas_dialog_token = dialog_token;
  1897. }
  1898. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1899. const struct wpabuf *adv_proto,
  1900. const struct wpabuf *query)
  1901. {
  1902. struct wpabuf *buf;
  1903. int ret = 0;
  1904. int freq;
  1905. struct wpa_bss *bss;
  1906. int res;
  1907. size_t len;
  1908. u8 query_resp_len_limit = 0, pame_bi = 0;
  1909. freq = wpa_s->assoc_freq;
  1910. bss = wpa_bss_get_bssid(wpa_s, dst);
  1911. if (bss)
  1912. freq = bss->freq;
  1913. if (freq <= 0)
  1914. return -1;
  1915. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  1916. MAC2STR(dst), freq);
  1917. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  1918. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  1919. len = 3 + wpabuf_len(adv_proto) + 2;
  1920. if (query)
  1921. len += wpabuf_len(query);
  1922. buf = gas_build_initial_req(0, len);
  1923. if (buf == NULL)
  1924. return -1;
  1925. /* Advertisement Protocol IE */
  1926. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1927. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  1928. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  1929. (pame_bi ? 0x80 : 0));
  1930. wpabuf_put_buf(buf, adv_proto);
  1931. /* GAS Query */
  1932. if (query) {
  1933. wpabuf_put_le16(buf, wpabuf_len(query));
  1934. wpabuf_put_buf(buf, query);
  1935. } else
  1936. wpabuf_put_le16(buf, 0);
  1937. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  1938. if (res < 0) {
  1939. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  1940. wpabuf_free(buf);
  1941. ret = -1;
  1942. } else
  1943. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  1944. "%u", res);
  1945. return ret;
  1946. }