CVE-2021-23192 librpc: Remove the gensec dependency from library dcerpc-binding
[Samba.git] / ctdb / tools / ctdb_killtcp.c
blobbab81092058a666e2d5817347b0aa2d80e38f501
1 /*
2 CTDB TCP connection killing utility
4 Copyright (C) Martin Schwenke <martin@meltin.net> 2016
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "system/network.h"
23 #include <talloc.h>
24 #include <tevent.h>
26 #include "lib/util/debug.h"
27 #include "lib/util/tevent_unix.h"
29 #include "protocol/protocol.h"
30 #include "protocol/protocol_util.h"
32 #include "common/db_hash.h"
33 #include "common/system_socket.h"
34 #include "common/logging.h"
37 struct reset_connections_state {
38 struct tevent_context *ev;
39 int capture_fd;
40 struct tevent_fd *fde;
41 struct db_hash_context *connections;
42 void *private_data;
43 unsigned int attempts;
44 unsigned int max_attempts;
45 struct timeval retry_interval;
46 unsigned int batch_count;
47 unsigned int batch_size;
51 static void reset_connections_capture_tcp_handler(struct tevent_context *ev,
52 struct tevent_fd *fde,
53 uint16_t flags,
54 void *private_data);
55 static void reset_connections_batch(struct tevent_req *subreq);
56 static int reset_connections_tickle_connection(
57 uint8_t *keybuf, size_t keylen,
58 uint8_t *databuf, size_t datalen,
59 void *private_data);
61 static struct tevent_req *reset_connections_send(
62 TALLOC_CTX *mem_ctx,
63 struct tevent_context *ev,
64 const char *iface,
65 struct ctdb_connection_list *conn_list)
67 struct tevent_req *req, *subreq;
68 struct reset_connections_state *state;
69 unsigned int i;
70 int ret;
72 req = tevent_req_create(mem_ctx, &state,
73 struct reset_connections_state);
74 if (req == NULL) {
75 return NULL;
78 state->ev = ev;
80 if (conn_list->num == 0) {
81 /* No connections, done! */
82 tevent_req_done(req);
83 return tevent_req_post(req, ev);
86 ret = db_hash_init(state, "connections", 2048, DB_HASH_SIMPLE,
87 &state->connections);
88 if (ret != 0) {
89 D_ERR("Failed to initialise connection hash (%s)\n",
90 strerror(ret));
91 tevent_req_error(req, ret);
92 return tevent_req_post(req, ev);
95 DBG_DEBUG("Adding %u connections to hash\n", conn_list->num);
96 for (i = 0; i < conn_list->num; i++) {
97 struct ctdb_connection *c = &conn_list->conn[i];
99 DBG_DEBUG("Adding connection to hash: %s\n",
100 ctdb_connection_to_string(conn_list, c, true));
102 /* Connection is stored as a key in the connections hash */
103 ret = db_hash_add(state->connections,
104 (uint8_t *)discard_const(c), sizeof(*c),
105 NULL, 0);
106 if (ret != 0) {
107 D_ERR("Error adding connection to hash (%s)\n",
108 strerror(ret));
109 tevent_req_error(req, ret);
110 return tevent_req_post(req, ev);
114 state->attempts = 0;
115 state->max_attempts = 50;
117 state->retry_interval.tv_sec = 0;
118 state->retry_interval.tv_usec = 100 * 1000;
120 state->batch_count = 0;
121 state->batch_size = 300;
123 state->capture_fd =
124 ctdb_sys_open_capture_socket(iface, &state->private_data);
125 if (state->capture_fd == -1) {
126 D_ERR("Failed to open capture socket on iface '%s' (%s)\n",
127 iface, strerror(errno));
128 tevent_req_error(req, EIO);
129 return tevent_req_post(req, ev);
132 state->fde = tevent_add_fd(ev, state, state->capture_fd,
133 TEVENT_FD_READ,
134 reset_connections_capture_tcp_handler,
135 state);
136 if (tevent_req_nomem(state->fde, req)) {
137 return tevent_req_post(req, ev);
139 tevent_fd_set_auto_close(state->fde);
141 subreq = tevent_wakeup_send(state, ev, tevent_timeval_current_ofs(0,0));
142 if (tevent_req_nomem(subreq, req)) {
143 return tevent_req_post(req, ev);
145 tevent_req_set_callback(subreq, reset_connections_batch, req);
147 return req;
151 called when we get a read event on the raw socket
153 static void reset_connections_capture_tcp_handler(struct tevent_context *ev,
154 struct tevent_fd *fde,
155 uint16_t flags,
156 void *private_data)
158 struct reset_connections_state *state = talloc_get_type_abort(
159 private_data, struct reset_connections_state);
160 /* 0 the parts that don't get set by ctdb_sys_read_tcp_packet */
161 struct ctdb_connection conn;
162 uint32_t ack_seq, seq;
163 int rst;
164 uint16_t window;
165 int ret;
167 ret = ctdb_sys_read_tcp_packet(state->capture_fd,
168 state->private_data,
169 &conn.server, &conn.client,
170 &ack_seq, &seq, &rst, &window);
171 if (ret != 0) {
172 /* probably a non-tcp ACK packet */
173 return;
176 if (window == htons(1234) && (rst || seq == 0)) {
177 /* Ignore packets that we sent! */
178 D_DEBUG("Ignoring packet: %s, "
179 "seq=%"PRIu32", ack_seq=%"PRIu32", "
180 "rst=%d, window=%"PRIu16"\n",
181 ctdb_connection_to_string(state, &conn, false),
182 seq, ack_seq, rst, ntohs(window));
183 return;
186 /* Check if this connection is one being reset, if found then delete */
187 ret = db_hash_delete(state->connections,
188 (uint8_t*)&conn, sizeof(conn));
189 if (ret == ENOENT) {
190 /* Packet for some other connection, ignore */
191 DBG_DEBUG("Ignoring packet for unknown connection: %s\n",
192 ctdb_connection_to_string(state, &conn, true));
193 return;
195 if (ret != 0) {
196 DBG_WARNING("Internal error (%s)\n", strerror(ret));
197 return;
200 D_INFO("Sending a TCP RST to for connection %s\n",
201 ctdb_connection_to_string(state, &conn, true));
203 ret = ctdb_sys_send_tcp(&conn.server, &conn.client, ack_seq, seq, 1);
204 if (ret != 0) {
205 DBG_ERR("Error sending TCP RST for connection\n");
210 * Called periodically until all sentenced connections have been reset
211 * or enough attempts have been made
213 static void reset_connections_batch(struct tevent_req *subreq)
215 struct tevent_req *req = tevent_req_callback_data(
216 subreq, struct tevent_req);
217 struct reset_connections_state *state = tevent_req_data(
218 req, struct reset_connections_state);
219 bool status;
220 int count, ret;
222 status = tevent_wakeup_recv(subreq);
223 TALLOC_FREE(subreq);
225 if (! status) {
226 DBG_WARNING("Unexpected error on timer expiry\n");
227 /* Keep going... */
230 /* loop over up to batch_size connections sending tickle ACKs */
231 state->batch_count = 0;
232 ret = db_hash_traverse(state->connections,
233 reset_connections_tickle_connection,
234 state, NULL);
235 if (ret != 0) {
236 DBG_WARNING("Unexpected error traversing connections (%s)\n",
237 strerror(ret));
240 state->attempts++;
243 * If there are no more connections to kill or we have tried
244 * too many times we're finished
246 ret = db_hash_traverse(state->connections, NULL, NULL, &count);
247 if (ret != 0) {
248 /* What now? Try again until max_attempts reached */
249 DBG_WARNING("Unexpected error traversing connections (%s)\n",
250 strerror(ret));
251 count = 1;
253 if (count == 0 ||
254 state->attempts >= state->max_attempts) {
255 tevent_req_done(req);
256 return;
259 /* Schedule next attempt */
260 subreq = tevent_wakeup_send(state, state->ev,
261 tevent_timeval_current_ofs(
262 state->retry_interval.tv_sec,
263 state->retry_interval.tv_usec));
264 if (tevent_req_nomem(subreq, req)) {
265 return;
267 tevent_req_set_callback(subreq, reset_connections_batch, req);
270 static int reset_connections_tickle_connection(
271 uint8_t *keybuf, size_t keylen,
272 uint8_t *databuf, size_t datalen,
273 void *private_data)
275 struct reset_connections_state *state = talloc_get_type_abort(
276 private_data, struct reset_connections_state);
277 struct ctdb_connection *conn;
278 int ret;
280 if (keylen != sizeof(*conn)) {
281 DBG_WARNING("Unexpected data in connection hash\n");
282 return 0;
285 conn = (struct ctdb_connection *)keybuf;
287 state->batch_count++;
288 if (state->batch_count > state->batch_size) {
289 /* Terminate the traverse */
290 return 1;
293 DBG_DEBUG("Sending tickle ACK for connection '%s'\n",
294 ctdb_connection_to_string(state, conn, true));
295 ret = ctdb_sys_send_tcp(&conn->server, &conn->client, 0, 0, 0);
296 if (ret != 0) {
297 DBG_ERR("Error sending tickle ACK\n");
298 /* continue */
301 return 0;
304 static bool reset_connections_recv(struct tevent_req *req, int *perr)
306 int err;
308 if (tevent_req_is_unix_error(req, &err)) {
309 if (perr != NULL) {
310 *perr = err;
312 return false;
315 return true;
318 static void usage(const char *prog)
320 printf("usage: %s <interface> [ <srcip:port> <dstip:port> ]\n", prog);
321 exit(1);
324 int main(int argc, char **argv)
326 struct ctdb_connection conn;
327 struct tevent_context *ev = NULL;
328 TALLOC_CTX *mem_ctx = NULL;
329 struct ctdb_connection_list *conn_list = NULL;
330 const char *t;
331 struct tevent_req *req;
332 int debug_level;
333 bool status;
334 bool ok;
335 int ret;
337 /* Set the debug level */
338 t = getenv("CTDB_DEBUGLEVEL");
339 if (t != NULL) {
340 ok = debug_level_parse(t, &debug_level);
341 if (!ok) {
342 debug_level = DEBUG_ERR;
344 debuglevel_set(debug_level);
347 if (argc != 2 && argc != 4) {
348 usage(argv[0]);
351 if (argc == 4) {
352 ret = ctdb_sock_addr_from_string(argv[2], &conn.client, true);
353 if (ret != 0) {
354 D_ERR("Bad IP:port '%s'\n", argv[2]);
355 goto fail;
358 ret = ctdb_sock_addr_from_string(argv[3], &conn.server, true);
359 if (ret != 0) {
360 D_ERR("Bad IP:port '%s'\n", argv[3]);
361 goto fail;
365 conn_list = talloc_zero(mem_ctx, struct ctdb_connection_list);
366 if (conn_list == NULL) {
367 ret = ENOMEM;
368 DBG_ERR("Internal error (%s)\n", strerror(ret));
369 goto fail;
371 ret = ctdb_connection_list_add(conn_list, &conn);
372 if (ret != 0) {
373 DBG_ERR("Internal error (%s)\n", strerror(ret));
374 goto fail;
376 } else {
377 ret = ctdb_connection_list_read(mem_ctx, 0, true, &conn_list);
378 if (ret != 0) {
379 D_ERR("Unable to parse connections (%s)\n",
380 strerror(ret));
381 goto fail;
385 mem_ctx = talloc_new(NULL);
386 if (mem_ctx == NULL) {
387 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
388 goto fail;
391 ev = tevent_context_init(mem_ctx);
392 if (ev == NULL) {
393 DEBUG(DEBUG_ERR, ("Failed to initialise tevent\n"));
394 goto fail;
397 req = reset_connections_send(mem_ctx, ev, argv[1], conn_list);
398 if (req == NULL) {
399 goto fail;
402 tevent_req_poll(req, ev);
404 status = reset_connections_recv(req, &ret);
405 if (! status) {
406 D_ERR("Failed to kill connections (%s)\n", strerror(ret));
407 goto fail;
410 talloc_free(mem_ctx);
412 return 0;
414 fail:
415 TALLOC_FREE(mem_ctx);
416 return -1;