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