Commit Graph

112 Commits

Author SHA1 Message Date
Tim Nordell 320262a7f0
mwan3: Fix packet routing when WAN interface is partially up
This introduces a new concept of "unknown_wan" to mwan3.  The action for
this can be configured in the globals section the default of which is
'none'.  This can be set to 'none', 'default', 'unreachable' or 'blacklist'
switching out the matching ip rule for this match.  This assignment for
a connection is temporary and is re-resolved for each additional
original direction packet through the firewall allowing the unknown WAN
to start resolving once the ifup has finished for the given interface.

An example configuration:

	config globals 'globals'
			option unknown_wan_action 'unreachable'

Prior to this commit, mwan3 had multiple hit spots for packets in the
following order:

 1. Packets are checked to see if they originate from known WAN interfaces
 2. Packets are checked to see if they destined for ipsets defined
 3. Packets are checked against default WAN policies

The WAN list is maintained via hotplug 'ifup'/'ifdown' events and the local
route ipset list is maintained via monitoring the routing table.  This
means that while a WAN interface is brought up, the list for 2 is
updated before the list for 1, since an interface is fully brought up
before the ifup event is fired off.  Additionally, we want to make sure we
don't apply a WAN policy for incoming packets from a WAN interface that
is in the process of being brought up.

We can identify packets that are presumably coming from a WAN interface
we don't recognize yet by eliminating all packets that the source comes
from networks we don't know about in the ipsets that mwan3 manages.  We
have to be careful here to only match the original direction of the
packet flow (e.g. for instance with ICMP, the ping request is in the
ORIGINAL direction, and the response is in the REPLY direction) or else
we might match something we didn't intend to.

By modifying the rule set to the following:

 1. Packets are checked to see if they are in a REPLY direction of flow
 2. Packets are checked to see if they originate from known WAN interfaces
 3. Packets are checked to see if they not sourced from ipsets defined
 4. Packets are checked to see if they destined for ipsets defined
 5. Packets are checked against default WAN policies

If a packet is in the REPLY direction of flow, we definitely don't want
to do any routing table assignments - we only want to do this for the
original direction of traffic flow.  This reduces the amount of rules
parsed within mwan3.

If a packet is not sourced from a defined ipset, this should match any
packet originating from a "default route" upstream.  We do this post the
known WAN interface check since we don't know what mask to apply to this
packet at this time until the 'ifup' has completed.  It's also setup to
reevaluate this decision by clearing this specific mark when a new
packet comes in in the REPLY direction of flow before any subsequent
evaluations.  This allows additional packets for the same connection to
eventually be assigned the appropriate mask once the 'ifup' has
finished.

One easy way to test this out before and after this change is to:

 - Bring down wan (e.g. ifdown wan)
 - Manually bring up WAN
    - This mitigates the firewall rules being added for 1 above, but 2
      is still added since this is monitoring the routing interface
 - Ping the device from a non-local subnet via the WAN interface; leave
   running
 - Observe mark set to ICMP session via conntrack
 - Bring up wan (e.g. ifup wan)
 - Observe mark set to ICMP session from above

Signed-off-by: Tim Nordell <tnordell@airgain.com>
2023-07-03 10:20:06 -05:00
Tim Nordell 8b35da1d1d
mwan3: Fix regression in removal of old WAN interfaces from firewall
In commit 502779755a, the code was
modified to use the mwan3_push_update helper, but the IPTR was not
defined per the address family being utilized.

Signed-off-by: Tim Nordell <tnordell@airgain.com>
2023-07-03 10:19:56 -05:00
Anna Tikhomirova 66257510d9 mwan3: fix addition of iptables rules for mwan3 sticky rules
Addition of iptables rules for mwan3 sticky rules is broken, resulting
in non-working sticky rules. The required parameters for the function
'mwan3_set_sticky_iptables' were passed in the wrong order.

Signed-off-by: Anna Tikhomirova <vamp@vampik.ru>
* Update commit message
* Quoting function arguments
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2023-05-04 11:49:56 +02:00
Florian Eckert a6606bed17 mwan3: change log message on already added route
Only show message on already set route, if debugging is enabled.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-10-17 15:17:07 +02:00
Florian Eckert 87c611abf8 mwan3: fix sticky chain length
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-28 14:02:18 +02:00
Florian Eckert 0b5f09162b mwan3: dump iptables and ipset command for debugging
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert 5a80a5dbfe mwan3: unify error messages
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert 408458a72f mwan3: Split ipsets into separate ipv4 and ipv6 sets
Nft does not directly support ipsets, nft sets must be used instead.
The mwan3 uses ipsets for certain tasks. They can be combinded. So called
an ipset of ipsets. This list type is not available in nft. So that
mwan3 could be ported to nft in the feature, the ipset handling should be
split. So we have for each ipset an iptables rule.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert 502779755a mwan3: use mwan3_push_update function for mwan3_delete_iface_iptables
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert b5c675392d mwan3: remove ipset generation from mwan3_set_general_iptables
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert 9c2abb19e2 mwan3: use sticky ipset generation mwan3_push_update pattern
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert e985f0dcd1 mwan3: do not create dynamic ipset for IPv6 if not supported
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert fedfbd5004 mwan3: do not add mwan3_custom_v6 set if ipv6 is not available
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert 54fad2326e mwan3: use also ipset restore for connected ipv4 sets
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Florian Eckert a49d0953dc mwan3: move command definitions to common.sh
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2022-03-14 09:34:49 +01:00
Daniel Golle cb02b42007
mwan3: use default routes from additional tables
Until now the additional tables listed in gobal 'rt_table_lookup' were
not considered for interfaces.
In order to be able to also use interface-defined routes from tables
other than main, consider also tables listed in 'rt_table_lookup'.
Update version to 2.10.10 as requested by maintainer.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2021-07-05 18:58:52 +01:00
Aaron Goodman 267b65ad9f mwan3: fix regression in ipv6 routing tables
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2021-01-02 21:37:37 -05:00
Florian Eckert 0dbbc58cbc mwan3: fix tab
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-12-22 08:38:30 +01:00
Aaron Goodman 5691ff247f mwan3: fix linkdown routes not being added
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-12-16 09:32:02 +01:00
Aaron Goodman a5f3e6bb6b mwan3: don't call rpcd on 'mwan3 interfaces'
Allow `mwan3 interfaces` to get uptime via an internal function and
thus remove the dependency on rpcd for `mwan3 interface` calls.

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-11-16 11:19:55 -05:00
Aaron Goodman ebfb7fb9d2 mwan3: report an error code on status failure
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-11-10 08:51:07 -05:00
Aaron Goodman b849fc73fb mwan3: add IPKG_INSTROOT to scripts
IPKG_INSTROOT needed for including mwan3 when building images

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-11-10 08:46:55 -05:00
Aaron Goodman 99b4b3a178 mwan3: add logging for ipset errors
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-11-10 08:44:49 -05:00
Florian Eckert 1f1d21ed28 mwan3: make it clearer who initiated the start
It was somewhat opaque how the variable a is questioned. To show this
better the variable is now a string and not a boolean. So you can see
directly what should happen. With a boolean you always have to think
about what it means when 0 or 1 is used.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-11-06 10:41:28 +01:00
Aaron Goodman 59d12598b5 mwan3: use procd for locking everywhere
Replace locks on /var/run/mwan3.lock with locks via procd.

This fixes a deadlock issue where mwan3 stop would have a procd
lock, but a hotplug script would have the /var/run/mwan3.lock

Locking can be removed from mwan3rtmon since:
1) procd will have sent the KILL signal to the process during
shutdown, so it will not add routes to already removed interfaces on
mwan3 shutdown and
2) mwan3rtmon checks if an interface is active based on the
mwan3_iface_in_<IFACE> entry in iptables, and the hotplug script
always adds this before creating the route table and removes it
before deleting the route table

Fixes github issue #13704
(https://github.com/openwrt/packages/issues/13704)
2020-11-06 10:40:57 +01:00
Aaron Goodman 13d22445e4 mwan3: fix rpcd with for routers with no IPv6 support
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-29 23:06:25 -04:00
Aaron Goodman bbbc6127ab mwan3: use helper library for mwan3track
Rather than using a special mwan3 user to manage mwan3track's tracking
packets, this commit implements a small helper library to bind to
device and to set a fwmark so that the tracking packets can be routed
out of the correct interface.

This provides a consistent method for binding to a device rather than
relying on various packages potentially buggy implementations. For
example: #8139 and #12836

This helper issue also allows for more tracking methods to be added
even if they do not have a command line option to bind to device,
such as iperf3 (eg  #13050).

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman fb4a2d99ef mwan3: give warning if ip of incorrect family specified in a rule
also use global IPv4_REGEX environment variable as consistent IPv4 regex

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman 566293d228 mwan3: use procd for mwan3rtmon and mwan3track
start all mwan3mon and mwan3track instances on mwan3 start
if an interface is down when mwan3track starts, it waits
for a signal from the hotplug script to start

procd can then handle stopping all of the scripts when mwan3
is halted

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman 5faa99f3b8 mwan3: fix bug when one interface is a prefix of another
correctly terminate interface status checks with new lines so that
interface status does not get confused when one interface is a prefix
of another interface.

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman c8deccc0e7 mwan3: improvements to route creation
handle creation of routing tables in mwan3rtmon to avoid race
conditions and potentially missing routes

handle ipv6 routes that have expiry

update directly connected ipset when routes are added or deleted

add fall through rules so that the default routing table is not
used if no rule in the interface-specific routing table matches

add option to comply with mwan3 source based routing

get default route parameters from main routing table

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman 27492f64f8 mwan3: use MWAN3TRACK_STATUS_DIR variable throughout
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman b7e26dd431 mwan3: fixup some extra spaces and shellcheck warnings
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-10-16 09:54:48 -04:00
Aaron Goodman c07f5230be mwan3: improve startup performance; version 2.9.0
improve startup and runtime performance by

1) moving common startup procedures out of hotplug script when called
from mwan3 start
2) reducing calls to iptables to check status of rules
3) consolidating iptables updates and updating with iptables-restore
4) do not wait for kill if nothing was killed
5) running interface hotplug scripts in parallel
6) eliminate operations in hotplug script that check status on every
single interface unnecessarily
7) consolidate how mwan3track makes hotplug calls
8) do not restart mwan3track on connected events

This is a significant refactor, but should not result in any breaking
changes or require users to update their configurations.

version bump to 2.9.0

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-08-17 22:02:36 -04:00
Aaron Goodman 39f58789e6 mwan3: use ip monitor route to detect routing changes
use only committed uci changes for updating routing table

use functions.sh functions rather than uci command line tool
to find interfaces for routing table.

consolidate rtmon_ipv4 and rtmon_ipv6 functions into a single function

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-08-15 20:19:56 -04:00
Florian Eckert ab747fe0fb mwan3: remove lock file entirely
Removing the lock file ist not necessary

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-28 06:53:24 +02:00
Florian Eckert b5bd6d757b mwan3: revert: also cleanup lock on mwan3 stop
This reverts commit cde2a77ed3.

Applying this change has shown that it is even quicker to provoke the
race condtition on simultan mwan3 commands execution.
By reversing the change we have the same behaviour as before.

But the race condition on mwan3 execute at the same time still exists.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-28 06:53:24 +02:00
Florian Eckert b0acbf057e mwan3: add online and uptime to detail output
Add also online time value and uptime time value from netifd to the
detail output view.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-20 08:13:34 +02:00
Florian Eckert cde2a77ed3 mwan3: also cleanup lock on mwan3 stop
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-20 08:12:31 +02:00
Florian Eckert bcd13ba95c mwan3: fix rtmon routing table function generation
If the uci option family is not set in the interface section, then there
is no default value set as in the `config_load / config_get` API.
The problem here is that if the family is not set, the default value ipv4
is normaly assumed. But the comparison fails here because the value is empty
and therefore the dedicated routing table for this interface is not compared
with the other routes from the main table and so not updated.

To fix this set the default value for this config option which is`false`
for enabled and `ipv4` for family.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-20 08:11:20 +02:00
Florian Eckert 8e3e6f8dde mwan3: fix IPv6 routing add handling
This fixes routing handling. Introduced with the last version update.
The following message disappears on the shell
when mwan3 is called with 'mwna3 restart`.

`Error: Invalid gateway address.`

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
[aaronjg@stanford.edu: fully unset variable and handle ipv4 as well]
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-19 23:24:13 -04:00
Florian Eckert a796b7a84e mwan3: fix idx calculation
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
[aaronjg@stanford.edu: fix syntax error]
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-19 23:24:13 -04:00
Aaron Goodman 30a46bdc9e mwan3: cleanup duplicate ipv4 and ipv6 logic
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-19 23:24:13 -04:00
Aaron Goodman 702a104f9c mwan3: don't send iptable setup failures to /dev/null
silencing failing rules makes debugging more difficult

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-19 23:18:22 -04:00
Aaron Goodman a0d66d4eeb mwan3: don't try to use ipv6 if not installed
fix issue  #11826

Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-19 23:18:18 -04:00
Aaron Goodman 84a53b7c79 mwan3: be more efficient with sleep after killing trackers
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-16 02:03:21 -04:00
Aaron Goodman da9a626f78 mwan3: don't add single ipv4 to connected list if already covered by a cidr
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
2020-07-16 02:03:21 -04:00
Florian Eckert d0c248a7da mwan3: cleanup function mwan3_create_iface_route
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-15 10:23:47 +02:00
Florian Eckert feae9e5742 mwan3: fix shellcheck warning SC2086
Add double quote to prevent globbing and word splitting where there is
no regression.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-15 10:22:26 +02:00
Florian Eckert c0fdfaa174 mwan3: fix shellcheck warning SC2166
Replace -o boolean check with ||.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2020-07-15 10:21:54 +02:00