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