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