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