]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/netfilter/ipvs/ip_vs_xmit.c
ipvs: fixed sparse warning
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / ipvs / ip_vs_xmit.c
CommitLineData
1da177e4
LT
1/*
2 * ip_vs_xmit.c: various packet transmitters for IPVS
3 *
1da177e4
LT
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Changes:
13 *
cb59155f
JA
14 * Description of forwarding methods:
15 * - all transmitters are called from LOCAL_IN (remote clients) and
16 * LOCAL_OUT (local clients) but for ICMP can be called from FORWARD
17 * - not all connections have destination server, for example,
18 * connections in backup server when fwmark is used
19 * - bypass connections use daddr from packet
20 * LOCAL_OUT rules:
21 * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING)
22 * - skb->pkt_type is not set yet
23 * - the only place where we can see skb->sk != NULL
1da177e4
LT
24 */
25
9aada7ac
HE
26#define KMSG_COMPONENT "IPVS"
27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
1da177e4 29#include <linux/kernel.h>
5a0e3ad6 30#include <linux/slab.h>
1da177e4 31#include <linux/tcp.h> /* for tcphdr */
c439cb2e 32#include <net/ip.h>
1da177e4
LT
33#include <net/tcp.h> /* for csum_tcpudp_magic */
34#include <net/udp.h>
35#include <net/icmp.h> /* for icmp_send */
36#include <net/route.h> /* for ip_route_output */
38cdcc9a
JV
37#include <net/ipv6.h>
38#include <net/ip6_route.h>
714f095f 39#include <net/addrconf.h>
38cdcc9a 40#include <linux/icmpv6.h>
1da177e4
LT
41#include <linux/netfilter.h>
42#include <linux/netfilter_ipv4.h>
43
44#include <net/ip_vs.h>
45
17a8f8e3
CG
46enum {
47 IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */
48 IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */
49 IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to
50 * local
51 */
52};
1da177e4
LT
53
54/*
55 * Destination cache to speed up outgoing route lookup
56 */
57static inline void
714f095f
HS
58__ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst,
59 u32 dst_cookie)
1da177e4
LT
60{
61 struct dst_entry *old_dst;
62
63 old_dst = dest->dst_cache;
64 dest->dst_cache = dst;
65 dest->dst_rtos = rtos;
714f095f 66 dest->dst_cookie = dst_cookie;
1da177e4
LT
67 dst_release(old_dst);
68}
69
70static inline struct dst_entry *
714f095f 71__ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
1da177e4
LT
72{
73 struct dst_entry *dst = dest->dst_cache;
74
75 if (!dst)
76 return NULL;
714f095f
HS
77 if ((dst->obsolete || rtos != dest->dst_rtos) &&
78 dst->ops->check(dst, dest->dst_cookie) == NULL) {
1da177e4
LT
79 dest->dst_cache = NULL;
80 dst_release(dst);
81 return NULL;
82 }
83 dst_hold(dst);
84 return dst;
85}
86
17a8f8e3 87/* Get route to destination or remote server */
ad1b30b1 88static struct rtable *
fc604767 89__ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
c92f5ca2 90 __be32 daddr, u32 rtos, int rt_mode, __be32 *ret_saddr)
1da177e4 91{
fc604767 92 struct net *net = dev_net(skb_dst(skb)->dev);
1da177e4 93 struct rtable *rt; /* Route to the other host */
fc604767
JA
94 struct rtable *ort; /* Original route */
95 int local;
1da177e4
LT
96
97 if (dest) {
98 spin_lock(&dest->dst_lock);
99 if (!(rt = (struct rtable *)
714f095f 100 __ip_vs_dst_check(dest, rtos))) {
c92f5ca2
JA
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);
b23dd4fe 107 if (IS_ERR(rt)) {
1da177e4 108 spin_unlock(&dest->dst_lock);
14d5e834
HH
109 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
110 &dest->addr.ip);
1da177e4
LT
111 return NULL;
112 }
714f095f 113 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
c92f5ca2
JA
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,
d8d1f30b 118 atomic_read(&rt->dst.__refcnt), rtos);
1da177e4 119 }
44e3125c 120 daddr = dest->addr.ip;
c92f5ca2
JA
121 if (ret_saddr)
122 *ret_saddr = dest->dst_saddr.ip;
1da177e4
LT
123 spin_unlock(&dest->dst_lock);
124 } else {
c92f5ca2
JA
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);
b23dd4fe 131 if (IS_ERR(rt)) {
14d5e834 132 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
fc604767 133 &daddr);
1da177e4
LT
134 return NULL;
135 }
c92f5ca2
JA
136 if (ret_saddr)
137 *ret_saddr = fl4.saddr;
1da177e4
LT
138 }
139
fc604767 140 local = rt->rt_flags & RTCF_LOCAL;
17a8f8e3
CG
141 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
142 rt_mode)) {
fc604767
JA
143 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
144 (rt->rt_flags & RTCF_LOCAL) ?
44e3125c 145 "local":"non-local", &daddr);
fc604767
JA
146 ip_rt_put(rt);
147 return NULL;
148 }
17a8f8e3
CG
149 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
150 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
fc604767
JA
151 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
152 "requires NAT method, dest: %pI4\n",
44e3125c 153 &ip_hdr(skb)->daddr, &daddr);
fc604767
JA
154 ip_rt_put(rt);
155 return NULL;
156 }
157 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
158 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
159 "to non-local address, dest: %pI4\n",
44e3125c 160 &ip_hdr(skb)->saddr, &daddr);
fc604767
JA
161 ip_rt_put(rt);
162 return NULL;
163 }
164
1da177e4
LT
165 return rt;
166}
167
fc604767
JA
168/* Reroute packet to local IPv4 stack after DNAT */
169static int
170__ip_vs_reroute_locally(struct sk_buff *skb)
171{
172 struct rtable *rt = skb_rtable(skb);
173 struct net_device *dev = rt->dst.dev;
174 struct net *net = dev_net(dev);
175 struct iphdr *iph = ip_hdr(skb);
176
c7537967 177 if (rt_is_input_route(rt)) {
fc604767
JA
178 unsigned long orefdst = skb->_skb_refdst;
179
180 if (ip_route_input(skb, iph->daddr, iph->saddr,
181 iph->tos, skb->dev))
182 return 0;
183 refdst_drop(orefdst);
184 } else {
9d6ec938
DM
185 struct flowi4 fl4 = {
186 .daddr = iph->daddr,
187 .saddr = iph->saddr,
188 .flowi4_tos = RT_TOS(iph->tos),
189 .flowi4_mark = skb->mark,
fc604767 190 };
fc604767 191
9d6ec938 192 rt = ip_route_output_key(net, &fl4);
b23dd4fe 193 if (IS_ERR(rt))
fc604767
JA
194 return 0;
195 if (!(rt->rt_flags & RTCF_LOCAL)) {
196 ip_rt_put(rt);
197 return 0;
198 }
199 /* Drop old route. */
200 skb_dst_drop(skb);
201 skb_dst_set(skb, &rt->dst);
202 }
203 return 1;
204}
205
38cdcc9a 206#ifdef CONFIG_IP_VS_IPV6
714f095f 207
fc604767
JA
208static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
209{
d1918542 210 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
fc604767
JA
211}
212
714f095f
HS
213static struct dst_entry *
214__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
215 struct in6_addr *ret_saddr, int do_xfrm)
216{
217 struct dst_entry *dst;
4c9483b2
DM
218 struct flowi6 fl6 = {
219 .daddr = *daddr,
714f095f
HS
220 };
221
4c9483b2 222 dst = ip6_route_output(net, NULL, &fl6);
714f095f
HS
223 if (dst->error)
224 goto out_err;
225 if (!ret_saddr)
226 return dst;
4c9483b2 227 if (ipv6_addr_any(&fl6.saddr) &&
714f095f 228 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
4c9483b2 229 &fl6.daddr, 0, &fl6.saddr) < 0)
714f095f 230 goto out_err;
452edd59 231 if (do_xfrm) {
4c9483b2 232 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
452edd59
DM
233 if (IS_ERR(dst)) {
234 dst = NULL;
235 goto out_err;
236 }
237 }
4e3fd7a0 238 *ret_saddr = fl6.saddr;
714f095f
HS
239 return dst;
240
241out_err:
242 dst_release(dst);
243 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
244 return NULL;
245}
246
fc604767
JA
247/*
248 * Get route to destination or remote server
fc604767 249 */
38cdcc9a 250static struct rt6_info *
fc604767
JA
251__ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
252 struct in6_addr *daddr, struct in6_addr *ret_saddr,
253 int do_xfrm, int rt_mode)
38cdcc9a 254{
fc604767 255 struct net *net = dev_net(skb_dst(skb)->dev);
38cdcc9a 256 struct rt6_info *rt; /* Route to the other host */
fc604767 257 struct rt6_info *ort; /* Original route */
714f095f 258 struct dst_entry *dst;
fc604767 259 int local;
38cdcc9a
JV
260
261 if (dest) {
262 spin_lock(&dest->dst_lock);
714f095f 263 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
38cdcc9a 264 if (!rt) {
714f095f 265 u32 cookie;
38cdcc9a 266
714f095f 267 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
c92f5ca2 268 &dest->dst_saddr.in6,
714f095f
HS
269 do_xfrm);
270 if (!dst) {
38cdcc9a 271 spin_unlock(&dest->dst_lock);
38cdcc9a
JV
272 return NULL;
273 }
714f095f
HS
274 rt = (struct rt6_info *) dst;
275 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
276 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
277 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
c92f5ca2 278 &dest->addr.in6, &dest->dst_saddr.in6,
d8d1f30b 279 atomic_read(&rt->dst.__refcnt));
38cdcc9a 280 }
714f095f 281 if (ret_saddr)
4e3fd7a0 282 *ret_saddr = dest->dst_saddr.in6;
38cdcc9a
JV
283 spin_unlock(&dest->dst_lock);
284 } else {
fc604767 285 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
714f095f 286 if (!dst)
38cdcc9a 287 return NULL;
714f095f 288 rt = (struct rt6_info *) dst;
38cdcc9a
JV
289 }
290
fc604767 291 local = __ip_vs_is_local_route6(rt);
e58b3442
DM
292 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
293 rt_mode)) {
fc604767
JA
294 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
295 local ? "local":"non-local", daddr);
296 dst_release(&rt->dst);
297 return NULL;
298 }
e58b3442 299 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
fc604767
JA
300 !((ort = (struct rt6_info *) skb_dst(skb)) &&
301 __ip_vs_is_local_route6(ort))) {
302 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
303 "requires NAT method, dest: %pI6\n",
304 &ipv6_hdr(skb)->daddr, daddr);
305 dst_release(&rt->dst);
306 return NULL;
307 }
308 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
309 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
310 IPV6_ADDR_LOOPBACK)) {
311 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
312 "to non-local address, dest: %pI6\n",
313 &ipv6_hdr(skb)->saddr, daddr);
314 dst_release(&rt->dst);
315 return NULL;
316 }
317
38cdcc9a
JV
318 return rt;
319}
320#endif
321
1da177e4
LT
322
323/*
324 * Release dest->dst_cache before a dest is removed
325 */
326void
327ip_vs_dst_reset(struct ip_vs_dest *dest)
328{
329 struct dst_entry *old_dst;
330
331 old_dst = dest->dst_cache;
332 dest->dst_cache = NULL;
333 dst_release(old_dst);
334}
335
f4bc17cd
JA
336#define IP_VS_XMIT_TUNNEL(skb, cp) \
337({ \
338 int __ret = NF_ACCEPT; \
339 \
cf356d69 340 (skb)->ipvs_property = 1; \
f4bc17cd 341 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
3c2de2ae 342 __ret = ip_vs_confirm_conntrack(skb); \
f4bc17cd
JA
343 if (__ret == NF_ACCEPT) { \
344 nf_reset(skb); \
4256f1aa 345 skb_forward_csum(skb); \
f4bc17cd
JA
346 } \
347 __ret; \
348})
349
fc604767 350#define IP_VS_XMIT_NAT(pf, skb, cp, local) \
1da177e4 351do { \
cf356d69 352 (skb)->ipvs_property = 1; \
f4bc17cd 353 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
cf356d69 354 ip_vs_notrack(skb); \
f4bc17cd
JA
355 else \
356 ip_vs_update_conntrack(skb, cp, 1); \
fc604767
JA
357 if (local) \
358 return NF_ACCEPT; \
ccc7911f 359 skb_forward_csum(skb); \
38cdcc9a 360 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
f4bc17cd
JA
361 skb_dst(skb)->dev, dst_output); \
362} while (0)
363
fc604767 364#define IP_VS_XMIT(pf, skb, cp, local) \
f4bc17cd 365do { \
cf356d69 366 (skb)->ipvs_property = 1; \
f4bc17cd 367 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
cf356d69 368 ip_vs_notrack(skb); \
fc604767
JA
369 if (local) \
370 return NF_ACCEPT; \
f4bc17cd
JA
371 skb_forward_csum(skb); \
372 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
373 skb_dst(skb)->dev, dst_output); \
1da177e4
LT
374} while (0)
375
376
377/*
378 * NULL transmitter (do nothing except return NF_ACCEPT)
379 */
380int
381ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
382 struct ip_vs_protocol *pp)
383{
384 /* we do not touch skb and do not need pskb ptr */
fc604767 385 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1da177e4
LT
386}
387
388
389/*
390 * Bypass transmitter
391 * Let packets bypass the destination when the destination is not
392 * available, it may be only used in transparent cache cluster.
393 */
394int
395ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
396 struct ip_vs_protocol *pp)
397{
398 struct rtable *rt; /* Route to the other host */
eddc9ec5 399 struct iphdr *iph = ip_hdr(skb);
1da177e4 400 int mtu;
1da177e4
LT
401
402 EnterFunction(10);
403
17a8f8e3 404 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
c92f5ca2 405 IP_VS_RT_MODE_NON_LOCAL, NULL)))
1da177e4 406 goto tx_error_icmp;
1da177e4
LT
407
408 /* MTU checking */
d8d1f30b 409 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
410 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
411 !skb_is_gso(skb)) {
1da177e4
LT
412 ip_rt_put(rt);
413 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 414 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1da177e4
LT
415 goto tx_error;
416 }
417
418 /*
419 * Call ip_send_check because we are not sure it is called
420 * after ip_defrag. Is copy-on-write needed?
421 */
422 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
423 ip_rt_put(rt);
424 return NF_STOLEN;
425 }
eddc9ec5 426 ip_send_check(ip_hdr(skb));
1da177e4
LT
427
428 /* drop old route */
adf30907 429 skb_dst_drop(skb);
d8d1f30b 430 skb_dst_set(skb, &rt->dst);
1da177e4
LT
431
432 /* Another hack: avoid icmp_send in ip_fragment */
433 skb->local_df = 1;
434
fc604767 435 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1da177e4
LT
436
437 LeaveFunction(10);
438 return NF_STOLEN;
439
440 tx_error_icmp:
441 dst_link_failure(skb);
442 tx_error:
443 kfree_skb(skb);
444 LeaveFunction(10);
445 return NF_STOLEN;
446}
447
b3cdd2a7
JV
448#ifdef CONFIG_IP_VS_IPV6
449int
450ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
451 struct ip_vs_protocol *pp)
452{
453 struct rt6_info *rt; /* Route to the other host */
454 struct ipv6hdr *iph = ipv6_hdr(skb);
455 int mtu;
b3cdd2a7
JV
456
457 EnterFunction(10);
458
e58b3442
DM
459 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0,
460 IP_VS_RT_MODE_NON_LOCAL)))
b3cdd2a7 461 goto tx_error_icmp;
b3cdd2a7
JV
462
463 /* MTU checking */
d8d1f30b 464 mtu = dst_mtu(&rt->dst);
8f1b03a4 465 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
466 if (!skb->dev) {
467 struct net *net = dev_net(skb_dst(skb)->dev);
468
469 skb->dev = net->loopback_dev;
470 }
3ffe533c 471 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
cb59155f 472 dst_release(&rt->dst);
1e3e238e 473 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
b3cdd2a7
JV
474 goto tx_error;
475 }
476
477 /*
478 * Call ip_send_check because we are not sure it is called
479 * after ip_defrag. Is copy-on-write needed?
480 */
481 skb = skb_share_check(skb, GFP_ATOMIC);
482 if (unlikely(skb == NULL)) {
d8d1f30b 483 dst_release(&rt->dst);
b3cdd2a7
JV
484 return NF_STOLEN;
485 }
486
487 /* drop old route */
adf30907 488 skb_dst_drop(skb);
d8d1f30b 489 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
490
491 /* Another hack: avoid icmp_send in ip_fragment */
492 skb->local_df = 1;
493
fc604767 494 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
b3cdd2a7
JV
495
496 LeaveFunction(10);
497 return NF_STOLEN;
498
499 tx_error_icmp:
500 dst_link_failure(skb);
501 tx_error:
502 kfree_skb(skb);
503 LeaveFunction(10);
504 return NF_STOLEN;
505}
506#endif
1da177e4
LT
507
508/*
509 * NAT transmitter (only for outside-to-inside nat forwarding)
510 * Not used for related ICMP
511 */
512int
513ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
514 struct ip_vs_protocol *pp)
515{
516 struct rtable *rt; /* Route to the other host */
517 int mtu;
eddc9ec5 518 struct iphdr *iph = ip_hdr(skb);
fc604767 519 int local;
1da177e4
LT
520
521 EnterFunction(10);
522
523 /* check if it is a connection of no-client-port */
524 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
014d730d 525 __be16 _pt, *p;
1da177e4
LT
526 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
527 if (p == NULL)
528 goto tx_error;
529 ip_vs_conn_fill_cport(cp, *p);
530 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
531 }
532
fc604767 533 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
534 RT_TOS(iph->tos),
535 IP_VS_RT_MODE_LOCAL |
536 IP_VS_RT_MODE_NON_LOCAL |
c92f5ca2 537 IP_VS_RT_MODE_RDR, NULL)))
1da177e4 538 goto tx_error_icmp;
fc604767
JA
539 local = rt->rt_flags & RTCF_LOCAL;
540 /*
541 * Avoid duplicate tuple in reply direction for NAT traffic
542 * to local address when connection is sync-ed
543 */
c0cd1156 544#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
545 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
546 enum ip_conntrack_info ctinfo;
547 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
548
549 if (ct && !nf_ct_is_untracked(ct)) {
0d79641a
JA
550 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
551 "ip_vs_nat_xmit(): "
fc604767
JA
552 "stopping DNAT to local address");
553 goto tx_error_put;
554 }
555 }
556#endif
557
558 /* From world but DNAT to loopback address? */
c92f5ca2 559 if (local && ipv4_is_loopback(cp->daddr.ip) &&
c7537967 560 rt_is_input_route(skb_rtable(skb))) {
0d79641a 561 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
fc604767
JA
562 "stopping DNAT to loopback address");
563 goto tx_error_put;
564 }
1da177e4
LT
565
566 /* MTU checking */
d8d1f30b 567 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
568 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
569 !skb_is_gso(skb)) {
1da177e4 570 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
0d79641a
JA
571 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
572 "ip_vs_nat_xmit(): frag needed for");
fc604767 573 goto tx_error_put;
1da177e4
LT
574 }
575
576 /* copy-on-write the packet before mangling it */
af1e1cf0 577 if (!skb_make_writable(skb, sizeof(struct iphdr)))
1da177e4
LT
578 goto tx_error_put;
579
d8d1f30b 580 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1da177e4
LT
581 goto tx_error_put;
582
1da177e4 583 /* mangle the packet */
3db05fea 584 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
fc604767 585 goto tx_error_put;
e7ade46a 586 ip_hdr(skb)->daddr = cp->daddr.ip;
eddc9ec5 587 ip_send_check(ip_hdr(skb));
1da177e4 588
fc604767
JA
589 if (!local) {
590 /* drop old route */
591 skb_dst_drop(skb);
592 skb_dst_set(skb, &rt->dst);
593 } else {
594 ip_rt_put(rt);
595 /*
596 * Some IPv4 replies get local address from routes,
597 * not from iph, so while we DNAT after routing
598 * we need this second input/output route.
599 */
600 if (!__ip_vs_reroute_locally(skb))
601 goto tx_error;
602 }
603
0d79641a 604 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
1da177e4
LT
605
606 /* FIXME: when application helper enlarges the packet and the length
607 is larger than the MTU of outgoing device, there will be still
608 MTU problem. */
609
610 /* Another hack: avoid icmp_send in ip_fragment */
611 skb->local_df = 1;
612
fc604767 613 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1da177e4
LT
614
615 LeaveFunction(10);
616 return NF_STOLEN;
617
618 tx_error_icmp:
619 dst_link_failure(skb);
620 tx_error:
1da177e4 621 kfree_skb(skb);
f4bc17cd 622 LeaveFunction(10);
1da177e4
LT
623 return NF_STOLEN;
624 tx_error_put:
625 ip_rt_put(rt);
626 goto tx_error;
627}
628
b3cdd2a7
JV
629#ifdef CONFIG_IP_VS_IPV6
630int
631ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
632 struct ip_vs_protocol *pp)
633{
634 struct rt6_info *rt; /* Route to the other host */
635 int mtu;
fc604767 636 int local;
b3cdd2a7
JV
637
638 EnterFunction(10);
639
640 /* check if it is a connection of no-client-port */
641 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
642 __be16 _pt, *p;
643 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
644 sizeof(_pt), &_pt);
645 if (p == NULL)
646 goto tx_error;
647 ip_vs_conn_fill_cport(cp, *p);
648 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
649 }
650
fc604767 651 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
652 0, (IP_VS_RT_MODE_LOCAL |
653 IP_VS_RT_MODE_NON_LOCAL |
654 IP_VS_RT_MODE_RDR))))
b3cdd2a7 655 goto tx_error_icmp;
fc604767
JA
656 local = __ip_vs_is_local_route6(rt);
657 /*
658 * Avoid duplicate tuple in reply direction for NAT traffic
659 * to local address when connection is sync-ed
660 */
c0cd1156 661#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
662 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
663 enum ip_conntrack_info ctinfo;
664 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
665
666 if (ct && !nf_ct_is_untracked(ct)) {
0d79641a 667 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
fc604767
JA
668 "ip_vs_nat_xmit_v6(): "
669 "stopping DNAT to local address");
670 goto tx_error_put;
671 }
672 }
673#endif
674
675 /* From world but DNAT to loopback address? */
676 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
677 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
0d79641a 678 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
fc604767
JA
679 "ip_vs_nat_xmit_v6(): "
680 "stopping DNAT to loopback address");
681 goto tx_error_put;
682 }
b3cdd2a7
JV
683
684 /* MTU checking */
d8d1f30b 685 mtu = dst_mtu(&rt->dst);
8f1b03a4 686 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
687 if (!skb->dev) {
688 struct net *net = dev_net(skb_dst(skb)->dev);
689
690 skb->dev = net->loopback_dev;
691 }
3ffe533c 692 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
0d79641a 693 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
b3cdd2a7 694 "ip_vs_nat_xmit_v6(): frag needed for");
fc604767 695 goto tx_error_put;
b3cdd2a7
JV
696 }
697
698 /* copy-on-write the packet before mangling it */
699 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
700 goto tx_error_put;
701
d8d1f30b 702 if (skb_cow(skb, rt->dst.dev->hard_header_len))
b3cdd2a7
JV
703 goto tx_error_put;
704
b3cdd2a7
JV
705 /* mangle the packet */
706 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
707 goto tx_error;
4e3fd7a0 708 ipv6_hdr(skb)->daddr = cp->daddr.in6;
fc604767
JA
709
710 if (!local || !skb->dev) {
711 /* drop the old route when skb is not shared */
712 skb_dst_drop(skb);
713 skb_dst_set(skb, &rt->dst);
714 } else {
715 /* destined to loopback, do we need to change route? */
716 dst_release(&rt->dst);
717 }
b3cdd2a7 718
0d79641a 719 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
b3cdd2a7
JV
720
721 /* FIXME: when application helper enlarges the packet and the length
722 is larger than the MTU of outgoing device, there will be still
723 MTU problem. */
724
725 /* Another hack: avoid icmp_send in ip_fragment */
726 skb->local_df = 1;
727
fc604767 728 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
b3cdd2a7
JV
729
730 LeaveFunction(10);
731 return NF_STOLEN;
732
733tx_error_icmp:
734 dst_link_failure(skb);
735tx_error:
736 LeaveFunction(10);
737 kfree_skb(skb);
738 return NF_STOLEN;
739tx_error_put:
d8d1f30b 740 dst_release(&rt->dst);
b3cdd2a7
JV
741 goto tx_error;
742}
743#endif
744
1da177e4
LT
745
746/*
747 * IP Tunneling transmitter
748 *
749 * This function encapsulates the packet in a new IP packet, its
750 * destination will be set to cp->daddr. Most code of this function
751 * is taken from ipip.c.
752 *
753 * It is used in VS/TUN cluster. The load balancer selects a real
754 * server from a cluster based on a scheduling algorithm,
755 * encapsulates the request packet and forwards it to the selected
756 * server. For example, all real servers are configured with
757 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
758 * the encapsulated packet, it will decapsulate the packet, processe
759 * the request and return the response packets directly to the client
760 * without passing the load balancer. This can greatly increase the
761 * scalability of virtual server.
762 *
763 * Used for ANY protocol
764 */
765int
766ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
767 struct ip_vs_protocol *pp)
768{
769 struct rtable *rt; /* Route to the other host */
c92f5ca2 770 __be32 saddr; /* Source for tunnel */
1da177e4 771 struct net_device *tdev; /* Device to other host */
eddc9ec5 772 struct iphdr *old_iph = ip_hdr(skb);
1da177e4 773 u8 tos = old_iph->tos;
76ab608d 774 __be16 df = old_iph->frag_off;
1da177e4 775 struct iphdr *iph; /* Our new IP header */
c2636b4d 776 unsigned int max_headroom; /* The extra header space needed */
1da177e4 777 int mtu;
f4bc17cd 778 int ret;
1da177e4
LT
779
780 EnterFunction(10);
781
fc604767 782 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3 783 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
c92f5ca2
JA
784 IP_VS_RT_MODE_NON_LOCAL,
785 &saddr)))
1da177e4 786 goto tx_error_icmp;
fc604767
JA
787 if (rt->rt_flags & RTCF_LOCAL) {
788 ip_rt_put(rt);
789 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
790 }
1da177e4 791
d8d1f30b 792 tdev = rt->dst.dev;
1da177e4 793
d8d1f30b 794 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
1da177e4 795 if (mtu < 68) {
1e3e238e 796 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
fc604767 797 goto tx_error_put;
1da177e4 798 }
adf30907 799 if (skb_dst(skb))
6700c270 800 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1da177e4 801
4412ec49 802 df |= (old_iph->frag_off & htons(IP_DF));
1da177e4 803
8f1b03a4
SH
804 if ((old_iph->frag_off & htons(IP_DF) &&
805 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
1da177e4 806 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 807 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 808 goto tx_error_put;
1da177e4
LT
809 }
810
811 /*
812 * Okay, now see if we can stuff it in the buffer as-is.
813 */
814 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
815
816 if (skb_headroom(skb) < max_headroom
817 || skb_cloned(skb) || skb_shared(skb)) {
818 struct sk_buff *new_skb =
819 skb_realloc_headroom(skb, max_headroom);
820 if (!new_skb) {
821 ip_rt_put(rt);
822 kfree_skb(skb);
1e3e238e 823 IP_VS_ERR_RL("%s(): no memory\n", __func__);
1da177e4
LT
824 return NF_STOLEN;
825 }
5d0ba55b 826 consume_skb(skb);
1da177e4 827 skb = new_skb;
eddc9ec5 828 old_iph = ip_hdr(skb);
1da177e4
LT
829 }
830
714f095f 831 skb->transport_header = skb->network_header;
1da177e4
LT
832
833 /* fix old IP header checksum */
834 ip_send_check(old_iph);
835
e2d1bca7
ACM
836 skb_push(skb, sizeof(struct iphdr));
837 skb_reset_network_header(skb);
1da177e4
LT
838 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
839
840 /* drop old route */
adf30907 841 skb_dst_drop(skb);
d8d1f30b 842 skb_dst_set(skb, &rt->dst);
1da177e4
LT
843
844 /*
845 * Push down and install the IPIP header.
846 */
eddc9ec5 847 iph = ip_hdr(skb);
1da177e4
LT
848 iph->version = 4;
849 iph->ihl = sizeof(struct iphdr)>>2;
850 iph->frag_off = df;
851 iph->protocol = IPPROTO_IPIP;
852 iph->tos = tos;
c92f5ca2
JA
853 iph->daddr = cp->daddr.ip;
854 iph->saddr = saddr;
1da177e4 855 iph->ttl = old_iph->ttl;
d8d1f30b 856 ip_select_ident(iph, &rt->dst, NULL);
1da177e4
LT
857
858 /* Another hack: avoid icmp_send in ip_fragment */
859 skb->local_df = 1;
860
f4bc17cd
JA
861 ret = IP_VS_XMIT_TUNNEL(skb, cp);
862 if (ret == NF_ACCEPT)
863 ip_local_out(skb);
864 else if (ret == NF_DROP)
865 kfree_skb(skb);
1da177e4
LT
866
867 LeaveFunction(10);
868
869 return NF_STOLEN;
870
871 tx_error_icmp:
872 dst_link_failure(skb);
873 tx_error:
874 kfree_skb(skb);
875 LeaveFunction(10);
876 return NF_STOLEN;
fc604767
JA
877tx_error_put:
878 ip_rt_put(rt);
879 goto tx_error;
1da177e4
LT
880}
881
b3cdd2a7
JV
882#ifdef CONFIG_IP_VS_IPV6
883int
884ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
885 struct ip_vs_protocol *pp)
886{
887 struct rt6_info *rt; /* Route to the other host */
714f095f 888 struct in6_addr saddr; /* Source for tunnel */
b3cdd2a7
JV
889 struct net_device *tdev; /* Device to other host */
890 struct ipv6hdr *old_iph = ipv6_hdr(skb);
b3cdd2a7
JV
891 struct ipv6hdr *iph; /* Our new IP header */
892 unsigned int max_headroom; /* The extra header space needed */
893 int mtu;
f4bc17cd 894 int ret;
b3cdd2a7
JV
895
896 EnterFunction(10);
897
fc604767 898 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
e58b3442
DM
899 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
900 IP_VS_RT_MODE_NON_LOCAL))))
b3cdd2a7 901 goto tx_error_icmp;
fc604767
JA
902 if (__ip_vs_is_local_route6(rt)) {
903 dst_release(&rt->dst);
904 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
905 }
b3cdd2a7 906
d8d1f30b 907 tdev = rt->dst.dev;
b3cdd2a7 908
d8d1f30b 909 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
714f095f 910 if (mtu < IPV6_MIN_MTU) {
714f095f
HS
911 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
912 IPV6_MIN_MTU);
fc604767 913 goto tx_error_put;
b3cdd2a7 914 }
adf30907 915 if (skb_dst(skb))
6700c270 916 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
b3cdd2a7 917
8f1b03a4
SH
918 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
919 !skb_is_gso(skb)) {
cb59155f
JA
920 if (!skb->dev) {
921 struct net *net = dev_net(skb_dst(skb)->dev);
922
923 skb->dev = net->loopback_dev;
924 }
3ffe533c 925 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1e3e238e 926 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 927 goto tx_error_put;
b3cdd2a7
JV
928 }
929
930 /*
931 * Okay, now see if we can stuff it in the buffer as-is.
932 */
933 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
934
935 if (skb_headroom(skb) < max_headroom
936 || skb_cloned(skb) || skb_shared(skb)) {
937 struct sk_buff *new_skb =
938 skb_realloc_headroom(skb, max_headroom);
939 if (!new_skb) {
d8d1f30b 940 dst_release(&rt->dst);
b3cdd2a7 941 kfree_skb(skb);
1e3e238e 942 IP_VS_ERR_RL("%s(): no memory\n", __func__);
b3cdd2a7
JV
943 return NF_STOLEN;
944 }
5d0ba55b 945 consume_skb(skb);
b3cdd2a7
JV
946 skb = new_skb;
947 old_iph = ipv6_hdr(skb);
948 }
949
714f095f 950 skb->transport_header = skb->network_header;
b3cdd2a7
JV
951
952 skb_push(skb, sizeof(struct ipv6hdr));
953 skb_reset_network_header(skb);
954 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
955
956 /* drop old route */
adf30907 957 skb_dst_drop(skb);
d8d1f30b 958 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
959
960 /*
961 * Push down and install the IPIP header.
962 */
963 iph = ipv6_hdr(skb);
964 iph->version = 6;
965 iph->nexthdr = IPPROTO_IPV6;
b7b45f47
HH
966 iph->payload_len = old_iph->payload_len;
967 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
b3cdd2a7
JV
968 iph->priority = old_iph->priority;
969 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
4e3fd7a0
AD
970 iph->daddr = cp->daddr.in6;
971 iph->saddr = saddr;
b3cdd2a7
JV
972 iph->hop_limit = old_iph->hop_limit;
973
974 /* Another hack: avoid icmp_send in ip_fragment */
975 skb->local_df = 1;
976
f4bc17cd
JA
977 ret = IP_VS_XMIT_TUNNEL(skb, cp);
978 if (ret == NF_ACCEPT)
979 ip6_local_out(skb);
980 else if (ret == NF_DROP)
981 kfree_skb(skb);
b3cdd2a7
JV
982
983 LeaveFunction(10);
984
985 return NF_STOLEN;
986
987tx_error_icmp:
988 dst_link_failure(skb);
989tx_error:
990 kfree_skb(skb);
991 LeaveFunction(10);
992 return NF_STOLEN;
fc604767
JA
993tx_error_put:
994 dst_release(&rt->dst);
995 goto tx_error;
b3cdd2a7
JV
996}
997#endif
998
1da177e4
LT
999
1000/*
1001 * Direct Routing transmitter
1002 * Used for ANY protocol
1003 */
1004int
1005ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1006 struct ip_vs_protocol *pp)
1007{
1008 struct rtable *rt; /* Route to the other host */
eddc9ec5 1009 struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
1010 int mtu;
1011
1012 EnterFunction(10);
1013
fc604767 1014 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
1015 RT_TOS(iph->tos),
1016 IP_VS_RT_MODE_LOCAL |
c92f5ca2 1017 IP_VS_RT_MODE_NON_LOCAL, NULL)))
1da177e4 1018 goto tx_error_icmp;
fc604767
JA
1019 if (rt->rt_flags & RTCF_LOCAL) {
1020 ip_rt_put(rt);
1021 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1022 }
1da177e4
LT
1023
1024 /* MTU checking */
d8d1f30b 1025 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
1026 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1027 !skb_is_gso(skb)) {
1da177e4
LT
1028 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1029 ip_rt_put(rt);
1e3e238e 1030 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1da177e4
LT
1031 goto tx_error;
1032 }
1033
1034 /*
1035 * Call ip_send_check because we are not sure it is called
1036 * after ip_defrag. Is copy-on-write needed?
1037 */
1038 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1039 ip_rt_put(rt);
1040 return NF_STOLEN;
1041 }
eddc9ec5 1042 ip_send_check(ip_hdr(skb));
1da177e4
LT
1043
1044 /* drop old route */
adf30907 1045 skb_dst_drop(skb);
d8d1f30b 1046 skb_dst_set(skb, &rt->dst);
1da177e4
LT
1047
1048 /* Another hack: avoid icmp_send in ip_fragment */
1049 skb->local_df = 1;
1050
fc604767 1051 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1da177e4
LT
1052
1053 LeaveFunction(10);
1054 return NF_STOLEN;
1055
1056 tx_error_icmp:
1057 dst_link_failure(skb);
1058 tx_error:
1059 kfree_skb(skb);
1060 LeaveFunction(10);
1061 return NF_STOLEN;
1062}
1063
b3cdd2a7
JV
1064#ifdef CONFIG_IP_VS_IPV6
1065int
1066ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1067 struct ip_vs_protocol *pp)
1068{
1069 struct rt6_info *rt; /* Route to the other host */
1070 int mtu;
1071
1072 EnterFunction(10);
1073
fc604767 1074 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
1075 0, (IP_VS_RT_MODE_LOCAL |
1076 IP_VS_RT_MODE_NON_LOCAL))))
b3cdd2a7 1077 goto tx_error_icmp;
fc604767
JA
1078 if (__ip_vs_is_local_route6(rt)) {
1079 dst_release(&rt->dst);
1080 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1081 }
b3cdd2a7
JV
1082
1083 /* MTU checking */
d8d1f30b 1084 mtu = dst_mtu(&rt->dst);
b3cdd2a7 1085 if (skb->len > mtu) {
cb59155f
JA
1086 if (!skb->dev) {
1087 struct net *net = dev_net(skb_dst(skb)->dev);
1088
1089 skb->dev = net->loopback_dev;
1090 }
3ffe533c 1091 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
d8d1f30b 1092 dst_release(&rt->dst);
1e3e238e 1093 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
b3cdd2a7
JV
1094 goto tx_error;
1095 }
1096
1097 /*
1098 * Call ip_send_check because we are not sure it is called
1099 * after ip_defrag. Is copy-on-write needed?
1100 */
1101 skb = skb_share_check(skb, GFP_ATOMIC);
1102 if (unlikely(skb == NULL)) {
d8d1f30b 1103 dst_release(&rt->dst);
b3cdd2a7
JV
1104 return NF_STOLEN;
1105 }
1106
1107 /* drop old route */
adf30907 1108 skb_dst_drop(skb);
d8d1f30b 1109 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
1110
1111 /* Another hack: avoid icmp_send in ip_fragment */
1112 skb->local_df = 1;
1113
fc604767 1114 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
b3cdd2a7
JV
1115
1116 LeaveFunction(10);
1117 return NF_STOLEN;
1118
1119tx_error_icmp:
1120 dst_link_failure(skb);
1121tx_error:
1122 kfree_skb(skb);
1123 LeaveFunction(10);
1124 return NF_STOLEN;
1125}
1126#endif
1127
1da177e4
LT
1128
1129/*
1130 * ICMP packet transmitter
1131 * called by the ip_vs_in_icmp
1132 */
1133int
1134ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
c92f5ca2 1135 struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
1da177e4
LT
1136{
1137 struct rtable *rt; /* Route to the other host */
1138 int mtu;
1139 int rc;
fc604767 1140 int local;
c92f5ca2 1141 int rt_mode;
1da177e4
LT
1142
1143 EnterFunction(10);
1144
1145 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1146 forwarded directly here, because there is no need to
1147 translate address/port back */
1148 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1149 if (cp->packet_xmit)
1150 rc = cp->packet_xmit(skb, cp, pp);
1151 else
1152 rc = NF_ACCEPT;
1153 /* do not touch skb anymore */
1154 atomic_inc(&cp->in_pkts);
1da177e4
LT
1155 goto out;
1156 }
1157
1158 /*
1159 * mangle and send the packet here (only for VS/NAT)
1160 */
1161
c92f5ca2
JA
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;
fc604767 1166 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3 1167 RT_TOS(ip_hdr(skb)->tos),
c92f5ca2 1168 rt_mode, NULL)))
1da177e4 1169 goto tx_error_icmp;
fc604767
JA
1170 local = rt->rt_flags & RTCF_LOCAL;
1171
1172 /*
1173 * Avoid duplicate tuple in reply direction for NAT traffic
1174 * to local address when connection is sync-ed
1175 */
c0cd1156 1176#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
1177 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1178 enum ip_conntrack_info ctinfo;
1179 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1180
1181 if (ct && !nf_ct_is_untracked(ct)) {
1182 IP_VS_DBG(10, "%s(): "
1183 "stopping DNAT to local address %pI4\n",
1184 __func__, &cp->daddr.ip);
1185 goto tx_error_put;
1186 }
1187 }
1188#endif
1189
1190 /* From world but DNAT to loopback address? */
c92f5ca2 1191 if (local && ipv4_is_loopback(cp->daddr.ip) &&
c7537967 1192 rt_is_input_route(skb_rtable(skb))) {
fc604767
JA
1193 IP_VS_DBG(1, "%s(): "
1194 "stopping DNAT to loopback %pI4\n",
1195 __func__, &cp->daddr.ip);
1196 goto tx_error_put;
1197 }
1da177e4
LT
1198
1199 /* MTU checking */
d8d1f30b 1200 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
1201 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1202 !skb_is_gso(skb)) {
1da177e4 1203 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 1204 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 1205 goto tx_error_put;
1da177e4
LT
1206 }
1207
1208 /* copy-on-write the packet before mangling it */
af1e1cf0 1209 if (!skb_make_writable(skb, offset))
1da177e4
LT
1210 goto tx_error_put;
1211
d8d1f30b 1212 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1da177e4
LT
1213 goto tx_error_put;
1214
1da177e4
LT
1215 ip_vs_nat_icmp(skb, pp, cp, 0);
1216
fc604767
JA
1217 if (!local) {
1218 /* drop the old route when skb is not shared */
1219 skb_dst_drop(skb);
1220 skb_dst_set(skb, &rt->dst);
1221 } else {
1222 ip_rt_put(rt);
1223 /*
1224 * Some IPv4 replies get local address from routes,
1225 * not from iph, so while we DNAT after routing
1226 * we need this second input/output route.
1227 */
1228 if (!__ip_vs_reroute_locally(skb))
1229 goto tx_error;
1230 }
1231
1da177e4
LT
1232 /* Another hack: avoid icmp_send in ip_fragment */
1233 skb->local_df = 1;
1234
fc604767 1235 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1da177e4
LT
1236
1237 rc = NF_STOLEN;
1238 goto out;
1239
1240 tx_error_icmp:
1241 dst_link_failure(skb);
1242 tx_error:
1243 dev_kfree_skb(skb);
1244 rc = NF_STOLEN;
1245 out:
1246 LeaveFunction(10);
1247 return rc;
1248 tx_error_put:
1249 ip_rt_put(rt);
1250 goto tx_error;
1251}
b3cdd2a7
JV
1252
1253#ifdef CONFIG_IP_VS_IPV6
1254int
1255ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
c92f5ca2 1256 struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
b3cdd2a7
JV
1257{
1258 struct rt6_info *rt; /* Route to the other host */
1259 int mtu;
1260 int rc;
fc604767 1261 int local;
c92f5ca2 1262 int rt_mode;
b3cdd2a7
JV
1263
1264 EnterFunction(10);
1265
1266 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1267 forwarded directly here, because there is no need to
1268 translate address/port back */
1269 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1270 if (cp->packet_xmit)
1271 rc = cp->packet_xmit(skb, cp, pp);
1272 else
1273 rc = NF_ACCEPT;
1274 /* do not touch skb anymore */
1275 atomic_inc(&cp->in_pkts);
1276 goto out;
1277 }
1278
1279 /*
1280 * mangle and send the packet here (only for VS/NAT)
1281 */
1282
c92f5ca2
JA
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;
fc604767 1287 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
c92f5ca2 1288 0, rt_mode)))
b3cdd2a7
JV
1289 goto tx_error_icmp;
1290
fc604767
JA
1291 local = __ip_vs_is_local_route6(rt);
1292 /*
1293 * Avoid duplicate tuple in reply direction for NAT traffic
1294 * to local address when connection is sync-ed
1295 */
c0cd1156 1296#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
1297 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1298 enum ip_conntrack_info ctinfo;
1299 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1300
1301 if (ct && !nf_ct_is_untracked(ct)) {
1302 IP_VS_DBG(10, "%s(): "
1303 "stopping DNAT to local address %pI6\n",
1304 __func__, &cp->daddr.in6);
1305 goto tx_error_put;
1306 }
1307 }
1308#endif
1309
1310 /* From world but DNAT to loopback address? */
1311 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1312 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1313 IP_VS_DBG(1, "%s(): "
1314 "stopping DNAT to loopback %pI6\n",
1315 __func__, &cp->daddr.in6);
1316 goto tx_error_put;
1317 }
1318
b3cdd2a7 1319 /* MTU checking */
d8d1f30b 1320 mtu = dst_mtu(&rt->dst);
8f1b03a4 1321 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
1322 if (!skb->dev) {
1323 struct net *net = dev_net(skb_dst(skb)->dev);
1324
1325 skb->dev = net->loopback_dev;
1326 }
3ffe533c 1327 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1e3e238e 1328 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 1329 goto tx_error_put;
b3cdd2a7
JV
1330 }
1331
1332 /* copy-on-write the packet before mangling it */
1333 if (!skb_make_writable(skb, offset))
1334 goto tx_error_put;
1335
d8d1f30b 1336 if (skb_cow(skb, rt->dst.dev->hard_header_len))
b3cdd2a7
JV
1337 goto tx_error_put;
1338
b3cdd2a7
JV
1339 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1340
fc604767
JA
1341 if (!local || !skb->dev) {
1342 /* drop the old route when skb is not shared */
1343 skb_dst_drop(skb);
1344 skb_dst_set(skb, &rt->dst);
1345 } else {
1346 /* destined to loopback, do we need to change route? */
1347 dst_release(&rt->dst);
1348 }
1349
b3cdd2a7
JV
1350 /* Another hack: avoid icmp_send in ip_fragment */
1351 skb->local_df = 1;
1352
fc604767 1353 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
b3cdd2a7
JV
1354
1355 rc = NF_STOLEN;
1356 goto out;
1357
1358tx_error_icmp:
1359 dst_link_failure(skb);
1360tx_error:
1361 dev_kfree_skb(skb);
1362 rc = NF_STOLEN;
1363out:
1364 LeaveFunction(10);
1365 return rc;
1366tx_error_put:
d8d1f30b 1367 dst_release(&rt->dst);
b3cdd2a7
JV
1368 goto tx_error;
1369}
1370#endif