interworking.c 50 KB

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