2 * ip_vs_xmit.c: various packet transmitters for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
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.
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
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
28 #define KMSG_COMPONENT "IPVS"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/tcp.h> /* for tcphdr */
35 #include <net/tcp.h> /* for csum_tcpudp_magic */
37 #include <net/icmp.h> /* for icmp_send */
38 #include <net/route.h> /* for ip_route_output */
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>
47 #include <net/ip_vs.h>
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
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 */
60 static inline struct ip_vs_dest_dst *ip_vs_dest_dst_alloc(void)
62 return kmalloc(sizeof(struct ip_vs_dest_dst), GFP_ATOMIC);
65 static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
71 * Destination cache to speed up outgoing route lookup
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)
77 struct ip_vs_dest_dst *old;
79 old = rcu_dereference_protected(dest->dest_dst,
80 lockdep_is_held(&dest->dst_lock));
83 dest_dst->dst_cache = dst;
84 dest_dst->dst_cookie = dst_cookie;
86 rcu_assign_pointer(dest->dest_dst, dest_dst);
89 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
92 static inline struct ip_vs_dest_dst *
93 __ip_vs_dst_check(struct ip_vs_dest *dest)
95 struct ip_vs_dest_dst *dest_dst = rcu_dereference(dest->dest_dst);
96 struct dst_entry *dst;
100 dst = dest_dst->dst_cache;
102 dst->ops->check(dst, dest_dst->dst_cookie) == NULL)
108 __mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
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.
114 if (IP6CB(skb)->frag_max_size > mtu)
115 return true; /* largest fragment violate MTU */
117 else if (skb->len > mtu && !skb_is_gso(skb)) {
118 return true; /* Packet size violate MTU size */
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)
131 memset(&fl4, 0, sizeof(fl4));
133 fl4.saddr = (rt_mode & IP_VS_RT_MODE_CONNECT) ? *saddr : 0;
134 fl4.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
135 FLOWI_FLAG_KNOWN_NH : 0;
138 rt = ip_route_output_key(net, &fl4);
140 /* Invalid saddr ? */
141 if (PTR_ERR(rt) == -EINVAL && *saddr &&
142 rt_mode & IP_VS_RT_MODE_CONNECT && !loop) {
144 flowi4_update_output(&fl4, 0, 0, daddr, 0);
147 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", &daddr);
149 } else if (!*saddr && rt_mode & IP_VS_RT_MODE_CONNECT && fl4.saddr) {
152 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
160 /* Get route to destination or remote server */
162 __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
163 __be32 daddr, int rt_mode, __be32 *ret_saddr)
165 struct net *net = dev_net(skb_dst(skb)->dev);
166 struct netns_ipvs *ipvs = net_ipvs(net);
167 struct ip_vs_dest_dst *dest_dst;
168 struct rtable *rt; /* Route to the other host */
169 struct rtable *ort; /* Original route */
173 int local, noref = 1;
176 dest_dst = __ip_vs_dst_check(dest);
177 if (likely(dest_dst))
178 rt = (struct rtable *) dest_dst->dst_cache;
180 dest_dst = ip_vs_dest_dst_alloc();
181 spin_lock_bh(&dest->dst_lock);
183 __ip_vs_dst_set(dest, NULL, NULL, 0);
184 spin_unlock_bh(&dest->dst_lock);
187 rt = do_output_route4(net, dest->addr.ip, rt_mode,
188 &dest_dst->dst_saddr.ip);
190 __ip_vs_dst_set(dest, NULL, NULL, 0);
191 spin_unlock_bh(&dest->dst_lock);
192 ip_vs_dest_dst_free(dest_dst);
195 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
196 spin_unlock_bh(&dest->dst_lock);
197 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
198 &dest->addr.ip, &dest_dst->dst_saddr.ip,
199 atomic_read(&rt->dst.__refcnt));
201 daddr = dest->addr.ip;
203 *ret_saddr = dest_dst->dst_saddr.ip;
205 __be32 saddr = htonl(INADDR_ANY);
209 /* For such unconfigured boxes avoid many route lookups
210 * for performance reasons because we do not remember saddr
212 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
213 rt = do_output_route4(net, daddr, rt_mode, &saddr);
220 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
221 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
223 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
224 (rt->rt_flags & RTCF_LOCAL) ?
225 "local":"non-local", &daddr);
229 if (likely(!local)) {
230 if (unlikely(ipv4_is_loopback(iph->saddr))) {
231 IP_VS_DBG_RL("Stopping traffic from loopback address "
232 "%pI4 to non-local address, dest: %pI4\n",
233 &iph->saddr, &daddr);
237 ort = skb_rtable(skb);
238 if (!(rt_mode & IP_VS_RT_MODE_RDR) &&
239 !(ort->rt_flags & RTCF_LOCAL)) {
240 IP_VS_DBG_RL("Redirect from non-local address %pI4 to "
241 "local requires NAT method, dest: %pI4\n",
242 &iph->daddr, &daddr);
245 /* skb to local stack, preserve old route */
251 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
252 mtu = dst_mtu(&rt->dst);
253 df = iph->frag_off & htons(IP_DF);
255 struct sock *sk = skb->sk;
257 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
259 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
262 ort = skb_rtable(skb);
263 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT)
264 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
265 /* MTU check allowed? */
266 df = sysctl_pmtu_disc(ipvs) ? iph->frag_off & htons(IP_DF) : 0;
270 if (unlikely(df && skb->len > mtu && !skb_is_gso(skb))) {
271 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
272 IP_VS_DBG(1, "frag needed for %pI4\n", &iph->saddr);
279 skb_dst_set_noref_force(skb, &rt->dst);
281 skb_dst_set(skb, dst_clone(&rt->dst));
283 skb_dst_set(skb, &rt->dst);
293 dst_link_failure(skb);
297 #ifdef CONFIG_IP_VS_IPV6
299 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
301 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
304 static struct dst_entry *
305 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
306 struct in6_addr *ret_saddr, int do_xfrm)
308 struct dst_entry *dst;
309 struct flowi6 fl6 = {
313 dst = ip6_route_output(net, NULL, &fl6);
318 if (ipv6_addr_any(&fl6.saddr) &&
319 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
320 &fl6.daddr, 0, &fl6.saddr) < 0)
323 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
329 *ret_saddr = fl6.saddr;
334 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
339 * Get route to destination or remote server
342 __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
343 struct in6_addr *daddr, struct in6_addr *ret_saddr,
344 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
346 struct net *net = dev_net(skb_dst(skb)->dev);
347 struct ip_vs_dest_dst *dest_dst;
348 struct rt6_info *rt; /* Route to the other host */
349 struct rt6_info *ort; /* Original route */
350 struct dst_entry *dst;
352 int local, noref = 1;
355 dest_dst = __ip_vs_dst_check(dest);
356 if (likely(dest_dst))
357 rt = (struct rt6_info *) dest_dst->dst_cache;
361 dest_dst = ip_vs_dest_dst_alloc();
362 spin_lock_bh(&dest->dst_lock);
364 __ip_vs_dst_set(dest, NULL, NULL, 0);
365 spin_unlock_bh(&dest->dst_lock);
368 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
369 &dest_dst->dst_saddr.in6,
372 __ip_vs_dst_set(dest, NULL, NULL, 0);
373 spin_unlock_bh(&dest->dst_lock);
374 ip_vs_dest_dst_free(dest_dst);
377 rt = (struct rt6_info *) dst;
378 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
379 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
380 spin_unlock_bh(&dest->dst_lock);
381 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
382 &dest->addr.in6, &dest_dst->dst_saddr.in6,
383 atomic_read(&rt->dst.__refcnt));
386 *ret_saddr = dest_dst->dst_saddr.in6;
389 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
392 rt = (struct rt6_info *) dst;
395 local = __ip_vs_is_local_route6(rt);
396 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
398 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6c\n",
399 local ? "local":"non-local", daddr);
402 if (likely(!local)) {
403 if (unlikely((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
404 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
405 IPV6_ADDR_LOOPBACK)) {
406 IP_VS_DBG_RL("Stopping traffic from loopback address "
407 "%pI6c to non-local address, "
409 &ipv6_hdr(skb)->saddr, daddr);
413 ort = (struct rt6_info *) skb_dst(skb);
414 if (!(rt_mode & IP_VS_RT_MODE_RDR) &&
415 !__ip_vs_is_local_route6(ort)) {
416 IP_VS_DBG_RL("Redirect from non-local address %pI6c "
417 "to local requires NAT method, "
419 &ipv6_hdr(skb)->daddr, daddr);
422 /* skb to local stack, preserve old route */
424 dst_release(&rt->dst);
429 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
430 mtu = dst_mtu(&rt->dst);
432 struct sock *sk = skb->sk;
434 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
435 if (mtu < IPV6_MIN_MTU) {
436 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
440 ort = (struct rt6_info *) skb_dst(skb);
441 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT)
442 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
445 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
447 skb->dev = net->loopback_dev;
448 /* only send ICMP too big on first fragment */
449 if (!ipvsh->fragoffs)
450 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
451 IP_VS_DBG(1, "frag needed for %pI6c\n", &ipv6_hdr(skb)->saddr);
458 skb_dst_set_noref_force(skb, &rt->dst);
460 skb_dst_set(skb, dst_clone(&rt->dst));
462 skb_dst_set(skb, &rt->dst);
468 dst_release(&rt->dst);
472 dst_link_failure(skb);
478 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
479 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
480 struct ip_vs_conn *cp)
484 skb->ipvs_property = 1;
485 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
486 ret = ip_vs_confirm_conntrack(skb);
487 if (ret == NF_ACCEPT) {
489 skb_forward_csum(skb);
494 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
495 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
496 struct ip_vs_conn *cp, int local)
500 skb->ipvs_property = 1;
501 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
504 ip_vs_update_conntrack(skb, cp, 1);
506 skb_forward_csum(skb);
507 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
514 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
515 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
516 struct ip_vs_conn *cp, int local)
520 skb->ipvs_property = 1;
521 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
524 skb_forward_csum(skb);
525 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
534 * NULL transmitter (do nothing except return NF_ACCEPT)
537 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
538 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
540 /* we do not touch skb and do not need pskb ptr */
541 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
547 * Let packets bypass the destination when the destination is not
548 * available, it may be only used in transparent cache cluster.
551 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
552 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
554 struct iphdr *iph = ip_hdr(skb);
559 if (__ip_vs_get_out_rt(skb, NULL, iph->daddr, IP_VS_RT_MODE_NON_LOCAL,
565 /* Another hack: avoid icmp_send in ip_fragment */
568 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
581 #ifdef CONFIG_IP_VS_IPV6
583 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
584 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
589 if (__ip_vs_get_out_rt_v6(skb, NULL, &ipvsh->daddr.in6, NULL,
590 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
593 /* Another hack: avoid icmp_send in ip_fragment */
596 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
611 * NAT transmitter (only for outside-to-inside nat forwarding)
612 * Not used for related ICMP
615 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
616 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
618 struct rtable *rt; /* Route to the other host */
619 int local, rc, was_input;
624 /* check if it is a connection of no-client-port */
625 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
628 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
631 ip_vs_conn_fill_cport(cp, *p);
632 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
635 was_input = rt_is_input_route(skb_rtable(skb));
636 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
637 IP_VS_RT_MODE_LOCAL |
638 IP_VS_RT_MODE_NON_LOCAL |
639 IP_VS_RT_MODE_RDR, NULL);
642 rt = skb_rtable(skb);
644 * Avoid duplicate tuple in reply direction for NAT traffic
645 * to local address when connection is sync-ed
647 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
648 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
649 enum ip_conntrack_info ctinfo;
650 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
652 if (ct && !nf_ct_is_untracked(ct)) {
653 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
655 "stopping DNAT to local address");
661 /* From world but DNAT to loopback address? */
662 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
663 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
664 "stopping DNAT to loopback address");
668 /* copy-on-write the packet before mangling it */
669 if (!skb_make_writable(skb, sizeof(struct iphdr)))
672 if (skb_cow(skb, rt->dst.dev->hard_header_len))
675 /* mangle the packet */
676 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
678 ip_hdr(skb)->daddr = cp->daddr.ip;
679 ip_send_check(ip_hdr(skb));
681 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
683 /* FIXME: when application helper enlarges the packet and the length
684 is larger than the MTU of outgoing device, there will be still
687 /* Another hack: avoid icmp_send in ip_fragment */
690 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
703 #ifdef CONFIG_IP_VS_IPV6
705 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
706 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
708 struct rt6_info *rt; /* Route to the other host */
714 /* check if it is a connection of no-client-port */
715 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
717 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
720 ip_vs_conn_fill_cport(cp, *p);
721 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
724 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
726 IP_VS_RT_MODE_LOCAL |
727 IP_VS_RT_MODE_NON_LOCAL |
731 rt = (struct rt6_info *) skb_dst(skb);
733 * Avoid duplicate tuple in reply direction for NAT traffic
734 * to local address when connection is sync-ed
736 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
737 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
738 enum ip_conntrack_info ctinfo;
739 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
741 if (ct && !nf_ct_is_untracked(ct)) {
742 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
743 "ip_vs_nat_xmit_v6(): "
744 "stopping DNAT to local address");
750 /* From world but DNAT to loopback address? */
751 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
752 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
753 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
754 "ip_vs_nat_xmit_v6(): "
755 "stopping DNAT to loopback address");
759 /* copy-on-write the packet before mangling it */
760 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
763 if (skb_cow(skb, rt->dst.dev->hard_header_len))
766 /* mangle the packet */
767 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
769 ipv6_hdr(skb)->daddr = cp->daddr.in6;
771 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
773 /* FIXME: when application helper enlarges the packet and the length
774 is larger than the MTU of outgoing device, there will be still
777 /* Another hack: avoid icmp_send in ip_fragment */
780 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
796 * IP Tunneling transmitter
798 * This function encapsulates the packet in a new IP packet, its
799 * destination will be set to cp->daddr. Most code of this function
800 * is taken from ipip.c.
802 * It is used in VS/TUN cluster. The load balancer selects a real
803 * server from a cluster based on a scheduling algorithm,
804 * encapsulates the request packet and forwards it to the selected
805 * server. For example, all real servers are configured with
806 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
807 * the encapsulated packet, it will decapsulate the packet, processe
808 * the request and return the response packets directly to the client
809 * without passing the load balancer. This can greatly increase the
810 * scalability of virtual server.
812 * Used for ANY protocol
815 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
816 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
818 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
819 struct rtable *rt; /* Route to the other host */
820 __be32 saddr; /* Source for tunnel */
821 struct net_device *tdev; /* Device to other host */
822 struct iphdr *old_iph = ip_hdr(skb);
823 u8 tos = old_iph->tos;
825 struct iphdr *iph; /* Our new IP header */
826 unsigned int max_headroom; /* The extra header space needed */
832 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
833 IP_VS_RT_MODE_LOCAL |
834 IP_VS_RT_MODE_NON_LOCAL |
835 IP_VS_RT_MODE_CONNECT |
836 IP_VS_RT_MODE_TUNNEL, &saddr);
841 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
844 rt = skb_rtable(skb);
847 /* Copy DF, reset fragment offset and MF */
848 df = sysctl_pmtu_disc(ipvs) ? old_iph->frag_off & htons(IP_DF) : 0;
851 * Okay, now see if we can stuff it in the buffer as-is.
853 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
855 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
856 struct sk_buff *new_skb =
857 skb_realloc_headroom(skb, max_headroom);
863 old_iph = ip_hdr(skb);
866 /* fix old IP header checksum */
867 ip_send_check(old_iph);
869 skb = iptunnel_handle_offloads(skb, false, SKB_GSO_IPIP);
873 skb->transport_header = skb->network_header;
875 skb_push(skb, sizeof(struct iphdr));
876 skb_reset_network_header(skb);
877 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
880 * Push down and install the IPIP header.
884 iph->ihl = sizeof(struct iphdr)>>2;
886 iph->protocol = IPPROTO_IPIP;
888 iph->daddr = cp->daddr.ip;
890 iph->ttl = old_iph->ttl;
891 ip_select_ident(skb, NULL);
893 /* Another hack: avoid icmp_send in ip_fragment */
896 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
897 if (ret == NF_ACCEPT)
899 else if (ret == NF_DROP)
915 #ifdef CONFIG_IP_VS_IPV6
917 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
918 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
920 struct rt6_info *rt; /* Route to the other host */
921 struct in6_addr saddr; /* Source for tunnel */
922 struct net_device *tdev; /* Device to other host */
923 struct ipv6hdr *old_iph = ipv6_hdr(skb);
924 struct ipv6hdr *iph; /* Our new IP header */
925 unsigned int max_headroom; /* The extra header space needed */
931 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
933 IP_VS_RT_MODE_LOCAL |
934 IP_VS_RT_MODE_NON_LOCAL |
935 IP_VS_RT_MODE_TUNNEL);
940 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
943 rt = (struct rt6_info *) skb_dst(skb);
947 * Okay, now see if we can stuff it in the buffer as-is.
949 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
951 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
952 struct sk_buff *new_skb =
953 skb_realloc_headroom(skb, max_headroom);
959 old_iph = ipv6_hdr(skb);
962 /* GSO: we need to provide proper SKB_GSO_ value for IPv6 */
963 skb = iptunnel_handle_offloads(skb, false, 0); /* SKB_GSO_SIT/IPV6 */
967 skb->transport_header = skb->network_header;
969 skb_push(skb, sizeof(struct ipv6hdr));
970 skb_reset_network_header(skb);
971 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
974 * Push down and install the IPIP header.
978 iph->nexthdr = IPPROTO_IPV6;
979 iph->payload_len = old_iph->payload_len;
980 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
981 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
982 ipv6_change_dsfield(iph, 0, ipv6_get_dsfield(old_iph));
983 iph->daddr = cp->daddr.in6;
985 iph->hop_limit = old_iph->hop_limit;
987 /* Another hack: avoid icmp_send in ip_fragment */
990 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
991 if (ret == NF_ACCEPT)
993 else if (ret == NF_DROP)
1012 * Direct Routing transmitter
1013 * Used for ANY protocol
1016 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1017 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1024 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1025 IP_VS_RT_MODE_LOCAL |
1026 IP_VS_RT_MODE_NON_LOCAL |
1027 IP_VS_RT_MODE_KNOWN_NH, NULL);
1032 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1035 ip_send_check(ip_hdr(skb));
1037 /* Another hack: avoid icmp_send in ip_fragment */
1040 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1053 #ifdef CONFIG_IP_VS_IPV6
1055 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1056 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1063 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1065 IP_VS_RT_MODE_LOCAL |
1066 IP_VS_RT_MODE_NON_LOCAL);
1071 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1074 /* Another hack: avoid icmp_send in ip_fragment */
1077 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1093 * ICMP packet transmitter
1094 * called by the ip_vs_in_icmp
1097 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1098 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1099 struct ip_vs_iphdr *iph)
1101 struct rtable *rt; /* Route to the other host */
1104 int rt_mode, was_input;
1108 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1109 forwarded directly here, because there is no need to
1110 translate address/port back */
1111 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1112 if (cp->packet_xmit)
1113 rc = cp->packet_xmit(skb, cp, pp, iph);
1116 /* do not touch skb anymore */
1117 atomic_inc(&cp->in_pkts);
1122 * mangle and send the packet here (only for VS/NAT)
1124 was_input = rt_is_input_route(skb_rtable(skb));
1126 /* LOCALNODE from FORWARD hook is not supported */
1127 rt_mode = (hooknum != NF_INET_FORWARD) ?
1128 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1129 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1131 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, rt_mode, NULL);
1134 rt = skb_rtable(skb);
1137 * Avoid duplicate tuple in reply direction for NAT traffic
1138 * to local address when connection is sync-ed
1140 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1141 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1142 enum ip_conntrack_info ctinfo;
1143 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1145 if (ct && !nf_ct_is_untracked(ct)) {
1146 IP_VS_DBG(10, "%s(): "
1147 "stopping DNAT to local address %pI4\n",
1148 __func__, &cp->daddr.ip);
1154 /* From world but DNAT to loopback address? */
1155 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1156 IP_VS_DBG(1, "%s(): "
1157 "stopping DNAT to loopback %pI4\n",
1158 __func__, &cp->daddr.ip);
1162 /* copy-on-write the packet before mangling it */
1163 if (!skb_make_writable(skb, offset))
1166 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1169 ip_vs_nat_icmp(skb, pp, cp, 0);
1171 /* Another hack: avoid icmp_send in ip_fragment */
1174 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1187 #ifdef CONFIG_IP_VS_IPV6
1189 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1190 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1191 struct ip_vs_iphdr *ipvsh)
1193 struct rt6_info *rt; /* Route to the other host */
1200 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1201 forwarded directly here, because there is no need to
1202 translate address/port back */
1203 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1204 if (cp->packet_xmit)
1205 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1208 /* do not touch skb anymore */
1209 atomic_inc(&cp->in_pkts);
1214 * mangle and send the packet here (only for VS/NAT)
1217 /* LOCALNODE from FORWARD hook is not supported */
1218 rt_mode = (hooknum != NF_INET_FORWARD) ?
1219 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1220 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1222 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1226 rt = (struct rt6_info *) skb_dst(skb);
1228 * Avoid duplicate tuple in reply direction for NAT traffic
1229 * to local address when connection is sync-ed
1231 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1232 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1233 enum ip_conntrack_info ctinfo;
1234 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1236 if (ct && !nf_ct_is_untracked(ct)) {
1237 IP_VS_DBG(10, "%s(): "
1238 "stopping DNAT to local address %pI6\n",
1239 __func__, &cp->daddr.in6);
1245 /* From world but DNAT to loopback address? */
1246 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1247 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1248 IP_VS_DBG(1, "%s(): "
1249 "stopping DNAT to loopback %pI6\n",
1250 __func__, &cp->daddr.in6);
1254 /* copy-on-write the packet before mangling it */
1255 if (!skb_make_writable(skb, offset))
1258 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1261 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1263 /* Another hack: avoid icmp_send in ip_fragment */
1266 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);