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 = strdup(optsstr);
62 if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
63
64 for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
65 {
66 next=strchr(cp, ',');
67 if (next) *next++='\0';
68 range = strchr(cp, ':');
69 if (range) {
70 if (i == IP6T_OPTS_OPTSNR-1)
71 xtables_error(PARAMETER_PROBLEM,
72 "too many ports specified");
73 *range++ = '\0';
74 }
75 opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
76 if (range) {
77 if (opts[i] == 0)
78 xtables_error(PARAMETER_PROBLEM, "PAD0 has not got length");
79 opts[i] |= parse_opts_num(range, "length") & 0xFF;
80 } else {
81 opts[i] |= (0x00FF);
82 }
83
84 #ifdef DEBUG
85 printf("opts str: %s %s\n", cp, range);
86 printf("opts opt: %04X\n", opts[i]);
87 #endif
88 }
89 if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
90
91 free(buffer);
92
93 #ifdef DEBUG
94 printf("addr nr: %d\n", i);
95 #endif
96
97 return i;
98 }
99
hbh_parse(struct xt_option_call * cb)100 static void hbh_parse(struct xt_option_call *cb)
101 {
102 struct ip6t_opts *optinfo = cb->data;
103
104 xtables_option_parse(cb);
105 switch (cb->entry->id) {
106 case O_HBH_LEN:
107 if (cb->invert)
108 optinfo->invflags |= IP6T_OPTS_INV_LEN;
109 optinfo->flags |= IP6T_OPTS_LEN;
110 break;
111 case O_HBH_OPTS:
112 optinfo->optsnr = parse_options(cb->arg, optinfo->opts);
113 optinfo->flags |= IP6T_OPTS_OPTS;
114 break;
115 }
116 }
117
118 static void
print_options(unsigned int optsnr,uint16_t * optsp)119 print_options(unsigned int optsnr, uint16_t *optsp)
120 {
121 unsigned int i;
122
123 for(i=0; i<optsnr; i++){
124 printf("%c", (i==0)?' ':',');
125 printf("%d", (optsp[i] & 0xFF00)>>8);
126 if ((optsp[i] & 0x00FF) != 0x00FF){
127 printf(":%d", (optsp[i] & 0x00FF));
128 }
129 }
130 }
131
hbh_print(const void * ip,const struct xt_entry_match * match,int numeric)132 static void hbh_print(const void *ip, const struct xt_entry_match *match,
133 int numeric)
134 {
135 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
136
137 printf(" hbh");
138 if (optinfo->flags & IP6T_OPTS_LEN) {
139 printf(" length");
140 printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "");
141 printf("%u", optinfo->hdrlen);
142 }
143 if (optinfo->flags & IP6T_OPTS_OPTS) printf(" opts");
144 print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
145 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
146 printf(" Unknown invflags: 0x%X",
147 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
148 }
149
hbh_save(const void * ip,const struct xt_entry_match * match)150 static void hbh_save(const void *ip, const struct xt_entry_match *match)
151 {
152 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
153
154 if (optinfo->flags & IP6T_OPTS_LEN) {
155 printf("%s --hbh-len %u",
156 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "",
157 optinfo->hdrlen);
158 }
159
160 if (optinfo->flags & IP6T_OPTS_OPTS)
161 printf(" --hbh-opts");
162 print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
163 }
164
hbh_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)165 static int hbh_xlate(struct xt_xlate *xl,
166 const struct xt_xlate_mt_params *params)
167 {
168 const struct ip6t_opts *optinfo =
169 (struct ip6t_opts *)params->match->data;
170
171 if (!(optinfo->flags & IP6T_OPTS_LEN) ||
172 (optinfo->flags & IP6T_OPTS_OPTS))
173 return 0;
174
175 xt_xlate_add(xl, "hbh hdrlength %s%u",
176 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "!= " : "",
177 optinfo->hdrlen);
178
179 return 1;
180 }
181
182 static struct xtables_match hbh_mt6_reg = {
183 .name = "hbh",
184 .version = XTABLES_VERSION,
185 .family = NFPROTO_IPV6,
186 .size = XT_ALIGN(sizeof(struct ip6t_opts)),
187 .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
188 .help = hbh_help,
189 .print = hbh_print,
190 .save = hbh_save,
191 .x6_parse = hbh_parse,
192 .x6_options = hbh_opts,
193 .xlate = hbh_xlate,
194 };
195
196 void
_init(void)197 _init(void)
198 {
199 xtables_register_match(&hbh_mt6_reg);
200 }
201