blob: 3cc864b203ee9566f945351bb6af70371a65a2ec [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 KOZAKAI0b82e8e2007-07-24 05:47:40 +000019#include <dlfcn.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000020#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000021#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000022#include <netdb.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000023#include <stdio.h>
24#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000025#include <string.h>
26#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000027#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000028#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/wait.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000031
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000032#include <xtables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000034#define NPROTO 255
35
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000036#ifndef PROC_SYS_MODPROBE
37#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
38#endif
39
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000040char *lib_dir;
41
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000042/* the path to command to load kernel module */
43const char *modprobe = NULL;
44
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000045/* Keeping track of external matches and targets: linked lists. */
46struct xtables_match *xtables_matches;
47struct xtables_target *xtables_targets;
48
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000049void *fw_calloc(size_t count, size_t size)
50{
51 void *p;
52
53 if ((p = calloc(count, size)) == NULL) {
54 perror("ip[6]tables: calloc failed");
55 exit(1);
56 }
57
58 return p;
59}
60
61void *fw_malloc(size_t size)
62{
63 void *p;
64
65 if ((p = malloc(size)) == NULL) {
66 perror("ip[6]tables: malloc failed");
67 exit(1);
68 }
69
70 return p;
71}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000072
73static char *get_modprobe(void)
74{
75 int procfile;
76 char *ret;
77
78#define PROCFILE_BUFSIZ 1024
79 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
80 if (procfile < 0)
81 return NULL;
82
83 ret = (char *) malloc(PROCFILE_BUFSIZ);
84 if (ret) {
85 memset(ret, 0, PROCFILE_BUFSIZ);
86 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
87 case -1: goto fail;
88 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
89 }
90 if (ret[strlen(ret)-1]=='\n')
91 ret[strlen(ret)-1]=0;
92 close(procfile);
93 return ret;
94 }
95 fail:
96 free(ret);
97 close(procfile);
98 return NULL;
99}
100
101int xtables_insmod(const char *modname, const char *modprobe, int quiet)
102{
103 char *buf = NULL;
104 char *argv[4];
105 int status;
106
107 /* If they don't explicitly set it, read out of kernel */
108 if (!modprobe) {
109 buf = get_modprobe();
110 if (!buf)
111 return -1;
112 modprobe = buf;
113 }
114
115 switch (fork()) {
116 case 0:
117 argv[0] = (char *)modprobe;
118 argv[1] = (char *)modname;
119 if (quiet) {
120 argv[2] = "-q";
121 argv[3] = NULL;
122 } else {
123 argv[2] = NULL;
124 argv[3] = NULL;
125 }
126 execv(argv[0], argv);
127
128 /* not usually reached */
129 exit(1);
130 case -1:
131 return -1;
132
133 default: /* parent */
134 wait(&status);
135 }
136
137 free(buf);
138 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
139 return 0;
140 return -1;
141}
142
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000143int load_xtables_ko(const char *modprobe, int quiet)
144{
145 static int loaded = 0;
146 static int ret = -1;
147
148 if (!loaded) {
149 ret = xtables_insmod(afinfo.kmod, modprobe, quiet);
150 loaded = (ret == 0);
151 }
152
153 return ret;
154}
155
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000156int string_to_number_ll(const char *s, unsigned long long min,
157 unsigned long long max, unsigned long long *ret)
158{
159 unsigned long long number;
160 char *end;
161
162 /* Handle hex, octal, etc. */
163 errno = 0;
164 number = strtoull(s, &end, 0);
165 if (*end == '\0' && end != s) {
166 /* we parsed a number, let's see if we want this */
167 if (errno != ERANGE && min <= number && (!max || number <= max)) {
168 *ret = number;
169 return 0;
170 }
171 }
172 return -1;
173}
174
175int string_to_number_l(const char *s, unsigned long min, unsigned long max,
176 unsigned long *ret)
177{
178 int result;
179 unsigned long long number;
180
181 result = string_to_number_ll(s, min, max, &number);
182 *ret = (unsigned long)number;
183
184 return result;
185}
186
187int string_to_number(const char *s, unsigned int min, unsigned int max,
188 unsigned int *ret)
189{
190 int result;
191 unsigned long number;
192
193 result = string_to_number_l(s, min, max, &number);
194 *ret = (unsigned int)number;
195
196 return result;
197}
198
199int service_to_port(const char *name, const char *proto)
200{
201 struct servent *service;
202
203 if ((service = getservbyname(name, proto)) != NULL)
204 return ntohs((unsigned short) service->s_port);
205
206 return -1;
207}
208
209u_int16_t parse_port(const char *port, const char *proto)
210{
211 unsigned int portnum;
212
213 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
214 (portnum = service_to_port(port, proto)) != -1)
215 return (u_int16_t)portnum;
216
217 exit_error(PARAMETER_PROBLEM,
218 "invalid port/service `%s' specified", port);
219}
220
221void parse_interface(const char *arg, char *vianame, unsigned char *mask)
222{
223 int vialen = strlen(arg);
224 unsigned int i;
225
226 memset(mask, 0, IFNAMSIZ);
227 memset(vianame, 0, IFNAMSIZ);
228
229 if (vialen + 1 > IFNAMSIZ)
230 exit_error(PARAMETER_PROBLEM,
231 "interface name `%s' must be shorter than IFNAMSIZ"
232 " (%i)", arg, IFNAMSIZ-1);
233
234 strcpy(vianame, arg);
235 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
236 memset(mask, 0, IFNAMSIZ);
237 else if (vianame[vialen - 1] == '+') {
238 memset(mask, 0xFF, vialen - 1);
239 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
240 /* Don't remove `+' here! -HW */
241 } else {
242 /* Include nul-terminator in match */
243 memset(mask, 0xFF, vialen + 1);
244 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
245 for (i = 0; vianame[i]; i++) {
246 if (vianame[i] == ':' ||
247 vianame[i] == '!' ||
248 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000249 fprintf(stderr,
250 "Warning: weird character in interface"
251 " `%s' (No aliases, :, ! or *).\n",
252 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000253 break;
254 }
255 }
256 }
257}
258
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000259struct xtables_match *find_match(const char *name, enum xt_tryload tryload,
260 struct xtables_rule_match **matches)
261{
262 struct xtables_match *ptr;
263 const char *icmp6 = "icmp6";
264
265 /* This is ugly as hell. Nonetheless, there is no way of changing
266 * this without hurting backwards compatibility */
267 if ( (strcmp(name,"icmpv6") == 0) ||
268 (strcmp(name,"ipv6-icmp") == 0) ||
269 (strcmp(name,"icmp6") == 0) )
270 name = icmp6;
271
272 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
273 if (strcmp(name, ptr->name) == 0) {
274 struct xtables_match *clone;
275
276 /* First match of this type: */
277 if (ptr->m == NULL)
278 break;
279
280 /* Second and subsequent clones */
281 clone = fw_malloc(sizeof(struct xtables_match));
282 memcpy(clone, ptr, sizeof(struct xtables_match));
283 clone->mflags = 0;
284 /* This is a clone: */
285 clone->next = clone;
286
287 ptr = clone;
288 break;
289 }
290 }
291
292#ifndef NO_SHARED_LIBS
293 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
294 char path[strlen(lib_dir) + sizeof("/.so")
295 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000296
297 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
298 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000299 /* Found library. If it didn't register itself,
300 maybe they specified target as match. */
301 ptr = find_match(name, DONT_LOAD, NULL);
302
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000303 if (ptr == NULL) {
304 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
305 name);
306 if (dlopen(path, RTLD_NOW) != NULL)
307 ptr = find_match(name, DONT_LOAD, NULL);
308 }
309
310 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000311 exit_error(PARAMETER_PROBLEM,
312 "Couldn't load match `%s':%s\n",
313 name, dlerror());
314 }
315#else
316 if (ptr && !ptr->loaded) {
317 if (tryload != DONT_LOAD)
318 ptr->loaded = 1;
319 else
320 ptr = NULL;
321 }
322 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
323 exit_error(PARAMETER_PROBLEM,
324 "Couldn't find match `%s'\n", name);
325 }
326#endif
327
328 if (ptr && matches) {
329 struct xtables_rule_match **i;
330 struct xtables_rule_match *newentry;
331
332 newentry = fw_malloc(sizeof(struct xtables_rule_match));
333
334 for (i = matches; *i; i = &(*i)->next) {
335 if (strcmp(name, (*i)->match->name) == 0)
336 (*i)->completed = 1;
337 }
338 newentry->match = ptr;
339 newentry->completed = 0;
340 newentry->next = NULL;
341 *i = newentry;
342 }
343
344 return ptr;
345}
346
347
348struct xtables_target *find_target(const char *name, enum xt_tryload tryload)
349{
350 struct xtables_target *ptr;
351
352 /* Standard target? */
353 if (strcmp(name, "") == 0
354 || strcmp(name, XTC_LABEL_ACCEPT) == 0
355 || strcmp(name, XTC_LABEL_DROP) == 0
356 || strcmp(name, XTC_LABEL_QUEUE) == 0
357 || strcmp(name, XTC_LABEL_RETURN) == 0)
358 name = "standard";
359
360 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
361 if (strcmp(name, ptr->name) == 0)
362 break;
363 }
364
365#ifndef NO_SHARED_LIBS
366 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
367 char path[strlen(lib_dir) + sizeof("/.so")
368 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000369
370 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
371 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000372 /* Found library. If it didn't register itself,
373 maybe they specified match as a target. */
374 ptr = find_target(name, DONT_LOAD);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000375
376 if (ptr == NULL) {
377 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
378 name);
379 if (dlopen(path, RTLD_NOW) != NULL)
380 ptr = find_target(name, DONT_LOAD);
381 }
382 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000383 exit_error(PARAMETER_PROBLEM,
384 "Couldn't load target `%s':%s\n",
385 name, dlerror());
386 }
387#else
388 if (ptr && !ptr->loaded) {
389 if (tryload != DONT_LOAD)
390 ptr->loaded = 1;
391 else
392 ptr = NULL;
393 }
394 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
395 exit_error(PARAMETER_PROBLEM,
396 "Couldn't find target `%s'\n", name);
397 }
398#endif
399
400 if (ptr)
401 ptr->used = 1;
402
403 return ptr;
404}
405
406static int compatible_revision(const char *name, u_int8_t revision, int opt)
407{
408 struct xt_get_revision rev;
409 socklen_t s = sizeof(rev);
410 int max_rev, sockfd;
411
412 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
413 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000414 if (errno == EPERM) {
415 /* revision 0 is always supported. */
416 if (revision != 0)
417 fprintf(stderr, "Could not determine whether "
418 "revision %u is supported, "
419 "assuming it is.\n",
420 revision);
421 return 1;
422 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000423 fprintf(stderr, "Could not open socket to kernel: %s\n",
424 strerror(errno));
425 exit(1);
426 }
427
428 load_xtables_ko(modprobe, 1);
429
430 strcpy(rev.name, name);
431 rev.revision = revision;
432
433 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
434 if (max_rev < 0) {
435 /* Definitely don't support this? */
436 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
437 close(sockfd);
438 return 0;
439 } else if (errno == ENOPROTOOPT) {
440 close(sockfd);
441 /* Assume only revision 0 support (old kernel) */
442 return (revision == 0);
443 } else {
444 fprintf(stderr, "getsockopt failed strangely: %s\n",
445 strerror(errno));
446 exit(1);
447 }
448 }
449 close(sockfd);
450 return 1;
451}
452
453
454static int compatible_match_revision(const char *name, u_int8_t revision)
455{
456 return compatible_revision(name, revision, afinfo.so_rev_match);
457}
458
459static int compatible_target_revision(const char *name, u_int8_t revision)
460{
461 return compatible_revision(name, revision, afinfo.so_rev_target);
462}
463
464void xtables_register_match(struct xtables_match *me)
465{
466 struct xtables_match **i, *old;
467
468 if (strcmp(me->version, program_version) != 0) {
469 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
470 program_name, me->name, me->version, program_version);
471 exit(1);
472 }
473
474 /* Revision field stole a char from name. */
475 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
476 fprintf(stderr, "%s: target `%s' has invalid name\n",
477 program_name, me->name);
478 exit(1);
479 }
480
481 if (me->family >= NPROTO) {
482 fprintf(stderr,
483 "%s: BUG: match %s has invalid protocol family\n",
484 program_name, me->name);
485 exit(1);
486 }
487
488 /* ignore not interested match */
489 if (me->family != afinfo.family)
490 return;
491
492 old = find_match(me->name, DURING_LOAD, NULL);
493 if (old) {
494 if (old->revision == me->revision) {
495 fprintf(stderr,
496 "%s: match `%s' already registered.\n",
497 program_name, me->name);
498 exit(1);
499 }
500
501 /* Now we have two (or more) options, check compatibility. */
502 if (compatible_match_revision(old->name, old->revision)
503 && old->revision > me->revision)
504 return;
505
506 /* Replace if compatible. */
507 if (!compatible_match_revision(me->name, me->revision))
508 return;
509
510 /* Delete old one. */
511 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
512 *i = old->next;
513 }
514
515 if (me->size != XT_ALIGN(me->size)) {
516 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
517 program_name, me->name, (unsigned int)me->size);
518 exit(1);
519 }
520
521 /* Append to list. */
522 for (i = &xtables_matches; *i; i = &(*i)->next);
523 me->next = NULL;
524 *i = me;
525
526 me->m = NULL;
527 me->mflags = 0;
528}
529
530void xtables_register_target(struct xtables_target *me)
531{
532 struct xtables_target *old;
533
534 if (strcmp(me->version, program_version) != 0) {
535 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
536 program_name, me->name, me->version, program_version);
537 exit(1);
538 }
539
540 /* Revision field stole a char from name. */
541 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
542 fprintf(stderr, "%s: target `%s' has invalid name\n",
543 program_name, me->name);
544 exit(1);
545 }
546
547 if (me->family >= NPROTO) {
548 fprintf(stderr,
549 "%s: BUG: target %s has invalid protocol family\n",
550 program_name, me->name);
551 exit(1);
552 }
553
554 /* ignore not interested target */
555 if (me->family != afinfo.family)
556 return;
557
558 old = find_target(me->name, DURING_LOAD);
559 if (old) {
560 struct xtables_target **i;
561
562 if (old->revision == me->revision) {
563 fprintf(stderr,
564 "%s: target `%s' already registered.\n",
565 program_name, me->name);
566 exit(1);
567 }
568
569 /* Now we have two (or more) options, check compatibility. */
570 if (compatible_target_revision(old->name, old->revision)
571 && old->revision > me->revision)
572 return;
573
574 /* Replace if compatible. */
575 if (!compatible_target_revision(me->name, me->revision))
576 return;
577
578 /* Delete old one. */
579 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
580 *i = old->next;
581 }
582
583 if (me->size != XT_ALIGN(me->size)) {
584 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
585 program_name, me->name, (unsigned int)me->size);
586 exit(1);
587 }
588
589 /* Prepend to list. */
590 me->next = xtables_targets;
591 xtables_targets = me;
592 me->t = NULL;
593 me->tflags = 0;
594}