9f57e9f8e03701a33010a0ffb26790680b45e7a0
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
);
247 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
250 pipe_fd
= fdopen(pfds
[1], "w");
255 fw3_pr(const char *fmt
, ...)
259 if (fw3_pr_debug
&& pipe_fd
!= stdout
)
262 vfprintf(stderr
, fmt
, args
);
267 vfprintf(pipe_fd
, fmt
, args
);
272 fw3_command_close(void)
274 if (pipe_fd
&& pipe_fd
!= stdout
)
278 waitpid(pipe_pid
, NULL
, 0);
280 signal(SIGPIPE
, SIG_DFL
);
287 fw3_has_table(bool ipv6
, const char *table
)
294 const char *path
= ipv6
295 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
297 if (!(f
= fopen(path
, "r")))
300 while (fgets(line
, sizeof(line
), f
))
302 if (!strncmp(line
, table
, strlen(table
)))
318 lock_fd
= open(FW3_LOCKFILE
, O_CREAT
|O_WRONLY
, S_IRUSR
|S_IWUSR
);
322 warn("Cannot create lock file %s: %s", FW3_LOCKFILE
, strerror(errno
));
326 if (flock(lock_fd
, LOCK_EX
))
328 warn("Cannot acquire exclusive lock: %s", strerror(errno
));
341 if (flock(lock_fd
, LOCK_UN
))
342 warn("Cannot release exclusive lock: %s", strerror(errno
));
345 unlink(FW3_LOCKFILE
);
352 write_defaults_uci(struct uci_context
*ctx
, struct fw3_defaults
*d
,
353 struct uci_package
*dest
)
356 struct uci_ptr ptr
= { .p
= dest
};
358 uci_add_section(ctx
, dest
, "defaults", &ptr
.s
);
360 sprintf(buf
, "%u", d
->flags
[0]);
362 ptr
.option
= "__flags_v4";
366 sprintf(buf
, "%u", d
->flags
[1]);
368 ptr
.option
= "__flags_v6";
374 write_zone_uci(struct uci_context
*ctx
, struct fw3_zone
*z
,
375 struct uci_package
*dest
)
377 struct fw3_device
*dev
;
378 struct fw3_address
*sub
;
379 enum fw3_family fam
= FW3_FAMILY_ANY
;
381 char addr
[INET6_ADDRSTRLEN
];
382 char buf
[INET6_ADDRSTRLEN
* 2 + 2];
385 struct uci_ptr ptr
= { .p
= dest
};
390 if (fw3_no_table(z
->flags
[0]) && !fw3_no_table(z
->flags
[1]))
392 else if (!fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
394 else if (fw3_no_table(z
->flags
[0]) && fw3_no_table(z
->flags
[1]))
397 uci_add_section(ctx
, dest
, "zone", &ptr
.s
);
405 ptr
.option
= "input";
406 ptr
.value
= fw3_flag_names
[z
->policy_input
];
410 ptr
.option
= "output";
411 ptr
.value
= fw3_flag_names
[z
->policy_output
];
415 ptr
.option
= "forward";
416 ptr
.value
= fw3_flag_names
[z
->policy_forward
];
421 ptr
.value
= z
->masq
? "1" : "0";
425 ptr
.option
= "conntrack";
426 ptr
.value
= z
->conntrack
? "1" : "0";
430 ptr
.option
= "mtu_fix";
431 ptr
.value
= z
->mtu_fix
? "1" : "0";
435 ptr
.option
= "custom_chains";
436 ptr
.value
= z
->custom_chains
? "1" : "0";
439 if (fam
!= FW3_FAMILY_ANY
)
442 ptr
.option
= "family";
443 ptr
.value
= fw3_flag_names
[fam
];
448 ptr
.option
= "device";
450 fw3_foreach(dev
, &z
->devices
)
458 p
+= sprintf(p
, "!");
460 p
+= sprintf(p
, "%s", dev
->name
);
463 uci_add_list(ctx
, &ptr
);
467 ptr
.option
= "subnet";
469 fw3_foreach(sub
, &z
->subnets
)
477 p
+= sprintf(p
, "!");
479 inet_ntop(sub
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
480 &sub
->address
.v4
, addr
, sizeof(addr
));
482 p
+= sprintf(p
, "%s", addr
);
486 inet_ntop(sub
->family
== FW3_FAMILY_V4
? AF_INET
: AF_INET6
,
487 &sub
->address2
.v4
, addr
, sizeof(addr
));
489 p
+= sprintf(p
, "-%s", addr
);
493 p
+= sprintf(p
, "/%u", sub
->mask
);
497 uci_add_list(ctx
, &ptr
);
500 sprintf(buf
, "%u", z
->flags
[0]);
502 ptr
.option
= "__flags_v4";
506 sprintf(buf
, "%u", z
->flags
[1]);
508 ptr
.option
= "__flags_v6";
514 fw3_write_statefile(void *state
)
517 struct fw3_state
*s
= state
;
520 struct uci_package
*p
;
522 if (fw3_no_family(s
->defaults
.flags
[0]) &&
523 fw3_no_family(s
->defaults
.flags
[1]))
525 unlink(FW3_STATEFILE
);
529 sf
= fopen(FW3_STATEFILE
, "w+");
533 warn("Cannot create state %s: %s", FW3_STATEFILE
, strerror(errno
));
537 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
538 uci_unload(s
->uci
, p
);
540 uci_import(s
->uci
, sf
, "fw3_state", NULL
, true);
542 if ((p
= uci_lookup_package(s
->uci
, "fw3_state")) != NULL
)
544 write_defaults_uci(s
->uci
, &s
->defaults
, p
);
546 list_for_each_entry(z
, &s
->zones
, list
)
547 write_zone_uci(s
->uci
, z
, p
);
549 uci_export(s
->uci
, sf
, p
, true);
550 uci_unload(s
->uci
, p
);
559 fw3_free_object(void *obj
, const void *opts
)
561 const struct fw3_option
*ol
;
562 struct list_head
*list
, *cur
, *tmp
;
564 for (ol
= opts
; ol
->name
; ol
++)
569 list
= (struct list_head
*)((char *)obj
+ ol
->offset
);
570 list_for_each_safe(cur
, tmp
, list
)
582 fw3_pr_rulespec(int table
, int family
, uint32_t *flags
, uint32_t mask
,
583 const struct fw3_rule_spec
*r
, const char *fmt
, ...)
589 uint32_t f
= flags
? flags
[family
== FW3_FAMILY_V6
] : 0;
594 for (; r
->format
; r
++)
596 if (!fw3_is_family(r
, family
))
599 if (r
->table
!= table
)
602 if ((r
->flag
!= 0) && !hasbit(f
, r
->flag
))
606 vsnprintf(buf
, sizeof(buf
), r
->format
, ap
);
619 fw3_hotplug(bool add
, void *zone
, void *device
)
621 struct fw3_zone
*z
= zone
;
622 struct fw3_device
*d
= device
;
630 warn("Unable to fork(): %s\n", strerror(errno
));
646 setenv("ACTION", add
? "add" : "remove", 1);
647 setenv("ZONE", z
->name
, 1);
648 setenv("INTERFACE", d
->network
->name
, 1);
649 setenv("DEVICE", d
->name
, 1);
651 execl(FW3_HOTPLUG
, FW3_HOTPLUG
, "firewall", NULL
);