interworking.c 75 KB

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