interworking.c 71 KB

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