changeset 5130:d34c808ae81e draft

Linux: Note router preference when adding routes This appears to just be cosmetic.
author Roy Marples <roy@marples.name>
date Thu, 09 Apr 2020 16:19:07 +0000
parents 8be78957f374
children d99a53af6486
files src/if-linux.c src/ipv6.c src/ipv6nd.c src/ipv6nd.h src/route.c src/route.h
diffstat 6 files changed, 50 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/if-linux.c	Thu Apr 09 15:38:18 2020 +0000
+++ b/src/if-linux.c	Thu Apr 09 16:19:07 2020 +0000
@@ -34,6 +34,7 @@
 #include <sys/param.h>
 #include <sys/stat.h>
 
+#include <linux/icmpv6.h>
 #include <linux/if_addr.h>
 #include <linux/if_link.h>
 #include <linux/if_packet.h>
@@ -1062,6 +1063,14 @@
 }
 
 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 @@
 			    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 @@
 	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)
 {
--- a/src/ipv6.c	Thu Apr 09 15:38:18 2020 +0000
+++ b/src/ipv6.c	Thu Apr 09 16:19:07 2020 +0000
@@ -2253,6 +2253,9 @@
 			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 @@
 		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;
--- a/src/ipv6nd.c	Thu Apr 09 15:38:18 2020 +0000
+++ b/src/ipv6nd.c	Thu Apr 09 16:19:07 2020 +0000
@@ -101,13 +101,6 @@
 #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 @@
 	    RTR_CARRIER_EXPIRE, ipv6nd_expire, ifp);
 }
 
-static int
+int
 ipv6nd_rtpref(struct ra *rap)
 {
 
--- a/src/ipv6nd.h	Thu Apr 09 15:38:18 2020 +0000
+++ b/src/ipv6nd.h	Thu Apr 09 16:19:07 2020 +0000
@@ -104,6 +104,7 @@
 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 *);
--- a/src/route.c	Thu Apr 09 15:38:18 2020 +0000
+++ b/src/route.c	Thu Apr 09 16:19:07 2020 +0000
@@ -349,11 +349,8 @@
 #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;
 }
 
--- a/src/route.h	Thu Apr 09 15:38:18 2020 +0000
+++ b/src/route.h	Thu Apr 09 16:19:07 2020 +0000
@@ -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 @@
 #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  */