Skip to content

Commit c92f5ca

Browse files
Julian Anastasovdavem330
authored andcommitted
ipvs: Remove all remaining references to rt->rt_{src,dst}
Remove all remaining references to rt->rt_{src,dst} by using dest->dst_saddr to cache saddr (used for TUN mode). For ICMP in FORWARD hook just restrict the rt_mode for NAT to disable LOCALNODE. All other modes do not allow IP_VS_RT_MODE_RDR, so we should be safe with the ICMP forwarding. Using cp->daddr as replacement for rt_dst is safe for all modes except BYPASS, even when cp->dest is NULL because it is cp->daddr that is used to assign cp->dest for sync-ed connections. Signed-off-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 44e3125 commit c92f5ca

3 files changed

Lines changed: 54 additions & 51 deletions

File tree

‎include/net/ip_vs.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,7 @@ struct ip_vs_dest {
665665
struct dst_entry *dst_cache; /* destination cache entry */
666666
u32 dst_rtos; /* RT_TOS(tos) for dst */
667667
u32 dst_cookie;
668-
#ifdef CONFIG_IP_VS_IPV6
669-
struct in6_addr dst_saddr;
670-
#endif
668+
union nf_inet_addr dst_saddr;
671669

672670
/* for virtual service */
673671
struct ip_vs_service *svc; /* service it belongs to */
@@ -1253,7 +1251,8 @@ extern int ip_vs_tunnel_xmit
12531251
extern int ip_vs_dr_xmit
12541252
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
12551253
extern int ip_vs_icmp_xmit
1256-
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
1254+
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
1255+
int offset, unsigned int hooknum);
12571256
extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
12581257

12591258
#ifdef CONFIG_IP_VS_IPV6
@@ -1267,7 +1266,7 @@ extern int ip_vs_dr_xmit_v6
12671266
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
12681267
extern int ip_vs_icmp_xmit_v6
12691268
(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
1270-
int offset);
1269+
int offset, unsigned int hooknum);
12711270
#endif
12721271

12731272
#ifdef CONFIG_SYSCTL

‎net/netfilter/ipvs/ip_vs_core.c‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,15 +1382,7 @@ ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
13821382
ip_vs_in_stats(cp, skb);
13831383
if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
13841384
offset += 2 * sizeof(__u16);
1385-
verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1386-
/* LOCALNODE from FORWARD hook is not supported */
1387-
if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1388-
skb_rtable(skb)->rt_flags & RTCF_LOCAL) {
1389-
IP_VS_DBG(1, "%s(): "
1390-
"local delivery to %pI4 but in FORWARD\n",
1391-
__func__, &skb_rtable(skb)->rt_dst);
1392-
verdict = NF_DROP;
1393-
}
1385+
verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum);
13941386

13951387
out:
13961388
__ip_vs_conn_put(cp);
@@ -1412,7 +1404,6 @@ ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
14121404
struct ip_vs_protocol *pp;
14131405
struct ip_vs_proto_data *pd;
14141406
unsigned int offset, verdict;
1415-
struct rt6_info *rt;
14161407

14171408
*related = 1;
14181409

@@ -1474,23 +1465,12 @@ ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
14741465
if (!cp)
14751466
return NF_ACCEPT;
14761467

1477-
verdict = NF_DROP;
1478-
14791468
/* do the statistics and put it back */
14801469
ip_vs_in_stats(cp, skb);
14811470
if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
14821471
IPPROTO_SCTP == cih->nexthdr)
14831472
offset += 2 * sizeof(__u16);
1484-
verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1485-
/* LOCALNODE from FORWARD hook is not supported */
1486-
if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1487-
(rt = (struct rt6_info *) skb_dst(skb)) &&
1488-
rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) {
1489-
IP_VS_DBG(1, "%s(): "
1490-
"local delivery to %pI6 but in FORWARD\n",
1491-
__func__, &rt->rt6i_dst);
1492-
verdict = NF_DROP;
1493-
}
1473+
verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum);
14941474

14951475
__ip_vs_conn_put(cp);
14961476

‎net/netfilter/ipvs/ip_vs_xmit.c‎

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
8787
/* Get route to destination or remote server */
8888
static struct rtable *
8989
__ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
90-
__be32 daddr, u32 rtos, int rt_mode)
90+
__be32 daddr, u32 rtos, int rt_mode, __be32 *ret_saddr)
9191
{
9292
struct net *net = dev_net(skb_dst(skb)->dev);
9393
struct rtable *rt; /* Route to the other host */
@@ -98,27 +98,43 @@ __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
9898
spin_lock(&dest->dst_lock);
9999
if (!(rt = (struct rtable *)
100100
__ip_vs_dst_check(dest, rtos))) {
101-
rt = ip_route_output(net, dest->addr.ip, 0, rtos, 0);
101+
struct flowi4 fl4;
102+
103+
memset(&fl4, 0, sizeof(fl4));
104+
fl4.daddr = dest->addr.ip;
105+
fl4.flowi4_tos = rtos;
106+
rt = ip_route_output_key(net, &fl4);
102107
if (IS_ERR(rt)) {
103108
spin_unlock(&dest->dst_lock);
104109
IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
105110
&dest->addr.ip);
106111
return NULL;
107112
}
108113
__ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
109-
IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
110-
&dest->addr.ip,
114+
dest->dst_saddr.ip = fl4.saddr;
115+
IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d, "
116+
"rtos=%X\n",
117+
&dest->addr.ip, &dest->dst_saddr.ip,
111118
atomic_read(&rt->dst.__refcnt), rtos);
112119
}
113120
daddr = dest->addr.ip;
121+
if (ret_saddr)
122+
*ret_saddr = dest->dst_saddr.ip;
114123
spin_unlock(&dest->dst_lock);
115124
} else {
116-
rt = ip_route_output(net, daddr, 0, rtos, 0);
125+
struct flowi4 fl4;
126+
127+
memset(&fl4, 0, sizeof(fl4));
128+
fl4.daddr = daddr;
129+
fl4.flowi4_tos = rtos;
130+
rt = ip_route_output_key(net, &fl4);
117131
if (IS_ERR(rt)) {
118132
IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
119133
&daddr);
120134
return NULL;
121135
}
136+
if (ret_saddr)
137+
*ret_saddr = fl4.saddr;
122138
}
123139

124140
local = rt->rt_flags & RTCF_LOCAL;
@@ -249,7 +265,7 @@ __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
249265
u32 cookie;
250266

251267
dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
252-
&dest->dst_saddr,
268+
&dest->dst_saddr.in6,
253269
do_xfrm);
254270
if (!dst) {
255271
spin_unlock(&dest->dst_lock);
@@ -259,11 +275,11 @@ __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
259275
cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
260276
__ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
261277
IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
262-
&dest->addr.in6, &dest->dst_saddr,
278+
&dest->addr.in6, &dest->dst_saddr.in6,
263279
atomic_read(&rt->dst.__refcnt));
264280
}
265281
if (ret_saddr)
266-
ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
282+
ipv6_addr_copy(ret_saddr, &dest->dst_saddr.in6);
267283
spin_unlock(&dest->dst_lock);
268284
} else {
269285
dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
@@ -386,7 +402,7 @@ ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
386402
EnterFunction(10);
387403

388404
if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
389-
IP_VS_RT_MODE_NON_LOCAL)))
405+
IP_VS_RT_MODE_NON_LOCAL, NULL)))
390406
goto tx_error_icmp;
391407

392408
/* MTU checking */
@@ -518,7 +534,7 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
518534
RT_TOS(iph->tos),
519535
IP_VS_RT_MODE_LOCAL |
520536
IP_VS_RT_MODE_NON_LOCAL |
521-
IP_VS_RT_MODE_RDR)))
537+
IP_VS_RT_MODE_RDR, NULL)))
522538
goto tx_error_icmp;
523539
local = rt->rt_flags & RTCF_LOCAL;
524540
/*
@@ -540,7 +556,7 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
540556
#endif
541557

542558
/* From world but DNAT to loopback address? */
543-
if (local && ipv4_is_loopback(rt->rt_dst) &&
559+
if (local && ipv4_is_loopback(cp->daddr.ip) &&
544560
rt_is_input_route(skb_rtable(skb))) {
545561
IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
546562
"stopping DNAT to loopback address");
@@ -751,6 +767,7 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
751767
struct ip_vs_protocol *pp)
752768
{
753769
struct rtable *rt; /* Route to the other host */
770+
__be32 saddr; /* Source for tunnel */
754771
struct net_device *tdev; /* Device to other host */
755772
struct iphdr *old_iph = ip_hdr(skb);
756773
u8 tos = old_iph->tos;
@@ -764,7 +781,8 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
764781

765782
if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
766783
RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
767-
IP_VS_RT_MODE_NON_LOCAL)))
784+
IP_VS_RT_MODE_NON_LOCAL,
785+
&saddr)))
768786
goto tx_error_icmp;
769787
if (rt->rt_flags & RTCF_LOCAL) {
770788
ip_rt_put(rt);
@@ -832,8 +850,8 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
832850
iph->frag_off = df;
833851
iph->protocol = IPPROTO_IPIP;
834852
iph->tos = tos;
835-
iph->daddr = rt->rt_dst;
836-
iph->saddr = rt->rt_src;
853+
iph->daddr = cp->daddr.ip;
854+
iph->saddr = saddr;
837855
iph->ttl = old_iph->ttl;
838856
ip_select_ident(iph, &rt->dst, NULL);
839857

@@ -996,7 +1014,7 @@ ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
9961014
if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
9971015
RT_TOS(iph->tos),
9981016
IP_VS_RT_MODE_LOCAL |
999-
IP_VS_RT_MODE_NON_LOCAL)))
1017+
IP_VS_RT_MODE_NON_LOCAL, NULL)))
10001018
goto tx_error_icmp;
10011019
if (rt->rt_flags & RTCF_LOCAL) {
10021020
ip_rt_put(rt);
@@ -1114,12 +1132,13 @@ ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
11141132
*/
11151133
int
11161134
ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1117-
struct ip_vs_protocol *pp, int offset)
1135+
struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
11181136
{
11191137
struct rtable *rt; /* Route to the other host */
11201138
int mtu;
11211139
int rc;
11221140
int local;
1141+
int rt_mode;
11231142

11241143
EnterFunction(10);
11251144

@@ -1140,11 +1159,13 @@ ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
11401159
* mangle and send the packet here (only for VS/NAT)
11411160
*/
11421161

1162+
/* LOCALNODE from FORWARD hook is not supported */
1163+
rt_mode = (hooknum != NF_INET_FORWARD) ?
1164+
IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1165+
IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
11431166
if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
11441167
RT_TOS(ip_hdr(skb)->tos),
1145-
IP_VS_RT_MODE_LOCAL |
1146-
IP_VS_RT_MODE_NON_LOCAL |
1147-
IP_VS_RT_MODE_RDR)))
1168+
rt_mode, NULL)))
11481169
goto tx_error_icmp;
11491170
local = rt->rt_flags & RTCF_LOCAL;
11501171

@@ -1167,7 +1188,7 @@ ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
11671188
#endif
11681189

11691190
/* From world but DNAT to loopback address? */
1170-
if (local && ipv4_is_loopback(rt->rt_dst) &&
1191+
if (local && ipv4_is_loopback(cp->daddr.ip) &&
11711192
rt_is_input_route(skb_rtable(skb))) {
11721193
IP_VS_DBG(1, "%s(): "
11731194
"stopping DNAT to loopback %pI4\n",
@@ -1232,12 +1253,13 @@ ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
12321253
#ifdef CONFIG_IP_VS_IPV6
12331254
int
12341255
ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1235-
struct ip_vs_protocol *pp, int offset)
1256+
struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
12361257
{
12371258
struct rt6_info *rt; /* Route to the other host */
12381259
int mtu;
12391260
int rc;
12401261
int local;
1262+
int rt_mode;
12411263

12421264
EnterFunction(10);
12431265

@@ -1258,10 +1280,12 @@ ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
12581280
* mangle and send the packet here (only for VS/NAT)
12591281
*/
12601282

1283+
/* LOCALNODE from FORWARD hook is not supported */
1284+
rt_mode = (hooknum != NF_INET_FORWARD) ?
1285+
IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1286+
IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
12611287
if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1262-
0, (IP_VS_RT_MODE_LOCAL |
1263-
IP_VS_RT_MODE_NON_LOCAL |
1264-
IP_VS_RT_MODE_RDR))))
1288+
0, rt_mode)))
12651289
goto tx_error_icmp;
12661290

12671291
local = __ip_vs_is_local_route6(rt);

0 commit comments

Comments
 (0)