RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv4 / ipvs / ip_vs_core.c
blob67c1d0ac8d1ad39dd68d99deca700631d82c0f4b
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
61 EXPORT_SYMBOL(ip_vs_make_skb_writable);
64 /* ID used in ICMP lookups */
65 #define icmp_id(icmph) (((icmph)->un).echo.id)
67 const char *ip_vs_proto_name(unsigned proto)
69 static char buf[20];
71 switch (proto) {
72 case IPPROTO_IP:
73 return "IP";
74 case IPPROTO_UDP:
75 return "UDP";
76 case IPPROTO_TCP:
77 return "TCP";
78 case IPPROTO_ICMP:
79 return "ICMP";
80 default:
81 sprintf(buf, "IP_%d", proto);
82 return buf;
86 void ip_vs_init_hash_table(struct list_head *table, int rows)
88 while (--rows >= 0)
89 INIT_LIST_HEAD(&table[rows]);
92 static inline void
93 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
95 struct ip_vs_dest *dest = cp->dest;
96 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
97 spin_lock(&dest->stats.lock);
98 dest->stats.inpkts++;
99 dest->stats.inbytes += skb->len;
100 spin_unlock(&dest->stats.lock);
102 spin_lock(&dest->svc->stats.lock);
103 dest->svc->stats.inpkts++;
104 dest->svc->stats.inbytes += skb->len;
105 spin_unlock(&dest->svc->stats.lock);
107 spin_lock(&ip_vs_stats.lock);
108 ip_vs_stats.inpkts++;
109 ip_vs_stats.inbytes += skb->len;
110 spin_unlock(&ip_vs_stats.lock);
115 static inline void
116 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
118 struct ip_vs_dest *dest = cp->dest;
119 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
120 spin_lock(&dest->stats.lock);
121 dest->stats.outpkts++;
122 dest->stats.outbytes += skb->len;
123 spin_unlock(&dest->stats.lock);
125 spin_lock(&dest->svc->stats.lock);
126 dest->svc->stats.outpkts++;
127 dest->svc->stats.outbytes += skb->len;
128 spin_unlock(&dest->svc->stats.lock);
130 spin_lock(&ip_vs_stats.lock);
131 ip_vs_stats.outpkts++;
132 ip_vs_stats.outbytes += skb->len;
133 spin_unlock(&ip_vs_stats.lock);
138 static inline void
139 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
141 spin_lock(&cp->dest->stats.lock);
142 cp->dest->stats.conns++;
143 spin_unlock(&cp->dest->stats.lock);
145 spin_lock(&svc->stats.lock);
146 svc->stats.conns++;
147 spin_unlock(&svc->stats.lock);
149 spin_lock(&ip_vs_stats.lock);
150 ip_vs_stats.conns++;
151 spin_unlock(&ip_vs_stats.lock);
155 static inline int
156 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
157 const struct sk_buff *skb,
158 struct ip_vs_protocol *pp)
160 if (unlikely(!pp->state_transition))
161 return 0;
162 return pp->state_transition(cp, direction, skb, pp);
166 int ip_vs_make_skb_writable(struct sk_buff **pskb, int writable_len)
168 struct sk_buff *skb = *pskb;
170 /* skb is already used, better copy skb and its payload */
171 if (unlikely(skb_shared(skb) || skb->sk))
172 goto copy_skb;
174 /* skb data is already used, copy it */
175 if (unlikely(skb_cloned(skb)))
176 goto copy_data;
178 return pskb_may_pull(skb, writable_len);
180 copy_data:
181 if (unlikely(writable_len > skb->len))
182 return 0;
183 return !pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
185 copy_skb:
186 if (unlikely(writable_len > skb->len))
187 return 0;
188 skb = skb_copy(skb, GFP_ATOMIC);
189 if (!skb)
190 return 0;
191 BUG_ON(skb_is_nonlinear(skb));
193 /* Rest of kernel will get very unhappy if we pass it a
194 suddenly-orphaned skbuff */
195 if ((*pskb)->sk)
196 skb_set_owner_w(skb, (*pskb)->sk);
197 kfree_skb(*pskb);
198 *pskb = skb;
199 return 1;
203 * IPVS persistent scheduling function
204 * It creates a connection entry according to its template if exists,
205 * or selects a server and creates a connection entry plus a template.
206 * Locking: we are svc user (svc->refcnt), so we hold all dests too
207 * Protocols supported: TCP, UDP
209 static struct ip_vs_conn *
210 ip_vs_sched_persist(struct ip_vs_service *svc,
211 const struct sk_buff *skb,
212 __be16 ports[2])
214 struct ip_vs_conn *cp = NULL;
215 struct iphdr *iph = ip_hdr(skb);
216 struct ip_vs_dest *dest;
217 struct ip_vs_conn *ct;
218 __be16 dport; /* destination port to forward */
219 __be32 snet; /* source network of the client, after masking */
221 /* Mask saddr with the netmask to adjust template granularity */
222 snet = iph->saddr & svc->netmask;
224 IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u "
225 "mnet %u.%u.%u.%u\n",
226 NIPQUAD(iph->saddr), ntohs(ports[0]),
227 NIPQUAD(iph->daddr), ntohs(ports[1]),
228 NIPQUAD(snet));
231 * As far as we know, FTP is a very complicated network protocol, and
232 * it uses control connection and data connections. For active FTP,
233 * FTP server initialize data connection to the client, its source port
234 * is often 20. For passive FTP, FTP server tells the clients the port
235 * that it passively listens to, and the client issues the data
236 * connection. In the tunneling or direct routing mode, the load
237 * balancer is on the client-to-server half of connection, the port
238 * number is unknown to the load balancer. So, a conn template like
239 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
240 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
241 * is created for other persistent services.
243 if (ports[1] == svc->port) {
244 /* Check if a template already exists */
245 if (svc->port != FTPPORT)
246 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
247 iph->daddr, ports[1]);
248 else
249 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
250 iph->daddr, 0);
252 if (!ct || !ip_vs_check_template(ct)) {
254 * No template found or the dest of the connection
255 * template is not available.
257 dest = svc->scheduler->schedule(svc, skb);
258 if (dest == NULL) {
259 IP_VS_DBG(1, "p-schedule: no dest found.\n");
260 return NULL;
264 * Create a template like <protocol,caddr,0,
265 * vaddr,vport,daddr,dport> for non-ftp service,
266 * and <protocol,caddr,0,vaddr,0,daddr,0>
267 * for ftp service.
269 if (svc->port != FTPPORT)
270 ct = ip_vs_conn_new(iph->protocol,
271 snet, 0,
272 iph->daddr,
273 ports[1],
274 dest->addr, dest->port,
275 IP_VS_CONN_F_TEMPLATE,
276 dest);
277 else
278 ct = ip_vs_conn_new(iph->protocol,
279 snet, 0,
280 iph->daddr, 0,
281 dest->addr, 0,
282 IP_VS_CONN_F_TEMPLATE,
283 dest);
284 if (ct == NULL)
285 return NULL;
287 ct->timeout = svc->timeout;
288 } else {
289 /* set destination with the found template */
290 dest = ct->dest;
292 dport = dest->port;
293 } else {
295 * Note: persistent fwmark-based services and persistent
296 * port zero service are handled here.
297 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
298 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
300 if (svc->fwmark)
301 ct = ip_vs_ct_in_get(IPPROTO_IP, snet, 0,
302 htonl(svc->fwmark), 0);
303 else
304 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
305 iph->daddr, 0);
307 if (!ct || !ip_vs_check_template(ct)) {
309 * If it is not persistent port zero, return NULL,
310 * otherwise create a connection template.
312 if (svc->port)
313 return NULL;
315 dest = svc->scheduler->schedule(svc, skb);
316 if (dest == NULL) {
317 IP_VS_DBG(1, "p-schedule: no dest found.\n");
318 return NULL;
322 * Create a template according to the service
324 if (svc->fwmark)
325 ct = ip_vs_conn_new(IPPROTO_IP,
326 snet, 0,
327 htonl(svc->fwmark), 0,
328 dest->addr, 0,
329 IP_VS_CONN_F_TEMPLATE,
330 dest);
331 else
332 ct = ip_vs_conn_new(iph->protocol,
333 snet, 0,
334 iph->daddr, 0,
335 dest->addr, 0,
336 IP_VS_CONN_F_TEMPLATE,
337 dest);
338 if (ct == NULL)
339 return NULL;
341 ct->timeout = svc->timeout;
342 } else {
343 /* set destination with the found template */
344 dest = ct->dest;
346 dport = ports[1];
350 * Create a new connection according to the template
352 cp = ip_vs_conn_new(iph->protocol,
353 iph->saddr, ports[0],
354 iph->daddr, ports[1],
355 dest->addr, dport,
357 dest);
358 if (cp == NULL) {
359 ip_vs_conn_put(ct);
360 return NULL;
364 * Add its control
366 ip_vs_control_add(cp, ct);
367 ip_vs_conn_put(ct);
369 ip_vs_conn_stats(cp, svc);
370 return cp;
375 * IPVS main scheduling function
376 * It selects a server according to the virtual service, and
377 * creates a connection entry.
378 * Protocols supported: TCP, UDP
380 struct ip_vs_conn *
381 ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
383 struct ip_vs_conn *cp = NULL;
384 struct iphdr *iph = ip_hdr(skb);
385 struct ip_vs_dest *dest;
386 __be16 _ports[2], *pptr;
388 pptr = skb_header_pointer(skb, iph->ihl*4,
389 sizeof(_ports), _ports);
390 if (pptr == NULL)
391 return NULL;
394 * Persistent service
396 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
397 return ip_vs_sched_persist(svc, skb, pptr);
400 * Non-persistent service
402 if (!svc->fwmark && pptr[1] != svc->port) {
403 if (!svc->port)
404 IP_VS_ERR("Schedule: port zero only supported "
405 "in persistent services, "
406 "check your ipvs configuration\n");
407 return NULL;
410 dest = svc->scheduler->schedule(svc, skb);
411 if (dest == NULL) {
412 IP_VS_DBG(1, "Schedule: no dest found.\n");
413 return NULL;
417 * Create a connection entry.
419 cp = ip_vs_conn_new(iph->protocol,
420 iph->saddr, pptr[0],
421 iph->daddr, pptr[1],
422 dest->addr, dest->port?dest->port:pptr[1],
424 dest);
425 if (cp == NULL)
426 return NULL;
428 IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u "
429 "d:%u.%u.%u.%u:%u conn->flags:%X conn->refcnt:%d\n",
430 ip_vs_fwd_tag(cp),
431 NIPQUAD(cp->caddr), ntohs(cp->cport),
432 NIPQUAD(cp->vaddr), ntohs(cp->vport),
433 NIPQUAD(cp->daddr), ntohs(cp->dport),
434 cp->flags, atomic_read(&cp->refcnt));
436 ip_vs_conn_stats(cp, svc);
437 return cp;
442 * Pass or drop the packet.
443 * Called by ip_vs_in, when the virtual service is available but
444 * no destination is available for a new connection.
446 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
447 struct ip_vs_protocol *pp)
449 __be16 _ports[2], *pptr;
450 struct iphdr *iph = ip_hdr(skb);
452 pptr = skb_header_pointer(skb, iph->ihl*4,
453 sizeof(_ports), _ports);
454 if (pptr == NULL) {
455 ip_vs_service_put(svc);
456 return NF_DROP;
459 /* if it is fwmark-based service, the cache_bypass sysctl is up
460 and the destination is RTN_UNICAST (and not local), then create
461 a cache_bypass connection entry */
462 if (sysctl_ip_vs_cache_bypass && svc->fwmark
463 && (inet_addr_type(iph->daddr) == RTN_UNICAST)) {
464 int ret, cs;
465 struct ip_vs_conn *cp;
467 ip_vs_service_put(svc);
469 /* create a new connection entry */
470 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
471 cp = ip_vs_conn_new(iph->protocol,
472 iph->saddr, pptr[0],
473 iph->daddr, pptr[1],
474 0, 0,
475 IP_VS_CONN_F_BYPASS,
476 NULL);
477 if (cp == NULL)
478 return NF_DROP;
480 /* statistics */
481 ip_vs_in_stats(cp, skb);
483 /* set state */
484 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
486 /* transmit the first SYN packet */
487 ret = cp->packet_xmit(skb, cp, pp);
488 /* do not touch skb anymore */
490 atomic_inc(&cp->in_pkts);
491 ip_vs_conn_put(cp);
492 return ret;
496 * When the virtual ftp service is presented, packets destined
497 * for other services on the VIP may get here (except services
498 * listed in the ipvs table), pass the packets, because it is
499 * not ipvs job to decide to drop the packets.
501 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
502 ip_vs_service_put(svc);
503 return NF_ACCEPT;
506 ip_vs_service_put(svc);
509 * Notify the client that the destination is unreachable, and
510 * release the socket buffer.
511 * Since it is in IP layer, the TCP socket is not actually
512 * created, the TCP RST packet cannot be sent, instead that
513 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
515 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
516 return NF_DROP;
521 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_IP_POST_ROUTING
522 * chain, and is used for VS/NAT.
523 * It detects packets for VS/NAT connections and sends the packets
524 * immediately. This can avoid that iptable_nat mangles the packets
525 * for VS/NAT.
527 static unsigned int ip_vs_post_routing(unsigned int hooknum,
528 struct sk_buff **pskb,
529 const struct net_device *in,
530 const struct net_device *out,
531 int (*okfn)(struct sk_buff *))
533 if (!((*pskb)->ipvs_property))
534 return NF_ACCEPT;
535 /* The packet was sent from IPVS, exit this chain */
536 return NF_STOP;
539 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
541 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
544 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
546 int err = ip_defrag(skb, user);
548 if (!err)
549 ip_send_check(ip_hdr(skb));
551 return err;
555 * Packet has been made sufficiently writable in caller
556 * - inout: 1=in->out, 0=out->in
558 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
559 struct ip_vs_conn *cp, int inout)
561 struct iphdr *iph = ip_hdr(skb);
562 unsigned int icmp_offset = iph->ihl*4;
563 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
564 icmp_offset);
565 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
567 if (inout) {
568 iph->saddr = cp->vaddr;
569 ip_send_check(iph);
570 ciph->daddr = cp->vaddr;
571 ip_send_check(ciph);
572 } else {
573 iph->daddr = cp->daddr;
574 ip_send_check(iph);
575 ciph->saddr = cp->daddr;
576 ip_send_check(ciph);
579 /* the TCP/UDP port */
580 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
581 __be16 *ports = (void *)ciph + ciph->ihl*4;
583 if (inout)
584 ports[1] = cp->vport;
585 else
586 ports[0] = cp->dport;
589 /* And finally the ICMP checksum */
590 icmph->checksum = 0;
591 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
592 skb->ip_summed = CHECKSUM_UNNECESSARY;
594 if (inout)
595 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
596 "Forwarding altered outgoing ICMP");
597 else
598 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
599 "Forwarding altered incoming ICMP");
603 * Handle ICMP messages in the inside-to-outside direction (outgoing).
604 * Find any that might be relevant, check against existing connections,
605 * forward to the right destination host if relevant.
606 * Currently handles error types - unreachable, quench, ttl exceeded.
607 * (Only used in VS/NAT)
609 static int ip_vs_out_icmp(struct sk_buff **pskb, int *related)
611 struct sk_buff *skb = *pskb;
612 struct iphdr *iph;
613 struct icmphdr _icmph, *ic;
614 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
615 struct ip_vs_conn *cp;
616 struct ip_vs_protocol *pp;
617 unsigned int offset, ihl, verdict;
619 *related = 1;
621 /* reassemble IP fragments */
622 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
623 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
624 return NF_STOLEN;
627 iph = ip_hdr(skb);
628 offset = ihl = iph->ihl * 4;
629 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
630 if (ic == NULL)
631 return NF_DROP;
633 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
634 ic->type, ntohs(icmp_id(ic)),
635 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
638 * Work through seeing if this is for us.
639 * These checks are supposed to be in an order that means easy
640 * things are checked first to speed up processing.... however
641 * this means that some packets will manage to get a long way
642 * down this stack and then be rejected, but that's life.
644 if ((ic->type != ICMP_DEST_UNREACH) &&
645 (ic->type != ICMP_SOURCE_QUENCH) &&
646 (ic->type != ICMP_TIME_EXCEEDED)) {
647 *related = 0;
648 return NF_ACCEPT;
651 /* Now find the contained IP header */
652 offset += sizeof(_icmph);
653 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
654 if (cih == NULL)
655 return NF_ACCEPT; /* The packet looks wrong, ignore */
657 pp = ip_vs_proto_get(cih->protocol);
658 if (!pp)
659 return NF_ACCEPT;
661 /* Is the embedded protocol header present? */
662 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
663 pp->dont_defrag))
664 return NF_ACCEPT;
666 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
668 offset += cih->ihl * 4;
670 /* The embedded headers contain source and dest in reverse order */
671 cp = pp->conn_out_get(skb, pp, cih, offset, 1);
672 if (!cp)
673 return NF_ACCEPT;
675 verdict = NF_DROP;
677 if (IP_VS_FWD_METHOD(cp) != 0) {
678 IP_VS_ERR("shouldn't reach here, because the box is on the"
679 "half connection in the tun/dr module.\n");
682 /* Ensure the checksum is correct */
683 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
684 /* Failed checksum! */
685 IP_VS_DBG(1, "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
686 NIPQUAD(iph->saddr));
687 goto out;
690 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
691 offset += 2 * sizeof(__u16);
692 if (!ip_vs_make_skb_writable(pskb, offset))
693 goto out;
694 skb = *pskb;
696 ip_vs_nat_icmp(skb, pp, cp, 1);
698 /* do the statistics and put it back */
699 ip_vs_out_stats(cp, skb);
701 skb->ipvs_property = 1;
702 verdict = NF_ACCEPT;
704 out:
705 __ip_vs_conn_put(cp);
707 return verdict;
710 static inline int is_tcp_reset(const struct sk_buff *skb)
712 struct tcphdr _tcph, *th;
714 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
715 if (th == NULL)
716 return 0;
717 return th->rst;
721 * It is hooked at the NF_IP_FORWARD chain, used only for VS/NAT.
722 * Check if outgoing packet belongs to the established ip_vs_conn,
723 * rewrite addresses of the packet and send it on its way...
725 static unsigned int
726 ip_vs_out(unsigned int hooknum, struct sk_buff **pskb,
727 const struct net_device *in, const struct net_device *out,
728 int (*okfn)(struct sk_buff *))
730 struct sk_buff *skb = *pskb;
731 struct iphdr *iph;
732 struct ip_vs_protocol *pp;
733 struct ip_vs_conn *cp;
734 int ihl;
736 EnterFunction(11);
738 if (skb->ipvs_property)
739 return NF_ACCEPT;
741 iph = ip_hdr(skb);
742 if (unlikely(iph->protocol == IPPROTO_ICMP)) {
743 int related, verdict = ip_vs_out_icmp(pskb, &related);
745 if (related)
746 return verdict;
747 skb = *pskb;
748 iph = ip_hdr(skb);
751 pp = ip_vs_proto_get(iph->protocol);
752 if (unlikely(!pp))
753 return NF_ACCEPT;
755 /* reassemble IP fragments */
756 if (unlikely(iph->frag_off & htons(IP_MF|IP_OFFSET) &&
757 !pp->dont_defrag)) {
758 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
759 return NF_STOLEN;
760 iph = ip_hdr(skb);
763 ihl = iph->ihl << 2;
766 * Check if the packet belongs to an existing entry
768 cp = pp->conn_out_get(skb, pp, iph, ihl, 0);
770 if (unlikely(!cp)) {
771 if (sysctl_ip_vs_nat_icmp_send &&
772 (pp->protocol == IPPROTO_TCP ||
773 pp->protocol == IPPROTO_UDP)) {
774 __be16 _ports[2], *pptr;
776 pptr = skb_header_pointer(skb, ihl,
777 sizeof(_ports), _ports);
778 if (pptr == NULL)
779 return NF_ACCEPT; /* Not for me */
780 if (ip_vs_lookup_real_service(iph->protocol,
781 iph->saddr, pptr[0])) {
783 * Notify the real server: there is no
784 * existing entry if it is not RST
785 * packet or not TCP packet.
787 if (iph->protocol != IPPROTO_TCP
788 || !is_tcp_reset(skb)) {
789 icmp_send(skb,ICMP_DEST_UNREACH,
790 ICMP_PORT_UNREACH, 0);
791 return NF_DROP;
795 IP_VS_DBG_PKT(12, pp, skb, 0,
796 "packet continues traversal as normal");
797 return NF_ACCEPT;
800 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
802 if (!ip_vs_make_skb_writable(pskb, ihl))
803 goto drop;
805 /* mangle the packet */
806 if (pp->snat_handler && !pp->snat_handler(pskb, pp, cp))
807 goto drop;
808 skb = *pskb;
809 ip_hdr(skb)->saddr = cp->vaddr;
810 ip_send_check(ip_hdr(skb));
812 /* For policy routing, packets originating from this
813 * machine itself may be routed differently to packets
814 * passing through. We want this packet to be routed as
815 * if it came from this machine itself. So re-compute
816 * the routing information.
818 if (ip_route_me_harder(pskb, RTN_LOCAL) != 0)
819 goto drop;
820 skb = *pskb;
822 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
824 ip_vs_out_stats(cp, skb);
825 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
826 ip_vs_conn_put(cp);
828 skb->ipvs_property = 1;
830 LeaveFunction(11);
831 return NF_ACCEPT;
833 drop:
834 ip_vs_conn_put(cp);
835 kfree_skb(*pskb);
836 return NF_STOLEN;
841 * Handle ICMP messages in the outside-to-inside direction (incoming).
842 * Find any that might be relevant, check against existing connections,
843 * forward to the right destination host if relevant.
844 * Currently handles error types - unreachable, quench, ttl exceeded.
846 static int
847 ip_vs_in_icmp(struct sk_buff **pskb, int *related, unsigned int hooknum)
849 struct sk_buff *skb = *pskb;
850 struct iphdr *iph;
851 struct icmphdr _icmph, *ic;
852 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
853 struct ip_vs_conn *cp;
854 struct ip_vs_protocol *pp;
855 unsigned int offset, ihl, verdict;
857 *related = 1;
859 /* reassemble IP fragments */
860 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
861 if (ip_vs_gather_frags(skb, hooknum == NF_IP_LOCAL_IN ?
862 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
863 return NF_STOLEN;
866 iph = ip_hdr(skb);
867 offset = ihl = iph->ihl * 4;
868 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
869 if (ic == NULL)
870 return NF_DROP;
872 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
873 ic->type, ntohs(icmp_id(ic)),
874 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
877 * Work through seeing if this is for us.
878 * These checks are supposed to be in an order that means easy
879 * things are checked first to speed up processing.... however
880 * this means that some packets will manage to get a long way
881 * down this stack and then be rejected, but that's life.
883 if ((ic->type != ICMP_DEST_UNREACH) &&
884 (ic->type != ICMP_SOURCE_QUENCH) &&
885 (ic->type != ICMP_TIME_EXCEEDED)) {
886 *related = 0;
887 return NF_ACCEPT;
890 /* Now find the contained IP header */
891 offset += sizeof(_icmph);
892 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
893 if (cih == NULL)
894 return NF_ACCEPT; /* The packet looks wrong, ignore */
896 pp = ip_vs_proto_get(cih->protocol);
897 if (!pp)
898 return NF_ACCEPT;
900 /* Is the embedded protocol header present? */
901 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
902 pp->dont_defrag))
903 return NF_ACCEPT;
905 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
907 offset += cih->ihl * 4;
909 /* The embedded headers contain source and dest in reverse order */
910 cp = pp->conn_in_get(skb, pp, cih, offset, 1);
911 if (!cp)
912 return NF_ACCEPT;
914 verdict = NF_DROP;
916 /* Ensure the checksum is correct */
917 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
918 /* Failed checksum! */
919 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
920 NIPQUAD(iph->saddr));
921 goto out;
924 /* do the statistics and put it back */
925 ip_vs_in_stats(cp, skb);
926 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
927 offset += 2 * sizeof(__u16);
928 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
929 /* do not touch skb anymore */
931 out:
932 __ip_vs_conn_put(cp);
934 return verdict;
938 * Check if it's for virtual services, look it up,
939 * and send it on its way...
941 static unsigned int
942 ip_vs_in(unsigned int hooknum, struct sk_buff **pskb,
943 const struct net_device *in, const struct net_device *out,
944 int (*okfn)(struct sk_buff *))
946 struct sk_buff *skb = *pskb;
947 struct iphdr *iph;
948 struct ip_vs_protocol *pp;
949 struct ip_vs_conn *cp;
950 int ret, restart;
951 int ihl;
954 * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
955 * ... don't know why 1st test DOES NOT include 2nd (?)
957 if (unlikely(skb->pkt_type != PACKET_HOST
958 || skb->dev == &loopback_dev || skb->sk)) {
959 IP_VS_DBG(12, "packet type=%d proto=%d daddr=%d.%d.%d.%d ignored\n",
960 skb->pkt_type,
961 ip_hdr(skb)->protocol,
962 NIPQUAD(ip_hdr(skb)->daddr));
963 return NF_ACCEPT;
966 iph = ip_hdr(skb);
967 if (unlikely(iph->protocol == IPPROTO_ICMP)) {
968 int related, verdict = ip_vs_in_icmp(pskb, &related, hooknum);
970 if (related)
971 return verdict;
972 skb = *pskb;
973 iph = ip_hdr(skb);
976 /* Protocol supported? */
977 pp = ip_vs_proto_get(iph->protocol);
978 if (unlikely(!pp))
979 return NF_ACCEPT;
981 ihl = iph->ihl << 2;
984 * Check if the packet belongs to an existing connection entry
986 cp = pp->conn_in_get(skb, pp, iph, ihl, 0);
988 if (unlikely(!cp)) {
989 int v;
991 if (!pp->conn_schedule(skb, pp, &v, &cp))
992 return v;
995 if (unlikely(!cp)) {
996 /* sorry, all this trouble for a no-hit :) */
997 IP_VS_DBG_PKT(12, pp, skb, 0,
998 "packet continues traversal as normal");
999 return NF_ACCEPT;
1002 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1004 /* Check the server status */
1005 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1006 /* the destination server is not available */
1008 if (sysctl_ip_vs_expire_nodest_conn) {
1009 /* try to expire the connection immediately */
1010 ip_vs_conn_expire_now(cp);
1012 /* don't restart its timer, and silently
1013 drop the packet. */
1014 __ip_vs_conn_put(cp);
1015 return NF_DROP;
1018 ip_vs_in_stats(cp, skb);
1019 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1020 if (cp->packet_xmit)
1021 ret = cp->packet_xmit(skb, cp, pp);
1022 /* do not touch skb anymore */
1023 else {
1024 IP_VS_DBG_RL("warning: packet_xmit is null");
1025 ret = NF_ACCEPT;
1028 /* increase its packet counter and check if it is needed
1029 to be synchronized */
1030 atomic_inc(&cp->in_pkts);
1031 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) &&
1032 (cp->protocol != IPPROTO_TCP ||
1033 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1034 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
1035 == sysctl_ip_vs_sync_threshold[0]))
1036 ip_vs_sync_conn(cp);
1038 ip_vs_conn_put(cp);
1039 return ret;
1044 * It is hooked at the NF_IP_FORWARD chain, in order to catch ICMP
1045 * related packets destined for 0.0.0.0/0.
1046 * When fwmark-based virtual service is used, such as transparent
1047 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1048 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1049 * sent to ip_vs_in_icmp. So, catch them at the NF_IP_FORWARD chain
1050 * and send them to ip_vs_in_icmp.
1052 static unsigned int
1053 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff **pskb,
1054 const struct net_device *in, const struct net_device *out,
1055 int (*okfn)(struct sk_buff *))
1057 int r;
1059 if (ip_hdr(*pskb)->protocol != IPPROTO_ICMP)
1060 return NF_ACCEPT;
1062 return ip_vs_in_icmp(pskb, &r, hooknum);
1066 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1067 or VS/NAT(change destination), so that filtering rules can be
1068 applied to IPVS. */
1069 static struct nf_hook_ops ip_vs_in_ops = {
1070 .hook = ip_vs_in,
1071 .owner = THIS_MODULE,
1072 .pf = PF_INET,
1073 .hooknum = NF_IP_LOCAL_IN,
1074 .priority = 100,
1077 /* After packet filtering, change source only for VS/NAT */
1078 static struct nf_hook_ops ip_vs_out_ops = {
1079 .hook = ip_vs_out,
1080 .owner = THIS_MODULE,
1081 .pf = PF_INET,
1082 .hooknum = NF_IP_FORWARD,
1083 .priority = 100,
1086 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1087 destined for 0.0.0.0/0, which is for incoming IPVS connections */
1088 static struct nf_hook_ops ip_vs_forward_icmp_ops = {
1089 .hook = ip_vs_forward_icmp,
1090 .owner = THIS_MODULE,
1091 .pf = PF_INET,
1092 .hooknum = NF_IP_FORWARD,
1093 .priority = 99,
1096 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1097 static struct nf_hook_ops ip_vs_post_routing_ops = {
1098 .hook = ip_vs_post_routing,
1099 .owner = THIS_MODULE,
1100 .pf = PF_INET,
1101 .hooknum = NF_IP_POST_ROUTING,
1102 .priority = NF_IP_PRI_NAT_SRC-1,
1107 * Initialize IP Virtual Server
1109 static int __init ip_vs_init(void)
1111 int ret;
1113 ret = ip_vs_control_init();
1114 if (ret < 0) {
1115 IP_VS_ERR("can't setup control.\n");
1116 goto cleanup_nothing;
1119 ip_vs_protocol_init();
1121 ret = ip_vs_app_init();
1122 if (ret < 0) {
1123 IP_VS_ERR("can't setup application helper.\n");
1124 goto cleanup_protocol;
1127 ret = ip_vs_conn_init();
1128 if (ret < 0) {
1129 IP_VS_ERR("can't setup connection table.\n");
1130 goto cleanup_app;
1133 ret = nf_register_hook(&ip_vs_in_ops);
1134 if (ret < 0) {
1135 IP_VS_ERR("can't register in hook.\n");
1136 goto cleanup_conn;
1139 ret = nf_register_hook(&ip_vs_out_ops);
1140 if (ret < 0) {
1141 IP_VS_ERR("can't register out hook.\n");
1142 goto cleanup_inops;
1144 ret = nf_register_hook(&ip_vs_post_routing_ops);
1145 if (ret < 0) {
1146 IP_VS_ERR("can't register post_routing hook.\n");
1147 goto cleanup_outops;
1149 ret = nf_register_hook(&ip_vs_forward_icmp_ops);
1150 if (ret < 0) {
1151 IP_VS_ERR("can't register forward_icmp hook.\n");
1152 goto cleanup_postroutingops;
1155 IP_VS_INFO("ipvs loaded.\n");
1156 return ret;
1158 cleanup_postroutingops:
1159 nf_unregister_hook(&ip_vs_post_routing_ops);
1160 cleanup_outops:
1161 nf_unregister_hook(&ip_vs_out_ops);
1162 cleanup_inops:
1163 nf_unregister_hook(&ip_vs_in_ops);
1164 cleanup_conn:
1165 ip_vs_conn_cleanup();
1166 cleanup_app:
1167 ip_vs_app_cleanup();
1168 cleanup_protocol:
1169 ip_vs_protocol_cleanup();
1170 ip_vs_control_cleanup();
1171 cleanup_nothing:
1172 return ret;
1175 static void __exit ip_vs_cleanup(void)
1177 nf_unregister_hook(&ip_vs_forward_icmp_ops);
1178 nf_unregister_hook(&ip_vs_post_routing_ops);
1179 nf_unregister_hook(&ip_vs_out_ops);
1180 nf_unregister_hook(&ip_vs_in_ops);
1181 ip_vs_conn_cleanup();
1182 ip_vs_app_cleanup();
1183 ip_vs_protocol_cleanup();
1184 ip_vs_control_cleanup();
1185 IP_VS_INFO("ipvs unloaded.\n");
1188 module_init(ip_vs_init);
1189 module_exit(ip_vs_cleanup);
1190 MODULE_LICENSE("GPL");