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