dpp_supplicant.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  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. static void
  1949. wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
  1950. unsigned int freq, const u8 *dst,
  1951. const u8 *src, const u8 *bssid,
  1952. const u8 *data, size_t data_len,
  1953. enum offchannel_send_action_result result)
  1954. {
  1955. const char *res_txt;
  1956. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1957. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1958. "FAILED");
  1959. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1960. " result=%s (DPP Peer Discovery Request)",
  1961. freq, MAC2STR(dst), res_txt);
  1962. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  1963. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  1964. /* TODO: Time out wait for response more quickly in error cases? */
  1965. }
  1966. int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  1967. struct wpa_bss *bss)
  1968. {
  1969. struct os_time now;
  1970. struct wpabuf *msg;
  1971. unsigned int wait_time;
  1972. if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
  1973. return 0; /* Not using DPP AKM - continue */
  1974. if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
  1975. return 0; /* PMKSA exists for DPP AKM - continue */
  1976. if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1977. !ssid->dpp_csign) {
  1978. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1979. "missing %s",
  1980. !ssid->dpp_connector ? "Connector" :
  1981. (!ssid->dpp_netaccesskey ? "netAccessKey" :
  1982. "C-sign-key"));
  1983. return -1;
  1984. }
  1985. os_get_time(&now);
  1986. if (ssid->dpp_netaccesskey_expiry &&
  1987. (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
  1988. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1989. "netAccessKey expired");
  1990. return -1;
  1991. }
  1992. wpa_printf(MSG_DEBUG,
  1993. "DPP: Starting network introduction protocol to derive PMKSA for "
  1994. MACSTR, MAC2STR(bss->bssid));
  1995. msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
  1996. 5 + 4 + os_strlen(ssid->dpp_connector));
  1997. if (!msg)
  1998. return -1;
  1999. #ifdef CONFIG_TESTING_OPTIONS
  2000. if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
  2001. wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
  2002. goto skip_trans_id;
  2003. }
  2004. if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
  2005. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
  2006. wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
  2007. wpabuf_put_le16(msg, 0);
  2008. goto skip_trans_id;
  2009. }
  2010. #endif /* CONFIG_TESTING_OPTIONS */
  2011. /* Transaction ID */
  2012. wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
  2013. wpabuf_put_le16(msg, 1);
  2014. wpabuf_put_u8(msg, TRANSACTION_ID);
  2015. #ifdef CONFIG_TESTING_OPTIONS
  2016. skip_trans_id:
  2017. if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
  2018. wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
  2019. goto skip_connector;
  2020. }
  2021. if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
  2022. char *connector;
  2023. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
  2024. connector = dpp_corrupt_connector_signature(
  2025. ssid->dpp_connector);
  2026. if (!connector) {
  2027. wpabuf_free(msg);
  2028. return -1;
  2029. }
  2030. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  2031. wpabuf_put_le16(msg, os_strlen(connector));
  2032. wpabuf_put_str(msg, connector);
  2033. os_free(connector);
  2034. goto skip_connector;
  2035. }
  2036. #endif /* CONFIG_TESTING_OPTIONS */
  2037. /* DPP Connector */
  2038. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  2039. wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
  2040. wpabuf_put_str(msg, ssid->dpp_connector);
  2041. #ifdef CONFIG_TESTING_OPTIONS
  2042. skip_connector:
  2043. #endif /* CONFIG_TESTING_OPTIONS */
  2044. /* TODO: Timeout on AP response */
  2045. wait_time = wpa_s->max_remain_on_chan;
  2046. if (wait_time > 2000)
  2047. wait_time = 2000;
  2048. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  2049. MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
  2050. offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
  2051. broadcast,
  2052. wpabuf_head(msg), wpabuf_len(msg),
  2053. wait_time, wpas_dpp_tx_introduction_status, 0);
  2054. wpabuf_free(msg);
  2055. /* Request this connection attempt to terminate - new one will be
  2056. * started when network introduction protocol completes */
  2057. os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
  2058. wpa_s->dpp_intro_network = ssid;
  2059. return 1;
  2060. }
  2061. int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
  2062. {
  2063. struct dpp_bootstrap_info *own_bi;
  2064. const char *pos, *end;
  2065. unsigned int wait_time;
  2066. pos = os_strstr(cmd, " own=");
  2067. if (!pos)
  2068. return -1;
  2069. pos += 5;
  2070. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  2071. if (!own_bi) {
  2072. wpa_printf(MSG_DEBUG,
  2073. "DPP: Identified bootstrap info not found");
  2074. return -1;
  2075. }
  2076. if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
  2077. wpa_printf(MSG_DEBUG,
  2078. "DPP: Identified bootstrap info not for PKEX");
  2079. return -1;
  2080. }
  2081. wpa_s->dpp_pkex_bi = own_bi;
  2082. own_bi->pkex_t = 0; /* clear pending errors on new code */
  2083. os_free(wpa_s->dpp_pkex_identifier);
  2084. wpa_s->dpp_pkex_identifier = NULL;
  2085. pos = os_strstr(cmd, " identifier=");
  2086. if (pos) {
  2087. pos += 12;
  2088. end = os_strchr(pos, ' ');
  2089. if (!end)
  2090. return -1;
  2091. wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
  2092. if (!wpa_s->dpp_pkex_identifier)
  2093. return -1;
  2094. os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
  2095. wpa_s->dpp_pkex_identifier[end - pos] = '\0';
  2096. }
  2097. pos = os_strstr(cmd, " code=");
  2098. if (!pos)
  2099. return -1;
  2100. os_free(wpa_s->dpp_pkex_code);
  2101. wpa_s->dpp_pkex_code = os_strdup(pos + 6);
  2102. if (!wpa_s->dpp_pkex_code)
  2103. return -1;
  2104. if (os_strstr(cmd, " init=1")) {
  2105. struct dpp_pkex *pkex;
  2106. struct wpabuf *msg;
  2107. wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
  2108. dpp_pkex_free(wpa_s->dpp_pkex);
  2109. wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
  2110. wpa_s->dpp_pkex_identifier,
  2111. wpa_s->dpp_pkex_code);
  2112. pkex = wpa_s->dpp_pkex;
  2113. if (!pkex)
  2114. return -1;
  2115. msg = pkex->exchange_req;
  2116. wait_time = wpa_s->max_remain_on_chan;
  2117. if (wait_time > 2000)
  2118. wait_time = 2000;
  2119. pkex->freq = 2437;
  2120. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  2121. " freq=%u type=%d",
  2122. MAC2STR(broadcast), pkex->freq,
  2123. DPP_PA_PKEX_EXCHANGE_REQ);
  2124. offchannel_send_action(wpa_s, pkex->freq, broadcast,
  2125. wpa_s->own_addr, broadcast,
  2126. wpabuf_head(msg), wpabuf_len(msg),
  2127. wait_time, wpas_dpp_tx_pkex_status, 0);
  2128. if (wait_time == 0)
  2129. wait_time = 2000;
  2130. pkex->exch_req_wait_time = wait_time;
  2131. pkex->exch_req_tries = 1;
  2132. }
  2133. /* TODO: Support multiple PKEX info entries */
  2134. os_free(wpa_s->dpp_pkex_auth_cmd);
  2135. wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
  2136. return 1;
  2137. }
  2138. int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
  2139. {
  2140. unsigned int id_val;
  2141. if (os_strcmp(id, "*") == 0) {
  2142. id_val = 0;
  2143. } else {
  2144. id_val = atoi(id);
  2145. if (id_val == 0)
  2146. return -1;
  2147. }
  2148. if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
  2149. return -1;
  2150. /* TODO: Support multiple PKEX entries */
  2151. os_free(wpa_s->dpp_pkex_code);
  2152. wpa_s->dpp_pkex_code = NULL;
  2153. os_free(wpa_s->dpp_pkex_identifier);
  2154. wpa_s->dpp_pkex_identifier = NULL;
  2155. os_free(wpa_s->dpp_pkex_auth_cmd);
  2156. wpa_s->dpp_pkex_auth_cmd = NULL;
  2157. wpa_s->dpp_pkex_bi = NULL;
  2158. /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
  2159. dpp_pkex_free(wpa_s->dpp_pkex);
  2160. wpa_s->dpp_pkex = NULL;
  2161. return 0;
  2162. }
  2163. void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
  2164. {
  2165. dpp_auth_deinit(wpa_s->dpp_auth);
  2166. wpa_s->dpp_auth = NULL;
  2167. dpp_pkex_free(wpa_s->dpp_pkex);
  2168. wpa_s->dpp_pkex = NULL;
  2169. if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
  2170. gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
  2171. }
  2172. int wpas_dpp_init(struct wpa_supplicant *wpa_s)
  2173. {
  2174. u8 adv_proto_id[7];
  2175. adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
  2176. adv_proto_id[1] = 5;
  2177. WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
  2178. adv_proto_id[5] = DPP_OUI_TYPE;
  2179. adv_proto_id[6] = 0x01;
  2180. if (gas_server_register(wpa_s->gas_server, adv_proto_id,
  2181. sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
  2182. wpas_dpp_gas_status_handler, wpa_s) < 0)
  2183. return -1;
  2184. dl_list_init(&wpa_s->dpp_bootstrap);
  2185. dl_list_init(&wpa_s->dpp_configurator);
  2186. wpa_s->dpp_init_done = 1;
  2187. return 0;
  2188. }
  2189. void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
  2190. {
  2191. #ifdef CONFIG_TESTING_OPTIONS
  2192. os_free(wpa_s->dpp_config_obj_override);
  2193. wpa_s->dpp_config_obj_override = NULL;
  2194. os_free(wpa_s->dpp_discovery_override);
  2195. wpa_s->dpp_discovery_override = NULL;
  2196. os_free(wpa_s->dpp_groups_override);
  2197. wpa_s->dpp_groups_override = NULL;
  2198. wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
  2199. #endif /* CONFIG_TESTING_OPTIONS */
  2200. if (!wpa_s->dpp_init_done)
  2201. return;
  2202. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  2203. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  2204. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  2205. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  2206. offchannel_send_action_done(wpa_s);
  2207. wpas_dpp_listen_stop(wpa_s);
  2208. dpp_bootstrap_del(wpa_s, 0);
  2209. dpp_configurator_del(wpa_s, 0);
  2210. wpas_dpp_stop(wpa_s);
  2211. wpas_dpp_pkex_remove(wpa_s, "*");
  2212. os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
  2213. os_free(wpa_s->dpp_configurator_params);
  2214. wpa_s->dpp_configurator_params = NULL;
  2215. }