blob: d0fc478af656457c99c2b64dc40def0f0fbcdb8d [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000019#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000021#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000022#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000023#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000024#include <stdio.h>
25#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000026#include <string.h>
27#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000028#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000032#include <arpa/inet.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000034#include <xtables.h>
Jan Engelhardt77f48c22009-02-07 19:59:53 +010035#include <linux/netfilter_ipv4/ip_tables.h>
36#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020037#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000038
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000039#ifndef NO_SHARED_LIBS
40#include <dlfcn.h>
41#endif
42
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000043#define NPROTO 255
44
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000045#ifndef PROC_SYS_MODPROBE
46#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
47#endif
48
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010049struct xtables_globals *xt_params = NULL;
50
51void basic_exit_error(enum xtables_exittype status, const char *msg, ...)
52{
53 va_list args;
54
55 va_start(args, msg);
56 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
57 vfprintf(stderr, msg, args);
58 va_end(args);
59 fprintf(stderr, "\n");
60 exit(status);
61}
62
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010063/**
64 * xtables_set_params - set the global parameters used by xtables
65 * @xtp: input xtables_globals structure
66 *
67 * The app is expected to pass a valid xtables_globals data-filled
68 * with proper values
69 * @xtp cannot be NULL
70 *
71 * Returns -1 on failure to set and 0 on success
72 */
73int xtables_set_params(struct xtables_globals *xtp)
74{
75 if (!xtp) {
76 fprintf(stderr, "%s: Illegal global params\n",__func__);
77 return -1;
78 }
79
80 xt_params = xtp;
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010081
82 if (!xt_params->exit_error)
83 xt_params->exit_error = basic_exit_error;
84
Jamal Hadi Salim8e90ce62009-02-11 12:58:54 +010085 return 0;
86}
87
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010088void xtables_free_opts(int reset_offset, struct option *original_opts)
89{
90 if (xt_params->opts != original_opts) {
91 if (original_opts)
92 free(xt_params->opts);
93 xt_params->opts = original_opts;
94 if (reset_offset)
95 xt_params->option_offset = 0;
96 }
97}
98
Jan Engelhardtdacafa52009-01-27 20:56:23 +010099/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100100 * xtables_afinfo - protocol family dependent information
101 * @kmod: kernel module basename (e.g. "ip_tables")
102 * @libprefix: prefix of .so library name (e.g. "libipt_")
103 * @family: nfproto family
104 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
105 * @so_rev_match: optname to check revision support of match
106 * @so_rev_target: optname to check revision support of target
107 */
108struct xtables_afinfo {
109 const char *kmod;
110 const char *libprefix;
111 uint8_t family;
112 uint8_t ipproto;
113 int so_rev_match;
114 int so_rev_target;
115};
116
117static const struct xtables_afinfo afinfo_ipv4 = {
118 .kmod = "ip_tables",
119 .libprefix = "libipt_",
120 .family = NFPROTO_IPV4,
121 .ipproto = IPPROTO_IP,
122 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
123 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
124};
125
126static const struct xtables_afinfo afinfo_ipv6 = {
127 .kmod = "ip6_tables",
128 .libprefix = "libip6t_",
129 .family = NFPROTO_IPV6,
130 .ipproto = IPPROTO_IPV6,
131 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
132 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
133};
134
135static const struct xtables_afinfo *afinfo;
136
137/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100138 * Program will set this to its own name.
139 */
140const char *xtables_program_name;
141
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100142/* Search path for Xtables .so files */
143static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000144
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000145/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100146const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000147
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000148/* Keeping track of external matches and targets: linked lists. */
149struct xtables_match *xtables_matches;
150struct xtables_target *xtables_targets;
151
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100152void xtables_init(void)
153{
154 xtables_libdir = getenv("XTABLES_LIBDIR");
155 if (xtables_libdir != NULL)
156 return;
157 xtables_libdir = getenv("IPTABLES_LIB_DIR");
158 if (xtables_libdir != NULL) {
159 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
160 "use XTABLES_LIBDIR.\n");
161 return;
162 }
163 xtables_libdir = XTABLES_LIBDIR;
164}
165
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100166void xtables_set_nfproto(uint8_t nfproto)
167{
168 switch (nfproto) {
169 case NFPROTO_IPV4:
170 afinfo = &afinfo_ipv4;
171 break;
172 case NFPROTO_IPV6:
173 afinfo = &afinfo_ipv6;
174 break;
175 default:
176 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
177 __func__);
178 }
179}
180
Jan Engelhardt630ef482009-01-27 14:58:41 +0100181/**
182 * xtables_*alloc - wrappers that exit on failure
183 */
184void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000185{
186 void *p;
187
188 if ((p = calloc(count, size)) == NULL) {
189 perror("ip[6]tables: calloc failed");
190 exit(1);
191 }
192
193 return p;
194}
195
Jan Engelhardt630ef482009-01-27 14:58:41 +0100196void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000197{
198 void *p;
199
200 if ((p = malloc(size)) == NULL) {
201 perror("ip[6]tables: malloc failed");
202 exit(1);
203 }
204
205 return p;
206}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000207
208static char *get_modprobe(void)
209{
210 int procfile;
211 char *ret;
212
213#define PROCFILE_BUFSIZ 1024
214 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
215 if (procfile < 0)
216 return NULL;
217
218 ret = (char *) malloc(PROCFILE_BUFSIZ);
219 if (ret) {
220 memset(ret, 0, PROCFILE_BUFSIZ);
221 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
222 case -1: goto fail;
223 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
224 }
225 if (ret[strlen(ret)-1]=='\n')
226 ret[strlen(ret)-1]=0;
227 close(procfile);
228 return ret;
229 }
230 fail:
231 free(ret);
232 close(procfile);
233 return NULL;
234}
235
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100236int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000237{
238 char *buf = NULL;
239 char *argv[4];
240 int status;
241
242 /* If they don't explicitly set it, read out of kernel */
243 if (!modprobe) {
244 buf = get_modprobe();
245 if (!buf)
246 return -1;
247 modprobe = buf;
248 }
249
250 switch (fork()) {
251 case 0:
252 argv[0] = (char *)modprobe;
253 argv[1] = (char *)modname;
254 if (quiet) {
255 argv[2] = "-q";
256 argv[3] = NULL;
257 } else {
258 argv[2] = NULL;
259 argv[3] = NULL;
260 }
261 execv(argv[0], argv);
262
263 /* not usually reached */
264 exit(1);
265 case -1:
266 return -1;
267
268 default: /* parent */
269 wait(&status);
270 }
271
272 free(buf);
273 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
274 return 0;
275 return -1;
276}
277
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100278int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000279{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100280 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000281 static int ret = -1;
282
283 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100284 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000285 loaded = (ret == 0);
286 }
287
288 return ret;
289}
290
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100291/**
292 * xtables_strtou{i,l} - string to number conversion
293 * @s: input string
294 * @end: like strtoul's "end" pointer
295 * @value: pointer for result
296 * @min: minimum accepted value
297 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000298 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100299 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
300 * "15a" is rejected.
301 * In either case, the value obtained is compared for min-max compliance.
302 * Base is always 0, i.e. autodetect depending on @s.
303 *
304 * Returns true/false whether number was accepted. On failure, *value has
305 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000306 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100307bool xtables_strtoul(const char *s, char **end, unsigned long *value,
308 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000309{
310 unsigned long v;
311 char *my_end;
312
313 errno = 0;
314 v = strtoul(s, &my_end, 0);
315
316 if (my_end == s)
317 return false;
318 if (end != NULL)
319 *end = my_end;
320
321 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
322 if (value != NULL)
323 *value = v;
324 if (end == NULL)
325 return *my_end == '\0';
326 return true;
327 }
328
329 return false;
330}
331
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100332bool xtables_strtoui(const char *s, char **end, unsigned int *value,
333 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000334{
335 unsigned long v;
336 bool ret;
337
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100338 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000339 if (value != NULL)
340 *value = v;
341 return ret;
342}
343
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100344int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000345{
346 struct servent *service;
347
348 if ((service = getservbyname(name, proto)) != NULL)
349 return ntohs((unsigned short) service->s_port);
350
351 return -1;
352}
353
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100354u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000355{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100356 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000357
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100358 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100359 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100360 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000361
362 exit_error(PARAMETER_PROBLEM,
363 "invalid port/service `%s' specified", port);
364}
365
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100366void xtables_parse_interface(const char *arg, char *vianame,
367 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000368{
369 int vialen = strlen(arg);
370 unsigned int i;
371
372 memset(mask, 0, IFNAMSIZ);
373 memset(vianame, 0, IFNAMSIZ);
374
375 if (vialen + 1 > IFNAMSIZ)
376 exit_error(PARAMETER_PROBLEM,
377 "interface name `%s' must be shorter than IFNAMSIZ"
378 " (%i)", arg, IFNAMSIZ-1);
379
380 strcpy(vianame, arg);
381 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
382 memset(mask, 0, IFNAMSIZ);
383 else if (vianame[vialen - 1] == '+') {
384 memset(mask, 0xFF, vialen - 1);
385 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
386 /* Don't remove `+' here! -HW */
387 } else {
388 /* Include nul-terminator in match */
389 memset(mask, 0xFF, vialen + 1);
390 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
391 for (i = 0; vianame[i]; i++) {
392 if (vianame[i] == ':' ||
393 vianame[i] == '!' ||
394 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000395 fprintf(stderr,
396 "Warning: weird character in interface"
397 " `%s' (No aliases, :, ! or *).\n",
398 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000399 break;
400 }
401 }
402 }
403}
404
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200405#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100406static void *load_extension(const char *search_path, const char *prefix,
407 const char *name, bool is_target)
408{
409 const char *dir = search_path, *next;
410 void *ptr = NULL;
411 struct stat sb;
412 char path[256];
413
414 do {
415 next = strchr(dir, ':');
416 if (next == NULL)
417 next = dir + strlen(dir);
418 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200419 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100420
421 if (dlopen(path, RTLD_NOW) != NULL) {
422 /* Found library. If it didn't register itself,
423 maybe they specified target as match. */
424 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100425 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100426 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100427 ptr = xtables_find_match(name,
428 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100429 } else if (stat(path, &sb) == 0) {
430 fprintf(stderr, "%s: %s\n", path, dlerror());
431 }
432
433 if (ptr != NULL)
434 return ptr;
435
436 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200437 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100438 if (dlopen(path, RTLD_NOW) != NULL) {
439 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100440 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100441 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100442 ptr = xtables_find_match(name,
443 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100444 } else if (stat(path, &sb) == 0) {
445 fprintf(stderr, "%s: %s\n", path, dlerror());
446 }
447
448 if (ptr != NULL)
449 return ptr;
450
451 dir = next + 1;
452 } while (*next != '\0');
453
454 return NULL;
455}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200456#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100457
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100458struct xtables_match *
459xtables_find_match(const char *name, enum xtables_tryload tryload,
460 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000461{
462 struct xtables_match *ptr;
463 const char *icmp6 = "icmp6";
464
465 /* This is ugly as hell. Nonetheless, there is no way of changing
466 * this without hurting backwards compatibility */
467 if ( (strcmp(name,"icmpv6") == 0) ||
468 (strcmp(name,"ipv6-icmp") == 0) ||
469 (strcmp(name,"icmp6") == 0) )
470 name = icmp6;
471
472 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
473 if (strcmp(name, ptr->name) == 0) {
474 struct xtables_match *clone;
475
476 /* First match of this type: */
477 if (ptr->m == NULL)
478 break;
479
480 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100481 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000482 memcpy(clone, ptr, sizeof(struct xtables_match));
483 clone->mflags = 0;
484 /* This is a clone: */
485 clone->next = clone;
486
487 ptr = clone;
488 break;
489 }
490 }
491
492#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100493 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100494 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100495 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000496
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100497 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000498 exit_error(PARAMETER_PROBLEM,
499 "Couldn't load match `%s':%s\n",
500 name, dlerror());
501 }
502#else
503 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100504 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000505 ptr->loaded = 1;
506 else
507 ptr = NULL;
508 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100509 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000510 exit_error(PARAMETER_PROBLEM,
511 "Couldn't find match `%s'\n", name);
512 }
513#endif
514
515 if (ptr && matches) {
516 struct xtables_rule_match **i;
517 struct xtables_rule_match *newentry;
518
Jan Engelhardt630ef482009-01-27 14:58:41 +0100519 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000520
521 for (i = matches; *i; i = &(*i)->next) {
522 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100523 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000524 }
525 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100526 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000527 newentry->next = NULL;
528 *i = newentry;
529 }
530
531 return ptr;
532}
533
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100534struct xtables_target *
535xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000536{
537 struct xtables_target *ptr;
538
539 /* Standard target? */
540 if (strcmp(name, "") == 0
541 || strcmp(name, XTC_LABEL_ACCEPT) == 0
542 || strcmp(name, XTC_LABEL_DROP) == 0
543 || strcmp(name, XTC_LABEL_QUEUE) == 0
544 || strcmp(name, XTC_LABEL_RETURN) == 0)
545 name = "standard";
546
547 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
548 if (strcmp(name, ptr->name) == 0)
549 break;
550 }
551
552#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100553 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100554 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100555 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000556
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100557 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000558 exit_error(PARAMETER_PROBLEM,
559 "Couldn't load target `%s':%s\n",
560 name, dlerror());
561 }
562#else
563 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100564 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000565 ptr->loaded = 1;
566 else
567 ptr = NULL;
568 }
569 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
570 exit_error(PARAMETER_PROBLEM,
571 "Couldn't find target `%s'\n", name);
572 }
573#endif
574
575 if (ptr)
576 ptr->used = 1;
577
578 return ptr;
579}
580
581static int compatible_revision(const char *name, u_int8_t revision, int opt)
582{
583 struct xt_get_revision rev;
584 socklen_t s = sizeof(rev);
585 int max_rev, sockfd;
586
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100587 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000588 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000589 if (errno == EPERM) {
590 /* revision 0 is always supported. */
591 if (revision != 0)
592 fprintf(stderr, "Could not determine whether "
593 "revision %u is supported, "
594 "assuming it is.\n",
595 revision);
596 return 1;
597 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000598 fprintf(stderr, "Could not open socket to kernel: %s\n",
599 strerror(errno));
600 exit(1);
601 }
602
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100603 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000604
605 strcpy(rev.name, name);
606 rev.revision = revision;
607
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100608 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000609 if (max_rev < 0) {
610 /* Definitely don't support this? */
611 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
612 close(sockfd);
613 return 0;
614 } else if (errno == ENOPROTOOPT) {
615 close(sockfd);
616 /* Assume only revision 0 support (old kernel) */
617 return (revision == 0);
618 } else {
619 fprintf(stderr, "getsockopt failed strangely: %s\n",
620 strerror(errno));
621 exit(1);
622 }
623 }
624 close(sockfd);
625 return 1;
626}
627
628
629static int compatible_match_revision(const char *name, u_int8_t revision)
630{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100631 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000632}
633
634static int compatible_target_revision(const char *name, u_int8_t revision)
635{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100636 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000637}
638
639void xtables_register_match(struct xtables_match *me)
640{
641 struct xtables_match **i, *old;
642
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100643 if (strcmp(me->version, XTABLES_VERSION) != 0) {
644 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
645 "but \"%s\" is required.\n",
646 xtables_program_name, me->name,
647 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000648 exit(1);
649 }
650
651 /* Revision field stole a char from name. */
652 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
653 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100654 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000655 exit(1);
656 }
657
658 if (me->family >= NPROTO) {
659 fprintf(stderr,
660 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100661 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000662 exit(1);
663 }
664
665 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100666 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000667 return;
668
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100669 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000670 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100671 if (old->revision == me->revision &&
672 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000673 fprintf(stderr,
674 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100675 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000676 exit(1);
677 }
678
679 /* Now we have two (or more) options, check compatibility. */
680 if (compatible_match_revision(old->name, old->revision)
681 && old->revision > me->revision)
682 return;
683
Jan Engelhardt23545c22008-02-14 04:23:04 +0100684 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000685 if (!compatible_match_revision(me->name, me->revision))
686 return;
687
Jan Engelhardt23545c22008-02-14 04:23:04 +0100688 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
689 if (old->revision == me->revision && me->family == AF_UNSPEC)
690 return;
691
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000692 /* Delete old one. */
693 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
694 *i = old->next;
695 }
696
697 if (me->size != XT_ALIGN(me->size)) {
698 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100699 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000700 exit(1);
701 }
702
703 /* Append to list. */
704 for (i = &xtables_matches; *i; i = &(*i)->next);
705 me->next = NULL;
706 *i = me;
707
708 me->m = NULL;
709 me->mflags = 0;
710}
711
712void xtables_register_target(struct xtables_target *me)
713{
714 struct xtables_target *old;
715
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100716 if (strcmp(me->version, XTABLES_VERSION) != 0) {
717 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
718 "but \"%s\" is required.\n",
719 xtables_program_name, me->name,
720 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000721 exit(1);
722 }
723
724 /* Revision field stole a char from name. */
725 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
726 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100727 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000728 exit(1);
729 }
730
731 if (me->family >= NPROTO) {
732 fprintf(stderr,
733 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100734 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000735 exit(1);
736 }
737
738 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100739 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000740 return;
741
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100742 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000743 if (old) {
744 struct xtables_target **i;
745
Jan Engelhardt23545c22008-02-14 04:23:04 +0100746 if (old->revision == me->revision &&
747 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000748 fprintf(stderr,
749 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100750 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000751 exit(1);
752 }
753
754 /* Now we have two (or more) options, check compatibility. */
755 if (compatible_target_revision(old->name, old->revision)
756 && old->revision > me->revision)
757 return;
758
Jan Engelhardt23545c22008-02-14 04:23:04 +0100759 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000760 if (!compatible_target_revision(me->name, me->revision))
761 return;
762
Jan Engelhardt23545c22008-02-14 04:23:04 +0100763 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
764 if (old->revision == me->revision && me->family == AF_UNSPEC)
765 return;
766
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000767 /* Delete old one. */
768 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
769 *i = old->next;
770 }
771
772 if (me->size != XT_ALIGN(me->size)) {
773 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100774 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000775 exit(1);
776 }
777
778 /* Prepend to list. */
779 me->next = xtables_targets;
780 xtables_targets = me;
781 me->t = NULL;
782 me->tflags = 0;
783}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000784
Jan Engelhardta41545c2009-01-27 21:27:19 +0100785/**
786 * xtables_param_act - act on condition
787 * @status: a constant from enum xtables_exittype
788 *
789 * %XTF_ONLY_ONCE: print error message that option may only be used once.
790 * @p1: module name (e.g. "mark")
791 * @p2(...): option in conflict (e.g. "--mark")
792 * @p3(...): condition to match on (see extensions/ for examples)
793 *
794 * %XTF_NO_INVERT: option does not support inversion
795 * @p1: module name
796 * @p2: option in conflict
797 * @p3: condition to match on
798 *
799 * %XTF_BAD_VALUE: bad value for option
800 * @p1: module name
801 * @p2: option with which the problem occured (e.g. "--mark")
802 * @p3: string the user passed in (e.g. "99999999999999")
803 *
804 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
805 * @p1: module name
806 *
807 * Displays an error message and exits the program.
808 */
809void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000810{
811 const char *p2, *p3;
812 va_list args;
813 bool b;
814
815 va_start(args, p1);
816
817 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100818 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000819 p2 = va_arg(args, const char *);
820 b = va_arg(args, unsigned int);
821 if (!b)
822 return;
823 exit_error(PARAMETER_PROBLEM,
824 "%s: \"%s\" option may only be specified once",
825 p1, p2);
826 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100827 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000828 p2 = va_arg(args, const char *);
829 b = va_arg(args, unsigned int);
830 if (!b)
831 return;
832 exit_error(PARAMETER_PROBLEM,
833 "%s: \"%s\" option cannot be inverted", p1, p2);
834 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100835 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000836 p2 = va_arg(args, const char *);
837 p3 = va_arg(args, const char *);
838 exit_error(PARAMETER_PROBLEM,
839 "%s: Bad value for \"%s\" option: \"%s\"",
840 p1, p2, p3);
841 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100842 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000843 b = va_arg(args, unsigned int);
844 if (!b)
845 return;
846 exit_error(PARAMETER_PROBLEM,
847 "%s: At most one action is possible", p1);
848 break;
849 default:
850 exit_error(status, p1, args);
851 break;
852 }
853
854 va_end(args);
855}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000856
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100857const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000858{
859 static char buf[20];
860 const unsigned char *bytep = (const void *)&addrp->s_addr;
861
862 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
863 return buf;
864}
865
866static const char *ipaddr_to_host(const struct in_addr *addr)
867{
868 struct hostent *host;
869
870 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
871 if (host == NULL)
872 return NULL;
873
874 return host->h_name;
875}
876
877static const char *ipaddr_to_network(const struct in_addr *addr)
878{
879 struct netent *net;
880
881 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
882 return net->n_name;
883
884 return NULL;
885}
886
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100887const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000888{
889 const char *name;
890
891 if ((name = ipaddr_to_host(addr)) != NULL ||
892 (name = ipaddr_to_network(addr)) != NULL)
893 return name;
894
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100895 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000896}
897
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100898const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000899{
900 static char buf[20];
901 uint32_t maskaddr, bits;
902 int i;
903
904 maskaddr = ntohl(mask->s_addr);
905
906 if (maskaddr == 0xFFFFFFFFL)
907 /* we don't want to see "/32" */
908 return "";
909
910 i = 32;
911 bits = 0xFFFFFFFEL;
912 while (--i >= 0 && maskaddr != bits)
913 bits <<= 1;
914 if (i >= 0)
915 sprintf(buf, "/%d", i);
916 else
917 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100918 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000919
920 return buf;
921}
922
Jan Engelhardtbd943842008-01-20 13:38:08 +0000923static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
924{
925 static struct in_addr addr;
926 unsigned char *addrp;
927 unsigned int onebyte;
928 char buf[20], *p, *q;
929 int i;
930
931 /* copy dotted string, because we need to modify it */
932 strncpy(buf, dotted, sizeof(buf) - 1);
933 buf[sizeof(buf) - 1] = '\0';
934 addrp = (void *)&addr.s_addr;
935
936 p = buf;
937 for (i = 0; i < 3; ++i) {
938 if ((q = strchr(p, '.')) == NULL) {
939 if (is_mask)
940 return NULL;
941
942 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100943 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000944 return NULL;
945
946 addrp[i] = onebyte;
947 while (i < 3)
948 addrp[++i] = 0;
949
950 return &addr;
951 }
952
953 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100954 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000955 return NULL;
956
957 addrp[i] = onebyte;
958 p = q + 1;
959 }
960
961 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100962 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000963 return NULL;
964
965 addrp[3] = onebyte;
966 return &addr;
967}
968
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100969struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000970{
971 return __numeric_to_ipaddr(dotted, false);
972}
973
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100974struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000975{
976 return __numeric_to_ipaddr(dotted, true);
977}
978
979static struct in_addr *network_to_ipaddr(const char *name)
980{
981 static struct in_addr addr;
982 struct netent *net;
983
984 if ((net = getnetbyname(name)) != NULL) {
985 if (net->n_addrtype != AF_INET)
986 return NULL;
987 addr.s_addr = htonl(net->n_net);
988 return &addr;
989 }
990
991 return NULL;
992}
993
994static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
995{
996 struct hostent *host;
997 struct in_addr *addr;
998 unsigned int i;
999
1000 *naddr = 0;
1001 if ((host = gethostbyname(name)) != NULL) {
1002 if (host->h_addrtype != AF_INET ||
1003 host->h_length != sizeof(struct in_addr))
1004 return NULL;
1005
1006 while (host->h_addr_list[*naddr] != NULL)
1007 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001008 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001009 for (i = 0; i < *naddr; i++)
1010 memcpy(&addr[i], host->h_addr_list[i],
1011 sizeof(struct in_addr));
1012 return addr;
1013 }
1014
1015 return NULL;
1016}
1017
1018static struct in_addr *
1019ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1020{
1021 struct in_addr *addrptmp, *addrp;
1022
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001023 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001024 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001025 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001026 memcpy(addrp, addrptmp, sizeof(*addrp));
1027 *naddrs = 1;
1028 return addrp;
1029 }
1030 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1031 return addrptmp;
1032
1033 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1034}
1035
1036static struct in_addr *parse_ipmask(const char *mask)
1037{
1038 static struct in_addr maskaddr;
1039 struct in_addr *addrp;
1040 unsigned int bits;
1041
1042 if (mask == NULL) {
1043 /* no mask at all defaults to 32 bits */
1044 maskaddr.s_addr = 0xFFFFFFFF;
1045 return &maskaddr;
1046 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001047 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001048 /* dotted_to_addr already returns a network byte order addr */
1049 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001050 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001051 exit_error(PARAMETER_PROBLEM,
1052 "invalid mask `%s' specified", mask);
1053 if (bits != 0) {
1054 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1055 return &maskaddr;
1056 }
1057
1058 maskaddr.s_addr = 0U;
1059 return &maskaddr;
1060}
1061
Jan Engelhardta0baae82009-01-30 04:32:50 +01001062/**
1063 * xtables_ipparse_any - transform arbitrary name to in_addr
1064 *
1065 * Possible inputs (pseudo regex):
1066 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1067 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1068 */
1069void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1070 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001071{
1072 unsigned int i, j, k, n;
1073 struct in_addr *addrp;
1074 char buf[256], *p;
1075
1076 strncpy(buf, name, sizeof(buf) - 1);
1077 buf[sizeof(buf) - 1] = '\0';
1078 if ((p = strrchr(buf, '/')) != NULL) {
1079 *p = '\0';
1080 addrp = parse_ipmask(p + 1);
1081 } else {
1082 addrp = parse_ipmask(NULL);
1083 }
1084 memcpy(maskp, addrp, sizeof(*maskp));
1085
1086 /* if a null mask is given, the name is ignored, like in "any/0" */
1087 if (maskp->s_addr == 0U)
1088 strcpy(buf, "0.0.0.0");
1089
1090 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1091 n = *naddrs;
1092 for (i = 0, j = 0; i < n; ++i) {
1093 addrp[j++].s_addr &= maskp->s_addr;
1094 for (k = 0; k < j - 1; ++k)
1095 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1096 --*naddrs;
1097 --j;
1098 break;
1099 }
1100 }
1101}
1102
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001103const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001104{
1105 /* 0000:0000:0000:0000:0000:000.000.000.000
1106 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1107 static char buf[50+1];
1108 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1109}
1110
1111static const char *ip6addr_to_host(const struct in6_addr *addr)
1112{
1113 static char hostname[NI_MAXHOST];
1114 struct sockaddr_in6 saddr;
1115 int err;
1116
1117 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1118 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1119 saddr.sin6_family = AF_INET6;
1120
1121 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1122 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1123 if (err != 0) {
1124#ifdef DEBUG
1125 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1126#endif
1127 return NULL;
1128 }
1129
1130#ifdef DEBUG
1131 fprintf (stderr, "\naddr2host: %s\n", hostname);
1132#endif
1133 return hostname;
1134}
1135
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001136const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001137{
1138 const char *name;
1139
1140 if ((name = ip6addr_to_host(addr)) != NULL)
1141 return name;
1142
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001143 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001144}
1145
1146static int ip6addr_prefix_length(const struct in6_addr *k)
1147{
1148 unsigned int bits = 0;
1149 uint32_t a, b, c, d;
1150
Jan Engelhardt48607812008-06-10 15:17:53 +02001151 a = ntohl(k->s6_addr32[0]);
1152 b = ntohl(k->s6_addr32[1]);
1153 c = ntohl(k->s6_addr32[2]);
1154 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001155 while (a & 0x80000000U) {
1156 ++bits;
1157 a <<= 1;
1158 a |= (b >> 31) & 1;
1159 b <<= 1;
1160 b |= (c >> 31) & 1;
1161 c <<= 1;
1162 c |= (d >> 31) & 1;
1163 d <<= 1;
1164 }
1165 if (a != 0 || b != 0 || c != 0 || d != 0)
1166 return -1;
1167 return bits;
1168}
1169
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001170const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001171{
1172 static char buf[50+2];
1173 int l = ip6addr_prefix_length(addrp);
1174
1175 if (l == -1) {
1176 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001177 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001178 return buf;
1179 }
1180 sprintf(buf, "/%d", l);
1181 return buf;
1182}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001183
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001184struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001185{
1186 static struct in6_addr ap;
1187 int err;
1188
1189 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1190 return &ap;
1191#ifdef DEBUG
1192 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1193#endif
1194 return NULL;
1195}
1196
1197static struct in6_addr *
1198host_to_ip6addr(const char *name, unsigned int *naddr)
1199{
1200 static struct in6_addr *addr;
1201 struct addrinfo hints;
1202 struct addrinfo *res;
1203 int err;
1204
1205 memset(&hints, 0, sizeof(hints));
1206 hints.ai_flags = AI_CANONNAME;
1207 hints.ai_family = AF_INET6;
1208 hints.ai_socktype = SOCK_RAW;
1209 hints.ai_protocol = IPPROTO_IPV6;
1210 hints.ai_next = NULL;
1211
1212 *naddr = 0;
1213 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1214#ifdef DEBUG
1215 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1216#endif
1217 return NULL;
1218 } else {
1219 if (res->ai_family != AF_INET6 ||
1220 res->ai_addrlen != sizeof(struct sockaddr_in6))
1221 return NULL;
1222
1223#ifdef DEBUG
1224 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1225 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1226#endif
1227 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001228 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001229 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1230 sizeof(struct in6_addr));
1231 freeaddrinfo(res);
1232 *naddr = 1;
1233 return addr;
1234 }
1235
1236 return NULL;
1237}
1238
1239static struct in6_addr *network_to_ip6addr(const char *name)
1240{
1241 /* abort();*/
1242 /* TODO: not implemented yet, but the exception breaks the
1243 * name resolvation */
1244 return NULL;
1245}
1246
1247static struct in6_addr *
1248ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1249{
1250 struct in6_addr *addrp, *addrptmp;
1251
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001252 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001253 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001254 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001255 memcpy(addrp, addrptmp, sizeof(*addrp));
1256 *naddrs = 1;
1257 return addrp;
1258 }
1259 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1260 return addrp;
1261
1262 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1263}
1264
1265static struct in6_addr *parse_ip6mask(char *mask)
1266{
1267 static struct in6_addr maskaddr;
1268 struct in6_addr *addrp;
1269 unsigned int bits;
1270
1271 if (mask == NULL) {
1272 /* no mask at all defaults to 128 bits */
1273 memset(&maskaddr, 0xff, sizeof maskaddr);
1274 return &maskaddr;
1275 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001276 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001277 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001278 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001279 exit_error(PARAMETER_PROBLEM,
1280 "invalid mask `%s' specified", mask);
1281 if (bits != 0) {
1282 char *p = (void *)&maskaddr;
1283 memset(p, 0xff, bits / 8);
1284 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1285 p[bits/8] = 0xff << (8 - (bits & 7));
1286 return &maskaddr;
1287 }
1288
1289 memset(&maskaddr, 0, sizeof(maskaddr));
1290 return &maskaddr;
1291}
1292
Jan Engelhardta0baae82009-01-30 04:32:50 +01001293void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1294 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001295{
1296 struct in6_addr *addrp;
1297 unsigned int i, j, k, n;
1298 char buf[256], *p;
1299
1300 strncpy(buf, name, sizeof(buf) - 1);
1301 buf[sizeof(buf)-1] = '\0';
1302 if ((p = strrchr(buf, '/')) != NULL) {
1303 *p = '\0';
1304 addrp = parse_ip6mask(p + 1);
1305 } else {
1306 addrp = parse_ip6mask(NULL);
1307 }
1308 memcpy(maskp, addrp, sizeof(*maskp));
1309
1310 /* if a null mask is given, the name is ignored, like in "any/0" */
1311 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1312 strcpy(buf, "::");
1313
1314 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1315 n = *naddrs;
1316 for (i = 0, j = 0; i < n; ++i) {
1317 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001318 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001319 ++j;
1320 for (k = 0; k < j - 1; ++k)
1321 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1322 --*naddrs;
1323 --j;
1324 break;
1325 }
1326 }
1327}
Max Kellermanna5d09942008-01-29 13:44:34 +00001328
Jan Engelhardta0baae82009-01-30 04:32:50 +01001329void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001330{
1331 static const char no_quote_chars[] = "_-0123456789"
1332 "abcdefghijklmnopqrstuvwxyz"
1333 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1334 static const char escape_chars[] = "\"\\'";
1335 size_t length;
1336 const char *p;
1337
1338 length = strcspn(value, no_quote_chars);
1339 if (length > 0 && value[length] == 0) {
1340 /* no quoting required */
1341 fputs(value, stdout);
1342 putchar(' ');
1343 } else {
1344 /* there is at least one dangerous character in the
1345 value, which we have to quote. Write double quotes
1346 around the value and escape special characters with
1347 a backslash */
1348 putchar('"');
1349
1350 for (p = strpbrk(value, escape_chars); p != NULL;
1351 p = strpbrk(value, escape_chars)) {
1352 if (p > value)
1353 fwrite(value, 1, p - value, stdout);
1354 putchar('\\');
1355 putchar(*p);
1356 value = p + 1;
1357 }
1358
1359 /* print the rest and finish the double quoted
1360 string */
1361 fputs(value, stdout);
1362 printf("\" ");
1363 }
1364}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001365
1366/**
1367 * Check for option-intrapositional negation.
1368 * Do not use in new code.
1369 */
1370int xtables_check_inverse(const char option[], int *invert,
1371 int *my_optind, int argc)
1372{
1373 if (option && strcmp(option, "!") == 0) {
1374 fprintf(stderr, "Using intrapositioned negation "
1375 "(`--option ! this`) is deprecated in favor of "
1376 "extrapositioned (`! --option this`).\n");
1377
1378 if (*invert)
1379 exit_error(PARAMETER_PROBLEM,
1380 "Multiple `!' flags not allowed");
1381 *invert = true;
1382 if (my_optind != NULL) {
1383 ++*my_optind;
1384 if (argc && *my_optind > argc)
1385 exit_error(PARAMETER_PROBLEM,
1386 "no argument following `!'");
1387 }
1388
1389 return true;
1390 }
1391 return false;
1392}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001393
1394const struct xtables_pprot xtables_chain_protos[] = {
1395 {"tcp", IPPROTO_TCP},
1396 {"sctp", IPPROTO_SCTP},
1397 {"udp", IPPROTO_UDP},
1398 {"udplite", IPPROTO_UDPLITE},
1399 {"icmp", IPPROTO_ICMP},
1400 {"icmpv6", IPPROTO_ICMPV6},
1401 {"ipv6-icmp", IPPROTO_ICMPV6},
1402 {"esp", IPPROTO_ESP},
1403 {"ah", IPPROTO_AH},
1404 {"ipv6-mh", IPPROTO_MH},
1405 {"mh", IPPROTO_MH},
1406 {"all", 0},
1407 {NULL},
1408};
1409
1410u_int16_t
1411xtables_parse_protocol(const char *s)
1412{
1413 unsigned int proto;
1414
1415 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1416 struct protoent *pent;
1417
1418 /* first deal with the special case of 'all' to prevent
1419 * people from being able to redefine 'all' in nsswitch
1420 * and/or provoke expensive [not working] ldap/nis/...
1421 * lookups */
1422 if (!strcmp(s, "all"))
1423 return 0;
1424
1425 if ((pent = getprotobyname(s)))
1426 proto = pent->p_proto;
1427 else {
1428 unsigned int i;
1429 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1430 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1431 proto = xtables_chain_protos[i].num;
1432 break;
1433 }
1434 }
1435 if (i == ARRAY_SIZE(xtables_chain_protos))
1436 exit_error(PARAMETER_PROBLEM,
1437 "unknown protocol `%s' specified",
1438 s);
1439 }
1440 }
1441
1442 return proto;
1443}