blob: 6c954754959603dfe2b3fe4d87b3f6d30db5d344 [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
Jan Engelhardtdacafa52009-01-27 20:56:23 +010049/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +010050 * xtables_afinfo - protocol family dependent information
51 * @kmod: kernel module basename (e.g. "ip_tables")
52 * @libprefix: prefix of .so library name (e.g. "libipt_")
53 * @family: nfproto family
54 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
55 * @so_rev_match: optname to check revision support of match
56 * @so_rev_target: optname to check revision support of target
57 */
58struct xtables_afinfo {
59 const char *kmod;
60 const char *libprefix;
61 uint8_t family;
62 uint8_t ipproto;
63 int so_rev_match;
64 int so_rev_target;
65};
66
67static const struct xtables_afinfo afinfo_ipv4 = {
68 .kmod = "ip_tables",
69 .libprefix = "libipt_",
70 .family = NFPROTO_IPV4,
71 .ipproto = IPPROTO_IP,
72 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
73 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
74};
75
76static const struct xtables_afinfo afinfo_ipv6 = {
77 .kmod = "ip6_tables",
78 .libprefix = "libip6t_",
79 .family = NFPROTO_IPV6,
80 .ipproto = IPPROTO_IPV6,
81 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
82 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
83};
84
85static const struct xtables_afinfo *afinfo;
86
87/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +010088 * Program will set this to its own name.
89 */
90const char *xtables_program_name;
91
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010092/* Search path for Xtables .so files */
93static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000094
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000095/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010096const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000097
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000098/* Keeping track of external matches and targets: linked lists. */
99struct xtables_match *xtables_matches;
100struct xtables_target *xtables_targets;
101
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100102void xtables_init(void)
103{
104 xtables_libdir = getenv("XTABLES_LIBDIR");
105 if (xtables_libdir != NULL)
106 return;
107 xtables_libdir = getenv("IPTABLES_LIB_DIR");
108 if (xtables_libdir != NULL) {
109 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
110 "use XTABLES_LIBDIR.\n");
111 return;
112 }
113 xtables_libdir = XTABLES_LIBDIR;
114}
115
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100116void xtables_set_nfproto(uint8_t nfproto)
117{
118 switch (nfproto) {
119 case NFPROTO_IPV4:
120 afinfo = &afinfo_ipv4;
121 break;
122 case NFPROTO_IPV6:
123 afinfo = &afinfo_ipv6;
124 break;
125 default:
126 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
127 __func__);
128 }
129}
130
Jan Engelhardt630ef482009-01-27 14:58:41 +0100131/**
132 * xtables_*alloc - wrappers that exit on failure
133 */
134void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000135{
136 void *p;
137
138 if ((p = calloc(count, size)) == NULL) {
139 perror("ip[6]tables: calloc failed");
140 exit(1);
141 }
142
143 return p;
144}
145
Jan Engelhardt630ef482009-01-27 14:58:41 +0100146void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000147{
148 void *p;
149
150 if ((p = malloc(size)) == NULL) {
151 perror("ip[6]tables: malloc failed");
152 exit(1);
153 }
154
155 return p;
156}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000157
158static char *get_modprobe(void)
159{
160 int procfile;
161 char *ret;
162
163#define PROCFILE_BUFSIZ 1024
164 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
165 if (procfile < 0)
166 return NULL;
167
168 ret = (char *) malloc(PROCFILE_BUFSIZ);
169 if (ret) {
170 memset(ret, 0, PROCFILE_BUFSIZ);
171 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
172 case -1: goto fail;
173 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
174 }
175 if (ret[strlen(ret)-1]=='\n')
176 ret[strlen(ret)-1]=0;
177 close(procfile);
178 return ret;
179 }
180 fail:
181 free(ret);
182 close(procfile);
183 return NULL;
184}
185
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100186int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000187{
188 char *buf = NULL;
189 char *argv[4];
190 int status;
191
192 /* If they don't explicitly set it, read out of kernel */
193 if (!modprobe) {
194 buf = get_modprobe();
195 if (!buf)
196 return -1;
197 modprobe = buf;
198 }
199
200 switch (fork()) {
201 case 0:
202 argv[0] = (char *)modprobe;
203 argv[1] = (char *)modname;
204 if (quiet) {
205 argv[2] = "-q";
206 argv[3] = NULL;
207 } else {
208 argv[2] = NULL;
209 argv[3] = NULL;
210 }
211 execv(argv[0], argv);
212
213 /* not usually reached */
214 exit(1);
215 case -1:
216 return -1;
217
218 default: /* parent */
219 wait(&status);
220 }
221
222 free(buf);
223 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
224 return 0;
225 return -1;
226}
227
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100228int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000229{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100230 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000231 static int ret = -1;
232
233 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100234 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000235 loaded = (ret == 0);
236 }
237
238 return ret;
239}
240
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100241/**
242 * xtables_strtou{i,l} - string to number conversion
243 * @s: input string
244 * @end: like strtoul's "end" pointer
245 * @value: pointer for result
246 * @min: minimum accepted value
247 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000248 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100249 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
250 * "15a" is rejected.
251 * In either case, the value obtained is compared for min-max compliance.
252 * Base is always 0, i.e. autodetect depending on @s.
253 *
254 * Returns true/false whether number was accepted. On failure, *value has
255 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000256 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100257bool xtables_strtoul(const char *s, char **end, unsigned long *value,
258 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000259{
260 unsigned long v;
261 char *my_end;
262
263 errno = 0;
264 v = strtoul(s, &my_end, 0);
265
266 if (my_end == s)
267 return false;
268 if (end != NULL)
269 *end = my_end;
270
271 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
272 if (value != NULL)
273 *value = v;
274 if (end == NULL)
275 return *my_end == '\0';
276 return true;
277 }
278
279 return false;
280}
281
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100282bool xtables_strtoui(const char *s, char **end, unsigned int *value,
283 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000284{
285 unsigned long v;
286 bool ret;
287
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100288 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000289 if (value != NULL)
290 *value = v;
291 return ret;
292}
293
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100294int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000295{
296 struct servent *service;
297
298 if ((service = getservbyname(name, proto)) != NULL)
299 return ntohs((unsigned short) service->s_port);
300
301 return -1;
302}
303
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100304u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000305{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100306 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000307
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100308 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100309 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100310 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000311
312 exit_error(PARAMETER_PROBLEM,
313 "invalid port/service `%s' specified", port);
314}
315
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100316void xtables_parse_interface(const char *arg, char *vianame,
317 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000318{
319 int vialen = strlen(arg);
320 unsigned int i;
321
322 memset(mask, 0, IFNAMSIZ);
323 memset(vianame, 0, IFNAMSIZ);
324
325 if (vialen + 1 > IFNAMSIZ)
326 exit_error(PARAMETER_PROBLEM,
327 "interface name `%s' must be shorter than IFNAMSIZ"
328 " (%i)", arg, IFNAMSIZ-1);
329
330 strcpy(vianame, arg);
331 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
332 memset(mask, 0, IFNAMSIZ);
333 else if (vianame[vialen - 1] == '+') {
334 memset(mask, 0xFF, vialen - 1);
335 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
336 /* Don't remove `+' here! -HW */
337 } else {
338 /* Include nul-terminator in match */
339 memset(mask, 0xFF, vialen + 1);
340 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
341 for (i = 0; vianame[i]; i++) {
342 if (vianame[i] == ':' ||
343 vianame[i] == '!' ||
344 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000345 fprintf(stderr,
346 "Warning: weird character in interface"
347 " `%s' (No aliases, :, ! or *).\n",
348 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000349 break;
350 }
351 }
352 }
353}
354
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200355#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100356static void *load_extension(const char *search_path, const char *prefix,
357 const char *name, bool is_target)
358{
359 const char *dir = search_path, *next;
360 void *ptr = NULL;
361 struct stat sb;
362 char path[256];
363
364 do {
365 next = strchr(dir, ':');
366 if (next == NULL)
367 next = dir + strlen(dir);
368 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200369 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100370
371 if (dlopen(path, RTLD_NOW) != NULL) {
372 /* Found library. If it didn't register itself,
373 maybe they specified target as match. */
374 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100375 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100376 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100377 ptr = xtables_find_match(name,
378 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100379 } else if (stat(path, &sb) == 0) {
380 fprintf(stderr, "%s: %s\n", path, dlerror());
381 }
382
383 if (ptr != NULL)
384 return ptr;
385
386 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200387 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100388 if (dlopen(path, RTLD_NOW) != NULL) {
389 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100390 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100391 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100392 ptr = xtables_find_match(name,
393 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100394 } else if (stat(path, &sb) == 0) {
395 fprintf(stderr, "%s: %s\n", path, dlerror());
396 }
397
398 if (ptr != NULL)
399 return ptr;
400
401 dir = next + 1;
402 } while (*next != '\0');
403
404 return NULL;
405}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200406#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100407
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100408struct xtables_match *
409xtables_find_match(const char *name, enum xtables_tryload tryload,
410 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000411{
412 struct xtables_match *ptr;
413 const char *icmp6 = "icmp6";
414
415 /* This is ugly as hell. Nonetheless, there is no way of changing
416 * this without hurting backwards compatibility */
417 if ( (strcmp(name,"icmpv6") == 0) ||
418 (strcmp(name,"ipv6-icmp") == 0) ||
419 (strcmp(name,"icmp6") == 0) )
420 name = icmp6;
421
422 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
423 if (strcmp(name, ptr->name) == 0) {
424 struct xtables_match *clone;
425
426 /* First match of this type: */
427 if (ptr->m == NULL)
428 break;
429
430 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100431 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000432 memcpy(clone, ptr, sizeof(struct xtables_match));
433 clone->mflags = 0;
434 /* This is a clone: */
435 clone->next = clone;
436
437 ptr = clone;
438 break;
439 }
440 }
441
442#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100443 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100444 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100445 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000446
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100447 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000448 exit_error(PARAMETER_PROBLEM,
449 "Couldn't load match `%s':%s\n",
450 name, dlerror());
451 }
452#else
453 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100454 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000455 ptr->loaded = 1;
456 else
457 ptr = NULL;
458 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100459 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000460 exit_error(PARAMETER_PROBLEM,
461 "Couldn't find match `%s'\n", name);
462 }
463#endif
464
465 if (ptr && matches) {
466 struct xtables_rule_match **i;
467 struct xtables_rule_match *newentry;
468
Jan Engelhardt630ef482009-01-27 14:58:41 +0100469 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000470
471 for (i = matches; *i; i = &(*i)->next) {
472 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100473 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000474 }
475 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100476 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000477 newentry->next = NULL;
478 *i = newentry;
479 }
480
481 return ptr;
482}
483
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100484struct xtables_target *
485xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000486{
487 struct xtables_target *ptr;
488
489 /* Standard target? */
490 if (strcmp(name, "") == 0
491 || strcmp(name, XTC_LABEL_ACCEPT) == 0
492 || strcmp(name, XTC_LABEL_DROP) == 0
493 || strcmp(name, XTC_LABEL_QUEUE) == 0
494 || strcmp(name, XTC_LABEL_RETURN) == 0)
495 name = "standard";
496
497 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
498 if (strcmp(name, ptr->name) == 0)
499 break;
500 }
501
502#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100503 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100504 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100505 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000506
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100507 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000508 exit_error(PARAMETER_PROBLEM,
509 "Couldn't load target `%s':%s\n",
510 name, dlerror());
511 }
512#else
513 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100514 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000515 ptr->loaded = 1;
516 else
517 ptr = NULL;
518 }
519 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
520 exit_error(PARAMETER_PROBLEM,
521 "Couldn't find target `%s'\n", name);
522 }
523#endif
524
525 if (ptr)
526 ptr->used = 1;
527
528 return ptr;
529}
530
531static int compatible_revision(const char *name, u_int8_t revision, int opt)
532{
533 struct xt_get_revision rev;
534 socklen_t s = sizeof(rev);
535 int max_rev, sockfd;
536
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100537 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000538 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000539 if (errno == EPERM) {
540 /* revision 0 is always supported. */
541 if (revision != 0)
542 fprintf(stderr, "Could not determine whether "
543 "revision %u is supported, "
544 "assuming it is.\n",
545 revision);
546 return 1;
547 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000548 fprintf(stderr, "Could not open socket to kernel: %s\n",
549 strerror(errno));
550 exit(1);
551 }
552
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100553 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000554
555 strcpy(rev.name, name);
556 rev.revision = revision;
557
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100558 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000559 if (max_rev < 0) {
560 /* Definitely don't support this? */
561 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
562 close(sockfd);
563 return 0;
564 } else if (errno == ENOPROTOOPT) {
565 close(sockfd);
566 /* Assume only revision 0 support (old kernel) */
567 return (revision == 0);
568 } else {
569 fprintf(stderr, "getsockopt failed strangely: %s\n",
570 strerror(errno));
571 exit(1);
572 }
573 }
574 close(sockfd);
575 return 1;
576}
577
578
579static int compatible_match_revision(const char *name, u_int8_t revision)
580{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100581 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000582}
583
584static int compatible_target_revision(const char *name, u_int8_t revision)
585{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100586 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000587}
588
589void xtables_register_match(struct xtables_match *me)
590{
591 struct xtables_match **i, *old;
592
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100593 if (strcmp(me->version, XTABLES_VERSION) != 0) {
594 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
595 "but \"%s\" is required.\n",
596 xtables_program_name, me->name,
597 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000598 exit(1);
599 }
600
601 /* Revision field stole a char from name. */
602 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
603 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100604 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000605 exit(1);
606 }
607
608 if (me->family >= NPROTO) {
609 fprintf(stderr,
610 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100611 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000612 exit(1);
613 }
614
615 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100616 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000617 return;
618
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100619 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000620 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100621 if (old->revision == me->revision &&
622 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000623 fprintf(stderr,
624 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100625 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000626 exit(1);
627 }
628
629 /* Now we have two (or more) options, check compatibility. */
630 if (compatible_match_revision(old->name, old->revision)
631 && old->revision > me->revision)
632 return;
633
Jan Engelhardt23545c22008-02-14 04:23:04 +0100634 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000635 if (!compatible_match_revision(me->name, me->revision))
636 return;
637
Jan Engelhardt23545c22008-02-14 04:23:04 +0100638 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
639 if (old->revision == me->revision && me->family == AF_UNSPEC)
640 return;
641
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000642 /* Delete old one. */
643 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
644 *i = old->next;
645 }
646
647 if (me->size != XT_ALIGN(me->size)) {
648 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100649 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000650 exit(1);
651 }
652
653 /* Append to list. */
654 for (i = &xtables_matches; *i; i = &(*i)->next);
655 me->next = NULL;
656 *i = me;
657
658 me->m = NULL;
659 me->mflags = 0;
660}
661
662void xtables_register_target(struct xtables_target *me)
663{
664 struct xtables_target *old;
665
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100666 if (strcmp(me->version, XTABLES_VERSION) != 0) {
667 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
668 "but \"%s\" is required.\n",
669 xtables_program_name, me->name,
670 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000671 exit(1);
672 }
673
674 /* Revision field stole a char from name. */
675 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
676 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100677 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000678 exit(1);
679 }
680
681 if (me->family >= NPROTO) {
682 fprintf(stderr,
683 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100684 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000685 exit(1);
686 }
687
688 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100689 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000690 return;
691
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100692 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000693 if (old) {
694 struct xtables_target **i;
695
Jan Engelhardt23545c22008-02-14 04:23:04 +0100696 if (old->revision == me->revision &&
697 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000698 fprintf(stderr,
699 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100700 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000701 exit(1);
702 }
703
704 /* Now we have two (or more) options, check compatibility. */
705 if (compatible_target_revision(old->name, old->revision)
706 && old->revision > me->revision)
707 return;
708
Jan Engelhardt23545c22008-02-14 04:23:04 +0100709 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000710 if (!compatible_target_revision(me->name, me->revision))
711 return;
712
Jan Engelhardt23545c22008-02-14 04:23:04 +0100713 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
714 if (old->revision == me->revision && me->family == AF_UNSPEC)
715 return;
716
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000717 /* Delete old one. */
718 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
719 *i = old->next;
720 }
721
722 if (me->size != XT_ALIGN(me->size)) {
723 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100724 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000725 exit(1);
726 }
727
728 /* Prepend to list. */
729 me->next = xtables_targets;
730 xtables_targets = me;
731 me->t = NULL;
732 me->tflags = 0;
733}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000734
Jan Engelhardta41545c2009-01-27 21:27:19 +0100735/**
736 * xtables_param_act - act on condition
737 * @status: a constant from enum xtables_exittype
738 *
739 * %XTF_ONLY_ONCE: print error message that option may only be used once.
740 * @p1: module name (e.g. "mark")
741 * @p2(...): option in conflict (e.g. "--mark")
742 * @p3(...): condition to match on (see extensions/ for examples)
743 *
744 * %XTF_NO_INVERT: option does not support inversion
745 * @p1: module name
746 * @p2: option in conflict
747 * @p3: condition to match on
748 *
749 * %XTF_BAD_VALUE: bad value for option
750 * @p1: module name
751 * @p2: option with which the problem occured (e.g. "--mark")
752 * @p3: string the user passed in (e.g. "99999999999999")
753 *
754 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
755 * @p1: module name
756 *
757 * Displays an error message and exits the program.
758 */
759void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000760{
761 const char *p2, *p3;
762 va_list args;
763 bool b;
764
765 va_start(args, p1);
766
767 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100768 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000769 p2 = va_arg(args, const char *);
770 b = va_arg(args, unsigned int);
771 if (!b)
772 return;
773 exit_error(PARAMETER_PROBLEM,
774 "%s: \"%s\" option may only be specified once",
775 p1, p2);
776 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100777 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000778 p2 = va_arg(args, const char *);
779 b = va_arg(args, unsigned int);
780 if (!b)
781 return;
782 exit_error(PARAMETER_PROBLEM,
783 "%s: \"%s\" option cannot be inverted", p1, p2);
784 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100785 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000786 p2 = va_arg(args, const char *);
787 p3 = va_arg(args, const char *);
788 exit_error(PARAMETER_PROBLEM,
789 "%s: Bad value for \"%s\" option: \"%s\"",
790 p1, p2, p3);
791 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100792 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000793 b = va_arg(args, unsigned int);
794 if (!b)
795 return;
796 exit_error(PARAMETER_PROBLEM,
797 "%s: At most one action is possible", p1);
798 break;
799 default:
800 exit_error(status, p1, args);
801 break;
802 }
803
804 va_end(args);
805}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000806
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100807const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000808{
809 static char buf[20];
810 const unsigned char *bytep = (const void *)&addrp->s_addr;
811
812 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
813 return buf;
814}
815
816static const char *ipaddr_to_host(const struct in_addr *addr)
817{
818 struct hostent *host;
819
820 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
821 if (host == NULL)
822 return NULL;
823
824 return host->h_name;
825}
826
827static const char *ipaddr_to_network(const struct in_addr *addr)
828{
829 struct netent *net;
830
831 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
832 return net->n_name;
833
834 return NULL;
835}
836
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100837const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000838{
839 const char *name;
840
841 if ((name = ipaddr_to_host(addr)) != NULL ||
842 (name = ipaddr_to_network(addr)) != NULL)
843 return name;
844
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100845 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000846}
847
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100848const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000849{
850 static char buf[20];
851 uint32_t maskaddr, bits;
852 int i;
853
854 maskaddr = ntohl(mask->s_addr);
855
856 if (maskaddr == 0xFFFFFFFFL)
857 /* we don't want to see "/32" */
858 return "";
859
860 i = 32;
861 bits = 0xFFFFFFFEL;
862 while (--i >= 0 && maskaddr != bits)
863 bits <<= 1;
864 if (i >= 0)
865 sprintf(buf, "/%d", i);
866 else
867 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100868 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000869
870 return buf;
871}
872
Jan Engelhardtbd943842008-01-20 13:38:08 +0000873static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
874{
875 static struct in_addr addr;
876 unsigned char *addrp;
877 unsigned int onebyte;
878 char buf[20], *p, *q;
879 int i;
880
881 /* copy dotted string, because we need to modify it */
882 strncpy(buf, dotted, sizeof(buf) - 1);
883 buf[sizeof(buf) - 1] = '\0';
884 addrp = (void *)&addr.s_addr;
885
886 p = buf;
887 for (i = 0; i < 3; ++i) {
888 if ((q = strchr(p, '.')) == NULL) {
889 if (is_mask)
890 return NULL;
891
892 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100893 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000894 return NULL;
895
896 addrp[i] = onebyte;
897 while (i < 3)
898 addrp[++i] = 0;
899
900 return &addr;
901 }
902
903 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100904 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000905 return NULL;
906
907 addrp[i] = onebyte;
908 p = q + 1;
909 }
910
911 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100912 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000913 return NULL;
914
915 addrp[3] = onebyte;
916 return &addr;
917}
918
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100919struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000920{
921 return __numeric_to_ipaddr(dotted, false);
922}
923
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100924struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000925{
926 return __numeric_to_ipaddr(dotted, true);
927}
928
929static struct in_addr *network_to_ipaddr(const char *name)
930{
931 static struct in_addr addr;
932 struct netent *net;
933
934 if ((net = getnetbyname(name)) != NULL) {
935 if (net->n_addrtype != AF_INET)
936 return NULL;
937 addr.s_addr = htonl(net->n_net);
938 return &addr;
939 }
940
941 return NULL;
942}
943
944static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
945{
946 struct hostent *host;
947 struct in_addr *addr;
948 unsigned int i;
949
950 *naddr = 0;
951 if ((host = gethostbyname(name)) != NULL) {
952 if (host->h_addrtype != AF_INET ||
953 host->h_length != sizeof(struct in_addr))
954 return NULL;
955
956 while (host->h_addr_list[*naddr] != NULL)
957 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100958 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000959 for (i = 0; i < *naddr; i++)
960 memcpy(&addr[i], host->h_addr_list[i],
961 sizeof(struct in_addr));
962 return addr;
963 }
964
965 return NULL;
966}
967
968static struct in_addr *
969ipparse_hostnetwork(const char *name, unsigned int *naddrs)
970{
971 struct in_addr *addrptmp, *addrp;
972
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100973 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +0000974 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +0100975 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +0000976 memcpy(addrp, addrptmp, sizeof(*addrp));
977 *naddrs = 1;
978 return addrp;
979 }
980 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
981 return addrptmp;
982
983 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
984}
985
986static struct in_addr *parse_ipmask(const char *mask)
987{
988 static struct in_addr maskaddr;
989 struct in_addr *addrp;
990 unsigned int bits;
991
992 if (mask == NULL) {
993 /* no mask at all defaults to 32 bits */
994 maskaddr.s_addr = 0xFFFFFFFF;
995 return &maskaddr;
996 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100997 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000998 /* dotted_to_addr already returns a network byte order addr */
999 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001000 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001001 exit_error(PARAMETER_PROBLEM,
1002 "invalid mask `%s' specified", mask);
1003 if (bits != 0) {
1004 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1005 return &maskaddr;
1006 }
1007
1008 maskaddr.s_addr = 0U;
1009 return &maskaddr;
1010}
1011
Jan Engelhardta0baae82009-01-30 04:32:50 +01001012/**
1013 * xtables_ipparse_any - transform arbitrary name to in_addr
1014 *
1015 * Possible inputs (pseudo regex):
1016 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1017 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1018 */
1019void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1020 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001021{
1022 unsigned int i, j, k, n;
1023 struct in_addr *addrp;
1024 char buf[256], *p;
1025
1026 strncpy(buf, name, sizeof(buf) - 1);
1027 buf[sizeof(buf) - 1] = '\0';
1028 if ((p = strrchr(buf, '/')) != NULL) {
1029 *p = '\0';
1030 addrp = parse_ipmask(p + 1);
1031 } else {
1032 addrp = parse_ipmask(NULL);
1033 }
1034 memcpy(maskp, addrp, sizeof(*maskp));
1035
1036 /* if a null mask is given, the name is ignored, like in "any/0" */
1037 if (maskp->s_addr == 0U)
1038 strcpy(buf, "0.0.0.0");
1039
1040 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1041 n = *naddrs;
1042 for (i = 0, j = 0; i < n; ++i) {
1043 addrp[j++].s_addr &= maskp->s_addr;
1044 for (k = 0; k < j - 1; ++k)
1045 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1046 --*naddrs;
1047 --j;
1048 break;
1049 }
1050 }
1051}
1052
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001053const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001054{
1055 /* 0000:0000:0000:0000:0000:000.000.000.000
1056 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1057 static char buf[50+1];
1058 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1059}
1060
1061static const char *ip6addr_to_host(const struct in6_addr *addr)
1062{
1063 static char hostname[NI_MAXHOST];
1064 struct sockaddr_in6 saddr;
1065 int err;
1066
1067 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1068 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1069 saddr.sin6_family = AF_INET6;
1070
1071 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1072 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1073 if (err != 0) {
1074#ifdef DEBUG
1075 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1076#endif
1077 return NULL;
1078 }
1079
1080#ifdef DEBUG
1081 fprintf (stderr, "\naddr2host: %s\n", hostname);
1082#endif
1083 return hostname;
1084}
1085
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001086const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001087{
1088 const char *name;
1089
1090 if ((name = ip6addr_to_host(addr)) != NULL)
1091 return name;
1092
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001093 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001094}
1095
1096static int ip6addr_prefix_length(const struct in6_addr *k)
1097{
1098 unsigned int bits = 0;
1099 uint32_t a, b, c, d;
1100
Jan Engelhardt48607812008-06-10 15:17:53 +02001101 a = ntohl(k->s6_addr32[0]);
1102 b = ntohl(k->s6_addr32[1]);
1103 c = ntohl(k->s6_addr32[2]);
1104 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001105 while (a & 0x80000000U) {
1106 ++bits;
1107 a <<= 1;
1108 a |= (b >> 31) & 1;
1109 b <<= 1;
1110 b |= (c >> 31) & 1;
1111 c <<= 1;
1112 c |= (d >> 31) & 1;
1113 d <<= 1;
1114 }
1115 if (a != 0 || b != 0 || c != 0 || d != 0)
1116 return -1;
1117 return bits;
1118}
1119
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001120const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001121{
1122 static char buf[50+2];
1123 int l = ip6addr_prefix_length(addrp);
1124
1125 if (l == -1) {
1126 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001127 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001128 return buf;
1129 }
1130 sprintf(buf, "/%d", l);
1131 return buf;
1132}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001133
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001134struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001135{
1136 static struct in6_addr ap;
1137 int err;
1138
1139 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1140 return &ap;
1141#ifdef DEBUG
1142 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1143#endif
1144 return NULL;
1145}
1146
1147static struct in6_addr *
1148host_to_ip6addr(const char *name, unsigned int *naddr)
1149{
1150 static struct in6_addr *addr;
1151 struct addrinfo hints;
1152 struct addrinfo *res;
1153 int err;
1154
1155 memset(&hints, 0, sizeof(hints));
1156 hints.ai_flags = AI_CANONNAME;
1157 hints.ai_family = AF_INET6;
1158 hints.ai_socktype = SOCK_RAW;
1159 hints.ai_protocol = IPPROTO_IPV6;
1160 hints.ai_next = NULL;
1161
1162 *naddr = 0;
1163 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1164#ifdef DEBUG
1165 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1166#endif
1167 return NULL;
1168 } else {
1169 if (res->ai_family != AF_INET6 ||
1170 res->ai_addrlen != sizeof(struct sockaddr_in6))
1171 return NULL;
1172
1173#ifdef DEBUG
1174 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1175 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1176#endif
1177 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001178 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001179 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1180 sizeof(struct in6_addr));
1181 freeaddrinfo(res);
1182 *naddr = 1;
1183 return addr;
1184 }
1185
1186 return NULL;
1187}
1188
1189static struct in6_addr *network_to_ip6addr(const char *name)
1190{
1191 /* abort();*/
1192 /* TODO: not implemented yet, but the exception breaks the
1193 * name resolvation */
1194 return NULL;
1195}
1196
1197static struct in6_addr *
1198ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1199{
1200 struct in6_addr *addrp, *addrptmp;
1201
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001202 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001203 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001204 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001205 memcpy(addrp, addrptmp, sizeof(*addrp));
1206 *naddrs = 1;
1207 return addrp;
1208 }
1209 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1210 return addrp;
1211
1212 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1213}
1214
1215static struct in6_addr *parse_ip6mask(char *mask)
1216{
1217 static struct in6_addr maskaddr;
1218 struct in6_addr *addrp;
1219 unsigned int bits;
1220
1221 if (mask == NULL) {
1222 /* no mask at all defaults to 128 bits */
1223 memset(&maskaddr, 0xff, sizeof maskaddr);
1224 return &maskaddr;
1225 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001226 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001227 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001228 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001229 exit_error(PARAMETER_PROBLEM,
1230 "invalid mask `%s' specified", mask);
1231 if (bits != 0) {
1232 char *p = (void *)&maskaddr;
1233 memset(p, 0xff, bits / 8);
1234 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1235 p[bits/8] = 0xff << (8 - (bits & 7));
1236 return &maskaddr;
1237 }
1238
1239 memset(&maskaddr, 0, sizeof(maskaddr));
1240 return &maskaddr;
1241}
1242
Jan Engelhardta0baae82009-01-30 04:32:50 +01001243void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1244 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001245{
1246 struct in6_addr *addrp;
1247 unsigned int i, j, k, n;
1248 char buf[256], *p;
1249
1250 strncpy(buf, name, sizeof(buf) - 1);
1251 buf[sizeof(buf)-1] = '\0';
1252 if ((p = strrchr(buf, '/')) != NULL) {
1253 *p = '\0';
1254 addrp = parse_ip6mask(p + 1);
1255 } else {
1256 addrp = parse_ip6mask(NULL);
1257 }
1258 memcpy(maskp, addrp, sizeof(*maskp));
1259
1260 /* if a null mask is given, the name is ignored, like in "any/0" */
1261 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1262 strcpy(buf, "::");
1263
1264 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1265 n = *naddrs;
1266 for (i = 0, j = 0; i < n; ++i) {
1267 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001268 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001269 ++j;
1270 for (k = 0; k < j - 1; ++k)
1271 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1272 --*naddrs;
1273 --j;
1274 break;
1275 }
1276 }
1277}
Max Kellermanna5d09942008-01-29 13:44:34 +00001278
Jan Engelhardta0baae82009-01-30 04:32:50 +01001279void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001280{
1281 static const char no_quote_chars[] = "_-0123456789"
1282 "abcdefghijklmnopqrstuvwxyz"
1283 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1284 static const char escape_chars[] = "\"\\'";
1285 size_t length;
1286 const char *p;
1287
1288 length = strcspn(value, no_quote_chars);
1289 if (length > 0 && value[length] == 0) {
1290 /* no quoting required */
1291 fputs(value, stdout);
1292 putchar(' ');
1293 } else {
1294 /* there is at least one dangerous character in the
1295 value, which we have to quote. Write double quotes
1296 around the value and escape special characters with
1297 a backslash */
1298 putchar('"');
1299
1300 for (p = strpbrk(value, escape_chars); p != NULL;
1301 p = strpbrk(value, escape_chars)) {
1302 if (p > value)
1303 fwrite(value, 1, p - value, stdout);
1304 putchar('\\');
1305 putchar(*p);
1306 value = p + 1;
1307 }
1308
1309 /* print the rest and finish the double quoted
1310 string */
1311 fputs(value, stdout);
1312 printf("\" ");
1313 }
1314}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001315
1316/**
1317 * Check for option-intrapositional negation.
1318 * Do not use in new code.
1319 */
1320int xtables_check_inverse(const char option[], int *invert,
1321 int *my_optind, int argc)
1322{
1323 if (option && strcmp(option, "!") == 0) {
1324 fprintf(stderr, "Using intrapositioned negation "
1325 "(`--option ! this`) is deprecated in favor of "
1326 "extrapositioned (`! --option this`).\n");
1327
1328 if (*invert)
1329 exit_error(PARAMETER_PROBLEM,
1330 "Multiple `!' flags not allowed");
1331 *invert = true;
1332 if (my_optind != NULL) {
1333 ++*my_optind;
1334 if (argc && *my_optind > argc)
1335 exit_error(PARAMETER_PROBLEM,
1336 "no argument following `!'");
1337 }
1338
1339 return true;
1340 }
1341 return false;
1342}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001343
1344const struct xtables_pprot xtables_chain_protos[] = {
1345 {"tcp", IPPROTO_TCP},
1346 {"sctp", IPPROTO_SCTP},
1347 {"udp", IPPROTO_UDP},
1348 {"udplite", IPPROTO_UDPLITE},
1349 {"icmp", IPPROTO_ICMP},
1350 {"icmpv6", IPPROTO_ICMPV6},
1351 {"ipv6-icmp", IPPROTO_ICMPV6},
1352 {"esp", IPPROTO_ESP},
1353 {"ah", IPPROTO_AH},
1354 {"ipv6-mh", IPPROTO_MH},
1355 {"mh", IPPROTO_MH},
1356 {"all", 0},
1357 {NULL},
1358};
1359
1360u_int16_t
1361xtables_parse_protocol(const char *s)
1362{
1363 unsigned int proto;
1364
1365 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1366 struct protoent *pent;
1367
1368 /* first deal with the special case of 'all' to prevent
1369 * people from being able to redefine 'all' in nsswitch
1370 * and/or provoke expensive [not working] ldap/nis/...
1371 * lookups */
1372 if (!strcmp(s, "all"))
1373 return 0;
1374
1375 if ((pent = getprotobyname(s)))
1376 proto = pent->p_proto;
1377 else {
1378 unsigned int i;
1379 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1380 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1381 proto = xtables_chain_protos[i].num;
1382 break;
1383 }
1384 }
1385 if (i == ARRAY_SIZE(xtables_chain_protos))
1386 exit_error(PARAMETER_PROBLEM,
1387 "unknown protocol `%s' specified",
1388 s);
1389 }
1390 }
1391
1392 return proto;
1393}