1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
/*
* (C) 2006-2007 by Pablo Neira Ayuso <pablo@netfilter.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "sync.h"
#include "netlink.h"
#include "traffic_stats.h"
#include "log.h"
#include "state_helper.h"
#include "cache.h"
#include "conntrackd.h"
#include "us-conntrack.h"
#include "network.h"
#include "debug.h"
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
static void do_mcast_handler_step(struct nethdr *net)
{
int query;
struct netpld *pld = NETHDR_DATA(net);
char __ct[nfct_maxsize()];
struct nf_conntrack *ct = (struct nf_conntrack *)(void*) __ct;
struct us_conntrack *u;
if (STATE_SYNC(sync)->recv(net))
return;
memset(ct, 0, sizeof(__ct));
/* XXX: check for malformed */
parse_netpld(ct, pld, &query);
switch(query) {
case NFCT_Q_CREATE:
retry:
if ((u = cache_add(STATE_SYNC(external), ct))) {
debug_ct(u->ct, "external new");
} else {
/*
* One certain connection A arrives to the cache but
* another existing connection B in the cache has
* the same configuration, therefore B clashes with A.
*/
if (errno == EEXIST) {
cache_del(STATE_SYNC(external), ct);
goto retry;
}
debug_ct(ct, "can't add");
}
break;
case NFCT_Q_UPDATE:
if ((u = cache_update_force(STATE_SYNC(external), ct))) {
debug_ct(u->ct, "external update");
} else
debug_ct(ct, "can't update");
break;
case NFCT_Q_DESTROY:
if (cache_del(STATE_SYNC(external), ct))
debug_ct(ct, "external destroy");
else
debug_ct(ct, "can't destroy");
break;
default:
dlog(LOG_ERR, "mcast unknown query %d\n", query);
break;
}
}
/* handler for multicast messages received */
static void mcast_handler(void)
{
int numbytes, remain;
char __net[65536], *ptr = __net; /* XXX: maximum MTU for IPv4 */
numbytes = mcast_recv(STATE_SYNC(mcast_server), __net, sizeof(__net));
if (numbytes <= 0)
return;
remain = numbytes;
while (remain > 0) {
struct nethdr *net = (struct nethdr *) ptr;
if (remain < NETHDR_SIZ) {
STATE(malformed)++;
break;
}
if (ntohs(net->len) > remain) {
dlog(LOG_ERR, "fragmented messages");
break;
}
debug("recv sq: %u fl:%u len:%u (rem:%d)\n",
ntohl(net->seq), ntohs(net->flags),
ntohs(net->len), remain);
/* sanity check and convert nethdr to host byte order */
if (handle_netmsg(net) == -1) {
STATE(malformed)++;
return;
}
do_mcast_handler_step(net);
ptr += net->len;
remain -= net->len;
}
}
static int init_sync(void)
{
state.sync = malloc(sizeof(struct ct_sync_state));
if (!state.sync) {
dlog(LOG_ERR, "can't allocate memory for sync");
return -1;
}
memset(state.sync, 0, sizeof(struct ct_sync_state));
if (CONFIG(flags) & CTD_SYNC_FTFW)
STATE_SYNC(sync) = &sync_ftfw;
else if (CONFIG(flags) & CTD_SYNC_ALARM)
STATE_SYNC(sync) = &sync_alarm;
else {
fprintf(stderr, "WARNING: No synchronization mode specified. "
"Defaulting to FT-FW mode.\n");
CONFIG(flags) |= CTD_SYNC_FTFW;
STATE_SYNC(sync) = &sync_ftfw;
}
if (STATE_SYNC(sync)->init)
STATE_SYNC(sync)->init();
STATE_SYNC(internal) =
cache_create("internal",
STATE_SYNC(sync)->internal_cache_flags,
CONFIG(family),
STATE_SYNC(sync)->internal_cache_extra);
if (!STATE_SYNC(internal)) {
dlog(LOG_ERR, "can't allocate memory for "
"the internal cache");
return -1;
}
/* straight forward commit of conntrack to kernel space */
if (CONFIG(cache_write_through))
STATE_SYNC(sync)->external_cache_flags |= WRITE_THROUGH;
STATE_SYNC(external) =
cache_create("external",
STATE_SYNC(sync)->external_cache_flags,
CONFIG(family),
NULL);
if (!STATE_SYNC(external)) {
dlog(LOG_ERR, "can't allocate memory for the "
"external cache");
return -1;
}
/* multicast server to receive events from the wire */
STATE_SYNC(mcast_server) = mcast_server_create(&CONFIG(mcast));
if (STATE_SYNC(mcast_server) == NULL) {
dlog(LOG_ERR, "can't open multicast server!");
return -1;
}
/* multicast client to send events on the wire */
STATE_SYNC(mcast_client) = mcast_client_create(&CONFIG(mcast));
if (STATE_SYNC(mcast_client) == NULL) {
dlog(LOG_ERR, "can't open client multicast socket");
mcast_server_destroy(STATE_SYNC(mcast_server));
return -1;
}
if (mcast_buffered_init(&CONFIG(mcast)) == -1) {
dlog(LOG_ERR, "can't init tx buffer!");
mcast_server_destroy(STATE_SYNC(mcast_server));
mcast_client_destroy(STATE_SYNC(mcast_client));
return -1;
}
/* initialization of multicast sequence generation */
STATE_SYNC(last_seq_sent) = time(NULL);
return 0;
}
static int add_fds_to_set_sync(fd_set *readfds)
{
FD_SET(STATE_SYNC(mcast_server->fd), readfds);
return STATE_SYNC(mcast_server->fd);
}
static void run_sync(fd_set *readfds)
{
/* multicast packet has been received */
if (FD_ISSET(STATE_SYNC(mcast_server->fd), readfds))
mcast_handler();
if (STATE_SYNC(sync)->run)
STATE_SYNC(sync)->run();
/* flush pending messages */
mcast_buffered_pending_netmsg(STATE_SYNC(mcast_client));
}
static void kill_sync(void)
{
cache_destroy(STATE_SYNC(internal));
cache_destroy(STATE_SYNC(external));
mcast_server_destroy(STATE_SYNC(mcast_server));
mcast_client_destroy(STATE_SYNC(mcast_client));
mcast_buffered_destroy();
if (STATE_SYNC(sync)->kill)
STATE_SYNC(sync)->kill();
}
static void dump_stats_sync(int fd)
{
char buf[512];
int size;
size = sprintf(buf, "multicast sequence tracking:\n"
"%20llu Pckts mfrm "
"%20llu Pckts lost\n\n",
(unsigned long long)STATE(malformed),
(unsigned long long)STATE_SYNC(packets_lost));
send(fd, buf, size, 0);
}
/* handler for requests coming via UNIX socket */
static int local_handler_sync(int fd, int type, void *data)
{
int ret = 1;
switch(type) {
case DUMP_INTERNAL:
ret = fork();
if (ret == 0) {
cache_dump(STATE_SYNC(internal), fd, NFCT_O_PLAIN);
exit(EXIT_SUCCESS);
}
break;
case DUMP_EXTERNAL:
ret = fork();
if (ret == 0) {
cache_dump(STATE_SYNC(external), fd, NFCT_O_PLAIN);
exit(EXIT_SUCCESS);
}
break;
case DUMP_INT_XML:
ret = fork();
if (ret == 0) {
cache_dump(STATE_SYNC(internal), fd, NFCT_O_XML);
exit(EXIT_SUCCESS);
}
break;
case DUMP_EXT_XML:
ret = fork();
if (ret == 0) {
cache_dump(STATE_SYNC(external), fd, NFCT_O_XML);
exit(EXIT_SUCCESS);
}
break;
case COMMIT:
ret = fork();
if (ret == 0) {
dlog(LOG_NOTICE,
"committing external cache");
cache_commit(STATE_SYNC(external));
exit(EXIT_SUCCESS);
}
break;
case FLUSH_CACHE:
dlog(LOG_NOTICE, "flushing caches");
cache_flush(STATE_SYNC(internal));
cache_flush(STATE_SYNC(external));
break;
case KILL:
killer(0);
break;
case STATS:
cache_stats(STATE_SYNC(internal), fd);
cache_stats(STATE_SYNC(external), fd);
dump_traffic_stats(fd);
mcast_dump_stats(fd, STATE_SYNC(mcast_client),
STATE_SYNC(mcast_server));
dump_stats_sync(fd);
break;
default:
if (STATE_SYNC(sync)->local)
ret = STATE_SYNC(sync)->local(fd, type, data);
break;
}
return ret;
}
static void dump_sync(struct nf_conntrack *ct)
{
if (!CONFIG(cache_write_through))
nfct_attr_unset(ct, ATTR_TIMEOUT);
/* This is required by kernels < 2.6.20 */
nfct_attr_unset(ct, ATTR_ORIG_COUNTER_BYTES);
nfct_attr_unset(ct, ATTR_ORIG_COUNTER_PACKETS);
nfct_attr_unset(ct, ATTR_REPL_COUNTER_BYTES);
nfct_attr_unset(ct, ATTR_REPL_COUNTER_PACKETS);
nfct_attr_unset(ct, ATTR_USE);
if (cache_update_force(STATE_SYNC(internal), ct))
debug_ct(ct, "resync");
}
static void mcast_send_sync(struct us_conntrack *u,
struct nf_conntrack *ct,
int query)
{
int len;
struct nethdr *net;
if (!state_helper_verdict(query, ct))
return;
net = BUILD_NETMSG(ct, query);
len = prepare_send_netmsg(STATE_SYNC(mcast_client), net);
mcast_buffered_send_netmsg(STATE_SYNC(mcast_client), net, len);
if (STATE_SYNC(sync)->send)
STATE_SYNC(sync)->send(net, u);
}
static int overrun_cb(enum nf_conntrack_msg_type type,
struct nf_conntrack *ct,
void *data)
{
struct us_conntrack *u;
if (ignore_conntrack(ct))
return NFCT_CB_CONTINUE;
if (!CONFIG(cache_write_through))
nfct_attr_unset(ct, ATTR_TIMEOUT);
/* This is required by kernels < 2.6.20 */
nfct_attr_unset(ct, ATTR_ORIG_COUNTER_BYTES);
nfct_attr_unset(ct, ATTR_ORIG_COUNTER_PACKETS);
nfct_attr_unset(ct, ATTR_REPL_COUNTER_BYTES);
nfct_attr_unset(ct, ATTR_REPL_COUNTER_PACKETS);
nfct_attr_unset(ct, ATTR_USE);
if (!cache_test(STATE_SYNC(internal), ct)) {
if ((u = cache_update_force(STATE_SYNC(internal), ct))) {
int len;
debug_ct(u->ct, "overrun resync");
struct nethdr *net = BUILD_NETMSG(u->ct, NFCT_Q_UPDATE);
len = prepare_send_netmsg(STATE_SYNC(mcast_client),net);
mcast_buffered_send_netmsg(STATE_SYNC(mcast_client),
net, len);
if (STATE_SYNC(sync)->send)
STATE_SYNC(sync)->send(net, u);
}
}
return NFCT_CB_CONTINUE;
}
static int overrun_purge_step(void *data1, void *data2)
{
int ret;
struct nfct_handle *h = data1;
struct us_conntrack *u = data2;
ret = nfct_query(h, NFCT_Q_GET, u->ct);
if (ret == -1 && errno == ENOENT) {
int len;
struct nethdr *net = BUILD_NETMSG(u->ct, NFCT_Q_DESTROY);
debug_ct(u->ct, "overrun purge resync");
len = prepare_send_netmsg(STATE_SYNC(mcast_client), net);
mcast_buffered_send_netmsg(STATE_SYNC(mcast_client), net, len);
if (STATE_SYNC(sync)->send)
STATE_SYNC(sync)->send(net, u);
cache_del(STATE_SYNC(internal), u->ct);
}
return 0;
}
/* it's likely that we're losing events, just try to do our best here */
static void overrun_sync(void)
{
int ret;
struct nfct_handle *h;
int family = CONFIG(family);
h = nfct_open(CONNTRACK, 0);
if (!h) {
dlog(LOG_ERR, "can't open overrun handler");
return;
}
nfct_callback_register(h, NFCT_T_ALL, overrun_cb, NULL);
ret = nfct_query(h, NFCT_Q_DUMP, &family);
if (ret == -1)
dlog(LOG_ERR,
"overrun query error %s", strerror(errno));
nfct_callback_unregister(h);
cache_iterate(STATE_SYNC(internal), h, overrun_purge_step);
nfct_close(h);
}
static void event_new_sync(struct nf_conntrack *ct)
{
struct us_conntrack *u;
if (!CONFIG(cache_write_through))
nfct_attr_unset(ct, ATTR_TIMEOUT);
/* required by linux kernel <= 2.6.20 */
nfct_attr_unset(ct, ATTR_ORIG_COUNTER_BYTES);
nfct_attr_unset(ct, ATTR_ORIG_COUNTER_PACKETS);
nfct_attr_unset(ct, ATTR_REPL_COUNTER_BYTES);
nfct_attr_unset(ct, ATTR_REPL_COUNTER_PACKETS);
retry:
if ((u = cache_add(STATE_SYNC(internal), ct))) {
mcast_send_sync(u, ct, NFCT_Q_CREATE);
debug_ct(u->ct, "internal new");
} else {
if (errno == EEXIST) {
cache_del(STATE_SYNC(internal), ct);
mcast_send_sync(NULL, ct, NFCT_Q_DESTROY);
goto retry;
}
dlog(LOG_ERR, "can't add to internal cache: "
"%s\n", strerror(errno));
debug_ct(ct, "can't add");
}
}
static void event_update_sync(struct nf_conntrack *ct)
{
struct us_conntrack *u;
if (!CONFIG(cache_write_through))
nfct_attr_unset(ct, ATTR_TIMEOUT);
if ((u = cache_update_force(STATE_SYNC(internal), ct)) == NULL) {
debug_ct(ct, "can't update");
return;
}
debug_ct(u->ct, "internal update");
mcast_send_sync(u, ct, NFCT_Q_UPDATE);
}
static int event_destroy_sync(struct nf_conntrack *ct)
{
if (!CONFIG(cache_write_through))
nfct_attr_unset(ct, ATTR_TIMEOUT);
if (cache_del(STATE_SYNC(internal), ct)) {
mcast_send_sync(NULL, ct, NFCT_Q_DESTROY);
debug_ct(ct, "internal destroy");
return 1;
} else {
debug_ct(ct, "can't destroy");
return 0;
}
}
struct ct_mode sync_mode = {
.init = init_sync,
.add_fds_to_set = add_fds_to_set_sync,
.run = run_sync,
.local = local_handler_sync,
.kill = kill_sync,
.dump = dump_sync,
.overrun = overrun_sync,
.event_new = event_new_sync,
.event_upd = event_update_sync,
.event_dst = event_destroy_sync
};
|