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