libxt_conntrack.c 45.5 KB
Newer Older
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
 *	libxt_conntrack
 *	Shared library add-on to iptables for conntrack matching support.
 *
 *	GPL (C) 2001  Marc Boucher (marc@mbsi.ca).
 *	Copyright © CC Computer Consultants GmbH, 2007 - 2008
 *	Jan Engelhardt <jengelh@computergmbh.de>
 */
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xtables.h>
#include <linux/netfilter/xt_conntrack.h>
#include <linux/netfilter/xt_state.h>
#include <linux/netfilter/nf_conntrack_common.h>
#ifndef XT_STATE_UNTRACKED
#define XT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1))
#endif

struct ip_conntrack_old_tuple {
	struct {
		__be32 ip;
		union {
			__u16 all;
		} u;
	} src;

	struct {
		__be32 ip;
		union {
			__u16 all;
		} u;

		/* The protocol. */
		__u16 protonum;
	} dst;
};

struct xt_conntrack_info {
	unsigned int statemask, statusmask;

	struct ip_conntrack_old_tuple tuple[IP_CT_DIR_MAX];
	struct in_addr sipmsk[IP_CT_DIR_MAX], dipmsk[IP_CT_DIR_MAX];

	unsigned long expires_min, expires_max;

	/* Flags word */
	uint8_t flags;
	/* Inverse flags */
	uint8_t invflags;
};

enum {
	O_CTSTATE = 0,
	O_CTPROTO,
	O_CTORIGSRC,
	O_CTORIGDST,
	O_CTREPLSRC,
	O_CTREPLDST,
	O_CTORIGSRCPORT,
	O_CTORIGDSTPORT,
	O_CTREPLSRCPORT,
	O_CTREPLDSTPORT,
	O_CTSTATUS,
	O_CTEXPIRE,
	O_CTDIR,
};

static void conntrack_mt_help(void)
{
	printf(
"conntrack match options:\n"
"[!] --ctstate {INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED|SNAT|DNAT}[,...]\n"
"                               State(s) to match\n"
"[!] --ctproto proto            Protocol to match; by number or name, e.g. \"tcp\"\n"
"[!] --ctorigsrc address[/mask]\n"
"[!] --ctorigdst address[/mask]\n"
"[!] --ctreplsrc address[/mask]\n"
"[!] --ctrepldst address[/mask]\n"
"                               Original/Reply source/destination address\n"
"[!] --ctorigsrcport port\n"
"[!] --ctorigdstport port\n"
"[!] --ctreplsrcport port\n"
"[!] --ctrepldstport port\n"
"                               TCP/UDP/SCTP orig./reply source/destination port\n"
"[!] --ctstatus {NONE|EXPECTED|SEEN_REPLY|ASSURED|CONFIRMED}[,...]\n"
"                               Status(es) to match\n"
"[!] --ctexpire time[:time]     Match remaining lifetime in seconds against\n"
"                               value or range of values (inclusive)\n"
"    --ctdir {ORIGINAL|REPLY}   Flow direction of packet\n");
}

#define s struct xt_conntrack_info /* for v0 */
static const struct xt_option_entry conntrack_mt_opts_v0[] = {
	{.name = "ctstate", .id = O_CTSTATE, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "ctproto", .id = O_CTPROTO, .type = XTTYPE_PROTOCOL,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigsrc", .id = O_CTORIGSRC, .type = XTTYPE_HOST,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigdst", .id = O_CTORIGDST, .type = XTTYPE_HOST,
	 .flags = XTOPT_INVERT},
	{.name = "ctreplsrc", .id = O_CTREPLSRC, .type = XTTYPE_HOST,
	 .flags = XTOPT_INVERT},
	{.name = "ctrepldst", .id = O_CTREPLDST, .type = XTTYPE_HOST,
	 .flags = XTOPT_INVERT},
	{.name = "ctstatus", .id = O_CTSTATUS, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "ctexpire", .id = O_CTEXPIRE, .type = XTTYPE_UINT32RC,
	 .flags = XTOPT_INVERT},
	XTOPT_TABLEEND,
};
#undef s

#define s struct xt_conntrack_mtinfo2
/* We exploit the fact that v1-v2 share the same xt_o_e layout */
static const struct xt_option_entry conntrack2_mt_opts[] = {
	{.name = "ctstate", .id = O_CTSTATE, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "ctproto", .id = O_CTPROTO, .type = XTTYPE_PROTOCOL,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigsrc", .id = O_CTORIGSRC, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigdst", .id = O_CTORIGDST, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctreplsrc", .id = O_CTREPLSRC, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctrepldst", .id = O_CTREPLDST, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctstatus", .id = O_CTSTATUS, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "ctexpire", .id = O_CTEXPIRE, .type = XTTYPE_UINT32RC,
	 .flags = XTOPT_INVERT},
	/*
	 * Rev 1 and 2 only store one port, and we would normally use
	 * %XTTYPE_PORT (rather than %XTTYPE_PORTRC) for that. The resulting
	 * error message - in case a user passed a range nevertheless -
	 * "port 22:23 resolved to nothing" is not quite as useful as using
	 * %XTTYPE_PORTC and libxt_conntrack's own range test.
	 */
	{.name = "ctorigsrcport", .id = O_CTORIGSRCPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT | XTOPT_NBO},
	{.name = "ctorigdstport", .id = O_CTORIGDSTPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT | XTOPT_NBO},
	{.name = "ctreplsrcport", .id = O_CTREPLSRCPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT | XTOPT_NBO},
	{.name = "ctrepldstport", .id = O_CTREPLDSTPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT | XTOPT_NBO},
	{.name = "ctdir", .id = O_CTDIR, .type = XTTYPE_STRING},
	XTOPT_TABLEEND,
};
#undef s

#define s struct xt_conntrack_mtinfo3
/* Difference from v2 is the non-NBO form. */
static const struct xt_option_entry conntrack3_mt_opts[] = {
	{.name = "ctstate", .id = O_CTSTATE, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "ctproto", .id = O_CTPROTO, .type = XTTYPE_PROTOCOL,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigsrc", .id = O_CTORIGSRC, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigdst", .id = O_CTORIGDST, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctreplsrc", .id = O_CTREPLSRC, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctrepldst", .id = O_CTREPLDST, .type = XTTYPE_HOSTMASK,
	 .flags = XTOPT_INVERT},
	{.name = "ctstatus", .id = O_CTSTATUS, .type = XTTYPE_STRING,
	 .flags = XTOPT_INVERT},
	{.name = "ctexpire", .id = O_CTEXPIRE, .type = XTTYPE_UINT32RC,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigsrcport", .id = O_CTORIGSRCPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT},
	{.name = "ctorigdstport", .id = O_CTORIGDSTPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT},
	{.name = "ctreplsrcport", .id = O_CTREPLSRCPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT},
	{.name = "ctrepldstport", .id = O_CTREPLDSTPORT, .type = XTTYPE_PORTRC,
	 .flags = XTOPT_INVERT},
	{.name = "ctdir", .id = O_CTDIR, .type = XTTYPE_STRING},
	XTOPT_TABLEEND,
};
#undef s

static int
parse_state(const char *state, size_t len, struct xt_conntrack_info *sinfo)
{
	if (strncasecmp(state, "INVALID", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_INVALID;
	else if (strncasecmp(state, "NEW", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_NEW);
	else if (strncasecmp(state, "ESTABLISHED", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED);
	else if (strncasecmp(state, "RELATED", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_RELATED);
	else if (strncasecmp(state, "UNTRACKED", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_UNTRACKED;
	else if (strncasecmp(state, "SNAT", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_SNAT;
	else if (strncasecmp(state, "DNAT", len) == 0)
		sinfo->statemask |= XT_CONNTRACK_STATE_DNAT;
	else
		return 0;
	return 1;
}

static void
parse_states(const char *arg, struct xt_conntrack_info *sinfo)
{
	const char *comma;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg || !parse_state(arg, comma-arg, sinfo))
			xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
		arg = comma+1;
	}
	if (!*arg)
		xtables_error(PARAMETER_PROBLEM, "\"--ctstate\" requires a list of "
					      "states with no spaces, e.g. "
					      "ESTABLISHED,RELATED");
	if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo))
		xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
}

static bool
conntrack_ps_state(struct xt_conntrack_mtinfo3 *info, const char *state,
                   size_t z)
{
	if (strncasecmp(state, "INVALID", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_INVALID;
	else if (strncasecmp(state, "NEW", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_NEW);
	else if (strncasecmp(state, "ESTABLISHED", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED);
	else if (strncasecmp(state, "RELATED", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_RELATED);
	else if (strncasecmp(state, "UNTRACKED", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_UNTRACKED;
	else if (strncasecmp(state, "SNAT", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_SNAT;
	else if (strncasecmp(state, "DNAT", z) == 0)
		info->state_mask |= XT_CONNTRACK_STATE_DNAT;
	else
		return false;
	return true;
}

static void
conntrack_ps_states(struct xt_conntrack_mtinfo3 *info, const char *arg)
{
	const char *comma;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg || !conntrack_ps_state(info, arg, comma - arg))
			xtables_error(PARAMETER_PROBLEM,
			           "Bad ctstate \"%s\"", arg);
		arg = comma + 1;
	}

	if (strlen(arg) == 0 || !conntrack_ps_state(info, arg, strlen(arg)))
		xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg);
}

static int
parse_status(const char *status, size_t len, struct xt_conntrack_info *sinfo)
{
	if (strncasecmp(status, "NONE", len) == 0)
		sinfo->statusmask |= 0;
	else if (strncasecmp(status, "EXPECTED", len) == 0)
		sinfo->statusmask |= IPS_EXPECTED;
	else if (strncasecmp(status, "SEEN_REPLY", len) == 0)
		sinfo->statusmask |= IPS_SEEN_REPLY;
	else if (strncasecmp(status, "ASSURED", len) == 0)
		sinfo->statusmask |= IPS_ASSURED;
#ifdef IPS_CONFIRMED
	else if (strncasecmp(status, "CONFIRMED", len) == 0)
		sinfo->statusmask |= IPS_CONFIRMED;
#endif
	else
		return 0;
	return 1;
}

static void
parse_statuses(const char *arg, struct xt_conntrack_info *sinfo)
{
	const char *comma;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg || !parse_status(arg, comma-arg, sinfo))
			xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg);
		arg = comma+1;
	}

	if (strlen(arg) == 0 || !parse_status(arg, strlen(arg), sinfo))
		xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg);
}

static bool
conntrack_ps_status(struct xt_conntrack_mtinfo3 *info, const char *status,
                    size_t z)
{
	if (strncasecmp(status, "NONE", z) == 0)
		info->status_mask |= 0;
	else if (strncasecmp(status, "EXPECTED", z) == 0)
		info->status_mask |= IPS_EXPECTED;
	else if (strncasecmp(status, "SEEN_REPLY", z) == 0)
		info->status_mask |= IPS_SEEN_REPLY;
	else if (strncasecmp(status, "ASSURED", z) == 0)
		info->status_mask |= IPS_ASSURED;
	else if (strncasecmp(status, "CONFIRMED", z) == 0)
		info->status_mask |= IPS_CONFIRMED;
	else
		return false;
	return true;
}

static void
conntrack_ps_statuses(struct xt_conntrack_mtinfo3 *info, const char *arg)
{
	const char *comma;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg || !conntrack_ps_status(info, arg, comma - arg))
			xtables_error(PARAMETER_PROBLEM,
			           "Bad ctstatus \"%s\"", arg);
		arg = comma + 1;
	}

	if (strlen(arg) == 0 || !conntrack_ps_status(info, arg, strlen(arg)))
		xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg);
}

static void conntrack_parse(struct xt_option_call *cb)
{
	struct xt_conntrack_info *sinfo = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_CTSTATE:
		parse_states(cb->arg, sinfo);
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_STATE;
		break;
	case O_CTPROTO:
		sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum = cb->val.protocol;
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_PROTO;
		if (sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum == 0
		    && (sinfo->invflags & XT_INV_PROTO))
			xtables_error(PARAMETER_PROBLEM,
				   "rule would never match protocol");

		sinfo->flags |= XT_CONNTRACK_PROTO;
		break;
	case O_CTORIGSRC:
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_ORIGSRC;
		sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip = cb->val.haddr.ip;
		sinfo->flags |= XT_CONNTRACK_ORIGSRC;
		break;
	case O_CTORIGDST:
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_ORIGDST;
		sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip = cb->val.haddr.ip;
		sinfo->flags |= XT_CONNTRACK_ORIGDST;
		break;
	case O_CTREPLSRC:
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_REPLSRC;
		sinfo->tuple[IP_CT_DIR_REPLY].src.ip = cb->val.haddr.ip;
		sinfo->flags |= XT_CONNTRACK_REPLSRC;
		break;
	case O_CTREPLDST:
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_REPLDST;
		sinfo->tuple[IP_CT_DIR_REPLY].dst.ip = cb->val.haddr.ip;
		sinfo->flags |= XT_CONNTRACK_REPLDST;
		break;
	case O_CTSTATUS:
		parse_statuses(cb->arg, sinfo);
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_STATUS;
		sinfo->flags |= XT_CONNTRACK_STATUS;
		break;
	case O_CTEXPIRE:
		sinfo->expires_min = cb->val.u32_range[0];
		sinfo->expires_max = cb->val.u32_range[0];
		if (cb->nvals >= 2)
			sinfo->expires_max = cb->val.u32_range[1];
		if (cb->invert)
			sinfo->invflags |= XT_CONNTRACK_EXPIRES;
		sinfo->flags |= XT_CONNTRACK_EXPIRES;
		break;
	}
}

static void conntrack_mt_parse(struct xt_option_call *cb, uint8_t rev)
{
	struct xt_conntrack_mtinfo3 *info = cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_CTSTATE:
		conntrack_ps_states(info, cb->arg);
		info->match_flags |= XT_CONNTRACK_STATE;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_STATE;
		break;
	case O_CTPROTO:
		info->l4proto = cb->val.protocol;
		if (info->l4proto == 0 && (info->invert_flags & XT_INV_PROTO))
			xtables_error(PARAMETER_PROBLEM, "conntrack: rule would "
			           "never match protocol");

		info->match_flags |= XT_CONNTRACK_PROTO;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_PROTO;
		break;
	case O_CTORIGSRC:
		info->origsrc_addr = cb->val.haddr;
		info->origsrc_mask = cb->val.hmask;
		info->match_flags |= XT_CONNTRACK_ORIGSRC;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_ORIGSRC;
		break;
	case O_CTORIGDST:
		info->origdst_addr = cb->val.haddr;
		info->origdst_mask = cb->val.hmask;
		info->match_flags |= XT_CONNTRACK_ORIGDST;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_ORIGDST;
		break;
	case O_CTREPLSRC:
		info->replsrc_addr = cb->val.haddr;
		info->replsrc_mask = cb->val.hmask;
		info->match_flags |= XT_CONNTRACK_REPLSRC;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_REPLSRC;
		break;
	case O_CTREPLDST:
		info->repldst_addr = cb->val.haddr;
		info->repldst_mask = cb->val.hmask;
		info->match_flags |= XT_CONNTRACK_REPLDST;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_REPLDST;
		break;
	case O_CTSTATUS:
		conntrack_ps_statuses(info, cb->arg);
		info->match_flags |= XT_CONNTRACK_STATUS;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_STATUS;
		break;
	case O_CTEXPIRE:
		info->expires_min = cb->val.u32_range[0];
		info->expires_max = cb->val.u32_range[0];
		if (cb->nvals >= 2)
			info->expires_max = cb->val.u32_range[1];
		info->match_flags |= XT_CONNTRACK_EXPIRES;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_EXPIRES;
		break;
	case O_CTORIGSRCPORT:
		info->origsrc_port = cb->val.port_range[0];
		info->origsrc_port_high = cb->val.port_range[cb->nvals >= 2];
		info->match_flags |= XT_CONNTRACK_ORIGSRC_PORT;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_ORIGSRC_PORT;
		break;
	case O_CTORIGDSTPORT:
		info->origdst_port = cb->val.port_range[0];
		info->origdst_port_high = cb->val.port_range[cb->nvals >= 2];
		info->match_flags |= XT_CONNTRACK_ORIGDST_PORT;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_ORIGDST_PORT;
		break;
	case O_CTREPLSRCPORT:
		info->replsrc_port = cb->val.port_range[0];
		info->replsrc_port_high = cb->val.port_range[cb->nvals >= 2];
		info->match_flags |= XT_CONNTRACK_REPLSRC_PORT;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_REPLSRC_PORT;
		break;
	case O_CTREPLDSTPORT:
		info->repldst_port = cb->val.port_range[0];
		info->repldst_port_high = cb->val.port_range[cb->nvals >= 2];
		info->match_flags |= XT_CONNTRACK_REPLDST_PORT;
		if (cb->invert)
			info->invert_flags |= XT_CONNTRACK_REPLDST_PORT;
		break;
	case O_CTDIR:
		if (strcasecmp(cb->arg, "ORIGINAL") == 0) {
			info->match_flags  |= XT_CONNTRACK_DIRECTION;
			info->invert_flags &= ~XT_CONNTRACK_DIRECTION;
		} else if (strcasecmp(cb->arg, "REPLY") == 0) {
			info->match_flags  |= XT_CONNTRACK_DIRECTION;
			info->invert_flags |= XT_CONNTRACK_DIRECTION;
		} else {
			xtables_param_act(XTF_BAD_VALUE, "conntrack", "--ctdir", cb->arg);
		}
		break;
	}
}

#define cinfo_transform(r, l) \
	do { \
		memcpy((r), (l), offsetof(typeof(*(l)), state_mask)); \
		(r)->state_mask  = (l)->state_mask; \
		(r)->status_mask = (l)->status_mask; \
	} while (false);

static void conntrack1_mt_parse(struct xt_option_call *cb)
{
	struct xt_conntrack_mtinfo1 *info = cb->data;
	struct xt_conntrack_mtinfo3 up;

	memset(&up, 0, sizeof(up));
	cinfo_transform(&up, info);
	up.origsrc_port_high = up.origsrc_port;
	up.origdst_port_high = up.origdst_port;
	up.replsrc_port_high = up.replsrc_port;
	up.repldst_port_high = up.repldst_port;
	cb->data = &up;
	conntrack_mt_parse(cb, 3);
	if (up.origsrc_port != up.origsrc_port_high ||
	    up.origdst_port != up.origdst_port_high ||
	    up.replsrc_port != up.replsrc_port_high ||
	    up.repldst_port != up.repldst_port_high)
		xtables_error(PARAMETER_PROBLEM,
			"conntrack rev 1 does not support port ranges");
	cinfo_transform(info, &up);
	cb->data = info;
}

static void conntrack2_mt_parse(struct xt_option_call *cb)
{
#define cinfo2_transform(r, l) \
		memcpy((r), (l), offsetof(typeof(*(l)), sizeof(*info));

	struct xt_conntrack_mtinfo2 *info = cb->data;
	struct xt_conntrack_mtinfo3 up;

	memset(&up, 0, sizeof(up));
	memcpy(&up, info, sizeof(*info));
	up.origsrc_port_high = up.origsrc_port;
	up.origdst_port_high = up.origdst_port;
	up.replsrc_port_high = up.replsrc_port;
	up.repldst_port_high = up.repldst_port;
	cb->data = &up;
	conntrack_mt_parse(cb, 3);
	if (up.origsrc_port != up.origsrc_port_high ||
	    up.origdst_port != up.origdst_port_high ||
	    up.replsrc_port != up.replsrc_port_high ||
	    up.repldst_port != up.repldst_port_high)
		xtables_error(PARAMETER_PROBLEM,
			"conntrack rev 2 does not support port ranges");
	memcpy(info, &up, sizeof(*info));
	cb->data = info;
#undef cinfo2_transform
}

static void conntrack3_mt_parse(struct xt_option_call *cb)
{
	conntrack_mt_parse(cb, 3);
}

static void conntrack_mt_check(struct xt_fcheck_call *cb)
{
	if (cb->xflags == 0)
		xtables_error(PARAMETER_PROBLEM, "conntrack: At least one option "
		           "is required");
}

static void
print_state(unsigned int statemask)
{
	const char *sep = " ";

	if (statemask & XT_CONNTRACK_STATE_INVALID) {
		printf("%sINVALID", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_NEW)) {
		printf("%sNEW", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_RELATED)) {
		printf("%sRELATED", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED)) {
		printf("%sESTABLISHED", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_UNTRACKED) {
		printf("%sUNTRACKED", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_SNAT) {
		printf("%sSNAT", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_DNAT) {
		printf("%sDNAT", sep);
		sep = ",";
	}
}

static void
print_status(unsigned int statusmask)
{
	const char *sep = " ";

	if (statusmask & IPS_EXPECTED) {
		printf("%sEXPECTED", sep);
		sep = ",";
	}
	if (statusmask & IPS_SEEN_REPLY) {
		printf("%sSEEN_REPLY", sep);
		sep = ",";
	}
	if (statusmask & IPS_ASSURED) {
		printf("%sASSURED", sep);
		sep = ",";
	}
	if (statusmask & IPS_CONFIRMED) {
		printf("%sCONFIRMED", sep);
		sep = ",";
	}
	if (statusmask == 0)
		printf("%sNONE", sep);
}

static void
conntrack_dump_addr(const union nf_inet_addr *addr,
                    const union nf_inet_addr *mask,
                    unsigned int family, bool numeric)
{
	if (family == NFPROTO_IPV4) {
		if (!numeric && addr->ip == 0) {
			printf(" anywhere");
			return;
		}
		if (numeric)
			printf(" %s%s",
			       xtables_ipaddr_to_numeric(&addr->in),
			       xtables_ipmask_to_numeric(&mask->in));
		else
			printf(" %s%s",
			       xtables_ipaddr_to_anyname(&addr->in),
			       xtables_ipmask_to_numeric(&mask->in));
	} else if (family == NFPROTO_IPV6) {
		if (!numeric && addr->ip6[0] == 0 && addr->ip6[1] == 0 &&
		    addr->ip6[2] == 0 && addr->ip6[3] == 0) {
			printf(" anywhere");
			return;
		}
		if (numeric)
			printf(" %s%s",
			       xtables_ip6addr_to_numeric(&addr->in6),
			       xtables_ip6mask_to_numeric(&mask->in6));
		else
			printf(" %s%s",
			       xtables_ip6addr_to_anyname(&addr->in6),
			       xtables_ip6mask_to_numeric(&mask->in6));
	}
}

static void
print_addr(const struct in_addr *addr, const struct in_addr *mask,
           int inv, int numeric)
{
	if (inv)
		printf(" !");

	if (mask->s_addr == 0L && !numeric)
680
		printf(" anywhere");
681
682
	else {
		if (numeric)
683
684
685
			printf(" %s%s",
			       xtables_ipaddr_to_numeric(addr),
			       xtables_ipmask_to_numeric(mask));
686
		else
687
688
689
			printf(" %s%s",
			       xtables_ipaddr_to_anyname(addr),
			       xtables_ipmask_to_numeric(mask));
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
	}
}

static void
matchinfo_print(const void *ip, const struct xt_entry_match *match, int numeric, const char *optpfx)
{
	const struct xt_conntrack_info *sinfo = (const void *)match->data;

	if(sinfo->flags & XT_CONNTRACK_STATE) {
        	if (sinfo->invflags & XT_CONNTRACK_STATE)
			printf(" !");
		printf(" %sctstate", optpfx);
		print_state(sinfo->statemask);
	}

	if(sinfo->flags & XT_CONNTRACK_PROTO) {
        	if (sinfo->invflags & XT_CONNTRACK_PROTO)
			printf(" !");
		printf(" %sctproto", optpfx);
		printf(" %u", sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum);
	}

	if(sinfo->flags & XT_CONNTRACK_ORIGSRC) {
		if (sinfo->invflags & XT_CONNTRACK_ORIGSRC)
			printf(" !");
		printf(" %sctorigsrc", optpfx);

		print_addr(
		    (struct in_addr *)&sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip,
		    &sinfo->sipmsk[IP_CT_DIR_ORIGINAL],
		    false,
		    numeric);
	}

	if(sinfo->flags & XT_CONNTRACK_ORIGDST) {
		if (sinfo->invflags & XT_CONNTRACK_ORIGDST)
			printf(" !");
		printf(" %sctorigdst", optpfx);

		print_addr(
		    (struct in_addr *)&sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip,
		    &sinfo->dipmsk[IP_CT_DIR_ORIGINAL],
		    false,
		    numeric);
	}

	if(sinfo->flags & XT_CONNTRACK_REPLSRC) {
		if (sinfo->invflags & XT_CONNTRACK_REPLSRC)
			printf(" !");
		printf(" %sctreplsrc", optpfx);

		print_addr(
		    (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].src.ip,
		    &sinfo->sipmsk[IP_CT_DIR_REPLY],
		    false,
		    numeric);
	}

	if(sinfo->flags & XT_CONNTRACK_REPLDST) {
		if (sinfo->invflags & XT_CONNTRACK_REPLDST)
			printf(" !");
		printf(" %sctrepldst", optpfx);

		print_addr(
		    (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].dst.ip,
		    &sinfo->dipmsk[IP_CT_DIR_REPLY],
		    false,
		    numeric);
	}

	if(sinfo->flags & XT_CONNTRACK_STATUS) {
        	if (sinfo->invflags & XT_CONNTRACK_STATUS)
			printf(" !");
		printf(" %sctstatus", optpfx);
		print_status(sinfo->statusmask);
	}

	if(sinfo->flags & XT_CONNTRACK_EXPIRES) {
        	if (sinfo->invflags & XT_CONNTRACK_EXPIRES)
			printf(" !");
		printf(" %sctexpire ", optpfx);

        	if (sinfo->expires_max == sinfo->expires_min)
			printf("%lu", sinfo->expires_min);
        	else
			printf("%lu:%lu", sinfo->expires_min, sinfo->expires_max);
	}
}

static void
conntrack_dump_ports(const char *prefix, const char *opt,
		     u_int16_t port_low, u_int16_t port_high)
{
	if (port_high == 0 || port_low == port_high)
		printf(" %s%s %u", prefix, opt, port_low);
	else
		printf(" %s%s %u:%u", prefix, opt, port_low, port_high);
}

static void
conntrack_dump(const struct xt_conntrack_mtinfo3 *info, const char *prefix,
               unsigned int family, bool numeric, bool v3)
{
	if (info->match_flags & XT_CONNTRACK_STATE) {
		if (info->invert_flags & XT_CONNTRACK_STATE)
			printf(" !");
		printf(" %s%s", prefix,
			info->match_flags & XT_CONNTRACK_STATE_ALIAS
				? "state" : "ctstate");
		print_state(info->state_mask);
	}

	if (info->match_flags & XT_CONNTRACK_PROTO) {
		if (info->invert_flags & XT_CONNTRACK_PROTO)
			printf(" !");
		printf(" %sctproto %u", prefix, info->l4proto);
	}

	if (info->match_flags & XT_CONNTRACK_ORIGSRC) {
		if (info->invert_flags & XT_CONNTRACK_ORIGSRC)
			printf(" !");
		printf(" %sctorigsrc", prefix);
		conntrack_dump_addr(&info->origsrc_addr, &info->origsrc_mask,
		                    family, numeric);
	}

	if (info->match_flags & XT_CONNTRACK_ORIGDST) {
		if (info->invert_flags & XT_CONNTRACK_ORIGDST)
			printf(" !");
		printf(" %sctorigdst", prefix);
		conntrack_dump_addr(&info->origdst_addr, &info->origdst_mask,
		                    family, numeric);
	}

	if (info->match_flags & XT_CONNTRACK_REPLSRC) {
		if (info->invert_flags & XT_CONNTRACK_REPLSRC)
			printf(" !");
		printf(" %sctreplsrc", prefix);
		conntrack_dump_addr(&info->replsrc_addr, &info->replsrc_mask,
		                    family, numeric);
	}

	if (info->match_flags & XT_CONNTRACK_REPLDST) {
		if (info->invert_flags & XT_CONNTRACK_REPLDST)
			printf(" !");
		printf(" %sctrepldst", prefix);
		conntrack_dump_addr(&info->repldst_addr, &info->repldst_mask,
		                    family, numeric);
	}

	if (info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) {
		if (info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT)
			printf(" !");
		conntrack_dump_ports(prefix, "ctorigsrcport",
				     v3 ? info->origsrc_port : ntohs(info->origsrc_port),
				     v3 ? info->origsrc_port_high : 0);
	}

	if (info->match_flags & XT_CONNTRACK_ORIGDST_PORT) {
		if (info->invert_flags & XT_CONNTRACK_ORIGDST_PORT)
			printf(" !");
		conntrack_dump_ports(prefix, "ctorigdstport",
				     v3 ? info->origdst_port : ntohs(info->origdst_port),
				     v3 ? info->origdst_port_high : 0);
	}

	if (info->match_flags & XT_CONNTRACK_REPLSRC_PORT) {
		if (info->invert_flags & XT_CONNTRACK_REPLSRC_PORT)
			printf(" !");
		conntrack_dump_ports(prefix, "ctreplsrcport",
				     v3 ? info->replsrc_port : ntohs(info->replsrc_port),
				     v3 ? info->replsrc_port_high : 0);
	}

	if (info->match_flags & XT_CONNTRACK_REPLDST_PORT) {
		if (info->invert_flags & XT_CONNTRACK_REPLDST_PORT)
			printf(" !");
		conntrack_dump_ports(prefix, "ctrepldstport",
				     v3 ? info->repldst_port : ntohs(info->repldst_port),
				     v3 ? info->repldst_port_high : 0);
	}

	if (info->match_flags & XT_CONNTRACK_STATUS) {
		if (info->invert_flags & XT_CONNTRACK_STATUS)
			printf(" !");
		printf(" %sctstatus", prefix);
		print_status(info->status_mask);
	}

	if (info->match_flags & XT_CONNTRACK_EXPIRES) {
		if (info->invert_flags & XT_CONNTRACK_EXPIRES)
			printf(" !");
		printf(" %sctexpire ", prefix);

		if (info->expires_max == info->expires_min)
			printf("%u", (unsigned int)info->expires_min);
		else
			printf("%u:%u", (unsigned int)info->expires_min,
			       (unsigned int)info->expires_max);
	}

	if (info->match_flags & XT_CONNTRACK_DIRECTION) {
		if (info->invert_flags & XT_CONNTRACK_DIRECTION)
			printf(" %sctdir REPLY", prefix);
		else
			printf(" %sctdir ORIGINAL", prefix);
	}
}

static const char *
conntrack_print_name_alias(const struct xt_entry_match *match)
{
	struct xt_conntrack_mtinfo1 *info = (void *)match->data;

	return info->match_flags & XT_CONNTRACK_STATE_ALIAS
		? "state" : "conntrack";
}

static void conntrack_print(const void *ip, const struct xt_entry_match *match,
                            int numeric)
{
	matchinfo_print(ip, match, numeric, "");
}

static void
conntrack1_mt4_print(const void *ip, const struct xt_entry_match *match,
                     int numeric)
{
	const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
	struct xt_conntrack_mtinfo3 up;

	cinfo_transform(&up, info);
	conntrack_dump(&up, "", NFPROTO_IPV4, numeric, false);
}

static void
conntrack1_mt6_print(const void *ip, const struct xt_entry_match *match,
                     int numeric)
{
	const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
	struct xt_conntrack_mtinfo3 up;

	cinfo_transform(&up, info);
	conntrack_dump(&up, "", NFPROTO_IPV6, numeric, false);
}

static void
conntrack2_mt_print(const void *ip, const struct xt_entry_match *match,
                    int numeric)
{
	conntrack_dump((const void *)match->data, "", NFPROTO_IPV4, numeric, false);
}

static void
conntrack2_mt6_print(const void *ip, const struct xt_entry_match *match,
                     int numeric)
{
	conntrack_dump((const void *)match->data, "", NFPROTO_IPV6, numeric, false);
}

static void
conntrack3_mt_print(const void *ip, const struct xt_entry_match *match,
                    int numeric)
{
	conntrack_dump((const void *)match->data, "", NFPROTO_IPV4, numeric, true);
}

static void
conntrack3_mt6_print(const void *ip, const struct xt_entry_match *match,
                     int numeric)
{
	conntrack_dump((const void *)match->data, "", NFPROTO_IPV6, numeric, true);
}

static void conntrack_save(const void *ip, const struct xt_entry_match *match)
{
	matchinfo_print(ip, match, 1, "--");
}

static void conntrack3_mt_save(const void *ip,
                               const struct xt_entry_match *match)
{
	conntrack_dump((const void *)match->data, "--", NFPROTO_IPV4, true, true);
}

static void conntrack3_mt6_save(const void *ip,
                                const struct xt_entry_match *match)
{
	conntrack_dump((const void *)match->data, "--", NFPROTO_IPV6, true, true);
}

static void conntrack2_mt_save(const void *ip,
                               const struct xt_entry_match *match)
{
	conntrack_dump((const void *)match->data, "--", NFPROTO_IPV4, true, false);
}

static void conntrack2_mt6_save(const void *ip,
                                const struct xt_entry_match *match)
{
	conntrack_dump((const void *)match->data, "--", NFPROTO_IPV6, true, false);
}

static void
conntrack1_mt4_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
	struct xt_conntrack_mtinfo3 up;

	cinfo_transform(&up, info);
	conntrack_dump(&up, "--", NFPROTO_IPV4, true, false);
}

static void
conntrack1_mt6_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_conntrack_mtinfo1 *info = (void *)match->data;
	struct xt_conntrack_mtinfo3 up;

	cinfo_transform(&up, info);
	conntrack_dump(&up, "--", NFPROTO_IPV6, true, false);
}

static void
state_help(void)
{
	printf(
"state match options:\n"
" [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n"
"				State(s) to match\n");
}

static const struct xt_option_entry state_opts[] = {
	{.name = "state", .id = O_CTSTATE, .type = XTTYPE_STRING,
	 .flags = XTOPT_MAND | XTOPT_INVERT},
	XTOPT_TABLEEND,
};

static unsigned int
state_parse_state(const char *state, size_t len)
{
	if (strncasecmp(state, "INVALID", len) == 0)
		return XT_CONNTRACK_STATE_INVALID;
	else if (strncasecmp(state, "NEW", len) == 0)
		return XT_CONNTRACK_STATE_BIT(IP_CT_NEW);
	else if (strncasecmp(state, "ESTABLISHED", len) == 0)
		return XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED);
	else if (strncasecmp(state, "RELATED", len) == 0)
		return XT_CONNTRACK_STATE_BIT(IP_CT_RELATED);
	else if (strncasecmp(state, "UNTRACKED", len) == 0)
		return XT_CONNTRACK_STATE_UNTRACKED;
	return 0;
}

static unsigned int
state_parse_states(const char *arg)
{
	const char *comma;
	unsigned int mask = 0, flag;

	while ((comma = strchr(arg, ',')) != NULL) {
		if (comma == arg)
			goto badstate;
		flag = state_parse_state(arg, comma-arg);
		if (flag == 0)
			goto badstate;
		mask |= flag;
		arg = comma+1;
	}
	if (!*arg)
		xtables_error(PARAMETER_PROBLEM, "\"--state\" requires a list of "
					      "states with no spaces, e.g. "
					      "ESTABLISHED,RELATED");
	if (strlen(arg) == 0)
		goto badstate;
	flag = state_parse_state(arg, strlen(arg));
	if (flag == 0)
		goto badstate;
	mask |= flag;
	return mask;
 badstate:
	xtables_error(PARAMETER_PROBLEM, "Bad state \"%s\"", arg);
}

static void state_parse(struct xt_option_call *cb)
{
	struct xt_state_info *sinfo = cb->data;

	xtables_option_parse(cb);
	sinfo->statemask = state_parse_states(cb->arg);
	if (cb->invert)
		sinfo->statemask = ~sinfo->statemask;
}

static void state_ct1_parse(struct xt_option_call *cb)
{
	struct xt_conntrack_mtinfo1 *sinfo = cb->data;

	xtables_option_parse(cb);
	sinfo->match_flags = XT_CONNTRACK_STATE | XT_CONNTRACK_STATE_ALIAS;
	sinfo->state_mask = state_parse_states(cb->arg);
	if (cb->invert)
		sinfo->invert_flags |= XT_CONNTRACK_STATE;
}

static void state_ct23_parse(struct xt_option_call *cb)
{
	struct xt_conntrack_mtinfo3 *sinfo = cb->data;

	xtables_option_parse(cb);
	sinfo->match_flags = XT_CONNTRACK_STATE | XT_CONNTRACK_STATE_ALIAS;
	sinfo->state_mask = state_parse_states(cb->arg);
	if (cb->invert)
		sinfo->invert_flags |= XT_CONNTRACK_STATE;
}

static void state_print_state(unsigned int statemask)
{
	const char *sep = "";

	if (statemask & XT_CONNTRACK_STATE_INVALID) {
		printf("%sINVALID", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_NEW)) {
		printf("%sNEW", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_RELATED)) {
		printf("%sRELATED", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED)) {
		printf("%sESTABLISHED", sep);
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_UNTRACKED) {
		printf("%sUNTRACKED", sep);
		sep = ",";
	}
}

static void
state_print(const void *ip,
      const struct xt_entry_match *match,
      int numeric)
{
	const struct xt_state_info *sinfo = (const void *)match->data;

	printf(" state ");
	state_print_state(sinfo->statemask);
}

static void state_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_state_info *sinfo = (const void *)match->data;

	printf(" --state ");
	state_print_state(sinfo->statemask);
}

1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
static void state_xlate_print(struct xt_xlate *xl, unsigned int statemask)
{
	const char *sep = "";

	if (statemask & XT_CONNTRACK_STATE_INVALID) {
		xt_xlate_add(xl, "%s%s", sep, "invalid");
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_NEW)) {
		xt_xlate_add(xl, "%s%s", sep, "new");
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_RELATED)) {
		xt_xlate_add(xl, "%s%s", sep, "related");
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED)) {
		xt_xlate_add(xl, "%s%s", sep, "established");
		sep = ",";
	}
	if (statemask & XT_CONNTRACK_STATE_UNTRACKED) {
		xt_xlate_add(xl, "%s%s", sep, "untracked");
		sep = ",";
	}
}

static int state_xlate(struct xt_xlate *xl,
		       const struct xt_xlate_mt_params *params)
{
	const struct xt_conntrack_mtinfo3 *sinfo =
		(const void *)params->match->data;

	xt_xlate_add(xl, "ct state %s", sinfo->invert_flags & XT_CONNTRACK_STATE ?
					"!= " : "");
	state_xlate_print(xl, sinfo->state_mask);
	xt_xlate_add(xl, " ");
	return 1;
}

static void status_xlate_print(struct xt_xlate *xl, unsigned int statusmask)
{
	const char *sep = "";

	if (statusmask & IPS_EXPECTED) {
		xt_xlate_add(xl, "%s%s", sep, "expected");
		sep = ",";
	}
	if (statusmask & IPS_SEEN_REPLY) {
		xt_xlate_add(xl, "%s%s", sep, "seen-reply");
		sep = ",";
	}
	if (statusmask & IPS_ASSURED) {
		xt_xlate_add(xl, "%s%s", sep, "assured");
		sep = ",";
	}
	if (statusmask & IPS_CONFIRMED) {
		xt_xlate_add(xl, "%s%s", sep, "confirmed");
		sep = ",";
	}
}

static void addr_xlate_print(struct xt_xlate *xl,
			     const union nf_inet_addr *addr,
			     const union nf_inet_addr *mask,
			     unsigned int family)
{
	if (family == NFPROTO_IPV4) {
		xt_xlate_add(xl, "%s%s", xtables_ipaddr_to_numeric(&addr->in),
		     xtables_ipmask_to_numeric(&mask->in));
	} else if (family == NFPROTO_IPV6) {
		xt_xlate_add(xl, "%s%s", xtables_ip6addr_to_numeric(&addr->in6),
		     xtables_ip6mask_to_numeric(&mask->in6));
	}
}

static int _conntrack3_mt_xlate(struct xt_xlate *xl,
				const struct xt_xlate_mt_params *params,
				int family)
{
	const struct xt_conntrack_mtinfo3 *sinfo =
		(const void *)params->match->data;
	char *space = "";

	if (sinfo->match_flags & XT_CONNTRACK_DIRECTION) {
		xt_xlate_add(xl, "ct direction %s",
			     sinfo->invert_flags & XT_CONNTRACK_DIRECTION ?
			     "reply" : "original");
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_PROTO) {
		xt_xlate_add(xl, "%sct %s protocol %s%u", space,
			     sinfo->invert_flags & XT_CONNTRACK_DIRECTION ?
			     "reply" : "original",
			     sinfo->invert_flags & XT_CONNTRACK_PROTO ?
			     "!= " : "",
			     sinfo->l4proto);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_STATE) {
		xt_xlate_add(xl, "%sct state %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_STATE ?
			     "!= " : "");
		state_xlate_print(xl, sinfo->state_mask);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_STATUS) {
		if (sinfo->status_mask == 1)
			return 0;
		xt_xlate_add(xl, "%sct status %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_STATUS ?
			     "!= " : "");
		status_xlate_print(xl, sinfo->status_mask);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_EXPIRES) {
		xt_xlate_add(xl, "%sct expiration %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_EXPIRES ?
			     "!= " : "");
		if (sinfo->expires_max == sinfo->expires_min)
			xt_xlate_add(xl, "%lu", sinfo->expires_min);
		else
			xt_xlate_add(xl, "%lu-%lu", sinfo->expires_min,
				     sinfo->expires_max);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_ORIGSRC) {
		if (&sinfo->origsrc_addr == 0L)
			return 0;

		xt_xlate_add(xl, "%sct original saddr %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_ORIGSRC ?
			     "!= " : "");
		addr_xlate_print(xl, &sinfo->origsrc_addr,
				 &sinfo->origsrc_mask, family);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_ORIGDST) {
		if (&sinfo->origdst_addr == 0L)
			return 0;

		xt_xlate_add(xl, "%sct original daddr %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_ORIGDST ?
			     "!= " : "");
		addr_xlate_print(xl, &sinfo->origdst_addr,
				 &sinfo->origdst_mask, family);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_REPLSRC) {
		if (&sinfo->replsrc_addr == 0L)
			return 0;

		xt_xlate_add(xl, "%sct reply saddr %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_REPLSRC ?
			     "!= " : "");
		addr_xlate_print(xl, &sinfo->replsrc_addr,
				 &sinfo->replsrc_mask, family);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_REPLDST) {
		if (&sinfo->repldst_addr == 0L)
			return 0;

		xt_xlate_add(xl, "%sct reply daddr %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_REPLDST ?
			     "!= " : "");
		addr_xlate_print(xl, &sinfo->repldst_addr,
				 &sinfo->repldst_mask, family);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_ORIGSRC_PORT) {
		xt_xlate_add(xl, "%sct original proto-src %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_ORIGSRC_PORT ?
			     "!= " : "");
		if (sinfo->origsrc_port == sinfo->origsrc_port_high)
			xt_xlate_add(xl, "%u", sinfo->origsrc_port);
		else
			xt_xlate_add(xl, "%u-%u", sinfo->origsrc_port,
				     sinfo->origsrc_port_high);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_ORIGDST_PORT) {
		xt_xlate_add(xl, "%sct original proto-dst %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_ORIGDST_PORT ?
			     "!= " : "");
		if (sinfo->origdst_port == sinfo->origdst_port_high)
			xt_xlate_add(xl, "%u", sinfo->origdst_port);
		else
			xt_xlate_add(xl, "%u-%u", sinfo->origdst_port,
				     sinfo->origdst_port_high);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_REPLSRC_PORT) {
		xt_xlate_add(xl, "%sct reply proto-src %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_REPLSRC_PORT ?
			     "!= " : "");
		if (sinfo->replsrc_port == sinfo->replsrc_port_high)
			xt_xlate_add(xl, "%u", sinfo->replsrc_port);
		else
			xt_xlate_add(xl, "%u-%u", sinfo->replsrc_port,
				     sinfo->replsrc_port_high);
		space = " ";
	}

	if (sinfo->match_flags & XT_CONNTRACK_REPLDST_PORT) {
		xt_xlate_add(xl, "%sct reply proto-dst %s", space,
			     sinfo->invert_flags & XT_CONNTRACK_REPLDST_PORT ?
			     "!= " : "", sinfo->repldst_port);
		if (sinfo->repldst_port == sinfo->repldst_port_high)
			xt_xlate_add(xl, "%u", sinfo->repldst_port);
		else
			xt_xlate_add(xl, "%u-%u", sinfo->repldst_port,
				     sinfo->repldst_port_high);
	}

	return 1;
}

static int conntrack3_mt4_xlate(struct xt_xlate *xl,
				const struct xt_xlate_mt_params *params)
{
	return _conntrack3_mt_xlate(xl, params, NFPROTO_IPV4);
}

static int conntrack3_mt6_xlate(struct xt_xlate *xl,
				const struct xt_xlate_mt_params *params)
{
	return _conntrack3_mt_xlate(xl, params, NFPROTO_IPV6);
}

1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
static struct xtables_match conntrack_mt_reg[] = {
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 0,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_info)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_info)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack_print,
		.save          = conntrack_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack_mt_opts_v0,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 1,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack1_mt_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack1_mt4_print,
		.save          = conntrack1_mt4_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack2_mt_opts,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 1,
		.family        = NFPROTO_IPV6,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack1_mt_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack1_mt6_print,
		.save          = conntrack1_mt6_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack2_mt_opts,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 2,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack2_mt_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack2_mt_print,
		.save          = conntrack2_mt_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack2_mt_opts,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 2,
		.family        = NFPROTO_IPV6,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack2_mt_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack2_mt6_print,
		.save          = conntrack2_mt6_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack2_mt_opts,
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 3,
		.family        = NFPROTO_IPV4,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack3_mt_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack3_mt_print,
		.save          = conntrack3_mt_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack3_mt_opts,
1481
		.xlate	       = conntrack3_mt4_xlate,
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
	},
	{
		.version       = XTABLES_VERSION,
		.name          = "conntrack",
		.revision      = 3,
		.family        = NFPROTO_IPV6,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
		.help          = conntrack_mt_help,
		.x6_parse      = conntrack3_mt_parse,
		.x6_fcheck     = conntrack_mt_check,
		.print         = conntrack3_mt6_print,
		.save          = conntrack3_mt6_save,
		.alias	       = conntrack_print_name_alias,
		.x6_options    = conntrack3_mt_opts,
1497
		.xlate	       = conntrack3_mt6_xlate,
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
	},
	{
		.family        = NFPROTO_UNSPEC,
		.name          = "state",
		.real_name     = "conntrack",
		.revision      = 1,
		.ext_flags     = XTABLES_EXT_ALIAS,
		.version       = XTABLES_VERSION,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)),
		.help          = state_help,
		.print         = state_print,
		.save          = state_save,
		.x6_parse      = state_ct1_parse,
		.x6_options    = state_opts,
	},
	{
		.family        = NFPROTO_UNSPEC,
		.name          = "state",
		.real_name     = "conntrack",
		.revision      = 2,
		.ext_flags     = XTABLES_EXT_ALIAS,
		.version       = XTABLES_VERSION,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo2)),
		.help          = state_help,
		.print         = state_print,
		.save          = state_save,
		.x6_parse      = state_ct23_parse,
		.x6_options    = state_opts,
	},
	{
		.family        = NFPROTO_UNSPEC,
		.name          = "state",
		.real_name     = "conntrack",
		.revision      = 3,
		.ext_flags     = XTABLES_EXT_ALIAS,
		.version       = XTABLES_VERSION,
		.size          = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo3)),
		.help          = state_help,
		.print         = state_print,
		.save          = state_save,
		.x6_parse      = state_ct23_parse,
		.x6_options    = state_opts,
1543
		.xlate         = state_xlate,
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
	},
	{
		.family        = NFPROTO_UNSPEC,
		.name          = "state",
		.revision      = 0,
		.version       = XTABLES_VERSION,
		.size          = XT_ALIGN(sizeof(struct xt_state_info)),
		.userspacesize = XT_ALIGN(sizeof(struct xt_state_info)),
		.help          = state_help,
		.print         = state_print,
		.save          = state_save,
		.x6_parse      = state_parse,
		.x6_options    = state_opts,
	},
};

void _init(void)
{
	xtables_register_matches(conntrack_mt_reg, ARRAY_SIZE(conntrack_mt_reg));
}