interworking.c 73 KB

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