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
28 #include <linux/module.h>
29 #include <linux/kernel.h>
31 #include <linux/tcp.h>
32 #include <linux/icmp.h>
37 #include <net/icmp.h> /* for icmp_send */
38 #include <net/route.h>
40 #include <linux/netfilter.h>
41 #include <linux/netfilter_ipv4.h>
43 #include <net/ip_vs.h>
46 EXPORT_SYMBOL(register_ip_vs_scheduler
);
47 EXPORT_SYMBOL(unregister_ip_vs_scheduler
);
48 EXPORT_SYMBOL(ip_vs_skb_replace
);
49 EXPORT_SYMBOL(ip_vs_proto_name
);
50 EXPORT_SYMBOL(ip_vs_conn_new
);
51 EXPORT_SYMBOL(ip_vs_conn_in_get
);
52 EXPORT_SYMBOL(ip_vs_conn_out_get
);
53 #ifdef CONFIG_IP_VS_PROTO_TCP
54 EXPORT_SYMBOL(ip_vs_tcp_conn_listen
);
56 EXPORT_SYMBOL(ip_vs_conn_put
);
57 #ifdef CONFIG_IP_VS_DEBUG
58 EXPORT_SYMBOL(ip_vs_get_debug_level
);
60 EXPORT_SYMBOL(ip_vs_make_skb_writable
);
63 /* ID used in ICMP lookups */
64 #define icmp_id(icmph) (((icmph)->un).echo.id)
66 const char *ip_vs_proto_name(unsigned proto
)
80 sprintf(buf
, "IP_%d", proto
);
85 void ip_vs_init_hash_table(struct list_head
*table
, int rows
)
88 INIT_LIST_HEAD(&table
[rows
]);
92 ip_vs_in_stats(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
94 struct ip_vs_dest
*dest
= cp
->dest
;
95 if (dest
&& (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
96 spin_lock(&dest
->stats
.lock
);
98 dest
->stats
.inbytes
+= skb
->len
;
99 spin_unlock(&dest
->stats
.lock
);
101 spin_lock(&dest
->svc
->stats
.lock
);
102 dest
->svc
->stats
.inpkts
++;
103 dest
->svc
->stats
.inbytes
+= skb
->len
;
104 spin_unlock(&dest
->svc
->stats
.lock
);
106 spin_lock(&ip_vs_stats
.lock
);
107 ip_vs_stats
.inpkts
++;
108 ip_vs_stats
.inbytes
+= skb
->len
;
109 spin_unlock(&ip_vs_stats
.lock
);
115 ip_vs_out_stats(struct ip_vs_conn
*cp
, struct sk_buff
*skb
)
117 struct ip_vs_dest
*dest
= cp
->dest
;
118 if (dest
&& (dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
119 spin_lock(&dest
->stats
.lock
);
120 dest
->stats
.outpkts
++;
121 dest
->stats
.outbytes
+= skb
->len
;
122 spin_unlock(&dest
->stats
.lock
);
124 spin_lock(&dest
->svc
->stats
.lock
);
125 dest
->svc
->stats
.outpkts
++;
126 dest
->svc
->stats
.outbytes
+= skb
->len
;
127 spin_unlock(&dest
->svc
->stats
.lock
);
129 spin_lock(&ip_vs_stats
.lock
);
130 ip_vs_stats
.outpkts
++;
131 ip_vs_stats
.outbytes
+= skb
->len
;
132 spin_unlock(&ip_vs_stats
.lock
);
138 ip_vs_conn_stats(struct ip_vs_conn
*cp
, struct ip_vs_service
*svc
)
140 spin_lock(&cp
->dest
->stats
.lock
);
141 cp
->dest
->stats
.conns
++;
142 spin_unlock(&cp
->dest
->stats
.lock
);
144 spin_lock(&svc
->stats
.lock
);
146 spin_unlock(&svc
->stats
.lock
);
148 spin_lock(&ip_vs_stats
.lock
);
150 spin_unlock(&ip_vs_stats
.lock
);
155 ip_vs_set_state(struct ip_vs_conn
*cp
, int direction
,
156 const struct sk_buff
*skb
,
157 struct ip_vs_protocol
*pp
)
159 if (unlikely(!pp
->state_transition
))
161 return pp
->state_transition(cp
, direction
, skb
, pp
);
165 int ip_vs_make_skb_writable(struct sk_buff
**pskb
, int writable_len
)
167 struct sk_buff
*skb
= *pskb
;
169 /* skb is already used, better copy skb and its payload */
170 if (unlikely(skb_shared(skb
) || skb
->sk
))
173 /* skb data is already used, copy it */
174 if (unlikely(skb_cloned(skb
)))
177 return pskb_may_pull(skb
, writable_len
);
180 if (unlikely(writable_len
> skb
->len
))
182 return !pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
185 if (unlikely(writable_len
> skb
->len
))
187 skb
= skb_copy(skb
, GFP_ATOMIC
);
190 BUG_ON(skb_is_nonlinear(skb
));
192 /* Rest of kernel will get very unhappy if we pass it a
193 suddenly-orphaned skbuff */
195 skb_set_owner_w(skb
, (*pskb
)->sk
);
202 * IPVS persistent scheduling function
203 * It creates a connection entry according to its template if exists,
204 * or selects a server and creates a connection entry plus a template.
205 * Locking: we are svc user (svc->refcnt), so we hold all dests too
206 * Protocols supported: TCP, UDP
208 static struct ip_vs_conn
*
209 ip_vs_sched_persist(struct ip_vs_service
*svc
,
210 const struct sk_buff
*skb
,
213 struct ip_vs_conn
*cp
= NULL
;
214 struct iphdr
*iph
= skb
->nh
.iph
;
215 struct ip_vs_dest
*dest
;
216 struct ip_vs_conn
*ct
;
217 __u16 dport
; /* destination port to forward */
218 __u32 snet
; /* source network of the client, after masking */
220 /* Mask saddr with the netmask to adjust template granularity */
221 snet
= iph
->saddr
& svc
->netmask
;
223 IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u "
224 "mnet %u.%u.%u.%u\n",
225 NIPQUAD(iph
->saddr
), ntohs(ports
[0]),
226 NIPQUAD(iph
->daddr
), ntohs(ports
[1]),
230 * As far as we know, FTP is a very complicated network protocol, and
231 * it uses control connection and data connections. For active FTP,
232 * FTP server initialize data connection to the client, its source port
233 * is often 20. For passive FTP, FTP server tells the clients the port
234 * that it passively listens to, and the client issues the data
235 * connection. In the tunneling or direct routing mode, the load
236 * balancer is on the client-to-server half of connection, the port
237 * number is unknown to the load balancer. So, a conn template like
238 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
239 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
240 * is created for other persistent services.
242 if (ports
[1] == svc
->port
) {
243 /* Check if a template already exists */
244 if (svc
->port
!= FTPPORT
)
245 ct
= ip_vs_conn_in_get(iph
->protocol
, snet
, 0,
246 iph
->daddr
, ports
[1]);
248 ct
= ip_vs_conn_in_get(iph
->protocol
, snet
, 0,
251 if (!ct
|| !ip_vs_check_template(ct
)) {
253 * No template found or the dest of the connection
254 * template is not available.
256 dest
= svc
->scheduler
->schedule(svc
, skb
);
258 IP_VS_DBG(1, "p-schedule: no dest found.\n");
263 * Create a template like <protocol,caddr,0,
264 * vaddr,vport,daddr,dport> for non-ftp service,
265 * and <protocol,caddr,0,vaddr,0,daddr,0>
268 if (svc
->port
!= FTPPORT
)
269 ct
= ip_vs_conn_new(iph
->protocol
,
273 dest
->addr
, dest
->port
,
277 ct
= ip_vs_conn_new(iph
->protocol
,
286 ct
->timeout
= svc
->timeout
;
288 /* set destination with the found template */
294 * Note: persistent fwmark-based services and persistent
295 * port zero service are handled here.
296 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
297 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
300 ct
= ip_vs_conn_in_get(IPPROTO_IP
, snet
, 0,
301 htonl(svc
->fwmark
), 0);
303 ct
= ip_vs_conn_in_get(iph
->protocol
, snet
, 0,
306 if (!ct
|| !ip_vs_check_template(ct
)) {
308 * If it is not persistent port zero, return NULL,
309 * otherwise create a connection template.
314 dest
= svc
->scheduler
->schedule(svc
, skb
);
316 IP_VS_DBG(1, "p-schedule: no dest found.\n");
321 * Create a template according to the service
324 ct
= ip_vs_conn_new(IPPROTO_IP
,
326 htonl(svc
->fwmark
), 0,
331 ct
= ip_vs_conn_new(iph
->protocol
,
340 ct
->timeout
= svc
->timeout
;
342 /* set destination with the found template */
349 * Create a new connection according to the template
351 cp
= ip_vs_conn_new(iph
->protocol
,
352 iph
->saddr
, ports
[0],
353 iph
->daddr
, ports
[1],
365 ip_vs_control_add(cp
, ct
);
368 ip_vs_conn_stats(cp
, svc
);
374 * IPVS main scheduling function
375 * It selects a server according to the virtual service, and
376 * creates a connection entry.
377 * Protocols supported: TCP, UDP
380 ip_vs_schedule(struct ip_vs_service
*svc
, const struct sk_buff
*skb
)
382 struct ip_vs_conn
*cp
= NULL
;
383 struct iphdr
*iph
= skb
->nh
.iph
;
384 struct ip_vs_dest
*dest
;
385 __u16 _ports
[2], *pptr
;
387 pptr
= skb_header_pointer(skb
, iph
->ihl
*4,
388 sizeof(_ports
), _ports
);
395 if (svc
->flags
& IP_VS_SVC_F_PERSISTENT
)
396 return ip_vs_sched_persist(svc
, skb
, pptr
);
399 * Non-persistent service
401 if (!svc
->fwmark
&& pptr
[1] != svc
->port
) {
403 IP_VS_ERR("Schedule: port zero only supported "
404 "in persistent services, "
405 "check your ipvs configuration\n");
409 dest
= svc
->scheduler
->schedule(svc
, skb
);
411 IP_VS_DBG(1, "Schedule: no dest found.\n");
416 * Create a connection entry.
418 cp
= ip_vs_conn_new(iph
->protocol
,
421 dest
->addr
, dest
->port
?dest
->port
:pptr
[1],
427 IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u "
428 "d:%u.%u.%u.%u:%u flg:%X cnt:%d\n",
430 NIPQUAD(cp
->caddr
), ntohs(cp
->cport
),
431 NIPQUAD(cp
->vaddr
), ntohs(cp
->vport
),
432 NIPQUAD(cp
->daddr
), ntohs(cp
->dport
),
433 cp
->flags
, atomic_read(&cp
->refcnt
));
435 ip_vs_conn_stats(cp
, svc
);
441 * Pass or drop the packet.
442 * Called by ip_vs_in, when the virtual service is available but
443 * no destination is available for a new connection.
445 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
446 struct ip_vs_protocol
*pp
)
448 __u16 _ports
[2], *pptr
;
449 struct iphdr
*iph
= skb
->nh
.iph
;
451 pptr
= skb_header_pointer(skb
, iph
->ihl
*4,
452 sizeof(_ports
), _ports
);
454 ip_vs_service_put(svc
);
458 /* if it is fwmark-based service, the cache_bypass sysctl is up
459 and the destination is RTN_UNICAST (and not local), then create
460 a cache_bypass connection entry */
461 if (sysctl_ip_vs_cache_bypass
&& svc
->fwmark
462 && (inet_addr_type(iph
->daddr
) == RTN_UNICAST
)) {
464 struct ip_vs_conn
*cp
;
466 ip_vs_service_put(svc
);
468 /* create a new connection entry */
469 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
470 cp
= ip_vs_conn_new(iph
->protocol
,
480 ip_vs_in_stats(cp
, skb
);
483 cs
= ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pp
);
485 /* transmit the first SYN packet */
486 ret
= cp
->packet_xmit(skb
, cp
, pp
);
487 /* do not touch skb anymore */
489 atomic_inc(&cp
->in_pkts
);
495 * When the virtual ftp service is presented, packets destined
496 * for other services on the VIP may get here (except services
497 * listed in the ipvs table), pass the packets, because it is
498 * not ipvs job to decide to drop the packets.
500 if ((svc
->port
== FTPPORT
) && (pptr
[1] != FTPPORT
)) {
501 ip_vs_service_put(svc
);
505 ip_vs_service_put(svc
);
508 * Notify the client that the destination is unreachable, and
509 * release the socket buffer.
510 * Since it is in IP layer, the TCP socket is not actually
511 * created, the TCP RST packet cannot be sent, instead that
512 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
514 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
520 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_IP_POST_ROUTING
521 * chain, and is used for VS/NAT.
522 * It detects packets for VS/NAT connections and sends the packets
523 * immediately. This can avoid that iptable_nat mangles the packets
526 static unsigned int ip_vs_post_routing(unsigned int hooknum
,
527 struct sk_buff
**pskb
,
528 const struct net_device
*in
,
529 const struct net_device
*out
,
530 int (*okfn
)(struct sk_buff
*))
532 if (!((*pskb
)->nfcache
& NFC_IPVS_PROPERTY
))
535 /* The packet was sent from IPVS, exit this chain */
541 u16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
543 return (u16
) csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
546 static inline struct sk_buff
*
547 ip_vs_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
549 skb
= ip_defrag(skb
, user
);
551 ip_send_check(skb
->nh
.iph
);
556 * Packet has been made sufficiently writable in caller
557 * - inout: 1=in->out, 0=out->in
559 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
560 struct ip_vs_conn
*cp
, int inout
)
562 struct iphdr
*iph
= skb
->nh
.iph
;
563 unsigned int icmp_offset
= iph
->ihl
*4;
564 struct icmphdr
*icmph
= (struct icmphdr
*)(skb
->nh
.raw
+ icmp_offset
);
565 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
568 iph
->saddr
= cp
->vaddr
;
570 ciph
->daddr
= cp
->vaddr
;
573 iph
->daddr
= cp
->daddr
;
575 ciph
->saddr
= cp
->daddr
;
579 /* the TCP/UDP port */
580 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
) {
581 __u16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
584 ports
[1] = cp
->vport
;
586 ports
[0] = cp
->dport
;
589 /* And finally the ICMP checksum */
591 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
592 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
595 IP_VS_DBG_PKT(11, pp
, skb
, (void *)ciph
- (void *)iph
,
596 "Forwarding altered outgoing ICMP");
598 IP_VS_DBG_PKT(11, pp
, skb
, (void *)ciph
- (void *)iph
,
599 "Forwarding altered incoming ICMP");
603 * Handle ICMP messages in the inside-to-outside direction (outgoing).
604 * Find any that might be relevant, check against existing connections,
605 * forward to the right destination host if relevant.
606 * Currently handles error types - unreachable, quench, ttl exceeded.
607 * (Only used in VS/NAT)
609 static int ip_vs_out_icmp(struct sk_buff
**pskb
, int *related
)
611 struct sk_buff
*skb
= *pskb
;
613 struct icmphdr _icmph
, *ic
;
614 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
615 struct ip_vs_conn
*cp
;
616 struct ip_vs_protocol
*pp
;
617 unsigned int offset
, ihl
, verdict
;
621 /* reassemble IP fragments */
622 if (skb
->nh
.iph
->frag_off
& __constant_htons(IP_MF
|IP_OFFSET
)) {
623 skb
= ip_vs_gather_frags(skb
, IP_DEFRAG_VS_OUT
);
630 offset
= ihl
= iph
->ihl
* 4;
631 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
635 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
636 ic
->type
, ntohs(icmp_id(ic
)),
637 NIPQUAD(iph
->saddr
), NIPQUAD(iph
->daddr
));
640 * Work through seeing if this is for us.
641 * These checks are supposed to be in an order that means easy
642 * things are checked first to speed up processing.... however
643 * this means that some packets will manage to get a long way
644 * down this stack and then be rejected, but that's life.
646 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
647 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
648 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
653 /* Now find the contained IP header */
654 offset
+= sizeof(_icmph
);
655 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
657 return NF_ACCEPT
; /* The packet looks wrong, ignore */
659 pp
= ip_vs_proto_get(cih
->protocol
);
663 /* Is the embedded protocol header present? */
664 if (unlikely(cih
->frag_off
& __constant_htons(IP_OFFSET
) &&
668 IP_VS_DBG_PKT(11, pp
, skb
, offset
, "Checking outgoing ICMP for");
670 offset
+= cih
->ihl
* 4;
672 /* The embedded headers contain source and dest in reverse order */
673 cp
= pp
->conn_out_get(skb
, pp
, cih
, offset
, 1);
679 if (IP_VS_FWD_METHOD(cp
) != 0) {
680 IP_VS_ERR("shouldn't reach here, because the box is on the"
681 "half connection in the tun/dr module.\n");
684 /* Ensure the checksum is correct */
685 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
686 ip_vs_checksum_complete(skb
, ihl
)) {
687 /* Failed checksum! */
688 IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
689 NIPQUAD(iph
->saddr
));
693 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
)
694 offset
+= 2 * sizeof(__u16
);
695 if (!ip_vs_make_skb_writable(pskb
, offset
))
699 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
701 /* do the statistics and put it back */
702 ip_vs_out_stats(cp
, skb
);
704 skb
->nfcache
|= NFC_IPVS_PROPERTY
;
708 __ip_vs_conn_put(cp
);
713 static inline int is_tcp_reset(const struct sk_buff
*skb
)
715 struct tcphdr _tcph
, *th
;
717 th
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
* 4,
718 sizeof(_tcph
), &_tcph
);
725 * It is hooked at the NF_IP_FORWARD chain, used only for VS/NAT.
726 * Check if outgoing packet belongs to the established ip_vs_conn,
727 * rewrite addresses of the packet and send it on its way...
730 ip_vs_out(unsigned int hooknum
, struct sk_buff
**pskb
,
731 const struct net_device
*in
, const struct net_device
*out
,
732 int (*okfn
)(struct sk_buff
*))
734 struct sk_buff
*skb
= *pskb
;
736 struct ip_vs_protocol
*pp
;
737 struct ip_vs_conn
*cp
;
742 if (skb
->nfcache
& NFC_IPVS_PROPERTY
)
746 if (unlikely(iph
->protocol
== IPPROTO_ICMP
)) {
747 int related
, verdict
= ip_vs_out_icmp(pskb
, &related
);
755 pp
= ip_vs_proto_get(iph
->protocol
);
759 /* reassemble IP fragments */
760 if (unlikely(iph
->frag_off
& __constant_htons(IP_MF
|IP_OFFSET
) &&
762 skb
= ip_vs_gather_frags(skb
, IP_DEFRAG_VS_OUT
);
772 * Check if the packet belongs to an existing entry
774 cp
= pp
->conn_out_get(skb
, pp
, iph
, ihl
, 0);
777 if (sysctl_ip_vs_nat_icmp_send
&&
778 (pp
->protocol
== IPPROTO_TCP
||
779 pp
->protocol
== IPPROTO_UDP
)) {
780 __u16 _ports
[2], *pptr
;
782 pptr
= skb_header_pointer(skb
, ihl
,
783 sizeof(_ports
), _ports
);
785 return NF_ACCEPT
; /* Not for me */
786 if (ip_vs_lookup_real_service(iph
->protocol
,
787 iph
->saddr
, pptr
[0])) {
789 * Notify the real server: there is no
790 * existing entry if it is not RST
791 * packet or not TCP packet.
793 if (iph
->protocol
!= IPPROTO_TCP
794 || !is_tcp_reset(skb
)) {
795 icmp_send(skb
,ICMP_DEST_UNREACH
,
796 ICMP_PORT_UNREACH
, 0);
801 IP_VS_DBG_PKT(12, pp
, skb
, 0,
802 "packet continues traversal as normal");
806 IP_VS_DBG_PKT(11, pp
, skb
, 0, "Outgoing packet");
808 if (!ip_vs_make_skb_writable(pskb
, ihl
))
811 /* mangle the packet */
812 if (pp
->snat_handler
&& !pp
->snat_handler(pskb
, pp
, cp
))
815 skb
->nh
.iph
->saddr
= cp
->vaddr
;
816 ip_send_check(skb
->nh
.iph
);
818 IP_VS_DBG_PKT(10, pp
, skb
, 0, "After SNAT");
820 ip_vs_out_stats(cp
, skb
);
821 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pp
);
824 skb
->nfcache
|= NFC_IPVS_PROPERTY
;
837 * Handle ICMP messages in the outside-to-inside direction (incoming).
838 * Find any that might be relevant, check against existing connections,
839 * forward to the right destination host if relevant.
840 * Currently handles error types - unreachable, quench, ttl exceeded.
843 ip_vs_in_icmp(struct sk_buff
**pskb
, int *related
, unsigned int hooknum
)
845 struct sk_buff
*skb
= *pskb
;
847 struct icmphdr _icmph
, *ic
;
848 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
849 struct ip_vs_conn
*cp
;
850 struct ip_vs_protocol
*pp
;
851 unsigned int offset
, ihl
, verdict
;
855 /* reassemble IP fragments */
856 if (skb
->nh
.iph
->frag_off
& __constant_htons(IP_MF
|IP_OFFSET
)) {
857 skb
= ip_vs_gather_frags(skb
,
858 hooknum
== NF_IP_LOCAL_IN
?
859 IP_DEFRAG_VS_IN
: IP_DEFRAG_VS_FWD
);
866 offset
= ihl
= iph
->ihl
* 4;
867 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
871 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
872 ic
->type
, ntohs(icmp_id(ic
)),
873 NIPQUAD(iph
->saddr
), NIPQUAD(iph
->daddr
));
876 * Work through seeing if this is for us.
877 * These checks are supposed to be in an order that means easy
878 * things are checked first to speed up processing.... however
879 * this means that some packets will manage to get a long way
880 * down this stack and then be rejected, but that's life.
882 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
883 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
884 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
889 /* Now find the contained IP header */
890 offset
+= sizeof(_icmph
);
891 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
893 return NF_ACCEPT
; /* The packet looks wrong, ignore */
895 pp
= ip_vs_proto_get(cih
->protocol
);
899 /* Is the embedded protocol header present? */
900 if (unlikely(cih
->frag_off
& __constant_htons(IP_OFFSET
) &&
904 IP_VS_DBG_PKT(11, pp
, skb
, offset
, "Checking incoming ICMP for");
906 offset
+= cih
->ihl
* 4;
908 /* The embedded headers contain source and dest in reverse order */
909 cp
= pp
->conn_in_get(skb
, pp
, cih
, offset
, 1);
915 /* Ensure the checksum is correct */
916 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
917 ip_vs_checksum_complete(skb
, ihl
)) {
918 /* Failed checksum! */
919 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
920 NIPQUAD(iph
->saddr
));
924 /* do the statistics and put it back */
925 ip_vs_in_stats(cp
, skb
);
926 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
)
927 offset
+= 2 * sizeof(__u16
);
928 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
);
929 /* do not touch skb anymore */
932 __ip_vs_conn_put(cp
);
938 * Check if it's for virtual services, look it up,
939 * and send it on its way...
942 ip_vs_in(unsigned int hooknum
, struct sk_buff
**pskb
,
943 const struct net_device
*in
, const struct net_device
*out
,
944 int (*okfn
)(struct sk_buff
*))
946 struct sk_buff
*skb
= *pskb
;
948 struct ip_vs_protocol
*pp
;
949 struct ip_vs_conn
*cp
;
954 * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
955 * ... don't know why 1st test DOES NOT include 2nd (?)
957 if (unlikely(skb
->pkt_type
!= PACKET_HOST
958 || skb
->dev
== &loopback_dev
|| skb
->sk
)) {
959 IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n",
961 skb
->nh
.iph
->protocol
,
962 NIPQUAD(skb
->nh
.iph
->daddr
));
967 if (unlikely(iph
->protocol
== IPPROTO_ICMP
)) {
968 int related
, verdict
= ip_vs_in_icmp(pskb
, &related
, hooknum
);
976 /* Protocol supported? */
977 pp
= ip_vs_proto_get(iph
->protocol
);
984 * Check if the packet belongs to an existing connection entry
986 cp
= pp
->conn_in_get(skb
, pp
, iph
, ihl
, 0);
991 if (!pp
->conn_schedule(skb
, pp
, &v
, &cp
))
996 /* sorry, all this trouble for a no-hit :) */
997 IP_VS_DBG_PKT(12, pp
, skb
, 0,
998 "packet continues traversal as normal");
1002 IP_VS_DBG_PKT(11, pp
, skb
, 0, "Incoming packet");
1004 /* Check the server status */
1005 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1006 /* the destination server is not available */
1008 if (sysctl_ip_vs_expire_nodest_conn
) {
1009 /* try to expire the connection immediately */
1010 ip_vs_conn_expire_now(cp
);
1012 /* don't restart its timer, and silently
1014 __ip_vs_conn_put(cp
);
1019 ip_vs_in_stats(cp
, skb
);
1020 restart
= ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pp
);
1021 if (cp
->packet_xmit
)
1022 ret
= cp
->packet_xmit(skb
, cp
, pp
);
1023 /* do not touch skb anymore */
1025 IP_VS_DBG_RL("warning: packet_xmit is null");
1029 /* increase its packet counter and check if it is needed
1030 to be synchronized */
1031 atomic_inc(&cp
->in_pkts
);
1032 if ((ip_vs_sync_state
& IP_VS_STATE_MASTER
) &&
1033 (cp
->protocol
!= IPPROTO_TCP
||
1034 cp
->state
== IP_VS_TCP_S_ESTABLISHED
) &&
1035 (atomic_read(&cp
->in_pkts
) % sysctl_ip_vs_sync_threshold
[1]
1036 == sysctl_ip_vs_sync_threshold
[0]))
1037 ip_vs_sync_conn(cp
);
1045 * It is hooked at the NF_IP_FORWARD chain, in order to catch ICMP
1046 * related packets destined for 0.0.0.0/0.
1047 * When fwmark-based virtual service is used, such as transparent
1048 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1049 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1050 * sent to ip_vs_in_icmp. So, catch them at the NF_IP_FORWARD chain
1051 * and send them to ip_vs_in_icmp.
1054 ip_vs_forward_icmp(unsigned int hooknum
, struct sk_buff
**pskb
,
1055 const struct net_device
*in
, const struct net_device
*out
,
1056 int (*okfn
)(struct sk_buff
*))
1060 if ((*pskb
)->nh
.iph
->protocol
!= IPPROTO_ICMP
)
1063 return ip_vs_in_icmp(pskb
, &r
, hooknum
);
1067 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1068 or VS/NAT(change destination), so that filtering rules can be
1070 static struct nf_hook_ops ip_vs_in_ops
= {
1072 .owner
= THIS_MODULE
,
1074 .hooknum
= NF_IP_LOCAL_IN
,
1078 /* After packet filtering, change source only for VS/NAT */
1079 static struct nf_hook_ops ip_vs_out_ops
= {
1081 .owner
= THIS_MODULE
,
1083 .hooknum
= NF_IP_FORWARD
,
1087 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1088 destined for 0.0.0.0/0, which is for incoming IPVS connections */
1089 static struct nf_hook_ops ip_vs_forward_icmp_ops
= {
1090 .hook
= ip_vs_forward_icmp
,
1091 .owner
= THIS_MODULE
,
1093 .hooknum
= NF_IP_FORWARD
,
1097 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1098 static struct nf_hook_ops ip_vs_post_routing_ops
= {
1099 .hook
= ip_vs_post_routing
,
1100 .owner
= THIS_MODULE
,
1102 .hooknum
= NF_IP_POST_ROUTING
,
1103 .priority
= NF_IP_PRI_NAT_SRC
-1,
1108 * Initialize IP Virtual Server
1110 static int __init
ip_vs_init(void)
1114 ret
= ip_vs_control_init();
1116 IP_VS_ERR("can't setup control.\n");
1117 goto cleanup_nothing
;
1120 ip_vs_protocol_init();
1122 ret
= ip_vs_app_init();
1124 IP_VS_ERR("can't setup application helper.\n");
1125 goto cleanup_protocol
;
1128 ret
= ip_vs_conn_init();
1130 IP_VS_ERR("can't setup connection table.\n");
1134 ret
= nf_register_hook(&ip_vs_in_ops
);
1136 IP_VS_ERR("can't register in hook.\n");
1140 ret
= nf_register_hook(&ip_vs_out_ops
);
1142 IP_VS_ERR("can't register out hook.\n");
1145 ret
= nf_register_hook(&ip_vs_post_routing_ops
);
1147 IP_VS_ERR("can't register post_routing hook.\n");
1148 goto cleanup_outops
;
1150 ret
= nf_register_hook(&ip_vs_forward_icmp_ops
);
1152 IP_VS_ERR("can't register forward_icmp hook.\n");
1153 goto cleanup_postroutingops
;
1156 IP_VS_INFO("ipvs loaded.\n");
1159 cleanup_postroutingops
:
1160 nf_unregister_hook(&ip_vs_post_routing_ops
);
1162 nf_unregister_hook(&ip_vs_out_ops
);
1164 nf_unregister_hook(&ip_vs_in_ops
);
1166 ip_vs_conn_cleanup();
1168 ip_vs_app_cleanup();
1170 ip_vs_protocol_cleanup();
1171 ip_vs_control_cleanup();
1176 static void __exit
ip_vs_cleanup(void)
1178 nf_unregister_hook(&ip_vs_forward_icmp_ops
);
1179 nf_unregister_hook(&ip_vs_post_routing_ops
);
1180 nf_unregister_hook(&ip_vs_out_ops
);
1181 nf_unregister_hook(&ip_vs_in_ops
);
1182 ip_vs_conn_cleanup();
1183 ip_vs_app_cleanup();
1184 ip_vs_protocol_cleanup();
1185 ip_vs_control_cleanup();
1186 IP_VS_INFO("ipvs unloaded.\n");
1189 module_init(ip_vs_init
);
1190 module_exit(ip_vs_cleanup
);
1191 MODULE_LICENSE("GPL");