]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/netfilter/ipvs/ip_vs_xmit.c
regmap: Fix regmap_can_raw_write check
[karo-tx-linux.git] / net / netfilter / ipvs / ip_vs_xmit.c
1 /*
2  * ip_vs_xmit.c: various packet transmitters for IPVS
3  *
4  * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
5  *              Julian Anastasov <ja@ssi.bg>
6  *
7  *              This program is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  * Changes:
13  *
14  * Description of forwarding methods:
15  * - all transmitters are called from LOCAL_IN (remote clients) and
16  * LOCAL_OUT (local clients) but for ICMP can be called from FORWARD
17  * - not all connections have destination server, for example,
18  * connections in backup server when fwmark is used
19  * - bypass connections use daddr from packet
20  * - 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_fullsock(sk))
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                 if (ret_saddr)
297                         *ret_saddr = dest_dst->dst_saddr.ip;
298         } else {
299                 __be32 saddr = htonl(INADDR_ANY);
300
301                 noref = 0;
302
303                 /* For such unconfigured boxes avoid many route lookups
304                  * for performance reasons because we do not remember saddr
305                  */
306                 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
307                 rt = do_output_route4(net, daddr, rt_mode, &saddr);
308                 if (!rt)
309                         goto err_unreach;
310                 if (ret_saddr)
311                         *ret_saddr = saddr;
312         }
313
314         local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
315         if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
316                                                   local))) {
317                 IP_VS_DBG_RL("We are crossing local and non-local addresses"
318                              " daddr=%pI4\n", &daddr);
319                 goto err_put;
320         }
321
322         if (unlikely(local)) {
323                 /* skb to local stack, preserve old route */
324                 if (!noref)
325                         ip_rt_put(rt);
326                 return local;
327         }
328
329         if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
330                 mtu = dst_mtu(&rt->dst);
331         } else {
332                 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
333                 if (mtu < 68) {
334                         IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
335                         goto err_put;
336                 }
337                 maybe_update_pmtu(skb_af, skb, mtu);
338         }
339
340         if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
341                 goto err_put;
342
343         skb_dst_drop(skb);
344         if (noref) {
345                 if (!local)
346                         skb_dst_set_noref(skb, &rt->dst);
347                 else
348                         skb_dst_set(skb, dst_clone(&rt->dst));
349         } else
350                 skb_dst_set(skb, &rt->dst);
351
352         return local;
353
354 err_put:
355         if (!noref)
356                 ip_rt_put(rt);
357         return -1;
358
359 err_unreach:
360         dst_link_failure(skb);
361         return -1;
362 }
363
364 #ifdef CONFIG_IP_VS_IPV6
365 static struct dst_entry *
366 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
367                         struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
368 {
369         struct dst_entry *dst;
370         struct flowi6 fl6 = {
371                 .daddr = *daddr,
372         };
373
374         if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
375                 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
376
377         dst = ip6_route_output(net, NULL, &fl6);
378         if (dst->error)
379                 goto out_err;
380         if (!ret_saddr)
381                 return dst;
382         if (ipv6_addr_any(&fl6.saddr) &&
383             ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
384                                &fl6.daddr, 0, &fl6.saddr) < 0)
385                 goto out_err;
386         if (do_xfrm) {
387                 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
388                 if (IS_ERR(dst)) {
389                         dst = NULL;
390                         goto out_err;
391                 }
392         }
393         *ret_saddr = fl6.saddr;
394         return dst;
395
396 out_err:
397         dst_release(dst);
398         IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
399         return NULL;
400 }
401
402 /*
403  * Get route to destination or remote server
404  */
405 static int
406 __ip_vs_get_out_rt_v6(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
407                       struct in6_addr *daddr, struct in6_addr *ret_saddr,
408                       struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
409 {
410         struct net *net = dev_net(skb_dst(skb)->dev);
411         struct ip_vs_dest_dst *dest_dst;
412         struct rt6_info *rt;                    /* Route to the other host */
413         struct dst_entry *dst;
414         int mtu;
415         int local, noref = 1;
416
417         if (dest) {
418                 dest_dst = __ip_vs_dst_check(dest);
419                 if (likely(dest_dst))
420                         rt = (struct rt6_info *) dest_dst->dst_cache;
421                 else {
422                         u32 cookie;
423
424                         dest_dst = ip_vs_dest_dst_alloc();
425                         spin_lock_bh(&dest->dst_lock);
426                         if (!dest_dst) {
427                                 __ip_vs_dst_set(dest, NULL, NULL, 0);
428                                 spin_unlock_bh(&dest->dst_lock);
429                                 goto err_unreach;
430                         }
431                         dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
432                                                       &dest_dst->dst_saddr.in6,
433                                                       do_xfrm, rt_mode);
434                         if (!dst) {
435                                 __ip_vs_dst_set(dest, NULL, NULL, 0);
436                                 spin_unlock_bh(&dest->dst_lock);
437                                 ip_vs_dest_dst_free(dest_dst);
438                                 goto err_unreach;
439                         }
440                         rt = (struct rt6_info *) dst;
441                         cookie = rt6_get_cookie(rt);
442                         __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
443                         spin_unlock_bh(&dest->dst_lock);
444                         IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
445                                   &dest->addr.in6, &dest_dst->dst_saddr.in6,
446                                   atomic_read(&rt->dst.__refcnt));
447                 }
448                 if (ret_saddr)
449                         *ret_saddr = dest_dst->dst_saddr.in6;
450         } else {
451                 noref = 0;
452                 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
453                                               rt_mode);
454                 if (!dst)
455                         goto err_unreach;
456                 rt = (struct rt6_info *) dst;
457         }
458
459         local = __ip_vs_is_local_route6(rt);
460
461         if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
462                                                   local))) {
463                 IP_VS_DBG_RL("We are crossing local and non-local addresses"
464                              " daddr=%pI6\n", daddr);
465                 goto err_put;
466         }
467
468         if (unlikely(local)) {
469                 /* skb to local stack, preserve old route */
470                 if (!noref)
471                         dst_release(&rt->dst);
472                 return local;
473         }
474
475         /* MTU checking */
476         if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
477                 mtu = dst_mtu(&rt->dst);
478         else {
479                 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
480                 if (mtu < IPV6_MIN_MTU) {
481                         IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
482                                      IPV6_MIN_MTU);
483                         goto err_put;
484                 }
485                 maybe_update_pmtu(skb_af, skb, mtu);
486         }
487
488         if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
489                 goto err_put;
490
491         skb_dst_drop(skb);
492         if (noref) {
493                 if (!local)
494                         skb_dst_set_noref(skb, &rt->dst);
495                 else
496                         skb_dst_set(skb, dst_clone(&rt->dst));
497         } else
498                 skb_dst_set(skb, &rt->dst);
499
500         return local;
501
502 err_put:
503         if (!noref)
504                 dst_release(&rt->dst);
505         return -1;
506
507 err_unreach:
508         dst_link_failure(skb);
509         return -1;
510 }
511 #endif
512
513
514 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
515 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
516                                             struct ip_vs_conn *cp)
517 {
518         int ret = NF_ACCEPT;
519
520         skb->ipvs_property = 1;
521         if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
522                 ret = ip_vs_confirm_conntrack(skb);
523         if (ret == NF_ACCEPT) {
524                 nf_reset(skb);
525                 skb_forward_csum(skb);
526         }
527         return ret;
528 }
529
530 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
531 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
532                                          struct ip_vs_conn *cp, int local)
533 {
534         int ret = NF_STOLEN;
535
536         skb->ipvs_property = 1;
537         if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
538                 ip_vs_notrack(skb);
539         else
540                 ip_vs_update_conntrack(skb, cp, 1);
541         if (!local) {
542                 skb_forward_csum(skb);
543                 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
544                         NULL, skb_dst(skb)->dev, dst_output_sk);
545         } else
546                 ret = NF_ACCEPT;
547         return ret;
548 }
549
550 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
551 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
552                                      struct ip_vs_conn *cp, int local)
553 {
554         int ret = NF_STOLEN;
555
556         skb->ipvs_property = 1;
557         if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
558                 ip_vs_notrack(skb);
559         if (!local) {
560                 skb_forward_csum(skb);
561                 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
562                         NULL, skb_dst(skb)->dev, dst_output_sk);
563         } else
564                 ret = NF_ACCEPT;
565         return ret;
566 }
567
568
569 /*
570  *      NULL transmitter (do nothing except return NF_ACCEPT)
571  */
572 int
573 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
574                 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
575 {
576         /* we do not touch skb and do not need pskb ptr */
577         return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
578 }
579
580
581 /*
582  *      Bypass transmitter
583  *      Let packets bypass the destination when the destination is not
584  *      available, it may be only used in transparent cache cluster.
585  */
586 int
587 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
588                   struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
589 {
590         struct iphdr  *iph = ip_hdr(skb);
591
592         EnterFunction(10);
593
594         rcu_read_lock();
595         if (__ip_vs_get_out_rt(cp->af, skb, NULL, iph->daddr,
596                                IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
597                 goto tx_error;
598
599         ip_send_check(iph);
600
601         /* Another hack: avoid icmp_send in ip_fragment */
602         skb->ignore_df = 1;
603
604         ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
605         rcu_read_unlock();
606
607         LeaveFunction(10);
608         return NF_STOLEN;
609
610  tx_error:
611         kfree_skb(skb);
612         rcu_read_unlock();
613         LeaveFunction(10);
614         return NF_STOLEN;
615 }
616
617 #ifdef CONFIG_IP_VS_IPV6
618 int
619 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
620                      struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
621 {
622         EnterFunction(10);
623
624         rcu_read_lock();
625         if (__ip_vs_get_out_rt_v6(cp->af, skb, NULL, &ipvsh->daddr.in6, NULL,
626                                   ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
627                 goto tx_error;
628
629         /* Another hack: avoid icmp_send in ip_fragment */
630         skb->ignore_df = 1;
631
632         ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
633         rcu_read_unlock();
634
635         LeaveFunction(10);
636         return NF_STOLEN;
637
638  tx_error:
639         kfree_skb(skb);
640         rcu_read_unlock();
641         LeaveFunction(10);
642         return NF_STOLEN;
643 }
644 #endif
645
646 /*
647  *      NAT transmitter (only for outside-to-inside nat forwarding)
648  *      Not used for related ICMP
649  */
650 int
651 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
652                struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
653 {
654         struct rtable *rt;              /* Route to the other host */
655         int local, rc, was_input;
656
657         EnterFunction(10);
658
659         rcu_read_lock();
660         /* check if it is a connection of no-client-port */
661         if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
662                 __be16 _pt, *p;
663
664                 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
665                 if (p == NULL)
666                         goto tx_error;
667                 ip_vs_conn_fill_cport(cp, *p);
668                 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
669         }
670
671         was_input = rt_is_input_route(skb_rtable(skb));
672         local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
673                                    IP_VS_RT_MODE_LOCAL |
674                                    IP_VS_RT_MODE_NON_LOCAL |
675                                    IP_VS_RT_MODE_RDR, NULL, ipvsh);
676         if (local < 0)
677                 goto tx_error;
678         rt = skb_rtable(skb);
679         /*
680          * Avoid duplicate tuple in reply direction for NAT traffic
681          * to local address when connection is sync-ed
682          */
683 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
684         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
685                 enum ip_conntrack_info ctinfo;
686                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
687
688                 if (ct && !nf_ct_is_untracked(ct)) {
689                         IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
690                                          "ip_vs_nat_xmit(): "
691                                          "stopping DNAT to local address");
692                         goto tx_error;
693                 }
694         }
695 #endif
696
697         /* From world but DNAT to loopback address? */
698         if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
699                 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
700                                  "stopping DNAT to loopback address");
701                 goto tx_error;
702         }
703
704         /* copy-on-write the packet before mangling it */
705         if (!skb_make_writable(skb, sizeof(struct iphdr)))
706                 goto tx_error;
707
708         if (skb_cow(skb, rt->dst.dev->hard_header_len))
709                 goto tx_error;
710
711         /* mangle the packet */
712         if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
713                 goto tx_error;
714         ip_hdr(skb)->daddr = cp->daddr.ip;
715         ip_send_check(ip_hdr(skb));
716
717         IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
718
719         /* FIXME: when application helper enlarges the packet and the length
720            is larger than the MTU of outgoing device, there will be still
721            MTU problem. */
722
723         /* Another hack: avoid icmp_send in ip_fragment */
724         skb->ignore_df = 1;
725
726         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
727         rcu_read_unlock();
728
729         LeaveFunction(10);
730         return rc;
731
732   tx_error:
733         kfree_skb(skb);
734         rcu_read_unlock();
735         LeaveFunction(10);
736         return NF_STOLEN;
737 }
738
739 #ifdef CONFIG_IP_VS_IPV6
740 int
741 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
742                   struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
743 {
744         struct rt6_info *rt;            /* Route to the other host */
745         int local, rc;
746
747         EnterFunction(10);
748
749         rcu_read_lock();
750         /* check if it is a connection of no-client-port */
751         if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
752                 __be16 _pt, *p;
753                 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
754                 if (p == NULL)
755                         goto tx_error;
756                 ip_vs_conn_fill_cport(cp, *p);
757                 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
758         }
759
760         local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
761                                       NULL, ipvsh, 0,
762                                       IP_VS_RT_MODE_LOCAL |
763                                       IP_VS_RT_MODE_NON_LOCAL |
764                                       IP_VS_RT_MODE_RDR);
765         if (local < 0)
766                 goto tx_error;
767         rt = (struct rt6_info *) skb_dst(skb);
768         /*
769          * Avoid duplicate tuple in reply direction for NAT traffic
770          * to local address when connection is sync-ed
771          */
772 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
773         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
774                 enum ip_conntrack_info ctinfo;
775                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
776
777                 if (ct && !nf_ct_is_untracked(ct)) {
778                         IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
779                                          "ip_vs_nat_xmit_v6(): "
780                                          "stopping DNAT to local address");
781                         goto tx_error;
782                 }
783         }
784 #endif
785
786         /* From world but DNAT to loopback address? */
787         if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
788             ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
789                 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
790                                  "ip_vs_nat_xmit_v6(): "
791                                  "stopping DNAT to loopback address");
792                 goto tx_error;
793         }
794
795         /* copy-on-write the packet before mangling it */
796         if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
797                 goto tx_error;
798
799         if (skb_cow(skb, rt->dst.dev->hard_header_len))
800                 goto tx_error;
801
802         /* mangle the packet */
803         if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
804                 goto tx_error;
805         ipv6_hdr(skb)->daddr = cp->daddr.in6;
806
807         IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
808
809         /* FIXME: when application helper enlarges the packet and the length
810            is larger than the MTU of outgoing device, there will be still
811            MTU problem. */
812
813         /* Another hack: avoid icmp_send in ip_fragment */
814         skb->ignore_df = 1;
815
816         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
817         rcu_read_unlock();
818
819         LeaveFunction(10);
820         return rc;
821
822 tx_error:
823         LeaveFunction(10);
824         kfree_skb(skb);
825         rcu_read_unlock();
826         return NF_STOLEN;
827 }
828 #endif
829
830 /* When forwarding a packet, we must ensure that we've got enough headroom
831  * for the encapsulation packet in the skb.  This also gives us an
832  * opportunity to figure out what the payload_len, dsfield, ttl, and df
833  * values should be, so that we won't need to look at the old ip header
834  * again
835  */
836 static struct sk_buff *
837 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
838                            unsigned int max_headroom, __u8 *next_protocol,
839                            __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
840                            __be16 *df)
841 {
842         struct sk_buff *new_skb = NULL;
843         struct iphdr *old_iph = NULL;
844 #ifdef CONFIG_IP_VS_IPV6
845         struct ipv6hdr *old_ipv6h = NULL;
846 #endif
847
848         if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
849                 new_skb = skb_realloc_headroom(skb, max_headroom);
850                 if (!new_skb)
851                         goto error;
852                 if (skb->sk)
853                         skb_set_owner_w(new_skb, skb->sk);
854                 consume_skb(skb);
855                 skb = new_skb;
856         }
857
858 #ifdef CONFIG_IP_VS_IPV6
859         if (skb_af == AF_INET6) {
860                 old_ipv6h = ipv6_hdr(skb);
861                 *next_protocol = IPPROTO_IPV6;
862                 if (payload_len)
863                         *payload_len =
864                                 ntohs(old_ipv6h->payload_len) +
865                                 sizeof(*old_ipv6h);
866                 *dsfield = ipv6_get_dsfield(old_ipv6h);
867                 *ttl = old_ipv6h->hop_limit;
868                 if (df)
869                         *df = 0;
870         } else
871 #endif
872         {
873                 old_iph = ip_hdr(skb);
874                 /* Copy DF, reset fragment offset and MF */
875                 if (df)
876                         *df = (old_iph->frag_off & htons(IP_DF));
877                 *next_protocol = IPPROTO_IPIP;
878
879                 /* fix old IP header checksum */
880                 ip_send_check(old_iph);
881                 *dsfield = ipv4_get_dsfield(old_iph);
882                 *ttl = old_iph->ttl;
883                 if (payload_len)
884                         *payload_len = ntohs(old_iph->tot_len);
885         }
886
887         return skb;
888 error:
889         kfree_skb(skb);
890         return ERR_PTR(-ENOMEM);
891 }
892
893 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
894 {
895         if (encaps_af == AF_INET) {
896                 if (orig_af == AF_INET)
897                         return SKB_GSO_IPIP;
898
899                 return SKB_GSO_SIT;
900         }
901
902         /* GSO: we need to provide proper SKB_GSO_ value for IPv6:
903          * SKB_GSO_SIT/IPV6
904          */
905         return 0;
906 }
907
908 /*
909  *   IP Tunneling transmitter
910  *
911  *   This function encapsulates the packet in a new IP packet, its
912  *   destination will be set to cp->daddr. Most code of this function
913  *   is taken from ipip.c.
914  *
915  *   It is used in VS/TUN cluster. The load balancer selects a real
916  *   server from a cluster based on a scheduling algorithm,
917  *   encapsulates the request packet and forwards it to the selected
918  *   server. For example, all real servers are configured with
919  *   "ifconfig tunl0 <Virtual IP Address> up". When the server receives
920  *   the encapsulated packet, it will decapsulate the packet, processe
921  *   the request and return the response packets directly to the client
922  *   without passing the load balancer. This can greatly increase the
923  *   scalability of virtual server.
924  *
925  *   Used for ANY protocol
926  */
927 int
928 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
929                   struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
930 {
931         struct net *net = skb_net(skb);
932         struct netns_ipvs *ipvs = net_ipvs(net);
933         struct rtable *rt;                      /* Route to the other host */
934         __be32 saddr;                           /* Source for tunnel */
935         struct net_device *tdev;                /* Device to other host */
936         __u8 next_protocol = 0;
937         __u8 dsfield = 0;
938         __u8 ttl = 0;
939         __be16 df = 0;
940         __be16 *dfp = NULL;
941         struct iphdr  *iph;                     /* Our new IP header */
942         unsigned int max_headroom;              /* The extra header space needed */
943         int ret, local;
944
945         EnterFunction(10);
946
947         rcu_read_lock();
948         local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
949                                    IP_VS_RT_MODE_LOCAL |
950                                    IP_VS_RT_MODE_NON_LOCAL |
951                                    IP_VS_RT_MODE_CONNECT |
952                                    IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
953         if (local < 0)
954                 goto tx_error;
955         if (local) {
956                 rcu_read_unlock();
957                 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
958         }
959
960         rt = skb_rtable(skb);
961         tdev = rt->dst.dev;
962
963         /*
964          * Okay, now see if we can stuff it in the buffer as-is.
965          */
966         max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
967
968         /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
969         dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
970         skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
971                                          &next_protocol, NULL, &dsfield,
972                                          &ttl, dfp);
973         if (IS_ERR(skb))
974                 goto tx_error;
975
976         skb = iptunnel_handle_offloads(
977                 skb, false, __tun_gso_type_mask(AF_INET, cp->af));
978         if (IS_ERR(skb))
979                 goto tx_error;
980
981         skb->transport_header = skb->network_header;
982
983         skb_push(skb, sizeof(struct iphdr));
984         skb_reset_network_header(skb);
985         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
986
987         /*
988          *      Push down and install the IPIP header.
989          */
990         iph                     =       ip_hdr(skb);
991         iph->version            =       4;
992         iph->ihl                =       sizeof(struct iphdr)>>2;
993         iph->frag_off           =       df;
994         iph->protocol           =       next_protocol;
995         iph->tos                =       dsfield;
996         iph->daddr              =       cp->daddr.ip;
997         iph->saddr              =       saddr;
998         iph->ttl                =       ttl;
999         ip_select_ident(net, skb, NULL);
1000
1001         /* Another hack: avoid icmp_send in ip_fragment */
1002         skb->ignore_df = 1;
1003
1004         ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1005         if (ret == NF_ACCEPT)
1006                 ip_local_out(skb);
1007         else if (ret == NF_DROP)
1008                 kfree_skb(skb);
1009         rcu_read_unlock();
1010
1011         LeaveFunction(10);
1012
1013         return NF_STOLEN;
1014
1015   tx_error:
1016         if (!IS_ERR(skb))
1017                 kfree_skb(skb);
1018         rcu_read_unlock();
1019         LeaveFunction(10);
1020         return NF_STOLEN;
1021 }
1022
1023 #ifdef CONFIG_IP_VS_IPV6
1024 int
1025 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1026                      struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1027 {
1028         struct rt6_info *rt;            /* Route to the other host */
1029         struct in6_addr saddr;          /* Source for tunnel */
1030         struct net_device *tdev;        /* Device to other host */
1031         __u8 next_protocol = 0;
1032         __u32 payload_len = 0;
1033         __u8 dsfield = 0;
1034         __u8 ttl = 0;
1035         struct ipv6hdr  *iph;           /* Our new IP header */
1036         unsigned int max_headroom;      /* The extra header space needed */
1037         int ret, local;
1038
1039         EnterFunction(10);
1040
1041         rcu_read_lock();
1042         local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1043                                       &saddr, ipvsh, 1,
1044                                       IP_VS_RT_MODE_LOCAL |
1045                                       IP_VS_RT_MODE_NON_LOCAL |
1046                                       IP_VS_RT_MODE_TUNNEL);
1047         if (local < 0)
1048                 goto tx_error;
1049         if (local) {
1050                 rcu_read_unlock();
1051                 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1052         }
1053
1054         rt = (struct rt6_info *) skb_dst(skb);
1055         tdev = rt->dst.dev;
1056
1057         /*
1058          * Okay, now see if we can stuff it in the buffer as-is.
1059          */
1060         max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1061
1062         skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1063                                          &next_protocol, &payload_len,
1064                                          &dsfield, &ttl, NULL);
1065         if (IS_ERR(skb))
1066                 goto tx_error;
1067
1068         skb = iptunnel_handle_offloads(
1069                 skb, false, __tun_gso_type_mask(AF_INET6, cp->af));
1070         if (IS_ERR(skb))
1071                 goto tx_error;
1072
1073         skb->transport_header = skb->network_header;
1074
1075         skb_push(skb, sizeof(struct ipv6hdr));
1076         skb_reset_network_header(skb);
1077         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1078
1079         /*
1080          *      Push down and install the IPIP header.
1081          */
1082         iph                     =       ipv6_hdr(skb);
1083         iph->version            =       6;
1084         iph->nexthdr            =       next_protocol;
1085         iph->payload_len        =       htons(payload_len);
1086         memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1087         ipv6_change_dsfield(iph, 0, dsfield);
1088         iph->daddr = cp->daddr.in6;
1089         iph->saddr = saddr;
1090         iph->hop_limit          =       ttl;
1091
1092         /* Another hack: avoid icmp_send in ip_fragment */
1093         skb->ignore_df = 1;
1094
1095         ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1096         if (ret == NF_ACCEPT)
1097                 ip6_local_out(skb);
1098         else if (ret == NF_DROP)
1099                 kfree_skb(skb);
1100         rcu_read_unlock();
1101
1102         LeaveFunction(10);
1103
1104         return NF_STOLEN;
1105
1106 tx_error:
1107         if (!IS_ERR(skb))
1108                 kfree_skb(skb);
1109         rcu_read_unlock();
1110         LeaveFunction(10);
1111         return NF_STOLEN;
1112 }
1113 #endif
1114
1115
1116 /*
1117  *      Direct Routing transmitter
1118  *      Used for ANY protocol
1119  */
1120 int
1121 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1122               struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1123 {
1124         int local;
1125
1126         EnterFunction(10);
1127
1128         rcu_read_lock();
1129         local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
1130                                    IP_VS_RT_MODE_LOCAL |
1131                                    IP_VS_RT_MODE_NON_LOCAL |
1132                                    IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1133         if (local < 0)
1134                 goto tx_error;
1135         if (local) {
1136                 rcu_read_unlock();
1137                 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1138         }
1139
1140         ip_send_check(ip_hdr(skb));
1141
1142         /* Another hack: avoid icmp_send in ip_fragment */
1143         skb->ignore_df = 1;
1144
1145         ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1146         rcu_read_unlock();
1147
1148         LeaveFunction(10);
1149         return NF_STOLEN;
1150
1151   tx_error:
1152         kfree_skb(skb);
1153         rcu_read_unlock();
1154         LeaveFunction(10);
1155         return NF_STOLEN;
1156 }
1157
1158 #ifdef CONFIG_IP_VS_IPV6
1159 int
1160 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1161                  struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1162 {
1163         int local;
1164
1165         EnterFunction(10);
1166
1167         rcu_read_lock();
1168         local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1169                                       NULL, ipvsh, 0,
1170                                       IP_VS_RT_MODE_LOCAL |
1171                                       IP_VS_RT_MODE_NON_LOCAL |
1172                                       IP_VS_RT_MODE_KNOWN_NH);
1173         if (local < 0)
1174                 goto tx_error;
1175         if (local) {
1176                 rcu_read_unlock();
1177                 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1178         }
1179
1180         /* Another hack: avoid icmp_send in ip_fragment */
1181         skb->ignore_df = 1;
1182
1183         ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1184         rcu_read_unlock();
1185
1186         LeaveFunction(10);
1187         return NF_STOLEN;
1188
1189 tx_error:
1190         kfree_skb(skb);
1191         rcu_read_unlock();
1192         LeaveFunction(10);
1193         return NF_STOLEN;
1194 }
1195 #endif
1196
1197
1198 /*
1199  *      ICMP packet transmitter
1200  *      called by the ip_vs_in_icmp
1201  */
1202 int
1203 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1204                 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1205                 struct ip_vs_iphdr *iph)
1206 {
1207         struct rtable   *rt;    /* Route to the other host */
1208         int rc;
1209         int local;
1210         int rt_mode, was_input;
1211
1212         EnterFunction(10);
1213
1214         /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1215            forwarded directly here, because there is no need to
1216            translate address/port back */
1217         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1218                 if (cp->packet_xmit)
1219                         rc = cp->packet_xmit(skb, cp, pp, iph);
1220                 else
1221                         rc = NF_ACCEPT;
1222                 /* do not touch skb anymore */
1223                 atomic_inc(&cp->in_pkts);
1224                 goto out;
1225         }
1226
1227         /*
1228          * mangle and send the packet here (only for VS/NAT)
1229          */
1230         was_input = rt_is_input_route(skb_rtable(skb));
1231
1232         /* LOCALNODE from FORWARD hook is not supported */
1233         rt_mode = (hooknum != NF_INET_FORWARD) ?
1234                   IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1235                   IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1236         rcu_read_lock();
1237         local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1238                                    NULL, iph);
1239         if (local < 0)
1240                 goto tx_error;
1241         rt = skb_rtable(skb);
1242
1243         /*
1244          * Avoid duplicate tuple in reply direction for NAT traffic
1245          * to local address when connection is sync-ed
1246          */
1247 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1248         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1249                 enum ip_conntrack_info ctinfo;
1250                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1251
1252                 if (ct && !nf_ct_is_untracked(ct)) {
1253                         IP_VS_DBG(10, "%s(): "
1254                                   "stopping DNAT to local address %pI4\n",
1255                                   __func__, &cp->daddr.ip);
1256                         goto tx_error;
1257                 }
1258         }
1259 #endif
1260
1261         /* From world but DNAT to loopback address? */
1262         if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1263                 IP_VS_DBG(1, "%s(): "
1264                           "stopping DNAT to loopback %pI4\n",
1265                           __func__, &cp->daddr.ip);
1266                 goto tx_error;
1267         }
1268
1269         /* copy-on-write the packet before mangling it */
1270         if (!skb_make_writable(skb, offset))
1271                 goto tx_error;
1272
1273         if (skb_cow(skb, rt->dst.dev->hard_header_len))
1274                 goto tx_error;
1275
1276         ip_vs_nat_icmp(skb, pp, cp, 0);
1277
1278         /* Another hack: avoid icmp_send in ip_fragment */
1279         skb->ignore_df = 1;
1280
1281         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1282         rcu_read_unlock();
1283         goto out;
1284
1285   tx_error:
1286         kfree_skb(skb);
1287         rcu_read_unlock();
1288         rc = NF_STOLEN;
1289   out:
1290         LeaveFunction(10);
1291         return rc;
1292 }
1293
1294 #ifdef CONFIG_IP_VS_IPV6
1295 int
1296 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1297                 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1298                 struct ip_vs_iphdr *ipvsh)
1299 {
1300         struct rt6_info *rt;    /* Route to the other host */
1301         int rc;
1302         int local;
1303         int rt_mode;
1304
1305         EnterFunction(10);
1306
1307         /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1308            forwarded directly here, because there is no need to
1309            translate address/port back */
1310         if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1311                 if (cp->packet_xmit)
1312                         rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1313                 else
1314                         rc = NF_ACCEPT;
1315                 /* do not touch skb anymore */
1316                 atomic_inc(&cp->in_pkts);
1317                 goto out;
1318         }
1319
1320         /*
1321          * mangle and send the packet here (only for VS/NAT)
1322          */
1323
1324         /* LOCALNODE from FORWARD hook is not supported */
1325         rt_mode = (hooknum != NF_INET_FORWARD) ?
1326                   IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1327                   IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1328         rcu_read_lock();
1329         local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1330                                       NULL, ipvsh, 0, rt_mode);
1331         if (local < 0)
1332                 goto tx_error;
1333         rt = (struct rt6_info *) skb_dst(skb);
1334         /*
1335          * Avoid duplicate tuple in reply direction for NAT traffic
1336          * to local address when connection is sync-ed
1337          */
1338 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1339         if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1340                 enum ip_conntrack_info ctinfo;
1341                 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1342
1343                 if (ct && !nf_ct_is_untracked(ct)) {
1344                         IP_VS_DBG(10, "%s(): "
1345                                   "stopping DNAT to local address %pI6\n",
1346                                   __func__, &cp->daddr.in6);
1347                         goto tx_error;
1348                 }
1349         }
1350 #endif
1351
1352         /* From world but DNAT to loopback address? */
1353         if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1354             ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1355                 IP_VS_DBG(1, "%s(): "
1356                           "stopping DNAT to loopback %pI6\n",
1357                           __func__, &cp->daddr.in6);
1358                 goto tx_error;
1359         }
1360
1361         /* copy-on-write the packet before mangling it */
1362         if (!skb_make_writable(skb, offset))
1363                 goto tx_error;
1364
1365         if (skb_cow(skb, rt->dst.dev->hard_header_len))
1366                 goto tx_error;
1367
1368         ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1369
1370         /* Another hack: avoid icmp_send in ip_fragment */
1371         skb->ignore_df = 1;
1372
1373         rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
1374         rcu_read_unlock();
1375         goto out;
1376
1377 tx_error:
1378         kfree_skb(skb);
1379         rcu_read_unlock();
1380         rc = NF_STOLEN;
1381 out:
1382         LeaveFunction(10);
1383         return rc;
1384 }
1385 #endif