blob: 642c04bb1bc209f8ed89a66eeb0ce7ee7595121e [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 Engelhardtef18e812008-08-04 12:47:48 +020035#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000036
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000037#ifndef NO_SHARED_LIBS
38#include <dlfcn.h>
39#endif
40
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000041#define NPROTO 255
42
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000043#ifndef PROC_SYS_MODPROBE
44#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
45#endif
46
Jan Engelhardtdacafa52009-01-27 20:56:23 +010047/**
48 * Program will set this to its own name.
49 */
50const char *xtables_program_name;
51
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010052/* Search path for Xtables .so files */
53static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000054
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000055/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010056const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000057
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000058/* Keeping track of external matches and targets: linked lists. */
59struct xtables_match *xtables_matches;
60struct xtables_target *xtables_targets;
61
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010062void xtables_init(void)
63{
64 xtables_libdir = getenv("XTABLES_LIBDIR");
65 if (xtables_libdir != NULL)
66 return;
67 xtables_libdir = getenv("IPTABLES_LIB_DIR");
68 if (xtables_libdir != NULL) {
69 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
70 "use XTABLES_LIBDIR.\n");
71 return;
72 }
73 xtables_libdir = XTABLES_LIBDIR;
74}
75
Jan Engelhardt630ef482009-01-27 14:58:41 +010076/**
77 * xtables_*alloc - wrappers that exit on failure
78 */
79void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000080{
81 void *p;
82
83 if ((p = calloc(count, size)) == NULL) {
84 perror("ip[6]tables: calloc failed");
85 exit(1);
86 }
87
88 return p;
89}
90
Jan Engelhardt630ef482009-01-27 14:58:41 +010091void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000092{
93 void *p;
94
95 if ((p = malloc(size)) == NULL) {
96 perror("ip[6]tables: malloc failed");
97 exit(1);
98 }
99
100 return p;
101}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000102
103static char *get_modprobe(void)
104{
105 int procfile;
106 char *ret;
107
108#define PROCFILE_BUFSIZ 1024
109 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
110 if (procfile < 0)
111 return NULL;
112
113 ret = (char *) malloc(PROCFILE_BUFSIZ);
114 if (ret) {
115 memset(ret, 0, PROCFILE_BUFSIZ);
116 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
117 case -1: goto fail;
118 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
119 }
120 if (ret[strlen(ret)-1]=='\n')
121 ret[strlen(ret)-1]=0;
122 close(procfile);
123 return ret;
124 }
125 fail:
126 free(ret);
127 close(procfile);
128 return NULL;
129}
130
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100131int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000132{
133 char *buf = NULL;
134 char *argv[4];
135 int status;
136
137 /* If they don't explicitly set it, read out of kernel */
138 if (!modprobe) {
139 buf = get_modprobe();
140 if (!buf)
141 return -1;
142 modprobe = buf;
143 }
144
145 switch (fork()) {
146 case 0:
147 argv[0] = (char *)modprobe;
148 argv[1] = (char *)modname;
149 if (quiet) {
150 argv[2] = "-q";
151 argv[3] = NULL;
152 } else {
153 argv[2] = NULL;
154 argv[3] = NULL;
155 }
156 execv(argv[0], argv);
157
158 /* not usually reached */
159 exit(1);
160 case -1:
161 return -1;
162
163 default: /* parent */
164 wait(&status);
165 }
166
167 free(buf);
168 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
169 return 0;
170 return -1;
171}
172
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100173int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000174{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100175 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000176 static int ret = -1;
177
178 if (!loaded) {
179 ret = xtables_insmod(afinfo.kmod, modprobe, quiet);
180 loaded = (ret == 0);
181 }
182
183 return ret;
184}
185
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100186/**
187 * xtables_strtou{i,l} - string to number conversion
188 * @s: input string
189 * @end: like strtoul's "end" pointer
190 * @value: pointer for result
191 * @min: minimum accepted value
192 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000193 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100194 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
195 * "15a" is rejected.
196 * In either case, the value obtained is compared for min-max compliance.
197 * Base is always 0, i.e. autodetect depending on @s.
198 *
199 * Returns true/false whether number was accepted. On failure, *value has
200 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000201 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100202bool xtables_strtoul(const char *s, char **end, unsigned long *value,
203 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000204{
205 unsigned long v;
206 char *my_end;
207
208 errno = 0;
209 v = strtoul(s, &my_end, 0);
210
211 if (my_end == s)
212 return false;
213 if (end != NULL)
214 *end = my_end;
215
216 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
217 if (value != NULL)
218 *value = v;
219 if (end == NULL)
220 return *my_end == '\0';
221 return true;
222 }
223
224 return false;
225}
226
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100227bool xtables_strtoui(const char *s, char **end, unsigned int *value,
228 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000229{
230 unsigned long v;
231 bool ret;
232
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100233 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000234 if (value != NULL)
235 *value = v;
236 return ret;
237}
238
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000239int service_to_port(const char *name, const char *proto)
240{
241 struct servent *service;
242
243 if ((service = getservbyname(name, proto)) != NULL)
244 return ntohs((unsigned short) service->s_port);
245
246 return -1;
247}
248
249u_int16_t parse_port(const char *port, const char *proto)
250{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100251 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000252
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100253 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Max Kellermann9ee386a2008-01-29 13:48:05 +0000254 (portnum = service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100255 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000256
257 exit_error(PARAMETER_PROBLEM,
258 "invalid port/service `%s' specified", port);
259}
260
261void parse_interface(const char *arg, char *vianame, unsigned char *mask)
262{
263 int vialen = strlen(arg);
264 unsigned int i;
265
266 memset(mask, 0, IFNAMSIZ);
267 memset(vianame, 0, IFNAMSIZ);
268
269 if (vialen + 1 > IFNAMSIZ)
270 exit_error(PARAMETER_PROBLEM,
271 "interface name `%s' must be shorter than IFNAMSIZ"
272 " (%i)", arg, IFNAMSIZ-1);
273
274 strcpy(vianame, arg);
275 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
276 memset(mask, 0, IFNAMSIZ);
277 else if (vianame[vialen - 1] == '+') {
278 memset(mask, 0xFF, vialen - 1);
279 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
280 /* Don't remove `+' here! -HW */
281 } else {
282 /* Include nul-terminator in match */
283 memset(mask, 0xFF, vialen + 1);
284 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
285 for (i = 0; vianame[i]; i++) {
286 if (vianame[i] == ':' ||
287 vianame[i] == '!' ||
288 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000289 fprintf(stderr,
290 "Warning: weird character in interface"
291 " `%s' (No aliases, :, ! or *).\n",
292 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000293 break;
294 }
295 }
296 }
297}
298
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200299#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100300static void *load_extension(const char *search_path, const char *prefix,
301 const char *name, bool is_target)
302{
303 const char *dir = search_path, *next;
304 void *ptr = NULL;
305 struct stat sb;
306 char path[256];
307
308 do {
309 next = strchr(dir, ':');
310 if (next == NULL)
311 next = dir + strlen(dir);
312 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200313 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100314
315 if (dlopen(path, RTLD_NOW) != NULL) {
316 /* Found library. If it didn't register itself,
317 maybe they specified target as match. */
318 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100319 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100320 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100321 ptr = xtables_find_match(name,
322 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100323 } else if (stat(path, &sb) == 0) {
324 fprintf(stderr, "%s: %s\n", path, dlerror());
325 }
326
327 if (ptr != NULL)
328 return ptr;
329
330 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200331 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100332 if (dlopen(path, RTLD_NOW) != NULL) {
333 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100334 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100335 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100336 ptr = xtables_find_match(name,
337 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100338 } else if (stat(path, &sb) == 0) {
339 fprintf(stderr, "%s: %s\n", path, dlerror());
340 }
341
342 if (ptr != NULL)
343 return ptr;
344
345 dir = next + 1;
346 } while (*next != '\0');
347
348 return NULL;
349}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200350#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100351
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100352struct xtables_match *
353xtables_find_match(const char *name, enum xtables_tryload tryload,
354 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000355{
356 struct xtables_match *ptr;
357 const char *icmp6 = "icmp6";
358
359 /* This is ugly as hell. Nonetheless, there is no way of changing
360 * this without hurting backwards compatibility */
361 if ( (strcmp(name,"icmpv6") == 0) ||
362 (strcmp(name,"ipv6-icmp") == 0) ||
363 (strcmp(name,"icmp6") == 0) )
364 name = icmp6;
365
366 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
367 if (strcmp(name, ptr->name) == 0) {
368 struct xtables_match *clone;
369
370 /* First match of this type: */
371 if (ptr->m == NULL)
372 break;
373
374 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100375 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000376 memcpy(clone, ptr, sizeof(struct xtables_match));
377 clone->mflags = 0;
378 /* This is a clone: */
379 clone->next = clone;
380
381 ptr = clone;
382 break;
383 }
384 }
385
386#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100387 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100388 ptr = load_extension(xtables_libdir, afinfo.libprefix,
389 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000390
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100391 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000392 exit_error(PARAMETER_PROBLEM,
393 "Couldn't load match `%s':%s\n",
394 name, dlerror());
395 }
396#else
397 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100398 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000399 ptr->loaded = 1;
400 else
401 ptr = NULL;
402 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100403 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000404 exit_error(PARAMETER_PROBLEM,
405 "Couldn't find match `%s'\n", name);
406 }
407#endif
408
409 if (ptr && matches) {
410 struct xtables_rule_match **i;
411 struct xtables_rule_match *newentry;
412
Jan Engelhardt630ef482009-01-27 14:58:41 +0100413 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000414
415 for (i = matches; *i; i = &(*i)->next) {
416 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100417 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000418 }
419 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100420 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000421 newentry->next = NULL;
422 *i = newentry;
423 }
424
425 return ptr;
426}
427
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100428struct xtables_target *
429xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000430{
431 struct xtables_target *ptr;
432
433 /* Standard target? */
434 if (strcmp(name, "") == 0
435 || strcmp(name, XTC_LABEL_ACCEPT) == 0
436 || strcmp(name, XTC_LABEL_DROP) == 0
437 || strcmp(name, XTC_LABEL_QUEUE) == 0
438 || strcmp(name, XTC_LABEL_RETURN) == 0)
439 name = "standard";
440
441 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
442 if (strcmp(name, ptr->name) == 0)
443 break;
444 }
445
446#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100447 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100448 ptr = load_extension(xtables_libdir, afinfo.libprefix,
449 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000450
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100451 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000452 exit_error(PARAMETER_PROBLEM,
453 "Couldn't load target `%s':%s\n",
454 name, dlerror());
455 }
456#else
457 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100458 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000459 ptr->loaded = 1;
460 else
461 ptr = NULL;
462 }
463 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
464 exit_error(PARAMETER_PROBLEM,
465 "Couldn't find target `%s'\n", name);
466 }
467#endif
468
469 if (ptr)
470 ptr->used = 1;
471
472 return ptr;
473}
474
475static int compatible_revision(const char *name, u_int8_t revision, int opt)
476{
477 struct xt_get_revision rev;
478 socklen_t s = sizeof(rev);
479 int max_rev, sockfd;
480
481 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
482 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000483 if (errno == EPERM) {
484 /* revision 0 is always supported. */
485 if (revision != 0)
486 fprintf(stderr, "Could not determine whether "
487 "revision %u is supported, "
488 "assuming it is.\n",
489 revision);
490 return 1;
491 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000492 fprintf(stderr, "Could not open socket to kernel: %s\n",
493 strerror(errno));
494 exit(1);
495 }
496
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100497 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000498
499 strcpy(rev.name, name);
500 rev.revision = revision;
501
502 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
503 if (max_rev < 0) {
504 /* Definitely don't support this? */
505 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
506 close(sockfd);
507 return 0;
508 } else if (errno == ENOPROTOOPT) {
509 close(sockfd);
510 /* Assume only revision 0 support (old kernel) */
511 return (revision == 0);
512 } else {
513 fprintf(stderr, "getsockopt failed strangely: %s\n",
514 strerror(errno));
515 exit(1);
516 }
517 }
518 close(sockfd);
519 return 1;
520}
521
522
523static int compatible_match_revision(const char *name, u_int8_t revision)
524{
525 return compatible_revision(name, revision, afinfo.so_rev_match);
526}
527
528static int compatible_target_revision(const char *name, u_int8_t revision)
529{
530 return compatible_revision(name, revision, afinfo.so_rev_target);
531}
532
533void xtables_register_match(struct xtables_match *me)
534{
535 struct xtables_match **i, *old;
536
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100537 if (strcmp(me->version, XTABLES_VERSION) != 0) {
538 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
539 "but \"%s\" is required.\n",
540 xtables_program_name, me->name,
541 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000542 exit(1);
543 }
544
545 /* Revision field stole a char from name. */
546 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
547 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100548 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000549 exit(1);
550 }
551
552 if (me->family >= NPROTO) {
553 fprintf(stderr,
554 "%s: BUG: match %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100555 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000556 exit(1);
557 }
558
559 /* ignore not interested match */
Jan Engelhardt23545c22008-02-14 04:23:04 +0100560 if (me->family != afinfo.family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000561 return;
562
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100563 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000564 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100565 if (old->revision == me->revision &&
566 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000567 fprintf(stderr,
568 "%s: match `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100569 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000570 exit(1);
571 }
572
573 /* Now we have two (or more) options, check compatibility. */
574 if (compatible_match_revision(old->name, old->revision)
575 && old->revision > me->revision)
576 return;
577
Jan Engelhardt23545c22008-02-14 04:23:04 +0100578 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000579 if (!compatible_match_revision(me->name, me->revision))
580 return;
581
Jan Engelhardt23545c22008-02-14 04:23:04 +0100582 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
583 if (old->revision == me->revision && me->family == AF_UNSPEC)
584 return;
585
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000586 /* Delete old one. */
587 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
588 *i = old->next;
589 }
590
591 if (me->size != XT_ALIGN(me->size)) {
592 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100593 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000594 exit(1);
595 }
596
597 /* Append to list. */
598 for (i = &xtables_matches; *i; i = &(*i)->next);
599 me->next = NULL;
600 *i = me;
601
602 me->m = NULL;
603 me->mflags = 0;
604}
605
606void xtables_register_target(struct xtables_target *me)
607{
608 struct xtables_target *old;
609
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100610 if (strcmp(me->version, XTABLES_VERSION) != 0) {
611 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
612 "but \"%s\" is required.\n",
613 xtables_program_name, me->name,
614 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000615 exit(1);
616 }
617
618 /* Revision field stole a char from name. */
619 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
620 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100621 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000622 exit(1);
623 }
624
625 if (me->family >= NPROTO) {
626 fprintf(stderr,
627 "%s: BUG: target %s has invalid protocol family\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100628 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000629 exit(1);
630 }
631
632 /* ignore not interested target */
Jan Engelhardt23545c22008-02-14 04:23:04 +0100633 if (me->family != afinfo.family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000634 return;
635
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100636 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000637 if (old) {
638 struct xtables_target **i;
639
Jan Engelhardt23545c22008-02-14 04:23:04 +0100640 if (old->revision == me->revision &&
641 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000642 fprintf(stderr,
643 "%s: target `%s' already registered.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100644 xtables_program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000645 exit(1);
646 }
647
648 /* Now we have two (or more) options, check compatibility. */
649 if (compatible_target_revision(old->name, old->revision)
650 && old->revision > me->revision)
651 return;
652
Jan Engelhardt23545c22008-02-14 04:23:04 +0100653 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000654 if (!compatible_target_revision(me->name, me->revision))
655 return;
656
Jan Engelhardt23545c22008-02-14 04:23:04 +0100657 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
658 if (old->revision == me->revision && me->family == AF_UNSPEC)
659 return;
660
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000661 /* Delete old one. */
662 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
663 *i = old->next;
664 }
665
666 if (me->size != XT_ALIGN(me->size)) {
667 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100668 xtables_program_name, me->name, (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000669 exit(1);
670 }
671
672 /* Prepend to list. */
673 me->next = xtables_targets;
674 xtables_targets = me;
675 me->t = NULL;
676 me->tflags = 0;
677}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000678
679void param_act(unsigned int status, const char *p1, ...)
680{
681 const char *p2, *p3;
682 va_list args;
683 bool b;
684
685 va_start(args, p1);
686
687 switch (status) {
688 case P_ONLY_ONCE:
689 p2 = va_arg(args, const char *);
690 b = va_arg(args, unsigned int);
691 if (!b)
692 return;
693 exit_error(PARAMETER_PROBLEM,
694 "%s: \"%s\" option may only be specified once",
695 p1, p2);
696 break;
697 case P_NO_INVERT:
698 p2 = va_arg(args, const char *);
699 b = va_arg(args, unsigned int);
700 if (!b)
701 return;
702 exit_error(PARAMETER_PROBLEM,
703 "%s: \"%s\" option cannot be inverted", p1, p2);
704 break;
705 case P_BAD_VALUE:
706 p2 = va_arg(args, const char *);
707 p3 = va_arg(args, const char *);
708 exit_error(PARAMETER_PROBLEM,
709 "%s: Bad value for \"%s\" option: \"%s\"",
710 p1, p2, p3);
711 break;
712 case P_ONE_ACTION:
713 b = va_arg(args, unsigned int);
714 if (!b)
715 return;
716 exit_error(PARAMETER_PROBLEM,
717 "%s: At most one action is possible", p1);
718 break;
719 default:
720 exit_error(status, p1, args);
721 break;
722 }
723
724 va_end(args);
725}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000726
727const char *ipaddr_to_numeric(const struct in_addr *addrp)
728{
729 static char buf[20];
730 const unsigned char *bytep = (const void *)&addrp->s_addr;
731
732 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
733 return buf;
734}
735
736static const char *ipaddr_to_host(const struct in_addr *addr)
737{
738 struct hostent *host;
739
740 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
741 if (host == NULL)
742 return NULL;
743
744 return host->h_name;
745}
746
747static const char *ipaddr_to_network(const struct in_addr *addr)
748{
749 struct netent *net;
750
751 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
752 return net->n_name;
753
754 return NULL;
755}
756
757const char *ipaddr_to_anyname(const struct in_addr *addr)
758{
759 const char *name;
760
761 if ((name = ipaddr_to_host(addr)) != NULL ||
762 (name = ipaddr_to_network(addr)) != NULL)
763 return name;
764
765 return ipaddr_to_numeric(addr);
766}
767
768const char *ipmask_to_numeric(const struct in_addr *mask)
769{
770 static char buf[20];
771 uint32_t maskaddr, bits;
772 int i;
773
774 maskaddr = ntohl(mask->s_addr);
775
776 if (maskaddr == 0xFFFFFFFFL)
777 /* we don't want to see "/32" */
778 return "";
779
780 i = 32;
781 bits = 0xFFFFFFFEL;
782 while (--i >= 0 && maskaddr != bits)
783 bits <<= 1;
784 if (i >= 0)
785 sprintf(buf, "/%d", i);
786 else
787 /* mask was not a decent combination of 1's and 0's */
788 sprintf(buf, "/%s", ipaddr_to_numeric(mask));
789
790 return buf;
791}
792
Jan Engelhardtbd943842008-01-20 13:38:08 +0000793static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
794{
795 static struct in_addr addr;
796 unsigned char *addrp;
797 unsigned int onebyte;
798 char buf[20], *p, *q;
799 int i;
800
801 /* copy dotted string, because we need to modify it */
802 strncpy(buf, dotted, sizeof(buf) - 1);
803 buf[sizeof(buf) - 1] = '\0';
804 addrp = (void *)&addr.s_addr;
805
806 p = buf;
807 for (i = 0; i < 3; ++i) {
808 if ((q = strchr(p, '.')) == NULL) {
809 if (is_mask)
810 return NULL;
811
812 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100813 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000814 return NULL;
815
816 addrp[i] = onebyte;
817 while (i < 3)
818 addrp[++i] = 0;
819
820 return &addr;
821 }
822
823 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100824 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000825 return NULL;
826
827 addrp[i] = onebyte;
828 p = q + 1;
829 }
830
831 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100832 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000833 return NULL;
834
835 addrp[3] = onebyte;
836 return &addr;
837}
838
839struct in_addr *numeric_to_ipaddr(const char *dotted)
840{
841 return __numeric_to_ipaddr(dotted, false);
842}
843
844struct in_addr *numeric_to_ipmask(const char *dotted)
845{
846 return __numeric_to_ipaddr(dotted, true);
847}
848
849static struct in_addr *network_to_ipaddr(const char *name)
850{
851 static struct in_addr addr;
852 struct netent *net;
853
854 if ((net = getnetbyname(name)) != NULL) {
855 if (net->n_addrtype != AF_INET)
856 return NULL;
857 addr.s_addr = htonl(net->n_net);
858 return &addr;
859 }
860
861 return NULL;
862}
863
864static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
865{
866 struct hostent *host;
867 struct in_addr *addr;
868 unsigned int i;
869
870 *naddr = 0;
871 if ((host = gethostbyname(name)) != NULL) {
872 if (host->h_addrtype != AF_INET ||
873 host->h_length != sizeof(struct in_addr))
874 return NULL;
875
876 while (host->h_addr_list[*naddr] != NULL)
877 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100878 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000879 for (i = 0; i < *naddr; i++)
880 memcpy(&addr[i], host->h_addr_list[i],
881 sizeof(struct in_addr));
882 return addr;
883 }
884
885 return NULL;
886}
887
888static struct in_addr *
889ipparse_hostnetwork(const char *name, unsigned int *naddrs)
890{
891 struct in_addr *addrptmp, *addrp;
892
893 if ((addrptmp = numeric_to_ipaddr(name)) != NULL ||
894 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +0100895 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +0000896 memcpy(addrp, addrptmp, sizeof(*addrp));
897 *naddrs = 1;
898 return addrp;
899 }
900 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
901 return addrptmp;
902
903 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
904}
905
906static struct in_addr *parse_ipmask(const char *mask)
907{
908 static struct in_addr maskaddr;
909 struct in_addr *addrp;
910 unsigned int bits;
911
912 if (mask == NULL) {
913 /* no mask at all defaults to 32 bits */
914 maskaddr.s_addr = 0xFFFFFFFF;
915 return &maskaddr;
916 }
917 if ((addrp = numeric_to_ipmask(mask)) != NULL)
918 /* dotted_to_addr already returns a network byte order addr */
919 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100920 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000921 exit_error(PARAMETER_PROBLEM,
922 "invalid mask `%s' specified", mask);
923 if (bits != 0) {
924 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
925 return &maskaddr;
926 }
927
928 maskaddr.s_addr = 0U;
929 return &maskaddr;
930}
931
932void ipparse_hostnetworkmask(const char *name, struct in_addr **addrpp,
933 struct in_addr *maskp, unsigned int *naddrs)
934{
935 unsigned int i, j, k, n;
936 struct in_addr *addrp;
937 char buf[256], *p;
938
939 strncpy(buf, name, sizeof(buf) - 1);
940 buf[sizeof(buf) - 1] = '\0';
941 if ((p = strrchr(buf, '/')) != NULL) {
942 *p = '\0';
943 addrp = parse_ipmask(p + 1);
944 } else {
945 addrp = parse_ipmask(NULL);
946 }
947 memcpy(maskp, addrp, sizeof(*maskp));
948
949 /* if a null mask is given, the name is ignored, like in "any/0" */
950 if (maskp->s_addr == 0U)
951 strcpy(buf, "0.0.0.0");
952
953 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
954 n = *naddrs;
955 for (i = 0, j = 0; i < n; ++i) {
956 addrp[j++].s_addr &= maskp->s_addr;
957 for (k = 0; k < j - 1; ++k)
958 if (addrp[k].s_addr == addrp[j-1].s_addr) {
959 --*naddrs;
960 --j;
961 break;
962 }
963 }
964}
965
Jan Engelhardt08b16162008-01-20 13:36:08 +0000966const char *ip6addr_to_numeric(const struct in6_addr *addrp)
967{
968 /* 0000:0000:0000:0000:0000:000.000.000.000
969 * 0000:0000:0000:0000:0000:0000:0000:0000 */
970 static char buf[50+1];
971 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
972}
973
974static const char *ip6addr_to_host(const struct in6_addr *addr)
975{
976 static char hostname[NI_MAXHOST];
977 struct sockaddr_in6 saddr;
978 int err;
979
980 memset(&saddr, 0, sizeof(struct sockaddr_in6));
981 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
982 saddr.sin6_family = AF_INET6;
983
984 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
985 hostname, sizeof(hostname) - 1, NULL, 0, 0);
986 if (err != 0) {
987#ifdef DEBUG
988 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
989#endif
990 return NULL;
991 }
992
993#ifdef DEBUG
994 fprintf (stderr, "\naddr2host: %s\n", hostname);
995#endif
996 return hostname;
997}
998
999const char *ip6addr_to_anyname(const struct in6_addr *addr)
1000{
1001 const char *name;
1002
1003 if ((name = ip6addr_to_host(addr)) != NULL)
1004 return name;
1005
1006 return ip6addr_to_numeric(addr);
1007}
1008
1009static int ip6addr_prefix_length(const struct in6_addr *k)
1010{
1011 unsigned int bits = 0;
1012 uint32_t a, b, c, d;
1013
Jan Engelhardt48607812008-06-10 15:17:53 +02001014 a = ntohl(k->s6_addr32[0]);
1015 b = ntohl(k->s6_addr32[1]);
1016 c = ntohl(k->s6_addr32[2]);
1017 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001018 while (a & 0x80000000U) {
1019 ++bits;
1020 a <<= 1;
1021 a |= (b >> 31) & 1;
1022 b <<= 1;
1023 b |= (c >> 31) & 1;
1024 c <<= 1;
1025 c |= (d >> 31) & 1;
1026 d <<= 1;
1027 }
1028 if (a != 0 || b != 0 || c != 0 || d != 0)
1029 return -1;
1030 return bits;
1031}
1032
1033const char *ip6mask_to_numeric(const struct in6_addr *addrp)
1034{
1035 static char buf[50+2];
1036 int l = ip6addr_prefix_length(addrp);
1037
1038 if (l == -1) {
1039 strcpy(buf, "/");
1040 strcat(buf, ip6addr_to_numeric(addrp));
1041 return buf;
1042 }
1043 sprintf(buf, "/%d", l);
1044 return buf;
1045}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001046
1047struct in6_addr *numeric_to_ip6addr(const char *num)
1048{
1049 static struct in6_addr ap;
1050 int err;
1051
1052 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1053 return &ap;
1054#ifdef DEBUG
1055 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1056#endif
1057 return NULL;
1058}
1059
1060static struct in6_addr *
1061host_to_ip6addr(const char *name, unsigned int *naddr)
1062{
1063 static struct in6_addr *addr;
1064 struct addrinfo hints;
1065 struct addrinfo *res;
1066 int err;
1067
1068 memset(&hints, 0, sizeof(hints));
1069 hints.ai_flags = AI_CANONNAME;
1070 hints.ai_family = AF_INET6;
1071 hints.ai_socktype = SOCK_RAW;
1072 hints.ai_protocol = IPPROTO_IPV6;
1073 hints.ai_next = NULL;
1074
1075 *naddr = 0;
1076 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1077#ifdef DEBUG
1078 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1079#endif
1080 return NULL;
1081 } else {
1082 if (res->ai_family != AF_INET6 ||
1083 res->ai_addrlen != sizeof(struct sockaddr_in6))
1084 return NULL;
1085
1086#ifdef DEBUG
1087 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1088 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1089#endif
1090 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001091 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001092 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1093 sizeof(struct in6_addr));
1094 freeaddrinfo(res);
1095 *naddr = 1;
1096 return addr;
1097 }
1098
1099 return NULL;
1100}
1101
1102static struct in6_addr *network_to_ip6addr(const char *name)
1103{
1104 /* abort();*/
1105 /* TODO: not implemented yet, but the exception breaks the
1106 * name resolvation */
1107 return NULL;
1108}
1109
1110static struct in6_addr *
1111ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1112{
1113 struct in6_addr *addrp, *addrptmp;
1114
1115 if ((addrptmp = numeric_to_ip6addr(name)) != NULL ||
1116 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001117 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001118 memcpy(addrp, addrptmp, sizeof(*addrp));
1119 *naddrs = 1;
1120 return addrp;
1121 }
1122 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1123 return addrp;
1124
1125 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1126}
1127
1128static struct in6_addr *parse_ip6mask(char *mask)
1129{
1130 static struct in6_addr maskaddr;
1131 struct in6_addr *addrp;
1132 unsigned int bits;
1133
1134 if (mask == NULL) {
1135 /* no mask at all defaults to 128 bits */
1136 memset(&maskaddr, 0xff, sizeof maskaddr);
1137 return &maskaddr;
1138 }
1139 if ((addrp = numeric_to_ip6addr(mask)) != NULL)
1140 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001141 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001142 exit_error(PARAMETER_PROBLEM,
1143 "invalid mask `%s' specified", mask);
1144 if (bits != 0) {
1145 char *p = (void *)&maskaddr;
1146 memset(p, 0xff, bits / 8);
1147 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1148 p[bits/8] = 0xff << (8 - (bits & 7));
1149 return &maskaddr;
1150 }
1151
1152 memset(&maskaddr, 0, sizeof(maskaddr));
1153 return &maskaddr;
1154}
1155
1156void ip6parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
1157 struct in6_addr *maskp, unsigned int *naddrs)
1158{
1159 struct in6_addr *addrp;
1160 unsigned int i, j, k, n;
1161 char buf[256], *p;
1162
1163 strncpy(buf, name, sizeof(buf) - 1);
1164 buf[sizeof(buf)-1] = '\0';
1165 if ((p = strrchr(buf, '/')) != NULL) {
1166 *p = '\0';
1167 addrp = parse_ip6mask(p + 1);
1168 } else {
1169 addrp = parse_ip6mask(NULL);
1170 }
1171 memcpy(maskp, addrp, sizeof(*maskp));
1172
1173 /* if a null mask is given, the name is ignored, like in "any/0" */
1174 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1175 strcpy(buf, "::");
1176
1177 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1178 n = *naddrs;
1179 for (i = 0, j = 0; i < n; ++i) {
1180 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001181 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001182 ++j;
1183 for (k = 0; k < j - 1; ++k)
1184 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1185 --*naddrs;
1186 --j;
1187 break;
1188 }
1189 }
1190}
Max Kellermanna5d09942008-01-29 13:44:34 +00001191
1192void save_string(const char *value)
1193{
1194 static const char no_quote_chars[] = "_-0123456789"
1195 "abcdefghijklmnopqrstuvwxyz"
1196 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1197 static const char escape_chars[] = "\"\\'";
1198 size_t length;
1199 const char *p;
1200
1201 length = strcspn(value, no_quote_chars);
1202 if (length > 0 && value[length] == 0) {
1203 /* no quoting required */
1204 fputs(value, stdout);
1205 putchar(' ');
1206 } else {
1207 /* there is at least one dangerous character in the
1208 value, which we have to quote. Write double quotes
1209 around the value and escape special characters with
1210 a backslash */
1211 putchar('"');
1212
1213 for (p = strpbrk(value, escape_chars); p != NULL;
1214 p = strpbrk(value, escape_chars)) {
1215 if (p > value)
1216 fwrite(value, 1, p - value, stdout);
1217 putchar('\\');
1218 putchar(*p);
1219 value = p + 1;
1220 }
1221
1222 /* print the rest and finish the double quoted
1223 string */
1224 fputs(value, stdout);
1225 printf("\" ");
1226 }
1227}