libip6t_rt.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
1/* Shared library add-on to ip6tables to add Routing header support. */ 2#include <stdbool.h> 3#include <stdio.h> 4#include <netdb.h> 5#include <string.h> 6#include <stdlib.h> 7#include <getopt.h> 8#include <errno.h> 9#include <xtables.h> 10/*#include <linux/in6.h>*/ 11#include <linux/netfilter_ipv6/ip6t_rt.h> 12#include <sys/types.h> 13#include <sys/socket.h> 14#include <arpa/inet.h> 15 16/*#define DEBUG 1*/ 17 18static void rt_help(void) 19{ 20 printf( 21"rt match options:\n" 22"[!] --rt-type type match the type\n" 23"[!] --rt-segsleft num[:num] match the Segments Left field (range)\n" 24"[!] --rt-len length total length of this header\n" 25" --rt-0-res check the reserved filed, too (type 0)\n" 26" --rt-0-addrs ADDR[,ADDR...] Type=0 addresses (list, max: %d)\n" 27" --rt-0-not-strict List of Type=0 addresses not a strict list\n", 28IP6T_RT_HOPS); 29} 30 31static const struct option rt_opts[] = { 32 {.name = "rt-type", .has_arg = true, .val = '1'}, 33 {.name = "rt-segsleft", .has_arg = true, .val = '2'}, 34 {.name = "rt-len", .has_arg = true, .val = '3'}, 35 {.name = "rt-0-res", .has_arg = false, .val = '4'}, 36 {.name = "rt-0-addrs", .has_arg = true, .val = '5'}, 37 {.name = "rt-0-not-strict", .has_arg = false, .val = '6'}, 38 XT_GETOPT_TABLEEND, 39}; 40 41static uint32_t 42parse_rt_num(const char *idstr, const char *typestr) 43{ 44 unsigned long int id; 45 char* ep; 46 47 id = strtoul(idstr,&ep,0) ; 48 49 if ( idstr == ep ) { 50 xtables_error(PARAMETER_PROBLEM, 51 "RT no valid digits in %s `%s'", typestr, idstr); 52 } 53 if ( id == ULONG_MAX && errno == ERANGE ) { 54 xtables_error(PARAMETER_PROBLEM, 55 "%s `%s' specified too big: would overflow", 56 typestr, idstr); 57 } 58 if ( *idstr != '\0' && *ep != '\0' ) { 59 xtables_error(PARAMETER_PROBLEM, 60 "RT error parsing %s `%s'", typestr, idstr); 61 } 62 return id; 63} 64 65static void 66parse_rt_segsleft(const char *idstring, uint32_t *ids) 67{ 68 char *buffer; 69 char *cp; 70 71 buffer = strdup(idstring); 72 if ((cp = strchr(buffer, ':')) == NULL) 73 ids[0] = ids[1] = parse_rt_num(buffer,"segsleft"); 74 else { 75 *cp = '\0'; 76 cp++; 77 78 ids[0] = buffer[0] ? parse_rt_num(buffer,"segsleft") : 0; 79 ids[1] = cp[0] ? parse_rt_num(cp,"segsleft") : 0xFFFFFFFF; 80 } 81 free(buffer); 82} 83 84static char * 85addr_to_numeric(const struct in6_addr *addrp) 86{ 87 static char buf[50+1]; 88 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf)); 89} 90 91static struct in6_addr * 92numeric_to_addr(const char *num) 93{ 94 static struct in6_addr ap; 95 int err; 96 97 if ((err=inet_pton(AF_INET6, num, &ap)) == 1) 98 return ≈ 99#ifdef DEBUG 100 fprintf(stderr, "\nnumeric2addr: %d\n", err); 101#endif 102 xtables_error(PARAMETER_PROBLEM, "bad address: %s", num); 103 104 return (struct in6_addr *)NULL; 105} 106 107 108static int 109parse_addresses(const char *addrstr, struct in6_addr *addrp) 110{ 111 char *buffer, *cp, *next; 112 unsigned int i; 113 114 buffer = strdup(addrstr); 115 if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed"); 116 117 for (cp=buffer, i=0; cp && i<IP6T_RT_HOPS; cp=next,i++) 118 { 119 next=strchr(cp, ','); 120 if (next) *next++='\0'; 121 memcpy(&(addrp[i]), numeric_to_addr(cp), sizeof(struct in6_addr)); 122#if DEBUG 123 printf("addr str: %s\n", cp); 124 printf("addr ip6: %s\n", addr_to_numeric((numeric_to_addr(cp)))); 125 printf("addr [%d]: %s\n", i, addr_to_numeric(&(addrp[i]))); 126#endif 127 } 128 if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified"); 129 130 free(buffer); 131 132#if DEBUG 133 printf("addr nr: %d\n", i); 134#endif 135 136 return i; 137} 138 139static void rt_init(struct xt_entry_match *m) 140{ 141 struct ip6t_rt *rtinfo = (struct ip6t_rt *)m->data; 142 143 rtinfo->rt_type = 0x0L; 144 rtinfo->segsleft[0] = 0x0L; 145 rtinfo->segsleft[1] = 0xFFFFFFFF; 146 rtinfo->hdrlen = 0; 147 rtinfo->flags = 0; 148 rtinfo->invflags = 0; 149 rtinfo->addrnr = 0; 150} 151 152static int rt_parse(int c, char **argv, int invert, unsigned int *flags, 153 const void *entry, struct xt_entry_match **match) 154{ 155 struct ip6t_rt *rtinfo = (struct ip6t_rt *)(*match)->data; 156 157 switch (c) { 158 case '1': 159 if (*flags & IP6T_RT_TYP) 160 xtables_error(PARAMETER_PROBLEM, 161 "Only one `--rt-type' allowed"); 162 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 163 rtinfo->rt_type = parse_rt_num(optarg, "type"); 164 if (invert) 165 rtinfo->invflags |= IP6T_RT_INV_TYP; 166 rtinfo->flags |= IP6T_RT_TYP; 167 *flags |= IP6T_RT_TYP; 168 break; 169 case '2': 170 if (*flags & IP6T_RT_SGS) 171 xtables_error(PARAMETER_PROBLEM, 172 "Only one `--rt-segsleft' allowed"); 173 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 174 parse_rt_segsleft(optarg, rtinfo->segsleft); 175 if (invert) 176 rtinfo->invflags |= IP6T_RT_INV_SGS; 177 rtinfo->flags |= IP6T_RT_SGS; 178 *flags |= IP6T_RT_SGS; 179 break; 180 case '3': 181 if (*flags & IP6T_RT_LEN) 182 xtables_error(PARAMETER_PROBLEM, 183 "Only one `--rt-len' allowed"); 184 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 185 rtinfo->hdrlen = parse_rt_num(optarg, "length"); 186 if (invert) 187 rtinfo->invflags |= IP6T_RT_INV_LEN; 188 rtinfo->flags |= IP6T_RT_LEN; 189 *flags |= IP6T_RT_LEN; 190 break; 191 case '4': 192 if (*flags & IP6T_RT_RES) 193 xtables_error(PARAMETER_PROBLEM, 194 "Only one `--rt-0-res' allowed"); 195 if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) ) 196 xtables_error(PARAMETER_PROBLEM, 197 "`--rt-type 0' required before `--rt-0-res'"); 198 rtinfo->flags |= IP6T_RT_RES; 199 *flags |= IP6T_RT_RES; 200 break; 201 case '5': 202 if (*flags & IP6T_RT_FST) 203 xtables_error(PARAMETER_PROBLEM, 204 "Only one `--rt-0-addrs' allowed"); 205 if ( !(*flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) || (rtinfo->invflags & IP6T_RT_INV_TYP) ) 206 xtables_error(PARAMETER_PROBLEM, 207 "`--rt-type 0' required before `--rt-0-addrs'"); 208 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 209 if (invert) 210 xtables_error(PARAMETER_PROBLEM, 211 " '!' not allowed with `--rt-0-addrs'"); 212 rtinfo->addrnr = parse_addresses(optarg, rtinfo->addrs); 213 rtinfo->flags |= IP6T_RT_FST; 214 *flags |= IP6T_RT_FST; 215 break; 216 case '6': 217 if (*flags & IP6T_RT_FST_NSTRICT) 218 xtables_error(PARAMETER_PROBLEM, 219 "Only one `--rt-0-not-strict' allowed"); 220 if ( !(*flags & IP6T_RT_FST) ) 221 xtables_error(PARAMETER_PROBLEM, 222 "`--rt-0-addr ...' required before `--rt-0-not-strict'"); 223 rtinfo->flags |= IP6T_RT_FST_NSTRICT; 224 *flags |= IP6T_RT_FST_NSTRICT; 225 break; 226 } 227 228 return 1; 229} 230 231static void 232print_nums(const char *name, uint32_t min, uint32_t max, 233 int invert) 234{ 235 const char *inv = invert ? "!" : ""; 236 237 if (min != 0 || max != 0xFFFFFFFF || invert) { 238 printf(" %s", name); 239 if (min == max) { 240 printf(":%s", inv); 241 printf("%u", min); 242 } else { 243 printf("s:%s", inv); 244 printf("%u",min); 245 printf(":"); 246 printf("%u",max); 247 } 248 } 249} 250 251static void 252print_addresses(unsigned int addrnr, struct in6_addr *addrp) 253{ 254 unsigned int i; 255 256 for(i=0; i<addrnr; i++){ 257 printf("%c%s", (i==0)?' ':',', addr_to_numeric(&(addrp[i]))); 258 } 259} 260 261static void rt_print(const void *ip, const struct xt_entry_match *match, 262 int numeric) 263{ 264 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data; 265 266 printf(" rt"); 267 if (rtinfo->flags & IP6T_RT_TYP) 268 printf(" type:%s%d", rtinfo->invflags & IP6T_RT_INV_TYP ? "!" : "", 269 rtinfo->rt_type); 270 print_nums("segsleft", rtinfo->segsleft[0], rtinfo->segsleft[1], 271 rtinfo->invflags & IP6T_RT_INV_SGS); 272 if (rtinfo->flags & IP6T_RT_LEN) { 273 printf(" length"); 274 printf(":%s", rtinfo->invflags & IP6T_RT_INV_LEN ? "!" : ""); 275 printf("%u", rtinfo->hdrlen); 276 } 277 if (rtinfo->flags & IP6T_RT_RES) printf(" reserved"); 278 if (rtinfo->flags & IP6T_RT_FST) printf(" 0-addrs"); 279 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs); 280 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" 0-not-strict"); 281 if (rtinfo->invflags & ~IP6T_RT_INV_MASK) 282 printf(" Unknown invflags: 0x%X", 283 rtinfo->invflags & ~IP6T_RT_INV_MASK); 284} 285 286static void rt_save(const void *ip, const struct xt_entry_match *match) 287{ 288 const struct ip6t_rt *rtinfo = (struct ip6t_rt *)match->data; 289 290 if (rtinfo->flags & IP6T_RT_TYP) { 291 printf("%s --rt-type %u", 292 (rtinfo->invflags & IP6T_RT_INV_TYP) ? " !" : "", 293 rtinfo->rt_type); 294 } 295 296 if (!(rtinfo->segsleft[0] == 0 297 && rtinfo->segsleft[1] == 0xFFFFFFFF)) { 298 printf("%s --rt-segsleft ", 299 (rtinfo->invflags & IP6T_RT_INV_SGS) ? " !" : ""); 300 if (rtinfo->segsleft[0] 301 != rtinfo->segsleft[1]) 302 printf("%u:%u", 303 rtinfo->segsleft[0], 304 rtinfo->segsleft[1]); 305 else 306 printf("%u", 307 rtinfo->segsleft[0]); 308 } 309 310 if (rtinfo->flags & IP6T_RT_LEN) { 311 printf("%s --rt-len %u", 312 (rtinfo->invflags & IP6T_RT_INV_LEN) ? " !" : "", 313 rtinfo->hdrlen); 314 } 315 316 if (rtinfo->flags & IP6T_RT_RES) printf(" --rt-0-res"); 317 if (rtinfo->flags & IP6T_RT_FST) printf(" --rt-0-addrs"); 318 print_addresses(rtinfo->addrnr, (struct in6_addr *)rtinfo->addrs); 319 if (rtinfo->flags & IP6T_RT_FST_NSTRICT) printf(" --rt-0-not-strict"); 320 321} 322 323static struct xtables_match rt_mt6_reg = { 324 .name = "rt", 325 .version = XTABLES_VERSION, 326 .family = NFPROTO_IPV6, 327 .size = XT_ALIGN(sizeof(struct ip6t_rt)), 328 .userspacesize = XT_ALIGN(sizeof(struct ip6t_rt)), 329 .help = rt_help, 330 .init = rt_init, 331 .parse = rt_parse, 332 .print = rt_print, 333 .save = rt_save, 334 .extra_opts = rt_opts, 335}; 336 337void 338_init(void) 339{ 340 xtables_register_match(&rt_mt6_reg); 341} 342