After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 692673 - [review] bg/iptables-sharing-rules-bgo692673 - NM adds duplicate iptables MASQUERADE rules
[review] bg/iptables-sharing-rules-bgo692673 - NM adds duplicate iptables MAS...
Status: RESOLVED FIXED
Product: NetworkManager
Classification: Platform
Component: general
0.9.x
Other Linux
: Normal normal
: ---
Assigned To: NetworkManager maintainer(s)
NetworkManager maintainer(s)
Depends on:
Blocks: nm-review nm-1-2
 
 
Reported: 2013-01-28 05:20 UTC by Popo
Modified: 2016-01-23 09:28 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Popo 2013-01-28 05:20:27 UTC
If you set up a connection sharing to your other devices, eg usb reverse tethering on your mobile phone and restart network manager while the device is connected, network manager keeps adding duplicate packet forwarding rule to iptables..


% sudo iptables -t nat -nvL POSTROUTING      
Chain POSTROUTING (policy ACCEPT 14 packets, 1112 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      *       10.42.0.0/24        !10.42.0.0/24        

% sudo service network-manager restart 
network-manager stop/waiting
network-manager start/running, process 21778

% sudo iptables -t nat -nvL POSTROUTING
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      *       10.42.0.0/24        !10.42.0.0/24        
    2   435 MASQUERADE  all  --  *      *       10.42.0.0/24        !10.42.0.0/24        

% sudo service network-manager restart 
network-manager stop/waiting
network-manager start/running, process 21947

% sudo iptables -t nat -nvL POSTROUTING
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      *       10.42.0.0/24        !10.42.0.0/24        
    1   272 MASQUERADE  all  --  *      *       10.42.0.0/24        !10.42.0.0/24        
    2   435 MASQUERADE  all  --  *      *       10.42.0.0/24        !10.42.0.0/24        


It's desirable if network manager checks for existing rule before adding a new one so it wouldn't clutter up iptables with duplicate rules
Comment 1 Pavel Simerda 2013-05-06 14:00:38 UTC
Could be done by trying to delete the rule first:

-D, --delete chain rule-specification
Comment 2 Popo 2013-05-06 14:53:49 UTC
(In reply to comment #1)
> Could be done by trying to delete the rule first:
> 
> -D, --delete chain rule-specification

Don't tell that to me..add it into nm.
Comment 3 Pavel Simerda 2013-05-06 18:51:20 UTC
(In reply to comment #2)
> Don't tell that to me..add it into nm.

This is why we have bugzilla. For communication.
Comment 4 Popo 2013-05-07 08:24:48 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > Don't tell that to me..add it into nm.
> 
> This is why we have bugzilla. For communication.

Yes and for filing bug report too.
Comment 5 Pavel Simerda 2013-05-07 09:48:05 UTC
(In reply to comment #4)
> Yes and for filing bug report too.

Misunderstanding sorted out off-list.

(In reply to comment #1)
> Could be done by trying to delete the rule first:
> 
> -D, --delete chain rule-specification

Just to be clear, this was a suggestion for NetworkManager code, as we are using the iptables command directly for handling MASQUERADE rules.
Comment 6 Pavel Simerda 2013-10-04 13:43:11 UTC
Update: When looking into the code and re-reading the bug report, I see the --delete is actually being used in the code (otherwise we could not switch back from shared connection). It may look like restating the obvious but...

The deletion is only omitted when stopping NetworkManager. Therefore we must always call nm_act_request_set_shared(act_request, FALSE) for all shared connections when exitting NetworkManager. Also I believe we should always log firewall changes (iptables actions) we perform (I suggest INFO level).
Comment 7 Pavel Simerda 2013-10-04 15:00:12 UTC
Just found a pretty amusing thing about the iptables rule code in NetworkManager:

1) The rules are added in the reverse order for no reason, as they are added with g_slist_append(). That means the slower function is being used only to get the rules in the wrong order. Later before the rules are applied, the list is copied and the new copy is reserved to fix it *once again*.

2) The rules are *always* the same. Therefore the list of rules is not needed at all as they can be easily constructed from the interface name, address and prefix length.
Comment 8 Pavel Simerda 2014-12-23 12:29:26 UTC
Proposing for 1.2.
Comment 9 Beniamino Galvani 2016-01-22 14:49:39 UTC
Pushed branch bg/iptables-sharing-rules-bgo692673.
Comment 10 Dan Williams 2016-01-23 02:52:06 UTC
LGTM; worth backporting to 1.0.x too.