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