1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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>
34#include <net/ip.h>
35#include <net/tcp.h>
36#include <net/udp.h>
37#include <net/icmp.h>
38#include <net/route.h>
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
49enum {
50 IP_VS_RT_MODE_LOCAL = 1,
51 IP_VS_RT_MODE_NON_LOCAL = 2,
52 IP_VS_RT_MODE_RDR = 4,
53
54
55 IP_VS_RT_MODE_CONNECT = 8,
56 IP_VS_RT_MODE_KNOWN_NH = 16,
57 IP_VS_RT_MODE_TUNNEL = 32,
58};
59
60static 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
65static inline void ip_vs_dest_dst_free(struct ip_vs_dest_dst *dest_dst)
66{
67 kfree(dest_dst);
68}
69
70
71
72
73static 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
92static 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
107static inline bool
108__mtu_check_toobig_v6(const struct sk_buff *skb, u32 mtu)
109{
110 if (IP6CB(skb)->frag_max_size) {
111
112
113
114 if (IP6CB(skb)->frag_max_size > mtu)
115 return true;
116 }
117 else if (skb->len > mtu && !skb_is_gso(skb)) {
118 return true;
119 }
120 return false;
121}
122
123
124static 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
136retry:
137 rt = ip_route_output_key(net, &fl4);
138 if (IS_ERR(rt)) {
139
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
160static 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
166static 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_NON_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
206static 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, true);
213}
214
215static 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
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
238
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
257static 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
268 if (ipv6_hdr(skb)->hop_limit <= 1) {
269 struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
270
271
272 skb->dev = dst->dev;
273 icmpv6_send(skb, ICMPV6_TIME_EXCEED,
274 ICMPV6_EXC_HOPLIMIT, 0);
275 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
276
277 return false;
278 }
279
280
281 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
282 return false;
283
284 ipv6_hdr(skb)->hop_limit--;
285 } else
286#endif
287 {
288 if (ip_hdr(skb)->ttl <= 1) {
289
290 __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
291 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
292 return false;
293 }
294
295
296 if (!skb_make_writable(skb, sizeof(struct iphdr)))
297 return false;
298
299
300 ip_decrease_ttl(ip_hdr(skb));
301 }
302
303 return true;
304}
305
306
307static int
308__ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
309 struct ip_vs_dest *dest,
310 __be32 daddr, int rt_mode, __be32 *ret_saddr,
311 struct ip_vs_iphdr *ipvsh)
312{
313 struct net *net = ipvs->net;
314 struct ip_vs_dest_dst *dest_dst;
315 struct rtable *rt;
316 int mtu;
317 int local, noref = 1;
318
319 if (dest) {
320 dest_dst = __ip_vs_dst_check(dest);
321 if (likely(dest_dst))
322 rt = (struct rtable *) dest_dst->dst_cache;
323 else {
324 dest_dst = ip_vs_dest_dst_alloc();
325 spin_lock_bh(&dest->dst_lock);
326 if (!dest_dst) {
327 __ip_vs_dst_set(dest, NULL, NULL, 0);
328 spin_unlock_bh(&dest->dst_lock);
329 goto err_unreach;
330 }
331 rt = do_output_route4(net, dest->addr.ip, rt_mode,
332 &dest_dst->dst_saddr.ip);
333 if (!rt) {
334 __ip_vs_dst_set(dest, NULL, NULL, 0);
335 spin_unlock_bh(&dest->dst_lock);
336 ip_vs_dest_dst_free(dest_dst);
337 goto err_unreach;
338 }
339 __ip_vs_dst_set(dest, dest_dst, &rt->dst, 0);
340 spin_unlock_bh(&dest->dst_lock);
341 IP_VS_DBG(10, "new dst %pI4, src %pI4, refcnt=%d\n",
342 &dest->addr.ip, &dest_dst->dst_saddr.ip,
343 atomic_read(&rt->dst.__refcnt));
344 }
345 if (ret_saddr)
346 *ret_saddr = dest_dst->dst_saddr.ip;
347 } else {
348 __be32 saddr = htonl(INADDR_ANY);
349
350 noref = 0;
351
352
353
354
355 rt_mode &= ~IP_VS_RT_MODE_CONNECT;
356 rt = do_output_route4(net, daddr, rt_mode, &saddr);
357 if (!rt)
358 goto err_unreach;
359 if (ret_saddr)
360 *ret_saddr = saddr;
361 }
362
363 local = (rt->rt_flags & RTCF_LOCAL) ? 1 : 0;
364 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
365 local))) {
366 IP_VS_DBG_RL("We are crossing local and non-local addresses"
367 " daddr=%pI4\n", &daddr);
368 goto err_put;
369 }
370
371 if (unlikely(local)) {
372
373 if (!noref)
374 ip_rt_put(rt);
375 return local;
376 }
377
378 if (!decrement_ttl(ipvs, skb_af, skb))
379 goto err_put;
380
381 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL))) {
382 mtu = dst_mtu(&rt->dst);
383 } else {
384 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
385 if (mtu < 68) {
386 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
387 goto err_put;
388 }
389 maybe_update_pmtu(skb_af, skb, mtu);
390 }
391
392 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
393 goto err_put;
394
395 skb_dst_drop(skb);
396 if (noref) {
397 if (!local)
398 skb_dst_set_noref(skb, &rt->dst);
399 else
400 skb_dst_set(skb, dst_clone(&rt->dst));
401 } else
402 skb_dst_set(skb, &rt->dst);
403
404 return local;
405
406err_put:
407 if (!noref)
408 ip_rt_put(rt);
409 return -1;
410
411err_unreach:
412 dst_link_failure(skb);
413 return -1;
414}
415
416#ifdef CONFIG_IP_VS_IPV6
417static struct dst_entry *
418__ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
419 struct in6_addr *ret_saddr, int do_xfrm, int rt_mode)
420{
421 struct dst_entry *dst;
422 struct flowi6 fl6 = {
423 .daddr = *daddr,
424 };
425
426 if (rt_mode & IP_VS_RT_MODE_KNOWN_NH)
427 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
428
429 dst = ip6_route_output(net, NULL, &fl6);
430 if (dst->error)
431 goto out_err;
432 if (!ret_saddr)
433 return dst;
434 if (ipv6_addr_any(&fl6.saddr) &&
435 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
436 &fl6.daddr, 0, &fl6.saddr) < 0)
437 goto out_err;
438 if (do_xfrm) {
439 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
440 if (IS_ERR(dst)) {
441 dst = NULL;
442 goto out_err;
443 }
444 }
445 *ret_saddr = fl6.saddr;
446 return dst;
447
448out_err:
449 dst_release(dst);
450 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
451 return NULL;
452}
453
454
455
456
457static int
458__ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
459 struct ip_vs_dest *dest,
460 struct in6_addr *daddr, struct in6_addr *ret_saddr,
461 struct ip_vs_iphdr *ipvsh, int do_xfrm, int rt_mode)
462{
463 struct net *net = ipvs->net;
464 struct ip_vs_dest_dst *dest_dst;
465 struct rt6_info *rt;
466 struct dst_entry *dst;
467 int mtu;
468 int local, noref = 1;
469
470 if (dest) {
471 dest_dst = __ip_vs_dst_check(dest);
472 if (likely(dest_dst))
473 rt = (struct rt6_info *) dest_dst->dst_cache;
474 else {
475 u32 cookie;
476
477 dest_dst = ip_vs_dest_dst_alloc();
478 spin_lock_bh(&dest->dst_lock);
479 if (!dest_dst) {
480 __ip_vs_dst_set(dest, NULL, NULL, 0);
481 spin_unlock_bh(&dest->dst_lock);
482 goto err_unreach;
483 }
484 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
485 &dest_dst->dst_saddr.in6,
486 do_xfrm, rt_mode);
487 if (!dst) {
488 __ip_vs_dst_set(dest, NULL, NULL, 0);
489 spin_unlock_bh(&dest->dst_lock);
490 ip_vs_dest_dst_free(dest_dst);
491 goto err_unreach;
492 }
493 rt = (struct rt6_info *) dst;
494 cookie = rt6_get_cookie(rt);
495 __ip_vs_dst_set(dest, dest_dst, &rt->dst, cookie);
496 spin_unlock_bh(&dest->dst_lock);
497 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
498 &dest->addr.in6, &dest_dst->dst_saddr.in6,
499 atomic_read(&rt->dst.__refcnt));
500 }
501 if (ret_saddr)
502 *ret_saddr = dest_dst->dst_saddr.in6;
503 } else {
504 noref = 0;
505 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
506 rt_mode);
507 if (!dst)
508 goto err_unreach;
509 rt = (struct rt6_info *) dst;
510 }
511
512 local = __ip_vs_is_local_route6(rt);
513
514 if (unlikely(crosses_local_route_boundary(skb_af, skb, rt_mode,
515 local))) {
516 IP_VS_DBG_RL("We are crossing local and non-local addresses"
517 " daddr=%pI6\n", daddr);
518 goto err_put;
519 }
520
521 if (unlikely(local)) {
522
523 if (!noref)
524 dst_release(&rt->dst);
525 return local;
526 }
527
528 if (!decrement_ttl(ipvs, skb_af, skb))
529 goto err_put;
530
531
532 if (likely(!(rt_mode & IP_VS_RT_MODE_TUNNEL)))
533 mtu = dst_mtu(&rt->dst);
534 else {
535 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
536 if (mtu < IPV6_MIN_MTU) {
537 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
538 IPV6_MIN_MTU);
539 goto err_put;
540 }
541 maybe_update_pmtu(skb_af, skb, mtu);
542 }
543
544 if (!ensure_mtu_is_adequate(ipvs, skb_af, rt_mode, ipvsh, skb, mtu))
545 goto err_put;
546
547 skb_dst_drop(skb);
548 if (noref) {
549 if (!local)
550 skb_dst_set_noref(skb, &rt->dst);
551 else
552 skb_dst_set(skb, dst_clone(&rt->dst));
553 } else
554 skb_dst_set(skb, &rt->dst);
555
556 return local;
557
558err_put:
559 if (!noref)
560 dst_release(&rt->dst);
561 return -1;
562
563err_unreach:
564
565
566
567
568 if (!skb->dev)
569 skb->dev = skb_dst(skb)->dev;
570
571 dst_link_failure(skb);
572 return -1;
573}
574#endif
575
576
577
578static inline int ip_vs_tunnel_xmit_prepare(struct sk_buff *skb,
579 struct ip_vs_conn *cp)
580{
581 int ret = NF_ACCEPT;
582
583 skb->ipvs_property = 1;
584 if (unlikely(cp->flags & IP_VS_CONN_F_NFCT))
585 ret = ip_vs_confirm_conntrack(skb);
586 if (ret == NF_ACCEPT) {
587 nf_reset(skb);
588 skb_forward_csum(skb);
589 if (skb->dev)
590 skb->tstamp = 0;
591 }
592 return ret;
593}
594
595
596
597
598
599
600
601static inline void ip_vs_drop_early_demux_sk(struct sk_buff *skb)
602{
603
604
605
606 if (skb->dev)
607 skb_orphan(skb);
608}
609
610
611static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
612 struct ip_vs_conn *cp, int local)
613{
614 int ret = NF_STOLEN;
615
616 skb->ipvs_property = 1;
617 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
618 ip_vs_notrack(skb);
619 else
620 ip_vs_update_conntrack(skb, cp, 1);
621
622
623
624
625 if (!local || cp->vport != cp->dport ||
626 !ip_vs_addr_equal(cp->af, &cp->vaddr, &cp->daddr))
627 ip_vs_drop_early_demux_sk(skb);
628
629 if (!local) {
630 skb_forward_csum(skb);
631 if (skb->dev)
632 skb->tstamp = 0;
633 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
634 NULL, skb_dst(skb)->dev, dst_output);
635 } else
636 ret = NF_ACCEPT;
637
638 return ret;
639}
640
641
642static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
643 struct ip_vs_conn *cp, int local)
644{
645 int ret = NF_STOLEN;
646
647 skb->ipvs_property = 1;
648 if (likely(!(cp->flags & IP_VS_CONN_F_NFCT)))
649 ip_vs_notrack(skb);
650 if (!local) {
651 ip_vs_drop_early_demux_sk(skb);
652 skb_forward_csum(skb);
653 if (skb->dev)
654 skb->tstamp = 0;
655 NF_HOOK(pf, NF_INET_LOCAL_OUT, cp->ipvs->net, NULL, skb,
656 NULL, skb_dst(skb)->dev, dst_output);
657 } else
658 ret = NF_ACCEPT;
659 return ret;
660}
661
662
663
664
665
666int
667ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
668 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
669{
670
671 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
672}
673
674
675
676
677
678
679
680int
681ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
682 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
683{
684 struct iphdr *iph = ip_hdr(skb);
685
686 EnterFunction(10);
687
688 if (__ip_vs_get_out_rt(cp->ipvs, cp->af, skb, NULL, iph->daddr,
689 IP_VS_RT_MODE_NON_LOCAL, NULL, ipvsh) < 0)
690 goto tx_error;
691
692 ip_send_check(iph);
693
694
695 skb->ignore_df = 1;
696
697 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
698
699 LeaveFunction(10);
700 return NF_STOLEN;
701
702 tx_error:
703 kfree_skb(skb);
704 LeaveFunction(10);
705 return NF_STOLEN;
706}
707
708#ifdef CONFIG_IP_VS_IPV6
709int
710ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
711 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
712{
713 struct ipv6hdr *iph = ipv6_hdr(skb);
714
715 EnterFunction(10);
716
717 if (__ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, NULL,
718 &iph->daddr, NULL,
719 ipvsh, 0, IP_VS_RT_MODE_NON_LOCAL) < 0)
720 goto tx_error;
721
722
723 skb->ignore_df = 1;
724
725 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
726
727 LeaveFunction(10);
728 return NF_STOLEN;
729
730 tx_error:
731 kfree_skb(skb);
732 LeaveFunction(10);
733 return NF_STOLEN;
734}
735#endif
736
737
738
739
740
741int
742ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
743 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
744{
745 struct rtable *rt;
746 int local, rc, was_input;
747
748 EnterFunction(10);
749
750
751 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
752 __be16 _pt, *p;
753
754 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
755 if (p == NULL)
756 goto tx_error;
757 ip_vs_conn_fill_cport(cp, *p);
758 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
759 }
760
761 was_input = rt_is_input_route(skb_rtable(skb));
762 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
763 IP_VS_RT_MODE_LOCAL |
764 IP_VS_RT_MODE_NON_LOCAL |
765 IP_VS_RT_MODE_RDR, NULL, ipvsh);
766 if (local < 0)
767 goto tx_error;
768 rt = skb_rtable(skb);
769
770
771
772
773#if IS_ENABLED(CONFIG_NF_CONNTRACK)
774 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
775 enum ip_conntrack_info ctinfo;
776 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
777
778 if (ct) {
779 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, ipvsh->off,
780 "ip_vs_nat_xmit(): "
781 "stopping DNAT to local address");
782 goto tx_error;
783 }
784 }
785#endif
786
787
788 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
789 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, ipvsh->off,
790 "ip_vs_nat_xmit(): stopping DNAT to loopback "
791 "address");
792 goto tx_error;
793 }
794
795
796 if (!skb_make_writable(skb, sizeof(struct iphdr)))
797 goto tx_error;
798
799 if (skb_cow(skb, rt->dst.dev->hard_header_len))
800 goto tx_error;
801
802
803 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
804 goto tx_error;
805 ip_hdr(skb)->daddr = cp->daddr.ip;
806 ip_send_check(ip_hdr(skb));
807
808 IP_VS_DBG_PKT(10, AF_INET, pp, skb, ipvsh->off, "After DNAT");
809
810
811
812
813
814
815 skb->ignore_df = 1;
816
817 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
818
819 LeaveFunction(10);
820 return rc;
821
822 tx_error:
823 kfree_skb(skb);
824 LeaveFunction(10);
825 return NF_STOLEN;
826}
827
828#ifdef CONFIG_IP_VS_IPV6
829int
830ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
831 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
832{
833 struct rt6_info *rt;
834 int local, rc;
835
836 EnterFunction(10);
837
838
839 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT && !ipvsh->fragoffs)) {
840 __be16 _pt, *p;
841 p = skb_header_pointer(skb, ipvsh->len, sizeof(_pt), &_pt);
842 if (p == NULL)
843 goto tx_error;
844 ip_vs_conn_fill_cport(cp, *p);
845 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
846 }
847
848 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
849 &cp->daddr.in6,
850 NULL, ipvsh, 0,
851 IP_VS_RT_MODE_LOCAL |
852 IP_VS_RT_MODE_NON_LOCAL |
853 IP_VS_RT_MODE_RDR);
854 if (local < 0)
855 goto tx_error;
856 rt = (struct rt6_info *) skb_dst(skb);
857
858
859
860
861#if IS_ENABLED(CONFIG_NF_CONNTRACK)
862 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
863 enum ip_conntrack_info ctinfo;
864 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
865
866 if (ct) {
867 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, ipvsh->off,
868 "ip_vs_nat_xmit_v6(): "
869 "stopping DNAT to local address");
870 goto tx_error;
871 }
872 }
873#endif
874
875
876 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
877 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
878 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, ipvsh->off,
879 "ip_vs_nat_xmit_v6(): "
880 "stopping DNAT to loopback address");
881 goto tx_error;
882 }
883
884
885 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
886 goto tx_error;
887
888 if (skb_cow(skb, rt->dst.dev->hard_header_len))
889 goto tx_error;
890
891
892 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp, ipvsh))
893 goto tx_error;
894 ipv6_hdr(skb)->daddr = cp->daddr.in6;
895
896 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, ipvsh->off, "After DNAT");
897
898
899
900
901
902
903 skb->ignore_df = 1;
904
905 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
906
907 LeaveFunction(10);
908 return rc;
909
910tx_error:
911 LeaveFunction(10);
912 kfree_skb(skb);
913 return NF_STOLEN;
914}
915#endif
916
917
918
919
920
921
922
923static struct sk_buff *
924ip_vs_prepare_tunneled_skb(struct sk_buff *skb, int skb_af,
925 unsigned int max_headroom, __u8 *next_protocol,
926 __u32 *payload_len, __u8 *dsfield, __u8 *ttl,
927 __be16 *df)
928{
929 struct sk_buff *new_skb = NULL;
930 struct iphdr *old_iph = NULL;
931 __u8 old_dsfield;
932#ifdef CONFIG_IP_VS_IPV6
933 struct ipv6hdr *old_ipv6h = NULL;
934#endif
935
936 ip_vs_drop_early_demux_sk(skb);
937
938 if (skb_headroom(skb) < max_headroom || skb_cloned(skb)) {
939 new_skb = skb_realloc_headroom(skb, max_headroom);
940 if (!new_skb)
941 goto error;
942 if (skb->sk)
943 skb_set_owner_w(new_skb, skb->sk);
944 consume_skb(skb);
945 skb = new_skb;
946 }
947
948#ifdef CONFIG_IP_VS_IPV6
949 if (skb_af == AF_INET6) {
950 old_ipv6h = ipv6_hdr(skb);
951 *next_protocol = IPPROTO_IPV6;
952 if (payload_len)
953 *payload_len =
954 ntohs(old_ipv6h->payload_len) +
955 sizeof(*old_ipv6h);
956 old_dsfield = ipv6_get_dsfield(old_ipv6h);
957 *ttl = old_ipv6h->hop_limit;
958 if (df)
959 *df = 0;
960 } else
961#endif
962 {
963 old_iph = ip_hdr(skb);
964
965 if (df)
966 *df = (old_iph->frag_off & htons(IP_DF));
967 *next_protocol = IPPROTO_IPIP;
968
969
970 ip_send_check(old_iph);
971 old_dsfield = ipv4_get_dsfield(old_iph);
972 *ttl = old_iph->ttl;
973 if (payload_len)
974 *payload_len = ntohs(old_iph->tot_len);
975 }
976
977
978 *dsfield = INET_ECN_encapsulate(old_dsfield, old_dsfield);
979
980 return skb;
981error:
982 kfree_skb(skb);
983 return ERR_PTR(-ENOMEM);
984}
985
986static inline int __tun_gso_type_mask(int encaps_af, int orig_af)
987{
988 switch (encaps_af) {
989 case AF_INET:
990 return SKB_GSO_IPXIP4;
991 case AF_INET6:
992 return SKB_GSO_IPXIP6;
993 default:
994 return 0;
995 }
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017int
1018ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1019 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1020{
1021 struct netns_ipvs *ipvs = cp->ipvs;
1022 struct net *net = ipvs->net;
1023 struct rtable *rt;
1024 __be32 saddr;
1025 struct net_device *tdev;
1026 __u8 next_protocol = 0;
1027 __u8 dsfield = 0;
1028 __u8 ttl = 0;
1029 __be16 df = 0;
1030 __be16 *dfp = NULL;
1031 struct iphdr *iph;
1032 unsigned int max_headroom;
1033 int ret, local;
1034
1035 EnterFunction(10);
1036
1037 local = __ip_vs_get_out_rt(ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1038 IP_VS_RT_MODE_LOCAL |
1039 IP_VS_RT_MODE_NON_LOCAL |
1040 IP_VS_RT_MODE_CONNECT |
1041 IP_VS_RT_MODE_TUNNEL, &saddr, ipvsh);
1042 if (local < 0)
1043 goto tx_error;
1044 if (local)
1045 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1046
1047 rt = skb_rtable(skb);
1048 tdev = rt->dst.dev;
1049
1050
1051
1052
1053 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
1054
1055
1056 dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
1057 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1058 &next_protocol, NULL, &dsfield,
1059 &ttl, dfp);
1060 if (IS_ERR(skb))
1061 goto tx_error;
1062
1063 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET, cp->af)))
1064 goto tx_error;
1065
1066 skb->transport_header = skb->network_header;
1067
1068 skb_push(skb, sizeof(struct iphdr));
1069 skb_reset_network_header(skb);
1070 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1071
1072
1073
1074
1075 iph = ip_hdr(skb);
1076 iph->version = 4;
1077 iph->ihl = sizeof(struct iphdr)>>2;
1078 iph->frag_off = df;
1079 iph->protocol = next_protocol;
1080 iph->tos = dsfield;
1081 iph->daddr = cp->daddr.ip;
1082 iph->saddr = saddr;
1083 iph->ttl = ttl;
1084 ip_select_ident(net, skb, NULL);
1085
1086
1087 skb->ignore_df = 1;
1088
1089 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1090 if (ret == NF_ACCEPT)
1091 ip_local_out(net, skb->sk, skb);
1092 else if (ret == NF_DROP)
1093 kfree_skb(skb);
1094
1095 LeaveFunction(10);
1096
1097 return NF_STOLEN;
1098
1099 tx_error:
1100 if (!IS_ERR(skb))
1101 kfree_skb(skb);
1102 LeaveFunction(10);
1103 return NF_STOLEN;
1104}
1105
1106#ifdef CONFIG_IP_VS_IPV6
1107int
1108ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1109 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1110{
1111 struct rt6_info *rt;
1112 struct in6_addr saddr;
1113 struct net_device *tdev;
1114 __u8 next_protocol = 0;
1115 __u32 payload_len = 0;
1116 __u8 dsfield = 0;
1117 __u8 ttl = 0;
1118 struct ipv6hdr *iph;
1119 unsigned int max_headroom;
1120 int ret, local;
1121
1122 EnterFunction(10);
1123
1124 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1125 &cp->daddr.in6,
1126 &saddr, ipvsh, 1,
1127 IP_VS_RT_MODE_LOCAL |
1128 IP_VS_RT_MODE_NON_LOCAL |
1129 IP_VS_RT_MODE_TUNNEL);
1130 if (local < 0)
1131 goto tx_error;
1132 if (local)
1133 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1134
1135 rt = (struct rt6_info *) skb_dst(skb);
1136 tdev = rt->dst.dev;
1137
1138
1139
1140
1141 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
1142
1143 skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
1144 &next_protocol, &payload_len,
1145 &dsfield, &ttl, NULL);
1146 if (IS_ERR(skb))
1147 goto tx_error;
1148
1149 if (iptunnel_handle_offloads(skb, __tun_gso_type_mask(AF_INET6, cp->af)))
1150 goto tx_error;
1151
1152 skb->transport_header = skb->network_header;
1153
1154 skb_push(skb, sizeof(struct ipv6hdr));
1155 skb_reset_network_header(skb);
1156 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1157
1158
1159
1160
1161 iph = ipv6_hdr(skb);
1162 iph->version = 6;
1163 iph->nexthdr = next_protocol;
1164 iph->payload_len = htons(payload_len);
1165 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
1166 ipv6_change_dsfield(iph, 0, dsfield);
1167 iph->daddr = cp->daddr.in6;
1168 iph->saddr = saddr;
1169 iph->hop_limit = ttl;
1170
1171
1172 skb->ignore_df = 1;
1173
1174 ret = ip_vs_tunnel_xmit_prepare(skb, cp);
1175 if (ret == NF_ACCEPT)
1176 ip6_local_out(cp->ipvs->net, skb->sk, skb);
1177 else if (ret == NF_DROP)
1178 kfree_skb(skb);
1179
1180 LeaveFunction(10);
1181
1182 return NF_STOLEN;
1183
1184tx_error:
1185 if (!IS_ERR(skb))
1186 kfree_skb(skb);
1187 LeaveFunction(10);
1188 return NF_STOLEN;
1189}
1190#endif
1191
1192
1193
1194
1195
1196
1197int
1198ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1199 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1200{
1201 int local;
1202
1203 EnterFunction(10);
1204
1205 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip,
1206 IP_VS_RT_MODE_LOCAL |
1207 IP_VS_RT_MODE_NON_LOCAL |
1208 IP_VS_RT_MODE_KNOWN_NH, NULL, ipvsh);
1209 if (local < 0)
1210 goto tx_error;
1211 if (local)
1212 return ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 1);
1213
1214 ip_send_check(ip_hdr(skb));
1215
1216
1217 skb->ignore_df = 1;
1218
1219 ip_vs_send_or_cont(NFPROTO_IPV4, skb, cp, 0);
1220
1221 LeaveFunction(10);
1222 return NF_STOLEN;
1223
1224 tx_error:
1225 kfree_skb(skb);
1226 LeaveFunction(10);
1227 return NF_STOLEN;
1228}
1229
1230#ifdef CONFIG_IP_VS_IPV6
1231int
1232ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1233 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
1234{
1235 int local;
1236
1237 EnterFunction(10);
1238
1239 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1240 &cp->daddr.in6,
1241 NULL, ipvsh, 0,
1242 IP_VS_RT_MODE_LOCAL |
1243 IP_VS_RT_MODE_NON_LOCAL |
1244 IP_VS_RT_MODE_KNOWN_NH);
1245 if (local < 0)
1246 goto tx_error;
1247 if (local)
1248 return ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 1);
1249
1250
1251 skb->ignore_df = 1;
1252
1253 ip_vs_send_or_cont(NFPROTO_IPV6, skb, cp, 0);
1254
1255 LeaveFunction(10);
1256 return NF_STOLEN;
1257
1258tx_error:
1259 kfree_skb(skb);
1260 LeaveFunction(10);
1261 return NF_STOLEN;
1262}
1263#endif
1264
1265
1266
1267
1268
1269
1270int
1271ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1272 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1273 struct ip_vs_iphdr *iph)
1274{
1275 struct rtable *rt;
1276 int rc;
1277 int local;
1278 int rt_mode, was_input;
1279
1280 EnterFunction(10);
1281
1282
1283
1284
1285 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1286 if (cp->packet_xmit)
1287 rc = cp->packet_xmit(skb, cp, pp, iph);
1288 else
1289 rc = NF_ACCEPT;
1290
1291 atomic_inc(&cp->in_pkts);
1292 goto out;
1293 }
1294
1295
1296
1297
1298 was_input = rt_is_input_route(skb_rtable(skb));
1299
1300
1301 rt_mode = (hooknum != NF_INET_FORWARD) ?
1302 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1303 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1304 local = __ip_vs_get_out_rt(cp->ipvs, cp->af, skb, cp->dest, cp->daddr.ip, rt_mode,
1305 NULL, iph);
1306 if (local < 0)
1307 goto tx_error;
1308 rt = skb_rtable(skb);
1309
1310
1311
1312
1313
1314#if IS_ENABLED(CONFIG_NF_CONNTRACK)
1315 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1316 enum ip_conntrack_info ctinfo;
1317 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1318
1319 if (ct) {
1320 IP_VS_DBG(10, "%s(): "
1321 "stopping DNAT to local address %pI4\n",
1322 __func__, &cp->daddr.ip);
1323 goto tx_error;
1324 }
1325 }
1326#endif
1327
1328
1329 if (local && ipv4_is_loopback(cp->daddr.ip) && was_input) {
1330 IP_VS_DBG(1, "%s(): "
1331 "stopping DNAT to loopback %pI4\n",
1332 __func__, &cp->daddr.ip);
1333 goto tx_error;
1334 }
1335
1336
1337 if (!skb_make_writable(skb, offset))
1338 goto tx_error;
1339
1340 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1341 goto tx_error;
1342
1343 ip_vs_nat_icmp(skb, pp, cp, 0);
1344
1345
1346 skb->ignore_df = 1;
1347
1348 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV4, skb, cp, local);
1349 goto out;
1350
1351 tx_error:
1352 kfree_skb(skb);
1353 rc = NF_STOLEN;
1354 out:
1355 LeaveFunction(10);
1356 return rc;
1357}
1358
1359#ifdef CONFIG_IP_VS_IPV6
1360int
1361ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1362 struct ip_vs_protocol *pp, int offset, unsigned int hooknum,
1363 struct ip_vs_iphdr *ipvsh)
1364{
1365 struct rt6_info *rt;
1366 int rc;
1367 int local;
1368 int rt_mode;
1369
1370 EnterFunction(10);
1371
1372
1373
1374
1375 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1376 if (cp->packet_xmit)
1377 rc = cp->packet_xmit(skb, cp, pp, ipvsh);
1378 else
1379 rc = NF_ACCEPT;
1380
1381 atomic_inc(&cp->in_pkts);
1382 goto out;
1383 }
1384
1385
1386
1387
1388
1389
1390 rt_mode = (hooknum != NF_INET_FORWARD) ?
1391 IP_VS_RT_MODE_LOCAL | IP_VS_RT_MODE_NON_LOCAL |
1392 IP_VS_RT_MODE_RDR : IP_VS_RT_MODE_NON_LOCAL;
1393 local = __ip_vs_get_out_rt_v6(cp->ipvs, cp->af, skb, cp->dest,
1394 &cp->daddr.in6, NULL, ipvsh, 0, rt_mode);
1395 if (local < 0)
1396 goto tx_error;
1397 rt = (struct rt6_info *) skb_dst(skb);
1398
1399
1400
1401
1402#if IS_ENABLED(CONFIG_NF_CONNTRACK)
1403 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1404 enum ip_conntrack_info ctinfo;
1405 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1406
1407 if (ct) {
1408 IP_VS_DBG(10, "%s(): "
1409 "stopping DNAT to local address %pI6\n",
1410 __func__, &cp->daddr.in6);
1411 goto tx_error;
1412 }
1413 }
1414#endif
1415
1416
1417 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1418 ipv6_addr_type(&cp->daddr.in6) & IPV6_ADDR_LOOPBACK) {
1419 IP_VS_DBG(1, "%s(): "
1420 "stopping DNAT to loopback %pI6\n",
1421 __func__, &cp->daddr.in6);
1422 goto tx_error;
1423 }
1424
1425
1426 if (!skb_make_writable(skb, offset))
1427 goto tx_error;
1428
1429 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1430 goto tx_error;
1431
1432 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1433
1434
1435 skb->ignore_df = 1;
1436
1437 rc = ip_vs_nat_send_or_cont(NFPROTO_IPV6, skb, cp, local);
1438 goto out;
1439
1440tx_error:
1441 kfree_skb(skb);
1442 rc = NF_STOLEN;
1443out:
1444 LeaveFunction(10);
1445 return rc;
1446}
1447#endif
1448