interworking.c 50 KB

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