interworking.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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_methods.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "config.h"
  19. #include "config_ssid.h"
  20. #include "bss.h"
  21. #include "scan.h"
  22. #include "notify.h"
  23. #include "gas_query.h"
  24. #include "hs20_supplicant.h"
  25. #include "interworking.h"
  26. #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
  27. #define INTERWORKING_3GPP
  28. #else
  29. #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
  30. #define INTERWORKING_3GPP
  31. #else
  32. #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
  33. #define INTERWORKING_3GPP
  34. #endif
  35. #endif
  36. #endif
  37. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  38. static void interworking_reconnect(struct wpa_supplicant *wpa_s)
  39. {
  40. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  41. wpa_supplicant_cancel_sched_scan(wpa_s);
  42. wpa_supplicant_deauthenticate(wpa_s,
  43. WLAN_REASON_DEAUTH_LEAVING);
  44. }
  45. wpa_s->disconnected = 0;
  46. wpa_s->reassociate = 1;
  47. wpa_supplicant_req_scan(wpa_s, 0, 0);
  48. }
  49. static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
  50. struct wpabuf *extra)
  51. {
  52. struct wpabuf *buf;
  53. size_t i;
  54. u8 *len_pos;
  55. buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
  56. (extra ? wpabuf_len(extra) : 0));
  57. if (buf == NULL)
  58. return NULL;
  59. len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
  60. for (i = 0; i < num_ids; i++)
  61. wpabuf_put_le16(buf, info_ids[i]);
  62. gas_anqp_set_element_len(buf, len_pos);
  63. if (extra)
  64. wpabuf_put_buf(buf, extra);
  65. gas_anqp_set_len(buf);
  66. return buf;
  67. }
  68. static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
  69. u8 dialog_token,
  70. enum gas_query_result result,
  71. const struct wpabuf *adv_proto,
  72. const struct wpabuf *resp,
  73. u16 status_code)
  74. {
  75. struct wpa_supplicant *wpa_s = ctx;
  76. anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
  77. status_code);
  78. interworking_next_anqp_fetch(wpa_s);
  79. }
  80. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  81. struct wpa_bss *bss)
  82. {
  83. struct wpabuf *buf;
  84. int ret = 0;
  85. int res;
  86. u16 info_ids[] = {
  87. ANQP_CAPABILITY_LIST,
  88. ANQP_VENUE_NAME,
  89. ANQP_NETWORK_AUTH_TYPE,
  90. ANQP_ROAMING_CONSORTIUM,
  91. ANQP_IP_ADDR_TYPE_AVAILABILITY,
  92. ANQP_NAI_REALM,
  93. ANQP_3GPP_CELLULAR_NETWORK,
  94. ANQP_DOMAIN_NAME
  95. };
  96. struct wpabuf *extra = NULL;
  97. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  98. MAC2STR(bss->bssid));
  99. #ifdef CONFIG_HS20
  100. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  101. u8 *len_pos;
  102. extra = wpabuf_alloc(100);
  103. if (!extra)
  104. return -1;
  105. len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
  106. wpabuf_put_be24(extra, OUI_WFA);
  107. wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
  108. wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
  109. wpabuf_put_u8(extra, 0); /* Reserved */
  110. wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
  111. wpabuf_put_u8(extra, HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  112. wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
  113. wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
  114. wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
  115. gas_anqp_set_element_len(extra, len_pos);
  116. }
  117. #endif /* CONFIG_HS20 */
  118. buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
  119. extra);
  120. wpabuf_free(extra);
  121. if (buf == NULL)
  122. return -1;
  123. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  124. interworking_anqp_resp_cb, wpa_s);
  125. if (res < 0) {
  126. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  127. ret = -1;
  128. } else
  129. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  130. "%u", res);
  131. wpabuf_free(buf);
  132. return ret;
  133. }
  134. struct nai_realm_eap {
  135. u8 method;
  136. u8 inner_method;
  137. enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
  138. u8 cred_type;
  139. u8 tunneled_cred_type;
  140. };
  141. struct nai_realm {
  142. u8 encoding;
  143. char *realm;
  144. u8 eap_count;
  145. struct nai_realm_eap *eap;
  146. };
  147. static void nai_realm_free(struct nai_realm *realms, u16 count)
  148. {
  149. u16 i;
  150. if (realms == NULL)
  151. return;
  152. for (i = 0; i < count; i++) {
  153. os_free(realms[i].eap);
  154. os_free(realms[i].realm);
  155. }
  156. os_free(realms);
  157. }
  158. static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
  159. const u8 *end)
  160. {
  161. u8 elen, auth_count, a;
  162. const u8 *e_end;
  163. if (pos + 3 > end) {
  164. wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
  165. return NULL;
  166. }
  167. elen = *pos++;
  168. if (pos + elen > end || elen < 2) {
  169. wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
  170. return NULL;
  171. }
  172. e_end = pos + elen;
  173. e->method = *pos++;
  174. auth_count = *pos++;
  175. wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
  176. elen, e->method, auth_count);
  177. for (a = 0; a < auth_count; a++) {
  178. u8 id, len;
  179. if (pos + 2 > end || pos + 2 + pos[1] > end) {
  180. wpa_printf(MSG_DEBUG, "No room for Authentication "
  181. "Parameter subfield");
  182. return NULL;
  183. }
  184. id = *pos++;
  185. len = *pos++;
  186. switch (id) {
  187. case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
  188. if (len < 1)
  189. break;
  190. e->inner_non_eap = *pos;
  191. if (e->method != EAP_TYPE_TTLS)
  192. break;
  193. switch (*pos) {
  194. case NAI_REALM_INNER_NON_EAP_PAP:
  195. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
  196. break;
  197. case NAI_REALM_INNER_NON_EAP_CHAP:
  198. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
  199. break;
  200. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  201. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
  202. break;
  203. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  204. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
  205. break;
  206. }
  207. break;
  208. case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
  209. if (len < 1)
  210. break;
  211. e->inner_method = *pos;
  212. wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
  213. e->inner_method);
  214. break;
  215. case NAI_REALM_EAP_AUTH_CRED_TYPE:
  216. if (len < 1)
  217. break;
  218. e->cred_type = *pos;
  219. wpa_printf(MSG_DEBUG, "Credential Type: %u",
  220. e->cred_type);
  221. break;
  222. case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
  223. if (len < 1)
  224. break;
  225. e->tunneled_cred_type = *pos;
  226. wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
  227. "Type: %u", e->tunneled_cred_type);
  228. break;
  229. default:
  230. wpa_printf(MSG_DEBUG, "Unsupported Authentication "
  231. "Parameter: id=%u len=%u", id, len);
  232. wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
  233. "Value", pos, len);
  234. break;
  235. }
  236. pos += len;
  237. }
  238. return e_end;
  239. }
  240. static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
  241. const u8 *end)
  242. {
  243. u16 len;
  244. const u8 *f_end;
  245. u8 realm_len, e;
  246. if (end - pos < 4) {
  247. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  248. "fixed fields");
  249. return NULL;
  250. }
  251. len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
  252. pos += 2;
  253. if (pos + len > end || len < 3) {
  254. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  255. "(len=%u; left=%u)",
  256. len, (unsigned int) (end - pos));
  257. return NULL;
  258. }
  259. f_end = pos + len;
  260. r->encoding = *pos++;
  261. realm_len = *pos++;
  262. if (pos + realm_len > f_end) {
  263. wpa_printf(MSG_DEBUG, "No room for NAI Realm "
  264. "(len=%u; left=%u)",
  265. realm_len, (unsigned int) (f_end - pos));
  266. return NULL;
  267. }
  268. wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
  269. r->realm = os_malloc(realm_len + 1);
  270. if (r->realm == NULL)
  271. return NULL;
  272. os_memcpy(r->realm, pos, realm_len);
  273. r->realm[realm_len] = '\0';
  274. pos += realm_len;
  275. if (pos + 1 > f_end) {
  276. wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
  277. return NULL;
  278. }
  279. r->eap_count = *pos++;
  280. wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
  281. if (pos + r->eap_count * 3 > f_end) {
  282. wpa_printf(MSG_DEBUG, "No room for EAP Methods");
  283. return NULL;
  284. }
  285. r->eap = os_zalloc(r->eap_count * sizeof(struct nai_realm_eap));
  286. if (r->eap == NULL)
  287. return NULL;
  288. for (e = 0; e < r->eap_count; e++) {
  289. pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
  290. if (pos == NULL)
  291. return NULL;
  292. }
  293. return f_end;
  294. }
  295. static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
  296. {
  297. struct nai_realm *realm;
  298. const u8 *pos, *end;
  299. u16 i, num;
  300. if (anqp == NULL || wpabuf_len(anqp) < 2)
  301. return NULL;
  302. pos = wpabuf_head_u8(anqp);
  303. end = pos + wpabuf_len(anqp);
  304. num = WPA_GET_LE16(pos);
  305. wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
  306. pos += 2;
  307. if (num * 5 > end - pos) {
  308. wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
  309. "enough data (%u octets) for that many realms",
  310. num, (unsigned int) (end - pos));
  311. return NULL;
  312. }
  313. realm = os_zalloc(num * sizeof(struct nai_realm));
  314. if (realm == NULL)
  315. return NULL;
  316. for (i = 0; i < num; i++) {
  317. pos = nai_realm_parse_realm(&realm[i], pos, end);
  318. if (pos == NULL) {
  319. nai_realm_free(realm, num);
  320. return NULL;
  321. }
  322. }
  323. *count = num;
  324. return realm;
  325. }
  326. static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
  327. {
  328. char *tmp, *pos, *end;
  329. int match = 0;
  330. if (realm->realm == NULL || home_realm == NULL)
  331. return 0;
  332. if (os_strchr(realm->realm, ';') == NULL)
  333. return os_strcasecmp(realm->realm, home_realm) == 0;
  334. tmp = os_strdup(realm->realm);
  335. if (tmp == NULL)
  336. return 0;
  337. pos = tmp;
  338. while (*pos) {
  339. end = os_strchr(pos, ';');
  340. if (end)
  341. *end = '\0';
  342. if (os_strcasecmp(pos, home_realm) == 0) {
  343. match = 1;
  344. break;
  345. }
  346. if (end == NULL)
  347. break;
  348. pos = end + 1;
  349. }
  350. os_free(tmp);
  351. return match;
  352. }
  353. static int nai_realm_cred_username(struct nai_realm_eap *eap)
  354. {
  355. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  356. return 0; /* method not supported */
  357. if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
  358. /* Only tunneled methods with username/password supported */
  359. return 0;
  360. }
  361. if (eap->method == EAP_TYPE_PEAP &&
  362. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  363. return 0;
  364. if (eap->method == EAP_TYPE_TTLS) {
  365. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  366. return 0;
  367. if (eap->inner_method &&
  368. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  369. return 0;
  370. if (eap->inner_non_eap &&
  371. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  372. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  373. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  374. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  375. return 0;
  376. }
  377. if (eap->inner_method &&
  378. eap->inner_method != EAP_TYPE_GTC &&
  379. eap->inner_method != EAP_TYPE_MSCHAPV2)
  380. return 0;
  381. return 1;
  382. }
  383. static int nai_realm_cred_cert(struct nai_realm_eap *eap)
  384. {
  385. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  386. return 0; /* method not supported */
  387. if (eap->method != EAP_TYPE_TLS) {
  388. /* Only EAP-TLS supported for credential authentication */
  389. return 0;
  390. }
  391. return 1;
  392. }
  393. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  394. struct nai_realm *realm)
  395. {
  396. u8 e;
  397. if (cred == NULL ||
  398. cred->username == NULL ||
  399. cred->username[0] == '\0' ||
  400. ((cred->password == NULL ||
  401. cred->password[0] == '\0') &&
  402. (cred->private_key == NULL ||
  403. cred->private_key[0] == '\0')))
  404. return NULL;
  405. for (e = 0; e < realm->eap_count; e++) {
  406. struct nai_realm_eap *eap = &realm->eap[e];
  407. if (cred->password && cred->password[0] &&
  408. nai_realm_cred_username(eap))
  409. return eap;
  410. if (cred->private_key && cred->private_key[0] &&
  411. nai_realm_cred_cert(eap))
  412. return eap;
  413. }
  414. return NULL;
  415. }
  416. #ifdef INTERWORKING_3GPP
  417. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  418. {
  419. u8 plmn[3];
  420. const u8 *pos, *end;
  421. u8 udhl;
  422. /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
  423. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  424. plmn[1] = imsi[2] - '0';
  425. /* default to MNC length 3 if unknown */
  426. if (mnc_len != 2)
  427. plmn[1] |= (imsi[5] - '0') << 4;
  428. else
  429. plmn[1] |= 0xf0;
  430. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  431. if (anqp == NULL)
  432. return 0;
  433. pos = wpabuf_head_u8(anqp);
  434. end = pos + wpabuf_len(anqp);
  435. if (pos + 2 > end)
  436. return 0;
  437. if (*pos != 0) {
  438. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  439. return 0;
  440. }
  441. pos++;
  442. udhl = *pos++;
  443. if (pos + udhl > end) {
  444. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  445. return 0;
  446. }
  447. end = pos + udhl;
  448. while (pos + 2 <= end) {
  449. u8 iei, len;
  450. const u8 *l_end;
  451. iei = *pos++;
  452. len = *pos++ & 0x7f;
  453. if (pos + len > end)
  454. break;
  455. l_end = pos + len;
  456. if (iei == 0 && len > 0) {
  457. /* PLMN List */
  458. u8 num, i;
  459. num = *pos++;
  460. for (i = 0; i < num; i++) {
  461. if (pos + 3 > end)
  462. break;
  463. if (os_memcmp(pos, plmn, 3) == 0)
  464. return 1; /* Found matching PLMN */
  465. }
  466. }
  467. pos = l_end;
  468. }
  469. return 0;
  470. }
  471. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  472. char prefix)
  473. {
  474. const char *sep, *msin;
  475. char *end, *pos;
  476. size_t msin_len, plmn_len;
  477. /*
  478. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  479. * Root NAI:
  480. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  481. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  482. */
  483. if (imsi == NULL || os_strlen(imsi) > 16) {
  484. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  485. return -1;
  486. }
  487. sep = os_strchr(imsi, '-');
  488. if (sep == NULL)
  489. return -1;
  490. plmn_len = sep - imsi;
  491. if (plmn_len != 5 && plmn_len != 6)
  492. return -1;
  493. msin = sep + 1;
  494. msin_len = os_strlen(msin);
  495. pos = nai;
  496. end = nai + nai_len;
  497. if (prefix)
  498. *pos++ = prefix;
  499. os_memcpy(pos, imsi, plmn_len);
  500. pos += plmn_len;
  501. os_memcpy(pos, msin, msin_len);
  502. pos += msin_len;
  503. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  504. if (plmn_len == 5) {
  505. *pos++ = '0';
  506. *pos++ = imsi[3];
  507. *pos++ = imsi[4];
  508. } else {
  509. *pos++ = imsi[3];
  510. *pos++ = imsi[4];
  511. *pos++ = imsi[5];
  512. }
  513. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  514. imsi[0], imsi[1], imsi[2]);
  515. return 0;
  516. }
  517. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  518. {
  519. char nai[100];
  520. if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
  521. return -1;
  522. return wpa_config_set_quoted(ssid, "identity", nai);
  523. }
  524. #endif /* INTERWORKING_3GPP */
  525. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  526. struct wpa_bss *bss)
  527. {
  528. #ifdef INTERWORKING_3GPP
  529. struct wpa_cred *cred;
  530. struct wpa_ssid *ssid;
  531. const u8 *ie;
  532. if (bss->anqp_3gpp == NULL)
  533. return -1;
  534. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  535. char *sep;
  536. const char *imsi;
  537. int mnc_len;
  538. #ifdef PCSC_FUNCS
  539. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  540. wpa_s->imsi[0]) {
  541. imsi = wpa_s->imsi;
  542. mnc_len = wpa_s->mnc_len;
  543. goto compare;
  544. }
  545. #endif /* PCSC_FUNCS */
  546. if (cred->imsi == NULL || !cred->imsi[0] ||
  547. cred->milenage == NULL || !cred->milenage[0])
  548. continue;
  549. sep = os_strchr(cred->imsi, '-');
  550. if (sep == NULL ||
  551. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  552. continue;
  553. mnc_len = sep - cred->imsi - 3;
  554. imsi = cred->imsi;
  555. #ifdef PCSC_FUNCS
  556. compare:
  557. #endif /* PCSC_FUNCS */
  558. if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
  559. break;
  560. }
  561. if (cred == NULL)
  562. return -1;
  563. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  564. if (ie == NULL)
  565. return -1;
  566. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  567. MAC2STR(bss->bssid));
  568. ssid = wpa_config_add_network(wpa_s->conf);
  569. if (ssid == NULL)
  570. return -1;
  571. wpas_notify_network_added(wpa_s, ssid);
  572. wpa_config_set_network_defaults(ssid);
  573. ssid->priority = cred->priority;
  574. ssid->temporary = 1;
  575. ssid->ssid = os_zalloc(ie[1] + 1);
  576. if (ssid->ssid == NULL)
  577. goto fail;
  578. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  579. ssid->ssid_len = ie[1];
  580. /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
  581. if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
  582. wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
  583. goto fail;
  584. }
  585. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  586. wpa_config_set(ssid, "eap", "AKA", 0);
  587. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
  588. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  589. goto fail;
  590. }
  591. if (cred->milenage && cred->milenage[0]) {
  592. if (wpa_config_set_quoted(ssid, "password",
  593. cred->milenage) < 0)
  594. goto fail;
  595. } else if (cred->pcsc) {
  596. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  597. goto fail;
  598. if (wpa_s->conf->pcsc_pin &&
  599. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  600. < 0)
  601. goto fail;
  602. }
  603. if (cred->password && cred->password[0] &&
  604. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  605. goto fail;
  606. wpa_config_update_prio_list(wpa_s->conf);
  607. interworking_reconnect(wpa_s);
  608. return 0;
  609. fail:
  610. wpas_notify_network_removed(wpa_s, ssid);
  611. wpa_config_remove_network(wpa_s->conf, ssid->id);
  612. #endif /* INTERWORKING_3GPP */
  613. return -1;
  614. }
  615. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  616. {
  617. struct wpa_cred *cred;
  618. struct wpa_ssid *ssid;
  619. struct nai_realm *realm;
  620. struct nai_realm_eap *eap = NULL;
  621. u16 count, i;
  622. char buf[100];
  623. const u8 *ie;
  624. if (wpa_s->conf->cred == NULL || bss == NULL)
  625. return -1;
  626. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  627. if (ie == NULL || ie[1] == 0) {
  628. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  629. MACSTR, MAC2STR(bss->bssid));
  630. return -1;
  631. }
  632. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  633. if (realm == NULL) {
  634. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  635. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  636. count = 0;
  637. }
  638. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  639. for (i = 0; i < count; i++) {
  640. if (!nai_realm_match(&realm[i], cred->realm))
  641. continue;
  642. eap = nai_realm_find_eap(cred, &realm[i]);
  643. if (eap)
  644. break;
  645. }
  646. if (eap)
  647. break;
  648. }
  649. if (!eap) {
  650. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  651. if (realm)
  652. nai_realm_free(realm, count);
  653. return 0;
  654. }
  655. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  656. "and EAP method found for " MACSTR,
  657. MAC2STR(bss->bssid));
  658. nai_realm_free(realm, count);
  659. return -1;
  660. }
  661. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  662. MAC2STR(bss->bssid));
  663. ssid = wpa_config_add_network(wpa_s->conf);
  664. if (ssid == NULL) {
  665. nai_realm_free(realm, count);
  666. return -1;
  667. }
  668. wpas_notify_network_added(wpa_s, ssid);
  669. wpa_config_set_network_defaults(ssid);
  670. ssid->priority = cred->priority;
  671. ssid->temporary = 1;
  672. ssid->ssid = os_zalloc(ie[1] + 1);
  673. if (ssid->ssid == NULL)
  674. goto fail;
  675. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  676. ssid->ssid_len = ie[1];
  677. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  678. eap->method), 0) < 0)
  679. goto fail;
  680. if (eap->method == EAP_TYPE_TTLS &&
  681. cred->username && cred->username[0]) {
  682. const char *pos;
  683. char *anon;
  684. /* Use anonymous NAI in Phase 1 */
  685. pos = os_strchr(cred->username, '@');
  686. if (pos) {
  687. size_t buflen = 9 + os_strlen(pos) + 1;
  688. anon = os_malloc(buflen);
  689. if (anon == NULL)
  690. goto fail;
  691. os_snprintf(anon, buflen, "anonymous%s", pos);
  692. } else if (cred->realm) {
  693. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  694. anon = os_malloc(buflen);
  695. if (anon == NULL)
  696. goto fail;
  697. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  698. } else {
  699. anon = os_strdup("anonymous");
  700. if (anon == NULL)
  701. goto fail;
  702. }
  703. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  704. 0) {
  705. os_free(anon);
  706. goto fail;
  707. }
  708. os_free(anon);
  709. }
  710. if (cred->username && cred->username[0] &&
  711. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  712. goto fail;
  713. if (cred->password && cred->password[0] &&
  714. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  715. goto fail;
  716. if (cred->client_cert && cred->client_cert[0] &&
  717. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  718. goto fail;
  719. if (cred->private_key && cred->private_key[0] &&
  720. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  721. goto fail;
  722. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  723. wpa_config_set_quoted(ssid, "private_key_passwd",
  724. cred->private_key_passwd) < 0)
  725. goto fail;
  726. switch (eap->method) {
  727. case EAP_TYPE_TTLS:
  728. if (eap->inner_method) {
  729. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  730. eap_get_name(EAP_VENDOR_IETF,
  731. eap->inner_method));
  732. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  733. goto fail;
  734. break;
  735. }
  736. switch (eap->inner_non_eap) {
  737. case NAI_REALM_INNER_NON_EAP_PAP:
  738. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  739. 0)
  740. goto fail;
  741. break;
  742. case NAI_REALM_INNER_NON_EAP_CHAP:
  743. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  744. < 0)
  745. goto fail;
  746. break;
  747. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  748. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  749. 0) < 0)
  750. goto fail;
  751. break;
  752. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  753. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  754. 0) < 0)
  755. goto fail;
  756. break;
  757. }
  758. break;
  759. case EAP_TYPE_PEAP:
  760. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  761. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  762. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  763. goto fail;
  764. break;
  765. case EAP_TYPE_TLS:
  766. break;
  767. }
  768. if (cred->ca_cert && cred->ca_cert[0] &&
  769. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  770. goto fail;
  771. nai_realm_free(realm, count);
  772. wpa_config_update_prio_list(wpa_s->conf);
  773. interworking_reconnect(wpa_s);
  774. return 0;
  775. fail:
  776. wpas_notify_network_removed(wpa_s, ssid);
  777. wpa_config_remove_network(wpa_s->conf, ssid->id);
  778. nai_realm_free(realm, count);
  779. return -1;
  780. }
  781. static struct wpa_cred * interworking_credentials_available_3gpp(
  782. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  783. {
  784. struct wpa_cred *cred, *selected = NULL;
  785. int ret;
  786. #ifdef INTERWORKING_3GPP
  787. if (bss->anqp_3gpp == NULL)
  788. return NULL;
  789. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  790. char *sep;
  791. const char *imsi;
  792. int mnc_len;
  793. #ifdef PCSC_FUNCS
  794. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  795. wpa_s->imsi[0]) {
  796. imsi = wpa_s->imsi;
  797. mnc_len = wpa_s->mnc_len;
  798. goto compare;
  799. }
  800. #endif /* PCSC_FUNCS */
  801. if (cred->imsi == NULL || !cred->imsi[0] ||
  802. cred->milenage == NULL || !cred->milenage[0])
  803. continue;
  804. sep = os_strchr(cred->imsi, '-');
  805. if (sep == NULL ||
  806. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  807. continue;
  808. mnc_len = sep - cred->imsi - 3;
  809. imsi = cred->imsi;
  810. #ifdef PCSC_FUNCS
  811. compare:
  812. #endif /* PCSC_FUNCS */
  813. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  814. MACSTR, MAC2STR(bss->bssid));
  815. ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
  816. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  817. if (ret) {
  818. if (selected == NULL ||
  819. selected->priority < cred->priority)
  820. selected = cred;
  821. }
  822. }
  823. #endif /* INTERWORKING_3GPP */
  824. return selected;
  825. }
  826. static struct wpa_cred * interworking_credentials_available_realm(
  827. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  828. {
  829. struct wpa_cred *cred, *selected = NULL;
  830. struct nai_realm *realm;
  831. u16 count, i;
  832. if (bss->anqp_nai_realm == NULL)
  833. return NULL;
  834. if (wpa_s->conf->cred == NULL)
  835. return NULL;
  836. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  837. MACSTR, MAC2STR(bss->bssid));
  838. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  839. if (realm == NULL) {
  840. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  841. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  842. return NULL;
  843. }
  844. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  845. if (cred->realm == NULL)
  846. continue;
  847. for (i = 0; i < count; i++) {
  848. if (!nai_realm_match(&realm[i], cred->realm))
  849. continue;
  850. if (nai_realm_find_eap(cred, &realm[i])) {
  851. if (selected == NULL ||
  852. selected->priority < cred->priority)
  853. selected = cred;
  854. break;
  855. }
  856. }
  857. }
  858. nai_realm_free(realm, count);
  859. return selected;
  860. }
  861. static struct wpa_cred * interworking_credentials_available(
  862. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  863. {
  864. struct wpa_cred *cred, *cred2;
  865. cred = interworking_credentials_available_realm(wpa_s, bss);
  866. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  867. if (cred && cred2 && cred2->priority >= cred->priority)
  868. cred = cred2;
  869. if (!cred)
  870. cred = cred2;
  871. return cred;
  872. }
  873. static int domain_name_list_contains(struct wpabuf *domain_names,
  874. const char *domain)
  875. {
  876. const u8 *pos, *end;
  877. size_t len;
  878. len = os_strlen(domain);
  879. pos = wpabuf_head(domain_names);
  880. end = pos + wpabuf_len(domain_names);
  881. while (pos + 1 < end) {
  882. if (pos + 1 + pos[0] > end)
  883. break;
  884. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  885. pos + 1, pos[0]);
  886. if (pos[0] == len &&
  887. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  888. return 1;
  889. pos += 1 + pos[0];
  890. }
  891. return 0;
  892. }
  893. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  894. struct wpabuf *domain_names)
  895. {
  896. struct wpa_cred *cred;
  897. #ifdef INTERWORKING_3GPP
  898. char nai[100], *realm;
  899. #endif /* INTERWORKING_3GPP */
  900. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  901. return -1;
  902. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  903. #ifdef INTERWORKING_3GPP
  904. if (cred->imsi &&
  905. build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
  906. realm = os_strchr(nai, '@');
  907. if (realm)
  908. realm++;
  909. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  910. "with SIM/USIM domain %s", realm);
  911. if (realm &&
  912. domain_name_list_contains(domain_names, realm))
  913. return 1;
  914. }
  915. #endif /* INTERWORKING_3GPP */
  916. if (cred->domain == NULL)
  917. continue;
  918. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  919. "home SP FQDN %s", cred->domain);
  920. if (domain_name_list_contains(domain_names, cred->domain))
  921. return 1;
  922. }
  923. return 0;
  924. }
  925. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  926. {
  927. struct wpa_bss *bss;
  928. struct wpa_ssid *ssid;
  929. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  930. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  931. if (wpas_network_disabled(wpa_s, ssid) ||
  932. ssid->mode != WPAS_MODE_INFRA)
  933. continue;
  934. if (ssid->ssid_len != bss->ssid_len ||
  935. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  936. 0)
  937. continue;
  938. /*
  939. * TODO: Consider more accurate matching of security
  940. * configuration similarly to what is done in events.c
  941. */
  942. return 1;
  943. }
  944. }
  945. return 0;
  946. }
  947. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  948. {
  949. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  950. int selected_prio = -999999, selected_home_prio = -999999;
  951. unsigned int count = 0;
  952. const char *type;
  953. int res;
  954. struct wpa_cred *cred;
  955. wpa_s->network_select = 0;
  956. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  957. cred = interworking_credentials_available(wpa_s, bss);
  958. if (!cred)
  959. continue;
  960. count++;
  961. res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
  962. if (res > 0)
  963. type = "home";
  964. else if (res == 0)
  965. type = "roaming";
  966. else
  967. type = "unknown";
  968. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  969. MAC2STR(bss->bssid), type);
  970. if (wpa_s->auto_select) {
  971. if (selected == NULL ||
  972. cred->priority > selected_prio) {
  973. selected = bss;
  974. selected_prio = cred->priority;
  975. }
  976. if (res > 0 &&
  977. (selected_home == NULL ||
  978. cred->priority > selected_home_prio)) {
  979. selected_home = bss;
  980. selected_home_prio = cred->priority;
  981. }
  982. }
  983. }
  984. if (selected_home && selected_home != selected &&
  985. selected_home_prio >= selected_prio) {
  986. /* Prefer network operated by the Home SP */
  987. selected = selected_home;
  988. }
  989. if (count == 0) {
  990. /*
  991. * No matching network was found based on configured
  992. * credentials. Check whether any of the enabled network blocks
  993. * have matching APs.
  994. */
  995. if (interworking_find_network_match(wpa_s)) {
  996. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  997. "match for enabled network configurations");
  998. interworking_reconnect(wpa_s);
  999. return;
  1000. }
  1001. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1002. "with matching credentials found");
  1003. }
  1004. if (selected)
  1005. interworking_connect(wpa_s, selected);
  1006. }
  1007. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1008. {
  1009. struct wpa_bss *bss;
  1010. int found = 0;
  1011. const u8 *ie;
  1012. if (!wpa_s->fetch_anqp_in_progress)
  1013. return;
  1014. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1015. if (!(bss->caps & IEEE80211_CAP_ESS))
  1016. continue;
  1017. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1018. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1019. continue; /* AP does not support Interworking */
  1020. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1021. found++;
  1022. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1023. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1024. MACSTR, MAC2STR(bss->bssid));
  1025. interworking_anqp_send_req(wpa_s, bss);
  1026. break;
  1027. }
  1028. }
  1029. if (found == 0) {
  1030. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1031. wpa_s->fetch_anqp_in_progress = 0;
  1032. if (wpa_s->network_select)
  1033. interworking_select_network(wpa_s);
  1034. }
  1035. }
  1036. static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1037. {
  1038. struct wpa_bss *bss;
  1039. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1040. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1041. wpa_s->fetch_anqp_in_progress = 1;
  1042. interworking_next_anqp_fetch(wpa_s);
  1043. }
  1044. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1045. {
  1046. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1047. return 0;
  1048. wpa_s->network_select = 0;
  1049. interworking_start_fetch_anqp(wpa_s);
  1050. return 0;
  1051. }
  1052. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1053. {
  1054. if (!wpa_s->fetch_anqp_in_progress)
  1055. return;
  1056. wpa_s->fetch_anqp_in_progress = 0;
  1057. }
  1058. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1059. u16 info_ids[], size_t num_ids)
  1060. {
  1061. struct wpabuf *buf;
  1062. int ret = 0;
  1063. int freq;
  1064. struct wpa_bss *bss;
  1065. int res;
  1066. freq = wpa_s->assoc_freq;
  1067. bss = wpa_bss_get_bssid(wpa_s, dst);
  1068. if (bss)
  1069. freq = bss->freq;
  1070. if (freq <= 0)
  1071. return -1;
  1072. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1073. MAC2STR(dst), (unsigned int) num_ids);
  1074. buf = anqp_build_req(info_ids, num_ids, NULL);
  1075. if (buf == NULL)
  1076. return -1;
  1077. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1078. if (res < 0) {
  1079. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1080. ret = -1;
  1081. } else
  1082. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1083. "%u", res);
  1084. wpabuf_free(buf);
  1085. return ret;
  1086. }
  1087. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1088. const u8 *sa, u16 info_id,
  1089. const u8 *data, size_t slen)
  1090. {
  1091. const u8 *pos = data;
  1092. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1093. #ifdef CONFIG_HS20
  1094. u8 type;
  1095. #endif /* CONFIG_HS20 */
  1096. switch (info_id) {
  1097. case ANQP_CAPABILITY_LIST:
  1098. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1099. " ANQP Capability list", MAC2STR(sa));
  1100. break;
  1101. case ANQP_VENUE_NAME:
  1102. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1103. " Venue Name", MAC2STR(sa));
  1104. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1105. if (bss) {
  1106. wpabuf_free(bss->anqp_venue_name);
  1107. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  1108. }
  1109. break;
  1110. case ANQP_NETWORK_AUTH_TYPE:
  1111. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1112. " Network Authentication Type information",
  1113. MAC2STR(sa));
  1114. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1115. "Type", pos, slen);
  1116. if (bss) {
  1117. wpabuf_free(bss->anqp_network_auth_type);
  1118. bss->anqp_network_auth_type =
  1119. wpabuf_alloc_copy(pos, slen);
  1120. }
  1121. break;
  1122. case ANQP_ROAMING_CONSORTIUM:
  1123. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1124. " Roaming Consortium list", MAC2STR(sa));
  1125. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1126. pos, slen);
  1127. if (bss) {
  1128. wpabuf_free(bss->anqp_roaming_consortium);
  1129. bss->anqp_roaming_consortium =
  1130. wpabuf_alloc_copy(pos, slen);
  1131. }
  1132. break;
  1133. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1134. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1135. " IP Address Type Availability information",
  1136. MAC2STR(sa));
  1137. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1138. pos, slen);
  1139. if (bss) {
  1140. wpabuf_free(bss->anqp_ip_addr_type_availability);
  1141. bss->anqp_ip_addr_type_availability =
  1142. wpabuf_alloc_copy(pos, slen);
  1143. }
  1144. break;
  1145. case ANQP_NAI_REALM:
  1146. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1147. " NAI Realm list", MAC2STR(sa));
  1148. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1149. if (bss) {
  1150. wpabuf_free(bss->anqp_nai_realm);
  1151. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  1152. }
  1153. break;
  1154. case ANQP_3GPP_CELLULAR_NETWORK:
  1155. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1156. " 3GPP Cellular Network information", MAC2STR(sa));
  1157. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1158. pos, slen);
  1159. if (bss) {
  1160. wpabuf_free(bss->anqp_3gpp);
  1161. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1162. }
  1163. break;
  1164. case ANQP_DOMAIN_NAME:
  1165. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1166. " Domain Name list", MAC2STR(sa));
  1167. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1168. if (bss) {
  1169. wpabuf_free(bss->anqp_domain_name);
  1170. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  1171. }
  1172. break;
  1173. case ANQP_VENDOR_SPECIFIC:
  1174. if (slen < 3)
  1175. return;
  1176. switch (WPA_GET_BE24(pos)) {
  1177. #ifdef CONFIG_HS20
  1178. case OUI_WFA:
  1179. pos += 3;
  1180. slen -= 3;
  1181. if (slen < 1)
  1182. return;
  1183. type = *pos++;
  1184. slen--;
  1185. switch (type) {
  1186. case HS20_ANQP_OUI_TYPE:
  1187. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1188. slen);
  1189. break;
  1190. default:
  1191. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1192. "vendor type %u", type);
  1193. break;
  1194. }
  1195. break;
  1196. #endif /* CONFIG_HS20 */
  1197. default:
  1198. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1199. "vendor-specific ANQP OUI %06x",
  1200. WPA_GET_BE24(pos));
  1201. return;
  1202. }
  1203. break;
  1204. default:
  1205. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1206. "%u", info_id);
  1207. break;
  1208. }
  1209. }
  1210. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1211. enum gas_query_result result,
  1212. const struct wpabuf *adv_proto,
  1213. const struct wpabuf *resp, u16 status_code)
  1214. {
  1215. struct wpa_supplicant *wpa_s = ctx;
  1216. const u8 *pos;
  1217. const u8 *end;
  1218. u16 info_id;
  1219. u16 slen;
  1220. if (result != GAS_QUERY_SUCCESS)
  1221. return;
  1222. pos = wpabuf_head(adv_proto);
  1223. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1224. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1225. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1226. "Protocol in response");
  1227. return;
  1228. }
  1229. pos = wpabuf_head(resp);
  1230. end = pos + wpabuf_len(resp);
  1231. while (pos < end) {
  1232. if (pos + 4 > end) {
  1233. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1234. break;
  1235. }
  1236. info_id = WPA_GET_LE16(pos);
  1237. pos += 2;
  1238. slen = WPA_GET_LE16(pos);
  1239. pos += 2;
  1240. if (pos + slen > end) {
  1241. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1242. "for Info ID %u", info_id);
  1243. break;
  1244. }
  1245. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1246. slen);
  1247. pos += slen;
  1248. }
  1249. }
  1250. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1251. struct wpa_scan_results *scan_res)
  1252. {
  1253. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1254. "ANQP fetch");
  1255. interworking_start_fetch_anqp(wpa_s);
  1256. }
  1257. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1258. {
  1259. interworking_stop_fetch_anqp(wpa_s);
  1260. wpa_s->network_select = 1;
  1261. wpa_s->auto_select = !!auto_select;
  1262. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1263. "selection");
  1264. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1265. wpa_s->scan_req = 2;
  1266. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1267. return 0;
  1268. }