ipvs: Remove unused parameter from ip_vs_confirm_conntrack()
[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{
014d730d 502 __be16 _ports[2], *pptr;
28364a59 503 struct ip_vs_iphdr iph;
a7a86b86
SH
504#ifdef CONFIG_SYSCTL
505 struct net *net;
506 struct netns_ipvs *ipvs;
2a3b791e 507 int unicast;
a7a86b86 508#endif
9330419d 509
2a3b791e 510 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
1da177e4 511
28364a59 512 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
1da177e4
LT
513 if (pptr == NULL) {
514 ip_vs_service_put(svc);
515 return NF_DROP;
516 }
a7a86b86
SH
517
518#ifdef CONFIG_SYSCTL
4a98480b 519 net = skb_net(skb);
1da177e4 520
2a3b791e
JV
521#ifdef CONFIG_IP_VS_IPV6
522 if (svc->af == AF_INET6)
523 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
524 else
525#endif
4a98480b 526 unicast = (inet_addr_type(net, iph.daddr.ip) == RTN_UNICAST);
2a3b791e 527
1da177e4 528 /* if it is fwmark-based service, the cache_bypass sysctl is up
2a3b791e 529 and the destination is a non-local unicast, then create
1da177e4 530 a cache_bypass connection entry */
4a98480b 531 ipvs = net_ipvs(net);
a0840e2e 532 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
1da177e4
LT
533 int ret, cs;
534 struct ip_vs_conn *cp;
3575792e
JA
535 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
536 iph.protocol == IPPROTO_UDP)?
537 IP_VS_CONN_F_ONE_PACKET : 0;
dff630dd 538 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
1da177e4
LT
539
540 ip_vs_service_put(svc);
541
542 /* create a new connection entry */
1e3e238e 543 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
f11017ec
SH
544 {
545 struct ip_vs_conn_param p;
6e67e586 546 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
f11017ec
SH
547 &iph.saddr, pptr[0],
548 &iph.daddr, pptr[1], &p);
549 cp = ip_vs_conn_new(&p, &daddr, 0,
550 IP_VS_CONN_F_BYPASS | flags,
0e051e68 551 NULL, skb->mark);
f11017ec
SH
552 if (!cp)
553 return NF_DROP;
554 }
1da177e4
LT
555
556 /* statistics */
557 ip_vs_in_stats(cp, skb);
558
559 /* set state */
9330419d 560 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1da177e4
LT
561
562 /* transmit the first SYN packet */
9330419d 563 ret = cp->packet_xmit(skb, cp, pd->pp);
1da177e4
LT
564 /* do not touch skb anymore */
565
566 atomic_inc(&cp->in_pkts);
567 ip_vs_conn_put(cp);
568 return ret;
569 }
a7a86b86 570#endif
1da177e4
LT
571
572 /*
573 * When the virtual ftp service is presented, packets destined
574 * for other services on the VIP may get here (except services
575 * listed in the ipvs table), pass the packets, because it is
576 * not ipvs job to decide to drop the packets.
577 */
578 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
579 ip_vs_service_put(svc);
580 return NF_ACCEPT;
581 }
582
583 ip_vs_service_put(svc);
584
585 /*
586 * Notify the client that the destination is unreachable, and
587 * release the socket buffer.
588 * Since it is in IP layer, the TCP socket is not actually
589 * created, the TCP RST packet cannot be sent, instead that
590 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
591 */
2a3b791e 592#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
593 if (svc->af == AF_INET6) {
594 if (!skb->dev) {
595 struct net *net = dev_net(skb_dst(skb)->dev);
596
597 skb->dev = net->loopback_dev;
598 }
3ffe533c 599 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
cb59155f 600 } else
2a3b791e
JV
601#endif
602 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
603
1da177e4
LT
604 return NF_DROP;
605}
606
84b3cee3
SH
607#ifdef CONFIG_SYSCTL
608
609static int sysctl_snat_reroute(struct sk_buff *skb)
610{
611 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
612 return ipvs->sysctl_snat_reroute;
613}
614
0cfa558e
SH
615static int sysctl_nat_icmp_send(struct net *net)
616{
617 struct netns_ipvs *ipvs = net_ipvs(net);
618 return ipvs->sysctl_nat_icmp_send;
619}
620
71a8ab6c
SH
621static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
622{
623 return ipvs->sysctl_expire_nodest_conn;
624}
625
84b3cee3
SH
626#else
627
628static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
0cfa558e 629static int sysctl_nat_icmp_send(struct net *net) { return 0; }
71a8ab6c 630static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
84b3cee3
SH
631
632#endif
633
b1550f22 634__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
1da177e4 635{
d3bc23e7 636 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
1da177e4
LT
637}
638
1ca5bb54
JA
639static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
640{
641 if (NF_INET_LOCAL_IN == hooknum)
642 return IP_DEFRAG_VS_IN;
643 if (NF_INET_FORWARD == hooknum)
644 return IP_DEFRAG_VS_FWD;
645 return IP_DEFRAG_VS_OUT;
646}
647
776c729e 648static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
1da177e4 649{
776c729e
HX
650 int err = ip_defrag(skb, user);
651
652 if (!err)
eddc9ec5 653 ip_send_check(ip_hdr(skb));
776c729e
HX
654
655 return err;
1da177e4
LT
656}
657
2a3b791e
JV
658#ifdef CONFIG_IP_VS_IPV6
659static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
660{
661 /* TODO IPv6: Find out what to do here for IPv6 */
662 return 0;
663}
664#endif
665
ba4fd7e9
SH
666static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
667{
ba4fd7e9
SH
668#ifdef CONFIG_IP_VS_IPV6
669 if (af == AF_INET6) {
84b3cee3 670 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
ba4fd7e9
SH
671 return 1;
672 } else
673#endif
84b3cee3 674 if ((sysctl_snat_reroute(skb) ||
ba4fd7e9
SH
675 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
676 ip_route_me_harder(skb, RTN_LOCAL) != 0)
677 return 1;
678
679 return 0;
680}
681
1da177e4
LT
682/*
683 * Packet has been made sufficiently writable in caller
684 * - inout: 1=in->out, 0=out->in
685 */
686void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
687 struct ip_vs_conn *cp, int inout)
688{
eddc9ec5 689 struct iphdr *iph = ip_hdr(skb);
1da177e4 690 unsigned int icmp_offset = iph->ihl*4;
d56f90a7
ACM
691 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
692 icmp_offset);
1da177e4
LT
693 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
694
695 if (inout) {
e7ade46a 696 iph->saddr = cp->vaddr.ip;
1da177e4 697 ip_send_check(iph);
e7ade46a 698 ciph->daddr = cp->vaddr.ip;
1da177e4
LT
699 ip_send_check(ciph);
700 } else {
e7ade46a 701 iph->daddr = cp->daddr.ip;
1da177e4 702 ip_send_check(iph);
e7ade46a 703 ciph->saddr = cp->daddr.ip;
1da177e4
LT
704 ip_send_check(ciph);
705 }
706
2906f66a
VMR
707 /* the TCP/UDP/SCTP port */
708 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
709 IPPROTO_SCTP == ciph->protocol) {
014d730d 710 __be16 *ports = (void *)ciph + ciph->ihl*4;
1da177e4
LT
711
712 if (inout)
713 ports[1] = cp->vport;
714 else
715 ports[0] = cp->dport;
716 }
717
718 /* And finally the ICMP checksum */
719 icmph->checksum = 0;
720 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
721 skb->ip_summed = CHECKSUM_UNNECESSARY;
722
723 if (inout)
0d79641a 724 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
725 "Forwarding altered outgoing ICMP");
726 else
0d79641a 727 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
728 "Forwarding altered incoming ICMP");
729}
730
b3cdd2a7
JV
731#ifdef CONFIG_IP_VS_IPV6
732void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
733 struct ip_vs_conn *cp, int inout)
734{
735 struct ipv6hdr *iph = ipv6_hdr(skb);
736 unsigned int icmp_offset = sizeof(struct ipv6hdr);
737 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
738 icmp_offset);
739 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
740
741 if (inout) {
742 iph->saddr = cp->vaddr.in6;
743 ciph->daddr = cp->vaddr.in6;
744 } else {
745 iph->daddr = cp->daddr.in6;
746 ciph->saddr = cp->daddr.in6;
747 }
748
2906f66a
VMR
749 /* the TCP/UDP/SCTP port */
750 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
751 IPPROTO_SCTP == ciph->nexthdr) {
b3cdd2a7
JV
752 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
753
754 if (inout)
755 ports[1] = cp->vport;
756 else
757 ports[0] = cp->dport;
758 }
759
760 /* And finally the ICMP checksum */
8870f842
SH
761 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
762 skb->len - icmp_offset,
763 IPPROTO_ICMPV6, 0);
764 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
765 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
766 skb->ip_summed = CHECKSUM_PARTIAL;
b3cdd2a7
JV
767
768 if (inout)
0d79641a
JA
769 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
770 (void *)ciph - (void *)iph,
771 "Forwarding altered outgoing ICMPv6");
b3cdd2a7 772 else
0d79641a
JA
773 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
774 (void *)ciph - (void *)iph,
775 "Forwarding altered incoming ICMPv6");
b3cdd2a7
JV
776}
777#endif
778
4856c84c 779/* Handle relevant response ICMP messages - forward to the right
6cb90db5 780 * destination host.
4856c84c 781 */
f2428ed5
SH
782static int handle_response_icmp(int af, struct sk_buff *skb,
783 union nf_inet_addr *snet,
784 __u8 protocol, struct ip_vs_conn *cp,
4856c84c
MT
785 struct ip_vs_protocol *pp,
786 unsigned int offset, unsigned int ihl)
787{
788 unsigned int verdict = NF_DROP;
789
790 if (IP_VS_FWD_METHOD(cp) != 0) {
1e3e238e
HE
791 pr_err("shouldn't reach here, because the box is on the "
792 "half connection in the tun/dr module.\n");
4856c84c
MT
793 }
794
795 /* Ensure the checksum is correct */
796 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
797 /* Failed checksum! */
f2428ed5
SH
798 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
799 IP_VS_DBG_ADDR(af, snet));
4856c84c
MT
800 goto out;
801 }
802
2906f66a
VMR
803 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
804 IPPROTO_SCTP == protocol)
4856c84c
MT
805 offset += 2 * sizeof(__u16);
806 if (!skb_make_writable(skb, offset))
807 goto out;
808
f2428ed5
SH
809#ifdef CONFIG_IP_VS_IPV6
810 if (af == AF_INET6)
811 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
812 else
813#endif
814 ip_vs_nat_icmp(skb, pp, cp, 1);
4856c84c 815
ba4fd7e9
SH
816 if (ip_vs_route_me_harder(af, skb))
817 goto out;
f5a41847 818
4856c84c
MT
819 /* do the statistics and put it back */
820 ip_vs_out_stats(cp, skb);
821
cf356d69 822 skb->ipvs_property = 1;
f4bc17cd 823 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 824 ip_vs_notrack(skb);
f4bc17cd
JA
825 else
826 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
827 verdict = NF_ACCEPT;
828
829out:
830 __ip_vs_conn_put(cp);
831
832 return verdict;
833}
834
1da177e4
LT
835/*
836 * Handle ICMP messages in the inside-to-outside direction (outgoing).
4856c84c 837 * Find any that might be relevant, check against existing connections.
1da177e4 838 * Currently handles error types - unreachable, quench, ttl exceeded.
1da177e4 839 */
1ca5bb54
JA
840static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
841 unsigned int hooknum)
1da177e4 842{
1da177e4
LT
843 struct iphdr *iph;
844 struct icmphdr _icmph, *ic;
845 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 846 struct ip_vs_iphdr ciph;
1da177e4
LT
847 struct ip_vs_conn *cp;
848 struct ip_vs_protocol *pp;
4856c84c 849 unsigned int offset, ihl;
f2428ed5 850 union nf_inet_addr snet;
1da177e4
LT
851
852 *related = 1;
853
854 /* reassemble IP fragments */
56f8a75c 855 if (ip_is_fragment(ip_hdr(skb))) {
1ca5bb54 856 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1da177e4 857 return NF_STOLEN;
1da177e4
LT
858 }
859
eddc9ec5 860 iph = ip_hdr(skb);
1da177e4
LT
861 offset = ihl = iph->ihl * 4;
862 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
863 if (ic == NULL)
864 return NF_DROP;
865
14d5e834 866 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 867 ic->type, ntohs(icmp_id(ic)),
14d5e834 868 &iph->saddr, &iph->daddr);
1da177e4
LT
869
870 /*
871 * Work through seeing if this is for us.
872 * These checks are supposed to be in an order that means easy
873 * things are checked first to speed up processing.... however
874 * this means that some packets will manage to get a long way
875 * down this stack and then be rejected, but that's life.
876 */
877 if ((ic->type != ICMP_DEST_UNREACH) &&
878 (ic->type != ICMP_SOURCE_QUENCH) &&
879 (ic->type != ICMP_TIME_EXCEEDED)) {
880 *related = 0;
881 return NF_ACCEPT;
882 }
883
884 /* Now find the contained IP header */
885 offset += sizeof(_icmph);
886 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
887 if (cih == NULL)
888 return NF_ACCEPT; /* The packet looks wrong, ignore */
889
890 pp = ip_vs_proto_get(cih->protocol);
891 if (!pp)
892 return NF_ACCEPT;
893
894 /* Is the embedded protocol header present? */
4412ec49 895 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
896 pp->dont_defrag))
897 return NF_ACCEPT;
898
0d79641a
JA
899 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
900 "Checking outgoing ICMP for");
1da177e4
LT
901
902 offset += cih->ihl * 4;
903
51ef348b 904 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 905 /* The embedded headers contain source and dest in reverse order */
9330419d 906 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
1da177e4
LT
907 if (!cp)
908 return NF_ACCEPT;
909
f2428ed5
SH
910 snet.ip = iph->saddr;
911 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
912 pp, offset, ihl);
1da177e4
LT
913}
914
2a3b791e 915#ifdef CONFIG_IP_VS_IPV6
1ca5bb54
JA
916static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
917 unsigned int hooknum)
2a3b791e
JV
918{
919 struct ipv6hdr *iph;
920 struct icmp6hdr _icmph, *ic;
921 struct ipv6hdr _ciph, *cih; /* The ip header contained
922 within the ICMP */
923 struct ip_vs_iphdr ciph;
924 struct ip_vs_conn *cp;
925 struct ip_vs_protocol *pp;
f2428ed5
SH
926 unsigned int offset;
927 union nf_inet_addr snet;
2a3b791e
JV
928
929 *related = 1;
930
931 /* reassemble IP fragments */
932 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1ca5bb54 933 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
2a3b791e
JV
934 return NF_STOLEN;
935 }
936
937 iph = ipv6_hdr(skb);
938 offset = sizeof(struct ipv6hdr);
939 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
940 if (ic == NULL)
941 return NF_DROP;
942
5b095d98 943 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
2a3b791e 944 ic->icmp6_type, ntohs(icmpv6_id(ic)),
38ff4fa4 945 &iph->saddr, &iph->daddr);
2a3b791e
JV
946
947 /*
948 * Work through seeing if this is for us.
949 * These checks are supposed to be in an order that means easy
950 * things are checked first to speed up processing.... however
951 * this means that some packets will manage to get a long way
952 * down this stack and then be rejected, but that's life.
953 */
954 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
955 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
956 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
957 *related = 0;
958 return NF_ACCEPT;
959 }
960
961 /* Now find the contained IP header */
962 offset += sizeof(_icmph);
963 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
964 if (cih == NULL)
965 return NF_ACCEPT; /* The packet looks wrong, ignore */
966
967 pp = ip_vs_proto_get(cih->nexthdr);
968 if (!pp)
969 return NF_ACCEPT;
970
971 /* Is the embedded protocol header present? */
972 /* TODO: we don't support fragmentation at the moment anyways */
973 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
974 return NF_ACCEPT;
975
0d79641a
JA
976 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
977 "Checking outgoing ICMPv6 for");
2a3b791e
JV
978
979 offset += sizeof(struct ipv6hdr);
980
981 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
982 /* The embedded headers contain source and dest in reverse order */
9330419d 983 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
2a3b791e
JV
984 if (!cp)
985 return NF_ACCEPT;
986
178f5e49 987 ipv6_addr_copy(&snet.in6, &iph->saddr);
f2428ed5
SH
988 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
989 pp, offset, sizeof(struct ipv6hdr));
2a3b791e
JV
990}
991#endif
992
2906f66a
VMR
993/*
994 * Check if sctp chunc is ABORT chunk
995 */
996static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
997{
998 sctp_chunkhdr_t *sch, schunk;
999 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1000 sizeof(schunk), &schunk);
1001 if (sch == NULL)
1002 return 0;
1003 if (sch->type == SCTP_CID_ABORT)
1004 return 1;
1005 return 0;
1006}
1007
2a3b791e 1008static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1da177e4
LT
1009{
1010 struct tcphdr _tcph, *th;
1011
2a3b791e 1012 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1da177e4
LT
1013 if (th == NULL)
1014 return 0;
1015 return th->rst;
1016}
1017
4856c84c 1018/* Handle response packets: rewrite addresses and send away...
4856c84c
MT
1019 */
1020static unsigned int
9330419d 1021handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
4856c84c
MT
1022 struct ip_vs_conn *cp, int ihl)
1023{
9330419d
HS
1024 struct ip_vs_protocol *pp = pd->pp;
1025
0d79641a 1026 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
4856c84c
MT
1027
1028 if (!skb_make_writable(skb, ihl))
1029 goto drop;
1030
1031 /* mangle the packet */
1032 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
1033 goto drop;
1034
1035#ifdef CONFIG_IP_VS_IPV6
1036 if (af == AF_INET6)
1037 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1038 else
1039#endif
1040 {
1041 ip_hdr(skb)->saddr = cp->vaddr.ip;
1042 ip_send_check(ip_hdr(skb));
1043 }
1044
8a803040
JA
1045 /*
1046 * nf_iterate does not expect change in the skb->dst->dev.
1047 * It looks like it is not fatal to enable this code for hooks
1048 * where our handlers are at the end of the chain list and
1049 * when all next handlers use skb->dst->dev and not outdev.
1050 * It will definitely route properly the inout NAT traffic
1051 * when multiple paths are used.
1052 */
1053
4856c84c
MT
1054 /* For policy routing, packets originating from this
1055 * machine itself may be routed differently to packets
1056 * passing through. We want this packet to be routed as
1057 * if it came from this machine itself. So re-compute
1058 * the routing information.
1059 */
ba4fd7e9
SH
1060 if (ip_vs_route_me_harder(af, skb))
1061 goto drop;
4856c84c 1062
0d79641a 1063 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
4856c84c
MT
1064
1065 ip_vs_out_stats(cp, skb);
9330419d 1066 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
cf356d69 1067 skb->ipvs_property = 1;
f4bc17cd 1068 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 1069 ip_vs_notrack(skb);
f4bc17cd
JA
1070 else
1071 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
1072 ip_vs_conn_put(cp);
1073
4856c84c
MT
1074 LeaveFunction(11);
1075 return NF_ACCEPT;
1076
1077drop:
1078 ip_vs_conn_put(cp);
1079 kfree_skb(skb);
f4bc17cd 1080 LeaveFunction(11);
4856c84c
MT
1081 return NF_STOLEN;
1082}
1083
1da177e4 1084/*
4856c84c 1085 * Check if outgoing packet belongs to the established ip_vs_conn.
1da177e4
LT
1086 */
1087static unsigned int
fc604767 1088ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1089{
fc723250 1090 struct net *net = NULL;
51ef348b 1091 struct ip_vs_iphdr iph;
1da177e4 1092 struct ip_vs_protocol *pp;
9330419d 1093 struct ip_vs_proto_data *pd;
1da177e4 1094 struct ip_vs_conn *cp;
1da177e4
LT
1095
1096 EnterFunction(11);
1097
fc604767 1098 /* Already marked as IPVS request or reply? */
6869c4d8 1099 if (skb->ipvs_property)
1da177e4
LT
1100 return NF_ACCEPT;
1101
fc604767
JA
1102 /* Bad... Do not break raw sockets */
1103 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1104 af == AF_INET)) {
1105 struct sock *sk = skb->sk;
1106 struct inet_sock *inet = inet_sk(skb->sk);
1107
1108 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1109 return NF_ACCEPT;
1110 }
1111
1112 if (unlikely(!skb_dst(skb)))
1113 return NF_ACCEPT;
1114
fc723250 1115 net = skb_net(skb);
7a4f0761
HS
1116 if (!net_ipvs(net)->enable)
1117 return NF_ACCEPT;
1118
2a3b791e
JV
1119 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1120#ifdef CONFIG_IP_VS_IPV6
1121 if (af == AF_INET6) {
1122 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54
JA
1123 int related;
1124 int verdict = ip_vs_out_icmp_v6(skb, &related,
1125 hooknum);
1da177e4 1126
f5a41847 1127 if (related)
2a3b791e
JV
1128 return verdict;
1129 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1130 }
1131 } else
1132#endif
1133 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54
JA
1134 int related;
1135 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
2a3b791e 1136
f5a41847 1137 if (related)
2a3b791e
JV
1138 return verdict;
1139 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1140 }
1da177e4 1141
9330419d
HS
1142 pd = ip_vs_proto_data_get(net, iph.protocol);
1143 if (unlikely(!pd))
1da177e4 1144 return NF_ACCEPT;
9330419d 1145 pp = pd->pp;
1da177e4
LT
1146
1147 /* reassemble IP fragments */
2a3b791e
JV
1148#ifdef CONFIG_IP_VS_IPV6
1149 if (af == AF_INET6) {
1ca5bb54
JA
1150 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1151 if (ip_vs_gather_frags_v6(skb,
1152 ip_vs_defrag_user(hooknum)))
1153 return NF_STOLEN;
2a3b791e 1154 }
1ca5bb54
JA
1155
1156 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
2a3b791e
JV
1157 } else
1158#endif
56f8a75c 1159 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1ca5bb54
JA
1160 if (ip_vs_gather_frags(skb,
1161 ip_vs_defrag_user(hooknum)))
2a3b791e
JV
1162 return NF_STOLEN;
1163
1164 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1165 }
1da177e4
LT
1166
1167 /*
1168 * Check if the packet belongs to an existing entry
1169 */
9330419d 1170 cp = pp->conn_out_get(af, skb, &iph, iph.len, 0);
1da177e4 1171
cb59155f 1172 if (likely(cp))
9330419d 1173 return handle_response(af, skb, pd, cp, iph.len);
0cfa558e 1174 if (sysctl_nat_icmp_send(net) &&
cb59155f
JA
1175 (pp->protocol == IPPROTO_TCP ||
1176 pp->protocol == IPPROTO_UDP ||
1177 pp->protocol == IPPROTO_SCTP)) {
1178 __be16 _ports[2], *pptr;
1179
1180 pptr = skb_header_pointer(skb, iph.len,
1181 sizeof(_ports), _ports);
1182 if (pptr == NULL)
1183 return NF_ACCEPT; /* Not for me */
fc723250 1184 if (ip_vs_lookup_real_service(net, af, iph.protocol,
cb59155f
JA
1185 &iph.saddr,
1186 pptr[0])) {
1187 /*
1188 * Notify the real server: there is no
1189 * existing entry if it is not RST
1190 * packet or not TCP packet.
1191 */
1192 if ((iph.protocol != IPPROTO_TCP &&
1193 iph.protocol != IPPROTO_SCTP)
1194 || ((iph.protocol == IPPROTO_TCP
1195 && !is_tcp_reset(skb, iph.len))
1196 || (iph.protocol == IPPROTO_SCTP
1197 && !is_sctp_abort(skb,
1198 iph.len)))) {
2a3b791e 1199#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
1200 if (af == AF_INET6) {
1201 struct net *net =
1202 dev_net(skb_dst(skb)->dev);
1203
1204 if (!skb->dev)
1205 skb->dev = net->loopback_dev;
1206 icmpv6_send(skb,
1207 ICMPV6_DEST_UNREACH,
1208 ICMPV6_PORT_UNREACH,
1209 0);
1210 } else
2a3b791e 1211#endif
cb59155f
JA
1212 icmp_send(skb,
1213 ICMP_DEST_UNREACH,
1214 ICMP_PORT_UNREACH, 0);
1215 return NF_DROP;
1da177e4
LT
1216 }
1217 }
1da177e4 1218 }
0d79641a 1219 IP_VS_DBG_PKT(12, af, pp, skb, 0,
cb59155f
JA
1220 "ip_vs_out: packet continues traversal as normal");
1221 return NF_ACCEPT;
1da177e4
LT
1222}
1223
fc604767 1224/*
cb59155f
JA
1225 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1226 * used only for VS/NAT.
fc604767
JA
1227 * Check if packet is reply for established ip_vs_conn.
1228 */
1229static unsigned int
1230ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1231 const struct net_device *in, const struct net_device *out,
1232 int (*okfn)(struct sk_buff *))
1233{
1234 return ip_vs_out(hooknum, skb, AF_INET);
1235}
1236
1237/*
1238 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1239 * Check if packet is reply for established ip_vs_conn.
1240 */
1241static unsigned int
1242ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1243 const struct net_device *in, const struct net_device *out,
1244 int (*okfn)(struct sk_buff *))
1245{
1246 unsigned int verdict;
1247
1248 /* Disable BH in LOCAL_OUT until all places are fixed */
1249 local_bh_disable();
1250 verdict = ip_vs_out(hooknum, skb, AF_INET);
1251 local_bh_enable();
1252 return verdict;
1253}
1254
1255#ifdef CONFIG_IP_VS_IPV6
1256
1257/*
cb59155f
JA
1258 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1259 * used only for VS/NAT.
fc604767
JA
1260 * Check if packet is reply for established ip_vs_conn.
1261 */
1262static unsigned int
1263ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1264 const struct net_device *in, const struct net_device *out,
1265 int (*okfn)(struct sk_buff *))
1266{
1267 return ip_vs_out(hooknum, skb, AF_INET6);
1268}
1269
1270/*
1271 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1272 * Check if packet is reply for established ip_vs_conn.
1273 */
1274static unsigned int
1275ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1276 const struct net_device *in, const struct net_device *out,
1277 int (*okfn)(struct sk_buff *))
1278{
1279 unsigned int verdict;
1280
1281 /* Disable BH in LOCAL_OUT until all places are fixed */
1282 local_bh_disable();
1283 verdict = ip_vs_out(hooknum, skb, AF_INET6);
1284 local_bh_enable();
1285 return verdict;
1286}
1287
1288#endif
1da177e4
LT
1289
1290/*
1291 * Handle ICMP messages in the outside-to-inside direction (incoming).
1292 * Find any that might be relevant, check against existing connections,
1293 * forward to the right destination host if relevant.
1294 * Currently handles error types - unreachable, quench, ttl exceeded.
1295 */
e905a9ed 1296static int
3db05fea 1297ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1da177e4 1298{
9330419d 1299 struct net *net = NULL;
1da177e4
LT
1300 struct iphdr *iph;
1301 struct icmphdr _icmph, *ic;
1302 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 1303 struct ip_vs_iphdr ciph;
1da177e4
LT
1304 struct ip_vs_conn *cp;
1305 struct ip_vs_protocol *pp;
9330419d 1306 struct ip_vs_proto_data *pd;
1da177e4
LT
1307 unsigned int offset, ihl, verdict;
1308
1309 *related = 1;
1310
1311 /* reassemble IP fragments */
56f8a75c 1312 if (ip_is_fragment(ip_hdr(skb))) {
1ca5bb54 1313 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1da177e4 1314 return NF_STOLEN;
1da177e4
LT
1315 }
1316
eddc9ec5 1317 iph = ip_hdr(skb);
1da177e4
LT
1318 offset = ihl = iph->ihl * 4;
1319 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1320 if (ic == NULL)
1321 return NF_DROP;
1322
14d5e834 1323 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 1324 ic->type, ntohs(icmp_id(ic)),
14d5e834 1325 &iph->saddr, &iph->daddr);
1da177e4
LT
1326
1327 /*
1328 * Work through seeing if this is for us.
1329 * These checks are supposed to be in an order that means easy
1330 * things are checked first to speed up processing.... however
1331 * this means that some packets will manage to get a long way
1332 * down this stack and then be rejected, but that's life.
1333 */
1334 if ((ic->type != ICMP_DEST_UNREACH) &&
1335 (ic->type != ICMP_SOURCE_QUENCH) &&
1336 (ic->type != ICMP_TIME_EXCEEDED)) {
1337 *related = 0;
1338 return NF_ACCEPT;
1339 }
1340
1341 /* Now find the contained IP header */
1342 offset += sizeof(_icmph);
1343 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1344 if (cih == NULL)
1345 return NF_ACCEPT; /* The packet looks wrong, ignore */
1346
9330419d 1347 net = skb_net(skb);
7a4f0761 1348
9330419d
HS
1349 pd = ip_vs_proto_data_get(net, cih->protocol);
1350 if (!pd)
1da177e4 1351 return NF_ACCEPT;
9330419d 1352 pp = pd->pp;
1da177e4
LT
1353
1354 /* Is the embedded protocol header present? */
4412ec49 1355 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
1356 pp->dont_defrag))
1357 return NF_ACCEPT;
1358
0d79641a
JA
1359 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1360 "Checking incoming ICMP for");
1da177e4
LT
1361
1362 offset += cih->ihl * 4;
1363
51ef348b 1364 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1da177e4 1365 /* The embedded headers contain source and dest in reverse order */
9330419d 1366 cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1);
6cb90db5 1367 if (!cp)
1da177e4
LT
1368 return NF_ACCEPT;
1369
1370 verdict = NF_DROP;
1371
1372 /* Ensure the checksum is correct */
60476372 1373 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4 1374 /* Failed checksum! */
14d5e834
HH
1375 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1376 &iph->saddr);
1da177e4
LT
1377 goto out;
1378 }
1379
1380 /* do the statistics and put it back */
1381 ip_vs_in_stats(cp, skb);
1382 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1383 offset += 2 * sizeof(__u16);
c92f5ca2 1384 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum);
1da177e4 1385
552ad65a 1386out:
1da177e4
LT
1387 __ip_vs_conn_put(cp);
1388
1389 return verdict;
1390}
1391
2a3b791e
JV
1392#ifdef CONFIG_IP_VS_IPV6
1393static int
1394ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1395{
9330419d 1396 struct net *net = NULL;
2a3b791e
JV
1397 struct ipv6hdr *iph;
1398 struct icmp6hdr _icmph, *ic;
1399 struct ipv6hdr _ciph, *cih; /* The ip header contained
1400 within the ICMP */
1401 struct ip_vs_iphdr ciph;
1402 struct ip_vs_conn *cp;
1403 struct ip_vs_protocol *pp;
9330419d 1404 struct ip_vs_proto_data *pd;
2a3b791e
JV
1405 unsigned int offset, verdict;
1406
1407 *related = 1;
1408
1409 /* reassemble IP fragments */
1410 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1ca5bb54 1411 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
2a3b791e
JV
1412 return NF_STOLEN;
1413 }
1414
1415 iph = ipv6_hdr(skb);
1416 offset = sizeof(struct ipv6hdr);
1417 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1418 if (ic == NULL)
1419 return NF_DROP;
1420
5b095d98 1421 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
2a3b791e 1422 ic->icmp6_type, ntohs(icmpv6_id(ic)),
38ff4fa4 1423 &iph->saddr, &iph->daddr);
2a3b791e
JV
1424
1425 /*
1426 * Work through seeing if this is for us.
1427 * These checks are supposed to be in an order that means easy
1428 * things are checked first to speed up processing.... however
1429 * this means that some packets will manage to get a long way
1430 * down this stack and then be rejected, but that's life.
1431 */
1432 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1433 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1434 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1435 *related = 0;
1436 return NF_ACCEPT;
1437 }
1438
1439 /* Now find the contained IP header */
1440 offset += sizeof(_icmph);
1441 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1442 if (cih == NULL)
1443 return NF_ACCEPT; /* The packet looks wrong, ignore */
1444
9330419d
HS
1445 net = skb_net(skb);
1446 pd = ip_vs_proto_data_get(net, cih->nexthdr);
1447 if (!pd)
2a3b791e 1448 return NF_ACCEPT;
9330419d 1449 pp = pd->pp;
2a3b791e
JV
1450
1451 /* Is the embedded protocol header present? */
1452 /* TODO: we don't support fragmentation at the moment anyways */
1453 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1454 return NF_ACCEPT;
1455
0d79641a
JA
1456 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1457 "Checking incoming ICMPv6 for");
2a3b791e
JV
1458
1459 offset += sizeof(struct ipv6hdr);
1460
1461 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1462 /* The embedded headers contain source and dest in reverse order */
9330419d 1463 cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1);
6cb90db5 1464 if (!cp)
2a3b791e
JV
1465 return NF_ACCEPT;
1466
2a3b791e
JV
1467 /* do the statistics and put it back */
1468 ip_vs_in_stats(cp, skb);
2906f66a
VMR
1469 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1470 IPPROTO_SCTP == cih->nexthdr)
2a3b791e 1471 offset += 2 * sizeof(__u16);
c92f5ca2 1472 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum);
2a3b791e
JV
1473
1474 __ip_vs_conn_put(cp);
1475
1476 return verdict;
1477}
1478#endif
1479
1480
1da177e4
LT
1481/*
1482 * Check if it's for virtual services, look it up,
1483 * and send it on its way...
1484 */
1485static unsigned int
cb59155f 1486ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1487{
f131315f 1488 struct net *net;
51ef348b 1489 struct ip_vs_iphdr iph;
1da177e4 1490 struct ip_vs_protocol *pp;
9330419d 1491 struct ip_vs_proto_data *pd;
1da177e4 1492 struct ip_vs_conn *cp;
cb59155f 1493 int ret, restart, pkts;
f131315f 1494 struct netns_ipvs *ipvs;
2a3b791e 1495
fc604767
JA
1496 /* Already marked as IPVS request or reply? */
1497 if (skb->ipvs_property)
1498 return NF_ACCEPT;
1499
1da177e4 1500 /*
cb59155f
JA
1501 * Big tappo:
1502 * - remote client: only PACKET_HOST
1503 * - route: used for struct net when skb->dev is unset
1da177e4 1504 */
cb59155f
JA
1505 if (unlikely((skb->pkt_type != PACKET_HOST &&
1506 hooknum != NF_INET_LOCAL_OUT) ||
1507 !skb_dst(skb))) {
1508 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1509 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1510 " ignored in hook %u\n",
1511 skb->pkt_type, iph.protocol,
1512 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1da177e4
LT
1513 return NF_ACCEPT;
1514 }
7a4f0761
HS
1515 /* ipvs enabled in this netns ? */
1516 net = skb_net(skb);
1517 if (!net_ipvs(net)->enable)
1518 return NF_ACCEPT;
1519
cb59155f
JA
1520 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1521
1522 /* Bad... Do not break raw sockets */
1523 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1524 af == AF_INET)) {
1525 struct sock *sk = skb->sk;
1526 struct inet_sock *inet = inet_sk(skb->sk);
1527
1528 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1529 return NF_ACCEPT;
1530 }
1da177e4 1531
94b26551
JV
1532#ifdef CONFIG_IP_VS_IPV6
1533 if (af == AF_INET6) {
1534 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54
JA
1535 int related;
1536 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
1da177e4 1537
94b26551
JV
1538 if (related)
1539 return verdict;
1540 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1541 }
1542 } else
1543#endif
1544 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54
JA
1545 int related;
1546 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
94b26551
JV
1547
1548 if (related)
1549 return verdict;
1550 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1551 }
1da177e4
LT
1552
1553 /* Protocol supported? */
9330419d
HS
1554 pd = ip_vs_proto_data_get(net, iph.protocol);
1555 if (unlikely(!pd))
1da177e4 1556 return NF_ACCEPT;
9330419d 1557 pp = pd->pp;
1da177e4
LT
1558 /*
1559 * Check if the packet belongs to an existing connection entry
1560 */
9330419d 1561 cp = pp->conn_in_get(af, skb, &iph, iph.len, 0);
1da177e4
LT
1562
1563 if (unlikely(!cp)) {
1564 int v;
1565
9330419d 1566 if (!pp->conn_schedule(af, skb, pd, &v, &cp))
1da177e4
LT
1567 return v;
1568 }
1569
1570 if (unlikely(!cp)) {
1571 /* sorry, all this trouble for a no-hit :) */
0d79641a 1572 IP_VS_DBG_PKT(12, af, pp, skb, 0,
cb59155f 1573 "ip_vs_in: packet continues traversal as normal");
1da177e4
LT
1574 return NF_ACCEPT;
1575 }
1576
0d79641a 1577 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
f131315f 1578 ipvs = net_ipvs(net);
1da177e4
LT
1579 /* Check the server status */
1580 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1581 /* the destination server is not available */
1582
71a8ab6c 1583 if (sysctl_expire_nodest_conn(ipvs)) {
1da177e4
LT
1584 /* try to expire the connection immediately */
1585 ip_vs_conn_expire_now(cp);
1da177e4 1586 }
dc8103f2
JA
1587 /* don't restart its timer, and silently
1588 drop the packet. */
1589 __ip_vs_conn_put(cp);
1da177e4
LT
1590 return NF_DROP;
1591 }
1592
1593 ip_vs_in_stats(cp, skb);
9330419d 1594 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1da177e4
LT
1595 if (cp->packet_xmit)
1596 ret = cp->packet_xmit(skb, cp, pp);
1597 /* do not touch skb anymore */
1598 else {
1599 IP_VS_DBG_RL("warning: packet_xmit is null");
1600 ret = NF_ACCEPT;
1601 }
1602
efac5276
RB
1603 /* Increase its packet counter and check if it is needed
1604 * to be synchronized
1605 *
1606 * Sync connection if it is about to close to
1607 * encorage the standby servers to update the connections timeout
986a0757
HS
1608 *
1609 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
efac5276 1610 */
f131315f 1611
986a0757 1612 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
59e0350e 1613 pkts = sysctl_sync_threshold(ipvs);
986a0757
HS
1614 else
1615 pkts = atomic_add_return(1, &cp->in_pkts);
1616
f131315f 1617 if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
2906f66a
VMR
1618 cp->protocol == IPPROTO_SCTP) {
1619 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
59e0350e
SH
1620 (pkts % sysctl_sync_period(ipvs)
1621 == sysctl_sync_threshold(ipvs))) ||
2906f66a
VMR
1622 (cp->old_state != cp->state &&
1623 ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1624 (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1625 (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
f131315f 1626 ip_vs_sync_conn(net, cp);
2906f66a
VMR
1627 goto out;
1628 }
1629 }
1630
8ed2163f 1631 /* Keep this block last: TCP and others with pp->num_states <= 1 */
f131315f 1632 else if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
efac5276
RB
1633 (((cp->protocol != IPPROTO_TCP ||
1634 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
59e0350e
SH
1635 (pkts % sysctl_sync_period(ipvs)
1636 == sysctl_sync_threshold(ipvs))) ||
efac5276
RB
1637 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1638 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
9abfe315 1639 (cp->state == IP_VS_TCP_S_CLOSE) ||
9d3a0de7
RB
1640 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1641 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
f131315f 1642 ip_vs_sync_conn(net, cp);
2906f66a 1643out:
efac5276 1644 cp->old_state = cp->state;
1da177e4
LT
1645
1646 ip_vs_conn_put(cp);
1647 return ret;
1648}
1649
cb59155f
JA
1650/*
1651 * AF_INET handler in NF_INET_LOCAL_IN chain
1652 * Schedule and forward packets from remote clients
1653 */
1654static unsigned int
1655ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1656 const struct net_device *in,
1657 const struct net_device *out,
1658 int (*okfn)(struct sk_buff *))
1659{
1660 return ip_vs_in(hooknum, skb, AF_INET);
1661}
1662
1663/*
1664 * AF_INET handler in NF_INET_LOCAL_OUT chain
1665 * Schedule and forward packets from local clients
1666 */
1667static unsigned int
1668ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1669 const struct net_device *in, const struct net_device *out,
1670 int (*okfn)(struct sk_buff *))
1671{
1672 unsigned int verdict;
1673
1674 /* Disable BH in LOCAL_OUT until all places are fixed */
1675 local_bh_disable();
1676 verdict = ip_vs_in(hooknum, skb, AF_INET);
1677 local_bh_enable();
1678 return verdict;
1679}
1680
1681#ifdef CONFIG_IP_VS_IPV6
1682
1683/*
1684 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1685 * Schedule and forward packets from remote clients
1686 */
1687static unsigned int
1688ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1689 const struct net_device *in,
1690 const struct net_device *out,
1691 int (*okfn)(struct sk_buff *))
1692{
1693 return ip_vs_in(hooknum, skb, AF_INET6);
1694}
1695
1696/*
1697 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1698 * Schedule and forward packets from local clients
1699 */
1700static unsigned int
1701ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1702 const struct net_device *in, const struct net_device *out,
1703 int (*okfn)(struct sk_buff *))
1704{
1705 unsigned int verdict;
1706
1707 /* Disable BH in LOCAL_OUT until all places are fixed */
1708 local_bh_disable();
1709 verdict = ip_vs_in(hooknum, skb, AF_INET6);
1710 local_bh_enable();
1711 return verdict;
1712}
1713
1714#endif
1715
1da177e4
LT
1716
1717/*
6e23ae2a 1718 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1da177e4
LT
1719 * related packets destined for 0.0.0.0/0.
1720 * When fwmark-based virtual service is used, such as transparent
1721 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1722 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
6e23ae2a 1723 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1da177e4
LT
1724 * and send them to ip_vs_in_icmp.
1725 */
1726static unsigned int
3db05fea 1727ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1da177e4
LT
1728 const struct net_device *in, const struct net_device *out,
1729 int (*okfn)(struct sk_buff *))
1730{
1731 int r;
7a4f0761 1732 struct net *net;
1da177e4 1733
3db05fea 1734 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
1735 return NF_ACCEPT;
1736
7a4f0761
HS
1737 /* ipvs enabled in this netns ? */
1738 net = skb_net(skb);
1739 if (!net_ipvs(net)->enable)
1740 return NF_ACCEPT;
1741
3db05fea 1742 return ip_vs_in_icmp(skb, &r, hooknum);
1da177e4
LT
1743}
1744
2a3b791e
JV
1745#ifdef CONFIG_IP_VS_IPV6
1746static unsigned int
1747ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1748 const struct net_device *in, const struct net_device *out,
1749 int (*okfn)(struct sk_buff *))
1750{
1751 int r;
7a4f0761 1752 struct net *net;
2a3b791e
JV
1753
1754 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1755 return NF_ACCEPT;
1756
7a4f0761
HS
1757 /* ipvs enabled in this netns ? */
1758 net = skb_net(skb);
1759 if (!net_ipvs(net)->enable)
1760 return NF_ACCEPT;
1761
2a3b791e
JV
1762 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1763}
1764#endif
1765
1da177e4 1766
1999414a 1767static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
cb59155f
JA
1768 /* After packet filtering, change source only for VS/NAT */
1769 {
1770 .hook = ip_vs_reply4,
1771 .owner = THIS_MODULE,
1772 .pf = PF_INET,
1773 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1774 .priority = NF_IP_PRI_NAT_SRC - 2,
cb59155f 1775 },
41c5b317
PM
1776 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1777 * or VS/NAT(change destination), so that filtering rules can be
1778 * applied to IPVS. */
1779 {
cb59155f 1780 .hook = ip_vs_remote_request4,
41c5b317
PM
1781 .owner = THIS_MODULE,
1782 .pf = PF_INET,
cb59155f 1783 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1784 .priority = NF_IP_PRI_NAT_SRC - 1,
41c5b317 1785 },
fc604767 1786 /* Before ip_vs_in, change source only for VS/NAT */
41c5b317 1787 {
fc604767 1788 .hook = ip_vs_local_reply4,
41c5b317
PM
1789 .owner = THIS_MODULE,
1790 .pf = PF_INET,
fc604767 1791 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1792 .priority = NF_IP_PRI_NAT_DST + 1,
41c5b317 1793 },
cb59155f
JA
1794 /* After mangle, schedule and forward local requests */
1795 {
1796 .hook = ip_vs_local_request4,
1797 .owner = THIS_MODULE,
1798 .pf = PF_INET,
1799 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1800 .priority = NF_IP_PRI_NAT_DST + 2,
cb59155f 1801 },
41c5b317
PM
1802 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1803 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1804 {
1805 .hook = ip_vs_forward_icmp,
1806 .owner = THIS_MODULE,
1807 .pf = PF_INET,
cb59155f
JA
1808 .hooknum = NF_INET_FORWARD,
1809 .priority = 99,
41c5b317 1810 },
fc604767
JA
1811 /* After packet filtering, change source only for VS/NAT */
1812 {
1813 .hook = ip_vs_reply4,
1814 .owner = THIS_MODULE,
1815 .pf = PF_INET,
1816 .hooknum = NF_INET_FORWARD,
1817 .priority = 100,
1818 },
473b23d3 1819#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
1820 /* After packet filtering, change source only for VS/NAT */
1821 {
1822 .hook = ip_vs_reply6,
1823 .owner = THIS_MODULE,
1824 .pf = PF_INET6,
1825 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1826 .priority = NF_IP6_PRI_NAT_SRC - 2,
cb59155f 1827 },
473b23d3
JV
1828 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1829 * or VS/NAT(change destination), so that filtering rules can be
1830 * applied to IPVS. */
1831 {
cb59155f 1832 .hook = ip_vs_remote_request6,
473b23d3
JV
1833 .owner = THIS_MODULE,
1834 .pf = PF_INET6,
cb59155f 1835 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1836 .priority = NF_IP6_PRI_NAT_SRC - 1,
473b23d3 1837 },
fc604767 1838 /* Before ip_vs_in, change source only for VS/NAT */
473b23d3 1839 {
fc604767 1840 .hook = ip_vs_local_reply6,
473b23d3 1841 .owner = THIS_MODULE,
fc604767
JA
1842 .pf = PF_INET,
1843 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1844 .priority = NF_IP6_PRI_NAT_DST + 1,
473b23d3 1845 },
cb59155f
JA
1846 /* After mangle, schedule and forward local requests */
1847 {
1848 .hook = ip_vs_local_request6,
1849 .owner = THIS_MODULE,
1850 .pf = PF_INET6,
1851 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1852 .priority = NF_IP6_PRI_NAT_DST + 2,
cb59155f 1853 },
473b23d3
JV
1854 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1855 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1856 {
1857 .hook = ip_vs_forward_icmp_v6,
1858 .owner = THIS_MODULE,
1859 .pf = PF_INET6,
cb59155f
JA
1860 .hooknum = NF_INET_FORWARD,
1861 .priority = 99,
473b23d3 1862 },
fc604767
JA
1863 /* After packet filtering, change source only for VS/NAT */
1864 {
1865 .hook = ip_vs_reply6,
1866 .owner = THIS_MODULE,
1867 .pf = PF_INET6,
1868 .hooknum = NF_INET_FORWARD,
1869 .priority = 100,
1870 },
473b23d3 1871#endif
1da177e4 1872};
61b1ab45
HS
1873/*
1874 * Initialize IP Virtual Server netns mem.
1875 */
1876static int __net_init __ip_vs_init(struct net *net)
1877{
1878 struct netns_ipvs *ipvs;
1879
61b1ab45
HS
1880 ipvs = net_generic(net, ip_vs_net_id);
1881 if (ipvs == NULL) {
1882 pr_err("%s(): no memory.\n", __func__);
1883 return -ENOMEM;
1884 }
7a4f0761
HS
1885 /* Hold the beast until a service is registerd */
1886 ipvs->enable = 0;
f6340ee0 1887 ipvs->net = net;
61b1ab45
HS
1888 /* Counters used for creating unique names */
1889 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1890 atomic_inc(&ipvs_netns_cnt);
1891 net->ipvs = ipvs;
7a4f0761 1892
503cf15a 1893 if (ip_vs_estimator_net_init(net) < 0)
7a4f0761
HS
1894 goto estimator_fail;
1895
503cf15a 1896 if (ip_vs_control_net_init(net) < 0)
7a4f0761
HS
1897 goto control_fail;
1898
503cf15a 1899 if (ip_vs_protocol_net_init(net) < 0)
7a4f0761
HS
1900 goto protocol_fail;
1901
503cf15a 1902 if (ip_vs_app_net_init(net) < 0)
7a4f0761
HS
1903 goto app_fail;
1904
503cf15a 1905 if (ip_vs_conn_net_init(net) < 0)
7a4f0761
HS
1906 goto conn_fail;
1907
503cf15a 1908 if (ip_vs_sync_net_init(net) < 0)
7a4f0761
HS
1909 goto sync_fail;
1910
a870c8c5 1911 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
61b1ab45
HS
1912 sizeof(struct netns_ipvs), ipvs->gen);
1913 return 0;
7a4f0761
HS
1914/*
1915 * Error handling
1916 */
1917
1918sync_fail:
503cf15a 1919 ip_vs_conn_net_cleanup(net);
7a4f0761 1920conn_fail:
503cf15a 1921 ip_vs_app_net_cleanup(net);
7a4f0761 1922app_fail:
503cf15a 1923 ip_vs_protocol_net_cleanup(net);
7a4f0761 1924protocol_fail:
503cf15a 1925 ip_vs_control_net_cleanup(net);
7a4f0761 1926control_fail:
503cf15a 1927 ip_vs_estimator_net_cleanup(net);
7a4f0761
HS
1928estimator_fail:
1929 return -ENOMEM;
61b1ab45
HS
1930}
1931
1932static void __net_exit __ip_vs_cleanup(struct net *net)
1933{
503cf15a
HS
1934 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
1935 ip_vs_conn_net_cleanup(net);
1936 ip_vs_app_net_cleanup(net);
1937 ip_vs_protocol_net_cleanup(net);
1938 ip_vs_control_net_cleanup(net);
1939 ip_vs_estimator_net_cleanup(net);
1ae132b0 1940 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
61b1ab45
HS
1941}
1942
7a4f0761
HS
1943static void __net_exit __ip_vs_dev_cleanup(struct net *net)
1944{
1945 EnterFunction(2);
1946 net_ipvs(net)->enable = 0; /* Disable packet reception */
8f4e0a18 1947 smp_wmb();
503cf15a 1948 ip_vs_sync_net_cleanup(net);
7a4f0761
HS
1949 LeaveFunction(2);
1950}
1951
61b1ab45
HS
1952static struct pernet_operations ipvs_core_ops = {
1953 .init = __ip_vs_init,
1954 .exit = __ip_vs_cleanup,
1955 .id = &ip_vs_net_id,
1956 .size = sizeof(struct netns_ipvs),
1957};
1da177e4 1958
7a4f0761
HS
1959static struct pernet_operations ipvs_core_dev_ops = {
1960 .exit = __ip_vs_dev_cleanup,
1961};
1962
1da177e4
LT
1963/*
1964 * Initialize IP Virtual Server
1965 */
1966static int __init ip_vs_init(void)
1967{
1968 int ret;
1969
1970 ret = ip_vs_control_init();
1971 if (ret < 0) {
1e3e238e 1972 pr_err("can't setup control.\n");
6c8f7949 1973 goto exit;
1da177e4
LT
1974 }
1975
1976 ip_vs_protocol_init();
1977
1da177e4
LT
1978 ret = ip_vs_conn_init();
1979 if (ret < 0) {
1e3e238e 1980 pr_err("can't setup connection table.\n");
6c8f7949 1981 goto cleanup_protocol;
61b1ab45
HS
1982 }
1983
7a4f0761
HS
1984 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
1985 if (ret < 0)
6c8f7949 1986 goto cleanup_conn;
7a4f0761
HS
1987
1988 ret = register_pernet_device(&ipvs_core_dev_ops);
1989 if (ret < 0)
1990 goto cleanup_sub;
1991
41c5b317 1992 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4 1993 if (ret < 0) {
1e3e238e 1994 pr_err("can't register hooks.\n");
7a4f0761 1995 goto cleanup_dev;
1da177e4
LT
1996 }
1997
1e3e238e 1998 pr_info("ipvs loaded.\n");
7a4f0761 1999
1da177e4
LT
2000 return ret;
2001
7a4f0761
HS
2002cleanup_dev:
2003 unregister_pernet_device(&ipvs_core_dev_ops);
2004cleanup_sub:
2005 unregister_pernet_subsys(&ipvs_core_ops);
552ad65a 2006cleanup_conn:
1da177e4 2007 ip_vs_conn_cleanup();
552ad65a 2008cleanup_protocol:
1da177e4
LT
2009 ip_vs_protocol_cleanup();
2010 ip_vs_control_cleanup();
6c8f7949 2011exit:
1da177e4
LT
2012 return ret;
2013}
2014
2015static void __exit ip_vs_cleanup(void)
2016{
41c5b317 2017 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
7a4f0761
HS
2018 unregister_pernet_device(&ipvs_core_dev_ops);
2019 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1da177e4 2020 ip_vs_conn_cleanup();
1da177e4
LT
2021 ip_vs_protocol_cleanup();
2022 ip_vs_control_cleanup();
1e3e238e 2023 pr_info("ipvs unloaded.\n");
1da177e4
LT
2024}
2025
2026module_init(ip_vs_init);
2027module_exit(ip_vs_cleanup);
2028MODULE_LICENSE("GPL");
This page took 0.705515 seconds and 5 git commands to generate.