interworking.c 49 KB

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