Since commit 7d3f3b4367f3 ("net: ipv4: Cache pmtu for all packet paths if multipath enabled") the mtu of a Fragmentation Needed is cached on every path of a multipath route, as the lookup for it may land on another path than the one the flow is on. But the mtu is still checked against that one path only, by the early returns on the looked-up dst: - a path whose mtu or pmtu is lower gets a pmtu above what it can carry, its flows blackhole until the exception expires - a mtu locked for being below ip_rt_min_pmtu is written as is, over a lower device mtu or locked pmtu - a looked-up path narrower than the mtu, or already at it, ends the update before the path of the flow has learned anything Check the mtu against each path instead, what it uses now as ipv4_mtu() reports it: cache the mtu only where it is lower, cap a locked mtu to it, leave a locked pmtu alone. The checks on the dst stay for the other callers, sockets and tunnels, whose dst is the path of their flow. A Fragmentation Needed that changes nothing costs the ICMP callers a fib lookup and a look at the exception of each path. Fixes: 7d3f3b4367f3 ("net: ipv4: Cache pmtu for all packet paths if multipath enabled") Signed-off-by: Vladimir Vdovin --- net/ipv4/route.c | 90 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/net/ipv4/route.c b/net/ipv4/route.c index d0a5f6d77094..b90eed715fb5 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -150,6 +150,8 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst, struct sk_buff *skb, const void *daddr); static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr); +static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc, + __be32 daddr); static struct dst_ops ipv4_dst_ops = { .family = AF_INET, @@ -1046,54 +1048,87 @@ out: kfree_skb_reason(skb, reason); return 0; } -static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu) +/* The pmtu to cache on a path for the mtu of a Fragmentation Needed, 0 to + * leave the path alone: what it uses now, as ipv4_mtu() reports it, is not + * above the mtu, or its pmtu is locked. An exception about to expire is + * refreshed, a locked mtu is capped at what the path uses. + */ +static u32 fib_nhc_pmtu(struct fib_nh_common *nhc, const struct fib_info *fi, + __be32 daddr, u32 mtu, bool lock) +{ + struct fib_nh_exception *fnhe = find_exception(nhc, daddr); + struct net *net = dev_net(nhc->nhc_dev); + u32 path_mtu = 0; + + if (fnhe && fnhe->fnhe_genid == fnhe_genid(net) && + time_before(jiffies, fnhe->fnhe_expires)) { + if (fnhe->fnhe_mtu_locked) + return 0; + if (!lock && fnhe->fnhe_pmtu == mtu && + time_before(jiffies, fnhe->fnhe_expires - + net->ipv4.ip_rt_mtu_expires / 2)) + return 0; + path_mtu = fnhe->fnhe_pmtu; + } + if (!path_mtu) + path_mtu = fi->fib_mtu; + if (!path_mtu) + path_mtu = READ_ONCE(nhc->nhc_dev->mtu); + path_mtu = min(path_mtu, IP_MAX_MTU); + + if (lock) + return min(mtu, path_mtu); + return mtu > path_mtu ? 0 : mtu; +} + +static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu, + bool from_lookup) { struct dst_entry *dst = &rt->dst; struct fib_result res; bool lock = false; struct net *net; - u32 old_mtu; - if (ip_mtu_locked(dst)) + if (dst_metric_locked(dst, RTAX_MTU)) return; - old_mtu = ipv4_mtu(dst); - if (old_mtu < mtu) + /* A dst looked up for the Fragmentation Needed, without the ports + * the packet was hashed on, may be another path of a multipath + * route than the flow is on: don't judge the mtu by it, each path + * does that below. + */ + if (!from_lookup && (rt->rt_mtu_locked || ipv4_mtu(dst) < mtu)) return; rcu_read_lock(); net = dst_dev_net_rcu(dst); if (mtu < net->ipv4.ip_rt_min_pmtu) { lock = true; - mtu = min(old_mtu, net->ipv4.ip_rt_min_pmtu); + mtu = net->ipv4.ip_rt_min_pmtu; } - if (rt->rt_pmtu == mtu && !lock && + if (!from_lookup && rt->rt_pmtu == mtu && !lock && time_before(jiffies, READ_ONCE(dst->expires) - net->ipv4.ip_rt_mtu_expires / 2)) goto out; if (fib_lookup(net, fl4, &res, 0) == 0) { struct fib_nh_common *nhc; + unsigned long expires; + u32 pmtu; + int nhsel; fib_select_path(net, &res, fl4, NULL); -#ifdef CONFIG_IP_ROUTE_MULTIPATH - if (fib_info_num_path(res.fi) > 1) { - int nhsel; - - for (nhsel = 0; nhsel < fib_info_num_path(res.fi); nhsel++) { - nhc = fib_info_nhc(res.fi, nhsel); - if (!nhc) - break; - update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock, - jiffies + net->ipv4.ip_rt_mtu_expires); - } - goto out; + expires = jiffies + net->ipv4.ip_rt_mtu_expires; + for (nhsel = 0; nhsel < fib_info_num_path(res.fi); nhsel++) { + nhc = fib_info_nhc(res.fi, nhsel); + if (!nhc) + break; + pmtu = fib_nhc_pmtu(nhc, res.fi, fl4->daddr, mtu, lock); + if (pmtu) + update_or_create_fnhe(nhc, fl4->daddr, 0, pmtu, + lock, expires); } -#endif /* CONFIG_IP_ROUTE_MULTIPATH */ - nhc = FIB_RES_NHC(res); - update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock, - jiffies + net->ipv4.ip_rt_mtu_expires); } out: rcu_read_unlock(); @@ -1112,7 +1147,7 @@ static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, if (skb && netif_is_any_bridge_port(skb->dev)) fl4.flowi4_oif = 0; - __ip_rt_update_pmtu(rt, &fl4, mtu); + __ip_rt_update_pmtu(rt, &fl4, mtu, false); } void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, @@ -1127,7 +1162,7 @@ void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, 0); rt = __ip_route_output_key(net, &fl4); if (!IS_ERR(rt)) { - __ip_rt_update_pmtu(rt, &fl4, mtu); + __ip_rt_update_pmtu(rt, &fl4, mtu, true); ip_rt_put(rt); } } @@ -1146,7 +1181,7 @@ static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) rt = __ip_route_output_key(sock_net(sk), &fl4); if (!IS_ERR(rt)) { - __ip_rt_update_pmtu(rt, &fl4, mtu); + __ip_rt_update_pmtu(rt, &fl4, mtu, true); ip_rt_put(rt); } } @@ -1183,7 +1218,8 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) new = true; } - __ip_rt_update_pmtu(dst_rtable(xfrm_dst_path(&rt->dst)), &fl4, mtu); + __ip_rt_update_pmtu(dst_rtable(xfrm_dst_path(&rt->dst)), &fl4, mtu, + new); if (!dst_check(&rt->dst, 0)) { if (new) -- 2.56.0