blob: 3c89a9d1f2a46df05fdedb4ebce6ea5830274308 [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
Jan Engelhardtc31870f2009-02-10 10:48:28 +010042#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
43# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
44# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
45#endif
46#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
47# define IP6T_SO_GET_REVISION_MATCH 68
48# define IP6T_SO_GET_REVISION_TARGET 69
49#endif
50
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000051
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000052#define NPROTO 255
53
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000054#ifndef PROC_SYS_MODPROBE
55#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
56#endif
57
Jan Engelhardtdacafa52009-01-27 20:56:23 +010058/**
Jan Engelhardt77f48c22009-02-07 19:59:53 +010059 * xtables_afinfo - protocol family dependent information
60 * @kmod: kernel module basename (e.g. "ip_tables")
61 * @libprefix: prefix of .so library name (e.g. "libipt_")
62 * @family: nfproto family
63 * @ipproto: used by setsockopt (e.g. IPPROTO_IP)
64 * @so_rev_match: optname to check revision support of match
65 * @so_rev_target: optname to check revision support of target
66 */
67struct xtables_afinfo {
68 const char *kmod;
69 const char *libprefix;
70 uint8_t family;
71 uint8_t ipproto;
72 int so_rev_match;
73 int so_rev_target;
74};
75
76static const struct xtables_afinfo afinfo_ipv4 = {
77 .kmod = "ip_tables",
78 .libprefix = "libipt_",
79 .family = NFPROTO_IPV4,
80 .ipproto = IPPROTO_IP,
81 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
82 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
83};
84
85static const struct xtables_afinfo afinfo_ipv6 = {
86 .kmod = "ip6_tables",
87 .libprefix = "libip6t_",
88 .family = NFPROTO_IPV6,
89 .ipproto = IPPROTO_IPV6,
90 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
91 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
92};
93
94static const struct xtables_afinfo *afinfo;
95
96/**
Jan Engelhardtdacafa52009-01-27 20:56:23 +010097 * Program will set this to its own name.
98 */
99const char *xtables_program_name;
100
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100101/* Search path for Xtables .so files */
102static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000103
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000104/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100105const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000106
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000107/* Keeping track of external matches and targets: linked lists. */
108struct xtables_match *xtables_matches;
109struct xtables_target *xtables_targets;
110
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100111void xtables_init(void)
112{
113 xtables_libdir = getenv("XTABLES_LIBDIR");
114 if (xtables_libdir != NULL)
115 return;
116 xtables_libdir = getenv("IPTABLES_LIB_DIR");
117 if (xtables_libdir != NULL) {
118 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
119 "use XTABLES_LIBDIR.\n");
120 return;
121 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100122 /*
123 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
124 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
125 * for these env vars are deprecated anyhow, and in light of the
126 * (shared) libxt_*.so files, makes less sense to have
127 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
128 */
129 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
130 if (xtables_libdir != NULL) {
131 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
132 "use XTABLES_LIBDIR.\n");
133 return;
134 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100135 xtables_libdir = XTABLES_LIBDIR;
136}
137
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100138void xtables_set_nfproto(uint8_t nfproto)
139{
140 switch (nfproto) {
141 case NFPROTO_IPV4:
142 afinfo = &afinfo_ipv4;
143 break;
144 case NFPROTO_IPV6:
145 afinfo = &afinfo_ipv6;
146 break;
147 default:
148 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
149 __func__);
150 }
151}
152
Jan Engelhardt630ef482009-01-27 14:58:41 +0100153/**
154 * xtables_*alloc - wrappers that exit on failure
155 */
156void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000157{
158 void *p;
159
160 if ((p = calloc(count, size)) == NULL) {
161 perror("ip[6]tables: calloc failed");
162 exit(1);
163 }
164
165 return p;
166}
167
Jan Engelhardt630ef482009-01-27 14:58:41 +0100168void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000169{
170 void *p;
171
172 if ((p = malloc(size)) == NULL) {
173 perror("ip[6]tables: malloc failed");
174 exit(1);
175 }
176
177 return p;
178}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000179
180static char *get_modprobe(void)
181{
182 int procfile;
183 char *ret;
184
185#define PROCFILE_BUFSIZ 1024
186 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
187 if (procfile < 0)
188 return NULL;
189
190 ret = (char *) malloc(PROCFILE_BUFSIZ);
191 if (ret) {
192 memset(ret, 0, PROCFILE_BUFSIZ);
193 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
194 case -1: goto fail;
195 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
196 }
197 if (ret[strlen(ret)-1]=='\n')
198 ret[strlen(ret)-1]=0;
199 close(procfile);
200 return ret;
201 }
202 fail:
203 free(ret);
204 close(procfile);
205 return NULL;
206}
207
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100208int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000209{
210 char *buf = NULL;
211 char *argv[4];
212 int status;
213
214 /* If they don't explicitly set it, read out of kernel */
215 if (!modprobe) {
216 buf = get_modprobe();
217 if (!buf)
218 return -1;
219 modprobe = buf;
220 }
221
222 switch (fork()) {
223 case 0:
224 argv[0] = (char *)modprobe;
225 argv[1] = (char *)modname;
226 if (quiet) {
227 argv[2] = "-q";
228 argv[3] = NULL;
229 } else {
230 argv[2] = NULL;
231 argv[3] = NULL;
232 }
233 execv(argv[0], argv);
234
235 /* not usually reached */
236 exit(1);
237 case -1:
238 return -1;
239
240 default: /* parent */
241 wait(&status);
242 }
243
244 free(buf);
245 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
246 return 0;
247 return -1;
248}
249
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100250int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000251{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100252 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000253 static int ret = -1;
254
255 if (!loaded) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100256 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000257 loaded = (ret == 0);
258 }
259
260 return ret;
261}
262
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100263/**
264 * xtables_strtou{i,l} - string to number conversion
265 * @s: input string
266 * @end: like strtoul's "end" pointer
267 * @value: pointer for result
268 * @min: minimum accepted value
269 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000270 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100271 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
272 * "15a" is rejected.
273 * In either case, the value obtained is compared for min-max compliance.
274 * Base is always 0, i.e. autodetect depending on @s.
275 *
276 * Returns true/false whether number was accepted. On failure, *value has
277 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000278 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100279bool xtables_strtoul(const char *s, char **end, unsigned long *value,
280 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000281{
282 unsigned long v;
283 char *my_end;
284
285 errno = 0;
286 v = strtoul(s, &my_end, 0);
287
288 if (my_end == s)
289 return false;
290 if (end != NULL)
291 *end = my_end;
292
293 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
294 if (value != NULL)
295 *value = v;
296 if (end == NULL)
297 return *my_end == '\0';
298 return true;
299 }
300
301 return false;
302}
303
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100304bool xtables_strtoui(const char *s, char **end, unsigned int *value,
305 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000306{
307 unsigned long v;
308 bool ret;
309
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100310 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000311 if (value != NULL)
312 *value = v;
313 return ret;
314}
315
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100316int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000317{
318 struct servent *service;
319
320 if ((service = getservbyname(name, proto)) != NULL)
321 return ntohs((unsigned short) service->s_port);
322
323 return -1;
324}
325
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100326u_int16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000327{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100328 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000329
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100330 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100331 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100332 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000333
334 exit_error(PARAMETER_PROBLEM,
335 "invalid port/service `%s' specified", port);
336}
337
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100338void xtables_parse_interface(const char *arg, char *vianame,
339 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000340{
341 int vialen = strlen(arg);
342 unsigned int i;
343
344 memset(mask, 0, IFNAMSIZ);
345 memset(vianame, 0, IFNAMSIZ);
346
347 if (vialen + 1 > IFNAMSIZ)
348 exit_error(PARAMETER_PROBLEM,
349 "interface name `%s' must be shorter than IFNAMSIZ"
350 " (%i)", arg, IFNAMSIZ-1);
351
352 strcpy(vianame, arg);
353 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
354 memset(mask, 0, IFNAMSIZ);
355 else if (vianame[vialen - 1] == '+') {
356 memset(mask, 0xFF, vialen - 1);
357 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
358 /* Don't remove `+' here! -HW */
359 } else {
360 /* Include nul-terminator in match */
361 memset(mask, 0xFF, vialen + 1);
362 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
363 for (i = 0; vianame[i]; i++) {
364 if (vianame[i] == ':' ||
365 vianame[i] == '!' ||
366 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000367 fprintf(stderr,
368 "Warning: weird character in interface"
369 " `%s' (No aliases, :, ! or *).\n",
370 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000371 break;
372 }
373 }
374 }
375}
376
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200377#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100378static void *load_extension(const char *search_path, const char *prefix,
379 const char *name, bool is_target)
380{
381 const char *dir = search_path, *next;
382 void *ptr = NULL;
383 struct stat sb;
384 char path[256];
385
386 do {
387 next = strchr(dir, ':');
388 if (next == NULL)
389 next = dir + strlen(dir);
390 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200391 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100392
393 if (dlopen(path, RTLD_NOW) != NULL) {
394 /* Found library. If it didn't register itself,
395 maybe they specified target as match. */
396 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100397 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100398 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100399 ptr = xtables_find_match(name,
400 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100401 } else if (stat(path, &sb) == 0) {
402 fprintf(stderr, "%s: %s\n", path, dlerror());
403 }
404
405 if (ptr != NULL)
406 return ptr;
407
408 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200409 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100410 if (dlopen(path, RTLD_NOW) != NULL) {
411 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100412 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100413 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100414 ptr = xtables_find_match(name,
415 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100416 } else if (stat(path, &sb) == 0) {
417 fprintf(stderr, "%s: %s\n", path, dlerror());
418 }
419
420 if (ptr != NULL)
421 return ptr;
422
423 dir = next + 1;
424 } while (*next != '\0');
425
426 return NULL;
427}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200428#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100429
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100430struct xtables_match *
431xtables_find_match(const char *name, enum xtables_tryload tryload,
432 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000433{
434 struct xtables_match *ptr;
435 const char *icmp6 = "icmp6";
436
437 /* This is ugly as hell. Nonetheless, there is no way of changing
438 * this without hurting backwards compatibility */
439 if ( (strcmp(name,"icmpv6") == 0) ||
440 (strcmp(name,"ipv6-icmp") == 0) ||
441 (strcmp(name,"icmp6") == 0) )
442 name = icmp6;
443
444 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
445 if (strcmp(name, ptr->name) == 0) {
446 struct xtables_match *clone;
447
448 /* First match of this type: */
449 if (ptr->m == NULL)
450 break;
451
452 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100453 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000454 memcpy(clone, ptr, sizeof(struct xtables_match));
455 clone->mflags = 0;
456 /* This is a clone: */
457 clone->next = clone;
458
459 ptr = clone;
460 break;
461 }
462 }
463
464#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100465 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100466 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100467 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000468
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100469 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000470 exit_error(PARAMETER_PROBLEM,
471 "Couldn't load match `%s':%s\n",
472 name, dlerror());
473 }
474#else
475 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100476 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000477 ptr->loaded = 1;
478 else
479 ptr = NULL;
480 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100481 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000482 exit_error(PARAMETER_PROBLEM,
483 "Couldn't find match `%s'\n", name);
484 }
485#endif
486
487 if (ptr && matches) {
488 struct xtables_rule_match **i;
489 struct xtables_rule_match *newentry;
490
Jan Engelhardt630ef482009-01-27 14:58:41 +0100491 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000492
493 for (i = matches; *i; i = &(*i)->next) {
494 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100495 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000496 }
497 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100498 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000499 newentry->next = NULL;
500 *i = newentry;
501 }
502
503 return ptr;
504}
505
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100506struct xtables_target *
507xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000508{
509 struct xtables_target *ptr;
510
511 /* Standard target? */
512 if (strcmp(name, "") == 0
513 || strcmp(name, XTC_LABEL_ACCEPT) == 0
514 || strcmp(name, XTC_LABEL_DROP) == 0
515 || strcmp(name, XTC_LABEL_QUEUE) == 0
516 || strcmp(name, XTC_LABEL_RETURN) == 0)
517 name = "standard";
518
519 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
520 if (strcmp(name, ptr->name) == 0)
521 break;
522 }
523
524#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100525 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100526 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100527 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000528
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100529 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000530 exit_error(PARAMETER_PROBLEM,
531 "Couldn't load target `%s':%s\n",
532 name, dlerror());
533 }
534#else
535 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100536 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000537 ptr->loaded = 1;
538 else
539 ptr = NULL;
540 }
541 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
542 exit_error(PARAMETER_PROBLEM,
543 "Couldn't find target `%s'\n", name);
544 }
545#endif
546
547 if (ptr)
548 ptr->used = 1;
549
550 return ptr;
551}
552
553static int compatible_revision(const char *name, u_int8_t revision, int opt)
554{
555 struct xt_get_revision rev;
556 socklen_t s = sizeof(rev);
557 int max_rev, sockfd;
558
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100559 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000560 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000561 if (errno == EPERM) {
562 /* revision 0 is always supported. */
563 if (revision != 0)
564 fprintf(stderr, "Could not determine whether "
565 "revision %u is supported, "
566 "assuming it is.\n",
567 revision);
568 return 1;
569 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000570 fprintf(stderr, "Could not open socket to kernel: %s\n",
571 strerror(errno));
572 exit(1);
573 }
574
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100575 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000576
577 strcpy(rev.name, name);
578 rev.revision = revision;
579
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100580 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000581 if (max_rev < 0) {
582 /* Definitely don't support this? */
583 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
584 close(sockfd);
585 return 0;
586 } else if (errno == ENOPROTOOPT) {
587 close(sockfd);
588 /* Assume only revision 0 support (old kernel) */
589 return (revision == 0);
590 } else {
591 fprintf(stderr, "getsockopt failed strangely: %s\n",
592 strerror(errno));
593 exit(1);
594 }
595 }
596 close(sockfd);
597 return 1;
598}
599
600
601static int compatible_match_revision(const char *name, u_int8_t revision)
602{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100603 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000604}
605
606static int compatible_target_revision(const char *name, u_int8_t revision)
607{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100608 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000609}
610
611void xtables_register_match(struct xtables_match *me)
612{
613 struct xtables_match **i, *old;
614
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100615 if (strcmp(me->version, XTABLES_VERSION) != 0) {
616 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
617 "but \"%s\" is required.\n",
618 xtables_program_name, me->name,
619 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000620 exit(1);
621 }
622
623 /* Revision field stole a char from name. */
624 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
625 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100626 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000627 exit(1);
628 }
629
630 if (me->family >= NPROTO) {
631 fprintf(stderr,
632 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100633 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000634 exit(1);
635 }
636
637 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100638 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000639 return;
640
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100641 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000642 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100643 if (old->revision == me->revision &&
644 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000645 fprintf(stderr,
646 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100647 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000648 exit(1);
649 }
650
651 /* Now we have two (or more) options, check compatibility. */
652 if (compatible_match_revision(old->name, old->revision)
653 && old->revision > me->revision)
654 return;
655
Jan Engelhardt23545c22008-02-14 04:23:04 +0100656 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000657 if (!compatible_match_revision(me->name, me->revision))
658 return;
659
Jan Engelhardt23545c22008-02-14 04:23:04 +0100660 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
661 if (old->revision == me->revision && me->family == AF_UNSPEC)
662 return;
663
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000664 /* Delete old one. */
665 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
666 *i = old->next;
667 }
668
669 if (me->size != XT_ALIGN(me->size)) {
670 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100671 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000672 exit(1);
673 }
674
675 /* Append to list. */
676 for (i = &xtables_matches; *i; i = &(*i)->next);
677 me->next = NULL;
678 *i = me;
679
680 me->m = NULL;
681 me->mflags = 0;
682}
683
684void xtables_register_target(struct xtables_target *me)
685{
686 struct xtables_target *old;
687
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100688 if (strcmp(me->version, XTABLES_VERSION) != 0) {
689 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
690 "but \"%s\" is required.\n",
691 xtables_program_name, me->name,
692 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000693 exit(1);
694 }
695
696 /* Revision field stole a char from name. */
697 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
698 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100699 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000700 exit(1);
701 }
702
703 if (me->family >= NPROTO) {
704 fprintf(stderr,
705 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100706 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000707 exit(1);
708 }
709
710 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100711 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000712 return;
713
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100714 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000715 if (old) {
716 struct xtables_target **i;
717
Jan Engelhardt23545c22008-02-14 04:23:04 +0100718 if (old->revision == me->revision &&
719 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000720 fprintf(stderr,
721 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100722 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000723 exit(1);
724 }
725
726 /* Now we have two (or more) options, check compatibility. */
727 if (compatible_target_revision(old->name, old->revision)
728 && old->revision > me->revision)
729 return;
730
Jan Engelhardt23545c22008-02-14 04:23:04 +0100731 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000732 if (!compatible_target_revision(me->name, me->revision))
733 return;
734
Jan Engelhardt23545c22008-02-14 04:23:04 +0100735 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
736 if (old->revision == me->revision && me->family == AF_UNSPEC)
737 return;
738
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000739 /* Delete old one. */
740 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
741 *i = old->next;
742 }
743
744 if (me->size != XT_ALIGN(me->size)) {
745 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100746 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000747 exit(1);
748 }
749
750 /* Prepend to list. */
751 me->next = xtables_targets;
752 xtables_targets = me;
753 me->t = NULL;
754 me->tflags = 0;
755}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000756
Jan Engelhardta41545c2009-01-27 21:27:19 +0100757/**
758 * xtables_param_act - act on condition
759 * @status: a constant from enum xtables_exittype
760 *
761 * %XTF_ONLY_ONCE: print error message that option may only be used once.
762 * @p1: module name (e.g. "mark")
763 * @p2(...): option in conflict (e.g. "--mark")
764 * @p3(...): condition to match on (see extensions/ for examples)
765 *
766 * %XTF_NO_INVERT: option does not support inversion
767 * @p1: module name
768 * @p2: option in conflict
769 * @p3: condition to match on
770 *
771 * %XTF_BAD_VALUE: bad value for option
772 * @p1: module name
773 * @p2: option with which the problem occured (e.g. "--mark")
774 * @p3: string the user passed in (e.g. "99999999999999")
775 *
776 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
777 * @p1: module name
778 *
779 * Displays an error message and exits the program.
780 */
781void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000782{
783 const char *p2, *p3;
784 va_list args;
785 bool b;
786
787 va_start(args, p1);
788
789 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +0100790 case XTF_ONLY_ONCE:
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 may only be specified once",
797 p1, p2);
798 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100799 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000800 p2 = va_arg(args, const char *);
801 b = va_arg(args, unsigned int);
802 if (!b)
803 return;
804 exit_error(PARAMETER_PROBLEM,
805 "%s: \"%s\" option cannot be inverted", p1, p2);
806 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100807 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000808 p2 = va_arg(args, const char *);
809 p3 = va_arg(args, const char *);
810 exit_error(PARAMETER_PROBLEM,
811 "%s: Bad value for \"%s\" option: \"%s\"",
812 p1, p2, p3);
813 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +0100814 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000815 b = va_arg(args, unsigned int);
816 if (!b)
817 return;
818 exit_error(PARAMETER_PROBLEM,
819 "%s: At most one action is possible", p1);
820 break;
821 default:
822 exit_error(status, p1, args);
823 break;
824 }
825
826 va_end(args);
827}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000828
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100829const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000830{
831 static char buf[20];
832 const unsigned char *bytep = (const void *)&addrp->s_addr;
833
834 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
835 return buf;
836}
837
838static const char *ipaddr_to_host(const struct in_addr *addr)
839{
840 struct hostent *host;
841
842 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
843 if (host == NULL)
844 return NULL;
845
846 return host->h_name;
847}
848
849static const char *ipaddr_to_network(const struct in_addr *addr)
850{
851 struct netent *net;
852
853 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
854 return net->n_name;
855
856 return NULL;
857}
858
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100859const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000860{
861 const char *name;
862
863 if ((name = ipaddr_to_host(addr)) != NULL ||
864 (name = ipaddr_to_network(addr)) != NULL)
865 return name;
866
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100867 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000868}
869
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100870const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000871{
872 static char buf[20];
873 uint32_t maskaddr, bits;
874 int i;
875
876 maskaddr = ntohl(mask->s_addr);
877
878 if (maskaddr == 0xFFFFFFFFL)
879 /* we don't want to see "/32" */
880 return "";
881
882 i = 32;
883 bits = 0xFFFFFFFEL;
884 while (--i >= 0 && maskaddr != bits)
885 bits <<= 1;
886 if (i >= 0)
887 sprintf(buf, "/%d", i);
888 else
889 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100890 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000891
892 return buf;
893}
894
Jan Engelhardtbd943842008-01-20 13:38:08 +0000895static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
896{
897 static struct in_addr addr;
898 unsigned char *addrp;
899 unsigned int onebyte;
900 char buf[20], *p, *q;
901 int i;
902
903 /* copy dotted string, because we need to modify it */
904 strncpy(buf, dotted, sizeof(buf) - 1);
905 buf[sizeof(buf) - 1] = '\0';
906 addrp = (void *)&addr.s_addr;
907
908 p = buf;
909 for (i = 0; i < 3; ++i) {
910 if ((q = strchr(p, '.')) == NULL) {
911 if (is_mask)
912 return NULL;
913
914 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100915 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000916 return NULL;
917
918 addrp[i] = onebyte;
919 while (i < 3)
920 addrp[++i] = 0;
921
922 return &addr;
923 }
924
925 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100926 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000927 return NULL;
928
929 addrp[i] = onebyte;
930 p = q + 1;
931 }
932
933 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100934 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000935 return NULL;
936
937 addrp[3] = onebyte;
938 return &addr;
939}
940
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100941struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000942{
943 return __numeric_to_ipaddr(dotted, false);
944}
945
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100946struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +0000947{
948 return __numeric_to_ipaddr(dotted, true);
949}
950
951static struct in_addr *network_to_ipaddr(const char *name)
952{
953 static struct in_addr addr;
954 struct netent *net;
955
956 if ((net = getnetbyname(name)) != NULL) {
957 if (net->n_addrtype != AF_INET)
958 return NULL;
959 addr.s_addr = htonl(net->n_net);
960 return &addr;
961 }
962
963 return NULL;
964}
965
966static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
967{
968 struct hostent *host;
969 struct in_addr *addr;
970 unsigned int i;
971
972 *naddr = 0;
973 if ((host = gethostbyname(name)) != NULL) {
974 if (host->h_addrtype != AF_INET ||
975 host->h_length != sizeof(struct in_addr))
976 return NULL;
977
978 while (host->h_addr_list[*naddr] != NULL)
979 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100980 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000981 for (i = 0; i < *naddr; i++)
982 memcpy(&addr[i], host->h_addr_list[i],
983 sizeof(struct in_addr));
984 return addr;
985 }
986
987 return NULL;
988}
989
990static struct in_addr *
991ipparse_hostnetwork(const char *name, unsigned int *naddrs)
992{
993 struct in_addr *addrptmp, *addrp;
994
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100995 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +0000996 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +0100997 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +0000998 memcpy(addrp, addrptmp, sizeof(*addrp));
999 *naddrs = 1;
1000 return addrp;
1001 }
1002 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1003 return addrptmp;
1004
1005 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1006}
1007
1008static struct in_addr *parse_ipmask(const char *mask)
1009{
1010 static struct in_addr maskaddr;
1011 struct in_addr *addrp;
1012 unsigned int bits;
1013
1014 if (mask == NULL) {
1015 /* no mask at all defaults to 32 bits */
1016 maskaddr.s_addr = 0xFFFFFFFF;
1017 return &maskaddr;
1018 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001019 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001020 /* dotted_to_addr already returns a network byte order addr */
1021 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001022 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001023 exit_error(PARAMETER_PROBLEM,
1024 "invalid mask `%s' specified", mask);
1025 if (bits != 0) {
1026 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1027 return &maskaddr;
1028 }
1029
1030 maskaddr.s_addr = 0U;
1031 return &maskaddr;
1032}
1033
Jan Engelhardta0baae82009-01-30 04:32:50 +01001034/**
1035 * xtables_ipparse_any - transform arbitrary name to in_addr
1036 *
1037 * Possible inputs (pseudo regex):
1038 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1039 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1040 */
1041void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1042 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001043{
1044 unsigned int i, j, k, n;
1045 struct in_addr *addrp;
1046 char buf[256], *p;
1047
1048 strncpy(buf, name, sizeof(buf) - 1);
1049 buf[sizeof(buf) - 1] = '\0';
1050 if ((p = strrchr(buf, '/')) != NULL) {
1051 *p = '\0';
1052 addrp = parse_ipmask(p + 1);
1053 } else {
1054 addrp = parse_ipmask(NULL);
1055 }
1056 memcpy(maskp, addrp, sizeof(*maskp));
1057
1058 /* if a null mask is given, the name is ignored, like in "any/0" */
1059 if (maskp->s_addr == 0U)
1060 strcpy(buf, "0.0.0.0");
1061
1062 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1063 n = *naddrs;
1064 for (i = 0, j = 0; i < n; ++i) {
1065 addrp[j++].s_addr &= maskp->s_addr;
1066 for (k = 0; k < j - 1; ++k)
1067 if (addrp[k].s_addr == addrp[j-1].s_addr) {
1068 --*naddrs;
1069 --j;
1070 break;
1071 }
1072 }
1073}
1074
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001075const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001076{
1077 /* 0000:0000:0000:0000:0000:000.000.000.000
1078 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1079 static char buf[50+1];
1080 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1081}
1082
1083static const char *ip6addr_to_host(const struct in6_addr *addr)
1084{
1085 static char hostname[NI_MAXHOST];
1086 struct sockaddr_in6 saddr;
1087 int err;
1088
1089 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1090 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1091 saddr.sin6_family = AF_INET6;
1092
1093 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1094 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1095 if (err != 0) {
1096#ifdef DEBUG
1097 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1098#endif
1099 return NULL;
1100 }
1101
1102#ifdef DEBUG
1103 fprintf (stderr, "\naddr2host: %s\n", hostname);
1104#endif
1105 return hostname;
1106}
1107
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001108const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001109{
1110 const char *name;
1111
1112 if ((name = ip6addr_to_host(addr)) != NULL)
1113 return name;
1114
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001115 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001116}
1117
1118static int ip6addr_prefix_length(const struct in6_addr *k)
1119{
1120 unsigned int bits = 0;
1121 uint32_t a, b, c, d;
1122
Jan Engelhardt48607812008-06-10 15:17:53 +02001123 a = ntohl(k->s6_addr32[0]);
1124 b = ntohl(k->s6_addr32[1]);
1125 c = ntohl(k->s6_addr32[2]);
1126 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001127 while (a & 0x80000000U) {
1128 ++bits;
1129 a <<= 1;
1130 a |= (b >> 31) & 1;
1131 b <<= 1;
1132 b |= (c >> 31) & 1;
1133 c <<= 1;
1134 c |= (d >> 31) & 1;
1135 d <<= 1;
1136 }
1137 if (a != 0 || b != 0 || c != 0 || d != 0)
1138 return -1;
1139 return bits;
1140}
1141
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001142const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001143{
1144 static char buf[50+2];
1145 int l = ip6addr_prefix_length(addrp);
1146
1147 if (l == -1) {
1148 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001149 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001150 return buf;
1151 }
1152 sprintf(buf, "/%d", l);
1153 return buf;
1154}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001155
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001156struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001157{
1158 static struct in6_addr ap;
1159 int err;
1160
1161 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1162 return &ap;
1163#ifdef DEBUG
1164 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1165#endif
1166 return NULL;
1167}
1168
1169static struct in6_addr *
1170host_to_ip6addr(const char *name, unsigned int *naddr)
1171{
1172 static struct in6_addr *addr;
1173 struct addrinfo hints;
1174 struct addrinfo *res;
1175 int err;
1176
1177 memset(&hints, 0, sizeof(hints));
1178 hints.ai_flags = AI_CANONNAME;
1179 hints.ai_family = AF_INET6;
1180 hints.ai_socktype = SOCK_RAW;
1181 hints.ai_protocol = IPPROTO_IPV6;
1182 hints.ai_next = NULL;
1183
1184 *naddr = 0;
1185 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1186#ifdef DEBUG
1187 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1188#endif
1189 return NULL;
1190 } else {
1191 if (res->ai_family != AF_INET6 ||
1192 res->ai_addrlen != sizeof(struct sockaddr_in6))
1193 return NULL;
1194
1195#ifdef DEBUG
1196 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1197 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1198#endif
1199 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001200 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001201 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1202 sizeof(struct in6_addr));
1203 freeaddrinfo(res);
1204 *naddr = 1;
1205 return addr;
1206 }
1207
1208 return NULL;
1209}
1210
1211static struct in6_addr *network_to_ip6addr(const char *name)
1212{
1213 /* abort();*/
1214 /* TODO: not implemented yet, but the exception breaks the
1215 * name resolvation */
1216 return NULL;
1217}
1218
1219static struct in6_addr *
1220ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1221{
1222 struct in6_addr *addrp, *addrptmp;
1223
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001224 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001225 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001226 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001227 memcpy(addrp, addrptmp, sizeof(*addrp));
1228 *naddrs = 1;
1229 return addrp;
1230 }
1231 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1232 return addrp;
1233
1234 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1235}
1236
1237static struct in6_addr *parse_ip6mask(char *mask)
1238{
1239 static struct in6_addr maskaddr;
1240 struct in6_addr *addrp;
1241 unsigned int bits;
1242
1243 if (mask == NULL) {
1244 /* no mask at all defaults to 128 bits */
1245 memset(&maskaddr, 0xff, sizeof maskaddr);
1246 return &maskaddr;
1247 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001248 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001249 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001250 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001251 exit_error(PARAMETER_PROBLEM,
1252 "invalid mask `%s' specified", mask);
1253 if (bits != 0) {
1254 char *p = (void *)&maskaddr;
1255 memset(p, 0xff, bits / 8);
1256 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1257 p[bits/8] = 0xff << (8 - (bits & 7));
1258 return &maskaddr;
1259 }
1260
1261 memset(&maskaddr, 0, sizeof(maskaddr));
1262 return &maskaddr;
1263}
1264
Jan Engelhardta0baae82009-01-30 04:32:50 +01001265void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1266 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001267{
1268 struct in6_addr *addrp;
1269 unsigned int i, j, k, n;
1270 char buf[256], *p;
1271
1272 strncpy(buf, name, sizeof(buf) - 1);
1273 buf[sizeof(buf)-1] = '\0';
1274 if ((p = strrchr(buf, '/')) != NULL) {
1275 *p = '\0';
1276 addrp = parse_ip6mask(p + 1);
1277 } else {
1278 addrp = parse_ip6mask(NULL);
1279 }
1280 memcpy(maskp, addrp, sizeof(*maskp));
1281
1282 /* if a null mask is given, the name is ignored, like in "any/0" */
1283 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1284 strcpy(buf, "::");
1285
1286 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1287 n = *naddrs;
1288 for (i = 0, j = 0; i < n; ++i) {
1289 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001290 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001291 ++j;
1292 for (k = 0; k < j - 1; ++k)
1293 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1294 --*naddrs;
1295 --j;
1296 break;
1297 }
1298 }
1299}
Max Kellermanna5d09942008-01-29 13:44:34 +00001300
Jan Engelhardta0baae82009-01-30 04:32:50 +01001301void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001302{
1303 static const char no_quote_chars[] = "_-0123456789"
1304 "abcdefghijklmnopqrstuvwxyz"
1305 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1306 static const char escape_chars[] = "\"\\'";
1307 size_t length;
1308 const char *p;
1309
1310 length = strcspn(value, no_quote_chars);
1311 if (length > 0 && value[length] == 0) {
1312 /* no quoting required */
1313 fputs(value, stdout);
1314 putchar(' ');
1315 } else {
1316 /* there is at least one dangerous character in the
1317 value, which we have to quote. Write double quotes
1318 around the value and escape special characters with
1319 a backslash */
1320 putchar('"');
1321
1322 for (p = strpbrk(value, escape_chars); p != NULL;
1323 p = strpbrk(value, escape_chars)) {
1324 if (p > value)
1325 fwrite(value, 1, p - value, stdout);
1326 putchar('\\');
1327 putchar(*p);
1328 value = p + 1;
1329 }
1330
1331 /* print the rest and finish the double quoted
1332 string */
1333 fputs(value, stdout);
1334 printf("\" ");
1335 }
1336}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001337
1338/**
1339 * Check for option-intrapositional negation.
1340 * Do not use in new code.
1341 */
1342int xtables_check_inverse(const char option[], int *invert,
1343 int *my_optind, int argc)
1344{
1345 if (option && strcmp(option, "!") == 0) {
1346 fprintf(stderr, "Using intrapositioned negation "
1347 "(`--option ! this`) is deprecated in favor of "
1348 "extrapositioned (`! --option this`).\n");
1349
1350 if (*invert)
1351 exit_error(PARAMETER_PROBLEM,
1352 "Multiple `!' flags not allowed");
1353 *invert = true;
1354 if (my_optind != NULL) {
1355 ++*my_optind;
1356 if (argc && *my_optind > argc)
1357 exit_error(PARAMETER_PROBLEM,
1358 "no argument following `!'");
1359 }
1360
1361 return true;
1362 }
1363 return false;
1364}
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001365
1366const struct xtables_pprot xtables_chain_protos[] = {
1367 {"tcp", IPPROTO_TCP},
1368 {"sctp", IPPROTO_SCTP},
1369 {"udp", IPPROTO_UDP},
1370 {"udplite", IPPROTO_UDPLITE},
1371 {"icmp", IPPROTO_ICMP},
1372 {"icmpv6", IPPROTO_ICMPV6},
1373 {"ipv6-icmp", IPPROTO_ICMPV6},
1374 {"esp", IPPROTO_ESP},
1375 {"ah", IPPROTO_AH},
1376 {"ipv6-mh", IPPROTO_MH},
1377 {"mh", IPPROTO_MH},
1378 {"all", 0},
1379 {NULL},
1380};
1381
1382u_int16_t
1383xtables_parse_protocol(const char *s)
1384{
1385 unsigned int proto;
1386
1387 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
1388 struct protoent *pent;
1389
1390 /* first deal with the special case of 'all' to prevent
1391 * people from being able to redefine 'all' in nsswitch
1392 * and/or provoke expensive [not working] ldap/nis/...
1393 * lookups */
1394 if (!strcmp(s, "all"))
1395 return 0;
1396
1397 if ((pent = getprotobyname(s)))
1398 proto = pent->p_proto;
1399 else {
1400 unsigned int i;
1401 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1402 if (strcmp(s, xtables_chain_protos[i].name) == 0) {
1403 proto = xtables_chain_protos[i].num;
1404 break;
1405 }
1406 }
1407 if (i == ARRAY_SIZE(xtables_chain_protos))
1408 exit_error(PARAMETER_PROBLEM,
1409 "unknown protocol `%s' specified",
1410 s);
1411 }
1412 }
1413
1414 return proto;
1415}