interworking.c 74 KB

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