blob: fa76601414532ed59cd97065816299a7b663d2f5 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +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 <iptables.h>
8#include <linux/netfilter_ipv4/ip_tables.h>
9
Harald Weltefc9237d2003-02-25 10:50:59 +000010/* special hack for icmp-type 'any':
11 * Up to kernel <=2.4.20 the problem was:
12 * '-p icmp ' matches all icmp packets
13 * '-p icmp -m icmp' matches _only_ ICMP type 0 :(
14 * This is now fixed by initializing the field * to icmp type 0xFF
15 * See: https://bugzilla.netfilter.org/cgi-bin/bugzilla/show_bug.cgi?id=37
16 */
17
Marc Bouchere6869a82000-03-20 06:03:29 +000018struct icmp_names {
19 const char *name;
20 u_int8_t type;
21 u_int8_t code_min, code_max;
22};
23
24static const struct icmp_names icmp_codes[] = {
Harald Weltefc9237d2003-02-25 10:50:59 +000025 { "any", 0xFF, 0, 0xFF },
Marc Bouchere6869a82000-03-20 06:03:29 +000026 { "echo-reply", 0, 0, 0xFF },
27 /* Alias */ { "pong", 0, 0, 0xFF },
28
29 { "destination-unreachable", 3, 0, 0xFF },
30 { "network-unreachable", 3, 0, 0 },
31 { "host-unreachable", 3, 1, 1 },
32 { "protocol-unreachable", 3, 2, 2 },
33 { "port-unreachable", 3, 3, 3 },
34 { "fragmentation-needed", 3, 4, 4 },
35 { "source-route-failed", 3, 5, 5 },
36 { "network-unknown", 3, 6, 6 },
37 { "host-unknown", 3, 7, 7 },
38 { "network-prohibited", 3, 9, 9 },
39 { "host-prohibited", 3, 10, 10 },
40 { "TOS-network-unreachable", 3, 11, 11 },
41 { "TOS-host-unreachable", 3, 12, 12 },
42 { "communication-prohibited", 3, 13, 13 },
43 { "host-precedence-violation", 3, 14, 14 },
44 { "precedence-cutoff", 3, 15, 15 },
45
46 { "source-quench", 4, 0, 0xFF },
47
48 { "redirect", 5, 0, 0xFF },
49 { "network-redirect", 5, 0, 0 },
50 { "host-redirect", 5, 1, 1 },
51 { "TOS-network-redirect", 5, 2, 2 },
52 { "TOS-host-redirect", 5, 3, 3 },
53
54 { "echo-request", 8, 0, 0xFF },
55 /* Alias */ { "ping", 8, 0, 0xFF },
56
57 { "router-advertisement", 9, 0, 0xFF },
58
59 { "router-solicitation", 10, 0, 0xFF },
60
61 { "time-exceeded", 11, 0, 0xFF },
62 /* Alias */ { "ttl-exceeded", 11, 0, 0xFF },
63 { "ttl-zero-during-transit", 11, 0, 0 },
64 { "ttl-zero-during-reassembly", 11, 1, 1 },
65
66 { "parameter-problem", 12, 0, 0xFF },
67 { "ip-header-bad", 12, 0, 0 },
68 { "required-option-missing", 12, 1, 1 },
69
70 { "timestamp-request", 13, 0, 0xFF },
71
72 { "timestamp-reply", 14, 0, 0xFF },
73
74 { "address-mask-request", 17, 0, 0xFF },
75
76 { "address-mask-reply", 18, 0, 0xFF }
77};
78
79static void
Patrick McHardy500f4832007-09-08 15:59:04 +000080print_icmptypes(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000081{
82 unsigned int i;
83 printf("Valid ICMP Types:");
84
85 for (i = 0; i < sizeof(icmp_codes)/sizeof(struct icmp_names); i++) {
86 if (i && icmp_codes[i].type == icmp_codes[i-1].type) {
87 if (icmp_codes[i].code_min == icmp_codes[i-1].code_min
88 && (icmp_codes[i].code_max
89 == icmp_codes[i-1].code_max))
90 printf(" (%s)", icmp_codes[i].name);
91 else
92 printf("\n %s", icmp_codes[i].name);
93 }
94 else
95 printf("\n%s", icmp_codes[i].name);
96 }
97 printf("\n");
98}
99
100/* Function which prints out usage message. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000101static void icmp_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +0000102{
103 printf(
104"ICMP v%s options:\n"
105" --icmp-type [!] typename match icmp type\n"
106" (or numeric type or type/code)\n"
Harald Welte80fe35d2002-05-29 13:08:15 +0000107"\n", IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +0000108 print_icmptypes();
109}
110
Jan Engelhardt59d16402007-10-04 16:28:39 +0000111static const struct option icmp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000112 { "icmp-type", 1, NULL, '1' },
113 { }
Marc Bouchere6869a82000-03-20 06:03:29 +0000114};
115
Pablo Neira8115e542005-02-14 13:13:04 +0000116static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000117parse_icmp(const char *icmptype, u_int8_t *type, u_int8_t code[])
118{
119 unsigned int limit = sizeof(icmp_codes)/sizeof(struct icmp_names);
120 unsigned int match = limit;
121 unsigned int i;
122
123 for (i = 0; i < limit; i++) {
124 if (strncasecmp(icmp_codes[i].name, icmptype, strlen(icmptype))
125 == 0) {
126 if (match != limit)
127 exit_error(PARAMETER_PROBLEM,
128 "Ambiguous ICMP type `%s':"
129 " `%s' or `%s'?",
130 icmptype,
131 icmp_codes[match].name,
132 icmp_codes[i].name);
133 match = i;
134 }
135 }
136
137 if (match != limit) {
138 *type = icmp_codes[match].type;
139 code[0] = icmp_codes[match].code_min;
140 code[1] = icmp_codes[match].code_max;
141 } else {
142 char *slash;
143 char buffer[strlen(icmptype) + 1];
Harald Welteb4719762001-07-23 02:14:22 +0000144 unsigned int number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000145
146 strcpy(buffer, icmptype);
147 slash = strchr(buffer, '/');
148
149 if (slash)
150 *slash = '\0';
151
Harald Welteb4719762001-07-23 02:14:22 +0000152 if (string_to_number(buffer, 0, 255, &number) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000153 exit_error(PARAMETER_PROBLEM,
154 "Invalid ICMP type `%s'\n", buffer);
155 *type = number;
156 if (slash) {
Harald Welteb4719762001-07-23 02:14:22 +0000157 if (string_to_number(slash+1, 0, 255, &number) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000158 exit_error(PARAMETER_PROBLEM,
159 "Invalid ICMP code `%s'\n",
160 slash+1);
161 code[0] = code[1] = number;
162 } else {
163 code[0] = 0;
164 code[1] = 0xFF;
165 }
166 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000167}
168
169/* Initialize the match. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000170static void icmp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000171{
172 struct ipt_icmp *icmpinfo = (struct ipt_icmp *)m->data;
173
Harald Weltefc9237d2003-02-25 10:50:59 +0000174 icmpinfo->type = 0xFF;
Marc Bouchere6869a82000-03-20 06:03:29 +0000175 icmpinfo->code[1] = 0xFF;
176}
177
178/* Function which parses command options; returns true if it
179 ate an option */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000180static int icmp_parse(int c, char **argv, int invert, unsigned int *flags,
181 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000182{
183 struct ipt_icmp *icmpinfo = (struct ipt_icmp *)(*match)->data;
184
185 switch (c) {
186 case '1':
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000187 if (*flags == 1)
188 exit_error(PARAMETER_PROBLEM,
189 "icmp match: only use --icmp-type once!");
Harald Welteb77f1da2002-03-14 11:35:58 +0000190 check_inverse(optarg, &invert, &optind, 0);
Pablo Neira8115e542005-02-14 13:13:04 +0000191 parse_icmp(argv[optind-1], &icmpinfo->type,
192 icmpinfo->code);
Marc Bouchere6869a82000-03-20 06:03:29 +0000193 if (invert)
194 icmpinfo->invflags |= IPT_ICMP_INV;
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000195 *flags = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196 break;
197
198 default:
199 return 0;
200 }
201
202 return 1;
203}
204
205static void print_icmptype(u_int8_t type,
206 u_int8_t code_min, u_int8_t code_max,
207 int invert,
208 int numeric)
209{
210 if (!numeric) {
211 unsigned int i;
212
213 for (i = 0;
214 i < sizeof(icmp_codes)/sizeof(struct icmp_names);
215 i++) {
216 if (icmp_codes[i].type == type
217 && icmp_codes[i].code_min == code_min
218 && icmp_codes[i].code_max == code_max)
219 break;
220 }
221
222 if (i != sizeof(icmp_codes)/sizeof(struct icmp_names)) {
223 printf("%s%s ",
224 invert ? "!" : "",
225 icmp_codes[i].name);
226 return;
227 }
228 }
229
230 if (invert)
231 printf("!");
232
233 printf("type %u", type);
234 if (code_min == 0 && code_max == 0xFF)
235 printf(" ");
236 else if (code_min == code_max)
237 printf(" code %u ", code_min);
238 else
239 printf(" codes %u-%u ", code_min, code_max);
240}
241
242/* Prints out the union ipt_matchinfo. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000243static void icmp_print(const void *ip, const struct xt_entry_match *match,
244 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000245{
246 const struct ipt_icmp *icmp = (struct ipt_icmp *)match->data;
247
248 printf("icmp ");
249 print_icmptype(icmp->type, icmp->code[0], icmp->code[1],
250 icmp->invflags & IPT_ICMP_INV,
251 numeric);
252
253 if (icmp->invflags & ~IPT_ICMP_INV)
254 printf("Unknown invflags: 0x%X ",
255 icmp->invflags & ~IPT_ICMP_INV);
256}
257
258/* Saves the match in parsable form to stdout. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000259static void icmp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000260{
261 const struct ipt_icmp *icmp = (struct ipt_icmp *)match->data;
262
263 if (icmp->invflags & IPT_ICMP_INV)
264 printf("! ");
265
Harald Weltefc9237d2003-02-25 10:50:59 +0000266 /* special hack for 'any' case */
267 if (icmp->type == 0xFF) {
Harald Welted2979572004-02-04 08:42:37 +0000268 printf("--icmp-type any ");
Harald Weltefc9237d2003-02-25 10:50:59 +0000269 } else {
270 printf("--icmp-type %u", icmp->type);
271 if (icmp->code[0] != 0 || icmp->code[1] != 0xFF)
272 printf("/%u", icmp->code[0]);
273 printf(" ");
274 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000275}
276
Jan Engelhardt59d16402007-10-04 16:28:39 +0000277static struct iptables_match icmp_match = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000278 .name = "icmp",
279 .version = IPTABLES_VERSION,
280 .size = IPT_ALIGN(sizeof(struct ipt_icmp)),
281 .userspacesize = IPT_ALIGN(sizeof(struct ipt_icmp)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000282 .help = icmp_help,
283 .init = icmp_init,
284 .parse = icmp_parse,
285 .print = icmp_print,
286 .save = icmp_save,
287 .extra_opts = icmp_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000288};
289
290void _init(void)
291{
Jan Engelhardt59d16402007-10-04 16:28:39 +0000292 register_match(&icmp_match);
Marc Bouchere6869a82000-03-20 06:03:29 +0000293}