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