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(int skb_af, int rt_mode,
216 struct ip_vs_iphdr *ipvsh,
217 struct sk_buff *skb, int mtu)
219 #ifdef CONFIG_IP_VS_IPV6
220 if (skb_af == AF_INET6) {
221 struct net *net = dev_net(skb_dst(skb)->dev);
223 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
225 skb->dev = net->loopback_dev;
226 /* only send ICMP too big on first fragment */
227 if (!ipvsh->fragoffs)
228 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
229 IP_VS_DBG(1, "frag needed for %pI6c\n",
230 &ipv6_hdr(skb)->saddr);
236 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
238 /* If we're going to tunnel the packet and pmtu discovery
239 * is disabled, we'll just fragment it anyway
241 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
244 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_DF) &&
245 skb->len > mtu && !skb_is_gso(skb))) {
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(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
260 __be32 daddr, int rt_mode, __be32 *ret_saddr,
261 struct ip_vs_iphdr *ipvsh)
263 struct net *net = dev_net(skb_dst(skb)->dev);
264 struct ip_vs_dest_dst *dest_dst;
265 struct rtable *rt; /* Route to the other host */
267 int local, noref = 1;
270 dest_dst = __ip_vs_dst_check(dest);
271 if (likely(dest_dst))
272 rt = (struct rtable *) dest_dst->dst_cache;
274 dest_dst = ip_vs_dest_dst_alloc();
275 spin_lock_bh(&dest->dst_lock);
277 __ip_vs_dst_set(dest, NULL, NULL, 0);
278 spin_unlock_bh(&dest->dst_lock);
281 rt = do_output_route4(net, dest->addr.ip, rt_mode,
282 &dest_dst->dst_saddr.ip);
284 __ip_vs_dst_set(dest, NULL, NULL, 0);
285 spin_unlock_bh(&dest->dst_lock);
286 ip_vs_dest_dst_free(dest_dst);
289 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
290 spin_unlock_bh(&dest->dst_lock);
291 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
292 &dest->addr.ip, &dest_dst->dst_saddr.ip,
293 atomic_read(&rt->dst.__refcnt));
296 *ret_saddr = dest_dst->dst_saddr.ip;
298 __be32 saddr = htonl(INADDR_ANY);
302 /* For such unconfigured boxes avoid many route lookups
303 * for performance reasons because we do not remember saddr
305 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
306 rt = do_output_route4(net, daddr, rt_mode, &saddr);
313 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
314 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
316 IP_VS_DBG_RL("We are crossing local and non-local addresses"
317 " daddr=%pI4\n", &daddr);
321 if (unlikely(local)) {
322 /* skb to local stack, preserve old route */
328 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
329 mtu = dst_mtu(&rt->dst);
331 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
333 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
336 maybe_update_pmtu(skb_af, skb, mtu);
339 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
345 skb_dst_set_noref(skb, &rt->dst);
347 skb_dst_set(skb, dst_clone(&rt->dst));
349 skb_dst_set(skb, &rt->dst);
359 dst_link_failure(skb);
363 #ifdef CONFIG_IP_VS_IPV6
364 static struct dst_entry *
365 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
366 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
368 struct dst_entry *dst;
369 struct flowi6 fl6 = {
373 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
374 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
376 dst = ip6_route_output(net, NULL, &fl6);
381 if (ipv6_addr_any(&fl6.saddr) &&
382 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
383 &fl6.daddr, 0, &fl6.saddr) < 0)
386 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
392 *ret_saddr = fl6.saddr;
397 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
402 * Get route to destination or remote server
405 __ip_vs_get_out_rt_v6(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
406 struct in6_addr *daddr, struct in6_addr *ret_saddr,
407 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
409 struct net *net = dev_net(skb_dst(skb)->dev);
410 struct ip_vs_dest_dst *dest_dst;
411 struct rt6_info *rt; /* Route to the other host */
412 struct dst_entry *dst;
414 int local, noref = 1;
417 dest_dst = __ip_vs_dst_check(dest);
418 if (likely(dest_dst))
419 rt = (struct rt6_info *) dest_dst->dst_cache;
423 dest_dst = ip_vs_dest_dst_alloc();
424 spin_lock_bh(&dest->dst_lock);
426 __ip_vs_dst_set(dest, NULL, NULL, 0);
427 spin_unlock_bh(&dest->dst_lock);
430 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
431 &dest_dst->dst_saddr.in6,
434 __ip_vs_dst_set(dest, NULL, NULL, 0);
435 spin_unlock_bh(&dest->dst_lock);
436 ip_vs_dest_dst_free(dest_dst);
439 rt = (struct rt6_info *) dst;
440 cookie = rt6_get_cookie(rt);
441 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
442 spin_unlock_bh(&dest->dst_lock);
443 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
444 &dest->addr.in6, &dest_dst->dst_saddr.in6,
445 atomic_read(&rt->dst.__refcnt));
448 *ret_saddr = dest_dst->dst_saddr.in6;
451 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
455 rt = (struct rt6_info *) dst;
458 local = __ip_vs_is_local_route6(rt);
460 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
462 IP_VS_DBG_RL("We are crossing local and non-local addresses"
463 " daddr=%pI6\n", daddr);
467 if (unlikely(local)) {
468 /* skb to local stack, preserve old route */
470 dst_release(&rt->dst);
475 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
476 mtu = dst_mtu(&rt->dst);
478 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
479 if (mtu < IPV6_MIN_MTU) {
480 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
484 maybe_update_pmtu(skb_af, skb, mtu);
487 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
493 skb_dst_set_noref(skb, &rt->dst);
495 skb_dst_set(skb, dst_clone(&rt->dst));
497 skb_dst_set(skb, &rt->dst);
503 dst_release(&rt->dst);
507 /* The ip6_link_failure function requires the dev field to be set
508 * in order to get the net (further for the sake of fwmark
512 skb->dev = skb_dst(skb)->dev;
514 dst_link_failure(skb);
520 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
521 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
522 struct ip_vs_conn *cp)
526 skb->ipvs_property = 1;
527 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
528 ret = ip_vs_confirm_conntrack(skb);
529 if (ret == NF_ACCEPT) {
531 skb_forward_csum(skb);
533 skb_sender_cpu_clear(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);
575 skb_sender_cpu_clear(skb);
576 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
577 NULL, skb_dst(skb)->dev, dst_output_sk);
584 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
585 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
586 struct ip_vs_conn *cp, int local)
590 skb->ipvs_property = 1;
591 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
594 ip_vs_drop_early_demux_sk(skb);
595 skb_forward_csum(skb);
597 skb_sender_cpu_clear(skb);
598 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
599 NULL, skb_dst(skb)->dev, dst_output_sk);
607 * NULL transmitter (do nothing except return NF_ACCEPT)
610 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
611 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
613 /* we do not touch skb and do not need pskb ptr */
614 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
620 * Let packets bypass the destination when the destination is not
621 * available, it may be only used in transparent cache cluster.
624 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
625 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
627 struct iphdr *iph = ip_hdr(skb);
632 if (__ip_vs_get_out_rt(cp->af, skb, NULL, iph->daddr,
633 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
638 /* Another hack: avoid icmp_send in ip_fragment */
641 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
654 #ifdef CONFIG_IP_VS_IPV6
656 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
657 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
662 if (__ip_vs_get_out_rt_v6(cp->af, skb, NULL, &ipvsh->daddr.in6, NULL,
663 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
666 /* Another hack: avoid icmp_send in ip_fragment */
669 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
684 * NAT transmitter (only for outside-to-inside nat forwarding)
685 * Not used for related ICMP
688 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
689 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
691 struct rtable *rt; /* Route to the other host */
692 int local, rc, was_input;
697 /* check if it is a connection of no-client-port */
698 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
701 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
704 ip_vs_conn_fill_cport(cp, *p);
705 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
708 was_input = rt_is_input_route(skb_rtable(skb));
709 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
710 IP_VS_RT_MODE_LOCAL |
711 IP_VS_RT_MODE_NON_LOCAL |
712 IP_VS_RT_MODE_RDR, NULL, ipvsh);
715 rt = skb_rtable(skb);
717 * Avoid duplicate tuple in reply direction for NAT traffic
718 * to local address when connection is sync-ed
720 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
721 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
722 enum ip_conntrack_info ctinfo;
723 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
725 if (ct && !nf_ct_is_untracked(ct)) {
726 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
728 "stopping DNAT to local address");
734 /* From world but DNAT to loopback address? */
735 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
736 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
737 "stopping DNAT to loopback address");
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, 0, "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->af, skb, cp->dest, &cp->daddr.in6,
799 IP_VS_RT_MODE_LOCAL |
800 IP_VS_RT_MODE_NON_LOCAL |
804 rt = (struct rt6_info *) skb_dst(skb);
806 * Avoid duplicate tuple in reply direction for NAT traffic
807 * to local address when connection is sync-ed
809 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
810 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
811 enum ip_conntrack_info ctinfo;
812 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
814 if (ct && !nf_ct_is_untracked(ct)) {
815 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
816 "ip_vs_nat_xmit_v6(): "
817 "stopping DNAT to local address");
823 /* From world but DNAT to loopback address? */
824 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
825 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
826 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
827 "ip_vs_nat_xmit_v6(): "
828 "stopping DNAT to loopback address");
832 /* copy-on-write the packet before mangling it */
833 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
836 if (skb_cow(skb, rt->dst.dev->hard_header_len))
839 /* mangle the packet */
840 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
842 ipv6_hdr(skb)->daddr = cp->daddr.in6;
844 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
846 /* FIXME: when application helper enlarges the packet and the length
847 is larger than the MTU of outgoing device, there will be still
850 /* Another hack: avoid icmp_send in ip_fragment */
853 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
867 /* When forwarding a packet, we must ensure that we've got enough headroom
868 * for the encapsulation packet in the skb. This also gives us an
869 * opportunity to figure out what the payload_len, dsfield, ttl, and df
870 * values should be, so that we won't need to look at the old ip header
873 static struct sk_buff *
874 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
875 unsigned int max_headroom, __u8 *next_protocol,
876 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
879 struct sk_buff *new_skb = NULL;
880 struct iphdr *old_iph = NULL;
881 #ifdef CONFIG_IP_VS_IPV6
882 struct ipv6hdr *old_ipv6h = NULL;
885 ip_vs_drop_early_demux_sk(skb);
887 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
888 new_skb = skb_realloc_headroom(skb, max_headroom);
892 skb_set_owner_w(new_skb, skb->sk);
897 #ifdef CONFIG_IP_VS_IPV6
898 if (skb_af == AF_INET6) {
899 old_ipv6h = ipv6_hdr(skb);
900 *next_protocol = IPPROTO_IPV6;
903 ntohs(old_ipv6h->payload_len) +
905 *dsfield = ipv6_get_dsfield(old_ipv6h);
906 *ttl = old_ipv6h->hop_limit;
912 old_iph = ip_hdr(skb);
913 /* Copy DF, reset fragment offset and MF */
915 *df = (old_iph->frag_off & htons(IP_DF));
916 *next_protocol = IPPROTO_IPIP;
918 /* fix old IP header checksum */
919 ip_send_check(old_iph);
920 *dsfield = ipv4_get_dsfield(old_iph);
923 *payload_len = ntohs(old_iph->tot_len);
929 return ERR_PTR(-ENOMEM);
932 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
934 if (encaps_af == AF_INET) {
935 if (orig_af == AF_INET)
941 /* GSO: we need to provide proper SKB_GSO_ value for IPv6:
948 * IP Tunneling transmitter
950 * This function encapsulates the packet in a new IP packet, its
951 * destination will be set to cp->daddr. Most code of this function
952 * is taken from ipip.c.
954 * It is used in VS/TUN cluster. The load balancer selects a real
955 * server from a cluster based on a scheduling algorithm,
956 * encapsulates the request packet and forwards it to the selected
957 * server. For example, all real servers are configured with
958 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
959 * the encapsulated packet, it will decapsulate the packet, processe
960 * the request and return the response packets directly to the client
961 * without passing the load balancer. This can greatly increase the
962 * scalability of virtual server.
964 * Used for ANY protocol
967 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
968 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
970 struct net *net = skb_net(skb);
971 struct netns_ipvs *ipvs = net_ipvs(net);
972 struct rtable *rt; /* Route to the other host */
973 __be32 saddr; /* Source for tunnel */
974 struct net_device *tdev; /* Device to other host */
975 __u8 next_protocol = 0;
980 struct iphdr *iph; /* Our new IP header */
981 unsigned int max_headroom; /* The extra header space needed */
987 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
988 IP_VS_RT_MODE_LOCAL |
989 IP_VS_RT_MODE_NON_LOCAL |
990 IP_VS_RT_MODE_CONNECT |
991 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
996 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
999 rt = skb_rtable(skb);
1003 * Okay, now see if we can stuff it in the buffer as-is.
1005 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1007 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1008 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1009 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1010 &next_protocol, NULL, &dsfield,
1015 skb = iptunnel_handle_offloads(
1016 skb, false, __tun_gso_type_mask(AF_INET, cp->af));
1020 skb->transport_header = skb->network_header;
1022 skb_push(skb, sizeof(struct iphdr));
1023 skb_reset_network_header(skb);
1024 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1027 * Push down and install the IPIP header.
1031 iph->ihl = sizeof(struct iphdr)>>2;
1033 iph->protocol = next_protocol;
1035 iph->daddr = cp->daddr.ip;
1038 ip_select_ident(net, skb, NULL);
1040 /* Another hack: avoid icmp_send in ip_fragment */
1043 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1044 if (ret == NF_ACCEPT)
1046 else if (ret == NF_DROP)
1062 #ifdef CONFIG_IP_VS_IPV6
1064 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1065 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1067 struct rt6_info *rt; /* Route to the other host */
1068 struct in6_addr saddr; /* Source for tunnel */
1069 struct net_device *tdev; /* Device to other host */
1070 __u8 next_protocol = 0;
1071 __u32 payload_len = 0;
1074 struct ipv6hdr *iph; /* Our new IP header */
1075 unsigned int max_headroom; /* The extra header space needed */
1081 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1083 IP_VS_RT_MODE_LOCAL |
1084 IP_VS_RT_MODE_NON_LOCAL |
1085 IP_VS_RT_MODE_TUNNEL);
1090 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1093 rt = (struct rt6_info *) skb_dst(skb);
1097 * Okay, now see if we can stuff it in the buffer as-is.
1099 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1101 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1102 &next_protocol, &payload_len,
1103 &dsfield, &ttl, NULL);
1107 skb = iptunnel_handle_offloads(
1108 skb, false, __tun_gso_type_mask(AF_INET6, cp->af));
1112 skb->transport_header = skb->network_header;
1114 skb_push(skb, sizeof(struct ipv6hdr));
1115 skb_reset_network_header(skb);
1116 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1119 * Push down and install the IPIP header.
1121 iph = ipv6_hdr(skb);
1123 iph->nexthdr = next_protocol;
1124 iph->payload_len = htons(payload_len);
1125 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1126 ipv6_change_dsfield(iph, 0, dsfield);
1127 iph->daddr = cp->daddr.in6;
1129 iph->hop_limit = ttl;
1131 /* Another hack: avoid icmp_send in ip_fragment */
1134 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1135 if (ret == NF_ACCEPT)
1137 else if (ret == NF_DROP)
1156 * Direct Routing transmitter
1157 * Used for ANY protocol
1160 ip_vs_dr_xmit(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(cp->af, skb, cp->dest, cp->daddr.ip,
1169 IP_VS_RT_MODE_LOCAL |
1170 IP_VS_RT_MODE_NON_LOCAL |
1171 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1176 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1179 ip_send_check(ip_hdr(skb));
1181 /* Another hack: avoid icmp_send in ip_fragment */
1184 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1197 #ifdef CONFIG_IP_VS_IPV6
1199 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1200 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1207 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1209 IP_VS_RT_MODE_LOCAL |
1210 IP_VS_RT_MODE_NON_LOCAL |
1211 IP_VS_RT_MODE_KNOWN_NH);
1216 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1219 /* Another hack: avoid icmp_send in ip_fragment */
1222 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1238 * ICMP packet transmitter
1239 * called by the ip_vs_in_icmp
1242 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1243 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1244 struct ip_vs_iphdr *iph)
1246 struct rtable *rt; /* Route to the other host */
1249 int rt_mode, was_input;
1253 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1254 forwarded directly here, because there is no need to
1255 translate address/port back */
1256 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1257 if (cp->packet_xmit)
1258 rc = cp->packet_xmit(skb, cp, pp, iph);
1261 /* do not touch skb anymore */
1262 atomic_inc(&cp->in_pkts);
1267 * mangle and send the packet here (only for VS/NAT)
1269 was_input = rt_is_input_route(skb_rtable(skb));
1271 /* LOCALNODE from FORWARD hook is not supported */
1272 rt_mode = (hooknum != NF_INET_FORWARD) ?
1273 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1274 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1276 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1280 rt = skb_rtable(skb);
1283 * Avoid duplicate tuple in reply direction for NAT traffic
1284 * to local address when connection is sync-ed
1286 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1287 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1288 enum ip_conntrack_info ctinfo;
1289 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1291 if (ct && !nf_ct_is_untracked(ct)) {
1292 IP_VS_DBG(10, "%s(): "
1293 "stopping DNAT to local address %pI4\n",
1294 __func__, &cp->daddr.ip);
1300 /* From world but DNAT to loopback address? */
1301 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1302 IP_VS_DBG(1, "%s(): "
1303 "stopping DNAT to loopback %pI4\n",
1304 __func__, &cp->daddr.ip);
1308 /* copy-on-write the packet before mangling it */
1309 if (!skb_make_writable(skb, offset))
1312 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1315 ip_vs_nat_icmp(skb, pp, cp, 0);
1317 /* Another hack: avoid icmp_send in ip_fragment */
1320 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1333 #ifdef CONFIG_IP_VS_IPV6
1335 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1336 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1337 struct ip_vs_iphdr *ipvsh)
1339 struct rt6_info *rt; /* Route to the other host */
1346 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1347 forwarded directly here, because there is no need to
1348 translate address/port back */
1349 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1350 if (cp->packet_xmit)
1351 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1354 /* do not touch skb anymore */
1355 atomic_inc(&cp->in_pkts);
1360 * mangle and send the packet here (only for VS/NAT)
1363 /* LOCALNODE from FORWARD hook is not supported */
1364 rt_mode = (hooknum != NF_INET_FORWARD) ?
1365 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1366 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1368 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1369 NULL, ipvsh, 0, rt_mode);
1372 rt = (struct rt6_info *) skb_dst(skb);
1374 * Avoid duplicate tuple in reply direction for NAT traffic
1375 * to local address when connection is sync-ed
1377 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1378 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1379 enum ip_conntrack_info ctinfo;
1380 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1382 if (ct && !nf_ct_is_untracked(ct)) {
1383 IP_VS_DBG(10, "%s(): "
1384 "stopping DNAT to local address %pI6\n",
1385 __func__, &cp->daddr.in6);
1391 /* From world but DNAT to loopback address? */
1392 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1393 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1394 IP_VS_DBG(1, "%s(): "
1395 "stopping DNAT to loopback %pI6\n",
1396 __func__, &cp->daddr.in6);
1400 /* copy-on-write the packet before mangling it */
1401 if (!skb_make_writable(skb, offset))
1404 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1407 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1409 /* Another hack: avoid icmp_send in ip_fragment */
1412 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);