blob: 1ff6e82b19285b0cf43d6c9b6e91d7313a349554 [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000019#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000021#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000022#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000023#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000024#include <stdio.h>
25#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000026#include <string.h>
27#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000028#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000032#include <arpa/inet.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000034#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +010035#include <limits.h> /* INT_MAX in ip_tables.h/ip6_tables.h */
Jan Engelhardt77f48c22009-02-07 19:59:53 +010036#include <linux/netfilter_ipv4/ip_tables.h>
37#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020038#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000040#ifndef NO_SHARED_LIBS
41#include <dlfcn.h>
42#endif
Jan Engelhardtc31870f2009-02-10 10:48:28 +010043#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
44# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
45# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
46#endif
47#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
48# define IP6T_SO_GET_REVISION_MATCH 68
49# define IP6T_SO_GET_REVISION_TARGET 69
50#endif
Jamal Hadi Salim70581922009-02-13 08:36:44 -050051#include <getopt.h>
Jan Engelhardtc31870f2009-02-10 10:48:28 +010052
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000053
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000054#define NPROTO 255
55
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000056#ifndef PROC_SYS_MODPROBE
57#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
58#endif
59
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010060void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
61
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010062struct xtables_globals *xt_params = NULL;
63
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010064void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010065{
66 va_list args;
67
68 va_start(args, msg);
69 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
70 vfprintf(stderr, msg, args);
71 va_end(args);
72 fprintf(stderr, "\n");
73 exit(status);
74}
75
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010076
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -050077void xtables_free_opts(int reset_offset)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010078{
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -050079 if (xt_params->opts != xt_params->orig_opts) {
80 free(xt_params->opts);
81 xt_params->opts = xt_params->orig_opts;
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010082 if (reset_offset)
83 xt_params->option_offset = 0;
84 }
85}
86
Jamal Hadi Salim70581922009-02-13 08:36:44 -050087struct option *xtables_merge_options(struct option *oldopts,
88 const struct option *newopts,
89 unsigned int *option_offset)
90{
91 unsigned int num_old, num_new, i;
92 struct option *merge;
93
94 if (newopts == NULL)
95 return oldopts;
96
97 for (num_old = 0; oldopts[num_old].name; num_old++) ;
98 for (num_new = 0; newopts[num_new].name; num_new++) ;
99
Jan Engelhardtbddcb922009-02-21 02:48:11 +0100100 xt_params->option_offset += 256;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500101 *option_offset = xt_params->option_offset;
102
103 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
104 if (merge == NULL)
105 return NULL;
106 memcpy(merge, oldopts, num_old * sizeof(struct option));
107 xtables_free_opts(0); /* Release any old options merged */
108 for (i = 0; i < num_new; i++) {
109 merge[num_old + i] = newopts[i];
110 merge[num_old + i].val += *option_offset;
111 }
112 memset(merge + num_old + num_new, 0, sizeof(struct option));
113
114 return merge;
115}
116
Jamal Hadi Salim85332212009-02-12 09:33:59 -0500117void xtables_set_revision(char *name, u_int8_t revision)
118{
119 /* Old kernel sources don't have ".revision" field,
120 * but we stole a byte from name. */
121 name[XT_FUNCTION_MAXNAMELEN - 2] = '\0';
122 name[XT_FUNCTION_MAXNAMELEN - 1] = revision;
123}
124
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100125/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100126 * xtables_afinfo - protocol family dependent information
127 * @kmod: kernel module basename (e.g. "ip_tables")
128 * @libprefix: prefix of .so library name (e.g. "libipt_")
129 * @family: nfproto family
130 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
131 * @so_rev_match: optname to check revision support of match
132 * @so_rev_target: optname to check revision support of target
133 */
134struct xtables_afinfo {
135 const char *kmod;
136 const char *libprefix;
137 uint8_t family;
138 uint8_t ipproto;
139 int so_rev_match;
140 int so_rev_target;
141};
142
143static const struct xtables_afinfo afinfo_ipv4 = {
144 .kmod = "ip_tables",
145 .libprefix = "libipt_",
146 .family = NFPROTO_IPV4,
147 .ipproto = IPPROTO_IP,
148 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
149 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
150};
151
152static const struct xtables_afinfo afinfo_ipv6 = {
153 .kmod = "ip6_tables",
154 .libprefix = "libip6t_",
155 .family = NFPROTO_IPV6,
156 .ipproto = IPPROTO_IPV6,
157 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
158 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
159};
160
161static const struct xtables_afinfo *afinfo;
162
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100163/* Search path for Xtables .so files */
164static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000165
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000166/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100167const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000168
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000169/* Keeping track of external matches and targets: linked lists. */
170struct xtables_match *xtables_matches;
171struct xtables_target *xtables_targets;
172
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100173void xtables_init(void)
174{
175 xtables_libdir = getenv("XTABLES_LIBDIR");
176 if (xtables_libdir != NULL)
177 return;
178 xtables_libdir = getenv("IPTABLES_LIB_DIR");
179 if (xtables_libdir != NULL) {
180 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
181 "use XTABLES_LIBDIR.\n");
182 return;
183 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100184 /*
185 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
186 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
187 * for these env vars are deprecated anyhow, and in light of the
188 * (shared) libxt_*.so files, makes less sense to have
189 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
190 */
191 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
192 if (xtables_libdir != NULL) {
193 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
194 "use XTABLES_LIBDIR.\n");
195 return;
196 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100197 xtables_libdir = XTABLES_LIBDIR;
198}
199
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100200void xtables_set_nfproto(uint8_t nfproto)
201{
202 switch (nfproto) {
203 case NFPROTO_IPV4:
204 afinfo = &afinfo_ipv4;
205 break;
206 case NFPROTO_IPV6:
207 afinfo = &afinfo_ipv6;
208 break;
209 default:
210 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
211 __func__);
212 }
213}
214
Jan Engelhardt630ef482009-01-27 14:58:41 +0100215/**
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500216 * xtables_set_params - set the global parameters used by xtables
217 * @xtp: input xtables_globals structure
218 *
219 * The app is expected to pass a valid xtables_globals data-filled
220 * with proper values
221 * @xtp cannot be NULL
222 *
223 * Returns -1 on failure to set and 0 on success
224 */
225int xtables_set_params(struct xtables_globals *xtp)
226{
227 if (!xtp) {
228 fprintf(stderr, "%s: Illegal global params\n",__func__);
229 return -1;
230 }
231
232 xt_params = xtp;
233
234 if (!xt_params->exit_err)
235 xt_params->exit_err = basic_exit_err;
236
237 return 0;
238}
239
240int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto)
241{
242 xtables_init();
243 xtables_set_nfproto(nfproto);
244 return xtables_set_params(xtp);
245}
246
247/**
Jan Engelhardt630ef482009-01-27 14:58:41 +0100248 * xtables_*alloc - wrappers that exit on failure
249 */
250void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000251{
252 void *p;
253
254 if ((p = calloc(count, size)) == NULL) {
255 perror("ip[6]tables: calloc failed");
256 exit(1);
257 }
258
259 return p;
260}
261
Jan Engelhardt630ef482009-01-27 14:58:41 +0100262void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000263{
264 void *p;
265
266 if ((p = malloc(size)) == NULL) {
267 perror("ip[6]tables: malloc failed");
268 exit(1);
269 }
270
271 return p;
272}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000273
Michael Granzow332e4ac2009-04-09 18:24:36 +0100274void *xtables_realloc(void *ptr, size_t size)
275{
276 void *p;
277
278 if ((p = realloc(ptr, size)) == NULL) {
279 perror("ip[6]tables: realloc failed");
280 exit(1);
281 }
282
283 return p;
284}
285
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000286static char *get_modprobe(void)
287{
288 int procfile;
289 char *ret;
290
291#define PROCFILE_BUFSIZ 1024
292 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
293 if (procfile < 0)
294 return NULL;
295
296 ret = (char *) malloc(PROCFILE_BUFSIZ);
297 if (ret) {
298 memset(ret, 0, PROCFILE_BUFSIZ);
299 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
300 case -1: goto fail;
301 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
302 }
303 if (ret[strlen(ret)-1]=='\n')
304 ret[strlen(ret)-1]=0;
305 close(procfile);
306 return ret;
307 }
308 fail:
309 free(ret);
310 close(procfile);
311 return NULL;
312}
313
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100314int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000315{
316 char *buf = NULL;
317 char *argv[4];
318 int status;
319
320 /* If they don't explicitly set it, read out of kernel */
321 if (!modprobe) {
322 buf = get_modprobe();
323 if (!buf)
324 return -1;
325 modprobe = buf;
326 }
327
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100328 /*
329 * Need to flush the buffer, or the child may output it again
330 * when switching the program thru execv.
331 */
332 fflush(stdout);
333
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000334 switch (fork()) {
335 case 0:
336 argv[0] = (char *)modprobe;
337 argv[1] = (char *)modname;
338 if (quiet) {
339 argv[2] = "-q";
340 argv[3] = NULL;
341 } else {
342 argv[2] = NULL;
343 argv[3] = NULL;
344 }
345 execv(argv[0], argv);
346
347 /* not usually reached */
348 exit(1);
349 case -1:
350 return -1;
351
352 default: /* parent */
353 wait(&status);
354 }
355
356 free(buf);
357 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
358 return 0;
359 return -1;
360}
361
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100362int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000363{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100364 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000365 static int ret = -1;
366
367 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100368 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000369 loaded = (ret == 0);
370 }
371
372 return ret;
373}
374
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100375/**
376 * xtables_strtou{i,l} - string to number conversion
377 * @s: input string
378 * @end: like strtoul's "end" pointer
379 * @value: pointer for result
380 * @min: minimum accepted value
381 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000382 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100383 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
384 * "15a" is rejected.
385 * In either case, the value obtained is compared for min-max compliance.
386 * Base is always 0, i.e. autodetect depending on @s.
387 *
388 * Returns true/false whether number was accepted. On failure, *value has
389 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000390 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100391bool xtables_strtoul(const char *s, char **end, unsigned long *value,
392 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000393{
394 unsigned long v;
395 char *my_end;
396
397 errno = 0;
398 v = strtoul(s, &my_end, 0);
399
400 if (my_end == s)
401 return false;
402 if (end != NULL)
403 *end = my_end;
404
405 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
406 if (value != NULL)
407 *value = v;
408 if (end == NULL)
409 return *my_end == '\0';
410 return true;
411 }
412
413 return false;
414}
415
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100416bool xtables_strtoui(const char *s, char **end, unsigned int *value,
417 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000418{
419 unsigned long v;
420 bool ret;
421
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100422 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000423 if (value != NULL)
424 *value = v;
425 return ret;
426}
427
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100428int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000429{
430 struct servent *service;
431
432 if ((service = getservbyname(name, proto)) != NULL)
433 return ntohs((unsigned short) service->s_port);
434
435 return -1;
436}
437
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100438u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000439{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100440 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000441
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100442 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100443 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100444 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000445
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100446 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000447 "invalid port/service `%s' specified", port);
448}
449
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100450void xtables_parse_interface(const char *arg, char *vianame,
451 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000452{
453 int vialen = strlen(arg);
454 unsigned int i;
455
456 memset(mask, 0, IFNAMSIZ);
457 memset(vianame, 0, IFNAMSIZ);
458
459 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100460 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000461 "interface name `%s' must be shorter than IFNAMSIZ"
462 " (%i)", arg, IFNAMSIZ-1);
463
464 strcpy(vianame, arg);
465 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
466 memset(mask, 0, IFNAMSIZ);
467 else if (vianame[vialen - 1] == '+') {
468 memset(mask, 0xFF, vialen - 1);
469 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
470 /* Don't remove `+' here! -HW */
471 } else {
472 /* Include nul-terminator in match */
473 memset(mask, 0xFF, vialen + 1);
474 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
475 for (i = 0; vianame[i]; i++) {
476 if (vianame[i] == ':' ||
477 vianame[i] == '!' ||
478 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000479 fprintf(stderr,
480 "Warning: weird character in interface"
481 " `%s' (No aliases, :, ! or *).\n",
482 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000483 break;
484 }
485 }
486 }
487}
488
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200489#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100490static void *load_extension(const char *search_path, const char *prefix,
491 const char *name, bool is_target)
492{
493 const char *dir = search_path, *next;
494 void *ptr = NULL;
495 struct stat sb;
496 char path[256];
497
498 do {
499 next = strchr(dir, ':');
500 if (next == NULL)
501 next = dir + strlen(dir);
502 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200503 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100504
505 if (dlopen(path, RTLD_NOW) != NULL) {
506 /* Found library. If it didn't register itself,
507 maybe they specified target as match. */
508 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100509 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100510 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100511 ptr = xtables_find_match(name,
512 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100513 } else if (stat(path, &sb) == 0) {
514 fprintf(stderr, "%s: %s\n", path, dlerror());
515 }
516
517 if (ptr != NULL)
518 return ptr;
519
520 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200521 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100522 if (dlopen(path, RTLD_NOW) != NULL) {
523 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100524 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100525 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100526 ptr = xtables_find_match(name,
527 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100528 } else if (stat(path, &sb) == 0) {
529 fprintf(stderr, "%s: %s\n", path, dlerror());
530 }
531
532 if (ptr != NULL)
533 return ptr;
534
535 dir = next + 1;
536 } while (*next != '\0');
537
538 return NULL;
539}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200540#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100541
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100542struct xtables_match *
543xtables_find_match(const char *name, enum xtables_tryload tryload,
544 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000545{
546 struct xtables_match *ptr;
547 const char *icmp6 = "icmp6";
548
549 /* This is ugly as hell. Nonetheless, there is no way of changing
550 * this without hurting backwards compatibility */
551 if ( (strcmp(name,"icmpv6") == 0) ||
552 (strcmp(name,"ipv6-icmp") == 0) ||
553 (strcmp(name,"icmp6") == 0) )
554 name = icmp6;
555
556 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
557 if (strcmp(name, ptr->name) == 0) {
558 struct xtables_match *clone;
559
560 /* First match of this type: */
561 if (ptr->m == NULL)
562 break;
563
564 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100565 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000566 memcpy(clone, ptr, sizeof(struct xtables_match));
567 clone->mflags = 0;
568 /* This is a clone: */
569 clone->next = clone;
570
571 ptr = clone;
572 break;
573 }
574 }
575
576#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100577 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100578 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100579 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000580
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100581 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100582 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000583 "Couldn't load match `%s':%s\n",
584 name, dlerror());
585 }
586#else
587 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100588 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000589 ptr->loaded = 1;
590 else
591 ptr = NULL;
592 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100593 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100594 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000595 "Couldn't find match `%s'\n", name);
596 }
597#endif
598
599 if (ptr && matches) {
600 struct xtables_rule_match **i;
601 struct xtables_rule_match *newentry;
602
Jan Engelhardt630ef482009-01-27 14:58:41 +0100603 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000604
605 for (i = matches; *i; i = &(*i)->next) {
606 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100607 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000608 }
609 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100610 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000611 newentry->next = NULL;
612 *i = newentry;
613 }
614
615 return ptr;
616}
617
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100618struct xtables_target *
619xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000620{
621 struct xtables_target *ptr;
622
623 /* Standard target? */
624 if (strcmp(name, "") == 0
625 || strcmp(name, XTC_LABEL_ACCEPT) == 0
626 || strcmp(name, XTC_LABEL_DROP) == 0
627 || strcmp(name, XTC_LABEL_QUEUE) == 0
628 || strcmp(name, XTC_LABEL_RETURN) == 0)
629 name = "standard";
630
631 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
632 if (strcmp(name, ptr->name) == 0)
633 break;
634 }
635
636#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100637 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100638 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100639 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000640
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100641 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100642 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000643 "Couldn't load target `%s':%s\n",
644 name, dlerror());
645 }
646#else
647 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100648 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000649 ptr->loaded = 1;
650 else
651 ptr = NULL;
652 }
Peter Volkov854d2d92009-03-24 11:09:16 +0300653 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100654 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000655 "Couldn't find target `%s'\n", name);
656 }
657#endif
658
659 if (ptr)
660 ptr->used = 1;
661
662 return ptr;
663}
664
665static int compatible_revision(const char *name, u_int8_t revision, int opt)
666{
667 struct xt_get_revision rev;
668 socklen_t s = sizeof(rev);
669 int max_rev, sockfd;
670
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100671 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000672 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000673 if (errno == EPERM) {
674 /* revision 0 is always supported. */
675 if (revision != 0)
676 fprintf(stderr, "Could not determine whether "
677 "revision %u is supported, "
678 "assuming it is.\n",
679 revision);
680 return 1;
681 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000682 fprintf(stderr, "Could not open socket to kernel: %s\n",
683 strerror(errno));
684 exit(1);
685 }
686
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100687 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000688
689 strcpy(rev.name, name);
690 rev.revision = revision;
691
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100692 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000693 if (max_rev < 0) {
694 /* Definitely don't support this? */
695 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
696 close(sockfd);
697 return 0;
698 } else if (errno == ENOPROTOOPT) {
699 close(sockfd);
700 /* Assume only revision 0 support (old kernel) */
701 return (revision == 0);
702 } else {
703 fprintf(stderr, "getsockopt failed strangely: %s\n",
704 strerror(errno));
705 exit(1);
706 }
707 }
708 close(sockfd);
709 return 1;
710}
711
712
713static int compatible_match_revision(const char *name, u_int8_t revision)
714{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100715 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000716}
717
718static int compatible_target_revision(const char *name, u_int8_t revision)
719{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100720 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000721}
722
723void xtables_register_match(struct xtables_match *me)
724{
725 struct xtables_match **i, *old;
726
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100727 if (strcmp(me->version, XTABLES_VERSION) != 0) {
728 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
729 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500730 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100731 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000732 exit(1);
733 }
734
735 /* Revision field stole a char from name. */
736 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
737 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500738 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000739 exit(1);
740 }
741
742 if (me->family >= NPROTO) {
743 fprintf(stderr,
744 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500745 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000746 exit(1);
747 }
748
749 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100750 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000751 return;
752
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100753 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000754 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100755 if (old->revision == me->revision &&
756 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000757 fprintf(stderr,
758 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500759 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000760 exit(1);
761 }
762
763 /* Now we have two (or more) options, check compatibility. */
764 if (compatible_match_revision(old->name, old->revision)
765 && old->revision > me->revision)
766 return;
767
Jan Engelhardt23545c22008-02-14 04:23:04 +0100768 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000769 if (!compatible_match_revision(me->name, me->revision))
770 return;
771
Jan Engelhardt23545c22008-02-14 04:23:04 +0100772 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
773 if (old->revision == me->revision && me->family == AF_UNSPEC)
774 return;
775
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000776 /* Delete old one. */
777 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
778 *i = old->next;
779 }
780
781 if (me->size != XT_ALIGN(me->size)) {
782 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500783 xt_params->program_name, me->name,
784 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000785 exit(1);
786 }
787
788 /* Append to list. */
789 for (i = &xtables_matches; *i; i = &(*i)->next);
790 me->next = NULL;
791 *i = me;
792
793 me->m = NULL;
794 me->mflags = 0;
795}
796
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200797void xtables_register_matches(struct xtables_match *match, unsigned int n)
798{
799 do {
800 xtables_register_match(&match[--n]);
801 } while (n > 0);
802}
803
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000804void xtables_register_target(struct xtables_target *me)
805{
806 struct xtables_target *old;
807
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100808 if (strcmp(me->version, XTABLES_VERSION) != 0) {
809 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
810 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500811 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100812 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000813 exit(1);
814 }
815
816 /* Revision field stole a char from name. */
817 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
818 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500819 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000820 exit(1);
821 }
822
823 if (me->family >= NPROTO) {
824 fprintf(stderr,
825 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500826 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000827 exit(1);
828 }
829
830 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100831 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000832 return;
833
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100834 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000835 if (old) {
836 struct xtables_target **i;
837
Jan Engelhardt23545c22008-02-14 04:23:04 +0100838 if (old->revision == me->revision &&
839 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000840 fprintf(stderr,
841 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500842 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000843 exit(1);
844 }
845
846 /* Now we have two (or more) options, check compatibility. */
847 if (compatible_target_revision(old->name, old->revision)
848 && old->revision > me->revision)
849 return;
850
Jan Engelhardt23545c22008-02-14 04:23:04 +0100851 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000852 if (!compatible_target_revision(me->name, me->revision))
853 return;
854
Jan Engelhardt23545c22008-02-14 04:23:04 +0100855 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
856 if (old->revision == me->revision && me->family == AF_UNSPEC)
857 return;
858
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000859 /* Delete old one. */
860 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
861 *i = old->next;
862 }
863
864 if (me->size != XT_ALIGN(me->size)) {
865 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500866 xt_params->program_name, me->name,
867 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000868 exit(1);
869 }
870
871 /* Prepend to list. */
872 me->next = xtables_targets;
873 xtables_targets = me;
874 me->t = NULL;
875 me->tflags = 0;
876}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000877
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200878void xtables_register_targets(struct xtables_target *target, unsigned int n)
879{
880 do {
881 xtables_register_target(&target[--n]);
882 } while (n > 0);
883}
884
Jan Engelhardta41545c2009-01-27 21:27:19 +0100885/**
886 * xtables_param_act - act on condition
887 * @status: a constant from enum xtables_exittype
888 *
889 * %XTF_ONLY_ONCE: print error message that option may only be used once.
890 * @p1: module name (e.g. "mark")
891 * @p2(...): option in conflict (e.g. "--mark")
892 * @p3(...): condition to match on (see extensions/ for examples)
893 *
894 * %XTF_NO_INVERT: option does not support inversion
895 * @p1: module name
896 * @p2: option in conflict
897 * @p3: condition to match on
898 *
899 * %XTF_BAD_VALUE: bad value for option
900 * @p1: module name
901 * @p2: option with which the problem occured (e.g. "--mark")
902 * @p3: string the user passed in (e.g. "99999999999999")
903 *
904 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
905 * @p1: module name
906 *
907 * Displays an error message and exits the program.
908 */
909void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000910{
911 const char *p2, *p3;
912 va_list args;
913 bool b;
914
915 va_start(args, p1);
916
917 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100918 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000919 p2 = va_arg(args, const char *);
920 b = va_arg(args, unsigned int);
921 if (!b)
922 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100923 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000924 "%s: \"%s\" option may only be specified once",
925 p1, p2);
926 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100927 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000928 p2 = va_arg(args, const char *);
929 b = va_arg(args, unsigned int);
930 if (!b)
931 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100932 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000933 "%s: \"%s\" option cannot be inverted", p1, p2);
934 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100935 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000936 p2 = va_arg(args, const char *);
937 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100938 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000939 "%s: Bad value for \"%s\" option: \"%s\"",
940 p1, p2, p3);
941 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100942 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000943 b = va_arg(args, unsigned int);
944 if (!b)
945 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100946 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000947 "%s: At most one action is possible", p1);
948 break;
949 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100950 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000951 break;
952 }
953
954 va_end(args);
955}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000956
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100957const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000958{
959 static char buf[20];
960 const unsigned char *bytep = (const void *)&addrp->s_addr;
961
962 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
963 return buf;
964}
965
966static const char *ipaddr_to_host(const struct in_addr *addr)
967{
968 struct hostent *host;
969
970 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
971 if (host == NULL)
972 return NULL;
973
974 return host->h_name;
975}
976
977static const char *ipaddr_to_network(const struct in_addr *addr)
978{
979 struct netent *net;
980
981 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
982 return net->n_name;
983
984 return NULL;
985}
986
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100987const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000988{
989 const char *name;
990
991 if ((name = ipaddr_to_host(addr)) != NULL ||
992 (name = ipaddr_to_network(addr)) != NULL)
993 return name;
994
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100995 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000996}
997
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100998const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000999{
1000 static char buf[20];
1001 uint32_t maskaddr, bits;
1002 int i;
1003
1004 maskaddr = ntohl(mask->s_addr);
1005
1006 if (maskaddr == 0xFFFFFFFFL)
1007 /* we don't want to see "/32" */
1008 return "";
1009
1010 i = 32;
1011 bits = 0xFFFFFFFEL;
1012 while (--i >= 0 && maskaddr != bits)
1013 bits <<= 1;
1014 if (i >= 0)
1015 sprintf(buf, "/%d", i);
1016 else
1017 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001018 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001019
1020 return buf;
1021}
1022
Jan Engelhardtbd943842008-01-20 13:38:08 +00001023static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1024{
1025 static struct in_addr addr;
1026 unsigned char *addrp;
1027 unsigned int onebyte;
1028 char buf[20], *p, *q;
1029 int i;
1030
1031 /* copy dotted string, because we need to modify it */
1032 strncpy(buf, dotted, sizeof(buf) - 1);
1033 buf[sizeof(buf) - 1] = '\0';
1034 addrp = (void *)&addr.s_addr;
1035
1036 p = buf;
1037 for (i = 0; i < 3; ++i) {
1038 if ((q = strchr(p, '.')) == NULL) {
1039 if (is_mask)
1040 return NULL;
1041
1042 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001043 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001044 return NULL;
1045
1046 addrp[i] = onebyte;
1047 while (i < 3)
1048 addrp[++i] = 0;
1049
1050 return &addr;
1051 }
1052
1053 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001054 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001055 return NULL;
1056
1057 addrp[i] = onebyte;
1058 p = q + 1;
1059 }
1060
1061 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001062 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001063 return NULL;
1064
1065 addrp[3] = onebyte;
1066 return &addr;
1067}
1068
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001069struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001070{
1071 return __numeric_to_ipaddr(dotted, false);
1072}
1073
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001074struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001075{
1076 return __numeric_to_ipaddr(dotted, true);
1077}
1078
1079static struct in_addr *network_to_ipaddr(const char *name)
1080{
1081 static struct in_addr addr;
1082 struct netent *net;
1083
1084 if ((net = getnetbyname(name)) != NULL) {
1085 if (net->n_addrtype != AF_INET)
1086 return NULL;
1087 addr.s_addr = htonl(net->n_net);
1088 return &addr;
1089 }
1090
1091 return NULL;
1092}
1093
1094static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1095{
1096 struct hostent *host;
1097 struct in_addr *addr;
1098 unsigned int i;
1099
1100 *naddr = 0;
1101 if ((host = gethostbyname(name)) != NULL) {
1102 if (host->h_addrtype != AF_INET ||
1103 host->h_length != sizeof(struct in_addr))
1104 return NULL;
1105
1106 while (host->h_addr_list[*naddr] != NULL)
1107 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001108 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001109 for (i = 0; i < *naddr; i++)
1110 memcpy(&addr[i], host->h_addr_list[i],
1111 sizeof(struct in_addr));
1112 return addr;
1113 }
1114
1115 return NULL;
1116}
1117
1118static struct in_addr *
1119ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1120{
1121 struct in_addr *addrptmp, *addrp;
1122
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001123 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001124 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001125 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001126 memcpy(addrp, addrptmp, sizeof(*addrp));
1127 *naddrs = 1;
1128 return addrp;
1129 }
1130 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1131 return addrptmp;
1132
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001133 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001134}
1135
1136static struct in_addr *parse_ipmask(const char *mask)
1137{
1138 static struct in_addr maskaddr;
1139 struct in_addr *addrp;
1140 unsigned int bits;
1141
1142 if (mask == NULL) {
1143 /* no mask at all defaults to 32 bits */
1144 maskaddr.s_addr = 0xFFFFFFFF;
1145 return &maskaddr;
1146 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001147 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001148 /* dotted_to_addr already returns a network byte order addr */
1149 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001150 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001151 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001152 "invalid mask `%s' specified", mask);
1153 if (bits != 0) {
1154 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1155 return &maskaddr;
1156 }
1157
1158 maskaddr.s_addr = 0U;
1159 return &maskaddr;
1160}
1161
Michael Granzow332e4ac2009-04-09 18:24:36 +01001162void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp,
1163 struct in_addr **maskpp, unsigned int *naddrs)
1164{
1165 struct in_addr *addrp;
1166 char buf[256], *p;
1167 unsigned int len, i, j, n, count = 1;
1168 const char *loop = name;
1169
1170 while ((loop = strchr(loop, ',')) != NULL) {
1171 ++count;
1172 ++loop; /* skip ',' */
1173 }
1174
1175 *addrpp = xtables_malloc(sizeof(struct in_addr) * count);
1176 *maskpp = xtables_malloc(sizeof(struct in_addr) * count);
1177
1178 loop = name;
1179
1180 for (i = 0; i < count; ++i) {
1181 if (loop == NULL)
1182 break;
1183 if (*loop == ',')
1184 ++loop;
1185 if (*loop == '\0')
1186 break;
1187 p = strchr(loop, ',');
1188 if (p != NULL)
1189 len = p - loop;
1190 else
1191 len = strlen(loop);
1192 if (len == 0 || sizeof(buf) - 1 < len)
1193 break;
1194
1195 strncpy(buf, loop, len);
1196 buf[len] = '\0';
1197 loop += len;
1198 if ((p = strrchr(buf, '/')) != NULL) {
1199 *p = '\0';
1200 addrp = parse_ipmask(p + 1);
1201 } else {
1202 addrp = parse_ipmask(NULL);
1203 }
1204 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1205
1206 /* if a null mask is given, the name is ignored, like in "any/0" */
1207 if ((*maskpp + i)->s_addr == 0)
1208 /*
1209 * A bit pointless to process multiple addresses
1210 * in this case...
1211 */
1212 strcpy(buf, "0.0.0.0");
1213
1214 addrp = ipparse_hostnetwork(buf, &n);
1215 if (n > 1) {
1216 count += n - 1;
1217 *addrpp = xtables_realloc(*addrpp,
1218 sizeof(struct in_addr) * count);
1219 *maskpp = xtables_realloc(*maskpp,
1220 sizeof(struct in_addr) * count);
1221 for (j = 0; j < n; ++j)
1222 /* for each new addr */
1223 memcpy(*addrpp + i + j, addrp + j,
1224 sizeof(*addrp));
1225 for (j = 1; j < n; ++j)
1226 /* for each new mask */
1227 memcpy(*maskpp + i + j, *maskpp + i,
1228 sizeof(*addrp));
1229 i += n - 1;
1230 } else {
1231 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1232 }
1233 /* free what ipparse_hostnetwork had allocated: */
1234 free(addrp);
1235 }
1236 *naddrs = count;
1237 for (i = 0; i < n; ++i)
1238 (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr;
1239}
1240
1241
Jan Engelhardta0baae82009-01-30 04:32:50 +01001242/**
1243 * xtables_ipparse_any - transform arbitrary name to in_addr
1244 *
1245 * Possible inputs (pseudo regex):
1246 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1247 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1248 */
1249void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1250 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001251{
1252 unsigned int i, j, k, n;
1253 struct in_addr *addrp;
1254 char buf[256], *p;
1255
1256 strncpy(buf, name, sizeof(buf) - 1);
1257 buf[sizeof(buf) - 1] = '\0';
1258 if ((p = strrchr(buf, '/')) != NULL) {
1259 *p = '\0';
1260 addrp = parse_ipmask(p + 1);
1261 } else {
1262 addrp = parse_ipmask(NULL);
1263 }
1264 memcpy(maskp, addrp, sizeof(*maskp));
1265
1266 /* if a null mask is given, the name is ignored, like in "any/0" */
1267 if (maskp->s_addr == 0U)
1268 strcpy(buf, "0.0.0.0");
1269
1270 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1271 n = *naddrs;
1272 for (i = 0, j = 0; i < n; ++i) {
1273 addrp[j++].s_addr &= maskp->s_addr;
1274 for (k = 0; k < j - 1; ++k)
1275 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1276 --*naddrs;
1277 --j;
1278 break;
1279 }
1280 }
1281}
1282
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001283const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001284{
1285 /* 0000:0000:0000:0000:0000:000.000.000.000
1286 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1287 static char buf[50+1];
1288 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1289}
1290
1291static const char *ip6addr_to_host(const struct in6_addr *addr)
1292{
1293 static char hostname[NI_MAXHOST];
1294 struct sockaddr_in6 saddr;
1295 int err;
1296
1297 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1298 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1299 saddr.sin6_family = AF_INET6;
1300
1301 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1302 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1303 if (err != 0) {
1304#ifdef DEBUG
1305 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1306#endif
1307 return NULL;
1308 }
1309
1310#ifdef DEBUG
1311 fprintf (stderr, "\naddr2host: %s\n", hostname);
1312#endif
1313 return hostname;
1314}
1315
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001316const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001317{
1318 const char *name;
1319
1320 if ((name = ip6addr_to_host(addr)) != NULL)
1321 return name;
1322
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001323 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001324}
1325
1326static int ip6addr_prefix_length(const struct in6_addr *k)
1327{
1328 unsigned int bits = 0;
1329 uint32_t a, b, c, d;
1330
Jan Engelhardt48607812008-06-10 15:17:53 +02001331 a = ntohl(k->s6_addr32[0]);
1332 b = ntohl(k->s6_addr32[1]);
1333 c = ntohl(k->s6_addr32[2]);
1334 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001335 while (a & 0x80000000U) {
1336 ++bits;
1337 a <<= 1;
1338 a |= (b >> 31) & 1;
1339 b <<= 1;
1340 b |= (c >> 31) & 1;
1341 c <<= 1;
1342 c |= (d >> 31) & 1;
1343 d <<= 1;
1344 }
1345 if (a != 0 || b != 0 || c != 0 || d != 0)
1346 return -1;
1347 return bits;
1348}
1349
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001350const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001351{
1352 static char buf[50+2];
1353 int l = ip6addr_prefix_length(addrp);
1354
1355 if (l == -1) {
1356 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001357 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001358 return buf;
1359 }
1360 sprintf(buf, "/%d", l);
1361 return buf;
1362}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001363
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001364struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001365{
1366 static struct in6_addr ap;
1367 int err;
1368
1369 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1370 return &ap;
1371#ifdef DEBUG
1372 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1373#endif
1374 return NULL;
1375}
1376
1377static struct in6_addr *
1378host_to_ip6addr(const char *name, unsigned int *naddr)
1379{
1380 static struct in6_addr *addr;
1381 struct addrinfo hints;
1382 struct addrinfo *res;
1383 int err;
1384
1385 memset(&hints, 0, sizeof(hints));
1386 hints.ai_flags = AI_CANONNAME;
1387 hints.ai_family = AF_INET6;
1388 hints.ai_socktype = SOCK_RAW;
1389 hints.ai_protocol = IPPROTO_IPV6;
1390 hints.ai_next = NULL;
1391
1392 *naddr = 0;
1393 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1394#ifdef DEBUG
1395 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1396#endif
1397 return NULL;
1398 } else {
1399 if (res->ai_family != AF_INET6 ||
1400 res->ai_addrlen != sizeof(struct sockaddr_in6))
1401 return NULL;
1402
1403#ifdef DEBUG
1404 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1405 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1406#endif
1407 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001408 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001409 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1410 sizeof(struct in6_addr));
1411 freeaddrinfo(res);
1412 *naddr = 1;
1413 return addr;
1414 }
1415
1416 return NULL;
1417}
1418
1419static struct in6_addr *network_to_ip6addr(const char *name)
1420{
1421 /* abort();*/
1422 /* TODO: not implemented yet, but the exception breaks the
1423 * name resolvation */
1424 return NULL;
1425}
1426
1427static struct in6_addr *
1428ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1429{
1430 struct in6_addr *addrp, *addrptmp;
1431
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001432 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001433 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001434 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001435 memcpy(addrp, addrptmp, sizeof(*addrp));
1436 *naddrs = 1;
1437 return addrp;
1438 }
1439 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1440 return addrp;
1441
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001442 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001443}
1444
1445static struct in6_addr *parse_ip6mask(char *mask)
1446{
1447 static struct in6_addr maskaddr;
1448 struct in6_addr *addrp;
1449 unsigned int bits;
1450
1451 if (mask == NULL) {
1452 /* no mask at all defaults to 128 bits */
1453 memset(&maskaddr, 0xff, sizeof maskaddr);
1454 return &maskaddr;
1455 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001456 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001457 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001458 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001459 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001460 "invalid mask `%s' specified", mask);
1461 if (bits != 0) {
1462 char *p = (void *)&maskaddr;
1463 memset(p, 0xff, bits / 8);
1464 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1465 p[bits/8] = 0xff << (8 - (bits & 7));
1466 return &maskaddr;
1467 }
1468
1469 memset(&maskaddr, 0, sizeof(maskaddr));
1470 return &maskaddr;
1471}
1472
Michael Granzow332e4ac2009-04-09 18:24:36 +01001473void
1474xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp,
1475 struct in6_addr **maskpp, unsigned int *naddrs)
1476{
1477 struct in6_addr *addrp;
1478 char buf[256], *p;
1479 unsigned int len, i, j, n, count = 1;
1480 const char *loop = name;
1481
1482 while ((loop = strchr(loop, ',')) != NULL) {
1483 ++count;
1484 ++loop; /* skip ',' */
1485 }
1486
1487 *addrpp = xtables_malloc(sizeof(struct in6_addr) * count);
1488 *maskpp = xtables_malloc(sizeof(struct in6_addr) * count);
1489
1490 loop = name;
1491
1492 for (i = 0; i < count /*NB: count can grow*/; ++i) {
1493 if (loop == NULL)
1494 break;
1495 if (*loop == ',')
1496 ++loop;
1497 if (*loop == '\0')
1498 break;
1499 p = strchr(loop, ',');
1500 if (p != NULL)
1501 len = p - loop;
1502 else
1503 len = strlen(loop);
1504 if (len == 0 || sizeof(buf) - 1 < len)
1505 break;
1506
1507 strncpy(buf, loop, len);
1508 buf[len] = '\0';
1509 loop += len;
1510 if ((p = strrchr(buf, '/')) != NULL) {
1511 *p = '\0';
1512 addrp = parse_ip6mask(p + 1);
1513 } else {
1514 addrp = parse_ip6mask(NULL);
1515 }
1516 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1517
1518 /* if a null mask is given, the name is ignored, like in "any/0" */
1519 if (memcmp(*maskpp + i, &in6addr_any, sizeof(in6addr_any)) == 0)
1520 strcpy(buf, "::");
1521
1522 addrp = ip6parse_hostnetwork(buf, &n);
1523 /* ip6parse_hostnetwork only ever returns one IP
1524 address (it exits if the resolution fails).
1525 Therefore, n will always be 1 here. Leaving the
1526 code below in anyway in case ip6parse_hostnetwork
1527 is improved some day to behave like
1528 ipparse_hostnetwork: */
1529 if (n > 1) {
1530 count += n - 1;
1531 *addrpp = xtables_realloc(*addrpp,
1532 sizeof(struct in6_addr) * count);
1533 *maskpp = xtables_realloc(*maskpp,
1534 sizeof(struct in6_addr) * count);
1535 for (j = 0; j < n; ++j)
1536 /* for each new addr */
1537 memcpy(*addrpp + i + j, addrp + j,
1538 sizeof(*addrp));
1539 for (j = 1; j < n; ++j)
1540 /* for each new mask */
1541 memcpy(*maskpp + i + j, *maskpp + i,
1542 sizeof(*addrp));
1543 i += n - 1;
1544 } else {
1545 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1546 }
1547 /* free what ip6parse_hostnetwork had allocated: */
1548 free(addrp);
1549 }
1550 *naddrs = count;
1551 for (i = 0; i < n; ++i)
1552 for (j = 0; j < 4; ++j)
1553 (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j];
1554}
1555
Jan Engelhardta0baae82009-01-30 04:32:50 +01001556void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1557 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001558{
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001559 static const struct in6_addr zero_addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001560 struct in6_addr *addrp;
1561 unsigned int i, j, k, n;
1562 char buf[256], *p;
1563
1564 strncpy(buf, name, sizeof(buf) - 1);
1565 buf[sizeof(buf)-1] = '\0';
1566 if ((p = strrchr(buf, '/')) != NULL) {
1567 *p = '\0';
1568 addrp = parse_ip6mask(p + 1);
1569 } else {
1570 addrp = parse_ip6mask(NULL);
1571 }
1572 memcpy(maskp, addrp, sizeof(*maskp));
1573
1574 /* if a null mask is given, the name is ignored, like in "any/0" */
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001575 if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001576 strcpy(buf, "::");
1577
1578 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1579 n = *naddrs;
1580 for (i = 0, j = 0; i < n; ++i) {
1581 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001582 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001583 ++j;
1584 for (k = 0; k < j - 1; ++k)
1585 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1586 --*naddrs;
1587 --j;
1588 break;
1589 }
1590 }
1591}
Max Kellermanna5d09942008-01-29 13:44:34 +00001592
Jan Engelhardta0baae82009-01-30 04:32:50 +01001593void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001594{
1595 static const char no_quote_chars[] = "_-0123456789"
1596 "abcdefghijklmnopqrstuvwxyz"
1597 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1598 static const char escape_chars[] = "\"\\'";
1599 size_t length;
1600 const char *p;
1601
1602 length = strcspn(value, no_quote_chars);
1603 if (length > 0 && value[length] == 0) {
1604 /* no quoting required */
1605 fputs(value, stdout);
1606 putchar(' ');
1607 } else {
1608 /* there is at least one dangerous character in the
1609 value, which we have to quote. Write double quotes
1610 around the value and escape special characters with
1611 a backslash */
1612 putchar('"');
1613
1614 for (p = strpbrk(value, escape_chars); p != NULL;
1615 p = strpbrk(value, escape_chars)) {
1616 if (p > value)
1617 fwrite(value, 1, p - value, stdout);
1618 putchar('\\');
1619 putchar(*p);
1620 value = p + 1;
1621 }
1622
1623 /* print the rest and finish the double quoted
1624 string */
1625 fputs(value, stdout);
1626 printf("\" ");
1627 }
1628}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001629
1630/**
1631 * Check for option-intrapositional negation.
1632 * Do not use in new code.
1633 */
1634int xtables_check_inverse(const char option[], int *invert,
1635 int *my_optind, int argc)
1636{
1637 if (option && strcmp(option, "!") == 0) {
1638 fprintf(stderr, "Using intrapositioned negation "
1639 "(`--option ! this`) is deprecated in favor of "
1640 "extrapositioned (`! --option this`).\n");
1641
1642 if (*invert)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001643 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001644 "Multiple `!' flags not allowed");
1645 *invert = true;
1646 if (my_optind != NULL) {
1647 ++*my_optind;
1648 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001649 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001650 "no argument following `!'");
1651 }
1652
1653 return true;
1654 }
1655 return false;
1656}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001657
1658const struct xtables_pprot xtables_chain_protos[] = {
1659 {"tcp", IPPROTO_TCP},
1660 {"sctp", IPPROTO_SCTP},
1661 {"udp", IPPROTO_UDP},
1662 {"udplite", IPPROTO_UDPLITE},
1663 {"icmp", IPPROTO_ICMP},
1664 {"icmpv6", IPPROTO_ICMPV6},
1665 {"ipv6-icmp", IPPROTO_ICMPV6},
1666 {"esp", IPPROTO_ESP},
1667 {"ah", IPPROTO_AH},
1668 {"ipv6-mh", IPPROTO_MH},
1669 {"mh", IPPROTO_MH},
1670 {"all", 0},
1671 {NULL},
1672};
1673
1674u_int16_t
1675xtables_parse_protocol(const char *s)
1676{
1677 unsigned int proto;
1678
1679 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1680 struct protoent *pent;
1681
1682 /* first deal with the special case of 'all' to prevent
1683 * people from being able to redefine 'all' in nsswitch
1684 * and/or provoke expensive [not working] ldap/nis/...
1685 * lookups */
1686 if (!strcmp(s, "all"))
1687 return 0;
1688
1689 if ((pent = getprotobyname(s)))
1690 proto = pent->p_proto;
1691 else {
1692 unsigned int i;
1693 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
Pablo Neira Ayusoe55cc4a2009-05-12 09:51:26 +02001694 if (xtables_chain_protos[i].name == NULL)
1695 continue;
1696
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001697 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1698 proto = xtables_chain_protos[i].num;
1699 break;
1700 }
1701 }
1702 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001703 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001704 "unknown protocol `%s' specified",
1705 s);
1706 }
1707 }
1708
1709 return proto;
1710}