@@ -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+
298363def get_all_wan_devices (uci , exclude_aliases = False ):
299364 """
300365 Retrieve all devices associated to the wan zone
0 commit comments