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 #ifdef CONFIG_IP_VS_IPV6
161 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
163 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
167 static inline bool crosses_local_route_boundary(int skb_af, struct sk_buff *skb,
169 bool new_rt_is_local)
171 bool rt_mode_allow_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
172 bool rt_mode_allow_non_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
173 bool rt_mode_allow_redirect = !!(rt_mode & IP_VS_RT_MODE_RDR);
174 bool source_is_loopback;
175 bool old_rt_is_local;
177 #ifdef CONFIG_IP_VS_IPV6
178 if (skb_af == AF_INET6) {
179 int addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
182 (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
183 (addr_type & IPV6_ADDR_LOOPBACK);
184 old_rt_is_local = __ip_vs_is_local_route6(
185 (struct rt6_info *)skb_dst(skb));
189 source_is_loopback = ipv4_is_loopback(ip_hdr(skb)->saddr);
190 old_rt_is_local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
193 if (unlikely(new_rt_is_local)) {
194 if (!rt_mode_allow_local)
196 if (!rt_mode_allow_redirect && !old_rt_is_local)
199 if (!rt_mode_allow_non_local)
201 if (source_is_loopback)
207 static inline void maybe_update_pmtu(int skb_af, struct sk_buff *skb, int mtu)
209 struct sock *sk = skb->sk;
210 struct rtable *ort = skb_rtable(skb);
212 if (!skb->dev && sk && sk_fullsock(sk))
213 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
216 static inline bool ensure_mtu_is_adequate(int skb_af, int rt_mode,
217 struct ip_vs_iphdr *ipvsh,
218 struct sk_buff *skb, int mtu)
220 #ifdef CONFIG_IP_VS_IPV6
221 if (skb_af == AF_INET6) {
222 struct net *net = dev_net(skb_dst(skb)->dev);
224 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
226 skb->dev = net->loopback_dev;
227 /* only send ICMP too big on first fragment */
228 if (!ipvsh->fragoffs)
229 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
230 IP_VS_DBG(1, "frag needed for %pI6c\n",
231 &ipv6_hdr(skb)->saddr);
237 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
239 /* If we're going to tunnel the packet and pmtu discovery
240 * is disabled, we'll just fragment it anyway
242 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
245 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_DF) &&
246 skb->len > mtu && !skb_is_gso(skb))) {
247 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
249 IP_VS_DBG(1, "frag needed for %pI4\n",
250 &ip_hdr(skb)->saddr);
258 /* Get route to destination or remote server */
260 __ip_vs_get_out_rt(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
261 __be32 daddr, int rt_mode, __be32 *ret_saddr,
262 struct ip_vs_iphdr *ipvsh)
264 struct net *net = dev_net(skb_dst(skb)->dev);
265 struct ip_vs_dest_dst *dest_dst;
266 struct rtable *rt; /* Route to the other host */
268 int local, noref = 1;
271 dest_dst = __ip_vs_dst_check(dest);
272 if (likely(dest_dst))
273 rt = (struct rtable *) dest_dst->dst_cache;
275 dest_dst = ip_vs_dest_dst_alloc();
276 spin_lock_bh(&dest->dst_lock);
278 __ip_vs_dst_set(dest, NULL, NULL, 0);
279 spin_unlock_bh(&dest->dst_lock);
282 rt = do_output_route4(net, dest->addr.ip, rt_mode,
283 &dest_dst->dst_saddr.ip);
285 __ip_vs_dst_set(dest, NULL, NULL, 0);
286 spin_unlock_bh(&dest->dst_lock);
287 ip_vs_dest_dst_free(dest_dst);
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));
297 *ret_saddr = dest_dst->dst_saddr.ip;
299 __be32 saddr = htonl(INADDR_ANY);
303 /* For such unconfigured boxes avoid many route lookups
304 * for performance reasons because we do not remember saddr
306 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
307 rt = do_output_route4(net, daddr, rt_mode, &saddr);
314 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
315 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
317 IP_VS_DBG_RL("We are crossing local and non-local addresses"
318 " daddr=%pI4\n", &daddr);
322 if (unlikely(local)) {
323 /* skb to local stack, preserve old route */
329 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
330 mtu = dst_mtu(&rt->dst);
332 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
334 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
337 maybe_update_pmtu(skb_af, skb, mtu);
340 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
346 skb_dst_set_noref(skb, &rt->dst);
348 skb_dst_set(skb, dst_clone(&rt->dst));
350 skb_dst_set(skb, &rt->dst);
360 dst_link_failure(skb);
364 #ifdef CONFIG_IP_VS_IPV6
365 static struct dst_entry *
366 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
367 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
369 struct dst_entry *dst;
370 struct flowi6 fl6 = {
374 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
375 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
377 dst = ip6_route_output(net, NULL, &fl6);
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)
387 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
393 *ret_saddr = fl6.saddr;
398 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
403 * Get route to destination or remote server
406 __ip_vs_get_out_rt_v6(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
407 struct in6_addr *daddr, struct in6_addr *ret_saddr,
408 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
410 struct net *net = dev_net(skb_dst(skb)->dev);
411 struct ip_vs_dest_dst *dest_dst;
412 struct rt6_info *rt; /* Route to the other host */
413 struct dst_entry *dst;
415 int local, noref = 1;
418 dest_dst = __ip_vs_dst_check(dest);
419 if (likely(dest_dst))
420 rt = (struct rt6_info *) dest_dst->dst_cache;
424 dest_dst = ip_vs_dest_dst_alloc();
425 spin_lock_bh(&dest->dst_lock);
427 __ip_vs_dst_set(dest, NULL, NULL, 0);
428 spin_unlock_bh(&dest->dst_lock);
431 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
432 &dest_dst->dst_saddr.in6,
435 __ip_vs_dst_set(dest, NULL, NULL, 0);
436 spin_unlock_bh(&dest->dst_lock);
437 ip_vs_dest_dst_free(dest_dst);
440 rt = (struct rt6_info *) dst;
441 cookie = rt6_get_cookie(rt);
442 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
443 spin_unlock_bh(&dest->dst_lock);
444 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
445 &dest->addr.in6, &dest_dst->dst_saddr.in6,
446 atomic_read(&rt->dst.__refcnt));
449 *ret_saddr = dest_dst->dst_saddr.in6;
452 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
456 rt = (struct rt6_info *) dst;
459 local = __ip_vs_is_local_route6(rt);
461 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
463 IP_VS_DBG_RL("We are crossing local and non-local addresses"
464 " daddr=%pI6\n", daddr);
468 if (unlikely(local)) {
469 /* skb to local stack, preserve old route */
471 dst_release(&rt->dst);
476 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
477 mtu = dst_mtu(&rt->dst);
479 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
480 if (mtu < IPV6_MIN_MTU) {
481 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
485 maybe_update_pmtu(skb_af, skb, mtu);
488 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
494 skb_dst_set_noref(skb, &rt->dst);
496 skb_dst_set(skb, dst_clone(&rt->dst));
498 skb_dst_set(skb, &rt->dst);
504 dst_release(&rt->dst);
508 dst_link_failure(skb);
514 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
515 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
516 struct ip_vs_conn *cp)
520 skb->ipvs_property = 1;
521 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
522 ret = ip_vs_confirm_conntrack(skb);
523 if (ret == NF_ACCEPT) {
525 skb_forward_csum(skb);
530 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
531 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
532 struct ip_vs_conn *cp, int local)
536 skb->ipvs_property = 1;
537 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
540 ip_vs_update_conntrack(skb, cp, 1);
542 skb_forward_csum(skb);
543 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
544 NULL, skb_dst(skb)->dev, dst_output_sk);
550 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
551 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
552 struct ip_vs_conn *cp, int local)
556 skb->ipvs_property = 1;
557 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
560 skb_forward_csum(skb);
561 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
562 NULL, skb_dst(skb)->dev, dst_output_sk);
570 * NULL transmitter (do nothing except return NF_ACCEPT)
573 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
574 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
576 /* we do not touch skb and do not need pskb ptr */
577 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
583 * Let packets bypass the destination when the destination is not
584 * available, it may be only used in transparent cache cluster.
587 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
588 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
590 struct iphdr *iph = ip_hdr(skb);
595 if (__ip_vs_get_out_rt(cp->af, skb, NULL, iph->daddr,
596 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
601 /* Another hack: avoid icmp_send in ip_fragment */
604 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
617 #ifdef CONFIG_IP_VS_IPV6
619 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
620 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
625 if (__ip_vs_get_out_rt_v6(cp->af, skb, NULL, &ipvsh->daddr.in6, NULL,
626 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
629 /* Another hack: avoid icmp_send in ip_fragment */
632 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
647 * NAT transmitter (only for outside-to-inside nat forwarding)
648 * Not used for related ICMP
651 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
652 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
654 struct rtable *rt; /* Route to the other host */
655 int local, rc, was_input;
660 /* check if it is a connection of no-client-port */
661 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
664 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
667 ip_vs_conn_fill_cport(cp, *p);
668 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
671 was_input = rt_is_input_route(skb_rtable(skb));
672 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
673 IP_VS_RT_MODE_LOCAL |
674 IP_VS_RT_MODE_NON_LOCAL |
675 IP_VS_RT_MODE_RDR, NULL, ipvsh);
678 rt = skb_rtable(skb);
680 * Avoid duplicate tuple in reply direction for NAT traffic
681 * to local address when connection is sync-ed
683 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
684 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
685 enum ip_conntrack_info ctinfo;
686 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
688 if (ct && !nf_ct_is_untracked(ct)) {
689 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
691 "stopping DNAT to local address");
697 /* From world but DNAT to loopback address? */
698 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
699 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
700 "stopping DNAT to loopback address");
704 /* copy-on-write the packet before mangling it */
705 if (!skb_make_writable(skb, sizeof(struct iphdr)))
708 if (skb_cow(skb, rt->dst.dev->hard_header_len))
711 /* mangle the packet */
712 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
714 ip_hdr(skb)->daddr = cp->daddr.ip;
715 ip_send_check(ip_hdr(skb));
717 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
719 /* FIXME: when application helper enlarges the packet and the length
720 is larger than the MTU of outgoing device, there will be still
723 /* Another hack: avoid icmp_send in ip_fragment */
726 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
739 #ifdef CONFIG_IP_VS_IPV6
741 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
742 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
744 struct rt6_info *rt; /* Route to the other host */
750 /* check if it is a connection of no-client-port */
751 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
753 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
756 ip_vs_conn_fill_cport(cp, *p);
757 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
760 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
762 IP_VS_RT_MODE_LOCAL |
763 IP_VS_RT_MODE_NON_LOCAL |
767 rt = (struct rt6_info *) skb_dst(skb);
769 * Avoid duplicate tuple in reply direction for NAT traffic
770 * to local address when connection is sync-ed
772 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
773 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
774 enum ip_conntrack_info ctinfo;
775 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
777 if (ct && !nf_ct_is_untracked(ct)) {
778 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
779 "ip_vs_nat_xmit_v6(): "
780 "stopping DNAT to local address");
786 /* From world but DNAT to loopback address? */
787 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
788 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
789 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
790 "ip_vs_nat_xmit_v6(): "
791 "stopping DNAT to loopback address");
795 /* copy-on-write the packet before mangling it */
796 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
799 if (skb_cow(skb, rt->dst.dev->hard_header_len))
802 /* mangle the packet */
803 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
805 ipv6_hdr(skb)->daddr = cp->daddr.in6;
807 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
809 /* FIXME: when application helper enlarges the packet and the length
810 is larger than the MTU of outgoing device, there will be still
813 /* Another hack: avoid icmp_send in ip_fragment */
816 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
830 /* When forwarding a packet, we must ensure that we've got enough headroom
831 * for the encapsulation packet in the skb. This also gives us an
832 * opportunity to figure out what the payload_len, dsfield, ttl, and df
833 * values should be, so that we won't need to look at the old ip header
836 static struct sk_buff *
837 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
838 unsigned int max_headroom, __u8 *next_protocol,
839 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
842 struct sk_buff *new_skb = NULL;
843 struct iphdr *old_iph = NULL;
844 #ifdef CONFIG_IP_VS_IPV6
845 struct ipv6hdr *old_ipv6h = NULL;
848 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
849 new_skb = skb_realloc_headroom(skb, max_headroom);
853 skb_set_owner_w(new_skb, skb->sk);
858 #ifdef CONFIG_IP_VS_IPV6
859 if (skb_af == AF_INET6) {
860 old_ipv6h = ipv6_hdr(skb);
861 *next_protocol = IPPROTO_IPV6;
864 ntohs(old_ipv6h->payload_len) +
866 *dsfield = ipv6_get_dsfield(old_ipv6h);
867 *ttl = old_ipv6h->hop_limit;
873 old_iph = ip_hdr(skb);
874 /* Copy DF, reset fragment offset and MF */
876 *df = (old_iph->frag_off & htons(IP_DF));
877 *next_protocol = IPPROTO_IPIP;
879 /* fix old IP header checksum */
880 ip_send_check(old_iph);
881 *dsfield = ipv4_get_dsfield(old_iph);
884 *payload_len = ntohs(old_iph->tot_len);
890 return ERR_PTR(-ENOMEM);
893 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
895 if (encaps_af == AF_INET) {
896 if (orig_af == AF_INET)
902 /* GSO: we need to provide proper SKB_GSO_ value for IPv6:
909 * IP Tunneling transmitter
911 * This function encapsulates the packet in a new IP packet, its
912 * destination will be set to cp->daddr. Most code of this function
913 * is taken from ipip.c.
915 * It is used in VS/TUN cluster. The load balancer selects a real
916 * server from a cluster based on a scheduling algorithm,
917 * encapsulates the request packet and forwards it to the selected
918 * server. For example, all real servers are configured with
919 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
920 * the encapsulated packet, it will decapsulate the packet, processe
921 * the request and return the response packets directly to the client
922 * without passing the load balancer. This can greatly increase the
923 * scalability of virtual server.
925 * Used for ANY protocol
928 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
929 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
931 struct net *net = skb_net(skb);
932 struct netns_ipvs *ipvs = net_ipvs(net);
933 struct rtable *rt; /* Route to the other host */
934 __be32 saddr; /* Source for tunnel */
935 struct net_device *tdev; /* Device to other host */
936 __u8 next_protocol = 0;
941 struct iphdr *iph; /* Our new IP header */
942 unsigned int max_headroom; /* The extra header space needed */
948 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
949 IP_VS_RT_MODE_LOCAL |
950 IP_VS_RT_MODE_NON_LOCAL |
951 IP_VS_RT_MODE_CONNECT |
952 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
957 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
960 rt = skb_rtable(skb);
964 * Okay, now see if we can stuff it in the buffer as-is.
966 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
968 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
969 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
970 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
971 &next_protocol, NULL, &dsfield,
976 skb = iptunnel_handle_offloads(
977 skb, false, __tun_gso_type_mask(AF_INET, cp->af));
981 skb->transport_header = skb->network_header;
983 skb_push(skb, sizeof(struct iphdr));
984 skb_reset_network_header(skb);
985 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
988 * Push down and install the IPIP header.
992 iph->ihl = sizeof(struct iphdr)>>2;
994 iph->protocol = next_protocol;
996 iph->daddr = cp->daddr.ip;
999 ip_select_ident(net, skb, NULL);
1001 /* Another hack: avoid icmp_send in ip_fragment */
1004 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1005 if (ret == NF_ACCEPT)
1007 else if (ret == NF_DROP)
1023 #ifdef CONFIG_IP_VS_IPV6
1025 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1026 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1028 struct rt6_info *rt; /* Route to the other host */
1029 struct in6_addr saddr; /* Source for tunnel */
1030 struct net_device *tdev; /* Device to other host */
1031 __u8 next_protocol = 0;
1032 __u32 payload_len = 0;
1035 struct ipv6hdr *iph; /* Our new IP header */
1036 unsigned int max_headroom; /* The extra header space needed */
1042 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1044 IP_VS_RT_MODE_LOCAL |
1045 IP_VS_RT_MODE_NON_LOCAL |
1046 IP_VS_RT_MODE_TUNNEL);
1051 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1054 rt = (struct rt6_info *) skb_dst(skb);
1058 * Okay, now see if we can stuff it in the buffer as-is.
1060 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1062 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1063 &next_protocol, &payload_len,
1064 &dsfield, &ttl, NULL);
1068 skb = iptunnel_handle_offloads(
1069 skb, false, __tun_gso_type_mask(AF_INET6, cp->af));
1073 skb->transport_header = skb->network_header;
1075 skb_push(skb, sizeof(struct ipv6hdr));
1076 skb_reset_network_header(skb);
1077 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1080 * Push down and install the IPIP header.
1082 iph = ipv6_hdr(skb);
1084 iph->nexthdr = next_protocol;
1085 iph->payload_len = htons(payload_len);
1086 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1087 ipv6_change_dsfield(iph, 0, dsfield);
1088 iph->daddr = cp->daddr.in6;
1090 iph->hop_limit = ttl;
1092 /* Another hack: avoid icmp_send in ip_fragment */
1095 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1096 if (ret == NF_ACCEPT)
1098 else if (ret == NF_DROP)
1117 * Direct Routing transmitter
1118 * Used for ANY protocol
1121 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1122 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1129 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
1130 IP_VS_RT_MODE_LOCAL |
1131 IP_VS_RT_MODE_NON_LOCAL |
1132 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1137 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1140 ip_send_check(ip_hdr(skb));
1142 /* Another hack: avoid icmp_send in ip_fragment */
1145 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1158 #ifdef CONFIG_IP_VS_IPV6
1160 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1161 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1168 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1170 IP_VS_RT_MODE_LOCAL |
1171 IP_VS_RT_MODE_NON_LOCAL |
1172 IP_VS_RT_MODE_KNOWN_NH);
1177 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1180 /* Another hack: avoid icmp_send in ip_fragment */
1183 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1199 * ICMP packet transmitter
1200 * called by the ip_vs_in_icmp
1203 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1204 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1205 struct ip_vs_iphdr *iph)
1207 struct rtable *rt; /* Route to the other host */
1210 int rt_mode, was_input;
1214 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1215 forwarded directly here, because there is no need to
1216 translate address/port back */
1217 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1218 if (cp->packet_xmit)
1219 rc = cp->packet_xmit(skb, cp, pp, iph);
1222 /* do not touch skb anymore */
1223 atomic_inc(&cp->in_pkts);
1228 * mangle and send the packet here (only for VS/NAT)
1230 was_input = rt_is_input_route(skb_rtable(skb));
1232 /* LOCALNODE from FORWARD hook is not supported */
1233 rt_mode = (hooknum != NF_INET_FORWARD) ?
1234 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1235 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1237 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1241 rt = skb_rtable(skb);
1244 * Avoid duplicate tuple in reply direction for NAT traffic
1245 * to local address when connection is sync-ed
1247 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1248 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1249 enum ip_conntrack_info ctinfo;
1250 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1252 if (ct && !nf_ct_is_untracked(ct)) {
1253 IP_VS_DBG(10, "%s(): "
1254 "stopping DNAT to local address %pI4\n",
1255 __func__, &cp->daddr.ip);
1261 /* From world but DNAT to loopback address? */
1262 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1263 IP_VS_DBG(1, "%s(): "
1264 "stopping DNAT to loopback %pI4\n",
1265 __func__, &cp->daddr.ip);
1269 /* copy-on-write the packet before mangling it */
1270 if (!skb_make_writable(skb, offset))
1273 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1276 ip_vs_nat_icmp(skb, pp, cp, 0);
1278 /* Another hack: avoid icmp_send in ip_fragment */
1281 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1294 #ifdef CONFIG_IP_VS_IPV6
1296 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1297 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1298 struct ip_vs_iphdr *ipvsh)
1300 struct rt6_info *rt; /* Route to the other host */
1307 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1308 forwarded directly here, because there is no need to
1309 translate address/port back */
1310 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1311 if (cp->packet_xmit)
1312 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1315 /* do not touch skb anymore */
1316 atomic_inc(&cp->in_pkts);
1321 * mangle and send the packet here (only for VS/NAT)
1324 /* LOCALNODE from FORWARD hook is not supported */
1325 rt_mode = (hooknum != NF_INET_FORWARD) ?
1326 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1327 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1329 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1330 NULL, ipvsh, 0, rt_mode);
1333 rt = (struct rt6_info *) skb_dst(skb);
1335 * Avoid duplicate tuple in reply direction for NAT traffic
1336 * to local address when connection is sync-ed
1338 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1339 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1340 enum ip_conntrack_info ctinfo;
1341 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1343 if (ct && !nf_ct_is_untracked(ct)) {
1344 IP_VS_DBG(10, "%s(): "
1345 "stopping DNAT to local address %pI6\n",
1346 __func__, &cp->daddr.in6);
1352 /* From world but DNAT to loopback address? */
1353 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1354 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1355 IP_VS_DBG(1, "%s(): "
1356 "stopping DNAT to loopback %pI6\n",
1357 __func__, &cp->daddr.in6);
1361 /* copy-on-write the packet before mangling it */
1362 if (!skb_make_writable(skb, offset))
1365 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1368 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1370 /* Another hack: avoid icmp_send in ip_fragment */
1373 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);