2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the Netfilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
8 * Version: $Id: ip_vs_core.c,v 1.34 2003/05/10 03:05:23 wensong Exp $
10 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
11 * Peter Kese <peter.kese@ijs.si>
12 * Julian Anastasov <ja@ssi.bg>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
19 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
20 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
24 * Paul `Rusty' Russell properly handle non-linear skbs
25 * Harald Welte don't use nfcache
29 #include <linux/module.h>
30 #include <linux/kernel.h>
32 #include <linux/tcp.h>
33 #include <linux/icmp.h>
38 #include <net/icmp.h> /* for icmp_send */
39 #include <net/route.h>
41 #include <linux/netfilter.h>
42 #include <linux/netfilter_ipv4.h>
44 #include <net/ip_vs.h>
47 EXPORT_SYMBOL(register_ip_vs_scheduler);
48 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
49 EXPORT_SYMBOL(ip_vs_skb_replace);
50 EXPORT_SYMBOL(ip_vs_proto_name);
51 EXPORT_SYMBOL(ip_vs_conn_new);
52 EXPORT_SYMBOL(ip_vs_conn_in_get);
53 EXPORT_SYMBOL(ip_vs_conn_out_get);
54 #ifdef CONFIG_IP_VS_PROTO_TCP
55 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
57 EXPORT_SYMBOL(ip_vs_conn_put);
58 #ifdef CONFIG_IP_VS_DEBUG
59 EXPORT_SYMBOL(ip_vs_get_debug_level);
61 EXPORT_SYMBOL(ip_vs_make_skb_writable);
64 /* ID used in ICMP lookups */
65 #define icmp_id(icmph) (((icmph)->un).echo.id)
67 const char *ip_vs_proto_name(unsigned proto)
81 sprintf(buf, "IP_%d", proto);
86 void ip_vs_init_hash_table(struct list_head *table, int rows)
89 INIT_LIST_HEAD(&table[rows]);
93 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
95 struct ip_vs_dest *dest = cp->dest;
96 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
97 spin_lock(&dest->stats.lock);
99 dest->stats.inbytes += skb->len;
100 spin_unlock(&dest->stats.lock);
102 spin_lock(&dest->svc->stats.lock);
103 dest->svc->stats.inpkts++;
104 dest->svc->stats.inbytes += skb->len;
105 spin_unlock(&dest->svc->stats.lock);
107 spin_lock(&ip_vs_stats.lock);
108 ip_vs_stats.inpkts++;
109 ip_vs_stats.inbytes += skb->len;
110 spin_unlock(&ip_vs_stats.lock);
116 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
118 struct ip_vs_dest *dest = cp->dest;
119 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
120 spin_lock(&dest->stats.lock);
121 dest->stats.outpkts++;
122 dest->stats.outbytes += skb->len;
123 spin_unlock(&dest->stats.lock);
125 spin_lock(&dest->svc->stats.lock);
126 dest->svc->stats.outpkts++;
127 dest->svc->stats.outbytes += skb->len;
128 spin_unlock(&dest->svc->stats.lock);
130 spin_lock(&ip_vs_stats.lock);
131 ip_vs_stats.outpkts++;
132 ip_vs_stats.outbytes += skb->len;
133 spin_unlock(&ip_vs_stats.lock);
139 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
141 spin_lock(&cp->dest->stats.lock);
142 cp->dest->stats.conns++;
143 spin_unlock(&cp->dest->stats.lock);
145 spin_lock(&svc->stats.lock);
147 spin_unlock(&svc->stats.lock);
149 spin_lock(&ip_vs_stats.lock);
151 spin_unlock(&ip_vs_stats.lock);
156 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
157 const struct sk_buff *skb,
158 struct ip_vs_protocol *pp)
160 if (unlikely(!pp->state_transition))
162 return pp->state_transition(cp, direction, skb, pp);
166 int ip_vs_make_skb_writable(struct sk_buff **pskb, int writable_len)
168 struct sk_buff *skb = *pskb;
170 /* skb is already used, better copy skb and its payload */
171 if (unlikely(skb_shared(skb) || skb->sk))
174 /* skb data is already used, copy it */
175 if (unlikely(skb_cloned(skb)))
178 return pskb_may_pull(skb, writable_len);
181 if (unlikely(writable_len > skb->len))
183 return !pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
186 if (unlikely(writable_len > skb->len))
188 skb = skb_copy(skb, GFP_ATOMIC);
191 BUG_ON(skb_is_nonlinear(skb));
193 /* Rest of kernel will get very unhappy if we pass it a
194 suddenly-orphaned skbuff */
196 skb_set_owner_w(skb, (*pskb)->sk);
203 * IPVS persistent scheduling function
204 * It creates a connection entry according to its template if exists,
205 * or selects a server and creates a connection entry plus a template.
206 * Locking: we are svc user (svc->refcnt), so we hold all dests too
207 * Protocols supported: TCP, UDP
209 static struct ip_vs_conn *
210 ip_vs_sched_persist(struct ip_vs_service *svc,
211 const struct sk_buff *skb,
214 struct ip_vs_conn *cp = NULL;
215 struct iphdr *iph = ip_hdr(skb);
216 struct ip_vs_dest *dest;
217 struct ip_vs_conn *ct;
218 __be16 dport; /* destination port to forward */
219 __be32 snet; /* source network of the client, after masking */
221 /* Mask saddr with the netmask to adjust template granularity */
222 snet = iph->saddr & svc->netmask;
224 IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u "
225 "mnet %u.%u.%u.%u\n",
226 NIPQUAD(iph->saddr), ntohs(ports[0]),
227 NIPQUAD(iph->daddr), ntohs(ports[1]),
231 * As far as we know, FTP is a very complicated network protocol, and
232 * it uses control connection and data connections. For active FTP,
233 * FTP server initialize data connection to the client, its source port
234 * is often 20. For passive FTP, FTP server tells the clients the port
235 * that it passively listens to, and the client issues the data
236 * connection. In the tunneling or direct routing mode, the load
237 * balancer is on the client-to-server half of connection, the port
238 * number is unknown to the load balancer. So, a conn template like
239 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
240 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
241 * is created for other persistent services.
243 if (ports[1] == svc->port) {
244 /* Check if a template already exists */
245 if (svc->port != FTPPORT)
246 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
247 iph->daddr, ports[1]);
249 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
252 if (!ct || !ip_vs_check_template(ct)) {
254 * No template found or the dest of the connection
255 * template is not available.
257 dest = svc->scheduler->schedule(svc, skb);
259 IP_VS_DBG(1, "p-schedule: no dest found.\n");
264 * Create a template like <protocol,caddr,0,
265 * vaddr,vport,daddr,dport> for non-ftp service,
266 * and <protocol,caddr,0,vaddr,0,daddr,0>
269 if (svc->port != FTPPORT)
270 ct = ip_vs_conn_new(iph->protocol,
274 dest->addr, dest->port,
275 IP_VS_CONN_F_TEMPLATE,
278 ct = ip_vs_conn_new(iph->protocol,
282 IP_VS_CONN_F_TEMPLATE,
287 ct->timeout = svc->timeout;
289 /* set destination with the found template */
295 * Note: persistent fwmark-based services and persistent
296 * port zero service are handled here.
297 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
298 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
301 ct = ip_vs_ct_in_get(IPPROTO_IP, snet, 0,
302 htonl(svc->fwmark), 0);
304 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
307 if (!ct || !ip_vs_check_template(ct)) {
309 * If it is not persistent port zero, return NULL,
310 * otherwise create a connection template.
315 dest = svc->scheduler->schedule(svc, skb);
317 IP_VS_DBG(1, "p-schedule: no dest found.\n");
322 * Create a template according to the service
325 ct = ip_vs_conn_new(IPPROTO_IP,
327 htonl(svc->fwmark), 0,
329 IP_VS_CONN_F_TEMPLATE,
332 ct = ip_vs_conn_new(iph->protocol,
336 IP_VS_CONN_F_TEMPLATE,
341 ct->timeout = svc->timeout;
343 /* set destination with the found template */
350 * Create a new connection according to the template
352 cp = ip_vs_conn_new(iph->protocol,
353 iph->saddr, ports[0],
354 iph->daddr, ports[1],
366 ip_vs_control_add(cp, ct);
369 ip_vs_conn_stats(cp, svc);
375 * IPVS main scheduling function
376 * It selects a server according to the virtual service, and
377 * creates a connection entry.
378 * Protocols supported: TCP, UDP
381 ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
383 struct ip_vs_conn *cp = NULL;
384 struct iphdr *iph = ip_hdr(skb);
385 struct ip_vs_dest *dest;
386 __be16 _ports[2], *pptr;
388 pptr = skb_header_pointer(skb, iph->ihl*4,
389 sizeof(_ports), _ports);
396 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
397 return ip_vs_sched_persist(svc, skb, pptr);
400 * Non-persistent service
402 if (!svc->fwmark && pptr[1] != svc->port) {
404 IP_VS_ERR("Schedule: port zero only supported "
405 "in persistent services, "
406 "check your ipvs configuration\n");
410 dest = svc->scheduler->schedule(svc, skb);
412 IP_VS_DBG(1, "Schedule: no dest found.\n");
417 * Create a connection entry.
419 cp = ip_vs_conn_new(iph->protocol,
422 dest->addr, dest->port?dest->port:pptr[1],
428 IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u "
429 "d:%u.%u.%u.%u:%u conn->flags:%X conn->refcnt:%d\n",
431 NIPQUAD(cp->caddr), ntohs(cp->cport),
432 NIPQUAD(cp->vaddr), ntohs(cp->vport),
433 NIPQUAD(cp->daddr), ntohs(cp->dport),
434 cp->flags, atomic_read(&cp->refcnt));
436 ip_vs_conn_stats(cp, svc);
442 * Pass or drop the packet.
443 * Called by ip_vs_in, when the virtual service is available but
444 * no destination is available for a new connection.
446 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
447 struct ip_vs_protocol *pp)
449 __be16 _ports[2], *pptr;
450 struct iphdr *iph = ip_hdr(skb);
452 pptr = skb_header_pointer(skb, iph->ihl*4,
453 sizeof(_ports), _ports);
455 ip_vs_service_put(svc);
459 /* if it is fwmark-based service, the cache_bypass sysctl is up
460 and the destination is RTN_UNICAST (and not local), then create
461 a cache_bypass connection entry */
462 if (sysctl_ip_vs_cache_bypass && svc->fwmark
463 && (inet_addr_type(iph->daddr) == RTN_UNICAST)) {
465 struct ip_vs_conn *cp;
467 ip_vs_service_put(svc);
469 /* create a new connection entry */
470 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
471 cp = ip_vs_conn_new(iph->protocol,
481 ip_vs_in_stats(cp, skb);
484 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
486 /* transmit the first SYN packet */
487 ret = cp->packet_xmit(skb, cp, pp);
488 /* do not touch skb anymore */
490 atomic_inc(&cp->in_pkts);
496 * When the virtual ftp service is presented, packets destined
497 * for other services on the VIP may get here (except services
498 * listed in the ipvs table), pass the packets, because it is
499 * not ipvs job to decide to drop the packets.
501 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
502 ip_vs_service_put(svc);
506 ip_vs_service_put(svc);
509 * Notify the client that the destination is unreachable, and
510 * release the socket buffer.
511 * Since it is in IP layer, the TCP socket is not actually
512 * created, the TCP RST packet cannot be sent, instead that
513 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
515 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
521 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_IP_POST_ROUTING
522 * chain, and is used for VS/NAT.
523 * It detects packets for VS/NAT connections and sends the packets
524 * immediately. This can avoid that iptable_nat mangles the packets
527 static unsigned int ip_vs_post_routing(unsigned int hooknum,
528 struct sk_buff **pskb,
529 const struct net_device *in,
530 const struct net_device *out,
531 int (*okfn)(struct sk_buff *))
533 if (!((*pskb)->ipvs_property))
535 /* The packet was sent from IPVS, exit this chain */
539 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
541 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
544 static inline struct sk_buff *
545 ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
547 skb = ip_defrag(skb, user);
549 ip_send_check(ip_hdr(skb));
554 * Packet has been made sufficiently writable in caller
555 * - inout: 1=in->out, 0=out->in
557 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
558 struct ip_vs_conn *cp, int inout)
560 struct iphdr *iph = ip_hdr(skb);
561 unsigned int icmp_offset = iph->ihl*4;
562 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
564 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
567 iph->saddr = cp->vaddr;
569 ciph->daddr = cp->vaddr;
572 iph->daddr = cp->daddr;
574 ciph->saddr = cp->daddr;
578 /* the TCP/UDP port */
579 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
580 __be16 *ports = (void *)ciph + ciph->ihl*4;
583 ports[1] = cp->vport;
585 ports[0] = cp->dport;
588 /* And finally the ICMP checksum */
590 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
591 skb->ip_summed = CHECKSUM_UNNECESSARY;
594 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
595 "Forwarding altered outgoing ICMP");
597 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
598 "Forwarding altered incoming ICMP");
602 * Handle ICMP messages in the inside-to-outside direction (outgoing).
603 * Find any that might be relevant, check against existing connections,
604 * forward to the right destination host if relevant.
605 * Currently handles error types - unreachable, quench, ttl exceeded.
606 * (Only used in VS/NAT)
608 static int ip_vs_out_icmp(struct sk_buff **pskb, int *related)
610 struct sk_buff *skb = *pskb;
612 struct icmphdr _icmph, *ic;
613 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
614 struct ip_vs_conn *cp;
615 struct ip_vs_protocol *pp;
616 unsigned int offset, ihl, verdict;
620 /* reassemble IP fragments */
621 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
622 skb = ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT);
629 offset = ihl = iph->ihl * 4;
630 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
634 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
635 ic->type, ntohs(icmp_id(ic)),
636 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
639 * Work through seeing if this is for us.
640 * These checks are supposed to be in an order that means easy
641 * things are checked first to speed up processing.... however
642 * this means that some packets will manage to get a long way
643 * down this stack and then be rejected, but that's life.
645 if ((ic->type != ICMP_DEST_UNREACH) &&
646 (ic->type != ICMP_SOURCE_QUENCH) &&
647 (ic->type != ICMP_TIME_EXCEEDED)) {
652 /* Now find the contained IP header */
653 offset += sizeof(_icmph);
654 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
656 return NF_ACCEPT; /* The packet looks wrong, ignore */
658 pp = ip_vs_proto_get(cih->protocol);
662 /* Is the embedded protocol header present? */
663 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
667 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
669 offset += cih->ihl * 4;
671 /* The embedded headers contain source and dest in reverse order */
672 cp = pp->conn_out_get(skb, pp, cih, offset, 1);
678 if (IP_VS_FWD_METHOD(cp) != 0) {
679 IP_VS_ERR("shouldn't reach here, because the box is on the"
680 "half connection in the tun/dr module.\n");
683 /* Ensure the checksum is correct */
684 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
685 /* Failed checksum! */
686 IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
687 NIPQUAD(iph->saddr));
691 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
692 offset += 2 * sizeof(__u16);
693 if (!ip_vs_make_skb_writable(pskb, offset))
697 ip_vs_nat_icmp(skb, pp, cp, 1);
699 /* do the statistics and put it back */
700 ip_vs_out_stats(cp, skb);
702 skb->ipvs_property = 1;
706 __ip_vs_conn_put(cp);
711 static inline int is_tcp_reset(const struct sk_buff *skb)
713 struct tcphdr _tcph, *th;
715 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
722 * It is hooked at the NF_IP_FORWARD chain, used only for VS/NAT.
723 * Check if outgoing packet belongs to the established ip_vs_conn,
724 * rewrite addresses of the packet and send it on its way...
727 ip_vs_out(unsigned int hooknum, struct sk_buff **pskb,
728 const struct net_device *in, const struct net_device *out,
729 int (*okfn)(struct sk_buff *))
731 struct sk_buff *skb = *pskb;
733 struct ip_vs_protocol *pp;
734 struct ip_vs_conn *cp;
739 if (skb->ipvs_property)
743 if (unlikely(iph->protocol == IPPROTO_ICMP)) {
744 int related, verdict = ip_vs_out_icmp(pskb, &related);
752 pp = ip_vs_proto_get(iph->protocol);
756 /* reassemble IP fragments */
757 if (unlikely(iph->frag_off & htons(IP_MF|IP_OFFSET) &&
759 skb = ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT);
769 * Check if the packet belongs to an existing entry
771 cp = pp->conn_out_get(skb, pp, iph, ihl, 0);
774 if (sysctl_ip_vs_nat_icmp_send &&
775 (pp->protocol == IPPROTO_TCP ||
776 pp->protocol == IPPROTO_UDP)) {
777 __be16 _ports[2], *pptr;
779 pptr = skb_header_pointer(skb, ihl,
780 sizeof(_ports), _ports);
782 return NF_ACCEPT; /* Not for me */
783 if (ip_vs_lookup_real_service(iph->protocol,
784 iph->saddr, pptr[0])) {
786 * Notify the real server: there is no
787 * existing entry if it is not RST
788 * packet or not TCP packet.
790 if (iph->protocol != IPPROTO_TCP
791 || !is_tcp_reset(skb)) {
792 icmp_send(skb,ICMP_DEST_UNREACH,
793 ICMP_PORT_UNREACH, 0);
798 IP_VS_DBG_PKT(12, pp, skb, 0,
799 "packet continues traversal as normal");
803 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
805 if (!ip_vs_make_skb_writable(pskb, ihl))
808 /* mangle the packet */
809 if (pp->snat_handler && !pp->snat_handler(pskb, pp, cp))
812 ip_hdr(skb)->saddr = cp->vaddr;
813 ip_send_check(ip_hdr(skb));
815 /* For policy routing, packets originating from this
816 * machine itself may be routed differently to packets
817 * passing through. We want this packet to be routed as
818 * if it came from this machine itself. So re-compute
819 * the routing information.
821 if (ip_route_me_harder(pskb, RTN_LOCAL) != 0)
825 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
827 ip_vs_out_stats(cp, skb);
828 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
831 skb->ipvs_property = 1;
844 * Handle ICMP messages in the outside-to-inside direction (incoming).
845 * Find any that might be relevant, check against existing connections,
846 * forward to the right destination host if relevant.
847 * Currently handles error types - unreachable, quench, ttl exceeded.
850 ip_vs_in_icmp(struct sk_buff **pskb, int *related, unsigned int hooknum)
852 struct sk_buff *skb = *pskb;
854 struct icmphdr _icmph, *ic;
855 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
856 struct ip_vs_conn *cp;
857 struct ip_vs_protocol *pp;
858 unsigned int offset, ihl, verdict;
862 /* reassemble IP fragments */
863 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
864 skb = ip_vs_gather_frags(skb,
865 hooknum == NF_IP_LOCAL_IN ?
866 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD);
873 offset = ihl = iph->ihl * 4;
874 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
878 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
879 ic->type, ntohs(icmp_id(ic)),
880 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
883 * Work through seeing if this is for us.
884 * These checks are supposed to be in an order that means easy
885 * things are checked first to speed up processing.... however
886 * this means that some packets will manage to get a long way
887 * down this stack and then be rejected, but that's life.
889 if ((ic->type != ICMP_DEST_UNREACH) &&
890 (ic->type != ICMP_SOURCE_QUENCH) &&
891 (ic->type != ICMP_TIME_EXCEEDED)) {
896 /* Now find the contained IP header */
897 offset += sizeof(_icmph);
898 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
900 return NF_ACCEPT; /* The packet looks wrong, ignore */
902 pp = ip_vs_proto_get(cih->protocol);
906 /* Is the embedded protocol header present? */
907 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
911 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
913 offset += cih->ihl * 4;
915 /* The embedded headers contain source and dest in reverse order */
916 cp = pp->conn_in_get(skb, pp, cih, offset, 1);
922 /* Ensure the checksum is correct */
923 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
924 /* Failed checksum! */
925 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
926 NIPQUAD(iph->saddr));
930 /* do the statistics and put it back */
931 ip_vs_in_stats(cp, skb);
932 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
933 offset += 2 * sizeof(__u16);
934 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
935 /* do not touch skb anymore */
938 __ip_vs_conn_put(cp);
944 * Check if it's for virtual services, look it up,
945 * and send it on its way...
948 ip_vs_in(unsigned int hooknum, struct sk_buff **pskb,
949 const struct net_device *in, const struct net_device *out,
950 int (*okfn)(struct sk_buff *))
952 struct sk_buff *skb = *pskb;
954 struct ip_vs_protocol *pp;
955 struct ip_vs_conn *cp;
960 * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
961 * ... don't know why 1st test DOES NOT include 2nd (?)
963 if (unlikely(skb->pkt_type != PACKET_HOST
964 || skb->dev->flags & IFF_LOOPBACK || skb->sk)) {
965 IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n",
967 ip_hdr(skb)->protocol,
968 NIPQUAD(ip_hdr(skb)->daddr));
973 if (unlikely(iph->protocol == IPPROTO_ICMP)) {
974 int related, verdict = ip_vs_in_icmp(pskb, &related, hooknum);
982 /* Protocol supported? */
983 pp = ip_vs_proto_get(iph->protocol);
990 * Check if the packet belongs to an existing connection entry
992 cp = pp->conn_in_get(skb, pp, iph, ihl, 0);
997 if (!pp->conn_schedule(skb, pp, &v, &cp))
1001 if (unlikely(!cp)) {
1002 /* sorry, all this trouble for a no-hit :) */
1003 IP_VS_DBG_PKT(12, pp, skb, 0,
1004 "packet continues traversal as normal");
1008 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1010 /* Check the server status */
1011 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1012 /* the destination server is not available */
1014 if (sysctl_ip_vs_expire_nodest_conn) {
1015 /* try to expire the connection immediately */
1016 ip_vs_conn_expire_now(cp);
1018 /* don't restart its timer, and silently
1020 __ip_vs_conn_put(cp);
1024 ip_vs_in_stats(cp, skb);
1025 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1026 if (cp->packet_xmit)
1027 ret = cp->packet_xmit(skb, cp, pp);
1028 /* do not touch skb anymore */
1030 IP_VS_DBG_RL("warning: packet_xmit is null");
1034 /* increase its packet counter and check if it is needed
1035 to be synchronized */
1036 atomic_inc(&cp->in_pkts);
1037 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) &&
1038 (cp->protocol != IPPROTO_TCP ||
1039 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1040 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
1041 == sysctl_ip_vs_sync_threshold[0]))
1042 ip_vs_sync_conn(cp);
1050 * It is hooked at the NF_IP_FORWARD chain, in order to catch ICMP
1051 * related packets destined for 0.0.0.0/0.
1052 * When fwmark-based virtual service is used, such as transparent
1053 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1054 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1055 * sent to ip_vs_in_icmp. So, catch them at the NF_IP_FORWARD chain
1056 * and send them to ip_vs_in_icmp.
1059 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff **pskb,
1060 const struct net_device *in, const struct net_device *out,
1061 int (*okfn)(struct sk_buff *))
1065 if (ip_hdr(*pskb)->protocol != IPPROTO_ICMP)
1068 return ip_vs_in_icmp(pskb, &r, hooknum);
1072 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1073 or VS/NAT(change destination), so that filtering rules can be
1075 static struct nf_hook_ops ip_vs_in_ops = {
1077 .owner = THIS_MODULE,
1079 .hooknum = NF_IP_LOCAL_IN,
1083 /* After packet filtering, change source only for VS/NAT */
1084 static struct nf_hook_ops ip_vs_out_ops = {
1086 .owner = THIS_MODULE,
1088 .hooknum = NF_IP_FORWARD,
1092 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1093 destined for 0.0.0.0/0, which is for incoming IPVS connections */
1094 static struct nf_hook_ops ip_vs_forward_icmp_ops = {
1095 .hook = ip_vs_forward_icmp,
1096 .owner = THIS_MODULE,
1098 .hooknum = NF_IP_FORWARD,
1102 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1103 static struct nf_hook_ops ip_vs_post_routing_ops = {
1104 .hook = ip_vs_post_routing,
1105 .owner = THIS_MODULE,
1107 .hooknum = NF_IP_POST_ROUTING,
1108 .priority = NF_IP_PRI_NAT_SRC-1,
1113 * Initialize IP Virtual Server
1115 static int __init ip_vs_init(void)
1119 ret = ip_vs_control_init();
1121 IP_VS_ERR("can't setup control.\n");
1122 goto cleanup_nothing;
1125 ip_vs_protocol_init();
1127 ret = ip_vs_app_init();
1129 IP_VS_ERR("can't setup application helper.\n");
1130 goto cleanup_protocol;
1133 ret = ip_vs_conn_init();
1135 IP_VS_ERR("can't setup connection table.\n");
1139 ret = nf_register_hook(&ip_vs_in_ops);
1141 IP_VS_ERR("can't register in hook.\n");
1145 ret = nf_register_hook(&ip_vs_out_ops);
1147 IP_VS_ERR("can't register out hook.\n");
1150 ret = nf_register_hook(&ip_vs_post_routing_ops);
1152 IP_VS_ERR("can't register post_routing hook.\n");
1153 goto cleanup_outops;
1155 ret = nf_register_hook(&ip_vs_forward_icmp_ops);
1157 IP_VS_ERR("can't register forward_icmp hook.\n");
1158 goto cleanup_postroutingops;
1161 IP_VS_INFO("ipvs loaded.\n");
1164 cleanup_postroutingops:
1165 nf_unregister_hook(&ip_vs_post_routing_ops);
1167 nf_unregister_hook(&ip_vs_out_ops);
1169 nf_unregister_hook(&ip_vs_in_ops);
1171 ip_vs_conn_cleanup();
1173 ip_vs_app_cleanup();
1175 ip_vs_protocol_cleanup();
1176 ip_vs_control_cleanup();
1181 static void __exit ip_vs_cleanup(void)
1183 nf_unregister_hook(&ip_vs_forward_icmp_ops);
1184 nf_unregister_hook(&ip_vs_post_routing_ops);
1185 nf_unregister_hook(&ip_vs_out_ops);
1186 nf_unregister_hook(&ip_vs_in_ops);
1187 ip_vs_conn_cleanup();
1188 ip_vs_app_cleanup();
1189 ip_vs_protocol_cleanup();
1190 ip_vs_control_cleanup();
1191 IP_VS_INFO("ipvs unloaded.\n");
1194 module_init(ip_vs_init);
1195 module_exit(ip_vs_cleanup);
1196 MODULE_LICENSE("GPL");