blob: 68b940bd517d5fe19996712af3d0f3dd9bbede5c [file] [log] [blame]
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +01001#include <stdint.h>
Philip Blundellb47050c2000-06-04 17:38:47 +00002#include <stdio.h>
Philip Blundellb47050c2000-06-04 17:38:47 +00003#include <string.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01004#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +01005#include <limits.h> /* INT_MAX in ip6_tables.h */
Philip Blundellb47050c2000-06-04 17:38:47 +00006#include <linux/netfilter_ipv6/ip6_tables.h>
7
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +01008enum {
9 O_ICMPV6_TYPE = 0,
10};
11
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000012struct icmpv6_names {
Philip Blundellb47050c2000-06-04 17:38:47 +000013 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +010014 uint8_t type;
15 uint8_t code_min, code_max;
Philip Blundellb47050c2000-06-04 17:38:47 +000016};
17
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000018static const struct icmpv6_names icmpv6_codes[] = {
Philip Blundellb47050c2000-06-04 17:38:47 +000019 { "destination-unreachable", 1, 0, 0xFF },
20 { "no-route", 1, 0, 0 },
21 { "communication-prohibited", 1, 1, 1 },
22 { "address-unreachable", 1, 3, 3 },
23 { "port-unreachable", 1, 4, 4 },
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
57static void
Patrick McHardy500f4832007-09-08 15:59:04 +000058print_icmpv6types(void)
Philip Blundellb47050c2000-06-04 17:38:47 +000059{
60 unsigned int i;
61 printf("Valid ICMPv6 Types:");
62
Jan Engelhardt2c69b552009-04-30 19:32:02 +020063 for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000064 if (i && icmpv6_codes[i].type == icmpv6_codes[i-1].type) {
65 if (icmpv6_codes[i].code_min == icmpv6_codes[i-1].code_min
66 && (icmpv6_codes[i].code_max
67 == icmpv6_codes[i-1].code_max))
68 printf(" (%s)", icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +000069 else
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000070 printf("\n %s", icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +000071 }
72 else
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000073 printf("\n%s", icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +000074 }
75 printf("\n");
76}
77
Jan Engelhardt997045f2007-10-04 16:29:21 +000078static void icmp6_help(void)
Philip Blundellb47050c2000-06-04 17:38:47 +000079{
80 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020081"icmpv6 match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020082"[!] --icmpv6-type typename match icmpv6 type\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020083" (or numeric type or type/code)\n");
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000084 print_icmpv6types();
Philip Blundellb47050c2000-06-04 17:38:47 +000085}
86
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +010087static const struct xt_option_entry icmp6_opts[] = {
88 {.name = "icmpv6-type", .id = O_ICMPV6_TYPE, .type = XTTYPE_STRING,
89 .flags = XTOPT_MAND | XTOPT_INVERT},
90 XTOPT_TABLEEND,
Philip Blundellb47050c2000-06-04 17:38:47 +000091};
92
Pablo Neira8115e542005-02-14 13:13:04 +000093static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +010094parse_icmpv6(const char *icmpv6type, uint8_t *type, uint8_t code[])
Philip Blundellb47050c2000-06-04 17:38:47 +000095{
Jan Engelhardt2c69b552009-04-30 19:32:02 +020096 static const unsigned int limit = ARRAY_SIZE(icmpv6_codes);
Philip Blundellb47050c2000-06-04 17:38:47 +000097 unsigned int match = limit;
98 unsigned int i;
99
100 for (i = 0; i < limit; i++) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000101 if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type))
Philip Blundellb47050c2000-06-04 17:38:47 +0000102 == 0) {
103 if (match != limit)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100104 xtables_error(PARAMETER_PROBLEM,
Philip Blundellb47050c2000-06-04 17:38:47 +0000105 "Ambiguous ICMPv6 type `%s':"
106 " `%s' or `%s'?",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000107 icmpv6type,
108 icmpv6_codes[match].name,
109 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000110 match = i;
111 }
112 }
113
114 if (match != limit) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000115 *type = icmpv6_codes[match].type;
116 code[0] = icmpv6_codes[match].code_min;
117 code[1] = icmpv6_codes[match].code_max;
Philip Blundellb47050c2000-06-04 17:38:47 +0000118 } else {
119 char *slash;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000120 char buffer[strlen(icmpv6type) + 1];
Harald Welteb4719762001-07-23 02:14:22 +0000121 unsigned int number;
Philip Blundellb47050c2000-06-04 17:38:47 +0000122
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000123 strcpy(buffer, icmpv6type);
Philip Blundellb47050c2000-06-04 17:38:47 +0000124 slash = strchr(buffer, '/');
125
126 if (slash)
127 *slash = '\0';
128
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100129 if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100130 xtables_error(PARAMETER_PROBLEM,
Philip Blundellb47050c2000-06-04 17:38:47 +0000131 "Invalid ICMPv6 type `%s'\n", buffer);
132 *type = number;
133 if (slash) {
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100134 if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100135 xtables_error(PARAMETER_PROBLEM,
Philip Blundellb47050c2000-06-04 17:38:47 +0000136 "Invalid ICMPv6 code `%s'\n",
137 slash+1);
138 code[0] = code[1] = number;
139 } else {
140 code[0] = 0;
141 code[1] = 0xFF;
142 }
143 }
Philip Blundellb47050c2000-06-04 17:38:47 +0000144}
145
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
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +0100153static void icmp6_parse(struct xt_option_call *cb)
Philip Blundellb47050c2000-06-04 17:38:47 +0000154{
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +0100155 struct ip6t_icmp *icmpv6info = cb->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000156
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +0100157 xtables_option_parse(cb);
158 parse_icmpv6(cb->arg, &icmpv6info->type, icmpv6info->code);
159 if (cb->invert)
160 icmpv6info->invflags |= IP6T_ICMP_INV;
Philip Blundellb47050c2000-06-04 17:38:47 +0000161}
162
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100163static void print_icmpv6type(uint8_t type,
164 uint8_t code_min, uint8_t code_max,
Philip Blundellb47050c2000-06-04 17:38:47 +0000165 int invert,
166 int numeric)
167{
168 if (!numeric) {
169 unsigned int i;
170
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200171 for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i)
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000172 if (icmpv6_codes[i].type == type
173 && icmpv6_codes[i].code_min == code_min
174 && icmpv6_codes[i].code_max == code_max)
Philip Blundellb47050c2000-06-04 17:38:47 +0000175 break;
Philip Blundellb47050c2000-06-04 17:38:47 +0000176
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200177 if (i != ARRAY_SIZE(icmpv6_codes)) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100178 printf(" %s%s",
Philip Blundellb47050c2000-06-04 17:38:47 +0000179 invert ? "!" : "",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000180 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000181 return;
182 }
183 }
184
185 if (invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100186 printf(" !");
Philip Blundellb47050c2000-06-04 17:38:47 +0000187
188 printf("type %u", type);
Jan Engelhardt73866352010-12-18 02:04:59 +0100189 if (code_min == code_max)
190 printf(" code %u", code_min);
191 else if (code_min != 0 || code_max != 0xFF)
192 printf(" codes %u-%u", code_min, code_max);
Philip Blundellb47050c2000-06-04 17:38:47 +0000193}
194
Jan Engelhardt997045f2007-10-04 16:29:21 +0000195static void icmp6_print(const void *ip, const struct xt_entry_match *match,
196 int numeric)
Philip Blundellb47050c2000-06-04 17:38:47 +0000197{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000198 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000199
Jan Engelhardt73866352010-12-18 02:04:59 +0100200 printf(" ipv6-icmp");
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000201 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
202 icmpv6->invflags & IP6T_ICMP_INV,
Philip Blundellb47050c2000-06-04 17:38:47 +0000203 numeric);
204
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000205 if (icmpv6->invflags & ~IP6T_ICMP_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100206 printf(" Unknown invflags: 0x%X",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000207 icmpv6->invflags & ~IP6T_ICMP_INV);
Philip Blundellb47050c2000-06-04 17:38:47 +0000208}
209
Jan Engelhardt997045f2007-10-04 16:29:21 +0000210static void icmp6_save(const void *ip, const struct xt_entry_match *match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000211{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000212 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000213
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000214 if (icmpv6->invflags & IP6T_ICMP_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100215 printf(" !");
Philip Blundellb47050c2000-06-04 17:38:47 +0000216
Jan Engelhardt73866352010-12-18 02:04:59 +0100217 printf(" --icmpv6-type %u", icmpv6->type);
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000218 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
219 printf("/%u", icmpv6->code[0]);
Philip Blundellb47050c2000-06-04 17:38:47 +0000220}
221
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200222static struct xtables_match icmp6_mt6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000223 .name = "icmp6",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200224 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100225 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200226 .size = XT_ALIGN(sizeof(struct ip6t_icmp)),
227 .userspacesize = XT_ALIGN(sizeof(struct ip6t_icmp)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000228 .help = icmp6_help,
229 .init = icmp6_init,
Jan Engelhardt997045f2007-10-04 16:29:21 +0000230 .print = icmp6_print,
231 .save = icmp6_save,
Jan Engelhardt1b8db4f2011-03-01 18:36:15 +0100232 .x6_parse = icmp6_parse,
233 .x6_options = icmp6_opts,
Philip Blundellb47050c2000-06-04 17:38:47 +0000234};
235
236void _init(void)
237{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200238 xtables_register_match(&icmp6_mt6_reg);
Philip Blundellb47050c2000-06-04 17:38:47 +0000239}