blob: 693c06d65ec61af89ef472346c29002ca8d66793 [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
Jan Engelhardtdfe99f12011-02-27 19:03:28 +010092 if (entry->type == XTTYPE_UINT8)
93 lmax = UINT8_MAX;
Jan Engelhardtd78254d2011-02-27 17:38:34 +010094 if (cb->entry->min != 0)
95 lmin = cb->entry->min;
96 if (cb->entry->max != 0)
97 lmax = cb->entry->max;
98
Jan Engelhardta93142d2011-02-16 01:22:25 +010099 if (!xtables_strtoui(cb->arg, NULL, &value, lmin, lmax))
100 xt_params->exit_err(PARAMETER_PROBLEM,
101 "%s: bad value for option \"--%s\", "
102 "or out of range (%u-%u).\n",
103 cb->ext_name, entry->name, lmin, lmax);
104
Jan Engelhardtdfe99f12011-02-27 19:03:28 +0100105 if (entry->type == XTTYPE_UINT8) {
106 cb->val.u8 = value;
107 if (entry->flags & XTOPT_PUT)
108 *(uint8_t *)XTOPT_MKPTR(cb) = cb->val.u8;
109 } else if (entry->type == XTTYPE_UINT32) {
Jan Engelhardta93142d2011-02-16 01:22:25 +0100110 cb->val.u32 = value;
111 if (entry->flags & XTOPT_PUT)
112 *(uint32_t *)XTOPT_MKPTR(cb) = cb->val.u32;
113 }
114}
115
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100116static void (*const xtopt_subparse[])(struct xt_option_call *) = {
Jan Engelhardtdfe99f12011-02-27 19:03:28 +0100117 [XTTYPE_UINT8] = xtopt_parse_int,
Jan Engelhardta93142d2011-02-16 01:22:25 +0100118 [XTTYPE_UINT32] = xtopt_parse_int,
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100119};
120
121static const size_t xtopt_psize[] = {
Jan Engelhardtdfe99f12011-02-27 19:03:28 +0100122 [XTTYPE_UINT8] = sizeof(uint8_t),
Jan Engelhardta93142d2011-02-16 01:22:25 +0100123 [XTTYPE_UINT32] = sizeof(uint32_t),
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100124};
125
126/**
127 * The master option parsing routine. May be used for the ".x6_parse"
128 * function pointer in extensions if fully automatic parsing is desired.
129 * It may be also called manually from a custom x6_parse function.
130 */
131void xtables_option_parse(struct xt_option_call *cb)
132{
133 const struct xt_option_entry *entry = cb->entry;
134 unsigned int eflag = 1 << cb->entry->id;
135
136 /*
137 * With {.id = P_FOO, .excl = P_FOO} we can have simple double-use
138 * prevention. Though it turned out that this is too much typing (most
139 * of the options are one-time use only), so now we also have
140 * %XTOPT_MULTI.
141 */
142 if ((!(entry->flags & XTOPT_MULTI) || (entry->excl & eflag)) &&
143 cb->xflags & eflag)
144 xt_params->exit_err(PARAMETER_PROBLEM,
145 "%s: option \"--%s\" can only be used once.\n",
146 cb->ext_name, cb->entry->name);
147 if (cb->invert && !(entry->flags & XTOPT_INVERT))
148 xt_params->exit_err(PARAMETER_PROBLEM,
149 "%s: option \"--%s\" cannot be inverted.\n",
150 cb->ext_name, entry->name);
151 if (entry->type != XTTYPE_NONE && optarg == NULL)
152 xt_params->exit_err(PARAMETER_PROBLEM,
153 "%s: option \"--%s\" requires an argument.\n",
154 cb->ext_name, entry->name);
155 if (entry->type <= ARRAY_SIZE(xtopt_subparse) &&
156 xtopt_subparse[entry->type] != NULL)
157 xtopt_subparse[entry->type](cb);
158 /* Exclusion with other flags tested later in finalize. */
159 cb->xflags |= 1 << entry->id;
160}
161
162/**
163 * Verifies that an extension's option map descriptor is valid, and ought to
164 * be called right after the extension has been loaded, and before option
165 * merging/xfrm.
166 */
167void xtables_option_metavalidate(const char *name,
168 const struct xt_option_entry *entry)
169{
170 for (; entry->name != NULL; ++entry) {
171 if (entry->id >= CHAR_BIT * sizeof(unsigned int) ||
172 entry->id >= XT_OPTION_OFFSET_SCALE)
173 xt_params->exit_err(OTHER_PROBLEM,
174 "Extension %s uses invalid ID %u\n",
175 name, entry->id);
176 if (!(entry->flags & XTOPT_PUT))
177 continue;
178 if (entry->type >= ARRAY_SIZE(xtopt_psize))
179 xt_params->exit_err(OTHER_PROBLEM,
180 "%s: entry type of option \"--%s\" cannot be "
181 "combined with XTOPT_PUT\n",
182 name, entry->name);
183 if (xtopt_psize[entry->type] != entry->size)
184 xt_params->exit_err(OTHER_PROBLEM,
185 "%s: option \"--%s\" points to a memory block "
186 "of wrong size (expected %zu, got %zu)\n",
187 name, entry->name,
188 xtopt_psize[entry->type], entry->size);
189 }
190}
191
192/**
193 * Find an option entry by its id.
194 */
195static const struct xt_option_entry *
196xtables_option_lookup(const struct xt_option_entry *entry, unsigned int id)
197{
198 for (; entry->name != NULL; ++entry)
199 if (entry->id == id)
200 return entry;
201 return NULL;
202}
203
204/**
205 * @c: getopt id (i.e. with offset)
206 * @fw: struct ipt_entry or ip6t_entry
207 *
208 * Dispatch arguments to the appropriate parse function, based upon the
209 * extension's choice of API.
210 */
211void xtables_option_tpcall(unsigned int c, char **argv, bool invert,
212 struct xtables_target *t, void *fw)
213{
214 struct xt_option_call cb;
215
216 if (t->x6_parse == NULL) {
217 if (t->parse != NULL)
218 t->parse(c - t->option_offset, argv, invert,
219 &t->tflags, fw, &t->t);
220 return;
221 }
222
223 c -= t->option_offset;
224 cb.entry = xtables_option_lookup(t->x6_options, c);
225 if (cb.entry == NULL)
226 xtables_error(OTHER_PROBLEM,
227 "Extension does not know id %u\n", c);
228 cb.arg = optarg;
229 cb.invert = invert;
230 cb.ext_name = t->name;
231 cb.data = t->t->data;
232 cb.xflags = t->tflags;
233 t->x6_parse(&cb);
234 t->tflags = cb.xflags;
235}
236
237/**
238 * @c: getopt id (i.e. with offset)
239 * @fw: struct ipt_entry or ip6t_entry
240 *
241 * Dispatch arguments to the appropriate parse function, based upon the
242 * extension's choice of API.
243 */
244void xtables_option_mpcall(unsigned int c, char **argv, bool invert,
245 struct xtables_match *m, void *fw)
246{
247 struct xt_option_call cb;
248
249 if (m->x6_parse == NULL) {
250 if (m->parse != NULL)
251 m->parse(c - m->option_offset, argv, invert,
252 &m->mflags, fw, &m->m);
253 return;
254 }
255
256 c -= m->option_offset;
257 cb.entry = xtables_option_lookup(m->x6_options, c);
258 if (cb.entry == NULL)
259 xtables_error(OTHER_PROBLEM,
260 "Extension does not know id %u\n", c);
261 cb.arg = optarg;
262 cb.invert = invert;
263 cb.ext_name = m->name;
264 cb.data = m->m->data;
265 cb.xflags = m->mflags;
266 m->x6_parse(&cb);
267 m->mflags = cb.xflags;
268}
269
270/**
271 * @name: name of extension
272 * @entry: current option (from all ext's entries) being validated
273 * @xflags: flags the extension has collected
274 * @i: conflicting option (id) to test for
275 */
276static void
277xtables_option_fcheck2(const char *name, const struct xt_option_entry *entry,
278 const struct xt_option_entry *other,
279 unsigned int xflags)
280{
281 unsigned int ef = 1 << entry->id, of = 1 << other->id;
282
283 if (entry->also & of && !(xflags & of))
284 xt_params->exit_err(PARAMETER_PROBLEM,
285 "%s: option \"--%s\" also requires \"--%s\".\n",
286 name, entry->name, other->name);
287
288 if (!(entry->excl & of))
289 /* Use of entry does not collide with other option, good. */
290 return;
291 if ((xflags & (ef | of)) != (ef | of))
292 /* Conflicting options were not used. */
293 return;
294
295 xt_params->exit_err(PARAMETER_PROBLEM,
296 "%s: option \"--%s\" cannot be used together with \"--%s\".\n",
297 name, entry->name, other->name);
298}
299
300/**
301 * @name: name of extension
302 * @xflags: accumulated flags
303 * @entry: extension's option table
304 *
305 * Check that all option constraints have been met. This effectively replaces
306 * ->final_check of the older API.
307 */
308void xtables_options_fcheck(const char *name, unsigned int xflags,
309 const struct xt_option_entry *table)
310{
311 const struct xt_option_entry *entry, *other;
312 unsigned int i;
313
314 for (entry = table; entry->name != NULL; ++entry) {
315 if (entry->flags & XTOPT_MAND &&
316 !(xflags & (1 << entry->id)))
317 xt_params->exit_err(PARAMETER_PROBLEM,
318 "%s: option \"--%s\" must be specified\n",
319 name, entry->name);
320
321 for (i = 0; i < CHAR_BIT * sizeof(entry->id); ++i) {
322 if (entry->id == i)
323 /*
324 * Avoid conflict with self. Multi-use check
325 * was done earlier in xtables_option_parse.
326 */
327 continue;
328 other = xtables_option_lookup(table, i);
329 if (other == NULL)
330 continue;
331 xtables_option_fcheck2(name, entry, other, xflags);
332 }
333 }
334}
Jan Engelhardt3af739b2011-02-10 16:57:37 +0100335
336/**
337 * Dispatch arguments to the appropriate final_check function, based upon the
338 * extension's choice of API.
339 */
340void xtables_option_tfcall(struct xtables_target *t)
341{
342 if (t->x6_fcheck != NULL) {
343 struct xt_fcheck_call cb;
344
345 cb.ext_name = t->name;
346 cb.data = t->t->data;
347 cb.xflags = t->tflags;
348 t->x6_fcheck(&cb);
349 } else if (t->final_check != NULL) {
350 t->final_check(t->tflags);
351 }
352 if (t->x6_options != NULL)
353 xtables_options_fcheck(t->name, t->tflags, t->x6_options);
354}
355
356/**
357 * Dispatch arguments to the appropriate final_check function, based upon the
358 * extension's choice of API.
359 */
360void xtables_option_mfcall(struct xtables_match *m)
361{
362 if (m->x6_fcheck != NULL) {
363 struct xt_fcheck_call cb;
364
365 cb.ext_name = m->name;
366 cb.data = m->m->data;
367 cb.xflags = m->mflags;
368 m->x6_fcheck(&cb);
369 } else if (m->final_check != NULL) {
370 m->final_check(m->mflags);
371 }
372 if (m->x6_options != NULL)
373 xtables_options_fcheck(m->name, m->mflags, m->x6_options);
374}