interworking.c 74 KB

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