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 */
198 union nf_inet_addr snet
; /* source network of the client,
201 ip_vs_fill_iphdr(svc
->af
, skb_network_header(skb
), &iph
);
203 /* Mask saddr with the netmask to adjust template granularity */
204 #ifdef CONFIG_IP_VS_IPV6
205 if (svc
->af
== AF_INET6
)
206 ipv6_addr_prefix(&snet
.in6
, &iph
.saddr
.in6
, svc
->netmask
);
209 snet
.ip
= iph
.saddr
.ip
& svc
->netmask
;
211 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
213 IP_VS_DBG_ADDR(svc
->af
, &iph
.saddr
), ntohs(ports
[0]),
214 IP_VS_DBG_ADDR(svc
->af
, &iph
.daddr
), ntohs(ports
[1]),
215 IP_VS_DBG_ADDR(svc
->af
, &snet
));
218 * As far as we know, FTP is a very complicated network protocol, and
219 * it uses control connection and data connections. For active FTP,
220 * FTP server initialize data connection to the client, its source port
221 * is often 20. For passive FTP, FTP server tells the clients the port
222 * that it passively listens to, and the client issues the data
223 * connection. In the tunneling or direct routing mode, the load
224 * balancer is on the client-to-server half of connection, the port
225 * number is unknown to the load balancer. So, a conn template like
226 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
227 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
228 * is created for other persistent services.
230 if (ports
[1] == svc
->port
) {
231 /* Check if a template already exists */
232 if (svc
->port
!= FTPPORT
)
233 ct
= ip_vs_ct_in_get(svc
->af
, iph
.protocol
, &snet
, 0,
234 &iph
.daddr
, ports
[1]);
236 ct
= ip_vs_ct_in_get(svc
->af
, iph
.protocol
, &snet
, 0,
239 if (!ct
|| !ip_vs_check_template(ct
)) {
241 * No template found or the dest of the connection
242 * template is not available.
244 dest
= svc
->scheduler
->schedule(svc
, skb
);
246 IP_VS_DBG(1, "p-schedule: no dest found.\n");
251 * Create a template like <protocol,caddr,0,
252 * vaddr,vport,daddr,dport> for non-ftp service,
253 * and <protocol,caddr,0,vaddr,0,daddr,0>
256 if (svc
->port
!= FTPPORT
)
257 ct
= ip_vs_conn_new(svc
->af
, iph
.protocol
,
261 &dest
->addr
, dest
->port
,
262 IP_VS_CONN_F_TEMPLATE
,
265 ct
= ip_vs_conn_new(svc
->af
, iph
.protocol
,
269 IP_VS_CONN_F_TEMPLATE
,
274 ct
->timeout
= svc
->timeout
;
276 /* set destination with the found template */
282 * Note: persistent fwmark-based services and persistent
283 * port zero service are handled here.
284 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
285 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
288 union nf_inet_addr fwmark
= {
289 .ip
= htonl(svc
->fwmark
)
292 ct
= ip_vs_ct_in_get(svc
->af
, IPPROTO_IP
, &snet
, 0,
295 ct
= ip_vs_ct_in_get(svc
->af
, iph
.protocol
, &snet
, 0,
298 if (!ct
|| !ip_vs_check_template(ct
)) {
300 * If it is not persistent port zero, return NULL,
301 * otherwise create a connection template.
306 dest
= svc
->scheduler
->schedule(svc
, skb
);
308 IP_VS_DBG(1, "p-schedule: no dest found.\n");
313 * Create a template according to the service
316 union nf_inet_addr fwmark
= {
317 .ip
= htonl(svc
->fwmark
)
320 ct
= ip_vs_conn_new(svc
->af
, IPPROTO_IP
,
324 IP_VS_CONN_F_TEMPLATE
,
327 ct
= ip_vs_conn_new(svc
->af
, iph
.protocol
,
331 IP_VS_CONN_F_TEMPLATE
,
336 ct
->timeout
= svc
->timeout
;
338 /* set destination with the found template */
344 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
345 && iph
.protocol
== IPPROTO_UDP
)?
346 IP_VS_CONN_F_ONE_PACKET
: 0;
349 * Create a new connection according to the template
351 cp
= ip_vs_conn_new(svc
->af
, 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 ip_vs_iphdr iph
;
384 struct ip_vs_dest
*dest
;
385 __be16 _ports
[2], *pptr
, flags
;
387 ip_vs_fill_iphdr(svc
->af
, skb_network_header(skb
), &iph
);
388 pptr
= skb_header_pointer(skb
, iph
.len
, 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 pr_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");
415 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
416 && iph
.protocol
== IPPROTO_UDP
)?
417 IP_VS_CONN_F_ONE_PACKET
: 0;
420 * Create a connection entry.
422 cp
= ip_vs_conn_new(svc
->af
, iph
.protocol
,
425 &dest
->addr
, dest
->port
? dest
->port
: pptr
[1],
431 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
432 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
434 IP_VS_DBG_ADDR(svc
->af
, &cp
->caddr
), ntohs(cp
->cport
),
435 IP_VS_DBG_ADDR(svc
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
436 IP_VS_DBG_ADDR(svc
->af
, &cp
->daddr
), ntohs(cp
->dport
),
437 cp
->flags
, atomic_read(&cp
->refcnt
));
439 ip_vs_conn_stats(cp
, svc
);
445 * Pass or drop the packet.
446 * Called by ip_vs_in, when the virtual service is available but
447 * no destination is available for a new connection.
449 int ip_vs_leave(struct ip_vs_service
*svc
, struct sk_buff
*skb
,
450 struct ip_vs_protocol
*pp
)
452 __be16 _ports
[2], *pptr
;
453 struct ip_vs_iphdr iph
;
455 ip_vs_fill_iphdr(svc
->af
, skb_network_header(skb
), &iph
);
457 pptr
= skb_header_pointer(skb
, iph
.len
, sizeof(_ports
), _ports
);
459 ip_vs_service_put(svc
);
463 #ifdef CONFIG_IP_VS_IPV6
464 if (svc
->af
== AF_INET6
)
465 unicast
= ipv6_addr_type(&iph
.daddr
.in6
) & IPV6_ADDR_UNICAST
;
468 unicast
= (inet_addr_type(&init_net
, iph
.daddr
.ip
) == RTN_UNICAST
);
470 /* if it is fwmark-based service, the cache_bypass sysctl is up
471 and the destination is a non-local unicast, then create
472 a cache_bypass connection entry */
473 if (sysctl_ip_vs_cache_bypass
&& svc
->fwmark
&& unicast
) {
475 struct ip_vs_conn
*cp
;
476 __u16 flags
= (svc
->flags
& IP_VS_SVC_F_ONEPACKET
&&
477 iph
.protocol
== IPPROTO_UDP
)?
478 IP_VS_CONN_F_ONE_PACKET
: 0;
479 union nf_inet_addr daddr
= { .all
= { 0, 0, 0, 0 } };
481 ip_vs_service_put(svc
);
483 /* create a new connection entry */
484 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__
);
485 cp
= ip_vs_conn_new(svc
->af
, iph
.protocol
,
489 IP_VS_CONN_F_BYPASS
| flags
,
495 ip_vs_in_stats(cp
, skb
);
498 cs
= ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pp
);
500 /* transmit the first SYN packet */
501 ret
= cp
->packet_xmit(skb
, cp
, pp
);
502 /* do not touch skb anymore */
504 atomic_inc(&cp
->in_pkts
);
510 * When the virtual ftp service is presented, packets destined
511 * for other services on the VIP may get here (except services
512 * listed in the ipvs table), pass the packets, because it is
513 * not ipvs job to decide to drop the packets.
515 if ((svc
->port
== FTPPORT
) && (pptr
[1] != FTPPORT
)) {
516 ip_vs_service_put(svc
);
520 ip_vs_service_put(svc
);
523 * Notify the client that the destination is unreachable, and
524 * release the socket buffer.
525 * Since it is in IP layer, the TCP socket is not actually
526 * created, the TCP RST packet cannot be sent, instead that
527 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
529 #ifdef CONFIG_IP_VS_IPV6
530 if (svc
->af
== AF_INET6
)
531 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
534 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
541 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
542 * chain, and is used for VS/NAT.
543 * It detects packets for VS/NAT connections and sends the packets
544 * immediately. This can avoid that iptable_nat mangles the packets
547 static unsigned int ip_vs_post_routing(unsigned int hooknum
,
549 const struct net_device
*in
,
550 const struct net_device
*out
,
551 int (*okfn
)(struct sk_buff
*))
553 if (!skb
->ipvs_property
)
555 /* The packet was sent from IPVS, exit this chain */
559 __sum16
ip_vs_checksum_complete(struct sk_buff
*skb
, int offset
)
561 return csum_fold(skb_checksum(skb
, offset
, skb
->len
- offset
, 0));
564 static inline int ip_vs_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
566 int err
= ip_defrag(skb
, user
);
569 ip_send_check(ip_hdr(skb
));
574 #ifdef CONFIG_IP_VS_IPV6
575 static inline int ip_vs_gather_frags_v6(struct sk_buff
*skb
, u_int32_t user
)
577 /* TODO IPv6: Find out what to do here for IPv6 */
583 * Packet has been made sufficiently writable in caller
584 * - inout: 1=in->out, 0=out->in
586 void ip_vs_nat_icmp(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
587 struct ip_vs_conn
*cp
, int inout
)
589 struct iphdr
*iph
= ip_hdr(skb
);
590 unsigned int icmp_offset
= iph
->ihl
*4;
591 struct icmphdr
*icmph
= (struct icmphdr
*)(skb_network_header(skb
) +
593 struct iphdr
*ciph
= (struct iphdr
*)(icmph
+ 1);
596 iph
->saddr
= cp
->vaddr
.ip
;
598 ciph
->daddr
= cp
->vaddr
.ip
;
601 iph
->daddr
= cp
->daddr
.ip
;
603 ciph
->saddr
= cp
->daddr
.ip
;
607 /* the TCP/UDP/SCTP port */
608 if (IPPROTO_TCP
== ciph
->protocol
|| IPPROTO_UDP
== ciph
->protocol
||
609 IPPROTO_SCTP
== ciph
->protocol
) {
610 __be16
*ports
= (void *)ciph
+ ciph
->ihl
*4;
613 ports
[1] = cp
->vport
;
615 ports
[0] = cp
->dport
;
618 /* And finally the ICMP checksum */
620 icmph
->checksum
= ip_vs_checksum_complete(skb
, icmp_offset
);
621 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
624 IP_VS_DBG_PKT(11, pp
, skb
, (void *)ciph
- (void *)iph
,
625 "Forwarding altered outgoing ICMP");
627 IP_VS_DBG_PKT(11, pp
, skb
, (void *)ciph
- (void *)iph
,
628 "Forwarding altered incoming ICMP");
631 #ifdef CONFIG_IP_VS_IPV6
632 void ip_vs_nat_icmp_v6(struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
633 struct ip_vs_conn
*cp
, int inout
)
635 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
636 unsigned int icmp_offset
= sizeof(struct ipv6hdr
);
637 struct icmp6hdr
*icmph
= (struct icmp6hdr
*)(skb_network_header(skb
) +
639 struct ipv6hdr
*ciph
= (struct ipv6hdr
*)(icmph
+ 1);
642 iph
->saddr
= cp
->vaddr
.in6
;
643 ciph
->daddr
= cp
->vaddr
.in6
;
645 iph
->daddr
= cp
->daddr
.in6
;
646 ciph
->saddr
= cp
->daddr
.in6
;
649 /* the TCP/UDP/SCTP port */
650 if (IPPROTO_TCP
== ciph
->nexthdr
|| IPPROTO_UDP
== ciph
->nexthdr
||
651 IPPROTO_SCTP
== ciph
->nexthdr
) {
652 __be16
*ports
= (void *)ciph
+ sizeof(struct ipv6hdr
);
655 ports
[1] = cp
->vport
;
657 ports
[0] = cp
->dport
;
660 /* And finally the ICMP checksum */
661 icmph
->icmp6_cksum
= 0;
662 /* TODO IPv6: is this correct for ICMPv6? */
663 ip_vs_checksum_complete(skb
, icmp_offset
);
664 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
667 IP_VS_DBG_PKT(11, pp
, skb
, (void *)ciph
- (void *)iph
,
668 "Forwarding altered outgoing ICMPv6");
670 IP_VS_DBG_PKT(11, pp
, skb
, (void *)ciph
- (void *)iph
,
671 "Forwarding altered incoming ICMPv6");
675 /* Handle relevant response ICMP messages - forward to the right
676 * destination host. Used for NAT and local client.
678 static int handle_response_icmp(int af
, struct sk_buff
*skb
,
679 union nf_inet_addr
*snet
,
680 __u8 protocol
, struct ip_vs_conn
*cp
,
681 struct ip_vs_protocol
*pp
,
682 unsigned int offset
, unsigned int ihl
)
684 unsigned int verdict
= NF_DROP
;
686 if (IP_VS_FWD_METHOD(cp
) != 0) {
687 pr_err("shouldn't reach here, because the box is on the "
688 "half connection in the tun/dr module.\n");
691 /* Ensure the checksum is correct */
692 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
693 /* Failed checksum! */
694 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
695 IP_VS_DBG_ADDR(af
, snet
));
699 if (IPPROTO_TCP
== protocol
|| IPPROTO_UDP
== protocol
||
700 IPPROTO_SCTP
== protocol
)
701 offset
+= 2 * sizeof(__u16
);
702 if (!skb_make_writable(skb
, offset
))
705 #ifdef CONFIG_IP_VS_IPV6
707 ip_vs_nat_icmp_v6(skb
, pp
, cp
, 1);
710 ip_vs_nat_icmp(skb
, pp
, cp
, 1);
712 /* do the statistics and put it back */
713 ip_vs_out_stats(cp
, skb
);
715 skb
->ipvs_property
= 1;
719 __ip_vs_conn_put(cp
);
725 * Handle ICMP messages in the inside-to-outside direction (outgoing).
726 * Find any that might be relevant, check against existing connections.
727 * Currently handles error types - unreachable, quench, ttl exceeded.
729 static int ip_vs_out_icmp(struct sk_buff
*skb
, int *related
)
732 struct icmphdr _icmph
, *ic
;
733 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
734 struct ip_vs_iphdr ciph
;
735 struct ip_vs_conn
*cp
;
736 struct ip_vs_protocol
*pp
;
737 unsigned int offset
, ihl
;
738 union nf_inet_addr snet
;
742 /* reassemble IP fragments */
743 if (ip_hdr(skb
)->frag_off
& htons(IP_MF
| IP_OFFSET
)) {
744 if (ip_vs_gather_frags(skb
, IP_DEFRAG_VS_OUT
))
749 offset
= ihl
= iph
->ihl
* 4;
750 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
754 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
755 ic
->type
, ntohs(icmp_id(ic
)),
756 &iph
->saddr
, &iph
->daddr
);
759 * Work through seeing if this is for us.
760 * These checks are supposed to be in an order that means easy
761 * things are checked first to speed up processing.... however
762 * this means that some packets will manage to get a long way
763 * down this stack and then be rejected, but that's life.
765 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
766 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
767 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
772 /* Now find the contained IP header */
773 offset
+= sizeof(_icmph
);
774 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
776 return NF_ACCEPT
; /* The packet looks wrong, ignore */
778 pp
= ip_vs_proto_get(cih
->protocol
);
782 /* Is the embedded protocol header present? */
783 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
787 IP_VS_DBG_PKT(11, pp
, skb
, offset
, "Checking outgoing ICMP for");
789 offset
+= cih
->ihl
* 4;
791 ip_vs_fill_iphdr(AF_INET
, cih
, &ciph
);
792 /* The embedded headers contain source and dest in reverse order */
793 cp
= pp
->conn_out_get(AF_INET
, skb
, pp
, &ciph
, offset
, 1);
797 snet
.ip
= iph
->saddr
;
798 return handle_response_icmp(AF_INET
, skb
, &snet
, cih
->protocol
, cp
,
802 #ifdef CONFIG_IP_VS_IPV6
803 static int ip_vs_out_icmp_v6(struct sk_buff
*skb
, int *related
)
806 struct icmp6hdr _icmph
, *ic
;
807 struct ipv6hdr _ciph
, *cih
; /* The ip header contained
809 struct ip_vs_iphdr ciph
;
810 struct ip_vs_conn
*cp
;
811 struct ip_vs_protocol
*pp
;
813 union nf_inet_addr snet
;
817 /* reassemble IP fragments */
818 if (ipv6_hdr(skb
)->nexthdr
== IPPROTO_FRAGMENT
) {
819 if (ip_vs_gather_frags_v6(skb
, IP_DEFRAG_VS_OUT
))
824 offset
= sizeof(struct ipv6hdr
);
825 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
829 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
830 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
831 &iph
->saddr
, &iph
->daddr
);
834 * Work through seeing if this is for us.
835 * These checks are supposed to be in an order that means easy
836 * things are checked first to speed up processing.... however
837 * this means that some packets will manage to get a long way
838 * down this stack and then be rejected, but that's life.
840 if ((ic
->icmp6_type
!= ICMPV6_DEST_UNREACH
) &&
841 (ic
->icmp6_type
!= ICMPV6_PKT_TOOBIG
) &&
842 (ic
->icmp6_type
!= ICMPV6_TIME_EXCEED
)) {
847 /* Now find the contained IP header */
848 offset
+= sizeof(_icmph
);
849 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
851 return NF_ACCEPT
; /* The packet looks wrong, ignore */
853 pp
= ip_vs_proto_get(cih
->nexthdr
);
857 /* Is the embedded protocol header present? */
858 /* TODO: we don't support fragmentation at the moment anyways */
859 if (unlikely(cih
->nexthdr
== IPPROTO_FRAGMENT
&& pp
->dont_defrag
))
862 IP_VS_DBG_PKT(11, pp
, skb
, offset
, "Checking outgoing ICMPv6 for");
864 offset
+= sizeof(struct ipv6hdr
);
866 ip_vs_fill_iphdr(AF_INET6
, cih
, &ciph
);
867 /* The embedded headers contain source and dest in reverse order */
868 cp
= pp
->conn_out_get(AF_INET6
, skb
, pp
, &ciph
, offset
, 1);
872 ipv6_addr_copy(&snet
.in6
, &iph
->saddr
);
873 return handle_response_icmp(AF_INET6
, skb
, &snet
, cih
->nexthdr
, cp
,
874 pp
, offset
, sizeof(struct ipv6hdr
));
879 * Check if sctp chunc is ABORT chunk
881 static inline int is_sctp_abort(const struct sk_buff
*skb
, int nh_len
)
883 sctp_chunkhdr_t
*sch
, schunk
;
884 sch
= skb_header_pointer(skb
, nh_len
+ sizeof(sctp_sctphdr_t
),
885 sizeof(schunk
), &schunk
);
888 if (sch
->type
== SCTP_CID_ABORT
)
893 static inline int is_tcp_reset(const struct sk_buff
*skb
, int nh_len
)
895 struct tcphdr _tcph
, *th
;
897 th
= skb_header_pointer(skb
, nh_len
, sizeof(_tcph
), &_tcph
);
903 /* Handle response packets: rewrite addresses and send away...
904 * Used for NAT and local client.
907 handle_response(int af
, struct sk_buff
*skb
, struct ip_vs_protocol
*pp
,
908 struct ip_vs_conn
*cp
, int ihl
)
910 IP_VS_DBG_PKT(11, pp
, skb
, 0, "Outgoing packet");
912 if (!skb_make_writable(skb
, ihl
))
915 /* mangle the packet */
916 if (pp
->snat_handler
&& !pp
->snat_handler(skb
, pp
, cp
))
919 #ifdef CONFIG_IP_VS_IPV6
921 ipv6_hdr(skb
)->saddr
= cp
->vaddr
.in6
;
925 ip_hdr(skb
)->saddr
= cp
->vaddr
.ip
;
926 ip_send_check(ip_hdr(skb
));
929 /* For policy routing, packets originating from this
930 * machine itself may be routed differently to packets
931 * passing through. We want this packet to be routed as
932 * if it came from this machine itself. So re-compute
933 * the routing information.
935 #ifdef CONFIG_IP_VS_IPV6
936 if (af
== AF_INET6
) {
937 if (ip6_route_me_harder(skb
) != 0)
941 if (ip_route_me_harder(skb
, RTN_LOCAL
) != 0)
944 IP_VS_DBG_PKT(10, pp
, skb
, 0, "After SNAT");
946 ip_vs_out_stats(cp
, skb
);
947 ip_vs_set_state(cp
, IP_VS_DIR_OUTPUT
, skb
, pp
);
950 skb
->ipvs_property
= 1;
962 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
963 * Check if outgoing packet belongs to the established ip_vs_conn.
966 ip_vs_out(unsigned int hooknum
, struct sk_buff
*skb
,
967 const struct net_device
*in
, const struct net_device
*out
,
968 int (*okfn
)(struct sk_buff
*))
970 struct ip_vs_iphdr iph
;
971 struct ip_vs_protocol
*pp
;
972 struct ip_vs_conn
*cp
;
977 af
= (skb
->protocol
== htons(ETH_P_IP
)) ? AF_INET
: AF_INET6
;
979 if (skb
->ipvs_property
)
982 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
983 #ifdef CONFIG_IP_VS_IPV6
984 if (af
== AF_INET6
) {
985 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
986 int related
, verdict
= ip_vs_out_icmp_v6(skb
, &related
);
990 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
994 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
995 int related
, verdict
= ip_vs_out_icmp(skb
, &related
);
999 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
1002 pp
= ip_vs_proto_get(iph
.protocol
);
1006 /* reassemble IP fragments */
1007 #ifdef CONFIG_IP_VS_IPV6
1008 if (af
== AF_INET6
) {
1009 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1010 int related
, verdict
= ip_vs_out_icmp_v6(skb
, &related
);
1015 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
1019 if (unlikely(ip_hdr(skb
)->frag_off
& htons(IP_MF
|IP_OFFSET
) &&
1020 !pp
->dont_defrag
)) {
1021 if (ip_vs_gather_frags(skb
, IP_DEFRAG_VS_OUT
))
1024 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
1028 * Check if the packet belongs to an existing entry
1030 cp
= pp
->conn_out_get(af
, skb
, pp
, &iph
, iph
.len
, 0);
1032 if (unlikely(!cp
)) {
1033 if (sysctl_ip_vs_nat_icmp_send
&&
1034 (pp
->protocol
== IPPROTO_TCP
||
1035 pp
->protocol
== IPPROTO_UDP
||
1036 pp
->protocol
== IPPROTO_SCTP
)) {
1037 __be16 _ports
[2], *pptr
;
1039 pptr
= skb_header_pointer(skb
, iph
.len
,
1040 sizeof(_ports
), _ports
);
1042 return NF_ACCEPT
; /* Not for me */
1043 if (ip_vs_lookup_real_service(af
, iph
.protocol
,
1047 * Notify the real server: there is no
1048 * existing entry if it is not RST
1049 * packet or not TCP packet.
1051 if ((iph
.protocol
!= IPPROTO_TCP
&&
1052 iph
.protocol
!= IPPROTO_SCTP
)
1053 || ((iph
.protocol
== IPPROTO_TCP
1054 && !is_tcp_reset(skb
, iph
.len
))
1055 || (iph
.protocol
== IPPROTO_SCTP
1056 && !is_sctp_abort(skb
,
1058 #ifdef CONFIG_IP_VS_IPV6
1061 ICMPV6_DEST_UNREACH
,
1062 ICMPV6_PORT_UNREACH
,
1068 ICMP_PORT_UNREACH
, 0);
1073 IP_VS_DBG_PKT(12, pp
, skb
, 0,
1074 "packet continues traversal as normal");
1078 return handle_response(af
, skb
, pp
, cp
, iph
.len
);
1083 * Handle ICMP messages in the outside-to-inside direction (incoming).
1084 * Find any that might be relevant, check against existing connections,
1085 * forward to the right destination host if relevant.
1086 * Currently handles error types - unreachable, quench, ttl exceeded.
1089 ip_vs_in_icmp(struct sk_buff
*skb
, int *related
, unsigned int hooknum
)
1092 struct icmphdr _icmph
, *ic
;
1093 struct iphdr _ciph
, *cih
; /* The ip header contained within the ICMP */
1094 struct ip_vs_iphdr ciph
;
1095 struct ip_vs_conn
*cp
;
1096 struct ip_vs_protocol
*pp
;
1097 unsigned int offset
, ihl
, verdict
;
1098 union nf_inet_addr snet
;
1102 /* reassemble IP fragments */
1103 if (ip_hdr(skb
)->frag_off
& htons(IP_MF
| IP_OFFSET
)) {
1104 if (ip_vs_gather_frags(skb
, hooknum
== NF_INET_LOCAL_IN
?
1105 IP_DEFRAG_VS_IN
: IP_DEFRAG_VS_FWD
))
1110 offset
= ihl
= iph
->ihl
* 4;
1111 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1115 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1116 ic
->type
, ntohs(icmp_id(ic
)),
1117 &iph
->saddr
, &iph
->daddr
);
1120 * Work through seeing if this is for us.
1121 * These checks are supposed to be in an order that means easy
1122 * things are checked first to speed up processing.... however
1123 * this means that some packets will manage to get a long way
1124 * down this stack and then be rejected, but that's life.
1126 if ((ic
->type
!= ICMP_DEST_UNREACH
) &&
1127 (ic
->type
!= ICMP_SOURCE_QUENCH
) &&
1128 (ic
->type
!= ICMP_TIME_EXCEEDED
)) {
1133 /* Now find the contained IP header */
1134 offset
+= sizeof(_icmph
);
1135 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1137 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1139 pp
= ip_vs_proto_get(cih
->protocol
);
1143 /* Is the embedded protocol header present? */
1144 if (unlikely(cih
->frag_off
& htons(IP_OFFSET
) &&
1148 IP_VS_DBG_PKT(11, pp
, skb
, offset
, "Checking incoming ICMP for");
1150 offset
+= cih
->ihl
* 4;
1152 ip_vs_fill_iphdr(AF_INET
, cih
, &ciph
);
1153 /* The embedded headers contain source and dest in reverse order */
1154 cp
= pp
->conn_in_get(AF_INET
, skb
, pp
, &ciph
, offset
, 1);
1156 /* The packet could also belong to a local client */
1157 cp
= pp
->conn_out_get(AF_INET
, skb
, pp
, &ciph
, offset
, 1);
1159 snet
.ip
= iph
->saddr
;
1160 return handle_response_icmp(AF_INET
, skb
, &snet
,
1161 cih
->protocol
, cp
, pp
,
1169 /* Ensure the checksum is correct */
1170 if (!skb_csum_unnecessary(skb
) && ip_vs_checksum_complete(skb
, ihl
)) {
1171 /* Failed checksum! */
1172 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1177 /* do the statistics and put it back */
1178 ip_vs_in_stats(cp
, skb
);
1179 if (IPPROTO_TCP
== cih
->protocol
|| IPPROTO_UDP
== cih
->protocol
)
1180 offset
+= 2 * sizeof(__u16
);
1181 verdict
= ip_vs_icmp_xmit(skb
, cp
, pp
, offset
);
1182 /* do not touch skb anymore */
1185 __ip_vs_conn_put(cp
);
1190 #ifdef CONFIG_IP_VS_IPV6
1192 ip_vs_in_icmp_v6(struct sk_buff
*skb
, int *related
, unsigned int hooknum
)
1194 struct ipv6hdr
*iph
;
1195 struct icmp6hdr _icmph
, *ic
;
1196 struct ipv6hdr _ciph
, *cih
; /* The ip header contained
1198 struct ip_vs_iphdr ciph
;
1199 struct ip_vs_conn
*cp
;
1200 struct ip_vs_protocol
*pp
;
1201 unsigned int offset
, verdict
;
1202 union nf_inet_addr snet
;
1206 /* reassemble IP fragments */
1207 if (ipv6_hdr(skb
)->nexthdr
== IPPROTO_FRAGMENT
) {
1208 if (ip_vs_gather_frags_v6(skb
, hooknum
== NF_INET_LOCAL_IN
?
1214 iph
= ipv6_hdr(skb
);
1215 offset
= sizeof(struct ipv6hdr
);
1216 ic
= skb_header_pointer(skb
, offset
, sizeof(_icmph
), &_icmph
);
1220 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
1221 ic
->icmp6_type
, ntohs(icmpv6_id(ic
)),
1222 &iph
->saddr
, &iph
->daddr
);
1225 * Work through seeing if this is for us.
1226 * These checks are supposed to be in an order that means easy
1227 * things are checked first to speed up processing.... however
1228 * this means that some packets will manage to get a long way
1229 * down this stack and then be rejected, but that's life.
1231 if ((ic
->icmp6_type
!= ICMPV6_DEST_UNREACH
) &&
1232 (ic
->icmp6_type
!= ICMPV6_PKT_TOOBIG
) &&
1233 (ic
->icmp6_type
!= ICMPV6_TIME_EXCEED
)) {
1238 /* Now find the contained IP header */
1239 offset
+= sizeof(_icmph
);
1240 cih
= skb_header_pointer(skb
, offset
, sizeof(_ciph
), &_ciph
);
1242 return NF_ACCEPT
; /* The packet looks wrong, ignore */
1244 pp
= ip_vs_proto_get(cih
->nexthdr
);
1248 /* Is the embedded protocol header present? */
1249 /* TODO: we don't support fragmentation at the moment anyways */
1250 if (unlikely(cih
->nexthdr
== IPPROTO_FRAGMENT
&& pp
->dont_defrag
))
1253 IP_VS_DBG_PKT(11, pp
, skb
, offset
, "Checking incoming ICMPv6 for");
1255 offset
+= sizeof(struct ipv6hdr
);
1257 ip_vs_fill_iphdr(AF_INET6
, cih
, &ciph
);
1258 /* The embedded headers contain source and dest in reverse order */
1259 cp
= pp
->conn_in_get(AF_INET6
, skb
, pp
, &ciph
, offset
, 1);
1261 /* The packet could also belong to a local client */
1262 cp
= pp
->conn_out_get(AF_INET6
, skb
, pp
, &ciph
, offset
, 1);
1264 ipv6_addr_copy(&snet
.in6
, &iph
->saddr
);
1265 return handle_response_icmp(AF_INET6
, skb
, &snet
,
1268 sizeof(struct ipv6hdr
));
1275 /* do the statistics and put it back */
1276 ip_vs_in_stats(cp
, skb
);
1277 if (IPPROTO_TCP
== cih
->nexthdr
|| IPPROTO_UDP
== cih
->nexthdr
||
1278 IPPROTO_SCTP
== cih
->nexthdr
)
1279 offset
+= 2 * sizeof(__u16
);
1280 verdict
= ip_vs_icmp_xmit_v6(skb
, cp
, pp
, offset
);
1281 /* do not touch skb anymore */
1283 __ip_vs_conn_put(cp
);
1291 * Check if it's for virtual services, look it up,
1292 * and send it on its way...
1295 ip_vs_in(unsigned int hooknum
, struct sk_buff
*skb
,
1296 const struct net_device
*in
, const struct net_device
*out
,
1297 int (*okfn
)(struct sk_buff
*))
1299 struct ip_vs_iphdr iph
;
1300 struct ip_vs_protocol
*pp
;
1301 struct ip_vs_conn
*cp
;
1302 int ret
, restart
, af
, pkts
;
1304 af
= (skb
->protocol
== htons(ETH_P_IP
)) ? AF_INET
: AF_INET6
;
1306 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
1309 * Big tappo: only PACKET_HOST, including loopback for local client
1310 * Don't handle local packets on IPv6 for now
1312 if (unlikely(skb
->pkt_type
!= PACKET_HOST
)) {
1313 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
1316 IP_VS_DBG_ADDR(af
, &iph
.daddr
));
1320 #ifdef CONFIG_IP_VS_IPV6
1321 if (af
== AF_INET6
) {
1322 if (unlikely(iph
.protocol
== IPPROTO_ICMPV6
)) {
1323 int related
, verdict
= ip_vs_in_icmp_v6(skb
, &related
, hooknum
);
1327 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
1331 if (unlikely(iph
.protocol
== IPPROTO_ICMP
)) {
1332 int related
, verdict
= ip_vs_in_icmp(skb
, &related
, hooknum
);
1336 ip_vs_fill_iphdr(af
, skb_network_header(skb
), &iph
);
1339 /* Protocol supported? */
1340 pp
= ip_vs_proto_get(iph
.protocol
);
1345 * Check if the packet belongs to an existing connection entry
1347 cp
= pp
->conn_in_get(af
, skb
, pp
, &iph
, iph
.len
, 0);
1349 if (unlikely(!cp
)) {
1352 /* For local client packets, it could be a response */
1353 cp
= pp
->conn_out_get(af
, skb
, pp
, &iph
, iph
.len
, 0);
1355 return handle_response(af
, skb
, pp
, cp
, iph
.len
);
1357 if (!pp
->conn_schedule(af
, skb
, pp
, &v
, &cp
))
1361 if (unlikely(!cp
)) {
1362 /* sorry, all this trouble for a no-hit :) */
1363 IP_VS_DBG_PKT(12, pp
, skb
, 0,
1364 "packet continues traversal as normal");
1368 IP_VS_DBG_PKT(11, pp
, skb
, 0, "Incoming packet");
1370 /* Check the server status */
1371 if (cp
->dest
&& !(cp
->dest
->flags
& IP_VS_DEST_F_AVAILABLE
)) {
1372 /* the destination server is not available */
1374 if (sysctl_ip_vs_expire_nodest_conn
) {
1375 /* try to expire the connection immediately */
1376 ip_vs_conn_expire_now(cp
);
1378 /* don't restart its timer, and silently
1380 __ip_vs_conn_put(cp
);
1384 ip_vs_in_stats(cp
, skb
);
1385 restart
= ip_vs_set_state(cp
, IP_VS_DIR_INPUT
, skb
, pp
);
1386 if (cp
->packet_xmit
)
1387 ret
= cp
->packet_xmit(skb
, cp
, pp
);
1388 /* do not touch skb anymore */
1390 IP_VS_DBG_RL("warning: packet_xmit is null");
1394 /* Increase its packet counter and check if it is needed
1395 * to be synchronized
1397 * Sync connection if it is about to close to
1398 * encorage the standby servers to update the connections timeout
1400 pkts
= atomic_add_return(1, &cp
->in_pkts
);
1401 if (af
== AF_INET
&& (ip_vs_sync_state
& IP_VS_STATE_MASTER
) &&
1402 cp
->protocol
== IPPROTO_SCTP
) {
1403 if ((cp
->state
== IP_VS_SCTP_S_ESTABLISHED
&&
1404 (atomic_read(&cp
->in_pkts
) %
1405 sysctl_ip_vs_sync_threshold
[1]
1406 == sysctl_ip_vs_sync_threshold
[0])) ||
1407 (cp
->old_state
!= cp
->state
&&
1408 ((cp
->state
== IP_VS_SCTP_S_CLOSED
) ||
1409 (cp
->state
== IP_VS_SCTP_S_SHUT_ACK_CLI
) ||
1410 (cp
->state
== IP_VS_SCTP_S_SHUT_ACK_SER
)))) {
1411 ip_vs_sync_conn(cp
);
1416 if (af
== AF_INET
&&
1417 (ip_vs_sync_state
& IP_VS_STATE_MASTER
) &&
1418 (((cp
->protocol
!= IPPROTO_TCP
||
1419 cp
->state
== IP_VS_TCP_S_ESTABLISHED
) &&
1420 (pkts
% sysctl_ip_vs_sync_threshold
[1]
1421 == sysctl_ip_vs_sync_threshold
[0])) ||
1422 ((cp
->protocol
== IPPROTO_TCP
) && (cp
->old_state
!= cp
->state
) &&
1423 ((cp
->state
== IP_VS_TCP_S_FIN_WAIT
) ||
1424 (cp
->state
== IP_VS_TCP_S_CLOSE
) ||
1425 (cp
->state
== IP_VS_TCP_S_CLOSE_WAIT
) ||
1426 (cp
->state
== IP_VS_TCP_S_TIME_WAIT
)))))
1427 ip_vs_sync_conn(cp
);
1429 cp
->old_state
= cp
->state
;
1437 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1438 * related packets destined for 0.0.0.0/0.
1439 * When fwmark-based virtual service is used, such as transparent
1440 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1441 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1442 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1443 * and send them to ip_vs_in_icmp.
1446 ip_vs_forward_icmp(unsigned int hooknum
, struct sk_buff
*skb
,
1447 const struct net_device
*in
, const struct net_device
*out
,
1448 int (*okfn
)(struct sk_buff
*))
1452 if (ip_hdr(skb
)->protocol
!= IPPROTO_ICMP
)
1455 return ip_vs_in_icmp(skb
, &r
, hooknum
);
1458 #ifdef CONFIG_IP_VS_IPV6
1460 ip_vs_forward_icmp_v6(unsigned int hooknum
, struct sk_buff
*skb
,
1461 const struct net_device
*in
, const struct net_device
*out
,
1462 int (*okfn
)(struct sk_buff
*))
1466 if (ipv6_hdr(skb
)->nexthdr
!= IPPROTO_ICMPV6
)
1469 return ip_vs_in_icmp_v6(skb
, &r
, hooknum
);
1474 static struct nf_hook_ops ip_vs_ops
[] __read_mostly
= {
1475 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1476 * or VS/NAT(change destination), so that filtering rules can be
1477 * applied to IPVS. */
1480 .owner
= THIS_MODULE
,
1482 .hooknum
= NF_INET_LOCAL_IN
,
1485 /* After packet filtering, change source only for VS/NAT */
1488 .owner
= THIS_MODULE
,
1490 .hooknum
= NF_INET_FORWARD
,
1493 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1494 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1496 .hook
= ip_vs_forward_icmp
,
1497 .owner
= THIS_MODULE
,
1499 .hooknum
= NF_INET_FORWARD
,
1502 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1504 .hook
= ip_vs_post_routing
,
1505 .owner
= THIS_MODULE
,
1507 .hooknum
= NF_INET_POST_ROUTING
,
1508 .priority
= NF_IP_PRI_NAT_SRC
-1,
1510 #ifdef CONFIG_IP_VS_IPV6
1511 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1512 * or VS/NAT(change destination), so that filtering rules can be
1513 * applied to IPVS. */
1516 .owner
= THIS_MODULE
,
1518 .hooknum
= NF_INET_LOCAL_IN
,
1521 /* After packet filtering, change source only for VS/NAT */
1524 .owner
= THIS_MODULE
,
1526 .hooknum
= NF_INET_FORWARD
,
1529 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1530 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1532 .hook
= ip_vs_forward_icmp_v6
,
1533 .owner
= THIS_MODULE
,
1535 .hooknum
= NF_INET_FORWARD
,
1538 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1540 .hook
= ip_vs_post_routing
,
1541 .owner
= THIS_MODULE
,
1543 .hooknum
= NF_INET_POST_ROUTING
,
1544 .priority
= NF_IP6_PRI_NAT_SRC
-1,
1551 * Initialize IP Virtual Server
1553 static int __init
ip_vs_init(void)
1557 ip_vs_estimator_init();
1559 ret
= ip_vs_control_init();
1561 pr_err("can't setup control.\n");
1562 goto cleanup_estimator
;
1565 ip_vs_protocol_init();
1567 ret
= ip_vs_app_init();
1569 pr_err("can't setup application helper.\n");
1570 goto cleanup_protocol
;
1573 ret
= ip_vs_conn_init();
1575 pr_err("can't setup connection table.\n");
1579 ret
= nf_register_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
1581 pr_err("can't register hooks.\n");
1585 pr_info("ipvs loaded.\n");
1589 ip_vs_conn_cleanup();
1591 ip_vs_app_cleanup();
1593 ip_vs_protocol_cleanup();
1594 ip_vs_control_cleanup();
1596 ip_vs_estimator_cleanup();
1600 static void __exit
ip_vs_cleanup(void)
1602 nf_unregister_hooks(ip_vs_ops
, ARRAY_SIZE(ip_vs_ops
));
1603 ip_vs_conn_cleanup();
1604 ip_vs_app_cleanup();
1605 ip_vs_protocol_cleanup();
1606 ip_vs_control_cleanup();
1607 ip_vs_estimator_cleanup();
1608 pr_info("ipvs unloaded.\n");
1611 module_init(ip_vs_init
);
1612 module_exit(ip_vs_cleanup
);
1613 MODULE_LICENSE("GPL");