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