interworking.c 29 KB

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