interworking.c 54 KB

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