blob: b5f451f20f522dfd7cbe7d2f98cc2669b1743df9 [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
Julian Anastasovb962abd2013-03-09 23:25:08 +020072static int ip_vs_net_id __read_mostly;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010073/* netns cnt used for uniqueness */
74static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* ID used in ICMP lookups */
77#define icmp_id(icmph) (((icmph)->un).echo.id)
Julius Volz2a3b7912008-09-02 15:55:47 +020078#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Eric Dumazet95c96172012-04-15 05:58:06 +000080const char *ip_vs_proto_name(unsigned int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 static char buf[20];
83
84 switch (proto) {
85 case IPPROTO_IP:
86 return "IP";
87 case IPPROTO_UDP:
88 return "UDP";
89 case IPPROTO_TCP:
90 return "TCP";
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010091 case IPPROTO_SCTP:
92 return "SCTP";
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case IPPROTO_ICMP:
94 return "ICMP";
Julius Volz2a3b7912008-09-02 15:55:47 +020095#ifdef CONFIG_IP_VS_IPV6
96 case IPPROTO_ICMPV6:
97 return "ICMPv6";
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 default:
Masanari Iida1404c3a2014-04-21 09:16:47 +0900100 sprintf(buf, "IP_%u", proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return buf;
102 }
103}
104
105void ip_vs_init_hash_table(struct list_head *table, int rows)
106{
107 while (--rows >= 0)
108 INIT_LIST_HEAD(&table[rows]);
109}
110
111static inline void
112ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
113{
114 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100115 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100118 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300119 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hans Schillstromb17fc992011-01-03 14:44:56 +0100121 s = this_cpu_ptr(dest->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100122 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200123 s->cnt.inpkts++;
124 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100125 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300127 rcu_read_lock();
128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100130 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200131 s->cnt.inpkts++;
132 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100133 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300134 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100135
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100137 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200138 s->cnt.inpkts++;
139 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100140 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142}
143
144
145static inline void
146ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147{
148 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100149 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100152 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300153 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Hans Schillstromb17fc992011-01-03 14:44:56 +0100155 s = this_cpu_ptr(dest->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100156 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200157 s->cnt.outpkts++;
158 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100159 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300161 rcu_read_lock();
162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100164 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200165 s->cnt.outpkts++;
166 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100167 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300168 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100169
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100171 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200172 s->cnt.outpkts++;
173 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100174 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176}
177
178
179static inline void
180ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
181{
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500182 struct netns_ipvs *ipvs = svc->ipvs;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100183 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hans Schillstromb17fc992011-01-03 14:44:56 +0100185 s = this_cpu_ptr(cp->dest->stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200186 u64_stats_update_begin(&s->syncp);
187 s->cnt.conns++;
188 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Hans Schillstromb17fc992011-01-03 14:44:56 +0100190 s = this_cpu_ptr(svc->stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200191 u64_stats_update_begin(&s->syncp);
192 s->cnt.conns++;
193 u64_stats_update_end(&s->syncp);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100194
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200195 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200196 u64_stats_update_begin(&s->syncp);
197 s->cnt.conns++;
198 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201
Simon Horman4a516f12011-09-16 14:11:49 +0900202static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203ip_vs_set_state(struct ip_vs_conn *cp, int direction,
204 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100205 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Simon Horman4a516f12011-09-16 14:11:49 +0900207 if (likely(pd->pp->state_transition))
208 pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Hans Schillstroma5959d52010-11-19 14:25:10 +0100211static inline int
Simon Horman85999282010-08-22 21:37:53 +0900212ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
213 struct sk_buff *skb, int protocol,
214 const union nf_inet_addr *caddr, __be16 cport,
215 const union nf_inet_addr *vaddr, __be16 vport,
216 struct ip_vs_conn_param *p)
217{
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500218 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100219 vport, p);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200220 p->pe = rcu_dereference(svc->pe);
Simon Horman85999282010-08-22 21:37:53 +0900221 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100222 return p->pe->fill_param(p, skb);
223
224 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
228 * IPVS persistent scheduling function
229 * It creates a connection entry according to its template if exists,
230 * or selects a server and creates a connection entry plus a template.
231 * Locking: we are svc user (svc->refcnt), so we hold all dests too
232 * Protocols supported: TCP, UDP
233 */
234static struct ip_vs_conn *
235ip_vs_sched_persist(struct ip_vs_service *svc,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200236 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
237 int *ignored, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct ip_vs_conn *cp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct ip_vs_dest *dest;
241 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900242 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200243 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900244 struct ip_vs_conn_param param;
Simon Hormane0aac522012-01-27 10:45:27 +0900245 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
Julius Volz28364a52008-09-02 15:55:43 +0200246 union nf_inet_addr snet; /* source network of the client,
247 after masking */
Alex Gartrellee78378f2015-08-26 09:40:33 -0700248 const union nf_inet_addr *src_addr, *dst_addr;
249
250 if (likely(!ip_vs_iph_inverse(iph))) {
251 src_addr = &iph->saddr;
252 dst_addr = &iph->daddr;
253 } else {
254 src_addr = &iph->daddr;
255 dst_addr = &iph->saddr;
256 }
257
Julius Volzcd17f9e2008-09-02 15:55:46 +0200258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200260#ifdef CONFIG_IP_VS_IPV6
261 if (svc->af == AF_INET6)
Alex Gartrellee78378f2015-08-26 09:40:33 -0700262 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
Julian Anastasov0a925862013-04-17 23:50:49 +0300263 (__force __u32) svc->netmask);
Julius Volzcd17f9e2008-09-02 15:55:46 +0200264 else
265#endif
Alex Gartrellee78378f2015-08-26 09:40:33 -0700266 snet.ip = src_addr->ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Julius Volzcd17f9e2008-09-02 15:55:46 +0200268 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
269 "mnet %s\n",
Alex Gartrellee78378f2015-08-26 09:40:33 -0700270 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
271 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200272 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /*
275 * As far as we know, FTP is a very complicated network protocol, and
276 * it uses control connection and data connections. For active FTP,
277 * FTP server initialize data connection to the client, its source port
278 * is often 20. For passive FTP, FTP server tells the clients the port
279 * that it passively listens to, and the client issues the data
280 * connection. In the tunneling or direct routing mode, the load
281 * balancer is on the client-to-server half of connection, the port
282 * number is unknown to the load balancer. So, a conn template like
283 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
284 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
285 * is created for other persistent services.
286 */
Simon Horman5b57a982010-08-22 21:37:51 +0900287 {
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200288 int protocol = iph->protocol;
Alex Gartrellee78378f2015-08-26 09:40:33 -0700289 const union nf_inet_addr *vaddr = dst_addr;
Simon Hormanf11017e2010-08-22 21:37:52 +0900290 __be16 vport = 0;
291
Hans Schillstromce144f22010-11-19 14:25:08 +0100292 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900293 /* non-FTP template:
294 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
295 * FTP template:
296 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100299 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900301 /* Note: persistent fwmark-based services and
302 * persistent port zero service are handled here.
303 * fwmark template:
304 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
305 * port zero template:
306 * <protocol,caddr,0,vaddr,0,daddr,0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Julius Volz28364a52008-09-02 15:55:43 +0200308 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900309 protocol = IPPROTO_IP;
310 vaddr = &fwmark;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100313 /* return *ignored = -1 so NF_DROP can be used */
314 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
315 vaddr, vport, &param) < 0) {
316 *ignored = -1;
317 return NULL;
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Simon Horman5b57a982010-08-22 21:37:51 +0900321 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900322 ct = ip_vs_ct_in_get(&param);
Simon Horman5b57a982010-08-22 21:37:51 +0900323 if (!ct || !ip_vs_check_template(ct)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200324 struct ip_vs_scheduler *sched;
325
Hans Schillstroma5959d52010-11-19 14:25:10 +0100326 /*
327 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900328 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100329 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900330 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200331 sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +0300332 if (sched) {
333 /* read svc->sched_data after svc->scheduler */
334 smp_rmb();
335 dest = sched->schedule(svc, skb, iph);
336 } else {
337 dest = NULL;
338 }
Simon Horman5b57a982010-08-22 21:37:51 +0900339 if (!dest) {
340 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900341 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100342 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900343 return NULL;
344 }
345
Hans Schillstromce144f22010-11-19 14:25:08 +0100346 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900347 dport = dest->port;
348
Simon Horman85999282010-08-22 21:37:53 +0900349 /* Create a template
350 * This adds param.pe_data to the template,
351 * and thus param.pe_data will be destroyed
352 * when the template expires */
Alex Gartrellba385282014-09-09 16:40:23 -0700353 ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100354 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900355 if (ct == NULL) {
356 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100357 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900358 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900359 }
Simon Horman5b57a982010-08-22 21:37:51 +0900360
361 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900362 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900363 /* set destination with the found template */
364 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900365 kfree(param.pe_data);
366 }
Simon Horman5b57a982010-08-22 21:37:51 +0900367
Hans Schillstromce144f22010-11-19 14:25:08 +0100368 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900369 if (dport == svc->port && dest->port)
370 dport = dest->port;
371
Nick Chalk26ec0372010-06-22 08:07:01 +0200372 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200373 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200374 IP_VS_CONN_F_ONE_PACKET : 0;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /*
377 * Create a new connection according to the template
378 */
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500379 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
Alex Gartrellee78378f2015-08-26 09:40:33 -0700380 src_port, dst_addr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100381
Alex Gartrellba385282014-09-09 16:40:23 -0700382 cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
383 skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (cp == NULL) {
385 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100386 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return NULL;
388 }
389
390 /*
391 * Add its control
392 */
393 ip_vs_control_add(cp, ct);
394 ip_vs_conn_put(ct);
395
396 ip_vs_conn_stats(cp, svc);
397 return cp;
398}
399
400
401/*
402 * IPVS main scheduling function
403 * It selects a server according to the virtual service, and
404 * creates a connection entry.
405 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100406 *
407 * Usage of *ignored
408 *
409 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
410 * svc/scheduler decides that this packet should be accepted with
411 * NF_ACCEPT because it must not be scheduled.
412 *
413 * 0 : scheduler can not find destination, so try bypass or
414 * return ICMP and then NF_DROP (ip_vs_leave).
415 *
416 * -1 : scheduler tried to schedule but fatal error occurred, eg.
417 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
418 * failure such as missing Call-ID, ENOMEM on skb_linearize
419 * or pe_data. In this case we should return NF_DROP without
420 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
422struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300423ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200424 struct ip_vs_proto_data *pd, int *ignored,
425 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Hans Schillstrom93304192011-01-03 14:44:51 +0100427 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct ip_vs_conn *cp = NULL;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200429 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct ip_vs_dest *dest;
Alex Gartrellee78378f2015-08-26 09:40:33 -0700431 __be16 _ports[2], *pptr, cport, vport;
432 const void *caddr, *vaddr;
Julian Anastasov35757922010-09-17 14:18:16 +0200433 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Julian Anastasov190ecd22010-10-17 16:24:37 +0300435 *ignored = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200436 /*
437 * IPv6 frags, only the first hit here.
438 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200439 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (pptr == NULL)
441 return NULL;
442
Alex Gartrellee78378f2015-08-26 09:40:33 -0700443 if (likely(!ip_vs_iph_inverse(iph))) {
444 cport = pptr[0];
445 caddr = &iph->saddr;
446 vport = pptr[1];
447 vaddr = &iph->daddr;
448 } else {
449 cport = pptr[1];
450 caddr = &iph->daddr;
451 vport = pptr[0];
452 vaddr = &iph->saddr;
453 }
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300456 * FTPDATA needs this check when using local real server.
457 * Never schedule Active FTPDATA connections from real server.
458 * For LVS-NAT they must be already created. For other methods
459 * with persistence the connection is created on SYN+ACK.
460 */
Alex Gartrellee78378f2015-08-26 09:40:33 -0700461 if (cport == FTPDATA) {
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700462 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
Julian Anastasov0d796412010-10-17 16:46:17 +0300463 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300464 return NULL;
465 }
466
467 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100468 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300469 */
Alex Gartrell802c41a2015-08-26 09:40:32 -0700470 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
471 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
472 cp = pp->conn_in_get(svc->af, skb, iph);
473 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
474
475 if (cp) {
476 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
477 "Not scheduling reply for existing"
478 " connection");
479 __ip_vs_conn_put(cp);
480 return NULL;
481 }
Julian Anastasov190ecd22010-10-17 16:24:37 +0300482 }
483
484 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * Persistent service
486 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100487 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
Alex Gartrellee78378f2015-08-26 09:40:33 -0700488 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200489 iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100490
491 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Non-persistent service
495 */
Alex Gartrellee78378f2015-08-26 09:40:33 -0700496 if (!svc->fwmark && vport != svc->port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000498 pr_err("Schedule: port zero only supported "
499 "in persistent services, "
500 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return NULL;
502 }
503
Julian Anastasovceec4c32013-03-22 11:46:53 +0200504 sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +0300505 if (sched) {
506 /* read svc->sched_data after svc->scheduler */
507 smp_rmb();
508 dest = sched->schedule(svc, skb, iph);
509 } else {
510 dest = NULL;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (dest == NULL) {
513 IP_VS_DBG(1, "Schedule: no dest found.\n");
514 return NULL;
515 }
516
Nick Chalk26ec0372010-06-22 08:07:01 +0200517 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200518 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200519 IP_VS_CONN_F_ONE_PACKET : 0;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * Create a connection entry.
523 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900524 {
525 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100526
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500527 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
Alex Gartrellee78378f2015-08-26 09:40:33 -0700528 caddr, cport, vaddr, vport, &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700529 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
Alex Gartrellee78378f2015-08-26 09:40:33 -0700530 dest->port ? dest->port : vport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100531 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100532 if (!cp) {
533 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900534 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100535 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Julius Volzcd17f9e2008-09-02 15:55:46 +0200538 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
539 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
540 ip_vs_fwd_tag(cp),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700541 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
542 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
543 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200544 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 ip_vs_conn_stats(cp, svc);
547 return cp;
548}
549
Alex Gartrell0b729022015-08-26 09:40:30 -0700550#ifdef CONFIG_SYSCTL
551static inline int ip_vs_addr_is_unicast(struct net *net, int af,
552 union nf_inet_addr *addr)
553{
554#ifdef CONFIG_IP_VS_IPV6
555 if (af == AF_INET6)
556 return ipv6_addr_type(&addr->in6) & IPV6_ADDR_UNICAST;
557#endif
558 return (inet_addr_type(net, addr->ip) == RTN_UNICAST);
559}
560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562/*
563 * Pass or drop the packet.
564 * Called by ip_vs_in, when the virtual service is available but
565 * no destination is available for a new connection.
566 */
567int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200568 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Alex Gartrell0b729022015-08-26 09:40:30 -0700570 __be16 _ports[2], *pptr, dport;
Simon Hormana7a86b82011-02-04 18:33:02 +0900571#ifdef CONFIG_SYSCTL
572 struct net *net;
573 struct netns_ipvs *ipvs;
Simon Hormana7a86b82011-02-04 18:33:02 +0900574#endif
Hans Schillstrom93304192011-01-03 14:44:51 +0100575
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200576 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Alex Gartrell0b729022015-08-26 09:40:30 -0700577 if (!pptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NF_DROP;
Alex Gartrell0b729022015-08-26 09:40:30 -0700579 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
Simon Hormana7a86b82011-02-04 18:33:02 +0900580
581#ifdef CONFIG_SYSCTL
Hans Schillstrom4a984802011-01-03 14:45:02 +0100582 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Julius Volz2a3b7912008-09-02 15:55:47 +0200584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200586 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 a cache_bypass connection entry */
Hans Schillstrom4a984802011-01-03 14:45:02 +0100588 ipvs = net_ipvs(net);
Alex Gartrell0b729022015-08-26 09:40:30 -0700589 if (ipvs->sysctl_cache_bypass && svc->fwmark &&
590 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
591 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100592 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200594 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200595 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200596 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000597 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000600 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900601 {
602 struct ip_vs_conn_param p;
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500603 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200604 &iph->saddr, pptr[0],
605 &iph->daddr, pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700606 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
Simon Hormanf11017e2010-08-22 21:37:52 +0900607 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100608 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900609 if (!cp)
610 return NF_DROP;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* statistics */
614 ip_vs_in_stats(cp, skb);
615
616 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900617 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200620 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* do not touch skb anymore */
622
623 atomic_inc(&cp->in_pkts);
624 ip_vs_conn_put(cp);
625 return ret;
626 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900627#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /*
630 * When the virtual ftp service is presented, packets destined
631 * for other services on the VIP may get here (except services
632 * listed in the ipvs table), pass the packets, because it is
633 * not ipvs job to decide to drop the packets.
634 */
Alex Gartrell0b729022015-08-26 09:40:30 -0700635 if (svc->port == FTPPORT && dport != FTPPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Alex Gartrell0b729022015-08-26 09:40:30 -0700638 if (unlikely(ip_vs_iph_icmp(iph)))
639 return NF_DROP;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Notify the client that the destination is unreachable, and
643 * release the socket buffer.
644 * Since it is in IP layer, the TCP socket is not actually
645 * created, the TCP RST packet cannot be sent, instead that
646 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
647 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200648#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300649 if (svc->af == AF_INET6) {
650 if (!skb->dev) {
Simon Horman9fd0fa72013-04-19 10:25:42 +0900651 struct net *net_ = dev_net(skb_dst(skb)->dev);
Julian Anastasovcb591552010-10-17 16:40:51 +0300652
Simon Horman9fd0fa72013-04-19 10:25:42 +0900653 skb->dev = net_->loopback_dev;
Julian Anastasovcb591552010-10-17 16:40:51 +0300654 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000655 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300656 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200657#endif
658 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return NF_DROP;
661}
662
Simon Horman84b3cee2011-02-04 18:33:01 +0900663#ifdef CONFIG_SYSCTL
664
665static int sysctl_snat_reroute(struct sk_buff *skb)
666{
667 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
668 return ipvs->sysctl_snat_reroute;
669}
670
Simon Horman0cfa5582011-02-04 18:33:01 +0900671static int sysctl_nat_icmp_send(struct net *net)
672{
673 struct netns_ipvs *ipvs = net_ipvs(net);
674 return ipvs->sysctl_nat_icmp_send;
675}
676
Simon Horman71a8ab62011-02-04 18:33:01 +0900677static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
678{
679 return ipvs->sysctl_expire_nodest_conn;
680}
681
Simon Horman84b3cee2011-02-04 18:33:01 +0900682#else
683
684static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
Simon Horman0cfa5582011-02-04 18:33:01 +0900685static int sysctl_nat_icmp_send(struct net *net) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900686static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900687
688#endif
689
Al Virob1550f22006-11-14 21:37:50 -0800690__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Al Virod3bc23e2006-11-14 21:24:49 -0800692 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300695static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
696{
697 if (NF_INET_LOCAL_IN == hooknum)
698 return IP_DEFRAG_VS_IN;
699 if (NF_INET_FORWARD == hooknum)
700 return IP_DEFRAG_VS_FWD;
701 return IP_DEFRAG_VS_OUT;
702}
703
Herbert Xu776c7292007-10-14 00:38:32 -0700704static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Julian Anastasovac692692013-03-22 11:46:54 +0200706 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700707
Julian Anastasovac692692013-03-22 11:46:54 +0200708 local_bh_disable();
709 err = ip_defrag(skb, user);
710 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700711 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700712 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700713
714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Julian Anastasov579eb622014-12-18 22:41:23 +0200717static int ip_vs_route_me_harder(int af, struct sk_buff *skb,
718 unsigned int hooknum)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900719{
Julian Anastasov579eb622014-12-18 22:41:23 +0200720 if (!sysctl_snat_reroute(skb))
721 return 0;
722 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
723 if (NF_INET_LOCAL_IN == hooknum)
724 return 0;
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900725#ifdef CONFIG_IP_VS_IPV6
726 if (af == AF_INET6) {
Julian Anastasov579eb622014-12-18 22:41:23 +0200727 struct dst_entry *dst = skb_dst(skb);
728
729 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
730 ip6_route_me_harder(skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900731 return 1;
732 } else
733#endif
Julian Anastasov579eb622014-12-18 22:41:23 +0200734 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900735 ip_route_me_harder(skb, RTN_LOCAL) != 0)
736 return 1;
737
738 return 0;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
742 * Packet has been made sufficiently writable in caller
743 * - inout: 1=in->out, 0=out->in
744 */
745void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
746 struct ip_vs_conn *cp, int inout)
747{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700748 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700750 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
751 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
753
754 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200755 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200757 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ip_send_check(ciph);
759 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200760 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200762 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ip_send_check(ciph);
764 }
765
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100766 /* the TCP/UDP/SCTP port */
767 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
768 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700769 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (inout)
772 ports[1] = cp->vport;
773 else
774 ports[0] = cp->dport;
775 }
776
777 /* And finally the ICMP checksum */
778 icmph->checksum = 0;
779 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
780 skb->ip_summed = CHECKSUM_UNNECESSARY;
781
782 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300783 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 "Forwarding altered outgoing ICMP");
785 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300786 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 "Forwarding altered incoming ICMP");
788}
789
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200790#ifdef CONFIG_IP_VS_IPV6
791void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
792 struct ip_vs_conn *cp, int inout)
793{
794 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200795 unsigned int icmp_offset = 0;
796 unsigned int offs = 0; /* header offset*/
797 int protocol;
798 struct icmp6hdr *icmph;
799 struct ipv6hdr *ciph;
800 unsigned short fragoffs;
801
802 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
803 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
804 offs = icmp_offset + sizeof(struct icmp6hdr);
805 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
806
807 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200808
809 if (inout) {
810 iph->saddr = cp->vaddr.in6;
811 ciph->daddr = cp->vaddr.in6;
812 } else {
813 iph->daddr = cp->daddr.in6;
814 ciph->saddr = cp->daddr.in6;
815 }
816
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100817 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200818 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
819 IPPROTO_SCTP == protocol)) {
820 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200821
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200822 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
823 ntohs(inout ? ports[1] : ports[0]),
824 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200825 if (inout)
826 ports[1] = cp->vport;
827 else
828 ports[0] = cp->dport;
829 }
830
831 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700832 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
833 skb->len - icmp_offset,
834 IPPROTO_ICMPV6, 0);
835 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
836 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
837 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200838
839 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300840 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
841 (void *)ciph - (void *)iph,
842 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200843 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300844 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
845 (void *)ciph - (void *)iph,
846 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200847}
848#endif
849
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000850/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200851 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000852 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000853static int handle_response_icmp(int af, struct sk_buff *skb,
854 union nf_inet_addr *snet,
855 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000856 struct ip_vs_protocol *pp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200857 unsigned int offset, unsigned int ihl,
858 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000859{
860 unsigned int verdict = NF_DROP;
861
862 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000863 pr_err("shouldn't reach here, because the box is on the "
864 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000865 }
866
867 /* Ensure the checksum is correct */
868 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
869 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000870 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
871 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000872 goto out;
873 }
874
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100875 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
876 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000877 offset += 2 * sizeof(__u16);
878 if (!skb_make_writable(skb, offset))
879 goto out;
880
Simon Hormanf2428ed2008-09-05 11:17:14 +1000881#ifdef CONFIG_IP_VS_IPV6
882 if (af == AF_INET6)
883 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
884 else
885#endif
886 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000887
Julian Anastasov579eb622014-12-18 22:41:23 +0200888 if (ip_vs_route_me_harder(af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900889 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300890
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000891 /* do the statistics and put it back */
892 ip_vs_out_stats(cp, skb);
893
Julian Anastasovcf356d62010-10-17 16:21:07 +0300894 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200895 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300896 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200897 else
898 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000899 verdict = NF_ACCEPT;
900
901out:
902 __ip_vs_conn_put(cp);
903
904 return verdict;
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907/*
908 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000909 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300912static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
913 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct iphdr *iph;
916 struct icmphdr _icmph, *ic;
917 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200918 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct ip_vs_conn *cp;
920 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000921 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000922 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 *related = 1;
925
926 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700927 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300928 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700932 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 offset = ihl = iph->ihl * 4;
934 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
935 if (ic == NULL)
936 return NF_DROP;
937
Harvey Harrison14d5e832008-10-31 00:54:29 -0700938 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700940 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /*
943 * Work through seeing if this is for us.
944 * These checks are supposed to be in an order that means easy
945 * things are checked first to speed up processing.... however
946 * this means that some packets will manage to get a long way
947 * down this stack and then be rejected, but that's life.
948 */
949 if ((ic->type != ICMP_DEST_UNREACH) &&
950 (ic->type != ICMP_SOURCE_QUENCH) &&
951 (ic->type != ICMP_TIME_EXCEEDED)) {
952 *related = 0;
953 return NF_ACCEPT;
954 }
955
956 /* Now find the contained IP header */
957 offset += sizeof(_icmph);
958 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
959 if (cih == NULL)
960 return NF_ACCEPT; /* The packet looks wrong, ignore */
961
962 pp = ip_vs_proto_get(cih->protocol);
963 if (!pp)
964 return NF_ACCEPT;
965
966 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900967 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 pp->dont_defrag))
969 return NF_ACCEPT;
970
Julian Anastasov0d796412010-10-17 16:46:17 +0300971 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
972 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Alex Gartrell4fd9bee2015-08-26 09:40:29 -0700974 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* The embedded headers contain source and dest in reverse order */
Alex Gartrell802c41a2015-08-26 09:40:32 -0700977 cp = pp->conn_out_get(AF_INET, skb, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!cp)
979 return NF_ACCEPT;
980
Simon Hormanf2428ed2008-09-05 11:17:14 +1000981 snet.ip = iph->saddr;
982 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200983 pp, ciph.len, ihl, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Julius Volz2a3b7912008-09-02 15:55:47 +0200986#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300987static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200988 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200989{
Julius Volz2a3b7912008-09-02 15:55:47 +0200990 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200991 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200992 struct ip_vs_conn *cp;
993 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000994 union nf_inet_addr snet;
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700995 unsigned int offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200996
Julius Volz2a3b7912008-09-02 15:55:47 +0200997 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200998 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200999 if (ic == NULL)
1000 return NF_DROP;
1001
Julius Volz2a3b7912008-09-02 15:55:47 +02001002 /*
1003 * Work through seeing if this is for us.
1004 * These checks are supposed to be in an order that means easy
1005 * things are checked first to speed up processing.... however
1006 * this means that some packets will manage to get a long way
1007 * down this stack and then be rejected, but that's life.
1008 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001009 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001010 *related = 0;
1011 return NF_ACCEPT;
1012 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001013 /* Fragment header that is before ICMP header tells us that:
1014 * it's not an error message since they can't be fragmented.
1015 */
David S. Millere7165032012-11-30 12:01:30 -05001016 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001017 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001018
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001019 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1020 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1021 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001022
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001023 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1024 true, &ciph))
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001025 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001026
1027 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +02001028 if (!pp)
1029 return NF_ACCEPT;
1030
Julius Volz2a3b7912008-09-02 15:55:47 +02001031 /* The embedded headers contain source and dest in reverse order */
Alex Gartrell802c41a2015-08-26 09:40:32 -07001032 cp = pp->conn_out_get(AF_INET6, skb, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001033 if (!cp)
1034 return NF_ACCEPT;
1035
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001036 snet.in6 = ciph.saddr.in6;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001037 offset = ciph.len;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001038 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001039 pp, offset, sizeof(struct ipv6hdr),
Julian Anastasov579eb622014-12-18 22:41:23 +02001040 hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001041}
1042#endif
1043
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001044/*
1045 * Check if sctp chunc is ABORT chunk
1046 */
1047static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1048{
1049 sctp_chunkhdr_t *sch, schunk;
1050 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1051 sizeof(schunk), &schunk);
1052 if (sch == NULL)
1053 return 0;
1054 if (sch->type == SCTP_CID_ABORT)
1055 return 1;
1056 return 0;
1057}
1058
Julius Volz2a3b7912008-09-02 15:55:47 +02001059static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct tcphdr _tcph, *th;
1062
Julius Volz2a3b7912008-09-02 15:55:47 +02001063 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (th == NULL)
1065 return 0;
1066 return th->rst;
1067}
1068
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001069static inline bool is_new_conn(const struct sk_buff *skb,
1070 struct ip_vs_iphdr *iph)
1071{
1072 switch (iph->protocol) {
1073 case IPPROTO_TCP: {
1074 struct tcphdr _tcph, *th;
1075
1076 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1077 if (th == NULL)
1078 return false;
1079 return th->syn;
1080 }
1081 case IPPROTO_SCTP: {
1082 sctp_chunkhdr_t *sch, schunk;
1083
1084 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1085 sizeof(schunk), &schunk);
1086 if (sch == NULL)
1087 return false;
1088 return sch->type == SCTP_CID_INIT;
1089 }
1090 default:
1091 return false;
1092 }
1093}
1094
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001095static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1096 int conn_reuse_mode)
1097{
1098 /* Controlled (FTP DATA or persistence)? */
1099 if (cp->control)
1100 return false;
1101
1102 switch (cp->protocol) {
1103 case IPPROTO_TCP:
1104 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1105 ((conn_reuse_mode & 2) &&
1106 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1107 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1108 case IPPROTO_SCTP:
1109 return cp->state == IP_VS_SCTP_S_CLOSED;
1110 default:
1111 return false;
1112 }
1113}
1114
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001115/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001116 */
1117static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001118handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Julian Anastasov579eb622014-12-18 22:41:23 +02001119 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1120 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001121{
Hans Schillstrom93304192011-01-03 14:44:51 +01001122 struct ip_vs_protocol *pp = pd->pp;
1123
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001124 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001125
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001126 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001127 goto drop;
1128
1129 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001130 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001131 goto drop;
1132
1133#ifdef CONFIG_IP_VS_IPV6
1134 if (af == AF_INET6)
1135 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1136 else
1137#endif
1138 {
1139 ip_hdr(skb)->saddr = cp->vaddr.ip;
1140 ip_send_check(ip_hdr(skb));
1141 }
1142
Julian Anastasov8a803042010-09-21 17:38:57 +02001143 /*
1144 * nf_iterate does not expect change in the skb->dst->dev.
1145 * It looks like it is not fatal to enable this code for hooks
1146 * where our handlers are at the end of the chain list and
1147 * when all next handlers use skb->dst->dev and not outdev.
1148 * It will definitely route properly the inout NAT traffic
1149 * when multiple paths are used.
1150 */
1151
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001152 /* For policy routing, packets originating from this
1153 * machine itself may be routed differently to packets
1154 * passing through. We want this packet to be routed as
1155 * if it came from this machine itself. So re-compute
1156 * the routing information.
1157 */
Julian Anastasov579eb622014-12-18 22:41:23 +02001158 if (ip_vs_route_me_harder(af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001159 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001160
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001161 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001162
1163 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001164 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001165 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001166 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001167 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001168 else
1169 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001170 ip_vs_conn_put(cp);
1171
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001172 LeaveFunction(11);
1173 return NF_ACCEPT;
1174
1175drop:
1176 ip_vs_conn_put(cp);
1177 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001178 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001179 return NF_STOLEN;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001183 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
1185static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001186ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Hans Schillstromfc723252011-01-03 14:44:43 +01001188 struct net *net = NULL;
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001189 struct netns_ipvs *ipvs;
Julius Volz51ef3482008-09-02 15:55:40 +02001190 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001192 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 EnterFunction(11);
1196
Julian Anastasovfc604762010-10-17 16:38:15 +03001197 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001198 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return NF_ACCEPT;
1200
Julian Anastasovfc604762010-10-17 16:38:15 +03001201 /* Bad... Do not break raw sockets */
1202 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1203 af == AF_INET)) {
1204 struct sock *sk = skb->sk;
1205 struct inet_sock *inet = inet_sk(skb->sk);
1206
1207 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1208 return NF_ACCEPT;
1209 }
1210
1211 if (unlikely(!skb_dst(skb)))
1212 return NF_ACCEPT;
1213
Hans Schillstromfc723252011-01-03 14:44:43 +01001214 net = skb_net(skb);
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001215 ipvs = net_ipvs(net);
1216 if (!ipvs->enable)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001217 return NF_ACCEPT;
1218
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001219 ip_vs_fill_iph_skb(af, skb, false, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001220#ifdef CONFIG_IP_VS_IPV6
1221 if (af == AF_INET6) {
1222 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001223 int related;
1224 int verdict = ip_vs_out_icmp_v6(skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001225 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Julian Anastasovf5a41842010-10-17 16:35:46 +03001227 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001228 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001229 }
1230 } else
1231#endif
1232 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001233 int related;
1234 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001235
Julian Anastasovf5a41842010-10-17 16:35:46 +03001236 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001237 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001240 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001241 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001243 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001246#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001247 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001248#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001249 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001250 if (ip_vs_gather_frags(skb,
1251 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001252 return NF_STOLEN;
1253
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001254 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /*
1258 * Check if the packet belongs to an existing entry
1259 */
Alex Gartrell802c41a2015-08-26 09:40:32 -07001260 cp = pp->conn_out_get(af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Julian Anastasovcb591552010-10-17 16:40:51 +03001262 if (likely(cp))
Julian Anastasov579eb622014-12-18 22:41:23 +02001263 return handle_response(af, skb, pd, cp, &iph, hooknum);
Simon Horman0cfa5582011-02-04 18:33:01 +09001264 if (sysctl_nat_icmp_send(net) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001265 (pp->protocol == IPPROTO_TCP ||
1266 pp->protocol == IPPROTO_UDP ||
1267 pp->protocol == IPPROTO_SCTP)) {
1268 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001270 pptr = frag_safe_skb_hp(skb, iph.len,
1271 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001272 if (pptr == NULL)
1273 return NF_ACCEPT; /* Not for me */
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001274 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
Julian Anastasov276472e2013-03-21 11:58:08 +02001275 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001276 /*
1277 * Notify the real server: there is no
1278 * existing entry if it is not RST
1279 * packet or not TCP packet.
1280 */
1281 if ((iph.protocol != IPPROTO_TCP &&
1282 iph.protocol != IPPROTO_SCTP)
1283 || ((iph.protocol == IPPROTO_TCP
1284 && !is_tcp_reset(skb, iph.len))
1285 || (iph.protocol == IPPROTO_SCTP
1286 && !is_sctp_abort(skb,
1287 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001288#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001289 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001290 if (!skb->dev)
1291 skb->dev = net->loopback_dev;
1292 icmpv6_send(skb,
1293 ICMPV6_DEST_UNREACH,
1294 ICMPV6_PORT_UNREACH,
1295 0);
1296 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001297#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001298 icmp_send(skb,
1299 ICMP_DEST_UNREACH,
1300 ICMP_PORT_UNREACH, 0);
1301 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001305 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
Julian Anastasovcb591552010-10-17 16:40:51 +03001306 "ip_vs_out: packet continues traversal as normal");
1307 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Julian Anastasovfc604762010-10-17 16:38:15 +03001310/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001311 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1312 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001313 * Check if packet is reply for established ip_vs_conn.
1314 */
1315static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001316ip_vs_reply4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001317 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001318{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001319 return ip_vs_out(state->hook, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001320}
1321
1322/*
1323 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1324 * Check if packet is reply for established ip_vs_conn.
1325 */
1326static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001327ip_vs_local_reply4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001328 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001329{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001330 return ip_vs_out(state->hook, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001331}
1332
1333#ifdef CONFIG_IP_VS_IPV6
1334
1335/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001336 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1337 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001338 * Check if packet is reply for established ip_vs_conn.
1339 */
1340static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001341ip_vs_reply6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001342 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001343{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001344 return ip_vs_out(state->hook, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001345}
1346
1347/*
1348 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1349 * Check if packet is reply for established ip_vs_conn.
1350 */
1351static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001352ip_vs_local_reply6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001353 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001354{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001355 return ip_vs_out(state->hook, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001356}
1357
1358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001360static unsigned int
1361ip_vs_try_to_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1362 int *verdict, struct ip_vs_conn **cpp,
1363 struct ip_vs_iphdr *iph)
1364{
1365 struct ip_vs_protocol *pp = pd->pp;
1366
1367 if (!iph->fragoffs) {
1368 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1369 * replayed fragment zero will already have created the cp
1370 */
1371
1372 /* Schedule and create new connection entry into cpp */
1373 if (!pp->conn_schedule(af, skb, pd, verdict, cpp, iph))
1374 return 0;
1375 }
1376
1377 if (unlikely(!*cpp)) {
1378 /* sorry, all this trouble for a no-hit :) */
1379 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1380 "ip_vs_in: packet continues traversal as normal");
1381 if (iph->fragoffs) {
1382 /* Fragment that couldn't be mapped to a conn entry
1383 * is missing module nf_defrag_ipv6
1384 */
1385 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1386 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1387 "unhandled fragment");
1388 }
1389 *verdict = NF_ACCEPT;
1390 return 0;
1391 }
1392
1393 return 1;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/*
1397 * Handle ICMP messages in the outside-to-inside direction (incoming).
1398 * Find any that might be relevant, check against existing connections,
1399 * forward to the right destination host if relevant.
1400 * Currently handles error types - unreachable, quench, ttl exceeded.
1401 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001402static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001403ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Hans Schillstrom93304192011-01-03 14:44:51 +01001405 struct net *net = NULL;
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001406 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct iphdr *iph;
1408 struct icmphdr _icmph, *ic;
1409 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001410 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct ip_vs_conn *cp;
1412 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001413 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001414 unsigned int offset, offset2, ihl, verdict;
Alex Gartrell6044eef2015-08-26 09:40:37 -07001415 bool ipip, new_cp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 *related = 1;
1418
1419 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001420 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001421 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001425 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 offset = ihl = iph->ihl * 4;
1427 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1428 if (ic == NULL)
1429 return NF_DROP;
1430
Harvey Harrison14d5e832008-10-31 00:54:29 -07001431 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001433 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
1436 * Work through seeing if this is for us.
1437 * These checks are supposed to be in an order that means easy
1438 * things are checked first to speed up processing.... however
1439 * this means that some packets will manage to get a long way
1440 * down this stack and then be rejected, but that's life.
1441 */
1442 if ((ic->type != ICMP_DEST_UNREACH) &&
1443 (ic->type != ICMP_SOURCE_QUENCH) &&
1444 (ic->type != ICMP_TIME_EXCEEDED)) {
1445 *related = 0;
1446 return NF_ACCEPT;
1447 }
1448
1449 /* Now find the contained IP header */
1450 offset += sizeof(_icmph);
1451 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1452 if (cih == NULL)
1453 return NF_ACCEPT; /* The packet looks wrong, ignore */
1454
Hans Schillstrom93304192011-01-03 14:44:51 +01001455 net = skb_net(skb);
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001456 ipvs = net_ipvs(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001457
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001458 /* Special case for errors for IPIP packets */
1459 ipip = false;
1460 if (cih->protocol == IPPROTO_IPIP) {
1461 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1462 return NF_ACCEPT;
1463 /* Error for our IPIP must arrive at LOCAL_IN */
1464 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1465 return NF_ACCEPT;
1466 offset += cih->ihl * 4;
1467 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1468 if (cih == NULL)
1469 return NF_ACCEPT; /* The packet looks wrong, ignore */
1470 ipip = true;
1471 }
1472
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001473 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001474 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001476 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001479 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 pp->dont_defrag))
1481 return NF_ACCEPT;
1482
Julian Anastasov0d796412010-10-17 16:46:17 +03001483 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1484 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001486 offset2 = offset;
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001487 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001488 offset = ciph.len;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001489
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001490 /* The embedded headers contain source and dest in reverse order.
1491 * For IPIP this is error for request, not for reply.
1492 */
Alex Gartrell802c41a2015-08-26 09:40:32 -07001493 cp = pp->conn_in_get(AF_INET, skb, &ciph);
Alex Gartrell6044eef2015-08-26 09:40:37 -07001494
1495 if (!cp) {
1496 int v;
1497
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001498 if (!sysctl_schedule_icmp(ipvs))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001499 return NF_ACCEPT;
1500
1501 if (!ip_vs_try_to_schedule(AF_INET, skb, pd, &v, &cp, &ciph))
1502 return v;
1503 new_cp = true;
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 verdict = NF_DROP;
1507
1508 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001509 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001511 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1512 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 goto out;
1514 }
1515
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001516 if (ipip) {
1517 __be32 info = ic->un.gateway;
Peter Christensenf44a5f42014-05-24 21:40:12 +02001518 __u8 type = ic->type;
1519 __u8 code = ic->code;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001520
1521 /* Update the MTU */
1522 if (ic->type == ICMP_DEST_UNREACH &&
1523 ic->code == ICMP_FRAG_NEEDED) {
1524 struct ip_vs_dest *dest = cp->dest;
1525 u32 mtu = ntohs(ic->un.frag.mtu);
Peter Christensenf44a5f42014-05-24 21:40:12 +02001526 __be16 frag_off = cih->frag_off;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001527
1528 /* Strip outer IP and ICMP, go to IPIP header */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001529 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1530 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001531 offset2 -= ihl + sizeof(_icmph);
1532 skb_reset_network_header(skb);
1533 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1534 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001535 ipv4_update_pmtu(skb, dev_net(skb->dev),
1536 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001537 /* Client uses PMTUD? */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001538 if (!(frag_off & htons(IP_DF)))
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001539 goto ignore_ipip;
1540 /* Prefer the resulting PMTU */
1541 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001542 struct ip_vs_dest_dst *dest_dst;
1543
1544 rcu_read_lock();
1545 dest_dst = rcu_dereference(dest->dest_dst);
1546 if (dest_dst)
1547 mtu = dst_mtu(dest_dst->dst_cache);
1548 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001549 }
1550 if (mtu > 68 + sizeof(struct iphdr))
1551 mtu -= sizeof(struct iphdr);
1552 info = htonl(mtu);
1553 }
1554 /* Strip outer IP, ICMP and IPIP, go to IP header of
1555 * original request.
1556 */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001557 if (pskb_pull(skb, offset2) == NULL)
1558 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001559 skb_reset_network_header(skb);
1560 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1561 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Peter Christensenf44a5f42014-05-24 21:40:12 +02001562 type, code, ntohl(info));
1563 icmp_send(skb, type, code, info);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001564 /* ICMP can be shorter but anyways, account it */
1565 ip_vs_out_stats(cp, skb);
1566
1567ignore_ipip:
1568 consume_skb(skb);
1569 verdict = NF_STOLEN;
1570 goto out;
1571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* do the statistics and put it back */
1574 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001575 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1576 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001578 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Hans Schillstrom552ad652011-06-13 12:19:26 +02001580out:
Alex Gartrell6044eef2015-08-26 09:40:37 -07001581 if (likely(!new_cp))
1582 __ip_vs_conn_put(cp);
1583 else
1584 ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 return verdict;
1587}
1588
Julius Volz2a3b7912008-09-02 15:55:47 +02001589#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001590static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1591 unsigned int hooknum, struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001592{
Hans Schillstrom93304192011-01-03 14:44:51 +01001593 struct net *net = NULL;
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001594 struct netns_ipvs *ipvs;
Julius Volz2a3b7912008-09-02 15:55:47 +02001595 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001596 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001597 struct ip_vs_conn *cp;
1598 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001599 struct ip_vs_proto_data *pd;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001600 unsigned int offset, verdict;
Alex Gartrell6044eef2015-08-26 09:40:37 -07001601 bool new_cp = false;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001602
Julius Volz2a3b7912008-09-02 15:55:47 +02001603 *related = 1;
1604
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001605 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001606 if (ic == NULL)
1607 return NF_DROP;
1608
Julius Volz2a3b7912008-09-02 15:55:47 +02001609 /*
1610 * Work through seeing if this is for us.
1611 * These checks are supposed to be in an order that means easy
1612 * things are checked first to speed up processing.... however
1613 * this means that some packets will manage to get a long way
1614 * down this stack and then be rejected, but that's life.
1615 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001616 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001617 *related = 0;
1618 return NF_ACCEPT;
1619 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001620 /* Fragment header that is before ICMP header tells us that:
1621 * it's not an error message since they can't be fragmented.
1622 */
David S. Millere7165032012-11-30 12:01:30 -05001623 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001624 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001625
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001626 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1627 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1628 &iph->saddr, &iph->daddr);
1629
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001630 offset = iph->len + sizeof(_icmph);
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001631 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001632 return NF_ACCEPT;
Julius Volz2a3b7912008-09-02 15:55:47 +02001633
Hans Schillstrom93304192011-01-03 14:44:51 +01001634 net = skb_net(skb);
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001635 ipvs = net_ipvs(net);
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001636 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001637 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001638 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001639 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001640
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001641 /* Cannot handle fragmented embedded protocol */
1642 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001643 return NF_ACCEPT;
1644
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001645 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
Julian Anastasov0d796412010-10-17 16:46:17 +03001646 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001647
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001648 /* The embedded headers contain source and dest in reverse order
1649 * if not from localhost
1650 */
Alex Gartrell802c41a2015-08-26 09:40:32 -07001651 cp = pp->conn_in_get(AF_INET6, skb, &ciph);
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001652
Alex Gartrell6044eef2015-08-26 09:40:37 -07001653 if (!cp) {
1654 int v;
1655
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001656 if (!sysctl_schedule_icmp(ipvs))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001657 return NF_ACCEPT;
1658
1659 if (!ip_vs_try_to_schedule(AF_INET6, skb, pd, &v, &cp, &ciph))
1660 return v;
1661
1662 new_cp = true;
1663 }
1664
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001665 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1666 if ((hooknum == NF_INET_LOCAL_OUT) &&
1667 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
Alex Gartrell6044eef2015-08-26 09:40:37 -07001668 verdict = NF_ACCEPT;
1669 goto out;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001670 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001671
Julius Volz2a3b7912008-09-02 15:55:47 +02001672 /* do the statistics and put it back */
1673 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001674
1675 /* Need to mangle contained IPv6 header in ICMPv6 packet */
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001676 offset = ciph.len;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001677 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1678 IPPROTO_SCTP == ciph.protocol)
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001679 offset += 2 * sizeof(__u16); /* Also mangle ports */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001680
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001681 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001682
Alex Gartrell6044eef2015-08-26 09:40:37 -07001683out:
1684 if (likely(!new_cp))
1685 __ip_vs_conn_put(cp);
1686 else
1687 ip_vs_conn_put(cp);
Julius Volz2a3b7912008-09-02 15:55:47 +02001688
1689 return verdict;
1690}
1691#endif
1692
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/*
1695 * Check if it's for virtual services, look it up,
1696 * and send it on its way...
1697 */
1698static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001699ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Hans Schillstromf1313152011-01-03 14:44:55 +01001701 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001702 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001704 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001706 int ret, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001707 struct netns_ipvs *ipvs;
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001708 int conn_reuse_mode;
Julius Volz51ef3482008-09-02 15:55:40 +02001709
Julian Anastasovfc604762010-10-17 16:38:15 +03001710 /* Already marked as IPVS request or reply? */
1711 if (skb->ipvs_property)
1712 return NF_ACCEPT;
1713
Julian Anastasovcb591552010-10-17 16:40:51 +03001714 /*
1715 * Big tappo:
1716 * - remote client: only PACKET_HOST
1717 * - route: used for struct net when skb->dev is unset
1718 */
1719 if (unlikely((skb->pkt_type != PACKET_HOST &&
1720 hooknum != NF_INET_LOCAL_OUT) ||
1721 !skb_dst(skb))) {
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001722 ip_vs_fill_iph_skb(af, skb, false, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001723 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1724 " ignored in hook %u\n",
1725 skb->pkt_type, iph.protocol,
1726 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1727 return NF_ACCEPT;
1728 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001729 /* ipvs enabled in this netns ? */
1730 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001731 ipvs = net_ipvs(net);
1732 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001733 return NF_ACCEPT;
1734
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001735 ip_vs_fill_iph_skb(af, skb, false, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Julian Anastasovcb591552010-10-17 16:40:51 +03001737 /* Bad... Do not break raw sockets */
1738 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1739 af == AF_INET)) {
1740 struct sock *sk = skb->sk;
1741 struct inet_sock *inet = inet_sk(skb->sk);
1742
1743 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1744 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
Julius Volz94b26552009-08-31 16:22:23 +02001747#ifdef CONFIG_IP_VS_IPV6
1748 if (af == AF_INET6) {
1749 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001750 int related;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001751 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1752 &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Julius Volz94b26552009-08-31 16:22:23 +02001754 if (related)
1755 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001756 }
1757 } else
1758#endif
1759 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001760 int related;
1761 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001762
1763 if (related)
1764 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 /* Protocol supported? */
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001768 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
Alex Gartrell4e478092015-09-14 23:23:05 -07001769 if (unlikely(!pd)) {
1770 /* The only way we'll see this packet again is if it's
1771 * encapsulated, so mark it with ipvs_property=1 so we
1772 * skip it if we're ignoring tunneled packets
1773 */
1774 if (sysctl_ignore_tunneled(ipvs))
1775 skb->ipvs_property = 1;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return NF_ACCEPT;
Alex Gartrell4e478092015-09-14 23:23:05 -07001778 }
Hans Schillstrom93304192011-01-03 14:44:51 +01001779 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
1781 * Check if the packet belongs to an existing connection entry
1782 */
Alex Gartrell802c41a2015-08-26 09:40:32 -07001783 cp = pp->conn_in_get(af, skb, &iph);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001784
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001785 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1786 if (conn_reuse_mode && !iph.fragoffs &&
1787 is_new_conn(skb, &iph) && cp &&
1788 ((unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1789 unlikely(!atomic_read(&cp->dest->weight))) ||
1790 unlikely(is_new_conn_expected(cp, conn_reuse_mode)))) {
1791 if (!atomic_read(&cp->n_control))
1792 ip_vs_conn_expire_now(cp);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001793 __ip_vs_conn_put(cp);
1794 cp = NULL;
1795 }
1796
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001797 if (unlikely(!cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 int v;
1799
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001800 if (!ip_vs_try_to_schedule(af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return v;
1802 }
1803
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001804 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* Check the server status */
1807 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1808 /* the destination server is not available */
1809
Simon Horman71a8ab62011-02-04 18:33:01 +09001810 if (sysctl_expire_nodest_conn(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* try to expire the connection immediately */
1812 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001814 /* don't restart its timer, and silently
1815 drop the packet. */
1816 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return NF_DROP;
1818 }
1819
1820 ip_vs_in_stats(cp, skb);
Simon Horman4a516f12011-09-16 14:11:49 +09001821 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001823 ret = cp->packet_xmit(skb, cp, pp, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /* do not touch skb anymore */
1825 else {
1826 IP_VS_DBG_RL("warning: packet_xmit is null");
1827 ret = NF_ACCEPT;
1828 }
1829
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001830 /* Increase its packet counter and check if it is needed
1831 * to be synchronized
1832 *
1833 * Sync connection if it is about to close to
1834 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001835 *
1836 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001837 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001838
Hans Schillstrom986a0752010-11-19 14:25:13 +01001839 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Simon Horman59e03502011-02-04 18:33:01 +09001840 pkts = sysctl_sync_threshold(ipvs);
Hans Schillstrom986a0752010-11-19 14:25:13 +01001841 else
1842 pkts = atomic_add_return(1, &cp->in_pkts);
1843
Julian Anastasov749c42b2012-04-24 23:46:40 +03001844 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -05001845 ip_vs_sync_conn(ipvs, cp, pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 ip_vs_conn_put(cp);
1848 return ret;
1849}
1850
Julian Anastasovcb591552010-10-17 16:40:51 +03001851/*
1852 * AF_INET handler in NF_INET_LOCAL_IN chain
1853 * Schedule and forward packets from remote clients
1854 */
1855static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001856ip_vs_remote_request4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001857 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001858{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001859 return ip_vs_in(state->hook, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001860}
1861
1862/*
1863 * AF_INET handler in NF_INET_LOCAL_OUT chain
1864 * Schedule and forward packets from local clients
1865 */
1866static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001867ip_vs_local_request4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001868 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001869{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001870 return ip_vs_in(state->hook, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001871}
1872
1873#ifdef CONFIG_IP_VS_IPV6
1874
1875/*
1876 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1877 * Schedule and forward packets from remote clients
1878 */
1879static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001880ip_vs_remote_request6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001881 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001882{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001883 return ip_vs_in(state->hook, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001884}
1885
1886/*
1887 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1888 * Schedule and forward packets from local clients
1889 */
1890static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001891ip_vs_local_request6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001892 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001893{
Eric W. Biederman176971b2015-09-18 14:33:05 -05001894 return ip_vs_in(state->hook, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001895}
1896
1897#endif
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001901 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 * related packets destined for 0.0.0.0/0.
1903 * When fwmark-based virtual service is used, such as transparent
1904 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1905 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001906 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 * and send them to ip_vs_in_icmp.
1908 */
1909static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001910ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001911 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 int r;
Julian Anastasov0c125822013-03-09 23:25:04 +02001914 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Herbert Xu3db05fe2007-10-15 00:53:15 -07001916 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return NF_ACCEPT;
1918
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001919 /* ipvs enabled in this netns ? */
Eric W. Biedermand484fc32015-09-21 13:01:40 -05001920 ipvs = net_ipvs(state->net);
Julian Anastasov0c125822013-03-09 23:25:04 +02001921 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001922 return NF_ACCEPT;
1923
Eric W. Biederman06198b32015-09-18 14:33:06 -05001924 return ip_vs_in_icmp(skb, &r, state->hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Julius Volz2a3b7912008-09-02 15:55:47 +02001927#ifdef CONFIG_IP_VS_IPV6
1928static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001929ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001930 const struct nf_hook_state *state)
Julius Volz2a3b7912008-09-02 15:55:47 +02001931{
1932 int r;
Julian Anastasov0c125822013-03-09 23:25:04 +02001933 struct netns_ipvs *ipvs;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001934 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02001935
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001936 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001937 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02001938 return NF_ACCEPT;
1939
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001940 /* ipvs enabled in this netns ? */
Eric W. Biedermand484fc32015-09-21 13:01:40 -05001941 ipvs = net_ipvs(state->net);
Julian Anastasov0c125822013-03-09 23:25:04 +02001942 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001943 return NF_ACCEPT;
1944
Eric W. Biederman06198b32015-09-18 14:33:06 -05001945 return ip_vs_in_icmp_v6(skb, &r, state->hook, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001946}
1947#endif
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Patrick McHardy19994142007-12-05 01:23:00 -08001950static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001951 /* After packet filtering, change source only for VS/NAT */
1952 {
1953 .hook = ip_vs_reply4,
1954 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001955 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001956 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001957 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001958 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001959 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1960 * or VS/NAT(change destination), so that filtering rules can be
1961 * applied to IPVS. */
1962 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001963 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001964 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001965 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001966 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001967 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001968 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001969 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001970 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001971 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001972 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001973 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001974 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001975 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001976 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001977 /* After mangle, schedule and forward local requests */
1978 {
1979 .hook = ip_vs_local_request4,
1980 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001981 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001982 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001983 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001984 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001985 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1986 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1987 {
1988 .hook = ip_vs_forward_icmp,
1989 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001990 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001991 .hooknum = NF_INET_FORWARD,
1992 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001993 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001994 /* After packet filtering, change source only for VS/NAT */
1995 {
1996 .hook = ip_vs_reply4,
1997 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001998 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001999 .hooknum = NF_INET_FORWARD,
2000 .priority = 100,
2001 },
Julius Volz473b23d2008-09-02 15:55:54 +02002002#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03002003 /* After packet filtering, change source only for VS/NAT */
2004 {
2005 .hook = ip_vs_reply6,
2006 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002007 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002008 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002009 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002010 },
Julius Volz473b23d2008-09-02 15:55:54 +02002011 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2012 * or VS/NAT(change destination), so that filtering rules can be
2013 * applied to IPVS. */
2014 {
Julian Anastasovcb591552010-10-17 16:40:51 +03002015 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02002016 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002017 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002018 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002019 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02002020 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002021 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02002022 {
Julian Anastasovfc604762010-10-17 16:38:15 +03002023 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02002024 .owner = THIS_MODULE,
Julian Anastasoveb90b0c2014-08-22 17:53:41 +03002025 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03002026 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002027 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02002028 },
Julian Anastasovcb591552010-10-17 16:40:51 +03002029 /* After mangle, schedule and forward local requests */
2030 {
2031 .hook = ip_vs_local_request6,
2032 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002033 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002034 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002035 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002036 },
Julius Volz473b23d2008-09-02 15:55:54 +02002037 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2038 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2039 {
2040 .hook = ip_vs_forward_icmp_v6,
2041 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002042 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002043 .hooknum = NF_INET_FORWARD,
2044 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02002045 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002046 /* After packet filtering, change source only for VS/NAT */
2047 {
2048 .hook = ip_vs_reply6,
2049 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002050 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03002051 .hooknum = NF_INET_FORWARD,
2052 .priority = 100,
2053 },
Julius Volz473b23d2008-09-02 15:55:54 +02002054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002056/*
2057 * Initialize IP Virtual Server netns mem.
2058 */
2059static int __net_init __ip_vs_init(struct net *net)
2060{
2061 struct netns_ipvs *ipvs;
2062
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002063 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07002064 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002065 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07002066
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002067 /* Hold the beast until a service is registerd */
2068 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01002069 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002070 /* Counters used for creating unique names */
2071 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2072 atomic_inc(&ipvs_netns_cnt);
2073 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002074
Hans Schillstrom503cf152011-05-01 18:50:16 +02002075 if (ip_vs_estimator_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002076 goto estimator_fail;
2077
Eric W. Biederman3d993762015-09-21 13:02:26 -05002078 if (ip_vs_control_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002079 goto control_fail;
2080
Hans Schillstrom503cf152011-05-01 18:50:16 +02002081 if (ip_vs_protocol_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002082 goto protocol_fail;
2083
Hans Schillstrom503cf152011-05-01 18:50:16 +02002084 if (ip_vs_app_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002085 goto app_fail;
2086
Hans Schillstrom503cf152011-05-01 18:50:16 +02002087 if (ip_vs_conn_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002088 goto conn_fail;
2089
Eric W. Biederman802cb432015-09-21 13:02:20 -05002090 if (ip_vs_sync_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002091 goto sync_fail;
2092
Simon Hormana870c8c2011-02-01 18:21:53 +01002093 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002094 sizeof(struct netns_ipvs), ipvs->gen);
2095 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002096/*
2097 * Error handling
2098 */
2099
2100sync_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002101 ip_vs_conn_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002102conn_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002103 ip_vs_app_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002104app_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002105 ip_vs_protocol_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002106protocol_fail:
Eric W. Biederman3d993762015-09-21 13:02:26 -05002107 ip_vs_control_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002108control_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002109 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002110estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03002111 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002112 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002113}
2114
2115static void __net_exit __ip_vs_cleanup(struct net *net)
2116{
Eric W. Biederman56d21692015-09-21 13:01:58 -05002117 struct netns_ipvs *ipvs = net_ipvs(net);
2118
2119 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
Hans Schillstrom503cf152011-05-01 18:50:16 +02002120 ip_vs_conn_net_cleanup(net);
2121 ip_vs_app_net_cleanup(net);
2122 ip_vs_protocol_net_cleanup(net);
Eric W. Biederman3d993762015-09-21 13:02:26 -05002123 ip_vs_control_net_cleanup(ipvs);
Hans Schillstrom503cf152011-05-01 18:50:16 +02002124 ip_vs_estimator_net_cleanup(net);
Eric W. Biederman56d21692015-09-21 13:01:58 -05002125 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002126 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002127}
2128
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002129static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2130{
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002131 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002132 EnterFunction(2);
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002133 ipvs->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002134 smp_wmb();
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002135 ip_vs_sync_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002136 LeaveFunction(2);
2137}
2138
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002139static struct pernet_operations ipvs_core_ops = {
2140 .init = __ip_vs_init,
2141 .exit = __ip_vs_cleanup,
2142 .id = &ip_vs_net_id,
2143 .size = sizeof(struct netns_ipvs),
2144};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002146static struct pernet_operations ipvs_core_dev_ops = {
2147 .exit = __ip_vs_dev_cleanup,
2148};
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150/*
2151 * Initialize IP Virtual Server
2152 */
2153static int __init ip_vs_init(void)
2154{
2155 int ret;
2156
2157 ret = ip_vs_control_init();
2158 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002159 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002160 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162
2163 ip_vs_protocol_init();
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 ret = ip_vs_conn_init();
2166 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002167 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002168 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002169 }
2170
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002171 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2172 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002173 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002174
2175 ret = register_pernet_device(&ipvs_core_dev_ops);
2176 if (ret < 0)
2177 goto cleanup_sub;
2178
Patrick McHardy41c5b312007-12-05 01:22:43 -08002179 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002181 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002182 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
Hans Schillstrom8537de82012-04-26 07:47:44 +02002185 ret = ip_vs_register_nl_ioctl();
2186 if (ret < 0) {
2187 pr_err("can't register netlink/ioctl.\n");
2188 goto cleanup_hooks;
2189 }
2190
Hannes Eder1e3e2382009-08-02 11:05:41 +00002191 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return ret;
2194
Hans Schillstrom8537de82012-04-26 07:47:44 +02002195cleanup_hooks:
2196 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002197cleanup_dev:
2198 unregister_pernet_device(&ipvs_core_dev_ops);
2199cleanup_sub:
2200 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002201cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002203cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ip_vs_protocol_cleanup();
2205 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002206exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return ret;
2208}
2209
2210static void __exit ip_vs_cleanup(void)
2211{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002212 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002213 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002214 unregister_pernet_device(&ipvs_core_dev_ops);
2215 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 ip_vs_protocol_cleanup();
2218 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002219 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222module_init(ip_vs_init);
2223module_exit(ip_vs_cleanup);
2224MODULE_LICENSE("GPL");