interworking.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882
  1. /*
  2. * Interworking (IEEE 802.11u)
  3. * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
  4. * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/gas.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "utils/pcsc_funcs.h"
  15. #include "utils/eloop.h"
  16. #include "drivers/driver.h"
  17. #include "eap_common/eap_defs.h"
  18. #include "eap_peer/eap.h"
  19. #include "eap_peer/eap_methods.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "config.h"
  24. #include "config_ssid.h"
  25. #include "bss.h"
  26. #include "scan.h"
  27. #include "notify.h"
  28. #include "gas_query.h"
  29. #include "hs20_supplicant.h"
  30. #include "interworking.h"
  31. #if defined(EAP_SIM) | defined(EAP_SIM_DYNAMIC)
  32. #define INTERWORKING_3GPP
  33. #else
  34. #if defined(EAP_AKA) | defined(EAP_AKA_DYNAMIC)
  35. #define INTERWORKING_3GPP
  36. #else
  37. #if defined(EAP_AKA_PRIME) | defined(EAP_AKA_PRIME_DYNAMIC)
  38. #define INTERWORKING_3GPP
  39. #endif
  40. #endif
  41. #endif
  42. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s);
  43. static struct wpa_cred * interworking_credentials_available_realm(
  44. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  45. int *excluded);
  46. static struct wpa_cred * interworking_credentials_available_3gpp(
  47. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  48. int *excluded);
  49. static int cred_prio_cmp(const struct wpa_cred *a, const struct wpa_cred *b)
  50. {
  51. if (a->priority > b->priority)
  52. return 1;
  53. if (a->priority < b->priority)
  54. return -1;
  55. if (a->provisioning_sp == NULL || b->provisioning_sp == NULL ||
  56. os_strcmp(a->provisioning_sp, b->provisioning_sp) != 0)
  57. return 0;
  58. if (a->sp_priority < b->sp_priority)
  59. return 1;
  60. if (a->sp_priority > b->sp_priority)
  61. return -1;
  62. return 0;
  63. }
  64. static void interworking_reconnect(struct wpa_supplicant *wpa_s)
  65. {
  66. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  67. wpa_supplicant_cancel_sched_scan(wpa_s);
  68. wpa_supplicant_deauthenticate(wpa_s,
  69. WLAN_REASON_DEAUTH_LEAVING);
  70. }
  71. wpa_s->disconnected = 0;
  72. wpa_s->reassociate = 1;
  73. if (wpa_supplicant_fast_associate(wpa_s) >= 0)
  74. return;
  75. wpa_supplicant_req_scan(wpa_s, 0, 0);
  76. }
  77. static struct wpabuf * anqp_build_req(u16 info_ids[], size_t num_ids,
  78. struct wpabuf *extra)
  79. {
  80. struct wpabuf *buf;
  81. size_t i;
  82. u8 *len_pos;
  83. buf = gas_anqp_build_initial_req(0, 4 + num_ids * 2 +
  84. (extra ? wpabuf_len(extra) : 0));
  85. if (buf == NULL)
  86. return NULL;
  87. len_pos = gas_anqp_add_element(buf, ANQP_QUERY_LIST);
  88. for (i = 0; i < num_ids; i++)
  89. wpabuf_put_le16(buf, info_ids[i]);
  90. gas_anqp_set_element_len(buf, len_pos);
  91. if (extra)
  92. wpabuf_put_buf(buf, extra);
  93. gas_anqp_set_len(buf);
  94. return buf;
  95. }
  96. static void interworking_anqp_resp_cb(void *ctx, const u8 *dst,
  97. u8 dialog_token,
  98. enum gas_query_result result,
  99. const struct wpabuf *adv_proto,
  100. const struct wpabuf *resp,
  101. u16 status_code)
  102. {
  103. struct wpa_supplicant *wpa_s = ctx;
  104. wpa_printf(MSG_DEBUG, "ANQP: Response callback dst=" MACSTR
  105. " dialog_token=%u result=%d status_code=%u",
  106. MAC2STR(dst), dialog_token, result, status_code);
  107. anqp_resp_cb(wpa_s, dst, dialog_token, result, adv_proto, resp,
  108. status_code);
  109. interworking_next_anqp_fetch(wpa_s);
  110. }
  111. static int cred_with_roaming_consortium(struct wpa_supplicant *wpa_s)
  112. {
  113. struct wpa_cred *cred;
  114. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  115. if (cred->roaming_consortium_len)
  116. return 1;
  117. if (cred->required_roaming_consortium_len)
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. static int cred_with_3gpp(struct wpa_supplicant *wpa_s)
  123. {
  124. struct wpa_cred *cred;
  125. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  126. if (cred->pcsc || cred->imsi)
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. static int cred_with_nai_realm(struct wpa_supplicant *wpa_s)
  132. {
  133. struct wpa_cred *cred;
  134. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  135. if (cred->pcsc || cred->imsi)
  136. continue;
  137. if (!cred->eap_method)
  138. return 1;
  139. if (cred->realm && cred->roaming_consortium_len == 0)
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. static int cred_with_domain(struct wpa_supplicant *wpa_s)
  145. {
  146. struct wpa_cred *cred;
  147. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  148. if (cred->domain || cred->pcsc || cred->imsi ||
  149. cred->roaming_partner)
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. #ifdef CONFIG_HS20
  155. static int cred_with_min_backhaul(struct wpa_supplicant *wpa_s)
  156. {
  157. struct wpa_cred *cred;
  158. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  159. if (cred->min_dl_bandwidth_home ||
  160. cred->min_ul_bandwidth_home ||
  161. cred->min_dl_bandwidth_roaming ||
  162. cred->min_ul_bandwidth_roaming)
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. static int cred_with_conn_capab(struct wpa_supplicant *wpa_s)
  168. {
  169. struct wpa_cred *cred;
  170. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  171. if (cred->num_req_conn_capab)
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. #endif /* CONFIG_HS20 */
  177. static int additional_roaming_consortiums(struct wpa_bss *bss)
  178. {
  179. const u8 *ie;
  180. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  181. if (ie == NULL || ie[1] == 0)
  182. return 0;
  183. return ie[2]; /* Number of ANQP OIs */
  184. }
  185. static void interworking_continue_anqp(void *eloop_ctx, void *sock_ctx)
  186. {
  187. struct wpa_supplicant *wpa_s = eloop_ctx;
  188. interworking_next_anqp_fetch(wpa_s);
  189. }
  190. static int interworking_anqp_send_req(struct wpa_supplicant *wpa_s,
  191. struct wpa_bss *bss)
  192. {
  193. struct wpabuf *buf;
  194. int ret = 0;
  195. int res;
  196. u16 info_ids[8];
  197. size_t num_info_ids = 0;
  198. struct wpabuf *extra = NULL;
  199. int all = wpa_s->fetch_all_anqp;
  200. wpa_printf(MSG_DEBUG, "Interworking: ANQP Query Request to " MACSTR,
  201. MAC2STR(bss->bssid));
  202. wpa_s->interworking_gas_bss = bss;
  203. info_ids[num_info_ids++] = ANQP_CAPABILITY_LIST;
  204. if (all) {
  205. info_ids[num_info_ids++] = ANQP_VENUE_NAME;
  206. info_ids[num_info_ids++] = ANQP_NETWORK_AUTH_TYPE;
  207. }
  208. if (all || (cred_with_roaming_consortium(wpa_s) &&
  209. additional_roaming_consortiums(bss)))
  210. info_ids[num_info_ids++] = ANQP_ROAMING_CONSORTIUM;
  211. if (all)
  212. info_ids[num_info_ids++] = ANQP_IP_ADDR_TYPE_AVAILABILITY;
  213. if (all || cred_with_nai_realm(wpa_s))
  214. info_ids[num_info_ids++] = ANQP_NAI_REALM;
  215. if (all || cred_with_3gpp(wpa_s))
  216. info_ids[num_info_ids++] = ANQP_3GPP_CELLULAR_NETWORK;
  217. if (all || cred_with_domain(wpa_s))
  218. info_ids[num_info_ids++] = ANQP_DOMAIN_NAME;
  219. wpa_hexdump(MSG_DEBUG, "Interworking: ANQP Query info",
  220. (u8 *) info_ids, num_info_ids * 2);
  221. #ifdef CONFIG_HS20
  222. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  223. u8 *len_pos;
  224. extra = wpabuf_alloc(100);
  225. if (!extra)
  226. return -1;
  227. len_pos = gas_anqp_add_element(extra, ANQP_VENDOR_SPECIFIC);
  228. wpabuf_put_be24(extra, OUI_WFA);
  229. wpabuf_put_u8(extra, HS20_ANQP_OUI_TYPE);
  230. wpabuf_put_u8(extra, HS20_STYPE_QUERY_LIST);
  231. wpabuf_put_u8(extra, 0); /* Reserved */
  232. wpabuf_put_u8(extra, HS20_STYPE_CAPABILITY_LIST);
  233. if (all)
  234. wpabuf_put_u8(extra,
  235. HS20_STYPE_OPERATOR_FRIENDLY_NAME);
  236. if (all || cred_with_min_backhaul(wpa_s))
  237. wpabuf_put_u8(extra, HS20_STYPE_WAN_METRICS);
  238. if (all || cred_with_conn_capab(wpa_s))
  239. wpabuf_put_u8(extra, HS20_STYPE_CONNECTION_CAPABILITY);
  240. if (all)
  241. wpabuf_put_u8(extra, HS20_STYPE_OPERATING_CLASS);
  242. if (all)
  243. wpabuf_put_u8(extra, HS20_STYPE_OSU_PROVIDERS_LIST);
  244. gas_anqp_set_element_len(extra, len_pos);
  245. }
  246. #endif /* CONFIG_HS20 */
  247. buf = anqp_build_req(info_ids, num_info_ids, extra);
  248. wpabuf_free(extra);
  249. if (buf == NULL)
  250. return -1;
  251. res = gas_query_req(wpa_s->gas, bss->bssid, bss->freq, buf,
  252. interworking_anqp_resp_cb, wpa_s);
  253. if (res < 0) {
  254. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  255. wpabuf_free(buf);
  256. ret = -1;
  257. eloop_register_timeout(0, 0, interworking_continue_anqp, wpa_s,
  258. NULL);
  259. } else
  260. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  261. "%u", res);
  262. return ret;
  263. }
  264. struct nai_realm_eap {
  265. u8 method;
  266. u8 inner_method;
  267. enum nai_realm_eap_auth_inner_non_eap inner_non_eap;
  268. u8 cred_type;
  269. u8 tunneled_cred_type;
  270. };
  271. struct nai_realm {
  272. u8 encoding;
  273. char *realm;
  274. u8 eap_count;
  275. struct nai_realm_eap *eap;
  276. };
  277. static void nai_realm_free(struct nai_realm *realms, u16 count)
  278. {
  279. u16 i;
  280. if (realms == NULL)
  281. return;
  282. for (i = 0; i < count; i++) {
  283. os_free(realms[i].eap);
  284. os_free(realms[i].realm);
  285. }
  286. os_free(realms);
  287. }
  288. static const u8 * nai_realm_parse_eap(struct nai_realm_eap *e, const u8 *pos,
  289. const u8 *end)
  290. {
  291. u8 elen, auth_count, a;
  292. const u8 *e_end;
  293. if (pos + 3 > end) {
  294. wpa_printf(MSG_DEBUG, "No room for EAP Method fixed fields");
  295. return NULL;
  296. }
  297. elen = *pos++;
  298. if (pos + elen > end || elen < 2) {
  299. wpa_printf(MSG_DEBUG, "No room for EAP Method subfield");
  300. return NULL;
  301. }
  302. e_end = pos + elen;
  303. e->method = *pos++;
  304. auth_count = *pos++;
  305. wpa_printf(MSG_DEBUG, "EAP Method: len=%u method=%u auth_count=%u",
  306. elen, e->method, auth_count);
  307. for (a = 0; a < auth_count; a++) {
  308. u8 id, len;
  309. if (pos + 2 > end || pos + 2 + pos[1] > end) {
  310. wpa_printf(MSG_DEBUG, "No room for Authentication "
  311. "Parameter subfield");
  312. return NULL;
  313. }
  314. id = *pos++;
  315. len = *pos++;
  316. switch (id) {
  317. case NAI_REALM_EAP_AUTH_NON_EAP_INNER_AUTH:
  318. if (len < 1)
  319. break;
  320. e->inner_non_eap = *pos;
  321. if (e->method != EAP_TYPE_TTLS)
  322. break;
  323. switch (*pos) {
  324. case NAI_REALM_INNER_NON_EAP_PAP:
  325. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP");
  326. break;
  327. case NAI_REALM_INNER_NON_EAP_CHAP:
  328. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP");
  329. break;
  330. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  331. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP");
  332. break;
  333. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  334. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2");
  335. break;
  336. }
  337. break;
  338. case NAI_REALM_EAP_AUTH_INNER_AUTH_EAP_METHOD:
  339. if (len < 1)
  340. break;
  341. e->inner_method = *pos;
  342. wpa_printf(MSG_DEBUG, "Inner EAP method: %u",
  343. e->inner_method);
  344. break;
  345. case NAI_REALM_EAP_AUTH_CRED_TYPE:
  346. if (len < 1)
  347. break;
  348. e->cred_type = *pos;
  349. wpa_printf(MSG_DEBUG, "Credential Type: %u",
  350. e->cred_type);
  351. break;
  352. case NAI_REALM_EAP_AUTH_TUNNELED_CRED_TYPE:
  353. if (len < 1)
  354. break;
  355. e->tunneled_cred_type = *pos;
  356. wpa_printf(MSG_DEBUG, "Tunneled EAP Method Credential "
  357. "Type: %u", e->tunneled_cred_type);
  358. break;
  359. default:
  360. wpa_printf(MSG_DEBUG, "Unsupported Authentication "
  361. "Parameter: id=%u len=%u", id, len);
  362. wpa_hexdump(MSG_DEBUG, "Authentication Parameter "
  363. "Value", pos, len);
  364. break;
  365. }
  366. pos += len;
  367. }
  368. return e_end;
  369. }
  370. static const u8 * nai_realm_parse_realm(struct nai_realm *r, const u8 *pos,
  371. const u8 *end)
  372. {
  373. u16 len;
  374. const u8 *f_end;
  375. u8 realm_len, e;
  376. if (end - pos < 4) {
  377. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  378. "fixed fields");
  379. return NULL;
  380. }
  381. len = WPA_GET_LE16(pos); /* NAI Realm Data field Length */
  382. pos += 2;
  383. if (pos + len > end || len < 3) {
  384. wpa_printf(MSG_DEBUG, "No room for NAI Realm Data "
  385. "(len=%u; left=%u)",
  386. len, (unsigned int) (end - pos));
  387. return NULL;
  388. }
  389. f_end = pos + len;
  390. r->encoding = *pos++;
  391. realm_len = *pos++;
  392. if (pos + realm_len > f_end) {
  393. wpa_printf(MSG_DEBUG, "No room for NAI Realm "
  394. "(len=%u; left=%u)",
  395. realm_len, (unsigned int) (f_end - pos));
  396. return NULL;
  397. }
  398. wpa_hexdump_ascii(MSG_DEBUG, "NAI Realm", pos, realm_len);
  399. r->realm = dup_binstr(pos, realm_len);
  400. if (r->realm == NULL)
  401. return NULL;
  402. pos += realm_len;
  403. if (pos + 1 > f_end) {
  404. wpa_printf(MSG_DEBUG, "No room for EAP Method Count");
  405. return NULL;
  406. }
  407. r->eap_count = *pos++;
  408. wpa_printf(MSG_DEBUG, "EAP Count: %u", r->eap_count);
  409. if (pos + r->eap_count * 3 > f_end) {
  410. wpa_printf(MSG_DEBUG, "No room for EAP Methods");
  411. return NULL;
  412. }
  413. r->eap = os_calloc(r->eap_count, sizeof(struct nai_realm_eap));
  414. if (r->eap == NULL)
  415. return NULL;
  416. for (e = 0; e < r->eap_count; e++) {
  417. pos = nai_realm_parse_eap(&r->eap[e], pos, f_end);
  418. if (pos == NULL)
  419. return NULL;
  420. }
  421. return f_end;
  422. }
  423. static struct nai_realm * nai_realm_parse(struct wpabuf *anqp, u16 *count)
  424. {
  425. struct nai_realm *realm;
  426. const u8 *pos, *end;
  427. u16 i, num;
  428. if (anqp == NULL || wpabuf_len(anqp) < 2)
  429. return NULL;
  430. pos = wpabuf_head_u8(anqp);
  431. end = pos + wpabuf_len(anqp);
  432. num = WPA_GET_LE16(pos);
  433. wpa_printf(MSG_DEBUG, "NAI Realm Count: %u", num);
  434. pos += 2;
  435. if (num * 5 > end - pos) {
  436. wpa_printf(MSG_DEBUG, "Invalid NAI Realm Count %u - not "
  437. "enough data (%u octets) for that many realms",
  438. num, (unsigned int) (end - pos));
  439. return NULL;
  440. }
  441. realm = os_calloc(num, sizeof(struct nai_realm));
  442. if (realm == NULL)
  443. return NULL;
  444. for (i = 0; i < num; i++) {
  445. pos = nai_realm_parse_realm(&realm[i], pos, end);
  446. if (pos == NULL) {
  447. nai_realm_free(realm, num);
  448. return NULL;
  449. }
  450. }
  451. *count = num;
  452. return realm;
  453. }
  454. static int nai_realm_match(struct nai_realm *realm, const char *home_realm)
  455. {
  456. char *tmp, *pos, *end;
  457. int match = 0;
  458. if (realm->realm == NULL || home_realm == NULL)
  459. return 0;
  460. if (os_strchr(realm->realm, ';') == NULL)
  461. return os_strcasecmp(realm->realm, home_realm) == 0;
  462. tmp = os_strdup(realm->realm);
  463. if (tmp == NULL)
  464. return 0;
  465. pos = tmp;
  466. while (*pos) {
  467. end = os_strchr(pos, ';');
  468. if (end)
  469. *end = '\0';
  470. if (os_strcasecmp(pos, home_realm) == 0) {
  471. match = 1;
  472. break;
  473. }
  474. if (end == NULL)
  475. break;
  476. pos = end + 1;
  477. }
  478. os_free(tmp);
  479. return match;
  480. }
  481. static int nai_realm_cred_username(struct nai_realm_eap *eap)
  482. {
  483. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  484. return 0; /* method not supported */
  485. if (eap->method != EAP_TYPE_TTLS && eap->method != EAP_TYPE_PEAP &&
  486. eap->method != EAP_TYPE_FAST) {
  487. /* Only tunneled methods with username/password supported */
  488. return 0;
  489. }
  490. if (eap->method == EAP_TYPE_PEAP || eap->method == EAP_TYPE_FAST) {
  491. if (eap->inner_method &&
  492. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  493. return 0;
  494. if (!eap->inner_method &&
  495. eap_get_name(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2) == NULL)
  496. return 0;
  497. }
  498. if (eap->method == EAP_TYPE_TTLS) {
  499. if (eap->inner_method == 0 && eap->inner_non_eap == 0)
  500. return 1; /* Assume TTLS/MSCHAPv2 is used */
  501. if (eap->inner_method &&
  502. eap_get_name(EAP_VENDOR_IETF, eap->inner_method) == NULL)
  503. return 0;
  504. if (eap->inner_non_eap &&
  505. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_PAP &&
  506. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_CHAP &&
  507. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAP &&
  508. eap->inner_non_eap != NAI_REALM_INNER_NON_EAP_MSCHAPV2)
  509. return 0;
  510. }
  511. if (eap->inner_method &&
  512. eap->inner_method != EAP_TYPE_GTC &&
  513. eap->inner_method != EAP_TYPE_MSCHAPV2)
  514. return 0;
  515. return 1;
  516. }
  517. static int nai_realm_cred_cert(struct nai_realm_eap *eap)
  518. {
  519. if (eap_get_name(EAP_VENDOR_IETF, eap->method) == NULL)
  520. return 0; /* method not supported */
  521. if (eap->method != EAP_TYPE_TLS) {
  522. /* Only EAP-TLS supported for credential authentication */
  523. return 0;
  524. }
  525. return 1;
  526. }
  527. static struct nai_realm_eap * nai_realm_find_eap(struct wpa_cred *cred,
  528. struct nai_realm *realm)
  529. {
  530. u8 e;
  531. if (cred == NULL ||
  532. cred->username == NULL ||
  533. cred->username[0] == '\0' ||
  534. ((cred->password == NULL ||
  535. cred->password[0] == '\0') &&
  536. (cred->private_key == NULL ||
  537. cred->private_key[0] == '\0')))
  538. return NULL;
  539. for (e = 0; e < realm->eap_count; e++) {
  540. struct nai_realm_eap *eap = &realm->eap[e];
  541. if (cred->password && cred->password[0] &&
  542. nai_realm_cred_username(eap))
  543. return eap;
  544. if (cred->private_key && cred->private_key[0] &&
  545. nai_realm_cred_cert(eap))
  546. return eap;
  547. }
  548. return NULL;
  549. }
  550. #ifdef INTERWORKING_3GPP
  551. static int plmn_id_match(struct wpabuf *anqp, const char *imsi, int mnc_len)
  552. {
  553. u8 plmn[3], plmn2[3];
  554. const u8 *pos, *end;
  555. u8 udhl;
  556. /*
  557. * See Annex A of 3GPP TS 24.234 v8.1.0 for description. The network
  558. * operator is allowed to include only two digits of the MNC, so allow
  559. * matches based on both two and three digit MNC assumptions. Since some
  560. * SIM/USIM cards may not expose MNC length conveniently, we may be
  561. * provided the default MNC length 3 here and as such, checking with MNC
  562. * length 2 is justifiable even though 3GPP TS 24.234 does not mention
  563. * that case. Anyway, MCC/MNC pair where both 2 and 3 digit MNC is used
  564. * with otherwise matching values would not be good idea in general, so
  565. * this should not result in selecting incorrect networks.
  566. */
  567. /* Match with 3 digit MNC */
  568. plmn[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  569. plmn[1] = (imsi[2] - '0') | ((imsi[5] - '0') << 4);
  570. plmn[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  571. /* Match with 2 digit MNC */
  572. plmn2[0] = (imsi[0] - '0') | ((imsi[1] - '0') << 4);
  573. plmn2[1] = (imsi[2] - '0') | 0xf0;
  574. plmn2[2] = (imsi[3] - '0') | ((imsi[4] - '0') << 4);
  575. if (anqp == NULL)
  576. return 0;
  577. pos = wpabuf_head_u8(anqp);
  578. end = pos + wpabuf_len(anqp);
  579. if (pos + 2 > end)
  580. return 0;
  581. if (*pos != 0) {
  582. wpa_printf(MSG_DEBUG, "Unsupported GUD version 0x%x", *pos);
  583. return 0;
  584. }
  585. pos++;
  586. udhl = *pos++;
  587. if (pos + udhl > end) {
  588. wpa_printf(MSG_DEBUG, "Invalid UDHL");
  589. return 0;
  590. }
  591. end = pos + udhl;
  592. wpa_printf(MSG_DEBUG, "Interworking: Matching against MCC/MNC alternatives: %02x:%02x:%02x or %02x:%02x:%02x (IMSI %s, MNC length %d)",
  593. plmn[0], plmn[1], plmn[2], plmn2[0], plmn2[1], plmn2[2],
  594. imsi, mnc_len);
  595. while (pos + 2 <= end) {
  596. u8 iei, len;
  597. const u8 *l_end;
  598. iei = *pos++;
  599. len = *pos++ & 0x7f;
  600. if (pos + len > end)
  601. break;
  602. l_end = pos + len;
  603. if (iei == 0 && len > 0) {
  604. /* PLMN List */
  605. u8 num, i;
  606. wpa_hexdump(MSG_DEBUG, "Interworking: PLMN List information element",
  607. pos, len);
  608. num = *pos++;
  609. for (i = 0; i < num; i++) {
  610. if (pos + 3 > l_end)
  611. break;
  612. if (os_memcmp(pos, plmn, 3) == 0 ||
  613. os_memcmp(pos, plmn2, 3) == 0)
  614. return 1; /* Found matching PLMN */
  615. pos += 3;
  616. }
  617. } else {
  618. wpa_hexdump(MSG_DEBUG, "Interworking: Unrecognized 3GPP information element",
  619. pos, len);
  620. }
  621. pos = l_end;
  622. }
  623. return 0;
  624. }
  625. static int build_root_nai(char *nai, size_t nai_len, const char *imsi,
  626. size_t mnc_len, char prefix)
  627. {
  628. const char *sep, *msin;
  629. char *end, *pos;
  630. size_t msin_len, plmn_len;
  631. /*
  632. * TS 23.003, Clause 14 (3GPP to WLAN Interworking)
  633. * Root NAI:
  634. * <aka:0|sim:1><IMSI>@wlan.mnc<MNC>.mcc<MCC>.3gppnetwork.org
  635. * <MNC> is zero-padded to three digits in case two-digit MNC is used
  636. */
  637. if (imsi == NULL || os_strlen(imsi) > 16) {
  638. wpa_printf(MSG_DEBUG, "No valid IMSI available");
  639. return -1;
  640. }
  641. sep = os_strchr(imsi, '-');
  642. if (sep) {
  643. plmn_len = sep - imsi;
  644. msin = sep + 1;
  645. } else if (mnc_len && os_strlen(imsi) >= 3 + mnc_len) {
  646. plmn_len = 3 + mnc_len;
  647. msin = imsi + plmn_len;
  648. } else
  649. return -1;
  650. if (plmn_len != 5 && plmn_len != 6)
  651. return -1;
  652. msin_len = os_strlen(msin);
  653. pos = nai;
  654. end = nai + nai_len;
  655. if (prefix)
  656. *pos++ = prefix;
  657. os_memcpy(pos, imsi, plmn_len);
  658. pos += plmn_len;
  659. os_memcpy(pos, msin, msin_len);
  660. pos += msin_len;
  661. pos += os_snprintf(pos, end - pos, "@wlan.mnc");
  662. if (plmn_len == 5) {
  663. *pos++ = '0';
  664. *pos++ = imsi[3];
  665. *pos++ = imsi[4];
  666. } else {
  667. *pos++ = imsi[3];
  668. *pos++ = imsi[4];
  669. *pos++ = imsi[5];
  670. }
  671. pos += os_snprintf(pos, end - pos, ".mcc%c%c%c.3gppnetwork.org",
  672. imsi[0], imsi[1], imsi[2]);
  673. return 0;
  674. }
  675. static int set_root_nai(struct wpa_ssid *ssid, const char *imsi, char prefix)
  676. {
  677. char nai[100];
  678. if (build_root_nai(nai, sizeof(nai), imsi, 0, prefix) < 0)
  679. return -1;
  680. return wpa_config_set_quoted(ssid, "identity", nai);
  681. }
  682. #endif /* INTERWORKING_3GPP */
  683. static int already_connected(struct wpa_supplicant *wpa_s,
  684. struct wpa_cred *cred, struct wpa_bss *bss)
  685. {
  686. struct wpa_ssid *ssid;
  687. if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
  688. return 0;
  689. ssid = wpa_s->current_ssid;
  690. if (ssid->parent_cred != cred)
  691. return 0;
  692. if (ssid->ssid_len != bss->ssid_len ||
  693. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  694. return 0;
  695. return 1;
  696. }
  697. static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
  698. struct wpa_cred *cred,
  699. struct wpa_bss *bss)
  700. {
  701. struct wpa_ssid *ssid;
  702. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  703. if (ssid->parent_cred != cred)
  704. continue;
  705. if (ssid->ssid_len != bss->ssid_len ||
  706. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  707. continue;
  708. break;
  709. }
  710. if (ssid == NULL)
  711. return;
  712. wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
  713. if (ssid == wpa_s->current_ssid) {
  714. wpa_sm_set_config(wpa_s->wpa, NULL);
  715. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  716. wpa_supplicant_deauthenticate(wpa_s,
  717. WLAN_REASON_DEAUTH_LEAVING);
  718. }
  719. wpas_notify_network_removed(wpa_s, ssid);
  720. wpa_config_remove_network(wpa_s->conf, ssid->id);
  721. }
  722. static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
  723. struct wpa_ssid *ssid)
  724. {
  725. if (wpa_config_set(ssid, "key_mgmt",
  726. wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
  727. "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
  728. return -1;
  729. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  730. return -1;
  731. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  732. return -1;
  733. return 0;
  734. }
  735. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  736. struct wpa_cred *cred,
  737. struct wpa_bss *bss)
  738. {
  739. #ifdef INTERWORKING_3GPP
  740. struct wpa_ssid *ssid;
  741. int eap_type;
  742. int res;
  743. char prefix;
  744. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  745. return -1;
  746. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  747. MAC2STR(bss->bssid));
  748. if (already_connected(wpa_s, cred, bss)) {
  749. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  750. MAC2STR(bss->bssid));
  751. return 0;
  752. }
  753. remove_duplicate_network(wpa_s, cred, bss);
  754. ssid = wpa_config_add_network(wpa_s->conf);
  755. if (ssid == NULL)
  756. return -1;
  757. ssid->parent_cred = cred;
  758. wpas_notify_network_added(wpa_s, ssid);
  759. wpa_config_set_network_defaults(ssid);
  760. ssid->priority = cred->priority;
  761. ssid->temporary = 1;
  762. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  763. if (ssid->ssid == NULL)
  764. goto fail;
  765. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  766. ssid->ssid_len = bss->ssid_len;
  767. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  768. goto fail;
  769. eap_type = EAP_TYPE_SIM;
  770. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  771. eap_type = EAP_TYPE_AKA;
  772. if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
  773. if (cred->eap_method[0].method == EAP_TYPE_SIM ||
  774. cred->eap_method[0].method == EAP_TYPE_AKA ||
  775. cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
  776. eap_type = cred->eap_method[0].method;
  777. }
  778. switch (eap_type) {
  779. case EAP_TYPE_SIM:
  780. prefix = '1';
  781. res = wpa_config_set(ssid, "eap", "SIM", 0);
  782. break;
  783. case EAP_TYPE_AKA:
  784. prefix = '0';
  785. res = wpa_config_set(ssid, "eap", "AKA", 0);
  786. break;
  787. case EAP_TYPE_AKA_PRIME:
  788. prefix = '6';
  789. res = wpa_config_set(ssid, "eap", "AKA'", 0);
  790. break;
  791. default:
  792. res = -1;
  793. break;
  794. }
  795. if (res < 0) {
  796. wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
  797. eap_type);
  798. goto fail;
  799. }
  800. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
  801. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  802. goto fail;
  803. }
  804. if (cred->milenage && cred->milenage[0]) {
  805. if (wpa_config_set_quoted(ssid, "password",
  806. cred->milenage) < 0)
  807. goto fail;
  808. } else if (cred->pcsc) {
  809. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  810. goto fail;
  811. if (wpa_s->conf->pcsc_pin &&
  812. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  813. < 0)
  814. goto fail;
  815. }
  816. if (cred->password && cred->password[0] &&
  817. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  818. goto fail;
  819. wpa_s->next_ssid = ssid;
  820. wpa_config_update_prio_list(wpa_s->conf);
  821. interworking_reconnect(wpa_s);
  822. return 0;
  823. fail:
  824. wpas_notify_network_removed(wpa_s, ssid);
  825. wpa_config_remove_network(wpa_s->conf, ssid->id);
  826. #endif /* INTERWORKING_3GPP */
  827. return -1;
  828. }
  829. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  830. size_t rc_len)
  831. {
  832. const u8 *pos, *end;
  833. u8 lens;
  834. if (ie == NULL)
  835. return 0;
  836. pos = ie + 2;
  837. end = ie + 2 + ie[1];
  838. /* Roaming Consortium element:
  839. * Number of ANQP OIs
  840. * OI #1 and #2 lengths
  841. * OI #1, [OI #2], [OI #3]
  842. */
  843. if (pos + 2 > end)
  844. return 0;
  845. pos++; /* skip Number of ANQP OIs */
  846. lens = *pos++;
  847. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  848. return 0;
  849. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  850. return 1;
  851. pos += lens & 0x0f;
  852. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  853. return 1;
  854. pos += lens >> 4;
  855. if (pos < end && (size_t) (end - pos) == rc_len &&
  856. os_memcmp(pos, rc_id, rc_len) == 0)
  857. return 1;
  858. return 0;
  859. }
  860. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  861. const u8 *rc_id, size_t rc_len)
  862. {
  863. const u8 *pos, *end;
  864. u8 len;
  865. if (anqp == NULL)
  866. return 0;
  867. pos = wpabuf_head(anqp);
  868. end = pos + wpabuf_len(anqp);
  869. /* Set of <OI Length, OI> duples */
  870. while (pos < end) {
  871. len = *pos++;
  872. if (pos + len > end)
  873. break;
  874. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  875. return 1;
  876. pos += len;
  877. }
  878. return 0;
  879. }
  880. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  881. const u8 *rc_id, size_t rc_len)
  882. {
  883. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  884. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  885. }
  886. static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
  887. {
  888. const u8 *ie;
  889. if (cred->required_roaming_consortium_len == 0)
  890. return 0;
  891. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  892. if (ie == NULL &&
  893. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  894. return 1;
  895. return !roaming_consortium_match(ie,
  896. bss->anqp ?
  897. bss->anqp->roaming_consortium : NULL,
  898. cred->required_roaming_consortium,
  899. cred->required_roaming_consortium_len);
  900. }
  901. static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
  902. {
  903. size_t i;
  904. if (!cred->excluded_ssid)
  905. return 0;
  906. for (i = 0; i < cred->num_excluded_ssid; i++) {
  907. struct excluded_ssid *e = &cred->excluded_ssid[i];
  908. if (bss->ssid_len == e->ssid_len &&
  909. os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
  910. return 1;
  911. }
  912. return 0;
  913. }
  914. static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
  915. struct wpa_cred *cred, struct wpa_bss *bss)
  916. {
  917. int res;
  918. unsigned int dl_bandwidth, ul_bandwidth;
  919. const u8 *wan;
  920. u8 wan_info, dl_load, ul_load;
  921. u16 lmd;
  922. u32 ul_speed, dl_speed;
  923. if (!cred->min_dl_bandwidth_home &&
  924. !cred->min_ul_bandwidth_home &&
  925. !cred->min_dl_bandwidth_roaming &&
  926. !cred->min_ul_bandwidth_roaming)
  927. return 0; /* No bandwidth constraint specified */
  928. if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
  929. return 0; /* No WAN Metrics known - ignore constraint */
  930. wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
  931. wan_info = wan[0];
  932. if (wan_info & BIT(3))
  933. return 1; /* WAN link at capacity */
  934. lmd = WPA_GET_LE16(wan + 11);
  935. if (lmd == 0)
  936. return 0; /* Downlink/Uplink Load was not measured */
  937. dl_speed = WPA_GET_LE32(wan + 1);
  938. ul_speed = WPA_GET_LE32(wan + 5);
  939. dl_load = wan[9];
  940. ul_load = wan[10];
  941. if (dl_speed >= 0xffffff)
  942. dl_bandwidth = dl_speed / 255 * (255 - dl_load);
  943. else
  944. dl_bandwidth = dl_speed * (255 - dl_load) / 255;
  945. if (ul_speed >= 0xffffff)
  946. ul_bandwidth = ul_speed / 255 * (255 - ul_load);
  947. else
  948. ul_bandwidth = ul_speed * (255 - ul_load) / 255;
  949. res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
  950. bss->anqp->domain_name : NULL);
  951. if (res > 0) {
  952. if (cred->min_dl_bandwidth_home > dl_bandwidth)
  953. return 1;
  954. if (cred->min_ul_bandwidth_home > ul_bandwidth)
  955. return 1;
  956. } else {
  957. if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
  958. return 1;
  959. if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
  960. return 1;
  961. }
  962. return 0;
  963. }
  964. static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
  965. struct wpa_cred *cred, struct wpa_bss *bss)
  966. {
  967. const u8 *ie;
  968. int res;
  969. if (!cred->max_bss_load)
  970. return 0; /* No BSS Load constraint specified */
  971. ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
  972. if (ie == NULL || ie[1] < 3)
  973. return 0; /* No BSS Load advertised */
  974. res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
  975. bss->anqp->domain_name : NULL);
  976. if (res <= 0)
  977. return 0; /* Not a home network */
  978. return ie[4] > cred->max_bss_load;
  979. }
  980. static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
  981. {
  982. while (pos + 4 <= end) {
  983. if (pos[0] == proto && pos[3] == 1 /* Open */)
  984. return 1;
  985. pos += 4;
  986. }
  987. return 0;
  988. }
  989. static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
  990. u16 port)
  991. {
  992. while (pos + 4 <= end) {
  993. if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
  994. pos[3] == 1 /* Open */)
  995. return 1;
  996. pos += 4;
  997. }
  998. return 0;
  999. }
  1000. static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
  1001. struct wpa_cred *cred, struct wpa_bss *bss)
  1002. {
  1003. int res;
  1004. const u8 *capab, *end;
  1005. unsigned int i, j;
  1006. int *ports;
  1007. if (!cred->num_req_conn_capab)
  1008. return 0; /* No connection capability constraint specified */
  1009. if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
  1010. return 0; /* No Connection Capability known - ignore constraint
  1011. */
  1012. res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
  1013. bss->anqp->domain_name : NULL);
  1014. if (res > 0)
  1015. return 0; /* No constraint in home network */
  1016. capab = wpabuf_head(bss->anqp->hs20_connection_capability);
  1017. end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
  1018. for (i = 0; i < cred->num_req_conn_capab; i++) {
  1019. ports = cred->req_conn_capab_port[i];
  1020. if (!ports) {
  1021. if (!has_proto_match(capab, end,
  1022. cred->req_conn_capab_proto[i]))
  1023. return 1;
  1024. } else {
  1025. for (j = 0; ports[j] > -1; j++) {
  1026. if (!has_proto_port_match(
  1027. capab, end,
  1028. cred->req_conn_capab_proto[i],
  1029. ports[j]))
  1030. return 1;
  1031. }
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  1037. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1038. int *excluded)
  1039. {
  1040. struct wpa_cred *cred, *selected = NULL;
  1041. const u8 *ie;
  1042. int is_excluded = 0;
  1043. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  1044. if (ie == NULL &&
  1045. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  1046. return NULL;
  1047. if (wpa_s->conf->cred == NULL)
  1048. return NULL;
  1049. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1050. if (cred->roaming_consortium_len == 0)
  1051. continue;
  1052. if (!roaming_consortium_match(ie,
  1053. bss->anqp ?
  1054. bss->anqp->roaming_consortium :
  1055. NULL,
  1056. cred->roaming_consortium,
  1057. cred->roaming_consortium_len))
  1058. continue;
  1059. if (cred_no_required_oi_match(cred, bss))
  1060. continue;
  1061. if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
  1062. continue;
  1063. if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
  1064. continue;
  1065. if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
  1066. continue;
  1067. if (cred_excluded_ssid(cred, bss)) {
  1068. if (excluded == NULL)
  1069. continue;
  1070. if (selected == NULL) {
  1071. selected = cred;
  1072. is_excluded = 1;
  1073. }
  1074. } else {
  1075. if (selected == NULL || is_excluded ||
  1076. cred_prio_cmp(selected, cred) < 0) {
  1077. selected = cred;
  1078. is_excluded = 0;
  1079. }
  1080. }
  1081. }
  1082. if (excluded)
  1083. *excluded = is_excluded;
  1084. return selected;
  1085. }
  1086. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  1087. struct wpa_cred *cred, int ttls)
  1088. {
  1089. if (cred->eap_method) {
  1090. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  1091. cred->eap_method->method == EAP_TYPE_TTLS;
  1092. os_free(ssid->eap.eap_methods);
  1093. ssid->eap.eap_methods =
  1094. os_malloc(sizeof(struct eap_method_type) * 2);
  1095. if (ssid->eap.eap_methods == NULL)
  1096. return -1;
  1097. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  1098. sizeof(*cred->eap_method));
  1099. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  1100. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  1101. }
  1102. if (ttls && cred->username && cred->username[0]) {
  1103. const char *pos;
  1104. char *anon;
  1105. /* Use anonymous NAI in Phase 1 */
  1106. pos = os_strchr(cred->username, '@');
  1107. if (pos) {
  1108. size_t buflen = 9 + os_strlen(pos) + 1;
  1109. anon = os_malloc(buflen);
  1110. if (anon == NULL)
  1111. return -1;
  1112. os_snprintf(anon, buflen, "anonymous%s", pos);
  1113. } else if (cred->realm) {
  1114. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  1115. anon = os_malloc(buflen);
  1116. if (anon == NULL)
  1117. return -1;
  1118. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  1119. } else {
  1120. anon = os_strdup("anonymous");
  1121. if (anon == NULL)
  1122. return -1;
  1123. }
  1124. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  1125. 0) {
  1126. os_free(anon);
  1127. return -1;
  1128. }
  1129. os_free(anon);
  1130. }
  1131. if (cred->username && cred->username[0] &&
  1132. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  1133. return -1;
  1134. if (cred->password && cred->password[0]) {
  1135. if (cred->ext_password &&
  1136. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  1137. return -1;
  1138. if (!cred->ext_password &&
  1139. wpa_config_set_quoted(ssid, "password", cred->password) <
  1140. 0)
  1141. return -1;
  1142. }
  1143. if (cred->client_cert && cred->client_cert[0] &&
  1144. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  1145. return -1;
  1146. #ifdef ANDROID
  1147. if (cred->private_key &&
  1148. os_strncmp(cred->private_key, "keystore://", 11) == 0) {
  1149. /* Use OpenSSL engine configuration for Android keystore */
  1150. if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
  1151. wpa_config_set_quoted(ssid, "key_id",
  1152. cred->private_key + 11) < 0 ||
  1153. wpa_config_set(ssid, "engine", "1", 0) < 0)
  1154. return -1;
  1155. } else
  1156. #endif /* ANDROID */
  1157. if (cred->private_key && cred->private_key[0] &&
  1158. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  1159. return -1;
  1160. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  1161. wpa_config_set_quoted(ssid, "private_key_passwd",
  1162. cred->private_key_passwd) < 0)
  1163. return -1;
  1164. if (cred->phase1) {
  1165. os_free(ssid->eap.phase1);
  1166. ssid->eap.phase1 = os_strdup(cred->phase1);
  1167. }
  1168. if (cred->phase2) {
  1169. os_free(ssid->eap.phase2);
  1170. ssid->eap.phase2 = os_strdup(cred->phase2);
  1171. }
  1172. if (cred->ca_cert && cred->ca_cert[0] &&
  1173. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  1174. return -1;
  1175. if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
  1176. wpa_config_set_quoted(ssid, "domain_suffix_match",
  1177. cred->domain_suffix_match) < 0)
  1178. return -1;
  1179. ssid->eap.ocsp = cred->ocsp;
  1180. return 0;
  1181. }
  1182. static int interworking_connect_roaming_consortium(
  1183. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  1184. struct wpa_bss *bss)
  1185. {
  1186. struct wpa_ssid *ssid;
  1187. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  1188. "roaming consortium match", MAC2STR(bss->bssid));
  1189. if (already_connected(wpa_s, cred, bss)) {
  1190. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1191. MAC2STR(bss->bssid));
  1192. return 0;
  1193. }
  1194. remove_duplicate_network(wpa_s, cred, bss);
  1195. ssid = wpa_config_add_network(wpa_s->conf);
  1196. if (ssid == NULL)
  1197. return -1;
  1198. ssid->parent_cred = cred;
  1199. wpas_notify_network_added(wpa_s, ssid);
  1200. wpa_config_set_network_defaults(ssid);
  1201. ssid->priority = cred->priority;
  1202. ssid->temporary = 1;
  1203. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  1204. if (ssid->ssid == NULL)
  1205. goto fail;
  1206. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  1207. ssid->ssid_len = bss->ssid_len;
  1208. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1209. goto fail;
  1210. if (cred->eap_method == NULL) {
  1211. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  1212. "credential using roaming consortium");
  1213. goto fail;
  1214. }
  1215. if (interworking_set_eap_params(
  1216. ssid, cred,
  1217. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  1218. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  1219. goto fail;
  1220. wpa_s->next_ssid = ssid;
  1221. wpa_config_update_prio_list(wpa_s->conf);
  1222. interworking_reconnect(wpa_s);
  1223. return 0;
  1224. fail:
  1225. wpas_notify_network_removed(wpa_s, ssid);
  1226. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1227. return -1;
  1228. }
  1229. static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
  1230. struct wpa_bss *bss, int allow_excluded)
  1231. {
  1232. struct wpa_cred *cred, *cred_rc, *cred_3gpp;
  1233. struct wpa_ssid *ssid;
  1234. struct nai_realm *realm;
  1235. struct nai_realm_eap *eap = NULL;
  1236. u16 count, i;
  1237. char buf[100];
  1238. int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
  1239. if (wpa_s->conf->cred == NULL || bss == NULL)
  1240. return -1;
  1241. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1242. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1243. wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
  1244. MACSTR, MAC2STR(bss->bssid));
  1245. return -1;
  1246. }
  1247. wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
  1248. " for connection (allow_excluded=%d)",
  1249. MAC2STR(bss->bssid), allow_excluded);
  1250. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1251. /*
  1252. * We currently support only HS 2.0 networks and those are
  1253. * required to use WPA2-Enterprise.
  1254. */
  1255. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  1256. "RSN");
  1257. return -1;
  1258. }
  1259. cred_rc = interworking_credentials_available_roaming_consortium(
  1260. wpa_s, bss, 0, excl);
  1261. if (cred_rc) {
  1262. wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
  1263. "consortium matching credential priority %d "
  1264. "sp_priority %d",
  1265. cred_rc->priority, cred_rc->sp_priority);
  1266. if (allow_excluded && excl && !(*excl))
  1267. excl = NULL;
  1268. }
  1269. cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
  1270. if (cred) {
  1271. wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
  1272. "matching credential priority %d sp_priority %d",
  1273. cred->priority, cred->sp_priority);
  1274. if (allow_excluded && excl && !(*excl))
  1275. excl = NULL;
  1276. }
  1277. cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
  1278. excl);
  1279. if (cred_3gpp) {
  1280. wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
  1281. "credential priority %d sp_priority %d",
  1282. cred_3gpp->priority, cred_3gpp->sp_priority);
  1283. if (allow_excluded && excl && !(*excl))
  1284. excl = NULL;
  1285. }
  1286. if (!cred_rc && !cred && !cred_3gpp) {
  1287. wpa_printf(MSG_DEBUG, "Interworking: No full credential matches - consider options without BW(etc.) limits");
  1288. cred_rc = interworking_credentials_available_roaming_consortium(
  1289. wpa_s, bss, 1, excl);
  1290. if (cred_rc) {
  1291. wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
  1292. "consortium matching credential priority %d "
  1293. "sp_priority %d (ignore BW)",
  1294. cred_rc->priority, cred_rc->sp_priority);
  1295. if (allow_excluded && excl && !(*excl))
  1296. excl = NULL;
  1297. }
  1298. cred = interworking_credentials_available_realm(wpa_s, bss, 1,
  1299. excl);
  1300. if (cred) {
  1301. wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm "
  1302. "list matching credential priority %d "
  1303. "sp_priority %d (ignore BW)",
  1304. cred->priority, cred->sp_priority);
  1305. if (allow_excluded && excl && !(*excl))
  1306. excl = NULL;
  1307. }
  1308. cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
  1309. 1, excl);
  1310. if (cred_3gpp) {
  1311. wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP "
  1312. "matching credential priority %d "
  1313. "sp_priority %d (ignore BW)",
  1314. cred_3gpp->priority, cred_3gpp->sp_priority);
  1315. if (allow_excluded && excl && !(*excl))
  1316. excl = NULL;
  1317. }
  1318. }
  1319. if (cred_rc &&
  1320. (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
  1321. (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
  1322. return interworking_connect_roaming_consortium(wpa_s, cred_rc,
  1323. bss);
  1324. if (cred_3gpp &&
  1325. (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
  1326. return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
  1327. }
  1328. if (cred == NULL) {
  1329. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1330. "found for " MACSTR, MAC2STR(bss->bssid));
  1331. return -1;
  1332. }
  1333. realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
  1334. &count);
  1335. if (realm == NULL) {
  1336. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1337. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1338. return -1;
  1339. }
  1340. for (i = 0; i < count; i++) {
  1341. if (!nai_realm_match(&realm[i], cred->realm))
  1342. continue;
  1343. eap = nai_realm_find_eap(cred, &realm[i]);
  1344. if (eap)
  1345. break;
  1346. }
  1347. if (!eap) {
  1348. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1349. "and EAP method found for " MACSTR,
  1350. MAC2STR(bss->bssid));
  1351. nai_realm_free(realm, count);
  1352. return -1;
  1353. }
  1354. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  1355. MAC2STR(bss->bssid));
  1356. if (already_connected(wpa_s, cred, bss)) {
  1357. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1358. MAC2STR(bss->bssid));
  1359. nai_realm_free(realm, count);
  1360. return 0;
  1361. }
  1362. remove_duplicate_network(wpa_s, cred, bss);
  1363. ssid = wpa_config_add_network(wpa_s->conf);
  1364. if (ssid == NULL) {
  1365. nai_realm_free(realm, count);
  1366. return -1;
  1367. }
  1368. ssid->parent_cred = cred;
  1369. wpas_notify_network_added(wpa_s, ssid);
  1370. wpa_config_set_network_defaults(ssid);
  1371. ssid->priority = cred->priority;
  1372. ssid->temporary = 1;
  1373. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  1374. if (ssid->ssid == NULL)
  1375. goto fail;
  1376. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  1377. ssid->ssid_len = bss->ssid_len;
  1378. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1379. goto fail;
  1380. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  1381. eap->method), 0) < 0)
  1382. goto fail;
  1383. switch (eap->method) {
  1384. case EAP_TYPE_TTLS:
  1385. if (eap->inner_method) {
  1386. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  1387. eap_get_name(EAP_VENDOR_IETF,
  1388. eap->inner_method));
  1389. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1390. goto fail;
  1391. break;
  1392. }
  1393. switch (eap->inner_non_eap) {
  1394. case NAI_REALM_INNER_NON_EAP_PAP:
  1395. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  1396. 0)
  1397. goto fail;
  1398. break;
  1399. case NAI_REALM_INNER_NON_EAP_CHAP:
  1400. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1401. < 0)
  1402. goto fail;
  1403. break;
  1404. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1405. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1406. 0) < 0)
  1407. goto fail;
  1408. break;
  1409. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1410. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1411. 0) < 0)
  1412. goto fail;
  1413. break;
  1414. default:
  1415. /* EAP params were not set - assume TTLS/MSCHAPv2 */
  1416. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1417. 0) < 0)
  1418. goto fail;
  1419. break;
  1420. }
  1421. break;
  1422. case EAP_TYPE_PEAP:
  1423. case EAP_TYPE_FAST:
  1424. if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
  1425. 0) < 0)
  1426. goto fail;
  1427. if (wpa_config_set(ssid, "pac_file",
  1428. "\"blob://pac_interworking\"", 0) < 0)
  1429. goto fail;
  1430. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1431. eap_get_name(EAP_VENDOR_IETF,
  1432. eap->inner_method ?
  1433. eap->inner_method :
  1434. EAP_TYPE_MSCHAPV2));
  1435. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1436. goto fail;
  1437. break;
  1438. case EAP_TYPE_TLS:
  1439. break;
  1440. }
  1441. if (interworking_set_eap_params(ssid, cred,
  1442. eap->method == EAP_TYPE_TTLS) < 0)
  1443. goto fail;
  1444. nai_realm_free(realm, count);
  1445. wpa_s->next_ssid = ssid;
  1446. wpa_config_update_prio_list(wpa_s->conf);
  1447. interworking_reconnect(wpa_s);
  1448. return 0;
  1449. fail:
  1450. wpas_notify_network_removed(wpa_s, ssid);
  1451. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1452. nai_realm_free(realm, count);
  1453. return -1;
  1454. }
  1455. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1456. {
  1457. return interworking_connect_helper(wpa_s, bss, 1);
  1458. }
  1459. static struct wpa_cred * interworking_credentials_available_3gpp(
  1460. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1461. int *excluded)
  1462. {
  1463. struct wpa_cred *selected = NULL;
  1464. #ifdef INTERWORKING_3GPP
  1465. struct wpa_cred *cred;
  1466. int ret;
  1467. int is_excluded = 0;
  1468. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  1469. return NULL;
  1470. #ifdef CONFIG_EAP_PROXY
  1471. if (!wpa_s->imsi[0]) {
  1472. size_t len;
  1473. wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
  1474. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
  1475. wpa_s->imsi,
  1476. &len);
  1477. if (wpa_s->mnc_len > 0) {
  1478. wpa_s->imsi[len] = '\0';
  1479. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  1480. wpa_s->imsi, wpa_s->mnc_len);
  1481. } else {
  1482. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  1483. }
  1484. }
  1485. #endif /* CONFIG_EAP_PROXY */
  1486. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1487. char *sep;
  1488. const char *imsi;
  1489. int mnc_len;
  1490. char imsi_buf[16];
  1491. size_t msin_len;
  1492. #ifdef PCSC_FUNCS
  1493. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1494. wpa_s->imsi[0]) {
  1495. imsi = wpa_s->imsi;
  1496. mnc_len = wpa_s->mnc_len;
  1497. goto compare;
  1498. }
  1499. #endif /* PCSC_FUNCS */
  1500. #ifdef CONFIG_EAP_PROXY
  1501. if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1502. imsi = wpa_s->imsi;
  1503. mnc_len = wpa_s->mnc_len;
  1504. goto compare;
  1505. }
  1506. #endif /* CONFIG_EAP_PROXY */
  1507. if (cred->imsi == NULL || !cred->imsi[0] ||
  1508. (!wpa_s->conf->external_sim &&
  1509. (cred->milenage == NULL || !cred->milenage[0])))
  1510. continue;
  1511. sep = os_strchr(cred->imsi, '-');
  1512. if (sep == NULL ||
  1513. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1514. continue;
  1515. mnc_len = sep - cred->imsi - 3;
  1516. os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
  1517. sep++;
  1518. msin_len = os_strlen(cred->imsi);
  1519. if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
  1520. msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
  1521. os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
  1522. imsi_buf[3 + mnc_len + msin_len] = '\0';
  1523. imsi = imsi_buf;
  1524. #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
  1525. compare:
  1526. #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
  1527. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1528. MACSTR, MAC2STR(bss->bssid));
  1529. ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
  1530. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1531. if (ret) {
  1532. if (cred_no_required_oi_match(cred, bss))
  1533. continue;
  1534. if (!ignore_bw &&
  1535. cred_below_min_backhaul(wpa_s, cred, bss))
  1536. continue;
  1537. if (!ignore_bw &&
  1538. cred_over_max_bss_load(wpa_s, cred, bss))
  1539. continue;
  1540. if (!ignore_bw &&
  1541. cred_conn_capab_missing(wpa_s, cred, bss))
  1542. continue;
  1543. if (cred_excluded_ssid(cred, bss)) {
  1544. if (excluded == NULL)
  1545. continue;
  1546. if (selected == NULL) {
  1547. selected = cred;
  1548. is_excluded = 1;
  1549. }
  1550. } else {
  1551. if (selected == NULL || is_excluded ||
  1552. cred_prio_cmp(selected, cred) < 0) {
  1553. selected = cred;
  1554. is_excluded = 0;
  1555. }
  1556. }
  1557. }
  1558. }
  1559. if (excluded)
  1560. *excluded = is_excluded;
  1561. #endif /* INTERWORKING_3GPP */
  1562. return selected;
  1563. }
  1564. static struct wpa_cred * interworking_credentials_available_realm(
  1565. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1566. int *excluded)
  1567. {
  1568. struct wpa_cred *cred, *selected = NULL;
  1569. struct nai_realm *realm;
  1570. u16 count, i;
  1571. int is_excluded = 0;
  1572. if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
  1573. return NULL;
  1574. if (wpa_s->conf->cred == NULL)
  1575. return NULL;
  1576. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1577. MACSTR, MAC2STR(bss->bssid));
  1578. realm = nai_realm_parse(bss->anqp->nai_realm, &count);
  1579. if (realm == NULL) {
  1580. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1581. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1582. return NULL;
  1583. }
  1584. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1585. if (cred->realm == NULL)
  1586. continue;
  1587. for (i = 0; i < count; i++) {
  1588. if (!nai_realm_match(&realm[i], cred->realm))
  1589. continue;
  1590. if (nai_realm_find_eap(cred, &realm[i])) {
  1591. if (cred_no_required_oi_match(cred, bss))
  1592. continue;
  1593. if (!ignore_bw &&
  1594. cred_below_min_backhaul(wpa_s, cred, bss))
  1595. continue;
  1596. if (!ignore_bw &&
  1597. cred_over_max_bss_load(wpa_s, cred, bss))
  1598. continue;
  1599. if (!ignore_bw &&
  1600. cred_conn_capab_missing(wpa_s, cred, bss))
  1601. continue;
  1602. if (cred_excluded_ssid(cred, bss)) {
  1603. if (excluded == NULL)
  1604. continue;
  1605. if (selected == NULL) {
  1606. selected = cred;
  1607. is_excluded = 1;
  1608. }
  1609. } else {
  1610. if (selected == NULL || is_excluded ||
  1611. cred_prio_cmp(selected, cred) < 0)
  1612. {
  1613. selected = cred;
  1614. is_excluded = 0;
  1615. }
  1616. }
  1617. break;
  1618. }
  1619. }
  1620. }
  1621. nai_realm_free(realm, count);
  1622. if (excluded)
  1623. *excluded = is_excluded;
  1624. return selected;
  1625. }
  1626. static struct wpa_cred * interworking_credentials_available_helper(
  1627. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1628. int *excluded)
  1629. {
  1630. struct wpa_cred *cred, *cred2;
  1631. int excluded1, excluded2;
  1632. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1633. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1634. wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
  1635. MACSTR, MAC2STR(bss->bssid));
  1636. return NULL;
  1637. }
  1638. cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
  1639. &excluded1);
  1640. cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
  1641. &excluded2);
  1642. if (cred && cred2 &&
  1643. (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
  1644. cred = cred2;
  1645. excluded1 = excluded2;
  1646. }
  1647. if (!cred) {
  1648. cred = cred2;
  1649. excluded1 = excluded2;
  1650. }
  1651. cred2 = interworking_credentials_available_roaming_consortium(
  1652. wpa_s, bss, ignore_bw, &excluded2);
  1653. if (cred && cred2 &&
  1654. (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
  1655. cred = cred2;
  1656. excluded1 = excluded2;
  1657. }
  1658. if (!cred) {
  1659. cred = cred2;
  1660. excluded1 = excluded2;
  1661. }
  1662. if (excluded)
  1663. *excluded = excluded1;
  1664. return cred;
  1665. }
  1666. static struct wpa_cred * interworking_credentials_available(
  1667. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
  1668. {
  1669. struct wpa_cred *cred;
  1670. if (excluded)
  1671. *excluded = 0;
  1672. cred = interworking_credentials_available_helper(wpa_s, bss, 0,
  1673. excluded);
  1674. if (cred)
  1675. return cred;
  1676. return interworking_credentials_available_helper(wpa_s, bss, 1,
  1677. excluded);
  1678. }
  1679. int domain_name_list_contains(struct wpabuf *domain_names,
  1680. const char *domain, int exact_match)
  1681. {
  1682. const u8 *pos, *end;
  1683. size_t len;
  1684. len = os_strlen(domain);
  1685. pos = wpabuf_head(domain_names);
  1686. end = pos + wpabuf_len(domain_names);
  1687. while (pos + 1 < end) {
  1688. if (pos + 1 + pos[0] > end)
  1689. break;
  1690. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1691. pos + 1, pos[0]);
  1692. if (pos[0] == len &&
  1693. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1694. return 1;
  1695. if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
  1696. const char *ap = (const char *) (pos + 1);
  1697. int offset = pos[0] - len;
  1698. if (os_strncasecmp(domain, ap + offset, len) == 0)
  1699. return 1;
  1700. }
  1701. pos += 1 + pos[0];
  1702. }
  1703. return 0;
  1704. }
  1705. int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
  1706. struct wpa_cred *cred,
  1707. struct wpabuf *domain_names)
  1708. {
  1709. size_t i;
  1710. int ret = -1;
  1711. #ifdef INTERWORKING_3GPP
  1712. char nai[100], *realm;
  1713. char *imsi = NULL;
  1714. int mnc_len = 0;
  1715. if (cred->imsi)
  1716. imsi = cred->imsi;
  1717. #ifdef CONFIG_PCSC
  1718. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1719. wpa_s->scard && wpa_s->imsi[0]) {
  1720. imsi = wpa_s->imsi;
  1721. mnc_len = wpa_s->mnc_len;
  1722. }
  1723. #endif /* CONFIG_PCSC */
  1724. #ifdef CONFIG_EAP_PROXY
  1725. else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1726. imsi = wpa_s->imsi;
  1727. mnc_len = wpa_s->mnc_len;
  1728. }
  1729. #endif /* CONFIG_EAP_PROXY */
  1730. if (domain_names &&
  1731. imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
  1732. realm = os_strchr(nai, '@');
  1733. if (realm)
  1734. realm++;
  1735. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1736. "with SIM/USIM domain %s", realm);
  1737. if (realm &&
  1738. domain_name_list_contains(domain_names, realm, 1))
  1739. return 1;
  1740. if (realm)
  1741. ret = 0;
  1742. }
  1743. #endif /* INTERWORKING_3GPP */
  1744. if (domain_names == NULL || cred->domain == NULL)
  1745. return ret;
  1746. for (i = 0; i < cred->num_domain; i++) {
  1747. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1748. "home SP FQDN %s", cred->domain[i]);
  1749. if (domain_name_list_contains(domain_names, cred->domain[i], 1))
  1750. return 1;
  1751. }
  1752. return 0;
  1753. }
  1754. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1755. struct wpabuf *domain_names)
  1756. {
  1757. struct wpa_cred *cred;
  1758. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1759. return -1;
  1760. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1761. int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
  1762. if (res)
  1763. return res;
  1764. }
  1765. return 0;
  1766. }
  1767. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1768. {
  1769. struct wpa_bss *bss;
  1770. struct wpa_ssid *ssid;
  1771. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1772. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1773. if (wpas_network_disabled(wpa_s, ssid) ||
  1774. ssid->mode != WPAS_MODE_INFRA)
  1775. continue;
  1776. if (ssid->ssid_len != bss->ssid_len ||
  1777. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1778. 0)
  1779. continue;
  1780. /*
  1781. * TODO: Consider more accurate matching of security
  1782. * configuration similarly to what is done in events.c
  1783. */
  1784. return 1;
  1785. }
  1786. }
  1787. return 0;
  1788. }
  1789. static int roaming_partner_match(struct wpa_supplicant *wpa_s,
  1790. struct roaming_partner *partner,
  1791. struct wpabuf *domain_names)
  1792. {
  1793. wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
  1794. partner->fqdn, partner->exact_match, partner->priority,
  1795. partner->country);
  1796. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
  1797. wpabuf_head(domain_names),
  1798. wpabuf_len(domain_names));
  1799. if (!domain_name_list_contains(domain_names, partner->fqdn,
  1800. partner->exact_match))
  1801. return 0;
  1802. /* TODO: match Country */
  1803. return 1;
  1804. }
  1805. static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  1806. struct wpa_bss *bss)
  1807. {
  1808. size_t i;
  1809. if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
  1810. wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
  1811. return 128; /* cannot check preference with domain name */
  1812. }
  1813. if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
  1814. {
  1815. wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
  1816. return 0; /* max preference for home SP network */
  1817. }
  1818. for (i = 0; i < cred->num_roaming_partner; i++) {
  1819. if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
  1820. bss->anqp->domain_name)) {
  1821. wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
  1822. cred->roaming_partner[i].priority);
  1823. return cred->roaming_partner[i].priority;
  1824. }
  1825. }
  1826. wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
  1827. return 128;
  1828. }
  1829. static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
  1830. struct wpa_bss *selected,
  1831. struct wpa_cred *cred)
  1832. {
  1833. struct wpa_bss *bss;
  1834. u8 best_prio, prio;
  1835. struct wpa_cred *cred2;
  1836. /*
  1837. * Check if any other BSS is operated by a more preferred roaming
  1838. * partner.
  1839. */
  1840. best_prio = roaming_prio(wpa_s, cred, selected);
  1841. wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
  1842. MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
  1843. cred->id);
  1844. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1845. if (bss == selected)
  1846. continue;
  1847. cred2 = interworking_credentials_available(wpa_s, bss, NULL);
  1848. if (!cred2)
  1849. continue;
  1850. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
  1851. continue;
  1852. prio = roaming_prio(wpa_s, cred2, bss);
  1853. wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
  1854. MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
  1855. cred2->id);
  1856. if (prio < best_prio) {
  1857. int bh1, bh2, load1, load2, conn1, conn2;
  1858. bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
  1859. load1 = cred_over_max_bss_load(wpa_s, cred, selected);
  1860. conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
  1861. bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
  1862. load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
  1863. conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
  1864. wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
  1865. bh1, load1, conn1, bh2, load2, conn2);
  1866. if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
  1867. wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
  1868. best_prio = prio;
  1869. selected = bss;
  1870. }
  1871. }
  1872. }
  1873. return selected;
  1874. }
  1875. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1876. {
  1877. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1878. struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
  1879. unsigned int count = 0;
  1880. const char *type;
  1881. int res;
  1882. struct wpa_cred *cred, *selected_cred = NULL;
  1883. struct wpa_cred *selected_home_cred = NULL;
  1884. struct wpa_cred *selected2_cred = NULL;
  1885. struct wpa_cred *selected2_home_cred = NULL;
  1886. wpa_s->network_select = 0;
  1887. wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
  1888. wpa_s->auto_select);
  1889. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1890. int excluded = 0;
  1891. int bh, bss_load, conn_capab;
  1892. cred = interworking_credentials_available(wpa_s, bss,
  1893. &excluded);
  1894. if (!cred)
  1895. continue;
  1896. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1897. /*
  1898. * We currently support only HS 2.0 networks and those
  1899. * are required to use WPA2-Enterprise.
  1900. */
  1901. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1902. "with " MACSTR " but network does not use "
  1903. "RSN", MAC2STR(bss->bssid));
  1904. continue;
  1905. }
  1906. if (!excluded)
  1907. count++;
  1908. res = interworking_home_sp(wpa_s, bss->anqp ?
  1909. bss->anqp->domain_name : NULL);
  1910. if (res > 0)
  1911. type = "home";
  1912. else if (res == 0)
  1913. type = "roaming";
  1914. else
  1915. type = "unknown";
  1916. bh = cred_below_min_backhaul(wpa_s, cred, bss);
  1917. bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
  1918. conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
  1919. wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
  1920. excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
  1921. MAC2STR(bss->bssid), type,
  1922. bh ? " below_min_backhaul=1" : "",
  1923. bss_load ? " over_max_bss_load=1" : "",
  1924. conn_capab ? " conn_capab_missing=1" : "",
  1925. cred->id, cred->priority, cred->sp_priority);
  1926. if (excluded)
  1927. continue;
  1928. if (wpa_s->auto_select ||
  1929. (wpa_s->conf->auto_interworking &&
  1930. wpa_s->auto_network_select)) {
  1931. if (bh || bss_load || conn_capab) {
  1932. if (selected2_cred == NULL ||
  1933. cred_prio_cmp(cred, selected2_cred) > 0) {
  1934. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
  1935. selected2 = bss;
  1936. selected2_cred = cred;
  1937. }
  1938. if (res > 0 &&
  1939. (selected2_home_cred == NULL ||
  1940. cred_prio_cmp(cred, selected2_home_cred) >
  1941. 0)) {
  1942. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
  1943. selected2_home = bss;
  1944. selected2_home_cred = cred;
  1945. }
  1946. } else {
  1947. if (selected_cred == NULL ||
  1948. cred_prio_cmp(cred, selected_cred) > 0) {
  1949. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
  1950. selected = bss;
  1951. selected_cred = cred;
  1952. }
  1953. if (res > 0 &&
  1954. (selected_home_cred == NULL ||
  1955. cred_prio_cmp(cred, selected_home_cred) >
  1956. 0)) {
  1957. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
  1958. selected_home = bss;
  1959. selected_home_cred = cred;
  1960. }
  1961. }
  1962. }
  1963. }
  1964. if (selected_home && selected_home != selected &&
  1965. selected_home_cred &&
  1966. (selected_cred == NULL ||
  1967. cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
  1968. /* Prefer network operated by the Home SP */
  1969. wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
  1970. selected = selected_home;
  1971. selected_cred = selected_home_cred;
  1972. }
  1973. if (!selected) {
  1974. if (selected2_home) {
  1975. wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
  1976. selected = selected2_home;
  1977. selected_cred = selected2_home_cred;
  1978. } else if (selected2) {
  1979. wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
  1980. selected = selected2;
  1981. selected_cred = selected2_cred;
  1982. }
  1983. }
  1984. if (count == 0) {
  1985. /*
  1986. * No matching network was found based on configured
  1987. * credentials. Check whether any of the enabled network blocks
  1988. * have matching APs.
  1989. */
  1990. if (interworking_find_network_match(wpa_s)) {
  1991. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1992. "match for enabled network configurations");
  1993. if (wpa_s->auto_select)
  1994. interworking_reconnect(wpa_s);
  1995. return;
  1996. }
  1997. if (wpa_s->auto_network_select) {
  1998. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  1999. "scanning after ANQP fetch");
  2000. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  2001. 0);
  2002. return;
  2003. }
  2004. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  2005. "with matching credentials found");
  2006. }
  2007. if (selected) {
  2008. wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
  2009. MAC2STR(selected->bssid));
  2010. selected = pick_best_roaming_partner(wpa_s, selected,
  2011. selected_cred);
  2012. wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
  2013. " (after best roaming partner selection)",
  2014. MAC2STR(selected->bssid));
  2015. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
  2016. MAC2STR(selected->bssid));
  2017. interworking_connect(wpa_s, selected);
  2018. }
  2019. }
  2020. static struct wpa_bss_anqp *
  2021. interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  2022. {
  2023. struct wpa_bss *other;
  2024. if (is_zero_ether_addr(bss->hessid))
  2025. return NULL; /* Cannot be in the same homegenous ESS */
  2026. dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
  2027. if (other == bss)
  2028. continue;
  2029. if (other->anqp == NULL)
  2030. continue;
  2031. if (other->anqp->roaming_consortium == NULL &&
  2032. other->anqp->nai_realm == NULL &&
  2033. other->anqp->anqp_3gpp == NULL &&
  2034. other->anqp->domain_name == NULL)
  2035. continue;
  2036. if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
  2037. continue;
  2038. if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
  2039. continue;
  2040. if (bss->ssid_len != other->ssid_len ||
  2041. os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
  2042. continue;
  2043. wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
  2044. "already fetched BSSID " MACSTR " and " MACSTR,
  2045. MAC2STR(other->bssid), MAC2STR(bss->bssid));
  2046. other->anqp->users++;
  2047. return other->anqp;
  2048. }
  2049. return NULL;
  2050. }
  2051. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  2052. {
  2053. struct wpa_bss *bss;
  2054. int found = 0;
  2055. const u8 *ie;
  2056. wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
  2057. "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
  2058. wpa_s->fetch_anqp_in_progress,
  2059. wpa_s->fetch_osu_icon_in_progress);
  2060. if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
  2061. wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
  2062. return;
  2063. }
  2064. if (wpa_s->fetch_osu_icon_in_progress) {
  2065. wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
  2066. hs20_next_osu_icon(wpa_s);
  2067. return;
  2068. }
  2069. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2070. if (!(bss->caps & IEEE80211_CAP_ESS))
  2071. continue;
  2072. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  2073. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  2074. continue; /* AP does not support Interworking */
  2075. if (disallowed_bssid(wpa_s, bss->bssid) ||
  2076. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
  2077. continue; /* Disallowed BSS */
  2078. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  2079. if (bss->anqp == NULL) {
  2080. bss->anqp = interworking_match_anqp_info(wpa_s,
  2081. bss);
  2082. if (bss->anqp) {
  2083. /* Shared data already fetched */
  2084. continue;
  2085. }
  2086. bss->anqp = wpa_bss_anqp_alloc();
  2087. if (bss->anqp == NULL)
  2088. break;
  2089. }
  2090. found++;
  2091. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  2092. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  2093. MACSTR, MAC2STR(bss->bssid));
  2094. interworking_anqp_send_req(wpa_s, bss);
  2095. break;
  2096. }
  2097. }
  2098. if (found == 0) {
  2099. if (wpa_s->fetch_osu_info) {
  2100. wpa_printf(MSG_DEBUG, "Interworking: Next icon");
  2101. hs20_osu_icon_fetch(wpa_s);
  2102. return;
  2103. }
  2104. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  2105. wpa_s->fetch_anqp_in_progress = 0;
  2106. if (wpa_s->network_select)
  2107. interworking_select_network(wpa_s);
  2108. }
  2109. }
  2110. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  2111. {
  2112. struct wpa_bss *bss;
  2113. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  2114. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  2115. wpa_s->fetch_anqp_in_progress = 1;
  2116. interworking_next_anqp_fetch(wpa_s);
  2117. }
  2118. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  2119. {
  2120. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  2121. return 0;
  2122. wpa_s->network_select = 0;
  2123. wpa_s->fetch_all_anqp = 1;
  2124. wpa_s->fetch_osu_info = 0;
  2125. interworking_start_fetch_anqp(wpa_s);
  2126. return 0;
  2127. }
  2128. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  2129. {
  2130. if (!wpa_s->fetch_anqp_in_progress)
  2131. return;
  2132. wpa_s->fetch_anqp_in_progress = 0;
  2133. }
  2134. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  2135. u16 info_ids[], size_t num_ids)
  2136. {
  2137. struct wpabuf *buf;
  2138. int ret = 0;
  2139. int freq;
  2140. struct wpa_bss *bss;
  2141. int res;
  2142. freq = wpa_s->assoc_freq;
  2143. bss = wpa_bss_get_bssid(wpa_s, dst);
  2144. if (bss) {
  2145. wpa_bss_anqp_unshare_alloc(bss);
  2146. freq = bss->freq;
  2147. }
  2148. if (freq <= 0)
  2149. return -1;
  2150. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  2151. MAC2STR(dst), (unsigned int) num_ids);
  2152. buf = anqp_build_req(info_ids, num_ids, NULL);
  2153. if (buf == NULL)
  2154. return -1;
  2155. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  2156. if (res < 0) {
  2157. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  2158. wpabuf_free(buf);
  2159. ret = -1;
  2160. } else
  2161. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  2162. "%u", res);
  2163. return ret;
  2164. }
  2165. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  2166. struct wpa_bss *bss, const u8 *sa,
  2167. u16 info_id,
  2168. const u8 *data, size_t slen)
  2169. {
  2170. const u8 *pos = data;
  2171. struct wpa_bss_anqp *anqp = NULL;
  2172. #ifdef CONFIG_HS20
  2173. u8 type;
  2174. #endif /* CONFIG_HS20 */
  2175. if (bss)
  2176. anqp = bss->anqp;
  2177. switch (info_id) {
  2178. case ANQP_CAPABILITY_LIST:
  2179. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2180. " ANQP Capability list", MAC2STR(sa));
  2181. break;
  2182. case ANQP_VENUE_NAME:
  2183. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2184. " Venue Name", MAC2STR(sa));
  2185. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  2186. if (anqp) {
  2187. wpabuf_free(anqp->venue_name);
  2188. anqp->venue_name = wpabuf_alloc_copy(pos, slen);
  2189. }
  2190. break;
  2191. case ANQP_NETWORK_AUTH_TYPE:
  2192. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2193. " Network Authentication Type information",
  2194. MAC2STR(sa));
  2195. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  2196. "Type", pos, slen);
  2197. if (anqp) {
  2198. wpabuf_free(anqp->network_auth_type);
  2199. anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
  2200. }
  2201. break;
  2202. case ANQP_ROAMING_CONSORTIUM:
  2203. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2204. " Roaming Consortium list", MAC2STR(sa));
  2205. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  2206. pos, slen);
  2207. if (anqp) {
  2208. wpabuf_free(anqp->roaming_consortium);
  2209. anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
  2210. }
  2211. break;
  2212. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  2213. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2214. " IP Address Type Availability information",
  2215. MAC2STR(sa));
  2216. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  2217. pos, slen);
  2218. if (anqp) {
  2219. wpabuf_free(anqp->ip_addr_type_availability);
  2220. anqp->ip_addr_type_availability =
  2221. wpabuf_alloc_copy(pos, slen);
  2222. }
  2223. break;
  2224. case ANQP_NAI_REALM:
  2225. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2226. " NAI Realm list", MAC2STR(sa));
  2227. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  2228. if (anqp) {
  2229. wpabuf_free(anqp->nai_realm);
  2230. anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
  2231. }
  2232. break;
  2233. case ANQP_3GPP_CELLULAR_NETWORK:
  2234. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2235. " 3GPP Cellular Network information", MAC2STR(sa));
  2236. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  2237. pos, slen);
  2238. if (anqp) {
  2239. wpabuf_free(anqp->anqp_3gpp);
  2240. anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  2241. }
  2242. break;
  2243. case ANQP_DOMAIN_NAME:
  2244. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2245. " Domain Name list", MAC2STR(sa));
  2246. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  2247. if (anqp) {
  2248. wpabuf_free(anqp->domain_name);
  2249. anqp->domain_name = wpabuf_alloc_copy(pos, slen);
  2250. }
  2251. break;
  2252. case ANQP_VENDOR_SPECIFIC:
  2253. if (slen < 3)
  2254. return;
  2255. switch (WPA_GET_BE24(pos)) {
  2256. #ifdef CONFIG_HS20
  2257. case OUI_WFA:
  2258. pos += 3;
  2259. slen -= 3;
  2260. if (slen < 1)
  2261. return;
  2262. type = *pos++;
  2263. slen--;
  2264. switch (type) {
  2265. case HS20_ANQP_OUI_TYPE:
  2266. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  2267. slen);
  2268. break;
  2269. default:
  2270. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  2271. "vendor type %u", type);
  2272. break;
  2273. }
  2274. break;
  2275. #endif /* CONFIG_HS20 */
  2276. default:
  2277. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  2278. "vendor-specific ANQP OUI %06x",
  2279. WPA_GET_BE24(pos));
  2280. return;
  2281. }
  2282. break;
  2283. default:
  2284. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  2285. "%u", info_id);
  2286. break;
  2287. }
  2288. }
  2289. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  2290. enum gas_query_result result,
  2291. const struct wpabuf *adv_proto,
  2292. const struct wpabuf *resp, u16 status_code)
  2293. {
  2294. struct wpa_supplicant *wpa_s = ctx;
  2295. const u8 *pos;
  2296. const u8 *end;
  2297. u16 info_id;
  2298. u16 slen;
  2299. struct wpa_bss *bss = NULL, *tmp;
  2300. wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
  2301. " dialog_token=%u result=%d status_code=%u",
  2302. MAC2STR(dst), dialog_token, result, status_code);
  2303. if (result != GAS_QUERY_SUCCESS) {
  2304. if (wpa_s->fetch_osu_icon_in_progress)
  2305. hs20_icon_fetch_failed(wpa_s);
  2306. return;
  2307. }
  2308. pos = wpabuf_head(adv_proto);
  2309. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  2310. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  2311. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  2312. "Protocol in response");
  2313. if (wpa_s->fetch_osu_icon_in_progress)
  2314. hs20_icon_fetch_failed(wpa_s);
  2315. return;
  2316. }
  2317. /*
  2318. * If possible, select the BSS entry based on which BSS entry was used
  2319. * for the request. This can help in cases where multiple BSS entries
  2320. * may exist for the same AP.
  2321. */
  2322. dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
  2323. if (tmp == wpa_s->interworking_gas_bss &&
  2324. os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
  2325. bss = tmp;
  2326. break;
  2327. }
  2328. }
  2329. if (bss == NULL)
  2330. bss = wpa_bss_get_bssid(wpa_s, dst);
  2331. pos = wpabuf_head(resp);
  2332. end = pos + wpabuf_len(resp);
  2333. while (pos < end) {
  2334. if (pos + 4 > end) {
  2335. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  2336. break;
  2337. }
  2338. info_id = WPA_GET_LE16(pos);
  2339. pos += 2;
  2340. slen = WPA_GET_LE16(pos);
  2341. pos += 2;
  2342. if (pos + slen > end) {
  2343. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  2344. "for Info ID %u", info_id);
  2345. break;
  2346. }
  2347. interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
  2348. slen);
  2349. pos += slen;
  2350. }
  2351. hs20_notify_parse_done(wpa_s);
  2352. }
  2353. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  2354. struct wpa_scan_results *scan_res)
  2355. {
  2356. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  2357. "ANQP fetch");
  2358. interworking_start_fetch_anqp(wpa_s);
  2359. }
  2360. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
  2361. int *freqs)
  2362. {
  2363. interworking_stop_fetch_anqp(wpa_s);
  2364. wpa_s->network_select = 1;
  2365. wpa_s->auto_network_select = 0;
  2366. wpa_s->auto_select = !!auto_select;
  2367. wpa_s->fetch_all_anqp = 0;
  2368. wpa_s->fetch_osu_info = 0;
  2369. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  2370. "selection");
  2371. wpa_s->scan_res_handler = interworking_scan_res_handler;
  2372. wpa_s->normal_scans = 0;
  2373. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2374. os_free(wpa_s->manual_scan_freqs);
  2375. wpa_s->manual_scan_freqs = freqs;
  2376. wpa_s->after_wps = 0;
  2377. wpa_s->known_wps_freq = 0;
  2378. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2379. return 0;
  2380. }
  2381. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  2382. enum gas_query_result result,
  2383. const struct wpabuf *adv_proto,
  2384. const struct wpabuf *resp, u16 status_code)
  2385. {
  2386. struct wpa_supplicant *wpa_s = ctx;
  2387. struct wpabuf *n;
  2388. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  2389. " dialog_token=%d status_code=%d resp_len=%d",
  2390. MAC2STR(addr), dialog_token, status_code,
  2391. resp ? (int) wpabuf_len(resp) : -1);
  2392. if (!resp)
  2393. return;
  2394. n = wpabuf_dup(resp);
  2395. if (n == NULL)
  2396. return;
  2397. wpabuf_free(wpa_s->prev_gas_resp);
  2398. wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
  2399. os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
  2400. wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
  2401. wpa_s->last_gas_resp = n;
  2402. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  2403. wpa_s->last_gas_dialog_token = dialog_token;
  2404. }
  2405. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  2406. const struct wpabuf *adv_proto,
  2407. const struct wpabuf *query)
  2408. {
  2409. struct wpabuf *buf;
  2410. int ret = 0;
  2411. int freq;
  2412. struct wpa_bss *bss;
  2413. int res;
  2414. size_t len;
  2415. u8 query_resp_len_limit = 0, pame_bi = 0;
  2416. freq = wpa_s->assoc_freq;
  2417. bss = wpa_bss_get_bssid(wpa_s, dst);
  2418. if (bss)
  2419. freq = bss->freq;
  2420. if (freq <= 0)
  2421. return -1;
  2422. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  2423. MAC2STR(dst), freq);
  2424. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  2425. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  2426. len = 3 + wpabuf_len(adv_proto) + 2;
  2427. if (query)
  2428. len += wpabuf_len(query);
  2429. buf = gas_build_initial_req(0, len);
  2430. if (buf == NULL)
  2431. return -1;
  2432. /* Advertisement Protocol IE */
  2433. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  2434. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  2435. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  2436. (pame_bi ? 0x80 : 0));
  2437. wpabuf_put_buf(buf, adv_proto);
  2438. /* GAS Query */
  2439. if (query) {
  2440. wpabuf_put_le16(buf, wpabuf_len(query));
  2441. wpabuf_put_buf(buf, query);
  2442. } else
  2443. wpabuf_put_le16(buf, 0);
  2444. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  2445. if (res < 0) {
  2446. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  2447. wpabuf_free(buf);
  2448. ret = -1;
  2449. } else
  2450. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  2451. "%u", res);
  2452. return ret;
  2453. }