interworking.c 71 KB

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