ipvs: fix sctp chunk length order
[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
61b1ab45
HS
72int ip_vs_net_id __read_mostly;
73#ifdef IP_VS_GENERIC_NETNS
74EXPORT_SYMBOL(ip_vs_net_id);
75#endif
76/* netns cnt used for uniqueness */
77static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
1da177e4
LT
78
79/* ID used in ICMP lookups */
80#define icmp_id(icmph) (((icmph)->un).echo.id)
2a3b791e 81#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
1da177e4 82
95c96174 83const char *ip_vs_proto_name(unsigned int proto)
1da177e4
LT
84{
85 static char buf[20];
86
87 switch (proto) {
88 case IPPROTO_IP:
89 return "IP";
90 case IPPROTO_UDP:
91 return "UDP";
92 case IPPROTO_TCP:
93 return "TCP";
2906f66a
VMR
94 case IPPROTO_SCTP:
95 return "SCTP";
1da177e4
LT
96 case IPPROTO_ICMP:
97 return "ICMP";
2a3b791e
JV
98#ifdef CONFIG_IP_VS_IPV6
99 case IPPROTO_ICMPV6:
100 return "ICMPv6";
101#endif
1da177e4
LT
102 default:
103 sprintf(buf, "IP_%d", proto);
104 return buf;
105 }
106}
107
108void ip_vs_init_hash_table(struct list_head *table, int rows)
109{
110 while (--rows >= 0)
111 INIT_LIST_HEAD(&table[rows]);
112}
113
114static inline void
115ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
116{
117 struct ip_vs_dest *dest = cp->dest;
b17fc996
HS
118 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
119
1da177e4 120 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
b17fc996
HS
121 struct ip_vs_cpu_stats *s;
122
123 s = this_cpu_ptr(dest->stats.cpustats);
124 s->ustats.inpkts++;
125 u64_stats_update_begin(&s->syncp);
126 s->ustats.inbytes += skb->len;
127 u64_stats_update_end(&s->syncp);
128
129 s = this_cpu_ptr(dest->svc->stats.cpustats);
130 s->ustats.inpkts++;
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
134
2a0751af 135 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996
HS
136 s->ustats.inpkts++;
137 u64_stats_update_begin(&s->syncp);
138 s->ustats.inbytes += skb->len;
139 u64_stats_update_end(&s->syncp);
1da177e4
LT
140 }
141}
142
143
144static inline void
145ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
146{
147 struct ip_vs_dest *dest = cp->dest;
b17fc996
HS
148 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
149
1da177e4 150 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
b17fc996
HS
151 struct ip_vs_cpu_stats *s;
152
153 s = this_cpu_ptr(dest->stats.cpustats);
154 s->ustats.outpkts++;
155 u64_stats_update_begin(&s->syncp);
156 s->ustats.outbytes += skb->len;
157 u64_stats_update_end(&s->syncp);
158
159 s = this_cpu_ptr(dest->svc->stats.cpustats);
160 s->ustats.outpkts++;
161 u64_stats_update_begin(&s->syncp);
162 s->ustats.outbytes += skb->len;
163 u64_stats_update_end(&s->syncp);
164
2a0751af 165 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996
HS
166 s->ustats.outpkts++;
167 u64_stats_update_begin(&s->syncp);
168 s->ustats.outbytes += skb->len;
169 u64_stats_update_end(&s->syncp);
1da177e4
LT
170 }
171}
172
173
174static inline void
175ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
176{
b17fc996
HS
177 struct netns_ipvs *ipvs = net_ipvs(svc->net);
178 struct ip_vs_cpu_stats *s;
1da177e4 179
b17fc996
HS
180 s = this_cpu_ptr(cp->dest->stats.cpustats);
181 s->ustats.conns++;
1da177e4 182
b17fc996
HS
183 s = this_cpu_ptr(svc->stats.cpustats);
184 s->ustats.conns++;
185
2a0751af 186 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
b17fc996 187 s->ustats.conns++;
1da177e4
LT
188}
189
190
4a516f11 191static inline void
1da177e4
LT
192ip_vs_set_state(struct ip_vs_conn *cp, int direction,
193 const struct sk_buff *skb,
9330419d 194 struct ip_vs_proto_data *pd)
1da177e4 195{
4a516f11
SH
196 if (likely(pd->pp->state_transition))
197 pd->pp->state_transition(cp, direction, skb, pd);
1da177e4
LT
198}
199
a5959d53 200static inline int
85999283
SH
201ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
202 struct sk_buff *skb, int protocol,
203 const union nf_inet_addr *caddr, __be16 cport,
204 const union nf_inet_addr *vaddr, __be16 vport,
205 struct ip_vs_conn_param *p)
206{
6e67e586
HS
207 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
208 vport, p);
85999283
SH
209 p->pe = svc->pe;
210 if (p->pe && p->pe->fill_param)
a5959d53
HS
211 return p->pe->fill_param(p, skb);
212
213 return 0;
85999283 214}
1da177e4 215
1da177e4
LT
216/*
217 * IPVS persistent scheduling function
218 * It creates a connection entry according to its template if exists,
219 * or selects a server and creates a connection entry plus a template.
220 * Locking: we are svc user (svc->refcnt), so we hold all dests too
221 * Protocols supported: TCP, UDP
222 */
223static struct ip_vs_conn *
224ip_vs_sched_persist(struct ip_vs_service *svc,
d4383f04
JDB
225 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
226 int *ignored, struct ip_vs_iphdr *iph)
1da177e4
LT
227{
228 struct ip_vs_conn *cp = NULL;
1da177e4
LT
229 struct ip_vs_dest *dest;
230 struct ip_vs_conn *ct;
5b57a98c 231 __be16 dport = 0; /* destination port to forward */
3575792e 232 unsigned int flags;
f11017ec 233 struct ip_vs_conn_param param;
e0aac52e 234 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
28364a59
JV
235 union nf_inet_addr snet; /* source network of the client,
236 after masking */
cd17f9ed 237
1da177e4 238 /* Mask saddr with the netmask to adjust template granularity */
cd17f9ed
JV
239#ifdef CONFIG_IP_VS_IPV6
240 if (svc->af == AF_INET6)
d4383f04 241 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6, svc->netmask);
cd17f9ed
JV
242 else
243#endif
d4383f04 244 snet.ip = iph->saddr.ip & svc->netmask;
1da177e4 245
cd17f9ed
JV
246 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
247 "mnet %s\n",
d4383f04
JDB
248 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
249 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
cd17f9ed 250 IP_VS_DBG_ADDR(svc->af, &snet));
1da177e4
LT
251
252 /*
253 * As far as we know, FTP is a very complicated network protocol, and
254 * it uses control connection and data connections. For active FTP,
255 * FTP server initialize data connection to the client, its source port
256 * is often 20. For passive FTP, FTP server tells the clients the port
257 * that it passively listens to, and the client issues the data
258 * connection. In the tunneling or direct routing mode, the load
259 * balancer is on the client-to-server half of connection, the port
260 * number is unknown to the load balancer. So, a conn template like
261 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
262 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
263 * is created for other persistent services.
264 */
5b57a98c 265 {
d4383f04
JDB
266 int protocol = iph->protocol;
267 const union nf_inet_addr *vaddr = &iph->daddr;
f11017ec
SH
268 __be16 vport = 0;
269
ce144f24 270 if (dst_port == svc->port) {
5b57a98c
SH
271 /* non-FTP template:
272 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
273 * FTP template:
274 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
1da177e4
LT
275 */
276 if (svc->port != FTPPORT)
ce144f24 277 vport = dst_port;
1da177e4 278 } else {
5b57a98c
SH
279 /* Note: persistent fwmark-based services and
280 * persistent port zero service are handled here.
281 * fwmark template:
282 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
283 * port zero template:
284 * <protocol,caddr,0,vaddr,0,daddr,0>
1da177e4 285 */
28364a59 286 if (svc->fwmark) {
5b57a98c
SH
287 protocol = IPPROTO_IP;
288 vaddr = &fwmark;
289 }
1da177e4 290 }
a5959d53
HS
291 /* return *ignored = -1 so NF_DROP can be used */
292 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
293 vaddr, vport, &param) < 0) {
294 *ignored = -1;
295 return NULL;
296 }
1da177e4
LT
297 }
298
5b57a98c 299 /* Check if a template already exists */
f11017ec 300 ct = ip_vs_ct_in_get(&param);
5b57a98c 301 if (!ct || !ip_vs_check_template(ct)) {
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
SH
306 */
307 dest = svc->scheduler->schedule(svc, skb);
308 if (!dest) {
309 IP_VS_DBG(1, "p-schedule: no dest found.\n");
85999283 310 kfree(param.pe_data);
a5959d53 311 *ignored = 0;
5b57a98c
SH
312 return NULL;
313 }
314
ce144f24 315 if (dst_port == svc->port && svc->port != FTPPORT)
5b57a98c
SH
316 dport = dest->port;
317
85999283
SH
318 /* Create a template
319 * This adds param.pe_data to the template,
320 * and thus param.pe_data will be destroyed
321 * when the template expires */
f11017ec 322 ct = ip_vs_conn_new(&param, &dest->addr, dport,
0e051e68 323 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
85999283
SH
324 if (ct == NULL) {
325 kfree(param.pe_data);
a5959d53 326 *ignored = -1;
5b57a98c 327 return NULL;
85999283 328 }
5b57a98c
SH
329
330 ct->timeout = svc->timeout;
85999283 331 } else {
5b57a98c
SH
332 /* set destination with the found template */
333 dest = ct->dest;
85999283
SH
334 kfree(param.pe_data);
335 }
5b57a98c 336
ce144f24 337 dport = dst_port;
5b57a98c
SH
338 if (dport == svc->port && dest->port)
339 dport = dest->port;
340
26ec037f 341 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
d4383f04 342 && iph->protocol == IPPROTO_UDP) ?
26ec037f
NC
343 IP_VS_CONN_F_ONE_PACKET : 0;
344
1da177e4
LT
345 /*
346 * Create a new connection according to the template
347 */
d4383f04
JDB
348 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
349 src_port, &iph->daddr, dst_port, &param);
ce144f24 350
0e051e68 351 cp = ip_vs_conn_new(&param, &dest->addr, dport, flags, dest, skb->mark);
1da177e4
LT
352 if (cp == NULL) {
353 ip_vs_conn_put(ct);
a5959d53 354 *ignored = -1;
1da177e4
LT
355 return NULL;
356 }
357
358 /*
359 * Add its control
360 */
361 ip_vs_control_add(cp, ct);
362 ip_vs_conn_put(ct);
363
364 ip_vs_conn_stats(cp, svc);
365 return cp;
366}
367
368
369/*
370 * IPVS main scheduling function
371 * It selects a server according to the virtual service, and
372 * creates a connection entry.
373 * Protocols supported: TCP, UDP
a5959d53
HS
374 *
375 * Usage of *ignored
376 *
377 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
378 * svc/scheduler decides that this packet should be accepted with
379 * NF_ACCEPT because it must not be scheduled.
380 *
381 * 0 : scheduler can not find destination, so try bypass or
382 * return ICMP and then NF_DROP (ip_vs_leave).
383 *
384 * -1 : scheduler tried to schedule but fatal error occurred, eg.
385 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
386 * failure such as missing Call-ID, ENOMEM on skb_linearize
387 * or pe_data. In this case we should return NF_DROP without
388 * any attempts to send ICMP with ip_vs_leave.
1da177e4
LT
389 */
390struct ip_vs_conn *
190ecd27 391ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
d4383f04
JDB
392 struct ip_vs_proto_data *pd, int *ignored,
393 struct ip_vs_iphdr *iph)
1da177e4 394{
9330419d 395 struct ip_vs_protocol *pp = pd->pp;
1da177e4 396 struct ip_vs_conn *cp = NULL;
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
452 dest = svc->scheduler->schedule(svc, skb);
453 if (dest == NULL) {
454 IP_VS_DBG(1, "Schedule: no dest found.\n");
455 return NULL;
456 }
457
26ec037f 458 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
d4383f04 459 && iph->protocol == IPPROTO_UDP) ?
26ec037f
NC
460 IP_VS_CONN_F_ONE_PACKET : 0;
461
1da177e4
LT
462 /*
463 * Create a connection entry.
464 */
f11017ec
SH
465 {
466 struct ip_vs_conn_param p;
6e67e586 467
d4383f04
JDB
468 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
469 &iph->saddr, pptr[0], &iph->daddr,
470 pptr[1], &p);
f11017ec
SH
471 cp = ip_vs_conn_new(&p, &dest->addr,
472 dest->port ? dest->port : pptr[1],
0e051e68 473 flags, dest, skb->mark);
a5959d53
HS
474 if (!cp) {
475 *ignored = -1;
f11017ec 476 return NULL;
a5959d53 477 }
f11017ec 478 }
1da177e4 479
cd17f9ed
JV
480 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
481 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
482 ip_vs_fwd_tag(cp),
483 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
484 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
485 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
486 cp->flags, atomic_read(&cp->refcnt));
1da177e4
LT
487
488 ip_vs_conn_stats(cp, svc);
489 return cp;
490}
491
492
493/*
494 * Pass or drop the packet.
495 * Called by ip_vs_in, when the virtual service is available but
496 * no destination is available for a new connection.
497 */
498int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
d4383f04 499 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
1da177e4 500{
014d730d 501 __be16 _ports[2], *pptr;
a7a86b86
SH
502#ifdef CONFIG_SYSCTL
503 struct net *net;
504 struct netns_ipvs *ipvs;
2a3b791e 505 int unicast;
a7a86b86 506#endif
9330419d 507
d4383f04 508 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
1da177e4
LT
509 if (pptr == NULL) {
510 ip_vs_service_put(svc);
511 return NF_DROP;
512 }
a7a86b86
SH
513
514#ifdef CONFIG_SYSCTL
4a98480b 515 net = skb_net(skb);
1da177e4 516
2a3b791e
JV
517#ifdef CONFIG_IP_VS_IPV6
518 if (svc->af == AF_INET6)
d4383f04 519 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
2a3b791e
JV
520 else
521#endif
d4383f04 522 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
2a3b791e 523
1da177e4 524 /* if it is fwmark-based service, the cache_bypass sysctl is up
2a3b791e 525 and the destination is a non-local unicast, then create
1da177e4 526 a cache_bypass connection entry */
4a98480b 527 ipvs = net_ipvs(net);
a0840e2e 528 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
ad542ced 529 int ret;
1da177e4 530 struct ip_vs_conn *cp;
3575792e 531 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
d4383f04 532 iph->protocol == IPPROTO_UDP) ?
3575792e 533 IP_VS_CONN_F_ONE_PACKET : 0;
dff630dd 534 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
1da177e4
LT
535
536 ip_vs_service_put(svc);
537
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 */
574 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
575 ip_vs_service_put(svc);
576 return NF_ACCEPT;
577 }
578
579 ip_vs_service_put(svc);
580
581 /*
582 * Notify the client that the destination is unreachable, and
583 * release the socket buffer.
584 * Since it is in IP layer, the TCP socket is not actually
585 * created, the TCP RST packet cannot be sent, instead that
586 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
587 */
2a3b791e 588#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
589 if (svc->af == AF_INET6) {
590 if (!skb->dev) {
591 struct net *net = dev_net(skb_dst(skb)->dev);
592
593 skb->dev = net->loopback_dev;
594 }
3ffe533c 595 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
cb59155f 596 } else
2a3b791e
JV
597#endif
598 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
599
1da177e4
LT
600 return NF_DROP;
601}
602
84b3cee3
SH
603#ifdef CONFIG_SYSCTL
604
605static int sysctl_snat_reroute(struct sk_buff *skb)
606{
607 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
608 return ipvs->sysctl_snat_reroute;
609}
610
0cfa558e
SH
611static int sysctl_nat_icmp_send(struct net *net)
612{
613 struct netns_ipvs *ipvs = net_ipvs(net);
614 return ipvs->sysctl_nat_icmp_send;
615}
616
71a8ab6c
SH
617static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
618{
619 return ipvs->sysctl_expire_nodest_conn;
620}
621
84b3cee3
SH
622#else
623
624static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
0cfa558e 625static int sysctl_nat_icmp_send(struct net *net) { return 0; }
71a8ab6c 626static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
84b3cee3
SH
627
628#endif
629
b1550f22 630__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
1da177e4 631{
d3bc23e7 632 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
1da177e4
LT
633}
634
1ca5bb54
JA
635static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
636{
637 if (NF_INET_LOCAL_IN == hooknum)
638 return IP_DEFRAG_VS_IN;
639 if (NF_INET_FORWARD == hooknum)
640 return IP_DEFRAG_VS_FWD;
641 return IP_DEFRAG_VS_OUT;
642}
643
776c729e 644static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
1da177e4 645{
776c729e
HX
646 int err = ip_defrag(skb, user);
647
648 if (!err)
eddc9ec5 649 ip_send_check(ip_hdr(skb));
776c729e
HX
650
651 return err;
1da177e4
LT
652}
653
ba4fd7e9
SH
654static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
655{
ba4fd7e9
SH
656#ifdef CONFIG_IP_VS_IPV6
657 if (af == AF_INET6) {
84b3cee3 658 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
ba4fd7e9
SH
659 return 1;
660 } else
661#endif
84b3cee3 662 if ((sysctl_snat_reroute(skb) ||
ba4fd7e9
SH
663 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
664 ip_route_me_harder(skb, RTN_LOCAL) != 0)
665 return 1;
666
667 return 0;
668}
669
1da177e4
LT
670/*
671 * Packet has been made sufficiently writable in caller
672 * - inout: 1=in->out, 0=out->in
673 */
674void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
675 struct ip_vs_conn *cp, int inout)
676{
eddc9ec5 677 struct iphdr *iph = ip_hdr(skb);
1da177e4 678 unsigned int icmp_offset = iph->ihl*4;
d56f90a7
ACM
679 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
680 icmp_offset);
1da177e4
LT
681 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
682
683 if (inout) {
e7ade46a 684 iph->saddr = cp->vaddr.ip;
1da177e4 685 ip_send_check(iph);
e7ade46a 686 ciph->daddr = cp->vaddr.ip;
1da177e4
LT
687 ip_send_check(ciph);
688 } else {
e7ade46a 689 iph->daddr = cp->daddr.ip;
1da177e4 690 ip_send_check(iph);
e7ade46a 691 ciph->saddr = cp->daddr.ip;
1da177e4
LT
692 ip_send_check(ciph);
693 }
694
2906f66a
VMR
695 /* the TCP/UDP/SCTP port */
696 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
697 IPPROTO_SCTP == ciph->protocol) {
014d730d 698 __be16 *ports = (void *)ciph + ciph->ihl*4;
1da177e4
LT
699
700 if (inout)
701 ports[1] = cp->vport;
702 else
703 ports[0] = cp->dport;
704 }
705
706 /* And finally the ICMP checksum */
707 icmph->checksum = 0;
708 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
709 skb->ip_summed = CHECKSUM_UNNECESSARY;
710
711 if (inout)
0d79641a 712 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
713 "Forwarding altered outgoing ICMP");
714 else
0d79641a 715 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
1da177e4
LT
716 "Forwarding altered incoming ICMP");
717}
718
b3cdd2a7
JV
719#ifdef CONFIG_IP_VS_IPV6
720void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
721 struct ip_vs_conn *cp, int inout)
722{
723 struct ipv6hdr *iph = ipv6_hdr(skb);
63dca2c0
JDB
724 unsigned int icmp_offset = 0;
725 unsigned int offs = 0; /* header offset*/
726 int protocol;
727 struct icmp6hdr *icmph;
728 struct ipv6hdr *ciph;
729 unsigned short fragoffs;
730
731 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
732 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
733 offs = icmp_offset + sizeof(struct icmp6hdr);
734 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
735
736 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
b3cdd2a7
JV
737
738 if (inout) {
739 iph->saddr = cp->vaddr.in6;
740 ciph->daddr = cp->vaddr.in6;
741 } else {
742 iph->daddr = cp->daddr.in6;
743 ciph->saddr = cp->daddr.in6;
744 }
745
2906f66a 746 /* the TCP/UDP/SCTP port */
63dca2c0
JDB
747 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
748 IPPROTO_SCTP == protocol)) {
749 __be16 *ports = (void *)(skb_network_header(skb) + offs);
b3cdd2a7 750
63dca2c0
JDB
751 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
752 ntohs(inout ? ports[1] : ports[0]),
753 ntohs(inout ? cp->vport : cp->dport));
b3cdd2a7
JV
754 if (inout)
755 ports[1] = cp->vport;
756 else
757 ports[0] = cp->dport;
758 }
759
760 /* And finally the ICMP checksum */
8870f842
SH
761 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
762 skb->len - icmp_offset,
763 IPPROTO_ICMPV6, 0);
764 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
765 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
766 skb->ip_summed = CHECKSUM_PARTIAL;
b3cdd2a7
JV
767
768 if (inout)
0d79641a
JA
769 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
770 (void *)ciph - (void *)iph,
771 "Forwarding altered outgoing ICMPv6");
b3cdd2a7 772 else
0d79641a
JA
773 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
774 (void *)ciph - (void *)iph,
775 "Forwarding altered incoming ICMPv6");
b3cdd2a7
JV
776}
777#endif
778
4856c84c 779/* Handle relevant response ICMP messages - forward to the right
6cb90db5 780 * destination host.
4856c84c 781 */
f2428ed5
SH
782static int handle_response_icmp(int af, struct sk_buff *skb,
783 union nf_inet_addr *snet,
784 __u8 protocol, struct ip_vs_conn *cp,
4856c84c
MT
785 struct ip_vs_protocol *pp,
786 unsigned int offset, unsigned int ihl)
787{
788 unsigned int verdict = NF_DROP;
789
790 if (IP_VS_FWD_METHOD(cp) != 0) {
1e3e238e
HE
791 pr_err("shouldn't reach here, because the box is on the "
792 "half connection in the tun/dr module.\n");
4856c84c
MT
793 }
794
795 /* Ensure the checksum is correct */
796 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
797 /* Failed checksum! */
f2428ed5
SH
798 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
799 IP_VS_DBG_ADDR(af, snet));
4856c84c
MT
800 goto out;
801 }
802
2906f66a
VMR
803 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
804 IPPROTO_SCTP == protocol)
4856c84c
MT
805 offset += 2 * sizeof(__u16);
806 if (!skb_make_writable(skb, offset))
807 goto out;
808
f2428ed5
SH
809#ifdef CONFIG_IP_VS_IPV6
810 if (af == AF_INET6)
811 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
812 else
813#endif
814 ip_vs_nat_icmp(skb, pp, cp, 1);
4856c84c 815
ba4fd7e9
SH
816 if (ip_vs_route_me_harder(af, skb))
817 goto out;
f5a41847 818
4856c84c
MT
819 /* do the statistics and put it back */
820 ip_vs_out_stats(cp, skb);
821
cf356d69 822 skb->ipvs_property = 1;
f4bc17cd 823 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 824 ip_vs_notrack(skb);
f4bc17cd
JA
825 else
826 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
827 verdict = NF_ACCEPT;
828
829out:
830 __ip_vs_conn_put(cp);
831
832 return verdict;
833}
834
1da177e4
LT
835/*
836 * Handle ICMP messages in the inside-to-outside direction (outgoing).
4856c84c 837 * Find any that might be relevant, check against existing connections.
1da177e4 838 * Currently handles error types - unreachable, quench, ttl exceeded.
1da177e4 839 */
1ca5bb54
JA
840static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
841 unsigned int hooknum)
1da177e4 842{
1da177e4
LT
843 struct iphdr *iph;
844 struct icmphdr _icmph, *ic;
845 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 846 struct ip_vs_iphdr ciph;
1da177e4
LT
847 struct ip_vs_conn *cp;
848 struct ip_vs_protocol *pp;
4856c84c 849 unsigned int offset, ihl;
f2428ed5 850 union nf_inet_addr snet;
1da177e4
LT
851
852 *related = 1;
853
854 /* reassemble IP fragments */
56f8a75c 855 if (ip_is_fragment(ip_hdr(skb))) {
1ca5bb54 856 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1da177e4 857 return NF_STOLEN;
1da177e4
LT
858 }
859
eddc9ec5 860 iph = ip_hdr(skb);
1da177e4
LT
861 offset = ihl = iph->ihl * 4;
862 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
863 if (ic == NULL)
864 return NF_DROP;
865
14d5e834 866 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 867 ic->type, ntohs(icmp_id(ic)),
14d5e834 868 &iph->saddr, &iph->daddr);
1da177e4
LT
869
870 /*
871 * Work through seeing if this is for us.
872 * These checks are supposed to be in an order that means easy
873 * things are checked first to speed up processing.... however
874 * this means that some packets will manage to get a long way
875 * down this stack and then be rejected, but that's life.
876 */
877 if ((ic->type != ICMP_DEST_UNREACH) &&
878 (ic->type != ICMP_SOURCE_QUENCH) &&
879 (ic->type != ICMP_TIME_EXCEEDED)) {
880 *related = 0;
881 return NF_ACCEPT;
882 }
883
884 /* Now find the contained IP header */
885 offset += sizeof(_icmph);
886 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
887 if (cih == NULL)
888 return NF_ACCEPT; /* The packet looks wrong, ignore */
889
890 pp = ip_vs_proto_get(cih->protocol);
891 if (!pp)
892 return NF_ACCEPT;
893
894 /* Is the embedded protocol header present? */
4412ec49 895 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
896 pp->dont_defrag))
897 return NF_ACCEPT;
898
0d79641a
JA
899 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
900 "Checking outgoing ICMP for");
1da177e4 901
63dca2c0
JDB
902 ip_vs_fill_ip4hdr(cih, &ciph);
903 ciph.len += offset;
1da177e4 904 /* The embedded headers contain source and dest in reverse order */
d4383f04 905 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
1da177e4
LT
906 if (!cp)
907 return NF_ACCEPT;
908
f2428ed5
SH
909 snet.ip = iph->saddr;
910 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
63dca2c0 911 pp, ciph.len, ihl);
1da177e4
LT
912}
913
2a3b791e 914#ifdef CONFIG_IP_VS_IPV6
1ca5bb54 915static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
d4383f04 916 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
2a3b791e 917{
2a3b791e 918 struct icmp6hdr _icmph, *ic;
63dca2c0
JDB
919 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
920 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
2a3b791e
JV
921 struct ip_vs_conn *cp;
922 struct ip_vs_protocol *pp;
f2428ed5 923 union nf_inet_addr snet;
63dca2c0 924 unsigned int writable;
2a3b791e 925
63dca2c0 926 *related = 1;
2f74713d 927 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
2a3b791e
JV
928 if (ic == NULL)
929 return NF_DROP;
930
2a3b791e
JV
931 /*
932 * Work through seeing if this is for us.
933 * These checks are supposed to be in an order that means easy
934 * things are checked first to speed up processing.... however
935 * this means that some packets will manage to get a long way
936 * down this stack and then be rejected, but that's life.
937 */
2fab8917 938 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
2a3b791e
JV
939 *related = 0;
940 return NF_ACCEPT;
941 }
2f74713d
JDB
942 /* Fragment header that is before ICMP header tells us that:
943 * it's not an error message since they can't be fragmented.
944 */
e7165030 945 if (ipvsh->flags & IP6_FH_F_FRAG)
2f74713d 946 return NF_DROP;
2a3b791e 947
63dca2c0
JDB
948 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
949 ic->icmp6_type, ntohs(icmpv6_id(ic)),
950 &ipvsh->saddr, &ipvsh->daddr);
951
2a3b791e 952 /* Now find the contained IP header */
63dca2c0
JDB
953 ciph.len = ipvsh->len + sizeof(_icmph);
954 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
955 if (ip6h == NULL)
2a3b791e 956 return NF_ACCEPT; /* The packet looks wrong, ignore */
63dca2c0
JDB
957 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
958 ciph.daddr.in6 = ip6h->daddr;
959 /* skip possible IPv6 exthdrs of contained IPv6 packet */
960 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
961 if (ciph.protocol < 0)
962 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
963
964 pp = ip_vs_proto_get(ciph.protocol);
2a3b791e
JV
965 if (!pp)
966 return NF_ACCEPT;
967
2a3b791e 968 /* The embedded headers contain source and dest in reverse order */
d4383f04 969 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
2a3b791e
JV
970 if (!cp)
971 return NF_ACCEPT;
972
63dca2c0
JDB
973 snet.in6 = ciph.saddr.in6;
974 writable = ciph.len;
975 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
976 pp, writable, sizeof(struct ipv6hdr));
2a3b791e
JV
977}
978#endif
979
2906f66a
VMR
980/*
981 * Check if sctp chunc is ABORT chunk
982 */
983static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
984{
985 sctp_chunkhdr_t *sch, schunk;
986 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
987 sizeof(schunk), &schunk);
988 if (sch == NULL)
989 return 0;
990 if (sch->type == SCTP_CID_ABORT)
991 return 1;
992 return 0;
993}
994
2a3b791e 995static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1da177e4
LT
996{
997 struct tcphdr _tcph, *th;
998
2a3b791e 999 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1da177e4
LT
1000 if (th == NULL)
1001 return 0;
1002 return th->rst;
1003}
1004
4856c84c 1005/* Handle response packets: rewrite addresses and send away...
4856c84c
MT
1006 */
1007static unsigned int
9330419d 1008handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
d4383f04 1009 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
4856c84c 1010{
9330419d
HS
1011 struct ip_vs_protocol *pp = pd->pp;
1012
0d79641a 1013 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
4856c84c 1014
d4383f04 1015 if (!skb_make_writable(skb, iph->len))
4856c84c
MT
1016 goto drop;
1017
1018 /* mangle the packet */
d4383f04 1019 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
4856c84c
MT
1020 goto drop;
1021
1022#ifdef CONFIG_IP_VS_IPV6
1023 if (af == AF_INET6)
1024 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1025 else
1026#endif
1027 {
1028 ip_hdr(skb)->saddr = cp->vaddr.ip;
1029 ip_send_check(ip_hdr(skb));
1030 }
1031
8a803040
JA
1032 /*
1033 * nf_iterate does not expect change in the skb->dst->dev.
1034 * It looks like it is not fatal to enable this code for hooks
1035 * where our handlers are at the end of the chain list and
1036 * when all next handlers use skb->dst->dev and not outdev.
1037 * It will definitely route properly the inout NAT traffic
1038 * when multiple paths are used.
1039 */
1040
4856c84c
MT
1041 /* For policy routing, packets originating from this
1042 * machine itself may be routed differently to packets
1043 * passing through. We want this packet to be routed as
1044 * if it came from this machine itself. So re-compute
1045 * the routing information.
1046 */
ba4fd7e9
SH
1047 if (ip_vs_route_me_harder(af, skb))
1048 goto drop;
4856c84c 1049
0d79641a 1050 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
4856c84c
MT
1051
1052 ip_vs_out_stats(cp, skb);
9330419d 1053 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
cf356d69 1054 skb->ipvs_property = 1;
f4bc17cd 1055 if (!(cp->flags & IP_VS_CONN_F_NFCT))
cf356d69 1056 ip_vs_notrack(skb);
f4bc17cd
JA
1057 else
1058 ip_vs_update_conntrack(skb, cp, 0);
4856c84c
MT
1059 ip_vs_conn_put(cp);
1060
4856c84c
MT
1061 LeaveFunction(11);
1062 return NF_ACCEPT;
1063
1064drop:
1065 ip_vs_conn_put(cp);
1066 kfree_skb(skb);
f4bc17cd 1067 LeaveFunction(11);
4856c84c
MT
1068 return NF_STOLEN;
1069}
1070
1da177e4 1071/*
4856c84c 1072 * Check if outgoing packet belongs to the established ip_vs_conn.
1da177e4
LT
1073 */
1074static unsigned int
fc604767 1075ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1da177e4 1076{
fc723250 1077 struct net *net = NULL;
51ef348b 1078 struct ip_vs_iphdr iph;
1da177e4 1079 struct ip_vs_protocol *pp;
9330419d 1080 struct ip_vs_proto_data *pd;
1da177e4 1081 struct ip_vs_conn *cp;
1da177e4
LT
1082
1083 EnterFunction(11);
1084
fc604767 1085 /* Already marked as IPVS request or reply? */
6869c4d8 1086 if (skb->ipvs_property)
1da177e4
LT
1087 return NF_ACCEPT;
1088
fc604767
JA
1089 /* Bad... Do not break raw sockets */
1090 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1091 af == AF_INET)) {
1092 struct sock *sk = skb->sk;
1093 struct inet_sock *inet = inet_sk(skb->sk);
1094
1095 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1096 return NF_ACCEPT;
1097 }
1098
1099 if (unlikely(!skb_dst(skb)))
1100 return NF_ACCEPT;
1101
fc723250 1102 net = skb_net(skb);
7a4f0761
HS
1103 if (!net_ipvs(net)->enable)
1104 return NF_ACCEPT;
1105
63dca2c0 1106 ip_vs_fill_iph_skb(af, skb, &iph);
2a3b791e
JV
1107#ifdef CONFIG_IP_VS_IPV6
1108 if (af == AF_INET6) {
2f74713d
JDB
1109 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1110 struct sk_buff *reasm = skb_nfct_reasm(skb);
1111 /* Save fw mark for coming frags */
1112 reasm->ipvs_property = 1;
1113 reasm->mark = skb->mark;
1114 }
2a3b791e 1115 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54
JA
1116 int related;
1117 int verdict = ip_vs_out_icmp_v6(skb, &related,
d4383f04 1118 hooknum, &iph);
1da177e4 1119
f5a41847 1120 if (related)
2a3b791e 1121 return verdict;
2a3b791e
JV
1122 }
1123 } else
1124#endif
1125 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54
JA
1126 int related;
1127 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
2a3b791e 1128
f5a41847 1129 if (related)
2a3b791e 1130 return verdict;
2a3b791e 1131 }
1da177e4 1132
9330419d
HS
1133 pd = ip_vs_proto_data_get(net, iph.protocol);
1134 if (unlikely(!pd))
1da177e4 1135 return NF_ACCEPT;
9330419d 1136 pp = pd->pp;
1da177e4
LT
1137
1138 /* reassemble IP fragments */
2a3b791e 1139#ifdef CONFIG_IP_VS_IPV6
63dca2c0 1140 if (af == AF_INET)
2a3b791e 1141#endif
56f8a75c 1142 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1ca5bb54
JA
1143 if (ip_vs_gather_frags(skb,
1144 ip_vs_defrag_user(hooknum)))
2a3b791e
JV
1145 return NF_STOLEN;
1146
63dca2c0 1147 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
2a3b791e 1148 }
1da177e4
LT
1149
1150 /*
1151 * Check if the packet belongs to an existing entry
1152 */
d4383f04 1153 cp = pp->conn_out_get(af, skb, &iph, 0);
1da177e4 1154
cb59155f 1155 if (likely(cp))
d4383f04 1156 return handle_response(af, skb, pd, cp, &iph);
0cfa558e 1157 if (sysctl_nat_icmp_send(net) &&
cb59155f
JA
1158 (pp->protocol == IPPROTO_TCP ||
1159 pp->protocol == IPPROTO_UDP ||
1160 pp->protocol == IPPROTO_SCTP)) {
1161 __be16 _ports[2], *pptr;
1162
2f74713d
JDB
1163 pptr = frag_safe_skb_hp(skb, iph.len,
1164 sizeof(_ports), _ports, &iph);
cb59155f
JA
1165 if (pptr == NULL)
1166 return NF_ACCEPT; /* Not for me */
fc723250 1167 if (ip_vs_lookup_real_service(net, af, iph.protocol,
cb59155f
JA
1168 &iph.saddr,
1169 pptr[0])) {
1170 /*
1171 * Notify the real server: there is no
1172 * existing entry if it is not RST
1173 * packet or not TCP packet.
1174 */
1175 if ((iph.protocol != IPPROTO_TCP &&
1176 iph.protocol != IPPROTO_SCTP)
1177 || ((iph.protocol == IPPROTO_TCP
1178 && !is_tcp_reset(skb, iph.len))
1179 || (iph.protocol == IPPROTO_SCTP
1180 && !is_sctp_abort(skb,
1181 iph.len)))) {
2a3b791e 1182#ifdef CONFIG_IP_VS_IPV6
cb59155f
JA
1183 if (af == AF_INET6) {
1184 struct net *net =
1185 dev_net(skb_dst(skb)->dev);
1186
1187 if (!skb->dev)
1188 skb->dev = net->loopback_dev;
1189 icmpv6_send(skb,
1190 ICMPV6_DEST_UNREACH,
1191 ICMPV6_PORT_UNREACH,
1192 0);
1193 } else
2a3b791e 1194#endif
cb59155f
JA
1195 icmp_send(skb,
1196 ICMP_DEST_UNREACH,
1197 ICMP_PORT_UNREACH, 0);
1198 return NF_DROP;
1da177e4
LT
1199 }
1200 }
1da177e4 1201 }
0d79641a 1202 IP_VS_DBG_PKT(12, af, pp, skb, 0,
cb59155f
JA
1203 "ip_vs_out: packet continues traversal as normal");
1204 return NF_ACCEPT;
1da177e4
LT
1205}
1206
fc604767 1207/*
cb59155f
JA
1208 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1209 * used only for VS/NAT.
fc604767
JA
1210 * Check if packet is reply for established ip_vs_conn.
1211 */
1212static unsigned int
1213ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1214 const struct net_device *in, const struct net_device *out,
1215 int (*okfn)(struct sk_buff *))
1216{
1217 return ip_vs_out(hooknum, skb, AF_INET);
1218}
1219
1220/*
1221 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1222 * Check if packet is reply for established ip_vs_conn.
1223 */
1224static unsigned int
1225ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1226 const struct net_device *in, const struct net_device *out,
1227 int (*okfn)(struct sk_buff *))
1228{
1229 unsigned int verdict;
1230
1231 /* Disable BH in LOCAL_OUT until all places are fixed */
1232 local_bh_disable();
1233 verdict = ip_vs_out(hooknum, skb, AF_INET);
1234 local_bh_enable();
1235 return verdict;
1236}
1237
1238#ifdef CONFIG_IP_VS_IPV6
1239
1240/*
cb59155f
JA
1241 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1242 * used only for VS/NAT.
fc604767
JA
1243 * Check if packet is reply for established ip_vs_conn.
1244 */
1245static unsigned int
1246ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1247 const struct net_device *in, const struct net_device *out,
1248 int (*okfn)(struct sk_buff *))
1249{
1250 return ip_vs_out(hooknum, skb, AF_INET6);
1251}
1252
1253/*
1254 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1255 * Check if packet is reply for established ip_vs_conn.
1256 */
1257static unsigned int
1258ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1259 const struct net_device *in, const struct net_device *out,
1260 int (*okfn)(struct sk_buff *))
1261{
1262 unsigned int verdict;
1263
1264 /* Disable BH in LOCAL_OUT until all places are fixed */
1265 local_bh_disable();
1266 verdict = ip_vs_out(hooknum, skb, AF_INET6);
1267 local_bh_enable();
1268 return verdict;
1269}
1270
1271#endif
1da177e4
LT
1272
1273/*
1274 * Handle ICMP messages in the outside-to-inside direction (incoming).
1275 * Find any that might be relevant, check against existing connections,
1276 * forward to the right destination host if relevant.
1277 * Currently handles error types - unreachable, quench, ttl exceeded.
1278 */
e905a9ed 1279static int
3db05fea 1280ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1da177e4 1281{
9330419d 1282 struct net *net = NULL;
1da177e4
LT
1283 struct iphdr *iph;
1284 struct icmphdr _icmph, *ic;
1285 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
51ef348b 1286 struct ip_vs_iphdr ciph;
1da177e4
LT
1287 struct ip_vs_conn *cp;
1288 struct ip_vs_protocol *pp;
9330419d 1289 struct ip_vs_proto_data *pd;
f2edb9f7
JA
1290 unsigned int offset, offset2, ihl, verdict;
1291 bool ipip;
1da177e4
LT
1292
1293 *related = 1;
1294
1295 /* reassemble IP fragments */
56f8a75c 1296 if (ip_is_fragment(ip_hdr(skb))) {
1ca5bb54 1297 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1da177e4 1298 return NF_STOLEN;
1da177e4
LT
1299 }
1300
eddc9ec5 1301 iph = ip_hdr(skb);
1da177e4
LT
1302 offset = ihl = iph->ihl * 4;
1303 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1304 if (ic == NULL)
1305 return NF_DROP;
1306
14d5e834 1307 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1da177e4 1308 ic->type, ntohs(icmp_id(ic)),
14d5e834 1309 &iph->saddr, &iph->daddr);
1da177e4
LT
1310
1311 /*
1312 * Work through seeing if this is for us.
1313 * These checks are supposed to be in an order that means easy
1314 * things are checked first to speed up processing.... however
1315 * this means that some packets will manage to get a long way
1316 * down this stack and then be rejected, but that's life.
1317 */
1318 if ((ic->type != ICMP_DEST_UNREACH) &&
1319 (ic->type != ICMP_SOURCE_QUENCH) &&
1320 (ic->type != ICMP_TIME_EXCEEDED)) {
1321 *related = 0;
1322 return NF_ACCEPT;
1323 }
1324
1325 /* Now find the contained IP header */
1326 offset += sizeof(_icmph);
1327 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1328 if (cih == NULL)
1329 return NF_ACCEPT; /* The packet looks wrong, ignore */
1330
9330419d 1331 net = skb_net(skb);
7a4f0761 1332
f2edb9f7
JA
1333 /* Special case for errors for IPIP packets */
1334 ipip = false;
1335 if (cih->protocol == IPPROTO_IPIP) {
1336 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1337 return NF_ACCEPT;
1338 /* Error for our IPIP must arrive at LOCAL_IN */
1339 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1340 return NF_ACCEPT;
1341 offset += cih->ihl * 4;
1342 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1343 if (cih == NULL)
1344 return NF_ACCEPT; /* The packet looks wrong, ignore */
1345 ipip = true;
1346 }
1347
9330419d
HS
1348 pd = ip_vs_proto_data_get(net, cih->protocol);
1349 if (!pd)
1da177e4 1350 return NF_ACCEPT;
9330419d 1351 pp = pd->pp;
1da177e4
LT
1352
1353 /* Is the embedded protocol header present? */
4412ec49 1354 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1da177e4
LT
1355 pp->dont_defrag))
1356 return NF_ACCEPT;
1357
0d79641a
JA
1358 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1359 "Checking incoming ICMP for");
1da177e4 1360
f2edb9f7 1361 offset2 = offset;
63dca2c0
JDB
1362 ip_vs_fill_ip4hdr(cih, &ciph);
1363 ciph.len += offset;
1364 offset = ciph.len;
f2edb9f7
JA
1365 /* The embedded headers contain source and dest in reverse order.
1366 * For IPIP this is error for request, not for reply.
1367 */
d4383f04 1368 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
6cb90db5 1369 if (!cp)
1da177e4
LT
1370 return NF_ACCEPT;
1371
1372 verdict = NF_DROP;
1373
1374 /* Ensure the checksum is correct */
60476372 1375 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1da177e4 1376 /* Failed checksum! */
14d5e834
HH
1377 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1378 &iph->saddr);
1da177e4
LT
1379 goto out;
1380 }
1381
f2edb9f7
JA
1382 if (ipip) {
1383 __be32 info = ic->un.gateway;
1384
1385 /* Update the MTU */
1386 if (ic->type == ICMP_DEST_UNREACH &&
1387 ic->code == ICMP_FRAG_NEEDED) {
1388 struct ip_vs_dest *dest = cp->dest;
1389 u32 mtu = ntohs(ic->un.frag.mtu);
1390
1391 /* Strip outer IP and ICMP, go to IPIP header */
1392 __skb_pull(skb, ihl + sizeof(_icmph));
1393 offset2 -= ihl + sizeof(_icmph);
1394 skb_reset_network_header(skb);
1395 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1396 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
1397 rcu_read_lock();
1398 ipv4_update_pmtu(skb, dev_net(skb->dev),
1399 mtu, 0, 0, 0, 0);
1400 rcu_read_unlock();
1401 /* Client uses PMTUD? */
1402 if (!(cih->frag_off & htons(IP_DF)))
1403 goto ignore_ipip;
1404 /* Prefer the resulting PMTU */
1405 if (dest) {
1406 spin_lock(&dest->dst_lock);
1407 if (dest->dst_cache)
1408 mtu = dst_mtu(dest->dst_cache);
1409 spin_unlock(&dest->dst_lock);
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);
1580 if (!net_ipvs(net)->enable)
1581 return NF_ACCEPT;
1582
63dca2c0 1583 ip_vs_fill_iph_skb(af, skb, &iph);
cb59155f
JA
1584
1585 /* Bad... Do not break raw sockets */
1586 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1587 af == AF_INET)) {
1588 struct sock *sk = skb->sk;
1589 struct inet_sock *inet = inet_sk(skb->sk);
1590
1591 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1592 return NF_ACCEPT;
1593 }
1da177e4 1594
94b26551
JV
1595#ifdef CONFIG_IP_VS_IPV6
1596 if (af == AF_INET6) {
2f74713d
JDB
1597 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1598 struct sk_buff *reasm = skb_nfct_reasm(skb);
1599 /* Save fw mark for coming frags. */
1600 reasm->ipvs_property = 1;
1601 reasm->mark = skb->mark;
1602 }
94b26551 1603 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1ca5bb54 1604 int related;
d4383f04
JDB
1605 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1606 &iph);
1da177e4 1607
94b26551
JV
1608 if (related)
1609 return verdict;
94b26551
JV
1610 }
1611 } else
1612#endif
1613 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1ca5bb54
JA
1614 int related;
1615 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
94b26551
JV
1616
1617 if (related)
1618 return verdict;
94b26551 1619 }
1da177e4
LT
1620
1621 /* Protocol supported? */
9330419d
HS
1622 pd = ip_vs_proto_data_get(net, iph.protocol);
1623 if (unlikely(!pd))
1da177e4 1624 return NF_ACCEPT;
9330419d 1625 pp = pd->pp;
1da177e4
LT
1626 /*
1627 * Check if the packet belongs to an existing connection entry
1628 */
d4383f04 1629 cp = pp->conn_in_get(af, skb, &iph, 0);
63dca2c0 1630 if (unlikely(!cp) && !iph.fragoffs) {
2f74713d
JDB
1631 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1632 * replayed fragment zero will already have created the cp
1633 */
1da177e4
LT
1634 int v;
1635
2f74713d 1636 /* Schedule and create new connection entry into &cp */
d4383f04 1637 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
1da177e4
LT
1638 return v;
1639 }
1640
1641 if (unlikely(!cp)) {
1642 /* sorry, all this trouble for a no-hit :) */
0d79641a 1643 IP_VS_DBG_PKT(12, af, pp, skb, 0,
cb59155f 1644 "ip_vs_in: packet continues traversal as normal");
2f74713d
JDB
1645 if (iph.fragoffs && !skb_nfct_reasm(skb)) {
1646 /* Fragment that couldn't be mapped to a conn entry
1647 * and don't have any pointer to a reasm skb
1648 * is missing module nf_defrag_ipv6
1649 */
1650 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1651 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1652 }
1da177e4
LT
1653 return NF_ACCEPT;
1654 }
1655
0d79641a 1656 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
f131315f 1657 ipvs = net_ipvs(net);
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;
1da177e4 1818
3db05fea 1819 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1da177e4
LT
1820 return NF_ACCEPT;
1821
7a4f0761
HS
1822 /* ipvs enabled in this netns ? */
1823 net = skb_net(skb);
1824 if (!net_ipvs(net)->enable)
1825 return NF_ACCEPT;
1826
3db05fea 1827 return ip_vs_in_icmp(skb, &r, hooknum);
1da177e4
LT
1828}
1829
2a3b791e
JV
1830#ifdef CONFIG_IP_VS_IPV6
1831static unsigned int
1832ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1833 const struct net_device *in, const struct net_device *out,
1834 int (*okfn)(struct sk_buff *))
1835{
1836 int r;
7a4f0761 1837 struct net *net;
63dca2c0 1838 struct ip_vs_iphdr iphdr;
2a3b791e 1839
63dca2c0
JDB
1840 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1841 if (iphdr.protocol != IPPROTO_ICMPV6)
2a3b791e
JV
1842 return NF_ACCEPT;
1843
7a4f0761
HS
1844 /* ipvs enabled in this netns ? */
1845 net = skb_net(skb);
1846 if (!net_ipvs(net)->enable)
1847 return NF_ACCEPT;
1848
d4383f04 1849 return ip_vs_in_icmp_v6(skb, &r, hooknum, &iphdr);
2a3b791e
JV
1850}
1851#endif
1852
1da177e4 1853
1999414a 1854static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
cb59155f
JA
1855 /* After packet filtering, change source only for VS/NAT */
1856 {
1857 .hook = ip_vs_reply4,
1858 .owner = THIS_MODULE,
4c809d63 1859 .pf = NFPROTO_IPV4,
cb59155f 1860 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1861 .priority = NF_IP_PRI_NAT_SRC - 2,
cb59155f 1862 },
41c5b317
PM
1863 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1864 * or VS/NAT(change destination), so that filtering rules can be
1865 * applied to IPVS. */
1866 {
cb59155f 1867 .hook = ip_vs_remote_request4,
41c5b317 1868 .owner = THIS_MODULE,
4c809d63 1869 .pf = NFPROTO_IPV4,
cb59155f 1870 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1871 .priority = NF_IP_PRI_NAT_SRC - 1,
41c5b317 1872 },
fc604767 1873 /* Before ip_vs_in, change source only for VS/NAT */
41c5b317 1874 {
fc604767 1875 .hook = ip_vs_local_reply4,
41c5b317 1876 .owner = THIS_MODULE,
4c809d63 1877 .pf = NFPROTO_IPV4,
fc604767 1878 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1879 .priority = NF_IP_PRI_NAT_DST + 1,
41c5b317 1880 },
cb59155f
JA
1881 /* After mangle, schedule and forward local requests */
1882 {
1883 .hook = ip_vs_local_request4,
1884 .owner = THIS_MODULE,
4c809d63 1885 .pf = NFPROTO_IPV4,
cb59155f 1886 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1887 .priority = NF_IP_PRI_NAT_DST + 2,
cb59155f 1888 },
41c5b317
PM
1889 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1890 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1891 {
1892 .hook = ip_vs_forward_icmp,
1893 .owner = THIS_MODULE,
4c809d63 1894 .pf = NFPROTO_IPV4,
cb59155f
JA
1895 .hooknum = NF_INET_FORWARD,
1896 .priority = 99,
41c5b317 1897 },
fc604767
JA
1898 /* After packet filtering, change source only for VS/NAT */
1899 {
1900 .hook = ip_vs_reply4,
1901 .owner = THIS_MODULE,
4c809d63 1902 .pf = NFPROTO_IPV4,
fc604767
JA
1903 .hooknum = NF_INET_FORWARD,
1904 .priority = 100,
1905 },
473b23d3 1906#ifdef CONFIG_IP_VS_IPV6
2f74713d
JDB
1907 /* After mangle & nat fetch 2:nd fragment and following */
1908 {
1909 .hook = ip_vs_preroute_frag6,
1910 .owner = THIS_MODULE,
1911 .pf = NFPROTO_IPV6,
1912 .hooknum = NF_INET_PRE_ROUTING,
1913 .priority = NF_IP6_PRI_NAT_DST + 1,
1914 },
cb59155f
JA
1915 /* After packet filtering, change source only for VS/NAT */
1916 {
1917 .hook = ip_vs_reply6,
1918 .owner = THIS_MODULE,
4c809d63 1919 .pf = NFPROTO_IPV6,
cb59155f 1920 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1921 .priority = NF_IP6_PRI_NAT_SRC - 2,
cb59155f 1922 },
473b23d3
JV
1923 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1924 * or VS/NAT(change destination), so that filtering rules can be
1925 * applied to IPVS. */
1926 {
cb59155f 1927 .hook = ip_vs_remote_request6,
473b23d3 1928 .owner = THIS_MODULE,
4c809d63 1929 .pf = NFPROTO_IPV6,
cb59155f 1930 .hooknum = NF_INET_LOCAL_IN,
afb523c5 1931 .priority = NF_IP6_PRI_NAT_SRC - 1,
473b23d3 1932 },
fc604767 1933 /* Before ip_vs_in, change source only for VS/NAT */
473b23d3 1934 {
fc604767 1935 .hook = ip_vs_local_reply6,
473b23d3 1936 .owner = THIS_MODULE,
4c809d63 1937 .pf = NFPROTO_IPV4,
fc604767 1938 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1939 .priority = NF_IP6_PRI_NAT_DST + 1,
473b23d3 1940 },
cb59155f
JA
1941 /* After mangle, schedule and forward local requests */
1942 {
1943 .hook = ip_vs_local_request6,
1944 .owner = THIS_MODULE,
4c809d63 1945 .pf = NFPROTO_IPV6,
cb59155f 1946 .hooknum = NF_INET_LOCAL_OUT,
afb523c5 1947 .priority = NF_IP6_PRI_NAT_DST + 2,
cb59155f 1948 },
473b23d3
JV
1949 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1950 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1951 {
1952 .hook = ip_vs_forward_icmp_v6,
1953 .owner = THIS_MODULE,
4c809d63 1954 .pf = NFPROTO_IPV6,
cb59155f
JA
1955 .hooknum = NF_INET_FORWARD,
1956 .priority = 99,
473b23d3 1957 },
fc604767
JA
1958 /* After packet filtering, change source only for VS/NAT */
1959 {
1960 .hook = ip_vs_reply6,
1961 .owner = THIS_MODULE,
4c809d63 1962 .pf = NFPROTO_IPV6,
fc604767
JA
1963 .hooknum = NF_INET_FORWARD,
1964 .priority = 100,
1965 },
473b23d3 1966#endif
1da177e4 1967};
61b1ab45
HS
1968/*
1969 * Initialize IP Virtual Server netns mem.
1970 */
1971static int __net_init __ip_vs_init(struct net *net)
1972{
1973 struct netns_ipvs *ipvs;
1974
61b1ab45 1975 ipvs = net_generic(net, ip_vs_net_id);
0a9ee813 1976 if (ipvs == NULL)
61b1ab45 1977 return -ENOMEM;
0a9ee813 1978
7a4f0761
HS
1979 /* Hold the beast until a service is registerd */
1980 ipvs->enable = 0;
f6340ee0 1981 ipvs->net = net;
61b1ab45
HS
1982 /* Counters used for creating unique names */
1983 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1984 atomic_inc(&ipvs_netns_cnt);
1985 net->ipvs = ipvs;
7a4f0761 1986
503cf15a 1987 if (ip_vs_estimator_net_init(net) < 0)
7a4f0761
HS
1988 goto estimator_fail;
1989
503cf15a 1990 if (ip_vs_control_net_init(net) < 0)
7a4f0761
HS
1991 goto control_fail;
1992
503cf15a 1993 if (ip_vs_protocol_net_init(net) < 0)
7a4f0761
HS
1994 goto protocol_fail;
1995
503cf15a 1996 if (ip_vs_app_net_init(net) < 0)
7a4f0761
HS
1997 goto app_fail;
1998
503cf15a 1999 if (ip_vs_conn_net_init(net) < 0)
7a4f0761
HS
2000 goto conn_fail;
2001
503cf15a 2002 if (ip_vs_sync_net_init(net) < 0)
7a4f0761
HS
2003 goto sync_fail;
2004
a870c8c5 2005 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
61b1ab45
HS
2006 sizeof(struct netns_ipvs), ipvs->gen);
2007 return 0;
7a4f0761
HS
2008/*
2009 * Error handling
2010 */
2011
2012sync_fail:
503cf15a 2013 ip_vs_conn_net_cleanup(net);
7a4f0761 2014conn_fail:
503cf15a 2015 ip_vs_app_net_cleanup(net);
7a4f0761 2016app_fail:
503cf15a 2017 ip_vs_protocol_net_cleanup(net);
7a4f0761 2018protocol_fail:
503cf15a 2019 ip_vs_control_net_cleanup(net);
7a4f0761 2020control_fail:
503cf15a 2021 ip_vs_estimator_net_cleanup(net);
7a4f0761 2022estimator_fail:
39f618b4 2023 net->ipvs = NULL;
7a4f0761 2024 return -ENOMEM;
61b1ab45
HS
2025}
2026
2027static void __net_exit __ip_vs_cleanup(struct net *net)
2028{
503cf15a
HS
2029 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2030 ip_vs_conn_net_cleanup(net);
2031 ip_vs_app_net_cleanup(net);
2032 ip_vs_protocol_net_cleanup(net);
2033 ip_vs_control_net_cleanup(net);
2034 ip_vs_estimator_net_cleanup(net);
1ae132b0 2035 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
39f618b4 2036 net->ipvs = NULL;
61b1ab45
HS
2037}
2038
7a4f0761
HS
2039static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2040{
2041 EnterFunction(2);
2042 net_ipvs(net)->enable = 0; /* Disable packet reception */
8f4e0a18 2043 smp_wmb();
503cf15a 2044 ip_vs_sync_net_cleanup(net);
7a4f0761
HS
2045 LeaveFunction(2);
2046}
2047
61b1ab45
HS
2048static struct pernet_operations ipvs_core_ops = {
2049 .init = __ip_vs_init,
2050 .exit = __ip_vs_cleanup,
2051 .id = &ip_vs_net_id,
2052 .size = sizeof(struct netns_ipvs),
2053};
1da177e4 2054
7a4f0761
HS
2055static struct pernet_operations ipvs_core_dev_ops = {
2056 .exit = __ip_vs_dev_cleanup,
2057};
2058
1da177e4
LT
2059/*
2060 * Initialize IP Virtual Server
2061 */
2062static int __init ip_vs_init(void)
2063{
2064 int ret;
2065
2066 ret = ip_vs_control_init();
2067 if (ret < 0) {
1e3e238e 2068 pr_err("can't setup control.\n");
6c8f7949 2069 goto exit;
1da177e4
LT
2070 }
2071
2072 ip_vs_protocol_init();
2073
1da177e4
LT
2074 ret = ip_vs_conn_init();
2075 if (ret < 0) {
1e3e238e 2076 pr_err("can't setup connection table.\n");
6c8f7949 2077 goto cleanup_protocol;
61b1ab45
HS
2078 }
2079
7a4f0761
HS
2080 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2081 if (ret < 0)
6c8f7949 2082 goto cleanup_conn;
7a4f0761
HS
2083
2084 ret = register_pernet_device(&ipvs_core_dev_ops);
2085 if (ret < 0)
2086 goto cleanup_sub;
2087
41c5b317 2088 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
1da177e4 2089 if (ret < 0) {
1e3e238e 2090 pr_err("can't register hooks.\n");
7a4f0761 2091 goto cleanup_dev;
1da177e4
LT
2092 }
2093
8537de8a
HS
2094 ret = ip_vs_register_nl_ioctl();
2095 if (ret < 0) {
2096 pr_err("can't register netlink/ioctl.\n");
2097 goto cleanup_hooks;
2098 }
2099
1e3e238e 2100 pr_info("ipvs loaded.\n");
7a4f0761 2101
1da177e4
LT
2102 return ret;
2103
8537de8a
HS
2104cleanup_hooks:
2105 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
7a4f0761
HS
2106cleanup_dev:
2107 unregister_pernet_device(&ipvs_core_dev_ops);
2108cleanup_sub:
2109 unregister_pernet_subsys(&ipvs_core_ops);
552ad65a 2110cleanup_conn:
1da177e4 2111 ip_vs_conn_cleanup();
552ad65a 2112cleanup_protocol:
1da177e4
LT
2113 ip_vs_protocol_cleanup();
2114 ip_vs_control_cleanup();
6c8f7949 2115exit:
1da177e4
LT
2116 return ret;
2117}
2118
2119static void __exit ip_vs_cleanup(void)
2120{
8537de8a 2121 ip_vs_unregister_nl_ioctl();
41c5b317 2122 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
7a4f0761
HS
2123 unregister_pernet_device(&ipvs_core_dev_ops);
2124 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
1da177e4 2125 ip_vs_conn_cleanup();
1da177e4
LT
2126 ip_vs_protocol_cleanup();
2127 ip_vs_control_cleanup();
1e3e238e 2128 pr_info("ipvs unloaded.\n");
1da177e4
LT
2129}
2130
2131module_init(ip_vs_init);
2132module_exit(ip_vs_cleanup);
2133MODULE_LICENSE("GPL");
This page took 0.802687 seconds and 5 git commands to generate.