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 <base/BLog.h>
  34. #include <system/BReactor.h>
  35. #include <system/BSignal.h>
  36. #include <flow/SinglePacketBuffer.h>
  37. #include <flow/PacketProtoEncoder.h>
  38. #include <nspr_support/DummyPRFileDesc.h>
  39. #include <nspr_support/BSocketPRFileDesc.h>
  40. #include <server_connection/ServerConnection.h>
  41. #ifndef BADVPN_USE_WINAPI
  42. #include <system/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 sockets
  173. if (BSocket_GlobalInit() < 0) {
  174. BLog(BLOG_ERROR, "BSocket_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 (!DummyPRFileDesc_GlobalInit()) {
  199. BLog(BLOG_ERROR, "DummyPRFileDesc_GlobalInit failed");
  200. goto fail2;
  201. }
  202. if (!BSocketPRFileDesc_GlobalInit()) {
  203. BLog(BLOG_ERROR, "BSocketPRFileDesc_GlobalInit failed");
  204. goto fail2;
  205. }
  206. // init NSS
  207. if (NSS_Init(options.nssdb) != SECSuccess) {
  208. BLog(BLOG_ERROR, "NSS_Init failed (%d)", (int)PR_GetError());
  209. goto fail2;
  210. }
  211. // set cipher policy
  212. if (NSS_SetDomesticPolicy() != SECSuccess) {
  213. BLog(BLOG_ERROR, "NSS_SetDomesticPolicy failed (%d)", (int)PR_GetError());
  214. goto fail3;
  215. }
  216. // init server cache
  217. if (SSL_ConfigServerSessionIDCache(0, 0, 0, NULL) != SECSuccess) {
  218. BLog(BLOG_ERROR, "SSL_ConfigServerSessionIDCache failed (%d)", (int)PR_GetError());
  219. goto fail3;
  220. }
  221. // open server certificate and private key
  222. if (!open_nss_cert_and_key(options.client_cert_name, &client_cert, &client_key)) {
  223. BLog(BLOG_ERROR, "Cannot open certificate and key");
  224. goto fail4;
  225. }
  226. }
  227. // start connecting to server
  228. if (!ServerConnection_Init(
  229. &server, &ss, server_addr, SC_KEEPALIVE_INTERVAL, SERVER_BUFFER_MIN_PACKETS, options.ssl, client_cert, client_key, server_name, NULL,
  230. server_handler_error, server_handler_ready, server_handler_newclient, server_handler_endclient, server_handler_message
  231. )) {
  232. BLog(BLOG_ERROR, "ServerConnection_Init failed");
  233. goto fail5;
  234. }
  235. // set server not ready
  236. server_ready = 0;
  237. // enter event loop
  238. BLog(BLOG_NOTICE, "entering event loop");
  239. BReactor_Exec(&ss);
  240. if (server_ready) {
  241. SinglePacketBuffer_Free(&flood_buffer);
  242. PacketProtoEncoder_Free(&flood_encoder);
  243. PacketRecvInterface_Free(&flood_source);
  244. }
  245. ServerConnection_Free(&server);
  246. fail5:
  247. if (options.ssl) {
  248. CERT_DestroyCertificate(client_cert);
  249. SECKEY_DestroyPrivateKey(client_key);
  250. fail4:
  251. ASSERT_FORCE(SSL_ShutdownServerSessionIDCache() == SECSuccess)
  252. fail3:
  253. SSL_ClearSessionCache();
  254. ASSERT_FORCE(NSS_Shutdown() == SECSuccess)
  255. fail2:
  256. ASSERT_FORCE(PR_Cleanup() == PR_SUCCESS)
  257. PL_ArenaFinish();
  258. }
  259. BSignal_Finish();
  260. fail1a:
  261. BReactor_Free(&ss);
  262. fail1:
  263. BLog(BLOG_NOTICE, "exiting");
  264. BLog_Free();
  265. fail0:
  266. DebugObjectGlobal_Finish();
  267. return 1;
  268. }
  269. void terminate (void)
  270. {
  271. BLog(BLOG_NOTICE, "tearing down");
  272. // exit event loop
  273. BReactor_Quit(&ss, 0);
  274. }
  275. void print_help (const char *name)
  276. {
  277. printf(
  278. "Usage:\n"
  279. " %s\n"
  280. " [--help]\n"
  281. " [--version]\n"
  282. " [--logger <"LOGGERS_STRING">]\n"
  283. #ifndef BADVPN_USE_WINAPI
  284. " (logger=syslog?\n"
  285. " [--syslog-facility <string>]\n"
  286. " [--syslog-ident <string>]\n"
  287. " )\n"
  288. #endif
  289. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  290. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  291. " [--ssl --nssdb <string> --client-cert-name <string>]\n"
  292. " [--server-name <string>]\n"
  293. " --server-addr <addr>\n"
  294. " [--flood-id <id>] ...\n"
  295. "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
  296. name
  297. );
  298. }
  299. void print_version (void)
  300. {
  301. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  302. }
  303. int parse_arguments (int argc, char *argv[])
  304. {
  305. if (argc <= 0) {
  306. return 0;
  307. }
  308. options.help = 0;
  309. options.version = 0;
  310. options.logger = LOGGER_STDOUT;
  311. #ifndef BADVPN_USE_WINAPI
  312. options.logger_syslog_facility = "daemon";
  313. options.logger_syslog_ident = argv[0];
  314. #endif
  315. options.loglevel = -1;
  316. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  317. options.loglevels[i] = -1;
  318. }
  319. options.ssl = 0;
  320. options.nssdb = NULL;
  321. options.client_cert_name = NULL;
  322. options.server_name = NULL;
  323. options.server_addr = NULL;
  324. options.num_floods = 0;
  325. int i;
  326. for (i = 1; i < argc; i++) {
  327. char *arg = argv[i];
  328. if (!strcmp(arg, "--help")) {
  329. options.help = 1;
  330. }
  331. else if (!strcmp(arg, "--version")) {
  332. options.version = 1;
  333. }
  334. else if (!strcmp(arg, "--logger")) {
  335. if (1 >= argc - i) {
  336. fprintf(stderr, "%s: requires an argument\n", arg);
  337. return 0;
  338. }
  339. char *arg2 = argv[i + 1];
  340. if (!strcmp(arg2, "stdout")) {
  341. options.logger = LOGGER_STDOUT;
  342. }
  343. #ifndef BADVPN_USE_WINAPI
  344. else if (!strcmp(arg2, "syslog")) {
  345. options.logger = LOGGER_SYSLOG;
  346. }
  347. #endif
  348. else {
  349. fprintf(stderr, "%s: wrong argument\n", arg);
  350. return 0;
  351. }
  352. i++;
  353. }
  354. #ifndef BADVPN_USE_WINAPI
  355. else if (!strcmp(arg, "--syslog-facility")) {
  356. if (1 >= argc - i) {
  357. fprintf(stderr, "%s: requires an argument\n", arg);
  358. return 0;
  359. }
  360. options.logger_syslog_facility = argv[i + 1];
  361. i++;
  362. }
  363. else if (!strcmp(arg, "--syslog-ident")) {
  364. if (1 >= argc - i) {
  365. fprintf(stderr, "%s: requires an argument\n", arg);
  366. return 0;
  367. }
  368. options.logger_syslog_ident = argv[i + 1];
  369. i++;
  370. }
  371. #endif
  372. else if (!strcmp(arg, "--loglevel")) {
  373. if (1 >= argc - i) {
  374. fprintf(stderr, "%s: requires an argument\n", arg);
  375. return 0;
  376. }
  377. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  378. fprintf(stderr, "%s: wrong argument\n", arg);
  379. return 0;
  380. }
  381. i++;
  382. }
  383. else if (!strcmp(arg, "--channel-loglevel")) {
  384. if (2 >= argc - i) {
  385. fprintf(stderr, "%s: requires two arguments\n", arg);
  386. return 0;
  387. }
  388. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  389. if (channel < 0) {
  390. fprintf(stderr, "%s: wrong channel argument\n", arg);
  391. return 0;
  392. }
  393. int loglevel = parse_loglevel(argv[i + 2]);
  394. if (loglevel < 0) {
  395. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  396. return 0;
  397. }
  398. options.loglevels[channel] = loglevel;
  399. i += 2;
  400. }
  401. else if (!strcmp(arg, "--ssl")) {
  402. options.ssl = 1;
  403. }
  404. else if (!strcmp(arg, "--nssdb")) {
  405. if (1 >= argc - i) {
  406. fprintf(stderr, "%s: requires an argument\n", arg);
  407. return 0;
  408. }
  409. options.nssdb = argv[i + 1];
  410. i++;
  411. }
  412. else if (!strcmp(arg, "--client-cert-name")) {
  413. if (1 >= argc - i) {
  414. fprintf(stderr, "%s: requires an argument\n", arg);
  415. return 0;
  416. }
  417. options.client_cert_name = argv[i + 1];
  418. i++;
  419. }
  420. else if (!strcmp(arg, "--server-name")) {
  421. if (1 >= argc - i) {
  422. fprintf(stderr, "%s: requires an argument\n", arg);
  423. return 0;
  424. }
  425. options.server_name = argv[i + 1];
  426. i++;
  427. }
  428. else if (!strcmp(arg, "--server-addr")) {
  429. if (1 >= argc - i) {
  430. fprintf(stderr, "%s: requires an argument\n", arg);
  431. return 0;
  432. }
  433. options.server_addr = argv[i + 1];
  434. i++;
  435. }
  436. else if (!strcmp(arg, "--flood-id")) {
  437. if (1 >= argc - i) {
  438. fprintf(stderr, "%s: requires an argument\n", arg);
  439. return 0;
  440. }
  441. if (options.num_floods == MAX_FLOODS) {
  442. fprintf(stderr, "%s: too many\n", arg);
  443. return 0;
  444. }
  445. options.floods[options.num_floods] = atoi(argv[i + 1]);
  446. options.num_floods++;
  447. i++;
  448. }
  449. else {
  450. fprintf(stderr, "unknown option: %s\n", arg);
  451. return 0;
  452. }
  453. }
  454. if (options.help || options.version) {
  455. return 1;
  456. }
  457. if (options.ssl != !!options.nssdb) {
  458. fprintf(stderr, "False: --ssl <=> --nssdb\n");
  459. return 0;
  460. }
  461. if (options.ssl != !!options.client_cert_name) {
  462. fprintf(stderr, "False: --ssl <=> --client-cert-name\n");
  463. return 0;
  464. }
  465. if (!options.server_addr) {
  466. fprintf(stderr, "False: --server-addr\n");
  467. return 0;
  468. }
  469. return 1;
  470. }
  471. int resolve_arguments (void)
  472. {
  473. // resolve server address
  474. ASSERT(options.server_addr)
  475. if (!BAddr_Parse(&server_addr, options.server_addr, server_name, sizeof(server_name))) {
  476. BLog(BLOG_ERROR, "server addr: BAddr_Parse failed");
  477. return 0;
  478. }
  479. if (!addr_supported(server_addr)) {
  480. BLog(BLOG_ERROR, "server addr: not supported");
  481. return 0;
  482. }
  483. // override server name if requested
  484. if (options.server_name) {
  485. snprintf(server_name, sizeof(server_name), "%s", options.server_name);
  486. }
  487. return 1;
  488. }
  489. void signal_handler (void *unused)
  490. {
  491. BLog(BLOG_NOTICE, "termination requested");
  492. terminate();
  493. }
  494. void server_handler_error (void *user)
  495. {
  496. BLog(BLOG_ERROR, "server connection failed, exiting");
  497. terminate();
  498. }
  499. void server_handler_ready (void *user, peerid_t param_my_id, uint32_t ext_ip)
  500. {
  501. ASSERT(!server_ready)
  502. // remember our ID
  503. my_id = param_my_id;
  504. // init flooding
  505. // init source
  506. PacketRecvInterface_Init(&flood_source, SC_MAX_ENC, flood_source_handler_recv, NULL, BReactor_PendingGroup(&ss));
  507. // init encoder
  508. PacketProtoEncoder_Init(&flood_encoder, &flood_source, BReactor_PendingGroup(&ss));
  509. // init buffer
  510. if (!SinglePacketBuffer_Init(&flood_buffer, PacketProtoEncoder_GetOutput(&flood_encoder), ServerConnection_GetSendInterface(&server), BReactor_PendingGroup(&ss))) {
  511. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed, exiting");
  512. goto fail1;
  513. }
  514. // set not blocking
  515. flood_blocking = 0;
  516. // set server ready
  517. server_ready = 1;
  518. BLog(BLOG_INFO, "server: ready, my ID is %d", (int)my_id);
  519. return;
  520. fail1:
  521. PacketProtoEncoder_Free(&flood_encoder);
  522. PacketRecvInterface_Free(&flood_source);
  523. terminate();
  524. }
  525. void server_handler_newclient (void *user, peerid_t peer_id, int flags, const uint8_t *cert, int cert_len)
  526. {
  527. ASSERT(server_ready)
  528. BLog(BLOG_INFO, "newclient %d", (int)peer_id);
  529. }
  530. void server_handler_endclient (void *user, peerid_t peer_id)
  531. {
  532. ASSERT(server_ready)
  533. BLog(BLOG_INFO, "endclient %d", (int)peer_id);
  534. }
  535. void server_handler_message (void *user, peerid_t peer_id, uint8_t *data, int data_len)
  536. {
  537. ASSERT(server_ready)
  538. ASSERT(data_len >= 0)
  539. ASSERT(data_len <= SC_MAX_MSGLEN)
  540. BLog(BLOG_INFO, "message from %d", (int)peer_id);
  541. }
  542. void flood_source_handler_recv (void *user, uint8_t *data)
  543. {
  544. ASSERT(server_ready)
  545. ASSERT(!flood_blocking)
  546. if (options.num_floods > 0) {
  547. ASSERT(flood_next >= 0)
  548. ASSERT(flood_next < options.num_floods)
  549. }
  550. if (options.num_floods == 0) {
  551. flood_blocking = 1;
  552. return;
  553. }
  554. peerid_t peer_id = options.floods[flood_next];
  555. flood_next = (flood_next + 1) % options.num_floods;
  556. BLog(BLOG_INFO, "message to %d", (int)peer_id);
  557. struct sc_header *header = (struct sc_header *)data;
  558. header->type = SCID_OUTMSG;
  559. struct sc_client_outmsg *msg = (struct sc_client_outmsg *)(data + sizeof(struct sc_header));
  560. msg->clientid = htol16(peer_id);
  561. memset(data + sizeof(struct sc_header) + sizeof(struct sc_client_outmsg), 0, SC_MAX_MSGLEN);
  562. PacketRecvInterface_Done(&flood_source, sizeof(struct sc_header) + sizeof(struct sc_client_outmsg) + SC_MAX_MSGLEN);
  563. }