Skip to content

Commit d2d9a6d

Browse files
committed
Merge branch 'srv6-traceroute'
Andrew Lunn says: ==================== 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 v4: Move short helper into header file. Rework getting SRH destination address v5: Fix comment to describe function, not caller 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. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents e8fe9e8 + 222a011 commit d2d9a6d

File tree

6 files changed

+91
-33
lines changed

6 files changed

+91
-33
lines changed

include/linux/ipv6.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ struct inet6_skb_parm {
133133
__u16 dsthao;
134134
#endif
135135
__u16 frag_max_size;
136+
__u16 srhoff;
136137

137138
#define IP6SKB_XFRM_TRANSFORMED 1
138139
#define IP6SKB_FORWARDED 2
@@ -142,6 +143,7 @@ struct inet6_skb_parm {
142143
#define IP6SKB_HOPBYHOP 32
143144
#define IP6SKB_L3SLAVE 64
144145
#define IP6SKB_JUMBOGRAM 128
146+
#define IP6SKB_SEG6 256
145147
};
146148

147149
#if defined(CONFIG_NET_L3_MASTER_DEV)

include/net/seg6.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,30 @@ extern int seg6_local_init(void);
5858
extern void seg6_local_exit(void);
5959

6060
extern bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len, bool reduced);
61+
extern struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb, int flags);
62+
extern void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt);
6163
extern int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh,
6264
int proto);
6365
extern int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh);
6466
extern int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
6567
u32 tbl_id);
68+
69+
/* If the packet which invoked an ICMP error contains an SRH return
70+
* the true destination address from within the SRH, otherwise use the
71+
* destination address in the IP header.
72+
*/
73+
static inline const struct in6_addr *seg6_get_daddr(struct sk_buff *skb,
74+
struct inet6_skb_parm *opt)
75+
{
76+
struct ipv6_sr_hdr *srh;
77+
78+
if (opt->flags & IP6SKB_SEG6) {
79+
srh = (struct ipv6_sr_hdr *)(skb->data + opt->srhoff);
80+
return &srh->segments[0];
81+
}
82+
83+
return NULL;
84+
}
85+
86+
6687
#endif

net/ipv6/icmp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <net/protocol.h>
5858
#include <net/raw.h>
5959
#include <net/rawv6.h>
60+
#include <net/seg6.h>
6061
#include <net/transp_v6.h>
6162
#include <net/ip6_route.h>
6263
#include <net/addrconf.h>
@@ -820,6 +821,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
820821

821822
void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
822823
{
824+
struct inet6_skb_parm *opt = IP6CB(skb);
823825
const struct inet6_protocol *ipprot;
824826
int inner_offset;
825827
__be16 frag_off;
@@ -829,6 +831,8 @@ void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
829831
if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
830832
goto out;
831833

834+
seg6_icmp_srh(skb, opt);
835+
832836
nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
833837
if (ipv6_ext_hdr(nexthdr)) {
834838
/* now skip over extension headers */
@@ -853,7 +857,7 @@ void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
853857

854858
ipprot = rcu_dereference(inet6_protos[nexthdr]);
855859
if (ipprot && ipprot->err_handler)
856-
ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
860+
ipprot->err_handler(skb, opt, type, code, inner_offset, info);
857861

858862
raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
859863
return;

net/ipv6/seg6.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,65 @@ bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len, bool reduced)
7575
return true;
7676
}
7777

78+
struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb, int flags)
79+
{
80+
struct ipv6_sr_hdr *srh;
81+
int len, srhoff = 0;
82+
83+
if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
84+
return NULL;
85+
86+
if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
87+
return NULL;
88+
89+
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
90+
91+
len = (srh->hdrlen + 1) << 3;
92+
93+
if (!pskb_may_pull(skb, srhoff + len))
94+
return NULL;
95+
96+
/* note that pskb_may_pull may change pointers in header;
97+
* for this reason it is necessary to reload them when needed.
98+
*/
99+
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
100+
101+
if (!seg6_validate_srh(srh, len, true))
102+
return NULL;
103+
104+
return srh;
105+
}
106+
107+
/* Determine if an ICMP invoking packet contains a segment routing
108+
* header. If it does, extract the offset to the true destination
109+
* address, which is in the first segment address.
110+
*/
111+
void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt)
112+
{
113+
__u16 network_header = skb->network_header;
114+
struct ipv6_sr_hdr *srh;
115+
116+
/* Update network header to point to the invoking packet
117+
* inside the ICMP packet, so we can use the seg6_get_srh()
118+
* helper.
119+
*/
120+
skb_reset_network_header(skb);
121+
122+
srh = seg6_get_srh(skb, 0);
123+
if (!srh)
124+
goto out;
125+
126+
if (srh->type != IPV6_SRCRT_TYPE_4)
127+
goto out;
128+
129+
opt->flags |= IP6SKB_SEG6;
130+
opt->srhoff = (unsigned char *)srh - skb->data;
131+
132+
out:
133+
/* Restore the network header back to the ICMP packet */
134+
skb->network_header = network_header;
135+
}
136+
78137
static struct genl_family seg6_genl_family;
79138

80139
static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {

net/ipv6/seg6_local.c

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,40 +150,11 @@ static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
150150
return (struct seg6_local_lwt *)lwt->data;
151151
}
152152

153-
static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb, int flags)
154-
{
155-
struct ipv6_sr_hdr *srh;
156-
int len, srhoff = 0;
157-
158-
if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
159-
return NULL;
160-
161-
if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
162-
return NULL;
163-
164-
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
165-
166-
len = (srh->hdrlen + 1) << 3;
167-
168-
if (!pskb_may_pull(skb, srhoff + len))
169-
return NULL;
170-
171-
/* note that pskb_may_pull may change pointers in header;
172-
* for this reason it is necessary to reload them when needed.
173-
*/
174-
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
175-
176-
if (!seg6_validate_srh(srh, len, true))
177-
return NULL;
178-
179-
return srh;
180-
}
181-
182153
static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
183154
{
184155
struct ipv6_sr_hdr *srh;
185156

186-
srh = get_srh(skb, IP6_FH_F_SKIP_RH);
157+
srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
187158
if (!srh)
188159
return NULL;
189160

@@ -200,7 +171,7 @@ static bool decap_and_validate(struct sk_buff *skb, int proto)
200171
struct ipv6_sr_hdr *srh;
201172
unsigned int off = 0;
202173

203-
srh = get_srh(skb, 0);
174+
srh = seg6_get_srh(skb, 0);
204175
if (srh && srh->segments_left > 0)
205176
return false;
206177

net/ipv6/udp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <net/transp_v6.h>
4141
#include <net/ip6_route.h>
4242
#include <net/raw.h>
43+
#include <net/seg6.h>
4344
#include <net/tcp_states.h>
4445
#include <net/ip6_checksum.h>
4546
#include <net/ip6_tunnel.h>
@@ -561,7 +562,7 @@ int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
561562
struct ipv6_pinfo *np;
562563
const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
563564
const struct in6_addr *saddr = &hdr->saddr;
564-
const struct in6_addr *daddr = &hdr->daddr;
565+
const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
565566
struct udphdr *uh = (struct udphdr *)(skb->data+offset);
566567
bool tunnel = false;
567568
struct sock *sk;

0 commit comments

Comments
 (0)