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 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
18 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
22 * Paul `Rusty' Russell properly handle non-linear skbs
23 * Harald Welte don't use nfcache
27 #define KMSG_COMPONENT "IPVS"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30 #include <linux/module.h>
31 #include <linux/kernel.h>
33 #include <linux/tcp.h>
34 #include <linux/sctp.h>
35 #include <linux/icmp.h>
36 #include <linux/slab.h>
41 #include <net/icmp.h> /* for icmp_send */
42 #include <net/route.h>
44 #include <linux/netfilter.h>
45 #include <linux/netfilter_ipv4.h>
47 #ifdef CONFIG_IP_VS_IPV6
49 #include <linux/netfilter_ipv6.h>
52 #include <net/ip_vs.h>
55 EXPORT_SYMBOL(register_ip_vs_scheduler);
56 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
57 EXPORT_SYMBOL(ip_vs_skb_replace);
58 EXPORT_SYMBOL(ip_vs_proto_name);
59 EXPORT_SYMBOL(ip_vs_conn_new);
60 EXPORT_SYMBOL(ip_vs_conn_in_get);
61 EXPORT_SYMBOL(ip_vs_conn_out_get);
62 #ifdef CONFIG_IP_VS_PROTO_TCP
63 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
65 EXPORT_SYMBOL(ip_vs_conn_put);
66 #ifdef CONFIG_IP_VS_DEBUG
67 EXPORT_SYMBOL(ip_vs_get_debug_level);
71 /* ID used in ICMP lookups */
72 #define icmp_id(icmph) (((icmph)->un).echo.id)
73 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
75 const char *ip_vs_proto_name(unsigned proto)
90 #ifdef CONFIG_IP_VS_IPV6
95 sprintf(buf, "IP_%d", proto);
100 void ip_vs_init_hash_table(struct list_head *table, int rows)
103 INIT_LIST_HEAD(&table[rows]);
107 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
109 struct ip_vs_dest *dest = cp->dest;
110 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
111 spin_lock(&dest->stats.lock);
112 dest->stats.ustats.inpkts++;
113 dest->stats.ustats.inbytes += skb->len;
114 spin_unlock(&dest->stats.lock);
116 spin_lock(&dest->svc->stats.lock);
117 dest->svc->stats.ustats.inpkts++;
118 dest->svc->stats.ustats.inbytes += skb->len;
119 spin_unlock(&dest->svc->stats.lock);
121 spin_lock(&ip_vs_stats.lock);
122 ip_vs_stats.ustats.inpkts++;
123 ip_vs_stats.ustats.inbytes += skb->len;
124 spin_unlock(&ip_vs_stats.lock);
130 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
132 struct ip_vs_dest *dest = cp->dest;
133 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
134 spin_lock(&dest->stats.lock);
135 dest->stats.ustats.outpkts++;
136 dest->stats.ustats.outbytes += skb->len;
137 spin_unlock(&dest->stats.lock);
139 spin_lock(&dest->svc->stats.lock);
140 dest->svc->stats.ustats.outpkts++;
141 dest->svc->stats.ustats.outbytes += skb->len;
142 spin_unlock(&dest->svc->stats.lock);
144 spin_lock(&ip_vs_stats.lock);
145 ip_vs_stats.ustats.outpkts++;
146 ip_vs_stats.ustats.outbytes += skb->len;
147 spin_unlock(&ip_vs_stats.lock);
153 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
155 spin_lock(&cp->dest->stats.lock);
156 cp->dest->stats.ustats.conns++;
157 spin_unlock(&cp->dest->stats.lock);
159 spin_lock(&svc->stats.lock);
160 svc->stats.ustats.conns++;
161 spin_unlock(&svc->stats.lock);
163 spin_lock(&ip_vs_stats.lock);
164 ip_vs_stats.ustats.conns++;
165 spin_unlock(&ip_vs_stats.lock);
170 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
171 const struct sk_buff *skb,
172 struct ip_vs_protocol *pp)
174 if (unlikely(!pp->state_transition))
176 return pp->state_transition(cp, direction, skb, pp);
181 * IPVS persistent scheduling function
182 * It creates a connection entry according to its template if exists,
183 * or selects a server and creates a connection entry plus a template.
184 * Locking: we are svc user (svc->refcnt), so we hold all dests too
185 * Protocols supported: TCP, UDP
187 static struct ip_vs_conn *
188 ip_vs_sched_persist(struct ip_vs_service *svc,
189 const struct sk_buff *skb,
192 struct ip_vs_conn *cp = NULL;
193 struct ip_vs_iphdr iph;
194 struct ip_vs_dest *dest;
195 struct ip_vs_conn *ct;
196 __be16 dport; /* destination port to forward */
197 union nf_inet_addr snet; /* source network of the client,
200 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
202 /* Mask saddr with the netmask to adjust template granularity */
203 #ifdef CONFIG_IP_VS_IPV6
204 if (svc->af == AF_INET6)
205 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
208 snet.ip = iph.saddr.ip & svc->netmask;
210 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
212 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(ports[0]),
213 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(ports[1]),
214 IP_VS_DBG_ADDR(svc->af, &snet));
217 * As far as we know, FTP is a very complicated network protocol, and
218 * it uses control connection and data connections. For active FTP,
219 * FTP server initialize data connection to the client, its source port
220 * is often 20. For passive FTP, FTP server tells the clients the port
221 * that it passively listens to, and the client issues the data
222 * connection. In the tunneling or direct routing mode, the load
223 * balancer is on the client-to-server half of connection, the port
224 * number is unknown to the load balancer. So, a conn template like
225 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
226 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
227 * is created for other persistent services.
229 if (ports[1] == svc->port) {
230 /* Check if a template already exists */
231 if (svc->port != FTPPORT)
232 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
233 &iph.daddr, ports[1]);
235 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
238 if (!ct || !ip_vs_check_template(ct)) {
240 * No template found or the dest of the connection
241 * template is not available.
243 dest = svc->scheduler->schedule(svc, skb);
245 IP_VS_DBG(1, "p-schedule: no dest found.\n");
250 * Create a template like <protocol,caddr,0,
251 * vaddr,vport,daddr,dport> for non-ftp service,
252 * and <protocol,caddr,0,vaddr,0,daddr,0>
255 if (svc->port != FTPPORT)
256 ct = ip_vs_conn_new(svc->af, iph.protocol,
260 &dest->addr, dest->port,
261 IP_VS_CONN_F_TEMPLATE,
264 ct = ip_vs_conn_new(svc->af, iph.protocol,
268 IP_VS_CONN_F_TEMPLATE,
273 ct->timeout = svc->timeout;
275 /* set destination with the found template */
281 * Note: persistent fwmark-based services and persistent
282 * port zero service are handled here.
283 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
284 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
287 union nf_inet_addr fwmark = {
288 .ip = htonl(svc->fwmark)
291 ct = ip_vs_ct_in_get(svc->af, IPPROTO_IP, &snet, 0,
294 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
297 if (!ct || !ip_vs_check_template(ct)) {
299 * If it is not persistent port zero, return NULL,
300 * otherwise create a connection template.
305 dest = svc->scheduler->schedule(svc, skb);
307 IP_VS_DBG(1, "p-schedule: no dest found.\n");
312 * Create a template according to the service
315 union nf_inet_addr fwmark = {
316 .ip = htonl(svc->fwmark)
319 ct = ip_vs_conn_new(svc->af, IPPROTO_IP,
323 IP_VS_CONN_F_TEMPLATE,
326 ct = ip_vs_conn_new(svc->af, iph.protocol,
330 IP_VS_CONN_F_TEMPLATE,
335 ct->timeout = svc->timeout;
337 /* set destination with the found template */
344 * Create a new connection according to the template
346 cp = ip_vs_conn_new(svc->af, iph.protocol,
347 &iph.saddr, ports[0],
348 &iph.daddr, ports[1],
360 ip_vs_control_add(cp, ct);
363 ip_vs_conn_stats(cp, svc);
369 * IPVS main scheduling function
370 * It selects a server according to the virtual service, and
371 * creates a connection entry.
372 * Protocols supported: TCP, UDP
375 ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
377 struct ip_vs_conn *cp = NULL;
378 struct ip_vs_iphdr iph;
379 struct ip_vs_dest *dest;
380 __be16 _ports[2], *pptr;
382 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
383 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
390 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
391 return ip_vs_sched_persist(svc, skb, pptr);
394 * Non-persistent service
396 if (!svc->fwmark && pptr[1] != svc->port) {
398 pr_err("Schedule: port zero only supported "
399 "in persistent services, "
400 "check your ipvs configuration\n");
404 dest = svc->scheduler->schedule(svc, skb);
406 IP_VS_DBG(1, "Schedule: no dest found.\n");
411 * Create a connection entry.
413 cp = ip_vs_conn_new(svc->af, iph.protocol,
416 &dest->addr, dest->port ? dest->port : pptr[1],
422 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
423 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
425 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
426 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
427 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
428 cp->flags, atomic_read(&cp->refcnt));
430 ip_vs_conn_stats(cp, svc);
436 * Pass or drop the packet.
437 * Called by ip_vs_in, when the virtual service is available but
438 * no destination is available for a new connection.
440 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
441 struct ip_vs_protocol *pp)
443 __be16 _ports[2], *pptr;
444 struct ip_vs_iphdr iph;
446 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
448 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
450 ip_vs_service_put(svc);
454 #ifdef CONFIG_IP_VS_IPV6
455 if (svc->af == AF_INET6)
456 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
459 unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
461 /* if it is fwmark-based service, the cache_bypass sysctl is up
462 and the destination is a non-local unicast, then create
463 a cache_bypass connection entry */
464 if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
466 struct ip_vs_conn *cp;
467 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
469 ip_vs_service_put(svc);
471 /* create a new connection entry */
472 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
473 cp = ip_vs_conn_new(svc->af, iph.protocol,
483 ip_vs_in_stats(cp, skb);
486 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
488 /* transmit the first SYN packet */
489 ret = cp->packet_xmit(skb, cp, pp);
490 /* do not touch skb anymore */
492 atomic_inc(&cp->in_pkts);
498 * When the virtual ftp service is presented, packets destined
499 * for other services on the VIP may get here (except services
500 * listed in the ipvs table), pass the packets, because it is
501 * not ipvs job to decide to drop the packets.
503 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
504 ip_vs_service_put(svc);
508 ip_vs_service_put(svc);
511 * Notify the client that the destination is unreachable, and
512 * release the socket buffer.
513 * Since it is in IP layer, the TCP socket is not actually
514 * created, the TCP RST packet cannot be sent, instead that
515 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
517 #ifdef CONFIG_IP_VS_IPV6
518 if (svc->af == AF_INET6)
519 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
522 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
529 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
530 * chain, and is used for VS/NAT.
531 * It detects packets for VS/NAT connections and sends the packets
532 * immediately. This can avoid that iptable_nat mangles the packets
535 static unsigned int ip_vs_post_routing(unsigned int hooknum,
537 const struct net_device *in,
538 const struct net_device *out,
539 int (*okfn)(struct sk_buff *))
541 if (!skb->ipvs_property)
543 /* The packet was sent from IPVS, exit this chain */
547 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
549 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
552 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
554 int err = ip_defrag(skb, user);
557 ip_send_check(ip_hdr(skb));
562 #ifdef CONFIG_IP_VS_IPV6
563 static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
565 /* TODO IPv6: Find out what to do here for IPv6 */
571 * Packet has been made sufficiently writable in caller
572 * - inout: 1=in->out, 0=out->in
574 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
575 struct ip_vs_conn *cp, int inout)
577 struct iphdr *iph = ip_hdr(skb);
578 unsigned int icmp_offset = iph->ihl*4;
579 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
581 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
584 iph->saddr = cp->vaddr.ip;
586 ciph->daddr = cp->vaddr.ip;
589 iph->daddr = cp->daddr.ip;
591 ciph->saddr = cp->daddr.ip;
595 /* the TCP/UDP/SCTP port */
596 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
597 IPPROTO_SCTP == ciph->protocol) {
598 __be16 *ports = (void *)ciph + ciph->ihl*4;
601 ports[1] = cp->vport;
603 ports[0] = cp->dport;
606 /* And finally the ICMP checksum */
608 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
609 skb->ip_summed = CHECKSUM_UNNECESSARY;
612 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
613 "Forwarding altered outgoing ICMP");
615 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
616 "Forwarding altered incoming ICMP");
619 #ifdef CONFIG_IP_VS_IPV6
620 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
621 struct ip_vs_conn *cp, int inout)
623 struct ipv6hdr *iph = ipv6_hdr(skb);
624 unsigned int icmp_offset = sizeof(struct ipv6hdr);
625 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
627 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
630 iph->saddr = cp->vaddr.in6;
631 ciph->daddr = cp->vaddr.in6;
633 iph->daddr = cp->daddr.in6;
634 ciph->saddr = cp->daddr.in6;
637 /* the TCP/UDP/SCTP port */
638 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
639 IPPROTO_SCTP == ciph->nexthdr) {
640 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
643 ports[1] = cp->vport;
645 ports[0] = cp->dport;
648 /* And finally the ICMP checksum */
649 icmph->icmp6_cksum = 0;
650 /* TODO IPv6: is this correct for ICMPv6? */
651 ip_vs_checksum_complete(skb, icmp_offset);
652 skb->ip_summed = CHECKSUM_UNNECESSARY;
655 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
656 "Forwarding altered outgoing ICMPv6");
658 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
659 "Forwarding altered incoming ICMPv6");
663 /* Handle relevant response ICMP messages - forward to the right
664 * destination host. Used for NAT and local client.
666 static int handle_response_icmp(int af, struct sk_buff *skb,
667 union nf_inet_addr *snet,
668 __u8 protocol, struct ip_vs_conn *cp,
669 struct ip_vs_protocol *pp,
670 unsigned int offset, unsigned int ihl)
672 unsigned int verdict = NF_DROP;
674 if (IP_VS_FWD_METHOD(cp) != 0) {
675 pr_err("shouldn't reach here, because the box is on the "
676 "half connection in the tun/dr module.\n");
679 /* Ensure the checksum is correct */
680 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
681 /* Failed checksum! */
682 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
683 IP_VS_DBG_ADDR(af, snet));
687 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
688 IPPROTO_SCTP == protocol)
689 offset += 2 * sizeof(__u16);
690 if (!skb_make_writable(skb, offset))
693 #ifdef CONFIG_IP_VS_IPV6
695 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
698 ip_vs_nat_icmp(skb, pp, cp, 1);
700 /* do the statistics and put it back */
701 ip_vs_out_stats(cp, skb);
703 skb->ipvs_property = 1;
707 __ip_vs_conn_put(cp);
713 * Handle ICMP messages in the inside-to-outside direction (outgoing).
714 * Find any that might be relevant, check against existing connections.
715 * Currently handles error types - unreachable, quench, ttl exceeded.
717 static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
720 struct icmphdr _icmph, *ic;
721 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
722 struct ip_vs_iphdr ciph;
723 struct ip_vs_conn *cp;
724 struct ip_vs_protocol *pp;
725 unsigned int offset, ihl;
726 union nf_inet_addr snet;
730 /* reassemble IP fragments */
731 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
732 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
737 offset = ihl = iph->ihl * 4;
738 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
742 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
743 ic->type, ntohs(icmp_id(ic)),
744 &iph->saddr, &iph->daddr);
747 * Work through seeing if this is for us.
748 * These checks are supposed to be in an order that means easy
749 * things are checked first to speed up processing.... however
750 * this means that some packets will manage to get a long way
751 * down this stack and then be rejected, but that's life.
753 if ((ic->type != ICMP_DEST_UNREACH) &&
754 (ic->type != ICMP_SOURCE_QUENCH) &&
755 (ic->type != ICMP_TIME_EXCEEDED)) {
760 /* Now find the contained IP header */
761 offset += sizeof(_icmph);
762 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
764 return NF_ACCEPT; /* The packet looks wrong, ignore */
766 pp = ip_vs_proto_get(cih->protocol);
770 /* Is the embedded protocol header present? */
771 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
775 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
777 offset += cih->ihl * 4;
779 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
780 /* The embedded headers contain source and dest in reverse order */
781 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
785 snet.ip = iph->saddr;
786 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
790 #ifdef CONFIG_IP_VS_IPV6
791 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
794 struct icmp6hdr _icmph, *ic;
795 struct ipv6hdr _ciph, *cih; /* The ip header contained
797 struct ip_vs_iphdr ciph;
798 struct ip_vs_conn *cp;
799 struct ip_vs_protocol *pp;
801 union nf_inet_addr snet;
805 /* reassemble IP fragments */
806 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
807 if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
812 offset = sizeof(struct ipv6hdr);
813 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
817 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
818 ic->icmp6_type, ntohs(icmpv6_id(ic)),
819 &iph->saddr, &iph->daddr);
822 * Work through seeing if this is for us.
823 * These checks are supposed to be in an order that means easy
824 * things are checked first to speed up processing.... however
825 * this means that some packets will manage to get a long way
826 * down this stack and then be rejected, but that's life.
828 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
829 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
830 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
835 /* Now find the contained IP header */
836 offset += sizeof(_icmph);
837 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
839 return NF_ACCEPT; /* The packet looks wrong, ignore */
841 pp = ip_vs_proto_get(cih->nexthdr);
845 /* Is the embedded protocol header present? */
846 /* TODO: we don't support fragmentation at the moment anyways */
847 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
850 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
852 offset += sizeof(struct ipv6hdr);
854 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
855 /* The embedded headers contain source and dest in reverse order */
856 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
860 ipv6_addr_copy(&snet.in6, &iph->saddr);
861 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
862 pp, offset, sizeof(struct ipv6hdr));
867 * Check if sctp chunc is ABORT chunk
869 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
871 sctp_chunkhdr_t *sch, schunk;
872 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
873 sizeof(schunk), &schunk);
876 if (sch->type == SCTP_CID_ABORT)
881 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
883 struct tcphdr _tcph, *th;
885 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
891 /* Handle response packets: rewrite addresses and send away...
892 * Used for NAT and local client.
895 handle_response(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
896 struct ip_vs_conn *cp, int ihl)
898 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
900 if (!skb_make_writable(skb, ihl))
903 /* mangle the packet */
904 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
907 #ifdef CONFIG_IP_VS_IPV6
909 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
913 ip_hdr(skb)->saddr = cp->vaddr.ip;
914 ip_send_check(ip_hdr(skb));
917 /* For policy routing, packets originating from this
918 * machine itself may be routed differently to packets
919 * passing through. We want this packet to be routed as
920 * if it came from this machine itself. So re-compute
921 * the routing information.
923 #ifdef CONFIG_IP_VS_IPV6
924 if (af == AF_INET6) {
925 if (ip6_route_me_harder(skb) != 0)
929 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
932 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
934 ip_vs_out_stats(cp, skb);
935 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
938 skb->ipvs_property = 1;
950 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
951 * Check if outgoing packet belongs to the established ip_vs_conn.
954 ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
955 const struct net_device *in, const struct net_device *out,
956 int (*okfn)(struct sk_buff *))
958 struct ip_vs_iphdr iph;
959 struct ip_vs_protocol *pp;
960 struct ip_vs_conn *cp;
965 af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
967 if (skb->ipvs_property)
970 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
971 #ifdef CONFIG_IP_VS_IPV6
972 if (af == AF_INET6) {
973 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
974 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
978 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
982 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
983 int related, verdict = ip_vs_out_icmp(skb, &related);
987 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
990 pp = ip_vs_proto_get(iph.protocol);
994 /* reassemble IP fragments */
995 #ifdef CONFIG_IP_VS_IPV6
996 if (af == AF_INET6) {
997 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
998 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
1003 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1007 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1008 !pp->dont_defrag)) {
1009 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
1012 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1016 * Check if the packet belongs to an existing entry
1018 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1020 if (unlikely(!cp)) {
1021 if (sysctl_ip_vs_nat_icmp_send &&
1022 (pp->protocol == IPPROTO_TCP ||
1023 pp->protocol == IPPROTO_UDP ||
1024 pp->protocol == IPPROTO_SCTP)) {
1025 __be16 _ports[2], *pptr;
1027 pptr = skb_header_pointer(skb, iph.len,
1028 sizeof(_ports), _ports);
1030 return NF_ACCEPT; /* Not for me */
1031 if (ip_vs_lookup_real_service(af, iph.protocol,
1035 * Notify the real server: there is no
1036 * existing entry if it is not RST
1037 * packet or not TCP packet.
1039 if ((iph.protocol != IPPROTO_TCP &&
1040 iph.protocol != IPPROTO_SCTP)
1041 || ((iph.protocol == IPPROTO_TCP
1042 && !is_tcp_reset(skb, iph.len))
1043 || (iph.protocol == IPPROTO_SCTP
1044 && !is_sctp_abort(skb,
1046 #ifdef CONFIG_IP_VS_IPV6
1049 ICMPV6_DEST_UNREACH,
1050 ICMPV6_PORT_UNREACH,
1056 ICMP_PORT_UNREACH, 0);
1061 IP_VS_DBG_PKT(12, pp, skb, 0,
1062 "packet continues traversal as normal");
1066 return handle_response(af, skb, pp, cp, iph.len);
1071 * Handle ICMP messages in the outside-to-inside direction (incoming).
1072 * Find any that might be relevant, check against existing connections,
1073 * forward to the right destination host if relevant.
1074 * Currently handles error types - unreachable, quench, ttl exceeded.
1077 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1080 struct icmphdr _icmph, *ic;
1081 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1082 struct ip_vs_iphdr ciph;
1083 struct ip_vs_conn *cp;
1084 struct ip_vs_protocol *pp;
1085 unsigned int offset, ihl, verdict;
1086 union nf_inet_addr snet;
1090 /* reassemble IP fragments */
1091 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
1092 if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
1093 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
1098 offset = ihl = iph->ihl * 4;
1099 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1103 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1104 ic->type, ntohs(icmp_id(ic)),
1105 &iph->saddr, &iph->daddr);
1108 * Work through seeing if this is for us.
1109 * These checks are supposed to be in an order that means easy
1110 * things are checked first to speed up processing.... however
1111 * this means that some packets will manage to get a long way
1112 * down this stack and then be rejected, but that's life.
1114 if ((ic->type != ICMP_DEST_UNREACH) &&
1115 (ic->type != ICMP_SOURCE_QUENCH) &&
1116 (ic->type != ICMP_TIME_EXCEEDED)) {
1121 /* Now find the contained IP header */
1122 offset += sizeof(_icmph);
1123 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1125 return NF_ACCEPT; /* The packet looks wrong, ignore */
1127 pp = ip_vs_proto_get(cih->protocol);
1131 /* Is the embedded protocol header present? */
1132 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1136 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
1138 offset += cih->ihl * 4;
1140 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1141 /* The embedded headers contain source and dest in reverse order */
1142 cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
1144 /* The packet could also belong to a local client */
1145 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
1147 snet.ip = iph->saddr;
1148 return handle_response_icmp(AF_INET, skb, &snet,
1149 cih->protocol, cp, pp,
1157 /* Ensure the checksum is correct */
1158 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1159 /* Failed checksum! */
1160 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1165 /* do the statistics and put it back */
1166 ip_vs_in_stats(cp, skb);
1167 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1168 offset += 2 * sizeof(__u16);
1169 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1170 /* do not touch skb anymore */
1173 __ip_vs_conn_put(cp);
1178 #ifdef CONFIG_IP_VS_IPV6
1180 ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1182 struct ipv6hdr *iph;
1183 struct icmp6hdr _icmph, *ic;
1184 struct ipv6hdr _ciph, *cih; /* The ip header contained
1186 struct ip_vs_iphdr ciph;
1187 struct ip_vs_conn *cp;
1188 struct ip_vs_protocol *pp;
1189 unsigned int offset, verdict;
1190 union nf_inet_addr snet;
1194 /* reassemble IP fragments */
1195 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1196 if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
1202 iph = ipv6_hdr(skb);
1203 offset = sizeof(struct ipv6hdr);
1204 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1208 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
1209 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1210 &iph->saddr, &iph->daddr);
1213 * Work through seeing if this is for us.
1214 * These checks are supposed to be in an order that means easy
1215 * things are checked first to speed up processing.... however
1216 * this means that some packets will manage to get a long way
1217 * down this stack and then be rejected, but that's life.
1219 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1220 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1221 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1226 /* Now find the contained IP header */
1227 offset += sizeof(_icmph);
1228 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1230 return NF_ACCEPT; /* The packet looks wrong, ignore */
1232 pp = ip_vs_proto_get(cih->nexthdr);
1236 /* Is the embedded protocol header present? */
1237 /* TODO: we don't support fragmentation at the moment anyways */
1238 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1241 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
1243 offset += sizeof(struct ipv6hdr);
1245 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1246 /* The embedded headers contain source and dest in reverse order */
1247 cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
1249 /* The packet could also belong to a local client */
1250 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
1252 ipv6_addr_copy(&snet.in6, &iph->saddr);
1253 return handle_response_icmp(AF_INET6, skb, &snet,
1256 sizeof(struct ipv6hdr));
1263 /* do the statistics and put it back */
1264 ip_vs_in_stats(cp, skb);
1265 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1266 IPPROTO_SCTP == cih->nexthdr)
1267 offset += 2 * sizeof(__u16);
1268 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1269 /* do not touch skb anymore */
1271 __ip_vs_conn_put(cp);
1279 * Check if it's for virtual services, look it up,
1280 * and send it on its way...
1283 ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
1284 const struct net_device *in, const struct net_device *out,
1285 int (*okfn)(struct sk_buff *))
1287 struct ip_vs_iphdr iph;
1288 struct ip_vs_protocol *pp;
1289 struct ip_vs_conn *cp;
1290 int ret, restart, af, pkts;
1292 af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
1294 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1297 * Big tappo: only PACKET_HOST, including loopback for local client
1298 * Don't handle local packets on IPv6 for now
1300 if (unlikely(skb->pkt_type != PACKET_HOST)) {
1301 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
1304 IP_VS_DBG_ADDR(af, &iph.daddr));
1308 #ifdef CONFIG_IP_VS_IPV6
1309 if (af == AF_INET6) {
1310 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1311 int related, verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
1315 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1319 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1320 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
1324 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1327 /* Protocol supported? */
1328 pp = ip_vs_proto_get(iph.protocol);
1333 * Check if the packet belongs to an existing connection entry
1335 cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
1337 if (unlikely(!cp)) {
1340 /* For local client packets, it could be a response */
1341 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1343 return handle_response(af, skb, pp, cp, iph.len);
1345 if (!pp->conn_schedule(af, skb, pp, &v, &cp))
1349 if (unlikely(!cp)) {
1350 /* sorry, all this trouble for a no-hit :) */
1351 IP_VS_DBG_PKT(12, pp, skb, 0,
1352 "packet continues traversal as normal");
1356 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1358 /* Check the server status */
1359 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1360 /* the destination server is not available */
1362 if (sysctl_ip_vs_expire_nodest_conn) {
1363 /* try to expire the connection immediately */
1364 ip_vs_conn_expire_now(cp);
1366 /* don't restart its timer, and silently
1368 __ip_vs_conn_put(cp);
1372 ip_vs_in_stats(cp, skb);
1373 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1374 if (cp->packet_xmit)
1375 ret = cp->packet_xmit(skb, cp, pp);
1376 /* do not touch skb anymore */
1378 IP_VS_DBG_RL("warning: packet_xmit is null");
1382 /* Increase its packet counter and check if it is needed
1383 * to be synchronized
1385 * Sync connection if it is about to close to
1386 * encorage the standby servers to update the connections timeout
1388 pkts = atomic_add_return(1, &cp->in_pkts);
1389 if (af == AF_INET && (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
1390 cp->protocol == IPPROTO_SCTP) {
1391 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
1392 (atomic_read(&cp->in_pkts) %
1393 sysctl_ip_vs_sync_threshold[1]
1394 == sysctl_ip_vs_sync_threshold[0])) ||
1395 (cp->old_state != cp->state &&
1396 ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1397 (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1398 (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
1399 ip_vs_sync_conn(cp);
1404 if (af == AF_INET &&
1405 (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
1406 (((cp->protocol != IPPROTO_TCP ||
1407 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1408 (pkts % sysctl_ip_vs_sync_threshold[1]
1409 == sysctl_ip_vs_sync_threshold[0])) ||
1410 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1411 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
1412 (cp->state == IP_VS_TCP_S_CLOSE) ||
1413 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1414 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1415 ip_vs_sync_conn(cp);
1417 cp->old_state = cp->state;
1425 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1426 * related packets destined for 0.0.0.0/0.
1427 * When fwmark-based virtual service is used, such as transparent
1428 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1429 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1430 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1431 * and send them to ip_vs_in_icmp.
1434 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1435 const struct net_device *in, const struct net_device *out,
1436 int (*okfn)(struct sk_buff *))
1440 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1443 return ip_vs_in_icmp(skb, &r, hooknum);
1446 #ifdef CONFIG_IP_VS_IPV6
1448 ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1449 const struct net_device *in, const struct net_device *out,
1450 int (*okfn)(struct sk_buff *))
1454 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1457 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1462 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1463 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1464 * or VS/NAT(change destination), so that filtering rules can be
1465 * applied to IPVS. */
1468 .owner = THIS_MODULE,
1470 .hooknum = NF_INET_LOCAL_IN,
1473 /* After packet filtering, change source only for VS/NAT */
1476 .owner = THIS_MODULE,
1478 .hooknum = NF_INET_FORWARD,
1481 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1482 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1484 .hook = ip_vs_forward_icmp,
1485 .owner = THIS_MODULE,
1487 .hooknum = NF_INET_FORWARD,
1490 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1492 .hook = ip_vs_post_routing,
1493 .owner = THIS_MODULE,
1495 .hooknum = NF_INET_POST_ROUTING,
1496 .priority = NF_IP_PRI_NAT_SRC-1,
1498 #ifdef CONFIG_IP_VS_IPV6
1499 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1500 * or VS/NAT(change destination), so that filtering rules can be
1501 * applied to IPVS. */
1504 .owner = THIS_MODULE,
1506 .hooknum = NF_INET_LOCAL_IN,
1509 /* After packet filtering, change source only for VS/NAT */
1512 .owner = THIS_MODULE,
1514 .hooknum = NF_INET_FORWARD,
1517 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1518 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1520 .hook = ip_vs_forward_icmp_v6,
1521 .owner = THIS_MODULE,
1523 .hooknum = NF_INET_FORWARD,
1526 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1528 .hook = ip_vs_post_routing,
1529 .owner = THIS_MODULE,
1531 .hooknum = NF_INET_POST_ROUTING,
1532 .priority = NF_IP6_PRI_NAT_SRC-1,
1539 * Initialize IP Virtual Server
1541 static int __init ip_vs_init(void)
1545 ip_vs_estimator_init();
1547 ret = ip_vs_control_init();
1549 pr_err("can't setup control.\n");
1550 goto cleanup_estimator;
1553 ip_vs_protocol_init();
1555 ret = ip_vs_app_init();
1557 pr_err("can't setup application helper.\n");
1558 goto cleanup_protocol;
1561 ret = ip_vs_conn_init();
1563 pr_err("can't setup connection table.\n");
1567 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1569 pr_err("can't register hooks.\n");
1573 pr_info("ipvs loaded.\n");
1577 ip_vs_conn_cleanup();
1579 ip_vs_app_cleanup();
1581 ip_vs_protocol_cleanup();
1582 ip_vs_control_cleanup();
1584 ip_vs_estimator_cleanup();
1588 static void __exit ip_vs_cleanup(void)
1590 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1591 ip_vs_conn_cleanup();
1592 ip_vs_app_cleanup();
1593 ip_vs_protocol_cleanup();
1594 ip_vs_control_cleanup();
1595 ip_vs_estimator_cleanup();
1596 pr_info("ipvs unloaded.\n");
1599 module_init(ip_vs_init);
1600 module_exit(ip_vs_cleanup);
1601 MODULE_LICENSE("GPL");