interworking.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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 "drivers/driver.h"
  14. #include "eap_common/eap_defs.h"
  15. #include "eap_peer/eap_methods.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "config.h"
  18. #include "config_ssid.h"
  19. #include "bss.h"
  20. #include "scan.h"
  21. #include "notify.h"
  22. #include "gas_query.h"
  23. #include "interworking.h"
  24. #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
  25. #define INTERWORKING_3GPP
  26. #else
  27. #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
  28. #define INTERWORKING_3GPP
  29. #else
  30. #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
  31. #define INTERWORKING_3GPP
  32. #endif
  33. #endif
  34. #endif
  35. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  36. static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
  37. struct wpabuf *extra)
  38. {
  39. struct wpabuf *buf;
  40. size_t i;
  41. u8 *len_pos;
  42. buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
  43. (extra ? wpabuf_len(extra) : 0));
  44. if (buf == NULL)
  45. return NULL;
  46. len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
  47. for (i = 0; i < num_ids; i++)
  48. wpabuf_put_le16(buf, info_ids[i]);
  49. gas_anqp_set_element_len(buf, len_pos);
  50. if (extra)
  51. wpabuf_put_buf(buf, extra);
  52. gas_anqp_set_len(buf);
  53. return buf;
  54. }
  55. static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
  56. u8 dialog_token,
  57. enum gas_query_result result,
  58. const struct wpabuf *adv_proto,
  59. const struct wpabuf *resp,
  60. u16 status_code)
  61. {
  62. struct wpa_supplicant *wpa_s = ctx;
  63. anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
  64. status_code);
  65. interworking_next_anqp_fetch(wpa_s);
  66. }
  67. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  68. struct wpa_bss *bss)
  69. {
  70. struct wpabuf *buf;
  71. int ret = 0;
  72. int res;
  73. u16 info_ids[] = {
  74. ANQP_CAPABILITY_LIST,
  75. ANQP_VENUE_NAME,
  76. ANQP_NETWORK_AUTH_TYPE,
  77. ANQP_ROAMING_CONSORTIUM,
  78. ANQP_IP_ADDR_TYPE_AVAILABILITY,
  79. ANQP_NAI_REALM,
  80. ANQP_3GPP_CELLULAR_NETWORK,
  81. ANQP_DOMAIN_NAME
  82. };
  83. struct wpabuf *extra = NULL;
  84. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  85. MAC2STR(bss->bssid));
  86. buf = anqp_build_req(info_ids, sizeof(info_ids) / sizeof(info_ids[0]),
  87. extra);
  88. wpabuf_free(extra);
  89. if (buf == NULL)
  90. return -1;
  91. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  92. interworking_anqp_resp_cb, wpa_s);
  93. if (res < 0) {
  94. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  95. ret = -1;
  96. } else
  97. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  98. "%u", res);
  99. wpabuf_free(buf);
  100. return ret;
  101. }
  102. struct nai_realm_eap {
  103. u8 method;
  104. u8 inner_method;
  105. enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
  106. u8 cred_type;
  107. u8 tunneled_cred_type;
  108. };
  109. struct nai_realm {
  110. u8 encoding;
  111. char *realm;
  112. u8 eap_count;
  113. struct nai_realm_eap *eap;
  114. };
  115. static void nai_realm_free(struct nai_realm *realms, u16 count)
  116. {
  117. u16 i;
  118. if (realms == NULL)
  119. return;
  120. for (i = 0; i < count; i++) {
  121. os_free(realms[i].eap);
  122. os_free(realms[i].realm);
  123. }
  124. os_free(realms);
  125. }
  126. static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
  127. const u8 *end)
  128. {
  129. u8 elen, auth_count, a;
  130. const u8 *e_end;
  131. if (pos + 3 > end) {
  132. wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
  133. return NULL;
  134. }
  135. elen = *pos++;
  136. if (pos + elen > end || elen < 2) {
  137. wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
  138. return NULL;
  139. }
  140. e_end = pos + elen;
  141. e->method = *pos++;
  142. auth_count = *pos++;
  143. wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
  144. elen, e->method, auth_count);
  145. for (a = 0; a < auth_count; a++) {
  146. u8 id, len;
  147. if (pos + 2 > end || pos + 2 + pos[1] > end) {
  148. wpa_printf(MSG_DEBUG, "No room for Authentication "
  149. "Parameter subfield");
  150. return NULL;
  151. }
  152. id = *pos++;
  153. len = *pos++;
  154. switch (id) {
  155. case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
  156. if (len < 1)
  157. break;
  158. e->inner_non_eap = *pos;
  159. if (e->method != EAP_TYPE_TTLS)
  160. break;
  161. switch (*pos) {
  162. case NAI_REALM_INNER_NON_EAP_PAP:
  163. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
  164. break;
  165. case NAI_REALM_INNER_NON_EAP_CHAP:
  166. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
  167. break;
  168. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  169. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
  170. break;
  171. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  172. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
  173. break;
  174. }
  175. break;
  176. case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
  177. if (len < 1)
  178. break;
  179. e->inner_method = *pos;
  180. wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
  181. e->inner_method);
  182. break;
  183. case NAI_REALM_EAP_AUTH_CRED_TYPE:
  184. if (len < 1)
  185. break;
  186. e->cred_type = *pos;
  187. wpa_printf(MSG_DEBUG, "Credential Type: %u",
  188. e->cred_type);
  189. break;
  190. case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
  191. if (len < 1)
  192. break;
  193. e->tunneled_cred_type = *pos;
  194. wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
  195. "Type: %u", e->tunneled_cred_type);
  196. break;
  197. default:
  198. wpa_printf(MSG_DEBUG, "Unsupported Authentication "
  199. "Parameter: id=%u len=%u", id, len);
  200. wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
  201. "Value", pos, len);
  202. break;
  203. }
  204. pos += len;
  205. }
  206. return e_end;
  207. }
  208. static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
  209. const u8 *end)
  210. {
  211. u16 len;
  212. const u8 *f_end;
  213. u8 realm_len, e;
  214. if (end - pos < 4) {
  215. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  216. "fixed fields");
  217. return NULL;
  218. }
  219. len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
  220. pos += 2;
  221. if (pos + len > end || len < 3) {
  222. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  223. "(len=%u; left=%u)",
  224. len, (unsigned int) (end - pos));
  225. return NULL;
  226. }
  227. f_end = pos + len;
  228. r->encoding = *pos++;
  229. realm_len = *pos++;
  230. if (pos + realm_len > f_end) {
  231. wpa_printf(MSG_DEBUG, "No room for NAI Realm "
  232. "(len=%u; left=%u)",
  233. realm_len, (unsigned int) (f_end - pos));
  234. return NULL;
  235. }
  236. wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
  237. r->realm = os_malloc(realm_len + 1);
  238. if (r->realm == NULL)
  239. return NULL;
  240. os_memcpy(r->realm, pos, realm_len);
  241. r->realm[realm_len] = '\0';
  242. pos += realm_len;
  243. if (pos + 1 > f_end) {
  244. wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
  245. return NULL;
  246. }
  247. r->eap_count = *pos++;
  248. wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
  249. if (pos + r->eap_count * 3 > f_end) {
  250. wpa_printf(MSG_DEBUG, "No room for EAP Methods");
  251. return NULL;
  252. }
  253. r->eap = os_zalloc(r->eap_count * sizeof(struct nai_realm_eap));
  254. if (r->eap == NULL)
  255. return NULL;
  256. for (e = 0; e < r->eap_count; e++) {
  257. pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
  258. if (pos == NULL)
  259. return NULL;
  260. }
  261. return f_end;
  262. }
  263. static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
  264. {
  265. struct nai_realm *realm;
  266. const u8 *pos, *end;
  267. u16 i, num;
  268. if (anqp == NULL || wpabuf_len(anqp) < 2)
  269. return NULL;
  270. pos = wpabuf_head_u8(anqp);
  271. end = pos + wpabuf_len(anqp);
  272. num = WPA_GET_LE16(pos);
  273. wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
  274. pos += 2;
  275. if (num * 5 > end - pos) {
  276. wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
  277. "enough data (%u octets) for that many realms",
  278. num, (unsigned int) (end - pos));
  279. return NULL;
  280. }
  281. realm = os_zalloc(num * sizeof(struct nai_realm));
  282. if (realm == NULL)
  283. return NULL;
  284. for (i = 0; i < num; i++) {
  285. pos = nai_realm_parse_realm(&realm[i], pos, end);
  286. if (pos == NULL) {
  287. nai_realm_free(realm, num);
  288. return NULL;
  289. }
  290. }
  291. *count = num;
  292. return realm;
  293. }
  294. static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
  295. {
  296. char *tmp, *pos, *end;
  297. int match = 0;
  298. if (realm->realm == NULL || home_realm == NULL)
  299. return 0;
  300. if (os_strchr(realm->realm, ';') == NULL)
  301. return os_strcasecmp(realm->realm, home_realm) == 0;
  302. tmp = os_strdup(realm->realm);
  303. if (tmp == NULL)
  304. return 0;
  305. pos = tmp;
  306. while (*pos) {
  307. end = os_strchr(pos, ';');
  308. if (end)
  309. *end = '\0';
  310. if (os_strcasecmp(pos, home_realm) == 0) {
  311. match = 1;
  312. break;
  313. }
  314. if (end == NULL)
  315. break;
  316. pos = end + 1;
  317. }
  318. os_free(tmp);
  319. return match;
  320. }
  321. static int nai_realm_cred_username(struct nai_realm_eap *eap)
  322. {
  323. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  324. return 0; /* method not supported */
  325. if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP) {
  326. /* Only tunneled methods with username/password supported */
  327. return 0;
  328. }
  329. if (eap->method == EAP_TYPE_PEAP &&
  330. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  331. return 0;
  332. if (eap->method == EAP_TYPE_TTLS) {
  333. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  334. return 0;
  335. if (eap->inner_method &&
  336. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  337. return 0;
  338. if (eap->inner_non_eap &&
  339. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  340. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  341. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  342. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  343. return 0;
  344. }
  345. if (eap->inner_method &&
  346. eap->inner_method != EAP_TYPE_GTC &&
  347. eap->inner_method != EAP_TYPE_MSCHAPV2)
  348. return 0;
  349. return 1;
  350. }
  351. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  352. struct nai_realm *realm)
  353. {
  354. u8 e;
  355. if (cred == NULL ||
  356. cred->username == NULL ||
  357. cred->username[0] == '\0' ||
  358. cred->password == NULL ||
  359. cred->password[0] == '\0')
  360. return NULL;
  361. for (e = 0; e < realm->eap_count; e++) {
  362. struct nai_realm_eap *eap = &realm->eap[e];
  363. if (nai_realm_cred_username(eap))
  364. return eap;
  365. }
  366. return NULL;
  367. }
  368. #ifdef INTERWORKING_3GPP
  369. static int plmn_id_match(struct wpabuf *anqp, const char *imsi)
  370. {
  371. const char *sep;
  372. u8 plmn[3];
  373. const u8 *pos, *end;
  374. u8 udhl;
  375. sep = os_strchr(imsi, '-');
  376. if (sep == NULL || (sep - imsi != 5 && sep - imsi != 6))
  377. return 0;
  378. /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
  379. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  380. plmn[1] = imsi[2] - '0';
  381. if (sep - imsi == 6)
  382. plmn[1] |= (imsi[5] - '0') << 4;
  383. else
  384. plmn[1] |= 0xf0;
  385. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  386. if (anqp == NULL)
  387. return 0;
  388. pos = wpabuf_head_u8(anqp);
  389. end = pos + wpabuf_len(anqp);
  390. if (pos + 2 > end)
  391. return 0;
  392. if (*pos != 0) {
  393. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  394. return 0;
  395. }
  396. pos++;
  397. udhl = *pos++;
  398. if (pos + udhl > end) {
  399. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  400. return 0;
  401. }
  402. end = pos + udhl;
  403. while (pos + 2 <= end) {
  404. u8 iei, len;
  405. const u8 *l_end;
  406. iei = *pos++;
  407. len = *pos++ & 0x7f;
  408. if (pos + len > end)
  409. break;
  410. l_end = pos + len;
  411. if (iei == 0 && len > 0) {
  412. /* PLMN List */
  413. u8 num, i;
  414. num = *pos++;
  415. for (i = 0; i < num; i++) {
  416. if (pos + 3 > end)
  417. break;
  418. if (os_memcmp(pos, plmn, 3) == 0)
  419. return 1; /* Found matching PLMN */
  420. }
  421. }
  422. pos = l_end;
  423. }
  424. return 0;
  425. }
  426. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  427. char prefix)
  428. {
  429. const char *sep, *msin;
  430. char *end, *pos;
  431. size_t msin_len, plmn_len;
  432. /*
  433. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  434. * Root NAI:
  435. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  436. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  437. */
  438. if (imsi == NULL || os_strlen(imsi) > 16) {
  439. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  440. return -1;
  441. }
  442. sep = os_strchr(imsi, '-');
  443. if (sep == NULL)
  444. return -1;
  445. plmn_len = sep - imsi;
  446. if (plmn_len != 5 && plmn_len != 6)
  447. return -1;
  448. msin = sep + 1;
  449. msin_len = os_strlen(msin);
  450. pos = nai;
  451. end = nai + nai_len;
  452. if (prefix)
  453. *pos++ = prefix;
  454. os_memcpy(pos, imsi, plmn_len);
  455. pos += plmn_len;
  456. os_memcpy(pos, msin, msin_len);
  457. pos += msin_len;
  458. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  459. if (plmn_len == 5) {
  460. *pos++ = '0';
  461. *pos++ = imsi[3];
  462. *pos++ = imsi[4];
  463. } else {
  464. *pos++ = imsi[3];
  465. *pos++ = imsi[4];
  466. *pos++ = imsi[5];
  467. }
  468. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  469. imsi[0], imsi[1], imsi[2]);
  470. return 0;
  471. }
  472. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  473. {
  474. char nai[100];
  475. if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
  476. return -1;
  477. return wpa_config_set_quoted(ssid, "identity", nai);
  478. }
  479. #endif /* INTERWORKING_3GPP */
  480. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  481. struct wpa_bss *bss)
  482. {
  483. #ifdef INTERWORKING_3GPP
  484. struct wpa_cred *cred;
  485. struct wpa_ssid *ssid;
  486. const u8 *ie;
  487. if (bss->anqp_3gpp == NULL)
  488. return -1;
  489. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  490. if (cred->imsi == NULL || !cred->imsi[0] ||
  491. cred->milenage == NULL || !cred->milenage[0])
  492. continue;
  493. if (plmn_id_match(bss->anqp_3gpp, cred->imsi))
  494. break;
  495. }
  496. if (cred == NULL)
  497. return -1;
  498. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  499. if (ie == NULL)
  500. return -1;
  501. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  502. MAC2STR(bss->bssid));
  503. ssid = wpa_config_add_network(wpa_s->conf);
  504. if (ssid == NULL)
  505. return -1;
  506. wpas_notify_network_added(wpa_s, ssid);
  507. wpa_config_set_network_defaults(ssid);
  508. ssid->priority = cred->priority;
  509. ssid->temporary = 1;
  510. ssid->ssid = os_zalloc(ie[1] + 1);
  511. if (ssid->ssid == NULL)
  512. goto fail;
  513. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  514. ssid->ssid_len = ie[1];
  515. /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
  516. if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
  517. wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
  518. goto fail;
  519. }
  520. if (set_root_nai(ssid, cred->imsi, '1') < 0) {
  521. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  522. goto fail;
  523. }
  524. if (cred->milenage && cred->milenage[0]) {
  525. if (wpa_config_set_quoted(ssid, "password",
  526. cred->milenage) < 0)
  527. goto fail;
  528. } else {
  529. /* TODO: PIN */
  530. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  531. goto fail;
  532. }
  533. if (cred->password && cred->password[0] &&
  534. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  535. goto fail;
  536. wpa_config_update_prio_list(wpa_s->conf);
  537. wpa_s->disconnected = 0;
  538. wpa_s->reassociate = 1;
  539. wpa_supplicant_req_scan(wpa_s, 0, 0);
  540. return 0;
  541. fail:
  542. wpas_notify_network_removed(wpa_s, ssid);
  543. wpa_config_remove_network(wpa_s->conf, ssid->id);
  544. #endif /* INTERWORKING_3GPP */
  545. return -1;
  546. }
  547. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  548. {
  549. struct wpa_cred *cred;
  550. struct wpa_ssid *ssid;
  551. struct nai_realm *realm;
  552. struct nai_realm_eap *eap = NULL;
  553. u16 count, i;
  554. char buf[100];
  555. const u8 *ie;
  556. if (wpa_s->conf->cred == NULL || bss == NULL)
  557. return -1;
  558. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  559. if (ie == NULL || ie[1] == 0) {
  560. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  561. MACSTR, MAC2STR(bss->bssid));
  562. return -1;
  563. }
  564. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  565. if (realm == NULL) {
  566. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  567. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  568. count = 0;
  569. }
  570. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  571. for (i = 0; i < count; i++) {
  572. if (!nai_realm_match(&realm[i], cred->realm))
  573. continue;
  574. eap = nai_realm_find_eap(cred, &realm[i]);
  575. if (eap)
  576. break;
  577. }
  578. if (eap)
  579. break;
  580. }
  581. if (!eap) {
  582. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  583. if (realm)
  584. nai_realm_free(realm, count);
  585. return 0;
  586. }
  587. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  588. "and EAP method found for " MACSTR,
  589. MAC2STR(bss->bssid));
  590. nai_realm_free(realm, count);
  591. return -1;
  592. }
  593. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  594. MAC2STR(bss->bssid));
  595. ssid = wpa_config_add_network(wpa_s->conf);
  596. if (ssid == NULL) {
  597. nai_realm_free(realm, count);
  598. return -1;
  599. }
  600. wpas_notify_network_added(wpa_s, ssid);
  601. wpa_config_set_network_defaults(ssid);
  602. ssid->priority = cred->priority;
  603. ssid->temporary = 1;
  604. ssid->ssid = os_zalloc(ie[1] + 1);
  605. if (ssid->ssid == NULL)
  606. goto fail;
  607. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  608. ssid->ssid_len = ie[1];
  609. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  610. eap->method), 0) < 0)
  611. goto fail;
  612. if (eap->method == EAP_TYPE_TTLS &&
  613. cred->username && cred->username[0]) {
  614. const char *pos;
  615. char *anon;
  616. /* Use anonymous NAI in Phase 1 */
  617. pos = os_strchr(cred->username, '@');
  618. if (pos) {
  619. size_t buflen = 9 + os_strlen(pos) + 1;
  620. anon = os_malloc(buflen);
  621. if (anon == NULL)
  622. goto fail;
  623. os_snprintf(anon, buflen, "anonymous%s", pos);
  624. } else {
  625. anon = os_strdup("anonymous");
  626. if (anon == NULL)
  627. goto fail;
  628. }
  629. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  630. 0) {
  631. os_free(anon);
  632. goto fail;
  633. }
  634. os_free(anon);
  635. }
  636. if (cred->username && cred->username[0] &&
  637. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  638. goto fail;
  639. if (cred->password && cred->password[0] &&
  640. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  641. goto fail;
  642. switch (eap->method) {
  643. case EAP_TYPE_TTLS:
  644. if (eap->inner_method) {
  645. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  646. eap_get_name(EAP_VENDOR_IETF,
  647. eap->inner_method));
  648. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  649. goto fail;
  650. break;
  651. }
  652. switch (eap->inner_non_eap) {
  653. case NAI_REALM_INNER_NON_EAP_PAP:
  654. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  655. 0)
  656. goto fail;
  657. break;
  658. case NAI_REALM_INNER_NON_EAP_CHAP:
  659. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  660. < 0)
  661. goto fail;
  662. break;
  663. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  664. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  665. 0) < 0)
  666. goto fail;
  667. break;
  668. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  669. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  670. 0) < 0)
  671. goto fail;
  672. break;
  673. }
  674. break;
  675. case EAP_TYPE_PEAP:
  676. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  677. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  678. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  679. goto fail;
  680. break;
  681. }
  682. if (cred->ca_cert && cred->ca_cert[0] &&
  683. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  684. goto fail;
  685. nai_realm_free(realm, count);
  686. wpa_config_update_prio_list(wpa_s->conf);
  687. wpa_s->disconnected = 0;
  688. wpa_s->reassociate = 1;
  689. wpa_supplicant_req_scan(wpa_s, 0, 0);
  690. return 0;
  691. fail:
  692. wpas_notify_network_removed(wpa_s, ssid);
  693. wpa_config_remove_network(wpa_s->conf, ssid->id);
  694. nai_realm_free(realm, count);
  695. return -1;
  696. }
  697. static struct wpa_cred * interworking_credentials_available_3gpp(
  698. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  699. {
  700. struct wpa_cred *cred, *selected = NULL;
  701. int ret;
  702. #ifdef INTERWORKING_3GPP
  703. if (bss->anqp_3gpp == NULL)
  704. return NULL;
  705. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  706. if (cred->imsi == NULL || !cred->imsi[0] ||
  707. cred->milenage == NULL || !cred->milenage[0])
  708. continue;
  709. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  710. MACSTR, MAC2STR(bss->bssid));
  711. ret = plmn_id_match(bss->anqp_3gpp, cred->imsi);
  712. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  713. if (ret) {
  714. if (selected == NULL ||
  715. selected->priority < cred->priority)
  716. selected = cred;
  717. }
  718. }
  719. #endif /* INTERWORKING_3GPP */
  720. return selected;
  721. }
  722. static struct wpa_cred * interworking_credentials_available_realm(
  723. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  724. {
  725. struct wpa_cred *cred, *selected = NULL;
  726. struct nai_realm *realm;
  727. u16 count, i;
  728. if (bss->anqp_nai_realm == NULL)
  729. return NULL;
  730. if (wpa_s->conf->cred == NULL)
  731. return NULL;
  732. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  733. MACSTR, MAC2STR(bss->bssid));
  734. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  735. if (realm == NULL) {
  736. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  737. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  738. return NULL;
  739. }
  740. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  741. if (cred->realm == NULL)
  742. continue;
  743. for (i = 0; i < count; i++) {
  744. if (!nai_realm_match(&realm[i], cred->realm))
  745. continue;
  746. if (nai_realm_find_eap(cred, &realm[i])) {
  747. if (selected == NULL ||
  748. selected->priority < cred->priority)
  749. selected = cred;
  750. break;
  751. }
  752. }
  753. }
  754. nai_realm_free(realm, count);
  755. return selected;
  756. }
  757. static struct wpa_cred * interworking_credentials_available(
  758. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  759. {
  760. struct wpa_cred *cred, *cred2;
  761. cred = interworking_credentials_available_realm(wpa_s, bss);
  762. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  763. if (cred && cred2 && cred2->priority >= cred->priority)
  764. cred = cred2;
  765. if (!cred)
  766. cred = cred2;
  767. return cred;
  768. }
  769. static int domain_name_list_contains(struct wpabuf *domain_names,
  770. const char *domain)
  771. {
  772. const u8 *pos, *end;
  773. size_t len;
  774. len = os_strlen(domain);
  775. pos = wpabuf_head(domain_names);
  776. end = pos + wpabuf_len(domain_names);
  777. while (pos + 1 < end) {
  778. if (pos + 1 + pos[0] > end)
  779. break;
  780. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  781. pos + 1, pos[0]);
  782. if (pos[0] == len &&
  783. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  784. return 1;
  785. pos += 1 + pos[0];
  786. }
  787. return 0;
  788. }
  789. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  790. struct wpabuf *domain_names)
  791. {
  792. struct wpa_cred *cred;
  793. #ifdef INTERWORKING_3GPP
  794. char nai[100], *realm;
  795. #endif /* INTERWORKING_3GPP */
  796. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  797. return -1;
  798. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  799. #ifdef INTERWORKING_3GPP
  800. if (cred->imsi &&
  801. build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
  802. realm = os_strchr(nai, '@');
  803. if (realm)
  804. realm++;
  805. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  806. "with SIM/USIM domain %s", realm);
  807. if (realm &&
  808. domain_name_list_contains(domain_names, realm))
  809. return 1;
  810. }
  811. #endif /* INTERWORKING_3GPP */
  812. if (cred->domain == NULL)
  813. continue;
  814. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  815. "home SP FQDN %s", cred->domain);
  816. if (domain_name_list_contains(domain_names, cred->domain))
  817. return 1;
  818. }
  819. return 0;
  820. }
  821. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  822. {
  823. struct wpa_bss *bss;
  824. struct wpa_ssid *ssid;
  825. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  826. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  827. if (ssid->disabled || ssid->mode != WPAS_MODE_INFRA)
  828. continue;
  829. if (ssid->ssid_len != bss->ssid_len ||
  830. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  831. 0)
  832. continue;
  833. /*
  834. * TODO: Consider more accurate matching of security
  835. * configuration similarly to what is done in events.c
  836. */
  837. return 1;
  838. }
  839. }
  840. return 0;
  841. }
  842. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  843. {
  844. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  845. int selected_prio = -999999, selected_home_prio = -999999;
  846. unsigned int count = 0;
  847. const char *type;
  848. int res;
  849. struct wpa_cred *cred;
  850. wpa_s->network_select = 0;
  851. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  852. cred = interworking_credentials_available(wpa_s, bss);
  853. if (!cred)
  854. continue;
  855. count++;
  856. res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
  857. if (res > 0)
  858. type = "home";
  859. else if (res == 0)
  860. type = "roaming";
  861. else
  862. type = "unknown";
  863. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  864. MAC2STR(bss->bssid), type);
  865. if (wpa_s->auto_select) {
  866. if (selected == NULL ||
  867. cred->priority > selected_prio) {
  868. selected = bss;
  869. selected_prio = cred->priority;
  870. }
  871. if (res > 0 &&
  872. (selected_home == NULL ||
  873. cred->priority > selected_home_prio)) {
  874. selected_home = bss;
  875. selected_home_prio = cred->priority;
  876. }
  877. }
  878. }
  879. if (selected_home && selected_home != selected &&
  880. selected_home_prio >= selected_prio) {
  881. /* Prefer network operated by the Home SP */
  882. selected = selected_home;
  883. }
  884. if (count == 0) {
  885. /*
  886. * No matching network was found based on configured
  887. * credentials. Check whether any of the enabled network blocks
  888. * have matching APs.
  889. */
  890. if (interworking_find_network_match(wpa_s)) {
  891. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  892. "match for enabled network configurations");
  893. wpa_s->disconnected = 0;
  894. wpa_s->reassociate = 1;
  895. wpa_supplicant_req_scan(wpa_s, 0, 0);
  896. return;
  897. }
  898. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  899. "with matching credentials found");
  900. }
  901. if (selected)
  902. interworking_connect(wpa_s, selected);
  903. }
  904. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  905. {
  906. struct wpa_bss *bss;
  907. int found = 0;
  908. const u8 *ie;
  909. if (!wpa_s->fetch_anqp_in_progress)
  910. return;
  911. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  912. if (!(bss->caps & IEEE80211_CAP_ESS))
  913. continue;
  914. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  915. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  916. continue; /* AP does not support Interworking */
  917. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  918. found++;
  919. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  920. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  921. MACSTR, MAC2STR(bss->bssid));
  922. interworking_anqp_send_req(wpa_s, bss);
  923. break;
  924. }
  925. }
  926. if (found == 0) {
  927. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  928. wpa_s->fetch_anqp_in_progress = 0;
  929. if (wpa_s->network_select)
  930. interworking_select_network(wpa_s);
  931. }
  932. }
  933. static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  934. {
  935. struct wpa_bss *bss;
  936. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  937. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  938. wpa_s->fetch_anqp_in_progress = 1;
  939. interworking_next_anqp_fetch(wpa_s);
  940. }
  941. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  942. {
  943. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  944. return 0;
  945. wpa_s->network_select = 0;
  946. interworking_start_fetch_anqp(wpa_s);
  947. return 0;
  948. }
  949. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  950. {
  951. if (!wpa_s->fetch_anqp_in_progress)
  952. return;
  953. wpa_s->fetch_anqp_in_progress = 0;
  954. }
  955. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  956. u16 info_ids[], size_t num_ids)
  957. {
  958. struct wpabuf *buf;
  959. int ret = 0;
  960. int freq;
  961. struct wpa_bss *bss;
  962. int res;
  963. freq = wpa_s->assoc_freq;
  964. bss = wpa_bss_get_bssid(wpa_s, dst);
  965. if (bss)
  966. freq = bss->freq;
  967. if (freq <= 0)
  968. return -1;
  969. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  970. MAC2STR(dst), (unsigned int) num_ids);
  971. buf = anqp_build_req(info_ids, num_ids, NULL);
  972. if (buf == NULL)
  973. return -1;
  974. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  975. if (res < 0) {
  976. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  977. ret = -1;
  978. } else
  979. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  980. "%u", res);
  981. wpabuf_free(buf);
  982. return ret;
  983. }
  984. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  985. const u8 *sa, u16 info_id,
  986. const u8 *data, size_t slen)
  987. {
  988. const u8 *pos = data;
  989. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  990. switch (info_id) {
  991. case ANQP_CAPABILITY_LIST:
  992. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  993. " ANQP Capability list", MAC2STR(sa));
  994. break;
  995. case ANQP_VENUE_NAME:
  996. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  997. " Venue Name", MAC2STR(sa));
  998. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  999. if (bss) {
  1000. wpabuf_free(bss->anqp_venue_name);
  1001. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  1002. }
  1003. break;
  1004. case ANQP_NETWORK_AUTH_TYPE:
  1005. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1006. " Network Authentication Type information",
  1007. MAC2STR(sa));
  1008. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1009. "Type", pos, slen);
  1010. if (bss) {
  1011. wpabuf_free(bss->anqp_network_auth_type);
  1012. bss->anqp_network_auth_type =
  1013. wpabuf_alloc_copy(pos, slen);
  1014. }
  1015. break;
  1016. case ANQP_ROAMING_CONSORTIUM:
  1017. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1018. " Roaming Consortium list", MAC2STR(sa));
  1019. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1020. pos, slen);
  1021. if (bss) {
  1022. wpabuf_free(bss->anqp_roaming_consortium);
  1023. bss->anqp_roaming_consortium =
  1024. wpabuf_alloc_copy(pos, slen);
  1025. }
  1026. break;
  1027. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1028. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1029. " IP Address Type Availability information",
  1030. MAC2STR(sa));
  1031. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1032. pos, slen);
  1033. if (bss) {
  1034. wpabuf_free(bss->anqp_ip_addr_type_availability);
  1035. bss->anqp_ip_addr_type_availability =
  1036. wpabuf_alloc_copy(pos, slen);
  1037. }
  1038. break;
  1039. case ANQP_NAI_REALM:
  1040. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1041. " NAI Realm list", MAC2STR(sa));
  1042. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1043. if (bss) {
  1044. wpabuf_free(bss->anqp_nai_realm);
  1045. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  1046. }
  1047. break;
  1048. case ANQP_3GPP_CELLULAR_NETWORK:
  1049. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1050. " 3GPP Cellular Network information", MAC2STR(sa));
  1051. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1052. pos, slen);
  1053. if (bss) {
  1054. wpabuf_free(bss->anqp_3gpp);
  1055. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1056. }
  1057. break;
  1058. case ANQP_DOMAIN_NAME:
  1059. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1060. " Domain Name list", MAC2STR(sa));
  1061. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1062. if (bss) {
  1063. wpabuf_free(bss->anqp_domain_name);
  1064. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  1065. }
  1066. break;
  1067. case ANQP_VENDOR_SPECIFIC:
  1068. if (slen < 3)
  1069. return;
  1070. switch (WPA_GET_BE24(pos)) {
  1071. default:
  1072. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1073. "vendor-specific ANQP OUI %06x",
  1074. WPA_GET_BE24(pos));
  1075. return;
  1076. }
  1077. break;
  1078. default:
  1079. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1080. "%u", info_id);
  1081. break;
  1082. }
  1083. }
  1084. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1085. enum gas_query_result result,
  1086. const struct wpabuf *adv_proto,
  1087. const struct wpabuf *resp, u16 status_code)
  1088. {
  1089. struct wpa_supplicant *wpa_s = ctx;
  1090. const u8 *pos;
  1091. const u8 *end;
  1092. u16 info_id;
  1093. u16 slen;
  1094. if (result != GAS_QUERY_SUCCESS)
  1095. return;
  1096. pos = wpabuf_head(adv_proto);
  1097. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1098. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1099. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1100. "Protocol in response");
  1101. return;
  1102. }
  1103. pos = wpabuf_head(resp);
  1104. end = pos + wpabuf_len(resp);
  1105. while (pos < end) {
  1106. if (pos + 4 > end) {
  1107. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1108. break;
  1109. }
  1110. info_id = WPA_GET_LE16(pos);
  1111. pos += 2;
  1112. slen = WPA_GET_LE16(pos);
  1113. pos += 2;
  1114. if (pos + slen > end) {
  1115. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1116. "for Info ID %u", info_id);
  1117. break;
  1118. }
  1119. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1120. slen);
  1121. pos += slen;
  1122. }
  1123. }
  1124. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1125. struct wpa_scan_results *scan_res)
  1126. {
  1127. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1128. "ANQP fetch");
  1129. interworking_start_fetch_anqp(wpa_s);
  1130. }
  1131. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1132. {
  1133. interworking_stop_fetch_anqp(wpa_s);
  1134. wpa_s->network_select = 1;
  1135. wpa_s->auto_select = !!auto_select;
  1136. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1137. "selection");
  1138. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1139. wpa_s->scan_req = 2;
  1140. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1141. return 0;
  1142. }