Skip to content

Commit 040f915

Browse files
m-dilorenziTbaile
authored andcommitted
fix(nat): list real devices with bound interfaces in NAT rules device field (#1984)
1 parent 149242e commit 040f915

4 files changed

Lines changed: 198 additions & 6 deletions

File tree

‎packages/ns-api/README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7211,6 +7211,31 @@ Output example:
72117211
}
72127212
```
72137213
7214+
### list-devices
7215+
7216+
List the devices associated to firewall zones, usable to limit a NAT rule to a specific device:
7217+
```
7218+
api-cli ns.nat list-devices
7219+
```
7220+
7221+
The `id` is the device name, the `label` also contains the logical interfaces bound to the device, if any.
7222+
Alias interfaces are resolved to the device of their parent interface, PPPoE and bonding interfaces
7223+
to the device they create (`pppoe-<interface>` and `bond-<interface>`).
7224+
Aliases of aliases are not supported and are not listed.
7225+
7226+
Output example:
7227+
```json
7228+
{
7229+
"devices": [
7230+
{"id": "br-lan", "label": "br-lan (lan)"},
7231+
{"id": "eth1", "label": "eth1 (f1000, al_f1000)"},
7232+
{"id": "pppoe-wan", "label": "pppoe-wan (wan, wan6)"},
7233+
{"id": "tunrw1", "label": "tunrw1"},
7234+
{"id": "wg1", "label": "wg1"}
7235+
]
7236+
}
7237+
```
7238+
72147239
### add-rule
72157240
72167241
Add a new NAT rule:

‎packages/ns-api/files/ns.nat‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ elif cmd == 'call':
4646
print(json.dumps({'rules': firewall.list_nat_rules(e_uci)}))
4747
elif action == 'list-devices':
4848
devices = []
49-
for zone in firewall.list_zones(e_uci).values():
50-
for device in utils.get_all_devices_by_zone(e_uci, zone.get('name')):
51-
devices.append({
52-
'id': device,
53-
'label': utils.get_interface_from_device(e_uci, device) or device
54-
})
49+
for device, interfaces in sorted(utils.get_all_zone_devices_with_interfaces(e_uci).items()):
50+
devices.append({
51+
'id': device,
52+
'label': f'{device} ({", ".join(interfaces)})' if interfaces else device
53+
})
5554
json.dump({
5655
'devices': devices,
5756
}, sys.stdout)

‎packages/python3-nethsec/src/nethsec/utils/__init__.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,71 @@ def get_all_devices_by_zone(uci, zone, exclude_aliases=False):
295295
return list(set(devices))
296296

297297

298+
def get_interface_l3_device(uci, interface):
299+
'''
300+
Retrieve the network device that carries the IP traffic of the given logical interface.
301+
Alias interfaces are resolved to the device of their parent interface, aliases of aliases are not supported.
302+
303+
Arguments:
304+
- uci -- EUci pointer
305+
- interface -- Logical interface name
306+
307+
Returns:
308+
- The device name as a string, None if it can't be resolved
309+
'''
310+
device = uci.get('network', interface, 'device', default='')
311+
# alias interface: use the parent interface
312+
if device.startswith('@'):
313+
interface = device[1:]
314+
device = uci.get('network', interface, 'device', default='')
315+
if uci.get('network', interface, default='') != 'interface':
316+
return None
317+
proto = uci.get('network', interface, 'proto', default='')
318+
# these protocols create a device named after the interface
319+
if proto in ('wireguard', 'xfrm'):
320+
return interface
321+
if proto == 'pppoe':
322+
return f'pppoe-{interface}'
323+
if proto == 'bonding':
324+
return f'bond-{interface}'
325+
# alias of alias
326+
if device.startswith('@'):
327+
return None
328+
return device or uci.get('network', interface, 'name', default='') or None
329+
330+
331+
def get_all_zone_devices_with_interfaces(uci):
332+
'''
333+
Retrieve all devices associated to firewall zones, along with the logical interfaces bound to them
334+
335+
Arguments:
336+
- uci -- EUci pointer
337+
338+
Returns:
339+
- A dictionary with device names as keys and lists of interface names as values,
340+
interfaces named like the device itself are not listed
341+
'''
342+
bound = {}
343+
for section in uci.get('network', list=True, default=[]):
344+
if uci.get('network', section, default='') == 'interface':
345+
device = get_interface_l3_device(uci, section)
346+
if device and device != section:
347+
bound.setdefault(device, []).append(section)
348+
349+
devices = {}
350+
for section in uci.get('firewall', list=True, default=[]):
351+
if uci.get('firewall', section, default='') != 'zone':
352+
continue
353+
for device in uci.get('firewall', section, 'device', list=True, default=[]):
354+
devices[device] = bound.get(device, [])
355+
for network in uci.get('firewall', section, 'network', list=True, default=[]):
356+
device = get_interface_l3_device(uci, network)
357+
if device:
358+
devices[device] = bound.get(device, [])
359+
360+
return devices
361+
362+
298363
def get_all_wan_devices(uci, exclude_aliases=False):
299364
"""
300365
Retrieve all devices associated to the wan zone

‎packages/python3-nethsec/tests/test_utils.py‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,109 @@ def test_get_all_lan_devices(tmp_path):
190190
u = _setup_db(tmp_path)
191191
assert(set(utils.get_all_lan_devices(u)) == set(['vnet3']))
192192

193+
zone_devices_network_db = """
194+
config interface 'lan'
195+
option device 'br-lan'
196+
option proto 'static'
197+
198+
config interface 'f1000'
199+
option device 'eth1'
200+
option proto 'static'
201+
202+
config interface 'al_f1000'
203+
option device '@f1000'
204+
option proto 'static'
205+
206+
config interface 'wan'
207+
option device 'eth2'
208+
option proto 'pppoe'
209+
210+
config interface 'wan6'
211+
option device '@wan'
212+
option proto 'dhcpv6'
213+
214+
config interface 'bond1'
215+
option proto 'bonding'
216+
list slaves 'eth3'
217+
218+
config interface 'wg1'
219+
option proto 'wireguard'
220+
221+
config interface 'ipsec1'
222+
option proto 'xfrm'
223+
224+
config interface 'ns_tun1'
225+
option device 'tuntun1'
226+
option proto 'none'
227+
228+
config interface 'al_al_f1000'
229+
option device '@al_f1000'
230+
231+
config interface 'al_missing'
232+
option device '@missing'
233+
234+
config interface 'loop1'
235+
option device '@loop2'
236+
237+
config interface 'loop2'
238+
option device '@loop1'
239+
"""
240+
241+
zone_devices_firewall_db = """
242+
config zone
243+
option name 'lan'
244+
list network 'lan'
245+
list network 'bond1'
246+
247+
config zone
248+
option name 'wan'
249+
list network 'f1000'
250+
list network 'al_f1000'
251+
list network 'wan'
252+
list network 'wan6'
253+
list network 'loop1'
254+
255+
config zone
256+
option name 'vpn'
257+
list network 'wg1'
258+
list network 'ipsec1'
259+
list device 'tunrw1'
260+
list device 'tuntun1'
261+
"""
262+
263+
def test_get_interface_l3_device(tmp_path):
264+
with tmp_path.joinpath('network').open('w') as fp:
265+
fp.write(zone_devices_network_db)
266+
u = EUci(confdir=tmp_path.as_posix())
267+
assert utils.get_interface_l3_device(u, 'f1000') == 'eth1'
268+
assert utils.get_interface_l3_device(u, 'al_f1000') == 'eth1'
269+
assert utils.get_interface_l3_device(u, 'wan') == 'pppoe-wan'
270+
assert utils.get_interface_l3_device(u, 'wan6') == 'pppoe-wan'
271+
assert utils.get_interface_l3_device(u, 'bond1') == 'bond-bond1'
272+
assert utils.get_interface_l3_device(u, 'wg1') == 'wg1'
273+
assert utils.get_interface_l3_device(u, 'ipsec1') == 'ipsec1'
274+
assert utils.get_interface_l3_device(u, 'al_al_f1000') == None
275+
assert utils.get_interface_l3_device(u, 'al_missing') == None
276+
assert utils.get_interface_l3_device(u, 'loop1') == None
277+
assert utils.get_interface_l3_device(u, 'missing') == None
278+
279+
def test_get_all_zone_devices_with_interfaces(tmp_path):
280+
with tmp_path.joinpath('network').open('w') as fp:
281+
fp.write(zone_devices_network_db)
282+
with tmp_path.joinpath('firewall').open('w') as fp:
283+
fp.write(zone_devices_firewall_db)
284+
u = EUci(confdir=tmp_path.as_posix())
285+
assert utils.get_all_zone_devices_with_interfaces(u) == {
286+
'br-lan': ['lan'],
287+
'bond-bond1': ['bond1'],
288+
'eth1': ['f1000', 'al_f1000'],
289+
'pppoe-wan': ['wan', 'wan6'],
290+
'wg1': [],
291+
'ipsec1': [],
292+
'tunrw1': [],
293+
'tuntun1': ['ns_tun1'],
294+
}
295+
193296
def test_get_random_id():
194297
id1 = utils.get_random_id()
195298
id2 = utils.get_random_id()

0 commit comments

Comments
 (0)