blob: 5d393c5a802d6ca844bb4e9489bc1806761a467e [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
20 * LOCAL_OUT rules:
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Hannes Eder9aada7a2009-07-30 14:29:44 -070026#define KMSG_COMPONENT "IPVS"
27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/tcp.h> /* for tcphdr */
Herbert Xuc439cb22008-01-11 19:14:00 -080032#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <net/tcp.h> /* for csum_tcpudp_magic */
34#include <net/udp.h>
35#include <net/icmp.h> /* for icmp_send */
36#include <net/route.h> /* for ip_route_output */
Julius Volz38cdcc92008-09-02 15:55:44 +020037#include <net/ipv6.h>
38#include <net/ip6_route.h>
Hans Schillstrom714f0952010-10-19 10:38:48 +020039#include <net/addrconf.h>
Julius Volz38cdcc92008-09-02 15:55:44 +020040#include <linux/icmpv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/netfilter.h>
42#include <linux/netfilter_ipv4.h>
43
44#include <net/ip_vs.h>
45
Changli Gao17a8f8e2011-02-24 08:19:57 +080046enum {
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
50 * local
51 */
52};
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * Destination cache to speed up outgoing route lookup
56 */
57static inline void
Hans Schillstrom714f0952010-10-19 10:38:48 +020058__ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst,
59 u32 dst_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct dst_entry *old_dst;
62
63 old_dst = dest->dst_cache;
64 dest->dst_cache = dst;
65 dest->dst_rtos = rtos;
Hans Schillstrom714f0952010-10-19 10:38:48 +020066 dest->dst_cookie = dst_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 dst_release(old_dst);
68}
69
70static inline struct dst_entry *
Hans Schillstrom714f0952010-10-19 10:38:48 +020071__ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct dst_entry *dst = dest->dst_cache;
74
75 if (!dst)
76 return NULL;
Hans Schillstrom714f0952010-10-19 10:38:48 +020077 if ((dst->obsolete || rtos != dest->dst_rtos) &&
78 dst->ops->check(dst, dest->dst_cookie) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 dest->dst_cache = NULL;
80 dst_release(dst);
81 return NULL;
82 }
83 dst_hold(dst);
84 return dst;
85}
86
Changli Gao17a8f8e2011-02-24 08:19:57 +080087/* Get route to destination or remote server */
Ilpo Järvinenad1b30b2008-01-05 23:12:40 -080088static struct rtable *
Julian Anastasovfc604762010-10-17 16:38:15 +030089__ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
90 __be32 daddr, u32 rtos, int rt_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Julian Anastasovfc604762010-10-17 16:38:15 +030092 struct net *net = dev_net(skb_dst(skb)->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct rtable *rt; /* Route to the other host */
Julian Anastasovfc604762010-10-17 16:38:15 +030094 struct rtable *ort; /* Original route */
95 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (dest) {
98 spin_lock(&dest->dst_lock);
99 if (!(rt = (struct rtable *)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200100 __ip_vs_dst_check(dest, rtos))) {
David S. Miller78fbfd82011-03-12 00:00:52 -0500101 rt = ip_route_output(net, dest->addr.ip, 0, rtos, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800102 if (IS_ERR(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 spin_unlock(&dest->dst_lock);
Harvey Harrison14d5e832008-10-31 00:54:29 -0700104 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
105 &dest->addr.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return NULL;
107 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200108 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
Harvey Harrison14d5e832008-10-31 00:54:29 -0700109 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
110 &dest->addr.ip,
Changli Gaod8d1f302010-06-10 23:31:35 -0700111 atomic_read(&rt->dst.__refcnt), rtos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 spin_unlock(&dest->dst_lock);
114 } else {
David S. Miller78fbfd82011-03-12 00:00:52 -0500115 rt = ip_route_output(net, daddr, 0, rtos, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800116 if (IS_ERR(rt)) {
Harvey Harrison14d5e832008-10-31 00:54:29 -0700117 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
Julian Anastasovfc604762010-10-17 16:38:15 +0300118 &daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return NULL;
120 }
121 }
122
Julian Anastasovfc604762010-10-17 16:38:15 +0300123 local = rt->rt_flags & RTCF_LOCAL;
Changli Gao17a8f8e2011-02-24 08:19:57 +0800124 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
125 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300126 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);
129 ip_rt_put(rt);
130 return NULL;
131 }
Changli Gao17a8f8e2011-02-24 08:19:57 +0800132 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
133 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300134 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);
137 ip_rt_put(rt);
138 return NULL;
139 }
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);
144 ip_rt_put(rt);
145 return NULL;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return rt;
149}
150
Julian Anastasovfc604762010-10-17 16:38:15 +0300151/* Reroute packet to local IPv4 stack after DNAT */
152static int
153__ip_vs_reroute_locally(struct sk_buff *skb)
154{
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);
159
David S. Millerc7537962010-11-11 17:07:48 -0800160 if (rt_is_input_route(rt)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300161 unsigned long orefdst = skb->_skb_refdst;
162
163 if (ip_route_input(skb, iph->daddr, iph->saddr,
164 iph->tos, skb->dev))
165 return 0;
166 refdst_drop(orefdst);
167 } else {
David S. Miller9d6ec932011-03-12 01:12:47 -0500168 struct flowi4 fl4 = {
169 .daddr = iph->daddr,
170 .saddr = iph->saddr,
171 .flowi4_tos = RT_TOS(iph->tos),
172 .flowi4_mark = skb->mark,
Julian Anastasovfc604762010-10-17 16:38:15 +0300173 };
Julian Anastasovfc604762010-10-17 16:38:15 +0300174
David S. Miller9d6ec932011-03-12 01:12:47 -0500175 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800176 if (IS_ERR(rt))
Julian Anastasovfc604762010-10-17 16:38:15 +0300177 return 0;
178 if (!(rt->rt_flags & RTCF_LOCAL)) {
179 ip_rt_put(rt);
180 return 0;
181 }
182 /* Drop old route. */
183 skb_dst_drop(skb);
184 skb_dst_set(skb, &rt->dst);
185 }
186 return 1;
187}
188
Julius Volz38cdcc92008-09-02 15:55:44 +0200189#ifdef CONFIG_IP_VS_IPV6
Hans Schillstrom714f0952010-10-19 10:38:48 +0200190
Julian Anastasovfc604762010-10-17 16:38:15 +0300191static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
192{
193 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
194}
195
Hans Schillstrom714f0952010-10-19 10:38:48 +0200196static 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)
Julius Volz38cdcc92008-09-02 15:55:44 +0200199{
Hans Schillstrom714f0952010-10-19 10:38:48 +0200200 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500201 struct flowi6 fl6 = {
202 .daddr = *daddr,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200203 };
204
David S. Miller4c9483b2011-03-12 16:22:43 -0500205 dst = ip6_route_output(net, NULL, &fl6);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200206 if (dst->error)
207 goto out_err;
208 if (!ret_saddr)
209 return dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500210 if (ipv6_addr_any(&fl6.saddr) &&
Hans Schillstrom714f0952010-10-19 10:38:48 +0200211 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
David S. Miller4c9483b2011-03-12 16:22:43 -0500212 &fl6.daddr, 0, &fl6.saddr) < 0)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200213 goto out_err;
David S. Miller452edd52011-03-02 13:27:41 -0800214 if (do_xfrm) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500215 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -0800216 if (IS_ERR(dst)) {
217 dst = NULL;
218 goto out_err;
219 }
220 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500221 ipv6_addr_copy(ret_saddr, &fl6.saddr);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200222 return dst;
223
224out_err:
225 dst_release(dst);
226 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
227 return NULL;
228}
229
Julian Anastasovfc604762010-10-17 16:38:15 +0300230/*
231 * Get route to destination or remote server
Julian Anastasovfc604762010-10-17 16:38:15 +0300232 */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200233static struct rt6_info *
Julian Anastasovfc604762010-10-17 16:38:15 +0300234__ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
235 struct in6_addr *daddr, struct in6_addr *ret_saddr,
236 int do_xfrm, int rt_mode)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200237{
Julian Anastasovfc604762010-10-17 16:38:15 +0300238 struct net *net = dev_net(skb_dst(skb)->dev);
Julius Volz38cdcc92008-09-02 15:55:44 +0200239 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovfc604762010-10-17 16:38:15 +0300240 struct rt6_info *ort; /* Original route */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200241 struct dst_entry *dst;
Julian Anastasovfc604762010-10-17 16:38:15 +0300242 int local;
Julius Volz38cdcc92008-09-02 15:55:44 +0200243
244 if (dest) {
245 spin_lock(&dest->dst_lock);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200246 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
Julius Volz38cdcc92008-09-02 15:55:44 +0200247 if (!rt) {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200248 u32 cookie;
Julius Volz38cdcc92008-09-02 15:55:44 +0200249
Hans Schillstrom714f0952010-10-19 10:38:48 +0200250 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
251 &dest->dst_saddr,
252 do_xfrm);
253 if (!dst) {
Julius Volz38cdcc92008-09-02 15:55:44 +0200254 spin_unlock(&dest->dst_lock);
Julius Volz38cdcc92008-09-02 15:55:44 +0200255 return NULL;
256 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200257 rt = (struct rt6_info *) dst;
258 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
259 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
260 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
261 &dest->addr.in6, &dest->dst_saddr,
Changli Gaod8d1f302010-06-10 23:31:35 -0700262 atomic_read(&rt->dst.__refcnt));
Julius Volz38cdcc92008-09-02 15:55:44 +0200263 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200264 if (ret_saddr)
265 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
Julius Volz38cdcc92008-09-02 15:55:44 +0200266 spin_unlock(&dest->dst_lock);
267 } else {
Julian Anastasovfc604762010-10-17 16:38:15 +0300268 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200269 if (!dst)
Julius Volz38cdcc92008-09-02 15:55:44 +0200270 return NULL;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200271 rt = (struct rt6_info *) dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200272 }
273
Julian Anastasovfc604762010-10-17 16:38:15 +0300274 local = __ip_vs_is_local_route6(rt);
David S. Millere58b3442011-05-12 18:22:34 -0400275 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
276 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300277 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
278 local ? "local":"non-local", daddr);
279 dst_release(&rt->dst);
280 return NULL;
281 }
David S. Millere58b3442011-05-12 18:22:34 -0400282 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
Julian Anastasovfc604762010-10-17 16:38:15 +0300283 !((ort = (struct rt6_info *) skb_dst(skb)) &&
284 __ip_vs_is_local_route6(ort))) {
285 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
286 "requires NAT method, dest: %pI6\n",
287 &ipv6_hdr(skb)->daddr, daddr);
288 dst_release(&rt->dst);
289 return NULL;
290 }
291 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
292 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
293 IPV6_ADDR_LOOPBACK)) {
294 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
295 "to non-local address, dest: %pI6\n",
296 &ipv6_hdr(skb)->saddr, daddr);
297 dst_release(&rt->dst);
298 return NULL;
299 }
300
Julius Volz38cdcc92008-09-02 15:55:44 +0200301 return rt;
302}
303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/*
307 * Release dest->dst_cache before a dest is removed
308 */
309void
310ip_vs_dst_reset(struct ip_vs_dest *dest)
311{
312 struct dst_entry *old_dst;
313
314 old_dst = dest->dst_cache;
315 dest->dst_cache = NULL;
316 dst_release(old_dst);
317}
318
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200319#define IP_VS_XMIT_TUNNEL(skb, cp) \
320({ \
321 int __ret = NF_ACCEPT; \
322 \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300323 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200324 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
325 __ret = ip_vs_confirm_conntrack(skb, cp); \
326 if (__ret == NF_ACCEPT) { \
327 nf_reset(skb); \
Julian Anastasov4256f1a2010-10-17 16:29:40 +0300328 skb_forward_csum(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200329 } \
330 __ret; \
331})
332
Julian Anastasovfc604762010-10-17 16:38:15 +0300333#define IP_VS_XMIT_NAT(pf, skb, cp, local) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334do { \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300335 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200336 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300337 ip_vs_notrack(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200338 else \
339 ip_vs_update_conntrack(skb, cp, 1); \
Julian Anastasovfc604762010-10-17 16:38:15 +0300340 if (local) \
341 return NF_ACCEPT; \
Herbert Xuccc79112007-07-30 16:20:12 -0700342 skb_forward_csum(skb); \
Julius Volz38cdcc92008-09-02 15:55:44 +0200343 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200344 skb_dst(skb)->dev, dst_output); \
345} while (0)
346
Julian Anastasovfc604762010-10-17 16:38:15 +0300347#define IP_VS_XMIT(pf, skb, cp, local) \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200348do { \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300349 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200350 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300351 ip_vs_notrack(skb); \
Julian Anastasovfc604762010-10-17 16:38:15 +0300352 if (local) \
353 return NF_ACCEPT; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200354 skb_forward_csum(skb); \
355 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
356 skb_dst(skb)->dev, dst_output); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357} while (0)
358
359
360/*
361 * NULL transmitter (do nothing except return NF_ACCEPT)
362 */
363int
364ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
365 struct ip_vs_protocol *pp)
366{
367 /* we do not touch skb and do not need pskb ptr */
Julian Anastasovfc604762010-10-17 16:38:15 +0300368 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371
372/*
373 * Bypass transmitter
374 * Let packets bypass the destination when the destination is not
375 * available, it may be only used in transparent cache cluster.
376 */
377int
378ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
379 struct ip_vs_protocol *pp)
380{
381 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700382 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 EnterFunction(10);
386
Changli Gao17a8f8e2011-02-24 08:19:57 +0800387 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
388 IP_VS_RT_MODE_NON_LOCAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto tx_error_icmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700392 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900393 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
394 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ip_rt_put(rt);
396 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000397 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto tx_error;
399 }
400
401 /*
402 * Call ip_send_check because we are not sure it is called
403 * after ip_defrag. Is copy-on-write needed?
404 */
405 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
406 ip_rt_put(rt);
407 return NF_STOLEN;
408 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700409 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000412 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700413 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Another hack: avoid icmp_send in ip_fragment */
416 skb->local_df = 1;
417
Julian Anastasovfc604762010-10-17 16:38:15 +0300418 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 LeaveFunction(10);
421 return NF_STOLEN;
422
423 tx_error_icmp:
424 dst_link_failure(skb);
425 tx_error:
426 kfree_skb(skb);
427 LeaveFunction(10);
428 return NF_STOLEN;
429}
430
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200431#ifdef CONFIG_IP_VS_IPV6
432int
433ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
434 struct ip_vs_protocol *pp)
435{
436 struct rt6_info *rt; /* Route to the other host */
437 struct ipv6hdr *iph = ipv6_hdr(skb);
438 int mtu;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200439
440 EnterFunction(10);
441
David S. Millere58b3442011-05-12 18:22:34 -0400442 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0,
443 IP_VS_RT_MODE_NON_LOCAL)))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200444 goto tx_error_icmp;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200445
446 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700447 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900448 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300449 if (!skb->dev) {
450 struct net *net = dev_net(skb_dst(skb)->dev);
451
452 skb->dev = net->loopback_dev;
453 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000454 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Julian Anastasovcb591552010-10-17 16:40:51 +0300455 dst_release(&rt->dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000456 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200457 goto tx_error;
458 }
459
460 /*
461 * Call ip_send_check because we are not sure it is called
462 * after ip_defrag. Is copy-on-write needed?
463 */
464 skb = skb_share_check(skb, GFP_ATOMIC);
465 if (unlikely(skb == NULL)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700466 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200467 return NF_STOLEN;
468 }
469
470 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000471 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700472 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200473
474 /* Another hack: avoid icmp_send in ip_fragment */
475 skb->local_df = 1;
476
Julian Anastasovfc604762010-10-17 16:38:15 +0300477 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200478
479 LeaveFunction(10);
480 return NF_STOLEN;
481
482 tx_error_icmp:
483 dst_link_failure(skb);
484 tx_error:
485 kfree_skb(skb);
486 LeaveFunction(10);
487 return NF_STOLEN;
488}
489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491/*
492 * NAT transmitter (only for outside-to-inside nat forwarding)
493 * Not used for related ICMP
494 */
495int
496ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
497 struct ip_vs_protocol *pp)
498{
499 struct rtable *rt; /* Route to the other host */
500 int mtu;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700501 struct iphdr *iph = ip_hdr(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300502 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 EnterFunction(10);
505
506 /* check if it is a connection of no-client-port */
507 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700508 __be16 _pt, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
510 if (p == NULL)
511 goto tx_error;
512 ip_vs_conn_fill_cport(cp, *p);
513 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
514 }
515
Julian Anastasovfc604762010-10-17 16:38:15 +0300516 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800517 RT_TOS(iph->tos),
518 IP_VS_RT_MODE_LOCAL |
519 IP_VS_RT_MODE_NON_LOCAL |
520 IP_VS_RT_MODE_RDR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300522 local = rt->rt_flags & RTCF_LOCAL;
523 /*
524 * Avoid duplicate tuple in reply direction for NAT traffic
525 * to local address when connection is sync-ed
526 */
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);
531
532 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300533 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
534 "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300535 "stopping DNAT to local address");
536 goto tx_error_put;
537 }
538 }
539#endif
540
541 /* From world but DNAT to loopback address? */
David S. Millerc7537962010-11-11 17:07:48 -0800542 if (local && ipv4_is_loopback(rt->rt_dst) &&
543 rt_is_input_route(skb_rtable(skb))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300544 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300545 "stopping DNAT to loopback address");
546 goto tx_error_put;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700550 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900551 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
552 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Julian Anastasov0d796412010-10-17 16:46:17 +0300554 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
555 "ip_vs_nat_xmit(): frag needed for");
Julian Anastasovfc604762010-10-17 16:38:15 +0300556 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700560 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto tx_error_put;
562
Changli Gaod8d1f302010-06-10 23:31:35 -0700563 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto tx_error_put;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* mangle the packet */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700567 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
Julian Anastasovfc604762010-10-17 16:38:15 +0300568 goto tx_error_put;
Julius Volze7ade462008-09-02 15:55:33 +0200569 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700570 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Julian Anastasovfc604762010-10-17 16:38:15 +0300572 if (!local) {
573 /* drop old route */
574 skb_dst_drop(skb);
575 skb_dst_set(skb, &rt->dst);
576 } else {
577 ip_rt_put(rt);
578 /*
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.
582 */
583 if (!__ip_vs_reroute_locally(skb))
584 goto tx_error;
585 }
586
Julian Anastasov0d796412010-10-17 16:46:17 +0300587 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* FIXME: when application helper enlarges the packet and the length
590 is larger than the MTU of outgoing device, there will be still
591 MTU problem. */
592
593 /* Another hack: avoid icmp_send in ip_fragment */
594 skb->local_df = 1;
595
Julian Anastasovfc604762010-10-17 16:38:15 +0300596 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 LeaveFunction(10);
599 return NF_STOLEN;
600
601 tx_error_icmp:
602 dst_link_failure(skb);
603 tx_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200605 LeaveFunction(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return NF_STOLEN;
607 tx_error_put:
608 ip_rt_put(rt);
609 goto tx_error;
610}
611
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200612#ifdef CONFIG_IP_VS_IPV6
613int
614ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
615 struct ip_vs_protocol *pp)
616{
617 struct rt6_info *rt; /* Route to the other host */
618 int mtu;
Julian Anastasovfc604762010-10-17 16:38:15 +0300619 int local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200620
621 EnterFunction(10);
622
623 /* check if it is a connection of no-client-port */
624 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
625 __be16 _pt, *p;
626 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
627 sizeof(_pt), &_pt);
628 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 Anastasovfc604762010-10-17 16:38:15 +0300634 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -0400635 0, (IP_VS_RT_MODE_LOCAL |
636 IP_VS_RT_MODE_NON_LOCAL |
637 IP_VS_RT_MODE_RDR))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200638 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300639 local = __ip_vs_is_local_route6(rt);
640 /*
641 * Avoid duplicate tuple in reply direction for NAT traffic
642 * to local address when connection is sync-ed
643 */
644#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
645 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
646 enum ip_conntrack_info ctinfo;
647 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
648
649 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300650 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300651 "ip_vs_nat_xmit_v6(): "
652 "stopping DNAT to local address");
653 goto tx_error_put;
654 }
655 }
656#endif
657
658 /* From world but DNAT to loopback address? */
659 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
660 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300661 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300662 "ip_vs_nat_xmit_v6(): "
663 "stopping DNAT to loopback address");
664 goto tx_error_put;
665 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200666
667 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700668 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900669 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300670 if (!skb->dev) {
671 struct net *net = dev_net(skb_dst(skb)->dev);
672
673 skb->dev = net->loopback_dev;
674 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000675 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Julian Anastasov0d796412010-10-17 16:46:17 +0300676 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200677 "ip_vs_nat_xmit_v6(): frag needed for");
Julian Anastasovfc604762010-10-17 16:38:15 +0300678 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200679 }
680
681 /* copy-on-write the packet before mangling it */
682 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
683 goto tx_error_put;
684
Changli Gaod8d1f302010-06-10 23:31:35 -0700685 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200686 goto tx_error_put;
687
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200688 /* mangle the packet */
689 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
690 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300691 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
692
693 if (!local || !skb->dev) {
694 /* drop the old route when skb is not shared */
695 skb_dst_drop(skb);
696 skb_dst_set(skb, &rt->dst);
697 } else {
698 /* destined to loopback, do we need to change route? */
699 dst_release(&rt->dst);
700 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200701
Julian Anastasov0d796412010-10-17 16:46:17 +0300702 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200703
704 /* FIXME: when application helper enlarges the packet and the length
705 is larger than the MTU of outgoing device, there will be still
706 MTU problem. */
707
708 /* Another hack: avoid icmp_send in ip_fragment */
709 skb->local_df = 1;
710
Julian Anastasovfc604762010-10-17 16:38:15 +0300711 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200712
713 LeaveFunction(10);
714 return NF_STOLEN;
715
716tx_error_icmp:
717 dst_link_failure(skb);
718tx_error:
719 LeaveFunction(10);
720 kfree_skb(skb);
721 return NF_STOLEN;
722tx_error_put:
Changli Gaod8d1f302010-06-10 23:31:35 -0700723 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200724 goto tx_error;
725}
726#endif
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729/*
730 * IP Tunneling transmitter
731 *
732 * This function encapsulates the packet in a new IP packet, its
733 * destination will be set to cp->daddr. Most code of this function
734 * is taken from ipip.c.
735 *
736 * It is used in VS/TUN cluster. The load balancer selects a real
737 * server from a cluster based on a scheduling algorithm,
738 * encapsulates the request packet and forwards it to the selected
739 * server. For example, all real servers are configured with
740 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
741 * the encapsulated packet, it will decapsulate the packet, processe
742 * the request and return the response packets directly to the client
743 * without passing the load balancer. This can greatly increase the
744 * scalability of virtual server.
745 *
746 * Used for ANY protocol
747 */
748int
749ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
750 struct ip_vs_protocol *pp)
751{
752 struct rtable *rt; /* Route to the other host */
753 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700754 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 u8 tos = old_iph->tos;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800756 __be16 df = old_iph->frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700758 unsigned int max_headroom; /* The extra header space needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int mtu;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200760 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 EnterFunction(10);
763
Julian Anastasovfc604762010-10-17 16:38:15 +0300764 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800765 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
766 IP_VS_RT_MODE_NON_LOCAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300768 if (rt->rt_flags & RTCF_LOCAL) {
769 ip_rt_put(rt);
770 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Changli Gaod8d1f302010-06-10 23:31:35 -0700773 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Changli Gaod8d1f302010-06-10 23:31:35 -0700775 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (mtu < 68) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000777 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300778 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000780 if (skb_dst(skb))
781 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900783 df |= (old_iph->frag_off & htons(IP_DF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Simon Horman8f1b03a2010-11-09 10:08:49 +0900785 if ((old_iph->frag_off & htons(IP_DF) &&
786 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000788 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300789 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 /*
793 * Okay, now see if we can stuff it in the buffer as-is.
794 */
795 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
796
797 if (skb_headroom(skb) < max_headroom
798 || skb_cloned(skb) || skb_shared(skb)) {
799 struct sk_buff *new_skb =
800 skb_realloc_headroom(skb, max_headroom);
801 if (!new_skb) {
802 ip_rt_put(rt);
803 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000804 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return NF_STOLEN;
806 }
807 kfree_skb(skb);
808 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700809 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
Hans Schillstrom714f0952010-10-19 10:38:48 +0200812 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* fix old IP header checksum */
815 ip_send_check(old_iph);
816
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700817 skb_push(skb, sizeof(struct iphdr));
818 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
820
821 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000822 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700823 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /*
826 * Push down and install the IPIP header.
827 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700828 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 iph->version = 4;
830 iph->ihl = sizeof(struct iphdr)>>2;
831 iph->frag_off = df;
832 iph->protocol = IPPROTO_IPIP;
833 iph->tos = tos;
834 iph->daddr = rt->rt_dst;
835 iph->saddr = rt->rt_src;
836 iph->ttl = old_iph->ttl;
Changli Gaod8d1f302010-06-10 23:31:35 -0700837 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* Another hack: avoid icmp_send in ip_fragment */
840 skb->local_df = 1;
841
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200842 ret = IP_VS_XMIT_TUNNEL(skb, cp);
843 if (ret == NF_ACCEPT)
844 ip_local_out(skb);
845 else if (ret == NF_DROP)
846 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 LeaveFunction(10);
849
850 return NF_STOLEN;
851
852 tx_error_icmp:
853 dst_link_failure(skb);
854 tx_error:
855 kfree_skb(skb);
856 LeaveFunction(10);
857 return NF_STOLEN;
Julian Anastasovfc604762010-10-17 16:38:15 +0300858tx_error_put:
859 ip_rt_put(rt);
860 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200863#ifdef CONFIG_IP_VS_IPV6
864int
865ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
866 struct ip_vs_protocol *pp)
867{
868 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200869 struct in6_addr saddr; /* Source for tunnel */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200870 struct net_device *tdev; /* Device to other host */
871 struct ipv6hdr *old_iph = ipv6_hdr(skb);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200872 struct ipv6hdr *iph; /* Our new IP header */
873 unsigned int max_headroom; /* The extra header space needed */
874 int mtu;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200875 int ret;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200876
877 EnterFunction(10);
878
Julian Anastasovfc604762010-10-17 16:38:15 +0300879 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
David S. Millere58b3442011-05-12 18:22:34 -0400880 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
881 IP_VS_RT_MODE_NON_LOCAL))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200882 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300883 if (__ip_vs_is_local_route6(rt)) {
884 dst_release(&rt->dst);
885 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
886 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200887
Changli Gaod8d1f302010-06-10 23:31:35 -0700888 tdev = rt->dst.dev;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200889
Changli Gaod8d1f302010-06-10 23:31:35 -0700890 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200891 if (mtu < IPV6_MIN_MTU) {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200892 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
893 IPV6_MIN_MTU);
Julian Anastasovfc604762010-10-17 16:38:15 +0300894 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200895 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000896 if (skb_dst(skb))
897 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200898
Simon Horman8f1b03a2010-11-09 10:08:49 +0900899 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
900 !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300901 if (!skb->dev) {
902 struct net *net = dev_net(skb_dst(skb)->dev);
903
904 skb->dev = net->loopback_dev;
905 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000906 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000907 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300908 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200909 }
910
911 /*
912 * Okay, now see if we can stuff it in the buffer as-is.
913 */
914 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
915
916 if (skb_headroom(skb) < max_headroom
917 || skb_cloned(skb) || skb_shared(skb)) {
918 struct sk_buff *new_skb =
919 skb_realloc_headroom(skb, max_headroom);
920 if (!new_skb) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700921 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200922 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000923 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200924 return NF_STOLEN;
925 }
926 kfree_skb(skb);
927 skb = new_skb;
928 old_iph = ipv6_hdr(skb);
929 }
930
Hans Schillstrom714f0952010-10-19 10:38:48 +0200931 skb->transport_header = skb->network_header;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200932
933 skb_push(skb, sizeof(struct ipv6hdr));
934 skb_reset_network_header(skb);
935 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
936
937 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000938 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700939 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200940
941 /*
942 * Push down and install the IPIP header.
943 */
944 iph = ipv6_hdr(skb);
945 iph->version = 6;
946 iph->nexthdr = IPPROTO_IPV6;
Harvey Harrisonb7b45f42008-11-10 16:46:06 -0800947 iph->payload_len = old_iph->payload_len;
948 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200949 iph->priority = old_iph->priority;
950 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
Hans Schillstrom714f0952010-10-19 10:38:48 +0200951 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
952 ipv6_addr_copy(&iph->saddr, &saddr);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200953 iph->hop_limit = old_iph->hop_limit;
954
955 /* Another hack: avoid icmp_send in ip_fragment */
956 skb->local_df = 1;
957
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200958 ret = IP_VS_XMIT_TUNNEL(skb, cp);
959 if (ret == NF_ACCEPT)
960 ip6_local_out(skb);
961 else if (ret == NF_DROP)
962 kfree_skb(skb);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200963
964 LeaveFunction(10);
965
966 return NF_STOLEN;
967
968tx_error_icmp:
969 dst_link_failure(skb);
970tx_error:
971 kfree_skb(skb);
972 LeaveFunction(10);
973 return NF_STOLEN;
Julian Anastasovfc604762010-10-17 16:38:15 +0300974tx_error_put:
975 dst_release(&rt->dst);
976 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200977}
978#endif
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981/*
982 * Direct Routing transmitter
983 * Used for ANY protocol
984 */
985int
986ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
987 struct ip_vs_protocol *pp)
988{
989 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700990 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 int mtu;
992
993 EnterFunction(10);
994
Julian Anastasovfc604762010-10-17 16:38:15 +0300995 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800996 RT_TOS(iph->tos),
997 IP_VS_RT_MODE_LOCAL |
998 IP_VS_RT_MODE_NON_LOCAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001000 if (rt->rt_flags & RTCF_LOCAL) {
1001 ip_rt_put(rt);
1002 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001006 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001007 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1008 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1010 ip_rt_put(rt);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001011 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto tx_error;
1013 }
1014
1015 /*
1016 * Call ip_send_check because we are not sure it is called
1017 * after ip_defrag. Is copy-on-write needed?
1018 */
1019 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1020 ip_rt_put(rt);
1021 return NF_STOLEN;
1022 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001023 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +00001026 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001027 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /* Another hack: avoid icmp_send in ip_fragment */
1030 skb->local_df = 1;
1031
Julian Anastasovfc604762010-10-17 16:38:15 +03001032 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 LeaveFunction(10);
1035 return NF_STOLEN;
1036
1037 tx_error_icmp:
1038 dst_link_failure(skb);
1039 tx_error:
1040 kfree_skb(skb);
1041 LeaveFunction(10);
1042 return NF_STOLEN;
1043}
1044
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001045#ifdef CONFIG_IP_VS_IPV6
1046int
1047ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1048 struct ip_vs_protocol *pp)
1049{
1050 struct rt6_info *rt; /* Route to the other host */
1051 int mtu;
1052
1053 EnterFunction(10);
1054
Julian Anastasovfc604762010-10-17 16:38:15 +03001055 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -04001056 0, (IP_VS_RT_MODE_LOCAL |
1057 IP_VS_RT_MODE_NON_LOCAL))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001058 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001059 if (__ip_vs_is_local_route6(rt)) {
1060 dst_release(&rt->dst);
1061 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1062 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001063
1064 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001065 mtu = dst_mtu(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001066 if (skb->len > mtu) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001067 if (!skb->dev) {
1068 struct net *net = dev_net(skb_dst(skb)->dev);
1069
1070 skb->dev = net->loopback_dev;
1071 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001072 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Changli Gaod8d1f302010-06-10 23:31:35 -07001073 dst_release(&rt->dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001074 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001075 goto tx_error;
1076 }
1077
1078 /*
1079 * Call ip_send_check because we are not sure it is called
1080 * after ip_defrag. Is copy-on-write needed?
1081 */
1082 skb = skb_share_check(skb, GFP_ATOMIC);
1083 if (unlikely(skb == NULL)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001084 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001085 return NF_STOLEN;
1086 }
1087
1088 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +00001089 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001090 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001091
1092 /* Another hack: avoid icmp_send in ip_fragment */
1093 skb->local_df = 1;
1094
Julian Anastasovfc604762010-10-17 16:38:15 +03001095 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001096
1097 LeaveFunction(10);
1098 return NF_STOLEN;
1099
1100tx_error_icmp:
1101 dst_link_failure(skb);
1102tx_error:
1103 kfree_skb(skb);
1104 LeaveFunction(10);
1105 return NF_STOLEN;
1106}
1107#endif
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110/*
1111 * ICMP packet transmitter
1112 * called by the ip_vs_in_icmp
1113 */
1114int
1115ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1116 struct ip_vs_protocol *pp, int offset)
1117{
1118 struct rtable *rt; /* Route to the other host */
1119 int mtu;
1120 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001121 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 EnterFunction(10);
1124
1125 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1126 forwarded directly here, because there is no need to
1127 translate address/port back */
1128 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1129 if (cp->packet_xmit)
1130 rc = cp->packet_xmit(skb, cp, pp);
1131 else
1132 rc = NF_ACCEPT;
1133 /* do not touch skb anymore */
1134 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto out;
1136 }
1137
1138 /*
1139 * mangle and send the packet here (only for VS/NAT)
1140 */
1141
Julian Anastasovfc604762010-10-17 16:38:15 +03001142 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +08001143 RT_TOS(ip_hdr(skb)->tos),
1144 IP_VS_RT_MODE_LOCAL |
1145 IP_VS_RT_MODE_NON_LOCAL |
1146 IP_VS_RT_MODE_RDR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001148 local = rt->rt_flags & RTCF_LOCAL;
1149
1150 /*
1151 * Avoid duplicate tuple in reply direction for NAT traffic
1152 * to local address when connection is sync-ed
1153 */
1154#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1155 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1156 enum ip_conntrack_info ctinfo;
1157 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1158
1159 if (ct && !nf_ct_is_untracked(ct)) {
1160 IP_VS_DBG(10, "%s(): "
1161 "stopping DNAT to local address %pI4\n",
1162 __func__, &cp->daddr.ip);
1163 goto tx_error_put;
1164 }
1165 }
1166#endif
1167
1168 /* From world but DNAT to loopback address? */
David S. Millerc7537962010-11-11 17:07:48 -08001169 if (local && ipv4_is_loopback(rt->rt_dst) &&
1170 rt_is_input_route(skb_rtable(skb))) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001171 IP_VS_DBG(1, "%s(): "
1172 "stopping DNAT to loopback %pI4\n",
1173 __func__, &cp->daddr.ip);
1174 goto tx_error_put;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001178 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001179 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1180 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +00001182 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +03001183 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -07001187 if (!skb_make_writable(skb, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 goto tx_error_put;
1189
Changli Gaod8d1f302010-06-10 23:31:35 -07001190 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto tx_error_put;
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ip_vs_nat_icmp(skb, pp, cp, 0);
1194
Julian Anastasovfc604762010-10-17 16:38:15 +03001195 if (!local) {
1196 /* drop the old route when skb is not shared */
1197 skb_dst_drop(skb);
1198 skb_dst_set(skb, &rt->dst);
1199 } else {
1200 ip_rt_put(rt);
1201 /*
1202 * Some IPv4 replies get local address from routes,
1203 * not from iph, so while we DNAT after routing
1204 * we need this second input/output route.
1205 */
1206 if (!__ip_vs_reroute_locally(skb))
1207 goto tx_error;
1208 }
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* Another hack: avoid icmp_send in ip_fragment */
1211 skb->local_df = 1;
1212
Julian Anastasovfc604762010-10-17 16:38:15 +03001213 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 rc = NF_STOLEN;
1216 goto out;
1217
1218 tx_error_icmp:
1219 dst_link_failure(skb);
1220 tx_error:
1221 dev_kfree_skb(skb);
1222 rc = NF_STOLEN;
1223 out:
1224 LeaveFunction(10);
1225 return rc;
1226 tx_error_put:
1227 ip_rt_put(rt);
1228 goto tx_error;
1229}
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001230
1231#ifdef CONFIG_IP_VS_IPV6
1232int
1233ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1234 struct ip_vs_protocol *pp, int offset)
1235{
1236 struct rt6_info *rt; /* Route to the other host */
1237 int mtu;
1238 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001239 int local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001240
1241 EnterFunction(10);
1242
1243 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1244 forwarded directly here, because there is no need to
1245 translate address/port back */
1246 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1247 if (cp->packet_xmit)
1248 rc = cp->packet_xmit(skb, cp, pp);
1249 else
1250 rc = NF_ACCEPT;
1251 /* do not touch skb anymore */
1252 atomic_inc(&cp->in_pkts);
1253 goto out;
1254 }
1255
1256 /*
1257 * mangle and send the packet here (only for VS/NAT)
1258 */
1259
Julian Anastasovfc604762010-10-17 16:38:15 +03001260 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -04001261 0, (IP_VS_RT_MODE_LOCAL |
1262 IP_VS_RT_MODE_NON_LOCAL |
1263 IP_VS_RT_MODE_RDR))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001264 goto tx_error_icmp;
1265
Julian Anastasovfc604762010-10-17 16:38:15 +03001266 local = __ip_vs_is_local_route6(rt);
1267 /*
1268 * Avoid duplicate tuple in reply direction for NAT traffic
1269 * to local address when connection is sync-ed
1270 */
1271#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1272 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1273 enum ip_conntrack_info ctinfo;
1274 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1275
1276 if (ct && !nf_ct_is_untracked(ct)) {
1277 IP_VS_DBG(10, "%s(): "
1278 "stopping DNAT to local address %pI6\n",
1279 __func__, &cp->daddr.in6);
1280 goto tx_error_put;
1281 }
1282 }
1283#endif
1284
1285 /* From world but DNAT to loopback address? */
1286 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1287 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1288 IP_VS_DBG(1, "%s(): "
1289 "stopping DNAT to loopback %pI6\n",
1290 __func__, &cp->daddr.in6);
1291 goto tx_error_put;
1292 }
1293
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001294 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001295 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001296 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001297 if (!skb->dev) {
1298 struct net *net = dev_net(skb_dst(skb)->dev);
1299
1300 skb->dev = net->loopback_dev;
1301 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001302 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001303 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +03001304 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001305 }
1306
1307 /* copy-on-write the packet before mangling it */
1308 if (!skb_make_writable(skb, offset))
1309 goto tx_error_put;
1310
Changli Gaod8d1f302010-06-10 23:31:35 -07001311 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001312 goto tx_error_put;
1313
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001314 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1315
Julian Anastasovfc604762010-10-17 16:38:15 +03001316 if (!local || !skb->dev) {
1317 /* drop the old route when skb is not shared */
1318 skb_dst_drop(skb);
1319 skb_dst_set(skb, &rt->dst);
1320 } else {
1321 /* destined to loopback, do we need to change route? */
1322 dst_release(&rt->dst);
1323 }
1324
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001325 /* Another hack: avoid icmp_send in ip_fragment */
1326 skb->local_df = 1;
1327
Julian Anastasovfc604762010-10-17 16:38:15 +03001328 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001329
1330 rc = NF_STOLEN;
1331 goto out;
1332
1333tx_error_icmp:
1334 dst_link_failure(skb);
1335tx_error:
1336 dev_kfree_skb(skb);
1337 rc = NF_STOLEN;
1338out:
1339 LeaveFunction(10);
1340 return rc;
1341tx_error_put:
Changli Gaod8d1f302010-06-10 23:31:35 -07001342 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001343 goto tx_error;
1344}
1345#endif