Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 1 | /* 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ó | 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 |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 56 | print_icmpv6types() |
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 | |
| 76 | /* Function which prints out usage message. */ |
| 77 | static void |
| 78 | help(void) |
| 79 | { |
| 80 | printf( |
| 81 | "ICMPv6 v%s options:\n" |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 82 | " --icmpv6-type [!] typename match icmpv6 type\n" |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 83 | " (or numeric type or type/code)\n" |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 84 | "\n", IPTABLES_VERSION); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 85 | print_icmpv6types(); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static struct option opts[] = { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 89 | { "icmpv6-type", 1, 0, '1' }, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 90 | {0} |
| 91 | }; |
| 92 | |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 93 | static void |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 94 | 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] | 95 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 96 | unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 97 | unsigned int match = limit; |
| 98 | unsigned int i; |
| 99 | |
| 100 | for (i = 0; i < limit; i++) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 101 | if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type)) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 102 | == 0) { |
| 103 | if (match != limit) |
| 104 | exit_error(PARAMETER_PROBLEM, |
| 105 | "Ambiguous ICMPv6 type `%s':" |
| 106 | " `%s' or `%s'?", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 107 | icmpv6type, |
| 108 | icmpv6_codes[match].name, |
| 109 | icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 110 | match = i; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (match != limit) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 115 | *type = icmpv6_codes[match].type; |
| 116 | code[0] = icmpv6_codes[match].code_min; |
| 117 | code[1] = icmpv6_codes[match].code_max; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 118 | } else { |
| 119 | char *slash; |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 120 | char buffer[strlen(icmpv6type) + 1]; |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 121 | unsigned int number; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 122 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 123 | strcpy(buffer, icmpv6type); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 124 | slash = strchr(buffer, '/'); |
| 125 | |
| 126 | if (slash) |
| 127 | *slash = '\0'; |
| 128 | |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 129 | if (string_to_number(buffer, 0, 255, &number) == -1) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 130 | exit_error(PARAMETER_PROBLEM, |
| 131 | "Invalid ICMPv6 type `%s'\n", buffer); |
| 132 | *type = number; |
| 133 | if (slash) { |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 134 | if (string_to_number(slash+1, 0, 255, &number) == -1) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 135 | 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 | } |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* Initialize the match. */ |
| 147 | static void |
Yasuyuki KOZAKAI | b85256b | 2007-07-24 05:58:56 +0000 | [diff] [blame] | 148 | init(struct xt_entry_match *m, unsigned int *nfcache) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 149 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 150 | struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 151 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 152 | icmpv6info->code[1] = 0xFF; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* Function which parses command options; returns true if it |
| 156 | ate an option */ |
| 157 | static int |
| 158 | parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | a620c61 | 2007-07-24 06:03:45 +0000 | [diff] [blame^] | 159 | const void *entry, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 160 | unsigned int *nfcache, |
Yasuyuki KOZAKAI | b85256b | 2007-07-24 05:58:56 +0000 | [diff] [blame] | 161 | struct xt_entry_match **match) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 162 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 163 | struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 164 | |
| 165 | switch (c) { |
| 166 | case '1': |
Yasuyuki KOZAKAI | b1cda88 | 2006-07-04 10:23:26 +0000 | [diff] [blame] | 167 | if (*flags == 1) |
| 168 | exit_error(PARAMETER_PROBLEM, |
| 169 | "icmpv6 match: only use --icmpv6-type once!"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 170 | check_inverse(optarg, &invert, &optind, 0); |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 171 | parse_icmpv6(argv[optind-1], &icmpv6info->type, |
| 172 | icmpv6info->code); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 173 | if (invert) |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 174 | icmpv6info->invflags |= IP6T_ICMP_INV; |
Yasuyuki KOZAKAI | b1cda88 | 2006-07-04 10:23:26 +0000 | [diff] [blame] | 175 | *flags = 1; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | default: |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | return 1; |
| 183 | } |
| 184 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 185 | static void print_icmpv6type(u_int8_t type, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 186 | u_int8_t code_min, u_int8_t code_max, |
| 187 | int invert, |
| 188 | int numeric) |
| 189 | { |
| 190 | if (!numeric) { |
| 191 | unsigned int i; |
| 192 | |
| 193 | for (i = 0; |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 194 | i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 195 | i++) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 196 | if (icmpv6_codes[i].type == type |
| 197 | && icmpv6_codes[i].code_min == code_min |
| 198 | && icmpv6_codes[i].code_max == code_max) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 202 | if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) { |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 203 | printf("%s%s ", |
| 204 | invert ? "!" : "", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 205 | icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (invert) |
| 211 | printf("!"); |
| 212 | |
| 213 | printf("type %u", type); |
| 214 | if (code_min == 0 && code_max == 0xFF) |
| 215 | printf(" "); |
| 216 | else if (code_min == code_max) |
| 217 | printf(" code %u ", code_min); |
| 218 | else |
| 219 | printf(" codes %u-%u ", code_min, code_max); |
| 220 | } |
| 221 | |
| 222 | /* Prints out the union ipt_matchinfo. */ |
| 223 | static void |
Yasuyuki KOZAKAI | a620c61 | 2007-07-24 06:03:45 +0000 | [diff] [blame^] | 224 | print(const void *ip, |
Yasuyuki KOZAKAI | b85256b | 2007-07-24 05:58:56 +0000 | [diff] [blame] | 225 | const struct xt_entry_match *match, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 226 | int numeric) |
| 227 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 228 | const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 229 | |
Harald Welte | cfaed1f | 2001-10-04 08:11:44 +0000 | [diff] [blame] | 230 | printf("ipv6-icmp "); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 231 | print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1], |
| 232 | icmpv6->invflags & IP6T_ICMP_INV, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 233 | numeric); |
| 234 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 235 | if (icmpv6->invflags & ~IP6T_ICMP_INV) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 236 | printf("Unknown invflags: 0x%X ", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 237 | icmpv6->invflags & ~IP6T_ICMP_INV); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Saves the match in parsable form to stdout. */ |
Yasuyuki KOZAKAI | a620c61 | 2007-07-24 06:03:45 +0000 | [diff] [blame^] | 241 | static void save(const void *ip, const struct xt_entry_match *match) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 242 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 243 | const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 244 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 245 | if (icmpv6->invflags & IP6T_ICMP_INV) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 246 | printf("! "); |
| 247 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 248 | printf("--icmpv6-type %u", icmpv6->type); |
| 249 | if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF) |
| 250 | printf("/%u", icmpv6->code[0]); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 251 | printf(" "); |
| 252 | } |
| 253 | |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 254 | static void final_check(unsigned int flags) |
| 255 | { |
Yasuyuki KOZAKAI | b1cda88 | 2006-07-04 10:23:26 +0000 | [diff] [blame] | 256 | if (!flags) |
| 257 | exit_error(PARAMETER_PROBLEM, |
| 258 | "icmpv6 match: You must specify `--icmpv6-type'"); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 261 | static struct ip6tables_match icmpv6 = { |
| 262 | .name = "icmp6", |
| 263 | .version = IPTABLES_VERSION, |
| 264 | .size = IP6T_ALIGN(sizeof(struct ip6t_icmp)), |
| 265 | .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_icmp)), |
| 266 | .help = &help, |
| 267 | .init = &init, |
| 268 | .parse = &parse, |
| 269 | .final_check = &final_check, |
| 270 | .print = &print, |
| 271 | .save = &save, |
| 272 | .extra_opts = opts, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | void _init(void) |
| 276 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 277 | register_match6(&icmpv6); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 278 | } |