ipvs: Eliminate rt->rt_dst usage in __ip_vs_get_out_rt().
[deliverable/linux.git] / net / netfilter / ipvs / ip_vs_xmit.c
CommitLineData
1da177e4
LT
1/*
2 * ip_vs_xmit.c: various packet transmitters for IPVS
3 *
1da177e4
LT
4 * 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 *
cb59155f
JA
14 * Description of forwarding methods:
15 * - all transmitters are called from LOCAL_IN (remote clients) and
16 * LOCAL_OUT (local clients) but for ICMP can be called from FORWARD
17 * - not all connections have destination server, for example,
18 * connections in backup server when fwmark is used
19 * - bypass connections use daddr from packet
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
1da177e4
LT
24 */
25
9aada7ac
HE
26#define KMSG_COMPONENT "IPVS"
27#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
1da177e4 29#include <linux/kernel.h>
5a0e3ad6 30#include <linux/slab.h>
1da177e4 31#include <linux/tcp.h> /* for tcphdr */
c439cb2e 32#include <net/ip.h>
1da177e4
LT
33#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 */
38cdcc9a
JV
37#include <net/ipv6.h>
38#include <net/ip6_route.h>
714f095f 39#include <net/addrconf.h>
38cdcc9a 40#include <linux/icmpv6.h>
1da177e4
LT
41#include <linux/netfilter.h>
42#include <linux/netfilter_ipv4.h>
43
44#include <net/ip_vs.h>
45
17a8f8e3
CG
46enum {
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};
1da177e4
LT
53
54/*
55 * Destination cache to speed up outgoing route lookup
56 */
57static inline void
714f095f
HS
58__ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst,
59 u32 dst_cookie)
1da177e4
LT
60{
61 struct dst_entry *old_dst;
62
63 old_dst = dest->dst_cache;
64 dest->dst_cache = dst;
65 dest->dst_rtos = rtos;
714f095f 66 dest->dst_cookie = dst_cookie;
1da177e4
LT
67 dst_release(old_dst);
68}
69
70static inline struct dst_entry *
714f095f 71__ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
1da177e4
LT
72{
73 struct dst_entry *dst = dest->dst_cache;
74
75 if (!dst)
76 return NULL;
714f095f
HS
77 if ((dst->obsolete || rtos != dest->dst_rtos) &&
78 dst->ops->check(dst, dest->dst_cookie) == NULL) {
1da177e4
LT
79 dest->dst_cache = NULL;
80 dst_release(dst);
81 return NULL;
82 }
83 dst_hold(dst);
84 return dst;
85}
86
17a8f8e3 87/* Get route to destination or remote server */
ad1b30b1 88static struct rtable *
fc604767
JA
89__ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
90 __be32 daddr, u32 rtos, int rt_mode)
1da177e4 91{
fc604767 92 struct net *net = dev_net(skb_dst(skb)->dev);
1da177e4 93 struct rtable *rt; /* Route to the other host */
fc604767
JA
94 struct rtable *ort; /* Original route */
95 int local;
1da177e4
LT
96
97 if (dest) {
98 spin_lock(&dest->dst_lock);
99 if (!(rt = (struct rtable *)
714f095f 100 __ip_vs_dst_check(dest, rtos))) {
78fbfd8a 101 rt = ip_route_output(net, dest->addr.ip, 0, rtos, 0);
b23dd4fe 102 if (IS_ERR(rt)) {
1da177e4 103 spin_unlock(&dest->dst_lock);
14d5e834
HH
104 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
105 &dest->addr.ip);
1da177e4
LT
106 return NULL;
107 }
714f095f 108 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
14d5e834
HH
109 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
110 &dest->addr.ip,
d8d1f30b 111 atomic_read(&rt->dst.__refcnt), rtos);
1da177e4 112 }
44e3125c 113 daddr = dest->addr.ip;
1da177e4
LT
114 spin_unlock(&dest->dst_lock);
115 } else {
78fbfd8a 116 rt = ip_route_output(net, daddr, 0, rtos, 0);
b23dd4fe 117 if (IS_ERR(rt)) {
14d5e834 118 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
fc604767 119 &daddr);
1da177e4
LT
120 return NULL;
121 }
122 }
123
fc604767 124 local = rt->rt_flags & RTCF_LOCAL;
17a8f8e3
CG
125 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
126 rt_mode)) {
fc604767
JA
127 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
128 (rt->rt_flags & RTCF_LOCAL) ?
44e3125c 129 "local":"non-local", &daddr);
fc604767
JA
130 ip_rt_put(rt);
131 return NULL;
132 }
17a8f8e3
CG
133 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
134 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
fc604767
JA
135 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
136 "requires NAT method, dest: %pI4\n",
44e3125c 137 &ip_hdr(skb)->daddr, &daddr);
fc604767
JA
138 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",
44e3125c 144 &ip_hdr(skb)->saddr, &daddr);
fc604767
JA
145 ip_rt_put(rt);
146 return NULL;
147 }
148
1da177e4
LT
149 return rt;
150}
151
fc604767
JA
152/* 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
c7537967 161 if (rt_is_input_route(rt)) {
fc604767
JA
162 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 {
9d6ec938
DM
169 struct flowi4 fl4 = {
170 .daddr = iph->daddr,
171 .saddr = iph->saddr,
172 .flowi4_tos = RT_TOS(iph->tos),
173 .flowi4_mark = skb->mark,
fc604767 174 };
fc604767 175
9d6ec938 176 rt = ip_route_output_key(net, &fl4);
b23dd4fe 177 if (IS_ERR(rt))
fc604767
JA
178 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
38cdcc9a 190#ifdef CONFIG_IP_VS_IPV6
714f095f 191
fc604767
JA
192static 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
714f095f
HS
197static 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)
200{
201 struct dst_entry *dst;
4c9483b2
DM
202 struct flowi6 fl6 = {
203 .daddr = *daddr,
714f095f
HS
204 };
205
4c9483b2 206 dst = ip6_route_output(net, NULL, &fl6);
714f095f
HS
207 if (dst->error)
208 goto out_err;
209 if (!ret_saddr)
210 return dst;
4c9483b2 211 if (ipv6_addr_any(&fl6.saddr) &&
714f095f 212 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
4c9483b2 213 &fl6.daddr, 0, &fl6.saddr) < 0)
714f095f 214 goto out_err;
452edd59 215 if (do_xfrm) {
4c9483b2 216 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
452edd59
DM
217 if (IS_ERR(dst)) {
218 dst = NULL;
219 goto out_err;
220 }
221 }
4c9483b2 222 ipv6_addr_copy(ret_saddr, &fl6.saddr);
714f095f
HS
223 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
fc604767
JA
231/*
232 * Get route to destination or remote server
fc604767 233 */
38cdcc9a 234static struct rt6_info *
fc604767
JA
235__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)
38cdcc9a 238{
fc604767 239 struct net *net = dev_net(skb_dst(skb)->dev);
38cdcc9a 240 struct rt6_info *rt; /* Route to the other host */
fc604767 241 struct rt6_info *ort; /* Original route */
714f095f 242 struct dst_entry *dst;
fc604767 243 int local;
38cdcc9a
JV
244
245 if (dest) {
246 spin_lock(&dest->dst_lock);
714f095f 247 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
38cdcc9a 248 if (!rt) {
714f095f 249 u32 cookie;
38cdcc9a 250
714f095f
HS
251 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
252 &dest->dst_saddr,
253 do_xfrm);
254 if (!dst) {
38cdcc9a 255 spin_unlock(&dest->dst_lock);
38cdcc9a
JV
256 return NULL;
257 }
714f095f
HS
258 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,
d8d1f30b 263 atomic_read(&rt->dst.__refcnt));
38cdcc9a 264 }
714f095f
HS
265 if (ret_saddr)
266 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
38cdcc9a
JV
267 spin_unlock(&dest->dst_lock);
268 } else {
fc604767 269 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
714f095f 270 if (!dst)
38cdcc9a 271 return NULL;
714f095f 272 rt = (struct rt6_info *) dst;
38cdcc9a
JV
273 }
274
fc604767 275 local = __ip_vs_is_local_route6(rt);
e58b3442
DM
276 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
277 rt_mode)) {
fc604767
JA
278 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 }
e58b3442 283 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
fc604767
JA
284 !((ort = (struct rt6_info *) skb_dst(skb)) &&
285 __ip_vs_is_local_route6(ort))) {
286 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
287 "requires NAT method, dest: %pI6\n",
288 &ipv6_hdr(skb)->daddr, daddr);
289 dst_release(&rt->dst);
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
38cdcc9a
JV
302 return rt;
303}
304#endif
305
1da177e4
LT
306
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
f4bc17cd
JA
320#define IP_VS_XMIT_TUNNEL(skb, cp) \
321({ \
322 int __ret = NF_ACCEPT; \
323 \
cf356d69 324 (skb)->ipvs_property = 1; \
f4bc17cd
JA
325 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); \
4256f1aa 329 skb_forward_csum(skb); \
f4bc17cd
JA
330 } \
331 __ret; \
332})
333
fc604767 334#define IP_VS_XMIT_NAT(pf, skb, cp, local) \
1da177e4 335do { \
cf356d69 336 (skb)->ipvs_property = 1; \
f4bc17cd 337 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
cf356d69 338 ip_vs_notrack(skb); \
f4bc17cd
JA
339 else \
340 ip_vs_update_conntrack(skb, cp, 1); \
fc604767
JA
341 if (local) \
342 return NF_ACCEPT; \
ccc7911f 343 skb_forward_csum(skb); \
38cdcc9a 344 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
f4bc17cd
JA
345 skb_dst(skb)->dev, dst_output); \
346} while (0)
347
fc604767 348#define IP_VS_XMIT(pf, skb, cp, local) \
f4bc17cd 349do { \
cf356d69 350 (skb)->ipvs_property = 1; \
f4bc17cd 351 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
cf356d69 352 ip_vs_notrack(skb); \
fc604767
JA
353 if (local) \
354 return NF_ACCEPT; \
f4bc17cd
JA
355 skb_forward_csum(skb); \
356 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
357 skb_dst(skb)->dev, dst_output); \
1da177e4
LT
358} 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 */
fc604767 369 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1da177e4
LT
370}
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 */
eddc9ec5 383 struct iphdr *iph = ip_hdr(skb);
1da177e4 384 int mtu;
1da177e4
LT
385
386 EnterFunction(10);
387
17a8f8e3
CG
388 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
389 IP_VS_RT_MODE_NON_LOCAL)))
1da177e4 390 goto tx_error_icmp;
1da177e4
LT
391
392 /* MTU checking */
d8d1f30b 393 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
394 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
395 !skb_is_gso(skb)) {
1da177e4
LT
396 ip_rt_put(rt);
397 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 398 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1da177e4
LT
399 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 }
eddc9ec5 410 ip_send_check(ip_hdr(skb));
1da177e4
LT
411
412 /* drop old route */
adf30907 413 skb_dst_drop(skb);
d8d1f30b 414 skb_dst_set(skb, &rt->dst);
1da177e4
LT
415
416 /* Another hack: avoid icmp_send in ip_fragment */
417 skb->local_df = 1;
418
fc604767 419 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1da177e4
LT
420
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
b3cdd2a7
JV
432#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;
b3cdd2a7
JV
440
441 EnterFunction(10);
442
e58b3442
DM
443 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0,
444 IP_VS_RT_MODE_NON_LOCAL)))
b3cdd2a7 445 goto tx_error_icmp;
b3cdd2a7
JV
446
447 /* MTU checking */
d8d1f30b 448 mtu = dst_mtu(&rt->dst);
8f1b03a4 449 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
450 if (!skb->dev) {
451 struct net *net = dev_net(skb_dst(skb)->dev);
452
453 skb->dev = net->loopback_dev;
454 }
3ffe533c 455 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
cb59155f 456 dst_release(&rt->dst);
1e3e238e 457 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
b3cdd2a7
JV
458 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)) {
d8d1f30b 467 dst_release(&rt->dst);
b3cdd2a7
JV
468 return NF_STOLEN;
469 }
470
471 /* drop old route */
adf30907 472 skb_dst_drop(skb);
d8d1f30b 473 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
474
475 /* Another hack: avoid icmp_send in ip_fragment */
476 skb->local_df = 1;
477
fc604767 478 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
b3cdd2a7
JV
479
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
1da177e4
LT
491
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;
eddc9ec5 502 struct iphdr *iph = ip_hdr(skb);
fc604767 503 int local;
1da177e4
LT
504
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)) {
014d730d 509 __be16 _pt, *p;
1da177e4
LT
510 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
fc604767 517 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
518 RT_TOS(iph->tos),
519 IP_VS_RT_MODE_LOCAL |
520 IP_VS_RT_MODE_NON_LOCAL |
521 IP_VS_RT_MODE_RDR)))
1da177e4 522 goto tx_error_icmp;
fc604767
JA
523 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)) {
0d79641a
JA
534 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
535 "ip_vs_nat_xmit(): "
fc604767
JA
536 "stopping DNAT to local address");
537 goto tx_error_put;
538 }
539 }
540#endif
541
542 /* From world but DNAT to loopback address? */
c7537967
DM
543 if (local && ipv4_is_loopback(rt->rt_dst) &&
544 rt_is_input_route(skb_rtable(skb))) {
0d79641a 545 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
fc604767
JA
546 "stopping DNAT to loopback address");
547 goto tx_error_put;
548 }
1da177e4
LT
549
550 /* MTU checking */
d8d1f30b 551 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
552 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
553 !skb_is_gso(skb)) {
1da177e4 554 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
0d79641a
JA
555 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
556 "ip_vs_nat_xmit(): frag needed for");
fc604767 557 goto tx_error_put;
1da177e4
LT
558 }
559
560 /* copy-on-write the packet before mangling it */
af1e1cf0 561 if (!skb_make_writable(skb, sizeof(struct iphdr)))
1da177e4
LT
562 goto tx_error_put;
563
d8d1f30b 564 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1da177e4
LT
565 goto tx_error_put;
566
1da177e4 567 /* mangle the packet */
3db05fea 568 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
fc604767 569 goto tx_error_put;
e7ade46a 570 ip_hdr(skb)->daddr = cp->daddr.ip;
eddc9ec5 571 ip_send_check(ip_hdr(skb));
1da177e4 572
fc604767
JA
573 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
0d79641a 588 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
1da177e4
LT
589
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
fc604767 597 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1da177e4
LT
598
599 LeaveFunction(10);
600 return NF_STOLEN;
601
602 tx_error_icmp:
603 dst_link_failure(skb);
604 tx_error:
1da177e4 605 kfree_skb(skb);
f4bc17cd 606 LeaveFunction(10);
1da177e4
LT
607 return NF_STOLEN;
608 tx_error_put:
609 ip_rt_put(rt);
610 goto tx_error;
611}
612
b3cdd2a7
JV
613#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;
fc604767 620 int local;
b3cdd2a7
JV
621
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
fc604767 635 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
636 0, (IP_VS_RT_MODE_LOCAL |
637 IP_VS_RT_MODE_NON_LOCAL |
638 IP_VS_RT_MODE_RDR))))
b3cdd2a7 639 goto tx_error_icmp;
fc604767
JA
640 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)) {
0d79641a 651 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
fc604767
JA
652 "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) {
0d79641a 662 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
fc604767
JA
663 "ip_vs_nat_xmit_v6(): "
664 "stopping DNAT to loopback address");
665 goto tx_error_put;
666 }
b3cdd2a7
JV
667
668 /* MTU checking */
d8d1f30b 669 mtu = dst_mtu(&rt->dst);
8f1b03a4 670 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
671 if (!skb->dev) {
672 struct net *net = dev_net(skb_dst(skb)->dev);
673
674 skb->dev = net->loopback_dev;
675 }
3ffe533c 676 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
0d79641a 677 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
b3cdd2a7 678 "ip_vs_nat_xmit_v6(): frag needed for");
fc604767 679 goto tx_error_put;
b3cdd2a7
JV
680 }
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
d8d1f30b 686 if (skb_cow(skb, rt->dst.dev->hard_header_len))
b3cdd2a7
JV
687 goto tx_error_put;
688
b3cdd2a7
JV
689 /* mangle the packet */
690 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
691 goto tx_error;
fc604767
JA
692 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 }
b3cdd2a7 702
0d79641a 703 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
b3cdd2a7
JV
704
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
fc604767 712 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
b3cdd2a7
JV
713
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:
d8d1f30b 724 dst_release(&rt->dst);
b3cdd2a7
JV
725 goto tx_error;
726}
727#endif
728
1da177e4
LT
729
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 */
eddc9ec5 755 struct iphdr *old_iph = ip_hdr(skb);
1da177e4 756 u8 tos = old_iph->tos;
76ab608d 757 __be16 df = old_iph->frag_off;
1da177e4 758 struct iphdr *iph; /* Our new IP header */
c2636b4d 759 unsigned int max_headroom; /* The extra header space needed */
1da177e4 760 int mtu;
f4bc17cd 761 int ret;
1da177e4
LT
762
763 EnterFunction(10);
764
fc604767 765 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
766 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
767 IP_VS_RT_MODE_NON_LOCAL)))
1da177e4 768 goto tx_error_icmp;
fc604767
JA
769 if (rt->rt_flags & RTCF_LOCAL) {
770 ip_rt_put(rt);
771 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
772 }
1da177e4 773
d8d1f30b 774 tdev = rt->dst.dev;
1da177e4 775
d8d1f30b 776 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
1da177e4 777 if (mtu < 68) {
1e3e238e 778 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
fc604767 779 goto tx_error_put;
1da177e4 780 }
adf30907
ED
781 if (skb_dst(skb))
782 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
1da177e4 783
4412ec49 784 df |= (old_iph->frag_off & htons(IP_DF));
1da177e4 785
8f1b03a4
SH
786 if ((old_iph->frag_off & htons(IP_DF) &&
787 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
1da177e4 788 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 789 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 790 goto tx_error_put;
1da177e4
LT
791 }
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);
1e3e238e 805 IP_VS_ERR_RL("%s(): no memory\n", __func__);
1da177e4
LT
806 return NF_STOLEN;
807 }
808 kfree_skb(skb);
809 skb = new_skb;
eddc9ec5 810 old_iph = ip_hdr(skb);
1da177e4
LT
811 }
812
714f095f 813 skb->transport_header = skb->network_header;
1da177e4
LT
814
815 /* fix old IP header checksum */
816 ip_send_check(old_iph);
817
e2d1bca7
ACM
818 skb_push(skb, sizeof(struct iphdr));
819 skb_reset_network_header(skb);
1da177e4
LT
820 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
821
822 /* drop old route */
adf30907 823 skb_dst_drop(skb);
d8d1f30b 824 skb_dst_set(skb, &rt->dst);
1da177e4
LT
825
826 /*
827 * Push down and install the IPIP header.
828 */
eddc9ec5 829 iph = ip_hdr(skb);
1da177e4
LT
830 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;
d8d1f30b 838 ip_select_ident(iph, &rt->dst, NULL);
1da177e4
LT
839
840 /* Another hack: avoid icmp_send in ip_fragment */
841 skb->local_df = 1;
842
f4bc17cd
JA
843 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);
1da177e4
LT
848
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;
fc604767
JA
859tx_error_put:
860 ip_rt_put(rt);
861 goto tx_error;
1da177e4
LT
862}
863
b3cdd2a7
JV
864#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 */
714f095f 870 struct in6_addr saddr; /* Source for tunnel */
b3cdd2a7
JV
871 struct net_device *tdev; /* Device to other host */
872 struct ipv6hdr *old_iph = ipv6_hdr(skb);
b3cdd2a7
JV
873 struct ipv6hdr *iph; /* Our new IP header */
874 unsigned int max_headroom; /* The extra header space needed */
875 int mtu;
f4bc17cd 876 int ret;
b3cdd2a7
JV
877
878 EnterFunction(10);
879
fc604767 880 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
e58b3442
DM
881 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
882 IP_VS_RT_MODE_NON_LOCAL))))
b3cdd2a7 883 goto tx_error_icmp;
fc604767
JA
884 if (__ip_vs_is_local_route6(rt)) {
885 dst_release(&rt->dst);
886 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
887 }
b3cdd2a7 888
d8d1f30b 889 tdev = rt->dst.dev;
b3cdd2a7 890
d8d1f30b 891 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
714f095f 892 if (mtu < IPV6_MIN_MTU) {
714f095f
HS
893 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
894 IPV6_MIN_MTU);
fc604767 895 goto tx_error_put;
b3cdd2a7 896 }
adf30907
ED
897 if (skb_dst(skb))
898 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
b3cdd2a7 899
8f1b03a4
SH
900 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
901 !skb_is_gso(skb)) {
cb59155f
JA
902 if (!skb->dev) {
903 struct net *net = dev_net(skb_dst(skb)->dev);
904
905 skb->dev = net->loopback_dev;
906 }
3ffe533c 907 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1e3e238e 908 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 909 goto tx_error_put;
b3cdd2a7
JV
910 }
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) {
d8d1f30b 922 dst_release(&rt->dst);
b3cdd2a7 923 kfree_skb(skb);
1e3e238e 924 IP_VS_ERR_RL("%s(): no memory\n", __func__);
b3cdd2a7
JV
925 return NF_STOLEN;
926 }
927 kfree_skb(skb);
928 skb = new_skb;
929 old_iph = ipv6_hdr(skb);
930 }
931
714f095f 932 skb->transport_header = skb->network_header;
b3cdd2a7
JV
933
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 */
adf30907 939 skb_dst_drop(skb);
d8d1f30b 940 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
941
942 /*
943 * Push down and install the IPIP header.
944 */
945 iph = ipv6_hdr(skb);
946 iph->version = 6;
947 iph->nexthdr = IPPROTO_IPV6;
b7b45f47
HH
948 iph->payload_len = old_iph->payload_len;
949 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
b3cdd2a7
JV
950 iph->priority = old_iph->priority;
951 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
714f095f
HS
952 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
953 ipv6_addr_copy(&iph->saddr, &saddr);
b3cdd2a7
JV
954 iph->hop_limit = old_iph->hop_limit;
955
956 /* Another hack: avoid icmp_send in ip_fragment */
957 skb->local_df = 1;
958
f4bc17cd
JA
959 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);
b3cdd2a7
JV
964
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;
fc604767
JA
975tx_error_put:
976 dst_release(&rt->dst);
977 goto tx_error;
b3cdd2a7
JV
978}
979#endif
980
1da177e4
LT
981
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 */
eddc9ec5 991 struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
992 int mtu;
993
994 EnterFunction(10);
995
fc604767 996 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
997 RT_TOS(iph->tos),
998 IP_VS_RT_MODE_LOCAL |
999 IP_VS_RT_MODE_NON_LOCAL)))
1da177e4 1000 goto tx_error_icmp;
fc604767
JA
1001 if (rt->rt_flags & RTCF_LOCAL) {
1002 ip_rt_put(rt);
1003 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1004 }
1da177e4
LT
1005
1006 /* MTU checking */
d8d1f30b 1007 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
1008 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1009 !skb_is_gso(skb)) {
1da177e4
LT
1010 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1011 ip_rt_put(rt);
1e3e238e 1012 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1da177e4
LT
1013 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 }
eddc9ec5 1024 ip_send_check(ip_hdr(skb));
1da177e4
LT
1025
1026 /* drop old route */
adf30907 1027 skb_dst_drop(skb);
d8d1f30b 1028 skb_dst_set(skb, &rt->dst);
1da177e4
LT
1029
1030 /* Another hack: avoid icmp_send in ip_fragment */
1031 skb->local_df = 1;
1032
fc604767 1033 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1da177e4
LT
1034
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
b3cdd2a7
JV
1046#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
fc604767 1056 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
1057 0, (IP_VS_RT_MODE_LOCAL |
1058 IP_VS_RT_MODE_NON_LOCAL))))
b3cdd2a7 1059 goto tx_error_icmp;
fc604767
JA
1060 if (__ip_vs_is_local_route6(rt)) {
1061 dst_release(&rt->dst);
1062 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1063 }
b3cdd2a7
JV
1064
1065 /* MTU checking */
d8d1f30b 1066 mtu = dst_mtu(&rt->dst);
b3cdd2a7 1067 if (skb->len > mtu) {
cb59155f
JA
1068 if (!skb->dev) {
1069 struct net *net = dev_net(skb_dst(skb)->dev);
1070
1071 skb->dev = net->loopback_dev;
1072 }
3ffe533c 1073 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
d8d1f30b 1074 dst_release(&rt->dst);
1e3e238e 1075 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
b3cdd2a7
JV
1076 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)) {
d8d1f30b 1085 dst_release(&rt->dst);
b3cdd2a7
JV
1086 return NF_STOLEN;
1087 }
1088
1089 /* drop old route */
adf30907 1090 skb_dst_drop(skb);
d8d1f30b 1091 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
1092
1093 /* Another hack: avoid icmp_send in ip_fragment */
1094 skb->local_df = 1;
1095
fc604767 1096 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
b3cdd2a7
JV
1097
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
1da177e4
LT
1110
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;
fc604767 1122 int local;
1da177e4
LT
1123
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);
1da177e4
LT
1136 goto out;
1137 }
1138
1139 /*
1140 * mangle and send the packet here (only for VS/NAT)
1141 */
1142
fc604767 1143 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
1144 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)))
1da177e4 1148 goto tx_error_icmp;
fc604767
JA
1149 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? */
c7537967
DM
1170 if (local && ipv4_is_loopback(rt->rt_dst) &&
1171 rt_is_input_route(skb_rtable(skb))) {
fc604767
JA
1172 IP_VS_DBG(1, "%s(): "
1173 "stopping DNAT to loopback %pI4\n",
1174 __func__, &cp->daddr.ip);
1175 goto tx_error_put;
1176 }
1da177e4
LT
1177
1178 /* MTU checking */
d8d1f30b 1179 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
1180 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1181 !skb_is_gso(skb)) {
1da177e4 1182 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 1183 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 1184 goto tx_error_put;
1da177e4
LT
1185 }
1186
1187 /* copy-on-write the packet before mangling it */
af1e1cf0 1188 if (!skb_make_writable(skb, offset))
1da177e4
LT
1189 goto tx_error_put;
1190
d8d1f30b 1191 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1da177e4
LT
1192 goto tx_error_put;
1193
1da177e4
LT
1194 ip_vs_nat_icmp(skb, pp, cp, 0);
1195
fc604767
JA
1196 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
1da177e4
LT
1211 /* Another hack: avoid icmp_send in ip_fragment */
1212 skb->local_df = 1;
1213
fc604767 1214 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1da177e4
LT
1215
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}
b3cdd2a7
JV
1231
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;
fc604767 1240 int local;
b3cdd2a7
JV
1241
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
fc604767 1261 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
1262 0, (IP_VS_RT_MODE_LOCAL |
1263 IP_VS_RT_MODE_NON_LOCAL |
1264 IP_VS_RT_MODE_RDR))))
b3cdd2a7
JV
1265 goto tx_error_icmp;
1266
fc604767
JA
1267 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
b3cdd2a7 1295 /* MTU checking */
d8d1f30b 1296 mtu = dst_mtu(&rt->dst);
8f1b03a4 1297 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
1298 if (!skb->dev) {
1299 struct net *net = dev_net(skb_dst(skb)->dev);
1300
1301 skb->dev = net->loopback_dev;
1302 }
3ffe533c 1303 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1e3e238e 1304 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 1305 goto tx_error_put;
b3cdd2a7
JV
1306 }
1307
1308 /* copy-on-write the packet before mangling it */
1309 if (!skb_make_writable(skb, offset))
1310 goto tx_error_put;
1311
d8d1f30b 1312 if (skb_cow(skb, rt->dst.dev->hard_header_len))
b3cdd2a7
JV
1313 goto tx_error_put;
1314
b3cdd2a7
JV
1315 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1316
fc604767
JA
1317 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
b3cdd2a7
JV
1326 /* Another hack: avoid icmp_send in ip_fragment */
1327 skb->local_df = 1;
1328
fc604767 1329 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
b3cdd2a7
JV
1330
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:
d8d1f30b 1343 dst_release(&rt->dst);
b3cdd2a7
JV
1344 goto tx_error;
1345}
1346#endif
This page took 0.670633 seconds and 5 git commands to generate.