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