• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <xtables.h>
3 #include <linux/netfilter/xt_esp.h>
4 
5 enum {
6 	O_ESPSPI = 0,
7 };
8 
esp_help(void)9 static void esp_help(void)
10 {
11 	printf(
12 "esp match options:\n"
13 "[!] --espspi spi[:spi]\n"
14 "				match spi (range)\n");
15 }
16 
17 static const struct xt_option_entry esp_opts[] = {
18 	{.name = "espspi", .id = O_ESPSPI, .type = XTTYPE_UINT32RC,
19 	 .flags = XTOPT_INVERT | XTOPT_PUT,
20 	 XTOPT_POINTER(struct xt_esp, spis)},
21 	XTOPT_TABLEEND,
22 };
23 
esp_init(struct xt_entry_match * m)24 static void esp_init(struct xt_entry_match *m)
25 {
26 	struct xt_esp *espinfo = (void *)m->data;
27 
28 	espinfo->spis[1] = ~0U;
29 }
30 
esp_parse(struct xt_option_call * cb)31 static void esp_parse(struct xt_option_call *cb)
32 {
33 	struct xt_esp *espinfo = cb->data;
34 
35 	xtables_option_parse(cb);
36 	if (cb->nvals == 1)
37 		espinfo->spis[1] = espinfo->spis[0];
38 	if (cb->invert)
39 		espinfo->invflags |= XT_ESP_INV_SPI;
40 }
41 
skip_spis_match(uint32_t min,uint32_t max,bool inv)42 static bool skip_spis_match(uint32_t min, uint32_t max, bool inv)
43 {
44 	return min == 0 && max == UINT32_MAX && !inv;
45 }
46 
47 static void
print_spis(const char * name,uint32_t min,uint32_t max,int invert)48 print_spis(const char *name, uint32_t min, uint32_t max,
49 	    int invert)
50 {
51 	const char *inv = invert ? "!" : "";
52 
53 	if (!skip_spis_match(min, max, invert)) {
54 		if (min == max)
55 			printf(" %s:%s%u", name, inv, min);
56 		else
57 			printf(" %ss:%s%u:%u", name, inv, min, max);
58 	}
59 }
60 
61 static void
esp_print(const void * ip,const struct xt_entry_match * match,int numeric)62 esp_print(const void *ip, const struct xt_entry_match *match, int numeric)
63 {
64 	const struct xt_esp *esp = (struct xt_esp *)match->data;
65 
66 	printf(" esp");
67 	print_spis("spi", esp->spis[0], esp->spis[1],
68 		    esp->invflags & XT_ESP_INV_SPI);
69 	if (esp->invflags & ~XT_ESP_INV_MASK)
70 		printf(" Unknown invflags: 0x%X",
71 		       esp->invflags & ~XT_ESP_INV_MASK);
72 }
73 
esp_save(const void * ip,const struct xt_entry_match * match)74 static void esp_save(const void *ip, const struct xt_entry_match *match)
75 {
76 	const struct xt_esp *espinfo = (struct xt_esp *)match->data;
77 	bool inv_spi = espinfo->invflags & XT_ESP_INV_SPI;
78 
79 	if (!skip_spis_match(espinfo->spis[0], espinfo->spis[1], inv_spi)) {
80 		printf("%s --espspi ", inv_spi ? " !" : "");
81 		if (espinfo->spis[0]
82 		    != espinfo->spis[1])
83 			printf("%u:%u",
84 			       espinfo->spis[0],
85 			       espinfo->spis[1]);
86 		else
87 			printf("%u",
88 			       espinfo->spis[0]);
89 	}
90 
91 }
92 
esp_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)93 static int esp_xlate(struct xt_xlate *xl,
94 		     const struct xt_xlate_mt_params *params)
95 {
96 	const struct xt_esp *espinfo = (struct xt_esp *)params->match->data;
97 	bool inv_spi = espinfo->invflags & XT_ESP_INV_SPI;
98 
99 	if (!skip_spis_match(espinfo->spis[0], espinfo->spis[1], inv_spi)) {
100 		xt_xlate_add(xl, "esp spi%s", inv_spi ? " !=" : "");
101 		if (espinfo->spis[0] != espinfo->spis[1])
102 			xt_xlate_add(xl, " %u-%u", espinfo->spis[0],
103 				   espinfo->spis[1]);
104 		else
105 			xt_xlate_add(xl, " %u", espinfo->spis[0]);
106 	} else if (afinfo->family == NFPROTO_IPV4) {
107 		xt_xlate_add(xl, "meta l4proto esp");
108 	} else if (afinfo->family == NFPROTO_IPV6) {
109 		xt_xlate_add(xl, "exthdr esp exists");
110 	} else {
111 		return 0;
112 	}
113 
114 	return 1;
115 }
116 
117 static struct xtables_match esp_match = {
118 	.family		= NFPROTO_UNSPEC,
119 	.name		= "esp",
120 	.version	= XTABLES_VERSION,
121 	.size		= XT_ALIGN(sizeof(struct xt_esp)),
122 	.userspacesize	= XT_ALIGN(sizeof(struct xt_esp)),
123 	.help		= esp_help,
124 	.init		= esp_init,
125 	.print		= esp_print,
126 	.save		= esp_save,
127 	.x6_parse	= esp_parse,
128 	.x6_options	= esp_opts,
129 	.xlate		= esp_xlate,
130 };
131 
132 void
_init(void)133 _init(void)
134 {
135 	xtables_register_match(&esp_match);
136 }
137