]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/netfilter/ipvs/ip_vs_xmit.c
Merge 4.2-rc6 into usb-next
[mirror_ubuntu-zesty-kernel.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
026ace06
JA
20 * - we can use dst without ref while sending in RCU section, we use
21 * ref when returning NF_ACCEPT for NAT-ed packet via loopback
cb59155f
JA
22 * LOCAL_OUT rules:
23 * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING)
24 * - skb->pkt_type is not set yet
25 * - the only place where we can see skb->sk != NULL
1da177e4
LT
26 */
27
9aada7ac
HE
28#define KMSG_COMPONENT "IPVS"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
1da177e4 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
1da177e4 33#include <linux/tcp.h> /* for tcphdr */
c439cb2e 34#include <net/ip.h>
1da177e4
LT
35#include <net/tcp.h> /* for csum_tcpudp_magic */
36#include <net/udp.h>
37#include <net/icmp.h> /* for icmp_send */
38#include <net/route.h> /* for ip_route_output */
38cdcc9a
JV
39#include <net/ipv6.h>
40#include <net/ip6_route.h>
ea1d5d77 41#include <net/ip_tunnels.h>
714f095f 42#include <net/addrconf.h>
38cdcc9a 43#include <linux/icmpv6.h>
1da177e4
LT
44#include <linux/netfilter.h>
45#include <linux/netfilter_ipv4.h>
46
47#include <net/ip_vs.h>
48
17a8f8e3
CG
49enum {
50 IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */
51 IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */
52 IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to
53 * local
54 */
f2edb9f7 55 IP_VS_RT_MODE_CONNECT = 8, /* Always bind route to saddr */
ad4d3ef8 56 IP_VS_RT_MODE_KNOWN_NH = 16,/* Route via remote addr */
4115ded1 57 IP_VS_RT_MODE_TUNNEL = 32,/* Tunnel mode */
17a8f8e3 58};
1da177e4 59
026ace06
JA
60static inline struct ip_vs_dest_dst *ip_vs_dest_dst_alloc(void)
61{
62 return kmalloc(sizeof(struct ip_vs_dest_dst), GFP_ATOMIC);
63}
64
65static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
66{
67 kfree(dest_dst);
68}
69
1da177e4
LT
70/*
71 * Destination cache to speed up outgoing route lookup
72 */
73static inline void
026ace06
JA
74__ip_vs_dst_set(struct ip_vs_dest *dest, struct ip_vs_dest_dst *dest_dst,
75 struct dst_entry *dst, u32 dst_cookie)
1da177e4 76{
026ace06
JA
77 struct ip_vs_dest_dst *old;
78
79 old = rcu_dereference_protected(dest->dest_dst,
80 lockdep_is_held(&dest->dst_lock));
81
82 if (dest_dst) {
83 dest_dst->dst_cache = dst;
84 dest_dst->dst_cookie = dst_cookie;
85 }
86 rcu_assign_pointer(dest->dest_dst, dest_dst);
1da177e4 87
026ace06
JA
88 if (old)
89 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
1da177e4
LT
90}
91
026ace06 92static inline struct ip_vs_dest_dst *
c90558da 93__ip_vs_dst_check(struct ip_vs_dest *dest)
1da177e4 94{
026ace06
JA
95 struct ip_vs_dest_dst *dest_dst = rcu_dereference(dest->dest_dst);
96 struct dst_entry *dst;
1da177e4 97
026ace06 98 if (!dest_dst)
1da177e4 99 return NULL;
026ace06
JA
100 dst = dest_dst->dst_cache;
101 if (dst->obsolete &&
102 dst->ops->check(dst, dest_dst->dst_cookie) == NULL)
1da177e4 103 return NULL;
026ace06 104 return dest_dst;
1da177e4
LT
105}
106
590e3f79
JDB
107static inline bool
108__mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
109{
4cdd3408
PM
110 if (IP6CB(skb)->frag_max_size) {
111 /* frag_max_size tell us that, this packet have been
112 * defragmented by netfilter IPv6 conntrack module.
113 */
114 if (IP6CB(skb)->frag_max_size > mtu)
115 return true; /* largest fragment violate MTU */
116 }
117 else if (skb->len > mtu && !skb_is_gso(skb)) {
590e3f79
JDB
118 return true; /* Packet size violate MTU size */
119 }
120 return false;
121}
122
f2edb9f7
JA
123/* Get route to daddr, update *saddr, optionally bind route to saddr */
124static struct rtable *do_output_route4(struct net *net, __be32 daddr,
c90558da 125 int rt_mode, __be32 *saddr)
f2edb9f7
JA
126{
127 struct flowi4 fl4;
128 struct rtable *rt;
129 int loop = 0;
130
131 memset(&fl4, 0, sizeof(fl4));
132 fl4.daddr = daddr;
ad4d3ef8
JA
133 fl4.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
134 FLOWI_FLAG_KNOWN_NH : 0;
f2edb9f7
JA
135
136retry:
137 rt = ip_route_output_key(net, &fl4);
138 if (IS_ERR(rt)) {
139 /* Invalid saddr ? */
140 if (PTR_ERR(rt) == -EINVAL && *saddr &&
141 rt_mode & IP_VS_RT_MODE_CONNECT && !loop) {
142 *saddr = 0;
c90558da 143 flowi4_update_output(&fl4, 0, 0, daddr, 0);
f2edb9f7
JA
144 goto retry;
145 }
146 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", &daddr);
147 return NULL;
148 } else if (!*saddr && rt_mode & IP_VS_RT_MODE_CONNECT && fl4.saddr) {
149 ip_rt_put(rt);
150 *saddr = fl4.saddr;
c90558da 151 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
f2edb9f7
JA
152 loop++;
153 goto retry;
154 }
155 *saddr = fl4.saddr;
156 return rt;
157}
158
4a4739d5
AG
159#ifdef CONFIG_IP_VS_IPV6
160static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
161{
162 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
163}
164#endif
165
166static inline bool crosses_local_route_boundary(int skb_af, struct sk_buff *skb,
167 int rt_mode,
168 bool new_rt_is_local)
169{
170 bool rt_mode_allow_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
171 bool rt_mode_allow_non_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
172 bool rt_mode_allow_redirect = !!(rt_mode & IP_VS_RT_MODE_RDR);
173 bool source_is_loopback;
174 bool old_rt_is_local;
175
176#ifdef CONFIG_IP_VS_IPV6
177 if (skb_af == AF_INET6) {
178 int addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
179
180 source_is_loopback =
181 (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
182 (addr_type & IPV6_ADDR_LOOPBACK);
183 old_rt_is_local = __ip_vs_is_local_route6(
184 (struct rt6_info *)skb_dst(skb));
185 } else
186#endif
187 {
188 source_is_loopback = ipv4_is_loopback(ip_hdr(skb)->saddr);
189 old_rt_is_local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
190 }
191
192 if (unlikely(new_rt_is_local)) {
193 if (!rt_mode_allow_local)
194 return true;
195 if (!rt_mode_allow_redirect && !old_rt_is_local)
196 return true;
197 } else {
198 if (!rt_mode_allow_non_local)
199 return true;
200 if (source_is_loopback)
201 return true;
202 }
203 return false;
204}
205
919aa0b2
AG
206static inline void maybe_update_pmtu(int skb_af, struct sk_buff *skb, int mtu)
207{
208 struct sock *sk = skb->sk;
209 struct rtable *ort = skb_rtable(skb);
210
a8399231 211 if (!skb->dev && sk && sk_fullsock(sk))
919aa0b2
AG
212 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
213}
214
c63e4de2
AG
215static inline bool ensure_mtu_is_adequate(int skb_af, int rt_mode,
216 struct ip_vs_iphdr *ipvsh,
217 struct sk_buff *skb, int mtu)
218{
219#ifdef CONFIG_IP_VS_IPV6
220 if (skb_af == AF_INET6) {
221 struct net *net = dev_net(skb_dst(skb)->dev);
222
223 if (unlikely(__mtu_check_toobig_v6(skb, mtu))) {
224 if (!skb->dev)
225 skb->dev = net->loopback_dev;
226 /* only send ICMP too big on first fragment */
227 if (!ipvsh->fragoffs)
228 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
229 IP_VS_DBG(1, "frag needed for %pI6c\n",
230 &ipv6_hdr(skb)->saddr);
231 return false;
232 }
233 } else
234#endif
235 {
236 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
237
238 /* If we're going to tunnel the packet and pmtu discovery
239 * is disabled, we'll just fragment it anyway
240 */
241 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
242 return true;
243
244 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_DF) &&
245 skb->len > mtu && !skb_is_gso(skb))) {
246 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
247 htonl(mtu));
248 IP_VS_DBG(1, "frag needed for %pI4\n",
249 &ip_hdr(skb)->saddr);
250 return false;
251 }
252 }
253
254 return true;
255}
256
17a8f8e3 257/* Get route to destination or remote server */
4115ded1 258static int
4a4739d5 259__ip_vs_get_out_rt(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
c63e4de2
AG
260 __be32 daddr, int rt_mode, __be32 *ret_saddr,
261 struct ip_vs_iphdr *ipvsh)
1da177e4 262{
fc604767 263 struct net *net = dev_net(skb_dst(skb)->dev);
026ace06 264 struct ip_vs_dest_dst *dest_dst;
1da177e4 265 struct rtable *rt; /* Route to the other host */
4115ded1 266 int mtu;
026ace06 267 int local, noref = 1;
1da177e4
LT
268
269 if (dest) {
026ace06
JA
270 dest_dst = __ip_vs_dst_check(dest);
271 if (likely(dest_dst))
272 rt = (struct rtable *) dest_dst->dst_cache;
273 else {
274 dest_dst = ip_vs_dest_dst_alloc();
ac69269a 275 spin_lock_bh(&dest->dst_lock);
026ace06
JA
276 if (!dest_dst) {
277 __ip_vs_dst_set(dest, NULL, NULL, 0);
ac69269a 278 spin_unlock_bh(&dest->dst_lock);
026ace06
JA
279 goto err_unreach;
280 }
c90558da 281 rt = do_output_route4(net, dest->addr.ip, rt_mode,
026ace06 282 &dest_dst->dst_saddr.ip);
f2edb9f7 283 if (!rt) {
026ace06 284 __ip_vs_dst_set(dest, NULL, NULL, 0);
ac69269a 285 spin_unlock_bh(&dest->dst_lock);
026ace06 286 ip_vs_dest_dst_free(dest_dst);
4115ded1 287 goto err_unreach;
1da177e4 288 }
026ace06 289 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
ac69269a 290 spin_unlock_bh(&dest->dst_lock);
c90558da 291 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
026ace06 292 &dest->addr.ip, &dest_dst->dst_saddr.ip,
c90558da 293 atomic_read(&rt->dst.__refcnt));
1da177e4 294 }
c92f5ca2 295 if (ret_saddr)
026ace06 296 *ret_saddr = dest_dst->dst_saddr.ip;
1da177e4 297 } else {
f2edb9f7 298 __be32 saddr = htonl(INADDR_ANY);
c92f5ca2 299
026ace06
JA
300 noref = 0;
301
f2edb9f7
JA
302 /* For such unconfigured boxes avoid many route lookups
303 * for performance reasons because we do not remember saddr
304 */
305 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
c90558da 306 rt = do_output_route4(net, daddr, rt_mode, &saddr);
f2edb9f7 307 if (!rt)
4115ded1 308 goto err_unreach;
c92f5ca2 309 if (ret_saddr)
f2edb9f7 310 *ret_saddr = saddr;
1da177e4
LT
311 }
312
4115ded1 313 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
4a4739d5
AG
314 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
315 local))) {
316 IP_VS_DBG_RL("We are crossing local and non-local addresses"
3d53666b 317 " daddr=%pI4\n", &daddr);
4115ded1 318 goto err_put;
fc604767 319 }
4a4739d5
AG
320
321 if (unlikely(local)) {
4115ded1 322 /* skb to local stack, preserve old route */
026ace06
JA
323 if (!noref)
324 ip_rt_put(rt);
4115ded1 325 return local;
fc604767 326 }
4115ded1
JA
327
328 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
329 mtu = dst_mtu(&rt->dst);
4115ded1 330 } else {
4115ded1
JA
331 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
332 if (mtu < 68) {
333 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
334 goto err_put;
335 }
919aa0b2 336 maybe_update_pmtu(skb_af, skb, mtu);
fc604767
JA
337 }
338
c63e4de2 339 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
4115ded1 340 goto err_put;
4115ded1
JA
341
342 skb_dst_drop(skb);
026ace06
JA
343 if (noref) {
344 if (!local)
dbfc4fb7 345 skb_dst_set_noref(skb, &rt->dst);
026ace06
JA
346 else
347 skb_dst_set(skb, dst_clone(&rt->dst));
348 } else
349 skb_dst_set(skb, &rt->dst);
4115ded1
JA
350
351 return local;
352
353err_put:
026ace06
JA
354 if (!noref)
355 ip_rt_put(rt);
4115ded1
JA
356 return -1;
357
358err_unreach:
359 dst_link_failure(skb);
360 return -1;
1da177e4
LT
361}
362
38cdcc9a 363#ifdef CONFIG_IP_VS_IPV6
714f095f
HS
364static struct dst_entry *
365__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
48e8aa6e 366 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
714f095f
HS
367{
368 struct dst_entry *dst;
4c9483b2
DM
369 struct flowi6 fl6 = {
370 .daddr = *daddr,
714f095f
HS
371 };
372
48e8aa6e
MKL
373 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
374 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
375
4c9483b2 376 dst = ip6_route_output(net, NULL, &fl6);
714f095f
HS
377 if (dst->error)
378 goto out_err;
379 if (!ret_saddr)
380 return dst;
4c9483b2 381 if (ipv6_addr_any(&fl6.saddr) &&
714f095f 382 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
4c9483b2 383 &fl6.daddr, 0, &fl6.saddr) < 0)
714f095f 384 goto out_err;
452edd59 385 if (do_xfrm) {
4c9483b2 386 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
452edd59
DM
387 if (IS_ERR(dst)) {
388 dst = NULL;
389 goto out_err;
390 }
391 }
4e3fd7a0 392 *ret_saddr = fl6.saddr;
714f095f
HS
393 return dst;
394
395out_err:
396 dst_release(dst);
397 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
398 return NULL;
399}
400
fc604767
JA
401/*
402 * Get route to destination or remote server
fc604767 403 */
4115ded1 404static int
4a4739d5 405__ip_vs_get_out_rt_v6(int skb_af, struct sk_buff *skb, struct ip_vs_dest *dest,
fc604767 406 struct in6_addr *daddr, struct in6_addr *ret_saddr,
4115ded1 407 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
38cdcc9a 408{
fc604767 409 struct net *net = dev_net(skb_dst(skb)->dev);
026ace06 410 struct ip_vs_dest_dst *dest_dst;
38cdcc9a 411 struct rt6_info *rt; /* Route to the other host */
714f095f 412 struct dst_entry *dst;
4115ded1 413 int mtu;
026ace06 414 int local, noref = 1;
38cdcc9a
JV
415
416 if (dest) {
026ace06
JA
417 dest_dst = __ip_vs_dst_check(dest);
418 if (likely(dest_dst))
419 rt = (struct rt6_info *) dest_dst->dst_cache;
420 else {
714f095f 421 u32 cookie;
38cdcc9a 422
026ace06 423 dest_dst = ip_vs_dest_dst_alloc();
ac69269a 424 spin_lock_bh(&dest->dst_lock);
026ace06
JA
425 if (!dest_dst) {
426 __ip_vs_dst_set(dest, NULL, NULL, 0);
ac69269a 427 spin_unlock_bh(&dest->dst_lock);
026ace06
JA
428 goto err_unreach;
429 }
714f095f 430 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
026ace06 431 &dest_dst->dst_saddr.in6,
48e8aa6e 432 do_xfrm, rt_mode);
714f095f 433 if (!dst) {
026ace06 434 __ip_vs_dst_set(dest, NULL, NULL, 0);
ac69269a 435 spin_unlock_bh(&dest->dst_lock);
026ace06 436 ip_vs_dest_dst_free(dest_dst);
4115ded1 437 goto err_unreach;
38cdcc9a 438 }
714f095f 439 rt = (struct rt6_info *) dst;
b197df4f 440 cookie = rt6_get_cookie(rt);
026ace06 441 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
ac69269a 442 spin_unlock_bh(&dest->dst_lock);
714f095f 443 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
026ace06 444 &dest->addr.in6, &dest_dst->dst_saddr.in6,
d8d1f30b 445 atomic_read(&rt->dst.__refcnt));
38cdcc9a 446 }
714f095f 447 if (ret_saddr)
026ace06 448 *ret_saddr = dest_dst->dst_saddr.in6;
38cdcc9a 449 } else {
026ace06 450 noref = 0;
48e8aa6e
MKL
451 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
452 rt_mode);
714f095f 453 if (!dst)
4115ded1 454 goto err_unreach;
714f095f 455 rt = (struct rt6_info *) dst;
38cdcc9a
JV
456 }
457
fc604767 458 local = __ip_vs_is_local_route6(rt);
4a4739d5
AG
459
460 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
461 local))) {
462 IP_VS_DBG_RL("We are crossing local and non-local addresses"
3d53666b 463 " daddr=%pI6\n", daddr);
4115ded1 464 goto err_put;
fc604767 465 }
4a4739d5
AG
466
467 if (unlikely(local)) {
4115ded1 468 /* skb to local stack, preserve old route */
026ace06
JA
469 if (!noref)
470 dst_release(&rt->dst);
4115ded1 471 return local;
fc604767 472 }
4115ded1
JA
473
474 /* MTU checking */
475 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
476 mtu = dst_mtu(&rt->dst);
477 else {
4115ded1
JA
478 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
479 if (mtu < IPV6_MIN_MTU) {
480 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
481 IPV6_MIN_MTU);
482 goto err_put;
483 }
919aa0b2 484 maybe_update_pmtu(skb_af, skb, mtu);
fc604767
JA
485 }
486
c63e4de2 487 if (!ensure_mtu_is_adequate(skb_af, rt_mode, ipvsh, skb, mtu))
4115ded1 488 goto err_put;
4115ded1
JA
489
490 skb_dst_drop(skb);
026ace06
JA
491 if (noref) {
492 if (!local)
dbfc4fb7 493 skb_dst_set_noref(skb, &rt->dst);
026ace06
JA
494 else
495 skb_dst_set(skb, dst_clone(&rt->dst));
496 } else
497 skb_dst_set(skb, &rt->dst);
4115ded1
JA
498
499 return local;
500
501err_put:
026ace06
JA
502 if (!noref)
503 dst_release(&rt->dst);
4115ded1
JA
504 return -1;
505
506err_unreach:
326bf17e
AG
507 /* The ip6_link_failure function requires the dev field to be set
508 * in order to get the net (further for the sake of fwmark
509 * reflection).
510 */
511 if (!skb->dev)
512 skb->dev = skb_dst(skb)->dev;
513
4115ded1
JA
514 dst_link_failure(skb);
515 return -1;
38cdcc9a
JV
516}
517#endif
518
1da177e4 519
b8abdf09
JA
520/* return NF_ACCEPT to allow forwarding or other NF_xxx on error */
521static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
522 struct ip_vs_conn *cp)
523{
524 int ret = NF_ACCEPT;
525
526 skb->ipvs_property = 1;
527 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
528 ret = ip_vs_confirm_conntrack(skb);
529 if (ret == NF_ACCEPT) {
530 nf_reset(skb);
531 skb_forward_csum(skb);
e3895c03
JA
532 if (!skb->sk)
533 skb_sender_cpu_clear(skb);
b8abdf09
JA
534 }
535 return ret;
536}
537
71563f34
AG
538/* In the event of a remote destination, it's possible that we would have
539 * matches against an old socket (particularly a TIME-WAIT socket). This
540 * causes havoc down the line (ip_local_out et. al. expect regular sockets
541 * and invalid memory accesses will happen) so simply drop the association
542 * in this case.
543*/
544static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
545{
546 /* If dev is set, the packet came from the LOCAL_IN callback and
547 * not from a local TCP socket.
548 */
549 if (skb->dev)
550 skb_orphan(skb);
551}
552
b8abdf09
JA
553/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
554static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
555 struct ip_vs_conn *cp, int local)
556{
557 int ret = NF_STOLEN;
558
559 skb->ipvs_property = 1;
560 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
561 ip_vs_notrack(skb);
562 else
563 ip_vs_update_conntrack(skb, cp, 1);
71563f34
AG
564
565 /* Remove the early_demux association unless it's bound for the
566 * exact same port and address on this host after translation.
567 */
568 if (!local || cp->vport != cp->dport ||
569 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
570 ip_vs_drop_early_demux_sk(skb);
571
b8abdf09
JA
572 if (!local) {
573 skb_forward_csum(skb);
e3895c03
JA
574 if (!skb->sk)
575 skb_sender_cpu_clear(skb);
7026b1dd
DM
576 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
577 NULL, skb_dst(skb)->dev, dst_output_sk);
b8abdf09
JA
578 } else
579 ret = NF_ACCEPT;
71563f34 580
b8abdf09
JA
581 return ret;
582}
583
584/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */
585static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
586 struct ip_vs_conn *cp, int local)
587{
588 int ret = NF_STOLEN;
589
590 skb->ipvs_property = 1;
591 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
592 ip_vs_notrack(skb);
593 if (!local) {
71563f34 594 ip_vs_drop_early_demux_sk(skb);
b8abdf09 595 skb_forward_csum(skb);
e3895c03
JA
596 if (!skb->sk)
597 skb_sender_cpu_clear(skb);
7026b1dd
DM
598 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
599 NULL, skb_dst(skb)->dev, dst_output_sk);
b8abdf09
JA
600 } else
601 ret = NF_ACCEPT;
602 return ret;
603}
1da177e4
LT
604
605
606/*
607 * NULL transmitter (do nothing except return NF_ACCEPT)
608 */
609int
610ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 611 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1da177e4
LT
612{
613 /* we do not touch skb and do not need pskb ptr */
b8abdf09 614 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1da177e4
LT
615}
616
617
618/*
619 * Bypass transmitter
620 * Let packets bypass the destination when the destination is not
621 * available, it may be only used in transparent cache cluster.
622 */
623int
624ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 625 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1da177e4 626{
eddc9ec5 627 struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
628
629 EnterFunction(10);
630
026ace06 631 rcu_read_lock();
4a4739d5 632 if (__ip_vs_get_out_rt(cp->af, skb, NULL, iph->daddr,
c63e4de2 633 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
1da177e4 634 goto tx_error;
1da177e4 635
4115ded1 636 ip_send_check(iph);
1da177e4
LT
637
638 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 639 skb->ignore_df = 1;
1da177e4 640
b8abdf09 641 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
026ace06 642 rcu_read_unlock();
1da177e4
LT
643
644 LeaveFunction(10);
645 return NF_STOLEN;
646
1da177e4
LT
647 tx_error:
648 kfree_skb(skb);
026ace06 649 rcu_read_unlock();
1da177e4
LT
650 LeaveFunction(10);
651 return NF_STOLEN;
652}
653
b3cdd2a7
JV
654#ifdef CONFIG_IP_VS_IPV6
655int
656ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
4115ded1 657 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
b3cdd2a7 658{
b3cdd2a7
JV
659 EnterFunction(10);
660
026ace06 661 rcu_read_lock();
4a4739d5 662 if (__ip_vs_get_out_rt_v6(cp->af, skb, NULL, &ipvsh->daddr.in6, NULL,
4115ded1 663 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
b3cdd2a7 664 goto tx_error;
b3cdd2a7
JV
665
666 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 667 skb->ignore_df = 1;
b3cdd2a7 668
b8abdf09 669 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
026ace06 670 rcu_read_unlock();
b3cdd2a7
JV
671
672 LeaveFunction(10);
673 return NF_STOLEN;
674
b3cdd2a7
JV
675 tx_error:
676 kfree_skb(skb);
026ace06 677 rcu_read_unlock();
b3cdd2a7
JV
678 LeaveFunction(10);
679 return NF_STOLEN;
680}
681#endif
1da177e4
LT
682
683/*
684 * NAT transmitter (only for outside-to-inside nat forwarding)
685 * Not used for related ICMP
686 */
687int
688ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 689 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1da177e4
LT
690{
691 struct rtable *rt; /* Route to the other host */
4115ded1 692 int local, rc, was_input;
1da177e4
LT
693
694 EnterFunction(10);
695
026ace06 696 rcu_read_lock();
1da177e4
LT
697 /* check if it is a connection of no-client-port */
698 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
014d730d 699 __be16 _pt, *p;
4115ded1
JA
700
701 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
1da177e4
LT
702 if (p == NULL)
703 goto tx_error;
704 ip_vs_conn_fill_cport(cp, *p);
705 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
706 }
707
4115ded1 708 was_input = rt_is_input_route(skb_rtable(skb));
4a4739d5 709 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
4115ded1
JA
710 IP_VS_RT_MODE_LOCAL |
711 IP_VS_RT_MODE_NON_LOCAL |
c63e4de2 712 IP_VS_RT_MODE_RDR, NULL, ipvsh);
4115ded1
JA
713 if (local < 0)
714 goto tx_error;
715 rt = skb_rtable(skb);
fc604767
JA
716 /*
717 * Avoid duplicate tuple in reply direction for NAT traffic
718 * to local address when connection is sync-ed
719 */
c0cd1156 720#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
721 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
722 enum ip_conntrack_info ctinfo;
05b4b065 723 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
fc604767
JA
724
725 if (ct && !nf_ct_is_untracked(ct)) {
0d79641a
JA
726 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
727 "ip_vs_nat_xmit(): "
fc604767 728 "stopping DNAT to local address");
4115ded1 729 goto tx_error;
fc604767
JA
730 }
731 }
732#endif
733
734 /* From world but DNAT to loopback address? */
4115ded1 735 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
0d79641a 736 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
fc604767 737 "stopping DNAT to loopback address");
4115ded1 738 goto tx_error;
1da177e4
LT
739 }
740
741 /* copy-on-write the packet before mangling it */
af1e1cf0 742 if (!skb_make_writable(skb, sizeof(struct iphdr)))
4115ded1 743 goto tx_error;
1da177e4 744
d8d1f30b 745 if (skb_cow(skb, rt->dst.dev->hard_header_len))
4115ded1 746 goto tx_error;
1da177e4 747
1da177e4 748 /* mangle the packet */
d4383f04 749 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
4115ded1 750 goto tx_error;
e7ade46a 751 ip_hdr(skb)->daddr = cp->daddr.ip;
eddc9ec5 752 ip_send_check(ip_hdr(skb));
1da177e4 753
0d79641a 754 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
1da177e4
LT
755
756 /* FIXME: when application helper enlarges the packet and the length
757 is larger than the MTU of outgoing device, there will be still
758 MTU problem. */
759
760 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 761 skb->ignore_df = 1;
1da177e4 762
b8abdf09 763 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
026ace06 764 rcu_read_unlock();
1da177e4
LT
765
766 LeaveFunction(10);
b8abdf09 767 return rc;
1da177e4 768
1da177e4 769 tx_error:
1da177e4 770 kfree_skb(skb);
026ace06 771 rcu_read_unlock();
f4bc17cd 772 LeaveFunction(10);
1da177e4 773 return NF_STOLEN;
1da177e4
LT
774}
775
b3cdd2a7
JV
776#ifdef CONFIG_IP_VS_IPV6
777int
778ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
4115ded1 779 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
b3cdd2a7
JV
780{
781 struct rt6_info *rt; /* Route to the other host */
b8abdf09 782 int local, rc;
b3cdd2a7
JV
783
784 EnterFunction(10);
785
026ace06 786 rcu_read_lock();
b3cdd2a7 787 /* check if it is a connection of no-client-port */
4115ded1 788 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
b3cdd2a7 789 __be16 _pt, *p;
4115ded1 790 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
b3cdd2a7
JV
791 if (p == NULL)
792 goto tx_error;
793 ip_vs_conn_fill_cport(cp, *p);
794 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
795 }
796
4a4739d5
AG
797 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
798 NULL, ipvsh, 0,
4115ded1
JA
799 IP_VS_RT_MODE_LOCAL |
800 IP_VS_RT_MODE_NON_LOCAL |
801 IP_VS_RT_MODE_RDR);
802 if (local < 0)
803 goto tx_error;
804 rt = (struct rt6_info *) skb_dst(skb);
fc604767
JA
805 /*
806 * Avoid duplicate tuple in reply direction for NAT traffic
807 * to local address when connection is sync-ed
808 */
c0cd1156 809#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
810 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
811 enum ip_conntrack_info ctinfo;
05b4b065 812 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
fc604767
JA
813
814 if (ct && !nf_ct_is_untracked(ct)) {
0d79641a 815 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
fc604767
JA
816 "ip_vs_nat_xmit_v6(): "
817 "stopping DNAT to local address");
4115ded1 818 goto tx_error;
fc604767
JA
819 }
820 }
821#endif
822
823 /* From world but DNAT to loopback address? */
824 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
fd0273d7 825 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
0d79641a 826 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
fc604767
JA
827 "ip_vs_nat_xmit_v6(): "
828 "stopping DNAT to loopback address");
4115ded1 829 goto tx_error;
b3cdd2a7
JV
830 }
831
832 /* copy-on-write the packet before mangling it */
833 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
4115ded1 834 goto tx_error;
b3cdd2a7 835
d8d1f30b 836 if (skb_cow(skb, rt->dst.dev->hard_header_len))
4115ded1 837 goto tx_error;
b3cdd2a7 838
b3cdd2a7 839 /* mangle the packet */
4115ded1 840 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
b3cdd2a7 841 goto tx_error;
4e3fd7a0 842 ipv6_hdr(skb)->daddr = cp->daddr.in6;
fc604767 843
0d79641a 844 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
b3cdd2a7
JV
845
846 /* FIXME: when application helper enlarges the packet and the length
847 is larger than the MTU of outgoing device, there will be still
848 MTU problem. */
849
850 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 851 skb->ignore_df = 1;
b3cdd2a7 852
b8abdf09 853 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
026ace06 854 rcu_read_unlock();
b3cdd2a7
JV
855
856 LeaveFunction(10);
b8abdf09 857 return rc;
b3cdd2a7 858
b3cdd2a7
JV
859tx_error:
860 LeaveFunction(10);
861 kfree_skb(skb);
026ace06 862 rcu_read_unlock();
b3cdd2a7 863 return NF_STOLEN;
b3cdd2a7
JV
864}
865#endif
866
8052ba29
AG
867/* When forwarding a packet, we must ensure that we've got enough headroom
868 * for the encapsulation packet in the skb. This also gives us an
869 * opportunity to figure out what the payload_len, dsfield, ttl, and df
870 * values should be, so that we won't need to look at the old ip header
871 * again
872 */
873static struct sk_buff *
874ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
875 unsigned int max_headroom, __u8 *next_protocol,
876 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
877 __be16 *df)
878{
879 struct sk_buff *new_skb = NULL;
880 struct iphdr *old_iph = NULL;
881#ifdef CONFIG_IP_VS_IPV6
882 struct ipv6hdr *old_ipv6h = NULL;
883#endif
884
71563f34
AG
885 ip_vs_drop_early_demux_sk(skb);
886
8052ba29
AG
887 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
888 new_skb = skb_realloc_headroom(skb, max_headroom);
889 if (!new_skb)
890 goto error;
50656d9d
CO
891 if (skb->sk)
892 skb_set_owner_w(new_skb, skb->sk);
8052ba29
AG
893 consume_skb(skb);
894 skb = new_skb;
895 }
896
897#ifdef CONFIG_IP_VS_IPV6
898 if (skb_af == AF_INET6) {
899 old_ipv6h = ipv6_hdr(skb);
900 *next_protocol = IPPROTO_IPV6;
901 if (payload_len)
902 *payload_len =
903 ntohs(old_ipv6h->payload_len) +
904 sizeof(*old_ipv6h);
905 *dsfield = ipv6_get_dsfield(old_ipv6h);
906 *ttl = old_ipv6h->hop_limit;
907 if (df)
908 *df = 0;
909 } else
910#endif
911 {
912 old_iph = ip_hdr(skb);
913 /* Copy DF, reset fragment offset and MF */
914 if (df)
915 *df = (old_iph->frag_off & htons(IP_DF));
916 *next_protocol = IPPROTO_IPIP;
917
918 /* fix old IP header checksum */
919 ip_send_check(old_iph);
920 *dsfield = ipv4_get_dsfield(old_iph);
921 *ttl = old_iph->ttl;
922 if (payload_len)
923 *payload_len = ntohs(old_iph->tot_len);
924 }
925
926 return skb;
927error:
928 kfree_skb(skb);
929 return ERR_PTR(-ENOMEM);
930}
931
932static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
933{
934 if (encaps_af == AF_INET) {
935 if (orig_af == AF_INET)
936 return SKB_GSO_IPIP;
937
938 return SKB_GSO_SIT;
939 }
940
941 /* GSO: we need to provide proper SKB_GSO_ value for IPv6:
942 * SKB_GSO_SIT/IPV6
943 */
944 return 0;
945}
1da177e4
LT
946
947/*
948 * IP Tunneling transmitter
949 *
950 * This function encapsulates the packet in a new IP packet, its
951 * destination will be set to cp->daddr. Most code of this function
952 * is taken from ipip.c.
953 *
954 * It is used in VS/TUN cluster. The load balancer selects a real
955 * server from a cluster based on a scheduling algorithm,
956 * encapsulates the request packet and forwards it to the selected
957 * server. For example, all real servers are configured with
958 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
959 * the encapsulated packet, it will decapsulate the packet, processe
960 * the request and return the response packets directly to the client
961 * without passing the load balancer. This can greatly increase the
962 * scalability of virtual server.
963 *
964 * Used for ANY protocol
965 */
966int
967ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 968 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1da177e4 969{
b6a7719a
HFS
970 struct net *net = skb_net(skb);
971 struct netns_ipvs *ipvs = net_ipvs(net);
1da177e4 972 struct rtable *rt; /* Route to the other host */
c92f5ca2 973 __be32 saddr; /* Source for tunnel */
1da177e4 974 struct net_device *tdev; /* Device to other host */
8052ba29
AG
975 __u8 next_protocol = 0;
976 __u8 dsfield = 0;
977 __u8 ttl = 0;
978 __be16 df = 0;
979 __be16 *dfp = NULL;
1da177e4 980 struct iphdr *iph; /* Our new IP header */
c2636b4d 981 unsigned int max_headroom; /* The extra header space needed */
4115ded1 982 int ret, local;
1da177e4
LT
983
984 EnterFunction(10);
985
026ace06 986 rcu_read_lock();
4a4739d5 987 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
4115ded1
JA
988 IP_VS_RT_MODE_LOCAL |
989 IP_VS_RT_MODE_NON_LOCAL |
990 IP_VS_RT_MODE_CONNECT |
c63e4de2 991 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
4115ded1
JA
992 if (local < 0)
993 goto tx_error;
026ace06
JA
994 if (local) {
995 rcu_read_unlock();
b8abdf09 996 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
026ace06 997 }
1da177e4 998
4115ded1 999 rt = skb_rtable(skb);
d8d1f30b 1000 tdev = rt->dst.dev;
1da177e4 1001
1da177e4
LT
1002 /*
1003 * Okay, now see if we can stuff it in the buffer as-is.
1004 */
1005 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1006
8052ba29
AG
1007 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1008 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1009 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1010 &next_protocol, NULL, &dsfield,
1011 &ttl, dfp);
1012 if (IS_ERR(skb))
1013 goto tx_error;
1da177e4 1014
8052ba29
AG
1015 skb = iptunnel_handle_offloads(
1016 skb, false, __tun_gso_type_mask(AF_INET, cp->af));
ea1d5d77
JA
1017 if (IS_ERR(skb))
1018 goto tx_error;
1019
1020 skb->transport_header = skb->network_header;
1021
e2d1bca7
ACM
1022 skb_push(skb, sizeof(struct iphdr));
1023 skb_reset_network_header(skb);
1da177e4
LT
1024 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1025
1da177e4
LT
1026 /*
1027 * Push down and install the IPIP header.
1028 */
eddc9ec5 1029 iph = ip_hdr(skb);
1da177e4
LT
1030 iph->version = 4;
1031 iph->ihl = sizeof(struct iphdr)>>2;
1032 iph->frag_off = df;
8052ba29
AG
1033 iph->protocol = next_protocol;
1034 iph->tos = dsfield;
c92f5ca2
JA
1035 iph->daddr = cp->daddr.ip;
1036 iph->saddr = saddr;
8052ba29 1037 iph->ttl = ttl;
b6a7719a 1038 ip_select_ident(net, skb, NULL);
1da177e4
LT
1039
1040 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 1041 skb->ignore_df = 1;
1da177e4 1042
b8abdf09 1043 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
f4bc17cd
JA
1044 if (ret == NF_ACCEPT)
1045 ip_local_out(skb);
1046 else if (ret == NF_DROP)
1047 kfree_skb(skb);
026ace06 1048 rcu_read_unlock();
1da177e4
LT
1049
1050 LeaveFunction(10);
1051
1052 return NF_STOLEN;
1053
1da177e4 1054 tx_error:
ea1d5d77
JA
1055 if (!IS_ERR(skb))
1056 kfree_skb(skb);
026ace06 1057 rcu_read_unlock();
1da177e4
LT
1058 LeaveFunction(10);
1059 return NF_STOLEN;
1060}
1061
b3cdd2a7
JV
1062#ifdef CONFIG_IP_VS_IPV6
1063int
1064ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 1065 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
b3cdd2a7
JV
1066{
1067 struct rt6_info *rt; /* Route to the other host */
714f095f 1068 struct in6_addr saddr; /* Source for tunnel */
b3cdd2a7 1069 struct net_device *tdev; /* Device to other host */
8052ba29
AG
1070 __u8 next_protocol = 0;
1071 __u32 payload_len = 0;
1072 __u8 dsfield = 0;
1073 __u8 ttl = 0;
b3cdd2a7
JV
1074 struct ipv6hdr *iph; /* Our new IP header */
1075 unsigned int max_headroom; /* The extra header space needed */
4115ded1 1076 int ret, local;
b3cdd2a7
JV
1077
1078 EnterFunction(10);
1079
026ace06 1080 rcu_read_lock();
4a4739d5 1081 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
4115ded1
JA
1082 &saddr, ipvsh, 1,
1083 IP_VS_RT_MODE_LOCAL |
1084 IP_VS_RT_MODE_NON_LOCAL |
1085 IP_VS_RT_MODE_TUNNEL);
1086 if (local < 0)
1087 goto tx_error;
026ace06
JA
1088 if (local) {
1089 rcu_read_unlock();
b8abdf09 1090 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
026ace06 1091 }
b3cdd2a7 1092
4115ded1 1093 rt = (struct rt6_info *) skb_dst(skb);
d8d1f30b 1094 tdev = rt->dst.dev;
b3cdd2a7 1095
b3cdd2a7
JV
1096 /*
1097 * Okay, now see if we can stuff it in the buffer as-is.
1098 */
1099 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1100
8052ba29
AG
1101 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1102 &next_protocol, &payload_len,
1103 &dsfield, &ttl, NULL);
1104 if (IS_ERR(skb))
1105 goto tx_error;
b3cdd2a7 1106
8052ba29
AG
1107 skb = iptunnel_handle_offloads(
1108 skb, false, __tun_gso_type_mask(AF_INET6, cp->af));
ea1d5d77
JA
1109 if (IS_ERR(skb))
1110 goto tx_error;
1111
714f095f 1112 skb->transport_header = skb->network_header;
b3cdd2a7
JV
1113
1114 skb_push(skb, sizeof(struct ipv6hdr));
1115 skb_reset_network_header(skb);
1116 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1117
b3cdd2a7
JV
1118 /*
1119 * Push down and install the IPIP header.
1120 */
1121 iph = ipv6_hdr(skb);
1122 iph->version = 6;
8052ba29
AG
1123 iph->nexthdr = next_protocol;
1124 iph->payload_len = htons(payload_len);
b3cdd2a7 1125 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
8052ba29 1126 ipv6_change_dsfield(iph, 0, dsfield);
4e3fd7a0
AD
1127 iph->daddr = cp->daddr.in6;
1128 iph->saddr = saddr;
8052ba29 1129 iph->hop_limit = ttl;
b3cdd2a7
JV
1130
1131 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 1132 skb->ignore_df = 1;
b3cdd2a7 1133
b8abdf09 1134 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
f4bc17cd
JA
1135 if (ret == NF_ACCEPT)
1136 ip6_local_out(skb);
1137 else if (ret == NF_DROP)
1138 kfree_skb(skb);
026ace06 1139 rcu_read_unlock();
b3cdd2a7
JV
1140
1141 LeaveFunction(10);
1142
1143 return NF_STOLEN;
1144
b3cdd2a7 1145tx_error:
ea1d5d77
JA
1146 if (!IS_ERR(skb))
1147 kfree_skb(skb);
026ace06 1148 rcu_read_unlock();
b3cdd2a7
JV
1149 LeaveFunction(10);
1150 return NF_STOLEN;
1151}
1152#endif
1153
1da177e4
LT
1154
1155/*
1156 * Direct Routing transmitter
1157 * Used for ANY protocol
1158 */
1159int
1160ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 1161 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1da177e4 1162{
4115ded1 1163 int local;
1da177e4
LT
1164
1165 EnterFunction(10);
1166
026ace06 1167 rcu_read_lock();
4a4739d5 1168 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip,
4115ded1
JA
1169 IP_VS_RT_MODE_LOCAL |
1170 IP_VS_RT_MODE_NON_LOCAL |
c63e4de2 1171 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
4115ded1 1172 if (local < 0)
1da177e4 1173 goto tx_error;
026ace06
JA
1174 if (local) {
1175 rcu_read_unlock();
4115ded1 1176 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
026ace06 1177 }
1da177e4 1178
eddc9ec5 1179 ip_send_check(ip_hdr(skb));
1da177e4 1180
1da177e4 1181 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 1182 skb->ignore_df = 1;
1da177e4 1183
b8abdf09 1184 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
026ace06 1185 rcu_read_unlock();
1da177e4
LT
1186
1187 LeaveFunction(10);
1188 return NF_STOLEN;
1189
1da177e4
LT
1190 tx_error:
1191 kfree_skb(skb);
026ace06 1192 rcu_read_unlock();
1da177e4
LT
1193 LeaveFunction(10);
1194 return NF_STOLEN;
1195}
1196
b3cdd2a7
JV
1197#ifdef CONFIG_IP_VS_IPV6
1198int
1199ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
4115ded1 1200 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
b3cdd2a7 1201{
4115ded1 1202 int local;
b3cdd2a7
JV
1203
1204 EnterFunction(10);
1205
026ace06 1206 rcu_read_lock();
4a4739d5
AG
1207 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1208 NULL, ipvsh, 0,
4115ded1 1209 IP_VS_RT_MODE_LOCAL |
48e8aa6e
MKL
1210 IP_VS_RT_MODE_NON_LOCAL |
1211 IP_VS_RT_MODE_KNOWN_NH);
4115ded1 1212 if (local < 0)
b3cdd2a7 1213 goto tx_error;
026ace06
JA
1214 if (local) {
1215 rcu_read_unlock();
4115ded1 1216 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
026ace06 1217 }
b3cdd2a7
JV
1218
1219 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 1220 skb->ignore_df = 1;
b3cdd2a7 1221
b8abdf09 1222 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
026ace06 1223 rcu_read_unlock();
b3cdd2a7
JV
1224
1225 LeaveFunction(10);
1226 return NF_STOLEN;
1227
b3cdd2a7
JV
1228tx_error:
1229 kfree_skb(skb);
026ace06 1230 rcu_read_unlock();
b3cdd2a7
JV
1231 LeaveFunction(10);
1232 return NF_STOLEN;
1233}
1234#endif
1235
1da177e4
LT
1236
1237/*
1238 * ICMP packet transmitter
1239 * called by the ip_vs_in_icmp
1240 */
1241int
1242ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04
JDB
1243 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1244 struct ip_vs_iphdr *iph)
1da177e4
LT
1245{
1246 struct rtable *rt; /* Route to the other host */
1da177e4 1247 int rc;
fc604767 1248 int local;
4115ded1 1249 int rt_mode, was_input;
1da177e4
LT
1250
1251 EnterFunction(10);
1252
1253 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1254 forwarded directly here, because there is no need to
1255 translate address/port back */
1256 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1257 if (cp->packet_xmit)
d4383f04 1258 rc = cp->packet_xmit(skb, cp, pp, iph);
1da177e4
LT
1259 else
1260 rc = NF_ACCEPT;
1261 /* do not touch skb anymore */
1262 atomic_inc(&cp->in_pkts);
1da177e4
LT
1263 goto out;
1264 }
1265
1266 /*
1267 * mangle and send the packet here (only for VS/NAT)
1268 */
4115ded1 1269 was_input = rt_is_input_route(skb_rtable(skb));
1da177e4 1270
c92f5ca2
JA
1271 /* LOCALNODE from FORWARD hook is not supported */
1272 rt_mode = (hooknum != NF_INET_FORWARD) ?
1273 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1274 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
026ace06 1275 rcu_read_lock();
4a4739d5 1276 local = __ip_vs_get_out_rt(cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
c63e4de2 1277 NULL, iph);
4115ded1
JA
1278 if (local < 0)
1279 goto tx_error;
1280 rt = skb_rtable(skb);
fc604767
JA
1281
1282 /*
1283 * Avoid duplicate tuple in reply direction for NAT traffic
1284 * to local address when connection is sync-ed
1285 */
c0cd1156 1286#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
1287 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1288 enum ip_conntrack_info ctinfo;
05b4b065 1289 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
fc604767
JA
1290
1291 if (ct && !nf_ct_is_untracked(ct)) {
1292 IP_VS_DBG(10, "%s(): "
1293 "stopping DNAT to local address %pI4\n",
1294 __func__, &cp->daddr.ip);
4115ded1 1295 goto tx_error;
fc604767
JA
1296 }
1297 }
1298#endif
1299
1300 /* From world but DNAT to loopback address? */
4115ded1 1301 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
fc604767
JA
1302 IP_VS_DBG(1, "%s(): "
1303 "stopping DNAT to loopback %pI4\n",
1304 __func__, &cp->daddr.ip);
4115ded1 1305 goto tx_error;
1da177e4
LT
1306 }
1307
1308 /* copy-on-write the packet before mangling it */
af1e1cf0 1309 if (!skb_make_writable(skb, offset))
4115ded1 1310 goto tx_error;
1da177e4 1311
d8d1f30b 1312 if (skb_cow(skb, rt->dst.dev->hard_header_len))
4115ded1 1313 goto tx_error;
1da177e4 1314
1da177e4
LT
1315 ip_vs_nat_icmp(skb, pp, cp, 0);
1316
1317 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 1318 skb->ignore_df = 1;
1da177e4 1319
b8abdf09 1320 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
026ace06 1321 rcu_read_unlock();
1da177e4
LT
1322 goto out;
1323
1da177e4 1324 tx_error:
026ace06
JA
1325 kfree_skb(skb);
1326 rcu_read_unlock();
1da177e4
LT
1327 rc = NF_STOLEN;
1328 out:
1329 LeaveFunction(10);
1330 return rc;
1da177e4 1331}
b3cdd2a7
JV
1332
1333#ifdef CONFIG_IP_VS_IPV6
1334int
1335ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
d4383f04 1336 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
4115ded1 1337 struct ip_vs_iphdr *ipvsh)
b3cdd2a7
JV
1338{
1339 struct rt6_info *rt; /* Route to the other host */
b3cdd2a7 1340 int rc;
fc604767 1341 int local;
c92f5ca2 1342 int rt_mode;
b3cdd2a7
JV
1343
1344 EnterFunction(10);
1345
1346 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1347 forwarded directly here, because there is no need to
1348 translate address/port back */
1349 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1350 if (cp->packet_xmit)
4115ded1 1351 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
b3cdd2a7
JV
1352 else
1353 rc = NF_ACCEPT;
1354 /* do not touch skb anymore */
1355 atomic_inc(&cp->in_pkts);
1356 goto out;
1357 }
1358
1359 /*
1360 * mangle and send the packet here (only for VS/NAT)
1361 */
1362
c92f5ca2
JA
1363 /* LOCALNODE from FORWARD hook is not supported */
1364 rt_mode = (hooknum != NF_INET_FORWARD) ?
1365 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1366 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
026ace06 1367 rcu_read_lock();
4a4739d5
AG
1368 local = __ip_vs_get_out_rt_v6(cp->af, skb, cp->dest, &cp->daddr.in6,
1369 NULL, ipvsh, 0, rt_mode);
4115ded1
JA
1370 if (local < 0)
1371 goto tx_error;
1372 rt = (struct rt6_info *) skb_dst(skb);
fc604767
JA
1373 /*
1374 * Avoid duplicate tuple in reply direction for NAT traffic
1375 * to local address when connection is sync-ed
1376 */
c0cd1156 1377#if IS_ENABLED(CONFIG_NF_CONNTRACK)
fc604767
JA
1378 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1379 enum ip_conntrack_info ctinfo;
05b4b065 1380 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
fc604767
JA
1381
1382 if (ct && !nf_ct_is_untracked(ct)) {
1383 IP_VS_DBG(10, "%s(): "
1384 "stopping DNAT to local address %pI6\n",
1385 __func__, &cp->daddr.in6);
4115ded1 1386 goto tx_error;
fc604767
JA
1387 }
1388 }
1389#endif
1390
1391 /* From world but DNAT to loopback address? */
1392 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
fd0273d7 1393 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
fc604767
JA
1394 IP_VS_DBG(1, "%s(): "
1395 "stopping DNAT to loopback %pI6\n",
1396 __func__, &cp->daddr.in6);
4115ded1 1397 goto tx_error;
b3cdd2a7
JV
1398 }
1399
1400 /* copy-on-write the packet before mangling it */
1401 if (!skb_make_writable(skb, offset))
4115ded1 1402 goto tx_error;
b3cdd2a7 1403
d8d1f30b 1404 if (skb_cow(skb, rt->dst.dev->hard_header_len))
4115ded1 1405 goto tx_error;
b3cdd2a7 1406
b3cdd2a7
JV
1407 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1408
1409 /* Another hack: avoid icmp_send in ip_fragment */
60ff7467 1410 skb->ignore_df = 1;
b3cdd2a7 1411
b8abdf09 1412 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
026ace06 1413 rcu_read_unlock();
b3cdd2a7
JV
1414 goto out;
1415
b3cdd2a7 1416tx_error:
026ace06
JA
1417 kfree_skb(skb);
1418 rcu_read_unlock();
b3cdd2a7
JV
1419 rc = NF_STOLEN;
1420out:
1421 LeaveFunction(10);
1422 return rc;
b3cdd2a7
JV
1423}
1424#endif