interworking.c 79 KB

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