summaryrefslogtreecommitdiffstats
path: root/extensions/libipt_ah.c
blob: 39e3013d3e74bb2ded8576f422337fd32cf65cad (plain)
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
#include <stdio.h>
#include <xtables.h>
#include <linux/netfilter_ipv4/ipt_ah.h>

enum {
	O_AHSPI = 0,
};

static void ah_help(void)
{
	printf(
"ah match options:\n"
"[!] --ahspi spi[:spi]\n"
"				match spi (range)\n");
}

static const struct xt_option_entry ah_opts[] = {
	{.name = "ahspi", .id = O_AHSPI, .type = XTTYPE_UINT32RC,
	 .flags = XTOPT_INVERT | XTOPT_PUT,
	 XTOPT_POINTER(struct ipt_ah, spis)},
	XTOPT_TABLEEND,
};

static void ah_init(struct xt_entry_match *m)
{
	struct ipt_ah *ahinfo = (void *)m->data;

	ahinfo->spis[1] = ~0U;
}

static void ah_parse(struct xt_option_call *cb)
{
	struct ipt_ah *ahinfo = cb->data;

	xtables_option_parse(cb);
	if (cb->nvals == 1)
		ahinfo->spis[1] = ahinfo->spis[0];
	if (cb->invert)
		ahinfo->invflags |= IPT_AH_INV_SPI;
}

static bool skip_spi_match(uint32_t min, uint32_t max, bool inv)
{
	return min == 0 && max == UINT32_MAX && !inv;
}

static void
print_spis(const char *name, uint32_t min, uint32_t max,
	    int invert)
{
	const char *inv = invert ? "!" : "";

	if (!skip_spi_match(min, max, invert)) {
		printf("%s", name);
		if (min == max) {
			printf(":%s", inv);
			printf("%u", min);
		} else {
			printf("s:%s", inv);
			printf("%u",min);
			printf(":");
			printf("%u",max);
		}
	}
}

static void ah_print(const void *ip, const struct xt_entry_match *match,
                     int numeric)
{
	const struct ipt_ah *ah = (struct ipt_ah *)match->data;

	printf(" ah ");
	print_spis("spi", ah->spis[0], ah->spis[1],
		    ah->invflags & IPT_AH_INV_SPI);
	if (ah->invflags & ~IPT_AH_INV_MASK)
		printf(" Unknown invflags: 0x%X",
		       ah->invflags & ~IPT_AH_INV_MASK);
}

static void ah_save(const void *ip, const struct xt_entry_match *match)
{
	const struct ipt_ah *ahinfo = (struct ipt_ah *)match->data;
	bool inv_spi = ahinfo->invflags & IPT_AH_INV_SPI;

	if (!skip_spi_match(ahinfo->spis[0], ahinfo->spis[1], inv_spi)) {
		printf("%s --ahspi ", inv_spi ? " !" : "");
		if (ahinfo->spis[0]
		    != ahinfo->spis[1])
			printf("%u:%u",
			       ahinfo->spis[0],
			       ahinfo->spis[1]);
		else
			printf("%u",
			       ahinfo->spis[0]);
	}

}

static int ah_xlate(struct xt_xlate *xl,
		    const struct xt_xlate_mt_params *params)
{
	const struct ipt_ah *ahinfo = (struct ipt_ah *)params->match->data;
	bool inv_spi = ahinfo->invflags & IPT_AH_INV_SPI;

	if (!skip_spi_match(ahinfo->spis[0], ahinfo->spis[1], inv_spi)) {
		xt_xlate_add(xl, "ah spi%s ", inv_spi ? " !=" : "");
		if (ahinfo->spis[0] != ahinfo->spis[1])
			xt_xlate_add(xl, "%u-%u", ahinfo->spis[0],
				   ahinfo->spis[1]);
		else
			xt_xlate_add(xl, "%u", ahinfo->spis[0]);
	} else {
		xt_xlate_add(xl, "meta l4proto ah");
	}

	return 1;
}

static struct xtables_match ah_mt_reg = {
	.name		= "ah",
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_IPV4,
	.size		= XT_ALIGN(sizeof(struct ipt_ah)),
	.userspacesize	= XT_ALIGN(sizeof(struct ipt_ah)),
	.help		= ah_help,
	.init		= ah_init,
	.print		= ah_print,
	.save		= ah_save,
	.x6_parse	= ah_parse,
	.x6_options	= ah_opts,
	.xlate		= ah_xlate,
};

void
_init(void)
{
	xtables_register_match(&ah_mt_reg);
}