blob: 50cfced86460c11eb1ca7866e481567fb0bd5e5d [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 Engelhardt77f48c22009-02-07 19:59:53 +010035#include <linux/netfilter_ipv4/ip_tables.h>
36#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020037#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000038
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000039#ifndef NO_SHARED_LIBS
40#include <dlfcn.h>
41#endif
Jan Engelhardtc31870f2009-02-10 10:48:28 +010042#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
43# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
44# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
45#endif
46#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
47# define IP6T_SO_GET_REVISION_MATCH 68
48# define IP6T_SO_GET_REVISION_TARGET 69
49#endif
Jamal Hadi Salim70581922009-02-13 08:36:44 -050050#include <getopt.h>
Jan Engelhardtc31870f2009-02-10 10:48:28 +010051
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000052
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000053#define NPROTO 255
54
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000055#ifndef PROC_SYS_MODPROBE
56#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
57#endif
58
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010059void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
60
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010061struct xtables_globals *xt_params = NULL;
62
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010063void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010064{
65 va_list args;
66
67 va_start(args, msg);
68 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
69 vfprintf(stderr, msg, args);
70 va_end(args);
71 fprintf(stderr, "\n");
72 exit(status);
73}
74
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010075
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -050076void xtables_free_opts(int reset_offset)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010077{
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -050078 if (xt_params->opts != xt_params->orig_opts) {
79 free(xt_params->opts);
80 xt_params->opts = xt_params->orig_opts;
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010081 if (reset_offset)
82 xt_params->option_offset = 0;
83 }
84}
85
Jamal Hadi Salim70581922009-02-13 08:36:44 -050086struct option *xtables_merge_options(struct option *oldopts,
87 const struct option *newopts,
88 unsigned int *option_offset)
89{
90 unsigned int num_old, num_new, i;
91 struct option *merge;
92
93 if (newopts == NULL)
94 return oldopts;
95
96 for (num_old = 0; oldopts[num_old].name; num_old++) ;
97 for (num_new = 0; newopts[num_new].name; num_new++) ;
98
99 xt_params->option_offset += OPTION_OFFSET;
100 *option_offset = xt_params->option_offset;
101
102 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
103 if (merge == NULL)
104 return NULL;
105 memcpy(merge, oldopts, num_old * sizeof(struct option));
106 xtables_free_opts(0); /* Release any old options merged */
107 for (i = 0; i < num_new; i++) {
108 merge[num_old + i] = newopts[i];
109 merge[num_old + i].val += *option_offset;
110 }
111 memset(merge + num_old + num_new, 0, sizeof(struct option));
112
113 return merge;
114}
115
Jamal Hadi Salim85332212009-02-12 09:33:59 -0500116void xtables_set_revision(char *name, u_int8_t revision)
117{
118 /* Old kernel sources don't have ".revision" field,
119 * but we stole a byte from name. */
120 name[XT_FUNCTION_MAXNAMELEN - 2] = '\0';
121 name[XT_FUNCTION_MAXNAMELEN - 1] = revision;
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
273static char *get_modprobe(void)
274{
275 int procfile;
276 char *ret;
277
278#define PROCFILE_BUFSIZ 1024
279 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
280 if (procfile < 0)
281 return NULL;
282
283 ret = (char *) malloc(PROCFILE_BUFSIZ);
284 if (ret) {
285 memset(ret, 0, PROCFILE_BUFSIZ);
286 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
287 case -1: goto fail;
288 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
289 }
290 if (ret[strlen(ret)-1]=='\n')
291 ret[strlen(ret)-1]=0;
292 close(procfile);
293 return ret;
294 }
295 fail:
296 free(ret);
297 close(procfile);
298 return NULL;
299}
300
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100301int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000302{
303 char *buf = NULL;
304 char *argv[4];
305 int status;
306
307 /* If they don't explicitly set it, read out of kernel */
308 if (!modprobe) {
309 buf = get_modprobe();
310 if (!buf)
311 return -1;
312 modprobe = buf;
313 }
314
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100315 /*
316 * Need to flush the buffer, or the child may output it again
317 * when switching the program thru execv.
318 */
319 fflush(stdout);
320
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000321 switch (fork()) {
322 case 0:
323 argv[0] = (char *)modprobe;
324 argv[1] = (char *)modname;
325 if (quiet) {
326 argv[2] = "-q";
327 argv[3] = NULL;
328 } else {
329 argv[2] = NULL;
330 argv[3] = NULL;
331 }
332 execv(argv[0], argv);
333
334 /* not usually reached */
335 exit(1);
336 case -1:
337 return -1;
338
339 default: /* parent */
340 wait(&status);
341 }
342
343 free(buf);
344 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
345 return 0;
346 return -1;
347}
348
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100349int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000350{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100351 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000352 static int ret = -1;
353
354 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100355 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000356 loaded = (ret == 0);
357 }
358
359 return ret;
360}
361
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100362/**
363 * xtables_strtou{i,l} - string to number conversion
364 * @s: input string
365 * @end: like strtoul's "end" pointer
366 * @value: pointer for result
367 * @min: minimum accepted value
368 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000369 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100370 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
371 * "15a" is rejected.
372 * In either case, the value obtained is compared for min-max compliance.
373 * Base is always 0, i.e. autodetect depending on @s.
374 *
375 * Returns true/false whether number was accepted. On failure, *value has
376 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000377 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100378bool xtables_strtoul(const char *s, char **end, unsigned long *value,
379 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000380{
381 unsigned long v;
382 char *my_end;
383
384 errno = 0;
385 v = strtoul(s, &my_end, 0);
386
387 if (my_end == s)
388 return false;
389 if (end != NULL)
390 *end = my_end;
391
392 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
393 if (value != NULL)
394 *value = v;
395 if (end == NULL)
396 return *my_end == '\0';
397 return true;
398 }
399
400 return false;
401}
402
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100403bool xtables_strtoui(const char *s, char **end, unsigned int *value,
404 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000405{
406 unsigned long v;
407 bool ret;
408
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100409 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000410 if (value != NULL)
411 *value = v;
412 return ret;
413}
414
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100415int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000416{
417 struct servent *service;
418
419 if ((service = getservbyname(name, proto)) != NULL)
420 return ntohs((unsigned short) service->s_port);
421
422 return -1;
423}
424
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100425u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000426{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100427 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000428
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100429 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100430 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100431 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000432
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100433 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000434 "invalid port/service `%s' specified", port);
435}
436
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100437void xtables_parse_interface(const char *arg, char *vianame,
438 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000439{
440 int vialen = strlen(arg);
441 unsigned int i;
442
443 memset(mask, 0, IFNAMSIZ);
444 memset(vianame, 0, IFNAMSIZ);
445
446 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100447 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000448 "interface name `%s' must be shorter than IFNAMSIZ"
449 " (%i)", arg, IFNAMSIZ-1);
450
451 strcpy(vianame, arg);
452 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
453 memset(mask, 0, IFNAMSIZ);
454 else if (vianame[vialen - 1] == '+') {
455 memset(mask, 0xFF, vialen - 1);
456 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
457 /* Don't remove `+' here! -HW */
458 } else {
459 /* Include nul-terminator in match */
460 memset(mask, 0xFF, vialen + 1);
461 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
462 for (i = 0; vianame[i]; i++) {
463 if (vianame[i] == ':' ||
464 vianame[i] == '!' ||
465 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000466 fprintf(stderr,
467 "Warning: weird character in interface"
468 " `%s' (No aliases, :, ! or *).\n",
469 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000470 break;
471 }
472 }
473 }
474}
475
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200476#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100477static void *load_extension(const char *search_path, const char *prefix,
478 const char *name, bool is_target)
479{
480 const char *dir = search_path, *next;
481 void *ptr = NULL;
482 struct stat sb;
483 char path[256];
484
485 do {
486 next = strchr(dir, ':');
487 if (next == NULL)
488 next = dir + strlen(dir);
489 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200490 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100491
492 if (dlopen(path, RTLD_NOW) != NULL) {
493 /* Found library. If it didn't register itself,
494 maybe they specified target as match. */
495 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100496 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100497 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100498 ptr = xtables_find_match(name,
499 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100500 } else if (stat(path, &sb) == 0) {
501 fprintf(stderr, "%s: %s\n", path, dlerror());
502 }
503
504 if (ptr != NULL)
505 return ptr;
506
507 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200508 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100509 if (dlopen(path, RTLD_NOW) != NULL) {
510 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100511 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100512 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100513 ptr = xtables_find_match(name,
514 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100515 } else if (stat(path, &sb) == 0) {
516 fprintf(stderr, "%s: %s\n", path, dlerror());
517 }
518
519 if (ptr != NULL)
520 return ptr;
521
522 dir = next + 1;
523 } while (*next != '\0');
524
525 return NULL;
526}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200527#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100528
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100529struct xtables_match *
530xtables_find_match(const char *name, enum xtables_tryload tryload,
531 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000532{
533 struct xtables_match *ptr;
534 const char *icmp6 = "icmp6";
535
536 /* This is ugly as hell. Nonetheless, there is no way of changing
537 * this without hurting backwards compatibility */
538 if ( (strcmp(name,"icmpv6") == 0) ||
539 (strcmp(name,"ipv6-icmp") == 0) ||
540 (strcmp(name,"icmp6") == 0) )
541 name = icmp6;
542
543 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
544 if (strcmp(name, ptr->name) == 0) {
545 struct xtables_match *clone;
546
547 /* First match of this type: */
548 if (ptr->m == NULL)
549 break;
550
551 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100552 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000553 memcpy(clone, ptr, sizeof(struct xtables_match));
554 clone->mflags = 0;
555 /* This is a clone: */
556 clone->next = clone;
557
558 ptr = clone;
559 break;
560 }
561 }
562
563#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100564 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100565 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100566 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000567
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100568 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100569 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000570 "Couldn't load match `%s':%s\n",
571 name, dlerror());
572 }
573#else
574 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100575 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000576 ptr->loaded = 1;
577 else
578 ptr = NULL;
579 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100580 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100581 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000582 "Couldn't find match `%s'\n", name);
583 }
584#endif
585
586 if (ptr && matches) {
587 struct xtables_rule_match **i;
588 struct xtables_rule_match *newentry;
589
Jan Engelhardt630ef482009-01-27 14:58:41 +0100590 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000591
592 for (i = matches; *i; i = &(*i)->next) {
593 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100594 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000595 }
596 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100597 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000598 newentry->next = NULL;
599 *i = newentry;
600 }
601
602 return ptr;
603}
604
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100605struct xtables_target *
606xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000607{
608 struct xtables_target *ptr;
609
610 /* Standard target? */
611 if (strcmp(name, "") == 0
612 || strcmp(name, XTC_LABEL_ACCEPT) == 0
613 || strcmp(name, XTC_LABEL_DROP) == 0
614 || strcmp(name, XTC_LABEL_QUEUE) == 0
615 || strcmp(name, XTC_LABEL_RETURN) == 0)
616 name = "standard";
617
618 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
619 if (strcmp(name, ptr->name) == 0)
620 break;
621 }
622
623#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100624 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100625 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100626 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000627
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100628 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100629 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000630 "Couldn't load target `%s':%s\n",
631 name, dlerror());
632 }
633#else
634 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100635 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000636 ptr->loaded = 1;
637 else
638 ptr = NULL;
639 }
640 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100641 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000642 "Couldn't find target `%s'\n", name);
643 }
644#endif
645
646 if (ptr)
647 ptr->used = 1;
648
649 return ptr;
650}
651
652static int compatible_revision(const char *name, u_int8_t revision, int opt)
653{
654 struct xt_get_revision rev;
655 socklen_t s = sizeof(rev);
656 int max_rev, sockfd;
657
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100658 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000659 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000660 if (errno == EPERM) {
661 /* revision 0 is always supported. */
662 if (revision != 0)
663 fprintf(stderr, "Could not determine whether "
664 "revision %u is supported, "
665 "assuming it is.\n",
666 revision);
667 return 1;
668 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000669 fprintf(stderr, "Could not open socket to kernel: %s\n",
670 strerror(errno));
671 exit(1);
672 }
673
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100674 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000675
676 strcpy(rev.name, name);
677 rev.revision = revision;
678
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100679 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000680 if (max_rev < 0) {
681 /* Definitely don't support this? */
682 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
683 close(sockfd);
684 return 0;
685 } else if (errno == ENOPROTOOPT) {
686 close(sockfd);
687 /* Assume only revision 0 support (old kernel) */
688 return (revision == 0);
689 } else {
690 fprintf(stderr, "getsockopt failed strangely: %s\n",
691 strerror(errno));
692 exit(1);
693 }
694 }
695 close(sockfd);
696 return 1;
697}
698
699
700static int compatible_match_revision(const char *name, u_int8_t revision)
701{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100702 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000703}
704
705static int compatible_target_revision(const char *name, u_int8_t revision)
706{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100707 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000708}
709
710void xtables_register_match(struct xtables_match *me)
711{
712 struct xtables_match **i, *old;
713
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100714 if (strcmp(me->version, XTABLES_VERSION) != 0) {
715 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
716 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500717 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100718 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000719 exit(1);
720 }
721
722 /* Revision field stole a char from name. */
723 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
724 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500725 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000726 exit(1);
727 }
728
729 if (me->family >= NPROTO) {
730 fprintf(stderr,
731 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500732 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000733 exit(1);
734 }
735
736 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100737 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000738 return;
739
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100740 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000741 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100742 if (old->revision == me->revision &&
743 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000744 fprintf(stderr,
745 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500746 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000747 exit(1);
748 }
749
750 /* Now we have two (or more) options, check compatibility. */
751 if (compatible_match_revision(old->name, old->revision)
752 && old->revision > me->revision)
753 return;
754
Jan Engelhardt23545c22008-02-14 04:23:04 +0100755 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000756 if (!compatible_match_revision(me->name, me->revision))
757 return;
758
Jan Engelhardt23545c22008-02-14 04:23:04 +0100759 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
760 if (old->revision == me->revision && me->family == AF_UNSPEC)
761 return;
762
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000763 /* Delete old one. */
764 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
765 *i = old->next;
766 }
767
768 if (me->size != XT_ALIGN(me->size)) {
769 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500770 xt_params->program_name, me->name,
771 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000772 exit(1);
773 }
774
775 /* Append to list. */
776 for (i = &xtables_matches; *i; i = &(*i)->next);
777 me->next = NULL;
778 *i = me;
779
780 me->m = NULL;
781 me->mflags = 0;
782}
783
784void xtables_register_target(struct xtables_target *me)
785{
786 struct xtables_target *old;
787
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100788 if (strcmp(me->version, XTABLES_VERSION) != 0) {
789 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
790 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500791 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100792 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000793 exit(1);
794 }
795
796 /* Revision field stole a char from name. */
797 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
798 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500799 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000800 exit(1);
801 }
802
803 if (me->family >= NPROTO) {
804 fprintf(stderr,
805 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500806 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000807 exit(1);
808 }
809
810 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100811 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000812 return;
813
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100814 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000815 if (old) {
816 struct xtables_target **i;
817
Jan Engelhardt23545c22008-02-14 04:23:04 +0100818 if (old->revision == me->revision &&
819 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000820 fprintf(stderr,
821 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500822 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000823 exit(1);
824 }
825
826 /* Now we have two (or more) options, check compatibility. */
827 if (compatible_target_revision(old->name, old->revision)
828 && old->revision > me->revision)
829 return;
830
Jan Engelhardt23545c22008-02-14 04:23:04 +0100831 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000832 if (!compatible_target_revision(me->name, me->revision))
833 return;
834
Jan Engelhardt23545c22008-02-14 04:23:04 +0100835 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
836 if (old->revision == me->revision && me->family == AF_UNSPEC)
837 return;
838
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000839 /* Delete old one. */
840 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
841 *i = old->next;
842 }
843
844 if (me->size != XT_ALIGN(me->size)) {
845 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500846 xt_params->program_name, me->name,
847 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000848 exit(1);
849 }
850
851 /* Prepend to list. */
852 me->next = xtables_targets;
853 xtables_targets = me;
854 me->t = NULL;
855 me->tflags = 0;
856}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000857
Jan Engelhardta41545c2009-01-27 21:27:19 +0100858/**
859 * xtables_param_act - act on condition
860 * @status: a constant from enum xtables_exittype
861 *
862 * %XTF_ONLY_ONCE: print error message that option may only be used once.
863 * @p1: module name (e.g. "mark")
864 * @p2(...): option in conflict (e.g. "--mark")
865 * @p3(...): condition to match on (see extensions/ for examples)
866 *
867 * %XTF_NO_INVERT: option does not support inversion
868 * @p1: module name
869 * @p2: option in conflict
870 * @p3: condition to match on
871 *
872 * %XTF_BAD_VALUE: bad value for option
873 * @p1: module name
874 * @p2: option with which the problem occured (e.g. "--mark")
875 * @p3: string the user passed in (e.g. "99999999999999")
876 *
877 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
878 * @p1: module name
879 *
880 * Displays an error message and exits the program.
881 */
882void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000883{
884 const char *p2, *p3;
885 va_list args;
886 bool b;
887
888 va_start(args, p1);
889
890 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100891 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000892 p2 = va_arg(args, const char *);
893 b = va_arg(args, unsigned int);
894 if (!b)
895 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100896 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000897 "%s: \"%s\" option may only be specified once",
898 p1, p2);
899 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100900 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000901 p2 = va_arg(args, const char *);
902 b = va_arg(args, unsigned int);
903 if (!b)
904 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100905 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000906 "%s: \"%s\" option cannot be inverted", p1, p2);
907 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100908 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000909 p2 = va_arg(args, const char *);
910 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100911 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000912 "%s: Bad value for \"%s\" option: \"%s\"",
913 p1, p2, p3);
914 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100915 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000916 b = va_arg(args, unsigned int);
917 if (!b)
918 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100919 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000920 "%s: At most one action is possible", p1);
921 break;
922 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100923 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000924 break;
925 }
926
927 va_end(args);
928}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000929
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100930const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000931{
932 static char buf[20];
933 const unsigned char *bytep = (const void *)&addrp->s_addr;
934
935 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
936 return buf;
937}
938
939static const char *ipaddr_to_host(const struct in_addr *addr)
940{
941 struct hostent *host;
942
943 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
944 if (host == NULL)
945 return NULL;
946
947 return host->h_name;
948}
949
950static const char *ipaddr_to_network(const struct in_addr *addr)
951{
952 struct netent *net;
953
954 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
955 return net->n_name;
956
957 return NULL;
958}
959
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100960const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000961{
962 const char *name;
963
964 if ((name = ipaddr_to_host(addr)) != NULL ||
965 (name = ipaddr_to_network(addr)) != NULL)
966 return name;
967
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100968 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000969}
970
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100971const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000972{
973 static char buf[20];
974 uint32_t maskaddr, bits;
975 int i;
976
977 maskaddr = ntohl(mask->s_addr);
978
979 if (maskaddr == 0xFFFFFFFFL)
980 /* we don't want to see "/32" */
981 return "";
982
983 i = 32;
984 bits = 0xFFFFFFFEL;
985 while (--i >= 0 && maskaddr != bits)
986 bits <<= 1;
987 if (i >= 0)
988 sprintf(buf, "/%d", i);
989 else
990 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100991 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000992
993 return buf;
994}
995
Jan Engelhardtbd943842008-01-20 13:38:08 +0000996static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
997{
998 static struct in_addr addr;
999 unsigned char *addrp;
1000 unsigned int onebyte;
1001 char buf[20], *p, *q;
1002 int i;
1003
1004 /* copy dotted string, because we need to modify it */
1005 strncpy(buf, dotted, sizeof(buf) - 1);
1006 buf[sizeof(buf) - 1] = '\0';
1007 addrp = (void *)&addr.s_addr;
1008
1009 p = buf;
1010 for (i = 0; i < 3; ++i) {
1011 if ((q = strchr(p, '.')) == NULL) {
1012 if (is_mask)
1013 return NULL;
1014
1015 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001016 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001017 return NULL;
1018
1019 addrp[i] = onebyte;
1020 while (i < 3)
1021 addrp[++i] = 0;
1022
1023 return &addr;
1024 }
1025
1026 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001027 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001028 return NULL;
1029
1030 addrp[i] = onebyte;
1031 p = q + 1;
1032 }
1033
1034 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001035 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001036 return NULL;
1037
1038 addrp[3] = onebyte;
1039 return &addr;
1040}
1041
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001042struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001043{
1044 return __numeric_to_ipaddr(dotted, false);
1045}
1046
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001047struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001048{
1049 return __numeric_to_ipaddr(dotted, true);
1050}
1051
1052static struct in_addr *network_to_ipaddr(const char *name)
1053{
1054 static struct in_addr addr;
1055 struct netent *net;
1056
1057 if ((net = getnetbyname(name)) != NULL) {
1058 if (net->n_addrtype != AF_INET)
1059 return NULL;
1060 addr.s_addr = htonl(net->n_net);
1061 return &addr;
1062 }
1063
1064 return NULL;
1065}
1066
1067static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1068{
1069 struct hostent *host;
1070 struct in_addr *addr;
1071 unsigned int i;
1072
1073 *naddr = 0;
1074 if ((host = gethostbyname(name)) != NULL) {
1075 if (host->h_addrtype != AF_INET ||
1076 host->h_length != sizeof(struct in_addr))
1077 return NULL;
1078
1079 while (host->h_addr_list[*naddr] != NULL)
1080 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001081 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001082 for (i = 0; i < *naddr; i++)
1083 memcpy(&addr[i], host->h_addr_list[i],
1084 sizeof(struct in_addr));
1085 return addr;
1086 }
1087
1088 return NULL;
1089}
1090
1091static struct in_addr *
1092ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1093{
1094 struct in_addr *addrptmp, *addrp;
1095
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001096 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001097 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001098 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001099 memcpy(addrp, addrptmp, sizeof(*addrp));
1100 *naddrs = 1;
1101 return addrp;
1102 }
1103 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1104 return addrptmp;
1105
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001106 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001107}
1108
1109static struct in_addr *parse_ipmask(const char *mask)
1110{
1111 static struct in_addr maskaddr;
1112 struct in_addr *addrp;
1113 unsigned int bits;
1114
1115 if (mask == NULL) {
1116 /* no mask at all defaults to 32 bits */
1117 maskaddr.s_addr = 0xFFFFFFFF;
1118 return &maskaddr;
1119 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001120 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001121 /* dotted_to_addr already returns a network byte order addr */
1122 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001123 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001124 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001125 "invalid mask `%s' specified", mask);
1126 if (bits != 0) {
1127 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1128 return &maskaddr;
1129 }
1130
1131 maskaddr.s_addr = 0U;
1132 return &maskaddr;
1133}
1134
Jan Engelhardta0baae82009-01-30 04:32:50 +01001135/**
1136 * xtables_ipparse_any - transform arbitrary name to in_addr
1137 *
1138 * Possible inputs (pseudo regex):
1139 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1140 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1141 */
1142void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1143 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001144{
1145 unsigned int i, j, k, n;
1146 struct in_addr *addrp;
1147 char buf[256], *p;
1148
1149 strncpy(buf, name, sizeof(buf) - 1);
1150 buf[sizeof(buf) - 1] = '\0';
1151 if ((p = strrchr(buf, '/')) != NULL) {
1152 *p = '\0';
1153 addrp = parse_ipmask(p + 1);
1154 } else {
1155 addrp = parse_ipmask(NULL);
1156 }
1157 memcpy(maskp, addrp, sizeof(*maskp));
1158
1159 /* if a null mask is given, the name is ignored, like in "any/0" */
1160 if (maskp->s_addr == 0U)
1161 strcpy(buf, "0.0.0.0");
1162
1163 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1164 n = *naddrs;
1165 for (i = 0, j = 0; i < n; ++i) {
1166 addrp[j++].s_addr &= maskp->s_addr;
1167 for (k = 0; k < j - 1; ++k)
1168 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1169 --*naddrs;
1170 --j;
1171 break;
1172 }
1173 }
1174}
1175
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001176const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001177{
1178 /* 0000:0000:0000:0000:0000:000.000.000.000
1179 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1180 static char buf[50+1];
1181 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1182}
1183
1184static const char *ip6addr_to_host(const struct in6_addr *addr)
1185{
1186 static char hostname[NI_MAXHOST];
1187 struct sockaddr_in6 saddr;
1188 int err;
1189
1190 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1191 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1192 saddr.sin6_family = AF_INET6;
1193
1194 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1195 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1196 if (err != 0) {
1197#ifdef DEBUG
1198 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1199#endif
1200 return NULL;
1201 }
1202
1203#ifdef DEBUG
1204 fprintf (stderr, "\naddr2host: %s\n", hostname);
1205#endif
1206 return hostname;
1207}
1208
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001209const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001210{
1211 const char *name;
1212
1213 if ((name = ip6addr_to_host(addr)) != NULL)
1214 return name;
1215
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001216 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001217}
1218
1219static int ip6addr_prefix_length(const struct in6_addr *k)
1220{
1221 unsigned int bits = 0;
1222 uint32_t a, b, c, d;
1223
Jan Engelhardt48607812008-06-10 15:17:53 +02001224 a = ntohl(k->s6_addr32[0]);
1225 b = ntohl(k->s6_addr32[1]);
1226 c = ntohl(k->s6_addr32[2]);
1227 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001228 while (a & 0x80000000U) {
1229 ++bits;
1230 a <<= 1;
1231 a |= (b >> 31) & 1;
1232 b <<= 1;
1233 b |= (c >> 31) & 1;
1234 c <<= 1;
1235 c |= (d >> 31) & 1;
1236 d <<= 1;
1237 }
1238 if (a != 0 || b != 0 || c != 0 || d != 0)
1239 return -1;
1240 return bits;
1241}
1242
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001243const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001244{
1245 static char buf[50+2];
1246 int l = ip6addr_prefix_length(addrp);
1247
1248 if (l == -1) {
1249 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001250 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001251 return buf;
1252 }
1253 sprintf(buf, "/%d", l);
1254 return buf;
1255}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001256
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001257struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001258{
1259 static struct in6_addr ap;
1260 int err;
1261
1262 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1263 return &ap;
1264#ifdef DEBUG
1265 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1266#endif
1267 return NULL;
1268}
1269
1270static struct in6_addr *
1271host_to_ip6addr(const char *name, unsigned int *naddr)
1272{
1273 static struct in6_addr *addr;
1274 struct addrinfo hints;
1275 struct addrinfo *res;
1276 int err;
1277
1278 memset(&hints, 0, sizeof(hints));
1279 hints.ai_flags = AI_CANONNAME;
1280 hints.ai_family = AF_INET6;
1281 hints.ai_socktype = SOCK_RAW;
1282 hints.ai_protocol = IPPROTO_IPV6;
1283 hints.ai_next = NULL;
1284
1285 *naddr = 0;
1286 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1287#ifdef DEBUG
1288 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1289#endif
1290 return NULL;
1291 } else {
1292 if (res->ai_family != AF_INET6 ||
1293 res->ai_addrlen != sizeof(struct sockaddr_in6))
1294 return NULL;
1295
1296#ifdef DEBUG
1297 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1298 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1299#endif
1300 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001301 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001302 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1303 sizeof(struct in6_addr));
1304 freeaddrinfo(res);
1305 *naddr = 1;
1306 return addr;
1307 }
1308
1309 return NULL;
1310}
1311
1312static struct in6_addr *network_to_ip6addr(const char *name)
1313{
1314 /* abort();*/
1315 /* TODO: not implemented yet, but the exception breaks the
1316 * name resolvation */
1317 return NULL;
1318}
1319
1320static struct in6_addr *
1321ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1322{
1323 struct in6_addr *addrp, *addrptmp;
1324
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001325 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001326 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001327 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001328 memcpy(addrp, addrptmp, sizeof(*addrp));
1329 *naddrs = 1;
1330 return addrp;
1331 }
1332 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1333 return addrp;
1334
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001335 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001336}
1337
1338static struct in6_addr *parse_ip6mask(char *mask)
1339{
1340 static struct in6_addr maskaddr;
1341 struct in6_addr *addrp;
1342 unsigned int bits;
1343
1344 if (mask == NULL) {
1345 /* no mask at all defaults to 128 bits */
1346 memset(&maskaddr, 0xff, sizeof maskaddr);
1347 return &maskaddr;
1348 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001349 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001350 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001351 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001352 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001353 "invalid mask `%s' specified", mask);
1354 if (bits != 0) {
1355 char *p = (void *)&maskaddr;
1356 memset(p, 0xff, bits / 8);
1357 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1358 p[bits/8] = 0xff << (8 - (bits & 7));
1359 return &maskaddr;
1360 }
1361
1362 memset(&maskaddr, 0, sizeof(maskaddr));
1363 return &maskaddr;
1364}
1365
Jan Engelhardta0baae82009-01-30 04:32:50 +01001366void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1367 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001368{
1369 struct in6_addr *addrp;
1370 unsigned int i, j, k, n;
1371 char buf[256], *p;
1372
1373 strncpy(buf, name, sizeof(buf) - 1);
1374 buf[sizeof(buf)-1] = '\0';
1375 if ((p = strrchr(buf, '/')) != NULL) {
1376 *p = '\0';
1377 addrp = parse_ip6mask(p + 1);
1378 } else {
1379 addrp = parse_ip6mask(NULL);
1380 }
1381 memcpy(maskp, addrp, sizeof(*maskp));
1382
1383 /* if a null mask is given, the name is ignored, like in "any/0" */
1384 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1385 strcpy(buf, "::");
1386
1387 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1388 n = *naddrs;
1389 for (i = 0, j = 0; i < n; ++i) {
1390 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001391 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001392 ++j;
1393 for (k = 0; k < j - 1; ++k)
1394 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1395 --*naddrs;
1396 --j;
1397 break;
1398 }
1399 }
1400}
Max Kellermanna5d09942008-01-29 13:44:34 +00001401
Jan Engelhardta0baae82009-01-30 04:32:50 +01001402void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001403{
1404 static const char no_quote_chars[] = "_-0123456789"
1405 "abcdefghijklmnopqrstuvwxyz"
1406 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1407 static const char escape_chars[] = "\"\\'";
1408 size_t length;
1409 const char *p;
1410
1411 length = strcspn(value, no_quote_chars);
1412 if (length > 0 && value[length] == 0) {
1413 /* no quoting required */
1414 fputs(value, stdout);
1415 putchar(' ');
1416 } else {
1417 /* there is at least one dangerous character in the
1418 value, which we have to quote. Write double quotes
1419 around the value and escape special characters with
1420 a backslash */
1421 putchar('"');
1422
1423 for (p = strpbrk(value, escape_chars); p != NULL;
1424 p = strpbrk(value, escape_chars)) {
1425 if (p > value)
1426 fwrite(value, 1, p - value, stdout);
1427 putchar('\\');
1428 putchar(*p);
1429 value = p + 1;
1430 }
1431
1432 /* print the rest and finish the double quoted
1433 string */
1434 fputs(value, stdout);
1435 printf("\" ");
1436 }
1437}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001438
1439/**
1440 * Check for option-intrapositional negation.
1441 * Do not use in new code.
1442 */
1443int xtables_check_inverse(const char option[], int *invert,
1444 int *my_optind, int argc)
1445{
1446 if (option && strcmp(option, "!") == 0) {
1447 fprintf(stderr, "Using intrapositioned negation "
1448 "(`--option ! this`) is deprecated in favor of "
1449 "extrapositioned (`! --option this`).\n");
1450
1451 if (*invert)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001452 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001453 "Multiple `!' flags not allowed");
1454 *invert = true;
1455 if (my_optind != NULL) {
1456 ++*my_optind;
1457 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001458 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001459 "no argument following `!'");
1460 }
1461
1462 return true;
1463 }
1464 return false;
1465}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001466
1467const struct xtables_pprot xtables_chain_protos[] = {
1468 {"tcp", IPPROTO_TCP},
1469 {"sctp", IPPROTO_SCTP},
1470 {"udp", IPPROTO_UDP},
1471 {"udplite", IPPROTO_UDPLITE},
1472 {"icmp", IPPROTO_ICMP},
1473 {"icmpv6", IPPROTO_ICMPV6},
1474 {"ipv6-icmp", IPPROTO_ICMPV6},
1475 {"esp", IPPROTO_ESP},
1476 {"ah", IPPROTO_AH},
1477 {"ipv6-mh", IPPROTO_MH},
1478 {"mh", IPPROTO_MH},
1479 {"all", 0},
1480 {NULL},
1481};
1482
1483u_int16_t
1484xtables_parse_protocol(const char *s)
1485{
1486 unsigned int proto;
1487
1488 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1489 struct protoent *pent;
1490
1491 /* first deal with the special case of 'all' to prevent
1492 * people from being able to redefine 'all' in nsswitch
1493 * and/or provoke expensive [not working] ldap/nis/...
1494 * lookups */
1495 if (!strcmp(s, "all"))
1496 return 0;
1497
1498 if ((pent = getprotobyname(s)))
1499 proto = pent->p_proto;
1500 else {
1501 unsigned int i;
1502 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1503 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1504 proto = xtables_chain_protos[i].num;
1505 break;
1506 }
1507 }
1508 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001509 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001510 "unknown protocol `%s' specified",
1511 s);
1512 }
1513 }
1514
1515 return proto;
1516}