flooder.c 18 KB

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