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/addrconf.h>
42 #include <linux/icmpv6.h>
43 #include <linux/netfilter.h>
44 #include <linux/netfilter_ipv4.h>
46 #include <net/ip_vs.h>
49 IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */
50 IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */
51 IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to
54 IP_VS_RT_MODE_CONNECT = 8, /* Always bind route to saddr */
55 IP_VS_RT_MODE_KNOWN_NH = 16,/* Route via remote addr */
56 IP_VS_RT_MODE_TUNNEL = 32,/* Tunnel mode */
59 static inline struct ip_vs_dest_dst *ip_vs_dest_dst_alloc(void)
61 return kmalloc(sizeof(struct ip_vs_dest_dst), GFP_ATOMIC);
64 static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
70 * Destination cache to speed up outgoing route lookup
73 __ip_vs_dst_set(struct ip_vs_dest *dest, struct ip_vs_dest_dst *dest_dst,
74 struct dst_entry *dst, u32 dst_cookie)
76 struct ip_vs_dest_dst *old;
78 old = rcu_dereference_protected(dest->dest_dst,
79 lockdep_is_held(&dest->dst_lock));
82 dest_dst->dst_cache = dst;
83 dest_dst->dst_cookie = dst_cookie;
85 rcu_assign_pointer(dest->dest_dst, dest_dst);
88 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
91 static inline struct ip_vs_dest_dst *
92 __ip_vs_dst_check(struct ip_vs_dest *dest)
94 struct ip_vs_dest_dst *dest_dst = rcu_dereference(dest->dest_dst);
95 struct dst_entry *dst;
99 dst = dest_dst->dst_cache;
101 dst->ops->check(dst, dest_dst->dst_cookie) == NULL)
107 __mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
109 if (IP6CB(skb)->frag_max_size) {
110 /* frag_max_size tell us that, this packet have been
111 * defragmented by netfilter IPv6 conntrack module.
113 if (IP6CB(skb)->frag_max_size > mtu)
114 return true; /* largest fragment violate MTU */
116 else if (skb->len > mtu && !skb_is_gso(skb)) {
117 return true; /* Packet size violate MTU size */
122 /* Get route to daddr, update *saddr, optionally bind route to saddr */
123 static struct rtable *do_output_route4(struct net *net, __be32 daddr,
124 int rt_mode, __be32 *saddr)
130 memset(&fl4, 0, sizeof(fl4));
132 fl4.saddr = (rt_mode & IP_VS_RT_MODE_CONNECT) ? *saddr : 0;
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 /* Get route to destination or remote server */
161 __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
162 __be32 daddr, int rt_mode, __be32 *ret_saddr)
164 struct net *net = dev_net(skb_dst(skb)->dev);
165 struct netns_ipvs *ipvs = net_ipvs(net);
166 struct ip_vs_dest_dst *dest_dst;
167 struct rtable *rt; /* Route to the other host */
168 struct rtable *ort; /* Original route */
172 int local, noref = 1;
175 dest_dst = __ip_vs_dst_check(dest);
176 if (likely(dest_dst))
177 rt = (struct rtable *) dest_dst->dst_cache;
179 dest_dst = ip_vs_dest_dst_alloc();
180 spin_lock_bh(&dest->dst_lock);
182 __ip_vs_dst_set(dest, NULL, NULL, 0);
183 spin_unlock_bh(&dest->dst_lock);
186 rt = do_output_route4(net, dest->addr.ip, rt_mode,
187 &dest_dst->dst_saddr.ip);
189 __ip_vs_dst_set(dest, NULL, NULL, 0);
190 spin_unlock_bh(&dest->dst_lock);
191 ip_vs_dest_dst_free(dest_dst);
194 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
195 spin_unlock_bh(&dest->dst_lock);
196 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
197 &dest->addr.ip, &dest_dst->dst_saddr.ip,
198 atomic_read(&rt->dst.__refcnt));
200 daddr = dest->addr.ip;
202 *ret_saddr = dest_dst->dst_saddr.ip;
204 __be32 saddr = htonl(INADDR_ANY);
208 /* For such unconfigured boxes avoid many route lookups
209 * for performance reasons because we do not remember saddr
211 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
212 rt = do_output_route4(net, daddr, rt_mode, &saddr);
219 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
220 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
222 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
223 (rt->rt_flags & RTCF_LOCAL) ?
224 "local":"non-local", &daddr);
228 if (likely(!local)) {
229 if (unlikely(ipv4_is_loopback(iph->saddr))) {
230 IP_VS_DBG_RL("Stopping traffic from loopback address "
231 "%pI4 to non-local address, dest: %pI4\n",
232 &iph->saddr, &daddr);
236 ort = skb_rtable(skb);
237 if (!(rt_mode & IP_VS_RT_MODE_RDR) &&
238 !(ort->rt_flags & RTCF_LOCAL)) {
239 IP_VS_DBG_RL("Redirect from non-local address %pI4 to "
240 "local requires NAT method, dest: %pI4\n",
241 &iph->daddr, &daddr);
244 /* skb to local stack, preserve old route */
250 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
251 mtu = dst_mtu(&rt->dst);
252 df = iph->frag_off & htons(IP_DF);
254 struct sock *sk = skb->sk;
256 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
258 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
261 ort = skb_rtable(skb);
262 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT)
263 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
264 /* MTU check allowed? */
265 df = sysctl_pmtu_disc(ipvs) ? iph->frag_off & htons(IP_DF) : 0;
269 if (unlikely(df && skb->len > mtu && !skb_is_gso(skb))) {
270 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
271 IP_VS_DBG(1, "frag needed for %pI4\n", &iph->saddr);
278 skb_dst_set_noref_force(skb, &rt->dst);
280 skb_dst_set(skb, dst_clone(&rt->dst));
282 skb_dst_set(skb, &rt->dst);
292 dst_link_failure(skb);
296 #ifdef CONFIG_IP_VS_IPV6
298 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
300 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
303 static struct dst_entry *
304 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
305 struct in6_addr *ret_saddr, int do_xfrm)
307 struct dst_entry *dst;
308 struct flowi6 fl6 = {
312 dst = ip6_route_output(net, NULL, &fl6);
317 if (ipv6_addr_any(&fl6.saddr) &&
318 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
319 &fl6.daddr, 0, &fl6.saddr) < 0)
322 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
328 *ret_saddr = fl6.saddr;
333 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
338 * Get route to destination or remote server
341 __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
342 struct in6_addr *daddr, struct in6_addr *ret_saddr,
343 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
345 struct net *net = dev_net(skb_dst(skb)->dev);
346 struct ip_vs_dest_dst *dest_dst;
347 struct rt6_info *rt; /* Route to the other host */
348 struct rt6_info *ort; /* Original route */
349 struct dst_entry *dst;
351 int local, noref = 1;
354 dest_dst = __ip_vs_dst_check(dest);
355 if (likely(dest_dst))
356 rt = (struct rt6_info *) dest_dst->dst_cache;
360 dest_dst = ip_vs_dest_dst_alloc();
361 spin_lock_bh(&dest->dst_lock);
363 __ip_vs_dst_set(dest, NULL, NULL, 0);
364 spin_unlock_bh(&dest->dst_lock);
367 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
368 &dest_dst->dst_saddr.in6,
371 __ip_vs_dst_set(dest, NULL, NULL, 0);
372 spin_unlock_bh(&dest->dst_lock);
373 ip_vs_dest_dst_free(dest_dst);
376 rt = (struct rt6_info *) dst;
377 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
378 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
379 spin_unlock_bh(&dest->dst_lock);
380 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
381 &dest->addr.in6, &dest_dst->dst_saddr.in6,
382 atomic_read(&rt->dst.__refcnt));
385 *ret_saddr = dest_dst->dst_saddr.in6;
388 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
391 rt = (struct rt6_info *) dst;
394 local = __ip_vs_is_local_route6(rt);
395 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
397 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6c\n",
398 local ? "local":"non-local", daddr);
401 if (likely(!local)) {
402 if (unlikely((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
403 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
404 IPV6_ADDR_LOOPBACK)) {
405 IP_VS_DBG_RL("Stopping traffic from loopback address "
406 "%pI6c to non-local address, "
408 &ipv6_hdr(skb)->saddr, daddr);
412 ort = (struct rt6_info *) skb_dst(skb);
413 if (!(rt_mode & IP_VS_RT_MODE_RDR) &&
414 !__ip_vs_is_local_route6(ort)) {
415 IP_VS_DBG_RL("Redirect from non-local address %pI6c "
416 "to local requires NAT method, "
418 &ipv6_hdr(skb)->daddr, daddr);
421 /* skb to local stack, preserve old route */
423 dst_release(&rt->dst);
428 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
429 mtu = dst_mtu(&rt->dst);
431 struct sock *sk = skb->sk;
433 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
434 if (mtu < IPV6_MIN_MTU) {
435 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
439 ort = (struct rt6_info *) skb_dst(skb);
440 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT)
441 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
444 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
446 skb->dev = net->loopback_dev;
447 /* only send ICMP too big on first fragment */
448 if (!ipvsh->fragoffs)
449 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
450 IP_VS_DBG(1, "frag needed for %pI6c\n", &ipv6_hdr(skb)->saddr);
457 skb_dst_set_noref_force(skb, &rt->dst);
459 skb_dst_set(skb, dst_clone(&rt->dst));
461 skb_dst_set(skb, &rt->dst);
467 dst_release(&rt->dst);
471 dst_link_failure(skb);
477 /* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
478 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
479 struct ip_vs_conn *cp)
483 skb->ipvs_property = 1;
484 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
485 ret = ip_vs_confirm_conntrack(skb);
486 if (ret == NF_ACCEPT) {
488 skb_forward_csum(skb);
493 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
494 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
495 struct ip_vs_conn *cp, int local)
499 skb->ipvs_property = 1;
500 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
503 ip_vs_update_conntrack(skb, cp, 1);
505 skb_forward_csum(skb);
506 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
513 /* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
514 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
515 struct ip_vs_conn *cp, int local)
519 skb->ipvs_property = 1;
520 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
523 skb_forward_csum(skb);
524 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
533 * NULL transmitter (do nothing except return NF_ACCEPT)
536 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
537 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
539 /* we do not touch skb and do not need pskb ptr */
540 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
546 * Let packets bypass the destination when the destination is not
547 * available, it may be only used in transparent cache cluster.
550 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
551 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
553 struct iphdr *iph = ip_hdr(skb);
558 if (__ip_vs_get_out_rt(skb, NULL, iph->daddr, IP_VS_RT_MODE_NON_LOCAL,
564 /* Another hack: avoid icmp_send in ip_fragment */
567 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
580 #ifdef CONFIG_IP_VS_IPV6
582 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
583 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
588 if (__ip_vs_get_out_rt_v6(skb, NULL, &ipvsh->daddr.in6, NULL,
589 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
592 /* Another hack: avoid icmp_send in ip_fragment */
595 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
610 * NAT transmitter (only for outside-to-inside nat forwarding)
611 * Not used for related ICMP
614 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
615 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
617 struct rtable *rt; /* Route to the other host */
618 int local, rc, was_input;
623 /* check if it is a connection of no-client-port */
624 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
627 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
630 ip_vs_conn_fill_cport(cp, *p);
631 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
634 was_input = rt_is_input_route(skb_rtable(skb));
635 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
636 IP_VS_RT_MODE_LOCAL |
637 IP_VS_RT_MODE_NON_LOCAL |
638 IP_VS_RT_MODE_RDR, NULL);
641 rt = skb_rtable(skb);
643 * Avoid duplicate tuple in reply direction for NAT traffic
644 * to local address when connection is sync-ed
646 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
647 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
648 enum ip_conntrack_info ctinfo;
649 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
651 if (ct && !nf_ct_is_untracked(ct)) {
652 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
654 "stopping DNAT to local address");
660 /* From world but DNAT to loopback address? */
661 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
662 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
663 "stopping DNAT to loopback address");
667 /* copy-on-write the packet before mangling it */
668 if (!skb_make_writable(skb, sizeof(struct iphdr)))
671 if (skb_cow(skb, rt->dst.dev->hard_header_len))
674 /* mangle the packet */
675 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
677 ip_hdr(skb)->daddr = cp->daddr.ip;
678 ip_send_check(ip_hdr(skb));
680 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
682 /* FIXME: when application helper enlarges the packet and the length
683 is larger than the MTU of outgoing device, there will be still
686 /* Another hack: avoid icmp_send in ip_fragment */
689 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
702 #ifdef CONFIG_IP_VS_IPV6
704 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
705 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
707 struct rt6_info *rt; /* Route to the other host */
713 /* check if it is a connection of no-client-port */
714 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
716 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
719 ip_vs_conn_fill_cport(cp, *p);
720 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
723 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
725 IP_VS_RT_MODE_LOCAL |
726 IP_VS_RT_MODE_NON_LOCAL |
730 rt = (struct rt6_info *) skb_dst(skb);
732 * Avoid duplicate tuple in reply direction for NAT traffic
733 * to local address when connection is sync-ed
735 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
736 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
737 enum ip_conntrack_info ctinfo;
738 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
740 if (ct && !nf_ct_is_untracked(ct)) {
741 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
742 "ip_vs_nat_xmit_v6(): "
743 "stopping DNAT to local address");
749 /* From world but DNAT to loopback address? */
750 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
751 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
752 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
753 "ip_vs_nat_xmit_v6(): "
754 "stopping DNAT to loopback address");
758 /* copy-on-write the packet before mangling it */
759 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
762 if (skb_cow(skb, rt->dst.dev->hard_header_len))
765 /* mangle the packet */
766 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
768 ipv6_hdr(skb)->daddr = cp->daddr.in6;
770 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
772 /* FIXME: when application helper enlarges the packet and the length
773 is larger than the MTU of outgoing device, there will be still
776 /* Another hack: avoid icmp_send in ip_fragment */
779 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
795 * IP Tunneling transmitter
797 * This function encapsulates the packet in a new IP packet, its
798 * destination will be set to cp->daddr. Most code of this function
799 * is taken from ipip.c.
801 * It is used in VS/TUN cluster. The load balancer selects a real
802 * server from a cluster based on a scheduling algorithm,
803 * encapsulates the request packet and forwards it to the selected
804 * server. For example, all real servers are configured with
805 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
806 * the encapsulated packet, it will decapsulate the packet, processe
807 * the request and return the response packets directly to the client
808 * without passing the load balancer. This can greatly increase the
809 * scalability of virtual server.
811 * Used for ANY protocol
814 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
815 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
817 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
818 struct rtable *rt; /* Route to the other host */
819 __be32 saddr; /* Source for tunnel */
820 struct net_device *tdev; /* Device to other host */
821 struct iphdr *old_iph = ip_hdr(skb);
822 u8 tos = old_iph->tos;
824 struct iphdr *iph; /* Our new IP header */
825 unsigned int max_headroom; /* The extra header space needed */
831 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
832 IP_VS_RT_MODE_LOCAL |
833 IP_VS_RT_MODE_NON_LOCAL |
834 IP_VS_RT_MODE_CONNECT |
835 IP_VS_RT_MODE_TUNNEL, &saddr);
840 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
843 rt = skb_rtable(skb);
846 /* Copy DF, reset fragment offset and MF */
847 df = sysctl_pmtu_disc(ipvs) ? old_iph->frag_off & htons(IP_DF) : 0;
850 * Okay, now see if we can stuff it in the buffer as-is.
852 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
854 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
855 struct sk_buff *new_skb =
856 skb_realloc_headroom(skb, max_headroom);
862 old_iph = ip_hdr(skb);
865 skb->transport_header = skb->network_header;
867 /* fix old IP header checksum */
868 ip_send_check(old_iph);
870 skb_push(skb, sizeof(struct iphdr));
871 skb_reset_network_header(skb);
872 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
875 * Push down and install the IPIP header.
879 iph->ihl = sizeof(struct iphdr)>>2;
881 iph->protocol = IPPROTO_IPIP;
883 iph->daddr = cp->daddr.ip;
885 iph->ttl = old_iph->ttl;
886 ip_select_ident(skb, NULL);
888 /* Another hack: avoid icmp_send in ip_fragment */
891 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
892 if (ret == NF_ACCEPT)
894 else if (ret == NF_DROP)
909 #ifdef CONFIG_IP_VS_IPV6
911 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
912 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
914 struct rt6_info *rt; /* Route to the other host */
915 struct in6_addr saddr; /* Source for tunnel */
916 struct net_device *tdev; /* Device to other host */
917 struct ipv6hdr *old_iph = ipv6_hdr(skb);
918 struct ipv6hdr *iph; /* Our new IP header */
919 unsigned int max_headroom; /* The extra header space needed */
925 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
927 IP_VS_RT_MODE_LOCAL |
928 IP_VS_RT_MODE_NON_LOCAL |
929 IP_VS_RT_MODE_TUNNEL);
934 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
937 rt = (struct rt6_info *) skb_dst(skb);
941 * Okay, now see if we can stuff it in the buffer as-is.
943 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
945 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
946 struct sk_buff *new_skb =
947 skb_realloc_headroom(skb, max_headroom);
953 old_iph = ipv6_hdr(skb);
956 skb->transport_header = skb->network_header;
958 skb_push(skb, sizeof(struct ipv6hdr));
959 skb_reset_network_header(skb);
960 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
963 * Push down and install the IPIP header.
967 iph->nexthdr = IPPROTO_IPV6;
968 iph->payload_len = old_iph->payload_len;
969 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
970 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
971 ipv6_change_dsfield(iph, 0, ipv6_get_dsfield(old_iph));
972 iph->daddr = cp->daddr.in6;
974 iph->hop_limit = old_iph->hop_limit;
976 /* Another hack: avoid icmp_send in ip_fragment */
979 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
980 if (ret == NF_ACCEPT)
982 else if (ret == NF_DROP)
1000 * Direct Routing transmitter
1001 * Used for ANY protocol
1004 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1005 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1012 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1013 IP_VS_RT_MODE_LOCAL |
1014 IP_VS_RT_MODE_NON_LOCAL |
1015 IP_VS_RT_MODE_KNOWN_NH, NULL);
1020 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1023 ip_send_check(ip_hdr(skb));
1025 /* Another hack: avoid icmp_send in ip_fragment */
1028 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1041 #ifdef CONFIG_IP_VS_IPV6
1043 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1044 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1051 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1053 IP_VS_RT_MODE_LOCAL |
1054 IP_VS_RT_MODE_NON_LOCAL);
1059 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1062 /* Another hack: avoid icmp_send in ip_fragment */
1065 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1081 * ICMP packet transmitter
1082 * called by the ip_vs_in_icmp
1085 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1086 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1087 struct ip_vs_iphdr *iph)
1089 struct rtable *rt; /* Route to the other host */
1092 int rt_mode, was_input;
1096 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1097 forwarded directly here, because there is no need to
1098 translate address/port back */
1099 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1100 if (cp->packet_xmit)
1101 rc = cp->packet_xmit(skb, cp, pp, iph);
1104 /* do not touch skb anymore */
1105 atomic_inc(&cp->in_pkts);
1110 * mangle and send the packet here (only for VS/NAT)
1112 was_input = rt_is_input_route(skb_rtable(skb));
1114 /* LOCALNODE from FORWARD hook is not supported */
1115 rt_mode = (hooknum != NF_INET_FORWARD) ?
1116 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1117 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1119 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, rt_mode, NULL);
1122 rt = skb_rtable(skb);
1125 * Avoid duplicate tuple in reply direction for NAT traffic
1126 * to local address when connection is sync-ed
1128 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1129 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1130 enum ip_conntrack_info ctinfo;
1131 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1133 if (ct && !nf_ct_is_untracked(ct)) {
1134 IP_VS_DBG(10, "%s(): "
1135 "stopping DNAT to local address %pI4\n",
1136 __func__, &cp->daddr.ip);
1142 /* From world but DNAT to loopback address? */
1143 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1144 IP_VS_DBG(1, "%s(): "
1145 "stopping DNAT to loopback %pI4\n",
1146 __func__, &cp->daddr.ip);
1150 /* copy-on-write the packet before mangling it */
1151 if (!skb_make_writable(skb, offset))
1154 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1157 ip_vs_nat_icmp(skb, pp, cp, 0);
1159 /* Another hack: avoid icmp_send in ip_fragment */
1162 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1175 #ifdef CONFIG_IP_VS_IPV6
1177 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1178 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1179 struct ip_vs_iphdr *ipvsh)
1181 struct rt6_info *rt; /* Route to the other host */
1188 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1189 forwarded directly here, because there is no need to
1190 translate address/port back */
1191 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1192 if (cp->packet_xmit)
1193 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1196 /* do not touch skb anymore */
1197 atomic_inc(&cp->in_pkts);
1202 * mangle and send the packet here (only for VS/NAT)
1205 /* LOCALNODE from FORWARD hook is not supported */
1206 rt_mode = (hooknum != NF_INET_FORWARD) ?
1207 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1208 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1210 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1214 rt = (struct rt6_info *) skb_dst(skb);
1216 * Avoid duplicate tuple in reply direction for NAT traffic
1217 * to local address when connection is sync-ed
1219 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1220 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1221 enum ip_conntrack_info ctinfo;
1222 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1224 if (ct && !nf_ct_is_untracked(ct)) {
1225 IP_VS_DBG(10, "%s(): "
1226 "stopping DNAT to local address %pI6\n",
1227 __func__, &cp->daddr.in6);
1233 /* From world but DNAT to loopback address? */
1234 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1235 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1236 IP_VS_DBG(1, "%s(): "
1237 "stopping DNAT to loopback %pI6\n",
1238 __func__, &cp->daddr.in6);
1242 /* copy-on-write the packet before mangling it */
1243 if (!skb_make_writable(skb, offset))
1246 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1249 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1251 /* Another hack: avoid icmp_send in ip_fragment */
1254 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);