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
21 * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING)
22 * - skb->pkt_type is not set yet
23 * - the only place where we can see skb->sk != NULL
26 #define KMSG_COMPONENT "IPVS"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/tcp.h> /* for tcphdr */
33 #include <net/tcp.h> /* for csum_tcpudp_magic */
35 #include <net/icmp.h> /* for icmp_send */
36 #include <net/route.h> /* for ip_route_output */
38 #include <net/ip6_route.h>
39 #include <net/addrconf.h>
40 #include <linux/icmpv6.h>
41 #include <linux/netfilter.h>
42 #include <linux/netfilter_ipv4.h>
44 #include <net/ip_vs.h>
48 * Destination cache to speed up outgoing route lookup
51 __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst,
54 struct dst_entry *old_dst;
56 old_dst = dest->dst_cache;
57 dest->dst_cache = dst;
58 dest->dst_rtos = rtos;
59 dest->dst_cookie = dst_cookie;
63 static inline struct dst_entry *
64 __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
66 struct dst_entry *dst = dest->dst_cache;
70 if ((dst->obsolete || rtos != dest->dst_rtos) &&
71 dst->ops->check(dst, dest->dst_cookie) == NULL) {
72 dest->dst_cache = NULL;
81 * Get route to destination or remote server
82 * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest,
83 * &4=Allow redirect from remote daddr to local
85 static struct rtable *
86 __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
87 __be32 daddr, u32 rtos, int rt_mode)
89 struct net *net = dev_net(skb_dst(skb)->dev);
90 struct rtable *rt; /* Route to the other host */
91 struct rtable *ort; /* Original route */
95 spin_lock(&dest->dst_lock);
96 if (!(rt = (struct rtable *)
97 __ip_vs_dst_check(dest, rtos))) {
102 .daddr = dest->addr.ip,
107 if (ip_route_output_key(net, &rt, &fl)) {
108 spin_unlock(&dest->dst_lock);
109 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
113 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
114 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
116 atomic_read(&rt->dst.__refcnt), rtos);
118 spin_unlock(&dest->dst_lock);
129 if (ip_route_output_key(net, &rt, &fl)) {
130 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
136 local = rt->rt_flags & RTCF_LOCAL;
137 if (!((local ? 1 : 2) & rt_mode)) {
138 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
139 (rt->rt_flags & RTCF_LOCAL) ?
140 "local":"non-local", &rt->rt_dst);
144 if (local && !(rt_mode & 4) && !((ort = skb_rtable(skb)) &&
145 ort->rt_flags & RTCF_LOCAL)) {
146 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
147 "requires NAT method, dest: %pI4\n",
148 &ip_hdr(skb)->daddr, &rt->rt_dst);
152 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
153 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
154 "to non-local address, dest: %pI4\n",
155 &ip_hdr(skb)->saddr, &rt->rt_dst);
163 /* Reroute packet to local IPv4 stack after DNAT */
165 __ip_vs_reroute_locally(struct sk_buff *skb)
167 struct rtable *rt = skb_rtable(skb);
168 struct net_device *dev = rt->dst.dev;
169 struct net *net = dev_net(dev);
170 struct iphdr *iph = ip_hdr(skb);
172 if (rt_is_input_route(rt)) {
173 unsigned long orefdst = skb->_skb_refdst;
175 if (ip_route_input(skb, iph->daddr, iph->saddr,
178 refdst_drop(orefdst);
186 .tos = RT_TOS(iph->tos),
193 if (ip_route_output_key(net, &rt, &fl))
195 if (!(rt->rt_flags & RTCF_LOCAL)) {
199 /* Drop old route. */
201 skb_dst_set(skb, &rt->dst);
206 #ifdef CONFIG_IP_VS_IPV6
208 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
210 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
213 static struct dst_entry *
214 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
215 struct in6_addr *ret_saddr, int do_xfrm)
217 struct dst_entry *dst;
227 dst = ip6_route_output(net, NULL, &fl);
232 if (ipv6_addr_any(&fl.fl6_src) &&
233 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
234 &fl.fl6_dst, 0, &fl.fl6_src) < 0)
236 if (do_xfrm && xfrm_lookup(net, &dst, &fl, NULL, 0) < 0)
238 ipv6_addr_copy(ret_saddr, &fl.fl6_src);
243 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
248 * Get route to destination or remote server
249 * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest,
250 * &4=Allow redirect from remote daddr to local
252 static struct rt6_info *
253 __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
254 struct in6_addr *daddr, struct in6_addr *ret_saddr,
255 int do_xfrm, int rt_mode)
257 struct net *net = dev_net(skb_dst(skb)->dev);
258 struct rt6_info *rt; /* Route to the other host */
259 struct rt6_info *ort; /* Original route */
260 struct dst_entry *dst;
264 spin_lock(&dest->dst_lock);
265 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
269 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
273 spin_unlock(&dest->dst_lock);
276 rt = (struct rt6_info *) dst;
277 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
278 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
279 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
280 &dest->addr.in6, &dest->dst_saddr,
281 atomic_read(&rt->dst.__refcnt));
284 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
285 spin_unlock(&dest->dst_lock);
287 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
290 rt = (struct rt6_info *) dst;
293 local = __ip_vs_is_local_route6(rt);
294 if (!((local ? 1 : 2) & rt_mode)) {
295 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
296 local ? "local":"non-local", daddr);
297 dst_release(&rt->dst);
300 if (local && !(rt_mode & 4) &&
301 !((ort = (struct rt6_info *) skb_dst(skb)) &&
302 __ip_vs_is_local_route6(ort))) {
303 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
304 "requires NAT method, dest: %pI6\n",
305 &ipv6_hdr(skb)->daddr, daddr);
306 dst_release(&rt->dst);
309 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
310 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
311 IPV6_ADDR_LOOPBACK)) {
312 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
313 "to non-local address, dest: %pI6\n",
314 &ipv6_hdr(skb)->saddr, daddr);
315 dst_release(&rt->dst);
325 * Release dest->dst_cache before a dest is removed
328 ip_vs_dst_reset(struct ip_vs_dest *dest)
330 struct dst_entry *old_dst;
332 old_dst = dest->dst_cache;
333 dest->dst_cache = NULL;
334 dst_release(old_dst);
337 #define IP_VS_XMIT_TUNNEL(skb, cp) \
339 int __ret = NF_ACCEPT; \
341 (skb)->ipvs_property = 1; \
342 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
343 __ret = ip_vs_confirm_conntrack(skb, cp); \
344 if (__ret == NF_ACCEPT) { \
346 skb_forward_csum(skb); \
351 #define IP_VS_XMIT_NAT(pf, skb, cp, local) \
353 (skb)->ipvs_property = 1; \
354 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
355 ip_vs_notrack(skb); \
357 ip_vs_update_conntrack(skb, cp, 1); \
360 skb_forward_csum(skb); \
361 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
362 skb_dst(skb)->dev, dst_output); \
365 #define IP_VS_XMIT(pf, skb, cp, local) \
367 (skb)->ipvs_property = 1; \
368 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
369 ip_vs_notrack(skb); \
372 skb_forward_csum(skb); \
373 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
374 skb_dst(skb)->dev, dst_output); \
379 * NULL transmitter (do nothing except return NF_ACCEPT)
382 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
383 struct ip_vs_protocol *pp)
385 /* we do not touch skb and do not need pskb ptr */
386 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
392 * Let packets bypass the destination when the destination is not
393 * available, it may be only used in transparent cache cluster.
396 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
397 struct ip_vs_protocol *pp)
399 struct rtable *rt; /* Route to the other host */
400 struct iphdr *iph = ip_hdr(skb);
405 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr,
406 RT_TOS(iph->tos), 2)))
410 mtu = dst_mtu(&rt->dst);
411 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
413 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
414 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
419 * Call ip_send_check because we are not sure it is called
420 * after ip_defrag. Is copy-on-write needed?
422 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
426 ip_send_check(ip_hdr(skb));
430 skb_dst_set(skb, &rt->dst);
432 /* Another hack: avoid icmp_send in ip_fragment */
435 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
441 dst_link_failure(skb);
448 #ifdef CONFIG_IP_VS_IPV6
450 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
451 struct ip_vs_protocol *pp)
453 struct rt6_info *rt; /* Route to the other host */
454 struct ipv6hdr *iph = ipv6_hdr(skb);
459 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0, 2)))
463 mtu = dst_mtu(&rt->dst);
464 if (skb->len > mtu) {
466 struct net *net = dev_net(skb_dst(skb)->dev);
468 skb->dev = net->loopback_dev;
470 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
471 dst_release(&rt->dst);
472 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
477 * Call ip_send_check because we are not sure it is called
478 * after ip_defrag. Is copy-on-write needed?
480 skb = skb_share_check(skb, GFP_ATOMIC);
481 if (unlikely(skb == NULL)) {
482 dst_release(&rt->dst);
488 skb_dst_set(skb, &rt->dst);
490 /* Another hack: avoid icmp_send in ip_fragment */
493 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
499 dst_link_failure(skb);
508 * NAT transmitter (only for outside-to-inside nat forwarding)
509 * Not used for related ICMP
512 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
513 struct ip_vs_protocol *pp)
515 struct rtable *rt; /* Route to the other host */
517 struct iphdr *iph = ip_hdr(skb);
522 /* check if it is a connection of no-client-port */
523 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
525 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
528 ip_vs_conn_fill_cport(cp, *p);
529 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
532 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
533 RT_TOS(iph->tos), 1|2|4)))
535 local = rt->rt_flags & RTCF_LOCAL;
537 * Avoid duplicate tuple in reply direction for NAT traffic
538 * to local address when connection is sync-ed
540 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
541 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
542 enum ip_conntrack_info ctinfo;
543 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
545 if (ct && !nf_ct_is_untracked(ct)) {
546 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
548 "stopping DNAT to local address");
554 /* From world but DNAT to loopback address? */
555 if (local && ipv4_is_loopback(rt->rt_dst) &&
556 rt_is_input_route(skb_rtable(skb))) {
557 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
558 "stopping DNAT to loopback address");
563 mtu = dst_mtu(&rt->dst);
564 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
565 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
566 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
567 "ip_vs_nat_xmit(): frag needed for");
571 /* copy-on-write the packet before mangling it */
572 if (!skb_make_writable(skb, sizeof(struct iphdr)))
575 if (skb_cow(skb, rt->dst.dev->hard_header_len))
578 /* mangle the packet */
579 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
581 ip_hdr(skb)->daddr = cp->daddr.ip;
582 ip_send_check(ip_hdr(skb));
587 skb_dst_set(skb, &rt->dst);
591 * Some IPv4 replies get local address from routes,
592 * not from iph, so while we DNAT after routing
593 * we need this second input/output route.
595 if (!__ip_vs_reroute_locally(skb))
599 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
601 /* FIXME: when application helper enlarges the packet and the length
602 is larger than the MTU of outgoing device, there will be still
605 /* Another hack: avoid icmp_send in ip_fragment */
608 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
614 dst_link_failure(skb);
624 #ifdef CONFIG_IP_VS_IPV6
626 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
627 struct ip_vs_protocol *pp)
629 struct rt6_info *rt; /* Route to the other host */
635 /* check if it is a connection of no-client-port */
636 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
638 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
642 ip_vs_conn_fill_cport(cp, *p);
643 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
646 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
649 local = __ip_vs_is_local_route6(rt);
651 * Avoid duplicate tuple in reply direction for NAT traffic
652 * to local address when connection is sync-ed
654 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
655 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
656 enum ip_conntrack_info ctinfo;
657 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
659 if (ct && !nf_ct_is_untracked(ct)) {
660 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
661 "ip_vs_nat_xmit_v6(): "
662 "stopping DNAT to local address");
668 /* From world but DNAT to loopback address? */
669 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
670 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
671 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
672 "ip_vs_nat_xmit_v6(): "
673 "stopping DNAT to loopback address");
678 mtu = dst_mtu(&rt->dst);
679 if (skb->len > mtu) {
681 struct net *net = dev_net(skb_dst(skb)->dev);
683 skb->dev = net->loopback_dev;
685 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
686 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
687 "ip_vs_nat_xmit_v6(): frag needed for");
691 /* copy-on-write the packet before mangling it */
692 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
695 if (skb_cow(skb, rt->dst.dev->hard_header_len))
698 /* mangle the packet */
699 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
701 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
703 if (!local || !skb->dev) {
704 /* drop the old route when skb is not shared */
706 skb_dst_set(skb, &rt->dst);
708 /* destined to loopback, do we need to change route? */
709 dst_release(&rt->dst);
712 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
714 /* FIXME: when application helper enlarges the packet and the length
715 is larger than the MTU of outgoing device, there will be still
718 /* Another hack: avoid icmp_send in ip_fragment */
721 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
727 dst_link_failure(skb);
733 dst_release(&rt->dst);
740 * IP Tunneling transmitter
742 * This function encapsulates the packet in a new IP packet, its
743 * destination will be set to cp->daddr. Most code of this function
744 * is taken from ipip.c.
746 * It is used in VS/TUN cluster. The load balancer selects a real
747 * server from a cluster based on a scheduling algorithm,
748 * encapsulates the request packet and forwards it to the selected
749 * server. For example, all real servers are configured with
750 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
751 * the encapsulated packet, it will decapsulate the packet, processe
752 * the request and return the response packets directly to the client
753 * without passing the load balancer. This can greatly increase the
754 * scalability of virtual server.
756 * Used for ANY protocol
759 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
760 struct ip_vs_protocol *pp)
762 struct rtable *rt; /* Route to the other host */
763 struct net_device *tdev; /* Device to other host */
764 struct iphdr *old_iph = ip_hdr(skb);
765 u8 tos = old_iph->tos;
766 __be16 df = old_iph->frag_off;
767 struct iphdr *iph; /* Our new IP header */
768 unsigned int max_headroom; /* The extra header space needed */
774 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
777 if (rt->rt_flags & RTCF_LOCAL) {
779 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
784 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
786 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
790 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
792 df |= (old_iph->frag_off & htons(IP_DF));
794 if ((old_iph->frag_off & htons(IP_DF))
795 && mtu < ntohs(old_iph->tot_len)) {
796 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
797 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
802 * Okay, now see if we can stuff it in the buffer as-is.
804 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
806 if (skb_headroom(skb) < max_headroom
807 || skb_cloned(skb) || skb_shared(skb)) {
808 struct sk_buff *new_skb =
809 skb_realloc_headroom(skb, max_headroom);
813 IP_VS_ERR_RL("%s(): no memory\n", __func__);
818 old_iph = ip_hdr(skb);
821 skb->transport_header = skb->network_header;
823 /* fix old IP header checksum */
824 ip_send_check(old_iph);
826 skb_push(skb, sizeof(struct iphdr));
827 skb_reset_network_header(skb);
828 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
832 skb_dst_set(skb, &rt->dst);
835 * Push down and install the IPIP header.
839 iph->ihl = sizeof(struct iphdr)>>2;
841 iph->protocol = IPPROTO_IPIP;
843 iph->daddr = rt->rt_dst;
844 iph->saddr = rt->rt_src;
845 iph->ttl = old_iph->ttl;
846 ip_select_ident(iph, &rt->dst, NULL);
848 /* Another hack: avoid icmp_send in ip_fragment */
851 ret = IP_VS_XMIT_TUNNEL(skb, cp);
852 if (ret == NF_ACCEPT)
854 else if (ret == NF_DROP)
862 dst_link_failure(skb);
872 #ifdef CONFIG_IP_VS_IPV6
874 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
875 struct ip_vs_protocol *pp)
877 struct rt6_info *rt; /* Route to the other host */
878 struct in6_addr saddr; /* Source for tunnel */
879 struct net_device *tdev; /* Device to other host */
880 struct ipv6hdr *old_iph = ipv6_hdr(skb);
881 struct ipv6hdr *iph; /* Our new IP header */
882 unsigned int max_headroom; /* The extra header space needed */
888 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
891 if (__ip_vs_is_local_route6(rt)) {
892 dst_release(&rt->dst);
893 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
898 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
899 if (mtu < IPV6_MIN_MTU) {
900 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
905 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
907 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr)) {
909 struct net *net = dev_net(skb_dst(skb)->dev);
911 skb->dev = net->loopback_dev;
913 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
914 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
919 * Okay, now see if we can stuff it in the buffer as-is.
921 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
923 if (skb_headroom(skb) < max_headroom
924 || skb_cloned(skb) || skb_shared(skb)) {
925 struct sk_buff *new_skb =
926 skb_realloc_headroom(skb, max_headroom);
928 dst_release(&rt->dst);
930 IP_VS_ERR_RL("%s(): no memory\n", __func__);
935 old_iph = ipv6_hdr(skb);
938 skb->transport_header = skb->network_header;
940 skb_push(skb, sizeof(struct ipv6hdr));
941 skb_reset_network_header(skb);
942 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
946 skb_dst_set(skb, &rt->dst);
949 * Push down and install the IPIP header.
953 iph->nexthdr = IPPROTO_IPV6;
954 iph->payload_len = old_iph->payload_len;
955 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
956 iph->priority = old_iph->priority;
957 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
958 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
959 ipv6_addr_copy(&iph->saddr, &saddr);
960 iph->hop_limit = old_iph->hop_limit;
962 /* Another hack: avoid icmp_send in ip_fragment */
965 ret = IP_VS_XMIT_TUNNEL(skb, cp);
966 if (ret == NF_ACCEPT)
968 else if (ret == NF_DROP)
976 dst_link_failure(skb);
982 dst_release(&rt->dst);
989 * Direct Routing transmitter
990 * Used for ANY protocol
993 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
994 struct ip_vs_protocol *pp)
996 struct rtable *rt; /* Route to the other host */
997 struct iphdr *iph = ip_hdr(skb);
1002 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1003 RT_TOS(iph->tos), 1|2)))
1005 if (rt->rt_flags & RTCF_LOCAL) {
1007 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1011 mtu = dst_mtu(&rt->dst);
1012 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu) {
1013 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1015 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1020 * Call ip_send_check because we are not sure it is called
1021 * after ip_defrag. Is copy-on-write needed?
1023 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1027 ip_send_check(ip_hdr(skb));
1029 /* drop old route */
1031 skb_dst_set(skb, &rt->dst);
1033 /* Another hack: avoid icmp_send in ip_fragment */
1036 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1042 dst_link_failure(skb);
1049 #ifdef CONFIG_IP_VS_IPV6
1051 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1052 struct ip_vs_protocol *pp)
1054 struct rt6_info *rt; /* Route to the other host */
1059 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1062 if (__ip_vs_is_local_route6(rt)) {
1063 dst_release(&rt->dst);
1064 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1068 mtu = dst_mtu(&rt->dst);
1069 if (skb->len > mtu) {
1071 struct net *net = dev_net(skb_dst(skb)->dev);
1073 skb->dev = net->loopback_dev;
1075 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1076 dst_release(&rt->dst);
1077 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1082 * Call ip_send_check because we are not sure it is called
1083 * after ip_defrag. Is copy-on-write needed?
1085 skb = skb_share_check(skb, GFP_ATOMIC);
1086 if (unlikely(skb == NULL)) {
1087 dst_release(&rt->dst);
1091 /* drop old route */
1093 skb_dst_set(skb, &rt->dst);
1095 /* Another hack: avoid icmp_send in ip_fragment */
1098 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
1104 dst_link_failure(skb);
1114 * ICMP packet transmitter
1115 * called by the ip_vs_in_icmp
1118 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1119 struct ip_vs_protocol *pp, int offset)
1121 struct rtable *rt; /* Route to the other host */
1128 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1129 forwarded directly here, because there is no need to
1130 translate address/port back */
1131 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1132 if (cp->packet_xmit)
1133 rc = cp->packet_xmit(skb, cp, pp);
1136 /* do not touch skb anymore */
1137 atomic_inc(&cp->in_pkts);
1142 * mangle and send the packet here (only for VS/NAT)
1145 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1146 RT_TOS(ip_hdr(skb)->tos), 1|2|4)))
1148 local = rt->rt_flags & RTCF_LOCAL;
1151 * Avoid duplicate tuple in reply direction for NAT traffic
1152 * to local address when connection is sync-ed
1154 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1155 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1156 enum ip_conntrack_info ctinfo;
1157 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1159 if (ct && !nf_ct_is_untracked(ct)) {
1160 IP_VS_DBG(10, "%s(): "
1161 "stopping DNAT to local address %pI4\n",
1162 __func__, &cp->daddr.ip);
1168 /* From world but DNAT to loopback address? */
1169 if (local && ipv4_is_loopback(rt->rt_dst) &&
1170 rt_is_input_route(skb_rtable(skb))) {
1171 IP_VS_DBG(1, "%s(): "
1172 "stopping DNAT to loopback %pI4\n",
1173 __func__, &cp->daddr.ip);
1178 mtu = dst_mtu(&rt->dst);
1179 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF))) {
1180 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1181 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1185 /* copy-on-write the packet before mangling it */
1186 if (!skb_make_writable(skb, offset))
1189 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1192 ip_vs_nat_icmp(skb, pp, cp, 0);
1195 /* drop the old route when skb is not shared */
1197 skb_dst_set(skb, &rt->dst);
1201 * Some IPv4 replies get local address from routes,
1202 * not from iph, so while we DNAT after routing
1203 * we need this second input/output route.
1205 if (!__ip_vs_reroute_locally(skb))
1209 /* Another hack: avoid icmp_send in ip_fragment */
1212 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1218 dst_link_failure(skb);
1230 #ifdef CONFIG_IP_VS_IPV6
1232 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1233 struct ip_vs_protocol *pp, int offset)
1235 struct rt6_info *rt; /* Route to the other host */
1242 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1243 forwarded directly here, because there is no need to
1244 translate address/port back */
1245 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1246 if (cp->packet_xmit)
1247 rc = cp->packet_xmit(skb, cp, pp);
1250 /* do not touch skb anymore */
1251 atomic_inc(&cp->in_pkts);
1256 * mangle and send the packet here (only for VS/NAT)
1259 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1263 local = __ip_vs_is_local_route6(rt);
1265 * Avoid duplicate tuple in reply direction for NAT traffic
1266 * to local address when connection is sync-ed
1268 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1269 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1270 enum ip_conntrack_info ctinfo;
1271 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1273 if (ct && !nf_ct_is_untracked(ct)) {
1274 IP_VS_DBG(10, "%s(): "
1275 "stopping DNAT to local address %pI6\n",
1276 __func__, &cp->daddr.in6);
1282 /* From world but DNAT to loopback address? */
1283 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1284 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1285 IP_VS_DBG(1, "%s(): "
1286 "stopping DNAT to loopback %pI6\n",
1287 __func__, &cp->daddr.in6);
1292 mtu = dst_mtu(&rt->dst);
1293 if (skb->len > mtu) {
1295 struct net *net = dev_net(skb_dst(skb)->dev);
1297 skb->dev = net->loopback_dev;
1299 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1300 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1304 /* copy-on-write the packet before mangling it */
1305 if (!skb_make_writable(skb, offset))
1308 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1311 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1313 if (!local || !skb->dev) {
1314 /* drop the old route when skb is not shared */
1316 skb_dst_set(skb, &rt->dst);
1318 /* destined to loopback, do we need to change route? */
1319 dst_release(&rt->dst);
1322 /* Another hack: avoid icmp_send in ip_fragment */
1325 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
1331 dst_link_failure(skb);
1339 dst_release(&rt->dst);