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);
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 /* Get route to destination or remote server */
259 __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
260 struct ip_vs_dest *dest,
261 __be32 daddr, int rt_mode, __be32 *ret_saddr,
262 struct ip_vs_iphdr *ipvsh)
264 struct net *net = ipvs->net;
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(ipvs, 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(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
407 struct ip_vs_dest *dest,
408 struct in6_addr *daddr, struct in6_addr *ret_saddr,
409 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
411 struct net *net = ipvs->net;
412 struct ip_vs_dest_dst *dest_dst;
413 struct rt6_info *rt; /* Route to the other host */
414 struct dst_entry *dst;
416 int local, noref = 1;
419 dest_dst = __ip_vs_dst_check(dest);
420 if (likely(dest_dst))
421 rt = (struct rt6_info *) dest_dst->dst_cache;
425 dest_dst = ip_vs_dest_dst_alloc();
426 spin_lock_bh(&dest->dst_lock);
428 __ip_vs_dst_set(dest, NULL, NULL, 0);
429 spin_unlock_bh(&dest->dst_lock);
432 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
433 &dest_dst->dst_saddr.in6,
436 __ip_vs_dst_set(dest, NULL, NULL, 0);
437 spin_unlock_bh(&dest->dst_lock);
438 ip_vs_dest_dst_free(dest_dst);
441 rt = (struct rt6_info *) dst;
442 cookie = rt6_get_cookie(rt);
443 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
444 spin_unlock_bh(&dest->dst_lock);
445 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
446 &dest->addr.in6, &dest_dst->dst_saddr.in6,
447 atomic_read(&rt->dst.__refcnt));
450 *ret_saddr = dest_dst->dst_saddr.in6;
453 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
457 rt = (struct rt6_info *) dst;
460 local = __ip_vs_is_local_route6(rt);
462 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
464 IP_VS_DBG_RL("We are crossing local and non-local addresses"
465 " daddr=%pI6\n", daddr);
469 if (unlikely(local)) {
470 /* skb to local stack, preserve old route */
472 dst_release(&rt->dst);
477 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
478 mtu = dst_mtu(&rt->dst);
480 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
481 if (mtu < IPV6_MIN_MTU) {
482 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
486 maybe_update_pmtu(skb_af, skb, mtu);
489 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
495 skb_dst_set_noref(skb, &rt->dst);
497 skb_dst_set(skb, dst_clone(&rt->dst));
499 skb_dst_set(skb, &rt->dst);
505 dst_release(&rt->dst);
509 /* The ip6_link_failure function requires the dev field to be set
510 * in order to get the net (further for the sake of fwmark
514 skb->dev = skb_dst(skb)->dev;
516 dst_link_failure(skb);
522 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
523 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
524 struct ip_vs_conn *cp)
528 skb->ipvs_property = 1;
529 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
530 ret = ip_vs_confirm_conntrack(skb);
531 if (ret == NF_ACCEPT) {
533 skb_forward_csum(skb);
538 /* In the event of a remote destination, it's possible that we would have
539 * matches against an old socket (particularly a TIME-WAIT socket). This
540 * causes havoc down the line (ip_local_out et. al. expect regular sockets
541 * and invalid memory accesses will happen) so simply drop the association
544 static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
546 /* If dev is set, the packet came from the LOCAL_IN callback and
547 * not from a local TCP socket.
553 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
554 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
555 struct ip_vs_conn *cp, int local)
559 skb->ipvs_property = 1;
560 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
563 ip_vs_update_conntrack(skb, cp, 1);
565 /* Remove the early_demux association unless it's bound for the
566 * exact same port and address on this host after translation.
568 if (!local || cp->vport != cp->dport ||
569 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
570 ip_vs_drop_early_demux_sk(skb);
573 skb_forward_csum(skb);
574 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
575 NULL, skb_dst(skb)->dev, dst_output);
582 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
583 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
584 struct ip_vs_conn *cp, int local)
588 skb->ipvs_property = 1;
589 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
592 ip_vs_drop_early_demux_sk(skb);
593 skb_forward_csum(skb);
594 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
595 NULL, skb_dst(skb)->dev, dst_output);
603 * NULL transmitter (do nothing except return NF_ACCEPT)
606 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
607 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
609 /* we do not touch skb and do not need pskb ptr */
610 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
616 * Let packets bypass the destination when the destination is not
617 * available, it may be only used in transparent cache cluster.
620 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
621 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
623 struct iphdr *iph = ip_hdr(skb);
628 if (__ip_vs_get_out_rt(cp->ipvs, cp->af, skb, NULL, iph->daddr,
629 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
634 /* Another hack: avoid icmp_send in ip_fragment */
637 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
650 #ifdef CONFIG_IP_VS_IPV6
652 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
653 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
655 struct ipv6hdr *iph = ipv6_hdr(skb);
660 if (__ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, NULL,
662 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
665 /* Another hack: avoid icmp_send in ip_fragment */
668 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
683 * NAT transmitter (only for outside-to-inside nat forwarding)
684 * Not used for related ICMP
687 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
688 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
690 struct rtable *rt; /* Route to the other host */
691 int local, rc, was_input;
696 /* check if it is a connection of no-client-port */
697 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
700 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
703 ip_vs_conn_fill_cport(cp, *p);
704 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
707 was_input = rt_is_input_route(skb_rtable(skb));
708 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
709 IP_VS_RT_MODE_LOCAL |
710 IP_VS_RT_MODE_NON_LOCAL |
711 IP_VS_RT_MODE_RDR, NULL, ipvsh);
714 rt = skb_rtable(skb);
716 * Avoid duplicate tuple in reply direction for NAT traffic
717 * to local address when connection is sync-ed
719 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
720 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
721 enum ip_conntrack_info ctinfo;
722 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
724 if (ct && !nf_ct_is_untracked(ct)) {
725 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
727 "stopping DNAT to local address");
733 /* From world but DNAT to loopback address? */
734 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
735 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
736 "ip_vs_nat_xmit(): stopping DNAT to loopback "
741 /* copy-on-write the packet before mangling it */
742 if (!skb_make_writable(skb, sizeof(struct iphdr)))
745 if (skb_cow(skb, rt->dst.dev->hard_header_len))
748 /* mangle the packet */
749 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
751 ip_hdr(skb)->daddr = cp->daddr.ip;
752 ip_send_check(ip_hdr(skb));
754 IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
756 /* FIXME: when application helper enlarges the packet and the length
757 is larger than the MTU of outgoing device, there will be still
760 /* Another hack: avoid icmp_send in ip_fragment */
763 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
776 #ifdef CONFIG_IP_VS_IPV6
778 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
779 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
781 struct rt6_info *rt; /* Route to the other host */
787 /* check if it is a connection of no-client-port */
788 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
790 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
793 ip_vs_conn_fill_cport(cp, *p);
794 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
797 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
800 IP_VS_RT_MODE_LOCAL |
801 IP_VS_RT_MODE_NON_LOCAL |
805 rt = (struct rt6_info *) skb_dst(skb);
807 * Avoid duplicate tuple in reply direction for NAT traffic
808 * to local address when connection is sync-ed
810 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
811 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
812 enum ip_conntrack_info ctinfo;
813 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
815 if (ct && !nf_ct_is_untracked(ct)) {
816 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
817 "ip_vs_nat_xmit_v6(): "
818 "stopping DNAT to local address");
824 /* From world but DNAT to loopback address? */
825 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
826 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
827 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
828 "ip_vs_nat_xmit_v6(): "
829 "stopping DNAT to loopback address");
833 /* copy-on-write the packet before mangling it */
834 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
837 if (skb_cow(skb, rt->dst.dev->hard_header_len))
840 /* mangle the packet */
841 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
843 ipv6_hdr(skb)->daddr = cp->daddr.in6;
845 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
847 /* FIXME: when application helper enlarges the packet and the length
848 is larger than the MTU of outgoing device, there will be still
851 /* Another hack: avoid icmp_send in ip_fragment */
854 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
868 /* When forwarding a packet, we must ensure that we've got enough headroom
869 * for the encapsulation packet in the skb. This also gives us an
870 * opportunity to figure out what the payload_len, dsfield, ttl, and df
871 * values should be, so that we won't need to look at the old ip header
874 static struct sk_buff *
875 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
876 unsigned int max_headroom, __u8 *next_protocol,
877 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
880 struct sk_buff *new_skb = NULL;
881 struct iphdr *old_iph = NULL;
882 #ifdef CONFIG_IP_VS_IPV6
883 struct ipv6hdr *old_ipv6h = NULL;
886 ip_vs_drop_early_demux_sk(skb);
888 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
889 new_skb = skb_realloc_headroom(skb, max_headroom);
893 skb_set_owner_w(new_skb, skb->sk);
898 #ifdef CONFIG_IP_VS_IPV6
899 if (skb_af == AF_INET6) {
900 old_ipv6h = ipv6_hdr(skb);
901 *next_protocol = IPPROTO_IPV6;
904 ntohs(old_ipv6h->payload_len) +
906 *dsfield = ipv6_get_dsfield(old_ipv6h);
907 *ttl = old_ipv6h->hop_limit;
913 old_iph = ip_hdr(skb);
914 /* Copy DF, reset fragment offset and MF */
916 *df = (old_iph->frag_off & htons(IP_DF));
917 *next_protocol = IPPROTO_IPIP;
919 /* fix old IP header checksum */
920 ip_send_check(old_iph);
921 *dsfield = ipv4_get_dsfield(old_iph);
924 *payload_len = ntohs(old_iph->tot_len);
930 return ERR_PTR(-ENOMEM);
933 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
937 return SKB_GSO_IPXIP4;
939 return SKB_GSO_IPXIP6;
946 * IP Tunneling transmitter
948 * This function encapsulates the packet in a new IP packet, its
949 * destination will be set to cp->daddr. Most code of this function
950 * is taken from ipip.c.
952 * It is used in VS/TUN cluster. The load balancer selects a real
953 * server from a cluster based on a scheduling algorithm,
954 * encapsulates the request packet and forwards it to the selected
955 * server. For example, all real servers are configured with
956 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
957 * the encapsulated packet, it will decapsulate the packet, processe
958 * the request and return the response packets directly to the client
959 * without passing the load balancer. This can greatly increase the
960 * scalability of virtual server.
962 * Used for ANY protocol
965 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
966 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
968 struct netns_ipvs *ipvs = cp->ipvs;
969 struct net *net = ipvs->net;
970 struct rtable *rt; /* Route to the other host */
971 __be32 saddr; /* Source for tunnel */
972 struct net_device *tdev; /* Device to other host */
973 __u8 next_protocol = 0;
978 struct iphdr *iph; /* Our new IP header */
979 unsigned int max_headroom; /* The extra header space needed */
985 local = __ip_vs_get_out_rt(ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
986 IP_VS_RT_MODE_LOCAL |
987 IP_VS_RT_MODE_NON_LOCAL |
988 IP_VS_RT_MODE_CONNECT |
989 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
994 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
997 rt = skb_rtable(skb);
1001 * Okay, now see if we can stuff it in the buffer as-is.
1003 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1005 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1006 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1007 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1008 &next_protocol, NULL, &dsfield,
1013 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET, cp->af)))
1016 skb->transport_header = skb->network_header;
1018 skb_push(skb, sizeof(struct iphdr));
1019 skb_reset_network_header(skb);
1020 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1023 * Push down and install the IPIP header.
1027 iph->ihl = sizeof(struct iphdr)>>2;
1029 iph->protocol = next_protocol;
1031 iph->daddr = cp->daddr.ip;
1034 ip_select_ident(net, skb, NULL);
1036 /* Another hack: avoid icmp_send in ip_fragment */
1039 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1040 if (ret == NF_ACCEPT)
1041 ip_local_out(net, skb->sk, skb);
1042 else if (ret == NF_DROP)
1058 #ifdef CONFIG_IP_VS_IPV6
1060 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1061 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1063 struct rt6_info *rt; /* Route to the other host */
1064 struct in6_addr saddr; /* Source for tunnel */
1065 struct net_device *tdev; /* Device to other host */
1066 __u8 next_protocol = 0;
1067 __u32 payload_len = 0;
1070 struct ipv6hdr *iph; /* Our new IP header */
1071 unsigned int max_headroom; /* The extra header space needed */
1077 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1080 IP_VS_RT_MODE_LOCAL |
1081 IP_VS_RT_MODE_NON_LOCAL |
1082 IP_VS_RT_MODE_TUNNEL);
1087 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1090 rt = (struct rt6_info *) skb_dst(skb);
1094 * Okay, now see if we can stuff it in the buffer as-is.
1096 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1098 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1099 &next_protocol, &payload_len,
1100 &dsfield, &ttl, NULL);
1104 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET6, cp->af)))
1107 skb->transport_header = skb->network_header;
1109 skb_push(skb, sizeof(struct ipv6hdr));
1110 skb_reset_network_header(skb);
1111 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1114 * Push down and install the IPIP header.
1116 iph = ipv6_hdr(skb);
1118 iph->nexthdr = next_protocol;
1119 iph->payload_len = htons(payload_len);
1120 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1121 ipv6_change_dsfield(iph, 0, dsfield);
1122 iph->daddr = cp->daddr.in6;
1124 iph->hop_limit = ttl;
1126 /* Another hack: avoid icmp_send in ip_fragment */
1129 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1130 if (ret == NF_ACCEPT)
1131 ip6_local_out(cp->ipvs->net, skb->sk, skb);
1132 else if (ret == NF_DROP)
1151 * Direct Routing transmitter
1152 * Used for ANY protocol
1155 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1156 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1163 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1164 IP_VS_RT_MODE_LOCAL |
1165 IP_VS_RT_MODE_NON_LOCAL |
1166 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1171 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1174 ip_send_check(ip_hdr(skb));
1176 /* Another hack: avoid icmp_send in ip_fragment */
1179 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1192 #ifdef CONFIG_IP_VS_IPV6
1194 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1195 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1202 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1205 IP_VS_RT_MODE_LOCAL |
1206 IP_VS_RT_MODE_NON_LOCAL |
1207 IP_VS_RT_MODE_KNOWN_NH);
1212 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1215 /* Another hack: avoid icmp_send in ip_fragment */
1218 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1234 * ICMP packet transmitter
1235 * called by the ip_vs_in_icmp
1238 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1239 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1240 struct ip_vs_iphdr *iph)
1242 struct rtable *rt; /* Route to the other host */
1245 int rt_mode, was_input;
1249 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1250 forwarded directly here, because there is no need to
1251 translate address/port back */
1252 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1253 if (cp->packet_xmit)
1254 rc = cp->packet_xmit(skb, cp, pp, iph);
1257 /* do not touch skb anymore */
1258 atomic_inc(&cp->in_pkts);
1263 * mangle and send the packet here (only for VS/NAT)
1265 was_input = rt_is_input_route(skb_rtable(skb));
1267 /* LOCALNODE from FORWARD hook is not supported */
1268 rt_mode = (hooknum != NF_INET_FORWARD) ?
1269 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1270 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1272 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1276 rt = skb_rtable(skb);
1279 * Avoid duplicate tuple in reply direction for NAT traffic
1280 * to local address when connection is sync-ed
1282 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1283 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1284 enum ip_conntrack_info ctinfo;
1285 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1287 if (ct && !nf_ct_is_untracked(ct)) {
1288 IP_VS_DBG(10, "%s(): "
1289 "stopping DNAT to local address %pI4\n",
1290 __func__, &cp->daddr.ip);
1296 /* From world but DNAT to loopback address? */
1297 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1298 IP_VS_DBG(1, "%s(): "
1299 "stopping DNAT to loopback %pI4\n",
1300 __func__, &cp->daddr.ip);
1304 /* copy-on-write the packet before mangling it */
1305 if (!skb_make_writable(skb, offset))
1308 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1311 ip_vs_nat_icmp(skb, pp, cp, 0);
1313 /* Another hack: avoid icmp_send in ip_fragment */
1316 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1329 #ifdef CONFIG_IP_VS_IPV6
1331 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1332 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1333 struct ip_vs_iphdr *ipvsh)
1335 struct rt6_info *rt; /* Route to the other host */
1342 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1343 forwarded directly here, because there is no need to
1344 translate address/port back */
1345 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1346 if (cp->packet_xmit)
1347 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1350 /* do not touch skb anymore */
1351 atomic_inc(&cp->in_pkts);
1356 * mangle and send the packet here (only for VS/NAT)
1359 /* LOCALNODE from FORWARD hook is not supported */
1360 rt_mode = (hooknum != NF_INET_FORWARD) ?
1361 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1362 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1364 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1365 &cp->daddr.in6, NULL, ipvsh, 0, rt_mode);
1368 rt = (struct rt6_info *) skb_dst(skb);
1370 * Avoid duplicate tuple in reply direction for NAT traffic
1371 * to local address when connection is sync-ed
1373 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1374 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1375 enum ip_conntrack_info ctinfo;
1376 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1378 if (ct && !nf_ct_is_untracked(ct)) {
1379 IP_VS_DBG(10, "%s(): "
1380 "stopping DNAT to local address %pI6\n",
1381 __func__, &cp->daddr.in6);
1387 /* From world but DNAT to loopback address? */
1388 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1389 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1390 IP_VS_DBG(1, "%s(): "
1391 "stopping DNAT to loopback %pI6\n",
1392 __func__, &cp->daddr.in6);
1396 /* copy-on-write the packet before mangling it */
1397 if (!skb_make_writable(skb, offset))
1400 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1403 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1405 /* Another hack: avoid icmp_send in ip_fragment */
1408 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);