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