interworking.c 48 KB

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