blob: 6c7458d8f63b20a83400eaef14b50684b46620ea [file] [log] [blame]
Jan Engelhardtddac6c52008-09-01 14:22:19 +02001/* Shared library add-on to ip6tables to add Hop-by-Hop 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/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>
Jan Engelhardtddac6c52008-09-01 14:22:19 +020014
András Kis-Szabó2ea56492002-04-29 13:51:37 +000015#define DEBUG 0
András Kis-Szabó2ea56492002-04-29 13:51:37 +000016
Jan Engelhardt997045f2007-10-04 16:29:21 +000017static void hbh_help(void)
András Kis-Szabó2ea56492002-04-29 13:51:37 +000018{
19 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020020"hbh match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020021"[!] --hbh-len length total length of this header\n"
Jan Engelhardte2f588a2007-10-04 16:30:40 +000022" --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n"
23" Options and its length (list, max: %d)\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020024IP6T_OPTS_OPTSNR);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000025}
26
Jan Engelhardt997045f2007-10-04 16:29:21 +000027static const struct option hbh_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000028 { "hbh-len", 1, NULL, '1' },
29 { "hbh-opts", 1, NULL, '2' },
30 { "hbh-not-strict", 1, NULL, '3' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000031 { .name = NULL }
András Kis-Szabó2ea56492002-04-29 13:51:37 +000032};
András Kis-Szabó2ea56492002-04-29 13:51:37 +000033
34static u_int32_t
35parse_opts_num(const char *idstr, const char *typestr)
36{
37 unsigned long int id;
38 char* ep;
39
40 id = strtoul(idstr,&ep,0) ;
41
42 if ( idstr == ep ) {
43 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +000044 "hbh: no valid digits in %s `%s'", typestr, idstr);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000045 }
46 if ( id == ULONG_MAX && errno == ERANGE ) {
47 exit_error(PARAMETER_PROBLEM,
48 "%s `%s' specified too big: would overflow",
49 typestr, idstr);
50 }
51 if ( *idstr != '\0' && *ep != '\0' ) {
52 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +000053 "hbh: error parsing %s `%s'", typestr, idstr);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000054 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010055 return id;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000056}
57
58static int
59parse_options(const char *optsstr, u_int16_t *opts)
60{
61 char *buffer, *cp, *next, *range;
62 unsigned int i;
63
64 buffer = strdup(optsstr);
65 if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed");
66
67 for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
68 {
69 next=strchr(cp, ',');
70 if (next) *next++='\0';
71 range = strchr(cp, ':');
72 if (range) {
73 if (i == IP6T_OPTS_OPTSNR-1)
74 exit_error(PARAMETER_PROBLEM,
75 "too many ports specified");
76 *range++ = '\0';
77 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010078 opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000079 if (range) {
80 if (opts[i] == 0)
81 exit_error(PARAMETER_PROBLEM, "PAD0 hasn't got length");
Jan Engelhardt213e1852009-01-27 17:24:34 +010082 opts[i] |= parse_opts_num(range, "length") & 0xFF;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000083 } else {
84 opts[i] |= (0x00FF);
85 }
86
87#if DEBUG
88 printf("opts str: %s %s\n", cp, range);
89 printf("opts opt: %04X\n", opts[i]);
90#endif
91 }
92 if (cp) exit_error(PARAMETER_PROBLEM, "too many addresses specified");
93
94 free(buffer);
95
96#if DEBUG
97 printf("addr nr: %d\n", i);
98#endif
99
100 return i;
101}
102
Jan Engelhardt997045f2007-10-04 16:29:21 +0000103static void hbh_init(struct xt_entry_match *m)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000104{
105 struct ip6t_opts *optinfo = (struct ip6t_opts *)m->data;
106
107 optinfo->hdrlen = 0;
108 optinfo->flags = 0;
109 optinfo->invflags = 0;
110 optinfo->optsnr = 0;
111}
112
Jan Engelhardt997045f2007-10-04 16:29:21 +0000113static int hbh_parse(int c, char **argv, int invert, unsigned int *flags,
114 const void *entry, struct xt_entry_match **match)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000115{
116 struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data;
117
118 switch (c) {
119 case '1':
120 if (*flags & IP6T_OPTS_LEN)
121 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000122 "Only one `--hbh-len' allowed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000123 check_inverse(optarg, &invert, &optind, 0);
124 optinfo->hdrlen = parse_opts_num(argv[optind-1], "length");
125 if (invert)
126 optinfo->invflags |= IP6T_OPTS_INV_LEN;
127 optinfo->flags |= IP6T_OPTS_LEN;
128 *flags |= IP6T_OPTS_LEN;
129 break;
130 case '2':
131 if (*flags & IP6T_OPTS_OPTS)
132 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000133 "Only one `--hbh-opts' allowed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000134 check_inverse(optarg, &invert, &optind, 0);
135 if (invert)
136 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000137 " '!' not allowed with `--hbh-opts'");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000138 optinfo->optsnr = parse_options(argv[optind-1], optinfo->opts);
139 optinfo->flags |= IP6T_OPTS_OPTS;
140 *flags |= IP6T_OPTS_OPTS;
141 break;
142 case '3':
143 if (*flags & IP6T_OPTS_NSTRICT)
144 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000145 "Only one `--hbh-not-strict' allowed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000146 if ( !(*flags & IP6T_OPTS_OPTS) )
147 exit_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000148 "`--hbh-opts ...' required before `--hbh-not-strict'");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000149 optinfo->flags |= IP6T_OPTS_NSTRICT;
150 *flags |= IP6T_OPTS_NSTRICT;
151 break;
152 default:
153 return 0;
154 }
155
156 return 1;
157}
158
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000159static void
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100160print_options(unsigned int optsnr, u_int16_t *optsp)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000161{
162 unsigned int i;
163
164 for(i=0; i<optsnr; i++){
165 printf("%d", (optsp[i] & 0xFF00)>>8);
166 if ((optsp[i] & 0x00FF) != 0x00FF){
167 printf(":%d", (optsp[i] & 0x00FF));
168 }
169 printf("%c", (i!=optsnr-1)?',':' ');
170 }
171}
172
Jan Engelhardt997045f2007-10-04 16:29:21 +0000173static void hbh_print(const void *ip, const struct xt_entry_match *match,
174 int numeric)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000175{
176 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
177
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000178 printf("hbh ");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000179 if (optinfo->flags & IP6T_OPTS_LEN) {
180 printf("length");
181 printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "");
182 printf("%u", optinfo->hdrlen);
183 printf(" ");
184 }
185 if (optinfo->flags & IP6T_OPTS_OPTS) printf("opts ");
Fabrice MARIEae31bb62002-06-14 07:38:16 +0000186 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000187 if (optinfo->flags & IP6T_OPTS_NSTRICT) printf("not-strict ");
188 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
189 printf("Unknown invflags: 0x%X ",
190 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
191}
192
Jan Engelhardt997045f2007-10-04 16:29:21 +0000193static void hbh_save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000194{
195 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
196
197 if (optinfo->flags & IP6T_OPTS_LEN) {
Jan Engelhardtcea9f712008-12-09 15:06:20 +0100198 printf("%s--hbh-len %u ",
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000199 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "! " : "",
200 optinfo->hdrlen);
201 }
202
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000203 if (optinfo->flags & IP6T_OPTS_OPTS)
204 printf("--hbh-opts ");
Fabrice MARIEae31bb62002-06-14 07:38:16 +0000205 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000206 if (optinfo->flags & IP6T_OPTS_NSTRICT)
207 printf("--hbh-not-strict ");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000208}
209
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200210static struct xtables_match hbh_mt6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000211 .name = "hbh",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200212 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100213 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200214 .size = XT_ALIGN(sizeof(struct ip6t_opts)),
215 .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000216 .help = hbh_help,
217 .init = hbh_init,
218 .parse = hbh_parse,
219 .print = hbh_print,
220 .save = hbh_save,
221 .extra_opts = hbh_opts,
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000222};
223
224void
225_init(void)
226{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200227 xtables_register_match(&hbh_mt6_reg);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000228}