interworking.c 52 KB

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