IPVS: Add sysctl_sync_ver()
[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>
61b1ab45 44#include <net/netns/generic.h> /* net_generic() */
1da177e4
LT
45
46#include <linux/netfilter.h>
47#include <linux/netfilter_ipv4.h>
48
2a3b791e
JV
49#ifdef CONFIG_IP_VS_IPV6
50#include <net/ipv6.h>
51#include <linux/netfilter_ipv6.h>
489fdeda 52#include <net/ip6_route.h>
2a3b791e
JV
53#endif
54
1da177e4
LT
55#include <net/ip_vs.h>
56
57
58EXPORT_SYMBOL(register_ip_vs_scheduler);
59EXPORT_SYMBOL(unregister_ip_vs_scheduler);
1da177e4
LT
60EXPORT_SYMBOL(ip_vs_proto_name);
61EXPORT_SYMBOL(ip_vs_conn_new);
62EXPORT_SYMBOL(ip_vs_conn_in_get);
63EXPORT_SYMBOL(ip_vs_conn_out_get);
64#ifdef CONFIG_IP_VS_PROTO_TCP
65EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
66#endif
67EXPORT_SYMBOL(ip_vs_conn_put);
68#ifdef CONFIG_IP_VS_DEBUG
69EXPORT_SYMBOL(ip_vs_get_debug_level);
70#endif
1da177e4 71
61b1ab45
HS
72int ip_vs_net_id __read_mostly;
73#ifdef IP_VS_GENERIC_NETNS
74EXPORT_SYMBOL(ip_vs_net_id);
75#endif
76/* netns cnt used for uniqueness */
77static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
1da177e4
LT
78
79/* ID used in ICMP lookups */
80#define icmp_id(icmph) (((icmph)->un).echo.id)
2a3b791e 81#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
1da177e4
LT
82
83const char *ip_vs_proto_name(unsigned proto)
84{
85 static char buf[20];
86
87 switch (proto) {
88 case IPPROTO_IP:
89 return "IP";
90 case IPPROTO_UDP:
91 return "UDP";
92 case IPPROTO_TCP:
93 return "TCP";
2906f66a
VMR
94 case IPPROTO_SCTP:
95 return "SCTP";
1da177e4
LT
96 case IPPROTO_ICMP:
97 return "ICMP";
2a3b791e
JV
98#ifdef CONFIG_IP_VS_IPV6
99 case IPPROTO_ICMPV6:
100 return "ICMPv6";
101#endif
1da177e4
LT
102 default:
103 sprintf(buf, "IP_%d", proto);
104 return buf;
105 }
106}
107
108void ip_vs_init_hash_table(struct list_head *table, int rows)
109{
110 while (--rows >= 0)
111 INIT_LIST_HEAD(&table[rows]);
112}
113
114static inline void
115ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
116{
117 struct ip_vs_dest *dest = cp->dest;
b17fc996
HS
118 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
119
1da177e4 120 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
b17fc996
HS
121 struct ip_vs_cpu_stats *s;
122
123 s = this_cpu_ptr(dest->stats.cpustats);
124 s->ustats.inpkts++;
125 u64_stats_update_begin(&s->syncp);
126 s->ustats.inbytes += skb->len;
127 u64_stats_update_end(&s->syncp);
128
129 s = this_cpu_ptr(dest->svc->stats.cpustats);
130 s->ustats.inpkts++;
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
134
2a0751af 135 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996
HS
136 s->ustats.inpkts++;
137 u64_stats_update_begin(&s->syncp);
138 s->ustats.inbytes += skb->len;
139 u64_stats_update_end(&s->syncp);
1da177e4
LT
140 }
141}
142
143
144static inline void
145ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
146{
147 struct ip_vs_dest *dest = cp->dest;
b17fc996
HS
148 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
149
1da177e4 150 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
b17fc996
HS
151 struct ip_vs_cpu_stats *s;
152
153 s = this_cpu_ptr(dest->stats.cpustats);
154 s->ustats.outpkts++;
155 u64_stats_update_begin(&s->syncp);
156 s->ustats.outbytes += skb->len;
157 u64_stats_update_end(&s->syncp);
158
159 s = this_cpu_ptr(dest->svc->stats.cpustats);
160 s->ustats.outpkts++;
161 u64_stats_update_begin(&s->syncp);
162 s->ustats.outbytes += skb->len;
163 u64_stats_update_end(&s->syncp);
164
2a0751af 165 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996
HS
166 s->ustats.outpkts++;
167 u64_stats_update_begin(&s->syncp);
168 s->ustats.outbytes += skb->len;
169 u64_stats_update_end(&s->syncp);
1da177e4
LT
170 }
171}
172
173
174static inline void
175ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
176{
b17fc996
HS
177 struct netns_ipvs *ipvs = net_ipvs(svc->net);
178 struct ip_vs_cpu_stats *s;
1da177e4 179
b17fc996
HS
180 s = this_cpu_ptr(cp->dest->stats.cpustats);
181 s->ustats.conns++;
1da177e4 182
b17fc996
HS
183 s = this_cpu_ptr(svc->stats.cpustats);
184 s->ustats.conns++;
185
2a0751af 186 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996 187 s->ustats.conns++;
1da177e4
LT
188}
189
190
191static inline int
192ip_vs_set_state(struct ip_vs_conn *cp, int direction,
193 const struct sk_buff *skb,
9330419d 194 struct ip_vs_proto_data *pd)
1da177e4 195{
9330419d 196 if (unlikely(!pd->pp->state_transition))
1da177e4 197 return 0;
9330419d 198 return pd->pp->state_transition(cp, direction, skb, pd);
1da177e4
LT
199}
200
a5959d53 201static inline int
85999283
SH
202ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
203 struct sk_buff *skb, int protocol,
204 const union nf_inet_addr *caddr, __be16 cport,
205 const union nf_inet_addr *vaddr, __be16 vport,
206 struct ip_vs_conn_param *p)
207{
6e67e586
HS
208 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
209 vport, p);
85999283
SH
210 p->pe = svc->pe;
211 if (p->pe && p->pe->fill_param)
a5959d53
HS
212 return p->pe->fill_param(p, skb);
213
214 return 0;
85999283 215}
1da177e4 216
1da177e4
LT
217/*
218 * IPVS persistent scheduling function
219 * It creates a connection entry according to its template if exists,
220 * or selects a server and creates a connection entry plus a template.
221 * Locking: we are svc user (svc->refcnt), so we hold all dests too
222 * Protocols supported: TCP, UDP
223 */
224static struct ip_vs_conn *
225ip_vs_sched_persist(struct ip_vs_service *svc,
85999283 226 struct sk_buff *skb,
a5959d53 227 __be16 src_port, __be16 dst_port, int *ignored)
1da177e4
LT
228{
229 struct ip_vs_conn *cp = NULL;
28364a59 230 struct ip_vs_iphdr iph;
1da177e4
LT
231 struct ip_vs_dest *dest;
232 struct ip_vs_conn *ct;
5b57a98c 233 __be16 dport = 0; /* destination port to forward */
3575792e 234 unsigned int flags;
f11017ec 235 struct ip_vs_conn_param param;
28364a59
JV
236 union nf_inet_addr snet; /* source network of the client,
237 after masking */
cd17f9ed
JV
238
239 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
1da177e4
LT
240
241 /* Mask saddr with the netmask to adjust template granularity */
cd17f9ed
JV
242#ifdef CONFIG_IP_VS_IPV6
243 if (svc->af == AF_INET6)
244 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
245 else
246#endif
247 snet.ip = iph.saddr.ip & svc->netmask;
1da177e4 248
cd17f9ed
JV
249 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
250 "mnet %s\n",
ce144f24
HS
251 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(src_port),
252 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(dst_port),
cd17f9ed 253 IP_VS_DBG_ADDR(svc->af, &snet));
1da177e4
LT
254
255 /*
256 * As far as we know, FTP is a very complicated network protocol, and
257 * it uses control connection and data connections. For active FTP,
258 * FTP server initialize data connection to the client, its source port
259 * is often 20. For passive FTP, FTP server tells the clients the port
260 * that it passively listens to, and the client issues the data
261 * connection. In the tunneling or direct routing mode, the load
262 * balancer is on the client-to-server half of connection, the port
263 * number is unknown to the load balancer. So, a conn template like
264 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
265 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
266 * is created for other persistent services.
267 */
5b57a98c 268 {
f11017ec
SH
269 int protocol = iph.protocol;
270 const union nf_inet_addr *vaddr = &iph.daddr;
271 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
272 __be16 vport = 0;
273
ce144f24 274 if (dst_port == svc->port) {
5b57a98c
SH
275 /* non-FTP template:
276 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
277 * FTP template:
278 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
1da177e4
LT
279 */
280 if (svc->port != FTPPORT)
ce144f24 281 vport = dst_port;
1da177e4 282 } else {
5b57a98c
SH
283 /* Note: persistent fwmark-based services and
284 * persistent port zero service are handled here.
285 * fwmark template:
286 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
287 * port zero template:
288 * <protocol,caddr,0,vaddr,0,daddr,0>
1da177e4 289 */
28364a59 290 if (svc->fwmark) {
5b57a98c
SH
291 protocol = IPPROTO_IP;
292 vaddr = &fwmark;
293 }
1da177e4 294 }
a5959d53
HS
295 /* return *ignored = -1 so NF_DROP can be used */
296 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
297 vaddr, vport, &param) < 0) {
298 *ignored = -1;
299 return NULL;
300 }
1da177e4
LT
301 }
302
5b57a98c 303 /* Check if a template already exists */
f11017ec 304 ct = ip_vs_ct_in_get(&param);
5b57a98c 305 if (!ct || !ip_vs_check_template(ct)) {
a5959d53
HS
306 /*
307 * No template found or the dest of the connection
5b57a98c 308 * template is not available.
a5959d53 309 * return *ignored=0 i.e. ICMP and NF_DROP
5b57a98c
SH
310 */
311 dest = svc->scheduler->schedule(svc, skb);
312 if (!dest) {
313 IP_VS_DBG(1, "p-schedule: no dest found.\n");
85999283 314 kfree(param.pe_data);
a5959d53 315 *ignored = 0;
5b57a98c
SH
316 return NULL;
317 }
318
ce144f24 319 if (dst_port == svc->port && svc->port != FTPPORT)
5b57a98c
SH
320 dport = dest->port;
321
85999283
SH
322 /* Create a template
323 * This adds param.pe_data to the template,
324 * and thus param.pe_data will be destroyed
325 * when the template expires */
f11017ec 326 ct = ip_vs_conn_new(&param, &dest->addr, dport,
0e051e68 327 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
85999283
SH
328 if (ct == NULL) {
329 kfree(param.pe_data);
a5959d53 330 *ignored = -1;
5b57a98c 331 return NULL;
85999283 332 }
5b57a98c
SH
333
334 ct->timeout = svc->timeout;
85999283 335 } else {
5b57a98c
SH
336 /* set destination with the found template */
337 dest = ct->dest;
85999283
SH
338 kfree(param.pe_data);
339 }
5b57a98c 340
ce144f24 341 dport = dst_port;
5b57a98c
SH
342 if (dport == svc->port && dest->port)
343 dport = dest->port;
344
26ec037f
NC
345 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
346 && iph.protocol == IPPROTO_UDP)?
347 IP_VS_CONN_F_ONE_PACKET : 0;
348
1da177e4
LT
349 /*
350 * Create a new connection according to the template
351 */
6e67e586
HS
352 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr,
353 src_port, &iph.daddr, dst_port, &param);
ce144f24 354
0e051e68 355 cp = ip_vs_conn_new(&param, &dest->addr, dport, flags, dest, skb->mark);
1da177e4
LT
356 if (cp == NULL) {
357 ip_vs_conn_put(ct);
a5959d53 358 *ignored = -1;
1da177e4
LT
359 return NULL;
360 }
361
362 /*
363 * Add its control
364 */
365 ip_vs_control_add(cp, ct);
366 ip_vs_conn_put(ct);
367
368 ip_vs_conn_stats(cp, svc);
369 return cp;
370}
371
372
373/*
374 * IPVS main scheduling function
375 * It selects a server according to the virtual service, and
376 * creates a connection entry.
377 * Protocols supported: TCP, UDP
a5959d53
HS
378 *
379 * Usage of *ignored
380 *
381 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
382 * svc/scheduler decides that this packet should be accepted with
383 * NF_ACCEPT because it must not be scheduled.
384 *
385 * 0 : scheduler can not find destination, so try bypass or
386 * return ICMP and then NF_DROP (ip_vs_leave).
387 *
388 * -1 : scheduler tried to schedule but fatal error occurred, eg.
389 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
390 * failure such as missing Call-ID, ENOMEM on skb_linearize
391 * or pe_data. In this case we should return NF_DROP without
392 * any attempts to send ICMP with ip_vs_leave.
1da177e4
LT
393 */
394struct ip_vs_conn *
190ecd27 395ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
9330419d 396 struct ip_vs_proto_data *pd, int *ignored)
1da177e4 397{
9330419d 398 struct ip_vs_protocol *pp = pd->pp;
1da177e4 399 struct ip_vs_conn *cp = NULL;
28364a59 400 struct ip_vs_iphdr iph;
1da177e4 401 struct ip_vs_dest *dest;
3575792e
JA
402 __be16 _ports[2], *pptr;
403 unsigned int flags;
1da177e4 404
190ecd27 405 *ignored = 1;
28364a59
JV
406 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
407 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
1da177e4
LT
408 if (pptr == NULL)
409 return NULL;
410
190ecd27
JA
411 /*
412 * FTPDATA needs this check when using local real server.
413 * Never schedule Active FTPDATA connections from real server.
414 * For LVS-NAT they must be already created. For other methods
415 * with persistence the connection is created on SYN+ACK.
416 */
417 if (pptr[0] == FTPDATA) {
0d79641a
JA
418 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
419 "Not scheduling FTPDATA");
190ecd27
JA
420 return NULL;
421 }
422
423 /*
a5959d53 424 * Do not schedule replies from local real server.
190ecd27
JA
425 */
426 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
9330419d 427 (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) {
0d79641a 428 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
190ecd27
JA
429 "Not scheduling reply for existing connection");
430 __ip_vs_conn_put(cp);
431 return NULL;
432 }
433
1da177e4
LT
434 /*
435 * Persistent service
436 */
a5959d53
HS
437 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
438 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored);
439
440 *ignored = 0;
1da177e4
LT
441
442 /*
443 * Non-persistent service
444 */
445 if (!svc->fwmark && pptr[1] != svc->port) {
446 if (!svc->port)
1e3e238e
HE
447 pr_err("Schedule: port zero only supported "
448 "in persistent services, "
449 "check your ipvs configuration\n");
1da177e4
LT
450 return NULL;
451 }
452
453 dest = svc->scheduler->schedule(svc, skb);
454 if (dest == NULL) {
455 IP_VS_DBG(1, "Schedule: no dest found.\n");
456 return NULL;
457 }
458
26ec037f
NC
459 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
460 && iph.protocol == IPPROTO_UDP)?
461 IP_VS_CONN_F_ONE_PACKET : 0;
462
1da177e4
LT
463 /*
464 * Create a connection entry.
465 */
f11017ec
SH
466 {
467 struct ip_vs_conn_param p;
6e67e586
HS
468
469 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
470 &iph.saddr, pptr[0], &iph.daddr, pptr[1],
471 &p);
f11017ec
SH
472 cp = ip_vs_conn_new(&p, &dest->addr,
473 dest->port ? dest->port : pptr[1],
0e051e68 474 flags, dest, skb->mark);
a5959d53
HS
475 if (!cp) {
476 *ignored = -1;
f11017ec 477 return NULL;
a5959d53 478 }
f11017ec 479 }
1da177e4 480
cd17f9ed
JV
481 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
482 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
483 ip_vs_fwd_tag(cp),
484 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
485 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
486 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
487 cp->flags, atomic_read(&cp->refcnt));
1da177e4
LT
488
489 ip_vs_conn_stats(cp, svc);
490 return cp;
491}
492
493
494/*
495 * Pass or drop the packet.
496 * Called by ip_vs_in, when the virtual service is available but
497 * no destination is available for a new connection.
498 */
499int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
9330419d 500 struct ip_vs_proto_data *pd)
1da177e4 501{
4a98480b 502 struct net *net;
a0840e2e 503 struct netns_ipvs *ipvs;
014d730d 504 __be16 _ports[2], *pptr;
28364a59 505 struct ip_vs_iphdr iph;
2a3b791e 506 int unicast;
9330419d 507
2a3b791e 508 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
1da177e4 509
28364a59 510 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
1da177e4
LT
511 if (pptr == NULL) {
512 ip_vs_service_put(svc);
513 return NF_DROP;
514 }
4a98480b 515 net = skb_net(skb);
1da177e4 516
2a3b791e
JV
517#ifdef CONFIG_IP_VS_IPV6
518 if (svc->af == AF_INET6)
519 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
520 else
521#endif
4a98480b 522 unicast = (inet_addr_type(net, iph.daddr.ip) == RTN_UNICAST);
2a3b791e 523
1da177e4 524 /* if it is fwmark-based service, the cache_bypass sysctl is up
2a3b791e 525 and the destination is a non-local unicast, then create
1da177e4 526 a cache_bypass connection entry */
4a98480b 527 ipvs = net_ipvs(net);
a0840e2e 528 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
1da177e4
LT
529 int ret, cs;
530 struct ip_vs_conn *cp;
3575792e
JA
531 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
532 iph.protocol == IPPROTO_UDP)?
533 IP_VS_CONN_F_ONE_PACKET : 0;
dff630dd 534 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
1da177e4
LT
535
536 ip_vs_service_put(svc);
537
538 /* create a new connection entry */
1e3e238e 539 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
f11017ec
SH
540 {
541 struct ip_vs_conn_param p;
6e67e586 542 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
f11017ec
SH
543 &iph.saddr, pptr[0],
544 &iph.daddr, pptr[1], &p);
545 cp = ip_vs_conn_new(&p, &daddr, 0,
546 IP_VS_CONN_F_BYPASS | flags,
0e051e68 547 NULL, skb->mark);
f11017ec
SH
548 if (!cp)
549 return NF_DROP;
550 }
1da177e4
LT
551
552 /* statistics */
553 ip_vs_in_stats(cp, skb);
554
555 /* set state */
9330419d 556 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1da177e4
LT
557
558 /* transmit the first SYN packet */
9330419d 559 ret = cp->packet_xmit(skb, cp, pd->pp);
1da177e4
LT
560 /* do not touch skb anymore */
561
562 atomic_inc(&cp->in_pkts);
563 ip_vs_conn_put(cp);
564 return ret;
565 }
566
567 /*
568 * When the virtual ftp service is presented, packets destined
569 * for other services on the VIP may get here (except services
570 * listed in the ipvs table), pass the packets, because it is
571 * not ipvs job to decide to drop the packets.
572 */
573 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
574 ip_vs_service_put(svc);
575 return NF_ACCEPT;
576 }
577
578 ip_vs_service_put(svc);
579
580 /*
581 * Notify the client that the destination is unreachable, and
582 * release the socket buffer.
583 * Since it is in IP layer, the TCP socket is not actually
584 * created, the TCP RST packet cannot be sent, instead that
585 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
586 */
2a3b791e 587#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
588 if (svc->af == AF_INET6) {
589 if (!skb->dev) {
590 struct net *net = dev_net(skb_dst(skb)->dev);
591
592 skb->dev = net->loopback_dev;
593 }
3ffe533c 594 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
cb59155f 595 } else
2a3b791e
JV
596#endif
597 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
598
1da177e4
LT
599 return NF_DROP;
600}
601
84b3cee3
SH
602#ifdef CONFIG_SYSCTL
603
604static int sysctl_snat_reroute(struct sk_buff *skb)
605{
606 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
607 return ipvs->sysctl_snat_reroute;
608}
609
0cfa558e
SH
610static int sysctl_nat_icmp_send(struct net *net)
611{
612 struct netns_ipvs *ipvs = net_ipvs(net);
613 return ipvs->sysctl_nat_icmp_send;
614}
615
84b3cee3
SH
616#else
617
618static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
0cfa558e 619static int sysctl_nat_icmp_send(struct net *net) { return 0; }
84b3cee3
SH
620
621#endif
622
b1550f22 623__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
1da177e4 624{
d3bc23e7 625 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
1da177e4
LT
626}
627
1ca5bb54
JA
628static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
629{
630 if (NF_INET_LOCAL_IN == hooknum)
631 return IP_DEFRAG_VS_IN;
632 if (NF_INET_FORWARD == hooknum)
633 return IP_DEFRAG_VS_FWD;
634 return IP_DEFRAG_VS_OUT;
635}
636
776c729e 637static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
1da177e4 638{
776c729e
HX
639 int err = ip_defrag(skb, user);
640
641 if (!err)
eddc9ec5 642 ip_send_check(ip_hdr(skb));
776c729e
HX
643
644 return err;
1da177e4
LT
645}
646
2a3b791e
JV
647#ifdef CONFIG_IP_VS_IPV6
648static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
649{
650 /* TODO IPv6: Find out what to do here for IPv6 */
651 return 0;
652}
653#endif
654
ba4fd7e9
SH
655static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
656{
ba4fd7e9
SH
657#ifdef CONFIG_IP_VS_IPV6
658 if (af == AF_INET6) {
84b3cee3 659 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
ba4fd7e9
SH
660 return 1;
661 } else
662#endif
84b3cee3 663 if ((sysctl_snat_reroute(skb) ||
ba4fd7e9
SH
664 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
665 ip_route_me_harder(skb, RTN_LOCAL) != 0)
666 return 1;
667
668 return 0;
669}
670
1da177e4
LT
671/*
672 * Packet has been made sufficiently writable in caller
673 * - inout: 1=in->out, 0=out->in
674 */
675void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
676 struct ip_vs_conn *cp, int inout)
677{
eddc9ec5 678 struct iphdr *iph = ip_hdr(skb);
1da177e4 679 unsigned int icmp_offset = iph->ihl*4;
d56f90a7
ACM
680 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
681 icmp_offset);
1da177e4
LT
682 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
683
684 if (inout) {
e7ade46a 685 iph->saddr = cp->vaddr.ip;
1da177e4 686 ip_send_check(iph);
e7ade46a 687 ciph->daddr = cp->vaddr.ip;
1da177e4
LT
688 ip_send_check(ciph);
689 } else {
e7ade46a 690 iph->daddr = cp->daddr.ip;
1da177e4 691 ip_send_check(iph);
e7ade46a 692 ciph->saddr = cp->daddr.ip;
1da177e4
LT
693 ip_send_check(ciph);
694 }
695
2906f66a
VMR
696 /* the TCP/UDP/SCTP port */
697 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
698 IPPROTO_SCTP == ciph->protocol) {
014d730d 699 __be16 *ports = (void *)ciph + ciph->ihl*4;
1da177e4
LT
700
701 if (inout)
702 ports[1] = cp->vport;
703 else
704 ports[0] = cp->dport;
705 }
706
707 /* And finally the ICMP checksum */
708 icmph->checksum = 0;
709 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
710 skb->ip_summed = CHECKSUM_UNNECESSARY;
711
712 if (inout)
0d79641a 713 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
714 "Forwarding altered outgoing ICMP");
715 else
0d79641a 716 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
717 "Forwarding altered incoming ICMP");
718}
719
b3cdd2a7
JV
720#ifdef CONFIG_IP_VS_IPV6
721void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
722 struct ip_vs_conn *cp, int inout)
723{
724 struct ipv6hdr *iph = ipv6_hdr(skb);
725 unsigned int icmp_offset = sizeof(struct ipv6hdr);
726 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
727 icmp_offset);
728 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
729
730 if (inout) {
731 iph->saddr = cp->vaddr.in6;
732 ciph->daddr = cp->vaddr.in6;
733 } else {
734 iph->daddr = cp->daddr.in6;
735 ciph->saddr = cp->daddr.in6;
736 }
737
2906f66a
VMR
738 /* the TCP/UDP/SCTP port */
739 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
740 IPPROTO_SCTP == ciph->nexthdr) {
b3cdd2a7
JV
741 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
742
743 if (inout)
744 ports[1] = cp->vport;
745 else
746 ports[0] = cp->dport;
747 }
748
749 /* And finally the ICMP checksum */
8870f842
SH
750 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
751 skb->len - icmp_offset,
752 IPPROTO_ICMPV6, 0);
753 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
754 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
755 skb->ip_summed = CHECKSUM_PARTIAL;
b3cdd2a7
JV
756
757 if (inout)
0d79641a
JA
758 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
759 (void *)ciph - (void *)iph,
760 "Forwarding altered outgoing ICMPv6");
b3cdd2a7 761 else
0d79641a
JA
762 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
763 (void *)ciph - (void *)iph,
764 "Forwarding altered incoming ICMPv6");
b3cdd2a7
JV
765}
766#endif
767
4856c84c 768/* Handle relevant response ICMP messages - forward to the right
6cb90db5 769 * destination host.
4856c84c 770 */
f2428ed5
SH
771static int handle_response_icmp(int af, struct sk_buff *skb,
772 union nf_inet_addr *snet,
773 __u8 protocol, struct ip_vs_conn *cp,
4856c84c
MT
774 struct ip_vs_protocol *pp,
775 unsigned int offset, unsigned int ihl)
776{
777 unsigned int verdict = NF_DROP;
778
779 if (IP_VS_FWD_METHOD(cp) != 0) {
1e3e238e
HE
780 pr_err("shouldn't reach here, because the box is on the "
781 "half connection in the tun/dr module.\n");
4856c84c
MT
782 }
783
784 /* Ensure the checksum is correct */
785 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
786 /* Failed checksum! */
f2428ed5
SH
787 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
788 IP_VS_DBG_ADDR(af, snet));
4856c84c
MT
789 goto out;
790 }
791
2906f66a
VMR
792 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
793 IPPROTO_SCTP == protocol)
4856c84c
MT
794 offset += 2 * sizeof(__u16);
795 if (!skb_make_writable(skb, offset))
796 goto out;
797
f2428ed5
SH
798#ifdef CONFIG_IP_VS_IPV6
799 if (af == AF_INET6)
800 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
801 else
802#endif
803 ip_vs_nat_icmp(skb, pp, cp, 1);
4856c84c 804
ba4fd7e9
SH
805 if (ip_vs_route_me_harder(af, skb))
806 goto out;
f5a41847 807
4856c84c
MT
808 /* do the statistics and put it back */
809 ip_vs_out_stats(cp, skb);
810
cf356d69 811 skb->ipvs_property = 1;
f4bc17cd 812 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 813 ip_vs_notrack(skb);
f4bc17cd
JA
814 else
815 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
816 verdict = NF_ACCEPT;
817
818out:
819 __ip_vs_conn_put(cp);
820
821 return verdict;
822}
823
1da177e4
LT
824/*
825 * Handle ICMP messages in the inside-to-outside direction (outgoing).
4856c84c 826 * Find any that might be relevant, check against existing connections.
1da177e4 827 * Currently handles error types - unreachable, quench, ttl exceeded.
1da177e4 828 */
1ca5bb54
JA
829static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
830 unsigned int hooknum)
1da177e4 831{
1da177e4
LT
832 struct iphdr *iph;
833 struct icmphdr _icmph, *ic;
834 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 835 struct ip_vs_iphdr ciph;
1da177e4
LT
836 struct ip_vs_conn *cp;
837 struct ip_vs_protocol *pp;
4856c84c 838 unsigned int offset, ihl;
f2428ed5 839 union nf_inet_addr snet;
1da177e4
LT
840
841 *related = 1;
842
843 /* reassemble IP fragments */
eddc9ec5 844 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
1ca5bb54 845 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1da177e4 846 return NF_STOLEN;
1da177e4
LT
847 }
848
eddc9ec5 849 iph = ip_hdr(skb);
1da177e4
LT
850 offset = ihl = iph->ihl * 4;
851 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
852 if (ic == NULL)
853 return NF_DROP;
854
14d5e834 855 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 856 ic->type, ntohs(icmp_id(ic)),
14d5e834 857 &iph->saddr, &iph->daddr);
1da177e4
LT
858
859 /*
860 * Work through seeing if this is for us.
861 * These checks are supposed to be in an order that means easy
862 * things are checked first to speed up processing.... however
863 * this means that some packets will manage to get a long way
864 * down this stack and then be rejected, but that's life.
865 */
866 if ((ic->type != ICMP_DEST_UNREACH) &&
867 (ic->type != ICMP_SOURCE_QUENCH) &&
868 (ic->type != ICMP_TIME_EXCEEDED)) {
869 *related = 0;
870 return NF_ACCEPT;
871 }
872
873 /* Now find the contained IP header */
874 offset += sizeof(_icmph);
875 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
876 if (cih == NULL)
877 return NF_ACCEPT; /* The packet looks wrong, ignore */
878
879 pp = ip_vs_proto_get(cih->protocol);
880 if (!pp)
881 return NF_ACCEPT;
882
883 /* Is the embedded protocol header present? */
4412ec49 884 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
885 pp->dont_defrag))
886 return NF_ACCEPT;
887
0d79641a
JA
888 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
889 "Checking outgoing ICMP for");
1da177e4
LT
890
891 offset += cih->ihl * 4;
892
51ef348b 893 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 894 /* The embedded headers contain source and dest in reverse order */
9330419d 895 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
1da177e4
LT
896 if (!cp)
897 return NF_ACCEPT;
898
f2428ed5
SH
899 snet.ip = iph->saddr;
900 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
901 pp, offset, ihl);
1da177e4
LT
902}
903
2a3b791e 904#ifdef CONFIG_IP_VS_IPV6
1ca5bb54
JA
905static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
906 unsigned int hooknum)
2a3b791e
JV
907{
908 struct ipv6hdr *iph;
909 struct icmp6hdr _icmph, *ic;
910 struct ipv6hdr _ciph, *cih; /* The ip header contained
911 within the ICMP */
912 struct ip_vs_iphdr ciph;
913 struct ip_vs_conn *cp;
914 struct ip_vs_protocol *pp;
f2428ed5
SH
915 unsigned int offset;
916 union nf_inet_addr snet;
2a3b791e
JV
917
918 *related = 1;
919
920 /* reassemble IP fragments */
921 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1ca5bb54 922 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
2a3b791e
JV
923 return NF_STOLEN;
924 }
925
926 iph = ipv6_hdr(skb);
927 offset = sizeof(struct ipv6hdr);
928 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
929 if (ic == NULL)
930 return NF_DROP;
931
5b095d98 932 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
2a3b791e 933 ic->icmp6_type, ntohs(icmpv6_id(ic)),
38ff4fa4 934 &iph->saddr, &iph->daddr);
2a3b791e
JV
935
936 /*
937 * Work through seeing if this is for us.
938 * These checks are supposed to be in an order that means easy
939 * things are checked first to speed up processing.... however
940 * this means that some packets will manage to get a long way
941 * down this stack and then be rejected, but that's life.
942 */
943 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
944 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
945 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
946 *related = 0;
947 return NF_ACCEPT;
948 }
949
950 /* Now find the contained IP header */
951 offset += sizeof(_icmph);
952 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
953 if (cih == NULL)
954 return NF_ACCEPT; /* The packet looks wrong, ignore */
955
956 pp = ip_vs_proto_get(cih->nexthdr);
957 if (!pp)
958 return NF_ACCEPT;
959
960 /* Is the embedded protocol header present? */
961 /* TODO: we don't support fragmentation at the moment anyways */
962 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
963 return NF_ACCEPT;
964
0d79641a
JA
965 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
966 "Checking outgoing ICMPv6 for");
2a3b791e
JV
967
968 offset += sizeof(struct ipv6hdr);
969
970 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
971 /* The embedded headers contain source and dest in reverse order */
9330419d 972 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
2a3b791e
JV
973 if (!cp)
974 return NF_ACCEPT;
975
178f5e49 976 ipv6_addr_copy(&snet.in6, &iph->saddr);
f2428ed5
SH
977 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
978 pp, offset, sizeof(struct ipv6hdr));
2a3b791e
JV
979}
980#endif
981
2906f66a
VMR
982/*
983 * Check if sctp chunc is ABORT chunk
984 */
985static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
986{
987 sctp_chunkhdr_t *sch, schunk;
988 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
989 sizeof(schunk), &schunk);
990 if (sch == NULL)
991 return 0;
992 if (sch->type == SCTP_CID_ABORT)
993 return 1;
994 return 0;
995}
996
2a3b791e 997static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1da177e4
LT
998{
999 struct tcphdr _tcph, *th;
1000
2a3b791e 1001 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1da177e4
LT
1002 if (th == NULL)
1003 return 0;
1004 return th->rst;
1005}
1006
4856c84c 1007/* Handle response packets: rewrite addresses and send away...
4856c84c
MT
1008 */
1009static unsigned int
9330419d 1010handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
4856c84c
MT
1011 struct ip_vs_conn *cp, int ihl)
1012{
9330419d
HS
1013 struct ip_vs_protocol *pp = pd->pp;
1014
0d79641a 1015 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
4856c84c
MT
1016
1017 if (!skb_make_writable(skb, ihl))
1018 goto drop;
1019
1020 /* mangle the packet */
1021 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
1022 goto drop;
1023
1024#ifdef CONFIG_IP_VS_IPV6
1025 if (af == AF_INET6)
1026 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1027 else
1028#endif
1029 {
1030 ip_hdr(skb)->saddr = cp->vaddr.ip;
1031 ip_send_check(ip_hdr(skb));
1032 }
1033
8a803040
JA
1034 /*
1035 * nf_iterate does not expect change in the skb->dst->dev.
1036 * It looks like it is not fatal to enable this code for hooks
1037 * where our handlers are at the end of the chain list and
1038 * when all next handlers use skb->dst->dev and not outdev.
1039 * It will definitely route properly the inout NAT traffic
1040 * when multiple paths are used.
1041 */
1042
4856c84c
MT
1043 /* For policy routing, packets originating from this
1044 * machine itself may be routed differently to packets
1045 * passing through. We want this packet to be routed as
1046 * if it came from this machine itself. So re-compute
1047 * the routing information.
1048 */
ba4fd7e9
SH
1049 if (ip_vs_route_me_harder(af, skb))
1050 goto drop;
4856c84c 1051
0d79641a 1052 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
4856c84c
MT
1053
1054 ip_vs_out_stats(cp, skb);
9330419d 1055 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
cf356d69 1056 skb->ipvs_property = 1;
f4bc17cd 1057 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 1058 ip_vs_notrack(skb);
f4bc17cd
JA
1059 else
1060 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
1061 ip_vs_conn_put(cp);
1062
4856c84c
MT
1063 LeaveFunction(11);
1064 return NF_ACCEPT;
1065
1066drop:
1067 ip_vs_conn_put(cp);
1068 kfree_skb(skb);
f4bc17cd 1069 LeaveFunction(11);
4856c84c
MT
1070 return NF_STOLEN;
1071}
1072
1da177e4 1073/*
4856c84c 1074 * Check if outgoing packet belongs to the established ip_vs_conn.
1da177e4
LT
1075 */
1076static unsigned int
fc604767 1077ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1078{
fc723250 1079 struct net *net = NULL;
51ef348b 1080 struct ip_vs_iphdr iph;
1da177e4 1081 struct ip_vs_protocol *pp;
9330419d 1082 struct ip_vs_proto_data *pd;
1da177e4 1083 struct ip_vs_conn *cp;
1da177e4
LT
1084
1085 EnterFunction(11);
1086
fc604767 1087 /* Already marked as IPVS request or reply? */
6869c4d8 1088 if (skb->ipvs_property)
1da177e4
LT
1089 return NF_ACCEPT;
1090
fc604767
JA
1091 /* Bad... Do not break raw sockets */
1092 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1093 af == AF_INET)) {
1094 struct sock *sk = skb->sk;
1095 struct inet_sock *inet = inet_sk(skb->sk);
1096
1097 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1098 return NF_ACCEPT;
1099 }
1100
1101 if (unlikely(!skb_dst(skb)))
1102 return NF_ACCEPT;
1103
fc723250 1104 net = skb_net(skb);
2a3b791e
JV
1105 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1106#ifdef CONFIG_IP_VS_IPV6
1107 if (af == AF_INET6) {
1108 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54
JA
1109 int related;
1110 int verdict = ip_vs_out_icmp_v6(skb, &related,
1111 hooknum);
1da177e4 1112
f5a41847 1113 if (related)
2a3b791e
JV
1114 return verdict;
1115 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1116 }
1117 } else
1118#endif
1119 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54
JA
1120 int related;
1121 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
2a3b791e 1122
f5a41847 1123 if (related)
2a3b791e
JV
1124 return verdict;
1125 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1126 }
1da177e4 1127
9330419d
HS
1128 pd = ip_vs_proto_data_get(net, iph.protocol);
1129 if (unlikely(!pd))
1da177e4 1130 return NF_ACCEPT;
9330419d 1131 pp = pd->pp;
1da177e4
LT
1132
1133 /* reassemble IP fragments */
2a3b791e
JV
1134#ifdef CONFIG_IP_VS_IPV6
1135 if (af == AF_INET6) {
1ca5bb54
JA
1136 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1137 if (ip_vs_gather_frags_v6(skb,
1138 ip_vs_defrag_user(hooknum)))
1139 return NF_STOLEN;
2a3b791e 1140 }
1ca5bb54
JA
1141
1142 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
2a3b791e
JV
1143 } else
1144#endif
1145 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1146 !pp->dont_defrag)) {
1ca5bb54
JA
1147 if (ip_vs_gather_frags(skb,
1148 ip_vs_defrag_user(hooknum)))
2a3b791e
JV
1149 return NF_STOLEN;
1150
1151 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1152 }
1da177e4
LT
1153
1154 /*
1155 * Check if the packet belongs to an existing entry
1156 */
9330419d 1157 cp = pp->conn_out_get(af, skb, &iph, iph.len, 0);
1da177e4 1158
cb59155f 1159 if (likely(cp))
9330419d 1160 return handle_response(af, skb, pd, cp, iph.len);
0cfa558e 1161 if (sysctl_nat_icmp_send(net) &&
cb59155f
JA
1162 (pp->protocol == IPPROTO_TCP ||
1163 pp->protocol == IPPROTO_UDP ||
1164 pp->protocol == IPPROTO_SCTP)) {
1165 __be16 _ports[2], *pptr;
1166
1167 pptr = skb_header_pointer(skb, iph.len,
1168 sizeof(_ports), _ports);
1169 if (pptr == NULL)
1170 return NF_ACCEPT; /* Not for me */
fc723250 1171 if (ip_vs_lookup_real_service(net, af, iph.protocol,
cb59155f
JA
1172 &iph.saddr,
1173 pptr[0])) {
1174 /*
1175 * Notify the real server: there is no
1176 * existing entry if it is not RST
1177 * packet or not TCP packet.
1178 */
1179 if ((iph.protocol != IPPROTO_TCP &&
1180 iph.protocol != IPPROTO_SCTP)
1181 || ((iph.protocol == IPPROTO_TCP
1182 && !is_tcp_reset(skb, iph.len))
1183 || (iph.protocol == IPPROTO_SCTP
1184 && !is_sctp_abort(skb,
1185 iph.len)))) {
2a3b791e 1186#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
1187 if (af == AF_INET6) {
1188 struct net *net =
1189 dev_net(skb_dst(skb)->dev);
1190
1191 if (!skb->dev)
1192 skb->dev = net->loopback_dev;
1193 icmpv6_send(skb,
1194 ICMPV6_DEST_UNREACH,
1195 ICMPV6_PORT_UNREACH,
1196 0);
1197 } else
2a3b791e 1198#endif
cb59155f
JA
1199 icmp_send(skb,
1200 ICMP_DEST_UNREACH,
1201 ICMP_PORT_UNREACH, 0);
1202 return NF_DROP;
1da177e4
LT
1203 }
1204 }
1da177e4 1205 }
0d79641a 1206 IP_VS_DBG_PKT(12, af, pp, skb, 0,
cb59155f
JA
1207 "ip_vs_out: packet continues traversal as normal");
1208 return NF_ACCEPT;
1da177e4
LT
1209}
1210
fc604767 1211/*
cb59155f
JA
1212 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1213 * used only for VS/NAT.
fc604767
JA
1214 * Check if packet is reply for established ip_vs_conn.
1215 */
1216static unsigned int
1217ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1218 const struct net_device *in, const struct net_device *out,
1219 int (*okfn)(struct sk_buff *))
1220{
1221 return ip_vs_out(hooknum, skb, AF_INET);
1222}
1223
1224/*
1225 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1226 * Check if packet is reply for established ip_vs_conn.
1227 */
1228static unsigned int
1229ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1230 const struct net_device *in, const struct net_device *out,
1231 int (*okfn)(struct sk_buff *))
1232{
1233 unsigned int verdict;
1234
1235 /* Disable BH in LOCAL_OUT until all places are fixed */
1236 local_bh_disable();
1237 verdict = ip_vs_out(hooknum, skb, AF_INET);
1238 local_bh_enable();
1239 return verdict;
1240}
1241
1242#ifdef CONFIG_IP_VS_IPV6
1243
1244/*
cb59155f
JA
1245 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1246 * used only for VS/NAT.
fc604767
JA
1247 * Check if packet is reply for established ip_vs_conn.
1248 */
1249static unsigned int
1250ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1251 const struct net_device *in, const struct net_device *out,
1252 int (*okfn)(struct sk_buff *))
1253{
1254 return ip_vs_out(hooknum, skb, AF_INET6);
1255}
1256
1257/*
1258 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1259 * Check if packet is reply for established ip_vs_conn.
1260 */
1261static unsigned int
1262ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1263 const struct net_device *in, const struct net_device *out,
1264 int (*okfn)(struct sk_buff *))
1265{
1266 unsigned int verdict;
1267
1268 /* Disable BH in LOCAL_OUT until all places are fixed */
1269 local_bh_disable();
1270 verdict = ip_vs_out(hooknum, skb, AF_INET6);
1271 local_bh_enable();
1272 return verdict;
1273}
1274
1275#endif
1da177e4
LT
1276
1277/*
1278 * Handle ICMP messages in the outside-to-inside direction (incoming).
1279 * Find any that might be relevant, check against existing connections,
1280 * forward to the right destination host if relevant.
1281 * Currently handles error types - unreachable, quench, ttl exceeded.
1282 */
e905a9ed 1283static int
3db05fea 1284ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1da177e4 1285{
9330419d 1286 struct net *net = NULL;
1da177e4
LT
1287 struct iphdr *iph;
1288 struct icmphdr _icmph, *ic;
1289 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 1290 struct ip_vs_iphdr ciph;
1da177e4
LT
1291 struct ip_vs_conn *cp;
1292 struct ip_vs_protocol *pp;
9330419d 1293 struct ip_vs_proto_data *pd;
1da177e4
LT
1294 unsigned int offset, ihl, verdict;
1295
1296 *related = 1;
1297
1298 /* reassemble IP fragments */
eddc9ec5 1299 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
1ca5bb54 1300 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1da177e4 1301 return NF_STOLEN;
1da177e4
LT
1302 }
1303
eddc9ec5 1304 iph = ip_hdr(skb);
1da177e4
LT
1305 offset = ihl = iph->ihl * 4;
1306 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1307 if (ic == NULL)
1308 return NF_DROP;
1309
14d5e834 1310 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 1311 ic->type, ntohs(icmp_id(ic)),
14d5e834 1312 &iph->saddr, &iph->daddr);
1da177e4
LT
1313
1314 /*
1315 * Work through seeing if this is for us.
1316 * These checks are supposed to be in an order that means easy
1317 * things are checked first to speed up processing.... however
1318 * this means that some packets will manage to get a long way
1319 * down this stack and then be rejected, but that's life.
1320 */
1321 if ((ic->type != ICMP_DEST_UNREACH) &&
1322 (ic->type != ICMP_SOURCE_QUENCH) &&
1323 (ic->type != ICMP_TIME_EXCEEDED)) {
1324 *related = 0;
1325 return NF_ACCEPT;
1326 }
1327
1328 /* Now find the contained IP header */
1329 offset += sizeof(_icmph);
1330 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1331 if (cih == NULL)
1332 return NF_ACCEPT; /* The packet looks wrong, ignore */
1333
9330419d
HS
1334 net = skb_net(skb);
1335 pd = ip_vs_proto_data_get(net, cih->protocol);
1336 if (!pd)
1da177e4 1337 return NF_ACCEPT;
9330419d 1338 pp = pd->pp;
1da177e4
LT
1339
1340 /* Is the embedded protocol header present? */
4412ec49 1341 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
1342 pp->dont_defrag))
1343 return NF_ACCEPT;
1344
0d79641a
JA
1345 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1346 "Checking incoming ICMP for");
1da177e4
LT
1347
1348 offset += cih->ihl * 4;
1349
51ef348b 1350 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 1351 /* The embedded headers contain source and dest in reverse order */
9330419d 1352 cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1);
6cb90db5 1353 if (!cp)
1da177e4
LT
1354 return NF_ACCEPT;
1355
1356 verdict = NF_DROP;
1357
1358 /* Ensure the checksum is correct */
60476372 1359 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4 1360 /* Failed checksum! */
14d5e834
HH
1361 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1362 &iph->saddr);
1da177e4
LT
1363 goto out;
1364 }
1365
1366 /* do the statistics and put it back */
1367 ip_vs_in_stats(cp, skb);
1368 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1369 offset += 2 * sizeof(__u16);
1370 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
489fdeda
JA
1371 /* LOCALNODE from FORWARD hook is not supported */
1372 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1373 skb_rtable(skb)->rt_flags & RTCF_LOCAL) {
1374 IP_VS_DBG(1, "%s(): "
1375 "local delivery to %pI4 but in FORWARD\n",
1376 __func__, &skb_rtable(skb)->rt_dst);
1377 verdict = NF_DROP;
1378 }
1da177e4
LT
1379
1380 out:
1381 __ip_vs_conn_put(cp);
1382
1383 return verdict;
1384}
1385
2a3b791e
JV
1386#ifdef CONFIG_IP_VS_IPV6
1387static int
1388ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1389{
9330419d 1390 struct net *net = NULL;
2a3b791e
JV
1391 struct ipv6hdr *iph;
1392 struct icmp6hdr _icmph, *ic;
1393 struct ipv6hdr _ciph, *cih; /* The ip header contained
1394 within the ICMP */
1395 struct ip_vs_iphdr ciph;
1396 struct ip_vs_conn *cp;
1397 struct ip_vs_protocol *pp;
9330419d 1398 struct ip_vs_proto_data *pd;
2a3b791e 1399 unsigned int offset, verdict;
489fdeda 1400 struct rt6_info *rt;
2a3b791e
JV
1401
1402 *related = 1;
1403
1404 /* reassemble IP fragments */
1405 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1ca5bb54 1406 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
2a3b791e
JV
1407 return NF_STOLEN;
1408 }
1409
1410 iph = ipv6_hdr(skb);
1411 offset = sizeof(struct ipv6hdr);
1412 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1413 if (ic == NULL)
1414 return NF_DROP;
1415
5b095d98 1416 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
2a3b791e 1417 ic->icmp6_type, ntohs(icmpv6_id(ic)),
38ff4fa4 1418 &iph->saddr, &iph->daddr);
2a3b791e
JV
1419
1420 /*
1421 * Work through seeing if this is for us.
1422 * These checks are supposed to be in an order that means easy
1423 * things are checked first to speed up processing.... however
1424 * this means that some packets will manage to get a long way
1425 * down this stack and then be rejected, but that's life.
1426 */
1427 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1428 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1429 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1430 *related = 0;
1431 return NF_ACCEPT;
1432 }
1433
1434 /* Now find the contained IP header */
1435 offset += sizeof(_icmph);
1436 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1437 if (cih == NULL)
1438 return NF_ACCEPT; /* The packet looks wrong, ignore */
1439
9330419d
HS
1440 net = skb_net(skb);
1441 pd = ip_vs_proto_data_get(net, cih->nexthdr);
1442 if (!pd)
2a3b791e 1443 return NF_ACCEPT;
9330419d 1444 pp = pd->pp;
2a3b791e
JV
1445
1446 /* Is the embedded protocol header present? */
1447 /* TODO: we don't support fragmentation at the moment anyways */
1448 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1449 return NF_ACCEPT;
1450
0d79641a
JA
1451 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1452 "Checking incoming ICMPv6 for");
2a3b791e
JV
1453
1454 offset += sizeof(struct ipv6hdr);
1455
1456 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1457 /* The embedded headers contain source and dest in reverse order */
9330419d 1458 cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1);
6cb90db5 1459 if (!cp)
2a3b791e
JV
1460 return NF_ACCEPT;
1461
1462 verdict = NF_DROP;
1463
1464 /* do the statistics and put it back */
1465 ip_vs_in_stats(cp, skb);
2906f66a
VMR
1466 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1467 IPPROTO_SCTP == cih->nexthdr)
2a3b791e
JV
1468 offset += 2 * sizeof(__u16);
1469 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
489fdeda
JA
1470 /* LOCALNODE from FORWARD hook is not supported */
1471 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1472 (rt = (struct rt6_info *) skb_dst(skb)) &&
1473 rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) {
1474 IP_VS_DBG(1, "%s(): "
1475 "local delivery to %pI6 but in FORWARD\n",
1476 __func__, &rt->rt6i_dst);
1477 verdict = NF_DROP;
1478 }
2a3b791e
JV
1479
1480 __ip_vs_conn_put(cp);
1481
1482 return verdict;
1483}
1484#endif
1485
1486
1da177e4
LT
1487/*
1488 * Check if it's for virtual services, look it up,
1489 * and send it on its way...
1490 */
1491static unsigned int
cb59155f 1492ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1493{
f131315f 1494 struct net *net;
51ef348b 1495 struct ip_vs_iphdr iph;
1da177e4 1496 struct ip_vs_protocol *pp;
9330419d 1497 struct ip_vs_proto_data *pd;
1da177e4 1498 struct ip_vs_conn *cp;
cb59155f 1499 int ret, restart, pkts;
f131315f 1500 struct netns_ipvs *ipvs;
2a3b791e 1501
fc604767
JA
1502 /* Already marked as IPVS request or reply? */
1503 if (skb->ipvs_property)
1504 return NF_ACCEPT;
1505
1da177e4 1506 /*
cb59155f
JA
1507 * Big tappo:
1508 * - remote client: only PACKET_HOST
1509 * - route: used for struct net when skb->dev is unset
1da177e4 1510 */
cb59155f
JA
1511 if (unlikely((skb->pkt_type != PACKET_HOST &&
1512 hooknum != NF_INET_LOCAL_OUT) ||
1513 !skb_dst(skb))) {
1514 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1515 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1516 " ignored in hook %u\n",
1517 skb->pkt_type, iph.protocol,
1518 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1da177e4
LT
1519 return NF_ACCEPT;
1520 }
cb59155f
JA
1521 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1522
1523 /* Bad... Do not break raw sockets */
1524 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1525 af == AF_INET)) {
1526 struct sock *sk = skb->sk;
1527 struct inet_sock *inet = inet_sk(skb->sk);
1528
1529 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1530 return NF_ACCEPT;
1531 }
1da177e4 1532
94b26551
JV
1533#ifdef CONFIG_IP_VS_IPV6
1534 if (af == AF_INET6) {
1535 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54
JA
1536 int related;
1537 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
1da177e4 1538
94b26551
JV
1539 if (related)
1540 return verdict;
1541 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1542 }
1543 } else
1544#endif
1545 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54
JA
1546 int related;
1547 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
94b26551
JV
1548
1549 if (related)
1550 return verdict;
1551 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1552 }
1da177e4 1553
9330419d 1554 net = skb_net(skb);
1da177e4 1555 /* Protocol supported? */
9330419d
HS
1556 pd = ip_vs_proto_data_get(net, iph.protocol);
1557 if (unlikely(!pd))
1da177e4 1558 return NF_ACCEPT;
9330419d 1559 pp = pd->pp;
1da177e4
LT
1560 /*
1561 * Check if the packet belongs to an existing connection entry
1562 */
9330419d 1563 cp = pp->conn_in_get(af, skb, &iph, iph.len, 0);
1da177e4
LT
1564
1565 if (unlikely(!cp)) {
1566 int v;
1567
9330419d 1568 if (!pp->conn_schedule(af, skb, pd, &v, &cp))
1da177e4
LT
1569 return v;
1570 }
1571
1572 if (unlikely(!cp)) {
1573 /* sorry, all this trouble for a no-hit :) */
0d79641a 1574 IP_VS_DBG_PKT(12, af, pp, skb, 0,
cb59155f 1575 "ip_vs_in: packet continues traversal as normal");
1da177e4
LT
1576 return NF_ACCEPT;
1577 }
1578
0d79641a 1579 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
f131315f
HS
1580 net = skb_net(skb);
1581 ipvs = net_ipvs(net);
1da177e4
LT
1582 /* Check the server status */
1583 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1584 /* the destination server is not available */
1585
a0840e2e 1586 if (ipvs->sysctl_expire_nodest_conn) {
1da177e4
LT
1587 /* try to expire the connection immediately */
1588 ip_vs_conn_expire_now(cp);
1da177e4 1589 }
dc8103f2
JA
1590 /* don't restart its timer, and silently
1591 drop the packet. */
1592 __ip_vs_conn_put(cp);
1da177e4
LT
1593 return NF_DROP;
1594 }
1595
1596 ip_vs_in_stats(cp, skb);
9330419d 1597 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1da177e4
LT
1598 if (cp->packet_xmit)
1599 ret = cp->packet_xmit(skb, cp, pp);
1600 /* do not touch skb anymore */
1601 else {
1602 IP_VS_DBG_RL("warning: packet_xmit is null");
1603 ret = NF_ACCEPT;
1604 }
1605
efac5276
RB
1606 /* Increase its packet counter and check if it is needed
1607 * to be synchronized
1608 *
1609 * Sync connection if it is about to close to
1610 * encorage the standby servers to update the connections timeout
986a0757
HS
1611 *
1612 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
efac5276 1613 */
f131315f 1614
986a0757 1615 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
59e0350e 1616 pkts = sysctl_sync_threshold(ipvs);
986a0757
HS
1617 else
1618 pkts = atomic_add_return(1, &cp->in_pkts);
1619
f131315f 1620 if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
2906f66a
VMR
1621 cp->protocol == IPPROTO_SCTP) {
1622 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
59e0350e
SH
1623 (pkts % sysctl_sync_period(ipvs)
1624 == sysctl_sync_threshold(ipvs))) ||
2906f66a
VMR
1625 (cp->old_state != cp->state &&
1626 ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1627 (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1628 (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
f131315f 1629 ip_vs_sync_conn(net, cp);
2906f66a
VMR
1630 goto out;
1631 }
1632 }
1633
8ed2163f 1634 /* Keep this block last: TCP and others with pp->num_states <= 1 */
f131315f 1635 else if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
efac5276
RB
1636 (((cp->protocol != IPPROTO_TCP ||
1637 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
59e0350e
SH
1638 (pkts % sysctl_sync_period(ipvs)
1639 == sysctl_sync_threshold(ipvs))) ||
efac5276
RB
1640 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1641 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
9abfe315 1642 (cp->state == IP_VS_TCP_S_CLOSE) ||
9d3a0de7
RB
1643 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1644 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
f131315f 1645 ip_vs_sync_conn(net, cp);
2906f66a 1646out:
efac5276 1647 cp->old_state = cp->state;
1da177e4
LT
1648
1649 ip_vs_conn_put(cp);
1650 return ret;
1651}
1652
cb59155f
JA
1653/*
1654 * AF_INET handler in NF_INET_LOCAL_IN chain
1655 * Schedule and forward packets from remote clients
1656 */
1657static unsigned int
1658ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1659 const struct net_device *in,
1660 const struct net_device *out,
1661 int (*okfn)(struct sk_buff *))
1662{
1663 return ip_vs_in(hooknum, skb, AF_INET);
1664}
1665
1666/*
1667 * AF_INET handler in NF_INET_LOCAL_OUT chain
1668 * Schedule and forward packets from local clients
1669 */
1670static unsigned int
1671ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1672 const struct net_device *in, const struct net_device *out,
1673 int (*okfn)(struct sk_buff *))
1674{
1675 unsigned int verdict;
1676
1677 /* Disable BH in LOCAL_OUT until all places are fixed */
1678 local_bh_disable();
1679 verdict = ip_vs_in(hooknum, skb, AF_INET);
1680 local_bh_enable();
1681 return verdict;
1682}
1683
1684#ifdef CONFIG_IP_VS_IPV6
1685
1686/*
1687 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1688 * Schedule and forward packets from remote clients
1689 */
1690static unsigned int
1691ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1692 const struct net_device *in,
1693 const struct net_device *out,
1694 int (*okfn)(struct sk_buff *))
1695{
1696 return ip_vs_in(hooknum, skb, AF_INET6);
1697}
1698
1699/*
1700 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1701 * Schedule and forward packets from local clients
1702 */
1703static unsigned int
1704ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1705 const struct net_device *in, const struct net_device *out,
1706 int (*okfn)(struct sk_buff *))
1707{
1708 unsigned int verdict;
1709
1710 /* Disable BH in LOCAL_OUT until all places are fixed */
1711 local_bh_disable();
1712 verdict = ip_vs_in(hooknum, skb, AF_INET6);
1713 local_bh_enable();
1714 return verdict;
1715}
1716
1717#endif
1718
1da177e4
LT
1719
1720/*
6e23ae2a 1721 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1da177e4
LT
1722 * related packets destined for 0.0.0.0/0.
1723 * When fwmark-based virtual service is used, such as transparent
1724 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1725 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
6e23ae2a 1726 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1da177e4
LT
1727 * and send them to ip_vs_in_icmp.
1728 */
1729static unsigned int
3db05fea 1730ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1731 const struct net_device *in, const struct net_device *out,
1732 int (*okfn)(struct sk_buff *))
1733{
1734 int r;
1735
3db05fea 1736 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
1737 return NF_ACCEPT;
1738
3db05fea 1739 return ip_vs_in_icmp(skb, &r, hooknum);
1da177e4
LT
1740}
1741
2a3b791e
JV
1742#ifdef CONFIG_IP_VS_IPV6
1743static unsigned int
1744ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1745 const struct net_device *in, const struct net_device *out,
1746 int (*okfn)(struct sk_buff *))
1747{
1748 int r;
1749
1750 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1751 return NF_ACCEPT;
1752
1753 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1754}
1755#endif
1756
1da177e4 1757
1999414a 1758static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
cb59155f
JA
1759 /* After packet filtering, change source only for VS/NAT */
1760 {
1761 .hook = ip_vs_reply4,
1762 .owner = THIS_MODULE,
1763 .pf = PF_INET,
1764 .hooknum = NF_INET_LOCAL_IN,
1765 .priority = 99,
1766 },
41c5b317
PM
1767 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1768 * or VS/NAT(change destination), so that filtering rules can be
1769 * applied to IPVS. */
1770 {
cb59155f 1771 .hook = ip_vs_remote_request4,
41c5b317
PM
1772 .owner = THIS_MODULE,
1773 .pf = PF_INET,
cb59155f
JA
1774 .hooknum = NF_INET_LOCAL_IN,
1775 .priority = 101,
41c5b317 1776 },
fc604767 1777 /* Before ip_vs_in, change source only for VS/NAT */
41c5b317 1778 {
fc604767 1779 .hook = ip_vs_local_reply4,
41c5b317
PM
1780 .owner = THIS_MODULE,
1781 .pf = PF_INET,
fc604767
JA
1782 .hooknum = NF_INET_LOCAL_OUT,
1783 .priority = -99,
41c5b317 1784 },
cb59155f
JA
1785 /* After mangle, schedule and forward local requests */
1786 {
1787 .hook = ip_vs_local_request4,
1788 .owner = THIS_MODULE,
1789 .pf = PF_INET,
1790 .hooknum = NF_INET_LOCAL_OUT,
1791 .priority = -98,
1792 },
41c5b317
PM
1793 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1794 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1795 {
1796 .hook = ip_vs_forward_icmp,
1797 .owner = THIS_MODULE,
1798 .pf = PF_INET,
cb59155f
JA
1799 .hooknum = NF_INET_FORWARD,
1800 .priority = 99,
41c5b317 1801 },
fc604767
JA
1802 /* After packet filtering, change source only for VS/NAT */
1803 {
1804 .hook = ip_vs_reply4,
1805 .owner = THIS_MODULE,
1806 .pf = PF_INET,
1807 .hooknum = NF_INET_FORWARD,
1808 .priority = 100,
1809 },
473b23d3 1810#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
1811 /* After packet filtering, change source only for VS/NAT */
1812 {
1813 .hook = ip_vs_reply6,
1814 .owner = THIS_MODULE,
1815 .pf = PF_INET6,
1816 .hooknum = NF_INET_LOCAL_IN,
1817 .priority = 99,
1818 },
473b23d3
JV
1819 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1820 * or VS/NAT(change destination), so that filtering rules can be
1821 * applied to IPVS. */
1822 {
cb59155f 1823 .hook = ip_vs_remote_request6,
473b23d3
JV
1824 .owner = THIS_MODULE,
1825 .pf = PF_INET6,
cb59155f
JA
1826 .hooknum = NF_INET_LOCAL_IN,
1827 .priority = 101,
473b23d3 1828 },
fc604767 1829 /* Before ip_vs_in, change source only for VS/NAT */
473b23d3 1830 {
fc604767 1831 .hook = ip_vs_local_reply6,
473b23d3 1832 .owner = THIS_MODULE,
fc604767
JA
1833 .pf = PF_INET,
1834 .hooknum = NF_INET_LOCAL_OUT,
1835 .priority = -99,
473b23d3 1836 },
cb59155f
JA
1837 /* After mangle, schedule and forward local requests */
1838 {
1839 .hook = ip_vs_local_request6,
1840 .owner = THIS_MODULE,
1841 .pf = PF_INET6,
1842 .hooknum = NF_INET_LOCAL_OUT,
1843 .priority = -98,
1844 },
473b23d3
JV
1845 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1846 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1847 {
1848 .hook = ip_vs_forward_icmp_v6,
1849 .owner = THIS_MODULE,
1850 .pf = PF_INET6,
cb59155f
JA
1851 .hooknum = NF_INET_FORWARD,
1852 .priority = 99,
473b23d3 1853 },
fc604767
JA
1854 /* After packet filtering, change source only for VS/NAT */
1855 {
1856 .hook = ip_vs_reply6,
1857 .owner = THIS_MODULE,
1858 .pf = PF_INET6,
1859 .hooknum = NF_INET_FORWARD,
1860 .priority = 100,
1861 },
473b23d3 1862#endif
1da177e4 1863};
61b1ab45
HS
1864/*
1865 * Initialize IP Virtual Server netns mem.
1866 */
1867static int __net_init __ip_vs_init(struct net *net)
1868{
1869 struct netns_ipvs *ipvs;
1870
61b1ab45
HS
1871 ipvs = net_generic(net, ip_vs_net_id);
1872 if (ipvs == NULL) {
1873 pr_err("%s(): no memory.\n", __func__);
1874 return -ENOMEM;
1875 }
f6340ee0 1876 ipvs->net = net;
61b1ab45
HS
1877 /* Counters used for creating unique names */
1878 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1879 atomic_inc(&ipvs_netns_cnt);
1880 net->ipvs = ipvs;
a870c8c5 1881 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
61b1ab45
HS
1882 sizeof(struct netns_ipvs), ipvs->gen);
1883 return 0;
1884}
1885
1886static void __net_exit __ip_vs_cleanup(struct net *net)
1887{
9f4e1ccd 1888 IP_VS_DBG(10, "ipvs netns %d released\n", net_ipvs(net)->gen);
61b1ab45
HS
1889}
1890
1891static struct pernet_operations ipvs_core_ops = {
1892 .init = __ip_vs_init,
1893 .exit = __ip_vs_cleanup,
1894 .id = &ip_vs_net_id,
1895 .size = sizeof(struct netns_ipvs),
1896};
1da177e4
LT
1897
1898/*
1899 * Initialize IP Virtual Server
1900 */
1901static int __init ip_vs_init(void)
1902{
1903 int ret;
1904
61b1ab45
HS
1905 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
1906 if (ret < 0)
1907 return ret;
a919cf4b 1908
61b1ab45 1909 ip_vs_estimator_init();
1da177e4
LT
1910 ret = ip_vs_control_init();
1911 if (ret < 0) {
1e3e238e 1912 pr_err("can't setup control.\n");
a919cf4b 1913 goto cleanup_estimator;
1da177e4
LT
1914 }
1915
1916 ip_vs_protocol_init();
1917
1918 ret = ip_vs_app_init();
1919 if (ret < 0) {
1e3e238e 1920 pr_err("can't setup application helper.\n");
1da177e4
LT
1921 goto cleanup_protocol;
1922 }
1923
1924 ret = ip_vs_conn_init();
1925 if (ret < 0) {
1e3e238e 1926 pr_err("can't setup connection table.\n");
1da177e4
LT
1927 goto cleanup_app;
1928 }
1929
61b1ab45
HS
1930 ret = ip_vs_sync_init();
1931 if (ret < 0) {
1932 pr_err("can't setup sync data.\n");
1933 goto cleanup_conn;
1934 }
1935
41c5b317 1936 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4 1937 if (ret < 0) {
1e3e238e 1938 pr_err("can't register hooks.\n");
61b1ab45 1939 goto cleanup_sync;
1da177e4
LT
1940 }
1941
1e3e238e 1942 pr_info("ipvs loaded.\n");
1da177e4
LT
1943 return ret;
1944
61b1ab45
HS
1945cleanup_sync:
1946 ip_vs_sync_cleanup();
1da177e4
LT
1947 cleanup_conn:
1948 ip_vs_conn_cleanup();
1949 cleanup_app:
1950 ip_vs_app_cleanup();
1951 cleanup_protocol:
1952 ip_vs_protocol_cleanup();
1953 ip_vs_control_cleanup();
a919cf4b
SW
1954 cleanup_estimator:
1955 ip_vs_estimator_cleanup();
61b1ab45 1956 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1da177e4
LT
1957 return ret;
1958}
1959
1960static void __exit ip_vs_cleanup(void)
1961{
41c5b317 1962 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
61b1ab45 1963 ip_vs_sync_cleanup();
1da177e4
LT
1964 ip_vs_conn_cleanup();
1965 ip_vs_app_cleanup();
1966 ip_vs_protocol_cleanup();
1967 ip_vs_control_cleanup();
a919cf4b 1968 ip_vs_estimator_cleanup();
61b1ab45 1969 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1e3e238e 1970 pr_info("ipvs unloaded.\n");
1da177e4
LT
1971}
1972
1973module_init(ip_vs_init);
1974module_exit(ip_vs_cleanup);
1975MODULE_LICENSE("GPL");
This page took 0.660418 seconds and 5 git commands to generate.