libip6t_hbh.c revision b9210cfd9da3d57610be4e86ef45c48dd1b65edf
1/* Shared library add-on to ip6tables to add Hop-by-Hop header support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <errno.h>
8#include <xtables.h>
9/*#include <linux/in6.h>*/
10#include <linux/netfilter_ipv6/ip6t_opts.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <arpa/inet.h>
14
15#define DEBUG		0
16
17static void hbh_help(void)
18{
19	printf(
20"hbh match options:\n"
21"[!] --hbh-len length            total length of this header\n"
22"  --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n"
23"                                Options and its length (list, max: %d)\n",
24IP6T_OPTS_OPTSNR);
25}
26
27static const struct option hbh_opts[] = {
28	{.name = "hbh-len",        .has_arg = true, .val = '1'},
29	{.name = "hbh-opts",       .has_arg = true, .val = '2'},
30	XT_GETOPT_TABLEEND,
31};
32
33static uint32_t
34parse_opts_num(const char *idstr, const char *typestr)
35{
36	unsigned long int id;
37	char* ep;
38
39	id =  strtoul(idstr,&ep,0) ;
40
41	if ( idstr == ep ) {
42		xtables_error(PARAMETER_PROBLEM,
43			   "hbh: no valid digits in %s `%s'", typestr, idstr);
44	}
45	if ( id == ULONG_MAX  && errno == ERANGE ) {
46		xtables_error(PARAMETER_PROBLEM,
47			   "%s `%s' specified too big: would overflow",
48			   typestr, idstr);
49	}
50	if ( *idstr != '\0'  && *ep != '\0' ) {
51		xtables_error(PARAMETER_PROBLEM,
52			   "hbh: error parsing %s `%s'", typestr, idstr);
53	}
54	return id;
55}
56
57static int
58parse_options(const char *optsstr, uint16_t *opts)
59{
60        char *buffer, *cp, *next, *range;
61        unsigned int i;
62
63	buffer = strdup(optsstr);
64	if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
65
66        for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
67        {
68                next=strchr(cp, ',');
69                if (next) *next++='\0';
70                range = strchr(cp, ':');
71                if (range) {
72                        if (i == IP6T_OPTS_OPTSNR-1)
73				xtables_error(PARAMETER_PROBLEM,
74                                           "too many ports specified");
75                        *range++ = '\0';
76                }
77		opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
78                if (range) {
79			if (opts[i] == 0)
80				xtables_error(PARAMETER_PROBLEM, "PAD0 has not got length");
81			opts[i] |= parse_opts_num(range, "length") & 0xFF;
82                } else {
83                        opts[i] |= (0x00FF);
84		}
85
86#if DEBUG
87		printf("opts str: %s %s\n", cp, range);
88		printf("opts opt: %04X\n", opts[i]);
89#endif
90	}
91	if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
92
93	free(buffer);
94
95#if DEBUG
96	printf("addr nr: %d\n", i);
97#endif
98
99	return i;
100}
101
102static int hbh_parse(int c, char **argv, int invert, unsigned int *flags,
103                     const void *entry, struct xt_entry_match **match)
104{
105	struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data;
106
107	switch (c) {
108	case '1':
109		if (*flags & IP6T_OPTS_LEN)
110			xtables_error(PARAMETER_PROBLEM,
111				   "Only one `--hbh-len' allowed");
112		xtables_check_inverse(optarg, &invert, &optind, 0, argv);
113		optinfo->hdrlen = parse_opts_num(optarg, "length");
114		if (invert)
115			optinfo->invflags |= IP6T_OPTS_INV_LEN;
116		optinfo->flags |= IP6T_OPTS_LEN;
117		*flags |= IP6T_OPTS_LEN;
118		break;
119	case '2':
120		if (*flags & IP6T_OPTS_OPTS)
121			xtables_error(PARAMETER_PROBLEM,
122				   "Only one `--hbh-opts' allowed");
123                xtables_check_inverse(optarg, &invert, &optind, 0, argv);
124                if (invert)
125			xtables_error(PARAMETER_PROBLEM,
126				" '!' not allowed with `--hbh-opts'");
127		optinfo->optsnr = parse_options(optarg, optinfo->opts);
128		optinfo->flags |= IP6T_OPTS_OPTS;
129		*flags |= IP6T_OPTS_OPTS;
130		break;
131	}
132
133	return 1;
134}
135
136static void
137print_options(unsigned int optsnr, uint16_t *optsp)
138{
139	unsigned int i;
140
141	for(i=0; i<optsnr; i++){
142		printf("%c", (i==0)?' ':',');
143		printf("%d", (optsp[i] & 0xFF00)>>8);
144		if ((optsp[i] & 0x00FF) != 0x00FF){
145			printf(":%d", (optsp[i] & 0x00FF));
146		}
147	}
148}
149
150static void hbh_print(const void *ip, const struct xt_entry_match *match,
151                      int numeric)
152{
153	const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
154
155	printf(" hbh");
156	if (optinfo->flags & IP6T_OPTS_LEN) {
157		printf(" length");
158		printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "");
159		printf("%u", optinfo->hdrlen);
160	}
161	if (optinfo->flags & IP6T_OPTS_OPTS) printf(" opts");
162	print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
163	if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
164		printf(" Unknown invflags: 0x%X",
165		       optinfo->invflags & ~IP6T_OPTS_INV_MASK);
166}
167
168static void hbh_save(const void *ip, const struct xt_entry_match *match)
169{
170	const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
171
172	if (optinfo->flags & IP6T_OPTS_LEN) {
173		printf("%s --hbh-len %u",
174			(optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "",
175			optinfo->hdrlen);
176	}
177
178	if (optinfo->flags & IP6T_OPTS_OPTS)
179		printf(" --hbh-opts");
180	print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
181}
182
183static struct xtables_match hbh_mt6_reg = {
184	.name 		= "hbh",
185	.version	= XTABLES_VERSION,
186	.family		= NFPROTO_IPV6,
187	.size		= XT_ALIGN(sizeof(struct ip6t_opts)),
188	.userspacesize	= XT_ALIGN(sizeof(struct ip6t_opts)),
189	.help		= hbh_help,
190	.parse		= hbh_parse,
191	.print		= hbh_print,
192	.save		= hbh_save,
193	.extra_opts	= hbh_opts,
194};
195
196void
197_init(void)
198{
199	xtables_register_match(&hbh_mt6_reg);
200}
201