ipvs: Use IP_VS_RT_MODE_* instead of magic constants.
[GitHub/LineageOS/android_kernel_samsung_universal7580.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
LT
112 }
113 spin_unlock(&dest->dst_lock);
114 } else {
78fbfd8a 115 rt = ip_route_output(net, daddr, 0, rtos, 0);
b23dd4fe 116 if (IS_ERR(rt)) {
14d5e834 117 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
fc604767 118 &daddr);
1da177e4
LT
119 return NULL;
120 }
121 }
122
fc604767 123 local = rt->rt_flags & RTCF_LOCAL;
17a8f8e3
CG
124 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
125 rt_mode)) {
fc604767
JA
126 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
127 (rt->rt_flags & RTCF_LOCAL) ?
128 "local":"non-local", &rt->rt_dst);
129 ip_rt_put(rt);
130 return NULL;
131 }
17a8f8e3
CG
132 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
133 !((ort = skb_rtable(skb)) && ort->rt_flags & RTCF_LOCAL)) {
fc604767
JA
134 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
135 "requires NAT method, dest: %pI4\n",
136 &ip_hdr(skb)->daddr, &rt->rt_dst);
137 ip_rt_put(rt);
138 return NULL;
139 }
140 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
141 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
142 "to non-local address, dest: %pI4\n",
143 &ip_hdr(skb)->saddr, &rt->rt_dst);
144 ip_rt_put(rt);
145 return NULL;
146 }
147
1da177e4
LT
148 return rt;
149}
150
fc604767
JA
151/* Reroute packet to local IPv4 stack after DNAT */
152static int
153__ip_vs_reroute_locally(struct sk_buff *skb)
154{
155 struct rtable *rt = skb_rtable(skb);
156 struct net_device *dev = rt->dst.dev;
157 struct net *net = dev_net(dev);
158 struct iphdr *iph = ip_hdr(skb);
159
c7537967 160 if (rt_is_input_route(rt)) {
fc604767
JA
161 unsigned long orefdst = skb->_skb_refdst;
162
163 if (ip_route_input(skb, iph->daddr, iph->saddr,
164 iph->tos, skb->dev))
165 return 0;
166 refdst_drop(orefdst);
167 } else {
9d6ec938
DM
168 struct flowi4 fl4 = {
169 .daddr = iph->daddr,
170 .saddr = iph->saddr,
171 .flowi4_tos = RT_TOS(iph->tos),
172 .flowi4_mark = skb->mark,
fc604767 173 };
fc604767 174
9d6ec938 175 rt = ip_route_output_key(net, &fl4);
b23dd4fe 176 if (IS_ERR(rt))
fc604767
JA
177 return 0;
178 if (!(rt->rt_flags & RTCF_LOCAL)) {
179 ip_rt_put(rt);
180 return 0;
181 }
182 /* Drop old route. */
183 skb_dst_drop(skb);
184 skb_dst_set(skb, &rt->dst);
185 }
186 return 1;
187}
188
38cdcc9a 189#ifdef CONFIG_IP_VS_IPV6
714f095f 190
fc604767
JA
191static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
192{
193 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
194}
195
714f095f
HS
196static struct dst_entry *
197__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
198 struct in6_addr *ret_saddr, int do_xfrm)
199{
200 struct dst_entry *dst;
4c9483b2
DM
201 struct flowi6 fl6 = {
202 .daddr = *daddr,
714f095f
HS
203 };
204
4c9483b2 205 dst = ip6_route_output(net, NULL, &fl6);
714f095f
HS
206 if (dst->error)
207 goto out_err;
208 if (!ret_saddr)
209 return dst;
4c9483b2 210 if (ipv6_addr_any(&fl6.saddr) &&
714f095f 211 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
4c9483b2 212 &fl6.daddr, 0, &fl6.saddr) < 0)
714f095f 213 goto out_err;
452edd59 214 if (do_xfrm) {
4c9483b2 215 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
452edd59
DM
216 if (IS_ERR(dst)) {
217 dst = NULL;
218 goto out_err;
219 }
220 }
4c9483b2 221 ipv6_addr_copy(ret_saddr, &fl6.saddr);
714f095f
HS
222 return dst;
223
224out_err:
225 dst_release(dst);
226 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
227 return NULL;
228}
229
fc604767
JA
230/*
231 * Get route to destination or remote server
fc604767 232 */
38cdcc9a 233static struct rt6_info *
fc604767
JA
234__ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
235 struct in6_addr *daddr, struct in6_addr *ret_saddr,
236 int do_xfrm, int rt_mode)
38cdcc9a 237{
fc604767 238 struct net *net = dev_net(skb_dst(skb)->dev);
38cdcc9a 239 struct rt6_info *rt; /* Route to the other host */
fc604767 240 struct rt6_info *ort; /* Original route */
714f095f 241 struct dst_entry *dst;
fc604767 242 int local;
38cdcc9a
JV
243
244 if (dest) {
245 spin_lock(&dest->dst_lock);
714f095f 246 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
38cdcc9a 247 if (!rt) {
714f095f 248 u32 cookie;
38cdcc9a 249
714f095f
HS
250 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
251 &dest->dst_saddr,
252 do_xfrm);
253 if (!dst) {
38cdcc9a 254 spin_unlock(&dest->dst_lock);
38cdcc9a
JV
255 return NULL;
256 }
714f095f
HS
257 rt = (struct rt6_info *) dst;
258 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
259 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
260 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
261 &dest->addr.in6, &dest->dst_saddr,
d8d1f30b 262 atomic_read(&rt->dst.__refcnt));
38cdcc9a 263 }
714f095f
HS
264 if (ret_saddr)
265 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
38cdcc9a
JV
266 spin_unlock(&dest->dst_lock);
267 } else {
fc604767 268 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
714f095f 269 if (!dst)
38cdcc9a 270 return NULL;
714f095f 271 rt = (struct rt6_info *) dst;
38cdcc9a
JV
272 }
273
fc604767 274 local = __ip_vs_is_local_route6(rt);
e58b3442
DM
275 if (!((local ? IP_VS_RT_MODE_LOCAL : IP_VS_RT_MODE_NON_LOCAL) &
276 rt_mode)) {
fc604767
JA
277 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
278 local ? "local":"non-local", daddr);
279 dst_release(&rt->dst);
280 return NULL;
281 }
e58b3442 282 if (local && !(rt_mode & IP_VS_RT_MODE_RDR) &&
fc604767
JA
283 !((ort = (struct rt6_info *) skb_dst(skb)) &&
284 __ip_vs_is_local_route6(ort))) {
285 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
286 "requires NAT method, dest: %pI6\n",
287 &ipv6_hdr(skb)->daddr, daddr);
288 dst_release(&rt->dst);
289 return NULL;
290 }
291 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
292 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
293 IPV6_ADDR_LOOPBACK)) {
294 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
295 "to non-local address, dest: %pI6\n",
296 &ipv6_hdr(skb)->saddr, daddr);
297 dst_release(&rt->dst);
298 return NULL;
299 }
300
38cdcc9a
JV
301 return rt;
302}
303#endif
304
1da177e4
LT
305
306/*
307 * Release dest->dst_cache before a dest is removed
308 */
309void
310ip_vs_dst_reset(struct ip_vs_dest *dest)
311{
312 struct dst_entry *old_dst;
313
314 old_dst = dest->dst_cache;
315 dest->dst_cache = NULL;
316 dst_release(old_dst);
317}
318
f4bc17cd
JA
319#define IP_VS_XMIT_TUNNEL(skb, cp) \
320({ \
321 int __ret = NF_ACCEPT; \
322 \
cf356d69 323 (skb)->ipvs_property = 1; \
f4bc17cd
JA
324 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
325 __ret = ip_vs_confirm_conntrack(skb, cp); \
326 if (__ret == NF_ACCEPT) { \
327 nf_reset(skb); \
4256f1aa 328 skb_forward_csum(skb); \
f4bc17cd
JA
329 } \
330 __ret; \
331})
332
fc604767 333#define IP_VS_XMIT_NAT(pf, skb, cp, local) \
1da177e4 334do { \
cf356d69 335 (skb)->ipvs_property = 1; \
f4bc17cd 336 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
cf356d69 337 ip_vs_notrack(skb); \
f4bc17cd
JA
338 else \
339 ip_vs_update_conntrack(skb, cp, 1); \
fc604767
JA
340 if (local) \
341 return NF_ACCEPT; \
ccc7911f 342 skb_forward_csum(skb); \
38cdcc9a 343 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
f4bc17cd
JA
344 skb_dst(skb)->dev, dst_output); \
345} while (0)
346
fc604767 347#define IP_VS_XMIT(pf, skb, cp, local) \
f4bc17cd 348do { \
cf356d69 349 (skb)->ipvs_property = 1; \
f4bc17cd 350 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
cf356d69 351 ip_vs_notrack(skb); \
fc604767
JA
352 if (local) \
353 return NF_ACCEPT; \
f4bc17cd
JA
354 skb_forward_csum(skb); \
355 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
356 skb_dst(skb)->dev, dst_output); \
1da177e4
LT
357} while (0)
358
359
360/*
361 * NULL transmitter (do nothing except return NF_ACCEPT)
362 */
363int
364ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
365 struct ip_vs_protocol *pp)
366{
367 /* we do not touch skb and do not need pskb ptr */
fc604767 368 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1da177e4
LT
369}
370
371
372/*
373 * Bypass transmitter
374 * Let packets bypass the destination when the destination is not
375 * available, it may be only used in transparent cache cluster.
376 */
377int
378ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
379 struct ip_vs_protocol *pp)
380{
381 struct rtable *rt; /* Route to the other host */
eddc9ec5 382 struct iphdr *iph = ip_hdr(skb);
1da177e4 383 int mtu;
1da177e4
LT
384
385 EnterFunction(10);
386
17a8f8e3
CG
387 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr, RT_TOS(iph->tos),
388 IP_VS_RT_MODE_NON_LOCAL)))
1da177e4 389 goto tx_error_icmp;
1da177e4
LT
390
391 /* MTU checking */
d8d1f30b 392 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
393 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
394 !skb_is_gso(skb)) {
1da177e4
LT
395 ip_rt_put(rt);
396 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 397 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1da177e4
LT
398 goto tx_error;
399 }
400
401 /*
402 * Call ip_send_check because we are not sure it is called
403 * after ip_defrag. Is copy-on-write needed?
404 */
405 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
406 ip_rt_put(rt);
407 return NF_STOLEN;
408 }
eddc9ec5 409 ip_send_check(ip_hdr(skb));
1da177e4
LT
410
411 /* drop old route */
adf30907 412 skb_dst_drop(skb);
d8d1f30b 413 skb_dst_set(skb, &rt->dst);
1da177e4
LT
414
415 /* Another hack: avoid icmp_send in ip_fragment */
416 skb->local_df = 1;
417
fc604767 418 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1da177e4
LT
419
420 LeaveFunction(10);
421 return NF_STOLEN;
422
423 tx_error_icmp:
424 dst_link_failure(skb);
425 tx_error:
426 kfree_skb(skb);
427 LeaveFunction(10);
428 return NF_STOLEN;
429}
430
b3cdd2a7
JV
431#ifdef CONFIG_IP_VS_IPV6
432int
433ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
434 struct ip_vs_protocol *pp)
435{
436 struct rt6_info *rt; /* Route to the other host */
437 struct ipv6hdr *iph = ipv6_hdr(skb);
438 int mtu;
b3cdd2a7
JV
439
440 EnterFunction(10);
441
e58b3442
DM
442 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0,
443 IP_VS_RT_MODE_NON_LOCAL)))
b3cdd2a7 444 goto tx_error_icmp;
b3cdd2a7
JV
445
446 /* MTU checking */
d8d1f30b 447 mtu = dst_mtu(&rt->dst);
8f1b03a4 448 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
449 if (!skb->dev) {
450 struct net *net = dev_net(skb_dst(skb)->dev);
451
452 skb->dev = net->loopback_dev;
453 }
3ffe533c 454 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
cb59155f 455 dst_release(&rt->dst);
1e3e238e 456 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
b3cdd2a7
JV
457 goto tx_error;
458 }
459
460 /*
461 * Call ip_send_check because we are not sure it is called
462 * after ip_defrag. Is copy-on-write needed?
463 */
464 skb = skb_share_check(skb, GFP_ATOMIC);
465 if (unlikely(skb == NULL)) {
d8d1f30b 466 dst_release(&rt->dst);
b3cdd2a7
JV
467 return NF_STOLEN;
468 }
469
470 /* drop old route */
adf30907 471 skb_dst_drop(skb);
d8d1f30b 472 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
473
474 /* Another hack: avoid icmp_send in ip_fragment */
475 skb->local_df = 1;
476
fc604767 477 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
b3cdd2a7
JV
478
479 LeaveFunction(10);
480 return NF_STOLEN;
481
482 tx_error_icmp:
483 dst_link_failure(skb);
484 tx_error:
485 kfree_skb(skb);
486 LeaveFunction(10);
487 return NF_STOLEN;
488}
489#endif
1da177e4
LT
490
491/*
492 * NAT transmitter (only for outside-to-inside nat forwarding)
493 * Not used for related ICMP
494 */
495int
496ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
497 struct ip_vs_protocol *pp)
498{
499 struct rtable *rt; /* Route to the other host */
500 int mtu;
eddc9ec5 501 struct iphdr *iph = ip_hdr(skb);
fc604767 502 int local;
1da177e4
LT
503
504 EnterFunction(10);
505
506 /* check if it is a connection of no-client-port */
507 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
014d730d 508 __be16 _pt, *p;
1da177e4
LT
509 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
510 if (p == NULL)
511 goto tx_error;
512 ip_vs_conn_fill_cport(cp, *p);
513 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
514 }
515
fc604767 516 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
517 RT_TOS(iph->tos),
518 IP_VS_RT_MODE_LOCAL |
519 IP_VS_RT_MODE_NON_LOCAL |
520 IP_VS_RT_MODE_RDR)))
1da177e4 521 goto tx_error_icmp;
fc604767
JA
522 local = rt->rt_flags & RTCF_LOCAL;
523 /*
524 * Avoid duplicate tuple in reply direction for NAT traffic
525 * to local address when connection is sync-ed
526 */
527#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
528 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
529 enum ip_conntrack_info ctinfo;
530 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
531
532 if (ct && !nf_ct_is_untracked(ct)) {
0d79641a
JA
533 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
534 "ip_vs_nat_xmit(): "
fc604767
JA
535 "stopping DNAT to local address");
536 goto tx_error_put;
537 }
538 }
539#endif
540
541 /* From world but DNAT to loopback address? */
c7537967
DM
542 if (local && ipv4_is_loopback(rt->rt_dst) &&
543 rt_is_input_route(skb_rtable(skb))) {
0d79641a 544 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
fc604767
JA
545 "stopping DNAT to loopback address");
546 goto tx_error_put;
547 }
1da177e4
LT
548
549 /* MTU checking */
d8d1f30b 550 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
551 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
552 !skb_is_gso(skb)) {
1da177e4 553 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
0d79641a
JA
554 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
555 "ip_vs_nat_xmit(): frag needed for");
fc604767 556 goto tx_error_put;
1da177e4
LT
557 }
558
559 /* copy-on-write the packet before mangling it */
af1e1cf0 560 if (!skb_make_writable(skb, sizeof(struct iphdr)))
1da177e4
LT
561 goto tx_error_put;
562
d8d1f30b 563 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1da177e4
LT
564 goto tx_error_put;
565
1da177e4 566 /* mangle the packet */
3db05fea 567 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
fc604767 568 goto tx_error_put;
e7ade46a 569 ip_hdr(skb)->daddr = cp->daddr.ip;
eddc9ec5 570 ip_send_check(ip_hdr(skb));
1da177e4 571
fc604767
JA
572 if (!local) {
573 /* drop old route */
574 skb_dst_drop(skb);
575 skb_dst_set(skb, &rt->dst);
576 } else {
577 ip_rt_put(rt);
578 /*
579 * Some IPv4 replies get local address from routes,
580 * not from iph, so while we DNAT after routing
581 * we need this second input/output route.
582 */
583 if (!__ip_vs_reroute_locally(skb))
584 goto tx_error;
585 }
586
0d79641a 587 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
1da177e4
LT
588
589 /* FIXME: when application helper enlarges the packet and the length
590 is larger than the MTU of outgoing device, there will be still
591 MTU problem. */
592
593 /* Another hack: avoid icmp_send in ip_fragment */
594 skb->local_df = 1;
595
fc604767 596 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1da177e4
LT
597
598 LeaveFunction(10);
599 return NF_STOLEN;
600
601 tx_error_icmp:
602 dst_link_failure(skb);
603 tx_error:
1da177e4 604 kfree_skb(skb);
f4bc17cd 605 LeaveFunction(10);
1da177e4
LT
606 return NF_STOLEN;
607 tx_error_put:
608 ip_rt_put(rt);
609 goto tx_error;
610}
611
b3cdd2a7
JV
612#ifdef CONFIG_IP_VS_IPV6
613int
614ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
615 struct ip_vs_protocol *pp)
616{
617 struct rt6_info *rt; /* Route to the other host */
618 int mtu;
fc604767 619 int local;
b3cdd2a7
JV
620
621 EnterFunction(10);
622
623 /* check if it is a connection of no-client-port */
624 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
625 __be16 _pt, *p;
626 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
627 sizeof(_pt), &_pt);
628 if (p == NULL)
629 goto tx_error;
630 ip_vs_conn_fill_cport(cp, *p);
631 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
632 }
633
fc604767 634 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
635 0, (IP_VS_RT_MODE_LOCAL |
636 IP_VS_RT_MODE_NON_LOCAL |
637 IP_VS_RT_MODE_RDR))))
b3cdd2a7 638 goto tx_error_icmp;
fc604767
JA
639 local = __ip_vs_is_local_route6(rt);
640 /*
641 * Avoid duplicate tuple in reply direction for NAT traffic
642 * to local address when connection is sync-ed
643 */
644#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
645 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
646 enum ip_conntrack_info ctinfo;
647 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
648
649 if (ct && !nf_ct_is_untracked(ct)) {
0d79641a 650 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
fc604767
JA
651 "ip_vs_nat_xmit_v6(): "
652 "stopping DNAT to local address");
653 goto tx_error_put;
654 }
655 }
656#endif
657
658 /* From world but DNAT to loopback address? */
659 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
660 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
0d79641a 661 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
fc604767
JA
662 "ip_vs_nat_xmit_v6(): "
663 "stopping DNAT to loopback address");
664 goto tx_error_put;
665 }
b3cdd2a7
JV
666
667 /* MTU checking */
d8d1f30b 668 mtu = dst_mtu(&rt->dst);
8f1b03a4 669 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
670 if (!skb->dev) {
671 struct net *net = dev_net(skb_dst(skb)->dev);
672
673 skb->dev = net->loopback_dev;
674 }
3ffe533c 675 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
0d79641a 676 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
b3cdd2a7 677 "ip_vs_nat_xmit_v6(): frag needed for");
fc604767 678 goto tx_error_put;
b3cdd2a7
JV
679 }
680
681 /* copy-on-write the packet before mangling it */
682 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
683 goto tx_error_put;
684
d8d1f30b 685 if (skb_cow(skb, rt->dst.dev->hard_header_len))
b3cdd2a7
JV
686 goto tx_error_put;
687
b3cdd2a7
JV
688 /* mangle the packet */
689 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
690 goto tx_error;
fc604767
JA
691 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
692
693 if (!local || !skb->dev) {
694 /* drop the old route when skb is not shared */
695 skb_dst_drop(skb);
696 skb_dst_set(skb, &rt->dst);
697 } else {
698 /* destined to loopback, do we need to change route? */
699 dst_release(&rt->dst);
700 }
b3cdd2a7 701
0d79641a 702 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
b3cdd2a7
JV
703
704 /* FIXME: when application helper enlarges the packet and the length
705 is larger than the MTU of outgoing device, there will be still
706 MTU problem. */
707
708 /* Another hack: avoid icmp_send in ip_fragment */
709 skb->local_df = 1;
710
fc604767 711 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
b3cdd2a7
JV
712
713 LeaveFunction(10);
714 return NF_STOLEN;
715
716tx_error_icmp:
717 dst_link_failure(skb);
718tx_error:
719 LeaveFunction(10);
720 kfree_skb(skb);
721 return NF_STOLEN;
722tx_error_put:
d8d1f30b 723 dst_release(&rt->dst);
b3cdd2a7
JV
724 goto tx_error;
725}
726#endif
727
1da177e4
LT
728
729/*
730 * IP Tunneling transmitter
731 *
732 * This function encapsulates the packet in a new IP packet, its
733 * destination will be set to cp->daddr. Most code of this function
734 * is taken from ipip.c.
735 *
736 * It is used in VS/TUN cluster. The load balancer selects a real
737 * server from a cluster based on a scheduling algorithm,
738 * encapsulates the request packet and forwards it to the selected
739 * server. For example, all real servers are configured with
740 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
741 * the encapsulated packet, it will decapsulate the packet, processe
742 * the request and return the response packets directly to the client
743 * without passing the load balancer. This can greatly increase the
744 * scalability of virtual server.
745 *
746 * Used for ANY protocol
747 */
748int
749ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
750 struct ip_vs_protocol *pp)
751{
752 struct rtable *rt; /* Route to the other host */
753 struct net_device *tdev; /* Device to other host */
eddc9ec5 754 struct iphdr *old_iph = ip_hdr(skb);
1da177e4 755 u8 tos = old_iph->tos;
76ab608d 756 __be16 df = old_iph->frag_off;
1da177e4 757 struct iphdr *iph; /* Our new IP header */
c2636b4d 758 unsigned int max_headroom; /* The extra header space needed */
1da177e4 759 int mtu;
f4bc17cd 760 int ret;
1da177e4
LT
761
762 EnterFunction(10);
763
fc604767 764 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
765 RT_TOS(tos), IP_VS_RT_MODE_LOCAL |
766 IP_VS_RT_MODE_NON_LOCAL)))
1da177e4 767 goto tx_error_icmp;
fc604767
JA
768 if (rt->rt_flags & RTCF_LOCAL) {
769 ip_rt_put(rt);
770 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
771 }
1da177e4 772
d8d1f30b 773 tdev = rt->dst.dev;
1da177e4 774
d8d1f30b 775 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
1da177e4 776 if (mtu < 68) {
1e3e238e 777 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
fc604767 778 goto tx_error_put;
1da177e4 779 }
adf30907
ED
780 if (skb_dst(skb))
781 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
1da177e4 782
4412ec49 783 df |= (old_iph->frag_off & htons(IP_DF));
1da177e4 784
8f1b03a4
SH
785 if ((old_iph->frag_off & htons(IP_DF) &&
786 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
1da177e4 787 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 788 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 789 goto tx_error_put;
1da177e4
LT
790 }
791
792 /*
793 * Okay, now see if we can stuff it in the buffer as-is.
794 */
795 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
796
797 if (skb_headroom(skb) < max_headroom
798 || skb_cloned(skb) || skb_shared(skb)) {
799 struct sk_buff *new_skb =
800 skb_realloc_headroom(skb, max_headroom);
801 if (!new_skb) {
802 ip_rt_put(rt);
803 kfree_skb(skb);
1e3e238e 804 IP_VS_ERR_RL("%s(): no memory\n", __func__);
1da177e4
LT
805 return NF_STOLEN;
806 }
807 kfree_skb(skb);
808 skb = new_skb;
eddc9ec5 809 old_iph = ip_hdr(skb);
1da177e4
LT
810 }
811
714f095f 812 skb->transport_header = skb->network_header;
1da177e4
LT
813
814 /* fix old IP header checksum */
815 ip_send_check(old_iph);
816
e2d1bca7
ACM
817 skb_push(skb, sizeof(struct iphdr));
818 skb_reset_network_header(skb);
1da177e4
LT
819 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
820
821 /* drop old route */
adf30907 822 skb_dst_drop(skb);
d8d1f30b 823 skb_dst_set(skb, &rt->dst);
1da177e4
LT
824
825 /*
826 * Push down and install the IPIP header.
827 */
eddc9ec5 828 iph = ip_hdr(skb);
1da177e4
LT
829 iph->version = 4;
830 iph->ihl = sizeof(struct iphdr)>>2;
831 iph->frag_off = df;
832 iph->protocol = IPPROTO_IPIP;
833 iph->tos = tos;
834 iph->daddr = rt->rt_dst;
835 iph->saddr = rt->rt_src;
836 iph->ttl = old_iph->ttl;
d8d1f30b 837 ip_select_ident(iph, &rt->dst, NULL);
1da177e4
LT
838
839 /* Another hack: avoid icmp_send in ip_fragment */
840 skb->local_df = 1;
841
f4bc17cd
JA
842 ret = IP_VS_XMIT_TUNNEL(skb, cp);
843 if (ret == NF_ACCEPT)
844 ip_local_out(skb);
845 else if (ret == NF_DROP)
846 kfree_skb(skb);
1da177e4
LT
847
848 LeaveFunction(10);
849
850 return NF_STOLEN;
851
852 tx_error_icmp:
853 dst_link_failure(skb);
854 tx_error:
855 kfree_skb(skb);
856 LeaveFunction(10);
857 return NF_STOLEN;
fc604767
JA
858tx_error_put:
859 ip_rt_put(rt);
860 goto tx_error;
1da177e4
LT
861}
862
b3cdd2a7
JV
863#ifdef CONFIG_IP_VS_IPV6
864int
865ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
866 struct ip_vs_protocol *pp)
867{
868 struct rt6_info *rt; /* Route to the other host */
714f095f 869 struct in6_addr saddr; /* Source for tunnel */
b3cdd2a7
JV
870 struct net_device *tdev; /* Device to other host */
871 struct ipv6hdr *old_iph = ipv6_hdr(skb);
b3cdd2a7
JV
872 struct ipv6hdr *iph; /* Our new IP header */
873 unsigned int max_headroom; /* The extra header space needed */
874 int mtu;
f4bc17cd 875 int ret;
b3cdd2a7
JV
876
877 EnterFunction(10);
878
fc604767 879 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
e58b3442
DM
880 &saddr, 1, (IP_VS_RT_MODE_LOCAL |
881 IP_VS_RT_MODE_NON_LOCAL))))
b3cdd2a7 882 goto tx_error_icmp;
fc604767
JA
883 if (__ip_vs_is_local_route6(rt)) {
884 dst_release(&rt->dst);
885 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
886 }
b3cdd2a7 887
d8d1f30b 888 tdev = rt->dst.dev;
b3cdd2a7 889
d8d1f30b 890 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
714f095f 891 if (mtu < IPV6_MIN_MTU) {
714f095f
HS
892 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
893 IPV6_MIN_MTU);
fc604767 894 goto tx_error_put;
b3cdd2a7 895 }
adf30907
ED
896 if (skb_dst(skb))
897 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
b3cdd2a7 898
8f1b03a4
SH
899 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
900 !skb_is_gso(skb)) {
cb59155f
JA
901 if (!skb->dev) {
902 struct net *net = dev_net(skb_dst(skb)->dev);
903
904 skb->dev = net->loopback_dev;
905 }
3ffe533c 906 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1e3e238e 907 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 908 goto tx_error_put;
b3cdd2a7
JV
909 }
910
911 /*
912 * Okay, now see if we can stuff it in the buffer as-is.
913 */
914 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
915
916 if (skb_headroom(skb) < max_headroom
917 || skb_cloned(skb) || skb_shared(skb)) {
918 struct sk_buff *new_skb =
919 skb_realloc_headroom(skb, max_headroom);
920 if (!new_skb) {
d8d1f30b 921 dst_release(&rt->dst);
b3cdd2a7 922 kfree_skb(skb);
1e3e238e 923 IP_VS_ERR_RL("%s(): no memory\n", __func__);
b3cdd2a7
JV
924 return NF_STOLEN;
925 }
926 kfree_skb(skb);
927 skb = new_skb;
928 old_iph = ipv6_hdr(skb);
929 }
930
714f095f 931 skb->transport_header = skb->network_header;
b3cdd2a7
JV
932
933 skb_push(skb, sizeof(struct ipv6hdr));
934 skb_reset_network_header(skb);
935 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
936
937 /* drop old route */
adf30907 938 skb_dst_drop(skb);
d8d1f30b 939 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
940
941 /*
942 * Push down and install the IPIP header.
943 */
944 iph = ipv6_hdr(skb);
945 iph->version = 6;
946 iph->nexthdr = IPPROTO_IPV6;
b7b45f47
HH
947 iph->payload_len = old_iph->payload_len;
948 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
b3cdd2a7
JV
949 iph->priority = old_iph->priority;
950 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
714f095f
HS
951 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
952 ipv6_addr_copy(&iph->saddr, &saddr);
b3cdd2a7
JV
953 iph->hop_limit = old_iph->hop_limit;
954
955 /* Another hack: avoid icmp_send in ip_fragment */
956 skb->local_df = 1;
957
f4bc17cd
JA
958 ret = IP_VS_XMIT_TUNNEL(skb, cp);
959 if (ret == NF_ACCEPT)
960 ip6_local_out(skb);
961 else if (ret == NF_DROP)
962 kfree_skb(skb);
b3cdd2a7
JV
963
964 LeaveFunction(10);
965
966 return NF_STOLEN;
967
968tx_error_icmp:
969 dst_link_failure(skb);
970tx_error:
971 kfree_skb(skb);
972 LeaveFunction(10);
973 return NF_STOLEN;
fc604767
JA
974tx_error_put:
975 dst_release(&rt->dst);
976 goto tx_error;
b3cdd2a7
JV
977}
978#endif
979
1da177e4
LT
980
981/*
982 * Direct Routing transmitter
983 * Used for ANY protocol
984 */
985int
986ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
987 struct ip_vs_protocol *pp)
988{
989 struct rtable *rt; /* Route to the other host */
eddc9ec5 990 struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
991 int mtu;
992
993 EnterFunction(10);
994
fc604767 995 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
996 RT_TOS(iph->tos),
997 IP_VS_RT_MODE_LOCAL |
998 IP_VS_RT_MODE_NON_LOCAL)))
1da177e4 999 goto tx_error_icmp;
fc604767
JA
1000 if (rt->rt_flags & RTCF_LOCAL) {
1001 ip_rt_put(rt);
1002 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1003 }
1da177e4
LT
1004
1005 /* MTU checking */
d8d1f30b 1006 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
1007 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1008 !skb_is_gso(skb)) {
1da177e4
LT
1009 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1010 ip_rt_put(rt);
1e3e238e 1011 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1da177e4
LT
1012 goto tx_error;
1013 }
1014
1015 /*
1016 * Call ip_send_check because we are not sure it is called
1017 * after ip_defrag. Is copy-on-write needed?
1018 */
1019 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1020 ip_rt_put(rt);
1021 return NF_STOLEN;
1022 }
eddc9ec5 1023 ip_send_check(ip_hdr(skb));
1da177e4
LT
1024
1025 /* drop old route */
adf30907 1026 skb_dst_drop(skb);
d8d1f30b 1027 skb_dst_set(skb, &rt->dst);
1da177e4
LT
1028
1029 /* Another hack: avoid icmp_send in ip_fragment */
1030 skb->local_df = 1;
1031
fc604767 1032 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1da177e4
LT
1033
1034 LeaveFunction(10);
1035 return NF_STOLEN;
1036
1037 tx_error_icmp:
1038 dst_link_failure(skb);
1039 tx_error:
1040 kfree_skb(skb);
1041 LeaveFunction(10);
1042 return NF_STOLEN;
1043}
1044
b3cdd2a7
JV
1045#ifdef CONFIG_IP_VS_IPV6
1046int
1047ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1048 struct ip_vs_protocol *pp)
1049{
1050 struct rt6_info *rt; /* Route to the other host */
1051 int mtu;
1052
1053 EnterFunction(10);
1054
fc604767 1055 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
1056 0, (IP_VS_RT_MODE_LOCAL |
1057 IP_VS_RT_MODE_NON_LOCAL))))
b3cdd2a7 1058 goto tx_error_icmp;
fc604767
JA
1059 if (__ip_vs_is_local_route6(rt)) {
1060 dst_release(&rt->dst);
1061 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1062 }
b3cdd2a7
JV
1063
1064 /* MTU checking */
d8d1f30b 1065 mtu = dst_mtu(&rt->dst);
b3cdd2a7 1066 if (skb->len > mtu) {
cb59155f
JA
1067 if (!skb->dev) {
1068 struct net *net = dev_net(skb_dst(skb)->dev);
1069
1070 skb->dev = net->loopback_dev;
1071 }
3ffe533c 1072 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
d8d1f30b 1073 dst_release(&rt->dst);
1e3e238e 1074 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
b3cdd2a7
JV
1075 goto tx_error;
1076 }
1077
1078 /*
1079 * Call ip_send_check because we are not sure it is called
1080 * after ip_defrag. Is copy-on-write needed?
1081 */
1082 skb = skb_share_check(skb, GFP_ATOMIC);
1083 if (unlikely(skb == NULL)) {
d8d1f30b 1084 dst_release(&rt->dst);
b3cdd2a7
JV
1085 return NF_STOLEN;
1086 }
1087
1088 /* drop old route */
adf30907 1089 skb_dst_drop(skb);
d8d1f30b 1090 skb_dst_set(skb, &rt->dst);
b3cdd2a7
JV
1091
1092 /* Another hack: avoid icmp_send in ip_fragment */
1093 skb->local_df = 1;
1094
fc604767 1095 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
b3cdd2a7
JV
1096
1097 LeaveFunction(10);
1098 return NF_STOLEN;
1099
1100tx_error_icmp:
1101 dst_link_failure(skb);
1102tx_error:
1103 kfree_skb(skb);
1104 LeaveFunction(10);
1105 return NF_STOLEN;
1106}
1107#endif
1108
1da177e4
LT
1109
1110/*
1111 * ICMP packet transmitter
1112 * called by the ip_vs_in_icmp
1113 */
1114int
1115ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1116 struct ip_vs_protocol *pp, int offset)
1117{
1118 struct rtable *rt; /* Route to the other host */
1119 int mtu;
1120 int rc;
fc604767 1121 int local;
1da177e4
LT
1122
1123 EnterFunction(10);
1124
1125 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1126 forwarded directly here, because there is no need to
1127 translate address/port back */
1128 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1129 if (cp->packet_xmit)
1130 rc = cp->packet_xmit(skb, cp, pp);
1131 else
1132 rc = NF_ACCEPT;
1133 /* do not touch skb anymore */
1134 atomic_inc(&cp->in_pkts);
1da177e4
LT
1135 goto out;
1136 }
1137
1138 /*
1139 * mangle and send the packet here (only for VS/NAT)
1140 */
1141
fc604767 1142 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
17a8f8e3
CG
1143 RT_TOS(ip_hdr(skb)->tos),
1144 IP_VS_RT_MODE_LOCAL |
1145 IP_VS_RT_MODE_NON_LOCAL |
1146 IP_VS_RT_MODE_RDR)))
1da177e4 1147 goto tx_error_icmp;
fc604767
JA
1148 local = rt->rt_flags & RTCF_LOCAL;
1149
1150 /*
1151 * Avoid duplicate tuple in reply direction for NAT traffic
1152 * to local address when connection is sync-ed
1153 */
1154#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1155 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1156 enum ip_conntrack_info ctinfo;
1157 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1158
1159 if (ct && !nf_ct_is_untracked(ct)) {
1160 IP_VS_DBG(10, "%s(): "
1161 "stopping DNAT to local address %pI4\n",
1162 __func__, &cp->daddr.ip);
1163 goto tx_error_put;
1164 }
1165 }
1166#endif
1167
1168 /* From world but DNAT to loopback address? */
c7537967
DM
1169 if (local && ipv4_is_loopback(rt->rt_dst) &&
1170 rt_is_input_route(skb_rtable(skb))) {
fc604767
JA
1171 IP_VS_DBG(1, "%s(): "
1172 "stopping DNAT to loopback %pI4\n",
1173 __func__, &cp->daddr.ip);
1174 goto tx_error_put;
1175 }
1da177e4
LT
1176
1177 /* MTU checking */
d8d1f30b 1178 mtu = dst_mtu(&rt->dst);
8f1b03a4
SH
1179 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1180 !skb_is_gso(skb)) {
1da177e4 1181 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1e3e238e 1182 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 1183 goto tx_error_put;
1da177e4
LT
1184 }
1185
1186 /* copy-on-write the packet before mangling it */
af1e1cf0 1187 if (!skb_make_writable(skb, offset))
1da177e4
LT
1188 goto tx_error_put;
1189
d8d1f30b 1190 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1da177e4
LT
1191 goto tx_error_put;
1192
1da177e4
LT
1193 ip_vs_nat_icmp(skb, pp, cp, 0);
1194
fc604767
JA
1195 if (!local) {
1196 /* drop the old route when skb is not shared */
1197 skb_dst_drop(skb);
1198 skb_dst_set(skb, &rt->dst);
1199 } else {
1200 ip_rt_put(rt);
1201 /*
1202 * Some IPv4 replies get local address from routes,
1203 * not from iph, so while we DNAT after routing
1204 * we need this second input/output route.
1205 */
1206 if (!__ip_vs_reroute_locally(skb))
1207 goto tx_error;
1208 }
1209
1da177e4
LT
1210 /* Another hack: avoid icmp_send in ip_fragment */
1211 skb->local_df = 1;
1212
fc604767 1213 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1da177e4
LT
1214
1215 rc = NF_STOLEN;
1216 goto out;
1217
1218 tx_error_icmp:
1219 dst_link_failure(skb);
1220 tx_error:
1221 dev_kfree_skb(skb);
1222 rc = NF_STOLEN;
1223 out:
1224 LeaveFunction(10);
1225 return rc;
1226 tx_error_put:
1227 ip_rt_put(rt);
1228 goto tx_error;
1229}
b3cdd2a7
JV
1230
1231#ifdef CONFIG_IP_VS_IPV6
1232int
1233ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1234 struct ip_vs_protocol *pp, int offset)
1235{
1236 struct rt6_info *rt; /* Route to the other host */
1237 int mtu;
1238 int rc;
fc604767 1239 int local;
b3cdd2a7
JV
1240
1241 EnterFunction(10);
1242
1243 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1244 forwarded directly here, because there is no need to
1245 translate address/port back */
1246 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1247 if (cp->packet_xmit)
1248 rc = cp->packet_xmit(skb, cp, pp);
1249 else
1250 rc = NF_ACCEPT;
1251 /* do not touch skb anymore */
1252 atomic_inc(&cp->in_pkts);
1253 goto out;
1254 }
1255
1256 /*
1257 * mangle and send the packet here (only for VS/NAT)
1258 */
1259
fc604767 1260 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
e58b3442
DM
1261 0, (IP_VS_RT_MODE_LOCAL |
1262 IP_VS_RT_MODE_NON_LOCAL |
1263 IP_VS_RT_MODE_RDR))))
b3cdd2a7
JV
1264 goto tx_error_icmp;
1265
fc604767
JA
1266 local = __ip_vs_is_local_route6(rt);
1267 /*
1268 * Avoid duplicate tuple in reply direction for NAT traffic
1269 * to local address when connection is sync-ed
1270 */
1271#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1272 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1273 enum ip_conntrack_info ctinfo;
1274 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1275
1276 if (ct && !nf_ct_is_untracked(ct)) {
1277 IP_VS_DBG(10, "%s(): "
1278 "stopping DNAT to local address %pI6\n",
1279 __func__, &cp->daddr.in6);
1280 goto tx_error_put;
1281 }
1282 }
1283#endif
1284
1285 /* From world but DNAT to loopback address? */
1286 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1287 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1288 IP_VS_DBG(1, "%s(): "
1289 "stopping DNAT to loopback %pI6\n",
1290 __func__, &cp->daddr.in6);
1291 goto tx_error_put;
1292 }
1293
b3cdd2a7 1294 /* MTU checking */
d8d1f30b 1295 mtu = dst_mtu(&rt->dst);
8f1b03a4 1296 if (skb->len > mtu && !skb_is_gso(skb)) {
cb59155f
JA
1297 if (!skb->dev) {
1298 struct net *net = dev_net(skb_dst(skb)->dev);
1299
1300 skb->dev = net->loopback_dev;
1301 }
3ffe533c 1302 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1e3e238e 1303 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
fc604767 1304 goto tx_error_put;
b3cdd2a7
JV
1305 }
1306
1307 /* copy-on-write the packet before mangling it */
1308 if (!skb_make_writable(skb, offset))
1309 goto tx_error_put;
1310
d8d1f30b 1311 if (skb_cow(skb, rt->dst.dev->hard_header_len))
b3cdd2a7
JV
1312 goto tx_error_put;
1313
b3cdd2a7
JV
1314 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1315
fc604767
JA
1316 if (!local || !skb->dev) {
1317 /* drop the old route when skb is not shared */
1318 skb_dst_drop(skb);
1319 skb_dst_set(skb, &rt->dst);
1320 } else {
1321 /* destined to loopback, do we need to change route? */
1322 dst_release(&rt->dst);
1323 }
1324
b3cdd2a7
JV
1325 /* Another hack: avoid icmp_send in ip_fragment */
1326 skb->local_df = 1;
1327
fc604767 1328 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
b3cdd2a7
JV
1329
1330 rc = NF_STOLEN;
1331 goto out;
1332
1333tx_error_icmp:
1334 dst_link_failure(skb);
1335tx_error:
1336 dev_kfree_skb(skb);
1337 rc = NF_STOLEN;
1338out:
1339 LeaveFunction(10);
1340 return rc;
1341tx_error_put:
d8d1f30b 1342 dst_release(&rt->dst);
b3cdd2a7
JV
1343 goto tx_error;
1344}
1345#endif