interworking.c 78 KB

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