blob: 7eb64cd706d43a9fdf13994a374193269b058dd1 [file] [log] [blame]
András Kis-Szabó2ea56492002-04-29 13:51:37 +00001/* Shared library add-on to ip6tables to add Hop-by-Hop and Dst headers 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 <ip6tables.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#define HOPBYHOP 0
17#define UNAME (HOPBYHOP ? "HBH" : "DST")
18#define LNAME (HOPBYHOP ? "hbh" : "dst")
19
20/* Function which prints out usage message. */
21static void
22help(void)
23{
24 printf(
25"%s v%s options:\n"
26" --%s-len [!] length total length of this header\n"
27" --%s-opts TYPE[:LEN][,TYPE[:LEN]...] \n"
28" Options and its length (list, max: %d)\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000029UNAME , IPTABLES_VERSION, LNAME, LNAME, IP6T_OPTS_OPTSNR);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000030}
31
32#if HOPBYHOP
33static struct option opts[] = {
Stephane Ouellette703575d2003-08-23 18:41:47 +000034 { .name = "hbh-len", .has_arg = 1, .flag = 0, .val = '1' },
35 { .name = "hbh-opts", .has_arg = 1, .flag = 0, .val = '2' },
36 { .name = "hbh-not-strict", .has_arg = 1, .flag = 0, .val = '3' },
37 { .name = 0 }
András Kis-Szabó2ea56492002-04-29 13:51:37 +000038};
39#else
40static struct option opts[] = {
Stephane Ouellette703575d2003-08-23 18:41:47 +000041 { .name = "dst-len", .has_arg = 1, .flag = 0, .val = '1' },
42 { .name = "dst-opts", .has_arg = 1, .flag = 0, .val = '2' },
43 { .name = "dst-not-strict", .has_arg = 1, .flag = 0, .val = '3' },
44 { .name = 0 }
András Kis-Szabó2ea56492002-04-29 13:51:37 +000045};
46#endif
47
48static u_int32_t
49parse_opts_num(const char *idstr, const char *typestr)
50{
51 unsigned long int id;
52 char* ep;
53
Stephane Ouellette703575d2003-08-23 18:41:47 +000054 id = strtoul(idstr, &ep, 0);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000055
56 if ( idstr == ep ) {
57 exit_error(PARAMETER_PROBLEM,
58 "%s no valid digits in %s `%s'", UNAME, typestr, idstr);
59 }
60 if ( id == ULONG_MAX && errno == ERANGE ) {
61 exit_error(PARAMETER_PROBLEM,
62 "%s `%s' specified too big: would overflow",
63 typestr, idstr);
64 }
65 if ( *idstr != '\0' && *ep != '\0' ) {
66 exit_error(PARAMETER_PROBLEM,
67 "%s error parsing %s `%s'", UNAME, typestr, idstr);
68 }
69 return (u_int32_t) id;
70}
71
72static int
73parse_options(const char *optsstr, u_int16_t *opts)
74{
75 char *buffer, *cp, *next, *range;
76 unsigned int i;
77
78 buffer = strdup(optsstr);
79 if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed");
80
81 for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
82 {
83 next=strchr(cp, ',');
84 if (next) *next++='\0';
85 range = strchr(cp, ':');
86 if (range) {
87 if (i == IP6T_OPTS_OPTSNR-1)
88 exit_error(PARAMETER_PROBLEM,
89 "too many ports specified");
90 *range++ = '\0';
91 }
92 opts[i] = (u_int16_t)((parse_opts_num(cp,"opt") & 0x000000FF)<<8);
93 if (range) {
94 if (opts[i] == 0)
95 exit_error(PARAMETER_PROBLEM, "PAD0 hasn't got length");
96 opts[i] |= (u_int16_t)(parse_opts_num(range,"length") &
97 0x000000FF);
98 } else {
99 opts[i] |= (0x00FF);
100 }
101
102#if DEBUG
103 printf("opts str: %s %s\n", cp, range);
104 printf("opts opt: %04X\n", opts[i]);
105#endif
106 }
107 if (cp) exit_error(PARAMETER_PROBLEM, "too many addresses specified");
108
109 free(buffer);
110
111#if DEBUG
112 printf("addr nr: %d\n", i);
113#endif
114
115 return i;
116}
117
118/* Initialize the match. */
119static void
120init(struct ip6t_entry_match *m, unsigned int *nfcache)
121{
122 struct ip6t_opts *optinfo = (struct ip6t_opts *)m->data;
123
124 optinfo->hdrlen = 0;
125 optinfo->flags = 0;
126 optinfo->invflags = 0;
127 optinfo->optsnr = 0;
128}
129
130/* Function which parses command options; returns true if it
131 ate an option */
132static int
133parse(int c, char **argv, int invert, unsigned int *flags,
134 const struct ip6t_entry *entry,
135 unsigned int *nfcache,
136 struct ip6t_entry_match **match)
137{
138 struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data;
139
140 switch (c) {
141 case '1':
142 if (*flags & IP6T_OPTS_LEN)
143 exit_error(PARAMETER_PROBLEM,
144 "Only one `--%s-len' allowed", LNAME);
145 check_inverse(optarg, &invert, &optind, 0);
146 optinfo->hdrlen = parse_opts_num(argv[optind-1], "length");
147 if (invert)
148 optinfo->invflags |= IP6T_OPTS_INV_LEN;
149 optinfo->flags |= IP6T_OPTS_LEN;
150 *flags |= IP6T_OPTS_LEN;
151 break;
152 case '2':
153 if (*flags & IP6T_OPTS_OPTS)
154 exit_error(PARAMETER_PROBLEM,
155 "Only one `--%s-opts' allowed", LNAME);
156 check_inverse(optarg, &invert, &optind, 0);
157 if (invert)
158 exit_error(PARAMETER_PROBLEM,
159 " '!' not allowed with `--%s-opts'", LNAME);
160 optinfo->optsnr = parse_options(argv[optind-1], optinfo->opts);
161 optinfo->flags |= IP6T_OPTS_OPTS;
162 *flags |= IP6T_OPTS_OPTS;
163 break;
164 case '3':
165 if (*flags & IP6T_OPTS_NSTRICT)
166 exit_error(PARAMETER_PROBLEM,
167 "Only one `--%s-not-strict' allowed", LNAME);
168 if ( !(*flags & IP6T_OPTS_OPTS) )
169 exit_error(PARAMETER_PROBLEM,
170 "`--%s-opts ...' required before `--%s-not-strict'", LNAME, LNAME);
171 optinfo->flags |= IP6T_OPTS_NSTRICT;
172 *flags |= IP6T_OPTS_NSTRICT;
173 break;
174 default:
175 return 0;
176 }
177
178 return 1;
179}
180
181/* Final check; we don't care. */
182static void
183final_check(unsigned int flags)
184{
185}
186
187static void
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000188print_options(int optsnr, u_int16_t *optsp)
189{
190 unsigned int i;
191
192 for(i=0; i<optsnr; i++){
193 printf("%d", (optsp[i] & 0xFF00)>>8);
194 if ((optsp[i] & 0x00FF) != 0x00FF){
195 printf(":%d", (optsp[i] & 0x00FF));
196 }
197 printf("%c", (i!=optsnr-1)?',':' ');
198 }
199}
200
201/* Prints out the union ip6t_matchinfo. */
202static void
203print(const struct ip6t_ip6 *ip,
204 const struct ip6t_entry_match *match, int numeric)
205{
206 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
207
208 printf("%s ", LNAME);
Stephane Ouellette703575d2003-08-23 18:41:47 +0000209 if (optinfo->flags & IP6T_OPTS_LEN)
210 printf("length:%s%u ",
211 optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "",
212 optinfo->hdrlen);
213
214 if (optinfo->flags & IP6T_OPTS_OPTS)
215 printf("opts ");
216
Fabrice MARIEae31bb62002-06-14 07:38:16 +0000217 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
Stephane Ouellette703575d2003-08-23 18:41:47 +0000218
219 if (optinfo->flags & IP6T_OPTS_NSTRICT)
220 printf("not-strict ");
221
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000222 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
223 printf("Unknown invflags: 0x%X ",
224 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
225}
226
227/* Saves the union ip6t_matchinfo in parsable form to stdout. */
228static void save(const struct ip6t_ip6 *ip, const struct ip6t_entry_match *match)
229{
230 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
231
232 if (optinfo->flags & IP6T_OPTS_LEN) {
233 printf("--%s-len %s%u ", LNAME,
234 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "! " : "",
235 optinfo->hdrlen);
236 }
237
Stephane Ouellette703575d2003-08-23 18:41:47 +0000238 if (optinfo->flags & IP6T_OPTS_OPTS)
239 printf("--%s-opts ", LNAME);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000240
Stephane Ouellette703575d2003-08-23 18:41:47 +0000241 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
242
243 if (optinfo->flags & IP6T_OPTS_NSTRICT)
244 printf("--%s-not-strict ", LNAME);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000245}
246
247static
Stephane Ouellette703575d2003-08-23 18:41:47 +0000248struct ip6tables_match optstruct = {
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000249#if HOPBYHOP
Stephane Ouellette703575d2003-08-23 18:41:47 +0000250 .name = "hbh",
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000251#else
Stephane Ouellette703575d2003-08-23 18:41:47 +0000252 .name = "dst",
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000253#endif
Stephane Ouellette703575d2003-08-23 18:41:47 +0000254 .version = IPTABLES_VERSION,
255 .size = IP6T_ALIGN(sizeof(struct ip6t_opts)),
256 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_opts)),
257 .help = &help,
258 .init = &init,
259 .parse = &parse,
260 .final = &final_check,
261 .print = &print,
262 .save = &save,
263 .extra_opts = opts
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000264};
265
266void
267_init(void)
268{
269 register_match6(&optstruct);
270}