~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/ipvs/ip_vs_xmit.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp