blob: 1b801d2d2d3bd008b19e20db483e8b7bf1a458d6 [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
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000056print_icmpv6types()
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. */
77static void
78help(void)
79{
80 printf(
81"ICMPv6 v%s options:\n"
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000082" --icmpv6-type [!] typename match icmpv6 type\n"
Philip Blundellb47050c2000-06-04 17:38:47 +000083" (or numeric type or type/code)\n"
84"\n", NETFILTER_VERSION);
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000085 print_icmpv6types();
Philip Blundellb47050c2000-06-04 17:38:47 +000086}
87
88static struct option opts[] = {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000089 { "icmpv6-type", 1, 0, '1' },
Philip Blundellb47050c2000-06-04 17:38:47 +000090 {0}
91};
92
93static unsigned int
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000094parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[])
Philip Blundellb47050c2000-06-04 17:38:47 +000095{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +000096 unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
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)
104 exit_error(PARAMETER_PROBLEM,
105 "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
Harald Welteb4719762001-07-23 02:14:22 +0000129 if (string_to_number(buffer, 0, 255, &number) == -1)
Philip Blundellb47050c2000-06-04 17:38:47 +0000130 exit_error(PARAMETER_PROBLEM,
131 "Invalid ICMPv6 type `%s'\n", buffer);
132 *type = number;
133 if (slash) {
Harald Welteb4719762001-07-23 02:14:22 +0000134 if (string_to_number(slash+1, 0, 255, &number) == -1)
Philip Blundellb47050c2000-06-04 17:38:47 +0000135 exit_error(PARAMETER_PROBLEM,
136 "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 }
144
145 if (code[0] == 0 && code[1] == 0xFF)
146 return NFC_IP6_SRC_PT;
147 else return NFC_IP6_SRC_PT | NFC_IP6_DST_PT;
148}
149
150/* Initialize the match. */
151static void
152init(struct ip6t_entry_match *m, unsigned int *nfcache)
153{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000154 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000155
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000156 icmpv6info->code[1] = 0xFF;
Philip Blundellb47050c2000-06-04 17:38:47 +0000157}
158
159/* Function which parses command options; returns true if it
160 ate an option */
161static int
162parse(int c, char **argv, int invert, unsigned int *flags,
163 const struct ip6t_entry *entry,
164 unsigned int *nfcache,
165 struct ip6t_entry_match **match)
166{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000167 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000168
169 switch (c) {
170 case '1':
171 if (check_inverse(optarg, &invert))
172 optind++;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000173 *nfcache |= parse_icmpv6(argv[optind-1],
174 &icmpv6info->type,
175 icmpv6info->code);
Philip Blundellb47050c2000-06-04 17:38:47 +0000176 if (invert)
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000177 icmpv6info->invflags |= IP6T_ICMP_INV;
Philip Blundellb47050c2000-06-04 17:38:47 +0000178 break;
179
180 default:
181 return 0;
182 }
183
184 return 1;
185}
186
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000187static void print_icmpv6type(u_int8_t type,
Philip Blundellb47050c2000-06-04 17:38:47 +0000188 u_int8_t code_min, u_int8_t code_max,
189 int invert,
190 int numeric)
191{
192 if (!numeric) {
193 unsigned int i;
194
195 for (i = 0;
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000196 i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
Philip Blundellb47050c2000-06-04 17:38:47 +0000197 i++) {
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000198 if (icmpv6_codes[i].type == type
199 && icmpv6_codes[i].code_min == code_min
200 && icmpv6_codes[i].code_max == code_max)
Philip Blundellb47050c2000-06-04 17:38:47 +0000201 break;
202 }
203
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000204 if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) {
Philip Blundellb47050c2000-06-04 17:38:47 +0000205 printf("%s%s ",
206 invert ? "!" : "",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000207 icmpv6_codes[i].name);
Philip Blundellb47050c2000-06-04 17:38:47 +0000208 return;
209 }
210 }
211
212 if (invert)
213 printf("!");
214
215 printf("type %u", type);
216 if (code_min == 0 && code_max == 0xFF)
217 printf(" ");
218 else if (code_min == code_max)
219 printf(" code %u ", code_min);
220 else
221 printf(" codes %u-%u ", code_min, code_max);
222}
223
224/* Prints out the union ipt_matchinfo. */
225static void
226print(const struct ip6t_ip6 *ip,
227 const struct ip6t_entry_match *match,
228 int numeric)
229{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000230 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000231
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000232 printf("icmpv6 ");
233 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
234 icmpv6->invflags & IP6T_ICMP_INV,
Philip Blundellb47050c2000-06-04 17:38:47 +0000235 numeric);
236
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000237 if (icmpv6->invflags & ~IP6T_ICMP_INV)
Philip Blundellb47050c2000-06-04 17:38:47 +0000238 printf("Unknown invflags: 0x%X ",
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000239 icmpv6->invflags & ~IP6T_ICMP_INV);
Philip Blundellb47050c2000-06-04 17:38:47 +0000240}
241
242/* Saves the match in parsable form to stdout. */
243static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
244{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000245 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
Philip Blundellb47050c2000-06-04 17:38:47 +0000246
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000247 if (icmpv6->invflags & IP6T_ICMP_INV)
Philip Blundellb47050c2000-06-04 17:38:47 +0000248 printf("! ");
249
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000250 printf("--icmpv6-type %u", icmpv6->type);
251 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
252 printf("/%u", icmpv6->code[0]);
Philip Blundellb47050c2000-06-04 17:38:47 +0000253 printf(" ");
254}
255
256/* Final check; we don't care. */
257static void final_check(unsigned int flags)
258{
259}
260
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000261struct ip6tables_match icmpv6
Philip Blundellb47050c2000-06-04 17:38:47 +0000262= { NULL,
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000263 "icmpv6",
Philip Blundellb47050c2000-06-04 17:38:47 +0000264 NETFILTER_VERSION,
Rusty Russell73f72f52000-07-03 10:17:57 +0000265 IP6T_ALIGN(sizeof(struct ip6t_icmp)),
266 IP6T_ALIGN(sizeof(struct ip6t_icmp)),
Philip Blundellb47050c2000-06-04 17:38:47 +0000267 &help,
268 &init,
269 &parse,
270 &final_check,
271 &print,
272 &save,
273 opts
274};
275
276void _init(void)
277{
András Kis-Szabóe0bc7a42001-07-14 20:21:46 +0000278 register_match6(&icmpv6);
Philip Blundellb47050c2000-06-04 17:38:47 +0000279}