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