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
|
/*
* Copyright (c) 2010-2013 Patrick McHardy <kaber@trash.net>
*/
#include <stdio.h>
#include <string.h>
#include <xtables.h>
#include <linux/netfilter/nf_conntrack_common.h>
#include <linux/netfilter/xt_CT.h>
static void ct_help(void)
{
printf(
"CT target options:\n"
" --notrack Don't track connection\n"
" --helper name Use conntrack helper 'name' for connection\n"
" --ctevents event[,event...] Generate specified conntrack events for connection\n"
" --expevents event[,event...] Generate specified expectation events for connection\n"
" --zone {ID|mark} Assign/Lookup connection in zone ID/packet nfmark\n"
" --zone-orig {ID|mark} Same as 'zone' option, but only applies to ORIGINAL direction\n"
" --zone-reply {ID|mark} Same as 'zone' option, but only applies to REPLY direction\n"
);
}
static void ct_help_v1(void)
{
printf(
"CT target options:\n"
" --notrack Don't track connection\n"
" --helper name Use conntrack helper 'name' for connection\n"
" --timeout name Use timeout policy 'name' for connection\n"
" --ctevents event[,event...] Generate specified conntrack events for connection\n"
" --expevents event[,event...] Generate specified expectation events for connection\n"
" --zone {ID|mark} Assign/Lookup connection in zone ID/packet nfmark\n"
" --zone-orig {ID|mark} Same as 'zone' option, but only applies to ORIGINAL direction\n"
" --zone-reply {ID|mark} Same as 'zone' option, but only applies to REPLY direction\n"
);
}
enum {
O_NOTRACK = 0,
O_HELPER,
O_TIMEOUT,
O_CTEVENTS,
O_EXPEVENTS,
O_ZONE,
O_ZONE_ORIG,
O_ZONE_REPLY,
};
#define s struct xt_ct_target_info
static const struct xt_option_entry ct_opts[] = {
{.name = "notrack", .id = O_NOTRACK, .type = XTTYPE_NONE},
{.name = "helper", .id = O_HELPER, .type = XTTYPE_STRING,
.flags = XTOPT_PUT, XTOPT_POINTER(s, helper)},
{.name = "ctevents", .id = O_CTEVENTS, .type = XTTYPE_STRING},
{.name = "expevents", .id = O_EXPEVENTS, .type = XTTYPE_STRING},
{.name = "zone-orig", .id = O_ZONE_ORIG, .type = XTTYPE_STRING},
{.name = "zone-reply", .id = O_ZONE_REPLY, .type = XTTYPE_STRING},
{.name = "zone", .id = O_ZONE, .type = XTTYPE_STRING},
XTOPT_TABLEEND,
};
#undef s
#define s struct xt_ct_target_info_v1
static const struct xt_option_entry ct_opts_v1[] = {
{.name = "notrack", .id = O_NOTRACK, .type = XTTYPE_NONE},
{.name = "helper", .id = O_HELPER, .type = XTTYPE_STRING,
.flags = XTOPT_PUT, XTOPT_POINTER(s, helper)},
{.name = "timeout", .id = O_TIMEOUT, .type = XTTYPE_STRING,
.flags = XTOPT_PUT, XTOPT_POINTER(s, timeout)},
{.name = "ctevents", .id = O_CTEVENTS, .type = XTTYPE_STRING},
{.name = "expevents", .id = O_EXPEVENTS, .type = XTTYPE_STRING},
{.name = "zone-orig", .id = O_ZONE_ORIG, .type = XTTYPE_STRING},
{.name = "zone-reply", .id = O_ZONE_REPLY, .type = XTTYPE_STRING},
{.name = "zone", .id = O_ZONE, .type = XTTYPE_STRING},
XTOPT_TABLEEND,
};
#undef s
struct event_tbl {
const char *name;
unsigned int event;
};
static const struct event_tbl ct_event_tbl[] = {
{ "new", IPCT_NEW },
{ "related", IPCT_RELATED },
{ "destroy", IPCT_DESTROY },
{ "reply", IPCT_REPLY },
{ "assured", IPCT_ASSURED },
{ "protoinfo", IPCT_PROTOINFO },
{ "helper", IPCT_HELPER },
{ "mark", IPCT_MARK },
{ "natseqinfo", IPCT_NATSEQADJ },
{ "secmark", IPCT_SECMARK },
};
static const struct event_tbl exp_event_tbl[] = {
{ "new", IPEXP_NEW },
};
static void ct_parse_zone_id(const char *opt, unsigned int opt_id,
uint16_t *zone_id, uint16_t *flags)
{
if (opt_id == O_ZONE_ORIG)
*flags |= XT_CT_ZONE_DIR_ORIG;
if (opt_id == O_ZONE_REPLY)
*flags |= XT_CT_ZONE_DIR_REPL;
*zone_id = 0;
if (strcasecmp(opt, "mark") == 0) {
*flags |= XT_CT_ZONE_MARK;
} else {
uintmax_t val;
if (!xtables_strtoul(opt, NULL, &val, 0, UINT16_MAX))
xtables_error(PARAMETER_PROBLEM,
"Cannot parse %s as a zone ID\n", opt);
*zone_id = (uint16_t)val;
}
}
static void ct_print_zone_id(const char *pfx, uint16_t zone_id, uint16_t flags)
{
printf(" %s", pfx);
if ((flags & (XT_CT_ZONE_DIR_ORIG |
XT_CT_ZONE_DIR_REPL)) == XT_CT_ZONE_DIR_ORIG)
printf("-orig");
if ((flags & (XT_CT_ZONE_DIR_ORIG |
XT_CT_ZONE_DIR_REPL)) == XT_CT_ZONE_DIR_REPL)
printf("-reply");
if (flags & XT_CT_ZONE_MARK)
printf(" mark");
else
printf(" %u", zone_id);
}
static uint32_t ct_parse_events(const struct event_tbl *tbl, unsigned int size,
const char *events)
{
char str[strlen(events) + 1], *e = str, *t;
unsigned int mask = 0, i;
strcpy(str, events);
while ((t = strsep(&e, ","))) {
for (i = 0; i < size; i++) {
if (strcmp(t, tbl[i].name))
continue;
mask |= 1 << tbl[i].event;
break;
}
if (i == size)
xtables_error(PARAMETER_PROBLEM, "Unknown event type \"%s\"", t);
}
return mask;
}
static void ct_print_events(const char *pfx, const struct event_tbl *tbl,
unsigned int size, uint32_t mask)
{
const char *sep = "";
unsigned int i;
printf(" %s ", pfx);
for (i = 0; i < size; i++) {
if (mask & (1 << tbl[i].event)) {
printf("%s%s", sep, tbl[i].name);
sep = ",";
}
}
}
static void ct_parse(struct xt_option_call *cb)
{
struct xt_ct_target_info *info = cb->data;
xtables_option_parse(cb);
switch (cb->entry->id) {
case O_NOTRACK:
info->flags |= XT_CT_NOTRACK;
break;
case O_ZONE_ORIG:
case O_ZONE_REPLY:
case O_ZONE:
ct_parse_zone_id(cb->arg, cb->entry->id, &info->zone,
&info->flags);
break;
case O_CTEVENTS:
info->ct_events = ct_parse_events(ct_event_tbl, ARRAY_SIZE(ct_event_tbl), cb->arg);
break;
case O_EXPEVENTS:
info->exp_events = ct_parse_events(exp_event_tbl, ARRAY_SIZE(exp_event_tbl), cb->arg);
break;
}
}
static void ct_parse_v1(struct xt_option_call *cb)
{
struct xt_ct_target_info_v1 *info = cb->data;
xtables_option_parse(cb);
switch (cb->entry->id) {
case O_NOTRACK:
info->flags |= XT_CT_NOTRACK;
break;
case O_ZONE_ORIG:
case O_ZONE_REPLY:
case O_ZONE:
ct_parse_zone_id(cb->arg, cb->entry->id, &info->zone,
&info->flags);
break;
case O_CTEVENTS:
info->ct_events = ct_parse_events(ct_event_tbl,
ARRAY_SIZE(ct_event_tbl),
cb->arg);
break;
case O_EXPEVENTS:
info->exp_events = ct_parse_events(exp_event_tbl,
ARRAY_SIZE(exp_event_tbl),
cb->arg);
break;
}
}
static void ct_print(const void *ip, const struct xt_entry_target *target, int numeric)
{
const struct xt_ct_target_info *info =
(const struct xt_ct_target_info *)target->data;
printf(" CT");
if (info->flags & XT_CT_NOTRACK)
printf(" notrack");
if (info->helper[0])
printf(" helper %s", info->helper);
if (info->ct_events)
ct_print_events("ctevents", ct_event_tbl,
ARRAY_SIZE(ct_event_tbl), info->ct_events);
if (info->exp_events)
ct_print_events("expevents", exp_event_tbl,
ARRAY_SIZE(exp_event_tbl), info->exp_events);
if (info->flags & XT_CT_ZONE_MARK || info->zone)
ct_print_zone_id("zone", info->zone, info->flags);
}
static void
ct_print_v1(const void *ip, const struct xt_entry_target *target, int numeric)
{
const struct xt_ct_target_info_v1 *info =
(const struct xt_ct_target_info_v1 *)target->data;
if (info->flags & XT_CT_NOTRACK_ALIAS) {
printf (" NOTRACK");
return;
}
printf(" CT");
if (info->flags & XT_CT_NOTRACK)
printf(" notrack");
if (info->helper[0])
printf(" helper %s", info->helper);
if (info->timeout[0])
printf(" timeout %s", info->timeout);
if (info->ct_events)
ct_print_events("ctevents", ct_event_tbl,
ARRAY_SIZE(ct_event_tbl), info->ct_events);
if (info->exp_events)
ct_print_events("expevents", exp_event_tbl,
ARRAY_SIZE(exp_event_tbl), info->exp_events);
if (info->flags & XT_CT_ZONE_MARK || info->zone)
ct_print_zone_id("zone", info->zone, info->flags);
}
static void ct_save(const void *ip, const struct xt_entry_target *target)
{
const struct xt_ct_target_info *info =
(const struct xt_ct_target_info *)target->data;
if (info->flags & XT_CT_NOTRACK_ALIAS)
return;
if (info->flags & XT_CT_NOTRACK)
printf(" --notrack");
if (info->helper[0])
printf(" --helper %s", info->helper);
if (info->ct_events)
ct_print_events("--ctevents", ct_event_tbl,
ARRAY_SIZE(ct_event_tbl), info->ct_events);
if (info->exp_events)
ct_print_events("--expevents", exp_event_tbl,
ARRAY_SIZE(exp_event_tbl), info->exp_events);
if (info->flags & XT_CT_ZONE_MARK || info->zone)
ct_print_zone_id("--zone", info->zone, info->flags);
}
static void ct_save_v1(const void *ip, const struct xt_entry_target *target)
{
const struct xt_ct_target_info_v1 *info =
(const struct xt_ct_target_info_v1 *)target->data;
if (info->flags & XT_CT_NOTRACK_ALIAS)
return;
if (info->flags & XT_CT_NOTRACK)
printf(" --notrack");
if (info->helper[0])
printf(" --helper %s", info->helper);
if (info->timeout[0])
printf(" --timeout %s", info->timeout);
if (info->ct_events)
ct_print_events("--ctevents", ct_event_tbl,
ARRAY_SIZE(ct_event_tbl), info->ct_events);
if (info->exp_events)
ct_print_events("--expevents", exp_event_tbl,
ARRAY_SIZE(exp_event_tbl), info->exp_events);
if (info->flags & XT_CT_ZONE_MARK || info->zone)
ct_print_zone_id("--zone", info->zone, info->flags);
}
static const char *
ct_print_name_alias(const struct xt_entry_target *target)
{
struct xt_ct_target_info *info = (void *)target->data;
return info->flags & XT_CT_NOTRACK_ALIAS ? "NOTRACK" : "CT";
}
static void notrack_ct0_tg_init(struct xt_entry_target *target)
{
struct xt_ct_target_info *info = (void *)target->data;
info->flags = XT_CT_NOTRACK;
}
static void notrack_ct1_tg_init(struct xt_entry_target *target)
{
struct xt_ct_target_info_v1 *info = (void *)target->data;
info->flags = XT_CT_NOTRACK;
}
static void notrack_ct2_tg_init(struct xt_entry_target *target)
{
struct xt_ct_target_info_v1 *info = (void *)target->data;
info->flags = XT_CT_NOTRACK | XT_CT_NOTRACK_ALIAS;
}
static int xlate_ct1_tg(struct xt_xlate *xl,
const struct xt_xlate_tg_params *params)
{
struct xt_ct_target_info_v1 *info =
(struct xt_ct_target_info_v1 *)params->target->data;
if (info->flags & XT_CT_NOTRACK)
xt_xlate_add(xl, "notrack");
else
return 0;
return 1;
}
static struct xtables_target ct_target_reg[] = {
{
.family = NFPROTO_UNSPEC,
.name = "CT",
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ct_target_info)),
.userspacesize = offsetof(struct xt_ct_target_info, ct),
.help = ct_help,
.print = ct_print,
.save = ct_save,
.x6_parse = ct_parse,
.x6_options = ct_opts,
},
{
.family = NFPROTO_UNSPEC,
.name = "CT",
.revision = 1,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ct_target_info_v1)),
.userspacesize = offsetof(struct xt_ct_target_info_v1, ct),
.help = ct_help_v1,
.print = ct_print_v1,
.save = ct_save_v1,
.x6_parse = ct_parse_v1,
.x6_options = ct_opts_v1,
},
{
.family = NFPROTO_UNSPEC,
.name = "CT",
.revision = 2,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ct_target_info_v1)),
.userspacesize = offsetof(struct xt_ct_target_info_v1, ct),
.help = ct_help_v1,
.print = ct_print_v1,
.save = ct_save_v1,
.alias = ct_print_name_alias,
.x6_parse = ct_parse_v1,
.x6_options = ct_opts_v1,
.xlate = xlate_ct1_tg,
},
{
.family = NFPROTO_UNSPEC,
.name = "NOTRACK",
.real_name = "CT",
.revision = 0,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ct_target_info)),
.userspacesize = offsetof(struct xt_ct_target_info, ct),
.init = notrack_ct0_tg_init,
},
{
.family = NFPROTO_UNSPEC,
.name = "NOTRACK",
.real_name = "CT",
.revision = 1,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ct_target_info_v1)),
.userspacesize = offsetof(struct xt_ct_target_info_v1, ct),
.init = notrack_ct1_tg_init,
},
{
.family = NFPROTO_UNSPEC,
.name = "NOTRACK",
.real_name = "CT",
.revision = 2,
.ext_flags = XTABLES_EXT_ALIAS,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ct_target_info_v1)),
.userspacesize = offsetof(struct xt_ct_target_info_v1, ct),
.init = notrack_ct2_tg_init,
.xlate = xlate_ct1_tg,
},
{
.family = NFPROTO_UNSPEC,
.name = "NOTRACK",
.revision = 0,
.version = XTABLES_VERSION,
},
};
void _init(void)
{
xtables_register_targets(ct_target_reg, ARRAY_SIZE(ct_target_reg));
}
|