1 /* Shared library add-on to iptables to add ICMP support. */
8 #include <linux/netfilter_ipv6/ip6_tables.h>
13 u_int8_t code_min, code_max;
16 static const struct icmpv6_names icmpv6_codes[] = {
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 },
23 { "packet-too-big", 2, 0, 0xFF },
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 },
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 },
35 { "echo-request", 128, 0, 0xFF },
36 /* Alias */ { "ping", 128, 0, 0xFF },
38 { "echo-reply", 129, 0, 0xFF },
39 /* Alias */ { "pong", 129, 0, 0xFF },
41 { "router-solicitation", 133, 0, 0xFF },
43 { "router-advertisement", 134, 0, 0xFF },
45 { "neighbour-solicitation", 135, 0, 0xFF },
46 /* Alias */ { "neighbor-solicitation", 135, 0, 0xFF },
48 { "neighbour-advertisement", 136, 0, 0xFF },
49 /* Alias */ { "neighbor-advertisement", 136, 0, 0xFF },
51 { "redirect", 137, 0, 0xFF },
59 printf("Valid ICMPv6 Types:");
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);
68 printf("\n %s", icmpv6_codes[i].name);
71 printf("\n%s", icmpv6_codes[i].name);
76 /* Function which prints out usage message. */
81 "ICMPv6 v%s options:\n"
82 " --icmpv6-type [!] typename match icmpv6 type\n"
83 " (or numeric type or type/code)\n"
84 "\n", IPTABLES_VERSION);
88 static struct option opts[] = {
89 { "icmpv6-type", 1, 0, '1' },
94 parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[])
96 unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
97 unsigned int match = limit;
100 for (i = 0; i < limit; i++) {
101 if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type))
104 exit_error(PARAMETER_PROBLEM,
105 "Ambiguous ICMPv6 type `%s':"
108 icmpv6_codes[match].name,
109 icmpv6_codes[i].name);
114 if (match != limit) {
115 *type = icmpv6_codes[match].type;
116 code[0] = icmpv6_codes[match].code_min;
117 code[1] = icmpv6_codes[match].code_max;
120 char buffer[strlen(icmpv6type) + 1];
123 strcpy(buffer, icmpv6type);
124 slash = strchr(buffer, '/');
129 if (string_to_number(buffer, 0, 255, &number) == -1)
130 exit_error(PARAMETER_PROBLEM,
131 "Invalid ICMPv6 type `%s'\n", buffer);
134 if (string_to_number(slash+1, 0, 255, &number) == -1)
135 exit_error(PARAMETER_PROBLEM,
136 "Invalid ICMPv6 code `%s'\n",
138 code[0] = code[1] = number;
145 if (code[0] == 0 && code[1] == 0xFF)
146 return NFC_IP6_SRC_PT;
147 else return NFC_IP6_SRC_PT | NFC_IP6_DST_PT;
150 /* Initialize the match. */
152 init(struct ip6t_entry_match *m, unsigned int *nfcache)
154 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data;
156 icmpv6info->code[1] = 0xFF;
159 /* Function which parses command options; returns true if it
162 parse(int c, char **argv, int invert, unsigned int *flags,
163 const struct ip6t_entry *entry,
164 unsigned int *nfcache,
165 struct ip6t_entry_match **match)
167 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data;
171 check_inverse(optarg, &invert, &optind, 0);
172 *nfcache |= parse_icmpv6(argv[optind-1],
176 icmpv6info->invflags |= IP6T_ICMP_INV;
186 static void print_icmpv6type(u_int8_t type,
187 u_int8_t code_min, u_int8_t code_max,
195 i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names);
197 if (icmpv6_codes[i].type == type
198 && icmpv6_codes[i].code_min == code_min
199 && icmpv6_codes[i].code_max == code_max)
203 if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) {
206 icmpv6_codes[i].name);
214 printf("type %u", type);
215 if (code_min == 0 && code_max == 0xFF)
217 else if (code_min == code_max)
218 printf(" code %u ", code_min);
220 printf(" codes %u-%u ", code_min, code_max);
223 /* Prints out the union ipt_matchinfo. */
225 print(const struct ip6t_ip6 *ip,
226 const struct ip6t_entry_match *match,
229 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
231 printf("ipv6-icmp ");
232 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1],
233 icmpv6->invflags & IP6T_ICMP_INV,
236 if (icmpv6->invflags & ~IP6T_ICMP_INV)
237 printf("Unknown invflags: 0x%X ",
238 icmpv6->invflags & ~IP6T_ICMP_INV);
241 /* Saves the match in parsable form to stdout. */
242 static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
244 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data;
246 if (icmpv6->invflags & IP6T_ICMP_INV)
249 printf("--icmpv6-type %u", icmpv6->type);
250 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF)
251 printf("/%u", icmpv6->code[0]);
255 /* Final check; we don't care. */
256 static void final_check(unsigned int flags)
260 static struct ip6tables_match icmpv6
264 IP6T_ALIGN(sizeof(struct ip6t_icmp)),
265 IP6T_ALIGN(sizeof(struct ip6t_icmp)),
277 register_match6(&icmpv6);