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