interworking.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 int nai_realm_cred_cert(struct nai_realm_eap *eap)
  352. {
  353. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  354. return 0; /* method not supported */
  355. if (eap->method != EAP_TYPE_TLS) {
  356. /* Only EAP-TLS supported for credential authentication */
  357. return 0;
  358. }
  359. return 1;
  360. }
  361. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  362. struct nai_realm *realm)
  363. {
  364. u8 e;
  365. if (cred == NULL ||
  366. cred->username == NULL ||
  367. cred->username[0] == '\0' ||
  368. ((cred->password == NULL ||
  369. cred->password[0] == '\0') &&
  370. (cred->private_key == NULL ||
  371. cred->private_key[0] == '\0')))
  372. return NULL;
  373. for (e = 0; e < realm->eap_count; e++) {
  374. struct nai_realm_eap *eap = &realm->eap[e];
  375. if (cred->password && cred->password[0] &&
  376. nai_realm_cred_username(eap))
  377. return eap;
  378. if (cred->private_key && cred->private_key[0] &&
  379. nai_realm_cred_cert(eap))
  380. return eap;
  381. }
  382. return NULL;
  383. }
  384. #ifdef INTERWORKING_3GPP
  385. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  386. {
  387. u8 plmn[3];
  388. const u8 *pos, *end;
  389. u8 udhl;
  390. /* See Annex A of 3GPP TS 24.234 v8.1.0 for description */
  391. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  392. plmn[1] = imsi[2] - '0';
  393. /* default to MNC length 3 if unknown */
  394. if (mnc_len != 2)
  395. plmn[1] |= (imsi[5] - '0') << 4;
  396. else
  397. plmn[1] |= 0xf0;
  398. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  399. if (anqp == NULL)
  400. return 0;
  401. pos = wpabuf_head_u8(anqp);
  402. end = pos + wpabuf_len(anqp);
  403. if (pos + 2 > end)
  404. return 0;
  405. if (*pos != 0) {
  406. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  407. return 0;
  408. }
  409. pos++;
  410. udhl = *pos++;
  411. if (pos + udhl > end) {
  412. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  413. return 0;
  414. }
  415. end = pos + udhl;
  416. while (pos + 2 <= end) {
  417. u8 iei, len;
  418. const u8 *l_end;
  419. iei = *pos++;
  420. len = *pos++ & 0x7f;
  421. if (pos + len > end)
  422. break;
  423. l_end = pos + len;
  424. if (iei == 0 && len > 0) {
  425. /* PLMN List */
  426. u8 num, i;
  427. num = *pos++;
  428. for (i = 0; i < num; i++) {
  429. if (pos + 3 > end)
  430. break;
  431. if (os_memcmp(pos, plmn, 3) == 0)
  432. return 1; /* Found matching PLMN */
  433. }
  434. }
  435. pos = l_end;
  436. }
  437. return 0;
  438. }
  439. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  440. char prefix)
  441. {
  442. const char *sep, *msin;
  443. char *end, *pos;
  444. size_t msin_len, plmn_len;
  445. /*
  446. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  447. * Root NAI:
  448. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  449. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  450. */
  451. if (imsi == NULL || os_strlen(imsi) > 16) {
  452. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  453. return -1;
  454. }
  455. sep = os_strchr(imsi, '-');
  456. if (sep == NULL)
  457. return -1;
  458. plmn_len = sep - imsi;
  459. if (plmn_len != 5 && plmn_len != 6)
  460. return -1;
  461. msin = sep + 1;
  462. msin_len = os_strlen(msin);
  463. pos = nai;
  464. end = nai + nai_len;
  465. if (prefix)
  466. *pos++ = prefix;
  467. os_memcpy(pos, imsi, plmn_len);
  468. pos += plmn_len;
  469. os_memcpy(pos, msin, msin_len);
  470. pos += msin_len;
  471. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  472. if (plmn_len == 5) {
  473. *pos++ = '0';
  474. *pos++ = imsi[3];
  475. *pos++ = imsi[4];
  476. } else {
  477. *pos++ = imsi[3];
  478. *pos++ = imsi[4];
  479. *pos++ = imsi[5];
  480. }
  481. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  482. imsi[0], imsi[1], imsi[2]);
  483. return 0;
  484. }
  485. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  486. {
  487. char nai[100];
  488. if (build_root_nai(nai, sizeof(nai), imsi, prefix) < 0)
  489. return -1;
  490. return wpa_config_set_quoted(ssid, "identity", nai);
  491. }
  492. #endif /* INTERWORKING_3GPP */
  493. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  494. struct wpa_bss *bss)
  495. {
  496. #ifdef INTERWORKING_3GPP
  497. struct wpa_cred *cred;
  498. struct wpa_ssid *ssid;
  499. const u8 *ie;
  500. if (bss->anqp_3gpp == NULL)
  501. return -1;
  502. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  503. char *sep;
  504. const char *imsi;
  505. int mnc_len;
  506. #ifdef PCSC_FUNCS
  507. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  508. wpa_s->imsi[0]) {
  509. imsi = wpa_s->imsi;
  510. mnc_len = wpa_s->mnc_len;
  511. goto compare;
  512. }
  513. #endif /* PCSC_FUNCS */
  514. if (cred->imsi == NULL || !cred->imsi[0] ||
  515. cred->milenage == NULL || !cred->milenage[0])
  516. continue;
  517. sep = os_strchr(cred->imsi, '-');
  518. if (sep == NULL ||
  519. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  520. continue;
  521. mnc_len = sep - cred->imsi - 3;
  522. imsi = cred->imsi;
  523. #ifdef PCSC_FUNCS
  524. compare:
  525. #endif /* PCSC_FUNCS */
  526. if (plmn_id_match(bss->anqp_3gpp, imsi, mnc_len))
  527. break;
  528. }
  529. if (cred == NULL)
  530. return -1;
  531. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  532. if (ie == NULL)
  533. return -1;
  534. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  535. MAC2STR(bss->bssid));
  536. ssid = wpa_config_add_network(wpa_s->conf);
  537. if (ssid == NULL)
  538. return -1;
  539. wpas_notify_network_added(wpa_s, ssid);
  540. wpa_config_set_network_defaults(ssid);
  541. ssid->priority = cred->priority;
  542. ssid->temporary = 1;
  543. ssid->ssid = os_zalloc(ie[1] + 1);
  544. if (ssid->ssid == NULL)
  545. goto fail;
  546. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  547. ssid->ssid_len = ie[1];
  548. /* TODO: figure out whether to use EAP-SIM, EAP-AKA, or EAP-AKA' */
  549. if (wpa_config_set(ssid, "eap", "SIM", 0) < 0) {
  550. wpa_printf(MSG_DEBUG, "EAP-SIM not supported");
  551. goto fail;
  552. }
  553. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, '1') < 0) {
  554. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  555. goto fail;
  556. }
  557. if (cred->milenage && cred->milenage[0]) {
  558. if (wpa_config_set_quoted(ssid, "password",
  559. cred->milenage) < 0)
  560. goto fail;
  561. } else if (cred->pcsc) {
  562. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  563. goto fail;
  564. if (wpa_s->conf->pcsc_pin &&
  565. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  566. < 0)
  567. goto fail;
  568. }
  569. if (cred->password && cred->password[0] &&
  570. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  571. goto fail;
  572. wpa_config_update_prio_list(wpa_s->conf);
  573. wpa_s->disconnected = 0;
  574. wpa_s->reassociate = 1;
  575. wpa_supplicant_req_scan(wpa_s, 0, 0);
  576. return 0;
  577. fail:
  578. wpas_notify_network_removed(wpa_s, ssid);
  579. wpa_config_remove_network(wpa_s->conf, ssid->id);
  580. #endif /* INTERWORKING_3GPP */
  581. return -1;
  582. }
  583. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  584. {
  585. struct wpa_cred *cred;
  586. struct wpa_ssid *ssid;
  587. struct nai_realm *realm;
  588. struct nai_realm_eap *eap = NULL;
  589. u16 count, i;
  590. char buf[100];
  591. const u8 *ie;
  592. if (wpa_s->conf->cred == NULL || bss == NULL)
  593. return -1;
  594. ie = wpa_bss_get_ie(bss, WLAN_EID_SSID);
  595. if (ie == NULL || ie[1] == 0) {
  596. wpa_printf(MSG_DEBUG, "Interworking: No SSID known for "
  597. MACSTR, MAC2STR(bss->bssid));
  598. return -1;
  599. }
  600. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  601. if (realm == NULL) {
  602. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  603. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  604. count = 0;
  605. }
  606. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  607. for (i = 0; i < count; i++) {
  608. if (!nai_realm_match(&realm[i], cred->realm))
  609. continue;
  610. eap = nai_realm_find_eap(cred, &realm[i]);
  611. if (eap)
  612. break;
  613. }
  614. if (eap)
  615. break;
  616. }
  617. if (!eap) {
  618. if (interworking_connect_3gpp(wpa_s, bss) == 0) {
  619. if (realm)
  620. nai_realm_free(realm, count);
  621. return 0;
  622. }
  623. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  624. "and EAP method found for " MACSTR,
  625. MAC2STR(bss->bssid));
  626. nai_realm_free(realm, count);
  627. return -1;
  628. }
  629. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  630. MAC2STR(bss->bssid));
  631. ssid = wpa_config_add_network(wpa_s->conf);
  632. if (ssid == NULL) {
  633. nai_realm_free(realm, count);
  634. return -1;
  635. }
  636. wpas_notify_network_added(wpa_s, ssid);
  637. wpa_config_set_network_defaults(ssid);
  638. ssid->priority = cred->priority;
  639. ssid->temporary = 1;
  640. ssid->ssid = os_zalloc(ie[1] + 1);
  641. if (ssid->ssid == NULL)
  642. goto fail;
  643. os_memcpy(ssid->ssid, ie + 2, ie[1]);
  644. ssid->ssid_len = ie[1];
  645. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  646. eap->method), 0) < 0)
  647. goto fail;
  648. if (eap->method == EAP_TYPE_TTLS &&
  649. cred->username && cred->username[0]) {
  650. const char *pos;
  651. char *anon;
  652. /* Use anonymous NAI in Phase 1 */
  653. pos = os_strchr(cred->username, '@');
  654. if (pos) {
  655. size_t buflen = 9 + os_strlen(pos) + 1;
  656. anon = os_malloc(buflen);
  657. if (anon == NULL)
  658. goto fail;
  659. os_snprintf(anon, buflen, "anonymous%s", pos);
  660. } else {
  661. anon = os_strdup("anonymous");
  662. if (anon == NULL)
  663. goto fail;
  664. }
  665. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  666. 0) {
  667. os_free(anon);
  668. goto fail;
  669. }
  670. os_free(anon);
  671. }
  672. if (cred->username && cred->username[0] &&
  673. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  674. goto fail;
  675. if (cred->password && cred->password[0] &&
  676. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  677. goto fail;
  678. if (cred->client_cert && cred->client_cert[0] &&
  679. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  680. goto fail;
  681. if (cred->private_key && cred->private_key[0] &&
  682. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  683. goto fail;
  684. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  685. wpa_config_set_quoted(ssid, "private_key_passwd",
  686. cred->private_key_passwd) < 0)
  687. goto fail;
  688. switch (eap->method) {
  689. case EAP_TYPE_TTLS:
  690. if (eap->inner_method) {
  691. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  692. eap_get_name(EAP_VENDOR_IETF,
  693. eap->inner_method));
  694. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  695. goto fail;
  696. break;
  697. }
  698. switch (eap->inner_non_eap) {
  699. case NAI_REALM_INNER_NON_EAP_PAP:
  700. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  701. 0)
  702. goto fail;
  703. break;
  704. case NAI_REALM_INNER_NON_EAP_CHAP:
  705. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  706. < 0)
  707. goto fail;
  708. break;
  709. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  710. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  711. 0) < 0)
  712. goto fail;
  713. break;
  714. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  715. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  716. 0) < 0)
  717. goto fail;
  718. break;
  719. }
  720. break;
  721. case EAP_TYPE_PEAP:
  722. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  723. eap_get_name(EAP_VENDOR_IETF, eap->inner_method));
  724. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  725. goto fail;
  726. break;
  727. case EAP_TYPE_TLS:
  728. break;
  729. }
  730. if (cred->ca_cert && cred->ca_cert[0] &&
  731. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  732. goto fail;
  733. nai_realm_free(realm, count);
  734. wpa_config_update_prio_list(wpa_s->conf);
  735. wpa_s->disconnected = 0;
  736. wpa_s->reassociate = 1;
  737. wpa_supplicant_req_scan(wpa_s, 0, 0);
  738. return 0;
  739. fail:
  740. wpas_notify_network_removed(wpa_s, ssid);
  741. wpa_config_remove_network(wpa_s->conf, ssid->id);
  742. nai_realm_free(realm, count);
  743. return -1;
  744. }
  745. static struct wpa_cred * interworking_credentials_available_3gpp(
  746. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  747. {
  748. struct wpa_cred *cred, *selected = NULL;
  749. int ret;
  750. #ifdef INTERWORKING_3GPP
  751. if (bss->anqp_3gpp == NULL)
  752. return NULL;
  753. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  754. char *sep;
  755. const char *imsi;
  756. int mnc_len;
  757. #ifdef PCSC_FUNCS
  758. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  759. wpa_s->imsi[0]) {
  760. imsi = wpa_s->imsi;
  761. mnc_len = wpa_s->mnc_len;
  762. goto compare;
  763. }
  764. #endif /* PCSC_FUNCS */
  765. if (cred->imsi == NULL || !cred->imsi[0] ||
  766. cred->milenage == NULL || !cred->milenage[0])
  767. continue;
  768. sep = os_strchr(cred->imsi, '-');
  769. if (sep == NULL ||
  770. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  771. continue;
  772. mnc_len = sep - cred->imsi - 3;
  773. imsi = cred->imsi;
  774. #ifdef PCSC_FUNCS
  775. compare:
  776. #endif /* PCSC_FUNCS */
  777. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  778. MACSTR, MAC2STR(bss->bssid));
  779. ret = plmn_id_match(bss->anqp_3gpp, imsi, mnc_len);
  780. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  781. if (ret) {
  782. if (selected == NULL ||
  783. selected->priority < cred->priority)
  784. selected = cred;
  785. }
  786. }
  787. #endif /* INTERWORKING_3GPP */
  788. return selected;
  789. }
  790. static struct wpa_cred * interworking_credentials_available_realm(
  791. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  792. {
  793. struct wpa_cred *cred, *selected = NULL;
  794. struct nai_realm *realm;
  795. u16 count, i;
  796. if (bss->anqp_nai_realm == NULL)
  797. return NULL;
  798. if (wpa_s->conf->cred == NULL)
  799. return NULL;
  800. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  801. MACSTR, MAC2STR(bss->bssid));
  802. realm = nai_realm_parse(bss->anqp_nai_realm, &count);
  803. if (realm == NULL) {
  804. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  805. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  806. return NULL;
  807. }
  808. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  809. if (cred->realm == NULL)
  810. continue;
  811. for (i = 0; i < count; i++) {
  812. if (!nai_realm_match(&realm[i], cred->realm))
  813. continue;
  814. if (nai_realm_find_eap(cred, &realm[i])) {
  815. if (selected == NULL ||
  816. selected->priority < cred->priority)
  817. selected = cred;
  818. break;
  819. }
  820. }
  821. }
  822. nai_realm_free(realm, count);
  823. return selected;
  824. }
  825. static struct wpa_cred * interworking_credentials_available(
  826. struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  827. {
  828. struct wpa_cred *cred, *cred2;
  829. cred = interworking_credentials_available_realm(wpa_s, bss);
  830. cred2 = interworking_credentials_available_3gpp(wpa_s, bss);
  831. if (cred && cred2 && cred2->priority >= cred->priority)
  832. cred = cred2;
  833. if (!cred)
  834. cred = cred2;
  835. return cred;
  836. }
  837. static int domain_name_list_contains(struct wpabuf *domain_names,
  838. const char *domain)
  839. {
  840. const u8 *pos, *end;
  841. size_t len;
  842. len = os_strlen(domain);
  843. pos = wpabuf_head(domain_names);
  844. end = pos + wpabuf_len(domain_names);
  845. while (pos + 1 < end) {
  846. if (pos + 1 + pos[0] > end)
  847. break;
  848. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  849. pos + 1, pos[0]);
  850. if (pos[0] == len &&
  851. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  852. return 1;
  853. pos += 1 + pos[0];
  854. }
  855. return 0;
  856. }
  857. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  858. struct wpabuf *domain_names)
  859. {
  860. struct wpa_cred *cred;
  861. #ifdef INTERWORKING_3GPP
  862. char nai[100], *realm;
  863. #endif /* INTERWORKING_3GPP */
  864. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  865. return -1;
  866. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  867. #ifdef INTERWORKING_3GPP
  868. if (cred->imsi &&
  869. build_root_nai(nai, sizeof(nai), cred->imsi, 0) == 0) {
  870. realm = os_strchr(nai, '@');
  871. if (realm)
  872. realm++;
  873. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  874. "with SIM/USIM domain %s", realm);
  875. if (realm &&
  876. domain_name_list_contains(domain_names, realm))
  877. return 1;
  878. }
  879. #endif /* INTERWORKING_3GPP */
  880. if (cred->domain == NULL)
  881. continue;
  882. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  883. "home SP FQDN %s", cred->domain);
  884. if (domain_name_list_contains(domain_names, cred->domain))
  885. return 1;
  886. }
  887. return 0;
  888. }
  889. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  890. {
  891. struct wpa_bss *bss;
  892. struct wpa_ssid *ssid;
  893. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  894. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  895. if (ssid->disabled || ssid->mode != WPAS_MODE_INFRA)
  896. continue;
  897. if (ssid->ssid_len != bss->ssid_len ||
  898. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  899. 0)
  900. continue;
  901. /*
  902. * TODO: Consider more accurate matching of security
  903. * configuration similarly to what is done in events.c
  904. */
  905. return 1;
  906. }
  907. }
  908. return 0;
  909. }
  910. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  911. {
  912. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  913. int selected_prio = -999999, selected_home_prio = -999999;
  914. unsigned int count = 0;
  915. const char *type;
  916. int res;
  917. struct wpa_cred *cred;
  918. wpa_s->network_select = 0;
  919. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  920. cred = interworking_credentials_available(wpa_s, bss);
  921. if (!cred)
  922. continue;
  923. count++;
  924. res = interworking_home_sp(wpa_s, bss->anqp_domain_name);
  925. if (res > 0)
  926. type = "home";
  927. else if (res == 0)
  928. type = "roaming";
  929. else
  930. type = "unknown";
  931. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_AP MACSTR " type=%s",
  932. MAC2STR(bss->bssid), type);
  933. if (wpa_s->auto_select) {
  934. if (selected == NULL ||
  935. cred->priority > selected_prio) {
  936. selected = bss;
  937. selected_prio = cred->priority;
  938. }
  939. if (res > 0 &&
  940. (selected_home == NULL ||
  941. cred->priority > selected_home_prio)) {
  942. selected_home = bss;
  943. selected_home_prio = cred->priority;
  944. }
  945. }
  946. }
  947. if (selected_home && selected_home != selected &&
  948. selected_home_prio >= selected_prio) {
  949. /* Prefer network operated by the Home SP */
  950. selected = selected_home;
  951. }
  952. if (count == 0) {
  953. /*
  954. * No matching network was found based on configured
  955. * credentials. Check whether any of the enabled network blocks
  956. * have matching APs.
  957. */
  958. if (interworking_find_network_match(wpa_s)) {
  959. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  960. "match for enabled network configurations");
  961. wpa_s->disconnected = 0;
  962. wpa_s->reassociate = 1;
  963. wpa_supplicant_req_scan(wpa_s, 0, 0);
  964. return;
  965. }
  966. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  967. "with matching credentials found");
  968. }
  969. if (selected)
  970. interworking_connect(wpa_s, selected);
  971. }
  972. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  973. {
  974. struct wpa_bss *bss;
  975. int found = 0;
  976. const u8 *ie;
  977. if (!wpa_s->fetch_anqp_in_progress)
  978. return;
  979. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  980. if (!(bss->caps & IEEE80211_CAP_ESS))
  981. continue;
  982. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  983. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  984. continue; /* AP does not support Interworking */
  985. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  986. found++;
  987. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  988. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  989. MACSTR, MAC2STR(bss->bssid));
  990. interworking_anqp_send_req(wpa_s, bss);
  991. break;
  992. }
  993. }
  994. if (found == 0) {
  995. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  996. wpa_s->fetch_anqp_in_progress = 0;
  997. if (wpa_s->network_select)
  998. interworking_select_network(wpa_s);
  999. }
  1000. }
  1001. static void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  1002. {
  1003. struct wpa_bss *bss;
  1004. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  1005. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  1006. wpa_s->fetch_anqp_in_progress = 1;
  1007. interworking_next_anqp_fetch(wpa_s);
  1008. }
  1009. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  1010. {
  1011. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  1012. return 0;
  1013. wpa_s->network_select = 0;
  1014. interworking_start_fetch_anqp(wpa_s);
  1015. return 0;
  1016. }
  1017. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  1018. {
  1019. if (!wpa_s->fetch_anqp_in_progress)
  1020. return;
  1021. wpa_s->fetch_anqp_in_progress = 0;
  1022. }
  1023. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  1024. u16 info_ids[], size_t num_ids)
  1025. {
  1026. struct wpabuf *buf;
  1027. int ret = 0;
  1028. int freq;
  1029. struct wpa_bss *bss;
  1030. int res;
  1031. freq = wpa_s->assoc_freq;
  1032. bss = wpa_bss_get_bssid(wpa_s, dst);
  1033. if (bss)
  1034. freq = bss->freq;
  1035. if (freq <= 0)
  1036. return -1;
  1037. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  1038. MAC2STR(dst), (unsigned int) num_ids);
  1039. buf = anqp_build_req(info_ids, num_ids, NULL);
  1040. if (buf == NULL)
  1041. return -1;
  1042. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  1043. if (res < 0) {
  1044. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  1045. ret = -1;
  1046. } else
  1047. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  1048. "%u", res);
  1049. wpabuf_free(buf);
  1050. return ret;
  1051. }
  1052. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  1053. const u8 *sa, u16 info_id,
  1054. const u8 *data, size_t slen)
  1055. {
  1056. const u8 *pos = data;
  1057. struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, sa);
  1058. switch (info_id) {
  1059. case ANQP_CAPABILITY_LIST:
  1060. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1061. " ANQP Capability list", MAC2STR(sa));
  1062. break;
  1063. case ANQP_VENUE_NAME:
  1064. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1065. " Venue Name", MAC2STR(sa));
  1066. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  1067. if (bss) {
  1068. wpabuf_free(bss->anqp_venue_name);
  1069. bss->anqp_venue_name = wpabuf_alloc_copy(pos, slen);
  1070. }
  1071. break;
  1072. case ANQP_NETWORK_AUTH_TYPE:
  1073. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1074. " Network Authentication Type information",
  1075. MAC2STR(sa));
  1076. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  1077. "Type", pos, slen);
  1078. if (bss) {
  1079. wpabuf_free(bss->anqp_network_auth_type);
  1080. bss->anqp_network_auth_type =
  1081. wpabuf_alloc_copy(pos, slen);
  1082. }
  1083. break;
  1084. case ANQP_ROAMING_CONSORTIUM:
  1085. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1086. " Roaming Consortium list", MAC2STR(sa));
  1087. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  1088. pos, slen);
  1089. if (bss) {
  1090. wpabuf_free(bss->anqp_roaming_consortium);
  1091. bss->anqp_roaming_consortium =
  1092. wpabuf_alloc_copy(pos, slen);
  1093. }
  1094. break;
  1095. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  1096. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1097. " IP Address Type Availability information",
  1098. MAC2STR(sa));
  1099. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  1100. pos, slen);
  1101. if (bss) {
  1102. wpabuf_free(bss->anqp_ip_addr_type_availability);
  1103. bss->anqp_ip_addr_type_availability =
  1104. wpabuf_alloc_copy(pos, slen);
  1105. }
  1106. break;
  1107. case ANQP_NAI_REALM:
  1108. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1109. " NAI Realm list", MAC2STR(sa));
  1110. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  1111. if (bss) {
  1112. wpabuf_free(bss->anqp_nai_realm);
  1113. bss->anqp_nai_realm = wpabuf_alloc_copy(pos, slen);
  1114. }
  1115. break;
  1116. case ANQP_3GPP_CELLULAR_NETWORK:
  1117. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1118. " 3GPP Cellular Network information", MAC2STR(sa));
  1119. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  1120. pos, slen);
  1121. if (bss) {
  1122. wpabuf_free(bss->anqp_3gpp);
  1123. bss->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  1124. }
  1125. break;
  1126. case ANQP_DOMAIN_NAME:
  1127. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  1128. " Domain Name list", MAC2STR(sa));
  1129. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  1130. if (bss) {
  1131. wpabuf_free(bss->anqp_domain_name);
  1132. bss->anqp_domain_name = wpabuf_alloc_copy(pos, slen);
  1133. }
  1134. break;
  1135. case ANQP_VENDOR_SPECIFIC:
  1136. if (slen < 3)
  1137. return;
  1138. switch (WPA_GET_BE24(pos)) {
  1139. default:
  1140. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  1141. "vendor-specific ANQP OUI %06x",
  1142. WPA_GET_BE24(pos));
  1143. return;
  1144. }
  1145. break;
  1146. default:
  1147. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  1148. "%u", info_id);
  1149. break;
  1150. }
  1151. }
  1152. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  1153. enum gas_query_result result,
  1154. const struct wpabuf *adv_proto,
  1155. const struct wpabuf *resp, u16 status_code)
  1156. {
  1157. struct wpa_supplicant *wpa_s = ctx;
  1158. const u8 *pos;
  1159. const u8 *end;
  1160. u16 info_id;
  1161. u16 slen;
  1162. if (result != GAS_QUERY_SUCCESS)
  1163. return;
  1164. pos = wpabuf_head(adv_proto);
  1165. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  1166. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  1167. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  1168. "Protocol in response");
  1169. return;
  1170. }
  1171. pos = wpabuf_head(resp);
  1172. end = pos + wpabuf_len(resp);
  1173. while (pos < end) {
  1174. if (pos + 4 > end) {
  1175. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  1176. break;
  1177. }
  1178. info_id = WPA_GET_LE16(pos);
  1179. pos += 2;
  1180. slen = WPA_GET_LE16(pos);
  1181. pos += 2;
  1182. if (pos + slen > end) {
  1183. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  1184. "for Info ID %u", info_id);
  1185. break;
  1186. }
  1187. interworking_parse_rx_anqp_resp(wpa_s, dst, info_id, pos,
  1188. slen);
  1189. pos += slen;
  1190. }
  1191. }
  1192. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  1193. struct wpa_scan_results *scan_res)
  1194. {
  1195. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  1196. "ANQP fetch");
  1197. interworking_start_fetch_anqp(wpa_s);
  1198. }
  1199. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select)
  1200. {
  1201. interworking_stop_fetch_anqp(wpa_s);
  1202. wpa_s->network_select = 1;
  1203. wpa_s->auto_select = !!auto_select;
  1204. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  1205. "selection");
  1206. wpa_s->scan_res_handler = interworking_scan_res_handler;
  1207. wpa_s->scan_req = 2;
  1208. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1209. return 0;
  1210. }