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