1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ip_vs_xmit.c: various packet transmitters for IPVS
4 *
5 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
6 * Julian Anastasov <ja@ssi.bg>
7 *
8 * Changes:
9 *
10 * Description of forwarding methods:
11 * - all transmitters are called from LOCAL_IN (remote clients) and
12 * LOCAL_OUT (local clients) but for ICMP can be called from FORWARD
13 * - not all connections have destination server, for example,
14 * connections in backup server when fwmark is used
15 * - bypass connections use daddr from packet
16 * - we can use dst without ref while sending in RCU section, we use
17 * ref when returning NF_ACCEPT for NAT-ed packet via loopback
18 * LOCAL_OUT rules:
19 * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING)
20 * - skb->pkt_type is not set yet
21 * - the only place where we can see skb->sk != NULL
22 */
23
24 #define KMSG_COMPONENT "IPVS"
25 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
26
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/tcp.h> /* for tcphdr */
30 #include <net/ip.h>
31 #include <net/gue.h>
32 #include <net/gre.h>
33 #include <net/tcp.h> /* for csum_tcpudp_magic */
34 #include <net/udp.h>
35 #include <net/icmp.h> /* for icmp_send */
36 #include <net/route.h> /* for ip_route_output */
37 #include <net/ipv6.h>
38 #include <net/ip6_route.h>
39 #include <net/ip_tunnels.h>
40 #include <net/ip6_checksum.h>
41 #include <net/addrconf.h>
42 #include <linux/icmpv6.h>
43 #include <linux/netfilter.h>
44 #include <linux/netfilter_ipv4.h>
45
46 #include <net/ip_vs.h>
47
48 enum {
49 IP_VS_RT_MODE_LOCAL = 1, /* Allow local dest */
50 IP_VS_RT_MODE_NON_LOCAL = 2, /* Allow non-local dest */
51 IP_VS_RT_MODE_RDR = 4, /* Allow redirect from remote daddr to
52 * local
53 */
54 IP_VS_RT_MODE_CONNECT = 8, /* Always bind route to saddr */
55 IP_VS_RT_MODE_KNOWN_NH = 16,/* Route via remote addr */
56 IP_VS_RT_MODE_TUNNEL = 32,/* Tunnel mode */
57 };
58
ip_vs_dest_dst_alloc(void)59 static inline struct ip_vs_dest_dst *ip_vs_dest_dst_alloc(void)
60 {
61 return kmalloc(sizeof(struct ip_vs_dest_dst), GFP_ATOMIC);
62 }
63
ip_vs_dest_dst_free(struct ip_vs_dest_dst * dest_dst)64 static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
65 {
66 kfree(dest_dst);
67 }
68
69 /*
70 * Destination cache to speed up outgoing route lookup
71 */
72 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)73 __ip_vs_dst_set(struct ip_vs_dest *dest, struct ip_vs_dest_dst *dest_dst,
74 struct dst_entry *dst, u32 dst_cookie)
75 {
76 struct ip_vs_dest_dst *old;
77
78 old = rcu_dereference_protected(dest->dest_dst,
79 lockdep_is_held(&dest->dst_lock));
80
81 if (dest_dst) {
82 dest_dst->dst_cache = dst;
83 dest_dst->dst_cookie = dst_cookie;
84 }
85 rcu_assign_pointer(dest->dest_dst, dest_dst);
86
87 if (old)
88 call_rcu(&old->rcu_head, ip_vs_dest_dst_rcu_free);
89 }
90
91 static inline struct ip_vs_dest_dst *
__ip_vs_dst_check(struct ip_vs_dest * dest)92 __ip_vs_dst_check(struct ip_vs_dest *dest)
93 {
94 struct ip_vs_dest_dst *dest_dst = rcu_dereference(dest->dest_dst);
95 struct dst_entry *dst;
96
97 if (!dest_dst)
98 return NULL;
99 dst = dest_dst->dst_cache;
100 if (dst->obsolete &&
101 dst->ops->check(dst, dest_dst->dst_cookie) == NULL)
102 return NULL;
103 return dest_dst;
104 }
105
106 static inline bool
__mtu_check_toobig_v6(const struct sk_buff * skb,u32 mtu)107 __mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
108 {
109 if (IP6CB(skb)->frag_max_size) {
110 /* frag_max_size tell us that, this packet have been
111 * defragmented by netfilter IPv6 conntrack module.
112 */
113 if (IP6CB(skb)->frag_max_size > mtu)
114 return true; /* largest fragment violate MTU */
115 }
116 else if (skb->len > mtu && !skb_is_gso(skb)) {
117 return true; /* Packet size violate MTU size */
118 }
119 return false;
120 }
121
122 /* Get route to daddr, update *saddr, optionally bind route to saddr */
do_output_route4(struct net * net,__be32 daddr,int rt_mode,__be32 * saddr)123 static struct rtable *do_output_route4(struct net *net, __be32 daddr,
124 int rt_mode, __be32 *saddr)
125 {
126 struct flowi4 fl4;
127 struct rtable *rt;
128 bool loop = false;
129
130 memset(&fl4, 0, sizeof(fl4));
131 fl4.daddr = daddr;
132 fl4.flowi4_flags = (rt_mode & IP_VS_RT_MODE_KNOWN_NH) ?
133 FLOWI_FLAG_KNOWN_NH : 0;
134
135 retry:
136 rt = ip_route_output_key(net, &fl4);
137 if (IS_ERR(rt)) {
138 /* Invalid saddr ? */
139 if (PTR_ERR(rt) == -EINVAL && *saddr &&
140 rt_mode & IP_VS_RT_MODE_CONNECT && !loop) {
141 *saddr = 0;
142 flowi4_update_output(&fl4, 0, 0, daddr, 0);
143 goto retry;
144 }
145 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n", &daddr);
146 return NULL;
147 } else if (!*saddr && rt_mode & IP_VS_RT_MODE_CONNECT && fl4.saddr) {
148 ip_rt_put(rt);
149 *saddr = fl4.saddr;
150 flowi4_update_output(&fl4, 0, 0, daddr, fl4.saddr);
151 loop = true;
152 goto retry;
153 }
154 *saddr = fl4.saddr;
155 return rt;
156 }
157
158 #ifdef CONFIG_IP_VS_IPV6
__ip_vs_is_local_route6(struct rt6_info * rt)159 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
160 {
161 return rt->dst.dev && rt->dst.dev->flags & IFF_LOOPBACK;
162 }
163 #endif
164
crosses_local_route_boundary(int skb_af,struct sk_buff * skb,int rt_mode,bool new_rt_is_local)165 static inline bool crosses_local_route_boundary(int skb_af, struct sk_buff *skb,
166 int rt_mode,
167 bool new_rt_is_local)
168 {
169 bool rt_mode_allow_local = !!(rt_mode & IP_VS_RT_MODE_LOCAL);
170 bool rt_mode_allow_non_local = !!(rt_mode & IP_VS_RT_MODE_NON_LOCAL);
171 bool rt_mode_allow_redirect = !!(rt_mode & IP_VS_RT_MODE_RDR);
172 bool source_is_loopback;
173 bool old_rt_is_local;
174
175 #ifdef CONFIG_IP_VS_IPV6
176 if (skb_af == AF_INET6) {
177 int addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
178
179 source_is_loopback =
180 (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
181 (addr_type & IPV6_ADDR_LOOPBACK);
182 old_rt_is_local = __ip_vs_is_local_route6(
183 (struct rt6_info *)skb_dst(skb));
184 } else
185 #endif
186 {
187 source_is_loopback = ipv4_is_loopback(ip_hdr(skb)->saddr);
188 old_rt_is_local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
189 }
190
191 if (unlikely(new_rt_is_local)) {
192 if (!rt_mode_allow_local)
193 return true;
194 if (!rt_mode_allow_redirect && !old_rt_is_local)
195 return true;
196 } else {
197 if (!rt_mode_allow_non_local)
198 return true;
199 if (source_is_loopback)
200 return true;
201 }
202 return false;
203 }
204
maybe_update_pmtu(int skb_af,struct sk_buff * skb,int mtu)205 static inline void maybe_update_pmtu(int skb_af, struct sk_buff *skb, int mtu)
206 {
207 struct sock *sk = skb->sk;
208 struct rtable *ort = skb_rtable(skb);
209
210 if (!skb->dev && sk && sk_fullsock(sk))
211 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu, true);
212 }
213
ensure_mtu_is_adequate(struct netns_ipvs * ipvs,int skb_af,int rt_mode,struct ip_vs_iphdr * ipvsh,struct sk_buff * skb,int mtu)214 static inline bool ensure_mtu_is_adequate(struct netns_ipvs *ipvs, int skb_af,
215 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 = ipvs->net;
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 && !ip_vs_iph_icmp(ipvsh))
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 /* If we're going to tunnel the packet and pmtu discovery
237 * is disabled, we'll just fragment it anyway
238 */
239 if ((rt_mode & IP_VS_RT_MODE_TUNNEL) && !sysctl_pmtu_disc(ipvs))
240 return true;
241
242 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_DF) &&
243 skb->len > mtu && !skb_is_gso(skb) &&
244 !ip_vs_iph_icmp(ipvsh))) {
245 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
246 htonl(mtu));
247 IP_VS_DBG(1, "frag needed for %pI4\n",
248 &ip_hdr(skb)->saddr);
249 return false;
250 }
251 }
252
253 return true;
254 }
255
decrement_ttl(struct netns_ipvs * ipvs,int skb_af,struct sk_buff * skb)256 static inline bool decrement_ttl(struct netns_ipvs *ipvs,
257 int skb_af,
258 struct sk_buff *skb)
259 {
260 struct net *net = ipvs->net;
261
262 #ifdef CONFIG_IP_VS_IPV6
263 if (skb_af == AF_INET6) {
264 struct dst_entry *dst = skb_dst(skb);
265
266 /* check and decrement ttl */
267 if (ipv6_hdr(skb)->hop_limit <= 1) {
268 struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
269
270 /* Force OUTPUT device used as source address */
271 skb->dev = dst->dev;
272 icmpv6_send(skb, ICMPV6_TIME_EXCEED,
273 ICMPV6_EXC_HOPLIMIT, 0);
274 IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
275
276 return false;
277 }
278
279 /* don't propagate ttl change to cloned packets */
280 if (skb_ensure_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_ensure_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
305 /* Get route to destination or remote server */
306 static int
__ip_vs_get_out_rt(struct netns_ipvs * ipvs,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)307 __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
308 struct ip_vs_dest *dest,
309 __be32 daddr, int rt_mode, __be32 *ret_saddr,
310 struct ip_vs_iphdr *ipvsh)
311 {
312 struct net *net = ipvs->net;
313 struct ip_vs_dest_dst *dest_dst;
314 struct rtable *rt; /* Route to the other host */
315 int mtu;
316 int local, noref = 1;
317
318 if (dest) {
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();
324 spin_lock_bh(&dest->dst_lock);
325 if (!dest_dst) {
326 __ip_vs_dst_set(dest, NULL, NULL, 0);
327 spin_unlock_bh(&dest->dst_lock);
328 goto err_unreach;
329 }
330 rt = do_output_route4(net, dest->addr.ip, rt_mode,
331 &dest_dst->dst_saddr.ip);
332 if (!rt) {
333 __ip_vs_dst_set(dest, NULL, NULL, 0);
334 spin_unlock_bh(&dest->dst_lock);
335 ip_vs_dest_dst_free(dest_dst);
336 goto err_unreach;
337 }
338 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
339 spin_unlock_bh(&dest->dst_lock);
340 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
341 &dest->addr.ip, &dest_dst->dst_saddr.ip,
342 atomic_read(&rt->dst.__refcnt));
343 }
344 if (ret_saddr)
345 *ret_saddr = dest_dst->dst_saddr.ip;
346 } else {
347 __be32 saddr = htonl(INADDR_ANY);
348
349 noref = 0;
350
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;
355 rt = do_output_route4(net, daddr, rt_mode, &saddr);
356 if (!rt)
357 goto err_unreach;
358 if (ret_saddr)
359 *ret_saddr = saddr;
360 }
361
362 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
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"
366 " daddr=%pI4\n", &daddr);
367 goto err_put;
368 }
369
370 if (unlikely(local)) {
371 /* skb to local stack, preserve old route */
372 if (!noref)
373 ip_rt_put(rt);
374 return local;
375 }
376
377 if (!decrement_ttl(ipvs, skb_af, skb))
378 goto err_put;
379
380 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
381 mtu = dst_mtu(&rt->dst);
382 } else {
383 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
384 if (!dest)
385 goto err_put;
386 if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
387 mtu -= sizeof(struct udphdr) + sizeof(struct guehdr);
388 if ((dest->tun_flags &
389 IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
390 skb->ip_summed == CHECKSUM_PARTIAL)
391 mtu -= GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
392 } else if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
393 __be16 tflags = 0;
394
395 if (dest->tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
396 tflags |= TUNNEL_CSUM;
397 mtu -= gre_calc_hlen(tflags);
398 }
399 if (mtu < 68) {
400 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
401 goto err_put;
402 }
403 maybe_update_pmtu(skb_af, skb, mtu);
404 }
405
406 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
407 goto err_put;
408
409 skb_dst_drop(skb);
410 if (noref) {
411 if (!local)
412 skb_dst_set_noref(skb, &rt->dst);
413 else
414 skb_dst_set(skb, dst_clone(&rt->dst));
415 } else
416 skb_dst_set(skb, &rt->dst);
417
418 return local;
419
420 err_put:
421 if (!noref)
422 ip_rt_put(rt);
423 return -1;
424
425 err_unreach:
426 dst_link_failure(skb);
427 return -1;
428 }
429
430 #ifdef CONFIG_IP_VS_IPV6
431 static struct dst_entry *
__ip_vs_route_output_v6(struct net * net,struct in6_addr * daddr,struct in6_addr * ret_saddr,int do_xfrm,int rt_mode)432 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
433 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
434 {
435 struct dst_entry *dst;
436 struct flowi6 fl6 = {
437 .daddr = *daddr,
438 };
439
440 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
441 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
442
443 dst = ip6_route_output(net, NULL, &fl6);
444 if (dst->error)
445 goto out_err;
446 if (!ret_saddr)
447 return dst;
448 if (ipv6_addr_any(&fl6.saddr) &&
449 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
450 &fl6.daddr, 0, &fl6.saddr) < 0)
451 goto out_err;
452 if (do_xfrm) {
453 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
454 if (IS_ERR(dst)) {
455 dst = NULL;
456 goto out_err;
457 }
458 }
459 *ret_saddr = fl6.saddr;
460 return dst;
461
462 out_err:
463 dst_release(dst);
464 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
465 return NULL;
466 }
467
468 /*
469 * Get route to destination or remote server
470 */
471 static int
__ip_vs_get_out_rt_v6(struct netns_ipvs * ipvs,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)472 __ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
473 struct ip_vs_dest *dest,
474 struct in6_addr *daddr, struct in6_addr *ret_saddr,
475 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
476 {
477 struct net *net = ipvs->net;
478 struct ip_vs_dest_dst *dest_dst;
479 struct rt6_info *rt; /* Route to the other host */
480 struct dst_entry *dst;
481 int mtu;
482 int local, noref = 1;
483
484 if (dest) {
485 dest_dst = __ip_vs_dst_check(dest);
486 if (likely(dest_dst))
487 rt = (struct rt6_info *) dest_dst->dst_cache;
488 else {
489 u32 cookie;
490
491 dest_dst = ip_vs_dest_dst_alloc();
492 spin_lock_bh(&dest->dst_lock);
493 if (!dest_dst) {
494 __ip_vs_dst_set(dest, NULL, NULL, 0);
495 spin_unlock_bh(&dest->dst_lock);
496 goto err_unreach;
497 }
498 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
499 &dest_dst->dst_saddr.in6,
500 do_xfrm, rt_mode);
501 if (!dst) {
502 __ip_vs_dst_set(dest, NULL, NULL, 0);
503 spin_unlock_bh(&dest->dst_lock);
504 ip_vs_dest_dst_free(dest_dst);
505 goto err_unreach;
506 }
507 rt = (struct rt6_info *) dst;
508 cookie = rt6_get_cookie(rt);
509 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
510 spin_unlock_bh(&dest->dst_lock);
511 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
512 &dest->addr.in6, &dest_dst->dst_saddr.in6,
513 atomic_read(&rt->dst.__refcnt));
514 }
515 if (ret_saddr)
516 *ret_saddr = dest_dst->dst_saddr.in6;
517 } else {
518 noref = 0;
519 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
520 rt_mode);
521 if (!dst)
522 goto err_unreach;
523 rt = (struct rt6_info *) dst;
524 }
525
526 local = __ip_vs_is_local_route6(rt);
527
528 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
529 local))) {
530 IP_VS_DBG_RL("We are crossing local and non-local addresses"
531 " daddr=%pI6\n", daddr);
532 goto err_put;
533 }
534
535 if (unlikely(local)) {
536 /* skb to local stack, preserve old route */
537 if (!noref)
538 dst_release(&rt->dst);
539 return local;
540 }
541
542 if (!decrement_ttl(ipvs, skb_af, skb))
543 goto err_put;
544
545 /* MTU checking */
546 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
547 mtu = dst_mtu(&rt->dst);
548 else {
549 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
550 if (!dest)
551 goto err_put;
552 if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
553 mtu -= sizeof(struct udphdr) + sizeof(struct guehdr);
554 if ((dest->tun_flags &
555 IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
556 skb->ip_summed == CHECKSUM_PARTIAL)
557 mtu -= GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
558 } else if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
559 __be16 tflags = 0;
560
561 if (dest->tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
562 tflags |= TUNNEL_CSUM;
563 mtu -= gre_calc_hlen(tflags);
564 }
565 if (mtu < IPV6_MIN_MTU) {
566 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
567 IPV6_MIN_MTU);
568 goto err_put;
569 }
570 maybe_update_pmtu(skb_af, skb, mtu);
571 }
572
573 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
574 goto err_put;
575
576 skb_dst_drop(skb);
577 if (noref) {
578 if (!local)
579 skb_dst_set_noref(skb, &rt->dst);
580 else
581 skb_dst_set(skb, dst_clone(&rt->dst));
582 } else
583 skb_dst_set(skb, &rt->dst);
584
585 return local;
586
587 err_put:
588 if (!noref)
589 dst_release(&rt->dst);
590 return -1;
591
592 err_unreach:
593 /* The ip6_link_failure function requires the dev field to be set
594 * in order to get the net (further for the sake of fwmark
595 * reflection).
596 */
597 if (!skb->dev)
598 skb->dev = skb_dst(skb)->dev;
599
600 dst_link_failure(skb);
601 return -1;
602 }
603 #endif
604
605
606 /* 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)607 static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
608 struct ip_vs_conn *cp)
609 {
610 int ret = NF_ACCEPT;
611
612 skb->ipvs_property = 1;
613 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
614 ret = ip_vs_confirm_conntrack(skb);
615 if (ret == NF_ACCEPT) {
616 nf_reset_ct(skb);
617 skb_forward_csum(skb);
618 if (skb->dev)
619 skb->tstamp = 0;
620 }
621 return ret;
622 }
623
624 /* In the event of a remote destination, it's possible that we would have
625 * matches against an old socket (particularly a TIME-WAIT socket). This
626 * causes havoc down the line (ip_local_out et. al. expect regular sockets
627 * and invalid memory accesses will happen) so simply drop the association
628 * in this case.
629 */
ip_vs_drop_early_demux_sk(struct sk_buff * skb)630 static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
631 {
632 /* If dev is set, the packet came from the LOCAL_IN callback and
633 * not from a local TCP socket.
634 */
635 if (skb->dev)
636 skb_orphan(skb);
637 }
638
639 /* 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)640 static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
641 struct ip_vs_conn *cp, int local)
642 {
643 int ret = NF_STOLEN;
644
645 skb->ipvs_property = 1;
646 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
647 ip_vs_notrack(skb);
648 else
649 ip_vs_update_conntrack(skb, cp, 1);
650
651 /* Remove the early_demux association unless it's bound for the
652 * exact same port and address on this host after translation.
653 */
654 if (!local || cp->vport != cp->dport ||
655 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
656 ip_vs_drop_early_demux_sk(skb);
657
658 if (!local) {
659 skb_forward_csum(skb);
660 if (skb->dev)
661 skb->tstamp = 0;
662 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
663 NULL, skb_dst(skb)->dev, dst_output);
664 } else
665 ret = NF_ACCEPT;
666
667 return ret;
668 }
669
670 /* 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)671 static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
672 struct ip_vs_conn *cp, int local)
673 {
674 int ret = NF_STOLEN;
675
676 skb->ipvs_property = 1;
677 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
678 ip_vs_notrack(skb);
679 if (!local) {
680 ip_vs_drop_early_demux_sk(skb);
681 skb_forward_csum(skb);
682 if (skb->dev)
683 skb->tstamp = 0;
684 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
685 NULL, skb_dst(skb)->dev, dst_output);
686 } else
687 ret = NF_ACCEPT;
688 return ret;
689 }
690
691
692 /*
693 * NULL transmitter (do nothing except return NF_ACCEPT)
694 */
695 int
ip_vs_null_xmit(struct sk_buff * skb,struct ip_vs_conn * cp,struct ip_vs_protocol * pp,struct ip_vs_iphdr * ipvsh)696 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
697 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
698 {
699 /* we do not touch skb and do not need pskb ptr */
700 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
701 }
702
703
704 /*
705 * Bypass transmitter
706 * Let packets bypass the destination when the destination is not
707 * available, it may be only used in transparent cache cluster.
708 */
709 int
ip_vs_bypass_xmit(struct sk_buff * skb,struct ip_vs_conn * cp,struct ip_vs_protocol * pp,struct ip_vs_iphdr * ipvsh)710 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
711 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
712 {
713 struct iphdr *iph = ip_hdr(skb);
714
715 EnterFunction(10);
716
717 if (__ip_vs_get_out_rt(cp->ipvs, cp->af, skb, NULL, iph->daddr,
718 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
719 goto tx_error;
720
721 ip_send_check(iph);
722
723 /* Another hack: avoid icmp_send in ip_fragment */
724 skb->ignore_df = 1;
725
726 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
727
728 LeaveFunction(10);
729 return NF_STOLEN;
730
731 tx_error:
732 kfree_skb(skb);
733 LeaveFunction(10);
734 return NF_STOLEN;
735 }
736
737 #ifdef CONFIG_IP_VS_IPV6
738 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)739 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
740 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
741 {
742 struct ipv6hdr *iph = ipv6_hdr(skb);
743
744 EnterFunction(10);
745
746 if (__ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, NULL,
747 &iph->daddr, NULL,
748 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
749 goto tx_error;
750
751 /* Another hack: avoid icmp_send in ip_fragment */
752 skb->ignore_df = 1;
753
754 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
755
756 LeaveFunction(10);
757 return NF_STOLEN;
758
759 tx_error:
760 kfree_skb(skb);
761 LeaveFunction(10);
762 return NF_STOLEN;
763 }
764 #endif
765
766 /*
767 * NAT transmitter (only for outside-to-inside nat forwarding)
768 * Not used for related ICMP
769 */
770 int
ip_vs_nat_xmit(struct sk_buff * skb,struct ip_vs_conn * cp,struct ip_vs_protocol * pp,struct ip_vs_iphdr * ipvsh)771 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
772 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
773 {
774 struct rtable *rt; /* Route to the other host */
775 int local, rc, was_input;
776
777 EnterFunction(10);
778
779 /* check if it is a connection of no-client-port */
780 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
781 __be16 _pt, *p;
782
783 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
784 if (p == NULL)
785 goto tx_error;
786 ip_vs_conn_fill_cport(cp, *p);
787 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
788 }
789
790 was_input = rt_is_input_route(skb_rtable(skb));
791 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
792 IP_VS_RT_MODE_LOCAL |
793 IP_VS_RT_MODE_NON_LOCAL |
794 IP_VS_RT_MODE_RDR, NULL, ipvsh);
795 if (local < 0)
796 goto tx_error;
797 rt = skb_rtable(skb);
798 /*
799 * Avoid duplicate tuple in reply direction for NAT traffic
800 * to local address when connection is sync-ed
801 */
802 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
803 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
804 enum ip_conntrack_info ctinfo;
805 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
806
807 if (ct) {
808 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
809 "ip_vs_nat_xmit(): "
810 "stopping DNAT to local address");
811 goto tx_error;
812 }
813 }
814 #endif
815
816 /* From world but DNAT to loopback address? */
817 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
818 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
819 "ip_vs_nat_xmit(): stopping DNAT to loopback "
820 "address");
821 goto tx_error;
822 }
823
824 /* copy-on-write the packet before mangling it */
825 if (skb_ensure_writable(skb, sizeof(struct iphdr)))
826 goto tx_error;
827
828 if (skb_cow(skb, rt->dst.dev->hard_header_len))
829 goto tx_error;
830
831 /* mangle the packet */
832 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
833 goto tx_error;
834 ip_hdr(skb)->daddr = cp->daddr.ip;
835 ip_send_check(ip_hdr(skb));
836
837 IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
838
839 /* FIXME: when application helper enlarges the packet and the length
840 is larger than the MTU of outgoing device, there will be still
841 MTU problem. */
842
843 /* Another hack: avoid icmp_send in ip_fragment */
844 skb->ignore_df = 1;
845
846 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
847
848 LeaveFunction(10);
849 return rc;
850
851 tx_error:
852 kfree_skb(skb);
853 LeaveFunction(10);
854 return NF_STOLEN;
855 }
856
857 #ifdef CONFIG_IP_VS_IPV6
858 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)859 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
860 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
861 {
862 struct rt6_info *rt; /* Route to the other host */
863 int local, rc;
864
865 EnterFunction(10);
866
867 /* check if it is a connection of no-client-port */
868 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
869 __be16 _pt, *p;
870 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
871 if (p == NULL)
872 goto tx_error;
873 ip_vs_conn_fill_cport(cp, *p);
874 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
875 }
876
877 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
878 &cp->daddr.in6,
879 NULL, ipvsh, 0,
880 IP_VS_RT_MODE_LOCAL |
881 IP_VS_RT_MODE_NON_LOCAL |
882 IP_VS_RT_MODE_RDR);
883 if (local < 0)
884 goto tx_error;
885 rt = (struct rt6_info *) skb_dst(skb);
886 /*
887 * Avoid duplicate tuple in reply direction for NAT traffic
888 * to local address when connection is sync-ed
889 */
890 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
891 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
892 enum ip_conntrack_info ctinfo;
893 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
894
895 if (ct) {
896 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
897 "ip_vs_nat_xmit_v6(): "
898 "stopping DNAT to local address");
899 goto tx_error;
900 }
901 }
902 #endif
903
904 /* From world but DNAT to loopback address? */
905 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
906 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
907 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
908 "ip_vs_nat_xmit_v6(): "
909 "stopping DNAT to loopback address");
910 goto tx_error;
911 }
912
913 /* copy-on-write the packet before mangling it */
914 if (skb_ensure_writable(skb, sizeof(struct ipv6hdr)))
915 goto tx_error;
916
917 if (skb_cow(skb, rt->dst.dev->hard_header_len))
918 goto tx_error;
919
920 /* mangle the packet */
921 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
922 goto tx_error;
923 ipv6_hdr(skb)->daddr = cp->daddr.in6;
924
925 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
926
927 /* FIXME: when application helper enlarges the packet and the length
928 is larger than the MTU of outgoing device, there will be still
929 MTU problem. */
930
931 /* Another hack: avoid icmp_send in ip_fragment */
932 skb->ignore_df = 1;
933
934 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
935
936 LeaveFunction(10);
937 return rc;
938
939 tx_error:
940 LeaveFunction(10);
941 kfree_skb(skb);
942 return NF_STOLEN;
943 }
944 #endif
945
946 /* When forwarding a packet, we must ensure that we've got enough headroom
947 * for the encapsulation packet in the skb. This also gives us an
948 * opportunity to figure out what the payload_len, dsfield, ttl, and df
949 * values should be, so that we won't need to look at the old ip header
950 * again
951 */
952 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)953 ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
954 unsigned int max_headroom, __u8 *next_protocol,
955 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
956 __be16 *df)
957 {
958 struct sk_buff *new_skb = NULL;
959 struct iphdr *old_iph = NULL;
960 __u8 old_dsfield;
961 #ifdef CONFIG_IP_VS_IPV6
962 struct ipv6hdr *old_ipv6h = NULL;
963 #endif
964
965 ip_vs_drop_early_demux_sk(skb);
966
967 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
968 new_skb = skb_realloc_headroom(skb, max_headroom);
969 if (!new_skb)
970 goto error;
971 if (skb->sk)
972 skb_set_owner_w(new_skb, skb->sk);
973 consume_skb(skb);
974 skb = new_skb;
975 }
976
977 #ifdef CONFIG_IP_VS_IPV6
978 if (skb_af == AF_INET6) {
979 old_ipv6h = ipv6_hdr(skb);
980 *next_protocol = IPPROTO_IPV6;
981 if (payload_len)
982 *payload_len =
983 ntohs(old_ipv6h->payload_len) +
984 sizeof(*old_ipv6h);
985 old_dsfield = ipv6_get_dsfield(old_ipv6h);
986 *ttl = old_ipv6h->hop_limit;
987 if (df)
988 *df = 0;
989 } else
990 #endif
991 {
992 old_iph = ip_hdr(skb);
993 /* Copy DF, reset fragment offset and MF */
994 if (df)
995 *df = (old_iph->frag_off & htons(IP_DF));
996 *next_protocol = IPPROTO_IPIP;
997
998 /* fix old IP header checksum */
999 ip_send_check(old_iph);
1000 old_dsfield = ipv4_get_dsfield(old_iph);
1001 *ttl = old_iph->ttl;
1002 if (payload_len)
1003 *payload_len = ntohs(old_iph->tot_len);
1004 }
1005
1006 /* Implement full-functionality option for ECN encapsulation */
1007 *dsfield = INET_ECN_encapsulate(old_dsfield, old_dsfield);
1008
1009 return skb;
1010 error:
1011 kfree_skb(skb);
1012 return ERR_PTR(-ENOMEM);
1013 }
1014
__tun_gso_type_mask(int encaps_af,int orig_af)1015 static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
1016 {
1017 switch (encaps_af) {
1018 case AF_INET:
1019 return SKB_GSO_IPXIP4;
1020 case AF_INET6:
1021 return SKB_GSO_IPXIP6;
1022 default:
1023 return 0;
1024 }
1025 }
1026
1027 static int
ipvs_gue_encap(struct net * net,struct sk_buff * skb,struct ip_vs_conn * cp,__u8 * next_protocol)1028 ipvs_gue_encap(struct net *net, struct sk_buff *skb,
1029 struct ip_vs_conn *cp, __u8 *next_protocol)
1030 {
1031 __be16 dport;
1032 __be16 sport = udp_flow_src_port(net, skb, 0, 0, false);
1033 struct udphdr *udph; /* Our new UDP header */
1034 struct guehdr *gueh; /* Our new GUE header */
1035 size_t hdrlen, optlen = 0;
1036 void *data;
1037 bool need_priv = false;
1038
1039 if ((cp->dest->tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1040 skb->ip_summed == CHECKSUM_PARTIAL) {
1041 optlen += GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
1042 need_priv = true;
1043 }
1044
1045 hdrlen = sizeof(struct guehdr) + optlen;
1046
1047 skb_push(skb, hdrlen);
1048
1049 gueh = (struct guehdr *)skb->data;
1050
1051 gueh->control = 0;
1052 gueh->version = 0;
1053 gueh->hlen = optlen >> 2;
1054 gueh->flags = 0;
1055 gueh->proto_ctype = *next_protocol;
1056
1057 data = &gueh[1];
1058
1059 if (need_priv) {
1060 __be32 *flags = data;
1061 u16 csum_start = skb_checksum_start_offset(skb);
1062 __be16 *pd;
1063
1064 gueh->flags |= GUE_FLAG_PRIV;
1065 *flags = 0;
1066 data += GUE_LEN_PRIV;
1067
1068 if (csum_start < hdrlen)
1069 return -EINVAL;
1070
1071 csum_start -= hdrlen;
1072 pd = data;
1073 pd[0] = htons(csum_start);
1074 pd[1] = htons(csum_start + skb->csum_offset);
1075
1076 if (!skb_is_gso(skb)) {
1077 skb->ip_summed = CHECKSUM_NONE;
1078 skb->encapsulation = 0;
1079 }
1080
1081 *flags |= GUE_PFLAG_REMCSUM;
1082 data += GUE_PLEN_REMCSUM;
1083 }
1084
1085 skb_push(skb, sizeof(struct udphdr));
1086 skb_reset_transport_header(skb);
1087
1088 udph = udp_hdr(skb);
1089
1090 dport = cp->dest->tun_port;
1091 udph->dest = dport;
1092 udph->source = sport;
1093 udph->len = htons(skb->len);
1094 udph->check = 0;
1095
1096 *next_protocol = IPPROTO_UDP;
1097
1098 return 0;
1099 }
1100
1101 static void
ipvs_gre_encap(struct net * net,struct sk_buff * skb,struct ip_vs_conn * cp,__u8 * next_protocol)1102 ipvs_gre_encap(struct net *net, struct sk_buff *skb,
1103 struct ip_vs_conn *cp, __u8 *next_protocol)
1104 {
1105 __be16 proto = *next_protocol == IPPROTO_IPIP ?
1106 htons(ETH_P_IP) : htons(ETH_P_IPV6);
1107 __be16 tflags = 0;
1108 size_t hdrlen;
1109
1110 if (cp->dest->tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
1111 tflags |= TUNNEL_CSUM;
1112
1113 hdrlen = gre_calc_hlen(tflags);
1114 gre_build_header(skb, hdrlen, tflags, proto, 0, 0);
1115
1116 *next_protocol = IPPROTO_GRE;
1117 }
1118
1119 /*
1120 * IP Tunneling transmitter
1121 *
1122 * This function encapsulates the packet in a new IP packet, its
1123 * destination will be set to cp->daddr. Most code of this function
1124 * is taken from ipip.c.
1125 *
1126 * It is used in VS/TUN cluster. The load balancer selects a real
1127 * server from a cluster based on a scheduling algorithm,
1128 * encapsulates the request packet and forwards it to the selected
1129 * server. For example, all real servers are configured with
1130 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
1131 * the encapsulated packet, it will decapsulate the packet, processe
1132 * the request and return the response packets directly to the client
1133 * without passing the load balancer. This can greatly increase the
1134 * scalability of virtual server.
1135 *
1136 * Used for ANY protocol
1137 */
1138 int
ip_vs_tunnel_xmit(struct sk_buff * skb,struct ip_vs_conn * cp,struct ip_vs_protocol * pp,struct ip_vs_iphdr * ipvsh)1139 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1140 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1141 {
1142 struct netns_ipvs *ipvs = cp->ipvs;
1143 struct net *net = ipvs->net;
1144 struct rtable *rt; /* Route to the other host */
1145 __be32 saddr; /* Source for tunnel */
1146 struct net_device *tdev; /* Device to other host */
1147 __u8 next_protocol = 0;
1148 __u8 dsfield = 0;
1149 __u8 ttl = 0;
1150 __be16 df = 0;
1151 __be16 *dfp = NULL;
1152 struct iphdr *iph; /* Our new IP header */
1153 unsigned int max_headroom; /* The extra header space needed */
1154 int ret, local;
1155 int tun_type, gso_type;
1156 int tun_flags;
1157
1158 EnterFunction(10);
1159
1160 local = __ip_vs_get_out_rt(ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1161 IP_VS_RT_MODE_LOCAL |
1162 IP_VS_RT_MODE_NON_LOCAL |
1163 IP_VS_RT_MODE_CONNECT |
1164 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
1165 if (local < 0)
1166 goto tx_error;
1167 if (local)
1168 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1169
1170 rt = skb_rtable(skb);
1171 tdev = rt->dst.dev;
1172
1173 /*
1174 * Okay, now see if we can stuff it in the buffer as-is.
1175 */
1176 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1177
1178 tun_type = cp->dest->tun_type;
1179 tun_flags = cp->dest->tun_flags;
1180
1181 if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1182 size_t gue_hdrlen, gue_optlen = 0;
1183
1184 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1185 skb->ip_summed == CHECKSUM_PARTIAL) {
1186 gue_optlen += GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
1187 }
1188 gue_hdrlen = sizeof(struct guehdr) + gue_optlen;
1189
1190 max_headroom += sizeof(struct udphdr) + gue_hdrlen;
1191 } else if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
1192 size_t gre_hdrlen;
1193 __be16 tflags = 0;
1194
1195 if (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
1196 tflags |= TUNNEL_CSUM;
1197 gre_hdrlen = gre_calc_hlen(tflags);
1198
1199 max_headroom += gre_hdrlen;
1200 }
1201
1202 /* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
1203 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1204 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1205 &next_protocol, NULL, &dsfield,
1206 &ttl, dfp);
1207 if (IS_ERR(skb))
1208 goto tx_error;
1209
1210 gso_type = __tun_gso_type_mask(AF_INET, cp->af);
1211 if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1212 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1213 (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1214 gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1215 else
1216 gso_type |= SKB_GSO_UDP_TUNNEL;
1217 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1218 skb->ip_summed == CHECKSUM_PARTIAL) {
1219 gso_type |= SKB_GSO_TUNNEL_REMCSUM;
1220 }
1221 } else if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
1222 if (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
1223 gso_type |= SKB_GSO_GRE_CSUM;
1224 else
1225 gso_type |= SKB_GSO_GRE;
1226 }
1227
1228 if (iptunnel_handle_offloads(skb, gso_type))
1229 goto tx_error;
1230
1231 skb->transport_header = skb->network_header;
1232
1233 skb_set_inner_ipproto(skb, next_protocol);
1234 skb_set_inner_mac_header(skb, skb_inner_network_offset(skb));
1235
1236 if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1237 bool check = false;
1238
1239 if (ipvs_gue_encap(net, skb, cp, &next_protocol))
1240 goto tx_error;
1241
1242 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1243 (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1244 check = true;
1245
1246 udp_set_csum(!check, skb, saddr, cp->daddr.ip, skb->len);
1247 } else if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE)
1248 ipvs_gre_encap(net, skb, cp, &next_protocol);
1249
1250 skb_push(skb, sizeof(struct iphdr));
1251 skb_reset_network_header(skb);
1252 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1253
1254 /*
1255 * Push down and install the IPIP header.
1256 */
1257 iph = ip_hdr(skb);
1258 iph->version = 4;
1259 iph->ihl = sizeof(struct iphdr)>>2;
1260 iph->frag_off = df;
1261 iph->protocol = next_protocol;
1262 iph->tos = dsfield;
1263 iph->daddr = cp->daddr.ip;
1264 iph->saddr = saddr;
1265 iph->ttl = ttl;
1266 ip_select_ident(net, skb, NULL);
1267
1268 /* Another hack: avoid icmp_send in ip_fragment */
1269 skb->ignore_df = 1;
1270
1271 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1272 if (ret == NF_ACCEPT)
1273 ip_local_out(net, skb->sk, skb);
1274 else if (ret == NF_DROP)
1275 kfree_skb(skb);
1276
1277 LeaveFunction(10);
1278
1279 return NF_STOLEN;
1280
1281 tx_error:
1282 if (!IS_ERR(skb))
1283 kfree_skb(skb);
1284 LeaveFunction(10);
1285 return NF_STOLEN;
1286 }
1287
1288 #ifdef CONFIG_IP_VS_IPV6
1289 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)1290 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1291 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1292 {
1293 struct netns_ipvs *ipvs = cp->ipvs;
1294 struct net *net = ipvs->net;
1295 struct rt6_info *rt; /* Route to the other host */
1296 struct in6_addr saddr; /* Source for tunnel */
1297 struct net_device *tdev; /* Device to other host */
1298 __u8 next_protocol = 0;
1299 __u32 payload_len = 0;
1300 __u8 dsfield = 0;
1301 __u8 ttl = 0;
1302 struct ipv6hdr *iph; /* Our new IP header */
1303 unsigned int max_headroom; /* The extra header space needed */
1304 int ret, local;
1305 int tun_type, gso_type;
1306 int tun_flags;
1307
1308 EnterFunction(10);
1309
1310 local = __ip_vs_get_out_rt_v6(ipvs, cp->af, skb, cp->dest,
1311 &cp->daddr.in6,
1312 &saddr, ipvsh, 1,
1313 IP_VS_RT_MODE_LOCAL |
1314 IP_VS_RT_MODE_NON_LOCAL |
1315 IP_VS_RT_MODE_TUNNEL);
1316 if (local < 0)
1317 goto tx_error;
1318 if (local)
1319 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1320
1321 rt = (struct rt6_info *) skb_dst(skb);
1322 tdev = rt->dst.dev;
1323
1324 /*
1325 * Okay, now see if we can stuff it in the buffer as-is.
1326 */
1327 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1328
1329 tun_type = cp->dest->tun_type;
1330 tun_flags = cp->dest->tun_flags;
1331
1332 if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1333 size_t gue_hdrlen, gue_optlen = 0;
1334
1335 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1336 skb->ip_summed == CHECKSUM_PARTIAL) {
1337 gue_optlen += GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
1338 }
1339 gue_hdrlen = sizeof(struct guehdr) + gue_optlen;
1340
1341 max_headroom += sizeof(struct udphdr) + gue_hdrlen;
1342 } else if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
1343 size_t gre_hdrlen;
1344 __be16 tflags = 0;
1345
1346 if (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
1347 tflags |= TUNNEL_CSUM;
1348 gre_hdrlen = gre_calc_hlen(tflags);
1349
1350 max_headroom += gre_hdrlen;
1351 }
1352
1353 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1354 &next_protocol, &payload_len,
1355 &dsfield, &ttl, NULL);
1356 if (IS_ERR(skb))
1357 goto tx_error;
1358
1359 gso_type = __tun_gso_type_mask(AF_INET6, cp->af);
1360 if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1361 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1362 (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1363 gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1364 else
1365 gso_type |= SKB_GSO_UDP_TUNNEL;
1366 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1367 skb->ip_summed == CHECKSUM_PARTIAL) {
1368 gso_type |= SKB_GSO_TUNNEL_REMCSUM;
1369 }
1370 } else if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE) {
1371 if (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM)
1372 gso_type |= SKB_GSO_GRE_CSUM;
1373 else
1374 gso_type |= SKB_GSO_GRE;
1375 }
1376
1377 if (iptunnel_handle_offloads(skb, gso_type))
1378 goto tx_error;
1379
1380 skb->transport_header = skb->network_header;
1381
1382 skb_set_inner_ipproto(skb, next_protocol);
1383 skb_set_inner_mac_header(skb, skb_inner_network_offset(skb));
1384
1385 if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1386 bool check = false;
1387
1388 if (ipvs_gue_encap(net, skb, cp, &next_protocol))
1389 goto tx_error;
1390
1391 if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1392 (tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1393 check = true;
1394
1395 udp6_set_csum(!check, skb, &saddr, &cp->daddr.in6, skb->len);
1396 } else if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GRE)
1397 ipvs_gre_encap(net, skb, cp, &next_protocol);
1398
1399 skb_push(skb, sizeof(struct ipv6hdr));
1400 skb_reset_network_header(skb);
1401 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1402
1403 /*
1404 * Push down and install the IPIP header.
1405 */
1406 iph = ipv6_hdr(skb);
1407 iph->version = 6;
1408 iph->nexthdr = next_protocol;
1409 iph->payload_len = htons(payload_len);
1410 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1411 ipv6_change_dsfield(iph, 0, dsfield);
1412 iph->daddr = cp->daddr.in6;
1413 iph->saddr = saddr;
1414 iph->hop_limit = ttl;
1415
1416 /* Another hack: avoid icmp_send in ip_fragment */
1417 skb->ignore_df = 1;
1418
1419 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1420 if (ret == NF_ACCEPT)
1421 ip6_local_out(net, skb->sk, skb);
1422 else if (ret == NF_DROP)
1423 kfree_skb(skb);
1424
1425 LeaveFunction(10);
1426
1427 return NF_STOLEN;
1428
1429 tx_error:
1430 if (!IS_ERR(skb))
1431 kfree_skb(skb);
1432 LeaveFunction(10);
1433 return NF_STOLEN;
1434 }
1435 #endif
1436
1437
1438 /*
1439 * Direct Routing transmitter
1440 * Used for ANY protocol
1441 */
1442 int
ip_vs_dr_xmit(struct sk_buff * skb,struct ip_vs_conn * cp,struct ip_vs_protocol * pp,struct ip_vs_iphdr * ipvsh)1443 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1444 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1445 {
1446 int local;
1447
1448 EnterFunction(10);
1449
1450 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1451 IP_VS_RT_MODE_LOCAL |
1452 IP_VS_RT_MODE_NON_LOCAL |
1453 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1454 if (local < 0)
1455 goto tx_error;
1456 if (local)
1457 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1458
1459 ip_send_check(ip_hdr(skb));
1460
1461 /* Another hack: avoid icmp_send in ip_fragment */
1462 skb->ignore_df = 1;
1463
1464 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1465
1466 LeaveFunction(10);
1467 return NF_STOLEN;
1468
1469 tx_error:
1470 kfree_skb(skb);
1471 LeaveFunction(10);
1472 return NF_STOLEN;
1473 }
1474
1475 #ifdef CONFIG_IP_VS_IPV6
1476 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)1477 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1478 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1479 {
1480 int local;
1481
1482 EnterFunction(10);
1483
1484 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1485 &cp->daddr.in6,
1486 NULL, ipvsh, 0,
1487 IP_VS_RT_MODE_LOCAL |
1488 IP_VS_RT_MODE_NON_LOCAL |
1489 IP_VS_RT_MODE_KNOWN_NH);
1490 if (local < 0)
1491 goto tx_error;
1492 if (local)
1493 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1494
1495 /* Another hack: avoid icmp_send in ip_fragment */
1496 skb->ignore_df = 1;
1497
1498 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1499
1500 LeaveFunction(10);
1501 return NF_STOLEN;
1502
1503 tx_error:
1504 kfree_skb(skb);
1505 LeaveFunction(10);
1506 return NF_STOLEN;
1507 }
1508 #endif
1509
1510
1511 /*
1512 * ICMP packet transmitter
1513 * called by the ip_vs_in_icmp
1514 */
1515 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)1516 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1517 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1518 struct ip_vs_iphdr *iph)
1519 {
1520 struct rtable *rt; /* Route to the other host */
1521 int rc;
1522 int local;
1523 int rt_mode, was_input;
1524
1525 EnterFunction(10);
1526
1527 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1528 forwarded directly here, because there is no need to
1529 translate address/port back */
1530 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1531 if (cp->packet_xmit)
1532 rc = cp->packet_xmit(skb, cp, pp, iph);
1533 else
1534 rc = NF_ACCEPT;
1535 /* do not touch skb anymore */
1536 atomic_inc(&cp->in_pkts);
1537 goto out;
1538 }
1539
1540 /*
1541 * mangle and send the packet here (only for VS/NAT)
1542 */
1543 was_input = rt_is_input_route(skb_rtable(skb));
1544
1545 /* LOCALNODE from FORWARD hook is not supported */
1546 rt_mode = (hooknum != NF_INET_FORWARD) ?
1547 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1548 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1549 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1550 NULL, iph);
1551 if (local < 0)
1552 goto tx_error;
1553 rt = skb_rtable(skb);
1554
1555 /*
1556 * Avoid duplicate tuple in reply direction for NAT traffic
1557 * to local address when connection is sync-ed
1558 */
1559 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1560 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1561 enum ip_conntrack_info ctinfo;
1562 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1563
1564 if (ct) {
1565 IP_VS_DBG(10, "%s(): "
1566 "stopping DNAT to local address %pI4\n",
1567 __func__, &cp->daddr.ip);
1568 goto tx_error;
1569 }
1570 }
1571 #endif
1572
1573 /* From world but DNAT to loopback address? */
1574 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1575 IP_VS_DBG(1, "%s(): "
1576 "stopping DNAT to loopback %pI4\n",
1577 __func__, &cp->daddr.ip);
1578 goto tx_error;
1579 }
1580
1581 /* copy-on-write the packet before mangling it */
1582 if (skb_ensure_writable(skb, offset))
1583 goto tx_error;
1584
1585 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1586 goto tx_error;
1587
1588 ip_vs_nat_icmp(skb, pp, cp, 0);
1589
1590 /* Another hack: avoid icmp_send in ip_fragment */
1591 skb->ignore_df = 1;
1592
1593 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1594 goto out;
1595
1596 tx_error:
1597 kfree_skb(skb);
1598 rc = NF_STOLEN;
1599 out:
1600 LeaveFunction(10);
1601 return rc;
1602 }
1603
1604 #ifdef CONFIG_IP_VS_IPV6
1605 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)1606 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1607 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1608 struct ip_vs_iphdr *ipvsh)
1609 {
1610 struct rt6_info *rt; /* Route to the other host */
1611 int rc;
1612 int local;
1613 int rt_mode;
1614
1615 EnterFunction(10);
1616
1617 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1618 forwarded directly here, because there is no need to
1619 translate address/port back */
1620 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1621 if (cp->packet_xmit)
1622 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1623 else
1624 rc = NF_ACCEPT;
1625 /* do not touch skb anymore */
1626 atomic_inc(&cp->in_pkts);
1627 goto out;
1628 }
1629
1630 /*
1631 * mangle and send the packet here (only for VS/NAT)
1632 */
1633
1634 /* LOCALNODE from FORWARD hook is not supported */
1635 rt_mode = (hooknum != NF_INET_FORWARD) ?
1636 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1637 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1638 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1639 &cp->daddr.in6, NULL, ipvsh, 0, rt_mode);
1640 if (local < 0)
1641 goto tx_error;
1642 rt = (struct rt6_info *) skb_dst(skb);
1643 /*
1644 * Avoid duplicate tuple in reply direction for NAT traffic
1645 * to local address when connection is sync-ed
1646 */
1647 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1648 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1649 enum ip_conntrack_info ctinfo;
1650 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1651
1652 if (ct) {
1653 IP_VS_DBG(10, "%s(): "
1654 "stopping DNAT to local address %pI6\n",
1655 __func__, &cp->daddr.in6);
1656 goto tx_error;
1657 }
1658 }
1659 #endif
1660
1661 /* From world but DNAT to loopback address? */
1662 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1663 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1664 IP_VS_DBG(1, "%s(): "
1665 "stopping DNAT to loopback %pI6\n",
1666 __func__, &cp->daddr.in6);
1667 goto tx_error;
1668 }
1669
1670 /* copy-on-write the packet before mangling it */
1671 if (skb_ensure_writable(skb, offset))
1672 goto tx_error;
1673
1674 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1675 goto tx_error;
1676
1677 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1678
1679 /* Another hack: avoid icmp_send in ip_fragment */
1680 skb->ignore_df = 1;
1681
1682 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
1683 goto out;
1684
1685 tx_error:
1686 kfree_skb(skb);
1687 rc = NF_STOLEN;
1688 out:
1689 LeaveFunction(10);
1690 return rc;
1691 }
1692 #endif
1693