-
Notifications
You must be signed in to change notification settings - Fork 34
/
nlbwmon.c
386 lines (301 loc) · 7.79 KB
/
nlbwmon.c
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
/*
ISC License
Copyright (c) 2016-2017, Jo-Philipp Wich <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include <libubox/avl.h>
#include <libubox/list.h>
#include <libubox/uloop.h>
#include <libubox/usock.h>
#include <libubox/utils.h>
#include <endian.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <errno.h>
#include <endian.h>
#include "nlbwmon.h"
#include "neigh.h"
#include "database.h"
#include "protocol.h"
#include "nfnetlink.h"
#include "subnets.h"
#include "socket.h"
#include "client.h"
#include "utils.h"
#ifdef HAVE_ULOOP_INTERVAL
# define uloop_timer_type uloop_interval
# define uloop_timer_set(tm, ms) uloop_interval_set(tm, ms)
# define uloop_timer_reset(tm, ms)
#else
# define uloop_timer_type uloop_timeout
# define uloop_timer_set(tm, ms) uloop_timeout_set(tm, ms)
# define uloop_timer_reset(tm, ms) uloop_timeout_set(tm, ms)
#endif
static struct uloop_timer_type commit_tm = { };
static struct uloop_timer_type refresh_tm = { };
struct options opt = {
.commit_interval = 86400,
.refresh_interval = 30,
.netlink_buffer_size = 524288,
.tempdir = "/tmp",
.socket = "/var/run/nlbwmon.sock",
.protocol_db = "/usr/share/nlbwmon/protocols",
.db = {
.directory = "/usr/share/nlbwmon/db"
}
};
static void save_persistent(uint32_t timestamp)
{
int err;
err = database_save(gdbh, opt.db.directory, timestamp, opt.db.compress);
if (err == -EEXIST) {
fprintf(stderr, "Existing database found, merging values\n");
err = database_load(gdbh, opt.db.directory, timestamp);
if (err) {
fprintf(stderr, "Unable to load existing database: %s\n",
strerror(-err));
}
}
err = database_save(gdbh, opt.db.directory, timestamp, opt.db.compress);
if (err) {
fprintf(stderr, "Unable to save database: %s\n",
strerror(-err));
}
}
static void handle_shutdown(int sig)
{
char path[256];
uint32_t timestamp = interval_timestamp(&opt.archive_interval, 0);
save_persistent(timestamp);
if (sig == SIGTERM) {
snprintf(path, sizeof(path), "%s/0.db", opt.tempdir);
unlink(path);
}
else {
database_save(gdbh, opt.tempdir, 0, false);
}
uloop_done();
exit(0);
}
static void
handle_commit(struct uloop_timer_type *tm)
{
uint32_t timestamp = interval_timestamp(&opt.archive_interval, 0);
uloop_timer_reset(tm, opt.commit_interval * 1000);
save_persistent(timestamp);
}
static void
handle_refresh(struct uloop_timer_type *tm)
{
int err;
uloop_timer_reset(tm, opt.refresh_interval * 1000);
err = database_archive(gdbh);
/* database successfully wrapped around and triggered a ct dump */
if (err == -ESTALE)
return;
/* fatal error during database archiving */
if (err != 0) {
fprintf(stderr, "Unable to archive database: %s\n",
strerror(-err));
return;
}
err = nfnetlink_dump(false);
if (err) {
fprintf(stderr, "Unable to dump conntrack: %s\n",
strerror(-err));
return;
}
database_save(gdbh, opt.tempdir, 0, false);
}
static int
parse_timearg(const char *val, time_t *dest)
{
char *e;
*dest = strtoul(val, &e, 10);
if (e == val)
return -EINVAL;
switch (*e)
{
case 'w':
*dest *= 604800;
break;
case 'd':
*dest *= 86400;
break;
case 'h':
*dest *= 3600;
break;
case 'm':
*dest *= 60;
break;
case 's':
case 0:
break;
default:
return -EINVAL;
}
if (*dest > 0x7fffffff / 1000)
return -ERANGE;
return 0;
}
static int
server_main(int argc, char **argv)
{
struct sigaction sa = { .sa_handler = handle_shutdown };
uint32_t timestamp;
int optchr, err;
char *e;
while ((optchr = getopt(argc, argv, "b:i:r:s:o:p:G:I:L:PZ")) > -1) {
switch (optchr) {
case 'b':
opt.netlink_buffer_size = (int)strtol(optarg, &e, 0);
if (e == optarg || *e || opt.netlink_buffer_size < 32768) {
fprintf(stderr, "Invalid netlink buffer size '%s'\n",
optarg);
return 1;
}
break;
case 'i':
err = parse_timearg(optarg, &opt.commit_interval);
if (err) {
fprintf(stderr, "Invalid commit interval '%s': %s\n",
optarg, strerror(-err));
return 1;
}
break;
case 'r':
err = parse_timearg(optarg, &opt.refresh_interval);
if (err) {
fprintf(stderr, "Invalid refresh interval '%s': %s\n",
optarg, strerror(-err));
return 1;
}
break;
case 's':
err = add_subnet(optarg);
if (err) {
fprintf(stderr, "Invalid subnet '%s': %s\n",
optarg, strerror(-err));
return 1;
}
break;
case 'o':
opt.db.directory = optarg;
break;
case 'p':
opt.protocol_db = optarg;
break;
case 'G':
opt.db.generations = strtoul(optarg, &e, 10);
if (e == optarg || *e != 0) {
fprintf(stderr, "Invalid generations argument: %s\n", optarg);
return 1;
}
break;
case 'I':
err = interval_pton(optarg, &opt.archive_interval);
if (err < 0) {
fprintf(stderr, "Invalid interval '%s': %s\n",
optarg, strerror(-err));
exit(1);
}
break;
case 'P':
opt.db.prealloc = true;
break;
case 'L':
opt.db.limit = strtoul(optarg, &e, 10);
if (e == optarg || *e != 0) {
fprintf(stderr, "Invalid limit argument: %s\n", optarg);
return 1;
}
break;
case 'Z':
opt.db.compress = true;
break;
}
}
if (!opt.archive_interval.type) {
fprintf(stderr, "No interval specified; assuming 1st of month\n");
interval_pton("1", &opt.archive_interval);
}
uloop_init();
err = rmkdir(opt.db.directory);
if (err) {
fprintf(stderr, "Unable to create database directory: %s\n",
strerror(-err));
return 1;
}
database_cleanup();
gdbh = database_init(&opt.archive_interval, opt.db.prealloc, opt.db.limit);
if (!gdbh) {
fprintf(stderr, "Unable to allocate memory database: %s\n",
strerror(ENOMEM));
return 1;
}
err = database_load(gdbh, opt.tempdir, 0);
if (err == -ENOENT) {
timestamp = interval_timestamp(&opt.archive_interval, 0);
err = database_load(gdbh, opt.db.directory, timestamp);
}
if (err != 0 && err != -ENOENT) {
fprintf(stderr, "Unable to restore database: %s\n",
strerror(-err));
exit(1);
}
err = init_protocols(opt.protocol_db);
if (err) {
fprintf(stderr, "Unable to read protocol list %s: %s\n",
opt.protocol_db, strerror(-err));
exit(1);
}
err = nfnetlink_connect(opt.netlink_buffer_size);
if (err) {
fprintf(stderr, "Unable to connect nfnetlink: %s\n",
strerror(-err));
exit(1);
}
err = socket_init(opt.socket);
if (err) {
fprintf(stderr, "Unable to create control socket: %s\n",
strerror(-err));
exit(1);
}
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
commit_tm.cb = handle_commit;
uloop_timer_set(&commit_tm, opt.commit_interval * 1000);
refresh_tm.cb = handle_refresh;
uloop_timer_set(&refresh_tm, opt.refresh_interval * 1000);
uloop_run();
return 0;
}
int
main(int argc, char **argv)
{
const char *name = basename(argv[0]);
if (!strcmp(name, "nlbw"))
return client_main(argc, argv);
return server_main(argc, argv);
}