interworking.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  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, *sel_ssid;
  687. struct wpa_bss *selected;
  688. if (wpa_s->wpa_state < WPA_ASSOCIATED || wpa_s->current_ssid == NULL)
  689. return 0;
  690. ssid = wpa_s->current_ssid;
  691. if (ssid->parent_cred != cred)
  692. return 0;
  693. if (ssid->ssid_len != bss->ssid_len ||
  694. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  695. return 0;
  696. sel_ssid = NULL;
  697. selected = wpa_supplicant_pick_network(wpa_s, &sel_ssid);
  698. if (selected && sel_ssid && sel_ssid->priority > ssid->priority)
  699. return 0; /* higher priority network in scan results */
  700. return 1;
  701. }
  702. static void remove_duplicate_network(struct wpa_supplicant *wpa_s,
  703. struct wpa_cred *cred,
  704. struct wpa_bss *bss)
  705. {
  706. struct wpa_ssid *ssid;
  707. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  708. if (ssid->parent_cred != cred)
  709. continue;
  710. if (ssid->ssid_len != bss->ssid_len ||
  711. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) != 0)
  712. continue;
  713. break;
  714. }
  715. if (ssid == NULL)
  716. return;
  717. wpa_printf(MSG_DEBUG, "Interworking: Remove duplicate network entry for the same credential");
  718. if (ssid == wpa_s->current_ssid) {
  719. wpa_sm_set_config(wpa_s->wpa, NULL);
  720. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  721. wpa_supplicant_deauthenticate(wpa_s,
  722. WLAN_REASON_DEAUTH_LEAVING);
  723. }
  724. wpas_notify_network_removed(wpa_s, ssid);
  725. wpa_config_remove_network(wpa_s->conf, ssid->id);
  726. }
  727. static int interworking_set_hs20_params(struct wpa_supplicant *wpa_s,
  728. struct wpa_ssid *ssid)
  729. {
  730. if (wpa_config_set(ssid, "key_mgmt",
  731. wpa_s->conf->pmf != NO_MGMT_FRAME_PROTECTION ?
  732. "WPA-EAP WPA-EAP-SHA256" : "WPA-EAP", 0) < 0)
  733. return -1;
  734. if (wpa_config_set(ssid, "proto", "RSN", 0) < 0)
  735. return -1;
  736. if (wpa_config_set(ssid, "pairwise", "CCMP", 0) < 0)
  737. return -1;
  738. return 0;
  739. }
  740. static int interworking_connect_3gpp(struct wpa_supplicant *wpa_s,
  741. struct wpa_cred *cred,
  742. struct wpa_bss *bss)
  743. {
  744. #ifdef INTERWORKING_3GPP
  745. struct wpa_ssid *ssid;
  746. int eap_type;
  747. int res;
  748. char prefix;
  749. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  750. return -1;
  751. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " (3GPP)",
  752. MAC2STR(bss->bssid));
  753. if (already_connected(wpa_s, cred, bss)) {
  754. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  755. MAC2STR(bss->bssid));
  756. return 0;
  757. }
  758. remove_duplicate_network(wpa_s, cred, bss);
  759. ssid = wpa_config_add_network(wpa_s->conf);
  760. if (ssid == NULL)
  761. return -1;
  762. ssid->parent_cred = cred;
  763. wpas_notify_network_added(wpa_s, ssid);
  764. wpa_config_set_network_defaults(ssid);
  765. ssid->priority = cred->priority;
  766. ssid->temporary = 1;
  767. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  768. if (ssid->ssid == NULL)
  769. goto fail;
  770. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  771. ssid->ssid_len = bss->ssid_len;
  772. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  773. goto fail;
  774. eap_type = EAP_TYPE_SIM;
  775. if (cred->pcsc && wpa_s->scard && scard_supports_umts(wpa_s->scard))
  776. eap_type = EAP_TYPE_AKA;
  777. if (cred->eap_method && cred->eap_method[0].vendor == EAP_VENDOR_IETF) {
  778. if (cred->eap_method[0].method == EAP_TYPE_SIM ||
  779. cred->eap_method[0].method == EAP_TYPE_AKA ||
  780. cred->eap_method[0].method == EAP_TYPE_AKA_PRIME)
  781. eap_type = cred->eap_method[0].method;
  782. }
  783. switch (eap_type) {
  784. case EAP_TYPE_SIM:
  785. prefix = '1';
  786. res = wpa_config_set(ssid, "eap", "SIM", 0);
  787. break;
  788. case EAP_TYPE_AKA:
  789. prefix = '0';
  790. res = wpa_config_set(ssid, "eap", "AKA", 0);
  791. break;
  792. case EAP_TYPE_AKA_PRIME:
  793. prefix = '6';
  794. res = wpa_config_set(ssid, "eap", "AKA'", 0);
  795. break;
  796. default:
  797. res = -1;
  798. break;
  799. }
  800. if (res < 0) {
  801. wpa_printf(MSG_DEBUG, "Selected EAP method (%d) not supported",
  802. eap_type);
  803. goto fail;
  804. }
  805. if (!cred->pcsc && set_root_nai(ssid, cred->imsi, prefix) < 0) {
  806. wpa_printf(MSG_DEBUG, "Failed to set Root NAI");
  807. goto fail;
  808. }
  809. if (cred->milenage && cred->milenage[0]) {
  810. if (wpa_config_set_quoted(ssid, "password",
  811. cred->milenage) < 0)
  812. goto fail;
  813. } else if (cred->pcsc) {
  814. if (wpa_config_set_quoted(ssid, "pcsc", "") < 0)
  815. goto fail;
  816. if (wpa_s->conf->pcsc_pin &&
  817. wpa_config_set_quoted(ssid, "pin", wpa_s->conf->pcsc_pin)
  818. < 0)
  819. goto fail;
  820. }
  821. if (cred->password && cred->password[0] &&
  822. wpa_config_set_quoted(ssid, "password", cred->password) < 0)
  823. goto fail;
  824. wpa_s->next_ssid = ssid;
  825. wpa_config_update_prio_list(wpa_s->conf);
  826. interworking_reconnect(wpa_s);
  827. return 0;
  828. fail:
  829. wpas_notify_network_removed(wpa_s, ssid);
  830. wpa_config_remove_network(wpa_s->conf, ssid->id);
  831. #endif /* INTERWORKING_3GPP */
  832. return -1;
  833. }
  834. static int roaming_consortium_element_match(const u8 *ie, const u8 *rc_id,
  835. size_t rc_len)
  836. {
  837. const u8 *pos, *end;
  838. u8 lens;
  839. if (ie == NULL)
  840. return 0;
  841. pos = ie + 2;
  842. end = ie + 2 + ie[1];
  843. /* Roaming Consortium element:
  844. * Number of ANQP OIs
  845. * OI #1 and #2 lengths
  846. * OI #1, [OI #2], [OI #3]
  847. */
  848. if (pos + 2 > end)
  849. return 0;
  850. pos++; /* skip Number of ANQP OIs */
  851. lens = *pos++;
  852. if (pos + (lens & 0x0f) + (lens >> 4) > end)
  853. return 0;
  854. if ((lens & 0x0f) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  855. return 1;
  856. pos += lens & 0x0f;
  857. if ((lens >> 4) == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  858. return 1;
  859. pos += lens >> 4;
  860. if (pos < end && (size_t) (end - pos) == rc_len &&
  861. os_memcmp(pos, rc_id, rc_len) == 0)
  862. return 1;
  863. return 0;
  864. }
  865. static int roaming_consortium_anqp_match(const struct wpabuf *anqp,
  866. const u8 *rc_id, size_t rc_len)
  867. {
  868. const u8 *pos, *end;
  869. u8 len;
  870. if (anqp == NULL)
  871. return 0;
  872. pos = wpabuf_head(anqp);
  873. end = pos + wpabuf_len(anqp);
  874. /* Set of <OI Length, OI> duples */
  875. while (pos < end) {
  876. len = *pos++;
  877. if (pos + len > end)
  878. break;
  879. if (len == rc_len && os_memcmp(pos, rc_id, rc_len) == 0)
  880. return 1;
  881. pos += len;
  882. }
  883. return 0;
  884. }
  885. static int roaming_consortium_match(const u8 *ie, const struct wpabuf *anqp,
  886. const u8 *rc_id, size_t rc_len)
  887. {
  888. return roaming_consortium_element_match(ie, rc_id, rc_len) ||
  889. roaming_consortium_anqp_match(anqp, rc_id, rc_len);
  890. }
  891. static int cred_no_required_oi_match(struct wpa_cred *cred, struct wpa_bss *bss)
  892. {
  893. const u8 *ie;
  894. if (cred->required_roaming_consortium_len == 0)
  895. return 0;
  896. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  897. if (ie == NULL &&
  898. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  899. return 1;
  900. return !roaming_consortium_match(ie,
  901. bss->anqp ?
  902. bss->anqp->roaming_consortium : NULL,
  903. cred->required_roaming_consortium,
  904. cred->required_roaming_consortium_len);
  905. }
  906. static int cred_excluded_ssid(struct wpa_cred *cred, struct wpa_bss *bss)
  907. {
  908. size_t i;
  909. if (!cred->excluded_ssid)
  910. return 0;
  911. for (i = 0; i < cred->num_excluded_ssid; i++) {
  912. struct excluded_ssid *e = &cred->excluded_ssid[i];
  913. if (bss->ssid_len == e->ssid_len &&
  914. os_memcmp(bss->ssid, e->ssid, e->ssid_len) == 0)
  915. return 1;
  916. }
  917. return 0;
  918. }
  919. static int cred_below_min_backhaul(struct wpa_supplicant *wpa_s,
  920. struct wpa_cred *cred, struct wpa_bss *bss)
  921. {
  922. int res;
  923. unsigned int dl_bandwidth, ul_bandwidth;
  924. const u8 *wan;
  925. u8 wan_info, dl_load, ul_load;
  926. u16 lmd;
  927. u32 ul_speed, dl_speed;
  928. if (!cred->min_dl_bandwidth_home &&
  929. !cred->min_ul_bandwidth_home &&
  930. !cred->min_dl_bandwidth_roaming &&
  931. !cred->min_ul_bandwidth_roaming)
  932. return 0; /* No bandwidth constraint specified */
  933. if (bss->anqp == NULL || bss->anqp->hs20_wan_metrics == NULL)
  934. return 0; /* No WAN Metrics known - ignore constraint */
  935. wan = wpabuf_head(bss->anqp->hs20_wan_metrics);
  936. wan_info = wan[0];
  937. if (wan_info & BIT(3))
  938. return 1; /* WAN link at capacity */
  939. lmd = WPA_GET_LE16(wan + 11);
  940. if (lmd == 0)
  941. return 0; /* Downlink/Uplink Load was not measured */
  942. dl_speed = WPA_GET_LE32(wan + 1);
  943. ul_speed = WPA_GET_LE32(wan + 5);
  944. dl_load = wan[9];
  945. ul_load = wan[10];
  946. if (dl_speed >= 0xffffff)
  947. dl_bandwidth = dl_speed / 255 * (255 - dl_load);
  948. else
  949. dl_bandwidth = dl_speed * (255 - dl_load) / 255;
  950. if (ul_speed >= 0xffffff)
  951. ul_bandwidth = ul_speed / 255 * (255 - ul_load);
  952. else
  953. ul_bandwidth = ul_speed * (255 - ul_load) / 255;
  954. res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
  955. bss->anqp->domain_name : NULL);
  956. if (res > 0) {
  957. if (cred->min_dl_bandwidth_home > dl_bandwidth)
  958. return 1;
  959. if (cred->min_ul_bandwidth_home > ul_bandwidth)
  960. return 1;
  961. } else {
  962. if (cred->min_dl_bandwidth_roaming > dl_bandwidth)
  963. return 1;
  964. if (cred->min_ul_bandwidth_roaming > ul_bandwidth)
  965. return 1;
  966. }
  967. return 0;
  968. }
  969. static int cred_over_max_bss_load(struct wpa_supplicant *wpa_s,
  970. struct wpa_cred *cred, struct wpa_bss *bss)
  971. {
  972. const u8 *ie;
  973. int res;
  974. if (!cred->max_bss_load)
  975. return 0; /* No BSS Load constraint specified */
  976. ie = wpa_bss_get_ie(bss, WLAN_EID_BSS_LOAD);
  977. if (ie == NULL || ie[1] < 3)
  978. return 0; /* No BSS Load advertised */
  979. res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
  980. bss->anqp->domain_name : NULL);
  981. if (res <= 0)
  982. return 0; /* Not a home network */
  983. return ie[4] > cred->max_bss_load;
  984. }
  985. static int has_proto_match(const u8 *pos, const u8 *end, u8 proto)
  986. {
  987. while (pos + 4 <= end) {
  988. if (pos[0] == proto && pos[3] == 1 /* Open */)
  989. return 1;
  990. pos += 4;
  991. }
  992. return 0;
  993. }
  994. static int has_proto_port_match(const u8 *pos, const u8 *end, u8 proto,
  995. u16 port)
  996. {
  997. while (pos + 4 <= end) {
  998. if (pos[0] == proto && WPA_GET_LE16(&pos[1]) == port &&
  999. pos[3] == 1 /* Open */)
  1000. return 1;
  1001. pos += 4;
  1002. }
  1003. return 0;
  1004. }
  1005. static int cred_conn_capab_missing(struct wpa_supplicant *wpa_s,
  1006. struct wpa_cred *cred, struct wpa_bss *bss)
  1007. {
  1008. int res;
  1009. const u8 *capab, *end;
  1010. unsigned int i, j;
  1011. int *ports;
  1012. if (!cred->num_req_conn_capab)
  1013. return 0; /* No connection capability constraint specified */
  1014. if (bss->anqp == NULL || bss->anqp->hs20_connection_capability == NULL)
  1015. return 0; /* No Connection Capability known - ignore constraint
  1016. */
  1017. res = interworking_home_sp_cred(wpa_s, cred, bss->anqp ?
  1018. bss->anqp->domain_name : NULL);
  1019. if (res > 0)
  1020. return 0; /* No constraint in home network */
  1021. capab = wpabuf_head(bss->anqp->hs20_connection_capability);
  1022. end = capab + wpabuf_len(bss->anqp->hs20_connection_capability);
  1023. for (i = 0; i < cred->num_req_conn_capab; i++) {
  1024. ports = cred->req_conn_capab_port[i];
  1025. if (!ports) {
  1026. if (!has_proto_match(capab, end,
  1027. cred->req_conn_capab_proto[i]))
  1028. return 1;
  1029. } else {
  1030. for (j = 0; ports[j] > -1; j++) {
  1031. if (!has_proto_port_match(
  1032. capab, end,
  1033. cred->req_conn_capab_proto[i],
  1034. ports[j]))
  1035. return 1;
  1036. }
  1037. }
  1038. }
  1039. return 0;
  1040. }
  1041. static struct wpa_cred * interworking_credentials_available_roaming_consortium(
  1042. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1043. int *excluded)
  1044. {
  1045. struct wpa_cred *cred, *selected = NULL;
  1046. const u8 *ie;
  1047. int is_excluded = 0;
  1048. ie = wpa_bss_get_ie(bss, WLAN_EID_ROAMING_CONSORTIUM);
  1049. if (ie == NULL &&
  1050. (bss->anqp == NULL || bss->anqp->roaming_consortium == NULL))
  1051. return NULL;
  1052. if (wpa_s->conf->cred == NULL)
  1053. return NULL;
  1054. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1055. if (cred->roaming_consortium_len == 0)
  1056. continue;
  1057. if (!roaming_consortium_match(ie,
  1058. bss->anqp ?
  1059. bss->anqp->roaming_consortium :
  1060. NULL,
  1061. cred->roaming_consortium,
  1062. cred->roaming_consortium_len))
  1063. continue;
  1064. if (cred_no_required_oi_match(cred, bss))
  1065. continue;
  1066. if (!ignore_bw && cred_below_min_backhaul(wpa_s, cred, bss))
  1067. continue;
  1068. if (!ignore_bw && cred_over_max_bss_load(wpa_s, cred, bss))
  1069. continue;
  1070. if (!ignore_bw && cred_conn_capab_missing(wpa_s, cred, bss))
  1071. continue;
  1072. if (cred_excluded_ssid(cred, bss)) {
  1073. if (excluded == NULL)
  1074. continue;
  1075. if (selected == NULL) {
  1076. selected = cred;
  1077. is_excluded = 1;
  1078. }
  1079. } else {
  1080. if (selected == NULL || is_excluded ||
  1081. cred_prio_cmp(selected, cred) < 0) {
  1082. selected = cred;
  1083. is_excluded = 0;
  1084. }
  1085. }
  1086. }
  1087. if (excluded)
  1088. *excluded = is_excluded;
  1089. return selected;
  1090. }
  1091. static int interworking_set_eap_params(struct wpa_ssid *ssid,
  1092. struct wpa_cred *cred, int ttls)
  1093. {
  1094. if (cred->eap_method) {
  1095. ttls = cred->eap_method->vendor == EAP_VENDOR_IETF &&
  1096. cred->eap_method->method == EAP_TYPE_TTLS;
  1097. os_free(ssid->eap.eap_methods);
  1098. ssid->eap.eap_methods =
  1099. os_malloc(sizeof(struct eap_method_type) * 2);
  1100. if (ssid->eap.eap_methods == NULL)
  1101. return -1;
  1102. os_memcpy(ssid->eap.eap_methods, cred->eap_method,
  1103. sizeof(*cred->eap_method));
  1104. ssid->eap.eap_methods[1].vendor = EAP_VENDOR_IETF;
  1105. ssid->eap.eap_methods[1].method = EAP_TYPE_NONE;
  1106. }
  1107. if (ttls && cred->username && cred->username[0]) {
  1108. const char *pos;
  1109. char *anon;
  1110. /* Use anonymous NAI in Phase 1 */
  1111. pos = os_strchr(cred->username, '@');
  1112. if (pos) {
  1113. size_t buflen = 9 + os_strlen(pos) + 1;
  1114. anon = os_malloc(buflen);
  1115. if (anon == NULL)
  1116. return -1;
  1117. os_snprintf(anon, buflen, "anonymous%s", pos);
  1118. } else if (cred->realm) {
  1119. size_t buflen = 10 + os_strlen(cred->realm) + 1;
  1120. anon = os_malloc(buflen);
  1121. if (anon == NULL)
  1122. return -1;
  1123. os_snprintf(anon, buflen, "anonymous@%s", cred->realm);
  1124. } else {
  1125. anon = os_strdup("anonymous");
  1126. if (anon == NULL)
  1127. return -1;
  1128. }
  1129. if (wpa_config_set_quoted(ssid, "anonymous_identity", anon) <
  1130. 0) {
  1131. os_free(anon);
  1132. return -1;
  1133. }
  1134. os_free(anon);
  1135. }
  1136. if (cred->username && cred->username[0] &&
  1137. wpa_config_set_quoted(ssid, "identity", cred->username) < 0)
  1138. return -1;
  1139. if (cred->password && cred->password[0]) {
  1140. if (cred->ext_password &&
  1141. wpa_config_set(ssid, "password", cred->password, 0) < 0)
  1142. return -1;
  1143. if (!cred->ext_password &&
  1144. wpa_config_set_quoted(ssid, "password", cred->password) <
  1145. 0)
  1146. return -1;
  1147. }
  1148. if (cred->client_cert && cred->client_cert[0] &&
  1149. wpa_config_set_quoted(ssid, "client_cert", cred->client_cert) < 0)
  1150. return -1;
  1151. #ifdef ANDROID
  1152. if (cred->private_key &&
  1153. os_strncmp(cred->private_key, "keystore://", 11) == 0) {
  1154. /* Use OpenSSL engine configuration for Android keystore */
  1155. if (wpa_config_set_quoted(ssid, "engine_id", "keystore") < 0 ||
  1156. wpa_config_set_quoted(ssid, "key_id",
  1157. cred->private_key + 11) < 0 ||
  1158. wpa_config_set(ssid, "engine", "1", 0) < 0)
  1159. return -1;
  1160. } else
  1161. #endif /* ANDROID */
  1162. if (cred->private_key && cred->private_key[0] &&
  1163. wpa_config_set_quoted(ssid, "private_key", cred->private_key) < 0)
  1164. return -1;
  1165. if (cred->private_key_passwd && cred->private_key_passwd[0] &&
  1166. wpa_config_set_quoted(ssid, "private_key_passwd",
  1167. cred->private_key_passwd) < 0)
  1168. return -1;
  1169. if (cred->phase1) {
  1170. os_free(ssid->eap.phase1);
  1171. ssid->eap.phase1 = os_strdup(cred->phase1);
  1172. }
  1173. if (cred->phase2) {
  1174. os_free(ssid->eap.phase2);
  1175. ssid->eap.phase2 = os_strdup(cred->phase2);
  1176. }
  1177. if (cred->ca_cert && cred->ca_cert[0] &&
  1178. wpa_config_set_quoted(ssid, "ca_cert", cred->ca_cert) < 0)
  1179. return -1;
  1180. if (cred->domain_suffix_match && cred->domain_suffix_match[0] &&
  1181. wpa_config_set_quoted(ssid, "domain_suffix_match",
  1182. cred->domain_suffix_match) < 0)
  1183. return -1;
  1184. ssid->eap.ocsp = cred->ocsp;
  1185. return 0;
  1186. }
  1187. static int interworking_connect_roaming_consortium(
  1188. struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  1189. struct wpa_bss *bss)
  1190. {
  1191. struct wpa_ssid *ssid;
  1192. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR " based on "
  1193. "roaming consortium match", MAC2STR(bss->bssid));
  1194. if (already_connected(wpa_s, cred, bss)) {
  1195. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1196. MAC2STR(bss->bssid));
  1197. return 0;
  1198. }
  1199. remove_duplicate_network(wpa_s, cred, bss);
  1200. ssid = wpa_config_add_network(wpa_s->conf);
  1201. if (ssid == NULL)
  1202. return -1;
  1203. ssid->parent_cred = cred;
  1204. wpas_notify_network_added(wpa_s, ssid);
  1205. wpa_config_set_network_defaults(ssid);
  1206. ssid->priority = cred->priority;
  1207. ssid->temporary = 1;
  1208. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  1209. if (ssid->ssid == NULL)
  1210. goto fail;
  1211. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  1212. ssid->ssid_len = bss->ssid_len;
  1213. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1214. goto fail;
  1215. if (cred->eap_method == NULL) {
  1216. wpa_printf(MSG_DEBUG, "Interworking: No EAP method set for "
  1217. "credential using roaming consortium");
  1218. goto fail;
  1219. }
  1220. if (interworking_set_eap_params(
  1221. ssid, cred,
  1222. cred->eap_method->vendor == EAP_VENDOR_IETF &&
  1223. cred->eap_method->method == EAP_TYPE_TTLS) < 0)
  1224. goto fail;
  1225. wpa_s->next_ssid = ssid;
  1226. wpa_config_update_prio_list(wpa_s->conf);
  1227. interworking_reconnect(wpa_s);
  1228. return 0;
  1229. fail:
  1230. wpas_notify_network_removed(wpa_s, ssid);
  1231. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1232. return -1;
  1233. }
  1234. static int interworking_connect_helper(struct wpa_supplicant *wpa_s,
  1235. struct wpa_bss *bss, int allow_excluded)
  1236. {
  1237. struct wpa_cred *cred, *cred_rc, *cred_3gpp;
  1238. struct wpa_ssid *ssid;
  1239. struct nai_realm *realm;
  1240. struct nai_realm_eap *eap = NULL;
  1241. u16 count, i;
  1242. char buf[100];
  1243. int excluded = 0, *excl = allow_excluded ? &excluded : NULL;
  1244. if (wpa_s->conf->cred == NULL || bss == NULL)
  1245. return -1;
  1246. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1247. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1248. wpa_printf(MSG_DEBUG, "Interworking: Reject connection to disallowed BSS "
  1249. MACSTR, MAC2STR(bss->bssid));
  1250. return -1;
  1251. }
  1252. wpa_printf(MSG_DEBUG, "Interworking: Considering BSS " MACSTR
  1253. " for connection (allow_excluded=%d)",
  1254. MAC2STR(bss->bssid), allow_excluded);
  1255. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1256. /*
  1257. * We currently support only HS 2.0 networks and those are
  1258. * required to use WPA2-Enterprise.
  1259. */
  1260. wpa_printf(MSG_DEBUG, "Interworking: Network does not use "
  1261. "RSN");
  1262. return -1;
  1263. }
  1264. cred_rc = interworking_credentials_available_roaming_consortium(
  1265. wpa_s, bss, 0, excl);
  1266. if (cred_rc) {
  1267. wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
  1268. "consortium matching credential priority %d "
  1269. "sp_priority %d",
  1270. cred_rc->priority, cred_rc->sp_priority);
  1271. if (allow_excluded && excl && !(*excl))
  1272. excl = NULL;
  1273. }
  1274. cred = interworking_credentials_available_realm(wpa_s, bss, 0, excl);
  1275. if (cred) {
  1276. wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm list "
  1277. "matching credential priority %d sp_priority %d",
  1278. cred->priority, cred->sp_priority);
  1279. if (allow_excluded && excl && !(*excl))
  1280. excl = NULL;
  1281. }
  1282. cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss, 0,
  1283. excl);
  1284. if (cred_3gpp) {
  1285. wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP matching "
  1286. "credential priority %d sp_priority %d",
  1287. cred_3gpp->priority, cred_3gpp->sp_priority);
  1288. if (allow_excluded && excl && !(*excl))
  1289. excl = NULL;
  1290. }
  1291. if (!cred_rc && !cred && !cred_3gpp) {
  1292. wpa_printf(MSG_DEBUG, "Interworking: No full credential matches - consider options without BW(etc.) limits");
  1293. cred_rc = interworking_credentials_available_roaming_consortium(
  1294. wpa_s, bss, 1, excl);
  1295. if (cred_rc) {
  1296. wpa_printf(MSG_DEBUG, "Interworking: Highest roaming "
  1297. "consortium matching credential priority %d "
  1298. "sp_priority %d (ignore BW)",
  1299. cred_rc->priority, cred_rc->sp_priority);
  1300. if (allow_excluded && excl && !(*excl))
  1301. excl = NULL;
  1302. }
  1303. cred = interworking_credentials_available_realm(wpa_s, bss, 1,
  1304. excl);
  1305. if (cred) {
  1306. wpa_printf(MSG_DEBUG, "Interworking: Highest NAI Realm "
  1307. "list matching credential priority %d "
  1308. "sp_priority %d (ignore BW)",
  1309. cred->priority, cred->sp_priority);
  1310. if (allow_excluded && excl && !(*excl))
  1311. excl = NULL;
  1312. }
  1313. cred_3gpp = interworking_credentials_available_3gpp(wpa_s, bss,
  1314. 1, excl);
  1315. if (cred_3gpp) {
  1316. wpa_printf(MSG_DEBUG, "Interworking: Highest 3GPP "
  1317. "matching credential priority %d "
  1318. "sp_priority %d (ignore BW)",
  1319. cred_3gpp->priority, cred_3gpp->sp_priority);
  1320. if (allow_excluded && excl && !(*excl))
  1321. excl = NULL;
  1322. }
  1323. }
  1324. if (cred_rc &&
  1325. (cred == NULL || cred_prio_cmp(cred_rc, cred) >= 0) &&
  1326. (cred_3gpp == NULL || cred_prio_cmp(cred_rc, cred_3gpp) >= 0))
  1327. return interworking_connect_roaming_consortium(wpa_s, cred_rc,
  1328. bss);
  1329. if (cred_3gpp &&
  1330. (cred == NULL || cred_prio_cmp(cred_3gpp, cred) >= 0)) {
  1331. return interworking_connect_3gpp(wpa_s, cred_3gpp, bss);
  1332. }
  1333. if (cred == NULL) {
  1334. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1335. "found for " MACSTR, MAC2STR(bss->bssid));
  1336. return -1;
  1337. }
  1338. realm = nai_realm_parse(bss->anqp ? bss->anqp->nai_realm : NULL,
  1339. &count);
  1340. if (realm == NULL) {
  1341. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1342. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1343. return -1;
  1344. }
  1345. for (i = 0; i < count; i++) {
  1346. if (!nai_realm_match(&realm[i], cred->realm))
  1347. continue;
  1348. eap = nai_realm_find_eap(cred, &realm[i]);
  1349. if (eap)
  1350. break;
  1351. }
  1352. if (!eap) {
  1353. wpa_printf(MSG_DEBUG, "Interworking: No matching credentials "
  1354. "and EAP method found for " MACSTR,
  1355. MAC2STR(bss->bssid));
  1356. nai_realm_free(realm, count);
  1357. return -1;
  1358. }
  1359. wpa_printf(MSG_DEBUG, "Interworking: Connect with " MACSTR,
  1360. MAC2STR(bss->bssid));
  1361. if (already_connected(wpa_s, cred, bss)) {
  1362. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_ALREADY_CONNECTED MACSTR,
  1363. MAC2STR(bss->bssid));
  1364. nai_realm_free(realm, count);
  1365. return 0;
  1366. }
  1367. remove_duplicate_network(wpa_s, cred, bss);
  1368. ssid = wpa_config_add_network(wpa_s->conf);
  1369. if (ssid == NULL) {
  1370. nai_realm_free(realm, count);
  1371. return -1;
  1372. }
  1373. ssid->parent_cred = cred;
  1374. wpas_notify_network_added(wpa_s, ssid);
  1375. wpa_config_set_network_defaults(ssid);
  1376. ssid->priority = cred->priority;
  1377. ssid->temporary = 1;
  1378. ssid->ssid = os_zalloc(bss->ssid_len + 1);
  1379. if (ssid->ssid == NULL)
  1380. goto fail;
  1381. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  1382. ssid->ssid_len = bss->ssid_len;
  1383. if (interworking_set_hs20_params(wpa_s, ssid) < 0)
  1384. goto fail;
  1385. if (wpa_config_set(ssid, "eap", eap_get_name(EAP_VENDOR_IETF,
  1386. eap->method), 0) < 0)
  1387. goto fail;
  1388. switch (eap->method) {
  1389. case EAP_TYPE_TTLS:
  1390. if (eap->inner_method) {
  1391. os_snprintf(buf, sizeof(buf), "\"autheap=%s\"",
  1392. eap_get_name(EAP_VENDOR_IETF,
  1393. eap->inner_method));
  1394. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1395. goto fail;
  1396. break;
  1397. }
  1398. switch (eap->inner_non_eap) {
  1399. case NAI_REALM_INNER_NON_EAP_PAP:
  1400. if (wpa_config_set(ssid, "phase2", "\"auth=PAP\"", 0) <
  1401. 0)
  1402. goto fail;
  1403. break;
  1404. case NAI_REALM_INNER_NON_EAP_CHAP:
  1405. if (wpa_config_set(ssid, "phase2", "\"auth=CHAP\"", 0)
  1406. < 0)
  1407. goto fail;
  1408. break;
  1409. case NAI_REALM_INNER_NON_EAP_MSCHAP:
  1410. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAP\"",
  1411. 0) < 0)
  1412. goto fail;
  1413. break;
  1414. case NAI_REALM_INNER_NON_EAP_MSCHAPV2:
  1415. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1416. 0) < 0)
  1417. goto fail;
  1418. break;
  1419. default:
  1420. /* EAP params were not set - assume TTLS/MSCHAPv2 */
  1421. if (wpa_config_set(ssid, "phase2", "\"auth=MSCHAPV2\"",
  1422. 0) < 0)
  1423. goto fail;
  1424. break;
  1425. }
  1426. break;
  1427. case EAP_TYPE_PEAP:
  1428. case EAP_TYPE_FAST:
  1429. if (wpa_config_set(ssid, "phase1", "\"fast_provisioning=2\"",
  1430. 0) < 0)
  1431. goto fail;
  1432. if (wpa_config_set(ssid, "pac_file",
  1433. "\"blob://pac_interworking\"", 0) < 0)
  1434. goto fail;
  1435. os_snprintf(buf, sizeof(buf), "\"auth=%s\"",
  1436. eap_get_name(EAP_VENDOR_IETF,
  1437. eap->inner_method ?
  1438. eap->inner_method :
  1439. EAP_TYPE_MSCHAPV2));
  1440. if (wpa_config_set(ssid, "phase2", buf, 0) < 0)
  1441. goto fail;
  1442. break;
  1443. case EAP_TYPE_TLS:
  1444. break;
  1445. }
  1446. if (interworking_set_eap_params(ssid, cred,
  1447. eap->method == EAP_TYPE_TTLS) < 0)
  1448. goto fail;
  1449. nai_realm_free(realm, count);
  1450. wpa_s->next_ssid = ssid;
  1451. wpa_config_update_prio_list(wpa_s->conf);
  1452. interworking_reconnect(wpa_s);
  1453. return 0;
  1454. fail:
  1455. wpas_notify_network_removed(wpa_s, ssid);
  1456. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1457. nai_realm_free(realm, count);
  1458. return -1;
  1459. }
  1460. int interworking_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  1461. {
  1462. return interworking_connect_helper(wpa_s, bss, 1);
  1463. }
  1464. static struct wpa_cred * interworking_credentials_available_3gpp(
  1465. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1466. int *excluded)
  1467. {
  1468. struct wpa_cred *selected = NULL;
  1469. #ifdef INTERWORKING_3GPP
  1470. struct wpa_cred *cred;
  1471. int ret;
  1472. int is_excluded = 0;
  1473. if (bss->anqp == NULL || bss->anqp->anqp_3gpp == NULL)
  1474. return NULL;
  1475. #ifdef CONFIG_EAP_PROXY
  1476. if (!wpa_s->imsi[0]) {
  1477. size_t len;
  1478. wpa_printf(MSG_DEBUG, "Interworking: IMSI not available - try to read again through eap_proxy");
  1479. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol,
  1480. wpa_s->imsi,
  1481. &len);
  1482. if (wpa_s->mnc_len > 0) {
  1483. wpa_s->imsi[len] = '\0';
  1484. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  1485. wpa_s->imsi, wpa_s->mnc_len);
  1486. } else {
  1487. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  1488. }
  1489. }
  1490. #endif /* CONFIG_EAP_PROXY */
  1491. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1492. char *sep;
  1493. const char *imsi;
  1494. int mnc_len;
  1495. char imsi_buf[16];
  1496. size_t msin_len;
  1497. #ifdef PCSC_FUNCS
  1498. if (cred->pcsc && wpa_s->conf->pcsc_reader && wpa_s->scard &&
  1499. wpa_s->imsi[0]) {
  1500. imsi = wpa_s->imsi;
  1501. mnc_len = wpa_s->mnc_len;
  1502. goto compare;
  1503. }
  1504. #endif /* PCSC_FUNCS */
  1505. #ifdef CONFIG_EAP_PROXY
  1506. if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1507. imsi = wpa_s->imsi;
  1508. mnc_len = wpa_s->mnc_len;
  1509. goto compare;
  1510. }
  1511. #endif /* CONFIG_EAP_PROXY */
  1512. if (cred->imsi == NULL || !cred->imsi[0] ||
  1513. (!wpa_s->conf->external_sim &&
  1514. (cred->milenage == NULL || !cred->milenage[0])))
  1515. continue;
  1516. sep = os_strchr(cred->imsi, '-');
  1517. if (sep == NULL ||
  1518. (sep - cred->imsi != 5 && sep - cred->imsi != 6))
  1519. continue;
  1520. mnc_len = sep - cred->imsi - 3;
  1521. os_memcpy(imsi_buf, cred->imsi, 3 + mnc_len);
  1522. sep++;
  1523. msin_len = os_strlen(cred->imsi);
  1524. if (3 + mnc_len + msin_len >= sizeof(imsi_buf) - 1)
  1525. msin_len = sizeof(imsi_buf) - 3 - mnc_len - 1;
  1526. os_memcpy(&imsi_buf[3 + mnc_len], sep, msin_len);
  1527. imsi_buf[3 + mnc_len + msin_len] = '\0';
  1528. imsi = imsi_buf;
  1529. #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
  1530. compare:
  1531. #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
  1532. wpa_printf(MSG_DEBUG, "Interworking: Parsing 3GPP info from "
  1533. MACSTR, MAC2STR(bss->bssid));
  1534. ret = plmn_id_match(bss->anqp->anqp_3gpp, imsi, mnc_len);
  1535. wpa_printf(MSG_DEBUG, "PLMN match %sfound", ret ? "" : "not ");
  1536. if (ret) {
  1537. if (cred_no_required_oi_match(cred, bss))
  1538. continue;
  1539. if (!ignore_bw &&
  1540. cred_below_min_backhaul(wpa_s, cred, bss))
  1541. continue;
  1542. if (!ignore_bw &&
  1543. cred_over_max_bss_load(wpa_s, cred, bss))
  1544. continue;
  1545. if (!ignore_bw &&
  1546. cred_conn_capab_missing(wpa_s, cred, bss))
  1547. continue;
  1548. if (cred_excluded_ssid(cred, bss)) {
  1549. if (excluded == NULL)
  1550. continue;
  1551. if (selected == NULL) {
  1552. selected = cred;
  1553. is_excluded = 1;
  1554. }
  1555. } else {
  1556. if (selected == NULL || is_excluded ||
  1557. cred_prio_cmp(selected, cred) < 0) {
  1558. selected = cred;
  1559. is_excluded = 0;
  1560. }
  1561. }
  1562. }
  1563. }
  1564. if (excluded)
  1565. *excluded = is_excluded;
  1566. #endif /* INTERWORKING_3GPP */
  1567. return selected;
  1568. }
  1569. static struct wpa_cred * interworking_credentials_available_realm(
  1570. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1571. int *excluded)
  1572. {
  1573. struct wpa_cred *cred, *selected = NULL;
  1574. struct nai_realm *realm;
  1575. u16 count, i;
  1576. int is_excluded = 0;
  1577. if (bss->anqp == NULL || bss->anqp->nai_realm == NULL)
  1578. return NULL;
  1579. if (wpa_s->conf->cred == NULL)
  1580. return NULL;
  1581. wpa_printf(MSG_DEBUG, "Interworking: Parsing NAI Realm list from "
  1582. MACSTR, MAC2STR(bss->bssid));
  1583. realm = nai_realm_parse(bss->anqp->nai_realm, &count);
  1584. if (realm == NULL) {
  1585. wpa_printf(MSG_DEBUG, "Interworking: Could not parse NAI "
  1586. "Realm list from " MACSTR, MAC2STR(bss->bssid));
  1587. return NULL;
  1588. }
  1589. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1590. if (cred->realm == NULL)
  1591. continue;
  1592. for (i = 0; i < count; i++) {
  1593. if (!nai_realm_match(&realm[i], cred->realm))
  1594. continue;
  1595. if (nai_realm_find_eap(cred, &realm[i])) {
  1596. if (cred_no_required_oi_match(cred, bss))
  1597. continue;
  1598. if (!ignore_bw &&
  1599. cred_below_min_backhaul(wpa_s, cred, bss))
  1600. continue;
  1601. if (!ignore_bw &&
  1602. cred_over_max_bss_load(wpa_s, cred, bss))
  1603. continue;
  1604. if (!ignore_bw &&
  1605. cred_conn_capab_missing(wpa_s, cred, bss))
  1606. continue;
  1607. if (cred_excluded_ssid(cred, bss)) {
  1608. if (excluded == NULL)
  1609. continue;
  1610. if (selected == NULL) {
  1611. selected = cred;
  1612. is_excluded = 1;
  1613. }
  1614. } else {
  1615. if (selected == NULL || is_excluded ||
  1616. cred_prio_cmp(selected, cred) < 0)
  1617. {
  1618. selected = cred;
  1619. is_excluded = 0;
  1620. }
  1621. }
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. nai_realm_free(realm, count);
  1627. if (excluded)
  1628. *excluded = is_excluded;
  1629. return selected;
  1630. }
  1631. static struct wpa_cred * interworking_credentials_available_helper(
  1632. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int ignore_bw,
  1633. int *excluded)
  1634. {
  1635. struct wpa_cred *cred, *cred2;
  1636. int excluded1, excluded2;
  1637. if (disallowed_bssid(wpa_s, bss->bssid) ||
  1638. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
  1639. wpa_printf(MSG_DEBUG, "Interworking: Ignore disallowed BSS "
  1640. MACSTR, MAC2STR(bss->bssid));
  1641. return NULL;
  1642. }
  1643. cred = interworking_credentials_available_realm(wpa_s, bss, ignore_bw,
  1644. &excluded1);
  1645. cred2 = interworking_credentials_available_3gpp(wpa_s, bss, ignore_bw,
  1646. &excluded2);
  1647. if (cred && cred2 &&
  1648. (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
  1649. cred = cred2;
  1650. excluded1 = excluded2;
  1651. }
  1652. if (!cred) {
  1653. cred = cred2;
  1654. excluded1 = excluded2;
  1655. }
  1656. cred2 = interworking_credentials_available_roaming_consortium(
  1657. wpa_s, bss, ignore_bw, &excluded2);
  1658. if (cred && cred2 &&
  1659. (cred_prio_cmp(cred2, cred) >= 0 || (!excluded2 && excluded1))) {
  1660. cred = cred2;
  1661. excluded1 = excluded2;
  1662. }
  1663. if (!cred) {
  1664. cred = cred2;
  1665. excluded1 = excluded2;
  1666. }
  1667. if (excluded)
  1668. *excluded = excluded1;
  1669. return cred;
  1670. }
  1671. static struct wpa_cred * interworking_credentials_available(
  1672. struct wpa_supplicant *wpa_s, struct wpa_bss *bss, int *excluded)
  1673. {
  1674. struct wpa_cred *cred;
  1675. if (excluded)
  1676. *excluded = 0;
  1677. cred = interworking_credentials_available_helper(wpa_s, bss, 0,
  1678. excluded);
  1679. if (cred)
  1680. return cred;
  1681. return interworking_credentials_available_helper(wpa_s, bss, 1,
  1682. excluded);
  1683. }
  1684. int domain_name_list_contains(struct wpabuf *domain_names,
  1685. const char *domain, int exact_match)
  1686. {
  1687. const u8 *pos, *end;
  1688. size_t len;
  1689. len = os_strlen(domain);
  1690. pos = wpabuf_head(domain_names);
  1691. end = pos + wpabuf_len(domain_names);
  1692. while (pos + 1 < end) {
  1693. if (pos + 1 + pos[0] > end)
  1694. break;
  1695. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: AP domain name",
  1696. pos + 1, pos[0]);
  1697. if (pos[0] == len &&
  1698. os_strncasecmp(domain, (const char *) (pos + 1), len) == 0)
  1699. return 1;
  1700. if (!exact_match && pos[0] > len && pos[pos[0] - len] == '.') {
  1701. const char *ap = (const char *) (pos + 1);
  1702. int offset = pos[0] - len;
  1703. if (os_strncasecmp(domain, ap + offset, len) == 0)
  1704. return 1;
  1705. }
  1706. pos += 1 + pos[0];
  1707. }
  1708. return 0;
  1709. }
  1710. int interworking_home_sp_cred(struct wpa_supplicant *wpa_s,
  1711. struct wpa_cred *cred,
  1712. struct wpabuf *domain_names)
  1713. {
  1714. size_t i;
  1715. int ret = -1;
  1716. #ifdef INTERWORKING_3GPP
  1717. char nai[100], *realm;
  1718. char *imsi = NULL;
  1719. int mnc_len = 0;
  1720. if (cred->imsi)
  1721. imsi = cred->imsi;
  1722. #ifdef CONFIG_PCSC
  1723. else if (cred->pcsc && wpa_s->conf->pcsc_reader &&
  1724. wpa_s->scard && wpa_s->imsi[0]) {
  1725. imsi = wpa_s->imsi;
  1726. mnc_len = wpa_s->mnc_len;
  1727. }
  1728. #endif /* CONFIG_PCSC */
  1729. #ifdef CONFIG_EAP_PROXY
  1730. else if (cred->pcsc && wpa_s->mnc_len > 0 && wpa_s->imsi[0]) {
  1731. imsi = wpa_s->imsi;
  1732. mnc_len = wpa_s->mnc_len;
  1733. }
  1734. #endif /* CONFIG_EAP_PROXY */
  1735. if (domain_names &&
  1736. imsi && build_root_nai(nai, sizeof(nai), imsi, mnc_len, 0) == 0) {
  1737. realm = os_strchr(nai, '@');
  1738. if (realm)
  1739. realm++;
  1740. wpa_printf(MSG_DEBUG, "Interworking: Search for match "
  1741. "with SIM/USIM domain %s", realm);
  1742. if (realm &&
  1743. domain_name_list_contains(domain_names, realm, 1))
  1744. return 1;
  1745. if (realm)
  1746. ret = 0;
  1747. }
  1748. #endif /* INTERWORKING_3GPP */
  1749. if (domain_names == NULL || cred->domain == NULL)
  1750. return ret;
  1751. for (i = 0; i < cred->num_domain; i++) {
  1752. wpa_printf(MSG_DEBUG, "Interworking: Search for match with "
  1753. "home SP FQDN %s", cred->domain[i]);
  1754. if (domain_name_list_contains(domain_names, cred->domain[i], 1))
  1755. return 1;
  1756. }
  1757. return 0;
  1758. }
  1759. static int interworking_home_sp(struct wpa_supplicant *wpa_s,
  1760. struct wpabuf *domain_names)
  1761. {
  1762. struct wpa_cred *cred;
  1763. if (domain_names == NULL || wpa_s->conf->cred == NULL)
  1764. return -1;
  1765. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1766. int res = interworking_home_sp_cred(wpa_s, cred, domain_names);
  1767. if (res)
  1768. return res;
  1769. }
  1770. return 0;
  1771. }
  1772. static int interworking_find_network_match(struct wpa_supplicant *wpa_s)
  1773. {
  1774. struct wpa_bss *bss;
  1775. struct wpa_ssid *ssid;
  1776. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1777. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1778. if (wpas_network_disabled(wpa_s, ssid) ||
  1779. ssid->mode != WPAS_MODE_INFRA)
  1780. continue;
  1781. if (ssid->ssid_len != bss->ssid_len ||
  1782. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) !=
  1783. 0)
  1784. continue;
  1785. /*
  1786. * TODO: Consider more accurate matching of security
  1787. * configuration similarly to what is done in events.c
  1788. */
  1789. return 1;
  1790. }
  1791. }
  1792. return 0;
  1793. }
  1794. static int roaming_partner_match(struct wpa_supplicant *wpa_s,
  1795. struct roaming_partner *partner,
  1796. struct wpabuf *domain_names)
  1797. {
  1798. wpa_printf(MSG_DEBUG, "Interworking: Comparing roaming_partner info fqdn='%s' exact_match=%d priority=%u country='%s'",
  1799. partner->fqdn, partner->exact_match, partner->priority,
  1800. partner->country);
  1801. wpa_hexdump_ascii(MSG_DEBUG, "Interworking: Domain names",
  1802. wpabuf_head(domain_names),
  1803. wpabuf_len(domain_names));
  1804. if (!domain_name_list_contains(domain_names, partner->fqdn,
  1805. partner->exact_match))
  1806. return 0;
  1807. /* TODO: match Country */
  1808. return 1;
  1809. }
  1810. static u8 roaming_prio(struct wpa_supplicant *wpa_s, struct wpa_cred *cred,
  1811. struct wpa_bss *bss)
  1812. {
  1813. size_t i;
  1814. if (bss->anqp == NULL || bss->anqp->domain_name == NULL) {
  1815. wpa_printf(MSG_DEBUG, "Interworking: No ANQP domain name info -> use default roaming partner priority 128");
  1816. return 128; /* cannot check preference with domain name */
  1817. }
  1818. if (interworking_home_sp_cred(wpa_s, cred, bss->anqp->domain_name) > 0)
  1819. {
  1820. wpa_printf(MSG_DEBUG, "Interworking: Determined to be home SP -> use maximum preference 0 as roaming partner priority");
  1821. return 0; /* max preference for home SP network */
  1822. }
  1823. for (i = 0; i < cred->num_roaming_partner; i++) {
  1824. if (roaming_partner_match(wpa_s, &cred->roaming_partner[i],
  1825. bss->anqp->domain_name)) {
  1826. wpa_printf(MSG_DEBUG, "Interworking: Roaming partner preference match - priority %u",
  1827. cred->roaming_partner[i].priority);
  1828. return cred->roaming_partner[i].priority;
  1829. }
  1830. }
  1831. wpa_printf(MSG_DEBUG, "Interworking: No roaming partner preference match - use default roaming partner priority 128");
  1832. return 128;
  1833. }
  1834. static struct wpa_bss * pick_best_roaming_partner(struct wpa_supplicant *wpa_s,
  1835. struct wpa_bss *selected,
  1836. struct wpa_cred *cred)
  1837. {
  1838. struct wpa_bss *bss;
  1839. u8 best_prio, prio;
  1840. struct wpa_cred *cred2;
  1841. /*
  1842. * Check if any other BSS is operated by a more preferred roaming
  1843. * partner.
  1844. */
  1845. best_prio = roaming_prio(wpa_s, cred, selected);
  1846. wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for selected BSS "
  1847. MACSTR " (cred=%d)", best_prio, MAC2STR(selected->bssid),
  1848. cred->id);
  1849. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1850. if (bss == selected)
  1851. continue;
  1852. cred2 = interworking_credentials_available(wpa_s, bss, NULL);
  1853. if (!cred2)
  1854. continue;
  1855. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN))
  1856. continue;
  1857. prio = roaming_prio(wpa_s, cred2, bss);
  1858. wpa_printf(MSG_DEBUG, "Interworking: roaming_prio=%u for BSS "
  1859. MACSTR " (cred=%d)", prio, MAC2STR(bss->bssid),
  1860. cred2->id);
  1861. if (prio < best_prio) {
  1862. int bh1, bh2, load1, load2, conn1, conn2;
  1863. bh1 = cred_below_min_backhaul(wpa_s, cred, selected);
  1864. load1 = cred_over_max_bss_load(wpa_s, cred, selected);
  1865. conn1 = cred_conn_capab_missing(wpa_s, cred, selected);
  1866. bh2 = cred_below_min_backhaul(wpa_s, cred2, bss);
  1867. load2 = cred_over_max_bss_load(wpa_s, cred2, bss);
  1868. conn2 = cred_conn_capab_missing(wpa_s, cred2, bss);
  1869. wpa_printf(MSG_DEBUG, "Interworking: old: %d %d %d new: %d %d %d",
  1870. bh1, load1, conn1, bh2, load2, conn2);
  1871. if (bh1 || load1 || conn1 || !(bh2 || load2 || conn2)) {
  1872. wpa_printf(MSG_DEBUG, "Interworking: Better roaming partner " MACSTR " selected", MAC2STR(bss->bssid));
  1873. best_prio = prio;
  1874. selected = bss;
  1875. }
  1876. }
  1877. }
  1878. return selected;
  1879. }
  1880. static void interworking_select_network(struct wpa_supplicant *wpa_s)
  1881. {
  1882. struct wpa_bss *bss, *selected = NULL, *selected_home = NULL;
  1883. struct wpa_bss *selected2 = NULL, *selected2_home = NULL;
  1884. unsigned int count = 0;
  1885. const char *type;
  1886. int res;
  1887. struct wpa_cred *cred, *selected_cred = NULL;
  1888. struct wpa_cred *selected_home_cred = NULL;
  1889. struct wpa_cred *selected2_cred = NULL;
  1890. struct wpa_cred *selected2_home_cred = NULL;
  1891. wpa_s->network_select = 0;
  1892. wpa_printf(MSG_DEBUG, "Interworking: Select network (auto_select=%d)",
  1893. wpa_s->auto_select);
  1894. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1895. int excluded = 0;
  1896. int bh, bss_load, conn_capab;
  1897. cred = interworking_credentials_available(wpa_s, bss,
  1898. &excluded);
  1899. if (!cred)
  1900. continue;
  1901. if (!wpa_bss_get_ie(bss, WLAN_EID_RSN)) {
  1902. /*
  1903. * We currently support only HS 2.0 networks and those
  1904. * are required to use WPA2-Enterprise.
  1905. */
  1906. wpa_printf(MSG_DEBUG, "Interworking: Credential match "
  1907. "with " MACSTR " but network does not use "
  1908. "RSN", MAC2STR(bss->bssid));
  1909. continue;
  1910. }
  1911. if (!excluded)
  1912. count++;
  1913. res = interworking_home_sp(wpa_s, bss->anqp ?
  1914. bss->anqp->domain_name : NULL);
  1915. if (res > 0)
  1916. type = "home";
  1917. else if (res == 0)
  1918. type = "roaming";
  1919. else
  1920. type = "unknown";
  1921. bh = cred_below_min_backhaul(wpa_s, cred, bss);
  1922. bss_load = cred_over_max_bss_load(wpa_s, cred, bss);
  1923. conn_capab = cred_conn_capab_missing(wpa_s, cred, bss);
  1924. wpa_msg(wpa_s, MSG_INFO, "%s" MACSTR " type=%s%s%s%s id=%d priority=%d sp_priority=%d",
  1925. excluded ? INTERWORKING_BLACKLISTED : INTERWORKING_AP,
  1926. MAC2STR(bss->bssid), type,
  1927. bh ? " below_min_backhaul=1" : "",
  1928. bss_load ? " over_max_bss_load=1" : "",
  1929. conn_capab ? " conn_capab_missing=1" : "",
  1930. cred->id, cred->priority, cred->sp_priority);
  1931. if (excluded)
  1932. continue;
  1933. if (wpa_s->auto_select ||
  1934. (wpa_s->conf->auto_interworking &&
  1935. wpa_s->auto_network_select)) {
  1936. if (bh || bss_load || conn_capab) {
  1937. if (selected2_cred == NULL ||
  1938. cred_prio_cmp(cred, selected2_cred) > 0) {
  1939. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2");
  1940. selected2 = bss;
  1941. selected2_cred = cred;
  1942. }
  1943. if (res > 0 &&
  1944. (selected2_home_cred == NULL ||
  1945. cred_prio_cmp(cred, selected2_home_cred) >
  1946. 0)) {
  1947. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected2_home");
  1948. selected2_home = bss;
  1949. selected2_home_cred = cred;
  1950. }
  1951. } else {
  1952. if (selected_cred == NULL ||
  1953. cred_prio_cmp(cred, selected_cred) > 0) {
  1954. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected");
  1955. selected = bss;
  1956. selected_cred = cred;
  1957. }
  1958. if (res > 0 &&
  1959. (selected_home_cred == NULL ||
  1960. cred_prio_cmp(cred, selected_home_cred) >
  1961. 0)) {
  1962. wpa_printf(MSG_DEBUG, "Interworking: Mark as selected_home");
  1963. selected_home = bss;
  1964. selected_home_cred = cred;
  1965. }
  1966. }
  1967. }
  1968. }
  1969. if (selected_home && selected_home != selected &&
  1970. selected_home_cred &&
  1971. (selected_cred == NULL ||
  1972. cred_prio_cmp(selected_home_cred, selected_cred) >= 0)) {
  1973. /* Prefer network operated by the Home SP */
  1974. wpa_printf(MSG_DEBUG, "Interworking: Overrided selected with selected_home");
  1975. selected = selected_home;
  1976. selected_cred = selected_home_cred;
  1977. }
  1978. if (!selected) {
  1979. if (selected2_home) {
  1980. wpa_printf(MSG_DEBUG, "Interworking: Use home BSS with BW limit mismatch since no other network could be selected");
  1981. selected = selected2_home;
  1982. selected_cred = selected2_home_cred;
  1983. } else if (selected2) {
  1984. wpa_printf(MSG_DEBUG, "Interworking: Use visited BSS with BW limit mismatch since no other network could be selected");
  1985. selected = selected2;
  1986. selected_cred = selected2_cred;
  1987. }
  1988. }
  1989. if (count == 0) {
  1990. /*
  1991. * No matching network was found based on configured
  1992. * credentials. Check whether any of the enabled network blocks
  1993. * have matching APs.
  1994. */
  1995. if (interworking_find_network_match(wpa_s)) {
  1996. wpa_printf(MSG_DEBUG, "Interworking: Possible BSS "
  1997. "match for enabled network configurations");
  1998. if (wpa_s->auto_select)
  1999. interworking_reconnect(wpa_s);
  2000. return;
  2001. }
  2002. if (wpa_s->auto_network_select) {
  2003. wpa_printf(MSG_DEBUG, "Interworking: Continue "
  2004. "scanning after ANQP fetch");
  2005. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval,
  2006. 0);
  2007. return;
  2008. }
  2009. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_NO_MATCH "No network "
  2010. "with matching credentials found");
  2011. }
  2012. if (selected) {
  2013. wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR,
  2014. MAC2STR(selected->bssid));
  2015. selected = pick_best_roaming_partner(wpa_s, selected,
  2016. selected_cred);
  2017. wpa_printf(MSG_DEBUG, "Interworking: Selected " MACSTR
  2018. " (after best roaming partner selection)",
  2019. MAC2STR(selected->bssid));
  2020. wpa_msg(wpa_s, MSG_INFO, INTERWORKING_SELECTED MACSTR,
  2021. MAC2STR(selected->bssid));
  2022. interworking_connect(wpa_s, selected);
  2023. }
  2024. }
  2025. static struct wpa_bss_anqp *
  2026. interworking_match_anqp_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  2027. {
  2028. struct wpa_bss *other;
  2029. if (is_zero_ether_addr(bss->hessid))
  2030. return NULL; /* Cannot be in the same homegenous ESS */
  2031. dl_list_for_each(other, &wpa_s->bss, struct wpa_bss, list) {
  2032. if (other == bss)
  2033. continue;
  2034. if (other->anqp == NULL)
  2035. continue;
  2036. if (other->anqp->roaming_consortium == NULL &&
  2037. other->anqp->nai_realm == NULL &&
  2038. other->anqp->anqp_3gpp == NULL &&
  2039. other->anqp->domain_name == NULL)
  2040. continue;
  2041. if (!(other->flags & WPA_BSS_ANQP_FETCH_TRIED))
  2042. continue;
  2043. if (os_memcmp(bss->hessid, other->hessid, ETH_ALEN) != 0)
  2044. continue;
  2045. if (bss->ssid_len != other->ssid_len ||
  2046. os_memcmp(bss->ssid, other->ssid, bss->ssid_len) != 0)
  2047. continue;
  2048. wpa_printf(MSG_DEBUG, "Interworking: Share ANQP data with "
  2049. "already fetched BSSID " MACSTR " and " MACSTR,
  2050. MAC2STR(other->bssid), MAC2STR(bss->bssid));
  2051. other->anqp->users++;
  2052. return other->anqp;
  2053. }
  2054. return NULL;
  2055. }
  2056. static void interworking_next_anqp_fetch(struct wpa_supplicant *wpa_s)
  2057. {
  2058. struct wpa_bss *bss;
  2059. int found = 0;
  2060. const u8 *ie;
  2061. wpa_printf(MSG_DEBUG, "Interworking: next_anqp_fetch - "
  2062. "fetch_anqp_in_progress=%d fetch_osu_icon_in_progress=%d",
  2063. wpa_s->fetch_anqp_in_progress,
  2064. wpa_s->fetch_osu_icon_in_progress);
  2065. if (eloop_terminated() || !wpa_s->fetch_anqp_in_progress) {
  2066. wpa_printf(MSG_DEBUG, "Interworking: Stop next-ANQP-fetch");
  2067. return;
  2068. }
  2069. if (wpa_s->fetch_osu_icon_in_progress) {
  2070. wpa_printf(MSG_DEBUG, "Interworking: Next icon (in progress)");
  2071. hs20_next_osu_icon(wpa_s);
  2072. return;
  2073. }
  2074. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2075. if (!(bss->caps & IEEE80211_CAP_ESS))
  2076. continue;
  2077. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB);
  2078. if (ie == NULL || ie[1] < 4 || !(ie[5] & 0x80))
  2079. continue; /* AP does not support Interworking */
  2080. if (disallowed_bssid(wpa_s, bss->bssid) ||
  2081. disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len))
  2082. continue; /* Disallowed BSS */
  2083. if (!(bss->flags & WPA_BSS_ANQP_FETCH_TRIED)) {
  2084. if (bss->anqp == NULL) {
  2085. bss->anqp = interworking_match_anqp_info(wpa_s,
  2086. bss);
  2087. if (bss->anqp) {
  2088. /* Shared data already fetched */
  2089. continue;
  2090. }
  2091. bss->anqp = wpa_bss_anqp_alloc();
  2092. if (bss->anqp == NULL)
  2093. break;
  2094. }
  2095. found++;
  2096. bss->flags |= WPA_BSS_ANQP_FETCH_TRIED;
  2097. wpa_msg(wpa_s, MSG_INFO, "Starting ANQP fetch for "
  2098. MACSTR, MAC2STR(bss->bssid));
  2099. interworking_anqp_send_req(wpa_s, bss);
  2100. break;
  2101. }
  2102. }
  2103. if (found == 0) {
  2104. if (wpa_s->fetch_osu_info) {
  2105. if (wpa_s->num_prov_found == 0 &&
  2106. wpa_s->num_osu_scans < 3) {
  2107. wpa_printf(MSG_DEBUG, "HS 2.0: No OSU providers seen - try to scan again");
  2108. hs20_start_osu_scan(wpa_s);
  2109. return;
  2110. }
  2111. wpa_printf(MSG_DEBUG, "Interworking: Next icon");
  2112. hs20_osu_icon_fetch(wpa_s);
  2113. return;
  2114. }
  2115. wpa_msg(wpa_s, MSG_INFO, "ANQP fetch completed");
  2116. wpa_s->fetch_anqp_in_progress = 0;
  2117. if (wpa_s->network_select)
  2118. interworking_select_network(wpa_s);
  2119. }
  2120. }
  2121. void interworking_start_fetch_anqp(struct wpa_supplicant *wpa_s)
  2122. {
  2123. struct wpa_bss *bss;
  2124. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list)
  2125. bss->flags &= ~WPA_BSS_ANQP_FETCH_TRIED;
  2126. wpa_s->fetch_anqp_in_progress = 1;
  2127. interworking_next_anqp_fetch(wpa_s);
  2128. }
  2129. int interworking_fetch_anqp(struct wpa_supplicant *wpa_s)
  2130. {
  2131. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select)
  2132. return 0;
  2133. wpa_s->network_select = 0;
  2134. wpa_s->fetch_all_anqp = 1;
  2135. wpa_s->fetch_osu_info = 0;
  2136. interworking_start_fetch_anqp(wpa_s);
  2137. return 0;
  2138. }
  2139. void interworking_stop_fetch_anqp(struct wpa_supplicant *wpa_s)
  2140. {
  2141. if (!wpa_s->fetch_anqp_in_progress)
  2142. return;
  2143. wpa_s->fetch_anqp_in_progress = 0;
  2144. }
  2145. int anqp_send_req(struct wpa_supplicant *wpa_s, const u8 *dst,
  2146. u16 info_ids[], size_t num_ids)
  2147. {
  2148. struct wpabuf *buf;
  2149. int ret = 0;
  2150. int freq;
  2151. struct wpa_bss *bss;
  2152. int res;
  2153. freq = wpa_s->assoc_freq;
  2154. bss = wpa_bss_get_bssid(wpa_s, dst);
  2155. if (bss) {
  2156. wpa_bss_anqp_unshare_alloc(bss);
  2157. freq = bss->freq;
  2158. }
  2159. if (freq <= 0)
  2160. return -1;
  2161. wpa_printf(MSG_DEBUG, "ANQP: Query Request to " MACSTR " for %u id(s)",
  2162. MAC2STR(dst), (unsigned int) num_ids);
  2163. buf = anqp_build_req(info_ids, num_ids, NULL);
  2164. if (buf == NULL)
  2165. return -1;
  2166. res = gas_query_req(wpa_s->gas, dst, freq, buf, anqp_resp_cb, wpa_s);
  2167. if (res < 0) {
  2168. wpa_printf(MSG_DEBUG, "ANQP: Failed to send Query Request");
  2169. wpabuf_free(buf);
  2170. ret = -1;
  2171. } else
  2172. wpa_printf(MSG_DEBUG, "ANQP: Query started with dialog token "
  2173. "%u", res);
  2174. return ret;
  2175. }
  2176. static void interworking_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  2177. struct wpa_bss *bss, const u8 *sa,
  2178. u16 info_id,
  2179. const u8 *data, size_t slen)
  2180. {
  2181. const u8 *pos = data;
  2182. struct wpa_bss_anqp *anqp = NULL;
  2183. #ifdef CONFIG_HS20
  2184. u8 type;
  2185. #endif /* CONFIG_HS20 */
  2186. if (bss)
  2187. anqp = bss->anqp;
  2188. switch (info_id) {
  2189. case ANQP_CAPABILITY_LIST:
  2190. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2191. " ANQP Capability list", MAC2STR(sa));
  2192. break;
  2193. case ANQP_VENUE_NAME:
  2194. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2195. " Venue Name", MAC2STR(sa));
  2196. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Venue Name", pos, slen);
  2197. if (anqp) {
  2198. wpabuf_free(anqp->venue_name);
  2199. anqp->venue_name = wpabuf_alloc_copy(pos, slen);
  2200. }
  2201. break;
  2202. case ANQP_NETWORK_AUTH_TYPE:
  2203. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2204. " Network Authentication Type information",
  2205. MAC2STR(sa));
  2206. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Network Authentication "
  2207. "Type", pos, slen);
  2208. if (anqp) {
  2209. wpabuf_free(anqp->network_auth_type);
  2210. anqp->network_auth_type = wpabuf_alloc_copy(pos, slen);
  2211. }
  2212. break;
  2213. case ANQP_ROAMING_CONSORTIUM:
  2214. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2215. " Roaming Consortium list", MAC2STR(sa));
  2216. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: Roaming Consortium",
  2217. pos, slen);
  2218. if (anqp) {
  2219. wpabuf_free(anqp->roaming_consortium);
  2220. anqp->roaming_consortium = wpabuf_alloc_copy(pos, slen);
  2221. }
  2222. break;
  2223. case ANQP_IP_ADDR_TYPE_AVAILABILITY:
  2224. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2225. " IP Address Type Availability information",
  2226. MAC2STR(sa));
  2227. wpa_hexdump(MSG_MSGDUMP, "ANQP: IP Address Availability",
  2228. pos, slen);
  2229. if (anqp) {
  2230. wpabuf_free(anqp->ip_addr_type_availability);
  2231. anqp->ip_addr_type_availability =
  2232. wpabuf_alloc_copy(pos, slen);
  2233. }
  2234. break;
  2235. case ANQP_NAI_REALM:
  2236. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2237. " NAI Realm list", MAC2STR(sa));
  2238. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: NAI Realm", pos, slen);
  2239. if (anqp) {
  2240. wpabuf_free(anqp->nai_realm);
  2241. anqp->nai_realm = wpabuf_alloc_copy(pos, slen);
  2242. }
  2243. break;
  2244. case ANQP_3GPP_CELLULAR_NETWORK:
  2245. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2246. " 3GPP Cellular Network information", MAC2STR(sa));
  2247. wpa_hexdump_ascii(MSG_DEBUG, "ANQP: 3GPP Cellular Network",
  2248. pos, slen);
  2249. if (anqp) {
  2250. wpabuf_free(anqp->anqp_3gpp);
  2251. anqp->anqp_3gpp = wpabuf_alloc_copy(pos, slen);
  2252. }
  2253. break;
  2254. case ANQP_DOMAIN_NAME:
  2255. wpa_msg(wpa_s, MSG_INFO, "RX-ANQP " MACSTR
  2256. " Domain Name list", MAC2STR(sa));
  2257. wpa_hexdump_ascii(MSG_MSGDUMP, "ANQP: Domain Name", pos, slen);
  2258. if (anqp) {
  2259. wpabuf_free(anqp->domain_name);
  2260. anqp->domain_name = wpabuf_alloc_copy(pos, slen);
  2261. }
  2262. break;
  2263. case ANQP_VENDOR_SPECIFIC:
  2264. if (slen < 3)
  2265. return;
  2266. switch (WPA_GET_BE24(pos)) {
  2267. #ifdef CONFIG_HS20
  2268. case OUI_WFA:
  2269. pos += 3;
  2270. slen -= 3;
  2271. if (slen < 1)
  2272. return;
  2273. type = *pos++;
  2274. slen--;
  2275. switch (type) {
  2276. case HS20_ANQP_OUI_TYPE:
  2277. hs20_parse_rx_hs20_anqp_resp(wpa_s, sa, pos,
  2278. slen);
  2279. break;
  2280. default:
  2281. wpa_printf(MSG_DEBUG, "HS20: Unsupported ANQP "
  2282. "vendor type %u", type);
  2283. break;
  2284. }
  2285. break;
  2286. #endif /* CONFIG_HS20 */
  2287. default:
  2288. wpa_printf(MSG_DEBUG, "Interworking: Unsupported "
  2289. "vendor-specific ANQP OUI %06x",
  2290. WPA_GET_BE24(pos));
  2291. return;
  2292. }
  2293. break;
  2294. default:
  2295. wpa_printf(MSG_DEBUG, "Interworking: Unsupported ANQP Info ID "
  2296. "%u", info_id);
  2297. break;
  2298. }
  2299. }
  2300. void anqp_resp_cb(void *ctx, const u8 *dst, u8 dialog_token,
  2301. enum gas_query_result result,
  2302. const struct wpabuf *adv_proto,
  2303. const struct wpabuf *resp, u16 status_code)
  2304. {
  2305. struct wpa_supplicant *wpa_s = ctx;
  2306. const u8 *pos;
  2307. const u8 *end;
  2308. u16 info_id;
  2309. u16 slen;
  2310. struct wpa_bss *bss = NULL, *tmp;
  2311. wpa_printf(MSG_DEBUG, "Interworking: anqp_resp_cb dst=" MACSTR
  2312. " dialog_token=%u result=%d status_code=%u",
  2313. MAC2STR(dst), dialog_token, result, status_code);
  2314. if (result != GAS_QUERY_SUCCESS) {
  2315. if (wpa_s->fetch_osu_icon_in_progress)
  2316. hs20_icon_fetch_failed(wpa_s);
  2317. return;
  2318. }
  2319. pos = wpabuf_head(adv_proto);
  2320. if (wpabuf_len(adv_proto) < 4 || pos[0] != WLAN_EID_ADV_PROTO ||
  2321. pos[1] < 2 || pos[3] != ACCESS_NETWORK_QUERY_PROTOCOL) {
  2322. wpa_printf(MSG_DEBUG, "ANQP: Unexpected Advertisement "
  2323. "Protocol in response");
  2324. if (wpa_s->fetch_osu_icon_in_progress)
  2325. hs20_icon_fetch_failed(wpa_s);
  2326. return;
  2327. }
  2328. /*
  2329. * If possible, select the BSS entry based on which BSS entry was used
  2330. * for the request. This can help in cases where multiple BSS entries
  2331. * may exist for the same AP.
  2332. */
  2333. dl_list_for_each_reverse(tmp, &wpa_s->bss, struct wpa_bss, list) {
  2334. if (tmp == wpa_s->interworking_gas_bss &&
  2335. os_memcmp(tmp->bssid, dst, ETH_ALEN) == 0) {
  2336. bss = tmp;
  2337. break;
  2338. }
  2339. }
  2340. if (bss == NULL)
  2341. bss = wpa_bss_get_bssid(wpa_s, dst);
  2342. pos = wpabuf_head(resp);
  2343. end = pos + wpabuf_len(resp);
  2344. while (pos < end) {
  2345. if (pos + 4 > end) {
  2346. wpa_printf(MSG_DEBUG, "ANQP: Invalid element");
  2347. break;
  2348. }
  2349. info_id = WPA_GET_LE16(pos);
  2350. pos += 2;
  2351. slen = WPA_GET_LE16(pos);
  2352. pos += 2;
  2353. if (pos + slen > end) {
  2354. wpa_printf(MSG_DEBUG, "ANQP: Invalid element length "
  2355. "for Info ID %u", info_id);
  2356. break;
  2357. }
  2358. interworking_parse_rx_anqp_resp(wpa_s, bss, dst, info_id, pos,
  2359. slen);
  2360. pos += slen;
  2361. }
  2362. hs20_notify_parse_done(wpa_s);
  2363. }
  2364. static void interworking_scan_res_handler(struct wpa_supplicant *wpa_s,
  2365. struct wpa_scan_results *scan_res)
  2366. {
  2367. wpa_printf(MSG_DEBUG, "Interworking: Scan results available - start "
  2368. "ANQP fetch");
  2369. interworking_start_fetch_anqp(wpa_s);
  2370. }
  2371. int interworking_select(struct wpa_supplicant *wpa_s, int auto_select,
  2372. int *freqs)
  2373. {
  2374. interworking_stop_fetch_anqp(wpa_s);
  2375. wpa_s->network_select = 1;
  2376. wpa_s->auto_network_select = 0;
  2377. wpa_s->auto_select = !!auto_select;
  2378. wpa_s->fetch_all_anqp = 0;
  2379. wpa_s->fetch_osu_info = 0;
  2380. wpa_printf(MSG_DEBUG, "Interworking: Start scan for network "
  2381. "selection");
  2382. wpa_s->scan_res_handler = interworking_scan_res_handler;
  2383. wpa_s->normal_scans = 0;
  2384. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2385. os_free(wpa_s->manual_scan_freqs);
  2386. wpa_s->manual_scan_freqs = freqs;
  2387. wpa_s->after_wps = 0;
  2388. wpa_s->known_wps_freq = 0;
  2389. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2390. return 0;
  2391. }
  2392. static void gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  2393. enum gas_query_result result,
  2394. const struct wpabuf *adv_proto,
  2395. const struct wpabuf *resp, u16 status_code)
  2396. {
  2397. struct wpa_supplicant *wpa_s = ctx;
  2398. struct wpabuf *n;
  2399. wpa_msg(wpa_s, MSG_INFO, GAS_RESPONSE_INFO "addr=" MACSTR
  2400. " dialog_token=%d status_code=%d resp_len=%d",
  2401. MAC2STR(addr), dialog_token, status_code,
  2402. resp ? (int) wpabuf_len(resp) : -1);
  2403. if (!resp)
  2404. return;
  2405. n = wpabuf_dup(resp);
  2406. if (n == NULL)
  2407. return;
  2408. wpabuf_free(wpa_s->prev_gas_resp);
  2409. wpa_s->prev_gas_resp = wpa_s->last_gas_resp;
  2410. os_memcpy(wpa_s->prev_gas_addr, wpa_s->last_gas_addr, ETH_ALEN);
  2411. wpa_s->prev_gas_dialog_token = wpa_s->last_gas_dialog_token;
  2412. wpa_s->last_gas_resp = n;
  2413. os_memcpy(wpa_s->last_gas_addr, addr, ETH_ALEN);
  2414. wpa_s->last_gas_dialog_token = dialog_token;
  2415. }
  2416. int gas_send_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  2417. const struct wpabuf *adv_proto,
  2418. const struct wpabuf *query)
  2419. {
  2420. struct wpabuf *buf;
  2421. int ret = 0;
  2422. int freq;
  2423. struct wpa_bss *bss;
  2424. int res;
  2425. size_t len;
  2426. u8 query_resp_len_limit = 0, pame_bi = 0;
  2427. freq = wpa_s->assoc_freq;
  2428. bss = wpa_bss_get_bssid(wpa_s, dst);
  2429. if (bss)
  2430. freq = bss->freq;
  2431. if (freq <= 0)
  2432. return -1;
  2433. wpa_printf(MSG_DEBUG, "GAS request to " MACSTR " (freq %d MHz)",
  2434. MAC2STR(dst), freq);
  2435. wpa_hexdump_buf(MSG_DEBUG, "Advertisement Protocol ID", adv_proto);
  2436. wpa_hexdump_buf(MSG_DEBUG, "GAS Query", query);
  2437. len = 3 + wpabuf_len(adv_proto) + 2;
  2438. if (query)
  2439. len += wpabuf_len(query);
  2440. buf = gas_build_initial_req(0, len);
  2441. if (buf == NULL)
  2442. return -1;
  2443. /* Advertisement Protocol IE */
  2444. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  2445. wpabuf_put_u8(buf, 1 + wpabuf_len(adv_proto)); /* Length */
  2446. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  2447. (pame_bi ? 0x80 : 0));
  2448. wpabuf_put_buf(buf, adv_proto);
  2449. /* GAS Query */
  2450. if (query) {
  2451. wpabuf_put_le16(buf, wpabuf_len(query));
  2452. wpabuf_put_buf(buf, query);
  2453. } else
  2454. wpabuf_put_le16(buf, 0);
  2455. res = gas_query_req(wpa_s->gas, dst, freq, buf, gas_resp_cb, wpa_s);
  2456. if (res < 0) {
  2457. wpa_printf(MSG_DEBUG, "GAS: Failed to send Query Request");
  2458. wpabuf_free(buf);
  2459. ret = -1;
  2460. } else
  2461. wpa_printf(MSG_DEBUG, "GAS: Query started with dialog token "
  2462. "%u", res);
  2463. return ret;
  2464. }