Fix traceroute in the presence of SRv6
Subsystems (5)
v3 cover Fix traceroute in the presence of SRv6
When using SRv6 the destination IP address in the IPv6 header is not always the true destination, it can be a router along the path that SRv6 is using.
When ICMP reports an error, e.g, time exceeded, which is what traceroute uses, it included the packet which invoked the error into the ICMP message body. Upon receiving such an ICMP packet, the invoking packet is examined and an attempt is made to find the socket which sent the packet, so the error can be reported. Lookup is performed using the source and destination address. If the intermediary router IP address from the IP header is used, the lookup fails. It is necessary to dig into the header and find the true destination address in the Segment Router header, SRH.
v2:
Play games with the skb->network_header rather than clone the skb v3
Move helpers into seg6.c
Patch 1 exports a helper which can find the SRH in a packet Patch 2 does the actual examination of the invoking packet Patch 3 makes use of the results when trying to find the socket.
Andrew Lunn (3):
seg6: export get_srh() for ICMP handling
icmp: ICMPV6: Examine invoking packet for Segment Route Headers.
udp6: Use Segment Routing Header for dest address if present
include/linux/ipv6.h | 2 ++ include/net/seg6.h | 1 + net/ipv6/icmp.c | 36 +++++++++++++++++++++++++++++++++++- net/ipv6/seg6.c | 29 +++++++++++++++++++++++++++++ net/ipv6/seg6_local.c | 33 ++------------------------------- net/ipv6/udp.c | 7 +++++++ 6 files changed, 76 insertions(+), 32 deletions(-)
Signature · 1 line
2.33.1
PATCH v3 1/1 2/3] icmp: ICMPV6: Examine invoking packet for Segment Route Headers.
RFC8754 says:
ICMP error packets generated within the SR domain are sent to source nodes within the SR domain. The invoking packet in the ICMP error message may contain an SRH. Since the destination address of a packet with an SRH changes as each segment is processed, it may not be the destination used by the socket or application that generated the invoking packet.
For the source of an invoking packet to process the ICMP error message, the ultimate destination address of the IPv6 header may be required. The following logic is used to determine the destination address for use by protocol-error handlers.
* Walk all extension headers of the invoking IPv6 packet to the routing extension header preceding the upper-layer header.
- If routing header is type 4 Segment Routing Header (SRH)
o The SID at Segment List[0] may be used as the destination address of the invoking packet.
Mangle the skb so the network header points to the invoking packet inside the ICMP packet. The seg6 helpers can then be used on the skb to find any segment routing headers. If found, mark this fact in the IPv6 control block of the skb, and store the offset into the packet of the SRH. Then restore the skb back to its old state.
--- include/linux/ipv6.h | 2 ++ include/net/seg6.h | 1 + net/ipv6/icmp.c | 6 +++++- net/ipv6/seg6.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-)
include/linux/ipv6.h +2 -0
@@ -133,6 +133,7 @@ struct inet6_skb_parm {__u16 dsthao;#endif __u16 frag_max_size; +__u16 srhoff;#define IP6SKB_XFRM_TRANSFORMED 1#define IP6SKB_FORWARDED 2
@@ -142,6 +143,7 @@ struct inet6_skb_parm {#define IP6SKB_HOPBYHOP 32#define IP6SKB_L3SLAVE 64#define IP6SKB_JUMBOGRAM 128 +#define IP6SKB_SEG6 256 };#if defined(CONFIG_NET_L3_MASTER_DEV )
include/net/seg6.h +1 -0
@@ -59,6 +59,7 @@ extern void seg6_local_exit(void);extern bool seg6_validate_srh (struct ipv6_sr_hdr *srh,int len,bool reduced);extern struct ipv6_sr_hdr *seg6_get_srh (struct sk_buff *skb,int flags); +extern void seg6_icmp_srh (struct sk_buff *skb,struct inet6_skb_parm *opt);extern int seg6_do_srh_encap (struct sk_buff *skb,struct ipv6_sr_hdr *osrh,int proto);extern int seg6_do_srh_inline (struct sk_buff *skb,struct ipv6_sr_hdr *osrh);
net/ipv6/icmp.c +5 -1
@@ -57,6 +57,7 @@#include <net/protocol.h> #include <net/raw.h> #include <net/rawv6.h> +#include <net/seg6.h> #include <net/transp_v6.h> #include <net/ip6_route.h> #include <net/addrconf.h>
@@ -820,6 +821,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb)void icmpv6_notify (struct sk_buff *skb,u8 type,u8 code,__be32 info) { +struct inet6_skb_parm *opt =IP6CB (skb);const struct inet6_protocol *ipprot;int inner_offset;__be16 frag_off;
@@ -829,6 +831,8 @@ void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)if (!pskb_may_pull (skb,sizeof (struct ipv6hdr ))) goto out; +seg6_icmp_srh (skb, opt); + nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;if (ipv6_ext_hdr (nexthdr)) {/* now skip over extension headers */
@@ -853,7 +857,7 @@ void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info) ipprot =rcu_dereference (inet6_protos[nexthdr]);if (ipprot && ipprot->err_handler) - ipprot->err_handler (skb,NULL , type, code, inner_offset, info); + ipprot->err_handler (skb, opt, type, code, inner_offset, info);raw6_icmp_error (skb, nexthdr, type, code, inner_offset, info);return ;
net/ipv6/seg6.c +30 -0
@@ -104,6 +104,36 @@ struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb, int flags)return srh; } +/* Determine if an ICMP invoking packet contains a segment routing +* header. If it does, extract the offset to the true destination +* address, which is in the first segment address. +*/ +void seg6_icmp_srh (struct sk_buff *skb,struct inet6_skb_parm *opt) +{ +__u16 network_header = skb->network_header; +struct ipv6_sr_hdr *srh; + +/* Update network header to point to the invoking packet +* inside the ICMP packet, so we can use the seg6_get_srh() +* helper. +*/ +skb_reset_network_header (skb); + + srh =seg6_get_srh (skb,0 ); +if (!srh) + goto out; + +if (srh->type !=IPV6_SRCRT_TYPE_4 ) + goto out; + + opt->flags |=IP6SKB_SEG6 ; + opt->srhoff = (unsigned char *)srh - skb->data; + +out: +/* Restore the network header back to the ICMP packet */ + skb->network_header = network_header; +} +static struct genl_family seg6_genl_family;static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX +1 ] = {
Signature · 1 line
2.33.1
PATCH v3 1/1 3/3] udp6: Use Segment Routing Header for dest address if present
In reply to Andrew LunnWhen finding the socket to report an error on, if the invoking packet is using Segment Routing, the IPv6 destination address is that of an intermediate router, not the end destination. Extract the ultimate destination address from the segment address.
This change allows traceroute to function in the presence of Segment Routing.
--- include/net/seg6.h | 2 ++ net/ipv6/seg6.c | 21 +++++++++++++++++++++ net/ipv6/udp.c | 3 ++- 3 files changed, 25 insertions(+), 1 deletion(-)
include/net/seg6.h +2 -0
@@ -60,6 +60,8 @@ extern void seg6_local_exit(void);extern bool seg6_validate_srh (struct ipv6_sr_hdr *srh,int len,bool reduced);extern struct ipv6_sr_hdr *seg6_get_srh (struct sk_buff *skb,int flags);extern void seg6_icmp_srh (struct sk_buff *skb,struct inet6_skb_parm *opt); +extern const struct in6_addr *seg6_get_daddr (struct sk_buff *skb, +struct inet6_skb_parm *opt);extern int seg6_do_srh_encap (struct sk_buff *skb,struct ipv6_sr_hdr *osrh,int proto);extern int seg6_do_srh_inline (struct sk_buff *skb,struct ipv6_sr_hdr *osrh);
net/ipv6/seg6.c +21 -0
@@ -134,6 +134,27 @@ void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt) skb->network_header = network_header; } +/* If the packet which invoked an ICMP error contains an SRH return +* the true destination address from within the SRH, otherwise use the +* destination address in the IP header. +*/ +const struct in6_addr *seg6_get_daddr (struct sk_buff *skb, +struct inet6_skb_parm *opt) +{ +/* ipv6_hdr() does not work here, since this IP header is +* nested inside an ICMP error report packet +*/ +const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; +struct ipv6_sr_hdr *srh; + +if (opt->flags &IP6SKB_SEG6 ) { + srh = (struct ipv6_sr_hdr *)(skb->data + opt->srhoff); +return &srh->segments[0 ]; + } + +return &hdr->daddr; +} +static struct genl_family seg6_genl_family;static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX +1 ] = {
net/ipv6/udp.c +2 -1
@@ -40,6 +40,7 @@#include <net/transp_v6.h> #include <net/ip6_route.h> #include <net/raw.h> +#include <net/seg6.h> #include <net/tcp_states.h> #include <net/ip6_checksum.h> #include <net/ip6_tunnel.h>
@@ -560,8 +561,8 @@ int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, {struct ipv6_pinfo *np;const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; +const struct in6_addr *daddr =seg6_get_daddr (skb, opt);const struct in6_addr *saddr = &hdr->saddr; -const struct in6_addr *daddr = &hdr->daddr;struct udphdr *uh = (struct udphdr *)(skb->data+offset);bool tunnel = false;struct sock *sk;
Signature · 1 line
2.33.1
Re: [[PATCH net-next v3] 3/3] udp6: Use Segment Routing Header for dest address if present
Quoted 46 lines · Andrew Lunn
net/ipv6/seg6.c +21
-0@@ -134,6 +134,27 @@ void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt) skb->network_header = network_header; } +/* If the packet which invoked an ICMP error contains an SRH return +* the true destination address from within the SRH, otherwise use the +* destination address in the IP header. +*/ +const struct in6_addr *seg6_get_daddr (struct sk_buff *skb, +struct inet6_skb_parm *opt) +{ +/* ipv6_hdr() does not work here, since this IP header is +* nested inside an ICMP error report packet +*/ +const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; +struct ipv6_sr_hdr *srh; + +if (opt->flags &IP6SKB_SEG6 ) { + srh = (struct ipv6_sr_hdr *)(skb->data + opt->srhoff); +return &srh->segments[0 ]; + } + +return &hdr->daddr; +} +static struct genl_family seg6_genl_family;static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX +1 ] = {net/ipv6/udp.c +1
-0@@ -40,6 +40,7 @@#include <net/transp_v6.h> #include <net/ip6_route.h> #include <net/raw.h> +#include <net/seg6.h> #include <net/tcp_states.h> #include <net/ip6_checksum.h> #include <net/ip6_tunnel.h>
net/ipv6/udp.c int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, · line 568
Quoted 9 lines
net/ipv6/udp.c +1
-1@@ -560,8 +561,8 @@ int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, {struct ipv6_pinfo *np;const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; +const struct in6_addr *daddr =seg6_get_daddr (skb, opt);const struct in6_addr *saddr = &hdr->saddr; -const struct in6_addr *daddr = &hdr->daddr;struct udphdr *uh = (struct udphdr *)(skb->data+offset);bool tunnel = false;struct sock *sk;
I was thinking something like:
const struct in6_addr *daddr daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
where seg6_get_daddr returns NULL if it is not returning an address due to SR6 and in that case the lookup uses the daddr from the ipv6 hdr. That keeps the SR6 logic independent.
Re: [[PATCH net-next v3] 3/3] udp6: Use Segment Routing Header for dest address if present
In reply to Andrew LunnQuoted 19 lines · Andrew Lunn
net/ipv6/seg6.c +11
-0@@ -134,6 +134,27 @@ void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt) skb->network_header = network_header; } +/* If the packet which invoked an ICMP error contains an SRH return +* the true destination address from within the SRH, otherwise use the +* destination address in the IP header. +*/ +const struct in6_addr *seg6_get_daddr (struct sk_buff *skb, +struct inet6_skb_parm *opt) +{ +/* ipv6_hdr() does not work here, since this IP header is +* nested inside an ICMP error report packet +*/ +const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
net/ipv6/seg6.c void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt) · line 157
Quoted 10 lines
net/ipv6/seg6.c +10
-0+struct ipv6_sr_hdr *srh; + +if (opt->flags &IP6SKB_SEG6 ) { + srh = (struct ipv6_sr_hdr *)(skb->data + opt->srhoff); +return &srh->segments[0 ]; + } + +return &hdr->daddr; +} +
also, that could be an inline in net/seg6.h given how short it is.
PATCH v3 1/1 1/3] seg6: export get_srh() for ICMP handling
In reply to Andrew LunnAn ICMP error message can contain in its message body part of an IPv6 packet which invoked the error. Such a packet might contain a segment router header. Export get_srh() so the ICMP code can make use of it.
Since his changes the scope of the function from local to global, add the seg6_ prefix to keep the namespace clean. And move it into seg6.c so it is always available, not just when IPV6_SEG6_LWTUNNEL is enabled.
--- include/net/seg6.h | 1 + net/ipv6/seg6.c | 29 +++++++++++++++++++++++++++++ net/ipv6/seg6_local.c | 33 ++------------------------------- 3 files changed, 32 insertions(+), 31 deletions(-)
include/net/seg6.h +1 -0
@@ -58,6 +58,7 @@ extern int seg6_local_init(void);extern void seg6_local_exit (void );extern bool seg6_validate_srh (struct ipv6_sr_hdr *srh,int len,bool reduced); +extern struct ipv6_sr_hdr *seg6_get_srh (struct sk_buff *skb,int flags);extern int seg6_do_srh_encap (struct sk_buff *skb,struct ipv6_sr_hdr *osrh,int proto);extern int seg6_do_srh_inline (struct sk_buff *skb,struct ipv6_sr_hdr *osrh);
net/ipv6/seg6.c +29 -0
@@ -75,6 +75,35 @@ bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len, bool reduced)return true; } +struct ipv6_sr_hdr *seg6_get_srh (struct sk_buff *skb,int flags) +{ +struct ipv6_sr_hdr *srh; +int len, srhoff =0 ; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL , &flags) <0 ) +return NULL ; + +if (!pskb_may_pull (skb, srhoff +sizeof (*srh))) +return NULL ; + + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + + len = (srh->hdrlen +1 ) <<3 ; + +if (!pskb_may_pull (skb, srhoff + len)) +return NULL ; + +/* note that pskb_may_pull may change pointers in header; +* for this reason it is necessary to reload them when needed. +*/ + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + +if (!seg6_validate_srh (srh, len, true)) +return NULL ; + +return srh; +} +static struct genl_family seg6_genl_family;static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX +1 ] = {
net/ipv6/seg6_local.c +2 -31
@@ -150,40 +150,11 @@ static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)return (struct seg6_local_lwt *)lwt->data; } -static struct ipv6_sr_hdr *get_srh (struct sk_buff *skb,int flags) -{ -struct ipv6_sr_hdr *srh; -int len, srhoff =0 ; - -if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL , &flags) <0 ) -return NULL ; - -if (!pskb_may_pull (skb, srhoff +sizeof (*srh))) -return NULL ; - - srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); - - len = (srh->hdrlen +1 ) <<3 ; - -if (!pskb_may_pull (skb, srhoff + len)) -return NULL ; - -/* note that pskb_may_pull may change pointers in header; -* for this reason it is necessary to reload them when needed. -*/ - srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); - -if (!seg6_validate_srh (srh, len, true)) -return NULL ; - -return srh; -} -static struct ipv6_sr_hdr *get_and_validate_srh (struct sk_buff *skb) {struct ipv6_sr_hdr *srh; - srh =get_srh (skb,IP6_FH_F_SKIP_RH ); + srh =seg6_get_srh (skb,IP6_FH_F_SKIP_RH );if (!srh)return NULL ;
@@ -200,7 +171,7 @@ static bool decap_and_validate(struct sk_buff *skb, int proto)struct ipv6_sr_hdr *srh;unsigned int off =0 ; - srh =get_srh (skb,0 ); + srh =seg6_get_srh (skb,0 );if (srh && srh->segments_left >0 )return false;
Signature · 1 line
2.33.1