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>
47 IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */
48 IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */
49 IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to
55 * Destination cache to speed up outgoing route lookup
58 __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst,
61 struct dst_entry *old_dst;
63 old_dst = dest->dst_cache;
64 dest->dst_cache = dst;
65 dest->dst_rtos = rtos;
66 dest->dst_cookie = dst_cookie;
70 static inline struct dst_entry *
71 __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
73 struct dst_entry *dst = dest->dst_cache;
77 if ((dst->obsolete || rtos != dest->dst_rtos) &&
78 dst->ops->check(dst, dest->dst_cookie) == NULL) {
79 dest->dst_cache = NULL;
87 /* Get route to destination or remote server */
88 static struct rtable *
89 __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
90 __be32 daddr, u32 rtos, int rt_mode, __be32 *ret_saddr)
92 struct net *net = dev_net(skb_dst(skb)->dev);
93 struct rtable *rt; /* Route to the other host */
94 struct rtable *ort; /* Original route */
98 spin_lock(&dest->dst_lock);
99 if (!(rt = (struct rtable *)
100 __ip_vs_dst_check(dest, rtos))) {
103 memset(&fl4, 0, sizeof(fl4));
104 fl4.daddr = dest->addr.ip;
105 fl4.flowi4_tos = rtos;
106 rt = ip_route_output_key(net, &fl4);
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 dest->dst_saddr.ip = fl4.saddr;
115 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d, "
117 &dest->addr.ip, &dest->dst_saddr.ip,
118 atomic_read(&rt->dst.__refcnt), rtos);
120 daddr = dest->addr.ip;
122 *ret_saddr = dest->dst_saddr.ip;
123 spin_unlock(&dest->dst_lock);
127 memset(&fl4, 0, sizeof(fl4));
129 fl4.flowi4_tos = rtos;
130 rt = ip_route_output_key(net, &fl4);
132 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
137 *ret_saddr = fl4.saddr;
140 local = rt->rt_flags & RTCF_LOCAL;
141 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
143 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
144 (rt->rt_flags & RTCF_LOCAL) ?
145 "local":"non-local", &daddr);
149 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
150 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
151 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
152 "requires NAT method, dest: %pI4\n",
153 &ip_hdr(skb)->daddr, &daddr);
157 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
158 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
159 "to non-local address, dest: %pI4\n",
160 &ip_hdr(skb)->saddr, &daddr);
168 /* Reroute packet to local IPv4 stack after DNAT */
170 __ip_vs_reroute_locally(struct sk_buff *skb)
172 struct rtable *rt = skb_rtable(skb);
173 struct net_device *dev = rt->dst.dev;
174 struct net *net = dev_net(dev);
175 struct iphdr *iph = ip_hdr(skb);
177 if (rt_is_input_route(rt)) {
178 unsigned long orefdst = skb->_skb_refdst;
180 if (ip_route_input(skb, iph->daddr, iph->saddr,
183 refdst_drop(orefdst);
185 struct flowi4 fl4 = {
188 .flowi4_tos = RT_TOS(iph->tos),
189 .flowi4_mark = skb->mark,
192 rt = ip_route_output_key(net, &fl4);
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;
218 struct flowi6 fl6 = {
222 dst = ip6_route_output(net, NULL, &fl6);
227 if (ipv6_addr_any(&fl6.saddr) &&
228 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
229 &fl6.daddr, 0, &fl6.saddr) < 0)
232 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
238 ipv6_addr_copy(ret_saddr, &fl6.saddr);
243 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
248 * Get route to destination or remote server
250 static struct rt6_info *
251 __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
252 struct in6_addr *daddr, struct in6_addr *ret_saddr,
253 int do_xfrm, int rt_mode)
255 struct net *net = dev_net(skb_dst(skb)->dev);
256 struct rt6_info *rt; /* Route to the other host */
257 struct rt6_info *ort; /* Original route */
258 struct dst_entry *dst;
262 spin_lock(&dest->dst_lock);
263 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
267 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
268 &dest->dst_saddr.in6,
271 spin_unlock(&dest->dst_lock);
274 rt = (struct rt6_info *) dst;
275 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
276 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
277 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
278 &dest->addr.in6, &dest->dst_saddr.in6,
279 atomic_read(&rt->dst.__refcnt));
282 ipv6_addr_copy(ret_saddr, &dest->dst_saddr.in6);
283 spin_unlock(&dest->dst_lock);
285 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
288 rt = (struct rt6_info *) dst;
291 local = __ip_vs_is_local_route6(rt);
292 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
294 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
295 local ? "local":"non-local", daddr);
296 dst_release(&rt->dst);
299 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
300 !((ort = (struct rt6_info *) skb_dst(skb)) &&
301 __ip_vs_is_local_route6(ort))) {
302 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
303 "requires NAT method, dest: %pI6\n",
304 &ipv6_hdr(skb)->daddr, daddr);
305 dst_release(&rt->dst);
308 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
309 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
310 IPV6_ADDR_LOOPBACK)) {
311 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
312 "to non-local address, dest: %pI6\n",
313 &ipv6_hdr(skb)->saddr, daddr);
314 dst_release(&rt->dst);
324 * Release dest->dst_cache before a dest is removed
327 ip_vs_dst_reset(struct ip_vs_dest *dest)
329 struct dst_entry *old_dst;
331 old_dst = dest->dst_cache;
332 dest->dst_cache = NULL;
333 dst_release(old_dst);
336 #define IP_VS_XMIT_TUNNEL(skb, cp) \
338 int __ret = NF_ACCEPT; \
340 (skb)->ipvs_property = 1; \
341 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
342 __ret = ip_vs_confirm_conntrack(skb, cp); \
343 if (__ret == NF_ACCEPT) { \
345 skb_forward_csum(skb); \
350 #define IP_VS_XMIT_NAT(pf, skb, cp, local) \
352 (skb)->ipvs_property = 1; \
353 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
354 ip_vs_notrack(skb); \
356 ip_vs_update_conntrack(skb, cp, 1); \
359 skb_forward_csum(skb); \
360 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
361 skb_dst(skb)->dev, dst_output); \
364 #define IP_VS_XMIT(pf, skb, cp, local) \
366 (skb)->ipvs_property = 1; \
367 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
368 ip_vs_notrack(skb); \
371 skb_forward_csum(skb); \
372 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
373 skb_dst(skb)->dev, dst_output); \
378 * NULL transmitter (do nothing except return NF_ACCEPT)
381 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
382 struct ip_vs_protocol *pp)
384 /* we do not touch skb and do not need pskb ptr */
385 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
391 * Let packets bypass the destination when the destination is not
392 * available, it may be only used in transparent cache cluster.
395 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
396 struct ip_vs_protocol *pp)
398 struct rtable *rt; /* Route to the other host */
399 struct iphdr *iph = ip_hdr(skb);
404 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
405 IP_VS_RT_MODE_NON_LOCAL, NULL)))
409 mtu = dst_mtu(&rt->dst);
410 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,
460 IP_VS_RT_MODE_NON_LOCAL)))
464 mtu = dst_mtu(&rt->dst);
465 if (skb->len > mtu && !skb_is_gso(skb)) {
467 struct net *net = dev_net(skb_dst(skb)->dev);
469 skb->dev = net->loopback_dev;
471 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
472 dst_release(&rt->dst);
473 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
478 * Call ip_send_check because we are not sure it is called
479 * after ip_defrag. Is copy-on-write needed?
481 skb = skb_share_check(skb, GFP_ATOMIC);
482 if (unlikely(skb == NULL)) {
483 dst_release(&rt->dst);
489 skb_dst_set(skb, &rt->dst);
491 /* Another hack: avoid icmp_send in ip_fragment */
494 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
500 dst_link_failure(skb);
509 * NAT transmitter (only for outside-to-inside nat forwarding)
510 * Not used for related ICMP
513 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
514 struct ip_vs_protocol *pp)
516 struct rtable *rt; /* Route to the other host */
518 struct iphdr *iph = ip_hdr(skb);
523 /* check if it is a connection of no-client-port */
524 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
526 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
529 ip_vs_conn_fill_cport(cp, *p);
530 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
533 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
535 IP_VS_RT_MODE_LOCAL |
536 IP_VS_RT_MODE_NON_LOCAL |
537 IP_VS_RT_MODE_RDR, NULL)))
539 local = rt->rt_flags & RTCF_LOCAL;
541 * Avoid duplicate tuple in reply direction for NAT traffic
542 * to local address when connection is sync-ed
544 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
545 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
546 enum ip_conntrack_info ctinfo;
547 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
549 if (ct && !nf_ct_is_untracked(ct)) {
550 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
552 "stopping DNAT to local address");
558 /* From world but DNAT to loopback address? */
559 if (local && ipv4_is_loopback(cp->daddr.ip) &&
560 rt_is_input_route(skb_rtable(skb))) {
561 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
562 "stopping DNAT to loopback address");
567 mtu = dst_mtu(&rt->dst);
568 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
570 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
571 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
572 "ip_vs_nat_xmit(): frag needed for");
576 /* copy-on-write the packet before mangling it */
577 if (!skb_make_writable(skb, sizeof(struct iphdr)))
580 if (skb_cow(skb, rt->dst.dev->hard_header_len))
583 /* mangle the packet */
584 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
586 ip_hdr(skb)->daddr = cp->daddr.ip;
587 ip_send_check(ip_hdr(skb));
592 skb_dst_set(skb, &rt->dst);
596 * Some IPv4 replies get local address from routes,
597 * not from iph, so while we DNAT after routing
598 * we need this second input/output route.
600 if (!__ip_vs_reroute_locally(skb))
604 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
606 /* FIXME: when application helper enlarges the packet and the length
607 is larger than the MTU of outgoing device, there will be still
610 /* Another hack: avoid icmp_send in ip_fragment */
613 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
619 dst_link_failure(skb);
629 #ifdef CONFIG_IP_VS_IPV6
631 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
632 struct ip_vs_protocol *pp)
634 struct rt6_info *rt; /* Route to the other host */
640 /* check if it is a connection of no-client-port */
641 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
643 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
647 ip_vs_conn_fill_cport(cp, *p);
648 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
651 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
652 0, (IP_VS_RT_MODE_LOCAL |
653 IP_VS_RT_MODE_NON_LOCAL |
654 IP_VS_RT_MODE_RDR))))
656 local = __ip_vs_is_local_route6(rt);
658 * Avoid duplicate tuple in reply direction for NAT traffic
659 * to local address when connection is sync-ed
661 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
662 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
663 enum ip_conntrack_info ctinfo;
664 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
666 if (ct && !nf_ct_is_untracked(ct)) {
667 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
668 "ip_vs_nat_xmit_v6(): "
669 "stopping DNAT to local address");
675 /* From world but DNAT to loopback address? */
676 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
677 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
678 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
679 "ip_vs_nat_xmit_v6(): "
680 "stopping DNAT to loopback address");
685 mtu = dst_mtu(&rt->dst);
686 if (skb->len > mtu && !skb_is_gso(skb)) {
688 struct net *net = dev_net(skb_dst(skb)->dev);
690 skb->dev = net->loopback_dev;
692 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
693 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
694 "ip_vs_nat_xmit_v6(): frag needed for");
698 /* copy-on-write the packet before mangling it */
699 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
702 if (skb_cow(skb, rt->dst.dev->hard_header_len))
705 /* mangle the packet */
706 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
708 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
710 if (!local || !skb->dev) {
711 /* drop the old route when skb is not shared */
713 skb_dst_set(skb, &rt->dst);
715 /* destined to loopback, do we need to change route? */
716 dst_release(&rt->dst);
719 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
721 /* FIXME: when application helper enlarges the packet and the length
722 is larger than the MTU of outgoing device, there will be still
725 /* Another hack: avoid icmp_send in ip_fragment */
728 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
734 dst_link_failure(skb);
740 dst_release(&rt->dst);
747 * IP Tunneling transmitter
749 * This function encapsulates the packet in a new IP packet, its
750 * destination will be set to cp->daddr. Most code of this function
751 * is taken from ipip.c.
753 * It is used in VS/TUN cluster. The load balancer selects a real
754 * server from a cluster based on a scheduling algorithm,
755 * encapsulates the request packet and forwards it to the selected
756 * server. For example, all real servers are configured with
757 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
758 * the encapsulated packet, it will decapsulate the packet, processe
759 * the request and return the response packets directly to the client
760 * without passing the load balancer. This can greatly increase the
761 * scalability of virtual server.
763 * Used for ANY protocol
766 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
767 struct ip_vs_protocol *pp)
769 struct rtable *rt; /* Route to the other host */
770 __be32 saddr; /* Source for tunnel */
771 struct net_device *tdev; /* Device to other host */
772 struct iphdr *old_iph = ip_hdr(skb);
773 u8 tos = old_iph->tos;
774 __be16 df = old_iph->frag_off;
775 struct iphdr *iph; /* Our new IP header */
776 unsigned int max_headroom; /* The extra header space needed */
782 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
783 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
784 IP_VS_RT_MODE_NON_LOCAL,
787 if (rt->rt_flags & RTCF_LOCAL) {
789 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
794 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
796 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
800 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
802 df |= (old_iph->frag_off & htons(IP_DF));
804 if ((old_iph->frag_off & htons(IP_DF) &&
805 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
806 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
807 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
812 * Okay, now see if we can stuff it in the buffer as-is.
814 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
816 if (skb_headroom(skb) < max_headroom
817 || skb_cloned(skb) || skb_shared(skb)) {
818 struct sk_buff *new_skb =
819 skb_realloc_headroom(skb, max_headroom);
823 IP_VS_ERR_RL("%s(): no memory\n", __func__);
828 old_iph = ip_hdr(skb);
831 skb->transport_header = skb->network_header;
833 /* fix old IP header checksum */
834 ip_send_check(old_iph);
836 skb_push(skb, sizeof(struct iphdr));
837 skb_reset_network_header(skb);
838 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
842 skb_dst_set(skb, &rt->dst);
845 * Push down and install the IPIP header.
849 iph->ihl = sizeof(struct iphdr)>>2;
851 iph->protocol = IPPROTO_IPIP;
853 iph->daddr = cp->daddr.ip;
855 iph->ttl = old_iph->ttl;
856 ip_select_ident(iph, &rt->dst, NULL);
858 /* Another hack: avoid icmp_send in ip_fragment */
861 ret = IP_VS_XMIT_TUNNEL(skb, cp);
862 if (ret == NF_ACCEPT)
864 else if (ret == NF_DROP)
872 dst_link_failure(skb);
882 #ifdef CONFIG_IP_VS_IPV6
884 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
885 struct ip_vs_protocol *pp)
887 struct rt6_info *rt; /* Route to the other host */
888 struct in6_addr saddr; /* Source for tunnel */
889 struct net_device *tdev; /* Device to other host */
890 struct ipv6hdr *old_iph = ipv6_hdr(skb);
891 struct ipv6hdr *iph; /* Our new IP header */
892 unsigned int max_headroom; /* The extra header space needed */
898 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
899 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
900 IP_VS_RT_MODE_NON_LOCAL))))
902 if (__ip_vs_is_local_route6(rt)) {
903 dst_release(&rt->dst);
904 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
909 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
910 if (mtu < IPV6_MIN_MTU) {
911 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
916 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
918 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
921 struct net *net = dev_net(skb_dst(skb)->dev);
923 skb->dev = net->loopback_dev;
925 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
926 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
931 * Okay, now see if we can stuff it in the buffer as-is.
933 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
935 if (skb_headroom(skb) < max_headroom
936 || skb_cloned(skb) || skb_shared(skb)) {
937 struct sk_buff *new_skb =
938 skb_realloc_headroom(skb, max_headroom);
940 dst_release(&rt->dst);
942 IP_VS_ERR_RL("%s(): no memory\n", __func__);
947 old_iph = ipv6_hdr(skb);
950 skb->transport_header = skb->network_header;
952 skb_push(skb, sizeof(struct ipv6hdr));
953 skb_reset_network_header(skb);
954 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
958 skb_dst_set(skb, &rt->dst);
961 * Push down and install the IPIP header.
965 iph->nexthdr = IPPROTO_IPV6;
966 iph->payload_len = old_iph->payload_len;
967 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
968 iph->priority = old_iph->priority;
969 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
970 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
971 ipv6_addr_copy(&iph->saddr, &saddr);
972 iph->hop_limit = old_iph->hop_limit;
974 /* Another hack: avoid icmp_send in ip_fragment */
977 ret = IP_VS_XMIT_TUNNEL(skb, cp);
978 if (ret == NF_ACCEPT)
980 else if (ret == NF_DROP)
988 dst_link_failure(skb);
994 dst_release(&rt->dst);
1001 * Direct Routing transmitter
1002 * Used for ANY protocol
1005 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1006 struct ip_vs_protocol *pp)
1008 struct rtable *rt; /* Route to the other host */
1009 struct iphdr *iph = ip_hdr(skb);
1014 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1016 IP_VS_RT_MODE_LOCAL |
1017 IP_VS_RT_MODE_NON_LOCAL, NULL)))
1019 if (rt->rt_flags & RTCF_LOCAL) {
1021 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1025 mtu = dst_mtu(&rt->dst);
1026 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1028 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1030 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1035 * Call ip_send_check because we are not sure it is called
1036 * after ip_defrag. Is copy-on-write needed?
1038 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1042 ip_send_check(ip_hdr(skb));
1044 /* drop old route */
1046 skb_dst_set(skb, &rt->dst);
1048 /* Another hack: avoid icmp_send in ip_fragment */
1051 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1057 dst_link_failure(skb);
1064 #ifdef CONFIG_IP_VS_IPV6
1066 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1067 struct ip_vs_protocol *pp)
1069 struct rt6_info *rt; /* Route to the other host */
1074 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1075 0, (IP_VS_RT_MODE_LOCAL |
1076 IP_VS_RT_MODE_NON_LOCAL))))
1078 if (__ip_vs_is_local_route6(rt)) {
1079 dst_release(&rt->dst);
1080 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1084 mtu = dst_mtu(&rt->dst);
1085 if (skb->len > mtu) {
1087 struct net *net = dev_net(skb_dst(skb)->dev);
1089 skb->dev = net->loopback_dev;
1091 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1092 dst_release(&rt->dst);
1093 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1098 * Call ip_send_check because we are not sure it is called
1099 * after ip_defrag. Is copy-on-write needed?
1101 skb = skb_share_check(skb, GFP_ATOMIC);
1102 if (unlikely(skb == NULL)) {
1103 dst_release(&rt->dst);
1107 /* drop old route */
1109 skb_dst_set(skb, &rt->dst);
1111 /* Another hack: avoid icmp_send in ip_fragment */
1114 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
1120 dst_link_failure(skb);
1130 * ICMP packet transmitter
1131 * called by the ip_vs_in_icmp
1134 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1135 struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
1137 struct rtable *rt; /* Route to the other host */
1145 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1146 forwarded directly here, because there is no need to
1147 translate address/port back */
1148 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1149 if (cp->packet_xmit)
1150 rc = cp->packet_xmit(skb, cp, pp);
1153 /* do not touch skb anymore */
1154 atomic_inc(&cp->in_pkts);
1159 * mangle and send the packet here (only for VS/NAT)
1162 /* LOCALNODE from FORWARD hook is not supported */
1163 rt_mode = (hooknum != NF_INET_FORWARD) ?
1164 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1165 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1166 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1167 RT_TOS(ip_hdr(skb)->tos),
1170 local = rt->rt_flags & RTCF_LOCAL;
1173 * Avoid duplicate tuple in reply direction for NAT traffic
1174 * to local address when connection is sync-ed
1176 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1177 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1178 enum ip_conntrack_info ctinfo;
1179 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1181 if (ct && !nf_ct_is_untracked(ct)) {
1182 IP_VS_DBG(10, "%s(): "
1183 "stopping DNAT to local address %pI4\n",
1184 __func__, &cp->daddr.ip);
1190 /* From world but DNAT to loopback address? */
1191 if (local && ipv4_is_loopback(cp->daddr.ip) &&
1192 rt_is_input_route(skb_rtable(skb))) {
1193 IP_VS_DBG(1, "%s(): "
1194 "stopping DNAT to loopback %pI4\n",
1195 __func__, &cp->daddr.ip);
1200 mtu = dst_mtu(&rt->dst);
1201 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1203 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1204 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1208 /* copy-on-write the packet before mangling it */
1209 if (!skb_make_writable(skb, offset))
1212 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1215 ip_vs_nat_icmp(skb, pp, cp, 0);
1218 /* drop the old route when skb is not shared */
1220 skb_dst_set(skb, &rt->dst);
1224 * Some IPv4 replies get local address from routes,
1225 * not from iph, so while we DNAT after routing
1226 * we need this second input/output route.
1228 if (!__ip_vs_reroute_locally(skb))
1232 /* Another hack: avoid icmp_send in ip_fragment */
1235 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1241 dst_link_failure(skb);
1253 #ifdef CONFIG_IP_VS_IPV6
1255 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1256 struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
1258 struct rt6_info *rt; /* Route to the other host */
1266 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1267 forwarded directly here, because there is no need to
1268 translate address/port back */
1269 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1270 if (cp->packet_xmit)
1271 rc = cp->packet_xmit(skb, cp, pp);
1274 /* do not touch skb anymore */
1275 atomic_inc(&cp->in_pkts);
1280 * mangle and send the packet here (only for VS/NAT)
1283 /* LOCALNODE from FORWARD hook is not supported */
1284 rt_mode = (hooknum != NF_INET_FORWARD) ?
1285 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1286 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1287 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1291 local = __ip_vs_is_local_route6(rt);
1293 * Avoid duplicate tuple in reply direction for NAT traffic
1294 * to local address when connection is sync-ed
1296 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1297 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1298 enum ip_conntrack_info ctinfo;
1299 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1301 if (ct && !nf_ct_is_untracked(ct)) {
1302 IP_VS_DBG(10, "%s(): "
1303 "stopping DNAT to local address %pI6\n",
1304 __func__, &cp->daddr.in6);
1310 /* From world but DNAT to loopback address? */
1311 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1312 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1313 IP_VS_DBG(1, "%s(): "
1314 "stopping DNAT to loopback %pI6\n",
1315 __func__, &cp->daddr.in6);
1320 mtu = dst_mtu(&rt->dst);
1321 if (skb->len > mtu && !skb_is_gso(skb)) {
1323 struct net *net = dev_net(skb_dst(skb)->dev);
1325 skb->dev = net->loopback_dev;
1327 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1328 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1332 /* copy-on-write the packet before mangling it */
1333 if (!skb_make_writable(skb, offset))
1336 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1339 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1341 if (!local || !skb->dev) {
1342 /* drop the old route when skb is not shared */
1344 skb_dst_set(skb, &rt->dst);
1346 /* destined to loopback, do we need to change route? */
1347 dst_release(&rt->dst);
1350 /* Another hack: avoid icmp_send in ip_fragment */
1353 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
1359 dst_link_failure(skb);
1367 dst_release(&rt->dst);