interworking.c 47 KB

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