IPVS: Handle Scheduling errors.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / ipvs / ip_vs_xmit.c
blobfb2a445ddc596718972645e0a65ca6dcc45fdb52
1 /*
2 * ip_vs_xmit.c: various packet transmitters for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
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.
12 * Changes:
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 * LOCAL_OUT rules:
21 * - skb->dev is NULL, skb->protocol is not set (both are set in POST_ROUTING)
22 * - skb->pkt_type is not set yet
23 * - the only place where we can see skb->sk != NULL
26 #define KMSG_COMPONENT "IPVS"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/tcp.h> /* for tcphdr */
32 #include <net/ip.h>
33 #include <net/tcp.h> /* for csum_tcpudp_magic */
34 #include <net/udp.h>
35 #include <net/icmp.h> /* for icmp_send */
36 #include <net/route.h> /* for ip_route_output */
37 #include <net/ipv6.h>
38 #include <net/ip6_route.h>
39 #include <net/addrconf.h>
40 #include <linux/icmpv6.h>
41 #include <linux/netfilter.h>
42 #include <linux/netfilter_ipv4.h>
44 #include <net/ip_vs.h>
48 * Destination cache to speed up outgoing route lookup
50 static inline void
51 __ip_vs_dst_set(struct ip_vs_dest *dest, u32 rtos, struct dst_entry *dst,
52 u32 dst_cookie)
54 struct dst_entry *old_dst;
56 old_dst = dest->dst_cache;
57 dest->dst_cache = dst;
58 dest->dst_rtos = rtos;
59 dest->dst_cookie = dst_cookie;
60 dst_release(old_dst);
63 static inline struct dst_entry *
64 __ip_vs_dst_check(struct ip_vs_dest *dest, u32 rtos)
66 struct dst_entry *dst = dest->dst_cache;
68 if (!dst)
69 return NULL;
70 if ((dst->obsolete || rtos != dest->dst_rtos) &&
71 dst->ops->check(dst, dest->dst_cookie) == NULL) {
72 dest->dst_cache = NULL;
73 dst_release(dst);
74 return NULL;
76 dst_hold(dst);
77 return dst;
81 * Get route to destination or remote server
82 * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest,
83 * &4=Allow redirect from remote daddr to local
85 static struct rtable *
86 __ip_vs_get_out_rt(struct sk_buff *skb, struct ip_vs_dest *dest,
87 __be32 daddr, u32 rtos, int rt_mode)
89 struct net *net = dev_net(skb_dst(skb)->dev);
90 struct rtable *rt; /* Route to the other host */
91 struct rtable *ort; /* Original route */
92 int local;
94 if (dest) {
95 spin_lock(&dest->dst_lock);
96 if (!(rt = (struct rtable *)
97 __ip_vs_dst_check(dest, rtos))) {
98 struct flowi fl = {
99 .oif = 0,
100 .nl_u = {
101 .ip4_u = {
102 .daddr = dest->addr.ip,
103 .saddr = 0,
104 .tos = rtos, } },
107 if (ip_route_output_key(net, &rt, &fl)) {
108 spin_unlock(&dest->dst_lock);
109 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
110 &dest->addr.ip);
111 return NULL;
113 __ip_vs_dst_set(dest, rtos, dst_clone(&rt->dst), 0);
114 IP_VS_DBG(10, "new dst %pI4, refcnt=%d, rtos=%X\n",
115 &dest->addr.ip,
116 atomic_read(&rt->dst.__refcnt), rtos);
118 spin_unlock(&dest->dst_lock);
119 } else {
120 struct flowi fl = {
121 .oif = 0,
122 .nl_u = {
123 .ip4_u = {
124 .daddr = daddr,
125 .saddr = 0,
126 .tos = rtos, } },
129 if (ip_route_output_key(net, &rt, &fl)) {
130 IP_VS_DBG_RL("ip_route_output error, dest: %pI4\n",
131 &daddr);
132 return NULL;
136 local = rt->rt_flags & RTCF_LOCAL;
137 if (!((local ? 1 : 2) & rt_mode)) {
138 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI4\n",
139 (rt->rt_flags & RTCF_LOCAL) ?
140 "local":"non-local", &rt->rt_dst);
141 ip_rt_put(rt);
142 return NULL;
144 if (local && !(rt_mode & 4) && !((ort = skb_rtable(skb)) &&
145 ort->rt_flags & RTCF_LOCAL)) {
146 IP_VS_DBG_RL("Redirect from non-local address %pI4 to local "
147 "requires NAT method, dest: %pI4\n",
148 &ip_hdr(skb)->daddr, &rt->rt_dst);
149 ip_rt_put(rt);
150 return NULL;
152 if (unlikely(!local && ipv4_is_loopback(ip_hdr(skb)->saddr))) {
153 IP_VS_DBG_RL("Stopping traffic from loopback address %pI4 "
154 "to non-local address, dest: %pI4\n",
155 &ip_hdr(skb)->saddr, &rt->rt_dst);
156 ip_rt_put(rt);
157 return NULL;
160 return rt;
163 /* Reroute packet to local IPv4 stack after DNAT */
164 static int
165 __ip_vs_reroute_locally(struct sk_buff *skb)
167 struct rtable *rt = skb_rtable(skb);
168 struct net_device *dev = rt->dst.dev;
169 struct net *net = dev_net(dev);
170 struct iphdr *iph = ip_hdr(skb);
172 if (rt_is_input_route(rt)) {
173 unsigned long orefdst = skb->_skb_refdst;
175 if (ip_route_input(skb, iph->daddr, iph->saddr,
176 iph->tos, skb->dev))
177 return 0;
178 refdst_drop(orefdst);
179 } else {
180 struct flowi fl = {
181 .oif = 0,
182 .nl_u = {
183 .ip4_u = {
184 .daddr = iph->daddr,
185 .saddr = iph->saddr,
186 .tos = RT_TOS(iph->tos),
189 .mark = skb->mark,
192 if (ip_route_output_key(net, &rt, &fl))
193 return 0;
194 if (!(rt->rt_flags & RTCF_LOCAL)) {
195 ip_rt_put(rt);
196 return 0;
198 /* Drop old route. */
199 skb_dst_drop(skb);
200 skb_dst_set(skb, &rt->dst);
202 return 1;
205 #ifdef CONFIG_IP_VS_IPV6
207 static inline int __ip_vs_is_local_route6(struct rt6_info *rt)
209 return rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK;
212 static struct dst_entry *
213 __ip_vs_route_output_v6(struct net *net, struct in6_addr *daddr,
214 struct in6_addr *ret_saddr, int do_xfrm)
216 struct dst_entry *dst;
217 struct flowi fl = {
218 .oif = 0,
219 .nl_u = {
220 .ip6_u = {
221 .daddr = *daddr,
226 dst = ip6_route_output(net, NULL, &fl);
227 if (dst->error)
228 goto out_err;
229 if (!ret_saddr)
230 return dst;
231 if (ipv6_addr_any(&fl.fl6_src) &&
232 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
233 &fl.fl6_dst, 0, &fl.fl6_src) < 0)
234 goto out_err;
235 if (do_xfrm && xfrm_lookup(net, &dst, &fl, NULL, 0) < 0)
236 goto out_err;
237 ipv6_addr_copy(ret_saddr, &fl.fl6_src);
238 return dst;
240 out_err:
241 dst_release(dst);
242 IP_VS_DBG_RL("ip6_route_output error, dest: %pI6\n", daddr);
243 return NULL;
247 * Get route to destination or remote server
248 * rt_mode: flags, &1=Allow local dest, &2=Allow non-local dest,
249 * &4=Allow redirect from remote daddr to local
251 static struct rt6_info *
252 __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,
253 struct in6_addr *daddr, struct in6_addr *ret_saddr,
254 int do_xfrm, int rt_mode)
256 struct net *net = dev_net(skb_dst(skb)->dev);
257 struct rt6_info *rt; /* Route to the other host */
258 struct rt6_info *ort; /* Original route */
259 struct dst_entry *dst;
260 int local;
262 if (dest) {
263 spin_lock(&dest->dst_lock);
264 rt = (struct rt6_info *)__ip_vs_dst_check(dest, 0);
265 if (!rt) {
266 u32 cookie;
268 dst = __ip_vs_route_output_v6(net, &dest->addr.in6,
269 &dest->dst_saddr,
270 do_xfrm);
271 if (!dst) {
272 spin_unlock(&dest->dst_lock);
273 return NULL;
275 rt = (struct rt6_info *) dst;
276 cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
277 __ip_vs_dst_set(dest, 0, dst_clone(&rt->dst), cookie);
278 IP_VS_DBG(10, "new dst %pI6, src %pI6, refcnt=%d\n",
279 &dest->addr.in6, &dest->dst_saddr,
280 atomic_read(&rt->dst.__refcnt));
282 if (ret_saddr)
283 ipv6_addr_copy(ret_saddr, &dest->dst_saddr);
284 spin_unlock(&dest->dst_lock);
285 } else {
286 dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm);
287 if (!dst)
288 return NULL;
289 rt = (struct rt6_info *) dst;
292 local = __ip_vs_is_local_route6(rt);
293 if (!((local ? 1 : 2) & rt_mode)) {
294 IP_VS_DBG_RL("Stopping traffic to %s address, dest: %pI6\n",
295 local ? "local":"non-local", daddr);
296 dst_release(&rt->dst);
297 return NULL;
299 if (local && !(rt_mode & 4) &&
300 !((ort = (struct rt6_info *) skb_dst(skb)) &&
301 __ip_vs_is_local_route6(ort))) {
302 IP_VS_DBG_RL("Redirect from non-local address %pI6 to local "
303 "requires NAT method, dest: %pI6\n",
304 &ipv6_hdr(skb)->daddr, daddr);
305 dst_release(&rt->dst);
306 return NULL;
308 if (unlikely(!local && (!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
309 ipv6_addr_type(&ipv6_hdr(skb)->saddr) &
310 IPV6_ADDR_LOOPBACK)) {
311 IP_VS_DBG_RL("Stopping traffic from loopback address %pI6 "
312 "to non-local address, dest: %pI6\n",
313 &ipv6_hdr(skb)->saddr, daddr);
314 dst_release(&rt->dst);
315 return NULL;
318 return rt;
320 #endif
324 * Release dest->dst_cache before a dest is removed
326 void
327 ip_vs_dst_reset(struct ip_vs_dest *dest)
329 struct dst_entry *old_dst;
331 old_dst = dest->dst_cache;
332 dest->dst_cache = NULL;
333 dst_release(old_dst);
336 #define IP_VS_XMIT_TUNNEL(skb, cp) \
337 ({ \
338 int __ret = NF_ACCEPT; \
340 (skb)->ipvs_property = 1; \
341 if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \
342 __ret = ip_vs_confirm_conntrack(skb, cp); \
343 if (__ret == NF_ACCEPT) { \
344 nf_reset(skb); \
345 skb_forward_csum(skb); \
347 __ret; \
350 #define IP_VS_XMIT_NAT(pf, skb, cp, local) \
351 do { \
352 (skb)->ipvs_property = 1; \
353 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
354 ip_vs_notrack(skb); \
355 else \
356 ip_vs_update_conntrack(skb, cp, 1); \
357 if (local) \
358 return NF_ACCEPT; \
359 skb_forward_csum(skb); \
360 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
361 skb_dst(skb)->dev, dst_output); \
362 } while (0)
364 #define IP_VS_XMIT(pf, skb, cp, local) \
365 do { \
366 (skb)->ipvs_property = 1; \
367 if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \
368 ip_vs_notrack(skb); \
369 if (local) \
370 return NF_ACCEPT; \
371 skb_forward_csum(skb); \
372 NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \
373 skb_dst(skb)->dev, dst_output); \
374 } while (0)
378 * NULL transmitter (do nothing except return NF_ACCEPT)
381 ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
382 struct ip_vs_protocol *pp)
384 /* we do not touch skb and do not need pskb ptr */
385 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
390 * Bypass transmitter
391 * Let packets bypass the destination when the destination is not
392 * available, it may be only used in transparent cache cluster.
395 ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
396 struct ip_vs_protocol *pp)
398 struct rtable *rt; /* Route to the other host */
399 struct iphdr *iph = ip_hdr(skb);
400 int mtu;
402 EnterFunction(10);
404 if (!(rt = __ip_vs_get_out_rt(skb, NULL, iph->daddr,
405 RT_TOS(iph->tos), 2)))
406 goto tx_error_icmp;
408 /* MTU checking */
409 mtu = dst_mtu(&rt->dst);
410 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
411 !skb_is_gso(skb)) {
412 ip_rt_put(rt);
413 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
414 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
415 goto tx_error;
419 * Call ip_send_check because we are not sure it is called
420 * after ip_defrag. Is copy-on-write needed?
422 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
423 ip_rt_put(rt);
424 return NF_STOLEN;
426 ip_send_check(ip_hdr(skb));
428 /* drop old route */
429 skb_dst_drop(skb);
430 skb_dst_set(skb, &rt->dst);
432 /* Another hack: avoid icmp_send in ip_fragment */
433 skb->local_df = 1;
435 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
437 LeaveFunction(10);
438 return NF_STOLEN;
440 tx_error_icmp:
441 dst_link_failure(skb);
442 tx_error:
443 kfree_skb(skb);
444 LeaveFunction(10);
445 return NF_STOLEN;
448 #ifdef CONFIG_IP_VS_IPV6
450 ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
451 struct ip_vs_protocol *pp)
453 struct rt6_info *rt; /* Route to the other host */
454 struct ipv6hdr *iph = ipv6_hdr(skb);
455 int mtu;
457 EnterFunction(10);
459 if (!(rt = __ip_vs_get_out_rt_v6(skb, NULL, &iph->daddr, NULL, 0, 2)))
460 goto tx_error_icmp;
462 /* MTU checking */
463 mtu = dst_mtu(&rt->dst);
464 if (skb->len > mtu && !skb_is_gso(skb)) {
465 if (!skb->dev) {
466 struct net *net = dev_net(skb_dst(skb)->dev);
468 skb->dev = net->loopback_dev;
470 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
471 dst_release(&rt->dst);
472 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
473 goto tx_error;
477 * Call ip_send_check because we are not sure it is called
478 * after ip_defrag. Is copy-on-write needed?
480 skb = skb_share_check(skb, GFP_ATOMIC);
481 if (unlikely(skb == NULL)) {
482 dst_release(&rt->dst);
483 return NF_STOLEN;
486 /* drop old route */
487 skb_dst_drop(skb);
488 skb_dst_set(skb, &rt->dst);
490 /* Another hack: avoid icmp_send in ip_fragment */
491 skb->local_df = 1;
493 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
495 LeaveFunction(10);
496 return NF_STOLEN;
498 tx_error_icmp:
499 dst_link_failure(skb);
500 tx_error:
501 kfree_skb(skb);
502 LeaveFunction(10);
503 return NF_STOLEN;
505 #endif
508 * NAT transmitter (only for outside-to-inside nat forwarding)
509 * Not used for related ICMP
512 ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
513 struct ip_vs_protocol *pp)
515 struct rtable *rt; /* Route to the other host */
516 int mtu;
517 struct iphdr *iph = ip_hdr(skb);
518 int local;
520 EnterFunction(10);
522 /* check if it is a connection of no-client-port */
523 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
524 __be16 _pt, *p;
525 p = skb_header_pointer(skb, iph->ihl*4, sizeof(_pt), &_pt);
526 if (p == NULL)
527 goto tx_error;
528 ip_vs_conn_fill_cport(cp, *p);
529 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
532 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
533 RT_TOS(iph->tos), 1|2|4)))
534 goto tx_error_icmp;
535 local = rt->rt_flags & RTCF_LOCAL;
537 * Avoid duplicate tuple in reply direction for NAT traffic
538 * to local address when connection is sync-ed
540 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
541 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
542 enum ip_conntrack_info ctinfo;
543 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
545 if (ct && !nf_ct_is_untracked(ct)) {
546 IP_VS_DBG_RL_PKT(10, AF_INET, pp, skb, 0,
547 "ip_vs_nat_xmit(): "
548 "stopping DNAT to local address");
549 goto tx_error_put;
552 #endif
554 /* From world but DNAT to loopback address? */
555 if (local && ipv4_is_loopback(rt->rt_dst) &&
556 rt_is_input_route(skb_rtable(skb))) {
557 IP_VS_DBG_RL_PKT(1, AF_INET, pp, skb, 0, "ip_vs_nat_xmit(): "
558 "stopping DNAT to loopback address");
559 goto tx_error_put;
562 /* MTU checking */
563 mtu = dst_mtu(&rt->dst);
564 if ((skb->len > mtu) && (iph->frag_off & htons(IP_DF)) &&
565 !skb_is_gso(skb)) {
566 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
567 IP_VS_DBG_RL_PKT(0, AF_INET, pp, skb, 0,
568 "ip_vs_nat_xmit(): frag needed for");
569 goto tx_error_put;
572 /* copy-on-write the packet before mangling it */
573 if (!skb_make_writable(skb, sizeof(struct iphdr)))
574 goto tx_error_put;
576 if (skb_cow(skb, rt->dst.dev->hard_header_len))
577 goto tx_error_put;
579 /* mangle the packet */
580 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
581 goto tx_error_put;
582 ip_hdr(skb)->daddr = cp->daddr.ip;
583 ip_send_check(ip_hdr(skb));
585 if (!local) {
586 /* drop old route */
587 skb_dst_drop(skb);
588 skb_dst_set(skb, &rt->dst);
589 } else {
590 ip_rt_put(rt);
592 * Some IPv4 replies get local address from routes,
593 * not from iph, so while we DNAT after routing
594 * we need this second input/output route.
596 if (!__ip_vs_reroute_locally(skb))
597 goto tx_error;
600 IP_VS_DBG_PKT(10, AF_INET, pp, skb, 0, "After DNAT");
602 /* FIXME: when application helper enlarges the packet and the length
603 is larger than the MTU of outgoing device, there will be still
604 MTU problem. */
606 /* Another hack: avoid icmp_send in ip_fragment */
607 skb->local_df = 1;
609 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
611 LeaveFunction(10);
612 return NF_STOLEN;
614 tx_error_icmp:
615 dst_link_failure(skb);
616 tx_error:
617 kfree_skb(skb);
618 LeaveFunction(10);
619 return NF_STOLEN;
620 tx_error_put:
621 ip_rt_put(rt);
622 goto tx_error;
625 #ifdef CONFIG_IP_VS_IPV6
627 ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
628 struct ip_vs_protocol *pp)
630 struct rt6_info *rt; /* Route to the other host */
631 int mtu;
632 int local;
634 EnterFunction(10);
636 /* check if it is a connection of no-client-port */
637 if (unlikely(cp->flags & IP_VS_CONN_F_NO_CPORT)) {
638 __be16 _pt, *p;
639 p = skb_header_pointer(skb, sizeof(struct ipv6hdr),
640 sizeof(_pt), &_pt);
641 if (p == NULL)
642 goto tx_error;
643 ip_vs_conn_fill_cport(cp, *p);
644 IP_VS_DBG(10, "filled cport=%d\n", ntohs(*p));
647 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
648 0, 1|2|4)))
649 goto tx_error_icmp;
650 local = __ip_vs_is_local_route6(rt);
652 * Avoid duplicate tuple in reply direction for NAT traffic
653 * to local address when connection is sync-ed
655 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
656 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
657 enum ip_conntrack_info ctinfo;
658 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
660 if (ct && !nf_ct_is_untracked(ct)) {
661 IP_VS_DBG_RL_PKT(10, AF_INET6, pp, skb, 0,
662 "ip_vs_nat_xmit_v6(): "
663 "stopping DNAT to local address");
664 goto tx_error_put;
667 #endif
669 /* From world but DNAT to loopback address? */
670 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
671 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
672 IP_VS_DBG_RL_PKT(1, AF_INET6, pp, skb, 0,
673 "ip_vs_nat_xmit_v6(): "
674 "stopping DNAT to loopback address");
675 goto tx_error_put;
678 /* MTU checking */
679 mtu = dst_mtu(&rt->dst);
680 if (skb->len > mtu && !skb_is_gso(skb)) {
681 if (!skb->dev) {
682 struct net *net = dev_net(skb_dst(skb)->dev);
684 skb->dev = net->loopback_dev;
686 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
687 IP_VS_DBG_RL_PKT(0, AF_INET6, pp, skb, 0,
688 "ip_vs_nat_xmit_v6(): frag needed for");
689 goto tx_error_put;
692 /* copy-on-write the packet before mangling it */
693 if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
694 goto tx_error_put;
696 if (skb_cow(skb, rt->dst.dev->hard_header_len))
697 goto tx_error_put;
699 /* mangle the packet */
700 if (pp->dnat_handler && !pp->dnat_handler(skb, pp, cp))
701 goto tx_error;
702 ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &cp->daddr.in6);
704 if (!local || !skb->dev) {
705 /* drop the old route when skb is not shared */
706 skb_dst_drop(skb);
707 skb_dst_set(skb, &rt->dst);
708 } else {
709 /* destined to loopback, do we need to change route? */
710 dst_release(&rt->dst);
713 IP_VS_DBG_PKT(10, AF_INET6, pp, skb, 0, "After DNAT");
715 /* FIXME: when application helper enlarges the packet and the length
716 is larger than the MTU of outgoing device, there will be still
717 MTU problem. */
719 /* Another hack: avoid icmp_send in ip_fragment */
720 skb->local_df = 1;
722 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
724 LeaveFunction(10);
725 return NF_STOLEN;
727 tx_error_icmp:
728 dst_link_failure(skb);
729 tx_error:
730 LeaveFunction(10);
731 kfree_skb(skb);
732 return NF_STOLEN;
733 tx_error_put:
734 dst_release(&rt->dst);
735 goto tx_error;
737 #endif
741 * IP Tunneling transmitter
743 * This function encapsulates the packet in a new IP packet, its
744 * destination will be set to cp->daddr. Most code of this function
745 * is taken from ipip.c.
747 * It is used in VS/TUN cluster. The load balancer selects a real
748 * server from a cluster based on a scheduling algorithm,
749 * encapsulates the request packet and forwards it to the selected
750 * server. For example, all real servers are configured with
751 * "ifconfig tunl0 <Virtual IP Address> up". When the server receives
752 * the encapsulated packet, it will decapsulate the packet, processe
753 * the request and return the response packets directly to the client
754 * without passing the load balancer. This can greatly increase the
755 * scalability of virtual server.
757 * Used for ANY protocol
760 ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
761 struct ip_vs_protocol *pp)
763 struct rtable *rt; /* Route to the other host */
764 struct net_device *tdev; /* Device to other host */
765 struct iphdr *old_iph = ip_hdr(skb);
766 u8 tos = old_iph->tos;
767 __be16 df = old_iph->frag_off;
768 struct iphdr *iph; /* Our new IP header */
769 unsigned int max_headroom; /* The extra header space needed */
770 int mtu;
771 int ret;
773 EnterFunction(10);
775 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
776 RT_TOS(tos), 1|2)))
777 goto tx_error_icmp;
778 if (rt->rt_flags & RTCF_LOCAL) {
779 ip_rt_put(rt);
780 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
783 tdev = rt->dst.dev;
785 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
786 if (mtu < 68) {
787 IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
788 goto tx_error_put;
790 if (skb_dst(skb))
791 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
793 df |= (old_iph->frag_off & htons(IP_DF));
795 if ((old_iph->frag_off & htons(IP_DF) &&
796 mtu < ntohs(old_iph->tot_len) && !skb_is_gso(skb))) {
797 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
798 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
799 goto tx_error_put;
803 * Okay, now see if we can stuff it in the buffer as-is.
805 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
807 if (skb_headroom(skb) < max_headroom
808 || skb_cloned(skb) || skb_shared(skb)) {
809 struct sk_buff *new_skb =
810 skb_realloc_headroom(skb, max_headroom);
811 if (!new_skb) {
812 ip_rt_put(rt);
813 kfree_skb(skb);
814 IP_VS_ERR_RL("%s(): no memory\n", __func__);
815 return NF_STOLEN;
817 kfree_skb(skb);
818 skb = new_skb;
819 old_iph = ip_hdr(skb);
822 skb->transport_header = skb->network_header;
824 /* fix old IP header checksum */
825 ip_send_check(old_iph);
827 skb_push(skb, sizeof(struct iphdr));
828 skb_reset_network_header(skb);
829 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
831 /* drop old route */
832 skb_dst_drop(skb);
833 skb_dst_set(skb, &rt->dst);
836 * Push down and install the IPIP header.
838 iph = ip_hdr(skb);
839 iph->version = 4;
840 iph->ihl = sizeof(struct iphdr)>>2;
841 iph->frag_off = df;
842 iph->protocol = IPPROTO_IPIP;
843 iph->tos = tos;
844 iph->daddr = rt->rt_dst;
845 iph->saddr = rt->rt_src;
846 iph->ttl = old_iph->ttl;
847 ip_select_ident(iph, &rt->dst, NULL);
849 /* Another hack: avoid icmp_send in ip_fragment */
850 skb->local_df = 1;
852 ret = IP_VS_XMIT_TUNNEL(skb, cp);
853 if (ret == NF_ACCEPT)
854 ip_local_out(skb);
855 else if (ret == NF_DROP)
856 kfree_skb(skb);
858 LeaveFunction(10);
860 return NF_STOLEN;
862 tx_error_icmp:
863 dst_link_failure(skb);
864 tx_error:
865 kfree_skb(skb);
866 LeaveFunction(10);
867 return NF_STOLEN;
868 tx_error_put:
869 ip_rt_put(rt);
870 goto tx_error;
873 #ifdef CONFIG_IP_VS_IPV6
875 ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
876 struct ip_vs_protocol *pp)
878 struct rt6_info *rt; /* Route to the other host */
879 struct in6_addr saddr; /* Source for tunnel */
880 struct net_device *tdev; /* Device to other host */
881 struct ipv6hdr *old_iph = ipv6_hdr(skb);
882 struct ipv6hdr *iph; /* Our new IP header */
883 unsigned int max_headroom; /* The extra header space needed */
884 int mtu;
885 int ret;
887 EnterFunction(10);
889 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6,
890 &saddr, 1, 1|2)))
891 goto tx_error_icmp;
892 if (__ip_vs_is_local_route6(rt)) {
893 dst_release(&rt->dst);
894 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
897 tdev = rt->dst.dev;
899 mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
900 if (mtu < IPV6_MIN_MTU) {
901 IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
902 IPV6_MIN_MTU);
903 goto tx_error_put;
905 if (skb_dst(skb))
906 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
908 if (mtu < ntohs(old_iph->payload_len) + sizeof(struct ipv6hdr) &&
909 !skb_is_gso(skb)) {
910 if (!skb->dev) {
911 struct net *net = dev_net(skb_dst(skb)->dev);
913 skb->dev = net->loopback_dev;
915 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
916 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
917 goto tx_error_put;
921 * Okay, now see if we can stuff it in the buffer as-is.
923 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
925 if (skb_headroom(skb) < max_headroom
926 || skb_cloned(skb) || skb_shared(skb)) {
927 struct sk_buff *new_skb =
928 skb_realloc_headroom(skb, max_headroom);
929 if (!new_skb) {
930 dst_release(&rt->dst);
931 kfree_skb(skb);
932 IP_VS_ERR_RL("%s(): no memory\n", __func__);
933 return NF_STOLEN;
935 kfree_skb(skb);
936 skb = new_skb;
937 old_iph = ipv6_hdr(skb);
940 skb->transport_header = skb->network_header;
942 skb_push(skb, sizeof(struct ipv6hdr));
943 skb_reset_network_header(skb);
944 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
946 /* drop old route */
947 skb_dst_drop(skb);
948 skb_dst_set(skb, &rt->dst);
951 * Push down and install the IPIP header.
953 iph = ipv6_hdr(skb);
954 iph->version = 6;
955 iph->nexthdr = IPPROTO_IPV6;
956 iph->payload_len = old_iph->payload_len;
957 be16_add_cpu(&iph->payload_len, sizeof(*old_iph));
958 iph->priority = old_iph->priority;
959 memset(&iph->flow_lbl, 0, sizeof(iph->flow_lbl));
960 ipv6_addr_copy(&iph->daddr, &cp->daddr.in6);
961 ipv6_addr_copy(&iph->saddr, &saddr);
962 iph->hop_limit = old_iph->hop_limit;
964 /* Another hack: avoid icmp_send in ip_fragment */
965 skb->local_df = 1;
967 ret = IP_VS_XMIT_TUNNEL(skb, cp);
968 if (ret == NF_ACCEPT)
969 ip6_local_out(skb);
970 else if (ret == NF_DROP)
971 kfree_skb(skb);
973 LeaveFunction(10);
975 return NF_STOLEN;
977 tx_error_icmp:
978 dst_link_failure(skb);
979 tx_error:
980 kfree_skb(skb);
981 LeaveFunction(10);
982 return NF_STOLEN;
983 tx_error_put:
984 dst_release(&rt->dst);
985 goto tx_error;
987 #endif
991 * Direct Routing transmitter
992 * Used for ANY protocol
995 ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
996 struct ip_vs_protocol *pp)
998 struct rtable *rt; /* Route to the other host */
999 struct iphdr *iph = ip_hdr(skb);
1000 int mtu;
1002 EnterFunction(10);
1004 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1005 RT_TOS(iph->tos), 1|2)))
1006 goto tx_error_icmp;
1007 if (rt->rt_flags & RTCF_LOCAL) {
1008 ip_rt_put(rt);
1009 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1);
1012 /* MTU checking */
1013 mtu = dst_mtu(&rt->dst);
1014 if ((iph->frag_off & htons(IP_DF)) && skb->len > mtu &&
1015 !skb_is_gso(skb)) {
1016 icmp_send(skb, ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED, htonl(mtu));
1017 ip_rt_put(rt);
1018 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1019 goto tx_error;
1023 * Call ip_send_check because we are not sure it is called
1024 * after ip_defrag. Is copy-on-write needed?
1026 if (unlikely((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)) {
1027 ip_rt_put(rt);
1028 return NF_STOLEN;
1030 ip_send_check(ip_hdr(skb));
1032 /* drop old route */
1033 skb_dst_drop(skb);
1034 skb_dst_set(skb, &rt->dst);
1036 /* Another hack: avoid icmp_send in ip_fragment */
1037 skb->local_df = 1;
1039 IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 0);
1041 LeaveFunction(10);
1042 return NF_STOLEN;
1044 tx_error_icmp:
1045 dst_link_failure(skb);
1046 tx_error:
1047 kfree_skb(skb);
1048 LeaveFunction(10);
1049 return NF_STOLEN;
1052 #ifdef CONFIG_IP_VS_IPV6
1054 ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1055 struct ip_vs_protocol *pp)
1057 struct rt6_info *rt; /* Route to the other host */
1058 int mtu;
1060 EnterFunction(10);
1062 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1063 0, 1|2)))
1064 goto tx_error_icmp;
1065 if (__ip_vs_is_local_route6(rt)) {
1066 dst_release(&rt->dst);
1067 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 1);
1070 /* MTU checking */
1071 mtu = dst_mtu(&rt->dst);
1072 if (skb->len > mtu) {
1073 if (!skb->dev) {
1074 struct net *net = dev_net(skb_dst(skb)->dev);
1076 skb->dev = net->loopback_dev;
1078 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1079 dst_release(&rt->dst);
1080 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1081 goto tx_error;
1085 * Call ip_send_check because we are not sure it is called
1086 * after ip_defrag. Is copy-on-write needed?
1088 skb = skb_share_check(skb, GFP_ATOMIC);
1089 if (unlikely(skb == NULL)) {
1090 dst_release(&rt->dst);
1091 return NF_STOLEN;
1094 /* drop old route */
1095 skb_dst_drop(skb);
1096 skb_dst_set(skb, &rt->dst);
1098 /* Another hack: avoid icmp_send in ip_fragment */
1099 skb->local_df = 1;
1101 IP_VS_XMIT(NFPROTO_IPV6, skb, cp, 0);
1103 LeaveFunction(10);
1104 return NF_STOLEN;
1106 tx_error_icmp:
1107 dst_link_failure(skb);
1108 tx_error:
1109 kfree_skb(skb);
1110 LeaveFunction(10);
1111 return NF_STOLEN;
1113 #endif
1117 * ICMP packet transmitter
1118 * called by the ip_vs_in_icmp
1121 ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1122 struct ip_vs_protocol *pp, int offset)
1124 struct rtable *rt; /* Route to the other host */
1125 int mtu;
1126 int rc;
1127 int local;
1129 EnterFunction(10);
1131 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1132 forwarded directly here, because there is no need to
1133 translate address/port back */
1134 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1135 if (cp->packet_xmit)
1136 rc = cp->packet_xmit(skb, cp, pp);
1137 else
1138 rc = NF_ACCEPT;
1139 /* do not touch skb anymore */
1140 atomic_inc(&cp->in_pkts);
1141 goto out;
1145 * mangle and send the packet here (only for VS/NAT)
1148 if (!(rt = __ip_vs_get_out_rt(skb, cp->dest, cp->daddr.ip,
1149 RT_TOS(ip_hdr(skb)->tos), 1|2|4)))
1150 goto tx_error_icmp;
1151 local = rt->rt_flags & RTCF_LOCAL;
1154 * Avoid duplicate tuple in reply direction for NAT traffic
1155 * to local address when connection is sync-ed
1157 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1158 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1159 enum ip_conntrack_info ctinfo;
1160 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1162 if (ct && !nf_ct_is_untracked(ct)) {
1163 IP_VS_DBG(10, "%s(): "
1164 "stopping DNAT to local address %pI4\n",
1165 __func__, &cp->daddr.ip);
1166 goto tx_error_put;
1169 #endif
1171 /* From world but DNAT to loopback address? */
1172 if (local && ipv4_is_loopback(rt->rt_dst) &&
1173 rt_is_input_route(skb_rtable(skb))) {
1174 IP_VS_DBG(1, "%s(): "
1175 "stopping DNAT to loopback %pI4\n",
1176 __func__, &cp->daddr.ip);
1177 goto tx_error_put;
1180 /* MTU checking */
1181 mtu = dst_mtu(&rt->dst);
1182 if ((skb->len > mtu) && (ip_hdr(skb)->frag_off & htons(IP_DF)) &&
1183 !skb_is_gso(skb)) {
1184 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1185 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1186 goto tx_error_put;
1189 /* copy-on-write the packet before mangling it */
1190 if (!skb_make_writable(skb, offset))
1191 goto tx_error_put;
1193 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1194 goto tx_error_put;
1196 ip_vs_nat_icmp(skb, pp, cp, 0);
1198 if (!local) {
1199 /* drop the old route when skb is not shared */
1200 skb_dst_drop(skb);
1201 skb_dst_set(skb, &rt->dst);
1202 } else {
1203 ip_rt_put(rt);
1205 * Some IPv4 replies get local address from routes,
1206 * not from iph, so while we DNAT after routing
1207 * we need this second input/output route.
1209 if (!__ip_vs_reroute_locally(skb))
1210 goto tx_error;
1213 /* Another hack: avoid icmp_send in ip_fragment */
1214 skb->local_df = 1;
1216 IP_VS_XMIT_NAT(NFPROTO_IPV4, skb, cp, local);
1218 rc = NF_STOLEN;
1219 goto out;
1221 tx_error_icmp:
1222 dst_link_failure(skb);
1223 tx_error:
1224 dev_kfree_skb(skb);
1225 rc = NF_STOLEN;
1226 out:
1227 LeaveFunction(10);
1228 return rc;
1229 tx_error_put:
1230 ip_rt_put(rt);
1231 goto tx_error;
1234 #ifdef CONFIG_IP_VS_IPV6
1236 ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
1237 struct ip_vs_protocol *pp, int offset)
1239 struct rt6_info *rt; /* Route to the other host */
1240 int mtu;
1241 int rc;
1242 int local;
1244 EnterFunction(10);
1246 /* The ICMP packet for VS/TUN, VS/DR and LOCALNODE will be
1247 forwarded directly here, because there is no need to
1248 translate address/port back */
1249 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
1250 if (cp->packet_xmit)
1251 rc = cp->packet_xmit(skb, cp, pp);
1252 else
1253 rc = NF_ACCEPT;
1254 /* do not touch skb anymore */
1255 atomic_inc(&cp->in_pkts);
1256 goto out;
1260 * mangle and send the packet here (only for VS/NAT)
1263 if (!(rt = __ip_vs_get_out_rt_v6(skb, cp->dest, &cp->daddr.in6, NULL,
1264 0, 1|2|4)))
1265 goto tx_error_icmp;
1267 local = __ip_vs_is_local_route6(rt);
1269 * Avoid duplicate tuple in reply direction for NAT traffic
1270 * to local address when connection is sync-ed
1272 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
1273 if (cp->flags & IP_VS_CONN_F_SYNC && local) {
1274 enum ip_conntrack_info ctinfo;
1275 struct nf_conn *ct = ct = nf_ct_get(skb, &ctinfo);
1277 if (ct && !nf_ct_is_untracked(ct)) {
1278 IP_VS_DBG(10, "%s(): "
1279 "stopping DNAT to local address %pI6\n",
1280 __func__, &cp->daddr.in6);
1281 goto tx_error_put;
1284 #endif
1286 /* From world but DNAT to loopback address? */
1287 if (local && skb->dev && !(skb->dev->flags & IFF_LOOPBACK) &&
1288 ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LOOPBACK) {
1289 IP_VS_DBG(1, "%s(): "
1290 "stopping DNAT to loopback %pI6\n",
1291 __func__, &cp->daddr.in6);
1292 goto tx_error_put;
1295 /* MTU checking */
1296 mtu = dst_mtu(&rt->dst);
1297 if (skb->len > mtu && !skb_is_gso(skb)) {
1298 if (!skb->dev) {
1299 struct net *net = dev_net(skb_dst(skb)->dev);
1301 skb->dev = net->loopback_dev;
1303 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1304 IP_VS_DBG_RL("%s(): frag needed\n", __func__);
1305 goto tx_error_put;
1308 /* copy-on-write the packet before mangling it */
1309 if (!skb_make_writable(skb, offset))
1310 goto tx_error_put;
1312 if (skb_cow(skb, rt->dst.dev->hard_header_len))
1313 goto tx_error_put;
1315 ip_vs_nat_icmp_v6(skb, pp, cp, 0);
1317 if (!local || !skb->dev) {
1318 /* drop the old route when skb is not shared */
1319 skb_dst_drop(skb);
1320 skb_dst_set(skb, &rt->dst);
1321 } else {
1322 /* destined to loopback, do we need to change route? */
1323 dst_release(&rt->dst);
1326 /* Another hack: avoid icmp_send in ip_fragment */
1327 skb->local_df = 1;
1329 IP_VS_XMIT_NAT(NFPROTO_IPV6, skb, cp, local);
1331 rc = NF_STOLEN;
1332 goto out;
1334 tx_error_icmp:
1335 dst_link_failure(skb);
1336 tx_error:
1337 dev_kfree_skb(skb);
1338 rc = NF_STOLEN;
1339 out:
1340 LeaveFunction(10);
1341 return rc;
1342 tx_error_put:
1343 dst_release(&rt->dst);
1344 goto tx_error;
1346 #endif