blob: 95be5f8eaac91c6ca9bb4c0c8affb09e2e099e66 [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000019#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000021#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000022#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000023#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000024#include <stdio.h>
25#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000026#include <string.h>
27#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000028#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000032#include <arpa/inet.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000034#include <xtables.h>
Jan Engelhardt77f48c22009-02-07 19:59:53 +010035#include <linux/netfilter_ipv4/ip_tables.h>
36#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020037#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000038
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000039#ifndef NO_SHARED_LIBS
40#include <dlfcn.h>
41#endif
42
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000043#define NPROTO 255
44
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000045#ifndef PROC_SYS_MODPROBE
46#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
47#endif
48
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010049struct xtables_globals *xt_params;
50/**
51 * xtables_set_params - set the global parameters used by xtables
52 * @xtp: input xtables_globals structure
53 *
54 * The app is expected to pass a valid xtables_globals data-filled
55 * with proper values
56 * @xtp cannot be NULL
57 *
58 * Returns -1 on failure to set and 0 on success
59 */
60int xtables_set_params(struct xtables_globals *xtp)
61{
62 if (!xtp) {
63 fprintf(stderr, "%s: Illegal global params\n",__func__);
64 return -1;
65 }
66
67 xt_params = xtp;
68 return 0;
69}
70
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010071void xtables_free_opts(int reset_offset, struct option *original_opts)
72{
73 if (xt_params->opts != original_opts) {
74 if (original_opts)
75 free(xt_params->opts);
76 xt_params->opts = original_opts;
77 if (reset_offset)
78 xt_params->option_offset = 0;
79 }
80}
81
Jan Engelhardtdacafa52009-01-27 20:56:23 +010082/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +010083 * xtables_afinfo - protocol family dependent information
84 * @kmod: kernel module basename (e.g. "ip_tables")
85 * @libprefix: prefix of .so library name (e.g. "libipt_")
86 * @family: nfproto family
87 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
88 * @so_rev_match: optname to check revision support of match
89 * @so_rev_target: optname to check revision support of target
90 */
91struct xtables_afinfo {
92 const char *kmod;
93 const char *libprefix;
94 uint8_t family;
95 uint8_t ipproto;
96 int so_rev_match;
97 int so_rev_target;
98};
99
100static const struct xtables_afinfo afinfo_ipv4 = {
101 .kmod = "ip_tables",
102 .libprefix = "libipt_",
103 .family = NFPROTO_IPV4,
104 .ipproto = IPPROTO_IP,
105 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
106 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
107};
108
109static const struct xtables_afinfo afinfo_ipv6 = {
110 .kmod = "ip6_tables",
111 .libprefix = "libip6t_",
112 .family = NFPROTO_IPV6,
113 .ipproto = IPPROTO_IPV6,
114 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
115 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
116};
117
118static const struct xtables_afinfo *afinfo;
119
120/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100121 * Program will set this to its own name.
122 */
123const char *xtables_program_name;
124
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100125/* Search path for Xtables .so files */
126static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000127
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000128/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100129const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000130
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000131/* Keeping track of external matches and targets: linked lists. */
132struct xtables_match *xtables_matches;
133struct xtables_target *xtables_targets;
134
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100135void xtables_init(void)
136{
137 xtables_libdir = getenv("XTABLES_LIBDIR");
138 if (xtables_libdir != NULL)
139 return;
140 xtables_libdir = getenv("IPTABLES_LIB_DIR");
141 if (xtables_libdir != NULL) {
142 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
143 "use XTABLES_LIBDIR.\n");
144 return;
145 }
146 xtables_libdir = XTABLES_LIBDIR;
147}
148
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100149void xtables_set_nfproto(uint8_t nfproto)
150{
151 switch (nfproto) {
152 case NFPROTO_IPV4:
153 afinfo = &afinfo_ipv4;
154 break;
155 case NFPROTO_IPV6:
156 afinfo = &afinfo_ipv6;
157 break;
158 default:
159 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
160 __func__);
161 }
162}
163
Jan Engelhardt630ef482009-01-27 14:58:41 +0100164/**
165 * xtables_*alloc - wrappers that exit on failure
166 */
167void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000168{
169 void *p;
170
171 if ((p = calloc(count, size)) == NULL) {
172 perror("ip[6]tables: calloc failed");
173 exit(1);
174 }
175
176 return p;
177}
178
Jan Engelhardt630ef482009-01-27 14:58:41 +0100179void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000180{
181 void *p;
182
183 if ((p = malloc(size)) == NULL) {
184 perror("ip[6]tables: malloc failed");
185 exit(1);
186 }
187
188 return p;
189}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000190
191static char *get_modprobe(void)
192{
193 int procfile;
194 char *ret;
195
196#define PROCFILE_BUFSIZ 1024
197 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
198 if (procfile < 0)
199 return NULL;
200
201 ret = (char *) malloc(PROCFILE_BUFSIZ);
202 if (ret) {
203 memset(ret, 0, PROCFILE_BUFSIZ);
204 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
205 case -1: goto fail;
206 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
207 }
208 if (ret[strlen(ret)-1]=='\n')
209 ret[strlen(ret)-1]=0;
210 close(procfile);
211 return ret;
212 }
213 fail:
214 free(ret);
215 close(procfile);
216 return NULL;
217}
218
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100219int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000220{
221 char *buf = NULL;
222 char *argv[4];
223 int status;
224
225 /* If they don't explicitly set it, read out of kernel */
226 if (!modprobe) {
227 buf = get_modprobe();
228 if (!buf)
229 return -1;
230 modprobe = buf;
231 }
232
233 switch (fork()) {
234 case 0:
235 argv[0] = (char *)modprobe;
236 argv[1] = (char *)modname;
237 if (quiet) {
238 argv[2] = "-q";
239 argv[3] = NULL;
240 } else {
241 argv[2] = NULL;
242 argv[3] = NULL;
243 }
244 execv(argv[0], argv);
245
246 /* not usually reached */
247 exit(1);
248 case -1:
249 return -1;
250
251 default: /* parent */
252 wait(&status);
253 }
254
255 free(buf);
256 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
257 return 0;
258 return -1;
259}
260
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100261int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000262{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100263 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000264 static int ret = -1;
265
266 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100267 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000268 loaded = (ret == 0);
269 }
270
271 return ret;
272}
273
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100274/**
275 * xtables_strtou{i,l} - string to number conversion
276 * @s: input string
277 * @end: like strtoul's "end" pointer
278 * @value: pointer for result
279 * @min: minimum accepted value
280 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000281 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100282 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
283 * "15a" is rejected.
284 * In either case, the value obtained is compared for min-max compliance.
285 * Base is always 0, i.e. autodetect depending on @s.
286 *
287 * Returns true/false whether number was accepted. On failure, *value has
288 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000289 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100290bool xtables_strtoul(const char *s, char **end, unsigned long *value,
291 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000292{
293 unsigned long v;
294 char *my_end;
295
296 errno = 0;
297 v = strtoul(s, &my_end, 0);
298
299 if (my_end == s)
300 return false;
301 if (end != NULL)
302 *end = my_end;
303
304 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
305 if (value != NULL)
306 *value = v;
307 if (end == NULL)
308 return *my_end == '\0';
309 return true;
310 }
311
312 return false;
313}
314
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100315bool xtables_strtoui(const char *s, char **end, unsigned int *value,
316 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000317{
318 unsigned long v;
319 bool ret;
320
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100321 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000322 if (value != NULL)
323 *value = v;
324 return ret;
325}
326
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100327int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000328{
329 struct servent *service;
330
331 if ((service = getservbyname(name, proto)) != NULL)
332 return ntohs((unsigned short) service->s_port);
333
334 return -1;
335}
336
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100337u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000338{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100339 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000340
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100341 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100342 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100343 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000344
345 exit_error(PARAMETER_PROBLEM,
346 "invalid port/service `%s' specified", port);
347}
348
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100349void xtables_parse_interface(const char *arg, char *vianame,
350 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000351{
352 int vialen = strlen(arg);
353 unsigned int i;
354
355 memset(mask, 0, IFNAMSIZ);
356 memset(vianame, 0, IFNAMSIZ);
357
358 if (vialen + 1 > IFNAMSIZ)
359 exit_error(PARAMETER_PROBLEM,
360 "interface name `%s' must be shorter than IFNAMSIZ"
361 " (%i)", arg, IFNAMSIZ-1);
362
363 strcpy(vianame, arg);
364 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
365 memset(mask, 0, IFNAMSIZ);
366 else if (vianame[vialen - 1] == '+') {
367 memset(mask, 0xFF, vialen - 1);
368 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
369 /* Don't remove `+' here! -HW */
370 } else {
371 /* Include nul-terminator in match */
372 memset(mask, 0xFF, vialen + 1);
373 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
374 for (i = 0; vianame[i]; i++) {
375 if (vianame[i] == ':' ||
376 vianame[i] == '!' ||
377 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000378 fprintf(stderr,
379 "Warning: weird character in interface"
380 " `%s' (No aliases, :, ! or *).\n",
381 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000382 break;
383 }
384 }
385 }
386}
387
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200388#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100389static void *load_extension(const char *search_path, const char *prefix,
390 const char *name, bool is_target)
391{
392 const char *dir = search_path, *next;
393 void *ptr = NULL;
394 struct stat sb;
395 char path[256];
396
397 do {
398 next = strchr(dir, ':');
399 if (next == NULL)
400 next = dir + strlen(dir);
401 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200402 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100403
404 if (dlopen(path, RTLD_NOW) != NULL) {
405 /* Found library. If it didn't register itself,
406 maybe they specified target as match. */
407 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100408 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100409 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100410 ptr = xtables_find_match(name,
411 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100412 } else if (stat(path, &sb) == 0) {
413 fprintf(stderr, "%s: %s\n", path, dlerror());
414 }
415
416 if (ptr != NULL)
417 return ptr;
418
419 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200420 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100421 if (dlopen(path, RTLD_NOW) != NULL) {
422 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100423 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100424 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100425 ptr = xtables_find_match(name,
426 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100427 } else if (stat(path, &sb) == 0) {
428 fprintf(stderr, "%s: %s\n", path, dlerror());
429 }
430
431 if (ptr != NULL)
432 return ptr;
433
434 dir = next + 1;
435 } while (*next != '\0');
436
437 return NULL;
438}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200439#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100440
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100441struct xtables_match *
442xtables_find_match(const char *name, enum xtables_tryload tryload,
443 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000444{
445 struct xtables_match *ptr;
446 const char *icmp6 = "icmp6";
447
448 /* This is ugly as hell. Nonetheless, there is no way of changing
449 * this without hurting backwards compatibility */
450 if ( (strcmp(name,"icmpv6") == 0) ||
451 (strcmp(name,"ipv6-icmp") == 0) ||
452 (strcmp(name,"icmp6") == 0) )
453 name = icmp6;
454
455 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
456 if (strcmp(name, ptr->name) == 0) {
457 struct xtables_match *clone;
458
459 /* First match of this type: */
460 if (ptr->m == NULL)
461 break;
462
463 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100464 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000465 memcpy(clone, ptr, sizeof(struct xtables_match));
466 clone->mflags = 0;
467 /* This is a clone: */
468 clone->next = clone;
469
470 ptr = clone;
471 break;
472 }
473 }
474
475#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100476 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100477 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100478 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000479
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100480 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000481 exit_error(PARAMETER_PROBLEM,
482 "Couldn't load match `%s':%s\n",
483 name, dlerror());
484 }
485#else
486 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100487 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000488 ptr->loaded = 1;
489 else
490 ptr = NULL;
491 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100492 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000493 exit_error(PARAMETER_PROBLEM,
494 "Couldn't find match `%s'\n", name);
495 }
496#endif
497
498 if (ptr && matches) {
499 struct xtables_rule_match **i;
500 struct xtables_rule_match *newentry;
501
Jan Engelhardt630ef482009-01-27 14:58:41 +0100502 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000503
504 for (i = matches; *i; i = &(*i)->next) {
505 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100506 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000507 }
508 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100509 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000510 newentry->next = NULL;
511 *i = newentry;
512 }
513
514 return ptr;
515}
516
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100517struct xtables_target *
518xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000519{
520 struct xtables_target *ptr;
521
522 /* Standard target? */
523 if (strcmp(name, "") == 0
524 || strcmp(name, XTC_LABEL_ACCEPT) == 0
525 || strcmp(name, XTC_LABEL_DROP) == 0
526 || strcmp(name, XTC_LABEL_QUEUE) == 0
527 || strcmp(name, XTC_LABEL_RETURN) == 0)
528 name = "standard";
529
530 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
531 if (strcmp(name, ptr->name) == 0)
532 break;
533 }
534
535#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100536 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100537 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100538 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000539
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100540 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000541 exit_error(PARAMETER_PROBLEM,
542 "Couldn't load target `%s':%s\n",
543 name, dlerror());
544 }
545#else
546 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100547 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000548 ptr->loaded = 1;
549 else
550 ptr = NULL;
551 }
552 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
553 exit_error(PARAMETER_PROBLEM,
554 "Couldn't find target `%s'\n", name);
555 }
556#endif
557
558 if (ptr)
559 ptr->used = 1;
560
561 return ptr;
562}
563
564static int compatible_revision(const char *name, u_int8_t revision, int opt)
565{
566 struct xt_get_revision rev;
567 socklen_t s = sizeof(rev);
568 int max_rev, sockfd;
569
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100570 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000571 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000572 if (errno == EPERM) {
573 /* revision 0 is always supported. */
574 if (revision != 0)
575 fprintf(stderr, "Could not determine whether "
576 "revision %u is supported, "
577 "assuming it is.\n",
578 revision);
579 return 1;
580 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000581 fprintf(stderr, "Could not open socket to kernel: %s\n",
582 strerror(errno));
583 exit(1);
584 }
585
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100586 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000587
588 strcpy(rev.name, name);
589 rev.revision = revision;
590
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100591 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000592 if (max_rev < 0) {
593 /* Definitely don't support this? */
594 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
595 close(sockfd);
596 return 0;
597 } else if (errno == ENOPROTOOPT) {
598 close(sockfd);
599 /* Assume only revision 0 support (old kernel) */
600 return (revision == 0);
601 } else {
602 fprintf(stderr, "getsockopt failed strangely: %s\n",
603 strerror(errno));
604 exit(1);
605 }
606 }
607 close(sockfd);
608 return 1;
609}
610
611
612static int compatible_match_revision(const char *name, u_int8_t revision)
613{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100614 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000615}
616
617static int compatible_target_revision(const char *name, u_int8_t revision)
618{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100619 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000620}
621
622void xtables_register_match(struct xtables_match *me)
623{
624 struct xtables_match **i, *old;
625
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100626 if (strcmp(me->version, XTABLES_VERSION) != 0) {
627 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
628 "but \"%s\" is required.\n",
629 xtables_program_name, me->name,
630 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000631 exit(1);
632 }
633
634 /* Revision field stole a char from name. */
635 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
636 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100637 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000638 exit(1);
639 }
640
641 if (me->family >= NPROTO) {
642 fprintf(stderr,
643 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100644 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000645 exit(1);
646 }
647
648 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100649 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000650 return;
651
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100652 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000653 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100654 if (old->revision == me->revision &&
655 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000656 fprintf(stderr,
657 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100658 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000659 exit(1);
660 }
661
662 /* Now we have two (or more) options, check compatibility. */
663 if (compatible_match_revision(old->name, old->revision)
664 && old->revision > me->revision)
665 return;
666
Jan Engelhardt23545c22008-02-14 04:23:04 +0100667 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000668 if (!compatible_match_revision(me->name, me->revision))
669 return;
670
Jan Engelhardt23545c22008-02-14 04:23:04 +0100671 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
672 if (old->revision == me->revision && me->family == AF_UNSPEC)
673 return;
674
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000675 /* Delete old one. */
676 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
677 *i = old->next;
678 }
679
680 if (me->size != XT_ALIGN(me->size)) {
681 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100682 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000683 exit(1);
684 }
685
686 /* Append to list. */
687 for (i = &xtables_matches; *i; i = &(*i)->next);
688 me->next = NULL;
689 *i = me;
690
691 me->m = NULL;
692 me->mflags = 0;
693}
694
695void xtables_register_target(struct xtables_target *me)
696{
697 struct xtables_target *old;
698
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100699 if (strcmp(me->version, XTABLES_VERSION) != 0) {
700 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
701 "but \"%s\" is required.\n",
702 xtables_program_name, me->name,
703 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000704 exit(1);
705 }
706
707 /* Revision field stole a char from name. */
708 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
709 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100710 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000711 exit(1);
712 }
713
714 if (me->family >= NPROTO) {
715 fprintf(stderr,
716 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100717 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000718 exit(1);
719 }
720
721 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100722 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000723 return;
724
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100725 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000726 if (old) {
727 struct xtables_target **i;
728
Jan Engelhardt23545c22008-02-14 04:23:04 +0100729 if (old->revision == me->revision &&
730 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000731 fprintf(stderr,
732 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100733 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000734 exit(1);
735 }
736
737 /* Now we have two (or more) options, check compatibility. */
738 if (compatible_target_revision(old->name, old->revision)
739 && old->revision > me->revision)
740 return;
741
Jan Engelhardt23545c22008-02-14 04:23:04 +0100742 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000743 if (!compatible_target_revision(me->name, me->revision))
744 return;
745
Jan Engelhardt23545c22008-02-14 04:23:04 +0100746 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
747 if (old->revision == me->revision && me->family == AF_UNSPEC)
748 return;
749
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000750 /* Delete old one. */
751 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
752 *i = old->next;
753 }
754
755 if (me->size != XT_ALIGN(me->size)) {
756 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100757 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000758 exit(1);
759 }
760
761 /* Prepend to list. */
762 me->next = xtables_targets;
763 xtables_targets = me;
764 me->t = NULL;
765 me->tflags = 0;
766}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000767
Jan Engelhardta41545c2009-01-27 21:27:19 +0100768/**
769 * xtables_param_act - act on condition
770 * @status: a constant from enum xtables_exittype
771 *
772 * %XTF_ONLY_ONCE: print error message that option may only be used once.
773 * @p1: module name (e.g. "mark")
774 * @p2(...): option in conflict (e.g. "--mark")
775 * @p3(...): condition to match on (see extensions/ for examples)
776 *
777 * %XTF_NO_INVERT: option does not support inversion
778 * @p1: module name
779 * @p2: option in conflict
780 * @p3: condition to match on
781 *
782 * %XTF_BAD_VALUE: bad value for option
783 * @p1: module name
784 * @p2: option with which the problem occured (e.g. "--mark")
785 * @p3: string the user passed in (e.g. "99999999999999")
786 *
787 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
788 * @p1: module name
789 *
790 * Displays an error message and exits the program.
791 */
792void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000793{
794 const char *p2, *p3;
795 va_list args;
796 bool b;
797
798 va_start(args, p1);
799
800 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100801 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000802 p2 = va_arg(args, const char *);
803 b = va_arg(args, unsigned int);
804 if (!b)
805 return;
806 exit_error(PARAMETER_PROBLEM,
807 "%s: \"%s\" option may only be specified once",
808 p1, p2);
809 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100810 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000811 p2 = va_arg(args, const char *);
812 b = va_arg(args, unsigned int);
813 if (!b)
814 return;
815 exit_error(PARAMETER_PROBLEM,
816 "%s: \"%s\" option cannot be inverted", p1, p2);
817 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100818 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000819 p2 = va_arg(args, const char *);
820 p3 = va_arg(args, const char *);
821 exit_error(PARAMETER_PROBLEM,
822 "%s: Bad value for \"%s\" option: \"%s\"",
823 p1, p2, p3);
824 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100825 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000826 b = va_arg(args, unsigned int);
827 if (!b)
828 return;
829 exit_error(PARAMETER_PROBLEM,
830 "%s: At most one action is possible", p1);
831 break;
832 default:
833 exit_error(status, p1, args);
834 break;
835 }
836
837 va_end(args);
838}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000839
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100840const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000841{
842 static char buf[20];
843 const unsigned char *bytep = (const void *)&addrp->s_addr;
844
845 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
846 return buf;
847}
848
849static const char *ipaddr_to_host(const struct in_addr *addr)
850{
851 struct hostent *host;
852
853 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
854 if (host == NULL)
855 return NULL;
856
857 return host->h_name;
858}
859
860static const char *ipaddr_to_network(const struct in_addr *addr)
861{
862 struct netent *net;
863
864 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
865 return net->n_name;
866
867 return NULL;
868}
869
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100870const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000871{
872 const char *name;
873
874 if ((name = ipaddr_to_host(addr)) != NULL ||
875 (name = ipaddr_to_network(addr)) != NULL)
876 return name;
877
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100878 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000879}
880
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100881const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000882{
883 static char buf[20];
884 uint32_t maskaddr, bits;
885 int i;
886
887 maskaddr = ntohl(mask->s_addr);
888
889 if (maskaddr == 0xFFFFFFFFL)
890 /* we don't want to see "/32" */
891 return "";
892
893 i = 32;
894 bits = 0xFFFFFFFEL;
895 while (--i >= 0 && maskaddr != bits)
896 bits <<= 1;
897 if (i >= 0)
898 sprintf(buf, "/%d", i);
899 else
900 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100901 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000902
903 return buf;
904}
905
Jan Engelhardtbd943842008-01-20 13:38:08 +0000906static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
907{
908 static struct in_addr addr;
909 unsigned char *addrp;
910 unsigned int onebyte;
911 char buf[20], *p, *q;
912 int i;
913
914 /* copy dotted string, because we need to modify it */
915 strncpy(buf, dotted, sizeof(buf) - 1);
916 buf[sizeof(buf) - 1] = '\0';
917 addrp = (void *)&addr.s_addr;
918
919 p = buf;
920 for (i = 0; i < 3; ++i) {
921 if ((q = strchr(p, '.')) == NULL) {
922 if (is_mask)
923 return NULL;
924
925 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100926 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000927 return NULL;
928
929 addrp[i] = onebyte;
930 while (i < 3)
931 addrp[++i] = 0;
932
933 return &addr;
934 }
935
936 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100937 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000938 return NULL;
939
940 addrp[i] = onebyte;
941 p = q + 1;
942 }
943
944 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100945 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000946 return NULL;
947
948 addrp[3] = onebyte;
949 return &addr;
950}
951
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100952struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000953{
954 return __numeric_to_ipaddr(dotted, false);
955}
956
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100957struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000958{
959 return __numeric_to_ipaddr(dotted, true);
960}
961
962static struct in_addr *network_to_ipaddr(const char *name)
963{
964 static struct in_addr addr;
965 struct netent *net;
966
967 if ((net = getnetbyname(name)) != NULL) {
968 if (net->n_addrtype != AF_INET)
969 return NULL;
970 addr.s_addr = htonl(net->n_net);
971 return &addr;
972 }
973
974 return NULL;
975}
976
977static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
978{
979 struct hostent *host;
980 struct in_addr *addr;
981 unsigned int i;
982
983 *naddr = 0;
984 if ((host = gethostbyname(name)) != NULL) {
985 if (host->h_addrtype != AF_INET ||
986 host->h_length != sizeof(struct in_addr))
987 return NULL;
988
989 while (host->h_addr_list[*naddr] != NULL)
990 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100991 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000992 for (i = 0; i < *naddr; i++)
993 memcpy(&addr[i], host->h_addr_list[i],
994 sizeof(struct in_addr));
995 return addr;
996 }
997
998 return NULL;
999}
1000
1001static struct in_addr *
1002ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1003{
1004 struct in_addr *addrptmp, *addrp;
1005
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001006 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001007 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001008 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001009 memcpy(addrp, addrptmp, sizeof(*addrp));
1010 *naddrs = 1;
1011 return addrp;
1012 }
1013 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1014 return addrptmp;
1015
1016 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1017}
1018
1019static struct in_addr *parse_ipmask(const char *mask)
1020{
1021 static struct in_addr maskaddr;
1022 struct in_addr *addrp;
1023 unsigned int bits;
1024
1025 if (mask == NULL) {
1026 /* no mask at all defaults to 32 bits */
1027 maskaddr.s_addr = 0xFFFFFFFF;
1028 return &maskaddr;
1029 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001030 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001031 /* dotted_to_addr already returns a network byte order addr */
1032 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001033 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001034 exit_error(PARAMETER_PROBLEM,
1035 "invalid mask `%s' specified", mask);
1036 if (bits != 0) {
1037 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1038 return &maskaddr;
1039 }
1040
1041 maskaddr.s_addr = 0U;
1042 return &maskaddr;
1043}
1044
Jan Engelhardta0baae82009-01-30 04:32:50 +01001045/**
1046 * xtables_ipparse_any - transform arbitrary name to in_addr
1047 *
1048 * Possible inputs (pseudo regex):
1049 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1050 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1051 */
1052void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1053 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001054{
1055 unsigned int i, j, k, n;
1056 struct in_addr *addrp;
1057 char buf[256], *p;
1058
1059 strncpy(buf, name, sizeof(buf) - 1);
1060 buf[sizeof(buf) - 1] = '\0';
1061 if ((p = strrchr(buf, '/')) != NULL) {
1062 *p = '\0';
1063 addrp = parse_ipmask(p + 1);
1064 } else {
1065 addrp = parse_ipmask(NULL);
1066 }
1067 memcpy(maskp, addrp, sizeof(*maskp));
1068
1069 /* if a null mask is given, the name is ignored, like in "any/0" */
1070 if (maskp->s_addr == 0U)
1071 strcpy(buf, "0.0.0.0");
1072
1073 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1074 n = *naddrs;
1075 for (i = 0, j = 0; i < n; ++i) {
1076 addrp[j++].s_addr &= maskp->s_addr;
1077 for (k = 0; k < j - 1; ++k)
1078 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1079 --*naddrs;
1080 --j;
1081 break;
1082 }
1083 }
1084}
1085
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001086const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001087{
1088 /* 0000:0000:0000:0000:0000:000.000.000.000
1089 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1090 static char buf[50+1];
1091 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1092}
1093
1094static const char *ip6addr_to_host(const struct in6_addr *addr)
1095{
1096 static char hostname[NI_MAXHOST];
1097 struct sockaddr_in6 saddr;
1098 int err;
1099
1100 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1101 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1102 saddr.sin6_family = AF_INET6;
1103
1104 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1105 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1106 if (err != 0) {
1107#ifdef DEBUG
1108 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1109#endif
1110 return NULL;
1111 }
1112
1113#ifdef DEBUG
1114 fprintf (stderr, "\naddr2host: %s\n", hostname);
1115#endif
1116 return hostname;
1117}
1118
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001119const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001120{
1121 const char *name;
1122
1123 if ((name = ip6addr_to_host(addr)) != NULL)
1124 return name;
1125
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001126 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001127}
1128
1129static int ip6addr_prefix_length(const struct in6_addr *k)
1130{
1131 unsigned int bits = 0;
1132 uint32_t a, b, c, d;
1133
Jan Engelhardt48607812008-06-10 15:17:53 +02001134 a = ntohl(k->s6_addr32[0]);
1135 b = ntohl(k->s6_addr32[1]);
1136 c = ntohl(k->s6_addr32[2]);
1137 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001138 while (a & 0x80000000U) {
1139 ++bits;
1140 a <<= 1;
1141 a |= (b >> 31) & 1;
1142 b <<= 1;
1143 b |= (c >> 31) & 1;
1144 c <<= 1;
1145 c |= (d >> 31) & 1;
1146 d <<= 1;
1147 }
1148 if (a != 0 || b != 0 || c != 0 || d != 0)
1149 return -1;
1150 return bits;
1151}
1152
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001153const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001154{
1155 static char buf[50+2];
1156 int l = ip6addr_prefix_length(addrp);
1157
1158 if (l == -1) {
1159 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001160 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001161 return buf;
1162 }
1163 sprintf(buf, "/%d", l);
1164 return buf;
1165}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001166
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001167struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001168{
1169 static struct in6_addr ap;
1170 int err;
1171
1172 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1173 return &ap;
1174#ifdef DEBUG
1175 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1176#endif
1177 return NULL;
1178}
1179
1180static struct in6_addr *
1181host_to_ip6addr(const char *name, unsigned int *naddr)
1182{
1183 static struct in6_addr *addr;
1184 struct addrinfo hints;
1185 struct addrinfo *res;
1186 int err;
1187
1188 memset(&hints, 0, sizeof(hints));
1189 hints.ai_flags = AI_CANONNAME;
1190 hints.ai_family = AF_INET6;
1191 hints.ai_socktype = SOCK_RAW;
1192 hints.ai_protocol = IPPROTO_IPV6;
1193 hints.ai_next = NULL;
1194
1195 *naddr = 0;
1196 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1197#ifdef DEBUG
1198 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1199#endif
1200 return NULL;
1201 } else {
1202 if (res->ai_family != AF_INET6 ||
1203 res->ai_addrlen != sizeof(struct sockaddr_in6))
1204 return NULL;
1205
1206#ifdef DEBUG
1207 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1208 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1209#endif
1210 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001211 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001212 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1213 sizeof(struct in6_addr));
1214 freeaddrinfo(res);
1215 *naddr = 1;
1216 return addr;
1217 }
1218
1219 return NULL;
1220}
1221
1222static struct in6_addr *network_to_ip6addr(const char *name)
1223{
1224 /* abort();*/
1225 /* TODO: not implemented yet, but the exception breaks the
1226 * name resolvation */
1227 return NULL;
1228}
1229
1230static struct in6_addr *
1231ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1232{
1233 struct in6_addr *addrp, *addrptmp;
1234
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001235 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001236 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001237 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001238 memcpy(addrp, addrptmp, sizeof(*addrp));
1239 *naddrs = 1;
1240 return addrp;
1241 }
1242 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1243 return addrp;
1244
1245 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1246}
1247
1248static struct in6_addr *parse_ip6mask(char *mask)
1249{
1250 static struct in6_addr maskaddr;
1251 struct in6_addr *addrp;
1252 unsigned int bits;
1253
1254 if (mask == NULL) {
1255 /* no mask at all defaults to 128 bits */
1256 memset(&maskaddr, 0xff, sizeof maskaddr);
1257 return &maskaddr;
1258 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001259 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001260 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001261 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001262 exit_error(PARAMETER_PROBLEM,
1263 "invalid mask `%s' specified", mask);
1264 if (bits != 0) {
1265 char *p = (void *)&maskaddr;
1266 memset(p, 0xff, bits / 8);
1267 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1268 p[bits/8] = 0xff << (8 - (bits & 7));
1269 return &maskaddr;
1270 }
1271
1272 memset(&maskaddr, 0, sizeof(maskaddr));
1273 return &maskaddr;
1274}
1275
Jan Engelhardta0baae82009-01-30 04:32:50 +01001276void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1277 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001278{
1279 struct in6_addr *addrp;
1280 unsigned int i, j, k, n;
1281 char buf[256], *p;
1282
1283 strncpy(buf, name, sizeof(buf) - 1);
1284 buf[sizeof(buf)-1] = '\0';
1285 if ((p = strrchr(buf, '/')) != NULL) {
1286 *p = '\0';
1287 addrp = parse_ip6mask(p + 1);
1288 } else {
1289 addrp = parse_ip6mask(NULL);
1290 }
1291 memcpy(maskp, addrp, sizeof(*maskp));
1292
1293 /* if a null mask is given, the name is ignored, like in "any/0" */
1294 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1295 strcpy(buf, "::");
1296
1297 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1298 n = *naddrs;
1299 for (i = 0, j = 0; i < n; ++i) {
1300 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001301 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001302 ++j;
1303 for (k = 0; k < j - 1; ++k)
1304 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1305 --*naddrs;
1306 --j;
1307 break;
1308 }
1309 }
1310}
Max Kellermanna5d09942008-01-29 13:44:34 +00001311
Jan Engelhardta0baae82009-01-30 04:32:50 +01001312void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001313{
1314 static const char no_quote_chars[] = "_-0123456789"
1315 "abcdefghijklmnopqrstuvwxyz"
1316 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1317 static const char escape_chars[] = "\"\\'";
1318 size_t length;
1319 const char *p;
1320
1321 length = strcspn(value, no_quote_chars);
1322 if (length > 0 && value[length] == 0) {
1323 /* no quoting required */
1324 fputs(value, stdout);
1325 putchar(' ');
1326 } else {
1327 /* there is at least one dangerous character in the
1328 value, which we have to quote. Write double quotes
1329 around the value and escape special characters with
1330 a backslash */
1331 putchar('"');
1332
1333 for (p = strpbrk(value, escape_chars); p != NULL;
1334 p = strpbrk(value, escape_chars)) {
1335 if (p > value)
1336 fwrite(value, 1, p - value, stdout);
1337 putchar('\\');
1338 putchar(*p);
1339 value = p + 1;
1340 }
1341
1342 /* print the rest and finish the double quoted
1343 string */
1344 fputs(value, stdout);
1345 printf("\" ");
1346 }
1347}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001348
1349/**
1350 * Check for option-intrapositional negation.
1351 * Do not use in new code.
1352 */
1353int xtables_check_inverse(const char option[], int *invert,
1354 int *my_optind, int argc)
1355{
1356 if (option && strcmp(option, "!") == 0) {
1357 fprintf(stderr, "Using intrapositioned negation "
1358 "(`--option ! this`) is deprecated in favor of "
1359 "extrapositioned (`! --option this`).\n");
1360
1361 if (*invert)
1362 exit_error(PARAMETER_PROBLEM,
1363 "Multiple `!' flags not allowed");
1364 *invert = true;
1365 if (my_optind != NULL) {
1366 ++*my_optind;
1367 if (argc && *my_optind > argc)
1368 exit_error(PARAMETER_PROBLEM,
1369 "no argument following `!'");
1370 }
1371
1372 return true;
1373 }
1374 return false;
1375}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001376
1377const struct xtables_pprot xtables_chain_protos[] = {
1378 {"tcp", IPPROTO_TCP},
1379 {"sctp", IPPROTO_SCTP},
1380 {"udp", IPPROTO_UDP},
1381 {"udplite", IPPROTO_UDPLITE},
1382 {"icmp", IPPROTO_ICMP},
1383 {"icmpv6", IPPROTO_ICMPV6},
1384 {"ipv6-icmp", IPPROTO_ICMPV6},
1385 {"esp", IPPROTO_ESP},
1386 {"ah", IPPROTO_AH},
1387 {"ipv6-mh", IPPROTO_MH},
1388 {"mh", IPPROTO_MH},
1389 {"all", 0},
1390 {NULL},
1391};
1392
1393u_int16_t
1394xtables_parse_protocol(const char *s)
1395{
1396 unsigned int proto;
1397
1398 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1399 struct protoent *pent;
1400
1401 /* first deal with the special case of 'all' to prevent
1402 * people from being able to redefine 'all' in nsswitch
1403 * and/or provoke expensive [not working] ldap/nis/...
1404 * lookups */
1405 if (!strcmp(s, "all"))
1406 return 0;
1407
1408 if ((pent = getprotobyname(s)))
1409 proto = pent->p_proto;
1410 else {
1411 unsigned int i;
1412 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1413 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1414 proto = xtables_chain_protos[i].num;
1415 break;
1416 }
1417 }
1418 if (i == ARRAY_SIZE(xtables_chain_protos))
1419 exit_error(PARAMETER_PROBLEM,
1420 "unknown protocol `%s' specified",
1421 s);
1422 }
1423 }
1424
1425 return proto;
1426}