interworking.c 53 KB

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