interworking.c 48 KB

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