interworking.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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. if (bss->anqp_3gpp == NULL)
  620. return -1;
  621. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  622. char *sep;
  623. const char *imsi;
  624. int mnc_len;
  625. #ifdef PCSC_FUNCS
  626. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  627. wpa_s->imsi[0]) {
  628. imsi = wpa_s->imsi;
  629. mnc_len = wpa_s->mnc_len;
  630. goto compare;
  631. }
  632. #endif /* PCSC_FUNCS */
  633. if (cred->imsi == NULL || !cred->imsi[0] ||
  634. cred->milenage == NULL || !cred->milenage[0])
  635. continue;
  636. sep = os_strchr(cred->imsi, '-');
  637. if (sep == NULL ||
  638. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  639. continue;
  640. mnc_len = sep - cred->imsi - 3;
  641. imsi = cred->imsi;
  642. #ifdef PCSC_FUNCS
  643. compare:
  644. #endif /* PCSC_FUNCS */
  645. if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
  646. break;
  647. }
  648. if (cred == NULL)
  649. return -1;
  650. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  651. if (ie == NULL)
  652. return -1;
  653. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  654. MAC2STR(bss->bssid));
  655. ssid = wpa_config_add_network(wpa_s->conf);
  656. if (ssid == NULL)
  657. return -1;
  658. wpas_notify_network_added(wpa_s, ssid);
  659. wpa_config_set_network_defaults(ssid);
  660. ssid->priority = cred->priority;
  661. ssid->temporary = 1;
  662. ssid->ssid = os_zalloc(ie[1] + 1);
  663. if (ssid->ssid == NULL)
  664. goto fail;
  665. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  666. ssid->ssid_len = ie[1];
  667. if (interworking_set_hs20_params(ssid) < 0)
  668. goto fail;
  669. /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
  670. if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
  671. wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
  672. goto fail;
  673. }
  674. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  675. wpa_config_set(ssid, "eap", "AKA", 0);
  676. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
  677. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  678. goto fail;
  679. }
  680. if (cred->milenage && cred->milenage[0]) {
  681. if (wpa_config_set_quoted(ssid, "password",
  682. cred->milenage) < 0)
  683. goto fail;
  684. } else if (cred->pcsc) {
  685. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  686. goto fail;
  687. if (wpa_s->conf->pcsc_pin &&
  688. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  689. < 0)
  690. goto fail;
  691. }
  692. if (cred->password && cred->password[0] &&
  693. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  694. goto fail;
  695. wpa_config_update_prio_list(wpa_s->conf);
  696. interworking_reconnect(wpa_s);
  697. return 0;
  698. fail:
  699. wpas_notify_network_removed(wpa_s, ssid);
  700. wpa_config_remove_network(wpa_s->conf, ssid->id);
  701. #endif /* INTERWORKING_3GPP */
  702. return -1;
  703. }
  704. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  705. size_t rc_len)
  706. {
  707. const u8 *pos, *end;
  708. u8 lens;
  709. if (ie == NULL)
  710. return 0;
  711. pos = ie + 2;
  712. end = ie + 2 + ie[1];
  713. /* Roaming Consortium element:
  714. * Number of ANQP OIs
  715. * OI #1 and #2 lengths
  716. * OI #1, [OI #2], [OI #3]
  717. */
  718. if (pos + 2 > end)
  719. return 0;
  720. pos++; /* skip Number of ANQP OIs */
  721. lens = *pos++;
  722. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  723. return 0;
  724. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  725. return 1;
  726. pos += lens & 0x0f;
  727. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  728. return 1;
  729. pos += lens >> 4;
  730. if (pos < end && (size_t) (end - pos) == rc_len &&
  731. os_memcmp(pos, rc_id, rc_len) == 0)
  732. return 1;
  733. return 0;
  734. }
  735. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  736. const u8 *rc_id, size_t rc_len)
  737. {
  738. const u8 *pos, *end;
  739. u8 len;
  740. if (anqp == NULL)
  741. return 0;
  742. pos = wpabuf_head(anqp);
  743. end = pos + wpabuf_len(anqp);
  744. /* Set of <OI Length, OI> duples */
  745. while (pos < end) {
  746. len = *pos++;
  747. if (pos + len > end)
  748. break;
  749. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  750. return 1;
  751. pos += len;
  752. }
  753. return 0;
  754. }
  755. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  756. const u8 *rc_id, size_t rc_len)
  757. {
  758. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  759. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  760. }
  761. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  762. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  763. {
  764. struct wpa_cred *cred, *selected = NULL;
  765. const u8 *ie;
  766. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  767. if (ie == NULL && bss->anqp_roaming_consortium == NULL)
  768. return NULL;
  769. if (wpa_s->conf->cred == NULL)
  770. return NULL;
  771. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  772. if (cred->roaming_consortium_len == 0)
  773. continue;
  774. if (!roaming_consortium_match(ie, bss->anqp_roaming_consortium,
  775. cred->roaming_consortium,
  776. cred->roaming_consortium_len))
  777. continue;
  778. if (selected == NULL ||
  779. selected->priority < cred->priority)
  780. selected = cred;
  781. }
  782. return selected;
  783. }
  784. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  785. struct wpa_cred *cred, int ttls)
  786. {
  787. if (cred->eap_method) {
  788. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  789. cred->eap_method->method == EAP_TYPE_TTLS;
  790. os_free(ssid->eap.eap_methods);
  791. ssid->eap.eap_methods =
  792. os_malloc(sizeof(struct eap_method_type) * 2);
  793. if (ssid->eap.eap_methods == NULL)
  794. return -1;
  795. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  796. sizeof(*cred->eap_method));
  797. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  798. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  799. }
  800. if (ttls && cred->username && cred->username[0]) {
  801. const char *pos;
  802. char *anon;
  803. /* Use anonymous NAI in Phase 1 */
  804. pos = os_strchr(cred->username, '@');
  805. if (pos) {
  806. size_t buflen = 9 + os_strlen(pos) + 1;
  807. anon = os_malloc(buflen);
  808. if (anon == NULL)
  809. return -1;
  810. os_snprintf(anon, buflen, "anonymous%s", pos);
  811. } else if (cred->realm) {
  812. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  813. anon = os_malloc(buflen);
  814. if (anon == NULL)
  815. return -1;
  816. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  817. } else {
  818. anon = os_strdup("anonymous");
  819. if (anon == NULL)
  820. return -1;
  821. }
  822. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  823. 0) {
  824. os_free(anon);
  825. return -1;
  826. }
  827. os_free(anon);
  828. }
  829. if (cred->username && cred->username[0] &&
  830. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  831. return -1;
  832. if (cred->password && cred->password[0]) {
  833. if (cred->ext_password &&
  834. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  835. return -1;
  836. if (!cred->ext_password &&
  837. wpa_config_set_quoted(ssid, "password", cred->password) <
  838. 0)
  839. return -1;
  840. }
  841. if (cred->client_cert && cred->client_cert[0] &&
  842. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  843. return -1;
  844. if (cred->private_key && cred->private_key[0] &&
  845. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  846. return -1;
  847. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  848. wpa_config_set_quoted(ssid, "private_key_passwd",
  849. cred->private_key_passwd) < 0)
  850. return -1;
  851. if (cred->phase1) {
  852. os_free(ssid->eap.phase1);
  853. ssid->eap.phase1 = os_strdup(cred->phase1);
  854. }
  855. if (cred->phase2) {
  856. os_free(ssid->eap.phase2);
  857. ssid->eap.phase2 = os_strdup(cred->phase2);
  858. }
  859. if (cred->ca_cert && cred->ca_cert[0] &&
  860. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  861. return -1;
  862. return 0;
  863. }
  864. static int interworking_connect_roaming_consortium(
  865. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  866. struct wpa_bss *bss, const u8 *ssid_ie)
  867. {
  868. struct wpa_ssid *ssid;
  869. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  870. "roaming consortium match", MAC2STR(bss->bssid));
  871. ssid = wpa_config_add_network(wpa_s->conf);
  872. if (ssid == NULL)
  873. return -1;
  874. wpas_notify_network_added(wpa_s, ssid);
  875. wpa_config_set_network_defaults(ssid);
  876. ssid->priority = cred->priority;
  877. ssid->temporary = 1;
  878. ssid->ssid = os_zalloc(ssid_ie[1] + 1);
  879. if (ssid->ssid == NULL)
  880. goto fail;
  881. os_memcpy(ssid->ssid, ssid_ie + 2, ssid_ie[1]);
  882. ssid->ssid_len = ssid_ie[1];
  883. if (interworking_set_hs20_params(ssid) < 0)
  884. goto fail;
  885. if (cred->eap_method == NULL) {
  886. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  887. "credential using roaming consortium");
  888. goto fail;
  889. }
  890. if (interworking_set_eap_params(
  891. ssid, cred,
  892. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  893. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  894. goto fail;
  895. wpa_config_update_prio_list(wpa_s->conf);
  896. interworking_reconnect(wpa_s);
  897. return 0;
  898. fail:
  899. wpas_notify_network_removed(wpa_s, ssid);
  900. wpa_config_remove_network(wpa_s->conf, ssid->id);
  901. return -1;
  902. }
  903. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  904. {
  905. struct wpa_cred *cred;
  906. struct wpa_ssid *ssid;
  907. struct nai_realm *realm;
  908. struct nai_realm_eap *eap = NULL;
  909. u16 count, i;
  910. char buf[100];
  911. const u8 *ie;
  912. if (wpa_s->conf->cred == NULL || bss == NULL)
  913. return -1;
  914. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  915. if (ie == NULL || ie[1] == 0) {
  916. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  917. MACSTR, MAC2STR(bss->bssid));
  918. return -1;
  919. }
  920. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  921. /*
  922. * We currently support only HS 2.0 networks and those are
  923. * required to use WPA2-Enterprise.
  924. */
  925. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  926. "RSN");
  927. return -1;
  928. }
  929. cred = interworking_credentials_available_roaming_consortium(wpa_s,
  930. bss);
  931. if (cred)
  932. return interworking_connect_roaming_consortium(wpa_s, cred,
  933. bss, ie);
  934. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  935. if (realm == NULL) {
  936. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  937. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  938. count = 0;
  939. }
  940. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  941. for (i = 0; i < count; i++) {
  942. if (!nai_realm_match(&realm[i], cred->realm))
  943. continue;
  944. eap = nai_realm_find_eap(cred, &realm[i]);
  945. if (eap)
  946. break;
  947. }
  948. if (eap)
  949. break;
  950. }
  951. if (!eap) {
  952. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  953. if (realm)
  954. nai_realm_free(realm, count);
  955. return 0;
  956. }
  957. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  958. "and EAP method found for " MACSTR,
  959. MAC2STR(bss->bssid));
  960. nai_realm_free(realm, count);
  961. return -1;
  962. }
  963. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  964. MAC2STR(bss->bssid));
  965. ssid = wpa_config_add_network(wpa_s->conf);
  966. if (ssid == NULL) {
  967. nai_realm_free(realm, count);
  968. return -1;
  969. }
  970. wpas_notify_network_added(wpa_s, ssid);
  971. wpa_config_set_network_defaults(ssid);
  972. ssid->priority = cred->priority;
  973. ssid->temporary = 1;
  974. ssid->ssid = os_zalloc(ie[1] + 1);
  975. if (ssid->ssid == NULL)
  976. goto fail;
  977. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  978. ssid->ssid_len = ie[1];
  979. if (interworking_set_hs20_params(ssid) < 0)
  980. goto fail;
  981. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  982. eap->method), 0) < 0)
  983. goto fail;
  984. switch (eap->method) {
  985. case EAP_TYPE_TTLS:
  986. if (eap->inner_method) {
  987. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  988. eap_get_name(EAP_VENDOR_IETF,
  989. eap->inner_method));
  990. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  991. goto fail;
  992. break;
  993. }
  994. switch (eap->inner_non_eap) {
  995. case NAI_REALM_INNER_NON_EAP_PAP:
  996. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  997. 0)
  998. goto fail;
  999. break;
  1000. case NAI_REALM_INNER_NON_EAP_CHAP:
  1001. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1002. < 0)
  1003. goto fail;
  1004. break;
  1005. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1006. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1007. 0) < 0)
  1008. goto fail;
  1009. break;
  1010. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1011. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1012. 0) < 0)
  1013. goto fail;
  1014. break;
  1015. }
  1016. break;
  1017. case EAP_TYPE_PEAP:
  1018. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1019. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  1020. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1021. goto fail;
  1022. break;
  1023. case EAP_TYPE_TLS:
  1024. break;
  1025. }
  1026. if (interworking_set_eap_params(ssid, cred,
  1027. eap->method == EAP_TYPE_TTLS) < 0)
  1028. goto fail;
  1029. nai_realm_free(realm, count);
  1030. wpa_config_update_prio_list(wpa_s->conf);
  1031. interworking_reconnect(wpa_s);
  1032. return 0;
  1033. fail:
  1034. wpas_notify_network_removed(wpa_s, ssid);
  1035. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1036. nai_realm_free(realm, count);
  1037. return -1;
  1038. }
  1039. static struct wpa_cred * interworking_credentials_available_3gpp(
  1040. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1041. {
  1042. struct wpa_cred *cred, *selected = NULL;
  1043. int ret;
  1044. #ifdef INTERWORKING_3GPP
  1045. if (bss->anqp_3gpp == NULL)
  1046. return NULL;
  1047. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1048. char *sep;
  1049. const char *imsi;
  1050. int mnc_len;
  1051. #ifdef PCSC_FUNCS
  1052. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1053. wpa_s->imsi[0]) {
  1054. imsi = wpa_s->imsi;
  1055. mnc_len = wpa_s->mnc_len;
  1056. goto compare;
  1057. }
  1058. #endif /* PCSC_FUNCS */
  1059. if (cred->imsi == NULL || !cred->imsi[0] ||
  1060. cred->milenage == NULL || !cred->milenage[0])
  1061. continue;
  1062. sep = os_strchr(cred->imsi, '-');
  1063. if (sep == NULL ||
  1064. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1065. continue;
  1066. mnc_len = sep - cred->imsi - 3;
  1067. imsi = cred->imsi;
  1068. #ifdef PCSC_FUNCS
  1069. compare:
  1070. #endif /* PCSC_FUNCS */
  1071. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1072. MACSTR, MAC2STR(bss->bssid));
  1073. ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
  1074. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1075. if (ret) {
  1076. if (selected == NULL ||
  1077. selected->priority < cred->priority)
  1078. selected = cred;
  1079. }
  1080. }
  1081. #endif /* INTERWORKING_3GPP */
  1082. return selected;
  1083. }
  1084. static struct wpa_cred * interworking_credentials_available_realm(
  1085. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1086. {
  1087. struct wpa_cred *cred, *selected = NULL;
  1088. struct nai_realm *realm;
  1089. u16 count, i;
  1090. if (bss->anqp_nai_realm == NULL)
  1091. return NULL;
  1092. if (wpa_s->conf->cred == NULL)
  1093. return NULL;
  1094. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1095. MACSTR, MAC2STR(bss->bssid));
  1096. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  1097. if (realm == NULL) {
  1098. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1099. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1100. return NULL;
  1101. }
  1102. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1103. if (cred->realm == NULL)
  1104. continue;
  1105. for (i = 0; i < count; i++) {
  1106. if (!nai_realm_match(&realm[i], cred->realm))
  1107. continue;
  1108. if (nai_realm_find_eap(cred, &realm[i])) {
  1109. if (selected == NULL ||
  1110. selected->priority < cred->priority)
  1111. selected = cred;
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. nai_realm_free(realm, count);
  1117. return selected;
  1118. }
  1119. static struct wpa_cred * interworking_credentials_available(
  1120. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1121. {
  1122. struct wpa_cred *cred, *cred2;
  1123. cred = interworking_credentials_available_realm(wpa_s, bss);
  1124. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  1125. if (cred && cred2 && cred2->priority >= cred->priority)
  1126. cred = cred2;
  1127. if (!cred)
  1128. cred = cred2;
  1129. cred2 = interworking_credentials_available_roaming_consortium(wpa_s,
  1130. bss);
  1131. if (cred && cred2 && cred2->priority >= cred->priority)
  1132. cred = cred2;
  1133. if (!cred)
  1134. cred = cred2;
  1135. return cred;
  1136. }
  1137. static int domain_name_list_contains(struct wpabuf *domain_names,
  1138. const char *domain)
  1139. {
  1140. const u8 *pos, *end;
  1141. size_t len;
  1142. len = os_strlen(domain);
  1143. pos = wpabuf_head(domain_names);
  1144. end = pos + wpabuf_len(domain_names);
  1145. while (pos + 1 < end) {
  1146. if (pos + 1 + pos[0] > end)
  1147. break;
  1148. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1149. pos + 1, pos[0]);
  1150. if (pos[0] == len &&
  1151. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1152. return 1;
  1153. pos += 1 + pos[0];
  1154. }
  1155. return 0;
  1156. }
  1157. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1158. struct wpabuf *domain_names)
  1159. {
  1160. struct wpa_cred *cred;
  1161. #ifdef INTERWORKING_3GPP
  1162. char nai[100], *realm;
  1163. #endif /* INTERWORKING_3GPP */
  1164. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1165. return -1;
  1166. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1167. #ifdef INTERWORKING_3GPP
  1168. char *imsi = NULL;
  1169. int mnc_len = 0;
  1170. if (cred->imsi)
  1171. imsi = cred->imsi;
  1172. #ifdef CONFIG_PCSC
  1173. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1174. wpa_s->scard && wpa_s->imsi[0]) {
  1175. imsi = wpa_s->imsi;
  1176. mnc_len = wpa_s->mnc_len;
  1177. }
  1178. #endif /* CONFIG_PCSC */
  1179. if (imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0)
  1180. == 0) {
  1181. realm = os_strchr(nai, '@');
  1182. if (realm)
  1183. realm++;
  1184. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1185. "with SIM/USIM domain %s", realm);
  1186. if (realm &&
  1187. domain_name_list_contains(domain_names, realm))
  1188. return 1;
  1189. }
  1190. #endif /* INTERWORKING_3GPP */
  1191. if (cred->domain == NULL)
  1192. continue;
  1193. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1194. "home SP FQDN %s", cred->domain);
  1195. if (domain_name_list_contains(domain_names, cred->domain))
  1196. return 1;
  1197. }
  1198. return 0;
  1199. }
  1200. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1201. {
  1202. struct wpa_bss *bss;
  1203. struct wpa_ssid *ssid;
  1204. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1205. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1206. if (wpas_network_disabled(wpa_s, ssid) ||
  1207. ssid->mode != WPAS_MODE_INFRA)
  1208. continue;
  1209. if (ssid->ssid_len != bss->ssid_len ||
  1210. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1211. 0)
  1212. continue;
  1213. /*
  1214. * TODO: Consider more accurate matching of security
  1215. * configuration similarly to what is done in events.c
  1216. */
  1217. return 1;
  1218. }
  1219. }
  1220. return 0;
  1221. }
  1222. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1223. {
  1224. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1225. int selected_prio = -999999, selected_home_prio = -999999;
  1226. unsigned int count = 0;
  1227. const char *type;
  1228. int res;
  1229. struct wpa_cred *cred;
  1230. wpa_s->network_select = 0;
  1231. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1232. cred = interworking_credentials_available(wpa_s, bss);
  1233. if (!cred)
  1234. continue;
  1235. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1236. /*
  1237. * We currently support only HS 2.0 networks and those
  1238. * are required to use WPA2-Enterprise.
  1239. */
  1240. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1241. "with " MACSTR " but network does not use "
  1242. "RSN", MAC2STR(bss->bssid));
  1243. continue;
  1244. }
  1245. count++;
  1246. res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
  1247. if (res > 0)
  1248. type = "home";
  1249. else if (res == 0)
  1250. type = "roaming";
  1251. else
  1252. type = "unknown";
  1253. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  1254. MAC2STR(bss->bssid), type);
  1255. if (wpa_s->auto_select ||
  1256. (wpa_s->conf->auto_interworking &&
  1257. wpa_s->auto_network_select)) {
  1258. if (selected == NULL ||
  1259. cred->priority > selected_prio) {
  1260. selected = bss;
  1261. selected_prio = cred->priority;
  1262. }
  1263. if (res > 0 &&
  1264. (selected_home == NULL ||
  1265. cred->priority > selected_home_prio)) {
  1266. selected_home = bss;
  1267. selected_home_prio = cred->priority;
  1268. }
  1269. }
  1270. }
  1271. if (selected_home && selected_home != selected &&
  1272. selected_home_prio >= selected_prio) {
  1273. /* Prefer network operated by the Home SP */
  1274. selected = selected_home;
  1275. }
  1276. if (count == 0) {
  1277. /*
  1278. * No matching network was found based on configured
  1279. * credentials. Check whether any of the enabled network blocks
  1280. * have matching APs.
  1281. */
  1282. if (interworking_find_network_match(wpa_s)) {
  1283. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1284. "match for enabled network configurations");
  1285. if (wpa_s->auto_select)
  1286. interworking_reconnect(wpa_s);
  1287. return;
  1288. }
  1289. if (wpa_s->auto_network_select) {
  1290. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1291. "scanning after ANQP fetch");
  1292. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  1293. 0);
  1294. return;
  1295. }
  1296. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  1297. "with matching credentials found");
  1298. }
  1299. if (selected)
  1300. interworking_connect(wpa_s, selected);
  1301. }
  1302. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  1303. {
  1304. struct wpa_bss *bss;
  1305. int found = 0;
  1306. const u8 *ie;
  1307. if (!wpa_s->fetch_anqp_in_progress)
  1308. return;
  1309. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1310. if (!(bss->caps & IEEE80211_CAP_ESS))
  1311. continue;
  1312. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  1313. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  1314. continue; /* AP does not support Interworking */
  1315. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  1316. found++;
  1317. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  1318. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  1319. MACSTR, MAC2STR(bss->bssid));
  1320. interworking_anqp_send_req(wpa_s, bss);
  1321. break;
  1322. }
  1323. }
  1324. if (found == 0) {
  1325. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  1326. wpa_s->fetch_anqp_in_progress = 0;
  1327. if (wpa_s->network_select)
  1328. interworking_select_network(wpa_s);
  1329. }
  1330. }
  1331. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1332. {
  1333. struct wpa_bss *bss;
  1334. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1335. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1336. wpa_s->fetch_anqp_in_progress = 1;
  1337. interworking_next_anqp_fetch(wpa_s);
  1338. }
  1339. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1340. {
  1341. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1342. return 0;
  1343. wpa_s->network_select = 0;
  1344. wpa_s->fetch_all_anqp = 1;
  1345. interworking_start_fetch_anqp(wpa_s);
  1346. return 0;
  1347. }
  1348. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1349. {
  1350. if (!wpa_s->fetch_anqp_in_progress)
  1351. return;
  1352. wpa_s->fetch_anqp_in_progress = 0;
  1353. }
  1354. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1355. u16 info_ids[], size_t num_ids)
  1356. {
  1357. struct wpabuf *buf;
  1358. int ret = 0;
  1359. int freq;
  1360. struct wpa_bss *bss;
  1361. int res;
  1362. freq = wpa_s->assoc_freq;
  1363. bss = wpa_bss_get_bssid(wpa_s, dst);
  1364. if (bss)
  1365. freq = bss->freq;
  1366. if (freq <= 0)
  1367. return -1;
  1368. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1369. MAC2STR(dst), (unsigned int) num_ids);
  1370. buf = anqp_build_req(info_ids, num_ids, NULL);
  1371. if (buf == NULL)
  1372. return -1;
  1373. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1374. if (res < 0) {
  1375. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1376. ret = -1;
  1377. } else
  1378. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1379. "%u", res);
  1380. wpabuf_free(buf);
  1381. return ret;
  1382. }
  1383. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1384. const u8 *sa, u16 info_id,
  1385. const u8 *data, size_t slen)
  1386. {
  1387. const u8 *pos = data;
  1388. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1389. #ifdef CONFIG_HS20
  1390. u8 type;
  1391. #endif /* CONFIG_HS20 */
  1392. switch (info_id) {
  1393. case ANQP_CAPABILITY_LIST:
  1394. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1395. " ANQP Capability list", MAC2STR(sa));
  1396. break;
  1397. case ANQP_VENUE_NAME:
  1398. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1399. " Venue Name", MAC2STR(sa));
  1400. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1401. if (bss) {
  1402. wpabuf_free(bss->anqp_venue_name);
  1403. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  1404. }
  1405. break;
  1406. case ANQP_NETWORK_AUTH_TYPE:
  1407. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1408. " Network Authentication Type information",
  1409. MAC2STR(sa));
  1410. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1411. "Type", pos, slen);
  1412. if (bss) {
  1413. wpabuf_free(bss->anqp_network_auth_type);
  1414. bss->anqp_network_auth_type =
  1415. wpabuf_alloc_copy(pos, slen);
  1416. }
  1417. break;
  1418. case ANQP_ROAMING_CONSORTIUM:
  1419. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1420. " Roaming Consortium list", MAC2STR(sa));
  1421. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1422. pos, slen);
  1423. if (bss) {
  1424. wpabuf_free(bss->anqp_roaming_consortium);
  1425. bss->anqp_roaming_consortium =
  1426. wpabuf_alloc_copy(pos, slen);
  1427. }
  1428. break;
  1429. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1430. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1431. " IP Address Type Availability information",
  1432. MAC2STR(sa));
  1433. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1434. pos, slen);
  1435. if (bss) {
  1436. wpabuf_free(bss->anqp_ip_addr_type_availability);
  1437. bss->anqp_ip_addr_type_availability =
  1438. wpabuf_alloc_copy(pos, slen);
  1439. }
  1440. break;
  1441. case ANQP_NAI_REALM:
  1442. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1443. " NAI Realm list", MAC2STR(sa));
  1444. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1445. if (bss) {
  1446. wpabuf_free(bss->anqp_nai_realm);
  1447. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  1448. }
  1449. break;
  1450. case ANQP_3GPP_CELLULAR_NETWORK:
  1451. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1452. " 3GPP Cellular Network information", MAC2STR(sa));
  1453. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1454. pos, slen);
  1455. if (bss) {
  1456. wpabuf_free(bss->anqp_3gpp);
  1457. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1458. }
  1459. break;
  1460. case ANQP_DOMAIN_NAME:
  1461. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1462. " Domain Name list", MAC2STR(sa));
  1463. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1464. if (bss) {
  1465. wpabuf_free(bss->anqp_domain_name);
  1466. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  1467. }
  1468. break;
  1469. case ANQP_VENDOR_SPECIFIC:
  1470. if (slen < 3)
  1471. return;
  1472. switch (WPA_GET_BE24(pos)) {
  1473. #ifdef CONFIG_HS20
  1474. case OUI_WFA:
  1475. pos += 3;
  1476. slen -= 3;
  1477. if (slen < 1)
  1478. return;
  1479. type = *pos++;
  1480. slen--;
  1481. switch (type) {
  1482. case HS20_ANQP_OUI_TYPE:
  1483. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  1484. slen);
  1485. break;
  1486. default:
  1487. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  1488. "vendor type %u", type);
  1489. break;
  1490. }
  1491. break;
  1492. #endif /* CONFIG_HS20 */
  1493. default:
  1494. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1495. "vendor-specific ANQP OUI %06x",
  1496. WPA_GET_BE24(pos));
  1497. return;
  1498. }
  1499. break;
  1500. default:
  1501. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1502. "%u", info_id);
  1503. break;
  1504. }
  1505. }
  1506. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1507. enum gas_query_result result,
  1508. const struct wpabuf *adv_proto,
  1509. const struct wpabuf *resp, u16 status_code)
  1510. {
  1511. struct wpa_supplicant *wpa_s = ctx;
  1512. const u8 *pos;
  1513. const u8 *end;
  1514. u16 info_id;
  1515. u16 slen;
  1516. if (result != GAS_QUERY_SUCCESS)
  1517. return;
  1518. pos = wpabuf_head(adv_proto);
  1519. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1520. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1521. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1522. "Protocol in response");
  1523. return;
  1524. }
  1525. pos = wpabuf_head(resp);
  1526. end = pos + wpabuf_len(resp);
  1527. while (pos < end) {
  1528. if (pos + 4 > end) {
  1529. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1530. break;
  1531. }
  1532. info_id = WPA_GET_LE16(pos);
  1533. pos += 2;
  1534. slen = WPA_GET_LE16(pos);
  1535. pos += 2;
  1536. if (pos + slen > end) {
  1537. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1538. "for Info ID %u", info_id);
  1539. break;
  1540. }
  1541. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1542. slen);
  1543. pos += slen;
  1544. }
  1545. }
  1546. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1547. struct wpa_scan_results *scan_res)
  1548. {
  1549. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1550. "ANQP fetch");
  1551. interworking_start_fetch_anqp(wpa_s);
  1552. }
  1553. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1554. {
  1555. interworking_stop_fetch_anqp(wpa_s);
  1556. wpa_s->network_select = 1;
  1557. wpa_s->auto_network_select = 0;
  1558. wpa_s->auto_select = !!auto_select;
  1559. wpa_s->fetch_all_anqp = 0;
  1560. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1561. "selection");
  1562. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1563. wpa_s->scan_req = 2;
  1564. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1565. return 0;
  1566. }
  1567. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1568. enum gas_query_result result,
  1569. const struct wpabuf *adv_proto,
  1570. const struct wpabuf *resp, u16 status_code)
  1571. {
  1572. struct wpa_supplicant *wpa_s = ctx;
  1573. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  1574. " dialog_token=%d status_code=%d resp_len=%d",
  1575. MAC2STR(addr), dialog_token, status_code,
  1576. resp ? (int) wpabuf_len(resp) : -1);
  1577. if (!resp)
  1578. return;
  1579. wpabuf_free(wpa_s->last_gas_resp);
  1580. wpa_s->last_gas_resp = wpabuf_dup(resp);
  1581. if (wpa_s->last_gas_resp == NULL)
  1582. return;
  1583. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  1584. wpa_s->last_gas_dialog_token = dialog_token;
  1585. }
  1586. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1587. const struct wpabuf *adv_proto,
  1588. const struct wpabuf *query)
  1589. {
  1590. struct wpabuf *buf;
  1591. int ret = 0;
  1592. int freq;
  1593. struct wpa_bss *bss;
  1594. int res;
  1595. size_t len;
  1596. u8 query_resp_len_limit = 0, pame_bi = 0;
  1597. freq = wpa_s->assoc_freq;
  1598. bss = wpa_bss_get_bssid(wpa_s, dst);
  1599. if (bss)
  1600. freq = bss->freq;
  1601. if (freq <= 0)
  1602. return -1;
  1603. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  1604. MAC2STR(dst), freq);
  1605. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  1606. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  1607. len = 3 + wpabuf_len(adv_proto) + 2;
  1608. if (query)
  1609. len += wpabuf_len(query);
  1610. buf = gas_build_initial_req(0, len);
  1611. if (buf == NULL)
  1612. return -1;
  1613. /* Advertisement Protocol IE */
  1614. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1615. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  1616. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  1617. (pame_bi ? 0x80 : 0));
  1618. wpabuf_put_buf(buf, adv_proto);
  1619. /* GAS Query */
  1620. if (query) {
  1621. wpabuf_put_le16(buf, wpabuf_len(query));
  1622. wpabuf_put_buf(buf, query);
  1623. } else
  1624. wpabuf_put_le16(buf, 0);
  1625. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  1626. if (res < 0) {
  1627. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  1628. ret = -1;
  1629. } else
  1630. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  1631. "%u", res);
  1632. wpabuf_free(buf);
  1633. return ret;
  1634. }