blob: 3f3e0f4c38a584a105856acd4f456ba34556cebb [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 Harrison14d5e8342008-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 Harrison14d5e8342008-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 }
David S. Miller44e31252011-05-09 14:38:06 -0700113 daddr = dest->addr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 spin_unlock(&dest->dst_lock);
115 } else {
David S. Miller78fbfd82011-03-12 00:00:52 -0500116 rt = ip_route_output(net, daddr, 0, rtos, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800117 if (IS_ERR(rt)) {
Harvey Harrison14d5e8342008-10-31 00:54:29 -0700118 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
Julian Anastasovfc604762010-10-17 16:38:15 +0300119 &daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return NULL;
121 }
122 }
123
Julian Anastasovfc604762010-10-17 16:38:15 +0300124 local = rt->rt_flags & RTCF_LOCAL;
Changli Gao17a8f8e2011-02-24 08:19:57 +0800125 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
126 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300127 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
128 (rt->rt_flags & RTCF_LOCAL) ?
David S. Miller44e31252011-05-09 14:38:06 -0700129 "local":"non-local", &daddr);
Julian Anastasovfc604762010-10-17 16:38:15 +0300130 ip_rt_put(rt);
131 return NULL;
132 }
Changli Gao17a8f8e2011-02-24 08:19:57 +0800133 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
134 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300135 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
136 "requires NAT method, dest: %pI4\n",
David S. Miller44e31252011-05-09 14:38:06 -0700137 &ip_hdr(skb)->daddr, &daddr);
Julian Anastasovfc604762010-10-17 16:38:15 +0300138 ip_rt_put(rt);
139 return NULL;
140 }
141 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
142 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
143 "to non-local address, dest: %pI4\n",
David S. Miller44e31252011-05-09 14:38:06 -0700144 &ip_hdr(skb)->saddr, &daddr);
Julian Anastasovfc604762010-10-17 16:38:15 +0300145 ip_rt_put(rt);
146 return NULL;
147 }
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return rt;
150}
151
Julian Anastasovfc604762010-10-17 16:38:15 +0300152/* Reroute packet to local IPv4 stack after DNAT */
153static int
154__ip_vs_reroute_locally(struct sk_buff *skb)
155{
156 struct rtable *rt = skb_rtable(skb);
157 struct net_device *dev = rt->dst.dev;
158 struct net *net = dev_net(dev);
159 struct iphdr *iph = ip_hdr(skb);
160
David S. Millerc7537962010-11-11 17:07:48 -0800161 if (rt_is_input_route(rt)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300162 unsigned long orefdst = skb->_skb_refdst;
163
164 if (ip_route_input(skb, iph->daddr, iph->saddr,
165 iph->tos, skb->dev))
166 return 0;
167 refdst_drop(orefdst);
168 } else {
David S. Miller9d6ec932011-03-12 01:12:47 -0500169 struct flowi4 fl4 = {
170 .daddr = iph->daddr,
171 .saddr = iph->saddr,
172 .flowi4_tos = RT_TOS(iph->tos),
173 .flowi4_mark = skb->mark,
Julian Anastasovfc604762010-10-17 16:38:15 +0300174 };
Julian Anastasovfc604762010-10-17 16:38:15 +0300175
David S. Miller9d6ec932011-03-12 01:12:47 -0500176 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800177 if (IS_ERR(rt))
Julian Anastasovfc604762010-10-17 16:38:15 +0300178 return 0;
179 if (!(rt->rt_flags & RTCF_LOCAL)) {
180 ip_rt_put(rt);
181 return 0;
182 }
183 /* Drop old route. */
184 skb_dst_drop(skb);
185 skb_dst_set(skb, &rt->dst);
186 }
187 return 1;
188}
189
Julius Volz38cdcc92008-09-02 15:55:44 +0200190#ifdef CONFIG_IP_VS_IPV6
Hans Schillstrom714f0952010-10-19 10:38:48 +0200191
Julian Anastasovfc604762010-10-17 16:38:15 +0300192static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
193{
194 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
195}
196
Hans Schillstrom714f0952010-10-19 10:38:48 +0200197static struct dst_entry *
198__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
199 struct in6_addr *ret_saddr, int do_xfrm)
Julius Volz38cdcc92008-09-02 15:55:44 +0200200{
Hans Schillstrom714f0952010-10-19 10:38:48 +0200201 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500202 struct flowi6 fl6 = {
203 .daddr = *daddr,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200204 };
205
David S. Miller4c9483b2011-03-12 16:22:43 -0500206 dst = ip6_route_output(net, NULL, &fl6);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200207 if (dst->error)
208 goto out_err;
209 if (!ret_saddr)
210 return dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500211 if (ipv6_addr_any(&fl6.saddr) &&
Hans Schillstrom714f0952010-10-19 10:38:48 +0200212 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
David S. Miller4c9483b2011-03-12 16:22:43 -0500213 &fl6.daddr, 0, &fl6.saddr) < 0)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200214 goto out_err;
David S. Miller452edd52011-03-02 13:27:41 -0800215 if (do_xfrm) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500216 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -0800217 if (IS_ERR(dst)) {
218 dst = NULL;
219 goto out_err;
220 }
221 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500222 ipv6_addr_copy(ret_saddr, &fl6.saddr);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200223 return dst;
224
225out_err:
226 dst_release(dst);
227 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
228 return NULL;
229}
230
Julian Anastasovfc604762010-10-17 16:38:15 +0300231/*
232 * Get route to destination or remote server
Julian Anastasovfc604762010-10-17 16:38:15 +0300233 */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200234static struct rt6_info *
Julian Anastasovfc604762010-10-17 16:38:15 +0300235__ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
236 struct in6_addr *daddr, struct in6_addr *ret_saddr,
237 int do_xfrm, int rt_mode)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200238{
Julian Anastasovfc604762010-10-17 16:38:15 +0300239 struct net *net = dev_net(skb_dst(skb)->dev);
Julius Volz38cdcc92008-09-02 15:55:44 +0200240 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovfc604762010-10-17 16:38:15 +0300241 struct rt6_info *ort; /* Original route */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200242 struct dst_entry *dst;
Julian Anastasovfc604762010-10-17 16:38:15 +0300243 int local;
Julius Volz38cdcc92008-09-02 15:55:44 +0200244
245 if (dest) {
246 spin_lock(&dest->dst_lock);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200247 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
Julius Volz38cdcc92008-09-02 15:55:44 +0200248 if (!rt) {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200249 u32 cookie;
Julius Volz38cdcc92008-09-02 15:55:44 +0200250
Hans Schillstrom714f0952010-10-19 10:38:48 +0200251 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
252 &dest->dst_saddr,
253 do_xfrm);
254 if (!dst) {
Julius Volz38cdcc92008-09-02 15:55:44 +0200255 spin_unlock(&dest->dst_lock);
Julius Volz38cdcc92008-09-02 15:55:44 +0200256 return NULL;
257 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200258 rt = (struct rt6_info *) dst;
259 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
260 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
261 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
262 &dest->addr.in6, &dest->dst_saddr,
Changli Gaod8d1f302010-06-10 23:31:35 -0700263 atomic_read(&rt->dst.__refcnt));
Julius Volz38cdcc92008-09-02 15:55:44 +0200264 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200265 if (ret_saddr)
266 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
Julius Volz38cdcc92008-09-02 15:55:44 +0200267 spin_unlock(&dest->dst_lock);
268 } else {
Julian Anastasovfc604762010-10-17 16:38:15 +0300269 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200270 if (!dst)
Julius Volz38cdcc92008-09-02 15:55:44 +0200271 return NULL;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200272 rt = (struct rt6_info *) dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200273 }
274
Julian Anastasovfc604762010-10-17 16:38:15 +0300275 local = __ip_vs_is_local_route6(rt);
David S. Millere58b3442011-05-12 18:22:34 -0400276 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
277 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300278 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
279 local ? "local":"non-local", daddr);
280 dst_release(&rt->dst);
281 return NULL;
282 }
David S. Millere58b3442011-05-12 18:22:34 -0400283 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
Julian Anastasovfc604762010-10-17 16:38:15 +0300284 !((ort = (struct rt6_info *) skb_dst(skb)) &&
285 __ip_vs_is_local_route6(ort))) {
286 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
287 "requires NAT method, dest: %pI6\n",
288 &ipv6_hdr(skb)->daddr, daddr);
289 dst_release(&rt->dst);
290 return NULL;
291 }
292 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
293 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
294 IPV6_ADDR_LOOPBACK)) {
295 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
296 "to non-local address, dest: %pI6\n",
297 &ipv6_hdr(skb)->saddr, daddr);
298 dst_release(&rt->dst);
299 return NULL;
300 }
301
Julius Volz38cdcc92008-09-02 15:55:44 +0200302 return rt;
303}
304#endif
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/*
308 * Release dest->dst_cache before a dest is removed
309 */
310void
311ip_vs_dst_reset(struct ip_vs_dest *dest)
312{
313 struct dst_entry *old_dst;
314
315 old_dst = dest->dst_cache;
316 dest->dst_cache = NULL;
317 dst_release(old_dst);
318}
319
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200320#define IP_VS_XMIT_TUNNEL(skb, cp) \
321({ \
322 int __ret = NF_ACCEPT; \
323 \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300324 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200325 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
326 __ret = ip_vs_confirm_conntrack(skb, cp); \
327 if (__ret == NF_ACCEPT) { \
328 nf_reset(skb); \
Julian Anastasov4256f1a2010-10-17 16:29:40 +0300329 skb_forward_csum(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200330 } \
331 __ret; \
332})
333
Julian Anastasovfc604762010-10-17 16:38:15 +0300334#define IP_VS_XMIT_NAT(pf, skb, cp, local) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335do { \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300336 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200337 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300338 ip_vs_notrack(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200339 else \
340 ip_vs_update_conntrack(skb, cp, 1); \
Julian Anastasovfc604762010-10-17 16:38:15 +0300341 if (local) \
342 return NF_ACCEPT; \
Herbert Xuccc79112007-07-30 16:20:12 -0700343 skb_forward_csum(skb); \
Julius Volz38cdcc92008-09-02 15:55:44 +0200344 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200345 skb_dst(skb)->dev, dst_output); \
346} while (0)
347
Julian Anastasovfc604762010-10-17 16:38:15 +0300348#define IP_VS_XMIT(pf, skb, cp, local) \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200349do { \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300350 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200351 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300352 ip_vs_notrack(skb); \
Julian Anastasovfc604762010-10-17 16:38:15 +0300353 if (local) \
354 return NF_ACCEPT; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200355 skb_forward_csum(skb); \
356 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
357 skb_dst(skb)->dev, dst_output); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358} while (0)
359
360
361/*
362 * NULL transmitter (do nothing except return NF_ACCEPT)
363 */
364int
365ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
366 struct ip_vs_protocol *pp)
367{
368 /* we do not touch skb and do not need pskb ptr */
Julian Anastasovfc604762010-10-17 16:38:15 +0300369 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372
373/*
374 * Bypass transmitter
375 * Let packets bypass the destination when the destination is not
376 * available, it may be only used in transparent cache cluster.
377 */
378int
379ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
380 struct ip_vs_protocol *pp)
381{
382 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700383 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 EnterFunction(10);
387
Changli Gao17a8f8e2011-02-24 08:19:57 +0800388 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
389 IP_VS_RT_MODE_NON_LOCAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto tx_error_icmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700393 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900394 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
395 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ip_rt_put(rt);
397 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000398 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto tx_error;
400 }
401
402 /*
403 * Call ip_send_check because we are not sure it is called
404 * after ip_defrag. Is copy-on-write needed?
405 */
406 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
407 ip_rt_put(rt);
408 return NF_STOLEN;
409 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700410 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000413 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700414 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* Another hack: avoid icmp_send in ip_fragment */
417 skb->local_df = 1;
418
Julian Anastasovfc604762010-10-17 16:38:15 +0300419 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 LeaveFunction(10);
422 return NF_STOLEN;
423
424 tx_error_icmp:
425 dst_link_failure(skb);
426 tx_error:
427 kfree_skb(skb);
428 LeaveFunction(10);
429 return NF_STOLEN;
430}
431
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200432#ifdef CONFIG_IP_VS_IPV6
433int
434ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
435 struct ip_vs_protocol *pp)
436{
437 struct rt6_info *rt; /* Route to the other host */
438 struct ipv6hdr *iph = ipv6_hdr(skb);
439 int mtu;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200440
441 EnterFunction(10);
442
David S. Millere58b3442011-05-12 18:22:34 -0400443 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0,
444 IP_VS_RT_MODE_NON_LOCAL)))
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200445 goto tx_error_icmp;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200446
447 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700448 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900449 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300450 if (!skb->dev) {
451 struct net *net = dev_net(skb_dst(skb)->dev);
452
453 skb->dev = net->loopback_dev;
454 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000455 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Julian Anastasovcb591552010-10-17 16:40:51 +0300456 dst_release(&rt->dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000457 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200458 goto tx_error;
459 }
460
461 /*
462 * Call ip_send_check because we are not sure it is called
463 * after ip_defrag. Is copy-on-write needed?
464 */
465 skb = skb_share_check(skb, GFP_ATOMIC);
466 if (unlikely(skb == NULL)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700467 dst_release(&rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200468 return NF_STOLEN;
469 }
470
471 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000472 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700473 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200474
475 /* Another hack: avoid icmp_send in ip_fragment */
476 skb->local_df = 1;
477
Julian Anastasovfc604762010-10-17 16:38:15 +0300478 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200479
480 LeaveFunction(10);
481 return NF_STOLEN;
482
483 tx_error_icmp:
484 dst_link_failure(skb);
485 tx_error:
486 kfree_skb(skb);
487 LeaveFunction(10);
488 return NF_STOLEN;
489}
490#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492/*
493 * NAT transmitter (only for outside-to-inside nat forwarding)
494 * Not used for related ICMP
495 */
496int
497ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
498 struct ip_vs_protocol *pp)
499{
500 struct rtable *rt; /* Route to the other host */
501 int mtu;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700502 struct iphdr *iph = ip_hdr(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300503 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 EnterFunction(10);
506
507 /* check if it is a connection of no-client-port */
508 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700509 __be16 _pt, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
511 if (p == NULL)
512 goto tx_error;
513 ip_vs_conn_fill_cport(cp, *p);
514 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
515 }
516
Julian Anastasovfc604762010-10-17 16:38:15 +0300517 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800518 RT_TOS(iph->tos),
519 IP_VS_RT_MODE_LOCAL |
520 IP_VS_RT_MODE_NON_LOCAL |
521 IP_VS_RT_MODE_RDR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300523 local = rt->rt_flags & RTCF_LOCAL;
524 /*
525 * Avoid duplicate tuple in reply direction for NAT traffic
526 * to local address when connection is sync-ed
527 */
528#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
529 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
530 enum ip_conntrack_info ctinfo;
531 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
532
533 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300534 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
535 "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300536 "stopping DNAT to local address");
537 goto tx_error_put;
538 }
539 }
540#endif
541
542 /* From world but DNAT to loopback address? */
David S. Millerc7537962010-11-11 17:07:48 -0800543 if (local && ipv4_is_loopback(rt->rt_dst) &&
544 rt_is_input_route(skb_rtable(skb))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300545 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300546 "stopping DNAT to loopback address");
547 goto tx_error_put;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700551 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900552 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
553 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Julian Anastasov0d796412010-10-17 16:46:17 +0300555 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
556 "ip_vs_nat_xmit(): frag needed for");
Julian Anastasovfc604762010-10-17 16:38:15 +0300557 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700561 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto tx_error_put;
563
Changli Gaod8d1f302010-06-10 23:31:35 -0700564 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto tx_error_put;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* mangle the packet */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700568 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
Julian Anastasovfc604762010-10-17 16:38:15 +0300569 goto tx_error_put;
Julius Volze7ade462008-09-02 15:55:33 +0200570 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700571 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Julian Anastasovfc604762010-10-17 16:38:15 +0300573 if (!local) {
574 /* drop old route */
575 skb_dst_drop(skb);
576 skb_dst_set(skb, &rt->dst);
577 } else {
578 ip_rt_put(rt);
579 /*
580 * Some IPv4 replies get local address from routes,
581 * not from iph, so while we DNAT after routing
582 * we need this second input/output route.
583 */
584 if (!__ip_vs_reroute_locally(skb))
585 goto tx_error;
586 }
587
Julian Anastasov0d796412010-10-17 16:46:17 +0300588 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* FIXME: when application helper enlarges the packet and the length
591 is larger than the MTU of outgoing device, there will be still
592 MTU problem. */
593
594 /* Another hack: avoid icmp_send in ip_fragment */
595 skb->local_df = 1;
596
Julian Anastasovfc604762010-10-17 16:38:15 +0300597 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 LeaveFunction(10);
600 return NF_STOLEN;
601
602 tx_error_icmp:
603 dst_link_failure(skb);
604 tx_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200606 LeaveFunction(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return NF_STOLEN;
608 tx_error_put:
609 ip_rt_put(rt);
610 goto tx_error;
611}
612
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200613#ifdef CONFIG_IP_VS_IPV6
614int
615ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
616 struct ip_vs_protocol *pp)
617{
618 struct rt6_info *rt; /* Route to the other host */
619 int mtu;
Julian Anastasovfc604762010-10-17 16:38:15 +0300620 int local;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200621
622 EnterFunction(10);
623
624 /* check if it is a connection of no-client-port */
625 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
626 __be16 _pt, *p;
627 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
628 sizeof(_pt), &_pt);
629 if (p == NULL)
630 goto tx_error;
631 ip_vs_conn_fill_cport(cp, *p);
632 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
633 }
634
Julian Anastasovfc604762010-10-17 16:38:15 +0300635 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -0400636 0, (IP_VS_RT_MODE_LOCAL |
637 IP_VS_RT_MODE_NON_LOCAL |
638 IP_VS_RT_MODE_RDR))))
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200639 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300640 local = __ip_vs_is_local_route6(rt);
641 /*
642 * Avoid duplicate tuple in reply direction for NAT traffic
643 * to local address when connection is sync-ed
644 */
645#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
646 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
647 enum ip_conntrack_info ctinfo;
648 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
649
650 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300651 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300652 "ip_vs_nat_xmit_v6(): "
653 "stopping DNAT to local address");
654 goto tx_error_put;
655 }
656 }
657#endif
658
659 /* From world but DNAT to loopback address? */
660 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
661 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300662 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300663 "ip_vs_nat_xmit_v6(): "
664 "stopping DNAT to loopback address");
665 goto tx_error_put;
666 }
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200667
668 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700669 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900670 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300671 if (!skb->dev) {
672 struct net *net = dev_net(skb_dst(skb)->dev);
673
674 skb->dev = net->loopback_dev;
675 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000676 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Julian Anastasov0d796412010-10-17 16:46:17 +0300677 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200678 "ip_vs_nat_xmit_v6(): frag needed for");
Julian Anastasovfc604762010-10-17 16:38:15 +0300679 goto tx_error_put;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200680 }
681
682 /* copy-on-write the packet before mangling it */
683 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
684 goto tx_error_put;
685
Changli Gaod8d1f302010-06-10 23:31:35 -0700686 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200687 goto tx_error_put;
688
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200689 /* mangle the packet */
690 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
691 goto tx_error;
Julian Anastasovfc604762010-10-17 16:38:15 +0300692 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
693
694 if (!local || !skb->dev) {
695 /* drop the old route when skb is not shared */
696 skb_dst_drop(skb);
697 skb_dst_set(skb, &rt->dst);
698 } else {
699 /* destined to loopback, do we need to change route? */
700 dst_release(&rt->dst);
701 }
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200702
Julian Anastasov0d796412010-10-17 16:46:17 +0300703 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200704
705 /* FIXME: when application helper enlarges the packet and the length
706 is larger than the MTU of outgoing device, there will be still
707 MTU problem. */
708
709 /* Another hack: avoid icmp_send in ip_fragment */
710 skb->local_df = 1;
711
Julian Anastasovfc604762010-10-17 16:38:15 +0300712 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200713
714 LeaveFunction(10);
715 return NF_STOLEN;
716
717tx_error_icmp:
718 dst_link_failure(skb);
719tx_error:
720 LeaveFunction(10);
721 kfree_skb(skb);
722 return NF_STOLEN;
723tx_error_put:
Changli Gaod8d1f302010-06-10 23:31:35 -0700724 dst_release(&rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200725 goto tx_error;
726}
727#endif
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730/*
731 * IP Tunneling transmitter
732 *
733 * This function encapsulates the packet in a new IP packet, its
734 * destination will be set to cp->daddr. Most code of this function
735 * is taken from ipip.c.
736 *
737 * It is used in VS/TUN cluster. The load balancer selects a real
738 * server from a cluster based on a scheduling algorithm,
739 * encapsulates the request packet and forwards it to the selected
740 * server. For example, all real servers are configured with
741 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
742 * the encapsulated packet, it will decapsulate the packet, processe
743 * the request and return the response packets directly to the client
744 * without passing the load balancer. This can greatly increase the
745 * scalability of virtual server.
746 *
747 * Used for ANY protocol
748 */
749int
750ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
751 struct ip_vs_protocol *pp)
752{
753 struct rtable *rt; /* Route to the other host */
754 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700755 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 u8 tos = old_iph->tos;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800757 __be16 df = old_iph->frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700759 unsigned int max_headroom; /* The extra header space needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int mtu;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200761 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 EnterFunction(10);
764
Julian Anastasovfc604762010-10-17 16:38:15 +0300765 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800766 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
767 IP_VS_RT_MODE_NON_LOCAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300769 if (rt->rt_flags & RTCF_LOCAL) {
770 ip_rt_put(rt);
771 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Changli Gaod8d1f302010-06-10 23:31:35 -0700774 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Changli Gaod8d1f302010-06-10 23:31:35 -0700776 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (mtu < 68) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000778 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300779 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000781 if (skb_dst(skb))
782 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900784 df |= (old_iph->frag_off & htons(IP_DF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Simon Horman8f1b03a2010-11-09 10:08:49 +0900786 if ((old_iph->frag_off & htons(IP_DF) &&
787 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000789 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300790 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 /*
794 * Okay, now see if we can stuff it in the buffer as-is.
795 */
796 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
797
798 if (skb_headroom(skb) < max_headroom
799 || skb_cloned(skb) || skb_shared(skb)) {
800 struct sk_buff *new_skb =
801 skb_realloc_headroom(skb, max_headroom);
802 if (!new_skb) {
803 ip_rt_put(rt);
804 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000805 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return NF_STOLEN;
807 }
808 kfree_skb(skb);
809 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700810 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Hans Schillstrom714f0952010-10-19 10:38:48 +0200813 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* fix old IP header checksum */
816 ip_send_check(old_iph);
817
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700818 skb_push(skb, sizeof(struct iphdr));
819 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
821
822 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000823 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700824 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
827 * Push down and install the IPIP header.
828 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700829 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 iph->version = 4;
831 iph->ihl = sizeof(struct iphdr)>>2;
832 iph->frag_off = df;
833 iph->protocol = IPPROTO_IPIP;
834 iph->tos = tos;
835 iph->daddr = rt->rt_dst;
836 iph->saddr = rt->rt_src;
837 iph->ttl = old_iph->ttl;
Changli Gaod8d1f302010-06-10 23:31:35 -0700838 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* Another hack: avoid icmp_send in ip_fragment */
841 skb->local_df = 1;
842
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200843 ret = IP_VS_XMIT_TUNNEL(skb, cp);
844 if (ret == NF_ACCEPT)
845 ip_local_out(skb);
846 else if (ret == NF_DROP)
847 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 LeaveFunction(10);
850
851 return NF_STOLEN;
852
853 tx_error_icmp:
854 dst_link_failure(skb);
855 tx_error:
856 kfree_skb(skb);
857 LeaveFunction(10);
858 return NF_STOLEN;
Julian Anastasovfc604762010-10-17 16:38:15 +0300859tx_error_put:
860 ip_rt_put(rt);
861 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200864#ifdef CONFIG_IP_VS_IPV6
865int
866ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
867 struct ip_vs_protocol *pp)
868{
869 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200870 struct in6_addr saddr; /* Source for tunnel */
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200871 struct net_device *tdev; /* Device to other host */
872 struct ipv6hdr *old_iph = ipv6_hdr(skb);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200873 struct ipv6hdr *iph; /* Our new IP header */
874 unsigned int max_headroom; /* The extra header space needed */
875 int mtu;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200876 int ret;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200877
878 EnterFunction(10);
879
Julian Anastasovfc604762010-10-17 16:38:15 +0300880 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
David S. Millere58b3442011-05-12 18:22:34 -0400881 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
882 IP_VS_RT_MODE_NON_LOCAL))))
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200883 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300884 if (__ip_vs_is_local_route6(rt)) {
885 dst_release(&rt->dst);
886 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
887 }
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200888
Changli Gaod8d1f302010-06-10 23:31:35 -0700889 tdev = rt->dst.dev;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200890
Changli Gaod8d1f302010-06-10 23:31:35 -0700891 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200892 if (mtu < IPV6_MIN_MTU) {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200893 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
894 IPV6_MIN_MTU);
Julian Anastasovfc604762010-10-17 16:38:15 +0300895 goto tx_error_put;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200896 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000897 if (skb_dst(skb))
898 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200899
Simon Horman8f1b03a2010-11-09 10:08:49 +0900900 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
901 !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300902 if (!skb->dev) {
903 struct net *net = dev_net(skb_dst(skb)->dev);
904
905 skb->dev = net->loopback_dev;
906 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000907 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000908 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300909 goto tx_error_put;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200910 }
911
912 /*
913 * Okay, now see if we can stuff it in the buffer as-is.
914 */
915 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
916
917 if (skb_headroom(skb) < max_headroom
918 || skb_cloned(skb) || skb_shared(skb)) {
919 struct sk_buff *new_skb =
920 skb_realloc_headroom(skb, max_headroom);
921 if (!new_skb) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700922 dst_release(&rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200923 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000924 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200925 return NF_STOLEN;
926 }
927 kfree_skb(skb);
928 skb = new_skb;
929 old_iph = ipv6_hdr(skb);
930 }
931
Hans Schillstrom714f0952010-10-19 10:38:48 +0200932 skb->transport_header = skb->network_header;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200933
934 skb_push(skb, sizeof(struct ipv6hdr));
935 skb_reset_network_header(skb);
936 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
937
938 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000939 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700940 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200941
942 /*
943 * Push down and install the IPIP header.
944 */
945 iph = ipv6_hdr(skb);
946 iph->version = 6;
947 iph->nexthdr = IPPROTO_IPV6;
Harvey Harrisonb7b45f42008-11-10 16:46:06 -0800948 iph->payload_len = old_iph->payload_len;
949 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200950 iph->priority = old_iph->priority;
951 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
Hans Schillstrom714f0952010-10-19 10:38:48 +0200952 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
953 ipv6_addr_copy(&iph->saddr, &saddr);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200954 iph->hop_limit = old_iph->hop_limit;
955
956 /* Another hack: avoid icmp_send in ip_fragment */
957 skb->local_df = 1;
958
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200959 ret = IP_VS_XMIT_TUNNEL(skb, cp);
960 if (ret == NF_ACCEPT)
961 ip6_local_out(skb);
962 else if (ret == NF_DROP)
963 kfree_skb(skb);
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200964
965 LeaveFunction(10);
966
967 return NF_STOLEN;
968
969tx_error_icmp:
970 dst_link_failure(skb);
971tx_error:
972 kfree_skb(skb);
973 LeaveFunction(10);
974 return NF_STOLEN;
Julian Anastasovfc604762010-10-17 16:38:15 +0300975tx_error_put:
976 dst_release(&rt->dst);
977 goto tx_error;
Julius Volzb3cdd2a2008-09-02 15:55:45 +0200978}
979#endif
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982/*
983 * Direct Routing transmitter
984 * Used for ANY protocol
985 */
986int
987ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
988 struct ip_vs_protocol *pp)
989{
990 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700991 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 int mtu;
993
994 EnterFunction(10);
995
Julian Anastasovfc604762010-10-17 16:38:15 +0300996 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800997 RT_TOS(iph->tos),
998 IP_VS_RT_MODE_LOCAL |
999 IP_VS_RT_MODE_NON_LOCAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001001 if (rt->rt_flags & RTCF_LOCAL) {
1002 ip_rt_put(rt);
1003 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001007 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001008 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1009 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1011 ip_rt_put(rt);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001012 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto tx_error;
1014 }
1015
1016 /*
1017 * Call ip_send_check because we are not sure it is called
1018 * after ip_defrag. Is copy-on-write needed?
1019 */
1020 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1021 ip_rt_put(rt);
1022 return NF_STOLEN;
1023 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001024 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +00001027 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001028 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* Another hack: avoid icmp_send in ip_fragment */
1031 skb->local_df = 1;
1032
Julian Anastasovfc604762010-10-17 16:38:15 +03001033 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 LeaveFunction(10);
1036 return NF_STOLEN;
1037
1038 tx_error_icmp:
1039 dst_link_failure(skb);
1040 tx_error:
1041 kfree_skb(skb);
1042 LeaveFunction(10);
1043 return NF_STOLEN;
1044}
1045
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001046#ifdef CONFIG_IP_VS_IPV6
1047int
1048ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1049 struct ip_vs_protocol *pp)
1050{
1051 struct rt6_info *rt; /* Route to the other host */
1052 int mtu;
1053
1054 EnterFunction(10);
1055
Julian Anastasovfc604762010-10-17 16:38:15 +03001056 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -04001057 0, (IP_VS_RT_MODE_LOCAL |
1058 IP_VS_RT_MODE_NON_LOCAL))))
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001059 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001060 if (__ip_vs_is_local_route6(rt)) {
1061 dst_release(&rt->dst);
1062 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1063 }
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001064
1065 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001066 mtu = dst_mtu(&rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001067 if (skb->len > mtu) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001068 if (!skb->dev) {
1069 struct net *net = dev_net(skb_dst(skb)->dev);
1070
1071 skb->dev = net->loopback_dev;
1072 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001073 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Changli Gaod8d1f302010-06-10 23:31:35 -07001074 dst_release(&rt->dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001075 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001076 goto tx_error;
1077 }
1078
1079 /*
1080 * Call ip_send_check because we are not sure it is called
1081 * after ip_defrag. Is copy-on-write needed?
1082 */
1083 skb = skb_share_check(skb, GFP_ATOMIC);
1084 if (unlikely(skb == NULL)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001085 dst_release(&rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001086 return NF_STOLEN;
1087 }
1088
1089 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +00001090 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001091 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001092
1093 /* Another hack: avoid icmp_send in ip_fragment */
1094 skb->local_df = 1;
1095
Julian Anastasovfc604762010-10-17 16:38:15 +03001096 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001097
1098 LeaveFunction(10);
1099 return NF_STOLEN;
1100
1101tx_error_icmp:
1102 dst_link_failure(skb);
1103tx_error:
1104 kfree_skb(skb);
1105 LeaveFunction(10);
1106 return NF_STOLEN;
1107}
1108#endif
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111/*
1112 * ICMP packet transmitter
1113 * called by the ip_vs_in_icmp
1114 */
1115int
1116ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1117 struct ip_vs_protocol *pp, int offset)
1118{
1119 struct rtable *rt; /* Route to the other host */
1120 int mtu;
1121 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001122 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 EnterFunction(10);
1125
1126 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1127 forwarded directly here, because there is no need to
1128 translate address/port back */
1129 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1130 if (cp->packet_xmit)
1131 rc = cp->packet_xmit(skb, cp, pp);
1132 else
1133 rc = NF_ACCEPT;
1134 /* do not touch skb anymore */
1135 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 goto out;
1137 }
1138
1139 /*
1140 * mangle and send the packet here (only for VS/NAT)
1141 */
1142
Julian Anastasovfc604762010-10-17 16:38:15 +03001143 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +08001144 RT_TOS(ip_hdr(skb)->tos),
1145 IP_VS_RT_MODE_LOCAL |
1146 IP_VS_RT_MODE_NON_LOCAL |
1147 IP_VS_RT_MODE_RDR)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001149 local = rt->rt_flags & RTCF_LOCAL;
1150
1151 /*
1152 * Avoid duplicate tuple in reply direction for NAT traffic
1153 * to local address when connection is sync-ed
1154 */
1155#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1156 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1157 enum ip_conntrack_info ctinfo;
1158 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1159
1160 if (ct && !nf_ct_is_untracked(ct)) {
1161 IP_VS_DBG(10, "%s(): "
1162 "stopping DNAT to local address %pI4\n",
1163 __func__, &cp->daddr.ip);
1164 goto tx_error_put;
1165 }
1166 }
1167#endif
1168
1169 /* From world but DNAT to loopback address? */
David S. Millerc7537962010-11-11 17:07:48 -08001170 if (local && ipv4_is_loopback(rt->rt_dst) &&
1171 rt_is_input_route(skb_rtable(skb))) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001172 IP_VS_DBG(1, "%s(): "
1173 "stopping DNAT to loopback %pI4\n",
1174 __func__, &cp->daddr.ip);
1175 goto tx_error_put;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001179 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001180 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1181 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +00001183 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +03001184 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -07001188 if (!skb_make_writable(skb, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto tx_error_put;
1190
Changli Gaod8d1f302010-06-10 23:31:35 -07001191 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto tx_error_put;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ip_vs_nat_icmp(skb, pp, cp, 0);
1195
Julian Anastasovfc604762010-10-17 16:38:15 +03001196 if (!local) {
1197 /* drop the old route when skb is not shared */
1198 skb_dst_drop(skb);
1199 skb_dst_set(skb, &rt->dst);
1200 } else {
1201 ip_rt_put(rt);
1202 /*
1203 * Some IPv4 replies get local address from routes,
1204 * not from iph, so while we DNAT after routing
1205 * we need this second input/output route.
1206 */
1207 if (!__ip_vs_reroute_locally(skb))
1208 goto tx_error;
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* Another hack: avoid icmp_send in ip_fragment */
1212 skb->local_df = 1;
1213
Julian Anastasovfc604762010-10-17 16:38:15 +03001214 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 rc = NF_STOLEN;
1217 goto out;
1218
1219 tx_error_icmp:
1220 dst_link_failure(skb);
1221 tx_error:
1222 dev_kfree_skb(skb);
1223 rc = NF_STOLEN;
1224 out:
1225 LeaveFunction(10);
1226 return rc;
1227 tx_error_put:
1228 ip_rt_put(rt);
1229 goto tx_error;
1230}
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001231
1232#ifdef CONFIG_IP_VS_IPV6
1233int
1234ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1235 struct ip_vs_protocol *pp, int offset)
1236{
1237 struct rt6_info *rt; /* Route to the other host */
1238 int mtu;
1239 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001240 int local;
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001241
1242 EnterFunction(10);
1243
1244 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1245 forwarded directly here, because there is no need to
1246 translate address/port back */
1247 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1248 if (cp->packet_xmit)
1249 rc = cp->packet_xmit(skb, cp, pp);
1250 else
1251 rc = NF_ACCEPT;
1252 /* do not touch skb anymore */
1253 atomic_inc(&cp->in_pkts);
1254 goto out;
1255 }
1256
1257 /*
1258 * mangle and send the packet here (only for VS/NAT)
1259 */
1260
Julian Anastasovfc604762010-10-17 16:38:15 +03001261 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -04001262 0, (IP_VS_RT_MODE_LOCAL |
1263 IP_VS_RT_MODE_NON_LOCAL |
1264 IP_VS_RT_MODE_RDR))))
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001265 goto tx_error_icmp;
1266
Julian Anastasovfc604762010-10-17 16:38:15 +03001267 local = __ip_vs_is_local_route6(rt);
1268 /*
1269 * Avoid duplicate tuple in reply direction for NAT traffic
1270 * to local address when connection is sync-ed
1271 */
1272#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1273 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1274 enum ip_conntrack_info ctinfo;
1275 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1276
1277 if (ct && !nf_ct_is_untracked(ct)) {
1278 IP_VS_DBG(10, "%s(): "
1279 "stopping DNAT to local address %pI6\n",
1280 __func__, &cp->daddr.in6);
1281 goto tx_error_put;
1282 }
1283 }
1284#endif
1285
1286 /* From world but DNAT to loopback address? */
1287 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1288 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1289 IP_VS_DBG(1, "%s(): "
1290 "stopping DNAT to loopback %pI6\n",
1291 __func__, &cp->daddr.in6);
1292 goto tx_error_put;
1293 }
1294
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001295 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001296 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001297 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001298 if (!skb->dev) {
1299 struct net *net = dev_net(skb_dst(skb)->dev);
1300
1301 skb->dev = net->loopback_dev;
1302 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001303 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001304 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +03001305 goto tx_error_put;
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001306 }
1307
1308 /* copy-on-write the packet before mangling it */
1309 if (!skb_make_writable(skb, offset))
1310 goto tx_error_put;
1311
Changli Gaod8d1f302010-06-10 23:31:35 -07001312 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001313 goto tx_error_put;
1314
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001315 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1316
Julian Anastasovfc604762010-10-17 16:38:15 +03001317 if (!local || !skb->dev) {
1318 /* drop the old route when skb is not shared */
1319 skb_dst_drop(skb);
1320 skb_dst_set(skb, &rt->dst);
1321 } else {
1322 /* destined to loopback, do we need to change route? */
1323 dst_release(&rt->dst);
1324 }
1325
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001326 /* Another hack: avoid icmp_send in ip_fragment */
1327 skb->local_df = 1;
1328
Julian Anastasovfc604762010-10-17 16:38:15 +03001329 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001330
1331 rc = NF_STOLEN;
1332 goto out;
1333
1334tx_error_icmp:
1335 dst_link_failure(skb);
1336tx_error:
1337 dev_kfree_skb(skb);
1338 rc = NF_STOLEN;
1339out:
1340 LeaveFunction(10);
1341 return rc;
1342tx_error_put:
Changli Gaod8d1f302010-06-10 23:31:35 -07001343 dst_release(&rt->dst);
Julius Volzb3cdd2a2008-09-02 15:55:45 +02001344 goto tx_error;
1345}
1346#endif