blob: d0aa868876831b62ac61f7edb88c56fec1c51fbb [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000019#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000021#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000022#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000023#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000024#include <stdio.h>
25#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000026#include <string.h>
27#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000028#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000032#include <arpa/inet.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000034#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +010035#include <limits.h> /* INT_MAX in ip_tables.h/ip6_tables.h */
Jan Engelhardt77f48c22009-02-07 19:59:53 +010036#include <linux/netfilter_ipv4/ip_tables.h>
37#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020038#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000040#ifndef NO_SHARED_LIBS
41#include <dlfcn.h>
42#endif
Jan Engelhardtc31870f2009-02-10 10:48:28 +010043#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
44# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
45# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
46#endif
47#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
48# define IP6T_SO_GET_REVISION_MATCH 68
49# define IP6T_SO_GET_REVISION_TARGET 69
50#endif
Jamal Hadi Salim70581922009-02-13 08:36:44 -050051#include <getopt.h>
Jan Engelhardtc31870f2009-02-10 10:48:28 +010052
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000053
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000054#define NPROTO 255
55
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000056#ifndef PROC_SYS_MODPROBE
57#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
58#endif
59
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010060void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
61
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010062struct xtables_globals *xt_params = NULL;
63
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010064void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010065{
66 va_list args;
67
68 va_start(args, msg);
69 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
70 vfprintf(stderr, msg, args);
71 va_end(args);
72 fprintf(stderr, "\n");
73 exit(status);
74}
75
Jan Engelhardt600f38d2010-10-29 18:57:42 +020076void xtables_free_opts(int unused)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010077{
Jan Engelhardt59e81142010-11-15 13:19:48 +010078 if (xt_params->opts != xt_params->orig_opts)
79 free(xt_params->opts);
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010080}
81
Jan Engelhardt600f38d2010-10-29 18:57:42 +020082struct option *xtables_merge_options(struct option *orig_opts,
83 struct option *oldopts,
Jamal Hadi Salim70581922009-02-13 08:36:44 -050084 const struct option *newopts,
85 unsigned int *option_offset)
86{
Jan Engelhardt600f38d2010-10-29 18:57:42 +020087 unsigned int num_oold = 0, num_old = 0, num_new = 0, i;
88 struct option *merge, *mp;
Jamal Hadi Salim70581922009-02-13 08:36:44 -050089
90 if (newopts == NULL)
91 return oldopts;
92
Jan Engelhardt600f38d2010-10-29 18:57:42 +020093 for (num_oold = 0; orig_opts[num_oold].name; num_oold++) ;
94 if (oldopts != NULL)
95 for (num_old = 0; oldopts[num_old].name; num_old++) ;
Jamal Hadi Salim70581922009-02-13 08:36:44 -050096 for (num_new = 0; newopts[num_new].name; num_new++) ;
97
Jan Engelhardt600f38d2010-10-29 18:57:42 +020098 merge = malloc(sizeof(*mp) * (num_oold + num_old + num_new + 1));
Jamal Hadi Salim70581922009-02-13 08:36:44 -050099 if (merge == NULL)
100 return NULL;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500101
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200102 /* Let the base options -[ADI...] have precedence over everything */
103 memcpy(merge, orig_opts, sizeof(*mp) * num_oold);
104 mp = merge + num_oold;
105
106 /* Second, the new options */
107 xt_params->option_offset += 256;
108 *option_offset = xt_params->option_offset;
109 memcpy(mp, newopts, sizeof(*mp) * num_new);
110
111 for (i = 0; i < num_new; ++i, ++mp)
112 mp->val += *option_offset;
113
114 /* Third, the old options */
115 memcpy(mp, oldopts, sizeof(*mp) * num_old);
116 mp += num_old;
117 xtables_free_opts(0);
118
119 /* Clear trailing entry */
120 memset(mp, 0, sizeof(*mp));
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500121 return merge;
122}
123
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100124/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100125 * xtables_afinfo - protocol family dependent information
126 * @kmod: kernel module basename (e.g. "ip_tables")
127 * @libprefix: prefix of .so library name (e.g. "libipt_")
128 * @family: nfproto family
129 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
130 * @so_rev_match: optname to check revision support of match
131 * @so_rev_target: optname to check revision support of target
132 */
133struct xtables_afinfo {
134 const char *kmod;
135 const char *libprefix;
136 uint8_t family;
137 uint8_t ipproto;
138 int so_rev_match;
139 int so_rev_target;
140};
141
142static const struct xtables_afinfo afinfo_ipv4 = {
143 .kmod = "ip_tables",
144 .libprefix = "libipt_",
145 .family = NFPROTO_IPV4,
146 .ipproto = IPPROTO_IP,
147 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
148 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
149};
150
151static const struct xtables_afinfo afinfo_ipv6 = {
152 .kmod = "ip6_tables",
153 .libprefix = "libip6t_",
154 .family = NFPROTO_IPV6,
155 .ipproto = IPPROTO_IPV6,
156 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
157 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
158};
159
160static const struct xtables_afinfo *afinfo;
161
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100162/* Search path for Xtables .so files */
163static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000164
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000165/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100166const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000167
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000168/* Keeping track of external matches and targets: linked lists. */
169struct xtables_match *xtables_matches;
170struct xtables_target *xtables_targets;
171
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100172void xtables_init(void)
173{
174 xtables_libdir = getenv("XTABLES_LIBDIR");
175 if (xtables_libdir != NULL)
176 return;
177 xtables_libdir = getenv("IPTABLES_LIB_DIR");
178 if (xtables_libdir != NULL) {
179 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
180 "use XTABLES_LIBDIR.\n");
181 return;
182 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100183 /*
184 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
185 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
186 * for these env vars are deprecated anyhow, and in light of the
187 * (shared) libxt_*.so files, makes less sense to have
188 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
189 */
190 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
191 if (xtables_libdir != NULL) {
192 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
193 "use XTABLES_LIBDIR.\n");
194 return;
195 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100196 xtables_libdir = XTABLES_LIBDIR;
197}
198
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100199void xtables_set_nfproto(uint8_t nfproto)
200{
201 switch (nfproto) {
202 case NFPROTO_IPV4:
203 afinfo = &afinfo_ipv4;
204 break;
205 case NFPROTO_IPV6:
206 afinfo = &afinfo_ipv6;
207 break;
208 default:
209 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
210 __func__);
211 }
212}
213
Jan Engelhardt630ef482009-01-27 14:58:41 +0100214/**
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500215 * xtables_set_params - set the global parameters used by xtables
216 * @xtp: input xtables_globals structure
217 *
218 * The app is expected to pass a valid xtables_globals data-filled
219 * with proper values
220 * @xtp cannot be NULL
221 *
222 * Returns -1 on failure to set and 0 on success
223 */
224int xtables_set_params(struct xtables_globals *xtp)
225{
226 if (!xtp) {
227 fprintf(stderr, "%s: Illegal global params\n",__func__);
228 return -1;
229 }
230
231 xt_params = xtp;
232
233 if (!xt_params->exit_err)
234 xt_params->exit_err = basic_exit_err;
235
236 return 0;
237}
238
239int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto)
240{
241 xtables_init();
242 xtables_set_nfproto(nfproto);
243 return xtables_set_params(xtp);
244}
245
246/**
Jan Engelhardt630ef482009-01-27 14:58:41 +0100247 * xtables_*alloc - wrappers that exit on failure
248 */
249void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000250{
251 void *p;
252
253 if ((p = calloc(count, size)) == NULL) {
254 perror("ip[6]tables: calloc failed");
255 exit(1);
256 }
257
258 return p;
259}
260
Jan Engelhardt630ef482009-01-27 14:58:41 +0100261void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000262{
263 void *p;
264
265 if ((p = malloc(size)) == NULL) {
266 perror("ip[6]tables: malloc failed");
267 exit(1);
268 }
269
270 return p;
271}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000272
Michael Granzow332e4ac2009-04-09 18:24:36 +0100273void *xtables_realloc(void *ptr, size_t size)
274{
275 void *p;
276
277 if ((p = realloc(ptr, size)) == NULL) {
278 perror("ip[6]tables: realloc failed");
279 exit(1);
280 }
281
282 return p;
283}
284
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000285static char *get_modprobe(void)
286{
287 int procfile;
288 char *ret;
289
290#define PROCFILE_BUFSIZ 1024
291 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
292 if (procfile < 0)
293 return NULL;
294
Jan Engelhardt371cea22010-07-25 23:36:17 +0200295 ret = malloc(PROCFILE_BUFSIZ);
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000296 if (ret) {
297 memset(ret, 0, PROCFILE_BUFSIZ);
298 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
299 case -1: goto fail;
300 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
301 }
302 if (ret[strlen(ret)-1]=='\n')
303 ret[strlen(ret)-1]=0;
304 close(procfile);
305 return ret;
306 }
307 fail:
308 free(ret);
309 close(procfile);
310 return NULL;
311}
312
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100313int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000314{
315 char *buf = NULL;
316 char *argv[4];
317 int status;
318
319 /* If they don't explicitly set it, read out of kernel */
320 if (!modprobe) {
321 buf = get_modprobe();
322 if (!buf)
323 return -1;
324 modprobe = buf;
325 }
326
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100327 /*
328 * Need to flush the buffer, or the child may output it again
329 * when switching the program thru execv.
330 */
331 fflush(stdout);
332
Jan Engelhardt94aa2ea2009-10-11 03:56:18 -0400333 switch (vfork()) {
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000334 case 0:
335 argv[0] = (char *)modprobe;
336 argv[1] = (char *)modname;
337 if (quiet) {
338 argv[2] = "-q";
339 argv[3] = NULL;
340 } else {
341 argv[2] = NULL;
342 argv[3] = NULL;
343 }
344 execv(argv[0], argv);
345
346 /* not usually reached */
347 exit(1);
348 case -1:
349 return -1;
350
351 default: /* parent */
352 wait(&status);
353 }
354
355 free(buf);
356 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
357 return 0;
358 return -1;
359}
360
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100361int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000362{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100363 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000364 static int ret = -1;
365
366 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100367 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000368 loaded = (ret == 0);
369 }
370
371 return ret;
372}
373
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100374/**
375 * xtables_strtou{i,l} - string to number conversion
376 * @s: input string
377 * @end: like strtoul's "end" pointer
378 * @value: pointer for result
379 * @min: minimum accepted value
380 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000381 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100382 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
383 * "15a" is rejected.
384 * In either case, the value obtained is compared for min-max compliance.
385 * Base is always 0, i.e. autodetect depending on @s.
386 *
387 * Returns true/false whether number was accepted. On failure, *value has
388 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000389 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100390bool xtables_strtoul(const char *s, char **end, unsigned long *value,
391 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000392{
393 unsigned long v;
394 char *my_end;
395
396 errno = 0;
397 v = strtoul(s, &my_end, 0);
398
399 if (my_end == s)
400 return false;
401 if (end != NULL)
402 *end = my_end;
403
404 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
405 if (value != NULL)
406 *value = v;
407 if (end == NULL)
408 return *my_end == '\0';
409 return true;
410 }
411
412 return false;
413}
414
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100415bool xtables_strtoui(const char *s, char **end, unsigned int *value,
416 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000417{
418 unsigned long v;
419 bool ret;
420
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100421 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000422 if (value != NULL)
423 *value = v;
424 return ret;
425}
426
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100427int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000428{
429 struct servent *service;
430
431 if ((service = getservbyname(name, proto)) != NULL)
432 return ntohs((unsigned short) service->s_port);
433
434 return -1;
435}
436
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100437u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000438{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100439 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000440
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100441 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100442 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100443 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000444
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100445 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000446 "invalid port/service `%s' specified", port);
447}
448
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100449void xtables_parse_interface(const char *arg, char *vianame,
450 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000451{
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100452 unsigned int vialen = strlen(arg);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000453 unsigned int i;
454
455 memset(mask, 0, IFNAMSIZ);
456 memset(vianame, 0, IFNAMSIZ);
457
458 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100459 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000460 "interface name `%s' must be shorter than IFNAMSIZ"
461 " (%i)", arg, IFNAMSIZ-1);
462
463 strcpy(vianame, arg);
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100464 if (vialen == 0)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000465 memset(mask, 0, IFNAMSIZ);
466 else if (vianame[vialen - 1] == '+') {
467 memset(mask, 0xFF, vialen - 1);
468 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
469 /* Don't remove `+' here! -HW */
470 } else {
471 /* Include nul-terminator in match */
472 memset(mask, 0xFF, vialen + 1);
473 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
474 for (i = 0; vianame[i]; i++) {
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100475 if (vianame[i] == '/' ||
476 vianame[i] == ' ') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000477 fprintf(stderr,
478 "Warning: weird character in interface"
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100479 " `%s' ('/' and ' ' are not allowed by the kernel).\n",
Max Kellermannaae4f822007-10-17 16:36:49 +0000480 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000481 break;
482 }
483 }
484 }
485}
486
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200487#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100488static void *load_extension(const char *search_path, const char *prefix,
489 const char *name, bool is_target)
490{
491 const char *dir = search_path, *next;
492 void *ptr = NULL;
493 struct stat sb;
494 char path[256];
495
496 do {
497 next = strchr(dir, ':');
498 if (next == NULL)
499 next = dir + strlen(dir);
500 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200501 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100502
503 if (dlopen(path, RTLD_NOW) != NULL) {
504 /* Found library. If it didn't register itself,
505 maybe they specified target as match. */
506 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100507 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100508 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100509 ptr = xtables_find_match(name,
510 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100511 } else if (stat(path, &sb) == 0) {
512 fprintf(stderr, "%s: %s\n", path, dlerror());
513 }
514
515 if (ptr != NULL)
516 return ptr;
517
518 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200519 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100520 if (dlopen(path, RTLD_NOW) != NULL) {
521 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100522 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100523 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100524 ptr = xtables_find_match(name,
525 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100526 } else if (stat(path, &sb) == 0) {
527 fprintf(stderr, "%s: %s\n", path, dlerror());
528 }
529
530 if (ptr != NULL)
531 return ptr;
532
533 dir = next + 1;
534 } while (*next != '\0');
535
536 return NULL;
537}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200538#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100539
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100540struct xtables_match *
541xtables_find_match(const char *name, enum xtables_tryload tryload,
542 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000543{
544 struct xtables_match *ptr;
545 const char *icmp6 = "icmp6";
546
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200547 if (strlen(name) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100548 xtables_error(PARAMETER_PROBLEM,
549 "Invalid match name \"%s\" (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200550 name, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100551
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000552 /* This is ugly as hell. Nonetheless, there is no way of changing
553 * this without hurting backwards compatibility */
554 if ( (strcmp(name,"icmpv6") == 0) ||
555 (strcmp(name,"ipv6-icmp") == 0) ||
556 (strcmp(name,"icmp6") == 0) )
557 name = icmp6;
558
559 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
560 if (strcmp(name, ptr->name) == 0) {
561 struct xtables_match *clone;
562
563 /* First match of this type: */
564 if (ptr->m == NULL)
565 break;
566
567 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100568 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000569 memcpy(clone, ptr, sizeof(struct xtables_match));
570 clone->mflags = 0;
571 /* This is a clone: */
572 clone->next = clone;
573
574 ptr = clone;
575 break;
576 }
577 }
578
579#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100580 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100581 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100582 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000583
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100584 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100585 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000586 "Couldn't load match `%s':%s\n",
587 name, dlerror());
588 }
589#else
590 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100591 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000592 ptr->loaded = 1;
593 else
594 ptr = NULL;
595 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100596 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100597 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000598 "Couldn't find match `%s'\n", name);
599 }
600#endif
601
602 if (ptr && matches) {
603 struct xtables_rule_match **i;
604 struct xtables_rule_match *newentry;
605
Jan Engelhardt630ef482009-01-27 14:58:41 +0100606 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000607
608 for (i = matches; *i; i = &(*i)->next) {
609 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100610 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000611 }
612 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100613 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000614 newentry->next = NULL;
615 *i = newentry;
616 }
617
618 return ptr;
619}
620
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100621struct xtables_target *
622xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000623{
624 struct xtables_target *ptr;
625
626 /* Standard target? */
627 if (strcmp(name, "") == 0
628 || strcmp(name, XTC_LABEL_ACCEPT) == 0
629 || strcmp(name, XTC_LABEL_DROP) == 0
630 || strcmp(name, XTC_LABEL_QUEUE) == 0
631 || strcmp(name, XTC_LABEL_RETURN) == 0)
632 name = "standard";
633
634 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
635 if (strcmp(name, ptr->name) == 0)
636 break;
637 }
638
639#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100640 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100641 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100642 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000643
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100644 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100645 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000646 "Couldn't load target `%s':%s\n",
647 name, dlerror());
648 }
649#else
650 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100651 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000652 ptr->loaded = 1;
653 else
654 ptr = NULL;
655 }
Peter Volkov854d2d92009-03-24 11:09:16 +0300656 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100657 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000658 "Couldn't find target `%s'\n", name);
659 }
660#endif
661
662 if (ptr)
663 ptr->used = 1;
664
665 return ptr;
666}
667
668static int compatible_revision(const char *name, u_int8_t revision, int opt)
669{
670 struct xt_get_revision rev;
671 socklen_t s = sizeof(rev);
672 int max_rev, sockfd;
673
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100674 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000675 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000676 if (errno == EPERM) {
677 /* revision 0 is always supported. */
678 if (revision != 0)
679 fprintf(stderr, "Could not determine whether "
680 "revision %u is supported, "
681 "assuming it is.\n",
682 revision);
683 return 1;
684 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000685 fprintf(stderr, "Could not open socket to kernel: %s\n",
686 strerror(errno));
687 exit(1);
688 }
689
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100690 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000691
692 strcpy(rev.name, name);
693 rev.revision = revision;
694
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100695 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000696 if (max_rev < 0) {
697 /* Definitely don't support this? */
698 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
699 close(sockfd);
700 return 0;
701 } else if (errno == ENOPROTOOPT) {
702 close(sockfd);
703 /* Assume only revision 0 support (old kernel) */
704 return (revision == 0);
705 } else {
706 fprintf(stderr, "getsockopt failed strangely: %s\n",
707 strerror(errno));
708 exit(1);
709 }
710 }
711 close(sockfd);
712 return 1;
713}
714
715
716static int compatible_match_revision(const char *name, u_int8_t revision)
717{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100718 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000719}
720
721static int compatible_target_revision(const char *name, u_int8_t revision)
722{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100723 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000724}
725
726void xtables_register_match(struct xtables_match *me)
727{
728 struct xtables_match **i, *old;
729
Jan Engelhardtc284de52009-06-25 21:25:24 +0200730 if (me->version == NULL) {
731 fprintf(stderr, "%s: match %s<%u> is missing a version\n",
732 xt_params->program_name, me->name, me->revision);
733 exit(1);
734 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100735 if (strcmp(me->version, XTABLES_VERSION) != 0) {
736 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
737 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500738 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100739 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000740 exit(1);
741 }
742
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200743 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000744 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500745 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000746 exit(1);
747 }
748
749 if (me->family >= NPROTO) {
750 fprintf(stderr,
751 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500752 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000753 exit(1);
754 }
755
756 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100757 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000758 return;
759
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100760 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000761 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100762 if (old->revision == me->revision &&
763 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000764 fprintf(stderr,
765 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500766 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000767 exit(1);
768 }
769
770 /* Now we have two (or more) options, check compatibility. */
771 if (compatible_match_revision(old->name, old->revision)
772 && old->revision > me->revision)
773 return;
774
Jan Engelhardt23545c22008-02-14 04:23:04 +0100775 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000776 if (!compatible_match_revision(me->name, me->revision))
777 return;
778
Jan Engelhardt23545c22008-02-14 04:23:04 +0100779 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
780 if (old->revision == me->revision && me->family == AF_UNSPEC)
781 return;
782
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000783 /* Delete old one. */
784 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
785 *i = old->next;
786 }
787
788 if (me->size != XT_ALIGN(me->size)) {
789 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500790 xt_params->program_name, me->name,
791 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000792 exit(1);
793 }
794
795 /* Append to list. */
796 for (i = &xtables_matches; *i; i = &(*i)->next);
797 me->next = NULL;
798 *i = me;
799
800 me->m = NULL;
801 me->mflags = 0;
802}
803
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200804void xtables_register_matches(struct xtables_match *match, unsigned int n)
805{
806 do {
807 xtables_register_match(&match[--n]);
808 } while (n > 0);
809}
810
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000811void xtables_register_target(struct xtables_target *me)
812{
813 struct xtables_target *old;
814
Jan Engelhardtc284de52009-06-25 21:25:24 +0200815 if (me->version == NULL) {
816 fprintf(stderr, "%s: target %s<%u> is missing a version\n",
817 xt_params->program_name, me->name, me->revision);
818 exit(1);
819 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100820 if (strcmp(me->version, XTABLES_VERSION) != 0) {
821 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
822 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500823 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100824 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000825 exit(1);
826 }
827
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200828 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000829 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500830 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000831 exit(1);
832 }
833
834 if (me->family >= NPROTO) {
835 fprintf(stderr,
836 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500837 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000838 exit(1);
839 }
840
841 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100842 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000843 return;
844
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100845 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000846 if (old) {
847 struct xtables_target **i;
848
Jan Engelhardt23545c22008-02-14 04:23:04 +0100849 if (old->revision == me->revision &&
850 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000851 fprintf(stderr,
852 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500853 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000854 exit(1);
855 }
856
857 /* Now we have two (or more) options, check compatibility. */
858 if (compatible_target_revision(old->name, old->revision)
859 && old->revision > me->revision)
860 return;
861
Jan Engelhardt23545c22008-02-14 04:23:04 +0100862 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000863 if (!compatible_target_revision(me->name, me->revision))
864 return;
865
Jan Engelhardt23545c22008-02-14 04:23:04 +0100866 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
867 if (old->revision == me->revision && me->family == AF_UNSPEC)
868 return;
869
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000870 /* Delete old one. */
871 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
872 *i = old->next;
873 }
874
875 if (me->size != XT_ALIGN(me->size)) {
876 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500877 xt_params->program_name, me->name,
878 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000879 exit(1);
880 }
881
882 /* Prepend to list. */
883 me->next = xtables_targets;
884 xtables_targets = me;
885 me->t = NULL;
886 me->tflags = 0;
887}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000888
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200889void xtables_register_targets(struct xtables_target *target, unsigned int n)
890{
891 do {
892 xtables_register_target(&target[--n]);
893 } while (n > 0);
894}
895
Jan Engelhardta41545c2009-01-27 21:27:19 +0100896/**
897 * xtables_param_act - act on condition
898 * @status: a constant from enum xtables_exittype
899 *
900 * %XTF_ONLY_ONCE: print error message that option may only be used once.
901 * @p1: module name (e.g. "mark")
902 * @p2(...): option in conflict (e.g. "--mark")
903 * @p3(...): condition to match on (see extensions/ for examples)
904 *
905 * %XTF_NO_INVERT: option does not support inversion
906 * @p1: module name
907 * @p2: option in conflict
908 * @p3: condition to match on
909 *
910 * %XTF_BAD_VALUE: bad value for option
911 * @p1: module name
912 * @p2: option with which the problem occured (e.g. "--mark")
913 * @p3: string the user passed in (e.g. "99999999999999")
914 *
915 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
916 * @p1: module name
917 *
918 * Displays an error message and exits the program.
919 */
920void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000921{
922 const char *p2, *p3;
923 va_list args;
924 bool b;
925
926 va_start(args, p1);
927
928 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100929 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000930 p2 = va_arg(args, const char *);
931 b = va_arg(args, unsigned int);
932 if (!b)
933 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100934 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000935 "%s: \"%s\" option may only be specified once",
936 p1, p2);
937 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100938 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000939 p2 = va_arg(args, const char *);
940 b = va_arg(args, unsigned int);
941 if (!b)
942 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100943 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000944 "%s: \"%s\" option cannot be inverted", p1, p2);
945 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100946 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000947 p2 = va_arg(args, const char *);
948 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100949 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000950 "%s: Bad value for \"%s\" option: \"%s\"",
951 p1, p2, p3);
952 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100953 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000954 b = va_arg(args, unsigned int);
955 if (!b)
956 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100957 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000958 "%s: At most one action is possible", p1);
959 break;
960 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100961 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000962 break;
963 }
964
965 va_end(args);
966}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000967
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100968const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000969{
970 static char buf[20];
971 const unsigned char *bytep = (const void *)&addrp->s_addr;
972
973 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
974 return buf;
975}
976
977static const char *ipaddr_to_host(const struct in_addr *addr)
978{
979 struct hostent *host;
980
981 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
982 if (host == NULL)
983 return NULL;
984
985 return host->h_name;
986}
987
988static const char *ipaddr_to_network(const struct in_addr *addr)
989{
990 struct netent *net;
991
992 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
993 return net->n_name;
994
995 return NULL;
996}
997
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100998const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000999{
1000 const char *name;
1001
1002 if ((name = ipaddr_to_host(addr)) != NULL ||
1003 (name = ipaddr_to_network(addr)) != NULL)
1004 return name;
1005
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001006 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001007}
1008
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001009const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001010{
1011 static char buf[20];
1012 uint32_t maskaddr, bits;
1013 int i;
1014
1015 maskaddr = ntohl(mask->s_addr);
1016
1017 if (maskaddr == 0xFFFFFFFFL)
1018 /* we don't want to see "/32" */
1019 return "";
1020
1021 i = 32;
1022 bits = 0xFFFFFFFEL;
1023 while (--i >= 0 && maskaddr != bits)
1024 bits <<= 1;
1025 if (i >= 0)
1026 sprintf(buf, "/%d", i);
1027 else
1028 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001029 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001030
1031 return buf;
1032}
1033
Jan Engelhardtbd943842008-01-20 13:38:08 +00001034static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1035{
1036 static struct in_addr addr;
1037 unsigned char *addrp;
1038 unsigned int onebyte;
1039 char buf[20], *p, *q;
1040 int i;
1041
1042 /* copy dotted string, because we need to modify it */
1043 strncpy(buf, dotted, sizeof(buf) - 1);
1044 buf[sizeof(buf) - 1] = '\0';
1045 addrp = (void *)&addr.s_addr;
1046
1047 p = buf;
1048 for (i = 0; i < 3; ++i) {
1049 if ((q = strchr(p, '.')) == NULL) {
1050 if (is_mask)
1051 return NULL;
1052
1053 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001054 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001055 return NULL;
1056
1057 addrp[i] = onebyte;
1058 while (i < 3)
1059 addrp[++i] = 0;
1060
1061 return &addr;
1062 }
1063
1064 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001065 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001066 return NULL;
1067
1068 addrp[i] = onebyte;
1069 p = q + 1;
1070 }
1071
1072 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001073 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001074 return NULL;
1075
1076 addrp[3] = onebyte;
1077 return &addr;
1078}
1079
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001080struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001081{
1082 return __numeric_to_ipaddr(dotted, false);
1083}
1084
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001085struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001086{
1087 return __numeric_to_ipaddr(dotted, true);
1088}
1089
1090static struct in_addr *network_to_ipaddr(const char *name)
1091{
1092 static struct in_addr addr;
1093 struct netent *net;
1094
1095 if ((net = getnetbyname(name)) != NULL) {
1096 if (net->n_addrtype != AF_INET)
1097 return NULL;
1098 addr.s_addr = htonl(net->n_net);
1099 return &addr;
1100 }
1101
1102 return NULL;
1103}
1104
1105static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1106{
1107 struct hostent *host;
1108 struct in_addr *addr;
1109 unsigned int i;
1110
1111 *naddr = 0;
1112 if ((host = gethostbyname(name)) != NULL) {
1113 if (host->h_addrtype != AF_INET ||
1114 host->h_length != sizeof(struct in_addr))
1115 return NULL;
1116
1117 while (host->h_addr_list[*naddr] != NULL)
1118 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001119 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001120 for (i = 0; i < *naddr; i++)
1121 memcpy(&addr[i], host->h_addr_list[i],
1122 sizeof(struct in_addr));
1123 return addr;
1124 }
1125
1126 return NULL;
1127}
1128
1129static struct in_addr *
1130ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1131{
1132 struct in_addr *addrptmp, *addrp;
1133
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001134 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001135 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001136 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001137 memcpy(addrp, addrptmp, sizeof(*addrp));
1138 *naddrs = 1;
1139 return addrp;
1140 }
1141 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1142 return addrptmp;
1143
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001144 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001145}
1146
1147static struct in_addr *parse_ipmask(const char *mask)
1148{
1149 static struct in_addr maskaddr;
1150 struct in_addr *addrp;
1151 unsigned int bits;
1152
1153 if (mask == NULL) {
1154 /* no mask at all defaults to 32 bits */
1155 maskaddr.s_addr = 0xFFFFFFFF;
1156 return &maskaddr;
1157 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001158 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001159 /* dotted_to_addr already returns a network byte order addr */
1160 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001161 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001162 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001163 "invalid mask `%s' specified", mask);
1164 if (bits != 0) {
1165 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1166 return &maskaddr;
1167 }
1168
1169 maskaddr.s_addr = 0U;
1170 return &maskaddr;
1171}
1172
Michael Granzow332e4ac2009-04-09 18:24:36 +01001173void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp,
1174 struct in_addr **maskpp, unsigned int *naddrs)
1175{
1176 struct in_addr *addrp;
1177 char buf[256], *p;
1178 unsigned int len, i, j, n, count = 1;
1179 const char *loop = name;
1180
1181 while ((loop = strchr(loop, ',')) != NULL) {
1182 ++count;
1183 ++loop; /* skip ',' */
1184 }
1185
1186 *addrpp = xtables_malloc(sizeof(struct in_addr) * count);
1187 *maskpp = xtables_malloc(sizeof(struct in_addr) * count);
1188
1189 loop = name;
1190
1191 for (i = 0; i < count; ++i) {
1192 if (loop == NULL)
1193 break;
1194 if (*loop == ',')
1195 ++loop;
1196 if (*loop == '\0')
1197 break;
1198 p = strchr(loop, ',');
1199 if (p != NULL)
1200 len = p - loop;
1201 else
1202 len = strlen(loop);
1203 if (len == 0 || sizeof(buf) - 1 < len)
1204 break;
1205
1206 strncpy(buf, loop, len);
1207 buf[len] = '\0';
1208 loop += len;
1209 if ((p = strrchr(buf, '/')) != NULL) {
1210 *p = '\0';
1211 addrp = parse_ipmask(p + 1);
1212 } else {
1213 addrp = parse_ipmask(NULL);
1214 }
1215 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1216
1217 /* if a null mask is given, the name is ignored, like in "any/0" */
1218 if ((*maskpp + i)->s_addr == 0)
1219 /*
1220 * A bit pointless to process multiple addresses
1221 * in this case...
1222 */
1223 strcpy(buf, "0.0.0.0");
1224
1225 addrp = ipparse_hostnetwork(buf, &n);
1226 if (n > 1) {
1227 count += n - 1;
1228 *addrpp = xtables_realloc(*addrpp,
1229 sizeof(struct in_addr) * count);
1230 *maskpp = xtables_realloc(*maskpp,
1231 sizeof(struct in_addr) * count);
1232 for (j = 0; j < n; ++j)
1233 /* for each new addr */
1234 memcpy(*addrpp + i + j, addrp + j,
1235 sizeof(*addrp));
1236 for (j = 1; j < n; ++j)
1237 /* for each new mask */
1238 memcpy(*maskpp + i + j, *maskpp + i,
1239 sizeof(*addrp));
1240 i += n - 1;
1241 } else {
1242 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1243 }
1244 /* free what ipparse_hostnetwork had allocated: */
1245 free(addrp);
1246 }
1247 *naddrs = count;
1248 for (i = 0; i < n; ++i)
1249 (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr;
1250}
1251
1252
Jan Engelhardta0baae82009-01-30 04:32:50 +01001253/**
1254 * xtables_ipparse_any - transform arbitrary name to in_addr
1255 *
1256 * Possible inputs (pseudo regex):
1257 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1258 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1259 */
1260void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1261 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001262{
1263 unsigned int i, j, k, n;
1264 struct in_addr *addrp;
1265 char buf[256], *p;
1266
1267 strncpy(buf, name, sizeof(buf) - 1);
1268 buf[sizeof(buf) - 1] = '\0';
1269 if ((p = strrchr(buf, '/')) != NULL) {
1270 *p = '\0';
1271 addrp = parse_ipmask(p + 1);
1272 } else {
1273 addrp = parse_ipmask(NULL);
1274 }
1275 memcpy(maskp, addrp, sizeof(*maskp));
1276
1277 /* if a null mask is given, the name is ignored, like in "any/0" */
1278 if (maskp->s_addr == 0U)
1279 strcpy(buf, "0.0.0.0");
1280
1281 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1282 n = *naddrs;
1283 for (i = 0, j = 0; i < n; ++i) {
1284 addrp[j++].s_addr &= maskp->s_addr;
1285 for (k = 0; k < j - 1; ++k)
1286 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1287 --*naddrs;
1288 --j;
1289 break;
1290 }
1291 }
1292}
1293
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001294const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001295{
1296 /* 0000:0000:0000:0000:0000:000.000.000.000
1297 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1298 static char buf[50+1];
1299 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1300}
1301
1302static const char *ip6addr_to_host(const struct in6_addr *addr)
1303{
1304 static char hostname[NI_MAXHOST];
1305 struct sockaddr_in6 saddr;
1306 int err;
1307
1308 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1309 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1310 saddr.sin6_family = AF_INET6;
1311
1312 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1313 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1314 if (err != 0) {
1315#ifdef DEBUG
1316 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1317#endif
1318 return NULL;
1319 }
1320
1321#ifdef DEBUG
1322 fprintf (stderr, "\naddr2host: %s\n", hostname);
1323#endif
1324 return hostname;
1325}
1326
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001327const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001328{
1329 const char *name;
1330
1331 if ((name = ip6addr_to_host(addr)) != NULL)
1332 return name;
1333
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001334 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001335}
1336
1337static int ip6addr_prefix_length(const struct in6_addr *k)
1338{
1339 unsigned int bits = 0;
1340 uint32_t a, b, c, d;
1341
Jan Engelhardt48607812008-06-10 15:17:53 +02001342 a = ntohl(k->s6_addr32[0]);
1343 b = ntohl(k->s6_addr32[1]);
1344 c = ntohl(k->s6_addr32[2]);
1345 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001346 while (a & 0x80000000U) {
1347 ++bits;
1348 a <<= 1;
1349 a |= (b >> 31) & 1;
1350 b <<= 1;
1351 b |= (c >> 31) & 1;
1352 c <<= 1;
1353 c |= (d >> 31) & 1;
1354 d <<= 1;
1355 }
1356 if (a != 0 || b != 0 || c != 0 || d != 0)
1357 return -1;
1358 return bits;
1359}
1360
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001361const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001362{
1363 static char buf[50+2];
1364 int l = ip6addr_prefix_length(addrp);
1365
1366 if (l == -1) {
1367 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001368 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001369 return buf;
1370 }
1371 sprintf(buf, "/%d", l);
1372 return buf;
1373}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001374
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001375struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001376{
1377 static struct in6_addr ap;
1378 int err;
1379
1380 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1381 return &ap;
1382#ifdef DEBUG
1383 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1384#endif
1385 return NULL;
1386}
1387
1388static struct in6_addr *
1389host_to_ip6addr(const char *name, unsigned int *naddr)
1390{
1391 static struct in6_addr *addr;
1392 struct addrinfo hints;
1393 struct addrinfo *res;
1394 int err;
1395
1396 memset(&hints, 0, sizeof(hints));
1397 hints.ai_flags = AI_CANONNAME;
1398 hints.ai_family = AF_INET6;
1399 hints.ai_socktype = SOCK_RAW;
1400 hints.ai_protocol = IPPROTO_IPV6;
1401 hints.ai_next = NULL;
1402
1403 *naddr = 0;
1404 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1405#ifdef DEBUG
1406 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1407#endif
1408 return NULL;
1409 } else {
1410 if (res->ai_family != AF_INET6 ||
1411 res->ai_addrlen != sizeof(struct sockaddr_in6))
1412 return NULL;
1413
1414#ifdef DEBUG
1415 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
Patrick McHardy30290ae2010-05-20 15:41:03 +02001416 xtables_ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001417#endif
1418 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001419 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001420 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1421 sizeof(struct in6_addr));
1422 freeaddrinfo(res);
1423 *naddr = 1;
1424 return addr;
1425 }
1426
1427 return NULL;
1428}
1429
1430static struct in6_addr *network_to_ip6addr(const char *name)
1431{
1432 /* abort();*/
1433 /* TODO: not implemented yet, but the exception breaks the
1434 * name resolvation */
1435 return NULL;
1436}
1437
1438static struct in6_addr *
1439ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1440{
1441 struct in6_addr *addrp, *addrptmp;
1442
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001443 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001444 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001445 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001446 memcpy(addrp, addrptmp, sizeof(*addrp));
1447 *naddrs = 1;
1448 return addrp;
1449 }
1450 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1451 return addrp;
1452
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001453 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001454}
1455
1456static struct in6_addr *parse_ip6mask(char *mask)
1457{
1458 static struct in6_addr maskaddr;
1459 struct in6_addr *addrp;
1460 unsigned int bits;
1461
1462 if (mask == NULL) {
1463 /* no mask at all defaults to 128 bits */
1464 memset(&maskaddr, 0xff, sizeof maskaddr);
1465 return &maskaddr;
1466 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001467 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001468 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001469 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001470 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001471 "invalid mask `%s' specified", mask);
1472 if (bits != 0) {
1473 char *p = (void *)&maskaddr;
1474 memset(p, 0xff, bits / 8);
1475 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1476 p[bits/8] = 0xff << (8 - (bits & 7));
1477 return &maskaddr;
1478 }
1479
1480 memset(&maskaddr, 0, sizeof(maskaddr));
1481 return &maskaddr;
1482}
1483
Michael Granzow332e4ac2009-04-09 18:24:36 +01001484void
1485xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp,
1486 struct in6_addr **maskpp, unsigned int *naddrs)
1487{
Olaf Rempel58df9012009-09-20 13:24:11 +02001488 static const struct in6_addr zero_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001489 struct in6_addr *addrp;
1490 char buf[256], *p;
1491 unsigned int len, i, j, n, count = 1;
1492 const char *loop = name;
1493
1494 while ((loop = strchr(loop, ',')) != NULL) {
1495 ++count;
1496 ++loop; /* skip ',' */
1497 }
1498
1499 *addrpp = xtables_malloc(sizeof(struct in6_addr) * count);
1500 *maskpp = xtables_malloc(sizeof(struct in6_addr) * count);
1501
1502 loop = name;
1503
1504 for (i = 0; i < count /*NB: count can grow*/; ++i) {
1505 if (loop == NULL)
1506 break;
1507 if (*loop == ',')
1508 ++loop;
1509 if (*loop == '\0')
1510 break;
1511 p = strchr(loop, ',');
1512 if (p != NULL)
1513 len = p - loop;
1514 else
1515 len = strlen(loop);
1516 if (len == 0 || sizeof(buf) - 1 < len)
1517 break;
1518
1519 strncpy(buf, loop, len);
1520 buf[len] = '\0';
1521 loop += len;
1522 if ((p = strrchr(buf, '/')) != NULL) {
1523 *p = '\0';
1524 addrp = parse_ip6mask(p + 1);
1525 } else {
1526 addrp = parse_ip6mask(NULL);
1527 }
1528 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1529
1530 /* if a null mask is given, the name is ignored, like in "any/0" */
Olaf Rempel58df9012009-09-20 13:24:11 +02001531 if (memcmp(*maskpp + i, &zero_addr, sizeof(zero_addr)) == 0)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001532 strcpy(buf, "::");
1533
1534 addrp = ip6parse_hostnetwork(buf, &n);
1535 /* ip6parse_hostnetwork only ever returns one IP
1536 address (it exits if the resolution fails).
1537 Therefore, n will always be 1 here. Leaving the
1538 code below in anyway in case ip6parse_hostnetwork
1539 is improved some day to behave like
1540 ipparse_hostnetwork: */
1541 if (n > 1) {
1542 count += n - 1;
1543 *addrpp = xtables_realloc(*addrpp,
1544 sizeof(struct in6_addr) * count);
1545 *maskpp = xtables_realloc(*maskpp,
1546 sizeof(struct in6_addr) * count);
1547 for (j = 0; j < n; ++j)
1548 /* for each new addr */
1549 memcpy(*addrpp + i + j, addrp + j,
1550 sizeof(*addrp));
1551 for (j = 1; j < n; ++j)
1552 /* for each new mask */
1553 memcpy(*maskpp + i + j, *maskpp + i,
1554 sizeof(*addrp));
1555 i += n - 1;
1556 } else {
1557 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1558 }
1559 /* free what ip6parse_hostnetwork had allocated: */
1560 free(addrp);
1561 }
1562 *naddrs = count;
1563 for (i = 0; i < n; ++i)
1564 for (j = 0; j < 4; ++j)
1565 (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j];
1566}
1567
Jan Engelhardta0baae82009-01-30 04:32:50 +01001568void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1569 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001570{
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001571 static const struct in6_addr zero_addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001572 struct in6_addr *addrp;
1573 unsigned int i, j, k, n;
1574 char buf[256], *p;
1575
1576 strncpy(buf, name, sizeof(buf) - 1);
1577 buf[sizeof(buf)-1] = '\0';
1578 if ((p = strrchr(buf, '/')) != NULL) {
1579 *p = '\0';
1580 addrp = parse_ip6mask(p + 1);
1581 } else {
1582 addrp = parse_ip6mask(NULL);
1583 }
1584 memcpy(maskp, addrp, sizeof(*maskp));
1585
1586 /* if a null mask is given, the name is ignored, like in "any/0" */
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001587 if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001588 strcpy(buf, "::");
1589
1590 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1591 n = *naddrs;
1592 for (i = 0, j = 0; i < n; ++i) {
1593 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001594 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001595 ++j;
1596 for (k = 0; k < j - 1; ++k)
1597 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1598 --*naddrs;
1599 --j;
1600 break;
1601 }
1602 }
1603}
Max Kellermanna5d09942008-01-29 13:44:34 +00001604
Jan Engelhardta0baae82009-01-30 04:32:50 +01001605void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001606{
1607 static const char no_quote_chars[] = "_-0123456789"
1608 "abcdefghijklmnopqrstuvwxyz"
1609 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1610 static const char escape_chars[] = "\"\\'";
1611 size_t length;
1612 const char *p;
1613
1614 length = strcspn(value, no_quote_chars);
1615 if (length > 0 && value[length] == 0) {
1616 /* no quoting required */
1617 fputs(value, stdout);
1618 putchar(' ');
1619 } else {
1620 /* there is at least one dangerous character in the
1621 value, which we have to quote. Write double quotes
1622 around the value and escape special characters with
1623 a backslash */
1624 putchar('"');
1625
1626 for (p = strpbrk(value, escape_chars); p != NULL;
1627 p = strpbrk(value, escape_chars)) {
1628 if (p > value)
1629 fwrite(value, 1, p - value, stdout);
1630 putchar('\\');
1631 putchar(*p);
1632 value = p + 1;
1633 }
1634
1635 /* print the rest and finish the double quoted
1636 string */
1637 fputs(value, stdout);
1638 printf("\" ");
1639 }
1640}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001641
1642/**
1643 * Check for option-intrapositional negation.
1644 * Do not use in new code.
1645 */
1646int xtables_check_inverse(const char option[], int *invert,
Jan Engelhardtbf971282009-11-03 19:55:11 +01001647 int *my_optind, int argc, char **argv)
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001648{
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001649 if (option == NULL || strcmp(option, "!") != 0)
1650 return false;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001651
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001652 fprintf(stderr, "Using intrapositioned negation "
1653 "(`--option ! this`) is deprecated in favor of "
1654 "extrapositioned (`! --option this`).\n");
1655
1656 if (*invert)
1657 xt_params->exit_err(PARAMETER_PROBLEM,
1658 "Multiple `!' flags not allowed");
1659 *invert = true;
1660 if (my_optind != NULL) {
Jan Engelhardtbf971282009-11-03 19:55:11 +01001661 optarg = argv[*my_optind];
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001662 ++*my_optind;
1663 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001664 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001665 "no argument following `!'");
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001666 }
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001667
1668 return true;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001669}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001670
1671const struct xtables_pprot xtables_chain_protos[] = {
1672 {"tcp", IPPROTO_TCP},
1673 {"sctp", IPPROTO_SCTP},
1674 {"udp", IPPROTO_UDP},
1675 {"udplite", IPPROTO_UDPLITE},
1676 {"icmp", IPPROTO_ICMP},
1677 {"icmpv6", IPPROTO_ICMPV6},
1678 {"ipv6-icmp", IPPROTO_ICMPV6},
1679 {"esp", IPPROTO_ESP},
1680 {"ah", IPPROTO_AH},
1681 {"ipv6-mh", IPPROTO_MH},
1682 {"mh", IPPROTO_MH},
1683 {"all", 0},
1684 {NULL},
1685};
1686
1687u_int16_t
1688xtables_parse_protocol(const char *s)
1689{
1690 unsigned int proto;
1691
1692 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1693 struct protoent *pent;
1694
1695 /* first deal with the special case of 'all' to prevent
1696 * people from being able to redefine 'all' in nsswitch
1697 * and/or provoke expensive [not working] ldap/nis/...
1698 * lookups */
1699 if (!strcmp(s, "all"))
1700 return 0;
1701
1702 if ((pent = getprotobyname(s)))
1703 proto = pent->p_proto;
1704 else {
1705 unsigned int i;
1706 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
Pablo Neira Ayusoe55cc4a2009-05-12 09:51:26 +02001707 if (xtables_chain_protos[i].name == NULL)
1708 continue;
1709
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001710 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1711 proto = xtables_chain_protos[i].num;
1712 break;
1713 }
1714 }
1715 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001716 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001717 "unknown protocol `%s' specified",
1718 s);
1719 }
1720 }
1721
1722 return proto;
1723}