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.
27 static int lock_fd
= -1;
28 static pid_t pipe_pid
= -1;
29 static FILE *pipe_fd
= NULL
;
31 bool fw3_pr_debug
= false;
35 warn_elem_section_name(struct uci_section
*s
, bool find_name
)
39 struct uci_element
*tmp
;
43 uci_foreach_element(&s
->package
->sections
, tmp
)
45 if (strcmp(uci_to_section(tmp
)->type
, s
->type
))
54 fprintf(stderr
, "@%s[%d]", s
->type
, i
);
58 uci_foreach_element(&s
->options
, tmp
)
60 o
= uci_to_option(tmp
);
62 if (!strcmp(tmp
->name
, "name") && (o
->type
== UCI_TYPE_STRING
))
64 fprintf(stderr
, " (%s)", o
->v
.string
);
72 fprintf(stderr
, "'%s'", s
->e
.name
);
80 warn_elem(struct uci_element
*e
, const char *format
, ...)
82 if (e
->type
== UCI_TYPE_SECTION
)
84 fprintf(stderr
, "Warning: Section ");
85 warn_elem_section_name(uci_to_section(e
), true);
87 else if (e
->type
== UCI_TYPE_OPTION
)
89 fprintf(stderr
, "Warning: Option ");
90 warn_elem_section_name(uci_to_option(e
)->section
, false);
91 fprintf(stderr
, ".%s ", e
->name
);
95 va_start(argptr
, format
);
96 vfprintf(stderr
, format
, argptr
);
99 fprintf(stderr
, "\n");
103 warn(const char* format
, ...)
105 fprintf(stderr
, "Warning: ");
107 va_start(argptr
, format
);
108 vfprintf(stderr
, format
, argptr
);
110 fprintf(stderr
, "\n");
114 error(const char* format
, ...)
116 fprintf(stderr
, "Error: ");
118 va_start(argptr
, format
);
119 vfprintf(stderr
, format
, argptr
);
121 fprintf(stderr
, "\n");
127 info(const char* format
, ...)
130 va_start(argptr
, format
);
131 vfprintf(stderr
, format
, argptr
);
133 fprintf(stderr
, "\n");
137 fw3_alloc(size_t size
)
141 mem
= calloc(1, size
);
144 error("Out of memory while allocating %d bytes", size
);
150 fw3_strdup(const char *s
)
157 error("Out of memory while duplicating string '%s'", s
);
163 fw3_find_command(const char *cmd
)
166 int plen
= 0, clen
= strlen(cmd
) + 1;
168 static char path
[PATH_MAX
];
170 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
173 search
= getenv("PATH");
176 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
182 if (*p
!= ':' && *p
!= '\0')
187 if ((plen
+ clen
) >= sizeof(path
))
190 strncpy(path
, search
, plen
);
191 sprintf(path
+ plen
, "/%s", cmd
);
193 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
204 fw3_stdout_pipe(void)
211 __fw3_command_pipe(bool silent
, const char *command
, ...)
217 char *arg
, **args
, **tmp
;
219 command
= fw3_find_command(command
);
228 args
= malloc(argn
* sizeof(arg
));
233 args
[0] = (char *)command
;
236 va_start(argp
, command
);
238 while ((arg
= va_arg(argp
, char *)) != NULL
)
240 tmp
= realloc(args
, ++argn
* sizeof(arg
));
252 switch ((pid
= fork()))
268 execv(command
, args
);
271 signal(SIGPIPE
, SIG_IGN
);
274 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
277 pipe_fd
= fdopen(pfds
[1], "w");
282 fw3_pr(const char *fmt
, ...)
286 if (fw3_pr_debug
&& pipe_fd
!= stdout
)
289 vfprintf(stderr
, fmt
, args
);
294 vfprintf(pipe_fd
, fmt
, args
);
299 fw3_command_close(void)
301 if (pipe_fd
&& pipe_fd
!= stdout
)
305 waitpid(pipe_pid
, NULL
, 0);
307 signal(SIGPIPE
, SIG_DFL
);
314 fw3_has_table(bool ipv6
, const char *table
)
321 const char *path
= ipv6
322 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
324 if (!(f
= fopen(path
, "r")))
327 while (fgets(line
, sizeof(line
), f
))
329 if (!strncmp(line
, table
, strlen(table
)))
345 lock_fd
= open(FW3_LOCKFILE
, O_CREAT
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
349 warn("Cannot create lock file %s: %s", FW3_LOCKFILE
, strerror(errno
));
353 if (flock(lock_fd
, LOCK_EX
))
355 warn("Cannot acquire exclusive lock: %s", strerror(errno
));
368 if (flock(lock_fd
, LOCK_UN
))
369 warn("Cannot release exclusive lock: %s", strerror(errno
));
372 unlink(FW3_LOCKFILE
);
379 write_defaults_uci(struct uci_context
*ctx
, struct fw3_defaults
*d
,
380 struct uci_package
*dest
)
382 char buf
[sizeof("0xffffffff\0")];
383 struct uci_ptr ptr
= { .p
= dest
};
385 uci_add_section(ctx
, dest
, "defaults", &ptr
.s
);
388 ptr
.option
= "input";
389 ptr
.value
= fw3_flag_names
[d
->policy_input
];
393 ptr
.option
= "output";
394 ptr
.value
= fw3_flag_names
[d
->policy_output
];
398 ptr
.option
= "forward";
399 ptr
.value
= fw3_flag_names
[d
->policy_forward
];
402 sprintf(buf
, "0x%x", d
->flags
[0]);
404 ptr
.option
= "__flags_v4";
408 sprintf(buf
, "0x%x", d
->flags
[1]);
410 ptr
.option
= "__flags_v6";
416 write_zone_uci(struct uci_context
*ctx
, struct fw3_zone
*z
,
417 struct uci_package
*dest
)
419 struct fw3_device
*dev
;
420 struct fw3_address
*sub
;
421 enum fw3_family fam
= FW3_FAMILY_ANY
;
425 struct uci_ptr ptr
= { .p
= dest
};
430 if (fw3_no_table(z
->flags
[0]) && !fw3_no_table(z
->flags
[1]))
432 else if (!fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
434 else if (fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
437 uci_add_section(ctx
, dest
, "zone", &ptr
.s
);
445 ptr
.option
= "input";
446 ptr
.value
= fw3_flag_names
[z
->policy_input
];
450 ptr
.option
= "output";
451 ptr
.value
= fw3_flag_names
[z
->policy_output
];
455 ptr
.option
= "forward";
456 ptr
.value
= fw3_flag_names
[z
->policy_forward
];
461 ptr
.value
= z
->masq
? "1" : "0";
465 ptr
.option
= "conntrack";
466 ptr
.value
= z
->conntrack
? "1" : "0";
470 ptr
.option
= "mtu_fix";
471 ptr
.value
= z
->mtu_fix
? "1" : "0";
475 ptr
.option
= "custom_chains";
476 ptr
.value
= z
->custom_chains
? "1" : "0";
479 if (fam
!= FW3_FAMILY_ANY
)
482 ptr
.option
= "family";
483 ptr
.value
= fw3_flag_names
[fam
];
488 ptr
.option
= "device";
490 fw3_foreach(dev
, &z
->devices
)
498 p
+= sprintf(p
, "!");
501 p
+= sprintf(p
, "%s@%s", dev
->name
, dev
->network
);
503 p
+= sprintf(p
, "%s", dev
->name
);
506 uci_add_list(ctx
, &ptr
);
510 ptr
.option
= "subnet";
512 fw3_foreach(sub
, &z
->subnets
)
517 ptr
.value
= fw3_address_to_string(sub
, true);
518 uci_add_list(ctx
, &ptr
);
521 sprintf(buf
, "0x%x", z
->flags
[0]);
523 ptr
.option
= "__flags_v4";
527 sprintf(buf
, "0x%x", z
->flags
[1]);
529 ptr
.option
= "__flags_v6";
535 write_ipset_uci(struct uci_context
*ctx
, struct fw3_ipset
*s
,
536 struct uci_package
*dest
)
538 struct fw3_ipset_datatype
*type
;
540 char buf
[sizeof("65535-65535\0")];
542 struct uci_ptr ptr
= { .p
= dest
};
544 if (!s
->enabled
|| s
->external
)
547 uci_add_section(ctx
, dest
, "ipset", &ptr
.s
);
555 ptr
.option
= "storage";
556 ptr
.value
= fw3_ipset_method_names
[s
->method
];
559 list_for_each_entry(type
, &s
->datatypes
, list
)
561 sprintf(buf
, "%s_%s", type
->dir
, fw3_ipset_type_names
[type
->type
]);
563 ptr
.option
= "match";
565 uci_add_list(ctx
, &ptr
);
571 ptr
.option
= "iprange";
572 ptr
.value
= fw3_address_to_string(&s
->iprange
, false);
576 if (s
->portrange
.set
)
578 sprintf(buf
, "%u-%u", s
->portrange
.port_min
, s
->portrange
.port_max
);
580 ptr
.option
= "portrange";
587 fw3_write_statefile(void *state
)
590 struct fw3_state
*s
= state
;
594 struct uci_package
*p
;
596 if (fw3_no_family(s
->defaults
.flags
[0]) &&
597 fw3_no_family(s
->defaults
.flags
[1]))
599 unlink(FW3_STATEFILE
);
603 sf
= fopen(FW3_STATEFILE
, "w+");
607 warn("Cannot create state %s: %s", FW3_STATEFILE
, strerror(errno
));
611 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
612 uci_unload(s
->uci
, p
);
614 uci_import(s
->uci
, sf
, "fw3_state", NULL
, true);
616 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
618 write_defaults_uci(s
->uci
, &s
->defaults
, p
);
620 list_for_each_entry(z
, &s
->zones
, list
)
621 write_zone_uci(s
->uci
, z
, p
);
623 list_for_each_entry(i
, &s
->ipsets
, list
)
624 write_ipset_uci(s
->uci
, i
, p
);
626 uci_export(s
->uci
, sf
, p
, true);
627 uci_unload(s
->uci
, p
);
637 fw3_free_object(void *obj
, const void *opts
)
639 const struct fw3_option
*ol
;
640 struct list_head
*list
, *cur
, *tmp
;
642 for (ol
= opts
; ol
->name
; ol
++)
647 list
= (struct list_head
*)((char *)obj
+ ol
->offset
);
648 list_for_each_safe(cur
, tmp
, list
)
659 fw3_free_list(struct list_head
*head
)
661 struct list_head
*entry
, *tmp
;
666 list_for_each_safe(entry
, tmp
, head
)
676 fw3_hotplug(bool add
, void *zone
, void *device
)
678 struct fw3_zone
*z
= zone
;
679 struct fw3_device
*d
= device
;
687 warn("Unable to fork(): %s\n", strerror(errno
));
703 setenv("ACTION", add
? "add" : "remove", 1);
704 setenv("ZONE", z
->name
, 1);
705 setenv("INTERFACE", d
->network
, 1);
706 setenv("DEVICE", d
->name
, 1);
708 execl(FW3_HOTPLUG
, FW3_HOTPLUG
, "firewall", NULL
);