blob: 7205b49c56c1bff02ba3dc610f824f7a0c7af38a [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
135 s = this_cpu_ptr(ipvs->cpustats);
136 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
165 s = this_cpu_ptr(ipvs->cpustats);
166 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
186 s = this_cpu_ptr(ipvs->cpustats);
187 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{
Al Viro014d7302006-09-28 14:29:52 -0700502 __be16 _ports[2], *pptr;
Julius Volz28364a52008-09-02 15:55:43 +0200503 struct ip_vs_iphdr iph;
Julius Volz2a3b7912008-09-02 15:55:47 +0200504 int unicast;
Hans Schillstrom93304192011-01-03 14:44:51 +0100505
Julius Volz2a3b7912008-09-02 15:55:47 +0200506 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Julius Volz28364a52008-09-02 15:55:43 +0200508 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (pptr == NULL) {
510 ip_vs_service_put(svc);
511 return NF_DROP;
512 }
513
Julius Volz2a3b7912008-09-02 15:55:47 +0200514#ifdef CONFIG_IP_VS_IPV6
515 if (svc->af == AF_INET6)
516 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
517 else
518#endif
519 unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200522 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 a cache_bypass connection entry */
Julius Volz2a3b7912008-09-02 15:55:47 +0200524 if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int ret, cs;
526 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200527 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
528 iph.protocol == IPPROTO_UDP)?
529 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000530 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 ip_vs_service_put(svc);
533
534 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000535 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900536 {
537 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100538 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
Simon Hormanf11017e2010-08-22 21:37:52 +0900539 &iph.saddr, pptr[0],
540 &iph.daddr, pptr[1], &p);
541 cp = ip_vs_conn_new(&p, &daddr, 0,
542 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100543 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900544 if (!cp)
545 return NF_DROP;
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* statistics */
549 ip_vs_in_stats(cp, skb);
550
551 /* set state */
Hans Schillstrom93304192011-01-03 14:44:51 +0100552 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* transmit the first SYN packet */
Hans Schillstrom93304192011-01-03 14:44:51 +0100555 ret = cp->packet_xmit(skb, cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* do not touch skb anymore */
557
558 atomic_inc(&cp->in_pkts);
559 ip_vs_conn_put(cp);
560 return ret;
561 }
562
563 /*
564 * When the virtual ftp service is presented, packets destined
565 * for other services on the VIP may get here (except services
566 * listed in the ipvs table), pass the packets, because it is
567 * not ipvs job to decide to drop the packets.
568 */
569 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
570 ip_vs_service_put(svc);
571 return NF_ACCEPT;
572 }
573
574 ip_vs_service_put(svc);
575
576 /*
577 * Notify the client that the destination is unreachable, and
578 * release the socket buffer.
579 * Since it is in IP layer, the TCP socket is not actually
580 * created, the TCP RST packet cannot be sent, instead that
581 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
582 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200583#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300584 if (svc->af == AF_INET6) {
585 if (!skb->dev) {
586 struct net *net = dev_net(skb_dst(skb)->dev);
587
588 skb->dev = net->loopback_dev;
589 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000590 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300591 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200592#endif
593 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return NF_DROP;
596}
597
Al Virob1550f22006-11-14 21:37:50 -0800598__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Al Virod3bc23e2006-11-14 21:24:49 -0800600 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300603static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
604{
605 if (NF_INET_LOCAL_IN == hooknum)
606 return IP_DEFRAG_VS_IN;
607 if (NF_INET_FORWARD == hooknum)
608 return IP_DEFRAG_VS_FWD;
609 return IP_DEFRAG_VS_OUT;
610}
611
Herbert Xu776c7292007-10-14 00:38:32 -0700612static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Herbert Xu776c7292007-10-14 00:38:32 -0700614 int err = ip_defrag(skb, user);
615
616 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700617 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700618
619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Julius Volz2a3b7912008-09-02 15:55:47 +0200622#ifdef CONFIG_IP_VS_IPV6
623static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
624{
625 /* TODO IPv6: Find out what to do here for IPv6 */
626 return 0;
627}
628#endif
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
631 * Packet has been made sufficiently writable in caller
632 * - inout: 1=in->out, 0=out->in
633 */
634void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
635 struct ip_vs_conn *cp, int inout)
636{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700637 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700639 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
640 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
642
643 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200644 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200646 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ip_send_check(ciph);
648 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200649 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200651 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ip_send_check(ciph);
653 }
654
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100655 /* the TCP/UDP/SCTP port */
656 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
657 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700658 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (inout)
661 ports[1] = cp->vport;
662 else
663 ports[0] = cp->dport;
664 }
665
666 /* And finally the ICMP checksum */
667 icmph->checksum = 0;
668 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
669 skb->ip_summed = CHECKSUM_UNNECESSARY;
670
671 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300672 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 "Forwarding altered outgoing ICMP");
674 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300675 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 "Forwarding altered incoming ICMP");
677}
678
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200679#ifdef CONFIG_IP_VS_IPV6
680void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
681 struct ip_vs_conn *cp, int inout)
682{
683 struct ipv6hdr *iph = ipv6_hdr(skb);
684 unsigned int icmp_offset = sizeof(struct ipv6hdr);
685 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
686 icmp_offset);
687 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
688
689 if (inout) {
690 iph->saddr = cp->vaddr.in6;
691 ciph->daddr = cp->vaddr.in6;
692 } else {
693 iph->daddr = cp->daddr.in6;
694 ciph->saddr = cp->daddr.in6;
695 }
696
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100697 /* the TCP/UDP/SCTP port */
698 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
699 IPPROTO_SCTP == ciph->nexthdr) {
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200700 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
701
702 if (inout)
703 ports[1] = cp->vport;
704 else
705 ports[0] = cp->dport;
706 }
707
708 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700709 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
710 skb->len - icmp_offset,
711 IPPROTO_ICMPV6, 0);
712 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
713 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
714 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200715
716 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300717 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
718 (void *)ciph - (void *)iph,
719 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200720 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300721 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
722 (void *)ciph - (void *)iph,
723 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200724}
725#endif
726
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000727/* Handle relevant response ICMP messages - forward to the right
728 * destination host. Used for NAT and local client.
729 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000730static int handle_response_icmp(int af, struct sk_buff *skb,
731 union nf_inet_addr *snet,
732 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000733 struct ip_vs_protocol *pp,
734 unsigned int offset, unsigned int ihl)
735{
736 unsigned int verdict = NF_DROP;
737
738 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000739 pr_err("shouldn't reach here, because the box is on the "
740 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000741 }
742
743 /* Ensure the checksum is correct */
744 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
745 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000746 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
747 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000748 goto out;
749 }
750
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100751 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
752 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000753 offset += 2 * sizeof(__u16);
754 if (!skb_make_writable(skb, offset))
755 goto out;
756
Simon Hormanf2428ed2008-09-05 11:17:14 +1000757#ifdef CONFIG_IP_VS_IPV6
758 if (af == AF_INET6)
759 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
760 else
761#endif
762 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000763
Julian Anastasovf5a41842010-10-17 16:35:46 +0300764#ifdef CONFIG_IP_VS_IPV6
765 if (af == AF_INET6) {
766 if (sysctl_ip_vs_snat_reroute && ip6_route_me_harder(skb) != 0)
767 goto out;
768 } else
769#endif
770 if ((sysctl_ip_vs_snat_reroute ||
771 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
772 ip_route_me_harder(skb, RTN_LOCAL) != 0)
773 goto out;
774
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000775 /* do the statistics and put it back */
776 ip_vs_out_stats(cp, skb);
777
Julian Anastasovcf356d62010-10-17 16:21:07 +0300778 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200779 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300780 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200781 else
782 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000783 verdict = NF_ACCEPT;
784
785out:
786 __ip_vs_conn_put(cp);
787
788 return verdict;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
792 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000793 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300796static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
797 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct iphdr *iph;
800 struct icmphdr _icmph, *ic;
801 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200802 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct ip_vs_conn *cp;
804 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000805 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000806 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 *related = 1;
809
810 /* reassemble IP fragments */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700811 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300812 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700816 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 offset = ihl = iph->ihl * 4;
818 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
819 if (ic == NULL)
820 return NF_DROP;
821
Harvey Harrison14d5e832008-10-31 00:54:29 -0700822 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700824 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
827 * Work through seeing if this is for us.
828 * These checks are supposed to be in an order that means easy
829 * things are checked first to speed up processing.... however
830 * this means that some packets will manage to get a long way
831 * down this stack and then be rejected, but that's life.
832 */
833 if ((ic->type != ICMP_DEST_UNREACH) &&
834 (ic->type != ICMP_SOURCE_QUENCH) &&
835 (ic->type != ICMP_TIME_EXCEEDED)) {
836 *related = 0;
837 return NF_ACCEPT;
838 }
839
840 /* Now find the contained IP header */
841 offset += sizeof(_icmph);
842 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
843 if (cih == NULL)
844 return NF_ACCEPT; /* The packet looks wrong, ignore */
845
846 pp = ip_vs_proto_get(cih->protocol);
847 if (!pp)
848 return NF_ACCEPT;
849
850 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900851 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 pp->dont_defrag))
853 return NF_ACCEPT;
854
Julian Anastasov0d796412010-10-17 16:46:17 +0300855 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
856 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 offset += cih->ihl * 4;
859
Julius Volz51ef3482008-09-02 15:55:40 +0200860 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +0100862 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!cp)
864 return NF_ACCEPT;
865
Simon Hormanf2428ed2008-09-05 11:17:14 +1000866 snet.ip = iph->saddr;
867 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
868 pp, offset, ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Julius Volz2a3b7912008-09-02 15:55:47 +0200871#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300872static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
873 unsigned int hooknum)
Julius Volz2a3b7912008-09-02 15:55:47 +0200874{
875 struct ipv6hdr *iph;
876 struct icmp6hdr _icmph, *ic;
877 struct ipv6hdr _ciph, *cih; /* The ip header contained
878 within the ICMP */
879 struct ip_vs_iphdr ciph;
880 struct ip_vs_conn *cp;
881 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000882 unsigned int offset;
883 union nf_inet_addr snet;
Julius Volz2a3b7912008-09-02 15:55:47 +0200884
885 *related = 1;
886
887 /* reassemble IP fragments */
888 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300889 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +0200890 return NF_STOLEN;
891 }
892
893 iph = ipv6_hdr(skb);
894 offset = sizeof(struct ipv6hdr);
895 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
896 if (ic == NULL)
897 return NF_DROP;
898
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700899 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
Julius Volz2a3b7912008-09-02 15:55:47 +0200900 ic->icmp6_type, ntohs(icmpv6_id(ic)),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700901 &iph->saddr, &iph->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200902
903 /*
904 * Work through seeing if this is for us.
905 * These checks are supposed to be in an order that means easy
906 * things are checked first to speed up processing.... however
907 * this means that some packets will manage to get a long way
908 * down this stack and then be rejected, but that's life.
909 */
910 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
911 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
912 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
913 *related = 0;
914 return NF_ACCEPT;
915 }
916
917 /* Now find the contained IP header */
918 offset += sizeof(_icmph);
919 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
920 if (cih == NULL)
921 return NF_ACCEPT; /* The packet looks wrong, ignore */
922
923 pp = ip_vs_proto_get(cih->nexthdr);
924 if (!pp)
925 return NF_ACCEPT;
926
927 /* Is the embedded protocol header present? */
928 /* TODO: we don't support fragmentation at the moment anyways */
929 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
930 return NF_ACCEPT;
931
Julian Anastasov0d796412010-10-17 16:46:17 +0300932 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
933 "Checking outgoing ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +0200934
935 offset += sizeof(struct ipv6hdr);
936
937 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
938 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +0100939 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
Julius Volz2a3b7912008-09-02 15:55:47 +0200940 if (!cp)
941 return NF_ACCEPT;
942
Simon Horman178f5e42008-09-08 09:34:46 +1000943 ipv6_addr_copy(&snet.in6, &iph->saddr);
Simon Hormanf2428ed2008-09-05 11:17:14 +1000944 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
945 pp, offset, sizeof(struct ipv6hdr));
Julius Volz2a3b7912008-09-02 15:55:47 +0200946}
947#endif
948
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100949/*
950 * Check if sctp chunc is ABORT chunk
951 */
952static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
953{
954 sctp_chunkhdr_t *sch, schunk;
955 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
956 sizeof(schunk), &schunk);
957 if (sch == NULL)
958 return 0;
959 if (sch->type == SCTP_CID_ABORT)
960 return 1;
961 return 0;
962}
963
Julius Volz2a3b7912008-09-02 15:55:47 +0200964static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct tcphdr _tcph, *th;
967
Julius Volz2a3b7912008-09-02 15:55:47 +0200968 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (th == NULL)
970 return 0;
971 return th->rst;
972}
973
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000974/* Handle response packets: rewrite addresses and send away...
975 * Used for NAT and local client.
976 */
977static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +0100978handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000979 struct ip_vs_conn *cp, int ihl)
980{
Hans Schillstrom93304192011-01-03 14:44:51 +0100981 struct ip_vs_protocol *pp = pd->pp;
982
Julian Anastasov0d796412010-10-17 16:46:17 +0300983 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000984
985 if (!skb_make_writable(skb, ihl))
986 goto drop;
987
988 /* mangle the packet */
989 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
990 goto drop;
991
992#ifdef CONFIG_IP_VS_IPV6
993 if (af == AF_INET6)
994 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
995 else
996#endif
997 {
998 ip_hdr(skb)->saddr = cp->vaddr.ip;
999 ip_send_check(ip_hdr(skb));
1000 }
1001
Julian Anastasov8a803042010-09-21 17:38:57 +02001002 /*
1003 * nf_iterate does not expect change in the skb->dst->dev.
1004 * It looks like it is not fatal to enable this code for hooks
1005 * where our handlers are at the end of the chain list and
1006 * when all next handlers use skb->dst->dev and not outdev.
1007 * It will definitely route properly the inout NAT traffic
1008 * when multiple paths are used.
1009 */
1010
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001011 /* For policy routing, packets originating from this
1012 * machine itself may be routed differently to packets
1013 * passing through. We want this packet to be routed as
1014 * if it came from this machine itself. So re-compute
1015 * the routing information.
1016 */
1017#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf5a41842010-10-17 16:35:46 +03001018 if (af == AF_INET6) {
1019 if (sysctl_ip_vs_snat_reroute && ip6_route_me_harder(skb) != 0)
1020 goto drop;
1021 } else
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001022#endif
Julian Anastasovf5a41842010-10-17 16:35:46 +03001023 if ((sysctl_ip_vs_snat_reroute ||
1024 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
1025 ip_route_me_harder(skb, RTN_LOCAL) != 0)
1026 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001027
Julian Anastasov0d796412010-10-17 16:46:17 +03001028 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001029
1030 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001031 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001032 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001033 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001034 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001035 else
1036 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001037 ip_vs_conn_put(cp);
1038
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001039 LeaveFunction(11);
1040 return NF_ACCEPT;
1041
1042drop:
1043 ip_vs_conn_put(cp);
1044 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001045 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001046 return NF_STOLEN;
1047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001050 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
1052static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001053ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Hans Schillstromfc723252011-01-03 14:44:43 +01001055 struct net *net = NULL;
Julius Volz51ef3482008-09-02 15:55:40 +02001056 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001058 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 EnterFunction(11);
1062
Julian Anastasovfc604762010-10-17 16:38:15 +03001063 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001064 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return NF_ACCEPT;
1066
Julian Anastasovfc604762010-10-17 16:38:15 +03001067 /* Bad... Do not break raw sockets */
1068 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1069 af == AF_INET)) {
1070 struct sock *sk = skb->sk;
1071 struct inet_sock *inet = inet_sk(skb->sk);
1072
1073 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1074 return NF_ACCEPT;
1075 }
1076
1077 if (unlikely(!skb_dst(skb)))
1078 return NF_ACCEPT;
1079
Hans Schillstromfc723252011-01-03 14:44:43 +01001080 net = skb_net(skb);
Julius Volz2a3b7912008-09-02 15:55:47 +02001081 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1082#ifdef CONFIG_IP_VS_IPV6
1083 if (af == AF_INET6) {
1084 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001085 int related;
1086 int verdict = ip_vs_out_icmp_v6(skb, &related,
1087 hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Julian Anastasovf5a41842010-10-17 16:35:46 +03001089 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001090 return verdict;
1091 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1092 }
1093 } else
1094#endif
1095 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001096 int related;
1097 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001098
Julian Anastasovf5a41842010-10-17 16:35:46 +03001099 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001100 return verdict;
1101 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Hans Schillstrom93304192011-01-03 14:44:51 +01001104 pd = ip_vs_proto_data_get(net, iph.protocol);
1105 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001107 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001110#ifdef CONFIG_IP_VS_IPV6
1111 if (af == AF_INET6) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001112 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1113 if (ip_vs_gather_frags_v6(skb,
1114 ip_vs_defrag_user(hooknum)))
1115 return NF_STOLEN;
Julius Volz2a3b7912008-09-02 15:55:47 +02001116 }
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001117
1118 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001119 } else
1120#endif
1121 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
1122 !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001123 if (ip_vs_gather_frags(skb,
1124 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001125 return NF_STOLEN;
1126
1127 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /*
1131 * Check if the packet belongs to an existing entry
1132 */
Hans Schillstrom93304192011-01-03 14:44:51 +01001133 cp = pp->conn_out_get(af, skb, &iph, iph.len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Julian Anastasovcb591552010-10-17 16:40:51 +03001135 if (likely(cp))
Hans Schillstrom93304192011-01-03 14:44:51 +01001136 return handle_response(af, skb, pd, cp, iph.len);
Julian Anastasovcb591552010-10-17 16:40:51 +03001137 if (sysctl_ip_vs_nat_icmp_send &&
1138 (pp->protocol == IPPROTO_TCP ||
1139 pp->protocol == IPPROTO_UDP ||
1140 pp->protocol == IPPROTO_SCTP)) {
1141 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Julian Anastasovcb591552010-10-17 16:40:51 +03001143 pptr = skb_header_pointer(skb, iph.len,
1144 sizeof(_ports), _ports);
1145 if (pptr == NULL)
1146 return NF_ACCEPT; /* Not for me */
Hans Schillstromfc723252011-01-03 14:44:43 +01001147 if (ip_vs_lookup_real_service(net, af, iph.protocol,
Julian Anastasovcb591552010-10-17 16:40:51 +03001148 &iph.saddr,
1149 pptr[0])) {
1150 /*
1151 * Notify the real server: there is no
1152 * existing entry if it is not RST
1153 * packet or not TCP packet.
1154 */
1155 if ((iph.protocol != IPPROTO_TCP &&
1156 iph.protocol != IPPROTO_SCTP)
1157 || ((iph.protocol == IPPROTO_TCP
1158 && !is_tcp_reset(skb, iph.len))
1159 || (iph.protocol == IPPROTO_SCTP
1160 && !is_sctp_abort(skb,
1161 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001162#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001163 if (af == AF_INET6) {
1164 struct net *net =
1165 dev_net(skb_dst(skb)->dev);
1166
1167 if (!skb->dev)
1168 skb->dev = net->loopback_dev;
1169 icmpv6_send(skb,
1170 ICMPV6_DEST_UNREACH,
1171 ICMPV6_PORT_UNREACH,
1172 0);
1173 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001174#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001175 icmp_send(skb,
1176 ICMP_DEST_UNREACH,
1177 ICMP_PORT_UNREACH, 0);
1178 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Julian Anastasov0d796412010-10-17 16:46:17 +03001182 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001183 "ip_vs_out: packet continues traversal as normal");
1184 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Julian Anastasovfc604762010-10-17 16:38:15 +03001187/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001188 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1189 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001190 * Check if packet is reply for established ip_vs_conn.
1191 */
1192static unsigned int
1193ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1194 const struct net_device *in, const struct net_device *out,
1195 int (*okfn)(struct sk_buff *))
1196{
1197 return ip_vs_out(hooknum, skb, AF_INET);
1198}
1199
1200/*
1201 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1202 * Check if packet is reply for established ip_vs_conn.
1203 */
1204static unsigned int
1205ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1206 const struct net_device *in, const struct net_device *out,
1207 int (*okfn)(struct sk_buff *))
1208{
1209 unsigned int verdict;
1210
1211 /* Disable BH in LOCAL_OUT until all places are fixed */
1212 local_bh_disable();
1213 verdict = ip_vs_out(hooknum, skb, AF_INET);
1214 local_bh_enable();
1215 return verdict;
1216}
1217
1218#ifdef CONFIG_IP_VS_IPV6
1219
1220/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001221 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1222 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001223 * Check if packet is reply for established ip_vs_conn.
1224 */
1225static unsigned int
1226ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1227 const struct net_device *in, const struct net_device *out,
1228 int (*okfn)(struct sk_buff *))
1229{
1230 return ip_vs_out(hooknum, skb, AF_INET6);
1231}
1232
1233/*
1234 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1235 * Check if packet is reply for established ip_vs_conn.
1236 */
1237static unsigned int
1238ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1239 const struct net_device *in, const struct net_device *out,
1240 int (*okfn)(struct sk_buff *))
1241{
1242 unsigned int verdict;
1243
1244 /* Disable BH in LOCAL_OUT until all places are fixed */
1245 local_bh_disable();
1246 verdict = ip_vs_out(hooknum, skb, AF_INET6);
1247 local_bh_enable();
1248 return verdict;
1249}
1250
1251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253/*
1254 * Handle ICMP messages in the outside-to-inside direction (incoming).
1255 * Find any that might be relevant, check against existing connections,
1256 * forward to the right destination host if relevant.
1257 * Currently handles error types - unreachable, quench, ttl exceeded.
1258 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001259static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001260ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Hans Schillstrom93304192011-01-03 14:44:51 +01001262 struct net *net = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct iphdr *iph;
1264 struct icmphdr _icmph, *ic;
1265 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001266 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct ip_vs_conn *cp;
1268 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001269 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned int offset, ihl, verdict;
Simon Hormanf2428ed2008-09-05 11:17:14 +10001271 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 *related = 1;
1274
1275 /* reassemble IP fragments */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001276 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001277 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001281 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 offset = ihl = iph->ihl * 4;
1283 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1284 if (ic == NULL)
1285 return NF_DROP;
1286
Harvey Harrison14d5e832008-10-31 00:54:29 -07001287 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001289 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /*
1292 * Work through seeing if this is for us.
1293 * These checks are supposed to be in an order that means easy
1294 * things are checked first to speed up processing.... however
1295 * this means that some packets will manage to get a long way
1296 * down this stack and then be rejected, but that's life.
1297 */
1298 if ((ic->type != ICMP_DEST_UNREACH) &&
1299 (ic->type != ICMP_SOURCE_QUENCH) &&
1300 (ic->type != ICMP_TIME_EXCEEDED)) {
1301 *related = 0;
1302 return NF_ACCEPT;
1303 }
1304
1305 /* Now find the contained IP header */
1306 offset += sizeof(_icmph);
1307 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1308 if (cih == NULL)
1309 return NF_ACCEPT; /* The packet looks wrong, ignore */
1310
Hans Schillstrom93304192011-01-03 14:44:51 +01001311 net = skb_net(skb);
1312 pd = ip_vs_proto_data_get(net, cih->protocol);
1313 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001315 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001318 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 pp->dont_defrag))
1320 return NF_ACCEPT;
1321
Julian Anastasov0d796412010-10-17 16:46:17 +03001322 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1323 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 offset += cih->ihl * 4;
1326
Julius Volz51ef3482008-09-02 15:55:40 +02001327 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +01001329 cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001330 if (!cp) {
1331 /* The packet could also belong to a local client */
Hans Schillstrom93304192011-01-03 14:44:51 +01001332 cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001333 if (cp) {
1334 snet.ip = iph->saddr;
1335 return handle_response_icmp(AF_INET, skb, &snet,
1336 cih->protocol, cp, pp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001337 offset, ihl);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return NF_ACCEPT;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 verdict = NF_DROP;
1343
1344 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001345 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001347 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1348 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 goto out;
1350 }
1351
1352 /* do the statistics and put it back */
1353 ip_vs_in_stats(cp, skb);
1354 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1355 offset += 2 * sizeof(__u16);
1356 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
Julian Anastasov489fded2010-10-17 16:27:31 +03001357 /* LOCALNODE from FORWARD hook is not supported */
1358 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1359 skb_rtable(skb)->rt_flags & RTCF_LOCAL) {
1360 IP_VS_DBG(1, "%s(): "
1361 "local delivery to %pI4 but in FORWARD\n",
1362 __func__, &skb_rtable(skb)->rt_dst);
1363 verdict = NF_DROP;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 out:
1367 __ip_vs_conn_put(cp);
1368
1369 return verdict;
1370}
1371
Julius Volz2a3b7912008-09-02 15:55:47 +02001372#ifdef CONFIG_IP_VS_IPV6
1373static int
1374ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1375{
Hans Schillstrom93304192011-01-03 14:44:51 +01001376 struct net *net = NULL;
Julius Volz2a3b7912008-09-02 15:55:47 +02001377 struct ipv6hdr *iph;
1378 struct icmp6hdr _icmph, *ic;
1379 struct ipv6hdr _ciph, *cih; /* The ip header contained
1380 within the ICMP */
1381 struct ip_vs_iphdr ciph;
1382 struct ip_vs_conn *cp;
1383 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001384 struct ip_vs_proto_data *pd;
Julius Volz2a3b7912008-09-02 15:55:47 +02001385 unsigned int offset, verdict;
Simon Hormanf2428ed2008-09-05 11:17:14 +10001386 union nf_inet_addr snet;
Julian Anastasov489fded2010-10-17 16:27:31 +03001387 struct rt6_info *rt;
Julius Volz2a3b7912008-09-02 15:55:47 +02001388
1389 *related = 1;
1390
1391 /* reassemble IP fragments */
1392 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001393 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001394 return NF_STOLEN;
1395 }
1396
1397 iph = ipv6_hdr(skb);
1398 offset = sizeof(struct ipv6hdr);
1399 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1400 if (ic == NULL)
1401 return NF_DROP;
1402
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001403 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
Julius Volz2a3b7912008-09-02 15:55:47 +02001404 ic->icmp6_type, ntohs(icmpv6_id(ic)),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001405 &iph->saddr, &iph->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001406
1407 /*
1408 * Work through seeing if this is for us.
1409 * These checks are supposed to be in an order that means easy
1410 * things are checked first to speed up processing.... however
1411 * this means that some packets will manage to get a long way
1412 * down this stack and then be rejected, but that's life.
1413 */
1414 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1415 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1416 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1417 *related = 0;
1418 return NF_ACCEPT;
1419 }
1420
1421 /* Now find the contained IP header */
1422 offset += sizeof(_icmph);
1423 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1424 if (cih == NULL)
1425 return NF_ACCEPT; /* The packet looks wrong, ignore */
1426
Hans Schillstrom93304192011-01-03 14:44:51 +01001427 net = skb_net(skb);
1428 pd = ip_vs_proto_data_get(net, cih->nexthdr);
1429 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001430 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001431 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001432
1433 /* Is the embedded protocol header present? */
1434 /* TODO: we don't support fragmentation at the moment anyways */
1435 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1436 return NF_ACCEPT;
1437
Julian Anastasov0d796412010-10-17 16:46:17 +03001438 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1439 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001440
1441 offset += sizeof(struct ipv6hdr);
1442
1443 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1444 /* The embedded headers contain source and dest in reverse order */
Hans Schillstrom93304192011-01-03 14:44:51 +01001445 cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001446 if (!cp) {
1447 /* The packet could also belong to a local client */
Hans Schillstrom93304192011-01-03 14:44:51 +01001448 cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001449 if (cp) {
Simon Horman178f5e42008-09-08 09:34:46 +10001450 ipv6_addr_copy(&snet.in6, &iph->saddr);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001451 return handle_response_icmp(AF_INET6, skb, &snet,
1452 cih->nexthdr,
1453 cp, pp, offset,
1454 sizeof(struct ipv6hdr));
1455 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001456 return NF_ACCEPT;
Simon Hormanf2428ed2008-09-05 11:17:14 +10001457 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001458
1459 verdict = NF_DROP;
1460
1461 /* do the statistics and put it back */
1462 ip_vs_in_stats(cp, skb);
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001463 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1464 IPPROTO_SCTP == cih->nexthdr)
Julius Volz2a3b7912008-09-02 15:55:47 +02001465 offset += 2 * sizeof(__u16);
1466 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
Julian Anastasov489fded2010-10-17 16:27:31 +03001467 /* LOCALNODE from FORWARD hook is not supported */
1468 if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD &&
1469 (rt = (struct rt6_info *) skb_dst(skb)) &&
1470 rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) {
1471 IP_VS_DBG(1, "%s(): "
1472 "local delivery to %pI6 but in FORWARD\n",
1473 __func__, &rt->rt6i_dst);
1474 verdict = NF_DROP;
1475 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001476
1477 __ip_vs_conn_put(cp);
1478
1479 return verdict;
1480}
1481#endif
1482
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/*
1485 * Check if it's for virtual services, look it up,
1486 * and send it on its way...
1487 */
1488static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001489ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Hans Schillstromf1313152011-01-03 14:44:55 +01001491 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001492 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001494 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 struct ip_vs_conn *cp;
Julian Anastasovcb591552010-10-17 16:40:51 +03001496 int ret, restart, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001497 struct netns_ipvs *ipvs;
Julius Volz51ef3482008-09-02 15:55:40 +02001498
Julian Anastasovfc604762010-10-17 16:38:15 +03001499 /* Already marked as IPVS request or reply? */
1500 if (skb->ipvs_property)
1501 return NF_ACCEPT;
1502
Julian Anastasovcb591552010-10-17 16:40:51 +03001503 /*
1504 * Big tappo:
1505 * - remote client: only PACKET_HOST
1506 * - route: used for struct net when skb->dev is unset
1507 */
1508 if (unlikely((skb->pkt_type != PACKET_HOST &&
1509 hooknum != NF_INET_LOCAL_OUT) ||
1510 !skb_dst(skb))) {
1511 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1512 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1513 " ignored in hook %u\n",
1514 skb->pkt_type, iph.protocol,
1515 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1516 return NF_ACCEPT;
1517 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001518 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Julian Anastasovcb591552010-10-17 16:40:51 +03001520 /* Bad... Do not break raw sockets */
1521 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1522 af == AF_INET)) {
1523 struct sock *sk = skb->sk;
1524 struct inet_sock *inet = inet_sk(skb->sk);
1525
1526 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1527 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
Julius Volz94b26552009-08-31 16:22:23 +02001530#ifdef CONFIG_IP_VS_IPV6
1531 if (af == AF_INET6) {
1532 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001533 int related;
1534 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Julius Volz94b26552009-08-31 16:22:23 +02001536 if (related)
1537 return verdict;
1538 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1539 }
1540 } else
1541#endif
1542 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001543 int related;
1544 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001545
1546 if (related)
1547 return verdict;
1548 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Hans Schillstrom93304192011-01-03 14:44:51 +01001551 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* Protocol supported? */
Hans Schillstrom93304192011-01-03 14:44:51 +01001553 pd = ip_vs_proto_data_get(net, iph.protocol);
1554 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001556 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /*
1558 * Check if the packet belongs to an existing connection entry
1559 */
Hans Schillstrom93304192011-01-03 14:44:51 +01001560 cp = pp->conn_in_get(af, skb, &iph, iph.len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 if (unlikely(!cp)) {
1563 int v;
1564
Hans Schillstrom93304192011-01-03 14:44:51 +01001565 if (!pp->conn_schedule(af, skb, pd, &v, &cp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return v;
1567 }
1568
1569 if (unlikely(!cp)) {
1570 /* sorry, all this trouble for a no-hit :) */
Julian Anastasov0d796412010-10-17 16:46:17 +03001571 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001572 "ip_vs_in: packet continues traversal as normal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return NF_ACCEPT;
1574 }
1575
Julian Anastasov0d796412010-10-17 16:46:17 +03001576 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
Hans Schillstromf1313152011-01-03 14:44:55 +01001577 net = skb_net(skb);
1578 ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Check the server status */
1580 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1581 /* the destination server is not available */
1582
1583 if (sysctl_ip_vs_expire_nodest_conn) {
1584 /* try to expire the connection immediately */
1585 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001587 /* don't restart its timer, and silently
1588 drop the packet. */
1589 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return NF_DROP;
1591 }
1592
1593 ip_vs_in_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001594 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (cp->packet_xmit)
1596 ret = cp->packet_xmit(skb, cp, pp);
1597 /* do not touch skb anymore */
1598 else {
1599 IP_VS_DBG_RL("warning: packet_xmit is null");
1600 ret = NF_ACCEPT;
1601 }
1602
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001603 /* Increase its packet counter and check if it is needed
1604 * to be synchronized
1605 *
1606 * Sync connection if it is about to close to
1607 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001608 *
1609 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001610 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001611
Hans Schillstrom986a0752010-11-19 14:25:13 +01001612 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1613 pkts = sysctl_ip_vs_sync_threshold[0];
1614 else
1615 pkts = atomic_add_return(1, &cp->in_pkts);
1616
Hans Schillstromf1313152011-01-03 14:44:55 +01001617 if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001618 cp->protocol == IPPROTO_SCTP) {
1619 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
Julian Anastasov8ed21632010-09-01 22:19:14 +00001620 (pkts % sysctl_ip_vs_sync_threshold[1]
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001621 == sysctl_ip_vs_sync_threshold[0])) ||
1622 (cp->old_state != cp->state &&
1623 ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1624 (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1625 (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
Hans Schillstromf1313152011-01-03 14:44:55 +01001626 ip_vs_sync_conn(net, cp);
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001627 goto out;
1628 }
1629 }
1630
Julian Anastasov8ed21632010-09-01 22:19:14 +00001631 /* Keep this block last: TCP and others with pp->num_states <= 1 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001632 else if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001633 (((cp->protocol != IPPROTO_TCP ||
1634 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
Simon Horman1e66daf2009-08-31 14:18:48 +02001635 (pkts % sysctl_ip_vs_sync_threshold[1]
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001636 == sysctl_ip_vs_sync_threshold[0])) ||
1637 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1638 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
Xiaotian Feng9abfe312009-12-14 16:38:21 +01001639 (cp->state == IP_VS_TCP_S_CLOSE) ||
Rumen G. Bogdanovski9d3a0de2008-07-16 20:04:23 -07001640 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1641 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
Hans Schillstromf1313152011-01-03 14:44:55 +01001642 ip_vs_sync_conn(net, cp);
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001643out:
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001644 cp->old_state = cp->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 ip_vs_conn_put(cp);
1647 return ret;
1648}
1649
Julian Anastasovcb591552010-10-17 16:40:51 +03001650/*
1651 * AF_INET handler in NF_INET_LOCAL_IN chain
1652 * Schedule and forward packets from remote clients
1653 */
1654static unsigned int
1655ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1656 const struct net_device *in,
1657 const struct net_device *out,
1658 int (*okfn)(struct sk_buff *))
1659{
1660 return ip_vs_in(hooknum, skb, AF_INET);
1661}
1662
1663/*
1664 * AF_INET handler in NF_INET_LOCAL_OUT chain
1665 * Schedule and forward packets from local clients
1666 */
1667static unsigned int
1668ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1669 const struct net_device *in, const struct net_device *out,
1670 int (*okfn)(struct sk_buff *))
1671{
1672 unsigned int verdict;
1673
1674 /* Disable BH in LOCAL_OUT until all places are fixed */
1675 local_bh_disable();
1676 verdict = ip_vs_in(hooknum, skb, AF_INET);
1677 local_bh_enable();
1678 return verdict;
1679}
1680
1681#ifdef CONFIG_IP_VS_IPV6
1682
1683/*
1684 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1685 * Schedule and forward packets from remote clients
1686 */
1687static unsigned int
1688ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1689 const struct net_device *in,
1690 const struct net_device *out,
1691 int (*okfn)(struct sk_buff *))
1692{
1693 return ip_vs_in(hooknum, skb, AF_INET6);
1694}
1695
1696/*
1697 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1698 * Schedule and forward packets from local clients
1699 */
1700static unsigned int
1701ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1702 const struct net_device *in, const struct net_device *out,
1703 int (*okfn)(struct sk_buff *))
1704{
1705 unsigned int verdict;
1706
1707 /* Disable BH in LOCAL_OUT until all places are fixed */
1708 local_bh_disable();
1709 verdict = ip_vs_in(hooknum, skb, AF_INET6);
1710 local_bh_enable();
1711 return verdict;
1712}
1713
1714#endif
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001718 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 * related packets destined for 0.0.0.0/0.
1720 * When fwmark-based virtual service is used, such as transparent
1721 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1722 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001723 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * and send them to ip_vs_in_icmp.
1725 */
1726static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001727ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 const struct net_device *in, const struct net_device *out,
1729 int (*okfn)(struct sk_buff *))
1730{
1731 int r;
1732
Herbert Xu3db05fe2007-10-15 00:53:15 -07001733 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return NF_ACCEPT;
1735
Herbert Xu3db05fe2007-10-15 00:53:15 -07001736 return ip_vs_in_icmp(skb, &r, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Julius Volz2a3b7912008-09-02 15:55:47 +02001739#ifdef CONFIG_IP_VS_IPV6
1740static unsigned int
1741ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1742 const struct net_device *in, const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1744{
1745 int r;
1746
1747 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1748 return NF_ACCEPT;
1749
1750 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1751}
1752#endif
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Patrick McHardy19994142007-12-05 01:23:00 -08001755static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001756 /* After packet filtering, change source only for VS/NAT */
1757 {
1758 .hook = ip_vs_reply4,
1759 .owner = THIS_MODULE,
1760 .pf = PF_INET,
1761 .hooknum = NF_INET_LOCAL_IN,
1762 .priority = 99,
1763 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001764 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1765 * or VS/NAT(change destination), so that filtering rules can be
1766 * applied to IPVS. */
1767 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001768 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001769 .owner = THIS_MODULE,
1770 .pf = PF_INET,
Julian Anastasovcb591552010-10-17 16:40:51 +03001771 .hooknum = NF_INET_LOCAL_IN,
1772 .priority = 101,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001773 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001774 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001775 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001776 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001777 .owner = THIS_MODULE,
1778 .pf = PF_INET,
Julian Anastasovfc604762010-10-17 16:38:15 +03001779 .hooknum = NF_INET_LOCAL_OUT,
1780 .priority = -99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001781 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001782 /* After mangle, schedule and forward local requests */
1783 {
1784 .hook = ip_vs_local_request4,
1785 .owner = THIS_MODULE,
1786 .pf = PF_INET,
1787 .hooknum = NF_INET_LOCAL_OUT,
1788 .priority = -98,
1789 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001790 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1791 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1792 {
1793 .hook = ip_vs_forward_icmp,
1794 .owner = THIS_MODULE,
1795 .pf = PF_INET,
Julian Anastasovcb591552010-10-17 16:40:51 +03001796 .hooknum = NF_INET_FORWARD,
1797 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001798 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001799 /* After packet filtering, change source only for VS/NAT */
1800 {
1801 .hook = ip_vs_reply4,
1802 .owner = THIS_MODULE,
1803 .pf = PF_INET,
1804 .hooknum = NF_INET_FORWARD,
1805 .priority = 100,
1806 },
Julius Volz473b23d2008-09-02 15:55:54 +02001807#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001808 /* After packet filtering, change source only for VS/NAT */
1809 {
1810 .hook = ip_vs_reply6,
1811 .owner = THIS_MODULE,
1812 .pf = PF_INET6,
1813 .hooknum = NF_INET_LOCAL_IN,
1814 .priority = 99,
1815 },
Julius Volz473b23d2008-09-02 15:55:54 +02001816 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1817 * or VS/NAT(change destination), so that filtering rules can be
1818 * applied to IPVS. */
1819 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001820 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001821 .owner = THIS_MODULE,
1822 .pf = PF_INET6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001823 .hooknum = NF_INET_LOCAL_IN,
1824 .priority = 101,
Julius Volz473b23d2008-09-02 15:55:54 +02001825 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001826 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001827 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001828 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001829 .owner = THIS_MODULE,
Julian Anastasovfc604762010-10-17 16:38:15 +03001830 .pf = PF_INET,
1831 .hooknum = NF_INET_LOCAL_OUT,
1832 .priority = -99,
Julius Volz473b23d2008-09-02 15:55:54 +02001833 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001834 /* After mangle, schedule and forward local requests */
1835 {
1836 .hook = ip_vs_local_request6,
1837 .owner = THIS_MODULE,
1838 .pf = PF_INET6,
1839 .hooknum = NF_INET_LOCAL_OUT,
1840 .priority = -98,
1841 },
Julius Volz473b23d2008-09-02 15:55:54 +02001842 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1843 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1844 {
1845 .hook = ip_vs_forward_icmp_v6,
1846 .owner = THIS_MODULE,
1847 .pf = PF_INET6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001848 .hooknum = NF_INET_FORWARD,
1849 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02001850 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001851 /* After packet filtering, change source only for VS/NAT */
1852 {
1853 .hook = ip_vs_reply6,
1854 .owner = THIS_MODULE,
1855 .pf = PF_INET6,
1856 .hooknum = NF_INET_FORWARD,
1857 .priority = 100,
1858 },
Julius Volz473b23d2008-09-02 15:55:54 +02001859#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001861/*
1862 * Initialize IP Virtual Server netns mem.
1863 */
1864static int __net_init __ip_vs_init(struct net *net)
1865{
1866 struct netns_ipvs *ipvs;
1867
1868 if (!net_eq(net, &init_net)) {
1869 pr_err("The final patch for enabling netns is missing\n");
1870 return -EPERM;
1871 }
1872 ipvs = net_generic(net, ip_vs_net_id);
1873 if (ipvs == NULL) {
1874 pr_err("%s(): no memory.\n", __func__);
1875 return -ENOMEM;
1876 }
1877 /* Counters used for creating unique names */
1878 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1879 atomic_inc(&ipvs_netns_cnt);
1880 net->ipvs = ipvs;
1881 printk(KERN_INFO "IPVS: Creating netns size=%lu id=%d\n",
1882 sizeof(struct netns_ipvs), ipvs->gen);
1883 return 0;
1884}
1885
1886static void __net_exit __ip_vs_cleanup(struct net *net)
1887{
1888 struct netns_ipvs *ipvs = net_ipvs(net);
1889
1890 IP_VS_DBG(10, "ipvs netns %d released\n", ipvs->gen);
1891}
1892
1893static struct pernet_operations ipvs_core_ops = {
1894 .init = __ip_vs_init,
1895 .exit = __ip_vs_cleanup,
1896 .id = &ip_vs_net_id,
1897 .size = sizeof(struct netns_ipvs),
1898};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900/*
1901 * Initialize IP Virtual Server
1902 */
1903static int __init ip_vs_init(void)
1904{
1905 int ret;
1906
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001907 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
1908 if (ret < 0)
1909 return ret;
Sven Wegenera919cf42008-08-14 00:47:16 +02001910
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001911 ip_vs_estimator_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 ret = ip_vs_control_init();
1913 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001914 pr_err("can't setup control.\n");
Sven Wegenera919cf42008-08-14 00:47:16 +02001915 goto cleanup_estimator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
1918 ip_vs_protocol_init();
1919
1920 ret = ip_vs_app_init();
1921 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001922 pr_err("can't setup application helper.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto cleanup_protocol;
1924 }
1925
1926 ret = ip_vs_conn_init();
1927 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001928 pr_err("can't setup connection table.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 goto cleanup_app;
1930 }
1931
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001932 ret = ip_vs_sync_init();
1933 if (ret < 0) {
1934 pr_err("can't setup sync data.\n");
1935 goto cleanup_conn;
1936 }
1937
Patrick McHardy41c5b312007-12-05 01:22:43 -08001938 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00001940 pr_err("can't register hooks.\n");
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001941 goto cleanup_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943
Hannes Eder1e3e2382009-08-02 11:05:41 +00001944 pr_info("ipvs loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return ret;
1946
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001947cleanup_sync:
1948 ip_vs_sync_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 cleanup_conn:
1950 ip_vs_conn_cleanup();
1951 cleanup_app:
1952 ip_vs_app_cleanup();
1953 cleanup_protocol:
1954 ip_vs_protocol_cleanup();
1955 ip_vs_control_cleanup();
Sven Wegenera919cf42008-08-14 00:47:16 +02001956 cleanup_estimator:
1957 ip_vs_estimator_cleanup();
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001958 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return ret;
1960}
1961
1962static void __exit ip_vs_cleanup(void)
1963{
Patrick McHardy41c5b312007-12-05 01:22:43 -08001964 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001965 ip_vs_sync_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 ip_vs_conn_cleanup();
1967 ip_vs_app_cleanup();
1968 ip_vs_protocol_cleanup();
1969 ip_vs_control_cleanup();
Sven Wegenera919cf42008-08-14 00:47:16 +02001970 ip_vs_estimator_cleanup();
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001971 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Hannes Eder1e3e2382009-08-02 11:05:41 +00001972 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975module_init(ip_vs_init);
1976module_exit(ip_vs_cleanup);
1977MODULE_LICENSE("GPL");