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