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