interworking.c 41 KB

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