Jan Engelhardt | ddac6c5 | 2008-09-01 14:22:19 +0200 | [diff] [blame] | 1 | /* Shared library add-on to ip6tables to add ICMP support. */ |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 7 | #include <xtables.h> |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 8 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 9 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 10 | struct icmpv6_names { |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 11 | const char *name; |
| 12 | u_int8_t type; |
| 13 | u_int8_t code_min, code_max; |
| 14 | }; |
| 15 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 16 | static const struct icmpv6_names icmpv6_codes[] = { |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 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 | |
| 55 | static void |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 56 | print_icmpv6types(void) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 57 | { |
| 58 | unsigned int i; |
| 59 | printf("Valid ICMPv6 Types:"); |
| 60 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 61 | 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 Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 67 | else |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 68 | printf("\n %s", icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 69 | } |
| 70 | else |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 71 | printf("\n%s", icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 72 | } |
| 73 | printf("\n"); |
| 74 | } |
| 75 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 76 | static void icmp6_help(void) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 77 | { |
| 78 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 79 | "icmpv6 match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 80 | "[!] --icmpv6-type typename match icmpv6 type\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 81 | " (or numeric type or type/code)\n"); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 82 | print_icmpv6types(); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 85 | static const struct option icmp6_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 86 | { "icmpv6-type", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 87 | { .name = NULL } |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 90 | static void |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 91 | parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[]) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 92 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 93 | unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 94 | unsigned int match = limit; |
| 95 | unsigned int i; |
| 96 | |
| 97 | for (i = 0; i < limit; i++) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 98 | if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type)) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 99 | == 0) { |
| 100 | if (match != limit) |
| 101 | exit_error(PARAMETER_PROBLEM, |
| 102 | "Ambiguous ICMPv6 type `%s':" |
| 103 | " `%s' or `%s'?", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 104 | icmpv6type, |
| 105 | icmpv6_codes[match].name, |
| 106 | icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 107 | match = i; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (match != limit) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 112 | *type = icmpv6_codes[match].type; |
| 113 | code[0] = icmpv6_codes[match].code_min; |
| 114 | code[1] = icmpv6_codes[match].code_max; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 115 | } else { |
| 116 | char *slash; |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 117 | char buffer[strlen(icmpv6type) + 1]; |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 118 | unsigned int number; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 119 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 120 | strcpy(buffer, icmpv6type); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 121 | slash = strchr(buffer, '/'); |
| 122 | |
| 123 | if (slash) |
| 124 | *slash = '\0'; |
| 125 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 126 | if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX)) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 127 | exit_error(PARAMETER_PROBLEM, |
| 128 | "Invalid ICMPv6 type `%s'\n", buffer); |
| 129 | *type = number; |
| 130 | if (slash) { |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 131 | if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX)) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 132 | exit_error(PARAMETER_PROBLEM, |
| 133 | "Invalid ICMPv6 code `%s'\n", |
| 134 | slash+1); |
| 135 | code[0] = code[1] = number; |
| 136 | } else { |
| 137 | code[0] = 0; |
| 138 | code[1] = 0xFF; |
| 139 | } |
| 140 | } |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 143 | static void icmp6_init(struct xt_entry_match *m) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 144 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 145 | struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 146 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 147 | icmpv6info->code[1] = 0xFF; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 150 | static int icmp6_parse(int c, char **argv, int invert, unsigned int *flags, |
| 151 | const void *entry, struct xt_entry_match **match) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 152 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 153 | struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 154 | |
| 155 | switch (c) { |
| 156 | case '1': |
Yasuyuki KOZAKAI | b1cda88 | 2006-07-04 10:23:26 +0000 | [diff] [blame] | 157 | if (*flags == 1) |
| 158 | exit_error(PARAMETER_PROBLEM, |
| 159 | "icmpv6 match: only use --icmpv6-type once!"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 160 | check_inverse(optarg, &invert, &optind, 0); |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 161 | parse_icmpv6(argv[optind-1], &icmpv6info->type, |
| 162 | icmpv6info->code); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 163 | if (invert) |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 164 | icmpv6info->invflags |= IP6T_ICMP_INV; |
Yasuyuki KOZAKAI | b1cda88 | 2006-07-04 10:23:26 +0000 | [diff] [blame] | 165 | *flags = 1; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | default: |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | return 1; |
| 173 | } |
| 174 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 175 | static void print_icmpv6type(u_int8_t type, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 176 | u_int8_t code_min, u_int8_t code_max, |
| 177 | int invert, |
| 178 | int numeric) |
| 179 | { |
| 180 | if (!numeric) { |
| 181 | unsigned int i; |
| 182 | |
| 183 | for (i = 0; |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 184 | i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 185 | i++) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 186 | if (icmpv6_codes[i].type == type |
| 187 | && icmpv6_codes[i].code_min == code_min |
| 188 | && icmpv6_codes[i].code_max == code_max) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 192 | if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) { |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 193 | printf("%s%s ", |
| 194 | invert ? "!" : "", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 195 | icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (invert) |
| 201 | printf("!"); |
| 202 | |
| 203 | printf("type %u", type); |
| 204 | if (code_min == 0 && code_max == 0xFF) |
| 205 | printf(" "); |
| 206 | else if (code_min == code_max) |
| 207 | printf(" code %u ", code_min); |
| 208 | else |
| 209 | printf(" codes %u-%u ", code_min, code_max); |
| 210 | } |
| 211 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 212 | static void icmp6_print(const void *ip, const struct xt_entry_match *match, |
| 213 | int numeric) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 214 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 215 | const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 216 | |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 217 | printf("ipv6-icmp "); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 218 | print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1], |
| 219 | icmpv6->invflags & IP6T_ICMP_INV, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 220 | numeric); |
| 221 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 222 | if (icmpv6->invflags & ~IP6T_ICMP_INV) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 223 | printf("Unknown invflags: 0x%X ", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 224 | icmpv6->invflags & ~IP6T_ICMP_INV); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 227 | static void icmp6_save(const void *ip, const struct xt_entry_match *match) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 228 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 229 | const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 230 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 231 | if (icmpv6->invflags & IP6T_ICMP_INV) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 232 | printf("! "); |
| 233 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 234 | printf("--icmpv6-type %u", icmpv6->type); |
| 235 | if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF) |
| 236 | printf("/%u", icmpv6->code[0]); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 237 | printf(" "); |
| 238 | } |
| 239 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 240 | static void icmp6_check(unsigned int flags) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 241 | { |
Yasuyuki KOZAKAI | b1cda88 | 2006-07-04 10:23:26 +0000 | [diff] [blame] | 242 | if (!flags) |
| 243 | exit_error(PARAMETER_PROBLEM, |
| 244 | "icmpv6 match: You must specify `--icmpv6-type'"); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 247 | static struct xtables_match icmp6_mt6_reg = { |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 248 | .name = "icmp6", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 249 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 250 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 251 | .size = XT_ALIGN(sizeof(struct ip6t_icmp)), |
| 252 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_icmp)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 253 | .help = icmp6_help, |
| 254 | .init = icmp6_init, |
| 255 | .parse = icmp6_parse, |
| 256 | .final_check = icmp6_check, |
| 257 | .print = icmp6_print, |
| 258 | .save = icmp6_save, |
| 259 | .extra_opts = icmp6_opts, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | void _init(void) |
| 263 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 264 | xtables_register_match(&icmp6_mt6_reg); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 265 | } |