IPVS: Activate IPv6 Netfilter hooks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / ipvs / ip_vs_core.c
blob7d3de9db5ac523edc37202b4a52deb83e47b86e0
1 /*
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
6 * cluster of servers.
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
19 * and others.
21 * Changes:
22 * Paul `Rusty' Russell properly handle non-linear skbs
23 * Harald Welte don't use nfcache
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/ip.h>
30 #include <linux/tcp.h>
31 #include <linux/icmp.h>
33 #include <net/ip.h>
34 #include <net/tcp.h>
35 #include <net/udp.h>
36 #include <net/icmp.h> /* for icmp_send */
37 #include <net/route.h>
39 #include <linux/netfilter.h>
40 #include <linux/netfilter_ipv4.h>
42 #ifdef CONFIG_IP_VS_IPV6
43 #include <net/ipv6.h>
44 #include <linux/netfilter_ipv6.h>
45 #endif
47 #include <net/ip_vs.h>
50 EXPORT_SYMBOL(register_ip_vs_scheduler);
51 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
52 EXPORT_SYMBOL(ip_vs_skb_replace);
53 EXPORT_SYMBOL(ip_vs_proto_name);
54 EXPORT_SYMBOL(ip_vs_conn_new);
55 EXPORT_SYMBOL(ip_vs_conn_in_get);
56 EXPORT_SYMBOL(ip_vs_conn_out_get);
57 #ifdef CONFIG_IP_VS_PROTO_TCP
58 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
59 #endif
60 EXPORT_SYMBOL(ip_vs_conn_put);
61 #ifdef CONFIG_IP_VS_DEBUG
62 EXPORT_SYMBOL(ip_vs_get_debug_level);
63 #endif
66 /* ID used in ICMP lookups */
67 #define icmp_id(icmph) (((icmph)->un).echo.id)
68 #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
70 const char *ip_vs_proto_name(unsigned proto)
72 static char buf[20];
74 switch (proto) {
75 case IPPROTO_IP:
76 return "IP";
77 case IPPROTO_UDP:
78 return "UDP";
79 case IPPROTO_TCP:
80 return "TCP";
81 case IPPROTO_ICMP:
82 return "ICMP";
83 #ifdef CONFIG_IP_VS_IPV6
84 case IPPROTO_ICMPV6:
85 return "ICMPv6";
86 #endif
87 default:
88 sprintf(buf, "IP_%d", proto);
89 return buf;
93 void ip_vs_init_hash_table(struct list_head *table, int rows)
95 while (--rows >= 0)
96 INIT_LIST_HEAD(&table[rows]);
99 static inline void
100 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
102 struct ip_vs_dest *dest = cp->dest;
103 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
104 spin_lock(&dest->stats.lock);
105 dest->stats.inpkts++;
106 dest->stats.inbytes += skb->len;
107 spin_unlock(&dest->stats.lock);
109 spin_lock(&dest->svc->stats.lock);
110 dest->svc->stats.inpkts++;
111 dest->svc->stats.inbytes += skb->len;
112 spin_unlock(&dest->svc->stats.lock);
114 spin_lock(&ip_vs_stats.lock);
115 ip_vs_stats.inpkts++;
116 ip_vs_stats.inbytes += skb->len;
117 spin_unlock(&ip_vs_stats.lock);
122 static inline void
123 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
125 struct ip_vs_dest *dest = cp->dest;
126 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
127 spin_lock(&dest->stats.lock);
128 dest->stats.outpkts++;
129 dest->stats.outbytes += skb->len;
130 spin_unlock(&dest->stats.lock);
132 spin_lock(&dest->svc->stats.lock);
133 dest->svc->stats.outpkts++;
134 dest->svc->stats.outbytes += skb->len;
135 spin_unlock(&dest->svc->stats.lock);
137 spin_lock(&ip_vs_stats.lock);
138 ip_vs_stats.outpkts++;
139 ip_vs_stats.outbytes += skb->len;
140 spin_unlock(&ip_vs_stats.lock);
145 static inline void
146 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
148 spin_lock(&cp->dest->stats.lock);
149 cp->dest->stats.conns++;
150 spin_unlock(&cp->dest->stats.lock);
152 spin_lock(&svc->stats.lock);
153 svc->stats.conns++;
154 spin_unlock(&svc->stats.lock);
156 spin_lock(&ip_vs_stats.lock);
157 ip_vs_stats.conns++;
158 spin_unlock(&ip_vs_stats.lock);
162 static inline int
163 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
164 const struct sk_buff *skb,
165 struct ip_vs_protocol *pp)
167 if (unlikely(!pp->state_transition))
168 return 0;
169 return pp->state_transition(cp, direction, skb, pp);
174 * IPVS persistent scheduling function
175 * It creates a connection entry according to its template if exists,
176 * or selects a server and creates a connection entry plus a template.
177 * Locking: we are svc user (svc->refcnt), so we hold all dests too
178 * Protocols supported: TCP, UDP
180 static struct ip_vs_conn *
181 ip_vs_sched_persist(struct ip_vs_service *svc,
182 const struct sk_buff *skb,
183 __be16 ports[2])
185 struct ip_vs_conn *cp = NULL;
186 struct ip_vs_iphdr iph;
187 struct ip_vs_dest *dest;
188 struct ip_vs_conn *ct;
189 __be16 dport; /* destination port to forward */
190 union nf_inet_addr snet; /* source network of the client,
191 after masking */
193 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
195 /* Mask saddr with the netmask to adjust template granularity */
196 #ifdef CONFIG_IP_VS_IPV6
197 if (svc->af == AF_INET6)
198 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
199 else
200 #endif
201 snet.ip = iph.saddr.ip & svc->netmask;
203 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
204 "mnet %s\n",
205 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(ports[0]),
206 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(ports[1]),
207 IP_VS_DBG_ADDR(svc->af, &snet));
210 * As far as we know, FTP is a very complicated network protocol, and
211 * it uses control connection and data connections. For active FTP,
212 * FTP server initialize data connection to the client, its source port
213 * is often 20. For passive FTP, FTP server tells the clients the port
214 * that it passively listens to, and the client issues the data
215 * connection. In the tunneling or direct routing mode, the load
216 * balancer is on the client-to-server half of connection, the port
217 * number is unknown to the load balancer. So, a conn template like
218 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
219 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
220 * is created for other persistent services.
222 if (ports[1] == svc->port) {
223 /* Check if a template already exists */
224 if (svc->port != FTPPORT)
225 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
226 &iph.daddr, ports[1]);
227 else
228 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
229 &iph.daddr, 0);
231 if (!ct || !ip_vs_check_template(ct)) {
233 * No template found or the dest of the connection
234 * template is not available.
236 dest = svc->scheduler->schedule(svc, skb);
237 if (dest == NULL) {
238 IP_VS_DBG(1, "p-schedule: no dest found.\n");
239 return NULL;
243 * Create a template like <protocol,caddr,0,
244 * vaddr,vport,daddr,dport> for non-ftp service,
245 * and <protocol,caddr,0,vaddr,0,daddr,0>
246 * for ftp service.
248 if (svc->port != FTPPORT)
249 ct = ip_vs_conn_new(svc->af, iph.protocol,
250 &snet, 0,
251 &iph.daddr,
252 ports[1],
253 &dest->addr, dest->port,
254 IP_VS_CONN_F_TEMPLATE,
255 dest);
256 else
257 ct = ip_vs_conn_new(svc->af, iph.protocol,
258 &snet, 0,
259 &iph.daddr, 0,
260 &dest->addr, 0,
261 IP_VS_CONN_F_TEMPLATE,
262 dest);
263 if (ct == NULL)
264 return NULL;
266 ct->timeout = svc->timeout;
267 } else {
268 /* set destination with the found template */
269 dest = ct->dest;
271 dport = dest->port;
272 } else {
274 * Note: persistent fwmark-based services and persistent
275 * port zero service are handled here.
276 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
277 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
279 if (svc->fwmark) {
280 union nf_inet_addr fwmark = {
281 .all = { 0, 0, 0, htonl(svc->fwmark) }
284 ct = ip_vs_ct_in_get(svc->af, IPPROTO_IP, &snet, 0,
285 &fwmark, 0);
286 } else
287 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
288 &iph.daddr, 0);
290 if (!ct || !ip_vs_check_template(ct)) {
292 * If it is not persistent port zero, return NULL,
293 * otherwise create a connection template.
295 if (svc->port)
296 return NULL;
298 dest = svc->scheduler->schedule(svc, skb);
299 if (dest == NULL) {
300 IP_VS_DBG(1, "p-schedule: no dest found.\n");
301 return NULL;
305 * Create a template according to the service
307 if (svc->fwmark) {
308 union nf_inet_addr fwmark = {
309 .all = { 0, 0, 0, htonl(svc->fwmark) }
312 ct = ip_vs_conn_new(svc->af, IPPROTO_IP,
313 &snet, 0,
314 &fwmark, 0,
315 &dest->addr, 0,
316 IP_VS_CONN_F_TEMPLATE,
317 dest);
318 } else
319 ct = ip_vs_conn_new(svc->af, iph.protocol,
320 &snet, 0,
321 &iph.daddr, 0,
322 &dest->addr, 0,
323 IP_VS_CONN_F_TEMPLATE,
324 dest);
325 if (ct == NULL)
326 return NULL;
328 ct->timeout = svc->timeout;
329 } else {
330 /* set destination with the found template */
331 dest = ct->dest;
333 dport = ports[1];
337 * Create a new connection according to the template
339 cp = ip_vs_conn_new(svc->af, iph.protocol,
340 &iph.saddr, ports[0],
341 &iph.daddr, ports[1],
342 &dest->addr, dport,
344 dest);
345 if (cp == NULL) {
346 ip_vs_conn_put(ct);
347 return NULL;
351 * Add its control
353 ip_vs_control_add(cp, ct);
354 ip_vs_conn_put(ct);
356 ip_vs_conn_stats(cp, svc);
357 return cp;
362 * IPVS main scheduling function
363 * It selects a server according to the virtual service, and
364 * creates a connection entry.
365 * Protocols supported: TCP, UDP
367 struct ip_vs_conn *
368 ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
370 struct ip_vs_conn *cp = NULL;
371 struct ip_vs_iphdr iph;
372 struct ip_vs_dest *dest;
373 __be16 _ports[2], *pptr;
375 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
376 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
377 if (pptr == NULL)
378 return NULL;
381 * Persistent service
383 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
384 return ip_vs_sched_persist(svc, skb, pptr);
387 * Non-persistent service
389 if (!svc->fwmark && pptr[1] != svc->port) {
390 if (!svc->port)
391 IP_VS_ERR("Schedule: port zero only supported "
392 "in persistent services, "
393 "check your ipvs configuration\n");
394 return NULL;
397 dest = svc->scheduler->schedule(svc, skb);
398 if (dest == NULL) {
399 IP_VS_DBG(1, "Schedule: no dest found.\n");
400 return NULL;
404 * Create a connection entry.
406 cp = ip_vs_conn_new(svc->af, iph.protocol,
407 &iph.saddr, pptr[0],
408 &iph.daddr, pptr[1],
409 &dest->addr, dest->port ? dest->port : pptr[1],
411 dest);
412 if (cp == NULL)
413 return NULL;
415 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
416 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
417 ip_vs_fwd_tag(cp),
418 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
419 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
420 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
421 cp->flags, atomic_read(&cp->refcnt));
423 ip_vs_conn_stats(cp, svc);
424 return cp;
429 * Pass or drop the packet.
430 * Called by ip_vs_in, when the virtual service is available but
431 * no destination is available for a new connection.
433 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
434 struct ip_vs_protocol *pp)
436 __be16 _ports[2], *pptr;
437 struct ip_vs_iphdr iph;
438 int unicast;
439 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
441 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
442 if (pptr == NULL) {
443 ip_vs_service_put(svc);
444 return NF_DROP;
447 #ifdef CONFIG_IP_VS_IPV6
448 if (svc->af == AF_INET6)
449 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
450 else
451 #endif
452 unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
454 /* if it is fwmark-based service, the cache_bypass sysctl is up
455 and the destination is a non-local unicast, then create
456 a cache_bypass connection entry */
457 if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
458 int ret, cs;
459 struct ip_vs_conn *cp;
461 ip_vs_service_put(svc);
463 /* create a new connection entry */
464 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
465 cp = ip_vs_conn_new(svc->af, iph.protocol,
466 &iph.saddr, pptr[0],
467 &iph.daddr, pptr[1],
468 0, 0,
469 IP_VS_CONN_F_BYPASS,
470 NULL);
471 if (cp == NULL)
472 return NF_DROP;
474 /* statistics */
475 ip_vs_in_stats(cp, skb);
477 /* set state */
478 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
480 /* transmit the first SYN packet */
481 ret = cp->packet_xmit(skb, cp, pp);
482 /* do not touch skb anymore */
484 atomic_inc(&cp->in_pkts);
485 ip_vs_conn_put(cp);
486 return ret;
490 * When the virtual ftp service is presented, packets destined
491 * for other services on the VIP may get here (except services
492 * listed in the ipvs table), pass the packets, because it is
493 * not ipvs job to decide to drop the packets.
495 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
496 ip_vs_service_put(svc);
497 return NF_ACCEPT;
500 ip_vs_service_put(svc);
503 * Notify the client that the destination is unreachable, and
504 * release the socket buffer.
505 * Since it is in IP layer, the TCP socket is not actually
506 * created, the TCP RST packet cannot be sent, instead that
507 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
509 #ifdef CONFIG_IP_VS_IPV6
510 if (svc->af == AF_INET6)
511 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0,
512 skb->dev);
513 else
514 #endif
515 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
517 return NF_DROP;
522 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
523 * chain, and is used for VS/NAT.
524 * It detects packets for VS/NAT connections and sends the packets
525 * immediately. This can avoid that iptable_nat mangles the packets
526 * for VS/NAT.
528 static unsigned int ip_vs_post_routing(unsigned int hooknum,
529 struct sk_buff *skb,
530 const struct net_device *in,
531 const struct net_device *out,
532 int (*okfn)(struct sk_buff *))
534 if (!skb->ipvs_property)
535 return NF_ACCEPT;
536 /* The packet was sent from IPVS, exit this chain */
537 return NF_STOP;
540 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
542 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
545 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
547 int err = ip_defrag(skb, user);
549 if (!err)
550 ip_send_check(ip_hdr(skb));
552 return err;
555 #ifdef CONFIG_IP_VS_IPV6
556 static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
558 /* TODO IPv6: Find out what to do here for IPv6 */
559 return 0;
561 #endif
564 * Packet has been made sufficiently writable in caller
565 * - inout: 1=in->out, 0=out->in
567 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
568 struct ip_vs_conn *cp, int inout)
570 struct iphdr *iph = ip_hdr(skb);
571 unsigned int icmp_offset = iph->ihl*4;
572 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
573 icmp_offset);
574 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
576 if (inout) {
577 iph->saddr = cp->vaddr.ip;
578 ip_send_check(iph);
579 ciph->daddr = cp->vaddr.ip;
580 ip_send_check(ciph);
581 } else {
582 iph->daddr = cp->daddr.ip;
583 ip_send_check(iph);
584 ciph->saddr = cp->daddr.ip;
585 ip_send_check(ciph);
588 /* the TCP/UDP port */
589 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
590 __be16 *ports = (void *)ciph + ciph->ihl*4;
592 if (inout)
593 ports[1] = cp->vport;
594 else
595 ports[0] = cp->dport;
598 /* And finally the ICMP checksum */
599 icmph->checksum = 0;
600 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
601 skb->ip_summed = CHECKSUM_UNNECESSARY;
603 if (inout)
604 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
605 "Forwarding altered outgoing ICMP");
606 else
607 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
608 "Forwarding altered incoming ICMP");
611 #ifdef CONFIG_IP_VS_IPV6
612 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
613 struct ip_vs_conn *cp, int inout)
615 struct ipv6hdr *iph = ipv6_hdr(skb);
616 unsigned int icmp_offset = sizeof(struct ipv6hdr);
617 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
618 icmp_offset);
619 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
621 if (inout) {
622 iph->saddr = cp->vaddr.in6;
623 ciph->daddr = cp->vaddr.in6;
624 } else {
625 iph->daddr = cp->daddr.in6;
626 ciph->saddr = cp->daddr.in6;
629 /* the TCP/UDP port */
630 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr) {
631 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
633 if (inout)
634 ports[1] = cp->vport;
635 else
636 ports[0] = cp->dport;
639 /* And finally the ICMP checksum */
640 icmph->icmp6_cksum = 0;
641 /* TODO IPv6: is this correct for ICMPv6? */
642 ip_vs_checksum_complete(skb, icmp_offset);
643 skb->ip_summed = CHECKSUM_UNNECESSARY;
645 if (inout)
646 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
647 "Forwarding altered outgoing ICMPv6");
648 else
649 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
650 "Forwarding altered incoming ICMPv6");
652 #endif
655 * Handle ICMP messages in the inside-to-outside direction (outgoing).
656 * Find any that might be relevant, check against existing connections,
657 * forward to the right destination host if relevant.
658 * Currently handles error types - unreachable, quench, ttl exceeded.
659 * (Only used in VS/NAT)
661 static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
663 struct iphdr *iph;
664 struct icmphdr _icmph, *ic;
665 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
666 struct ip_vs_iphdr ciph;
667 struct ip_vs_conn *cp;
668 struct ip_vs_protocol *pp;
669 unsigned int offset, ihl, verdict;
671 *related = 1;
673 /* reassemble IP fragments */
674 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
675 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
676 return NF_STOLEN;
679 iph = ip_hdr(skb);
680 offset = ihl = iph->ihl * 4;
681 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
682 if (ic == NULL)
683 return NF_DROP;
685 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
686 ic->type, ntohs(icmp_id(ic)),
687 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
690 * Work through seeing if this is for us.
691 * These checks are supposed to be in an order that means easy
692 * things are checked first to speed up processing.... however
693 * this means that some packets will manage to get a long way
694 * down this stack and then be rejected, but that's life.
696 if ((ic->type != ICMP_DEST_UNREACH) &&
697 (ic->type != ICMP_SOURCE_QUENCH) &&
698 (ic->type != ICMP_TIME_EXCEEDED)) {
699 *related = 0;
700 return NF_ACCEPT;
703 /* Now find the contained IP header */
704 offset += sizeof(_icmph);
705 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
706 if (cih == NULL)
707 return NF_ACCEPT; /* The packet looks wrong, ignore */
709 pp = ip_vs_proto_get(cih->protocol);
710 if (!pp)
711 return NF_ACCEPT;
713 /* Is the embedded protocol header present? */
714 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
715 pp->dont_defrag))
716 return NF_ACCEPT;
718 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
720 offset += cih->ihl * 4;
722 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
723 /* The embedded headers contain source and dest in reverse order */
724 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
725 if (!cp)
726 return NF_ACCEPT;
728 verdict = NF_DROP;
730 if (IP_VS_FWD_METHOD(cp) != 0) {
731 IP_VS_ERR("shouldn't reach here, because the box is on the "
732 "half connection in the tun/dr module.\n");
735 /* Ensure the checksum is correct */
736 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
737 /* Failed checksum! */
738 IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
739 NIPQUAD(iph->saddr));
740 goto out;
743 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
744 offset += 2 * sizeof(__u16);
745 if (!skb_make_writable(skb, offset))
746 goto out;
748 ip_vs_nat_icmp(skb, pp, cp, 1);
750 /* do the statistics and put it back */
751 ip_vs_out_stats(cp, skb);
753 skb->ipvs_property = 1;
754 verdict = NF_ACCEPT;
756 out:
757 __ip_vs_conn_put(cp);
759 return verdict;
762 #ifdef CONFIG_IP_VS_IPV6
763 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
765 struct ipv6hdr *iph;
766 struct icmp6hdr _icmph, *ic;
767 struct ipv6hdr _ciph, *cih; /* The ip header contained
768 within the ICMP */
769 struct ip_vs_iphdr ciph;
770 struct ip_vs_conn *cp;
771 struct ip_vs_protocol *pp;
772 unsigned int offset, verdict;
774 *related = 1;
776 /* reassemble IP fragments */
777 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
778 if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
779 return NF_STOLEN;
782 iph = ipv6_hdr(skb);
783 offset = sizeof(struct ipv6hdr);
784 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
785 if (ic == NULL)
786 return NF_DROP;
788 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
789 ic->icmp6_type, ntohs(icmpv6_id(ic)),
790 NIP6(iph->saddr), NIP6(iph->daddr));
793 * Work through seeing if this is for us.
794 * These checks are supposed to be in an order that means easy
795 * things are checked first to speed up processing.... however
796 * this means that some packets will manage to get a long way
797 * down this stack and then be rejected, but that's life.
799 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
800 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
801 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
802 *related = 0;
803 return NF_ACCEPT;
806 /* Now find the contained IP header */
807 offset += sizeof(_icmph);
808 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
809 if (cih == NULL)
810 return NF_ACCEPT; /* The packet looks wrong, ignore */
812 pp = ip_vs_proto_get(cih->nexthdr);
813 if (!pp)
814 return NF_ACCEPT;
816 /* Is the embedded protocol header present? */
817 /* TODO: we don't support fragmentation at the moment anyways */
818 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
819 return NF_ACCEPT;
821 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
823 offset += sizeof(struct ipv6hdr);
825 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
826 /* The embedded headers contain source and dest in reverse order */
827 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
828 if (!cp)
829 return NF_ACCEPT;
831 verdict = NF_DROP;
833 if (IP_VS_FWD_METHOD(cp) != 0) {
834 IP_VS_ERR("shouldn't reach here, because the box is on the "
835 "half connection in the tun/dr module.\n");
838 /* Ensure the checksum is correct */
839 if (!skb_csum_unnecessary(skb)
840 && ip_vs_checksum_complete(skb, sizeof(struct ipv6hdr))) {
841 /* Failed checksum! */
842 IP_VS_DBG(1, "Forward ICMPv6: failed checksum from "
843 NIP6_FMT "!\n",
844 NIP6(iph->saddr));
845 goto out;
848 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
849 offset += 2 * sizeof(__u16);
850 if (!skb_make_writable(skb, offset))
851 goto out;
853 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
855 /* do the statistics and put it back */
856 ip_vs_out_stats(cp, skb);
858 skb->ipvs_property = 1;
859 verdict = NF_ACCEPT;
861 out:
862 __ip_vs_conn_put(cp);
864 return verdict;
866 #endif
868 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
870 struct tcphdr _tcph, *th;
872 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
873 if (th == NULL)
874 return 0;
875 return th->rst;
879 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
880 * Check if outgoing packet belongs to the established ip_vs_conn,
881 * rewrite addresses of the packet and send it on its way...
883 static unsigned int
884 ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
885 const struct net_device *in, const struct net_device *out,
886 int (*okfn)(struct sk_buff *))
888 struct ip_vs_iphdr iph;
889 struct ip_vs_protocol *pp;
890 struct ip_vs_conn *cp;
891 int af;
893 EnterFunction(11);
895 af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
897 if (skb->ipvs_property)
898 return NF_ACCEPT;
900 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
901 #ifdef CONFIG_IP_VS_IPV6
902 if (af == AF_INET6) {
903 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
904 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
906 if (related)
907 return verdict;
908 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
910 } else
911 #endif
912 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
913 int related, verdict = ip_vs_out_icmp(skb, &related);
915 if (related)
916 return verdict;
917 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
920 pp = ip_vs_proto_get(iph.protocol);
921 if (unlikely(!pp))
922 return NF_ACCEPT;
924 /* reassemble IP fragments */
925 #ifdef CONFIG_IP_VS_IPV6
926 if (af == AF_INET6) {
927 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
928 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
930 if (related)
931 return verdict;
933 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
935 } else
936 #endif
937 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
938 !pp->dont_defrag)) {
939 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
940 return NF_STOLEN;
942 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
946 * Check if the packet belongs to an existing entry
948 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
950 if (unlikely(!cp)) {
951 if (sysctl_ip_vs_nat_icmp_send &&
952 (pp->protocol == IPPROTO_TCP ||
953 pp->protocol == IPPROTO_UDP)) {
954 __be16 _ports[2], *pptr;
956 pptr = skb_header_pointer(skb, iph.len,
957 sizeof(_ports), _ports);
958 if (pptr == NULL)
959 return NF_ACCEPT; /* Not for me */
960 if (ip_vs_lookup_real_service(af, iph.protocol,
961 &iph.saddr,
962 pptr[0])) {
964 * Notify the real server: there is no
965 * existing entry if it is not RST
966 * packet or not TCP packet.
968 if (iph.protocol != IPPROTO_TCP
969 || !is_tcp_reset(skb, iph.len)) {
970 #ifdef CONFIG_IP_VS_IPV6
971 if (af == AF_INET6)
972 icmpv6_send(skb,
973 ICMPV6_DEST_UNREACH,
974 ICMPV6_PORT_UNREACH,
975 0, skb->dev);
976 else
977 #endif
978 icmp_send(skb,
979 ICMP_DEST_UNREACH,
980 ICMP_PORT_UNREACH, 0);
981 return NF_DROP;
985 IP_VS_DBG_PKT(12, pp, skb, 0,
986 "packet continues traversal as normal");
987 return NF_ACCEPT;
990 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
992 if (!skb_make_writable(skb, iph.len))
993 goto drop;
995 /* mangle the packet */
996 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
997 goto drop;
999 #ifdef CONFIG_IP_VS_IPV6
1000 if (af == AF_INET6)
1001 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1002 else
1003 #endif
1005 ip_hdr(skb)->saddr = cp->vaddr.ip;
1006 ip_send_check(ip_hdr(skb));
1009 /* For policy routing, packets originating from this
1010 * machine itself may be routed differently to packets
1011 * passing through. We want this packet to be routed as
1012 * if it came from this machine itself. So re-compute
1013 * the routing information.
1015 #ifdef CONFIG_IP_VS_IPV6
1016 if (af == AF_INET6) {
1017 if (ip6_route_me_harder(skb) != 0)
1018 goto drop;
1019 } else
1020 #endif
1021 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
1022 goto drop;
1024 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
1026 ip_vs_out_stats(cp, skb);
1027 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
1028 ip_vs_conn_put(cp);
1030 skb->ipvs_property = 1;
1032 LeaveFunction(11);
1033 return NF_ACCEPT;
1035 drop:
1036 ip_vs_conn_put(cp);
1037 kfree_skb(skb);
1038 return NF_STOLEN;
1043 * Handle ICMP messages in the outside-to-inside direction (incoming).
1044 * Find any that might be relevant, check against existing connections,
1045 * forward to the right destination host if relevant.
1046 * Currently handles error types - unreachable, quench, ttl exceeded.
1048 static int
1049 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1051 struct iphdr *iph;
1052 struct icmphdr _icmph, *ic;
1053 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
1054 struct ip_vs_iphdr ciph;
1055 struct ip_vs_conn *cp;
1056 struct ip_vs_protocol *pp;
1057 unsigned int offset, ihl, verdict;
1059 *related = 1;
1061 /* reassemble IP fragments */
1062 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
1063 if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
1064 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
1065 return NF_STOLEN;
1068 iph = ip_hdr(skb);
1069 offset = ihl = iph->ihl * 4;
1070 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1071 if (ic == NULL)
1072 return NF_DROP;
1074 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
1075 ic->type, ntohs(icmp_id(ic)),
1076 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1079 * Work through seeing if this is for us.
1080 * These checks are supposed to be in an order that means easy
1081 * things are checked first to speed up processing.... however
1082 * this means that some packets will manage to get a long way
1083 * down this stack and then be rejected, but that's life.
1085 if ((ic->type != ICMP_DEST_UNREACH) &&
1086 (ic->type != ICMP_SOURCE_QUENCH) &&
1087 (ic->type != ICMP_TIME_EXCEEDED)) {
1088 *related = 0;
1089 return NF_ACCEPT;
1092 /* Now find the contained IP header */
1093 offset += sizeof(_icmph);
1094 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1095 if (cih == NULL)
1096 return NF_ACCEPT; /* The packet looks wrong, ignore */
1098 pp = ip_vs_proto_get(cih->protocol);
1099 if (!pp)
1100 return NF_ACCEPT;
1102 /* Is the embedded protocol header present? */
1103 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1104 pp->dont_defrag))
1105 return NF_ACCEPT;
1107 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
1109 offset += cih->ihl * 4;
1111 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1112 /* The embedded headers contain source and dest in reverse order */
1113 cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
1114 if (!cp)
1115 return NF_ACCEPT;
1117 verdict = NF_DROP;
1119 /* Ensure the checksum is correct */
1120 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1121 /* Failed checksum! */
1122 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
1123 NIPQUAD(iph->saddr));
1124 goto out;
1127 /* do the statistics and put it back */
1128 ip_vs_in_stats(cp, skb);
1129 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1130 offset += 2 * sizeof(__u16);
1131 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1132 /* do not touch skb anymore */
1134 out:
1135 __ip_vs_conn_put(cp);
1137 return verdict;
1140 #ifdef CONFIG_IP_VS_IPV6
1141 static int
1142 ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1144 struct ipv6hdr *iph;
1145 struct icmp6hdr _icmph, *ic;
1146 struct ipv6hdr _ciph, *cih; /* The ip header contained
1147 within the ICMP */
1148 struct ip_vs_iphdr ciph;
1149 struct ip_vs_conn *cp;
1150 struct ip_vs_protocol *pp;
1151 unsigned int offset, verdict;
1153 *related = 1;
1155 /* reassemble IP fragments */
1156 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1157 if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
1158 IP_DEFRAG_VS_IN :
1159 IP_DEFRAG_VS_FWD))
1160 return NF_STOLEN;
1163 iph = ipv6_hdr(skb);
1164 offset = sizeof(struct ipv6hdr);
1165 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1166 if (ic == NULL)
1167 return NF_DROP;
1169 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
1170 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1171 NIP6(iph->saddr), NIP6(iph->daddr));
1174 * Work through seeing if this is for us.
1175 * These checks are supposed to be in an order that means easy
1176 * things are checked first to speed up processing.... however
1177 * this means that some packets will manage to get a long way
1178 * down this stack and then be rejected, but that's life.
1180 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1181 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1182 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1183 *related = 0;
1184 return NF_ACCEPT;
1187 /* Now find the contained IP header */
1188 offset += sizeof(_icmph);
1189 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1190 if (cih == NULL)
1191 return NF_ACCEPT; /* The packet looks wrong, ignore */
1193 pp = ip_vs_proto_get(cih->nexthdr);
1194 if (!pp)
1195 return NF_ACCEPT;
1197 /* Is the embedded protocol header present? */
1198 /* TODO: we don't support fragmentation at the moment anyways */
1199 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1200 return NF_ACCEPT;
1202 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
1204 offset += sizeof(struct ipv6hdr);
1206 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1207 /* The embedded headers contain source and dest in reverse order */
1208 cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
1209 if (!cp)
1210 return NF_ACCEPT;
1212 verdict = NF_DROP;
1214 /* do the statistics and put it back */
1215 ip_vs_in_stats(cp, skb);
1216 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
1217 offset += 2 * sizeof(__u16);
1218 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1219 /* do not touch skb anymore */
1221 __ip_vs_conn_put(cp);
1223 return verdict;
1225 #endif
1229 * Check if it's for virtual services, look it up,
1230 * and send it on its way...
1232 static unsigned int
1233 ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
1234 const struct net_device *in, const struct net_device *out,
1235 int (*okfn)(struct sk_buff *))
1237 struct ip_vs_iphdr iph;
1238 struct ip_vs_protocol *pp;
1239 struct ip_vs_conn *cp;
1240 int ret, restart, af;
1242 af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
1244 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1247 * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
1248 * ... don't know why 1st test DOES NOT include 2nd (?)
1250 if (unlikely(skb->pkt_type != PACKET_HOST
1251 || skb->dev->flags & IFF_LOOPBACK || skb->sk)) {
1252 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
1253 skb->pkt_type,
1254 iph.protocol,
1255 IP_VS_DBG_ADDR(af, &iph.daddr));
1256 return NF_ACCEPT;
1259 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1260 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
1262 if (related)
1263 return verdict;
1264 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1267 /* Protocol supported? */
1268 pp = ip_vs_proto_get(iph.protocol);
1269 if (unlikely(!pp))
1270 return NF_ACCEPT;
1273 * Check if the packet belongs to an existing connection entry
1275 cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
1277 if (unlikely(!cp)) {
1278 int v;
1280 if (!pp->conn_schedule(af, skb, pp, &v, &cp))
1281 return v;
1284 if (unlikely(!cp)) {
1285 /* sorry, all this trouble for a no-hit :) */
1286 IP_VS_DBG_PKT(12, pp, skb, 0,
1287 "packet continues traversal as normal");
1288 return NF_ACCEPT;
1291 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1293 /* Check the server status */
1294 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1295 /* the destination server is not available */
1297 if (sysctl_ip_vs_expire_nodest_conn) {
1298 /* try to expire the connection immediately */
1299 ip_vs_conn_expire_now(cp);
1301 /* don't restart its timer, and silently
1302 drop the packet. */
1303 __ip_vs_conn_put(cp);
1304 return NF_DROP;
1307 ip_vs_in_stats(cp, skb);
1308 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1309 if (cp->packet_xmit)
1310 ret = cp->packet_xmit(skb, cp, pp);
1311 /* do not touch skb anymore */
1312 else {
1313 IP_VS_DBG_RL("warning: packet_xmit is null");
1314 ret = NF_ACCEPT;
1317 /* Increase its packet counter and check if it is needed
1318 * to be synchronized
1320 * Sync connection if it is about to close to
1321 * encorage the standby servers to update the connections timeout
1323 atomic_inc(&cp->in_pkts);
1324 if (af == AF_INET &&
1325 (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
1326 (((cp->protocol != IPPROTO_TCP ||
1327 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1328 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
1329 == sysctl_ip_vs_sync_threshold[0])) ||
1330 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1331 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
1332 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1333 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1334 ip_vs_sync_conn(cp);
1335 cp->old_state = cp->state;
1337 ip_vs_conn_put(cp);
1338 return ret;
1343 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1344 * related packets destined for 0.0.0.0/0.
1345 * When fwmark-based virtual service is used, such as transparent
1346 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1347 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1348 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1349 * and send them to ip_vs_in_icmp.
1351 static unsigned int
1352 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1353 const struct net_device *in, const struct net_device *out,
1354 int (*okfn)(struct sk_buff *))
1356 int r;
1358 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1359 return NF_ACCEPT;
1361 return ip_vs_in_icmp(skb, &r, hooknum);
1364 #ifdef CONFIG_IP_VS_IPV6
1365 static unsigned int
1366 ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1367 const struct net_device *in, const struct net_device *out,
1368 int (*okfn)(struct sk_buff *))
1370 int r;
1372 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1373 return NF_ACCEPT;
1375 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1377 #endif
1380 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1381 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1382 * or VS/NAT(change destination), so that filtering rules can be
1383 * applied to IPVS. */
1385 .hook = ip_vs_in,
1386 .owner = THIS_MODULE,
1387 .pf = PF_INET,
1388 .hooknum = NF_INET_LOCAL_IN,
1389 .priority = 100,
1391 /* After packet filtering, change source only for VS/NAT */
1393 .hook = ip_vs_out,
1394 .owner = THIS_MODULE,
1395 .pf = PF_INET,
1396 .hooknum = NF_INET_FORWARD,
1397 .priority = 100,
1399 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1400 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1402 .hook = ip_vs_forward_icmp,
1403 .owner = THIS_MODULE,
1404 .pf = PF_INET,
1405 .hooknum = NF_INET_FORWARD,
1406 .priority = 99,
1408 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1410 .hook = ip_vs_post_routing,
1411 .owner = THIS_MODULE,
1412 .pf = PF_INET,
1413 .hooknum = NF_INET_POST_ROUTING,
1414 .priority = NF_IP_PRI_NAT_SRC-1,
1416 #ifdef CONFIG_IP_VS_IPV6
1417 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1418 * or VS/NAT(change destination), so that filtering rules can be
1419 * applied to IPVS. */
1421 .hook = ip_vs_in,
1422 .owner = THIS_MODULE,
1423 .pf = PF_INET6,
1424 .hooknum = NF_INET_LOCAL_IN,
1425 .priority = 100,
1427 /* After packet filtering, change source only for VS/NAT */
1429 .hook = ip_vs_out,
1430 .owner = THIS_MODULE,
1431 .pf = PF_INET6,
1432 .hooknum = NF_INET_FORWARD,
1433 .priority = 100,
1435 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1436 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1438 .hook = ip_vs_forward_icmp_v6,
1439 .owner = THIS_MODULE,
1440 .pf = PF_INET6,
1441 .hooknum = NF_INET_FORWARD,
1442 .priority = 99,
1444 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1446 .hook = ip_vs_post_routing,
1447 .owner = THIS_MODULE,
1448 .pf = PF_INET6,
1449 .hooknum = NF_INET_POST_ROUTING,
1450 .priority = NF_IP6_PRI_NAT_SRC-1,
1452 #endif
1457 * Initialize IP Virtual Server
1459 static int __init ip_vs_init(void)
1461 int ret;
1463 ip_vs_estimator_init();
1465 ret = ip_vs_control_init();
1466 if (ret < 0) {
1467 IP_VS_ERR("can't setup control.\n");
1468 goto cleanup_estimator;
1471 ip_vs_protocol_init();
1473 ret = ip_vs_app_init();
1474 if (ret < 0) {
1475 IP_VS_ERR("can't setup application helper.\n");
1476 goto cleanup_protocol;
1479 ret = ip_vs_conn_init();
1480 if (ret < 0) {
1481 IP_VS_ERR("can't setup connection table.\n");
1482 goto cleanup_app;
1485 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1486 if (ret < 0) {
1487 IP_VS_ERR("can't register hooks.\n");
1488 goto cleanup_conn;
1491 IP_VS_INFO("ipvs loaded.\n");
1492 return ret;
1494 cleanup_conn:
1495 ip_vs_conn_cleanup();
1496 cleanup_app:
1497 ip_vs_app_cleanup();
1498 cleanup_protocol:
1499 ip_vs_protocol_cleanup();
1500 ip_vs_control_cleanup();
1501 cleanup_estimator:
1502 ip_vs_estimator_cleanup();
1503 return ret;
1506 static void __exit ip_vs_cleanup(void)
1508 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1509 ip_vs_conn_cleanup();
1510 ip_vs_app_cleanup();
1511 ip_vs_protocol_cleanup();
1512 ip_vs_control_cleanup();
1513 ip_vs_estimator_cleanup();
1514 IP_VS_INFO("ipvs unloaded.\n");
1517 module_init(ip_vs_init);
1518 module_exit(ip_vs_cleanup);
1519 MODULE_LICENSE("GPL");