blob: 72b82b8ac5a727bce65d67827c40952e42be4e7a [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,
Julian Anastasovc92f5ca2011-05-10 12:46:05 +000090 __be32 daddr, u32 rtos, int rt_mode, __be32 *ret_saddr)
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))) {
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000101 struct flowi4 fl4;
102
103 memset(&fl4, 0, sizeof(fl4));
104 fl4.daddr = dest->addr.ip;
105 fl4.flowi4_tos = rtos;
106 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800107 if (IS_ERR(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 spin_unlock(&dest->dst_lock);
Harvey Harrison14d5e832008-10-31 00:54:29 -0700109 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
110 &dest->addr.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return NULL;
112 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200113 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000114 dest->dst_saddr.ip = fl4.saddr;
115 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d, "
116 "rtos=%X\n",
117 &dest->addr.ip, &dest->dst_saddr.ip,
Changli Gaod8d1f302010-06-10 23:31:35 -0700118 atomic_read(&rt->dst.__refcnt), rtos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
David S. Miller44e31252011-05-09 14:38:06 -0700120 daddr = dest->addr.ip;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000121 if (ret_saddr)
122 *ret_saddr = dest->dst_saddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 spin_unlock(&dest->dst_lock);
124 } else {
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000125 struct flowi4 fl4;
126
127 memset(&fl4, 0, sizeof(fl4));
128 fl4.daddr = daddr;
129 fl4.flowi4_tos = rtos;
130 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800131 if (IS_ERR(rt)) {
Harvey Harrison14d5e832008-10-31 00:54:29 -0700132 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
Julian Anastasovfc604762010-10-17 16:38:15 +0300133 &daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return NULL;
135 }
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000136 if (ret_saddr)
137 *ret_saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Julian Anastasovfc604762010-10-17 16:38:15 +0300140 local = rt->rt_flags & RTCF_LOCAL;
Changli Gao17a8f8e2011-02-24 08:19:57 +0800141 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
142 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300143 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
144 (rt->rt_flags & RTCF_LOCAL) ?
David S. Miller44e31252011-05-09 14:38:06 -0700145 "local":"non-local", &daddr);
Julian Anastasovfc604762010-10-17 16:38:15 +0300146 ip_rt_put(rt);
147 return NULL;
148 }
Changli Gao17a8f8e2011-02-24 08:19:57 +0800149 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
150 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300151 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
152 "requires NAT method, dest: %pI4\n",
David S. Miller44e31252011-05-09 14:38:06 -0700153 &ip_hdr(skb)->daddr, &daddr);
Julian Anastasovfc604762010-10-17 16:38:15 +0300154 ip_rt_put(rt);
155 return NULL;
156 }
157 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
158 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
159 "to non-local address, dest: %pI4\n",
David S. Miller44e31252011-05-09 14:38:06 -0700160 &ip_hdr(skb)->saddr, &daddr);
Julian Anastasovfc604762010-10-17 16:38:15 +0300161 ip_rt_put(rt);
162 return NULL;
163 }
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return rt;
166}
167
Julian Anastasovfc604762010-10-17 16:38:15 +0300168/* Reroute packet to local IPv4 stack after DNAT */
169static int
170__ip_vs_reroute_locally(struct sk_buff *skb)
171{
172 struct rtable *rt = skb_rtable(skb);
173 struct net_device *dev = rt->dst.dev;
174 struct net *net = dev_net(dev);
175 struct iphdr *iph = ip_hdr(skb);
176
David S. Millerc7537962010-11-11 17:07:48 -0800177 if (rt_is_input_route(rt)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300178 unsigned long orefdst = skb->_skb_refdst;
179
180 if (ip_route_input(skb, iph->daddr, iph->saddr,
181 iph->tos, skb->dev))
182 return 0;
183 refdst_drop(orefdst);
184 } else {
David S. Miller9d6ec932011-03-12 01:12:47 -0500185 struct flowi4 fl4 = {
186 .daddr = iph->daddr,
187 .saddr = iph->saddr,
188 .flowi4_tos = RT_TOS(iph->tos),
189 .flowi4_mark = skb->mark,
Julian Anastasovfc604762010-10-17 16:38:15 +0300190 };
Julian Anastasovfc604762010-10-17 16:38:15 +0300191
David S. Miller9d6ec932011-03-12 01:12:47 -0500192 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800193 if (IS_ERR(rt))
Julian Anastasovfc604762010-10-17 16:38:15 +0300194 return 0;
195 if (!(rt->rt_flags & RTCF_LOCAL)) {
196 ip_rt_put(rt);
197 return 0;
198 }
199 /* Drop old route. */
200 skb_dst_drop(skb);
201 skb_dst_set(skb, &rt->dst);
202 }
203 return 1;
204}
205
Julius Volz38cdcc92008-09-02 15:55:44 +0200206#ifdef CONFIG_IP_VS_IPV6
Hans Schillstrom714f0952010-10-19 10:38:48 +0200207
Julian Anastasovfc604762010-10-17 16:38:15 +0300208static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
209{
210 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
211}
212
Hans Schillstrom714f0952010-10-19 10:38:48 +0200213static struct dst_entry *
214__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
215 struct in6_addr *ret_saddr, int do_xfrm)
Julius Volz38cdcc92008-09-02 15:55:44 +0200216{
Hans Schillstrom714f0952010-10-19 10:38:48 +0200217 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500218 struct flowi6 fl6 = {
219 .daddr = *daddr,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200220 };
221
David S. Miller4c9483b2011-03-12 16:22:43 -0500222 dst = ip6_route_output(net, NULL, &fl6);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200223 if (dst->error)
224 goto out_err;
225 if (!ret_saddr)
226 return dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500227 if (ipv6_addr_any(&fl6.saddr) &&
Hans Schillstrom714f0952010-10-19 10:38:48 +0200228 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
David S. Miller4c9483b2011-03-12 16:22:43 -0500229 &fl6.daddr, 0, &fl6.saddr) < 0)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200230 goto out_err;
David S. Miller452edd52011-03-02 13:27:41 -0800231 if (do_xfrm) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500232 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -0800233 if (IS_ERR(dst)) {
234 dst = NULL;
235 goto out_err;
236 }
237 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000238 *ret_saddr = fl6.saddr;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200239 return dst;
240
241out_err:
242 dst_release(dst);
243 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
244 return NULL;
245}
246
Julian Anastasovfc604762010-10-17 16:38:15 +0300247/*
248 * Get route to destination or remote server
Julian Anastasovfc604762010-10-17 16:38:15 +0300249 */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200250static struct rt6_info *
Julian Anastasovfc604762010-10-17 16:38:15 +0300251__ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
252 struct in6_addr *daddr, struct in6_addr *ret_saddr,
253 int do_xfrm, int rt_mode)
Hans Schillstrom714f0952010-10-19 10:38:48 +0200254{
Julian Anastasovfc604762010-10-17 16:38:15 +0300255 struct net *net = dev_net(skb_dst(skb)->dev);
Julius Volz38cdcc92008-09-02 15:55:44 +0200256 struct rt6_info *rt; /* Route to the other host */
Julian Anastasovfc604762010-10-17 16:38:15 +0300257 struct rt6_info *ort; /* Original route */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200258 struct dst_entry *dst;
Julian Anastasovfc604762010-10-17 16:38:15 +0300259 int local;
Julius Volz38cdcc92008-09-02 15:55:44 +0200260
261 if (dest) {
262 spin_lock(&dest->dst_lock);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200263 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
Julius Volz38cdcc92008-09-02 15:55:44 +0200264 if (!rt) {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200265 u32 cookie;
Julius Volz38cdcc92008-09-02 15:55:44 +0200266
Hans Schillstrom714f0952010-10-19 10:38:48 +0200267 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000268 &dest->dst_saddr.in6,
Hans Schillstrom714f0952010-10-19 10:38:48 +0200269 do_xfrm);
270 if (!dst) {
Julius Volz38cdcc92008-09-02 15:55:44 +0200271 spin_unlock(&dest->dst_lock);
Julius Volz38cdcc92008-09-02 15:55:44 +0200272 return NULL;
273 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200274 rt = (struct rt6_info *) dst;
275 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
276 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
277 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000278 &dest->addr.in6, &dest->dst_saddr.in6,
Changli Gaod8d1f302010-06-10 23:31:35 -0700279 atomic_read(&rt->dst.__refcnt));
Julius Volz38cdcc92008-09-02 15:55:44 +0200280 }
Hans Schillstrom714f0952010-10-19 10:38:48 +0200281 if (ret_saddr)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000282 *ret_saddr = dest->dst_saddr.in6;
Julius Volz38cdcc92008-09-02 15:55:44 +0200283 spin_unlock(&dest->dst_lock);
284 } else {
Julian Anastasovfc604762010-10-17 16:38:15 +0300285 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200286 if (!dst)
Julius Volz38cdcc92008-09-02 15:55:44 +0200287 return NULL;
Hans Schillstrom714f0952010-10-19 10:38:48 +0200288 rt = (struct rt6_info *) dst;
Julius Volz38cdcc92008-09-02 15:55:44 +0200289 }
290
Julian Anastasovfc604762010-10-17 16:38:15 +0300291 local = __ip_vs_is_local_route6(rt);
David S. Millere58b3442011-05-12 18:22:34 -0400292 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
293 rt_mode)) {
Julian Anastasovfc604762010-10-17 16:38:15 +0300294 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
295 local ? "local":"non-local", daddr);
296 dst_release(&rt->dst);
297 return NULL;
298 }
David S. Millere58b3442011-05-12 18:22:34 -0400299 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
Julian Anastasovfc604762010-10-17 16:38:15 +0300300 !((ort = (struct rt6_info *) skb_dst(skb)) &&
301 __ip_vs_is_local_route6(ort))) {
302 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
303 "requires NAT method, dest: %pI6\n",
304 &ipv6_hdr(skb)->daddr, daddr);
305 dst_release(&rt->dst);
306 return NULL;
307 }
308 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
309 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
310 IPV6_ADDR_LOOPBACK)) {
311 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
312 "to non-local address, dest: %pI6\n",
313 &ipv6_hdr(skb)->saddr, daddr);
314 dst_release(&rt->dst);
315 return NULL;
316 }
317
Julius Volz38cdcc92008-09-02 15:55:44 +0200318 return rt;
319}
320#endif
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/*
324 * Release dest->dst_cache before a dest is removed
325 */
326void
327ip_vs_dst_reset(struct ip_vs_dest *dest)
328{
329 struct dst_entry *old_dst;
330
331 old_dst = dest->dst_cache;
332 dest->dst_cache = NULL;
333 dst_release(old_dst);
334}
335
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200336#define IP_VS_XMIT_TUNNEL(skb, cp) \
337({ \
338 int __ret = NF_ACCEPT; \
339 \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300340 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200341 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
Simon Horman3c2de2a2011-09-16 14:02:19 +0900342 __ret = ip_vs_confirm_conntrack(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200343 if (__ret == NF_ACCEPT) { \
344 nf_reset(skb); \
Julian Anastasov4256f1a2010-10-17 16:29:40 +0300345 skb_forward_csum(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200346 } \
347 __ret; \
348})
349
Julian Anastasovfc604762010-10-17 16:38:15 +0300350#define IP_VS_XMIT_NAT(pf, skb, cp, local) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351do { \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300352 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200353 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300354 ip_vs_notrack(skb); \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200355 else \
356 ip_vs_update_conntrack(skb, cp, 1); \
Julian Anastasovfc604762010-10-17 16:38:15 +0300357 if (local) \
358 return NF_ACCEPT; \
Herbert Xuccc79112007-07-30 16:20:12 -0700359 skb_forward_csum(skb); \
Julius Volz38cdcc92008-09-02 15:55:44 +0200360 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200361 skb_dst(skb)->dev, dst_output); \
362} while (0)
363
Julian Anastasovfc604762010-10-17 16:38:15 +0300364#define IP_VS_XMIT(pf, skb, cp, local) \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200365do { \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300366 (skb)->ipvs_property = 1; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200367 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
Julian Anastasovcf356d62010-10-17 16:21:07 +0300368 ip_vs_notrack(skb); \
Julian Anastasovfc604762010-10-17 16:38:15 +0300369 if (local) \
370 return NF_ACCEPT; \
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200371 skb_forward_csum(skb); \
372 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
373 skb_dst(skb)->dev, dst_output); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374} while (0)
375
376
377/*
378 * NULL transmitter (do nothing except return NF_ACCEPT)
379 */
380int
381ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
382 struct ip_vs_protocol *pp)
383{
384 /* we do not touch skb and do not need pskb ptr */
Julian Anastasovfc604762010-10-17 16:38:15 +0300385 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388
389/*
390 * Bypass transmitter
391 * Let packets bypass the destination when the destination is not
392 * available, it may be only used in transparent cache cluster.
393 */
394int
395ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
396 struct ip_vs_protocol *pp)
397{
398 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700399 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 EnterFunction(10);
403
Changli Gao17a8f8e2011-02-24 08:19:57 +0800404 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000405 IP_VS_RT_MODE_NON_LOCAL, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto tx_error_icmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700409 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900410 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
411 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ip_rt_put(rt);
413 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000414 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto tx_error;
416 }
417
418 /*
419 * Call ip_send_check because we are not sure it is called
420 * after ip_defrag. Is copy-on-write needed?
421 */
422 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
423 ip_rt_put(rt);
424 return NF_STOLEN;
425 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700426 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000429 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700430 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Another hack: avoid icmp_send in ip_fragment */
433 skb->local_df = 1;
434
Julian Anastasovfc604762010-10-17 16:38:15 +0300435 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 LeaveFunction(10);
438 return NF_STOLEN;
439
440 tx_error_icmp:
441 dst_link_failure(skb);
442 tx_error:
443 kfree_skb(skb);
444 LeaveFunction(10);
445 return NF_STOLEN;
446}
447
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200448#ifdef CONFIG_IP_VS_IPV6
449int
450ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
451 struct ip_vs_protocol *pp)
452{
453 struct rt6_info *rt; /* Route to the other host */
454 struct ipv6hdr *iph = ipv6_hdr(skb);
455 int mtu;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200456
457 EnterFunction(10);
458
David S. Millere58b3442011-05-12 18:22:34 -0400459 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0,
460 IP_VS_RT_MODE_NON_LOCAL)))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200461 goto tx_error_icmp;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200462
463 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700464 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900465 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300466 if (!skb->dev) {
467 struct net *net = dev_net(skb_dst(skb)->dev);
468
469 skb->dev = net->loopback_dev;
470 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000471 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Julian Anastasovcb591552010-10-17 16:40:51 +0300472 dst_release(&rt->dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000473 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200474 goto tx_error;
475 }
476
477 /*
478 * Call ip_send_check because we are not sure it is called
479 * after ip_defrag. Is copy-on-write needed?
480 */
481 skb = skb_share_check(skb, GFP_ATOMIC);
482 if (unlikely(skb == NULL)) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700483 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200484 return NF_STOLEN;
485 }
486
487 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000488 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700489 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200490
491 /* Another hack: avoid icmp_send in ip_fragment */
492 skb->local_df = 1;
493
Julian Anastasovfc604762010-10-17 16:38:15 +0300494 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200495
496 LeaveFunction(10);
497 return NF_STOLEN;
498
499 tx_error_icmp:
500 dst_link_failure(skb);
501 tx_error:
502 kfree_skb(skb);
503 LeaveFunction(10);
504 return NF_STOLEN;
505}
506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508/*
509 * NAT transmitter (only for outside-to-inside nat forwarding)
510 * Not used for related ICMP
511 */
512int
513ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
514 struct ip_vs_protocol *pp)
515{
516 struct rtable *rt; /* Route to the other host */
517 int mtu;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700518 struct iphdr *iph = ip_hdr(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +0300519 int local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 EnterFunction(10);
522
523 /* check if it is a connection of no-client-port */
524 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
Al Viro014d7302006-09-28 14:29:52 -0700525 __be16 _pt, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
527 if (p == NULL)
528 goto tx_error;
529 ip_vs_conn_fill_cport(cp, *p);
530 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
531 }
532
Julian Anastasovfc604762010-10-17 16:38:15 +0300533 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800534 RT_TOS(iph->tos),
535 IP_VS_RT_MODE_LOCAL |
536 IP_VS_RT_MODE_NON_LOCAL |
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000537 IP_VS_RT_MODE_RDR, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300539 local = rt->rt_flags & RTCF_LOCAL;
540 /*
541 * Avoid duplicate tuple in reply direction for NAT traffic
542 * to local address when connection is sync-ed
543 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000544#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300545 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
546 enum ip_conntrack_info ctinfo;
547 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
548
549 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300550 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
551 "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300552 "stopping DNAT to local address");
553 goto tx_error_put;
554 }
555 }
556#endif
557
558 /* From world but DNAT to loopback address? */
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000559 if (local && ipv4_is_loopback(cp->daddr.ip) &&
David S. Millerc7537962010-11-11 17:07:48 -0800560 rt_is_input_route(skb_rtable(skb))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300561 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
Julian Anastasovfc604762010-10-17 16:38:15 +0300562 "stopping DNAT to loopback address");
563 goto tx_error_put;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700567 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900568 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
569 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Julian Anastasov0d796412010-10-17 16:46:17 +0300571 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
572 "ip_vs_nat_xmit(): frag needed for");
Julian Anastasovfc604762010-10-17 16:38:15 +0300573 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
576 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -0700577 if (!skb_make_writable(skb, sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto tx_error_put;
579
Changli Gaod8d1f302010-06-10 23:31:35 -0700580 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto tx_error_put;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* mangle the packet */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700584 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
Julian Anastasovfc604762010-10-17 16:38:15 +0300585 goto tx_error_put;
Julius Volze7ade462008-09-02 15:55:33 +0200586 ip_hdr(skb)->daddr = cp->daddr.ip;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700587 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Julian Anastasovfc604762010-10-17 16:38:15 +0300589 if (!local) {
590 /* drop old route */
591 skb_dst_drop(skb);
592 skb_dst_set(skb, &rt->dst);
593 } else {
594 ip_rt_put(rt);
595 /*
596 * Some IPv4 replies get local address from routes,
597 * not from iph, so while we DNAT after routing
598 * we need this second input/output route.
599 */
600 if (!__ip_vs_reroute_locally(skb))
601 goto tx_error;
602 }
603
Julian Anastasov0d796412010-10-17 16:46:17 +0300604 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* FIXME: when application helper enlarges the packet and the length
607 is larger than the MTU of outgoing device, there will be still
608 MTU problem. */
609
610 /* Another hack: avoid icmp_send in ip_fragment */
611 skb->local_df = 1;
612
Julian Anastasovfc604762010-10-17 16:38:15 +0300613 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 LeaveFunction(10);
616 return NF_STOLEN;
617
618 tx_error_icmp:
619 dst_link_failure(skb);
620 tx_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200622 LeaveFunction(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return NF_STOLEN;
624 tx_error_put:
625 ip_rt_put(rt);
626 goto tx_error;
627}
628
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200629#ifdef CONFIG_IP_VS_IPV6
630int
631ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
632 struct ip_vs_protocol *pp)
633{
634 struct rt6_info *rt; /* Route to the other host */
635 int mtu;
Julian Anastasovfc604762010-10-17 16:38:15 +0300636 int local;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200637
638 EnterFunction(10);
639
640 /* check if it is a connection of no-client-port */
641 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
642 __be16 _pt, *p;
643 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
644 sizeof(_pt), &_pt);
645 if (p == NULL)
646 goto tx_error;
647 ip_vs_conn_fill_cport(cp, *p);
648 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
649 }
650
Julian Anastasovfc604762010-10-17 16:38:15 +0300651 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -0400652 0, (IP_VS_RT_MODE_LOCAL |
653 IP_VS_RT_MODE_NON_LOCAL |
654 IP_VS_RT_MODE_RDR))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200655 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300656 local = __ip_vs_is_local_route6(rt);
657 /*
658 * Avoid duplicate tuple in reply direction for NAT traffic
659 * to local address when connection is sync-ed
660 */
Igor Maravićc0cd1152011-12-12 02:58:24 +0000661#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +0300662 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
663 enum ip_conntrack_info ctinfo;
664 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
665
666 if (ct && !nf_ct_is_untracked(ct)) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300667 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300668 "ip_vs_nat_xmit_v6(): "
669 "stopping DNAT to local address");
670 goto tx_error_put;
671 }
672 }
673#endif
674
675 /* From world but DNAT to loopback address? */
676 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
677 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300678 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
Julian Anastasovfc604762010-10-17 16:38:15 +0300679 "ip_vs_nat_xmit_v6(): "
680 "stopping DNAT to loopback address");
681 goto tx_error_put;
682 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200683
684 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -0700685 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +0900686 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300687 if (!skb->dev) {
688 struct net *net = dev_net(skb_dst(skb)->dev);
689
690 skb->dev = net->loopback_dev;
691 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000692 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Julian Anastasov0d796412010-10-17 16:46:17 +0300693 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200694 "ip_vs_nat_xmit_v6(): frag needed for");
Julian Anastasovfc604762010-10-17 16:38:15 +0300695 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200696 }
697
698 /* copy-on-write the packet before mangling it */
699 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
700 goto tx_error_put;
701
Changli Gaod8d1f302010-06-10 23:31:35 -0700702 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200703 goto tx_error_put;
704
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200705 /* mangle the packet */
706 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
707 goto tx_error;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000708 ipv6_hdr(skb)->daddr = cp->daddr.in6;
Julian Anastasovfc604762010-10-17 16:38:15 +0300709
710 if (!local || !skb->dev) {
711 /* drop the old route when skb is not shared */
712 skb_dst_drop(skb);
713 skb_dst_set(skb, &rt->dst);
714 } else {
715 /* destined to loopback, do we need to change route? */
716 dst_release(&rt->dst);
717 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200718
Julian Anastasov0d796412010-10-17 16:46:17 +0300719 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200720
721 /* FIXME: when application helper enlarges the packet and the length
722 is larger than the MTU of outgoing device, there will be still
723 MTU problem. */
724
725 /* Another hack: avoid icmp_send in ip_fragment */
726 skb->local_df = 1;
727
Julian Anastasovfc604762010-10-17 16:38:15 +0300728 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200729
730 LeaveFunction(10);
731 return NF_STOLEN;
732
733tx_error_icmp:
734 dst_link_failure(skb);
735tx_error:
736 LeaveFunction(10);
737 kfree_skb(skb);
738 return NF_STOLEN;
739tx_error_put:
Changli Gaod8d1f302010-06-10 23:31:35 -0700740 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200741 goto tx_error;
742}
743#endif
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746/*
747 * IP Tunneling transmitter
748 *
749 * This function encapsulates the packet in a new IP packet, its
750 * destination will be set to cp->daddr. Most code of this function
751 * is taken from ipip.c.
752 *
753 * It is used in VS/TUN cluster. The load balancer selects a real
754 * server from a cluster based on a scheduling algorithm,
755 * encapsulates the request packet and forwards it to the selected
756 * server. For example, all real servers are configured with
757 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
758 * the encapsulated packet, it will decapsulate the packet, processe
759 * the request and return the response packets directly to the client
760 * without passing the load balancer. This can greatly increase the
761 * scalability of virtual server.
762 *
763 * Used for ANY protocol
764 */
765int
766ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
767 struct ip_vs_protocol *pp)
768{
769 struct rtable *rt; /* Route to the other host */
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000770 __be32 saddr; /* Source for tunnel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct net_device *tdev; /* Device to other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700772 struct iphdr *old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 u8 tos = old_iph->tos;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800774 __be16 df = old_iph->frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct iphdr *iph; /* Our new IP header */
Chuck Leverc2636b42007-10-23 21:07:32 -0700776 unsigned int max_headroom; /* The extra header space needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int mtu;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200778 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 EnterFunction(10);
781
Julian Anastasovfc604762010-10-17 16:38:15 +0300782 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +0800783 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000784 IP_VS_RT_MODE_NON_LOCAL,
785 &saddr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300787 if (rt->rt_flags & RTCF_LOCAL) {
788 ip_rt_put(rt);
789 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Changli Gaod8d1f302010-06-10 23:31:35 -0700792 tdev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Changli Gaod8d1f302010-06-10 23:31:35 -0700794 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (mtu < 68) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000796 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300797 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000799 if (skb_dst(skb))
800 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900802 df |= (old_iph->frag_off & htons(IP_DF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Simon Horman8f1b03a2010-11-09 10:08:49 +0900804 if ((old_iph->frag_off & htons(IP_DF) &&
805 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +0000807 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300808 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 /*
812 * Okay, now see if we can stuff it in the buffer as-is.
813 */
814 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
815
816 if (skb_headroom(skb) < max_headroom
817 || skb_cloned(skb) || skb_shared(skb)) {
818 struct sk_buff *new_skb =
819 skb_realloc_headroom(skb, max_headroom);
820 if (!new_skb) {
821 ip_rt_put(rt);
822 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000823 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return NF_STOLEN;
825 }
826 kfree_skb(skb);
827 skb = new_skb;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700828 old_iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Hans Schillstrom714f0952010-10-19 10:38:48 +0200831 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* fix old IP header checksum */
834 ip_send_check(old_iph);
835
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700836 skb_push(skb, sizeof(struct iphdr));
837 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
839
840 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000841 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700842 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
845 * Push down and install the IPIP header.
846 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700847 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 iph->version = 4;
849 iph->ihl = sizeof(struct iphdr)>>2;
850 iph->frag_off = df;
851 iph->protocol = IPPROTO_IPIP;
852 iph->tos = tos;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +0000853 iph->daddr = cp->daddr.ip;
854 iph->saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 iph->ttl = old_iph->ttl;
Changli Gaod8d1f302010-06-10 23:31:35 -0700856 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Another hack: avoid icmp_send in ip_fragment */
859 skb->local_df = 1;
860
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200861 ret = IP_VS_XMIT_TUNNEL(skb, cp);
862 if (ret == NF_ACCEPT)
863 ip_local_out(skb);
864 else if (ret == NF_DROP)
865 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 LeaveFunction(10);
868
869 return NF_STOLEN;
870
871 tx_error_icmp:
872 dst_link_failure(skb);
873 tx_error:
874 kfree_skb(skb);
875 LeaveFunction(10);
876 return NF_STOLEN;
Julian Anastasovfc604762010-10-17 16:38:15 +0300877tx_error_put:
878 ip_rt_put(rt);
879 goto tx_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200882#ifdef CONFIG_IP_VS_IPV6
883int
884ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
885 struct ip_vs_protocol *pp)
886{
887 struct rt6_info *rt; /* Route to the other host */
Hans Schillstrom714f0952010-10-19 10:38:48 +0200888 struct in6_addr saddr; /* Source for tunnel */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200889 struct net_device *tdev; /* Device to other host */
890 struct ipv6hdr *old_iph = ipv6_hdr(skb);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200891 struct ipv6hdr *iph; /* Our new IP header */
892 unsigned int max_headroom; /* The extra header space needed */
893 int mtu;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200894 int ret;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200895
896 EnterFunction(10);
897
Julian Anastasovfc604762010-10-17 16:38:15 +0300898 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
David S. Millere58b3442011-05-12 18:22:34 -0400899 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
900 IP_VS_RT_MODE_NON_LOCAL))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200901 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +0300902 if (__ip_vs_is_local_route6(rt)) {
903 dst_release(&rt->dst);
904 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
905 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200906
Changli Gaod8d1f302010-06-10 23:31:35 -0700907 tdev = rt->dst.dev;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200908
Changli Gaod8d1f302010-06-10 23:31:35 -0700909 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
Hans Schillstrom714f0952010-10-19 10:38:48 +0200910 if (mtu < IPV6_MIN_MTU) {
Hans Schillstrom714f0952010-10-19 10:38:48 +0200911 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
912 IPV6_MIN_MTU);
Julian Anastasovfc604762010-10-17 16:38:15 +0300913 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200914 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000915 if (skb_dst(skb))
916 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200917
Simon Horman8f1b03a2010-11-09 10:08:49 +0900918 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
919 !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +0300920 if (!skb->dev) {
921 struct net *net = dev_net(skb_dst(skb)->dev);
922
923 skb->dev = net->loopback_dev;
924 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000925 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000926 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +0300927 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200928 }
929
930 /*
931 * Okay, now see if we can stuff it in the buffer as-is.
932 */
933 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
934
935 if (skb_headroom(skb) < max_headroom
936 || skb_cloned(skb) || skb_shared(skb)) {
937 struct sk_buff *new_skb =
938 skb_realloc_headroom(skb, max_headroom);
939 if (!new_skb) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700940 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200941 kfree_skb(skb);
Hannes Eder1e3e2382009-08-02 11:05:41 +0000942 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200943 return NF_STOLEN;
944 }
945 kfree_skb(skb);
946 skb = new_skb;
947 old_iph = ipv6_hdr(skb);
948 }
949
Hans Schillstrom714f0952010-10-19 10:38:48 +0200950 skb->transport_header = skb->network_header;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200951
952 skb_push(skb, sizeof(struct ipv6hdr));
953 skb_reset_network_header(skb);
954 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
955
956 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +0000957 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700958 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200959
960 /*
961 * Push down and install the IPIP header.
962 */
963 iph = ipv6_hdr(skb);
964 iph->version = 6;
965 iph->nexthdr = IPPROTO_IPV6;
Harvey Harrisonb7b45f42008-11-10 16:46:06 -0800966 iph->payload_len = old_iph->payload_len;
967 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200968 iph->priority = old_iph->priority;
969 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000970 iph->daddr = cp->daddr.in6;
971 iph->saddr = saddr;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200972 iph->hop_limit = old_iph->hop_limit;
973
974 /* Another hack: avoid icmp_send in ip_fragment */
975 skb->local_df = 1;
976
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200977 ret = IP_VS_XMIT_TUNNEL(skb, cp);
978 if (ret == NF_ACCEPT)
979 ip6_local_out(skb);
980 else if (ret == NF_DROP)
981 kfree_skb(skb);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200982
983 LeaveFunction(10);
984
985 return NF_STOLEN;
986
987tx_error_icmp:
988 dst_link_failure(skb);
989tx_error:
990 kfree_skb(skb);
991 LeaveFunction(10);
992 return NF_STOLEN;
Julian Anastasovfc604762010-10-17 16:38:15 +0300993tx_error_put:
994 dst_release(&rt->dst);
995 goto tx_error;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200996}
997#endif
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000/*
1001 * Direct Routing transmitter
1002 * Used for ANY protocol
1003 */
1004int
1005ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1006 struct ip_vs_protocol *pp)
1007{
1008 struct rtable *rt; /* Route to the other host */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001009 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int mtu;
1011
1012 EnterFunction(10);
1013
Julian Anastasovfc604762010-10-17 16:38:15 +03001014 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +08001015 RT_TOS(iph->tos),
1016 IP_VS_RT_MODE_LOCAL |
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001017 IP_VS_RT_MODE_NON_LOCAL, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001019 if (rt->rt_flags & RTCF_LOCAL) {
1020 ip_rt_put(rt);
1021 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001025 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001026 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1027 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1029 ip_rt_put(rt);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001030 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto tx_error;
1032 }
1033
1034 /*
1035 * Call ip_send_check because we are not sure it is called
1036 * after ip_defrag. Is copy-on-write needed?
1037 */
1038 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1039 ip_rt_put(rt);
1040 return NF_STOLEN;
1041 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001042 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +00001045 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001046 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* Another hack: avoid icmp_send in ip_fragment */
1049 skb->local_df = 1;
1050
Julian Anastasovfc604762010-10-17 16:38:15 +03001051 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 LeaveFunction(10);
1054 return NF_STOLEN;
1055
1056 tx_error_icmp:
1057 dst_link_failure(skb);
1058 tx_error:
1059 kfree_skb(skb);
1060 LeaveFunction(10);
1061 return NF_STOLEN;
1062}
1063
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001064#ifdef CONFIG_IP_VS_IPV6
1065int
1066ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1067 struct ip_vs_protocol *pp)
1068{
1069 struct rt6_info *rt; /* Route to the other host */
1070 int mtu;
1071
1072 EnterFunction(10);
1073
Julian Anastasovfc604762010-10-17 16:38:15 +03001074 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
David S. Millere58b3442011-05-12 18:22:34 -04001075 0, (IP_VS_RT_MODE_LOCAL |
1076 IP_VS_RT_MODE_NON_LOCAL))))
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001077 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001078 if (__ip_vs_is_local_route6(rt)) {
1079 dst_release(&rt->dst);
1080 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1081 }
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001082
1083 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001084 mtu = dst_mtu(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001085 if (skb->len > mtu) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001086 if (!skb->dev) {
1087 struct net *net = dev_net(skb_dst(skb)->dev);
1088
1089 skb->dev = net->loopback_dev;
1090 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001091 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Changli Gaod8d1f302010-06-10 23:31:35 -07001092 dst_release(&rt->dst);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001093 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001094 goto tx_error;
1095 }
1096
1097 /*
1098 * Call ip_send_check because we are not sure it is called
1099 * after ip_defrag. Is copy-on-write needed?
1100 */
1101 skb = skb_share_check(skb, GFP_ATOMIC);
1102 if (unlikely(skb == NULL)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001103 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001104 return NF_STOLEN;
1105 }
1106
1107 /* drop old route */
Eric Dumazetadf30902009-06-02 05:19:30 +00001108 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001109 skb_dst_set(skb, &rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001110
1111 /* Another hack: avoid icmp_send in ip_fragment */
1112 skb->local_df = 1;
1113
Julian Anastasovfc604762010-10-17 16:38:15 +03001114 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001115
1116 LeaveFunction(10);
1117 return NF_STOLEN;
1118
1119tx_error_icmp:
1120 dst_link_failure(skb);
1121tx_error:
1122 kfree_skb(skb);
1123 LeaveFunction(10);
1124 return NF_STOLEN;
1125}
1126#endif
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129/*
1130 * ICMP packet transmitter
1131 * called by the ip_vs_in_icmp
1132 */
1133int
1134ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001135 struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct rtable *rt; /* Route to the other host */
1138 int mtu;
1139 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001140 int local;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001141 int rt_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 EnterFunction(10);
1144
1145 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1146 forwarded directly here, because there is no need to
1147 translate address/port back */
1148 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1149 if (cp->packet_xmit)
1150 rc = cp->packet_xmit(skb, cp, pp);
1151 else
1152 rc = NF_ACCEPT;
1153 /* do not touch skb anymore */
1154 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 goto out;
1156 }
1157
1158 /*
1159 * mangle and send the packet here (only for VS/NAT)
1160 */
1161
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001162 /* LOCALNODE from FORWARD hook is not supported */
1163 rt_mode = (hooknum != NF_INET_FORWARD) ?
1164 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1165 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasovfc604762010-10-17 16:38:15 +03001166 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
Changli Gao17a8f8e2011-02-24 08:19:57 +08001167 RT_TOS(ip_hdr(skb)->tos),
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001168 rt_mode, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto tx_error_icmp;
Julian Anastasovfc604762010-10-17 16:38:15 +03001170 local = rt->rt_flags & RTCF_LOCAL;
1171
1172 /*
1173 * Avoid duplicate tuple in reply direction for NAT traffic
1174 * to local address when connection is sync-ed
1175 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001176#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001177 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1178 enum ip_conntrack_info ctinfo;
1179 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1180
1181 if (ct && !nf_ct_is_untracked(ct)) {
1182 IP_VS_DBG(10, "%s(): "
1183 "stopping DNAT to local address %pI4\n",
1184 __func__, &cp->daddr.ip);
1185 goto tx_error_put;
1186 }
1187 }
1188#endif
1189
1190 /* From world but DNAT to loopback address? */
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001191 if (local && ipv4_is_loopback(cp->daddr.ip) &&
David S. Millerc7537962010-11-11 17:07:48 -08001192 rt_is_input_route(skb_rtable(skb))) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001193 IP_VS_DBG(1, "%s(): "
1194 "stopping DNAT to loopback %pI4\n",
1195 __func__, &cp->daddr.ip);
1196 goto tx_error_put;
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001200 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001201 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1202 !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
Hannes Eder1e3e2382009-08-02 11:05:41 +00001204 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +03001205 goto tx_error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 /* copy-on-write the packet before mangling it */
Herbert Xuaf1e1cf2007-10-14 00:39:33 -07001209 if (!skb_make_writable(skb, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto tx_error_put;
1211
Changli Gaod8d1f302010-06-10 23:31:35 -07001212 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 goto tx_error_put;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ip_vs_nat_icmp(skb, pp, cp, 0);
1216
Julian Anastasovfc604762010-10-17 16:38:15 +03001217 if (!local) {
1218 /* drop the old route when skb is not shared */
1219 skb_dst_drop(skb);
1220 skb_dst_set(skb, &rt->dst);
1221 } else {
1222 ip_rt_put(rt);
1223 /*
1224 * Some IPv4 replies get local address from routes,
1225 * not from iph, so while we DNAT after routing
1226 * we need this second input/output route.
1227 */
1228 if (!__ip_vs_reroute_locally(skb))
1229 goto tx_error;
1230 }
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* Another hack: avoid icmp_send in ip_fragment */
1233 skb->local_df = 1;
1234
Julian Anastasovfc604762010-10-17 16:38:15 +03001235 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 rc = NF_STOLEN;
1238 goto out;
1239
1240 tx_error_icmp:
1241 dst_link_failure(skb);
1242 tx_error:
1243 dev_kfree_skb(skb);
1244 rc = NF_STOLEN;
1245 out:
1246 LeaveFunction(10);
1247 return rc;
1248 tx_error_put:
1249 ip_rt_put(rt);
1250 goto tx_error;
1251}
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001252
1253#ifdef CONFIG_IP_VS_IPV6
1254int
1255ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001256 struct ip_vs_protocol *pp, int offset, unsigned int hooknum)
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001257{
1258 struct rt6_info *rt; /* Route to the other host */
1259 int mtu;
1260 int rc;
Julian Anastasovfc604762010-10-17 16:38:15 +03001261 int local;
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001262 int rt_mode;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001263
1264 EnterFunction(10);
1265
1266 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1267 forwarded directly here, because there is no need to
1268 translate address/port back */
1269 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1270 if (cp->packet_xmit)
1271 rc = cp->packet_xmit(skb, cp, pp);
1272 else
1273 rc = NF_ACCEPT;
1274 /* do not touch skb anymore */
1275 atomic_inc(&cp->in_pkts);
1276 goto out;
1277 }
1278
1279 /*
1280 * mangle and send the packet here (only for VS/NAT)
1281 */
1282
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001283 /* LOCALNODE from FORWARD hook is not supported */
1284 rt_mode = (hooknum != NF_INET_FORWARD) ?
1285 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1286 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
Julian Anastasovfc604762010-10-17 16:38:15 +03001287 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
Julian Anastasovc92f5ca2011-05-10 12:46:05 +00001288 0, rt_mode)))
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001289 goto tx_error_icmp;
1290
Julian Anastasovfc604762010-10-17 16:38:15 +03001291 local = __ip_vs_is_local_route6(rt);
1292 /*
1293 * Avoid duplicate tuple in reply direction for NAT traffic
1294 * to local address when connection is sync-ed
1295 */
Igor Maravićc0cd1152011-12-12 02:58:24 +00001296#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Julian Anastasovfc604762010-10-17 16:38:15 +03001297 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1298 enum ip_conntrack_info ctinfo;
1299 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1300
1301 if (ct && !nf_ct_is_untracked(ct)) {
1302 IP_VS_DBG(10, "%s(): "
1303 "stopping DNAT to local address %pI6\n",
1304 __func__, &cp->daddr.in6);
1305 goto tx_error_put;
1306 }
1307 }
1308#endif
1309
1310 /* From world but DNAT to loopback address? */
1311 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1312 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1313 IP_VS_DBG(1, "%s(): "
1314 "stopping DNAT to loopback %pI6\n",
1315 __func__, &cp->daddr.in6);
1316 goto tx_error_put;
1317 }
1318
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001319 /* MTU checking */
Changli Gaod8d1f302010-06-10 23:31:35 -07001320 mtu = dst_mtu(&rt->dst);
Simon Horman8f1b03a2010-11-09 10:08:49 +09001321 if (skb->len > mtu && !skb_is_gso(skb)) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001322 if (!skb->dev) {
1323 struct net *net = dev_net(skb_dst(skb)->dev);
1324
1325 skb->dev = net->loopback_dev;
1326 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001327 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Hannes Eder1e3e2382009-08-02 11:05:41 +00001328 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
Julian Anastasovfc604762010-10-17 16:38:15 +03001329 goto tx_error_put;
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001330 }
1331
1332 /* copy-on-write the packet before mangling it */
1333 if (!skb_make_writable(skb, offset))
1334 goto tx_error_put;
1335
Changli Gaod8d1f302010-06-10 23:31:35 -07001336 if (skb_cow(skb, rt->dst.dev->hard_header_len))
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001337 goto tx_error_put;
1338
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001339 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1340
Julian Anastasovfc604762010-10-17 16:38:15 +03001341 if (!local || !skb->dev) {
1342 /* drop the old route when skb is not shared */
1343 skb_dst_drop(skb);
1344 skb_dst_set(skb, &rt->dst);
1345 } else {
1346 /* destined to loopback, do we need to change route? */
1347 dst_release(&rt->dst);
1348 }
1349
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001350 /* Another hack: avoid icmp_send in ip_fragment */
1351 skb->local_df = 1;
1352
Julian Anastasovfc604762010-10-17 16:38:15 +03001353 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001354
1355 rc = NF_STOLEN;
1356 goto out;
1357
1358tx_error_icmp:
1359 dst_link_failure(skb);
1360tx_error:
1361 dev_kfree_skb(skb);
1362 rc = NF_STOLEN;
1363out:
1364 LeaveFunction(10);
1365 return rc;
1366tx_error_put:
Changli Gaod8d1f302010-06-10 23:31:35 -07001367 dst_release(&rt->dst);
Julius Volzb3cdd2a72008-09-02 15:55:45 +02001368 goto tx_error;
1369}
1370#endif