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