interworking.c 43 KB

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