]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
seg6: add tunsrc support in iproute_lwtunnel
authorJustin Iurman <justin.iurman@6wind.com>
Fri, 20 Mar 2026 14:08:46 +0000 (15:08 +0100)
committerDavid Ahern <dsahern@kernel.org>
Tue, 7 Apr 2026 15:02:14 +0000 (09:02 -0600)
Add support for the new optional "tunsrc" parameter. Now, a tunnel
source address can be configured per route and has priority over the
configured per-netns source address (if any).

Example:
ip -6 r a 2001:db8:1::/64 encap seg6 mode encap tunsrc 2001:db8:ab::
    segs 2001:db8:42::1,2001:db8:ffff::2 dev eth0

Signed-off-by: Justin Iurman <justin.iurman@6wind.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
ip/iproute_lwtunnel.c

index a7885dba28c1c0ad15c543ff33a884594492039e..00b4f7565be651c421840dea9f8dc75a17637777 100644 (file)
@@ -266,6 +266,13 @@ static void print_encap_seg6(FILE *fp, struct rtattr *encap)
        print_string(PRINT_ANY, "mode",
                     "mode %s ", format_seg6mode_type(tuninfo->mode));
 
+       if (tb[SEG6_IPTUNNEL_SRC]) {
+               print_color_string(PRINT_ANY, COLOR_INET6,
+                                  "tunsrc", "tunsrc %s ",
+                                  rt_addr_n2a_rta(AF_INET6,
+                                                  tb[SEG6_IPTUNNEL_SRC]));
+       }
+
        print_srh(fp, tuninfo->srh);
 }
 
@@ -953,6 +960,8 @@ static int parse_encap_seg6(struct rtattr *rta, size_t len, int *argcp,
        struct ipv6_sr_hdr *srh;
        char **argv = *argvp;
        char segbuf[1024] = "";
+       bool tunsrc = false;
+       inet_prefix saddr;
        int argc = *argcp;
        int encap = -1;
        __u32 hmac = 0;
@@ -967,6 +976,22 @@ static int parse_encap_seg6(struct rtattr *rta, size_t len, int *argcp,
                        encap = read_seg6mode_type(*argv);
                        if (encap < 0)
                                invarg("\"mode\" value is invalid\n", *argv);
+               } else if (strcmp(*argv, "tunsrc") == 0) {
+                       NEXT_ARG();
+                       if (encap == -1)
+                               invarg("\"tunsrc\" provided before \"mode\"\n",
+                                      *argv);
+                       if (encap == SEG6_IPTUN_MODE_INLINE)
+                               invarg("\"tunsrc\" invalid with inline mode\n",
+                                      *argv);
+                       if (tunsrc)
+                               duparg2("tunsrc", *argv);
+
+                       get_addr(&saddr, *argv, AF_INET6);
+                       if (saddr.family != AF_INET6 || saddr.bytelen != 16)
+                               invarg("\"tunsrc\" value is invalid\n", *argv);
+
+                       tunsrc = true;
                } else if (strcmp(*argv, "segs") == 0) {
                        NEXT_ARG();
                        if (segs_ok++)
@@ -1005,6 +1030,12 @@ static int parse_encap_seg6(struct rtattr *rta, size_t len, int *argcp,
                          sizeof(*tuninfo) + srhlen))
                goto out;
 
+       if (tunsrc) {
+               if (rta_addattr_l(rta, len, SEG6_IPTUNNEL_SRC,
+                                 &saddr.data, saddr.bytelen))
+                       goto out;
+       }
+
        *argcp = argc + 1;
        *argvp = argv - 1;
        ret = 0;