oweals/firewall3.git
5 years agozones: add zone identifying local traffic in raw OUTPUT chain
Hans Dedecker [Thu, 6 Dec 2018 14:44:57 +0000 (15:44 +0100)]
zones: add zone identifying local traffic in raw OUTPUT chain

Add zone chains identifying local generated traffic; either by configuring
a loopback device or as subnet a loopback address; in the raw OUTPUT chain
as local generated traffic is passing this chain.
This allows helpers to be used for local generated traffic.

Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Acked-by: Jo-Philipp Wich <jo@mein.io>
5 years agoutils: Free args in __fw3_command_pipe()
Hauke Mehrtens [Wed, 19 Dec 2018 09:40:07 +0000 (10:40 +0100)]
utils: Free args in __fw3_command_pipe()

args was not freed after leaving this function.

Fixes Coverity issue 1412470 Resource leak

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
Acked-by: Jo-Philipp Wich <jo@mein.io>
5 years agooptions: redirects: Fix possible buffer overflows
Hauke Mehrtens [Wed, 19 Dec 2018 09:34:08 +0000 (10:34 +0100)]
options: redirects: Fix possible buffer overflows

This fixes two possible situations where strncpy() produces a not null
terminated buffer.

Coverity IDs:
1412247 Buffer not null terminated
1412279 Buffer not null terminated

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
Acked-by: Jo-Philipp Wich <jo@mein.io>
5 years agoredirects: properly handle src_dport in SNAT rules
Hans Dedecker [Thu, 6 Dec 2018 17:03:06 +0000 (18:03 +0100)]
redirects: properly handle src_dport in SNAT rules

In case of SNAT rules the src_dport parameter is used both as a rewrite
parameter as well as a matching parameter which is not the expected
behavior.
The latter is caused by port_redir being set to src_dport in case dest_port
parameter is not.
As this logic is in place to mimic the old shell script based firewall
behavior for DNAT only set port_redir in case the redirect rule is
a DNAT rule.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Acked-by: Jo-Philipp Wich <jo@mein.io>
6 years agodefaults: fix check_kmod() function
Jo-Philipp Wich [Mon, 13 Aug 2018 11:58:22 +0000 (13:58 +0200)]
defaults: fix check_kmod() function

Fixes 06fa692 ("defaults: use a generic check_kmod() function")
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agodefaults: use a generic check_kmod() function
Joe Holden [Fri, 13 Jul 2018 15:44:39 +0000 (16:44 +0100)]
defaults: use a generic check_kmod() function

Rather than having functions for check_offloading() et al, replace it with a
generic function.

Signed-off-by: Joe Holden <jwh@zorins.co.uk>
6 years agoAdd support for DSCP matches and target
Jo-Philipp Wich [Fri, 3 Aug 2018 20:29:59 +0000 (22:29 +0200)]
Add support for DSCP matches and target

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agoubus: avoid dumping interface state with NULL message
Jo-Philipp Wich [Thu, 26 Jul 2018 06:02:01 +0000 (08:02 +0200)]
ubus: avoid dumping interface state with NULL message

Invoking ubus methods with NULL message is not supported anymore, so
make sure that network.interface/dump is called with an empty blob buffer
argument.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agofirewall3: make reject types selectable by user
Alin Nastac [Thu, 12 Jul 2018 08:36:02 +0000 (10:36 +0200)]
firewall3: make reject types selectable by user

RFC 6092 recommends in section 3.3.1 that an IPv6 CPE must respond to
unsolicited inbound SYNs with an ICMPv6 Destination Unreachable error
code 1 (Communication with destination administratively prohibited).

Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
Acked-by: Jo-Philipp Wich <jo@mein.io>
6 years agofirewall3: Fix GCC8 warnings by replacing sprintf with snprintf
Rosen Penev [Thu, 21 Jun 2018 23:09:56 +0000 (16:09 -0700)]
firewall3: Fix GCC8 warnings by replacing sprintf with snprintf

error: ‘%u’ directive writing between 1 and 10 bytes into a region of size
between 7 and 11 [-Werror=format-overflow=]
sprintf(buf, "%u-%u", port->port_min, port->port_max);
                 ^~
note: directive argument in the range [0, 2147483647]
sprintf(buf, "%u-%u", port->port_min, port->port_max);
             ^~~~~~~
note: ‘sprintf’ output between 4 and 17 bytes into a destination of size
13
sprintf(buf, "%u-%u", port->port_min, port->port_max);

Signed-off-by: Rosen Penev <rosenp@gmail.com>
6 years agozones: add interface/subnet bound LOG rules
Jo-Philipp Wich [Sat, 19 May 2018 18:36:44 +0000 (20:36 +0200)]
zones: add interface/subnet bound LOG rules

Emit LOG rules bound to the source/destination device or subnet to match the
same traffic handled by the terminal REJECT/DROP rules.

This fixes superflous logging of unrelated traffic.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agooptions: treat time strings as UTC times
Jo-Philipp Wich [Thu, 12 Apr 2018 08:51:51 +0000 (10:51 +0200)]
options: treat time strings as UTC times

When parsing user supplied time strings, calculate an UTC time instant by
substracting the current zone offset from the result of mktime(3), then use
gmtime_r(3) to turn the time_t value back into a sanitized time structure.

This ensures that user supplied dates are not interpreted as local time.

Fixes FS#1483.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agohelpers: fix the set_helper in the rule structure
Pierre Lebleu [Wed, 9 May 2018 07:58:59 +0000 (09:58 +0200)]
helpers: fix the set_helper in the rule structure

The set_helper field has to be set by set_helper and not helper.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
6 years agohelpers.conf: support also tcp in the CT sip helper
Pierre Lebleu [Mon, 7 May 2018 14:17:17 +0000 (16:17 +0200)]
helpers.conf: support also tcp in the CT sip helper

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
6 years agohelpers: make the proto field as a list rather than one option
Pierre Lebleu [Mon, 7 May 2018 14:17:16 +0000 (16:17 +0200)]
helpers: make the proto field as a list rather than one option

The field proto in the struct fw3_cthelper should be implemented
as a list in order to support multiple protocols.

For example, the helper for SIP should be able to support both
TCP and UDP within only one entry in the config file.

config helper
        option name 'sip'
        option description 'SIP VoIP connection tracking'
        option module 'nf_conntrack_sip'
        option family 'any'
        option proto 'tcpudp'
        option port '5060'

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
6 years agodefaults: add support for setting --hw on the xt_FLOWOFFLOAD rule
Felix Fietkau [Thu, 5 Apr 2018 12:44:40 +0000 (14:44 +0200)]
defaults: add support for setting --hw on the xt_FLOWOFFLOAD rule

Enables hardware offload support where supported

Signed-off-by: Felix Fietkau <nbd@nbd.name>
6 years agohelpers.conf: add CT rtsp helper
Hans Dedecker [Tue, 20 Mar 2018 20:45:47 +0000 (21:45 +0100)]
helpers.conf: add CT rtsp helper

Add rtsp conntrack helper listening on IPv4 tcp port 554

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
6 years agoReword rule comments
Jo-Philipp Wich [Tue, 13 Mar 2018 14:54:49 +0000 (15:54 +0100)]
Reword rule comments

Reword various rule comments to be more explicit and also annotate the flow
offloading rule while we're at it.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agodefaults: add support for xt_FLOWOFFLOAD rule
Jo-Philipp Wich [Tue, 13 Mar 2018 14:45:38 +0000 (15:45 +0100)]
defaults: add support for xt_FLOWOFFLOAD rule

Introduce a new defaults section option "flow_offloading" which,
when enabled, causes fw3 to emit a -j FLOWOFFLOAD rule in the
forwarding chain.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agoipsets: add support for specifying entries
Jo-Philipp Wich [Sat, 10 Mar 2018 12:45:44 +0000 (13:45 +0100)]
ipsets: add support for specifying entries

Introduce a new list option "entry" which can be used to specify entries
to add to the ipset, e.g.

    config ipset
      option name test
      ...
      list entry 1.2.3.4,8080
      list entry 5.6.7.8,8081

Also introduce a new option "loadfile" which refers to an external file
containing set entries to add, with one item per line.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agoiptables: fix possible NULL pointer access on constructing rule masks
Jo-Philipp Wich [Fri, 2 Mar 2018 10:36:39 +0000 (11:36 +0100)]
iptables: fix possible NULL pointer access on constructing rule masks

Due to a misplaced parenthesis, rule_mask() may try to access
r->target->userspacesize through a r->target NULL pointer.

Fix this problem by correcting the parenthesis placement in the memset
expression, using the originally intented operator precedence.

Spotted in the cz.nic fork of firewall3.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agozones: allow per-table log control
Stijn Tintel [Mon, 26 Feb 2018 22:06:03 +0000 (23:06 +0100)]
zones: allow per-table log control

When enabling logging for a zone, logging is enabled in the filter and
mangle tables. The log rule in the mangle table enables mtu_fix logging,
which has the tendency to flood logs. Allow per-table log control by
making the log boolean a bit field that can be used to enabled logging
in the filter and/or mangle tables.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
6 years agohelpers: implement explicit CT helper assignment support
Jo-Philipp Wich [Mon, 19 Feb 2018 17:16:26 +0000 (18:16 +0100)]
helpers: implement explicit CT helper assignment support

Implement support for explicit per-zone conntrack helper assignment in
the raw table in order to compensate for the now disabled automatic
helper assignment in recent Linux kernels.

This commit adds, along with the required infrastructure, a new per-
zone uci option "helper" which can be used to tie one or more CT helpers
to a given zone.

For example the following configuration:

    config zone
      option name lan
      option network lan
      list helper ftp
      list helper sip

... will assign the FTP and SIP conntrack helpers as specified in
/usr/share/fw3/helpers.conf to traffic originating from the LAN zone.

Additionally, a new boolean option "auto_helper" has been defined for
both "config defaults" and "config zone" sections, with the former
option overruling the latter.

When the default true "option auto_helper" is set, all available helpers
are automatically attached to each non-masq zone (i.e. "lan" by default).

When one or more "list helper" options are specified, the zone has
masquerading enabled or "auto_helper" is set to false, then the automatic
helper attachment is disabled for the corresponding zone.

Furthermore, this commit introduces support for a new 'HELPER' target in
"config rule" sections, along with "option helper" to match helper traffic
and "option set_helper" to assign CT helpers to a stream.

Finally, "config redirect" sections support "option helper" too now,
which causes fw3 to emit helper setting rules for forwarded DNAT traffic.

When "option helper" is not defined for a redirect and when the global
option "auto_helper" is not disabled, fw3 will pick a suitable helper
based on the destination protocol and port and assign it to DNATed traffic.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agozones: disable masq when resolving of all masq_src or masq_dest items failed
Jo-Philipp Wich [Wed, 7 Jun 2017 14:20:24 +0000 (16:20 +0200)]
zones: disable masq when resolving of all masq_src or masq_dest items failed

Avoid generating 0.0.0.0/0 masquerade rules when resolving of the
corresponding symbolic masq_src or masq_dest value failed.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agooptions: emit an empty address item when resolving networks fails
Jo-Philipp Wich [Wed, 7 Jun 2017 14:18:30 +0000 (16:18 +0200)]
options: emit an empty address item when resolving networks fails

Adjust fw3_parse_network() to emit an empty address item with
.set = false, .resolved = true when we failed to resolve the network
name into any ip address.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
6 years agoubus: let fw3_ubus_address() return the number of resolved addresses
Jo-Philipp Wich [Wed, 7 Jun 2017 13:17:42 +0000 (15:17 +0200)]
ubus: let fw3_ubus_address() return the number of resolved addresses

Change fw3_ubus_address() to return the number of addresses resolved from
the given network name, this will be required to handle failed resolving
in higher layers later on.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agofirewall3: ubus: parse the firewall data within the service itself
Pierre Lebleu [Fri, 3 Nov 2017 16:12:09 +0000 (17:12 +0100)]
firewall3: ubus: parse the firewall data within the service itself

It gives the ability to create firewall rules within the service
itself and independently of the instances.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agooptions: remove stray continue statement
Jo-Philipp Wich [Sat, 27 May 2017 10:29:55 +0000 (12:29 +0200)]
options: remove stray continue statement

The continue statement was mistakingly copy-pasted from other error
handling code.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agooptions: improve handling of negations when parsing space separated values
Jo-Philipp Wich [Fri, 26 May 2017 16:42:05 +0000 (18:42 +0200)]
options: improve handling of negations when parsing space separated values

Improve the space separated list parser to interprete "val1 ! val2"
as ("val1", "!val2") instead of ("val1", "!", "val2").

This corrects parsing of sections like ...

    config rule
        option sec_ip '! 1.1.1.0/24'

... which previously errored out with:

    Warning: Option @rule[0].src_ip has invalid value '!'

Fixes FS#806.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agoiptables: support -i, -o, -s and -d in option extra
Jo-Philipp Wich [Fri, 26 May 2017 16:25:32 +0000 (18:25 +0200)]
iptables: support -i, -o, -s and -d in option extra

Allow "option extra" to specify "-i", "-o", "-s" and "-d" arguments which
previously caused rules to get rejected with "Bad argument" errors.

Partially fixes FS#806.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agoubus: increase ubus network interface dump timeout
Hans Dedecker [Wed, 17 May 2017 12:49:17 +0000 (14:49 +0200)]
ubus: increase ubus network interface dump timeout

When netifd is being stressed (eg doing reconfig using multiple interfaces)
ubus timeouts can be observed when invoking network interface dump.
Increase the ubus timeout to 2000 msec similar to the service ubus timeout.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
7 years agoiptables: add exception handling
Jo-Philipp Wich [Tue, 2 May 2017 13:01:02 +0000 (15:01 +0200)]
iptables: add exception handling

Override libxtables standard error handler to not exit the program but to
longjmp() back to error handling code which is simply skipping the rule.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agofirewall3: add check_snat() function
Pierre Lebleu [Thu, 4 May 2017 08:52:59 +0000 (10:52 +0200)]
firewall3: add check_snat() function

The snat rule check is done by a function to avoid the walking
through the list twice.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: display the section type for UBUS rules
Pierre Lebleu [Thu, 4 May 2017 08:52:58 +0000 (10:52 +0200)]
firewall3: display the section type for UBUS rules

It gives the ability to display the section type for
rules pushed via procd services and netifd interface
firewall data.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: add UBUS support for include scripts
Pierre Lebleu [Thu, 4 May 2017 08:52:57 +0000 (10:52 +0200)]
firewall3: add UBUS support for include scripts

It gives the ability to include scripts via procd
services and netifd interface firewall data.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: add UBUS support for ipset sections
Pierre Lebleu [Thu, 4 May 2017 08:52:56 +0000 (10:52 +0200)]
firewall3: add UBUS support for ipset sections

It gives the ability to create ipset rules via procd
services and netifd interface firewall data.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: add UBUS support for forwarding sections
Pierre Lebleu [Thu, 4 May 2017 08:52:55 +0000 (10:52 +0200)]
firewall3: add UBUS support for forwarding sections

It gives the ability to create forward rules via procd
services and netifd interface firewall data.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: add UBUS support for redirect sections
Pierre Lebleu [Thu, 4 May 2017 08:52:54 +0000 (10:52 +0200)]
firewall3: add UBUS support for redirect sections

It gives the ability to create redirect rules via
procd services and netifd interface firewall data.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: add fw3_attr_parse_name_type() function
Pierre Lebleu [Thu, 4 May 2017 08:52:53 +0000 (10:52 +0200)]
firewall3: add fw3_attr_parse_name_type() function

Move the name and type parsing out of the rule file
in order to make it reusable by others.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: replace warn_rule() by warn_section()
Pierre Lebleu [Thu, 4 May 2017 08:52:52 +0000 (10:52 +0200)]
firewall3: replace warn_rule() by warn_section()

Replace the wan_rule() by warn_section() in order to
make it reusable by the other section type.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agofirewall3: check the return value of fw3_parse_options()
Pierre Lebleu [Thu, 4 May 2017 08:50:56 +0000 (10:50 +0200)]
firewall3: check the return value of fw3_parse_options()

The return value of fw3_parse_options() should be checked.

Signed-off-by: Pierre Lebleu <pme.lebleu@gmail.com>
7 years agobuild: use -Wno-format-truncation instead of -Wno-error=format-truncation
Felix Fietkau [Tue, 9 May 2017 10:42:37 +0000 (12:42 +0200)]
build: use -Wno-format-truncation instead of -Wno-error=format-truncation

Fixes build error with older gcc

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agoutils: replace sprintf use with snprintf to avoid overflows
Felix Fietkau [Thu, 4 May 2017 14:21:17 +0000 (16:21 +0200)]
utils: replace sprintf use with snprintf to avoid overflows

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agobuild: disable the format-truncation warning error to fix gcc 7 build errors
Felix Fietkau [Thu, 4 May 2017 14:17:51 +0000 (16:17 +0200)]
build: disable the format-truncation warning error to fix gcc 7 build errors

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agozones: drop outgoing invalid traffic in masqueraded zones
Jo-Philipp Wich [Sun, 9 Apr 2017 12:35:32 +0000 (14:35 +0200)]
zones: drop outgoing invalid traffic in masqueraded zones

Install conntrack state invalid drop rules to catch outgoing, un-natted
traffic in zones with enabled masquerading.

Also introduce a new option "masq_allow_invalid" it inhibit this new
drop rules.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agorules: fix UCI context in error reporting
Jo-Philipp Wich [Sun, 9 Apr 2017 13:19:52 +0000 (15:19 +0200)]
rules: fix UCI context in error reporting

Commit e678dcb "Add support for netifd-generated rules" broke the UCI
context reporting for rule warnings. Refactor the code to restore this
functionality.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agoubus: fix interface name and proto lookup
Hans Dedecker [Thu, 13 Apr 2017 13:49:04 +0000 (15:49 +0200)]
ubus: fix interface name and proto lookup

Lookup of iface_name and iface_proto in the json data were switched
in fw3_ubus_rules

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
7 years agofirewall3: fix handling of UTC times
Jo-Philipp Wich [Wed, 22 Feb 2017 09:46:58 +0000 (10:46 +0100)]
firewall3: fix handling of UTC times

The --utc parameter is deprecated and UTC times are the default now.
To achieve local time, the --kenreltz param has to be passed instead
so invert the logic and swap --utc with --kerneltz.

Fixes #548.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agoiptables: support xtables API > 11
Jo-Philipp Wich [Tue, 7 Feb 2017 21:10:19 +0000 (22:10 +0100)]
iptables: support xtables API > 11

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agozones: do not check conntrack state in zone_*_dest_ACCEPT chains
Jo-Philipp Wich [Fri, 13 Jan 2017 17:19:43 +0000 (18:19 +0100)]
zones: do not check conntrack state in zone_*_dest_ACCEPT chains

Packets which are merely forwarded by the router and which are neither
involved in any DNAT/SNAT nor originate locally, are considered INVALID
from a conntrack point of view, causing them to get dropped in the
zone_*_dest_ACCEPT chains, since those only allow stream with state NEW
or UNTRACKED.

Remove the ctstate restriction on dest accept chains to properly pass-
through unrelated 3rd party traffic.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
7 years agoglobal: remove automatic notrack rules
Jo-Philipp Wich [Tue, 29 Nov 2016 11:27:42 +0000 (12:27 +0100)]
global: remove automatic notrack rules

With recent Kernel versions and the introduction of the conntrack routing
cache there is no need to maintain performance hacks in userspace anymore,
so simply drop the generation of automatic -j CT --notrack rules for zones.

This also fixes some cases where traffic is not matched for zones that do
not explicitely enforce connection tracking.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoforwards: properly propagate conntrack flag
Jo-Philipp Wich [Mon, 7 Nov 2016 14:27:49 +0000 (15:27 +0100)]
forwards: properly propagate conntrack flag

In the following topology:

    config zone
      option name A

    config zone
      option name B

    config zone
      option name C
      option conntrack 1

    config forwarding
      option src A
      option dest B

    config forwarding
      option src A
      option dest C

... the conntrack flag needs to be propagated into both zones A and B as well.

Since A is connected with C, A will inherit C's conntrack requirement which
means that B will need to inherit the flag as well since it is connected to A.

The current code fails to apply the conntrack requirement flag recursively to
zones, leading to stray NOTRACK rules which break conntrack based traffic
policing.

Change the implementation to iteratively reapply the conntrack fixup logic
until no more zones had been changed in order to ensure that all directly and
indirectly connected zones receive the conntrack requirement flag.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: move includes into iptables.c to avoid kernel header clashes
Jo-Philipp Wich [Sun, 6 Nov 2016 18:14:47 +0000 (19:14 +0100)]
iptables: move includes into iptables.c to avoid kernel header clashes

In order to avoid header clashes and redefinition errors in compilation
units which include iptables.h, move all includes into the iptables.c
file and only provide a forward declaration for struct fw3_ipt_rule.

This allows us to hide all xtables specific direct and indirect includes
in order to only expose a clean interface which does not rely on any kernel
header bits.

Within iptables.c, reshuffle the includes and predeclare some guard defines
to allow compilation on both glibc as well as patched and unpatched musl
systems.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agomusl-compat: avoid kernel header conflicts
Ralph Sennhauser [Sun, 6 Nov 2016 09:59:43 +0000 (10:59 +0100)]
musl-compat: avoid kernel header conflicts

The conflict between Musls net/if.h and linux/if.h is an old well known
one and taken care of by a series of linux-headers patches in OpenWrt.
Since Linux 4.8-rc5 Firewall3 also indirectly pulls in linux/in.h and
linux/in6.h leading to new conflicts.

As Firewall3 is fine with just the libc headers prevent inclusion of the
corresponding kernel headers.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
8 years agoiptables: remove usage of xt_id
Jo-Philipp Wich [Sun, 6 Nov 2016 16:18:36 +0000 (17:18 +0100)]
iptables: remove usage of xt_id

Instead of relying on the nonstandard xt_id match, use the xt_comment match
to tag own rules. Any rule with a comment starting with "!fw3" is considered
to be firewall3 internal.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agomain: make failing ubus connection nonfatal
Jo-Philipp Wich [Sun, 6 Nov 2016 15:58:09 +0000 (16:58 +0100)]
main: make failing ubus connection nonfatal

The ubus network runtime information is not strictly required to use firewall3,
so make a failing ubus connection nonfatal.

This allows testing and running firewall3 on an ordinary desktop linux system,
given an appropriate configuration which uses "option device" instead of
"option network" for zone declarations.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: rework extension loader
Jo-Philipp Wich [Sun, 6 Nov 2016 15:47:23 +0000 (16:47 +0100)]
iptables: rework extension loader

Now that we wrap xtables_register_match() and xtables_register_target() we do
not need to load the extensions ourselves anymore since there is no need to
keep the library handles for dlclose().

Switch to libxtables own loader by invoking xtables_find_match() and
xtables_find_target() with XTF_TRY_LOAD .

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: declare _GNU_SOURCE to define RTLD_NEXT
Jo-Philipp Wich [Sun, 6 Nov 2016 14:20:23 +0000 (15:20 +0100)]
iptables: declare _GNU_SOURCE to define RTLD_NEXT

This is required to build firewall3 on non-musl systems.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: optional loading of static extensions
Ralph Sennhauser [Sun, 6 Nov 2016 07:33:37 +0000 (08:33 +0100)]
iptables: optional loading of static extensions

Make loading of static extensions optional to support vanilla iptables
in it's default configuration by setting DISABLE_STATIC_EXTENSIONS
instead of hackery.

In case iptables is built with --disable-static libext.a, libext4.a and
libext6.a which OpenWrt installs in the form of libiptext.so,
libiptext4.so, libiptext6.so to save a couple more bytes are of no use
or non-existent one could say. So this commit avoids requiring a
tampered with iptables.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
[Jo-Philipp Wich: stub init_extensions*() instead to reduce amount of ifdefs]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: fix loading standard target
Ralph Sennhauser [Sat, 5 Nov 2016 16:40:38 +0000 (17:40 +0100)]
iptables: fix loading standard target

In case iptables is built with --disable-static xt_standard needs to be
loaded just like the other extensions.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
[Jo-Philipp Wich: minor code style change to if/return instead of if/else]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: add support for version 1.6.0
Ralph Sennhauser [Fri, 4 Nov 2016 13:41:10 +0000 (14:41 +0100)]
iptables: add support for version 1.6.0

Account for the struct xtables_globals change and add API version 11 to
the supported APIs.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
8 years agozones: properly handle multiple masq_src / masq_dest negations (FS#248)
Jo-Philipp Wich [Tue, 1 Nov 2016 22:19:24 +0000 (23:19 +0100)]
zones: properly handle multiple masq_src / masq_dest negations (FS#248)

Properly implement masquerade exceptions by using -j RETURN rules to jump out
of the postrouting container chain and only emit the permutated -j MASQUERADE
rules for non-negated addresses.

Fixes FD#248.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoiptables: use different approach for managing loadable extensions
Jo-Philipp Wich [Tue, 9 Aug 2016 09:00:45 +0000 (11:00 +0200)]
iptables: use different approach for managing loadable extensions

Since musl libc does not support unloading libraries via dlclose() and since
we should not explicitely call library constructors we need to use an
alternative approach to track the match registrations performed by iptables
shared objects.

This commit changes the iptables glue code to keep a global registry of non-
builtin matches and targets.

We implement the bookkeeping by intercepting xtables_register_match() and
xtables_register_target() calls in order to record any extension registration
attempt performed by a loadable iptables library.

The code subsequently uses the global list of dynamically loaded extensions
to re-register dynamic matches and targets for each address family / table
combination.

As a consequence we can get rid of the lib vector in the iptables handle
and remove the dlclose() handling entirely. This simplifies the
load_extension() as well.

Fixes FS#31.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoutils.h: Avoid name clashes for setbit/delbit/hasbit
Florian Fainelli [Fri, 2 Sep 2016 02:10:15 +0000 (19:10 -0700)]
utils.h: Avoid name clashes for setbit/delbit/hasbit

Rename to fw3_{set,del,has}bit to avoid name clashes with sys/param.h:

/opt/toolchains/stbgcc-4.8-1.5/arm-linux-gnueabihf/sys-root/usr/include/sys/param.h:80:0: note: this is the location of the previous definition
 #define setbit(a,i)     ((a)[(i)/NBBY] |= 1<<((i)%NBBY))

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
8 years agozones: allow untracked traffic as well
Jo-Philipp Wich [Mon, 8 Aug 2016 14:48:47 +0000 (16:48 +0200)]
zones: allow untracked traffic as well

Now that we only allow ctstate NEW traffic by default we also need to
whitelist traffic explicitely marked by --notrack.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agodefaults: disable drop_invalid by default
Jo-Philipp Wich [Mon, 8 Aug 2016 14:25:37 +0000 (16:25 +0200)]
defaults: disable drop_invalid by default

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agozones: restrict default ACCEPT rules to NEW ctstate
Jo-Philipp Wich [Mon, 8 Aug 2016 13:52:28 +0000 (15:52 +0200)]
zones: restrict default ACCEPT rules to NEW ctstate

Restrict the per-zone default accept rules to only accept streams with
conntrack state NEW when drop_invalid is disabled.

This commit hardens the firewall in order to allow disabling drop_invalid
by default since ctstate INVALID also matches desired traffic like IPv6
neighbour discovery messages.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agocmake: Find uci.h
Florian Fainelli [Mon, 11 Jul 2016 19:07:08 +0000 (12:07 -0700)]
cmake: Find uci.h

Add a CMake FIND_PATH and INCLUDE_DIRECTORIES searching for uci.h. Some
external toolchains which do not include standard locations would fail
to find the header otherwise.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
8 years agotreewide: replace jow@openwrt.org with jo@mein.io
Jo-Philipp Wich [Tue, 7 Jun 2016 12:13:25 +0000 (14:13 +0200)]
treewide: replace jow@openwrt.org with jo@mein.io

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
8 years agoload running state after lock is acquired
Alin Năstac [Fri, 29 Apr 2016 13:00:01 +0000 (15:00 +0200)]
load running state after lock is acquired

When running "/etc/init.d/firewall reload & fw3 -q restart", the
fw3 instance that handle the reload might try to read the running
state after firewall was stopped by the fw3 instance that does the
restarting. Since a NULL run_state will transform reload operation in
start operation, the resulted iptables chains will contain duplicate
sets of rules.

8 years agoset mark for locally generated traffic in OUTPUT chain
Daniel Golle [Thu, 28 Apr 2016 12:25:02 +0000 (14:25 +0200)]
set mark for locally generated traffic in OUTPUT chain

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
8 years agodefaults.c: remove toplevel_rule struct
Alexandru Ardelean [Wed, 27 Apr 2016 06:16:51 +0000 (09:16 +0300)]
defaults.c: remove toplevel_rule struct

Since commit 60f1444 , this struct is no longer used.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
8 years agodefaults: emit ctstate INVALID drop rules by default
Jo-Philipp Wich [Fri, 29 Jan 2016 17:22:34 +0000 (18:22 +0100)]
defaults: emit ctstate INVALID drop rules by default

Enable the creation of state invalid catch rules by default to prevent
unnatted traffic from leaking onto the wan.

Fixes OpenWrt ticket #21738.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
8 years agoiptables: fix inversion flags
Len White [Fri, 29 Jan 2016 07:10:44 +0000 (02:10 -0500)]
iptables: fix inversion flags

Signed-off-by: Len White <lwhite@nrw.ca>
8 years agoRemove commented code
Jo-Philipp Wich [Sun, 24 Jan 2016 17:07:26 +0000 (18:07 +0100)]
Remove commented code

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
8 years agoUse xt_id match to track own rules
Jo-Philipp Wich [Sun, 24 Jan 2016 16:43:30 +0000 (17:43 +0100)]
Use xt_id match to track own rules

Instead of relying on the delegate_* chains to isolate own toplevel
rules from user supplied ones, use the xt_id match to attach a magic
value to fw3 rules which allows selective cleanup regardless of the
container chain.

Also add an experimental "fw3 gc" call to garbage collect empty chains.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoredirects: only emit REDIRECT rules if dest_ip is unset
Jo-Philipp Wich [Tue, 26 May 2015 12:50:21 +0000 (14:50 +0200)]
redirects: only emit REDIRECT rules if dest_ip is unset

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoRework match initialization
Jo-Philipp Wich [Tue, 26 May 2015 10:29:52 +0000 (12:29 +0200)]
Rework match initialization

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoLink libext dynamically
Jo-Philipp Wich [Tue, 5 May 2015 15:21:22 +0000 (17:21 +0200)]
Link libext dynamically

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoiptables: initialize multiport match
Jo-Philipp Wich [Fri, 22 May 2015 18:18:09 +0000 (20:18 +0200)]
iptables: initialize multiport match

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoubus: allow proto handlers to override device in announced rules
Jo-Philipp Wich [Thu, 21 May 2015 13:04:11 +0000 (15:04 +0200)]
ubus: allow proto handlers to override device in announced rules

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoubus: print rule name when reporting errors
Jo-Philipp Wich [Fri, 17 Apr 2015 14:12:14 +0000 (16:12 +0200)]
ubus: print rule name when reporting errors

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agoubus: store rule origin as comment
Jo-Philipp Wich [Fri, 17 Apr 2015 14:06:39 +0000 (16:06 +0200)]
ubus: store rule origin as comment

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agofirewall3: fix null pointer access when no target is present
Hans Dedecker [Wed, 25 Feb 2015 15:00:56 +0000 (16:00 +0100)]
firewall3: fix null pointer access when no target is present

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
9 years agoredirects: fix possible null pointer access
Jo-Philipp Wich [Tue, 13 Jan 2015 11:46:37 +0000 (12:46 +0100)]
redirects: fix possible null pointer access

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
9 years agofirewall3: fix left shift on 64 bit systems in fw3_bitlen2netmask
Ulrich Weber [Mon, 5 Jan 2015 14:58:34 +0000 (15:58 +0100)]
firewall3: fix left shift on 64 bit systems in fw3_bitlen2netmask

otherwise 0.0.0.0/0 is set as 0.0.0.0/255.255.255.255 on x86_64

Signed-off-by: Ulrich Weber <uw@ocedo.com>
9 years agoredirects: respect src_dip option for reflection rules
Jo-Philipp Wich [Thu, 8 Jan 2015 13:17:16 +0000 (14:17 +0100)]
redirects: respect src_dip option for reflection rules

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agooptions: allow '*' as value for protocols and families
Jo-Philipp Wich [Fri, 19 Sep 2014 18:09:19 +0000 (20:09 +0200)]
options: allow '*' as value for protocols and families

No functional change, just a little bit of consistency with src / dest
specifiers where '*' means 'any' or 'all'. To follow the principle of
least surprise, allow the some for family and protocol options.

  option proto '*' is equivalent to option proto 'all'
  option family '*' is equivalent to option family 'any'

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoutils: rework fw3_bitlen2netmask() IPv6 mask calculation
Jo-Philipp Wich [Thu, 18 Sep 2014 10:09:12 +0000 (12:09 +0200)]
utils: rework fw3_bitlen2netmask() IPv6 mask calculation

The previous code wrote beyound the end of the destination buffer under
certain circumstances, causing possible heap corruptions.

Rewrite the IPv6 mask calculation code to use a safe byte-wise assignment
loop instead of two memset() calls and one byte assignment in the middle.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoredirect: emit -j REDIRECT rules for local port forwards
Jo-Philipp Wich [Wed, 17 Sep 2014 21:57:39 +0000 (23:57 +0200)]
redirect: emit -j REDIRECT rules for local port forwards

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoutils: fix invalid memory access in fw3_bitlen2netmask()
Jo-Philipp Wich [Wed, 17 Sep 2014 17:49:53 +0000 (19:49 +0200)]
utils: fix invalid memory access in fw3_bitlen2netmask()

When fw3_bitlen2netmask() is invoked with a bit length of 128, the next
byte after the end of struct in6_addr is errorneously zeroed, leading to
a heap corruption on at least x86_64 with uclibc and possibly others.

Prevent the invalid writes by explicitely testing for a bit count < 128.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoutils: ifa_addr may be NULL, skip such entries
Jo-Philipp Wich [Mon, 11 Aug 2014 17:42:59 +0000 (19:42 +0200)]
utils: ifa_addr may be NULL, skip such entries

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoSelectively flush conntrack
Jo-Philipp Wich [Wed, 6 Aug 2014 17:00:18 +0000 (19:00 +0200)]
Selectively flush conntrack

Record active IP addresses in firewall state file and trigger
conntrack flush for changed IP addresses on firewall reload.

Additionally trigger a complete flush on the first firewall
start in order to clear out streams which might have bypassed
the masquerading rules.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agozones: make forward policy destination bound
Jo-Philipp Wich [Mon, 21 Jul 2014 14:06:04 +0000 (16:06 +0200)]
zones: make forward policy destination bound

The zone forwarding policy was installed source bound which resulted
in zones with forward accept policy to allow traffic anywhere while
only traffic between the zones network is supposed to be allowed in this
case.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agooptions: fix logic flaw when parsing ipaddr/mask notation
Jo-Philipp Wich [Sat, 19 Jul 2014 12:42:47 +0000 (14:42 +0200)]
options: fix logic flaw when parsing ipaddr/mask notation

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoUse netmasks instead of prefix lengths internally
Jo-Philipp Wich [Fri, 18 Jul 2014 13:43:56 +0000 (15:43 +0200)]
Use netmasks instead of prefix lengths internally

Iptables supports using non-continuous netmasks like FFFF::FFFF which would
match the first and last 16bit of an IPv6 address while ignoring the parts
in between which is useful fordeclaring rules targeting hosts on rotating
prefixes.

Instead of storing parsed netmasks as bitcount internally, use a full mask
which is passed to iptables as-is.

Also support a new shorthand notation "addr/-N" which will construct a mask
that matches the *last* N bits of an address - useful for matching the host
part only of an IPv4 address, e.g.

  option dest_ip '::c23f:eff:fe7a:a094/-64'

This will convert to a netmask of "::ffff:ffff:ffff:ffff".

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoubus: handle attribute access after NULL check in parse_subnets()
Jo-Philipp Wich [Thu, 10 Jul 2014 16:38:35 +0000 (18:38 +0200)]
ubus: handle attribute access after NULL check in parse_subnets()

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoubus: fix fw3_ubus_address()
Jo-Philipp Wich [Thu, 10 Jul 2014 09:15:03 +0000 (11:15 +0200)]
ubus: fix fw3_ubus_address()

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agoubus: fix fw3_ubus_device() to only return a pointer if a device was found
Jo-Philipp Wich [Thu, 10 Jul 2014 09:03:13 +0000 (11:03 +0200)]
ubus: fix fw3_ubus_device() to only return a pointer if a device was found

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
10 years agooptions: fix fw3_parse_network() when destination pointer is not a list
Jo-Philipp Wich [Thu, 3 Jul 2014 08:52:48 +0000 (10:52 +0200)]
options: fix fw3_parse_network() when destination pointer is not a list

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>