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