blob: f8c4e2474534ac33e43b9d1d2136de9e2f8bf9f7 [file] [log] [blame]
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +00001/* 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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010019#include <xtables.h>
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000020#include <linux/netfilter_ipv6/ip6_tables.h>
21#include <linux/netfilter_ipv6/ip6t_mh.h>
22
23struct mh_name {
24 const char *name;
25 u_int8_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 < 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 Engelhardt997045f2007-10-04 16:29:21 +000061static void mh_help(void)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000062{
63 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020064"mh match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020065"[!] --mh-type type[:type] match mh type\n");
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000066 print_types_all();
67}
68
Jan Engelhardt997045f2007-10-04 16:29:21 +000069static void mh_init(struct xt_entry_match *m)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000070{
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 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
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010096 if (!xtables_strtoui(name, NULL, &number, 0, UINT8_MAX))
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +000097 exit_error(PARAMETER_PROBLEM,
98 "Invalid MH type `%s'\n", name);
99 return number;
100 }
101}
102
103static 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 Engelhardt997045f2007-10-04 16:29:21 +0000127static int mh_parse(int c, char **argv, int invert, unsigned int *flags,
128 const void *entry, struct xt_entry_match **match)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000129{
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 NAKAMURA00d46e12007-02-09 11:24:14 +0000151static 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
163static 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
172static 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 Engelhardt997045f2007-10-04 16:29:21 +0000190static void mh_print(const void *ip, const struct xt_entry_match *match,
191 int numeric)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000192{
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 Engelhardt997045f2007-10-04 16:29:21 +0000204static void mh_save(const void *ip, const struct xt_entry_match *match)
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000205{
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 Engelhardt997045f2007-10-04 16:29:21 +0000220static const struct option mh_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +0000221 { "mh-type", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +0000222 { .name = NULL }
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000223};
224
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200225static struct xtables_match mh_mt6_reg = {
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000226 .name = "mh",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200227 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100228 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200229 .size = XT_ALIGN(sizeof(struct ip6t_mh)),
230 .userspacesize = XT_ALIGN(sizeof(struct ip6t_mh)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000231 .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 NAKAMURA00d46e12007-02-09 11:24:14 +0000237};
238
239void _init(void)
240{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200241 xtables_register_match(&mh_mt6_reg);
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000242}