blob: 58b4e81b45a11387d8b56c6fc903360258831c60 [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] == '*') {
249 printf("Warning: weird character in interface"
250 " `%s' (No aliases, :, ! or *).\n",
251 vianame);
252 break;
253 }
254 }
255 }
256}
257
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000258struct xtables_match *find_match(const char *name, enum xt_tryload tryload,
259 struct xtables_rule_match **matches)
260{
261 struct xtables_match *ptr;
262 const char *icmp6 = "icmp6";
263
264 /* This is ugly as hell. Nonetheless, there is no way of changing
265 * this without hurting backwards compatibility */
266 if ( (strcmp(name,"icmpv6") == 0) ||
267 (strcmp(name,"ipv6-icmp") == 0) ||
268 (strcmp(name,"icmp6") == 0) )
269 name = icmp6;
270
271 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
272 if (strcmp(name, ptr->name) == 0) {
273 struct xtables_match *clone;
274
275 /* First match of this type: */
276 if (ptr->m == NULL)
277 break;
278
279 /* Second and subsequent clones */
280 clone = fw_malloc(sizeof(struct xtables_match));
281 memcpy(clone, ptr, sizeof(struct xtables_match));
282 clone->mflags = 0;
283 /* This is a clone: */
284 clone->next = clone;
285
286 ptr = clone;
287 break;
288 }
289 }
290
291#ifndef NO_SHARED_LIBS
292 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
293 char path[strlen(lib_dir) + sizeof("/.so")
294 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000295
296 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
297 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000298 /* Found library. If it didn't register itself,
299 maybe they specified target as match. */
300 ptr = find_match(name, DONT_LOAD, NULL);
301
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000302 if (ptr == NULL) {
303 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
304 name);
305 if (dlopen(path, RTLD_NOW) != NULL)
306 ptr = find_match(name, DONT_LOAD, NULL);
307 }
308
309 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000310 exit_error(PARAMETER_PROBLEM,
311 "Couldn't load match `%s':%s\n",
312 name, dlerror());
313 }
314#else
315 if (ptr && !ptr->loaded) {
316 if (tryload != DONT_LOAD)
317 ptr->loaded = 1;
318 else
319 ptr = NULL;
320 }
321 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
322 exit_error(PARAMETER_PROBLEM,
323 "Couldn't find match `%s'\n", name);
324 }
325#endif
326
327 if (ptr && matches) {
328 struct xtables_rule_match **i;
329 struct xtables_rule_match *newentry;
330
331 newentry = fw_malloc(sizeof(struct xtables_rule_match));
332
333 for (i = matches; *i; i = &(*i)->next) {
334 if (strcmp(name, (*i)->match->name) == 0)
335 (*i)->completed = 1;
336 }
337 newentry->match = ptr;
338 newentry->completed = 0;
339 newentry->next = NULL;
340 *i = newentry;
341 }
342
343 return ptr;
344}
345
346
347struct xtables_target *find_target(const char *name, enum xt_tryload tryload)
348{
349 struct xtables_target *ptr;
350
351 /* Standard target? */
352 if (strcmp(name, "") == 0
353 || strcmp(name, XTC_LABEL_ACCEPT) == 0
354 || strcmp(name, XTC_LABEL_DROP) == 0
355 || strcmp(name, XTC_LABEL_QUEUE) == 0
356 || strcmp(name, XTC_LABEL_RETURN) == 0)
357 name = "standard";
358
359 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
360 if (strcmp(name, ptr->name) == 0)
361 break;
362 }
363
364#ifndef NO_SHARED_LIBS
365 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
366 char path[strlen(lib_dir) + sizeof("/.so")
367 + strlen(afinfo.libprefix) + strlen(name)];
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000368
369 sprintf(path, "%s/libxt_%s.so", lib_dir, name);
370 if (dlopen(path, RTLD_NOW) != NULL)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000371 /* Found library. If it didn't register itself,
372 maybe they specified match as a target. */
373 ptr = find_target(name, DONT_LOAD);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000374
375 if (ptr == NULL) {
376 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix,
377 name);
378 if (dlopen(path, RTLD_NOW) != NULL)
379 ptr = find_target(name, DONT_LOAD);
380 }
381 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000382 exit_error(PARAMETER_PROBLEM,
383 "Couldn't load target `%s':%s\n",
384 name, dlerror());
385 }
386#else
387 if (ptr && !ptr->loaded) {
388 if (tryload != DONT_LOAD)
389 ptr->loaded = 1;
390 else
391 ptr = NULL;
392 }
393 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
394 exit_error(PARAMETER_PROBLEM,
395 "Couldn't find target `%s'\n", name);
396 }
397#endif
398
399 if (ptr)
400 ptr->used = 1;
401
402 return ptr;
403}
404
405static int compatible_revision(const char *name, u_int8_t revision, int opt)
406{
407 struct xt_get_revision rev;
408 socklen_t s = sizeof(rev);
409 int max_rev, sockfd;
410
411 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
412 if (sockfd < 0) {
413 fprintf(stderr, "Could not open socket to kernel: %s\n",
414 strerror(errno));
415 exit(1);
416 }
417
418 load_xtables_ko(modprobe, 1);
419
420 strcpy(rev.name, name);
421 rev.revision = revision;
422
423 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
424 if (max_rev < 0) {
425 /* Definitely don't support this? */
426 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
427 close(sockfd);
428 return 0;
429 } else if (errno == ENOPROTOOPT) {
430 close(sockfd);
431 /* Assume only revision 0 support (old kernel) */
432 return (revision == 0);
433 } else {
434 fprintf(stderr, "getsockopt failed strangely: %s\n",
435 strerror(errno));
436 exit(1);
437 }
438 }
439 close(sockfd);
440 return 1;
441}
442
443
444static int compatible_match_revision(const char *name, u_int8_t revision)
445{
446 return compatible_revision(name, revision, afinfo.so_rev_match);
447}
448
449static int compatible_target_revision(const char *name, u_int8_t revision)
450{
451 return compatible_revision(name, revision, afinfo.so_rev_target);
452}
453
454void xtables_register_match(struct xtables_match *me)
455{
456 struct xtables_match **i, *old;
457
458 if (strcmp(me->version, program_version) != 0) {
459 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
460 program_name, me->name, me->version, program_version);
461 exit(1);
462 }
463
464 /* Revision field stole a char from name. */
465 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
466 fprintf(stderr, "%s: target `%s' has invalid name\n",
467 program_name, me->name);
468 exit(1);
469 }
470
471 if (me->family >= NPROTO) {
472 fprintf(stderr,
473 "%s: BUG: match %s has invalid protocol family\n",
474 program_name, me->name);
475 exit(1);
476 }
477
478 /* ignore not interested match */
479 if (me->family != afinfo.family)
480 return;
481
482 old = find_match(me->name, DURING_LOAD, NULL);
483 if (old) {
484 if (old->revision == me->revision) {
485 fprintf(stderr,
486 "%s: match `%s' already registered.\n",
487 program_name, me->name);
488 exit(1);
489 }
490
491 /* Now we have two (or more) options, check compatibility. */
492 if (compatible_match_revision(old->name, old->revision)
493 && old->revision > me->revision)
494 return;
495
496 /* Replace if compatible. */
497 if (!compatible_match_revision(me->name, me->revision))
498 return;
499
500 /* Delete old one. */
501 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
502 *i = old->next;
503 }
504
505 if (me->size != XT_ALIGN(me->size)) {
506 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
507 program_name, me->name, (unsigned int)me->size);
508 exit(1);
509 }
510
511 /* Append to list. */
512 for (i = &xtables_matches; *i; i = &(*i)->next);
513 me->next = NULL;
514 *i = me;
515
516 me->m = NULL;
517 me->mflags = 0;
518}
519
520void xtables_register_target(struct xtables_target *me)
521{
522 struct xtables_target *old;
523
524 if (strcmp(me->version, program_version) != 0) {
525 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
526 program_name, me->name, me->version, program_version);
527 exit(1);
528 }
529
530 /* Revision field stole a char from name. */
531 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
532 fprintf(stderr, "%s: target `%s' has invalid name\n",
533 program_name, me->name);
534 exit(1);
535 }
536
537 if (me->family >= NPROTO) {
538 fprintf(stderr,
539 "%s: BUG: target %s has invalid protocol family\n",
540 program_name, me->name);
541 exit(1);
542 }
543
544 /* ignore not interested target */
545 if (me->family != afinfo.family)
546 return;
547
548 old = find_target(me->name, DURING_LOAD);
549 if (old) {
550 struct xtables_target **i;
551
552 if (old->revision == me->revision) {
553 fprintf(stderr,
554 "%s: target `%s' already registered.\n",
555 program_name, me->name);
556 exit(1);
557 }
558
559 /* Now we have two (or more) options, check compatibility. */
560 if (compatible_target_revision(old->name, old->revision)
561 && old->revision > me->revision)
562 return;
563
564 /* Replace if compatible. */
565 if (!compatible_target_revision(me->name, me->revision))
566 return;
567
568 /* Delete old one. */
569 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
570 *i = old->next;
571 }
572
573 if (me->size != XT_ALIGN(me->size)) {
574 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
575 program_name, me->name, (unsigned int)me->size);
576 exit(1);
577 }
578
579 /* Prepend to list. */
580 me->next = xtables_targets;
581 xtables_targets = me;
582 me->t = NULL;
583 me->tflags = 0;
584}