libip6t_icmp6.c revision 1f312c2e3ee57eadde7c8bfc12d838aa0f60e99e
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 10struct icmpv6_names { 11 const char *name; 12 u_int8_t type; 13 u_int8_t code_min, code_max; 14}; 15 16static 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 }, 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 55static void 56print_icmpv6types() 57{ 58 unsigned int i; 59 printf("Valid ICMPv6 Types:"); 60 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); 67 else 68 printf("\n %s", icmpv6_codes[i].name); 69 } 70 else 71 printf("\n%s", icmpv6_codes[i].name); 72 } 73 printf("\n"); 74} 75 76/* Function which prints out usage message. */ 77static void 78help(void) 79{ 80 printf( 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); 85 print_icmpv6types(); 86} 87 88static struct option opts[] = { 89 { "icmpv6-type", 1, 0, '1' }, 90 {0} 91}; 92 93static void 94parse_icmpv6(const char *icmpv6type, u_int8_t *type, u_int8_t code[]) 95{ 96 unsigned int limit = sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); 97 unsigned int match = limit; 98 unsigned int i; 99 100 for (i = 0; i < limit; i++) { 101 if (strncasecmp(icmpv6_codes[i].name, icmpv6type, strlen(icmpv6type)) 102 == 0) { 103 if (match != limit) 104 exit_error(PARAMETER_PROBLEM, 105 "Ambiguous ICMPv6 type `%s':" 106 " `%s' or `%s'?", 107 icmpv6type, 108 icmpv6_codes[match].name, 109 icmpv6_codes[i].name); 110 match = i; 111 } 112 } 113 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; 118 } else { 119 char *slash; 120 char buffer[strlen(icmpv6type) + 1]; 121 unsigned int number; 122 123 strcpy(buffer, icmpv6type); 124 slash = strchr(buffer, '/'); 125 126 if (slash) 127 *slash = '\0'; 128 129 if (string_to_number(buffer, 0, 255, &number) == -1) 130 exit_error(PARAMETER_PROBLEM, 131 "Invalid ICMPv6 type `%s'\n", buffer); 132 *type = number; 133 if (slash) { 134 if (string_to_number(slash+1, 0, 255, &number) == -1) 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 } 144} 145 146/* Initialize the match. */ 147static void 148init(struct ip6t_entry_match *m, unsigned int *nfcache) 149{ 150 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)m->data; 151 152 icmpv6info->code[1] = 0xFF; 153} 154 155/* Function which parses command options; returns true if it 156 ate an option */ 157static int 158parse(int c, char **argv, int invert, unsigned int *flags, 159 const struct ip6t_entry *entry, 160 unsigned int *nfcache, 161 struct ip6t_entry_match **match) 162{ 163 struct ip6t_icmp *icmpv6info = (struct ip6t_icmp *)(*match)->data; 164 165 switch (c) { 166 case '1': 167 check_inverse(optarg, &invert, &optind, 0); 168 parse_icmpv6(argv[optind-1], &icmpv6info->type, 169 icmpv6info->code); 170 if (invert) 171 icmpv6info->invflags |= IP6T_ICMP_INV; 172 break; 173 174 default: 175 return 0; 176 } 177 178 return 1; 179} 180 181static void print_icmpv6type(u_int8_t type, 182 u_int8_t code_min, u_int8_t code_max, 183 int invert, 184 int numeric) 185{ 186 if (!numeric) { 187 unsigned int i; 188 189 for (i = 0; 190 i < sizeof(icmpv6_codes)/sizeof(struct icmpv6_names); 191 i++) { 192 if (icmpv6_codes[i].type == type 193 && icmpv6_codes[i].code_min == code_min 194 && icmpv6_codes[i].code_max == code_max) 195 break; 196 } 197 198 if (i != sizeof(icmpv6_codes)/sizeof(struct icmpv6_names)) { 199 printf("%s%s ", 200 invert ? "!" : "", 201 icmpv6_codes[i].name); 202 return; 203 } 204 } 205 206 if (invert) 207 printf("!"); 208 209 printf("type %u", type); 210 if (code_min == 0 && code_max == 0xFF) 211 printf(" "); 212 else if (code_min == code_max) 213 printf(" code %u ", code_min); 214 else 215 printf(" codes %u-%u ", code_min, code_max); 216} 217 218/* Prints out the union ipt_matchinfo. */ 219static void 220print(const struct ip6t_ip6 *ip, 221 const struct ip6t_entry_match *match, 222 int numeric) 223{ 224 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; 225 226 printf("ipv6-icmp "); 227 print_icmpv6type(icmpv6->type, icmpv6->code[0], icmpv6->code[1], 228 icmpv6->invflags & IP6T_ICMP_INV, 229 numeric); 230 231 if (icmpv6->invflags & ~IP6T_ICMP_INV) 232 printf("Unknown invflags: 0x%X ", 233 icmpv6->invflags & ~IP6T_ICMP_INV); 234} 235 236/* Saves the match in parsable form to stdout. */ 237static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match) 238{ 239 const struct ip6t_icmp *icmpv6 = (struct ip6t_icmp *)match->data; 240 241 if (icmpv6->invflags & IP6T_ICMP_INV) 242 printf("! "); 243 244 printf("--icmpv6-type %u", icmpv6->type); 245 if (icmpv6->code[0] != 0 || icmpv6->code[1] != 0xFF) 246 printf("/%u", icmpv6->code[0]); 247 printf(" "); 248} 249 250/* Final check; we don't care. */ 251static void final_check(unsigned int flags) 252{ 253} 254 255static struct ip6tables_match icmpv6 = { 256 .name = "icmp6", 257 .version = IPTABLES_VERSION, 258 .size = IP6T_ALIGN(sizeof(struct ip6t_icmp)), 259 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_icmp)), 260 .help = &help, 261 .init = &init, 262 .parse = &parse, 263 .final_check = &final_check, 264 .print = &print, 265 .save = &save, 266 .extra_opts = opts, 267}; 268 269void _init(void) 270{ 271 register_match6(&icmpv6); 272} 273