interworking.c 79 KB

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