interworking.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. /*
  2. * Interworking (IEEE 802.11u)
  3. * Copyright (c) 2011-2012, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "common/gas.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "utils/pcsc_funcs.h"
  14. #include "utils/eloop.h"
  15. #include "drivers/driver.h"
  16. #include "eap_common/eap_defs.h"
  17. #include "eap_peer/eap.h"
  18. #include "eap_peer/eap_methods.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "config.h"
  21. #include "config_ssid.h"
  22. #include "bss.h"
  23. #include "scan.h"
  24. #include "notify.h"
  25. #include "gas_query.h"
  26. #include "hs20_supplicant.h"
  27. #include "interworking.h"
  28. #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
  29. #define INTERWORKING_3GPP
  30. #else
  31. #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
  32. #define INTERWORKING_3GPP
  33. #else
  34. #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
  35. #define INTERWORKING_3GPP
  36. #endif
  37. #endif
  38. #endif
  39. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  40. static void interworking_reconnect(struct wpa_supplicant *wpa_s)
  41. {
  42. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  43. wpa_supplicant_cancel_sched_scan(wpa_s);
  44. wpa_supplicant_deauthenticate(wpa_s,
  45. WLAN_REASON_DEAUTH_LEAVING);
  46. }
  47. wpa_s->disconnected = 0;
  48. wpa_s->reassociate = 1;
  49. if (wpa_s->last_scan_res_used > 0) {
  50. struct os_time now;
  51. os_get_time(&now);
  52. if (now.sec - wpa_s->last_scan.sec <= 5) {
  53. wpa_printf(MSG_DEBUG, "Interworking: Old scan results "
  54. "are fresh - connect without new scan");
  55. if (wpas_select_network_from_last_scan(wpa_s) >= 0)
  56. return;
  57. }
  58. }
  59. wpa_supplicant_req_scan(wpa_s, 0, 0);
  60. }
  61. static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
  62. struct wpabuf *extra)
  63. {
  64. struct wpabuf *buf;
  65. size_t i;
  66. u8 *len_pos;
  67. buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
  68. (extra ? wpabuf_len(extra) : 0));
  69. if (buf == NULL)
  70. return NULL;
  71. len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
  72. for (i = 0; i < num_ids; i++)
  73. wpabuf_put_le16(buf, info_ids[i]);
  74. gas_anqp_set_element_len(buf, len_pos);
  75. if (extra)
  76. wpabuf_put_buf(buf, extra);
  77. gas_anqp_set_len(buf);
  78. return buf;
  79. }
  80. static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
  81. u8 dialog_token,
  82. enum gas_query_result result,
  83. const struct wpabuf *adv_proto,
  84. const struct wpabuf *resp,
  85. u16 status_code)
  86. {
  87. struct wpa_supplicant *wpa_s = ctx;
  88. anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
  89. status_code);
  90. interworking_next_anqp_fetch(wpa_s);
  91. }
  92. static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
  93. {
  94. struct wpa_cred *cred;
  95. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  96. if (cred->roaming_consortium_len)
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
  102. {
  103. struct wpa_cred *cred;
  104. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  105. if (cred->pcsc || cred->imsi)
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
  111. {
  112. struct wpa_cred *cred;
  113. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  114. if (cred->pcsc || cred->imsi)
  115. continue;
  116. if (!cred->eap_method)
  117. return 1;
  118. if (cred->realm && cred->roaming_consortium_len == 0)
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. static int cred_with_domain(struct wpa_supplicant *wpa_s)
  124. {
  125. struct wpa_cred *cred;
  126. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  127. if (cred->domain || cred->pcsc || cred->imsi)
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. static int additional_roaming_consortiums(struct wpa_bss *bss)
  133. {
  134. const u8 *ie;
  135. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  136. if (ie == NULL || ie[1] == 0)
  137. return 0;
  138. return ie[2]; /* Number of ANQP OIs */
  139. }
  140. static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
  141. {
  142. struct wpa_supplicant *wpa_s = eloop_ctx;
  143. interworking_next_anqp_fetch(wpa_s);
  144. }
  145. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  146. struct wpa_bss *bss)
  147. {
  148. struct wpabuf *buf;
  149. int ret = 0;
  150. int res;
  151. u16 info_ids[8];
  152. size_t num_info_ids = 0;
  153. struct wpabuf *extra = NULL;
  154. int all = wpa_s->fetch_all_anqp;
  155. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  156. MAC2STR(bss->bssid));
  157. info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
  158. if (all) {
  159. info_ids[num_info_ids++] = ANQP_VENUE_NAME;
  160. info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
  161. }
  162. if (all || (cred_with_roaming_consortium(wpa_s) &&
  163. additional_roaming_consortiums(bss)))
  164. info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
  165. if (all)
  166. info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
  167. if (all || cred_with_nai_realm(wpa_s))
  168. info_ids[num_info_ids++] = ANQP_NAI_REALM;
  169. if (all || cred_with_3gpp(wpa_s))
  170. info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
  171. if (all || cred_with_domain(wpa_s))
  172. info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
  173. wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
  174. (u8 *) info_ids, num_info_ids * 2);
  175. #ifdef CONFIG_HS20
  176. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  177. u8 *len_pos;
  178. extra = wpabuf_alloc(100);
  179. if (!extra)
  180. return -1;
  181. len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
  182. wpabuf_put_be24(extra, OUI_WFA);
  183. wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
  184. wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
  185. wpabuf_put_u8(extra, 0); /* Reserved */
  186. wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
  187. if (all) {
  188. wpabuf_put_u8(extra,
  189. HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  190. wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
  191. wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
  192. wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
  193. }
  194. gas_anqp_set_element_len(extra, len_pos);
  195. }
  196. #endif /* CONFIG_HS20 */
  197. buf = anqp_build_req(info_ids, num_info_ids, extra);
  198. wpabuf_free(extra);
  199. if (buf == NULL)
  200. return -1;
  201. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  202. interworking_anqp_resp_cb, wpa_s);
  203. if (res < 0) {
  204. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  205. ret = -1;
  206. eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
  207. NULL);
  208. } else
  209. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  210. "%u", res);
  211. wpabuf_free(buf);
  212. return ret;
  213. }
  214. struct nai_realm_eap {
  215. u8 method;
  216. u8 inner_method;
  217. enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
  218. u8 cred_type;
  219. u8 tunneled_cred_type;
  220. };
  221. struct nai_realm {
  222. u8 encoding;
  223. char *realm;
  224. u8 eap_count;
  225. struct nai_realm_eap *eap;
  226. };
  227. static void nai_realm_free(struct nai_realm *realms, u16 count)
  228. {
  229. u16 i;
  230. if (realms == NULL)
  231. return;
  232. for (i = 0; i < count; i++) {
  233. os_free(realms[i].eap);
  234. os_free(realms[i].realm);
  235. }
  236. os_free(realms);
  237. }
  238. static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
  239. const u8 *end)
  240. {
  241. u8 elen, auth_count, a;
  242. const u8 *e_end;
  243. if (pos + 3 > end) {
  244. wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
  245. return NULL;
  246. }
  247. elen = *pos++;
  248. if (pos + elen > end || elen < 2) {
  249. wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
  250. return NULL;
  251. }
  252. e_end = pos + elen;
  253. e->method = *pos++;
  254. auth_count = *pos++;
  255. wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
  256. elen, e->method, auth_count);
  257. for (a = 0; a < auth_count; a++) {
  258. u8 id, len;
  259. if (pos + 2 > end || pos + 2 + pos[1] > end) {
  260. wpa_printf(MSG_DEBUG, "No room for Authentication "
  261. "Parameter subfield");
  262. return NULL;
  263. }
  264. id = *pos++;
  265. len = *pos++;
  266. switch (id) {
  267. case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
  268. if (len < 1)
  269. break;
  270. e->inner_non_eap = *pos;
  271. if (e->method != EAP_TYPE_TTLS)
  272. break;
  273. switch (*pos) {
  274. case NAI_REALM_INNER_NON_EAP_PAP:
  275. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
  276. break;
  277. case NAI_REALM_INNER_NON_EAP_CHAP:
  278. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
  279. break;
  280. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  281. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
  282. break;
  283. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  284. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
  285. break;
  286. }
  287. break;
  288. case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
  289. if (len < 1)
  290. break;
  291. e->inner_method = *pos;
  292. wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
  293. e->inner_method);
  294. break;
  295. case NAI_REALM_EAP_AUTH_CRED_TYPE:
  296. if (len < 1)
  297. break;
  298. e->cred_type = *pos;
  299. wpa_printf(MSG_DEBUG, "Credential Type: %u",
  300. e->cred_type);
  301. break;
  302. case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
  303. if (len < 1)
  304. break;
  305. e->tunneled_cred_type = *pos;
  306. wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
  307. "Type: %u", e->tunneled_cred_type);
  308. break;
  309. default:
  310. wpa_printf(MSG_DEBUG, "Unsupported Authentication "
  311. "Parameter: id=%u len=%u", id, len);
  312. wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
  313. "Value", pos, len);
  314. break;
  315. }
  316. pos += len;
  317. }
  318. return e_end;
  319. }
  320. static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
  321. const u8 *end)
  322. {
  323. u16 len;
  324. const u8 *f_end;
  325. u8 realm_len, e;
  326. if (end - pos < 4) {
  327. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  328. "fixed fields");
  329. return NULL;
  330. }
  331. len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
  332. pos += 2;
  333. if (pos + len > end || len < 3) {
  334. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  335. "(len=%u; left=%u)",
  336. len, (unsigned int) (end - pos));
  337. return NULL;
  338. }
  339. f_end = pos + len;
  340. r->encoding = *pos++;
  341. realm_len = *pos++;
  342. if (pos + realm_len > f_end) {
  343. wpa_printf(MSG_DEBUG, "No room for NAI Realm "
  344. "(len=%u; left=%u)",
  345. realm_len, (unsigned int) (f_end - pos));
  346. return NULL;
  347. }
  348. wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
  349. r->realm = os_malloc(realm_len + 1);
  350. if (r->realm == NULL)
  351. return NULL;
  352. os_memcpy(r->realm, pos, realm_len);
  353. r->realm[realm_len] = '\0';
  354. pos += realm_len;
  355. if (pos + 1 > f_end) {
  356. wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
  357. return NULL;
  358. }
  359. r->eap_count = *pos++;
  360. wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
  361. if (pos + r->eap_count * 3 > f_end) {
  362. wpa_printf(MSG_DEBUG, "No room for EAP Methods");
  363. return NULL;
  364. }
  365. r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
  366. if (r->eap == NULL)
  367. return NULL;
  368. for (e = 0; e < r->eap_count; e++) {
  369. pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
  370. if (pos == NULL)
  371. return NULL;
  372. }
  373. return f_end;
  374. }
  375. static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
  376. {
  377. struct nai_realm *realm;
  378. const u8 *pos, *end;
  379. u16 i, num;
  380. if (anqp == NULL || wpabuf_len(anqp) < 2)
  381. return NULL;
  382. pos = wpabuf_head_u8(anqp);
  383. end = pos + wpabuf_len(anqp);
  384. num = WPA_GET_LE16(pos);
  385. wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
  386. pos += 2;
  387. if (num * 5 > end - pos) {
  388. wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
  389. "enough data (%u octets) for that many realms",
  390. num, (unsigned int) (end - pos));
  391. return NULL;
  392. }
  393. realm = os_calloc(num, sizeof(struct nai_realm));
  394. if (realm == NULL)
  395. return NULL;
  396. for (i = 0; i < num; i++) {
  397. pos = nai_realm_parse_realm(&realm[i], pos, end);
  398. if (pos == NULL) {
  399. nai_realm_free(realm, num);
  400. return NULL;
  401. }
  402. }
  403. *count = num;
  404. return realm;
  405. }
  406. static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
  407. {
  408. char *tmp, *pos, *end;
  409. int match = 0;
  410. if (realm->realm == NULL || home_realm == NULL)
  411. return 0;
  412. if (os_strchr(realm->realm, ';') == NULL)
  413. return os_strcasecmp(realm->realm, home_realm) == 0;
  414. tmp = os_strdup(realm->realm);
  415. if (tmp == NULL)
  416. return 0;
  417. pos = tmp;
  418. while (*pos) {
  419. end = os_strchr(pos, ';');
  420. if (end)
  421. *end = '\0';
  422. if (os_strcasecmp(pos, home_realm) == 0) {
  423. match = 1;
  424. break;
  425. }
  426. if (end == NULL)
  427. break;
  428. pos = end + 1;
  429. }
  430. os_free(tmp);
  431. return match;
  432. }
  433. static int nai_realm_cred_username(struct nai_realm_eap *eap)
  434. {
  435. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  436. return 0; /* method not supported */
  437. if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
  438. /* Only tunneled methods with username/password supported */
  439. return 0;
  440. }
  441. if (eap->method == EAP_TYPE_PEAP) {
  442. if (eap->inner_method &&
  443. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  444. return 0;
  445. if (!eap->inner_method &&
  446. eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
  447. return 0;
  448. }
  449. if (eap->method == EAP_TYPE_TTLS) {
  450. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  451. return 1; /* Assume TTLS/MSCHAPv2 is used */
  452. if (eap->inner_method &&
  453. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  454. return 0;
  455. if (eap->inner_non_eap &&
  456. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  457. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  458. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  459. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  460. return 0;
  461. }
  462. if (eap->inner_method &&
  463. eap->inner_method != EAP_TYPE_GTC &&
  464. eap->inner_method != EAP_TYPE_MSCHAPV2)
  465. return 0;
  466. return 1;
  467. }
  468. static int nai_realm_cred_cert(struct nai_realm_eap *eap)
  469. {
  470. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  471. return 0; /* method not supported */
  472. if (eap->method != EAP_TYPE_TLS) {
  473. /* Only EAP-TLS supported for credential authentication */
  474. return 0;
  475. }
  476. return 1;
  477. }
  478. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  479. struct nai_realm *realm)
  480. {
  481. u8 e;
  482. if (cred == NULL ||
  483. cred->username == NULL ||
  484. cred->username[0] == '\0' ||
  485. ((cred->password == NULL ||
  486. cred->password[0] == '\0') &&
  487. (cred->private_key == NULL ||
  488. cred->private_key[0] == '\0')))
  489. return NULL;
  490. for (e = 0; e < realm->eap_count; e++) {
  491. struct nai_realm_eap *eap = &realm->eap[e];
  492. if (cred->password && cred->password[0] &&
  493. nai_realm_cred_username(eap))
  494. return eap;
  495. if (cred->private_key && cred->private_key[0] &&
  496. nai_realm_cred_cert(eap))
  497. return eap;
  498. }
  499. return NULL;
  500. }
  501. #ifdef INTERWORKING_3GPP
  502. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  503. {
  504. u8 plmn[3];
  505. const u8 *pos, *end;
  506. u8 udhl;
  507. /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
  508. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  509. plmn[1] = imsi[2] - '0';
  510. /* default to MNC length 3 if unknown */
  511. if (mnc_len != 2)
  512. plmn[1] |= (imsi[5] - '0') << 4;
  513. else
  514. plmn[1] |= 0xf0;
  515. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  516. if (anqp == NULL)
  517. return 0;
  518. pos = wpabuf_head_u8(anqp);
  519. end = pos + wpabuf_len(anqp);
  520. if (pos + 2 > end)
  521. return 0;
  522. if (*pos != 0) {
  523. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  524. return 0;
  525. }
  526. pos++;
  527. udhl = *pos++;
  528. if (pos + udhl > end) {
  529. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  530. return 0;
  531. }
  532. end = pos + udhl;
  533. while (pos + 2 <= end) {
  534. u8 iei, len;
  535. const u8 *l_end;
  536. iei = *pos++;
  537. len = *pos++ & 0x7f;
  538. if (pos + len > end)
  539. break;
  540. l_end = pos + len;
  541. if (iei == 0 && len > 0) {
  542. /* PLMN List */
  543. u8 num, i;
  544. num = *pos++;
  545. for (i = 0; i < num; i++) {
  546. if (pos + 3 > end)
  547. break;
  548. if (os_memcmp(pos, plmn, 3) == 0)
  549. return 1; /* Found matching PLMN */
  550. pos += 3;
  551. }
  552. }
  553. pos = l_end;
  554. }
  555. return 0;
  556. }
  557. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  558. size_t mnc_len, char prefix)
  559. {
  560. const char *sep, *msin;
  561. char *end, *pos;
  562. size_t msin_len, plmn_len;
  563. /*
  564. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  565. * Root NAI:
  566. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  567. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  568. */
  569. if (imsi == NULL || os_strlen(imsi) > 16) {
  570. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  571. return -1;
  572. }
  573. sep = os_strchr(imsi, '-');
  574. if (sep) {
  575. plmn_len = sep - imsi;
  576. msin = sep + 1;
  577. } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
  578. plmn_len = 3 + mnc_len;
  579. msin = imsi + plmn_len;
  580. } else
  581. return -1;
  582. if (plmn_len != 5 && plmn_len != 6)
  583. return -1;
  584. msin_len = os_strlen(msin);
  585. pos = nai;
  586. end = nai + nai_len;
  587. if (prefix)
  588. *pos++ = prefix;
  589. os_memcpy(pos, imsi, plmn_len);
  590. pos += plmn_len;
  591. os_memcpy(pos, msin, msin_len);
  592. pos += msin_len;
  593. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  594. if (plmn_len == 5) {
  595. *pos++ = '0';
  596. *pos++ = imsi[3];
  597. *pos++ = imsi[4];
  598. } else {
  599. *pos++ = imsi[3];
  600. *pos++ = imsi[4];
  601. *pos++ = imsi[5];
  602. }
  603. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  604. imsi[0], imsi[1], imsi[2]);
  605. return 0;
  606. }
  607. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  608. {
  609. char nai[100];
  610. if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
  611. return -1;
  612. return wpa_config_set_quoted(ssid, "identity", nai);
  613. }
  614. #endif /* INTERWORKING_3GPP */
  615. static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
  616. struct wpa_ssid *ssid)
  617. {
  618. if (wpa_config_set(ssid, "key_mgmt",
  619. wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
  620. "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
  621. return -1;
  622. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  623. return -1;
  624. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  625. return -1;
  626. return 0;
  627. }
  628. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  629. struct wpa_bss *bss)
  630. {
  631. #ifdef INTERWORKING_3GPP
  632. struct wpa_cred *cred;
  633. struct wpa_ssid *ssid;
  634. const u8 *ie;
  635. int eap_type;
  636. int res;
  637. char prefix;
  638. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  639. return -1;
  640. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  641. char *sep;
  642. const char *imsi;
  643. int mnc_len;
  644. #ifdef PCSC_FUNCS
  645. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  646. wpa_s->imsi[0]) {
  647. imsi = wpa_s->imsi;
  648. mnc_len = wpa_s->mnc_len;
  649. goto compare;
  650. }
  651. #endif /* PCSC_FUNCS */
  652. if (cred->imsi == NULL || !cred->imsi[0] ||
  653. cred->milenage == NULL || !cred->milenage[0])
  654. continue;
  655. sep = os_strchr(cred->imsi, '-');
  656. if (sep == NULL ||
  657. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  658. continue;
  659. mnc_len = sep - cred->imsi - 3;
  660. imsi = cred->imsi;
  661. #ifdef PCSC_FUNCS
  662. compare:
  663. #endif /* PCSC_FUNCS */
  664. if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
  665. break;
  666. }
  667. if (cred == NULL)
  668. return -1;
  669. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  670. if (ie == NULL)
  671. return -1;
  672. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  673. MAC2STR(bss->bssid));
  674. ssid = wpa_config_add_network(wpa_s->conf);
  675. if (ssid == NULL)
  676. return -1;
  677. ssid->parent_cred = cred;
  678. wpas_notify_network_added(wpa_s, ssid);
  679. wpa_config_set_network_defaults(ssid);
  680. ssid->priority = cred->priority;
  681. ssid->temporary = 1;
  682. ssid->ssid = os_zalloc(ie[1] + 1);
  683. if (ssid->ssid == NULL)
  684. goto fail;
  685. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  686. ssid->ssid_len = ie[1];
  687. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  688. goto fail;
  689. eap_type = EAP_TYPE_SIM;
  690. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  691. eap_type = EAP_TYPE_AKA;
  692. if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
  693. if (cred->eap_method[0].method == EAP_TYPE_SIM ||
  694. cred->eap_method[0].method == EAP_TYPE_AKA ||
  695. cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
  696. eap_type = cred->eap_method[0].method;
  697. }
  698. switch (eap_type) {
  699. case EAP_TYPE_SIM:
  700. prefix = '1';
  701. res = wpa_config_set(ssid, "eap", "SIM", 0);
  702. break;
  703. case EAP_TYPE_AKA:
  704. prefix = '0';
  705. res = wpa_config_set(ssid, "eap", "AKA", 0);
  706. break;
  707. case EAP_TYPE_AKA_PRIME:
  708. prefix = '6';
  709. res = wpa_config_set(ssid, "eap", "AKA'", 0);
  710. break;
  711. default:
  712. res = -1;
  713. break;
  714. }
  715. if (res < 0) {
  716. wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
  717. eap_type);
  718. goto fail;
  719. }
  720. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
  721. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  722. goto fail;
  723. }
  724. if (cred->milenage && cred->milenage[0]) {
  725. if (wpa_config_set_quoted(ssid, "password",
  726. cred->milenage) < 0)
  727. goto fail;
  728. } else if (cred->pcsc) {
  729. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  730. goto fail;
  731. if (wpa_s->conf->pcsc_pin &&
  732. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  733. < 0)
  734. goto fail;
  735. }
  736. if (cred->password && cred->password[0] &&
  737. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  738. goto fail;
  739. wpa_config_update_prio_list(wpa_s->conf);
  740. interworking_reconnect(wpa_s);
  741. return 0;
  742. fail:
  743. wpas_notify_network_removed(wpa_s, ssid);
  744. wpa_config_remove_network(wpa_s->conf, ssid->id);
  745. #endif /* INTERWORKING_3GPP */
  746. return -1;
  747. }
  748. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  749. size_t rc_len)
  750. {
  751. const u8 *pos, *end;
  752. u8 lens;
  753. if (ie == NULL)
  754. return 0;
  755. pos = ie + 2;
  756. end = ie + 2 + ie[1];
  757. /* Roaming Consortium element:
  758. * Number of ANQP OIs
  759. * OI #1 and #2 lengths
  760. * OI #1, [OI #2], [OI #3]
  761. */
  762. if (pos + 2 > end)
  763. return 0;
  764. pos++; /* skip Number of ANQP OIs */
  765. lens = *pos++;
  766. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  767. return 0;
  768. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  769. return 1;
  770. pos += lens & 0x0f;
  771. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  772. return 1;
  773. pos += lens >> 4;
  774. if (pos < end && (size_t) (end - pos) == rc_len &&
  775. os_memcmp(pos, rc_id, rc_len) == 0)
  776. return 1;
  777. return 0;
  778. }
  779. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  780. const u8 *rc_id, size_t rc_len)
  781. {
  782. const u8 *pos, *end;
  783. u8 len;
  784. if (anqp == NULL)
  785. return 0;
  786. pos = wpabuf_head(anqp);
  787. end = pos + wpabuf_len(anqp);
  788. /* Set of <OI Length, OI> duples */
  789. while (pos < end) {
  790. len = *pos++;
  791. if (pos + len > end)
  792. break;
  793. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  794. return 1;
  795. pos += len;
  796. }
  797. return 0;
  798. }
  799. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  800. const u8 *rc_id, size_t rc_len)
  801. {
  802. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  803. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  804. }
  805. static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
  806. {
  807. size_t i;
  808. if (!cred->excluded_ssid)
  809. return 0;
  810. for (i = 0; i < cred->num_excluded_ssid; i++) {
  811. struct excluded_ssid *e = &cred->excluded_ssid[i];
  812. if (bss->ssid_len == e->ssid_len &&
  813. os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  819. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  820. {
  821. struct wpa_cred *cred, *selected = NULL;
  822. const u8 *ie;
  823. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  824. if (ie == NULL &&
  825. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  826. return NULL;
  827. if (wpa_s->conf->cred == NULL)
  828. return NULL;
  829. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  830. if (cred->roaming_consortium_len == 0)
  831. continue;
  832. if (!roaming_consortium_match(ie,
  833. bss->anqp ?
  834. bss->anqp->roaming_consortium :
  835. NULL,
  836. cred->roaming_consortium,
  837. cred->roaming_consortium_len))
  838. continue;
  839. if (cred_excluded_ssid(cred, bss))
  840. continue;
  841. if (selected == NULL ||
  842. selected->priority < cred->priority)
  843. selected = cred;
  844. }
  845. return selected;
  846. }
  847. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  848. struct wpa_cred *cred, int ttls)
  849. {
  850. if (cred->eap_method) {
  851. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  852. cred->eap_method->method == EAP_TYPE_TTLS;
  853. os_free(ssid->eap.eap_methods);
  854. ssid->eap.eap_methods =
  855. os_malloc(sizeof(struct eap_method_type) * 2);
  856. if (ssid->eap.eap_methods == NULL)
  857. return -1;
  858. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  859. sizeof(*cred->eap_method));
  860. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  861. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  862. }
  863. if (ttls && cred->username && cred->username[0]) {
  864. const char *pos;
  865. char *anon;
  866. /* Use anonymous NAI in Phase 1 */
  867. pos = os_strchr(cred->username, '@');
  868. if (pos) {
  869. size_t buflen = 9 + os_strlen(pos) + 1;
  870. anon = os_malloc(buflen);
  871. if (anon == NULL)
  872. return -1;
  873. os_snprintf(anon, buflen, "anonymous%s", pos);
  874. } else if (cred->realm) {
  875. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  876. anon = os_malloc(buflen);
  877. if (anon == NULL)
  878. return -1;
  879. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  880. } else {
  881. anon = os_strdup("anonymous");
  882. if (anon == NULL)
  883. return -1;
  884. }
  885. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  886. 0) {
  887. os_free(anon);
  888. return -1;
  889. }
  890. os_free(anon);
  891. }
  892. if (cred->username && cred->username[0] &&
  893. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  894. return -1;
  895. if (cred->password && cred->password[0]) {
  896. if (cred->ext_password &&
  897. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  898. return -1;
  899. if (!cred->ext_password &&
  900. wpa_config_set_quoted(ssid, "password", cred->password) <
  901. 0)
  902. return -1;
  903. }
  904. if (cred->client_cert && cred->client_cert[0] &&
  905. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  906. return -1;
  907. #ifdef ANDROID
  908. if (cred->private_key &&
  909. os_strncmp(cred->private_key, "keystore://", 11) == 0) {
  910. /* Use OpenSSL engine configuration for Android keystore */
  911. if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
  912. wpa_config_set_quoted(ssid, "key_id",
  913. cred->private_key + 11) < 0 ||
  914. wpa_config_set(ssid, "engine", "1", 0) < 0)
  915. return -1;
  916. } else
  917. #endif /* ANDROID */
  918. if (cred->private_key && cred->private_key[0] &&
  919. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  920. return -1;
  921. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  922. wpa_config_set_quoted(ssid, "private_key_passwd",
  923. cred->private_key_passwd) < 0)
  924. return -1;
  925. if (cred->phase1) {
  926. os_free(ssid->eap.phase1);
  927. ssid->eap.phase1 = os_strdup(cred->phase1);
  928. }
  929. if (cred->phase2) {
  930. os_free(ssid->eap.phase2);
  931. ssid->eap.phase2 = os_strdup(cred->phase2);
  932. }
  933. if (cred->ca_cert && cred->ca_cert[0] &&
  934. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  935. return -1;
  936. return 0;
  937. }
  938. static int interworking_connect_roaming_consortium(
  939. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  940. struct wpa_bss *bss, const u8 *ssid_ie)
  941. {
  942. struct wpa_ssid *ssid;
  943. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  944. "roaming consortium match", MAC2STR(bss->bssid));
  945. ssid = wpa_config_add_network(wpa_s->conf);
  946. if (ssid == NULL)
  947. return -1;
  948. ssid->parent_cred = cred;
  949. wpas_notify_network_added(wpa_s, ssid);
  950. wpa_config_set_network_defaults(ssid);
  951. ssid->priority = cred->priority;
  952. ssid->temporary = 1;
  953. ssid->ssid = os_zalloc(ssid_ie[1] + 1);
  954. if (ssid->ssid == NULL)
  955. goto fail;
  956. os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
  957. ssid->ssid_len = ssid_ie[1];
  958. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  959. goto fail;
  960. if (cred->eap_method == NULL) {
  961. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  962. "credential using roaming consortium");
  963. goto fail;
  964. }
  965. if (interworking_set_eap_params(
  966. ssid, cred,
  967. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  968. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  969. goto fail;
  970. wpa_config_update_prio_list(wpa_s->conf);
  971. interworking_reconnect(wpa_s);
  972. return 0;
  973. fail:
  974. wpas_notify_network_removed(wpa_s, ssid);
  975. wpa_config_remove_network(wpa_s->conf, ssid->id);
  976. return -1;
  977. }
  978. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  979. {
  980. struct wpa_cred *cred;
  981. struct wpa_ssid *ssid;
  982. struct nai_realm *realm;
  983. struct nai_realm_eap *eap = NULL;
  984. u16 count, i;
  985. char buf[100];
  986. const u8 *ie;
  987. if (wpa_s->conf->cred == NULL || bss == NULL)
  988. return -1;
  989. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  990. if (ie == NULL || ie[1] == 0) {
  991. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  992. MACSTR, MAC2STR(bss->bssid));
  993. return -1;
  994. }
  995. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  996. /*
  997. * We currently support only HS 2.0 networks and those are
  998. * required to use WPA2-Enterprise.
  999. */
  1000. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  1001. "RSN");
  1002. return -1;
  1003. }
  1004. cred = interworking_credentials_available_roaming_consortium(wpa_s,
  1005. bss);
  1006. if (cred)
  1007. return interworking_connect_roaming_consortium(wpa_s, cred,
  1008. bss, ie);
  1009. realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
  1010. &count);
  1011. if (realm == NULL) {
  1012. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1013. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1014. count = 0;
  1015. }
  1016. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1017. for (i = 0; i < count; i++) {
  1018. if (!nai_realm_match(&realm[i], cred->realm))
  1019. continue;
  1020. eap = nai_realm_find_eap(cred, &realm[i]);
  1021. if (eap)
  1022. break;
  1023. }
  1024. if (eap)
  1025. break;
  1026. }
  1027. if (!eap) {
  1028. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  1029. if (realm)
  1030. nai_realm_free(realm, count);
  1031. return 0;
  1032. }
  1033. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1034. "and EAP method found for " MACSTR,
  1035. MAC2STR(bss->bssid));
  1036. nai_realm_free(realm, count);
  1037. return -1;
  1038. }
  1039. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  1040. MAC2STR(bss->bssid));
  1041. ssid = wpa_config_add_network(wpa_s->conf);
  1042. if (ssid == NULL) {
  1043. nai_realm_free(realm, count);
  1044. return -1;
  1045. }
  1046. ssid->parent_cred = cred;
  1047. wpas_notify_network_added(wpa_s, ssid);
  1048. wpa_config_set_network_defaults(ssid);
  1049. ssid->priority = cred->priority;
  1050. ssid->temporary = 1;
  1051. ssid->ssid = os_zalloc(ie[1] + 1);
  1052. if (ssid->ssid == NULL)
  1053. goto fail;
  1054. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  1055. ssid->ssid_len = ie[1];
  1056. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1057. goto fail;
  1058. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  1059. eap->method), 0) < 0)
  1060. goto fail;
  1061. switch (eap->method) {
  1062. case EAP_TYPE_TTLS:
  1063. if (eap->inner_method) {
  1064. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  1065. eap_get_name(EAP_VENDOR_IETF,
  1066. eap->inner_method));
  1067. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1068. goto fail;
  1069. break;
  1070. }
  1071. switch (eap->inner_non_eap) {
  1072. case NAI_REALM_INNER_NON_EAP_PAP:
  1073. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  1074. 0)
  1075. goto fail;
  1076. break;
  1077. case NAI_REALM_INNER_NON_EAP_CHAP:
  1078. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1079. < 0)
  1080. goto fail;
  1081. break;
  1082. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1083. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1084. 0) < 0)
  1085. goto fail;
  1086. break;
  1087. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1088. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1089. 0) < 0)
  1090. goto fail;
  1091. break;
  1092. default:
  1093. /* EAP params were not set - assume TTLS/MSCHAPv2 */
  1094. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1095. 0) < 0)
  1096. goto fail;
  1097. break;
  1098. }
  1099. break;
  1100. case EAP_TYPE_PEAP:
  1101. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1102. eap_get_name(EAP_VENDOR_IETF,
  1103. eap->inner_method ?
  1104. eap->inner_method :
  1105. EAP_TYPE_MSCHAPV2));
  1106. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1107. goto fail;
  1108. break;
  1109. case EAP_TYPE_TLS:
  1110. break;
  1111. }
  1112. if (interworking_set_eap_params(ssid, cred,
  1113. eap->method == EAP_TYPE_TTLS) < 0)
  1114. goto fail;
  1115. nai_realm_free(realm, count);
  1116. wpa_config_update_prio_list(wpa_s->conf);
  1117. interworking_reconnect(wpa_s);
  1118. return 0;
  1119. fail:
  1120. wpas_notify_network_removed(wpa_s, ssid);
  1121. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1122. nai_realm_free(realm, count);
  1123. return -1;
  1124. }
  1125. static struct wpa_cred * interworking_credentials_available_3gpp(
  1126. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1127. {
  1128. struct wpa_cred *cred, *selected = NULL;
  1129. int ret;
  1130. #ifdef INTERWORKING_3GPP
  1131. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  1132. return NULL;
  1133. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1134. char *sep;
  1135. const char *imsi;
  1136. int mnc_len;
  1137. #ifdef PCSC_FUNCS
  1138. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1139. wpa_s->imsi[0]) {
  1140. imsi = wpa_s->imsi;
  1141. mnc_len = wpa_s->mnc_len;
  1142. goto compare;
  1143. }
  1144. #endif /* PCSC_FUNCS */
  1145. if (cred->imsi == NULL || !cred->imsi[0] ||
  1146. cred->milenage == NULL || !cred->milenage[0])
  1147. continue;
  1148. sep = os_strchr(cred->imsi, '-');
  1149. if (sep == NULL ||
  1150. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1151. continue;
  1152. mnc_len = sep - cred->imsi - 3;
  1153. imsi = cred->imsi;
  1154. #ifdef PCSC_FUNCS
  1155. compare:
  1156. #endif /* PCSC_FUNCS */
  1157. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1158. MACSTR, MAC2STR(bss->bssid));
  1159. ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
  1160. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1161. if (ret) {
  1162. if (cred_excluded_ssid(cred, bss))
  1163. continue;
  1164. if (selected == NULL ||
  1165. selected->priority < cred->priority)
  1166. selected = cred;
  1167. }
  1168. }
  1169. #endif /* INTERWORKING_3GPP */
  1170. return selected;
  1171. }
  1172. static struct wpa_cred * interworking_credentials_available_realm(
  1173. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1174. {
  1175. struct wpa_cred *cred, *selected = NULL;
  1176. struct nai_realm *realm;
  1177. u16 count, i;
  1178. if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
  1179. return NULL;
  1180. if (wpa_s->conf->cred == NULL)
  1181. return NULL;
  1182. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1183. MACSTR, MAC2STR(bss->bssid));
  1184. realm = nai_realm_parse(bss->anqp->nai_realm, &count);
  1185. if (realm == NULL) {
  1186. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1187. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1188. return NULL;
  1189. }
  1190. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1191. if (cred->realm == NULL)
  1192. continue;
  1193. for (i = 0; i < count; i++) {
  1194. if (!nai_realm_match(&realm[i], cred->realm))
  1195. continue;
  1196. if (nai_realm_find_eap(cred, &realm[i])) {
  1197. if (cred_excluded_ssid(cred, bss))
  1198. continue;
  1199. if (selected == NULL ||
  1200. selected->priority < cred->priority)
  1201. selected = cred;
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. nai_realm_free(realm, count);
  1207. return selected;
  1208. }
  1209. static struct wpa_cred * interworking_credentials_available(
  1210. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1211. {
  1212. struct wpa_cred *cred, *cred2;
  1213. cred = interworking_credentials_available_realm(wpa_s, bss);
  1214. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1215. if (cred && cred2 && cred2->priority >= cred->priority)
  1216. cred = cred2;
  1217. if (!cred)
  1218. cred = cred2;
  1219. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1220. bss);
  1221. if (cred && cred2 && cred2->priority >= cred->priority)
  1222. cred = cred2;
  1223. if (!cred)
  1224. cred = cred2;
  1225. return cred;
  1226. }
  1227. static int domain_name_list_contains(struct wpabuf *domain_names,
  1228. const char *domain)
  1229. {
  1230. const u8 *pos, *end;
  1231. size_t len;
  1232. len = os_strlen(domain);
  1233. pos = wpabuf_head(domain_names);
  1234. end = pos + wpabuf_len(domain_names);
  1235. while (pos + 1 < end) {
  1236. if (pos + 1 + pos[0] > end)
  1237. break;
  1238. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1239. pos + 1, pos[0]);
  1240. if (pos[0] == len &&
  1241. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1242. return 1;
  1243. pos += 1 + pos[0];
  1244. }
  1245. return 0;
  1246. }
  1247. int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
  1248. struct wpa_cred *cred,
  1249. struct wpabuf *domain_names)
  1250. {
  1251. #ifdef INTERWORKING_3GPP
  1252. char nai[100], *realm;
  1253. char *imsi = NULL;
  1254. int mnc_len = 0;
  1255. if (cred->imsi)
  1256. imsi = cred->imsi;
  1257. #ifdef CONFIG_PCSC
  1258. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1259. wpa_s->scard && wpa_s->imsi[0]) {
  1260. imsi = wpa_s->imsi;
  1261. mnc_len = wpa_s->mnc_len;
  1262. }
  1263. #endif /* CONFIG_PCSC */
  1264. if (domain_names &&
  1265. imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
  1266. realm = os_strchr(nai, '@');
  1267. if (realm)
  1268. realm++;
  1269. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1270. "with SIM/USIM domain %s", realm);
  1271. if (realm &&
  1272. domain_name_list_contains(domain_names, realm))
  1273. return 1;
  1274. }
  1275. #endif /* INTERWORKING_3GPP */
  1276. if (domain_names == NULL || cred->domain == NULL)
  1277. return 0;
  1278. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1279. "home SP FQDN %s", cred->domain);
  1280. if (domain_name_list_contains(domain_names, cred->domain))
  1281. return 1;
  1282. return 0;
  1283. }
  1284. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1285. struct wpabuf *domain_names)
  1286. {
  1287. struct wpa_cred *cred;
  1288. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1289. return -1;
  1290. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1291. int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
  1292. if (res)
  1293. return res;
  1294. }
  1295. return 0;
  1296. }
  1297. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1298. {
  1299. struct wpa_bss *bss;
  1300. struct wpa_ssid *ssid;
  1301. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1302. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1303. if (wpas_network_disabled(wpa_s, ssid) ||
  1304. ssid->mode != WPAS_MODE_INFRA)
  1305. continue;
  1306. if (ssid->ssid_len != bss->ssid_len ||
  1307. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1308. 0)
  1309. continue;
  1310. /*
  1311. * TODO: Consider more accurate matching of security
  1312. * configuration similarly to what is done in events.c
  1313. */
  1314. return 1;
  1315. }
  1316. }
  1317. return 0;
  1318. }
  1319. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1320. {
  1321. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1322. int selected_prio = -999999, selected_home_prio = -999999;
  1323. unsigned int count = 0;
  1324. const char *type;
  1325. int res;
  1326. struct wpa_cred *cred;
  1327. wpa_s->network_select = 0;
  1328. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1329. cred = interworking_credentials_available(wpa_s, bss);
  1330. if (!cred)
  1331. continue;
  1332. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1333. /*
  1334. * We currently support only HS 2.0 networks and those
  1335. * are required to use WPA2-Enterprise.
  1336. */
  1337. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1338. "with " MACSTR " but network does not use "
  1339. "RSN", MAC2STR(bss->bssid));
  1340. continue;
  1341. }
  1342. count++;
  1343. res = interworking_home_sp(wpa_s, bss->anqp ?
  1344. bss->anqp->domain_name : NULL);
  1345. if (res > 0)
  1346. type = "home";
  1347. else if (res == 0)
  1348. type = "roaming";
  1349. else
  1350. type = "unknown";
  1351. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1352. MAC2STR(bss->bssid), type);
  1353. if (wpa_s->auto_select ||
  1354. (wpa_s->conf->auto_interworking &&
  1355. wpa_s->auto_network_select)) {
  1356. if (selected == NULL ||
  1357. cred->priority > selected_prio) {
  1358. selected = bss;
  1359. selected_prio = cred->priority;
  1360. }
  1361. if (res > 0 &&
  1362. (selected_home == NULL ||
  1363. cred->priority > selected_home_prio)) {
  1364. selected_home = bss;
  1365. selected_home_prio = cred->priority;
  1366. }
  1367. }
  1368. }
  1369. if (selected_home && selected_home != selected &&
  1370. selected_home_prio >= selected_prio) {
  1371. /* Prefer network operated by the Home SP */
  1372. selected = selected_home;
  1373. }
  1374. if (count == 0) {
  1375. /*
  1376. * No matching network was found based on configured
  1377. * credentials. Check whether any of the enabled network blocks
  1378. * have matching APs.
  1379. */
  1380. if (interworking_find_network_match(wpa_s)) {
  1381. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1382. "match for enabled network configurations");
  1383. if (wpa_s->auto_select)
  1384. interworking_reconnect(wpa_s);
  1385. return;
  1386. }
  1387. if (wpa_s->auto_network_select) {
  1388. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1389. "scanning after ANQP fetch");
  1390. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  1391. 0);
  1392. return;
  1393. }
  1394. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1395. "with matching credentials found");
  1396. }
  1397. if (selected)
  1398. interworking_connect(wpa_s, selected);
  1399. }
  1400. static struct wpa_bss_anqp *
  1401. interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1402. {
  1403. struct wpa_bss *other;
  1404. if (is_zero_ether_addr(bss->hessid))
  1405. return NULL; /* Cannot be in the same homegenous ESS */
  1406. dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
  1407. if (other == bss)
  1408. continue;
  1409. if (other->anqp == NULL)
  1410. continue;
  1411. if (other->anqp->roaming_consortium == NULL &&
  1412. other->anqp->nai_realm == NULL &&
  1413. other->anqp->anqp_3gpp == NULL &&
  1414. other->anqp->domain_name == NULL)
  1415. continue;
  1416. if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
  1417. continue;
  1418. if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
  1419. continue;
  1420. if (bss->ssid_len != other->ssid_len ||
  1421. os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
  1422. continue;
  1423. wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
  1424. "already fetched BSSID " MACSTR " and " MACSTR,
  1425. MAC2STR(other->bssid), MAC2STR(bss->bssid));
  1426. other->anqp->users++;
  1427. return other->anqp;
  1428. }
  1429. return NULL;
  1430. }
  1431. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1432. {
  1433. struct wpa_bss *bss;
  1434. int found = 0;
  1435. const u8 *ie;
  1436. if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress)
  1437. return;
  1438. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1439. if (!(bss->caps & IEEE80211_CAP_ESS))
  1440. continue;
  1441. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1442. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1443. continue; /* AP does not support Interworking */
  1444. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1445. if (bss->anqp == NULL) {
  1446. bss->anqp = interworking_match_anqp_info(wpa_s,
  1447. bss);
  1448. if (bss->anqp) {
  1449. /* Shared data already fetched */
  1450. continue;
  1451. }
  1452. bss->anqp = wpa_bss_anqp_alloc();
  1453. if (bss->anqp == NULL)
  1454. break;
  1455. }
  1456. found++;
  1457. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1458. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1459. MACSTR, MAC2STR(bss->bssid));
  1460. interworking_anqp_send_req(wpa_s, bss);
  1461. break;
  1462. }
  1463. }
  1464. if (found == 0) {
  1465. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1466. wpa_s->fetch_anqp_in_progress = 0;
  1467. if (wpa_s->network_select)
  1468. interworking_select_network(wpa_s);
  1469. }
  1470. }
  1471. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1472. {
  1473. struct wpa_bss *bss;
  1474. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1475. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1476. wpa_s->fetch_anqp_in_progress = 1;
  1477. interworking_next_anqp_fetch(wpa_s);
  1478. }
  1479. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1480. {
  1481. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1482. return 0;
  1483. wpa_s->network_select = 0;
  1484. wpa_s->fetch_all_anqp = 1;
  1485. interworking_start_fetch_anqp(wpa_s);
  1486. return 0;
  1487. }
  1488. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1489. {
  1490. if (!wpa_s->fetch_anqp_in_progress)
  1491. return;
  1492. wpa_s->fetch_anqp_in_progress = 0;
  1493. }
  1494. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1495. u16 info_ids[], size_t num_ids)
  1496. {
  1497. struct wpabuf *buf;
  1498. int ret = 0;
  1499. int freq;
  1500. struct wpa_bss *bss;
  1501. int res;
  1502. freq = wpa_s->assoc_freq;
  1503. bss = wpa_bss_get_bssid(wpa_s, dst);
  1504. if (bss) {
  1505. wpa_bss_anqp_unshare_alloc(bss);
  1506. freq = bss->freq;
  1507. }
  1508. if (freq <= 0)
  1509. return -1;
  1510. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1511. MAC2STR(dst), (unsigned int) num_ids);
  1512. buf = anqp_build_req(info_ids, num_ids, NULL);
  1513. if (buf == NULL)
  1514. return -1;
  1515. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1516. if (res < 0) {
  1517. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1518. ret = -1;
  1519. } else
  1520. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1521. "%u", res);
  1522. wpabuf_free(buf);
  1523. return ret;
  1524. }
  1525. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1526. const u8 *sa, u16 info_id,
  1527. const u8 *data, size_t slen)
  1528. {
  1529. const u8 *pos = data;
  1530. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1531. struct wpa_bss_anqp *anqp = NULL;
  1532. #ifdef CONFIG_HS20
  1533. u8 type;
  1534. #endif /* CONFIG_HS20 */
  1535. if (bss)
  1536. anqp = bss->anqp;
  1537. switch (info_id) {
  1538. case ANQP_CAPABILITY_LIST:
  1539. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1540. " ANQP Capability list", MAC2STR(sa));
  1541. break;
  1542. case ANQP_VENUE_NAME:
  1543. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1544. " Venue Name", MAC2STR(sa));
  1545. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1546. if (anqp) {
  1547. wpabuf_free(anqp->venue_name);
  1548. anqp->venue_name = wpabuf_alloc_copy(pos, slen);
  1549. }
  1550. break;
  1551. case ANQP_NETWORK_AUTH_TYPE:
  1552. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1553. " Network Authentication Type information",
  1554. MAC2STR(sa));
  1555. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1556. "Type", pos, slen);
  1557. if (anqp) {
  1558. wpabuf_free(anqp->network_auth_type);
  1559. anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
  1560. }
  1561. break;
  1562. case ANQP_ROAMING_CONSORTIUM:
  1563. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1564. " Roaming Consortium list", MAC2STR(sa));
  1565. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1566. pos, slen);
  1567. if (anqp) {
  1568. wpabuf_free(anqp->roaming_consortium);
  1569. anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
  1570. }
  1571. break;
  1572. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1573. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1574. " IP Address Type Availability information",
  1575. MAC2STR(sa));
  1576. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1577. pos, slen);
  1578. if (anqp) {
  1579. wpabuf_free(anqp->ip_addr_type_availability);
  1580. anqp->ip_addr_type_availability =
  1581. wpabuf_alloc_copy(pos, slen);
  1582. }
  1583. break;
  1584. case ANQP_NAI_REALM:
  1585. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1586. " NAI Realm list", MAC2STR(sa));
  1587. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1588. if (anqp) {
  1589. wpabuf_free(anqp->nai_realm);
  1590. anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
  1591. }
  1592. break;
  1593. case ANQP_3GPP_CELLULAR_NETWORK:
  1594. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1595. " 3GPP Cellular Network information", MAC2STR(sa));
  1596. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1597. pos, slen);
  1598. if (anqp) {
  1599. wpabuf_free(anqp->anqp_3gpp);
  1600. anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1601. }
  1602. break;
  1603. case ANQP_DOMAIN_NAME:
  1604. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1605. " Domain Name list", MAC2STR(sa));
  1606. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1607. if (anqp) {
  1608. wpabuf_free(anqp->domain_name);
  1609. anqp->domain_name = wpabuf_alloc_copy(pos, slen);
  1610. }
  1611. break;
  1612. case ANQP_VENDOR_SPECIFIC:
  1613. if (slen < 3)
  1614. return;
  1615. switch (WPA_GET_BE24(pos)) {
  1616. #ifdef CONFIG_HS20
  1617. case OUI_WFA:
  1618. pos += 3;
  1619. slen -= 3;
  1620. if (slen < 1)
  1621. return;
  1622. type = *pos++;
  1623. slen--;
  1624. switch (type) {
  1625. case HS20_ANQP_OUI_TYPE:
  1626. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1627. slen);
  1628. break;
  1629. default:
  1630. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1631. "vendor type %u", type);
  1632. break;
  1633. }
  1634. break;
  1635. #endif /* CONFIG_HS20 */
  1636. default:
  1637. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1638. "vendor-specific ANQP OUI %06x",
  1639. WPA_GET_BE24(pos));
  1640. return;
  1641. }
  1642. break;
  1643. default:
  1644. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1645. "%u", info_id);
  1646. break;
  1647. }
  1648. }
  1649. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1650. enum gas_query_result result,
  1651. const struct wpabuf *adv_proto,
  1652. const struct wpabuf *resp, u16 status_code)
  1653. {
  1654. struct wpa_supplicant *wpa_s = ctx;
  1655. const u8 *pos;
  1656. const u8 *end;
  1657. u16 info_id;
  1658. u16 slen;
  1659. if (result != GAS_QUERY_SUCCESS)
  1660. return;
  1661. pos = wpabuf_head(adv_proto);
  1662. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1663. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1664. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1665. "Protocol in response");
  1666. return;
  1667. }
  1668. pos = wpabuf_head(resp);
  1669. end = pos + wpabuf_len(resp);
  1670. while (pos < end) {
  1671. if (pos + 4 > end) {
  1672. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1673. break;
  1674. }
  1675. info_id = WPA_GET_LE16(pos);
  1676. pos += 2;
  1677. slen = WPA_GET_LE16(pos);
  1678. pos += 2;
  1679. if (pos + slen > end) {
  1680. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1681. "for Info ID %u", info_id);
  1682. break;
  1683. }
  1684. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1685. slen);
  1686. pos += slen;
  1687. }
  1688. }
  1689. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1690. struct wpa_scan_results *scan_res)
  1691. {
  1692. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1693. "ANQP fetch");
  1694. interworking_start_fetch_anqp(wpa_s);
  1695. }
  1696. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1697. {
  1698. interworking_stop_fetch_anqp(wpa_s);
  1699. wpa_s->network_select = 1;
  1700. wpa_s->auto_network_select = 0;
  1701. wpa_s->auto_select = !!auto_select;
  1702. wpa_s->fetch_all_anqp = 0;
  1703. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1704. "selection");
  1705. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1706. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1707. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1708. return 0;
  1709. }
  1710. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1711. enum gas_query_result result,
  1712. const struct wpabuf *adv_proto,
  1713. const struct wpabuf *resp, u16 status_code)
  1714. {
  1715. struct wpa_supplicant *wpa_s = ctx;
  1716. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  1717. " dialog_token=%d status_code=%d resp_len=%d",
  1718. MAC2STR(addr), dialog_token, status_code,
  1719. resp ? (int) wpabuf_len(resp) : -1);
  1720. if (!resp)
  1721. return;
  1722. wpabuf_free(wpa_s->last_gas_resp);
  1723. wpa_s->last_gas_resp = wpabuf_dup(resp);
  1724. if (wpa_s->last_gas_resp == NULL)
  1725. return;
  1726. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  1727. wpa_s->last_gas_dialog_token = dialog_token;
  1728. }
  1729. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1730. const struct wpabuf *adv_proto,
  1731. const struct wpabuf *query)
  1732. {
  1733. struct wpabuf *buf;
  1734. int ret = 0;
  1735. int freq;
  1736. struct wpa_bss *bss;
  1737. int res;
  1738. size_t len;
  1739. u8 query_resp_len_limit = 0, pame_bi = 0;
  1740. freq = wpa_s->assoc_freq;
  1741. bss = wpa_bss_get_bssid(wpa_s, dst);
  1742. if (bss)
  1743. freq = bss->freq;
  1744. if (freq <= 0)
  1745. return -1;
  1746. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  1747. MAC2STR(dst), freq);
  1748. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  1749. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  1750. len = 3 + wpabuf_len(adv_proto) + 2;
  1751. if (query)
  1752. len += wpabuf_len(query);
  1753. buf = gas_build_initial_req(0, len);
  1754. if (buf == NULL)
  1755. return -1;
  1756. /* Advertisement Protocol IE */
  1757. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1758. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  1759. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  1760. (pame_bi ? 0x80 : 0));
  1761. wpabuf_put_buf(buf, adv_proto);
  1762. /* GAS Query */
  1763. if (query) {
  1764. wpabuf_put_le16(buf, wpabuf_len(query));
  1765. wpabuf_put_buf(buf, query);
  1766. } else
  1767. wpabuf_put_le16(buf, 0);
  1768. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  1769. if (res < 0) {
  1770. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  1771. ret = -1;
  1772. } else
  1773. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  1774. "%u", res);
  1775. wpabuf_free(buf);
  1776. return ret;
  1777. }