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