From: Roy Marples Date: Thu, 9 Apr 2020 16:19:07 +0000 (+0000) Subject: Linux: Note router preference when adding routes X-Git-Tag: v9.0.1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db58572fd54a12ee5959d86e2b77b0d3621f9adf;p=thirdparty%2Fdhcpcd.git Linux: Note router preference when adding routes This appears to just be cosmetic. --- diff --git a/src/if-linux.c b/src/if-linux.c index 8b8fe973..d139af43 100644 --- a/src/if-linux.c +++ b/src/if-linux.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -1061,6 +1062,14 @@ add_attr_l(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, return 0; } +static int +add_attr_8(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, + uint8_t data) +{ + + return add_attr_l(n, maxlen, type, &data, sizeof(data)); +} + static int add_attr_32(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, uint32_t data) @@ -1504,6 +1513,26 @@ if_route(unsigned char cmd, const struct rt *rt) RTA_DATA(metrics), (unsigned short)RTA_PAYLOAD(metrics)); } + + if (rt->rt_dflags & RTDF_RA) { + uint8_t pref; + + switch(rt->rt_pref) { + case RTPREF_LOW: + pref = ICMPV6_ROUTER_PREF_LOW; + break; + case RTPREF_MEDIUM: + pref = ICMPV6_ROUTER_PREF_MEDIUM; + break; + case RTPREF_HIGH: + pref = ICMPV6_ROUTER_PREF_HIGH; + break; + default: + pref = ICMPV6_ROUTER_PREF_INVALID; + break; + } + add_attr_8(&nlm.hdr, sizeof(nlm), RTA_PREF, pref); + } } if (!sa_is_loopback(&rt->rt_gateway)) @@ -1878,14 +1907,6 @@ struct nlml char buffer[32]; }; -static int -add_attr_8(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, - uint8_t data) -{ - - return add_attr_l(n, maxlen, type, &data, sizeof(data)); -} - static struct rtattr * add_attr_nest(struct nlmsghdr *n, unsigned short maxlen, unsigned short type) { diff --git a/src/ipv6.c b/src/ipv6.c index ede87c3d..570879ae 100644 --- a/src/ipv6.c +++ b/src/ipv6.c @@ -2253,6 +2253,9 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) rt = inet6_makeprefix(rap->iface, rap, addr); if (rt) { rt->rt_dflags |= RTDF_RA; +#ifdef HAVE_ROUTE_PREF + rt->rt_pref = ipv6nd_rtpref(rap); +#endif rt_proto_add(routes, rt); } } @@ -2264,6 +2267,9 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) if (rt == NULL) continue; rt->rt_dflags |= RTDF_RA; +#ifdef HAVE_ROUTE_PREF + rt->rt_pref = ipv6nd_rtpref(rap); +#endif rt_proto_add(routes, rt); } return 0; diff --git a/src/ipv6nd.c b/src/ipv6nd.c index 2860e67c..7ce34001 100644 --- a/src/ipv6nd.c +++ b/src/ipv6nd.c @@ -101,13 +101,6 @@ __CTASSERT(sizeof(struct nd_opt_rdnss) == 8); #define ND_RA_FLAG_RTPREF_RSV 0x10 #endif -/* RTPREF_MEDIUM has to be 0! */ -#define RTPREF_HIGH 1 -#define RTPREF_MEDIUM 0 -#define RTPREF_LOW (-1) -#define RTPREF_RESERVED (-2) -#define RTPREF_INVALID (-3) /* internal */ - #define EXPIRED_MAX 5 /* Remember 5 expired routers to avoid logspam. */ @@ -580,7 +573,7 @@ ipv6nd_startexpire(struct interface *ifp) RTR_CARRIER_EXPIRE, ipv6nd_expire, ifp); } -static int +int ipv6nd_rtpref(struct ra *rap) { diff --git a/src/ipv6nd.h b/src/ipv6nd.h index bb14d7b3..8d616999 100644 --- a/src/ipv6nd.h +++ b/src/ipv6nd.h @@ -104,6 +104,7 @@ int ipv6nd_open(struct interface *); int ipv6nd_open(struct dhcpcd_ctx *); #endif void ipv6nd_recvmsg(struct dhcpcd_ctx *, struct msghdr *); +int ipv6nd_rtpref(struct ra *); void ipv6nd_printoptions(const struct dhcpcd_ctx *, const struct dhcp_opt *, size_t); void ipv6nd_startrs(struct interface *); diff --git a/src/route.c b/src/route.c index 401ce07c..125c9532 100644 --- a/src/route.c +++ b/src/route.c @@ -349,11 +349,8 @@ rt_new0(struct dhcpcd_ctx *ctx) #endif } else #endif - if ((rt = malloc(sizeof(*rt))) == NULL) { + if ((rt = calloc(1, sizeof(*rt))) == NULL) logerr(__func__); - return NULL; - } - memset(rt, 0, sizeof(*rt)); return rt; } diff --git a/src/route.h b/src/route.h index 629fb200..a061497c 100644 --- a/src/route.h +++ b/src/route.h @@ -60,6 +60,10 @@ # endif #endif +#ifdef __linux__ +# define HAVE_ROUTE_PREF +#endif + #if defined(__OpenBSD__) || defined (__sun) # define ROUTE_PER_GATEWAY /* XXX dhcpcd doesn't really support this yet. @@ -86,6 +90,14 @@ struct rt { #ifdef HAVE_ROUTE_METRIC unsigned int rt_metric; #endif +#ifdef HAVE_ROUTE_PREF + int rt_pref; +#endif +#define RTPREF_HIGH 1 +#define RTPREF_MEDIUM 0 /* has to be zero */ +#define RTPREF_LOW (-1) +#define RTPREF_RESERVED (-2) +#define RTPREF_INVALID (-3) /* internal */ unsigned int rt_dflags; #define RTDF_IFA_ROUTE 0x02 /* Address generated route */ #define RTDF_FAKE 0x04 /* Maybe us on lease reboot */