]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Backport adding the src pref address from trunk.
authorRoy Marples <roy@marples.name>
Thu, 27 Nov 2008 16:17:24 +0000 (16:17 +0000)
committerRoy Marples <roy@marples.name>
Thu, 27 Nov 2008 16:17:24 +0000 (16:17 +0000)
configure.c
if-bsd.c
if-linux.c
net.h

index dad3bce6d19c5397716c9ee3c92fbf7d577fba7b..736c1883e8bea32cb186cf51f30566067ff867e3 100644 (file)
@@ -181,7 +181,7 @@ reverse_routes(struct rt *routes)
 }
 
 static int
-delete_route(const char *iface, struct rt *rt, int metric)
+delete_route(const struct interface *iface, struct rt *rt, int metric)
 {
        char *addr;
        int retval;
@@ -206,7 +206,7 @@ delete_routes(struct interface *iface, int metric)
        rt = reverse_routes(iface->routes);
        while (rt) {
                rtn = rt->next;
-               retval += delete_route(iface->name, rt, metric);
+               retval += delete_route(iface, rt, metric);
                free(rt);
                rt = rtn;
        }
@@ -272,7 +272,7 @@ configure_routes(struct interface *iface, const struct dhcp_message *dhcp,
        iface->routes = reverse_routes(iface->routes);
        for (rt = iface->routes; rt; rt = rt->next)
                if (in_routes(ort, rt) != 0)
-                       delete_route(iface->name, rt, options->metric);
+                       delete_route(iface, rt, options->metric);
 
        for (rt = ort; rt; rt = rt->next) {
                /* Don't set default routes if not asked to */
@@ -285,7 +285,7 @@ configure_routes(struct interface *iface, const struct dhcp_message *dhcp,
                logger(LOG_DEBUG, "adding route to %s/%d via %s",
                        addr, inet_ntocidr(rt->net), inet_ntoa(rt->gate));
                free(addr);
-               remember = add_route(iface->name, &rt->dest,
+               remember = add_route(iface, &rt->dest,
                                     &rt->net, &rt->gate,
                                     options->metric);
                retval += remember;
@@ -400,8 +400,8 @@ configure(struct interface *iface, const char *reason,
        {
                dest.s_addr = addr.s_addr & net.s_addr;
                gate.s_addr = 0;
-               add_route(iface->name, &dest, &net, &gate, options->metric);
-               del_route(iface->name, &dest, &net, &gate, 0);
+               add_route(iface, &dest, &net, &gate, options->metric);
+               del_route(iface, &dest, &net, &gate, 0);
        }
 #endif
 
index 5be10c1ef3c9ab33b3d261f7c7aea26cb0e4a3d6..e90b36c652a57781136c175d1ac0ba6925abcf38 100644 (file)
--- a/if-bsd.c
+++ b/if-bsd.c
@@ -98,7 +98,7 @@ if_address(const char *ifname, const struct in_addr *address,
 }
 
 int
-if_route(const char *ifname, const struct in_addr *destination,
+if_route(const struct interface *iface, const struct in_addr *destination,
         const struct in_addr *netmask, const struct in_addr *gateway,
         _unused int metric, int action)
 {
@@ -172,7 +172,7 @@ if_route(const char *ifname, const struct in_addr *destination,
        memset(&su, 0, sizeof(su));
        su.sdl.sdl_family = AF_LINK;
        su.sdl.sdl_len = sizeof(su.sdl);
-       link_addr(ifname, &su.sdl);
+       link_addr(iface->name, &su.sdl);
        ADDSU(su);
 
        rtm.hdr.rtm_msglen = l = bp - (char *)&rtm;
index 7039cbd792df4463c644fc415cb7137ab98fb7bf..a75455432484e260439acbb5e0c4344485991ac0 100644 (file)
@@ -317,7 +317,7 @@ if_address(const char *ifname,
 }
 
 int
-if_route(const char *ifname,
+if_route(const struct interface *iface,
         const struct in_addr *destination, const struct in_addr *netmask,
         const struct in_addr *gateway, int metric, int action)
 {
@@ -325,7 +325,7 @@ if_route(const char *ifname,
        unsigned int ifindex;
        int retval = 0;
 
-       if (!(ifindex = if_nametoindex(ifname))) {
+       if (!(ifindex = if_nametoindex(iface->name))) {
                errno = ENODEV;
                return -1;
        }
@@ -347,7 +347,13 @@ if_route(const char *ifname,
                nlm->rt.rtm_scope = RT_SCOPE_NOWHERE;
        else {
                nlm->hdr.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
-               nlm->rt.rtm_protocol = RTPROT_BOOT;
+               /* We only change route metrics for kernel routes */
+               if (destination->s_addr ==
+                   (iface->addr.s_addr & iface->net.s_addr) &&
+                   netmask->s_addr == iface->net.s_addr)
+                       nlm->rt.rtm_protocol = RTPROT_KERNEL;
+               else
+                       nlm->rt.rtm_protocol = RTPROT_BOOT;
                if (gateway->s_addr == INADDR_ANY ||
                    (gateway->s_addr == destination->s_addr &&
                     netmask->s_addr == INADDR_BROADCAST))
@@ -360,6 +366,10 @@ if_route(const char *ifname,
        nlm->rt.rtm_dst_len = inet_ntocidr(*netmask);
        add_attr_l(&nlm->hdr, sizeof(*nlm), RTA_DST,
                   &destination->s_addr, sizeof(destination->s_addr));
+       if (nlm->rt.rtm_protocol == RTPROT_KERNEL) {
+               add_attr_l(&nlm->hdr, sizeof(*nlm), RTA_PREFSRC,
+                          &iface->addr.s_addr, sizeof(iface->addr.s_addr));
+       }
        /* If destination == gateway then don't add the gateway */
        if (destination->s_addr != gateway->s_addr ||
            netmask->s_addr != INADDR_BROADCAST)
diff --git a/net.h b/net.h
index 3ade0257e0843d82198448d6e338d08f9fae6281..e9005d021d56f8c2116d5bec21ae86147830fa04 100644 (file)
--- a/net.h
+++ b/net.h
@@ -144,14 +144,15 @@ int if_address(const char *, const struct in_addr *, const struct in_addr *,
 #define get_address(ifname, addr, net) \
        do_interface(ifname, NULL, NULL, addr, net, 1)
 
-int if_route(const char *, const struct in_addr *, const struct in_addr *,
+int if_route(const struct interface *,
+            const struct in_addr *,const struct in_addr *,
             const struct in_addr *, int, int);
-#define add_route(ifname, dest, mask, gate, metric) \
-       if_route(ifname, dest, mask, gate, metric, 1)
-#define change_route(ifname, dest, mask, gate, metric) \
-       if_route(ifname, dest, mask, gate, metric, 0)
-#define del_route(ifname, dest, mask, gate, metric) \
-       if_route(ifname, dest, mask, gate, metric, -1)
+#define add_route(iface, dest, mask, gate, metric) \
+       if_route(iface, dest, mask, gate, metric, 1)
+#define change_route(iface, dest, mask, gate, metric) \
+       if_route(iface, dest, mask, gate, metric, 0)
+#define del_route(iface, dest, mask, gate, metric) \
+       if_route(iface, dest, mask, gate, metric, -1)
 void free_routes(struct rt *);
 
 int open_udp_socket(struct interface *);