interworking.c 74 KB

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