Skip to content

Commit 87a2496

Browse files
Julian Anastasovgregkh
authored andcommitted
ipv4: introduce rt_uses_gateway
[ Upstream commit 155e833 ] Add new flag to remember when route is via gateway. We will use it to allow rt_gateway to contain address of directly connected host for the cases when DST_NOCACHE is used or when the NH exception caches per-destination route without DST_NOCACHE flag, i.e. when routes are not used for other destinations. By this way we force the neighbour resolving to work with the routed destination but we can use different address in the packet, feature needed for IPVS-DR where original packet for virtual IP is routed via route to real IP. Signed-off-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c82f64b commit 87a2496

6 files changed

Lines changed: 34 additions & 28 deletions

File tree

‎include/net/route.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ struct rtable {
4848
int rt_genid;
4949
unsigned int rt_flags;
5050
__u16 rt_type;
51-
__u16 rt_is_input;
51+
__u8 rt_is_input;
52+
__u8 rt_uses_gateway;
5253

5354
int rt_iif;
5455

‎net/ipv4/inet_connection_sock.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ struct dst_entry *inet_csk_route_req(struct sock *sk,
386386
rt = ip_route_output_flow(net, fl4, sk);
387387
if (IS_ERR(rt))
388388
goto no_route;
389-
if (opt && opt->opt.is_strictroute && rt->rt_gateway)
389+
if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
390390
goto route_err;
391391
return &rt->dst;
392392

@@ -422,7 +422,7 @@ struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
422422
rt = ip_route_output_flow(net, fl4, sk);
423423
if (IS_ERR(rt))
424424
goto no_route;
425-
if (opt && opt->opt.is_strictroute && rt->rt_gateway)
425+
if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
426426
goto route_err;
427427
rcu_read_unlock();
428428
return &rt->dst;

‎net/ipv4/ip_forward.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int ip_forward(struct sk_buff *skb)
8585

8686
rt = skb_rtable(skb);
8787

88-
if (opt->is_strictroute && rt->rt_gateway)
88+
if (opt->is_strictroute && rt->rt_uses_gateway)
8989
goto sr_failed;
9090

9191
if (unlikely(skb->len > dst_mtu(&rt->dst) && !skb_is_gso(skb) &&

‎net/ipv4/ip_output.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static inline int ip_finish_output2(struct sk_buff *skb)
193193
}
194194

195195
rcu_read_lock_bh();
196-
nexthop = rt->rt_gateway ? rt->rt_gateway : ip_hdr(skb)->daddr;
196+
nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
197197
neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
198198
if (unlikely(!neigh))
199199
neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
@@ -371,7 +371,7 @@ int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl)
371371
skb_dst_set_noref(skb, &rt->dst);
372372

373373
packet_routed:
374-
if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_gateway)
374+
if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
375375
goto no_route;
376376

377377
/* OK, we know where to send it, allocate and build IP header. */

‎net/ipv4/route.c‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ static unsigned int ipv4_mtu(const struct dst_entry *dst)
11291129
mtu = dst->dev->mtu;
11301130

11311131
if (unlikely(dst_metric_locked(dst, RTAX_MTU))) {
1132-
if (rt->rt_gateway && mtu > 576)
1132+
if (rt->rt_uses_gateway && mtu > 576)
11331133
mtu = 576;
11341134
}
11351135

@@ -1180,7 +1180,9 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
11801180
if (fnhe->fnhe_gw) {
11811181
rt->rt_flags |= RTCF_REDIRECTED;
11821182
rt->rt_gateway = fnhe->fnhe_gw;
1183-
}
1183+
rt->rt_uses_gateway = 1;
1184+
} else if (!rt->rt_gateway)
1185+
rt->rt_gateway = daddr;
11841186

11851187
orig = rcu_dereference(fnhe->fnhe_rth);
11861188
rcu_assign_pointer(fnhe->fnhe_rth, rt);
@@ -1189,13 +1191,6 @@ static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
11891191

11901192
fnhe->fnhe_stamp = jiffies;
11911193
ret = true;
1192-
} else {
1193-
/* Routes we intend to cache in nexthop exception have
1194-
* the DST_NOCACHE bit clear. However, if we are
1195-
* unsuccessful at storing this route into the cache
1196-
* we really need to set it.
1197-
*/
1198-
rt->dst.flags |= DST_NOCACHE;
11991194
}
12001195
spin_unlock_bh(&fnhe_lock);
12011196

@@ -1218,15 +1213,8 @@ static bool rt_cache_route(struct fib_nh *nh, struct rtable *rt)
12181213
if (prev == orig) {
12191214
if (orig)
12201215
rt_free(orig);
1221-
} else {
1222-
/* Routes we intend to cache in the FIB nexthop have
1223-
* the DST_NOCACHE bit clear. However, if we are
1224-
* unsuccessful at storing this route into the cache
1225-
* we really need to set it.
1226-
*/
1227-
rt->dst.flags |= DST_NOCACHE;
1216+
} else
12281217
ret = false;
1229-
}
12301218

12311219
return ret;
12321220
}
@@ -1287,8 +1275,10 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12871275
if (fi) {
12881276
struct fib_nh *nh = &FIB_RES_NH(*res);
12891277

1290-
if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
1278+
if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK) {
12911279
rt->rt_gateway = nh->nh_gw;
1280+
rt->rt_uses_gateway = 1;
1281+
}
12921282
dst_init_metrics(&rt->dst, fi->fib_metrics, true);
12931283
#ifdef CONFIG_IP_ROUTE_CLASSID
12941284
rt->dst.tclassid = nh->nh_tclassid;
@@ -1297,8 +1287,18 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12971287
cached = rt_bind_exception(rt, fnhe, daddr);
12981288
else if (!(rt->dst.flags & DST_NOCACHE))
12991289
cached = rt_cache_route(nh, rt);
1300-
}
1301-
if (unlikely(!cached))
1290+
if (unlikely(!cached)) {
1291+
/* Routes we intend to cache in nexthop exception or
1292+
* FIB nexthop have the DST_NOCACHE bit clear.
1293+
* However, if we are unsuccessful at storing this
1294+
* route into the cache we really need to set it.
1295+
*/
1296+
rt->dst.flags |= DST_NOCACHE;
1297+
if (!rt->rt_gateway)
1298+
rt->rt_gateway = daddr;
1299+
rt_add_uncached_list(rt);
1300+
}
1301+
} else
13021302
rt_add_uncached_list(rt);
13031303

13041304
#ifdef CONFIG_IP_ROUTE_CLASSID
@@ -1366,6 +1366,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
13661366
rth->rt_iif = 0;
13671367
rth->rt_pmtu = 0;
13681368
rth->rt_gateway = 0;
1369+
rth->rt_uses_gateway = 0;
13691370
INIT_LIST_HEAD(&rth->rt_uncached);
13701371
if (our) {
13711372
rth->dst.input= ip_local_deliver;
@@ -1435,7 +1436,6 @@ static int __mkroute_input(struct sk_buff *skb,
14351436
return -EINVAL;
14361437
}
14371438

1438-
14391439
err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
14401440
in_dev->dev, in_dev, &itag);
14411441
if (err < 0) {
@@ -1491,6 +1491,7 @@ static int __mkroute_input(struct sk_buff *skb,
14911491
rth->rt_iif = 0;
14921492
rth->rt_pmtu = 0;
14931493
rth->rt_gateway = 0;
1494+
rth->rt_uses_gateway = 0;
14941495
INIT_LIST_HEAD(&rth->rt_uncached);
14951496

14961497
rth->dst.input = ip_forward;
@@ -1658,6 +1659,7 @@ out: return err;
16581659
rth->rt_iif = 0;
16591660
rth->rt_pmtu = 0;
16601661
rth->rt_gateway = 0;
1662+
rth->rt_uses_gateway = 0;
16611663
INIT_LIST_HEAD(&rth->rt_uncached);
16621664
if (res.type == RTN_UNREACHABLE) {
16631665
rth->dst.input= ip_error;
@@ -1826,6 +1828,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
18261828
rth->rt_iif = orig_oif ? : 0;
18271829
rth->rt_pmtu = 0;
18281830
rth->rt_gateway = 0;
1831+
rth->rt_uses_gateway = 0;
18291832
INIT_LIST_HEAD(&rth->rt_uncached);
18301833

18311834
RT_CACHE_STAT_INC(out_slow_tot);
@@ -2104,6 +2107,7 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_or
21042107
rt->rt_flags = ort->rt_flags;
21052108
rt->rt_type = ort->rt_type;
21062109
rt->rt_gateway = ort->rt_gateway;
2110+
rt->rt_uses_gateway = ort->rt_uses_gateway;
21072111

21082112
INIT_LIST_HEAD(&rt->rt_uncached);
21092113

@@ -2182,7 +2186,7 @@ static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
21822186
if (nla_put_be32(skb, RTA_PREFSRC, fl4->saddr))
21832187
goto nla_put_failure;
21842188
}
2185-
if (rt->rt_gateway &&
2189+
if (rt->rt_uses_gateway &&
21862190
nla_put_be32(skb, RTA_GATEWAY, rt->rt_gateway))
21872191
goto nla_put_failure;
21882192

‎net/ipv4/xfrm4_policy.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
9191
RTCF_LOCAL);
9292
xdst->u.rt.rt_type = rt->rt_type;
9393
xdst->u.rt.rt_gateway = rt->rt_gateway;
94+
xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway;
9495
xdst->u.rt.rt_pmtu = rt->rt_pmtu;
9596
INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
9697

0 commit comments

Comments
 (0)