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_alloc(size_t size
)
140 mem
= calloc(1, size
);
143 error("Out of memory while allocating %d bytes", size
);
149 fw3_strdup(const char *s
)
156 error("Out of memory while duplicating string '%s'", s
);
162 fw3_find_command(const char *cmd
)
165 int plen
= 0, clen
= strlen(cmd
) + 1;
167 static char path
[PATH_MAX
];
169 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
172 search
= getenv("PATH");
175 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
181 if (*p
!= ':' && *p
!= '\0')
186 if ((plen
+ clen
) >= sizeof(path
))
189 strncpy(path
, search
, plen
);
190 sprintf(path
+ plen
, "/%s", cmd
);
192 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
203 fw3_stdout_pipe(void)
210 __fw3_command_pipe(bool silent
, const char *command
, ...)
216 char *arg
, **args
, **tmp
;
218 command
= fw3_find_command(command
);
227 args
= malloc(argn
* sizeof(arg
));
232 args
[0] = (char *)command
;
235 va_start(argp
, command
);
237 while ((arg
= va_arg(argp
, char *)) != NULL
)
239 tmp
= realloc(args
, ++argn
* sizeof(arg
));
251 switch ((pid
= fork()))
267 execv(command
, args
);
270 signal(SIGPIPE
, SIG_IGN
);
273 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
276 pipe_fd
= fdopen(pfds
[1], "w");
281 fw3_pr(const char *fmt
, ...)
285 if (fw3_pr_debug
&& pipe_fd
!= stdout
)
288 vfprintf(stderr
, fmt
, args
);
293 vfprintf(pipe_fd
, fmt
, args
);
298 fw3_command_close(void)
300 if (pipe_fd
&& pipe_fd
!= stdout
)
304 waitpid(pipe_pid
, NULL
, 0);
306 signal(SIGPIPE
, SIG_DFL
);
313 fw3_has_table(bool ipv6
, const char *table
)
320 const char *path
= ipv6
321 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
323 if (!(f
= fopen(path
, "r")))
326 while (fgets(line
, sizeof(line
), f
))
328 if (!strncmp(line
, table
, strlen(table
)))
344 lock_fd
= open(FW3_LOCKFILE
, O_CREAT
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
348 warn("Cannot create lock file %s: %s", FW3_LOCKFILE
, strerror(errno
));
352 if (flock(lock_fd
, LOCK_EX
))
354 warn("Cannot acquire exclusive lock: %s", strerror(errno
));
367 if (flock(lock_fd
, LOCK_UN
))
368 warn("Cannot release exclusive lock: %s", strerror(errno
));
371 unlink(FW3_LOCKFILE
);
378 write_defaults_uci(struct uci_context
*ctx
, struct fw3_defaults
*d
,
379 struct uci_package
*dest
)
381 char buf
[sizeof("0xffffffff\0")];
382 struct uci_ptr ptr
= { .p
= dest
};
384 uci_add_section(ctx
, dest
, "defaults", &ptr
.s
);
387 ptr
.option
= "input";
388 ptr
.value
= fw3_flag_names
[d
->policy_input
];
392 ptr
.option
= "output";
393 ptr
.value
= fw3_flag_names
[d
->policy_output
];
397 ptr
.option
= "forward";
398 ptr
.value
= fw3_flag_names
[d
->policy_forward
];
401 sprintf(buf
, "0x%x", d
->flags
[0]);
403 ptr
.option
= "__flags_v4";
407 sprintf(buf
, "0x%x", d
->flags
[1]);
409 ptr
.option
= "__flags_v6";
415 write_zone_uci(struct uci_context
*ctx
, struct fw3_zone
*z
,
416 struct uci_package
*dest
)
418 struct fw3_device
*dev
;
419 struct fw3_address
*sub
;
420 enum fw3_family fam
= FW3_FAMILY_ANY
;
424 struct uci_ptr ptr
= { .p
= dest
};
429 if (fw3_no_table(z
->flags
[0]) && !fw3_no_table(z
->flags
[1]))
431 else if (!fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
433 else if (fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
436 uci_add_section(ctx
, dest
, "zone", &ptr
.s
);
444 ptr
.option
= "input";
445 ptr
.value
= fw3_flag_names
[z
->policy_input
];
449 ptr
.option
= "output";
450 ptr
.value
= fw3_flag_names
[z
->policy_output
];
454 ptr
.option
= "forward";
455 ptr
.value
= fw3_flag_names
[z
->policy_forward
];
460 ptr
.value
= z
->masq
? "1" : "0";
464 ptr
.option
= "conntrack";
465 ptr
.value
= z
->conntrack
? "1" : "0";
469 ptr
.option
= "mtu_fix";
470 ptr
.value
= z
->mtu_fix
? "1" : "0";
474 ptr
.option
= "custom_chains";
475 ptr
.value
= z
->custom_chains
? "1" : "0";
478 if (fam
!= FW3_FAMILY_ANY
)
481 ptr
.option
= "family";
482 ptr
.value
= fw3_flag_names
[fam
];
487 ptr
.option
= "device";
489 fw3_foreach(dev
, &z
->devices
)
497 p
+= sprintf(p
, "!");
500 p
+= sprintf(p
, "%s@%s", dev
->name
, dev
->network
);
502 p
+= sprintf(p
, "%s", dev
->name
);
505 uci_add_list(ctx
, &ptr
);
509 ptr
.option
= "subnet";
511 fw3_foreach(sub
, &z
->subnets
)
516 ptr
.value
= fw3_address_to_string(sub
, true);
517 uci_add_list(ctx
, &ptr
);
520 sprintf(buf
, "0x%x", z
->flags
[0]);
522 ptr
.option
= "__flags_v4";
526 sprintf(buf
, "0x%x", z
->flags
[1]);
528 ptr
.option
= "__flags_v6";
534 write_ipset_uci(struct uci_context
*ctx
, struct fw3_ipset
*s
,
535 struct uci_package
*dest
)
537 struct fw3_ipset_datatype
*type
;
539 char buf
[sizeof("65535-65535\0")];
541 struct uci_ptr ptr
= { .p
= dest
};
543 if (!s
->enabled
|| s
->external
)
546 uci_add_section(ctx
, dest
, "ipset", &ptr
.s
);
554 ptr
.option
= "storage";
555 ptr
.value
= fw3_ipset_method_names
[s
->method
];
558 list_for_each_entry(type
, &s
->datatypes
, list
)
560 sprintf(buf
, "%s_%s", type
->dest
? "dst" : "src",
561 fw3_ipset_type_names
[type
->type
]);
564 ptr
.option
= "match";
566 uci_add_list(ctx
, &ptr
);
572 ptr
.option
= "iprange";
573 ptr
.value
= fw3_address_to_string(&s
->iprange
, false);
577 if (s
->portrange
.set
)
579 sprintf(buf
, "%u-%u", s
->portrange
.port_min
, s
->portrange
.port_max
);
581 ptr
.option
= "portrange";
588 fw3_write_statefile(void *state
)
591 struct fw3_state
*s
= state
;
595 struct uci_package
*p
;
597 if (fw3_no_family(s
->defaults
.flags
[0]) &&
598 fw3_no_family(s
->defaults
.flags
[1]))
600 unlink(FW3_STATEFILE
);
604 sf
= fopen(FW3_STATEFILE
, "w+");
608 warn("Cannot create state %s: %s", FW3_STATEFILE
, strerror(errno
));
612 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
613 uci_unload(s
->uci
, p
);
615 uci_import(s
->uci
, sf
, "fw3_state", NULL
, true);
617 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
619 write_defaults_uci(s
->uci
, &s
->defaults
, p
);
621 list_for_each_entry(z
, &s
->zones
, list
)
622 write_zone_uci(s
->uci
, z
, p
);
624 list_for_each_entry(i
, &s
->ipsets
, list
)
625 write_ipset_uci(s
->uci
, i
, p
);
627 uci_export(s
->uci
, sf
, p
, true);
628 uci_unload(s
->uci
, p
);
638 fw3_free_object(void *obj
, const void *opts
)
640 const struct fw3_option
*ol
;
641 struct list_head
*list
, *cur
, *tmp
;
643 for (ol
= opts
; ol
->name
; ol
++)
648 list
= (struct list_head
*)((char *)obj
+ ol
->offset
);
649 list_for_each_safe(cur
, tmp
, list
)
661 fw3_hotplug(bool add
, void *zone
, void *device
)
663 struct fw3_zone
*z
= zone
;
664 struct fw3_device
*d
= device
;
672 warn("Unable to fork(): %s\n", strerror(errno
));
688 setenv("ACTION", add
? "add" : "remove", 1);
689 setenv("ZONE", z
->name
, 1);
690 setenv("INTERFACE", d
->network
, 1);
691 setenv("DEVICE", d
->name
, 1);
693 execl(FW3_HOTPLUG
, FW3_HOTPLUG
, "firewall", NULL
);