]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - net/netfilter/ipvs/ip_vs_xmit.c
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 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
;
924 #ifdef CONFIG_IP_VS_IPV6
925 struct ipv6hdr
*old_ipv6h
= NULL
;
928 ip_vs_drop_early_demux_sk(skb
);
930 if (skb_headroom(skb
) < max_headroom
|| skb_cloned(skb
)) {
931 new_skb
= skb_realloc_headroom(skb
, max_headroom
);
935 skb_set_owner_w(new_skb
, skb
->sk
);
940 #ifdef CONFIG_IP_VS_IPV6
941 if (skb_af
== AF_INET6
) {
942 old_ipv6h
= ipv6_hdr(skb
);
943 *next_protocol
= IPPROTO_IPV6
;
946 ntohs(old_ipv6h
->payload_len
) +
948 *dsfield
= ipv6_get_dsfield(old_ipv6h
);
949 *ttl
= old_ipv6h
->hop_limit
;
955 old_iph
= ip_hdr(skb
);
956 /* Copy DF, reset fragment offset and MF */
958 *df
= (old_iph
->frag_off
& htons(IP_DF
));
959 *next_protocol
= IPPROTO_IPIP
;
961 /* fix old IP header checksum */
962 ip_send_check(old_iph
);
963 *dsfield
= ipv4_get_dsfield(old_iph
);
966 *payload_len
= ntohs(old_iph
->tot_len
);
972 return ERR_PTR(-ENOMEM
);
975 static inline int __tun_gso_type_mask(int encaps_af
, int orig_af
)
979 return SKB_GSO_IPXIP4
;
981 return SKB_GSO_IPXIP6
;
988 * IP Tunneling transmitter
990 * This function encapsulates the packet in a new IP packet, its
991 * destination will be set to cp->daddr. Most code of this function
992 * is taken from ipip.c.
994 * It is used in VS/TUN cluster. The load balancer selects a real
995 * server from a cluster based on a scheduling algorithm,
996 * encapsulates the request packet and forwards it to the selected
997 * server. For example, all real servers are configured with
998 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
999 * the encapsulated packet, it will decapsulate the packet, processe
1000 * the request and return the response packets directly to the client
1001 * without passing the load balancer. This can greatly increase the
1002 * scalability of virtual server.
1004 * Used for ANY protocol
1007 ip_vs_tunnel_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1008 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1010 struct netns_ipvs
*ipvs
= cp
->ipvs
;
1011 struct net
*net
= ipvs
->net
;
1012 struct rtable
*rt
; /* Route to the other host */
1013 __be32 saddr
; /* Source for tunnel */
1014 struct net_device
*tdev
; /* Device to other host */
1015 __u8 next_protocol
= 0;
1020 struct iphdr
*iph
; /* Our new IP header */
1021 unsigned int max_headroom
; /* The extra header space needed */
1026 local
= __ip_vs_get_out_rt(ipvs
, cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
,
1027 IP_VS_RT_MODE_LOCAL
|
1028 IP_VS_RT_MODE_NON_LOCAL
|
1029 IP_VS_RT_MODE_CONNECT
|
1030 IP_VS_RT_MODE_TUNNEL
, &saddr
, ipvsh
);
1034 return ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 1);
1036 rt
= skb_rtable(skb
);
1040 * Okay, now see if we can stuff it in the buffer as-is.
1042 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(struct iphdr
);
1044 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1045 dfp
= sysctl_pmtu_disc(ipvs
) ? &df
: NULL
;
1046 skb
= ip_vs_prepare_tunneled_skb(skb
, cp
->af
, max_headroom
,
1047 &next_protocol
, NULL
, &dsfield
,
1052 if (iptunnel_handle_offloads(skb
, __tun_gso_type_mask(AF_INET
, cp
->af
)))
1055 skb
->transport_header
= skb
->network_header
;
1057 skb_push(skb
, sizeof(struct iphdr
));
1058 skb_reset_network_header(skb
);
1059 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1062 * Push down and install the IPIP header.
1066 iph
->ihl
= sizeof(struct iphdr
)>>2;
1068 iph
->protocol
= next_protocol
;
1070 iph
->daddr
= cp
->daddr
.ip
;
1073 ip_select_ident(net
, skb
, NULL
);
1075 /* Another hack: avoid icmp_send in ip_fragment */
1078 ret
= ip_vs_tunnel_xmit_prepare(skb
, cp
);
1079 if (ret
== NF_ACCEPT
)
1080 ip_local_out(net
, skb
->sk
, skb
);
1081 else if (ret
== NF_DROP
)
1095 #ifdef CONFIG_IP_VS_IPV6
1097 ip_vs_tunnel_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1098 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1100 struct rt6_info
*rt
; /* Route to the other host */
1101 struct in6_addr saddr
; /* Source for tunnel */
1102 struct net_device
*tdev
; /* Device to other host */
1103 __u8 next_protocol
= 0;
1104 __u32 payload_len
= 0;
1107 struct ipv6hdr
*iph
; /* Our new IP header */
1108 unsigned int max_headroom
; /* The extra header space needed */
1113 local
= __ip_vs_get_out_rt_v6(cp
->ipvs
, cp
->af
, skb
, cp
->dest
,
1116 IP_VS_RT_MODE_LOCAL
|
1117 IP_VS_RT_MODE_NON_LOCAL
|
1118 IP_VS_RT_MODE_TUNNEL
);
1122 return ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 1);
1124 rt
= (struct rt6_info
*) skb_dst(skb
);
1128 * Okay, now see if we can stuff it in the buffer as-is.
1130 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(struct ipv6hdr
);
1132 skb
= ip_vs_prepare_tunneled_skb(skb
, cp
->af
, max_headroom
,
1133 &next_protocol
, &payload_len
,
1134 &dsfield
, &ttl
, NULL
);
1138 if (iptunnel_handle_offloads(skb
, __tun_gso_type_mask(AF_INET6
, cp
->af
)))
1141 skb
->transport_header
= skb
->network_header
;
1143 skb_push(skb
, sizeof(struct ipv6hdr
));
1144 skb_reset_network_header(skb
);
1145 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1148 * Push down and install the IPIP header.
1150 iph
= ipv6_hdr(skb
);
1152 iph
->nexthdr
= next_protocol
;
1153 iph
->payload_len
= htons(payload_len
);
1154 memset(&iph
->flow_lbl
, 0, sizeof(iph
->flow_lbl
));
1155 ipv6_change_dsfield(iph
, 0, dsfield
);
1156 iph
->daddr
= cp
->daddr
.in6
;
1158 iph
->hop_limit
= ttl
;
1160 /* Another hack: avoid icmp_send in ip_fragment */
1163 ret
= ip_vs_tunnel_xmit_prepare(skb
, cp
);
1164 if (ret
== NF_ACCEPT
)
1165 ip6_local_out(cp
->ipvs
->net
, skb
->sk
, skb
);
1166 else if (ret
== NF_DROP
)
1183 * Direct Routing transmitter
1184 * Used for ANY protocol
1187 ip_vs_dr_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1188 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1194 local
= __ip_vs_get_out_rt(cp
->ipvs
, cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
,
1195 IP_VS_RT_MODE_LOCAL
|
1196 IP_VS_RT_MODE_NON_LOCAL
|
1197 IP_VS_RT_MODE_KNOWN_NH
, NULL
, ipvsh
);
1201 return ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 1);
1203 ip_send_check(ip_hdr(skb
));
1205 /* Another hack: avoid icmp_send in ip_fragment */
1208 ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 0);
1219 #ifdef CONFIG_IP_VS_IPV6
1221 ip_vs_dr_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1222 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1228 local
= __ip_vs_get_out_rt_v6(cp
->ipvs
, cp
->af
, skb
, cp
->dest
,
1231 IP_VS_RT_MODE_LOCAL
|
1232 IP_VS_RT_MODE_NON_LOCAL
|
1233 IP_VS_RT_MODE_KNOWN_NH
);
1237 return ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 1);
1239 /* Another hack: avoid icmp_send in ip_fragment */
1242 ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 0);
1256 * ICMP packet transmitter
1257 * called by the ip_vs_in_icmp
1260 ip_vs_icmp_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1261 struct ip_vs_protocol
*pp
, int offset
, unsigned int hooknum
,
1262 struct ip_vs_iphdr
*iph
)
1264 struct rtable
*rt
; /* Route to the other host */
1267 int rt_mode
, was_input
;
1271 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1272 forwarded directly here, because there is no need to
1273 translate address/port back */
1274 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
) {
1275 if (cp
->packet_xmit
)
1276 rc
= cp
->packet_xmit(skb
, cp
, pp
, iph
);
1279 /* do not touch skb anymore */
1280 atomic_inc(&cp
->in_pkts
);
1285 * mangle and send the packet here (only for VS/NAT)
1287 was_input
= rt_is_input_route(skb_rtable(skb
));
1289 /* LOCALNODE from FORWARD hook is not supported */
1290 rt_mode
= (hooknum
!= NF_INET_FORWARD
) ?
1291 IP_VS_RT_MODE_LOCAL
| IP_VS_RT_MODE_NON_LOCAL
|
1292 IP_VS_RT_MODE_RDR
: IP_VS_RT_MODE_NON_LOCAL
;
1293 local
= __ip_vs_get_out_rt(cp
->ipvs
, cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
, rt_mode
,
1297 rt
= skb_rtable(skb
);
1300 * Avoid duplicate tuple in reply direction for NAT traffic
1301 * to local address when connection is sync-ed
1303 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1304 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
1305 enum ip_conntrack_info ctinfo
;
1306 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1309 IP_VS_DBG(10, "%s(): "
1310 "stopping DNAT to local address %pI4\n",
1311 __func__
, &cp
->daddr
.ip
);
1317 /* From world but DNAT to loopback address? */
1318 if (local
&& ipv4_is_loopback(cp
->daddr
.ip
) && was_input
) {
1319 IP_VS_DBG(1, "%s(): "
1320 "stopping DNAT to loopback %pI4\n",
1321 __func__
, &cp
->daddr
.ip
);
1325 /* copy-on-write the packet before mangling it */
1326 if (!skb_make_writable(skb
, offset
))
1329 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
1332 ip_vs_nat_icmp(skb
, pp
, cp
, 0);
1334 /* Another hack: avoid icmp_send in ip_fragment */
1337 rc
= ip_vs_nat_send_or_cont(NFPROTO_IPV4
, skb
, cp
, local
);
1348 #ifdef CONFIG_IP_VS_IPV6
1350 ip_vs_icmp_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1351 struct ip_vs_protocol
*pp
, int offset
, unsigned int hooknum
,
1352 struct ip_vs_iphdr
*ipvsh
)
1354 struct rt6_info
*rt
; /* Route to the other host */
1361 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1362 forwarded directly here, because there is no need to
1363 translate address/port back */
1364 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
) {
1365 if (cp
->packet_xmit
)
1366 rc
= cp
->packet_xmit(skb
, cp
, pp
, ipvsh
);
1369 /* do not touch skb anymore */
1370 atomic_inc(&cp
->in_pkts
);
1375 * mangle and send the packet here (only for VS/NAT)
1378 /* LOCALNODE from FORWARD hook is not supported */
1379 rt_mode
= (hooknum
!= NF_INET_FORWARD
) ?
1380 IP_VS_RT_MODE_LOCAL
| IP_VS_RT_MODE_NON_LOCAL
|
1381 IP_VS_RT_MODE_RDR
: IP_VS_RT_MODE_NON_LOCAL
;
1382 local
= __ip_vs_get_out_rt_v6(cp
->ipvs
, cp
->af
, skb
, cp
->dest
,
1383 &cp
->daddr
.in6
, NULL
, ipvsh
, 0, rt_mode
);
1386 rt
= (struct rt6_info
*) skb_dst(skb
);
1388 * Avoid duplicate tuple in reply direction for NAT traffic
1389 * to local address when connection is sync-ed
1391 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1392 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
1393 enum ip_conntrack_info ctinfo
;
1394 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1397 IP_VS_DBG(10, "%s(): "
1398 "stopping DNAT to local address %pI6\n",
1399 __func__
, &cp
->daddr
.in6
);
1405 /* From world but DNAT to loopback address? */
1406 if (local
&& skb
->dev
&& !(skb
->dev
->flags
& IFF_LOOPBACK
) &&
1407 ipv6_addr_type(&cp
->daddr
.in6
) & IPV6_ADDR_LOOPBACK
) {
1408 IP_VS_DBG(1, "%s(): "
1409 "stopping DNAT to loopback %pI6\n",
1410 __func__
, &cp
->daddr
.in6
);
1414 /* copy-on-write the packet before mangling it */
1415 if (!skb_make_writable(skb
, offset
))
1418 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
1421 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 0);
1423 /* Another hack: avoid icmp_send in ip_fragment */
1426 rc
= ip_vs_nat_send_or_cont(NFPROTO_IPV6
, skb
, cp
, local
);