]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/ipvs/ip_vs_core.c
ipvs: Restrict connection table size via Kconfig
[net-next-2.6.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
2a3b791e
JV
42#ifdef CONFIG_IP_VS_IPV6
43#include <net/ipv6.h>
44#include <linux/netfilter_ipv6.h>
45#endif
46
1da177e4
LT
47#include <net/ip_vs.h>
48
49
50EXPORT_SYMBOL(register_ip_vs_scheduler);
51EXPORT_SYMBOL(unregister_ip_vs_scheduler);
52EXPORT_SYMBOL(ip_vs_skb_replace);
53EXPORT_SYMBOL(ip_vs_proto_name);
54EXPORT_SYMBOL(ip_vs_conn_new);
55EXPORT_SYMBOL(ip_vs_conn_in_get);
56EXPORT_SYMBOL(ip_vs_conn_out_get);
57#ifdef CONFIG_IP_VS_PROTO_TCP
58EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
59#endif
60EXPORT_SYMBOL(ip_vs_conn_put);
61#ifdef CONFIG_IP_VS_DEBUG
62EXPORT_SYMBOL(ip_vs_get_debug_level);
63#endif
1da177e4
LT
64
65
66/* ID used in ICMP lookups */
67#define icmp_id(icmph) (((icmph)->un).echo.id)
2a3b791e 68#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
1da177e4
LT
69
70const char *ip_vs_proto_name(unsigned proto)
71{
72 static char buf[20];
73
74 switch (proto) {
75 case IPPROTO_IP:
76 return "IP";
77 case IPPROTO_UDP:
78 return "UDP";
79 case IPPROTO_TCP:
80 return "TCP";
81 case IPPROTO_ICMP:
82 return "ICMP";
2a3b791e
JV
83#ifdef CONFIG_IP_VS_IPV6
84 case IPPROTO_ICMPV6:
85 return "ICMPv6";
86#endif
1da177e4
LT
87 default:
88 sprintf(buf, "IP_%d", proto);
89 return buf;
90 }
91}
92
93void ip_vs_init_hash_table(struct list_head *table, int rows)
94{
95 while (--rows >= 0)
96 INIT_LIST_HEAD(&table[rows]);
97}
98
99static inline void
100ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
101{
102 struct ip_vs_dest *dest = cp->dest;
103 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
104 spin_lock(&dest->stats.lock);
105 dest->stats.inpkts++;
106 dest->stats.inbytes += skb->len;
107 spin_unlock(&dest->stats.lock);
108
109 spin_lock(&dest->svc->stats.lock);
110 dest->svc->stats.inpkts++;
111 dest->svc->stats.inbytes += skb->len;
112 spin_unlock(&dest->svc->stats.lock);
113
114 spin_lock(&ip_vs_stats.lock);
115 ip_vs_stats.inpkts++;
116 ip_vs_stats.inbytes += skb->len;
117 spin_unlock(&ip_vs_stats.lock);
118 }
119}
120
121
122static inline void
123ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
124{
125 struct ip_vs_dest *dest = cp->dest;
126 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
127 spin_lock(&dest->stats.lock);
128 dest->stats.outpkts++;
129 dest->stats.outbytes += skb->len;
130 spin_unlock(&dest->stats.lock);
131
132 spin_lock(&dest->svc->stats.lock);
133 dest->svc->stats.outpkts++;
134 dest->svc->stats.outbytes += skb->len;
135 spin_unlock(&dest->svc->stats.lock);
136
137 spin_lock(&ip_vs_stats.lock);
138 ip_vs_stats.outpkts++;
139 ip_vs_stats.outbytes += skb->len;
140 spin_unlock(&ip_vs_stats.lock);
141 }
142}
143
144
145static inline void
146ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
147{
148 spin_lock(&cp->dest->stats.lock);
149 cp->dest->stats.conns++;
150 spin_unlock(&cp->dest->stats.lock);
151
152 spin_lock(&svc->stats.lock);
153 svc->stats.conns++;
154 spin_unlock(&svc->stats.lock);
155
156 spin_lock(&ip_vs_stats.lock);
157 ip_vs_stats.conns++;
158 spin_unlock(&ip_vs_stats.lock);
159}
160
161
162static inline int
163ip_vs_set_state(struct ip_vs_conn *cp, int direction,
164 const struct sk_buff *skb,
165 struct ip_vs_protocol *pp)
166{
167 if (unlikely(!pp->state_transition))
168 return 0;
169 return pp->state_transition(cp, direction, skb, pp);
170}
171
172
1da177e4
LT
173/*
174 * IPVS persistent scheduling function
175 * It creates a connection entry according to its template if exists,
176 * or selects a server and creates a connection entry plus a template.
177 * Locking: we are svc user (svc->refcnt), so we hold all dests too
178 * Protocols supported: TCP, UDP
179 */
180static struct ip_vs_conn *
181ip_vs_sched_persist(struct ip_vs_service *svc,
182 const struct sk_buff *skb,
014d730d 183 __be16 ports[2])
1da177e4
LT
184{
185 struct ip_vs_conn *cp = NULL;
28364a59 186 struct ip_vs_iphdr iph;
1da177e4
LT
187 struct ip_vs_dest *dest;
188 struct ip_vs_conn *ct;
cd17f9ed 189 __be16 dport; /* destination port to forward */
28364a59
JV
190 union nf_inet_addr snet; /* source network of the client,
191 after masking */
cd17f9ed
JV
192
193 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
1da177e4
LT
194
195 /* Mask saddr with the netmask to adjust template granularity */
cd17f9ed
JV
196#ifdef CONFIG_IP_VS_IPV6
197 if (svc->af == AF_INET6)
198 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
199 else
200#endif
201 snet.ip = iph.saddr.ip & svc->netmask;
1da177e4 202
cd17f9ed
JV
203 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
204 "mnet %s\n",
205 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(ports[0]),
206 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(ports[1]),
207 IP_VS_DBG_ADDR(svc->af, &snet));
1da177e4
LT
208
209 /*
210 * As far as we know, FTP is a very complicated network protocol, and
211 * it uses control connection and data connections. For active FTP,
212 * FTP server initialize data connection to the client, its source port
213 * is often 20. For passive FTP, FTP server tells the clients the port
214 * that it passively listens to, and the client issues the data
215 * connection. In the tunneling or direct routing mode, the load
216 * balancer is on the client-to-server half of connection, the port
217 * number is unknown to the load balancer. So, a conn template like
218 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
219 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
220 * is created for other persistent services.
221 */
222 if (ports[1] == svc->port) {
223 /* Check if a template already exists */
224 if (svc->port != FTPPORT)
cd17f9ed 225 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
28364a59 226 &iph.daddr, ports[1]);
1da177e4 227 else
cd17f9ed 228 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
28364a59 229 &iph.daddr, 0);
1da177e4
LT
230
231 if (!ct || !ip_vs_check_template(ct)) {
232 /*
233 * No template found or the dest of the connection
234 * template is not available.
235 */
236 dest = svc->scheduler->schedule(svc, skb);
237 if (dest == NULL) {
238 IP_VS_DBG(1, "p-schedule: no dest found.\n");
239 return NULL;
240 }
241
242 /*
243 * Create a template like <protocol,caddr,0,
244 * vaddr,vport,daddr,dport> for non-ftp service,
245 * and <protocol,caddr,0,vaddr,0,daddr,0>
246 * for ftp service.
247 */
248 if (svc->port != FTPPORT)
cd17f9ed 249 ct = ip_vs_conn_new(svc->af, iph.protocol,
28364a59
JV
250 &snet, 0,
251 &iph.daddr,
1da177e4 252 ports[1],
28364a59 253 &dest->addr, dest->port,
87375ab4 254 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
255 dest);
256 else
cd17f9ed 257 ct = ip_vs_conn_new(svc->af, iph.protocol,
28364a59
JV
258 &snet, 0,
259 &iph.daddr, 0,
260 &dest->addr, 0,
87375ab4 261 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
262 dest);
263 if (ct == NULL)
264 return NULL;
265
266 ct->timeout = svc->timeout;
267 } else {
268 /* set destination with the found template */
269 dest = ct->dest;
270 }
271 dport = dest->port;
272 } else {
273 /*
274 * Note: persistent fwmark-based services and persistent
275 * port zero service are handled here.
276 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
277 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
278 */
28364a59
JV
279 if (svc->fwmark) {
280 union nf_inet_addr fwmark = {
281 .all = { 0, 0, 0, htonl(svc->fwmark) }
282 };
283
cd17f9ed 284 ct = ip_vs_ct_in_get(svc->af, IPPROTO_IP, &snet, 0,
28364a59
JV
285 &fwmark, 0);
286 } else
cd17f9ed 287 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
28364a59 288 &iph.daddr, 0);
1da177e4
LT
289
290 if (!ct || !ip_vs_check_template(ct)) {
291 /*
292 * If it is not persistent port zero, return NULL,
293 * otherwise create a connection template.
294 */
295 if (svc->port)
296 return NULL;
297
298 dest = svc->scheduler->schedule(svc, skb);
299 if (dest == NULL) {
300 IP_VS_DBG(1, "p-schedule: no dest found.\n");
301 return NULL;
302 }
303
304 /*
305 * Create a template according to the service
306 */
28364a59
JV
307 if (svc->fwmark) {
308 union nf_inet_addr fwmark = {
309 .all = { 0, 0, 0, htonl(svc->fwmark) }
310 };
311
cd17f9ed 312 ct = ip_vs_conn_new(svc->af, IPPROTO_IP,
28364a59
JV
313 &snet, 0,
314 &fwmark, 0,
315 &dest->addr, 0,
87375ab4 316 IP_VS_CONN_F_TEMPLATE,
1da177e4 317 dest);
28364a59 318 } else
cd17f9ed 319 ct = ip_vs_conn_new(svc->af, iph.protocol,
28364a59
JV
320 &snet, 0,
321 &iph.daddr, 0,
322 &dest->addr, 0,
87375ab4 323 IP_VS_CONN_F_TEMPLATE,
1da177e4
LT
324 dest);
325 if (ct == NULL)
326 return NULL;
327
328 ct->timeout = svc->timeout;
329 } else {
330 /* set destination with the found template */
331 dest = ct->dest;
332 }
333 dport = ports[1];
334 }
335
336 /*
337 * Create a new connection according to the template
338 */
cd17f9ed 339 cp = ip_vs_conn_new(svc->af, iph.protocol,
28364a59
JV
340 &iph.saddr, ports[0],
341 &iph.daddr, ports[1],
342 &dest->addr, dport,
1da177e4
LT
343 0,
344 dest);
345 if (cp == NULL) {
346 ip_vs_conn_put(ct);
347 return NULL;
348 }
349
350 /*
351 * Add its control
352 */
353 ip_vs_control_add(cp, ct);
354 ip_vs_conn_put(ct);
355
356 ip_vs_conn_stats(cp, svc);
357 return cp;
358}
359
360
361/*
362 * IPVS main scheduling function
363 * It selects a server according to the virtual service, and
364 * creates a connection entry.
365 * Protocols supported: TCP, UDP
366 */
367struct ip_vs_conn *
368ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
369{
370 struct ip_vs_conn *cp = NULL;
28364a59 371 struct ip_vs_iphdr iph;
1da177e4 372 struct ip_vs_dest *dest;
014d730d 373 __be16 _ports[2], *pptr;
1da177e4 374
28364a59
JV
375 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
376 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
1da177e4
LT
377 if (pptr == NULL)
378 return NULL;
379
380 /*
381 * Persistent service
382 */
383 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
384 return ip_vs_sched_persist(svc, skb, pptr);
385
386 /*
387 * Non-persistent service
388 */
389 if (!svc->fwmark && pptr[1] != svc->port) {
390 if (!svc->port)
391 IP_VS_ERR("Schedule: port zero only supported "
392 "in persistent services, "
393 "check your ipvs configuration\n");
394 return NULL;
395 }
396
397 dest = svc->scheduler->schedule(svc, skb);
398 if (dest == NULL) {
399 IP_VS_DBG(1, "Schedule: no dest found.\n");
400 return NULL;
401 }
402
403 /*
404 * Create a connection entry.
405 */
cd17f9ed 406 cp = ip_vs_conn_new(svc->af, iph.protocol,
28364a59
JV
407 &iph.saddr, pptr[0],
408 &iph.daddr, pptr[1],
409 &dest->addr, dest->port ? dest->port : pptr[1],
1da177e4
LT
410 0,
411 dest);
412 if (cp == NULL)
413 return NULL;
414
cd17f9ed
JV
415 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
416 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
417 ip_vs_fwd_tag(cp),
418 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
419 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
420 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
421 cp->flags, atomic_read(&cp->refcnt));
1da177e4
LT
422
423 ip_vs_conn_stats(cp, svc);
424 return cp;
425}
426
427
428/*
429 * Pass or drop the packet.
430 * Called by ip_vs_in, when the virtual service is available but
431 * no destination is available for a new connection.
432 */
433int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
434 struct ip_vs_protocol *pp)
435{
014d730d 436 __be16 _ports[2], *pptr;
28364a59 437 struct ip_vs_iphdr iph;
2a3b791e
JV
438 int unicast;
439 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
1da177e4 440
28364a59 441 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
1da177e4
LT
442 if (pptr == NULL) {
443 ip_vs_service_put(svc);
444 return NF_DROP;
445 }
446
2a3b791e
JV
447#ifdef CONFIG_IP_VS_IPV6
448 if (svc->af == AF_INET6)
449 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
450 else
451#endif
452 unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
453
1da177e4 454 /* if it is fwmark-based service, the cache_bypass sysctl is up
2a3b791e 455 and the destination is a non-local unicast, then create
1da177e4 456 a cache_bypass connection entry */
2a3b791e 457 if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
1da177e4
LT
458 int ret, cs;
459 struct ip_vs_conn *cp;
460
461 ip_vs_service_put(svc);
462
463 /* create a new connection entry */
464 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
2a3b791e 465 cp = ip_vs_conn_new(svc->af, iph.protocol,
28364a59
JV
466 &iph.saddr, pptr[0],
467 &iph.daddr, pptr[1],
1da177e4
LT
468 0, 0,
469 IP_VS_CONN_F_BYPASS,
470 NULL);
471 if (cp == NULL)
472 return NF_DROP;
473
474 /* statistics */
475 ip_vs_in_stats(cp, skb);
476
477 /* set state */
478 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
479
480 /* transmit the first SYN packet */
481 ret = cp->packet_xmit(skb, cp, pp);
482 /* do not touch skb anymore */
483
484 atomic_inc(&cp->in_pkts);
485 ip_vs_conn_put(cp);
486 return ret;
487 }
488
489 /*
490 * When the virtual ftp service is presented, packets destined
491 * for other services on the VIP may get here (except services
492 * listed in the ipvs table), pass the packets, because it is
493 * not ipvs job to decide to drop the packets.
494 */
495 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
496 ip_vs_service_put(svc);
497 return NF_ACCEPT;
498 }
499
500 ip_vs_service_put(svc);
501
502 /*
503 * Notify the client that the destination is unreachable, and
504 * release the socket buffer.
505 * Since it is in IP layer, the TCP socket is not actually
506 * created, the TCP RST packet cannot be sent, instead that
507 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
508 */
2a3b791e
JV
509#ifdef CONFIG_IP_VS_IPV6
510 if (svc->af == AF_INET6)
511 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0,
512 skb->dev);
513 else
514#endif
515 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
516
1da177e4
LT
517 return NF_DROP;
518}
519
520
521/*
6e23ae2a 522 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
1da177e4
LT
523 * chain, and is used for VS/NAT.
524 * It detects packets for VS/NAT connections and sends the packets
525 * immediately. This can avoid that iptable_nat mangles the packets
526 * for VS/NAT.
527 */
528static unsigned int ip_vs_post_routing(unsigned int hooknum,
3db05fea 529 struct sk_buff *skb,
1da177e4
LT
530 const struct net_device *in,
531 const struct net_device *out,
532 int (*okfn)(struct sk_buff *))
533{
3db05fea 534 if (!skb->ipvs_property)
1da177e4 535 return NF_ACCEPT;
1da177e4 536 /* The packet was sent from IPVS, exit this chain */
abbcc739 537 return NF_STOP;
1da177e4
LT
538}
539
b1550f22 540__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
1da177e4 541{
d3bc23e7 542 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
1da177e4
LT
543}
544
776c729e 545static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
1da177e4 546{
776c729e
HX
547 int err = ip_defrag(skb, user);
548
549 if (!err)
eddc9ec5 550 ip_send_check(ip_hdr(skb));
776c729e
HX
551
552 return err;
1da177e4
LT
553}
554
2a3b791e
JV
555#ifdef CONFIG_IP_VS_IPV6
556static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
557{
558 /* TODO IPv6: Find out what to do here for IPv6 */
559 return 0;
560}
561#endif
562
1da177e4
LT
563/*
564 * Packet has been made sufficiently writable in caller
565 * - inout: 1=in->out, 0=out->in
566 */
567void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
568 struct ip_vs_conn *cp, int inout)
569{
eddc9ec5 570 struct iphdr *iph = ip_hdr(skb);
1da177e4 571 unsigned int icmp_offset = iph->ihl*4;
d56f90a7
ACM
572 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
573 icmp_offset);
1da177e4
LT
574 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
575
576 if (inout) {
e7ade46a 577 iph->saddr = cp->vaddr.ip;
1da177e4 578 ip_send_check(iph);
e7ade46a 579 ciph->daddr = cp->vaddr.ip;
1da177e4
LT
580 ip_send_check(ciph);
581 } else {
e7ade46a 582 iph->daddr = cp->daddr.ip;
1da177e4 583 ip_send_check(iph);
e7ade46a 584 ciph->saddr = cp->daddr.ip;
1da177e4
LT
585 ip_send_check(ciph);
586 }
587
588 /* the TCP/UDP port */
589 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
014d730d 590 __be16 *ports = (void *)ciph + ciph->ihl*4;
1da177e4
LT
591
592 if (inout)
593 ports[1] = cp->vport;
594 else
595 ports[0] = cp->dport;
596 }
597
598 /* And finally the ICMP checksum */
599 icmph->checksum = 0;
600 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
601 skb->ip_summed = CHECKSUM_UNNECESSARY;
602
603 if (inout)
604 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
605 "Forwarding altered outgoing ICMP");
606 else
607 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
608 "Forwarding altered incoming ICMP");
609}
610
b3cdd2a7
JV
611#ifdef CONFIG_IP_VS_IPV6
612void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
613 struct ip_vs_conn *cp, int inout)
614{
615 struct ipv6hdr *iph = ipv6_hdr(skb);
616 unsigned int icmp_offset = sizeof(struct ipv6hdr);
617 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
618 icmp_offset);
619 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
620
621 if (inout) {
622 iph->saddr = cp->vaddr.in6;
623 ciph->daddr = cp->vaddr.in6;
624 } else {
625 iph->daddr = cp->daddr.in6;
626 ciph->saddr = cp->daddr.in6;
627 }
628
629 /* the TCP/UDP port */
630 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr) {
631 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
632
633 if (inout)
634 ports[1] = cp->vport;
635 else
636 ports[0] = cp->dport;
637 }
638
639 /* And finally the ICMP checksum */
640 icmph->icmp6_cksum = 0;
641 /* TODO IPv6: is this correct for ICMPv6? */
642 ip_vs_checksum_complete(skb, icmp_offset);
643 skb->ip_summed = CHECKSUM_UNNECESSARY;
644
645 if (inout)
646 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
647 "Forwarding altered outgoing ICMPv6");
648 else
649 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
650 "Forwarding altered incoming ICMPv6");
651}
652#endif
653
4856c84c
MT
654/* Handle relevant response ICMP messages - forward to the right
655 * destination host. Used for NAT and local client.
656 */
f2428ed5
SH
657static int handle_response_icmp(int af, struct sk_buff *skb,
658 union nf_inet_addr *snet,
659 __u8 protocol, struct ip_vs_conn *cp,
4856c84c
MT
660 struct ip_vs_protocol *pp,
661 unsigned int offset, unsigned int ihl)
662{
663 unsigned int verdict = NF_DROP;
664
665 if (IP_VS_FWD_METHOD(cp) != 0) {
666 IP_VS_ERR("shouldn't reach here, because the box is on the "
667 "half connection in the tun/dr module.\n");
668 }
669
670 /* Ensure the checksum is correct */
671 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
672 /* Failed checksum! */
f2428ed5
SH
673 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
674 IP_VS_DBG_ADDR(af, snet));
4856c84c
MT
675 goto out;
676 }
677
f2428ed5 678 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol)
4856c84c
MT
679 offset += 2 * sizeof(__u16);
680 if (!skb_make_writable(skb, offset))
681 goto out;
682
f2428ed5
SH
683#ifdef CONFIG_IP_VS_IPV6
684 if (af == AF_INET6)
685 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
686 else
687#endif
688 ip_vs_nat_icmp(skb, pp, cp, 1);
4856c84c
MT
689
690 /* do the statistics and put it back */
691 ip_vs_out_stats(cp, skb);
692
693 skb->ipvs_property = 1;
694 verdict = NF_ACCEPT;
695
696out:
697 __ip_vs_conn_put(cp);
698
699 return verdict;
700}
701
1da177e4
LT
702/*
703 * Handle ICMP messages in the inside-to-outside direction (outgoing).
4856c84c 704 * Find any that might be relevant, check against existing connections.
1da177e4 705 * Currently handles error types - unreachable, quench, ttl exceeded.
1da177e4 706 */
3db05fea 707static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
1da177e4 708{
1da177e4
LT
709 struct iphdr *iph;
710 struct icmphdr _icmph, *ic;
711 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 712 struct ip_vs_iphdr ciph;
1da177e4
LT
713 struct ip_vs_conn *cp;
714 struct ip_vs_protocol *pp;
4856c84c 715 unsigned int offset, ihl;
f2428ed5 716 union nf_inet_addr snet;
1da177e4
LT
717
718 *related = 1;
719
720 /* reassemble IP fragments */
eddc9ec5 721 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
776c729e 722 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
1da177e4 723 return NF_STOLEN;
1da177e4
LT
724 }
725
eddc9ec5 726 iph = ip_hdr(skb);
1da177e4
LT
727 offset = ihl = iph->ihl * 4;
728 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
729 if (ic == NULL)
730 return NF_DROP;
731
732 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
733 ic->type, ntohs(icmp_id(ic)),
734 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
735
736 /*
737 * Work through seeing if this is for us.
738 * These checks are supposed to be in an order that means easy
739 * things are checked first to speed up processing.... however
740 * this means that some packets will manage to get a long way
741 * down this stack and then be rejected, but that's life.
742 */
743 if ((ic->type != ICMP_DEST_UNREACH) &&
744 (ic->type != ICMP_SOURCE_QUENCH) &&
745 (ic->type != ICMP_TIME_EXCEEDED)) {
746 *related = 0;
747 return NF_ACCEPT;
748 }
749
750 /* Now find the contained IP header */
751 offset += sizeof(_icmph);
752 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
753 if (cih == NULL)
754 return NF_ACCEPT; /* The packet looks wrong, ignore */
755
756 pp = ip_vs_proto_get(cih->protocol);
757 if (!pp)
758 return NF_ACCEPT;
759
760 /* Is the embedded protocol header present? */
4412ec49 761 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
762 pp->dont_defrag))
763 return NF_ACCEPT;
764
765 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
766
767 offset += cih->ihl * 4;
768
51ef348b 769 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 770 /* The embedded headers contain source and dest in reverse order */
51ef348b 771 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
1da177e4
LT
772 if (!cp)
773 return NF_ACCEPT;
774
f2428ed5
SH
775 snet.ip = iph->saddr;
776 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
777 pp, offset, ihl);
1da177e4
LT
778}
779
2a3b791e
JV
780#ifdef CONFIG_IP_VS_IPV6
781static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
782{
783 struct ipv6hdr *iph;
784 struct icmp6hdr _icmph, *ic;
785 struct ipv6hdr _ciph, *cih; /* The ip header contained
786 within the ICMP */
787 struct ip_vs_iphdr ciph;
788 struct ip_vs_conn *cp;
789 struct ip_vs_protocol *pp;
f2428ed5
SH
790 unsigned int offset;
791 union nf_inet_addr snet;
2a3b791e
JV
792
793 *related = 1;
794
795 /* reassemble IP fragments */
796 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
797 if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
798 return NF_STOLEN;
799 }
800
801 iph = ipv6_hdr(skb);
802 offset = sizeof(struct ipv6hdr);
803 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
804 if (ic == NULL)
805 return NF_DROP;
806
807 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
808 ic->icmp6_type, ntohs(icmpv6_id(ic)),
809 NIP6(iph->saddr), NIP6(iph->daddr));
810
811 /*
812 * Work through seeing if this is for us.
813 * These checks are supposed to be in an order that means easy
814 * things are checked first to speed up processing.... however
815 * this means that some packets will manage to get a long way
816 * down this stack and then be rejected, but that's life.
817 */
818 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
819 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
820 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
821 *related = 0;
822 return NF_ACCEPT;
823 }
824
825 /* Now find the contained IP header */
826 offset += sizeof(_icmph);
827 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
828 if (cih == NULL)
829 return NF_ACCEPT; /* The packet looks wrong, ignore */
830
831 pp = ip_vs_proto_get(cih->nexthdr);
832 if (!pp)
833 return NF_ACCEPT;
834
835 /* Is the embedded protocol header present? */
836 /* TODO: we don't support fragmentation at the moment anyways */
837 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
838 return NF_ACCEPT;
839
840 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
841
842 offset += sizeof(struct ipv6hdr);
843
844 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
845 /* The embedded headers contain source and dest in reverse order */
846 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
847 if (!cp)
848 return NF_ACCEPT;
849
178f5e49 850 ipv6_addr_copy(&snet.in6, &iph->saddr);
f2428ed5
SH
851 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
852 pp, offset, sizeof(struct ipv6hdr));
2a3b791e
JV
853}
854#endif
855
856static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1da177e4
LT
857{
858 struct tcphdr _tcph, *th;
859
2a3b791e 860 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1da177e4
LT
861 if (th == NULL)
862 return 0;
863 return th->rst;
864}
865
4856c84c
MT
866/* Handle response packets: rewrite addresses and send away...
867 * Used for NAT and local client.
868 */
869static unsigned int
870handle_response(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
871 struct ip_vs_conn *cp, int ihl)
872{
873 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
874
875 if (!skb_make_writable(skb, ihl))
876 goto drop;
877
878 /* mangle the packet */
879 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
880 goto drop;
881
882#ifdef CONFIG_IP_VS_IPV6
883 if (af == AF_INET6)
884 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
885 else
886#endif
887 {
888 ip_hdr(skb)->saddr = cp->vaddr.ip;
889 ip_send_check(ip_hdr(skb));
890 }
891
892 /* For policy routing, packets originating from this
893 * machine itself may be routed differently to packets
894 * passing through. We want this packet to be routed as
895 * if it came from this machine itself. So re-compute
896 * the routing information.
897 */
898#ifdef CONFIG_IP_VS_IPV6
899 if (af == AF_INET6) {
900 if (ip6_route_me_harder(skb) != 0)
901 goto drop;
902 } else
903#endif
904 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
905 goto drop;
906
4856c84c
MT
907 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
908
909 ip_vs_out_stats(cp, skb);
910 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
911 ip_vs_conn_put(cp);
912
913 skb->ipvs_property = 1;
914
915 LeaveFunction(11);
916 return NF_ACCEPT;
917
918drop:
919 ip_vs_conn_put(cp);
920 kfree_skb(skb);
921 return NF_STOLEN;
922}
923
1da177e4 924/*
6e23ae2a 925 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
4856c84c 926 * Check if outgoing packet belongs to the established ip_vs_conn.
1da177e4
LT
927 */
928static unsigned int
3db05fea 929ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
930 const struct net_device *in, const struct net_device *out,
931 int (*okfn)(struct sk_buff *))
932{
51ef348b 933 struct ip_vs_iphdr iph;
1da177e4
LT
934 struct ip_vs_protocol *pp;
935 struct ip_vs_conn *cp;
2a3b791e 936 int af;
1da177e4
LT
937
938 EnterFunction(11);
939
2a3b791e
JV
940 af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
941
6869c4d8 942 if (skb->ipvs_property)
1da177e4
LT
943 return NF_ACCEPT;
944
2a3b791e
JV
945 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
946#ifdef CONFIG_IP_VS_IPV6
947 if (af == AF_INET6) {
948 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
949 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
1da177e4 950
2a3b791e
JV
951 if (related)
952 return verdict;
953 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
954 }
955 } else
956#endif
957 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
958 int related, verdict = ip_vs_out_icmp(skb, &related);
959
960 if (related)
961 return verdict;
962 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
963 }
1da177e4 964
51ef348b 965 pp = ip_vs_proto_get(iph.protocol);
1da177e4
LT
966 if (unlikely(!pp))
967 return NF_ACCEPT;
968
969 /* reassemble IP fragments */
2a3b791e
JV
970#ifdef CONFIG_IP_VS_IPV6
971 if (af == AF_INET6) {
972 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
973 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
1da177e4 974
2a3b791e
JV
975 if (related)
976 return verdict;
977
978 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
979 }
980 } else
981#endif
982 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
983 !pp->dont_defrag)) {
984 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
985 return NF_STOLEN;
986
987 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
988 }
1da177e4
LT
989
990 /*
991 * Check if the packet belongs to an existing entry
992 */
2a3b791e 993 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1da177e4
LT
994
995 if (unlikely(!cp)) {
996 if (sysctl_ip_vs_nat_icmp_send &&
997 (pp->protocol == IPPROTO_TCP ||
998 pp->protocol == IPPROTO_UDP)) {
014d730d 999 __be16 _ports[2], *pptr;
1da177e4 1000
51ef348b 1001 pptr = skb_header_pointer(skb, iph.len,
1da177e4
LT
1002 sizeof(_ports), _ports);
1003 if (pptr == NULL)
1004 return NF_ACCEPT; /* Not for me */
7937df15
JV
1005 if (ip_vs_lookup_real_service(af, iph.protocol,
1006 &iph.saddr,
2a3b791e 1007 pptr[0])) {
1da177e4
LT
1008 /*
1009 * Notify the real server: there is no
1010 * existing entry if it is not RST
1011 * packet or not TCP packet.
1012 */
51ef348b 1013 if (iph.protocol != IPPROTO_TCP
2a3b791e
JV
1014 || !is_tcp_reset(skb, iph.len)) {
1015#ifdef CONFIG_IP_VS_IPV6
1016 if (af == AF_INET6)
1017 icmpv6_send(skb,
1018 ICMPV6_DEST_UNREACH,
1019 ICMPV6_PORT_UNREACH,
1020 0, skb->dev);
1021 else
1022#endif
1023 icmp_send(skb,
1024 ICMP_DEST_UNREACH,
1025 ICMP_PORT_UNREACH, 0);
1da177e4 1026 return NF_DROP;
5af149cc 1027 }
1da177e4
LT
1028 }
1029 }
1030 IP_VS_DBG_PKT(12, pp, skb, 0,
1031 "packet continues traversal as normal");
1032 return NF_ACCEPT;
1033 }
1034
4856c84c 1035 return handle_response(af, skb, pp, cp, iph.len);
1da177e4
LT
1036}
1037
1038
1039/*
1040 * Handle ICMP messages in the outside-to-inside direction (incoming).
1041 * Find any that might be relevant, check against existing connections,
1042 * forward to the right destination host if relevant.
1043 * Currently handles error types - unreachable, quench, ttl exceeded.
1044 */
e905a9ed 1045static int
3db05fea 1046ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1da177e4 1047{
1da177e4
LT
1048 struct iphdr *iph;
1049 struct icmphdr _icmph, *ic;
1050 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 1051 struct ip_vs_iphdr ciph;
1da177e4
LT
1052 struct ip_vs_conn *cp;
1053 struct ip_vs_protocol *pp;
1054 unsigned int offset, ihl, verdict;
f2428ed5 1055 union nf_inet_addr snet;
1da177e4
LT
1056
1057 *related = 1;
1058
1059 /* reassemble IP fragments */
eddc9ec5 1060 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
6e23ae2a 1061 if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
776c729e 1062 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
1da177e4 1063 return NF_STOLEN;
1da177e4
LT
1064 }
1065
eddc9ec5 1066 iph = ip_hdr(skb);
1da177e4
LT
1067 offset = ihl = iph->ihl * 4;
1068 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1069 if (ic == NULL)
1070 return NF_DROP;
1071
1072 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
1073 ic->type, ntohs(icmp_id(ic)),
1074 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1075
1076 /*
1077 * Work through seeing if this is for us.
1078 * These checks are supposed to be in an order that means easy
1079 * things are checked first to speed up processing.... however
1080 * this means that some packets will manage to get a long way
1081 * down this stack and then be rejected, but that's life.
1082 */
1083 if ((ic->type != ICMP_DEST_UNREACH) &&
1084 (ic->type != ICMP_SOURCE_QUENCH) &&
1085 (ic->type != ICMP_TIME_EXCEEDED)) {
1086 *related = 0;
1087 return NF_ACCEPT;
1088 }
1089
1090 /* Now find the contained IP header */
1091 offset += sizeof(_icmph);
1092 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1093 if (cih == NULL)
1094 return NF_ACCEPT; /* The packet looks wrong, ignore */
1095
1096 pp = ip_vs_proto_get(cih->protocol);
1097 if (!pp)
1098 return NF_ACCEPT;
1099
1100 /* Is the embedded protocol header present? */
4412ec49 1101 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
1102 pp->dont_defrag))
1103 return NF_ACCEPT;
1104
1105 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
1106
1107 offset += cih->ihl * 4;
1108
51ef348b 1109 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 1110 /* The embedded headers contain source and dest in reverse order */
51ef348b 1111 cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
4856c84c
MT
1112 if (!cp) {
1113 /* The packet could also belong to a local client */
1114 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
f2428ed5
SH
1115 if (cp) {
1116 snet.ip = iph->saddr;
1117 return handle_response_icmp(AF_INET, skb, &snet,
1118 cih->protocol, cp, pp,
4856c84c 1119 offset, ihl);
f2428ed5 1120 }
1da177e4 1121 return NF_ACCEPT;
4856c84c 1122 }
1da177e4
LT
1123
1124 verdict = NF_DROP;
1125
1126 /* Ensure the checksum is correct */
60476372 1127 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4
LT
1128 /* Failed checksum! */
1129 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
1130 NIPQUAD(iph->saddr));
1131 goto out;
1132 }
1133
1134 /* do the statistics and put it back */
1135 ip_vs_in_stats(cp, skb);
1136 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1137 offset += 2 * sizeof(__u16);
1138 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1139 /* do not touch skb anymore */
1140
1141 out:
1142 __ip_vs_conn_put(cp);
1143
1144 return verdict;
1145}
1146
2a3b791e
JV
1147#ifdef CONFIG_IP_VS_IPV6
1148static int
1149ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1150{
1151 struct ipv6hdr *iph;
1152 struct icmp6hdr _icmph, *ic;
1153 struct ipv6hdr _ciph, *cih; /* The ip header contained
1154 within the ICMP */
1155 struct ip_vs_iphdr ciph;
1156 struct ip_vs_conn *cp;
1157 struct ip_vs_protocol *pp;
1158 unsigned int offset, verdict;
f2428ed5 1159 union nf_inet_addr snet;
2a3b791e
JV
1160
1161 *related = 1;
1162
1163 /* reassemble IP fragments */
1164 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1165 if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
1166 IP_DEFRAG_VS_IN :
1167 IP_DEFRAG_VS_FWD))
1168 return NF_STOLEN;
1169 }
1170
1171 iph = ipv6_hdr(skb);
1172 offset = sizeof(struct ipv6hdr);
1173 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1174 if (ic == NULL)
1175 return NF_DROP;
1176
1177 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
1178 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1179 NIP6(iph->saddr), NIP6(iph->daddr));
1180
1181 /*
1182 * Work through seeing if this is for us.
1183 * These checks are supposed to be in an order that means easy
1184 * things are checked first to speed up processing.... however
1185 * this means that some packets will manage to get a long way
1186 * down this stack and then be rejected, but that's life.
1187 */
1188 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1189 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1190 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1191 *related = 0;
1192 return NF_ACCEPT;
1193 }
1194
1195 /* Now find the contained IP header */
1196 offset += sizeof(_icmph);
1197 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1198 if (cih == NULL)
1199 return NF_ACCEPT; /* The packet looks wrong, ignore */
1200
1201 pp = ip_vs_proto_get(cih->nexthdr);
1202 if (!pp)
1203 return NF_ACCEPT;
1204
1205 /* Is the embedded protocol header present? */
1206 /* TODO: we don't support fragmentation at the moment anyways */
1207 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1208 return NF_ACCEPT;
1209
1210 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
1211
1212 offset += sizeof(struct ipv6hdr);
1213
1214 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1215 /* The embedded headers contain source and dest in reverse order */
1216 cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
f2428ed5
SH
1217 if (!cp) {
1218 /* The packet could also belong to a local client */
1219 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
1220 if (cp) {
178f5e49 1221 ipv6_addr_copy(&snet.in6, &iph->saddr);
f2428ed5
SH
1222 return handle_response_icmp(AF_INET6, skb, &snet,
1223 cih->nexthdr,
1224 cp, pp, offset,
1225 sizeof(struct ipv6hdr));
1226 }
2a3b791e 1227 return NF_ACCEPT;
f2428ed5 1228 }
2a3b791e
JV
1229
1230 verdict = NF_DROP;
1231
1232 /* do the statistics and put it back */
1233 ip_vs_in_stats(cp, skb);
1234 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
1235 offset += 2 * sizeof(__u16);
1236 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1237 /* do not touch skb anymore */
1238
1239 __ip_vs_conn_put(cp);
1240
1241 return verdict;
1242}
1243#endif
1244
1245
1da177e4
LT
1246/*
1247 * Check if it's for virtual services, look it up,
1248 * and send it on its way...
1249 */
1250static unsigned int
3db05fea 1251ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1252 const struct net_device *in, const struct net_device *out,
1253 int (*okfn)(struct sk_buff *))
1254{
51ef348b 1255 struct ip_vs_iphdr iph;
1da177e4
LT
1256 struct ip_vs_protocol *pp;
1257 struct ip_vs_conn *cp;
2a3b791e
JV
1258 int ret, restart, af;
1259
1260 af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
51ef348b 1261
2a3b791e 1262 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1da177e4
LT
1263
1264 /*
4856c84c
MT
1265 * Big tappo: only PACKET_HOST, including loopback for local client
1266 * Don't handle local packets on IPv6 for now
1da177e4 1267 */
f2428ed5 1268 if (unlikely(skb->pkt_type != PACKET_HOST)) {
51ef348b
JV
1269 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
1270 skb->pkt_type,
1271 iph.protocol,
2a3b791e 1272 IP_VS_DBG_ADDR(af, &iph.daddr));
1da177e4
LT
1273 return NF_ACCEPT;
1274 }
1275
51ef348b 1276 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
3db05fea 1277 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
1da177e4
LT
1278
1279 if (related)
1280 return verdict;
2a3b791e 1281 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1da177e4
LT
1282 }
1283
1284 /* Protocol supported? */
51ef348b 1285 pp = ip_vs_proto_get(iph.protocol);
1da177e4
LT
1286 if (unlikely(!pp))
1287 return NF_ACCEPT;
1288
1da177e4
LT
1289 /*
1290 * Check if the packet belongs to an existing connection entry
1291 */
2a3b791e 1292 cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
1da177e4
LT
1293
1294 if (unlikely(!cp)) {
1295 int v;
1296
4856c84c
MT
1297 /* For local client packets, it could be a response */
1298 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1299 if (cp)
1300 return handle_response(af, skb, pp, cp, iph.len);
1301
2a3b791e 1302 if (!pp->conn_schedule(af, skb, pp, &v, &cp))
1da177e4
LT
1303 return v;
1304 }
1305
1306 if (unlikely(!cp)) {
1307 /* sorry, all this trouble for a no-hit :) */
1308 IP_VS_DBG_PKT(12, pp, skb, 0,
1309 "packet continues traversal as normal");
1310 return NF_ACCEPT;
1311 }
1312
1313 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1314
1315 /* Check the server status */
1316 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1317 /* the destination server is not available */
1318
1319 if (sysctl_ip_vs_expire_nodest_conn) {
1320 /* try to expire the connection immediately */
1321 ip_vs_conn_expire_now(cp);
1da177e4 1322 }
dc8103f2
JA
1323 /* don't restart its timer, and silently
1324 drop the packet. */
1325 __ip_vs_conn_put(cp);
1da177e4
LT
1326 return NF_DROP;
1327 }
1328
1329 ip_vs_in_stats(cp, skb);
1330 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1331 if (cp->packet_xmit)
1332 ret = cp->packet_xmit(skb, cp, pp);
1333 /* do not touch skb anymore */
1334 else {
1335 IP_VS_DBG_RL("warning: packet_xmit is null");
1336 ret = NF_ACCEPT;
1337 }
1338
efac5276
RB
1339 /* Increase its packet counter and check if it is needed
1340 * to be synchronized
1341 *
1342 * Sync connection if it is about to close to
1343 * encorage the standby servers to update the connections timeout
1344 */
1da177e4 1345 atomic_inc(&cp->in_pkts);
c6883f58
JV
1346 if (af == AF_INET &&
1347 (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
efac5276
RB
1348 (((cp->protocol != IPPROTO_TCP ||
1349 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1350 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
1351 == sysctl_ip_vs_sync_threshold[0])) ||
1352 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1353 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
9d3a0de7
RB
1354 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1355 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1da177e4 1356 ip_vs_sync_conn(cp);
efac5276 1357 cp->old_state = cp->state;
1da177e4
LT
1358
1359 ip_vs_conn_put(cp);
1360 return ret;
1361}
1362
1363
1364/*
6e23ae2a 1365 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1da177e4
LT
1366 * related packets destined for 0.0.0.0/0.
1367 * When fwmark-based virtual service is used, such as transparent
1368 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1369 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
6e23ae2a 1370 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1da177e4
LT
1371 * and send them to ip_vs_in_icmp.
1372 */
1373static unsigned int
3db05fea 1374ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1375 const struct net_device *in, const struct net_device *out,
1376 int (*okfn)(struct sk_buff *))
1377{
1378 int r;
1379
3db05fea 1380 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
1381 return NF_ACCEPT;
1382
3db05fea 1383 return ip_vs_in_icmp(skb, &r, hooknum);
1da177e4
LT
1384}
1385
2a3b791e
JV
1386#ifdef CONFIG_IP_VS_IPV6
1387static unsigned int
1388ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1389 const struct net_device *in, const struct net_device *out,
1390 int (*okfn)(struct sk_buff *))
1391{
1392 int r;
1393
1394 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1395 return NF_ACCEPT;
1396
1397 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1398}
1399#endif
1400
1da177e4 1401
1999414a 1402static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
41c5b317
PM
1403 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1404 * or VS/NAT(change destination), so that filtering rules can be
1405 * applied to IPVS. */
1406 {
1407 .hook = ip_vs_in,
1408 .owner = THIS_MODULE,
1409 .pf = PF_INET,
1410 .hooknum = NF_INET_LOCAL_IN,
1411 .priority = 100,
1412 },
1413 /* After packet filtering, change source only for VS/NAT */
1414 {
1415 .hook = ip_vs_out,
1416 .owner = THIS_MODULE,
1417 .pf = PF_INET,
1418 .hooknum = NF_INET_FORWARD,
1419 .priority = 100,
1420 },
1421 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1422 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1423 {
1424 .hook = ip_vs_forward_icmp,
1425 .owner = THIS_MODULE,
1426 .pf = PF_INET,
1427 .hooknum = NF_INET_FORWARD,
1428 .priority = 99,
1429 },
1430 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1431 {
1432 .hook = ip_vs_post_routing,
1433 .owner = THIS_MODULE,
1434 .pf = PF_INET,
1435 .hooknum = NF_INET_POST_ROUTING,
1436 .priority = NF_IP_PRI_NAT_SRC-1,
1437 },
473b23d3
JV
1438#ifdef CONFIG_IP_VS_IPV6
1439 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1440 * or VS/NAT(change destination), so that filtering rules can be
1441 * applied to IPVS. */
1442 {
1443 .hook = ip_vs_in,
1444 .owner = THIS_MODULE,
1445 .pf = PF_INET6,
1446 .hooknum = NF_INET_LOCAL_IN,
1447 .priority = 100,
1448 },
1449 /* After packet filtering, change source only for VS/NAT */
1450 {
1451 .hook = ip_vs_out,
1452 .owner = THIS_MODULE,
1453 .pf = PF_INET6,
1454 .hooknum = NF_INET_FORWARD,
1455 .priority = 100,
1456 },
1457 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1458 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1459 {
1460 .hook = ip_vs_forward_icmp_v6,
1461 .owner = THIS_MODULE,
1462 .pf = PF_INET6,
1463 .hooknum = NF_INET_FORWARD,
1464 .priority = 99,
1465 },
1466 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1467 {
1468 .hook = ip_vs_post_routing,
1469 .owner = THIS_MODULE,
1470 .pf = PF_INET6,
1471 .hooknum = NF_INET_POST_ROUTING,
1472 .priority = NF_IP6_PRI_NAT_SRC-1,
1473 },
1474#endif
1da177e4
LT
1475};
1476
1477
1478/*
1479 * Initialize IP Virtual Server
1480 */
1481static int __init ip_vs_init(void)
1482{
1483 int ret;
1484
a919cf4b
SW
1485 ip_vs_estimator_init();
1486
1da177e4
LT
1487 ret = ip_vs_control_init();
1488 if (ret < 0) {
1489 IP_VS_ERR("can't setup control.\n");
a919cf4b 1490 goto cleanup_estimator;
1da177e4
LT
1491 }
1492
1493 ip_vs_protocol_init();
1494
1495 ret = ip_vs_app_init();
1496 if (ret < 0) {
1497 IP_VS_ERR("can't setup application helper.\n");
1498 goto cleanup_protocol;
1499 }
1500
1501 ret = ip_vs_conn_init();
1502 if (ret < 0) {
1503 IP_VS_ERR("can't setup connection table.\n");
1504 goto cleanup_app;
1505 }
1506
41c5b317 1507 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4 1508 if (ret < 0) {
41c5b317 1509 IP_VS_ERR("can't register hooks.\n");
1da177e4
LT
1510 goto cleanup_conn;
1511 }
1512
1da177e4
LT
1513 IP_VS_INFO("ipvs loaded.\n");
1514 return ret;
1515
1da177e4
LT
1516 cleanup_conn:
1517 ip_vs_conn_cleanup();
1518 cleanup_app:
1519 ip_vs_app_cleanup();
1520 cleanup_protocol:
1521 ip_vs_protocol_cleanup();
1522 ip_vs_control_cleanup();
a919cf4b
SW
1523 cleanup_estimator:
1524 ip_vs_estimator_cleanup();
1da177e4
LT
1525 return ret;
1526}
1527
1528static void __exit ip_vs_cleanup(void)
1529{
41c5b317 1530 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4
LT
1531 ip_vs_conn_cleanup();
1532 ip_vs_app_cleanup();
1533 ip_vs_protocol_cleanup();
1534 ip_vs_control_cleanup();
a919cf4b 1535 ip_vs_estimator_cleanup();
1da177e4
LT
1536 IP_VS_INFO("ipvs unloaded.\n");
1537}
1538
1539module_init(ip_vs_init);
1540module_exit(ip_vs_cleanup);
1541MODULE_LICENSE("GPL");