libip6t_mh.c revision 9ee386a1b6d7704b259460152c959ab0e79e02aa
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
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
61static void mh_help(void)
62{
63	printf(
64"MH v%s options:\n"
65" --mh-type [!] type[:type]	match mh type\n",
66IPTABLES_VERSION);
67	print_types_all();
68}
69
70static void mh_init(struct xt_entry_match *m)
71{
72	struct ip6t_mh *mhinfo = (struct ip6t_mh *)m->data;
73
74	mhinfo->types[1] = 0xFF;
75}
76
77static unsigned int name_to_type(const char *name)
78{
79	int namelen = strlen(name);
80	unsigned int limit = sizeof(mh_names)/sizeof(struct mh_name);
81	unsigned int match = limit;
82	unsigned int i;
83
84	for (i = 0; i < limit; i++) {
85		if (strncasecmp(mh_names[i].name, name, namelen) == 0) {
86			int len = strlen(mh_names[i].name);
87			if (match == limit || len == namelen)
88				match = i;
89		}
90	}
91
92	if (match != limit) {
93		return mh_names[match].type;
94	} else {
95		unsigned int number;
96
97		if (string_to_number(name, 0, 255, &number) == -1)
98			exit_error(PARAMETER_PROBLEM,
99				   "Invalid MH type `%s'\n", name);
100		return number;
101	}
102}
103
104static void parse_mh_types(const char *mhtype, u_int8_t *types)
105{
106	char *buffer;
107	char *cp;
108
109	buffer = strdup(mhtype);
110	if ((cp = strchr(buffer, ':')) == NULL)
111		types[0] = types[1] = name_to_type(buffer);
112	else {
113		*cp = '\0';
114		cp++;
115
116		types[0] = buffer[0] ? name_to_type(buffer) : 0;
117		types[1] = cp[0] ? name_to_type(cp) : 0xFF;
118
119		if (types[0] > types[1])
120			exit_error(PARAMETER_PROBLEM,
121				   "Invalid MH type range (min > max)");
122	}
123	free(buffer);
124}
125
126#define MH_TYPES 0x01
127
128static int mh_parse(int c, char **argv, int invert, unsigned int *flags,
129                    const void *entry, struct xt_entry_match **match)
130{
131	struct ip6t_mh *mhinfo = (struct ip6t_mh *)(*match)->data;
132
133	switch (c) {
134	case '1':
135		if (*flags & MH_TYPES)
136			exit_error(PARAMETER_PROBLEM,
137				   "Only one `--mh-type' allowed");
138		check_inverse(optarg, &invert, &optind, 0);
139		parse_mh_types(argv[optind-1], mhinfo->types);
140		if (invert)
141			mhinfo->invflags |= IP6T_MH_INV_TYPE;
142		*flags |= MH_TYPES;
143		break;
144
145	default:
146		return 0;
147	}
148
149	return 1;
150}
151
152static const char *type_to_name(u_int8_t type)
153{
154	unsigned int i;
155
156	for (i = 0; i < sizeof(mh_names)/sizeof(struct mh_name); i++) {
157		if (mh_names[i].type == type)
158			return mh_names[i].name;
159	}
160
161	return NULL;
162}
163
164static void print_type(u_int8_t type, int numeric)
165{
166	const char *name;
167	if (numeric || !(name = type_to_name(type)))
168		printf("%u", type);
169	else
170		printf("%s", name);
171}
172
173static void print_types(u_int8_t min, u_int8_t max, int invert, int numeric)
174{
175	const char *inv = invert ? "!" : "";
176
177	if (min != 0 || max != 0xFF || invert) {
178		if (min == max) {
179			printf("%s", inv);
180			print_type(min, numeric);
181		} else {
182			printf("%s", inv);
183			print_type(min, numeric);
184			printf(":");
185			print_type(max, numeric);
186		}
187		printf(" ");
188	}
189}
190
191static void mh_print(const void *ip, const struct xt_entry_match *match,
192                     int numeric)
193{
194	const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
195
196	printf("mh ");
197	print_types(mhinfo->types[0], mhinfo->types[1],
198		    mhinfo->invflags & IP6T_MH_INV_TYPE,
199		    numeric);
200	if (mhinfo->invflags & ~IP6T_MH_INV_MASK)
201		printf("Unknown invflags: 0x%X ",
202		       mhinfo->invflags & ~IP6T_MH_INV_MASK);
203}
204
205static void mh_save(const void *ip, const struct xt_entry_match *match)
206{
207	const struct ip6t_mh *mhinfo = (struct ip6t_mh *)match->data;
208
209	if (mhinfo->types[0] == 0 && mhinfo->types[1] == 0xFF)
210		return;
211
212	if (mhinfo->invflags & IP6T_MH_INV_TYPE)
213		printf("! ");
214
215	if (mhinfo->types[0] != mhinfo->types[1])
216		printf("--mh-type %u:%u ", mhinfo->types[0], mhinfo->types[1]);
217	else
218		printf("--mh-type %u ", mhinfo->types[0]);
219}
220
221static const struct option mh_opts[] = {
222	{ "mh-type", 1, NULL, '1' },
223	{ .name = NULL }
224};
225
226static struct ip6tables_match mh_match6 = {
227	.name		= "mh",
228	.version	= IPTABLES_VERSION,
229	.size		= IP6T_ALIGN(sizeof(struct ip6t_mh)),
230	.userspacesize	= IP6T_ALIGN(sizeof(struct ip6t_mh)),
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,
237};
238
239void _init(void)
240{
241	register_match6(&mh_match6);
242}
243