• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <xtables.h>
5 #include <limits.h> /* INT_MAX in ip6_tables.h */
6 #include <linux/netfilter_ipv6/ip6_tables.h>
7 #include <netinet/icmp6.h>
8 
9 #include "libxt_icmp.h"
10 
11 enum {
12 	O_ICMPV6_TYPE = 0,
13 };
14 
15 static const struct xt_icmp_names icmpv6_codes[] = {
16 	{ "destination-unreachable", 1, 0, 0xFF },
17 	{   "no-route", 1, 0, 0 },
18 	{   "communication-prohibited", 1, 1, 1 },
19 	{   "beyond-scope", 1, 2, 2 },
20 	{   "address-unreachable", 1, 3, 3 },
21 	{   "port-unreachable", 1, 4, 4 },
22 	{   "failed-policy", 1, 5, 5 },
23 	{   "reject-route", 1, 6, 6 },
24 
25 	{ "packet-too-big", 2, 0, 0xFF },
26 
27 	{ "time-exceeded", 3, 0, 0xFF },
28 	/* Alias */ { "ttl-exceeded", 3, 0, 0xFF },
29 	{   "ttl-zero-during-transit", 3, 0, 0 },
30 	{   "ttl-zero-during-reassembly", 3, 1, 1 },
31 
32 	{ "parameter-problem", 4, 0, 0xFF },
33 	{   "bad-header", 4, 0, 0 },
34 	{   "unknown-header-type", 4, 1, 1 },
35 	{   "unknown-option", 4, 2, 2 },
36 
37 	{ "echo-request", 128, 0, 0xFF },
38 	/* Alias */ { "ping", 128, 0, 0xFF },
39 
40 	{ "echo-reply", 129, 0, 0xFF },
41 	/* Alias */ { "pong", 129, 0, 0xFF },
42 
43 	{ "router-solicitation", 133, 0, 0xFF },
44 
45 	{ "router-advertisement", 134, 0, 0xFF },
46 
47 	{ "neighbour-solicitation", 135, 0, 0xFF },
48 	/* Alias */ { "neighbor-solicitation", 135, 0, 0xFF },
49 
50 	{ "neighbour-advertisement", 136, 0, 0xFF },
51 	/* Alias */ { "neighbor-advertisement", 136, 0, 0xFF },
52 
53 	{ "redirect", 137, 0, 0xFF },
54 
55 };
56 
icmp6_help(void)57 static void icmp6_help(void)
58 {
59 	printf(
60 "icmpv6 match options:\n"
61 "[!] --icmpv6-type typename	match icmpv6 type\n"
62 "				(or numeric type or type/code)\n");
63 	printf("Valid ICMPv6 Types:");
64 	xt_print_icmp_types(icmpv6_codes, ARRAY_SIZE(icmpv6_codes));
65 }
66 
67 static const struct xt_option_entry icmp6_opts[] = {
68 	{.name = "icmpv6-type", .id = O_ICMPV6_TYPE, .type = XTTYPE_STRING,
69 	 .flags = XTOPT_MAND | XTOPT_INVERT},
70 	XTOPT_TABLEEND,
71 };
72 
73 static void
parse_icmpv6(const char * icmpv6type,uint8_t * type,uint8_t code[])74 parse_icmpv6(const char *icmpv6type, uint8_t *type, uint8_t code[])
75 {
76 	static const unsigned int limit = ARRAY_SIZE(icmpv6_codes);
77 	unsigned int match = limit;
78 	unsigned int i;
79 
80 	for (i = 0; i < limit; i++) {
81 		if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type))
82 		    == 0) {
83 			if (match != limit)
84 				xtables_error(PARAMETER_PROBLEM,
85 					   "Ambiguous ICMPv6 type `%s':"
86 					   " `%s' or `%s'?",
87 					   icmpv6type,
88 					   icmpv6_codes[match].name,
89 					   icmpv6_codes[i].name);
90 			match = i;
91 		}
92 	}
93 
94 	if (match != limit) {
95 		*type = icmpv6_codes[match].type;
96 		code[0] = icmpv6_codes[match].code_min;
97 		code[1] = icmpv6_codes[match].code_max;
98 	} else {
99 		char *slash;
100 		char buffer[strlen(icmpv6type) + 1];
101 		unsigned int number;
102 
103 		strcpy(buffer, icmpv6type);
104 		slash = strchr(buffer, '/');
105 
106 		if (slash)
107 			*slash = '\0';
108 
109 		if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX))
110 			xtables_error(PARAMETER_PROBLEM,
111 				   "Invalid ICMPv6 type `%s'\n", buffer);
112 		*type = number;
113 		if (slash) {
114 			if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX))
115 				xtables_error(PARAMETER_PROBLEM,
116 					   "Invalid ICMPv6 code `%s'\n",
117 					   slash+1);
118 			code[0] = code[1] = number;
119 		} else {
120 			code[0] = 0;
121 			code[1] = 0xFF;
122 		}
123 	}
124 }
125 
icmp6_init(struct xt_entry_match * m)126 static void icmp6_init(struct xt_entry_match *m)
127 {
128 	struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
129 
130 	icmpv6info->code[1] = 0xFF;
131 }
132 
icmp6_parse(struct xt_option_call * cb)133 static void icmp6_parse(struct xt_option_call *cb)
134 {
135 	struct ip6t_icmp *icmpv6info = cb->data;
136 
137 	xtables_option_parse(cb);
138 	parse_icmpv6(cb->arg, &icmpv6info->type, icmpv6info->code);
139 	if (cb->invert)
140 		icmpv6info->invflags |= IP6T_ICMP_INV;
141 }
142 
print_icmpv6type(uint8_t type,uint8_t code_min,uint8_t code_max,int invert,int numeric)143 static void print_icmpv6type(uint8_t type,
144 			   uint8_t code_min, uint8_t code_max,
145 			   int invert,
146 			   int numeric)
147 {
148 	if (!numeric) {
149 		unsigned int i;
150 
151 		for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i)
152 			if (icmpv6_codes[i].type == type
153 			    && icmpv6_codes[i].code_min == code_min
154 			    && icmpv6_codes[i].code_max == code_max)
155 				break;
156 
157 		if (i != ARRAY_SIZE(icmpv6_codes)) {
158 			printf(" %s%s",
159 			       invert ? "!" : "",
160 			       icmpv6_codes[i].name);
161 			return;
162 		}
163 	}
164 
165 	if (invert)
166 		printf(" !");
167 
168 	printf("type %u", type);
169 	if (code_min == code_max)
170 		printf(" code %u", code_min);
171 	else if (code_min != 0 || code_max != 0xFF)
172 		printf(" codes %u-%u", code_min, code_max);
173 }
174 
icmp6_print(const void * ip,const struct xt_entry_match * match,int numeric)175 static void icmp6_print(const void *ip, const struct xt_entry_match *match,
176                         int numeric)
177 {
178 	const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
179 
180 	printf(" ipv6-icmp");
181 	print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
182 		       icmpv6->invflags & IP6T_ICMP_INV,
183 		       numeric);
184 
185 	if (icmpv6->invflags & ~IP6T_ICMP_INV)
186 		printf(" Unknown invflags: 0x%X",
187 		       icmpv6->invflags & ~IP6T_ICMP_INV);
188 }
189 
icmp6_save(const void * ip,const struct xt_entry_match * match)190 static void icmp6_save(const void *ip, const struct xt_entry_match *match)
191 {
192 	const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
193 
194 	if (icmpv6->invflags & IP6T_ICMP_INV)
195 		printf(" !");
196 
197 	printf(" --icmpv6-type %u", icmpv6->type);
198 	if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
199 		printf("/%u", icmpv6->code[0]);
200 }
201 
202 #define XT_ICMPV6_TYPE(type)	(type - ND_ROUTER_SOLICIT)
203 
204 static const char *icmp6_type_xlate_array[] = {
205 	[XT_ICMPV6_TYPE(ND_ROUTER_SOLICIT)]	= "nd-router-solicit",
206 	[XT_ICMPV6_TYPE(ND_ROUTER_ADVERT)]	= "nd-router-advert",
207 	[XT_ICMPV6_TYPE(ND_NEIGHBOR_SOLICIT)]	= "nd-neighbor-solicit",
208 	[XT_ICMPV6_TYPE(ND_NEIGHBOR_ADVERT)]	= "nd-neighbor-advert",
209 	[XT_ICMPV6_TYPE(ND_REDIRECT)]		= "nd-redirect",
210 };
211 
icmp6_type_xlate(unsigned int type)212 static const char *icmp6_type_xlate(unsigned int type)
213 {
214 	if (type < ND_ROUTER_SOLICIT || type > ND_REDIRECT)
215 		return NULL;
216 
217 	return icmp6_type_xlate_array[XT_ICMPV6_TYPE(type)];
218 }
219 
type_xlate_print(struct xt_xlate * xl,unsigned int icmptype,unsigned int code_min,unsigned int code_max)220 static unsigned int type_xlate_print(struct xt_xlate *xl, unsigned int icmptype,
221 				     unsigned int code_min,
222 				     unsigned int code_max)
223 {
224 	unsigned int i;
225 	const char *type_name;
226 
227 	if (code_min == code_max)
228 		return 0;
229 
230 	type_name = icmp6_type_xlate(icmptype);
231 
232 	if (type_name) {
233 		xt_xlate_add(xl, "%s", type_name);
234 	} else {
235 		for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i)
236 			if (icmpv6_codes[i].type == icmptype &&
237 			    icmpv6_codes[i].code_min == code_min &&
238 			    icmpv6_codes[i].code_max == code_max)
239 				break;
240 
241 		if (i != ARRAY_SIZE(icmpv6_codes))
242 			xt_xlate_add(xl, "%s", icmpv6_codes[i].name);
243 		else
244 			return 0;
245 	}
246 
247 	return 1;
248 }
249 
icmp6_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)250 static int icmp6_xlate(struct xt_xlate *xl,
251 		       const struct xt_xlate_mt_params *params)
252 {
253 	const struct ip6t_icmp *info = (struct ip6t_icmp *)params->match->data;
254 
255 	xt_xlate_add(xl, "icmpv6 type%s ",
256 		     (info->invflags & IP6T_ICMP_INV) ? " !=" : "");
257 
258 	if (!type_xlate_print(xl, info->type, info->code[0], info->code[1]))
259 		return 0;
260 
261 	return 1;
262 }
263 
264 static struct xtables_match icmp6_mt6_reg = {
265 	.name 		= "icmp6",
266 	.version 	= XTABLES_VERSION,
267 	.family		= NFPROTO_IPV6,
268 	.size		= XT_ALIGN(sizeof(struct ip6t_icmp)),
269 	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_icmp)),
270 	.help		= icmp6_help,
271 	.init		= icmp6_init,
272 	.print		= icmp6_print,
273 	.save		= icmp6_save,
274 	.x6_parse	= icmp6_parse,
275 	.x6_options	= icmp6_opts,
276 	.xlate		= icmp6_xlate,
277 };
278 
_init(void)279 void _init(void)
280 {
281 	xtables_register_match(&icmp6_mt6_reg);
282 }
283