interworking.c 79 KB

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