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