[PATCH 3/3] rtnl_route: decode RPL_IPUNNEL_SRH in RTA_ENCAP

Mathis Marion Mathis.Marion at silabs.com
Mon Oct 23 08:56:36 UTC 2023


From: Mathis Marion <mathis.marion at silabs.com>

Tested by running the following command:

    ip route add 2001:db8::1 encap rpl segs 2001:db8::2,2001:db8::3

Before change:

    {nlmsg_len=112, nlmsg_type=RTM_NEWROUTE, nlmsg_flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, nlmsg_seq=1683911670, nlmsg_pid=0},
    {rtm_family=AF_INET6, rtm_dst_len=128, rtm_src_len=0, rtm_tos=0, rtm_table=RT_TABLE_MAIN, rtm_protocol=RTPROT_BOOT, rtm_scope=RT_SCOPE_UNIVERSE, rtm_type=RTN_UNICAST, rtm_flags=0},
    [
        [{nla_len=20, nla_type=RTA_DST}, inet_pton(AF_INET6, "2001:db8::1")],
       	[{nla_len=48, nla_type=NLA_F_NESTED|RTA_ENCAP}, "\x2c\x00\x01\x00\x00\x04\x03\x02\x00\x00\x00\x00\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x20\x01\x0d\xb8"...],
       	[{nla_len=6, nla_type=RTA_ENCAP_TYPE}, LWTUNNEL_ENCAP_RPL]
    ]

After:

    {nlmsg_len=112, nlmsg_type=RTM_NEWROUTE, nlmsg_flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, nlmsg_seq=1683919618, nlmsg_pid=0},
    {rtm_family=AF_INET6, rtm_dst_len=128, rtm_src_len=0, rtm_tos=0, rtm_table=RT_TABLE_MAIN, rtm_protocol=RTPROT_BOOT, rtm_scope=RT_SCOPE_UNIVERSE, rtm_type=RTN_UNICAST, rtm_flags=0},
    [
        [{nla_len=20, nla_type=RTA_DST}, inet_pton(AF_INET6, "2001:db8::1")],
        [
            {nla_len=48, nla_type=NLA_F_NESTED|RTA_ENCAP},
            [{nla_len=44, nla_type=RPL_IPTUNNEL_SRH}, {inet_pton(AF_INET6, "2001:db8::3"), inet_pton(AF_INET6, "2001:db8::2")}]
        ],
        [{nla_len=6, nla_type=RTA_ENCAP_TYPE}, LWTUNNEL_ENCAP_RPL]
    ]
---
 src/rtnl_route.c               | 60 +++++++++++++++++++++++++++++++++-
 src/xlat/lwtunnel_encap_rpl.in |  3 ++
 2 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 src/xlat/lwtunnel_encap_rpl.in

diff --git a/src/rtnl_route.c b/src/rtnl_route.c
index 6bb34cfd1..e3951ee78 100644
--- a/src/rtnl_route.c
+++ b/src/rtnl_route.c
@@ -11,10 +11,14 @@
 #include "netlink_route.h"
 #include "nlattr.h"
 
+#include <sys/socket.h>
 #include <linux/ip.h>
+#include <linux/rpl.h>
+#include <linux/rpl_iptunnel.h>
 #include <linux/rtnetlink.h>
 
 #include "xlat/ip_type_of_services.h"
+#include "xlat/lwtunnel_encap_rpl.h"
 #include "xlat/lwtunnel_encap_types.h"
 #include "xlat/route_nexthop_flags.h"
 #include "xlat/routing_flags.h"
@@ -213,6 +217,60 @@ decode_nla_lwt_encap_type(struct tcb *const tcp,
 	return true;
 }
 
+static bool
+decode_rpl_srh(struct tcb *const tcp,
+	       const kernel_ulong_t addr,
+	       const unsigned int len,
+	       const void *const opaque_data)
+{
+        struct ipv6_rpl_sr_hdr srh;
+
+        if (len < sizeof(srh))
+                return false;
+        else if (!umove_or_printaddr(tcp, addr, &srh)) {
+                if (srh.cmpri || srh.cmpre || !srh.segments_left)
+                        return false;
+                if (16 * srh.segments_left > len - sizeof(srh))
+                        return false;
+                tprint_struct_begin();
+                decode_inet_addr(tcp, addr + sizeof(srh), len - sizeof(srh),
+                                 AF_INET6, NULL);
+                for (unsigned int i = 1; i < srh.segments_left; i++) {
+                        tprint_struct_next();
+                        decode_inet_addr(tcp, addr + sizeof(srh) + 16 * i,
+                                         len - sizeof(srh) - 16 * i,
+                                         AF_INET6, NULL);
+                }
+                tprint_struct_end();
+        }
+
+        return true;
+}
+
+static const nla_decoder_t lwt_encap_rpl_decoders[] = {
+	[RPL_IPTUNNEL_SRH]	= decode_rpl_srh,
+};
+
+static bool
+decode_nla_lwt_encap(struct tcb *const tcp,
+		     const kernel_ulong_t addr,
+		     const unsigned int len,
+		     const void *const opaque_data)
+{
+        const struct rtmsg_ctx *const ctx = opaque_data;
+
+        switch (ctx->lwt_encap_type) {
+        case LWTUNNEL_ENCAP_RPL:
+                decode_nlattr(tcp, addr, len, lwtunnel_encap_rpl,
+                              "RPL_IPTUNNEL_??", lwt_encap_rpl_decoders,
+                              ARRAY_SIZE(lwt_encap_rpl_decoders),
+                              opaque_data);
+                return true;
+        default:
+                return false;
+        }
+}
+
 static const nla_decoder_t rtmsg_nla_decoders[] = {
 	[RTA_DST]		= decode_route_addr,
 	[RTA_SRC]		= decode_route_addr,
@@ -235,7 +293,7 @@ static const nla_decoder_t rtmsg_nla_decoders[] = {
 	[RTA_NEWDST]		= decode_route_addr,
 	[RTA_PREF]		= decode_nla_u8,
 	[RTA_ENCAP_TYPE]	= decode_nla_lwt_encap_type,
-	[RTA_ENCAP]		= NULL, /* unimplemented */
+	[RTA_ENCAP]		= decode_nla_lwt_encap,
 	[RTA_EXPIRES]		= decode_nla_u64,
 	[RTA_PAD]		= NULL,
 	[RTA_UID]		= decode_nla_u32,
diff --git a/src/xlat/lwtunnel_encap_rpl.in b/src/xlat/lwtunnel_encap_rpl.in
new file mode 100644
index 000000000..c60c45c9a
--- /dev/null
+++ b/src/xlat/lwtunnel_encap_rpl.in
@@ -0,0 +1,3 @@
+#value_indexed
+RPL_IPTUNNEL_UNSPEC	0
+RPL_IPTUNNEL_SRH	1
-- 
2.42.0



More information about the Strace-devel mailing list