blob: a812ef163da25871b7fee21132f5b9d0cc3d309b [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
162/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100163 * Program will set this to its own name.
164 */
165const char *xtables_program_name;
166
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100167/* Search path for Xtables .so files */
168static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000169
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000170/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100171const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000172
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000173/* Keeping track of external matches and targets: linked lists. */
174struct xtables_match *xtables_matches;
175struct xtables_target *xtables_targets;
176
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100177void xtables_init(void)
178{
179 xtables_libdir = getenv("XTABLES_LIBDIR");
180 if (xtables_libdir != NULL)
181 return;
182 xtables_libdir = getenv("IPTABLES_LIB_DIR");
183 if (xtables_libdir != NULL) {
184 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
185 "use XTABLES_LIBDIR.\n");
186 return;
187 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100188 /*
189 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
190 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
191 * for these env vars are deprecated anyhow, and in light of the
192 * (shared) libxt_*.so files, makes less sense to have
193 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
194 */
195 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
196 if (xtables_libdir != NULL) {
197 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
198 "use XTABLES_LIBDIR.\n");
199 return;
200 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100201 xtables_libdir = XTABLES_LIBDIR;
202}
203
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100204void xtables_set_nfproto(uint8_t nfproto)
205{
206 switch (nfproto) {
207 case NFPROTO_IPV4:
208 afinfo = &afinfo_ipv4;
209 break;
210 case NFPROTO_IPV6:
211 afinfo = &afinfo_ipv6;
212 break;
213 default:
214 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
215 __func__);
216 }
217}
218
Jan Engelhardt630ef482009-01-27 14:58:41 +0100219/**
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500220 * xtables_set_params - set the global parameters used by xtables
221 * @xtp: input xtables_globals structure
222 *
223 * The app is expected to pass a valid xtables_globals data-filled
224 * with proper values
225 * @xtp cannot be NULL
226 *
227 * Returns -1 on failure to set and 0 on success
228 */
229int xtables_set_params(struct xtables_globals *xtp)
230{
231 if (!xtp) {
232 fprintf(stderr, "%s: Illegal global params\n",__func__);
233 return -1;
234 }
235
236 xt_params = xtp;
237
238 if (!xt_params->exit_err)
239 xt_params->exit_err = basic_exit_err;
240
241 return 0;
242}
243
244int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto)
245{
246 xtables_init();
247 xtables_set_nfproto(nfproto);
248 return xtables_set_params(xtp);
249}
250
251/**
Jan Engelhardt630ef482009-01-27 14:58:41 +0100252 * xtables_*alloc - wrappers that exit on failure
253 */
254void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000255{
256 void *p;
257
258 if ((p = calloc(count, size)) == NULL) {
259 perror("ip[6]tables: calloc failed");
260 exit(1);
261 }
262
263 return p;
264}
265
Jan Engelhardt630ef482009-01-27 14:58:41 +0100266void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000267{
268 void *p;
269
270 if ((p = malloc(size)) == NULL) {
271 perror("ip[6]tables: malloc failed");
272 exit(1);
273 }
274
275 return p;
276}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000277
278static char *get_modprobe(void)
279{
280 int procfile;
281 char *ret;
282
283#define PROCFILE_BUFSIZ 1024
284 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
285 if (procfile < 0)
286 return NULL;
287
288 ret = (char *) malloc(PROCFILE_BUFSIZ);
289 if (ret) {
290 memset(ret, 0, PROCFILE_BUFSIZ);
291 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
292 case -1: goto fail;
293 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
294 }
295 if (ret[strlen(ret)-1]=='\n')
296 ret[strlen(ret)-1]=0;
297 close(procfile);
298 return ret;
299 }
300 fail:
301 free(ret);
302 close(procfile);
303 return NULL;
304}
305
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100306int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000307{
308 char *buf = NULL;
309 char *argv[4];
310 int status;
311
312 /* If they don't explicitly set it, read out of kernel */
313 if (!modprobe) {
314 buf = get_modprobe();
315 if (!buf)
316 return -1;
317 modprobe = buf;
318 }
319
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100320 /*
321 * Need to flush the buffer, or the child may output it again
322 * when switching the program thru execv.
323 */
324 fflush(stdout);
325
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000326 switch (fork()) {
327 case 0:
328 argv[0] = (char *)modprobe;
329 argv[1] = (char *)modname;
330 if (quiet) {
331 argv[2] = "-q";
332 argv[3] = NULL;
333 } else {
334 argv[2] = NULL;
335 argv[3] = NULL;
336 }
337 execv(argv[0], argv);
338
339 /* not usually reached */
340 exit(1);
341 case -1:
342 return -1;
343
344 default: /* parent */
345 wait(&status);
346 }
347
348 free(buf);
349 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
350 return 0;
351 return -1;
352}
353
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100354int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000355{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100356 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000357 static int ret = -1;
358
359 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100360 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000361 loaded = (ret == 0);
362 }
363
364 return ret;
365}
366
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100367/**
368 * xtables_strtou{i,l} - string to number conversion
369 * @s: input string
370 * @end: like strtoul's "end" pointer
371 * @value: pointer for result
372 * @min: minimum accepted value
373 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000374 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100375 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
376 * "15a" is rejected.
377 * In either case, the value obtained is compared for min-max compliance.
378 * Base is always 0, i.e. autodetect depending on @s.
379 *
380 * Returns true/false whether number was accepted. On failure, *value has
381 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000382 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100383bool xtables_strtoul(const char *s, char **end, unsigned long *value,
384 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000385{
386 unsigned long v;
387 char *my_end;
388
389 errno = 0;
390 v = strtoul(s, &my_end, 0);
391
392 if (my_end == s)
393 return false;
394 if (end != NULL)
395 *end = my_end;
396
397 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
398 if (value != NULL)
399 *value = v;
400 if (end == NULL)
401 return *my_end == '\0';
402 return true;
403 }
404
405 return false;
406}
407
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100408bool xtables_strtoui(const char *s, char **end, unsigned int *value,
409 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000410{
411 unsigned long v;
412 bool ret;
413
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100414 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000415 if (value != NULL)
416 *value = v;
417 return ret;
418}
419
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100420int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000421{
422 struct servent *service;
423
424 if ((service = getservbyname(name, proto)) != NULL)
425 return ntohs((unsigned short) service->s_port);
426
427 return -1;
428}
429
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100430u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000431{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100432 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000433
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100434 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100435 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100436 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000437
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100438 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000439 "invalid port/service `%s' specified", port);
440}
441
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100442void xtables_parse_interface(const char *arg, char *vianame,
443 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000444{
445 int vialen = strlen(arg);
446 unsigned int i;
447
448 memset(mask, 0, IFNAMSIZ);
449 memset(vianame, 0, IFNAMSIZ);
450
451 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100452 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000453 "interface name `%s' must be shorter than IFNAMSIZ"
454 " (%i)", arg, IFNAMSIZ-1);
455
456 strcpy(vianame, arg);
457 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
458 memset(mask, 0, IFNAMSIZ);
459 else if (vianame[vialen - 1] == '+') {
460 memset(mask, 0xFF, vialen - 1);
461 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
462 /* Don't remove `+' here! -HW */
463 } else {
464 /* Include nul-terminator in match */
465 memset(mask, 0xFF, vialen + 1);
466 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
467 for (i = 0; vianame[i]; i++) {
468 if (vianame[i] == ':' ||
469 vianame[i] == '!' ||
470 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000471 fprintf(stderr,
472 "Warning: weird character in interface"
473 " `%s' (No aliases, :, ! or *).\n",
474 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000475 break;
476 }
477 }
478 }
479}
480
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200481#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100482static void *load_extension(const char *search_path, const char *prefix,
483 const char *name, bool is_target)
484{
485 const char *dir = search_path, *next;
486 void *ptr = NULL;
487 struct stat sb;
488 char path[256];
489
490 do {
491 next = strchr(dir, ':');
492 if (next == NULL)
493 next = dir + strlen(dir);
494 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200495 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100496
497 if (dlopen(path, RTLD_NOW) != NULL) {
498 /* Found library. If it didn't register itself,
499 maybe they specified target as match. */
500 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100501 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100502 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100503 ptr = xtables_find_match(name,
504 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100505 } else if (stat(path, &sb) == 0) {
506 fprintf(stderr, "%s: %s\n", path, dlerror());
507 }
508
509 if (ptr != NULL)
510 return ptr;
511
512 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200513 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100514 if (dlopen(path, RTLD_NOW) != NULL) {
515 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100516 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100517 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100518 ptr = xtables_find_match(name,
519 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100520 } else if (stat(path, &sb) == 0) {
521 fprintf(stderr, "%s: %s\n", path, dlerror());
522 }
523
524 if (ptr != NULL)
525 return ptr;
526
527 dir = next + 1;
528 } while (*next != '\0');
529
530 return NULL;
531}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200532#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100533
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100534struct xtables_match *
535xtables_find_match(const char *name, enum xtables_tryload tryload,
536 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000537{
538 struct xtables_match *ptr;
539 const char *icmp6 = "icmp6";
540
541 /* This is ugly as hell. Nonetheless, there is no way of changing
542 * this without hurting backwards compatibility */
543 if ( (strcmp(name,"icmpv6") == 0) ||
544 (strcmp(name,"ipv6-icmp") == 0) ||
545 (strcmp(name,"icmp6") == 0) )
546 name = icmp6;
547
548 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
549 if (strcmp(name, ptr->name) == 0) {
550 struct xtables_match *clone;
551
552 /* First match of this type: */
553 if (ptr->m == NULL)
554 break;
555
556 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100557 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000558 memcpy(clone, ptr, sizeof(struct xtables_match));
559 clone->mflags = 0;
560 /* This is a clone: */
561 clone->next = clone;
562
563 ptr = clone;
564 break;
565 }
566 }
567
568#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100569 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100570 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100571 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000572
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100573 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100574 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000575 "Couldn't load match `%s':%s\n",
576 name, dlerror());
577 }
578#else
579 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100580 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000581 ptr->loaded = 1;
582 else
583 ptr = NULL;
584 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100585 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100586 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000587 "Couldn't find match `%s'\n", name);
588 }
589#endif
590
591 if (ptr && matches) {
592 struct xtables_rule_match **i;
593 struct xtables_rule_match *newentry;
594
Jan Engelhardt630ef482009-01-27 14:58:41 +0100595 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000596
597 for (i = matches; *i; i = &(*i)->next) {
598 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100599 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000600 }
601 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100602 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000603 newentry->next = NULL;
604 *i = newentry;
605 }
606
607 return ptr;
608}
609
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100610struct xtables_target *
611xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000612{
613 struct xtables_target *ptr;
614
615 /* Standard target? */
616 if (strcmp(name, "") == 0
617 || strcmp(name, XTC_LABEL_ACCEPT) == 0
618 || strcmp(name, XTC_LABEL_DROP) == 0
619 || strcmp(name, XTC_LABEL_QUEUE) == 0
620 || strcmp(name, XTC_LABEL_RETURN) == 0)
621 name = "standard";
622
623 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
624 if (strcmp(name, ptr->name) == 0)
625 break;
626 }
627
628#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100629 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100630 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100631 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000632
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100633 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100634 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000635 "Couldn't load target `%s':%s\n",
636 name, dlerror());
637 }
638#else
639 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100640 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000641 ptr->loaded = 1;
642 else
643 ptr = NULL;
644 }
645 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100646 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000647 "Couldn't find target `%s'\n", name);
648 }
649#endif
650
651 if (ptr)
652 ptr->used = 1;
653
654 return ptr;
655}
656
657static int compatible_revision(const char *name, u_int8_t revision, int opt)
658{
659 struct xt_get_revision rev;
660 socklen_t s = sizeof(rev);
661 int max_rev, sockfd;
662
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100663 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000664 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000665 if (errno == EPERM) {
666 /* revision 0 is always supported. */
667 if (revision != 0)
668 fprintf(stderr, "Could not determine whether "
669 "revision %u is supported, "
670 "assuming it is.\n",
671 revision);
672 return 1;
673 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000674 fprintf(stderr, "Could not open socket to kernel: %s\n",
675 strerror(errno));
676 exit(1);
677 }
678
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100679 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000680
681 strcpy(rev.name, name);
682 rev.revision = revision;
683
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100684 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000685 if (max_rev < 0) {
686 /* Definitely don't support this? */
687 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
688 close(sockfd);
689 return 0;
690 } else if (errno == ENOPROTOOPT) {
691 close(sockfd);
692 /* Assume only revision 0 support (old kernel) */
693 return (revision == 0);
694 } else {
695 fprintf(stderr, "getsockopt failed strangely: %s\n",
696 strerror(errno));
697 exit(1);
698 }
699 }
700 close(sockfd);
701 return 1;
702}
703
704
705static int compatible_match_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_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000708}
709
710static int compatible_target_revision(const char *name, u_int8_t revision)
711{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100712 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000713}
714
715void xtables_register_match(struct xtables_match *me)
716{
717 struct xtables_match **i, *old;
718
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100719 if (strcmp(me->version, XTABLES_VERSION) != 0) {
720 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
721 "but \"%s\" is required.\n",
722 xtables_program_name, me->name,
723 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000724 exit(1);
725 }
726
727 /* Revision field stole a char from name. */
728 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
729 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100730 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000731 exit(1);
732 }
733
734 if (me->family >= NPROTO) {
735 fprintf(stderr,
736 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100737 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000738 exit(1);
739 }
740
741 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100742 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000743 return;
744
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100745 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000746 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100747 if (old->revision == me->revision &&
748 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000749 fprintf(stderr,
750 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100751 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000752 exit(1);
753 }
754
755 /* Now we have two (or more) options, check compatibility. */
756 if (compatible_match_revision(old->name, old->revision)
757 && old->revision > me->revision)
758 return;
759
Jan Engelhardt23545c22008-02-14 04:23:04 +0100760 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000761 if (!compatible_match_revision(me->name, me->revision))
762 return;
763
Jan Engelhardt23545c22008-02-14 04:23:04 +0100764 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
765 if (old->revision == me->revision && me->family == AF_UNSPEC)
766 return;
767
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000768 /* Delete old one. */
769 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
770 *i = old->next;
771 }
772
773 if (me->size != XT_ALIGN(me->size)) {
774 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100775 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000776 exit(1);
777 }
778
779 /* Append to list. */
780 for (i = &xtables_matches; *i; i = &(*i)->next);
781 me->next = NULL;
782 *i = me;
783
784 me->m = NULL;
785 me->mflags = 0;
786}
787
788void xtables_register_target(struct xtables_target *me)
789{
790 struct xtables_target *old;
791
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100792 if (strcmp(me->version, XTABLES_VERSION) != 0) {
793 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
794 "but \"%s\" is required.\n",
795 xtables_program_name, me->name,
796 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000797 exit(1);
798 }
799
800 /* Revision field stole a char from name. */
801 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
802 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100803 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000804 exit(1);
805 }
806
807 if (me->family >= NPROTO) {
808 fprintf(stderr,
809 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100810 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000811 exit(1);
812 }
813
814 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100815 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000816 return;
817
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100818 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000819 if (old) {
820 struct xtables_target **i;
821
Jan Engelhardt23545c22008-02-14 04:23:04 +0100822 if (old->revision == me->revision &&
823 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000824 fprintf(stderr,
825 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100826 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000827 exit(1);
828 }
829
830 /* Now we have two (or more) options, check compatibility. */
831 if (compatible_target_revision(old->name, old->revision)
832 && old->revision > me->revision)
833 return;
834
Jan Engelhardt23545c22008-02-14 04:23:04 +0100835 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000836 if (!compatible_target_revision(me->name, me->revision))
837 return;
838
Jan Engelhardt23545c22008-02-14 04:23:04 +0100839 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
840 if (old->revision == me->revision && me->family == AF_UNSPEC)
841 return;
842
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000843 /* Delete old one. */
844 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
845 *i = old->next;
846 }
847
848 if (me->size != XT_ALIGN(me->size)) {
849 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100850 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000851 exit(1);
852 }
853
854 /* Prepend to list. */
855 me->next = xtables_targets;
856 xtables_targets = me;
857 me->t = NULL;
858 me->tflags = 0;
859}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000860
Jan Engelhardta41545c2009-01-27 21:27:19 +0100861/**
862 * xtables_param_act - act on condition
863 * @status: a constant from enum xtables_exittype
864 *
865 * %XTF_ONLY_ONCE: print error message that option may only be used once.
866 * @p1: module name (e.g. "mark")
867 * @p2(...): option in conflict (e.g. "--mark")
868 * @p3(...): condition to match on (see extensions/ for examples)
869 *
870 * %XTF_NO_INVERT: option does not support inversion
871 * @p1: module name
872 * @p2: option in conflict
873 * @p3: condition to match on
874 *
875 * %XTF_BAD_VALUE: bad value for option
876 * @p1: module name
877 * @p2: option with which the problem occured (e.g. "--mark")
878 * @p3: string the user passed in (e.g. "99999999999999")
879 *
880 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
881 * @p1: module name
882 *
883 * Displays an error message and exits the program.
884 */
885void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000886{
887 const char *p2, *p3;
888 va_list args;
889 bool b;
890
891 va_start(args, p1);
892
893 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100894 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000895 p2 = va_arg(args, const char *);
896 b = va_arg(args, unsigned int);
897 if (!b)
898 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100899 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000900 "%s: \"%s\" option may only be specified once",
901 p1, p2);
902 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100903 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000904 p2 = va_arg(args, const char *);
905 b = va_arg(args, unsigned int);
906 if (!b)
907 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100908 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000909 "%s: \"%s\" option cannot be inverted", p1, p2);
910 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100911 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000912 p2 = va_arg(args, const char *);
913 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100914 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000915 "%s: Bad value for \"%s\" option: \"%s\"",
916 p1, p2, p3);
917 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100918 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000919 b = va_arg(args, unsigned int);
920 if (!b)
921 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100922 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000923 "%s: At most one action is possible", p1);
924 break;
925 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100926 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000927 break;
928 }
929
930 va_end(args);
931}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000932
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100933const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000934{
935 static char buf[20];
936 const unsigned char *bytep = (const void *)&addrp->s_addr;
937
938 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
939 return buf;
940}
941
942static const char *ipaddr_to_host(const struct in_addr *addr)
943{
944 struct hostent *host;
945
946 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
947 if (host == NULL)
948 return NULL;
949
950 return host->h_name;
951}
952
953static const char *ipaddr_to_network(const struct in_addr *addr)
954{
955 struct netent *net;
956
957 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
958 return net->n_name;
959
960 return NULL;
961}
962
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100963const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000964{
965 const char *name;
966
967 if ((name = ipaddr_to_host(addr)) != NULL ||
968 (name = ipaddr_to_network(addr)) != NULL)
969 return name;
970
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100971 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000972}
973
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100974const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000975{
976 static char buf[20];
977 uint32_t maskaddr, bits;
978 int i;
979
980 maskaddr = ntohl(mask->s_addr);
981
982 if (maskaddr == 0xFFFFFFFFL)
983 /* we don't want to see "/32" */
984 return "";
985
986 i = 32;
987 bits = 0xFFFFFFFEL;
988 while (--i >= 0 && maskaddr != bits)
989 bits <<= 1;
990 if (i >= 0)
991 sprintf(buf, "/%d", i);
992 else
993 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100994 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000995
996 return buf;
997}
998
Jan Engelhardtbd943842008-01-20 13:38:08 +0000999static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1000{
1001 static struct in_addr addr;
1002 unsigned char *addrp;
1003 unsigned int onebyte;
1004 char buf[20], *p, *q;
1005 int i;
1006
1007 /* copy dotted string, because we need to modify it */
1008 strncpy(buf, dotted, sizeof(buf) - 1);
1009 buf[sizeof(buf) - 1] = '\0';
1010 addrp = (void *)&addr.s_addr;
1011
1012 p = buf;
1013 for (i = 0; i < 3; ++i) {
1014 if ((q = strchr(p, '.')) == NULL) {
1015 if (is_mask)
1016 return NULL;
1017
1018 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001019 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001020 return NULL;
1021
1022 addrp[i] = onebyte;
1023 while (i < 3)
1024 addrp[++i] = 0;
1025
1026 return &addr;
1027 }
1028
1029 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001030 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001031 return NULL;
1032
1033 addrp[i] = onebyte;
1034 p = q + 1;
1035 }
1036
1037 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001038 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001039 return NULL;
1040
1041 addrp[3] = onebyte;
1042 return &addr;
1043}
1044
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001045struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001046{
1047 return __numeric_to_ipaddr(dotted, false);
1048}
1049
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001050struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001051{
1052 return __numeric_to_ipaddr(dotted, true);
1053}
1054
1055static struct in_addr *network_to_ipaddr(const char *name)
1056{
1057 static struct in_addr addr;
1058 struct netent *net;
1059
1060 if ((net = getnetbyname(name)) != NULL) {
1061 if (net->n_addrtype != AF_INET)
1062 return NULL;
1063 addr.s_addr = htonl(net->n_net);
1064 return &addr;
1065 }
1066
1067 return NULL;
1068}
1069
1070static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1071{
1072 struct hostent *host;
1073 struct in_addr *addr;
1074 unsigned int i;
1075
1076 *naddr = 0;
1077 if ((host = gethostbyname(name)) != NULL) {
1078 if (host->h_addrtype != AF_INET ||
1079 host->h_length != sizeof(struct in_addr))
1080 return NULL;
1081
1082 while (host->h_addr_list[*naddr] != NULL)
1083 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001084 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001085 for (i = 0; i < *naddr; i++)
1086 memcpy(&addr[i], host->h_addr_list[i],
1087 sizeof(struct in_addr));
1088 return addr;
1089 }
1090
1091 return NULL;
1092}
1093
1094static struct in_addr *
1095ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1096{
1097 struct in_addr *addrptmp, *addrp;
1098
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001099 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001100 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001101 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001102 memcpy(addrp, addrptmp, sizeof(*addrp));
1103 *naddrs = 1;
1104 return addrp;
1105 }
1106 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1107 return addrptmp;
1108
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001109 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001110}
1111
1112static struct in_addr *parse_ipmask(const char *mask)
1113{
1114 static struct in_addr maskaddr;
1115 struct in_addr *addrp;
1116 unsigned int bits;
1117
1118 if (mask == NULL) {
1119 /* no mask at all defaults to 32 bits */
1120 maskaddr.s_addr = 0xFFFFFFFF;
1121 return &maskaddr;
1122 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001123 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001124 /* dotted_to_addr already returns a network byte order addr */
1125 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001126 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001127 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001128 "invalid mask `%s' specified", mask);
1129 if (bits != 0) {
1130 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1131 return &maskaddr;
1132 }
1133
1134 maskaddr.s_addr = 0U;
1135 return &maskaddr;
1136}
1137
Jan Engelhardta0baae82009-01-30 04:32:50 +01001138/**
1139 * xtables_ipparse_any - transform arbitrary name to in_addr
1140 *
1141 * Possible inputs (pseudo regex):
1142 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1143 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1144 */
1145void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1146 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001147{
1148 unsigned int i, j, k, n;
1149 struct in_addr *addrp;
1150 char buf[256], *p;
1151
1152 strncpy(buf, name, sizeof(buf) - 1);
1153 buf[sizeof(buf) - 1] = '\0';
1154 if ((p = strrchr(buf, '/')) != NULL) {
1155 *p = '\0';
1156 addrp = parse_ipmask(p + 1);
1157 } else {
1158 addrp = parse_ipmask(NULL);
1159 }
1160 memcpy(maskp, addrp, sizeof(*maskp));
1161
1162 /* if a null mask is given, the name is ignored, like in "any/0" */
1163 if (maskp->s_addr == 0U)
1164 strcpy(buf, "0.0.0.0");
1165
1166 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1167 n = *naddrs;
1168 for (i = 0, j = 0; i < n; ++i) {
1169 addrp[j++].s_addr &= maskp->s_addr;
1170 for (k = 0; k < j - 1; ++k)
1171 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1172 --*naddrs;
1173 --j;
1174 break;
1175 }
1176 }
1177}
1178
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001179const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001180{
1181 /* 0000:0000:0000:0000:0000:000.000.000.000
1182 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1183 static char buf[50+1];
1184 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1185}
1186
1187static const char *ip6addr_to_host(const struct in6_addr *addr)
1188{
1189 static char hostname[NI_MAXHOST];
1190 struct sockaddr_in6 saddr;
1191 int err;
1192
1193 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1194 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1195 saddr.sin6_family = AF_INET6;
1196
1197 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1198 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1199 if (err != 0) {
1200#ifdef DEBUG
1201 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1202#endif
1203 return NULL;
1204 }
1205
1206#ifdef DEBUG
1207 fprintf (stderr, "\naddr2host: %s\n", hostname);
1208#endif
1209 return hostname;
1210}
1211
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001212const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001213{
1214 const char *name;
1215
1216 if ((name = ip6addr_to_host(addr)) != NULL)
1217 return name;
1218
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001219 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001220}
1221
1222static int ip6addr_prefix_length(const struct in6_addr *k)
1223{
1224 unsigned int bits = 0;
1225 uint32_t a, b, c, d;
1226
Jan Engelhardt48607812008-06-10 15:17:53 +02001227 a = ntohl(k->s6_addr32[0]);
1228 b = ntohl(k->s6_addr32[1]);
1229 c = ntohl(k->s6_addr32[2]);
1230 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001231 while (a & 0x80000000U) {
1232 ++bits;
1233 a <<= 1;
1234 a |= (b >> 31) & 1;
1235 b <<= 1;
1236 b |= (c >> 31) & 1;
1237 c <<= 1;
1238 c |= (d >> 31) & 1;
1239 d <<= 1;
1240 }
1241 if (a != 0 || b != 0 || c != 0 || d != 0)
1242 return -1;
1243 return bits;
1244}
1245
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001246const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001247{
1248 static char buf[50+2];
1249 int l = ip6addr_prefix_length(addrp);
1250
1251 if (l == -1) {
1252 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001253 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001254 return buf;
1255 }
1256 sprintf(buf, "/%d", l);
1257 return buf;
1258}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001259
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001260struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001261{
1262 static struct in6_addr ap;
1263 int err;
1264
1265 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1266 return &ap;
1267#ifdef DEBUG
1268 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1269#endif
1270 return NULL;
1271}
1272
1273static struct in6_addr *
1274host_to_ip6addr(const char *name, unsigned int *naddr)
1275{
1276 static struct in6_addr *addr;
1277 struct addrinfo hints;
1278 struct addrinfo *res;
1279 int err;
1280
1281 memset(&hints, 0, sizeof(hints));
1282 hints.ai_flags = AI_CANONNAME;
1283 hints.ai_family = AF_INET6;
1284 hints.ai_socktype = SOCK_RAW;
1285 hints.ai_protocol = IPPROTO_IPV6;
1286 hints.ai_next = NULL;
1287
1288 *naddr = 0;
1289 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1290#ifdef DEBUG
1291 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1292#endif
1293 return NULL;
1294 } else {
1295 if (res->ai_family != AF_INET6 ||
1296 res->ai_addrlen != sizeof(struct sockaddr_in6))
1297 return NULL;
1298
1299#ifdef DEBUG
1300 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1301 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1302#endif
1303 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001304 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001305 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1306 sizeof(struct in6_addr));
1307 freeaddrinfo(res);
1308 *naddr = 1;
1309 return addr;
1310 }
1311
1312 return NULL;
1313}
1314
1315static struct in6_addr *network_to_ip6addr(const char *name)
1316{
1317 /* abort();*/
1318 /* TODO: not implemented yet, but the exception breaks the
1319 * name resolvation */
1320 return NULL;
1321}
1322
1323static struct in6_addr *
1324ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1325{
1326 struct in6_addr *addrp, *addrptmp;
1327
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001328 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001329 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001330 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001331 memcpy(addrp, addrptmp, sizeof(*addrp));
1332 *naddrs = 1;
1333 return addrp;
1334 }
1335 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1336 return addrp;
1337
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001338 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001339}
1340
1341static struct in6_addr *parse_ip6mask(char *mask)
1342{
1343 static struct in6_addr maskaddr;
1344 struct in6_addr *addrp;
1345 unsigned int bits;
1346
1347 if (mask == NULL) {
1348 /* no mask at all defaults to 128 bits */
1349 memset(&maskaddr, 0xff, sizeof maskaddr);
1350 return &maskaddr;
1351 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001352 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001353 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001354 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001355 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001356 "invalid mask `%s' specified", mask);
1357 if (bits != 0) {
1358 char *p = (void *)&maskaddr;
1359 memset(p, 0xff, bits / 8);
1360 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1361 p[bits/8] = 0xff << (8 - (bits & 7));
1362 return &maskaddr;
1363 }
1364
1365 memset(&maskaddr, 0, sizeof(maskaddr));
1366 return &maskaddr;
1367}
1368
Jan Engelhardta0baae82009-01-30 04:32:50 +01001369void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1370 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001371{
1372 struct in6_addr *addrp;
1373 unsigned int i, j, k, n;
1374 char buf[256], *p;
1375
1376 strncpy(buf, name, sizeof(buf) - 1);
1377 buf[sizeof(buf)-1] = '\0';
1378 if ((p = strrchr(buf, '/')) != NULL) {
1379 *p = '\0';
1380 addrp = parse_ip6mask(p + 1);
1381 } else {
1382 addrp = parse_ip6mask(NULL);
1383 }
1384 memcpy(maskp, addrp, sizeof(*maskp));
1385
1386 /* if a null mask is given, the name is ignored, like in "any/0" */
1387 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1388 strcpy(buf, "::");
1389
1390 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1391 n = *naddrs;
1392 for (i = 0, j = 0; i < n; ++i) {
1393 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001394 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001395 ++j;
1396 for (k = 0; k < j - 1; ++k)
1397 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1398 --*naddrs;
1399 --j;
1400 break;
1401 }
1402 }
1403}
Max Kellermanna5d09942008-01-29 13:44:34 +00001404
Jan Engelhardta0baae82009-01-30 04:32:50 +01001405void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001406{
1407 static const char no_quote_chars[] = "_-0123456789"
1408 "abcdefghijklmnopqrstuvwxyz"
1409 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1410 static const char escape_chars[] = "\"\\'";
1411 size_t length;
1412 const char *p;
1413
1414 length = strcspn(value, no_quote_chars);
1415 if (length > 0 && value[length] == 0) {
1416 /* no quoting required */
1417 fputs(value, stdout);
1418 putchar(' ');
1419 } else {
1420 /* there is at least one dangerous character in the
1421 value, which we have to quote. Write double quotes
1422 around the value and escape special characters with
1423 a backslash */
1424 putchar('"');
1425
1426 for (p = strpbrk(value, escape_chars); p != NULL;
1427 p = strpbrk(value, escape_chars)) {
1428 if (p > value)
1429 fwrite(value, 1, p - value, stdout);
1430 putchar('\\');
1431 putchar(*p);
1432 value = p + 1;
1433 }
1434
1435 /* print the rest and finish the double quoted
1436 string */
1437 fputs(value, stdout);
1438 printf("\" ");
1439 }
1440}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001441
1442/**
1443 * Check for option-intrapositional negation.
1444 * Do not use in new code.
1445 */
1446int xtables_check_inverse(const char option[], int *invert,
1447 int *my_optind, int argc)
1448{
1449 if (option && strcmp(option, "!") == 0) {
1450 fprintf(stderr, "Using intrapositioned negation "
1451 "(`--option ! this`) is deprecated in favor of "
1452 "extrapositioned (`! --option this`).\n");
1453
1454 if (*invert)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001455 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001456 "Multiple `!' flags not allowed");
1457 *invert = true;
1458 if (my_optind != NULL) {
1459 ++*my_optind;
1460 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001461 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001462 "no argument following `!'");
1463 }
1464
1465 return true;
1466 }
1467 return false;
1468}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001469
1470const struct xtables_pprot xtables_chain_protos[] = {
1471 {"tcp", IPPROTO_TCP},
1472 {"sctp", IPPROTO_SCTP},
1473 {"udp", IPPROTO_UDP},
1474 {"udplite", IPPROTO_UDPLITE},
1475 {"icmp", IPPROTO_ICMP},
1476 {"icmpv6", IPPROTO_ICMPV6},
1477 {"ipv6-icmp", IPPROTO_ICMPV6},
1478 {"esp", IPPROTO_ESP},
1479 {"ah", IPPROTO_AH},
1480 {"ipv6-mh", IPPROTO_MH},
1481 {"mh", IPPROTO_MH},
1482 {"all", 0},
1483 {NULL},
1484};
1485
1486u_int16_t
1487xtables_parse_protocol(const char *s)
1488{
1489 unsigned int proto;
1490
1491 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1492 struct protoent *pent;
1493
1494 /* first deal with the special case of 'all' to prevent
1495 * people from being able to redefine 'all' in nsswitch
1496 * and/or provoke expensive [not working] ldap/nis/...
1497 * lookups */
1498 if (!strcmp(s, "all"))
1499 return 0;
1500
1501 if ((pent = getprotobyname(s)))
1502 proto = pent->p_proto;
1503 else {
1504 unsigned int i;
1505 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1506 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1507 proto = xtables_chain_protos[i].num;
1508 break;
1509 }
1510 }
1511 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001512 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001513 "unknown protocol `%s' specified",
1514 s);
1515 }
1516 }
1517
1518 return proto;
1519}