blob: f3baf84d2fd557ce63914bea823c04e1d0db92d5 [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
Jan Engelhardt94aa2ea2009-10-11 03:56:18 -0400334 switch (vfork()) {
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000335 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{
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100453 unsigned int vialen = strlen(arg);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000454 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);
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100465 if (vialen == 0)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000466 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++) {
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100476 if (vianame[i] == '/' ||
477 vianame[i] == ' ') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000478 fprintf(stderr,
479 "Warning: weird character in interface"
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100480 " `%s' ('/' and ' ' are not allowed by the kernel).\n",
Max Kellermannaae4f822007-10-17 16:36:49 +0000481 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000482 break;
483 }
484 }
485 }
486}
487
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200488#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100489static void *load_extension(const char *search_path, const char *prefix,
490 const char *name, bool is_target)
491{
492 const char *dir = search_path, *next;
493 void *ptr = NULL;
494 struct stat sb;
495 char path[256];
496
497 do {
498 next = strchr(dir, ':');
499 if (next == NULL)
500 next = dir + strlen(dir);
501 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200502 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100503
504 if (dlopen(path, RTLD_NOW) != NULL) {
505 /* Found library. If it didn't register itself,
506 maybe they specified target as match. */
507 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100508 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100509 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100510 ptr = xtables_find_match(name,
511 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100512 } else if (stat(path, &sb) == 0) {
513 fprintf(stderr, "%s: %s\n", path, dlerror());
514 }
515
516 if (ptr != NULL)
517 return ptr;
518
519 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200520 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100521 if (dlopen(path, RTLD_NOW) != NULL) {
522 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100523 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100524 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100525 ptr = xtables_find_match(name,
526 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100527 } else if (stat(path, &sb) == 0) {
528 fprintf(stderr, "%s: %s\n", path, dlerror());
529 }
530
531 if (ptr != NULL)
532 return ptr;
533
534 dir = next + 1;
535 } while (*next != '\0');
536
537 return NULL;
538}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200539#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100540
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100541struct xtables_match *
542xtables_find_match(const char *name, enum xtables_tryload tryload,
543 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000544{
545 struct xtables_match *ptr;
546 const char *icmp6 = "icmp6";
547
548 /* This is ugly as hell. Nonetheless, there is no way of changing
549 * this without hurting backwards compatibility */
550 if ( (strcmp(name,"icmpv6") == 0) ||
551 (strcmp(name,"ipv6-icmp") == 0) ||
552 (strcmp(name,"icmp6") == 0) )
553 name = icmp6;
554
555 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
556 if (strcmp(name, ptr->name) == 0) {
557 struct xtables_match *clone;
558
559 /* First match of this type: */
560 if (ptr->m == NULL)
561 break;
562
563 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100564 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000565 memcpy(clone, ptr, sizeof(struct xtables_match));
566 clone->mflags = 0;
567 /* This is a clone: */
568 clone->next = clone;
569
570 ptr = clone;
571 break;
572 }
573 }
574
575#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100576 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100577 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100578 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000579
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100580 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100581 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000582 "Couldn't load match `%s':%s\n",
583 name, dlerror());
584 }
585#else
586 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100587 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000588 ptr->loaded = 1;
589 else
590 ptr = NULL;
591 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100592 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100593 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000594 "Couldn't find match `%s'\n", name);
595 }
596#endif
597
598 if (ptr && matches) {
599 struct xtables_rule_match **i;
600 struct xtables_rule_match *newentry;
601
Jan Engelhardt630ef482009-01-27 14:58:41 +0100602 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000603
604 for (i = matches; *i; i = &(*i)->next) {
605 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100606 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000607 }
608 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100609 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000610 newentry->next = NULL;
611 *i = newentry;
612 }
613
614 return ptr;
615}
616
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100617struct xtables_target *
618xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000619{
620 struct xtables_target *ptr;
621
622 /* Standard target? */
623 if (strcmp(name, "") == 0
624 || strcmp(name, XTC_LABEL_ACCEPT) == 0
625 || strcmp(name, XTC_LABEL_DROP) == 0
626 || strcmp(name, XTC_LABEL_QUEUE) == 0
627 || strcmp(name, XTC_LABEL_RETURN) == 0)
628 name = "standard";
629
630 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
631 if (strcmp(name, ptr->name) == 0)
632 break;
633 }
634
635#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100636 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100637 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100638 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000639
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100640 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100641 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000642 "Couldn't load target `%s':%s\n",
643 name, dlerror());
644 }
645#else
646 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100647 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000648 ptr->loaded = 1;
649 else
650 ptr = NULL;
651 }
Peter Volkov854d2d92009-03-24 11:09:16 +0300652 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100653 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000654 "Couldn't find target `%s'\n", name);
655 }
656#endif
657
658 if (ptr)
659 ptr->used = 1;
660
661 return ptr;
662}
663
664static int compatible_revision(const char *name, u_int8_t revision, int opt)
665{
666 struct xt_get_revision rev;
667 socklen_t s = sizeof(rev);
668 int max_rev, sockfd;
669
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100670 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000671 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000672 if (errno == EPERM) {
673 /* revision 0 is always supported. */
674 if (revision != 0)
675 fprintf(stderr, "Could not determine whether "
676 "revision %u is supported, "
677 "assuming it is.\n",
678 revision);
679 return 1;
680 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000681 fprintf(stderr, "Could not open socket to kernel: %s\n",
682 strerror(errno));
683 exit(1);
684 }
685
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100686 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000687
688 strcpy(rev.name, name);
689 rev.revision = revision;
690
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100691 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000692 if (max_rev < 0) {
693 /* Definitely don't support this? */
694 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
695 close(sockfd);
696 return 0;
697 } else if (errno == ENOPROTOOPT) {
698 close(sockfd);
699 /* Assume only revision 0 support (old kernel) */
700 return (revision == 0);
701 } else {
702 fprintf(stderr, "getsockopt failed strangely: %s\n",
703 strerror(errno));
704 exit(1);
705 }
706 }
707 close(sockfd);
708 return 1;
709}
710
711
712static int compatible_match_revision(const char *name, u_int8_t revision)
713{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100714 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000715}
716
717static int compatible_target_revision(const char *name, u_int8_t revision)
718{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100719 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000720}
721
722void xtables_register_match(struct xtables_match *me)
723{
724 struct xtables_match **i, *old;
725
Jan Engelhardtc284de52009-06-25 21:25:24 +0200726 if (me->version == NULL) {
727 fprintf(stderr, "%s: match %s<%u> is missing a version\n",
728 xt_params->program_name, me->name, me->revision);
729 exit(1);
730 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100731 if (strcmp(me->version, XTABLES_VERSION) != 0) {
732 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
733 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500734 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100735 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000736 exit(1);
737 }
738
739 /* Revision field stole a char from name. */
740 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
741 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500742 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000743 exit(1);
744 }
745
746 if (me->family >= NPROTO) {
747 fprintf(stderr,
748 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500749 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000750 exit(1);
751 }
752
753 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100754 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000755 return;
756
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100757 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000758 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100759 if (old->revision == me->revision &&
760 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000761 fprintf(stderr,
762 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500763 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000764 exit(1);
765 }
766
767 /* Now we have two (or more) options, check compatibility. */
768 if (compatible_match_revision(old->name, old->revision)
769 && old->revision > me->revision)
770 return;
771
Jan Engelhardt23545c22008-02-14 04:23:04 +0100772 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000773 if (!compatible_match_revision(me->name, me->revision))
774 return;
775
Jan Engelhardt23545c22008-02-14 04:23:04 +0100776 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
777 if (old->revision == me->revision && me->family == AF_UNSPEC)
778 return;
779
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000780 /* Delete old one. */
781 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
782 *i = old->next;
783 }
784
785 if (me->size != XT_ALIGN(me->size)) {
786 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500787 xt_params->program_name, me->name,
788 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000789 exit(1);
790 }
791
792 /* Append to list. */
793 for (i = &xtables_matches; *i; i = &(*i)->next);
794 me->next = NULL;
795 *i = me;
796
797 me->m = NULL;
798 me->mflags = 0;
799}
800
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200801void xtables_register_matches(struct xtables_match *match, unsigned int n)
802{
803 do {
804 xtables_register_match(&match[--n]);
805 } while (n > 0);
806}
807
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000808void xtables_register_target(struct xtables_target *me)
809{
810 struct xtables_target *old;
811
Jan Engelhardtc284de52009-06-25 21:25:24 +0200812 if (me->version == NULL) {
813 fprintf(stderr, "%s: target %s<%u> is missing a version\n",
814 xt_params->program_name, me->name, me->revision);
815 exit(1);
816 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100817 if (strcmp(me->version, XTABLES_VERSION) != 0) {
818 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
819 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500820 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100821 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000822 exit(1);
823 }
824
825 /* Revision field stole a char from name. */
826 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
827 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500828 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000829 exit(1);
830 }
831
832 if (me->family >= NPROTO) {
833 fprintf(stderr,
834 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500835 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000836 exit(1);
837 }
838
839 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100840 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000841 return;
842
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100843 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000844 if (old) {
845 struct xtables_target **i;
846
Jan Engelhardt23545c22008-02-14 04:23:04 +0100847 if (old->revision == me->revision &&
848 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000849 fprintf(stderr,
850 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500851 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000852 exit(1);
853 }
854
855 /* Now we have two (or more) options, check compatibility. */
856 if (compatible_target_revision(old->name, old->revision)
857 && old->revision > me->revision)
858 return;
859
Jan Engelhardt23545c22008-02-14 04:23:04 +0100860 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000861 if (!compatible_target_revision(me->name, me->revision))
862 return;
863
Jan Engelhardt23545c22008-02-14 04:23:04 +0100864 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
865 if (old->revision == me->revision && me->family == AF_UNSPEC)
866 return;
867
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000868 /* Delete old one. */
869 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
870 *i = old->next;
871 }
872
873 if (me->size != XT_ALIGN(me->size)) {
874 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500875 xt_params->program_name, me->name,
876 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000877 exit(1);
878 }
879
880 /* Prepend to list. */
881 me->next = xtables_targets;
882 xtables_targets = me;
883 me->t = NULL;
884 me->tflags = 0;
885}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000886
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200887void xtables_register_targets(struct xtables_target *target, unsigned int n)
888{
889 do {
890 xtables_register_target(&target[--n]);
891 } while (n > 0);
892}
893
Jan Engelhardta41545c2009-01-27 21:27:19 +0100894/**
895 * xtables_param_act - act on condition
896 * @status: a constant from enum xtables_exittype
897 *
898 * %XTF_ONLY_ONCE: print error message that option may only be used once.
899 * @p1: module name (e.g. "mark")
900 * @p2(...): option in conflict (e.g. "--mark")
901 * @p3(...): condition to match on (see extensions/ for examples)
902 *
903 * %XTF_NO_INVERT: option does not support inversion
904 * @p1: module name
905 * @p2: option in conflict
906 * @p3: condition to match on
907 *
908 * %XTF_BAD_VALUE: bad value for option
909 * @p1: module name
910 * @p2: option with which the problem occured (e.g. "--mark")
911 * @p3: string the user passed in (e.g. "99999999999999")
912 *
913 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
914 * @p1: module name
915 *
916 * Displays an error message and exits the program.
917 */
918void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000919{
920 const char *p2, *p3;
921 va_list args;
922 bool b;
923
924 va_start(args, p1);
925
926 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100927 case XTF_ONLY_ONCE:
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 may only be specified once",
934 p1, p2);
935 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100936 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000937 p2 = va_arg(args, const char *);
938 b = va_arg(args, unsigned int);
939 if (!b)
940 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100941 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000942 "%s: \"%s\" option cannot be inverted", p1, p2);
943 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100944 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000945 p2 = va_arg(args, const char *);
946 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100947 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000948 "%s: Bad value for \"%s\" option: \"%s\"",
949 p1, p2, p3);
950 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100951 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000952 b = va_arg(args, unsigned int);
953 if (!b)
954 return;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100955 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000956 "%s: At most one action is possible", p1);
957 break;
958 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100959 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000960 break;
961 }
962
963 va_end(args);
964}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000965
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100966const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000967{
968 static char buf[20];
969 const unsigned char *bytep = (const void *)&addrp->s_addr;
970
971 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
972 return buf;
973}
974
975static const char *ipaddr_to_host(const struct in_addr *addr)
976{
977 struct hostent *host;
978
979 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
980 if (host == NULL)
981 return NULL;
982
983 return host->h_name;
984}
985
986static const char *ipaddr_to_network(const struct in_addr *addr)
987{
988 struct netent *net;
989
990 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
991 return net->n_name;
992
993 return NULL;
994}
995
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100996const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000997{
998 const char *name;
999
1000 if ((name = ipaddr_to_host(addr)) != NULL ||
1001 (name = ipaddr_to_network(addr)) != NULL)
1002 return name;
1003
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001004 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001005}
1006
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001007const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001008{
1009 static char buf[20];
1010 uint32_t maskaddr, bits;
1011 int i;
1012
1013 maskaddr = ntohl(mask->s_addr);
1014
1015 if (maskaddr == 0xFFFFFFFFL)
1016 /* we don't want to see "/32" */
1017 return "";
1018
1019 i = 32;
1020 bits = 0xFFFFFFFEL;
1021 while (--i >= 0 && maskaddr != bits)
1022 bits <<= 1;
1023 if (i >= 0)
1024 sprintf(buf, "/%d", i);
1025 else
1026 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001027 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001028
1029 return buf;
1030}
1031
Jan Engelhardtbd943842008-01-20 13:38:08 +00001032static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1033{
1034 static struct in_addr addr;
1035 unsigned char *addrp;
1036 unsigned int onebyte;
1037 char buf[20], *p, *q;
1038 int i;
1039
1040 /* copy dotted string, because we need to modify it */
1041 strncpy(buf, dotted, sizeof(buf) - 1);
1042 buf[sizeof(buf) - 1] = '\0';
1043 addrp = (void *)&addr.s_addr;
1044
1045 p = buf;
1046 for (i = 0; i < 3; ++i) {
1047 if ((q = strchr(p, '.')) == NULL) {
1048 if (is_mask)
1049 return NULL;
1050
1051 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001052 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001053 return NULL;
1054
1055 addrp[i] = onebyte;
1056 while (i < 3)
1057 addrp[++i] = 0;
1058
1059 return &addr;
1060 }
1061
1062 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001063 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001064 return NULL;
1065
1066 addrp[i] = onebyte;
1067 p = q + 1;
1068 }
1069
1070 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001071 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001072 return NULL;
1073
1074 addrp[3] = onebyte;
1075 return &addr;
1076}
1077
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001078struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001079{
1080 return __numeric_to_ipaddr(dotted, false);
1081}
1082
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001083struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001084{
1085 return __numeric_to_ipaddr(dotted, true);
1086}
1087
1088static struct in_addr *network_to_ipaddr(const char *name)
1089{
1090 static struct in_addr addr;
1091 struct netent *net;
1092
1093 if ((net = getnetbyname(name)) != NULL) {
1094 if (net->n_addrtype != AF_INET)
1095 return NULL;
1096 addr.s_addr = htonl(net->n_net);
1097 return &addr;
1098 }
1099
1100 return NULL;
1101}
1102
1103static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1104{
1105 struct hostent *host;
1106 struct in_addr *addr;
1107 unsigned int i;
1108
1109 *naddr = 0;
1110 if ((host = gethostbyname(name)) != NULL) {
1111 if (host->h_addrtype != AF_INET ||
1112 host->h_length != sizeof(struct in_addr))
1113 return NULL;
1114
1115 while (host->h_addr_list[*naddr] != NULL)
1116 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001117 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001118 for (i = 0; i < *naddr; i++)
1119 memcpy(&addr[i], host->h_addr_list[i],
1120 sizeof(struct in_addr));
1121 return addr;
1122 }
1123
1124 return NULL;
1125}
1126
1127static struct in_addr *
1128ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1129{
1130 struct in_addr *addrptmp, *addrp;
1131
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001132 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001133 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001134 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001135 memcpy(addrp, addrptmp, sizeof(*addrp));
1136 *naddrs = 1;
1137 return addrp;
1138 }
1139 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1140 return addrptmp;
1141
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001142 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001143}
1144
1145static struct in_addr *parse_ipmask(const char *mask)
1146{
1147 static struct in_addr maskaddr;
1148 struct in_addr *addrp;
1149 unsigned int bits;
1150
1151 if (mask == NULL) {
1152 /* no mask at all defaults to 32 bits */
1153 maskaddr.s_addr = 0xFFFFFFFF;
1154 return &maskaddr;
1155 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001156 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001157 /* dotted_to_addr already returns a network byte order addr */
1158 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001159 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001160 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001161 "invalid mask `%s' specified", mask);
1162 if (bits != 0) {
1163 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1164 return &maskaddr;
1165 }
1166
1167 maskaddr.s_addr = 0U;
1168 return &maskaddr;
1169}
1170
Michael Granzow332e4ac2009-04-09 18:24:36 +01001171void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp,
1172 struct in_addr **maskpp, unsigned int *naddrs)
1173{
1174 struct in_addr *addrp;
1175 char buf[256], *p;
1176 unsigned int len, i, j, n, count = 1;
1177 const char *loop = name;
1178
1179 while ((loop = strchr(loop, ',')) != NULL) {
1180 ++count;
1181 ++loop; /* skip ',' */
1182 }
1183
1184 *addrpp = xtables_malloc(sizeof(struct in_addr) * count);
1185 *maskpp = xtables_malloc(sizeof(struct in_addr) * count);
1186
1187 loop = name;
1188
1189 for (i = 0; i < count; ++i) {
1190 if (loop == NULL)
1191 break;
1192 if (*loop == ',')
1193 ++loop;
1194 if (*loop == '\0')
1195 break;
1196 p = strchr(loop, ',');
1197 if (p != NULL)
1198 len = p - loop;
1199 else
1200 len = strlen(loop);
1201 if (len == 0 || sizeof(buf) - 1 < len)
1202 break;
1203
1204 strncpy(buf, loop, len);
1205 buf[len] = '\0';
1206 loop += len;
1207 if ((p = strrchr(buf, '/')) != NULL) {
1208 *p = '\0';
1209 addrp = parse_ipmask(p + 1);
1210 } else {
1211 addrp = parse_ipmask(NULL);
1212 }
1213 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1214
1215 /* if a null mask is given, the name is ignored, like in "any/0" */
1216 if ((*maskpp + i)->s_addr == 0)
1217 /*
1218 * A bit pointless to process multiple addresses
1219 * in this case...
1220 */
1221 strcpy(buf, "0.0.0.0");
1222
1223 addrp = ipparse_hostnetwork(buf, &n);
1224 if (n > 1) {
1225 count += n - 1;
1226 *addrpp = xtables_realloc(*addrpp,
1227 sizeof(struct in_addr) * count);
1228 *maskpp = xtables_realloc(*maskpp,
1229 sizeof(struct in_addr) * count);
1230 for (j = 0; j < n; ++j)
1231 /* for each new addr */
1232 memcpy(*addrpp + i + j, addrp + j,
1233 sizeof(*addrp));
1234 for (j = 1; j < n; ++j)
1235 /* for each new mask */
1236 memcpy(*maskpp + i + j, *maskpp + i,
1237 sizeof(*addrp));
1238 i += n - 1;
1239 } else {
1240 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1241 }
1242 /* free what ipparse_hostnetwork had allocated: */
1243 free(addrp);
1244 }
1245 *naddrs = count;
1246 for (i = 0; i < n; ++i)
1247 (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr;
1248}
1249
1250
Jan Engelhardta0baae82009-01-30 04:32:50 +01001251/**
1252 * xtables_ipparse_any - transform arbitrary name to in_addr
1253 *
1254 * Possible inputs (pseudo regex):
1255 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1256 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1257 */
1258void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1259 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001260{
1261 unsigned int i, j, k, n;
1262 struct in_addr *addrp;
1263 char buf[256], *p;
1264
1265 strncpy(buf, name, sizeof(buf) - 1);
1266 buf[sizeof(buf) - 1] = '\0';
1267 if ((p = strrchr(buf, '/')) != NULL) {
1268 *p = '\0';
1269 addrp = parse_ipmask(p + 1);
1270 } else {
1271 addrp = parse_ipmask(NULL);
1272 }
1273 memcpy(maskp, addrp, sizeof(*maskp));
1274
1275 /* if a null mask is given, the name is ignored, like in "any/0" */
1276 if (maskp->s_addr == 0U)
1277 strcpy(buf, "0.0.0.0");
1278
1279 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1280 n = *naddrs;
1281 for (i = 0, j = 0; i < n; ++i) {
1282 addrp[j++].s_addr &= maskp->s_addr;
1283 for (k = 0; k < j - 1; ++k)
1284 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1285 --*naddrs;
1286 --j;
1287 break;
1288 }
1289 }
1290}
1291
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001292const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001293{
1294 /* 0000:0000:0000:0000:0000:000.000.000.000
1295 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1296 static char buf[50+1];
1297 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1298}
1299
1300static const char *ip6addr_to_host(const struct in6_addr *addr)
1301{
1302 static char hostname[NI_MAXHOST];
1303 struct sockaddr_in6 saddr;
1304 int err;
1305
1306 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1307 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1308 saddr.sin6_family = AF_INET6;
1309
1310 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1311 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1312 if (err != 0) {
1313#ifdef DEBUG
1314 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1315#endif
1316 return NULL;
1317 }
1318
1319#ifdef DEBUG
1320 fprintf (stderr, "\naddr2host: %s\n", hostname);
1321#endif
1322 return hostname;
1323}
1324
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001325const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001326{
1327 const char *name;
1328
1329 if ((name = ip6addr_to_host(addr)) != NULL)
1330 return name;
1331
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001332 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001333}
1334
1335static int ip6addr_prefix_length(const struct in6_addr *k)
1336{
1337 unsigned int bits = 0;
1338 uint32_t a, b, c, d;
1339
Jan Engelhardt48607812008-06-10 15:17:53 +02001340 a = ntohl(k->s6_addr32[0]);
1341 b = ntohl(k->s6_addr32[1]);
1342 c = ntohl(k->s6_addr32[2]);
1343 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001344 while (a & 0x80000000U) {
1345 ++bits;
1346 a <<= 1;
1347 a |= (b >> 31) & 1;
1348 b <<= 1;
1349 b |= (c >> 31) & 1;
1350 c <<= 1;
1351 c |= (d >> 31) & 1;
1352 d <<= 1;
1353 }
1354 if (a != 0 || b != 0 || c != 0 || d != 0)
1355 return -1;
1356 return bits;
1357}
1358
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001359const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001360{
1361 static char buf[50+2];
1362 int l = ip6addr_prefix_length(addrp);
1363
1364 if (l == -1) {
1365 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001366 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001367 return buf;
1368 }
1369 sprintf(buf, "/%d", l);
1370 return buf;
1371}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001372
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001373struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001374{
1375 static struct in6_addr ap;
1376 int err;
1377
1378 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1379 return &ap;
1380#ifdef DEBUG
1381 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1382#endif
1383 return NULL;
1384}
1385
1386static struct in6_addr *
1387host_to_ip6addr(const char *name, unsigned int *naddr)
1388{
1389 static struct in6_addr *addr;
1390 struct addrinfo hints;
1391 struct addrinfo *res;
1392 int err;
1393
1394 memset(&hints, 0, sizeof(hints));
1395 hints.ai_flags = AI_CANONNAME;
1396 hints.ai_family = AF_INET6;
1397 hints.ai_socktype = SOCK_RAW;
1398 hints.ai_protocol = IPPROTO_IPV6;
1399 hints.ai_next = NULL;
1400
1401 *naddr = 0;
1402 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1403#ifdef DEBUG
1404 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1405#endif
1406 return NULL;
1407 } else {
1408 if (res->ai_family != AF_INET6 ||
1409 res->ai_addrlen != sizeof(struct sockaddr_in6))
1410 return NULL;
1411
1412#ifdef DEBUG
1413 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1414 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1415#endif
1416 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001417 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001418 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1419 sizeof(struct in6_addr));
1420 freeaddrinfo(res);
1421 *naddr = 1;
1422 return addr;
1423 }
1424
1425 return NULL;
1426}
1427
1428static struct in6_addr *network_to_ip6addr(const char *name)
1429{
1430 /* abort();*/
1431 /* TODO: not implemented yet, but the exception breaks the
1432 * name resolvation */
1433 return NULL;
1434}
1435
1436static struct in6_addr *
1437ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1438{
1439 struct in6_addr *addrp, *addrptmp;
1440
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001441 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001442 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001443 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001444 memcpy(addrp, addrptmp, sizeof(*addrp));
1445 *naddrs = 1;
1446 return addrp;
1447 }
1448 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1449 return addrp;
1450
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001451 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001452}
1453
1454static struct in6_addr *parse_ip6mask(char *mask)
1455{
1456 static struct in6_addr maskaddr;
1457 struct in6_addr *addrp;
1458 unsigned int bits;
1459
1460 if (mask == NULL) {
1461 /* no mask at all defaults to 128 bits */
1462 memset(&maskaddr, 0xff, sizeof maskaddr);
1463 return &maskaddr;
1464 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001465 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001466 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001467 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001468 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001469 "invalid mask `%s' specified", mask);
1470 if (bits != 0) {
1471 char *p = (void *)&maskaddr;
1472 memset(p, 0xff, bits / 8);
1473 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1474 p[bits/8] = 0xff << (8 - (bits & 7));
1475 return &maskaddr;
1476 }
1477
1478 memset(&maskaddr, 0, sizeof(maskaddr));
1479 return &maskaddr;
1480}
1481
Michael Granzow332e4ac2009-04-09 18:24:36 +01001482void
1483xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp,
1484 struct in6_addr **maskpp, unsigned int *naddrs)
1485{
Olaf Rempel58df9012009-09-20 13:24:11 +02001486 static const struct in6_addr zero_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001487 struct in6_addr *addrp;
1488 char buf[256], *p;
1489 unsigned int len, i, j, n, count = 1;
1490 const char *loop = name;
1491
1492 while ((loop = strchr(loop, ',')) != NULL) {
1493 ++count;
1494 ++loop; /* skip ',' */
1495 }
1496
1497 *addrpp = xtables_malloc(sizeof(struct in6_addr) * count);
1498 *maskpp = xtables_malloc(sizeof(struct in6_addr) * count);
1499
1500 loop = name;
1501
1502 for (i = 0; i < count /*NB: count can grow*/; ++i) {
1503 if (loop == NULL)
1504 break;
1505 if (*loop == ',')
1506 ++loop;
1507 if (*loop == '\0')
1508 break;
1509 p = strchr(loop, ',');
1510 if (p != NULL)
1511 len = p - loop;
1512 else
1513 len = strlen(loop);
1514 if (len == 0 || sizeof(buf) - 1 < len)
1515 break;
1516
1517 strncpy(buf, loop, len);
1518 buf[len] = '\0';
1519 loop += len;
1520 if ((p = strrchr(buf, '/')) != NULL) {
1521 *p = '\0';
1522 addrp = parse_ip6mask(p + 1);
1523 } else {
1524 addrp = parse_ip6mask(NULL);
1525 }
1526 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1527
1528 /* if a null mask is given, the name is ignored, like in "any/0" */
Olaf Rempel58df9012009-09-20 13:24:11 +02001529 if (memcmp(*maskpp + i, &zero_addr, sizeof(zero_addr)) == 0)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001530 strcpy(buf, "::");
1531
1532 addrp = ip6parse_hostnetwork(buf, &n);
1533 /* ip6parse_hostnetwork only ever returns one IP
1534 address (it exits if the resolution fails).
1535 Therefore, n will always be 1 here. Leaving the
1536 code below in anyway in case ip6parse_hostnetwork
1537 is improved some day to behave like
1538 ipparse_hostnetwork: */
1539 if (n > 1) {
1540 count += n - 1;
1541 *addrpp = xtables_realloc(*addrpp,
1542 sizeof(struct in6_addr) * count);
1543 *maskpp = xtables_realloc(*maskpp,
1544 sizeof(struct in6_addr) * count);
1545 for (j = 0; j < n; ++j)
1546 /* for each new addr */
1547 memcpy(*addrpp + i + j, addrp + j,
1548 sizeof(*addrp));
1549 for (j = 1; j < n; ++j)
1550 /* for each new mask */
1551 memcpy(*maskpp + i + j, *maskpp + i,
1552 sizeof(*addrp));
1553 i += n - 1;
1554 } else {
1555 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1556 }
1557 /* free what ip6parse_hostnetwork had allocated: */
1558 free(addrp);
1559 }
1560 *naddrs = count;
1561 for (i = 0; i < n; ++i)
1562 for (j = 0; j < 4; ++j)
1563 (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j];
1564}
1565
Jan Engelhardta0baae82009-01-30 04:32:50 +01001566void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1567 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001568{
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001569 static const struct in6_addr zero_addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001570 struct in6_addr *addrp;
1571 unsigned int i, j, k, n;
1572 char buf[256], *p;
1573
1574 strncpy(buf, name, sizeof(buf) - 1);
1575 buf[sizeof(buf)-1] = '\0';
1576 if ((p = strrchr(buf, '/')) != NULL) {
1577 *p = '\0';
1578 addrp = parse_ip6mask(p + 1);
1579 } else {
1580 addrp = parse_ip6mask(NULL);
1581 }
1582 memcpy(maskp, addrp, sizeof(*maskp));
1583
1584 /* if a null mask is given, the name is ignored, like in "any/0" */
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001585 if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001586 strcpy(buf, "::");
1587
1588 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1589 n = *naddrs;
1590 for (i = 0, j = 0; i < n; ++i) {
1591 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001592 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001593 ++j;
1594 for (k = 0; k < j - 1; ++k)
1595 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1596 --*naddrs;
1597 --j;
1598 break;
1599 }
1600 }
1601}
Max Kellermanna5d09942008-01-29 13:44:34 +00001602
Jan Engelhardta0baae82009-01-30 04:32:50 +01001603void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001604{
1605 static const char no_quote_chars[] = "_-0123456789"
1606 "abcdefghijklmnopqrstuvwxyz"
1607 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1608 static const char escape_chars[] = "\"\\'";
1609 size_t length;
1610 const char *p;
1611
1612 length = strcspn(value, no_quote_chars);
1613 if (length > 0 && value[length] == 0) {
1614 /* no quoting required */
1615 fputs(value, stdout);
1616 putchar(' ');
1617 } else {
1618 /* there is at least one dangerous character in the
1619 value, which we have to quote. Write double quotes
1620 around the value and escape special characters with
1621 a backslash */
1622 putchar('"');
1623
1624 for (p = strpbrk(value, escape_chars); p != NULL;
1625 p = strpbrk(value, escape_chars)) {
1626 if (p > value)
1627 fwrite(value, 1, p - value, stdout);
1628 putchar('\\');
1629 putchar(*p);
1630 value = p + 1;
1631 }
1632
1633 /* print the rest and finish the double quoted
1634 string */
1635 fputs(value, stdout);
1636 printf("\" ");
1637 }
1638}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001639
1640/**
1641 * Check for option-intrapositional negation.
1642 * Do not use in new code.
1643 */
1644int xtables_check_inverse(const char option[], int *invert,
Jan Engelhardtbf971282009-11-03 19:55:11 +01001645 int *my_optind, int argc, char **argv)
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001646{
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001647 if (option == NULL || strcmp(option, "!") != 0)
1648 return false;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001649
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001650 fprintf(stderr, "Using intrapositioned negation "
1651 "(`--option ! this`) is deprecated in favor of "
1652 "extrapositioned (`! --option this`).\n");
1653
1654 if (*invert)
1655 xt_params->exit_err(PARAMETER_PROBLEM,
1656 "Multiple `!' flags not allowed");
1657 *invert = true;
1658 if (my_optind != NULL) {
Jan Engelhardtbf971282009-11-03 19:55:11 +01001659 optarg = argv[*my_optind];
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001660 ++*my_optind;
1661 if (argc && *my_optind > argc)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001662 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001663 "no argument following `!'");
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001664 }
Jan Engelhardt2be22fb2009-10-24 00:08:09 +02001665
1666 return true;
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001667}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001668
1669const struct xtables_pprot xtables_chain_protos[] = {
1670 {"tcp", IPPROTO_TCP},
1671 {"sctp", IPPROTO_SCTP},
1672 {"udp", IPPROTO_UDP},
1673 {"udplite", IPPROTO_UDPLITE},
1674 {"icmp", IPPROTO_ICMP},
1675 {"icmpv6", IPPROTO_ICMPV6},
1676 {"ipv6-icmp", IPPROTO_ICMPV6},
1677 {"esp", IPPROTO_ESP},
1678 {"ah", IPPROTO_AH},
1679 {"ipv6-mh", IPPROTO_MH},
1680 {"mh", IPPROTO_MH},
1681 {"all", 0},
1682 {NULL},
1683};
1684
1685u_int16_t
1686xtables_parse_protocol(const char *s)
1687{
1688 unsigned int proto;
1689
1690 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1691 struct protoent *pent;
1692
1693 /* first deal with the special case of 'all' to prevent
1694 * people from being able to redefine 'all' in nsswitch
1695 * and/or provoke expensive [not working] ldap/nis/...
1696 * lookups */
1697 if (!strcmp(s, "all"))
1698 return 0;
1699
1700 if ((pent = getprotobyname(s)))
1701 proto = pent->p_proto;
1702 else {
1703 unsigned int i;
1704 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
Pablo Neira Ayusoe55cc4a2009-05-12 09:51:26 +02001705 if (xtables_chain_protos[i].name == NULL)
1706 continue;
1707
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001708 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1709 proto = xtables_chain_protos[i].num;
1710 break;
1711 }
1712 }
1713 if (i == ARRAY_SIZE(xtables_chain_protos))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001714 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001715 "unknown protocol `%s' specified",
1716 s);
1717 }
1718 }
1719
1720 return proto;
1721}