blob: 299c7f33e18b3daa14696f286fa42aa8faf82af0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Harald Welte6869c4d2005-08-09 19:24:19 -070023 * Harald Welte don't use nfcache
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 */
26
Hannes Eder9aada7a2009-07-30 14:29:44 -070027#define KMSG_COMPONENT "IPVS"
28#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/ip.h>
33#include <linux/tcp.h>
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010034#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/icmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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>
Stephen Rothwell2c70b512010-08-29 17:04:53 +000043#include <net/ip6_checksum.h>
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010044#include <net/netns/generic.h> /* net_generic() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/netfilter.h>
47#include <linux/netfilter_ipv4.h>
48
Julius Volz2a3b7912008-09-02 15:55:47 +020049#ifdef CONFIG_IP_VS_IPV6
50#include <net/ipv6.h>
51#include <linux/netfilter_ipv6.h>
Julian Anastasov489fded2010-10-17 16:27:31 +030052#include <net/ip6_route.h>
Julius Volz2a3b7912008-09-02 15:55:47 +020053#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/ip_vs.h>
56
57
58EXPORT_SYMBOL(register_ip_vs_scheduler);
59EXPORT_SYMBOL(unregister_ip_vs_scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060EXPORT_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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010072int 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* ID used in ICMP lookups */
80#define icmp_id(icmph) (((icmph)->un).echo.id)
Julius Volz2a3b7912008-09-02 15:55:47 +020081#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83const char *ip_vs_proto_name(unsigned proto)
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";
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010094 case IPPROTO_SCTP:
95 return "SCTP";
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 case IPPROTO_ICMP:
97 return "ICMP";
Julius Volz2a3b7912008-09-02 15:55:47 +020098#ifdef CONFIG_IP_VS_IPV6
99 case IPPROTO_ICMPV6:
100 return "ICMPv6";
101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100118 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100121 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Hans Schillstromb17fc992011-01-03 14:44:56 +0100123 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Hans Schillstromb17fc992011-01-03 14:44:56 +0100129 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
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200135 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100136 s->ustats.inpkts++;
137 u64_stats_update_begin(&s->syncp);
138 s->ustats.inbytes += skb->len;
139 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
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;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100148 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100151 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Hans Schillstromb17fc992011-01-03 14:44:56 +0100153 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Hans Schillstromb17fc992011-01-03 14:44:56 +0100159 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
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200165 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100166 s->ustats.outpkts++;
167 u64_stats_update_begin(&s->syncp);
168 s->ustats.outbytes += skb->len;
169 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171}
172
173
174static inline void
175ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
176{
Hans Schillstromb17fc992011-01-03 14:44:56 +0100177 struct netns_ipvs *ipvs = net_ipvs(svc->net);
178 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Hans Schillstromb17fc992011-01-03 14:44:56 +0100180 s = this_cpu_ptr(cp->dest->stats.cpustats);
181 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Hans Schillstromb17fc992011-01-03 14:44:56 +0100183 s = this_cpu_ptr(svc->stats.cpustats);
184 s->ustats.conns++;
185
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200186 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100187 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190
191static inline int
192ip_vs_set_state(struct ip_vs_conn *cp, int direction,
193 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100194 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Hans Schillstrom93304192011-01-03 14:44:51 +0100196 if (unlikely(!pd->pp->state_transition))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
Hans Schillstrom93304192011-01-03 14:44:51 +0100198 return pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Hans Schillstroma5959d52010-11-19 14:25:10 +0100201static inline int
Simon Horman85999282010-08-22 21:37:53 +0900202ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
203 struct sk_buff *skb, int protocol,
204 const union nf_inet_addr *caddr, __be16 cport,
205 const union nf_inet_addr *vaddr, __be16 vport,
206 struct ip_vs_conn_param *p)
207{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100208 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
209 vport, p);
Simon Horman85999282010-08-22 21:37:53 +0900210 p->pe = svc->pe;
211 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100212 return p->pe->fill_param(p, skb);
213
214 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * IPVS persistent scheduling function
219 * It creates a connection entry according to its template if exists,
220 * or selects a server and creates a connection entry plus a template.
221 * Locking: we are svc user (svc->refcnt), so we hold all dests too
222 * Protocols supported: TCP, UDP
223 */
224static struct ip_vs_conn *
225ip_vs_sched_persist(struct ip_vs_service *svc,
Simon Horman85999282010-08-22 21:37:53 +0900226 struct sk_buff *skb,
Hans Schillstroma5959d52010-11-19 14:25:10 +0100227 __be16 src_port, __be16 dst_port, int *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct ip_vs_conn *cp = NULL;
Julius Volz28364a52008-09-02 15:55:43 +0200230 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct ip_vs_dest *dest;
232 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900233 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200234 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900235 struct ip_vs_conn_param param;
Julius Volz28364a52008-09-02 15:55:43 +0200236 union nf_inet_addr snet; /* source network of the client,
237 after masking */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200238
239 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200242#ifdef CONFIG_IP_VS_IPV6
243 if (svc->af == AF_INET6)
244 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
245 else
246#endif
247 snet.ip = iph.saddr.ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Julius Volzcd17f9e2008-09-02 15:55:46 +0200249 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
250 "mnet %s\n",
Hans Schillstromce144f22010-11-19 14:25:08 +0100251 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(src_port),
252 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200253 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /*
256 * As far as we know, FTP is a very complicated network protocol, and
257 * it uses control connection and data connections. For active FTP,
258 * FTP server initialize data connection to the client, its source port
259 * is often 20. For passive FTP, FTP server tells the clients the port
260 * that it passively listens to, and the client issues the data
261 * connection. In the tunneling or direct routing mode, the load
262 * balancer is on the client-to-server half of connection, the port
263 * number is unknown to the load balancer. So, a conn template like
264 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
265 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
266 * is created for other persistent services.
267 */
Simon Horman5b57a982010-08-22 21:37:51 +0900268 {
Simon Hormanf11017e2010-08-22 21:37:52 +0900269 int protocol = iph.protocol;
270 const union nf_inet_addr *vaddr = &iph.daddr;
271 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
272 __be16 vport = 0;
273
Hans Schillstromce144f22010-11-19 14:25:08 +0100274 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900275 /* non-FTP template:
276 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
277 * FTP template:
278 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100281 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900283 /* Note: persistent fwmark-based services and
284 * persistent port zero service are handled here.
285 * fwmark template:
286 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
287 * port zero template:
288 * <protocol,caddr,0,vaddr,0,daddr,0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Julius Volz28364a52008-09-02 15:55:43 +0200290 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900291 protocol = IPPROTO_IP;
292 vaddr = &fwmark;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100295 /* return *ignored = -1 so NF_DROP can be used */
296 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
297 vaddr, vport, &param) < 0) {
298 *ignored = -1;
299 return NULL;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Simon Horman5b57a982010-08-22 21:37:51 +0900303 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900304 ct = ip_vs_ct_in_get(&param);
Simon Horman5b57a982010-08-22 21:37:51 +0900305 if (!ct || !ip_vs_check_template(ct)) {
Hans Schillstroma5959d52010-11-19 14:25:10 +0100306 /*
307 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900308 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100309 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900310 */
311 dest = svc->scheduler->schedule(svc, skb);
312 if (!dest) {
313 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900314 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100315 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900316 return NULL;
317 }
318
Hans Schillstromce144f22010-11-19 14:25:08 +0100319 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900320 dport = dest->port;
321
Simon Horman85999282010-08-22 21:37:53 +0900322 /* Create a template
323 * This adds param.pe_data to the template,
324 * and thus param.pe_data will be destroyed
325 * when the template expires */
Simon Hormanf11017e2010-08-22 21:37:52 +0900326 ct = ip_vs_conn_new(&param, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100327 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900328 if (ct == NULL) {
329 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100330 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900331 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900332 }
Simon Horman5b57a982010-08-22 21:37:51 +0900333
334 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900335 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900336 /* set destination with the found template */
337 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900338 kfree(param.pe_data);
339 }
Simon Horman5b57a982010-08-22 21:37:51 +0900340
Hans Schillstromce144f22010-11-19 14:25:08 +0100341 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900342 if (dport == svc->port && dest->port)
343 dport = dest->port;
344
Nick Chalk26ec0372010-06-22 08:07:01 +0200345 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
346 && iph.protocol == IPPROTO_UDP)?
347 IP_VS_CONN_F_ONE_PACKET : 0;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * Create a new connection according to the template
351 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100352 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr,
353 src_port, &iph.daddr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100354
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100355 cp = ip_vs_conn_new(&param, &dest->addr, dport, flags, dest, skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (cp == NULL) {
357 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100358 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return NULL;
360 }
361
362 /*
363 * Add its control
364 */
365 ip_vs_control_add(cp, ct);
366 ip_vs_conn_put(ct);
367
368 ip_vs_conn_stats(cp, svc);
369 return cp;
370}
371
372
373/*
374 * IPVS main scheduling function
375 * It selects a server according to the virtual service, and
376 * creates a connection entry.
377 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100378 *
379 * Usage of *ignored
380 *
381 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
382 * svc/scheduler decides that this packet should be accepted with
383 * NF_ACCEPT because it must not be scheduled.
384 *
385 * 0 : scheduler can not find destination, so try bypass or
386 * return ICMP and then NF_DROP (ip_vs_leave).
387 *
388 * -1 : scheduler tried to schedule but fatal error occurred, eg.
389 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
390 * failure such as missing Call-ID, ENOMEM on skb_linearize
391 * or pe_data. In this case we should return NF_DROP without
392 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
394struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300395ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100396 struct ip_vs_proto_data *pd, int *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Hans Schillstrom93304192011-01-03 14:44:51 +0100398 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct ip_vs_conn *cp = NULL;
Julius Volz28364a52008-09-02 15:55:43 +0200400 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct ip_vs_dest *dest;
Julian Anastasov35757922010-09-17 14:18:16 +0200402 __be16 _ports[2], *pptr;
403 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Julian Anastasov190ecd22010-10-17 16:24:37 +0300405 *ignored = 1;
Julius Volz28364a52008-09-02 15:55:43 +0200406 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
407 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (pptr == NULL)
409 return NULL;
410
411 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300412 * 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) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300418 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
419 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300420 return NULL;
421 }
422
423 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100424 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300425 */
426 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
Hans Schillstrom93304192011-01-03 14:44:51 +0100427 (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300428 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
Julian Anastasov190ecd22010-10-17 16:24:37 +0300429 "Not scheduling reply for existing connection");
430 __ip_vs_conn_put(cp);
431 return NULL;
432 }
433
434 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * Persistent service
436 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100437 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
438 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored);
439
440 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /*
443 * Non-persistent service
444 */
445 if (!svc->fwmark && pptr[1] != svc->port) {
446 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000447 pr_err("Schedule: port zero only supported "
448 "in persistent services, "
449 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return NULL;
451 }
452
453 dest = svc->scheduler->schedule(svc, skb);
454 if (dest == NULL) {
455 IP_VS_DBG(1, "Schedule: no dest found.\n");
456 return NULL;
457 }
458
Nick Chalk26ec0372010-06-22 08:07:01 +0200459 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
460 && iph.protocol == IPPROTO_UDP)?
461 IP_VS_CONN_F_ONE_PACKET : 0;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Create a connection entry.
465 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900466 {
467 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100468
469 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
470 &iph.saddr, pptr[0], &iph.daddr, pptr[1],
471 &p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900472 cp = ip_vs_conn_new(&p, &dest->addr,
473 dest->port ? dest->port : pptr[1],
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100474 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100475 if (!cp) {
476 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900477 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100478 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Julius Volzcd17f9e2008-09-02 15:55:46 +0200481 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
482 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
483 ip_vs_fwd_tag(cp),
484 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
485 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
486 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
487 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 ip_vs_conn_stats(cp, svc);
490 return cp;
491}
492
493
494/*
495 * Pass or drop the packet.
496 * Called by ip_vs_in, when the virtual service is available but
497 * no destination is available for a new connection.
498 */
499int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100500 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Hans Schillstrom4a984802011-01-03 14:45:02 +0100502 struct net *net;
Hans Schillstroma0840e22011-01-03 14:44:58 +0100503 struct netns_ipvs *ipvs;
Al Viro014d7302006-09-28 14:29:52 -0700504 __be16 _ports[2], *pptr;
Julius Volz28364a52008-09-02 15:55:43 +0200505 struct ip_vs_iphdr iph;
Julius Volz2a3b7912008-09-02 15:55:47 +0200506 int unicast;
Hans Schillstrom93304192011-01-03 14:44:51 +0100507
Julius Volz2a3b7912008-09-02 15:55:47 +0200508 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Julius Volz28364a52008-09-02 15:55:43 +0200510 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (pptr == NULL) {
512 ip_vs_service_put(svc);
513 return NF_DROP;
514 }
Hans Schillstrom4a984802011-01-03 14:45:02 +0100515 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Julius Volz2a3b7912008-09-02 15:55:47 +0200517#ifdef CONFIG_IP_VS_IPV6
518 if (svc->af == AF_INET6)
519 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
520 else
521#endif
Hans Schillstrom4a984802011-01-03 14:45:02 +0100522 unicast = (inet_addr_type(net, iph.daddr.ip) == RTN_UNICAST);
Julius Volz2a3b7912008-09-02 15:55:47 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200525 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 a cache_bypass connection entry */
Hans Schillstrom4a984802011-01-03 14:45:02 +0100527 ipvs = net_ipvs(net);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100528 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int ret, cs;
530 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200531 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
532 iph.protocol == IPPROTO_UDP)?
533 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000534 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 ip_vs_service_put(svc);
537
538 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000539 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900540 {
541 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100542 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
Simon Hormanf11017e2010-08-22 21:37:52 +0900543 &iph.saddr, pptr[0],
544 &iph.daddr, pptr[1], &p);
545 cp = ip_vs_conn_new(&p, &daddr, 0,
546 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100547 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900548 if (!cp)
549 return NF_DROP;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* statistics */
553 ip_vs_in_stats(cp, skb);
554
555 /* set state */
Hans Schillstrom93304192011-01-03 14:44:51 +0100556 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* transmit the first SYN packet */
Hans Schillstrom93304192011-01-03 14:44:51 +0100559 ret = cp->packet_xmit(skb, cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* do not touch skb anymore */
561
562 atomic_inc(&cp->in_pkts);
563 ip_vs_conn_put(cp);
564 return ret;
565 }
566
567 /*
568 * When the virtual ftp service is presented, packets destined
569 * for other services on the VIP may get here (except services
570 * listed in the ipvs table), pass the packets, because it is
571 * not ipvs job to decide to drop the packets.
572 */
573 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
574 ip_vs_service_put(svc);
575 return NF_ACCEPT;
576 }
577
578 ip_vs_service_put(svc);
579
580 /*
581 * Notify the client that the destination is unreachable, and
582 * release the socket buffer.
583 * Since it is in IP layer, the TCP socket is not actually
584 * created, the TCP RST packet cannot be sent, instead that
585 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
586 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200587#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300588 if (svc->af == AF_INET6) {
589 if (!skb->dev) {
590 struct net *net = dev_net(skb_dst(skb)->dev);
591
592 skb->dev = net->loopback_dev;
593 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000594 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300595 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200596#endif
597 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return NF_DROP;
600}
601
Al Virob1550f22006-11-14 21:37:50 -0800602__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Al Virod3bc23e2006-11-14 21:24:49 -0800604 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300607static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
608{
609 if (NF_INET_LOCAL_IN == hooknum)
610 return IP_DEFRAG_VS_IN;
611 if (NF_INET_FORWARD == hooknum)
612 return IP_DEFRAG_VS_FWD;
613 return IP_DEFRAG_VS_OUT;
614}
615
Herbert Xu776c7292007-10-14 00:38:32 -0700616static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Herbert Xu776c7292007-10-14 00:38:32 -0700618 int err = ip_defrag(skb, user);
619
620 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700621 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700622
623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Julius Volz2a3b7912008-09-02 15:55:47 +0200626#ifdef CONFIG_IP_VS_IPV6
627static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
628{
629 /* TODO IPv6: Find out what to do here for IPv6 */
630 return 0;
631}
632#endif
633
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900634static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
635{
636 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
637
638#ifdef CONFIG_IP_VS_IPV6
639 if (af == AF_INET6) {
640 if (ipvs->sysctl_snat_reroute && ip6_route_me_harder(skb) != 0)
641 return 1;
642 } else
643#endif
644 if ((ipvs->sysctl_snat_reroute ||
645 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
646 ip_route_me_harder(skb, RTN_LOCAL) != 0)
647 return 1;
648
649 return 0;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/*
653 * Packet has been made sufficiently writable in caller
654 * - inout: 1=in->out, 0=out->in
655 */
656void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
657 struct ip_vs_conn *cp, int inout)
658{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700659 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700661 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
662 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
664
665 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200666 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200668 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ip_send_check(ciph);
670 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200671 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200673 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ip_send_check(ciph);
675 }
676
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100677 /* the TCP/UDP/SCTP port */
678 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
679 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700680 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (inout)
683 ports[1] = cp->vport;
684 else
685 ports[0] = cp->dport;
686 }
687
688 /* And finally the ICMP checksum */
689 icmph->checksum = 0;
690 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
691 skb->ip_summed = CHECKSUM_UNNECESSARY;
692
693 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300694 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 "Forwarding altered outgoing ICMP");
696 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300697 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 "Forwarding altered incoming ICMP");
699}
700
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200701#ifdef CONFIG_IP_VS_IPV6
702void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
703 struct ip_vs_conn *cp, int inout)
704{
705 struct ipv6hdr *iph = ipv6_hdr(skb);
706 unsigned int icmp_offset = sizeof(struct ipv6hdr);
707 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
708 icmp_offset);
709 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
710
711 if (inout) {
712 iph->saddr = cp->vaddr.in6;
713 ciph->daddr = cp->vaddr.in6;
714 } else {
715 iph->daddr = cp->daddr.in6;
716 ciph->saddr = cp->daddr.in6;
717 }
718
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100719 /* the TCP/UDP/SCTP port */
720 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
721 IPPROTO_SCTP == ciph->nexthdr) {
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200722 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
723
724 if (inout)
725 ports[1] = cp->vport;
726 else
727 ports[0] = cp->dport;
728 }
729
730 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700731 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
732 skb->len - icmp_offset,
733 IPPROTO_ICMPV6, 0);
734 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
735 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
736 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200737
738 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300739 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
740 (void *)ciph - (void *)iph,
741 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200742 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300743 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
744 (void *)ciph - (void *)iph,
745 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200746}
747#endif
748
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000749/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200750 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000751 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000752static int handle_response_icmp(int af, struct sk_buff *skb,
753 union nf_inet_addr *snet,
754 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000755 struct ip_vs_protocol *pp,
756 unsigned int offset, unsigned int ihl)
757{
758 unsigned int verdict = NF_DROP;
759
760 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000761 pr_err("shouldn't reach here, because the box is on the "
762 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000763 }
764
765 /* Ensure the checksum is correct */
766 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
767 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000768 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
769 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000770 goto out;
771 }
772
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100773 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
774 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000775 offset += 2 * sizeof(__u16);
776 if (!skb_make_writable(skb, offset))
777 goto out;
778
Simon Hormanf2428ed2008-09-05 11:17:14 +1000779#ifdef CONFIG_IP_VS_IPV6
780 if (af == AF_INET6)
781 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
782 else
783#endif
784 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000785
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900786 if (ip_vs_route_me_harder(af, skb))
787 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300788
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000789 /* do the statistics and put it back */
790 ip_vs_out_stats(cp, skb);
791
Julian Anastasovcf356d62010-10-17 16:21:07 +0300792 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200793 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300794 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200795 else
796 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000797 verdict = NF_ACCEPT;
798
799out:
800 __ip_vs_conn_put(cp);
801
802 return verdict;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000807 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300810static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
811 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct iphdr *iph;
814 struct icmphdr _icmph, *ic;
815 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200816 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct ip_vs_conn *cp;
818 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000819 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000820 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 *related = 1;
823
824 /* reassemble IP fragments */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700825 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300826 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700830 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 offset = ihl = iph->ihl * 4;
832 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
833 if (ic == NULL)
834 return NF_DROP;
835
Harvey Harrison14d5e832008-10-31 00:54:29 -0700836 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700838 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /*
841 * Work through seeing if this is for us.
842 * These checks are supposed to be in an order that means easy
843 * things are checked first to speed up processing.... however
844 * this means that some packets will manage to get a long way
845 * down this stack and then be rejected, but that's life.
846 */
847 if ((ic->type != ICMP_DEST_UNREACH) &&
848 (ic->type != ICMP_SOURCE_QUENCH) &&
849 (ic->type != ICMP_TIME_EXCEEDED)) {
850 *related = 0;
851 return NF_ACCEPT;
852 }
853
854 /* Now find the contained IP header */
855 offset += sizeof(_icmph);
856 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
857 if (cih == NULL)
858 return NF_ACCEPT; /* The packet looks wrong, ignore */
859
860 pp = ip_vs_proto_get(cih->protocol);
861 if (!pp)
862 return NF_ACCEPT;
863
864 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900865 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 pp->dont_defrag))
867 return NF_ACCEPT;
868
Julian Anastasov0d796412010-10-17 16:46:17 +0300869 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
870 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 offset += cih->ihl * 4;
873
Julius Volz51ef3482008-09-02 15:55:40 +0200874 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +0100876 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!cp)
878 return NF_ACCEPT;
879
Simon Hormanf2428ed2008-09-05 11:17:14 +1000880 snet.ip = iph->saddr;
881 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
882 pp, offset, ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Julius Volz2a3b7912008-09-02 15:55:47 +0200885#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300886static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
887 unsigned int hooknum)
Julius Volz2a3b7912008-09-02 15:55:47 +0200888{
889 struct ipv6hdr *iph;
890 struct icmp6hdr _icmph, *ic;
891 struct ipv6hdr _ciph, *cih; /* The ip header contained
892 within the ICMP */
893 struct ip_vs_iphdr ciph;
894 struct ip_vs_conn *cp;
895 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000896 unsigned int offset;
897 union nf_inet_addr snet;
Julius Volz2a3b7912008-09-02 15:55:47 +0200898
899 *related = 1;
900
901 /* reassemble IP fragments */
902 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300903 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +0200904 return NF_STOLEN;
905 }
906
907 iph = ipv6_hdr(skb);
908 offset = sizeof(struct ipv6hdr);
909 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
910 if (ic == NULL)
911 return NF_DROP;
912
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700913 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
Julius Volz2a3b7912008-09-02 15:55:47 +0200914 ic->icmp6_type, ntohs(icmpv6_id(ic)),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700915 &iph->saddr, &iph->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200916
917 /*
918 * Work through seeing if this is for us.
919 * These checks are supposed to be in an order that means easy
920 * things are checked first to speed up processing.... however
921 * this means that some packets will manage to get a long way
922 * down this stack and then be rejected, but that's life.
923 */
924 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
925 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
926 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
927 *related = 0;
928 return NF_ACCEPT;
929 }
930
931 /* Now find the contained IP header */
932 offset += sizeof(_icmph);
933 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
934 if (cih == NULL)
935 return NF_ACCEPT; /* The packet looks wrong, ignore */
936
937 pp = ip_vs_proto_get(cih->nexthdr);
938 if (!pp)
939 return NF_ACCEPT;
940
941 /* Is the embedded protocol header present? */
942 /* TODO: we don't support fragmentation at the moment anyways */
943 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
944 return NF_ACCEPT;
945
Julian Anastasov0d796412010-10-17 16:46:17 +0300946 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
947 "Checking outgoing ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +0200948
949 offset += sizeof(struct ipv6hdr);
950
951 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
952 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +0100953 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
Julius Volz2a3b7912008-09-02 15:55:47 +0200954 if (!cp)
955 return NF_ACCEPT;
956
Simon Horman178f5e42008-09-08 09:34:46 +1000957 ipv6_addr_copy(&snet.in6, &iph->saddr);
Simon Hormanf2428ed2008-09-05 11:17:14 +1000958 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
959 pp, offset, sizeof(struct ipv6hdr));
Julius Volz2a3b7912008-09-02 15:55:47 +0200960}
961#endif
962
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100963/*
964 * Check if sctp chunc is ABORT chunk
965 */
966static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
967{
968 sctp_chunkhdr_t *sch, schunk;
969 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
970 sizeof(schunk), &schunk);
971 if (sch == NULL)
972 return 0;
973 if (sch->type == SCTP_CID_ABORT)
974 return 1;
975 return 0;
976}
977
Julius Volz2a3b7912008-09-02 15:55:47 +0200978static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct tcphdr _tcph, *th;
981
Julius Volz2a3b7912008-09-02 15:55:47 +0200982 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (th == NULL)
984 return 0;
985 return th->rst;
986}
987
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000988/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000989 */
990static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +0100991handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000992 struct ip_vs_conn *cp, int ihl)
993{
Hans Schillstrom93304192011-01-03 14:44:51 +0100994 struct ip_vs_protocol *pp = pd->pp;
995
Julian Anastasov0d796412010-10-17 16:46:17 +0300996 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000997
998 if (!skb_make_writable(skb, ihl))
999 goto drop;
1000
1001 /* mangle the packet */
1002 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
1003 goto drop;
1004
1005#ifdef CONFIG_IP_VS_IPV6
1006 if (af == AF_INET6)
1007 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1008 else
1009#endif
1010 {
1011 ip_hdr(skb)->saddr = cp->vaddr.ip;
1012 ip_send_check(ip_hdr(skb));
1013 }
1014
Julian Anastasov8a803042010-09-21 17:38:57 +02001015 /*
1016 * nf_iterate does not expect change in the skb->dst->dev.
1017 * It looks like it is not fatal to enable this code for hooks
1018 * where our handlers are at the end of the chain list and
1019 * when all next handlers use skb->dst->dev and not outdev.
1020 * It will definitely route properly the inout NAT traffic
1021 * when multiple paths are used.
1022 */
1023
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001024 /* For policy routing, packets originating from this
1025 * machine itself may be routed differently to packets
1026 * passing through. We want this packet to be routed as
1027 * if it came from this machine itself. So re-compute
1028 * the routing information.
1029 */
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001030 if (ip_vs_route_me_harder(af, skb))
1031 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001032
Julian Anastasov0d796412010-10-17 16:46:17 +03001033 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001034
1035 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001036 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001037 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001038 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001039 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001040 else
1041 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001042 ip_vs_conn_put(cp);
1043
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001044 LeaveFunction(11);
1045 return NF_ACCEPT;
1046
1047drop:
1048 ip_vs_conn_put(cp);
1049 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001050 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001051 return NF_STOLEN;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001055 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
1057static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001058ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Hans Schillstromfc723252011-01-03 14:44:43 +01001060 struct net *net = NULL;
Julius Volz51ef3482008-09-02 15:55:40 +02001061 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001063 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct ip_vs_conn *cp;
Hans Schillstroma0840e22011-01-03 14:44:58 +01001065 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 EnterFunction(11);
1068
Julian Anastasovfc604762010-10-17 16:38:15 +03001069 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001070 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return NF_ACCEPT;
1072
Julian Anastasovfc604762010-10-17 16:38:15 +03001073 /* Bad... Do not break raw sockets */
1074 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1075 af == AF_INET)) {
1076 struct sock *sk = skb->sk;
1077 struct inet_sock *inet = inet_sk(skb->sk);
1078
1079 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1080 return NF_ACCEPT;
1081 }
1082
1083 if (unlikely(!skb_dst(skb)))
1084 return NF_ACCEPT;
1085
Hans Schillstromfc723252011-01-03 14:44:43 +01001086 net = skb_net(skb);
Julius Volz2a3b7912008-09-02 15:55:47 +02001087 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1088#ifdef CONFIG_IP_VS_IPV6
1089 if (af == AF_INET6) {
1090 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001091 int related;
1092 int verdict = ip_vs_out_icmp_v6(skb, &related,
1093 hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Julian Anastasovf5a41842010-10-17 16:35:46 +03001095 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001096 return verdict;
1097 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1098 }
1099 } else
1100#endif
1101 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001102 int related;
1103 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001104
Julian Anastasovf5a41842010-10-17 16:35:46 +03001105 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001106 return verdict;
1107 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Hans Schillstrom93304192011-01-03 14:44:51 +01001110 pd = ip_vs_proto_data_get(net, iph.protocol);
1111 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001113 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001116#ifdef CONFIG_IP_VS_IPV6
1117 if (af == AF_INET6) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001118 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1119 if (ip_vs_gather_frags_v6(skb,
1120 ip_vs_defrag_user(hooknum)))
1121 return NF_STOLEN;
Julius Volz2a3b7912008-09-02 15:55:47 +02001122 }
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001123
1124 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001125 } else
1126#endif
1127 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1128 !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001129 if (ip_vs_gather_frags(skb,
1130 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001131 return NF_STOLEN;
1132
1133 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /*
1137 * Check if the packet belongs to an existing entry
1138 */
Hans Schillstrom93304192011-01-03 14:44:51 +01001139 cp = pp->conn_out_get(af, skb, &iph, iph.len, 0);
Hans Schillstroma0840e22011-01-03 14:44:58 +01001140 ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Julian Anastasovcb591552010-10-17 16:40:51 +03001142 if (likely(cp))
Hans Schillstrom93304192011-01-03 14:44:51 +01001143 return handle_response(af, skb, pd, cp, iph.len);
Hans Schillstroma0840e22011-01-03 14:44:58 +01001144 if (ipvs->sysctl_nat_icmp_send &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001145 (pp->protocol == IPPROTO_TCP ||
1146 pp->protocol == IPPROTO_UDP ||
1147 pp->protocol == IPPROTO_SCTP)) {
1148 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Julian Anastasovcb591552010-10-17 16:40:51 +03001150 pptr = skb_header_pointer(skb, iph.len,
1151 sizeof(_ports), _ports);
1152 if (pptr == NULL)
1153 return NF_ACCEPT; /* Not for me */
Hans Schillstromfc723252011-01-03 14:44:43 +01001154 if (ip_vs_lookup_real_service(net, af, iph.protocol,
Julian Anastasovcb591552010-10-17 16:40:51 +03001155 &iph.saddr,
1156 pptr[0])) {
1157 /*
1158 * Notify the real server: there is no
1159 * existing entry if it is not RST
1160 * packet or not TCP packet.
1161 */
1162 if ((iph.protocol != IPPROTO_TCP &&
1163 iph.protocol != IPPROTO_SCTP)
1164 || ((iph.protocol == IPPROTO_TCP
1165 && !is_tcp_reset(skb, iph.len))
1166 || (iph.protocol == IPPROTO_SCTP
1167 && !is_sctp_abort(skb,
1168 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001169#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001170 if (af == AF_INET6) {
1171 struct net *net =
1172 dev_net(skb_dst(skb)->dev);
1173
1174 if (!skb->dev)
1175 skb->dev = net->loopback_dev;
1176 icmpv6_send(skb,
1177 ICMPV6_DEST_UNREACH,
1178 ICMPV6_PORT_UNREACH,
1179 0);
1180 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001181#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001182 icmp_send(skb,
1183 ICMP_DEST_UNREACH,
1184 ICMP_PORT_UNREACH, 0);
1185 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
Julian Anastasov0d796412010-10-17 16:46:17 +03001189 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001190 "ip_vs_out: packet continues traversal as normal");
1191 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Julian Anastasovfc604762010-10-17 16:38:15 +03001194/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001195 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1196 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001197 * Check if packet is reply for established ip_vs_conn.
1198 */
1199static unsigned int
1200ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1201 const struct net_device *in, const struct net_device *out,
1202 int (*okfn)(struct sk_buff *))
1203{
1204 return ip_vs_out(hooknum, skb, AF_INET);
1205}
1206
1207/*
1208 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1209 * Check if packet is reply for established ip_vs_conn.
1210 */
1211static unsigned int
1212ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1213 const struct net_device *in, const struct net_device *out,
1214 int (*okfn)(struct sk_buff *))
1215{
1216 unsigned int verdict;
1217
1218 /* Disable BH in LOCAL_OUT until all places are fixed */
1219 local_bh_disable();
1220 verdict = ip_vs_out(hooknum, skb, AF_INET);
1221 local_bh_enable();
1222 return verdict;
1223}
1224
1225#ifdef CONFIG_IP_VS_IPV6
1226
1227/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001228 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1229 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001230 * Check if packet is reply for established ip_vs_conn.
1231 */
1232static unsigned int
1233ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1234 const struct net_device *in, const struct net_device *out,
1235 int (*okfn)(struct sk_buff *))
1236{
1237 return ip_vs_out(hooknum, skb, AF_INET6);
1238}
1239
1240/*
1241 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1242 * Check if packet is reply for established ip_vs_conn.
1243 */
1244static unsigned int
1245ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1246 const struct net_device *in, const struct net_device *out,
1247 int (*okfn)(struct sk_buff *))
1248{
1249 unsigned int verdict;
1250
1251 /* Disable BH in LOCAL_OUT until all places are fixed */
1252 local_bh_disable();
1253 verdict = ip_vs_out(hooknum, skb, AF_INET6);
1254 local_bh_enable();
1255 return verdict;
1256}
1257
1258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260/*
1261 * Handle ICMP messages in the outside-to-inside direction (incoming).
1262 * Find any that might be relevant, check against existing connections,
1263 * forward to the right destination host if relevant.
1264 * Currently handles error types - unreachable, quench, ttl exceeded.
1265 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001266static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001267ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Hans Schillstrom93304192011-01-03 14:44:51 +01001269 struct net *net = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct iphdr *iph;
1271 struct icmphdr _icmph, *ic;
1272 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001273 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct ip_vs_conn *cp;
1275 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001276 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 unsigned int offset, ihl, verdict;
1278
1279 *related = 1;
1280
1281 /* reassemble IP fragments */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001282 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001283 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001287 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 offset = ihl = iph->ihl * 4;
1289 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1290 if (ic == NULL)
1291 return NF_DROP;
1292
Harvey Harrison14d5e832008-10-31 00:54:29 -07001293 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001295 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * Work through seeing if this is for us.
1299 * These checks are supposed to be in an order that means easy
1300 * things are checked first to speed up processing.... however
1301 * this means that some packets will manage to get a long way
1302 * down this stack and then be rejected, but that's life.
1303 */
1304 if ((ic->type != ICMP_DEST_UNREACH) &&
1305 (ic->type != ICMP_SOURCE_QUENCH) &&
1306 (ic->type != ICMP_TIME_EXCEEDED)) {
1307 *related = 0;
1308 return NF_ACCEPT;
1309 }
1310
1311 /* Now find the contained IP header */
1312 offset += sizeof(_icmph);
1313 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1314 if (cih == NULL)
1315 return NF_ACCEPT; /* The packet looks wrong, ignore */
1316
Hans Schillstrom93304192011-01-03 14:44:51 +01001317 net = skb_net(skb);
1318 pd = ip_vs_proto_data_get(net, cih->protocol);
1319 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001321 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001324 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 pp->dont_defrag))
1326 return NF_ACCEPT;
1327
Julian Anastasov0d796412010-10-17 16:46:17 +03001328 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1329 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 offset += cih->ihl * 4;
1332
Julius Volz51ef3482008-09-02 15:55:40 +02001333 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +01001335 cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1);
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001336 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return NF_ACCEPT;
1338
1339 verdict = NF_DROP;
1340
1341 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001342 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001344 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1345 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 goto out;
1347 }
1348
1349 /* do the statistics and put it back */
1350 ip_vs_in_stats(cp, skb);
1351 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1352 offset += 2 * sizeof(__u16);
1353 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
Julian Anastasov489fded2010-10-17 16:27:31 +03001354 /* LOCALNODE from FORWARD hook is not supported */
1355 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1356 skb_rtable(skb)->rt_flags & RTCF_LOCAL) {
1357 IP_VS_DBG(1, "%s(): "
1358 "local delivery to %pI4 but in FORWARD\n",
1359 __func__, &skb_rtable(skb)->rt_dst);
1360 verdict = NF_DROP;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 out:
1364 __ip_vs_conn_put(cp);
1365
1366 return verdict;
1367}
1368
Julius Volz2a3b7912008-09-02 15:55:47 +02001369#ifdef CONFIG_IP_VS_IPV6
1370static int
1371ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1372{
Hans Schillstrom93304192011-01-03 14:44:51 +01001373 struct net *net = NULL;
Julius Volz2a3b7912008-09-02 15:55:47 +02001374 struct ipv6hdr *iph;
1375 struct icmp6hdr _icmph, *ic;
1376 struct ipv6hdr _ciph, *cih; /* The ip header contained
1377 within the ICMP */
1378 struct ip_vs_iphdr ciph;
1379 struct ip_vs_conn *cp;
1380 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001381 struct ip_vs_proto_data *pd;
Julius Volz2a3b7912008-09-02 15:55:47 +02001382 unsigned int offset, verdict;
Julian Anastasov489fded2010-10-17 16:27:31 +03001383 struct rt6_info *rt;
Julius Volz2a3b7912008-09-02 15:55:47 +02001384
1385 *related = 1;
1386
1387 /* reassemble IP fragments */
1388 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001389 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001390 return NF_STOLEN;
1391 }
1392
1393 iph = ipv6_hdr(skb);
1394 offset = sizeof(struct ipv6hdr);
1395 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1396 if (ic == NULL)
1397 return NF_DROP;
1398
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001399 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
Julius Volz2a3b7912008-09-02 15:55:47 +02001400 ic->icmp6_type, ntohs(icmpv6_id(ic)),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001401 &iph->saddr, &iph->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001402
1403 /*
1404 * Work through seeing if this is for us.
1405 * These checks are supposed to be in an order that means easy
1406 * things are checked first to speed up processing.... however
1407 * this means that some packets will manage to get a long way
1408 * down this stack and then be rejected, but that's life.
1409 */
1410 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1411 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1412 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1413 *related = 0;
1414 return NF_ACCEPT;
1415 }
1416
1417 /* Now find the contained IP header */
1418 offset += sizeof(_icmph);
1419 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1420 if (cih == NULL)
1421 return NF_ACCEPT; /* The packet looks wrong, ignore */
1422
Hans Schillstrom93304192011-01-03 14:44:51 +01001423 net = skb_net(skb);
1424 pd = ip_vs_proto_data_get(net, cih->nexthdr);
1425 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001426 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001427 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001428
1429 /* Is the embedded protocol header present? */
1430 /* TODO: we don't support fragmentation at the moment anyways */
1431 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1432 return NF_ACCEPT;
1433
Julian Anastasov0d796412010-10-17 16:46:17 +03001434 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1435 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001436
1437 offset += sizeof(struct ipv6hdr);
1438
1439 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1440 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +01001441 cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1);
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001442 if (!cp)
Julius Volz2a3b7912008-09-02 15:55:47 +02001443 return NF_ACCEPT;
1444
1445 verdict = NF_DROP;
1446
1447 /* do the statistics and put it back */
1448 ip_vs_in_stats(cp, skb);
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001449 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1450 IPPROTO_SCTP == cih->nexthdr)
Julius Volz2a3b7912008-09-02 15:55:47 +02001451 offset += 2 * sizeof(__u16);
1452 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
Julian Anastasov489fded2010-10-17 16:27:31 +03001453 /* LOCALNODE from FORWARD hook is not supported */
1454 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1455 (rt = (struct rt6_info *) skb_dst(skb)) &&
1456 rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) {
1457 IP_VS_DBG(1, "%s(): "
1458 "local delivery to %pI6 but in FORWARD\n",
1459 __func__, &rt->rt6i_dst);
1460 verdict = NF_DROP;
1461 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001462
1463 __ip_vs_conn_put(cp);
1464
1465 return verdict;
1466}
1467#endif
1468
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470/*
1471 * Check if it's for virtual services, look it up,
1472 * and send it on its way...
1473 */
1474static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001475ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Hans Schillstromf1313152011-01-03 14:44:55 +01001477 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001478 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001480 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct ip_vs_conn *cp;
Julian Anastasovcb591552010-10-17 16:40:51 +03001482 int ret, restart, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001483 struct netns_ipvs *ipvs;
Julius Volz51ef3482008-09-02 15:55:40 +02001484
Julian Anastasovfc604762010-10-17 16:38:15 +03001485 /* Already marked as IPVS request or reply? */
1486 if (skb->ipvs_property)
1487 return NF_ACCEPT;
1488
Julian Anastasovcb591552010-10-17 16:40:51 +03001489 /*
1490 * Big tappo:
1491 * - remote client: only PACKET_HOST
1492 * - route: used for struct net when skb->dev is unset
1493 */
1494 if (unlikely((skb->pkt_type != PACKET_HOST &&
1495 hooknum != NF_INET_LOCAL_OUT) ||
1496 !skb_dst(skb))) {
1497 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1498 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1499 " ignored in hook %u\n",
1500 skb->pkt_type, iph.protocol,
1501 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1502 return NF_ACCEPT;
1503 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001504 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Julian Anastasovcb591552010-10-17 16:40:51 +03001506 /* Bad... Do not break raw sockets */
1507 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1508 af == AF_INET)) {
1509 struct sock *sk = skb->sk;
1510 struct inet_sock *inet = inet_sk(skb->sk);
1511
1512 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1513 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515
Julius Volz94b26552009-08-31 16:22:23 +02001516#ifdef CONFIG_IP_VS_IPV6
1517 if (af == AF_INET6) {
1518 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001519 int related;
1520 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Julius Volz94b26552009-08-31 16:22:23 +02001522 if (related)
1523 return verdict;
1524 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1525 }
1526 } else
1527#endif
1528 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001529 int related;
1530 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001531
1532 if (related)
1533 return verdict;
1534 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Hans Schillstrom93304192011-01-03 14:44:51 +01001537 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* Protocol supported? */
Hans Schillstrom93304192011-01-03 14:44:51 +01001539 pd = ip_vs_proto_data_get(net, iph.protocol);
1540 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001542 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
1544 * Check if the packet belongs to an existing connection entry
1545 */
Hans Schillstrom93304192011-01-03 14:44:51 +01001546 cp = pp->conn_in_get(af, skb, &iph, iph.len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (unlikely(!cp)) {
1549 int v;
1550
Hans Schillstrom93304192011-01-03 14:44:51 +01001551 if (!pp->conn_schedule(af, skb, pd, &v, &cp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return v;
1553 }
1554
1555 if (unlikely(!cp)) {
1556 /* sorry, all this trouble for a no-hit :) */
Julian Anastasov0d796412010-10-17 16:46:17 +03001557 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001558 "ip_vs_in: packet continues traversal as normal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return NF_ACCEPT;
1560 }
1561
Julian Anastasov0d796412010-10-17 16:46:17 +03001562 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
Hans Schillstromf1313152011-01-03 14:44:55 +01001563 net = skb_net(skb);
1564 ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Check the server status */
1566 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1567 /* the destination server is not available */
1568
Hans Schillstroma0840e22011-01-03 14:44:58 +01001569 if (ipvs->sysctl_expire_nodest_conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* try to expire the connection immediately */
1571 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001573 /* don't restart its timer, and silently
1574 drop the packet. */
1575 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return NF_DROP;
1577 }
1578
1579 ip_vs_in_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001580 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (cp->packet_xmit)
1582 ret = cp->packet_xmit(skb, cp, pp);
1583 /* do not touch skb anymore */
1584 else {
1585 IP_VS_DBG_RL("warning: packet_xmit is null");
1586 ret = NF_ACCEPT;
1587 }
1588
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001589 /* Increase its packet counter and check if it is needed
1590 * to be synchronized
1591 *
1592 * Sync connection if it is about to close to
1593 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001594 *
1595 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001596 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001597
Hans Schillstrom986a0752010-11-19 14:25:13 +01001598 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Hans Schillstroma0840e22011-01-03 14:44:58 +01001599 pkts = ipvs->sysctl_sync_threshold[0];
Hans Schillstrom986a0752010-11-19 14:25:13 +01001600 else
1601 pkts = atomic_add_return(1, &cp->in_pkts);
1602
Hans Schillstromf1313152011-01-03 14:44:55 +01001603 if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001604 cp->protocol == IPPROTO_SCTP) {
1605 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
Hans Schillstroma0840e22011-01-03 14:44:58 +01001606 (pkts % ipvs->sysctl_sync_threshold[1]
1607 == ipvs->sysctl_sync_threshold[0])) ||
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001608 (cp->old_state != cp->state &&
1609 ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1610 (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1611 (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
Hans Schillstromf1313152011-01-03 14:44:55 +01001612 ip_vs_sync_conn(net, cp);
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001613 goto out;
1614 }
1615 }
1616
Julian Anastasov8ed21632010-09-01 22:19:14 +00001617 /* Keep this block last: TCP and others with pp->num_states <= 1 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001618 else if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001619 (((cp->protocol != IPPROTO_TCP ||
1620 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
Hans Schillstroma0840e22011-01-03 14:44:58 +01001621 (pkts % ipvs->sysctl_sync_threshold[1]
1622 == ipvs->sysctl_sync_threshold[0])) ||
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001623 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1624 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
Xiaotian Feng9abfe312009-12-14 16:38:21 +01001625 (cp->state == IP_VS_TCP_S_CLOSE) ||
Rumen G. Bogdanovski9d3a0de2008-07-16 20:04:23 -07001626 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1627 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
Hans Schillstromf1313152011-01-03 14:44:55 +01001628 ip_vs_sync_conn(net, cp);
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001629out:
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001630 cp->old_state = cp->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 ip_vs_conn_put(cp);
1633 return ret;
1634}
1635
Julian Anastasovcb591552010-10-17 16:40:51 +03001636/*
1637 * AF_INET handler in NF_INET_LOCAL_IN chain
1638 * Schedule and forward packets from remote clients
1639 */
1640static unsigned int
1641ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1642 const struct net_device *in,
1643 const struct net_device *out,
1644 int (*okfn)(struct sk_buff *))
1645{
1646 return ip_vs_in(hooknum, skb, AF_INET);
1647}
1648
1649/*
1650 * AF_INET handler in NF_INET_LOCAL_OUT chain
1651 * Schedule and forward packets from local clients
1652 */
1653static unsigned int
1654ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1655 const struct net_device *in, const struct net_device *out,
1656 int (*okfn)(struct sk_buff *))
1657{
1658 unsigned int verdict;
1659
1660 /* Disable BH in LOCAL_OUT until all places are fixed */
1661 local_bh_disable();
1662 verdict = ip_vs_in(hooknum, skb, AF_INET);
1663 local_bh_enable();
1664 return verdict;
1665}
1666
1667#ifdef CONFIG_IP_VS_IPV6
1668
1669/*
1670 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1671 * Schedule and forward packets from remote clients
1672 */
1673static unsigned int
1674ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1675 const struct net_device *in,
1676 const struct net_device *out,
1677 int (*okfn)(struct sk_buff *))
1678{
1679 return ip_vs_in(hooknum, skb, AF_INET6);
1680}
1681
1682/*
1683 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1684 * Schedule and forward packets from local clients
1685 */
1686static unsigned int
1687ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1688 const struct net_device *in, const struct net_device *out,
1689 int (*okfn)(struct sk_buff *))
1690{
1691 unsigned int verdict;
1692
1693 /* Disable BH in LOCAL_OUT until all places are fixed */
1694 local_bh_disable();
1695 verdict = ip_vs_in(hooknum, skb, AF_INET6);
1696 local_bh_enable();
1697 return verdict;
1698}
1699
1700#endif
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001704 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * related packets destined for 0.0.0.0/0.
1706 * When fwmark-based virtual service is used, such as transparent
1707 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1708 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001709 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 * and send them to ip_vs_in_icmp.
1711 */
1712static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001713ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 const struct net_device *in, const struct net_device *out,
1715 int (*okfn)(struct sk_buff *))
1716{
1717 int r;
1718
Herbert Xu3db05fe2007-10-15 00:53:15 -07001719 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return NF_ACCEPT;
1721
Herbert Xu3db05fe2007-10-15 00:53:15 -07001722 return ip_vs_in_icmp(skb, &r, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Julius Volz2a3b7912008-09-02 15:55:47 +02001725#ifdef CONFIG_IP_VS_IPV6
1726static unsigned int
1727ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1728 const struct net_device *in, const struct net_device *out,
1729 int (*okfn)(struct sk_buff *))
1730{
1731 int r;
1732
1733 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1734 return NF_ACCEPT;
1735
1736 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1737}
1738#endif
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Patrick McHardy19994142007-12-05 01:23:00 -08001741static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001742 /* After packet filtering, change source only for VS/NAT */
1743 {
1744 .hook = ip_vs_reply4,
1745 .owner = THIS_MODULE,
1746 .pf = PF_INET,
1747 .hooknum = NF_INET_LOCAL_IN,
1748 .priority = 99,
1749 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001750 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1751 * or VS/NAT(change destination), so that filtering rules can be
1752 * applied to IPVS. */
1753 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001754 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001755 .owner = THIS_MODULE,
1756 .pf = PF_INET,
Julian Anastasovcb591552010-10-17 16:40:51 +03001757 .hooknum = NF_INET_LOCAL_IN,
1758 .priority = 101,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001759 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001760 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001761 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001762 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001763 .owner = THIS_MODULE,
1764 .pf = PF_INET,
Julian Anastasovfc604762010-10-17 16:38:15 +03001765 .hooknum = NF_INET_LOCAL_OUT,
1766 .priority = -99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001767 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001768 /* After mangle, schedule and forward local requests */
1769 {
1770 .hook = ip_vs_local_request4,
1771 .owner = THIS_MODULE,
1772 .pf = PF_INET,
1773 .hooknum = NF_INET_LOCAL_OUT,
1774 .priority = -98,
1775 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001776 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1777 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1778 {
1779 .hook = ip_vs_forward_icmp,
1780 .owner = THIS_MODULE,
1781 .pf = PF_INET,
Julian Anastasovcb591552010-10-17 16:40:51 +03001782 .hooknum = NF_INET_FORWARD,
1783 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001784 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001785 /* After packet filtering, change source only for VS/NAT */
1786 {
1787 .hook = ip_vs_reply4,
1788 .owner = THIS_MODULE,
1789 .pf = PF_INET,
1790 .hooknum = NF_INET_FORWARD,
1791 .priority = 100,
1792 },
Julius Volz473b23d2008-09-02 15:55:54 +02001793#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001794 /* After packet filtering, change source only for VS/NAT */
1795 {
1796 .hook = ip_vs_reply6,
1797 .owner = THIS_MODULE,
1798 .pf = PF_INET6,
1799 .hooknum = NF_INET_LOCAL_IN,
1800 .priority = 99,
1801 },
Julius Volz473b23d2008-09-02 15:55:54 +02001802 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1803 * or VS/NAT(change destination), so that filtering rules can be
1804 * applied to IPVS. */
1805 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001806 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001807 .owner = THIS_MODULE,
1808 .pf = PF_INET6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001809 .hooknum = NF_INET_LOCAL_IN,
1810 .priority = 101,
Julius Volz473b23d2008-09-02 15:55:54 +02001811 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001812 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001813 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001814 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001815 .owner = THIS_MODULE,
Julian Anastasovfc604762010-10-17 16:38:15 +03001816 .pf = PF_INET,
1817 .hooknum = NF_INET_LOCAL_OUT,
1818 .priority = -99,
Julius Volz473b23d2008-09-02 15:55:54 +02001819 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001820 /* After mangle, schedule and forward local requests */
1821 {
1822 .hook = ip_vs_local_request6,
1823 .owner = THIS_MODULE,
1824 .pf = PF_INET6,
1825 .hooknum = NF_INET_LOCAL_OUT,
1826 .priority = -98,
1827 },
Julius Volz473b23d2008-09-02 15:55:54 +02001828 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1829 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1830 {
1831 .hook = ip_vs_forward_icmp_v6,
1832 .owner = THIS_MODULE,
1833 .pf = PF_INET6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001834 .hooknum = NF_INET_FORWARD,
1835 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02001836 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001837 /* After packet filtering, change source only for VS/NAT */
1838 {
1839 .hook = ip_vs_reply6,
1840 .owner = THIS_MODULE,
1841 .pf = PF_INET6,
1842 .hooknum = NF_INET_FORWARD,
1843 .priority = 100,
1844 },
Julius Volz473b23d2008-09-02 15:55:54 +02001845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001847/*
1848 * Initialize IP Virtual Server netns mem.
1849 */
1850static int __net_init __ip_vs_init(struct net *net)
1851{
1852 struct netns_ipvs *ipvs;
1853
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001854 ipvs = net_generic(net, ip_vs_net_id);
1855 if (ipvs == NULL) {
1856 pr_err("%s(): no memory.\n", __func__);
1857 return -ENOMEM;
1858 }
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001859 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001860 /* Counters used for creating unique names */
1861 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1862 atomic_inc(&ipvs_netns_cnt);
1863 net->ipvs = ipvs;
Simon Hormana870c8c2011-02-01 18:21:53 +01001864 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001865 sizeof(struct netns_ipvs), ipvs->gen);
1866 return 0;
1867}
1868
1869static void __net_exit __ip_vs_cleanup(struct net *net)
1870{
Changli Gao9f4e1cc2011-01-25 12:40:18 +08001871 IP_VS_DBG(10, "ipvs netns %d released\n", net_ipvs(net)->gen);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001872}
1873
1874static struct pernet_operations ipvs_core_ops = {
1875 .init = __ip_vs_init,
1876 .exit = __ip_vs_cleanup,
1877 .id = &ip_vs_net_id,
1878 .size = sizeof(struct netns_ipvs),
1879};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881/*
1882 * Initialize IP Virtual Server
1883 */
1884static int __init ip_vs_init(void)
1885{
1886 int ret;
1887
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001888 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
1889 if (ret < 0)
1890 return ret;
Sven Wegenera919cf42008-08-14 00:47:16 +02001891
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001892 ip_vs_estimator_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 ret = ip_vs_control_init();
1894 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001895 pr_err("can't setup control.\n");
Sven Wegenera919cf42008-08-14 00:47:16 +02001896 goto cleanup_estimator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
1899 ip_vs_protocol_init();
1900
1901 ret = ip_vs_app_init();
1902 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001903 pr_err("can't setup application helper.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto cleanup_protocol;
1905 }
1906
1907 ret = ip_vs_conn_init();
1908 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001909 pr_err("can't setup connection table.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto cleanup_app;
1911 }
1912
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001913 ret = ip_vs_sync_init();
1914 if (ret < 0) {
1915 pr_err("can't setup sync data.\n");
1916 goto cleanup_conn;
1917 }
1918
Patrick McHardy41c5b312007-12-05 01:22:43 -08001919 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001921 pr_err("can't register hooks.\n");
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001922 goto cleanup_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924
Hannes Eder1e3e2382009-08-02 11:05:41 +00001925 pr_info("ipvs loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return ret;
1927
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001928cleanup_sync:
1929 ip_vs_sync_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 cleanup_conn:
1931 ip_vs_conn_cleanup();
1932 cleanup_app:
1933 ip_vs_app_cleanup();
1934 cleanup_protocol:
1935 ip_vs_protocol_cleanup();
1936 ip_vs_control_cleanup();
Sven Wegenera919cf42008-08-14 00:47:16 +02001937 cleanup_estimator:
1938 ip_vs_estimator_cleanup();
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001939 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return ret;
1941}
1942
1943static void __exit ip_vs_cleanup(void)
1944{
Patrick McHardy41c5b312007-12-05 01:22:43 -08001945 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001946 ip_vs_sync_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 ip_vs_conn_cleanup();
1948 ip_vs_app_cleanup();
1949 ip_vs_protocol_cleanup();
1950 ip_vs_control_cleanup();
Sven Wegenera919cf42008-08-14 00:47:16 +02001951 ip_vs_estimator_cleanup();
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001952 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Hannes Eder1e3e2382009-08-02 11:05:41 +00001953 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956module_init(ip_vs_init);
1957module_exit(ip_vs_cleanup);
1958MODULE_LICENSE("GPL");