blob: 401c2780736a7cfb3303110fd2336472c33afae3 [file] [log] [blame]
Jan Engelhardtddac6c52008-09-01 14:22:19 +02001/* Shared library add-on to ip6tables to add ICMP support. */
Philip Blundellb47050c2000-06-04 17:38:47 +00002#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01007#include <xtables.h>
Philip Blundellb47050c2000-06-04 17:38:47 +00008#include <linux/netfilter_ipv6/ip6_tables.h>
9
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000010struct icmpv6_names {
Philip Blundellb47050c2000-06-04 17:38:47 +000011 const char *name;
12 u_int8_t type;
13 u_int8_t code_min, code_max;
14};
15
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000016static const struct icmpv6_names icmpv6_codes[] = {
Philip Blundellb47050c2000-06-04 17:38:47 +000017 { "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
Patrick McHardy500f4832007-09-08 15:59:04 +000056print_icmpv6types(void)
Philip Blundellb47050c2000-06-04 17:38:47 +000057{
58 unsigned int i;
59 printf("Valid ICMPv6 Types:");
60
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000061 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);
Philip Blundellb47050c2000-06-04 17:38:47 +000067 else
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000068 printf("\n %s", icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +000069 }
70 else
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000071 printf("\n%s", icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +000072 }
73 printf("\n");
74}
75
Jan Engelhardt997045f2007-10-04 16:29:21 +000076static void icmp6_help(void)
Philip Blundellb47050c2000-06-04 17:38:47 +000077{
78 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020079"icmpv6 match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020080"[!] --icmpv6-type typename match icmpv6 type\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020081" (or numeric type or type/code)\n");
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000082 print_icmpv6types();
Philip Blundellb47050c2000-06-04 17:38:47 +000083}
84
Jan Engelhardt997045f2007-10-04 16:29:21 +000085static const struct option icmp6_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000086 { "icmpv6-type", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000087 { .name = NULL }
Philip Blundellb47050c2000-06-04 17:38:47 +000088};
89
Pablo Neira8115e542005-02-14 13:13:04 +000090static void
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000091parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[])
Philip Blundellb47050c2000-06-04 17:38:47 +000092{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000093 unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
Philip Blundellb47050c2000-06-04 17:38:47 +000094 unsigned int match = limit;
95 unsigned int i;
96
97 for (i = 0; i < limit; i++) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000098 if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type))
Philip Blundellb47050c2000-06-04 17:38:47 +000099 == 0) {
100 if (match != limit)
101 exit_error(PARAMETER_PROBLEM,
102 "Ambiguous ICMPv6 type `%s':"
103 " `%s' or `%s'?",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000104 icmpv6type,
105 icmpv6_codes[match].name,
106 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000107 match = i;
108 }
109 }
110
111 if (match != limit) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000112 *type = icmpv6_codes[match].type;
113 code[0] = icmpv6_codes[match].code_min;
114 code[1] = icmpv6_codes[match].code_max;
Philip Blundellb47050c2000-06-04 17:38:47 +0000115 } else {
116 char *slash;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000117 char buffer[strlen(icmpv6type) + 1];
Harald Welteb4719762001-07-23 02:14:22 +0000118 unsigned int number;
Philip Blundellb47050c2000-06-04 17:38:47 +0000119
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000120 strcpy(buffer, icmpv6type);
Philip Blundellb47050c2000-06-04 17:38:47 +0000121 slash = strchr(buffer, '/');
122
123 if (slash)
124 *slash = '\0';
125
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100126 if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX))
Philip Blundellb47050c2000-06-04 17:38:47 +0000127 exit_error(PARAMETER_PROBLEM,
128 "Invalid ICMPv6 type `%s'\n", buffer);
129 *type = number;
130 if (slash) {
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100131 if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX))
Philip Blundellb47050c2000-06-04 17:38:47 +0000132 exit_error(PARAMETER_PROBLEM,
133 "Invalid ICMPv6 code `%s'\n",
134 slash+1);
135 code[0] = code[1] = number;
136 } else {
137 code[0] = 0;
138 code[1] = 0xFF;
139 }
140 }
Philip Blundellb47050c2000-06-04 17:38:47 +0000141}
142
Jan Engelhardt997045f2007-10-04 16:29:21 +0000143static void icmp6_init(struct xt_entry_match *m)
Philip Blundellb47050c2000-06-04 17:38:47 +0000144{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000145 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000146
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000147 icmpv6info->code[1] = 0xFF;
Philip Blundellb47050c2000-06-04 17:38:47 +0000148}
149
Jan Engelhardt997045f2007-10-04 16:29:21 +0000150static int icmp6_parse(int c, char **argv, int invert, unsigned int *flags,
151 const void *entry, struct xt_entry_match **match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000152{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000153 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000154
155 switch (c) {
156 case '1':
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000157 if (*flags == 1)
158 exit_error(PARAMETER_PROBLEM,
159 "icmpv6 match: only use --icmpv6-type once!");
Harald Welteb77f1da2002-03-14 11:35:58 +0000160 check_inverse(optarg, &invert, &optind, 0);
Pablo Neira8115e542005-02-14 13:13:04 +0000161 parse_icmpv6(argv[optind-1], &icmpv6info->type,
162 icmpv6info->code);
Philip Blundellb47050c2000-06-04 17:38:47 +0000163 if (invert)
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000164 icmpv6info->invflags |= IP6T_ICMP_INV;
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000165 *flags = 1;
Philip Blundellb47050c2000-06-04 17:38:47 +0000166 break;
167
168 default:
169 return 0;
170 }
171
172 return 1;
173}
174
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000175static void print_icmpv6type(u_int8_t type,
Philip Blundellb47050c2000-06-04 17:38:47 +0000176 u_int8_t code_min, u_int8_t code_max,
177 int invert,
178 int numeric)
179{
180 if (!numeric) {
181 unsigned int i;
182
183 for (i = 0;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000184 i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
Philip Blundellb47050c2000-06-04 17:38:47 +0000185 i++) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000186 if (icmpv6_codes[i].type == type
187 && icmpv6_codes[i].code_min == code_min
188 && icmpv6_codes[i].code_max == code_max)
Philip Blundellb47050c2000-06-04 17:38:47 +0000189 break;
190 }
191
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000192 if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) {
Philip Blundellb47050c2000-06-04 17:38:47 +0000193 printf("%s%s ",
194 invert ? "!" : "",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000195 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000196 return;
197 }
198 }
199
200 if (invert)
201 printf("!");
202
203 printf("type %u", type);
204 if (code_min == 0 && code_max == 0xFF)
205 printf(" ");
206 else if (code_min == code_max)
207 printf(" code %u ", code_min);
208 else
209 printf(" codes %u-%u ", code_min, code_max);
210}
211
Jan Engelhardt997045f2007-10-04 16:29:21 +0000212static void icmp6_print(const void *ip, const struct xt_entry_match *match,
213 int numeric)
Philip Blundellb47050c2000-06-04 17:38:47 +0000214{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000215 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000216
Harald Weltecfaed1f2001-10-04 08:11:44 +0000217 printf("ipv6-icmp ");
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000218 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
219 icmpv6->invflags & IP6T_ICMP_INV,
Philip Blundellb47050c2000-06-04 17:38:47 +0000220 numeric);
221
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000222 if (icmpv6->invflags & ~IP6T_ICMP_INV)
Philip Blundellb47050c2000-06-04 17:38:47 +0000223 printf("Unknown invflags: 0x%X ",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000224 icmpv6->invflags & ~IP6T_ICMP_INV);
Philip Blundellb47050c2000-06-04 17:38:47 +0000225}
226
Jan Engelhardt997045f2007-10-04 16:29:21 +0000227static void icmp6_save(const void *ip, const struct xt_entry_match *match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000228{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000229 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000230
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000231 if (icmpv6->invflags & IP6T_ICMP_INV)
Philip Blundellb47050c2000-06-04 17:38:47 +0000232 printf("! ");
233
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000234 printf("--icmpv6-type %u", icmpv6->type);
235 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
236 printf("/%u", icmpv6->code[0]);
Philip Blundellb47050c2000-06-04 17:38:47 +0000237 printf(" ");
238}
239
Jan Engelhardt997045f2007-10-04 16:29:21 +0000240static void icmp6_check(unsigned int flags)
Philip Blundellb47050c2000-06-04 17:38:47 +0000241{
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000242 if (!flags)
243 exit_error(PARAMETER_PROBLEM,
244 "icmpv6 match: You must specify `--icmpv6-type'");
Philip Blundellb47050c2000-06-04 17:38:47 +0000245}
246
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200247static struct xtables_match icmp6_mt6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000248 .name = "icmp6",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200249 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100250 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200251 .size = XT_ALIGN(sizeof(struct ip6t_icmp)),
252 .userspacesize = XT_ALIGN(sizeof(struct ip6t_icmp)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000253 .help = icmp6_help,
254 .init = icmp6_init,
255 .parse = icmp6_parse,
256 .final_check = icmp6_check,
257 .print = icmp6_print,
258 .save = icmp6_save,
259 .extra_opts = icmp6_opts,
Philip Blundellb47050c2000-06-04 17:38:47 +0000260};
261
262void _init(void)
263{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200264 xtables_register_match(&icmp6_mt6_reg);
Philip Blundellb47050c2000-06-04 17:38:47 +0000265}