Dynamic state reset function improperly implemented
Important notices
Before you add a new report, we ask you kindly to acknowledge the following:
- [x] I have read the contributing guide lines at https://github.com/opnsense/core/blob/master/CONTRIBUTING.md
- [x] I am convinced that my issue is new after having checked both open and closed issues at https://github.com/opnsense/core/issues?q=is%3Aissue
Describe the bug
In the past a new option "Dynamic state reset" (Firewall -> Settings -> Advanced) has been introduced due to issues with dynamically changing IP addresses of dial-up connections. The issue has been observed because of broken SIP connections to the VoIP provider after the public WAN IP address has changed. A deeper investigation identified outdated entries in the NAT table (and possibly corresponding firewall entries based on the WAN IP address used in aliases) to be the cause. My proposal, only resetting the WAN related entries in state tables was not implemented in the way I intended. The current implementation has side effects. The mentioned option performs a global state reset and is affecting all connections even belonging to other interfaces. I already pointed out in the past that this solution will break other things (e.g. localhost based IPC).
Time has passed since this discussion and now a problem regarding "unbound" DNS occurred which was traced back to the improper state reset implementation I had mentioned before. A more detailed analysis of Opnsense version 27.7.3 showed that function calls for resetting states can be found at several lines of the code.
Hints (OPNsense 21.7.3_3-amd64:):
-
rc.newwanip:164: mwexec('/sbin/pfctl -k ' . $cacheip);
- File has already been deleted before command is executed and in case of interface is missing (e.g. link loss)
-
inc/interfaces.inc:1105: mwexecf('/sbin/pfctl -i %s -Fs', $dev);
- in general command has no effect due to NAT states are not bound to an interface
- it looks like
$devis invalid (interface not in array) in case of interface is missing (e.g. link loss)
-
inc/filter.inc:565: mwexec('/sbin/pfctl -Fs');
- in general command has no effect due to NAT states are not bound to an interface
To Reproduce
Force a dynamic IP change on the PPPoE WAN interface with the following two commands and enabled state reset function (Dynamic state reset: Firewall -> Settings -> Advanced). Active LAN and localhost connections will break which must not occur.
$ kill -s USR2 `pgrep mpd5`
$ kill -s USR1 `pgrep mpd5`
Expected behavior
These two commands should be sufficient during a dynamic IP change on the WAN interface to reset all invalid states whereas $cacheip is the old outdated IPv4 address. We need to consider that the new IPv4 address differs from the old one, otherwise a state reset is not needed:
mwexec('/sbin/pfctl -k ' . $cacheip);
mwexec('/sbin/pfctl -k 0.0.0.0/0 -k ' . $cacheip);
Additional context
Example showing what happens in case of simulated dynamic IP change on PPPoE WAN interface with disabled global state reset feature: OpnSense Forum
Environment
Software version used and hardware type if relevant, e.g.:
OPNsense 21.7.3_3-amd64.
File has already been deleted before command is executed and in case of interface is missing (e.g. link loss)
There is a log message for this to confirm and I am expecting the code to do the right thing since at least a couple of years looking at the guards around $cacheip and $cacheip_file handing.
https://github.com/opnsense/core/blob/db231b34c38742cf585441648f0346970b6ee55b/src/etc/rc.newwanip#L163
in general command has no effect due to NAT states are not bound to an interface
I can't agree with that assessment since there are likely states being killed even if it's not NAT states.
it looks like $dev is invalid (interface not in array) in case of interface is missing (e.g. link loss)
Can't agree with this for further code reasons.
https://github.com/opnsense/core/blob/db231b34c38742cf585441648f0346970b6ee55b/src/etc/inc/util.inc#L1033-L1036
in general command has no effect due to NAT states are not bound to an interface
Same as above.
Cheers, Franco
I believe that we need to take a closer look at various aspects of this issue.
There is a log message for this to confirm and I am expecting the code to do the right thing since at least a couple of years looking at the guards around $cacheip and $cacheip_file handing.
We do have a problem when the $cacheip_file no longer exists in /var/db. In this case code line 162 in rc.newwanip will return false. Hence, code lines 163-164 won't be executed at all and we are left with stale entries in the state table.
How can we get into this situation with missing $cacheip_file?
The $cacheip_file will be deleted by interface_bring_down function in interfaces.inc once the pppoe interface will be removed (maybe due to pulling out the WAN network cable or when clicking pppoe disconnect in the GUI).
One could assume that the interface_bring_down function would kill old states that belong to the corresponding interface. Unfortunately, that won't happen due to multiple logic errors.
in general command has no effect due to NAT states are not bound to an interface
Somova is right. States are floating by default (i. e. not bound to interfaces) unless you have enabled "Bind states to interface" under Firewall -> Settings -> Advanced.
That no states are bound to the PPPoE interface can be easily verified with the following command:
pfctl -i pppoe0 -s states
Hence, pfctl command in code line 1105 of interfaces.inc won't kill any stale states because there are no states bound to the interface.
But there is also another major problem - regardless of whether the states are bound to an interface or not. In code lines 1103-1106 of interfaces.inc we iterate through array $pfctlflush. The problem is that $pfctlflush is emtpy!
How can we get into this situation with empty array $pfctlflush?
The logic error is that we kill the mpd5 daemon in code line 1050 of interfaces.inc. This automatically removes the pppoe0 interface from the interface list. Hence, does_interface_exist($realif) in code line 1067 of interfaces.inc will return false and array $pfctlflush won't get filled.
The result is that we try to iterate through an empty array and that states can't be flushed.
3. inc/filter.inc:565: mwexec('/sbin/pfctl -Fs'); * in general command has no effect due to NAT states are not bound to an interface
I don't see any problems with no. 3. This has nothing to do with interface bound states. This command will only be triggered when "Reset all states when a dynamic IP address changes" under Firewall -> Settings -> Advanced is checked. pfctl -Fs will just kill ALL states.
mwexec('/sbin/pfctl -k ' . $cacheip); mwexec('/sbin/pfctl -k 0.0.0.0/0 -k ' . $cacheip);
Agreed. To sufficiently kill all stale states we need both commands.
@glasid I'm curious why interface_bring_down() is allegedly executed here trashing the (cache)ip files. Can you skim the system log for "DEVD Ethernet detached event" messages?
Thanks, Franco
2021-11-23T12:08:58 | opnsense[66034] | /usr/local/etc/rc.linkup: DEVD Ethernet detached event for wan
2021-11-23T12:08:57 | kernel | ix0_vlan7: link state changed to DOWN
2021-11-23T12:08:57 | kernel | ix0: link state changed to DOWN
-> mpd5 killed -> pppoe0 interface gone -> stale states remain in the state table (as $pfctlarray is empty)
Maybe all necessary arrays need to be created before killing mpd5.
However, we still have a problem in code line 1105:
mwexecf('/sbin/pfctl -i %s -Fs', $dev);
States are floating by default, i. e. states are not bound to the interface. Hence, even if $dev contains a valid interface, pfctl won't kill stale states.
Let's focus on cacheip hit and miss with built-in state kill via 028302ae74c5 first. Does that work as expected then?
FWIW, I'm seeing pfSense use the same pfctl -i commands so either they have a patch or they do work to a certain degree. I'm not sure if spending core time for research on this subject is a good idea as it looks like a time sink. I'd rather add the -i to see if it keeps working with the state on 028302a, because of e.g.:
https://github.com/opnsense/core/blob/028302ae74c5bb8669d8ac123db27dab854392a5/src/opnsense/scripts/interfaces/ppp-linkup.sh#L9-L10
3. inc/filter.inc:565: mwexec('/sbin/pfctl -Fs'); * in general command has no effect due to NAT states are not bound to an interfaceI don't see any problems with no. 3. This has nothing to do with interface bound states. This command will only be triggered when "Reset all states when a dynamic IP address changes" under Firewall -> Settings -> Advanced is checked. pfctl -Fs will just kill ALL states.
You're right. It was a mistake by me. This line of code does not care about states are bond to an interface or not.
2021-11-23T12:08:58 | opnsense[66034] | /usr/local/etc/rc.linkup: DEVD Ethernet detached event for wan 2021-11-23T12:08:57 | kernel | ix0_vlan7: link state changed to DOWN 2021-11-23T12:08:57 | kernel | ix0: link state changed to DOWN-> mpd5 killed -> pppoe0 interface gone -> stale states remain in the state table (as $pfctlarray is empty)
Maybe all necessary arrays need to be created before killing mpd5.
I don't know why the mpd5 daemon process will be killed if the pppoe connection gets closed. There is no need for it (the same goes for shutting down the pppoe interface or killing the netgraph). If there is only one existing pppoe connection (which is normally the case), this can be controlled by sending SIGUSR1 and SIGUSR2 system signals to the process. Alternatively, mpd5 daemon can provide a command line interface.
Let's focus on cacheip hit and miss with built-in state kill via 028302a first. Does that work as expected then?
It would be really nice to get feedback on this otherwise I'm going to stop entertaining it. There's no point adding tickets and huge blobs of talk without providing hard evidence either as prerequisite or commit feedback. Also, with a loose scope like this ticket saying that there's a bug because Unbound bugs out pivoting to all state killing being supposedly buggy doesn't even help with Unbound where we haven't gotten any patches in because of the recent forum proposal blocked by @somova, but maybe that's just my partial view. Am I missing something obvious?
I'm surprised that people should be able to make a statement in less than one day whether and how they can test any patches. I can try to test the patch on the weekend, but cannot ensure to simulate a link loss by pulling the cable. On weekdays testing this patch is to risky because availability of the internet connection is of highest priority for the business. So, please let people enough time and consider a lot of people support this project in their free time. Everybody has to decide for himself if he likes to contribute or not.
Sure, everybody on their own time. What I don't like is the hit-and-miss strategy deployed here coupled with a lack of regard for precedent, code history and a general lack of evidence that things don't work a certain way when they appear to be working for the bulk of users regardless.
The comment https://github.com/opnsense/core/issues/5367#issuecomment-975827008 went without feedback so I don't even know what is what. And I'm trying to pull these facts for you in my free community time as well. I always have.
Cheers, Franco
It would be really nice to get feedback
I have some issues applying the patch.
Fetched 028302a via https://github.com/opnsense/core
1 out of 1 hunks failed while patching etc/rc.newwanip
Looks like the code lines of your rc.newwanip file are slightly shifted. The patch inserts a new code line before line number 167. With my rc.newwanip file (OPNsense 21.7.5-amd64) I would expect a code line insertion before line number 164.
Seems to be that I would have to apply e729542 and ba70f14 before. Any risk that I will break anything?
@glasid ok, here's a backport on top of 21.7.5 that should apply: 66cc6d0b1
Cheers, Franco
Patch looks good.
rc.newwanip now successfully kills all old states as $cacheip_file is retained in the filesystem.
@glasid thanks, very helpful. ~~Can you try a8522b7243 on top of the other one? it should patch without a backport now that the lines are the same. I just want to bring the code in line with the other part of the system that seems to work ok~~
@glasid nevermind about that second test request. we reverted that one.
The patch 66cc6d0 applied without any problems and looks good. If have tested the patch with "Dynamic state reset" function disabled. In case the IPv4 address changes after pppoe reconnect, all related states in the state table get killed. If the IPv4 address does not change, all related states will remain in the state table. So, the patch works as intended :-).
But there is still a minor issue which still can lead to connection issues no matter what how we deal with resetting states in the state table. Some Opnsense services are directly bound to the WAN interface and its public IPv4 address. These services are still trying to use the outdated IPv4 address, because the services pass the NAT state table. I have observed this issue with the squid proxy. Presumably, regarding squid this is a minor issue and does only affects new connections but needs further investigation.
Due to the low dependency of the local sockets issue (as far as I know) and the good improvement we should mark this ticket as solved and raise a new one to discuss and solve the socket issue in the future.
Thanks a lot to all contributers.
I saw that you removed state killing on gateway failure in rc.filter_configure ( #5045), which appears a good idea, since it entirely flushed all states and disrupted established connections on other WANs (our PBX communicating on WAN1 lost all voice communication, if WAN2 failed).
For VOIP failover (using gateway groups with WAN failover configuration), however, I assume, that selective killing (only of states associated with the IP of the failing WAN interface, similarly as discussed here for rc.newwanip) is needed to work. Has this been considered?
Best regards and thank you for your excellent work! Walter
@wkochFPV
Hi Walter,
The default is still here https://github.com/opnsense/core/blob/67910fbfeab0e1754507a6b7f0eaafdf1ece319f/src/etc/rc.newwanip#L156-L160 so that should be fine. You can still get the state killing equivalent of gateway down with the 'ip_change_kill_states' switch above, but I think eventually we would remove this too if the default is sufficient and to stop meddling with multi-wan states.
Cheers, Franco