blob: 703a6ceeb351d9f833d9f0bb5b057d4677351657 [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>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000035
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000036#ifndef NO_SHARED_LIBS
37#include <dlfcn.h>
38#endif
39
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000040#define NPROTO 255
41
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000042#ifndef PROC_SYS_MODPROBE
43#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
44#endif
45
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000046char *lib_dir;
47
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000048/* the path to command to load kernel module */
49const char *modprobe = NULL;
50
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000051/* Keeping track of external matches and targets: linked lists. */
52struct xtables_match *xtables_matches;
53struct xtables_target *xtables_targets;
54
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000055void *fw_calloc(size_t count, size_t size)
56{
57 void *p;
58
59 if ((p = calloc(count, size)) == NULL) {
60 perror("ip[6]tables: calloc failed");
61 exit(1);
62 }
63
64 return p;
65}
66
67void *fw_malloc(size_t size)
68{
69 void *p;
70
71 if ((p = malloc(size)) == NULL) {
72 perror("ip[6]tables: malloc failed");
73 exit(1);
74 }
75
76 return p;
77}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000078
79static char *get_modprobe(void)
80{
81 int procfile;
82 char *ret;
83
84#define PROCFILE_BUFSIZ 1024
85 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
86 if (procfile < 0)
87 return NULL;
88
89 ret = (char *) malloc(PROCFILE_BUFSIZ);
90 if (ret) {
91 memset(ret, 0, PROCFILE_BUFSIZ);
92 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
93 case -1: goto fail;
94 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
95 }
96 if (ret[strlen(ret)-1]=='\n')
97 ret[strlen(ret)-1]=0;
98 close(procfile);
99 return ret;
100 }
101 fail:
102 free(ret);
103 close(procfile);
104 return NULL;
105}
106
107int xtables_insmod(const char *modname, const char *modprobe, int quiet)
108{
109 char *buf = NULL;
110 char *argv[4];
111 int status;
112
113 /* If they don't explicitly set it, read out of kernel */
114 if (!modprobe) {
115 buf = get_modprobe();
116 if (!buf)
117 return -1;
118 modprobe = buf;
119 }
120
121 switch (fork()) {
122 case 0:
123 argv[0] = (char *)modprobe;
124 argv[1] = (char *)modname;
125 if (quiet) {
126 argv[2] = "-q";
127 argv[3] = NULL;
128 } else {
129 argv[2] = NULL;
130 argv[3] = NULL;
131 }
132 execv(argv[0], argv);
133
134 /* not usually reached */
135 exit(1);
136 case -1:
137 return -1;
138
139 default: /* parent */
140 wait(&status);
141 }
142
143 free(buf);
144 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
145 return 0;
146 return -1;
147}
148
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000149int load_xtables_ko(const char *modprobe, int quiet)
150{
151 static int loaded = 0;
152 static int ret = -1;
153
154 if (!loaded) {
155 ret = xtables_insmod(afinfo.kmod, modprobe, quiet);
156 loaded = (ret == 0);
157 }
158
159 return ret;
160}
161
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000162int string_to_number_ll(const char *s, unsigned long long min,
163 unsigned long long max, unsigned long long *ret)
164{
165 unsigned long long number;
166 char *end;
167
168 /* Handle hex, octal, etc. */
169 errno = 0;
170 number = strtoull(s, &end, 0);
171 if (*end == '\0' && end != s) {
172 /* we parsed a number, let's see if we want this */
173 if (errno != ERANGE && min <= number && (!max || number <= max)) {
174 *ret = number;
175 return 0;
176 }
177 }
178 return -1;
179}
180
181int string_to_number_l(const char *s, unsigned long min, unsigned long max,
182 unsigned long *ret)
183{
184 int result;
185 unsigned long long number;
186
187 result = string_to_number_ll(s, min, max, &number);
188 *ret = (unsigned long)number;
189
190 return result;
191}
192
193int string_to_number(const char *s, unsigned int min, unsigned int max,
194 unsigned int *ret)
195{
196 int result;
197 unsigned long number;
198
199 result = string_to_number_l(s, min, max, &number);
200 *ret = (unsigned int)number;
201
202 return result;
203}
204
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000205/*
206 * strtonum{,l} - string to number conversion
207 *
208 * If @end is NULL, we assume the caller does not want
209 * a case like "15a", so reject it.
210 */
211bool strtonuml(const char *s, char **end, unsigned long *value,
212 unsigned long min, unsigned long max)
213{
214 unsigned long v;
215 char *my_end;
216
217 errno = 0;
218 v = strtoul(s, &my_end, 0);
219
220 if (my_end == s)
221 return false;
222 if (end != NULL)
223 *end = my_end;
224
225 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
226 if (value != NULL)
227 *value = v;
228 if (end == NULL)
229 return *my_end == '\0';
230 return true;
231 }
232
233 return false;
234}
235
236bool strtonum(const char *s, char **end, unsigned int *value,
237 unsigned int min, unsigned int max)
238{
239 unsigned long v;
240 bool ret;
241
242 ret = strtonuml(s, end, &v, min, max);
243 if (value != NULL)
244 *value = v;
245 return ret;
246}
247
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000248int service_to_port(const char *name, const char *proto)
249{
250 struct servent *service;
251
252 if ((service = getservbyname(name, proto)) != NULL)
253 return ntohs((unsigned short) service->s_port);
254
255 return -1;
256}
257
258u_int16_t parse_port(const char *port, const char *proto)
259{
260 unsigned int portnum;
261
262 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
263 (portnum = service_to_port(port, proto)) != -1)
264 return (u_int16_t)portnum;
265
266 exit_error(PARAMETER_PROBLEM,
267 "invalid port/service `%s' specified", port);
268}
269
270void parse_interface(const char *arg, char *vianame, unsigned char *mask)
271{
272 int vialen = strlen(arg);
273 unsigned int i;
274
275 memset(mask, 0, IFNAMSIZ);
276 memset(vianame, 0, IFNAMSIZ);
277
278 if (vialen + 1 > IFNAMSIZ)
279 exit_error(PARAMETER_PROBLEM,
280 "interface name `%s' must be shorter than IFNAMSIZ"
281 " (%i)", arg, IFNAMSIZ-1);
282
283 strcpy(vianame, arg);
284 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
285 memset(mask, 0, IFNAMSIZ);
286 else if (vianame[vialen - 1] == '+') {
287 memset(mask, 0xFF, vialen - 1);
288 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
289 /* Don't remove `+' here! -HW */
290 } else {
291 /* Include nul-terminator in match */
292 memset(mask, 0xFF, vialen + 1);
293 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
294 for (i = 0; vianame[i]; i++) {
295 if (vianame[i] == ':' ||
296 vianame[i] == '!' ||
297 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000298 fprintf(stderr,
299 "Warning: weird character in interface"
300 " `%s' (No aliases, :, ! or *).\n",
301 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000302 break;
303 }
304 }
305 }
306}
307
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000308struct xtables_match *find_match(const char *name, enum xt_tryload tryload,
309 struct xtables_rule_match **matches)
310{
Jan Engelhardt23976ac2008-01-20 13:32:50 +0000311#ifndef NO_SHARED_LIBS
312 struct stat sb;
313#endif
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000314 struct xtables_match *ptr;
315 const char *icmp6 = "icmp6";
316
317 /* This is ugly as hell. Nonetheless, there is no way of changing
318 * this without hurting backwards compatibility */
319 if ( (strcmp(name,"icmpv6") == 0) ||
320 (strcmp(name,"ipv6-icmp") == 0) ||
321 (strcmp(name,"icmp6") == 0) )
322 name = icmp6;
323
324 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
325 if (strcmp(name, ptr->name) == 0) {
326 struct xtables_match *clone;
327
328 /* First match of this type: */
329 if (ptr->m == NULL)
330 break;
331
332 /* Second and subsequent clones */
333 clone = fw_malloc(sizeof(struct xtables_match));
334 memcpy(clone, ptr, sizeof(struct xtables_match));
335 clone->mflags = 0;
336 /* This is a clone: */
337 clone->next = clone;
338
339 ptr = clone;
340 break;
341 }
342 }
343
344#ifndef NO_SHARED_LIBS
345 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
346 char path[strlen(lib_dir) + sizeof("/.so")
347 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000348
349 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
350 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000351 /* Found library. If it didn't register itself,
352 maybe they specified target as match. */
353 ptr = find_match(name, DONT_LOAD, NULL);
Jan Engelhardt23976ac2008-01-20 13:32:50 +0000354 else if (stat(path, &sb) == 0)
355 fprintf(stderr, "%s: %s\n", path, dlerror());
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000356
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000357 if (ptr == NULL) {
358 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
359 name);
360 if (dlopen(path, RTLD_NOW) != NULL)
361 ptr = find_match(name, DONT_LOAD, NULL);
Jan Engelhardt23976ac2008-01-20 13:32:50 +0000362 else if (stat(path, &sb) == 0)
363 fprintf(stderr, "%s: %s\n", path, dlerror());
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000364 }
365
366 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000367 exit_error(PARAMETER_PROBLEM,
368 "Couldn't load match `%s':%s\n",
369 name, dlerror());
370 }
371#else
372 if (ptr && !ptr->loaded) {
373 if (tryload != DONT_LOAD)
374 ptr->loaded = 1;
375 else
376 ptr = NULL;
377 }
378 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
379 exit_error(PARAMETER_PROBLEM,
380 "Couldn't find match `%s'\n", name);
381 }
382#endif
383
384 if (ptr && matches) {
385 struct xtables_rule_match **i;
386 struct xtables_rule_match *newentry;
387
388 newentry = fw_malloc(sizeof(struct xtables_rule_match));
389
390 for (i = matches; *i; i = &(*i)->next) {
391 if (strcmp(name, (*i)->match->name) == 0)
392 (*i)->completed = 1;
393 }
394 newentry->match = ptr;
395 newentry->completed = 0;
396 newentry->next = NULL;
397 *i = newentry;
398 }
399
400 return ptr;
401}
402
403
404struct xtables_target *find_target(const char *name, enum xt_tryload tryload)
405{
Jan Engelhardt23976ac2008-01-20 13:32:50 +0000406#ifndef NO_SHARED_LIBS
407 struct stat sb;
408#endif
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000409 struct xtables_target *ptr;
410
411 /* Standard target? */
412 if (strcmp(name, "") == 0
413 || strcmp(name, XTC_LABEL_ACCEPT) == 0
414 || strcmp(name, XTC_LABEL_DROP) == 0
415 || strcmp(name, XTC_LABEL_QUEUE) == 0
416 || strcmp(name, XTC_LABEL_RETURN) == 0)
417 name = "standard";
418
419 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
420 if (strcmp(name, ptr->name) == 0)
421 break;
422 }
423
424#ifndef NO_SHARED_LIBS
425 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
426 char path[strlen(lib_dir) + sizeof("/.so")
427 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000428
429 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
430 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000431 /* Found library. If it didn't register itself,
432 maybe they specified match as a target. */
433 ptr = find_target(name, DONT_LOAD);
Jan Engelhardt23976ac2008-01-20 13:32:50 +0000434 else if (stat(path, &sb) == 0)
435 fprintf(stderr, "%s: %s\n", path, dlerror());
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000436
437 if (ptr == NULL) {
438 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
439 name);
440 if (dlopen(path, RTLD_NOW) != NULL)
441 ptr = find_target(name, DONT_LOAD);
Jan Engelhardt23976ac2008-01-20 13:32:50 +0000442 else if (stat(path, &sb) == 0)
443 fprintf(stderr, "%s: %s\n", path, dlerror());
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000444 }
445 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000446 exit_error(PARAMETER_PROBLEM,
447 "Couldn't load target `%s':%s\n",
448 name, dlerror());
449 }
450#else
451 if (ptr && !ptr->loaded) {
452 if (tryload != DONT_LOAD)
453 ptr->loaded = 1;
454 else
455 ptr = NULL;
456 }
457 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
458 exit_error(PARAMETER_PROBLEM,
459 "Couldn't find target `%s'\n", name);
460 }
461#endif
462
463 if (ptr)
464 ptr->used = 1;
465
466 return ptr;
467}
468
469static int compatible_revision(const char *name, u_int8_t revision, int opt)
470{
471 struct xt_get_revision rev;
472 socklen_t s = sizeof(rev);
473 int max_rev, sockfd;
474
475 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
476 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000477 if (errno == EPERM) {
478 /* revision 0 is always supported. */
479 if (revision != 0)
480 fprintf(stderr, "Could not determine whether "
481 "revision %u is supported, "
482 "assuming it is.\n",
483 revision);
484 return 1;
485 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000486 fprintf(stderr, "Could not open socket to kernel: %s\n",
487 strerror(errno));
488 exit(1);
489 }
490
491 load_xtables_ko(modprobe, 1);
492
493 strcpy(rev.name, name);
494 rev.revision = revision;
495
496 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
497 if (max_rev < 0) {
498 /* Definitely don't support this? */
499 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
500 close(sockfd);
501 return 0;
502 } else if (errno == ENOPROTOOPT) {
503 close(sockfd);
504 /* Assume only revision 0 support (old kernel) */
505 return (revision == 0);
506 } else {
507 fprintf(stderr, "getsockopt failed strangely: %s\n",
508 strerror(errno));
509 exit(1);
510 }
511 }
512 close(sockfd);
513 return 1;
514}
515
516
517static int compatible_match_revision(const char *name, u_int8_t revision)
518{
519 return compatible_revision(name, revision, afinfo.so_rev_match);
520}
521
522static int compatible_target_revision(const char *name, u_int8_t revision)
523{
524 return compatible_revision(name, revision, afinfo.so_rev_target);
525}
526
527void xtables_register_match(struct xtables_match *me)
528{
529 struct xtables_match **i, *old;
530
531 if (strcmp(me->version, program_version) != 0) {
532 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
533 program_name, me->name, me->version, program_version);
534 exit(1);
535 }
536
537 /* Revision field stole a char from name. */
538 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
539 fprintf(stderr, "%s: target `%s' has invalid name\n",
540 program_name, me->name);
541 exit(1);
542 }
543
544 if (me->family >= NPROTO) {
545 fprintf(stderr,
546 "%s: BUG: match %s has invalid protocol family\n",
547 program_name, me->name);
548 exit(1);
549 }
550
551 /* ignore not interested match */
552 if (me->family != afinfo.family)
553 return;
554
555 old = find_match(me->name, DURING_LOAD, NULL);
556 if (old) {
557 if (old->revision == me->revision) {
558 fprintf(stderr,
559 "%s: match `%s' already registered.\n",
560 program_name, me->name);
561 exit(1);
562 }
563
564 /* Now we have two (or more) options, check compatibility. */
565 if (compatible_match_revision(old->name, old->revision)
566 && old->revision > me->revision)
567 return;
568
569 /* Replace if compatible. */
570 if (!compatible_match_revision(me->name, me->revision))
571 return;
572
573 /* Delete old one. */
574 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
575 *i = old->next;
576 }
577
578 if (me->size != XT_ALIGN(me->size)) {
579 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
580 program_name, me->name, (unsigned int)me->size);
581 exit(1);
582 }
583
584 /* Append to list. */
585 for (i = &xtables_matches; *i; i = &(*i)->next);
586 me->next = NULL;
587 *i = me;
588
589 me->m = NULL;
590 me->mflags = 0;
591}
592
593void xtables_register_target(struct xtables_target *me)
594{
595 struct xtables_target *old;
596
597 if (strcmp(me->version, program_version) != 0) {
598 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
599 program_name, me->name, me->version, program_version);
600 exit(1);
601 }
602
603 /* Revision field stole a char from name. */
604 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
605 fprintf(stderr, "%s: target `%s' has invalid name\n",
606 program_name, me->name);
607 exit(1);
608 }
609
610 if (me->family >= NPROTO) {
611 fprintf(stderr,
612 "%s: BUG: target %s has invalid protocol family\n",
613 program_name, me->name);
614 exit(1);
615 }
616
617 /* ignore not interested target */
618 if (me->family != afinfo.family)
619 return;
620
621 old = find_target(me->name, DURING_LOAD);
622 if (old) {
623 struct xtables_target **i;
624
625 if (old->revision == me->revision) {
626 fprintf(stderr,
627 "%s: target `%s' already registered.\n",
628 program_name, me->name);
629 exit(1);
630 }
631
632 /* Now we have two (or more) options, check compatibility. */
633 if (compatible_target_revision(old->name, old->revision)
634 && old->revision > me->revision)
635 return;
636
637 /* Replace if compatible. */
638 if (!compatible_target_revision(me->name, me->revision))
639 return;
640
641 /* Delete old one. */
642 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
643 *i = old->next;
644 }
645
646 if (me->size != XT_ALIGN(me->size)) {
647 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
648 program_name, me->name, (unsigned int)me->size);
649 exit(1);
650 }
651
652 /* Prepend to list. */
653 me->next = xtables_targets;
654 xtables_targets = me;
655 me->t = NULL;
656 me->tflags = 0;
657}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000658
659void param_act(unsigned int status, const char *p1, ...)
660{
661 const char *p2, *p3;
662 va_list args;
663 bool b;
664
665 va_start(args, p1);
666
667 switch (status) {
668 case P_ONLY_ONCE:
669 p2 = va_arg(args, const char *);
670 b = va_arg(args, unsigned int);
671 if (!b)
672 return;
673 exit_error(PARAMETER_PROBLEM,
674 "%s: \"%s\" option may only be specified once",
675 p1, p2);
676 break;
677 case P_NO_INVERT:
678 p2 = va_arg(args, const char *);
679 b = va_arg(args, unsigned int);
680 if (!b)
681 return;
682 exit_error(PARAMETER_PROBLEM,
683 "%s: \"%s\" option cannot be inverted", p1, p2);
684 break;
685 case P_BAD_VALUE:
686 p2 = va_arg(args, const char *);
687 p3 = va_arg(args, const char *);
688 exit_error(PARAMETER_PROBLEM,
689 "%s: Bad value for \"%s\" option: \"%s\"",
690 p1, p2, p3);
691 break;
692 case P_ONE_ACTION:
693 b = va_arg(args, unsigned int);
694 if (!b)
695 return;
696 exit_error(PARAMETER_PROBLEM,
697 "%s: At most one action is possible", p1);
698 break;
699 default:
700 exit_error(status, p1, args);
701 break;
702 }
703
704 va_end(args);
705}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000706
707const char *ipaddr_to_numeric(const struct in_addr *addrp)
708{
709 static char buf[20];
710 const unsigned char *bytep = (const void *)&addrp->s_addr;
711
712 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
713 return buf;
714}
715
716static const char *ipaddr_to_host(const struct in_addr *addr)
717{
718 struct hostent *host;
719
720 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
721 if (host == NULL)
722 return NULL;
723
724 return host->h_name;
725}
726
727static const char *ipaddr_to_network(const struct in_addr *addr)
728{
729 struct netent *net;
730
731 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
732 return net->n_name;
733
734 return NULL;
735}
736
737const char *ipaddr_to_anyname(const struct in_addr *addr)
738{
739 const char *name;
740
741 if ((name = ipaddr_to_host(addr)) != NULL ||
742 (name = ipaddr_to_network(addr)) != NULL)
743 return name;
744
745 return ipaddr_to_numeric(addr);
746}
747
748const char *ipmask_to_numeric(const struct in_addr *mask)
749{
750 static char buf[20];
751 uint32_t maskaddr, bits;
752 int i;
753
754 maskaddr = ntohl(mask->s_addr);
755
756 if (maskaddr == 0xFFFFFFFFL)
757 /* we don't want to see "/32" */
758 return "";
759
760 i = 32;
761 bits = 0xFFFFFFFEL;
762 while (--i >= 0 && maskaddr != bits)
763 bits <<= 1;
764 if (i >= 0)
765 sprintf(buf, "/%d", i);
766 else
767 /* mask was not a decent combination of 1's and 0's */
768 sprintf(buf, "/%s", ipaddr_to_numeric(mask));
769
770 return buf;
771}
772
773const char *ip6addr_to_numeric(const struct in6_addr *addrp)
774{
775 /* 0000:0000:0000:0000:0000:000.000.000.000
776 * 0000:0000:0000:0000:0000:0000:0000:0000 */
777 static char buf[50+1];
778 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
779}
780
781static const char *ip6addr_to_host(const struct in6_addr *addr)
782{
783 static char hostname[NI_MAXHOST];
784 struct sockaddr_in6 saddr;
785 int err;
786
787 memset(&saddr, 0, sizeof(struct sockaddr_in6));
788 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
789 saddr.sin6_family = AF_INET6;
790
791 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
792 hostname, sizeof(hostname) - 1, NULL, 0, 0);
793 if (err != 0) {
794#ifdef DEBUG
795 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
796#endif
797 return NULL;
798 }
799
800#ifdef DEBUG
801 fprintf (stderr, "\naddr2host: %s\n", hostname);
802#endif
803 return hostname;
804}
805
806const char *ip6addr_to_anyname(const struct in6_addr *addr)
807{
808 const char *name;
809
810 if ((name = ip6addr_to_host(addr)) != NULL)
811 return name;
812
813 return ip6addr_to_numeric(addr);
814}
815
816static int ip6addr_prefix_length(const struct in6_addr *k)
817{
818 unsigned int bits = 0;
819 uint32_t a, b, c, d;
820
821 a = k->s6_addr32[0];
822 b = k->s6_addr32[1];
823 c = k->s6_addr32[2];
824 d = k->s6_addr32[3];
825 while (a & 0x80000000U) {
826 ++bits;
827 a <<= 1;
828 a |= (b >> 31) & 1;
829 b <<= 1;
830 b |= (c >> 31) & 1;
831 c <<= 1;
832 c |= (d >> 31) & 1;
833 d <<= 1;
834 }
835 if (a != 0 || b != 0 || c != 0 || d != 0)
836 return -1;
837 return bits;
838}
839
840const char *ip6mask_to_numeric(const struct in6_addr *addrp)
841{
842 static char buf[50+2];
843 int l = ip6addr_prefix_length(addrp);
844
845 if (l == -1) {
846 strcpy(buf, "/");
847 strcat(buf, ip6addr_to_numeric(addrp));
848 return buf;
849 }
850 sprintf(buf, "/%d", l);
851 return buf;
852}