]>
git.proxmox.com Git - mirror_ubuntu-bionic-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(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
&& !ip_vs_iph_icmp(ipvsh
))
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 !ip_vs_iph_icmp(ipvsh
))) {
247 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
249 IP_VS_DBG(1, "frag needed for %pI4\n",
250 &ip_hdr(skb
)->saddr
);
258 /* Get route to destination or remote server */
260 __ip_vs_get_out_rt(int skb_af
, struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
261 __be32 daddr
, int rt_mode
, __be32
*ret_saddr
,
262 struct ip_vs_iphdr
*ipvsh
)
264 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
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(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(int skb_af
, struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
407 struct in6_addr
*daddr
, struct in6_addr
*ret_saddr
,
408 struct ip_vs_iphdr
*ipvsh
, int do_xfrm
, int rt_mode
)
410 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
411 struct ip_vs_dest_dst
*dest_dst
;
412 struct rt6_info
*rt
; /* Route to the other host */
413 struct dst_entry
*dst
;
415 int local
, noref
= 1;
418 dest_dst
= __ip_vs_dst_check(dest
);
419 if (likely(dest_dst
))
420 rt
= (struct rt6_info
*) dest_dst
->dst_cache
;
424 dest_dst
= ip_vs_dest_dst_alloc();
425 spin_lock_bh(&dest
->dst_lock
);
427 __ip_vs_dst_set(dest
, NULL
, NULL
, 0);
428 spin_unlock_bh(&dest
->dst_lock
);
431 dst
= __ip_vs_route_output_v6(net
, &dest
->addr
.in6
,
432 &dest_dst
->dst_saddr
.in6
,
435 __ip_vs_dst_set(dest
, NULL
, NULL
, 0);
436 spin_unlock_bh(&dest
->dst_lock
);
437 ip_vs_dest_dst_free(dest_dst
);
440 rt
= (struct rt6_info
*) dst
;
441 cookie
= rt6_get_cookie(rt
);
442 __ip_vs_dst_set(dest
, dest_dst
, &rt
->dst
, cookie
);
443 spin_unlock_bh(&dest
->dst_lock
);
444 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
445 &dest
->addr
.in6
, &dest_dst
->dst_saddr
.in6
,
446 atomic_read(&rt
->dst
.__refcnt
));
449 *ret_saddr
= dest_dst
->dst_saddr
.in6
;
452 dst
= __ip_vs_route_output_v6(net
, daddr
, ret_saddr
, do_xfrm
,
456 rt
= (struct rt6_info
*) dst
;
459 local
= __ip_vs_is_local_route6(rt
);
461 if (unlikely(crosses_local_route_boundary(skb_af
, skb
, rt_mode
,
463 IP_VS_DBG_RL("We are crossing local and non-local addresses"
464 " daddr=%pI6\n", daddr
);
468 if (unlikely(local
)) {
469 /* skb to local stack, preserve old route */
471 dst_release(&rt
->dst
);
476 if (likely(!(rt_mode
& IP_VS_RT_MODE_TUNNEL
)))
477 mtu
= dst_mtu(&rt
->dst
);
479 mtu
= dst_mtu(&rt
->dst
) - sizeof(struct ipv6hdr
);
480 if (mtu
< IPV6_MIN_MTU
) {
481 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__
,
485 maybe_update_pmtu(skb_af
, skb
, mtu
);
488 if (!ensure_mtu_is_adequate(skb_af
, rt_mode
, ipvsh
, skb
, mtu
))
494 skb_dst_set_noref(skb
, &rt
->dst
);
496 skb_dst_set(skb
, dst_clone(&rt
->dst
));
498 skb_dst_set(skb
, &rt
->dst
);
504 dst_release(&rt
->dst
);
508 /* The ip6_link_failure function requires the dev field to be set
509 * in order to get the net (further for the sake of fwmark
513 skb
->dev
= skb_dst(skb
)->dev
;
515 dst_link_failure(skb
);
521 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
522 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff
*skb
,
523 struct ip_vs_conn
*cp
)
527 skb
->ipvs_property
= 1;
528 if (unlikely(cp
->flags
& IP_VS_CONN_F_NFCT
))
529 ret
= ip_vs_confirm_conntrack(skb
);
530 if (ret
== NF_ACCEPT
) {
532 skb_forward_csum(skb
);
534 skb_sender_cpu_clear(skb
);
539 /* In the event of a remote destination, it's possible that we would have
540 * matches against an old socket (particularly a TIME-WAIT socket). This
541 * causes havoc down the line (ip_local_out et. al. expect regular sockets
542 * and invalid memory accesses will happen) so simply drop the association
545 static inline void ip_vs_drop_early_demux_sk(struct sk_buff
*skb
)
547 /* If dev is set, the packet came from the LOCAL_IN callback and
548 * not from a local TCP socket.
554 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
555 static inline int ip_vs_nat_send_or_cont(int pf
, struct sk_buff
*skb
,
556 struct ip_vs_conn
*cp
, int local
)
560 skb
->ipvs_property
= 1;
561 if (likely(!(cp
->flags
& IP_VS_CONN_F_NFCT
)))
564 ip_vs_update_conntrack(skb
, cp
, 1);
566 /* Remove the early_demux association unless it's bound for the
567 * exact same port and address on this host after translation.
569 if (!local
|| cp
->vport
!= cp
->dport
||
570 !ip_vs_addr_equal(cp
->af
, &cp
->vaddr
, &cp
->daddr
))
571 ip_vs_drop_early_demux_sk(skb
);
574 skb_forward_csum(skb
);
576 skb_sender_cpu_clear(skb
);
577 NF_HOOK(pf
, NF_INET_LOCAL_OUT
, ip_vs_conn_net(cp
), NULL
, skb
,
578 NULL
, skb_dst(skb
)->dev
, dst_output_okfn
);
585 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
586 static inline int ip_vs_send_or_cont(int pf
, struct sk_buff
*skb
,
587 struct ip_vs_conn
*cp
, int local
)
591 skb
->ipvs_property
= 1;
592 if (likely(!(cp
->flags
& IP_VS_CONN_F_NFCT
)))
595 ip_vs_drop_early_demux_sk(skb
);
596 skb_forward_csum(skb
);
598 skb_sender_cpu_clear(skb
);
599 NF_HOOK(pf
, NF_INET_LOCAL_OUT
, ip_vs_conn_net(cp
), NULL
, skb
,
600 NULL
, skb_dst(skb
)->dev
, dst_output_okfn
);
608 * NULL transmitter (do nothing except return NF_ACCEPT)
611 ip_vs_null_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
612 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
614 /* we do not touch skb and do not need pskb ptr */
615 return ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 1);
621 * Let packets bypass the destination when the destination is not
622 * available, it may be only used in transparent cache cluster.
625 ip_vs_bypass_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
626 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
628 struct iphdr
*iph
= ip_hdr(skb
);
633 if (__ip_vs_get_out_rt(cp
->af
, skb
, NULL
, iph
->daddr
,
634 IP_VS_RT_MODE_NON_LOCAL
, NULL
, ipvsh
) < 0)
639 /* Another hack: avoid icmp_send in ip_fragment */
642 ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 0);
655 #ifdef CONFIG_IP_VS_IPV6
657 ip_vs_bypass_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
658 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
660 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
665 if (__ip_vs_get_out_rt_v6(cp
->af
, skb
, NULL
, &iph
->daddr
, NULL
,
666 ipvsh
, 0, IP_VS_RT_MODE_NON_LOCAL
) < 0)
669 /* Another hack: avoid icmp_send in ip_fragment */
672 ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 0);
687 * NAT transmitter (only for outside-to-inside nat forwarding)
688 * Not used for related ICMP
691 ip_vs_nat_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
692 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
694 struct rtable
*rt
; /* Route to the other host */
695 int local
, rc
, was_input
;
700 /* check if it is a connection of no-client-port */
701 if (unlikely(cp
->flags
& IP_VS_CONN_F_NO_CPORT
)) {
704 p
= skb_header_pointer(skb
, ipvsh
->len
, sizeof(_pt
), &_pt
);
707 ip_vs_conn_fill_cport(cp
, *p
);
708 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p
));
711 was_input
= rt_is_input_route(skb_rtable(skb
));
712 local
= __ip_vs_get_out_rt(cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
,
713 IP_VS_RT_MODE_LOCAL
|
714 IP_VS_RT_MODE_NON_LOCAL
|
715 IP_VS_RT_MODE_RDR
, NULL
, ipvsh
);
718 rt
= skb_rtable(skb
);
720 * Avoid duplicate tuple in reply direction for NAT traffic
721 * to local address when connection is sync-ed
723 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
724 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
725 enum ip_conntrack_info ctinfo
;
726 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
728 if (ct
&& !nf_ct_is_untracked(ct
)) {
729 IP_VS_DBG_RL_PKT(10, AF_INET
, pp
, skb
, ipvsh
->off
,
731 "stopping DNAT to local address");
737 /* From world but DNAT to loopback address? */
738 if (local
&& ipv4_is_loopback(cp
->daddr
.ip
) && was_input
) {
739 IP_VS_DBG_RL_PKT(1, AF_INET
, pp
, skb
, ipvsh
->off
,
740 "ip_vs_nat_xmit(): stopping DNAT to loopback "
745 /* copy-on-write the packet before mangling it */
746 if (!skb_make_writable(skb
, sizeof(struct iphdr
)))
749 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
752 /* mangle the packet */
753 if (pp
->dnat_handler
&& !pp
->dnat_handler(skb
, pp
, cp
, ipvsh
))
755 ip_hdr(skb
)->daddr
= cp
->daddr
.ip
;
756 ip_send_check(ip_hdr(skb
));
758 IP_VS_DBG_PKT(10, AF_INET
, pp
, skb
, ipvsh
->off
, "After DNAT");
760 /* FIXME: when application helper enlarges the packet and the length
761 is larger than the MTU of outgoing device, there will be still
764 /* Another hack: avoid icmp_send in ip_fragment */
767 rc
= ip_vs_nat_send_or_cont(NFPROTO_IPV4
, skb
, cp
, local
);
780 #ifdef CONFIG_IP_VS_IPV6
782 ip_vs_nat_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
783 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
785 struct rt6_info
*rt
; /* Route to the other host */
791 /* check if it is a connection of no-client-port */
792 if (unlikely(cp
->flags
& IP_VS_CONN_F_NO_CPORT
&& !ipvsh
->fragoffs
)) {
794 p
= skb_header_pointer(skb
, ipvsh
->len
, sizeof(_pt
), &_pt
);
797 ip_vs_conn_fill_cport(cp
, *p
);
798 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p
));
801 local
= __ip_vs_get_out_rt_v6(cp
->af
, skb
, cp
->dest
, &cp
->daddr
.in6
,
803 IP_VS_RT_MODE_LOCAL
|
804 IP_VS_RT_MODE_NON_LOCAL
|
808 rt
= (struct rt6_info
*) skb_dst(skb
);
810 * Avoid duplicate tuple in reply direction for NAT traffic
811 * to local address when connection is sync-ed
813 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
814 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
815 enum ip_conntrack_info ctinfo
;
816 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
818 if (ct
&& !nf_ct_is_untracked(ct
)) {
819 IP_VS_DBG_RL_PKT(10, AF_INET6
, pp
, skb
, ipvsh
->off
,
820 "ip_vs_nat_xmit_v6(): "
821 "stopping DNAT to local address");
827 /* From world but DNAT to loopback address? */
828 if (local
&& skb
->dev
&& !(skb
->dev
->flags
& IFF_LOOPBACK
) &&
829 ipv6_addr_type(&cp
->daddr
.in6
) & IPV6_ADDR_LOOPBACK
) {
830 IP_VS_DBG_RL_PKT(1, AF_INET6
, pp
, skb
, ipvsh
->off
,
831 "ip_vs_nat_xmit_v6(): "
832 "stopping DNAT to loopback address");
836 /* copy-on-write the packet before mangling it */
837 if (!skb_make_writable(skb
, sizeof(struct ipv6hdr
)))
840 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
843 /* mangle the packet */
844 if (pp
->dnat_handler
&& !pp
->dnat_handler(skb
, pp
, cp
, ipvsh
))
846 ipv6_hdr(skb
)->daddr
= cp
->daddr
.in6
;
848 IP_VS_DBG_PKT(10, AF_INET6
, pp
, skb
, ipvsh
->off
, "After DNAT");
850 /* FIXME: when application helper enlarges the packet and the length
851 is larger than the MTU of outgoing device, there will be still
854 /* Another hack: avoid icmp_send in ip_fragment */
857 rc
= ip_vs_nat_send_or_cont(NFPROTO_IPV6
, skb
, cp
, local
);
871 /* When forwarding a packet, we must ensure that we've got enough headroom
872 * for the encapsulation packet in the skb. This also gives us an
873 * opportunity to figure out what the payload_len, dsfield, ttl, and df
874 * values should be, so that we won't need to look at the old ip header
877 static struct sk_buff
*
878 ip_vs_prepare_tunneled_skb(struct sk_buff
*skb
, int skb_af
,
879 unsigned int max_headroom
, __u8
*next_protocol
,
880 __u32
*payload_len
, __u8
*dsfield
, __u8
*ttl
,
883 struct sk_buff
*new_skb
= NULL
;
884 struct iphdr
*old_iph
= NULL
;
885 #ifdef CONFIG_IP_VS_IPV6
886 struct ipv6hdr
*old_ipv6h
= NULL
;
889 ip_vs_drop_early_demux_sk(skb
);
891 if (skb_headroom(skb
) < max_headroom
|| skb_cloned(skb
)) {
892 new_skb
= skb_realloc_headroom(skb
, max_headroom
);
896 skb_set_owner_w(new_skb
, skb
->sk
);
901 #ifdef CONFIG_IP_VS_IPV6
902 if (skb_af
== AF_INET6
) {
903 old_ipv6h
= ipv6_hdr(skb
);
904 *next_protocol
= IPPROTO_IPV6
;
907 ntohs(old_ipv6h
->payload_len
) +
909 *dsfield
= ipv6_get_dsfield(old_ipv6h
);
910 *ttl
= old_ipv6h
->hop_limit
;
916 old_iph
= ip_hdr(skb
);
917 /* Copy DF, reset fragment offset and MF */
919 *df
= (old_iph
->frag_off
& htons(IP_DF
));
920 *next_protocol
= IPPROTO_IPIP
;
922 /* fix old IP header checksum */
923 ip_send_check(old_iph
);
924 *dsfield
= ipv4_get_dsfield(old_iph
);
927 *payload_len
= ntohs(old_iph
->tot_len
);
933 return ERR_PTR(-ENOMEM
);
936 static inline int __tun_gso_type_mask(int encaps_af
, int orig_af
)
938 if (encaps_af
== AF_INET
) {
939 if (orig_af
== AF_INET
)
945 /* GSO: we need to provide proper SKB_GSO_ value for IPv6:
952 * IP Tunneling transmitter
954 * This function encapsulates the packet in a new IP packet, its
955 * destination will be set to cp->daddr. Most code of this function
956 * is taken from ipip.c.
958 * It is used in VS/TUN cluster. The load balancer selects a real
959 * server from a cluster based on a scheduling algorithm,
960 * encapsulates the request packet and forwards it to the selected
961 * server. For example, all real servers are configured with
962 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
963 * the encapsulated packet, it will decapsulate the packet, processe
964 * the request and return the response packets directly to the client
965 * without passing the load balancer. This can greatly increase the
966 * scalability of virtual server.
968 * Used for ANY protocol
971 ip_vs_tunnel_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
972 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
974 struct net
*net
= skb_net(skb
);
975 struct netns_ipvs
*ipvs
= net_ipvs(net
);
976 struct rtable
*rt
; /* Route to the other host */
977 __be32 saddr
; /* Source for tunnel */
978 struct net_device
*tdev
; /* Device to other host */
979 __u8 next_protocol
= 0;
984 struct iphdr
*iph
; /* Our new IP header */
985 unsigned int max_headroom
; /* The extra header space needed */
991 local
= __ip_vs_get_out_rt(cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
,
992 IP_VS_RT_MODE_LOCAL
|
993 IP_VS_RT_MODE_NON_LOCAL
|
994 IP_VS_RT_MODE_CONNECT
|
995 IP_VS_RT_MODE_TUNNEL
, &saddr
, ipvsh
);
1000 return ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 1);
1003 rt
= skb_rtable(skb
);
1007 * Okay, now see if we can stuff it in the buffer as-is.
1009 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(struct iphdr
);
1011 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1012 dfp
= sysctl_pmtu_disc(ipvs
) ? &df
: NULL
;
1013 skb
= ip_vs_prepare_tunneled_skb(skb
, cp
->af
, max_headroom
,
1014 &next_protocol
, NULL
, &dsfield
,
1019 skb
= iptunnel_handle_offloads(
1020 skb
, false, __tun_gso_type_mask(AF_INET
, cp
->af
));
1024 skb
->transport_header
= skb
->network_header
;
1026 skb_push(skb
, sizeof(struct iphdr
));
1027 skb_reset_network_header(skb
);
1028 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1031 * Push down and install the IPIP header.
1035 iph
->ihl
= sizeof(struct iphdr
)>>2;
1037 iph
->protocol
= next_protocol
;
1039 iph
->daddr
= cp
->daddr
.ip
;
1042 ip_select_ident(net
, skb
, NULL
);
1044 /* Another hack: avoid icmp_send in ip_fragment */
1047 ret
= ip_vs_tunnel_xmit_prepare(skb
, cp
);
1048 if (ret
== NF_ACCEPT
)
1050 else if (ret
== NF_DROP
)
1066 #ifdef CONFIG_IP_VS_IPV6
1068 ip_vs_tunnel_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1069 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1071 struct rt6_info
*rt
; /* Route to the other host */
1072 struct in6_addr saddr
; /* Source for tunnel */
1073 struct net_device
*tdev
; /* Device to other host */
1074 __u8 next_protocol
= 0;
1075 __u32 payload_len
= 0;
1078 struct ipv6hdr
*iph
; /* Our new IP header */
1079 unsigned int max_headroom
; /* The extra header space needed */
1085 local
= __ip_vs_get_out_rt_v6(cp
->af
, skb
, cp
->dest
, &cp
->daddr
.in6
,
1087 IP_VS_RT_MODE_LOCAL
|
1088 IP_VS_RT_MODE_NON_LOCAL
|
1089 IP_VS_RT_MODE_TUNNEL
);
1094 return ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 1);
1097 rt
= (struct rt6_info
*) skb_dst(skb
);
1101 * Okay, now see if we can stuff it in the buffer as-is.
1103 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(struct ipv6hdr
);
1105 skb
= ip_vs_prepare_tunneled_skb(skb
, cp
->af
, max_headroom
,
1106 &next_protocol
, &payload_len
,
1107 &dsfield
, &ttl
, NULL
);
1111 skb
= iptunnel_handle_offloads(
1112 skb
, false, __tun_gso_type_mask(AF_INET6
, cp
->af
));
1116 skb
->transport_header
= skb
->network_header
;
1118 skb_push(skb
, sizeof(struct ipv6hdr
));
1119 skb_reset_network_header(skb
);
1120 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1123 * Push down and install the IPIP header.
1125 iph
= ipv6_hdr(skb
);
1127 iph
->nexthdr
= next_protocol
;
1128 iph
->payload_len
= htons(payload_len
);
1129 memset(&iph
->flow_lbl
, 0, sizeof(iph
->flow_lbl
));
1130 ipv6_change_dsfield(iph
, 0, dsfield
);
1131 iph
->daddr
= cp
->daddr
.in6
;
1133 iph
->hop_limit
= ttl
;
1135 /* Another hack: avoid icmp_send in ip_fragment */
1138 ret
= ip_vs_tunnel_xmit_prepare(skb
, cp
);
1139 if (ret
== NF_ACCEPT
)
1141 else if (ret
== NF_DROP
)
1160 * Direct Routing transmitter
1161 * Used for ANY protocol
1164 ip_vs_dr_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1165 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1172 local
= __ip_vs_get_out_rt(cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
,
1173 IP_VS_RT_MODE_LOCAL
|
1174 IP_VS_RT_MODE_NON_LOCAL
|
1175 IP_VS_RT_MODE_KNOWN_NH
, NULL
, ipvsh
);
1180 return ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 1);
1183 ip_send_check(ip_hdr(skb
));
1185 /* Another hack: avoid icmp_send in ip_fragment */
1188 ip_vs_send_or_cont(NFPROTO_IPV4
, skb
, cp
, 0);
1201 #ifdef CONFIG_IP_VS_IPV6
1203 ip_vs_dr_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1204 struct ip_vs_protocol
*pp
, struct ip_vs_iphdr
*ipvsh
)
1211 local
= __ip_vs_get_out_rt_v6(cp
->af
, skb
, cp
->dest
, &cp
->daddr
.in6
,
1213 IP_VS_RT_MODE_LOCAL
|
1214 IP_VS_RT_MODE_NON_LOCAL
|
1215 IP_VS_RT_MODE_KNOWN_NH
);
1220 return ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 1);
1223 /* Another hack: avoid icmp_send in ip_fragment */
1226 ip_vs_send_or_cont(NFPROTO_IPV6
, skb
, cp
, 0);
1242 * ICMP packet transmitter
1243 * called by the ip_vs_in_icmp
1246 ip_vs_icmp_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1247 struct ip_vs_protocol
*pp
, int offset
, unsigned int hooknum
,
1248 struct ip_vs_iphdr
*iph
)
1250 struct rtable
*rt
; /* Route to the other host */
1253 int rt_mode
, was_input
;
1257 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1258 forwarded directly here, because there is no need to
1259 translate address/port back */
1260 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
) {
1261 if (cp
->packet_xmit
)
1262 rc
= cp
->packet_xmit(skb
, cp
, pp
, iph
);
1265 /* do not touch skb anymore */
1266 atomic_inc(&cp
->in_pkts
);
1271 * mangle and send the packet here (only for VS/NAT)
1273 was_input
= rt_is_input_route(skb_rtable(skb
));
1275 /* LOCALNODE from FORWARD hook is not supported */
1276 rt_mode
= (hooknum
!= NF_INET_FORWARD
) ?
1277 IP_VS_RT_MODE_LOCAL
| IP_VS_RT_MODE_NON_LOCAL
|
1278 IP_VS_RT_MODE_RDR
: IP_VS_RT_MODE_NON_LOCAL
;
1280 local
= __ip_vs_get_out_rt(cp
->af
, skb
, cp
->dest
, cp
->daddr
.ip
, rt_mode
,
1284 rt
= skb_rtable(skb
);
1287 * Avoid duplicate tuple in reply direction for NAT traffic
1288 * to local address when connection is sync-ed
1290 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1291 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
1292 enum ip_conntrack_info ctinfo
;
1293 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1295 if (ct
&& !nf_ct_is_untracked(ct
)) {
1296 IP_VS_DBG(10, "%s(): "
1297 "stopping DNAT to local address %pI4\n",
1298 __func__
, &cp
->daddr
.ip
);
1304 /* From world but DNAT to loopback address? */
1305 if (local
&& ipv4_is_loopback(cp
->daddr
.ip
) && was_input
) {
1306 IP_VS_DBG(1, "%s(): "
1307 "stopping DNAT to loopback %pI4\n",
1308 __func__
, &cp
->daddr
.ip
);
1312 /* copy-on-write the packet before mangling it */
1313 if (!skb_make_writable(skb
, offset
))
1316 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
1319 ip_vs_nat_icmp(skb
, pp
, cp
, 0);
1321 /* Another hack: avoid icmp_send in ip_fragment */
1324 rc
= ip_vs_nat_send_or_cont(NFPROTO_IPV4
, skb
, cp
, local
);
1337 #ifdef CONFIG_IP_VS_IPV6
1339 ip_vs_icmp_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1340 struct ip_vs_protocol
*pp
, int offset
, unsigned int hooknum
,
1341 struct ip_vs_iphdr
*ipvsh
)
1343 struct rt6_info
*rt
; /* Route to the other host */
1350 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1351 forwarded directly here, because there is no need to
1352 translate address/port back */
1353 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
) {
1354 if (cp
->packet_xmit
)
1355 rc
= cp
->packet_xmit(skb
, cp
, pp
, ipvsh
);
1358 /* do not touch skb anymore */
1359 atomic_inc(&cp
->in_pkts
);
1364 * mangle and send the packet here (only for VS/NAT)
1367 /* LOCALNODE from FORWARD hook is not supported */
1368 rt_mode
= (hooknum
!= NF_INET_FORWARD
) ?
1369 IP_VS_RT_MODE_LOCAL
| IP_VS_RT_MODE_NON_LOCAL
|
1370 IP_VS_RT_MODE_RDR
: IP_VS_RT_MODE_NON_LOCAL
;
1372 local
= __ip_vs_get_out_rt_v6(cp
->af
, skb
, cp
->dest
, &cp
->daddr
.in6
,
1373 NULL
, ipvsh
, 0, rt_mode
);
1376 rt
= (struct rt6_info
*) skb_dst(skb
);
1378 * Avoid duplicate tuple in reply direction for NAT traffic
1379 * to local address when connection is sync-ed
1381 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1382 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
1383 enum ip_conntrack_info ctinfo
;
1384 struct nf_conn
*ct
= nf_ct_get(skb
, &ctinfo
);
1386 if (ct
&& !nf_ct_is_untracked(ct
)) {
1387 IP_VS_DBG(10, "%s(): "
1388 "stopping DNAT to local address %pI6\n",
1389 __func__
, &cp
->daddr
.in6
);
1395 /* From world but DNAT to loopback address? */
1396 if (local
&& skb
->dev
&& !(skb
->dev
->flags
& IFF_LOOPBACK
) &&
1397 ipv6_addr_type(&cp
->daddr
.in6
) & IPV6_ADDR_LOOPBACK
) {
1398 IP_VS_DBG(1, "%s(): "
1399 "stopping DNAT to loopback %pI6\n",
1400 __func__
, &cp
->daddr
.in6
);
1404 /* copy-on-write the packet before mangling it */
1405 if (!skb_make_writable(skb
, offset
))
1408 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
1411 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 0);
1413 /* Another hack: avoid icmp_send in ip_fragment */
1416 rc
= ip_vs_nat_send_or_cont(NFPROTO_IPV6
, skb
, cp
, local
);