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 | */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 14 | #include <stdbool.h> |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <netdb.h> |
| 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <getopt.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 20 | #include <xtables.h> |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 21 | #include <linux/netfilter_ipv6/ip6t_mh.h> |
| 22 | |
| 23 | struct mh_name { |
| 24 | const char *name; |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 25 | uint8_t type; |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 52 | for (i = 0; i < ARRAY_SIZE(mh_names); ++i) { |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 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); |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 79 | static const unsigned int limit = ARRAY_SIZE(mh_names); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 96 | if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 97 | xtables_error(PARAMETER_PROBLEM, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 98 | "Invalid MH type `%s'\n", name); |
| 99 | return number; |
| 100 | } |
| 101 | } |
| 102 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 103 | static void parse_mh_types(const char *mhtype, uint8_t *types) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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]) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 119 | xtables_error(PARAMETER_PROBLEM, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 135 | xtables_error(PARAMETER_PROBLEM, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 136 | "Only one `--mh-type' allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 137 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 138 | parse_mh_types(optarg, mhinfo->types); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 139 | if (invert) |
| 140 | mhinfo->invflags |= IP6T_MH_INV_TYPE; |
| 141 | *flags |= MH_TYPES; |
| 142 | break; |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return 1; |
| 146 | } |
| 147 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 148 | static const char *type_to_name(uint8_t type) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 149 | { |
| 150 | unsigned int i; |
| 151 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 152 | for (i = 0; i < ARRAY_SIZE(mh_names); ++i) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 153 | if (mh_names[i].type == type) |
| 154 | return mh_names[i].name; |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 155 | |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 159 | static void print_type(uint8_t type, int numeric) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 168 | static void print_types(uint8_t min, uint8_t max, int invert, int numeric) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 169 | { |
| 170 | const char *inv = invert ? "!" : ""; |
| 171 | |
| 172 | if (min != 0 || max != 0xFF || invert) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 173 | printf(" "); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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 | } |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 186 | static void mh_print(const void *ip, const struct xt_entry_match *match, |
| 187 | int numeric) |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 188 | { |
| 189 | const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data; |
| 190 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 191 | printf(" mh"); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 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) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 196 | printf(" Unknown invflags: 0x%X", |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 197 | mhinfo->invflags & ~IP6T_MH_INV_MASK); |
| 198 | } |
| 199 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 200 | 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] | 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) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 208 | printf(" !"); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 209 | |
| 210 | if (mhinfo->types[0] != mhinfo->types[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 211 | printf(" --mh-type %u:%u", mhinfo->types[0], mhinfo->types[1]); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 212 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 213 | printf(" --mh-type %u", mhinfo->types[0]); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 216 | static const struct option mh_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 217 | {.name = "mh-type", .has_arg = true, .val = '1'}, |
| 218 | XT_GETOPT_TABLEEND, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 221 | static struct xtables_match mh_mt6_reg = { |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 222 | .name = "mh", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 223 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 224 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 225 | .size = XT_ALIGN(sizeof(struct ip6t_mh)), |
| 226 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 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, |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | void _init(void) |
| 236 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 237 | xtables_register_match(&mh_mt6_reg); |
Masahide NAKAMURA | 00d46e1 | 2007-02-09 11:24:14 +0000 | [diff] [blame] | 238 | } |