blob: 8e28d5ebd1c18ff3c18cb034de4a3acf6c7af637 [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
42
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000043#define NPROTO 255
44
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000045#ifndef PROC_SYS_MODPROBE
46#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
47#endif
48
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010049void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
50
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010051struct xtables_globals *xt_params = NULL;
52
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010053void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010054{
55 va_list args;
56
57 va_start(args, msg);
58 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
59 vfprintf(stderr, msg, args);
60 va_end(args);
61 fprintf(stderr, "\n");
62 exit(status);
63}
64
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010065
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010066/**
67 * xtables_set_params - set the global parameters used by xtables
68 * @xtp: input xtables_globals structure
69 *
70 * The app is expected to pass a valid xtables_globals data-filled
71 * with proper values
72 * @xtp cannot be NULL
73 *
74 * Returns -1 on failure to set and 0 on success
75 */
76int xtables_set_params(struct xtables_globals *xtp)
77{
78 if (!xtp) {
79 fprintf(stderr, "%s: Illegal global params\n",__func__);
80 return -1;
81 }
82
83 xt_params = xtp;
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010084
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010085 if (!xt_params->exit_err)
86 xt_params->exit_err = basic_exit_err;
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010087
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010088 return 0;
89}
90
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010091void xtables_free_opts(int reset_offset, struct option *original_opts)
92{
93 if (xt_params->opts != original_opts) {
94 if (original_opts)
95 free(xt_params->opts);
96 xt_params->opts = original_opts;
97 if (reset_offset)
98 xt_params->option_offset = 0;
99 }
100}
101
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100102/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100103 * xtables_afinfo - protocol family dependent information
104 * @kmod: kernel module basename (e.g. "ip_tables")
105 * @libprefix: prefix of .so library name (e.g. "libipt_")
106 * @family: nfproto family
107 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
108 * @so_rev_match: optname to check revision support of match
109 * @so_rev_target: optname to check revision support of target
110 */
111struct xtables_afinfo {
112 const char *kmod;
113 const char *libprefix;
114 uint8_t family;
115 uint8_t ipproto;
116 int so_rev_match;
117 int so_rev_target;
118};
119
120static const struct xtables_afinfo afinfo_ipv4 = {
121 .kmod = "ip_tables",
122 .libprefix = "libipt_",
123 .family = NFPROTO_IPV4,
124 .ipproto = IPPROTO_IP,
125 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
126 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
127};
128
129static const struct xtables_afinfo afinfo_ipv6 = {
130 .kmod = "ip6_tables",
131 .libprefix = "libip6t_",
132 .family = NFPROTO_IPV6,
133 .ipproto = IPPROTO_IPV6,
134 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
135 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
136};
137
138static const struct xtables_afinfo *afinfo;
139
140/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100141 * Program will set this to its own name.
142 */
143const char *xtables_program_name;
144
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100145/* Search path for Xtables .so files */
146static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000147
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000148/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100149const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000150
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000151/* Keeping track of external matches and targets: linked lists. */
152struct xtables_match *xtables_matches;
153struct xtables_target *xtables_targets;
154
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100155void xtables_init(void)
156{
157 xtables_libdir = getenv("XTABLES_LIBDIR");
158 if (xtables_libdir != NULL)
159 return;
160 xtables_libdir = getenv("IPTABLES_LIB_DIR");
161 if (xtables_libdir != NULL) {
162 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
163 "use XTABLES_LIBDIR.\n");
164 return;
165 }
166 xtables_libdir = XTABLES_LIBDIR;
167}
168
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100169void xtables_set_nfproto(uint8_t nfproto)
170{
171 switch (nfproto) {
172 case NFPROTO_IPV4:
173 afinfo = &afinfo_ipv4;
174 break;
175 case NFPROTO_IPV6:
176 afinfo = &afinfo_ipv6;
177 break;
178 default:
179 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
180 __func__);
181 }
182}
183
Jan Engelhardt630ef482009-01-27 14:58:41 +0100184/**
185 * xtables_*alloc - wrappers that exit on failure
186 */
187void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000188{
189 void *p;
190
191 if ((p = calloc(count, size)) == NULL) {
192 perror("ip[6]tables: calloc failed");
193 exit(1);
194 }
195
196 return p;
197}
198
Jan Engelhardt630ef482009-01-27 14:58:41 +0100199void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000200{
201 void *p;
202
203 if ((p = malloc(size)) == NULL) {
204 perror("ip[6]tables: malloc failed");
205 exit(1);
206 }
207
208 return p;
209}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000210
211static char *get_modprobe(void)
212{
213 int procfile;
214 char *ret;
215
216#define PROCFILE_BUFSIZ 1024
217 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
218 if (procfile < 0)
219 return NULL;
220
221 ret = (char *) malloc(PROCFILE_BUFSIZ);
222 if (ret) {
223 memset(ret, 0, PROCFILE_BUFSIZ);
224 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
225 case -1: goto fail;
226 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
227 }
228 if (ret[strlen(ret)-1]=='\n')
229 ret[strlen(ret)-1]=0;
230 close(procfile);
231 return ret;
232 }
233 fail:
234 free(ret);
235 close(procfile);
236 return NULL;
237}
238
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100239int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000240{
241 char *buf = NULL;
242 char *argv[4];
243 int status;
244
245 /* If they don't explicitly set it, read out of kernel */
246 if (!modprobe) {
247 buf = get_modprobe();
248 if (!buf)
249 return -1;
250 modprobe = buf;
251 }
252
253 switch (fork()) {
254 case 0:
255 argv[0] = (char *)modprobe;
256 argv[1] = (char *)modname;
257 if (quiet) {
258 argv[2] = "-q";
259 argv[3] = NULL;
260 } else {
261 argv[2] = NULL;
262 argv[3] = NULL;
263 }
264 execv(argv[0], argv);
265
266 /* not usually reached */
267 exit(1);
268 case -1:
269 return -1;
270
271 default: /* parent */
272 wait(&status);
273 }
274
275 free(buf);
276 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
277 return 0;
278 return -1;
279}
280
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100281int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000282{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100283 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000284 static int ret = -1;
285
286 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100287 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000288 loaded = (ret == 0);
289 }
290
291 return ret;
292}
293
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100294/**
295 * xtables_strtou{i,l} - string to number conversion
296 * @s: input string
297 * @end: like strtoul's "end" pointer
298 * @value: pointer for result
299 * @min: minimum accepted value
300 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000301 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100302 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
303 * "15a" is rejected.
304 * In either case, the value obtained is compared for min-max compliance.
305 * Base is always 0, i.e. autodetect depending on @s.
306 *
307 * Returns true/false whether number was accepted. On failure, *value has
308 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000309 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100310bool xtables_strtoul(const char *s, char **end, unsigned long *value,
311 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000312{
313 unsigned long v;
314 char *my_end;
315
316 errno = 0;
317 v = strtoul(s, &my_end, 0);
318
319 if (my_end == s)
320 return false;
321 if (end != NULL)
322 *end = my_end;
323
324 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
325 if (value != NULL)
326 *value = v;
327 if (end == NULL)
328 return *my_end == '\0';
329 return true;
330 }
331
332 return false;
333}
334
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100335bool xtables_strtoui(const char *s, char **end, unsigned int *value,
336 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000337{
338 unsigned long v;
339 bool ret;
340
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100341 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000342 if (value != NULL)
343 *value = v;
344 return ret;
345}
346
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100347int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000348{
349 struct servent *service;
350
351 if ((service = getservbyname(name, proto)) != NULL)
352 return ntohs((unsigned short) service->s_port);
353
354 return -1;
355}
356
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100357u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000358{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100359 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000360
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100361 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100362 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100363 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000364
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100365 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000366 "invalid port/service `%s' specified", port);
367}
368
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100369void xtables_parse_interface(const char *arg, char *vianame,
370 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000371{
372 int vialen = strlen(arg);
373 unsigned int i;
374
375 memset(mask, 0, IFNAMSIZ);
376 memset(vianame, 0, IFNAMSIZ);
377
378 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100379 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000380 "interface name `%s' must be shorter than IFNAMSIZ"
381 " (%i)", arg, IFNAMSIZ-1);
382
383 strcpy(vianame, arg);
384 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
385 memset(mask, 0, IFNAMSIZ);
386 else if (vianame[vialen - 1] == '+') {
387 memset(mask, 0xFF, vialen - 1);
388 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
389 /* Don't remove `+' here! -HW */
390 } else {
391 /* Include nul-terminator in match */
392 memset(mask, 0xFF, vialen + 1);
393 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
394 for (i = 0; vianame[i]; i++) {
395 if (vianame[i] == ':' ||
396 vianame[i] == '!' ||
397 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000398 fprintf(stderr,
399 "Warning: weird character in interface"
400 " `%s' (No aliases, :, ! or *).\n",
401 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000402 break;
403 }
404 }
405 }
406}
407
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200408#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100409static void *load_extension(const char *search_path, const char *prefix,
410 const char *name, bool is_target)
411{
412 const char *dir = search_path, *next;
413 void *ptr = NULL;
414 struct stat sb;
415 char path[256];
416
417 do {
418 next = strchr(dir, ':');
419 if (next == NULL)
420 next = dir + strlen(dir);
421 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200422 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100423
424 if (dlopen(path, RTLD_NOW) != NULL) {
425 /* Found library. If it didn't register itself,
426 maybe they specified target as match. */
427 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100428 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100429 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100430 ptr = xtables_find_match(name,
431 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100432 } else if (stat(path, &sb) == 0) {
433 fprintf(stderr, "%s: %s\n", path, dlerror());
434 }
435
436 if (ptr != NULL)
437 return ptr;
438
439 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200440 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100441 if (dlopen(path, RTLD_NOW) != NULL) {
442 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100443 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100444 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100445 ptr = xtables_find_match(name,
446 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100447 } else if (stat(path, &sb) == 0) {
448 fprintf(stderr, "%s: %s\n", path, dlerror());
449 }
450
451 if (ptr != NULL)
452 return ptr;
453
454 dir = next + 1;
455 } while (*next != '\0');
456
457 return NULL;
458}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200459#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100460
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100461struct xtables_match *
462xtables_find_match(const char *name, enum xtables_tryload tryload,
463 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000464{
465 struct xtables_match *ptr;
466 const char *icmp6 = "icmp6";
467
468 /* This is ugly as hell. Nonetheless, there is no way of changing
469 * this without hurting backwards compatibility */
470 if ( (strcmp(name,"icmpv6") == 0) ||
471 (strcmp(name,"ipv6-icmp") == 0) ||
472 (strcmp(name,"icmp6") == 0) )
473 name = icmp6;
474
475 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
476 if (strcmp(name, ptr->name) == 0) {
477 struct xtables_match *clone;
478
479 /* First match of this type: */
480 if (ptr->m == NULL)
481 break;
482
483 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100484 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000485 memcpy(clone, ptr, sizeof(struct xtables_match));
486 clone->mflags = 0;
487 /* This is a clone: */
488 clone->next = clone;
489
490 ptr = clone;
491 break;
492 }
493 }
494
495#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100496 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100497 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100498 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000499
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100500 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100501 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000502 "Couldn't load match `%s':%s\n",
503 name, dlerror());
504 }
505#else
506 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100507 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000508 ptr->loaded = 1;
509 else
510 ptr = NULL;
511 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100512 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100513 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000514 "Couldn't find match `%s'\n", name);
515 }
516#endif
517
518 if (ptr && matches) {
519 struct xtables_rule_match **i;
520 struct xtables_rule_match *newentry;
521
Jan Engelhardt630ef482009-01-27 14:58:41 +0100522 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000523
524 for (i = matches; *i; i = &(*i)->next) {
525 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100526 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000527 }
528 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100529 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000530 newentry->next = NULL;
531 *i = newentry;
532 }
533
534 return ptr;
535}
536
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100537struct xtables_target *
538xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000539{
540 struct xtables_target *ptr;
541
542 /* Standard target? */
543 if (strcmp(name, "") == 0
544 || strcmp(name, XTC_LABEL_ACCEPT) == 0
545 || strcmp(name, XTC_LABEL_DROP) == 0
546 || strcmp(name, XTC_LABEL_QUEUE) == 0
547 || strcmp(name, XTC_LABEL_RETURN) == 0)
548 name = "standard";
549
550 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
551 if (strcmp(name, ptr->name) == 0)
552 break;
553 }
554
555#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100556 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100557 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100558 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000559
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100560 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100561 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000562 "Couldn't load target `%s':%s\n",
563 name, dlerror());
564 }
565#else
566 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100567 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000568 ptr->loaded = 1;
569 else
570 ptr = NULL;
571 }
572 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100573 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000574 "Couldn't find target `%s'\n", name);
575 }
576#endif
577
578 if (ptr)
579 ptr->used = 1;
580
581 return ptr;
582}
583
584static int compatible_revision(const char *name, u_int8_t revision, int opt)
585{
586 struct xt_get_revision rev;
587 socklen_t s = sizeof(rev);
588 int max_rev, sockfd;
589
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100590 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000591 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000592 if (errno == EPERM) {
593 /* revision 0 is always supported. */
594 if (revision != 0)
595 fprintf(stderr, "Could not determine whether "
596 "revision %u is supported, "
597 "assuming it is.\n",
598 revision);
599 return 1;
600 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000601 fprintf(stderr, "Could not open socket to kernel: %s\n",
602 strerror(errno));
603 exit(1);
604 }
605
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100606 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000607
608 strcpy(rev.name, name);
609 rev.revision = revision;
610
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100611 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000612 if (max_rev < 0) {
613 /* Definitely don't support this? */
614 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
615 close(sockfd);
616 return 0;
617 } else if (errno == ENOPROTOOPT) {
618 close(sockfd);
619 /* Assume only revision 0 support (old kernel) */
620 return (revision == 0);
621 } else {
622 fprintf(stderr, "getsockopt failed strangely: %s\n",
623 strerror(errno));
624 exit(1);
625 }
626 }
627 close(sockfd);
628 return 1;
629}
630
631
632static int compatible_match_revision(const char *name, u_int8_t revision)
633{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100634 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000635}
636
637static int compatible_target_revision(const char *name, u_int8_t revision)
638{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100639 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000640}
641
642void xtables_register_match(struct xtables_match *me)
643{
644 struct xtables_match **i, *old;
645
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100646 if (strcmp(me->version, XTABLES_VERSION) != 0) {
647 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
648 "but \"%s\" is required.\n",
649 xtables_program_name, me->name,
650 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000651 exit(1);
652 }
653
654 /* Revision field stole a char from name. */
655 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
656 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100657 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000658 exit(1);
659 }
660
661 if (me->family >= NPROTO) {
662 fprintf(stderr,
663 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100664 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000665 exit(1);
666 }
667
668 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100669 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000670 return;
671
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100672 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000673 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100674 if (old->revision == me->revision &&
675 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000676 fprintf(stderr,
677 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100678 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000679 exit(1);
680 }
681
682 /* Now we have two (or more) options, check compatibility. */
683 if (compatible_match_revision(old->name, old->revision)
684 && old->revision > me->revision)
685 return;
686
Jan Engelhardt23545c22008-02-14 04:23:04 +0100687 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000688 if (!compatible_match_revision(me->name, me->revision))
689 return;
690
Jan Engelhardt23545c22008-02-14 04:23:04 +0100691 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
692 if (old->revision == me->revision && me->family == AF_UNSPEC)
693 return;
694
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000695 /* Delete old one. */
696 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
697 *i = old->next;
698 }
699
700 if (me->size != XT_ALIGN(me->size)) {
701 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100702 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000703 exit(1);
704 }
705
706 /* Append to list. */
707 for (i = &xtables_matches; *i; i = &(*i)->next);
708 me->next = NULL;
709 *i = me;
710
711 me->m = NULL;
712 me->mflags = 0;
713}
714
715void xtables_register_target(struct xtables_target *me)
716{
717 struct xtables_target *old;
718
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100719 if (strcmp(me->version, XTABLES_VERSION) != 0) {
720 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
721 "but \"%s\" is required.\n",
722 xtables_program_name, me->name,
723 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000724 exit(1);
725 }
726
727 /* Revision field stole a char from name. */
728 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
729 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100730 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000731 exit(1);
732 }
733
734 if (me->family >= NPROTO) {
735 fprintf(stderr,
736 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100737 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000738 exit(1);
739 }
740
741 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100742 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000743 return;
744
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100745 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000746 if (old) {
747 struct xtables_target **i;
748
Jan Engelhardt23545c22008-02-14 04:23:04 +0100749 if (old->revision == me->revision &&
750 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000751 fprintf(stderr,
752 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100753 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000754 exit(1);
755 }
756
757 /* Now we have two (or more) options, check compatibility. */
758 if (compatible_target_revision(old->name, old->revision)
759 && old->revision > me->revision)
760 return;
761
Jan Engelhardt23545c22008-02-14 04:23:04 +0100762 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000763 if (!compatible_target_revision(me->name, me->revision))
764 return;
765
Jan Engelhardt23545c22008-02-14 04:23:04 +0100766 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
767 if (old->revision == me->revision && me->family == AF_UNSPEC)
768 return;
769
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000770 /* Delete old one. */
771 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
772 *i = old->next;
773 }
774
775 if (me->size != XT_ALIGN(me->size)) {
776 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100777 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000778 exit(1);
779 }
780
781 /* Prepend to list. */
782 me->next = xtables_targets;
783 xtables_targets = me;
784 me->t = NULL;
785 me->tflags = 0;
786}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000787
Jan Engelhardta41545c2009-01-27 21:27:19 +0100788/**
789 * xtables_param_act - act on condition
790 * @status: a constant from enum xtables_exittype
791 *
792 * %XTF_ONLY_ONCE: print error message that option may only be used once.
793 * @p1: module name (e.g. "mark")
794 * @p2(...): option in conflict (e.g. "--mark")
795 * @p3(...): condition to match on (see extensions/ for examples)
796 *
797 * %XTF_NO_INVERT: option does not support inversion
798 * @p1: module name
799 * @p2: option in conflict
800 * @p3: condition to match on
801 *
802 * %XTF_BAD_VALUE: bad value for option
803 * @p1: module name
804 * @p2: option with which the problem occured (e.g. "--mark")
805 * @p3: string the user passed in (e.g. "99999999999999")
806 *
807 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
808 * @p1: module name
809 *
810 * Displays an error message and exits the program.
811 */
812void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000813{
814 const char *p2, *p3;
815 va_list args;
816 bool b;
817
818 va_start(args, p1);
819
820 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100821 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000822 p2 = va_arg(args, const char *);
823 b = va_arg(args, unsigned int);
824 if (!b)
825 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100826 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000827 "%s: \"%s\" option may only be specified once",
828 p1, p2);
829 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100830 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000831 p2 = va_arg(args, const char *);
832 b = va_arg(args, unsigned int);
833 if (!b)
834 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100835 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000836 "%s: \"%s\" option cannot be inverted", p1, p2);
837 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100838 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000839 p2 = va_arg(args, const char *);
840 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100841 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000842 "%s: Bad value for \"%s\" option: \"%s\"",
843 p1, p2, p3);
844 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100845 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000846 b = va_arg(args, unsigned int);
847 if (!b)
848 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100849 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000850 "%s: At most one action is possible", p1);
851 break;
852 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100853 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000854 break;
855 }
856
857 va_end(args);
858}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000859
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100860const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000861{
862 static char buf[20];
863 const unsigned char *bytep = (const void *)&addrp->s_addr;
864
865 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
866 return buf;
867}
868
869static const char *ipaddr_to_host(const struct in_addr *addr)
870{
871 struct hostent *host;
872
873 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
874 if (host == NULL)
875 return NULL;
876
877 return host->h_name;
878}
879
880static const char *ipaddr_to_network(const struct in_addr *addr)
881{
882 struct netent *net;
883
884 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
885 return net->n_name;
886
887 return NULL;
888}
889
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100890const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000891{
892 const char *name;
893
894 if ((name = ipaddr_to_host(addr)) != NULL ||
895 (name = ipaddr_to_network(addr)) != NULL)
896 return name;
897
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100898 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000899}
900
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100901const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000902{
903 static char buf[20];
904 uint32_t maskaddr, bits;
905 int i;
906
907 maskaddr = ntohl(mask->s_addr);
908
909 if (maskaddr == 0xFFFFFFFFL)
910 /* we don't want to see "/32" */
911 return "";
912
913 i = 32;
914 bits = 0xFFFFFFFEL;
915 while (--i >= 0 && maskaddr != bits)
916 bits <<= 1;
917 if (i >= 0)
918 sprintf(buf, "/%d", i);
919 else
920 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100921 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000922
923 return buf;
924}
925
Jan Engelhardtbd943842008-01-20 13:38:08 +0000926static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
927{
928 static struct in_addr addr;
929 unsigned char *addrp;
930 unsigned int onebyte;
931 char buf[20], *p, *q;
932 int i;
933
934 /* copy dotted string, because we need to modify it */
935 strncpy(buf, dotted, sizeof(buf) - 1);
936 buf[sizeof(buf) - 1] = '\0';
937 addrp = (void *)&addr.s_addr;
938
939 p = buf;
940 for (i = 0; i < 3; ++i) {
941 if ((q = strchr(p, '.')) == NULL) {
942 if (is_mask)
943 return NULL;
944
945 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100946 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000947 return NULL;
948
949 addrp[i] = onebyte;
950 while (i < 3)
951 addrp[++i] = 0;
952
953 return &addr;
954 }
955
956 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100957 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000958 return NULL;
959
960 addrp[i] = onebyte;
961 p = q + 1;
962 }
963
964 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100965 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000966 return NULL;
967
968 addrp[3] = onebyte;
969 return &addr;
970}
971
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100972struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000973{
974 return __numeric_to_ipaddr(dotted, false);
975}
976
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100977struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000978{
979 return __numeric_to_ipaddr(dotted, true);
980}
981
982static struct in_addr *network_to_ipaddr(const char *name)
983{
984 static struct in_addr addr;
985 struct netent *net;
986
987 if ((net = getnetbyname(name)) != NULL) {
988 if (net->n_addrtype != AF_INET)
989 return NULL;
990 addr.s_addr = htonl(net->n_net);
991 return &addr;
992 }
993
994 return NULL;
995}
996
997static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
998{
999 struct hostent *host;
1000 struct in_addr *addr;
1001 unsigned int i;
1002
1003 *naddr = 0;
1004 if ((host = gethostbyname(name)) != NULL) {
1005 if (host->h_addrtype != AF_INET ||
1006 host->h_length != sizeof(struct in_addr))
1007 return NULL;
1008
1009 while (host->h_addr_list[*naddr] != NULL)
1010 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001011 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001012 for (i = 0; i < *naddr; i++)
1013 memcpy(&addr[i], host->h_addr_list[i],
1014 sizeof(struct in_addr));
1015 return addr;
1016 }
1017
1018 return NULL;
1019}
1020
1021static struct in_addr *
1022ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1023{
1024 struct in_addr *addrptmp, *addrp;
1025
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001026 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001027 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001028 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001029 memcpy(addrp, addrptmp, sizeof(*addrp));
1030 *naddrs = 1;
1031 return addrp;
1032 }
1033 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1034 return addrptmp;
1035
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001036 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001037}
1038
1039static struct in_addr *parse_ipmask(const char *mask)
1040{
1041 static struct in_addr maskaddr;
1042 struct in_addr *addrp;
1043 unsigned int bits;
1044
1045 if (mask == NULL) {
1046 /* no mask at all defaults to 32 bits */
1047 maskaddr.s_addr = 0xFFFFFFFF;
1048 return &maskaddr;
1049 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001050 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001051 /* dotted_to_addr already returns a network byte order addr */
1052 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001053 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001054 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001055 "invalid mask `%s' specified", mask);
1056 if (bits != 0) {
1057 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1058 return &maskaddr;
1059 }
1060
1061 maskaddr.s_addr = 0U;
1062 return &maskaddr;
1063}
1064
Jan Engelhardta0baae82009-01-30 04:32:50 +01001065/**
1066 * xtables_ipparse_any - transform arbitrary name to in_addr
1067 *
1068 * Possible inputs (pseudo regex):
1069 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1070 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1071 */
1072void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1073 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001074{
1075 unsigned int i, j, k, n;
1076 struct in_addr *addrp;
1077 char buf[256], *p;
1078
1079 strncpy(buf, name, sizeof(buf) - 1);
1080 buf[sizeof(buf) - 1] = '\0';
1081 if ((p = strrchr(buf, '/')) != NULL) {
1082 *p = '\0';
1083 addrp = parse_ipmask(p + 1);
1084 } else {
1085 addrp = parse_ipmask(NULL);
1086 }
1087 memcpy(maskp, addrp, sizeof(*maskp));
1088
1089 /* if a null mask is given, the name is ignored, like in "any/0" */
1090 if (maskp->s_addr == 0U)
1091 strcpy(buf, "0.0.0.0");
1092
1093 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1094 n = *naddrs;
1095 for (i = 0, j = 0; i < n; ++i) {
1096 addrp[j++].s_addr &= maskp->s_addr;
1097 for (k = 0; k < j - 1; ++k)
1098 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1099 --*naddrs;
1100 --j;
1101 break;
1102 }
1103 }
1104}
1105
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001106const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001107{
1108 /* 0000:0000:0000:0000:0000:000.000.000.000
1109 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1110 static char buf[50+1];
1111 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1112}
1113
1114static const char *ip6addr_to_host(const struct in6_addr *addr)
1115{
1116 static char hostname[NI_MAXHOST];
1117 struct sockaddr_in6 saddr;
1118 int err;
1119
1120 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1121 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1122 saddr.sin6_family = AF_INET6;
1123
1124 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1125 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1126 if (err != 0) {
1127#ifdef DEBUG
1128 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1129#endif
1130 return NULL;
1131 }
1132
1133#ifdef DEBUG
1134 fprintf (stderr, "\naddr2host: %s\n", hostname);
1135#endif
1136 return hostname;
1137}
1138
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001139const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001140{
1141 const char *name;
1142
1143 if ((name = ip6addr_to_host(addr)) != NULL)
1144 return name;
1145
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001146 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001147}
1148
1149static int ip6addr_prefix_length(const struct in6_addr *k)
1150{
1151 unsigned int bits = 0;
1152 uint32_t a, b, c, d;
1153
Jan Engelhardt48607812008-06-10 15:17:53 +02001154 a = ntohl(k->s6_addr32[0]);
1155 b = ntohl(k->s6_addr32[1]);
1156 c = ntohl(k->s6_addr32[2]);
1157 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001158 while (a & 0x80000000U) {
1159 ++bits;
1160 a <<= 1;
1161 a |= (b >> 31) & 1;
1162 b <<= 1;
1163 b |= (c >> 31) & 1;
1164 c <<= 1;
1165 c |= (d >> 31) & 1;
1166 d <<= 1;
1167 }
1168 if (a != 0 || b != 0 || c != 0 || d != 0)
1169 return -1;
1170 return bits;
1171}
1172
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001173const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001174{
1175 static char buf[50+2];
1176 int l = ip6addr_prefix_length(addrp);
1177
1178 if (l == -1) {
1179 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001180 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001181 return buf;
1182 }
1183 sprintf(buf, "/%d", l);
1184 return buf;
1185}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001186
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001187struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001188{
1189 static struct in6_addr ap;
1190 int err;
1191
1192 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1193 return &ap;
1194#ifdef DEBUG
1195 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1196#endif
1197 return NULL;
1198}
1199
1200static struct in6_addr *
1201host_to_ip6addr(const char *name, unsigned int *naddr)
1202{
1203 static struct in6_addr *addr;
1204 struct addrinfo hints;
1205 struct addrinfo *res;
1206 int err;
1207
1208 memset(&hints, 0, sizeof(hints));
1209 hints.ai_flags = AI_CANONNAME;
1210 hints.ai_family = AF_INET6;
1211 hints.ai_socktype = SOCK_RAW;
1212 hints.ai_protocol = IPPROTO_IPV6;
1213 hints.ai_next = NULL;
1214
1215 *naddr = 0;
1216 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1217#ifdef DEBUG
1218 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1219#endif
1220 return NULL;
1221 } else {
1222 if (res->ai_family != AF_INET6 ||
1223 res->ai_addrlen != sizeof(struct sockaddr_in6))
1224 return NULL;
1225
1226#ifdef DEBUG
1227 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1228 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1229#endif
1230 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001231 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001232 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1233 sizeof(struct in6_addr));
1234 freeaddrinfo(res);
1235 *naddr = 1;
1236 return addr;
1237 }
1238
1239 return NULL;
1240}
1241
1242static struct in6_addr *network_to_ip6addr(const char *name)
1243{
1244 /* abort();*/
1245 /* TODO: not implemented yet, but the exception breaks the
1246 * name resolvation */
1247 return NULL;
1248}
1249
1250static struct in6_addr *
1251ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1252{
1253 struct in6_addr *addrp, *addrptmp;
1254
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001255 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001256 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001257 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001258 memcpy(addrp, addrptmp, sizeof(*addrp));
1259 *naddrs = 1;
1260 return addrp;
1261 }
1262 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1263 return addrp;
1264
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001265 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001266}
1267
1268static struct in6_addr *parse_ip6mask(char *mask)
1269{
1270 static struct in6_addr maskaddr;
1271 struct in6_addr *addrp;
1272 unsigned int bits;
1273
1274 if (mask == NULL) {
1275 /* no mask at all defaults to 128 bits */
1276 memset(&maskaddr, 0xff, sizeof maskaddr);
1277 return &maskaddr;
1278 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001279 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001280 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001281 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001282 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001283 "invalid mask `%s' specified", mask);
1284 if (bits != 0) {
1285 char *p = (void *)&maskaddr;
1286 memset(p, 0xff, bits / 8);
1287 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1288 p[bits/8] = 0xff << (8 - (bits & 7));
1289 return &maskaddr;
1290 }
1291
1292 memset(&maskaddr, 0, sizeof(maskaddr));
1293 return &maskaddr;
1294}
1295
Jan Engelhardta0baae82009-01-30 04:32:50 +01001296void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1297 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001298{
1299 struct in6_addr *addrp;
1300 unsigned int i, j, k, n;
1301 char buf[256], *p;
1302
1303 strncpy(buf, name, sizeof(buf) - 1);
1304 buf[sizeof(buf)-1] = '\0';
1305 if ((p = strrchr(buf, '/')) != NULL) {
1306 *p = '\0';
1307 addrp = parse_ip6mask(p + 1);
1308 } else {
1309 addrp = parse_ip6mask(NULL);
1310 }
1311 memcpy(maskp, addrp, sizeof(*maskp));
1312
1313 /* if a null mask is given, the name is ignored, like in "any/0" */
1314 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1315 strcpy(buf, "::");
1316
1317 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1318 n = *naddrs;
1319 for (i = 0, j = 0; i < n; ++i) {
1320 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001321 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001322 ++j;
1323 for (k = 0; k < j - 1; ++k)
1324 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1325 --*naddrs;
1326 --j;
1327 break;
1328 }
1329 }
1330}
Max Kellermanna5d09942008-01-29 13:44:34 +00001331
Jan Engelhardta0baae82009-01-30 04:32:50 +01001332void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001333{
1334 static const char no_quote_chars[] = "_-0123456789"
1335 "abcdefghijklmnopqrstuvwxyz"
1336 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1337 static const char escape_chars[] = "\"\\'";
1338 size_t length;
1339 const char *p;
1340
1341 length = strcspn(value, no_quote_chars);
1342 if (length > 0 && value[length] == 0) {
1343 /* no quoting required */
1344 fputs(value, stdout);
1345 putchar(' ');
1346 } else {
1347 /* there is at least one dangerous character in the
1348 value, which we have to quote. Write double quotes
1349 around the value and escape special characters with
1350 a backslash */
1351 putchar('"');
1352
1353 for (p = strpbrk(value, escape_chars); p != NULL;
1354 p = strpbrk(value, escape_chars)) {
1355 if (p > value)
1356 fwrite(value, 1, p - value, stdout);
1357 putchar('\\');
1358 putchar(*p);
1359 value = p + 1;
1360 }
1361
1362 /* print the rest and finish the double quoted
1363 string */
1364 fputs(value, stdout);
1365 printf("\" ");
1366 }
1367}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001368
1369/**
1370 * Check for option-intrapositional negation.
1371 * Do not use in new code.
1372 */
1373int xtables_check_inverse(const char option[], int *invert,
1374 int *my_optind, int argc)
1375{
1376 if (option && strcmp(option, "!") == 0) {
1377 fprintf(stderr, "Using intrapositioned negation "
1378 "(`--option ! this`) is deprecated in favor of "
1379 "extrapositioned (`! --option this`).\n");
1380
1381 if (*invert)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001382 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001383 "Multiple `!' flags not allowed");
1384 *invert = true;
1385 if (my_optind != NULL) {
1386 ++*my_optind;
1387 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001388 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001389 "no argument following `!'");
1390 }
1391
1392 return true;
1393 }
1394 return false;
1395}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001396
1397const struct xtables_pprot xtables_chain_protos[] = {
1398 {"tcp", IPPROTO_TCP},
1399 {"sctp", IPPROTO_SCTP},
1400 {"udp", IPPROTO_UDP},
1401 {"udplite", IPPROTO_UDPLITE},
1402 {"icmp", IPPROTO_ICMP},
1403 {"icmpv6", IPPROTO_ICMPV6},
1404 {"ipv6-icmp", IPPROTO_ICMPV6},
1405 {"esp", IPPROTO_ESP},
1406 {"ah", IPPROTO_AH},
1407 {"ipv6-mh", IPPROTO_MH},
1408 {"mh", IPPROTO_MH},
1409 {"all", 0},
1410 {NULL},
1411};
1412
1413u_int16_t
1414xtables_parse_protocol(const char *s)
1415{
1416 unsigned int proto;
1417
1418 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1419 struct protoent *pent;
1420
1421 /* first deal with the special case of 'all' to prevent
1422 * people from being able to redefine 'all' in nsswitch
1423 * and/or provoke expensive [not working] ldap/nis/...
1424 * lookups */
1425 if (!strcmp(s, "all"))
1426 return 0;
1427
1428 if ((pent = getprotobyname(s)))
1429 proto = pent->p_proto;
1430 else {
1431 unsigned int i;
1432 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1433 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1434 proto = xtables_chain_protos[i].num;
1435 break;
1436 }
1437 }
1438 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001439 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001440 "unknown protocol `%s' specified",
1441 s);
1442 }
1443 }
1444
1445 return proto;
1446}