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