interworking.c 68 KB

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