Skip to content

Commit 544b4dd

Browse files
Guillaume Naultkuba-moo
authored andcommitted
ipv4: Fix route lookups when handling ICMP redirects and PMTU updates
The PMTU update and ICMP redirect helper functions initialise their fl4 variable with either __build_flow_key() or build_sk_flow_key(). These initialisation functions always set ->flowi4_scope with RT_SCOPE_UNIVERSE and might set the ECN bits of ->flowi4_tos. This is not a problem when the route lookup is later done via ip_route_output_key_hash(), which properly clears the ECN bits from ->flowi4_tos and initialises ->flowi4_scope based on the RTO_ONLINK flag. However, some helpers call fib_lookup() directly, without sanitising the tos and scope fields, so the route lookup can fail and, as a result, the ICMP redirect or PMTU update aren't taken into account. Fix this by extracting the ->flowi4_tos and ->flowi4_scope sanitisation code into ip_rt_fix_tos(), then use this function in handlers that call fib_lookup() directly. Note 1: We can't sanitise ->flowi4_tos and ->flowi4_scope in a central place (like __build_flow_key() or flowi4_init_output()), because ip_route_output_key_hash() expects non-sanitised values. When called with sanitised values, it can erroneously overwrite RT_SCOPE_LINK with RT_SCOPE_UNIVERSE in ->flowi4_scope. Therefore we have to be careful to sanitise the values only for those paths that don't call ip_route_output_key_hash(). Note 2: The problem is mostly about sanitising ->flowi4_tos. Having ->flowi4_scope initialised with RT_SCOPE_UNIVERSE instead of RT_SCOPE_LINK probably wasn't really a problem: sockets with the SOCK_LOCALROUTE flag set (those that'd result in RTO_ONLINK being set) normally shouldn't receive ICMP redirects or PMTU updates. Fixes: 4895c77 ("ipv4: Add FIB nexthop exceptions.") Signed-off-by: Guillaume Nault <gnault@redhat.com> Reviewed-by: David Ahern <dsahern@kernel.org> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 6bd0c76 commit 544b4dd

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

net/ipv4/route.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,15 @@ void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
499499
}
500500
EXPORT_SYMBOL(__ip_select_ident);
501501

502+
static void ip_rt_fix_tos(struct flowi4 *fl4)
503+
{
504+
__u8 tos = RT_FL_TOS(fl4);
505+
506+
fl4->flowi4_tos = tos & IPTOS_RT_MASK;
507+
fl4->flowi4_scope = tos & RTO_ONLINK ?
508+
RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
509+
}
510+
502511
static void __build_flow_key(const struct net *net, struct flowi4 *fl4,
503512
const struct sock *sk,
504513
const struct iphdr *iph,
@@ -824,6 +833,7 @@ static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buf
824833
rt = (struct rtable *) dst;
825834

826835
__build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0);
836+
ip_rt_fix_tos(&fl4);
827837
__ip_do_redirect(rt, skb, &fl4, true);
828838
}
829839

@@ -1048,6 +1058,7 @@ static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
10481058
struct flowi4 fl4;
10491059

10501060
ip_rt_build_flow_key(&fl4, sk, skb);
1061+
ip_rt_fix_tos(&fl4);
10511062

10521063
/* Don't make lookup fail for bridged encapsulations */
10531064
if (skb && netif_is_any_bridge_port(skb->dev))
@@ -1122,6 +1133,8 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
11221133
goto out;
11231134

11241135
new = true;
1136+
} else {
1137+
ip_rt_fix_tos(&fl4);
11251138
}
11261139

11271140
__ip_rt_update_pmtu((struct rtable *)xfrm_dst_path(&rt->dst), &fl4, mtu);
@@ -2603,7 +2616,6 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
26032616
struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
26042617
const struct sk_buff *skb)
26052618
{
2606-
__u8 tos = RT_FL_TOS(fl4);
26072619
struct fib_result res = {
26082620
.type = RTN_UNSPEC,
26092621
.fi = NULL,
@@ -2613,9 +2625,7 @@ struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
26132625
struct rtable *rth;
26142626

26152627
fl4->flowi4_iif = LOOPBACK_IFINDEX;
2616-
fl4->flowi4_tos = tos & IPTOS_RT_MASK;
2617-
fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
2618-
RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
2628+
ip_rt_fix_tos(fl4);
26192629

26202630
rcu_read_lock();
26212631
rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb);

0 commit comments

Comments
 (0)