blob: c75713d21b7a1ff3e84b5a1be7fa1f739a5bc7c7 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add ICMP support. */
Jan Engelhardt32b8e612010-07-23 21:16:14 +02002#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +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 ip_tables.h */
Marc Bouchere6869a82000-03-20 06:03:29 +000010#include <linux/netfilter_ipv4/ip_tables.h>
11
Harald Weltefc9237d2003-02-25 10:50:59 +000012/* special hack for icmp-type 'any':
13 * Up to kernel <=2.4.20 the problem was:
14 * '-p icmp ' matches all icmp packets
15 * '-p icmp -m icmp' matches _only_ ICMP type 0 :(
16 * This is now fixed by initializing the field * to icmp type 0xFF
17 * See: https://bugzilla.netfilter.org/cgi-bin/bugzilla/show_bug.cgi?id=37
18 */
19
Marc Bouchere6869a82000-03-20 06:03:29 +000020struct icmp_names {
21 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +010022 uint8_t type;
23 uint8_t code_min, code_max;
Marc Bouchere6869a82000-03-20 06:03:29 +000024};
25
26static const struct icmp_names icmp_codes[] = {
Harald Weltefc9237d2003-02-25 10:50:59 +000027 { "any", 0xFF, 0, 0xFF },
Marc Bouchere6869a82000-03-20 06:03:29 +000028 { "echo-reply", 0, 0, 0xFF },
29 /* Alias */ { "pong", 0, 0, 0xFF },
30
31 { "destination-unreachable", 3, 0, 0xFF },
32 { "network-unreachable", 3, 0, 0 },
33 { "host-unreachable", 3, 1, 1 },
34 { "protocol-unreachable", 3, 2, 2 },
35 { "port-unreachable", 3, 3, 3 },
36 { "fragmentation-needed", 3, 4, 4 },
37 { "source-route-failed", 3, 5, 5 },
38 { "network-unknown", 3, 6, 6 },
39 { "host-unknown", 3, 7, 7 },
40 { "network-prohibited", 3, 9, 9 },
41 { "host-prohibited", 3, 10, 10 },
42 { "TOS-network-unreachable", 3, 11, 11 },
43 { "TOS-host-unreachable", 3, 12, 12 },
44 { "communication-prohibited", 3, 13, 13 },
45 { "host-precedence-violation", 3, 14, 14 },
46 { "precedence-cutoff", 3, 15, 15 },
47
48 { "source-quench", 4, 0, 0xFF },
49
50 { "redirect", 5, 0, 0xFF },
51 { "network-redirect", 5, 0, 0 },
52 { "host-redirect", 5, 1, 1 },
53 { "TOS-network-redirect", 5, 2, 2 },
54 { "TOS-host-redirect", 5, 3, 3 },
55
56 { "echo-request", 8, 0, 0xFF },
57 /* Alias */ { "ping", 8, 0, 0xFF },
58
59 { "router-advertisement", 9, 0, 0xFF },
60
61 { "router-solicitation", 10, 0, 0xFF },
62
63 { "time-exceeded", 11, 0, 0xFF },
64 /* Alias */ { "ttl-exceeded", 11, 0, 0xFF },
65 { "ttl-zero-during-transit", 11, 0, 0 },
66 { "ttl-zero-during-reassembly", 11, 1, 1 },
67
68 { "parameter-problem", 12, 0, 0xFF },
69 { "ip-header-bad", 12, 0, 0 },
70 { "required-option-missing", 12, 1, 1 },
71
72 { "timestamp-request", 13, 0, 0xFF },
73
74 { "timestamp-reply", 14, 0, 0xFF },
75
76 { "address-mask-request", 17, 0, 0xFF },
77
78 { "address-mask-reply", 18, 0, 0xFF }
79};
80
81static void
Patrick McHardy500f4832007-09-08 15:59:04 +000082print_icmptypes(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000083{
84 unsigned int i;
85 printf("Valid ICMP Types:");
86
Jan Engelhardt2c69b552009-04-30 19:32:02 +020087 for (i = 0; i < ARRAY_SIZE(icmp_codes); ++i) {
Marc Bouchere6869a82000-03-20 06:03:29 +000088 if (i && icmp_codes[i].type == icmp_codes[i-1].type) {
89 if (icmp_codes[i].code_min == icmp_codes[i-1].code_min
90 && (icmp_codes[i].code_max
91 == icmp_codes[i-1].code_max))
92 printf(" (%s)", icmp_codes[i].name);
93 else
94 printf("\n %s", icmp_codes[i].name);
95 }
96 else
97 printf("\n%s", icmp_codes[i].name);
98 }
99 printf("\n");
100}
101
Jan Engelhardt59d16402007-10-04 16:28:39 +0000102static void icmp_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +0000103{
104 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200105"icmp match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200106"[!] --icmp-type typename match icmp type\n"
Jan Engelhardt3d12c3b2009-01-13 15:30:10 +0100107"[!] --icmp-type type[/code] (or numeric type or type/code)\n");
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[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +0200112 {.name = "icmp-type", .has_arg = true, .val = '1'},
113 XT_GETOPT_TABLEEND,
Marc Bouchere6869a82000-03-20 06:03:29 +0000114};
115
Pablo Neira8115e542005-02-14 13:13:04 +0000116static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100117parse_icmp(const char *icmptype, uint8_t *type, uint8_t code[])
Marc Bouchere6869a82000-03-20 06:03:29 +0000118{
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200119 static const unsigned int limit = ARRAY_SIZE(icmp_codes);
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 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)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100127 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000128 "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
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100152 if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100153 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000154 "Invalid ICMP type `%s'\n", buffer);
155 *type = number;
156 if (slash) {
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100157 if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100158 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 "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
Jan Engelhardt59d16402007-10-04 16:28:39 +0000169static void icmp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000170{
171 struct ipt_icmp *icmpinfo = (struct ipt_icmp *)m->data;
172
Harald Weltefc9237d2003-02-25 10:50:59 +0000173 icmpinfo->type = 0xFF;
Marc Bouchere6869a82000-03-20 06:03:29 +0000174 icmpinfo->code[1] = 0xFF;
175}
176
Jan Engelhardt59d16402007-10-04 16:28:39 +0000177static int icmp_parse(int c, char **argv, int invert, unsigned int *flags,
178 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000179{
180 struct ipt_icmp *icmpinfo = (struct ipt_icmp *)(*match)->data;
181
182 switch (c) {
183 case '1':
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000184 if (*flags == 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100185 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000186 "icmp match: only use --icmp-type once!");
Jan Engelhardtbf971282009-11-03 19:55:11 +0100187 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
Jan Engelhardtbbe83862009-10-24 00:45:33 +0200188 parse_icmp(optarg, &icmpinfo->type,
Pablo Neira8115e542005-02-14 13:13:04 +0000189 icmpinfo->code);
Marc Bouchere6869a82000-03-20 06:03:29 +0000190 if (invert)
191 icmpinfo->invflags |= IPT_ICMP_INV;
Yasuyuki KOZAKAIb1cda882006-07-04 10:23:26 +0000192 *flags = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000193 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000194 }
195
196 return 1;
197}
198
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100199static void print_icmptype(uint8_t type,
200 uint8_t code_min, uint8_t code_max,
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 int invert,
202 int numeric)
203{
204 if (!numeric) {
205 unsigned int i;
206
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200207 for (i = 0; i < ARRAY_SIZE(icmp_codes); ++i)
Marc Bouchere6869a82000-03-20 06:03:29 +0000208 if (icmp_codes[i].type == type
209 && icmp_codes[i].code_min == code_min
210 && icmp_codes[i].code_max == code_max)
211 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000212
Jan Engelhardt2c69b552009-04-30 19:32:02 +0200213 if (i != ARRAY_SIZE(icmp_codes)) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100214 printf(" %s%s",
Marc Bouchere6869a82000-03-20 06:03:29 +0000215 invert ? "!" : "",
216 icmp_codes[i].name);
217 return;
218 }
219 }
220
221 if (invert)
Jan Engelhardt73866352010-12-18 02:04:59 +0100222 printf(" !");
Marc Bouchere6869a82000-03-20 06:03:29 +0000223
224 printf("type %u", type);
Jan Engelhardt73866352010-12-18 02:04:59 +0100225 if (code_min == code_max)
226 printf(" code %u", code_min);
227 else if (code_min != 0 || code_max != 0xFF)
228 printf(" codes %u-%u", code_min, code_max);
Marc Bouchere6869a82000-03-20 06:03:29 +0000229}
230
Jan Engelhardt59d16402007-10-04 16:28:39 +0000231static void icmp_print(const void *ip, const struct xt_entry_match *match,
232 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000233{
234 const struct ipt_icmp *icmp = (struct ipt_icmp *)match->data;
235
Jan Engelhardt73866352010-12-18 02:04:59 +0100236 printf(" icmp");
Marc Bouchere6869a82000-03-20 06:03:29 +0000237 print_icmptype(icmp->type, icmp->code[0], icmp->code[1],
238 icmp->invflags & IPT_ICMP_INV,
239 numeric);
240
241 if (icmp->invflags & ~IPT_ICMP_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100242 printf(" Unknown invflags: 0x%X",
Marc Bouchere6869a82000-03-20 06:03:29 +0000243 icmp->invflags & ~IPT_ICMP_INV);
244}
245
Jan Engelhardt59d16402007-10-04 16:28:39 +0000246static void icmp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000247{
248 const struct ipt_icmp *icmp = (struct ipt_icmp *)match->data;
249
250 if (icmp->invflags & IPT_ICMP_INV)
Jan Engelhardt73866352010-12-18 02:04:59 +0100251 printf(" !");
Marc Bouchere6869a82000-03-20 06:03:29 +0000252
Harald Weltefc9237d2003-02-25 10:50:59 +0000253 /* special hack for 'any' case */
254 if (icmp->type == 0xFF) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100255 printf(" --icmp-type any");
Harald Weltefc9237d2003-02-25 10:50:59 +0000256 } else {
Jan Engelhardt73866352010-12-18 02:04:59 +0100257 printf(" --icmp-type %u", icmp->type);
Harald Weltefc9237d2003-02-25 10:50:59 +0000258 if (icmp->code[0] != 0 || icmp->code[1] != 0xFF)
259 printf("/%u", icmp->code[0]);
Harald Weltefc9237d2003-02-25 10:50:59 +0000260 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000261}
262
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200263static struct xtables_match icmp_mt_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000264 .name = "icmp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200265 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100266 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200267 .size = XT_ALIGN(sizeof(struct ipt_icmp)),
268 .userspacesize = XT_ALIGN(sizeof(struct ipt_icmp)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000269 .help = icmp_help,
270 .init = icmp_init,
271 .parse = icmp_parse,
272 .print = icmp_print,
273 .save = icmp_save,
274 .extra_opts = icmp_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000275};
276
277void _init(void)
278{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200279 xtables_register_match(&icmp_mt_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000280}