From 507c0513d1766757d969530c51fe7d368354538d Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 May 2022 17:21:23 +0200 Subject: interface-ip: add support for excluding interfaces in host route lookup When adding host routes needed for an interface to communicate, it may be necessary to skip the interface itself, in case it provides a default route. This helps with avoiding accidental loops Signed-off-by: Felix Fietkau --- interface-ip.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'interface-ip.c') diff --git a/interface-ip.c b/interface-ip.c index 54d5fe0..585cb6f 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -257,12 +257,19 @@ interface_ip_find_route_target(struct interface *iface, union if_addr *a, } struct interface * -interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface) +interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface, + bool exclude) { struct device_route *route, *r_next = NULL; bool defaultroute_target = false; union if_addr addr_zero; int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in); + struct interface *exclude_iface = NULL; + + if (exclude) { + exclude_iface = iface; + iface = NULL; + } memset(&addr_zero, 0, sizeof(addr_zero)); if (memcmp(&addr_zero, addr, addrsize) == 0) @@ -278,6 +285,9 @@ interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *if interface_ip_find_route_target(iface, addr, v6, &r_next); } else { vlist_for_each_element(&interfaces, iface, node) { + if (iface == exclude_iface) + continue; + /* look for locally addressable target first */ if (interface_ip_find_addr_target(iface, addr, v6)) return iface; -- cgit v1.2.1