diff options
author | Felix Fietkau <nbd@openwrt.org> | 2012-05-20 01:40:10 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2012-05-20 01:40:10 +0200 |
commit | 575b1fe1bc8f15e65d89a0905e1640277c17fadc (patch) | |
tree | 2862c096b2c5d6cc68cab57eed6a42141506e336 /interface-ip.c | |
parent | 0079ac42fcff0e703467c188c37db902ee8cead8 (diff) | |
download | netifd-575b1fe1bc8f15e65d89a0905e1640277c17fadc.tar.gz |
replace the kernel's implicit network routes if the metric is set
Diffstat (limited to 'interface-ip.c')
-rw-r--r-- | interface-ip.c | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/interface-ip.c b/interface-ip.c index b681b81..0558b6c 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -50,23 +50,33 @@ const struct config_param_list route_attr_list = { .params = route_attr, }; -static bool -match_if_addr(union if_addr *a1, union if_addr *a2, int mask) +static void +clear_if_addr(union if_addr *a, int mask) { - uint8_t *p1, *p2; int m_bytes = (mask + 7) / 8; uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1; + uint8_t *p = (uint8_t *) a; + + if (m_bytes < sizeof(a)) + memset(p + m_bytes, 0, sizeof(a) - m_bytes); + + p[m_bytes - 1] &= ~m_clear; +} - p1 = alloca(m_bytes); - p2 = alloca(m_bytes); +static bool +match_if_addr(union if_addr *a1, union if_addr *a2, int mask) +{ + union if_addr *p1, *p2; - memcpy(p1, a1, m_bytes); - memcpy(p2, a2, m_bytes); + p1 = alloca(sizeof(*a1)); + p2 = alloca(sizeof(*a2)); - p1[m_bytes - 1] &= ~m_clear; - p2[m_bytes - 1] &= ~m_clear; + memcpy(p1, a1, sizeof(*a1)); + clear_if_addr(p1, mask); + memcpy(p2, a2, sizeof(*a2)); + clear_if_addr(p2, mask); - return !memcmp(p1, p2, m_bytes); + return !memcmp(p1, p2, sizeof(*p1)); } static bool @@ -252,6 +262,7 @@ interface_update_proto_addr(struct vlist_tree *tree, struct device *dev; struct device_addr *a_new = NULL, *a_old = NULL; bool keep = false; + struct device_route *route; ip = container_of(tree, struct interface_ip_settings, addr); iface = ip->iface; @@ -292,10 +303,28 @@ interface_update_proto_addr(struct vlist_tree *tree, } if (node_new) { - if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) - system_add_address(dev, a_new); a_new->enabled = true; + if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) { + system_add_address(dev, a_new); + if (iface->metric) + goto replace_route; + } } + return; + +replace_route: + route = calloc(1, sizeof(*route)); + route->iface = iface; + route->flags = a_new->flags | DEVADDR_KERNEL; + route->mask = a_new->mask; + memcpy(&route->addr, &a_new->addr, sizeof(route->addr)); + clear_if_addr(&route->addr, route->mask); + + system_del_route(dev, route); + + route->flags &= ~DEVADDR_KERNEL; + route->metric = iface->metric; + vlist_add(&ip->route, &route->node, &route->flags); } static bool |