interworking.c 74 KB

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