flooder.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /**
  2. * @file flooder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <protocol/addr.h>
  27. #include <protocol/scproto.h>
  28. #include <misc/loglevel.h>
  29. #include <misc/version.h>
  30. #include <misc/nsskey.h>
  31. #include <misc/byteorder.h>
  32. #include <misc/loggers_string.h>
  33. #include <misc/open_standard_streams.h>
  34. #include <base/BLog.h>
  35. #include <system/BReactor.h>
  36. #include <system/BSignal.h>
  37. #include <system/BNetwork.h>
  38. #include <flow/SinglePacketBuffer.h>
  39. #include <flow/PacketProtoEncoder.h>
  40. #include <nspr_support/BSSLConnection.h>
  41. #include <server_connection/ServerConnection.h>
  42. #ifndef BADVPN_USE_WINAPI
  43. #include <base/BLog_syslog.h>
  44. #endif
  45. #include <flooder/flooder.h>
  46. #include <generated/blog_channel_flooder.h>
  47. #define LOGGER_STDOUT 1
  48. #define LOGGER_SYSLOG 2
  49. // command-line options
  50. struct {
  51. int help;
  52. int version;
  53. int logger;
  54. #ifndef BADVPN_USE_WINAPI
  55. char *logger_syslog_facility;
  56. char *logger_syslog_ident;
  57. #endif
  58. int loglevel;
  59. int loglevels[BLOG_NUM_CHANNELS];
  60. int ssl;
  61. char *nssdb;
  62. char *client_cert_name;
  63. char *server_name;
  64. char *server_addr;
  65. peerid_t floods[MAX_FLOODS];
  66. int num_floods;
  67. } options;
  68. // server address we connect to
  69. BAddr server_addr;
  70. // server name to use for SSL
  71. char server_name[256];
  72. // reactor
  73. BReactor ss;
  74. // client certificate if using SSL
  75. CERTCertificate *client_cert;
  76. // client private key if using SSL
  77. SECKEYPrivateKey *client_key;
  78. // server connection
  79. ServerConnection server;
  80. // whether server is ready
  81. int server_ready;
  82. // my ID, defined only after server_ready
  83. peerid_t my_id;
  84. // flooding output
  85. PacketRecvInterface flood_source;
  86. PacketProtoEncoder flood_encoder;
  87. SinglePacketBuffer flood_buffer;
  88. // whether we were asked for a packet and blocked
  89. int flood_blocking;
  90. // index of next peer to send packet too
  91. int flood_next;
  92. /**
  93. * Cleans up everything that can be cleaned up from inside the event loop.
  94. */
  95. static void terminate (void);
  96. /**
  97. * Prints command line help.
  98. */
  99. static void print_help (const char *name);
  100. /**
  101. * Prints program name, version and copyright notice.
  102. */
  103. static void print_version (void);
  104. /**
  105. * Parses command line options into the options strucute.
  106. *
  107. * @return 1 on success, 0 on failure
  108. */
  109. static int parse_arguments (int argc, char *argv[]);
  110. /**
  111. * Processes command line options.
  112. *
  113. * @return 1 on success, 0 on failure
  114. */
  115. static int resolve_arguments (void);
  116. /**
  117. * Handler invoked when program termination is requested.
  118. */
  119. static void signal_handler (void *unused);
  120. static void server_handler_error (void *user);
  121. static void server_handler_ready (void *user, peerid_t param_my_id, uint32_t ext_ip);
  122. static void server_handler_newclient (void *user, peerid_t peer_id, int flags, const uint8_t *cert, int cert_len);
  123. static void server_handler_endclient (void *user, peerid_t peer_id);
  124. static void server_handler_message (void *user, peerid_t peer_id, uint8_t *data, int data_len);
  125. static void flood_source_handler_recv (void *user, uint8_t *data);
  126. int main (int argc, char *argv[])
  127. {
  128. if (argc <= 0) {
  129. return 1;
  130. }
  131. // open standard streams
  132. open_standard_streams();
  133. // parse command-line arguments
  134. if (!parse_arguments(argc, argv)) {
  135. fprintf(stderr, "Failed to parse arguments\n");
  136. print_help(argv[0]);
  137. goto fail0;
  138. }
  139. // handle --help and --version
  140. if (options.help) {
  141. print_version();
  142. print_help(argv[0]);
  143. return 0;
  144. }
  145. if (options.version) {
  146. print_version();
  147. return 0;
  148. }
  149. // initialize logger
  150. switch (options.logger) {
  151. case LOGGER_STDOUT:
  152. BLog_InitStdout();
  153. break;
  154. #ifndef BADVPN_USE_WINAPI
  155. case LOGGER_SYSLOG:
  156. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  157. fprintf(stderr, "Failed to initialize syslog logger\n");
  158. goto fail0;
  159. }
  160. break;
  161. #endif
  162. default:
  163. ASSERT(0);
  164. }
  165. // configure logger channels
  166. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  167. if (options.loglevels[i] >= 0) {
  168. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  169. }
  170. else if (options.loglevel >= 0) {
  171. BLog_SetChannelLoglevel(i, options.loglevel);
  172. }
  173. }
  174. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  175. // initialize network
  176. if (!BNetwork_GlobalInit()) {
  177. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  178. goto fail1;
  179. }
  180. // init time
  181. BTime_Init();
  182. // resolve addresses
  183. if (!resolve_arguments()) {
  184. BLog(BLOG_ERROR, "Failed to resolve arguments");
  185. goto fail1;
  186. }
  187. // init reactor
  188. if (!BReactor_Init(&ss)) {
  189. BLog(BLOG_ERROR, "BReactor_Init failed");
  190. goto fail1;
  191. }
  192. // setup signal handler
  193. if (!BSignal_Init(&ss, signal_handler, NULL)) {
  194. BLog(BLOG_ERROR, "BSignal_Init failed");
  195. goto fail1a;
  196. }
  197. if (options.ssl) {
  198. // init NSPR
  199. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  200. // register local NSPR file types
  201. if (!BSSLConnection_GlobalInit()) {
  202. BLog(BLOG_ERROR, "BSSLConnection_GlobalInit failed");
  203. goto fail3;
  204. }
  205. // init NSS
  206. if (NSS_Init(options.nssdb) != SECSuccess) {
  207. BLog(BLOG_ERROR, "NSS_Init failed (%d)", (int)PR_GetError());
  208. goto fail2;
  209. }
  210. // set cipher policy
  211. if (NSS_SetDomesticPolicy() != SECSuccess) {
  212. BLog(BLOG_ERROR, "NSS_SetDomesticPolicy failed (%d)", (int)PR_GetError());
  213. goto fail3;
  214. }
  215. // init server cache
  216. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  217. BLog(BLOG_ERROR, "SSL_ConfigServerSessionIDCache failed (%d)", (int)PR_GetError());
  218. goto fail3;
  219. }
  220. // open server certificate and private key
  221. if (!open_nss_cert_and_key(options.client_cert_name, &client_cert, &client_key)) {
  222. BLog(BLOG_ERROR, "Cannot open certificate and key");
  223. goto fail4;
  224. }
  225. }
  226. // start connecting to server
  227. if (!ServerConnection_Init(
  228. &server, &ss, server_addr, SC_KEEPALIVE_INTERVAL, SERVER_BUFFER_MIN_PACKETS, options.ssl, client_cert, client_key, server_name, NULL,
  229. server_handler_error, server_handler_ready, server_handler_newclient, server_handler_endclient, server_handler_message
  230. )) {
  231. BLog(BLOG_ERROR, "ServerConnection_Init failed");
  232. goto fail5;
  233. }
  234. // set server not ready
  235. server_ready = 0;
  236. // enter event loop
  237. BLog(BLOG_NOTICE, "entering event loop");
  238. BReactor_Exec(&ss);
  239. if (server_ready) {
  240. SinglePacketBuffer_Free(&flood_buffer);
  241. PacketProtoEncoder_Free(&flood_encoder);
  242. PacketRecvInterface_Free(&flood_source);
  243. }
  244. ServerConnection_Free(&server);
  245. fail5:
  246. if (options.ssl) {
  247. CERT_DestroyCertificate(client_cert);
  248. SECKEY_DestroyPrivateKey(client_key);
  249. fail4:
  250. ASSERT_FORCE(SSL_ShutdownServerSessionIDCache() == SECSuccess)
  251. fail3:
  252. SSL_ClearSessionCache();
  253. ASSERT_FORCE(NSS_Shutdown() == SECSuccess)
  254. fail2:
  255. ASSERT_FORCE(PR_Cleanup() == PR_SUCCESS)
  256. PL_ArenaFinish();
  257. }
  258. BSignal_Finish();
  259. fail1a:
  260. BReactor_Free(&ss);
  261. fail1:
  262. BLog(BLOG_NOTICE, "exiting");
  263. BLog_Free();
  264. fail0:
  265. DebugObjectGlobal_Finish();
  266. return 1;
  267. }
  268. void terminate (void)
  269. {
  270. BLog(BLOG_NOTICE, "tearing down");
  271. // exit event loop
  272. BReactor_Quit(&ss, 0);
  273. }
  274. void print_help (const char *name)
  275. {
  276. printf(
  277. "Usage:\n"
  278. " %s\n"
  279. " [--help]\n"
  280. " [--version]\n"
  281. " [--logger <"LOGGERS_STRING">]\n"
  282. #ifndef BADVPN_USE_WINAPI
  283. " (logger=syslog?\n"
  284. " [--syslog-facility <string>]\n"
  285. " [--syslog-ident <string>]\n"
  286. " )\n"
  287. #endif
  288. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  289. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  290. " [--ssl --nssdb <string> --client-cert-name <string>]\n"
  291. " [--server-name <string>]\n"
  292. " --server-addr <addr>\n"
  293. " [--flood-id <id>] ...\n"
  294. "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
  295. name
  296. );
  297. }
  298. void print_version (void)
  299. {
  300. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  301. }
  302. int parse_arguments (int argc, char *argv[])
  303. {
  304. if (argc <= 0) {
  305. return 0;
  306. }
  307. options.help = 0;
  308. options.version = 0;
  309. options.logger = LOGGER_STDOUT;
  310. #ifndef BADVPN_USE_WINAPI
  311. options.logger_syslog_facility = "daemon";
  312. options.logger_syslog_ident = argv[0];
  313. #endif
  314. options.loglevel = -1;
  315. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  316. options.loglevels[i] = -1;
  317. }
  318. options.ssl = 0;
  319. options.nssdb = NULL;
  320. options.client_cert_name = NULL;
  321. options.server_name = NULL;
  322. options.server_addr = NULL;
  323. options.num_floods = 0;
  324. int i;
  325. for (i = 1; i < argc; i++) {
  326. char *arg = argv[i];
  327. if (!strcmp(arg, "--help")) {
  328. options.help = 1;
  329. }
  330. else if (!strcmp(arg, "--version")) {
  331. options.version = 1;
  332. }
  333. else if (!strcmp(arg, "--logger")) {
  334. if (1 >= argc - i) {
  335. fprintf(stderr, "%s: requires an argument\n", arg);
  336. return 0;
  337. }
  338. char *arg2 = argv[i + 1];
  339. if (!strcmp(arg2, "stdout")) {
  340. options.logger = LOGGER_STDOUT;
  341. }
  342. #ifndef BADVPN_USE_WINAPI
  343. else if (!strcmp(arg2, "syslog")) {
  344. options.logger = LOGGER_SYSLOG;
  345. }
  346. #endif
  347. else {
  348. fprintf(stderr, "%s: wrong argument\n", arg);
  349. return 0;
  350. }
  351. i++;
  352. }
  353. #ifndef BADVPN_USE_WINAPI
  354. else if (!strcmp(arg, "--syslog-facility")) {
  355. if (1 >= argc - i) {
  356. fprintf(stderr, "%s: requires an argument\n", arg);
  357. return 0;
  358. }
  359. options.logger_syslog_facility = argv[i + 1];
  360. i++;
  361. }
  362. else if (!strcmp(arg, "--syslog-ident")) {
  363. if (1 >= argc - i) {
  364. fprintf(stderr, "%s: requires an argument\n", arg);
  365. return 0;
  366. }
  367. options.logger_syslog_ident = argv[i + 1];
  368. i++;
  369. }
  370. #endif
  371. else if (!strcmp(arg, "--loglevel")) {
  372. if (1 >= argc - i) {
  373. fprintf(stderr, "%s: requires an argument\n", arg);
  374. return 0;
  375. }
  376. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  377. fprintf(stderr, "%s: wrong argument\n", arg);
  378. return 0;
  379. }
  380. i++;
  381. }
  382. else if (!strcmp(arg, "--channel-loglevel")) {
  383. if (2 >= argc - i) {
  384. fprintf(stderr, "%s: requires two arguments\n", arg);
  385. return 0;
  386. }
  387. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  388. if (channel < 0) {
  389. fprintf(stderr, "%s: wrong channel argument\n", arg);
  390. return 0;
  391. }
  392. int loglevel = parse_loglevel(argv[i + 2]);
  393. if (loglevel < 0) {
  394. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  395. return 0;
  396. }
  397. options.loglevels[channel] = loglevel;
  398. i += 2;
  399. }
  400. else if (!strcmp(arg, "--ssl")) {
  401. options.ssl = 1;
  402. }
  403. else if (!strcmp(arg, "--nssdb")) {
  404. if (1 >= argc - i) {
  405. fprintf(stderr, "%s: requires an argument\n", arg);
  406. return 0;
  407. }
  408. options.nssdb = argv[i + 1];
  409. i++;
  410. }
  411. else if (!strcmp(arg, "--client-cert-name")) {
  412. if (1 >= argc - i) {
  413. fprintf(stderr, "%s: requires an argument\n", arg);
  414. return 0;
  415. }
  416. options.client_cert_name = argv[i + 1];
  417. i++;
  418. }
  419. else if (!strcmp(arg, "--server-name")) {
  420. if (1 >= argc - i) {
  421. fprintf(stderr, "%s: requires an argument\n", arg);
  422. return 0;
  423. }
  424. options.server_name = argv[i + 1];
  425. i++;
  426. }
  427. else if (!strcmp(arg, "--server-addr")) {
  428. if (1 >= argc - i) {
  429. fprintf(stderr, "%s: requires an argument\n", arg);
  430. return 0;
  431. }
  432. options.server_addr = argv[i + 1];
  433. i++;
  434. }
  435. else if (!strcmp(arg, "--flood-id")) {
  436. if (1 >= argc - i) {
  437. fprintf(stderr, "%s: requires an argument\n", arg);
  438. return 0;
  439. }
  440. if (options.num_floods == MAX_FLOODS) {
  441. fprintf(stderr, "%s: too many\n", arg);
  442. return 0;
  443. }
  444. options.floods[options.num_floods] = atoi(argv[i + 1]);
  445. options.num_floods++;
  446. i++;
  447. }
  448. else {
  449. fprintf(stderr, "unknown option: %s\n", arg);
  450. return 0;
  451. }
  452. }
  453. if (options.help || options.version) {
  454. return 1;
  455. }
  456. if (options.ssl != !!options.nssdb) {
  457. fprintf(stderr, "False: --ssl <=> --nssdb\n");
  458. return 0;
  459. }
  460. if (options.ssl != !!options.client_cert_name) {
  461. fprintf(stderr, "False: --ssl <=> --client-cert-name\n");
  462. return 0;
  463. }
  464. if (!options.server_addr) {
  465. fprintf(stderr, "False: --server-addr\n");
  466. return 0;
  467. }
  468. return 1;
  469. }
  470. int resolve_arguments (void)
  471. {
  472. // resolve server address
  473. ASSERT(options.server_addr)
  474. if (!BAddr_Parse(&server_addr, options.server_addr, server_name, sizeof(server_name))) {
  475. BLog(BLOG_ERROR, "server addr: BAddr_Parse failed");
  476. return 0;
  477. }
  478. if (!addr_supported(server_addr)) {
  479. BLog(BLOG_ERROR, "server addr: not supported");
  480. return 0;
  481. }
  482. // override server name if requested
  483. if (options.server_name) {
  484. snprintf(server_name, sizeof(server_name), "%s", options.server_name);
  485. }
  486. return 1;
  487. }
  488. void signal_handler (void *unused)
  489. {
  490. BLog(BLOG_NOTICE, "termination requested");
  491. terminate();
  492. }
  493. void server_handler_error (void *user)
  494. {
  495. BLog(BLOG_ERROR, "server connection failed, exiting");
  496. terminate();
  497. }
  498. void server_handler_ready (void *user, peerid_t param_my_id, uint32_t ext_ip)
  499. {
  500. ASSERT(!server_ready)
  501. // remember our ID
  502. my_id = param_my_id;
  503. // init flooding
  504. // init source
  505. PacketRecvInterface_Init(&flood_source, SC_MAX_ENC, flood_source_handler_recv, NULL, BReactor_PendingGroup(&ss));
  506. // init encoder
  507. PacketProtoEncoder_Init(&flood_encoder, &flood_source, BReactor_PendingGroup(&ss));
  508. // init buffer
  509. if (!SinglePacketBuffer_Init(&flood_buffer, PacketProtoEncoder_GetOutput(&flood_encoder), ServerConnection_GetSendInterface(&server), BReactor_PendingGroup(&ss))) {
  510. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed, exiting");
  511. goto fail1;
  512. }
  513. // set not blocking
  514. flood_blocking = 0;
  515. // set server ready
  516. server_ready = 1;
  517. BLog(BLOG_INFO, "server: ready, my ID is %d", (int)my_id);
  518. return;
  519. fail1:
  520. PacketProtoEncoder_Free(&flood_encoder);
  521. PacketRecvInterface_Free(&flood_source);
  522. terminate();
  523. }
  524. void server_handler_newclient (void *user, peerid_t peer_id, int flags, const uint8_t *cert, int cert_len)
  525. {
  526. ASSERT(server_ready)
  527. BLog(BLOG_INFO, "newclient %d", (int)peer_id);
  528. }
  529. void server_handler_endclient (void *user, peerid_t peer_id)
  530. {
  531. ASSERT(server_ready)
  532. BLog(BLOG_INFO, "endclient %d", (int)peer_id);
  533. }
  534. void server_handler_message (void *user, peerid_t peer_id, uint8_t *data, int data_len)
  535. {
  536. ASSERT(server_ready)
  537. ASSERT(data_len >= 0)
  538. ASSERT(data_len <= SC_MAX_MSGLEN)
  539. BLog(BLOG_INFO, "message from %d", (int)peer_id);
  540. }
  541. void flood_source_handler_recv (void *user, uint8_t *data)
  542. {
  543. ASSERT(server_ready)
  544. ASSERT(!flood_blocking)
  545. if (options.num_floods > 0) {
  546. ASSERT(flood_next >= 0)
  547. ASSERT(flood_next < options.num_floods)
  548. }
  549. if (options.num_floods == 0) {
  550. flood_blocking = 1;
  551. return;
  552. }
  553. peerid_t peer_id = options.floods[flood_next];
  554. flood_next = (flood_next + 1) % options.num_floods;
  555. BLog(BLOG_INFO, "message to %d", (int)peer_id);
  556. struct sc_header *header = (struct sc_header *)data;
  557. header->type = SCID_OUTMSG;
  558. struct sc_client_outmsg *msg = (struct sc_client_outmsg *)(data + sizeof(struct sc_header));
  559. msg->clientid = htol16(peer_id);
  560. memset(data + sizeof(struct sc_header) + sizeof(struct sc_client_outmsg), 0, SC_MAX_MSGLEN);
  561. PacketRecvInterface_Done(&flood_source, sizeof(struct sc_header) + sizeof(struct sc_client_outmsg) + SC_MAX_MSGLEN);
  562. }