6132b213eddc369770de650680e238232e9b924b
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
)
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
))) {
101 rt
= ip_route_output(net
, dest
->addr
.ip
, 0, rtos
, 0);
103 spin_unlock(&dest
->dst_lock
);
104 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
108 __ip_vs_dst_set(dest
, rtos
, dst_clone(&rt
->dst
), 0);
109 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
111 atomic_read(&rt
->dst
.__refcnt
), rtos
);
113 spin_unlock(&dest
->dst_lock
);
115 rt
= ip_route_output(net
, daddr
, 0, rtos
, 0);
117 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
123 local
= rt
->rt_flags
& RTCF_LOCAL
;
124 if (!((local
? IP_VS_RT_MODE_LOCAL
: IP_VS_RT_MODE_NON_LOCAL
) &
126 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
127 (rt
->rt_flags
& RTCF_LOCAL
) ?
128 "local":"non-local", &rt
->rt_dst
);
132 if (local
&& !(rt_mode
& IP_VS_RT_MODE_RDR
) &&
133 !((ort
= skb_rtable(skb
)) && ort
->rt_flags
& RTCF_LOCAL
)) {
134 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
135 "requires NAT method, dest: %pI4\n",
136 &ip_hdr(skb
)->daddr
, &rt
->rt_dst
);
140 if (unlikely(!local
&& ipv4_is_loopback(ip_hdr(skb
)->saddr
))) {
141 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
142 "to non-local address, dest: %pI4\n",
143 &ip_hdr(skb
)->saddr
, &rt
->rt_dst
);
151 /* Reroute packet to local IPv4 stack after DNAT */
153 __ip_vs_reroute_locally(struct sk_buff
*skb
)
155 struct rtable
*rt
= skb_rtable(skb
);
156 struct net_device
*dev
= rt
->dst
.dev
;
157 struct net
*net
= dev_net(dev
);
158 struct iphdr
*iph
= ip_hdr(skb
);
160 if (rt_is_input_route(rt
)) {
161 unsigned long orefdst
= skb
->_skb_refdst
;
163 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
,
166 refdst_drop(orefdst
);
168 struct flowi4 fl4
= {
171 .flowi4_tos
= RT_TOS(iph
->tos
),
172 .flowi4_mark
= skb
->mark
,
175 rt
= ip_route_output_key(net
, &fl4
);
178 if (!(rt
->rt_flags
& RTCF_LOCAL
)) {
182 /* Drop old route. */
184 skb_dst_set(skb
, &rt
->dst
);
189 #ifdef CONFIG_IP_VS_IPV6
191 static inline int __ip_vs_is_local_route6(struct rt6_info
*rt
)
193 return rt
->rt6i_dev
&& rt
->rt6i_dev
->flags
& IFF_LOOPBACK
;
196 static struct dst_entry
*
197 __ip_vs_route_output_v6(struct net
*net
, struct in6_addr
*daddr
,
198 struct in6_addr
*ret_saddr
, int do_xfrm
)
200 struct dst_entry
*dst
;
201 struct flowi6 fl6
= {
205 dst
= ip6_route_output(net
, NULL
, &fl6
);
210 if (ipv6_addr_any(&fl6
.saddr
) &&
211 ipv6_dev_get_saddr(net
, ip6_dst_idev(dst
)->dev
,
212 &fl6
.daddr
, 0, &fl6
.saddr
) < 0)
215 dst
= xfrm_lookup(net
, dst
, flowi6_to_flowi(&fl6
), NULL
, 0);
221 ipv6_addr_copy(ret_saddr
, &fl6
.saddr
);
226 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr
);
231 * Get route to destination or remote server
232 * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest,
233 * &4=Allow redirect from remote daddr to local
235 static struct rt6_info
*
236 __ip_vs_get_out_rt_v6(struct sk_buff
*skb
, struct ip_vs_dest
*dest
,
237 struct in6_addr
*daddr
, struct in6_addr
*ret_saddr
,
238 int do_xfrm
, int rt_mode
)
240 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
241 struct rt6_info
*rt
; /* Route to the other host */
242 struct rt6_info
*ort
; /* Original route */
243 struct dst_entry
*dst
;
247 spin_lock(&dest
->dst_lock
);
248 rt
= (struct rt6_info
*)__ip_vs_dst_check(dest
, 0);
252 dst
= __ip_vs_route_output_v6(net
, &dest
->addr
.in6
,
256 spin_unlock(&dest
->dst_lock
);
259 rt
= (struct rt6_info
*) dst
;
260 cookie
= rt
->rt6i_node
? rt
->rt6i_node
->fn_sernum
: 0;
261 __ip_vs_dst_set(dest
, 0, dst_clone(&rt
->dst
), cookie
);
262 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
263 &dest
->addr
.in6
, &dest
->dst_saddr
,
264 atomic_read(&rt
->dst
.__refcnt
));
267 ipv6_addr_copy(ret_saddr
, &dest
->dst_saddr
);
268 spin_unlock(&dest
->dst_lock
);
270 dst
= __ip_vs_route_output_v6(net
, daddr
, ret_saddr
, do_xfrm
);
273 rt
= (struct rt6_info
*) dst
;
276 local
= __ip_vs_is_local_route6(rt
);
277 if (!((local
? 1 : 2) & rt_mode
)) {
278 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
279 local
? "local":"non-local", daddr
);
280 dst_release(&rt
->dst
);
283 if (local
&& !(rt_mode
& 4) &&
284 !((ort
= (struct rt6_info
*) skb_dst(skb
)) &&
285 __ip_vs_is_local_route6(ort
))) {
286 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
287 "requires NAT method, dest: %pI6\n",
288 &ipv6_hdr(skb
)->daddr
, daddr
);
289 dst_release(&rt
->dst
);
292 if (unlikely(!local
&& (!skb
->dev
|| skb
->dev
->flags
& IFF_LOOPBACK
) &&
293 ipv6_addr_type(&ipv6_hdr(skb
)->saddr
) &
294 IPV6_ADDR_LOOPBACK
)) {
295 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
296 "to non-local address, dest: %pI6\n",
297 &ipv6_hdr(skb
)->saddr
, daddr
);
298 dst_release(&rt
->dst
);
308 * Release dest->dst_cache before a dest is removed
311 ip_vs_dst_reset(struct ip_vs_dest
*dest
)
313 struct dst_entry
*old_dst
;
315 old_dst
= dest
->dst_cache
;
316 dest
->dst_cache
= NULL
;
317 dst_release(old_dst
);
320 #define IP_VS_XMIT_TUNNEL(skb, cp) \
322 int __ret = NF_ACCEPT; \
324 (skb)->ipvs_property = 1; \
325 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
326 __ret = ip_vs_confirm_conntrack(skb, cp); \
327 if (__ret == NF_ACCEPT) { \
329 skb_forward_csum(skb); \
334 #define IP_VS_XMIT_NAT(pf, skb, cp, local) \
336 (skb)->ipvs_property = 1; \
337 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
338 ip_vs_notrack(skb); \
340 ip_vs_update_conntrack(skb, cp, 1); \
343 skb_forward_csum(skb); \
344 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
345 skb_dst(skb)->dev, dst_output); \
348 #define IP_VS_XMIT(pf, skb, cp, local) \
350 (skb)->ipvs_property = 1; \
351 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
352 ip_vs_notrack(skb); \
355 skb_forward_csum(skb); \
356 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
357 skb_dst(skb)->dev, dst_output); \
362 * NULL transmitter (do nothing except return NF_ACCEPT)
365 ip_vs_null_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
366 struct ip_vs_protocol
*pp
)
368 /* we do not touch skb and do not need pskb ptr */
369 IP_VS_XMIT(NFPROTO_IPV4
, skb
, cp
, 1);
375 * Let packets bypass the destination when the destination is not
376 * available, it may be only used in transparent cache cluster.
379 ip_vs_bypass_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
380 struct ip_vs_protocol
*pp
)
382 struct rtable
*rt
; /* Route to the other host */
383 struct iphdr
*iph
= ip_hdr(skb
);
388 if (!(rt
= __ip_vs_get_out_rt(skb
, NULL
, iph
->daddr
, RT_TOS(iph
->tos
),
389 IP_VS_RT_MODE_NON_LOCAL
)))
393 mtu
= dst_mtu(&rt
->dst
);
394 if ((skb
->len
> mtu
) && (iph
->frag_off
& htons(IP_DF
)) &&
397 icmp_send(skb
, ICMP_DEST_UNREACH
,ICMP_FRAG_NEEDED
, htonl(mtu
));
398 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
403 * Call ip_send_check because we are not sure it is called
404 * after ip_defrag. Is copy-on-write needed?
406 if (unlikely((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)) {
410 ip_send_check(ip_hdr(skb
));
414 skb_dst_set(skb
, &rt
->dst
);
416 /* Another hack: avoid icmp_send in ip_fragment */
419 IP_VS_XMIT(NFPROTO_IPV4
, skb
, cp
, 0);
425 dst_link_failure(skb
);
432 #ifdef CONFIG_IP_VS_IPV6
434 ip_vs_bypass_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
435 struct ip_vs_protocol
*pp
)
437 struct rt6_info
*rt
; /* Route to the other host */
438 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
443 if (!(rt
= __ip_vs_get_out_rt_v6(skb
, NULL
, &iph
->daddr
, NULL
, 0, 2)))
447 mtu
= dst_mtu(&rt
->dst
);
448 if (skb
->len
> mtu
&& !skb_is_gso(skb
)) {
450 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
452 skb
->dev
= net
->loopback_dev
;
454 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
455 dst_release(&rt
->dst
);
456 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
461 * Call ip_send_check because we are not sure it is called
462 * after ip_defrag. Is copy-on-write needed?
464 skb
= skb_share_check(skb
, GFP_ATOMIC
);
465 if (unlikely(skb
== NULL
)) {
466 dst_release(&rt
->dst
);
472 skb_dst_set(skb
, &rt
->dst
);
474 /* Another hack: avoid icmp_send in ip_fragment */
477 IP_VS_XMIT(NFPROTO_IPV6
, skb
, cp
, 0);
483 dst_link_failure(skb
);
492 * NAT transmitter (only for outside-to-inside nat forwarding)
493 * Not used for related ICMP
496 ip_vs_nat_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
497 struct ip_vs_protocol
*pp
)
499 struct rtable
*rt
; /* Route to the other host */
501 struct iphdr
*iph
= ip_hdr(skb
);
506 /* check if it is a connection of no-client-port */
507 if (unlikely(cp
->flags
& IP_VS_CONN_F_NO_CPORT
)) {
509 p
= skb_header_pointer(skb
, iph
->ihl
*4, sizeof(_pt
), &_pt
);
512 ip_vs_conn_fill_cport(cp
, *p
);
513 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p
));
516 if (!(rt
= __ip_vs_get_out_rt(skb
, cp
->dest
, cp
->daddr
.ip
,
518 IP_VS_RT_MODE_LOCAL
|
519 IP_VS_RT_MODE_NON_LOCAL
|
522 local
= rt
->rt_flags
& RTCF_LOCAL
;
524 * Avoid duplicate tuple in reply direction for NAT traffic
525 * to local address when connection is sync-ed
527 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
528 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
529 enum ip_conntrack_info ctinfo
;
530 struct nf_conn
*ct
= ct
= nf_ct_get(skb
, &ctinfo
);
532 if (ct
&& !nf_ct_is_untracked(ct
)) {
533 IP_VS_DBG_RL_PKT(10, AF_INET
, pp
, skb
, 0,
535 "stopping DNAT to local address");
541 /* From world but DNAT to loopback address? */
542 if (local
&& ipv4_is_loopback(rt
->rt_dst
) &&
543 rt_is_input_route(skb_rtable(skb
))) {
544 IP_VS_DBG_RL_PKT(1, AF_INET
, pp
, skb
, 0, "ip_vs_nat_xmit(): "
545 "stopping DNAT to loopback address");
550 mtu
= dst_mtu(&rt
->dst
);
551 if ((skb
->len
> mtu
) && (iph
->frag_off
& htons(IP_DF
)) &&
553 icmp_send(skb
, ICMP_DEST_UNREACH
,ICMP_FRAG_NEEDED
, htonl(mtu
));
554 IP_VS_DBG_RL_PKT(0, AF_INET
, pp
, skb
, 0,
555 "ip_vs_nat_xmit(): frag needed for");
559 /* copy-on-write the packet before mangling it */
560 if (!skb_make_writable(skb
, sizeof(struct iphdr
)))
563 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
566 /* mangle the packet */
567 if (pp
->dnat_handler
&& !pp
->dnat_handler(skb
, pp
, cp
))
569 ip_hdr(skb
)->daddr
= cp
->daddr
.ip
;
570 ip_send_check(ip_hdr(skb
));
575 skb_dst_set(skb
, &rt
->dst
);
579 * Some IPv4 replies get local address from routes,
580 * not from iph, so while we DNAT after routing
581 * we need this second input/output route.
583 if (!__ip_vs_reroute_locally(skb
))
587 IP_VS_DBG_PKT(10, AF_INET
, pp
, skb
, 0, "After DNAT");
589 /* FIXME: when application helper enlarges the packet and the length
590 is larger than the MTU of outgoing device, there will be still
593 /* Another hack: avoid icmp_send in ip_fragment */
596 IP_VS_XMIT_NAT(NFPROTO_IPV4
, skb
, cp
, local
);
602 dst_link_failure(skb
);
612 #ifdef CONFIG_IP_VS_IPV6
614 ip_vs_nat_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
615 struct ip_vs_protocol
*pp
)
617 struct rt6_info
*rt
; /* Route to the other host */
623 /* check if it is a connection of no-client-port */
624 if (unlikely(cp
->flags
& IP_VS_CONN_F_NO_CPORT
)) {
626 p
= skb_header_pointer(skb
, sizeof(struct ipv6hdr
),
630 ip_vs_conn_fill_cport(cp
, *p
);
631 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p
));
634 if (!(rt
= __ip_vs_get_out_rt_v6(skb
, cp
->dest
, &cp
->daddr
.in6
, NULL
,
637 local
= __ip_vs_is_local_route6(rt
);
639 * Avoid duplicate tuple in reply direction for NAT traffic
640 * to local address when connection is sync-ed
642 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
643 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
644 enum ip_conntrack_info ctinfo
;
645 struct nf_conn
*ct
= ct
= nf_ct_get(skb
, &ctinfo
);
647 if (ct
&& !nf_ct_is_untracked(ct
)) {
648 IP_VS_DBG_RL_PKT(10, AF_INET6
, pp
, skb
, 0,
649 "ip_vs_nat_xmit_v6(): "
650 "stopping DNAT to local address");
656 /* From world but DNAT to loopback address? */
657 if (local
&& skb
->dev
&& !(skb
->dev
->flags
& IFF_LOOPBACK
) &&
658 ipv6_addr_type(&rt
->rt6i_dst
.addr
) & IPV6_ADDR_LOOPBACK
) {
659 IP_VS_DBG_RL_PKT(1, AF_INET6
, pp
, skb
, 0,
660 "ip_vs_nat_xmit_v6(): "
661 "stopping DNAT to loopback address");
666 mtu
= dst_mtu(&rt
->dst
);
667 if (skb
->len
> mtu
&& !skb_is_gso(skb
)) {
669 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
671 skb
->dev
= net
->loopback_dev
;
673 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
674 IP_VS_DBG_RL_PKT(0, AF_INET6
, pp
, skb
, 0,
675 "ip_vs_nat_xmit_v6(): frag needed for");
679 /* copy-on-write the packet before mangling it */
680 if (!skb_make_writable(skb
, sizeof(struct ipv6hdr
)))
683 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
686 /* mangle the packet */
687 if (pp
->dnat_handler
&& !pp
->dnat_handler(skb
, pp
, cp
))
689 ipv6_addr_copy(&ipv6_hdr(skb
)->daddr
, &cp
->daddr
.in6
);
691 if (!local
|| !skb
->dev
) {
692 /* drop the old route when skb is not shared */
694 skb_dst_set(skb
, &rt
->dst
);
696 /* destined to loopback, do we need to change route? */
697 dst_release(&rt
->dst
);
700 IP_VS_DBG_PKT(10, AF_INET6
, pp
, skb
, 0, "After DNAT");
702 /* FIXME: when application helper enlarges the packet and the length
703 is larger than the MTU of outgoing device, there will be still
706 /* Another hack: avoid icmp_send in ip_fragment */
709 IP_VS_XMIT_NAT(NFPROTO_IPV6
, skb
, cp
, local
);
715 dst_link_failure(skb
);
721 dst_release(&rt
->dst
);
728 * IP Tunneling transmitter
730 * This function encapsulates the packet in a new IP packet, its
731 * destination will be set to cp->daddr. Most code of this function
732 * is taken from ipip.c.
734 * It is used in VS/TUN cluster. The load balancer selects a real
735 * server from a cluster based on a scheduling algorithm,
736 * encapsulates the request packet and forwards it to the selected
737 * server. For example, all real servers are configured with
738 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
739 * the encapsulated packet, it will decapsulate the packet, processe
740 * the request and return the response packets directly to the client
741 * without passing the load balancer. This can greatly increase the
742 * scalability of virtual server.
744 * Used for ANY protocol
747 ip_vs_tunnel_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
748 struct ip_vs_protocol
*pp
)
750 struct rtable
*rt
; /* Route to the other host */
751 struct net_device
*tdev
; /* Device to other host */
752 struct iphdr
*old_iph
= ip_hdr(skb
);
753 u8 tos
= old_iph
->tos
;
754 __be16 df
= old_iph
->frag_off
;
755 struct iphdr
*iph
; /* Our new IP header */
756 unsigned int max_headroom
; /* The extra header space needed */
762 if (!(rt
= __ip_vs_get_out_rt(skb
, cp
->dest
, cp
->daddr
.ip
,
763 RT_TOS(tos
), IP_VS_RT_MODE_LOCAL
|
764 IP_VS_RT_MODE_NON_LOCAL
)))
766 if (rt
->rt_flags
& RTCF_LOCAL
) {
768 IP_VS_XMIT(NFPROTO_IPV4
, skb
, cp
, 1);
773 mtu
= dst_mtu(&rt
->dst
) - sizeof(struct iphdr
);
775 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__
);
779 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), mtu
);
781 df
|= (old_iph
->frag_off
& htons(IP_DF
));
783 if ((old_iph
->frag_off
& htons(IP_DF
) &&
784 mtu
< ntohs(old_iph
->tot_len
) && !skb_is_gso(skb
))) {
785 icmp_send(skb
, ICMP_DEST_UNREACH
,ICMP_FRAG_NEEDED
, htonl(mtu
));
786 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
791 * Okay, now see if we can stuff it in the buffer as-is.
793 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(struct iphdr
);
795 if (skb_headroom(skb
) < max_headroom
796 || skb_cloned(skb
) || skb_shared(skb
)) {
797 struct sk_buff
*new_skb
=
798 skb_realloc_headroom(skb
, max_headroom
);
802 IP_VS_ERR_RL("%s(): no memory\n", __func__
);
807 old_iph
= ip_hdr(skb
);
810 skb
->transport_header
= skb
->network_header
;
812 /* fix old IP header checksum */
813 ip_send_check(old_iph
);
815 skb_push(skb
, sizeof(struct iphdr
));
816 skb_reset_network_header(skb
);
817 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
821 skb_dst_set(skb
, &rt
->dst
);
824 * Push down and install the IPIP header.
828 iph
->ihl
= sizeof(struct iphdr
)>>2;
830 iph
->protocol
= IPPROTO_IPIP
;
832 iph
->daddr
= rt
->rt_dst
;
833 iph
->saddr
= rt
->rt_src
;
834 iph
->ttl
= old_iph
->ttl
;
835 ip_select_ident(iph
, &rt
->dst
, NULL
);
837 /* Another hack: avoid icmp_send in ip_fragment */
840 ret
= IP_VS_XMIT_TUNNEL(skb
, cp
);
841 if (ret
== NF_ACCEPT
)
843 else if (ret
== NF_DROP
)
851 dst_link_failure(skb
);
861 #ifdef CONFIG_IP_VS_IPV6
863 ip_vs_tunnel_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
864 struct ip_vs_protocol
*pp
)
866 struct rt6_info
*rt
; /* Route to the other host */
867 struct in6_addr saddr
; /* Source for tunnel */
868 struct net_device
*tdev
; /* Device to other host */
869 struct ipv6hdr
*old_iph
= ipv6_hdr(skb
);
870 struct ipv6hdr
*iph
; /* Our new IP header */
871 unsigned int max_headroom
; /* The extra header space needed */
877 if (!(rt
= __ip_vs_get_out_rt_v6(skb
, cp
->dest
, &cp
->daddr
.in6
,
880 if (__ip_vs_is_local_route6(rt
)) {
881 dst_release(&rt
->dst
);
882 IP_VS_XMIT(NFPROTO_IPV6
, skb
, cp
, 1);
887 mtu
= dst_mtu(&rt
->dst
) - sizeof(struct ipv6hdr
);
888 if (mtu
< IPV6_MIN_MTU
) {
889 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__
,
894 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), mtu
);
896 if (mtu
< ntohs(old_iph
->payload_len
) + sizeof(struct ipv6hdr
) &&
899 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
901 skb
->dev
= net
->loopback_dev
;
903 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
904 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
909 * Okay, now see if we can stuff it in the buffer as-is.
911 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(struct ipv6hdr
);
913 if (skb_headroom(skb
) < max_headroom
914 || skb_cloned(skb
) || skb_shared(skb
)) {
915 struct sk_buff
*new_skb
=
916 skb_realloc_headroom(skb
, max_headroom
);
918 dst_release(&rt
->dst
);
920 IP_VS_ERR_RL("%s(): no memory\n", __func__
);
925 old_iph
= ipv6_hdr(skb
);
928 skb
->transport_header
= skb
->network_header
;
930 skb_push(skb
, sizeof(struct ipv6hdr
));
931 skb_reset_network_header(skb
);
932 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
936 skb_dst_set(skb
, &rt
->dst
);
939 * Push down and install the IPIP header.
943 iph
->nexthdr
= IPPROTO_IPV6
;
944 iph
->payload_len
= old_iph
->payload_len
;
945 be16_add_cpu(&iph
->payload_len
, sizeof(*old_iph
));
946 iph
->priority
= old_iph
->priority
;
947 memset(&iph
->flow_lbl
, 0, sizeof(iph
->flow_lbl
));
948 ipv6_addr_copy(&iph
->daddr
, &cp
->daddr
.in6
);
949 ipv6_addr_copy(&iph
->saddr
, &saddr
);
950 iph
->hop_limit
= old_iph
->hop_limit
;
952 /* Another hack: avoid icmp_send in ip_fragment */
955 ret
= IP_VS_XMIT_TUNNEL(skb
, cp
);
956 if (ret
== NF_ACCEPT
)
958 else if (ret
== NF_DROP
)
966 dst_link_failure(skb
);
972 dst_release(&rt
->dst
);
979 * Direct Routing transmitter
980 * Used for ANY protocol
983 ip_vs_dr_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
984 struct ip_vs_protocol
*pp
)
986 struct rtable
*rt
; /* Route to the other host */
987 struct iphdr
*iph
= ip_hdr(skb
);
992 if (!(rt
= __ip_vs_get_out_rt(skb
, cp
->dest
, cp
->daddr
.ip
,
994 IP_VS_RT_MODE_LOCAL
|
995 IP_VS_RT_MODE_NON_LOCAL
)))
997 if (rt
->rt_flags
& RTCF_LOCAL
) {
999 IP_VS_XMIT(NFPROTO_IPV4
, skb
, cp
, 1);
1003 mtu
= dst_mtu(&rt
->dst
);
1004 if ((iph
->frag_off
& htons(IP_DF
)) && skb
->len
> mtu
&&
1006 icmp_send(skb
, ICMP_DEST_UNREACH
,ICMP_FRAG_NEEDED
, htonl(mtu
));
1008 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
1013 * Call ip_send_check because we are not sure it is called
1014 * after ip_defrag. Is copy-on-write needed?
1016 if (unlikely((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
)) {
1020 ip_send_check(ip_hdr(skb
));
1022 /* drop old route */
1024 skb_dst_set(skb
, &rt
->dst
);
1026 /* Another hack: avoid icmp_send in ip_fragment */
1029 IP_VS_XMIT(NFPROTO_IPV4
, skb
, cp
, 0);
1035 dst_link_failure(skb
);
1042 #ifdef CONFIG_IP_VS_IPV6
1044 ip_vs_dr_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1045 struct ip_vs_protocol
*pp
)
1047 struct rt6_info
*rt
; /* Route to the other host */
1052 if (!(rt
= __ip_vs_get_out_rt_v6(skb
, cp
->dest
, &cp
->daddr
.in6
, NULL
,
1055 if (__ip_vs_is_local_route6(rt
)) {
1056 dst_release(&rt
->dst
);
1057 IP_VS_XMIT(NFPROTO_IPV6
, skb
, cp
, 1);
1061 mtu
= dst_mtu(&rt
->dst
);
1062 if (skb
->len
> mtu
) {
1064 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
1066 skb
->dev
= net
->loopback_dev
;
1068 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1069 dst_release(&rt
->dst
);
1070 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
1075 * Call ip_send_check because we are not sure it is called
1076 * after ip_defrag. Is copy-on-write needed?
1078 skb
= skb_share_check(skb
, GFP_ATOMIC
);
1079 if (unlikely(skb
== NULL
)) {
1080 dst_release(&rt
->dst
);
1084 /* drop old route */
1086 skb_dst_set(skb
, &rt
->dst
);
1088 /* Another hack: avoid icmp_send in ip_fragment */
1091 IP_VS_XMIT(NFPROTO_IPV6
, skb
, cp
, 0);
1097 dst_link_failure(skb
);
1107 * ICMP packet transmitter
1108 * called by the ip_vs_in_icmp
1111 ip_vs_icmp_xmit(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1112 struct ip_vs_protocol
*pp
, int offset
)
1114 struct rtable
*rt
; /* Route to the other host */
1121 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1122 forwarded directly here, because there is no need to
1123 translate address/port back */
1124 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
) {
1125 if (cp
->packet_xmit
)
1126 rc
= cp
->packet_xmit(skb
, cp
, pp
);
1129 /* do not touch skb anymore */
1130 atomic_inc(&cp
->in_pkts
);
1135 * mangle and send the packet here (only for VS/NAT)
1138 if (!(rt
= __ip_vs_get_out_rt(skb
, cp
->dest
, cp
->daddr
.ip
,
1139 RT_TOS(ip_hdr(skb
)->tos
),
1140 IP_VS_RT_MODE_LOCAL
|
1141 IP_VS_RT_MODE_NON_LOCAL
|
1142 IP_VS_RT_MODE_RDR
)))
1144 local
= rt
->rt_flags
& RTCF_LOCAL
;
1147 * Avoid duplicate tuple in reply direction for NAT traffic
1148 * to local address when connection is sync-ed
1150 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1151 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
1152 enum ip_conntrack_info ctinfo
;
1153 struct nf_conn
*ct
= ct
= nf_ct_get(skb
, &ctinfo
);
1155 if (ct
&& !nf_ct_is_untracked(ct
)) {
1156 IP_VS_DBG(10, "%s(): "
1157 "stopping DNAT to local address %pI4\n",
1158 __func__
, &cp
->daddr
.ip
);
1164 /* From world but DNAT to loopback address? */
1165 if (local
&& ipv4_is_loopback(rt
->rt_dst
) &&
1166 rt_is_input_route(skb_rtable(skb
))) {
1167 IP_VS_DBG(1, "%s(): "
1168 "stopping DNAT to loopback %pI4\n",
1169 __func__
, &cp
->daddr
.ip
);
1174 mtu
= dst_mtu(&rt
->dst
);
1175 if ((skb
->len
> mtu
) && (ip_hdr(skb
)->frag_off
& htons(IP_DF
)) &&
1177 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
, htonl(mtu
));
1178 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
1182 /* copy-on-write the packet before mangling it */
1183 if (!skb_make_writable(skb
, offset
))
1186 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
1189 ip_vs_nat_icmp(skb
, pp
, cp
, 0);
1192 /* drop the old route when skb is not shared */
1194 skb_dst_set(skb
, &rt
->dst
);
1198 * Some IPv4 replies get local address from routes,
1199 * not from iph, so while we DNAT after routing
1200 * we need this second input/output route.
1202 if (!__ip_vs_reroute_locally(skb
))
1206 /* Another hack: avoid icmp_send in ip_fragment */
1209 IP_VS_XMIT_NAT(NFPROTO_IPV4
, skb
, cp
, local
);
1215 dst_link_failure(skb
);
1227 #ifdef CONFIG_IP_VS_IPV6
1229 ip_vs_icmp_xmit_v6(struct sk_buff
*skb
, struct ip_vs_conn
*cp
,
1230 struct ip_vs_protocol
*pp
, int offset
)
1232 struct rt6_info
*rt
; /* Route to the other host */
1239 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1240 forwarded directly here, because there is no need to
1241 translate address/port back */
1242 if (IP_VS_FWD_METHOD(cp
) != IP_VS_CONN_F_MASQ
) {
1243 if (cp
->packet_xmit
)
1244 rc
= cp
->packet_xmit(skb
, cp
, pp
);
1247 /* do not touch skb anymore */
1248 atomic_inc(&cp
->in_pkts
);
1253 * mangle and send the packet here (only for VS/NAT)
1256 if (!(rt
= __ip_vs_get_out_rt_v6(skb
, cp
->dest
, &cp
->daddr
.in6
, NULL
,
1260 local
= __ip_vs_is_local_route6(rt
);
1262 * Avoid duplicate tuple in reply direction for NAT traffic
1263 * to local address when connection is sync-ed
1265 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1266 if (cp
->flags
& IP_VS_CONN_F_SYNC
&& local
) {
1267 enum ip_conntrack_info ctinfo
;
1268 struct nf_conn
*ct
= ct
= nf_ct_get(skb
, &ctinfo
);
1270 if (ct
&& !nf_ct_is_untracked(ct
)) {
1271 IP_VS_DBG(10, "%s(): "
1272 "stopping DNAT to local address %pI6\n",
1273 __func__
, &cp
->daddr
.in6
);
1279 /* From world but DNAT to loopback address? */
1280 if (local
&& skb
->dev
&& !(skb
->dev
->flags
& IFF_LOOPBACK
) &&
1281 ipv6_addr_type(&rt
->rt6i_dst
.addr
) & IPV6_ADDR_LOOPBACK
) {
1282 IP_VS_DBG(1, "%s(): "
1283 "stopping DNAT to loopback %pI6\n",
1284 __func__
, &cp
->daddr
.in6
);
1289 mtu
= dst_mtu(&rt
->dst
);
1290 if (skb
->len
> mtu
&& !skb_is_gso(skb
)) {
1292 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
1294 skb
->dev
= net
->loopback_dev
;
1296 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1297 IP_VS_DBG_RL("%s(): frag needed\n", __func__
);
1301 /* copy-on-write the packet before mangling it */
1302 if (!skb_make_writable(skb
, offset
))
1305 if (skb_cow(skb
, rt
->dst
.dev
->hard_header_len
))
1308 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 0);
1310 if (!local
|| !skb
->dev
) {
1311 /* drop the old route when skb is not shared */
1313 skb_dst_set(skb
, &rt
->dst
);
1315 /* destined to loopback, do we need to change route? */
1316 dst_release(&rt
->dst
);
1319 /* Another hack: avoid icmp_send in ip_fragment */
1322 IP_VS_XMIT_NAT(NFPROTO_IPV6
, skb
, cp
, local
);
1328 dst_link_failure(skb
);
1336 dst_release(&rt
->dst
);