interworking.c 35 KB

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