dpp_supplicant.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599
  1. /*
  2. * wpa_supplicant - DPP
  3. * Copyright (c) 2017, Qualcomm Atheros, Inc.
  4. * Copyright (c) 2018, The Linux Foundation
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "utils/includes.h"
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "common/dpp.h"
  13. #include "common/gas.h"
  14. #include "common/gas_server.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "rsn_supp/pmksa_cache.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "config.h"
  19. #include "driver_i.h"
  20. #include "offchannel.h"
  21. #include "gas_query.h"
  22. #include "bss.h"
  23. #include "scan.h"
  24. #include "notify.h"
  25. #include "dpp_supplicant.h"
  26. static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
  27. unsigned int freq);
  28. static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx);
  29. static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator);
  30. static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
  31. unsigned int freq, const u8 *dst,
  32. const u8 *src, const u8 *bssid,
  33. const u8 *data, size_t data_len,
  34. enum offchannel_send_action_result result);
  35. static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx);
  36. static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s);
  37. static void
  38. wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
  39. unsigned int freq, const u8 *dst,
  40. const u8 *src, const u8 *bssid,
  41. const u8 *data, size_t data_len,
  42. enum offchannel_send_action_result result);
  43. static const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  44. /* Use a hardcoded Transaction ID 1 in Peer Discovery frames since there is only
  45. * a single transaction in progress at any point in time. */
  46. static const u8 TRANSACTION_ID = 1;
  47. static struct dpp_configurator *
  48. dpp_configurator_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  49. {
  50. struct dpp_configurator *conf;
  51. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  52. struct dpp_configurator, list) {
  53. if (conf->id == id)
  54. return conf;
  55. }
  56. return NULL;
  57. }
  58. static unsigned int wpas_dpp_next_id(struct wpa_supplicant *wpa_s)
  59. {
  60. struct dpp_bootstrap_info *bi;
  61. unsigned int max_id = 0;
  62. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  63. list) {
  64. if (bi->id > max_id)
  65. max_id = bi->id;
  66. }
  67. return max_id + 1;
  68. }
  69. /**
  70. * wpas_dpp_qr_code - Parse and add DPP bootstrapping info from a QR Code
  71. * @wpa_s: Pointer to wpa_supplicant data
  72. * @cmd: DPP URI read from a QR Code
  73. * Returns: Identifier of the stored info or -1 on failure
  74. */
  75. int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
  76. {
  77. struct dpp_bootstrap_info *bi;
  78. struct dpp_authentication *auth = wpa_s->dpp_auth;
  79. bi = dpp_parse_qr_code(cmd);
  80. if (!bi)
  81. return -1;
  82. bi->id = wpas_dpp_next_id(wpa_s);
  83. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  84. if (auth && auth->response_pending &&
  85. dpp_notify_new_qr_code(auth, bi) == 1) {
  86. wpa_printf(MSG_DEBUG,
  87. "DPP: Sending out pending authentication response");
  88. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  89. " freq=%u type=%d",
  90. MAC2STR(auth->peer_mac_addr), auth->curr_freq,
  91. DPP_PA_AUTHENTICATION_RESP);
  92. offchannel_send_action(wpa_s, auth->curr_freq,
  93. auth->peer_mac_addr, wpa_s->own_addr,
  94. broadcast,
  95. wpabuf_head(auth->resp_msg),
  96. wpabuf_len(auth->resp_msg),
  97. 500, wpas_dpp_tx_status, 0);
  98. }
  99. return bi->id;
  100. }
  101. static char * get_param(const char *cmd, const char *param)
  102. {
  103. const char *pos, *end;
  104. char *val;
  105. size_t len;
  106. pos = os_strstr(cmd, param);
  107. if (!pos)
  108. return NULL;
  109. pos += os_strlen(param);
  110. end = os_strchr(pos, ' ');
  111. if (end)
  112. len = end - pos;
  113. else
  114. len = os_strlen(pos);
  115. val = os_malloc(len + 1);
  116. if (!val)
  117. return NULL;
  118. os_memcpy(val, pos, len);
  119. val[len] = '\0';
  120. return val;
  121. }
  122. int wpas_dpp_bootstrap_gen(struct wpa_supplicant *wpa_s, const char *cmd)
  123. {
  124. char *chan = NULL, *mac = NULL, *info = NULL, *pk = NULL, *curve = NULL;
  125. char *key = NULL;
  126. u8 *privkey = NULL;
  127. size_t privkey_len = 0;
  128. size_t len;
  129. int ret = -1;
  130. struct dpp_bootstrap_info *bi;
  131. bi = os_zalloc(sizeof(*bi));
  132. if (!bi)
  133. goto fail;
  134. if (os_strstr(cmd, "type=qrcode"))
  135. bi->type = DPP_BOOTSTRAP_QR_CODE;
  136. else if (os_strstr(cmd, "type=pkex"))
  137. bi->type = DPP_BOOTSTRAP_PKEX;
  138. else
  139. goto fail;
  140. chan = get_param(cmd, " chan=");
  141. mac = get_param(cmd, " mac=");
  142. info = get_param(cmd, " info=");
  143. curve = get_param(cmd, " curve=");
  144. key = get_param(cmd, " key=");
  145. if (key) {
  146. privkey_len = os_strlen(key) / 2;
  147. privkey = os_malloc(privkey_len);
  148. if (!privkey ||
  149. hexstr2bin(key, privkey, privkey_len) < 0)
  150. goto fail;
  151. }
  152. pk = dpp_keygen(bi, curve, privkey, privkey_len);
  153. if (!pk)
  154. goto fail;
  155. len = 4; /* "DPP:" */
  156. if (chan) {
  157. if (dpp_parse_uri_chan_list(bi, chan) < 0)
  158. goto fail;
  159. len += 3 + os_strlen(chan); /* C:...; */
  160. }
  161. if (mac) {
  162. if (dpp_parse_uri_mac(bi, mac) < 0)
  163. goto fail;
  164. len += 3 + os_strlen(mac); /* M:...; */
  165. }
  166. if (info) {
  167. if (dpp_parse_uri_info(bi, info) < 0)
  168. goto fail;
  169. len += 3 + os_strlen(info); /* I:...; */
  170. }
  171. len += 4 + os_strlen(pk);
  172. bi->uri = os_malloc(len + 1);
  173. if (!bi->uri)
  174. goto fail;
  175. os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%s%sK:%s;;",
  176. chan ? "C:" : "", chan ? chan : "", chan ? ";" : "",
  177. mac ? "M:" : "", mac ? mac : "", mac ? ";" : "",
  178. info ? "I:" : "", info ? info : "", info ? ";" : "",
  179. pk);
  180. bi->id = wpas_dpp_next_id(wpa_s);
  181. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  182. ret = bi->id;
  183. bi = NULL;
  184. fail:
  185. os_free(curve);
  186. os_free(pk);
  187. os_free(chan);
  188. os_free(mac);
  189. os_free(info);
  190. str_clear_free(key);
  191. bin_clear_free(privkey, privkey_len);
  192. dpp_bootstrap_info_free(bi);
  193. return ret;
  194. }
  195. static struct dpp_bootstrap_info *
  196. dpp_bootstrap_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  197. {
  198. struct dpp_bootstrap_info *bi;
  199. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  200. list) {
  201. if (bi->id == id)
  202. return bi;
  203. }
  204. return NULL;
  205. }
  206. static int dpp_bootstrap_del(struct wpa_supplicant *wpa_s, unsigned int id)
  207. {
  208. struct dpp_bootstrap_info *bi, *tmp;
  209. int found = 0;
  210. dl_list_for_each_safe(bi, tmp, &wpa_s->dpp_bootstrap,
  211. struct dpp_bootstrap_info, list) {
  212. if (id && bi->id != id)
  213. continue;
  214. found = 1;
  215. dl_list_del(&bi->list);
  216. dpp_bootstrap_info_free(bi);
  217. }
  218. if (id == 0)
  219. return 0; /* flush succeeds regardless of entries found */
  220. return found ? 0 : -1;
  221. }
  222. int wpas_dpp_bootstrap_remove(struct wpa_supplicant *wpa_s, const char *id)
  223. {
  224. unsigned int id_val;
  225. if (os_strcmp(id, "*") == 0) {
  226. id_val = 0;
  227. } else {
  228. id_val = atoi(id);
  229. if (id_val == 0)
  230. return -1;
  231. }
  232. return dpp_bootstrap_del(wpa_s, id_val);
  233. }
  234. const char * wpas_dpp_bootstrap_get_uri(struct wpa_supplicant *wpa_s,
  235. unsigned int id)
  236. {
  237. struct dpp_bootstrap_info *bi;
  238. bi = dpp_bootstrap_get_id(wpa_s, id);
  239. if (!bi)
  240. return NULL;
  241. return bi->uri;
  242. }
  243. int wpas_dpp_bootstrap_info(struct wpa_supplicant *wpa_s, int id,
  244. char *reply, int reply_size)
  245. {
  246. struct dpp_bootstrap_info *bi;
  247. bi = dpp_bootstrap_get_id(wpa_s, id);
  248. if (!bi)
  249. return -1;
  250. return os_snprintf(reply, reply_size, "type=%s\n"
  251. "mac_addr=" MACSTR "\n"
  252. "info=%s\n"
  253. "num_freq=%u\n"
  254. "curve=%s\n",
  255. dpp_bootstrap_type_txt(bi->type),
  256. MAC2STR(bi->mac_addr),
  257. bi->info ? bi->info : "",
  258. bi->num_freq,
  259. bi->curve->name);
  260. }
  261. static void wpas_dpp_auth_resp_retry_timeout(void *eloop_ctx, void *timeout_ctx)
  262. {
  263. struct wpa_supplicant *wpa_s = eloop_ctx;
  264. struct dpp_authentication *auth = wpa_s->dpp_auth;
  265. if (!auth || !auth->resp_msg)
  266. return;
  267. wpa_printf(MSG_DEBUG,
  268. "DPP: Retry Authentication Response after timeout");
  269. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  270. " freq=%u type=%d",
  271. MAC2STR(auth->peer_mac_addr), auth->curr_freq,
  272. DPP_PA_AUTHENTICATION_RESP);
  273. offchannel_send_action(wpa_s, auth->curr_freq, auth->peer_mac_addr,
  274. wpa_s->own_addr, broadcast,
  275. wpabuf_head(auth->resp_msg),
  276. wpabuf_len(auth->resp_msg),
  277. 500, wpas_dpp_tx_status, 0);
  278. }
  279. static void wpas_dpp_auth_resp_retry(struct wpa_supplicant *wpa_s)
  280. {
  281. struct dpp_authentication *auth = wpa_s->dpp_auth;
  282. unsigned int wait_time, max_tries;
  283. if (!auth || !auth->resp_msg)
  284. return;
  285. if (wpa_s->dpp_resp_max_tries)
  286. max_tries = wpa_s->dpp_resp_max_tries;
  287. else
  288. max_tries = 5;
  289. auth->auth_resp_tries++;
  290. if (auth->auth_resp_tries >= max_tries) {
  291. wpa_printf(MSG_INFO, "DPP: No confirm received from initiator - stopping exchange");
  292. offchannel_send_action_done(wpa_s);
  293. dpp_auth_deinit(wpa_s->dpp_auth);
  294. wpa_s->dpp_auth = NULL;
  295. return;
  296. }
  297. if (wpa_s->dpp_resp_retry_time)
  298. wait_time = wpa_s->dpp_resp_retry_time;
  299. else
  300. wait_time = 1000;
  301. wpa_printf(MSG_DEBUG,
  302. "DPP: Schedule retransmission of Authentication Response frame in %u ms",
  303. wait_time);
  304. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  305. eloop_register_timeout(wait_time / 1000,
  306. (wait_time % 1000) * 1000,
  307. wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  308. }
  309. static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
  310. unsigned int freq, const u8 *dst,
  311. const u8 *src, const u8 *bssid,
  312. const u8 *data, size_t data_len,
  313. enum offchannel_send_action_result result)
  314. {
  315. const char *res_txt;
  316. struct dpp_authentication *auth = wpa_s->dpp_auth;
  317. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  318. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  319. "FAILED");
  320. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  321. " result=%s", freq, MAC2STR(dst), res_txt);
  322. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  323. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  324. if (!wpa_s->dpp_auth) {
  325. wpa_printf(MSG_DEBUG,
  326. "DPP: Ignore TX status since there is no ongoing authentication exchange");
  327. return;
  328. }
  329. if (wpa_s->dpp_auth->remove_on_tx_status) {
  330. wpa_printf(MSG_DEBUG,
  331. "DPP: Terminate authentication exchange due to an earlier error");
  332. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  333. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  334. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
  335. NULL);
  336. offchannel_send_action_done(wpa_s);
  337. dpp_auth_deinit(wpa_s->dpp_auth);
  338. wpa_s->dpp_auth = NULL;
  339. return;
  340. }
  341. if (wpa_s->dpp_auth_ok_on_ack)
  342. wpas_dpp_auth_success(wpa_s, 1);
  343. if (!is_broadcast_ether_addr(dst) &&
  344. result != OFFCHANNEL_SEND_ACTION_SUCCESS) {
  345. wpa_printf(MSG_DEBUG,
  346. "DPP: Unicast DPP Action frame was not ACKed");
  347. if (auth->waiting_auth_resp) {
  348. /* In case of DPP Authentication Request frame, move to
  349. * the next channel immediately. */
  350. offchannel_send_action_done(wpa_s);
  351. wpas_dpp_auth_init_next(wpa_s);
  352. return;
  353. }
  354. if (auth->waiting_auth_conf) {
  355. wpas_dpp_auth_resp_retry(wpa_s);
  356. return;
  357. }
  358. }
  359. if (!is_broadcast_ether_addr(dst) && auth->waiting_auth_resp &&
  360. result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
  361. /* Allow timeout handling to stop iteration if no response is
  362. * received from a peer that has ACKed a request. */
  363. auth->auth_req_ack = 1;
  364. }
  365. if (!wpa_s->dpp_auth_ok_on_ack && wpa_s->dpp_auth->neg_freq > 0 &&
  366. wpa_s->dpp_auth->curr_freq != wpa_s->dpp_auth->neg_freq) {
  367. wpa_printf(MSG_DEBUG,
  368. "DPP: Move from curr_freq %u MHz to neg_freq %u MHz for response",
  369. wpa_s->dpp_auth->curr_freq,
  370. wpa_s->dpp_auth->neg_freq);
  371. offchannel_send_action_done(wpa_s);
  372. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->neg_freq);
  373. }
  374. if (wpa_s->dpp_auth_ok_on_ack)
  375. wpa_s->dpp_auth_ok_on_ack = 0;
  376. }
  377. static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
  378. {
  379. struct wpa_supplicant *wpa_s = eloop_ctx;
  380. struct dpp_authentication *auth = wpa_s->dpp_auth;
  381. unsigned int freq;
  382. struct os_reltime now, diff;
  383. unsigned int wait_time, diff_ms;
  384. if (!auth || !auth->waiting_auth_resp)
  385. return;
  386. wait_time = wpa_s->dpp_resp_wait_time ?
  387. wpa_s->dpp_resp_wait_time : 2000;
  388. os_get_reltime(&now);
  389. os_reltime_sub(&now, &wpa_s->dpp_last_init, &diff);
  390. diff_ms = diff.sec * 1000 + diff.usec / 1000;
  391. wpa_printf(MSG_DEBUG,
  392. "DPP: Reply wait timeout - wait_time=%u diff_ms=%u",
  393. wait_time, diff_ms);
  394. if (auth->auth_req_ack && diff_ms >= wait_time) {
  395. /* Peer ACK'ed Authentication Request frame, but did not reply
  396. * with Authentication Response frame within two seconds. */
  397. wpa_printf(MSG_INFO,
  398. "DPP: No response received from responder - stopping initiation attempt");
  399. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
  400. offchannel_send_action_done(wpa_s);
  401. wpas_dpp_listen_stop(wpa_s);
  402. dpp_auth_deinit(auth);
  403. wpa_s->dpp_auth = NULL;
  404. return;
  405. }
  406. if (diff_ms >= wait_time) {
  407. /* Authentication Request frame was not ACK'ed and no reply
  408. * was receiving within two seconds. */
  409. wpa_printf(MSG_DEBUG,
  410. "DPP: Continue Initiator channel iteration");
  411. offchannel_send_action_done(wpa_s);
  412. wpas_dpp_listen_stop(wpa_s);
  413. wpas_dpp_auth_init_next(wpa_s);
  414. return;
  415. }
  416. /* Driver did not support 2000 ms long wait_time with TX command, so
  417. * schedule listen operation to continue waiting for the response.
  418. *
  419. * DPP listen operations continue until stopped, so simply schedule a
  420. * new call to this function at the point when the two second reply
  421. * wait has expired. */
  422. wait_time -= diff_ms;
  423. freq = auth->curr_freq;
  424. if (auth->neg_freq > 0)
  425. freq = auth->neg_freq;
  426. wpa_printf(MSG_DEBUG,
  427. "DPP: Continue reply wait on channel %u MHz for %u ms",
  428. freq, wait_time);
  429. wpa_s->dpp_in_response_listen = 1;
  430. wpas_dpp_listen_start(wpa_s, freq);
  431. eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
  432. wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  433. }
  434. static void wpas_dpp_set_testing_options(struct wpa_supplicant *wpa_s,
  435. struct dpp_authentication *auth)
  436. {
  437. #ifdef CONFIG_TESTING_OPTIONS
  438. if (wpa_s->dpp_config_obj_override)
  439. auth->config_obj_override =
  440. os_strdup(wpa_s->dpp_config_obj_override);
  441. if (wpa_s->dpp_discovery_override)
  442. auth->discovery_override =
  443. os_strdup(wpa_s->dpp_discovery_override);
  444. if (wpa_s->dpp_groups_override)
  445. auth->groups_override =
  446. os_strdup(wpa_s->dpp_groups_override);
  447. auth->ignore_netaccesskey_mismatch =
  448. wpa_s->dpp_ignore_netaccesskey_mismatch;
  449. #endif /* CONFIG_TESTING_OPTIONS */
  450. }
  451. static void wpas_dpp_set_configurator(struct wpa_supplicant *wpa_s,
  452. struct dpp_authentication *auth,
  453. const char *cmd)
  454. {
  455. const char *pos, *end;
  456. struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
  457. struct dpp_configurator *conf = NULL;
  458. u8 ssid[32] = { "test" };
  459. size_t ssid_len = 4;
  460. char pass[64] = { };
  461. size_t pass_len = 0;
  462. u8 psk[PMK_LEN];
  463. int psk_set = 0;
  464. if (!cmd)
  465. return;
  466. wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
  467. pos = os_strstr(cmd, " ssid=");
  468. if (pos) {
  469. pos += 6;
  470. end = os_strchr(pos, ' ');
  471. ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
  472. ssid_len /= 2;
  473. if (ssid_len > sizeof(ssid) ||
  474. hexstr2bin(pos, ssid, ssid_len) < 0)
  475. goto fail;
  476. }
  477. pos = os_strstr(cmd, " pass=");
  478. if (pos) {
  479. pos += 6;
  480. end = os_strchr(pos, ' ');
  481. pass_len = end ? (size_t) (end - pos) : os_strlen(pos);
  482. pass_len /= 2;
  483. if (pass_len > sizeof(pass) - 1 || pass_len < 8 ||
  484. hexstr2bin(pos, (u8 *) pass, pass_len) < 0)
  485. goto fail;
  486. }
  487. pos = os_strstr(cmd, " psk=");
  488. if (pos) {
  489. pos += 5;
  490. if (hexstr2bin(pos, psk, PMK_LEN) < 0)
  491. goto fail;
  492. psk_set = 1;
  493. }
  494. if (os_strstr(cmd, " conf=sta-")) {
  495. conf_sta = os_zalloc(sizeof(struct dpp_configuration));
  496. if (!conf_sta)
  497. goto fail;
  498. os_memcpy(conf_sta->ssid, ssid, ssid_len);
  499. conf_sta->ssid_len = ssid_len;
  500. if (os_strstr(cmd, " conf=sta-psk") ||
  501. os_strstr(cmd, " conf=sta-sae") ||
  502. os_strstr(cmd, " conf=sta-psk-sae")) {
  503. if (os_strstr(cmd, " conf=sta-psk-sae"))
  504. conf_sta->akm = DPP_AKM_PSK_SAE;
  505. else if (os_strstr(cmd, " conf=sta-sae"))
  506. conf_sta->akm = DPP_AKM_SAE;
  507. else
  508. conf_sta->akm = DPP_AKM_PSK;
  509. if (psk_set) {
  510. os_memcpy(conf_sta->psk, psk, PMK_LEN);
  511. } else {
  512. conf_sta->passphrase = os_strdup(pass);
  513. if (!conf_sta->passphrase)
  514. goto fail;
  515. }
  516. } else if (os_strstr(cmd, " conf=sta-dpp")) {
  517. conf_sta->akm = DPP_AKM_DPP;
  518. } else {
  519. goto fail;
  520. }
  521. }
  522. if (os_strstr(cmd, " conf=ap-")) {
  523. conf_ap = os_zalloc(sizeof(struct dpp_configuration));
  524. if (!conf_ap)
  525. goto fail;
  526. os_memcpy(conf_ap->ssid, ssid, ssid_len);
  527. conf_ap->ssid_len = ssid_len;
  528. if (os_strstr(cmd, " conf=ap-psk") ||
  529. os_strstr(cmd, " conf=ap-sae") ||
  530. os_strstr(cmd, " conf=ap-psk-sae")) {
  531. if (os_strstr(cmd, " conf=ap-psk-sae"))
  532. conf_ap->akm = DPP_AKM_PSK_SAE;
  533. else if (os_strstr(cmd, " conf=ap-sae"))
  534. conf_ap->akm = DPP_AKM_SAE;
  535. else
  536. conf_ap->akm = DPP_AKM_PSK;
  537. if (psk_set) {
  538. os_memcpy(conf_ap->psk, psk, PMK_LEN);
  539. } else {
  540. conf_ap->passphrase = os_strdup(pass);
  541. if (!conf_ap->passphrase)
  542. goto fail;
  543. }
  544. } else if (os_strstr(cmd, " conf=ap-dpp")) {
  545. conf_ap->akm = DPP_AKM_DPP;
  546. } else {
  547. goto fail;
  548. }
  549. }
  550. pos = os_strstr(cmd, " expiry=");
  551. if (pos) {
  552. long int val;
  553. pos += 8;
  554. val = strtol(pos, NULL, 0);
  555. if (val <= 0)
  556. goto fail;
  557. if (conf_sta)
  558. conf_sta->netaccesskey_expiry = val;
  559. if (conf_ap)
  560. conf_ap->netaccesskey_expiry = val;
  561. }
  562. pos = os_strstr(cmd, " configurator=");
  563. if (pos) {
  564. pos += 14;
  565. conf = dpp_configurator_get_id(wpa_s, atoi(pos));
  566. if (!conf) {
  567. wpa_printf(MSG_INFO,
  568. "DPP: Could not find the specified configurator");
  569. goto fail;
  570. }
  571. }
  572. auth->conf_sta = conf_sta;
  573. auth->conf_ap = conf_ap;
  574. auth->conf = conf;
  575. return;
  576. fail:
  577. wpa_printf(MSG_DEBUG, "DPP: Failed to set configurator parameters");
  578. dpp_configuration_free(conf_sta);
  579. dpp_configuration_free(conf_ap);
  580. }
  581. static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx)
  582. {
  583. struct wpa_supplicant *wpa_s = eloop_ctx;
  584. if (!wpa_s->dpp_auth)
  585. return;
  586. wpa_printf(MSG_DEBUG, "DPP: Retry initiation after timeout");
  587. wpas_dpp_auth_init_next(wpa_s);
  588. }
  589. static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s)
  590. {
  591. struct dpp_authentication *auth = wpa_s->dpp_auth;
  592. const u8 *dst;
  593. unsigned int wait_time, max_wait_time, freq, max_tries, used;
  594. struct os_reltime now, diff;
  595. wpa_s->dpp_in_response_listen = 0;
  596. if (!auth)
  597. return -1;
  598. if (auth->freq_idx == 0)
  599. os_get_reltime(&wpa_s->dpp_init_iter_start);
  600. if (auth->freq_idx >= auth->num_freq) {
  601. auth->num_freq_iters++;
  602. if (wpa_s->dpp_init_max_tries)
  603. max_tries = wpa_s->dpp_init_max_tries;
  604. else
  605. max_tries = 5;
  606. if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
  607. wpa_printf(MSG_INFO,
  608. "DPP: No response received from responder - stopping initiation attempt");
  609. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
  610. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout,
  611. wpa_s, NULL);
  612. offchannel_send_action_done(wpa_s);
  613. dpp_auth_deinit(wpa_s->dpp_auth);
  614. wpa_s->dpp_auth = NULL;
  615. return -1;
  616. }
  617. auth->freq_idx = 0;
  618. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  619. if (wpa_s->dpp_init_retry_time)
  620. wait_time = wpa_s->dpp_init_retry_time;
  621. else
  622. wait_time = 10000;
  623. os_get_reltime(&now);
  624. os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
  625. used = diff.sec * 1000 + diff.usec / 1000;
  626. if (used > wait_time)
  627. wait_time = 0;
  628. else
  629. wait_time -= used;
  630. wpa_printf(MSG_DEBUG, "DPP: Next init attempt in %u ms",
  631. wait_time);
  632. eloop_register_timeout(wait_time / 1000,
  633. (wait_time % 1000) * 1000,
  634. wpas_dpp_init_timeout, wpa_s,
  635. NULL);
  636. return 0;
  637. }
  638. freq = auth->freq[auth->freq_idx++];
  639. auth->curr_freq = freq;
  640. if (is_zero_ether_addr(auth->peer_bi->mac_addr))
  641. dst = broadcast;
  642. else
  643. dst = auth->peer_bi->mac_addr;
  644. wpa_s->dpp_auth_ok_on_ack = 0;
  645. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  646. wait_time = wpa_s->max_remain_on_chan;
  647. max_wait_time = wpa_s->dpp_resp_wait_time ?
  648. wpa_s->dpp_resp_wait_time : 2000;
  649. if (wait_time > max_wait_time)
  650. wait_time = max_wait_time;
  651. wait_time += 10; /* give the driver some extra time to complete */
  652. eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
  653. wpas_dpp_reply_wait_timeout,
  654. wpa_s, NULL);
  655. wait_time -= 10;
  656. if (auth->neg_freq > 0 && freq != auth->neg_freq) {
  657. wpa_printf(MSG_DEBUG,
  658. "DPP: Initiate on %u MHz and move to neg_freq %u MHz for response",
  659. freq, auth->neg_freq);
  660. }
  661. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  662. MAC2STR(dst), freq, DPP_PA_AUTHENTICATION_REQ);
  663. auth->auth_req_ack = 0;
  664. os_get_reltime(&wpa_s->dpp_last_init);
  665. return offchannel_send_action(wpa_s, freq, dst,
  666. wpa_s->own_addr, broadcast,
  667. wpabuf_head(auth->req_msg),
  668. wpabuf_len(auth->req_msg),
  669. wait_time, wpas_dpp_tx_status, 0);
  670. }
  671. int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
  672. {
  673. const char *pos;
  674. struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
  675. u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
  676. unsigned int neg_freq = 0;
  677. wpa_s->dpp_gas_client = 0;
  678. pos = os_strstr(cmd, " peer=");
  679. if (!pos)
  680. return -1;
  681. pos += 6;
  682. peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  683. if (!peer_bi) {
  684. wpa_printf(MSG_INFO,
  685. "DPP: Could not find bootstrapping info for the identified peer");
  686. return -1;
  687. }
  688. pos = os_strstr(cmd, " own=");
  689. if (pos) {
  690. pos += 5;
  691. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  692. if (!own_bi) {
  693. wpa_printf(MSG_INFO,
  694. "DPP: Could not find bootstrapping info for the identified local entry");
  695. return -1;
  696. }
  697. if (peer_bi->curve != own_bi->curve) {
  698. wpa_printf(MSG_INFO,
  699. "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
  700. peer_bi->curve->name, own_bi->curve->name);
  701. return -1;
  702. }
  703. }
  704. pos = os_strstr(cmd, " role=");
  705. if (pos) {
  706. pos += 6;
  707. if (os_strncmp(pos, "configurator", 12) == 0)
  708. allowed_roles = DPP_CAPAB_CONFIGURATOR;
  709. else if (os_strncmp(pos, "enrollee", 8) == 0)
  710. allowed_roles = DPP_CAPAB_ENROLLEE;
  711. else if (os_strncmp(pos, "either", 6) == 0)
  712. allowed_roles = DPP_CAPAB_CONFIGURATOR |
  713. DPP_CAPAB_ENROLLEE;
  714. else
  715. goto fail;
  716. }
  717. pos = os_strstr(cmd, " netrole=");
  718. if (pos) {
  719. pos += 9;
  720. wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
  721. }
  722. pos = os_strstr(cmd, " neg_freq=");
  723. if (pos)
  724. neg_freq = atoi(pos + 10);
  725. if (wpa_s->dpp_auth) {
  726. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  727. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  728. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
  729. NULL);
  730. offchannel_send_action_done(wpa_s);
  731. dpp_auth_deinit(wpa_s->dpp_auth);
  732. }
  733. wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, allowed_roles,
  734. neg_freq,
  735. wpa_s->hw.modes, wpa_s->hw.num_modes);
  736. if (!wpa_s->dpp_auth)
  737. goto fail;
  738. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  739. wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth, cmd);
  740. wpa_s->dpp_auth->neg_freq = neg_freq;
  741. if (!is_zero_ether_addr(peer_bi->mac_addr))
  742. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
  743. ETH_ALEN);
  744. return wpas_dpp_auth_init_next(wpa_s);
  745. fail:
  746. return -1;
  747. }
  748. struct wpas_dpp_listen_work {
  749. unsigned int freq;
  750. unsigned int duration;
  751. struct wpabuf *probe_resp_ie;
  752. };
  753. static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
  754. {
  755. if (!lwork)
  756. return;
  757. os_free(lwork);
  758. }
  759. static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
  760. {
  761. struct wpas_dpp_listen_work *lwork;
  762. if (!wpa_s->dpp_listen_work)
  763. return;
  764. lwork = wpa_s->dpp_listen_work->ctx;
  765. wpas_dpp_listen_work_free(lwork);
  766. radio_work_done(wpa_s->dpp_listen_work);
  767. wpa_s->dpp_listen_work = NULL;
  768. }
  769. static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
  770. {
  771. struct wpa_supplicant *wpa_s = work->wpa_s;
  772. struct wpas_dpp_listen_work *lwork = work->ctx;
  773. if (deinit) {
  774. if (work->started) {
  775. wpa_s->dpp_listen_work = NULL;
  776. wpas_dpp_listen_stop(wpa_s);
  777. }
  778. wpas_dpp_listen_work_free(lwork);
  779. return;
  780. }
  781. wpa_s->dpp_listen_work = work;
  782. wpa_s->dpp_pending_listen_freq = lwork->freq;
  783. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
  784. wpa_s->max_remain_on_chan) < 0) {
  785. wpa_printf(MSG_DEBUG,
  786. "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
  787. lwork->freq);
  788. wpas_dpp_listen_work_done(wpa_s);
  789. wpa_s->dpp_pending_listen_freq = 0;
  790. return;
  791. }
  792. wpa_s->off_channel_freq = 0;
  793. wpa_s->roc_waiting_drv_freq = lwork->freq;
  794. }
  795. static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
  796. unsigned int freq)
  797. {
  798. struct wpas_dpp_listen_work *lwork;
  799. if (wpa_s->dpp_listen_work) {
  800. wpa_printf(MSG_DEBUG,
  801. "DPP: Reject start_listen since dpp_listen_work already exists");
  802. return -1;
  803. }
  804. if (wpa_s->dpp_listen_freq)
  805. wpas_dpp_listen_stop(wpa_s);
  806. wpa_s->dpp_listen_freq = freq;
  807. lwork = os_zalloc(sizeof(*lwork));
  808. if (!lwork)
  809. return -1;
  810. lwork->freq = freq;
  811. if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
  812. lwork) < 0) {
  813. wpas_dpp_listen_work_free(lwork);
  814. return -1;
  815. }
  816. return 0;
  817. }
  818. int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
  819. {
  820. int freq;
  821. freq = atoi(cmd);
  822. if (freq <= 0)
  823. return -1;
  824. if (os_strstr(cmd, " role=configurator"))
  825. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
  826. else if (os_strstr(cmd, " role=enrollee"))
  827. wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
  828. else
  829. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
  830. DPP_CAPAB_ENROLLEE;
  831. wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
  832. wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
  833. if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
  834. wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
  835. freq);
  836. return 0;
  837. }
  838. return wpas_dpp_listen_start(wpa_s, freq);
  839. }
  840. void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
  841. {
  842. wpa_s->dpp_in_response_listen = 0;
  843. if (!wpa_s->dpp_listen_freq)
  844. return;
  845. wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
  846. wpa_s->dpp_listen_freq);
  847. wpa_drv_cancel_remain_on_channel(wpa_s);
  848. wpa_s->dpp_listen_freq = 0;
  849. wpas_dpp_listen_work_done(wpa_s);
  850. }
  851. void wpas_dpp_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  852. unsigned int freq)
  853. {
  854. if (!wpa_s->dpp_listen_freq && !wpa_s->dpp_pending_listen_freq)
  855. return;
  856. wpa_printf(MSG_DEBUG,
  857. "DPP: remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u)",
  858. wpa_s->off_channel_freq, wpa_s->dpp_pending_listen_freq,
  859. wpa_s->roc_waiting_drv_freq, freq);
  860. if (wpa_s->off_channel_freq &&
  861. wpa_s->off_channel_freq == wpa_s->dpp_pending_listen_freq) {
  862. wpa_printf(MSG_DEBUG, "DPP: Listen on %u MHz started", freq);
  863. wpa_s->dpp_pending_listen_freq = 0;
  864. } else {
  865. wpa_printf(MSG_DEBUG,
  866. "DPP: Ignore remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d freq=%u)",
  867. wpa_s->off_channel_freq,
  868. wpa_s->dpp_pending_listen_freq, freq);
  869. }
  870. }
  871. void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  872. unsigned int freq)
  873. {
  874. wpas_dpp_listen_work_done(wpa_s);
  875. if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
  876. unsigned int new_freq;
  877. /* Continue listen with a new remain-on-channel */
  878. if (wpa_s->dpp_auth->neg_freq > 0)
  879. new_freq = wpa_s->dpp_auth->neg_freq;
  880. else
  881. new_freq = wpa_s->dpp_auth->curr_freq;
  882. wpa_printf(MSG_DEBUG,
  883. "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
  884. new_freq);
  885. wpas_dpp_listen_start(wpa_s, new_freq);
  886. return;
  887. }
  888. if (wpa_s->dpp_listen_freq) {
  889. /* Continue listen with a new remain-on-channel */
  890. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
  891. }
  892. }
  893. static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
  894. const u8 *hdr, const u8 *buf, size_t len,
  895. unsigned int freq)
  896. {
  897. const u8 *r_bootstrap, *i_bootstrap;
  898. u16 r_bootstrap_len, i_bootstrap_len;
  899. struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
  900. wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
  901. MAC2STR(src));
  902. r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
  903. &r_bootstrap_len);
  904. if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
  905. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  906. "Missing or invalid required Responder Bootstrapping Key Hash attribute");
  907. return;
  908. }
  909. wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
  910. r_bootstrap, r_bootstrap_len);
  911. i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  912. &i_bootstrap_len);
  913. if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
  914. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  915. "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
  916. return;
  917. }
  918. wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
  919. i_bootstrap, i_bootstrap_len);
  920. /* Try to find own and peer bootstrapping key matches based on the
  921. * received hash values */
  922. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  923. list) {
  924. if (!own_bi && bi->own &&
  925. os_memcmp(bi->pubkey_hash, r_bootstrap,
  926. SHA256_MAC_LEN) == 0) {
  927. wpa_printf(MSG_DEBUG,
  928. "DPP: Found matching own bootstrapping information");
  929. own_bi = bi;
  930. }
  931. if (!peer_bi && !bi->own &&
  932. os_memcmp(bi->pubkey_hash, i_bootstrap,
  933. SHA256_MAC_LEN) == 0) {
  934. wpa_printf(MSG_DEBUG,
  935. "DPP: Found matching peer bootstrapping information");
  936. peer_bi = bi;
  937. }
  938. if (own_bi && peer_bi)
  939. break;
  940. }
  941. if (!own_bi) {
  942. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  943. "No matching own bootstrapping key found - ignore message");
  944. return;
  945. }
  946. if (wpa_s->dpp_auth) {
  947. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  948. "Already in DPP authentication exchange - ignore new one");
  949. return;
  950. }
  951. wpa_s->dpp_gas_client = 0;
  952. wpa_s->dpp_auth_ok_on_ack = 0;
  953. wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
  954. wpa_s->dpp_qr_mutual,
  955. peer_bi, own_bi, freq, hdr, buf, len);
  956. if (!wpa_s->dpp_auth) {
  957. wpa_printf(MSG_DEBUG, "DPP: No response generated");
  958. return;
  959. }
  960. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  961. wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth,
  962. wpa_s->dpp_configurator_params);
  963. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
  964. if (wpa_s->dpp_listen_freq &&
  965. wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
  966. wpa_printf(MSG_DEBUG,
  967. "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
  968. wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
  969. wpas_dpp_listen_stop(wpa_s);
  970. }
  971. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  972. MAC2STR(src), wpa_s->dpp_auth->curr_freq,
  973. DPP_PA_AUTHENTICATION_RESP);
  974. offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
  975. src, wpa_s->own_addr, broadcast,
  976. wpabuf_head(wpa_s->dpp_auth->resp_msg),
  977. wpabuf_len(wpa_s->dpp_auth->resp_msg),
  978. 500, wpas_dpp_tx_status, 0);
  979. }
  980. static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
  981. {
  982. /* TODO: stop wait and start ROC */
  983. }
  984. static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
  985. struct dpp_authentication *auth)
  986. {
  987. struct wpa_ssid *ssid;
  988. ssid = wpa_config_add_network(wpa_s->conf);
  989. if (!ssid)
  990. return NULL;
  991. wpas_notify_network_added(wpa_s, ssid);
  992. wpa_config_set_network_defaults(ssid);
  993. ssid->disabled = 1;
  994. ssid->ssid = os_malloc(auth->ssid_len);
  995. if (!ssid->ssid)
  996. goto fail;
  997. os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
  998. ssid->ssid_len = auth->ssid_len;
  999. if (auth->connector) {
  1000. ssid->key_mgmt = WPA_KEY_MGMT_DPP;
  1001. ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
  1002. ssid->dpp_connector = os_strdup(auth->connector);
  1003. if (!ssid->dpp_connector)
  1004. goto fail;
  1005. }
  1006. if (auth->c_sign_key) {
  1007. ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
  1008. if (!ssid->dpp_csign)
  1009. goto fail;
  1010. os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
  1011. wpabuf_len(auth->c_sign_key));
  1012. ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
  1013. }
  1014. if (auth->net_access_key) {
  1015. ssid->dpp_netaccesskey =
  1016. os_malloc(wpabuf_len(auth->net_access_key));
  1017. if (!ssid->dpp_netaccesskey)
  1018. goto fail;
  1019. os_memcpy(ssid->dpp_netaccesskey,
  1020. wpabuf_head(auth->net_access_key),
  1021. wpabuf_len(auth->net_access_key));
  1022. ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
  1023. ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
  1024. }
  1025. if (!auth->connector) {
  1026. ssid->key_mgmt = 0;
  1027. if (auth->akm == DPP_AKM_PSK || auth->akm == DPP_AKM_PSK_SAE)
  1028. ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
  1029. WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
  1030. if (auth->akm == DPP_AKM_SAE || auth->akm == DPP_AKM_PSK_SAE)
  1031. ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
  1032. WPA_KEY_MGMT_FT_SAE;
  1033. ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
  1034. if (auth->passphrase[0]) {
  1035. if (wpa_config_set_quoted(ssid, "psk",
  1036. auth->passphrase) < 0)
  1037. goto fail;
  1038. wpa_config_update_psk(ssid);
  1039. ssid->export_keys = 1;
  1040. } else {
  1041. ssid->psk_set = auth->psk_set;
  1042. os_memcpy(ssid->psk, auth->psk, PMK_LEN);
  1043. }
  1044. }
  1045. return ssid;
  1046. fail:
  1047. wpas_notify_network_removed(wpa_s, ssid);
  1048. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1049. return NULL;
  1050. }
  1051. static void wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
  1052. struct dpp_authentication *auth)
  1053. {
  1054. struct wpa_ssid *ssid;
  1055. if (wpa_s->conf->dpp_config_processing < 1)
  1056. return;
  1057. ssid = wpas_dpp_add_network(wpa_s, auth);
  1058. if (!ssid)
  1059. return;
  1060. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
  1061. if (wpa_s->conf->dpp_config_processing < 2)
  1062. return;
  1063. wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
  1064. ssid->disabled = 0;
  1065. wpa_s->disconnected = 0;
  1066. wpa_s->reassociate = 1;
  1067. wpa_s->scan_runs = 0;
  1068. wpa_s->normal_scans = 0;
  1069. wpa_supplicant_cancel_sched_scan(wpa_s);
  1070. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1071. }
  1072. static void wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
  1073. struct dpp_authentication *auth)
  1074. {
  1075. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
  1076. if (auth->ssid_len)
  1077. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
  1078. wpa_ssid_txt(auth->ssid, auth->ssid_len));
  1079. if (auth->connector) {
  1080. /* TODO: Save the Connector and consider using a command
  1081. * to fetch the value instead of sending an event with
  1082. * it. The Connector could end up being larger than what
  1083. * most clients are ready to receive as an event
  1084. * message. */
  1085. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
  1086. auth->connector);
  1087. }
  1088. if (auth->c_sign_key) {
  1089. char *hex;
  1090. size_t hexlen;
  1091. hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
  1092. hex = os_malloc(hexlen);
  1093. if (hex) {
  1094. wpa_snprintf_hex(hex, hexlen,
  1095. wpabuf_head(auth->c_sign_key),
  1096. wpabuf_len(auth->c_sign_key));
  1097. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
  1098. hex);
  1099. os_free(hex);
  1100. }
  1101. }
  1102. if (auth->net_access_key) {
  1103. char *hex;
  1104. size_t hexlen;
  1105. hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
  1106. hex = os_malloc(hexlen);
  1107. if (hex) {
  1108. wpa_snprintf_hex(hex, hexlen,
  1109. wpabuf_head(auth->net_access_key),
  1110. wpabuf_len(auth->net_access_key));
  1111. if (auth->net_access_key_expiry)
  1112. wpa_msg(wpa_s, MSG_INFO,
  1113. DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
  1114. (long unsigned)
  1115. auth->net_access_key_expiry);
  1116. else
  1117. wpa_msg(wpa_s, MSG_INFO,
  1118. DPP_EVENT_NET_ACCESS_KEY "%s", hex);
  1119. os_free(hex);
  1120. }
  1121. }
  1122. wpas_dpp_process_config(wpa_s, auth);
  1123. }
  1124. static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1125. enum gas_query_result result,
  1126. const struct wpabuf *adv_proto,
  1127. const struct wpabuf *resp, u16 status_code)
  1128. {
  1129. struct wpa_supplicant *wpa_s = ctx;
  1130. const u8 *pos;
  1131. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1132. wpa_s->dpp_gas_dialog_token = -1;
  1133. if (!auth || !auth->auth_success) {
  1134. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  1135. return;
  1136. }
  1137. if (!resp || status_code != WLAN_STATUS_SUCCESS) {
  1138. wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
  1139. goto fail;
  1140. }
  1141. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
  1142. adv_proto);
  1143. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
  1144. resp);
  1145. if (wpabuf_len(adv_proto) != 10 ||
  1146. !(pos = wpabuf_head(adv_proto)) ||
  1147. pos[0] != WLAN_EID_ADV_PROTO ||
  1148. pos[1] != 8 ||
  1149. pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
  1150. pos[4] != 5 ||
  1151. WPA_GET_BE24(&pos[5]) != OUI_WFA ||
  1152. pos[8] != 0x1a ||
  1153. pos[9] != 1) {
  1154. wpa_printf(MSG_DEBUG,
  1155. "DPP: Not a DPP Advertisement Protocol ID");
  1156. goto fail;
  1157. }
  1158. if (dpp_conf_resp_rx(auth, resp) < 0) {
  1159. wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
  1160. goto fail;
  1161. }
  1162. wpas_dpp_handle_config_obj(wpa_s, auth);
  1163. dpp_auth_deinit(wpa_s->dpp_auth);
  1164. wpa_s->dpp_auth = NULL;
  1165. return;
  1166. fail:
  1167. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1168. dpp_auth_deinit(wpa_s->dpp_auth);
  1169. wpa_s->dpp_auth = NULL;
  1170. }
  1171. static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
  1172. {
  1173. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1174. struct wpabuf *buf, *conf_req;
  1175. char json[100];
  1176. int res;
  1177. wpa_s->dpp_gas_client = 1;
  1178. os_snprintf(json, sizeof(json),
  1179. "{\"name\":\"Test\","
  1180. "\"wi-fi_tech\":\"infra\","
  1181. "\"netRole\":\"%s\"}",
  1182. wpa_s->dpp_netrole_ap ? "ap" : "sta");
  1183. #ifdef CONFIG_TESTING_OPTIONS
  1184. if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
  1185. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
  1186. json[29] = 'k'; /* replace "infra" with "knfra" */
  1187. }
  1188. #endif /* CONFIG_TESTING_OPTIONS */
  1189. wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
  1190. offchannel_send_action_done(wpa_s);
  1191. wpas_dpp_listen_stop(wpa_s);
  1192. conf_req = dpp_build_conf_req(auth, json);
  1193. if (!conf_req) {
  1194. wpa_printf(MSG_DEBUG,
  1195. "DPP: No configuration request data available");
  1196. return;
  1197. }
  1198. buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
  1199. if (!buf) {
  1200. wpabuf_free(conf_req);
  1201. return;
  1202. }
  1203. /* Advertisement Protocol IE */
  1204. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1205. wpabuf_put_u8(buf, 8); /* Length */
  1206. wpabuf_put_u8(buf, 0x7f);
  1207. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  1208. wpabuf_put_u8(buf, 5);
  1209. wpabuf_put_be24(buf, OUI_WFA);
  1210. wpabuf_put_u8(buf, DPP_OUI_TYPE);
  1211. wpabuf_put_u8(buf, 0x01);
  1212. /* GAS Query */
  1213. wpabuf_put_le16(buf, wpabuf_len(conf_req));
  1214. wpabuf_put_buf(buf, conf_req);
  1215. wpabuf_free(conf_req);
  1216. wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
  1217. MAC2STR(auth->peer_mac_addr), auth->curr_freq);
  1218. res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
  1219. 1, buf, wpas_dpp_gas_resp_cb, wpa_s);
  1220. if (res < 0) {
  1221. wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
  1222. wpabuf_free(buf);
  1223. } else {
  1224. wpa_printf(MSG_DEBUG,
  1225. "DPP: GAS query started with dialog token %u", res);
  1226. wpa_s->dpp_gas_dialog_token = res;
  1227. }
  1228. }
  1229. static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
  1230. {
  1231. wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
  1232. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
  1233. #ifdef CONFIG_TESTING_OPTIONS
  1234. if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
  1235. wpa_printf(MSG_INFO,
  1236. "DPP: TESTING - stop at Authentication Confirm");
  1237. if (wpa_s->dpp_auth->configurator) {
  1238. /* Prevent GAS response */
  1239. wpa_s->dpp_auth->auth_success = 0;
  1240. }
  1241. return;
  1242. }
  1243. #endif /* CONFIG_TESTING_OPTIONS */
  1244. if (wpa_s->dpp_auth->configurator)
  1245. wpas_dpp_start_gas_server(wpa_s);
  1246. else
  1247. wpas_dpp_start_gas_client(wpa_s);
  1248. }
  1249. static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  1250. const u8 *hdr, const u8 *buf, size_t len,
  1251. unsigned int freq)
  1252. {
  1253. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1254. struct wpabuf *msg;
  1255. wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
  1256. " (freq %u MHz)", MAC2STR(src), freq);
  1257. if (!auth) {
  1258. wpa_printf(MSG_DEBUG,
  1259. "DPP: No DPP Authentication in progress - drop");
  1260. return;
  1261. }
  1262. if (!is_zero_ether_addr(auth->peer_mac_addr) &&
  1263. os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1264. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  1265. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  1266. return;
  1267. }
  1268. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1269. if (auth->curr_freq != freq && auth->neg_freq == freq) {
  1270. wpa_printf(MSG_DEBUG,
  1271. "DPP: Responder accepted request for different negotiation channel");
  1272. auth->curr_freq = freq;
  1273. }
  1274. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  1275. msg = dpp_auth_resp_rx(auth, hdr, buf, len);
  1276. if (!msg) {
  1277. if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
  1278. wpa_printf(MSG_DEBUG,
  1279. "DPP: Start wait for full response");
  1280. offchannel_send_action_done(wpa_s);
  1281. wpas_dpp_listen_start(wpa_s, auth->curr_freq);
  1282. return;
  1283. }
  1284. wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
  1285. return;
  1286. }
  1287. os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
  1288. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1289. MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
  1290. offchannel_send_action(wpa_s, auth->curr_freq,
  1291. src, wpa_s->own_addr, broadcast,
  1292. wpabuf_head(msg), wpabuf_len(msg),
  1293. 500, wpas_dpp_tx_status, 0);
  1294. wpabuf_free(msg);
  1295. wpa_s->dpp_auth_ok_on_ack = 1;
  1296. }
  1297. static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
  1298. const u8 *hdr, const u8 *buf, size_t len)
  1299. {
  1300. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1301. wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
  1302. MAC2STR(src));
  1303. if (!auth) {
  1304. wpa_printf(MSG_DEBUG,
  1305. "DPP: No DPP Authentication in progress - drop");
  1306. return;
  1307. }
  1308. if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1309. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  1310. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  1311. return;
  1312. }
  1313. if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
  1314. wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
  1315. return;
  1316. }
  1317. wpas_dpp_auth_success(wpa_s, 0);
  1318. }
  1319. static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
  1320. const u8 *src,
  1321. const u8 *buf, size_t len)
  1322. {
  1323. struct wpa_ssid *ssid;
  1324. const u8 *connector, *trans_id, *status;
  1325. u16 connector_len, trans_id_len, status_len;
  1326. struct dpp_introduction intro;
  1327. struct rsn_pmksa_cache_entry *entry;
  1328. struct os_time now;
  1329. struct os_reltime rnow;
  1330. os_time_t expiry;
  1331. unsigned int seconds;
  1332. enum dpp_status_error res;
  1333. wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
  1334. MAC2STR(src));
  1335. if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
  1336. os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
  1337. wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
  1338. MACSTR " - drop", MAC2STR(src));
  1339. return;
  1340. }
  1341. offchannel_send_action_done(wpa_s);
  1342. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1343. if (ssid == wpa_s->dpp_intro_network)
  1344. break;
  1345. }
  1346. if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1347. !ssid->dpp_csign) {
  1348. wpa_printf(MSG_DEBUG,
  1349. "DPP: Profile not found for network introduction");
  1350. return;
  1351. }
  1352. trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
  1353. &trans_id_len);
  1354. if (!trans_id || trans_id_len != 1) {
  1355. wpa_printf(MSG_DEBUG,
  1356. "DPP: Peer did not include Transaction ID");
  1357. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1358. " fail=missing_transaction_id", MAC2STR(src));
  1359. goto fail;
  1360. }
  1361. if (trans_id[0] != TRANSACTION_ID) {
  1362. wpa_printf(MSG_DEBUG,
  1363. "DPP: Ignore frame with unexpected Transaction ID %u",
  1364. trans_id[0]);
  1365. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1366. " fail=transaction_id_mismatch", MAC2STR(src));
  1367. goto fail;
  1368. }
  1369. status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
  1370. if (!status || status_len != 1) {
  1371. wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
  1372. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1373. " fail=missing_status", MAC2STR(src));
  1374. goto fail;
  1375. }
  1376. if (status[0] != DPP_STATUS_OK) {
  1377. wpa_printf(MSG_DEBUG,
  1378. "DPP: Peer rejected network introduction: Status %u",
  1379. status[0]);
  1380. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1381. " status=%u", MAC2STR(src), status[0]);
  1382. goto fail;
  1383. }
  1384. connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
  1385. if (!connector) {
  1386. wpa_printf(MSG_DEBUG,
  1387. "DPP: Peer did not include its Connector");
  1388. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1389. " fail=missing_connector", MAC2STR(src));
  1390. goto fail;
  1391. }
  1392. res = dpp_peer_intro(&intro, ssid->dpp_connector,
  1393. ssid->dpp_netaccesskey,
  1394. ssid->dpp_netaccesskey_len,
  1395. ssid->dpp_csign,
  1396. ssid->dpp_csign_len,
  1397. connector, connector_len, &expiry);
  1398. if (res != DPP_STATUS_OK) {
  1399. wpa_printf(MSG_INFO,
  1400. "DPP: Network Introduction protocol resulted in failure");
  1401. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1402. " fail=peer_connector_validation_failed", MAC2STR(src));
  1403. goto fail;
  1404. }
  1405. entry = os_zalloc(sizeof(*entry));
  1406. if (!entry)
  1407. goto fail;
  1408. os_memcpy(entry->aa, src, ETH_ALEN);
  1409. os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
  1410. os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
  1411. entry->pmk_len = intro.pmk_len;
  1412. entry->akmp = WPA_KEY_MGMT_DPP;
  1413. if (expiry) {
  1414. os_get_time(&now);
  1415. seconds = expiry - now.sec;
  1416. } else {
  1417. seconds = 86400 * 7;
  1418. }
  1419. os_get_reltime(&rnow);
  1420. entry->expiration = rnow.sec + seconds;
  1421. entry->reauth_time = rnow.sec + seconds;
  1422. entry->network_ctx = ssid;
  1423. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  1424. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1425. " status=%u", MAC2STR(src), status[0]);
  1426. wpa_printf(MSG_DEBUG,
  1427. "DPP: Try connection again after successful network introduction");
  1428. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  1429. wpa_supplicant_cancel_sched_scan(wpa_s);
  1430. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1431. }
  1432. fail:
  1433. os_memset(&intro, 0, sizeof(intro));
  1434. }
  1435. static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
  1436. {
  1437. int i, j;
  1438. if (!wpa_s->hw.modes)
  1439. return -1;
  1440. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  1441. struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
  1442. for (j = 0; j < mode->num_channels; j++) {
  1443. struct hostapd_channel_data *chan = &mode->channels[j];
  1444. if (chan->freq != (int) freq)
  1445. continue;
  1446. if (chan->flag & (HOSTAPD_CHAN_DISABLED |
  1447. HOSTAPD_CHAN_NO_IR |
  1448. HOSTAPD_CHAN_RADAR))
  1449. continue;
  1450. return 1;
  1451. }
  1452. }
  1453. wpa_printf(MSG_DEBUG,
  1454. "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
  1455. freq);
  1456. return 0;
  1457. }
  1458. static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
  1459. struct dpp_pkex *pkex)
  1460. {
  1461. if (pkex->freq == 2437)
  1462. pkex->freq = 5745;
  1463. else if (pkex->freq == 5745)
  1464. pkex->freq = 5220;
  1465. else if (pkex->freq == 5220)
  1466. pkex->freq = 60480;
  1467. else
  1468. return -1; /* no more channels to try */
  1469. if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
  1470. wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
  1471. pkex->freq);
  1472. return 0;
  1473. }
  1474. /* Could not use this channel - try the next one */
  1475. return wpas_dpp_pkex_next_channel(wpa_s, pkex);
  1476. }
  1477. static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
  1478. {
  1479. struct wpa_supplicant *wpa_s = eloop_ctx;
  1480. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1481. if (!pkex || !pkex->exchange_req)
  1482. return;
  1483. if (pkex->exch_req_tries >= 5) {
  1484. if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
  1485. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  1486. "No response from PKEX peer");
  1487. dpp_pkex_free(pkex);
  1488. wpa_s->dpp_pkex = NULL;
  1489. return;
  1490. }
  1491. pkex->exch_req_tries = 0;
  1492. }
  1493. pkex->exch_req_tries++;
  1494. wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
  1495. pkex->exch_req_tries);
  1496. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1497. MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
  1498. offchannel_send_action(wpa_s, pkex->freq, broadcast,
  1499. wpa_s->own_addr, broadcast,
  1500. wpabuf_head(pkex->exchange_req),
  1501. wpabuf_len(pkex->exchange_req),
  1502. pkex->exch_req_wait_time,
  1503. wpas_dpp_tx_pkex_status, 0);
  1504. }
  1505. static void
  1506. wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
  1507. unsigned int freq, const u8 *dst,
  1508. const u8 *src, const u8 *bssid,
  1509. const u8 *data, size_t data_len,
  1510. enum offchannel_send_action_result result)
  1511. {
  1512. const char *res_txt;
  1513. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1514. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1515. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1516. "FAILED");
  1517. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1518. " result=%s (PKEX)",
  1519. freq, MAC2STR(dst), res_txt);
  1520. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  1521. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  1522. if (!pkex) {
  1523. wpa_printf(MSG_DEBUG,
  1524. "DPP: Ignore TX status since there is no ongoing PKEX exchange");
  1525. return;
  1526. }
  1527. if (pkex->failed) {
  1528. wpa_printf(MSG_DEBUG,
  1529. "DPP: Terminate PKEX exchange due to an earlier error");
  1530. if (pkex->t > pkex->own_bi->pkex_t)
  1531. pkex->own_bi->pkex_t = pkex->t;
  1532. dpp_pkex_free(pkex);
  1533. wpa_s->dpp_pkex = NULL;
  1534. return;
  1535. }
  1536. if (pkex->exch_req_wait_time && pkex->exchange_req) {
  1537. /* Wait for PKEX Exchange Response frame and retry request if
  1538. * no response is seen. */
  1539. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  1540. eloop_register_timeout(pkex->exch_req_wait_time / 1000,
  1541. (pkex->exch_req_wait_time % 1000) * 1000,
  1542. wpas_dpp_pkex_retry_timeout, wpa_s,
  1543. NULL);
  1544. }
  1545. }
  1546. static void
  1547. wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
  1548. const u8 *buf, size_t len, unsigned int freq)
  1549. {
  1550. struct wpabuf *msg;
  1551. unsigned int wait_time;
  1552. wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
  1553. MAC2STR(src));
  1554. /* TODO: Support multiple PKEX codes by iterating over all the enabled
  1555. * values here */
  1556. if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
  1557. wpa_printf(MSG_DEBUG,
  1558. "DPP: No PKEX code configured - ignore request");
  1559. return;
  1560. }
  1561. if (wpa_s->dpp_pkex) {
  1562. /* TODO: Support parallel operations */
  1563. wpa_printf(MSG_DEBUG,
  1564. "DPP: Already in PKEX session - ignore new request");
  1565. return;
  1566. }
  1567. wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
  1568. wpa_s->own_addr, src,
  1569. wpa_s->dpp_pkex_identifier,
  1570. wpa_s->dpp_pkex_code,
  1571. buf, len);
  1572. if (!wpa_s->dpp_pkex) {
  1573. wpa_printf(MSG_DEBUG,
  1574. "DPP: Failed to process the request - ignore it");
  1575. return;
  1576. }
  1577. msg = wpa_s->dpp_pkex->exchange_resp;
  1578. wait_time = wpa_s->max_remain_on_chan;
  1579. if (wait_time > 2000)
  1580. wait_time = 2000;
  1581. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1582. MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
  1583. offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
  1584. broadcast,
  1585. wpabuf_head(msg), wpabuf_len(msg),
  1586. wait_time, wpas_dpp_tx_pkex_status, 0);
  1587. }
  1588. static void
  1589. wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  1590. const u8 *buf, size_t len, unsigned int freq)
  1591. {
  1592. struct wpabuf *msg;
  1593. unsigned int wait_time;
  1594. wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
  1595. MAC2STR(src));
  1596. /* TODO: Support multiple PKEX codes by iterating over all the enabled
  1597. * values here */
  1598. if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
  1599. wpa_s->dpp_pkex->exchange_done) {
  1600. wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
  1601. return;
  1602. }
  1603. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  1604. wpa_s->dpp_pkex->exch_req_wait_time = 0;
  1605. msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
  1606. if (!msg) {
  1607. wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
  1608. return;
  1609. }
  1610. wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
  1611. MAC2STR(src));
  1612. wait_time = wpa_s->max_remain_on_chan;
  1613. if (wait_time > 2000)
  1614. wait_time = 2000;
  1615. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1616. MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
  1617. offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
  1618. broadcast,
  1619. wpabuf_head(msg), wpabuf_len(msg),
  1620. wait_time, wpas_dpp_tx_pkex_status, 0);
  1621. wpabuf_free(msg);
  1622. }
  1623. static struct dpp_bootstrap_info *
  1624. wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
  1625. unsigned int freq)
  1626. {
  1627. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1628. struct dpp_bootstrap_info *bi;
  1629. bi = os_zalloc(sizeof(*bi));
  1630. if (!bi)
  1631. return NULL;
  1632. bi->id = wpas_dpp_next_id(wpa_s);
  1633. bi->type = DPP_BOOTSTRAP_PKEX;
  1634. os_memcpy(bi->mac_addr, peer, ETH_ALEN);
  1635. bi->num_freq = 1;
  1636. bi->freq[0] = freq;
  1637. bi->curve = pkex->own_bi->curve;
  1638. bi->pubkey = pkex->peer_bootstrap_key;
  1639. pkex->peer_bootstrap_key = NULL;
  1640. dpp_pkex_free(pkex);
  1641. wpa_s->dpp_pkex = NULL;
  1642. if (dpp_bootstrap_key_hash(bi) < 0) {
  1643. dpp_bootstrap_info_free(bi);
  1644. return NULL;
  1645. }
  1646. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  1647. return bi;
  1648. }
  1649. static void
  1650. wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
  1651. const u8 *hdr, const u8 *buf, size_t len,
  1652. unsigned int freq)
  1653. {
  1654. struct wpabuf *msg;
  1655. unsigned int wait_time;
  1656. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1657. wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
  1658. MAC2STR(src));
  1659. if (!pkex || pkex->initiator || !pkex->exchange_done) {
  1660. wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
  1661. return;
  1662. }
  1663. msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
  1664. if (!msg) {
  1665. wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
  1666. if (pkex->failed) {
  1667. wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
  1668. if (pkex->t > pkex->own_bi->pkex_t)
  1669. pkex->own_bi->pkex_t = pkex->t;
  1670. dpp_pkex_free(wpa_s->dpp_pkex);
  1671. wpa_s->dpp_pkex = NULL;
  1672. }
  1673. return;
  1674. }
  1675. wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
  1676. MACSTR, MAC2STR(src));
  1677. wait_time = wpa_s->max_remain_on_chan;
  1678. if (wait_time > 2000)
  1679. wait_time = 2000;
  1680. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1681. MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
  1682. offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
  1683. broadcast,
  1684. wpabuf_head(msg), wpabuf_len(msg),
  1685. wait_time, wpas_dpp_tx_pkex_status, 0);
  1686. wpabuf_free(msg);
  1687. wpas_dpp_pkex_finish(wpa_s, src, freq);
  1688. }
  1689. static void
  1690. wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  1691. const u8 *hdr, const u8 *buf, size_t len,
  1692. unsigned int freq)
  1693. {
  1694. int res;
  1695. struct dpp_bootstrap_info *bi;
  1696. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1697. char cmd[500];
  1698. wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
  1699. MAC2STR(src));
  1700. if (!pkex || !pkex->initiator || !pkex->exchange_done) {
  1701. wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
  1702. return;
  1703. }
  1704. res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
  1705. if (res < 0) {
  1706. wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
  1707. return;
  1708. }
  1709. bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
  1710. if (!bi)
  1711. return;
  1712. os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
  1713. bi->id,
  1714. wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
  1715. wpa_printf(MSG_DEBUG,
  1716. "DPP: Start authentication after PKEX with parameters: %s",
  1717. cmd);
  1718. if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
  1719. wpa_printf(MSG_DEBUG,
  1720. "DPP: Authentication initialization failed");
  1721. return;
  1722. }
  1723. }
  1724. void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
  1725. const u8 *buf, size_t len, unsigned int freq)
  1726. {
  1727. u8 crypto_suite;
  1728. enum dpp_public_action_frame_type type;
  1729. const u8 *hdr;
  1730. unsigned int pkex_t;
  1731. if (len < DPP_HDR_LEN)
  1732. return;
  1733. if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
  1734. return;
  1735. hdr = buf;
  1736. buf += 4;
  1737. len -= 4;
  1738. crypto_suite = *buf++;
  1739. type = *buf++;
  1740. len -= 2;
  1741. wpa_printf(MSG_DEBUG,
  1742. "DPP: Received DPP Public Action frame crypto suite %u type %d from "
  1743. MACSTR " freq=%u",
  1744. crypto_suite, type, MAC2STR(src), freq);
  1745. if (crypto_suite != 1) {
  1746. wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
  1747. crypto_suite);
  1748. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
  1749. " freq=%u type=%d ignore=unsupported-crypto-suite",
  1750. MAC2STR(src), freq, type);
  1751. return;
  1752. }
  1753. wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
  1754. if (dpp_check_attrs(buf, len) < 0) {
  1755. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
  1756. " freq=%u type=%d ignore=invalid-attributes",
  1757. MAC2STR(src), freq, type);
  1758. return;
  1759. }
  1760. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
  1761. MAC2STR(src), freq, type);
  1762. switch (type) {
  1763. case DPP_PA_AUTHENTICATION_REQ:
  1764. wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
  1765. break;
  1766. case DPP_PA_AUTHENTICATION_RESP:
  1767. wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
  1768. break;
  1769. case DPP_PA_AUTHENTICATION_CONF:
  1770. wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
  1771. break;
  1772. case DPP_PA_PEER_DISCOVERY_RESP:
  1773. wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
  1774. break;
  1775. case DPP_PA_PKEX_EXCHANGE_REQ:
  1776. wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
  1777. break;
  1778. case DPP_PA_PKEX_EXCHANGE_RESP:
  1779. wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
  1780. break;
  1781. case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
  1782. wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
  1783. freq);
  1784. break;
  1785. case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
  1786. wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
  1787. freq);
  1788. break;
  1789. default:
  1790. wpa_printf(MSG_DEBUG,
  1791. "DPP: Ignored unsupported frame subtype %d", type);
  1792. break;
  1793. }
  1794. if (wpa_s->dpp_pkex)
  1795. pkex_t = wpa_s->dpp_pkex->t;
  1796. else if (wpa_s->dpp_pkex_bi)
  1797. pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
  1798. else
  1799. pkex_t = 0;
  1800. if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
  1801. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
  1802. wpas_dpp_pkex_remove(wpa_s, "*");
  1803. }
  1804. }
  1805. static struct wpabuf *
  1806. wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
  1807. size_t query_len)
  1808. {
  1809. struct wpa_supplicant *wpa_s = ctx;
  1810. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1811. struct wpabuf *resp;
  1812. wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
  1813. MAC2STR(sa));
  1814. if (!auth || !auth->auth_success ||
  1815. os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1816. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  1817. return NULL;
  1818. }
  1819. wpa_hexdump(MSG_DEBUG,
  1820. "DPP: Received Configuration Request (GAS Query Request)",
  1821. query, query_len);
  1822. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
  1823. MAC2STR(sa));
  1824. resp = dpp_conf_req_rx(auth, query, query_len);
  1825. if (!resp)
  1826. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1827. auth->conf_resp = resp;
  1828. return resp;
  1829. }
  1830. static void
  1831. wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
  1832. {
  1833. struct wpa_supplicant *wpa_s = ctx;
  1834. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1835. if (!auth) {
  1836. wpabuf_free(resp);
  1837. return;
  1838. }
  1839. if (auth->conf_resp != resp) {
  1840. wpa_printf(MSG_DEBUG,
  1841. "DPP: Ignore GAS status report (ok=%d) for unknown response",
  1842. ok);
  1843. wpabuf_free(resp);
  1844. return;
  1845. }
  1846. wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
  1847. ok);
  1848. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1849. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  1850. offchannel_send_action_done(wpa_s);
  1851. wpas_dpp_listen_stop(wpa_s);
  1852. if (ok)
  1853. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
  1854. else
  1855. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1856. dpp_auth_deinit(wpa_s->dpp_auth);
  1857. wpa_s->dpp_auth = NULL;
  1858. wpabuf_free(resp);
  1859. }
  1860. static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
  1861. {
  1862. struct dpp_configurator *conf;
  1863. unsigned int max_id = 0;
  1864. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  1865. struct dpp_configurator, list) {
  1866. if (conf->id > max_id)
  1867. max_id = conf->id;
  1868. }
  1869. return max_id + 1;
  1870. }
  1871. int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
  1872. {
  1873. char *curve = NULL;
  1874. char *key = NULL;
  1875. u8 *privkey = NULL;
  1876. size_t privkey_len = 0;
  1877. int ret = -1;
  1878. struct dpp_configurator *conf = NULL;
  1879. curve = get_param(cmd, " curve=");
  1880. key = get_param(cmd, " key=");
  1881. if (key) {
  1882. privkey_len = os_strlen(key) / 2;
  1883. privkey = os_malloc(privkey_len);
  1884. if (!privkey ||
  1885. hexstr2bin(key, privkey, privkey_len) < 0)
  1886. goto fail;
  1887. }
  1888. conf = dpp_keygen_configurator(curve, privkey, privkey_len);
  1889. if (!conf)
  1890. goto fail;
  1891. conf->id = wpas_dpp_next_configurator_id(wpa_s);
  1892. dl_list_add(&wpa_s->dpp_configurator, &conf->list);
  1893. ret = conf->id;
  1894. conf = NULL;
  1895. fail:
  1896. os_free(curve);
  1897. str_clear_free(key);
  1898. bin_clear_free(privkey, privkey_len);
  1899. dpp_configurator_free(conf);
  1900. return ret;
  1901. }
  1902. static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
  1903. {
  1904. struct dpp_configurator *conf, *tmp;
  1905. int found = 0;
  1906. dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
  1907. struct dpp_configurator, list) {
  1908. if (id && conf->id != id)
  1909. continue;
  1910. found = 1;
  1911. dl_list_del(&conf->list);
  1912. dpp_configurator_free(conf);
  1913. }
  1914. if (id == 0)
  1915. return 0; /* flush succeeds regardless of entries found */
  1916. return found ? 0 : -1;
  1917. }
  1918. int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
  1919. {
  1920. unsigned int id_val;
  1921. if (os_strcmp(id, "*") == 0) {
  1922. id_val = 0;
  1923. } else {
  1924. id_val = atoi(id);
  1925. if (id_val == 0)
  1926. return -1;
  1927. }
  1928. return dpp_configurator_del(wpa_s, id_val);
  1929. }
  1930. int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
  1931. {
  1932. struct dpp_authentication *auth;
  1933. int ret = -1;
  1934. char *curve = NULL;
  1935. auth = os_zalloc(sizeof(*auth));
  1936. if (!auth)
  1937. return -1;
  1938. curve = get_param(cmd, " curve=");
  1939. wpas_dpp_set_configurator(wpa_s, auth, cmd);
  1940. if (dpp_configurator_own_config(auth, curve, 0) == 0) {
  1941. wpas_dpp_handle_config_obj(wpa_s, auth);
  1942. ret = 0;
  1943. }
  1944. dpp_auth_deinit(auth);
  1945. os_free(curve);
  1946. return ret;
  1947. }
  1948. int wpas_dpp_configurator_get_key(struct wpa_supplicant *wpa_s, unsigned int id,
  1949. char *buf, size_t buflen)
  1950. {
  1951. struct dpp_configurator *conf;
  1952. conf = dpp_configurator_get_id(wpa_s, id);
  1953. if (!conf)
  1954. return -1;
  1955. return dpp_configurator_get_key(conf, buf, buflen);
  1956. }
  1957. static void
  1958. wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
  1959. unsigned int freq, const u8 *dst,
  1960. const u8 *src, const u8 *bssid,
  1961. const u8 *data, size_t data_len,
  1962. enum offchannel_send_action_result result)
  1963. {
  1964. const char *res_txt;
  1965. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1966. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1967. "FAILED");
  1968. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1969. " result=%s (DPP Peer Discovery Request)",
  1970. freq, MAC2STR(dst), res_txt);
  1971. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  1972. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  1973. /* TODO: Time out wait for response more quickly in error cases? */
  1974. }
  1975. int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  1976. struct wpa_bss *bss)
  1977. {
  1978. struct os_time now;
  1979. struct wpabuf *msg;
  1980. unsigned int wait_time;
  1981. if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
  1982. return 0; /* Not using DPP AKM - continue */
  1983. if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
  1984. return 0; /* PMKSA exists for DPP AKM - continue */
  1985. if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1986. !ssid->dpp_csign) {
  1987. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1988. "missing %s",
  1989. !ssid->dpp_connector ? "Connector" :
  1990. (!ssid->dpp_netaccesskey ? "netAccessKey" :
  1991. "C-sign-key"));
  1992. return -1;
  1993. }
  1994. os_get_time(&now);
  1995. if (ssid->dpp_netaccesskey_expiry &&
  1996. (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
  1997. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1998. "netAccessKey expired");
  1999. return -1;
  2000. }
  2001. wpa_printf(MSG_DEBUG,
  2002. "DPP: Starting network introduction protocol to derive PMKSA for "
  2003. MACSTR, MAC2STR(bss->bssid));
  2004. msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
  2005. 5 + 4 + os_strlen(ssid->dpp_connector));
  2006. if (!msg)
  2007. return -1;
  2008. #ifdef CONFIG_TESTING_OPTIONS
  2009. if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
  2010. wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
  2011. goto skip_trans_id;
  2012. }
  2013. if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
  2014. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
  2015. wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
  2016. wpabuf_put_le16(msg, 0);
  2017. goto skip_trans_id;
  2018. }
  2019. #endif /* CONFIG_TESTING_OPTIONS */
  2020. /* Transaction ID */
  2021. wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
  2022. wpabuf_put_le16(msg, 1);
  2023. wpabuf_put_u8(msg, TRANSACTION_ID);
  2024. #ifdef CONFIG_TESTING_OPTIONS
  2025. skip_trans_id:
  2026. if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
  2027. wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
  2028. goto skip_connector;
  2029. }
  2030. if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
  2031. char *connector;
  2032. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
  2033. connector = dpp_corrupt_connector_signature(
  2034. ssid->dpp_connector);
  2035. if (!connector) {
  2036. wpabuf_free(msg);
  2037. return -1;
  2038. }
  2039. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  2040. wpabuf_put_le16(msg, os_strlen(connector));
  2041. wpabuf_put_str(msg, connector);
  2042. os_free(connector);
  2043. goto skip_connector;
  2044. }
  2045. #endif /* CONFIG_TESTING_OPTIONS */
  2046. /* DPP Connector */
  2047. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  2048. wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
  2049. wpabuf_put_str(msg, ssid->dpp_connector);
  2050. #ifdef CONFIG_TESTING_OPTIONS
  2051. skip_connector:
  2052. #endif /* CONFIG_TESTING_OPTIONS */
  2053. /* TODO: Timeout on AP response */
  2054. wait_time = wpa_s->max_remain_on_chan;
  2055. if (wait_time > 2000)
  2056. wait_time = 2000;
  2057. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  2058. MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
  2059. offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
  2060. broadcast,
  2061. wpabuf_head(msg), wpabuf_len(msg),
  2062. wait_time, wpas_dpp_tx_introduction_status, 0);
  2063. wpabuf_free(msg);
  2064. /* Request this connection attempt to terminate - new one will be
  2065. * started when network introduction protocol completes */
  2066. os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
  2067. wpa_s->dpp_intro_network = ssid;
  2068. return 1;
  2069. }
  2070. int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
  2071. {
  2072. struct dpp_bootstrap_info *own_bi;
  2073. const char *pos, *end;
  2074. unsigned int wait_time;
  2075. pos = os_strstr(cmd, " own=");
  2076. if (!pos)
  2077. return -1;
  2078. pos += 5;
  2079. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  2080. if (!own_bi) {
  2081. wpa_printf(MSG_DEBUG,
  2082. "DPP: Identified bootstrap info not found");
  2083. return -1;
  2084. }
  2085. if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
  2086. wpa_printf(MSG_DEBUG,
  2087. "DPP: Identified bootstrap info not for PKEX");
  2088. return -1;
  2089. }
  2090. wpa_s->dpp_pkex_bi = own_bi;
  2091. own_bi->pkex_t = 0; /* clear pending errors on new code */
  2092. os_free(wpa_s->dpp_pkex_identifier);
  2093. wpa_s->dpp_pkex_identifier = NULL;
  2094. pos = os_strstr(cmd, " identifier=");
  2095. if (pos) {
  2096. pos += 12;
  2097. end = os_strchr(pos, ' ');
  2098. if (!end)
  2099. return -1;
  2100. wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
  2101. if (!wpa_s->dpp_pkex_identifier)
  2102. return -1;
  2103. os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
  2104. wpa_s->dpp_pkex_identifier[end - pos] = '\0';
  2105. }
  2106. pos = os_strstr(cmd, " code=");
  2107. if (!pos)
  2108. return -1;
  2109. os_free(wpa_s->dpp_pkex_code);
  2110. wpa_s->dpp_pkex_code = os_strdup(pos + 6);
  2111. if (!wpa_s->dpp_pkex_code)
  2112. return -1;
  2113. if (os_strstr(cmd, " init=1")) {
  2114. struct dpp_pkex *pkex;
  2115. struct wpabuf *msg;
  2116. wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
  2117. dpp_pkex_free(wpa_s->dpp_pkex);
  2118. wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
  2119. wpa_s->dpp_pkex_identifier,
  2120. wpa_s->dpp_pkex_code);
  2121. pkex = wpa_s->dpp_pkex;
  2122. if (!pkex)
  2123. return -1;
  2124. msg = pkex->exchange_req;
  2125. wait_time = wpa_s->max_remain_on_chan;
  2126. if (wait_time > 2000)
  2127. wait_time = 2000;
  2128. pkex->freq = 2437;
  2129. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  2130. " freq=%u type=%d",
  2131. MAC2STR(broadcast), pkex->freq,
  2132. DPP_PA_PKEX_EXCHANGE_REQ);
  2133. offchannel_send_action(wpa_s, pkex->freq, broadcast,
  2134. wpa_s->own_addr, broadcast,
  2135. wpabuf_head(msg), wpabuf_len(msg),
  2136. wait_time, wpas_dpp_tx_pkex_status, 0);
  2137. if (wait_time == 0)
  2138. wait_time = 2000;
  2139. pkex->exch_req_wait_time = wait_time;
  2140. pkex->exch_req_tries = 1;
  2141. }
  2142. /* TODO: Support multiple PKEX info entries */
  2143. os_free(wpa_s->dpp_pkex_auth_cmd);
  2144. wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
  2145. return 1;
  2146. }
  2147. int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
  2148. {
  2149. unsigned int id_val;
  2150. if (os_strcmp(id, "*") == 0) {
  2151. id_val = 0;
  2152. } else {
  2153. id_val = atoi(id);
  2154. if (id_val == 0)
  2155. return -1;
  2156. }
  2157. if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
  2158. return -1;
  2159. /* TODO: Support multiple PKEX entries */
  2160. os_free(wpa_s->dpp_pkex_code);
  2161. wpa_s->dpp_pkex_code = NULL;
  2162. os_free(wpa_s->dpp_pkex_identifier);
  2163. wpa_s->dpp_pkex_identifier = NULL;
  2164. os_free(wpa_s->dpp_pkex_auth_cmd);
  2165. wpa_s->dpp_pkex_auth_cmd = NULL;
  2166. wpa_s->dpp_pkex_bi = NULL;
  2167. /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
  2168. dpp_pkex_free(wpa_s->dpp_pkex);
  2169. wpa_s->dpp_pkex = NULL;
  2170. return 0;
  2171. }
  2172. void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
  2173. {
  2174. dpp_auth_deinit(wpa_s->dpp_auth);
  2175. wpa_s->dpp_auth = NULL;
  2176. dpp_pkex_free(wpa_s->dpp_pkex);
  2177. wpa_s->dpp_pkex = NULL;
  2178. if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
  2179. gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
  2180. }
  2181. int wpas_dpp_init(struct wpa_supplicant *wpa_s)
  2182. {
  2183. u8 adv_proto_id[7];
  2184. adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
  2185. adv_proto_id[1] = 5;
  2186. WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
  2187. adv_proto_id[5] = DPP_OUI_TYPE;
  2188. adv_proto_id[6] = 0x01;
  2189. if (gas_server_register(wpa_s->gas_server, adv_proto_id,
  2190. sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
  2191. wpas_dpp_gas_status_handler, wpa_s) < 0)
  2192. return -1;
  2193. dl_list_init(&wpa_s->dpp_bootstrap);
  2194. dl_list_init(&wpa_s->dpp_configurator);
  2195. wpa_s->dpp_init_done = 1;
  2196. return 0;
  2197. }
  2198. void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
  2199. {
  2200. #ifdef CONFIG_TESTING_OPTIONS
  2201. os_free(wpa_s->dpp_config_obj_override);
  2202. wpa_s->dpp_config_obj_override = NULL;
  2203. os_free(wpa_s->dpp_discovery_override);
  2204. wpa_s->dpp_discovery_override = NULL;
  2205. os_free(wpa_s->dpp_groups_override);
  2206. wpa_s->dpp_groups_override = NULL;
  2207. wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
  2208. #endif /* CONFIG_TESTING_OPTIONS */
  2209. if (!wpa_s->dpp_init_done)
  2210. return;
  2211. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  2212. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  2213. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  2214. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  2215. offchannel_send_action_done(wpa_s);
  2216. wpas_dpp_listen_stop(wpa_s);
  2217. dpp_bootstrap_del(wpa_s, 0);
  2218. dpp_configurator_del(wpa_s, 0);
  2219. wpas_dpp_stop(wpa_s);
  2220. wpas_dpp_pkex_remove(wpa_s, "*");
  2221. os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
  2222. os_free(wpa_s->dpp_configurator_params);
  2223. wpa_s->dpp_configurator_params = NULL;
  2224. }