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_NON_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 struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
271 /* Force OUTPUT device used as source address */
273 icmpv6_send(skb, ICMPV6_TIME_EXCEED,
274 ICMPV6_EXC_HOPLIMIT, 0);
275 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
280 /* don't propagate ttl change to cloned packets */
281 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
284 ipv6_hdr(skb)->hop_limit--;
288 if (ip_hdr(skb)->ttl <= 1) {
289 /* Tell the sender its packet died... */
290 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
291 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
295 /* don't propagate ttl change to cloned packets */
296 if (!skb_make_writable(skb, sizeof(struct iphdr)))
300 ip_decrease_ttl(ip_hdr(skb));
306 /* Get route to destination or remote server */
308 __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
309 struct ip_vs_dest *dest,
310 __be32 daddr, int rt_mode, __be32 *ret_saddr,
311 struct ip_vs_iphdr *ipvsh)
313 struct net *net = ipvs->net;
314 struct ip_vs_dest_dst *dest_dst;
315 struct rtable *rt; /* Route to the other host */
317 int local, noref = 1;
320 dest_dst = __ip_vs_dst_check(dest);
321 if (likely(dest_dst))
322 rt = (struct rtable *) dest_dst->dst_cache;
324 dest_dst = ip_vs_dest_dst_alloc();
325 spin_lock_bh(&dest->dst_lock);
327 __ip_vs_dst_set(dest, NULL, NULL, 0);
328 spin_unlock_bh(&dest->dst_lock);
331 rt = do_output_route4(net, dest->addr.ip, rt_mode,
332 &dest_dst->dst_saddr.ip);
334 __ip_vs_dst_set(dest, NULL, NULL, 0);
335 spin_unlock_bh(&dest->dst_lock);
336 ip_vs_dest_dst_free(dest_dst);
339 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
340 spin_unlock_bh(&dest->dst_lock);
341 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
342 &dest->addr.ip, &dest_dst->dst_saddr.ip,
343 atomic_read(&rt->dst.__refcnt));
346 *ret_saddr = dest_dst->dst_saddr.ip;
348 __be32 saddr = htonl(INADDR_ANY);
352 /* For such unconfigured boxes avoid many route lookups
353 * for performance reasons because we do not remember saddr
355 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
356 rt = do_output_route4(net, daddr, rt_mode, &saddr);
363 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
364 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
366 IP_VS_DBG_RL("We are crossing local and non-local addresses"
367 " daddr=%pI4\n", &daddr);
371 if (unlikely(local)) {
372 /* skb to local stack, preserve old route */
378 if (!decrement_ttl(ipvs, skb_af, skb))
381 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
382 mtu = dst_mtu(&rt->dst);
384 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
386 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
389 maybe_update_pmtu(skb_af, skb, mtu);
392 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
398 skb_dst_set_noref(skb, &rt->dst);
400 skb_dst_set(skb, dst_clone(&rt->dst));
402 skb_dst_set(skb, &rt->dst);
412 dst_link_failure(skb);
416 #ifdef CONFIG_IP_VS_IPV6
417 static struct dst_entry *
418 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
419 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
421 struct dst_entry *dst;
422 struct flowi6 fl6 = {
426 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
427 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
429 dst = ip6_route_output(net, NULL, &fl6);
434 if (ipv6_addr_any(&fl6.saddr) &&
435 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
436 &fl6.daddr, 0, &fl6.saddr) < 0)
439 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
445 *ret_saddr = fl6.saddr;
450 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
455 * Get route to destination or remote server
458 __ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
459 struct ip_vs_dest *dest,
460 struct in6_addr *daddr, struct in6_addr *ret_saddr,
461 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
463 struct net *net = ipvs->net;
464 struct ip_vs_dest_dst *dest_dst;
465 struct rt6_info *rt; /* Route to the other host */
466 struct dst_entry *dst;
468 int local, noref = 1;
471 dest_dst = __ip_vs_dst_check(dest);
472 if (likely(dest_dst))
473 rt = (struct rt6_info *) dest_dst->dst_cache;
477 dest_dst = ip_vs_dest_dst_alloc();
478 spin_lock_bh(&dest->dst_lock);
480 __ip_vs_dst_set(dest, NULL, NULL, 0);
481 spin_unlock_bh(&dest->dst_lock);
484 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
485 &dest_dst->dst_saddr.in6,
488 __ip_vs_dst_set(dest, NULL, NULL, 0);
489 spin_unlock_bh(&dest->dst_lock);
490 ip_vs_dest_dst_free(dest_dst);
493 rt = (struct rt6_info *) dst;
494 cookie = rt6_get_cookie(rt);
495 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
496 spin_unlock_bh(&dest->dst_lock);
497 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
498 &dest->addr.in6, &dest_dst->dst_saddr.in6,
499 atomic_read(&rt->dst.__refcnt));
502 *ret_saddr = dest_dst->dst_saddr.in6;
505 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
509 rt = (struct rt6_info *) dst;
512 local = __ip_vs_is_local_route6(rt);
514 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
516 IP_VS_DBG_RL("We are crossing local and non-local addresses"
517 " daddr=%pI6\n", daddr);
521 if (unlikely(local)) {
522 /* skb to local stack, preserve old route */
524 dst_release(&rt->dst);
528 if (!decrement_ttl(ipvs, skb_af, skb))
532 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
533 mtu = dst_mtu(&rt->dst);
535 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
536 if (mtu < IPV6_MIN_MTU) {
537 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
541 maybe_update_pmtu(skb_af, skb, mtu);
544 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
550 skb_dst_set_noref(skb, &rt->dst);
552 skb_dst_set(skb, dst_clone(&rt->dst));
554 skb_dst_set(skb, &rt->dst);
560 dst_release(&rt->dst);
564 /* The ip6_link_failure function requires the dev field to be set
565 * in order to get the net (further for the sake of fwmark
569 skb->dev = skb_dst(skb)->dev;
571 dst_link_failure(skb);
577 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
578 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
579 struct ip_vs_conn *cp)
583 skb->ipvs_property = 1;
584 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
585 ret = ip_vs_confirm_conntrack(skb);
586 if (ret == NF_ACCEPT) {
588 skb_forward_csum(skb);
595 /* In the event of a remote destination, it's possible that we would have
596 * matches against an old socket (particularly a TIME-WAIT socket). This
597 * causes havoc down the line (ip_local_out et. al. expect regular sockets
598 * and invalid memory accesses will happen) so simply drop the association
601 static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
603 /* If dev is set, the packet came from the LOCAL_IN callback and
604 * not from a local TCP socket.
610 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
611 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
612 struct ip_vs_conn *cp, int local)
616 skb->ipvs_property = 1;
617 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
620 ip_vs_update_conntrack(skb, cp, 1);
622 /* Remove the early_demux association unless it's bound for the
623 * exact same port and address on this host after translation.
625 if (!local || cp->vport != cp->dport ||
626 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
627 ip_vs_drop_early_demux_sk(skb);
630 skb_forward_csum(skb);
633 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
634 NULL, skb_dst(skb)->dev, dst_output);
641 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
642 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
643 struct ip_vs_conn *cp, int local)
647 skb->ipvs_property = 1;
648 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
651 ip_vs_drop_early_demux_sk(skb);
652 skb_forward_csum(skb);
655 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
656 NULL, skb_dst(skb)->dev, dst_output);
664 * NULL transmitter (do nothing except return NF_ACCEPT)
667 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
668 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
670 /* we do not touch skb and do not need pskb ptr */
671 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
677 * Let packets bypass the destination when the destination is not
678 * available, it may be only used in transparent cache cluster.
681 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
682 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
684 struct iphdr *iph = ip_hdr(skb);
688 if (__ip_vs_get_out_rt(cp->ipvs, cp->af, skb, NULL, iph->daddr,
689 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
694 /* Another hack: avoid icmp_send in ip_fragment */
697 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
708 #ifdef CONFIG_IP_VS_IPV6
710 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
711 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
713 struct ipv6hdr *iph = ipv6_hdr(skb);
717 if (__ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, NULL,
719 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
722 /* Another hack: avoid icmp_send in ip_fragment */
725 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
738 * NAT transmitter (only for outside-to-inside nat forwarding)
739 * Not used for related ICMP
742 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
743 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
745 struct rtable *rt; /* Route to the other host */
746 int local, rc, was_input;
750 /* check if it is a connection of no-client-port */
751 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
754 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
757 ip_vs_conn_fill_cport(cp, *p);
758 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
761 was_input = rt_is_input_route(skb_rtable(skb));
762 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
763 IP_VS_RT_MODE_LOCAL |
764 IP_VS_RT_MODE_NON_LOCAL |
765 IP_VS_RT_MODE_RDR, NULL, ipvsh);
768 rt = skb_rtable(skb);
770 * Avoid duplicate tuple in reply direction for NAT traffic
771 * to local address when connection is sync-ed
773 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
774 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
775 enum ip_conntrack_info ctinfo;
776 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
779 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
781 "stopping DNAT to local address");
787 /* From world but DNAT to loopback address? */
788 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
789 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
790 "ip_vs_nat_xmit(): stopping DNAT to loopback "
795 /* copy-on-write the packet before mangling it */
796 if (!skb_make_writable(skb, sizeof(struct iphdr)))
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 ip_hdr(skb)->daddr = cp->daddr.ip;
806 ip_send_check(ip_hdr(skb));
808 IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
810 /* FIXME: when application helper enlarges the packet and the length
811 is larger than the MTU of outgoing device, there will be still
814 /* Another hack: avoid icmp_send in ip_fragment */
817 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
828 #ifdef CONFIG_IP_VS_IPV6
830 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
831 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
833 struct rt6_info *rt; /* Route to the other host */
838 /* check if it is a connection of no-client-port */
839 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
841 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
844 ip_vs_conn_fill_cport(cp, *p);
845 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
848 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
851 IP_VS_RT_MODE_LOCAL |
852 IP_VS_RT_MODE_NON_LOCAL |
856 rt = (struct rt6_info *) skb_dst(skb);
858 * Avoid duplicate tuple in reply direction for NAT traffic
859 * to local address when connection is sync-ed
861 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
862 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
863 enum ip_conntrack_info ctinfo;
864 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
867 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
868 "ip_vs_nat_xmit_v6(): "
869 "stopping DNAT to local address");
875 /* From world but DNAT to loopback address? */
876 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
877 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
878 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
879 "ip_vs_nat_xmit_v6(): "
880 "stopping DNAT to loopback address");
884 /* copy-on-write the packet before mangling it */
885 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
888 if (skb_cow(skb, rt->dst.dev->hard_header_len))
891 /* mangle the packet */
892 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
894 ipv6_hdr(skb)->daddr = cp->daddr.in6;
896 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
898 /* FIXME: when application helper enlarges the packet and the length
899 is larger than the MTU of outgoing device, there will be still
902 /* Another hack: avoid icmp_send in ip_fragment */
905 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
917 /* When forwarding a packet, we must ensure that we've got enough headroom
918 * for the encapsulation packet in the skb. This also gives us an
919 * opportunity to figure out what the payload_len, dsfield, ttl, and df
920 * values should be, so that we won't need to look at the old ip header
923 static struct sk_buff *
924 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
925 unsigned int max_headroom, __u8 *next_protocol,
926 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
929 struct sk_buff *new_skb = NULL;
930 struct iphdr *old_iph = NULL;
932 #ifdef CONFIG_IP_VS_IPV6
933 struct ipv6hdr *old_ipv6h = NULL;
936 ip_vs_drop_early_demux_sk(skb);
938 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
939 new_skb = skb_realloc_headroom(skb, max_headroom);
943 skb_set_owner_w(new_skb, skb->sk);
948 #ifdef CONFIG_IP_VS_IPV6
949 if (skb_af == AF_INET6) {
950 old_ipv6h = ipv6_hdr(skb);
951 *next_protocol = IPPROTO_IPV6;
954 ntohs(old_ipv6h->payload_len) +
956 old_dsfield = ipv6_get_dsfield(old_ipv6h);
957 *ttl = old_ipv6h->hop_limit;
963 old_iph = ip_hdr(skb);
964 /* Copy DF, reset fragment offset and MF */
966 *df = (old_iph->frag_off & htons(IP_DF));
967 *next_protocol = IPPROTO_IPIP;
969 /* fix old IP header checksum */
970 ip_send_check(old_iph);
971 old_dsfield = ipv4_get_dsfield(old_iph);
974 *payload_len = ntohs(old_iph->tot_len);
977 /* Implement full-functionality option for ECN encapsulation */
978 *dsfield = INET_ECN_encapsulate(old_dsfield, old_dsfield);
983 return ERR_PTR(-ENOMEM);
986 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
990 return SKB_GSO_IPXIP4;
992 return SKB_GSO_IPXIP6;
999 * IP Tunneling transmitter
1001 * This function encapsulates the packet in a new IP packet, its
1002 * destination will be set to cp->daddr. Most code of this function
1003 * is taken from ipip.c.
1005 * It is used in VS/TUN cluster. The load balancer selects a real
1006 * server from a cluster based on a scheduling algorithm,
1007 * encapsulates the request packet and forwards it to the selected
1008 * server. For example, all real servers are configured with
1009 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
1010 * the encapsulated packet, it will decapsulate the packet, processe
1011 * the request and return the response packets directly to the client
1012 * without passing the load balancer. This can greatly increase the
1013 * scalability of virtual server.
1015 * Used for ANY protocol
1018 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1019 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1021 struct netns_ipvs *ipvs = cp->ipvs;
1022 struct net *net = ipvs->net;
1023 struct rtable *rt; /* Route to the other host */
1024 __be32 saddr; /* Source for tunnel */
1025 struct net_device *tdev; /* Device to other host */
1026 __u8 next_protocol = 0;
1031 struct iphdr *iph; /* Our new IP header */
1032 unsigned int max_headroom; /* The extra header space needed */
1037 local = __ip_vs_get_out_rt(ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1038 IP_VS_RT_MODE_LOCAL |
1039 IP_VS_RT_MODE_NON_LOCAL |
1040 IP_VS_RT_MODE_CONNECT |
1041 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
1045 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1047 rt = skb_rtable(skb);
1051 * Okay, now see if we can stuff it in the buffer as-is.
1053 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1055 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1056 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1057 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1058 &next_protocol, NULL, &dsfield,
1063 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET, cp->af)))
1066 skb->transport_header = skb->network_header;
1068 skb_push(skb, sizeof(struct iphdr));
1069 skb_reset_network_header(skb);
1070 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1073 * Push down and install the IPIP header.
1077 iph->ihl = sizeof(struct iphdr)>>2;
1079 iph->protocol = next_protocol;
1081 iph->daddr = cp->daddr.ip;
1084 ip_select_ident(net, skb, NULL);
1086 /* Another hack: avoid icmp_send in ip_fragment */
1089 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1090 if (ret == NF_ACCEPT)
1091 ip_local_out(net, skb->sk, skb);
1092 else if (ret == NF_DROP)
1106 #ifdef CONFIG_IP_VS_IPV6
1108 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1109 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1111 struct rt6_info *rt; /* Route to the other host */
1112 struct in6_addr saddr; /* Source for tunnel */
1113 struct net_device *tdev; /* Device to other host */
1114 __u8 next_protocol = 0;
1115 __u32 payload_len = 0;
1118 struct ipv6hdr *iph; /* Our new IP header */
1119 unsigned int max_headroom; /* The extra header space needed */
1124 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1127 IP_VS_RT_MODE_LOCAL |
1128 IP_VS_RT_MODE_NON_LOCAL |
1129 IP_VS_RT_MODE_TUNNEL);
1133 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1135 rt = (struct rt6_info *) skb_dst(skb);
1139 * Okay, now see if we can stuff it in the buffer as-is.
1141 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1143 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1144 &next_protocol, &payload_len,
1145 &dsfield, &ttl, NULL);
1149 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET6, cp->af)))
1152 skb->transport_header = skb->network_header;
1154 skb_push(skb, sizeof(struct ipv6hdr));
1155 skb_reset_network_header(skb);
1156 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1159 * Push down and install the IPIP header.
1161 iph = ipv6_hdr(skb);
1163 iph->nexthdr = next_protocol;
1164 iph->payload_len = htons(payload_len);
1165 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1166 ipv6_change_dsfield(iph, 0, dsfield);
1167 iph->daddr = cp->daddr.in6;
1169 iph->hop_limit = ttl;
1171 /* Another hack: avoid icmp_send in ip_fragment */
1174 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1175 if (ret == NF_ACCEPT)
1176 ip6_local_out(cp->ipvs->net, skb->sk, skb);
1177 else if (ret == NF_DROP)
1194 * Direct Routing transmitter
1195 * Used for ANY protocol
1198 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1199 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1205 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1206 IP_VS_RT_MODE_LOCAL |
1207 IP_VS_RT_MODE_NON_LOCAL |
1208 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1212 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1214 ip_send_check(ip_hdr(skb));
1216 /* Another hack: avoid icmp_send in ip_fragment */
1219 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1230 #ifdef CONFIG_IP_VS_IPV6
1232 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1233 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1239 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1242 IP_VS_RT_MODE_LOCAL |
1243 IP_VS_RT_MODE_NON_LOCAL |
1244 IP_VS_RT_MODE_KNOWN_NH);
1248 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1250 /* Another hack: avoid icmp_send in ip_fragment */
1253 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1267 * ICMP packet transmitter
1268 * called by the ip_vs_in_icmp
1271 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1272 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1273 struct ip_vs_iphdr *iph)
1275 struct rtable *rt; /* Route to the other host */
1278 int rt_mode, was_input;
1282 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1283 forwarded directly here, because there is no need to
1284 translate address/port back */
1285 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1286 if (cp->packet_xmit)
1287 rc = cp->packet_xmit(skb, cp, pp, iph);
1290 /* do not touch skb anymore */
1291 atomic_inc(&cp->in_pkts);
1296 * mangle and send the packet here (only for VS/NAT)
1298 was_input = rt_is_input_route(skb_rtable(skb));
1300 /* LOCALNODE from FORWARD hook is not supported */
1301 rt_mode = (hooknum != NF_INET_FORWARD) ?
1302 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1303 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1304 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1308 rt = skb_rtable(skb);
1311 * Avoid duplicate tuple in reply direction for NAT traffic
1312 * to local address when connection is sync-ed
1314 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1315 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1316 enum ip_conntrack_info ctinfo;
1317 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1320 IP_VS_DBG(10, "%s(): "
1321 "stopping DNAT to local address %pI4\n",
1322 __func__, &cp->daddr.ip);
1328 /* From world but DNAT to loopback address? */
1329 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1330 IP_VS_DBG(1, "%s(): "
1331 "stopping DNAT to loopback %pI4\n",
1332 __func__, &cp->daddr.ip);
1336 /* copy-on-write the packet before mangling it */
1337 if (!skb_make_writable(skb, offset))
1340 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1343 ip_vs_nat_icmp(skb, pp, cp, 0);
1345 /* Another hack: avoid icmp_send in ip_fragment */
1348 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1359 #ifdef CONFIG_IP_VS_IPV6
1361 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1362 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1363 struct ip_vs_iphdr *ipvsh)
1365 struct rt6_info *rt; /* Route to the other host */
1372 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1373 forwarded directly here, because there is no need to
1374 translate address/port back */
1375 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1376 if (cp->packet_xmit)
1377 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1380 /* do not touch skb anymore */
1381 atomic_inc(&cp->in_pkts);
1386 * mangle and send the packet here (only for VS/NAT)
1389 /* LOCALNODE from FORWARD hook is not supported */
1390 rt_mode = (hooknum != NF_INET_FORWARD) ?
1391 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1392 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1393 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1394 &cp->daddr.in6, NULL, ipvsh, 0, rt_mode);
1397 rt = (struct rt6_info *) skb_dst(skb);
1399 * Avoid duplicate tuple in reply direction for NAT traffic
1400 * to local address when connection is sync-ed
1402 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1403 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1404 enum ip_conntrack_info ctinfo;
1405 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1408 IP_VS_DBG(10, "%s(): "
1409 "stopping DNAT to local address %pI6\n",
1410 __func__, &cp->daddr.in6);
1416 /* From world but DNAT to loopback address? */
1417 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1418 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1419 IP_VS_DBG(1, "%s(): "
1420 "stopping DNAT to loopback %pI6\n",
1421 __func__, &cp->daddr.in6);
1425 /* copy-on-write the packet before mangling it */
1426 if (!skb_make_writable(skb, offset))
1429 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1432 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1434 /* Another hack: avoid icmp_send in ip_fragment */
1437 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);