Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 1 | #include <stdint.h> |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 3 | #include <string.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 4 | #include <xtables.h> |
Jan Engelhardt | 4e41854 | 2009-02-21 03:46:37 +0100 | [diff] [blame] | 5 | #include <limits.h> /* INT_MAX in ip6_tables.h */ |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 6 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 7 | |
Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 8 | enum { |
| 9 | O_ICMPV6_TYPE = 0, |
| 10 | }; |
| 11 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 12 | struct icmpv6_names { |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 13 | const char *name; |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 14 | uint8_t type; |
| 15 | uint8_t code_min, code_max; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 16 | }; |
| 17 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 18 | static const struct icmpv6_names icmpv6_codes[] = { |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 19 | { "destination-unreachable", 1, 0, 0xFF }, |
| 20 | { "no-route", 1, 0, 0 }, |
| 21 | { "communication-prohibited", 1, 1, 1 }, |
| 22 | { "address-unreachable", 1, 3, 3 }, |
| 23 | { "port-unreachable", 1, 4, 4 }, |
| 24 | |
| 25 | { "packet-too-big", 2, 0, 0xFF }, |
| 26 | |
| 27 | { "time-exceeded", 3, 0, 0xFF }, |
| 28 | /* Alias */ { "ttl-exceeded", 3, 0, 0xFF }, |
| 29 | { "ttl-zero-during-transit", 3, 0, 0 }, |
| 30 | { "ttl-zero-during-reassembly", 3, 1, 1 }, |
| 31 | |
| 32 | { "parameter-problem", 4, 0, 0xFF }, |
| 33 | { "bad-header", 4, 0, 0 }, |
| 34 | { "unknown-header-type", 4, 1, 1 }, |
| 35 | { "unknown-option", 4, 2, 2 }, |
| 36 | |
| 37 | { "echo-request", 128, 0, 0xFF }, |
| 38 | /* Alias */ { "ping", 128, 0, 0xFF }, |
| 39 | |
| 40 | { "echo-reply", 129, 0, 0xFF }, |
| 41 | /* Alias */ { "pong", 129, 0, 0xFF }, |
| 42 | |
| 43 | { "router-solicitation", 133, 0, 0xFF }, |
| 44 | |
| 45 | { "router-advertisement", 134, 0, 0xFF }, |
| 46 | |
| 47 | { "neighbour-solicitation", 135, 0, 0xFF }, |
| 48 | /* Alias */ { "neighbor-solicitation", 135, 0, 0xFF }, |
| 49 | |
| 50 | { "neighbour-advertisement", 136, 0, 0xFF }, |
| 51 | /* Alias */ { "neighbor-advertisement", 136, 0, 0xFF }, |
| 52 | |
| 53 | { "redirect", 137, 0, 0xFF }, |
| 54 | |
| 55 | }; |
| 56 | |
| 57 | static void |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 58 | print_icmpv6types(void) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned int i; |
| 61 | printf("Valid ICMPv6 Types:"); |
| 62 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 63 | for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i) { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 64 | if (i && icmpv6_codes[i].type == icmpv6_codes[i-1].type) { |
| 65 | if (icmpv6_codes[i].code_min == icmpv6_codes[i-1].code_min |
| 66 | && (icmpv6_codes[i].code_max |
| 67 | == icmpv6_codes[i-1].code_max)) |
| 68 | printf(" (%s)", icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 69 | else |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 70 | printf("\n %s", icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 71 | } |
| 72 | else |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 73 | printf("\n%s", icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 74 | } |
| 75 | printf("\n"); |
| 76 | } |
| 77 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 78 | static void icmp6_help(void) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 79 | { |
| 80 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 81 | "icmpv6 match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 82 | "[!] --icmpv6-type typename match icmpv6 type\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 83 | " (or numeric type or type/code)\n"); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 84 | print_icmpv6types(); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 87 | static const struct xt_option_entry icmp6_opts[] = { |
| 88 | {.name = "icmpv6-type", .id = O_ICMPV6_TYPE, .type = XTTYPE_STRING, |
| 89 | .flags = XTOPT_MAND | XTOPT_INVERT}, |
| 90 | XTOPT_TABLEEND, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Pablo Neira | 8115e54 | 2005-02-14 13:13:04 +0000 | [diff] [blame] | 93 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 94 | parse_icmpv6(const char *icmpv6type, uint8_t *type, uint8_t code[]) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 95 | { |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 96 | static const unsigned int limit = ARRAY_SIZE(icmpv6_codes); |
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) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 104 | xtables_error(PARAMETER_PROBLEM, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 129 | if (!xtables_strtoui(buffer, NULL, &number, 0, UINT8_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 130 | xtables_error(PARAMETER_PROBLEM, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 131 | "Invalid ICMPv6 type `%s'\n", buffer); |
| 132 | *type = number; |
| 133 | if (slash) { |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 134 | if (!xtables_strtoui(slash+1, NULL, &number, 0, UINT8_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 135 | xtables_error(PARAMETER_PROBLEM, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 146 | static void icmp6_init(struct xt_entry_match *m) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 147 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 148 | struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data; |
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 | icmpv6info->code[1] = 0xFF; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 153 | static void icmp6_parse(struct xt_option_call *cb) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 154 | { |
Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 155 | struct ip6t_icmp *icmpv6info = cb->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 156 | |
Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 157 | xtables_option_parse(cb); |
| 158 | parse_icmpv6(cb->arg, &icmpv6info->type, icmpv6info->code); |
| 159 | if (cb->invert) |
| 160 | icmpv6info->invflags |= IP6T_ICMP_INV; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 163 | static void print_icmpv6type(uint8_t type, |
| 164 | uint8_t code_min, uint8_t code_max, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 165 | int invert, |
| 166 | int numeric) |
| 167 | { |
| 168 | if (!numeric) { |
| 169 | unsigned int i; |
| 170 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 171 | for (i = 0; i < ARRAY_SIZE(icmpv6_codes); ++i) |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 172 | if (icmpv6_codes[i].type == type |
| 173 | && icmpv6_codes[i].code_min == code_min |
| 174 | && icmpv6_codes[i].code_max == code_max) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 175 | break; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 176 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 177 | if (i != ARRAY_SIZE(icmpv6_codes)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 178 | printf(" %s%s", |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 179 | invert ? "!" : "", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 180 | icmpv6_codes[i].name); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 186 | printf(" !"); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 187 | |
| 188 | printf("type %u", type); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 189 | if (code_min == code_max) |
| 190 | printf(" code %u", code_min); |
| 191 | else if (code_min != 0 || code_max != 0xFF) |
| 192 | printf(" codes %u-%u", code_min, code_max); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 195 | static void icmp6_print(const void *ip, const struct xt_entry_match *match, |
| 196 | int numeric) |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 197 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 198 | const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 199 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 200 | printf(" ipv6-icmp"); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 201 | print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1], |
| 202 | icmpv6->invflags & IP6T_ICMP_INV, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 203 | numeric); |
| 204 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 205 | if (icmpv6->invflags & ~IP6T_ICMP_INV) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 206 | printf(" Unknown invflags: 0x%X", |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 207 | icmpv6->invflags & ~IP6T_ICMP_INV); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 210 | 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] | 211 | { |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 212 | const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 213 | |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 214 | if (icmpv6->invflags & IP6T_ICMP_INV) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 215 | printf(" !"); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 216 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 217 | printf(" --icmpv6-type %u", icmpv6->type); |
András Kis-Szabó | e0bc7a4 | 2001-07-14 20:21:46 +0000 | [diff] [blame] | 218 | if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF) |
| 219 | printf("/%u", icmpv6->code[0]); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 222 | static struct xtables_match icmp6_mt6_reg = { |
Harald Welte | 02aa733 | 2005-02-01 15:38:20 +0000 | [diff] [blame] | 223 | .name = "icmp6", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 224 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 225 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 226 | .size = XT_ALIGN(sizeof(struct ip6t_icmp)), |
| 227 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_icmp)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 228 | .help = icmp6_help, |
| 229 | .init = icmp6_init, |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 230 | .print = icmp6_print, |
| 231 | .save = icmp6_save, |
Jan Engelhardt | 1b8db4f | 2011-03-01 18:36:15 +0100 | [diff] [blame] | 232 | .x6_parse = icmp6_parse, |
| 233 | .x6_options = icmp6_opts, |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | void _init(void) |
| 237 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 238 | xtables_register_match(&icmp6_mt6_reg); |
Philip Blundell | b47050c | 2000-06-04 17:38:47 +0000 | [diff] [blame] | 239 | } |