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