interworking.c 78 KB

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