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