From: Roy Marples Date: Fri, 12 Oct 2007 11:19:29 +0000 (+0000) Subject: Always add a link-local route unless we're given a non-private address by the DHCP... X-Git-Tag: v3.2.3~183 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a41c971c80cf3a50d252acc186bf0346ea86f7d6;p=thirdparty%2Fdhcpcd.git Always add a link-local route unless we're given a non-private address by the DHCP server. --- diff --git a/ChangeLog b/ChangeLog index 4b8c6beb..e7a543af 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,5 @@ +Always add a link-local route unless we're given a non-private +address by the DHCP server. Reduce stack usage by using malloc more. If we're on a different subnet from the one we get DHCP for, don't use our current address in messages. Thanks to Wilson Callan. diff --git a/configure.c b/configure.c index 0847b2ea..067131e4 100644 --- a/configure.c +++ b/configure.c @@ -49,6 +49,9 @@ #ifdef ENABLE_INFO # include "info.h" #endif +#ifdef ENABLE_IPV4LL +# include "ipv4ll.h" +#endif #include "interface.h" #include "dhcpcd.h" #include "logger.h" @@ -335,10 +338,15 @@ int configure (const options_t *options, interface_t *iface, const dhcp_t *dhcp, bool up) { route_t *route = NULL; + route_t *new_routes = NULL; route_t *new_route = NULL; route_t *old_route = NULL; char *newhostname = NULL; char *curhostname = NULL; + int remember; +#ifdef ENABLE_IPV4LL + bool haslinklocal = false; +#endif if (! options || ! iface || ! dhcp) return (-1); @@ -453,13 +461,18 @@ int configure (const options_t *options, interface_t *iface, /* Remember added routes */ if (dhcp->routes) { - route_t *new_routes = NULL; - int remember; #ifdef THERE_IS_NO_FORK int skip = 0; #endif for (route = dhcp->routes; route; route = route->next) { +#ifdef ENABLE_IPV4LL + /* Check if we have already got a link locale route dished + * out by the DHCP server */ + if (route->destination.s_addr == htonl (LINKLOCAL_ADDR) && + route->netmask.s_addr == htonl (LINKLOCAL_MASK)) + haslinklocal = true; +#endif /* Don't set default routes if not asked to */ if (route->destination.s_addr == 0 && route->netmask.s_addr == 0 && ! options->dogateway) @@ -510,11 +523,45 @@ int configure (const options_t *options, interface_t *iface, #endif } - if (iface->previous_routes) - free_route (iface->previous_routes); + } - iface->previous_routes = new_routes; +#ifdef ENABLE_IPV4LL + /* Ensure we always add the link local route if we got a private + * address and isn't link local itself */ + if (options-> doipv4ll && + ! haslinklocal && + IN_PRIVATE (dhcp->address.s_addr)) + { + struct in_addr dest; + struct in_addr mask; + struct in_addr gate; + + dest.s_addr = htonl (LINKLOCAL_ADDR); + mask.s_addr = htonl (LINKLOCAL_MASK); + gate.s_addr = 0; + remember = add_route (iface->name, dest, mask, gate, + options->metric); + + if (remember >= 0) { + if (! new_routes) { + new_routes = xmalloc (sizeof (route_t)); + memset (new_routes, 0, sizeof (route_t)); + new_route = new_routes; + } else { + new_route->next = xmalloc (sizeof (route_t)); + new_route = new_route->next; + new_route->next = NULL; + } + new_route->destination.s_addr = dest.s_addr; + new_route->netmask.s_addr = mask.s_addr; + new_route->gateway.s_addr = gate.s_addr; + } } +#endif + + if (iface->previous_routes) + free_route (iface->previous_routes); + iface->previous_routes = new_routes; if (options->dodns && dhcp->dnsservers) make_resolv(iface->name, dhcp); diff --git a/interface.h b/interface.h index f3bf0505..59f5024f 100644 --- a/interface.h +++ b/interface.h @@ -51,7 +51,18 @@ # define ARPHRD_INFINIBAND 27 #endif -#define HWADDR_LEN 20 +#define HWADDR_LEN 20 + +/* Work out if we have a private address or not + * 10/8 + * 172.16/12 + * 192.168/16 + */ +#ifndef IN_PRIVATE +# define IN_PRIVATE(addr) (((ntohl (addr) & IN_CLASSA_NET) == 0x0a000000) || \ + ((ntohl (addr) & 0xfff00000) == 0xac100000) || \ + ((ntohl (addr) & IN_CLASSB_NET) == 0xc0a80000)) +#endif typedef struct route_t {