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))) {
99 .fl4_dst = dest->addr.ip,
103 if (ip_route_output_key(net, &rt, &fl)) {
104 spin_unlock(&dest->dst_lock);
105 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
109 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
110 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
112 atomic_read(&rt->dst.__refcnt), rtos);
114 spin_unlock(&dest->dst_lock);
121 if (ip_route_output_key(net, &rt, &fl)) {
122 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
128 local = rt->rt_flags & RTCF_LOCAL;
129 if (!((local ? 1 : 2) & rt_mode)) {
130 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
131 (rt->rt_flags & RTCF_LOCAL) ?
132 "local":"non-local", &rt->rt_dst);
136 if (local && !(rt_mode & 4) && !((ort = skb_rtable(skb)) &&
137 ort->rt_flags & RTCF_LOCAL)) {
138 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
139 "requires NAT method, dest: %pI4\n",
140 &ip_hdr(skb)->daddr, &rt->rt_dst);
144 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
145 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
146 "to non-local address, dest: %pI4\n",
147 &ip_hdr(skb)->saddr, &rt->rt_dst);
155 /* Reroute packet to local IPv4 stack after DNAT */
157 __ip_vs_reroute_locally(struct sk_buff *skb)
159 struct rtable *rt = skb_rtable(skb);
160 struct net_device *dev = rt->dst.dev;
161 struct net *net = dev_net(dev);
162 struct iphdr *iph = ip_hdr(skb);
164 if (rt_is_input_route(rt)) {
165 unsigned long orefdst = skb->_skb_refdst;
167 if (ip_route_input(skb, iph->daddr, iph->saddr,
170 refdst_drop(orefdst);
173 .fl4_dst = iph->daddr,
174 .fl4_src = iph->saddr,
175 .fl4_tos = RT_TOS(iph->tos),
180 if (ip_route_output_key(net, &rt, &fl))
182 if (!(rt->rt_flags & RTCF_LOCAL)) {
186 /* Drop old route. */
188 skb_dst_set(skb, &rt->dst);
193 #ifdef CONFIG_IP_VS_IPV6
195 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
197 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
200 static struct dst_entry *
201 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
202 struct in6_addr *ret_saddr, int do_xfrm)
204 struct dst_entry *dst;
209 dst = ip6_route_output(net, NULL, &fl);
214 if (ipv6_addr_any(&fl.fl6_src) &&
215 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
216 &fl.fl6_dst, 0, &fl.fl6_src) < 0)
218 if (do_xfrm && xfrm_lookup(net, &dst, &fl, NULL, 0) < 0)
220 ipv6_addr_copy(ret_saddr, &fl.fl6_src);
225 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
230 * Get route to destination or remote server
231 * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest,
232 * &4=Allow redirect from remote daddr to local
234 static struct rt6_info *
235 __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
236 struct in6_addr *daddr, struct in6_addr *ret_saddr,
237 int do_xfrm, int rt_mode)
239 struct net *net = dev_net(skb_dst(skb)->dev);
240 struct rt6_info *rt; /* Route to the other host */
241 struct rt6_info *ort; /* Original route */
242 struct dst_entry *dst;
246 spin_lock(&dest->dst_lock);
247 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
251 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
255 spin_unlock(&dest->dst_lock);
258 rt = (struct rt6_info *) dst;
259 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
260 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
261 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
262 &dest->addr.in6, &dest->dst_saddr,
263 atomic_read(&rt->dst.__refcnt));
266 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
267 spin_unlock(&dest->dst_lock);
269 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
272 rt = (struct rt6_info *) dst;
275 local = __ip_vs_is_local_route6(rt);
276 if (!((local ? 1 : 2) & rt_mode)) {
277 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
278 local ? "local":"non-local", daddr);
279 dst_release(&rt->dst);
282 if (local && !(rt_mode & 4) &&
283 !((ort = (struct rt6_info *) skb_dst(skb)) &&
284 __ip_vs_is_local_route6(ort))) {
285 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
286 "requires NAT method, dest: %pI6\n",
287 &ipv6_hdr(skb)->daddr, daddr);
288 dst_release(&rt->dst);
291 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
292 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
293 IPV6_ADDR_LOOPBACK)) {
294 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
295 "to non-local address, dest: %pI6\n",
296 &ipv6_hdr(skb)->saddr, daddr);
297 dst_release(&rt->dst);
307 * Release dest->dst_cache before a dest is removed
310 ip_vs_dst_reset(struct ip_vs_dest *dest)
312 struct dst_entry *old_dst;
314 old_dst = dest->dst_cache;
315 dest->dst_cache = NULL;
316 dst_release(old_dst);
319 #define IP_VS_XMIT_TUNNEL(skb, cp) \
321 int __ret = NF_ACCEPT; \
323 (skb)->ipvs_property = 1; \
324 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
325 __ret = ip_vs_confirm_conntrack(skb, cp); \
326 if (__ret == NF_ACCEPT) { \
328 skb_forward_csum(skb); \
333 #define IP_VS_XMIT_NAT(pf, skb, cp, local) \
335 (skb)->ipvs_property = 1; \
336 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
337 ip_vs_notrack(skb); \
339 ip_vs_update_conntrack(skb, cp, 1); \
342 skb_forward_csum(skb); \
343 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
344 skb_dst(skb)->dev, dst_output); \
347 #define IP_VS_XMIT(pf, skb, cp, local) \
349 (skb)->ipvs_property = 1; \
350 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
351 ip_vs_notrack(skb); \
354 skb_forward_csum(skb); \
355 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
356 skb_dst(skb)->dev, dst_output); \
361 * NULL transmitter (do nothing except return NF_ACCEPT)
364 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
365 struct ip_vs_protocol *pp)
367 /* we do not touch skb and do not need pskb ptr */
368 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
374 * Let packets bypass the destination when the destination is not
375 * available, it may be only used in transparent cache cluster.
378 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
379 struct ip_vs_protocol *pp)
381 struct rtable *rt; /* Route to the other host */
382 struct iphdr *iph = ip_hdr(skb);
387 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr,
388 RT_TOS(iph->tos), 2)))
392 mtu = dst_mtu(&rt->dst);
393 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
395 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
396 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
401 * Call ip_send_check because we are not sure it is called
402 * after ip_defrag. Is copy-on-write needed?
404 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
408 ip_send_check(ip_hdr(skb));
412 skb_dst_set(skb, &rt->dst);
414 /* Another hack: avoid icmp_send in ip_fragment */
417 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
423 dst_link_failure(skb);
430 #ifdef CONFIG_IP_VS_IPV6
432 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
433 struct ip_vs_protocol *pp)
435 struct rt6_info *rt; /* Route to the other host */
436 struct ipv6hdr *iph = ipv6_hdr(skb);
441 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0, 2)))
445 mtu = dst_mtu(&rt->dst);
446 if (skb->len > mtu) {
448 struct net *net = dev_net(skb_dst(skb)->dev);
450 skb->dev = net->loopback_dev;
452 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
453 dst_release(&rt->dst);
454 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
459 * Call ip_send_check because we are not sure it is called
460 * after ip_defrag. Is copy-on-write needed?
462 skb = skb_share_check(skb, GFP_ATOMIC);
463 if (unlikely(skb == NULL)) {
464 dst_release(&rt->dst);
470 skb_dst_set(skb, &rt->dst);
472 /* Another hack: avoid icmp_send in ip_fragment */
475 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
481 dst_link_failure(skb);
490 * NAT transmitter (only for outside-to-inside nat forwarding)
491 * Not used for related ICMP
494 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
495 struct ip_vs_protocol *pp)
497 struct rtable *rt; /* Route to the other host */
499 struct iphdr *iph = ip_hdr(skb);
504 /* check if it is a connection of no-client-port */
505 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
507 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
510 ip_vs_conn_fill_cport(cp, *p);
511 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
514 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
515 RT_TOS(iph->tos), 1|2|4)))
517 local = rt->rt_flags & RTCF_LOCAL;
519 * Avoid duplicate tuple in reply direction for NAT traffic
520 * to local address when connection is sync-ed
522 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
523 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
524 enum ip_conntrack_info ctinfo;
525 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
527 if (ct && !nf_ct_is_untracked(ct)) {
528 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
530 "stopping DNAT to local address");
536 /* From world but DNAT to loopback address? */
537 if (local && ipv4_is_loopback(rt->rt_dst) &&
538 rt_is_input_route(skb_rtable(skb))) {
539 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
540 "stopping DNAT to loopback address");
545 mtu = dst_mtu(&rt->dst);
546 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF))) {
547 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
548 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
549 "ip_vs_nat_xmit(): frag needed for");
553 /* copy-on-write the packet before mangling it */
554 if (!skb_make_writable(skb, sizeof(struct iphdr)))
557 if (skb_cow(skb, rt->dst.dev->hard_header_len))
560 /* mangle the packet */
561 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
563 ip_hdr(skb)->daddr = cp->daddr.ip;
564 ip_send_check(ip_hdr(skb));
569 skb_dst_set(skb, &rt->dst);
573 * Some IPv4 replies get local address from routes,
574 * not from iph, so while we DNAT after routing
575 * we need this second input/output route.
577 if (!__ip_vs_reroute_locally(skb))
581 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
583 /* FIXME: when application helper enlarges the packet and the length
584 is larger than the MTU of outgoing device, there will be still
587 /* Another hack: avoid icmp_send in ip_fragment */
590 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
596 dst_link_failure(skb);
606 #ifdef CONFIG_IP_VS_IPV6
608 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
609 struct ip_vs_protocol *pp)
611 struct rt6_info *rt; /* Route to the other host */
617 /* check if it is a connection of no-client-port */
618 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
620 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
624 ip_vs_conn_fill_cport(cp, *p);
625 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
628 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
631 local = __ip_vs_is_local_route6(rt);
633 * Avoid duplicate tuple in reply direction for NAT traffic
634 * to local address when connection is sync-ed
636 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
637 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
638 enum ip_conntrack_info ctinfo;
639 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
641 if (ct && !nf_ct_is_untracked(ct)) {
642 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
643 "ip_vs_nat_xmit_v6(): "
644 "stopping DNAT to local address");
650 /* From world but DNAT to loopback address? */
651 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
652 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
653 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
654 "ip_vs_nat_xmit_v6(): "
655 "stopping DNAT to loopback address");
660 mtu = dst_mtu(&rt->dst);
661 if (skb->len > mtu) {
663 struct net *net = dev_net(skb_dst(skb)->dev);
665 skb->dev = net->loopback_dev;
667 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
668 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
669 "ip_vs_nat_xmit_v6(): frag needed for");
673 /* copy-on-write the packet before mangling it */
674 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
677 if (skb_cow(skb, rt->dst.dev->hard_header_len))
680 /* mangle the packet */
681 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
683 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
685 if (!local || !skb->dev) {
686 /* drop the old route when skb is not shared */
688 skb_dst_set(skb, &rt->dst);
690 /* destined to loopback, do we need to change route? */
691 dst_release(&rt->dst);
694 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
696 /* FIXME: when application helper enlarges the packet and the length
697 is larger than the MTU of outgoing device, there will be still
700 /* Another hack: avoid icmp_send in ip_fragment */
703 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
709 dst_link_failure(skb);
715 dst_release(&rt->dst);
722 * IP Tunneling transmitter
724 * This function encapsulates the packet in a new IP packet, its
725 * destination will be set to cp->daddr. Most code of this function
726 * is taken from ipip.c.
728 * It is used in VS/TUN cluster. The load balancer selects a real
729 * server from a cluster based on a scheduling algorithm,
730 * encapsulates the request packet and forwards it to the selected
731 * server. For example, all real servers are configured with
732 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
733 * the encapsulated packet, it will decapsulate the packet, processe
734 * the request and return the response packets directly to the client
735 * without passing the load balancer. This can greatly increase the
736 * scalability of virtual server.
738 * Used for ANY protocol
741 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
742 struct ip_vs_protocol *pp)
744 struct rtable *rt; /* Route to the other host */
745 struct net_device *tdev; /* Device to other host */
746 struct iphdr *old_iph = ip_hdr(skb);
747 u8 tos = old_iph->tos;
748 __be16 df = old_iph->frag_off;
749 struct iphdr *iph; /* Our new IP header */
750 unsigned int max_headroom; /* The extra header space needed */
756 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
759 if (rt->rt_flags & RTCF_LOCAL) {
761 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
766 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
768 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
772 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
774 df |= (old_iph->frag_off & htons(IP_DF));
776 if ((old_iph->frag_off & htons(IP_DF))
777 && mtu < ntohs(old_iph->tot_len)) {
778 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
779 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
784 * Okay, now see if we can stuff it in the buffer as-is.
786 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
788 if (skb_headroom(skb) < max_headroom
789 || skb_cloned(skb) || skb_shared(skb)) {
790 struct sk_buff *new_skb =
791 skb_realloc_headroom(skb, max_headroom);
795 IP_VS_ERR_RL("%s(): no memory\n", __func__);
800 old_iph = ip_hdr(skb);
803 skb->transport_header = skb->network_header;
805 /* fix old IP header checksum */
806 ip_send_check(old_iph);
808 skb_push(skb, sizeof(struct iphdr));
809 skb_reset_network_header(skb);
810 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
814 skb_dst_set(skb, &rt->dst);
817 * Push down and install the IPIP header.
821 iph->ihl = sizeof(struct iphdr)>>2;
823 iph->protocol = IPPROTO_IPIP;
825 iph->daddr = rt->rt_dst;
826 iph->saddr = rt->rt_src;
827 iph->ttl = old_iph->ttl;
828 ip_select_ident(iph, &rt->dst, NULL);
830 /* Another hack: avoid icmp_send in ip_fragment */
833 ret = IP_VS_XMIT_TUNNEL(skb, cp);
834 if (ret == NF_ACCEPT)
836 else if (ret == NF_DROP)
844 dst_link_failure(skb);
854 #ifdef CONFIG_IP_VS_IPV6
856 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
857 struct ip_vs_protocol *pp)
859 struct rt6_info *rt; /* Route to the other host */
860 struct in6_addr saddr; /* Source for tunnel */
861 struct net_device *tdev; /* Device to other host */
862 struct ipv6hdr *old_iph = ipv6_hdr(skb);
863 struct ipv6hdr *iph; /* Our new IP header */
864 unsigned int max_headroom; /* The extra header space needed */
870 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
873 if (__ip_vs_is_local_route6(rt)) {
874 dst_release(&rt->dst);
875 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
880 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
881 if (mtu < IPV6_MIN_MTU) {
882 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
887 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
889 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr)) {
891 struct net *net = dev_net(skb_dst(skb)->dev);
893 skb->dev = net->loopback_dev;
895 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
896 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
901 * Okay, now see if we can stuff it in the buffer as-is.
903 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
905 if (skb_headroom(skb) < max_headroom
906 || skb_cloned(skb) || skb_shared(skb)) {
907 struct sk_buff *new_skb =
908 skb_realloc_headroom(skb, max_headroom);
910 dst_release(&rt->dst);
912 IP_VS_ERR_RL("%s(): no memory\n", __func__);
917 old_iph = ipv6_hdr(skb);
920 skb->transport_header = skb->network_header;
922 skb_push(skb, sizeof(struct ipv6hdr));
923 skb_reset_network_header(skb);
924 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
928 skb_dst_set(skb, &rt->dst);
931 * Push down and install the IPIP header.
935 iph->nexthdr = IPPROTO_IPV6;
936 iph->payload_len = old_iph->payload_len;
937 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
938 iph->priority = old_iph->priority;
939 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
940 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
941 ipv6_addr_copy(&iph->saddr, &saddr);
942 iph->hop_limit = old_iph->hop_limit;
944 /* Another hack: avoid icmp_send in ip_fragment */
947 ret = IP_VS_XMIT_TUNNEL(skb, cp);
948 if (ret == NF_ACCEPT)
950 else if (ret == NF_DROP)
958 dst_link_failure(skb);
964 dst_release(&rt->dst);
971 * Direct Routing transmitter
972 * Used for ANY protocol
975 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
976 struct ip_vs_protocol *pp)
978 struct rtable *rt; /* Route to the other host */
979 struct iphdr *iph = ip_hdr(skb);
984 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
985 RT_TOS(iph->tos), 1|2)))
987 if (rt->rt_flags & RTCF_LOCAL) {
989 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
993 mtu = dst_mtu(&rt->dst);
994 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu) {
995 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
997 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1002 * Call ip_send_check because we are not sure it is called
1003 * after ip_defrag. Is copy-on-write needed?
1005 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1009 ip_send_check(ip_hdr(skb));
1011 /* drop old route */
1013 skb_dst_set(skb, &rt->dst);
1015 /* Another hack: avoid icmp_send in ip_fragment */
1018 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1024 dst_link_failure(skb);
1031 #ifdef CONFIG_IP_VS_IPV6
1033 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1034 struct ip_vs_protocol *pp)
1036 struct rt6_info *rt; /* Route to the other host */
1041 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1044 if (__ip_vs_is_local_route6(rt)) {
1045 dst_release(&rt->dst);
1046 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1050 mtu = dst_mtu(&rt->dst);
1051 if (skb->len > mtu) {
1053 struct net *net = dev_net(skb_dst(skb)->dev);
1055 skb->dev = net->loopback_dev;
1057 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1058 dst_release(&rt->dst);
1059 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1064 * Call ip_send_check because we are not sure it is called
1065 * after ip_defrag. Is copy-on-write needed?
1067 skb = skb_share_check(skb, GFP_ATOMIC);
1068 if (unlikely(skb == NULL)) {
1069 dst_release(&rt->dst);
1073 /* drop old route */
1075 skb_dst_set(skb, &rt->dst);
1077 /* Another hack: avoid icmp_send in ip_fragment */
1080 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
1086 dst_link_failure(skb);
1096 * ICMP packet transmitter
1097 * called by the ip_vs_in_icmp
1100 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1101 struct ip_vs_protocol *pp, int offset)
1103 struct rtable *rt; /* Route to the other host */
1110 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1111 forwarded directly here, because there is no need to
1112 translate address/port back */
1113 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1114 if (cp->packet_xmit)
1115 rc = cp->packet_xmit(skb, cp, pp);
1118 /* do not touch skb anymore */
1119 atomic_inc(&cp->in_pkts);
1124 * mangle and send the packet here (only for VS/NAT)
1127 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1128 RT_TOS(ip_hdr(skb)->tos), 1|2|4)))
1130 local = rt->rt_flags & RTCF_LOCAL;
1133 * Avoid duplicate tuple in reply direction for NAT traffic
1134 * to local address when connection is sync-ed
1136 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1137 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1138 enum ip_conntrack_info ctinfo;
1139 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1141 if (ct && !nf_ct_is_untracked(ct)) {
1142 IP_VS_DBG(10, "%s(): "
1143 "stopping DNAT to local address %pI4\n",
1144 __func__, &cp->daddr.ip);
1150 /* From world but DNAT to loopback address? */
1151 if (local && ipv4_is_loopback(rt->rt_dst) &&
1152 rt_is_input_route(skb_rtable(skb))) {
1153 IP_VS_DBG(1, "%s(): "
1154 "stopping DNAT to loopback %pI4\n",
1155 __func__, &cp->daddr.ip);
1160 mtu = dst_mtu(&rt->dst);
1161 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF))) {
1162 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1163 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1167 /* copy-on-write the packet before mangling it */
1168 if (!skb_make_writable(skb, offset))
1171 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1174 ip_vs_nat_icmp(skb, pp, cp, 0);
1177 /* drop the old route when skb is not shared */
1179 skb_dst_set(skb, &rt->dst);
1183 * Some IPv4 replies get local address from routes,
1184 * not from iph, so while we DNAT after routing
1185 * we need this second input/output route.
1187 if (!__ip_vs_reroute_locally(skb))
1191 /* Another hack: avoid icmp_send in ip_fragment */
1194 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1200 dst_link_failure(skb);
1212 #ifdef CONFIG_IP_VS_IPV6
1214 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1215 struct ip_vs_protocol *pp, int offset)
1217 struct rt6_info *rt; /* Route to the other host */
1224 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1225 forwarded directly here, because there is no need to
1226 translate address/port back */
1227 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1228 if (cp->packet_xmit)
1229 rc = cp->packet_xmit(skb, cp, pp);
1232 /* do not touch skb anymore */
1233 atomic_inc(&cp->in_pkts);
1238 * mangle and send the packet here (only for VS/NAT)
1241 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1245 local = __ip_vs_is_local_route6(rt);
1247 * Avoid duplicate tuple in reply direction for NAT traffic
1248 * to local address when connection is sync-ed
1250 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1251 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1252 enum ip_conntrack_info ctinfo;
1253 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1255 if (ct && !nf_ct_is_untracked(ct)) {
1256 IP_VS_DBG(10, "%s(): "
1257 "stopping DNAT to local address %pI6\n",
1258 __func__, &cp->daddr.in6);
1264 /* From world but DNAT to loopback address? */
1265 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1266 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1267 IP_VS_DBG(1, "%s(): "
1268 "stopping DNAT to loopback %pI6\n",
1269 __func__, &cp->daddr.in6);
1274 mtu = dst_mtu(&rt->dst);
1275 if (skb->len > mtu) {
1277 struct net *net = dev_net(skb_dst(skb)->dev);
1279 skb->dev = net->loopback_dev;
1281 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1282 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1286 /* copy-on-write the packet before mangling it */
1287 if (!skb_make_writable(skb, offset))
1290 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1293 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1295 if (!local || !skb->dev) {
1296 /* drop the old route when skb is not shared */
1298 skb_dst_set(skb, &rt->dst);
1300 /* destined to loopback, do we need to change route? */
1301 dst_release(&rt->dst);
1304 /* Another hack: avoid icmp_send in ip_fragment */
1307 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
1313 dst_link_failure(skb);
1321 dst_release(&rt->dst);