interworking.c 50 KB

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