Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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 <stdio.h> |
| 15 | #include <netdb.h> |
| 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <getopt.h> |
| 19 | #include <ip6tables.h> |
| 20 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 21 | #include <linux/netfilter_ipv6/ip6t_mh.h> |
| 22 | |
| 23 | struct mh_name { |
| 24 | const char *name; |
| 25 | u_int8_t type; |
| 26 | }; |
| 27 | |
| 28 | static 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 | |
| 47 | static void print_types_all(void) |
| 48 | { |
| 49 | unsigned int i; |
| 50 | printf("Valid MH types:"); |
| 51 | |
| 52 | for (i = 0; i < sizeof(mh_names)/sizeof(struct mh_name); 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 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 61 | static void mh_help(void) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 62 | { |
| 63 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 64 | "mh match options:\n" |
| 65 | " --mh-type [!] type[:type] match mh type\n"); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 66 | print_types_all(); |
| 67 | } |
| 68 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 69 | static void mh_init(struct xt_entry_match *m) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 70 | { |
| 71 | struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data; |
| 72 | |
| 73 | mhinfo->types[1] = 0xFF; |
| 74 | } |
| 75 | |
| 76 | static unsigned int name_to_type(const char *name) |
| 77 | { |
| 78 | int namelen = strlen(name); |
| 79 | unsigned int limit = sizeof(mh_names)/sizeof(struct mh_name); |
| 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 (string_to_number(name, 0, 255, &number) == -1) |
| 97 | exit_error(PARAMETER_PROBLEM, |
| 98 | "Invalid MH type `%s'\n", name); |
| 99 | return number; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void parse_mh_types(const char *mhtype, u_int8_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 | exit_error(PARAMETER_PROBLEM, |
| 120 | "Invalid MH type range (min > max)"); |
| 121 | } |
| 122 | free(buffer); |
| 123 | } |
| 124 | |
| 125 | #define MH_TYPES 0x01 |
| 126 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 127 | static int mh_parse(int c, char **argv, int invert, unsigned int *flags, |
| 128 | const void *entry, struct xt_entry_match **match) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 129 | { |
| 130 | struct ip6t_mh *mhinfo = (struct ip6t_mh *)(*match)->data; |
| 131 | |
| 132 | switch (c) { |
| 133 | case '1': |
| 134 | if (*flags & MH_TYPES) |
| 135 | exit_error(PARAMETER_PROBLEM, |
| 136 | "Only one `--mh-type' allowed"); |
| 137 | check_inverse(optarg, &invert, &optind, 0); |
| 138 | parse_mh_types(argv[optind-1], mhinfo->types); |
| 139 | if (invert) |
| 140 | mhinfo->invflags |= IP6T_MH_INV_TYPE; |
| 141 | *flags |= MH_TYPES; |
| 142 | break; |
| 143 | |
| 144 | default: |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | return 1; |
| 149 | } |
| 150 | |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 151 | static const char *type_to_name(u_int8_t type) |
| 152 | { |
| 153 | unsigned int i; |
| 154 | |
| 155 | for (i = 0; i < sizeof(mh_names)/sizeof(struct mh_name); i++) { |
| 156 | if (mh_names[i].type == type) |
| 157 | return mh_names[i].name; |
| 158 | } |
| 159 | |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | static void print_type(u_int8_t type, int numeric) |
| 164 | { |
| 165 | const char *name; |
| 166 | if (numeric || !(name = type_to_name(type))) |
| 167 | printf("%u", type); |
| 168 | else |
| 169 | printf("%s", name); |
| 170 | } |
| 171 | |
| 172 | static void print_types(u_int8_t min, u_int8_t max, int invert, int numeric) |
| 173 | { |
| 174 | const char *inv = invert ? "!" : ""; |
| 175 | |
| 176 | if (min != 0 || max != 0xFF || invert) { |
| 177 | if (min == max) { |
| 178 | printf("%s", inv); |
| 179 | print_type(min, numeric); |
| 180 | } else { |
| 181 | printf("%s", inv); |
| 182 | print_type(min, numeric); |
| 183 | printf(":"); |
| 184 | print_type(max, numeric); |
| 185 | } |
| 186 | printf(" "); |
| 187 | } |
| 188 | } |
| 189 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 190 | static void mh_print(const void *ip, const struct xt_entry_match *match, |
| 191 | int numeric) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 192 | { |
| 193 | const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data; |
| 194 | |
| 195 | printf("mh "); |
| 196 | print_types(mhinfo->types[0], mhinfo->types[1], |
| 197 | mhinfo->invflags & IP6T_MH_INV_TYPE, |
| 198 | numeric); |
| 199 | if (mhinfo->invflags & ~IP6T_MH_INV_MASK) |
| 200 | printf("Unknown invflags: 0x%X ", |
| 201 | mhinfo->invflags & ~IP6T_MH_INV_MASK); |
| 202 | } |
| 203 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 204 | static void mh_save(const void *ip, const struct xt_entry_match *match) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 205 | { |
| 206 | const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data; |
| 207 | |
| 208 | if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF) |
| 209 | return; |
| 210 | |
| 211 | if (mhinfo->invflags & IP6T_MH_INV_TYPE) |
| 212 | printf("! "); |
| 213 | |
| 214 | if (mhinfo->types[0] != mhinfo->types[1]) |
| 215 | printf("--mh-type %u:%u ", mhinfo->types[0], mhinfo->types[1]); |
| 216 | else |
| 217 | printf("--mh-type %u ", mhinfo->types[0]); |
| 218 | } |
| 219 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 220 | static const struct option mh_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 221 | { "mh-type", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 222 | { .name = NULL } |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 225 | static struct xtables_match mh_mt6_reg = { |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 226 | .name = "mh", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 227 | .version = XTABLES_VERSION, |
| 228 | .family = PF_INET6, |
| 229 | .size = XT_ALIGN(sizeof(struct ip6t_mh)), |
| 230 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 231 | .help = mh_help, |
| 232 | .init = mh_init, |
| 233 | .parse = mh_parse, |
| 234 | .print = mh_print, |
| 235 | .save = mh_save, |
| 236 | .extra_opts = mh_opts, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | void _init(void) |
| 240 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame^] | 241 | xtables_register_match(&mh_mt6_reg); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 242 | } |