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