blob: cf64352170c2bb63552f6c75cf70727eac3088b7 [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 Engelhardt1de7edf2009-01-30 05:38:11 +010035#include <ip6tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020036#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000037
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000038#ifndef NO_SHARED_LIBS
39#include <dlfcn.h>
40#endif
41
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000042#define NPROTO 255
43
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000044#ifndef PROC_SYS_MODPROBE
45#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
46#endif
47
Jan Engelhardtdacafa52009-01-27 20:56:23 +010048/**
49 * Program will set this to its own name.
50 */
51const char *xtables_program_name;
52
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010053/* Search path for Xtables .so files */
54static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000055
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000056/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010057const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000058
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000059/* Keeping track of external matches and targets: linked lists. */
60struct xtables_match *xtables_matches;
61struct xtables_target *xtables_targets;
62
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010063void xtables_init(void)
64{
65 xtables_libdir = getenv("XTABLES_LIBDIR");
66 if (xtables_libdir != NULL)
67 return;
68 xtables_libdir = getenv("IPTABLES_LIB_DIR");
69 if (xtables_libdir != NULL) {
70 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
71 "use XTABLES_LIBDIR.\n");
72 return;
73 }
74 xtables_libdir = XTABLES_LIBDIR;
75}
76
Jan Engelhardt630ef482009-01-27 14:58:41 +010077/**
78 * xtables_*alloc - wrappers that exit on failure
79 */
80void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000081{
82 void *p;
83
84 if ((p = calloc(count, size)) == NULL) {
85 perror("ip[6]tables: calloc failed");
86 exit(1);
87 }
88
89 return p;
90}
91
Jan Engelhardt630ef482009-01-27 14:58:41 +010092void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000093{
94 void *p;
95
96 if ((p = malloc(size)) == NULL) {
97 perror("ip[6]tables: malloc failed");
98 exit(1);
99 }
100
101 return p;
102}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000103
104static char *get_modprobe(void)
105{
106 int procfile;
107 char *ret;
108
109#define PROCFILE_BUFSIZ 1024
110 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
111 if (procfile < 0)
112 return NULL;
113
114 ret = (char *) malloc(PROCFILE_BUFSIZ);
115 if (ret) {
116 memset(ret, 0, PROCFILE_BUFSIZ);
117 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
118 case -1: goto fail;
119 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
120 }
121 if (ret[strlen(ret)-1]=='\n')
122 ret[strlen(ret)-1]=0;
123 close(procfile);
124 return ret;
125 }
126 fail:
127 free(ret);
128 close(procfile);
129 return NULL;
130}
131
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100132int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000133{
134 char *buf = NULL;
135 char *argv[4];
136 int status;
137
138 /* If they don't explicitly set it, read out of kernel */
139 if (!modprobe) {
140 buf = get_modprobe();
141 if (!buf)
142 return -1;
143 modprobe = buf;
144 }
145
146 switch (fork()) {
147 case 0:
148 argv[0] = (char *)modprobe;
149 argv[1] = (char *)modname;
150 if (quiet) {
151 argv[2] = "-q";
152 argv[3] = NULL;
153 } else {
154 argv[2] = NULL;
155 argv[3] = NULL;
156 }
157 execv(argv[0], argv);
158
159 /* not usually reached */
160 exit(1);
161 case -1:
162 return -1;
163
164 default: /* parent */
165 wait(&status);
166 }
167
168 free(buf);
169 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
170 return 0;
171 return -1;
172}
173
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100174int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000175{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100176 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000177 static int ret = -1;
178
179 if (!loaded) {
180 ret = xtables_insmod(afinfo.kmod, modprobe, quiet);
181 loaded = (ret == 0);
182 }
183
184 return ret;
185}
186
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100187/**
188 * xtables_strtou{i,l} - string to number conversion
189 * @s: input string
190 * @end: like strtoul's "end" pointer
191 * @value: pointer for result
192 * @min: minimum accepted value
193 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000194 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100195 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
196 * "15a" is rejected.
197 * In either case, the value obtained is compared for min-max compliance.
198 * Base is always 0, i.e. autodetect depending on @s.
199 *
200 * Returns true/false whether number was accepted. On failure, *value has
201 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000202 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100203bool xtables_strtoul(const char *s, char **end, unsigned long *value,
204 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000205{
206 unsigned long v;
207 char *my_end;
208
209 errno = 0;
210 v = strtoul(s, &my_end, 0);
211
212 if (my_end == s)
213 return false;
214 if (end != NULL)
215 *end = my_end;
216
217 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
218 if (value != NULL)
219 *value = v;
220 if (end == NULL)
221 return *my_end == '\0';
222 return true;
223 }
224
225 return false;
226}
227
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100228bool xtables_strtoui(const char *s, char **end, unsigned int *value,
229 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000230{
231 unsigned long v;
232 bool ret;
233
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100234 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000235 if (value != NULL)
236 *value = v;
237 return ret;
238}
239
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100240int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000241{
242 struct servent *service;
243
244 if ((service = getservbyname(name, proto)) != NULL)
245 return ntohs((unsigned short) service->s_port);
246
247 return -1;
248}
249
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100250u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000251{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100252 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000253
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100254 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100255 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100256 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000257
258 exit_error(PARAMETER_PROBLEM,
259 "invalid port/service `%s' specified", port);
260}
261
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100262void xtables_parse_interface(const char *arg, char *vianame,
263 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000264{
265 int vialen = strlen(arg);
266 unsigned int i;
267
268 memset(mask, 0, IFNAMSIZ);
269 memset(vianame, 0, IFNAMSIZ);
270
271 if (vialen + 1 > IFNAMSIZ)
272 exit_error(PARAMETER_PROBLEM,
273 "interface name `%s' must be shorter than IFNAMSIZ"
274 " (%i)", arg, IFNAMSIZ-1);
275
276 strcpy(vianame, arg);
277 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
278 memset(mask, 0, IFNAMSIZ);
279 else if (vianame[vialen - 1] == '+') {
280 memset(mask, 0xFF, vialen - 1);
281 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
282 /* Don't remove `+' here! -HW */
283 } else {
284 /* Include nul-terminator in match */
285 memset(mask, 0xFF, vialen + 1);
286 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
287 for (i = 0; vianame[i]; i++) {
288 if (vianame[i] == ':' ||
289 vianame[i] == '!' ||
290 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000291 fprintf(stderr,
292 "Warning: weird character in interface"
293 " `%s' (No aliases, :, ! or *).\n",
294 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000295 break;
296 }
297 }
298 }
299}
300
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200301#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100302static void *load_extension(const char *search_path, const char *prefix,
303 const char *name, bool is_target)
304{
305 const char *dir = search_path, *next;
306 void *ptr = NULL;
307 struct stat sb;
308 char path[256];
309
310 do {
311 next = strchr(dir, ':');
312 if (next == NULL)
313 next = dir + strlen(dir);
314 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200315 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100316
317 if (dlopen(path, RTLD_NOW) != NULL) {
318 /* Found library. If it didn't register itself,
319 maybe they specified target as match. */
320 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100321 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100322 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100323 ptr = xtables_find_match(name,
324 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100325 } else if (stat(path, &sb) == 0) {
326 fprintf(stderr, "%s: %s\n", path, dlerror());
327 }
328
329 if (ptr != NULL)
330 return ptr;
331
332 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200333 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100334 if (dlopen(path, RTLD_NOW) != NULL) {
335 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100336 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100337 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100338 ptr = xtables_find_match(name,
339 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100340 } else if (stat(path, &sb) == 0) {
341 fprintf(stderr, "%s: %s\n", path, dlerror());
342 }
343
344 if (ptr != NULL)
345 return ptr;
346
347 dir = next + 1;
348 } while (*next != '\0');
349
350 return NULL;
351}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200352#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100353
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100354struct xtables_match *
355xtables_find_match(const char *name, enum xtables_tryload tryload,
356 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000357{
358 struct xtables_match *ptr;
359 const char *icmp6 = "icmp6";
360
361 /* This is ugly as hell. Nonetheless, there is no way of changing
362 * this without hurting backwards compatibility */
363 if ( (strcmp(name,"icmpv6") == 0) ||
364 (strcmp(name,"ipv6-icmp") == 0) ||
365 (strcmp(name,"icmp6") == 0) )
366 name = icmp6;
367
368 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
369 if (strcmp(name, ptr->name) == 0) {
370 struct xtables_match *clone;
371
372 /* First match of this type: */
373 if (ptr->m == NULL)
374 break;
375
376 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100377 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000378 memcpy(clone, ptr, sizeof(struct xtables_match));
379 clone->mflags = 0;
380 /* This is a clone: */
381 clone->next = clone;
382
383 ptr = clone;
384 break;
385 }
386 }
387
388#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100389 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100390 ptr = load_extension(xtables_libdir, afinfo.libprefix,
391 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000392
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100393 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000394 exit_error(PARAMETER_PROBLEM,
395 "Couldn't load match `%s':%s\n",
396 name, dlerror());
397 }
398#else
399 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100400 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000401 ptr->loaded = 1;
402 else
403 ptr = NULL;
404 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100405 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000406 exit_error(PARAMETER_PROBLEM,
407 "Couldn't find match `%s'\n", name);
408 }
409#endif
410
411 if (ptr && matches) {
412 struct xtables_rule_match **i;
413 struct xtables_rule_match *newentry;
414
Jan Engelhardt630ef482009-01-27 14:58:41 +0100415 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000416
417 for (i = matches; *i; i = &(*i)->next) {
418 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100419 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000420 }
421 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100422 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000423 newentry->next = NULL;
424 *i = newentry;
425 }
426
427 return ptr;
428}
429
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100430struct xtables_target *
431xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000432{
433 struct xtables_target *ptr;
434
435 /* Standard target? */
436 if (strcmp(name, "") == 0
437 || strcmp(name, XTC_LABEL_ACCEPT) == 0
438 || strcmp(name, XTC_LABEL_DROP) == 0
439 || strcmp(name, XTC_LABEL_QUEUE) == 0
440 || strcmp(name, XTC_LABEL_RETURN) == 0)
441 name = "standard";
442
443 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
444 if (strcmp(name, ptr->name) == 0)
445 break;
446 }
447
448#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100449 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100450 ptr = load_extension(xtables_libdir, afinfo.libprefix,
451 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000452
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100453 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000454 exit_error(PARAMETER_PROBLEM,
455 "Couldn't load target `%s':%s\n",
456 name, dlerror());
457 }
458#else
459 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100460 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000461 ptr->loaded = 1;
462 else
463 ptr = NULL;
464 }
465 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
466 exit_error(PARAMETER_PROBLEM,
467 "Couldn't find target `%s'\n", name);
468 }
469#endif
470
471 if (ptr)
472 ptr->used = 1;
473
474 return ptr;
475}
476
477static int compatible_revision(const char *name, u_int8_t revision, int opt)
478{
479 struct xt_get_revision rev;
480 socklen_t s = sizeof(rev);
481 int max_rev, sockfd;
482
483 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
484 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000485 if (errno == EPERM) {
486 /* revision 0 is always supported. */
487 if (revision != 0)
488 fprintf(stderr, "Could not determine whether "
489 "revision %u is supported, "
490 "assuming it is.\n",
491 revision);
492 return 1;
493 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000494 fprintf(stderr, "Could not open socket to kernel: %s\n",
495 strerror(errno));
496 exit(1);
497 }
498
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100499 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000500
501 strcpy(rev.name, name);
502 rev.revision = revision;
503
504 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
505 if (max_rev < 0) {
506 /* Definitely don't support this? */
507 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
508 close(sockfd);
509 return 0;
510 } else if (errno == ENOPROTOOPT) {
511 close(sockfd);
512 /* Assume only revision 0 support (old kernel) */
513 return (revision == 0);
514 } else {
515 fprintf(stderr, "getsockopt failed strangely: %s\n",
516 strerror(errno));
517 exit(1);
518 }
519 }
520 close(sockfd);
521 return 1;
522}
523
524
525static int compatible_match_revision(const char *name, u_int8_t revision)
526{
527 return compatible_revision(name, revision, afinfo.so_rev_match);
528}
529
530static int compatible_target_revision(const char *name, u_int8_t revision)
531{
532 return compatible_revision(name, revision, afinfo.so_rev_target);
533}
534
535void xtables_register_match(struct xtables_match *me)
536{
537 struct xtables_match **i, *old;
538
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100539 if (strcmp(me->version, XTABLES_VERSION) != 0) {
540 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
541 "but \"%s\" is required.\n",
542 xtables_program_name, me->name,
543 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000544 exit(1);
545 }
546
547 /* Revision field stole a char from name. */
548 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
549 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100550 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000551 exit(1);
552 }
553
554 if (me->family >= NPROTO) {
555 fprintf(stderr,
556 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100557 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000558 exit(1);
559 }
560
561 /* ignore not interested match */
Jan Engelhardt23545c22008-02-14 04:23:04 +0100562 if (me->family != afinfo.family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000563 return;
564
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100565 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000566 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100567 if (old->revision == me->revision &&
568 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000569 fprintf(stderr,
570 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100571 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000572 exit(1);
573 }
574
575 /* Now we have two (or more) options, check compatibility. */
576 if (compatible_match_revision(old->name, old->revision)
577 && old->revision > me->revision)
578 return;
579
Jan Engelhardt23545c22008-02-14 04:23:04 +0100580 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000581 if (!compatible_match_revision(me->name, me->revision))
582 return;
583
Jan Engelhardt23545c22008-02-14 04:23:04 +0100584 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
585 if (old->revision == me->revision && me->family == AF_UNSPEC)
586 return;
587
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000588 /* Delete old one. */
589 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
590 *i = old->next;
591 }
592
593 if (me->size != XT_ALIGN(me->size)) {
594 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100595 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000596 exit(1);
597 }
598
599 /* Append to list. */
600 for (i = &xtables_matches; *i; i = &(*i)->next);
601 me->next = NULL;
602 *i = me;
603
604 me->m = NULL;
605 me->mflags = 0;
606}
607
608void xtables_register_target(struct xtables_target *me)
609{
610 struct xtables_target *old;
611
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100612 if (strcmp(me->version, XTABLES_VERSION) != 0) {
613 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
614 "but \"%s\" is required.\n",
615 xtables_program_name, me->name,
616 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000617 exit(1);
618 }
619
620 /* Revision field stole a char from name. */
621 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
622 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100623 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000624 exit(1);
625 }
626
627 if (me->family >= NPROTO) {
628 fprintf(stderr,
629 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100630 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000631 exit(1);
632 }
633
634 /* ignore not interested target */
Jan Engelhardt23545c22008-02-14 04:23:04 +0100635 if (me->family != afinfo.family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000636 return;
637
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100638 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000639 if (old) {
640 struct xtables_target **i;
641
Jan Engelhardt23545c22008-02-14 04:23:04 +0100642 if (old->revision == me->revision &&
643 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000644 fprintf(stderr,
645 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100646 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000647 exit(1);
648 }
649
650 /* Now we have two (or more) options, check compatibility. */
651 if (compatible_target_revision(old->name, old->revision)
652 && old->revision > me->revision)
653 return;
654
Jan Engelhardt23545c22008-02-14 04:23:04 +0100655 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000656 if (!compatible_target_revision(me->name, me->revision))
657 return;
658
Jan Engelhardt23545c22008-02-14 04:23:04 +0100659 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
660 if (old->revision == me->revision && me->family == AF_UNSPEC)
661 return;
662
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000663 /* Delete old one. */
664 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
665 *i = old->next;
666 }
667
668 if (me->size != XT_ALIGN(me->size)) {
669 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100670 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000671 exit(1);
672 }
673
674 /* Prepend to list. */
675 me->next = xtables_targets;
676 xtables_targets = me;
677 me->t = NULL;
678 me->tflags = 0;
679}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000680
Jan Engelhardta41545c2009-01-27 21:27:19 +0100681/**
682 * xtables_param_act - act on condition
683 * @status: a constant from enum xtables_exittype
684 *
685 * %XTF_ONLY_ONCE: print error message that option may only be used once.
686 * @p1: module name (e.g. "mark")
687 * @p2(...): option in conflict (e.g. "--mark")
688 * @p3(...): condition to match on (see extensions/ for examples)
689 *
690 * %XTF_NO_INVERT: option does not support inversion
691 * @p1: module name
692 * @p2: option in conflict
693 * @p3: condition to match on
694 *
695 * %XTF_BAD_VALUE: bad value for option
696 * @p1: module name
697 * @p2: option with which the problem occured (e.g. "--mark")
698 * @p3: string the user passed in (e.g. "99999999999999")
699 *
700 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
701 * @p1: module name
702 *
703 * Displays an error message and exits the program.
704 */
705void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000706{
707 const char *p2, *p3;
708 va_list args;
709 bool b;
710
711 va_start(args, p1);
712
713 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100714 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000715 p2 = va_arg(args, const char *);
716 b = va_arg(args, unsigned int);
717 if (!b)
718 return;
719 exit_error(PARAMETER_PROBLEM,
720 "%s: \"%s\" option may only be specified once",
721 p1, p2);
722 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100723 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000724 p2 = va_arg(args, const char *);
725 b = va_arg(args, unsigned int);
726 if (!b)
727 return;
728 exit_error(PARAMETER_PROBLEM,
729 "%s: \"%s\" option cannot be inverted", p1, p2);
730 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100731 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000732 p2 = va_arg(args, const char *);
733 p3 = va_arg(args, const char *);
734 exit_error(PARAMETER_PROBLEM,
735 "%s: Bad value for \"%s\" option: \"%s\"",
736 p1, p2, p3);
737 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100738 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000739 b = va_arg(args, unsigned int);
740 if (!b)
741 return;
742 exit_error(PARAMETER_PROBLEM,
743 "%s: At most one action is possible", p1);
744 break;
745 default:
746 exit_error(status, p1, args);
747 break;
748 }
749
750 va_end(args);
751}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000752
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100753const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000754{
755 static char buf[20];
756 const unsigned char *bytep = (const void *)&addrp->s_addr;
757
758 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
759 return buf;
760}
761
762static const char *ipaddr_to_host(const struct in_addr *addr)
763{
764 struct hostent *host;
765
766 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
767 if (host == NULL)
768 return NULL;
769
770 return host->h_name;
771}
772
773static const char *ipaddr_to_network(const struct in_addr *addr)
774{
775 struct netent *net;
776
777 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
778 return net->n_name;
779
780 return NULL;
781}
782
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100783const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000784{
785 const char *name;
786
787 if ((name = ipaddr_to_host(addr)) != NULL ||
788 (name = ipaddr_to_network(addr)) != NULL)
789 return name;
790
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100791 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000792}
793
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100794const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000795{
796 static char buf[20];
797 uint32_t maskaddr, bits;
798 int i;
799
800 maskaddr = ntohl(mask->s_addr);
801
802 if (maskaddr == 0xFFFFFFFFL)
803 /* we don't want to see "/32" */
804 return "";
805
806 i = 32;
807 bits = 0xFFFFFFFEL;
808 while (--i >= 0 && maskaddr != bits)
809 bits <<= 1;
810 if (i >= 0)
811 sprintf(buf, "/%d", i);
812 else
813 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100814 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000815
816 return buf;
817}
818
Jan Engelhardtbd943842008-01-20 13:38:08 +0000819static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
820{
821 static struct in_addr addr;
822 unsigned char *addrp;
823 unsigned int onebyte;
824 char buf[20], *p, *q;
825 int i;
826
827 /* copy dotted string, because we need to modify it */
828 strncpy(buf, dotted, sizeof(buf) - 1);
829 buf[sizeof(buf) - 1] = '\0';
830 addrp = (void *)&addr.s_addr;
831
832 p = buf;
833 for (i = 0; i < 3; ++i) {
834 if ((q = strchr(p, '.')) == NULL) {
835 if (is_mask)
836 return NULL;
837
838 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100839 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000840 return NULL;
841
842 addrp[i] = onebyte;
843 while (i < 3)
844 addrp[++i] = 0;
845
846 return &addr;
847 }
848
849 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100850 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000851 return NULL;
852
853 addrp[i] = onebyte;
854 p = q + 1;
855 }
856
857 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100858 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000859 return NULL;
860
861 addrp[3] = onebyte;
862 return &addr;
863}
864
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100865struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000866{
867 return __numeric_to_ipaddr(dotted, false);
868}
869
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100870struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000871{
872 return __numeric_to_ipaddr(dotted, true);
873}
874
875static struct in_addr *network_to_ipaddr(const char *name)
876{
877 static struct in_addr addr;
878 struct netent *net;
879
880 if ((net = getnetbyname(name)) != NULL) {
881 if (net->n_addrtype != AF_INET)
882 return NULL;
883 addr.s_addr = htonl(net->n_net);
884 return &addr;
885 }
886
887 return NULL;
888}
889
890static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
891{
892 struct hostent *host;
893 struct in_addr *addr;
894 unsigned int i;
895
896 *naddr = 0;
897 if ((host = gethostbyname(name)) != NULL) {
898 if (host->h_addrtype != AF_INET ||
899 host->h_length != sizeof(struct in_addr))
900 return NULL;
901
902 while (host->h_addr_list[*naddr] != NULL)
903 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100904 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000905 for (i = 0; i < *naddr; i++)
906 memcpy(&addr[i], host->h_addr_list[i],
907 sizeof(struct in_addr));
908 return addr;
909 }
910
911 return NULL;
912}
913
914static struct in_addr *
915ipparse_hostnetwork(const char *name, unsigned int *naddrs)
916{
917 struct in_addr *addrptmp, *addrp;
918
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100919 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +0000920 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +0100921 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +0000922 memcpy(addrp, addrptmp, sizeof(*addrp));
923 *naddrs = 1;
924 return addrp;
925 }
926 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
927 return addrptmp;
928
929 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
930}
931
932static struct in_addr *parse_ipmask(const char *mask)
933{
934 static struct in_addr maskaddr;
935 struct in_addr *addrp;
936 unsigned int bits;
937
938 if (mask == NULL) {
939 /* no mask at all defaults to 32 bits */
940 maskaddr.s_addr = 0xFFFFFFFF;
941 return &maskaddr;
942 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100943 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000944 /* dotted_to_addr already returns a network byte order addr */
945 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100946 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000947 exit_error(PARAMETER_PROBLEM,
948 "invalid mask `%s' specified", mask);
949 if (bits != 0) {
950 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
951 return &maskaddr;
952 }
953
954 maskaddr.s_addr = 0U;
955 return &maskaddr;
956}
957
Jan Engelhardta0baae82009-01-30 04:32:50 +0100958/**
959 * xtables_ipparse_any - transform arbitrary name to in_addr
960 *
961 * Possible inputs (pseudo regex):
962 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
963 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
964 */
965void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
966 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000967{
968 unsigned int i, j, k, n;
969 struct in_addr *addrp;
970 char buf[256], *p;
971
972 strncpy(buf, name, sizeof(buf) - 1);
973 buf[sizeof(buf) - 1] = '\0';
974 if ((p = strrchr(buf, '/')) != NULL) {
975 *p = '\0';
976 addrp = parse_ipmask(p + 1);
977 } else {
978 addrp = parse_ipmask(NULL);
979 }
980 memcpy(maskp, addrp, sizeof(*maskp));
981
982 /* if a null mask is given, the name is ignored, like in "any/0" */
983 if (maskp->s_addr == 0U)
984 strcpy(buf, "0.0.0.0");
985
986 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
987 n = *naddrs;
988 for (i = 0, j = 0; i < n; ++i) {
989 addrp[j++].s_addr &= maskp->s_addr;
990 for (k = 0; k < j - 1; ++k)
991 if (addrp[k].s_addr == addrp[j-1].s_addr) {
992 --*naddrs;
993 --j;
994 break;
995 }
996 }
997}
998
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100999const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001000{
1001 /* 0000:0000:0000:0000:0000:000.000.000.000
1002 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1003 static char buf[50+1];
1004 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1005}
1006
1007static const char *ip6addr_to_host(const struct in6_addr *addr)
1008{
1009 static char hostname[NI_MAXHOST];
1010 struct sockaddr_in6 saddr;
1011 int err;
1012
1013 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1014 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1015 saddr.sin6_family = AF_INET6;
1016
1017 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1018 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1019 if (err != 0) {
1020#ifdef DEBUG
1021 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1022#endif
1023 return NULL;
1024 }
1025
1026#ifdef DEBUG
1027 fprintf (stderr, "\naddr2host: %s\n", hostname);
1028#endif
1029 return hostname;
1030}
1031
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001032const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001033{
1034 const char *name;
1035
1036 if ((name = ip6addr_to_host(addr)) != NULL)
1037 return name;
1038
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001039 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001040}
1041
1042static int ip6addr_prefix_length(const struct in6_addr *k)
1043{
1044 unsigned int bits = 0;
1045 uint32_t a, b, c, d;
1046
Jan Engelhardt48607812008-06-10 15:17:53 +02001047 a = ntohl(k->s6_addr32[0]);
1048 b = ntohl(k->s6_addr32[1]);
1049 c = ntohl(k->s6_addr32[2]);
1050 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001051 while (a & 0x80000000U) {
1052 ++bits;
1053 a <<= 1;
1054 a |= (b >> 31) & 1;
1055 b <<= 1;
1056 b |= (c >> 31) & 1;
1057 c <<= 1;
1058 c |= (d >> 31) & 1;
1059 d <<= 1;
1060 }
1061 if (a != 0 || b != 0 || c != 0 || d != 0)
1062 return -1;
1063 return bits;
1064}
1065
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001066const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001067{
1068 static char buf[50+2];
1069 int l = ip6addr_prefix_length(addrp);
1070
1071 if (l == -1) {
1072 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001073 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001074 return buf;
1075 }
1076 sprintf(buf, "/%d", l);
1077 return buf;
1078}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001079
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001080struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001081{
1082 static struct in6_addr ap;
1083 int err;
1084
1085 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1086 return &ap;
1087#ifdef DEBUG
1088 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1089#endif
1090 return NULL;
1091}
1092
1093static struct in6_addr *
1094host_to_ip6addr(const char *name, unsigned int *naddr)
1095{
1096 static struct in6_addr *addr;
1097 struct addrinfo hints;
1098 struct addrinfo *res;
1099 int err;
1100
1101 memset(&hints, 0, sizeof(hints));
1102 hints.ai_flags = AI_CANONNAME;
1103 hints.ai_family = AF_INET6;
1104 hints.ai_socktype = SOCK_RAW;
1105 hints.ai_protocol = IPPROTO_IPV6;
1106 hints.ai_next = NULL;
1107
1108 *naddr = 0;
1109 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1110#ifdef DEBUG
1111 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1112#endif
1113 return NULL;
1114 } else {
1115 if (res->ai_family != AF_INET6 ||
1116 res->ai_addrlen != sizeof(struct sockaddr_in6))
1117 return NULL;
1118
1119#ifdef DEBUG
1120 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1121 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1122#endif
1123 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001124 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001125 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1126 sizeof(struct in6_addr));
1127 freeaddrinfo(res);
1128 *naddr = 1;
1129 return addr;
1130 }
1131
1132 return NULL;
1133}
1134
1135static struct in6_addr *network_to_ip6addr(const char *name)
1136{
1137 /* abort();*/
1138 /* TODO: not implemented yet, but the exception breaks the
1139 * name resolvation */
1140 return NULL;
1141}
1142
1143static struct in6_addr *
1144ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1145{
1146 struct in6_addr *addrp, *addrptmp;
1147
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001148 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001149 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001150 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001151 memcpy(addrp, addrptmp, sizeof(*addrp));
1152 *naddrs = 1;
1153 return addrp;
1154 }
1155 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1156 return addrp;
1157
1158 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1159}
1160
1161static struct in6_addr *parse_ip6mask(char *mask)
1162{
1163 static struct in6_addr maskaddr;
1164 struct in6_addr *addrp;
1165 unsigned int bits;
1166
1167 if (mask == NULL) {
1168 /* no mask at all defaults to 128 bits */
1169 memset(&maskaddr, 0xff, sizeof maskaddr);
1170 return &maskaddr;
1171 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001172 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001173 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001174 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001175 exit_error(PARAMETER_PROBLEM,
1176 "invalid mask `%s' specified", mask);
1177 if (bits != 0) {
1178 char *p = (void *)&maskaddr;
1179 memset(p, 0xff, bits / 8);
1180 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1181 p[bits/8] = 0xff << (8 - (bits & 7));
1182 return &maskaddr;
1183 }
1184
1185 memset(&maskaddr, 0, sizeof(maskaddr));
1186 return &maskaddr;
1187}
1188
Jan Engelhardta0baae82009-01-30 04:32:50 +01001189void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1190 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001191{
1192 struct in6_addr *addrp;
1193 unsigned int i, j, k, n;
1194 char buf[256], *p;
1195
1196 strncpy(buf, name, sizeof(buf) - 1);
1197 buf[sizeof(buf)-1] = '\0';
1198 if ((p = strrchr(buf, '/')) != NULL) {
1199 *p = '\0';
1200 addrp = parse_ip6mask(p + 1);
1201 } else {
1202 addrp = parse_ip6mask(NULL);
1203 }
1204 memcpy(maskp, addrp, sizeof(*maskp));
1205
1206 /* if a null mask is given, the name is ignored, like in "any/0" */
1207 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1208 strcpy(buf, "::");
1209
1210 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1211 n = *naddrs;
1212 for (i = 0, j = 0; i < n; ++i) {
1213 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001214 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001215 ++j;
1216 for (k = 0; k < j - 1; ++k)
1217 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1218 --*naddrs;
1219 --j;
1220 break;
1221 }
1222 }
1223}
Max Kellermanna5d09942008-01-29 13:44:34 +00001224
Jan Engelhardta0baae82009-01-30 04:32:50 +01001225void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001226{
1227 static const char no_quote_chars[] = "_-0123456789"
1228 "abcdefghijklmnopqrstuvwxyz"
1229 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1230 static const char escape_chars[] = "\"\\'";
1231 size_t length;
1232 const char *p;
1233
1234 length = strcspn(value, no_quote_chars);
1235 if (length > 0 && value[length] == 0) {
1236 /* no quoting required */
1237 fputs(value, stdout);
1238 putchar(' ');
1239 } else {
1240 /* there is at least one dangerous character in the
1241 value, which we have to quote. Write double quotes
1242 around the value and escape special characters with
1243 a backslash */
1244 putchar('"');
1245
1246 for (p = strpbrk(value, escape_chars); p != NULL;
1247 p = strpbrk(value, escape_chars)) {
1248 if (p > value)
1249 fwrite(value, 1, p - value, stdout);
1250 putchar('\\');
1251 putchar(*p);
1252 value = p + 1;
1253 }
1254
1255 /* print the rest and finish the double quoted
1256 string */
1257 fputs(value, stdout);
1258 printf("\" ");
1259 }
1260}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001261
1262/**
1263 * Check for option-intrapositional negation.
1264 * Do not use in new code.
1265 */
1266int xtables_check_inverse(const char option[], int *invert,
1267 int *my_optind, int argc)
1268{
1269 if (option && strcmp(option, "!") == 0) {
1270 fprintf(stderr, "Using intrapositioned negation "
1271 "(`--option ! this`) is deprecated in favor of "
1272 "extrapositioned (`! --option this`).\n");
1273
1274 if (*invert)
1275 exit_error(PARAMETER_PROBLEM,
1276 "Multiple `!' flags not allowed");
1277 *invert = true;
1278 if (my_optind != NULL) {
1279 ++*my_optind;
1280 if (argc && *my_optind > argc)
1281 exit_error(PARAMETER_PROBLEM,
1282 "no argument following `!'");
1283 }
1284
1285 return true;
1286 }
1287 return false;
1288}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001289
1290const struct xtables_pprot xtables_chain_protos[] = {
1291 {"tcp", IPPROTO_TCP},
1292 {"sctp", IPPROTO_SCTP},
1293 {"udp", IPPROTO_UDP},
1294 {"udplite", IPPROTO_UDPLITE},
1295 {"icmp", IPPROTO_ICMP},
1296 {"icmpv6", IPPROTO_ICMPV6},
1297 {"ipv6-icmp", IPPROTO_ICMPV6},
1298 {"esp", IPPROTO_ESP},
1299 {"ah", IPPROTO_AH},
1300 {"ipv6-mh", IPPROTO_MH},
1301 {"mh", IPPROTO_MH},
1302 {"all", 0},
1303 {NULL},
1304};
1305
1306u_int16_t
1307xtables_parse_protocol(const char *s)
1308{
1309 unsigned int proto;
1310
1311 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1312 struct protoent *pent;
1313
1314 /* first deal with the special case of 'all' to prevent
1315 * people from being able to redefine 'all' in nsswitch
1316 * and/or provoke expensive [not working] ldap/nis/...
1317 * lookups */
1318 if (!strcmp(s, "all"))
1319 return 0;
1320
1321 if ((pent = getprotobyname(s)))
1322 proto = pent->p_proto;
1323 else {
1324 unsigned int i;
1325 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1326 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1327 proto = xtables_chain_protos[i].num;
1328 break;
1329 }
1330 }
1331 if (i == ARRAY_SIZE(xtables_chain_protos))
1332 exit_error(PARAMETER_PROBLEM,
1333 "unknown protocol `%s' specified",
1334 s);
1335 }
1336 }
1337
1338 return proto;
1339}