blob: fab1d79d6d86412c8f76da6f400c6613d6f017aa [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>
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +020030#include <sys/statfs.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000031#include <sys/types.h>
32#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000033#include <arpa/inet.h>
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +020034#include <linux/magic.h> /* for PROC_SUPER_MAGIC */
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000035
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000036#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +010037#include <limits.h> /* INT_MAX in ip_tables.h/ip6_tables.h */
Jan Engelhardt77f48c22009-02-07 19:59:53 +010038#include <linux/netfilter_ipv4/ip_tables.h>
39#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020040#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000041
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000042#ifndef NO_SHARED_LIBS
43#include <dlfcn.h>
44#endif
Jan Engelhardtc31870f2009-02-10 10:48:28 +010045#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
46# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
47# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
48#endif
49#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
50# define IP6T_SO_GET_REVISION_MATCH 68
51# define IP6T_SO_GET_REVISION_TARGET 69
52#endif
Jamal Hadi Salim70581922009-02-13 08:36:44 -050053#include <getopt.h>
Jan Engelhardtaa37acc2011-02-07 04:00:50 +010054#include "iptables/internal.h"
Jan Engelhardt1e128bd2011-01-08 02:25:28 +010055#include "xshared.h"
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000056
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000057#define NPROTO 255
58
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000059#ifndef PROC_SYS_MODPROBE
60#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
61#endif
62
Maciej Zenczykowski37911de2011-04-05 12:42:37 +020063/* we need this for ip6?tables-restore. ip6?tables-restore.c sets line to the
64 * current line of the input file, in order to give a more precise error
65 * message. ip6?tables itself doesn't need this, so it is initialized to the
66 * magic number of -1 */
67int line = -1;
68
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010069void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
70
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010071struct xtables_globals *xt_params = NULL;
72
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010073void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010074{
75 va_list args;
76
77 va_start(args, msg);
78 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
79 vfprintf(stderr, msg, args);
80 va_end(args);
81 fprintf(stderr, "\n");
82 exit(status);
83}
84
Jan Engelhardt600f38d2010-10-29 18:57:42 +020085void xtables_free_opts(int unused)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010086{
Jan Engelhardtdf288232011-01-31 02:33:43 +010087 if (xt_params->opts != xt_params->orig_opts) {
Jan Engelhardt59e81142010-11-15 13:19:48 +010088 free(xt_params->opts);
Jan Engelhardtdf288232011-01-31 02:33:43 +010089 xt_params->opts = NULL;
90 }
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010091}
92
Jan Engelhardt600f38d2010-10-29 18:57:42 +020093struct option *xtables_merge_options(struct option *orig_opts,
94 struct option *oldopts,
Jamal Hadi Salim70581922009-02-13 08:36:44 -050095 const struct option *newopts,
96 unsigned int *option_offset)
97{
Jan Engelhardt600f38d2010-10-29 18:57:42 +020098 unsigned int num_oold = 0, num_old = 0, num_new = 0, i;
99 struct option *merge, *mp;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500100
101 if (newopts == NULL)
102 return oldopts;
103
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200104 for (num_oold = 0; orig_opts[num_oold].name; num_oold++) ;
105 if (oldopts != NULL)
106 for (num_old = 0; oldopts[num_old].name; num_old++) ;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500107 for (num_new = 0; newopts[num_new].name; num_new++) ;
108
Jan Engelhardt1dc27392011-01-08 02:10:52 +0100109 /*
110 * Since @oldopts also has @orig_opts already (and does so at the
111 * start), skip these entries.
112 */
113 oldopts += num_oold;
114 num_old -= num_oold;
115
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200116 merge = malloc(sizeof(*mp) * (num_oold + num_old + num_new + 1));
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500117 if (merge == NULL)
118 return NULL;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500119
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200120 /* Let the base options -[ADI...] have precedence over everything */
121 memcpy(merge, orig_opts, sizeof(*mp) * num_oold);
122 mp = merge + num_oold;
123
124 /* Second, the new options */
Jan Engelhardt1e128bd2011-01-08 02:25:28 +0100125 xt_params->option_offset += XT_OPTION_OFFSET_SCALE;
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200126 *option_offset = xt_params->option_offset;
127 memcpy(mp, newopts, sizeof(*mp) * num_new);
128
129 for (i = 0; i < num_new; ++i, ++mp)
130 mp->val += *option_offset;
131
132 /* Third, the old options */
133 memcpy(mp, oldopts, sizeof(*mp) * num_old);
134 mp += num_old;
135 xtables_free_opts(0);
136
137 /* Clear trailing entry */
138 memset(mp, 0, sizeof(*mp));
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500139 return merge;
140}
141
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100142static const struct xtables_afinfo afinfo_ipv4 = {
143 .kmod = "ip_tables",
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200144 .proc_exists = "/proc/net/ip_tables_names",
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100145 .libprefix = "libipt_",
146 .family = NFPROTO_IPV4,
147 .ipproto = IPPROTO_IP,
148 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
149 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
150};
151
152static const struct xtables_afinfo afinfo_ipv6 = {
153 .kmod = "ip6_tables",
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200154 .proc_exists = "/proc/net/ip6_tables_names",
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100155 .libprefix = "libip6t_",
156 .family = NFPROTO_IPV6,
157 .ipproto = IPPROTO_IPV6,
158 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
159 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
160};
161
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100162const struct xtables_afinfo *afinfo;
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100163
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100164/* Search path for Xtables .so files */
165static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000166
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000167/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100168const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000169
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200170/* Keep track of matches/targets pending full registration: linked lists. */
171struct xtables_match *xtables_pending_matches;
172struct xtables_target *xtables_pending_targets;
173
174/* Keep track of fully registered external matches/targets: linked lists. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000175struct xtables_match *xtables_matches;
176struct xtables_target *xtables_targets;
177
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200178/* Fully register a match/target which was previously partially registered. */
179static void xtables_fully_register_pending_match(struct xtables_match *me);
180static void xtables_fully_register_pending_target(struct xtables_target *me);
181
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100182void xtables_init(void)
183{
184 xtables_libdir = getenv("XTABLES_LIBDIR");
185 if (xtables_libdir != NULL)
186 return;
187 xtables_libdir = getenv("IPTABLES_LIB_DIR");
188 if (xtables_libdir != NULL) {
189 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
190 "use XTABLES_LIBDIR.\n");
191 return;
192 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100193 /*
194 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
195 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
196 * for these env vars are deprecated anyhow, and in light of the
197 * (shared) libxt_*.so files, makes less sense to have
198 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
199 */
200 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
201 if (xtables_libdir != NULL) {
202 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
203 "use XTABLES_LIBDIR.\n");
204 return;
205 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100206 xtables_libdir = XTABLES_LIBDIR;
207}
208
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100209void xtables_set_nfproto(uint8_t nfproto)
210{
211 switch (nfproto) {
212 case NFPROTO_IPV4:
213 afinfo = &afinfo_ipv4;
214 break;
215 case NFPROTO_IPV6:
216 afinfo = &afinfo_ipv6;
217 break;
218 default:
219 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
220 __func__);
221 }
222}
223
Jan Engelhardt630ef482009-01-27 14:58:41 +0100224/**
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500225 * xtables_set_params - set the global parameters used by xtables
226 * @xtp: input xtables_globals structure
227 *
228 * The app is expected to pass a valid xtables_globals data-filled
229 * with proper values
230 * @xtp cannot be NULL
231 *
232 * Returns -1 on failure to set and 0 on success
233 */
234int xtables_set_params(struct xtables_globals *xtp)
235{
236 if (!xtp) {
237 fprintf(stderr, "%s: Illegal global params\n",__func__);
238 return -1;
239 }
240
241 xt_params = xtp;
242
243 if (!xt_params->exit_err)
244 xt_params->exit_err = basic_exit_err;
245
246 return 0;
247}
248
249int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto)
250{
251 xtables_init();
252 xtables_set_nfproto(nfproto);
253 return xtables_set_params(xtp);
254}
255
256/**
Jan Engelhardt630ef482009-01-27 14:58:41 +0100257 * xtables_*alloc - wrappers that exit on failure
258 */
259void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000260{
261 void *p;
262
263 if ((p = calloc(count, size)) == NULL) {
264 perror("ip[6]tables: calloc failed");
265 exit(1);
266 }
267
268 return p;
269}
270
Jan Engelhardt630ef482009-01-27 14:58:41 +0100271void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000272{
273 void *p;
274
275 if ((p = malloc(size)) == NULL) {
276 perror("ip[6]tables: malloc failed");
277 exit(1);
278 }
279
280 return p;
281}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000282
Michael Granzow332e4ac2009-04-09 18:24:36 +0100283void *xtables_realloc(void *ptr, size_t size)
284{
285 void *p;
286
287 if ((p = realloc(ptr, size)) == NULL) {
288 perror("ip[6]tables: realloc failed");
289 exit(1);
290 }
291
292 return p;
293}
294
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000295static char *get_modprobe(void)
296{
297 int procfile;
298 char *ret;
299
300#define PROCFILE_BUFSIZ 1024
301 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
302 if (procfile < 0)
303 return NULL;
Maciej Zenczykowskia2397282011-04-04 15:30:32 +0200304 if (fcntl(procfile, F_SETFD, FD_CLOEXEC) == -1) {
305 fprintf(stderr, "Could not set close on exec: %s\n",
306 strerror(errno));
307 exit(1);
308 }
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000309
Jan Engelhardt371cea22010-07-25 23:36:17 +0200310 ret = malloc(PROCFILE_BUFSIZ);
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000311 if (ret) {
312 memset(ret, 0, PROCFILE_BUFSIZ);
313 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
314 case -1: goto fail;
315 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
316 }
317 if (ret[strlen(ret)-1]=='\n')
318 ret[strlen(ret)-1]=0;
319 close(procfile);
320 return ret;
321 }
322 fail:
323 free(ret);
324 close(procfile);
325 return NULL;
326}
327
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100328int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000329{
330 char *buf = NULL;
331 char *argv[4];
332 int status;
333
334 /* If they don't explicitly set it, read out of kernel */
335 if (!modprobe) {
336 buf = get_modprobe();
337 if (!buf)
338 return -1;
339 modprobe = buf;
340 }
341
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100342 /*
343 * Need to flush the buffer, or the child may output it again
344 * when switching the program thru execv.
345 */
346 fflush(stdout);
347
Jan Engelhardt94aa2ea2009-10-11 03:56:18 -0400348 switch (vfork()) {
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000349 case 0:
350 argv[0] = (char *)modprobe;
351 argv[1] = (char *)modname;
352 if (quiet) {
353 argv[2] = "-q";
354 argv[3] = NULL;
355 } else {
356 argv[2] = NULL;
357 argv[3] = NULL;
358 }
359 execv(argv[0], argv);
360
361 /* not usually reached */
362 exit(1);
363 case -1:
364 return -1;
365
366 default: /* parent */
367 wait(&status);
368 }
369
370 free(buf);
371 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
372 return 0;
373 return -1;
374}
375
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200376/* return true if a given file exists within procfs */
377static bool proc_file_exists(const char *filename)
378{
379 struct stat s;
380 struct statfs f;
381
382 if (lstat(filename, &s))
383 return false;
384 if (!S_ISREG(s.st_mode))
385 return false;
386 if (statfs(filename, &f))
387 return false;
388 if (f.f_type != PROC_SUPER_MAGIC)
389 return false;
390 return true;
391}
392
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100393int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000394{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100395 static bool loaded = false;
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200396 int ret;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000397
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200398 if (loaded)
399 return 0;
400
401 if (proc_file_exists(afinfo->proc_exists)) {
402 loaded = true;
403 return 0;
404 };
405
406 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
407 if (ret == 0)
408 loaded = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000409
410 return ret;
411}
412
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100413/**
414 * xtables_strtou{i,l} - string to number conversion
415 * @s: input string
416 * @end: like strtoul's "end" pointer
417 * @value: pointer for result
418 * @min: minimum accepted value
419 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000420 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100421 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
422 * "15a" is rejected.
423 * In either case, the value obtained is compared for min-max compliance.
424 * Base is always 0, i.e. autodetect depending on @s.
425 *
426 * Returns true/false whether number was accepted. On failure, *value has
427 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000428 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100429bool xtables_strtoul(const char *s, char **end, unsigned long *value,
430 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000431{
432 unsigned long v;
433 char *my_end;
434
435 errno = 0;
436 v = strtoul(s, &my_end, 0);
437
438 if (my_end == s)
439 return false;
440 if (end != NULL)
441 *end = my_end;
442
443 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
444 if (value != NULL)
445 *value = v;
446 if (end == NULL)
447 return *my_end == '\0';
448 return true;
449 }
450
451 return false;
452}
453
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100454bool xtables_strtoui(const char *s, char **end, unsigned int *value,
455 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000456{
457 unsigned long v;
458 bool ret;
459
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100460 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000461 if (value != NULL)
462 *value = v;
463 return ret;
464}
465
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100466int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000467{
468 struct servent *service;
469
470 if ((service = getservbyname(name, proto)) != NULL)
471 return ntohs((unsigned short) service->s_port);
472
473 return -1;
474}
475
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100476uint16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000477{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100478 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000479
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100480 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100481 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100482 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000483
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100484 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000485 "invalid port/service `%s' specified", port);
486}
487
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100488void xtables_parse_interface(const char *arg, char *vianame,
489 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000490{
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100491 unsigned int vialen = strlen(arg);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000492 unsigned int i;
493
494 memset(mask, 0, IFNAMSIZ);
495 memset(vianame, 0, IFNAMSIZ);
496
497 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100498 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000499 "interface name `%s' must be shorter than IFNAMSIZ"
500 " (%i)", arg, IFNAMSIZ-1);
501
502 strcpy(vianame, arg);
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100503 if (vialen == 0)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000504 memset(mask, 0, IFNAMSIZ);
505 else if (vianame[vialen - 1] == '+') {
506 memset(mask, 0xFF, vialen - 1);
507 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
508 /* Don't remove `+' here! -HW */
509 } else {
510 /* Include nul-terminator in match */
511 memset(mask, 0xFF, vialen + 1);
512 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
513 for (i = 0; vianame[i]; i++) {
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100514 if (vianame[i] == '/' ||
515 vianame[i] == ' ') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000516 fprintf(stderr,
517 "Warning: weird character in interface"
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100518 " `%s' ('/' and ' ' are not allowed by the kernel).\n",
Max Kellermannaae4f822007-10-17 16:36:49 +0000519 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000520 break;
521 }
522 }
523 }
524}
525
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200526#ifndef NO_SHARED_LIBS
Jan Engelhardt92738502011-01-30 14:18:17 +0100527static void *load_extension(const char *search_path, const char *af_prefix,
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100528 const char *name, bool is_target)
529{
Jan Engelhardt92738502011-01-30 14:18:17 +0100530 const char *all_prefixes[] = {"libxt_", af_prefix, NULL};
531 const char **prefix;
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100532 const char *dir = search_path, *next;
533 void *ptr = NULL;
534 struct stat sb;
535 char path[256];
536
537 do {
538 next = strchr(dir, ':');
539 if (next == NULL)
540 next = dir + strlen(dir);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100541
Jan Engelhardt92738502011-01-30 14:18:17 +0100542 for (prefix = all_prefixes; *prefix != NULL; ++prefix) {
543 snprintf(path, sizeof(path), "%.*s/%s%s.so",
544 (unsigned int)(next - dir), dir,
545 *prefix, name);
546
547 if (stat(path, &sb) != 0) {
548 if (errno == ENOENT)
549 continue;
550 fprintf(stderr, "%s: %s\n", path,
551 strerror(errno));
552 return NULL;
553 }
554 if (dlopen(path, RTLD_NOW) == NULL) {
555 fprintf(stderr, "%s: %s\n", path, dlerror());
556 break;
557 }
558
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100559 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100560 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100561 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100562 ptr = xtables_find_match(name,
563 XTF_DONT_LOAD, NULL);
Jan Engelhardt92738502011-01-30 14:18:17 +0100564
565 if (ptr != NULL)
566 return ptr;
567
568 fprintf(stderr, "%s: no \"%s\" extension found for "
569 "this protocol\n", path, name);
570 errno = ENOENT;
571 return NULL;
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100572 }
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100573 dir = next + 1;
574 } while (*next != '\0');
575
576 return NULL;
577}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200578#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100579
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100580struct xtables_match *
581xtables_find_match(const char *name, enum xtables_tryload tryload,
582 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000583{
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200584 struct xtables_match **dptr;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000585 struct xtables_match *ptr;
586 const char *icmp6 = "icmp6";
587
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200588 if (strlen(name) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100589 xtables_error(PARAMETER_PROBLEM,
590 "Invalid match name \"%s\" (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200591 name, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100592
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000593 /* This is ugly as hell. Nonetheless, there is no way of changing
594 * this without hurting backwards compatibility */
595 if ( (strcmp(name,"icmpv6") == 0) ||
596 (strcmp(name,"ipv6-icmp") == 0) ||
597 (strcmp(name,"icmp6") == 0) )
598 name = icmp6;
599
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200600 /* Trigger delayed initialization */
601 for (dptr = &xtables_pending_matches; *dptr; ) {
602 if (strcmp(name, (*dptr)->name) == 0) {
603 ptr = *dptr;
604 *dptr = (*dptr)->next;
605 ptr->next = NULL;
606 xtables_fully_register_pending_match(ptr);
607 } else {
608 dptr = &((*dptr)->next);
609 }
610 }
611
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000612 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
613 if (strcmp(name, ptr->name) == 0) {
614 struct xtables_match *clone;
615
616 /* First match of this type: */
617 if (ptr->m == NULL)
618 break;
619
620 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100621 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000622 memcpy(clone, ptr, sizeof(struct xtables_match));
623 clone->mflags = 0;
624 /* This is a clone: */
625 clone->next = clone;
626
627 ptr = clone;
628 break;
629 }
630 }
631
632#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100633 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100634 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100635 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000636
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100637 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100638 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000639 "Couldn't load match `%s':%s\n",
Jan Engelhardt92738502011-01-30 14:18:17 +0100640 name, strerror(errno));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000641 }
642#else
643 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100644 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000645 ptr->loaded = 1;
646 else
647 ptr = NULL;
648 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100649 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100650 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000651 "Couldn't find match `%s'\n", name);
652 }
653#endif
654
655 if (ptr && matches) {
656 struct xtables_rule_match **i;
657 struct xtables_rule_match *newentry;
658
Jan Engelhardt630ef482009-01-27 14:58:41 +0100659 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000660
661 for (i = matches; *i; i = &(*i)->next) {
662 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100663 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000664 }
665 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100666 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000667 newentry->next = NULL;
668 *i = newentry;
669 }
670
671 return ptr;
672}
673
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100674struct xtables_target *
675xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000676{
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200677 struct xtables_target **dptr;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000678 struct xtables_target *ptr;
679
680 /* Standard target? */
681 if (strcmp(name, "") == 0
682 || strcmp(name, XTC_LABEL_ACCEPT) == 0
683 || strcmp(name, XTC_LABEL_DROP) == 0
684 || strcmp(name, XTC_LABEL_QUEUE) == 0
685 || strcmp(name, XTC_LABEL_RETURN) == 0)
686 name = "standard";
687
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200688 /* Trigger delayed initialization */
689 for (dptr = &xtables_pending_targets; *dptr; ) {
690 if (strcmp(name, (*dptr)->name) == 0) {
691 ptr = *dptr;
692 *dptr = (*dptr)->next;
693 ptr->next = NULL;
694 xtables_fully_register_pending_target(ptr);
695 } else {
696 dptr = &((*dptr)->next);
697 }
698 }
699
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000700 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
701 if (strcmp(name, ptr->name) == 0)
702 break;
703 }
704
705#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100706 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100707 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100708 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000709
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100710 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100711 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000712 "Couldn't load target `%s':%s\n",
Jan Engelhardt92738502011-01-30 14:18:17 +0100713 name, strerror(errno));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000714 }
715#else
716 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100717 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000718 ptr->loaded = 1;
719 else
720 ptr = NULL;
721 }
Peter Volkov854d2d92009-03-24 11:09:16 +0300722 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100723 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000724 "Couldn't find target `%s'\n", name);
725 }
726#endif
727
728 if (ptr)
729 ptr->used = 1;
730
731 return ptr;
732}
733
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100734static int compatible_revision(const char *name, uint8_t revision, int opt)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000735{
736 struct xt_get_revision rev;
737 socklen_t s = sizeof(rev);
738 int max_rev, sockfd;
739
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100740 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000741 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000742 if (errno == EPERM) {
743 /* revision 0 is always supported. */
744 if (revision != 0)
745 fprintf(stderr, "Could not determine whether "
746 "revision %u is supported, "
747 "assuming it is.\n",
748 revision);
749 return 1;
750 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000751 fprintf(stderr, "Could not open socket to kernel: %s\n",
752 strerror(errno));
753 exit(1);
754 }
755
Maciej Zenczykowskia2397282011-04-04 15:30:32 +0200756 if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
757 fprintf(stderr, "Could not set close on exec: %s\n",
758 strerror(errno));
759 exit(1);
760 }
761
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100762 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000763
764 strcpy(rev.name, name);
765 rev.revision = revision;
766
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100767 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000768 if (max_rev < 0) {
769 /* Definitely don't support this? */
770 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
771 close(sockfd);
772 return 0;
773 } else if (errno == ENOPROTOOPT) {
774 close(sockfd);
775 /* Assume only revision 0 support (old kernel) */
776 return (revision == 0);
777 } else {
778 fprintf(stderr, "getsockopt failed strangely: %s\n",
779 strerror(errno));
780 exit(1);
781 }
782 }
783 close(sockfd);
784 return 1;
785}
786
787
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100788static int compatible_match_revision(const char *name, uint8_t revision)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000789{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100790 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000791}
792
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100793static int compatible_target_revision(const char *name, uint8_t revision)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000794{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100795 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000796}
797
Jan Engelhardtdfbedfe2011-01-08 03:31:04 +0100798static void xtables_check_options(const char *name, const struct option *opt)
799{
800 for (; opt->name != NULL; ++opt)
801 if (opt->val < 0 || opt->val >= XT_OPTION_OFFSET_SCALE) {
802 fprintf(stderr, "%s: Extension %s uses invalid "
803 "option value %d\n",xt_params->program_name,
804 name, opt->val);
805 exit(1);
806 }
807}
808
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000809void xtables_register_match(struct xtables_match *me)
810{
Jan Engelhardtc284de52009-06-25 21:25:24 +0200811 if (me->version == NULL) {
812 fprintf(stderr, "%s: match %s<%u> is missing a version\n",
813 xt_params->program_name, me->name, me->revision);
814 exit(1);
815 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100816 if (strcmp(me->version, XTABLES_VERSION) != 0) {
817 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
818 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500819 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100820 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000821 exit(1);
822 }
823
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200824 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Li Yewang281439b2011-01-09 22:26:58 +0100825 fprintf(stderr, "%s: match `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500826 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000827 exit(1);
828 }
829
830 if (me->family >= NPROTO) {
831 fprintf(stderr,
832 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500833 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000834 exit(1);
835 }
836
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100837 if (me->x6_options != NULL)
838 xtables_option_metavalidate(me->name, me->x6_options);
Jan Engelhardtdfbedfe2011-01-08 03:31:04 +0100839 if (me->extra_opts != NULL)
840 xtables_check_options(me->name, me->extra_opts);
841
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000842 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100843 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000844 return;
845
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200846 /* place on linked list of matches pending full registration */
847 me->next = xtables_pending_matches;
848 xtables_pending_matches = me;
849}
850
851static void xtables_fully_register_pending_match(struct xtables_match *me)
852{
853 struct xtables_match **i, *old;
854
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100855 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000856 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100857 if (old->revision == me->revision &&
858 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000859 fprintf(stderr,
860 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500861 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000862 exit(1);
863 }
864
865 /* Now we have two (or more) options, check compatibility. */
866 if (compatible_match_revision(old->name, old->revision)
867 && old->revision > me->revision)
868 return;
869
Jan Engelhardt23545c22008-02-14 04:23:04 +0100870 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000871 if (!compatible_match_revision(me->name, me->revision))
872 return;
873
Jan Engelhardt23545c22008-02-14 04:23:04 +0100874 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
875 if (old->revision == me->revision && me->family == AF_UNSPEC)
876 return;
877
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000878 /* Delete old one. */
879 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
880 *i = old->next;
881 }
882
883 if (me->size != XT_ALIGN(me->size)) {
884 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500885 xt_params->program_name, me->name,
886 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000887 exit(1);
888 }
889
890 /* Append to list. */
891 for (i = &xtables_matches; *i; i = &(*i)->next);
892 me->next = NULL;
893 *i = me;
894
895 me->m = NULL;
896 me->mflags = 0;
897}
898
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200899void xtables_register_matches(struct xtables_match *match, unsigned int n)
900{
901 do {
902 xtables_register_match(&match[--n]);
903 } while (n > 0);
904}
905
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000906void xtables_register_target(struct xtables_target *me)
907{
Jan Engelhardtc284de52009-06-25 21:25:24 +0200908 if (me->version == NULL) {
909 fprintf(stderr, "%s: target %s<%u> is missing a version\n",
910 xt_params->program_name, me->name, me->revision);
911 exit(1);
912 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100913 if (strcmp(me->version, XTABLES_VERSION) != 0) {
914 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
915 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500916 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100917 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000918 exit(1);
919 }
920
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200921 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000922 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500923 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000924 exit(1);
925 }
926
927 if (me->family >= NPROTO) {
928 fprintf(stderr,
929 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500930 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000931 exit(1);
932 }
933
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100934 if (me->x6_options != NULL)
935 xtables_option_metavalidate(me->name, me->x6_options);
Jan Engelhardtdfbedfe2011-01-08 03:31:04 +0100936 if (me->extra_opts != NULL)
937 xtables_check_options(me->name, me->extra_opts);
938
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000939 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100940 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000941 return;
942
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200943 /* place on linked list of targets pending full registration */
944 me->next = xtables_pending_targets;
945 xtables_pending_targets = me;
946}
947
948static void xtables_fully_register_pending_target(struct xtables_target *me)
949{
950 struct xtables_target *old;
951
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100952 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000953 if (old) {
954 struct xtables_target **i;
955
Jan Engelhardt23545c22008-02-14 04:23:04 +0100956 if (old->revision == me->revision &&
957 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000958 fprintf(stderr,
959 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500960 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000961 exit(1);
962 }
963
964 /* Now we have two (or more) options, check compatibility. */
965 if (compatible_target_revision(old->name, old->revision)
966 && old->revision > me->revision)
967 return;
968
Jan Engelhardt23545c22008-02-14 04:23:04 +0100969 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000970 if (!compatible_target_revision(me->name, me->revision))
971 return;
972
Jan Engelhardt23545c22008-02-14 04:23:04 +0100973 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
974 if (old->revision == me->revision && me->family == AF_UNSPEC)
975 return;
976
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000977 /* Delete old one. */
978 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
979 *i = old->next;
980 }
981
982 if (me->size != XT_ALIGN(me->size)) {
983 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500984 xt_params->program_name, me->name,
985 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000986 exit(1);
987 }
988
989 /* Prepend to list. */
990 me->next = xtables_targets;
991 xtables_targets = me;
992 me->t = NULL;
993 me->tflags = 0;
994}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000995
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200996void xtables_register_targets(struct xtables_target *target, unsigned int n)
997{
998 do {
999 xtables_register_target(&target[--n]);
1000 } while (n > 0);
1001}
1002
Jan Engelhardta41545c2009-01-27 21:27:19 +01001003/**
1004 * xtables_param_act - act on condition
1005 * @status: a constant from enum xtables_exittype
1006 *
1007 * %XTF_ONLY_ONCE: print error message that option may only be used once.
1008 * @p1: module name (e.g. "mark")
1009 * @p2(...): option in conflict (e.g. "--mark")
1010 * @p3(...): condition to match on (see extensions/ for examples)
1011 *
1012 * %XTF_NO_INVERT: option does not support inversion
1013 * @p1: module name
1014 * @p2: option in conflict
1015 * @p3: condition to match on
1016 *
1017 * %XTF_BAD_VALUE: bad value for option
1018 * @p1: module name
1019 * @p2: option with which the problem occured (e.g. "--mark")
1020 * @p3: string the user passed in (e.g. "99999999999999")
1021 *
1022 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
1023 * @p1: module name
1024 *
1025 * Displays an error message and exits the program.
1026 */
1027void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001028{
1029 const char *p2, *p3;
1030 va_list args;
1031 bool b;
1032
1033 va_start(args, p1);
1034
1035 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +01001036 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001037 p2 = va_arg(args, const char *);
1038 b = va_arg(args, unsigned int);
1039 if (!b)
1040 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001041 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001042 "%s: \"%s\" option may only be specified once",
1043 p1, p2);
1044 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +01001045 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001046 p2 = va_arg(args, const char *);
1047 b = va_arg(args, unsigned int);
1048 if (!b)
1049 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001050 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001051 "%s: \"%s\" option cannot be inverted", p1, p2);
1052 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +01001053 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001054 p2 = va_arg(args, const char *);
1055 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001056 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001057 "%s: Bad value for \"%s\" option: \"%s\"",
1058 p1, p2, p3);
1059 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +01001060 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001061 b = va_arg(args, unsigned int);
1062 if (!b)
1063 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001064 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001065 "%s: At most one action is possible", p1);
1066 break;
1067 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001068 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001069 break;
1070 }
1071
1072 va_end(args);
1073}
Jan Engelhardt08b16162008-01-20 13:36:08 +00001074
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001075const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001076{
1077 static char buf[20];
1078 const unsigned char *bytep = (const void *)&addrp->s_addr;
1079
1080 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
1081 return buf;
1082}
1083
1084static const char *ipaddr_to_host(const struct in_addr *addr)
1085{
1086 struct hostent *host;
1087
1088 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
1089 if (host == NULL)
1090 return NULL;
1091
1092 return host->h_name;
1093}
1094
1095static const char *ipaddr_to_network(const struct in_addr *addr)
1096{
1097 struct netent *net;
1098
1099 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
1100 return net->n_name;
1101
1102 return NULL;
1103}
1104
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001105const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001106{
1107 const char *name;
1108
1109 if ((name = ipaddr_to_host(addr)) != NULL ||
1110 (name = ipaddr_to_network(addr)) != NULL)
1111 return name;
1112
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001113 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001114}
1115
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001116const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001117{
1118 static char buf[20];
1119 uint32_t maskaddr, bits;
1120 int i;
1121
1122 maskaddr = ntohl(mask->s_addr);
1123
1124 if (maskaddr == 0xFFFFFFFFL)
1125 /* we don't want to see "/32" */
1126 return "";
1127
1128 i = 32;
1129 bits = 0xFFFFFFFEL;
1130 while (--i >= 0 && maskaddr != bits)
1131 bits <<= 1;
1132 if (i >= 0)
1133 sprintf(buf, "/%d", i);
1134 else
1135 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001136 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001137
1138 return buf;
1139}
1140
Jan Engelhardtbd943842008-01-20 13:38:08 +00001141static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1142{
1143 static struct in_addr addr;
1144 unsigned char *addrp;
1145 unsigned int onebyte;
1146 char buf[20], *p, *q;
1147 int i;
1148
1149 /* copy dotted string, because we need to modify it */
1150 strncpy(buf, dotted, sizeof(buf) - 1);
1151 buf[sizeof(buf) - 1] = '\0';
1152 addrp = (void *)&addr.s_addr;
1153
1154 p = buf;
1155 for (i = 0; i < 3; ++i) {
1156 if ((q = strchr(p, '.')) == NULL) {
1157 if (is_mask)
1158 return NULL;
1159
1160 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001161 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001162 return NULL;
1163
1164 addrp[i] = onebyte;
1165 while (i < 3)
1166 addrp[++i] = 0;
1167
1168 return &addr;
1169 }
1170
1171 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001172 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001173 return NULL;
1174
1175 addrp[i] = onebyte;
1176 p = q + 1;
1177 }
1178
1179 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001180 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001181 return NULL;
1182
1183 addrp[3] = onebyte;
1184 return &addr;
1185}
1186
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001187struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001188{
1189 return __numeric_to_ipaddr(dotted, false);
1190}
1191
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001192struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001193{
1194 return __numeric_to_ipaddr(dotted, true);
1195}
1196
1197static struct in_addr *network_to_ipaddr(const char *name)
1198{
1199 static struct in_addr addr;
1200 struct netent *net;
1201
1202 if ((net = getnetbyname(name)) != NULL) {
1203 if (net->n_addrtype != AF_INET)
1204 return NULL;
1205 addr.s_addr = htonl(net->n_net);
1206 return &addr;
1207 }
1208
1209 return NULL;
1210}
1211
1212static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1213{
1214 struct hostent *host;
1215 struct in_addr *addr;
1216 unsigned int i;
1217
1218 *naddr = 0;
1219 if ((host = gethostbyname(name)) != NULL) {
1220 if (host->h_addrtype != AF_INET ||
1221 host->h_length != sizeof(struct in_addr))
1222 return NULL;
1223
1224 while (host->h_addr_list[*naddr] != NULL)
1225 ++*naddr;
Wes Campaigne11e250b2011-02-21 19:10:11 -05001226 addr = xtables_calloc(*naddr, sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001227 for (i = 0; i < *naddr; i++)
1228 memcpy(&addr[i], host->h_addr_list[i],
1229 sizeof(struct in_addr));
1230 return addr;
1231 }
1232
1233 return NULL;
1234}
1235
1236static struct in_addr *
1237ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1238{
1239 struct in_addr *addrptmp, *addrp;
1240
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001241 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001242 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001243 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001244 memcpy(addrp, addrptmp, sizeof(*addrp));
1245 *naddrs = 1;
1246 return addrp;
1247 }
1248 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1249 return addrptmp;
1250
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001251 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001252}
1253
1254static struct in_addr *parse_ipmask(const char *mask)
1255{
1256 static struct in_addr maskaddr;
1257 struct in_addr *addrp;
1258 unsigned int bits;
1259
1260 if (mask == NULL) {
1261 /* no mask at all defaults to 32 bits */
1262 maskaddr.s_addr = 0xFFFFFFFF;
1263 return &maskaddr;
1264 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001265 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001266 /* dotted_to_addr already returns a network byte order addr */
1267 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001268 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001269 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001270 "invalid mask `%s' specified", mask);
1271 if (bits != 0) {
1272 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1273 return &maskaddr;
1274 }
1275
1276 maskaddr.s_addr = 0U;
1277 return &maskaddr;
1278}
1279
Michael Granzow332e4ac2009-04-09 18:24:36 +01001280void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp,
1281 struct in_addr **maskpp, unsigned int *naddrs)
1282{
1283 struct in_addr *addrp;
1284 char buf[256], *p;
1285 unsigned int len, i, j, n, count = 1;
1286 const char *loop = name;
1287
1288 while ((loop = strchr(loop, ',')) != NULL) {
1289 ++count;
1290 ++loop; /* skip ',' */
1291 }
1292
1293 *addrpp = xtables_malloc(sizeof(struct in_addr) * count);
1294 *maskpp = xtables_malloc(sizeof(struct in_addr) * count);
1295
1296 loop = name;
1297
1298 for (i = 0; i < count; ++i) {
1299 if (loop == NULL)
1300 break;
1301 if (*loop == ',')
1302 ++loop;
1303 if (*loop == '\0')
1304 break;
1305 p = strchr(loop, ',');
1306 if (p != NULL)
1307 len = p - loop;
1308 else
1309 len = strlen(loop);
1310 if (len == 0 || sizeof(buf) - 1 < len)
1311 break;
1312
1313 strncpy(buf, loop, len);
1314 buf[len] = '\0';
1315 loop += len;
1316 if ((p = strrchr(buf, '/')) != NULL) {
1317 *p = '\0';
1318 addrp = parse_ipmask(p + 1);
1319 } else {
1320 addrp = parse_ipmask(NULL);
1321 }
1322 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1323
1324 /* if a null mask is given, the name is ignored, like in "any/0" */
1325 if ((*maskpp + i)->s_addr == 0)
1326 /*
1327 * A bit pointless to process multiple addresses
1328 * in this case...
1329 */
1330 strcpy(buf, "0.0.0.0");
1331
1332 addrp = ipparse_hostnetwork(buf, &n);
1333 if (n > 1) {
1334 count += n - 1;
1335 *addrpp = xtables_realloc(*addrpp,
1336 sizeof(struct in_addr) * count);
1337 *maskpp = xtables_realloc(*maskpp,
1338 sizeof(struct in_addr) * count);
1339 for (j = 0; j < n; ++j)
1340 /* for each new addr */
1341 memcpy(*addrpp + i + j, addrp + j,
1342 sizeof(*addrp));
1343 for (j = 1; j < n; ++j)
1344 /* for each new mask */
1345 memcpy(*maskpp + i + j, *maskpp + i,
1346 sizeof(*addrp));
1347 i += n - 1;
1348 } else {
1349 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1350 }
1351 /* free what ipparse_hostnetwork had allocated: */
1352 free(addrp);
1353 }
1354 *naddrs = count;
Jan Engelhardt4b110b42011-02-21 03:21:18 +01001355 for (i = 0; i < count; ++i)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001356 (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr;
1357}
1358
1359
Jan Engelhardta0baae82009-01-30 04:32:50 +01001360/**
1361 * xtables_ipparse_any - transform arbitrary name to in_addr
1362 *
1363 * Possible inputs (pseudo regex):
1364 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1365 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1366 */
1367void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1368 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001369{
1370 unsigned int i, j, k, n;
1371 struct in_addr *addrp;
1372 char buf[256], *p;
1373
1374 strncpy(buf, name, sizeof(buf) - 1);
1375 buf[sizeof(buf) - 1] = '\0';
1376 if ((p = strrchr(buf, '/')) != NULL) {
1377 *p = '\0';
1378 addrp = parse_ipmask(p + 1);
1379 } else {
1380 addrp = parse_ipmask(NULL);
1381 }
1382 memcpy(maskp, addrp, sizeof(*maskp));
1383
1384 /* if a null mask is given, the name is ignored, like in "any/0" */
1385 if (maskp->s_addr == 0U)
1386 strcpy(buf, "0.0.0.0");
1387
1388 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1389 n = *naddrs;
1390 for (i = 0, j = 0; i < n; ++i) {
1391 addrp[j++].s_addr &= maskp->s_addr;
1392 for (k = 0; k < j - 1; ++k)
1393 if (addrp[k].s_addr == addrp[j-1].s_addr) {
Wes Campaigneadcb2812011-02-21 19:10:12 -05001394 /*
1395 * Nuke the dup by copying an address from the
1396 * tail here, and check the current position
1397 * again (--j).
1398 */
1399 memcpy(&addrp[--j], &addrp[--*naddrs],
1400 sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001401 break;
1402 }
1403 }
1404}
1405
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001406const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001407{
Maciej Zenczykowskicf3e52d2011-04-04 15:31:09 +02001408 /* 0000:0000:0000:0000:0000:0000:000.000.000.000
Jan Engelhardt08b16162008-01-20 13:36:08 +00001409 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1410 static char buf[50+1];
1411 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1412}
1413
1414static const char *ip6addr_to_host(const struct in6_addr *addr)
1415{
1416 static char hostname[NI_MAXHOST];
1417 struct sockaddr_in6 saddr;
1418 int err;
1419
1420 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1421 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1422 saddr.sin6_family = AF_INET6;
1423
1424 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1425 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1426 if (err != 0) {
1427#ifdef DEBUG
1428 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1429#endif
1430 return NULL;
1431 }
1432
1433#ifdef DEBUG
1434 fprintf (stderr, "\naddr2host: %s\n", hostname);
1435#endif
1436 return hostname;
1437}
1438
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001439const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001440{
1441 const char *name;
1442
1443 if ((name = ip6addr_to_host(addr)) != NULL)
1444 return name;
1445
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001446 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001447}
1448
1449static int ip6addr_prefix_length(const struct in6_addr *k)
1450{
1451 unsigned int bits = 0;
1452 uint32_t a, b, c, d;
1453
Jan Engelhardt48607812008-06-10 15:17:53 +02001454 a = ntohl(k->s6_addr32[0]);
1455 b = ntohl(k->s6_addr32[1]);
1456 c = ntohl(k->s6_addr32[2]);
1457 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001458 while (a & 0x80000000U) {
1459 ++bits;
1460 a <<= 1;
1461 a |= (b >> 31) & 1;
1462 b <<= 1;
1463 b |= (c >> 31) & 1;
1464 c <<= 1;
1465 c |= (d >> 31) & 1;
1466 d <<= 1;
1467 }
1468 if (a != 0 || b != 0 || c != 0 || d != 0)
1469 return -1;
1470 return bits;
1471}
1472
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001473const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001474{
1475 static char buf[50+2];
1476 int l = ip6addr_prefix_length(addrp);
1477
1478 if (l == -1) {
1479 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001480 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001481 return buf;
1482 }
1483 sprintf(buf, "/%d", l);
1484 return buf;
1485}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001486
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001487struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001488{
1489 static struct in6_addr ap;
1490 int err;
1491
1492 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1493 return &ap;
1494#ifdef DEBUG
1495 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1496#endif
1497 return NULL;
1498}
1499
1500static struct in6_addr *
1501host_to_ip6addr(const char *name, unsigned int *naddr)
1502{
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001503 struct in6_addr *addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001504 struct addrinfo hints;
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001505 struct addrinfo *res, *p;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001506 int err;
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001507 unsigned int i;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001508
1509 memset(&hints, 0, sizeof(hints));
1510 hints.ai_flags = AI_CANONNAME;
1511 hints.ai_family = AF_INET6;
1512 hints.ai_socktype = SOCK_RAW;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001513
1514 *naddr = 0;
1515 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1516#ifdef DEBUG
1517 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1518#endif
1519 return NULL;
1520 } else {
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001521 /* Find length of address chain */
1522 for (p = res; p != NULL; p = p->ai_next)
1523 ++*naddr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001524#ifdef DEBUG
1525 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
Patrick McHardy30290ae2010-05-20 15:41:03 +02001526 xtables_ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001527#endif
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001528 /* Copy each element of the address chain */
1529 addr = xtables_calloc(*naddr, sizeof(struct in6_addr));
1530 for (i = 0, p = res; p != NULL; p = p->ai_next)
1531 memcpy(&addr[i++],
1532 &((const struct sockaddr_in6 *)p->ai_addr)->sin6_addr,
1533 sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001534 freeaddrinfo(res);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001535 return addr;
1536 }
1537
1538 return NULL;
1539}
1540
1541static struct in6_addr *network_to_ip6addr(const char *name)
1542{
1543 /* abort();*/
1544 /* TODO: not implemented yet, but the exception breaks the
1545 * name resolvation */
1546 return NULL;
1547}
1548
1549static struct in6_addr *
1550ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1551{
1552 struct in6_addr *addrp, *addrptmp;
1553
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001554 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001555 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001556 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001557 memcpy(addrp, addrptmp, sizeof(*addrp));
1558 *naddrs = 1;
1559 return addrp;
1560 }
1561 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1562 return addrp;
1563
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001564 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001565}
1566
1567static struct in6_addr *parse_ip6mask(char *mask)
1568{
1569 static struct in6_addr maskaddr;
1570 struct in6_addr *addrp;
1571 unsigned int bits;
1572
1573 if (mask == NULL) {
1574 /* no mask at all defaults to 128 bits */
1575 memset(&maskaddr, 0xff, sizeof maskaddr);
1576 return &maskaddr;
1577 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001578 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001579 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001580 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001581 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001582 "invalid mask `%s' specified", mask);
1583 if (bits != 0) {
1584 char *p = (void *)&maskaddr;
1585 memset(p, 0xff, bits / 8);
1586 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1587 p[bits/8] = 0xff << (8 - (bits & 7));
1588 return &maskaddr;
1589 }
1590
1591 memset(&maskaddr, 0, sizeof(maskaddr));
1592 return &maskaddr;
1593}
1594
Michael Granzow332e4ac2009-04-09 18:24:36 +01001595void
1596xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp,
1597 struct in6_addr **maskpp, unsigned int *naddrs)
1598{
Olaf Rempel58df9012009-09-20 13:24:11 +02001599 static const struct in6_addr zero_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001600 struct in6_addr *addrp;
1601 char buf[256], *p;
1602 unsigned int len, i, j, n, count = 1;
1603 const char *loop = name;
1604
1605 while ((loop = strchr(loop, ',')) != NULL) {
1606 ++count;
1607 ++loop; /* skip ',' */
1608 }
1609
1610 *addrpp = xtables_malloc(sizeof(struct in6_addr) * count);
1611 *maskpp = xtables_malloc(sizeof(struct in6_addr) * count);
1612
1613 loop = name;
1614
1615 for (i = 0; i < count /*NB: count can grow*/; ++i) {
1616 if (loop == NULL)
1617 break;
1618 if (*loop == ',')
1619 ++loop;
1620 if (*loop == '\0')
1621 break;
1622 p = strchr(loop, ',');
1623 if (p != NULL)
1624 len = p - loop;
1625 else
1626 len = strlen(loop);
1627 if (len == 0 || sizeof(buf) - 1 < len)
1628 break;
1629
1630 strncpy(buf, loop, len);
1631 buf[len] = '\0';
1632 loop += len;
1633 if ((p = strrchr(buf, '/')) != NULL) {
1634 *p = '\0';
1635 addrp = parse_ip6mask(p + 1);
1636 } else {
1637 addrp = parse_ip6mask(NULL);
1638 }
1639 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1640
1641 /* if a null mask is given, the name is ignored, like in "any/0" */
Olaf Rempel58df9012009-09-20 13:24:11 +02001642 if (memcmp(*maskpp + i, &zero_addr, sizeof(zero_addr)) == 0)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001643 strcpy(buf, "::");
1644
1645 addrp = ip6parse_hostnetwork(buf, &n);
Michael Granzow332e4ac2009-04-09 18:24:36 +01001646 if (n > 1) {
1647 count += n - 1;
1648 *addrpp = xtables_realloc(*addrpp,
1649 sizeof(struct in6_addr) * count);
1650 *maskpp = xtables_realloc(*maskpp,
1651 sizeof(struct in6_addr) * count);
1652 for (j = 0; j < n; ++j)
1653 /* for each new addr */
1654 memcpy(*addrpp + i + j, addrp + j,
1655 sizeof(*addrp));
1656 for (j = 1; j < n; ++j)
1657 /* for each new mask */
1658 memcpy(*maskpp + i + j, *maskpp + i,
1659 sizeof(*addrp));
1660 i += n - 1;
1661 } else {
1662 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1663 }
1664 /* free what ip6parse_hostnetwork had allocated: */
1665 free(addrp);
1666 }
1667 *naddrs = count;
Jan Engelhardt4b110b42011-02-21 03:21:18 +01001668 for (i = 0; i < count; ++i)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001669 for (j = 0; j < 4; ++j)
1670 (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j];
1671}
1672
Jan Engelhardta0baae82009-01-30 04:32:50 +01001673void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1674 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001675{
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001676 static const struct in6_addr zero_addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001677 struct in6_addr *addrp;
1678 unsigned int i, j, k, n;
1679 char buf[256], *p;
1680
1681 strncpy(buf, name, sizeof(buf) - 1);
1682 buf[sizeof(buf)-1] = '\0';
1683 if ((p = strrchr(buf, '/')) != NULL) {
1684 *p = '\0';
1685 addrp = parse_ip6mask(p + 1);
1686 } else {
1687 addrp = parse_ip6mask(NULL);
1688 }
1689 memcpy(maskp, addrp, sizeof(*maskp));
1690
1691 /* if a null mask is given, the name is ignored, like in "any/0" */
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001692 if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001693 strcpy(buf, "::");
1694
1695 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1696 n = *naddrs;
1697 for (i = 0, j = 0; i < n; ++i) {
1698 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001699 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001700 ++j;
1701 for (k = 0; k < j - 1; ++k)
1702 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Wes Campaigneadcb2812011-02-21 19:10:12 -05001703 /*
1704 * Nuke the dup by copying an address from the
1705 * tail here, and check the current position
1706 * again (--j).
1707 */
1708 memcpy(&addrp[--j], &addrp[--*naddrs],
1709 sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001710 break;
1711 }
1712 }
1713}
Max Kellermanna5d09942008-01-29 13:44:34 +00001714
Jan Engelhardta0baae82009-01-30 04:32:50 +01001715void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001716{
1717 static const char no_quote_chars[] = "_-0123456789"
1718 "abcdefghijklmnopqrstuvwxyz"
1719 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1720 static const char escape_chars[] = "\"\\'";
1721 size_t length;
1722 const char *p;
1723
Max Kellerman87dc7c42011-02-17 11:57:19 +01001724 length = strspn(value, no_quote_chars);
Max Kellermanna5d09942008-01-29 13:44:34 +00001725 if (length > 0 && value[length] == 0) {
1726 /* no quoting required */
Max Kellermanna5d09942008-01-29 13:44:34 +00001727 putchar(' ');
Jan Engelhardt73866352010-12-18 02:04:59 +01001728 fputs(value, stdout);
Max Kellermanna5d09942008-01-29 13:44:34 +00001729 } else {
1730 /* there is at least one dangerous character in the
1731 value, which we have to quote. Write double quotes
1732 around the value and escape special characters with
1733 a backslash */
Jan Engelhardt73866352010-12-18 02:04:59 +01001734 printf(" \"");
Max Kellermanna5d09942008-01-29 13:44:34 +00001735
1736 for (p = strpbrk(value, escape_chars); p != NULL;
1737 p = strpbrk(value, escape_chars)) {
1738 if (p > value)
1739 fwrite(value, 1, p - value, stdout);
1740 putchar('\\');
1741 putchar(*p);
1742 value = p + 1;
1743 }
1744
1745 /* print the rest and finish the double quoted
1746 string */
1747 fputs(value, stdout);
Jan Engelhardt73866352010-12-18 02:04:59 +01001748 putchar('\"');
Max Kellermanna5d09942008-01-29 13:44:34 +00001749 }
1750}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001751
1752/**
1753 * Check for option-intrapositional negation.
1754 * Do not use in new code.
1755 */
1756int xtables_check_inverse(const char option[], int *invert,
Jan Engelhardtbf971282009-11-03 19:55:11 +01001757 int *my_optind, int argc, char **argv)
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001758{
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001759 if (option == NULL || strcmp(option, "!") != 0)
1760 return false;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001761
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001762 fprintf(stderr, "Using intrapositioned negation "
1763 "(`--option ! this`) is deprecated in favor of "
1764 "extrapositioned (`! --option this`).\n");
1765
1766 if (*invert)
1767 xt_params->exit_err(PARAMETER_PROBLEM,
1768 "Multiple `!' flags not allowed");
1769 *invert = true;
1770 if (my_optind != NULL) {
Jan Engelhardtbf971282009-11-03 19:55:11 +01001771 optarg = argv[*my_optind];
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001772 ++*my_optind;
1773 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001774 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001775 "no argument following `!'");
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001776 }
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001777
1778 return true;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001779}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001780
1781const struct xtables_pprot xtables_chain_protos[] = {
1782 {"tcp", IPPROTO_TCP},
1783 {"sctp", IPPROTO_SCTP},
1784 {"udp", IPPROTO_UDP},
1785 {"udplite", IPPROTO_UDPLITE},
1786 {"icmp", IPPROTO_ICMP},
1787 {"icmpv6", IPPROTO_ICMPV6},
1788 {"ipv6-icmp", IPPROTO_ICMPV6},
1789 {"esp", IPPROTO_ESP},
1790 {"ah", IPPROTO_AH},
1791 {"ipv6-mh", IPPROTO_MH},
1792 {"mh", IPPROTO_MH},
1793 {"all", 0},
1794 {NULL},
1795};
1796
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001797uint16_t
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001798xtables_parse_protocol(const char *s)
1799{
1800 unsigned int proto;
1801
1802 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1803 struct protoent *pent;
1804
1805 /* first deal with the special case of 'all' to prevent
1806 * people from being able to redefine 'all' in nsswitch
1807 * and/or provoke expensive [not working] ldap/nis/...
1808 * lookups */
1809 if (!strcmp(s, "all"))
1810 return 0;
1811
1812 if ((pent = getprotobyname(s)))
1813 proto = pent->p_proto;
1814 else {
1815 unsigned int i;
1816 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
Pablo Neira Ayusoe55cc4a2009-05-12 09:51:26 +02001817 if (xtables_chain_protos[i].name == NULL)
1818 continue;
1819
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001820 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1821 proto = xtables_chain_protos[i].num;
1822 break;
1823 }
1824 }
1825 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001826 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001827 "unknown protocol `%s' specified",
1828 s);
1829 }
1830 }
1831
1832 return proto;
1833}