Debian Codegraph @

net/netfilter/ipvs/ip_vs_xmit.c

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