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