blob: d864112982867c78529848c5307048ccf4391ca2 [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
10struct icmp_names {
11 const char *name;
12 u_int8_t type;
13 u_int8_t code_min, code_max;
14};
15
16static const struct icmp_names icmp_codes[] = {
17 { "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
56print_icmptypes()
57{
58 unsigned int i;
59 printf("Valid ICMPv6 Types:");
60
61 for (i = 0; i < sizeof(icmp_codes)/sizeof(struct icmp_names); i++) {
62 if (i && icmp_codes[i].type == icmp_codes[i-1].type) {
63 if (icmp_codes[i].code_min == icmp_codes[i-1].code_min
64 && (icmp_codes[i].code_max
65 == icmp_codes[i-1].code_max))
66 printf(" (%s)", icmp_codes[i].name);
67 else
68 printf("\n %s", icmp_codes[i].name);
69 }
70 else
71 printf("\n%s", icmp_codes[i].name);
72 }
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"
82" --icmp-type [!] typename match icmp type\n"
83" (or numeric type or type/code)\n"
84"\n", NETFILTER_VERSION);
85 print_icmptypes();
86}
87
88static struct option opts[] = {
89 { "icmp-type", 1, 0, '1' },
90 {0}
91};
92
93static unsigned int
94parse_icmp(const char *icmptype, u_int8_t *type, u_int8_t code[])
95{
96 unsigned int limit = sizeof(icmp_codes)/sizeof(struct icmp_names);
97 unsigned int match = limit;
98 unsigned int i;
99
100 for (i = 0; i < limit; i++) {
101 if (strncasecmp(icmp_codes[i].name, icmptype, strlen(icmptype))
102 == 0) {
103 if (match != limit)
104 exit_error(PARAMETER_PROBLEM,
105 "Ambiguous ICMPv6 type `%s':"
106 " `%s' or `%s'?",
107 icmptype,
108 icmp_codes[match].name,
109 icmp_codes[i].name);
110 match = i;
111 }
112 }
113
114 if (match != limit) {
115 *type = icmp_codes[match].type;
116 code[0] = icmp_codes[match].code_min;
117 code[1] = icmp_codes[match].code_max;
118 } else {
119 char *slash;
120 char buffer[strlen(icmptype) + 1];
121 int number;
122
123 strcpy(buffer, icmptype);
124 slash = strchr(buffer, '/');
125
126 if (slash)
127 *slash = '\0';
128
129 number = string_to_number(buffer, 0, 255);
130 if (number == -1)
131 exit_error(PARAMETER_PROBLEM,
132 "Invalid ICMPv6 type `%s'\n", buffer);
133 *type = number;
134 if (slash) {
135 number = string_to_number(slash+1, 0, 255);
136 if (number == -1)
137 exit_error(PARAMETER_PROBLEM,
138 "Invalid ICMPv6 code `%s'\n",
139 slash+1);
140 code[0] = code[1] = number;
141 } else {
142 code[0] = 0;
143 code[1] = 0xFF;
144 }
145 }
146
147 if (code[0] == 0 && code[1] == 0xFF)
148 return NFC_IP6_SRC_PT;
149 else return NFC_IP6_SRC_PT | NFC_IP6_DST_PT;
150}
151
152/* Initialize the match. */
153static void
154init(struct ip6t_entry_match *m, unsigned int *nfcache)
155{
156 struct ip6t_icmp *icmpinfo = (struct ip6t_icmp *)m->data;
157
158 icmpinfo->code[1] = 0xFF;
159}
160
161/* Function which parses command options; returns true if it
162 ate an option */
163static int
164parse(int c, char **argv, int invert, unsigned int *flags,
165 const struct ip6t_entry *entry,
166 unsigned int *nfcache,
167 struct ip6t_entry_match **match)
168{
169 struct ip6t_icmp *icmpinfo = (struct ip6t_icmp *)(*match)->data;
170
171 switch (c) {
172 case '1':
173 if (check_inverse(optarg, &invert))
174 optind++;
175 *nfcache |= parse_icmp(argv[optind-1],
176 &icmpinfo->type,
177 icmpinfo->code);
178 if (invert)
179 icmpinfo->invflags |= IP6T_ICMP_INV;
180 break;
181
182 default:
183 return 0;
184 }
185
186 return 1;
187}
188
189static void print_icmptype(u_int8_t type,
190 u_int8_t code_min, u_int8_t code_max,
191 int invert,
192 int numeric)
193{
194 if (!numeric) {
195 unsigned int i;
196
197 for (i = 0;
198 i < sizeof(icmp_codes)/sizeof(struct icmp_names);
199 i++) {
200 if (icmp_codes[i].type == type
201 && icmp_codes[i].code_min == code_min
202 && icmp_codes[i].code_max == code_max)
203 break;
204 }
205
206 if (i != sizeof(icmp_codes)/sizeof(struct icmp_names)) {
207 printf("%s%s ",
208 invert ? "!" : "",
209 icmp_codes[i].name);
210 return;
211 }
212 }
213
214 if (invert)
215 printf("!");
216
217 printf("type %u", type);
218 if (code_min == 0 && code_max == 0xFF)
219 printf(" ");
220 else if (code_min == code_max)
221 printf(" code %u ", code_min);
222 else
223 printf(" codes %u-%u ", code_min, code_max);
224}
225
226/* Prints out the union ipt_matchinfo. */
227static void
228print(const struct ip6t_ip6 *ip,
229 const struct ip6t_entry_match *match,
230 int numeric)
231{
232 const struct ip6t_icmp *icmp = (struct ip6t_icmp *)match->data;
233
234 printf("icmp ");
235 print_icmptype(icmp->type, icmp->code[0], icmp->code[1],
236 icmp->invflags & IP6T_ICMP_INV,
237 numeric);
238
239 if (icmp->invflags & ~IP6T_ICMP_INV)
240 printf("Unknown invflags: 0x%X ",
241 icmp->invflags & ~IP6T_ICMP_INV);
242}
243
244/* Saves the match in parsable form to stdout. */
245static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
246{
247 const struct ip6t_icmp *icmp = (struct ip6t_icmp *)match->data;
248
249 if (icmp->invflags & IP6T_ICMP_INV)
250 printf("! ");
251
252 printf("--icmp-type %u", icmp->type);
253 if (icmp->code[0] != 0 || icmp->code[1] != 0xFF)
254 printf("/%u", icmp->code[0]);
255 printf(" ");
256}
257
258/* Final check; we don't care. */
259static void final_check(unsigned int flags)
260{
261}
262
263struct ip6tables_match icmp
264= { NULL,
265 "icmp",
266 NETFILTER_VERSION,
267 sizeof(struct ip6t_icmp),
268 sizeof(struct ip6t_icmp),
269 &help,
270 &init,
271 &parse,
272 &final_check,
273 &print,
274 &save,
275 opts
276};
277
278void _init(void)
279{
280 register_match6(&icmp);
281}