interworking.c 64 KB

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