interworking.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  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 0;
  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_ssid *ssid)
  603. {
  604. if (wpa_config_set(ssid, "key_mgmt", "WPA-EAP", 0) < 0)
  605. return -1;
  606. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  607. return -1;
  608. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  609. return -1;
  610. return 0;
  611. }
  612. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  613. struct wpa_bss *bss)
  614. {
  615. #ifdef INTERWORKING_3GPP
  616. struct wpa_cred *cred;
  617. struct wpa_ssid *ssid;
  618. const u8 *ie;
  619. int eap_type;
  620. int res;
  621. char prefix;
  622. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  623. return -1;
  624. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  625. char *sep;
  626. const char *imsi;
  627. int mnc_len;
  628. #ifdef PCSC_FUNCS
  629. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  630. wpa_s->imsi[0]) {
  631. imsi = wpa_s->imsi;
  632. mnc_len = wpa_s->mnc_len;
  633. goto compare;
  634. }
  635. #endif /* PCSC_FUNCS */
  636. if (cred->imsi == NULL || !cred->imsi[0] ||
  637. cred->milenage == NULL || !cred->milenage[0])
  638. continue;
  639. sep = os_strchr(cred->imsi, '-');
  640. if (sep == NULL ||
  641. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  642. continue;
  643. mnc_len = sep - cred->imsi - 3;
  644. imsi = cred->imsi;
  645. #ifdef PCSC_FUNCS
  646. compare:
  647. #endif /* PCSC_FUNCS */
  648. if (plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len))
  649. break;
  650. }
  651. if (cred == NULL)
  652. return -1;
  653. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  654. if (ie == NULL)
  655. return -1;
  656. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  657. MAC2STR(bss->bssid));
  658. ssid = wpa_config_add_network(wpa_s->conf);
  659. if (ssid == NULL)
  660. return -1;
  661. ssid->parent_cred = cred;
  662. wpas_notify_network_added(wpa_s, ssid);
  663. wpa_config_set_network_defaults(ssid);
  664. ssid->priority = cred->priority;
  665. ssid->temporary = 1;
  666. ssid->ssid = os_zalloc(ie[1] + 1);
  667. if (ssid->ssid == NULL)
  668. goto fail;
  669. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  670. ssid->ssid_len = ie[1];
  671. if (interworking_set_hs20_params(ssid) < 0)
  672. goto fail;
  673. eap_type = EAP_TYPE_SIM;
  674. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  675. eap_type = EAP_TYPE_AKA;
  676. if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
  677. if (cred->eap_method[0].method == EAP_TYPE_SIM ||
  678. cred->eap_method[0].method == EAP_TYPE_AKA ||
  679. cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
  680. eap_type = cred->eap_method[0].method;
  681. }
  682. switch (eap_type) {
  683. case EAP_TYPE_SIM:
  684. prefix = '1';
  685. res = wpa_config_set(ssid, "eap", "SIM", 0);
  686. break;
  687. case EAP_TYPE_AKA:
  688. prefix = '0';
  689. res = wpa_config_set(ssid, "eap", "AKA", 0);
  690. break;
  691. case EAP_TYPE_AKA_PRIME:
  692. prefix = '6';
  693. res = wpa_config_set(ssid, "eap", "AKA'", 0);
  694. break;
  695. default:
  696. res = -1;
  697. break;
  698. }
  699. if (res < 0) {
  700. wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
  701. eap_type);
  702. goto fail;
  703. }
  704. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
  705. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  706. goto fail;
  707. }
  708. if (cred->milenage && cred->milenage[0]) {
  709. if (wpa_config_set_quoted(ssid, "password",
  710. cred->milenage) < 0)
  711. goto fail;
  712. } else if (cred->pcsc) {
  713. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  714. goto fail;
  715. if (wpa_s->conf->pcsc_pin &&
  716. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  717. < 0)
  718. goto fail;
  719. }
  720. if (cred->password && cred->password[0] &&
  721. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  722. goto fail;
  723. wpa_config_update_prio_list(wpa_s->conf);
  724. interworking_reconnect(wpa_s);
  725. return 0;
  726. fail:
  727. wpas_notify_network_removed(wpa_s, ssid);
  728. wpa_config_remove_network(wpa_s->conf, ssid->id);
  729. #endif /* INTERWORKING_3GPP */
  730. return -1;
  731. }
  732. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  733. size_t rc_len)
  734. {
  735. const u8 *pos, *end;
  736. u8 lens;
  737. if (ie == NULL)
  738. return 0;
  739. pos = ie + 2;
  740. end = ie + 2 + ie[1];
  741. /* Roaming Consortium element:
  742. * Number of ANQP OIs
  743. * OI #1 and #2 lengths
  744. * OI #1, [OI #2], [OI #3]
  745. */
  746. if (pos + 2 > end)
  747. return 0;
  748. pos++; /* skip Number of ANQP OIs */
  749. lens = *pos++;
  750. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  751. return 0;
  752. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  753. return 1;
  754. pos += lens & 0x0f;
  755. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  756. return 1;
  757. pos += lens >> 4;
  758. if (pos < end && (size_t) (end - pos) == rc_len &&
  759. os_memcmp(pos, rc_id, rc_len) == 0)
  760. return 1;
  761. return 0;
  762. }
  763. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  764. const u8 *rc_id, size_t rc_len)
  765. {
  766. const u8 *pos, *end;
  767. u8 len;
  768. if (anqp == NULL)
  769. return 0;
  770. pos = wpabuf_head(anqp);
  771. end = pos + wpabuf_len(anqp);
  772. /* Set of <OI Length, OI> duples */
  773. while (pos < end) {
  774. len = *pos++;
  775. if (pos + len > end)
  776. break;
  777. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  778. return 1;
  779. pos += len;
  780. }
  781. return 0;
  782. }
  783. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  784. const u8 *rc_id, size_t rc_len)
  785. {
  786. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  787. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  788. }
  789. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  790. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  791. {
  792. struct wpa_cred *cred, *selected = NULL;
  793. const u8 *ie;
  794. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  795. if (ie == NULL &&
  796. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  797. return NULL;
  798. if (wpa_s->conf->cred == NULL)
  799. return NULL;
  800. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  801. if (cred->roaming_consortium_len == 0)
  802. continue;
  803. if (!roaming_consortium_match(ie,
  804. bss->anqp ?
  805. bss->anqp->roaming_consortium :
  806. NULL,
  807. cred->roaming_consortium,
  808. cred->roaming_consortium_len))
  809. continue;
  810. if (selected == NULL ||
  811. selected->priority < cred->priority)
  812. selected = cred;
  813. }
  814. return selected;
  815. }
  816. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  817. struct wpa_cred *cred, int ttls)
  818. {
  819. if (cred->eap_method) {
  820. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  821. cred->eap_method->method == EAP_TYPE_TTLS;
  822. os_free(ssid->eap.eap_methods);
  823. ssid->eap.eap_methods =
  824. os_malloc(sizeof(struct eap_method_type) * 2);
  825. if (ssid->eap.eap_methods == NULL)
  826. return -1;
  827. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  828. sizeof(*cred->eap_method));
  829. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  830. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  831. }
  832. if (ttls && cred->username && cred->username[0]) {
  833. const char *pos;
  834. char *anon;
  835. /* Use anonymous NAI in Phase 1 */
  836. pos = os_strchr(cred->username, '@');
  837. if (pos) {
  838. size_t buflen = 9 + os_strlen(pos) + 1;
  839. anon = os_malloc(buflen);
  840. if (anon == NULL)
  841. return -1;
  842. os_snprintf(anon, buflen, "anonymous%s", pos);
  843. } else if (cred->realm) {
  844. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  845. anon = os_malloc(buflen);
  846. if (anon == NULL)
  847. return -1;
  848. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  849. } else {
  850. anon = os_strdup("anonymous");
  851. if (anon == NULL)
  852. return -1;
  853. }
  854. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  855. 0) {
  856. os_free(anon);
  857. return -1;
  858. }
  859. os_free(anon);
  860. }
  861. if (cred->username && cred->username[0] &&
  862. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  863. return -1;
  864. if (cred->password && cred->password[0]) {
  865. if (cred->ext_password &&
  866. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  867. return -1;
  868. if (!cred->ext_password &&
  869. wpa_config_set_quoted(ssid, "password", cred->password) <
  870. 0)
  871. return -1;
  872. }
  873. if (cred->client_cert && cred->client_cert[0] &&
  874. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  875. return -1;
  876. #ifdef ANDROID
  877. if (cred->private_key &&
  878. os_strncmp(cred->private_key, "keystore://", 11) == 0) {
  879. /* Use OpenSSL engine configuration for Android keystore */
  880. if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
  881. wpa_config_set_quoted(ssid, "key_id",
  882. cred->private_key + 11) < 0 ||
  883. wpa_config_set(ssid, "engine", "1", 0) < 0)
  884. return -1;
  885. } else
  886. #endif /* ANDROID */
  887. if (cred->private_key && cred->private_key[0] &&
  888. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  889. return -1;
  890. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  891. wpa_config_set_quoted(ssid, "private_key_passwd",
  892. cred->private_key_passwd) < 0)
  893. return -1;
  894. if (cred->phase1) {
  895. os_free(ssid->eap.phase1);
  896. ssid->eap.phase1 = os_strdup(cred->phase1);
  897. }
  898. if (cred->phase2) {
  899. os_free(ssid->eap.phase2);
  900. ssid->eap.phase2 = os_strdup(cred->phase2);
  901. }
  902. if (cred->ca_cert && cred->ca_cert[0] &&
  903. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  904. return -1;
  905. return 0;
  906. }
  907. static int interworking_connect_roaming_consortium(
  908. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  909. struct wpa_bss *bss, const u8 *ssid_ie)
  910. {
  911. struct wpa_ssid *ssid;
  912. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  913. "roaming consortium match", MAC2STR(bss->bssid));
  914. ssid = wpa_config_add_network(wpa_s->conf);
  915. if (ssid == NULL)
  916. return -1;
  917. ssid->parent_cred = cred;
  918. wpas_notify_network_added(wpa_s, ssid);
  919. wpa_config_set_network_defaults(ssid);
  920. ssid->priority = cred->priority;
  921. ssid->temporary = 1;
  922. ssid->ssid = os_zalloc(ssid_ie[1] + 1);
  923. if (ssid->ssid == NULL)
  924. goto fail;
  925. os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
  926. ssid->ssid_len = ssid_ie[1];
  927. if (interworking_set_hs20_params(ssid) < 0)
  928. goto fail;
  929. if (cred->eap_method == NULL) {
  930. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  931. "credential using roaming consortium");
  932. goto fail;
  933. }
  934. if (interworking_set_eap_params(
  935. ssid, cred,
  936. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  937. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  938. goto fail;
  939. wpa_config_update_prio_list(wpa_s->conf);
  940. interworking_reconnect(wpa_s);
  941. return 0;
  942. fail:
  943. wpas_notify_network_removed(wpa_s, ssid);
  944. wpa_config_remove_network(wpa_s->conf, ssid->id);
  945. return -1;
  946. }
  947. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  948. {
  949. struct wpa_cred *cred;
  950. struct wpa_ssid *ssid;
  951. struct nai_realm *realm;
  952. struct nai_realm_eap *eap = NULL;
  953. u16 count, i;
  954. char buf[100];
  955. const u8 *ie;
  956. if (wpa_s->conf->cred == NULL || bss == NULL)
  957. return -1;
  958. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  959. if (ie == NULL || ie[1] == 0) {
  960. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  961. MACSTR, MAC2STR(bss->bssid));
  962. return -1;
  963. }
  964. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  965. /*
  966. * We currently support only HS 2.0 networks and those are
  967. * required to use WPA2-Enterprise.
  968. */
  969. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  970. "RSN");
  971. return -1;
  972. }
  973. cred = interworking_credentials_available_roaming_consortium(wpa_s,
  974. bss);
  975. if (cred)
  976. return interworking_connect_roaming_consortium(wpa_s, cred,
  977. bss, ie);
  978. realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
  979. &count);
  980. if (realm == NULL) {
  981. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  982. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  983. count = 0;
  984. }
  985. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  986. for (i = 0; i < count; i++) {
  987. if (!nai_realm_match(&realm[i], cred->realm))
  988. continue;
  989. eap = nai_realm_find_eap(cred, &realm[i]);
  990. if (eap)
  991. break;
  992. }
  993. if (eap)
  994. break;
  995. }
  996. if (!eap) {
  997. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  998. if (realm)
  999. nai_realm_free(realm, count);
  1000. return 0;
  1001. }
  1002. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1003. "and EAP method found for " MACSTR,
  1004. MAC2STR(bss->bssid));
  1005. nai_realm_free(realm, count);
  1006. return -1;
  1007. }
  1008. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  1009. MAC2STR(bss->bssid));
  1010. ssid = wpa_config_add_network(wpa_s->conf);
  1011. if (ssid == NULL) {
  1012. nai_realm_free(realm, count);
  1013. return -1;
  1014. }
  1015. ssid->parent_cred = cred;
  1016. wpas_notify_network_added(wpa_s, ssid);
  1017. wpa_config_set_network_defaults(ssid);
  1018. ssid->priority = cred->priority;
  1019. ssid->temporary = 1;
  1020. ssid->ssid = os_zalloc(ie[1] + 1);
  1021. if (ssid->ssid == NULL)
  1022. goto fail;
  1023. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  1024. ssid->ssid_len = ie[1];
  1025. if (interworking_set_hs20_params(ssid) < 0)
  1026. goto fail;
  1027. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  1028. eap->method), 0) < 0)
  1029. goto fail;
  1030. switch (eap->method) {
  1031. case EAP_TYPE_TTLS:
  1032. if (eap->inner_method) {
  1033. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  1034. eap_get_name(EAP_VENDOR_IETF,
  1035. eap->inner_method));
  1036. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1037. goto fail;
  1038. break;
  1039. }
  1040. switch (eap->inner_non_eap) {
  1041. case NAI_REALM_INNER_NON_EAP_PAP:
  1042. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  1043. 0)
  1044. goto fail;
  1045. break;
  1046. case NAI_REALM_INNER_NON_EAP_CHAP:
  1047. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1048. < 0)
  1049. goto fail;
  1050. break;
  1051. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1052. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1053. 0) < 0)
  1054. goto fail;
  1055. break;
  1056. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1057. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1058. 0) < 0)
  1059. goto fail;
  1060. break;
  1061. }
  1062. break;
  1063. case EAP_TYPE_PEAP:
  1064. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1065. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  1066. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1067. goto fail;
  1068. break;
  1069. case EAP_TYPE_TLS:
  1070. break;
  1071. }
  1072. if (interworking_set_eap_params(ssid, cred,
  1073. eap->method == EAP_TYPE_TTLS) < 0)
  1074. goto fail;
  1075. nai_realm_free(realm, count);
  1076. wpa_config_update_prio_list(wpa_s->conf);
  1077. interworking_reconnect(wpa_s);
  1078. return 0;
  1079. fail:
  1080. wpas_notify_network_removed(wpa_s, ssid);
  1081. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1082. nai_realm_free(realm, count);
  1083. return -1;
  1084. }
  1085. static struct wpa_cred * interworking_credentials_available_3gpp(
  1086. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1087. {
  1088. struct wpa_cred *cred, *selected = NULL;
  1089. int ret;
  1090. #ifdef INTERWORKING_3GPP
  1091. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  1092. return NULL;
  1093. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1094. char *sep;
  1095. const char *imsi;
  1096. int mnc_len;
  1097. #ifdef PCSC_FUNCS
  1098. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1099. wpa_s->imsi[0]) {
  1100. imsi = wpa_s->imsi;
  1101. mnc_len = wpa_s->mnc_len;
  1102. goto compare;
  1103. }
  1104. #endif /* PCSC_FUNCS */
  1105. if (cred->imsi == NULL || !cred->imsi[0] ||
  1106. cred->milenage == NULL || !cred->milenage[0])
  1107. continue;
  1108. sep = os_strchr(cred->imsi, '-');
  1109. if (sep == NULL ||
  1110. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1111. continue;
  1112. mnc_len = sep - cred->imsi - 3;
  1113. imsi = cred->imsi;
  1114. #ifdef PCSC_FUNCS
  1115. compare:
  1116. #endif /* PCSC_FUNCS */
  1117. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1118. MACSTR, MAC2STR(bss->bssid));
  1119. ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
  1120. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1121. if (ret) {
  1122. if (selected == NULL ||
  1123. selected->priority < cred->priority)
  1124. selected = cred;
  1125. }
  1126. }
  1127. #endif /* INTERWORKING_3GPP */
  1128. return selected;
  1129. }
  1130. static struct wpa_cred * interworking_credentials_available_realm(
  1131. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1132. {
  1133. struct wpa_cred *cred, *selected = NULL;
  1134. struct nai_realm *realm;
  1135. u16 count, i;
  1136. if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
  1137. return NULL;
  1138. if (wpa_s->conf->cred == NULL)
  1139. return NULL;
  1140. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1141. MACSTR, MAC2STR(bss->bssid));
  1142. realm = nai_realm_parse(bss->anqp->nai_realm, &count);
  1143. if (realm == NULL) {
  1144. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1145. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1146. return NULL;
  1147. }
  1148. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1149. if (cred->realm == NULL)
  1150. continue;
  1151. for (i = 0; i < count; i++) {
  1152. if (!nai_realm_match(&realm[i], cred->realm))
  1153. continue;
  1154. if (nai_realm_find_eap(cred, &realm[i])) {
  1155. if (selected == NULL ||
  1156. selected->priority < cred->priority)
  1157. selected = cred;
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. nai_realm_free(realm, count);
  1163. return selected;
  1164. }
  1165. static struct wpa_cred * interworking_credentials_available(
  1166. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1167. {
  1168. struct wpa_cred *cred, *cred2;
  1169. cred = interworking_credentials_available_realm(wpa_s, bss);
  1170. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1171. if (cred && cred2 && cred2->priority >= cred->priority)
  1172. cred = cred2;
  1173. if (!cred)
  1174. cred = cred2;
  1175. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1176. bss);
  1177. if (cred && cred2 && cred2->priority >= cred->priority)
  1178. cred = cred2;
  1179. if (!cred)
  1180. cred = cred2;
  1181. return cred;
  1182. }
  1183. static int domain_name_list_contains(struct wpabuf *domain_names,
  1184. const char *domain)
  1185. {
  1186. const u8 *pos, *end;
  1187. size_t len;
  1188. len = os_strlen(domain);
  1189. pos = wpabuf_head(domain_names);
  1190. end = pos + wpabuf_len(domain_names);
  1191. while (pos + 1 < end) {
  1192. if (pos + 1 + pos[0] > end)
  1193. break;
  1194. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1195. pos + 1, pos[0]);
  1196. if (pos[0] == len &&
  1197. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1198. return 1;
  1199. pos += 1 + pos[0];
  1200. }
  1201. return 0;
  1202. }
  1203. int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
  1204. struct wpa_cred *cred,
  1205. struct wpabuf *domain_names)
  1206. {
  1207. #ifdef INTERWORKING_3GPP
  1208. char nai[100], *realm;
  1209. char *imsi = NULL;
  1210. int mnc_len = 0;
  1211. if (cred->imsi)
  1212. imsi = cred->imsi;
  1213. #ifdef CONFIG_PCSC
  1214. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1215. wpa_s->scard && wpa_s->imsi[0]) {
  1216. imsi = wpa_s->imsi;
  1217. mnc_len = wpa_s->mnc_len;
  1218. }
  1219. #endif /* CONFIG_PCSC */
  1220. if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
  1221. realm = os_strchr(nai, '@');
  1222. if (realm)
  1223. realm++;
  1224. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1225. "with SIM/USIM domain %s", realm);
  1226. if (realm &&
  1227. domain_name_list_contains(domain_names, realm))
  1228. return 1;
  1229. }
  1230. #endif /* INTERWORKING_3GPP */
  1231. if (cred->domain == NULL)
  1232. return 0;
  1233. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1234. "home SP FQDN %s", cred->domain);
  1235. if (domain_name_list_contains(domain_names, cred->domain))
  1236. return 1;
  1237. return 0;
  1238. }
  1239. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1240. struct wpabuf *domain_names)
  1241. {
  1242. struct wpa_cred *cred;
  1243. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1244. return -1;
  1245. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1246. int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
  1247. if (res)
  1248. return res;
  1249. }
  1250. return 0;
  1251. }
  1252. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1253. {
  1254. struct wpa_bss *bss;
  1255. struct wpa_ssid *ssid;
  1256. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1257. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1258. if (wpas_network_disabled(wpa_s, ssid) ||
  1259. ssid->mode != WPAS_MODE_INFRA)
  1260. continue;
  1261. if (ssid->ssid_len != bss->ssid_len ||
  1262. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1263. 0)
  1264. continue;
  1265. /*
  1266. * TODO: Consider more accurate matching of security
  1267. * configuration similarly to what is done in events.c
  1268. */
  1269. return 1;
  1270. }
  1271. }
  1272. return 0;
  1273. }
  1274. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1275. {
  1276. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1277. int selected_prio = -999999, selected_home_prio = -999999;
  1278. unsigned int count = 0;
  1279. const char *type;
  1280. int res;
  1281. struct wpa_cred *cred;
  1282. wpa_s->network_select = 0;
  1283. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1284. cred = interworking_credentials_available(wpa_s, bss);
  1285. if (!cred)
  1286. continue;
  1287. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1288. /*
  1289. * We currently support only HS 2.0 networks and those
  1290. * are required to use WPA2-Enterprise.
  1291. */
  1292. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1293. "with " MACSTR " but network does not use "
  1294. "RSN", MAC2STR(bss->bssid));
  1295. continue;
  1296. }
  1297. count++;
  1298. res = interworking_home_sp(wpa_s, bss->anqp ?
  1299. bss->anqp->domain_name : NULL);
  1300. if (res > 0)
  1301. type = "home";
  1302. else if (res == 0)
  1303. type = "roaming";
  1304. else
  1305. type = "unknown";
  1306. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1307. MAC2STR(bss->bssid), type);
  1308. if (wpa_s->auto_select ||
  1309. (wpa_s->conf->auto_interworking &&
  1310. wpa_s->auto_network_select)) {
  1311. if (selected == NULL ||
  1312. cred->priority > selected_prio) {
  1313. selected = bss;
  1314. selected_prio = cred->priority;
  1315. }
  1316. if (res > 0 &&
  1317. (selected_home == NULL ||
  1318. cred->priority > selected_home_prio)) {
  1319. selected_home = bss;
  1320. selected_home_prio = cred->priority;
  1321. }
  1322. }
  1323. }
  1324. if (selected_home && selected_home != selected &&
  1325. selected_home_prio >= selected_prio) {
  1326. /* Prefer network operated by the Home SP */
  1327. selected = selected_home;
  1328. }
  1329. if (count == 0) {
  1330. /*
  1331. * No matching network was found based on configured
  1332. * credentials. Check whether any of the enabled network blocks
  1333. * have matching APs.
  1334. */
  1335. if (interworking_find_network_match(wpa_s)) {
  1336. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1337. "match for enabled network configurations");
  1338. if (wpa_s->auto_select)
  1339. interworking_reconnect(wpa_s);
  1340. return;
  1341. }
  1342. if (wpa_s->auto_network_select) {
  1343. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1344. "scanning after ANQP fetch");
  1345. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  1346. 0);
  1347. return;
  1348. }
  1349. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1350. "with matching credentials found");
  1351. }
  1352. if (selected)
  1353. interworking_connect(wpa_s, selected);
  1354. }
  1355. static struct wpa_bss_anqp *
  1356. interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1357. {
  1358. struct wpa_bss *other;
  1359. if (is_zero_ether_addr(bss->hessid))
  1360. return NULL; /* Cannot be in the same homegenous ESS */
  1361. dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
  1362. if (other == bss)
  1363. continue;
  1364. if (other->anqp == NULL)
  1365. continue;
  1366. if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
  1367. continue;
  1368. if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
  1369. continue;
  1370. if (bss->ssid_len != other->ssid_len ||
  1371. os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
  1372. continue;
  1373. wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
  1374. "already fetched BSSID " MACSTR " and " MACSTR,
  1375. MAC2STR(other->bssid), MAC2STR(bss->bssid));
  1376. other->anqp->users++;
  1377. return other->anqp;
  1378. }
  1379. return NULL;
  1380. }
  1381. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1382. {
  1383. struct wpa_bss *bss;
  1384. int found = 0;
  1385. const u8 *ie;
  1386. if (!wpa_s->fetch_anqp_in_progress)
  1387. return;
  1388. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1389. if (!(bss->caps & IEEE80211_CAP_ESS))
  1390. continue;
  1391. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1392. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1393. continue; /* AP does not support Interworking */
  1394. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1395. if (bss->anqp == NULL) {
  1396. bss->anqp = interworking_match_anqp_info(wpa_s,
  1397. bss);
  1398. if (bss->anqp) {
  1399. /* Shared data already fetched */
  1400. continue;
  1401. }
  1402. bss->anqp = wpa_bss_anqp_alloc();
  1403. if (bss->anqp == NULL)
  1404. break;
  1405. }
  1406. found++;
  1407. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1408. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1409. MACSTR, MAC2STR(bss->bssid));
  1410. interworking_anqp_send_req(wpa_s, bss);
  1411. break;
  1412. }
  1413. }
  1414. if (found == 0) {
  1415. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1416. wpa_s->fetch_anqp_in_progress = 0;
  1417. if (wpa_s->network_select)
  1418. interworking_select_network(wpa_s);
  1419. }
  1420. }
  1421. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1422. {
  1423. struct wpa_bss *bss;
  1424. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1425. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1426. wpa_s->fetch_anqp_in_progress = 1;
  1427. interworking_next_anqp_fetch(wpa_s);
  1428. }
  1429. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1430. {
  1431. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1432. return 0;
  1433. wpa_s->network_select = 0;
  1434. wpa_s->fetch_all_anqp = 1;
  1435. interworking_start_fetch_anqp(wpa_s);
  1436. return 0;
  1437. }
  1438. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1439. {
  1440. if (!wpa_s->fetch_anqp_in_progress)
  1441. return;
  1442. wpa_s->fetch_anqp_in_progress = 0;
  1443. }
  1444. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1445. u16 info_ids[], size_t num_ids)
  1446. {
  1447. struct wpabuf *buf;
  1448. int ret = 0;
  1449. int freq;
  1450. struct wpa_bss *bss;
  1451. int res;
  1452. freq = wpa_s->assoc_freq;
  1453. bss = wpa_bss_get_bssid(wpa_s, dst);
  1454. if (bss) {
  1455. wpa_bss_anqp_unshare_alloc(bss);
  1456. freq = bss->freq;
  1457. }
  1458. if (freq <= 0)
  1459. return -1;
  1460. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1461. MAC2STR(dst), (unsigned int) num_ids);
  1462. buf = anqp_build_req(info_ids, num_ids, NULL);
  1463. if (buf == NULL)
  1464. return -1;
  1465. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1466. if (res < 0) {
  1467. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1468. ret = -1;
  1469. } else
  1470. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1471. "%u", res);
  1472. wpabuf_free(buf);
  1473. return ret;
  1474. }
  1475. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1476. const u8 *sa, u16 info_id,
  1477. const u8 *data, size_t slen)
  1478. {
  1479. const u8 *pos = data;
  1480. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1481. struct wpa_bss_anqp *anqp = NULL;
  1482. #ifdef CONFIG_HS20
  1483. u8 type;
  1484. #endif /* CONFIG_HS20 */
  1485. if (bss)
  1486. anqp = bss->anqp;
  1487. switch (info_id) {
  1488. case ANQP_CAPABILITY_LIST:
  1489. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1490. " ANQP Capability list", MAC2STR(sa));
  1491. break;
  1492. case ANQP_VENUE_NAME:
  1493. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1494. " Venue Name", MAC2STR(sa));
  1495. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1496. if (anqp) {
  1497. wpabuf_free(anqp->venue_name);
  1498. anqp->venue_name = wpabuf_alloc_copy(pos, slen);
  1499. }
  1500. break;
  1501. case ANQP_NETWORK_AUTH_TYPE:
  1502. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1503. " Network Authentication Type information",
  1504. MAC2STR(sa));
  1505. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1506. "Type", pos, slen);
  1507. if (anqp) {
  1508. wpabuf_free(anqp->network_auth_type);
  1509. anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
  1510. }
  1511. break;
  1512. case ANQP_ROAMING_CONSORTIUM:
  1513. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1514. " Roaming Consortium list", MAC2STR(sa));
  1515. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1516. pos, slen);
  1517. if (anqp) {
  1518. wpabuf_free(anqp->roaming_consortium);
  1519. anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
  1520. }
  1521. break;
  1522. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1523. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1524. " IP Address Type Availability information",
  1525. MAC2STR(sa));
  1526. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1527. pos, slen);
  1528. if (anqp) {
  1529. wpabuf_free(anqp->ip_addr_type_availability);
  1530. anqp->ip_addr_type_availability =
  1531. wpabuf_alloc_copy(pos, slen);
  1532. }
  1533. break;
  1534. case ANQP_NAI_REALM:
  1535. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1536. " NAI Realm list", MAC2STR(sa));
  1537. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1538. if (anqp) {
  1539. wpabuf_free(anqp->nai_realm);
  1540. anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
  1541. }
  1542. break;
  1543. case ANQP_3GPP_CELLULAR_NETWORK:
  1544. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1545. " 3GPP Cellular Network information", MAC2STR(sa));
  1546. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1547. pos, slen);
  1548. if (anqp) {
  1549. wpabuf_free(anqp->anqp_3gpp);
  1550. anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1551. }
  1552. break;
  1553. case ANQP_DOMAIN_NAME:
  1554. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1555. " Domain Name list", MAC2STR(sa));
  1556. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1557. if (anqp) {
  1558. wpabuf_free(anqp->domain_name);
  1559. anqp->domain_name = wpabuf_alloc_copy(pos, slen);
  1560. }
  1561. break;
  1562. case ANQP_VENDOR_SPECIFIC:
  1563. if (slen < 3)
  1564. return;
  1565. switch (WPA_GET_BE24(pos)) {
  1566. #ifdef CONFIG_HS20
  1567. case OUI_WFA:
  1568. pos += 3;
  1569. slen -= 3;
  1570. if (slen < 1)
  1571. return;
  1572. type = *pos++;
  1573. slen--;
  1574. switch (type) {
  1575. case HS20_ANQP_OUI_TYPE:
  1576. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1577. slen);
  1578. break;
  1579. default:
  1580. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1581. "vendor type %u", type);
  1582. break;
  1583. }
  1584. break;
  1585. #endif /* CONFIG_HS20 */
  1586. default:
  1587. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1588. "vendor-specific ANQP OUI %06x",
  1589. WPA_GET_BE24(pos));
  1590. return;
  1591. }
  1592. break;
  1593. default:
  1594. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1595. "%u", info_id);
  1596. break;
  1597. }
  1598. }
  1599. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1600. enum gas_query_result result,
  1601. const struct wpabuf *adv_proto,
  1602. const struct wpabuf *resp, u16 status_code)
  1603. {
  1604. struct wpa_supplicant *wpa_s = ctx;
  1605. const u8 *pos;
  1606. const u8 *end;
  1607. u16 info_id;
  1608. u16 slen;
  1609. if (result != GAS_QUERY_SUCCESS)
  1610. return;
  1611. pos = wpabuf_head(adv_proto);
  1612. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1613. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1614. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1615. "Protocol in response");
  1616. return;
  1617. }
  1618. pos = wpabuf_head(resp);
  1619. end = pos + wpabuf_len(resp);
  1620. while (pos < end) {
  1621. if (pos + 4 > end) {
  1622. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1623. break;
  1624. }
  1625. info_id = WPA_GET_LE16(pos);
  1626. pos += 2;
  1627. slen = WPA_GET_LE16(pos);
  1628. pos += 2;
  1629. if (pos + slen > end) {
  1630. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1631. "for Info ID %u", info_id);
  1632. break;
  1633. }
  1634. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1635. slen);
  1636. pos += slen;
  1637. }
  1638. }
  1639. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1640. struct wpa_scan_results *scan_res)
  1641. {
  1642. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1643. "ANQP fetch");
  1644. interworking_start_fetch_anqp(wpa_s);
  1645. }
  1646. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1647. {
  1648. interworking_stop_fetch_anqp(wpa_s);
  1649. wpa_s->network_select = 1;
  1650. wpa_s->auto_network_select = 0;
  1651. wpa_s->auto_select = !!auto_select;
  1652. wpa_s->fetch_all_anqp = 0;
  1653. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1654. "selection");
  1655. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1656. wpa_s->scan_req = 2;
  1657. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1658. return 0;
  1659. }
  1660. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1661. enum gas_query_result result,
  1662. const struct wpabuf *adv_proto,
  1663. const struct wpabuf *resp, u16 status_code)
  1664. {
  1665. struct wpa_supplicant *wpa_s = ctx;
  1666. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  1667. " dialog_token=%d status_code=%d resp_len=%d",
  1668. MAC2STR(addr), dialog_token, status_code,
  1669. resp ? (int) wpabuf_len(resp) : -1);
  1670. if (!resp)
  1671. return;
  1672. wpabuf_free(wpa_s->last_gas_resp);
  1673. wpa_s->last_gas_resp = wpabuf_dup(resp);
  1674. if (wpa_s->last_gas_resp == NULL)
  1675. return;
  1676. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  1677. wpa_s->last_gas_dialog_token = dialog_token;
  1678. }
  1679. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1680. const struct wpabuf *adv_proto,
  1681. const struct wpabuf *query)
  1682. {
  1683. struct wpabuf *buf;
  1684. int ret = 0;
  1685. int freq;
  1686. struct wpa_bss *bss;
  1687. int res;
  1688. size_t len;
  1689. u8 query_resp_len_limit = 0, pame_bi = 0;
  1690. freq = wpa_s->assoc_freq;
  1691. bss = wpa_bss_get_bssid(wpa_s, dst);
  1692. if (bss)
  1693. freq = bss->freq;
  1694. if (freq <= 0)
  1695. return -1;
  1696. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  1697. MAC2STR(dst), freq);
  1698. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  1699. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  1700. len = 3 + wpabuf_len(adv_proto) + 2;
  1701. if (query)
  1702. len += wpabuf_len(query);
  1703. buf = gas_build_initial_req(0, len);
  1704. if (buf == NULL)
  1705. return -1;
  1706. /* Advertisement Protocol IE */
  1707. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1708. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  1709. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  1710. (pame_bi ? 0x80 : 0));
  1711. wpabuf_put_buf(buf, adv_proto);
  1712. /* GAS Query */
  1713. if (query) {
  1714. wpabuf_put_le16(buf, wpabuf_len(query));
  1715. wpabuf_put_buf(buf, query);
  1716. } else
  1717. wpabuf_put_le16(buf, 0);
  1718. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  1719. if (res < 0) {
  1720. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  1721. ret = -1;
  1722. } else
  1723. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  1724. "%u", res);
  1725. wpabuf_free(buf);
  1726. return ret;
  1727. }