blob: 47a0d9cbfbe91772fabf2f7386dfd30d181c1484 [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000019#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000021#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000022#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000023#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000024#include <stdio.h>
25#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000026#include <string.h>
27#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000028#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000032#include <arpa/inet.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000034#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +010035#include <limits.h> /* INT_MAX in ip_tables.h/ip6_tables.h */
Jan Engelhardt77f48c22009-02-07 19:59:53 +010036#include <linux/netfilter_ipv4/ip_tables.h>
37#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020038#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000040#ifndef NO_SHARED_LIBS
41#include <dlfcn.h>
42#endif
Jan Engelhardtc31870f2009-02-10 10:48:28 +010043#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
44# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
45# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
46#endif
47#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
48# define IP6T_SO_GET_REVISION_MATCH 68
49# define IP6T_SO_GET_REVISION_TARGET 69
50#endif
Jamal Hadi Salim70581922009-02-13 08:36:44 -050051#include <getopt.h>
Jan Engelhardtc31870f2009-02-10 10:48:28 +010052
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000053
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000054#define NPROTO 255
55
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000056#ifndef PROC_SYS_MODPROBE
57#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
58#endif
59
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010060void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
61
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010062struct xtables_globals *xt_params = NULL;
63
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010064void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010065{
66 va_list args;
67
68 va_start(args, msg);
69 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
70 vfprintf(stderr, msg, args);
71 va_end(args);
72 fprintf(stderr, "\n");
73 exit(status);
74}
75
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010076
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -050077void xtables_free_opts(int reset_offset)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010078{
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -050079 if (xt_params->opts != xt_params->orig_opts) {
80 free(xt_params->opts);
81 xt_params->opts = xt_params->orig_opts;
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010082 if (reset_offset)
83 xt_params->option_offset = 0;
84 }
85}
86
Jamal Hadi Salim70581922009-02-13 08:36:44 -050087struct option *xtables_merge_options(struct option *oldopts,
88 const struct option *newopts,
89 unsigned int *option_offset)
90{
91 unsigned int num_old, num_new, i;
92 struct option *merge;
93
94 if (newopts == NULL)
95 return oldopts;
96
97 for (num_old = 0; oldopts[num_old].name; num_old++) ;
98 for (num_new = 0; newopts[num_new].name; num_new++) ;
99
Jan Engelhardtbddcb922009-02-21 02:48:11 +0100100 xt_params->option_offset += 256;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500101 *option_offset = xt_params->option_offset;
102
103 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
104 if (merge == NULL)
105 return NULL;
106 memcpy(merge, oldopts, num_old * sizeof(struct option));
107 xtables_free_opts(0); /* Release any old options merged */
108 for (i = 0; i < num_new; i++) {
109 merge[num_old + i] = newopts[i];
110 merge[num_old + i].val += *option_offset;
111 }
112 memset(merge + num_old + num_new, 0, sizeof(struct option));
113
114 return merge;
115}
116
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100117/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100118 * xtables_afinfo - protocol family dependent information
119 * @kmod: kernel module basename (e.g. "ip_tables")
120 * @libprefix: prefix of .so library name (e.g. "libipt_")
121 * @family: nfproto family
122 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
123 * @so_rev_match: optname to check revision support of match
124 * @so_rev_target: optname to check revision support of target
125 */
126struct xtables_afinfo {
127 const char *kmod;
128 const char *libprefix;
129 uint8_t family;
130 uint8_t ipproto;
131 int so_rev_match;
132 int so_rev_target;
133};
134
135static const struct xtables_afinfo afinfo_ipv4 = {
136 .kmod = "ip_tables",
137 .libprefix = "libipt_",
138 .family = NFPROTO_IPV4,
139 .ipproto = IPPROTO_IP,
140 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
141 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
142};
143
144static const struct xtables_afinfo afinfo_ipv6 = {
145 .kmod = "ip6_tables",
146 .libprefix = "libip6t_",
147 .family = NFPROTO_IPV6,
148 .ipproto = IPPROTO_IPV6,
149 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
150 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
151};
152
153static const struct xtables_afinfo *afinfo;
154
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100155/* Search path for Xtables .so files */
156static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000157
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000158/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100159const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000160
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000161/* Keeping track of external matches and targets: linked lists. */
162struct xtables_match *xtables_matches;
163struct xtables_target *xtables_targets;
164
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100165void xtables_init(void)
166{
167 xtables_libdir = getenv("XTABLES_LIBDIR");
168 if (xtables_libdir != NULL)
169 return;
170 xtables_libdir = getenv("IPTABLES_LIB_DIR");
171 if (xtables_libdir != NULL) {
172 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
173 "use XTABLES_LIBDIR.\n");
174 return;
175 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100176 /*
177 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
178 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
179 * for these env vars are deprecated anyhow, and in light of the
180 * (shared) libxt_*.so files, makes less sense to have
181 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
182 */
183 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
184 if (xtables_libdir != NULL) {
185 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
186 "use XTABLES_LIBDIR.\n");
187 return;
188 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100189 xtables_libdir = XTABLES_LIBDIR;
190}
191
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100192void xtables_set_nfproto(uint8_t nfproto)
193{
194 switch (nfproto) {
195 case NFPROTO_IPV4:
196 afinfo = &afinfo_ipv4;
197 break;
198 case NFPROTO_IPV6:
199 afinfo = &afinfo_ipv6;
200 break;
201 default:
202 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
203 __func__);
204 }
205}
206
Jan Engelhardt630ef482009-01-27 14:58:41 +0100207/**
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500208 * xtables_set_params - set the global parameters used by xtables
209 * @xtp: input xtables_globals structure
210 *
211 * The app is expected to pass a valid xtables_globals data-filled
212 * with proper values
213 * @xtp cannot be NULL
214 *
215 * Returns -1 on failure to set and 0 on success
216 */
217int xtables_set_params(struct xtables_globals *xtp)
218{
219 if (!xtp) {
220 fprintf(stderr, "%s: Illegal global params\n",__func__);
221 return -1;
222 }
223
224 xt_params = xtp;
225
226 if (!xt_params->exit_err)
227 xt_params->exit_err = basic_exit_err;
228
229 return 0;
230}
231
232int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto)
233{
234 xtables_init();
235 xtables_set_nfproto(nfproto);
236 return xtables_set_params(xtp);
237}
238
239/**
Jan Engelhardt630ef482009-01-27 14:58:41 +0100240 * xtables_*alloc - wrappers that exit on failure
241 */
242void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000243{
244 void *p;
245
246 if ((p = calloc(count, size)) == NULL) {
247 perror("ip[6]tables: calloc failed");
248 exit(1);
249 }
250
251 return p;
252}
253
Jan Engelhardt630ef482009-01-27 14:58:41 +0100254void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000255{
256 void *p;
257
258 if ((p = malloc(size)) == NULL) {
259 perror("ip[6]tables: malloc failed");
260 exit(1);
261 }
262
263 return p;
264}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000265
Michael Granzow332e4ac2009-04-09 18:24:36 +0100266void *xtables_realloc(void *ptr, size_t size)
267{
268 void *p;
269
270 if ((p = realloc(ptr, size)) == NULL) {
271 perror("ip[6]tables: realloc failed");
272 exit(1);
273 }
274
275 return p;
276}
277
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000278static 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
Jan Engelhardt371cea22010-07-25 23:36:17 +0200288 ret = malloc(PROCFILE_BUFSIZ);
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000289 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
Jan Engelhardt94aa2ea2009-10-11 03:56:18 -0400326 switch (vfork()) {
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000327 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{
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100445 unsigned int vialen = strlen(arg);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000446 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);
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100457 if (vialen == 0)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000458 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++) {
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100468 if (vianame[i] == '/' ||
469 vianame[i] == ' ') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000470 fprintf(stderr,
471 "Warning: weird character in interface"
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100472 " `%s' ('/' and ' ' are not allowed by the kernel).\n",
Max Kellermannaae4f822007-10-17 16:36:49 +0000473 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000474 break;
475 }
476 }
477 }
478}
479
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200480#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100481static void *load_extension(const char *search_path, const char *prefix,
482 const char *name, bool is_target)
483{
484 const char *dir = search_path, *next;
485 void *ptr = NULL;
486 struct stat sb;
487 char path[256];
488
489 do {
490 next = strchr(dir, ':');
491 if (next == NULL)
492 next = dir + strlen(dir);
493 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200494 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100495
496 if (dlopen(path, RTLD_NOW) != NULL) {
497 /* Found library. If it didn't register itself,
498 maybe they specified target as match. */
499 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100500 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100501 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100502 ptr = xtables_find_match(name,
503 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100504 } else if (stat(path, &sb) == 0) {
505 fprintf(stderr, "%s: %s\n", path, dlerror());
506 }
507
508 if (ptr != NULL)
509 return ptr;
510
511 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200512 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100513 if (dlopen(path, RTLD_NOW) != NULL) {
514 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100515 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100516 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100517 ptr = xtables_find_match(name,
518 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100519 } else if (stat(path, &sb) == 0) {
520 fprintf(stderr, "%s: %s\n", path, dlerror());
521 }
522
523 if (ptr != NULL)
524 return ptr;
525
526 dir = next + 1;
527 } while (*next != '\0');
528
529 return NULL;
530}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200531#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100532
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100533struct xtables_match *
534xtables_find_match(const char *name, enum xtables_tryload tryload,
535 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000536{
537 struct xtables_match *ptr;
538 const char *icmp6 = "icmp6";
539
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200540 if (strlen(name) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100541 xtables_error(PARAMETER_PROBLEM,
542 "Invalid match name \"%s\" (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200543 name, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100544
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000545 /* This is ugly as hell. Nonetheless, there is no way of changing
546 * this without hurting backwards compatibility */
547 if ( (strcmp(name,"icmpv6") == 0) ||
548 (strcmp(name,"ipv6-icmp") == 0) ||
549 (strcmp(name,"icmp6") == 0) )
550 name = icmp6;
551
552 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
553 if (strcmp(name, ptr->name) == 0) {
554 struct xtables_match *clone;
555
556 /* First match of this type: */
557 if (ptr->m == NULL)
558 break;
559
560 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100561 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000562 memcpy(clone, ptr, sizeof(struct xtables_match));
563 clone->mflags = 0;
564 /* This is a clone: */
565 clone->next = clone;
566
567 ptr = clone;
568 break;
569 }
570 }
571
572#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100573 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100574 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100575 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000576
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100577 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100578 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000579 "Couldn't load match `%s':%s\n",
580 name, dlerror());
581 }
582#else
583 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100584 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000585 ptr->loaded = 1;
586 else
587 ptr = NULL;
588 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100589 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100590 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000591 "Couldn't find match `%s'\n", name);
592 }
593#endif
594
595 if (ptr && matches) {
596 struct xtables_rule_match **i;
597 struct xtables_rule_match *newentry;
598
Jan Engelhardt630ef482009-01-27 14:58:41 +0100599 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000600
601 for (i = matches; *i; i = &(*i)->next) {
602 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100603 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000604 }
605 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100606 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000607 newentry->next = NULL;
608 *i = newentry;
609 }
610
611 return ptr;
612}
613
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100614struct xtables_target *
615xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000616{
617 struct xtables_target *ptr;
618
619 /* Standard target? */
620 if (strcmp(name, "") == 0
621 || strcmp(name, XTC_LABEL_ACCEPT) == 0
622 || strcmp(name, XTC_LABEL_DROP) == 0
623 || strcmp(name, XTC_LABEL_QUEUE) == 0
624 || strcmp(name, XTC_LABEL_RETURN) == 0)
625 name = "standard";
626
627 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
628 if (strcmp(name, ptr->name) == 0)
629 break;
630 }
631
632#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100633 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100634 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100635 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000636
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100637 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100638 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000639 "Couldn't load target `%s':%s\n",
640 name, dlerror());
641 }
642#else
643 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100644 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000645 ptr->loaded = 1;
646 else
647 ptr = NULL;
648 }
Peter Volkov854d2d92009-03-24 11:09:16 +0300649 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100650 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000651 "Couldn't find target `%s'\n", name);
652 }
653#endif
654
655 if (ptr)
656 ptr->used = 1;
657
658 return ptr;
659}
660
661static int compatible_revision(const char *name, u_int8_t revision, int opt)
662{
663 struct xt_get_revision rev;
664 socklen_t s = sizeof(rev);
665 int max_rev, sockfd;
666
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100667 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000668 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000669 if (errno == EPERM) {
670 /* revision 0 is always supported. */
671 if (revision != 0)
672 fprintf(stderr, "Could not determine whether "
673 "revision %u is supported, "
674 "assuming it is.\n",
675 revision);
676 return 1;
677 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000678 fprintf(stderr, "Could not open socket to kernel: %s\n",
679 strerror(errno));
680 exit(1);
681 }
682
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100683 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000684
685 strcpy(rev.name, name);
686 rev.revision = revision;
687
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100688 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000689 if (max_rev < 0) {
690 /* Definitely don't support this? */
691 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
692 close(sockfd);
693 return 0;
694 } else if (errno == ENOPROTOOPT) {
695 close(sockfd);
696 /* Assume only revision 0 support (old kernel) */
697 return (revision == 0);
698 } else {
699 fprintf(stderr, "getsockopt failed strangely: %s\n",
700 strerror(errno));
701 exit(1);
702 }
703 }
704 close(sockfd);
705 return 1;
706}
707
708
709static int compatible_match_revision(const char *name, u_int8_t revision)
710{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100711 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000712}
713
714static int compatible_target_revision(const char *name, u_int8_t revision)
715{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100716 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000717}
718
719void xtables_register_match(struct xtables_match *me)
720{
721 struct xtables_match **i, *old;
722
Jan Engelhardtc284de52009-06-25 21:25:24 +0200723 if (me->version == NULL) {
724 fprintf(stderr, "%s: match %s<%u> is missing a version\n",
725 xt_params->program_name, me->name, me->revision);
726 exit(1);
727 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100728 if (strcmp(me->version, XTABLES_VERSION) != 0) {
729 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
730 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500731 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100732 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000733 exit(1);
734 }
735
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200736 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000737 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500738 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000739 exit(1);
740 }
741
742 if (me->family >= NPROTO) {
743 fprintf(stderr,
744 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500745 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000746 exit(1);
747 }
748
749 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100750 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000751 return;
752
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100753 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000754 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100755 if (old->revision == me->revision &&
756 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000757 fprintf(stderr,
758 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500759 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000760 exit(1);
761 }
762
763 /* Now we have two (or more) options, check compatibility. */
764 if (compatible_match_revision(old->name, old->revision)
765 && old->revision > me->revision)
766 return;
767
Jan Engelhardt23545c22008-02-14 04:23:04 +0100768 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000769 if (!compatible_match_revision(me->name, me->revision))
770 return;
771
Jan Engelhardt23545c22008-02-14 04:23:04 +0100772 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
773 if (old->revision == me->revision && me->family == AF_UNSPEC)
774 return;
775
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000776 /* Delete old one. */
777 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
778 *i = old->next;
779 }
780
781 if (me->size != XT_ALIGN(me->size)) {
782 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500783 xt_params->program_name, me->name,
784 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000785 exit(1);
786 }
787
788 /* Append to list. */
789 for (i = &xtables_matches; *i; i = &(*i)->next);
790 me->next = NULL;
791 *i = me;
792
793 me->m = NULL;
794 me->mflags = 0;
795}
796
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200797void xtables_register_matches(struct xtables_match *match, unsigned int n)
798{
799 do {
800 xtables_register_match(&match[--n]);
801 } while (n > 0);
802}
803
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000804void xtables_register_target(struct xtables_target *me)
805{
806 struct xtables_target *old;
807
Jan Engelhardtc284de52009-06-25 21:25:24 +0200808 if (me->version == NULL) {
809 fprintf(stderr, "%s: target %s<%u> is missing a version\n",
810 xt_params->program_name, me->name, me->revision);
811 exit(1);
812 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100813 if (strcmp(me->version, XTABLES_VERSION) != 0) {
814 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
815 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500816 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100817 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000818 exit(1);
819 }
820
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200821 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000822 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500823 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000824 exit(1);
825 }
826
827 if (me->family >= NPROTO) {
828 fprintf(stderr,
829 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500830 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000831 exit(1);
832 }
833
834 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100835 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000836 return;
837
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100838 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000839 if (old) {
840 struct xtables_target **i;
841
Jan Engelhardt23545c22008-02-14 04:23:04 +0100842 if (old->revision == me->revision &&
843 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000844 fprintf(stderr,
845 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500846 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000847 exit(1);
848 }
849
850 /* Now we have two (or more) options, check compatibility. */
851 if (compatible_target_revision(old->name, old->revision)
852 && old->revision > me->revision)
853 return;
854
Jan Engelhardt23545c22008-02-14 04:23:04 +0100855 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000856 if (!compatible_target_revision(me->name, me->revision))
857 return;
858
Jan Engelhardt23545c22008-02-14 04:23:04 +0100859 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
860 if (old->revision == me->revision && me->family == AF_UNSPEC)
861 return;
862
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000863 /* Delete old one. */
864 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
865 *i = old->next;
866 }
867
868 if (me->size != XT_ALIGN(me->size)) {
869 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500870 xt_params->program_name, me->name,
871 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000872 exit(1);
873 }
874
875 /* Prepend to list. */
876 me->next = xtables_targets;
877 xtables_targets = me;
878 me->t = NULL;
879 me->tflags = 0;
880}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000881
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200882void xtables_register_targets(struct xtables_target *target, unsigned int n)
883{
884 do {
885 xtables_register_target(&target[--n]);
886 } while (n > 0);
887}
888
Jan Engelhardta41545c2009-01-27 21:27:19 +0100889/**
890 * xtables_param_act - act on condition
891 * @status: a constant from enum xtables_exittype
892 *
893 * %XTF_ONLY_ONCE: print error message that option may only be used once.
894 * @p1: module name (e.g. "mark")
895 * @p2(...): option in conflict (e.g. "--mark")
896 * @p3(...): condition to match on (see extensions/ for examples)
897 *
898 * %XTF_NO_INVERT: option does not support inversion
899 * @p1: module name
900 * @p2: option in conflict
901 * @p3: condition to match on
902 *
903 * %XTF_BAD_VALUE: bad value for option
904 * @p1: module name
905 * @p2: option with which the problem occured (e.g. "--mark")
906 * @p3: string the user passed in (e.g. "99999999999999")
907 *
908 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
909 * @p1: module name
910 *
911 * Displays an error message and exits the program.
912 */
913void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000914{
915 const char *p2, *p3;
916 va_list args;
917 bool b;
918
919 va_start(args, p1);
920
921 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100922 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000923 p2 = va_arg(args, const char *);
924 b = va_arg(args, unsigned int);
925 if (!b)
926 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100927 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000928 "%s: \"%s\" option may only be specified once",
929 p1, p2);
930 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100931 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000932 p2 = va_arg(args, const char *);
933 b = va_arg(args, unsigned int);
934 if (!b)
935 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100936 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000937 "%s: \"%s\" option cannot be inverted", p1, p2);
938 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100939 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000940 p2 = va_arg(args, const char *);
941 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100942 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000943 "%s: Bad value for \"%s\" option: \"%s\"",
944 p1, p2, p3);
945 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100946 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000947 b = va_arg(args, unsigned int);
948 if (!b)
949 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100950 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000951 "%s: At most one action is possible", p1);
952 break;
953 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100954 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000955 break;
956 }
957
958 va_end(args);
959}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000960
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100961const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000962{
963 static char buf[20];
964 const unsigned char *bytep = (const void *)&addrp->s_addr;
965
966 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
967 return buf;
968}
969
970static const char *ipaddr_to_host(const struct in_addr *addr)
971{
972 struct hostent *host;
973
974 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
975 if (host == NULL)
976 return NULL;
977
978 return host->h_name;
979}
980
981static const char *ipaddr_to_network(const struct in_addr *addr)
982{
983 struct netent *net;
984
985 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
986 return net->n_name;
987
988 return NULL;
989}
990
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100991const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000992{
993 const char *name;
994
995 if ((name = ipaddr_to_host(addr)) != NULL ||
996 (name = ipaddr_to_network(addr)) != NULL)
997 return name;
998
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100999 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001000}
1001
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001002const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001003{
1004 static char buf[20];
1005 uint32_t maskaddr, bits;
1006 int i;
1007
1008 maskaddr = ntohl(mask->s_addr);
1009
1010 if (maskaddr == 0xFFFFFFFFL)
1011 /* we don't want to see "/32" */
1012 return "";
1013
1014 i = 32;
1015 bits = 0xFFFFFFFEL;
1016 while (--i >= 0 && maskaddr != bits)
1017 bits <<= 1;
1018 if (i >= 0)
1019 sprintf(buf, "/%d", i);
1020 else
1021 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001022 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001023
1024 return buf;
1025}
1026
Jan Engelhardtbd943842008-01-20 13:38:08 +00001027static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1028{
1029 static struct in_addr addr;
1030 unsigned char *addrp;
1031 unsigned int onebyte;
1032 char buf[20], *p, *q;
1033 int i;
1034
1035 /* copy dotted string, because we need to modify it */
1036 strncpy(buf, dotted, sizeof(buf) - 1);
1037 buf[sizeof(buf) - 1] = '\0';
1038 addrp = (void *)&addr.s_addr;
1039
1040 p = buf;
1041 for (i = 0; i < 3; ++i) {
1042 if ((q = strchr(p, '.')) == NULL) {
1043 if (is_mask)
1044 return NULL;
1045
1046 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001047 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001048 return NULL;
1049
1050 addrp[i] = onebyte;
1051 while (i < 3)
1052 addrp[++i] = 0;
1053
1054 return &addr;
1055 }
1056
1057 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001058 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001059 return NULL;
1060
1061 addrp[i] = onebyte;
1062 p = q + 1;
1063 }
1064
1065 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001066 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001067 return NULL;
1068
1069 addrp[3] = onebyte;
1070 return &addr;
1071}
1072
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001073struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001074{
1075 return __numeric_to_ipaddr(dotted, false);
1076}
1077
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001078struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001079{
1080 return __numeric_to_ipaddr(dotted, true);
1081}
1082
1083static struct in_addr *network_to_ipaddr(const char *name)
1084{
1085 static struct in_addr addr;
1086 struct netent *net;
1087
1088 if ((net = getnetbyname(name)) != NULL) {
1089 if (net->n_addrtype != AF_INET)
1090 return NULL;
1091 addr.s_addr = htonl(net->n_net);
1092 return &addr;
1093 }
1094
1095 return NULL;
1096}
1097
1098static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1099{
1100 struct hostent *host;
1101 struct in_addr *addr;
1102 unsigned int i;
1103
1104 *naddr = 0;
1105 if ((host = gethostbyname(name)) != NULL) {
1106 if (host->h_addrtype != AF_INET ||
1107 host->h_length != sizeof(struct in_addr))
1108 return NULL;
1109
1110 while (host->h_addr_list[*naddr] != NULL)
1111 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001112 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001113 for (i = 0; i < *naddr; i++)
1114 memcpy(&addr[i], host->h_addr_list[i],
1115 sizeof(struct in_addr));
1116 return addr;
1117 }
1118
1119 return NULL;
1120}
1121
1122static struct in_addr *
1123ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1124{
1125 struct in_addr *addrptmp, *addrp;
1126
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001127 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001128 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001129 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001130 memcpy(addrp, addrptmp, sizeof(*addrp));
1131 *naddrs = 1;
1132 return addrp;
1133 }
1134 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1135 return addrptmp;
1136
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001137 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001138}
1139
1140static struct in_addr *parse_ipmask(const char *mask)
1141{
1142 static struct in_addr maskaddr;
1143 struct in_addr *addrp;
1144 unsigned int bits;
1145
1146 if (mask == NULL) {
1147 /* no mask at all defaults to 32 bits */
1148 maskaddr.s_addr = 0xFFFFFFFF;
1149 return &maskaddr;
1150 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001151 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001152 /* dotted_to_addr already returns a network byte order addr */
1153 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001154 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001155 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001156 "invalid mask `%s' specified", mask);
1157 if (bits != 0) {
1158 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1159 return &maskaddr;
1160 }
1161
1162 maskaddr.s_addr = 0U;
1163 return &maskaddr;
1164}
1165
Michael Granzow332e4ac2009-04-09 18:24:36 +01001166void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp,
1167 struct in_addr **maskpp, unsigned int *naddrs)
1168{
1169 struct in_addr *addrp;
1170 char buf[256], *p;
1171 unsigned int len, i, j, n, count = 1;
1172 const char *loop = name;
1173
1174 while ((loop = strchr(loop, ',')) != NULL) {
1175 ++count;
1176 ++loop; /* skip ',' */
1177 }
1178
1179 *addrpp = xtables_malloc(sizeof(struct in_addr) * count);
1180 *maskpp = xtables_malloc(sizeof(struct in_addr) * count);
1181
1182 loop = name;
1183
1184 for (i = 0; i < count; ++i) {
1185 if (loop == NULL)
1186 break;
1187 if (*loop == ',')
1188 ++loop;
1189 if (*loop == '\0')
1190 break;
1191 p = strchr(loop, ',');
1192 if (p != NULL)
1193 len = p - loop;
1194 else
1195 len = strlen(loop);
1196 if (len == 0 || sizeof(buf) - 1 < len)
1197 break;
1198
1199 strncpy(buf, loop, len);
1200 buf[len] = '\0';
1201 loop += len;
1202 if ((p = strrchr(buf, '/')) != NULL) {
1203 *p = '\0';
1204 addrp = parse_ipmask(p + 1);
1205 } else {
1206 addrp = parse_ipmask(NULL);
1207 }
1208 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1209
1210 /* if a null mask is given, the name is ignored, like in "any/0" */
1211 if ((*maskpp + i)->s_addr == 0)
1212 /*
1213 * A bit pointless to process multiple addresses
1214 * in this case...
1215 */
1216 strcpy(buf, "0.0.0.0");
1217
1218 addrp = ipparse_hostnetwork(buf, &n);
1219 if (n > 1) {
1220 count += n - 1;
1221 *addrpp = xtables_realloc(*addrpp,
1222 sizeof(struct in_addr) * count);
1223 *maskpp = xtables_realloc(*maskpp,
1224 sizeof(struct in_addr) * count);
1225 for (j = 0; j < n; ++j)
1226 /* for each new addr */
1227 memcpy(*addrpp + i + j, addrp + j,
1228 sizeof(*addrp));
1229 for (j = 1; j < n; ++j)
1230 /* for each new mask */
1231 memcpy(*maskpp + i + j, *maskpp + i,
1232 sizeof(*addrp));
1233 i += n - 1;
1234 } else {
1235 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1236 }
1237 /* free what ipparse_hostnetwork had allocated: */
1238 free(addrp);
1239 }
1240 *naddrs = count;
1241 for (i = 0; i < n; ++i)
1242 (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr;
1243}
1244
1245
Jan Engelhardta0baae82009-01-30 04:32:50 +01001246/**
1247 * xtables_ipparse_any - transform arbitrary name to in_addr
1248 *
1249 * Possible inputs (pseudo regex):
1250 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1251 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1252 */
1253void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1254 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001255{
1256 unsigned int i, j, k, n;
1257 struct in_addr *addrp;
1258 char buf[256], *p;
1259
1260 strncpy(buf, name, sizeof(buf) - 1);
1261 buf[sizeof(buf) - 1] = '\0';
1262 if ((p = strrchr(buf, '/')) != NULL) {
1263 *p = '\0';
1264 addrp = parse_ipmask(p + 1);
1265 } else {
1266 addrp = parse_ipmask(NULL);
1267 }
1268 memcpy(maskp, addrp, sizeof(*maskp));
1269
1270 /* if a null mask is given, the name is ignored, like in "any/0" */
1271 if (maskp->s_addr == 0U)
1272 strcpy(buf, "0.0.0.0");
1273
1274 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1275 n = *naddrs;
1276 for (i = 0, j = 0; i < n; ++i) {
1277 addrp[j++].s_addr &= maskp->s_addr;
1278 for (k = 0; k < j - 1; ++k)
1279 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1280 --*naddrs;
1281 --j;
1282 break;
1283 }
1284 }
1285}
1286
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001287const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001288{
1289 /* 0000:0000:0000:0000:0000:000.000.000.000
1290 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1291 static char buf[50+1];
1292 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1293}
1294
1295static const char *ip6addr_to_host(const struct in6_addr *addr)
1296{
1297 static char hostname[NI_MAXHOST];
1298 struct sockaddr_in6 saddr;
1299 int err;
1300
1301 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1302 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1303 saddr.sin6_family = AF_INET6;
1304
1305 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1306 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1307 if (err != 0) {
1308#ifdef DEBUG
1309 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1310#endif
1311 return NULL;
1312 }
1313
1314#ifdef DEBUG
1315 fprintf (stderr, "\naddr2host: %s\n", hostname);
1316#endif
1317 return hostname;
1318}
1319
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001320const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001321{
1322 const char *name;
1323
1324 if ((name = ip6addr_to_host(addr)) != NULL)
1325 return name;
1326
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001327 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001328}
1329
1330static int ip6addr_prefix_length(const struct in6_addr *k)
1331{
1332 unsigned int bits = 0;
1333 uint32_t a, b, c, d;
1334
Jan Engelhardt48607812008-06-10 15:17:53 +02001335 a = ntohl(k->s6_addr32[0]);
1336 b = ntohl(k->s6_addr32[1]);
1337 c = ntohl(k->s6_addr32[2]);
1338 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001339 while (a & 0x80000000U) {
1340 ++bits;
1341 a <<= 1;
1342 a |= (b >> 31) & 1;
1343 b <<= 1;
1344 b |= (c >> 31) & 1;
1345 c <<= 1;
1346 c |= (d >> 31) & 1;
1347 d <<= 1;
1348 }
1349 if (a != 0 || b != 0 || c != 0 || d != 0)
1350 return -1;
1351 return bits;
1352}
1353
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001354const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001355{
1356 static char buf[50+2];
1357 int l = ip6addr_prefix_length(addrp);
1358
1359 if (l == -1) {
1360 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001361 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001362 return buf;
1363 }
1364 sprintf(buf, "/%d", l);
1365 return buf;
1366}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001367
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001368struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001369{
1370 static struct in6_addr ap;
1371 int err;
1372
1373 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1374 return &ap;
1375#ifdef DEBUG
1376 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1377#endif
1378 return NULL;
1379}
1380
1381static struct in6_addr *
1382host_to_ip6addr(const char *name, unsigned int *naddr)
1383{
1384 static struct in6_addr *addr;
1385 struct addrinfo hints;
1386 struct addrinfo *res;
1387 int err;
1388
1389 memset(&hints, 0, sizeof(hints));
1390 hints.ai_flags = AI_CANONNAME;
1391 hints.ai_family = AF_INET6;
1392 hints.ai_socktype = SOCK_RAW;
1393 hints.ai_protocol = IPPROTO_IPV6;
1394 hints.ai_next = NULL;
1395
1396 *naddr = 0;
1397 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1398#ifdef DEBUG
1399 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1400#endif
1401 return NULL;
1402 } else {
1403 if (res->ai_family != AF_INET6 ||
1404 res->ai_addrlen != sizeof(struct sockaddr_in6))
1405 return NULL;
1406
1407#ifdef DEBUG
1408 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
Patrick McHardy30290ae2010-05-20 15:41:03 +02001409 xtables_ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001410#endif
1411 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001412 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001413 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1414 sizeof(struct in6_addr));
1415 freeaddrinfo(res);
1416 *naddr = 1;
1417 return addr;
1418 }
1419
1420 return NULL;
1421}
1422
1423static struct in6_addr *network_to_ip6addr(const char *name)
1424{
1425 /* abort();*/
1426 /* TODO: not implemented yet, but the exception breaks the
1427 * name resolvation */
1428 return NULL;
1429}
1430
1431static struct in6_addr *
1432ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1433{
1434 struct in6_addr *addrp, *addrptmp;
1435
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001436 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001437 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001438 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001439 memcpy(addrp, addrptmp, sizeof(*addrp));
1440 *naddrs = 1;
1441 return addrp;
1442 }
1443 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1444 return addrp;
1445
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001446 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001447}
1448
1449static struct in6_addr *parse_ip6mask(char *mask)
1450{
1451 static struct in6_addr maskaddr;
1452 struct in6_addr *addrp;
1453 unsigned int bits;
1454
1455 if (mask == NULL) {
1456 /* no mask at all defaults to 128 bits */
1457 memset(&maskaddr, 0xff, sizeof maskaddr);
1458 return &maskaddr;
1459 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001460 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001461 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001462 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001463 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001464 "invalid mask `%s' specified", mask);
1465 if (bits != 0) {
1466 char *p = (void *)&maskaddr;
1467 memset(p, 0xff, bits / 8);
1468 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1469 p[bits/8] = 0xff << (8 - (bits & 7));
1470 return &maskaddr;
1471 }
1472
1473 memset(&maskaddr, 0, sizeof(maskaddr));
1474 return &maskaddr;
1475}
1476
Michael Granzow332e4ac2009-04-09 18:24:36 +01001477void
1478xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp,
1479 struct in6_addr **maskpp, unsigned int *naddrs)
1480{
Olaf Rempel58df9012009-09-20 13:24:11 +02001481 static const struct in6_addr zero_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001482 struct in6_addr *addrp;
1483 char buf[256], *p;
1484 unsigned int len, i, j, n, count = 1;
1485 const char *loop = name;
1486
1487 while ((loop = strchr(loop, ',')) != NULL) {
1488 ++count;
1489 ++loop; /* skip ',' */
1490 }
1491
1492 *addrpp = xtables_malloc(sizeof(struct in6_addr) * count);
1493 *maskpp = xtables_malloc(sizeof(struct in6_addr) * count);
1494
1495 loop = name;
1496
1497 for (i = 0; i < count /*NB: count can grow*/; ++i) {
1498 if (loop == NULL)
1499 break;
1500 if (*loop == ',')
1501 ++loop;
1502 if (*loop == '\0')
1503 break;
1504 p = strchr(loop, ',');
1505 if (p != NULL)
1506 len = p - loop;
1507 else
1508 len = strlen(loop);
1509 if (len == 0 || sizeof(buf) - 1 < len)
1510 break;
1511
1512 strncpy(buf, loop, len);
1513 buf[len] = '\0';
1514 loop += len;
1515 if ((p = strrchr(buf, '/')) != NULL) {
1516 *p = '\0';
1517 addrp = parse_ip6mask(p + 1);
1518 } else {
1519 addrp = parse_ip6mask(NULL);
1520 }
1521 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1522
1523 /* if a null mask is given, the name is ignored, like in "any/0" */
Olaf Rempel58df9012009-09-20 13:24:11 +02001524 if (memcmp(*maskpp + i, &zero_addr, sizeof(zero_addr)) == 0)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001525 strcpy(buf, "::");
1526
1527 addrp = ip6parse_hostnetwork(buf, &n);
1528 /* ip6parse_hostnetwork only ever returns one IP
1529 address (it exits if the resolution fails).
1530 Therefore, n will always be 1 here. Leaving the
1531 code below in anyway in case ip6parse_hostnetwork
1532 is improved some day to behave like
1533 ipparse_hostnetwork: */
1534 if (n > 1) {
1535 count += n - 1;
1536 *addrpp = xtables_realloc(*addrpp,
1537 sizeof(struct in6_addr) * count);
1538 *maskpp = xtables_realloc(*maskpp,
1539 sizeof(struct in6_addr) * count);
1540 for (j = 0; j < n; ++j)
1541 /* for each new addr */
1542 memcpy(*addrpp + i + j, addrp + j,
1543 sizeof(*addrp));
1544 for (j = 1; j < n; ++j)
1545 /* for each new mask */
1546 memcpy(*maskpp + i + j, *maskpp + i,
1547 sizeof(*addrp));
1548 i += n - 1;
1549 } else {
1550 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1551 }
1552 /* free what ip6parse_hostnetwork had allocated: */
1553 free(addrp);
1554 }
1555 *naddrs = count;
1556 for (i = 0; i < n; ++i)
1557 for (j = 0; j < 4; ++j)
1558 (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j];
1559}
1560
Jan Engelhardta0baae82009-01-30 04:32:50 +01001561void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1562 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001563{
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001564 static const struct in6_addr zero_addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001565 struct in6_addr *addrp;
1566 unsigned int i, j, k, n;
1567 char buf[256], *p;
1568
1569 strncpy(buf, name, sizeof(buf) - 1);
1570 buf[sizeof(buf)-1] = '\0';
1571 if ((p = strrchr(buf, '/')) != NULL) {
1572 *p = '\0';
1573 addrp = parse_ip6mask(p + 1);
1574 } else {
1575 addrp = parse_ip6mask(NULL);
1576 }
1577 memcpy(maskp, addrp, sizeof(*maskp));
1578
1579 /* if a null mask is given, the name is ignored, like in "any/0" */
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001580 if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001581 strcpy(buf, "::");
1582
1583 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1584 n = *naddrs;
1585 for (i = 0, j = 0; i < n; ++i) {
1586 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001587 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001588 ++j;
1589 for (k = 0; k < j - 1; ++k)
1590 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1591 --*naddrs;
1592 --j;
1593 break;
1594 }
1595 }
1596}
Max Kellermanna5d09942008-01-29 13:44:34 +00001597
Jan Engelhardta0baae82009-01-30 04:32:50 +01001598void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001599{
1600 static const char no_quote_chars[] = "_-0123456789"
1601 "abcdefghijklmnopqrstuvwxyz"
1602 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1603 static const char escape_chars[] = "\"\\'";
1604 size_t length;
1605 const char *p;
1606
1607 length = strcspn(value, no_quote_chars);
1608 if (length > 0 && value[length] == 0) {
1609 /* no quoting required */
1610 fputs(value, stdout);
1611 putchar(' ');
1612 } else {
1613 /* there is at least one dangerous character in the
1614 value, which we have to quote. Write double quotes
1615 around the value and escape special characters with
1616 a backslash */
1617 putchar('"');
1618
1619 for (p = strpbrk(value, escape_chars); p != NULL;
1620 p = strpbrk(value, escape_chars)) {
1621 if (p > value)
1622 fwrite(value, 1, p - value, stdout);
1623 putchar('\\');
1624 putchar(*p);
1625 value = p + 1;
1626 }
1627
1628 /* print the rest and finish the double quoted
1629 string */
1630 fputs(value, stdout);
1631 printf("\" ");
1632 }
1633}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001634
1635/**
1636 * Check for option-intrapositional negation.
1637 * Do not use in new code.
1638 */
1639int xtables_check_inverse(const char option[], int *invert,
Jan Engelhardtbf971282009-11-03 19:55:11 +01001640 int *my_optind, int argc, char **argv)
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001641{
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001642 if (option == NULL || strcmp(option, "!") != 0)
1643 return false;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001644
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001645 fprintf(stderr, "Using intrapositioned negation "
1646 "(`--option ! this`) is deprecated in favor of "
1647 "extrapositioned (`! --option this`).\n");
1648
1649 if (*invert)
1650 xt_params->exit_err(PARAMETER_PROBLEM,
1651 "Multiple `!' flags not allowed");
1652 *invert = true;
1653 if (my_optind != NULL) {
Jan Engelhardtbf971282009-11-03 19:55:11 +01001654 optarg = argv[*my_optind];
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001655 ++*my_optind;
1656 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001657 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001658 "no argument following `!'");
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001659 }
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001660
1661 return true;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001662}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001663
1664const struct xtables_pprot xtables_chain_protos[] = {
1665 {"tcp", IPPROTO_TCP},
1666 {"sctp", IPPROTO_SCTP},
1667 {"udp", IPPROTO_UDP},
1668 {"udplite", IPPROTO_UDPLITE},
1669 {"icmp", IPPROTO_ICMP},
1670 {"icmpv6", IPPROTO_ICMPV6},
1671 {"ipv6-icmp", IPPROTO_ICMPV6},
1672 {"esp", IPPROTO_ESP},
1673 {"ah", IPPROTO_AH},
1674 {"ipv6-mh", IPPROTO_MH},
1675 {"mh", IPPROTO_MH},
1676 {"all", 0},
1677 {NULL},
1678};
1679
1680u_int16_t
1681xtables_parse_protocol(const char *s)
1682{
1683 unsigned int proto;
1684
1685 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1686 struct protoent *pent;
1687
1688 /* first deal with the special case of 'all' to prevent
1689 * people from being able to redefine 'all' in nsswitch
1690 * and/or provoke expensive [not working] ldap/nis/...
1691 * lookups */
1692 if (!strcmp(s, "all"))
1693 return 0;
1694
1695 if ((pent = getprotobyname(s)))
1696 proto = pent->p_proto;
1697 else {
1698 unsigned int i;
1699 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
Pablo Neira Ayusoe55cc4a2009-05-12 09:51:26 +02001700 if (xtables_chain_protos[i].name == NULL)
1701 continue;
1702
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001703 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1704 proto = xtables_chain_protos[i].num;
1705 break;
1706 }
1707 }
1708 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001709 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001710 "unknown protocol `%s' specified",
1711 s);
1712 }
1713 }
1714
1715 return proto;
1716}