interworking.c 79 KB

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