libip6t_mh.c revision 73866357e4a7a0fdc1b293bf8863fee2bd56da9e
1/* Shared library add-on to ip6tables to add mobility header support. */ 2/* 3 * Copyright (C)2006 USAGI/WIDE Project 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Author: 10 * Masahide NAKAMURA @USAGI <masahide.nakamura.cz@hitachi.com> 11 * 12 * Based on libip6t_{icmpv6,udp}.c 13 */ 14#include <stdbool.h> 15#include <stdio.h> 16#include <netdb.h> 17#include <string.h> 18#include <stdlib.h> 19#include <getopt.h> 20#include <xtables.h> 21#include <linux/netfilter_ipv6/ip6t_mh.h> 22 23struct mh_name { 24 const char *name; 25 uint8_t type; 26}; 27 28static const struct mh_name mh_names[] = { 29 { "binding-refresh-request", 0, }, 30 /* Alias */ { "brr", 0, }, 31 { "home-test-init", 1, }, 32 /* Alias */ { "hoti", 1, }, 33 { "careof-test-init", 2, }, 34 /* Alias */ { "coti", 2, }, 35 { "home-test", 3, }, 36 /* Alias */ { "hot", 3, }, 37 { "careof-test", 4, }, 38 /* Alias */ { "cot", 4, }, 39 { "binding-update", 5, }, 40 /* Alias */ { "bu", 5, }, 41 { "binding-acknowledgement", 6, }, 42 /* Alias */ { "ba", 6, }, 43 { "binding-error", 7, }, 44 /* Alias */ { "be", 7, }, 45}; 46 47static void print_types_all(void) 48{ 49 unsigned int i; 50 printf("Valid MH types:"); 51 52 for (i = 0; i < ARRAY_SIZE(mh_names); ++i) { 53 if (i && mh_names[i].type == mh_names[i-1].type) 54 printf(" (%s)", mh_names[i].name); 55 else 56 printf("\n%s", mh_names[i].name); 57 } 58 printf("\n"); 59} 60 61static void mh_help(void) 62{ 63 printf( 64"mh match options:\n" 65"[!] --mh-type type[:type] match mh type\n"); 66 print_types_all(); 67} 68 69static void mh_init(struct xt_entry_match *m) 70{ 71 struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data; 72 73 mhinfo->types[1] = 0xFF; 74} 75 76static unsigned int name_to_type(const char *name) 77{ 78 int namelen = strlen(name); 79 static const unsigned int limit = ARRAY_SIZE(mh_names); 80 unsigned int match = limit; 81 unsigned int i; 82 83 for (i = 0; i < limit; i++) { 84 if (strncasecmp(mh_names[i].name, name, namelen) == 0) { 85 int len = strlen(mh_names[i].name); 86 if (match == limit || len == namelen) 87 match = i; 88 } 89 } 90 91 if (match != limit) { 92 return mh_names[match].type; 93 } else { 94 unsigned int number; 95 96 if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX)) 97 xtables_error(PARAMETER_PROBLEM, 98 "Invalid MH type `%s'\n", name); 99 return number; 100 } 101} 102 103static void parse_mh_types(const char *mhtype, uint8_t *types) 104{ 105 char *buffer; 106 char *cp; 107 108 buffer = strdup(mhtype); 109 if ((cp = strchr(buffer, ':')) == NULL) 110 types[0] = types[1] = name_to_type(buffer); 111 else { 112 *cp = '\0'; 113 cp++; 114 115 types[0] = buffer[0] ? name_to_type(buffer) : 0; 116 types[1] = cp[0] ? name_to_type(cp) : 0xFF; 117 118 if (types[0] > types[1]) 119 xtables_error(PARAMETER_PROBLEM, 120 "Invalid MH type range (min > max)"); 121 } 122 free(buffer); 123} 124 125#define MH_TYPES 0x01 126 127static int mh_parse(int c, char **argv, int invert, unsigned int *flags, 128 const void *entry, struct xt_entry_match **match) 129{ 130 struct ip6t_mh *mhinfo = (struct ip6t_mh *)(*match)->data; 131 132 switch (c) { 133 case '1': 134 if (*flags & MH_TYPES) 135 xtables_error(PARAMETER_PROBLEM, 136 "Only one `--mh-type' allowed"); 137 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 138 parse_mh_types(optarg, mhinfo->types); 139 if (invert) 140 mhinfo->invflags |= IP6T_MH_INV_TYPE; 141 *flags |= MH_TYPES; 142 break; 143 } 144 145 return 1; 146} 147 148static const char *type_to_name(uint8_t type) 149{ 150 unsigned int i; 151 152 for (i = 0; i < ARRAY_SIZE(mh_names); ++i) 153 if (mh_names[i].type == type) 154 return mh_names[i].name; 155 156 return NULL; 157} 158 159static void print_type(uint8_t type, int numeric) 160{ 161 const char *name; 162 if (numeric || !(name = type_to_name(type))) 163 printf("%u", type); 164 else 165 printf("%s", name); 166} 167 168static void print_types(uint8_t min, uint8_t max, int invert, int numeric) 169{ 170 const char *inv = invert ? "!" : ""; 171 172 if (min != 0 || max != 0xFF || invert) { 173 printf(" "); 174 if (min == max) { 175 printf("%s", inv); 176 print_type(min, numeric); 177 } else { 178 printf("%s", inv); 179 print_type(min, numeric); 180 printf(":"); 181 print_type(max, numeric); 182 } 183 } 184} 185 186static void mh_print(const void *ip, const struct xt_entry_match *match, 187 int numeric) 188{ 189 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data; 190 191 printf(" mh"); 192 print_types(mhinfo->types[0], mhinfo->types[1], 193 mhinfo->invflags & IP6T_MH_INV_TYPE, 194 numeric); 195 if (mhinfo->invflags & ~IP6T_MH_INV_MASK) 196 printf(" Unknown invflags: 0x%X", 197 mhinfo->invflags & ~IP6T_MH_INV_MASK); 198} 199 200static void mh_save(const void *ip, const struct xt_entry_match *match) 201{ 202 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data; 203 204 if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF) 205 return; 206 207 if (mhinfo->invflags & IP6T_MH_INV_TYPE) 208 printf(" !"); 209 210 if (mhinfo->types[0] != mhinfo->types[1]) 211 printf(" --mh-type %u:%u", mhinfo->types[0], mhinfo->types[1]); 212 else 213 printf(" --mh-type %u", mhinfo->types[0]); 214} 215 216static const struct option mh_opts[] = { 217 {.name = "mh-type", .has_arg = true, .val = '1'}, 218 XT_GETOPT_TABLEEND, 219}; 220 221static struct xtables_match mh_mt6_reg = { 222 .name = "mh", 223 .version = XTABLES_VERSION, 224 .family = NFPROTO_IPV6, 225 .size = XT_ALIGN(sizeof(struct ip6t_mh)), 226 .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)), 227 .help = mh_help, 228 .init = mh_init, 229 .parse = mh_parse, 230 .print = mh_print, 231 .save = mh_save, 232 .extra_opts = mh_opts, 233}; 234 235void _init(void) 236{ 237 xtables_register_match(&mh_mt6_reg); 238} 239