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.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
134 FLOWI_FLAG_KNOWN_NH : 0;
137 rt = ip_route_output_key(net, &fl4);
139 /* Invalid saddr ? */
140 if (PTR_ERR(rt) == -EINVAL && *saddr &&
141 rt_mode & IP_VS_RT_MODE_CONNECT && !loop) {
143 flowi4_update_output(&fl4, 0, 0, daddr, 0);
146 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", &daddr);
148 } else if (!*saddr && rt_mode & IP_VS_RT_MODE_CONNECT && fl4.saddr) {
151 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
159 #ifdef CONFIG_IP_VS_IPV6
160 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
162 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
166 static inline bool crosses_local_route_boundary(int skb_af, struct sk_buff *skb,
168 bool new_rt_is_local)
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;
176 #ifdef CONFIG_IP_VS_IPV6
177 if (skb_af == AF_INET6) {
178 int addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
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));
188 source_is_loopback = ipv4_is_loopback(ip_hdr(skb)->saddr);
189 old_rt_is_local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
192 if (unlikely(new_rt_is_local)) {
193 if (!rt_mode_allow_local)
195 if (!rt_mode_allow_redirect && !old_rt_is_local)
198 if (!rt_mode_allow_non_local)
200 if (source_is_loopback)
206 static inline void maybe_update_pmtu(int skb_af, struct sk_buff *skb, int mtu)
208 struct sock *sk = skb->sk;
209 struct rtable *ort = skb_rtable(skb);
211 if (!skb->dev && sk && sk_fullsock(sk))
212 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu, true);
215 static inline bool ensure_mtu_is_adequate(struct netns_ipvs *ipvs, int skb_af,
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 = ipvs->net;
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 && !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);
237 /* If we're going to tunnel the packet and pmtu discovery
238 * is disabled, we'll just fragment it anyway
240 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
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,
248 IP_VS_DBG(1, "frag needed for %pI4\n",
249 &ip_hdr(skb)->saddr);
257 static inline bool decrement_ttl(struct netns_ipvs *ipvs,
261 struct net *net = ipvs->net;
263 #ifdef CONFIG_IP_VS_IPV6
264 if (skb_af == AF_INET6) {
265 struct dst_entry *dst = skb_dst(skb);
267 /* check and decrement ttl */
268 if (ipv6_hdr(skb)->hop_limit <= 1) {
269 /* Force OUTPUT device used as source address */
271 icmpv6_send(skb, ICMPV6_TIME_EXCEED,
272 ICMPV6_EXC_HOPLIMIT, 0);
273 __IP6_INC_STATS(net, ip6_dst_idev(dst),
274 IPSTATS_MIB_INHDRERRORS);
279 /* don't propagate ttl change to cloned packets */
280 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
283 ipv6_hdr(skb)->hop_limit--;
287 if (ip_hdr(skb)->ttl <= 1) {
288 /* Tell the sender its packet died... */
289 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
290 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
294 /* don't propagate ttl change to cloned packets */
295 if (!skb_make_writable(skb, sizeof(struct iphdr)))
299 ip_decrease_ttl(ip_hdr(skb));
305 /* Get route to destination or remote server */
307 __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
308 struct ip_vs_dest *dest,
309 __be32 daddr, int rt_mode, __be32 *ret_saddr,
310 struct ip_vs_iphdr *ipvsh)
312 struct net *net = ipvs->net;
313 struct ip_vs_dest_dst *dest_dst;
314 struct rtable *rt; /* Route to the other host */
316 int local, noref = 1;
319 dest_dst = __ip_vs_dst_check(dest);
320 if (likely(dest_dst))
321 rt = (struct rtable *) dest_dst->dst_cache;
323 dest_dst = ip_vs_dest_dst_alloc();
324 spin_lock_bh(&dest->dst_lock);
326 __ip_vs_dst_set(dest, NULL, NULL, 0);
327 spin_unlock_bh(&dest->dst_lock);
330 rt = do_output_route4(net, dest->addr.ip, rt_mode,
331 &dest_dst->dst_saddr.ip);
333 __ip_vs_dst_set(dest, NULL, NULL, 0);
334 spin_unlock_bh(&dest->dst_lock);
335 ip_vs_dest_dst_free(dest_dst);
338 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
339 spin_unlock_bh(&dest->dst_lock);
340 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
341 &dest->addr.ip, &dest_dst->dst_saddr.ip,
342 atomic_read(&rt->dst.__refcnt));
345 *ret_saddr = dest_dst->dst_saddr.ip;
347 __be32 saddr = htonl(INADDR_ANY);
351 /* For such unconfigured boxes avoid many route lookups
352 * for performance reasons because we do not remember saddr
354 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
355 rt = do_output_route4(net, daddr, rt_mode, &saddr);
362 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
363 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
365 IP_VS_DBG_RL("We are crossing local and non-local addresses"
366 " daddr=%pI4\n", &daddr);
370 if (unlikely(local)) {
371 /* skb to local stack, preserve old route */
377 if (!decrement_ttl(ipvs, skb_af, skb))
380 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
381 mtu = dst_mtu(&rt->dst);
383 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
385 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
388 maybe_update_pmtu(skb_af, skb, mtu);
391 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
397 skb_dst_set_noref(skb, &rt->dst);
399 skb_dst_set(skb, dst_clone(&rt->dst));
401 skb_dst_set(skb, &rt->dst);
411 dst_link_failure(skb);
415 #ifdef CONFIG_IP_VS_IPV6
416 static struct dst_entry *
417 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
418 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
420 struct dst_entry *dst;
421 struct flowi6 fl6 = {
425 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
426 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
428 dst = ip6_route_output(net, NULL, &fl6);
433 if (ipv6_addr_any(&fl6.saddr) &&
434 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
435 &fl6.daddr, 0, &fl6.saddr) < 0)
438 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
444 *ret_saddr = fl6.saddr;
449 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
454 * Get route to destination or remote server
457 __ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
458 struct ip_vs_dest *dest,
459 struct in6_addr *daddr, struct in6_addr *ret_saddr,
460 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
462 struct net *net = ipvs->net;
463 struct ip_vs_dest_dst *dest_dst;
464 struct rt6_info *rt; /* Route to the other host */
465 struct dst_entry *dst;
467 int local, noref = 1;
470 dest_dst = __ip_vs_dst_check(dest);
471 if (likely(dest_dst))
472 rt = (struct rt6_info *) dest_dst->dst_cache;
476 dest_dst = ip_vs_dest_dst_alloc();
477 spin_lock_bh(&dest->dst_lock);
479 __ip_vs_dst_set(dest, NULL, NULL, 0);
480 spin_unlock_bh(&dest->dst_lock);
483 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
484 &dest_dst->dst_saddr.in6,
487 __ip_vs_dst_set(dest, NULL, NULL, 0);
488 spin_unlock_bh(&dest->dst_lock);
489 ip_vs_dest_dst_free(dest_dst);
492 rt = (struct rt6_info *) dst;
493 cookie = rt6_get_cookie(rt);
494 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
495 spin_unlock_bh(&dest->dst_lock);
496 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
497 &dest->addr.in6, &dest_dst->dst_saddr.in6,
498 atomic_read(&rt->dst.__refcnt));
501 *ret_saddr = dest_dst->dst_saddr.in6;
504 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
508 rt = (struct rt6_info *) dst;
511 local = __ip_vs_is_local_route6(rt);
513 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
515 IP_VS_DBG_RL("We are crossing local and non-local addresses"
516 " daddr=%pI6\n", daddr);
520 if (unlikely(local)) {
521 /* skb to local stack, preserve old route */
523 dst_release(&rt->dst);
527 if (!decrement_ttl(ipvs, skb_af, skb))
531 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
532 mtu = dst_mtu(&rt->dst);
534 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
535 if (mtu < IPV6_MIN_MTU) {
536 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
540 maybe_update_pmtu(skb_af, skb, mtu);
543 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
549 skb_dst_set_noref(skb, &rt->dst);
551 skb_dst_set(skb, dst_clone(&rt->dst));
553 skb_dst_set(skb, &rt->dst);
559 dst_release(&rt->dst);
563 /* The ip6_link_failure function requires the dev field to be set
564 * in order to get the net (further for the sake of fwmark
568 skb->dev = skb_dst(skb)->dev;
570 dst_link_failure(skb);
576 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
577 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
578 struct ip_vs_conn *cp)
582 skb->ipvs_property = 1;
583 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
584 ret = ip_vs_confirm_conntrack(skb);
585 if (ret == NF_ACCEPT) {
587 skb_forward_csum(skb);
592 /* In the event of a remote destination, it's possible that we would have
593 * matches against an old socket (particularly a TIME-WAIT socket). This
594 * causes havoc down the line (ip_local_out et. al. expect regular sockets
595 * and invalid memory accesses will happen) so simply drop the association
598 static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
600 /* If dev is set, the packet came from the LOCAL_IN callback and
601 * not from a local TCP socket.
607 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
608 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
609 struct ip_vs_conn *cp, int local)
613 skb->ipvs_property = 1;
614 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
617 ip_vs_update_conntrack(skb, cp, 1);
619 /* Remove the early_demux association unless it's bound for the
620 * exact same port and address on this host after translation.
622 if (!local || cp->vport != cp->dport ||
623 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
624 ip_vs_drop_early_demux_sk(skb);
627 skb_forward_csum(skb);
628 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
629 NULL, skb_dst(skb)->dev, dst_output);
636 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
637 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
638 struct ip_vs_conn *cp, int local)
642 skb->ipvs_property = 1;
643 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
646 ip_vs_drop_early_demux_sk(skb);
647 skb_forward_csum(skb);
648 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
649 NULL, skb_dst(skb)->dev, dst_output);
657 * NULL transmitter (do nothing except return NF_ACCEPT)
660 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
661 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
663 /* we do not touch skb and do not need pskb ptr */
664 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
670 * Let packets bypass the destination when the destination is not
671 * available, it may be only used in transparent cache cluster.
674 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
675 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
677 struct iphdr *iph = ip_hdr(skb);
681 if (__ip_vs_get_out_rt(cp->ipvs, cp->af, skb, NULL, iph->daddr,
682 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
687 /* Another hack: avoid icmp_send in ip_fragment */
690 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
701 #ifdef CONFIG_IP_VS_IPV6
703 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
704 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
706 struct ipv6hdr *iph = ipv6_hdr(skb);
710 if (__ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, NULL,
712 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
715 /* Another hack: avoid icmp_send in ip_fragment */
718 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
731 * NAT transmitter (only for outside-to-inside nat forwarding)
732 * Not used for related ICMP
735 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
736 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
738 struct rtable *rt; /* Route to the other host */
739 int local, rc, was_input;
743 /* check if it is a connection of no-client-port */
744 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
747 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
750 ip_vs_conn_fill_cport(cp, *p);
751 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
754 was_input = rt_is_input_route(skb_rtable(skb));
755 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
756 IP_VS_RT_MODE_LOCAL |
757 IP_VS_RT_MODE_NON_LOCAL |
758 IP_VS_RT_MODE_RDR, NULL, ipvsh);
761 rt = skb_rtable(skb);
763 * Avoid duplicate tuple in reply direction for NAT traffic
764 * to local address when connection is sync-ed
766 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
767 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
768 enum ip_conntrack_info ctinfo;
769 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
772 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
774 "stopping DNAT to local address");
780 /* From world but DNAT to loopback address? */
781 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
782 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
783 "ip_vs_nat_xmit(): stopping DNAT to loopback "
788 /* copy-on-write the packet before mangling it */
789 if (!skb_make_writable(skb, sizeof(struct iphdr)))
792 if (skb_cow(skb, rt->dst.dev->hard_header_len))
795 /* mangle the packet */
796 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
798 ip_hdr(skb)->daddr = cp->daddr.ip;
799 ip_send_check(ip_hdr(skb));
801 IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
803 /* FIXME: when application helper enlarges the packet and the length
804 is larger than the MTU of outgoing device, there will be still
807 /* Another hack: avoid icmp_send in ip_fragment */
810 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
821 #ifdef CONFIG_IP_VS_IPV6
823 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
824 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
826 struct rt6_info *rt; /* Route to the other host */
831 /* check if it is a connection of no-client-port */
832 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
834 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
837 ip_vs_conn_fill_cport(cp, *p);
838 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
841 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
844 IP_VS_RT_MODE_LOCAL |
845 IP_VS_RT_MODE_NON_LOCAL |
849 rt = (struct rt6_info *) skb_dst(skb);
851 * Avoid duplicate tuple in reply direction for NAT traffic
852 * to local address when connection is sync-ed
854 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
855 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
856 enum ip_conntrack_info ctinfo;
857 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
860 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
861 "ip_vs_nat_xmit_v6(): "
862 "stopping DNAT to local address");
868 /* From world but DNAT to loopback address? */
869 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
870 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
871 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
872 "ip_vs_nat_xmit_v6(): "
873 "stopping DNAT to loopback address");
877 /* copy-on-write the packet before mangling it */
878 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
881 if (skb_cow(skb, rt->dst.dev->hard_header_len))
884 /* mangle the packet */
885 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
887 ipv6_hdr(skb)->daddr = cp->daddr.in6;
889 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
891 /* FIXME: when application helper enlarges the packet and the length
892 is larger than the MTU of outgoing device, there will be still
895 /* Another hack: avoid icmp_send in ip_fragment */
898 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
910 /* When forwarding a packet, we must ensure that we've got enough headroom
911 * for the encapsulation packet in the skb. This also gives us an
912 * opportunity to figure out what the payload_len, dsfield, ttl, and df
913 * values should be, so that we won't need to look at the old ip header
916 static struct sk_buff *
917 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
918 unsigned int max_headroom, __u8 *next_protocol,
919 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
922 struct sk_buff *new_skb = NULL;
923 struct iphdr *old_iph = NULL;
925 #ifdef CONFIG_IP_VS_IPV6
926 struct ipv6hdr *old_ipv6h = NULL;
929 ip_vs_drop_early_demux_sk(skb);
931 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
932 new_skb = skb_realloc_headroom(skb, max_headroom);
936 skb_set_owner_w(new_skb, skb->sk);
941 #ifdef CONFIG_IP_VS_IPV6
942 if (skb_af == AF_INET6) {
943 old_ipv6h = ipv6_hdr(skb);
944 *next_protocol = IPPROTO_IPV6;
947 ntohs(old_ipv6h->payload_len) +
949 old_dsfield = ipv6_get_dsfield(old_ipv6h);
950 *ttl = old_ipv6h->hop_limit;
956 old_iph = ip_hdr(skb);
957 /* Copy DF, reset fragment offset and MF */
959 *df = (old_iph->frag_off & htons(IP_DF));
960 *next_protocol = IPPROTO_IPIP;
962 /* fix old IP header checksum */
963 ip_send_check(old_iph);
964 old_dsfield = ipv4_get_dsfield(old_iph);
967 *payload_len = ntohs(old_iph->tot_len);
970 /* Implement full-functionality option for ECN encapsulation */
971 *dsfield = INET_ECN_encapsulate(old_dsfield, old_dsfield);
976 return ERR_PTR(-ENOMEM);
979 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
983 return SKB_GSO_IPXIP4;
985 return SKB_GSO_IPXIP6;
992 * IP Tunneling transmitter
994 * This function encapsulates the packet in a new IP packet, its
995 * destination will be set to cp->daddr. Most code of this function
996 * is taken from ipip.c.
998 * It is used in VS/TUN cluster. The load balancer selects a real
999 * server from a cluster based on a scheduling algorithm,
1000 * encapsulates the request packet and forwards it to the selected
1001 * server. For example, all real servers are configured with
1002 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
1003 * the encapsulated packet, it will decapsulate the packet, processe
1004 * the request and return the response packets directly to the client
1005 * without passing the load balancer. This can greatly increase the
1006 * scalability of virtual server.
1008 * Used for ANY protocol
1011 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1012 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1014 struct netns_ipvs *ipvs = cp->ipvs;
1015 struct net *net = ipvs->net;
1016 struct rtable *rt; /* Route to the other host */
1017 __be32 saddr; /* Source for tunnel */
1018 struct net_device *tdev; /* Device to other host */
1019 __u8 next_protocol = 0;
1024 struct iphdr *iph; /* Our new IP header */
1025 unsigned int max_headroom; /* The extra header space needed */
1030 local = __ip_vs_get_out_rt(ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1031 IP_VS_RT_MODE_LOCAL |
1032 IP_VS_RT_MODE_NON_LOCAL |
1033 IP_VS_RT_MODE_CONNECT |
1034 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
1038 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1040 rt = skb_rtable(skb);
1044 * Okay, now see if we can stuff it in the buffer as-is.
1046 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1048 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1049 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1050 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1051 &next_protocol, NULL, &dsfield,
1056 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET, cp->af)))
1059 skb->transport_header = skb->network_header;
1061 skb_push(skb, sizeof(struct iphdr));
1062 skb_reset_network_header(skb);
1063 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1066 * Push down and install the IPIP header.
1070 iph->ihl = sizeof(struct iphdr)>>2;
1072 iph->protocol = next_protocol;
1074 iph->daddr = cp->daddr.ip;
1077 ip_select_ident(net, skb, NULL);
1079 /* Another hack: avoid icmp_send in ip_fragment */
1082 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1083 if (ret == NF_ACCEPT)
1084 ip_local_out(net, skb->sk, skb);
1085 else if (ret == NF_DROP)
1099 #ifdef CONFIG_IP_VS_IPV6
1101 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1102 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1104 struct rt6_info *rt; /* Route to the other host */
1105 struct in6_addr saddr; /* Source for tunnel */
1106 struct net_device *tdev; /* Device to other host */
1107 __u8 next_protocol = 0;
1108 __u32 payload_len = 0;
1111 struct ipv6hdr *iph; /* Our new IP header */
1112 unsigned int max_headroom; /* The extra header space needed */
1117 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1120 IP_VS_RT_MODE_LOCAL |
1121 IP_VS_RT_MODE_NON_LOCAL |
1122 IP_VS_RT_MODE_TUNNEL);
1126 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1128 rt = (struct rt6_info *) skb_dst(skb);
1132 * Okay, now see if we can stuff it in the buffer as-is.
1134 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1136 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1137 &next_protocol, &payload_len,
1138 &dsfield, &ttl, NULL);
1142 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET6, cp->af)))
1145 skb->transport_header = skb->network_header;
1147 skb_push(skb, sizeof(struct ipv6hdr));
1148 skb_reset_network_header(skb);
1149 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1152 * Push down and install the IPIP header.
1154 iph = ipv6_hdr(skb);
1156 iph->nexthdr = next_protocol;
1157 iph->payload_len = htons(payload_len);
1158 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1159 ipv6_change_dsfield(iph, 0, dsfield);
1160 iph->daddr = cp->daddr.in6;
1162 iph->hop_limit = ttl;
1164 /* Another hack: avoid icmp_send in ip_fragment */
1167 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1168 if (ret == NF_ACCEPT)
1169 ip6_local_out(cp->ipvs->net, skb->sk, skb);
1170 else if (ret == NF_DROP)
1187 * Direct Routing transmitter
1188 * Used for ANY protocol
1191 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1192 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1198 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1199 IP_VS_RT_MODE_LOCAL |
1200 IP_VS_RT_MODE_NON_LOCAL |
1201 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1205 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1207 ip_send_check(ip_hdr(skb));
1209 /* Another hack: avoid icmp_send in ip_fragment */
1212 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1223 #ifdef CONFIG_IP_VS_IPV6
1225 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1226 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1232 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1235 IP_VS_RT_MODE_LOCAL |
1236 IP_VS_RT_MODE_NON_LOCAL |
1237 IP_VS_RT_MODE_KNOWN_NH);
1241 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1243 /* Another hack: avoid icmp_send in ip_fragment */
1246 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1260 * ICMP packet transmitter
1261 * called by the ip_vs_in_icmp
1264 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1265 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1266 struct ip_vs_iphdr *iph)
1268 struct rtable *rt; /* Route to the other host */
1271 int rt_mode, was_input;
1275 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1276 forwarded directly here, because there is no need to
1277 translate address/port back */
1278 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1279 if (cp->packet_xmit)
1280 rc = cp->packet_xmit(skb, cp, pp, iph);
1283 /* do not touch skb anymore */
1284 atomic_inc(&cp->in_pkts);
1289 * mangle and send the packet here (only for VS/NAT)
1291 was_input = rt_is_input_route(skb_rtable(skb));
1293 /* LOCALNODE from FORWARD hook is not supported */
1294 rt_mode = (hooknum != NF_INET_FORWARD) ?
1295 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1296 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1297 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1301 rt = skb_rtable(skb);
1304 * Avoid duplicate tuple in reply direction for NAT traffic
1305 * to local address when connection is sync-ed
1307 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1308 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1309 enum ip_conntrack_info ctinfo;
1310 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1313 IP_VS_DBG(10, "%s(): "
1314 "stopping DNAT to local address %pI4\n",
1315 __func__, &cp->daddr.ip);
1321 /* From world but DNAT to loopback address? */
1322 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1323 IP_VS_DBG(1, "%s(): "
1324 "stopping DNAT to loopback %pI4\n",
1325 __func__, &cp->daddr.ip);
1329 /* copy-on-write the packet before mangling it */
1330 if (!skb_make_writable(skb, offset))
1333 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1336 ip_vs_nat_icmp(skb, pp, cp, 0);
1338 /* Another hack: avoid icmp_send in ip_fragment */
1341 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1352 #ifdef CONFIG_IP_VS_IPV6
1354 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1355 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1356 struct ip_vs_iphdr *ipvsh)
1358 struct rt6_info *rt; /* Route to the other host */
1365 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1366 forwarded directly here, because there is no need to
1367 translate address/port back */
1368 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1369 if (cp->packet_xmit)
1370 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1373 /* do not touch skb anymore */
1374 atomic_inc(&cp->in_pkts);
1379 * mangle and send the packet here (only for VS/NAT)
1382 /* LOCALNODE from FORWARD hook is not supported */
1383 rt_mode = (hooknum != NF_INET_FORWARD) ?
1384 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1385 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1386 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1387 &cp->daddr.in6, NULL, ipvsh, 0, rt_mode);
1390 rt = (struct rt6_info *) skb_dst(skb);
1392 * Avoid duplicate tuple in reply direction for NAT traffic
1393 * to local address when connection is sync-ed
1395 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1396 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1397 enum ip_conntrack_info ctinfo;
1398 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1401 IP_VS_DBG(10, "%s(): "
1402 "stopping DNAT to local address %pI6\n",
1403 __func__, &cp->daddr.in6);
1409 /* From world but DNAT to loopback address? */
1410 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1411 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1412 IP_VS_DBG(1, "%s(): "
1413 "stopping DNAT to loopback %pI6\n",
1414 __func__, &cp->daddr.in6);
1418 /* copy-on-write the packet before mangling it */
1419 if (!skb_make_writable(skb, offset))
1422 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1425 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1427 /* Another hack: avoid icmp_send in ip_fragment */
1430 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);