interworking.c 76 KB

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