ipvs: load balance IPv4 connections from a local process
[deliverable/linux.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 */
657static int handle_response_icmp(struct sk_buff *skb, struct iphdr *iph,
658 struct iphdr *cih, struct ip_vs_conn *cp,
659 struct ip_vs_protocol *pp,
660 unsigned int offset, unsigned int ihl)
661{
662 unsigned int verdict = NF_DROP;
663
664 if (IP_VS_FWD_METHOD(cp) != 0) {
665 IP_VS_ERR("shouldn't reach here, because the box is on the "
666 "half connection in the tun/dr module.\n");
667 }
668
669 /* Ensure the checksum is correct */
670 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
671 /* Failed checksum! */
672 IP_VS_DBG(1,
673 "Forward ICMP: failed checksum from %d.%d.%d.%d!\n",
674 NIPQUAD(iph->saddr));
675 goto out;
676 }
677
678 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
679 offset += 2 * sizeof(__u16);
680 if (!skb_make_writable(skb, offset))
681 goto out;
682
683 ip_vs_nat_icmp(skb, pp, cp, 1);
684
685 /* do the statistics and put it back */
686 ip_vs_out_stats(cp, skb);
687
688 skb->ipvs_property = 1;
689 verdict = NF_ACCEPT;
690
691out:
692 __ip_vs_conn_put(cp);
693
694 return verdict;
695}
696
1da177e4
LT
697/*
698 * Handle ICMP messages in the inside-to-outside direction (outgoing).
4856c84c 699 * Find any that might be relevant, check against existing connections.
1da177e4 700 * Currently handles error types - unreachable, quench, ttl exceeded.
1da177e4 701 */
3db05fea 702static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
1da177e4 703{
1da177e4
LT
704 struct iphdr *iph;
705 struct icmphdr _icmph, *ic;
706 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 707 struct ip_vs_iphdr ciph;
1da177e4
LT
708 struct ip_vs_conn *cp;
709 struct ip_vs_protocol *pp;
4856c84c 710 unsigned int offset, ihl;
1da177e4
LT
711
712 *related = 1;
713
714 /* reassemble IP fragments */
eddc9ec5 715 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
776c729e 716 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
1da177e4 717 return NF_STOLEN;
1da177e4
LT
718 }
719
eddc9ec5 720 iph = ip_hdr(skb);
1da177e4
LT
721 offset = ihl = iph->ihl * 4;
722 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
723 if (ic == NULL)
724 return NF_DROP;
725
726 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
727 ic->type, ntohs(icmp_id(ic)),
728 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
729
730 /*
731 * Work through seeing if this is for us.
732 * These checks are supposed to be in an order that means easy
733 * things are checked first to speed up processing.... however
734 * this means that some packets will manage to get a long way
735 * down this stack and then be rejected, but that's life.
736 */
737 if ((ic->type != ICMP_DEST_UNREACH) &&
738 (ic->type != ICMP_SOURCE_QUENCH) &&
739 (ic->type != ICMP_TIME_EXCEEDED)) {
740 *related = 0;
741 return NF_ACCEPT;
742 }
743
744 /* Now find the contained IP header */
745 offset += sizeof(_icmph);
746 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
747 if (cih == NULL)
748 return NF_ACCEPT; /* The packet looks wrong, ignore */
749
750 pp = ip_vs_proto_get(cih->protocol);
751 if (!pp)
752 return NF_ACCEPT;
753
754 /* Is the embedded protocol header present? */
4412ec49 755 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
756 pp->dont_defrag))
757 return NF_ACCEPT;
758
759 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
760
761 offset += cih->ihl * 4;
762
51ef348b 763 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 764 /* The embedded headers contain source and dest in reverse order */
51ef348b 765 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
1da177e4
LT
766 if (!cp)
767 return NF_ACCEPT;
768
4856c84c 769 return handle_response_icmp(skb, iph, cih, cp, pp, offset, ihl);
1da177e4
LT
770}
771
2a3b791e
JV
772#ifdef CONFIG_IP_VS_IPV6
773static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
774{
775 struct ipv6hdr *iph;
776 struct icmp6hdr _icmph, *ic;
777 struct ipv6hdr _ciph, *cih; /* The ip header contained
778 within the ICMP */
779 struct ip_vs_iphdr ciph;
780 struct ip_vs_conn *cp;
781 struct ip_vs_protocol *pp;
782 unsigned int offset, verdict;
783
784 *related = 1;
785
786 /* reassemble IP fragments */
787 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
788 if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
789 return NF_STOLEN;
790 }
791
792 iph = ipv6_hdr(skb);
793 offset = sizeof(struct ipv6hdr);
794 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
795 if (ic == NULL)
796 return NF_DROP;
797
798 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
799 ic->icmp6_type, ntohs(icmpv6_id(ic)),
800 NIP6(iph->saddr), NIP6(iph->daddr));
801
802 /*
803 * Work through seeing if this is for us.
804 * These checks are supposed to be in an order that means easy
805 * things are checked first to speed up processing.... however
806 * this means that some packets will manage to get a long way
807 * down this stack and then be rejected, but that's life.
808 */
809 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
810 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
811 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
812 *related = 0;
813 return NF_ACCEPT;
814 }
815
816 /* Now find the contained IP header */
817 offset += sizeof(_icmph);
818 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
819 if (cih == NULL)
820 return NF_ACCEPT; /* The packet looks wrong, ignore */
821
822 pp = ip_vs_proto_get(cih->nexthdr);
823 if (!pp)
824 return NF_ACCEPT;
825
826 /* Is the embedded protocol header present? */
827 /* TODO: we don't support fragmentation at the moment anyways */
828 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
829 return NF_ACCEPT;
830
831 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
832
833 offset += sizeof(struct ipv6hdr);
834
835 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
836 /* The embedded headers contain source and dest in reverse order */
837 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
838 if (!cp)
839 return NF_ACCEPT;
840
841 verdict = NF_DROP;
842
843 if (IP_VS_FWD_METHOD(cp) != 0) {
844 IP_VS_ERR("shouldn't reach here, because the box is on the "
845 "half connection in the tun/dr module.\n");
846 }
847
848 /* Ensure the checksum is correct */
849 if (!skb_csum_unnecessary(skb)
850 && ip_vs_checksum_complete(skb, sizeof(struct ipv6hdr))) {
851 /* Failed checksum! */
852 IP_VS_DBG(1, "Forward ICMPv6: failed checksum from "
853 NIP6_FMT "!\n",
854 NIP6(iph->saddr));
855 goto out;
856 }
857
858 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
859 offset += 2 * sizeof(__u16);
860 if (!skb_make_writable(skb, offset))
861 goto out;
862
863 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
864
865 /* do the statistics and put it back */
866 ip_vs_out_stats(cp, skb);
867
868 skb->ipvs_property = 1;
869 verdict = NF_ACCEPT;
870
871out:
872 __ip_vs_conn_put(cp);
873
874 return verdict;
875}
876#endif
877
878static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1da177e4
LT
879{
880 struct tcphdr _tcph, *th;
881
2a3b791e 882 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1da177e4
LT
883 if (th == NULL)
884 return 0;
885 return th->rst;
886}
887
4856c84c
MT
888/* Handle response packets: rewrite addresses and send away...
889 * Used for NAT and local client.
890 */
891static unsigned int
892handle_response(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
893 struct ip_vs_conn *cp, int ihl)
894{
895 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
896
897 if (!skb_make_writable(skb, ihl))
898 goto drop;
899
900 /* mangle the packet */
901 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
902 goto drop;
903
904#ifdef CONFIG_IP_VS_IPV6
905 if (af == AF_INET6)
906 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
907 else
908#endif
909 {
910 ip_hdr(skb)->saddr = cp->vaddr.ip;
911 ip_send_check(ip_hdr(skb));
912 }
913
914 /* For policy routing, packets originating from this
915 * machine itself may be routed differently to packets
916 * passing through. We want this packet to be routed as
917 * if it came from this machine itself. So re-compute
918 * the routing information.
919 */
920#ifdef CONFIG_IP_VS_IPV6
921 if (af == AF_INET6) {
922 if (ip6_route_me_harder(skb) != 0)
923 goto drop;
924 } else
925#endif
926 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
927 goto drop;
928
929 /* For policy routing, packets originating from this
930 * machine itself may be routed differently to packets
931 * passing through. We want this packet to be routed as
932 * if it came from this machine itself. So re-compute
933 * the routing information.
934 */
935 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
936 goto drop;
937
938 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
939
940 ip_vs_out_stats(cp, skb);
941 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
942 ip_vs_conn_put(cp);
943
944 skb->ipvs_property = 1;
945
946 LeaveFunction(11);
947 return NF_ACCEPT;
948
949drop:
950 ip_vs_conn_put(cp);
951 kfree_skb(skb);
952 return NF_STOLEN;
953}
954
1da177e4 955/*
6e23ae2a 956 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
4856c84c 957 * Check if outgoing packet belongs to the established ip_vs_conn.
1da177e4
LT
958 */
959static unsigned int
3db05fea 960ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
961 const struct net_device *in, const struct net_device *out,
962 int (*okfn)(struct sk_buff *))
963{
51ef348b 964 struct ip_vs_iphdr iph;
1da177e4
LT
965 struct ip_vs_protocol *pp;
966 struct ip_vs_conn *cp;
2a3b791e 967 int af;
1da177e4
LT
968
969 EnterFunction(11);
970
2a3b791e
JV
971 af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
972
6869c4d8 973 if (skb->ipvs_property)
1da177e4
LT
974 return NF_ACCEPT;
975
2a3b791e
JV
976 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
977#ifdef CONFIG_IP_VS_IPV6
978 if (af == AF_INET6) {
979 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
980 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
1da177e4 981
2a3b791e
JV
982 if (related)
983 return verdict;
984 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
985 }
986 } else
987#endif
988 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
989 int related, verdict = ip_vs_out_icmp(skb, &related);
990
991 if (related)
992 return verdict;
993 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
994 }
1da177e4 995
51ef348b 996 pp = ip_vs_proto_get(iph.protocol);
1da177e4
LT
997 if (unlikely(!pp))
998 return NF_ACCEPT;
999
1000 /* reassemble IP fragments */
2a3b791e
JV
1001#ifdef CONFIG_IP_VS_IPV6
1002 if (af == AF_INET6) {
1003 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1004 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
1da177e4 1005
2a3b791e
JV
1006 if (related)
1007 return verdict;
1008
1009 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1010 }
1011 } else
1012#endif
1013 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1014 !pp->dont_defrag)) {
1015 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
1016 return NF_STOLEN;
1017
1018 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1019 }
1da177e4
LT
1020
1021 /*
1022 * Check if the packet belongs to an existing entry
1023 */
2a3b791e 1024 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1da177e4
LT
1025
1026 if (unlikely(!cp)) {
1027 if (sysctl_ip_vs_nat_icmp_send &&
1028 (pp->protocol == IPPROTO_TCP ||
1029 pp->protocol == IPPROTO_UDP)) {
014d730d 1030 __be16 _ports[2], *pptr;
1da177e4 1031
51ef348b 1032 pptr = skb_header_pointer(skb, iph.len,
1da177e4
LT
1033 sizeof(_ports), _ports);
1034 if (pptr == NULL)
1035 return NF_ACCEPT; /* Not for me */
7937df15
JV
1036 if (ip_vs_lookup_real_service(af, iph.protocol,
1037 &iph.saddr,
2a3b791e 1038 pptr[0])) {
1da177e4
LT
1039 /*
1040 * Notify the real server: there is no
1041 * existing entry if it is not RST
1042 * packet or not TCP packet.
1043 */
51ef348b 1044 if (iph.protocol != IPPROTO_TCP
2a3b791e
JV
1045 || !is_tcp_reset(skb, iph.len)) {
1046#ifdef CONFIG_IP_VS_IPV6
1047 if (af == AF_INET6)
1048 icmpv6_send(skb,
1049 ICMPV6_DEST_UNREACH,
1050 ICMPV6_PORT_UNREACH,
1051 0, skb->dev);
1052 else
1053#endif
1054 icmp_send(skb,
1055 ICMP_DEST_UNREACH,
1056 ICMP_PORT_UNREACH, 0);
1da177e4
LT
1057 return NF_DROP;
1058 }
1059 }
1060 }
1061 IP_VS_DBG_PKT(12, pp, skb, 0,
1062 "packet continues traversal as normal");
1063 return NF_ACCEPT;
1064 }
1065
4856c84c 1066 return handle_response(af, skb, pp, cp, iph.len);
1da177e4
LT
1067}
1068
1069
1070/*
1071 * Handle ICMP messages in the outside-to-inside direction (incoming).
1072 * Find any that might be relevant, check against existing connections,
1073 * forward to the right destination host if relevant.
1074 * Currently handles error types - unreachable, quench, ttl exceeded.
1075 */
e905a9ed 1076static int
3db05fea 1077ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1da177e4 1078{
1da177e4
LT
1079 struct iphdr *iph;
1080 struct icmphdr _icmph, *ic;
1081 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 1082 struct ip_vs_iphdr ciph;
1da177e4
LT
1083 struct ip_vs_conn *cp;
1084 struct ip_vs_protocol *pp;
1085 unsigned int offset, ihl, verdict;
1086
1087 *related = 1;
1088
1089 /* reassemble IP fragments */
eddc9ec5 1090 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
6e23ae2a 1091 if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
776c729e 1092 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
1da177e4 1093 return NF_STOLEN;
1da177e4
LT
1094 }
1095
eddc9ec5 1096 iph = ip_hdr(skb);
1da177e4
LT
1097 offset = ihl = iph->ihl * 4;
1098 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1099 if (ic == NULL)
1100 return NF_DROP;
1101
1102 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %u.%u.%u.%u->%u.%u.%u.%u\n",
1103 ic->type, ntohs(icmp_id(ic)),
1104 NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1105
1106 /*
1107 * Work through seeing if this is for us.
1108 * These checks are supposed to be in an order that means easy
1109 * things are checked first to speed up processing.... however
1110 * this means that some packets will manage to get a long way
1111 * down this stack and then be rejected, but that's life.
1112 */
1113 if ((ic->type != ICMP_DEST_UNREACH) &&
1114 (ic->type != ICMP_SOURCE_QUENCH) &&
1115 (ic->type != ICMP_TIME_EXCEEDED)) {
1116 *related = 0;
1117 return NF_ACCEPT;
1118 }
1119
1120 /* Now find the contained IP header */
1121 offset += sizeof(_icmph);
1122 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1123 if (cih == NULL)
1124 return NF_ACCEPT; /* The packet looks wrong, ignore */
1125
1126 pp = ip_vs_proto_get(cih->protocol);
1127 if (!pp)
1128 return NF_ACCEPT;
1129
1130 /* Is the embedded protocol header present? */
4412ec49 1131 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
1132 pp->dont_defrag))
1133 return NF_ACCEPT;
1134
1135 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
1136
1137 offset += cih->ihl * 4;
1138
51ef348b 1139 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 1140 /* The embedded headers contain source and dest in reverse order */
51ef348b 1141 cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
4856c84c
MT
1142 if (!cp) {
1143 /* The packet could also belong to a local client */
1144 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
1145 if (cp)
1146 return handle_response_icmp(skb, iph, cih, cp, pp,
1147 offset, ihl);
1da177e4 1148 return NF_ACCEPT;
4856c84c 1149 }
1da177e4
LT
1150
1151 verdict = NF_DROP;
1152
1153 /* Ensure the checksum is correct */
60476372 1154 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4
LT
1155 /* Failed checksum! */
1156 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %d.%d.%d.%d!\n",
1157 NIPQUAD(iph->saddr));
1158 goto out;
1159 }
1160
1161 /* do the statistics and put it back */
1162 ip_vs_in_stats(cp, skb);
1163 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1164 offset += 2 * sizeof(__u16);
1165 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1166 /* do not touch skb anymore */
1167
1168 out:
1169 __ip_vs_conn_put(cp);
1170
1171 return verdict;
1172}
1173
2a3b791e
JV
1174#ifdef CONFIG_IP_VS_IPV6
1175static int
1176ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1177{
1178 struct ipv6hdr *iph;
1179 struct icmp6hdr _icmph, *ic;
1180 struct ipv6hdr _ciph, *cih; /* The ip header contained
1181 within the ICMP */
1182 struct ip_vs_iphdr ciph;
1183 struct ip_vs_conn *cp;
1184 struct ip_vs_protocol *pp;
1185 unsigned int offset, verdict;
1186
1187 *related = 1;
1188
1189 /* reassemble IP fragments */
1190 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1191 if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
1192 IP_DEFRAG_VS_IN :
1193 IP_DEFRAG_VS_FWD))
1194 return NF_STOLEN;
1195 }
1196
1197 iph = ipv6_hdr(skb);
1198 offset = sizeof(struct ipv6hdr);
1199 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1200 if (ic == NULL)
1201 return NF_DROP;
1202
1203 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) " NIP6_FMT "->" NIP6_FMT "\n",
1204 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1205 NIP6(iph->saddr), NIP6(iph->daddr));
1206
1207 /*
1208 * Work through seeing if this is for us.
1209 * These checks are supposed to be in an order that means easy
1210 * things are checked first to speed up processing.... however
1211 * this means that some packets will manage to get a long way
1212 * down this stack and then be rejected, but that's life.
1213 */
1214 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1215 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1216 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1217 *related = 0;
1218 return NF_ACCEPT;
1219 }
1220
1221 /* Now find the contained IP header */
1222 offset += sizeof(_icmph);
1223 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1224 if (cih == NULL)
1225 return NF_ACCEPT; /* The packet looks wrong, ignore */
1226
1227 pp = ip_vs_proto_get(cih->nexthdr);
1228 if (!pp)
1229 return NF_ACCEPT;
1230
1231 /* Is the embedded protocol header present? */
1232 /* TODO: we don't support fragmentation at the moment anyways */
1233 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1234 return NF_ACCEPT;
1235
1236 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
1237
1238 offset += sizeof(struct ipv6hdr);
1239
1240 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1241 /* The embedded headers contain source and dest in reverse order */
1242 cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
1243 if (!cp)
1244 return NF_ACCEPT;
1245
1246 verdict = NF_DROP;
1247
1248 /* do the statistics and put it back */
1249 ip_vs_in_stats(cp, skb);
1250 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
1251 offset += 2 * sizeof(__u16);
1252 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1253 /* do not touch skb anymore */
1254
1255 __ip_vs_conn_put(cp);
1256
1257 return verdict;
1258}
1259#endif
1260
1261
1da177e4
LT
1262/*
1263 * Check if it's for virtual services, look it up,
1264 * and send it on its way...
1265 */
1266static unsigned int
3db05fea 1267ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1268 const struct net_device *in, const struct net_device *out,
1269 int (*okfn)(struct sk_buff *))
1270{
51ef348b 1271 struct ip_vs_iphdr iph;
1da177e4
LT
1272 struct ip_vs_protocol *pp;
1273 struct ip_vs_conn *cp;
2a3b791e
JV
1274 int ret, restart, af;
1275
1276 af = (skb->protocol == __constant_htons(ETH_P_IP)) ? AF_INET : AF_INET6;
51ef348b 1277
2a3b791e 1278 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1da177e4
LT
1279
1280 /*
4856c84c
MT
1281 * Big tappo: only PACKET_HOST, including loopback for local client
1282 * Don't handle local packets on IPv6 for now
1da177e4 1283 */
4856c84c
MT
1284 if (unlikely(skb->pkt_type != PACKET_HOST ||
1285 (af == AF_INET6 || (skb->dev->flags & IFF_LOOPBACK ||
1286 skb->sk)))) {
51ef348b
JV
1287 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
1288 skb->pkt_type,
1289 iph.protocol,
2a3b791e 1290 IP_VS_DBG_ADDR(af, &iph.daddr));
1da177e4
LT
1291 return NF_ACCEPT;
1292 }
1293
51ef348b 1294 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
3db05fea 1295 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
1da177e4
LT
1296
1297 if (related)
1298 return verdict;
2a3b791e 1299 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1da177e4
LT
1300 }
1301
1302 /* Protocol supported? */
51ef348b 1303 pp = ip_vs_proto_get(iph.protocol);
1da177e4
LT
1304 if (unlikely(!pp))
1305 return NF_ACCEPT;
1306
1da177e4
LT
1307 /*
1308 * Check if the packet belongs to an existing connection entry
1309 */
2a3b791e 1310 cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
1da177e4
LT
1311
1312 if (unlikely(!cp)) {
1313 int v;
1314
4856c84c
MT
1315 /* For local client packets, it could be a response */
1316 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1317 if (cp)
1318 return handle_response(af, skb, pp, cp, iph.len);
1319
2a3b791e 1320 if (!pp->conn_schedule(af, skb, pp, &v, &cp))
1da177e4
LT
1321 return v;
1322 }
1323
1324 if (unlikely(!cp)) {
1325 /* sorry, all this trouble for a no-hit :) */
1326 IP_VS_DBG_PKT(12, pp, skb, 0,
1327 "packet continues traversal as normal");
1328 return NF_ACCEPT;
1329 }
1330
1331 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1332
1333 /* Check the server status */
1334 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1335 /* the destination server is not available */
1336
1337 if (sysctl_ip_vs_expire_nodest_conn) {
1338 /* try to expire the connection immediately */
1339 ip_vs_conn_expire_now(cp);
1da177e4 1340 }
dc8103f2
JA
1341 /* don't restart its timer, and silently
1342 drop the packet. */
1343 __ip_vs_conn_put(cp);
1da177e4
LT
1344 return NF_DROP;
1345 }
1346
1347 ip_vs_in_stats(cp, skb);
1348 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1349 if (cp->packet_xmit)
1350 ret = cp->packet_xmit(skb, cp, pp);
1351 /* do not touch skb anymore */
1352 else {
1353 IP_VS_DBG_RL("warning: packet_xmit is null");
1354 ret = NF_ACCEPT;
1355 }
1356
efac5276
RB
1357 /* Increase its packet counter and check if it is needed
1358 * to be synchronized
1359 *
1360 * Sync connection if it is about to close to
1361 * encorage the standby servers to update the connections timeout
1362 */
1da177e4 1363 atomic_inc(&cp->in_pkts);
c6883f58
JV
1364 if (af == AF_INET &&
1365 (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
efac5276
RB
1366 (((cp->protocol != IPPROTO_TCP ||
1367 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1368 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
1369 == sysctl_ip_vs_sync_threshold[0])) ||
1370 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1371 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
9d3a0de7
RB
1372 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1373 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1da177e4 1374 ip_vs_sync_conn(cp);
efac5276 1375 cp->old_state = cp->state;
1da177e4
LT
1376
1377 ip_vs_conn_put(cp);
1378 return ret;
1379}
1380
1381
1382/*
6e23ae2a 1383 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1da177e4
LT
1384 * related packets destined for 0.0.0.0/0.
1385 * When fwmark-based virtual service is used, such as transparent
1386 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1387 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
6e23ae2a 1388 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1da177e4
LT
1389 * and send them to ip_vs_in_icmp.
1390 */
1391static unsigned int
3db05fea 1392ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1393 const struct net_device *in, const struct net_device *out,
1394 int (*okfn)(struct sk_buff *))
1395{
1396 int r;
1397
3db05fea 1398 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
1399 return NF_ACCEPT;
1400
3db05fea 1401 return ip_vs_in_icmp(skb, &r, hooknum);
1da177e4
LT
1402}
1403
2a3b791e
JV
1404#ifdef CONFIG_IP_VS_IPV6
1405static unsigned int
1406ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1407 const struct net_device *in, const struct net_device *out,
1408 int (*okfn)(struct sk_buff *))
1409{
1410 int r;
1411
1412 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1413 return NF_ACCEPT;
1414
1415 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1416}
1417#endif
1418
1da177e4 1419
1999414a 1420static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
41c5b317
PM
1421 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1422 * or VS/NAT(change destination), so that filtering rules can be
1423 * applied to IPVS. */
1424 {
1425 .hook = ip_vs_in,
1426 .owner = THIS_MODULE,
1427 .pf = PF_INET,
1428 .hooknum = NF_INET_LOCAL_IN,
1429 .priority = 100,
1430 },
1431 /* After packet filtering, change source only for VS/NAT */
1432 {
1433 .hook = ip_vs_out,
1434 .owner = THIS_MODULE,
1435 .pf = PF_INET,
1436 .hooknum = NF_INET_FORWARD,
1437 .priority = 100,
1438 },
1439 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1440 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1441 {
1442 .hook = ip_vs_forward_icmp,
1443 .owner = THIS_MODULE,
1444 .pf = PF_INET,
1445 .hooknum = NF_INET_FORWARD,
1446 .priority = 99,
1447 },
1448 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1449 {
1450 .hook = ip_vs_post_routing,
1451 .owner = THIS_MODULE,
1452 .pf = PF_INET,
1453 .hooknum = NF_INET_POST_ROUTING,
1454 .priority = NF_IP_PRI_NAT_SRC-1,
1455 },
473b23d3
JV
1456#ifdef CONFIG_IP_VS_IPV6
1457 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1458 * or VS/NAT(change destination), so that filtering rules can be
1459 * applied to IPVS. */
1460 {
1461 .hook = ip_vs_in,
1462 .owner = THIS_MODULE,
1463 .pf = PF_INET6,
1464 .hooknum = NF_INET_LOCAL_IN,
1465 .priority = 100,
1466 },
1467 /* After packet filtering, change source only for VS/NAT */
1468 {
1469 .hook = ip_vs_out,
1470 .owner = THIS_MODULE,
1471 .pf = PF_INET6,
1472 .hooknum = NF_INET_FORWARD,
1473 .priority = 100,
1474 },
1475 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1476 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1477 {
1478 .hook = ip_vs_forward_icmp_v6,
1479 .owner = THIS_MODULE,
1480 .pf = PF_INET6,
1481 .hooknum = NF_INET_FORWARD,
1482 .priority = 99,
1483 },
1484 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1485 {
1486 .hook = ip_vs_post_routing,
1487 .owner = THIS_MODULE,
1488 .pf = PF_INET6,
1489 .hooknum = NF_INET_POST_ROUTING,
1490 .priority = NF_IP6_PRI_NAT_SRC-1,
1491 },
1492#endif
1da177e4
LT
1493};
1494
1495
1496/*
1497 * Initialize IP Virtual Server
1498 */
1499static int __init ip_vs_init(void)
1500{
1501 int ret;
1502
a919cf4b
SW
1503 ip_vs_estimator_init();
1504
1da177e4
LT
1505 ret = ip_vs_control_init();
1506 if (ret < 0) {
1507 IP_VS_ERR("can't setup control.\n");
a919cf4b 1508 goto cleanup_estimator;
1da177e4
LT
1509 }
1510
1511 ip_vs_protocol_init();
1512
1513 ret = ip_vs_app_init();
1514 if (ret < 0) {
1515 IP_VS_ERR("can't setup application helper.\n");
1516 goto cleanup_protocol;
1517 }
1518
1519 ret = ip_vs_conn_init();
1520 if (ret < 0) {
1521 IP_VS_ERR("can't setup connection table.\n");
1522 goto cleanup_app;
1523 }
1524
41c5b317 1525 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4 1526 if (ret < 0) {
41c5b317 1527 IP_VS_ERR("can't register hooks.\n");
1da177e4
LT
1528 goto cleanup_conn;
1529 }
1530
1da177e4
LT
1531 IP_VS_INFO("ipvs loaded.\n");
1532 return ret;
1533
1da177e4
LT
1534 cleanup_conn:
1535 ip_vs_conn_cleanup();
1536 cleanup_app:
1537 ip_vs_app_cleanup();
1538 cleanup_protocol:
1539 ip_vs_protocol_cleanup();
1540 ip_vs_control_cleanup();
a919cf4b
SW
1541 cleanup_estimator:
1542 ip_vs_estimator_cleanup();
1da177e4
LT
1543 return ret;
1544}
1545
1546static void __exit ip_vs_cleanup(void)
1547{
41c5b317 1548 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4
LT
1549 ip_vs_conn_cleanup();
1550 ip_vs_app_cleanup();
1551 ip_vs_protocol_cleanup();
1552 ip_vs_control_cleanup();
a919cf4b 1553 ip_vs_estimator_cleanup();
1da177e4
LT
1554 IP_VS_INFO("ipvs unloaded.\n");
1555}
1556
1557module_init(ip_vs_init);
1558module_exit(ip_vs_cleanup);
1559MODULE_LICENSE("GPL");
This page took 0.672719 seconds and 5 git commands to generate.