interworking.c 32 KB

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