]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/ipvs/ip_vs_core.c
sctp: remove the typedef sctp_sctphdr_t
[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
39b97223 71EXPORT_SYMBOL(ip_vs_new_conn_out);
1da177e4 72
c7d03a00 73static unsigned int ip_vs_net_id __read_mostly;
61b1ab45
HS
74/* netns cnt used for uniqueness */
75static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
1da177e4
LT
76
77/* ID used in ICMP lookups */
78#define icmp_id(icmph) (((icmph)->un).echo.id)
2a3b791e 79#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
1da177e4 80
95c96174 81const char *ip_vs_proto_name(unsigned int proto)
1da177e4
LT
82{
83 static char buf[20];
84
85 switch (proto) {
86 case IPPROTO_IP:
87 return "IP";
88 case IPPROTO_UDP:
89 return "UDP";
90 case IPPROTO_TCP:
91 return "TCP";
2906f66a
VMR
92 case IPPROTO_SCTP:
93 return "SCTP";
1da177e4
LT
94 case IPPROTO_ICMP:
95 return "ICMP";
2a3b791e
JV
96#ifdef CONFIG_IP_VS_IPV6
97 case IPPROTO_ICMPV6:
98 return "ICMPv6";
99#endif
1da177e4 100 default:
1404c3ab 101 sprintf(buf, "IP_%u", proto);
1da177e4
LT
102 return buf;
103 }
104}
105
106void ip_vs_init_hash_table(struct list_head *table, int rows)
107{
108 while (--rows >= 0)
109 INIT_LIST_HEAD(&table[rows]);
110}
111
112static inline void
113ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
114{
115 struct ip_vs_dest *dest = cp->dest;
7c08d78e 116 struct netns_ipvs *ipvs = cp->ipvs;
b17fc996 117
1da177e4 118 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
b17fc996 119 struct ip_vs_cpu_stats *s;
bcbde4c0 120 struct ip_vs_service *svc;
b17fc996
HS
121
122 s = this_cpu_ptr(dest->stats.cpustats);
b17fc996 123 u64_stats_update_begin(&s->syncp);
cd67cd5e
JA
124 s->cnt.inpkts++;
125 s->cnt.inbytes += skb->len;
b17fc996
HS
126 u64_stats_update_end(&s->syncp);
127
bcbde4c0
JA
128 rcu_read_lock();
129 svc = rcu_dereference(dest->svc);
130 s = this_cpu_ptr(svc->stats.cpustats);
b17fc996 131 u64_stats_update_begin(&s->syncp);
cd67cd5e
JA
132 s->cnt.inpkts++;
133 s->cnt.inbytes += skb->len;
b17fc996 134 u64_stats_update_end(&s->syncp);
bcbde4c0 135 rcu_read_unlock();
b17fc996 136
2a0751af 137 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996 138 u64_stats_update_begin(&s->syncp);
cd67cd5e
JA
139 s->cnt.inpkts++;
140 s->cnt.inbytes += skb->len;
b17fc996 141 u64_stats_update_end(&s->syncp);
1da177e4
LT
142 }
143}
144
145
146static inline void
147ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
148{
149 struct ip_vs_dest *dest = cp->dest;
7c08d78e 150 struct netns_ipvs *ipvs = cp->ipvs;
b17fc996 151
1da177e4 152 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
b17fc996 153 struct ip_vs_cpu_stats *s;
bcbde4c0 154 struct ip_vs_service *svc;
b17fc996
HS
155
156 s = this_cpu_ptr(dest->stats.cpustats);
b17fc996 157 u64_stats_update_begin(&s->syncp);
cd67cd5e
JA
158 s->cnt.outpkts++;
159 s->cnt.outbytes += skb->len;
b17fc996
HS
160 u64_stats_update_end(&s->syncp);
161
bcbde4c0
JA
162 rcu_read_lock();
163 svc = rcu_dereference(dest->svc);
164 s = this_cpu_ptr(svc->stats.cpustats);
b17fc996 165 u64_stats_update_begin(&s->syncp);
cd67cd5e
JA
166 s->cnt.outpkts++;
167 s->cnt.outbytes += skb->len;
b17fc996 168 u64_stats_update_end(&s->syncp);
bcbde4c0 169 rcu_read_unlock();
b17fc996 170
2a0751af 171 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996 172 u64_stats_update_begin(&s->syncp);
cd67cd5e
JA
173 s->cnt.outpkts++;
174 s->cnt.outbytes += skb->len;
b17fc996 175 u64_stats_update_end(&s->syncp);
1da177e4
LT
176 }
177}
178
179
180static inline void
181ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
182{
3109d2f2 183 struct netns_ipvs *ipvs = svc->ipvs;
b17fc996 184 struct ip_vs_cpu_stats *s;
1da177e4 185
b17fc996 186 s = this_cpu_ptr(cp->dest->stats.cpustats);
cd67cd5e
JA
187 u64_stats_update_begin(&s->syncp);
188 s->cnt.conns++;
189 u64_stats_update_end(&s->syncp);
1da177e4 190
b17fc996 191 s = this_cpu_ptr(svc->stats.cpustats);
cd67cd5e
JA
192 u64_stats_update_begin(&s->syncp);
193 s->cnt.conns++;
194 u64_stats_update_end(&s->syncp);
b17fc996 195
2a0751af 196 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
cd67cd5e
JA
197 u64_stats_update_begin(&s->syncp);
198 s->cnt.conns++;
199 u64_stats_update_end(&s->syncp);
1da177e4
LT
200}
201
202
4a516f11 203static inline void
1da177e4
LT
204ip_vs_set_state(struct ip_vs_conn *cp, int direction,
205 const struct sk_buff *skb,
9330419d 206 struct ip_vs_proto_data *pd)
1da177e4 207{
4a516f11
SH
208 if (likely(pd->pp->state_transition))
209 pd->pp->state_transition(cp, direction, skb, pd);
1da177e4
LT
210}
211
a5959d53 212static inline int
85999283
SH
213ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
214 struct sk_buff *skb, int protocol,
215 const union nf_inet_addr *caddr, __be16 cport,
216 const union nf_inet_addr *vaddr, __be16 vport,
217 struct ip_vs_conn_param *p)
218{
3109d2f2 219 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
6e67e586 220 vport, p);
ceec4c38 221 p->pe = rcu_dereference(svc->pe);
85999283 222 if (p->pe && p->pe->fill_param)
a5959d53
HS
223 return p->pe->fill_param(p, skb);
224
225 return 0;
85999283 226}
1da177e4 227
1da177e4
LT
228/*
229 * IPVS persistent scheduling function
230 * It creates a connection entry according to its template if exists,
231 * or selects a server and creates a connection entry plus a template.
232 * Locking: we are svc user (svc->refcnt), so we hold all dests too
233 * Protocols supported: TCP, UDP
234 */
235static struct ip_vs_conn *
236ip_vs_sched_persist(struct ip_vs_service *svc,
d4383f04
JDB
237 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
238 int *ignored, struct ip_vs_iphdr *iph)
1da177e4
LT
239{
240 struct ip_vs_conn *cp = NULL;
1da177e4
LT
241 struct ip_vs_dest *dest;
242 struct ip_vs_conn *ct;
5b57a98c 243 __be16 dport = 0; /* destination port to forward */
3575792e 244 unsigned int flags;
f11017ec 245 struct ip_vs_conn_param param;
e0aac52e 246 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
28364a59
JV
247 union nf_inet_addr snet; /* source network of the client,
248 after masking */
ee78378f
AG
249 const union nf_inet_addr *src_addr, *dst_addr;
250
251 if (likely(!ip_vs_iph_inverse(iph))) {
252 src_addr = &iph->saddr;
253 dst_addr = &iph->daddr;
254 } else {
255 src_addr = &iph->daddr;
256 dst_addr = &iph->saddr;
257 }
258
cd17f9ed 259
1da177e4 260 /* Mask saddr with the netmask to adjust template granularity */
cd17f9ed
JV
261#ifdef CONFIG_IP_VS_IPV6
262 if (svc->af == AF_INET6)
ee78378f 263 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
0a925864 264 (__force __u32) svc->netmask);
cd17f9ed
JV
265 else
266#endif
ee78378f 267 snet.ip = src_addr->ip & svc->netmask;
1da177e4 268
cd17f9ed
JV
269 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
270 "mnet %s\n",
ee78378f
AG
271 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
272 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
cd17f9ed 273 IP_VS_DBG_ADDR(svc->af, &snet));
1da177e4
LT
274
275 /*
276 * As far as we know, FTP is a very complicated network protocol, and
277 * it uses control connection and data connections. For active FTP,
278 * FTP server initialize data connection to the client, its source port
279 * is often 20. For passive FTP, FTP server tells the clients the port
280 * that it passively listens to, and the client issues the data
281 * connection. In the tunneling or direct routing mode, the load
282 * balancer is on the client-to-server half of connection, the port
283 * number is unknown to the load balancer. So, a conn template like
284 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
285 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
286 * is created for other persistent services.
287 */
5b57a98c 288 {
d4383f04 289 int protocol = iph->protocol;
ee78378f 290 const union nf_inet_addr *vaddr = dst_addr;
f11017ec
SH
291 __be16 vport = 0;
292
ce144f24 293 if (dst_port == svc->port) {
5b57a98c
SH
294 /* non-FTP template:
295 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
296 * FTP template:
297 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
1da177e4
LT
298 */
299 if (svc->port != FTPPORT)
ce144f24 300 vport = dst_port;
1da177e4 301 } else {
5b57a98c
SH
302 /* Note: persistent fwmark-based services and
303 * persistent port zero service are handled here.
304 * fwmark template:
305 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
306 * port zero template:
307 * <protocol,caddr,0,vaddr,0,daddr,0>
1da177e4 308 */
28364a59 309 if (svc->fwmark) {
5b57a98c
SH
310 protocol = IPPROTO_IP;
311 vaddr = &fwmark;
312 }
1da177e4 313 }
a5959d53
HS
314 /* return *ignored = -1 so NF_DROP can be used */
315 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
316 vaddr, vport, &param) < 0) {
317 *ignored = -1;
318 return NULL;
319 }
1da177e4
LT
320 }
321
5b57a98c 322 /* Check if a template already exists */
f11017ec 323 ct = ip_vs_ct_in_get(&param);
3ec10d3a 324 if (!ct || !ip_vs_check_template(ct, NULL)) {
ceec4c38
JA
325 struct ip_vs_scheduler *sched;
326
a5959d53
HS
327 /*
328 * No template found or the dest of the connection
5b57a98c 329 * template is not available.
a5959d53 330 * return *ignored=0 i.e. ICMP and NF_DROP
5b57a98c 331 */
ceec4c38 332 sched = rcu_dereference(svc->scheduler);
05f00505
JA
333 if (sched) {
334 /* read svc->sched_data after svc->scheduler */
335 smp_rmb();
336 dest = sched->schedule(svc, skb, iph);
337 } else {
338 dest = NULL;
339 }
5b57a98c
SH
340 if (!dest) {
341 IP_VS_DBG(1, "p-schedule: no dest found.\n");
85999283 342 kfree(param.pe_data);
a5959d53 343 *ignored = 0;
5b57a98c
SH
344 return NULL;
345 }
346
ce144f24 347 if (dst_port == svc->port && svc->port != FTPPORT)
5b57a98c
SH
348 dport = dest->port;
349
85999283
SH
350 /* Create a template
351 * This adds param.pe_data to the template,
352 * and thus param.pe_data will be destroyed
353 * when the template expires */
ba38528a 354 ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
0e051e68 355 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
85999283
SH
356 if (ct == NULL) {
357 kfree(param.pe_data);
a5959d53 358 *ignored = -1;
5b57a98c 359 return NULL;
85999283 360 }
5b57a98c
SH
361
362 ct->timeout = svc->timeout;
85999283 363 } else {
5b57a98c
SH
364 /* set destination with the found template */
365 dest = ct->dest;
85999283
SH
366 kfree(param.pe_data);
367 }
5b57a98c 368
ce144f24 369 dport = dst_port;
5b57a98c
SH
370 if (dport == svc->port && dest->port)
371 dport = dest->port;
372
26ec037f 373 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
d4383f04 374 && iph->protocol == IPPROTO_UDP) ?
26ec037f
NC
375 IP_VS_CONN_F_ONE_PACKET : 0;
376
1da177e4
LT
377 /*
378 * Create a new connection according to the template
379 */
3109d2f2 380 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
ee78378f 381 src_port, dst_addr, dst_port, &param);
ce144f24 382
ba38528a
AG
383 cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
384 skb->mark);
1da177e4
LT
385 if (cp == NULL) {
386 ip_vs_conn_put(ct);
a5959d53 387 *ignored = -1;
1da177e4
LT
388 return NULL;
389 }
390
391 /*
392 * Add its control
393 */
394 ip_vs_control_add(cp, ct);
395 ip_vs_conn_put(ct);
396
397 ip_vs_conn_stats(cp, svc);
398 return cp;
399}
400
401
402/*
403 * IPVS main scheduling function
404 * It selects a server according to the virtual service, and
405 * creates a connection entry.
406 * Protocols supported: TCP, UDP
a5959d53
HS
407 *
408 * Usage of *ignored
409 *
410 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
411 * svc/scheduler decides that this packet should be accepted with
412 * NF_ACCEPT because it must not be scheduled.
413 *
414 * 0 : scheduler can not find destination, so try bypass or
415 * return ICMP and then NF_DROP (ip_vs_leave).
416 *
417 * -1 : scheduler tried to schedule but fatal error occurred, eg.
418 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
419 * failure such as missing Call-ID, ENOMEM on skb_linearize
420 * or pe_data. In this case we should return NF_DROP without
421 * any attempts to send ICMP with ip_vs_leave.
1da177e4
LT
422 */
423struct ip_vs_conn *
190ecd27 424ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
d4383f04
JDB
425 struct ip_vs_proto_data *pd, int *ignored,
426 struct ip_vs_iphdr *iph)
1da177e4 427{
9330419d 428 struct ip_vs_protocol *pp = pd->pp;
1da177e4 429 struct ip_vs_conn *cp = NULL;
ceec4c38 430 struct ip_vs_scheduler *sched;
1da177e4 431 struct ip_vs_dest *dest;
ee78378f
AG
432 __be16 _ports[2], *pptr, cport, vport;
433 const void *caddr, *vaddr;
3575792e 434 unsigned int flags;
1da177e4 435
190ecd27 436 *ignored = 1;
2f74713d
JDB
437 /*
438 * IPv6 frags, only the first hit here.
439 */
d4383f04 440 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
1da177e4
LT
441 if (pptr == NULL)
442 return NULL;
443
ee78378f
AG
444 if (likely(!ip_vs_iph_inverse(iph))) {
445 cport = pptr[0];
446 caddr = &iph->saddr;
447 vport = pptr[1];
448 vaddr = &iph->daddr;
449 } else {
450 cport = pptr[1];
451 caddr = &iph->daddr;
452 vport = pptr[0];
453 vaddr = &iph->saddr;
454 }
455
190ecd27
JA
456 /*
457 * FTPDATA needs this check when using local real server.
458 * Never schedule Active FTPDATA connections from real server.
459 * For LVS-NAT they must be already created. For other methods
460 * with persistence the connection is created on SYN+ACK.
461 */
ee78378f 462 if (cport == FTPDATA) {
b0e010c5 463 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
0d79641a 464 "Not scheduling FTPDATA");
190ecd27
JA
465 return NULL;
466 }
467
468 /*
a5959d53 469 * Do not schedule replies from local real server.
190ecd27 470 */
802c41ad
AG
471 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
472 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
ab161976 473 cp = pp->conn_in_get(svc->ipvs, svc->af, skb, iph);
802c41ad
AG
474 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
475
476 if (cp) {
477 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
478 "Not scheduling reply for existing"
479 " connection");
480 __ip_vs_conn_put(cp);
481 return NULL;
482 }
190ecd27
JA
483 }
484
1da177e4
LT
485 /*
486 * Persistent service
487 */
a5959d53 488 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
ee78378f 489 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
d4383f04 490 iph);
a5959d53
HS
491
492 *ignored = 0;
1da177e4
LT
493
494 /*
495 * Non-persistent service
496 */
ee78378f 497 if (!svc->fwmark && vport != svc->port) {
1da177e4 498 if (!svc->port)
1e3e238e
HE
499 pr_err("Schedule: port zero only supported "
500 "in persistent services, "
501 "check your ipvs configuration\n");
1da177e4
LT
502 return NULL;
503 }
504
ceec4c38 505 sched = rcu_dereference(svc->scheduler);
05f00505
JA
506 if (sched) {
507 /* read svc->sched_data after svc->scheduler */
508 smp_rmb();
509 dest = sched->schedule(svc, skb, iph);
510 } else {
511 dest = NULL;
512 }
1da177e4
LT
513 if (dest == NULL) {
514 IP_VS_DBG(1, "Schedule: no dest found.\n");
515 return NULL;
516 }
517
26ec037f 518 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
d4383f04 519 && iph->protocol == IPPROTO_UDP) ?
26ec037f
NC
520 IP_VS_CONN_F_ONE_PACKET : 0;
521
1da177e4
LT
522 /*
523 * Create a connection entry.
524 */
f11017ec
SH
525 {
526 struct ip_vs_conn_param p;
6e67e586 527
3109d2f2 528 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
ee78378f 529 caddr, cport, vaddr, vport, &p);
ba38528a 530 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
ee78378f 531 dest->port ? dest->port : vport,
0e051e68 532 flags, dest, skb->mark);
a5959d53
HS
533 if (!cp) {
534 *ignored = -1;
f11017ec 535 return NULL;
a5959d53 536 }
f11017ec 537 }
1da177e4 538
cd17f9ed
JV
539 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
540 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
541 ip_vs_fwd_tag(cp),
f18ae720
JA
542 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
543 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
544 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
b54ab92b 545 cp->flags, refcount_read(&cp->refcnt));
1da177e4
LT
546
547 ip_vs_conn_stats(cp, svc);
548 return cp;
549}
550
0b729021
AG
551static inline int ip_vs_addr_is_unicast(struct net *net, int af,
552 union nf_inet_addr *addr)
553{
554#ifdef CONFIG_IP_VS_IPV6
555 if (af == AF_INET6)
556 return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
557#endif
558 return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
559}
1da177e4
LT
560
561/*
562 * Pass or drop the packet.
563 * Called by ip_vs_in, when the virtual service is available but
564 * no destination is available for a new connection.
565 */
566int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
d4383f04 567 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
1da177e4 568{
0b729021 569 __be16 _ports[2], *pptr, dport;
51efbcbb
EB
570 struct netns_ipvs *ipvs = svc->ipvs;
571 struct net *net = ipvs->net;
9330419d 572
d4383f04 573 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
0b729021 574 if (!pptr)
1da177e4 575 return NF_DROP;
0b729021 576 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
a7a86b86 577
1da177e4 578 /* if it is fwmark-based service, the cache_bypass sysctl is up
2a3b791e 579 and the destination is a non-local unicast, then create
1da177e4 580 a cache_bypass connection entry */
57032948 581 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
0b729021
AG
582 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
583 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
ad542ced 584 int ret;
1da177e4 585 struct ip_vs_conn *cp;
3575792e 586 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
d4383f04 587 iph->protocol == IPPROTO_UDP) ?
3575792e 588 IP_VS_CONN_F_ONE_PACKET : 0;
dff630dd 589 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
1da177e4 590
1da177e4 591 /* create a new connection entry */
1e3e238e 592 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
f11017ec
SH
593 {
594 struct ip_vs_conn_param p;
3109d2f2 595 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
d4383f04
JDB
596 &iph->saddr, pptr[0],
597 &iph->daddr, pptr[1], &p);
ba38528a 598 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
f11017ec 599 IP_VS_CONN_F_BYPASS | flags,
0e051e68 600 NULL, skb->mark);
f11017ec
SH
601 if (!cp)
602 return NF_DROP;
603 }
1da177e4
LT
604
605 /* statistics */
606 ip_vs_in_stats(cp, skb);
607
608 /* set state */
4a516f11 609 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1da177e4
LT
610
611 /* transmit the first SYN packet */
d4383f04 612 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
1da177e4
LT
613 /* do not touch skb anymore */
614
698e2a8d
MA
615 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
616 atomic_inc(&cp->control->in_pkts);
617 else
618 atomic_inc(&cp->in_pkts);
1da177e4
LT
619 ip_vs_conn_put(cp);
620 return ret;
621 }
622
623 /*
624 * When the virtual ftp service is presented, packets destined
625 * for other services on the VIP may get here (except services
626 * listed in the ipvs table), pass the packets, because it is
627 * not ipvs job to decide to drop the packets.
628 */
0b729021 629 if (svc->port == FTPPORT && dport != FTPPORT)
1da177e4 630 return NF_ACCEPT;
0b729021
AG
631
632 if (unlikely(ip_vs_iph_icmp(iph)))
633 return NF_DROP;
1da177e4
LT
634
635 /*
636 * Notify the client that the destination is unreachable, and
637 * release the socket buffer.
638 * Since it is in IP layer, the TCP socket is not actually
639 * created, the TCP RST packet cannot be sent, instead that
640 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
641 */
2a3b791e 642#ifdef CONFIG_IP_VS_IPV6
cb59155f 643 if (svc->af == AF_INET6) {
57032948
EB
644 if (!skb->dev)
645 skb->dev = net->loopback_dev;
3ffe533c 646 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
cb59155f 647 } else
2a3b791e
JV
648#endif
649 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
650
1da177e4
LT
651 return NF_DROP;
652}
653
84b3cee3
SH
654#ifdef CONFIG_SYSCTL
655
a43d1a6b 656static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
84b3cee3 657{
84b3cee3
SH
658 return ipvs->sysctl_snat_reroute;
659}
660
2300f045 661static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
0cfa558e 662{
0cfa558e
SH
663 return ipvs->sysctl_nat_icmp_send;
664}
665
71a8ab6c
SH
666static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
667{
668 return ipvs->sysctl_expire_nodest_conn;
669}
670
84b3cee3
SH
671#else
672
a43d1a6b 673static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
2300f045 674static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
71a8ab6c 675static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
84b3cee3
SH
676
677#endif
678
b1550f22 679__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
1da177e4 680{
d3bc23e7 681 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
1da177e4
LT
682}
683
1ca5bb54
JA
684static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
685{
686 if (NF_INET_LOCAL_IN == hooknum)
687 return IP_DEFRAG_VS_IN;
688 if (NF_INET_FORWARD == hooknum)
689 return IP_DEFRAG_VS_FWD;
690 return IP_DEFRAG_VS_OUT;
691}
692
57781c1c
EB
693static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
694 struct sk_buff *skb, u_int32_t user)
1da177e4 695{
ac69269a 696 int err;
776c729e 697
ac69269a 698 local_bh_disable();
19bcf9f2 699 err = ip_defrag(ipvs->net, skb, user);
ac69269a 700 local_bh_enable();
776c729e 701 if (!err)
eddc9ec5 702 ip_send_check(ip_hdr(skb));
776c729e
HX
703
704 return err;
1da177e4
LT
705}
706
a43d1a6b
EB
707static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
708 struct sk_buff *skb, unsigned int hooknum)
ba4fd7e9 709{
a43d1a6b 710 if (!sysctl_snat_reroute(ipvs))
579eb62a
JA
711 return 0;
712 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
713 if (NF_INET_LOCAL_IN == hooknum)
714 return 0;
ba4fd7e9
SH
715#ifdef CONFIG_IP_VS_IPV6
716 if (af == AF_INET6) {
579eb62a
JA
717 struct dst_entry *dst = skb_dst(skb);
718
719 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
5f5d74d7 720 ip6_route_me_harder(ipvs->net, skb) != 0)
ba4fd7e9
SH
721 return 1;
722 } else
723#endif
579eb62a 724 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
e45f5066 725 ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
ba4fd7e9
SH
726 return 1;
727
728 return 0;
729}
730
1da177e4
LT
731/*
732 * Packet has been made sufficiently writable in caller
733 * - inout: 1=in->out, 0=out->in
734 */
735void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
736 struct ip_vs_conn *cp, int inout)
737{
eddc9ec5 738 struct iphdr *iph = ip_hdr(skb);
1da177e4 739 unsigned int icmp_offset = iph->ihl*4;
d56f90a7
ACM
740 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
741 icmp_offset);
1da177e4
LT
742 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
743
744 if (inout) {
e7ade46a 745 iph->saddr = cp->vaddr.ip;
1da177e4 746 ip_send_check(iph);
e7ade46a 747 ciph->daddr = cp->vaddr.ip;
1da177e4
LT
748 ip_send_check(ciph);
749 } else {
e7ade46a 750 iph->daddr = cp->daddr.ip;
1da177e4 751 ip_send_check(iph);
e7ade46a 752 ciph->saddr = cp->daddr.ip;
1da177e4
LT
753 ip_send_check(ciph);
754 }
755
2906f66a
VMR
756 /* the TCP/UDP/SCTP port */
757 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
758 IPPROTO_SCTP == ciph->protocol) {
014d730d 759 __be16 *ports = (void *)ciph + ciph->ihl*4;
1da177e4
LT
760
761 if (inout)
762 ports[1] = cp->vport;
763 else
764 ports[0] = cp->dport;
765 }
766
767 /* And finally the ICMP checksum */
768 icmph->checksum = 0;
769 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
770 skb->ip_summed = CHECKSUM_UNNECESSARY;
771
772 if (inout)
0d79641a 773 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
774 "Forwarding altered outgoing ICMP");
775 else
0d79641a 776 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
777 "Forwarding altered incoming ICMP");
778}
779
b3cdd2a7
JV
780#ifdef CONFIG_IP_VS_IPV6
781void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
782 struct ip_vs_conn *cp, int inout)
783{
784 struct ipv6hdr *iph = ipv6_hdr(skb);
63dca2c0
JDB
785 unsigned int icmp_offset = 0;
786 unsigned int offs = 0; /* header offset*/
787 int protocol;
788 struct icmp6hdr *icmph;
789 struct ipv6hdr *ciph;
790 unsigned short fragoffs;
791
792 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
793 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
794 offs = icmp_offset + sizeof(struct icmp6hdr);
795 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
796
797 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
b3cdd2a7
JV
798
799 if (inout) {
800 iph->saddr = cp->vaddr.in6;
801 ciph->daddr = cp->vaddr.in6;
802 } else {
803 iph->daddr = cp->daddr.in6;
804 ciph->saddr = cp->daddr.in6;
805 }
806
2906f66a 807 /* the TCP/UDP/SCTP port */
63dca2c0
JDB
808 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
809 IPPROTO_SCTP == protocol)) {
810 __be16 *ports = (void *)(skb_network_header(skb) + offs);
b3cdd2a7 811
63dca2c0
JDB
812 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
813 ntohs(inout ? ports[1] : ports[0]),
814 ntohs(inout ? cp->vport : cp->dport));
b3cdd2a7
JV
815 if (inout)
816 ports[1] = cp->vport;
817 else
818 ports[0] = cp->dport;
819 }
820
821 /* And finally the ICMP checksum */
8870f842
SH
822 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
823 skb->len - icmp_offset,
824 IPPROTO_ICMPV6, 0);
825 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
826 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
827 skb->ip_summed = CHECKSUM_PARTIAL;
b3cdd2a7
JV
828
829 if (inout)
0d79641a
JA
830 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
831 (void *)ciph - (void *)iph,
832 "Forwarding altered outgoing ICMPv6");
b3cdd2a7 833 else
0d79641a
JA
834 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
835 (void *)ciph - (void *)iph,
836 "Forwarding altered incoming ICMPv6");
b3cdd2a7
JV
837}
838#endif
839
4856c84c 840/* Handle relevant response ICMP messages - forward to the right
6cb90db5 841 * destination host.
4856c84c 842 */
f2428ed5
SH
843static int handle_response_icmp(int af, struct sk_buff *skb,
844 union nf_inet_addr *snet,
845 __u8 protocol, struct ip_vs_conn *cp,
4856c84c 846 struct ip_vs_protocol *pp,
579eb62a
JA
847 unsigned int offset, unsigned int ihl,
848 unsigned int hooknum)
4856c84c
MT
849{
850 unsigned int verdict = NF_DROP;
851
3c5ab3f3
JA
852 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
853 goto ignore_cp;
4856c84c
MT
854
855 /* Ensure the checksum is correct */
856 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
857 /* Failed checksum! */
f2428ed5
SH
858 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
859 IP_VS_DBG_ADDR(af, snet));
4856c84c
MT
860 goto out;
861 }
862
2906f66a
VMR
863 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
864 IPPROTO_SCTP == protocol)
4856c84c
MT
865 offset += 2 * sizeof(__u16);
866 if (!skb_make_writable(skb, offset))
867 goto out;
868
f2428ed5
SH
869#ifdef CONFIG_IP_VS_IPV6
870 if (af == AF_INET6)
871 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
872 else
873#endif
874 ip_vs_nat_icmp(skb, pp, cp, 1);
4856c84c 875
a43d1a6b 876 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
ba4fd7e9 877 goto out;
f5a41847 878
4856c84c
MT
879 /* do the statistics and put it back */
880 ip_vs_out_stats(cp, skb);
881
cf356d69 882 skb->ipvs_property = 1;
f4bc17cd 883 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 884 ip_vs_notrack(skb);
f4bc17cd
JA
885 else
886 ip_vs_update_conntrack(skb, cp, 0);
3c5ab3f3
JA
887
888ignore_cp:
4856c84c
MT
889 verdict = NF_ACCEPT;
890
891out:
892 __ip_vs_conn_put(cp);
893
894 return verdict;
895}
896
1da177e4
LT
897/*
898 * Handle ICMP messages in the inside-to-outside direction (outgoing).
4856c84c 899 * Find any that might be relevant, check against existing connections.
1da177e4 900 * Currently handles error types - unreachable, quench, ttl exceeded.
1da177e4 901 */
7b5f689a
EB
902static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
903 int *related, unsigned int hooknum)
1da177e4 904{
1da177e4
LT
905 struct iphdr *iph;
906 struct icmphdr _icmph, *ic;
907 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 908 struct ip_vs_iphdr ciph;
1da177e4
LT
909 struct ip_vs_conn *cp;
910 struct ip_vs_protocol *pp;
4856c84c 911 unsigned int offset, ihl;
f2428ed5 912 union nf_inet_addr snet;
1da177e4
LT
913
914 *related = 1;
915
916 /* reassemble IP fragments */
56f8a75c 917 if (ip_is_fragment(ip_hdr(skb))) {
57781c1c 918 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
1da177e4 919 return NF_STOLEN;
1da177e4
LT
920 }
921
eddc9ec5 922 iph = ip_hdr(skb);
1da177e4
LT
923 offset = ihl = iph->ihl * 4;
924 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
925 if (ic == NULL)
926 return NF_DROP;
927
14d5e834 928 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 929 ic->type, ntohs(icmp_id(ic)),
14d5e834 930 &iph->saddr, &iph->daddr);
1da177e4
LT
931
932 /*
933 * Work through seeing if this is for us.
934 * These checks are supposed to be in an order that means easy
935 * things are checked first to speed up processing.... however
936 * this means that some packets will manage to get a long way
937 * down this stack and then be rejected, but that's life.
938 */
939 if ((ic->type != ICMP_DEST_UNREACH) &&
940 (ic->type != ICMP_SOURCE_QUENCH) &&
941 (ic->type != ICMP_TIME_EXCEEDED)) {
942 *related = 0;
943 return NF_ACCEPT;
944 }
945
946 /* Now find the contained IP header */
947 offset += sizeof(_icmph);
948 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
949 if (cih == NULL)
950 return NF_ACCEPT; /* The packet looks wrong, ignore */
951
952 pp = ip_vs_proto_get(cih->protocol);
953 if (!pp)
954 return NF_ACCEPT;
955
956 /* Is the embedded protocol header present? */
4412ec49 957 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
958 pp->dont_defrag))
959 return NF_ACCEPT;
960
0d79641a
JA
961 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
962 "Checking outgoing ICMP for");
1da177e4 963
4fd9beef 964 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
b0e010c5 965
1da177e4 966 /* The embedded headers contain source and dest in reverse order */
0cf705c8 967 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
1da177e4
LT
968 if (!cp)
969 return NF_ACCEPT;
970
f2428ed5
SH
971 snet.ip = iph->saddr;
972 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
579eb62a 973 pp, ciph.len, ihl, hooknum);
1da177e4
LT
974}
975
2a3b791e 976#ifdef CONFIG_IP_VS_IPV6
7b5f689a
EB
977static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
978 int *related, unsigned int hooknum,
979 struct ip_vs_iphdr *ipvsh)
2a3b791e 980{
2a3b791e 981 struct icmp6hdr _icmph, *ic;
63dca2c0 982 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
2a3b791e
JV
983 struct ip_vs_conn *cp;
984 struct ip_vs_protocol *pp;
f2428ed5 985 union nf_inet_addr snet;
b0e010c5 986 unsigned int offset;
2a3b791e 987
63dca2c0 988 *related = 1;
2f74713d 989 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
2a3b791e
JV
990 if (ic == NULL)
991 return NF_DROP;
992
2a3b791e
JV
993 /*
994 * Work through seeing if this is for us.
995 * These checks are supposed to be in an order that means easy
996 * things are checked first to speed up processing.... however
997 * this means that some packets will manage to get a long way
998 * down this stack and then be rejected, but that's life.
999 */
2fab8917 1000 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
2a3b791e
JV
1001 *related = 0;
1002 return NF_ACCEPT;
1003 }
2f74713d
JDB
1004 /* Fragment header that is before ICMP header tells us that:
1005 * it's not an error message since they can't be fragmented.
1006 */
e7165030 1007 if (ipvsh->flags & IP6_FH_F_FRAG)
2f74713d 1008 return NF_DROP;
2a3b791e 1009
63dca2c0
JDB
1010 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1011 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1012 &ipvsh->saddr, &ipvsh->daddr);
1013
4fd9beef
AG
1014 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1015 true, &ciph))
2a3b791e 1016 return NF_ACCEPT; /* The packet looks wrong, ignore */
63dca2c0
JDB
1017
1018 pp = ip_vs_proto_get(ciph.protocol);
2a3b791e
JV
1019 if (!pp)
1020 return NF_ACCEPT;
1021
2a3b791e 1022 /* The embedded headers contain source and dest in reverse order */
0cf705c8 1023 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
2a3b791e
JV
1024 if (!cp)
1025 return NF_ACCEPT;
1026
63dca2c0 1027 snet.in6 = ciph.saddr.in6;
b0e010c5 1028 offset = ciph.len;
63dca2c0 1029 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
b0e010c5 1030 pp, offset, sizeof(struct ipv6hdr),
579eb62a 1031 hooknum);
2a3b791e
JV
1032}
1033#endif
1034
2906f66a
VMR
1035/*
1036 * Check if sctp chunc is ABORT chunk
1037 */
1038static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1039{
1040 sctp_chunkhdr_t *sch, schunk;
ae146d9b
XL
1041 sch = skb_header_pointer(skb, nh_len + sizeof(struct sctphdr),
1042 sizeof(schunk), &schunk);
2906f66a
VMR
1043 if (sch == NULL)
1044 return 0;
1045 if (sch->type == SCTP_CID_ABORT)
1046 return 1;
1047 return 0;
1048}
1049
2a3b791e 1050static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1da177e4
LT
1051{
1052 struct tcphdr _tcph, *th;
1053
2a3b791e 1054 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1da177e4
LT
1055 if (th == NULL)
1056 return 0;
1057 return th->rst;
1058}
1059
dc7b3eb9
GL
1060static inline bool is_new_conn(const struct sk_buff *skb,
1061 struct ip_vs_iphdr *iph)
1062{
1063 switch (iph->protocol) {
1064 case IPPROTO_TCP: {
1065 struct tcphdr _tcph, *th;
1066
1067 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1068 if (th == NULL)
1069 return false;
1070 return th->syn;
1071 }
1072 case IPPROTO_SCTP: {
1073 sctp_chunkhdr_t *sch, schunk;
1074
ae146d9b 1075 sch = skb_header_pointer(skb, iph->len + sizeof(struct sctphdr),
dc7b3eb9
GL
1076 sizeof(schunk), &schunk);
1077 if (sch == NULL)
1078 return false;
1079 return sch->type == SCTP_CID_INIT;
1080 }
1081 default:
1082 return false;
1083 }
1084}
1085
d752c364
MRL
1086static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1087 int conn_reuse_mode)
1088{
1089 /* Controlled (FTP DATA or persistence)? */
1090 if (cp->control)
1091 return false;
1092
1093 switch (cp->protocol) {
1094 case IPPROTO_TCP:
1095 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
f911b675 1096 (cp->state == IP_VS_TCP_S_CLOSE) ||
d752c364
MRL
1097 ((conn_reuse_mode & 2) &&
1098 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1099 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1100 case IPPROTO_SCTP:
1101 return cp->state == IP_VS_SCTP_S_CLOSED;
1102 default:
1103 return false;
1104 }
1105}
1106
39b97223
MA
1107/* Generic function to create new connections for outgoing RS packets
1108 *
1109 * Pre-requisites for successful connection creation:
1110 * 1) Virtual Service is NOT fwmark based:
1111 * In fwmark-VS actual vaddr and vport are unknown to IPVS
1112 * 2) Real Server and Virtual Service were NOT configured without port:
1113 * This is to allow match of different VS to the same RS ip-addr
1114 */
1115struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
1116 struct ip_vs_dest *dest,
1117 struct sk_buff *skb,
1118 const struct ip_vs_iphdr *iph,
1119 __be16 dport,
1120 __be16 cport)
1121{
1122 struct ip_vs_conn_param param;
1123 struct ip_vs_conn *ct = NULL, *cp = NULL;
1124 const union nf_inet_addr *vaddr, *daddr, *caddr;
1125 union nf_inet_addr snet;
1126 __be16 vport;
1127 unsigned int flags;
1128
1129 EnterFunction(12);
1130 vaddr = &svc->addr;
1131 vport = svc->port;
1132 daddr = &iph->saddr;
1133 caddr = &iph->daddr;
1134
1135 /* check pre-requisites are satisfied */
1136 if (svc->fwmark)
1137 return NULL;
1138 if (!vport || !dport)
1139 return NULL;
1140
1141 /* for persistent service first create connection template */
1142 if (svc->flags & IP_VS_SVC_F_PERSISTENT) {
1143 /* apply netmask the same way ingress-side does */
1144#ifdef CONFIG_IP_VS_IPV6
1145 if (svc->af == AF_INET6)
1146 ipv6_addr_prefix(&snet.in6, &caddr->in6,
1147 (__force __u32)svc->netmask);
1148 else
1149#endif
1150 snet.ip = caddr->ip & svc->netmask;
1151 /* fill params and create template if not existent */
1152 if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol,
1153 &snet, 0, vaddr,
1154 vport, &param) < 0)
1155 return NULL;
1156 ct = ip_vs_ct_in_get(&param);
3ec10d3a
MA
1157 /* check if template exists and points to the same dest */
1158 if (!ct || !ip_vs_check_template(ct, dest)) {
39b97223
MA
1159 ct = ip_vs_conn_new(&param, dest->af, daddr, dport,
1160 IP_VS_CONN_F_TEMPLATE, dest, 0);
1161 if (!ct) {
1162 kfree(param.pe_data);
1163 return NULL;
1164 }
1165 ct->timeout = svc->timeout;
1166 } else {
1167 kfree(param.pe_data);
1168 }
1169 }
1170
1171 /* connection flags */
1172 flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) &&
1173 iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
1174 /* create connection */
1175 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
1176 caddr, cport, vaddr, vport, &param);
1177 cp = ip_vs_conn_new(&param, dest->af, daddr, dport, flags, dest, 0);
1178 if (!cp) {
1179 if (ct)
1180 ip_vs_conn_put(ct);
1181 return NULL;
1182 }
1183 if (ct) {
1184 ip_vs_control_add(cp, ct);
1185 ip_vs_conn_put(ct);
1186 }
1187 ip_vs_conn_stats(cp, svc);
1188
1189 /* return connection (will be used to handle outgoing packet) */
1190 IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
1191 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
1192 ip_vs_fwd_tag(cp),
1193 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
1194 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
1195 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
b54ab92b 1196 cp->flags, refcount_read(&cp->refcnt));
39b97223
MA
1197 LeaveFunction(12);
1198 return cp;
1199}
1200
1201/* Handle outgoing packets which are considered requests initiated by
1202 * real servers, so that subsequent responses from external client can be
1203 * routed to the right real server.
1204 * Used also for outgoing responses in OPS mode.
1205 *
1206 * Connection management is handled by persistent-engine specific callback.
1207 */
1208static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum,
1209 struct netns_ipvs *ipvs,
1210 int af, struct sk_buff *skb,
1211 const struct ip_vs_iphdr *iph)
1212{
1213 struct ip_vs_dest *dest;
1214 struct ip_vs_conn *cp = NULL;
1215 __be16 _ports[2], *pptr;
1216
1217 if (hooknum == NF_INET_LOCAL_IN)
1218 return NULL;
1219
1220 pptr = frag_safe_skb_hp(skb, iph->len,
1221 sizeof(_ports), _ports, iph);
1222 if (!pptr)
1223 return NULL;
1224
1225 rcu_read_lock();
1226 dest = ip_vs_find_real_service(ipvs, af, iph->protocol,
1227 &iph->saddr, pptr[0]);
1228 if (dest) {
1229 struct ip_vs_service *svc;
1230 struct ip_vs_pe *pe;
1231
1232 svc = rcu_dereference(dest->svc);
1233 if (svc) {
1234 pe = rcu_dereference(svc->pe);
1235 if (pe && pe->conn_out)
1236 cp = pe->conn_out(svc, dest, skb, iph,
1237 pptr[0], pptr[1]);
1238 }
1239 }
1240 rcu_read_unlock();
1241
1242 return cp;
1243}
1244
4856c84c 1245/* Handle response packets: rewrite addresses and send away...
4856c84c
MT
1246 */
1247static unsigned int
9330419d 1248handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
579eb62a
JA
1249 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1250 unsigned int hooknum)
4856c84c 1251{
9330419d
HS
1252 struct ip_vs_protocol *pp = pd->pp;
1253
b0e010c5 1254 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
4856c84c 1255
d4383f04 1256 if (!skb_make_writable(skb, iph->len))
4856c84c
MT
1257 goto drop;
1258
1259 /* mangle the packet */
d4383f04 1260 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
4856c84c
MT
1261 goto drop;
1262
1263#ifdef CONFIG_IP_VS_IPV6
1264 if (af == AF_INET6)
1265 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1266 else
1267#endif
1268 {
1269 ip_hdr(skb)->saddr = cp->vaddr.ip;
1270 ip_send_check(ip_hdr(skb));
1271 }
1272
8a803040
JA
1273 /*
1274 * nf_iterate does not expect change in the skb->dst->dev.
1275 * It looks like it is not fatal to enable this code for hooks
1276 * where our handlers are at the end of the chain list and
1277 * when all next handlers use skb->dst->dev and not outdev.
1278 * It will definitely route properly the inout NAT traffic
1279 * when multiple paths are used.
1280 */
1281
4856c84c
MT
1282 /* For policy routing, packets originating from this
1283 * machine itself may be routed differently to packets
1284 * passing through. We want this packet to be routed as
1285 * if it came from this machine itself. So re-compute
1286 * the routing information.
1287 */
a43d1a6b 1288 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
ba4fd7e9 1289 goto drop;
4856c84c 1290
b0e010c5 1291 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
4856c84c
MT
1292
1293 ip_vs_out_stats(cp, skb);
9330419d 1294 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
cf356d69 1295 skb->ipvs_property = 1;
f4bc17cd 1296 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 1297 ip_vs_notrack(skb);
f4bc17cd
JA
1298 else
1299 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
1300 ip_vs_conn_put(cp);
1301
4856c84c
MT
1302 LeaveFunction(11);
1303 return NF_ACCEPT;
1304
1305drop:
1306 ip_vs_conn_put(cp);
1307 kfree_skb(skb);
f4bc17cd 1308 LeaveFunction(11);
4856c84c
MT
1309 return NF_STOLEN;
1310}
1311
1da177e4 1312/*
4856c84c 1313 * Check if outgoing packet belongs to the established ip_vs_conn.
1da177e4
LT
1314 */
1315static unsigned int
1b75097d 1316ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1317{
51ef348b 1318 struct ip_vs_iphdr iph;
1da177e4 1319 struct ip_vs_protocol *pp;
9330419d 1320 struct ip_vs_proto_data *pd;
1da177e4 1321 struct ip_vs_conn *cp;
340c78e5 1322 struct sock *sk;
1da177e4
LT
1323
1324 EnterFunction(11);
1325
fc604767 1326 /* Already marked as IPVS request or reply? */
6869c4d8 1327 if (skb->ipvs_property)
1da177e4
LT
1328 return NF_ACCEPT;
1329
340c78e5 1330 sk = skb_to_full_sk(skb);
fc604767 1331 /* Bad... Do not break raw sockets */
340c78e5 1332 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
fc604767 1333 af == AF_INET)) {
fc604767 1334
340c78e5 1335 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
fc604767
JA
1336 return NF_ACCEPT;
1337 }
1338
1339 if (unlikely(!skb_dst(skb)))
1340 return NF_ACCEPT;
1341
48aed1b0 1342 if (!ipvs->enable)
7a4f0761
HS
1343 return NF_ACCEPT;
1344
4fd9beef 1345 ip_vs_fill_iph_skb(af, skb, false, &iph);
2a3b791e
JV
1346#ifdef CONFIG_IP_VS_IPV6
1347 if (af == AF_INET6) {
1348 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54 1349 int related;
7b5f689a 1350 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
d4383f04 1351 hooknum, &iph);
1da177e4 1352
f5a41847 1353 if (related)
2a3b791e 1354 return verdict;
2a3b791e
JV
1355 }
1356 } else
1357#endif
1358 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54 1359 int related;
7b5f689a 1360 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
2a3b791e 1361
f5a41847 1362 if (related)
2a3b791e 1363 return verdict;
2a3b791e 1364 }
1da177e4 1365
18d6ade6 1366 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
9330419d 1367 if (unlikely(!pd))
1da177e4 1368 return NF_ACCEPT;
9330419d 1369 pp = pd->pp;
1da177e4
LT
1370
1371 /* reassemble IP fragments */
2a3b791e 1372#ifdef CONFIG_IP_VS_IPV6
63dca2c0 1373 if (af == AF_INET)
2a3b791e 1374#endif
56f8a75c 1375 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
57781c1c 1376 if (ip_vs_gather_frags(ipvs, skb,
1ca5bb54 1377 ip_vs_defrag_user(hooknum)))
2a3b791e
JV
1378 return NF_STOLEN;
1379
4fd9beef 1380 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
2a3b791e 1381 }
1da177e4
LT
1382
1383 /*
1384 * Check if the packet belongs to an existing entry
1385 */
0cf705c8 1386 cp = pp->conn_out_get(ipvs, af, skb, &iph);
1da177e4 1387
3c5ab3f3
JA
1388 if (likely(cp)) {
1389 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)
1390 goto ignore_cp;
579eb62a 1391 return handle_response(af, skb, pd, cp, &iph, hooknum);
3c5ab3f3 1392 }
39b97223
MA
1393
1394 /* Check for real-server-started requests */
1395 if (atomic_read(&ipvs->conn_out_counter)) {
1396 /* Currently only for UDP:
1397 * connection oriented protocols typically use
1398 * ephemeral ports for outgoing connections, so
1399 * related incoming responses would not match any VS
1400 */
1401 if (pp->protocol == IPPROTO_UDP) {
1402 cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph);
1403 if (likely(cp))
1404 return handle_response(af, skb, pd, cp, &iph,
1405 hooknum);
1406 }
1407 }
1408
2300f045 1409 if (sysctl_nat_icmp_send(ipvs) &&
cb59155f
JA
1410 (pp->protocol == IPPROTO_TCP ||
1411 pp->protocol == IPPROTO_UDP ||
1412 pp->protocol == IPPROTO_SCTP)) {
1413 __be16 _ports[2], *pptr;
1414
2f74713d
JDB
1415 pptr = frag_safe_skb_hp(skb, iph.len,
1416 sizeof(_ports), _ports, &iph);
cb59155f
JA
1417 if (pptr == NULL)
1418 return NF_ACCEPT; /* Not for me */
48aed1b0 1419 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
276472ea 1420 pptr[0])) {
cb59155f
JA
1421 /*
1422 * Notify the real server: there is no
1423 * existing entry if it is not RST
1424 * packet or not TCP packet.
1425 */
1426 if ((iph.protocol != IPPROTO_TCP &&
1427 iph.protocol != IPPROTO_SCTP)
1428 || ((iph.protocol == IPPROTO_TCP
1429 && !is_tcp_reset(skb, iph.len))
1430 || (iph.protocol == IPPROTO_SCTP
1431 && !is_sctp_abort(skb,
1432 iph.len)))) {
2a3b791e 1433#ifdef CONFIG_IP_VS_IPV6
cb59155f 1434 if (af == AF_INET6) {
cb59155f 1435 if (!skb->dev)
ed1c9f0e 1436 skb->dev = ipvs->net->loopback_dev;
cb59155f
JA
1437 icmpv6_send(skb,
1438 ICMPV6_DEST_UNREACH,
1439 ICMPV6_PORT_UNREACH,
1440 0);
1441 } else
2a3b791e 1442#endif
cb59155f
JA
1443 icmp_send(skb,
1444 ICMP_DEST_UNREACH,
1445 ICMP_PORT_UNREACH, 0);
1446 return NF_DROP;
1da177e4
LT
1447 }
1448 }
1da177e4 1449 }
3c5ab3f3
JA
1450
1451out:
b0e010c5 1452 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
cb59155f
JA
1453 "ip_vs_out: packet continues traversal as normal");
1454 return NF_ACCEPT;
3c5ab3f3
JA
1455
1456ignore_cp:
1457 __ip_vs_conn_put(cp);
1458 goto out;
1da177e4
LT
1459}
1460
fc604767 1461/*
cb59155f
JA
1462 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1463 * used only for VS/NAT.
fc604767
JA
1464 * Check if packet is reply for established ip_vs_conn.
1465 */
1466static unsigned int
06198b34 1467ip_vs_reply4(void *priv, struct sk_buff *skb,
238e54c9 1468 const struct nf_hook_state *state)
fc604767 1469{
1b75097d 1470 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
fc604767
JA
1471}
1472
1473/*
1474 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1475 * Check if packet is reply for established ip_vs_conn.
1476 */
1477static unsigned int
06198b34 1478ip_vs_local_reply4(void *priv, struct sk_buff *skb,
238e54c9 1479 const struct nf_hook_state *state)
fc604767 1480{
1b75097d 1481 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
fc604767
JA
1482}
1483
1484#ifdef CONFIG_IP_VS_IPV6
1485
1486/*
cb59155f
JA
1487 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1488 * used only for VS/NAT.
fc604767
JA
1489 * Check if packet is reply for established ip_vs_conn.
1490 */
1491static unsigned int
06198b34 1492ip_vs_reply6(void *priv, struct sk_buff *skb,
238e54c9 1493 const struct nf_hook_state *state)
fc604767 1494{
1b75097d 1495 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
fc604767
JA
1496}
1497
1498/*
1499 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1500 * Check if packet is reply for established ip_vs_conn.
1501 */
1502static unsigned int
06198b34 1503ip_vs_local_reply6(void *priv, struct sk_buff *skb,
238e54c9 1504 const struct nf_hook_state *state)
fc604767 1505{
1b75097d 1506 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
fc604767
JA
1507}
1508
1509#endif
1da177e4 1510
3b5ca617 1511static unsigned int
d8f44c33
EB
1512ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1513 struct ip_vs_proto_data *pd,
3b5ca617
AG
1514 int *verdict, struct ip_vs_conn **cpp,
1515 struct ip_vs_iphdr *iph)
1516{
1517 struct ip_vs_protocol *pp = pd->pp;
1518
1519 if (!iph->fragoffs) {
1520 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1521 * replayed fragment zero will already have created the cp
1522 */
1523
1524 /* Schedule and create new connection entry into cpp */
d8f44c33 1525 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
3b5ca617
AG
1526 return 0;
1527 }
1528
1529 if (unlikely(!*cpp)) {
1530 /* sorry, all this trouble for a no-hit :) */
1531 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1532 "ip_vs_in: packet continues traversal as normal");
1533 if (iph->fragoffs) {
1534 /* Fragment that couldn't be mapped to a conn entry
1535 * is missing module nf_defrag_ipv6
1536 */
1537 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1538 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1539 "unhandled fragment");
1540 }
1541 *verdict = NF_ACCEPT;
1542 return 0;
1543 }
1544
1545 return 1;
1546}
1547
1da177e4
LT
1548/*
1549 * Handle ICMP messages in the outside-to-inside direction (incoming).
1550 * Find any that might be relevant, check against existing connections,
1551 * forward to the right destination host if relevant.
1552 * Currently handles error types - unreachable, quench, ttl exceeded.
1553 */
e905a9ed 1554static int
6f2bcea9
EB
1555ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1556 unsigned int hooknum)
1da177e4 1557{
1da177e4
LT
1558 struct iphdr *iph;
1559 struct icmphdr _icmph, *ic;
1560 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 1561 struct ip_vs_iphdr ciph;
1da177e4
LT
1562 struct ip_vs_conn *cp;
1563 struct ip_vs_protocol *pp;
9330419d 1564 struct ip_vs_proto_data *pd;
f2edb9f7 1565 unsigned int offset, offset2, ihl, verdict;
6044eeff 1566 bool ipip, new_cp = false;
1da177e4
LT
1567
1568 *related = 1;
1569
1570 /* reassemble IP fragments */
56f8a75c 1571 if (ip_is_fragment(ip_hdr(skb))) {
57781c1c 1572 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
1da177e4 1573 return NF_STOLEN;
1da177e4
LT
1574 }
1575
eddc9ec5 1576 iph = ip_hdr(skb);
1da177e4
LT
1577 offset = ihl = iph->ihl * 4;
1578 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1579 if (ic == NULL)
1580 return NF_DROP;
1581
14d5e834 1582 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 1583 ic->type, ntohs(icmp_id(ic)),
14d5e834 1584 &iph->saddr, &iph->daddr);
1da177e4
LT
1585
1586 /*
1587 * Work through seeing if this is for us.
1588 * These checks are supposed to be in an order that means easy
1589 * things are checked first to speed up processing.... however
1590 * this means that some packets will manage to get a long way
1591 * down this stack and then be rejected, but that's life.
1592 */
1593 if ((ic->type != ICMP_DEST_UNREACH) &&
1594 (ic->type != ICMP_SOURCE_QUENCH) &&
1595 (ic->type != ICMP_TIME_EXCEEDED)) {
1596 *related = 0;
1597 return NF_ACCEPT;
1598 }
1599
1600 /* Now find the contained IP header */
1601 offset += sizeof(_icmph);
1602 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1603 if (cih == NULL)
1604 return NF_ACCEPT; /* The packet looks wrong, ignore */
1605
f2edb9f7
JA
1606 /* Special case for errors for IPIP packets */
1607 ipip = false;
1608 if (cih->protocol == IPPROTO_IPIP) {
1609 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1610 return NF_ACCEPT;
1611 /* Error for our IPIP must arrive at LOCAL_IN */
1612 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1613 return NF_ACCEPT;
1614 offset += cih->ihl * 4;
1615 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1616 if (cih == NULL)
1617 return NF_ACCEPT; /* The packet looks wrong, ignore */
1618 ipip = true;
1619 }
1620
18d6ade6 1621 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
9330419d 1622 if (!pd)
1da177e4 1623 return NF_ACCEPT;
9330419d 1624 pp = pd->pp;
1da177e4
LT
1625
1626 /* Is the embedded protocol header present? */
4412ec49 1627 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
1628 pp->dont_defrag))
1629 return NF_ACCEPT;
1630
0d79641a
JA
1631 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1632 "Checking incoming ICMP for");
1da177e4 1633
f2edb9f7 1634 offset2 = offset;
4fd9beef 1635 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
63dca2c0 1636 offset = ciph.len;
b0e010c5 1637
f2edb9f7
JA
1638 /* The embedded headers contain source and dest in reverse order.
1639 * For IPIP this is error for request, not for reply.
1640 */
ab161976 1641 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
6044eeff
AG
1642
1643 if (!cp) {
1644 int v;
1645
a47b4300 1646 if (!sysctl_schedule_icmp(ipvs))
6044eeff
AG
1647 return NF_ACCEPT;
1648
d8f44c33 1649 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
6044eeff
AG
1650 return v;
1651 new_cp = true;
1652 }
1da177e4
LT
1653
1654 verdict = NF_DROP;
1655
1656 /* Ensure the checksum is correct */
60476372 1657 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4 1658 /* Failed checksum! */
14d5e834
HH
1659 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1660 &iph->saddr);
1da177e4
LT
1661 goto out;
1662 }
1663
f2edb9f7
JA
1664 if (ipip) {
1665 __be32 info = ic->un.gateway;
f44a5f45
PC
1666 __u8 type = ic->type;
1667 __u8 code = ic->code;
f2edb9f7
JA
1668
1669 /* Update the MTU */
1670 if (ic->type == ICMP_DEST_UNREACH &&
1671 ic->code == ICMP_FRAG_NEEDED) {
1672 struct ip_vs_dest *dest = cp->dest;
1673 u32 mtu = ntohs(ic->un.frag.mtu);
f44a5f45 1674 __be16 frag_off = cih->frag_off;
f2edb9f7
JA
1675
1676 /* Strip outer IP and ICMP, go to IPIP header */
f44a5f45
PC
1677 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1678 goto ignore_ipip;
f2edb9f7
JA
1679 offset2 -= ihl + sizeof(_icmph);
1680 skb_reset_network_header(skb);
1681 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1682 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
6f2bcea9 1683 ipv4_update_pmtu(skb, ipvs->net,
f2edb9f7 1684 mtu, 0, 0, 0, 0);
f2edb9f7 1685 /* Client uses PMTUD? */
f44a5f45 1686 if (!(frag_off & htons(IP_DF)))
f2edb9f7
JA
1687 goto ignore_ipip;
1688 /* Prefer the resulting PMTU */
1689 if (dest) {
026ace06
JA
1690 struct ip_vs_dest_dst *dest_dst;
1691
1692 rcu_read_lock();
1693 dest_dst = rcu_dereference(dest->dest_dst);
1694 if (dest_dst)
1695 mtu = dst_mtu(dest_dst->dst_cache);
1696 rcu_read_unlock();
f2edb9f7
JA
1697 }
1698 if (mtu > 68 + sizeof(struct iphdr))
1699 mtu -= sizeof(struct iphdr);
1700 info = htonl(mtu);
1701 }
1702 /* Strip outer IP, ICMP and IPIP, go to IP header of
1703 * original request.
1704 */
f44a5f45
PC
1705 if (pskb_pull(skb, offset2) == NULL)
1706 goto ignore_ipip;
f2edb9f7
JA
1707 skb_reset_network_header(skb);
1708 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1709 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
f44a5f45
PC
1710 type, code, ntohl(info));
1711 icmp_send(skb, type, code, info);
f2edb9f7
JA
1712 /* ICMP can be shorter but anyways, account it */
1713 ip_vs_out_stats(cp, skb);
1714
1715ignore_ipip:
1716 consume_skb(skb);
1717 verdict = NF_STOLEN;
1718 goto out;
1719 }
1720
1da177e4
LT
1721 /* do the statistics and put it back */
1722 ip_vs_in_stats(cp, skb);
06f3d7f9
JA
1723 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1724 IPPROTO_SCTP == cih->protocol)
1da177e4 1725 offset += 2 * sizeof(__u16);
d4383f04 1726 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
1da177e4 1727
552ad65a 1728out:
6044eeff
AG
1729 if (likely(!new_cp))
1730 __ip_vs_conn_put(cp);
1731 else
1732 ip_vs_conn_put(cp);
1da177e4
LT
1733
1734 return verdict;
1735}
1736
2a3b791e 1737#ifdef CONFIG_IP_VS_IPV6
6f2bcea9
EB
1738static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1739 int *related, unsigned int hooknum,
1740 struct ip_vs_iphdr *iph)
2a3b791e 1741{
2a3b791e 1742 struct icmp6hdr _icmph, *ic;
63dca2c0 1743 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
2a3b791e
JV
1744 struct ip_vs_conn *cp;
1745 struct ip_vs_protocol *pp;
9330419d 1746 struct ip_vs_proto_data *pd;
b0e010c5 1747 unsigned int offset, verdict;
6044eeff 1748 bool new_cp = false;
2a3b791e 1749
63dca2c0 1750 *related = 1;
2a3b791e 1751
2f74713d 1752 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
2a3b791e
JV
1753 if (ic == NULL)
1754 return NF_DROP;
1755
2a3b791e
JV
1756 /*
1757 * Work through seeing if this is for us.
1758 * These checks are supposed to be in an order that means easy
1759 * things are checked first to speed up processing.... however
1760 * this means that some packets will manage to get a long way
1761 * down this stack and then be rejected, but that's life.
1762 */
2fab8917 1763 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
2a3b791e
JV
1764 *related = 0;
1765 return NF_ACCEPT;
1766 }
2f74713d
JDB
1767 /* Fragment header that is before ICMP header tells us that:
1768 * it's not an error message since they can't be fragmented.
1769 */
e7165030 1770 if (iph->flags & IP6_FH_F_FRAG)
2f74713d 1771 return NF_DROP;
2a3b791e 1772
63dca2c0
JDB
1773 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1774 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1775 &iph->saddr, &iph->daddr);
1776
b0e010c5 1777 offset = iph->len + sizeof(_icmph);
4fd9beef 1778 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
b0e010c5 1779 return NF_ACCEPT;
2a3b791e 1780
18d6ade6 1781 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
9330419d 1782 if (!pd)
2a3b791e 1783 return NF_ACCEPT;
9330419d 1784 pp = pd->pp;
2a3b791e 1785
63dca2c0
JDB
1786 /* Cannot handle fragmented embedded protocol */
1787 if (ciph.fragoffs)
2a3b791e
JV
1788 return NF_ACCEPT;
1789
b0e010c5 1790 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
0d79641a 1791 "Checking incoming ICMPv6 for");
2a3b791e 1792
2f74713d
JDB
1793 /* The embedded headers contain source and dest in reverse order
1794 * if not from localhost
1795 */
ab161976 1796 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
2f74713d 1797
6044eeff
AG
1798 if (!cp) {
1799 int v;
1800
a47b4300 1801 if (!sysctl_schedule_icmp(ipvs))
6044eeff
AG
1802 return NF_ACCEPT;
1803
d8f44c33 1804 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
6044eeff
AG
1805 return v;
1806
1807 new_cp = true;
1808 }
1809
2f74713d
JDB
1810 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1811 if ((hooknum == NF_INET_LOCAL_OUT) &&
1812 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
6044eeff
AG
1813 verdict = NF_ACCEPT;
1814 goto out;
2f74713d 1815 }
2a3b791e 1816
2a3b791e
JV
1817 /* do the statistics and put it back */
1818 ip_vs_in_stats(cp, skb);
63dca2c0
JDB
1819
1820 /* Need to mangle contained IPv6 header in ICMPv6 packet */
b0e010c5 1821 offset = ciph.len;
63dca2c0
JDB
1822 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1823 IPPROTO_SCTP == ciph.protocol)
b0e010c5 1824 offset += 2 * sizeof(__u16); /* Also mangle ports */
63dca2c0 1825
b0e010c5 1826 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
2a3b791e 1827
6044eeff
AG
1828out:
1829 if (likely(!new_cp))
1830 __ip_vs_conn_put(cp);
1831 else
1832 ip_vs_conn_put(cp);
2a3b791e
JV
1833
1834 return verdict;
1835}
1836#endif
1837
1838
1da177e4
LT
1839/*
1840 * Check if it's for virtual services, look it up,
1841 * and send it on its way...
1842 */
1843static unsigned int
6e385bb3 1844ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1845{
51ef348b 1846 struct ip_vs_iphdr iph;
1da177e4 1847 struct ip_vs_protocol *pp;
9330419d 1848 struct ip_vs_proto_data *pd;
1da177e4 1849 struct ip_vs_conn *cp;
4a516f11 1850 int ret, pkts;
d752c364 1851 int conn_reuse_mode;
340c78e5 1852 struct sock *sk;
2a3b791e 1853
fc604767
JA
1854 /* Already marked as IPVS request or reply? */
1855 if (skb->ipvs_property)
1856 return NF_ACCEPT;
1857
1da177e4 1858 /*
cb59155f
JA
1859 * Big tappo:
1860 * - remote client: only PACKET_HOST
1861 * - route: used for struct net when skb->dev is unset
1da177e4 1862 */
cb59155f
JA
1863 if (unlikely((skb->pkt_type != PACKET_HOST &&
1864 hooknum != NF_INET_LOCAL_OUT) ||
1865 !skb_dst(skb))) {
4fd9beef 1866 ip_vs_fill_iph_skb(af, skb, false, &iph);
cb59155f
JA
1867 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1868 " ignored in hook %u\n",
1869 skb->pkt_type, iph.protocol,
1870 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1da177e4
LT
1871 return NF_ACCEPT;
1872 }
7a4f0761 1873 /* ipvs enabled in this netns ? */
0c12582f 1874 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
7a4f0761
HS
1875 return NF_ACCEPT;
1876
4fd9beef 1877 ip_vs_fill_iph_skb(af, skb, false, &iph);
cb59155f
JA
1878
1879 /* Bad... Do not break raw sockets */
340c78e5
ED
1880 sk = skb_to_full_sk(skb);
1881 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
cb59155f 1882 af == AF_INET)) {
cb59155f 1883
340c78e5 1884 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
cb59155f
JA
1885 return NF_ACCEPT;
1886 }
1da177e4 1887
94b26551
JV
1888#ifdef CONFIG_IP_VS_IPV6
1889 if (af == AF_INET6) {
1890 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54 1891 int related;
6f2bcea9
EB
1892 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
1893 hooknum, &iph);
1da177e4 1894
94b26551
JV
1895 if (related)
1896 return verdict;
94b26551
JV
1897 }
1898 } else
1899#endif
1900 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54 1901 int related;
6f2bcea9
EB
1902 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
1903 hooknum);
94b26551
JV
1904
1905 if (related)
1906 return verdict;
94b26551 1907 }
1da177e4
LT
1908
1909 /* Protocol supported? */
18d6ade6 1910 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
4e478098
AG
1911 if (unlikely(!pd)) {
1912 /* The only way we'll see this packet again is if it's
1913 * encapsulated, so mark it with ipvs_property=1 so we
1914 * skip it if we're ignoring tunneled packets
1915 */
1916 if (sysctl_ignore_tunneled(ipvs))
1917 skb->ipvs_property = 1;
1918
1da177e4 1919 return NF_ACCEPT;
4e478098 1920 }
9330419d 1921 pp = pd->pp;
1da177e4
LT
1922 /*
1923 * Check if the packet belongs to an existing connection entry
1924 */
ab161976 1925 cp = pp->conn_in_get(ipvs, af, skb, &iph);
dc7b3eb9 1926
d752c364 1927 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
f719e375
JA
1928 if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) {
1929 bool uses_ct = false, resched = false;
1930
1931 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1932 unlikely(!atomic_read(&cp->dest->weight))) {
1933 resched = true;
1934 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
1935 } else if (is_new_conn_expected(cp, conn_reuse_mode)) {
1936 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
1937 if (!atomic_read(&cp->n_control)) {
1938 resched = true;
1939 } else {
1940 /* Do not reschedule controlling connection
1941 * that uses conntrack while it is still
1942 * referenced by controlled connection(s).
1943 */
1944 resched = !uses_ct;
1945 }
1946 }
1947
1948 if (resched) {
1949 if (!atomic_read(&cp->n_control))
1950 ip_vs_conn_expire_now(cp);
1951 __ip_vs_conn_put(cp);
1952 if (uses_ct)
1953 return NF_DROP;
1954 cp = NULL;
1955 }
dc7b3eb9
GL
1956 }
1957
3b5ca617 1958 if (unlikely(!cp)) {
1da177e4
LT
1959 int v;
1960
d8f44c33 1961 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
1da177e4
LT
1962 return v;
1963 }
1964
b0e010c5 1965 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
3b5ca617 1966
1da177e4
LT
1967 /* Check the server status */
1968 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1969 /* the destination server is not available */
1970
71a8ab6c 1971 if (sysctl_expire_nodest_conn(ipvs)) {
1da177e4
LT
1972 /* try to expire the connection immediately */
1973 ip_vs_conn_expire_now(cp);
1da177e4 1974 }
dc8103f2
JA
1975 /* don't restart its timer, and silently
1976 drop the packet. */
1977 __ip_vs_conn_put(cp);
1da177e4
LT
1978 return NF_DROP;
1979 }
1980
1981 ip_vs_in_stats(cp, skb);
4a516f11 1982 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1da177e4 1983 if (cp->packet_xmit)
d4383f04 1984 ret = cp->packet_xmit(skb, cp, pp, &iph);
1da177e4
LT
1985 /* do not touch skb anymore */
1986 else {
1987 IP_VS_DBG_RL("warning: packet_xmit is null");
1988 ret = NF_ACCEPT;
1989 }
1990
efac5276
RB
1991 /* Increase its packet counter and check if it is needed
1992 * to be synchronized
1993 *
1994 * Sync connection if it is about to close to
1995 * encorage the standby servers to update the connections timeout
986a0757
HS
1996 *
1997 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
efac5276 1998 */
f131315f 1999
986a0757 2000 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
59e0350e 2001 pkts = sysctl_sync_threshold(ipvs);
986a0757
HS
2002 else
2003 pkts = atomic_add_return(1, &cp->in_pkts);
2004
749c42b6 2005 if (ipvs->sync_state & IP_VS_STATE_MASTER)
b61a8c1a 2006 ip_vs_sync_conn(ipvs, cp, pkts);
698e2a8d
MA
2007 else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
2008 /* increment is done inside ip_vs_sync_conn too */
2009 atomic_inc(&cp->control->in_pkts);
1da177e4
LT
2010
2011 ip_vs_conn_put(cp);
2012 return ret;
2013}
2014
cb59155f
JA
2015/*
2016 * AF_INET handler in NF_INET_LOCAL_IN chain
2017 * Schedule and forward packets from remote clients
2018 */
2019static unsigned int
06198b34 2020ip_vs_remote_request4(void *priv, struct sk_buff *skb,
238e54c9 2021 const struct nf_hook_state *state)
cb59155f 2022{
6e385bb3 2023 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
cb59155f
JA
2024}
2025
2026/*
2027 * AF_INET handler in NF_INET_LOCAL_OUT chain
2028 * Schedule and forward packets from local clients
2029 */
2030static unsigned int
06198b34 2031ip_vs_local_request4(void *priv, struct sk_buff *skb,
238e54c9 2032 const struct nf_hook_state *state)
cb59155f 2033{
6e385bb3 2034 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
cb59155f
JA
2035}
2036
2037#ifdef CONFIG_IP_VS_IPV6
2038
2039/*
2040 * AF_INET6 handler in NF_INET_LOCAL_IN chain
2041 * Schedule and forward packets from remote clients
2042 */
2043static unsigned int
06198b34 2044ip_vs_remote_request6(void *priv, struct sk_buff *skb,
238e54c9 2045 const struct nf_hook_state *state)
cb59155f 2046{
6e385bb3 2047 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
cb59155f
JA
2048}
2049
2050/*
2051 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
2052 * Schedule and forward packets from local clients
2053 */
2054static unsigned int
06198b34 2055ip_vs_local_request6(void *priv, struct sk_buff *skb,
238e54c9 2056 const struct nf_hook_state *state)
cb59155f 2057{
6e385bb3 2058 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
cb59155f
JA
2059}
2060
2061#endif
2062
1da177e4
LT
2063
2064/*
6e23ae2a 2065 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1da177e4
LT
2066 * related packets destined for 0.0.0.0/0.
2067 * When fwmark-based virtual service is used, such as transparent
2068 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
2069 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
6e23ae2a 2070 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1da177e4
LT
2071 * and send them to ip_vs_in_icmp.
2072 */
2073static unsigned int
06198b34 2074ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
238e54c9 2075 const struct nf_hook_state *state)
1da177e4
LT
2076{
2077 int r;
6f2bcea9 2078 struct netns_ipvs *ipvs = net_ipvs(state->net);
1da177e4 2079
3db05fea 2080 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
2081 return NF_ACCEPT;
2082
7a4f0761 2083 /* ipvs enabled in this netns ? */
0c12582f 2084 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
7a4f0761
HS
2085 return NF_ACCEPT;
2086
6f2bcea9 2087 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
1da177e4
LT
2088}
2089
2a3b791e
JV
2090#ifdef CONFIG_IP_VS_IPV6
2091static unsigned int
06198b34 2092ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
238e54c9 2093 const struct nf_hook_state *state)
2a3b791e
JV
2094{
2095 int r;
6f2bcea9 2096 struct netns_ipvs *ipvs = net_ipvs(state->net);
63dca2c0 2097 struct ip_vs_iphdr iphdr;
2a3b791e 2098
4fd9beef 2099 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
63dca2c0 2100 if (iphdr.protocol != IPPROTO_ICMPV6)
2a3b791e
JV
2101 return NF_ACCEPT;
2102
7a4f0761 2103 /* ipvs enabled in this netns ? */
0c12582f 2104 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
7a4f0761
HS
2105 return NF_ACCEPT;
2106
6f2bcea9 2107 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
2a3b791e
JV
2108}
2109#endif
2110
1da177e4 2111
1999414a 2112static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
cb59155f
JA
2113 /* After packet filtering, change source only for VS/NAT */
2114 {
2115 .hook = ip_vs_reply4,
4c809d63 2116 .pf = NFPROTO_IPV4,
cb59155f 2117 .hooknum = NF_INET_LOCAL_IN,
afb523c5 2118 .priority = NF_IP_PRI_NAT_SRC - 2,
cb59155f 2119 },
41c5b317
PM
2120 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2121 * or VS/NAT(change destination), so that filtering rules can be
2122 * applied to IPVS. */
2123 {
cb59155f 2124 .hook = ip_vs_remote_request4,
4c809d63 2125 .pf = NFPROTO_IPV4,
cb59155f 2126 .hooknum = NF_INET_LOCAL_IN,
afb523c5 2127 .priority = NF_IP_PRI_NAT_SRC - 1,
41c5b317 2128 },
fc604767 2129 /* Before ip_vs_in, change source only for VS/NAT */
41c5b317 2130 {
fc604767 2131 .hook = ip_vs_local_reply4,
4c809d63 2132 .pf = NFPROTO_IPV4,
fc604767 2133 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 2134 .priority = NF_IP_PRI_NAT_DST + 1,
41c5b317 2135 },
cb59155f
JA
2136 /* After mangle, schedule and forward local requests */
2137 {
2138 .hook = ip_vs_local_request4,
4c809d63 2139 .pf = NFPROTO_IPV4,
cb59155f 2140 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 2141 .priority = NF_IP_PRI_NAT_DST + 2,
cb59155f 2142 },
41c5b317
PM
2143 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2144 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2145 {
2146 .hook = ip_vs_forward_icmp,
4c809d63 2147 .pf = NFPROTO_IPV4,
cb59155f
JA
2148 .hooknum = NF_INET_FORWARD,
2149 .priority = 99,
41c5b317 2150 },
fc604767
JA
2151 /* After packet filtering, change source only for VS/NAT */
2152 {
2153 .hook = ip_vs_reply4,
4c809d63 2154 .pf = NFPROTO_IPV4,
fc604767
JA
2155 .hooknum = NF_INET_FORWARD,
2156 .priority = 100,
2157 },
473b23d3 2158#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
2159 /* After packet filtering, change source only for VS/NAT */
2160 {
2161 .hook = ip_vs_reply6,
4c809d63 2162 .pf = NFPROTO_IPV6,
cb59155f 2163 .hooknum = NF_INET_LOCAL_IN,
afb523c5 2164 .priority = NF_IP6_PRI_NAT_SRC - 2,
cb59155f 2165 },
473b23d3
JV
2166 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2167 * or VS/NAT(change destination), so that filtering rules can be
2168 * applied to IPVS. */
2169 {
cb59155f 2170 .hook = ip_vs_remote_request6,
4c809d63 2171 .pf = NFPROTO_IPV6,
cb59155f 2172 .hooknum = NF_INET_LOCAL_IN,
afb523c5 2173 .priority = NF_IP6_PRI_NAT_SRC - 1,
473b23d3 2174 },
fc604767 2175 /* Before ip_vs_in, change source only for VS/NAT */
473b23d3 2176 {
fc604767 2177 .hook = ip_vs_local_reply6,
eb90b0c7 2178 .pf = NFPROTO_IPV6,
fc604767 2179 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 2180 .priority = NF_IP6_PRI_NAT_DST + 1,
473b23d3 2181 },
cb59155f
JA
2182 /* After mangle, schedule and forward local requests */
2183 {
2184 .hook = ip_vs_local_request6,
4c809d63 2185 .pf = NFPROTO_IPV6,
cb59155f 2186 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 2187 .priority = NF_IP6_PRI_NAT_DST + 2,
cb59155f 2188 },
473b23d3
JV
2189 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2190 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2191 {
2192 .hook = ip_vs_forward_icmp_v6,
4c809d63 2193 .pf = NFPROTO_IPV6,
cb59155f
JA
2194 .hooknum = NF_INET_FORWARD,
2195 .priority = 99,
473b23d3 2196 },
fc604767
JA
2197 /* After packet filtering, change source only for VS/NAT */
2198 {
2199 .hook = ip_vs_reply6,
4c809d63 2200 .pf = NFPROTO_IPV6,
fc604767
JA
2201 .hooknum = NF_INET_FORWARD,
2202 .priority = 100,
2203 },
473b23d3 2204#endif
1da177e4 2205};
61b1ab45
HS
2206/*
2207 * Initialize IP Virtual Server netns mem.
2208 */
2209static int __net_init __ip_vs_init(struct net *net)
2210{
2211 struct netns_ipvs *ipvs;
efe41606 2212 int ret;
61b1ab45 2213
61b1ab45 2214 ipvs = net_generic(net, ip_vs_net_id);
0a9ee813 2215 if (ipvs == NULL)
61b1ab45 2216 return -ENOMEM;
0a9ee813 2217
7a4f0761
HS
2218 /* Hold the beast until a service is registerd */
2219 ipvs->enable = 0;
f6340ee0 2220 ipvs->net = net;
61b1ab45
HS
2221 /* Counters used for creating unique names */
2222 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2223 atomic_inc(&ipvs_netns_cnt);
2224 net->ipvs = ipvs;
7a4f0761 2225
a4dd0360 2226 if (ip_vs_estimator_net_init(ipvs) < 0)
7a4f0761
HS
2227 goto estimator_fail;
2228
3d993766 2229 if (ip_vs_control_net_init(ipvs) < 0)
7a4f0761
HS
2230 goto control_fail;
2231
7d1f88ec 2232 if (ip_vs_protocol_net_init(ipvs) < 0)
7a4f0761
HS
2233 goto protocol_fail;
2234
b5dd212c 2235 if (ip_vs_app_net_init(ipvs) < 0)
7a4f0761
HS
2236 goto app_fail;
2237
2f3edc6a 2238 if (ip_vs_conn_net_init(ipvs) < 0)
7a4f0761
HS
2239 goto conn_fail;
2240
802cb437 2241 if (ip_vs_sync_net_init(ipvs) < 0)
7a4f0761
HS
2242 goto sync_fail;
2243
efe41606
FW
2244 ret = nf_register_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2245 if (ret < 0)
2246 goto hook_fail;
2247
61b1ab45 2248 return 0;
7a4f0761
HS
2249/*
2250 * Error handling
2251 */
2252
efe41606
FW
2253hook_fail:
2254 ip_vs_sync_net_cleanup(ipvs);
7a4f0761 2255sync_fail:
2f3edc6a 2256 ip_vs_conn_net_cleanup(ipvs);
7a4f0761 2257conn_fail:
b5dd212c 2258 ip_vs_app_net_cleanup(ipvs);
7a4f0761 2259app_fail:
7d1f88ec 2260 ip_vs_protocol_net_cleanup(ipvs);
7a4f0761 2261protocol_fail:
3d993766 2262 ip_vs_control_net_cleanup(ipvs);
7a4f0761 2263control_fail:
a4dd0360 2264 ip_vs_estimator_net_cleanup(ipvs);
7a4f0761 2265estimator_fail:
39f618b4 2266 net->ipvs = NULL;
7a4f0761 2267 return -ENOMEM;
61b1ab45
HS
2268}
2269
2270static void __net_exit __ip_vs_cleanup(struct net *net)
2271{
56d2169b
EB
2272 struct netns_ipvs *ipvs = net_ipvs(net);
2273
efe41606 2274 nf_unregister_net_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
56d2169b 2275 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
2f3edc6a 2276 ip_vs_conn_net_cleanup(ipvs);
b5dd212c 2277 ip_vs_app_net_cleanup(ipvs);
7d1f88ec 2278 ip_vs_protocol_net_cleanup(ipvs);
3d993766 2279 ip_vs_control_net_cleanup(ipvs);
a4dd0360 2280 ip_vs_estimator_net_cleanup(ipvs);
56d2169b 2281 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
39f618b4 2282 net->ipvs = NULL;
61b1ab45
HS
2283}
2284
7a4f0761
HS
2285static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2286{
ebea1f7c 2287 struct netns_ipvs *ipvs = net_ipvs(net);
7a4f0761 2288 EnterFunction(2);
ebea1f7c 2289 ipvs->enable = 0; /* Disable packet reception */
8f4e0a18 2290 smp_wmb();
ebea1f7c 2291 ip_vs_sync_net_cleanup(ipvs);
7a4f0761
HS
2292 LeaveFunction(2);
2293}
2294
61b1ab45
HS
2295static struct pernet_operations ipvs_core_ops = {
2296 .init = __ip_vs_init,
2297 .exit = __ip_vs_cleanup,
2298 .id = &ip_vs_net_id,
2299 .size = sizeof(struct netns_ipvs),
2300};
1da177e4 2301
7a4f0761
HS
2302static struct pernet_operations ipvs_core_dev_ops = {
2303 .exit = __ip_vs_dev_cleanup,
2304};
2305
1da177e4
LT
2306/*
2307 * Initialize IP Virtual Server
2308 */
2309static int __init ip_vs_init(void)
2310{
2311 int ret;
2312
2313 ret = ip_vs_control_init();
2314 if (ret < 0) {
1e3e238e 2315 pr_err("can't setup control.\n");
6c8f7949 2316 goto exit;
1da177e4
LT
2317 }
2318
2319 ip_vs_protocol_init();
2320
1da177e4
LT
2321 ret = ip_vs_conn_init();
2322 if (ret < 0) {
1e3e238e 2323 pr_err("can't setup connection table.\n");
6c8f7949 2324 goto cleanup_protocol;
61b1ab45
HS
2325 }
2326
7a4f0761
HS
2327 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2328 if (ret < 0)
6c8f7949 2329 goto cleanup_conn;
7a4f0761
HS
2330
2331 ret = register_pernet_device(&ipvs_core_dev_ops);
2332 if (ret < 0)
2333 goto cleanup_sub;
2334
8537de8a
HS
2335 ret = ip_vs_register_nl_ioctl();
2336 if (ret < 0) {
2337 pr_err("can't register netlink/ioctl.\n");
efe41606 2338 goto cleanup_dev;
8537de8a
HS
2339 }
2340
1e3e238e 2341 pr_info("ipvs loaded.\n");
7a4f0761 2342
1da177e4
LT
2343 return ret;
2344
7a4f0761
HS
2345cleanup_dev:
2346 unregister_pernet_device(&ipvs_core_dev_ops);
2347cleanup_sub:
2348 unregister_pernet_subsys(&ipvs_core_ops);
552ad65a 2349cleanup_conn:
1da177e4 2350 ip_vs_conn_cleanup();
552ad65a 2351cleanup_protocol:
1da177e4
LT
2352 ip_vs_protocol_cleanup();
2353 ip_vs_control_cleanup();
6c8f7949 2354exit:
1da177e4
LT
2355 return ret;
2356}
2357
2358static void __exit ip_vs_cleanup(void)
2359{
8537de8a 2360 ip_vs_unregister_nl_ioctl();
7a4f0761
HS
2361 unregister_pernet_device(&ipvs_core_dev_ops);
2362 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1da177e4 2363 ip_vs_conn_cleanup();
1da177e4
LT
2364 ip_vs_protocol_cleanup();
2365 ip_vs_control_cleanup();
1e3e238e 2366 pr_info("ipvs unloaded.\n");
1da177e4
LT
2367}
2368
2369module_init(ip_vs_init);
2370module_exit(ip_vs_cleanup);
2371MODULE_LICENSE("GPL");