ipvs: convert the IP_VS_XMIT macros to functions
[firefly-linux-kernel-4.4.55.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 /*
366  *      Release dest->dst_cache before a dest is removed
367  */
368 void
369 ip_vs_dst_reset(struct ip_vs_dest *dest)
370 {
371         struct dst_entry *old_dst;
372
373         old_dst = dest->dst_cache;
374         dest->dst_cache = NULL;
375         dst_release(old_dst);
376         dest->dst_saddr.ip = 0;
377 }
378
379 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
380 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
381                                             struct ip_vs_conn *cp)
382 {
383         int ret = NF_ACCEPT;
384
385         skb->ipvs_property = 1;
386         if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
387                 ret = ip_vs_confirm_conntrack(skb);
388         if (ret == NF_ACCEPT) {
389                 nf_reset(skb);
390                 skb_forward_csum(skb);
391         }
392         return ret;
393 }
394
395 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
396 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
397                                          struct ip_vs_conn *cp, int local)
398 {
399         int ret = NF_STOLEN;
400
401         skb->ipvs_property = 1;
402         if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
403                 ip_vs_notrack(skb);
404         else
405                 ip_vs_update_conntrack(skb, cp, 1);
406         if (!local) {
407                 skb_forward_csum(skb);
408                 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
409                         dst_output);
410         } else
411                 ret = NF_ACCEPT;
412         return ret;
413 }
414
415 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
416 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
417                                      struct ip_vs_conn *cp, int local)
418 {
419         int ret = NF_STOLEN;
420
421         skb->ipvs_property = 1;
422         if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
423                 ip_vs_notrack(skb);
424         if (!local) {
425                 skb_forward_csum(skb);
426                 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
427                         dst_output);
428         } else
429                 ret = NF_ACCEPT;
430         return ret;
431 }
432
433
434 /*
435  *      NULL transmitter (do nothing except return NF_ACCEPT)
436  */
437 int
438 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
439                 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
440 {
441         /* we do not touch skb and do not need pskb ptr */
442         return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
443 }
444
445
446 /*
447  *      Bypass transmitter
448  *      Let packets bypass the destination when the destination is not
449  *      available, it may be only used in transparent cache cluster.
450  */
451 int
452 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
453                   struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
454 {
455         struct rtable *rt;                      /* Route to the other host */
456         struct iphdr  *iph = ip_hdr(skb);
457         int    mtu;
458
459         EnterFunction(10);
460
461         rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, IP_VS_RT_MODE_NON_LOCAL,
462                                 NULL);
463         if (!rt)
464                 goto tx_error_icmp;
465
466         /* MTU checking */
467         mtu = dst_mtu(&rt->dst);
468         if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
469             !skb_is_gso(skb)) {
470                 ip_rt_put(rt);
471                 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
472                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
473                 goto tx_error;
474         }
475
476         /*
477          * Call ip_send_check because we are not sure it is called
478          * after ip_defrag. Is copy-on-write needed?
479          */
480         if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
481                 ip_rt_put(rt);
482                 return NF_STOLEN;
483         }
484         ip_send_check(ip_hdr(skb));
485
486         /* drop old route */
487         skb_dst_drop(skb);
488         skb_dst_set(skb, &rt->dst);
489
490         /* Another hack: avoid icmp_send in ip_fragment */
491         skb->local_df = 1;
492
493         ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
494
495         LeaveFunction(10);
496         return NF_STOLEN;
497
498  tx_error_icmp:
499         dst_link_failure(skb);
500  tx_error:
501         kfree_skb(skb);
502         LeaveFunction(10);
503         return NF_STOLEN;
504 }
505
506 #ifdef CONFIG_IP_VS_IPV6
507 int
508 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
509                      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph)
510 {
511         struct rt6_info *rt;                    /* Route to the other host */
512         int    mtu;
513
514         EnterFunction(10);
515
516         rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr.in6, NULL, 0,
517                                    IP_VS_RT_MODE_NON_LOCAL);
518         if (!rt)
519                 goto tx_error_icmp;
520
521         /* MTU checking */
522         mtu = dst_mtu(&rt->dst);
523         if (__mtu_check_toobig_v6(skb, mtu)) {
524                 if (!skb->dev) {
525                         struct net *net = dev_net(skb_dst(skb)->dev);
526
527                         skb->dev = net->loopback_dev;
528                 }
529                 /* only send ICMP too big on first fragment */
530                 if (!iph->fragoffs)
531                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
532                 dst_release(&rt->dst);
533                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
534                 goto tx_error;
535         }
536
537         /*
538          * Call ip_send_check because we are not sure it is called
539          * after ip_defrag. Is copy-on-write needed?
540          */
541         skb = skb_share_check(skb, GFP_ATOMIC);
542         if (unlikely(skb == NULL)) {
543                 dst_release(&rt->dst);
544                 return NF_STOLEN;
545         }
546
547         /* drop old route */
548         skb_dst_drop(skb);
549         skb_dst_set(skb, &rt->dst);
550
551         /* Another hack: avoid icmp_send in ip_fragment */
552         skb->local_df = 1;
553
554         ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
555
556         LeaveFunction(10);
557         return NF_STOLEN;
558
559  tx_error_icmp:
560         dst_link_failure(skb);
561  tx_error:
562         kfree_skb(skb);
563         LeaveFunction(10);
564         return NF_STOLEN;
565 }
566 #endif
567
568 /*
569  *      NAT transmitter (only for outside-to-inside nat forwarding)
570  *      Not used for related ICMP
571  */
572 int
573 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
574                struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
575 {
576         struct rtable *rt;              /* Route to the other host */
577         int mtu;
578         struct iphdr *iph = ip_hdr(skb);
579         int local, rc;
580
581         EnterFunction(10);
582
583         /* check if it is a connection of no-client-port */
584         if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
585                 __be16 _pt, *p;
586                 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
587                 if (p == NULL)
588                         goto tx_error;
589                 ip_vs_conn_fill_cport(cp, *p);
590                 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
591         }
592
593         if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
594                                       IP_VS_RT_MODE_LOCAL |
595                                       IP_VS_RT_MODE_NON_LOCAL |
596                                       IP_VS_RT_MODE_RDR, NULL)))
597                 goto tx_error_icmp;
598         local = rt->rt_flags & RTCF_LOCAL;
599         /*
600          * Avoid duplicate tuple in reply direction for NAT traffic
601          * to local address when connection is sync-ed
602          */
603 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
604         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
605                 enum ip_conntrack_info ctinfo;
606                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
607
608                 if (ct && !nf_ct_is_untracked(ct)) {
609                         IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
610                                          "ip_vs_nat_xmit(): "
611                                          "stopping DNAT to local address");
612                         goto tx_error_put;
613                 }
614         }
615 #endif
616
617         /* From world but DNAT to loopback address? */
618         if (local && ipv4_is_loopback(cp->daddr.ip) &&
619             rt_is_input_route(skb_rtable(skb))) {
620                 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
621                                  "stopping DNAT to loopback address");
622                 goto tx_error_put;
623         }
624
625         /* MTU checking */
626         mtu = dst_mtu(&rt->dst);
627         if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
628             !skb_is_gso(skb)) {
629                 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
630                 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
631                                  "ip_vs_nat_xmit(): frag needed for");
632                 goto tx_error_put;
633         }
634
635         /* copy-on-write the packet before mangling it */
636         if (!skb_make_writable(skb, sizeof(struct iphdr)))
637                 goto tx_error_put;
638
639         if (skb_cow(skb, rt->dst.dev->hard_header_len))
640                 goto tx_error_put;
641
642         /* mangle the packet */
643         if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
644                 goto tx_error_put;
645         ip_hdr(skb)->daddr = cp->daddr.ip;
646         ip_send_check(ip_hdr(skb));
647
648         if (!local) {
649                 /* drop old route */
650                 skb_dst_drop(skb);
651                 skb_dst_set(skb, &rt->dst);
652         } else {
653                 ip_rt_put(rt);
654                 /*
655                  * Some IPv4 replies get local address from routes,
656                  * not from iph, so while we DNAT after routing
657                  * we need this second input/output route.
658                  */
659                 if (!__ip_vs_reroute_locally(skb))
660                         goto tx_error;
661         }
662
663         IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
664
665         /* FIXME: when application helper enlarges the packet and the length
666            is larger than the MTU of outgoing device, there will be still
667            MTU problem. */
668
669         /* Another hack: avoid icmp_send in ip_fragment */
670         skb->local_df = 1;
671
672         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
673
674         LeaveFunction(10);
675         return rc;
676
677   tx_error_icmp:
678         dst_link_failure(skb);
679   tx_error:
680         kfree_skb(skb);
681         LeaveFunction(10);
682         return NF_STOLEN;
683   tx_error_put:
684         ip_rt_put(rt);
685         goto tx_error;
686 }
687
688 #ifdef CONFIG_IP_VS_IPV6
689 int
690 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
691                   struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph)
692 {
693         struct rt6_info *rt;            /* Route to the other host */
694         int mtu;
695         int local, rc;
696
697         EnterFunction(10);
698
699         /* check if it is a connection of no-client-port */
700         if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !iph->fragoffs)) {
701                 __be16 _pt, *p;
702                 p = skb_header_pointer(skb, iph->len, sizeof(_pt), &_pt);
703                 if (p == NULL)
704                         goto tx_error;
705                 ip_vs_conn_fill_cport(cp, *p);
706                 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
707         }
708
709         if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
710                                          0, (IP_VS_RT_MODE_LOCAL |
711                                              IP_VS_RT_MODE_NON_LOCAL |
712                                              IP_VS_RT_MODE_RDR))))
713                 goto tx_error_icmp;
714         local = __ip_vs_is_local_route6(rt);
715         /*
716          * Avoid duplicate tuple in reply direction for NAT traffic
717          * to local address when connection is sync-ed
718          */
719 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
720         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
721                 enum ip_conntrack_info ctinfo;
722                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
723
724                 if (ct && !nf_ct_is_untracked(ct)) {
725                         IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
726                                          "ip_vs_nat_xmit_v6(): "
727                                          "stopping DNAT to local address");
728                         goto tx_error_put;
729                 }
730         }
731 #endif
732
733         /* From world but DNAT to loopback address? */
734         if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
735             ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
736                 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
737                                  "ip_vs_nat_xmit_v6(): "
738                                  "stopping DNAT to loopback address");
739                 goto tx_error_put;
740         }
741
742         /* MTU checking */
743         mtu = dst_mtu(&rt->dst);
744         if (__mtu_check_toobig_v6(skb, mtu)) {
745                 if (!skb->dev) {
746                         struct net *net = dev_net(skb_dst(skb)->dev);
747
748                         skb->dev = net->loopback_dev;
749                 }
750                 /* only send ICMP too big on first fragment */
751                 if (!iph->fragoffs)
752                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
753                 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
754                                  "ip_vs_nat_xmit_v6(): frag needed for");
755                 goto tx_error_put;
756         }
757
758         /* copy-on-write the packet before mangling it */
759         if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
760                 goto tx_error_put;
761
762         if (skb_cow(skb, rt->dst.dev->hard_header_len))
763                 goto tx_error_put;
764
765         /* mangle the packet */
766         if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, iph))
767                 goto tx_error;
768         ipv6_hdr(skb)->daddr = cp->daddr.in6;
769
770         if (!local || !skb->dev) {
771                 /* drop the old route when skb is not shared */
772                 skb_dst_drop(skb);
773                 skb_dst_set(skb, &rt->dst);
774         } else {
775                 /* destined to loopback, do we need to change route? */
776                 dst_release(&rt->dst);
777         }
778
779         IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
780
781         /* FIXME: when application helper enlarges the packet and the length
782            is larger than the MTU of outgoing device, there will be still
783            MTU problem. */
784
785         /* Another hack: avoid icmp_send in ip_fragment */
786         skb->local_df = 1;
787
788         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
789
790         LeaveFunction(10);
791         return rc;
792
793 tx_error_icmp:
794         dst_link_failure(skb);
795 tx_error:
796         LeaveFunction(10);
797         kfree_skb(skb);
798         return NF_STOLEN;
799 tx_error_put:
800         dst_release(&rt->dst);
801         goto tx_error;
802 }
803 #endif
804
805
806 /*
807  *   IP Tunneling transmitter
808  *
809  *   This function encapsulates the packet in a new IP packet, its
810  *   destination will be set to cp->daddr. Most code of this function
811  *   is taken from ipip.c.
812  *
813  *   It is used in VS/TUN cluster. The load balancer selects a real
814  *   server from a cluster based on a scheduling algorithm,
815  *   encapsulates the request packet and forwards it to the selected
816  *   server. For example, all real servers are configured with
817  *   "ifconfig tunl0 <Virtual IP Address> up". When the server receives
818  *   the encapsulated packet, it will decapsulate the packet, processe
819  *   the request and return the response packets directly to the client
820  *   without passing the load balancer. This can greatly increase the
821  *   scalability of virtual server.
822  *
823  *   Used for ANY protocol
824  */
825 int
826 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
827                   struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
828 {
829         struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
830         struct rtable *rt;                      /* Route to the other host */
831         __be32 saddr;                           /* Source for tunnel */
832         struct net_device *tdev;                /* Device to other host */
833         struct iphdr  *old_iph = ip_hdr(skb);
834         u8     tos = old_iph->tos;
835         __be16 df;
836         struct iphdr  *iph;                     /* Our new IP header */
837         unsigned int max_headroom;              /* The extra header space needed */
838         int    mtu;
839         int ret;
840
841         EnterFunction(10);
842
843         if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
844                                       IP_VS_RT_MODE_LOCAL |
845                                       IP_VS_RT_MODE_NON_LOCAL |
846                                       IP_VS_RT_MODE_CONNECT, &saddr)))
847                 goto tx_error_icmp;
848         if (rt->rt_flags & RTCF_LOCAL) {
849                 ip_rt_put(rt);
850                 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
851         }
852
853         tdev = rt->dst.dev;
854
855         mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
856         if (mtu < 68) {
857                 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
858                 goto tx_error_put;
859         }
860         if (rt_is_output_route(skb_rtable(skb)))
861                 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
862
863         /* Copy DF, reset fragment offset and MF */
864         df = sysctl_pmtu_disc(ipvs) ? old_iph->frag_off & htons(IP_DF) : 0;
865
866         if (df && mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb)) {
867                 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
868                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
869                 goto tx_error_put;
870         }
871
872         /*
873          * Okay, now see if we can stuff it in the buffer as-is.
874          */
875         max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
876
877         if (skb_headroom(skb) < max_headroom
878             || skb_cloned(skb) || skb_shared(skb)) {
879                 struct sk_buff *new_skb =
880                         skb_realloc_headroom(skb, max_headroom);
881                 if (!new_skb) {
882                         ip_rt_put(rt);
883                         kfree_skb(skb);
884                         IP_VS_ERR_RL("%s(): no memory\n", __func__);
885                         return NF_STOLEN;
886                 }
887                 consume_skb(skb);
888                 skb = new_skb;
889                 old_iph = ip_hdr(skb);
890         }
891
892         skb->transport_header = skb->network_header;
893
894         /* fix old IP header checksum */
895         ip_send_check(old_iph);
896
897         skb_push(skb, sizeof(struct iphdr));
898         skb_reset_network_header(skb);
899         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
900
901         /* drop old route */
902         skb_dst_drop(skb);
903         skb_dst_set(skb, &rt->dst);
904
905         /*
906          *      Push down and install the IPIP header.
907          */
908         iph                     =       ip_hdr(skb);
909         iph->version            =       4;
910         iph->ihl                =       sizeof(struct iphdr)>>2;
911         iph->frag_off           =       df;
912         iph->protocol           =       IPPROTO_IPIP;
913         iph->tos                =       tos;
914         iph->daddr              =       cp->daddr.ip;
915         iph->saddr              =       saddr;
916         iph->ttl                =       old_iph->ttl;
917         ip_select_ident(iph, &rt->dst, NULL);
918
919         /* Another hack: avoid icmp_send in ip_fragment */
920         skb->local_df = 1;
921
922         ret = ip_vs_tunnel_xmit_prepare(skb, cp);
923         if (ret == NF_ACCEPT)
924                 ip_local_out(skb);
925         else if (ret == NF_DROP)
926                 kfree_skb(skb);
927
928         LeaveFunction(10);
929
930         return NF_STOLEN;
931
932   tx_error_icmp:
933         dst_link_failure(skb);
934   tx_error:
935         kfree_skb(skb);
936         LeaveFunction(10);
937         return NF_STOLEN;
938 tx_error_put:
939         ip_rt_put(rt);
940         goto tx_error;
941 }
942
943 #ifdef CONFIG_IP_VS_IPV6
944 int
945 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
946                      struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
947 {
948         struct rt6_info *rt;            /* Route to the other host */
949         struct in6_addr saddr;          /* Source for tunnel */
950         struct net_device *tdev;        /* Device to other host */
951         struct ipv6hdr  *old_iph = ipv6_hdr(skb);
952         struct ipv6hdr  *iph;           /* Our new IP header */
953         unsigned int max_headroom;      /* The extra header space needed */
954         int    mtu;
955         int ret;
956
957         EnterFunction(10);
958
959         if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
960                                          &saddr, 1, (IP_VS_RT_MODE_LOCAL |
961                                                      IP_VS_RT_MODE_NON_LOCAL))))
962                 goto tx_error_icmp;
963         if (__ip_vs_is_local_route6(rt)) {
964                 dst_release(&rt->dst);
965                 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
966         }
967
968         tdev = rt->dst.dev;
969
970         mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
971         if (mtu < IPV6_MIN_MTU) {
972                 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
973                              IPV6_MIN_MTU);
974                 goto tx_error_put;
975         }
976         if (skb_dst(skb))
977                 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
978
979         /* MTU checking: Notice that 'mtu' have been adjusted before hand */
980         if (__mtu_check_toobig_v6(skb, mtu)) {
981                 if (!skb->dev) {
982                         struct net *net = dev_net(skb_dst(skb)->dev);
983
984                         skb->dev = net->loopback_dev;
985                 }
986                 /* only send ICMP too big on first fragment */
987                 if (!ipvsh->fragoffs)
988                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
989                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
990                 goto tx_error_put;
991         }
992
993         /*
994          * Okay, now see if we can stuff it in the buffer as-is.
995          */
996         max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
997
998         if (skb_headroom(skb) < max_headroom
999             || skb_cloned(skb) || skb_shared(skb)) {
1000                 struct sk_buff *new_skb =
1001                         skb_realloc_headroom(skb, max_headroom);
1002                 if (!new_skb) {
1003                         dst_release(&rt->dst);
1004                         kfree_skb(skb);
1005                         IP_VS_ERR_RL("%s(): no memory\n", __func__);
1006                         return NF_STOLEN;
1007                 }
1008                 consume_skb(skb);
1009                 skb = new_skb;
1010                 old_iph = ipv6_hdr(skb);
1011         }
1012
1013         skb->transport_header = skb->network_header;
1014
1015         skb_push(skb, sizeof(struct ipv6hdr));
1016         skb_reset_network_header(skb);
1017         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1018
1019         /* drop old route */
1020         skb_dst_drop(skb);
1021         skb_dst_set(skb, &rt->dst);
1022
1023         /*
1024          *      Push down and install the IPIP header.
1025          */
1026         iph                     =       ipv6_hdr(skb);
1027         iph->version            =       6;
1028         iph->nexthdr            =       IPPROTO_IPV6;
1029         iph->payload_len        =       old_iph->payload_len;
1030         be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
1031         iph->priority           =       old_iph->priority;
1032         memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1033         iph->daddr = cp->daddr.in6;
1034         iph->saddr = saddr;
1035         iph->hop_limit          =       old_iph->hop_limit;
1036
1037         /* Another hack: avoid icmp_send in ip_fragment */
1038         skb->local_df = 1;
1039
1040         ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1041         if (ret == NF_ACCEPT)
1042                 ip6_local_out(skb);
1043         else if (ret == NF_DROP)
1044                 kfree_skb(skb);
1045
1046         LeaveFunction(10);
1047
1048         return NF_STOLEN;
1049
1050 tx_error_icmp:
1051         dst_link_failure(skb);
1052 tx_error:
1053         kfree_skb(skb);
1054         LeaveFunction(10);
1055         return NF_STOLEN;
1056 tx_error_put:
1057         dst_release(&rt->dst);
1058         goto tx_error;
1059 }
1060 #endif
1061
1062
1063 /*
1064  *      Direct Routing transmitter
1065  *      Used for ANY protocol
1066  */
1067 int
1068 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1069               struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1070 {
1071         struct rtable *rt;                      /* Route to the other host */
1072         struct iphdr  *iph = ip_hdr(skb);
1073         int    mtu;
1074
1075         EnterFunction(10);
1076
1077         if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1078                                       IP_VS_RT_MODE_LOCAL |
1079                                       IP_VS_RT_MODE_NON_LOCAL |
1080                                       IP_VS_RT_MODE_KNOWN_NH, NULL)))
1081                 goto tx_error_icmp;
1082         if (rt->rt_flags & RTCF_LOCAL) {
1083                 ip_rt_put(rt);
1084                 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1085         }
1086
1087         /* MTU checking */
1088         mtu = dst_mtu(&rt->dst);
1089         if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1090             !skb_is_gso(skb)) {
1091                 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1092                 ip_rt_put(rt);
1093                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
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         if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1102                 ip_rt_put(rt);
1103                 return NF_STOLEN;
1104         }
1105         ip_send_check(ip_hdr(skb));
1106
1107         /* drop old route */
1108         skb_dst_drop(skb);
1109         skb_dst_set(skb, &rt->dst);
1110
1111         /* Another hack: avoid icmp_send in ip_fragment */
1112         skb->local_df = 1;
1113
1114         ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1115
1116         LeaveFunction(10);
1117         return NF_STOLEN;
1118
1119   tx_error_icmp:
1120         dst_link_failure(skb);
1121   tx_error:
1122         kfree_skb(skb);
1123         LeaveFunction(10);
1124         return NF_STOLEN;
1125 }
1126
1127 #ifdef CONFIG_IP_VS_IPV6
1128 int
1129 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1130                  struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph)
1131 {
1132         struct rt6_info *rt;                    /* Route to the other host */
1133         int    mtu;
1134
1135         EnterFunction(10);
1136
1137         if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1138                                          0, (IP_VS_RT_MODE_LOCAL |
1139                                              IP_VS_RT_MODE_NON_LOCAL))))
1140                 goto tx_error_icmp;
1141         if (__ip_vs_is_local_route6(rt)) {
1142                 dst_release(&rt->dst);
1143                 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1144         }
1145
1146         /* MTU checking */
1147         mtu = dst_mtu(&rt->dst);
1148         if (__mtu_check_toobig_v6(skb, mtu)) {
1149                 if (!skb->dev) {
1150                         struct net *net = dev_net(skb_dst(skb)->dev);
1151
1152                         skb->dev = net->loopback_dev;
1153                 }
1154                 /* only send ICMP too big on first fragment */
1155                 if (!iph->fragoffs)
1156                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1157                 dst_release(&rt->dst);
1158                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1159                 goto tx_error;
1160         }
1161
1162         /*
1163          * Call ip_send_check because we are not sure it is called
1164          * after ip_defrag. Is copy-on-write needed?
1165          */
1166         skb = skb_share_check(skb, GFP_ATOMIC);
1167         if (unlikely(skb == NULL)) {
1168                 dst_release(&rt->dst);
1169                 return NF_STOLEN;
1170         }
1171
1172         /* drop old route */
1173         skb_dst_drop(skb);
1174         skb_dst_set(skb, &rt->dst);
1175
1176         /* Another hack: avoid icmp_send in ip_fragment */
1177         skb->local_df = 1;
1178
1179         ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1180
1181         LeaveFunction(10);
1182         return NF_STOLEN;
1183
1184 tx_error_icmp:
1185         dst_link_failure(skb);
1186 tx_error:
1187         kfree_skb(skb);
1188         LeaveFunction(10);
1189         return NF_STOLEN;
1190 }
1191 #endif
1192
1193
1194 /*
1195  *      ICMP packet transmitter
1196  *      called by the ip_vs_in_icmp
1197  */
1198 int
1199 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1200                 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1201                 struct ip_vs_iphdr *iph)
1202 {
1203         struct rtable   *rt;    /* Route to the other host */
1204         int mtu;
1205         int rc;
1206         int local;
1207         int rt_mode;
1208
1209         EnterFunction(10);
1210
1211         /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1212            forwarded directly here, because there is no need to
1213            translate address/port back */
1214         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1215                 if (cp->packet_xmit)
1216                         rc = cp->packet_xmit(skb, cp, pp, iph);
1217                 else
1218                         rc = NF_ACCEPT;
1219                 /* do not touch skb anymore */
1220                 atomic_inc(&cp->in_pkts);
1221                 goto out;
1222         }
1223
1224         /*
1225          * mangle and send the packet here (only for VS/NAT)
1226          */
1227
1228         /* LOCALNODE from FORWARD hook is not supported */
1229         rt_mode = (hooknum != NF_INET_FORWARD) ?
1230                   IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1231                   IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1232         if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1233                                       rt_mode, NULL)))
1234                 goto tx_error_icmp;
1235         local = rt->rt_flags & RTCF_LOCAL;
1236
1237         /*
1238          * Avoid duplicate tuple in reply direction for NAT traffic
1239          * to local address when connection is sync-ed
1240          */
1241 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1242         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1243                 enum ip_conntrack_info ctinfo;
1244                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1245
1246                 if (ct && !nf_ct_is_untracked(ct)) {
1247                         IP_VS_DBG(10, "%s(): "
1248                                   "stopping DNAT to local address %pI4\n",
1249                                   __func__, &cp->daddr.ip);
1250                         goto tx_error_put;
1251                 }
1252         }
1253 #endif
1254
1255         /* From world but DNAT to loopback address? */
1256         if (local && ipv4_is_loopback(cp->daddr.ip) &&
1257             rt_is_input_route(skb_rtable(skb))) {
1258                 IP_VS_DBG(1, "%s(): "
1259                           "stopping DNAT to loopback %pI4\n",
1260                           __func__, &cp->daddr.ip);
1261                 goto tx_error_put;
1262         }
1263
1264         /* MTU checking */
1265         mtu = dst_mtu(&rt->dst);
1266         if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1267             !skb_is_gso(skb)) {
1268                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1269                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1270                 goto tx_error_put;
1271         }
1272
1273         /* copy-on-write the packet before mangling it */
1274         if (!skb_make_writable(skb, offset))
1275                 goto tx_error_put;
1276
1277         if (skb_cow(skb, rt->dst.dev->hard_header_len))
1278                 goto tx_error_put;
1279
1280         ip_vs_nat_icmp(skb, pp, cp, 0);
1281
1282         if (!local) {
1283                 /* drop the old route when skb is not shared */
1284                 skb_dst_drop(skb);
1285                 skb_dst_set(skb, &rt->dst);
1286         } else {
1287                 ip_rt_put(rt);
1288                 /*
1289                  * Some IPv4 replies get local address from routes,
1290                  * not from iph, so while we DNAT after routing
1291                  * we need this second input/output route.
1292                  */
1293                 if (!__ip_vs_reroute_locally(skb))
1294                         goto tx_error;
1295         }
1296
1297         /* Another hack: avoid icmp_send in ip_fragment */
1298         skb->local_df = 1;
1299
1300         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1301         goto out;
1302
1303   tx_error_icmp:
1304         dst_link_failure(skb);
1305   tx_error:
1306         dev_kfree_skb(skb);
1307         rc = NF_STOLEN;
1308   out:
1309         LeaveFunction(10);
1310         return rc;
1311   tx_error_put:
1312         ip_rt_put(rt);
1313         goto tx_error;
1314 }
1315
1316 #ifdef CONFIG_IP_VS_IPV6
1317 int
1318 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1319                 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1320                 struct ip_vs_iphdr *iph)
1321 {
1322         struct rt6_info *rt;    /* Route to the other host */
1323         int mtu;
1324         int rc;
1325         int local;
1326         int rt_mode;
1327
1328         EnterFunction(10);
1329
1330         /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1331            forwarded directly here, because there is no need to
1332            translate address/port back */
1333         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1334                 if (cp->packet_xmit)
1335                         rc = cp->packet_xmit(skb, cp, pp, iph);
1336                 else
1337                         rc = NF_ACCEPT;
1338                 /* do not touch skb anymore */
1339                 atomic_inc(&cp->in_pkts);
1340                 goto out;
1341         }
1342
1343         /*
1344          * mangle and send the packet here (only for VS/NAT)
1345          */
1346
1347         /* LOCALNODE from FORWARD hook is not supported */
1348         rt_mode = (hooknum != NF_INET_FORWARD) ?
1349                   IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1350                   IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1351         if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1352                                          0, rt_mode)))
1353                 goto tx_error_icmp;
1354
1355         local = __ip_vs_is_local_route6(rt);
1356         /*
1357          * Avoid duplicate tuple in reply direction for NAT traffic
1358          * to local address when connection is sync-ed
1359          */
1360 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1361         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1362                 enum ip_conntrack_info ctinfo;
1363                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1364
1365                 if (ct && !nf_ct_is_untracked(ct)) {
1366                         IP_VS_DBG(10, "%s(): "
1367                                   "stopping DNAT to local address %pI6\n",
1368                                   __func__, &cp->daddr.in6);
1369                         goto tx_error_put;
1370                 }
1371         }
1372 #endif
1373
1374         /* From world but DNAT to loopback address? */
1375         if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1376             ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1377                 IP_VS_DBG(1, "%s(): "
1378                           "stopping DNAT to loopback %pI6\n",
1379                           __func__, &cp->daddr.in6);
1380                 goto tx_error_put;
1381         }
1382
1383         /* MTU checking */
1384         mtu = dst_mtu(&rt->dst);
1385         if (__mtu_check_toobig_v6(skb, mtu)) {
1386                 if (!skb->dev) {
1387                         struct net *net = dev_net(skb_dst(skb)->dev);
1388
1389                         skb->dev = net->loopback_dev;
1390                 }
1391                 /* only send ICMP too big on first fragment */
1392                 if (!iph->fragoffs)
1393                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1394                 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1395                 goto tx_error_put;
1396         }
1397
1398         /* copy-on-write the packet before mangling it */
1399         if (!skb_make_writable(skb, offset))
1400                 goto tx_error_put;
1401
1402         if (skb_cow(skb, rt->dst.dev->hard_header_len))
1403                 goto tx_error_put;
1404
1405         ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1406
1407         if (!local || !skb->dev) {
1408                 /* drop the old route when skb is not shared */
1409                 skb_dst_drop(skb);
1410                 skb_dst_set(skb, &rt->dst);
1411         } else {
1412                 /* destined to loopback, do we need to change route? */
1413                 dst_release(&rt->dst);
1414         }
1415
1416         /* Another hack: avoid icmp_send in ip_fragment */
1417         skb->local_df = 1;
1418
1419         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
1420         goto out;
1421
1422 tx_error_icmp:
1423         dst_link_failure(skb);
1424 tx_error:
1425         dev_kfree_skb(skb);
1426         rc = NF_STOLEN;
1427 out:
1428         LeaveFunction(10);
1429         return rc;
1430 tx_error_put:
1431         dst_release(&rt->dst);
1432         goto tx_error;
1433 }
1434 #endif