interworking.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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. 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 == NULL)
  491. return -1;
  492. plmn_len = sep - imsi;
  493. if (plmn_len != 5 && plmn_len != 6)
  494. return -1;
  495. msin = sep + 1;
  496. msin_len = os_strlen(msin);
  497. pos = nai;
  498. end = nai + nai_len;
  499. if (prefix)
  500. *pos++ = prefix;
  501. os_memcpy(pos, imsi, plmn_len);
  502. pos += plmn_len;
  503. os_memcpy(pos, msin, msin_len);
  504. pos += msin_len;
  505. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  506. if (plmn_len == 5) {
  507. *pos++ = '0';
  508. *pos++ = imsi[3];
  509. *pos++ = imsi[4];
  510. } else {
  511. *pos++ = imsi[3];
  512. *pos++ = imsi[4];
  513. *pos++ = imsi[5];
  514. }
  515. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  516. imsi[0], imsi[1], imsi[2]);
  517. return 0;
  518. }
  519. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  520. {
  521. char nai[100];
  522. if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
  523. return -1;
  524. return wpa_config_set_quoted(ssid, "identity", nai);
  525. }
  526. #endif /* INTERWORKING_3GPP */
  527. static int interworking_set_hs20_params(struct wpa_ssid *ssid)
  528. {
  529. if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
  530. return -1;
  531. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  532. return -1;
  533. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  534. return -1;
  535. return 0;
  536. }
  537. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  538. struct wpa_bss *bss)
  539. {
  540. #ifdef INTERWORKING_3GPP
  541. struct wpa_cred *cred;
  542. struct wpa_ssid *ssid;
  543. const u8 *ie;
  544. if (bss->anqp_3gpp == NULL)
  545. return -1;
  546. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  547. char *sep;
  548. const char *imsi;
  549. int mnc_len;
  550. #ifdef PCSC_FUNCS
  551. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  552. wpa_s->imsi[0]) {
  553. imsi = wpa_s->imsi;
  554. mnc_len = wpa_s->mnc_len;
  555. goto compare;
  556. }
  557. #endif /* PCSC_FUNCS */
  558. if (cred->imsi == NULL || !cred->imsi[0] ||
  559. cred->milenage == NULL || !cred->milenage[0])
  560. continue;
  561. sep = os_strchr(cred->imsi, '-');
  562. if (sep == NULL ||
  563. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  564. continue;
  565. mnc_len = sep - cred->imsi - 3;
  566. imsi = cred->imsi;
  567. #ifdef PCSC_FUNCS
  568. compare:
  569. #endif /* PCSC_FUNCS */
  570. if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
  571. break;
  572. }
  573. if (cred == NULL)
  574. return -1;
  575. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  576. if (ie == NULL)
  577. return -1;
  578. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  579. MAC2STR(bss->bssid));
  580. ssid = wpa_config_add_network(wpa_s->conf);
  581. if (ssid == NULL)
  582. return -1;
  583. wpas_notify_network_added(wpa_s, ssid);
  584. wpa_config_set_network_defaults(ssid);
  585. ssid->priority = cred->priority;
  586. ssid->temporary = 1;
  587. ssid->ssid = os_zalloc(ie[1] + 1);
  588. if (ssid->ssid == NULL)
  589. goto fail;
  590. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  591. ssid->ssid_len = ie[1];
  592. if (interworking_set_hs20_params(ssid) < 0)
  593. goto fail;
  594. /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
  595. if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
  596. wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
  597. goto fail;
  598. }
  599. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  600. wpa_config_set(ssid, "eap", "AKA", 0);
  601. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
  602. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  603. goto fail;
  604. }
  605. if (cred->milenage && cred->milenage[0]) {
  606. if (wpa_config_set_quoted(ssid, "password",
  607. cred->milenage) < 0)
  608. goto fail;
  609. } else if (cred->pcsc) {
  610. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  611. goto fail;
  612. if (wpa_s->conf->pcsc_pin &&
  613. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  614. < 0)
  615. goto fail;
  616. }
  617. if (cred->password && cred->password[0] &&
  618. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  619. goto fail;
  620. wpa_config_update_prio_list(wpa_s->conf);
  621. interworking_reconnect(wpa_s);
  622. return 0;
  623. fail:
  624. wpas_notify_network_removed(wpa_s, ssid);
  625. wpa_config_remove_network(wpa_s->conf, ssid->id);
  626. #endif /* INTERWORKING_3GPP */
  627. return -1;
  628. }
  629. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  630. size_t rc_len)
  631. {
  632. const u8 *pos, *end;
  633. u8 lens;
  634. if (ie == NULL)
  635. return 0;
  636. pos = ie + 2;
  637. end = ie + 2 + ie[1];
  638. /* Roaming Consortium element:
  639. * Number of ANQP OIs
  640. * OI #1 and #2 lengths
  641. * OI #1, [OI #2], [OI #3]
  642. */
  643. if (pos + 2 > end)
  644. return 0;
  645. pos++; /* skip Number of ANQP OIs */
  646. lens = *pos++;
  647. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  648. return 0;
  649. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  650. return 1;
  651. pos += lens & 0x0f;
  652. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  653. return 1;
  654. pos += lens >> 4;
  655. if (pos < end && (size_t) (end - pos) == rc_len &&
  656. os_memcmp(pos, rc_id, rc_len) == 0)
  657. return 1;
  658. return 0;
  659. }
  660. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  661. const u8 *rc_id, size_t rc_len)
  662. {
  663. const u8 *pos, *end;
  664. u8 len;
  665. if (anqp == NULL)
  666. return 0;
  667. pos = wpabuf_head(anqp);
  668. end = pos + wpabuf_len(anqp);
  669. /* Set of <OI Length, OI> duples */
  670. while (pos < end) {
  671. len = *pos++;
  672. if (pos + len > end)
  673. break;
  674. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  675. return 1;
  676. pos += len;
  677. }
  678. return 0;
  679. }
  680. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  681. const u8 *rc_id, size_t rc_len)
  682. {
  683. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  684. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  685. }
  686. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  687. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  688. {
  689. struct wpa_cred *cred, *selected = NULL;
  690. const u8 *ie;
  691. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  692. if (ie == NULL && bss->anqp_roaming_consortium == NULL)
  693. return NULL;
  694. if (wpa_s->conf->cred == NULL)
  695. return NULL;
  696. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  697. if (cred->roaming_consortium_len == 0)
  698. continue;
  699. if (!roaming_consortium_match(ie, bss->anqp_roaming_consortium,
  700. cred->roaming_consortium,
  701. cred->roaming_consortium_len))
  702. continue;
  703. if (selected == NULL ||
  704. selected->priority < cred->priority)
  705. selected = cred;
  706. }
  707. return selected;
  708. }
  709. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  710. struct wpa_cred *cred, int ttls)
  711. {
  712. if (cred->eap_method) {
  713. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  714. cred->eap_method->method == EAP_TYPE_TTLS;
  715. os_free(ssid->eap.eap_methods);
  716. ssid->eap.eap_methods =
  717. os_malloc(sizeof(struct eap_method_type) * 2);
  718. if (ssid->eap.eap_methods == NULL)
  719. return -1;
  720. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  721. sizeof(*cred->eap_method));
  722. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  723. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  724. }
  725. if (ttls && cred->username && cred->username[0]) {
  726. const char *pos;
  727. char *anon;
  728. /* Use anonymous NAI in Phase 1 */
  729. pos = os_strchr(cred->username, '@');
  730. if (pos) {
  731. size_t buflen = 9 + os_strlen(pos) + 1;
  732. anon = os_malloc(buflen);
  733. if (anon == NULL)
  734. return -1;
  735. os_snprintf(anon, buflen, "anonymous%s", pos);
  736. } else if (cred->realm) {
  737. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  738. anon = os_malloc(buflen);
  739. if (anon == NULL)
  740. return -1;
  741. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  742. } else {
  743. anon = os_strdup("anonymous");
  744. if (anon == NULL)
  745. return -1;
  746. }
  747. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  748. 0) {
  749. os_free(anon);
  750. return -1;
  751. }
  752. os_free(anon);
  753. }
  754. if (cred->username && cred->username[0] &&
  755. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  756. return -1;
  757. if (cred->password && cred->password[0]) {
  758. if (cred->ext_password &&
  759. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  760. return -1;
  761. if (!cred->ext_password &&
  762. wpa_config_set_quoted(ssid, "password", cred->password) <
  763. 0)
  764. return -1;
  765. }
  766. if (cred->client_cert && cred->client_cert[0] &&
  767. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  768. return -1;
  769. if (cred->private_key && cred->private_key[0] &&
  770. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  771. return -1;
  772. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  773. wpa_config_set_quoted(ssid, "private_key_passwd",
  774. cred->private_key_passwd) < 0)
  775. return -1;
  776. if (cred->phase1) {
  777. os_free(ssid->eap.phase1);
  778. ssid->eap.phase1 = os_strdup(cred->phase1);
  779. }
  780. if (cred->phase2) {
  781. os_free(ssid->eap.phase2);
  782. ssid->eap.phase2 = os_strdup(cred->phase2);
  783. }
  784. if (cred->ca_cert && cred->ca_cert[0] &&
  785. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  786. return -1;
  787. return 0;
  788. }
  789. static int interworking_connect_roaming_consortium(
  790. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  791. struct wpa_bss *bss, const u8 *ssid_ie)
  792. {
  793. struct wpa_ssid *ssid;
  794. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  795. "roaming consortium match", MAC2STR(bss->bssid));
  796. ssid = wpa_config_add_network(wpa_s->conf);
  797. if (ssid == NULL)
  798. return -1;
  799. wpas_notify_network_added(wpa_s, ssid);
  800. wpa_config_set_network_defaults(ssid);
  801. ssid->priority = cred->priority;
  802. ssid->temporary = 1;
  803. ssid->ssid = os_zalloc(ssid_ie[1] + 1);
  804. if (ssid->ssid == NULL)
  805. goto fail;
  806. os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
  807. ssid->ssid_len = ssid_ie[1];
  808. if (interworking_set_hs20_params(ssid) < 0)
  809. goto fail;
  810. if (cred->eap_method == NULL) {
  811. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  812. "credential using roaming consortium");
  813. goto fail;
  814. }
  815. if (interworking_set_eap_params(
  816. ssid, cred,
  817. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  818. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  819. goto fail;
  820. wpa_config_update_prio_list(wpa_s->conf);
  821. interworking_reconnect(wpa_s);
  822. return 0;
  823. fail:
  824. wpas_notify_network_removed(wpa_s, ssid);
  825. wpa_config_remove_network(wpa_s->conf, ssid->id);
  826. return -1;
  827. }
  828. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  829. {
  830. struct wpa_cred *cred;
  831. struct wpa_ssid *ssid;
  832. struct nai_realm *realm;
  833. struct nai_realm_eap *eap = NULL;
  834. u16 count, i;
  835. char buf[100];
  836. const u8 *ie;
  837. if (wpa_s->conf->cred == NULL || bss == NULL)
  838. return -1;
  839. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  840. if (ie == NULL || ie[1] == 0) {
  841. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  842. MACSTR, MAC2STR(bss->bssid));
  843. return -1;
  844. }
  845. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  846. /*
  847. * We currently support only HS 2.0 networks and those are
  848. * required to use WPA2-Enterprise.
  849. */
  850. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  851. "RSN");
  852. return -1;
  853. }
  854. cred = interworking_credentials_available_roaming_consortium(wpa_s,
  855. bss);
  856. if (cred)
  857. return interworking_connect_roaming_consortium(wpa_s, cred,
  858. bss, ie);
  859. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  860. if (realm == NULL) {
  861. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  862. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  863. count = 0;
  864. }
  865. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  866. for (i = 0; i < count; i++) {
  867. if (!nai_realm_match(&realm[i], cred->realm))
  868. continue;
  869. eap = nai_realm_find_eap(cred, &realm[i]);
  870. if (eap)
  871. break;
  872. }
  873. if (eap)
  874. break;
  875. }
  876. if (!eap) {
  877. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  878. if (realm)
  879. nai_realm_free(realm, count);
  880. return 0;
  881. }
  882. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  883. "and EAP method found for " MACSTR,
  884. MAC2STR(bss->bssid));
  885. nai_realm_free(realm, count);
  886. return -1;
  887. }
  888. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  889. MAC2STR(bss->bssid));
  890. ssid = wpa_config_add_network(wpa_s->conf);
  891. if (ssid == NULL) {
  892. nai_realm_free(realm, count);
  893. return -1;
  894. }
  895. wpas_notify_network_added(wpa_s, ssid);
  896. wpa_config_set_network_defaults(ssid);
  897. ssid->priority = cred->priority;
  898. ssid->temporary = 1;
  899. ssid->ssid = os_zalloc(ie[1] + 1);
  900. if (ssid->ssid == NULL)
  901. goto fail;
  902. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  903. ssid->ssid_len = ie[1];
  904. if (interworking_set_hs20_params(ssid) < 0)
  905. goto fail;
  906. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  907. eap->method), 0) < 0)
  908. goto fail;
  909. switch (eap->method) {
  910. case EAP_TYPE_TTLS:
  911. if (eap->inner_method) {
  912. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  913. eap_get_name(EAP_VENDOR_IETF,
  914. eap->inner_method));
  915. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  916. goto fail;
  917. break;
  918. }
  919. switch (eap->inner_non_eap) {
  920. case NAI_REALM_INNER_NON_EAP_PAP:
  921. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  922. 0)
  923. goto fail;
  924. break;
  925. case NAI_REALM_INNER_NON_EAP_CHAP:
  926. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  927. < 0)
  928. goto fail;
  929. break;
  930. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  931. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  932. 0) < 0)
  933. goto fail;
  934. break;
  935. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  936. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  937. 0) < 0)
  938. goto fail;
  939. break;
  940. }
  941. break;
  942. case EAP_TYPE_PEAP:
  943. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  944. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  945. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  946. goto fail;
  947. break;
  948. case EAP_TYPE_TLS:
  949. break;
  950. }
  951. if (interworking_set_eap_params(ssid, cred,
  952. eap->method == EAP_TYPE_TTLS) < 0)
  953. goto fail;
  954. nai_realm_free(realm, count);
  955. wpa_config_update_prio_list(wpa_s->conf);
  956. interworking_reconnect(wpa_s);
  957. return 0;
  958. fail:
  959. wpas_notify_network_removed(wpa_s, ssid);
  960. wpa_config_remove_network(wpa_s->conf, ssid->id);
  961. nai_realm_free(realm, count);
  962. return -1;
  963. }
  964. static struct wpa_cred * interworking_credentials_available_3gpp(
  965. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  966. {
  967. struct wpa_cred *cred, *selected = NULL;
  968. int ret;
  969. #ifdef INTERWORKING_3GPP
  970. if (bss->anqp_3gpp == NULL)
  971. return NULL;
  972. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  973. char *sep;
  974. const char *imsi;
  975. int mnc_len;
  976. #ifdef PCSC_FUNCS
  977. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  978. wpa_s->imsi[0]) {
  979. imsi = wpa_s->imsi;
  980. mnc_len = wpa_s->mnc_len;
  981. goto compare;
  982. }
  983. #endif /* PCSC_FUNCS */
  984. if (cred->imsi == NULL || !cred->imsi[0] ||
  985. cred->milenage == NULL || !cred->milenage[0])
  986. continue;
  987. sep = os_strchr(cred->imsi, '-');
  988. if (sep == NULL ||
  989. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  990. continue;
  991. mnc_len = sep - cred->imsi - 3;
  992. imsi = cred->imsi;
  993. #ifdef PCSC_FUNCS
  994. compare:
  995. #endif /* PCSC_FUNCS */
  996. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  997. MACSTR, MAC2STR(bss->bssid));
  998. ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
  999. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1000. if (ret) {
  1001. if (selected == NULL ||
  1002. selected->priority < cred->priority)
  1003. selected = cred;
  1004. }
  1005. }
  1006. #endif /* INTERWORKING_3GPP */
  1007. return selected;
  1008. }
  1009. static struct wpa_cred * interworking_credentials_available_realm(
  1010. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1011. {
  1012. struct wpa_cred *cred, *selected = NULL;
  1013. struct nai_realm *realm;
  1014. u16 count, i;
  1015. if (bss->anqp_nai_realm == NULL)
  1016. return NULL;
  1017. if (wpa_s->conf->cred == NULL)
  1018. return NULL;
  1019. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1020. MACSTR, MAC2STR(bss->bssid));
  1021. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  1022. if (realm == NULL) {
  1023. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1024. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1025. return NULL;
  1026. }
  1027. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1028. if (cred->realm == NULL)
  1029. continue;
  1030. for (i = 0; i < count; i++) {
  1031. if (!nai_realm_match(&realm[i], cred->realm))
  1032. continue;
  1033. if (nai_realm_find_eap(cred, &realm[i])) {
  1034. if (selected == NULL ||
  1035. selected->priority < cred->priority)
  1036. selected = cred;
  1037. break;
  1038. }
  1039. }
  1040. }
  1041. nai_realm_free(realm, count);
  1042. return selected;
  1043. }
  1044. static struct wpa_cred * interworking_credentials_available(
  1045. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1046. {
  1047. struct wpa_cred *cred, *cred2;
  1048. cred = interworking_credentials_available_realm(wpa_s, bss);
  1049. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1050. if (cred && cred2 && cred2->priority >= cred->priority)
  1051. cred = cred2;
  1052. if (!cred)
  1053. cred = cred2;
  1054. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1055. bss);
  1056. if (cred && cred2 && cred2->priority >= cred->priority)
  1057. cred = cred2;
  1058. if (!cred)
  1059. cred = cred2;
  1060. return cred;
  1061. }
  1062. static int domain_name_list_contains(struct wpabuf *domain_names,
  1063. const char *domain)
  1064. {
  1065. const u8 *pos, *end;
  1066. size_t len;
  1067. len = os_strlen(domain);
  1068. pos = wpabuf_head(domain_names);
  1069. end = pos + wpabuf_len(domain_names);
  1070. while (pos + 1 < end) {
  1071. if (pos + 1 + pos[0] > end)
  1072. break;
  1073. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1074. pos + 1, pos[0]);
  1075. if (pos[0] == len &&
  1076. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1077. return 1;
  1078. pos += 1 + pos[0];
  1079. }
  1080. return 0;
  1081. }
  1082. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1083. struct wpabuf *domain_names)
  1084. {
  1085. struct wpa_cred *cred;
  1086. #ifdef INTERWORKING_3GPP
  1087. char nai[100], *realm;
  1088. #endif /* INTERWORKING_3GPP */
  1089. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1090. return -1;
  1091. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1092. #ifdef INTERWORKING_3GPP
  1093. if (cred->imsi &&
  1094. build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
  1095. realm = os_strchr(nai, '@');
  1096. if (realm)
  1097. realm++;
  1098. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1099. "with SIM/USIM domain %s", realm);
  1100. if (realm &&
  1101. domain_name_list_contains(domain_names, realm))
  1102. return 1;
  1103. }
  1104. #endif /* INTERWORKING_3GPP */
  1105. if (cred->domain == NULL)
  1106. continue;
  1107. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1108. "home SP FQDN %s", cred->domain);
  1109. if (domain_name_list_contains(domain_names, cred->domain))
  1110. return 1;
  1111. }
  1112. return 0;
  1113. }
  1114. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1115. {
  1116. struct wpa_bss *bss;
  1117. struct wpa_ssid *ssid;
  1118. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1119. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1120. if (wpas_network_disabled(wpa_s, ssid) ||
  1121. ssid->mode != WPAS_MODE_INFRA)
  1122. continue;
  1123. if (ssid->ssid_len != bss->ssid_len ||
  1124. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1125. 0)
  1126. continue;
  1127. /*
  1128. * TODO: Consider more accurate matching of security
  1129. * configuration similarly to what is done in events.c
  1130. */
  1131. return 1;
  1132. }
  1133. }
  1134. return 0;
  1135. }
  1136. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1137. {
  1138. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1139. int selected_prio = -999999, selected_home_prio = -999999;
  1140. unsigned int count = 0;
  1141. const char *type;
  1142. int res;
  1143. struct wpa_cred *cred;
  1144. wpa_s->network_select = 0;
  1145. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1146. cred = interworking_credentials_available(wpa_s, bss);
  1147. if (!cred)
  1148. continue;
  1149. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1150. /*
  1151. * We currently support only HS 2.0 networks and those
  1152. * are required to use WPA2-Enterprise.
  1153. */
  1154. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1155. "with " MACSTR " but network does not use "
  1156. "RSN", MAC2STR(bss->bssid));
  1157. continue;
  1158. }
  1159. count++;
  1160. res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
  1161. if (res > 0)
  1162. type = "home";
  1163. else if (res == 0)
  1164. type = "roaming";
  1165. else
  1166. type = "unknown";
  1167. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1168. MAC2STR(bss->bssid), type);
  1169. if (wpa_s->auto_select) {
  1170. if (selected == NULL ||
  1171. cred->priority > selected_prio) {
  1172. selected = bss;
  1173. selected_prio = cred->priority;
  1174. }
  1175. if (res > 0 &&
  1176. (selected_home == NULL ||
  1177. cred->priority > selected_home_prio)) {
  1178. selected_home = bss;
  1179. selected_home_prio = cred->priority;
  1180. }
  1181. }
  1182. }
  1183. if (selected_home && selected_home != selected &&
  1184. selected_home_prio >= selected_prio) {
  1185. /* Prefer network operated by the Home SP */
  1186. selected = selected_home;
  1187. }
  1188. if (count == 0) {
  1189. /*
  1190. * No matching network was found based on configured
  1191. * credentials. Check whether any of the enabled network blocks
  1192. * have matching APs.
  1193. */
  1194. if (interworking_find_network_match(wpa_s)) {
  1195. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1196. "match for enabled network configurations");
  1197. if (wpa_s->auto_select)
  1198. interworking_reconnect(wpa_s);
  1199. return;
  1200. }
  1201. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1202. "with matching credentials found");
  1203. }
  1204. if (selected)
  1205. interworking_connect(wpa_s, selected);
  1206. }
  1207. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1208. {
  1209. struct wpa_bss *bss;
  1210. int found = 0;
  1211. const u8 *ie;
  1212. if (!wpa_s->fetch_anqp_in_progress)
  1213. return;
  1214. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1215. if (!(bss->caps & IEEE80211_CAP_ESS))
  1216. continue;
  1217. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1218. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1219. continue; /* AP does not support Interworking */
  1220. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1221. found++;
  1222. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1223. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1224. MACSTR, MAC2STR(bss->bssid));
  1225. interworking_anqp_send_req(wpa_s, bss);
  1226. break;
  1227. }
  1228. }
  1229. if (found == 0) {
  1230. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1231. wpa_s->fetch_anqp_in_progress = 0;
  1232. if (wpa_s->network_select)
  1233. interworking_select_network(wpa_s);
  1234. }
  1235. }
  1236. static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1237. {
  1238. struct wpa_bss *bss;
  1239. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1240. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1241. wpa_s->fetch_anqp_in_progress = 1;
  1242. interworking_next_anqp_fetch(wpa_s);
  1243. }
  1244. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1245. {
  1246. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1247. return 0;
  1248. wpa_s->network_select = 0;
  1249. interworking_start_fetch_anqp(wpa_s);
  1250. return 0;
  1251. }
  1252. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1253. {
  1254. if (!wpa_s->fetch_anqp_in_progress)
  1255. return;
  1256. wpa_s->fetch_anqp_in_progress = 0;
  1257. }
  1258. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1259. u16 info_ids[], size_t num_ids)
  1260. {
  1261. struct wpabuf *buf;
  1262. int ret = 0;
  1263. int freq;
  1264. struct wpa_bss *bss;
  1265. int res;
  1266. freq = wpa_s->assoc_freq;
  1267. bss = wpa_bss_get_bssid(wpa_s, dst);
  1268. if (bss)
  1269. freq = bss->freq;
  1270. if (freq <= 0)
  1271. return -1;
  1272. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1273. MAC2STR(dst), (unsigned int) num_ids);
  1274. buf = anqp_build_req(info_ids, num_ids, NULL);
  1275. if (buf == NULL)
  1276. return -1;
  1277. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1278. if (res < 0) {
  1279. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1280. ret = -1;
  1281. } else
  1282. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1283. "%u", res);
  1284. wpabuf_free(buf);
  1285. return ret;
  1286. }
  1287. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1288. const u8 *sa, u16 info_id,
  1289. const u8 *data, size_t slen)
  1290. {
  1291. const u8 *pos = data;
  1292. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1293. #ifdef CONFIG_HS20
  1294. u8 type;
  1295. #endif /* CONFIG_HS20 */
  1296. switch (info_id) {
  1297. case ANQP_CAPABILITY_LIST:
  1298. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1299. " ANQP Capability list", MAC2STR(sa));
  1300. break;
  1301. case ANQP_VENUE_NAME:
  1302. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1303. " Venue Name", MAC2STR(sa));
  1304. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1305. if (bss) {
  1306. wpabuf_free(bss->anqp_venue_name);
  1307. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  1308. }
  1309. break;
  1310. case ANQP_NETWORK_AUTH_TYPE:
  1311. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1312. " Network Authentication Type information",
  1313. MAC2STR(sa));
  1314. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1315. "Type", pos, slen);
  1316. if (bss) {
  1317. wpabuf_free(bss->anqp_network_auth_type);
  1318. bss->anqp_network_auth_type =
  1319. wpabuf_alloc_copy(pos, slen);
  1320. }
  1321. break;
  1322. case ANQP_ROAMING_CONSORTIUM:
  1323. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1324. " Roaming Consortium list", MAC2STR(sa));
  1325. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1326. pos, slen);
  1327. if (bss) {
  1328. wpabuf_free(bss->anqp_roaming_consortium);
  1329. bss->anqp_roaming_consortium =
  1330. wpabuf_alloc_copy(pos, slen);
  1331. }
  1332. break;
  1333. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1334. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1335. " IP Address Type Availability information",
  1336. MAC2STR(sa));
  1337. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1338. pos, slen);
  1339. if (bss) {
  1340. wpabuf_free(bss->anqp_ip_addr_type_availability);
  1341. bss->anqp_ip_addr_type_availability =
  1342. wpabuf_alloc_copy(pos, slen);
  1343. }
  1344. break;
  1345. case ANQP_NAI_REALM:
  1346. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1347. " NAI Realm list", MAC2STR(sa));
  1348. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1349. if (bss) {
  1350. wpabuf_free(bss->anqp_nai_realm);
  1351. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  1352. }
  1353. break;
  1354. case ANQP_3GPP_CELLULAR_NETWORK:
  1355. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1356. " 3GPP Cellular Network information", MAC2STR(sa));
  1357. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1358. pos, slen);
  1359. if (bss) {
  1360. wpabuf_free(bss->anqp_3gpp);
  1361. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1362. }
  1363. break;
  1364. case ANQP_DOMAIN_NAME:
  1365. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1366. " Domain Name list", MAC2STR(sa));
  1367. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1368. if (bss) {
  1369. wpabuf_free(bss->anqp_domain_name);
  1370. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  1371. }
  1372. break;
  1373. case ANQP_VENDOR_SPECIFIC:
  1374. if (slen < 3)
  1375. return;
  1376. switch (WPA_GET_BE24(pos)) {
  1377. #ifdef CONFIG_HS20
  1378. case OUI_WFA:
  1379. pos += 3;
  1380. slen -= 3;
  1381. if (slen < 1)
  1382. return;
  1383. type = *pos++;
  1384. slen--;
  1385. switch (type) {
  1386. case HS20_ANQP_OUI_TYPE:
  1387. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1388. slen);
  1389. break;
  1390. default:
  1391. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1392. "vendor type %u", type);
  1393. break;
  1394. }
  1395. break;
  1396. #endif /* CONFIG_HS20 */
  1397. default:
  1398. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1399. "vendor-specific ANQP OUI %06x",
  1400. WPA_GET_BE24(pos));
  1401. return;
  1402. }
  1403. break;
  1404. default:
  1405. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1406. "%u", info_id);
  1407. break;
  1408. }
  1409. }
  1410. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1411. enum gas_query_result result,
  1412. const struct wpabuf *adv_proto,
  1413. const struct wpabuf *resp, u16 status_code)
  1414. {
  1415. struct wpa_supplicant *wpa_s = ctx;
  1416. const u8 *pos;
  1417. const u8 *end;
  1418. u16 info_id;
  1419. u16 slen;
  1420. if (result != GAS_QUERY_SUCCESS)
  1421. return;
  1422. pos = wpabuf_head(adv_proto);
  1423. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1424. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1425. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1426. "Protocol in response");
  1427. return;
  1428. }
  1429. pos = wpabuf_head(resp);
  1430. end = pos + wpabuf_len(resp);
  1431. while (pos < end) {
  1432. if (pos + 4 > end) {
  1433. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1434. break;
  1435. }
  1436. info_id = WPA_GET_LE16(pos);
  1437. pos += 2;
  1438. slen = WPA_GET_LE16(pos);
  1439. pos += 2;
  1440. if (pos + slen > end) {
  1441. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1442. "for Info ID %u", info_id);
  1443. break;
  1444. }
  1445. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1446. slen);
  1447. pos += slen;
  1448. }
  1449. }
  1450. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1451. struct wpa_scan_results *scan_res)
  1452. {
  1453. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1454. "ANQP fetch");
  1455. interworking_start_fetch_anqp(wpa_s);
  1456. }
  1457. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1458. {
  1459. interworking_stop_fetch_anqp(wpa_s);
  1460. wpa_s->network_select = 1;
  1461. wpa_s->auto_select = !!auto_select;
  1462. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1463. "selection");
  1464. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1465. wpa_s->scan_req = 2;
  1466. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1467. return 0;
  1468. }