interworking.c 48 KB

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