sh: remove PTRACE_O_TRACESYSGOOD from asm/ptrace.h
[linux-2.6/mini2440.git] / net / ipv4 / ipvs / ip_vs_core.c
blobc6ed7654e839c652ccce23a4064b104b85435eeb
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 * 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
21 * and others.
23 * Changes:
24 * Paul `Rusty' Russell properly handle non-linear skbs
25 * Harald Welte don't use nfcache
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/ip.h>
32 #include <linux/tcp.h>
33 #include <linux/icmp.h>
35 #include <net/ip.h>
36 #include <net/tcp.h>
37 #include <net/udp.h>
38 #include <net/icmp.h> /* for icmp_send */
39 #include <net/route.h>
41 #include <linux/netfilter.h>
42 #include <linux/netfilter_ipv4.h>
44 #include <net/ip_vs.h>
47 EXPORT_SYMBOL(register_ip_vs_scheduler);
48 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
49 EXPORT_SYMBOL(ip_vs_skb_replace);
50 EXPORT_SYMBOL(ip_vs_proto_name);
51 EXPORT_SYMBOL(ip_vs_conn_new);
52 EXPORT_SYMBOL(ip_vs_conn_in_get);
53 EXPORT_SYMBOL(ip_vs_conn_out_get);
54 #ifdef CONFIG_IP_VS_PROTO_TCP
55 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
56 #endif
57 EXPORT_SYMBOL(ip_vs_conn_put);
58 #ifdef CONFIG_IP_VS_DEBUG
59 EXPORT_SYMBOL(ip_vs_get_debug_level);
60 #endif
63 /* ID used in ICMP lookups */
64 #define icmp_id(icmph) (((icmph)->un).echo.id)
66 const char *ip_vs_proto_name(unsigned proto)
68 static char buf[20];
70 switch (proto) {
71 case IPPROTO_IP:
72 return "IP";
73 case IPPROTO_UDP:
74 return "UDP";
75 case IPPROTO_TCP:
76 return "TCP";
77 case IPPROTO_ICMP:
78 return "ICMP";
79 default:
80 sprintf(buf, "IP_%d", proto);
81 return buf;
85 void ip_vs_init_hash_table(struct list_head *table, int rows)
87 while (--rows >= 0)
88 INIT_LIST_HEAD(&table[rows]);
91 static inline void
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);
97 dest->stats.inpkts++;
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);
114 static inline void
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);
137 static inline void
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);
145 svc->stats.conns++;
146 spin_unlock(&svc->stats.lock);
148 spin_lock(&ip_vs_stats.lock);
149 ip_vs_stats.conns++;
150 spin_unlock(&ip_vs_stats.lock);
154 static inline int
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))
160 return 0;
161 return pp->state_transition(cp, direction, skb, pp);
166 * IPVS persistent scheduling function
167 * It creates a connection entry according to its template if exists,
168 * or selects a server and creates a connection entry plus a template.
169 * Locking: we are svc user (svc->refcnt), so we hold all dests too
170 * Protocols supported: TCP, UDP
172 static struct ip_vs_conn *
173 ip_vs_sched_persist(struct ip_vs_service *svc,
174 const struct sk_buff *skb,
175 __be16 ports[2])
177 struct ip_vs_conn *cp = NULL;
178 struct iphdr *iph = ip_hdr(skb);
179 struct ip_vs_dest *dest;
180 struct ip_vs_conn *ct;
181 __be16 dport; /* destination port to forward */
182 __be32 snet; /* source network of the client, after masking */
184 /* Mask saddr with the netmask to adjust template granularity */
185 snet = iph->saddr & svc->netmask;
187 IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u "
188 "mnet %u.%u.%u.%u\n",
189 NIPQUAD(iph->saddr), ntohs(ports[0]),
190 NIPQUAD(iph->daddr), ntohs(ports[1]),
191 NIPQUAD(snet));
194 * As far as we know, FTP is a very complicated network protocol, and
195 * it uses control connection and data connections. For active FTP,
196 * FTP server initialize data connection to the client, its source port
197 * is often 20. For passive FTP, FTP server tells the clients the port
198 * that it passively listens to, and the client issues the data
199 * connection. In the tunneling or direct routing mode, the load
200 * balancer is on the client-to-server half of connection, the port
201 * number is unknown to the load balancer. So, a conn template like
202 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
203 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
204 * is created for other persistent services.
206 if (ports[1] == svc->port) {
207 /* Check if a template already exists */
208 if (svc->port != FTPPORT)
209 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
210 iph->daddr, ports[1]);
211 else
212 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
213 iph->daddr, 0);
215 if (!ct || !ip_vs_check_template(ct)) {
217 * No template found or the dest of the connection
218 * template is not available.
220 dest = svc->scheduler->schedule(svc, skb);
221 if (dest == NULL) {
222 IP_VS_DBG(1, "p-schedule: no dest found.\n");
223 return NULL;
227 * Create a template like <protocol,caddr,0,
228 * vaddr,vport,daddr,dport> for non-ftp service,
229 * and <protocol,caddr,0,vaddr,0,daddr,0>
230 * for ftp service.
232 if (svc->port != FTPPORT)
233 ct = ip_vs_conn_new(iph->protocol,
234 snet, 0,
235 iph->daddr,
236 ports[1],
237 dest->addr, dest->port,
238 IP_VS_CONN_F_TEMPLATE,
239 dest);
240 else
241 ct = ip_vs_conn_new(iph->protocol,
242 snet, 0,
243 iph->daddr, 0,
244 dest->addr, 0,
245 IP_VS_CONN_F_TEMPLATE,
246 dest);
247 if (ct == NULL)
248 return NULL;
250 ct->timeout = svc->timeout;
251 } else {
252 /* set destination with the found template */
253 dest = ct->dest;
255 dport = dest->port;
256 } else {
258 * Note: persistent fwmark-based services and persistent
259 * port zero service are handled here.
260 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
261 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
263 if (svc->fwmark)
264 ct = ip_vs_ct_in_get(IPPROTO_IP, snet, 0,
265 htonl(svc->fwmark), 0);
266 else
267 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
268 iph->daddr, 0);
270 if (!ct || !ip_vs_check_template(ct)) {
272 * If it is not persistent port zero, return NULL,
273 * otherwise create a connection template.
275 if (svc->port)
276 return NULL;
278 dest = svc->scheduler->schedule(svc, skb);
279 if (dest == NULL) {
280 IP_VS_DBG(1, "p-schedule: no dest found.\n");
281 return NULL;
285 * Create a template according to the service
287 if (svc->fwmark)
288 ct = ip_vs_conn_new(IPPROTO_IP,
289 snet, 0,
290 htonl(svc->fwmark), 0,
291 dest->addr, 0,
292 IP_VS_CONN_F_TEMPLATE,
293 dest);
294 else
295 ct = ip_vs_conn_new(iph->protocol,
296 snet, 0,
297 iph->daddr, 0,
298 dest->addr, 0,
299 IP_VS_CONN_F_TEMPLATE,
300 dest);
301 if (ct == NULL)
302 return NULL;
304 ct->timeout = svc->timeout;
305 } else {
306 /* set destination with the found template */
307 dest = ct->dest;
309 dport = ports[1];
313 * Create a new connection according to the template
315 cp = ip_vs_conn_new(iph->protocol,
316 iph->saddr, ports[0],
317 iph->daddr, ports[1],
318 dest->addr, dport,
320 dest);
321 if (cp == NULL) {
322 ip_vs_conn_put(ct);
323 return NULL;
327 * Add its control
329 ip_vs_control_add(cp, ct);
330 ip_vs_conn_put(ct);
332 ip_vs_conn_stats(cp, svc);
333 return cp;
338 * IPVS main scheduling function
339 * It selects a server according to the virtual service, and
340 * creates a connection entry.
341 * Protocols supported: TCP, UDP
343 struct ip_vs_conn *
344 ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
346 struct ip_vs_conn *cp = NULL;
347 struct iphdr *iph = ip_hdr(skb);
348 struct ip_vs_dest *dest;
349 __be16 _ports[2], *pptr;
351 pptr = skb_header_pointer(skb, iph->ihl*4,
352 sizeof(_ports), _ports);
353 if (pptr == NULL)
354 return NULL;
357 * Persistent service
359 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
360 return ip_vs_sched_persist(svc, skb, pptr);
363 * Non-persistent service
365 if (!svc->fwmark && pptr[1] != svc->port) {
366 if (!svc->port)
367 IP_VS_ERR("Schedule: port zero only supported "
368 "in persistent services, "
369 "check your ipvs configuration\n");
370 return NULL;
373 dest = svc->scheduler->schedule(svc, skb);
374 if (dest == NULL) {
375 IP_VS_DBG(1, "Schedule: no dest found.\n");
376 return NULL;
380 * Create a connection entry.
382 cp = ip_vs_conn_new(iph->protocol,
383 iph->saddr, pptr[0],
384 iph->daddr, pptr[1],
385 dest->addr, dest->port?dest->port:pptr[1],
387 dest);
388 if (cp == NULL)
389 return NULL;
391 IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u "
392 "d:%u.%u.%u.%u:%u conn->flags:%X conn->refcnt:%d\n",
393 ip_vs_fwd_tag(cp),
394 NIPQUAD(cp->caddr), ntohs(cp->cport),
395 NIPQUAD(cp->vaddr), ntohs(cp->vport),
396 NIPQUAD(cp->daddr), ntohs(cp->dport),
397 cp->flags, atomic_read(&cp->refcnt));
399 ip_vs_conn_stats(cp, svc);
400 return cp;
405 * Pass or drop the packet.
406 * Called by ip_vs_in, when the virtual service is available but
407 * no destination is available for a new connection.
409 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
410 struct ip_vs_protocol *pp)
412 __be16 _ports[2], *pptr;
413 struct iphdr *iph = ip_hdr(skb);
415 pptr = skb_header_pointer(skb, iph->ihl*4,
416 sizeof(_ports), _ports);
417 if (pptr == NULL) {
418 ip_vs_service_put(svc);
419 return NF_DROP;
422 /* if it is fwmark-based service, the cache_bypass sysctl is up
423 and the destination is RTN_UNICAST (and not local), then create
424 a cache_bypass connection entry */
425 if (sysctl_ip_vs_cache_bypass && svc->fwmark
426 && (inet_addr_type(iph->daddr) == RTN_UNICAST)) {
427 int ret, cs;
428 struct ip_vs_conn *cp;
430 ip_vs_service_put(svc);
432 /* create a new connection entry */
433 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
434 cp = ip_vs_conn_new(iph->protocol,
435 iph->saddr, pptr[0],
436 iph->daddr, pptr[1],
437 0, 0,
438 IP_VS_CONN_F_BYPASS,
439 NULL);
440 if (cp == NULL)
441 return NF_DROP;
443 /* statistics */
444 ip_vs_in_stats(cp, skb);
446 /* set state */
447 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
449 /* transmit the first SYN packet */
450 ret = cp->packet_xmit(skb, cp, pp);
451 /* do not touch skb anymore */
453 atomic_inc(&cp->in_pkts);
454 ip_vs_conn_put(cp);
455 return ret;
459 * When the virtual ftp service is presented, packets destined
460 * for other services on the VIP may get here (except services
461 * listed in the ipvs table), pass the packets, because it is
462 * not ipvs job to decide to drop the packets.
464 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
465 ip_vs_service_put(svc);
466 return NF_ACCEPT;
469 ip_vs_service_put(svc);
472 * Notify the client that the destination is unreachable, and
473 * release the socket buffer.
474 * Since it is in IP layer, the TCP socket is not actually
475 * created, the TCP RST packet cannot be sent, instead that
476 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
478 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
479 return NF_DROP;
484 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_IP_POST_ROUTING
485 * chain, and is used for VS/NAT.
486 * It detects packets for VS/NAT connections and sends the packets
487 * immediately. This can avoid that iptable_nat mangles the packets
488 * for VS/NAT.
490 static unsigned int ip_vs_post_routing(unsigned int hooknum,
491 struct sk_buff *skb,
492 const struct net_device *in,
493 const struct net_device *out,
494 int (*okfn)(struct sk_buff *))
496 if (!skb->ipvs_property)
497 return NF_ACCEPT;
498 /* The packet was sent from IPVS, exit this chain */
499 return NF_STOP;
502 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
504 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
507 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
509 int err = ip_defrag(skb, user);
511 if (!err)
512 ip_send_check(ip_hdr(skb));
514 return err;
518 * Packet has been made sufficiently writable in caller
519 * - inout: 1=in->out, 0=out->in
521 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
522 struct ip_vs_conn *cp, int inout)
524 struct iphdr *iph = ip_hdr(skb);
525 unsigned int icmp_offset = iph->ihl*4;
526 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
527 icmp_offset);
528 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
530 if (inout) {
531 iph->saddr = cp->vaddr;
532 ip_send_check(iph);
533 ciph->daddr = cp->vaddr;
534 ip_send_check(ciph);
535 } else {
536 iph->daddr = cp->daddr;
537 ip_send_check(iph);
538 ciph->saddr = cp->daddr;
539 ip_send_check(ciph);
542 /* the TCP/UDP port */
543 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
544 __be16 *ports = (void *)ciph + ciph->ihl*4;
546 if (inout)
547 ports[1] = cp->vport;
548 else
549 ports[0] = cp->dport;
552 /* And finally the ICMP checksum */
553 icmph->checksum = 0;
554 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
555 skb->ip_summed = CHECKSUM_UNNECESSARY;
557 if (inout)
558 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
559 "Forwarding altered outgoing ICMP");
560 else
561 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
562 "Forwarding altered incoming ICMP");
566 * Handle ICMP messages in the inside-to-outside direction (outgoing).
567 * Find any that might be relevant, check against existing connections,
568 * forward to the right destination host if relevant.
569 * Currently handles error types - unreachable, quench, ttl exceeded.
570 * (Only used in VS/NAT)
572 static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
574 struct iphdr *iph;
575 struct icmphdr _icmph, *ic;
576 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
577 struct ip_vs_conn *cp;
578 struct ip_vs_protocol *pp;
579 unsigned int offset, ihl, verdict;
581 *related = 1;
583 /* reassemble IP fragments */
584 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
585 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
586 return NF_STOLEN;
589 iph = ip_hdr(skb);
590 offset = ihl = iph->ihl * 4;
591 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
592 if (ic == NULL)
593 return NF_DROP;
595 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
596 ic->type, ntohs(icmp_id(ic)),
597 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
600 * Work through seeing if this is for us.
601 * These checks are supposed to be in an order that means easy
602 * things are checked first to speed up processing.... however
603 * this means that some packets will manage to get a long way
604 * down this stack and then be rejected, but that's life.
606 if ((ic->type != ICMP_DEST_UNREACH) &&
607 (ic->type != ICMP_SOURCE_QUENCH) &&
608 (ic->type != ICMP_TIME_EXCEEDED)) {
609 *related = 0;
610 return NF_ACCEPT;
613 /* Now find the contained IP header */
614 offset += sizeof(_icmph);
615 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
616 if (cih == NULL)
617 return NF_ACCEPT; /* The packet looks wrong, ignore */
619 pp = ip_vs_proto_get(cih->protocol);
620 if (!pp)
621 return NF_ACCEPT;
623 /* Is the embedded protocol header present? */
624 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
625 pp->dont_defrag))
626 return NF_ACCEPT;
628 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
630 offset += cih->ihl * 4;
632 /* The embedded headers contain source and dest in reverse order */
633 cp = pp->conn_out_get(skb, pp, cih, offset, 1);
634 if (!cp)
635 return NF_ACCEPT;
637 verdict = NF_DROP;
639 if (IP_VS_FWD_METHOD(cp) != 0) {
640 IP_VS_ERR("shouldn't reach here, because the box is on the"
641 "half connection in the tun/dr module.\n");
644 /* Ensure the checksum is correct */
645 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
646 /* Failed checksum! */
647 IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
648 NIPQUAD(iph->saddr));
649 goto out;
652 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
653 offset += 2 * sizeof(__u16);
654 if (!skb_make_writable(skb, offset))
655 goto out;
657 ip_vs_nat_icmp(skb, pp, cp, 1);
659 /* do the statistics and put it back */
660 ip_vs_out_stats(cp, skb);
662 skb->ipvs_property = 1;
663 verdict = NF_ACCEPT;
665 out:
666 __ip_vs_conn_put(cp);
668 return verdict;
671 static inline int is_tcp_reset(const struct sk_buff *skb)
673 struct tcphdr _tcph, *th;
675 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
676 if (th == NULL)
677 return 0;
678 return th->rst;
682 * It is hooked at the NF_IP_FORWARD chain, used only for VS/NAT.
683 * Check if outgoing packet belongs to the established ip_vs_conn,
684 * rewrite addresses of the packet and send it on its way...
686 static unsigned int
687 ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
688 const struct net_device *in, const struct net_device *out,
689 int (*okfn)(struct sk_buff *))
691 struct iphdr *iph;
692 struct ip_vs_protocol *pp;
693 struct ip_vs_conn *cp;
694 int ihl;
696 EnterFunction(11);
698 if (skb->ipvs_property)
699 return NF_ACCEPT;
701 iph = ip_hdr(skb);
702 if (unlikely(iph->protocol == IPPROTO_ICMP)) {
703 int related, verdict = ip_vs_out_icmp(skb, &related);
705 if (related)
706 return verdict;
707 iph = ip_hdr(skb);
710 pp = ip_vs_proto_get(iph->protocol);
711 if (unlikely(!pp))
712 return NF_ACCEPT;
714 /* reassemble IP fragments */
715 if (unlikely(iph->frag_off & htons(IP_MF|IP_OFFSET) &&
716 !pp->dont_defrag)) {
717 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
718 return NF_STOLEN;
719 iph = ip_hdr(skb);
722 ihl = iph->ihl << 2;
725 * Check if the packet belongs to an existing entry
727 cp = pp->conn_out_get(skb, pp, iph, ihl, 0);
729 if (unlikely(!cp)) {
730 if (sysctl_ip_vs_nat_icmp_send &&
731 (pp->protocol == IPPROTO_TCP ||
732 pp->protocol == IPPROTO_UDP)) {
733 __be16 _ports[2], *pptr;
735 pptr = skb_header_pointer(skb, ihl,
736 sizeof(_ports), _ports);
737 if (pptr == NULL)
738 return NF_ACCEPT; /* Not for me */
739 if (ip_vs_lookup_real_service(iph->protocol,
740 iph->saddr, pptr[0])) {
742 * Notify the real server: there is no
743 * existing entry if it is not RST
744 * packet or not TCP packet.
746 if (iph->protocol != IPPROTO_TCP
747 || !is_tcp_reset(skb)) {
748 icmp_send(skb,ICMP_DEST_UNREACH,
749 ICMP_PORT_UNREACH, 0);
750 return NF_DROP;
754 IP_VS_DBG_PKT(12, pp, skb, 0,
755 "packet continues traversal as normal");
756 return NF_ACCEPT;
759 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
761 if (!skb_make_writable(skb, ihl))
762 goto drop;
764 /* mangle the packet */
765 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
766 goto drop;
767 ip_hdr(skb)->saddr = cp->vaddr;
768 ip_send_check(ip_hdr(skb));
770 /* For policy routing, packets originating from this
771 * machine itself may be routed differently to packets
772 * passing through. We want this packet to be routed as
773 * if it came from this machine itself. So re-compute
774 * the routing information.
776 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
777 goto drop;
779 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
781 ip_vs_out_stats(cp, skb);
782 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
783 ip_vs_conn_put(cp);
785 skb->ipvs_property = 1;
787 LeaveFunction(11);
788 return NF_ACCEPT;
790 drop:
791 ip_vs_conn_put(cp);
792 kfree_skb(skb);
793 return NF_STOLEN;
798 * Handle ICMP messages in the outside-to-inside direction (incoming).
799 * Find any that might be relevant, check against existing connections,
800 * forward to the right destination host if relevant.
801 * Currently handles error types - unreachable, quench, ttl exceeded.
803 static int
804 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
806 struct iphdr *iph;
807 struct icmphdr _icmph, *ic;
808 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
809 struct ip_vs_conn *cp;
810 struct ip_vs_protocol *pp;
811 unsigned int offset, ihl, verdict;
813 *related = 1;
815 /* reassemble IP fragments */
816 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
817 if (ip_vs_gather_frags(skb, hooknum == NF_IP_LOCAL_IN ?
818 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
819 return NF_STOLEN;
822 iph = ip_hdr(skb);
823 offset = ihl = iph->ihl * 4;
824 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
825 if (ic == NULL)
826 return NF_DROP;
828 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
829 ic->type, ntohs(icmp_id(ic)),
830 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
833 * Work through seeing if this is for us.
834 * These checks are supposed to be in an order that means easy
835 * things are checked first to speed up processing.... however
836 * this means that some packets will manage to get a long way
837 * down this stack and then be rejected, but that's life.
839 if ((ic->type != ICMP_DEST_UNREACH) &&
840 (ic->type != ICMP_SOURCE_QUENCH) &&
841 (ic->type != ICMP_TIME_EXCEEDED)) {
842 *related = 0;
843 return NF_ACCEPT;
846 /* Now find the contained IP header */
847 offset += sizeof(_icmph);
848 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
849 if (cih == NULL)
850 return NF_ACCEPT; /* The packet looks wrong, ignore */
852 pp = ip_vs_proto_get(cih->protocol);
853 if (!pp)
854 return NF_ACCEPT;
856 /* Is the embedded protocol header present? */
857 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
858 pp->dont_defrag))
859 return NF_ACCEPT;
861 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
863 offset += cih->ihl * 4;
865 /* The embedded headers contain source and dest in reverse order */
866 cp = pp->conn_in_get(skb, pp, cih, offset, 1);
867 if (!cp)
868 return NF_ACCEPT;
870 verdict = NF_DROP;
872 /* Ensure the checksum is correct */
873 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
874 /* Failed checksum! */
875 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
876 NIPQUAD(iph->saddr));
877 goto out;
880 /* do the statistics and put it back */
881 ip_vs_in_stats(cp, skb);
882 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
883 offset += 2 * sizeof(__u16);
884 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
885 /* do not touch skb anymore */
887 out:
888 __ip_vs_conn_put(cp);
890 return verdict;
894 * Check if it's for virtual services, look it up,
895 * and send it on its way...
897 static unsigned int
898 ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
899 const struct net_device *in, const struct net_device *out,
900 int (*okfn)(struct sk_buff *))
902 struct iphdr *iph;
903 struct ip_vs_protocol *pp;
904 struct ip_vs_conn *cp;
905 int ret, restart;
906 int ihl;
909 * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
910 * ... don't know why 1st test DOES NOT include 2nd (?)
912 if (unlikely(skb->pkt_type != PACKET_HOST
913 || skb->dev->flags & IFF_LOOPBACK || skb->sk)) {
914 IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n",
915 skb->pkt_type,
916 ip_hdr(skb)->protocol,
917 NIPQUAD(ip_hdr(skb)->daddr));
918 return NF_ACCEPT;
921 iph = ip_hdr(skb);
922 if (unlikely(iph->protocol == IPPROTO_ICMP)) {
923 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
925 if (related)
926 return verdict;
927 iph = ip_hdr(skb);
930 /* Protocol supported? */
931 pp = ip_vs_proto_get(iph->protocol);
932 if (unlikely(!pp))
933 return NF_ACCEPT;
935 ihl = iph->ihl << 2;
938 * Check if the packet belongs to an existing connection entry
940 cp = pp->conn_in_get(skb, pp, iph, ihl, 0);
942 if (unlikely(!cp)) {
943 int v;
945 if (!pp->conn_schedule(skb, pp, &v, &cp))
946 return v;
949 if (unlikely(!cp)) {
950 /* sorry, all this trouble for a no-hit :) */
951 IP_VS_DBG_PKT(12, pp, skb, 0,
952 "packet continues traversal as normal");
953 return NF_ACCEPT;
956 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
958 /* Check the server status */
959 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
960 /* the destination server is not available */
962 if (sysctl_ip_vs_expire_nodest_conn) {
963 /* try to expire the connection immediately */
964 ip_vs_conn_expire_now(cp);
966 /* don't restart its timer, and silently
967 drop the packet. */
968 __ip_vs_conn_put(cp);
969 return NF_DROP;
972 ip_vs_in_stats(cp, skb);
973 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
974 if (cp->packet_xmit)
975 ret = cp->packet_xmit(skb, cp, pp);
976 /* do not touch skb anymore */
977 else {
978 IP_VS_DBG_RL("warning: packet_xmit is null");
979 ret = NF_ACCEPT;
982 /* increase its packet counter and check if it is needed
983 to be synchronized */
984 atomic_inc(&cp->in_pkts);
985 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) &&
986 (cp->protocol != IPPROTO_TCP ||
987 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
988 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
989 == sysctl_ip_vs_sync_threshold[0]))
990 ip_vs_sync_conn(cp);
992 ip_vs_conn_put(cp);
993 return ret;
998 * It is hooked at the NF_IP_FORWARD chain, in order to catch ICMP
999 * related packets destined for 0.0.0.0/0.
1000 * When fwmark-based virtual service is used, such as transparent
1001 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1002 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1003 * sent to ip_vs_in_icmp. So, catch them at the NF_IP_FORWARD chain
1004 * and send them to ip_vs_in_icmp.
1006 static unsigned int
1007 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1008 const struct net_device *in, const struct net_device *out,
1009 int (*okfn)(struct sk_buff *))
1011 int r;
1013 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1014 return NF_ACCEPT;
1016 return ip_vs_in_icmp(skb, &r, hooknum);
1020 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1021 or VS/NAT(change destination), so that filtering rules can be
1022 applied to IPVS. */
1023 static struct nf_hook_ops ip_vs_in_ops = {
1024 .hook = ip_vs_in,
1025 .owner = THIS_MODULE,
1026 .pf = PF_INET,
1027 .hooknum = NF_IP_LOCAL_IN,
1028 .priority = 100,
1031 /* After packet filtering, change source only for VS/NAT */
1032 static struct nf_hook_ops ip_vs_out_ops = {
1033 .hook = ip_vs_out,
1034 .owner = THIS_MODULE,
1035 .pf = PF_INET,
1036 .hooknum = NF_IP_FORWARD,
1037 .priority = 100,
1040 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1041 destined for 0.0.0.0/0, which is for incoming IPVS connections */
1042 static struct nf_hook_ops ip_vs_forward_icmp_ops = {
1043 .hook = ip_vs_forward_icmp,
1044 .owner = THIS_MODULE,
1045 .pf = PF_INET,
1046 .hooknum = NF_IP_FORWARD,
1047 .priority = 99,
1050 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1051 static struct nf_hook_ops ip_vs_post_routing_ops = {
1052 .hook = ip_vs_post_routing,
1053 .owner = THIS_MODULE,
1054 .pf = PF_INET,
1055 .hooknum = NF_IP_POST_ROUTING,
1056 .priority = NF_IP_PRI_NAT_SRC-1,
1061 * Initialize IP Virtual Server
1063 static int __init ip_vs_init(void)
1065 int ret;
1067 ret = ip_vs_control_init();
1068 if (ret < 0) {
1069 IP_VS_ERR("can't setup control.\n");
1070 goto cleanup_nothing;
1073 ip_vs_protocol_init();
1075 ret = ip_vs_app_init();
1076 if (ret < 0) {
1077 IP_VS_ERR("can't setup application helper.\n");
1078 goto cleanup_protocol;
1081 ret = ip_vs_conn_init();
1082 if (ret < 0) {
1083 IP_VS_ERR("can't setup connection table.\n");
1084 goto cleanup_app;
1087 ret = nf_register_hook(&ip_vs_in_ops);
1088 if (ret < 0) {
1089 IP_VS_ERR("can't register in hook.\n");
1090 goto cleanup_conn;
1093 ret = nf_register_hook(&ip_vs_out_ops);
1094 if (ret < 0) {
1095 IP_VS_ERR("can't register out hook.\n");
1096 goto cleanup_inops;
1098 ret = nf_register_hook(&ip_vs_post_routing_ops);
1099 if (ret < 0) {
1100 IP_VS_ERR("can't register post_routing hook.\n");
1101 goto cleanup_outops;
1103 ret = nf_register_hook(&ip_vs_forward_icmp_ops);
1104 if (ret < 0) {
1105 IP_VS_ERR("can't register forward_icmp hook.\n");
1106 goto cleanup_postroutingops;
1109 IP_VS_INFO("ipvs loaded.\n");
1110 return ret;
1112 cleanup_postroutingops:
1113 nf_unregister_hook(&ip_vs_post_routing_ops);
1114 cleanup_outops:
1115 nf_unregister_hook(&ip_vs_out_ops);
1116 cleanup_inops:
1117 nf_unregister_hook(&ip_vs_in_ops);
1118 cleanup_conn:
1119 ip_vs_conn_cleanup();
1120 cleanup_app:
1121 ip_vs_app_cleanup();
1122 cleanup_protocol:
1123 ip_vs_protocol_cleanup();
1124 ip_vs_control_cleanup();
1125 cleanup_nothing:
1126 return ret;
1129 static void __exit ip_vs_cleanup(void)
1131 nf_unregister_hook(&ip_vs_forward_icmp_ops);
1132 nf_unregister_hook(&ip_vs_post_routing_ops);
1133 nf_unregister_hook(&ip_vs_out_ops);
1134 nf_unregister_hook(&ip_vs_in_ops);
1135 ip_vs_conn_cleanup();
1136 ip_vs_app_cleanup();
1137 ip_vs_protocol_cleanup();
1138 ip_vs_control_cleanup();
1139 IP_VS_INFO("ipvs unloaded.\n");
1142 module_init(ip_vs_init);
1143 module_exit(ip_vs_cleanup);
1144 MODULE_LICENSE("GPL");