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_DSTLEN = 0,
10 O_DSTOPTS,
11 };
12
dst_help(void)13 static void dst_help(void)
14 {
15 printf(
16 "dst match options:\n"
17 "[!] --dst-len length total length of this header\n"
18 " --dst-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 dst_opts[] = {
24 {.name = "dst-len", .id = O_DSTLEN, .type = XTTYPE_UINT32,
25 .flags = XTOPT_INVERT | XTOPT_PUT,
26 XTOPT_POINTER(struct ip6t_opts, hdrlen)},
27 {.name = "dst-opts", .id = O_DSTOPTS, .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 "dst: 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 "dst: 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
67 if (next)
68 *next++='\0';
69
70 range = strchr(cp, ':');
71
72 if (range) {
73 if (i == IP6T_OPTS_OPTSNR-1)
74 xtables_error(PARAMETER_PROBLEM,
75 "too many ports specified");
76 *range++ = '\0';
77 }
78
79 opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
80 if (range) {
81 if (opts[i] == 0)
82 xtables_error(PARAMETER_PROBLEM,
83 "PAD0 hasn't got length");
84 opts[i] |= parse_opts_num(range, "length") & 0xFF;
85 } else
86 opts[i] |= (0x00FF);
87
88 #ifdef DEBUG
89 printf("opts str: %s %s\n", cp, range);
90 printf("opts opt: %04X\n", opts[i]);
91 #endif
92 }
93
94 if (cp)
95 xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
96
97 free(buffer);
98
99 #ifdef DEBUG
100 printf("addr nr: %d\n", i);
101 #endif
102
103 return i;
104 }
105
dst_parse(struct xt_option_call * cb)106 static void dst_parse(struct xt_option_call *cb)
107 {
108 struct ip6t_opts *optinfo = cb->data;
109
110 xtables_option_parse(cb);
111 switch (cb->entry->id) {
112 case O_DSTLEN:
113 if (cb->invert)
114 optinfo->invflags |= IP6T_OPTS_INV_LEN;
115 optinfo->flags |= IP6T_OPTS_LEN;
116 break;
117 case O_DSTOPTS:
118 optinfo->optsnr = parse_options(cb->arg, optinfo->opts);
119 optinfo->flags |= IP6T_OPTS_OPTS;
120 break;
121 }
122 }
123
124 static void
print_options(unsigned int optsnr,uint16_t * optsp)125 print_options(unsigned int optsnr, uint16_t *optsp)
126 {
127 unsigned int i;
128 char sep = ' ';
129
130 for(i = 0; i < optsnr; i++) {
131 printf("%c%d", sep, (optsp[i] & 0xFF00) >> 8);
132
133 if ((optsp[i] & 0x00FF) != 0x00FF)
134 printf(":%d", (optsp[i] & 0x00FF));
135
136 sep = ',';
137 }
138 }
139
dst_print(const void * ip,const struct xt_entry_match * match,int numeric)140 static void dst_print(const void *ip, const struct xt_entry_match *match,
141 int numeric)
142 {
143 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
144
145 printf(" dst");
146 if (optinfo->flags & IP6T_OPTS_LEN)
147 printf(" length:%s%u",
148 optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "",
149 optinfo->hdrlen);
150
151 if (optinfo->flags & IP6T_OPTS_OPTS)
152 printf(" opts");
153
154 print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
155
156 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
157 printf(" Unknown invflags: 0x%X",
158 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
159 }
160
dst_save(const void * ip,const struct xt_entry_match * match)161 static void dst_save(const void *ip, const struct xt_entry_match *match)
162 {
163 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
164
165 if (optinfo->flags & IP6T_OPTS_LEN) {
166 printf("%s --dst-len %u",
167 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "",
168 optinfo->hdrlen);
169 }
170
171 if (optinfo->flags & IP6T_OPTS_OPTS)
172 printf(" --dst-opts");
173
174 print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
175 }
176
177 static struct xtables_match dst_mt6_reg = {
178 .name = "dst",
179 .version = XTABLES_VERSION,
180 .family = NFPROTO_IPV6,
181 .size = XT_ALIGN(sizeof(struct ip6t_opts)),
182 .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
183 .help = dst_help,
184 .print = dst_print,
185 .save = dst_save,
186 .x6_parse = dst_parse,
187 .x6_options = dst_opts,
188 };
189
190 void
_init(void)191 _init(void)
192 {
193 xtables_register_match(&dst_mt6_reg);
194 }
195