blob: fa87b696ab060e2a37e5feeabbab0e9b1ebf441e [file] [log] [blame]
Jan Engelhardtddac6c52008-09-01 14:22:19 +02001/* Shared library add-on to ip6tables to add ICMP support. */
Jan Engelhardt32b8e612010-07-23 21:16:14 +02002#include <stdbool.h>
Philip Blundellb47050c2000-06-04 17:38:47 +00003#include <stdio.h>
4#include <netdb.h>
5#include <string.h>
6#include <stdlib.h>
7#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01008#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +01009#include <limits.h> /* INT_MAX in ip6_tables.h */
Philip Blundellb47050c2000-06-04 17:38:47 +000010#include <linux/netfilter_ipv6/ip6_tables.h>
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 Engelhardt997045f2007-10-04 16:29:21 +000087static const struct option icmp6_opts[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +020088 {.name = "icmpv6-type", .has_arg = true, .val = '1'},
89 XT_GETOPT_TABLEEND,
Philip Blundellb47050c2000-06-04 17:38:47 +000090};
91
Pablo Neira8115e542005-02-14 13:13:04 +000092static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +010093parse_icmpv6(const char *icmpv6type, uint8_t *type, uint8_t code[])
Philip Blundellb47050c2000-06-04 17:38:47 +000094{
Jan Engelhardt2c69b552009-04-30 19:32:02 +020095 static const unsigned int limit = ARRAY_SIZE(icmpv6_codes);
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)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100103 xtables_error(PARAMETER_PROBLEM,
Philip Blundellb47050c2000-06-04 17:38:47 +0000104 "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
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100128 if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100129 xtables_error(PARAMETER_PROBLEM,
Philip Blundellb47050c2000-06-04 17:38:47 +0000130 "Invalid ICMPv6 type `%s'\n", buffer);
131 *type = number;
132 if (slash) {
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100133 if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100134 xtables_error(PARAMETER_PROBLEM,
Philip Blundellb47050c2000-06-04 17:38:47 +0000135 "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
Jan Engelhardt997045f2007-10-04 16:29:21 +0000145static void icmp6_init(struct xt_entry_match *m)
Philip Blundellb47050c2000-06-04 17:38:47 +0000146{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000147 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000148
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000149 icmpv6info->code[1] = 0xFF;
Philip Blundellb47050c2000-06-04 17:38:47 +0000150}
151
Jan Engelhardt997045f2007-10-04 16:29:21 +0000152static int icmp6_parse(int c, char **argv, int invert, unsigned int *flags,
153 const void *entry, struct xt_entry_match **match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000154{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000155 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000156
157 switch (c) {
158 case '1':
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000159 if (*flags == 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100160 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000161 "icmpv6 match: only use --icmpv6-type once!");
Jan Engelhardtbf971282009-11-03 19:55:11 +0100162 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
Jan Engelhardtbbe83862009-10-24 00:45:33 +0200163 parse_icmpv6(optarg, &icmpv6info->type,
Pablo Neira8115e542005-02-14 13:13:04 +0000164 icmpv6info->code);
Philip Blundellb47050c2000-06-04 17:38:47 +0000165 if (invert)
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000166 icmpv6info->invflags |= IP6T_ICMP_INV;
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000167 *flags = 1;
Philip Blundellb47050c2000-06-04 17:38:47 +0000168 break;
Philip Blundellb47050c2000-06-04 17:38:47 +0000169 }
170
171 return 1;
172}
173
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100174static void print_icmpv6type(uint8_t type,
175 uint8_t code_min, uint8_t code_max,
Philip Blundellb47050c2000-06-04 17:38:47 +0000176 int invert,
177 int numeric)
178{
179 if (!numeric) {
180 unsigned int i;
181
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200182 for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i)
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000183 if (icmpv6_codes[i].type == type
184 && icmpv6_codes[i].code_min == code_min
185 && icmpv6_codes[i].code_max == code_max)
Philip Blundellb47050c2000-06-04 17:38:47 +0000186 break;
Philip Blundellb47050c2000-06-04 17:38:47 +0000187
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200188 if (i != ARRAY_SIZE(icmpv6_codes)) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100189 printf(" %s%s",
Philip Blundellb47050c2000-06-04 17:38:47 +0000190 invert ? "!" : "",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000191 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000192 return;
193 }
194 }
195
196 if (invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100197 printf(" !");
Philip Blundellb47050c2000-06-04 17:38:47 +0000198
199 printf("type %u", type);
Jan Engelhardt73866352010-12-18 02:04:59 +0100200 if (code_min == code_max)
201 printf(" code %u", code_min);
202 else if (code_min != 0 || code_max != 0xFF)
203 printf(" codes %u-%u", code_min, code_max);
Philip Blundellb47050c2000-06-04 17:38:47 +0000204}
205
Jan Engelhardt997045f2007-10-04 16:29:21 +0000206static void icmp6_print(const void *ip, const struct xt_entry_match *match,
207 int numeric)
Philip Blundellb47050c2000-06-04 17:38:47 +0000208{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000209 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000210
Jan Engelhardt73866352010-12-18 02:04:59 +0100211 printf(" ipv6-icmp");
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000212 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
213 icmpv6->invflags & IP6T_ICMP_INV,
Philip Blundellb47050c2000-06-04 17:38:47 +0000214 numeric);
215
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000216 if (icmpv6->invflags & ~IP6T_ICMP_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100217 printf(" Unknown invflags: 0x%X",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000218 icmpv6->invflags & ~IP6T_ICMP_INV);
Philip Blundellb47050c2000-06-04 17:38:47 +0000219}
220
Jan Engelhardt997045f2007-10-04 16:29:21 +0000221static void icmp6_save(const void *ip, const struct xt_entry_match *match)
Philip Blundellb47050c2000-06-04 17:38:47 +0000222{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000223 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000224
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000225 if (icmpv6->invflags & IP6T_ICMP_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100226 printf(" !");
Philip Blundellb47050c2000-06-04 17:38:47 +0000227
Jan Engelhardt73866352010-12-18 02:04:59 +0100228 printf(" --icmpv6-type %u", icmpv6->type);
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000229 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
230 printf("/%u", icmpv6->code[0]);
Philip Blundellb47050c2000-06-04 17:38:47 +0000231}
232
Jan Engelhardt997045f2007-10-04 16:29:21 +0000233static void icmp6_check(unsigned int flags)
Philip Blundellb47050c2000-06-04 17:38:47 +0000234{
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000235 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100236 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000237 "icmpv6 match: You must specify `--icmpv6-type'");
Philip Blundellb47050c2000-06-04 17:38:47 +0000238}
239
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200240static struct xtables_match icmp6_mt6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000241 .name = "icmp6",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200242 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100243 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200244 .size = XT_ALIGN(sizeof(struct ip6t_icmp)),
245 .userspacesize = XT_ALIGN(sizeof(struct ip6t_icmp)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000246 .help = icmp6_help,
247 .init = icmp6_init,
248 .parse = icmp6_parse,
249 .final_check = icmp6_check,
250 .print = icmp6_print,
251 .save = icmp6_save,
252 .extra_opts = icmp6_opts,
Philip Blundellb47050c2000-06-04 17:38:47 +0000253};
254
255void _init(void)
256{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200257 xtables_register_match(&icmp6_mt6_reg);
Philip Blundellb47050c2000-06-04 17:38:47 +0000258}