blob: c8024bf225282cf78a5c3e3f6440ba129290acb3 [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 Salim139b3fe2009-02-12 11:43:01 -0500100void xtables_free_opts(int reset_offset)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +0100101{
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500102 if (xt_params->opts != xt_params->orig_opts) {
103 free(xt_params->opts);
104 xt_params->opts = xt_params->orig_opts;
Jamal Hadi Salim84c30552009-02-11 13:00:02 +0100105 if (reset_offset)
106 xt_params->option_offset = 0;
107 }
108}
109
Jamal Hadi Salim85332212009-02-12 09:33:59 -0500110void xtables_set_revision(char *name, u_int8_t revision)
111{
112 /* Old kernel sources don't have ".revision" field,
113 * but we stole a byte from name. */
114 name[XT_FUNCTION_MAXNAMELEN - 2] = '\0';
115 name[XT_FUNCTION_MAXNAMELEN - 1] = revision;
116}
117
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100118/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100119 * xtables_afinfo - protocol family dependent information
120 * @kmod: kernel module basename (e.g. "ip_tables")
121 * @libprefix: prefix of .so library name (e.g. "libipt_")
122 * @family: nfproto family
123 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
124 * @so_rev_match: optname to check revision support of match
125 * @so_rev_target: optname to check revision support of target
126 */
127struct xtables_afinfo {
128 const char *kmod;
129 const char *libprefix;
130 uint8_t family;
131 uint8_t ipproto;
132 int so_rev_match;
133 int so_rev_target;
134};
135
136static const struct xtables_afinfo afinfo_ipv4 = {
137 .kmod = "ip_tables",
138 .libprefix = "libipt_",
139 .family = NFPROTO_IPV4,
140 .ipproto = IPPROTO_IP,
141 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
142 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
143};
144
145static const struct xtables_afinfo afinfo_ipv6 = {
146 .kmod = "ip6_tables",
147 .libprefix = "libip6t_",
148 .family = NFPROTO_IPV6,
149 .ipproto = IPPROTO_IPV6,
150 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
151 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
152};
153
154static const struct xtables_afinfo *afinfo;
155
156/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100157 * Program will set this to its own name.
158 */
159const char *xtables_program_name;
160
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100161/* Search path for Xtables .so files */
162static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000163
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000164/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100165const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000166
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000167/* Keeping track of external matches and targets: linked lists. */
168struct xtables_match *xtables_matches;
169struct xtables_target *xtables_targets;
170
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100171void xtables_init(void)
172{
173 xtables_libdir = getenv("XTABLES_LIBDIR");
174 if (xtables_libdir != NULL)
175 return;
176 xtables_libdir = getenv("IPTABLES_LIB_DIR");
177 if (xtables_libdir != NULL) {
178 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
179 "use XTABLES_LIBDIR.\n");
180 return;
181 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100182 /*
183 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
184 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
185 * for these env vars are deprecated anyhow, and in light of the
186 * (shared) libxt_*.so files, makes less sense to have
187 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
188 */
189 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
190 if (xtables_libdir != NULL) {
191 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
192 "use XTABLES_LIBDIR.\n");
193 return;
194 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100195 xtables_libdir = XTABLES_LIBDIR;
196}
197
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100198void xtables_set_nfproto(uint8_t nfproto)
199{
200 switch (nfproto) {
201 case NFPROTO_IPV4:
202 afinfo = &afinfo_ipv4;
203 break;
204 case NFPROTO_IPV6:
205 afinfo = &afinfo_ipv6;
206 break;
207 default:
208 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
209 __func__);
210 }
211}
212
Jan Engelhardt630ef482009-01-27 14:58:41 +0100213/**
214 * xtables_*alloc - wrappers that exit on failure
215 */
216void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000217{
218 void *p;
219
220 if ((p = calloc(count, size)) == NULL) {
221 perror("ip[6]tables: calloc failed");
222 exit(1);
223 }
224
225 return p;
226}
227
Jan Engelhardt630ef482009-01-27 14:58:41 +0100228void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000229{
230 void *p;
231
232 if ((p = malloc(size)) == NULL) {
233 perror("ip[6]tables: malloc failed");
234 exit(1);
235 }
236
237 return p;
238}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000239
240static char *get_modprobe(void)
241{
242 int procfile;
243 char *ret;
244
245#define PROCFILE_BUFSIZ 1024
246 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
247 if (procfile < 0)
248 return NULL;
249
250 ret = (char *) malloc(PROCFILE_BUFSIZ);
251 if (ret) {
252 memset(ret, 0, PROCFILE_BUFSIZ);
253 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
254 case -1: goto fail;
255 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
256 }
257 if (ret[strlen(ret)-1]=='\n')
258 ret[strlen(ret)-1]=0;
259 close(procfile);
260 return ret;
261 }
262 fail:
263 free(ret);
264 close(procfile);
265 return NULL;
266}
267
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100268int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000269{
270 char *buf = NULL;
271 char *argv[4];
272 int status;
273
274 /* If they don't explicitly set it, read out of kernel */
275 if (!modprobe) {
276 buf = get_modprobe();
277 if (!buf)
278 return -1;
279 modprobe = buf;
280 }
281
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100282 /*
283 * Need to flush the buffer, or the child may output it again
284 * when switching the program thru execv.
285 */
286 fflush(stdout);
287
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000288 switch (fork()) {
289 case 0:
290 argv[0] = (char *)modprobe;
291 argv[1] = (char *)modname;
292 if (quiet) {
293 argv[2] = "-q";
294 argv[3] = NULL;
295 } else {
296 argv[2] = NULL;
297 argv[3] = NULL;
298 }
299 execv(argv[0], argv);
300
301 /* not usually reached */
302 exit(1);
303 case -1:
304 return -1;
305
306 default: /* parent */
307 wait(&status);
308 }
309
310 free(buf);
311 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
312 return 0;
313 return -1;
314}
315
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100316int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000317{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100318 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000319 static int ret = -1;
320
321 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100322 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000323 loaded = (ret == 0);
324 }
325
326 return ret;
327}
328
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100329/**
330 * xtables_strtou{i,l} - string to number conversion
331 * @s: input string
332 * @end: like strtoul's "end" pointer
333 * @value: pointer for result
334 * @min: minimum accepted value
335 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000336 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100337 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
338 * "15a" is rejected.
339 * In either case, the value obtained is compared for min-max compliance.
340 * Base is always 0, i.e. autodetect depending on @s.
341 *
342 * Returns true/false whether number was accepted. On failure, *value has
343 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000344 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100345bool xtables_strtoul(const char *s, char **end, unsigned long *value,
346 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000347{
348 unsigned long v;
349 char *my_end;
350
351 errno = 0;
352 v = strtoul(s, &my_end, 0);
353
354 if (my_end == s)
355 return false;
356 if (end != NULL)
357 *end = my_end;
358
359 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
360 if (value != NULL)
361 *value = v;
362 if (end == NULL)
363 return *my_end == '\0';
364 return true;
365 }
366
367 return false;
368}
369
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100370bool xtables_strtoui(const char *s, char **end, unsigned int *value,
371 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000372{
373 unsigned long v;
374 bool ret;
375
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100376 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000377 if (value != NULL)
378 *value = v;
379 return ret;
380}
381
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100382int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000383{
384 struct servent *service;
385
386 if ((service = getservbyname(name, proto)) != NULL)
387 return ntohs((unsigned short) service->s_port);
388
389 return -1;
390}
391
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100392u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000393{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100394 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000395
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100396 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100397 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100398 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000399
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100400 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000401 "invalid port/service `%s' specified", port);
402}
403
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100404void xtables_parse_interface(const char *arg, char *vianame,
405 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000406{
407 int vialen = strlen(arg);
408 unsigned int i;
409
410 memset(mask, 0, IFNAMSIZ);
411 memset(vianame, 0, IFNAMSIZ);
412
413 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100414 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000415 "interface name `%s' must be shorter than IFNAMSIZ"
416 " (%i)", arg, IFNAMSIZ-1);
417
418 strcpy(vianame, arg);
419 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
420 memset(mask, 0, IFNAMSIZ);
421 else if (vianame[vialen - 1] == '+') {
422 memset(mask, 0xFF, vialen - 1);
423 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
424 /* Don't remove `+' here! -HW */
425 } else {
426 /* Include nul-terminator in match */
427 memset(mask, 0xFF, vialen + 1);
428 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
429 for (i = 0; vianame[i]; i++) {
430 if (vianame[i] == ':' ||
431 vianame[i] == '!' ||
432 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000433 fprintf(stderr,
434 "Warning: weird character in interface"
435 " `%s' (No aliases, :, ! or *).\n",
436 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000437 break;
438 }
439 }
440 }
441}
442
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200443#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100444static void *load_extension(const char *search_path, const char *prefix,
445 const char *name, bool is_target)
446{
447 const char *dir = search_path, *next;
448 void *ptr = NULL;
449 struct stat sb;
450 char path[256];
451
452 do {
453 next = strchr(dir, ':');
454 if (next == NULL)
455 next = dir + strlen(dir);
456 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200457 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100458
459 if (dlopen(path, RTLD_NOW) != NULL) {
460 /* Found library. If it didn't register itself,
461 maybe they specified target as match. */
462 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100463 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100464 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100465 ptr = xtables_find_match(name,
466 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100467 } else if (stat(path, &sb) == 0) {
468 fprintf(stderr, "%s: %s\n", path, dlerror());
469 }
470
471 if (ptr != NULL)
472 return ptr;
473
474 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200475 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100476 if (dlopen(path, RTLD_NOW) != NULL) {
477 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100478 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100479 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100480 ptr = xtables_find_match(name,
481 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100482 } else if (stat(path, &sb) == 0) {
483 fprintf(stderr, "%s: %s\n", path, dlerror());
484 }
485
486 if (ptr != NULL)
487 return ptr;
488
489 dir = next + 1;
490 } while (*next != '\0');
491
492 return NULL;
493}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200494#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100495
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100496struct xtables_match *
497xtables_find_match(const char *name, enum xtables_tryload tryload,
498 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000499{
500 struct xtables_match *ptr;
501 const char *icmp6 = "icmp6";
502
503 /* This is ugly as hell. Nonetheless, there is no way of changing
504 * this without hurting backwards compatibility */
505 if ( (strcmp(name,"icmpv6") == 0) ||
506 (strcmp(name,"ipv6-icmp") == 0) ||
507 (strcmp(name,"icmp6") == 0) )
508 name = icmp6;
509
510 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
511 if (strcmp(name, ptr->name) == 0) {
512 struct xtables_match *clone;
513
514 /* First match of this type: */
515 if (ptr->m == NULL)
516 break;
517
518 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100519 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000520 memcpy(clone, ptr, sizeof(struct xtables_match));
521 clone->mflags = 0;
522 /* This is a clone: */
523 clone->next = clone;
524
525 ptr = clone;
526 break;
527 }
528 }
529
530#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100531 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100532 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100533 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000534
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100535 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100536 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000537 "Couldn't load match `%s':%s\n",
538 name, dlerror());
539 }
540#else
541 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100542 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000543 ptr->loaded = 1;
544 else
545 ptr = NULL;
546 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100547 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100548 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000549 "Couldn't find match `%s'\n", name);
550 }
551#endif
552
553 if (ptr && matches) {
554 struct xtables_rule_match **i;
555 struct xtables_rule_match *newentry;
556
Jan Engelhardt630ef482009-01-27 14:58:41 +0100557 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000558
559 for (i = matches; *i; i = &(*i)->next) {
560 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100561 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000562 }
563 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100564 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000565 newentry->next = NULL;
566 *i = newentry;
567 }
568
569 return ptr;
570}
571
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100572struct xtables_target *
573xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000574{
575 struct xtables_target *ptr;
576
577 /* Standard target? */
578 if (strcmp(name, "") == 0
579 || strcmp(name, XTC_LABEL_ACCEPT) == 0
580 || strcmp(name, XTC_LABEL_DROP) == 0
581 || strcmp(name, XTC_LABEL_QUEUE) == 0
582 || strcmp(name, XTC_LABEL_RETURN) == 0)
583 name = "standard";
584
585 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
586 if (strcmp(name, ptr->name) == 0)
587 break;
588 }
589
590#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100591 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100592 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100593 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000594
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100595 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100596 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000597 "Couldn't load target `%s':%s\n",
598 name, dlerror());
599 }
600#else
601 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100602 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000603 ptr->loaded = 1;
604 else
605 ptr = NULL;
606 }
607 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100608 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000609 "Couldn't find target `%s'\n", name);
610 }
611#endif
612
613 if (ptr)
614 ptr->used = 1;
615
616 return ptr;
617}
618
619static int compatible_revision(const char *name, u_int8_t revision, int opt)
620{
621 struct xt_get_revision rev;
622 socklen_t s = sizeof(rev);
623 int max_rev, sockfd;
624
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100625 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000626 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000627 if (errno == EPERM) {
628 /* revision 0 is always supported. */
629 if (revision != 0)
630 fprintf(stderr, "Could not determine whether "
631 "revision %u is supported, "
632 "assuming it is.\n",
633 revision);
634 return 1;
635 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000636 fprintf(stderr, "Could not open socket to kernel: %s\n",
637 strerror(errno));
638 exit(1);
639 }
640
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100641 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000642
643 strcpy(rev.name, name);
644 rev.revision = revision;
645
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100646 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000647 if (max_rev < 0) {
648 /* Definitely don't support this? */
649 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
650 close(sockfd);
651 return 0;
652 } else if (errno == ENOPROTOOPT) {
653 close(sockfd);
654 /* Assume only revision 0 support (old kernel) */
655 return (revision == 0);
656 } else {
657 fprintf(stderr, "getsockopt failed strangely: %s\n",
658 strerror(errno));
659 exit(1);
660 }
661 }
662 close(sockfd);
663 return 1;
664}
665
666
667static int compatible_match_revision(const char *name, u_int8_t revision)
668{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100669 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000670}
671
672static int compatible_target_revision(const char *name, u_int8_t revision)
673{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100674 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000675}
676
677void xtables_register_match(struct xtables_match *me)
678{
679 struct xtables_match **i, *old;
680
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100681 if (strcmp(me->version, XTABLES_VERSION) != 0) {
682 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
683 "but \"%s\" is required.\n",
684 xtables_program_name, me->name,
685 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000686 exit(1);
687 }
688
689 /* Revision field stole a char from name. */
690 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
691 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100692 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000693 exit(1);
694 }
695
696 if (me->family >= NPROTO) {
697 fprintf(stderr,
698 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100699 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000700 exit(1);
701 }
702
703 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100704 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000705 return;
706
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100707 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000708 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100709 if (old->revision == me->revision &&
710 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000711 fprintf(stderr,
712 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100713 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000714 exit(1);
715 }
716
717 /* Now we have two (or more) options, check compatibility. */
718 if (compatible_match_revision(old->name, old->revision)
719 && old->revision > me->revision)
720 return;
721
Jan Engelhardt23545c22008-02-14 04:23:04 +0100722 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000723 if (!compatible_match_revision(me->name, me->revision))
724 return;
725
Jan Engelhardt23545c22008-02-14 04:23:04 +0100726 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
727 if (old->revision == me->revision && me->family == AF_UNSPEC)
728 return;
729
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000730 /* Delete old one. */
731 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
732 *i = old->next;
733 }
734
735 if (me->size != XT_ALIGN(me->size)) {
736 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100737 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000738 exit(1);
739 }
740
741 /* Append to list. */
742 for (i = &xtables_matches; *i; i = &(*i)->next);
743 me->next = NULL;
744 *i = me;
745
746 me->m = NULL;
747 me->mflags = 0;
748}
749
750void xtables_register_target(struct xtables_target *me)
751{
752 struct xtables_target *old;
753
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100754 if (strcmp(me->version, XTABLES_VERSION) != 0) {
755 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
756 "but \"%s\" is required.\n",
757 xtables_program_name, me->name,
758 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000759 exit(1);
760 }
761
762 /* Revision field stole a char from name. */
763 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
764 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100765 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000766 exit(1);
767 }
768
769 if (me->family >= NPROTO) {
770 fprintf(stderr,
771 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100772 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000773 exit(1);
774 }
775
776 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100777 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000778 return;
779
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100780 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000781 if (old) {
782 struct xtables_target **i;
783
Jan Engelhardt23545c22008-02-14 04:23:04 +0100784 if (old->revision == me->revision &&
785 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000786 fprintf(stderr,
787 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100788 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000789 exit(1);
790 }
791
792 /* Now we have two (or more) options, check compatibility. */
793 if (compatible_target_revision(old->name, old->revision)
794 && old->revision > me->revision)
795 return;
796
Jan Engelhardt23545c22008-02-14 04:23:04 +0100797 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000798 if (!compatible_target_revision(me->name, me->revision))
799 return;
800
Jan Engelhardt23545c22008-02-14 04:23:04 +0100801 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
802 if (old->revision == me->revision && me->family == AF_UNSPEC)
803 return;
804
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000805 /* Delete old one. */
806 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
807 *i = old->next;
808 }
809
810 if (me->size != XT_ALIGN(me->size)) {
811 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100812 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000813 exit(1);
814 }
815
816 /* Prepend to list. */
817 me->next = xtables_targets;
818 xtables_targets = me;
819 me->t = NULL;
820 me->tflags = 0;
821}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000822
Jan Engelhardta41545c2009-01-27 21:27:19 +0100823/**
824 * xtables_param_act - act on condition
825 * @status: a constant from enum xtables_exittype
826 *
827 * %XTF_ONLY_ONCE: print error message that option may only be used once.
828 * @p1: module name (e.g. "mark")
829 * @p2(...): option in conflict (e.g. "--mark")
830 * @p3(...): condition to match on (see extensions/ for examples)
831 *
832 * %XTF_NO_INVERT: option does not support inversion
833 * @p1: module name
834 * @p2: option in conflict
835 * @p3: condition to match on
836 *
837 * %XTF_BAD_VALUE: bad value for option
838 * @p1: module name
839 * @p2: option with which the problem occured (e.g. "--mark")
840 * @p3: string the user passed in (e.g. "99999999999999")
841 *
842 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
843 * @p1: module name
844 *
845 * Displays an error message and exits the program.
846 */
847void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000848{
849 const char *p2, *p3;
850 va_list args;
851 bool b;
852
853 va_start(args, p1);
854
855 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100856 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000857 p2 = va_arg(args, const char *);
858 b = va_arg(args, unsigned int);
859 if (!b)
860 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100861 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000862 "%s: \"%s\" option may only be specified once",
863 p1, p2);
864 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100865 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000866 p2 = va_arg(args, const char *);
867 b = va_arg(args, unsigned int);
868 if (!b)
869 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100870 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000871 "%s: \"%s\" option cannot be inverted", p1, p2);
872 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100873 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000874 p2 = va_arg(args, const char *);
875 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100876 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000877 "%s: Bad value for \"%s\" option: \"%s\"",
878 p1, p2, p3);
879 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100880 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000881 b = va_arg(args, unsigned int);
882 if (!b)
883 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100884 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000885 "%s: At most one action is possible", p1);
886 break;
887 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100888 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000889 break;
890 }
891
892 va_end(args);
893}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000894
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100895const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000896{
897 static char buf[20];
898 const unsigned char *bytep = (const void *)&addrp->s_addr;
899
900 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
901 return buf;
902}
903
904static const char *ipaddr_to_host(const struct in_addr *addr)
905{
906 struct hostent *host;
907
908 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
909 if (host == NULL)
910 return NULL;
911
912 return host->h_name;
913}
914
915static const char *ipaddr_to_network(const struct in_addr *addr)
916{
917 struct netent *net;
918
919 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
920 return net->n_name;
921
922 return NULL;
923}
924
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100925const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000926{
927 const char *name;
928
929 if ((name = ipaddr_to_host(addr)) != NULL ||
930 (name = ipaddr_to_network(addr)) != NULL)
931 return name;
932
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100933 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000934}
935
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100936const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000937{
938 static char buf[20];
939 uint32_t maskaddr, bits;
940 int i;
941
942 maskaddr = ntohl(mask->s_addr);
943
944 if (maskaddr == 0xFFFFFFFFL)
945 /* we don't want to see "/32" */
946 return "";
947
948 i = 32;
949 bits = 0xFFFFFFFEL;
950 while (--i >= 0 && maskaddr != bits)
951 bits <<= 1;
952 if (i >= 0)
953 sprintf(buf, "/%d", i);
954 else
955 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100956 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000957
958 return buf;
959}
960
Jan Engelhardtbd943842008-01-20 13:38:08 +0000961static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
962{
963 static struct in_addr addr;
964 unsigned char *addrp;
965 unsigned int onebyte;
966 char buf[20], *p, *q;
967 int i;
968
969 /* copy dotted string, because we need to modify it */
970 strncpy(buf, dotted, sizeof(buf) - 1);
971 buf[sizeof(buf) - 1] = '\0';
972 addrp = (void *)&addr.s_addr;
973
974 p = buf;
975 for (i = 0; i < 3; ++i) {
976 if ((q = strchr(p, '.')) == NULL) {
977 if (is_mask)
978 return NULL;
979
980 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100981 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000982 return NULL;
983
984 addrp[i] = onebyte;
985 while (i < 3)
986 addrp[++i] = 0;
987
988 return &addr;
989 }
990
991 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100992 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000993 return NULL;
994
995 addrp[i] = onebyte;
996 p = q + 1;
997 }
998
999 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001000 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001001 return NULL;
1002
1003 addrp[3] = onebyte;
1004 return &addr;
1005}
1006
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001007struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001008{
1009 return __numeric_to_ipaddr(dotted, false);
1010}
1011
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001012struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001013{
1014 return __numeric_to_ipaddr(dotted, true);
1015}
1016
1017static struct in_addr *network_to_ipaddr(const char *name)
1018{
1019 static struct in_addr addr;
1020 struct netent *net;
1021
1022 if ((net = getnetbyname(name)) != NULL) {
1023 if (net->n_addrtype != AF_INET)
1024 return NULL;
1025 addr.s_addr = htonl(net->n_net);
1026 return &addr;
1027 }
1028
1029 return NULL;
1030}
1031
1032static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1033{
1034 struct hostent *host;
1035 struct in_addr *addr;
1036 unsigned int i;
1037
1038 *naddr = 0;
1039 if ((host = gethostbyname(name)) != NULL) {
1040 if (host->h_addrtype != AF_INET ||
1041 host->h_length != sizeof(struct in_addr))
1042 return NULL;
1043
1044 while (host->h_addr_list[*naddr] != NULL)
1045 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001046 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001047 for (i = 0; i < *naddr; i++)
1048 memcpy(&addr[i], host->h_addr_list[i],
1049 sizeof(struct in_addr));
1050 return addr;
1051 }
1052
1053 return NULL;
1054}
1055
1056static struct in_addr *
1057ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1058{
1059 struct in_addr *addrptmp, *addrp;
1060
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001061 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001062 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001063 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001064 memcpy(addrp, addrptmp, sizeof(*addrp));
1065 *naddrs = 1;
1066 return addrp;
1067 }
1068 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1069 return addrptmp;
1070
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001071 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001072}
1073
1074static struct in_addr *parse_ipmask(const char *mask)
1075{
1076 static struct in_addr maskaddr;
1077 struct in_addr *addrp;
1078 unsigned int bits;
1079
1080 if (mask == NULL) {
1081 /* no mask at all defaults to 32 bits */
1082 maskaddr.s_addr = 0xFFFFFFFF;
1083 return &maskaddr;
1084 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001085 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001086 /* dotted_to_addr already returns a network byte order addr */
1087 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001088 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001089 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001090 "invalid mask `%s' specified", mask);
1091 if (bits != 0) {
1092 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1093 return &maskaddr;
1094 }
1095
1096 maskaddr.s_addr = 0U;
1097 return &maskaddr;
1098}
1099
Jan Engelhardta0baae82009-01-30 04:32:50 +01001100/**
1101 * xtables_ipparse_any - transform arbitrary name to in_addr
1102 *
1103 * Possible inputs (pseudo regex):
1104 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1105 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1106 */
1107void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1108 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001109{
1110 unsigned int i, j, k, n;
1111 struct in_addr *addrp;
1112 char buf[256], *p;
1113
1114 strncpy(buf, name, sizeof(buf) - 1);
1115 buf[sizeof(buf) - 1] = '\0';
1116 if ((p = strrchr(buf, '/')) != NULL) {
1117 *p = '\0';
1118 addrp = parse_ipmask(p + 1);
1119 } else {
1120 addrp = parse_ipmask(NULL);
1121 }
1122 memcpy(maskp, addrp, sizeof(*maskp));
1123
1124 /* if a null mask is given, the name is ignored, like in "any/0" */
1125 if (maskp->s_addr == 0U)
1126 strcpy(buf, "0.0.0.0");
1127
1128 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1129 n = *naddrs;
1130 for (i = 0, j = 0; i < n; ++i) {
1131 addrp[j++].s_addr &= maskp->s_addr;
1132 for (k = 0; k < j - 1; ++k)
1133 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1134 --*naddrs;
1135 --j;
1136 break;
1137 }
1138 }
1139}
1140
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001141const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001142{
1143 /* 0000:0000:0000:0000:0000:000.000.000.000
1144 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1145 static char buf[50+1];
1146 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1147}
1148
1149static const char *ip6addr_to_host(const struct in6_addr *addr)
1150{
1151 static char hostname[NI_MAXHOST];
1152 struct sockaddr_in6 saddr;
1153 int err;
1154
1155 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1156 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1157 saddr.sin6_family = AF_INET6;
1158
1159 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1160 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1161 if (err != 0) {
1162#ifdef DEBUG
1163 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1164#endif
1165 return NULL;
1166 }
1167
1168#ifdef DEBUG
1169 fprintf (stderr, "\naddr2host: %s\n", hostname);
1170#endif
1171 return hostname;
1172}
1173
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001174const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001175{
1176 const char *name;
1177
1178 if ((name = ip6addr_to_host(addr)) != NULL)
1179 return name;
1180
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001181 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001182}
1183
1184static int ip6addr_prefix_length(const struct in6_addr *k)
1185{
1186 unsigned int bits = 0;
1187 uint32_t a, b, c, d;
1188
Jan Engelhardt48607812008-06-10 15:17:53 +02001189 a = ntohl(k->s6_addr32[0]);
1190 b = ntohl(k->s6_addr32[1]);
1191 c = ntohl(k->s6_addr32[2]);
1192 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001193 while (a & 0x80000000U) {
1194 ++bits;
1195 a <<= 1;
1196 a |= (b >> 31) & 1;
1197 b <<= 1;
1198 b |= (c >> 31) & 1;
1199 c <<= 1;
1200 c |= (d >> 31) & 1;
1201 d <<= 1;
1202 }
1203 if (a != 0 || b != 0 || c != 0 || d != 0)
1204 return -1;
1205 return bits;
1206}
1207
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001208const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001209{
1210 static char buf[50+2];
1211 int l = ip6addr_prefix_length(addrp);
1212
1213 if (l == -1) {
1214 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001215 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001216 return buf;
1217 }
1218 sprintf(buf, "/%d", l);
1219 return buf;
1220}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001221
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001222struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001223{
1224 static struct in6_addr ap;
1225 int err;
1226
1227 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1228 return &ap;
1229#ifdef DEBUG
1230 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1231#endif
1232 return NULL;
1233}
1234
1235static struct in6_addr *
1236host_to_ip6addr(const char *name, unsigned int *naddr)
1237{
1238 static struct in6_addr *addr;
1239 struct addrinfo hints;
1240 struct addrinfo *res;
1241 int err;
1242
1243 memset(&hints, 0, sizeof(hints));
1244 hints.ai_flags = AI_CANONNAME;
1245 hints.ai_family = AF_INET6;
1246 hints.ai_socktype = SOCK_RAW;
1247 hints.ai_protocol = IPPROTO_IPV6;
1248 hints.ai_next = NULL;
1249
1250 *naddr = 0;
1251 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1252#ifdef DEBUG
1253 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1254#endif
1255 return NULL;
1256 } else {
1257 if (res->ai_family != AF_INET6 ||
1258 res->ai_addrlen != sizeof(struct sockaddr_in6))
1259 return NULL;
1260
1261#ifdef DEBUG
1262 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1263 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1264#endif
1265 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001266 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001267 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1268 sizeof(struct in6_addr));
1269 freeaddrinfo(res);
1270 *naddr = 1;
1271 return addr;
1272 }
1273
1274 return NULL;
1275}
1276
1277static struct in6_addr *network_to_ip6addr(const char *name)
1278{
1279 /* abort();*/
1280 /* TODO: not implemented yet, but the exception breaks the
1281 * name resolvation */
1282 return NULL;
1283}
1284
1285static struct in6_addr *
1286ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1287{
1288 struct in6_addr *addrp, *addrptmp;
1289
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001290 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001291 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001292 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001293 memcpy(addrp, addrptmp, sizeof(*addrp));
1294 *naddrs = 1;
1295 return addrp;
1296 }
1297 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1298 return addrp;
1299
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001300 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001301}
1302
1303static struct in6_addr *parse_ip6mask(char *mask)
1304{
1305 static struct in6_addr maskaddr;
1306 struct in6_addr *addrp;
1307 unsigned int bits;
1308
1309 if (mask == NULL) {
1310 /* no mask at all defaults to 128 bits */
1311 memset(&maskaddr, 0xff, sizeof maskaddr);
1312 return &maskaddr;
1313 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001314 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001315 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001316 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001317 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001318 "invalid mask `%s' specified", mask);
1319 if (bits != 0) {
1320 char *p = (void *)&maskaddr;
1321 memset(p, 0xff, bits / 8);
1322 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1323 p[bits/8] = 0xff << (8 - (bits & 7));
1324 return &maskaddr;
1325 }
1326
1327 memset(&maskaddr, 0, sizeof(maskaddr));
1328 return &maskaddr;
1329}
1330
Jan Engelhardta0baae82009-01-30 04:32:50 +01001331void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1332 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001333{
1334 struct in6_addr *addrp;
1335 unsigned int i, j, k, n;
1336 char buf[256], *p;
1337
1338 strncpy(buf, name, sizeof(buf) - 1);
1339 buf[sizeof(buf)-1] = '\0';
1340 if ((p = strrchr(buf, '/')) != NULL) {
1341 *p = '\0';
1342 addrp = parse_ip6mask(p + 1);
1343 } else {
1344 addrp = parse_ip6mask(NULL);
1345 }
1346 memcpy(maskp, addrp, sizeof(*maskp));
1347
1348 /* if a null mask is given, the name is ignored, like in "any/0" */
1349 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1350 strcpy(buf, "::");
1351
1352 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1353 n = *naddrs;
1354 for (i = 0, j = 0; i < n; ++i) {
1355 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001356 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001357 ++j;
1358 for (k = 0; k < j - 1; ++k)
1359 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1360 --*naddrs;
1361 --j;
1362 break;
1363 }
1364 }
1365}
Max Kellermanna5d09942008-01-29 13:44:34 +00001366
Jan Engelhardta0baae82009-01-30 04:32:50 +01001367void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001368{
1369 static const char no_quote_chars[] = "_-0123456789"
1370 "abcdefghijklmnopqrstuvwxyz"
1371 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1372 static const char escape_chars[] = "\"\\'";
1373 size_t length;
1374 const char *p;
1375
1376 length = strcspn(value, no_quote_chars);
1377 if (length > 0 && value[length] == 0) {
1378 /* no quoting required */
1379 fputs(value, stdout);
1380 putchar(' ');
1381 } else {
1382 /* there is at least one dangerous character in the
1383 value, which we have to quote. Write double quotes
1384 around the value and escape special characters with
1385 a backslash */
1386 putchar('"');
1387
1388 for (p = strpbrk(value, escape_chars); p != NULL;
1389 p = strpbrk(value, escape_chars)) {
1390 if (p > value)
1391 fwrite(value, 1, p - value, stdout);
1392 putchar('\\');
1393 putchar(*p);
1394 value = p + 1;
1395 }
1396
1397 /* print the rest and finish the double quoted
1398 string */
1399 fputs(value, stdout);
1400 printf("\" ");
1401 }
1402}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001403
1404/**
1405 * Check for option-intrapositional negation.
1406 * Do not use in new code.
1407 */
1408int xtables_check_inverse(const char option[], int *invert,
1409 int *my_optind, int argc)
1410{
1411 if (option && strcmp(option, "!") == 0) {
1412 fprintf(stderr, "Using intrapositioned negation "
1413 "(`--option ! this`) is deprecated in favor of "
1414 "extrapositioned (`! --option this`).\n");
1415
1416 if (*invert)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001417 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001418 "Multiple `!' flags not allowed");
1419 *invert = true;
1420 if (my_optind != NULL) {
1421 ++*my_optind;
1422 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001423 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001424 "no argument following `!'");
1425 }
1426
1427 return true;
1428 }
1429 return false;
1430}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001431
1432const struct xtables_pprot xtables_chain_protos[] = {
1433 {"tcp", IPPROTO_TCP},
1434 {"sctp", IPPROTO_SCTP},
1435 {"udp", IPPROTO_UDP},
1436 {"udplite", IPPROTO_UDPLITE},
1437 {"icmp", IPPROTO_ICMP},
1438 {"icmpv6", IPPROTO_ICMPV6},
1439 {"ipv6-icmp", IPPROTO_ICMPV6},
1440 {"esp", IPPROTO_ESP},
1441 {"ah", IPPROTO_AH},
1442 {"ipv6-mh", IPPROTO_MH},
1443 {"mh", IPPROTO_MH},
1444 {"all", 0},
1445 {NULL},
1446};
1447
1448u_int16_t
1449xtables_parse_protocol(const char *s)
1450{
1451 unsigned int proto;
1452
1453 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1454 struct protoent *pent;
1455
1456 /* first deal with the special case of 'all' to prevent
1457 * people from being able to redefine 'all' in nsswitch
1458 * and/or provoke expensive [not working] ldap/nis/...
1459 * lookups */
1460 if (!strcmp(s, "all"))
1461 return 0;
1462
1463 if ((pent = getprotobyname(s)))
1464 proto = pent->p_proto;
1465 else {
1466 unsigned int i;
1467 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1468 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1469 proto = xtables_chain_protos[i].num;
1470 break;
1471 }
1472 }
1473 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001474 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001475 "unknown protocol `%s' specified",
1476 s);
1477 }
1478 }
1479
1480 return proto;
1481}