blob: 0a2fdb1df03f6f4be7754e4f2e33279fdab3534d [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>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000032
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000033#include <xtables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000034
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000035#ifndef NO_SHARED_LIBS
36#include <dlfcn.h>
37#endif
38
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000039#define NPROTO 255
40
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000041#ifndef PROC_SYS_MODPROBE
42#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
43#endif
44
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000045char *lib_dir;
46
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000047/* the path to command to load kernel module */
48const char *modprobe = NULL;
49
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000050/* Keeping track of external matches and targets: linked lists. */
51struct xtables_match *xtables_matches;
52struct xtables_target *xtables_targets;
53
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000054void *fw_calloc(size_t count, size_t size)
55{
56 void *p;
57
58 if ((p = calloc(count, size)) == NULL) {
59 perror("ip[6]tables: calloc failed");
60 exit(1);
61 }
62
63 return p;
64}
65
66void *fw_malloc(size_t size)
67{
68 void *p;
69
70 if ((p = malloc(size)) == NULL) {
71 perror("ip[6]tables: malloc failed");
72 exit(1);
73 }
74
75 return p;
76}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000077
78static char *get_modprobe(void)
79{
80 int procfile;
81 char *ret;
82
83#define PROCFILE_BUFSIZ 1024
84 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
85 if (procfile < 0)
86 return NULL;
87
88 ret = (char *) malloc(PROCFILE_BUFSIZ);
89 if (ret) {
90 memset(ret, 0, PROCFILE_BUFSIZ);
91 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
92 case -1: goto fail;
93 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
94 }
95 if (ret[strlen(ret)-1]=='\n')
96 ret[strlen(ret)-1]=0;
97 close(procfile);
98 return ret;
99 }
100 fail:
101 free(ret);
102 close(procfile);
103 return NULL;
104}
105
106int xtables_insmod(const char *modname, const char *modprobe, int quiet)
107{
108 char *buf = NULL;
109 char *argv[4];
110 int status;
111
112 /* If they don't explicitly set it, read out of kernel */
113 if (!modprobe) {
114 buf = get_modprobe();
115 if (!buf)
116 return -1;
117 modprobe = buf;
118 }
119
120 switch (fork()) {
121 case 0:
122 argv[0] = (char *)modprobe;
123 argv[1] = (char *)modname;
124 if (quiet) {
125 argv[2] = "-q";
126 argv[3] = NULL;
127 } else {
128 argv[2] = NULL;
129 argv[3] = NULL;
130 }
131 execv(argv[0], argv);
132
133 /* not usually reached */
134 exit(1);
135 case -1:
136 return -1;
137
138 default: /* parent */
139 wait(&status);
140 }
141
142 free(buf);
143 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
144 return 0;
145 return -1;
146}
147
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000148int load_xtables_ko(const char *modprobe, int quiet)
149{
150 static int loaded = 0;
151 static int ret = -1;
152
153 if (!loaded) {
154 ret = xtables_insmod(afinfo.kmod, modprobe, quiet);
155 loaded = (ret == 0);
156 }
157
158 return ret;
159}
160
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000161int string_to_number_ll(const char *s, unsigned long long min,
162 unsigned long long max, unsigned long long *ret)
163{
164 unsigned long long number;
165 char *end;
166
167 /* Handle hex, octal, etc. */
168 errno = 0;
169 number = strtoull(s, &end, 0);
170 if (*end == '\0' && end != s) {
171 /* we parsed a number, let's see if we want this */
172 if (errno != ERANGE && min <= number && (!max || number <= max)) {
173 *ret = number;
174 return 0;
175 }
176 }
177 return -1;
178}
179
180int string_to_number_l(const char *s, unsigned long min, unsigned long max,
181 unsigned long *ret)
182{
183 int result;
184 unsigned long long number;
185
186 result = string_to_number_ll(s, min, max, &number);
187 *ret = (unsigned long)number;
188
189 return result;
190}
191
192int string_to_number(const char *s, unsigned int min, unsigned int max,
193 unsigned int *ret)
194{
195 int result;
196 unsigned long number;
197
198 result = string_to_number_l(s, min, max, &number);
199 *ret = (unsigned int)number;
200
201 return result;
202}
203
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000204/*
205 * strtonum{,l} - string to number conversion
206 *
207 * If @end is NULL, we assume the caller does not want
208 * a case like "15a", so reject it.
209 */
210bool strtonuml(const char *s, char **end, unsigned long *value,
211 unsigned long min, unsigned long max)
212{
213 unsigned long v;
214 char *my_end;
215
216 errno = 0;
217 v = strtoul(s, &my_end, 0);
218
219 if (my_end == s)
220 return false;
221 if (end != NULL)
222 *end = my_end;
223
224 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
225 if (value != NULL)
226 *value = v;
227 if (end == NULL)
228 return *my_end == '\0';
229 return true;
230 }
231
232 return false;
233}
234
235bool strtonum(const char *s, char **end, unsigned int *value,
236 unsigned int min, unsigned int max)
237{
238 unsigned long v;
239 bool ret;
240
241 ret = strtonuml(s, end, &v, min, max);
242 if (value != NULL)
243 *value = v;
244 return ret;
245}
246
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000247int service_to_port(const char *name, const char *proto)
248{
249 struct servent *service;
250
251 if ((service = getservbyname(name, proto)) != NULL)
252 return ntohs((unsigned short) service->s_port);
253
254 return -1;
255}
256
257u_int16_t parse_port(const char *port, const char *proto)
258{
259 unsigned int portnum;
260
261 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
262 (portnum = service_to_port(port, proto)) != -1)
263 return (u_int16_t)portnum;
264
265 exit_error(PARAMETER_PROBLEM,
266 "invalid port/service `%s' specified", port);
267}
268
269void parse_interface(const char *arg, char *vianame, unsigned char *mask)
270{
271 int vialen = strlen(arg);
272 unsigned int i;
273
274 memset(mask, 0, IFNAMSIZ);
275 memset(vianame, 0, IFNAMSIZ);
276
277 if (vialen + 1 > IFNAMSIZ)
278 exit_error(PARAMETER_PROBLEM,
279 "interface name `%s' must be shorter than IFNAMSIZ"
280 " (%i)", arg, IFNAMSIZ-1);
281
282 strcpy(vianame, arg);
283 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
284 memset(mask, 0, IFNAMSIZ);
285 else if (vianame[vialen - 1] == '+') {
286 memset(mask, 0xFF, vialen - 1);
287 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
288 /* Don't remove `+' here! -HW */
289 } else {
290 /* Include nul-terminator in match */
291 memset(mask, 0xFF, vialen + 1);
292 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
293 for (i = 0; vianame[i]; i++) {
294 if (vianame[i] == ':' ||
295 vianame[i] == '!' ||
296 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000297 fprintf(stderr,
298 "Warning: weird character in interface"
299 " `%s' (No aliases, :, ! or *).\n",
300 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000301 break;
302 }
303 }
304 }
305}
306
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000307struct xtables_match *find_match(const char *name, enum xt_tryload tryload,
308 struct xtables_rule_match **matches)
309{
310 struct xtables_match *ptr;
311 const char *icmp6 = "icmp6";
312
313 /* This is ugly as hell. Nonetheless, there is no way of changing
314 * this without hurting backwards compatibility */
315 if ( (strcmp(name,"icmpv6") == 0) ||
316 (strcmp(name,"ipv6-icmp") == 0) ||
317 (strcmp(name,"icmp6") == 0) )
318 name = icmp6;
319
320 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
321 if (strcmp(name, ptr->name) == 0) {
322 struct xtables_match *clone;
323
324 /* First match of this type: */
325 if (ptr->m == NULL)
326 break;
327
328 /* Second and subsequent clones */
329 clone = fw_malloc(sizeof(struct xtables_match));
330 memcpy(clone, ptr, sizeof(struct xtables_match));
331 clone->mflags = 0;
332 /* This is a clone: */
333 clone->next = clone;
334
335 ptr = clone;
336 break;
337 }
338 }
339
340#ifndef NO_SHARED_LIBS
341 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
342 char path[strlen(lib_dir) + sizeof("/.so")
343 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000344
345 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
346 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000347 /* Found library. If it didn't register itself,
348 maybe they specified target as match. */
349 ptr = find_match(name, DONT_LOAD, NULL);
350
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000351 if (ptr == NULL) {
352 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
353 name);
354 if (dlopen(path, RTLD_NOW) != NULL)
355 ptr = find_match(name, DONT_LOAD, NULL);
356 }
357
358 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000359 exit_error(PARAMETER_PROBLEM,
360 "Couldn't load match `%s':%s\n",
361 name, dlerror());
362 }
363#else
364 if (ptr && !ptr->loaded) {
365 if (tryload != DONT_LOAD)
366 ptr->loaded = 1;
367 else
368 ptr = NULL;
369 }
370 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
371 exit_error(PARAMETER_PROBLEM,
372 "Couldn't find match `%s'\n", name);
373 }
374#endif
375
376 if (ptr && matches) {
377 struct xtables_rule_match **i;
378 struct xtables_rule_match *newentry;
379
380 newentry = fw_malloc(sizeof(struct xtables_rule_match));
381
382 for (i = matches; *i; i = &(*i)->next) {
383 if (strcmp(name, (*i)->match->name) == 0)
384 (*i)->completed = 1;
385 }
386 newentry->match = ptr;
387 newentry->completed = 0;
388 newentry->next = NULL;
389 *i = newentry;
390 }
391
392 return ptr;
393}
394
395
396struct xtables_target *find_target(const char *name, enum xt_tryload tryload)
397{
398 struct xtables_target *ptr;
399
400 /* Standard target? */
401 if (strcmp(name, "") == 0
402 || strcmp(name, XTC_LABEL_ACCEPT) == 0
403 || strcmp(name, XTC_LABEL_DROP) == 0
404 || strcmp(name, XTC_LABEL_QUEUE) == 0
405 || strcmp(name, XTC_LABEL_RETURN) == 0)
406 name = "standard";
407
408 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
409 if (strcmp(name, ptr->name) == 0)
410 break;
411 }
412
413#ifndef NO_SHARED_LIBS
414 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
415 char path[strlen(lib_dir) + sizeof("/.so")
416 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000417
418 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
419 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000420 /* Found library. If it didn't register itself,
421 maybe they specified match as a target. */
422 ptr = find_target(name, DONT_LOAD);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000423
424 if (ptr == NULL) {
425 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
426 name);
427 if (dlopen(path, RTLD_NOW) != NULL)
428 ptr = find_target(name, DONT_LOAD);
429 }
430 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000431 exit_error(PARAMETER_PROBLEM,
432 "Couldn't load target `%s':%s\n",
433 name, dlerror());
434 }
435#else
436 if (ptr && !ptr->loaded) {
437 if (tryload != DONT_LOAD)
438 ptr->loaded = 1;
439 else
440 ptr = NULL;
441 }
442 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
443 exit_error(PARAMETER_PROBLEM,
444 "Couldn't find target `%s'\n", name);
445 }
446#endif
447
448 if (ptr)
449 ptr->used = 1;
450
451 return ptr;
452}
453
454static int compatible_revision(const char *name, u_int8_t revision, int opt)
455{
456 struct xt_get_revision rev;
457 socklen_t s = sizeof(rev);
458 int max_rev, sockfd;
459
460 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
461 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000462 if (errno == EPERM) {
463 /* revision 0 is always supported. */
464 if (revision != 0)
465 fprintf(stderr, "Could not determine whether "
466 "revision %u is supported, "
467 "assuming it is.\n",
468 revision);
469 return 1;
470 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000471 fprintf(stderr, "Could not open socket to kernel: %s\n",
472 strerror(errno));
473 exit(1);
474 }
475
476 load_xtables_ko(modprobe, 1);
477
478 strcpy(rev.name, name);
479 rev.revision = revision;
480
481 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
482 if (max_rev < 0) {
483 /* Definitely don't support this? */
484 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
485 close(sockfd);
486 return 0;
487 } else if (errno == ENOPROTOOPT) {
488 close(sockfd);
489 /* Assume only revision 0 support (old kernel) */
490 return (revision == 0);
491 } else {
492 fprintf(stderr, "getsockopt failed strangely: %s\n",
493 strerror(errno));
494 exit(1);
495 }
496 }
497 close(sockfd);
498 return 1;
499}
500
501
502static int compatible_match_revision(const char *name, u_int8_t revision)
503{
504 return compatible_revision(name, revision, afinfo.so_rev_match);
505}
506
507static int compatible_target_revision(const char *name, u_int8_t revision)
508{
509 return compatible_revision(name, revision, afinfo.so_rev_target);
510}
511
512void xtables_register_match(struct xtables_match *me)
513{
514 struct xtables_match **i, *old;
515
516 if (strcmp(me->version, program_version) != 0) {
517 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
518 program_name, me->name, me->version, program_version);
519 exit(1);
520 }
521
522 /* Revision field stole a char from name. */
523 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
524 fprintf(stderr, "%s: target `%s' has invalid name\n",
525 program_name, me->name);
526 exit(1);
527 }
528
529 if (me->family >= NPROTO) {
530 fprintf(stderr,
531 "%s: BUG: match %s has invalid protocol family\n",
532 program_name, me->name);
533 exit(1);
534 }
535
536 /* ignore not interested match */
537 if (me->family != afinfo.family)
538 return;
539
540 old = find_match(me->name, DURING_LOAD, NULL);
541 if (old) {
542 if (old->revision == me->revision) {
543 fprintf(stderr,
544 "%s: match `%s' already registered.\n",
545 program_name, me->name);
546 exit(1);
547 }
548
549 /* Now we have two (or more) options, check compatibility. */
550 if (compatible_match_revision(old->name, old->revision)
551 && old->revision > me->revision)
552 return;
553
554 /* Replace if compatible. */
555 if (!compatible_match_revision(me->name, me->revision))
556 return;
557
558 /* Delete old one. */
559 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
560 *i = old->next;
561 }
562
563 if (me->size != XT_ALIGN(me->size)) {
564 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
565 program_name, me->name, (unsigned int)me->size);
566 exit(1);
567 }
568
569 /* Append to list. */
570 for (i = &xtables_matches; *i; i = &(*i)->next);
571 me->next = NULL;
572 *i = me;
573
574 me->m = NULL;
575 me->mflags = 0;
576}
577
578void xtables_register_target(struct xtables_target *me)
579{
580 struct xtables_target *old;
581
582 if (strcmp(me->version, program_version) != 0) {
583 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
584 program_name, me->name, me->version, program_version);
585 exit(1);
586 }
587
588 /* Revision field stole a char from name. */
589 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
590 fprintf(stderr, "%s: target `%s' has invalid name\n",
591 program_name, me->name);
592 exit(1);
593 }
594
595 if (me->family >= NPROTO) {
596 fprintf(stderr,
597 "%s: BUG: target %s has invalid protocol family\n",
598 program_name, me->name);
599 exit(1);
600 }
601
602 /* ignore not interested target */
603 if (me->family != afinfo.family)
604 return;
605
606 old = find_target(me->name, DURING_LOAD);
607 if (old) {
608 struct xtables_target **i;
609
610 if (old->revision == me->revision) {
611 fprintf(stderr,
612 "%s: target `%s' already registered.\n",
613 program_name, me->name);
614 exit(1);
615 }
616
617 /* Now we have two (or more) options, check compatibility. */
618 if (compatible_target_revision(old->name, old->revision)
619 && old->revision > me->revision)
620 return;
621
622 /* Replace if compatible. */
623 if (!compatible_target_revision(me->name, me->revision))
624 return;
625
626 /* Delete old one. */
627 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
628 *i = old->next;
629 }
630
631 if (me->size != XT_ALIGN(me->size)) {
632 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
633 program_name, me->name, (unsigned int)me->size);
634 exit(1);
635 }
636
637 /* Prepend to list. */
638 me->next = xtables_targets;
639 xtables_targets = me;
640 me->t = NULL;
641 me->tflags = 0;
642}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000643
644void param_act(unsigned int status, const char *p1, ...)
645{
646 const char *p2, *p3;
647 va_list args;
648 bool b;
649
650 va_start(args, p1);
651
652 switch (status) {
653 case P_ONLY_ONCE:
654 p2 = va_arg(args, const char *);
655 b = va_arg(args, unsigned int);
656 if (!b)
657 return;
658 exit_error(PARAMETER_PROBLEM,
659 "%s: \"%s\" option may only be specified once",
660 p1, p2);
661 break;
662 case P_NO_INVERT:
663 p2 = va_arg(args, const char *);
664 b = va_arg(args, unsigned int);
665 if (!b)
666 return;
667 exit_error(PARAMETER_PROBLEM,
668 "%s: \"%s\" option cannot be inverted", p1, p2);
669 break;
670 case P_BAD_VALUE:
671 p2 = va_arg(args, const char *);
672 p3 = va_arg(args, const char *);
673 exit_error(PARAMETER_PROBLEM,
674 "%s: Bad value for \"%s\" option: \"%s\"",
675 p1, p2, p3);
676 break;
677 case P_ONE_ACTION:
678 b = va_arg(args, unsigned int);
679 if (!b)
680 return;
681 exit_error(PARAMETER_PROBLEM,
682 "%s: At most one action is possible", p1);
683 break;
684 default:
685 exit_error(status, p1, args);
686 break;
687 }
688
689 va_end(args);
690}