1 /* Shared library add-on to ip6tables to add Hop-by-Hop and Dst headers support. */
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>
17 /* Function which prints out usage message. */
18 static void hbh_help(void)
21 "hbh match options:\n"
22 " --hbh-len [!] length total length of this header\n"
23 " --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n"
24 " Options and its length (list, max: %d)\n",
28 static const struct option hbh_opts[] = {
29 { "hbh-len", 1, NULL, '1' },
30 { "hbh-opts", 1, NULL, '2' },
31 { "hbh-not-strict", 1, NULL, '3' },
36 parse_opts_num(const char *idstr, const char *typestr)
41 id = strtoul(idstr,&ep,0) ;
44 exit_error(PARAMETER_PROBLEM,
45 "hbh: no valid digits in %s `%s'", typestr, idstr);
47 if ( id == ULONG_MAX && errno == ERANGE ) {
48 exit_error(PARAMETER_PROBLEM,
49 "%s `%s' specified too big: would overflow",
52 if ( *idstr != '\0' && *ep != '\0' ) {
53 exit_error(PARAMETER_PROBLEM,
54 "hbh: error parsing %s `%s'", typestr, idstr);
56 return (u_int32_t) id;
60 parse_options(const char *optsstr, u_int16_t *opts)
62 char *buffer, *cp, *next, *range;
65 buffer = strdup(optsstr);
66 if (!buffer) exit_error(OTHER_PROBLEM, "strdup failed");
68 for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
71 if (next) *next++='\0';
72 range = strchr(cp, ':');
74 if (i == IP6T_OPTS_OPTSNR-1)
75 exit_error(PARAMETER_PROBLEM,
76 "too many ports specified");
79 opts[i] = (u_int16_t)((parse_opts_num(cp,"opt") & 0x000000FF)<<8);
82 exit_error(PARAMETER_PROBLEM, "PAD0 hasn't got length");
83 opts[i] |= (u_int16_t)(parse_opts_num(range,"length") &
90 printf("opts str: %s %s\n", cp, range);
91 printf("opts opt: %04X\n", opts[i]);
94 if (cp) exit_error(PARAMETER_PROBLEM, "too many addresses specified");
99 printf("addr nr: %d\n", i);
105 /* Initialize the match. */
106 static void hbh_init(struct xt_entry_match *m)
108 struct ip6t_opts *optinfo = (struct ip6t_opts *)m->data;
112 optinfo->invflags = 0;
116 /* Function which parses command options; returns true if it
118 static int hbh_parse(int c, char **argv, int invert, unsigned int *flags,
119 const void *entry, struct xt_entry_match **match)
121 struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data;
125 if (*flags & IP6T_OPTS_LEN)
126 exit_error(PARAMETER_PROBLEM,
127 "Only one `--hbh-len' allowed");
128 check_inverse(optarg, &invert, &optind, 0);
129 optinfo->hdrlen = parse_opts_num(argv[optind-1], "length");
131 optinfo->invflags |= IP6T_OPTS_INV_LEN;
132 optinfo->flags |= IP6T_OPTS_LEN;
133 *flags |= IP6T_OPTS_LEN;
136 if (*flags & IP6T_OPTS_OPTS)
137 exit_error(PARAMETER_PROBLEM,
138 "Only one `--hbh-opts' allowed");
139 check_inverse(optarg, &invert, &optind, 0);
141 exit_error(PARAMETER_PROBLEM,
142 " '!' not allowed with `--hbh-opts'");
143 optinfo->optsnr = parse_options(argv[optind-1], optinfo->opts);
144 optinfo->flags |= IP6T_OPTS_OPTS;
145 *flags |= IP6T_OPTS_OPTS;
148 if (*flags & IP6T_OPTS_NSTRICT)
149 exit_error(PARAMETER_PROBLEM,
150 "Only one `--hbh-not-strict' allowed");
151 if ( !(*flags & IP6T_OPTS_OPTS) )
152 exit_error(PARAMETER_PROBLEM,
153 "`--hbh-opts ...' required before `--hbh-not-strict'");
154 optinfo->flags |= IP6T_OPTS_NSTRICT;
155 *flags |= IP6T_OPTS_NSTRICT;
165 print_options(unsigned int optsnr, u_int16_t *optsp)
169 for(i=0; i<optsnr; i++){
170 printf("%d", (optsp[i] & 0xFF00)>>8);
171 if ((optsp[i] & 0x00FF) != 0x00FF){
172 printf(":%d", (optsp[i] & 0x00FF));
174 printf("%c", (i!=optsnr-1)?',':' ');
178 /* Prints out the union ip6t_matchinfo. */
179 static void hbh_print(const void *ip, const struct xt_entry_match *match,
182 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
185 if (optinfo->flags & IP6T_OPTS_LEN) {
187 printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "");
188 printf("%u", optinfo->hdrlen);
191 if (optinfo->flags & IP6T_OPTS_OPTS) printf("opts ");
192 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
193 if (optinfo->flags & IP6T_OPTS_NSTRICT) printf("not-strict ");
194 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
195 printf("Unknown invflags: 0x%X ",
196 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
199 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
200 static void hbh_save(const void *ip, const struct xt_entry_match *match)
202 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
204 if (optinfo->flags & IP6T_OPTS_LEN) {
205 printf("--hbh-len %s%u ",
206 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "! " : "",
210 if (optinfo->flags & IP6T_OPTS_OPTS)
211 printf("--hbh-opts ");
212 print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts);
213 if (optinfo->flags & IP6T_OPTS_NSTRICT)
214 printf("--hbh-not-strict ");
217 static struct xtables_match hbh_mt6_reg = {
219 .version = XTABLES_VERSION,
221 .size = XT_ALIGN(sizeof(struct ip6t_opts)),
222 .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
228 .extra_opts = hbh_opts,
234 xtables_register_match(&hbh_mt6_reg);