blob: c8988893f0232b70eaf742a7ac3f56216c2efe5e [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 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100113 /*
114 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
115 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
116 * for these env vars are deprecated anyhow, and in light of the
117 * (shared) libxt_*.so files, makes less sense to have
118 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
119 */
120 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
121 if (xtables_libdir != NULL) {
122 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
123 "use XTABLES_LIBDIR.\n");
124 return;
125 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100126 xtables_libdir = XTABLES_LIBDIR;
127}
128
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100129void xtables_set_nfproto(uint8_t nfproto)
130{
131 switch (nfproto) {
132 case NFPROTO_IPV4:
133 afinfo = &afinfo_ipv4;
134 break;
135 case NFPROTO_IPV6:
136 afinfo = &afinfo_ipv6;
137 break;
138 default:
139 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
140 __func__);
141 }
142}
143
Jan Engelhardt630ef482009-01-27 14:58:41 +0100144/**
145 * xtables_*alloc - wrappers that exit on failure
146 */
147void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000148{
149 void *p;
150
151 if ((p = calloc(count, size)) == NULL) {
152 perror("ip[6]tables: calloc failed");
153 exit(1);
154 }
155
156 return p;
157}
158
Jan Engelhardt630ef482009-01-27 14:58:41 +0100159void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000160{
161 void *p;
162
163 if ((p = malloc(size)) == NULL) {
164 perror("ip[6]tables: malloc failed");
165 exit(1);
166 }
167
168 return p;
169}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000170
171static char *get_modprobe(void)
172{
173 int procfile;
174 char *ret;
175
176#define PROCFILE_BUFSIZ 1024
177 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
178 if (procfile < 0)
179 return NULL;
180
181 ret = (char *) malloc(PROCFILE_BUFSIZ);
182 if (ret) {
183 memset(ret, 0, PROCFILE_BUFSIZ);
184 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
185 case -1: goto fail;
186 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
187 }
188 if (ret[strlen(ret)-1]=='\n')
189 ret[strlen(ret)-1]=0;
190 close(procfile);
191 return ret;
192 }
193 fail:
194 free(ret);
195 close(procfile);
196 return NULL;
197}
198
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100199int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000200{
201 char *buf = NULL;
202 char *argv[4];
203 int status;
204
205 /* If they don't explicitly set it, read out of kernel */
206 if (!modprobe) {
207 buf = get_modprobe();
208 if (!buf)
209 return -1;
210 modprobe = buf;
211 }
212
213 switch (fork()) {
214 case 0:
215 argv[0] = (char *)modprobe;
216 argv[1] = (char *)modname;
217 if (quiet) {
218 argv[2] = "-q";
219 argv[3] = NULL;
220 } else {
221 argv[2] = NULL;
222 argv[3] = NULL;
223 }
224 execv(argv[0], argv);
225
226 /* not usually reached */
227 exit(1);
228 case -1:
229 return -1;
230
231 default: /* parent */
232 wait(&status);
233 }
234
235 free(buf);
236 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
237 return 0;
238 return -1;
239}
240
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100241int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000242{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100243 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000244 static int ret = -1;
245
246 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100247 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000248 loaded = (ret == 0);
249 }
250
251 return ret;
252}
253
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100254/**
255 * xtables_strtou{i,l} - string to number conversion
256 * @s: input string
257 * @end: like strtoul's "end" pointer
258 * @value: pointer for result
259 * @min: minimum accepted value
260 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000261 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100262 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
263 * "15a" is rejected.
264 * In either case, the value obtained is compared for min-max compliance.
265 * Base is always 0, i.e. autodetect depending on @s.
266 *
267 * Returns true/false whether number was accepted. On failure, *value has
268 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000269 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100270bool xtables_strtoul(const char *s, char **end, unsigned long *value,
271 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000272{
273 unsigned long v;
274 char *my_end;
275
276 errno = 0;
277 v = strtoul(s, &my_end, 0);
278
279 if (my_end == s)
280 return false;
281 if (end != NULL)
282 *end = my_end;
283
284 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
285 if (value != NULL)
286 *value = v;
287 if (end == NULL)
288 return *my_end == '\0';
289 return true;
290 }
291
292 return false;
293}
294
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100295bool xtables_strtoui(const char *s, char **end, unsigned int *value,
296 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000297{
298 unsigned long v;
299 bool ret;
300
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100301 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000302 if (value != NULL)
303 *value = v;
304 return ret;
305}
306
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100307int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000308{
309 struct servent *service;
310
311 if ((service = getservbyname(name, proto)) != NULL)
312 return ntohs((unsigned short) service->s_port);
313
314 return -1;
315}
316
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100317u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000318{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100319 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000320
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100321 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100322 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100323 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000324
325 exit_error(PARAMETER_PROBLEM,
326 "invalid port/service `%s' specified", port);
327}
328
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100329void xtables_parse_interface(const char *arg, char *vianame,
330 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000331{
332 int vialen = strlen(arg);
333 unsigned int i;
334
335 memset(mask, 0, IFNAMSIZ);
336 memset(vianame, 0, IFNAMSIZ);
337
338 if (vialen + 1 > IFNAMSIZ)
339 exit_error(PARAMETER_PROBLEM,
340 "interface name `%s' must be shorter than IFNAMSIZ"
341 " (%i)", arg, IFNAMSIZ-1);
342
343 strcpy(vianame, arg);
344 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
345 memset(mask, 0, IFNAMSIZ);
346 else if (vianame[vialen - 1] == '+') {
347 memset(mask, 0xFF, vialen - 1);
348 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
349 /* Don't remove `+' here! -HW */
350 } else {
351 /* Include nul-terminator in match */
352 memset(mask, 0xFF, vialen + 1);
353 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
354 for (i = 0; vianame[i]; i++) {
355 if (vianame[i] == ':' ||
356 vianame[i] == '!' ||
357 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000358 fprintf(stderr,
359 "Warning: weird character in interface"
360 " `%s' (No aliases, :, ! or *).\n",
361 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000362 break;
363 }
364 }
365 }
366}
367
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200368#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100369static void *load_extension(const char *search_path, const char *prefix,
370 const char *name, bool is_target)
371{
372 const char *dir = search_path, *next;
373 void *ptr = NULL;
374 struct stat sb;
375 char path[256];
376
377 do {
378 next = strchr(dir, ':');
379 if (next == NULL)
380 next = dir + strlen(dir);
381 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200382 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100383
384 if (dlopen(path, RTLD_NOW) != NULL) {
385 /* Found library. If it didn't register itself,
386 maybe they specified target as match. */
387 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100388 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100389 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100390 ptr = xtables_find_match(name,
391 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100392 } else if (stat(path, &sb) == 0) {
393 fprintf(stderr, "%s: %s\n", path, dlerror());
394 }
395
396 if (ptr != NULL)
397 return ptr;
398
399 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200400 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100401 if (dlopen(path, RTLD_NOW) != NULL) {
402 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100403 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100404 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100405 ptr = xtables_find_match(name,
406 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100407 } else if (stat(path, &sb) == 0) {
408 fprintf(stderr, "%s: %s\n", path, dlerror());
409 }
410
411 if (ptr != NULL)
412 return ptr;
413
414 dir = next + 1;
415 } while (*next != '\0');
416
417 return NULL;
418}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200419#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100420
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100421struct xtables_match *
422xtables_find_match(const char *name, enum xtables_tryload tryload,
423 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000424{
425 struct xtables_match *ptr;
426 const char *icmp6 = "icmp6";
427
428 /* This is ugly as hell. Nonetheless, there is no way of changing
429 * this without hurting backwards compatibility */
430 if ( (strcmp(name,"icmpv6") == 0) ||
431 (strcmp(name,"ipv6-icmp") == 0) ||
432 (strcmp(name,"icmp6") == 0) )
433 name = icmp6;
434
435 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
436 if (strcmp(name, ptr->name) == 0) {
437 struct xtables_match *clone;
438
439 /* First match of this type: */
440 if (ptr->m == NULL)
441 break;
442
443 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100444 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000445 memcpy(clone, ptr, sizeof(struct xtables_match));
446 clone->mflags = 0;
447 /* This is a clone: */
448 clone->next = clone;
449
450 ptr = clone;
451 break;
452 }
453 }
454
455#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100456 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100457 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100458 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000459
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100460 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000461 exit_error(PARAMETER_PROBLEM,
462 "Couldn't load match `%s':%s\n",
463 name, dlerror());
464 }
465#else
466 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100467 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000468 ptr->loaded = 1;
469 else
470 ptr = NULL;
471 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100472 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000473 exit_error(PARAMETER_PROBLEM,
474 "Couldn't find match `%s'\n", name);
475 }
476#endif
477
478 if (ptr && matches) {
479 struct xtables_rule_match **i;
480 struct xtables_rule_match *newentry;
481
Jan Engelhardt630ef482009-01-27 14:58:41 +0100482 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000483
484 for (i = matches; *i; i = &(*i)->next) {
485 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100486 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000487 }
488 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100489 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000490 newentry->next = NULL;
491 *i = newentry;
492 }
493
494 return ptr;
495}
496
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100497struct xtables_target *
498xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000499{
500 struct xtables_target *ptr;
501
502 /* Standard target? */
503 if (strcmp(name, "") == 0
504 || strcmp(name, XTC_LABEL_ACCEPT) == 0
505 || strcmp(name, XTC_LABEL_DROP) == 0
506 || strcmp(name, XTC_LABEL_QUEUE) == 0
507 || strcmp(name, XTC_LABEL_RETURN) == 0)
508 name = "standard";
509
510 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
511 if (strcmp(name, ptr->name) == 0)
512 break;
513 }
514
515#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100516 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100517 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100518 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000519
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100520 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000521 exit_error(PARAMETER_PROBLEM,
522 "Couldn't load target `%s':%s\n",
523 name, dlerror());
524 }
525#else
526 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100527 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000528 ptr->loaded = 1;
529 else
530 ptr = NULL;
531 }
532 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
533 exit_error(PARAMETER_PROBLEM,
534 "Couldn't find target `%s'\n", name);
535 }
536#endif
537
538 if (ptr)
539 ptr->used = 1;
540
541 return ptr;
542}
543
544static int compatible_revision(const char *name, u_int8_t revision, int opt)
545{
546 struct xt_get_revision rev;
547 socklen_t s = sizeof(rev);
548 int max_rev, sockfd;
549
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100550 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000551 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000552 if (errno == EPERM) {
553 /* revision 0 is always supported. */
554 if (revision != 0)
555 fprintf(stderr, "Could not determine whether "
556 "revision %u is supported, "
557 "assuming it is.\n",
558 revision);
559 return 1;
560 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000561 fprintf(stderr, "Could not open socket to kernel: %s\n",
562 strerror(errno));
563 exit(1);
564 }
565
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100566 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000567
568 strcpy(rev.name, name);
569 rev.revision = revision;
570
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100571 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000572 if (max_rev < 0) {
573 /* Definitely don't support this? */
574 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
575 close(sockfd);
576 return 0;
577 } else if (errno == ENOPROTOOPT) {
578 close(sockfd);
579 /* Assume only revision 0 support (old kernel) */
580 return (revision == 0);
581 } else {
582 fprintf(stderr, "getsockopt failed strangely: %s\n",
583 strerror(errno));
584 exit(1);
585 }
586 }
587 close(sockfd);
588 return 1;
589}
590
591
592static int compatible_match_revision(const char *name, u_int8_t revision)
593{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100594 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000595}
596
597static int compatible_target_revision(const char *name, u_int8_t revision)
598{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100599 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000600}
601
602void xtables_register_match(struct xtables_match *me)
603{
604 struct xtables_match **i, *old;
605
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100606 if (strcmp(me->version, XTABLES_VERSION) != 0) {
607 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
608 "but \"%s\" is required.\n",
609 xtables_program_name, me->name,
610 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000611 exit(1);
612 }
613
614 /* Revision field stole a char from name. */
615 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
616 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100617 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000618 exit(1);
619 }
620
621 if (me->family >= NPROTO) {
622 fprintf(stderr,
623 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100624 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000625 exit(1);
626 }
627
628 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100629 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000630 return;
631
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100632 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000633 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100634 if (old->revision == me->revision &&
635 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000636 fprintf(stderr,
637 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100638 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000639 exit(1);
640 }
641
642 /* Now we have two (or more) options, check compatibility. */
643 if (compatible_match_revision(old->name, old->revision)
644 && old->revision > me->revision)
645 return;
646
Jan Engelhardt23545c22008-02-14 04:23:04 +0100647 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000648 if (!compatible_match_revision(me->name, me->revision))
649 return;
650
Jan Engelhardt23545c22008-02-14 04:23:04 +0100651 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
652 if (old->revision == me->revision && me->family == AF_UNSPEC)
653 return;
654
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000655 /* Delete old one. */
656 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
657 *i = old->next;
658 }
659
660 if (me->size != XT_ALIGN(me->size)) {
661 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100662 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000663 exit(1);
664 }
665
666 /* Append to list. */
667 for (i = &xtables_matches; *i; i = &(*i)->next);
668 me->next = NULL;
669 *i = me;
670
671 me->m = NULL;
672 me->mflags = 0;
673}
674
675void xtables_register_target(struct xtables_target *me)
676{
677 struct xtables_target *old;
678
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100679 if (strcmp(me->version, XTABLES_VERSION) != 0) {
680 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
681 "but \"%s\" is required.\n",
682 xtables_program_name, me->name,
683 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000684 exit(1);
685 }
686
687 /* Revision field stole a char from name. */
688 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
689 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100690 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000691 exit(1);
692 }
693
694 if (me->family >= NPROTO) {
695 fprintf(stderr,
696 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100697 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000698 exit(1);
699 }
700
701 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100702 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000703 return;
704
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100705 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000706 if (old) {
707 struct xtables_target **i;
708
Jan Engelhardt23545c22008-02-14 04:23:04 +0100709 if (old->revision == me->revision &&
710 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000711 fprintf(stderr,
712 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100713 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000714 exit(1);
715 }
716
717 /* Now we have two (or more) options, check compatibility. */
718 if (compatible_target_revision(old->name, old->revision)
719 && old->revision > me->revision)
720 return;
721
Jan Engelhardt23545c22008-02-14 04:23:04 +0100722 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000723 if (!compatible_target_revision(me->name, me->revision))
724 return;
725
Jan Engelhardt23545c22008-02-14 04:23:04 +0100726 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
727 if (old->revision == me->revision && me->family == AF_UNSPEC)
728 return;
729
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000730 /* Delete old one. */
731 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
732 *i = old->next;
733 }
734
735 if (me->size != XT_ALIGN(me->size)) {
736 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100737 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000738 exit(1);
739 }
740
741 /* Prepend to list. */
742 me->next = xtables_targets;
743 xtables_targets = me;
744 me->t = NULL;
745 me->tflags = 0;
746}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000747
Jan Engelhardta41545c2009-01-27 21:27:19 +0100748/**
749 * xtables_param_act - act on condition
750 * @status: a constant from enum xtables_exittype
751 *
752 * %XTF_ONLY_ONCE: print error message that option may only be used once.
753 * @p1: module name (e.g. "mark")
754 * @p2(...): option in conflict (e.g. "--mark")
755 * @p3(...): condition to match on (see extensions/ for examples)
756 *
757 * %XTF_NO_INVERT: option does not support inversion
758 * @p1: module name
759 * @p2: option in conflict
760 * @p3: condition to match on
761 *
762 * %XTF_BAD_VALUE: bad value for option
763 * @p1: module name
764 * @p2: option with which the problem occured (e.g. "--mark")
765 * @p3: string the user passed in (e.g. "99999999999999")
766 *
767 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
768 * @p1: module name
769 *
770 * Displays an error message and exits the program.
771 */
772void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000773{
774 const char *p2, *p3;
775 va_list args;
776 bool b;
777
778 va_start(args, p1);
779
780 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100781 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000782 p2 = va_arg(args, const char *);
783 b = va_arg(args, unsigned int);
784 if (!b)
785 return;
786 exit_error(PARAMETER_PROBLEM,
787 "%s: \"%s\" option may only be specified once",
788 p1, p2);
789 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100790 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000791 p2 = va_arg(args, const char *);
792 b = va_arg(args, unsigned int);
793 if (!b)
794 return;
795 exit_error(PARAMETER_PROBLEM,
796 "%s: \"%s\" option cannot be inverted", p1, p2);
797 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100798 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000799 p2 = va_arg(args, const char *);
800 p3 = va_arg(args, const char *);
801 exit_error(PARAMETER_PROBLEM,
802 "%s: Bad value for \"%s\" option: \"%s\"",
803 p1, p2, p3);
804 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100805 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000806 b = va_arg(args, unsigned int);
807 if (!b)
808 return;
809 exit_error(PARAMETER_PROBLEM,
810 "%s: At most one action is possible", p1);
811 break;
812 default:
813 exit_error(status, p1, args);
814 break;
815 }
816
817 va_end(args);
818}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000819
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100820const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000821{
822 static char buf[20];
823 const unsigned char *bytep = (const void *)&addrp->s_addr;
824
825 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
826 return buf;
827}
828
829static const char *ipaddr_to_host(const struct in_addr *addr)
830{
831 struct hostent *host;
832
833 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
834 if (host == NULL)
835 return NULL;
836
837 return host->h_name;
838}
839
840static const char *ipaddr_to_network(const struct in_addr *addr)
841{
842 struct netent *net;
843
844 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
845 return net->n_name;
846
847 return NULL;
848}
849
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100850const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000851{
852 const char *name;
853
854 if ((name = ipaddr_to_host(addr)) != NULL ||
855 (name = ipaddr_to_network(addr)) != NULL)
856 return name;
857
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100858 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000859}
860
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100861const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000862{
863 static char buf[20];
864 uint32_t maskaddr, bits;
865 int i;
866
867 maskaddr = ntohl(mask->s_addr);
868
869 if (maskaddr == 0xFFFFFFFFL)
870 /* we don't want to see "/32" */
871 return "";
872
873 i = 32;
874 bits = 0xFFFFFFFEL;
875 while (--i >= 0 && maskaddr != bits)
876 bits <<= 1;
877 if (i >= 0)
878 sprintf(buf, "/%d", i);
879 else
880 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100881 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000882
883 return buf;
884}
885
Jan Engelhardtbd943842008-01-20 13:38:08 +0000886static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
887{
888 static struct in_addr addr;
889 unsigned char *addrp;
890 unsigned int onebyte;
891 char buf[20], *p, *q;
892 int i;
893
894 /* copy dotted string, because we need to modify it */
895 strncpy(buf, dotted, sizeof(buf) - 1);
896 buf[sizeof(buf) - 1] = '\0';
897 addrp = (void *)&addr.s_addr;
898
899 p = buf;
900 for (i = 0; i < 3; ++i) {
901 if ((q = strchr(p, '.')) == NULL) {
902 if (is_mask)
903 return NULL;
904
905 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100906 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000907 return NULL;
908
909 addrp[i] = onebyte;
910 while (i < 3)
911 addrp[++i] = 0;
912
913 return &addr;
914 }
915
916 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100917 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000918 return NULL;
919
920 addrp[i] = onebyte;
921 p = q + 1;
922 }
923
924 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100925 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000926 return NULL;
927
928 addrp[3] = onebyte;
929 return &addr;
930}
931
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100932struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000933{
934 return __numeric_to_ipaddr(dotted, false);
935}
936
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100937struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000938{
939 return __numeric_to_ipaddr(dotted, true);
940}
941
942static struct in_addr *network_to_ipaddr(const char *name)
943{
944 static struct in_addr addr;
945 struct netent *net;
946
947 if ((net = getnetbyname(name)) != NULL) {
948 if (net->n_addrtype != AF_INET)
949 return NULL;
950 addr.s_addr = htonl(net->n_net);
951 return &addr;
952 }
953
954 return NULL;
955}
956
957static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
958{
959 struct hostent *host;
960 struct in_addr *addr;
961 unsigned int i;
962
963 *naddr = 0;
964 if ((host = gethostbyname(name)) != NULL) {
965 if (host->h_addrtype != AF_INET ||
966 host->h_length != sizeof(struct in_addr))
967 return NULL;
968
969 while (host->h_addr_list[*naddr] != NULL)
970 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100971 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000972 for (i = 0; i < *naddr; i++)
973 memcpy(&addr[i], host->h_addr_list[i],
974 sizeof(struct in_addr));
975 return addr;
976 }
977
978 return NULL;
979}
980
981static struct in_addr *
982ipparse_hostnetwork(const char *name, unsigned int *naddrs)
983{
984 struct in_addr *addrptmp, *addrp;
985
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100986 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +0000987 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +0100988 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +0000989 memcpy(addrp, addrptmp, sizeof(*addrp));
990 *naddrs = 1;
991 return addrp;
992 }
993 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
994 return addrptmp;
995
996 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
997}
998
999static struct in_addr *parse_ipmask(const char *mask)
1000{
1001 static struct in_addr maskaddr;
1002 struct in_addr *addrp;
1003 unsigned int bits;
1004
1005 if (mask == NULL) {
1006 /* no mask at all defaults to 32 bits */
1007 maskaddr.s_addr = 0xFFFFFFFF;
1008 return &maskaddr;
1009 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001010 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001011 /* dotted_to_addr already returns a network byte order addr */
1012 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001013 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001014 exit_error(PARAMETER_PROBLEM,
1015 "invalid mask `%s' specified", mask);
1016 if (bits != 0) {
1017 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1018 return &maskaddr;
1019 }
1020
1021 maskaddr.s_addr = 0U;
1022 return &maskaddr;
1023}
1024
Jan Engelhardta0baae82009-01-30 04:32:50 +01001025/**
1026 * xtables_ipparse_any - transform arbitrary name to in_addr
1027 *
1028 * Possible inputs (pseudo regex):
1029 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1030 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1031 */
1032void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1033 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001034{
1035 unsigned int i, j, k, n;
1036 struct in_addr *addrp;
1037 char buf[256], *p;
1038
1039 strncpy(buf, name, sizeof(buf) - 1);
1040 buf[sizeof(buf) - 1] = '\0';
1041 if ((p = strrchr(buf, '/')) != NULL) {
1042 *p = '\0';
1043 addrp = parse_ipmask(p + 1);
1044 } else {
1045 addrp = parse_ipmask(NULL);
1046 }
1047 memcpy(maskp, addrp, sizeof(*maskp));
1048
1049 /* if a null mask is given, the name is ignored, like in "any/0" */
1050 if (maskp->s_addr == 0U)
1051 strcpy(buf, "0.0.0.0");
1052
1053 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1054 n = *naddrs;
1055 for (i = 0, j = 0; i < n; ++i) {
1056 addrp[j++].s_addr &= maskp->s_addr;
1057 for (k = 0; k < j - 1; ++k)
1058 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1059 --*naddrs;
1060 --j;
1061 break;
1062 }
1063 }
1064}
1065
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001066const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001067{
1068 /* 0000:0000:0000:0000:0000:000.000.000.000
1069 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1070 static char buf[50+1];
1071 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1072}
1073
1074static const char *ip6addr_to_host(const struct in6_addr *addr)
1075{
1076 static char hostname[NI_MAXHOST];
1077 struct sockaddr_in6 saddr;
1078 int err;
1079
1080 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1081 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1082 saddr.sin6_family = AF_INET6;
1083
1084 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1085 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1086 if (err != 0) {
1087#ifdef DEBUG
1088 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1089#endif
1090 return NULL;
1091 }
1092
1093#ifdef DEBUG
1094 fprintf (stderr, "\naddr2host: %s\n", hostname);
1095#endif
1096 return hostname;
1097}
1098
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001099const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001100{
1101 const char *name;
1102
1103 if ((name = ip6addr_to_host(addr)) != NULL)
1104 return name;
1105
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001106 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001107}
1108
1109static int ip6addr_prefix_length(const struct in6_addr *k)
1110{
1111 unsigned int bits = 0;
1112 uint32_t a, b, c, d;
1113
Jan Engelhardt48607812008-06-10 15:17:53 +02001114 a = ntohl(k->s6_addr32[0]);
1115 b = ntohl(k->s6_addr32[1]);
1116 c = ntohl(k->s6_addr32[2]);
1117 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001118 while (a & 0x80000000U) {
1119 ++bits;
1120 a <<= 1;
1121 a |= (b >> 31) & 1;
1122 b <<= 1;
1123 b |= (c >> 31) & 1;
1124 c <<= 1;
1125 c |= (d >> 31) & 1;
1126 d <<= 1;
1127 }
1128 if (a != 0 || b != 0 || c != 0 || d != 0)
1129 return -1;
1130 return bits;
1131}
1132
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001133const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001134{
1135 static char buf[50+2];
1136 int l = ip6addr_prefix_length(addrp);
1137
1138 if (l == -1) {
1139 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001140 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001141 return buf;
1142 }
1143 sprintf(buf, "/%d", l);
1144 return buf;
1145}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001146
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001147struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001148{
1149 static struct in6_addr ap;
1150 int err;
1151
1152 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1153 return &ap;
1154#ifdef DEBUG
1155 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1156#endif
1157 return NULL;
1158}
1159
1160static struct in6_addr *
1161host_to_ip6addr(const char *name, unsigned int *naddr)
1162{
1163 static struct in6_addr *addr;
1164 struct addrinfo hints;
1165 struct addrinfo *res;
1166 int err;
1167
1168 memset(&hints, 0, sizeof(hints));
1169 hints.ai_flags = AI_CANONNAME;
1170 hints.ai_family = AF_INET6;
1171 hints.ai_socktype = SOCK_RAW;
1172 hints.ai_protocol = IPPROTO_IPV6;
1173 hints.ai_next = NULL;
1174
1175 *naddr = 0;
1176 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1177#ifdef DEBUG
1178 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1179#endif
1180 return NULL;
1181 } else {
1182 if (res->ai_family != AF_INET6 ||
1183 res->ai_addrlen != sizeof(struct sockaddr_in6))
1184 return NULL;
1185
1186#ifdef DEBUG
1187 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1188 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1189#endif
1190 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001191 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001192 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1193 sizeof(struct in6_addr));
1194 freeaddrinfo(res);
1195 *naddr = 1;
1196 return addr;
1197 }
1198
1199 return NULL;
1200}
1201
1202static struct in6_addr *network_to_ip6addr(const char *name)
1203{
1204 /* abort();*/
1205 /* TODO: not implemented yet, but the exception breaks the
1206 * name resolvation */
1207 return NULL;
1208}
1209
1210static struct in6_addr *
1211ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1212{
1213 struct in6_addr *addrp, *addrptmp;
1214
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001215 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001216 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001217 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001218 memcpy(addrp, addrptmp, sizeof(*addrp));
1219 *naddrs = 1;
1220 return addrp;
1221 }
1222 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1223 return addrp;
1224
1225 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1226}
1227
1228static struct in6_addr *parse_ip6mask(char *mask)
1229{
1230 static struct in6_addr maskaddr;
1231 struct in6_addr *addrp;
1232 unsigned int bits;
1233
1234 if (mask == NULL) {
1235 /* no mask at all defaults to 128 bits */
1236 memset(&maskaddr, 0xff, sizeof maskaddr);
1237 return &maskaddr;
1238 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001239 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001240 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001241 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001242 exit_error(PARAMETER_PROBLEM,
1243 "invalid mask `%s' specified", mask);
1244 if (bits != 0) {
1245 char *p = (void *)&maskaddr;
1246 memset(p, 0xff, bits / 8);
1247 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1248 p[bits/8] = 0xff << (8 - (bits & 7));
1249 return &maskaddr;
1250 }
1251
1252 memset(&maskaddr, 0, sizeof(maskaddr));
1253 return &maskaddr;
1254}
1255
Jan Engelhardta0baae82009-01-30 04:32:50 +01001256void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1257 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001258{
1259 struct in6_addr *addrp;
1260 unsigned int i, j, k, n;
1261 char buf[256], *p;
1262
1263 strncpy(buf, name, sizeof(buf) - 1);
1264 buf[sizeof(buf)-1] = '\0';
1265 if ((p = strrchr(buf, '/')) != NULL) {
1266 *p = '\0';
1267 addrp = parse_ip6mask(p + 1);
1268 } else {
1269 addrp = parse_ip6mask(NULL);
1270 }
1271 memcpy(maskp, addrp, sizeof(*maskp));
1272
1273 /* if a null mask is given, the name is ignored, like in "any/0" */
1274 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1275 strcpy(buf, "::");
1276
1277 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1278 n = *naddrs;
1279 for (i = 0, j = 0; i < n; ++i) {
1280 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001281 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001282 ++j;
1283 for (k = 0; k < j - 1; ++k)
1284 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1285 --*naddrs;
1286 --j;
1287 break;
1288 }
1289 }
1290}
Max Kellermanna5d09942008-01-29 13:44:34 +00001291
Jan Engelhardta0baae82009-01-30 04:32:50 +01001292void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001293{
1294 static const char no_quote_chars[] = "_-0123456789"
1295 "abcdefghijklmnopqrstuvwxyz"
1296 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1297 static const char escape_chars[] = "\"\\'";
1298 size_t length;
1299 const char *p;
1300
1301 length = strcspn(value, no_quote_chars);
1302 if (length > 0 && value[length] == 0) {
1303 /* no quoting required */
1304 fputs(value, stdout);
1305 putchar(' ');
1306 } else {
1307 /* there is at least one dangerous character in the
1308 value, which we have to quote. Write double quotes
1309 around the value and escape special characters with
1310 a backslash */
1311 putchar('"');
1312
1313 for (p = strpbrk(value, escape_chars); p != NULL;
1314 p = strpbrk(value, escape_chars)) {
1315 if (p > value)
1316 fwrite(value, 1, p - value, stdout);
1317 putchar('\\');
1318 putchar(*p);
1319 value = p + 1;
1320 }
1321
1322 /* print the rest and finish the double quoted
1323 string */
1324 fputs(value, stdout);
1325 printf("\" ");
1326 }
1327}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001328
1329/**
1330 * Check for option-intrapositional negation.
1331 * Do not use in new code.
1332 */
1333int xtables_check_inverse(const char option[], int *invert,
1334 int *my_optind, int argc)
1335{
1336 if (option && strcmp(option, "!") == 0) {
1337 fprintf(stderr, "Using intrapositioned negation "
1338 "(`--option ! this`) is deprecated in favor of "
1339 "extrapositioned (`! --option this`).\n");
1340
1341 if (*invert)
1342 exit_error(PARAMETER_PROBLEM,
1343 "Multiple `!' flags not allowed");
1344 *invert = true;
1345 if (my_optind != NULL) {
1346 ++*my_optind;
1347 if (argc && *my_optind > argc)
1348 exit_error(PARAMETER_PROBLEM,
1349 "no argument following `!'");
1350 }
1351
1352 return true;
1353 }
1354 return false;
1355}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001356
1357const struct xtables_pprot xtables_chain_protos[] = {
1358 {"tcp", IPPROTO_TCP},
1359 {"sctp", IPPROTO_SCTP},
1360 {"udp", IPPROTO_UDP},
1361 {"udplite", IPPROTO_UDPLITE},
1362 {"icmp", IPPROTO_ICMP},
1363 {"icmpv6", IPPROTO_ICMPV6},
1364 {"ipv6-icmp", IPPROTO_ICMPV6},
1365 {"esp", IPPROTO_ESP},
1366 {"ah", IPPROTO_AH},
1367 {"ipv6-mh", IPPROTO_MH},
1368 {"mh", IPPROTO_MH},
1369 {"all", 0},
1370 {NULL},
1371};
1372
1373u_int16_t
1374xtables_parse_protocol(const char *s)
1375{
1376 unsigned int proto;
1377
1378 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1379 struct protoent *pent;
1380
1381 /* first deal with the special case of 'all' to prevent
1382 * people from being able to redefine 'all' in nsswitch
1383 * and/or provoke expensive [not working] ldap/nis/...
1384 * lookups */
1385 if (!strcmp(s, "all"))
1386 return 0;
1387
1388 if ((pent = getprotobyname(s)))
1389 proto = pent->p_proto;
1390 else {
1391 unsigned int i;
1392 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1393 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1394 proto = xtables_chain_protos[i].num;
1395 break;
1396 }
1397 }
1398 if (i == ARRAY_SIZE(xtables_chain_protos))
1399 exit_error(PARAMETER_PROBLEM,
1400 "unknown protocol `%s' specified",
1401 s);
1402 }
1403 }
1404
1405 return proto;
1406}