- if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
- interface_handle_subnet_route(iface, a_old, false);
- system_del_address(dev, a_old);
+ if (a_old->enabled && !keep) {
+ if ((a_old->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
+ v6 = true;
+
+ unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
+
+ //This is needed for source routing to work correctly. If a device
+ //has two connections to a network using the same subnet, adding
+ //only the network-rule will cause packets to be routed through the
+ //first matching network (source IP matches both masks).
+ if (table) {
+ set_ip_source_policy(false, v6, IPRULE_PRIORITY_ADDR, &a_old->addr,
+ (v6) ? 128 : 32, table, NULL, NULL);
+
+ if (a_old->mask != ((v6) ? 128 : 32))
+ set_ip_source_policy(false, v6, IPRULE_PRIORITY_NW, &a_old->addr,
+ a_old->mask, table, NULL, NULL);
+ }
+
+ if (!(a_old->flags & DEVADDR_EXTERNAL)) {
+ interface_handle_subnet_route(iface, a_old, false);
+ system_del_address(dev, a_old);
+ }