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