1 /* Shared library add-on to ip6tables to add mobility header support. */
3 * Copyright (C)2006 USAGI/WIDE Project
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.
10 * Masahide NAKAMURA @USAGI <masahide.nakamura.cz@hitachi.com>
12 * Based on libip6t_{icmpv6,udp}.c
20 #include <linux/netfilter_ipv6/ip6t_mh.h>
27 static const struct mh_name mh_names[] = {
28 { "binding-refresh-request", 0, },
29 /* Alias */ { "brr", 0, },
30 { "home-test-init", 1, },
31 /* Alias */ { "hoti", 1, },
32 { "careof-test-init", 2, },
33 /* Alias */ { "coti", 2, },
35 /* Alias */ { "hot", 3, },
36 { "careof-test", 4, },
37 /* Alias */ { "cot", 4, },
38 { "binding-update", 5, },
39 /* Alias */ { "bu", 5, },
40 { "binding-acknowledgement", 6, },
41 /* Alias */ { "ba", 6, },
42 { "binding-error", 7, },
43 /* Alias */ { "be", 7, },
46 static void print_types_all(void)
49 printf("Valid MH types:");
51 for (i = 0; i < ARRAY_SIZE(mh_names); ++i) {
52 if (i && mh_names[i].type == mh_names[i-1].type)
53 printf(" (%s)", mh_names[i].name);
55 printf("\n%s", mh_names[i].name);
60 static void mh_help(void)
64 "[!] --mh-type type[:type] match mh type\n");
68 static void mh_init(struct xt_entry_match *m)
70 struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data;
72 mhinfo->types[1] = 0xFF;
75 static unsigned int name_to_type(const char *name)
77 int namelen = strlen(name);
78 static const unsigned int limit = ARRAY_SIZE(mh_names);
79 unsigned int match = limit;
82 for (i = 0; i < limit; i++) {
83 if (strncasecmp(mh_names[i].name, name, namelen) == 0) {
84 int len = strlen(mh_names[i].name);
85 if (match == limit || len == namelen)
91 return mh_names[match].type;
95 if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX))
96 xtables_error(PARAMETER_PROBLEM,
97 "Invalid MH type `%s'\n", name);
102 static void parse_mh_types(const char *mhtype, u_int8_t *types)
107 buffer = strdup(mhtype);
108 if ((cp = strchr(buffer, ':')) == NULL)
109 types[0] = types[1] = name_to_type(buffer);
114 types[0] = buffer[0] ? name_to_type(buffer) : 0;
115 types[1] = cp[0] ? name_to_type(cp) : 0xFF;
117 if (types[0] > types[1])
118 xtables_error(PARAMETER_PROBLEM,
119 "Invalid MH type range (min > max)");
124 #define MH_TYPES 0x01
126 static int mh_parse(int c, char **argv, int invert, unsigned int *flags,
127 const void *entry, struct xt_entry_match **match)
129 struct ip6t_mh *mhinfo = (struct ip6t_mh *)(*match)->data;
133 if (*flags & MH_TYPES)
134 xtables_error(PARAMETER_PROBLEM,
135 "Only one `--mh-type' allowed");
136 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
137 parse_mh_types(optarg, mhinfo->types);
139 mhinfo->invflags |= IP6T_MH_INV_TYPE;
150 static const char *type_to_name(u_int8_t type)
154 for (i = 0; i < ARRAY_SIZE(mh_names); ++i)
155 if (mh_names[i].type == type)
156 return mh_names[i].name;
161 static void print_type(u_int8_t type, int numeric)
164 if (numeric || !(name = type_to_name(type)))
170 static void print_types(u_int8_t min, u_int8_t max, int invert, int numeric)
172 const char *inv = invert ? "!" : "";
174 if (min != 0 || max != 0xFF || invert) {
177 print_type(min, numeric);
180 print_type(min, numeric);
182 print_type(max, numeric);
188 static void mh_print(const void *ip, const struct xt_entry_match *match,
191 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
194 print_types(mhinfo->types[0], mhinfo->types[1],
195 mhinfo->invflags & IP6T_MH_INV_TYPE,
197 if (mhinfo->invflags & ~IP6T_MH_INV_MASK)
198 printf("Unknown invflags: 0x%X ",
199 mhinfo->invflags & ~IP6T_MH_INV_MASK);
202 static void mh_save(const void *ip, const struct xt_entry_match *match)
204 const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
206 if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF)
209 if (mhinfo->invflags & IP6T_MH_INV_TYPE)
212 if (mhinfo->types[0] != mhinfo->types[1])
213 printf("--mh-type %u:%u ", mhinfo->types[0], mhinfo->types[1]);
215 printf("--mh-type %u ", mhinfo->types[0]);
218 static const struct option mh_opts[] = {
219 { "mh-type", 1, NULL, '1' },
223 static struct xtables_match mh_mt6_reg = {
225 .version = XTABLES_VERSION,
226 .family = NFPROTO_IPV6,
227 .size = XT_ALIGN(sizeof(struct ip6t_mh)),
228 .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)),
234 .extra_opts = mh_opts,
239 xtables_register_match(&mh_mt6_reg);