1942cbc71068effda0c923f4e4f0ec55cfaf0aa9
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 static int lock_fd
= -1;
27 static pid_t pipe_pid
= -1;
28 static FILE *pipe_fd
= NULL
;
30 bool fw3_pr_debug
= false;
34 warn_elem_section_name(struct uci_section
*s
, bool find_name
)
38 struct uci_element
*tmp
;
42 uci_foreach_element(&s
->package
->sections
, tmp
)
44 if (strcmp(uci_to_section(tmp
)->type
, s
->type
))
53 fprintf(stderr
, "@%s[%d]", s
->type
, i
);
57 uci_foreach_element(&s
->options
, tmp
)
59 o
= uci_to_option(tmp
);
61 if (!strcmp(tmp
->name
, "name") && (o
->type
== UCI_TYPE_STRING
))
63 fprintf(stderr
, " (%s)", o
->v
.string
);
71 fprintf(stderr
, "'%s'", s
->e
.name
);
79 warn_elem(struct uci_element
*e
, const char *format
, ...)
81 if (e
->type
== UCI_TYPE_SECTION
)
83 fprintf(stderr
, "Warning: Section ");
84 warn_elem_section_name(uci_to_section(e
), true);
86 else if (e
->type
== UCI_TYPE_OPTION
)
88 fprintf(stderr
, "Warning: Option ");
89 warn_elem_section_name(uci_to_option(e
)->section
, false);
90 fprintf(stderr
, ".%s ", e
->name
);
94 va_start(argptr
, format
);
95 vfprintf(stderr
, format
, argptr
);
98 fprintf(stderr
, "\n");
102 warn(const char* format
, ...)
104 fprintf(stderr
, "Warning: ");
106 va_start(argptr
, format
);
107 vfprintf(stderr
, format
, argptr
);
109 fprintf(stderr
, "\n");
113 error(const char* format
, ...)
115 fprintf(stderr
, "Error: ");
117 va_start(argptr
, format
);
118 vfprintf(stderr
, format
, argptr
);
120 fprintf(stderr
, "\n");
126 info(const char* format
, ...)
129 va_start(argptr
, format
);
130 vfprintf(stderr
, format
, argptr
);
132 fprintf(stderr
, "\n");
136 fw3_find_command(const char *cmd
)
139 int plen
= 0, clen
= strlen(cmd
) + 1;
141 static char path
[PATH_MAX
];
143 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
146 search
= getenv("PATH");
149 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
155 if (*p
!= ':' && *p
!= '\0')
160 if ((plen
+ clen
) >= sizeof(path
))
163 strncpy(path
, search
, plen
);
164 sprintf(path
+ plen
, "/%s", cmd
);
166 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
177 fw3_stdout_pipe(void)
184 __fw3_command_pipe(bool silent
, const char *command
, ...)
190 char *arg
, **args
, **tmp
;
192 command
= fw3_find_command(command
);
201 args
= malloc(argn
* sizeof(arg
));
206 args
[0] = (char *)command
;
209 va_start(argp
, command
);
211 while ((arg
= va_arg(argp
, char *)) != NULL
)
213 tmp
= realloc(args
, ++argn
* sizeof(arg
));
225 switch ((pid
= fork()))
241 execv(command
, args
);
244 signal(SIGPIPE
, SIG_IGN
);
249 pipe_fd
= fdopen(pfds
[1], "w");
254 fw3_pr(const char *fmt
, ...)
258 if (fw3_pr_debug
&& pipe_fd
!= stdout
)
261 vfprintf(stderr
, fmt
, args
);
266 vfprintf(pipe_fd
, fmt
, args
);
271 fw3_command_close(void)
273 if (pipe_fd
&& pipe_fd
!= stdout
)
277 waitpid(pipe_pid
, NULL
, 0);
279 signal(SIGPIPE
, SIG_DFL
);
286 fw3_has_table(bool ipv6
, const char *table
)
293 const char *path
= ipv6
294 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
296 if (!(f
= fopen(path
, "r")))
299 while (fgets(line
, sizeof(line
), f
))
301 if (!strncmp(line
, table
, strlen(table
)))
317 lock_fd
= open(FW3_LOCKFILE
, O_CREAT
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
321 warn("Cannot create lock file %s: %s", FW3_LOCKFILE
, strerror(errno
));
325 if (flock(lock_fd
, LOCK_EX
))
327 warn("Cannot acquire exclusive lock: %s", strerror(errno
));
340 if (flock(lock_fd
, LOCK_UN
))
341 warn("Cannot release exclusive lock: %s", strerror(errno
));
344 unlink(FW3_LOCKFILE
);
351 fw3_read_statefile(void *state
)
357 const char *p
, *name
;
361 struct fw3_state
*s
= state
;
362 struct fw3_zone
*zone
;
363 struct fw3_ipset
*ipset
;
364 struct fw3_device
*net
, *dev
;
366 sf
= fopen(FW3_STATEFILE
, "r");
371 while (fgets(line
, sizeof(line
), sf
))
373 p
= strtok(line
, " \t\n");
378 type
= strtoul(p
, NULL
, 16);
379 name
= strtok(NULL
, " \t\n");
384 if (!(p
= strtok(NULL
, " \t\n")))
387 flags
[0] = strtoul(p
, NULL
, 16);
389 if (!(p
= strtok(NULL
, " \t\n")))
392 flags
[1] = strtoul(p
, NULL
, 16);
396 case FW3_TYPE_DEFAULTS
:
397 s
->defaults
.flags
[0] = flags
[0];
398 s
->defaults
.flags
[1] = flags
[1];
402 if (!(zone
= fw3_lookup_zone(state
, name
, false)))
404 zone
= fw3_alloc_zone();
409 zone
->name
= strdup(name
);
410 list_add_tail(&zone
->list
, &s
->zones
);
413 zone
->flags
[0] = flags
[0];
414 zone
->flags
[1] = flags
[1];
415 list_add_tail(&zone
->running_list
, &s
->running_zones
);
419 if (!(ipset
= fw3_lookup_ipset(state
, name
, false)))
421 ipset
= fw3_alloc_ipset();
426 ipset
->name
= strdup(name
);
427 list_add_tail(&ipset
->list
, &s
->ipsets
);
430 ipset
->flags
[0] = flags
[0];
431 ipset
->flags
[1] = flags
[1];
432 list_add_tail(&ipset
->running_list
, &s
->running_ipsets
);
435 case FW3_TYPE_NETWORK
:
436 if (!(zone
= fw3_lookup_zone(state
, name
, false)))
439 if (!(p
= strtok(NULL
, " \t\n")) || !(name
= strtok(NULL
, " \t\n")))
442 if (!(net
= malloc(sizeof(*net
))))
445 memset(net
, 0, sizeof(*net
));
446 snprintf(net
->name
, sizeof(net
->name
), "%s", p
);
447 list_add_tail(&net
->list
, &zone
->running_networks
);
449 if (!(dev
= malloc(sizeof(*dev
))))
452 memset(dev
, 0, sizeof(*dev
));
454 snprintf(dev
->name
, sizeof(dev
->name
), "%s", name
);
455 list_add_tail(&dev
->list
, &zone
->running_devices
);
465 fw3_write_statefile(void *state
)
468 struct fw3_state
*s
= state
;
469 struct fw3_defaults
*defs
= &s
->defaults
;
472 struct fw3_device
*d
;
474 if (fw3_no_table(defs
->flags
[0]) && fw3_no_table(defs
->flags
[1]))
476 if (unlink(FW3_STATEFILE
))
477 warn("Unable to remove state %s: %s",
478 FW3_STATEFILE
, strerror(errno
));
483 sf
= fopen(FW3_STATEFILE
, "w");
487 warn("Cannot create state %s: %s", FW3_STATEFILE
, strerror(errno
));
491 fprintf(sf
, "%x - %x %x\n",
492 FW3_TYPE_DEFAULTS
, defs
->flags
[0], defs
->flags
[1]);
494 list_for_each_entry(z
, &s
->running_zones
, running_list
)
496 if (fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
499 fprintf(sf
, "%x %s %x %x\n",
500 FW3_TYPE_ZONE
, z
->name
, z
->flags
[0], z
->flags
[1]);
502 list_for_each_entry(d
, &z
->devices
, list
)
507 fprintf(sf
, "%x %s 0 0 %s %s\n",
508 FW3_TYPE_NETWORK
, z
->name
, d
->network
->name
, d
->name
);
512 list_for_each_entry(i
, &s
->running_ipsets
, running_list
)
514 if (!fw3_no_family(i
->flags
[0]) || !fw3_no_family(i
->flags
[1]))
516 fprintf(sf
, "%x %s %x %x\n",
517 FW3_TYPE_IPSET
, i
->name
, i
->flags
[0], i
->flags
[1]);
525 struct object_list_heads
527 struct list_head list
;
528 struct list_head running_list
;
532 fw3_set_running(void *object
, struct list_head
*dest
)
534 struct object_list_heads
*o
= object
;
536 if (dest
&& !o
->running_list
.next
)
537 list_add_tail(&o
->running_list
, dest
);
538 else if (!dest
&& o
->running_list
.next
)
539 list_del(&o
->running_list
);
543 fw3_free_object(void *obj
, const void *opts
)
545 const struct fw3_option
*ol
;
546 struct list_head
*list
, *cur
, *tmp
;
548 for (ol
= opts
; ol
->name
; ol
++)
553 list
= (struct list_head
*)((char *)obj
+ ol
->offset
);
554 list_for_each_safe(cur
, tmp
, list
)
566 fw3_pr_rulespec(int table
, int family
, uint32_t *flags
, uint32_t mask
,
567 const struct fw3_rule_spec
*r
, const char *fmt
, ...)
573 uint32_t f
= flags
? flags
[family
== FW3_FAMILY_V6
] : 0;
578 for (; r
->format
; r
++)
580 if (!fw3_is_family(r
, family
))
583 if (r
->table
!= table
)
586 if ((r
->flag
!= 0) && !hasbit(f
, r
->flag
))
590 vsnprintf(buf
, sizeof(buf
), r
->format
, ap
);