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