interworking.c 79 KB

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