blob: 07a791ecdfbab9fee7f6c78bcc53a6f9204cbe90 [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;
Eric W. Biederman7c08d782015-09-21 13:02:48 -0500115 struct netns_ipvs *ipvs = cp->ipvs;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100116
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;
Eric W. Biederman7c08d782015-09-21 13:02:48 -0500149 struct netns_ipvs *ipvs = cp->ipvs;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100150
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;
Eric W. Biedermanab161972015-09-21 13:02:38 -0500472 cp = pp->conn_in_get(svc->ipvs, svc->af, skb, iph);
Alex Gartrell802c41a2015-08-26 09:40:32 -0700473 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;
Eric W. Biederman51efbcb2015-09-21 13:02:50 -0500571 struct netns_ipvs *ipvs = svc->ipvs;
572 struct net *net = ipvs->net;
Hans Schillstrom93304192011-01-03 14:44:51 +0100573
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200574 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Alex Gartrell0b729022015-08-26 09:40:30 -0700575 if (!pptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NF_DROP;
Alex Gartrell0b729022015-08-26 09:40:30 -0700577 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
Simon Hormana7a86b82011-02-04 18:33:02 +0900578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200580 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 a cache_bypass connection entry */
Eric W. Biederman57032942015-09-21 13:02:49 -0500582 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
Alex Gartrell0b729022015-08-26 09:40:30 -0700583 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
584 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100585 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200587 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200588 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200589 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000590 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000593 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900594 {
595 struct ip_vs_conn_param p;
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500596 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200597 &iph->saddr, pptr[0],
598 &iph->daddr, pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700599 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
Simon Hormanf11017e2010-08-22 21:37:52 +0900600 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100601 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900602 if (!cp)
603 return NF_DROP;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* statistics */
607 ip_vs_in_stats(cp, skb);
608
609 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900610 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200613 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* do not touch skb anymore */
615
616 atomic_inc(&cp->in_pkts);
617 ip_vs_conn_put(cp);
618 return ret;
619 }
620
621 /*
622 * When the virtual ftp service is presented, packets destined
623 * for other services on the VIP may get here (except services
624 * listed in the ipvs table), pass the packets, because it is
625 * not ipvs job to decide to drop the packets.
626 */
Alex Gartrell0b729022015-08-26 09:40:30 -0700627 if (svc->port == FTPPORT && dport != FTPPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Alex Gartrell0b729022015-08-26 09:40:30 -0700630 if (unlikely(ip_vs_iph_icmp(iph)))
631 return NF_DROP;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Notify the client that the destination is unreachable, and
635 * release the socket buffer.
636 * Since it is in IP layer, the TCP socket is not actually
637 * created, the TCP RST packet cannot be sent, instead that
638 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
639 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200640#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300641 if (svc->af == AF_INET6) {
Eric W. Biederman57032942015-09-21 13:02:49 -0500642 if (!skb->dev)
643 skb->dev = net->loopback_dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000644 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300645 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200646#endif
647 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NF_DROP;
650}
651
Simon Horman84b3cee2011-02-04 18:33:01 +0900652#ifdef CONFIG_SYSCTL
653
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500654static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
Simon Horman84b3cee2011-02-04 18:33:01 +0900655{
Simon Horman84b3cee2011-02-04 18:33:01 +0900656 return ipvs->sysctl_snat_reroute;
657}
658
Eric W. Biederman2300f042015-09-21 13:02:51 -0500659static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
Simon Horman0cfa5582011-02-04 18:33:01 +0900660{
Simon Horman0cfa5582011-02-04 18:33:01 +0900661 return ipvs->sysctl_nat_icmp_send;
662}
663
Simon Horman71a8ab62011-02-04 18:33:01 +0900664static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
665{
666 return ipvs->sysctl_expire_nodest_conn;
667}
668
Simon Horman84b3cee2011-02-04 18:33:01 +0900669#else
670
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500671static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
Eric W. Biederman2300f042015-09-21 13:02:51 -0500672static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900673static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900674
675#endif
676
Al Virob1550f22006-11-14 21:37:50 -0800677__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Al Virod3bc23e2006-11-14 21:24:49 -0800679 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300682static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
683{
684 if (NF_INET_LOCAL_IN == hooknum)
685 return IP_DEFRAG_VS_IN;
686 if (NF_INET_FORWARD == hooknum)
687 return IP_DEFRAG_VS_FWD;
688 return IP_DEFRAG_VS_OUT;
689}
690
Eric W. Biederman57781c12015-09-21 13:03:01 -0500691static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
692 struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Julian Anastasovac692692013-03-22 11:46:54 +0200694 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700695
Julian Anastasovac692692013-03-22 11:46:54 +0200696 local_bh_disable();
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500697 err = ip_defrag(ipvs->net, skb, user);
Julian Anastasovac692692013-03-22 11:46:54 +0200698 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700699 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700700 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700701
702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500705static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
706 struct sk_buff *skb, unsigned int hooknum)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900707{
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500708 if (!sysctl_snat_reroute(ipvs))
Julian Anastasov579eb622014-12-18 22:41:23 +0200709 return 0;
710 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
711 if (NF_INET_LOCAL_IN == hooknum)
712 return 0;
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900713#ifdef CONFIG_IP_VS_IPV6
714 if (af == AF_INET6) {
Julian Anastasov579eb622014-12-18 22:41:23 +0200715 struct dst_entry *dst = skb_dst(skb);
716
717 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
Eric W. Biederman5f5d74d2015-09-25 15:07:31 -0500718 ip6_route_me_harder(ipvs->net, skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900719 return 1;
720 } else
721#endif
Julian Anastasov579eb622014-12-18 22:41:23 +0200722 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
Eric W. Biedermane45f5062015-09-25 15:07:30 -0500723 ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900724 return 1;
725
726 return 0;
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/*
730 * Packet has been made sufficiently writable in caller
731 * - inout: 1=in->out, 0=out->in
732 */
733void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
734 struct ip_vs_conn *cp, int inout)
735{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700736 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700738 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
739 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
741
742 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200743 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200745 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 ip_send_check(ciph);
747 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200748 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200750 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ip_send_check(ciph);
752 }
753
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100754 /* the TCP/UDP/SCTP port */
755 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
756 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700757 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (inout)
760 ports[1] = cp->vport;
761 else
762 ports[0] = cp->dport;
763 }
764
765 /* And finally the ICMP checksum */
766 icmph->checksum = 0;
767 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
768 skb->ip_summed = CHECKSUM_UNNECESSARY;
769
770 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300771 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 "Forwarding altered outgoing ICMP");
773 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300774 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 "Forwarding altered incoming ICMP");
776}
777
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200778#ifdef CONFIG_IP_VS_IPV6
779void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
780 struct ip_vs_conn *cp, int inout)
781{
782 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200783 unsigned int icmp_offset = 0;
784 unsigned int offs = 0; /* header offset*/
785 int protocol;
786 struct icmp6hdr *icmph;
787 struct ipv6hdr *ciph;
788 unsigned short fragoffs;
789
790 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
791 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
792 offs = icmp_offset + sizeof(struct icmp6hdr);
793 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
794
795 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200796
797 if (inout) {
798 iph->saddr = cp->vaddr.in6;
799 ciph->daddr = cp->vaddr.in6;
800 } else {
801 iph->daddr = cp->daddr.in6;
802 ciph->saddr = cp->daddr.in6;
803 }
804
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100805 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200806 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
807 IPPROTO_SCTP == protocol)) {
808 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200809
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200810 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
811 ntohs(inout ? ports[1] : ports[0]),
812 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200813 if (inout)
814 ports[1] = cp->vport;
815 else
816 ports[0] = cp->dport;
817 }
818
819 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700820 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
821 skb->len - icmp_offset,
822 IPPROTO_ICMPV6, 0);
823 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
824 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
825 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200826
827 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300828 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
829 (void *)ciph - (void *)iph,
830 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200831 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300832 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
833 (void *)ciph - (void *)iph,
834 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200835}
836#endif
837
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000838/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200839 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000840 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000841static int handle_response_icmp(int af, struct sk_buff *skb,
842 union nf_inet_addr *snet,
843 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000844 struct ip_vs_protocol *pp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200845 unsigned int offset, unsigned int ihl,
846 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000847{
848 unsigned int verdict = NF_DROP;
849
850 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000851 pr_err("shouldn't reach here, because the box is on the "
852 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000853 }
854
855 /* Ensure the checksum is correct */
856 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
857 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000858 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
859 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000860 goto out;
861 }
862
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100863 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
864 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000865 offset += 2 * sizeof(__u16);
866 if (!skb_make_writable(skb, offset))
867 goto out;
868
Simon Hormanf2428ed2008-09-05 11:17:14 +1000869#ifdef CONFIG_IP_VS_IPV6
870 if (af == AF_INET6)
871 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
872 else
873#endif
874 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000875
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500876 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900877 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300878
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000879 /* do the statistics and put it back */
880 ip_vs_out_stats(cp, skb);
881
Julian Anastasovcf356d62010-10-17 16:21:07 +0300882 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200883 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300884 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200885 else
886 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000887 verdict = NF_ACCEPT;
888
889out:
890 __ip_vs_conn_put(cp);
891
892 return verdict;
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000897 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Eric W. Biederman7b5f6892015-09-21 13:02:55 -0500900static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
901 int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct iphdr *iph;
904 struct icmphdr _icmph, *ic;
905 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200906 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct ip_vs_conn *cp;
908 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000909 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000910 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 *related = 1;
913
914 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700915 if (ip_is_fragment(ip_hdr(skb))) {
Eric W. Biederman57781c12015-09-21 13:03:01 -0500916 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700920 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 offset = ihl = iph->ihl * 4;
922 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
923 if (ic == NULL)
924 return NF_DROP;
925
Harvey Harrison14d5e832008-10-31 00:54:29 -0700926 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700928 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /*
931 * Work through seeing if this is for us.
932 * These checks are supposed to be in an order that means easy
933 * things are checked first to speed up processing.... however
934 * this means that some packets will manage to get a long way
935 * down this stack and then be rejected, but that's life.
936 */
937 if ((ic->type != ICMP_DEST_UNREACH) &&
938 (ic->type != ICMP_SOURCE_QUENCH) &&
939 (ic->type != ICMP_TIME_EXCEEDED)) {
940 *related = 0;
941 return NF_ACCEPT;
942 }
943
944 /* Now find the contained IP header */
945 offset += sizeof(_icmph);
946 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
947 if (cih == NULL)
948 return NF_ACCEPT; /* The packet looks wrong, ignore */
949
950 pp = ip_vs_proto_get(cih->protocol);
951 if (!pp)
952 return NF_ACCEPT;
953
954 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900955 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 pp->dont_defrag))
957 return NF_ACCEPT;
958
Julian Anastasov0d796412010-10-17 16:46:17 +0300959 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
960 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Alex Gartrell4fd9bee2015-08-26 09:40:29 -0700962 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /* The embedded headers contain source and dest in reverse order */
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -0500965 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!cp)
967 return NF_ACCEPT;
968
Simon Hormanf2428ed2008-09-05 11:17:14 +1000969 snet.ip = iph->saddr;
970 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200971 pp, ciph.len, ihl, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Julius Volz2a3b7912008-09-02 15:55:47 +0200974#ifdef CONFIG_IP_VS_IPV6
Eric W. Biederman7b5f6892015-09-21 13:02:55 -0500975static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
976 int *related, unsigned int hooknum,
977 struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200978{
Julius Volz2a3b7912008-09-02 15:55:47 +0200979 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200980 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200981 struct ip_vs_conn *cp;
982 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000983 union nf_inet_addr snet;
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700984 unsigned int offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200985
Julius Volz2a3b7912008-09-02 15:55:47 +0200986 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200987 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200988 if (ic == NULL)
989 return NF_DROP;
990
Julius Volz2a3b7912008-09-02 15:55:47 +0200991 /*
992 * Work through seeing if this is for us.
993 * These checks are supposed to be in an order that means easy
994 * things are checked first to speed up processing.... however
995 * this means that some packets will manage to get a long way
996 * down this stack and then be rejected, but that's life.
997 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +0200998 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +0200999 *related = 0;
1000 return NF_ACCEPT;
1001 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001002 /* Fragment header that is before ICMP header tells us that:
1003 * it's not an error message since they can't be fragmented.
1004 */
David S. Millere7165032012-11-30 12:01:30 -05001005 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001006 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001007
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001008 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1009 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1010 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001011
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001012 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1013 true, &ciph))
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001014 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001015
1016 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +02001017 if (!pp)
1018 return NF_ACCEPT;
1019
Julius Volz2a3b7912008-09-02 15:55:47 +02001020 /* The embedded headers contain source and dest in reverse order */
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -05001021 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001022 if (!cp)
1023 return NF_ACCEPT;
1024
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001025 snet.in6 = ciph.saddr.in6;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001026 offset = ciph.len;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001027 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001028 pp, offset, sizeof(struct ipv6hdr),
Julian Anastasov579eb622014-12-18 22:41:23 +02001029 hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001030}
1031#endif
1032
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001033/*
1034 * Check if sctp chunc is ABORT chunk
1035 */
1036static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1037{
1038 sctp_chunkhdr_t *sch, schunk;
1039 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1040 sizeof(schunk), &schunk);
1041 if (sch == NULL)
1042 return 0;
1043 if (sch->type == SCTP_CID_ABORT)
1044 return 1;
1045 return 0;
1046}
1047
Julius Volz2a3b7912008-09-02 15:55:47 +02001048static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct tcphdr _tcph, *th;
1051
Julius Volz2a3b7912008-09-02 15:55:47 +02001052 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (th == NULL)
1054 return 0;
1055 return th->rst;
1056}
1057
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001058static inline bool is_new_conn(const struct sk_buff *skb,
1059 struct ip_vs_iphdr *iph)
1060{
1061 switch (iph->protocol) {
1062 case IPPROTO_TCP: {
1063 struct tcphdr _tcph, *th;
1064
1065 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1066 if (th == NULL)
1067 return false;
1068 return th->syn;
1069 }
1070 case IPPROTO_SCTP: {
1071 sctp_chunkhdr_t *sch, schunk;
1072
1073 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1074 sizeof(schunk), &schunk);
1075 if (sch == NULL)
1076 return false;
1077 return sch->type == SCTP_CID_INIT;
1078 }
1079 default:
1080 return false;
1081 }
1082}
1083
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001084static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1085 int conn_reuse_mode)
1086{
1087 /* Controlled (FTP DATA or persistence)? */
1088 if (cp->control)
1089 return false;
1090
1091 switch (cp->protocol) {
1092 case IPPROTO_TCP:
1093 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1094 ((conn_reuse_mode & 2) &&
1095 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1096 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1097 case IPPROTO_SCTP:
1098 return cp->state == IP_VS_SCTP_S_CLOSED;
1099 default:
1100 return false;
1101 }
1102}
1103
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001104/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001105 */
1106static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001107handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Julian Anastasov579eb622014-12-18 22:41:23 +02001108 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1109 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001110{
Hans Schillstrom93304192011-01-03 14:44:51 +01001111 struct ip_vs_protocol *pp = pd->pp;
1112
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001113 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001114
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001115 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001116 goto drop;
1117
1118 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001119 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001120 goto drop;
1121
1122#ifdef CONFIG_IP_VS_IPV6
1123 if (af == AF_INET6)
1124 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1125 else
1126#endif
1127 {
1128 ip_hdr(skb)->saddr = cp->vaddr.ip;
1129 ip_send_check(ip_hdr(skb));
1130 }
1131
Julian Anastasov8a803042010-09-21 17:38:57 +02001132 /*
1133 * nf_iterate does not expect change in the skb->dst->dev.
1134 * It looks like it is not fatal to enable this code for hooks
1135 * where our handlers are at the end of the chain list and
1136 * when all next handlers use skb->dst->dev and not outdev.
1137 * It will definitely route properly the inout NAT traffic
1138 * when multiple paths are used.
1139 */
1140
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001141 /* For policy routing, packets originating from this
1142 * machine itself may be routed differently to packets
1143 * passing through. We want this packet to be routed as
1144 * if it came from this machine itself. So re-compute
1145 * the routing information.
1146 */
Eric W. Biedermana43d1a62015-09-21 13:02:56 -05001147 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001148 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001149
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001150 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001151
1152 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001153 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001154 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001155 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001156 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001157 else
1158 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001159 ip_vs_conn_put(cp);
1160
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001161 LeaveFunction(11);
1162 return NF_ACCEPT;
1163
1164drop:
1165 ip_vs_conn_put(cp);
1166 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001167 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001168 return NF_STOLEN;
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001172 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
1174static unsigned int
Eric W. Biederman1b750972015-09-21 13:02:52 -05001175ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001177 struct net *net = ipvs->net;
Julius Volz51ef3482008-09-02 15:55:40 +02001178 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001180 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 EnterFunction(11);
1184
Julian Anastasovfc604762010-10-17 16:38:15 +03001185 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001186 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return NF_ACCEPT;
1188
Julian Anastasovfc604762010-10-17 16:38:15 +03001189 /* Bad... Do not break raw sockets */
1190 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1191 af == AF_INET)) {
1192 struct sock *sk = skb->sk;
1193 struct inet_sock *inet = inet_sk(skb->sk);
1194
1195 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1196 return NF_ACCEPT;
1197 }
1198
1199 if (unlikely(!skb_dst(skb)))
1200 return NF_ACCEPT;
1201
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001202 if (!ipvs->enable)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001203 return NF_ACCEPT;
1204
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001205 ip_vs_fill_iph_skb(af, skb, false, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001206#ifdef CONFIG_IP_VS_IPV6
1207 if (af == AF_INET6) {
1208 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001209 int related;
Eric W. Biederman7b5f6892015-09-21 13:02:55 -05001210 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001211 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Julian Anastasovf5a41842010-10-17 16:35:46 +03001213 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001214 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001215 }
1216 } else
1217#endif
1218 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001219 int related;
Eric W. Biederman7b5f6892015-09-21 13:02:55 -05001220 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001221
Julian Anastasovf5a41842010-10-17 16:35:46 +03001222 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001223 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001226 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001227 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001229 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001232#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001233 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001234#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001235 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Eric W. Biederman57781c12015-09-21 13:03:01 -05001236 if (ip_vs_gather_frags(ipvs, skb,
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001237 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001238 return NF_STOLEN;
1239
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001240 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /*
1244 * Check if the packet belongs to an existing entry
1245 */
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -05001246 cp = pp->conn_out_get(ipvs, af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Julian Anastasovcb591552010-10-17 16:40:51 +03001248 if (likely(cp))
Julian Anastasov579eb622014-12-18 22:41:23 +02001249 return handle_response(af, skb, pd, cp, &iph, hooknum);
Eric W. Biederman2300f042015-09-21 13:02:51 -05001250 if (sysctl_nat_icmp_send(ipvs) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001251 (pp->protocol == IPPROTO_TCP ||
1252 pp->protocol == IPPROTO_UDP ||
1253 pp->protocol == IPPROTO_SCTP)) {
1254 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001256 pptr = frag_safe_skb_hp(skb, iph.len,
1257 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001258 if (pptr == NULL)
1259 return NF_ACCEPT; /* Not for me */
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001260 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
Julian Anastasov276472e2013-03-21 11:58:08 +02001261 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001262 /*
1263 * Notify the real server: there is no
1264 * existing entry if it is not RST
1265 * packet or not TCP packet.
1266 */
1267 if ((iph.protocol != IPPROTO_TCP &&
1268 iph.protocol != IPPROTO_SCTP)
1269 || ((iph.protocol == IPPROTO_TCP
1270 && !is_tcp_reset(skb, iph.len))
1271 || (iph.protocol == IPPROTO_SCTP
1272 && !is_sctp_abort(skb,
1273 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001274#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001275 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001276 if (!skb->dev)
1277 skb->dev = net->loopback_dev;
1278 icmpv6_send(skb,
1279 ICMPV6_DEST_UNREACH,
1280 ICMPV6_PORT_UNREACH,
1281 0);
1282 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001283#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001284 icmp_send(skb,
1285 ICMP_DEST_UNREACH,
1286 ICMP_PORT_UNREACH, 0);
1287 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001291 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
Julian Anastasovcb591552010-10-17 16:40:51 +03001292 "ip_vs_out: packet continues traversal as normal");
1293 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Julian Anastasovfc604762010-10-17 16:38:15 +03001296/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001297 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1298 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001299 * Check if packet is reply for established ip_vs_conn.
1300 */
1301static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001302ip_vs_reply4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001303 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001304{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001305 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001306}
1307
1308/*
1309 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1310 * Check if packet is reply for established ip_vs_conn.
1311 */
1312static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001313ip_vs_local_reply4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001314 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001315{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001316 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001317}
1318
1319#ifdef CONFIG_IP_VS_IPV6
1320
1321/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001322 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1323 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001324 * Check if packet is reply for established ip_vs_conn.
1325 */
1326static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001327ip_vs_reply6(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. Biederman1b750972015-09-21 13:02:52 -05001330 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001331}
1332
1333/*
1334 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1335 * Check if packet is reply for established ip_vs_conn.
1336 */
1337static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001338ip_vs_local_reply6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001339 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001340{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001341 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001342}
1343
1344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001346static unsigned int
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001347ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1348 struct ip_vs_proto_data *pd,
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001349 int *verdict, struct ip_vs_conn **cpp,
1350 struct ip_vs_iphdr *iph)
1351{
1352 struct ip_vs_protocol *pp = pd->pp;
1353
1354 if (!iph->fragoffs) {
1355 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1356 * replayed fragment zero will already have created the cp
1357 */
1358
1359 /* Schedule and create new connection entry into cpp */
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001360 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001361 return 0;
1362 }
1363
1364 if (unlikely(!*cpp)) {
1365 /* sorry, all this trouble for a no-hit :) */
1366 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1367 "ip_vs_in: packet continues traversal as normal");
1368 if (iph->fragoffs) {
1369 /* Fragment that couldn't be mapped to a conn entry
1370 * is missing module nf_defrag_ipv6
1371 */
1372 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1373 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1374 "unhandled fragment");
1375 }
1376 *verdict = NF_ACCEPT;
1377 return 0;
1378 }
1379
1380 return 1;
1381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/*
1384 * Handle ICMP messages in the outside-to-inside direction (incoming).
1385 * Find any that might be relevant, check against existing connections,
1386 * forward to the right destination host if relevant.
1387 * Currently handles error types - unreachable, quench, ttl exceeded.
1388 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001389static int
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001390ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1391 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct iphdr *iph;
1394 struct icmphdr _icmph, *ic;
1395 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001396 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct ip_vs_conn *cp;
1398 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001399 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001400 unsigned int offset, offset2, ihl, verdict;
Alex Gartrell6044eef2015-08-26 09:40:37 -07001401 bool ipip, new_cp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 *related = 1;
1404
1405 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001406 if (ip_is_fragment(ip_hdr(skb))) {
Eric W. Biederman57781c12015-09-21 13:03:01 -05001407 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001411 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 offset = ihl = iph->ihl * 4;
1413 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1414 if (ic == NULL)
1415 return NF_DROP;
1416
Harvey Harrison14d5e832008-10-31 00:54:29 -07001417 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001419 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * Work through seeing if this is for us.
1423 * These checks are supposed to be in an order that means easy
1424 * things are checked first to speed up processing.... however
1425 * this means that some packets will manage to get a long way
1426 * down this stack and then be rejected, but that's life.
1427 */
1428 if ((ic->type != ICMP_DEST_UNREACH) &&
1429 (ic->type != ICMP_SOURCE_QUENCH) &&
1430 (ic->type != ICMP_TIME_EXCEEDED)) {
1431 *related = 0;
1432 return NF_ACCEPT;
1433 }
1434
1435 /* Now find the contained IP header */
1436 offset += sizeof(_icmph);
1437 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1438 if (cih == NULL)
1439 return NF_ACCEPT; /* The packet looks wrong, ignore */
1440
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001441 /* Special case for errors for IPIP packets */
1442 ipip = false;
1443 if (cih->protocol == IPPROTO_IPIP) {
1444 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1445 return NF_ACCEPT;
1446 /* Error for our IPIP must arrive at LOCAL_IN */
1447 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1448 return NF_ACCEPT;
1449 offset += cih->ihl * 4;
1450 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1451 if (cih == NULL)
1452 return NF_ACCEPT; /* The packet looks wrong, ignore */
1453 ipip = true;
1454 }
1455
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001456 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001457 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001459 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001462 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 pp->dont_defrag))
1464 return NF_ACCEPT;
1465
Julian Anastasov0d796412010-10-17 16:46:17 +03001466 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1467 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001469 offset2 = offset;
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001470 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001471 offset = ciph.len;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001472
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001473 /* The embedded headers contain source and dest in reverse order.
1474 * For IPIP this is error for request, not for reply.
1475 */
Eric W. Biedermanab161972015-09-21 13:02:38 -05001476 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
Alex Gartrell6044eef2015-08-26 09:40:37 -07001477
1478 if (!cp) {
1479 int v;
1480
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001481 if (!sysctl_schedule_icmp(ipvs))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001482 return NF_ACCEPT;
1483
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001484 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001485 return v;
1486 new_cp = true;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 verdict = NF_DROP;
1490
1491 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001492 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001494 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1495 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto out;
1497 }
1498
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001499 if (ipip) {
1500 __be32 info = ic->un.gateway;
Peter Christensenf44a5f42014-05-24 21:40:12 +02001501 __u8 type = ic->type;
1502 __u8 code = ic->code;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001503
1504 /* Update the MTU */
1505 if (ic->type == ICMP_DEST_UNREACH &&
1506 ic->code == ICMP_FRAG_NEEDED) {
1507 struct ip_vs_dest *dest = cp->dest;
1508 u32 mtu = ntohs(ic->un.frag.mtu);
Peter Christensenf44a5f42014-05-24 21:40:12 +02001509 __be16 frag_off = cih->frag_off;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001510
1511 /* Strip outer IP and ICMP, go to IPIP header */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001512 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1513 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001514 offset2 -= ihl + sizeof(_icmph);
1515 skb_reset_network_header(skb);
1516 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1517 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001518 ipv4_update_pmtu(skb, ipvs->net,
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001519 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001520 /* Client uses PMTUD? */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001521 if (!(frag_off & htons(IP_DF)))
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001522 goto ignore_ipip;
1523 /* Prefer the resulting PMTU */
1524 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001525 struct ip_vs_dest_dst *dest_dst;
1526
1527 rcu_read_lock();
1528 dest_dst = rcu_dereference(dest->dest_dst);
1529 if (dest_dst)
1530 mtu = dst_mtu(dest_dst->dst_cache);
1531 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001532 }
1533 if (mtu > 68 + sizeof(struct iphdr))
1534 mtu -= sizeof(struct iphdr);
1535 info = htonl(mtu);
1536 }
1537 /* Strip outer IP, ICMP and IPIP, go to IP header of
1538 * original request.
1539 */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001540 if (pskb_pull(skb, offset2) == NULL)
1541 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001542 skb_reset_network_header(skb);
1543 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1544 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Peter Christensenf44a5f42014-05-24 21:40:12 +02001545 type, code, ntohl(info));
1546 icmp_send(skb, type, code, info);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001547 /* ICMP can be shorter but anyways, account it */
1548 ip_vs_out_stats(cp, skb);
1549
1550ignore_ipip:
1551 consume_skb(skb);
1552 verdict = NF_STOLEN;
1553 goto out;
1554 }
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /* do the statistics and put it back */
1557 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001558 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1559 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001561 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Hans Schillstrom552ad652011-06-13 12:19:26 +02001563out:
Alex Gartrell6044eef2015-08-26 09:40:37 -07001564 if (likely(!new_cp))
1565 __ip_vs_conn_put(cp);
1566 else
1567 ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 return verdict;
1570}
1571
Julius Volz2a3b7912008-09-02 15:55:47 +02001572#ifdef CONFIG_IP_VS_IPV6
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001573static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1574 int *related, unsigned int hooknum,
1575 struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001576{
Julius Volz2a3b7912008-09-02 15:55:47 +02001577 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001578 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001579 struct ip_vs_conn *cp;
1580 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001581 struct ip_vs_proto_data *pd;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001582 unsigned int offset, verdict;
Alex Gartrell6044eef2015-08-26 09:40:37 -07001583 bool new_cp = false;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001584
Julius Volz2a3b7912008-09-02 15:55:47 +02001585 *related = 1;
1586
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001587 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001588 if (ic == NULL)
1589 return NF_DROP;
1590
Julius Volz2a3b7912008-09-02 15:55:47 +02001591 /*
1592 * Work through seeing if this is for us.
1593 * These checks are supposed to be in an order that means easy
1594 * things are checked first to speed up processing.... however
1595 * this means that some packets will manage to get a long way
1596 * down this stack and then be rejected, but that's life.
1597 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001598 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001599 *related = 0;
1600 return NF_ACCEPT;
1601 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001602 /* Fragment header that is before ICMP header tells us that:
1603 * it's not an error message since they can't be fragmented.
1604 */
David S. Millere7165032012-11-30 12:01:30 -05001605 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001606 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001607
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001608 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1609 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1610 &iph->saddr, &iph->daddr);
1611
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001612 offset = iph->len + sizeof(_icmph);
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001613 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001614 return NF_ACCEPT;
Julius Volz2a3b7912008-09-02 15:55:47 +02001615
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001616 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001617 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001618 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001619 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001620
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001621 /* Cannot handle fragmented embedded protocol */
1622 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001623 return NF_ACCEPT;
1624
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001625 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
Julian Anastasov0d796412010-10-17 16:46:17 +03001626 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001627
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001628 /* The embedded headers contain source and dest in reverse order
1629 * if not from localhost
1630 */
Eric W. Biedermanab161972015-09-21 13:02:38 -05001631 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001632
Alex Gartrell6044eef2015-08-26 09:40:37 -07001633 if (!cp) {
1634 int v;
1635
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001636 if (!sysctl_schedule_icmp(ipvs))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001637 return NF_ACCEPT;
1638
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001639 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001640 return v;
1641
1642 new_cp = true;
1643 }
1644
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001645 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1646 if ((hooknum == NF_INET_LOCAL_OUT) &&
1647 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
Alex Gartrell6044eef2015-08-26 09:40:37 -07001648 verdict = NF_ACCEPT;
1649 goto out;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001650 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001651
Julius Volz2a3b7912008-09-02 15:55:47 +02001652 /* do the statistics and put it back */
1653 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001654
1655 /* Need to mangle contained IPv6 header in ICMPv6 packet */
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001656 offset = ciph.len;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001657 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1658 IPPROTO_SCTP == ciph.protocol)
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001659 offset += 2 * sizeof(__u16); /* Also mangle ports */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001660
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001661 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001662
Alex Gartrell6044eef2015-08-26 09:40:37 -07001663out:
1664 if (likely(!new_cp))
1665 __ip_vs_conn_put(cp);
1666 else
1667 ip_vs_conn_put(cp);
Julius Volz2a3b7912008-09-02 15:55:47 +02001668
1669 return verdict;
1670}
1671#endif
1672
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674/*
1675 * Check if it's for virtual services, look it up,
1676 * and send it on its way...
1677 */
1678static unsigned int
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05001679ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Julius Volz51ef3482008-09-02 15:55:40 +02001681 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001683 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001685 int ret, pkts;
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001686 int conn_reuse_mode;
Julius Volz51ef3482008-09-02 15:55:40 +02001687
Julian Anastasovfc604762010-10-17 16:38:15 +03001688 /* Already marked as IPVS request or reply? */
1689 if (skb->ipvs_property)
1690 return NF_ACCEPT;
1691
Julian Anastasovcb591552010-10-17 16:40:51 +03001692 /*
1693 * Big tappo:
1694 * - remote client: only PACKET_HOST
1695 * - route: used for struct net when skb->dev is unset
1696 */
1697 if (unlikely((skb->pkt_type != PACKET_HOST &&
1698 hooknum != NF_INET_LOCAL_OUT) ||
1699 !skb_dst(skb))) {
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001700 ip_vs_fill_iph_skb(af, skb, false, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001701 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1702 " ignored in hook %u\n",
1703 skb->pkt_type, iph.protocol,
1704 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1705 return NF_ACCEPT;
1706 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001707 /* ipvs enabled in this netns ? */
Julian Anastasov0c125822013-03-09 23:25:04 +02001708 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001709 return NF_ACCEPT;
1710
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001711 ip_vs_fill_iph_skb(af, skb, false, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Julian Anastasovcb591552010-10-17 16:40:51 +03001713 /* Bad... Do not break raw sockets */
1714 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1715 af == AF_INET)) {
1716 struct sock *sk = skb->sk;
1717 struct inet_sock *inet = inet_sk(skb->sk);
1718
1719 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1720 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
Julius Volz94b26552009-08-31 16:22:23 +02001723#ifdef CONFIG_IP_VS_IPV6
1724 if (af == AF_INET6) {
1725 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001726 int related;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001727 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
1728 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Julius Volz94b26552009-08-31 16:22:23 +02001730 if (related)
1731 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001732 }
1733 } else
1734#endif
1735 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001736 int related;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001737 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
1738 hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001739
1740 if (related)
1741 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /* Protocol supported? */
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001745 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
Alex Gartrell4e478092015-09-14 23:23:05 -07001746 if (unlikely(!pd)) {
1747 /* The only way we'll see this packet again is if it's
1748 * encapsulated, so mark it with ipvs_property=1 so we
1749 * skip it if we're ignoring tunneled packets
1750 */
1751 if (sysctl_ignore_tunneled(ipvs))
1752 skb->ipvs_property = 1;
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return NF_ACCEPT;
Alex Gartrell4e478092015-09-14 23:23:05 -07001755 }
Hans Schillstrom93304192011-01-03 14:44:51 +01001756 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * Check if the packet belongs to an existing connection entry
1759 */
Eric W. Biedermanab161972015-09-21 13:02:38 -05001760 cp = pp->conn_in_get(ipvs, af, skb, &iph);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001761
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001762 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1763 if (conn_reuse_mode && !iph.fragoffs &&
1764 is_new_conn(skb, &iph) && cp &&
1765 ((unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1766 unlikely(!atomic_read(&cp->dest->weight))) ||
1767 unlikely(is_new_conn_expected(cp, conn_reuse_mode)))) {
1768 if (!atomic_read(&cp->n_control))
1769 ip_vs_conn_expire_now(cp);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001770 __ip_vs_conn_put(cp);
1771 cp = NULL;
1772 }
1773
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001774 if (unlikely(!cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int v;
1776
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001777 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return v;
1779 }
1780
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001781 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Check the server status */
1784 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1785 /* the destination server is not available */
1786
Simon Horman71a8ab62011-02-04 18:33:01 +09001787 if (sysctl_expire_nodest_conn(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /* try to expire the connection immediately */
1789 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001791 /* don't restart its timer, and silently
1792 drop the packet. */
1793 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return NF_DROP;
1795 }
1796
1797 ip_vs_in_stats(cp, skb);
Simon Horman4a516f12011-09-16 14:11:49 +09001798 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001800 ret = cp->packet_xmit(skb, cp, pp, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* do not touch skb anymore */
1802 else {
1803 IP_VS_DBG_RL("warning: packet_xmit is null");
1804 ret = NF_ACCEPT;
1805 }
1806
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001807 /* Increase its packet counter and check if it is needed
1808 * to be synchronized
1809 *
1810 * Sync connection if it is about to close to
1811 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001812 *
1813 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001814 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001815
Hans Schillstrom986a0752010-11-19 14:25:13 +01001816 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Simon Horman59e03502011-02-04 18:33:01 +09001817 pkts = sysctl_sync_threshold(ipvs);
Hans Schillstrom986a0752010-11-19 14:25:13 +01001818 else
1819 pkts = atomic_add_return(1, &cp->in_pkts);
1820
Julian Anastasov749c42b2012-04-24 23:46:40 +03001821 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -05001822 ip_vs_sync_conn(ipvs, cp, pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 ip_vs_conn_put(cp);
1825 return ret;
1826}
1827
Julian Anastasovcb591552010-10-17 16:40:51 +03001828/*
1829 * AF_INET handler in NF_INET_LOCAL_IN chain
1830 * Schedule and forward packets from remote clients
1831 */
1832static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001833ip_vs_remote_request4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001834 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001835{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05001836 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001837}
1838
1839/*
1840 * AF_INET handler in NF_INET_LOCAL_OUT chain
1841 * Schedule and forward packets from local clients
1842 */
1843static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001844ip_vs_local_request4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001845 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001846{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05001847 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001848}
1849
1850#ifdef CONFIG_IP_VS_IPV6
1851
1852/*
1853 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1854 * Schedule and forward packets from remote clients
1855 */
1856static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001857ip_vs_remote_request6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001858 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001859{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05001860 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001861}
1862
1863/*
1864 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1865 * Schedule and forward packets from local clients
1866 */
1867static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001868ip_vs_local_request6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001869 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001870{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05001871 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001872}
1873
1874#endif
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001878 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * related packets destined for 0.0.0.0/0.
1880 * When fwmark-based virtual service is used, such as transparent
1881 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1882 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001883 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * and send them to ip_vs_in_icmp.
1885 */
1886static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001887ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001888 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 int r;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001891 struct netns_ipvs *ipvs = net_ipvs(state->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Herbert Xu3db05fe2007-10-15 00:53:15 -07001893 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return NF_ACCEPT;
1895
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001896 /* ipvs enabled in this netns ? */
Julian Anastasov0c125822013-03-09 23:25:04 +02001897 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001898 return NF_ACCEPT;
1899
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001900 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Julius Volz2a3b7912008-09-02 15:55:47 +02001903#ifdef CONFIG_IP_VS_IPV6
1904static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001905ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001906 const struct nf_hook_state *state)
Julius Volz2a3b7912008-09-02 15:55:47 +02001907{
1908 int r;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001909 struct netns_ipvs *ipvs = net_ipvs(state->net);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001910 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02001911
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001912 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001913 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02001914 return NF_ACCEPT;
1915
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001916 /* ipvs enabled in this netns ? */
Julian Anastasov0c125822013-03-09 23:25:04 +02001917 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001918 return NF_ACCEPT;
1919
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001920 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001921}
1922#endif
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Patrick McHardy19994142007-12-05 01:23:00 -08001925static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001926 /* After packet filtering, change source only for VS/NAT */
1927 {
1928 .hook = ip_vs_reply4,
1929 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001930 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001931 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001932 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001933 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001934 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1935 * or VS/NAT(change destination), so that filtering rules can be
1936 * applied to IPVS. */
1937 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001938 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001939 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001940 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001941 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001942 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001943 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001944 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001945 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001946 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001947 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001948 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001949 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001950 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001951 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001952 /* After mangle, schedule and forward local requests */
1953 {
1954 .hook = ip_vs_local_request4,
1955 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001956 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001957 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001958 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001959 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001960 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1961 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1962 {
1963 .hook = ip_vs_forward_icmp,
1964 .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_FORWARD,
1967 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001968 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001969 /* After packet filtering, change source only for VS/NAT */
1970 {
1971 .hook = ip_vs_reply4,
1972 .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_FORWARD,
1975 .priority = 100,
1976 },
Julius Volz473b23d2008-09-02 15:55:54 +02001977#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001978 /* After packet filtering, change source only for VS/NAT */
1979 {
1980 .hook = ip_vs_reply6,
1981 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001982 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001983 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001984 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001985 },
Julius Volz473b23d2008-09-02 15:55:54 +02001986 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1987 * or VS/NAT(change destination), so that filtering rules can be
1988 * applied to IPVS. */
1989 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001990 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001991 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001992 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001993 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001994 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001995 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001996 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001997 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001998 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001999 .owner = THIS_MODULE,
Julian Anastasoveb90b0c2014-08-22 17:53:41 +03002000 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03002001 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002002 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02002003 },
Julian Anastasovcb591552010-10-17 16:40:51 +03002004 /* After mangle, schedule and forward local requests */
2005 {
2006 .hook = ip_vs_local_request6,
2007 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002008 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002009 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002010 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002011 },
Julius Volz473b23d2008-09-02 15:55:54 +02002012 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2013 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2014 {
2015 .hook = ip_vs_forward_icmp_v6,
2016 .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_FORWARD,
2019 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02002020 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002021 /* After packet filtering, change source only for VS/NAT */
2022 {
2023 .hook = ip_vs_reply6,
2024 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00002025 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03002026 .hooknum = NF_INET_FORWARD,
2027 .priority = 100,
2028 },
Julius Volz473b23d2008-09-02 15:55:54 +02002029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002031/*
2032 * Initialize IP Virtual Server netns mem.
2033 */
2034static int __net_init __ip_vs_init(struct net *net)
2035{
2036 struct netns_ipvs *ipvs;
2037
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002038 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07002039 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002040 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07002041
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002042 /* Hold the beast until a service is registerd */
2043 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01002044 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002045 /* Counters used for creating unique names */
2046 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2047 atomic_inc(&ipvs_netns_cnt);
2048 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002049
Eric W. Biedermana4dd0362015-09-21 13:02:28 -05002050 if (ip_vs_estimator_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002051 goto estimator_fail;
2052
Eric W. Biederman3d993762015-09-21 13:02:26 -05002053 if (ip_vs_control_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002054 goto control_fail;
2055
Eric W. Biederman7d1f88e2015-09-21 13:02:58 -05002056 if (ip_vs_protocol_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002057 goto protocol_fail;
2058
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -05002059 if (ip_vs_app_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002060 goto app_fail;
2061
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05002062 if (ip_vs_conn_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002063 goto conn_fail;
2064
Eric W. Biederman802cb432015-09-21 13:02:20 -05002065 if (ip_vs_sync_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002066 goto sync_fail;
2067
Simon Hormana870c8c2011-02-01 18:21:53 +01002068 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002069 sizeof(struct netns_ipvs), ipvs->gen);
2070 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002071/*
2072 * Error handling
2073 */
2074
2075sync_fail:
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05002076 ip_vs_conn_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002077conn_fail:
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -05002078 ip_vs_app_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002079app_fail:
Eric W. Biederman7d1f88e2015-09-21 13:02:58 -05002080 ip_vs_protocol_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002081protocol_fail:
Eric W. Biederman3d993762015-09-21 13:02:26 -05002082 ip_vs_control_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002083control_fail:
Eric W. Biedermana4dd0362015-09-21 13:02:28 -05002084 ip_vs_estimator_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002085estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03002086 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002087 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002088}
2089
2090static void __net_exit __ip_vs_cleanup(struct net *net)
2091{
Eric W. Biederman56d21692015-09-21 13:01:58 -05002092 struct netns_ipvs *ipvs = net_ipvs(net);
2093
2094 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05002095 ip_vs_conn_net_cleanup(ipvs);
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -05002096 ip_vs_app_net_cleanup(ipvs);
Eric W. Biederman7d1f88e2015-09-21 13:02:58 -05002097 ip_vs_protocol_net_cleanup(ipvs);
Eric W. Biederman3d993762015-09-21 13:02:26 -05002098 ip_vs_control_net_cleanup(ipvs);
Eric W. Biedermana4dd0362015-09-21 13:02:28 -05002099 ip_vs_estimator_net_cleanup(ipvs);
Eric W. Biederman56d21692015-09-21 13:01:58 -05002100 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002101 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002102}
2103
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002104static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2105{
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002106 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002107 EnterFunction(2);
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002108 ipvs->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002109 smp_wmb();
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002110 ip_vs_sync_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002111 LeaveFunction(2);
2112}
2113
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002114static struct pernet_operations ipvs_core_ops = {
2115 .init = __ip_vs_init,
2116 .exit = __ip_vs_cleanup,
2117 .id = &ip_vs_net_id,
2118 .size = sizeof(struct netns_ipvs),
2119};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002121static struct pernet_operations ipvs_core_dev_ops = {
2122 .exit = __ip_vs_dev_cleanup,
2123};
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125/*
2126 * Initialize IP Virtual Server
2127 */
2128static int __init ip_vs_init(void)
2129{
2130 int ret;
2131
2132 ret = ip_vs_control_init();
2133 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002134 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002135 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137
2138 ip_vs_protocol_init();
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ret = ip_vs_conn_init();
2141 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002142 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002143 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002144 }
2145
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002146 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2147 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002148 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002149
2150 ret = register_pernet_device(&ipvs_core_dev_ops);
2151 if (ret < 0)
2152 goto cleanup_sub;
2153
Patrick McHardy41c5b312007-12-05 01:22:43 -08002154 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002156 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002157 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159
Hans Schillstrom8537de82012-04-26 07:47:44 +02002160 ret = ip_vs_register_nl_ioctl();
2161 if (ret < 0) {
2162 pr_err("can't register netlink/ioctl.\n");
2163 goto cleanup_hooks;
2164 }
2165
Hannes Eder1e3e2382009-08-02 11:05:41 +00002166 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return ret;
2169
Hans Schillstrom8537de82012-04-26 07:47:44 +02002170cleanup_hooks:
2171 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002172cleanup_dev:
2173 unregister_pernet_device(&ipvs_core_dev_ops);
2174cleanup_sub:
2175 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002176cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002178cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 ip_vs_protocol_cleanup();
2180 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002181exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return ret;
2183}
2184
2185static void __exit ip_vs_cleanup(void)
2186{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002187 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002188 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002189 unregister_pernet_device(&ipvs_core_dev_ops);
2190 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 ip_vs_protocol_cleanup();
2193 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002194 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197module_init(ip_vs_init);
2198module_exit(ip_vs_cleanup);
2199MODULE_LICENSE("GPL");