blob: bc6a65e5bc6110f557cd97dbca65131ff3a72710 [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
50
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000051
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000052#define NPROTO 255
53
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000054#ifndef PROC_SYS_MODPROBE
55#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
56#endif
57
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010058void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
59
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010060struct xtables_globals *xt_params = NULL;
61
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010062void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010063{
64 va_list args;
65
66 va_start(args, msg);
67 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
68 vfprintf(stderr, msg, args);
69 va_end(args);
70 fprintf(stderr, "\n");
71 exit(status);
72}
73
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010074
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010075/**
76 * xtables_set_params - set the global parameters used by xtables
77 * @xtp: input xtables_globals structure
78 *
79 * The app is expected to pass a valid xtables_globals data-filled
80 * with proper values
81 * @xtp cannot be NULL
82 *
83 * Returns -1 on failure to set and 0 on success
84 */
85int xtables_set_params(struct xtables_globals *xtp)
86{
87 if (!xtp) {
88 fprintf(stderr, "%s: Illegal global params\n",__func__);
89 return -1;
90 }
91
92 xt_params = xtp;
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010093
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010094 if (!xt_params->exit_err)
95 xt_params->exit_err = basic_exit_err;
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010096
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010097 return 0;
98}
99
Jamal Hadi Salim84c30552009-02-11 13:00:02 +0100100void xtables_free_opts(int reset_offset, struct option *original_opts)
101{
102 if (xt_params->opts != original_opts) {
103 if (original_opts)
104 free(xt_params->opts);
105 xt_params->opts = original_opts;
106 if (reset_offset)
107 xt_params->option_offset = 0;
108 }
109}
110
Jamal Hadi Salim85332212009-02-12 09:33:59 -0500111void xtables_set_revision(char *name, u_int8_t revision)
112{
113 /* Old kernel sources don't have ".revision" field,
114 * but we stole a byte from name. */
115 name[XT_FUNCTION_MAXNAMELEN - 2] = '\0';
116 name[XT_FUNCTION_MAXNAMELEN - 1] = revision;
117}
118
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100119/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100120 * xtables_afinfo - protocol family dependent information
121 * @kmod: kernel module basename (e.g. "ip_tables")
122 * @libprefix: prefix of .so library name (e.g. "libipt_")
123 * @family: nfproto family
124 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
125 * @so_rev_match: optname to check revision support of match
126 * @so_rev_target: optname to check revision support of target
127 */
128struct xtables_afinfo {
129 const char *kmod;
130 const char *libprefix;
131 uint8_t family;
132 uint8_t ipproto;
133 int so_rev_match;
134 int so_rev_target;
135};
136
137static const struct xtables_afinfo afinfo_ipv4 = {
138 .kmod = "ip_tables",
139 .libprefix = "libipt_",
140 .family = NFPROTO_IPV4,
141 .ipproto = IPPROTO_IP,
142 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
143 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
144};
145
146static const struct xtables_afinfo afinfo_ipv6 = {
147 .kmod = "ip6_tables",
148 .libprefix = "libip6t_",
149 .family = NFPROTO_IPV6,
150 .ipproto = IPPROTO_IPV6,
151 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
152 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
153};
154
155static const struct xtables_afinfo *afinfo;
156
157/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100158 * Program will set this to its own name.
159 */
160const char *xtables_program_name;
161
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100162/* Search path for Xtables .so files */
163static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000164
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000165/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100166const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000167
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000168/* Keeping track of external matches and targets: linked lists. */
169struct xtables_match *xtables_matches;
170struct xtables_target *xtables_targets;
171
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100172void xtables_init(void)
173{
174 xtables_libdir = getenv("XTABLES_LIBDIR");
175 if (xtables_libdir != NULL)
176 return;
177 xtables_libdir = getenv("IPTABLES_LIB_DIR");
178 if (xtables_libdir != NULL) {
179 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
180 "use XTABLES_LIBDIR.\n");
181 return;
182 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100183 /*
184 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
185 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
186 * for these env vars are deprecated anyhow, and in light of the
187 * (shared) libxt_*.so files, makes less sense to have
188 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
189 */
190 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
191 if (xtables_libdir != NULL) {
192 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
193 "use XTABLES_LIBDIR.\n");
194 return;
195 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100196 xtables_libdir = XTABLES_LIBDIR;
197}
198
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100199void xtables_set_nfproto(uint8_t nfproto)
200{
201 switch (nfproto) {
202 case NFPROTO_IPV4:
203 afinfo = &afinfo_ipv4;
204 break;
205 case NFPROTO_IPV6:
206 afinfo = &afinfo_ipv6;
207 break;
208 default:
209 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
210 __func__);
211 }
212}
213
Jan Engelhardt630ef482009-01-27 14:58:41 +0100214/**
215 * xtables_*alloc - wrappers that exit on failure
216 */
217void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000218{
219 void *p;
220
221 if ((p = calloc(count, size)) == NULL) {
222 perror("ip[6]tables: calloc failed");
223 exit(1);
224 }
225
226 return p;
227}
228
Jan Engelhardt630ef482009-01-27 14:58:41 +0100229void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000230{
231 void *p;
232
233 if ((p = malloc(size)) == NULL) {
234 perror("ip[6]tables: malloc failed");
235 exit(1);
236 }
237
238 return p;
239}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000240
241static char *get_modprobe(void)
242{
243 int procfile;
244 char *ret;
245
246#define PROCFILE_BUFSIZ 1024
247 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
248 if (procfile < 0)
249 return NULL;
250
251 ret = (char *) malloc(PROCFILE_BUFSIZ);
252 if (ret) {
253 memset(ret, 0, PROCFILE_BUFSIZ);
254 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
255 case -1: goto fail;
256 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
257 }
258 if (ret[strlen(ret)-1]=='\n')
259 ret[strlen(ret)-1]=0;
260 close(procfile);
261 return ret;
262 }
263 fail:
264 free(ret);
265 close(procfile);
266 return NULL;
267}
268
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100269int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000270{
271 char *buf = NULL;
272 char *argv[4];
273 int status;
274
275 /* If they don't explicitly set it, read out of kernel */
276 if (!modprobe) {
277 buf = get_modprobe();
278 if (!buf)
279 return -1;
280 modprobe = buf;
281 }
282
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100283 /*
284 * Need to flush the buffer, or the child may output it again
285 * when switching the program thru execv.
286 */
287 fflush(stdout);
288
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000289 switch (fork()) {
290 case 0:
291 argv[0] = (char *)modprobe;
292 argv[1] = (char *)modname;
293 if (quiet) {
294 argv[2] = "-q";
295 argv[3] = NULL;
296 } else {
297 argv[2] = NULL;
298 argv[3] = NULL;
299 }
300 execv(argv[0], argv);
301
302 /* not usually reached */
303 exit(1);
304 case -1:
305 return -1;
306
307 default: /* parent */
308 wait(&status);
309 }
310
311 free(buf);
312 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
313 return 0;
314 return -1;
315}
316
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100317int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000318{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100319 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000320 static int ret = -1;
321
322 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100323 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000324 loaded = (ret == 0);
325 }
326
327 return ret;
328}
329
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100330/**
331 * xtables_strtou{i,l} - string to number conversion
332 * @s: input string
333 * @end: like strtoul's "end" pointer
334 * @value: pointer for result
335 * @min: minimum accepted value
336 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000337 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100338 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
339 * "15a" is rejected.
340 * In either case, the value obtained is compared for min-max compliance.
341 * Base is always 0, i.e. autodetect depending on @s.
342 *
343 * Returns true/false whether number was accepted. On failure, *value has
344 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000345 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100346bool xtables_strtoul(const char *s, char **end, unsigned long *value,
347 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000348{
349 unsigned long v;
350 char *my_end;
351
352 errno = 0;
353 v = strtoul(s, &my_end, 0);
354
355 if (my_end == s)
356 return false;
357 if (end != NULL)
358 *end = my_end;
359
360 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
361 if (value != NULL)
362 *value = v;
363 if (end == NULL)
364 return *my_end == '\0';
365 return true;
366 }
367
368 return false;
369}
370
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100371bool xtables_strtoui(const char *s, char **end, unsigned int *value,
372 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000373{
374 unsigned long v;
375 bool ret;
376
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100377 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000378 if (value != NULL)
379 *value = v;
380 return ret;
381}
382
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100383int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000384{
385 struct servent *service;
386
387 if ((service = getservbyname(name, proto)) != NULL)
388 return ntohs((unsigned short) service->s_port);
389
390 return -1;
391}
392
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100393u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000394{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100395 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000396
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100397 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100398 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100399 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000400
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100401 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000402 "invalid port/service `%s' specified", port);
403}
404
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100405void xtables_parse_interface(const char *arg, char *vianame,
406 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000407{
408 int vialen = strlen(arg);
409 unsigned int i;
410
411 memset(mask, 0, IFNAMSIZ);
412 memset(vianame, 0, IFNAMSIZ);
413
414 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100415 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000416 "interface name `%s' must be shorter than IFNAMSIZ"
417 " (%i)", arg, IFNAMSIZ-1);
418
419 strcpy(vianame, arg);
420 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
421 memset(mask, 0, IFNAMSIZ);
422 else if (vianame[vialen - 1] == '+') {
423 memset(mask, 0xFF, vialen - 1);
424 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
425 /* Don't remove `+' here! -HW */
426 } else {
427 /* Include nul-terminator in match */
428 memset(mask, 0xFF, vialen + 1);
429 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
430 for (i = 0; vianame[i]; i++) {
431 if (vianame[i] == ':' ||
432 vianame[i] == '!' ||
433 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000434 fprintf(stderr,
435 "Warning: weird character in interface"
436 " `%s' (No aliases, :, ! or *).\n",
437 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000438 break;
439 }
440 }
441 }
442}
443
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200444#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100445static void *load_extension(const char *search_path, const char *prefix,
446 const char *name, bool is_target)
447{
448 const char *dir = search_path, *next;
449 void *ptr = NULL;
450 struct stat sb;
451 char path[256];
452
453 do {
454 next = strchr(dir, ':');
455 if (next == NULL)
456 next = dir + strlen(dir);
457 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200458 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100459
460 if (dlopen(path, RTLD_NOW) != NULL) {
461 /* Found library. If it didn't register itself,
462 maybe they specified target as match. */
463 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100464 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100465 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100466 ptr = xtables_find_match(name,
467 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100468 } else if (stat(path, &sb) == 0) {
469 fprintf(stderr, "%s: %s\n", path, dlerror());
470 }
471
472 if (ptr != NULL)
473 return ptr;
474
475 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200476 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100477 if (dlopen(path, RTLD_NOW) != NULL) {
478 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100479 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100480 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100481 ptr = xtables_find_match(name,
482 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100483 } else if (stat(path, &sb) == 0) {
484 fprintf(stderr, "%s: %s\n", path, dlerror());
485 }
486
487 if (ptr != NULL)
488 return ptr;
489
490 dir = next + 1;
491 } while (*next != '\0');
492
493 return NULL;
494}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200495#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100496
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100497struct xtables_match *
498xtables_find_match(const char *name, enum xtables_tryload tryload,
499 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000500{
501 struct xtables_match *ptr;
502 const char *icmp6 = "icmp6";
503
504 /* This is ugly as hell. Nonetheless, there is no way of changing
505 * this without hurting backwards compatibility */
506 if ( (strcmp(name,"icmpv6") == 0) ||
507 (strcmp(name,"ipv6-icmp") == 0) ||
508 (strcmp(name,"icmp6") == 0) )
509 name = icmp6;
510
511 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
512 if (strcmp(name, ptr->name) == 0) {
513 struct xtables_match *clone;
514
515 /* First match of this type: */
516 if (ptr->m == NULL)
517 break;
518
519 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100520 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000521 memcpy(clone, ptr, sizeof(struct xtables_match));
522 clone->mflags = 0;
523 /* This is a clone: */
524 clone->next = clone;
525
526 ptr = clone;
527 break;
528 }
529 }
530
531#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100532 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100533 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100534 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000535
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100536 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100537 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000538 "Couldn't load match `%s':%s\n",
539 name, dlerror());
540 }
541#else
542 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100543 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000544 ptr->loaded = 1;
545 else
546 ptr = NULL;
547 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100548 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100549 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000550 "Couldn't find match `%s'\n", name);
551 }
552#endif
553
554 if (ptr && matches) {
555 struct xtables_rule_match **i;
556 struct xtables_rule_match *newentry;
557
Jan Engelhardt630ef482009-01-27 14:58:41 +0100558 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000559
560 for (i = matches; *i; i = &(*i)->next) {
561 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100562 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000563 }
564 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100565 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000566 newentry->next = NULL;
567 *i = newentry;
568 }
569
570 return ptr;
571}
572
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100573struct xtables_target *
574xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000575{
576 struct xtables_target *ptr;
577
578 /* Standard target? */
579 if (strcmp(name, "") == 0
580 || strcmp(name, XTC_LABEL_ACCEPT) == 0
581 || strcmp(name, XTC_LABEL_DROP) == 0
582 || strcmp(name, XTC_LABEL_QUEUE) == 0
583 || strcmp(name, XTC_LABEL_RETURN) == 0)
584 name = "standard";
585
586 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
587 if (strcmp(name, ptr->name) == 0)
588 break;
589 }
590
591#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100592 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100593 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100594 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000595
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100596 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100597 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000598 "Couldn't load target `%s':%s\n",
599 name, dlerror());
600 }
601#else
602 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100603 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000604 ptr->loaded = 1;
605 else
606 ptr = NULL;
607 }
608 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100609 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000610 "Couldn't find target `%s'\n", name);
611 }
612#endif
613
614 if (ptr)
615 ptr->used = 1;
616
617 return ptr;
618}
619
620static int compatible_revision(const char *name, u_int8_t revision, int opt)
621{
622 struct xt_get_revision rev;
623 socklen_t s = sizeof(rev);
624 int max_rev, sockfd;
625
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100626 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000627 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000628 if (errno == EPERM) {
629 /* revision 0 is always supported. */
630 if (revision != 0)
631 fprintf(stderr, "Could not determine whether "
632 "revision %u is supported, "
633 "assuming it is.\n",
634 revision);
635 return 1;
636 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000637 fprintf(stderr, "Could not open socket to kernel: %s\n",
638 strerror(errno));
639 exit(1);
640 }
641
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100642 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000643
644 strcpy(rev.name, name);
645 rev.revision = revision;
646
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100647 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000648 if (max_rev < 0) {
649 /* Definitely don't support this? */
650 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
651 close(sockfd);
652 return 0;
653 } else if (errno == ENOPROTOOPT) {
654 close(sockfd);
655 /* Assume only revision 0 support (old kernel) */
656 return (revision == 0);
657 } else {
658 fprintf(stderr, "getsockopt failed strangely: %s\n",
659 strerror(errno));
660 exit(1);
661 }
662 }
663 close(sockfd);
664 return 1;
665}
666
667
668static int compatible_match_revision(const char *name, u_int8_t revision)
669{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100670 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000671}
672
673static int compatible_target_revision(const char *name, u_int8_t revision)
674{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100675 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000676}
677
678void xtables_register_match(struct xtables_match *me)
679{
680 struct xtables_match **i, *old;
681
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100682 if (strcmp(me->version, XTABLES_VERSION) != 0) {
683 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
684 "but \"%s\" is required.\n",
685 xtables_program_name, me->name,
686 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000687 exit(1);
688 }
689
690 /* Revision field stole a char from name. */
691 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
692 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100693 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000694 exit(1);
695 }
696
697 if (me->family >= NPROTO) {
698 fprintf(stderr,
699 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100700 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000701 exit(1);
702 }
703
704 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100705 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000706 return;
707
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100708 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000709 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100710 if (old->revision == me->revision &&
711 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000712 fprintf(stderr,
713 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100714 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000715 exit(1);
716 }
717
718 /* Now we have two (or more) options, check compatibility. */
719 if (compatible_match_revision(old->name, old->revision)
720 && old->revision > me->revision)
721 return;
722
Jan Engelhardt23545c22008-02-14 04:23:04 +0100723 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000724 if (!compatible_match_revision(me->name, me->revision))
725 return;
726
Jan Engelhardt23545c22008-02-14 04:23:04 +0100727 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
728 if (old->revision == me->revision && me->family == AF_UNSPEC)
729 return;
730
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000731 /* Delete old one. */
732 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
733 *i = old->next;
734 }
735
736 if (me->size != XT_ALIGN(me->size)) {
737 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100738 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000739 exit(1);
740 }
741
742 /* Append to list. */
743 for (i = &xtables_matches; *i; i = &(*i)->next);
744 me->next = NULL;
745 *i = me;
746
747 me->m = NULL;
748 me->mflags = 0;
749}
750
751void xtables_register_target(struct xtables_target *me)
752{
753 struct xtables_target *old;
754
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100755 if (strcmp(me->version, XTABLES_VERSION) != 0) {
756 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
757 "but \"%s\" is required.\n",
758 xtables_program_name, me->name,
759 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000760 exit(1);
761 }
762
763 /* Revision field stole a char from name. */
764 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
765 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100766 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000767 exit(1);
768 }
769
770 if (me->family >= NPROTO) {
771 fprintf(stderr,
772 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100773 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000774 exit(1);
775 }
776
777 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100778 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000779 return;
780
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100781 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000782 if (old) {
783 struct xtables_target **i;
784
Jan Engelhardt23545c22008-02-14 04:23:04 +0100785 if (old->revision == me->revision &&
786 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000787 fprintf(stderr,
788 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100789 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000790 exit(1);
791 }
792
793 /* Now we have two (or more) options, check compatibility. */
794 if (compatible_target_revision(old->name, old->revision)
795 && old->revision > me->revision)
796 return;
797
Jan Engelhardt23545c22008-02-14 04:23:04 +0100798 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000799 if (!compatible_target_revision(me->name, me->revision))
800 return;
801
Jan Engelhardt23545c22008-02-14 04:23:04 +0100802 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
803 if (old->revision == me->revision && me->family == AF_UNSPEC)
804 return;
805
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000806 /* Delete old one. */
807 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
808 *i = old->next;
809 }
810
811 if (me->size != XT_ALIGN(me->size)) {
812 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100813 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000814 exit(1);
815 }
816
817 /* Prepend to list. */
818 me->next = xtables_targets;
819 xtables_targets = me;
820 me->t = NULL;
821 me->tflags = 0;
822}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000823
Jan Engelhardta41545c2009-01-27 21:27:19 +0100824/**
825 * xtables_param_act - act on condition
826 * @status: a constant from enum xtables_exittype
827 *
828 * %XTF_ONLY_ONCE: print error message that option may only be used once.
829 * @p1: module name (e.g. "mark")
830 * @p2(...): option in conflict (e.g. "--mark")
831 * @p3(...): condition to match on (see extensions/ for examples)
832 *
833 * %XTF_NO_INVERT: option does not support inversion
834 * @p1: module name
835 * @p2: option in conflict
836 * @p3: condition to match on
837 *
838 * %XTF_BAD_VALUE: bad value for option
839 * @p1: module name
840 * @p2: option with which the problem occured (e.g. "--mark")
841 * @p3: string the user passed in (e.g. "99999999999999")
842 *
843 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
844 * @p1: module name
845 *
846 * Displays an error message and exits the program.
847 */
848void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000849{
850 const char *p2, *p3;
851 va_list args;
852 bool b;
853
854 va_start(args, p1);
855
856 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100857 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000858 p2 = va_arg(args, const char *);
859 b = va_arg(args, unsigned int);
860 if (!b)
861 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100862 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000863 "%s: \"%s\" option may only be specified once",
864 p1, p2);
865 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100866 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000867 p2 = va_arg(args, const char *);
868 b = va_arg(args, unsigned int);
869 if (!b)
870 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100871 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000872 "%s: \"%s\" option cannot be inverted", p1, p2);
873 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100874 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000875 p2 = va_arg(args, const char *);
876 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100877 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000878 "%s: Bad value for \"%s\" option: \"%s\"",
879 p1, p2, p3);
880 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100881 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000882 b = va_arg(args, unsigned int);
883 if (!b)
884 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100885 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000886 "%s: At most one action is possible", p1);
887 break;
888 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100889 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000890 break;
891 }
892
893 va_end(args);
894}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000895
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100896const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000897{
898 static char buf[20];
899 const unsigned char *bytep = (const void *)&addrp->s_addr;
900
901 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
902 return buf;
903}
904
905static const char *ipaddr_to_host(const struct in_addr *addr)
906{
907 struct hostent *host;
908
909 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
910 if (host == NULL)
911 return NULL;
912
913 return host->h_name;
914}
915
916static const char *ipaddr_to_network(const struct in_addr *addr)
917{
918 struct netent *net;
919
920 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
921 return net->n_name;
922
923 return NULL;
924}
925
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100926const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000927{
928 const char *name;
929
930 if ((name = ipaddr_to_host(addr)) != NULL ||
931 (name = ipaddr_to_network(addr)) != NULL)
932 return name;
933
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100934 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000935}
936
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100937const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000938{
939 static char buf[20];
940 uint32_t maskaddr, bits;
941 int i;
942
943 maskaddr = ntohl(mask->s_addr);
944
945 if (maskaddr == 0xFFFFFFFFL)
946 /* we don't want to see "/32" */
947 return "";
948
949 i = 32;
950 bits = 0xFFFFFFFEL;
951 while (--i >= 0 && maskaddr != bits)
952 bits <<= 1;
953 if (i >= 0)
954 sprintf(buf, "/%d", i);
955 else
956 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100957 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000958
959 return buf;
960}
961
Jan Engelhardtbd943842008-01-20 13:38:08 +0000962static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
963{
964 static struct in_addr addr;
965 unsigned char *addrp;
966 unsigned int onebyte;
967 char buf[20], *p, *q;
968 int i;
969
970 /* copy dotted string, because we need to modify it */
971 strncpy(buf, dotted, sizeof(buf) - 1);
972 buf[sizeof(buf) - 1] = '\0';
973 addrp = (void *)&addr.s_addr;
974
975 p = buf;
976 for (i = 0; i < 3; ++i) {
977 if ((q = strchr(p, '.')) == NULL) {
978 if (is_mask)
979 return NULL;
980
981 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100982 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000983 return NULL;
984
985 addrp[i] = onebyte;
986 while (i < 3)
987 addrp[++i] = 0;
988
989 return &addr;
990 }
991
992 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100993 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000994 return NULL;
995
996 addrp[i] = onebyte;
997 p = q + 1;
998 }
999
1000 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001001 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001002 return NULL;
1003
1004 addrp[3] = onebyte;
1005 return &addr;
1006}
1007
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001008struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001009{
1010 return __numeric_to_ipaddr(dotted, false);
1011}
1012
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001013struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001014{
1015 return __numeric_to_ipaddr(dotted, true);
1016}
1017
1018static struct in_addr *network_to_ipaddr(const char *name)
1019{
1020 static struct in_addr addr;
1021 struct netent *net;
1022
1023 if ((net = getnetbyname(name)) != NULL) {
1024 if (net->n_addrtype != AF_INET)
1025 return NULL;
1026 addr.s_addr = htonl(net->n_net);
1027 return &addr;
1028 }
1029
1030 return NULL;
1031}
1032
1033static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1034{
1035 struct hostent *host;
1036 struct in_addr *addr;
1037 unsigned int i;
1038
1039 *naddr = 0;
1040 if ((host = gethostbyname(name)) != NULL) {
1041 if (host->h_addrtype != AF_INET ||
1042 host->h_length != sizeof(struct in_addr))
1043 return NULL;
1044
1045 while (host->h_addr_list[*naddr] != NULL)
1046 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001047 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001048 for (i = 0; i < *naddr; i++)
1049 memcpy(&addr[i], host->h_addr_list[i],
1050 sizeof(struct in_addr));
1051 return addr;
1052 }
1053
1054 return NULL;
1055}
1056
1057static struct in_addr *
1058ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1059{
1060 struct in_addr *addrptmp, *addrp;
1061
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001062 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001063 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001064 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001065 memcpy(addrp, addrptmp, sizeof(*addrp));
1066 *naddrs = 1;
1067 return addrp;
1068 }
1069 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1070 return addrptmp;
1071
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001072 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001073}
1074
1075static struct in_addr *parse_ipmask(const char *mask)
1076{
1077 static struct in_addr maskaddr;
1078 struct in_addr *addrp;
1079 unsigned int bits;
1080
1081 if (mask == NULL) {
1082 /* no mask at all defaults to 32 bits */
1083 maskaddr.s_addr = 0xFFFFFFFF;
1084 return &maskaddr;
1085 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001086 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001087 /* dotted_to_addr already returns a network byte order addr */
1088 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001089 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001090 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001091 "invalid mask `%s' specified", mask);
1092 if (bits != 0) {
1093 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1094 return &maskaddr;
1095 }
1096
1097 maskaddr.s_addr = 0U;
1098 return &maskaddr;
1099}
1100
Jan Engelhardta0baae82009-01-30 04:32:50 +01001101/**
1102 * xtables_ipparse_any - transform arbitrary name to in_addr
1103 *
1104 * Possible inputs (pseudo regex):
1105 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1106 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1107 */
1108void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1109 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001110{
1111 unsigned int i, j, k, n;
1112 struct in_addr *addrp;
1113 char buf[256], *p;
1114
1115 strncpy(buf, name, sizeof(buf) - 1);
1116 buf[sizeof(buf) - 1] = '\0';
1117 if ((p = strrchr(buf, '/')) != NULL) {
1118 *p = '\0';
1119 addrp = parse_ipmask(p + 1);
1120 } else {
1121 addrp = parse_ipmask(NULL);
1122 }
1123 memcpy(maskp, addrp, sizeof(*maskp));
1124
1125 /* if a null mask is given, the name is ignored, like in "any/0" */
1126 if (maskp->s_addr == 0U)
1127 strcpy(buf, "0.0.0.0");
1128
1129 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1130 n = *naddrs;
1131 for (i = 0, j = 0; i < n; ++i) {
1132 addrp[j++].s_addr &= maskp->s_addr;
1133 for (k = 0; k < j - 1; ++k)
1134 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1135 --*naddrs;
1136 --j;
1137 break;
1138 }
1139 }
1140}
1141
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001142const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001143{
1144 /* 0000:0000:0000:0000:0000:000.000.000.000
1145 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1146 static char buf[50+1];
1147 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1148}
1149
1150static const char *ip6addr_to_host(const struct in6_addr *addr)
1151{
1152 static char hostname[NI_MAXHOST];
1153 struct sockaddr_in6 saddr;
1154 int err;
1155
1156 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1157 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1158 saddr.sin6_family = AF_INET6;
1159
1160 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1161 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1162 if (err != 0) {
1163#ifdef DEBUG
1164 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1165#endif
1166 return NULL;
1167 }
1168
1169#ifdef DEBUG
1170 fprintf (stderr, "\naddr2host: %s\n", hostname);
1171#endif
1172 return hostname;
1173}
1174
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001175const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001176{
1177 const char *name;
1178
1179 if ((name = ip6addr_to_host(addr)) != NULL)
1180 return name;
1181
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001182 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001183}
1184
1185static int ip6addr_prefix_length(const struct in6_addr *k)
1186{
1187 unsigned int bits = 0;
1188 uint32_t a, b, c, d;
1189
Jan Engelhardt48607812008-06-10 15:17:53 +02001190 a = ntohl(k->s6_addr32[0]);
1191 b = ntohl(k->s6_addr32[1]);
1192 c = ntohl(k->s6_addr32[2]);
1193 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001194 while (a & 0x80000000U) {
1195 ++bits;
1196 a <<= 1;
1197 a |= (b >> 31) & 1;
1198 b <<= 1;
1199 b |= (c >> 31) & 1;
1200 c <<= 1;
1201 c |= (d >> 31) & 1;
1202 d <<= 1;
1203 }
1204 if (a != 0 || b != 0 || c != 0 || d != 0)
1205 return -1;
1206 return bits;
1207}
1208
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001209const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001210{
1211 static char buf[50+2];
1212 int l = ip6addr_prefix_length(addrp);
1213
1214 if (l == -1) {
1215 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001216 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001217 return buf;
1218 }
1219 sprintf(buf, "/%d", l);
1220 return buf;
1221}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001222
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001223struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001224{
1225 static struct in6_addr ap;
1226 int err;
1227
1228 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1229 return &ap;
1230#ifdef DEBUG
1231 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1232#endif
1233 return NULL;
1234}
1235
1236static struct in6_addr *
1237host_to_ip6addr(const char *name, unsigned int *naddr)
1238{
1239 static struct in6_addr *addr;
1240 struct addrinfo hints;
1241 struct addrinfo *res;
1242 int err;
1243
1244 memset(&hints, 0, sizeof(hints));
1245 hints.ai_flags = AI_CANONNAME;
1246 hints.ai_family = AF_INET6;
1247 hints.ai_socktype = SOCK_RAW;
1248 hints.ai_protocol = IPPROTO_IPV6;
1249 hints.ai_next = NULL;
1250
1251 *naddr = 0;
1252 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1253#ifdef DEBUG
1254 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1255#endif
1256 return NULL;
1257 } else {
1258 if (res->ai_family != AF_INET6 ||
1259 res->ai_addrlen != sizeof(struct sockaddr_in6))
1260 return NULL;
1261
1262#ifdef DEBUG
1263 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1264 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1265#endif
1266 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001267 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001268 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1269 sizeof(struct in6_addr));
1270 freeaddrinfo(res);
1271 *naddr = 1;
1272 return addr;
1273 }
1274
1275 return NULL;
1276}
1277
1278static struct in6_addr *network_to_ip6addr(const char *name)
1279{
1280 /* abort();*/
1281 /* TODO: not implemented yet, but the exception breaks the
1282 * name resolvation */
1283 return NULL;
1284}
1285
1286static struct in6_addr *
1287ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1288{
1289 struct in6_addr *addrp, *addrptmp;
1290
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001291 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001292 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001293 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001294 memcpy(addrp, addrptmp, sizeof(*addrp));
1295 *naddrs = 1;
1296 return addrp;
1297 }
1298 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1299 return addrp;
1300
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001301 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001302}
1303
1304static struct in6_addr *parse_ip6mask(char *mask)
1305{
1306 static struct in6_addr maskaddr;
1307 struct in6_addr *addrp;
1308 unsigned int bits;
1309
1310 if (mask == NULL) {
1311 /* no mask at all defaults to 128 bits */
1312 memset(&maskaddr, 0xff, sizeof maskaddr);
1313 return &maskaddr;
1314 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001315 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001316 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001317 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001318 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001319 "invalid mask `%s' specified", mask);
1320 if (bits != 0) {
1321 char *p = (void *)&maskaddr;
1322 memset(p, 0xff, bits / 8);
1323 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1324 p[bits/8] = 0xff << (8 - (bits & 7));
1325 return &maskaddr;
1326 }
1327
1328 memset(&maskaddr, 0, sizeof(maskaddr));
1329 return &maskaddr;
1330}
1331
Jan Engelhardta0baae82009-01-30 04:32:50 +01001332void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1333 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001334{
1335 struct in6_addr *addrp;
1336 unsigned int i, j, k, n;
1337 char buf[256], *p;
1338
1339 strncpy(buf, name, sizeof(buf) - 1);
1340 buf[sizeof(buf)-1] = '\0';
1341 if ((p = strrchr(buf, '/')) != NULL) {
1342 *p = '\0';
1343 addrp = parse_ip6mask(p + 1);
1344 } else {
1345 addrp = parse_ip6mask(NULL);
1346 }
1347 memcpy(maskp, addrp, sizeof(*maskp));
1348
1349 /* if a null mask is given, the name is ignored, like in "any/0" */
1350 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1351 strcpy(buf, "::");
1352
1353 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1354 n = *naddrs;
1355 for (i = 0, j = 0; i < n; ++i) {
1356 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001357 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001358 ++j;
1359 for (k = 0; k < j - 1; ++k)
1360 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1361 --*naddrs;
1362 --j;
1363 break;
1364 }
1365 }
1366}
Max Kellermanna5d09942008-01-29 13:44:34 +00001367
Jan Engelhardta0baae82009-01-30 04:32:50 +01001368void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001369{
1370 static const char no_quote_chars[] = "_-0123456789"
1371 "abcdefghijklmnopqrstuvwxyz"
1372 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1373 static const char escape_chars[] = "\"\\'";
1374 size_t length;
1375 const char *p;
1376
1377 length = strcspn(value, no_quote_chars);
1378 if (length > 0 && value[length] == 0) {
1379 /* no quoting required */
1380 fputs(value, stdout);
1381 putchar(' ');
1382 } else {
1383 /* there is at least one dangerous character in the
1384 value, which we have to quote. Write double quotes
1385 around the value and escape special characters with
1386 a backslash */
1387 putchar('"');
1388
1389 for (p = strpbrk(value, escape_chars); p != NULL;
1390 p = strpbrk(value, escape_chars)) {
1391 if (p > value)
1392 fwrite(value, 1, p - value, stdout);
1393 putchar('\\');
1394 putchar(*p);
1395 value = p + 1;
1396 }
1397
1398 /* print the rest and finish the double quoted
1399 string */
1400 fputs(value, stdout);
1401 printf("\" ");
1402 }
1403}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001404
1405/**
1406 * Check for option-intrapositional negation.
1407 * Do not use in new code.
1408 */
1409int xtables_check_inverse(const char option[], int *invert,
1410 int *my_optind, int argc)
1411{
1412 if (option && strcmp(option, "!") == 0) {
1413 fprintf(stderr, "Using intrapositioned negation "
1414 "(`--option ! this`) is deprecated in favor of "
1415 "extrapositioned (`! --option this`).\n");
1416
1417 if (*invert)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001418 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001419 "Multiple `!' flags not allowed");
1420 *invert = true;
1421 if (my_optind != NULL) {
1422 ++*my_optind;
1423 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001424 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001425 "no argument following `!'");
1426 }
1427
1428 return true;
1429 }
1430 return false;
1431}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001432
1433const struct xtables_pprot xtables_chain_protos[] = {
1434 {"tcp", IPPROTO_TCP},
1435 {"sctp", IPPROTO_SCTP},
1436 {"udp", IPPROTO_UDP},
1437 {"udplite", IPPROTO_UDPLITE},
1438 {"icmp", IPPROTO_ICMP},
1439 {"icmpv6", IPPROTO_ICMPV6},
1440 {"ipv6-icmp", IPPROTO_ICMPV6},
1441 {"esp", IPPROTO_ESP},
1442 {"ah", IPPROTO_AH},
1443 {"ipv6-mh", IPPROTO_MH},
1444 {"mh", IPPROTO_MH},
1445 {"all", 0},
1446 {NULL},
1447};
1448
1449u_int16_t
1450xtables_parse_protocol(const char *s)
1451{
1452 unsigned int proto;
1453
1454 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1455 struct protoent *pent;
1456
1457 /* first deal with the special case of 'all' to prevent
1458 * people from being able to redefine 'all' in nsswitch
1459 * and/or provoke expensive [not working] ldap/nis/...
1460 * lookups */
1461 if (!strcmp(s, "all"))
1462 return 0;
1463
1464 if ((pent = getprotobyname(s)))
1465 proto = pent->p_proto;
1466 else {
1467 unsigned int i;
1468 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1469 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1470 proto = xtables_chain_protos[i].num;
1471 break;
1472 }
1473 }
1474 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001475 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001476 "unknown protocol `%s' specified",
1477 s);
1478 }
1479 }
1480
1481 return proto;
1482}