blob: 2c1b498a7a271df0f6e3ffa86407b89ba103e9d9 [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
Marco Angaroni39b97222016-04-05 18:26:29 +020071EXPORT_SYMBOL(ip_vs_new_conn_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Julian Anastasovb962abd2013-03-09 23:25:08 +020073static int ip_vs_net_id __read_mostly;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010074/* netns cnt used for uniqueness */
75static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/* ID used in ICMP lookups */
78#define icmp_id(icmph) (((icmph)->un).echo.id)
Julius Volz2a3b7912008-09-02 15:55:47 +020079#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Eric Dumazet95c96172012-04-15 05:58:06 +000081const char *ip_vs_proto_name(unsigned int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 static char buf[20];
84
85 switch (proto) {
86 case IPPROTO_IP:
87 return "IP";
88 case IPPROTO_UDP:
89 return "UDP";
90 case IPPROTO_TCP:
91 return "TCP";
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010092 case IPPROTO_SCTP:
93 return "SCTP";
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 case IPPROTO_ICMP:
95 return "ICMP";
Julius Volz2a3b7912008-09-02 15:55:47 +020096#ifdef CONFIG_IP_VS_IPV6
97 case IPPROTO_ICMPV6:
98 return "ICMPv6";
99#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 default:
Masanari Iida1404c3a2014-04-21 09:16:47 +0900101 sprintf(buf, "IP_%u", proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return buf;
103 }
104}
105
106void ip_vs_init_hash_table(struct list_head *table, int rows)
107{
108 while (--rows >= 0)
109 INIT_LIST_HEAD(&table[rows]);
110}
111
112static inline void
113ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
114{
115 struct ip_vs_dest *dest = cp->dest;
Eric W. Biederman7c08d782015-09-21 13:02:48 -0500116 struct netns_ipvs *ipvs = cp->ipvs;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100119 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300120 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Hans Schillstromb17fc992011-01-03 14:44:56 +0100122 s = this_cpu_ptr(dest->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100123 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200124 s->cnt.inpkts++;
125 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100126 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300128 rcu_read_lock();
129 svc = rcu_dereference(dest->svc);
130 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100131 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200132 s->cnt.inpkts++;
133 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100134 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300135 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100136
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200137 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100138 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200139 s->cnt.inpkts++;
140 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100141 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143}
144
145
146static inline void
147ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
148{
149 struct ip_vs_dest *dest = cp->dest;
Eric W. Biederman7c08d782015-09-21 13:02:48 -0500150 struct netns_ipvs *ipvs = cp->ipvs;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100153 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300154 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Hans Schillstromb17fc992011-01-03 14:44:56 +0100156 s = this_cpu_ptr(dest->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100157 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200158 s->cnt.outpkts++;
159 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100160 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300162 rcu_read_lock();
163 svc = rcu_dereference(dest->svc);
164 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100165 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200166 s->cnt.outpkts++;
167 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100168 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300169 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100170
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200171 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100172 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200173 s->cnt.outpkts++;
174 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100175 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177}
178
179
180static inline void
181ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
182{
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500183 struct netns_ipvs *ipvs = svc->ipvs;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100184 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Hans Schillstromb17fc992011-01-03 14:44:56 +0100186 s = this_cpu_ptr(cp->dest->stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200187 u64_stats_update_begin(&s->syncp);
188 s->cnt.conns++;
189 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Hans Schillstromb17fc992011-01-03 14:44:56 +0100191 s = this_cpu_ptr(svc->stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200192 u64_stats_update_begin(&s->syncp);
193 s->cnt.conns++;
194 u64_stats_update_end(&s->syncp);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100195
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200196 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200197 u64_stats_update_begin(&s->syncp);
198 s->cnt.conns++;
199 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202
Simon Horman4a516f12011-09-16 14:11:49 +0900203static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204ip_vs_set_state(struct ip_vs_conn *cp, int direction,
205 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100206 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Simon Horman4a516f12011-09-16 14:11:49 +0900208 if (likely(pd->pp->state_transition))
209 pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Hans Schillstroma5959d52010-11-19 14:25:10 +0100212static inline int
Simon Horman85999282010-08-22 21:37:53 +0900213ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
214 struct sk_buff *skb, int protocol,
215 const union nf_inet_addr *caddr, __be16 cport,
216 const union nf_inet_addr *vaddr, __be16 vport,
217 struct ip_vs_conn_param *p)
218{
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500219 ip_vs_conn_fill_param(svc->ipvs, svc->af, protocol, caddr, cport, vaddr,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100220 vport, p);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200221 p->pe = rcu_dereference(svc->pe);
Simon Horman85999282010-08-22 21:37:53 +0900222 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100223 return p->pe->fill_param(p, skb);
224
225 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * IPVS persistent scheduling function
230 * It creates a connection entry according to its template if exists,
231 * or selects a server and creates a connection entry plus a template.
232 * Locking: we are svc user (svc->refcnt), so we hold all dests too
233 * Protocols supported: TCP, UDP
234 */
235static struct ip_vs_conn *
236ip_vs_sched_persist(struct ip_vs_service *svc,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200237 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
238 int *ignored, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct ip_vs_conn *cp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct ip_vs_dest *dest;
242 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900243 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200244 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900245 struct ip_vs_conn_param param;
Simon Hormane0aac522012-01-27 10:45:27 +0900246 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
Julius Volz28364a52008-09-02 15:55:43 +0200247 union nf_inet_addr snet; /* source network of the client,
248 after masking */
Alex Gartrellee78378f2015-08-26 09:40:33 -0700249 const union nf_inet_addr *src_addr, *dst_addr;
250
251 if (likely(!ip_vs_iph_inverse(iph))) {
252 src_addr = &iph->saddr;
253 dst_addr = &iph->daddr;
254 } else {
255 src_addr = &iph->daddr;
256 dst_addr = &iph->saddr;
257 }
258
Julius Volzcd17f9e2008-09-02 15:55:46 +0200259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200261#ifdef CONFIG_IP_VS_IPV6
262 if (svc->af == AF_INET6)
Alex Gartrellee78378f2015-08-26 09:40:33 -0700263 ipv6_addr_prefix(&snet.in6, &src_addr->in6,
Julian Anastasov0a925862013-04-17 23:50:49 +0300264 (__force __u32) svc->netmask);
Julius Volzcd17f9e2008-09-02 15:55:46 +0200265 else
266#endif
Alex Gartrellee78378f2015-08-26 09:40:33 -0700267 snet.ip = src_addr->ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Julius Volzcd17f9e2008-09-02 15:55:46 +0200269 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
270 "mnet %s\n",
Alex Gartrellee78378f2015-08-26 09:40:33 -0700271 IP_VS_DBG_ADDR(svc->af, src_addr), ntohs(src_port),
272 IP_VS_DBG_ADDR(svc->af, dst_addr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200273 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * As far as we know, FTP is a very complicated network protocol, and
277 * it uses control connection and data connections. For active FTP,
278 * FTP server initialize data connection to the client, its source port
279 * is often 20. For passive FTP, FTP server tells the clients the port
280 * that it passively listens to, and the client issues the data
281 * connection. In the tunneling or direct routing mode, the load
282 * balancer is on the client-to-server half of connection, the port
283 * number is unknown to the load balancer. So, a conn template like
284 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
285 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
286 * is created for other persistent services.
287 */
Simon Horman5b57a982010-08-22 21:37:51 +0900288 {
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200289 int protocol = iph->protocol;
Alex Gartrellee78378f2015-08-26 09:40:33 -0700290 const union nf_inet_addr *vaddr = dst_addr;
Simon Hormanf11017e2010-08-22 21:37:52 +0900291 __be16 vport = 0;
292
Hans Schillstromce144f22010-11-19 14:25:08 +0100293 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900294 /* non-FTP template:
295 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
296 * FTP template:
297 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100300 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900302 /* Note: persistent fwmark-based services and
303 * persistent port zero service are handled here.
304 * fwmark template:
305 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
306 * port zero template:
307 * <protocol,caddr,0,vaddr,0,daddr,0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Julius Volz28364a52008-09-02 15:55:43 +0200309 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900310 protocol = IPPROTO_IP;
311 vaddr = &fwmark;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100314 /* return *ignored = -1 so NF_DROP can be used */
315 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
316 vaddr, vport, &param) < 0) {
317 *ignored = -1;
318 return NULL;
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Simon Horman5b57a982010-08-22 21:37:51 +0900322 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900323 ct = ip_vs_ct_in_get(&param);
Marco Angaroni3ec10d32016-05-16 19:18:09 +0200324 if (!ct || !ip_vs_check_template(ct, NULL)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200325 struct ip_vs_scheduler *sched;
326
Hans Schillstroma5959d52010-11-19 14:25:10 +0100327 /*
328 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900329 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100330 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900331 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200332 sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +0300333 if (sched) {
334 /* read svc->sched_data after svc->scheduler */
335 smp_rmb();
336 dest = sched->schedule(svc, skb, iph);
337 } else {
338 dest = NULL;
339 }
Simon Horman5b57a982010-08-22 21:37:51 +0900340 if (!dest) {
341 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900342 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100343 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900344 return NULL;
345 }
346
Hans Schillstromce144f22010-11-19 14:25:08 +0100347 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900348 dport = dest->port;
349
Simon Horman85999282010-08-22 21:37:53 +0900350 /* Create a template
351 * This adds param.pe_data to the template,
352 * and thus param.pe_data will be destroyed
353 * when the template expires */
Alex Gartrellba385282014-09-09 16:40:23 -0700354 ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100355 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900356 if (ct == NULL) {
357 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100358 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900359 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900360 }
Simon Horman5b57a982010-08-22 21:37:51 +0900361
362 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900363 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900364 /* set destination with the found template */
365 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900366 kfree(param.pe_data);
367 }
Simon Horman5b57a982010-08-22 21:37:51 +0900368
Hans Schillstromce144f22010-11-19 14:25:08 +0100369 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900370 if (dport == svc->port && dest->port)
371 dport = dest->port;
372
Nick Chalk26ec0372010-06-22 08:07:01 +0200373 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200374 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200375 IP_VS_CONN_F_ONE_PACKET : 0;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Create a new connection according to the template
379 */
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500380 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol, src_addr,
Alex Gartrellee78378f2015-08-26 09:40:33 -0700381 src_port, dst_addr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100382
Alex Gartrellba385282014-09-09 16:40:23 -0700383 cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
384 skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (cp == NULL) {
386 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100387 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return NULL;
389 }
390
391 /*
392 * Add its control
393 */
394 ip_vs_control_add(cp, ct);
395 ip_vs_conn_put(ct);
396
397 ip_vs_conn_stats(cp, svc);
398 return cp;
399}
400
401
402/*
403 * IPVS main scheduling function
404 * It selects a server according to the virtual service, and
405 * creates a connection entry.
406 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100407 *
408 * Usage of *ignored
409 *
410 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
411 * svc/scheduler decides that this packet should be accepted with
412 * NF_ACCEPT because it must not be scheduled.
413 *
414 * 0 : scheduler can not find destination, so try bypass or
415 * return ICMP and then NF_DROP (ip_vs_leave).
416 *
417 * -1 : scheduler tried to schedule but fatal error occurred, eg.
418 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
419 * failure such as missing Call-ID, ENOMEM on skb_linearize
420 * or pe_data. In this case we should return NF_DROP without
421 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
423struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300424ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200425 struct ip_vs_proto_data *pd, int *ignored,
426 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Hans Schillstrom93304192011-01-03 14:44:51 +0100428 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct ip_vs_conn *cp = NULL;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200430 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct ip_vs_dest *dest;
Alex Gartrellee78378f2015-08-26 09:40:33 -0700432 __be16 _ports[2], *pptr, cport, vport;
433 const void *caddr, *vaddr;
Julian Anastasov35757922010-09-17 14:18:16 +0200434 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Julian Anastasov190ecd22010-10-17 16:24:37 +0300436 *ignored = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200437 /*
438 * IPv6 frags, only the first hit here.
439 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200440 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (pptr == NULL)
442 return NULL;
443
Alex Gartrellee78378f2015-08-26 09:40:33 -0700444 if (likely(!ip_vs_iph_inverse(iph))) {
445 cport = pptr[0];
446 caddr = &iph->saddr;
447 vport = pptr[1];
448 vaddr = &iph->daddr;
449 } else {
450 cport = pptr[1];
451 caddr = &iph->daddr;
452 vport = pptr[0];
453 vaddr = &iph->saddr;
454 }
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300457 * FTPDATA needs this check when using local real server.
458 * Never schedule Active FTPDATA connections from real server.
459 * For LVS-NAT they must be already created. For other methods
460 * with persistence the connection is created on SYN+ACK.
461 */
Alex Gartrellee78378f2015-08-26 09:40:33 -0700462 if (cport == FTPDATA) {
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700463 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
Julian Anastasov0d796412010-10-17 16:46:17 +0300464 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300465 return NULL;
466 }
467
468 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100469 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300470 */
Alex Gartrell802c41a2015-08-26 09:40:32 -0700471 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK)) {
472 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
Eric W. Biedermanab161972015-09-21 13:02:38 -0500473 cp = pp->conn_in_get(svc->ipvs, svc->af, skb, iph);
Alex Gartrell802c41a2015-08-26 09:40:32 -0700474 iph->hdr_flags ^= IP_VS_HDR_INVERSE;
475
476 if (cp) {
477 IP_VS_DBG_PKT(12, svc->af, pp, skb, iph->off,
478 "Not scheduling reply for existing"
479 " connection");
480 __ip_vs_conn_put(cp);
481 return NULL;
482 }
Julian Anastasov190ecd22010-10-17 16:24:37 +0300483 }
484
485 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * Persistent service
487 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100488 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
Alex Gartrellee78378f2015-08-26 09:40:33 -0700489 return ip_vs_sched_persist(svc, skb, cport, vport, ignored,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200490 iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100491
492 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /*
495 * Non-persistent service
496 */
Alex Gartrellee78378f2015-08-26 09:40:33 -0700497 if (!svc->fwmark && vport != svc->port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000499 pr_err("Schedule: port zero only supported "
500 "in persistent services, "
501 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return NULL;
503 }
504
Julian Anastasovceec4c32013-03-22 11:46:53 +0200505 sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +0300506 if (sched) {
507 /* read svc->sched_data after svc->scheduler */
508 smp_rmb();
509 dest = sched->schedule(svc, skb, iph);
510 } else {
511 dest = NULL;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (dest == NULL) {
514 IP_VS_DBG(1, "Schedule: no dest found.\n");
515 return NULL;
516 }
517
Nick Chalk26ec0372010-06-22 08:07:01 +0200518 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200519 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200520 IP_VS_CONN_F_ONE_PACKET : 0;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * Create a connection entry.
524 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900525 {
526 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100527
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500528 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
Alex Gartrellee78378f2015-08-26 09:40:33 -0700529 caddr, cport, vaddr, vport, &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700530 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
Alex Gartrellee78378f2015-08-26 09:40:33 -0700531 dest->port ? dest->port : vport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100532 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100533 if (!cp) {
534 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900535 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100536 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Julius Volzcd17f9e2008-09-02 15:55:46 +0200539 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
540 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
541 ip_vs_fwd_tag(cp),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700542 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
543 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
544 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200545 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 ip_vs_conn_stats(cp, svc);
548 return cp;
549}
550
Alex Gartrell0b729022015-08-26 09:40:30 -0700551static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/*
562 * Pass or drop the packet.
563 * Called by ip_vs_in, when the virtual service is available but
564 * no destination is available for a new connection.
565 */
566int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200567 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Alex Gartrell0b729022015-08-26 09:40:30 -0700569 __be16 _ports[2], *pptr, dport;
Eric W. Biederman51efbcb2015-09-21 13:02:50 -0500570 struct netns_ipvs *ipvs = svc->ipvs;
571 struct net *net = ipvs->net;
Hans Schillstrom93304192011-01-03 14:44:51 +0100572
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200573 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Alex Gartrell0b729022015-08-26 09:40:30 -0700574 if (!pptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return NF_DROP;
Alex Gartrell0b729022015-08-26 09:40:30 -0700576 dport = likely(!ip_vs_iph_inverse(iph)) ? pptr[1] : pptr[0];
Simon Hormana7a86b82011-02-04 18:33:02 +0900577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200579 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 a cache_bypass connection entry */
Eric W. Biederman57032942015-09-21 13:02:49 -0500581 if (sysctl_cache_bypass(ipvs) && svc->fwmark &&
Alex Gartrell0b729022015-08-26 09:40:30 -0700582 !(iph->hdr_flags & (IP_VS_HDR_INVERSE | IP_VS_HDR_ICMP)) &&
583 ip_vs_addr_is_unicast(net, svc->af, &iph->daddr)) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100584 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200586 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200587 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200588 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000589 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000592 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900593 {
594 struct ip_vs_conn_param p;
Eric W. Biederman3109d2f2015-09-21 13:01:44 -0500595 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200596 &iph->saddr, pptr[0],
597 &iph->daddr, pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700598 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
Simon Hormanf11017e2010-08-22 21:37:52 +0900599 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100600 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900601 if (!cp)
602 return NF_DROP;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* statistics */
606 ip_vs_in_stats(cp, skb);
607
608 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900609 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200612 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* do not touch skb anymore */
614
Marco Angaroni698e2a82016-04-26 21:20:22 +0200615 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
616 atomic_inc(&cp->control->in_pkts);
617 else
618 atomic_inc(&cp->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ip_vs_conn_put(cp);
620 return ret;
621 }
622
623 /*
624 * When the virtual ftp service is presented, packets destined
625 * for other services on the VIP may get here (except services
626 * listed in the ipvs table), pass the packets, because it is
627 * not ipvs job to decide to drop the packets.
628 */
Alex Gartrell0b729022015-08-26 09:40:30 -0700629 if (svc->port == FTPPORT && dport != FTPPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Alex Gartrell0b729022015-08-26 09:40:30 -0700632 if (unlikely(ip_vs_iph_icmp(iph)))
633 return NF_DROP;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * Notify the client that the destination is unreachable, and
637 * release the socket buffer.
638 * Since it is in IP layer, the TCP socket is not actually
639 * created, the TCP RST packet cannot be sent, instead that
640 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
641 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200642#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300643 if (svc->af == AF_INET6) {
Eric W. Biederman57032942015-09-21 13:02:49 -0500644 if (!skb->dev)
645 skb->dev = net->loopback_dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000646 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300647 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200648#endif
649 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return NF_DROP;
652}
653
Simon Horman84b3cee2011-02-04 18:33:01 +0900654#ifdef CONFIG_SYSCTL
655
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500656static int sysctl_snat_reroute(struct netns_ipvs *ipvs)
Simon Horman84b3cee2011-02-04 18:33:01 +0900657{
Simon Horman84b3cee2011-02-04 18:33:01 +0900658 return ipvs->sysctl_snat_reroute;
659}
660
Eric W. Biederman2300f042015-09-21 13:02:51 -0500661static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs)
Simon Horman0cfa5582011-02-04 18:33:01 +0900662{
Simon Horman0cfa5582011-02-04 18:33:01 +0900663 return ipvs->sysctl_nat_icmp_send;
664}
665
Simon Horman71a8ab62011-02-04 18:33:01 +0900666static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
667{
668 return ipvs->sysctl_expire_nodest_conn;
669}
670
Simon Horman84b3cee2011-02-04 18:33:01 +0900671#else
672
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500673static int sysctl_snat_reroute(struct netns_ipvs *ipvs) { return 0; }
Eric W. Biederman2300f042015-09-21 13:02:51 -0500674static int sysctl_nat_icmp_send(struct netns_ipvs *ipvs) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900675static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900676
677#endif
678
Al Virob1550f22006-11-14 21:37:50 -0800679__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Al Virod3bc23e2006-11-14 21:24:49 -0800681 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300684static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
685{
686 if (NF_INET_LOCAL_IN == hooknum)
687 return IP_DEFRAG_VS_IN;
688 if (NF_INET_FORWARD == hooknum)
689 return IP_DEFRAG_VS_FWD;
690 return IP_DEFRAG_VS_OUT;
691}
692
Eric W. Biederman57781c12015-09-21 13:03:01 -0500693static inline int ip_vs_gather_frags(struct netns_ipvs *ipvs,
694 struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Julian Anastasovac692692013-03-22 11:46:54 +0200696 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700697
Julian Anastasovac692692013-03-22 11:46:54 +0200698 local_bh_disable();
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500699 err = ip_defrag(ipvs->net, skb, user);
Julian Anastasovac692692013-03-22 11:46:54 +0200700 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700701 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700702 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700703
704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500707static int ip_vs_route_me_harder(struct netns_ipvs *ipvs, int af,
708 struct sk_buff *skb, unsigned int hooknum)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900709{
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500710 if (!sysctl_snat_reroute(ipvs))
Julian Anastasov579eb622014-12-18 22:41:23 +0200711 return 0;
712 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
713 if (NF_INET_LOCAL_IN == hooknum)
714 return 0;
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900715#ifdef CONFIG_IP_VS_IPV6
716 if (af == AF_INET6) {
Julian Anastasov579eb622014-12-18 22:41:23 +0200717 struct dst_entry *dst = skb_dst(skb);
718
719 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
Eric W. Biederman5f5d74d2015-09-25 15:07:31 -0500720 ip6_route_me_harder(ipvs->net, skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900721 return 1;
722 } else
723#endif
Julian Anastasov579eb622014-12-18 22:41:23 +0200724 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
Eric W. Biedermane45f5062015-09-25 15:07:30 -0500725 ip_route_me_harder(ipvs->net, skb, RTN_LOCAL) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900726 return 1;
727
728 return 0;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * Packet has been made sufficiently writable in caller
733 * - inout: 1=in->out, 0=out->in
734 */
735void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
736 struct ip_vs_conn *cp, int inout)
737{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700738 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700740 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
741 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
743
744 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200745 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200747 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 ip_send_check(ciph);
749 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200750 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200752 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 ip_send_check(ciph);
754 }
755
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100756 /* the TCP/UDP/SCTP port */
757 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
758 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700759 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (inout)
762 ports[1] = cp->vport;
763 else
764 ports[0] = cp->dport;
765 }
766
767 /* And finally the ICMP checksum */
768 icmph->checksum = 0;
769 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
770 skb->ip_summed = CHECKSUM_UNNECESSARY;
771
772 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300773 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 "Forwarding altered outgoing ICMP");
775 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300776 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 "Forwarding altered incoming ICMP");
778}
779
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200780#ifdef CONFIG_IP_VS_IPV6
781void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
782 struct ip_vs_conn *cp, int inout)
783{
784 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200785 unsigned int icmp_offset = 0;
786 unsigned int offs = 0; /* header offset*/
787 int protocol;
788 struct icmp6hdr *icmph;
789 struct ipv6hdr *ciph;
790 unsigned short fragoffs;
791
792 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
793 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
794 offs = icmp_offset + sizeof(struct icmp6hdr);
795 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
796
797 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200798
799 if (inout) {
800 iph->saddr = cp->vaddr.in6;
801 ciph->daddr = cp->vaddr.in6;
802 } else {
803 iph->daddr = cp->daddr.in6;
804 ciph->saddr = cp->daddr.in6;
805 }
806
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100807 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200808 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
809 IPPROTO_SCTP == protocol)) {
810 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200811
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200812 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
813 ntohs(inout ? ports[1] : ports[0]),
814 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200815 if (inout)
816 ports[1] = cp->vport;
817 else
818 ports[0] = cp->dport;
819 }
820
821 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700822 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
823 skb->len - icmp_offset,
824 IPPROTO_ICMPV6, 0);
825 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
826 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
827 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200828
829 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300830 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
831 (void *)ciph - (void *)iph,
832 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200833 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300834 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
835 (void *)ciph - (void *)iph,
836 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200837}
838#endif
839
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000840/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200841 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000842 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000843static int handle_response_icmp(int af, struct sk_buff *skb,
844 union nf_inet_addr *snet,
845 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000846 struct ip_vs_protocol *pp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200847 unsigned int offset, unsigned int ihl,
848 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000849{
850 unsigned int verdict = NF_DROP;
851
852 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000853 pr_err("shouldn't reach here, because the box is on the "
854 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000855 }
856
857 /* Ensure the checksum is correct */
858 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
859 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000860 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
861 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000862 goto out;
863 }
864
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100865 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
866 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000867 offset += 2 * sizeof(__u16);
868 if (!skb_make_writable(skb, offset))
869 goto out;
870
Simon Hormanf2428ed2008-09-05 11:17:14 +1000871#ifdef CONFIG_IP_VS_IPV6
872 if (af == AF_INET6)
873 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
874 else
875#endif
876 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000877
Eric W. Biedermana43d1a62015-09-21 13:02:56 -0500878 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900879 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300880
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000881 /* do the statistics and put it back */
882 ip_vs_out_stats(cp, skb);
883
Julian Anastasovcf356d62010-10-17 16:21:07 +0300884 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200885 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300886 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200887 else
888 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000889 verdict = NF_ACCEPT;
890
891out:
892 __ip_vs_conn_put(cp);
893
894 return verdict;
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/*
898 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000899 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Eric W. Biederman7b5f6892015-09-21 13:02:55 -0500902static int ip_vs_out_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb,
903 int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct iphdr *iph;
906 struct icmphdr _icmph, *ic;
907 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200908 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct ip_vs_conn *cp;
910 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000911 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000912 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 *related = 1;
915
916 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700917 if (ip_is_fragment(ip_hdr(skb))) {
Eric W. Biederman57781c12015-09-21 13:03:01 -0500918 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700922 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 offset = ihl = iph->ihl * 4;
924 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
925 if (ic == NULL)
926 return NF_DROP;
927
Harvey Harrison14d5e832008-10-31 00:54:29 -0700928 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700930 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /*
933 * Work through seeing if this is for us.
934 * These checks are supposed to be in an order that means easy
935 * things are checked first to speed up processing.... however
936 * this means that some packets will manage to get a long way
937 * down this stack and then be rejected, but that's life.
938 */
939 if ((ic->type != ICMP_DEST_UNREACH) &&
940 (ic->type != ICMP_SOURCE_QUENCH) &&
941 (ic->type != ICMP_TIME_EXCEEDED)) {
942 *related = 0;
943 return NF_ACCEPT;
944 }
945
946 /* Now find the contained IP header */
947 offset += sizeof(_icmph);
948 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
949 if (cih == NULL)
950 return NF_ACCEPT; /* The packet looks wrong, ignore */
951
952 pp = ip_vs_proto_get(cih->protocol);
953 if (!pp)
954 return NF_ACCEPT;
955
956 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900957 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 pp->dont_defrag))
959 return NF_ACCEPT;
960
Julian Anastasov0d796412010-10-17 16:46:17 +0300961 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
962 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Alex Gartrell4fd9bee2015-08-26 09:40:29 -0700964 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, true, &ciph);
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* The embedded headers contain source and dest in reverse order */
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -0500967 cp = pp->conn_out_get(ipvs, AF_INET, skb, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!cp)
969 return NF_ACCEPT;
970
Simon Hormanf2428ed2008-09-05 11:17:14 +1000971 snet.ip = iph->saddr;
972 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200973 pp, ciph.len, ihl, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Julius Volz2a3b7912008-09-02 15:55:47 +0200976#ifdef CONFIG_IP_VS_IPV6
Eric W. Biederman7b5f6892015-09-21 13:02:55 -0500977static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
978 int *related, unsigned int hooknum,
979 struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200980{
Julius Volz2a3b7912008-09-02 15:55:47 +0200981 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200982 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200983 struct ip_vs_conn *cp;
984 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000985 union nf_inet_addr snet;
Alex Gartrellb0e010c2015-08-26 09:40:28 -0700986 unsigned int offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200987
Julius Volz2a3b7912008-09-02 15:55:47 +0200988 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200989 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200990 if (ic == NULL)
991 return NF_DROP;
992
Julius Volz2a3b7912008-09-02 15:55:47 +0200993 /*
994 * Work through seeing if this is for us.
995 * These checks are supposed to be in an order that means easy
996 * things are checked first to speed up processing.... however
997 * this means that some packets will manage to get a long way
998 * down this stack and then be rejected, but that's life.
999 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001000 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001001 *related = 0;
1002 return NF_ACCEPT;
1003 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001004 /* Fragment header that is before ICMP header tells us that:
1005 * it's not an error message since they can't be fragmented.
1006 */
David S. Millere7165032012-11-30 12:01:30 -05001007 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001008 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001009
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001010 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1011 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1012 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001013
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001014 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, ipvsh->len + sizeof(_icmph),
1015 true, &ciph))
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001016 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001017
1018 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +02001019 if (!pp)
1020 return NF_ACCEPT;
1021
Julius Volz2a3b7912008-09-02 15:55:47 +02001022 /* The embedded headers contain source and dest in reverse order */
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -05001023 cp = pp->conn_out_get(ipvs, AF_INET6, skb, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001024 if (!cp)
1025 return NF_ACCEPT;
1026
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001027 snet.in6 = ciph.saddr.in6;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001028 offset = ciph.len;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001029 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001030 pp, offset, sizeof(struct ipv6hdr),
Julian Anastasov579eb622014-12-18 22:41:23 +02001031 hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001032}
1033#endif
1034
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001035/*
1036 * Check if sctp chunc is ABORT chunk
1037 */
1038static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1039{
1040 sctp_chunkhdr_t *sch, schunk;
1041 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1042 sizeof(schunk), &schunk);
1043 if (sch == NULL)
1044 return 0;
1045 if (sch->type == SCTP_CID_ABORT)
1046 return 1;
1047 return 0;
1048}
1049
Julius Volz2a3b7912008-09-02 15:55:47 +02001050static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct tcphdr _tcph, *th;
1053
Julius Volz2a3b7912008-09-02 15:55:47 +02001054 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (th == NULL)
1056 return 0;
1057 return th->rst;
1058}
1059
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001060static inline bool is_new_conn(const struct sk_buff *skb,
1061 struct ip_vs_iphdr *iph)
1062{
1063 switch (iph->protocol) {
1064 case IPPROTO_TCP: {
1065 struct tcphdr _tcph, *th;
1066
1067 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1068 if (th == NULL)
1069 return false;
1070 return th->syn;
1071 }
1072 case IPPROTO_SCTP: {
1073 sctp_chunkhdr_t *sch, schunk;
1074
1075 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1076 sizeof(schunk), &schunk);
1077 if (sch == NULL)
1078 return false;
1079 return sch->type == SCTP_CID_INIT;
1080 }
1081 default:
1082 return false;
1083 }
1084}
1085
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001086static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1087 int conn_reuse_mode)
1088{
1089 /* Controlled (FTP DATA or persistence)? */
1090 if (cp->control)
1091 return false;
1092
1093 switch (cp->protocol) {
1094 case IPPROTO_TCP:
1095 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
Julian Anastasovf911b672016-03-05 15:03:23 +02001096 (cp->state == IP_VS_TCP_S_CLOSE) ||
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001097 ((conn_reuse_mode & 2) &&
1098 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1099 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1100 case IPPROTO_SCTP:
1101 return cp->state == IP_VS_SCTP_S_CLOSED;
1102 default:
1103 return false;
1104 }
1105}
1106
Marco Angaroni39b97222016-04-05 18:26:29 +02001107/* Generic function to create new connections for outgoing RS packets
1108 *
1109 * Pre-requisites for successful connection creation:
1110 * 1) Virtual Service is NOT fwmark based:
1111 * In fwmark-VS actual vaddr and vport are unknown to IPVS
1112 * 2) Real Server and Virtual Service were NOT configured without port:
1113 * This is to allow match of different VS to the same RS ip-addr
1114 */
1115struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
1116 struct ip_vs_dest *dest,
1117 struct sk_buff *skb,
1118 const struct ip_vs_iphdr *iph,
1119 __be16 dport,
1120 __be16 cport)
1121{
1122 struct ip_vs_conn_param param;
1123 struct ip_vs_conn *ct = NULL, *cp = NULL;
1124 const union nf_inet_addr *vaddr, *daddr, *caddr;
1125 union nf_inet_addr snet;
1126 __be16 vport;
1127 unsigned int flags;
1128
1129 EnterFunction(12);
1130 vaddr = &svc->addr;
1131 vport = svc->port;
1132 daddr = &iph->saddr;
1133 caddr = &iph->daddr;
1134
1135 /* check pre-requisites are satisfied */
1136 if (svc->fwmark)
1137 return NULL;
1138 if (!vport || !dport)
1139 return NULL;
1140
1141 /* for persistent service first create connection template */
1142 if (svc->flags & IP_VS_SVC_F_PERSISTENT) {
1143 /* apply netmask the same way ingress-side does */
1144#ifdef CONFIG_IP_VS_IPV6
1145 if (svc->af == AF_INET6)
1146 ipv6_addr_prefix(&snet.in6, &caddr->in6,
1147 (__force __u32)svc->netmask);
1148 else
1149#endif
1150 snet.ip = caddr->ip & svc->netmask;
1151 /* fill params and create template if not existent */
1152 if (ip_vs_conn_fill_param_persist(svc, skb, iph->protocol,
1153 &snet, 0, vaddr,
1154 vport, &param) < 0)
1155 return NULL;
1156 ct = ip_vs_ct_in_get(&param);
Marco Angaroni3ec10d32016-05-16 19:18:09 +02001157 /* check if template exists and points to the same dest */
1158 if (!ct || !ip_vs_check_template(ct, dest)) {
Marco Angaroni39b97222016-04-05 18:26:29 +02001159 ct = ip_vs_conn_new(&param, dest->af, daddr, dport,
1160 IP_VS_CONN_F_TEMPLATE, dest, 0);
1161 if (!ct) {
1162 kfree(param.pe_data);
1163 return NULL;
1164 }
1165 ct->timeout = svc->timeout;
1166 } else {
1167 kfree(param.pe_data);
1168 }
1169 }
1170
1171 /* connection flags */
1172 flags = ((svc->flags & IP_VS_SVC_F_ONEPACKET) &&
1173 iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
1174 /* create connection */
1175 ip_vs_conn_fill_param(svc->ipvs, svc->af, iph->protocol,
1176 caddr, cport, vaddr, vport, &param);
1177 cp = ip_vs_conn_new(&param, dest->af, daddr, dport, flags, dest, 0);
1178 if (!cp) {
1179 if (ct)
1180 ip_vs_conn_put(ct);
1181 return NULL;
1182 }
1183 if (ct) {
1184 ip_vs_control_add(cp, ct);
1185 ip_vs_conn_put(ct);
1186 }
1187 ip_vs_conn_stats(cp, svc);
1188
1189 /* return connection (will be used to handle outgoing packet) */
1190 IP_VS_DBG_BUF(6, "New connection RS-initiated:%c c:%s:%u v:%s:%u "
1191 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
1192 ip_vs_fwd_tag(cp),
1193 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
1194 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
1195 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
1196 cp->flags, atomic_read(&cp->refcnt));
1197 LeaveFunction(12);
1198 return cp;
1199}
1200
1201/* Handle outgoing packets which are considered requests initiated by
1202 * real servers, so that subsequent responses from external client can be
1203 * routed to the right real server.
1204 * Used also for outgoing responses in OPS mode.
1205 *
1206 * Connection management is handled by persistent-engine specific callback.
1207 */
1208static struct ip_vs_conn *__ip_vs_rs_conn_out(unsigned int hooknum,
1209 struct netns_ipvs *ipvs,
1210 int af, struct sk_buff *skb,
1211 const struct ip_vs_iphdr *iph)
1212{
1213 struct ip_vs_dest *dest;
1214 struct ip_vs_conn *cp = NULL;
1215 __be16 _ports[2], *pptr;
1216
1217 if (hooknum == NF_INET_LOCAL_IN)
1218 return NULL;
1219
1220 pptr = frag_safe_skb_hp(skb, iph->len,
1221 sizeof(_ports), _ports, iph);
1222 if (!pptr)
1223 return NULL;
1224
1225 rcu_read_lock();
1226 dest = ip_vs_find_real_service(ipvs, af, iph->protocol,
1227 &iph->saddr, pptr[0]);
1228 if (dest) {
1229 struct ip_vs_service *svc;
1230 struct ip_vs_pe *pe;
1231
1232 svc = rcu_dereference(dest->svc);
1233 if (svc) {
1234 pe = rcu_dereference(svc->pe);
1235 if (pe && pe->conn_out)
1236 cp = pe->conn_out(svc, dest, skb, iph,
1237 pptr[0], pptr[1]);
1238 }
1239 }
1240 rcu_read_unlock();
1241
1242 return cp;
1243}
1244
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001245/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001246 */
1247static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001248handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Julian Anastasov579eb622014-12-18 22:41:23 +02001249 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1250 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001251{
Hans Schillstrom93304192011-01-03 14:44:51 +01001252 struct ip_vs_protocol *pp = pd->pp;
1253
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001254 IP_VS_DBG_PKT(11, af, pp, skb, iph->off, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001255
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001256 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001257 goto drop;
1258
1259 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001260 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001261 goto drop;
1262
1263#ifdef CONFIG_IP_VS_IPV6
1264 if (af == AF_INET6)
1265 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1266 else
1267#endif
1268 {
1269 ip_hdr(skb)->saddr = cp->vaddr.ip;
1270 ip_send_check(ip_hdr(skb));
1271 }
1272
Julian Anastasov8a803042010-09-21 17:38:57 +02001273 /*
1274 * nf_iterate does not expect change in the skb->dst->dev.
1275 * It looks like it is not fatal to enable this code for hooks
1276 * where our handlers are at the end of the chain list and
1277 * when all next handlers use skb->dst->dev and not outdev.
1278 * It will definitely route properly the inout NAT traffic
1279 * when multiple paths are used.
1280 */
1281
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001282 /* For policy routing, packets originating from this
1283 * machine itself may be routed differently to packets
1284 * passing through. We want this packet to be routed as
1285 * if it came from this machine itself. So re-compute
1286 * the routing information.
1287 */
Eric W. Biedermana43d1a62015-09-21 13:02:56 -05001288 if (ip_vs_route_me_harder(cp->ipvs, af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001289 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001290
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001291 IP_VS_DBG_PKT(10, af, pp, skb, iph->off, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001292
1293 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001294 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001295 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001296 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001297 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001298 else
1299 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001300 ip_vs_conn_put(cp);
1301
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001302 LeaveFunction(11);
1303 return NF_ACCEPT;
1304
1305drop:
1306 ip_vs_conn_put(cp);
1307 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001308 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001309 return NF_STOLEN;
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001313 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
1315static unsigned int
Eric W. Biederman1b750972015-09-21 13:02:52 -05001316ip_vs_out(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Julius Volz51ef3482008-09-02 15:55:40 +02001318 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001320 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct ip_vs_conn *cp;
Eric Dumazet340c78e2015-11-12 09:14:12 -08001322 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 EnterFunction(11);
1325
Julian Anastasovfc604762010-10-17 16:38:15 +03001326 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001327 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return NF_ACCEPT;
1329
Eric Dumazet340c78e2015-11-12 09:14:12 -08001330 sk = skb_to_full_sk(skb);
Julian Anastasovfc604762010-10-17 16:38:15 +03001331 /* Bad... Do not break raw sockets */
Eric Dumazet340c78e2015-11-12 09:14:12 -08001332 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
Julian Anastasovfc604762010-10-17 16:38:15 +03001333 af == AF_INET)) {
Julian Anastasovfc604762010-10-17 16:38:15 +03001334
Eric Dumazet340c78e2015-11-12 09:14:12 -08001335 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
Julian Anastasovfc604762010-10-17 16:38:15 +03001336 return NF_ACCEPT;
1337 }
1338
1339 if (unlikely(!skb_dst(skb)))
1340 return NF_ACCEPT;
1341
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001342 if (!ipvs->enable)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001343 return NF_ACCEPT;
1344
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001345 ip_vs_fill_iph_skb(af, skb, false, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001346#ifdef CONFIG_IP_VS_IPV6
1347 if (af == AF_INET6) {
1348 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001349 int related;
Eric W. Biederman7b5f6892015-09-21 13:02:55 -05001350 int verdict = ip_vs_out_icmp_v6(ipvs, skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001351 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Julian Anastasovf5a41842010-10-17 16:35:46 +03001353 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001354 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001355 }
1356 } else
1357#endif
1358 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001359 int related;
Eric W. Biederman7b5f6892015-09-21 13:02:55 -05001360 int verdict = ip_vs_out_icmp(ipvs, skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001361
Julian Anastasovf5a41842010-10-17 16:35:46 +03001362 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001363 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001366 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001367 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001369 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001372#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001373 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001374#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001375 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Eric W. Biederman57781c12015-09-21 13:03:01 -05001376 if (ip_vs_gather_frags(ipvs, skb,
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001377 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001378 return NF_STOLEN;
1379
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001380 ip_vs_fill_iph_skb(AF_INET, skb, false, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
1384 * Check if the packet belongs to an existing entry
1385 */
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -05001386 cp = pp->conn_out_get(ipvs, af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Julian Anastasovcb591552010-10-17 16:40:51 +03001388 if (likely(cp))
Julian Anastasov579eb622014-12-18 22:41:23 +02001389 return handle_response(af, skb, pd, cp, &iph, hooknum);
Marco Angaroni39b97222016-04-05 18:26:29 +02001390
1391 /* Check for real-server-started requests */
1392 if (atomic_read(&ipvs->conn_out_counter)) {
1393 /* Currently only for UDP:
1394 * connection oriented protocols typically use
1395 * ephemeral ports for outgoing connections, so
1396 * related incoming responses would not match any VS
1397 */
1398 if (pp->protocol == IPPROTO_UDP) {
1399 cp = __ip_vs_rs_conn_out(hooknum, ipvs, af, skb, &iph);
1400 if (likely(cp))
1401 return handle_response(af, skb, pd, cp, &iph,
1402 hooknum);
1403 }
1404 }
1405
Eric W. Biederman2300f042015-09-21 13:02:51 -05001406 if (sysctl_nat_icmp_send(ipvs) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001407 (pp->protocol == IPPROTO_TCP ||
1408 pp->protocol == IPPROTO_UDP ||
1409 pp->protocol == IPPROTO_SCTP)) {
1410 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001412 pptr = frag_safe_skb_hp(skb, iph.len,
1413 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001414 if (pptr == NULL)
1415 return NF_ACCEPT; /* Not for me */
Eric W. Biederman48aed1b2015-09-21 13:01:50 -05001416 if (ip_vs_has_real_service(ipvs, af, iph.protocol, &iph.saddr,
Julian Anastasov276472e2013-03-21 11:58:08 +02001417 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001418 /*
1419 * Notify the real server: there is no
1420 * existing entry if it is not RST
1421 * packet or not TCP packet.
1422 */
1423 if ((iph.protocol != IPPROTO_TCP &&
1424 iph.protocol != IPPROTO_SCTP)
1425 || ((iph.protocol == IPPROTO_TCP
1426 && !is_tcp_reset(skb, iph.len))
1427 || (iph.protocol == IPPROTO_SCTP
1428 && !is_sctp_abort(skb,
1429 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001430#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001431 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001432 if (!skb->dev)
David Aherned1c9f02015-10-01 08:49:04 -07001433 skb->dev = ipvs->net->loopback_dev;
Julian Anastasovcb591552010-10-17 16:40:51 +03001434 icmpv6_send(skb,
1435 ICMPV6_DEST_UNREACH,
1436 ICMPV6_PORT_UNREACH,
1437 0);
1438 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001439#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001440 icmp_send(skb,
1441 ICMP_DEST_UNREACH,
1442 ICMP_PORT_UNREACH, 0);
1443 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001447 IP_VS_DBG_PKT(12, af, pp, skb, iph.off,
Julian Anastasovcb591552010-10-17 16:40:51 +03001448 "ip_vs_out: packet continues traversal as normal");
1449 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Julian Anastasovfc604762010-10-17 16:38:15 +03001452/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001453 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1454 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001455 * Check if packet is reply for established ip_vs_conn.
1456 */
1457static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001458ip_vs_reply4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001459 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001460{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001461 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001462}
1463
1464/*
1465 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1466 * Check if packet is reply for established ip_vs_conn.
1467 */
1468static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001469ip_vs_local_reply4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001470 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001471{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001472 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001473}
1474
1475#ifdef CONFIG_IP_VS_IPV6
1476
1477/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001478 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1479 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001480 * Check if packet is reply for established ip_vs_conn.
1481 */
1482static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001483ip_vs_reply6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001484 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001485{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001486 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001487}
1488
1489/*
1490 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1491 * Check if packet is reply for established ip_vs_conn.
1492 */
1493static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05001494ip_vs_local_reply6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001495 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001496{
Eric W. Biederman1b750972015-09-21 13:02:52 -05001497 return ip_vs_out(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001498}
1499
1500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001502static unsigned int
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001503ip_vs_try_to_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,
1504 struct ip_vs_proto_data *pd,
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001505 int *verdict, struct ip_vs_conn **cpp,
1506 struct ip_vs_iphdr *iph)
1507{
1508 struct ip_vs_protocol *pp = pd->pp;
1509
1510 if (!iph->fragoffs) {
1511 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1512 * replayed fragment zero will already have created the cp
1513 */
1514
1515 /* Schedule and create new connection entry into cpp */
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001516 if (!pp->conn_schedule(ipvs, af, skb, pd, verdict, cpp, iph))
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001517 return 0;
1518 }
1519
1520 if (unlikely(!*cpp)) {
1521 /* sorry, all this trouble for a no-hit :) */
1522 IP_VS_DBG_PKT(12, af, pp, skb, iph->off,
1523 "ip_vs_in: packet continues traversal as normal");
1524 if (iph->fragoffs) {
1525 /* Fragment that couldn't be mapped to a conn entry
1526 * is missing module nf_defrag_ipv6
1527 */
1528 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1529 IP_VS_DBG_PKT(7, af, pp, skb, iph->off,
1530 "unhandled fragment");
1531 }
1532 *verdict = NF_ACCEPT;
1533 return 0;
1534 }
1535
1536 return 1;
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
1540 * Handle ICMP messages in the outside-to-inside direction (incoming).
1541 * Find any that might be relevant, check against existing connections,
1542 * forward to the right destination host if relevant.
1543 * Currently handles error types - unreachable, quench, ttl exceeded.
1544 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001545static int
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001546ip_vs_in_icmp(struct netns_ipvs *ipvs, struct sk_buff *skb, int *related,
1547 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct iphdr *iph;
1550 struct icmphdr _icmph, *ic;
1551 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001552 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct ip_vs_conn *cp;
1554 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001555 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001556 unsigned int offset, offset2, ihl, verdict;
Alex Gartrell6044eef2015-08-26 09:40:37 -07001557 bool ipip, new_cp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 *related = 1;
1560
1561 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001562 if (ip_is_fragment(ip_hdr(skb))) {
Eric W. Biederman57781c12015-09-21 13:03:01 -05001563 if (ip_vs_gather_frags(ipvs, skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001567 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 offset = ihl = iph->ihl * 4;
1569 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1570 if (ic == NULL)
1571 return NF_DROP;
1572
Harvey Harrison14d5e832008-10-31 00:54:29 -07001573 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001575 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /*
1578 * Work through seeing if this is for us.
1579 * These checks are supposed to be in an order that means easy
1580 * things are checked first to speed up processing.... however
1581 * this means that some packets will manage to get a long way
1582 * down this stack and then be rejected, but that's life.
1583 */
1584 if ((ic->type != ICMP_DEST_UNREACH) &&
1585 (ic->type != ICMP_SOURCE_QUENCH) &&
1586 (ic->type != ICMP_TIME_EXCEEDED)) {
1587 *related = 0;
1588 return NF_ACCEPT;
1589 }
1590
1591 /* Now find the contained IP header */
1592 offset += sizeof(_icmph);
1593 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1594 if (cih == NULL)
1595 return NF_ACCEPT; /* The packet looks wrong, ignore */
1596
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001597 /* Special case for errors for IPIP packets */
1598 ipip = false;
1599 if (cih->protocol == IPPROTO_IPIP) {
1600 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1601 return NF_ACCEPT;
1602 /* Error for our IPIP must arrive at LOCAL_IN */
1603 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1604 return NF_ACCEPT;
1605 offset += cih->ihl * 4;
1606 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1607 if (cih == NULL)
1608 return NF_ACCEPT; /* The packet looks wrong, ignore */
1609 ipip = true;
1610 }
1611
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001612 pd = ip_vs_proto_data_get(ipvs, cih->protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001613 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001615 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001618 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 pp->dont_defrag))
1620 return NF_ACCEPT;
1621
Julian Anastasov0d796412010-10-17 16:46:17 +03001622 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1623 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001625 offset2 = offset;
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001626 ip_vs_fill_iph_skb_icmp(AF_INET, skb, offset, !ipip, &ciph);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001627 offset = ciph.len;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001628
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001629 /* The embedded headers contain source and dest in reverse order.
1630 * For IPIP this is error for request, not for reply.
1631 */
Eric W. Biedermanab161972015-09-21 13:02:38 -05001632 cp = pp->conn_in_get(ipvs, AF_INET, skb, &ciph);
Alex Gartrell6044eef2015-08-26 09:40:37 -07001633
1634 if (!cp) {
1635 int v;
1636
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001637 if (!sysctl_schedule_icmp(ipvs))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001638 return NF_ACCEPT;
1639
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001640 if (!ip_vs_try_to_schedule(ipvs, AF_INET, skb, pd, &v, &cp, &ciph))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001641 return v;
1642 new_cp = true;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 verdict = NF_DROP;
1646
1647 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001648 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001650 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1651 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto out;
1653 }
1654
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001655 if (ipip) {
1656 __be32 info = ic->un.gateway;
Peter Christensenf44a5f42014-05-24 21:40:12 +02001657 __u8 type = ic->type;
1658 __u8 code = ic->code;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001659
1660 /* Update the MTU */
1661 if (ic->type == ICMP_DEST_UNREACH &&
1662 ic->code == ICMP_FRAG_NEEDED) {
1663 struct ip_vs_dest *dest = cp->dest;
1664 u32 mtu = ntohs(ic->un.frag.mtu);
Peter Christensenf44a5f42014-05-24 21:40:12 +02001665 __be16 frag_off = cih->frag_off;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001666
1667 /* Strip outer IP and ICMP, go to IPIP header */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001668 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1669 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001670 offset2 -= ihl + sizeof(_icmph);
1671 skb_reset_network_header(skb);
1672 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1673 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001674 ipv4_update_pmtu(skb, ipvs->net,
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001675 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001676 /* Client uses PMTUD? */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001677 if (!(frag_off & htons(IP_DF)))
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001678 goto ignore_ipip;
1679 /* Prefer the resulting PMTU */
1680 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001681 struct ip_vs_dest_dst *dest_dst;
1682
1683 rcu_read_lock();
1684 dest_dst = rcu_dereference(dest->dest_dst);
1685 if (dest_dst)
1686 mtu = dst_mtu(dest_dst->dst_cache);
1687 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001688 }
1689 if (mtu > 68 + sizeof(struct iphdr))
1690 mtu -= sizeof(struct iphdr);
1691 info = htonl(mtu);
1692 }
1693 /* Strip outer IP, ICMP and IPIP, go to IP header of
1694 * original request.
1695 */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001696 if (pskb_pull(skb, offset2) == NULL)
1697 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001698 skb_reset_network_header(skb);
1699 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1700 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Peter Christensenf44a5f42014-05-24 21:40:12 +02001701 type, code, ntohl(info));
1702 icmp_send(skb, type, code, info);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001703 /* ICMP can be shorter but anyways, account it */
1704 ip_vs_out_stats(cp, skb);
1705
1706ignore_ipip:
1707 consume_skb(skb);
1708 verdict = NF_STOLEN;
1709 goto out;
1710 }
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* do the statistics and put it back */
1713 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001714 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1715 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001717 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Hans Schillstrom552ad652011-06-13 12:19:26 +02001719out:
Alex Gartrell6044eef2015-08-26 09:40:37 -07001720 if (likely(!new_cp))
1721 __ip_vs_conn_put(cp);
1722 else
1723 ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 return verdict;
1726}
1727
Julius Volz2a3b7912008-09-02 15:55:47 +02001728#ifdef CONFIG_IP_VS_IPV6
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001729static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb,
1730 int *related, unsigned int hooknum,
1731 struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001732{
Julius Volz2a3b7912008-09-02 15:55:47 +02001733 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001734 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001735 struct ip_vs_conn *cp;
1736 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001737 struct ip_vs_proto_data *pd;
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001738 unsigned int offset, verdict;
Alex Gartrell6044eef2015-08-26 09:40:37 -07001739 bool new_cp = false;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001740
Julius Volz2a3b7912008-09-02 15:55:47 +02001741 *related = 1;
1742
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001743 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001744 if (ic == NULL)
1745 return NF_DROP;
1746
Julius Volz2a3b7912008-09-02 15:55:47 +02001747 /*
1748 * Work through seeing if this is for us.
1749 * These checks are supposed to be in an order that means easy
1750 * things are checked first to speed up processing.... however
1751 * this means that some packets will manage to get a long way
1752 * down this stack and then be rejected, but that's life.
1753 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001754 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001755 *related = 0;
1756 return NF_ACCEPT;
1757 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001758 /* Fragment header that is before ICMP header tells us that:
1759 * it's not an error message since they can't be fragmented.
1760 */
David S. Millere7165032012-11-30 12:01:30 -05001761 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001762 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001763
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001764 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1765 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1766 &iph->saddr, &iph->daddr);
1767
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001768 offset = iph->len + sizeof(_icmph);
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001769 if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph))
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001770 return NF_ACCEPT;
Julius Volz2a3b7912008-09-02 15:55:47 +02001771
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001772 pd = ip_vs_proto_data_get(ipvs, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001773 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001774 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001775 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001776
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001777 /* Cannot handle fragmented embedded protocol */
1778 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001779 return NF_ACCEPT;
1780
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001781 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
Julian Anastasov0d796412010-10-17 16:46:17 +03001782 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001783
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001784 /* The embedded headers contain source and dest in reverse order
1785 * if not from localhost
1786 */
Eric W. Biedermanab161972015-09-21 13:02:38 -05001787 cp = pp->conn_in_get(ipvs, AF_INET6, skb, &ciph);
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001788
Alex Gartrell6044eef2015-08-26 09:40:37 -07001789 if (!cp) {
1790 int v;
1791
Eric W. Biedermana47b4302015-09-21 13:02:00 -05001792 if (!sysctl_schedule_icmp(ipvs))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001793 return NF_ACCEPT;
1794
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001795 if (!ip_vs_try_to_schedule(ipvs, AF_INET6, skb, pd, &v, &cp, &ciph))
Alex Gartrell6044eef2015-08-26 09:40:37 -07001796 return v;
1797
1798 new_cp = true;
1799 }
1800
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001801 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1802 if ((hooknum == NF_INET_LOCAL_OUT) &&
1803 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
Alex Gartrell6044eef2015-08-26 09:40:37 -07001804 verdict = NF_ACCEPT;
1805 goto out;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001806 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001807
Julius Volz2a3b7912008-09-02 15:55:47 +02001808 /* do the statistics and put it back */
1809 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001810
1811 /* Need to mangle contained IPv6 header in ICMPv6 packet */
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001812 offset = ciph.len;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001813 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1814 IPPROTO_SCTP == ciph.protocol)
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001815 offset += 2 * sizeof(__u16); /* Also mangle ports */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001816
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001817 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001818
Alex Gartrell6044eef2015-08-26 09:40:37 -07001819out:
1820 if (likely(!new_cp))
1821 __ip_vs_conn_put(cp);
1822 else
1823 ip_vs_conn_put(cp);
Julius Volz2a3b7912008-09-02 15:55:47 +02001824
1825 return verdict;
1826}
1827#endif
1828
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830/*
1831 * Check if it's for virtual services, look it up,
1832 * and send it on its way...
1833 */
1834static unsigned int
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05001835ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Julius Volz51ef3482008-09-02 15:55:40 +02001837 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001839 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001841 int ret, pkts;
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001842 int conn_reuse_mode;
Eric Dumazet340c78e2015-11-12 09:14:12 -08001843 struct sock *sk;
Julius Volz51ef3482008-09-02 15:55:40 +02001844
Julian Anastasovfc604762010-10-17 16:38:15 +03001845 /* Already marked as IPVS request or reply? */
1846 if (skb->ipvs_property)
1847 return NF_ACCEPT;
1848
Julian Anastasovcb591552010-10-17 16:40:51 +03001849 /*
1850 * Big tappo:
1851 * - remote client: only PACKET_HOST
1852 * - route: used for struct net when skb->dev is unset
1853 */
1854 if (unlikely((skb->pkt_type != PACKET_HOST &&
1855 hooknum != NF_INET_LOCAL_OUT) ||
1856 !skb_dst(skb))) {
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001857 ip_vs_fill_iph_skb(af, skb, false, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001858 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1859 " ignored in hook %u\n",
1860 skb->pkt_type, iph.protocol,
1861 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1862 return NF_ACCEPT;
1863 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001864 /* ipvs enabled in this netns ? */
Julian Anastasov0c125822013-03-09 23:25:04 +02001865 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001866 return NF_ACCEPT;
1867
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07001868 ip_vs_fill_iph_skb(af, skb, false, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Julian Anastasovcb591552010-10-17 16:40:51 +03001870 /* Bad... Do not break raw sockets */
Eric Dumazet340c78e2015-11-12 09:14:12 -08001871 sk = skb_to_full_sk(skb);
1872 if (unlikely(sk && hooknum == NF_INET_LOCAL_OUT &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001873 af == AF_INET)) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001874
Eric Dumazet340c78e2015-11-12 09:14:12 -08001875 if (sk->sk_family == PF_INET && inet_sk(sk)->nodefrag)
Julian Anastasovcb591552010-10-17 16:40:51 +03001876 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Julius Volz94b26552009-08-31 16:22:23 +02001879#ifdef CONFIG_IP_VS_IPV6
1880 if (af == AF_INET6) {
1881 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001882 int related;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001883 int verdict = ip_vs_in_icmp_v6(ipvs, skb, &related,
1884 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Julius Volz94b26552009-08-31 16:22:23 +02001886 if (related)
1887 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001888 }
1889 } else
1890#endif
1891 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001892 int related;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05001893 int verdict = ip_vs_in_icmp(ipvs, skb, &related,
1894 hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001895
1896 if (related)
1897 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /* Protocol supported? */
Eric W. Biederman18d6ade2015-09-21 13:02:01 -05001901 pd = ip_vs_proto_data_get(ipvs, iph.protocol);
Alex Gartrell4e478092015-09-14 23:23:05 -07001902 if (unlikely(!pd)) {
1903 /* The only way we'll see this packet again is if it's
1904 * encapsulated, so mark it with ipvs_property=1 so we
1905 * skip it if we're ignoring tunneled packets
1906 */
1907 if (sysctl_ignore_tunneled(ipvs))
1908 skb->ipvs_property = 1;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return NF_ACCEPT;
Alex Gartrell4e478092015-09-14 23:23:05 -07001911 }
Hans Schillstrom93304192011-01-03 14:44:51 +01001912 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
1914 * Check if the packet belongs to an existing connection entry
1915 */
Eric W. Biedermanab161972015-09-21 13:02:38 -05001916 cp = pp->conn_in_get(ipvs, af, skb, &iph);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001917
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001918 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
Julian Anastasovf719e372016-03-05 15:03:22 +02001919 if (conn_reuse_mode && !iph.fragoffs && is_new_conn(skb, &iph) && cp) {
1920 bool uses_ct = false, resched = false;
1921
1922 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1923 unlikely(!atomic_read(&cp->dest->weight))) {
1924 resched = true;
1925 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
1926 } else if (is_new_conn_expected(cp, conn_reuse_mode)) {
1927 uses_ct = ip_vs_conn_uses_conntrack(cp, skb);
1928 if (!atomic_read(&cp->n_control)) {
1929 resched = true;
1930 } else {
1931 /* Do not reschedule controlling connection
1932 * that uses conntrack while it is still
1933 * referenced by controlled connection(s).
1934 */
1935 resched = !uses_ct;
1936 }
1937 }
1938
1939 if (resched) {
1940 if (!atomic_read(&cp->n_control))
1941 ip_vs_conn_expire_now(cp);
1942 __ip_vs_conn_put(cp);
1943 if (uses_ct)
1944 return NF_DROP;
1945 cp = NULL;
1946 }
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001947 }
1948
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001949 if (unlikely(!cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int v;
1951
Eric W. Biedermand8f44c32015-09-21 13:02:43 -05001952 if (!ip_vs_try_to_schedule(ipvs, af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return v;
1954 }
1955
Alex Gartrellb0e010c2015-08-26 09:40:28 -07001956 IP_VS_DBG_PKT(11, af, pp, skb, iph.off, "Incoming packet");
Alex Gartrell3b5ca612015-08-26 09:40:31 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* Check the server status */
1959 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1960 /* the destination server is not available */
1961
Simon Horman71a8ab62011-02-04 18:33:01 +09001962 if (sysctl_expire_nodest_conn(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* try to expire the connection immediately */
1964 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001966 /* don't restart its timer, and silently
1967 drop the packet. */
1968 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return NF_DROP;
1970 }
1971
1972 ip_vs_in_stats(cp, skb);
Simon Horman4a516f12011-09-16 14:11:49 +09001973 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001975 ret = cp->packet_xmit(skb, cp, pp, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* do not touch skb anymore */
1977 else {
1978 IP_VS_DBG_RL("warning: packet_xmit is null");
1979 ret = NF_ACCEPT;
1980 }
1981
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001982 /* Increase its packet counter and check if it is needed
1983 * to be synchronized
1984 *
1985 * Sync connection if it is about to close to
1986 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001987 *
1988 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001989 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001990
Hans Schillstrom986a0752010-11-19 14:25:13 +01001991 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Simon Horman59e03502011-02-04 18:33:01 +09001992 pkts = sysctl_sync_threshold(ipvs);
Hans Schillstrom986a0752010-11-19 14:25:13 +01001993 else
1994 pkts = atomic_add_return(1, &cp->in_pkts);
1995
Julian Anastasov749c42b2012-04-24 23:46:40 +03001996 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -05001997 ip_vs_sync_conn(ipvs, cp, pkts);
Marco Angaroni698e2a82016-04-26 21:20:22 +02001998 else if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && cp->control)
1999 /* increment is done inside ip_vs_sync_conn too */
2000 atomic_inc(&cp->control->in_pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 ip_vs_conn_put(cp);
2003 return ret;
2004}
2005
Julian Anastasovcb591552010-10-17 16:40:51 +03002006/*
2007 * AF_INET handler in NF_INET_LOCAL_IN chain
2008 * Schedule and forward packets from remote clients
2009 */
2010static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05002011ip_vs_remote_request4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04002012 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03002013{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05002014 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03002015}
2016
2017/*
2018 * AF_INET handler in NF_INET_LOCAL_OUT chain
2019 * Schedule and forward packets from local clients
2020 */
2021static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05002022ip_vs_local_request4(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04002023 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03002024{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05002025 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03002026}
2027
2028#ifdef CONFIG_IP_VS_IPV6
2029
2030/*
2031 * AF_INET6 handler in NF_INET_LOCAL_IN chain
2032 * Schedule and forward packets from remote clients
2033 */
2034static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05002035ip_vs_remote_request6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04002036 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03002037{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05002038 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03002039}
2040
2041/*
2042 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
2043 * Schedule and forward packets from local clients
2044 */
2045static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05002046ip_vs_local_request6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04002047 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03002048{
Eric W. Biederman6e385bb2015-09-21 13:02:53 -05002049 return ip_vs_in(net_ipvs(state->net), state->hook, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03002050}
2051
2052#endif
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08002056 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 * related packets destined for 0.0.0.0/0.
2058 * When fwmark-based virtual service is used, such as transparent
2059 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
2060 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08002061 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * and send them to ip_vs_in_icmp.
2063 */
2064static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05002065ip_vs_forward_icmp(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04002066 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 int r;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05002069 struct netns_ipvs *ipvs = net_ipvs(state->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Herbert Xu3db05fe2007-10-15 00:53:15 -07002071 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return NF_ACCEPT;
2073
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002074 /* ipvs enabled in this netns ? */
Julian Anastasov0c125822013-03-09 23:25:04 +02002075 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002076 return NF_ACCEPT;
2077
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05002078 return ip_vs_in_icmp(ipvs, skb, &r, state->hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Julius Volz2a3b7912008-09-02 15:55:47 +02002081#ifdef CONFIG_IP_VS_IPV6
2082static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -05002083ip_vs_forward_icmp_v6(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04002084 const struct nf_hook_state *state)
Julius Volz2a3b7912008-09-02 15:55:47 +02002085{
2086 int r;
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05002087 struct netns_ipvs *ipvs = net_ipvs(state->net);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02002088 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02002089
Alex Gartrell4fd9bee2015-08-26 09:40:29 -07002090 ip_vs_fill_iph_skb(AF_INET6, skb, false, &iphdr);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02002091 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02002092 return NF_ACCEPT;
2093
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002094 /* ipvs enabled in this netns ? */
Julian Anastasov0c125822013-03-09 23:25:04 +02002095 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002096 return NF_ACCEPT;
2097
Eric W. Biederman6f2bcea2015-09-21 13:02:54 -05002098 return ip_vs_in_icmp_v6(ipvs, skb, &r, state->hook, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02002099}
2100#endif
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Patrick McHardy19994142007-12-05 01:23:00 -08002103static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03002104 /* After packet filtering, change source only for VS/NAT */
2105 {
2106 .hook = ip_vs_reply4,
Alban Crequy4c809d62012-05-14 03:56:38 +00002107 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03002108 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002109 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002110 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08002111 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2112 * or VS/NAT(change destination), so that filtering rules can be
2113 * applied to IPVS. */
2114 {
Julian Anastasovcb591552010-10-17 16:40:51 +03002115 .hook = ip_vs_remote_request4,
Alban Crequy4c809d62012-05-14 03:56:38 +00002116 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03002117 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002118 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08002119 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002120 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08002121 {
Julian Anastasovfc604762010-10-17 16:38:15 +03002122 .hook = ip_vs_local_reply4,
Alban Crequy4c809d62012-05-14 03:56:38 +00002123 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03002124 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002125 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08002126 },
Julian Anastasovcb591552010-10-17 16:40:51 +03002127 /* After mangle, schedule and forward local requests */
2128 {
2129 .hook = ip_vs_local_request4,
Alban Crequy4c809d62012-05-14 03:56:38 +00002130 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03002131 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002132 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002133 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08002134 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2135 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2136 {
2137 .hook = ip_vs_forward_icmp,
Alban Crequy4c809d62012-05-14 03:56:38 +00002138 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03002139 .hooknum = NF_INET_FORWARD,
2140 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08002141 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002142 /* After packet filtering, change source only for VS/NAT */
2143 {
2144 .hook = ip_vs_reply4,
Alban Crequy4c809d62012-05-14 03:56:38 +00002145 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03002146 .hooknum = NF_INET_FORWARD,
2147 .priority = 100,
2148 },
Julius Volz473b23d2008-09-02 15:55:54 +02002149#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03002150 /* After packet filtering, change source only for VS/NAT */
2151 {
2152 .hook = ip_vs_reply6,
Alban Crequy4c809d62012-05-14 03:56:38 +00002153 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002154 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002155 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002156 },
Julius Volz473b23d2008-09-02 15:55:54 +02002157 /* After packet filtering, forward packet through VS/DR, VS/TUN,
2158 * or VS/NAT(change destination), so that filtering rules can be
2159 * applied to IPVS. */
2160 {
Julian Anastasovcb591552010-10-17 16:40:51 +03002161 .hook = ip_vs_remote_request6,
Alban Crequy4c809d62012-05-14 03:56:38 +00002162 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002163 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002164 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02002165 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002166 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02002167 {
Julian Anastasovfc604762010-10-17 16:38:15 +03002168 .hook = ip_vs_local_reply6,
Julian Anastasoveb90b0c2014-08-22 17:53:41 +03002169 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03002170 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002171 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02002172 },
Julian Anastasovcb591552010-10-17 16:40:51 +03002173 /* After mangle, schedule and forward local requests */
2174 {
2175 .hook = ip_vs_local_request6,
Alban Crequy4c809d62012-05-14 03:56:38 +00002176 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002177 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09002178 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03002179 },
Julius Volz473b23d2008-09-02 15:55:54 +02002180 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
2181 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
2182 {
2183 .hook = ip_vs_forward_icmp_v6,
Alban Crequy4c809d62012-05-14 03:56:38 +00002184 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03002185 .hooknum = NF_INET_FORWARD,
2186 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02002187 },
Julian Anastasovfc604762010-10-17 16:38:15 +03002188 /* After packet filtering, change source only for VS/NAT */
2189 {
2190 .hook = ip_vs_reply6,
Alban Crequy4c809d62012-05-14 03:56:38 +00002191 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03002192 .hooknum = NF_INET_FORWARD,
2193 .priority = 100,
2194 },
Julius Volz473b23d2008-09-02 15:55:54 +02002195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002197/*
2198 * Initialize IP Virtual Server netns mem.
2199 */
2200static int __net_init __ip_vs_init(struct net *net)
2201{
2202 struct netns_ipvs *ipvs;
2203
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002204 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07002205 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002206 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07002207
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002208 /* Hold the beast until a service is registerd */
2209 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01002210 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002211 /* Counters used for creating unique names */
2212 ipvs->gen = atomic_read(&ipvs_netns_cnt);
2213 atomic_inc(&ipvs_netns_cnt);
2214 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002215
Eric W. Biedermana4dd0362015-09-21 13:02:28 -05002216 if (ip_vs_estimator_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002217 goto estimator_fail;
2218
Eric W. Biederman3d993762015-09-21 13:02:26 -05002219 if (ip_vs_control_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002220 goto control_fail;
2221
Eric W. Biederman7d1f88e2015-09-21 13:02:58 -05002222 if (ip_vs_protocol_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002223 goto protocol_fail;
2224
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -05002225 if (ip_vs_app_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002226 goto app_fail;
2227
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05002228 if (ip_vs_conn_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002229 goto conn_fail;
2230
Eric W. Biederman802cb432015-09-21 13:02:20 -05002231 if (ip_vs_sync_net_init(ipvs) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002232 goto sync_fail;
2233
Simon Hormana870c8c2011-02-01 18:21:53 +01002234 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002235 sizeof(struct netns_ipvs), ipvs->gen);
2236 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002237/*
2238 * Error handling
2239 */
2240
2241sync_fail:
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05002242 ip_vs_conn_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002243conn_fail:
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -05002244 ip_vs_app_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002245app_fail:
Eric W. Biederman7d1f88e2015-09-21 13:02:58 -05002246 ip_vs_protocol_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002247protocol_fail:
Eric W. Biederman3d993762015-09-21 13:02:26 -05002248 ip_vs_control_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002249control_fail:
Eric W. Biedermana4dd0362015-09-21 13:02:28 -05002250 ip_vs_estimator_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002251estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03002252 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002253 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002254}
2255
2256static void __net_exit __ip_vs_cleanup(struct net *net)
2257{
Eric W. Biederman56d21692015-09-21 13:01:58 -05002258 struct netns_ipvs *ipvs = net_ipvs(net);
2259
2260 ip_vs_service_net_cleanup(ipvs); /* ip_vs_flush() with locks */
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05002261 ip_vs_conn_net_cleanup(ipvs);
Eric W. Biedermanb5dd2122015-09-21 13:02:34 -05002262 ip_vs_app_net_cleanup(ipvs);
Eric W. Biederman7d1f88e2015-09-21 13:02:58 -05002263 ip_vs_protocol_net_cleanup(ipvs);
Eric W. Biederman3d993762015-09-21 13:02:26 -05002264 ip_vs_control_net_cleanup(ipvs);
Eric W. Biedermana4dd0362015-09-21 13:02:28 -05002265 ip_vs_estimator_net_cleanup(ipvs);
Eric W. Biederman56d21692015-09-21 13:01:58 -05002266 IP_VS_DBG(2, "ipvs netns %d released\n", ipvs->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002267 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002268}
2269
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002270static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2271{
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002272 struct netns_ipvs *ipvs = net_ipvs(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002273 EnterFunction(2);
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002274 ipvs->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002275 smp_wmb();
Eric W. Biedermanebea1f72015-09-21 13:02:21 -05002276 ip_vs_sync_net_cleanup(ipvs);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002277 LeaveFunction(2);
2278}
2279
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002280static struct pernet_operations ipvs_core_ops = {
2281 .init = __ip_vs_init,
2282 .exit = __ip_vs_cleanup,
2283 .id = &ip_vs_net_id,
2284 .size = sizeof(struct netns_ipvs),
2285};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002287static struct pernet_operations ipvs_core_dev_ops = {
2288 .exit = __ip_vs_dev_cleanup,
2289};
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/*
2292 * Initialize IP Virtual Server
2293 */
2294static int __init ip_vs_init(void)
2295{
2296 int ret;
2297
2298 ret = ip_vs_control_init();
2299 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002300 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002301 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
2304 ip_vs_protocol_init();
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 ret = ip_vs_conn_init();
2307 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002308 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002309 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002310 }
2311
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002312 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2313 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002314 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002315
2316 ret = register_pernet_device(&ipvs_core_dev_ops);
2317 if (ret < 0)
2318 goto cleanup_sub;
2319
Patrick McHardy41c5b312007-12-05 01:22:43 -08002320 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002322 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002323 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325
Hans Schillstrom8537de82012-04-26 07:47:44 +02002326 ret = ip_vs_register_nl_ioctl();
2327 if (ret < 0) {
2328 pr_err("can't register netlink/ioctl.\n");
2329 goto cleanup_hooks;
2330 }
2331
Hannes Eder1e3e2382009-08-02 11:05:41 +00002332 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return ret;
2335
Hans Schillstrom8537de82012-04-26 07:47:44 +02002336cleanup_hooks:
2337 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002338cleanup_dev:
2339 unregister_pernet_device(&ipvs_core_dev_ops);
2340cleanup_sub:
2341 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002342cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002344cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 ip_vs_protocol_cleanup();
2346 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002347exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return ret;
2349}
2350
2351static void __exit ip_vs_cleanup(void)
2352{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002353 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002354 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002355 unregister_pernet_device(&ipvs_core_dev_ops);
2356 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ip_vs_protocol_cleanup();
2359 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002360 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363module_init(ip_vs_init);
2364module_exit(ip_vs_cleanup);
2365MODULE_LICENSE("GPL");