blob: 843395be2c0cb7c9747443b7785a326bda0092aa [file] [log] [blame]
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001/*
2 * Argument parser
3 * Copyright © Jan Engelhardt, 2011
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 */
10#include <errno.h>
11#include <getopt.h>
12#include <limits.h>
13#include <netdb.h>
14#include <stdbool.h>
15#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <arpa/inet.h>
20#include "xtables.h"
21#include "xshared.h"
22
23#define XTOPT_MKPTR(cb) \
24 ((void *)((char *)(cb)->data + (cb)->entry->ptroff))
25
26/**
27 * Creates getopt options from the x6-style option map, and assigns each a
28 * getopt id.
29 */
30struct option *
31xtables_options_xfrm(struct option *orig_opts, struct option *oldopts,
32 const struct xt_option_entry *entry, unsigned int *offset)
33{
34 unsigned int num_orig, num_old = 0, num_new, i;
35 struct option *merge, *mp;
36
37 if (entry == NULL)
38 return oldopts;
39 for (num_orig = 0; orig_opts[num_orig].name != NULL; ++num_orig)
40 ;
41 if (oldopts != NULL)
42 for (num_old = 0; oldopts[num_old].name != NULL; ++num_old)
43 ;
44 for (num_new = 0; entry[num_new].name != NULL; ++num_new)
45 ;
46
47 /*
48 * Since @oldopts also has @orig_opts already (and does so at the
49 * start), skip these entries.
50 */
51 oldopts += num_orig;
52 num_old -= num_orig;
53
54 merge = malloc(sizeof(*mp) * (num_orig + num_old + num_new + 1));
55 if (merge == NULL)
56 return NULL;
57
58 /* Let the base options -[ADI...] have precedence over everything */
59 memcpy(merge, orig_opts, sizeof(*mp) * num_orig);
60 mp = merge + num_orig;
61
62 /* Second, the new options */
63 xt_params->option_offset += XT_OPTION_OFFSET_SCALE;
64 *offset = xt_params->option_offset;
65
66 for (i = 0; i < num_new; ++i, ++mp, ++entry) {
67 mp->name = entry->name;
68 mp->has_arg = entry->type != XTTYPE_NONE;
69 mp->flag = NULL;
70 mp->val = entry->id + *offset;
71 }
72
73 /* Third, the old options */
74 memcpy(mp, oldopts, sizeof(*mp) * num_old);
75 mp += num_old;
76 xtables_free_opts(0);
77
78 /* Clear trailing entry */
79 memset(mp, 0, sizeof(*mp));
80 return merge;
81}
82
Jan Engelhardta93142d2011-02-16 01:22:25 +010083/**
84 * Require a simple integer.
85 */
86static void xtopt_parse_int(struct xt_option_call *cb)
87{
88 const struct xt_option_entry *entry = cb->entry;
89 unsigned int lmin = 0, lmax = UINT32_MAX;
90 unsigned int value;
91
92 if (!xtables_strtoui(cb->arg, NULL, &value, lmin, lmax))
93 xt_params->exit_err(PARAMETER_PROBLEM,
94 "%s: bad value for option \"--%s\", "
95 "or out of range (%u-%u).\n",
96 cb->ext_name, entry->name, lmin, lmax);
97
98 if (entry->type == XTTYPE_UINT32) {
99 cb->val.u32 = value;
100 if (entry->flags & XTOPT_PUT)
101 *(uint32_t *)XTOPT_MKPTR(cb) = cb->val.u32;
102 }
103}
104
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100105static void (*const xtopt_subparse[])(struct xt_option_call *) = {
Jan Engelhardta93142d2011-02-16 01:22:25 +0100106 [XTTYPE_UINT32] = xtopt_parse_int,
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100107};
108
109static const size_t xtopt_psize[] = {
Jan Engelhardta93142d2011-02-16 01:22:25 +0100110 [XTTYPE_UINT32] = sizeof(uint32_t),
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100111};
112
113/**
114 * The master option parsing routine. May be used for the ".x6_parse"
115 * function pointer in extensions if fully automatic parsing is desired.
116 * It may be also called manually from a custom x6_parse function.
117 */
118void xtables_option_parse(struct xt_option_call *cb)
119{
120 const struct xt_option_entry *entry = cb->entry;
121 unsigned int eflag = 1 << cb->entry->id;
122
123 /*
124 * With {.id = P_FOO, .excl = P_FOO} we can have simple double-use
125 * prevention. Though it turned out that this is too much typing (most
126 * of the options are one-time use only), so now we also have
127 * %XTOPT_MULTI.
128 */
129 if ((!(entry->flags & XTOPT_MULTI) || (entry->excl & eflag)) &&
130 cb->xflags & eflag)
131 xt_params->exit_err(PARAMETER_PROBLEM,
132 "%s: option \"--%s\" can only be used once.\n",
133 cb->ext_name, cb->entry->name);
134 if (cb->invert && !(entry->flags & XTOPT_INVERT))
135 xt_params->exit_err(PARAMETER_PROBLEM,
136 "%s: option \"--%s\" cannot be inverted.\n",
137 cb->ext_name, entry->name);
138 if (entry->type != XTTYPE_NONE && optarg == NULL)
139 xt_params->exit_err(PARAMETER_PROBLEM,
140 "%s: option \"--%s\" requires an argument.\n",
141 cb->ext_name, entry->name);
142 if (entry->type <= ARRAY_SIZE(xtopt_subparse) &&
143 xtopt_subparse[entry->type] != NULL)
144 xtopt_subparse[entry->type](cb);
145 /* Exclusion with other flags tested later in finalize. */
146 cb->xflags |= 1 << entry->id;
147}
148
149/**
150 * Verifies that an extension's option map descriptor is valid, and ought to
151 * be called right after the extension has been loaded, and before option
152 * merging/xfrm.
153 */
154void xtables_option_metavalidate(const char *name,
155 const struct xt_option_entry *entry)
156{
157 for (; entry->name != NULL; ++entry) {
158 if (entry->id >= CHAR_BIT * sizeof(unsigned int) ||
159 entry->id >= XT_OPTION_OFFSET_SCALE)
160 xt_params->exit_err(OTHER_PROBLEM,
161 "Extension %s uses invalid ID %u\n",
162 name, entry->id);
163 if (!(entry->flags & XTOPT_PUT))
164 continue;
165 if (entry->type >= ARRAY_SIZE(xtopt_psize))
166 xt_params->exit_err(OTHER_PROBLEM,
167 "%s: entry type of option \"--%s\" cannot be "
168 "combined with XTOPT_PUT\n",
169 name, entry->name);
170 if (xtopt_psize[entry->type] != entry->size)
171 xt_params->exit_err(OTHER_PROBLEM,
172 "%s: option \"--%s\" points to a memory block "
173 "of wrong size (expected %zu, got %zu)\n",
174 name, entry->name,
175 xtopt_psize[entry->type], entry->size);
176 }
177}
178
179/**
180 * Find an option entry by its id.
181 */
182static const struct xt_option_entry *
183xtables_option_lookup(const struct xt_option_entry *entry, unsigned int id)
184{
185 for (; entry->name != NULL; ++entry)
186 if (entry->id == id)
187 return entry;
188 return NULL;
189}
190
191/**
192 * @c: getopt id (i.e. with offset)
193 * @fw: struct ipt_entry or ip6t_entry
194 *
195 * Dispatch arguments to the appropriate parse function, based upon the
196 * extension's choice of API.
197 */
198void xtables_option_tpcall(unsigned int c, char **argv, bool invert,
199 struct xtables_target *t, void *fw)
200{
201 struct xt_option_call cb;
202
203 if (t->x6_parse == NULL) {
204 if (t->parse != NULL)
205 t->parse(c - t->option_offset, argv, invert,
206 &t->tflags, fw, &t->t);
207 return;
208 }
209
210 c -= t->option_offset;
211 cb.entry = xtables_option_lookup(t->x6_options, c);
212 if (cb.entry == NULL)
213 xtables_error(OTHER_PROBLEM,
214 "Extension does not know id %u\n", c);
215 cb.arg = optarg;
216 cb.invert = invert;
217 cb.ext_name = t->name;
218 cb.data = t->t->data;
219 cb.xflags = t->tflags;
220 t->x6_parse(&cb);
221 t->tflags = cb.xflags;
222}
223
224/**
225 * @c: getopt id (i.e. with offset)
226 * @fw: struct ipt_entry or ip6t_entry
227 *
228 * Dispatch arguments to the appropriate parse function, based upon the
229 * extension's choice of API.
230 */
231void xtables_option_mpcall(unsigned int c, char **argv, bool invert,
232 struct xtables_match *m, void *fw)
233{
234 struct xt_option_call cb;
235
236 if (m->x6_parse == NULL) {
237 if (m->parse != NULL)
238 m->parse(c - m->option_offset, argv, invert,
239 &m->mflags, fw, &m->m);
240 return;
241 }
242
243 c -= m->option_offset;
244 cb.entry = xtables_option_lookup(m->x6_options, c);
245 if (cb.entry == NULL)
246 xtables_error(OTHER_PROBLEM,
247 "Extension does not know id %u\n", c);
248 cb.arg = optarg;
249 cb.invert = invert;
250 cb.ext_name = m->name;
251 cb.data = m->m->data;
252 cb.xflags = m->mflags;
253 m->x6_parse(&cb);
254 m->mflags = cb.xflags;
255}
256
257/**
258 * @name: name of extension
259 * @entry: current option (from all ext's entries) being validated
260 * @xflags: flags the extension has collected
261 * @i: conflicting option (id) to test for
262 */
263static void
264xtables_option_fcheck2(const char *name, const struct xt_option_entry *entry,
265 const struct xt_option_entry *other,
266 unsigned int xflags)
267{
268 unsigned int ef = 1 << entry->id, of = 1 << other->id;
269
270 if (entry->also & of && !(xflags & of))
271 xt_params->exit_err(PARAMETER_PROBLEM,
272 "%s: option \"--%s\" also requires \"--%s\".\n",
273 name, entry->name, other->name);
274
275 if (!(entry->excl & of))
276 /* Use of entry does not collide with other option, good. */
277 return;
278 if ((xflags & (ef | of)) != (ef | of))
279 /* Conflicting options were not used. */
280 return;
281
282 xt_params->exit_err(PARAMETER_PROBLEM,
283 "%s: option \"--%s\" cannot be used together with \"--%s\".\n",
284 name, entry->name, other->name);
285}
286
287/**
288 * @name: name of extension
289 * @xflags: accumulated flags
290 * @entry: extension's option table
291 *
292 * Check that all option constraints have been met. This effectively replaces
293 * ->final_check of the older API.
294 */
295void xtables_options_fcheck(const char *name, unsigned int xflags,
296 const struct xt_option_entry *table)
297{
298 const struct xt_option_entry *entry, *other;
299 unsigned int i;
300
301 for (entry = table; entry->name != NULL; ++entry) {
302 if (entry->flags & XTOPT_MAND &&
303 !(xflags & (1 << entry->id)))
304 xt_params->exit_err(PARAMETER_PROBLEM,
305 "%s: option \"--%s\" must be specified\n",
306 name, entry->name);
307
308 for (i = 0; i < CHAR_BIT * sizeof(entry->id); ++i) {
309 if (entry->id == i)
310 /*
311 * Avoid conflict with self. Multi-use check
312 * was done earlier in xtables_option_parse.
313 */
314 continue;
315 other = xtables_option_lookup(table, i);
316 if (other == NULL)
317 continue;
318 xtables_option_fcheck2(name, entry, other, xflags);
319 }
320 }
321}
Jan Engelhardt3af739b2011-02-10 16:57:37 +0100322
323/**
324 * Dispatch arguments to the appropriate final_check function, based upon the
325 * extension's choice of API.
326 */
327void xtables_option_tfcall(struct xtables_target *t)
328{
329 if (t->x6_fcheck != NULL) {
330 struct xt_fcheck_call cb;
331
332 cb.ext_name = t->name;
333 cb.data = t->t->data;
334 cb.xflags = t->tflags;
335 t->x6_fcheck(&cb);
336 } else if (t->final_check != NULL) {
337 t->final_check(t->tflags);
338 }
339 if (t->x6_options != NULL)
340 xtables_options_fcheck(t->name, t->tflags, t->x6_options);
341}
342
343/**
344 * Dispatch arguments to the appropriate final_check function, based upon the
345 * extension's choice of API.
346 */
347void xtables_option_mfcall(struct xtables_match *m)
348{
349 if (m->x6_fcheck != NULL) {
350 struct xt_fcheck_call cb;
351
352 cb.ext_name = m->name;
353 cb.data = m->m->data;
354 cb.xflags = m->mflags;
355 m->x6_fcheck(&cb);
356 } else if (m->final_check != NULL) {
357 m->final_check(m->mflags);
358 }
359 if (m->x6_options != NULL)
360 xtables_options_fcheck(m->name, m->mflags, m->x6_options);
361}