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