interworking.c 49 KB

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