interworking.c 77 KB

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