IPVS: Extend protocol DNAT/SNAT and state handlers
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / ipvs / ip_vs_core.c
CommitLineData
1da177e4
LT
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.
7 *
1da177e4
LT
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
11 *
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.
16 *
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.
20 *
21 * Changes:
22 * Paul `Rusty' Russell properly handle non-linear skbs
6869c4d8 23 * Harald Welte don't use nfcache
1da177e4
LT
24 *
25 */
26
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>
32
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>
38
39#include <linux/netfilter.h>
40#include <linux/netfilter_ipv4.h>
41
42#include <net/ip_vs.h>
43
44
45EXPORT_SYMBOL(register_ip_vs_scheduler);
46EXPORT_SYMBOL(unregister_ip_vs_scheduler);
47EXPORT_SYMBOL(ip_vs_skb_replace);
48EXPORT_SYMBOL(ip_vs_proto_name);
49EXPORT_SYMBOL(ip_vs_conn_new);
50EXPORT_SYMBOL(ip_vs_conn_in_get);
51EXPORT_SYMBOL(ip_vs_conn_out_get);
52#ifdef CONFIG_IP_VS_PROTO_TCP
53EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
54#endif
55EXPORT_SYMBOL(ip_vs_conn_put);
56#ifdef CONFIG_IP_VS_DEBUG
57EXPORT_SYMBOL(ip_vs_get_debug_level);
58#endif
1da177e4
LT
59
60
61/* ID used in ICMP lookups */
62#define icmp_id(icmph) (((icmph)->un).echo.id)
63
64const char *ip_vs_proto_name(unsigned proto)
65{
66 static char buf[20];
67
68 switch (proto) {
69 case IPPROTO_IP:
70 return "IP";
71 case IPPROTO_UDP:
72 return "UDP";
73 case IPPROTO_TCP:
74 return "TCP";
75 case IPPROTO_ICMP:
76 return "ICMP";
77 default:
78 sprintf(buf, "IP_%d", proto);
79 return buf;
80 }
81}
82
83void ip_vs_init_hash_table(struct list_head *table, int rows)
84{
85 while (--rows >= 0)
86 INIT_LIST_HEAD(&table[rows]);
87}
88
89static inline void
90ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
91{
92 struct ip_vs_dest *dest = cp->dest;
93 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
94 spin_lock(&dest->stats.lock);
95 dest->stats.inpkts++;
96 dest->stats.inbytes += skb->len;
97 spin_unlock(&dest->stats.lock);
98
99 spin_lock(&dest->svc->stats.lock);
100 dest->svc->stats.inpkts++;
101 dest->svc->stats.inbytes += skb->len;
102 spin_unlock(&dest->svc->stats.lock);
103
104 spin_lock(&ip_vs_stats.lock);
105 ip_vs_stats.inpkts++;
106 ip_vs_stats.inbytes += skb->len;
107 spin_unlock(&ip_vs_stats.lock);
108 }
109}
110
111
112static inline void
113ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
114{
115 struct ip_vs_dest *dest = cp->dest;
116 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
117 spin_lock(&dest->stats.lock);
118 dest->stats.outpkts++;
119 dest->stats.outbytes += skb->len;
120 spin_unlock(&dest->stats.lock);
121
122 spin_lock(&dest->svc->stats.lock);
123 dest->svc->stats.outpkts++;
124 dest->svc->stats.outbytes += skb->len;
125 spin_unlock(&dest->svc->stats.lock);
126
127 spin_lock(&ip_vs_stats.lock);
128 ip_vs_stats.outpkts++;
129 ip_vs_stats.outbytes += skb->len;
130 spin_unlock(&ip_vs_stats.lock);
131 }
132}
133
134
135static inline void
136ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
137{
138 spin_lock(&cp->dest->stats.lock);
139 cp->dest->stats.conns++;
140 spin_unlock(&cp->dest->stats.lock);
141
142 spin_lock(&svc->stats.lock);
143 svc->stats.conns++;
144 spin_unlock(&svc->stats.lock);
145
146 spin_lock(&ip_vs_stats.lock);
147 ip_vs_stats.conns++;
148 spin_unlock(&ip_vs_stats.lock);
149}
150
151
152static inline int
153ip_vs_set_state(struct ip_vs_conn *cp, int direction,
154 const struct sk_buff *skb,
155 struct ip_vs_protocol *pp)
156{
157 if (unlikely(!pp->state_transition))
158 return 0;
159 return pp->state_transition(cp, direction, skb, pp);
160}
161
162
1da177e4
LT
163/*
164 * IPVS persistent scheduling function
165 * It creates a connection entry according to its template if exists,
166 * or selects a server and creates a connection entry plus a template.
167 * Locking: we are svc user (svc->refcnt), so we hold all dests too
168 * Protocols supported: TCP, UDP
169 */
170static struct ip_vs_conn *
171ip_vs_sched_persist(struct ip_vs_service *svc,
172 const struct sk_buff *skb,
014d730d 173 __be16 ports[2])
1da177e4
LT
174{
175 struct ip_vs_conn *cp = NULL;
eddc9ec5 176 struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
177 struct ip_vs_dest *dest;
178 struct ip_vs_conn *ct;
014d730d
AV
179 __be16 dport; /* destination port to forward */
180 __be32 snet; /* source network of the client, after masking */
1da177e4
LT
181
182 /* Mask saddr with the netmask to adjust template granularity */
183 snet = iph->saddr & svc->netmask;
184
185 IP_VS_DBG(6, "p-schedule: src %u.%u.%u.%u:%u dest %u.%u.%u.%u:%u "
186 "mnet %u.%u.%u.%u\n",
187 NIPQUAD(iph->saddr), ntohs(ports[0]),
188 NIPQUAD(iph->daddr), ntohs(ports[1]),
189 NIPQUAD(snet));
190
191 /*
192 * As far as we know, FTP is a very complicated network protocol, and
193 * it uses control connection and data connections. For active FTP,
194 * FTP server initialize data connection to the client, its source port
195 * is often 20. For passive FTP, FTP server tells the clients the port
196 * that it passively listens to, and the client issues the data
197 * connection. In the tunneling or direct routing mode, the load
198 * balancer is on the client-to-server half of connection, the port
199 * number is unknown to the load balancer. So, a conn template like
200 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
201 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
202 * is created for other persistent services.
203 */
204 if (ports[1] == svc->port) {
205 /* Check if a template already exists */
206 if (svc->port != FTPPORT)
87375ab4 207 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
1da177e4
LT
208 iph->daddr, ports[1]);
209 else
87375ab4 210 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
1da177e4
LT
211 iph->daddr, 0);
212
213 if (!ct || !ip_vs_check_template(ct)) {
214 /*
215 * No template found or the dest of the connection
216 * template is not available.
217 */
218 dest = svc->scheduler->schedule(svc, skb);
219 if (dest == NULL) {
220 IP_VS_DBG(1, "p-schedule: no dest found.\n");
221 return NULL;
222 }
223
224 /*
225 * Create a template like <protocol,caddr,0,
226 * vaddr,vport,daddr,dport> for non-ftp service,
227 * and <protocol,caddr,0,vaddr,0,daddr,0>
228 * for ftp service.
229 */
230 if (svc->port != FTPPORT)
231 ct = ip_vs_conn_new(iph->protocol,
232 snet, 0,
233 iph->daddr,
234 ports[1],
e7ade46a 235 dest->addr.ip, dest->port,
87375ab4 236 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
237 dest);
238 else
239 ct = ip_vs_conn_new(iph->protocol,
240 snet, 0,
241 iph->daddr, 0,
e7ade46a 242 dest->addr.ip, 0,
87375ab4 243 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
244 dest);
245 if (ct == NULL)
246 return NULL;
247
248 ct->timeout = svc->timeout;
249 } else {
250 /* set destination with the found template */
251 dest = ct->dest;
252 }
253 dport = dest->port;
254 } else {
255 /*
256 * Note: persistent fwmark-based services and persistent
257 * port zero service are handled here.
258 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
259 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
260 */
261 if (svc->fwmark)
87375ab4 262 ct = ip_vs_ct_in_get(IPPROTO_IP, snet, 0,
1da177e4
LT
263 htonl(svc->fwmark), 0);
264 else
87375ab4 265 ct = ip_vs_ct_in_get(iph->protocol, snet, 0,
1da177e4
LT
266 iph->daddr, 0);
267
268 if (!ct || !ip_vs_check_template(ct)) {
269 /*
270 * If it is not persistent port zero, return NULL,
271 * otherwise create a connection template.
272 */
273 if (svc->port)
274 return NULL;
275
276 dest = svc->scheduler->schedule(svc, skb);
277 if (dest == NULL) {
278 IP_VS_DBG(1, "p-schedule: no dest found.\n");
279 return NULL;
280 }
281
282 /*
283 * Create a template according to the service
284 */
285 if (svc->fwmark)
286 ct = ip_vs_conn_new(IPPROTO_IP,
287 snet, 0,
288 htonl(svc->fwmark), 0,
e7ade46a 289 dest->addr.ip, 0,
87375ab4 290 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
291 dest);
292 else
293 ct = ip_vs_conn_new(iph->protocol,
294 snet, 0,
295 iph->daddr, 0,
e7ade46a 296 dest->addr.ip, 0,
87375ab4 297 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
298 dest);
299 if (ct == NULL)
300 return NULL;
301
302 ct->timeout = svc->timeout;
303 } else {
304 /* set destination with the found template */
305 dest = ct->dest;
306 }
307 dport = ports[1];
308 }
309
310 /*
311 * Create a new connection according to the template
312 */
313 cp = ip_vs_conn_new(iph->protocol,
314 iph->saddr, ports[0],
315 iph->daddr, ports[1],
e7ade46a 316 dest->addr.ip, dport,
1da177e4
LT
317 0,
318 dest);
319 if (cp == NULL) {
320 ip_vs_conn_put(ct);
321 return NULL;
322 }
323
324 /*
325 * Add its control
326 */
327 ip_vs_control_add(cp, ct);
328 ip_vs_conn_put(ct);
329
330 ip_vs_conn_stats(cp, svc);
331 return cp;
332}
333
334
335/*
336 * IPVS main scheduling function
337 * It selects a server according to the virtual service, and
338 * creates a connection entry.
339 * Protocols supported: TCP, UDP
340 */
341struct ip_vs_conn *
342ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
343{
344 struct ip_vs_conn *cp = NULL;
eddc9ec5 345 struct iphdr *iph = ip_hdr(skb);
1da177e4 346 struct ip_vs_dest *dest;
014d730d 347 __be16 _ports[2], *pptr;
1da177e4
LT
348
349 pptr = skb_header_pointer(skb, iph->ihl*4,
350 sizeof(_ports), _ports);
351 if (pptr == NULL)
352 return NULL;
353
354 /*
355 * Persistent service
356 */
357 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
358 return ip_vs_sched_persist(svc, skb, pptr);
359
360 /*
361 * Non-persistent service
362 */
363 if (!svc->fwmark && pptr[1] != svc->port) {
364 if (!svc->port)
365 IP_VS_ERR("Schedule: port zero only supported "
366 "in persistent services, "
367 "check your ipvs configuration\n");
368 return NULL;
369 }
370
371 dest = svc->scheduler->schedule(svc, skb);
372 if (dest == NULL) {
373 IP_VS_DBG(1, "Schedule: no dest found.\n");
374 return NULL;
375 }
376
377 /*
378 * Create a connection entry.
379 */
380 cp = ip_vs_conn_new(iph->protocol,
381 iph->saddr, pptr[0],
382 iph->daddr, pptr[1],
e7ade46a 383 dest->addr.ip, dest->port ? dest->port : pptr[1],
1da177e4
LT
384 0,
385 dest);
386 if (cp == NULL)
387 return NULL;
388
389 IP_VS_DBG(6, "Schedule fwd:%c c:%u.%u.%u.%u:%u v:%u.%u.%u.%u:%u "
4b5bdf5c 390 "d:%u.%u.%u.%u:%u conn->flags:%X conn->refcnt:%d\n",
1da177e4 391 ip_vs_fwd_tag(cp),
e7ade46a
JV
392 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
393 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
394 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
1da177e4
LT
395 cp->flags, atomic_read(&cp->refcnt));
396
397 ip_vs_conn_stats(cp, svc);
398 return cp;
399}
400
401
402/*
403 * Pass or drop the packet.
404 * Called by ip_vs_in, when the virtual service is available but
405 * no destination is available for a new connection.
406 */
407int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
408 struct ip_vs_protocol *pp)
409{
014d730d 410 __be16 _ports[2], *pptr;
eddc9ec5 411 struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
412
413 pptr = skb_header_pointer(skb, iph->ihl*4,
414 sizeof(_ports), _ports);
415 if (pptr == NULL) {
416 ip_vs_service_put(svc);
417 return NF_DROP;
418 }
419
420 /* if it is fwmark-based service, the cache_bypass sysctl is up
421 and the destination is RTN_UNICAST (and not local), then create
422 a cache_bypass connection entry */
423 if (sysctl_ip_vs_cache_bypass && svc->fwmark
6b175b26 424 && (inet_addr_type(&init_net, iph->daddr) == RTN_UNICAST)) {
1da177e4
LT
425 int ret, cs;
426 struct ip_vs_conn *cp;
427
428 ip_vs_service_put(svc);
429
430 /* create a new connection entry */
431 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
432 cp = ip_vs_conn_new(iph->protocol,
433 iph->saddr, pptr[0],
434 iph->daddr, pptr[1],
435 0, 0,
436 IP_VS_CONN_F_BYPASS,
437 NULL);
438 if (cp == NULL)
439 return NF_DROP;
440
441 /* statistics */
442 ip_vs_in_stats(cp, skb);
443
444 /* set state */
445 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
446
447 /* transmit the first SYN packet */
448 ret = cp->packet_xmit(skb, cp, pp);
449 /* do not touch skb anymore */
450
451 atomic_inc(&cp->in_pkts);
452 ip_vs_conn_put(cp);
453 return ret;
454 }
455
456 /*
457 * When the virtual ftp service is presented, packets destined
458 * for other services on the VIP may get here (except services
459 * listed in the ipvs table), pass the packets, because it is
460 * not ipvs job to decide to drop the packets.
461 */
462 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
463 ip_vs_service_put(svc);
464 return NF_ACCEPT;
465 }
466
467 ip_vs_service_put(svc);
468
469 /*
470 * Notify the client that the destination is unreachable, and
471 * release the socket buffer.
472 * Since it is in IP layer, the TCP socket is not actually
473 * created, the TCP RST packet cannot be sent, instead that
474 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
475 */
476 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
477 return NF_DROP;
478}
479
480
481/*
6e23ae2a 482 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
1da177e4
LT
483 * chain, and is used for VS/NAT.
484 * It detects packets for VS/NAT connections and sends the packets
485 * immediately. This can avoid that iptable_nat mangles the packets
486 * for VS/NAT.
487 */
488static unsigned int ip_vs_post_routing(unsigned int hooknum,
3db05fea 489 struct sk_buff *skb,
1da177e4
LT
490 const struct net_device *in,
491 const struct net_device *out,
492 int (*okfn)(struct sk_buff *))
493{
3db05fea 494 if (!skb->ipvs_property)
1da177e4 495 return NF_ACCEPT;
1da177e4 496 /* The packet was sent from IPVS, exit this chain */
abbcc739 497 return NF_STOP;
1da177e4
LT
498}
499
b1550f22 500__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
1da177e4 501{
d3bc23e7 502 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
1da177e4
LT
503}
504
776c729e 505static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
1da177e4 506{
776c729e
HX
507 int err = ip_defrag(skb, user);
508
509 if (!err)
eddc9ec5 510 ip_send_check(ip_hdr(skb));
776c729e
HX
511
512 return err;
1da177e4
LT
513}
514
515/*
516 * Packet has been made sufficiently writable in caller
517 * - inout: 1=in->out, 0=out->in
518 */
519void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
520 struct ip_vs_conn *cp, int inout)
521{
eddc9ec5 522 struct iphdr *iph = ip_hdr(skb);
1da177e4 523 unsigned int icmp_offset = iph->ihl*4;
d56f90a7
ACM
524 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
525 icmp_offset);
1da177e4
LT
526 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
527
528 if (inout) {
e7ade46a 529 iph->saddr = cp->vaddr.ip;
1da177e4 530 ip_send_check(iph);
e7ade46a 531 ciph->daddr = cp->vaddr.ip;
1da177e4
LT
532 ip_send_check(ciph);
533 } else {
e7ade46a 534 iph->daddr = cp->daddr.ip;
1da177e4 535 ip_send_check(iph);
e7ade46a 536 ciph->saddr = cp->daddr.ip;
1da177e4
LT
537 ip_send_check(ciph);
538 }
539
540 /* the TCP/UDP port */
541 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
014d730d 542 __be16 *ports = (void *)ciph + ciph->ihl*4;
1da177e4
LT
543
544 if (inout)
545 ports[1] = cp->vport;
546 else
547 ports[0] = cp->dport;
548 }
549
550 /* And finally the ICMP checksum */
551 icmph->checksum = 0;
552 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
553 skb->ip_summed = CHECKSUM_UNNECESSARY;
554
555 if (inout)
556 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
557 "Forwarding altered outgoing ICMP");
558 else
559 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
560 "Forwarding altered incoming ICMP");
561}
562
563/*
564 * Handle ICMP messages in the inside-to-outside direction (outgoing).
565 * Find any that might be relevant, check against existing connections,
566 * forward to the right destination host if relevant.
567 * Currently handles error types - unreachable, quench, ttl exceeded.
568 * (Only used in VS/NAT)
569 */
3db05fea 570static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
1da177e4 571{
1da177e4
LT
572 struct iphdr *iph;
573 struct icmphdr _icmph, *ic;
574 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 575 struct ip_vs_iphdr ciph;
1da177e4
LT
576 struct ip_vs_conn *cp;
577 struct ip_vs_protocol *pp;
578 unsigned int offset, ihl, verdict;
579
580 *related = 1;
581
582 /* reassemble IP fragments */
eddc9ec5 583 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
776c729e 584 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
1da177e4 585 return NF_STOLEN;
1da177e4
LT
586 }
587
eddc9ec5 588 iph = ip_hdr(skb);
1da177e4
LT
589 offset = ihl = iph->ihl * 4;
590 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
591 if (ic == NULL)
592 return NF_DROP;
593
594 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
595 ic->type, ntohs(icmp_id(ic)),
596 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
597
598 /*
599 * Work through seeing if this is for us.
600 * These checks are supposed to be in an order that means easy
601 * things are checked first to speed up processing.... however
602 * this means that some packets will manage to get a long way
603 * down this stack and then be rejected, but that's life.
604 */
605 if ((ic->type != ICMP_DEST_UNREACH) &&
606 (ic->type != ICMP_SOURCE_QUENCH) &&
607 (ic->type != ICMP_TIME_EXCEEDED)) {
608 *related = 0;
609 return NF_ACCEPT;
610 }
611
612 /* Now find the contained IP header */
613 offset += sizeof(_icmph);
614 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
615 if (cih == NULL)
616 return NF_ACCEPT; /* The packet looks wrong, ignore */
617
618 pp = ip_vs_proto_get(cih->protocol);
619 if (!pp)
620 return NF_ACCEPT;
621
622 /* Is the embedded protocol header present? */
4412ec49 623 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
624 pp->dont_defrag))
625 return NF_ACCEPT;
626
627 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
628
629 offset += cih->ihl * 4;
630
51ef348b 631 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 632 /* The embedded headers contain source and dest in reverse order */
51ef348b 633 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
1da177e4
LT
634 if (!cp)
635 return NF_ACCEPT;
636
637 verdict = NF_DROP;
638
639 if (IP_VS_FWD_METHOD(cp) != 0) {
464c4f18 640 IP_VS_ERR("shouldn't reach here, because the box is on the "
1da177e4
LT
641 "half connection in the tun/dr module.\n");
642 }
643
644 /* Ensure the checksum is correct */
60476372 645 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4
LT
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;
650 }
651
652 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
653 offset += 2 * sizeof(__u16);
af1e1cf0 654 if (!skb_make_writable(skb, offset))
1da177e4 655 goto out;
1da177e4
LT
656
657 ip_vs_nat_icmp(skb, pp, cp, 1);
658
659 /* do the statistics and put it back */
660 ip_vs_out_stats(cp, skb);
661
6869c4d8 662 skb->ipvs_property = 1;
1da177e4
LT
663 verdict = NF_ACCEPT;
664
665 out:
666 __ip_vs_conn_put(cp);
667
668 return verdict;
669}
670
671static inline int is_tcp_reset(const struct sk_buff *skb)
672{
673 struct tcphdr _tcph, *th;
674
c9bdd4b5 675 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
1da177e4
LT
676 if (th == NULL)
677 return 0;
678 return th->rst;
679}
680
681/*
6e23ae2a 682 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
1da177e4
LT
683 * Check if outgoing packet belongs to the established ip_vs_conn,
684 * rewrite addresses of the packet and send it on its way...
685 */
686static unsigned int
3db05fea 687ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
688 const struct net_device *in, const struct net_device *out,
689 int (*okfn)(struct sk_buff *))
690{
51ef348b 691 struct ip_vs_iphdr iph;
1da177e4
LT
692 struct ip_vs_protocol *pp;
693 struct ip_vs_conn *cp;
1da177e4
LT
694
695 EnterFunction(11);
696
6869c4d8 697 if (skb->ipvs_property)
1da177e4
LT
698 return NF_ACCEPT;
699
51ef348b
JV
700 ip_vs_fill_iphdr(AF_INET, skb_network_header(skb), &iph);
701 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
3db05fea 702 int related, verdict = ip_vs_out_icmp(skb, &related);
1da177e4
LT
703
704 if (related)
705 return verdict;
51ef348b 706 ip_vs_fill_iphdr(AF_INET, skb_network_header(skb), &iph);
1da177e4
LT
707 }
708
51ef348b 709 pp = ip_vs_proto_get(iph.protocol);
1da177e4
LT
710 if (unlikely(!pp))
711 return NF_ACCEPT;
712
713 /* reassemble IP fragments */
51ef348b 714 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1da177e4 715 !pp->dont_defrag)) {
776c729e 716 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
1da177e4 717 return NF_STOLEN;
1da177e4 718
51ef348b
JV
719 ip_vs_fill_iphdr(AF_INET, skb_network_header(skb), &iph);
720 }
1da177e4
LT
721
722 /*
723 * Check if the packet belongs to an existing entry
724 */
51ef348b 725 cp = pp->conn_out_get(AF_INET, skb, pp, &iph, iph.len, 0);
1da177e4
LT
726
727 if (unlikely(!cp)) {
728 if (sysctl_ip_vs_nat_icmp_send &&
729 (pp->protocol == IPPROTO_TCP ||
730 pp->protocol == IPPROTO_UDP)) {
014d730d 731 __be16 _ports[2], *pptr;
1da177e4 732
51ef348b 733 pptr = skb_header_pointer(skb, iph.len,
1da177e4
LT
734 sizeof(_ports), _ports);
735 if (pptr == NULL)
736 return NF_ACCEPT; /* Not for me */
51ef348b
JV
737 if (ip_vs_lookup_real_service(iph.protocol,
738 iph.saddr.ip, pptr[0])) {
1da177e4
LT
739 /*
740 * Notify the real server: there is no
741 * existing entry if it is not RST
742 * packet or not TCP packet.
743 */
51ef348b 744 if (iph.protocol != IPPROTO_TCP
1da177e4
LT
745 || !is_tcp_reset(skb)) {
746 icmp_send(skb,ICMP_DEST_UNREACH,
747 ICMP_PORT_UNREACH, 0);
748 return NF_DROP;
749 }
750 }
751 }
752 IP_VS_DBG_PKT(12, pp, skb, 0,
753 "packet continues traversal as normal");
754 return NF_ACCEPT;
755 }
756
757 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
758
51ef348b 759 if (!skb_make_writable(skb, iph.len))
1da177e4
LT
760 goto drop;
761
762 /* mangle the packet */
3db05fea 763 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
1da177e4 764 goto drop;
e7ade46a 765 ip_hdr(skb)->saddr = cp->vaddr.ip;
eddc9ec5 766 ip_send_check(ip_hdr(skb));
1da177e4 767
e905a9ed
YH
768 /* For policy routing, packets originating from this
769 * machine itself may be routed differently to packets
770 * passing through. We want this packet to be routed as
771 * if it came from this machine itself. So re-compute
772 * the routing information.
773 */
3db05fea 774 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
e905a9ed 775 goto drop;
901eaf6c 776
1da177e4
LT
777 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
778
779 ip_vs_out_stats(cp, skb);
780 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
781 ip_vs_conn_put(cp);
782
6869c4d8 783 skb->ipvs_property = 1;
1da177e4
LT
784
785 LeaveFunction(11);
786 return NF_ACCEPT;
787
788 drop:
789 ip_vs_conn_put(cp);
3db05fea 790 kfree_skb(skb);
1da177e4
LT
791 return NF_STOLEN;
792}
793
794
795/*
796 * Handle ICMP messages in the outside-to-inside direction (incoming).
797 * Find any that might be relevant, check against existing connections,
798 * forward to the right destination host if relevant.
799 * Currently handles error types - unreachable, quench, ttl exceeded.
800 */
e905a9ed 801static int
3db05fea 802ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1da177e4 803{
1da177e4
LT
804 struct iphdr *iph;
805 struct icmphdr _icmph, *ic;
806 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 807 struct ip_vs_iphdr ciph;
1da177e4
LT
808 struct ip_vs_conn *cp;
809 struct ip_vs_protocol *pp;
810 unsigned int offset, ihl, verdict;
811
812 *related = 1;
813
814 /* reassemble IP fragments */
eddc9ec5 815 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
6e23ae2a 816 if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
776c729e 817 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
1da177e4 818 return NF_STOLEN;
1da177e4
LT
819 }
820
eddc9ec5 821 iph = ip_hdr(skb);
1da177e4
LT
822 offset = ihl = iph->ihl * 4;
823 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
824 if (ic == NULL)
825 return NF_DROP;
826
827 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
828 ic->type, ntohs(icmp_id(ic)),
829 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
830
831 /*
832 * Work through seeing if this is for us.
833 * These checks are supposed to be in an order that means easy
834 * things are checked first to speed up processing.... however
835 * this means that some packets will manage to get a long way
836 * down this stack and then be rejected, but that's life.
837 */
838 if ((ic->type != ICMP_DEST_UNREACH) &&
839 (ic->type != ICMP_SOURCE_QUENCH) &&
840 (ic->type != ICMP_TIME_EXCEEDED)) {
841 *related = 0;
842 return NF_ACCEPT;
843 }
844
845 /* Now find the contained IP header */
846 offset += sizeof(_icmph);
847 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
848 if (cih == NULL)
849 return NF_ACCEPT; /* The packet looks wrong, ignore */
850
851 pp = ip_vs_proto_get(cih->protocol);
852 if (!pp)
853 return NF_ACCEPT;
854
855 /* Is the embedded protocol header present? */
4412ec49 856 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
857 pp->dont_defrag))
858 return NF_ACCEPT;
859
860 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
861
862 offset += cih->ihl * 4;
863
51ef348b 864 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 865 /* The embedded headers contain source and dest in reverse order */
51ef348b 866 cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
1da177e4
LT
867 if (!cp)
868 return NF_ACCEPT;
869
870 verdict = NF_DROP;
871
872 /* Ensure the checksum is correct */
60476372 873 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4
LT
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;
878 }
879
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 */
886
887 out:
888 __ip_vs_conn_put(cp);
889
890 return verdict;
891}
892
893/*
894 * Check if it's for virtual services, look it up,
895 * and send it on its way...
896 */
897static unsigned int
3db05fea 898ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
899 const struct net_device *in, const struct net_device *out,
900 int (*okfn)(struct sk_buff *))
901{
51ef348b 902 struct ip_vs_iphdr iph;
1da177e4
LT
903 struct ip_vs_protocol *pp;
904 struct ip_vs_conn *cp;
905 int ret, restart;
51ef348b
JV
906
907 ip_vs_fill_iphdr(AF_INET, skb_network_header(skb), &iph);
1da177e4
LT
908
909 /*
910 * Big tappo: only PACKET_HOST (neither loopback nor mcasts)
911 * ... don't know why 1st test DOES NOT include 2nd (?)
912 */
913 if (unlikely(skb->pkt_type != PACKET_HOST
0cc217e1 914 || skb->dev->flags & IFF_LOOPBACK || skb->sk)) {
51ef348b
JV
915 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
916 skb->pkt_type,
917 iph.protocol,
918 IP_VS_DBG_ADDR(AF_INET, &iph.daddr));
1da177e4
LT
919 return NF_ACCEPT;
920 }
921
51ef348b 922 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
3db05fea 923 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
1da177e4
LT
924
925 if (related)
926 return verdict;
51ef348b 927 ip_vs_fill_iphdr(AF_INET, skb_network_header(skb), &iph);
1da177e4
LT
928 }
929
930 /* Protocol supported? */
51ef348b 931 pp = ip_vs_proto_get(iph.protocol);
1da177e4
LT
932 if (unlikely(!pp))
933 return NF_ACCEPT;
934
1da177e4
LT
935 /*
936 * Check if the packet belongs to an existing connection entry
937 */
51ef348b 938 cp = pp->conn_in_get(AF_INET, skb, pp, &iph, iph.len, 0);
1da177e4
LT
939
940 if (unlikely(!cp)) {
941 int v;
942
51ef348b 943 if (!pp->conn_schedule(AF_INET, skb, pp, &v, &cp))
1da177e4
LT
944 return v;
945 }
946
947 if (unlikely(!cp)) {
948 /* sorry, all this trouble for a no-hit :) */
949 IP_VS_DBG_PKT(12, pp, skb, 0,
950 "packet continues traversal as normal");
951 return NF_ACCEPT;
952 }
953
954 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
955
956 /* Check the server status */
957 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
958 /* the destination server is not available */
959
960 if (sysctl_ip_vs_expire_nodest_conn) {
961 /* try to expire the connection immediately */
962 ip_vs_conn_expire_now(cp);
1da177e4 963 }
dc8103f2
JA
964 /* don't restart its timer, and silently
965 drop the packet. */
966 __ip_vs_conn_put(cp);
1da177e4
LT
967 return NF_DROP;
968 }
969
970 ip_vs_in_stats(cp, skb);
971 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
972 if (cp->packet_xmit)
973 ret = cp->packet_xmit(skb, cp, pp);
974 /* do not touch skb anymore */
975 else {
976 IP_VS_DBG_RL("warning: packet_xmit is null");
977 ret = NF_ACCEPT;
978 }
979
efac5276
RB
980 /* Increase its packet counter and check if it is needed
981 * to be synchronized
982 *
983 * Sync connection if it is about to close to
984 * encorage the standby servers to update the connections timeout
985 */
1da177e4
LT
986 atomic_inc(&cp->in_pkts);
987 if ((ip_vs_sync_state & IP_VS_STATE_MASTER) &&
efac5276
RB
988 (((cp->protocol != IPPROTO_TCP ||
989 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
990 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
991 == sysctl_ip_vs_sync_threshold[0])) ||
992 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
993 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
9d3a0de7
RB
994 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
995 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1da177e4 996 ip_vs_sync_conn(cp);
efac5276 997 cp->old_state = cp->state;
1da177e4
LT
998
999 ip_vs_conn_put(cp);
1000 return ret;
1001}
1002
1003
1004/*
6e23ae2a 1005 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1da177e4
LT
1006 * related packets destined for 0.0.0.0/0.
1007 * When fwmark-based virtual service is used, such as transparent
1008 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1009 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
6e23ae2a 1010 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1da177e4
LT
1011 * and send them to ip_vs_in_icmp.
1012 */
1013static unsigned int
3db05fea 1014ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1015 const struct net_device *in, const struct net_device *out,
1016 int (*okfn)(struct sk_buff *))
1017{
1018 int r;
1019
3db05fea 1020 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
1021 return NF_ACCEPT;
1022
3db05fea 1023 return ip_vs_in_icmp(skb, &r, hooknum);
1da177e4
LT
1024}
1025
1026
1999414a 1027static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
41c5b317
PM
1028 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1029 * or VS/NAT(change destination), so that filtering rules can be
1030 * applied to IPVS. */
1031 {
1032 .hook = ip_vs_in,
1033 .owner = THIS_MODULE,
1034 .pf = PF_INET,
1035 .hooknum = NF_INET_LOCAL_IN,
1036 .priority = 100,
1037 },
1038 /* After packet filtering, change source only for VS/NAT */
1039 {
1040 .hook = ip_vs_out,
1041 .owner = THIS_MODULE,
1042 .pf = PF_INET,
1043 .hooknum = NF_INET_FORWARD,
1044 .priority = 100,
1045 },
1046 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1047 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1048 {
1049 .hook = ip_vs_forward_icmp,
1050 .owner = THIS_MODULE,
1051 .pf = PF_INET,
1052 .hooknum = NF_INET_FORWARD,
1053 .priority = 99,
1054 },
1055 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1056 {
1057 .hook = ip_vs_post_routing,
1058 .owner = THIS_MODULE,
1059 .pf = PF_INET,
1060 .hooknum = NF_INET_POST_ROUTING,
1061 .priority = NF_IP_PRI_NAT_SRC-1,
1062 },
1da177e4
LT
1063};
1064
1065
1066/*
1067 * Initialize IP Virtual Server
1068 */
1069static int __init ip_vs_init(void)
1070{
1071 int ret;
1072
a919cf4b
SW
1073 ip_vs_estimator_init();
1074
1da177e4
LT
1075 ret = ip_vs_control_init();
1076 if (ret < 0) {
1077 IP_VS_ERR("can't setup control.\n");
a919cf4b 1078 goto cleanup_estimator;
1da177e4
LT
1079 }
1080
1081 ip_vs_protocol_init();
1082
1083 ret = ip_vs_app_init();
1084 if (ret < 0) {
1085 IP_VS_ERR("can't setup application helper.\n");
1086 goto cleanup_protocol;
1087 }
1088
1089 ret = ip_vs_conn_init();
1090 if (ret < 0) {
1091 IP_VS_ERR("can't setup connection table.\n");
1092 goto cleanup_app;
1093 }
1094
41c5b317 1095 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4 1096 if (ret < 0) {
41c5b317 1097 IP_VS_ERR("can't register hooks.\n");
1da177e4
LT
1098 goto cleanup_conn;
1099 }
1100
1da177e4
LT
1101 IP_VS_INFO("ipvs loaded.\n");
1102 return ret;
1103
1da177e4
LT
1104 cleanup_conn:
1105 ip_vs_conn_cleanup();
1106 cleanup_app:
1107 ip_vs_app_cleanup();
1108 cleanup_protocol:
1109 ip_vs_protocol_cleanup();
1110 ip_vs_control_cleanup();
a919cf4b
SW
1111 cleanup_estimator:
1112 ip_vs_estimator_cleanup();
1da177e4
LT
1113 return ret;
1114}
1115
1116static void __exit ip_vs_cleanup(void)
1117{
41c5b317 1118 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4
LT
1119 ip_vs_conn_cleanup();
1120 ip_vs_app_cleanup();
1121 ip_vs_protocol_cleanup();
1122 ip_vs_control_cleanup();
a919cf4b 1123 ip_vs_estimator_cleanup();
1da177e4
LT
1124 IP_VS_INFO("ipvs unloaded.\n");
1125}
1126
1127module_init(ip_vs_init);
1128module_exit(ip_vs_cleanup);
1129MODULE_LICENSE("GPL");