blob: 43562c17b4506a04cd5a4b0c42a6c5ada815d8da [file] [log] [blame]
Jan Engelhardtddac6c52008-09-01 14:22:19 +02001/* Shared library add-on to ip6tables to add Dst header support. */
András Kis-Szabó2ea56492002-04-29 13:51:37 +00002#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <errno.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01008#include <xtables.h>
András Kis-Szabó2ea56492002-04-29 13:51:37 +00009#include <linux/netfilter_ipv6/ip6t_opts.h>
10#include <sys/types.h>
11#include <sys/socket.h>
12#include <arpa/inet.h>
Harald Weltead8d1ab2003-09-04 21:55:10 +000013
Jan Engelhardt997045f2007-10-04 16:29:21 +000014static void dst_help(void)
András Kis-Szabó2ea56492002-04-29 13:51:37 +000015{
16 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020017"dst match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020018"[!] --dst-len length total length of this header\n"
Jan Engelhardte2f588a2007-10-04 16:30:40 +000019" --dst-opts TYPE[:LEN][,TYPE[:LEN]...]\n"
20" Options and its length (list, max: %d)\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020021IP6T_OPTS_OPTSNR);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000022}
23
Jan Engelhardt997045f2007-10-04 16:29:21 +000024static const struct option dst_opts[] = {
Jan Engelhardte2f588a2007-10-04 16:30:40 +000025 { .name = "dst-len", .has_arg = 1, .val = '1' },
26 { .name = "dst-opts", .has_arg = 1, .val = '2' },
27 { .name = "dst-not-strict", .has_arg = 1, .val = '3' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000028 { .name = NULL }
András Kis-Szabó2ea56492002-04-29 13:51:37 +000029};
András Kis-Szabó2ea56492002-04-29 13:51:37 +000030
31static u_int32_t
32parse_opts_num(const char *idstr, const char *typestr)
33{
34 unsigned long int id;
35 char* ep;
36
Stephane Ouellette703575d2003-08-23 18:41:47 +000037 id = strtoul(idstr, &ep, 0);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000038
39 if ( idstr == ep ) {
40 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +000041 "dst: no valid digits in %s `%s'", typestr, idstr);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000042 }
43 if ( id == ULONG_MAX && errno == ERANGE ) {
44 exit_error(PARAMETER_PROBLEM,
45 "%s `%s' specified too big: would overflow",
46 typestr, idstr);
Harald Weltead8d1ab2003-09-04 21:55:10 +000047 }
András Kis-Szabó2ea56492002-04-29 13:51:37 +000048 if ( *idstr != '\0' && *ep != '\0' ) {
49 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +000050 "dst: error parsing %s `%s'", typestr, idstr);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000051 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010052 return id;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000053}
54
55static int
56parse_options(const char *optsstr, u_int16_t *opts)
57{
58 char *buffer, *cp, *next, *range;
59 unsigned int i;
60
61 buffer = strdup(optsstr);
Harald Weltead8d1ab2003-09-04 21:55:10 +000062 if (!buffer)
63 exit_error(OTHER_PROBLEM, "strdup failed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +000064
Harald Weltead8d1ab2003-09-04 21:55:10 +000065 for (cp = buffer, i = 0; cp && i < IP6T_OPTS_OPTSNR; cp = next, i++)
András Kis-Szabó2ea56492002-04-29 13:51:37 +000066 {
Harald Weltead8d1ab2003-09-04 21:55:10 +000067 next = strchr(cp, ',');
68
69 if (next)
70 *next++='\0';
71
András Kis-Szabó2ea56492002-04-29 13:51:37 +000072 range = strchr(cp, ':');
Harald Weltead8d1ab2003-09-04 21:55:10 +000073
András Kis-Szabó2ea56492002-04-29 13:51:37 +000074 if (range) {
75 if (i == IP6T_OPTS_OPTSNR-1)
76 exit_error(PARAMETER_PROBLEM,
77 "too many ports specified");
78 *range++ = '\0';
79 }
Harald Weltead8d1ab2003-09-04 21:55:10 +000080
Jan Engelhardt213e1852009-01-27 17:24:34 +010081 opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000082 if (range) {
83 if (opts[i] == 0)
Harald Weltead8d1ab2003-09-04 21:55:10 +000084 exit_error(PARAMETER_PROBLEM,
85 "PAD0 hasn't got length");
Jan Engelhardt213e1852009-01-27 17:24:34 +010086 opts[i] |= parse_opts_num(range, "length") & 0xFF;
Harald Weltead8d1ab2003-09-04 21:55:10 +000087 } else
András Kis-Szabó2ea56492002-04-29 13:51:37 +000088 opts[i] |= (0x00FF);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000089
Harald Weltead8d1ab2003-09-04 21:55:10 +000090#ifdef DEBUG
András Kis-Szabó2ea56492002-04-29 13:51:37 +000091 printf("opts str: %s %s\n", cp, range);
92 printf("opts opt: %04X\n", opts[i]);
93#endif
94 }
Harald Weltead8d1ab2003-09-04 21:55:10 +000095
96 if (cp)
97 exit_error(PARAMETER_PROBLEM, "too many addresses specified");
András Kis-Szabó2ea56492002-04-29 13:51:37 +000098
99 free(buffer);
100
Harald Weltead8d1ab2003-09-04 21:55:10 +0000101#ifdef DEBUG
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000102 printf("addr nr: %d\n", i);
103#endif
104
105 return i;
106}
107
Jan Engelhardt997045f2007-10-04 16:29:21 +0000108static void dst_init(struct xt_entry_match *m)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000109{
110 struct ip6t_opts *optinfo = (struct ip6t_opts *)m->data;
111
112 optinfo->hdrlen = 0;
113 optinfo->flags = 0;
114 optinfo->invflags = 0;
115 optinfo->optsnr = 0;
116}
117
Jan Engelhardt997045f2007-10-04 16:29:21 +0000118static int dst_parse(int c, char **argv, int invert, unsigned int *flags,
119 const void *entry, struct xt_entry_match **match)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000120{
121 struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data;
122
123 switch (c) {
124 case '1':
125 if (*flags & IP6T_OPTS_LEN)
126 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000127 "Only one `--dst-len' allowed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000128 check_inverse(optarg, &invert, &optind, 0);
129 optinfo->hdrlen = parse_opts_num(argv[optind-1], "length");
130 if (invert)
131 optinfo->invflags |= IP6T_OPTS_INV_LEN;
132 optinfo->flags |= IP6T_OPTS_LEN;
133 *flags |= IP6T_OPTS_LEN;
134 break;
135 case '2':
136 if (*flags & IP6T_OPTS_OPTS)
137 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000138 "Only one `--dst-opts' allowed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000139 check_inverse(optarg, &invert, &optind, 0);
140 if (invert)
141 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000142 " '!' not allowed with `--dst-opts'");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000143 optinfo->optsnr = parse_options(argv[optind-1], optinfo->opts);
144 optinfo->flags |= IP6T_OPTS_OPTS;
145 *flags |= IP6T_OPTS_OPTS;
146 break;
147 case '3':
148 if (*flags & IP6T_OPTS_NSTRICT)
149 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000150 "Only one `--dst-not-strict' allowed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000151 if ( !(*flags & IP6T_OPTS_OPTS) )
152 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000153 "`--dst-opts ...' required before "
154 "`--dst-not-strict'");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000155 optinfo->flags |= IP6T_OPTS_NSTRICT;
156 *flags |= IP6T_OPTS_NSTRICT;
157 break;
158 default:
159 return 0;
160 }
161
162 return 1;
163}
164
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000165static void
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100166print_options(unsigned int optsnr, u_int16_t *optsp)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000167{
168 unsigned int i;
169
Harald Weltead8d1ab2003-09-04 21:55:10 +0000170 for(i = 0; i < optsnr; i++) {
171 printf("%d", (optsp[i] & 0xFF00) >> 8);
172
173 if ((optsp[i] & 0x00FF) != 0x00FF)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000174 printf(":%d", (optsp[i] & 0x00FF));
Harald Weltead8d1ab2003-09-04 21:55:10 +0000175
176 printf("%c", (i != optsnr - 1) ? ',' : ' ');
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000177 }
178}
179
Jan Engelhardt997045f2007-10-04 16:29:21 +0000180static void dst_print(const void *ip, const struct xt_entry_match *match,
181 int numeric)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000182{
183 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
184
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000185 printf("dst ");
Stephane Ouellette703575d2003-08-23 18:41:47 +0000186 if (optinfo->flags & IP6T_OPTS_LEN)
187 printf("length:%s%u ",
188 optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "",
189 optinfo->hdrlen);
190
191 if (optinfo->flags & IP6T_OPTS_OPTS)
192 printf("opts ");
193
Fabrice MARIEae31bb62002-06-14 07:38:16 +0000194 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
Stephane Ouellette703575d2003-08-23 18:41:47 +0000195
196 if (optinfo->flags & IP6T_OPTS_NSTRICT)
197 printf("not-strict ");
198
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000199 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
200 printf("Unknown invflags: 0x%X ",
201 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
202}
203
Jan Engelhardt997045f2007-10-04 16:29:21 +0000204static void dst_save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000205{
206 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
207
208 if (optinfo->flags & IP6T_OPTS_LEN) {
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100209 printf("%s--dst-len %u ",
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000210 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "! " : "",
211 optinfo->hdrlen);
212 }
213
Stephane Ouellette703575d2003-08-23 18:41:47 +0000214 if (optinfo->flags & IP6T_OPTS_OPTS)
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000215 printf("--dst-opts ");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000216
Stephane Ouellette703575d2003-08-23 18:41:47 +0000217 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
218
219 if (optinfo->flags & IP6T_OPTS_NSTRICT)
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000220 printf("--dst-not-strict ");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000221}
222
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200223static struct xtables_match dst_mt6_reg = {
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000224 .name = "dst",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200225 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100226 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200227 .size = XT_ALIGN(sizeof(struct ip6t_opts)),
228 .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000229 .help = dst_help,
230 .init = dst_init,
231 .parse = dst_parse,
232 .print = dst_print,
233 .save = dst_save,
234 .extra_opts = dst_opts,
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000235};
236
237void
238_init(void)
239{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200240 xtables_register_match(&dst_mt6_reg);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000241}