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