blob: b75ff6429a04e25d4ba8e6368173ab84a342751c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ip_vs_xmit.c: various packet transmitters for IPVS
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
6 *
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.
11 *
12 * Changes:
13 *
Julian Anastasovcb591552010-10-17 16:40:51 +030014 * 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
Julian Anastasov026ace02013-03-21 11:58:06 +020020 * - we can use dst without ref while sending in RCU section, we use
21 * ref when returning NF_ACCEPT for NAT-ed packet via loopback
Julian Anastasovcb591552010-10-17 16:40:51 +030022 * LOCAL_OUT rules:
23 * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING)
24 * - skb->pkt_type is not set yet
25 * - the only place where we can see skb->sk != NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Hannes Eder9aada7a2009-07-30 14:29:44 -070028#define KMSG_COMPONENT "IPVS"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tcp.h> /* for tcphdr */
Herbert Xuc439cb22008-01-11 19:14:00 -080034#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/tcp.h> /* for csum_tcpudp_magic */
36#include <net/udp.h>
37#include <net/icmp.h> /* for icmp_send */
38#include <net/route.h> /* for ip_route_output */
Julius Volz38cdcc92008-09-02 15:55:44 +020039#include <net/ipv6.h>
40#include <net/ip6_route.h>
Hans Schillstrom714f0952010-10-19 10:38:48 +020041#include <net/addrconf.h>
Julius Volz38cdcc92008-09-02 15:55:44 +020042#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/netfilter.h>
44#include <linux/netfilter_ipv4.h>
45
46#include <net/ip_vs.h>
47
Changli Gao17a8f8e2011-02-24 08:19:57 +080048enum {
49 IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */
50 IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */
51 IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to
52 * local
53 */
Julian Anastasovf2edb9f2012-07-20 11:59:52 +030054 IP_VS_RT_MODE_CONNECT = 8, /* Always bind route to saddr */
Julian Anastasovad4d3ef2012-10-08 11:41:20 +000055 IP_VS_RT_MODE_KNOWN_NH = 16,/* Route via remote addr */
Julian Anastasov4115ded2013-03-21 11:58:05 +020056 IP_VS_RT_MODE_TUNNEL = 32,/* Tunnel mode */
Changli Gao17a8f8e2011-02-24 08:19:57 +080057};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Julian Anastasov026ace02013-03-21 11:58:06 +020059static inline struct ip_vs_dest_dst *ip_vs_dest_dst_alloc(void)
60{
61 return kmalloc(sizeof(struct ip_vs_dest_dst), GFP_ATOMIC);
62}
63
64static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
65{
66 kfree(dest_dst);
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Destination cache to speed up outgoing route lookup
71 */
72static inline void
Julian Anastasov026ace02013-03-21 11:58:06 +020073__ip_vs_dst_set(struct ip_vs_dest *dest, struct ip_vs_dest_dst *dest_dst,
74 struct dst_entry *dst, u32 dst_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Julian Anastasov026ace02013-03-21 11:58:06 +020076 struct ip_vs_dest_dst *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Julian Anastasov026ace02013-03-21 11:58:06 +020078 old = rcu_dereference_protected(dest->dest_dst,
79 lockdep_is_held(&dest->dst_lock));
80
81 if (dest_dst) {
82 dest_dst->dst_cache = dst;
83 dest_dst->dst_cookie = dst_cookie;
84 }
85 rcu_assign_pointer(dest->dest_dst, dest_dst);
86
87 if (old)
88 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Julian Anastasov026ace02013-03-21 11:58:06 +020091static inline struct ip_vs_dest_dst *
Julian Anastasovc90558d2013-03-21 11:57:59 +020092__ip_vs_dst_check(struct ip_vs_dest *dest)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Julian Anastasov026ace02013-03-21 11:58:06 +020094 struct ip_vs_dest_dst *dest_dst = rcu_dereference(dest->dest_dst);
95 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Julian Anastasov026ace02013-03-21 11:58:06 +020097 if (!dest_dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return NULL;
Julian Anastasov026ace02013-03-21 11:58:06 +020099 dst = dest_dst->dst_cache;
100 if (dst->obsolete &&
101 dst->ops->check(dst, dest_dst->dst_cookie) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return NULL;
Julian Anastasov026ace02013-03-21 11:58:06 +0200103 return dest_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Jesper Dangaard Brouer590e3f72012-08-28 22:05:51 +0200106static inline bool
107__mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
108{
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200109 if (IP6CB(skb)->frag_max_size) {
110 /* frag_max_size tell us that, this packet have been
111 * defragmented by netfilter IPv6 conntrack module.
112 */
113 if (IP6CB(skb)->frag_max_size > mtu)
114 return true; /* largest fragment violate MTU */
115 }
116 else if (skb->len > mtu && !skb_is_gso(skb)) {
Jesper Dangaard Brouer590e3f72012-08-28 22:05:51 +0200117 return true; /* Packet size violate MTU size */
118 }
119 return false;
120}
121
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300122/* Get route to daddr, update *saddr, optionally bind route to saddr */
123static struct rtable *do_output_route4(struct net *net, __be32 daddr,
Julian Anastasovc90558d2013-03-21 11:57:59 +0200124 int rt_mode, __be32 *saddr)
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300125{
126 struct flowi4 fl4;
127 struct rtable *rt;
128 int loop = 0;
129
130 memset(&fl4, 0, sizeof(fl4));
131 fl4.daddr = daddr;
132 fl4.saddr = (rt_mode & IP_VS_RT_MODE_CONNECT) ? *saddr : 0;
Julian Anastasovad4d3ef2012-10-08 11:41:20 +0000133 fl4.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
134 FLOWI_FLAG_KNOWN_NH : 0;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300135
136retry:
137 rt = ip_route_output_key(net, &fl4);
138 if (IS_ERR(rt)) {
139 /* Invalid saddr ? */
140 if (PTR_ERR(rt) == -EINVAL && *saddr &&
141 rt_mode & IP_VS_RT_MODE_CONNECT && !loop) {
142 *saddr = 0;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200143 flowi4_update_output(&fl4, 0, 0, daddr, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300144 goto retry;
145 }
146 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", &daddr);
147 return NULL;
148 } else if (!*saddr && rt_mode & IP_VS_RT_MODE_CONNECT && fl4.saddr) {
149 ip_rt_put(rt);
150 *saddr = fl4.saddr;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200151 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300152 loop++;
153 goto retry;
154 }
155 *saddr = fl4.saddr;
156 return rt;
157}
158
Changli Gao17a8f8e2011-02-24 08:19:57 +0800159/* Get route to destination or remote server */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200160static int
Julian Anastasovfc604762010-10-17 16:38:15 +0300161__ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
Julian Anastasovc90558d2013-03-21 11:57:59 +0200162 __be32 daddr, int rt_mode, __be32 *ret_saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Julian Anastasovfc604762010-10-17 16:38:15 +0300164 struct net *net = dev_net(skb_dst(skb)->dev);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200165 struct netns_ipvs *ipvs = net_ipvs(net);
Julian Anastasov026ace02013-03-21 11:58:06 +0200166 struct ip_vs_dest_dst *dest_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct rtable *rt; /* Route to the other host */
Julian Anastasovfc604762010-10-17 16:38:15 +0300168 struct rtable *ort; /* Original route */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200169 struct iphdr *iph;
170 __be16 df;
171 int mtu;
Julian Anastasov026ace02013-03-21 11:58:06 +0200172 int local, noref = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200175 dest_dst = __ip_vs_dst_check(dest);
176 if (likely(dest_dst))
177 rt = (struct rtable *) dest_dst->dst_cache;
178 else {
179 dest_dst = ip_vs_dest_dst_alloc();
Julian Anastasovac692692013-03-22 11:46:54 +0200180 spin_lock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200181 if (!dest_dst) {
182 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200183 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200184 goto err_unreach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Julian Anastasov026ace02013-03-21 11:58:06 +0200186 rt = do_output_route4(net, dest->addr.ip, rt_mode,
187 &dest_dst->dst_saddr.ip);
188 if (!rt) {
189 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200190 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200191 ip_vs_dest_dst_free(dest_dst);
192 goto err_unreach;
193 }
194 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200195 spin_unlock_bh(&dest->dst_lock);
Julian Anastasovc90558d2013-03-21 11:57:59 +0200196 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
Julian Anastasov026ace02013-03-21 11:58:06 +0200197 &dest->addr.ip, &dest_dst->dst_saddr.ip,
Julian Anastasovc90558d2013-03-21 11:57:59 +0200198 atomic_read(&rt->dst.__refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
David S. Miller44e31252011-05-09 14:38:06 -0700200 daddr = dest->addr.ip;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000201 if (ret_saddr)
Julian Anastasov026ace02013-03-21 11:58:06 +0200202 *ret_saddr = dest_dst->dst_saddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300204 __be32 saddr = htonl(INADDR_ANY);
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000205
Julian Anastasov026ace02013-03-21 11:58:06 +0200206 noref = 0;
207
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300208 /* For such unconfigured boxes avoid many route lookups
209 * for performance reasons because we do not remember saddr
210 */
211 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
Julian Anastasovc90558d2013-03-21 11:57:59 +0200212 rt = do_output_route4(net, daddr, rt_mode, &saddr);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300213 if (!rt)
Julian Anastasov4115ded2013-03-21 11:58:05 +0200214 goto err_unreach;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000215 if (ret_saddr)
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300216 *ret_saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Julian Anastasov4115ded2013-03-21 11:58:05 +0200219 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
Changli Gao17a8f8e2011-02-24 08:19:57 +0800220 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
221 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300222 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
223 (rt->rt_flags & RTCF_LOCAL) ?
David S. Miller44e31252011-05-09 14:38:06 -0700224 "local":"non-local", &daddr);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200225 goto err_put;
Julian Anastasovfc604762010-10-17 16:38:15 +0300226 }
Julian Anastasov4115ded2013-03-21 11:58:05 +0200227 iph = ip_hdr(skb);
228 if (likely(!local)) {
229 if (unlikely(ipv4_is_loopback(iph->saddr))) {
230 IP_VS_DBG_RL("Stopping traffic from loopback address "
231 "%pI4 to non-local address, dest: %pI4\n",
232 &iph->saddr, &daddr);
233 goto err_put;
234 }
235 } else {
236 ort = skb_rtable(skb);
237 if (!(rt_mode & IP_VS_RT_MODE_RDR) &&
238 !(ort->rt_flags & RTCF_LOCAL)) {
239 IP_VS_DBG_RL("Redirect from non-local address %pI4 to "
240 "local requires NAT method, dest: %pI4\n",
241 &iph->daddr, &daddr);
242 goto err_put;
243 }
244 /* skb to local stack, preserve old route */
Julian Anastasov026ace02013-03-21 11:58:06 +0200245 if (!noref)
246 ip_rt_put(rt);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200247 return local;
Julian Anastasovfc604762010-10-17 16:38:15 +0300248 }
249
Julian Anastasov4115ded2013-03-21 11:58:05 +0200250 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
251 mtu = dst_mtu(&rt->dst);
252 df = iph->frag_off & htons(IP_DF);
253 } else {
254 struct sock *sk = skb->sk;
255
256 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
257 if (mtu < 68) {
258 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
259 goto err_put;
260 }
261 ort = skb_rtable(skb);
262 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT)
263 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
264 /* MTU check allowed? */
265 df = sysctl_pmtu_disc(ipvs) ? iph->frag_off & htons(IP_DF) : 0;
266 }
267
268 /* MTU checking */
269 if (unlikely(df && skb->len > mtu && !skb_is_gso(skb))) {
270 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
271 IP_VS_DBG(1, "frag needed for %pI4\n", &iph->saddr);
272 goto err_put;
273 }
274
275 skb_dst_drop(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200276 if (noref) {
277 if (!local)
278 skb_dst_set_noref_force(skb, &rt->dst);
279 else
280 skb_dst_set(skb, dst_clone(&rt->dst));
281 } else
282 skb_dst_set(skb, &rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200283
284 return local;
285
286err_put:
Julian Anastasov026ace02013-03-21 11:58:06 +0200287 if (!noref)
288 ip_rt_put(rt);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200289 return -1;
290
291err_unreach:
292 dst_link_failure(skb);
293 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Julius Volz38cdcc92008-09-02 15:55:44 +0200296#ifdef CONFIG_IP_VS_IPV6
Hans Schillstrom714f0952010-10-19 10:38:48 +0200297
Julian Anastasovfc604762010-10-17 16:38:15 +0300298static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
299{
David S. Millerd1918542011-12-28 20:19:20 -0500300 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
Julian Anastasovfc604762010-10-17 16:38:15 +0300301}
302
Hans Schillstrom714f0952010-10-19 10:38:48 +0200303static struct dst_entry *
304__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
305 struct in6_addr *ret_saddr, int do_xfrm)
Julius Volz38cdcc92008-09-02 15:55:44 +0200306{
Hans Schillstrom714f0952010-10-19 10:38:48 +0200307 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500308 struct flowi6 fl6 = {
309 .daddr = *daddr,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200310 };
311
David S. Miller4c9483b2011-03-12 16:22:43 -0500312 dst = ip6_route_output(net, NULL, &fl6);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200313 if (dst->error)
314 goto out_err;
315 if (!ret_saddr)
316 return dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500317 if (ipv6_addr_any(&fl6.saddr) &&
Hans Schillstrom714f0952010-10-19 10:38:48 +0200318 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
David S. Miller4c9483b2011-03-12 16:22:43 -0500319 &fl6.daddr, 0, &fl6.saddr) < 0)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200320 goto out_err;
David S. Miller452edd52011-03-02 13:27:41 -0800321 if (do_xfrm) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500322 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -0800323 if (IS_ERR(dst)) {
324 dst = NULL;
325 goto out_err;
326 }
327 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000328 *ret_saddr = fl6.saddr;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200329 return dst;
330
331out_err:
332 dst_release(dst);
333 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
334 return NULL;
335}
336
Julian Anastasovfc604762010-10-17 16:38:15 +0300337/*
338 * Get route to destination or remote server
Julian Anastasovfc604762010-10-17 16:38:15 +0300339 */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200340static int
Julian Anastasovfc604762010-10-17 16:38:15 +0300341__ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
342 struct in6_addr *daddr, struct in6_addr *ret_saddr,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200343 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200344{
Julian Anastasovfc604762010-10-17 16:38:15 +0300345 struct net *net = dev_net(skb_dst(skb)->dev);
Julian Anastasov026ace02013-03-21 11:58:06 +0200346 struct ip_vs_dest_dst *dest_dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200347 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovfc604762010-10-17 16:38:15 +0300348 struct rt6_info *ort; /* Original route */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200349 struct dst_entry *dst;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200350 int mtu;
Julian Anastasov026ace02013-03-21 11:58:06 +0200351 int local, noref = 1;
Julius Volz38cdcc92008-09-02 15:55:44 +0200352
353 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200354 dest_dst = __ip_vs_dst_check(dest);
355 if (likely(dest_dst))
356 rt = (struct rt6_info *) dest_dst->dst_cache;
357 else {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200358 u32 cookie;
Julius Volz38cdcc92008-09-02 15:55:44 +0200359
Julian Anastasov026ace02013-03-21 11:58:06 +0200360 dest_dst = ip_vs_dest_dst_alloc();
Julian Anastasovac692692013-03-22 11:46:54 +0200361 spin_lock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200362 if (!dest_dst) {
363 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200364 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200365 goto err_unreach;
366 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200367 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
Julian Anastasov026ace02013-03-21 11:58:06 +0200368 &dest_dst->dst_saddr.in6,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200369 do_xfrm);
370 if (!dst) {
Julian Anastasov026ace02013-03-21 11:58:06 +0200371 __ip_vs_dst_set(dest, NULL, NULL, 0);
Julian Anastasovac692692013-03-22 11:46:54 +0200372 spin_unlock_bh(&dest->dst_lock);
Julian Anastasov026ace02013-03-21 11:58:06 +0200373 ip_vs_dest_dst_free(dest_dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200374 goto err_unreach;
Julius Volz38cdcc92008-09-02 15:55:44 +0200375 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200376 rt = (struct rt6_info *) dst;
377 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
Julian Anastasov026ace02013-03-21 11:58:06 +0200378 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
Julian Anastasovac692692013-03-22 11:46:54 +0200379 spin_unlock_bh(&dest->dst_lock);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200380 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
Julian Anastasov026ace02013-03-21 11:58:06 +0200381 &dest->addr.in6, &dest_dst->dst_saddr.in6,
Changli Gaod8d1f302010-06-10 23:31:35 -0700382 atomic_read(&rt->dst.__refcnt));
Julius Volz38cdcc92008-09-02 15:55:44 +0200383 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200384 if (ret_saddr)
Julian Anastasov026ace02013-03-21 11:58:06 +0200385 *ret_saddr = dest_dst->dst_saddr.in6;
Julius Volz38cdcc92008-09-02 15:55:44 +0200386 } else {
Julian Anastasov026ace02013-03-21 11:58:06 +0200387 noref = 0;
Julian Anastasovfc604762010-10-17 16:38:15 +0300388 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200389 if (!dst)
Julian Anastasov4115ded2013-03-21 11:58:05 +0200390 goto err_unreach;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200391 rt = (struct rt6_info *) dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200392 }
393
Julian Anastasovfc604762010-10-17 16:38:15 +0300394 local = __ip_vs_is_local_route6(rt);
David S. Millere58b3442011-05-12 18:22:34 -0400395 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
396 rt_mode)) {
Jesper Dangaard Brouer120b9c12012-09-26 14:05:53 +0200397 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6c\n",
Julian Anastasovfc604762010-10-17 16:38:15 +0300398 local ? "local":"non-local", daddr);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200399 goto err_put;
Julian Anastasovfc604762010-10-17 16:38:15 +0300400 }
Julian Anastasov4115ded2013-03-21 11:58:05 +0200401 if (likely(!local)) {
402 if (unlikely((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
403 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
404 IPV6_ADDR_LOOPBACK)) {
405 IP_VS_DBG_RL("Stopping traffic from loopback address "
406 "%pI6c to non-local address, "
407 "dest: %pI6c\n",
408 &ipv6_hdr(skb)->saddr, daddr);
409 goto err_put;
410 }
411 } else {
412 ort = (struct rt6_info *) skb_dst(skb);
413 if (!(rt_mode & IP_VS_RT_MODE_RDR) &&
414 !__ip_vs_is_local_route6(ort)) {
415 IP_VS_DBG_RL("Redirect from non-local address %pI6c "
416 "to local requires NAT method, "
417 "dest: %pI6c\n",
418 &ipv6_hdr(skb)->daddr, daddr);
419 goto err_put;
420 }
421 /* skb to local stack, preserve old route */
Julian Anastasov026ace02013-03-21 11:58:06 +0200422 if (!noref)
423 dst_release(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200424 return local;
Julian Anastasovfc604762010-10-17 16:38:15 +0300425 }
426
Julian Anastasov4115ded2013-03-21 11:58:05 +0200427 /* MTU checking */
428 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
429 mtu = dst_mtu(&rt->dst);
430 else {
431 struct sock *sk = skb->sk;
432
433 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
434 if (mtu < IPV6_MIN_MTU) {
435 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
436 IPV6_MIN_MTU);
437 goto err_put;
438 }
439 ort = (struct rt6_info *) skb_dst(skb);
440 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT)
441 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
442 }
443
444 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
445 if (!skb->dev)
446 skb->dev = net->loopback_dev;
447 /* only send ICMP too big on first fragment */
448 if (!ipvsh->fragoffs)
449 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
450 IP_VS_DBG(1, "frag needed for %pI6c\n", &ipv6_hdr(skb)->saddr);
451 goto err_put;
452 }
453
454 skb_dst_drop(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200455 if (noref) {
456 if (!local)
457 skb_dst_set_noref_force(skb, &rt->dst);
458 else
459 skb_dst_set(skb, dst_clone(&rt->dst));
460 } else
461 skb_dst_set(skb, &rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200462
463 return local;
464
465err_put:
Julian Anastasov026ace02013-03-21 11:58:06 +0200466 if (!noref)
467 dst_release(&rt->dst);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200468 return -1;
469
470err_unreach:
471 dst_link_failure(skb);
472 return -1;
Julius Volz38cdcc92008-09-02 15:55:44 +0200473}
474#endif
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200477/* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
478static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
479 struct ip_vs_conn *cp)
480{
481 int ret = NF_ACCEPT;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200482
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200483 skb->ipvs_property = 1;
484 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
485 ret = ip_vs_confirm_conntrack(skb);
486 if (ret == NF_ACCEPT) {
487 nf_reset(skb);
488 skb_forward_csum(skb);
489 }
490 return ret;
491}
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200492
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200493/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
494static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
495 struct ip_vs_conn *cp, int local)
496{
497 int ret = NF_STOLEN;
498
499 skb->ipvs_property = 1;
500 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
501 ip_vs_notrack(skb);
502 else
503 ip_vs_update_conntrack(skb, cp, 1);
504 if (!local) {
505 skb_forward_csum(skb);
506 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
507 dst_output);
508 } else
509 ret = NF_ACCEPT;
510 return ret;
511}
512
513/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
514static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
515 struct ip_vs_conn *cp, int local)
516{
517 int ret = NF_STOLEN;
518
519 skb->ipvs_property = 1;
520 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
521 ip_vs_notrack(skb);
522 if (!local) {
523 skb_forward_csum(skb);
524 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev,
525 dst_output);
526 } else
527 ret = NF_ACCEPT;
528 return ret;
529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531
532/*
533 * NULL transmitter (do nothing except return NF_ACCEPT)
534 */
535int
536ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200537 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 /* we do not touch skb and do not need pskb ptr */
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200540 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543
544/*
545 * Bypass transmitter
546 * Let packets bypass the destination when the destination is not
547 * available, it may be only used in transparent cache cluster.
548 */
549int
550ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200551 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700553 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 EnterFunction(10);
556
Julian Anastasov026ace02013-03-21 11:58:06 +0200557 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +0200558 if (__ip_vs_get_out_rt(skb, NULL, iph->daddr, IP_VS_RT_MODE_NON_LOCAL,
559 NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Julian Anastasov4115ded2013-03-21 11:58:05 +0200562 ip_send_check(iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Another hack: avoid icmp_send in ip_fragment */
565 skb->local_df = 1;
566
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200567 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +0200568 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 LeaveFunction(10);
571 return NF_STOLEN;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 tx_error:
574 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200575 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 LeaveFunction(10);
577 return NF_STOLEN;
578}
579
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200580#ifdef CONFIG_IP_VS_IPV6
581int
582ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200583 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200584{
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200585 EnterFunction(10);
586
Julian Anastasov026ace02013-03-21 11:58:06 +0200587 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +0200588 if (__ip_vs_get_out_rt_v6(skb, NULL, &ipvsh->daddr.in6, NULL,
589 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200590 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200591
592 /* Another hack: avoid icmp_send in ip_fragment */
593 skb->local_df = 1;
594
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200595 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +0200596 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200597
598 LeaveFunction(10);
599 return NF_STOLEN;
600
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200601 tx_error:
602 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200603 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200604 LeaveFunction(10);
605 return NF_STOLEN;
606}
607#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609/*
610 * NAT transmitter (only for outside-to-inside nat forwarding)
611 * Not used for related ICMP
612 */
613int
614ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200615 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct rtable *rt; /* Route to the other host */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200618 int local, rc, was_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 EnterFunction(10);
621
Julian Anastasov026ace02013-03-21 11:58:06 +0200622 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* check if it is a connection of no-client-port */
624 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700625 __be16 _pt, *p;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200626
627 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (p == NULL)
629 goto tx_error;
630 ip_vs_conn_fill_cport(cp, *p);
631 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
632 }
633
Julian Anastasov4115ded2013-03-21 11:58:05 +0200634 was_input = rt_is_input_route(skb_rtable(skb));
635 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
636 IP_VS_RT_MODE_LOCAL |
637 IP_VS_RT_MODE_NON_LOCAL |
638 IP_VS_RT_MODE_RDR, NULL);
639 if (local < 0)
640 goto tx_error;
641 rt = skb_rtable(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300642 /*
643 * Avoid duplicate tuple in reply direction for NAT traffic
644 * to local address when connection is sync-ed
645 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000646#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300647 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
648 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +0000649 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +0300650
651 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300652 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
653 "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300654 "stopping DNAT to local address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200655 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300656 }
657 }
658#endif
659
660 /* From world but DNAT to loopback address? */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200661 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300662 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300663 "stopping DNAT to loopback address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200664 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
667 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700668 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200669 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Changli Gaod8d1f302010-06-10 23:31:35 -0700671 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200672 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200675 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200676 goto tx_error;
Julius Volze7ade462008-09-02 15:55:33 +0200677 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700678 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Julian Anastasov0d796412010-10-17 16:46:17 +0300680 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* FIXME: when application helper enlarges the packet and the length
683 is larger than the MTU of outgoing device, there will be still
684 MTU problem. */
685
686 /* Another hack: avoid icmp_send in ip_fragment */
687 skb->local_df = 1;
688
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200689 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +0200690 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 LeaveFunction(10);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200693 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 tx_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200697 rcu_read_unlock();
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200698 LeaveFunction(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200702#ifdef CONFIG_IP_VS_IPV6
703int
704ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +0200705 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200706{
707 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200708 int local, rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200709
710 EnterFunction(10);
711
Julian Anastasov026ace02013-03-21 11:58:06 +0200712 rcu_read_lock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200713 /* check if it is a connection of no-client-port */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200714 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200715 __be16 _pt, *p;
Julian Anastasov4115ded2013-03-21 11:58:05 +0200716 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200717 if (p == NULL)
718 goto tx_error;
719 ip_vs_conn_fill_cport(cp, *p);
720 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
721 }
722
Julian Anastasov4115ded2013-03-21 11:58:05 +0200723 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
724 ipvsh, 0,
725 IP_VS_RT_MODE_LOCAL |
726 IP_VS_RT_MODE_NON_LOCAL |
727 IP_VS_RT_MODE_RDR);
728 if (local < 0)
729 goto tx_error;
730 rt = (struct rt6_info *) skb_dst(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300731 /*
732 * Avoid duplicate tuple in reply direction for NAT traffic
733 * to local address when connection is sync-ed
734 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000735#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300736 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
737 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +0000738 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +0300739
740 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300741 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300742 "ip_vs_nat_xmit_v6(): "
743 "stopping DNAT to local address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200744 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300745 }
746 }
747#endif
748
749 /* From world but DNAT to loopback address? */
750 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
751 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300752 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300753 "ip_vs_nat_xmit_v6(): "
754 "stopping DNAT to loopback address");
Julian Anastasov4115ded2013-03-21 11:58:05 +0200755 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200756 }
757
758 /* copy-on-write the packet before mangling it */
759 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200760 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200761
Changli Gaod8d1f302010-06-10 23:31:35 -0700762 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +0200763 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200764
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200765 /* mangle the packet */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200766 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200767 goto tx_error;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000768 ipv6_hdr(skb)->daddr = cp->daddr.in6;
Julian Anastasovfc604762010-10-17 16:38:15 +0300769
Julian Anastasov0d796412010-10-17 16:46:17 +0300770 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200771
772 /* FIXME: when application helper enlarges the packet and the length
773 is larger than the MTU of outgoing device, there will be still
774 MTU problem. */
775
776 /* Another hack: avoid icmp_send in ip_fragment */
777 skb->local_df = 1;
778
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200779 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +0200780 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200781
782 LeaveFunction(10);
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200783 return rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200784
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200785tx_error:
786 LeaveFunction(10);
787 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200788 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200789 return NF_STOLEN;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200790}
791#endif
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794/*
795 * IP Tunneling transmitter
796 *
797 * This function encapsulates the packet in a new IP packet, its
798 * destination will be set to cp->daddr. Most code of this function
799 * is taken from ipip.c.
800 *
801 * It is used in VS/TUN cluster. The load balancer selects a real
802 * server from a cluster based on a scheduling algorithm,
803 * encapsulates the request packet and forwards it to the selected
804 * server. For example, all real servers are configured with
805 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
806 * the encapsulated packet, it will decapsulate the packet, processe
807 * the request and return the response packets directly to the client
808 * without passing the load balancer. This can greatly increase the
809 * scalability of virtual server.
810 *
811 * Used for ANY protocol
812 */
813int
814ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200815 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Julian Anastasov3654e612012-07-20 11:59:53 +0300817 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct rtable *rt; /* Route to the other host */
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000819 __be32 saddr; /* Source for tunnel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700821 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 u8 tos = old_iph->tos;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300823 __be16 df;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700825 unsigned int max_headroom; /* The extra header space needed */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200826 int ret, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 EnterFunction(10);
829
Julian Anastasov026ace02013-03-21 11:58:06 +0200830 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +0200831 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
832 IP_VS_RT_MODE_LOCAL |
833 IP_VS_RT_MODE_NON_LOCAL |
834 IP_VS_RT_MODE_CONNECT |
835 IP_VS_RT_MODE_TUNNEL, &saddr);
836 if (local < 0)
837 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +0200838 if (local) {
839 rcu_read_unlock();
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200840 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +0200841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Julian Anastasov4115ded2013-03-21 11:58:05 +0200843 rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700844 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Julian Anastasovf2edb9f2012-07-20 11:59:52 +0300846 /* Copy DF, reset fragment offset and MF */
Julian Anastasov3654e612012-07-20 11:59:53 +0300847 df = sysctl_pmtu_disc(ipvs) ? old_iph->frag_off & htons(IP_DF) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * Okay, now see if we can stuff it in the buffer as-is.
851 */
852 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
853
Julian Anastasovf11cb2c2013-03-21 11:58:04 +0200854 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct sk_buff *new_skb =
856 skb_realloc_headroom(skb, max_headroom);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200857
858 if (!new_skb)
859 goto tx_error;
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000860 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700862 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Hans Schillstrom714f0952010-10-19 10:38:48 +0200865 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /* fix old IP header checksum */
868 ip_send_check(old_iph);
869
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700870 skb_push(skb, sizeof(struct iphdr));
871 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
875 * Push down and install the IPIP header.
876 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700877 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 iph->version = 4;
879 iph->ihl = sizeof(struct iphdr)>>2;
880 iph->frag_off = df;
881 iph->protocol = IPPROTO_IPIP;
882 iph->tos = tos;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000883 iph->daddr = cp->daddr.ip;
884 iph->saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 iph->ttl = old_iph->ttl;
Changli Gaod8d1f302010-06-10 23:31:35 -0700886 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /* Another hack: avoid icmp_send in ip_fragment */
889 skb->local_df = 1;
890
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200891 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200892 if (ret == NF_ACCEPT)
893 ip_local_out(skb);
894 else if (ret == NF_DROP)
895 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200896 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 LeaveFunction(10);
899
900 return NF_STOLEN;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 tx_error:
903 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200904 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 LeaveFunction(10);
906 return NF_STOLEN;
907}
908
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200909#ifdef CONFIG_IP_VS_IPV6
910int
911ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200912 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200913{
914 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200915 struct in6_addr saddr; /* Source for tunnel */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200916 struct net_device *tdev; /* Device to other host */
917 struct ipv6hdr *old_iph = ipv6_hdr(skb);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200918 struct ipv6hdr *iph; /* Our new IP header */
919 unsigned int max_headroom; /* The extra header space needed */
Julian Anastasov4115ded2013-03-21 11:58:05 +0200920 int ret, local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200921
922 EnterFunction(10);
923
Julian Anastasov026ace02013-03-21 11:58:06 +0200924 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +0200925 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
926 &saddr, ipvsh, 1,
927 IP_VS_RT_MODE_LOCAL |
928 IP_VS_RT_MODE_NON_LOCAL |
929 IP_VS_RT_MODE_TUNNEL);
930 if (local < 0)
931 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +0200932 if (local) {
933 rcu_read_unlock();
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200934 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +0200935 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200936
Julian Anastasov4115ded2013-03-21 11:58:05 +0200937 rt = (struct rt6_info *) skb_dst(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700938 tdev = rt->dst.dev;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200939
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200940 /*
941 * Okay, now see if we can stuff it in the buffer as-is.
942 */
943 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
944
Julian Anastasovf11cb2c2013-03-21 11:58:04 +0200945 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200946 struct sk_buff *new_skb =
947 skb_realloc_headroom(skb, max_headroom);
Julian Anastasov4115ded2013-03-21 11:58:05 +0200948
949 if (!new_skb)
950 goto tx_error;
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000951 consume_skb(skb);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200952 skb = new_skb;
953 old_iph = ipv6_hdr(skb);
954 }
955
Hans Schillstrom714f0952010-10-19 10:38:48 +0200956 skb->transport_header = skb->network_header;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200957
958 skb_push(skb, sizeof(struct ipv6hdr));
959 skb_reset_network_header(skb);
960 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
961
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200962 /*
963 * Push down and install the IPIP header.
964 */
965 iph = ipv6_hdr(skb);
966 iph->version = 6;
967 iph->nexthdr = IPPROTO_IPV6;
Harvey Harrisonb7b45f42008-11-10 16:46:06 -0800968 iph->payload_len = old_iph->payload_len;
969 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200970 iph->priority = old_iph->priority;
971 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000972 iph->daddr = cp->daddr.in6;
973 iph->saddr = saddr;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200974 iph->hop_limit = old_iph->hop_limit;
975
976 /* Another hack: avoid icmp_send in ip_fragment */
977 skb->local_df = 1;
978
Julian Anastasovb8abdf02013-03-21 11:58:01 +0200979 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200980 if (ret == NF_ACCEPT)
981 ip6_local_out(skb);
982 else if (ret == NF_DROP)
983 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200984 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200985
986 LeaveFunction(10);
987
988 return NF_STOLEN;
989
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200990tx_error:
991 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +0200992 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200993 LeaveFunction(10);
994 return NF_STOLEN;
995}
996#endif
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999/*
1000 * Direct Routing transmitter
1001 * Used for ANY protocol
1002 */
1003int
1004ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001005 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Julian Anastasov4115ded2013-03-21 11:58:05 +02001007 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 EnterFunction(10);
1010
Julian Anastasov026ace02013-03-21 11:58:06 +02001011 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001012 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1013 IP_VS_RT_MODE_LOCAL |
1014 IP_VS_RT_MODE_NON_LOCAL |
1015 IP_VS_RT_MODE_KNOWN_NH, NULL);
1016 if (local < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001018 if (local) {
1019 rcu_read_unlock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001020 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001023 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Another hack: avoid icmp_send in ip_fragment */
1026 skb->local_df = 1;
1027
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001028 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +02001029 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 LeaveFunction(10);
1032 return NF_STOLEN;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 tx_error:
1035 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001036 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 LeaveFunction(10);
1038 return NF_STOLEN;
1039}
1040
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001041#ifdef CONFIG_IP_VS_IPV6
1042int
1043ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001044 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001045{
Julian Anastasov4115ded2013-03-21 11:58:05 +02001046 int local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001047
1048 EnterFunction(10);
1049
Julian Anastasov026ace02013-03-21 11:58:06 +02001050 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001051 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1052 ipvsh, 0,
1053 IP_VS_RT_MODE_LOCAL |
1054 IP_VS_RT_MODE_NON_LOCAL);
1055 if (local < 0)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001056 goto tx_error;
Julian Anastasov026ace02013-03-21 11:58:06 +02001057 if (local) {
1058 rcu_read_unlock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001059 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
Julian Anastasov026ace02013-03-21 11:58:06 +02001060 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001061
1062 /* Another hack: avoid icmp_send in ip_fragment */
1063 skb->local_df = 1;
1064
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001065 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
Julian Anastasov026ace02013-03-21 11:58:06 +02001066 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001067
1068 LeaveFunction(10);
1069 return NF_STOLEN;
1070
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001071tx_error:
1072 kfree_skb(skb);
Julian Anastasov026ace02013-03-21 11:58:06 +02001073 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001074 LeaveFunction(10);
1075 return NF_STOLEN;
1076}
1077#endif
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080/*
1081 * ICMP packet transmitter
1082 * called by the ip_vs_in_icmp
1083 */
1084int
1085ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001086 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1087 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct rtable *rt; /* Route to the other host */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001091 int local;
Julian Anastasov4115ded2013-03-21 11:58:05 +02001092 int rt_mode, was_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 EnterFunction(10);
1095
1096 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1097 forwarded directly here, because there is no need to
1098 translate address/port back */
1099 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1100 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001101 rc = cp->packet_xmit(skb, cp, pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 else
1103 rc = NF_ACCEPT;
1104 /* do not touch skb anymore */
1105 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto out;
1107 }
1108
1109 /*
1110 * mangle and send the packet here (only for VS/NAT)
1111 */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001112 was_input = rt_is_input_route(skb_rtable(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001114 /* LOCALNODE from FORWARD hook is not supported */
1115 rt_mode = (hooknum != NF_INET_FORWARD) ?
1116 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1117 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasov026ace02013-03-21 11:58:06 +02001118 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001119 local = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip, rt_mode, NULL);
1120 if (local < 0)
1121 goto tx_error;
1122 rt = skb_rtable(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001123
1124 /*
1125 * Avoid duplicate tuple in reply direction for NAT traffic
1126 * to local address when connection is sync-ed
1127 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001128#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001129 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1130 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +00001131 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +03001132
1133 if (ct && !nf_ct_is_untracked(ct)) {
1134 IP_VS_DBG(10, "%s(): "
1135 "stopping DNAT to local address %pI4\n",
1136 __func__, &cp->daddr.ip);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001137 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +03001138 }
1139 }
1140#endif
1141
1142 /* From world but DNAT to loopback address? */
Julian Anastasov4115ded2013-03-21 11:58:05 +02001143 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001144 IP_VS_DBG(1, "%s(): "
1145 "stopping DNAT to loopback %pI4\n",
1146 __func__, &cp->daddr.ip);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001147 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -07001151 if (!skb_make_writable(skb, offset))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001152 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Changli Gaod8d1f302010-06-10 23:31:35 -07001154 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001155 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ip_vs_nat_icmp(skb, pp, cp, 0);
1158
1159 /* Another hack: avoid icmp_send in ip_fragment */
1160 skb->local_df = 1;
1161
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001162 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +02001163 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto out;
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 tx_error:
Julian Anastasov026ace02013-03-21 11:58:06 +02001167 kfree_skb(skb);
1168 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 rc = NF_STOLEN;
1170 out:
1171 LeaveFunction(10);
1172 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001174
1175#ifdef CONFIG_IP_VS_IPV6
1176int
1177ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001178 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
Julian Anastasov4115ded2013-03-21 11:58:05 +02001179 struct ip_vs_iphdr *ipvsh)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001180{
1181 struct rt6_info *rt; /* Route to the other host */
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001182 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001183 int local;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001184 int rt_mode;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001185
1186 EnterFunction(10);
1187
1188 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1189 forwarded directly here, because there is no need to
1190 translate address/port back */
1191 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1192 if (cp->packet_xmit)
Julian Anastasov4115ded2013-03-21 11:58:05 +02001193 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001194 else
1195 rc = NF_ACCEPT;
1196 /* do not touch skb anymore */
1197 atomic_inc(&cp->in_pkts);
1198 goto out;
1199 }
1200
1201 /*
1202 * mangle and send the packet here (only for VS/NAT)
1203 */
1204
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001205 /* LOCALNODE from FORWARD hook is not supported */
1206 rt_mode = (hooknum != NF_INET_FORWARD) ?
1207 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1208 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasov026ace02013-03-21 11:58:06 +02001209 rcu_read_lock();
Julian Anastasov4115ded2013-03-21 11:58:05 +02001210 local = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1211 ipvsh, 0, rt_mode);
1212 if (local < 0)
1213 goto tx_error;
1214 rt = (struct rt6_info *) skb_dst(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001215 /*
1216 * Avoid duplicate tuple in reply direction for NAT traffic
1217 * to local address when connection is sync-ed
1218 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001219#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001220 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1221 enum ip_conntrack_info ctinfo;
Alan Cox05b4b062012-10-26 00:13:27 +00001222 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
Julian Anastasovfc604762010-10-17 16:38:15 +03001223
1224 if (ct && !nf_ct_is_untracked(ct)) {
1225 IP_VS_DBG(10, "%s(): "
1226 "stopping DNAT to local address %pI6\n",
1227 __func__, &cp->daddr.in6);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001228 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +03001229 }
1230 }
1231#endif
1232
1233 /* From world but DNAT to loopback address? */
1234 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1235 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1236 IP_VS_DBG(1, "%s(): "
1237 "stopping DNAT to loopback %pI6\n",
1238 __func__, &cp->daddr.in6);
Julian Anastasov4115ded2013-03-21 11:58:05 +02001239 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001240 }
1241
1242 /* copy-on-write the packet before mangling it */
1243 if (!skb_make_writable(skb, offset))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001244 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001245
Changli Gaod8d1f302010-06-10 23:31:35 -07001246 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julian Anastasov4115ded2013-03-21 11:58:05 +02001247 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001248
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001249 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1250
1251 /* Another hack: avoid icmp_send in ip_fragment */
1252 skb->local_df = 1;
1253
Julian Anastasovb8abdf02013-03-21 11:58:01 +02001254 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
Julian Anastasov026ace02013-03-21 11:58:06 +02001255 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001256 goto out;
1257
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001258tx_error:
Julian Anastasov026ace02013-03-21 11:58:06 +02001259 kfree_skb(skb);
1260 rcu_read_unlock();
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001261 rc = NF_STOLEN;
1262out:
1263 LeaveFunction(10);
1264 return rc;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001265}
1266#endif