interworking.c 33 KB

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