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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
/*
* (C) 2005 by Pablo Neira Ayuso <pablo@eurodev.net>
*
* 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.
*/
#include <stdio.h>
#include <getopt.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
#include <linux/netfilter_ipv4/ip_conntrack.h>
#include "libctnetlink.h"
#include "libnfnetlink.h"
#include "linux_list.h"
#include "libct_proto.h"
#if 0
#define DEBUGP printf
#else
#define DEBUGP
#endif
extern char *lib_dir;
extern struct list_head proto_list;
extern char *proto2str[];
static void print_status(unsigned int status)
{
if (status & IPS_ASSURED)
fprintf(stdout, "[ASSURED] ");
if (!(status & IPS_SEEN_REPLY))
fprintf(stdout, "[UNREPLIED] ");
}
static int handler(struct sockaddr_nl *sock, struct nlmsghdr *nlh, void *arg)
{
struct nfgenmsg *nfmsg;
struct nfattr *nfa;
int min_len = 0;
struct ctproto_handler *h = NULL;
struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
struct ip_conntrack_tuple *orig, *reply;
struct cta_counters *ctr;
unsigned long *status, *timeout;
struct cta_proto *proto;
unsigned long *id, *mark;
DEBUGP("netlink header\n");
DEBUGP("len: %d type: %d flags: %d seq: %d pid: %d\n",
nlh->nlmsg_len, nlh->nlmsg_type, nlh->nlmsg_flags,
nlh->nlmsg_seq, nlh->nlmsg_pid);
nfmsg = NLMSG_DATA(nlh);
DEBUGP("nfmsg->nfgen_family: %d\n", nfmsg->nfgen_family);
min_len = sizeof(struct nfgenmsg);
if (nlh->nlmsg_len < min_len)
return -EINVAL;
DEBUGP("size:%d\n", nlh->nlmsg_len);
while (NFA_OK(attr, attrlen)) {
switch(attr->nfa_type) {
case CTA_ORIG:
orig = NFA_DATA(attr);
fprintf(stdout, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
NIPQUAD(orig->src.ip),
NIPQUAD(orig->dst.ip));
h = findproto(proto2str[orig->dst.protonum]);
if (h && h->print_tuple)
h->print_tuple(orig);
break;
case CTA_RPLY:
reply = NFA_DATA(attr);
fprintf(stdout, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
NIPQUAD(reply->src.ip),
NIPQUAD(reply->dst.ip));
h = findproto(proto2str[reply->dst.protonum]);
if (h && h->print_tuple)
h->print_tuple(reply);
break;
case CTA_STATUS:
status = NFA_DATA(attr);
print_status(*status);
break;
case CTA_PROTOINFO:
proto = NFA_DATA(attr);
if (proto2str[proto->num_proto]) {
fprintf(stdout, "%s %d ", proto2str[proto->num_proto], proto->num_proto);
h = findproto(proto2str[proto->num_proto]);
if (h && h->print_proto)
h->print_proto(&proto->proto);
} else
fprintf(stdout, "unknown %d ", proto->num_proto);
break;
case CTA_TIMEOUT:
timeout = NFA_DATA(attr);
fprintf(stdout, "timeout=%lu ", *timeout);
break;
case CTA_MARK:
mark = NFA_DATA(attr);
fprintf(stdout, "mark=%lu ", *mark);
break;
case CTA_COUNTERS:
ctr = NFA_DATA(attr);
fprintf(stdout, "orig_packets=%llu orig_bytes=%llu, "
"reply_packets=%llu reply_bytes=%llu ",
ctr->orig.packets, ctr->orig.bytes,
ctr->reply.packets, ctr->reply.bytes);
break;
}
DEBUGP("nfa->nfa_type: %d\n", attr->nfa_type);
DEBUGP("nfa->nfa_len: %d\n", attr->nfa_len);
attr = NFA_NEXT(attr, attrlen);
}
fprintf(stdout, "\n");
return 0;
}
static char *typemsg2str(type, flags)
{
char *ret = "UNKNOWN";
if (type == IPCTNL_MSG_CT_NEW) {
if (flags & NLM_F_CREATE)
ret = "NEW";
else
ret = "UPDATE";
} else if (type == IPCTNL_MSG_CT_DELETE)
ret = "DESTROY";
return ret;
}
static int event_handler(struct sockaddr_nl *sock, struct nlmsghdr *nlh,
void *arg)
{
struct nfgenmsg *nfmsg;
struct nfattr *nfa;
int min_len = 0;
struct ctproto_handler *h = NULL;
int type = NFNL_MSG_TYPE(nlh->nlmsg_type);
struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
struct ip_conntrack_tuple *orig, *reply;
struct cta_counters *ctr;
unsigned long *status, *timeout, *mark;
struct cta_proto *proto;
unsigned long *id;
DEBUGP("netlink header\n");
DEBUGP("len: %d type: %d flags: %d seq: %d pid: %d\n",
nlh->nlmsg_len, nlh->nlmsg_type, nlh->nlmsg_flags,
nlh->nlmsg_seq, nlh->nlmsg_pid);
nfmsg = NLMSG_DATA(nlh);
DEBUGP("nfmsg->nfgen_family: %d\n", nfmsg->nfgen_family);
min_len = sizeof(struct nfgenmsg);
if (nlh->nlmsg_len < min_len)
return -EINVAL;
DEBUGP("size:%d\n", nlh->nlmsg_len);
fprintf(stdout, "[%s] ", typemsg2str(type, nlh->nlmsg_flags));
while (NFA_OK(attr, attrlen)) {
switch(attr->nfa_type) {
case CTA_ORIG:
orig = NFA_DATA(attr);
fprintf(stdout, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
NIPQUAD(orig->src.ip),
NIPQUAD(orig->dst.ip));
h = findproto(proto2str[orig->dst.protonum]);
if (h && h->print_tuple)
h->print_tuple(orig);
break;
case CTA_RPLY:
reply = NFA_DATA(attr);
fprintf(stdout, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
NIPQUAD(reply->src.ip),
NIPQUAD(reply->dst.ip));
h = findproto(proto2str[reply->dst.protonum]);
if (h && h->print_tuple)
h->print_tuple(reply);
break;
case CTA_STATUS:
status = NFA_DATA(attr);
print_status(*status);
break;
case CTA_PROTOINFO:
proto = NFA_DATA(attr);
if (proto2str[proto->num_proto]) {
fprintf(stdout, "%s %d ", proto2str[proto->num_proto], proto->num_proto);
h = findproto(proto2str[proto->num_proto]);
if (h && h->print_proto)
h->print_proto(&proto->proto);
} else
fprintf(stdout, "unknown %d ", proto->num_proto);
break;
case CTA_TIMEOUT:
timeout = NFA_DATA(attr);
fprintf(stdout, "timeout:%lu ", *timeout);
break;
case CTA_MARK:
mark = NFA_DATA(attr);
fprintf(stdout, "mark=%lu ", *mark);
break;
case CTA_COUNTERS:
ctr = NFA_DATA(attr);
fprintf(stdout, "orig_packets=%llu orig_bytes=%llu, "
"reply_packets=%llu reply_bytes=%llu ",
ctr->orig.packets, ctr->orig.bytes,
ctr->reply.packets, ctr->reply.bytes);
break;
}
DEBUGP("nfa->nfa_type: %d\n", attr->nfa_type);
DEBUGP("nfa->nfa_len: %d\n", attr->nfa_len);
attr = NFA_NEXT(attr, attrlen);
}
fprintf(stdout, "\n");
return 0;
}
static int expect_handler(struct sockaddr_nl *sock, struct nlmsghdr *nlh, void *arg)
{
struct nfgenmsg *nfmsg;
struct nfattr *nfa;
int min_len = 0;
struct ctproto_handler *h = NULL;
struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
struct ip_conntrack_tuple *exp, *mask;
unsigned long *timeout;
DEBUGP("netlink header\n");
DEBUGP("len: %d type: %d flags: %d seq: %d pid: %d\n",
nlh->nlmsg_len, nlh->nlmsg_type, nlh->nlmsg_flags,
nlh->nlmsg_seq, nlh->nlmsg_pid);
nfmsg = NLMSG_DATA(nlh);
DEBUGP("nfmsg->nfgen_family: %d\n", nfmsg->nfgen_family);
min_len = sizeof(struct nfgenmsg);
if (nlh->nlmsg_len < min_len)
return -EINVAL;
DEBUGP("size:%d\n", nlh->nlmsg_len);
while (NFA_OK(attr, attrlen)) {
switch(attr->nfa_type) {
case CTA_EXP_TUPLE:
exp = NFA_DATA(attr);
fprintf(stdout, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
NIPQUAD(exp->src.ip),
NIPQUAD(exp->dst.ip));
h = findproto(proto2str[exp->dst.protonum]);
if (h && h->print_tuple)
h->print_tuple(exp);
break;
case CTA_EXP_MASK:
mask = NFA_DATA(attr);
fprintf(stdout, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
NIPQUAD(mask->src.ip),
NIPQUAD(mask->dst.ip));
h = findproto(proto2str[mask->dst.protonum]);
if (h && h->print_tuple)
h->print_tuple(mask);
break;
case CTA_EXP_TIMEOUT:
timeout = NFA_DATA(attr);
fprintf(stdout, "timeout:%lu ", *timeout);
break;
}
DEBUGP("nfa->nfa_type: %d\n", attr->nfa_type);
DEBUGP("nfa->nfa_len: %d\n", attr->nfa_len);
attr = NFA_NEXT(attr, attrlen);
}
fprintf(stdout, "\n");
return 0;
}
int create_conntrack(struct ip_conntrack_tuple *orig,
struct ip_conntrack_tuple *reply,
unsigned long timeout,
union ip_conntrack_proto *proto,
unsigned int status)
{
struct cta_proto cta;
struct nfattr *cda[CTA_MAX];
struct ctnl_handle cth;
cta.num_proto = orig->dst.protonum;
memcpy(&cta.proto, proto, sizeof(*proto));
if (ctnl_open(&cth, 0) < 0)
return -1;
/* FIXME: please unify returns values... */
if (ctnl_new_conntrack(&cth, orig, reply, timeout, &cta, status) < 0)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
int delete_conntrack(struct ip_conntrack_tuple *tuple,
enum ctattr_type_t t)
{
struct nfattr *cda[CTA_MAX];
struct ctnl_handle cth;
if (ctnl_open(&cth, 0) < 0)
return -1;
/* FIXME: please unify returns values... */
if (ctnl_del_conntrack(&cth, tuple, t) < 0)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
/* get_conntrack_handler */
int get_conntrack(struct ip_conntrack_tuple *tuple,
enum ctattr_type_t t,
unsigned long id)
{
struct nfattr *cda[CTA_MAX];
struct ctnl_handle cth;
struct ctnl_msg_handler h = {
.type = 0,
.handler = handler
};
if (ctnl_open(&cth, 0) < 0)
return -1;
ctnl_register_handler(&cth, &h);
/* FIXME!!!! get_conntrack_handler returns -100 */
if (ctnl_get_conntrack(&cth, tuple, t) != -100)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
int dump_conntrack_table(int zero)
{
int ret;
struct ctnl_handle cth;
struct ctnl_msg_handler h = {
.type = 0, /* Hm... really? */
.handler = handler
};
if (ctnl_open(&cth, 0) < 0)
return -1;
ctnl_register_handler(&cth, &h);
if (zero) {
ret = ctnl_list_conntrack_zero_counters(&cth, AF_INET);
} else
ret = ctnl_list_conntrack(&cth, AF_INET);
if (ret != -100)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
int event_conntrack(unsigned int event_mask)
{
struct ctnl_handle cth;
struct ctnl_msg_handler hnew = {
.type = 0, /* new */
.handler = event_handler
};
struct ctnl_msg_handler hdestroy = {
.type = 2, /* destroy */
.handler = event_handler
};
if (ctnl_open(&cth, event_mask) < 0)
return -1;
ctnl_register_handler(&cth, &hnew);
ctnl_register_handler(&cth, &hdestroy);
if (ctnl_event_conntrack(&cth, AF_INET) < 0)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
struct ctproto_handler *findproto(char *name)
{
void *h = NULL;
struct list_head *i;
struct ctproto_handler *cur = NULL, *handler = NULL;
if (!name)
return handler;
lib_dir = getenv("CONNTRACK_LIB_DIR");
if (!lib_dir)
lib_dir = CONNTRACK_LIB_DIR;
list_for_each(i, &proto_list) {
cur = (struct ctproto_handler *) i;
if (strcmp(cur->name, name) == 0) {
handler = cur;
break;
}
}
if (!handler) {
char path[sizeof("libct_proto_.so")
+ strlen(name) + strlen(lib_dir)];
sprintf(path, "%s/libct_proto_%s.so", lib_dir, name);
if (dlopen(path, RTLD_NOW))
handler = findproto(name);
else
DEBUGP(stderr, "%s\n", dlerror());
}
return handler;
}
void register_proto(struct ctproto_handler *h)
{
list_add(&h->head, &proto_list);
}
void unregister_proto(struct ctproto_handler *h)
{
list_del(&h->head);
}
int dump_expect_list()
{
struct ctnl_handle cth;
struct ctnl_msg_handler h = {
.type = 0, /* Hm... really? */
.handler = expect_handler
};
if (ctnl_open(&cth, 0) < 0)
return -1;
ctnl_register_handler(&cth, &h);
if (ctnl_list_expect(&cth, AF_INET) != -100)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
int set_mask(unsigned int mask, int type)
{
struct ctnl_handle cth;
enum ctattr_type_t cta_type;
switch(type) {
case 0:
cta_type = CTA_DUMPMASK;
break;
case 1:
cta_type = CTA_EVENTMASK;
break;
default:
return -1;
}
if (ctnl_open(&cth, 0) < 0)
return -1;
if (ctnl_set_mask(&cth, mask, cta_type) < 0)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
int flush_conntrack()
{
struct ctnl_handle cth;
if (ctnl_open(&cth, 0) < 0)
return -1;
if (ctnl_flush_conntrack(&cth) < 0)
return -1;
if (ctnl_close(&cth) < 0)
return -1;
return 0;
}
|