blob: febf52375dd754d7a3a767de492f99328a3d9d14 [file] [log] [blame]
Philip Blundellb47050c2000-06-04 17:38:47 +00001/* 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
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
76/* Function which prints out usage message. */
Jan Engelhardt997045f2007-10-04 16:29:21 +000077static void icmp6_help(void)
Philip Blundellb47050c2000-06-04 17:38:47 +000078{
79 printf(
80"ICMPv6 v%s options:\n"
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000081" --icmpv6-type [!] typename match icmpv6 type\n"
Philip Blundellb47050c2000-06-04 17:38:47 +000082" (or numeric type or type/code)\n"
Harald Welte80fe35d2002-05-29 13:08:15 +000083"\n", IPTABLES_VERSION);
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000084 print_icmpv6types();
Philip Blundellb47050c2000-06-04 17:38:47 +000085}
86
Jan Engelhardt997045f2007-10-04 16:29:21 +000087static const struct option icmp6_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000088 { "icmpv6-type", 1, NULL, '1' },
89 { }
Philip Blundellb47050c2000-06-04 17:38:47 +000090};
91
Pablo Neira8115e542005-02-14 13:13:04 +000092static void
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000093parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[])
Philip Blundellb47050c2000-06-04 17:38:47 +000094{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000095 unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
Philip Blundellb47050c2000-06-04 17:38:47 +000096 unsigned int match = limit;
97 unsigned int i;
98
99 for (i = 0; i < limit; i++) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000100 if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type))
Philip Blundellb47050c2000-06-04 17:38:47 +0000101 == 0) {
102 if (match != limit)
103 exit_error(PARAMETER_PROBLEM,
104 "Ambiguous ICMPv6 type `%s':"
105 " `%s' or `%s'?",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000106 icmpv6type,
107 icmpv6_codes[match].name,
108 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000109 match = i;
110 }
111 }
112
113 if (match != limit) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000114 *type = icmpv6_codes[match].type;
115 code[0] = icmpv6_codes[match].code_min;
116 code[1] = icmpv6_codes[match].code_max;
Philip Blundellb47050c2000-06-04 17:38:47 +0000117 } else {
118 char *slash;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000119 char buffer[strlen(icmpv6type) + 1];
Harald Welteb4719762001-07-23 02:14:22 +0000120 unsigned int number;
Philip Blundellb47050c2000-06-04 17:38:47 +0000121
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000122 strcpy(buffer, icmpv6type);
Philip Blundellb47050c2000-06-04 17:38:47 +0000123 slash = strchr(buffer, '/');
124
125 if (slash)
126 *slash = '\0';
127
Harald Welteb4719762001-07-23 02:14:22 +0000128 if (string_to_number(buffer, 0, 255, &number) == -1)
Philip Blundellb47050c2000-06-04 17:38:47 +0000129 exit_error(PARAMETER_PROBLEM,
130 "Invalid ICMPv6 type `%s'\n", buffer);
131 *type = number;
132 if (slash) {
Harald Welteb4719762001-07-23 02:14:22 +0000133 if (string_to_number(slash+1, 0, 255, &number) == -1)
Philip Blundellb47050c2000-06-04 17:38:47 +0000134 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 }
Philip Blundellb47050c2000-06-04 17:38:47 +0000143}
144
145/* Initialize the match. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000146static void icmp6_init(struct xt_entry_match *m)
Philip Blundellb47050c2000-06-04 17:38:47 +0000147{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000148 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000149
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000150 icmpv6info->code[1] = 0xFF;
Philip Blundellb47050c2000-06-04 17:38:47 +0000151}
152
153/* Function which parses command options; returns true if it
154 ate an option */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000155static int icmp6_parse(int c, char **argv, int invert, unsigned int *flags,
156 const void *entry, struct xt_entry_match **match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000157{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000158 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000159
160 switch (c) {
161 case '1':
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000162 if (*flags == 1)
163 exit_error(PARAMETER_PROBLEM,
164 "icmpv6 match: only use --icmpv6-type once!");
Harald Welteb77f1da2002-03-14 11:35:58 +0000165 check_inverse(optarg, &invert, &optind, 0);
Pablo Neira8115e542005-02-14 13:13:04 +0000166 parse_icmpv6(argv[optind-1], &icmpv6info->type,
167 icmpv6info->code);
Philip Blundellb47050c2000-06-04 17:38:47 +0000168 if (invert)
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000169 icmpv6info->invflags |= IP6T_ICMP_INV;
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000170 *flags = 1;
Philip Blundellb47050c2000-06-04 17:38:47 +0000171 break;
172
173 default:
174 return 0;
175 }
176
177 return 1;
178}
179
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000180static void print_icmpv6type(u_int8_t type,
Philip Blundellb47050c2000-06-04 17:38:47 +0000181 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;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000189 i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
Philip Blundellb47050c2000-06-04 17:38:47 +0000190 i++) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000191 if (icmpv6_codes[i].type == type
192 && icmpv6_codes[i].code_min == code_min
193 && icmpv6_codes[i].code_max == code_max)
Philip Blundellb47050c2000-06-04 17:38:47 +0000194 break;
195 }
196
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000197 if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) {
Philip Blundellb47050c2000-06-04 17:38:47 +0000198 printf("%s%s ",
199 invert ? "!" : "",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000200 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000201 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. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000218static void icmp6_print(const void *ip, const struct xt_entry_match *match,
219 int numeric)
Philip Blundellb47050c2000-06-04 17:38:47 +0000220{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000221 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000222
Harald Weltecfaed1f2001-10-04 08:11:44 +0000223 printf("ipv6-icmp ");
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000224 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
225 icmpv6->invflags & IP6T_ICMP_INV,
Philip Blundellb47050c2000-06-04 17:38:47 +0000226 numeric);
227
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000228 if (icmpv6->invflags & ~IP6T_ICMP_INV)
Philip Blundellb47050c2000-06-04 17:38:47 +0000229 printf("Unknown invflags: 0x%X ",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000230 icmpv6->invflags & ~IP6T_ICMP_INV);
Philip Blundellb47050c2000-06-04 17:38:47 +0000231}
232
233/* Saves the match in parsable form to stdout. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000234static void icmp6_save(const void *ip, const struct xt_entry_match *match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000235{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000236 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000237
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000238 if (icmpv6->invflags & IP6T_ICMP_INV)
Philip Blundellb47050c2000-06-04 17:38:47 +0000239 printf("! ");
240
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000241 printf("--icmpv6-type %u", icmpv6->type);
242 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
243 printf("/%u", icmpv6->code[0]);
Philip Blundellb47050c2000-06-04 17:38:47 +0000244 printf(" ");
245}
246
Jan Engelhardt997045f2007-10-04 16:29:21 +0000247static void icmp6_check(unsigned int flags)
Philip Blundellb47050c2000-06-04 17:38:47 +0000248{
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000249 if (!flags)
250 exit_error(PARAMETER_PROBLEM,
251 "icmpv6 match: You must specify `--icmpv6-type'");
Philip Blundellb47050c2000-06-04 17:38:47 +0000252}
253
Jan Engelhardt997045f2007-10-04 16:29:21 +0000254static struct ip6tables_match icmp6_match6 = {
Harald Welte02aa7332005-02-01 15:38:20 +0000255 .name = "icmp6",
256 .version = IPTABLES_VERSION,
257 .size = IP6T_ALIGN(sizeof(struct ip6t_icmp)),
258 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_icmp)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000259 .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,
Philip Blundellb47050c2000-06-04 17:38:47 +0000266};
267
268void _init(void)
269{
Jan Engelhardt997045f2007-10-04 16:29:21 +0000270 register_match6(&icmp6_match6);
Philip Blundellb47050c2000-06-04 17:38:47 +0000271}