libip6t_icmp6.c revision 9ee386a1b6d7704b259460152c959ab0e79e02aa
1/* Shared library add-on to iptables to add ICMP support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <ip6tables.h>
8#include <linux/netfilter_ipv6/ip6_tables.h>
9
10struct icmpv6_names {
11	const char *name;
12	u_int8_t type;
13	u_int8_t code_min, code_max;
14};
15
16static const struct icmpv6_names icmpv6_codes[] = {
17	{ "destination-unreachable", 1, 0, 0xFF },
18	{   "no-route", 1, 0, 0 },
19	{   "communication-prohibited", 1, 1, 1 },
20	{   "address-unreachable", 1, 3, 3 },
21	{   "port-unreachable", 1, 4, 4 },
22
23	{ "packet-too-big", 2, 0, 0xFF },
24
25	{ "time-exceeded", 3, 0, 0xFF },
26	/* Alias */ { "ttl-exceeded", 3, 0, 0xFF },
27	{   "ttl-zero-during-transit", 3, 0, 0 },
28	{   "ttl-zero-during-reassembly", 3, 1, 1 },
29
30	{ "parameter-problem", 4, 0, 0xFF },
31	{   "bad-header", 4, 0, 0 },
32	{   "unknown-header-type", 4, 1, 1 },
33	{   "unknown-option", 4, 2, 2 },
34
35	{ "echo-request", 128, 0, 0xFF },
36	/* Alias */ { "ping", 128, 0, 0xFF },
37
38	{ "echo-reply", 129, 0, 0xFF },
39	/* Alias */ { "pong", 129, 0, 0xFF },
40
41	{ "router-solicitation", 133, 0, 0xFF },
42
43	{ "router-advertisement", 134, 0, 0xFF },
44
45	{ "neighbour-solicitation", 135, 0, 0xFF },
46	/* Alias */ { "neighbor-solicitation", 135, 0, 0xFF },
47
48	{ "neighbour-advertisement", 136, 0, 0xFF },
49	/* Alias */ { "neighbor-advertisement", 136, 0, 0xFF },
50
51	{ "redirect", 137, 0, 0xFF },
52
53};
54
55static void
56print_icmpv6types(void)
57{
58	unsigned int i;
59	printf("Valid ICMPv6 Types:");
60
61	for (i = 0; i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); i++) {
62		if (i && icmpv6_codes[i].type == icmpv6_codes[i-1].type) {
63			if (icmpv6_codes[i].code_min == icmpv6_codes[i-1].code_min
64			    && (icmpv6_codes[i].code_max
65				== icmpv6_codes[i-1].code_max))
66				printf(" (%s)", icmpv6_codes[i].name);
67			else
68				printf("\n   %s", icmpv6_codes[i].name);
69		}
70		else
71			printf("\n%s", icmpv6_codes[i].name);
72	}
73	printf("\n");
74}
75
76/* Function which prints out usage message. */
77static void icmp6_help(void)
78{
79	printf(
80"ICMPv6 v%s options:\n"
81" --icmpv6-type [!] typename	match icmpv6 type\n"
82"				(or numeric type or type/code)\n"
83"\n", IPTABLES_VERSION);
84	print_icmpv6types();
85}
86
87static const struct option icmp6_opts[] = {
88	{ "icmpv6-type", 1, NULL, '1' },
89	{ .name = NULL }
90};
91
92static void
93parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[])
94{
95	unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
96	unsigned int match = limit;
97	unsigned int i;
98
99	for (i = 0; i < limit; i++) {
100		if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type))
101		    == 0) {
102			if (match != limit)
103				exit_error(PARAMETER_PROBLEM,
104					   "Ambiguous ICMPv6 type `%s':"
105					   " `%s' or `%s'?",
106					   icmpv6type,
107					   icmpv6_codes[match].name,
108					   icmpv6_codes[i].name);
109			match = i;
110		}
111	}
112
113	if (match != limit) {
114		*type = icmpv6_codes[match].type;
115		code[0] = icmpv6_codes[match].code_min;
116		code[1] = icmpv6_codes[match].code_max;
117	} else {
118		char *slash;
119		char buffer[strlen(icmpv6type) + 1];
120		unsigned int number;
121
122		strcpy(buffer, icmpv6type);
123		slash = strchr(buffer, '/');
124
125		if (slash)
126			*slash = '\0';
127
128		if (string_to_number(buffer, 0, 255, &number) == -1)
129			exit_error(PARAMETER_PROBLEM,
130				   "Invalid ICMPv6 type `%s'\n", buffer);
131		*type = number;
132		if (slash) {
133			if (string_to_number(slash+1, 0, 255, &number) == -1)
134				exit_error(PARAMETER_PROBLEM,
135					   "Invalid ICMPv6 code `%s'\n",
136					   slash+1);
137			code[0] = code[1] = number;
138		} else {
139			code[0] = 0;
140			code[1] = 0xFF;
141		}
142	}
143}
144
145/* Initialize the match. */
146static void icmp6_init(struct xt_entry_match *m)
147{
148	struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
149
150	icmpv6info->code[1] = 0xFF;
151}
152
153/* Function which parses command options; returns true if it
154   ate an option */
155static int icmp6_parse(int c, char **argv, int invert, unsigned int *flags,
156                       const void *entry, struct xt_entry_match **match)
157{
158	struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data;
159
160	switch (c) {
161	case '1':
162		if (*flags == 1)
163			exit_error(PARAMETER_PROBLEM,
164				   "icmpv6 match: only use --icmpv6-type once!");
165		check_inverse(optarg, &invert, &optind, 0);
166		parse_icmpv6(argv[optind-1], &icmpv6info->type,
167			     icmpv6info->code);
168		if (invert)
169			icmpv6info->invflags |= IP6T_ICMP_INV;
170		*flags = 1;
171		break;
172
173	default:
174		return 0;
175	}
176
177	return 1;
178}
179
180static void print_icmpv6type(u_int8_t type,
181			   u_int8_t code_min, u_int8_t code_max,
182			   int invert,
183			   int numeric)
184{
185	if (!numeric) {
186		unsigned int i;
187
188		for (i = 0;
189		     i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
190		     i++) {
191			if (icmpv6_codes[i].type == type
192			    && icmpv6_codes[i].code_min == code_min
193			    && icmpv6_codes[i].code_max == code_max)
194				break;
195		}
196
197		if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) {
198			printf("%s%s ",
199			       invert ? "!" : "",
200			       icmpv6_codes[i].name);
201			return;
202		}
203	}
204
205	if (invert)
206		printf("!");
207
208	printf("type %u", type);
209	if (code_min == 0 && code_max == 0xFF)
210		printf(" ");
211	else if (code_min == code_max)
212		printf(" code %u ", code_min);
213	else
214		printf(" codes %u-%u ", code_min, code_max);
215}
216
217/* Prints out the union ipt_matchinfo. */
218static void icmp6_print(const void *ip, const struct xt_entry_match *match,
219                        int numeric)
220{
221	const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
222
223	printf("ipv6-icmp ");
224	print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
225		       icmpv6->invflags & IP6T_ICMP_INV,
226		       numeric);
227
228	if (icmpv6->invflags & ~IP6T_ICMP_INV)
229		printf("Unknown invflags: 0x%X ",
230		       icmpv6->invflags & ~IP6T_ICMP_INV);
231}
232
233/* Saves the match in parsable form to stdout. */
234static void icmp6_save(const void *ip, const struct xt_entry_match *match)
235{
236	const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
237
238	if (icmpv6->invflags & IP6T_ICMP_INV)
239		printf("! ");
240
241	printf("--icmpv6-type %u", icmpv6->type);
242	if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
243		printf("/%u", icmpv6->code[0]);
244	printf(" ");
245}
246
247static void icmp6_check(unsigned int flags)
248{
249	if (!flags)
250		exit_error(PARAMETER_PROBLEM,
251			   "icmpv6 match: You must specify `--icmpv6-type'");
252}
253
254static struct ip6tables_match icmp6_match6 = {
255	.name 		= "icmp6",
256	.version 	= IPTABLES_VERSION,
257	.size		= IP6T_ALIGN(sizeof(struct ip6t_icmp)),
258	.userspacesize	= IP6T_ALIGN(sizeof(struct ip6t_icmp)),
259	.help		= icmp6_help,
260	.init		= icmp6_init,
261	.parse		= icmp6_parse,
262	.final_check	= icmp6_check,
263	.print		= icmp6_print,
264	.save		= icmp6_save,
265	.extra_opts	= icmp6_opts,
266};
267
268void _init(void)
269{
270	register_match6(&icmp6_match6);
271}
272