interworking.c 74 KB

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