blob: 096a45103f14cbbaccc07b7574b9fcfc7e55ff92 [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. Many code here is taken from IP MASQ code of kernel 2.2.
20 *
21 * Changes:
22 *
23 */
24
Hannes Eder9aada7a2009-07-30 14:29:44 -070025#define KMSG_COMPONENT "IPVS"
26#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
27
Andrew Mortone9242832006-01-05 14:57:36 -080028#include <linux/interrupt.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <linux/in.h>
Julian Anastasovf18ae722014-09-09 16:40:38 -070030#include <linux/inet.h>
Arnaldo Carvalho de Melof1900552006-01-04 02:02:20 -020031#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020033#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/vmalloc.h>
35#include <linux/proc_fs.h> /* for proc_net_* */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/seq_file.h>
38#include <linux/jhash.h>
39#include <linux/random.h>
40
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020041#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <net/ip_vs.h>
43
44
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010045#ifndef CONFIG_IP_VS_TAB_BITS
46#define CONFIG_IP_VS_TAB_BITS 12
47#endif
48
49/*
50 * Connection hash size. Default is what was selected at compile time.
51*/
Eric Dumazet4ecd2942010-11-15 18:38:52 +010052static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010053module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
54MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
55
56/* size and mask values */
Eric Dumazet4ecd2942010-11-15 18:38:52 +010057int ip_vs_conn_tab_size __read_mostly;
58static int ip_vs_conn_tab_mask __read_mostly;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +010059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Connection hash table: for input and output packets lookups of IPVS
62 */
Changli Gao731109e2011-02-19 18:05:08 +080063static struct hlist_head *ip_vs_conn_tab __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* SLAB cache for IPVS connections */
Christoph Lametere18b8902006-12-06 20:33:20 -080066static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* counter for no client port connections */
69static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
70
71/* random value for IPVS connection hash */
Eric Dumazet4ecd2942010-11-15 18:38:52 +010072static unsigned int ip_vs_conn_rnd __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Fine locking granularity for big connection hash table
76 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +010077#define CT_LOCKARRAY_BITS 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
79#define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
80
Julian Anastasovf18ae722014-09-09 16:40:38 -070081/* We need an addrstrlen that works with or without v6 */
82#ifdef CONFIG_IP_VS_IPV6
83#define IP_VS_ADDRSTRLEN INET6_ADDRSTRLEN
84#else
85#define IP_VS_ADDRSTRLEN (8+1)
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct ip_vs_aligned_lock
89{
Julian Anastasov088339a2013-03-21 11:58:10 +020090 spinlock_t l;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091} __attribute__((__aligned__(SMP_CACHE_BYTES)));
92
93/* lock array for conn table */
94static struct ip_vs_aligned_lock
95__ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
96
Julian Anastasovac692692013-03-22 11:46:54 +020097static inline void ct_write_lock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Julian Anastasovac692692013-03-22 11:46:54 +020099 spin_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Julian Anastasovac692692013-03-22 11:46:54 +0200102static inline void ct_write_unlock_bh(unsigned int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Julian Anastasovac692692013-03-22 11:46:54 +0200104 spin_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Marco Angaroni013b0422016-04-05 18:26:52 +0200107static void ip_vs_conn_expire(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
110 * Returns hash value for IPVS connection entry
111 */
Eric W. Biederman754b81a2015-09-21 13:02:40 -0500112static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
Julius Volz28364a52008-09-02 15:55:43 +0200113 const union nf_inet_addr *addr,
114 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Julius Volz28364a52008-09-02 15:55:43 +0200116#ifdef CONFIG_IP_VS_IPV6
117 if (af == AF_INET6)
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100118 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
119 (__force u32)port, proto, ip_vs_conn_rnd) ^
Eric W. Biederman754b81a2015-09-21 13:02:40 -0500120 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
Julius Volz28364a52008-09-02 15:55:43 +0200121#endif
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100122 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
123 ip_vs_conn_rnd) ^
Eric W. Biederman754b81a2015-09-21 13:02:40 -0500124 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Simon Horman85999282010-08-22 21:37:53 +0900127static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
128 bool inverse)
129{
130 const union nf_inet_addr *addr;
131 __be16 port;
132
Simon Hormanf71499a2010-08-22 21:37:54 +0900133 if (p->pe_data && p->pe->hashkey_raw)
Simon Horman85999282010-08-22 21:37:53 +0900134 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
135 ip_vs_conn_tab_mask;
136
137 if (likely(!inverse)) {
138 addr = p->caddr;
139 port = p->cport;
140 } else {
141 addr = p->vaddr;
142 port = p->vport;
143 }
144
Eric W. Biederman754b81a2015-09-21 13:02:40 -0500145 return ip_vs_conn_hashkey(p->ipvs, p->af, p->protocol, addr, port);
Simon Horman85999282010-08-22 21:37:53 +0900146}
147
148static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
149{
150 struct ip_vs_conn_param p;
151
Eric W. Biederman19913de2015-09-21 13:01:43 -0500152 ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100153 &cp->caddr, cp->cport, NULL, 0, &p);
Simon Horman85999282010-08-22 21:37:53 +0900154
Simon Hormane9e5eee2010-11-08 20:05:57 +0900155 if (cp->pe) {
156 p.pe = cp->pe;
Simon Horman85999282010-08-22 21:37:53 +0900157 p.pe_data = cp->pe_data;
158 p.pe_data_len = cp->pe_data_len;
159 }
160
161 return ip_vs_conn_hashkey_param(&p, false);
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100165 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * returns bool success.
167 */
168static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
169{
Eric Dumazet95c96172012-04-15 05:58:06 +0000170 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int ret;
172
Nick Chalk26ec0372010-06-22 08:07:01 +0200173 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
174 return 0;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Hash by protocol, client address and port */
Simon Horman85999282010-08-22 21:37:53 +0900177 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Julian Anastasovac692692013-03-22 11:46:54 +0200179 ct_write_lock_bh(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200180 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 cp->flags |= IP_VS_CONN_F_HASHED;
184 atomic_inc(&cp->refcnt);
Julian Anastasov088339a2013-03-21 11:58:10 +0200185 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ret = 1;
187 } else {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000188 pr_err("%s(): request for already hashed, called from %pF\n",
189 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ret = 0;
191 }
192
Sven Wegeneraea9d712010-06-09 16:10:57 +0200193 spin_unlock(&cp->lock);
Julian Anastasovac692692013-03-22 11:46:54 +0200194 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return ret;
197}
198
199
200/*
201 * UNhashes ip_vs_conn from ip_vs_conn_tab.
Julian Anastasov088339a2013-03-21 11:58:10 +0200202 * returns bool success. Caller should hold conn reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
205{
Eric Dumazet95c96172012-04-15 05:58:06 +0000206 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int ret;
208
209 /* unhash it and decrease its reference counter */
Simon Horman85999282010-08-22 21:37:53 +0900210 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Julian Anastasovac692692013-03-22 11:46:54 +0200212 ct_write_lock_bh(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200213 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 if (cp->flags & IP_VS_CONN_F_HASHED) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200216 hlist_del_rcu(&cp->c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 cp->flags &= ~IP_VS_CONN_F_HASHED;
218 atomic_dec(&cp->refcnt);
219 ret = 1;
220 } else
221 ret = 0;
222
Sven Wegeneraea9d712010-06-09 16:10:57 +0200223 spin_unlock(&cp->lock);
Julian Anastasovac692692013-03-22 11:46:54 +0200224 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return ret;
227}
228
Julian Anastasov088339a2013-03-21 11:58:10 +0200229/* Try to unlink ip_vs_conn from ip_vs_conn_tab.
230 * returns bool success.
231 */
232static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
233{
234 unsigned int hash;
235 bool ret;
236
237 hash = ip_vs_conn_hashkey_conn(cp);
238
Julian Anastasovac692692013-03-22 11:46:54 +0200239 ct_write_lock_bh(hash);
Julian Anastasov088339a2013-03-21 11:58:10 +0200240 spin_lock(&cp->lock);
241
242 if (cp->flags & IP_VS_CONN_F_HASHED) {
243 ret = false;
244 /* Decrease refcnt and unlink conn only if we are last user */
245 if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) {
246 hlist_del_rcu(&cp->c_list);
247 cp->flags &= ~IP_VS_CONN_F_HASHED;
248 ret = true;
249 }
250 } else
251 ret = atomic_read(&cp->refcnt) ? false : true;
252
253 spin_unlock(&cp->lock);
Julian Anastasovac692692013-03-22 11:46:54 +0200254 ct_write_unlock_bh(hash);
Julian Anastasov088339a2013-03-21 11:58:10 +0200255
256 return ret;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/*
261 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
262 * Called for pkts coming from OUTside-to-INside.
Simon Hormanf11017e2010-08-22 21:37:52 +0900263 * p->caddr, p->cport: pkt source address (foreign host)
264 * p->vaddr, p->vport: pkt dest address (load balancer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900266static inline struct ip_vs_conn *
267__ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Eric Dumazet95c96172012-04-15 05:58:06 +0000269 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct ip_vs_conn *cp;
271
Simon Horman85999282010-08-22 21:37:53 +0900272 hash = ip_vs_conn_hashkey_param(p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Julian Anastasov088339a2013-03-21 11:58:10 +0200274 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Julian Anastasov088339a2013-03-21 11:58:10 +0200276 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200277 if (p->cport == cp->cport && p->vport == cp->vport &&
278 cp->af == p->af &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900279 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
280 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900281 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100282 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500283 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200284 if (!__ip_vs_conn_get(cp))
285 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* HIT */
Julian Anastasov088339a2013-03-21 11:58:10 +0200287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return cp;
289 }
290 }
291
Julian Anastasov088339a2013-03-21 11:58:10 +0200292 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 return NULL;
295}
296
Simon Hormanf11017e2010-08-22 21:37:52 +0900297struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct ip_vs_conn *cp;
300
Simon Hormanf11017e2010-08-22 21:37:52 +0900301 cp = __ip_vs_conn_in_get(p);
302 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
303 struct ip_vs_conn_param cport_zero_p = *p;
304 cport_zero_p.cport = 0;
305 cp = __ip_vs_conn_in_get(&cport_zero_p);
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Julius Volz28364a52008-09-02 15:55:43 +0200308 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900309 ip_vs_proto_name(p->protocol),
310 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
311 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200312 cp ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 return cp;
315}
316
Simon Hormanf11017e2010-08-22 21:37:52 +0900317static int
Eric W. Biedermanf5099dd2015-09-21 13:02:37 -0500318ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
319 int af, const struct sk_buff *skb,
Simon Hormanf11017e2010-08-22 21:37:52 +0900320 const struct ip_vs_iphdr *iph,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700321 struct ip_vs_conn_param *p)
Simon Hormanf11017e2010-08-22 21:37:52 +0900322{
323 __be16 _ports[2], *pptr;
324
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200325 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Simon Hormanf11017e2010-08-22 21:37:52 +0900326 if (pptr == NULL)
327 return 1;
328
Alex Gartrell802c41a2015-08-26 09:40:32 -0700329 if (likely(!ip_vs_iph_inverse(iph)))
Eric W. Biederman19913de2015-09-21 13:01:43 -0500330 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100331 pptr[0], &iph->daddr, pptr[1], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900332 else
Eric W. Biederman19913de2015-09-21 13:01:43 -0500333 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100334 pptr[1], &iph->saddr, pptr[0], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900335 return 0;
336}
337
Simon Horman5c0d2372010-08-02 17:12:44 +0200338struct ip_vs_conn *
Eric W. Biedermanab161972015-09-21 13:02:38 -0500339ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
340 const struct sk_buff *skb,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700341 const struct ip_vs_iphdr *iph)
Simon Horman5c0d2372010-08-02 17:12:44 +0200342{
Simon Hormanf11017e2010-08-22 21:37:52 +0900343 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200344
Eric W. Biedermanf5099dd2015-09-21 13:02:37 -0500345 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200346 return NULL;
347
Simon Hormanf11017e2010-08-22 21:37:52 +0900348 return ip_vs_conn_in_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200349}
350EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
351
Julian Anastasov87375ab2005-09-14 21:08:51 -0700352/* Get reference to connection template */
Simon Hormanf11017e2010-08-22 21:37:52 +0900353struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700354{
Eric Dumazet95c96172012-04-15 05:58:06 +0000355 unsigned int hash;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700356 struct ip_vs_conn *cp;
357
Simon Horman85999282010-08-22 21:37:53 +0900358 hash = ip_vs_conn_hashkey_param(p, false);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700359
Julian Anastasov088339a2013-03-21 11:58:10 +0200360 rcu_read_lock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700361
Julian Anastasov088339a2013-03-21 11:58:10 +0200362 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200363 if (unlikely(p->pe_data && p->pe->ct_match)) {
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500364 if (cp->ipvs != p->ipvs)
Julian Anastasov1845ed02013-03-21 11:58:11 +0200365 continue;
Julian Anastasov088339a2013-03-21 11:58:10 +0200366 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
367 if (__ip_vs_conn_get(cp))
368 goto out;
369 }
Simon Horman85999282010-08-22 21:37:53 +0900370 continue;
371 }
372
Simon Hormanf11017e2010-08-22 21:37:52 +0900373 if (cp->af == p->af &&
374 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000375 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900376 * p->vaddr is a fwmark */
377 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
378 p->af, p->vaddr, &cp->vaddr) &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200379 p->vport == cp->vport && p->cport == cp->cport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700380 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200381 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500382 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200383 if (__ip_vs_conn_get(cp))
384 goto out;
385 }
Julian Anastasov87375ab2005-09-14 21:08:51 -0700386 }
387 cp = NULL;
388
389 out:
Julian Anastasov088339a2013-03-21 11:58:10 +0200390 rcu_read_unlock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700391
Julius Volz28364a52008-09-02 15:55:43 +0200392 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900393 ip_vs_proto_name(p->protocol),
394 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
395 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200396 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700397
398 return cp;
399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Simon Hormanf11017e2010-08-22 21:37:52 +0900401/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
402 * Called for pkts coming from inside-to-OUTside.
403 * p->caddr, p->cport: pkt source address (inside host)
404 * p->vaddr, p->vport: pkt dest address (foreign host) */
405struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Eric Dumazet95c96172012-04-15 05:58:06 +0000407 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct ip_vs_conn *cp, *ret=NULL;
409
410 /*
411 * Check for "full" addressed entries
412 */
Simon Horman85999282010-08-22 21:37:53 +0900413 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Julian Anastasov088339a2013-03-21 11:58:10 +0200415 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Julian Anastasov088339a2013-03-21 11:58:10 +0200417 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200418 if (p->vport == cp->cport && p->cport == cp->dport &&
419 cp->af == p->af &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900420 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
421 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100422 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500423 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200424 if (!__ip_vs_conn_get(cp))
425 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ret = cp;
428 break;
429 }
430 }
431
Julian Anastasov088339a2013-03-21 11:58:10 +0200432 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Julius Volz28364a52008-09-02 15:55:43 +0200434 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900435 ip_vs_proto_name(p->protocol),
436 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
437 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200438 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 return ret;
441}
442
Simon Horman5c0d2372010-08-02 17:12:44 +0200443struct ip_vs_conn *
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -0500444ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
445 const struct sk_buff *skb,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700446 const struct ip_vs_iphdr *iph)
Simon Horman5c0d2372010-08-02 17:12:44 +0200447{
Simon Hormanf11017e2010-08-22 21:37:52 +0900448 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200449
Eric W. Biedermanf5099dd2015-09-21 13:02:37 -0500450 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200451 return NULL;
452
Simon Hormanf11017e2010-08-22 21:37:52 +0900453 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200454}
455EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Marco Angaroni013b0422016-04-05 18:26:52 +0200457static void __ip_vs_conn_put_notimer(struct ip_vs_conn *cp)
458{
459 __ip_vs_conn_put(cp);
460 ip_vs_conn_expire((unsigned long)cp);
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*
464 * Put back the conn and restart its timer with its timeout
465 */
Marco Angaroni013b0422016-04-05 18:26:52 +0200466static void __ip_vs_conn_put_timer(struct ip_vs_conn *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Nick Chalk26ec0372010-06-22 08:07:01 +0200468 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
469 0 : cp->timeout;
470 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 __ip_vs_conn_put(cp);
473}
474
Marco Angaroni013b0422016-04-05 18:26:52 +0200475void ip_vs_conn_put(struct ip_vs_conn *cp)
476{
477 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) &&
478 (atomic_read(&cp->refcnt) == 1) &&
479 !timer_pending(&cp->timer))
480 /* expire connection immediately */
481 __ip_vs_conn_put_notimer(cp);
482 else
483 __ip_vs_conn_put_timer(cp);
484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486/*
487 * Fill a no_client_port connection with a client port number
488 */
Al Viro014d7302006-09-28 14:29:52 -0700489void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 if (ip_vs_conn_unhash(cp)) {
Julian Anastasovac692692013-03-22 11:46:54 +0200492 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
494 atomic_dec(&ip_vs_conn_no_cport_cnt);
495 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
496 cp->cport = cport;
497 }
Julian Anastasovac692692013-03-22 11:46:54 +0200498 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* hash on new dport */
501 ip_vs_conn_hash(cp);
502 }
503}
504
505
506/*
507 * Bind a connection entry with the corresponding packet_xmit.
508 * Called by ip_vs_conn_new.
509 */
510static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
511{
512 switch (IP_VS_FWD_METHOD(cp)) {
513 case IP_VS_CONN_F_MASQ:
514 cp->packet_xmit = ip_vs_nat_xmit;
515 break;
516
517 case IP_VS_CONN_F_TUNNEL:
Alex Gartrell8052ba22014-09-09 16:40:28 -0700518#ifdef CONFIG_IP_VS_IPV6
519 if (cp->daf == AF_INET6)
520 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
521 else
522#endif
523 cp->packet_xmit = ip_vs_tunnel_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
525
526 case IP_VS_CONN_F_DROUTE:
527 cp->packet_xmit = ip_vs_dr_xmit;
528 break;
529
530 case IP_VS_CONN_F_LOCALNODE:
531 cp->packet_xmit = ip_vs_null_xmit;
532 break;
533
534 case IP_VS_CONN_F_BYPASS:
535 cp->packet_xmit = ip_vs_bypass_xmit;
536 break;
537 }
538}
539
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200540#ifdef CONFIG_IP_VS_IPV6
541static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
542{
543 switch (IP_VS_FWD_METHOD(cp)) {
544 case IP_VS_CONN_F_MASQ:
545 cp->packet_xmit = ip_vs_nat_xmit_v6;
546 break;
547
548 case IP_VS_CONN_F_TUNNEL:
Alex Gartrell8052ba22014-09-09 16:40:28 -0700549 if (cp->daf == AF_INET6)
550 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
551 else
552 cp->packet_xmit = ip_vs_tunnel_xmit;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200553 break;
554
555 case IP_VS_CONN_F_DROUTE:
556 cp->packet_xmit = ip_vs_dr_xmit_v6;
557 break;
558
559 case IP_VS_CONN_F_LOCALNODE:
560 cp->packet_xmit = ip_vs_null_xmit;
561 break;
562
563 case IP_VS_CONN_F_BYPASS:
564 cp->packet_xmit = ip_vs_bypass_xmit_v6;
565 break;
566 }
567}
568#endif
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
572{
573 return atomic_read(&dest->activeconns)
574 + atomic_read(&dest->inactconns);
575}
576
577/*
578 * Bind a connection entry with a virtual service destination
579 * Called just after a new connection entry is created.
580 */
581static inline void
582ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
583{
Julian Anastasov35757922010-09-17 14:18:16 +0200584 unsigned int conn_flags;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200585 __u32 flags;
Julian Anastasov35757922010-09-17 14:18:16 +0200586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* if dest is NULL, then return directly */
588 if (!dest)
589 return;
590
591 /* Increase the refcnt counter of the dest */
Julian Anastasovfca9c202013-03-22 11:46:38 +0200592 ip_vs_dest_hold(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Julian Anastasov35757922010-09-17 14:18:16 +0200594 conn_flags = atomic_read(&dest->conn_flags);
595 if (cp->protocol != IPPROTO_UDP)
596 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200597 flags = cp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* Bind with the destination and its corresponding transmitter */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200599 if (flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800600 /* if the connection is not template and is created
601 * by sync, preserve the activity flag.
602 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200603 if (!(flags & IP_VS_CONN_F_TEMPLATE))
Julian Anastasov35757922010-09-17 14:18:16 +0200604 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300605 /* connections inherit forwarding method from dest */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200606 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
Julian Anastasov35757922010-09-17 14:18:16 +0200607 }
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200608 flags |= conn_flags;
609 cp->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 cp->dest = dest;
611
Julius Volzcfc78c52008-09-02 15:55:53 +0200612 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
613 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
614 "dest->refcnt:%d\n",
615 ip_vs_proto_name(cp->protocol),
616 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
617 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700618 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcfc78c52008-09-02 15:55:53 +0200619 ip_vs_fwd_tag(cp), cp->state,
620 cp->flags, atomic_read(&cp->refcnt),
621 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Update the connection counters */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200624 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
Julian Anastasov06611f82012-04-24 23:46:36 +0300625 /* It is a normal connection, so modify the counters
626 * according to the flags, later the protocol can
627 * update them on state change
628 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200629 if (!(flags & IP_VS_CONN_F_INACTIVE))
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800630 atomic_inc(&dest->activeconns);
631 else
632 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 } else {
634 /* It is a persistent connection/template, so increase
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300635 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 atomic_inc(&dest->persistconns);
637 }
638
639 if (dest->u_threshold != 0 &&
640 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
641 dest->flags |= IP_VS_DEST_F_OVERLOAD;
642}
643
644
645/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800646 * Check if there is a destination for the connection, if so
647 * bind the connection to the destination.
648 */
Julian Anastasov413c2d042013-03-22 11:46:52 +0200649void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800650{
651 struct ip_vs_dest *dest;
652
Julian Anastasov413c2d042013-03-22 11:46:52 +0200653 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -0700654
655 /* This function is only invoked by the synchronization code. We do
656 * not currently support heterogeneous pools with synchronization,
657 * so we can make the assumption that the svc_af is the same as the
658 * dest_af
659 */
Eric W. Biedermandc2add62015-09-21 13:01:51 -0500660 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
Julian Anastasov882a8442012-04-24 23:46:37 +0300661 cp->dport, &cp->vaddr, cp->vport,
662 cp->protocol, cp->fwmark, cp->flags);
663 if (dest) {
664 struct ip_vs_proto_data *pd;
665
Julian Anastasovac692692013-03-22 11:46:54 +0200666 spin_lock_bh(&cp->lock);
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200667 if (cp->dest) {
Julian Anastasovac692692013-03-22 11:46:54 +0200668 spin_unlock_bh(&cp->lock);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200669 rcu_read_unlock();
670 return;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200671 }
672
Julian Anastasov882a8442012-04-24 23:46:37 +0300673 /* Applications work depending on the forwarding method
674 * but better to reassign them always when binding dest */
675 if (cp->app)
676 ip_vs_unbind_app(cp);
677
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800678 ip_vs_bind_dest(cp, dest);
Julian Anastasovac692692013-03-22 11:46:54 +0200679 spin_unlock_bh(&cp->lock);
Julian Anastasov882a8442012-04-24 23:46:37 +0300680
681 /* Update its packet transmitter */
682 cp->packet_xmit = NULL;
683#ifdef CONFIG_IP_VS_IPV6
684 if (cp->af == AF_INET6)
685 ip_vs_bind_xmit_v6(cp);
686 else
687#endif
688 ip_vs_bind_xmit(cp);
689
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500690 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
Julian Anastasov882a8442012-04-24 23:46:37 +0300691 if (pd && atomic_read(&pd->appcnt))
692 ip_vs_bind_app(cp, pd->pp);
693 }
Julian Anastasov413c2d042013-03-22 11:46:52 +0200694 rcu_read_unlock();
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800695}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800696
697
698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * Unbind a connection entry with its VS destination
700 * Called by the ip_vs_conn_expire function.
701 */
702static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
703{
704 struct ip_vs_dest *dest = cp->dest;
705
706 if (!dest)
707 return;
708
Julius Volzcfc78c52008-09-02 15:55:53 +0200709 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
710 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
711 "dest->refcnt:%d\n",
712 ip_vs_proto_name(cp->protocol),
713 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
714 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700715 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcfc78c52008-09-02 15:55:53 +0200716 ip_vs_fwd_tag(cp), cp->state,
717 cp->flags, atomic_read(&cp->refcnt),
718 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700721 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* It is a normal connection, so decrease the inactconns
723 or activeconns counter */
724 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
725 atomic_dec(&dest->inactconns);
726 } else {
727 atomic_dec(&dest->activeconns);
728 }
729 } else {
730 /* It is a persistent connection/template, so decrease
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300731 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 atomic_dec(&dest->persistconns);
733 }
734
735 if (dest->l_threshold != 0) {
736 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
737 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
738 } else if (dest->u_threshold != 0) {
739 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
740 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
741 } else {
742 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
743 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
744 }
745
Julian Anastasovfca9c202013-03-22 11:46:38 +0200746 ip_vs_dest_put(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Simon Horman8e1b0b12011-02-04 18:33:01 +0900749static int expire_quiescent_template(struct netns_ipvs *ipvs,
750 struct ip_vs_dest *dest)
751{
752#ifdef CONFIG_SYSCTL
753 return ipvs->sysctl_expire_quiescent_template &&
754 (atomic_read(&dest->weight) == 0);
755#else
756 return 0;
757#endif
758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760/*
761 * Checking if the destination of a connection template is available.
762 * If available, return 1, otherwise invalidate this connection
763 * template and return 0.
764 */
Marco Angaroni3ec10d32016-05-16 19:18:09 +0200765int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct ip_vs_dest *dest = ct->dest;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500768 struct netns_ipvs *ipvs = ct->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /*
771 * Checking the dest server status.
772 */
773 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900774 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
Marco Angaroni3ec10d32016-05-16 19:18:09 +0200775 expire_quiescent_template(ipvs, dest) ||
776 (cdest && (dest != cdest))) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200777 IP_VS_DBG_BUF(9, "check_template: dest not available for "
778 "protocol %s s:%s:%d v:%s:%d "
779 "-> d:%s:%d\n",
780 ip_vs_proto_name(ct->protocol),
781 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
782 ntohs(ct->cport),
783 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
784 ntohs(ct->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700785 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
Julius Volzcfc78c52008-09-02 15:55:53 +0200786 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /*
789 * Invalidate the connection template
790 */
Al Viro014d7302006-09-28 14:29:52 -0700791 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700793 ct->dport = htons(0xffff);
794 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 ct->cport = 0;
796 ip_vs_conn_hash(ct);
797 }
798 }
799
800 /*
801 * Simply decrease the refcnt of the template,
802 * don't restart its timer.
803 */
Julian Anastasov088339a2013-03-21 11:58:10 +0200804 __ip_vs_conn_put(ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806 }
807 return 1;
808}
809
Julian Anastasov088339a2013-03-21 11:58:10 +0200810static void ip_vs_conn_rcu_free(struct rcu_head *head)
811{
812 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
813 rcu_head);
814
815 ip_vs_pe_put(cp->pe);
816 kfree(cp->pe_data);
817 kmem_cache_free(ip_vs_conn_cachep, cp);
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static void ip_vs_conn_expire(unsigned long data)
821{
822 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500823 struct netns_ipvs *ipvs = cp->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * do I control anybody?
827 */
828 if (atomic_read(&cp->n_control))
829 goto expire_later;
830
Julian Anastasov088339a2013-03-21 11:58:10 +0200831 /* Unlink conn if not referenced anymore */
832 if (likely(ip_vs_conn_unlink(cp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* delete the timer if it is activated by other users */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000834 del_timer(&cp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* does anybody control me? */
837 if (cp->control)
838 ip_vs_control_del(cp);
839
Marco Angaroni8fb04d92016-04-09 14:14:23 +0200840 if ((cp->flags & IP_VS_CONN_F_NFCT) &&
841 !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) {
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200842 /* Do not access conntracks during subsys cleanup
843 * because nf_conntrack_find_get can not be used after
844 * conntrack cleanup for the net.
845 */
846 smp_rmb();
847 if (ipvs->enable)
848 ip_vs_conn_drop_conntrack(cp);
849 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (unlikely(cp->app != NULL))
852 ip_vs_unbind_app(cp);
853 ip_vs_unbind_dest(cp);
854 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
855 atomic_dec(&ip_vs_conn_no_cport_cnt);
Marco Angaroni013b0422016-04-05 18:26:52 +0200856 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
857 ip_vs_conn_rcu_free(&cp->rcu_head);
858 else
859 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100860 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return;
862 }
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200865 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
866 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 atomic_read(&cp->n_control));
868
Julian Anastasov088339a2013-03-21 11:58:10 +0200869 atomic_inc(&cp->refcnt);
870 cp->timeout = 60*HZ;
871
Julian Anastasov749c42b2012-04-24 23:46:40 +0300872 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -0500873 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
Julian Anastasov749c42b2012-04-24 23:46:40 +0300874
Marco Angaroni013b0422016-04-05 18:26:52 +0200875 __ip_vs_conn_put_timer(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Julian Anastasov088339a2013-03-21 11:58:10 +0200878/* Modify timer, so that it expires as soon as possible.
879 * Can be called without reference only if under RCU lock.
880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
882{
Julian Anastasov088339a2013-03-21 11:58:10 +0200883 /* Using mod_timer_pending will ensure the timer is not
884 * modified after the final del_timer in ip_vs_conn_expire.
885 */
886 if (timer_pending(&cp->timer) &&
887 time_after(cp->timer.expires, jiffies))
888 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891
892/*
893 * Create a new connection entry and hash it into the ip_vs_conn_tab
894 */
895struct ip_vs_conn *
Alex Gartrellba385282014-09-09 16:40:23 -0700896ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
Eric Dumazet95c96172012-04-15 05:58:06 +0000897 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100898 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 struct ip_vs_conn *cp;
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500901 struct netns_ipvs *ipvs = p->ipvs;
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500902 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100903 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200905 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000907 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return NULL;
909 }
910
Changli Gao731109e2011-02-19 18:05:08 +0800911 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800912 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500913 cp->ipvs = ipvs;
Simon Hormanf11017e2010-08-22 21:37:52 +0900914 cp->af = p->af;
Alex Gartrellba385282014-09-09 16:40:23 -0700915 cp->daf = dest_af;
Simon Hormanf11017e2010-08-22 21:37:52 +0900916 cp->protocol = p->protocol;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200917 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
Simon Hormanf11017e2010-08-22 21:37:52 +0900918 cp->cport = p->cport;
Michal Kubecek2a971352014-01-30 08:50:20 +0100919 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200920 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Michal Kubecek2a971352014-01-30 08:50:20 +0100921 &cp->vaddr, p->vaddr);
922 cp->vport = p->vport;
Alex Gartrellba385282014-09-09 16:40:23 -0700923 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 cp->dport = dport;
925 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100926 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900927 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
928 ip_vs_pe_get(p->pe);
929 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900930 cp->pe_data = p->pe_data;
931 cp->pe_data_len = p->pe_data_len;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200932 } else {
933 cp->pe = NULL;
934 cp->pe_data = NULL;
935 cp->pe_data_len = 0;
Simon Horman85999282010-08-22 21:37:53 +0900936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 spin_lock_init(&cp->lock);
938
939 /*
940 * Set the entry is referenced by the current thread before hashing
941 * it in the table, so that other thread run ip_vs_random_dropentry
942 * but cannot drop this entry.
943 */
944 atomic_set(&cp->refcnt, 1);
945
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200946 cp->control = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 atomic_set(&cp->n_control, 0);
948 atomic_set(&cp->in_pkts, 0);
949
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200950 cp->packet_xmit = NULL;
951 cp->app = NULL;
952 cp->app_data = NULL;
953 /* reset struct ip_vs_seq */
954 cp->in_seq.delta = 0;
955 cp->out_seq.delta = 0;
956
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100957 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (flags & IP_VS_CONN_F_NO_CPORT)
959 atomic_inc(&ip_vs_conn_no_cport_cnt);
960
961 /* Bind the connection with a destination server */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200962 cp->dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 ip_vs_bind_dest(cp, dest);
964
965 /* Set its state and timeout */
966 cp->state = 0;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200967 cp->old_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300969 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200972#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900973 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200974 ip_vs_bind_xmit_v6(cp);
975 else
976#endif
977 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100979 if (unlikely(pd && atomic_read(&pd->appcnt)))
980 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200982 /*
983 * Allow conntrack to be preserved. By default, conntrack
984 * is created and destroyed for every packet.
985 * Sometimes keeping conntrack can be useful for
986 * IP_VS_CONN_F_ONE_PACKET too.
987 */
988
Hans Schillstroma0840e22011-01-03 14:44:58 +0100989 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200990 cp->flags |= IP_VS_CONN_F_NFCT;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Hash it in the ip_vs_conn_tab finally */
993 ip_vs_conn_hash(cp);
994
995 return cp;
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * /proc/net/ip_vs_conn entries
1000 */
1001#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001002struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +08001003 struct seq_net_private p;
1004 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001005};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
1008{
1009 int idx;
1010 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001011 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001012
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001013 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001014 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1015 /* __ip_vs_conn_get() is not needed by
1016 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
1017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001019 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +08001020 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001023 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 return NULL;
1027}
1028
1029static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001030 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001032 struct ip_vs_iter_state *iter = seq->private;
1033
1034 iter->l = NULL;
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001035 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1037}
1038
1039static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1040{
1041 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001042 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +02001043 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +08001044 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int idx;
1046
1047 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001048 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return ip_vs_conn_array(seq, 0);
1050
1051 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +02001052 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1053 if (e)
1054 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001057 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001058 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001059 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001061 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001062 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001064 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return NULL;
1066}
1067
1068static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001069 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001071 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1075{
1076
1077 if (v == SEQ_START_TOKEN)
1078 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001079 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 else {
1081 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001082 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001083 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1084 size_t len = 0;
Julian Anastasovf18ae722014-09-09 16:40:38 -07001085 char dbuf[IP_VS_ADDRSTRLEN];
Simon Hormana3c918a2010-08-22 21:37:53 +09001086
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001087 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001088 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001089 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001090 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001091 len = strlen(cp->pe->name);
1092 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001093 pe_data[len + 1] = ' ';
1094 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001095 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001096 }
1097 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Vince Busam667a5f12008-09-02 15:55:49 +02001099#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001100 if (cp->daf == AF_INET6)
1101 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1102 else
1103#endif
1104 snprintf(dbuf, sizeof(dbuf), "%08X",
1105 ntohl(cp->daddr.ip));
1106
1107#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001108 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001109 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001110 "%s %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001111 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001112 &cp->caddr.in6, ntohs(cp->cport),
1113 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001114 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001115 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001116 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001117 else
1118#endif
1119 seq_printf(seq,
1120 "%-3s %08X %04X %08X %04X"
Julian Anastasovf18ae722014-09-09 16:40:38 -07001121 " %s %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001123 ntohl(cp->caddr.ip), ntohs(cp->cport),
1124 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001125 dbuf, ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001127 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 return 0;
1130}
1131
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001132static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 .start = ip_vs_conn_seq_start,
1134 .next = ip_vs_conn_seq_next,
1135 .stop = ip_vs_conn_seq_stop,
1136 .show = ip_vs_conn_seq_show,
1137};
1138
1139static int ip_vs_conn_open(struct inode *inode, struct file *file)
1140{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001141 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1142 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Arjan van de Ven9a321442007-02-12 00:55:35 -08001145static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 .owner = THIS_MODULE,
1147 .open = ip_vs_conn_open,
1148 .read = seq_read,
1149 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001150 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001152
Eric Dumazet95c96172012-04-15 05:58:06 +00001153static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001154{
1155 if (flags & IP_VS_CONN_F_SYNC)
1156 return "SYNC";
1157 else
1158 return "LOCAL";
1159}
1160
1161static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1162{
Julian Anastasovf18ae722014-09-09 16:40:38 -07001163 char dbuf[IP_VS_ADDRSTRLEN];
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001164
1165 if (v == SEQ_START_TOKEN)
1166 seq_puts(seq,
1167 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1168 else {
1169 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001170 struct net *net = seq_file_net(seq);
1171
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001172 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001173 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001174
Vince Busam667a5f12008-09-02 15:55:49 +02001175#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001176 if (cp->daf == AF_INET6)
1177 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1178 else
1179#endif
1180 snprintf(dbuf, sizeof(dbuf), "%08X",
1181 ntohl(cp->daddr.ip));
1182
1183#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001184 if (cp->af == AF_INET6)
Julian Anastasovf18ae722014-09-09 16:40:38 -07001185 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1186 "%s %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001187 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001188 &cp->caddr.in6, ntohs(cp->cport),
1189 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001190 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001191 ip_vs_state_name(cp->protocol, cp->state),
1192 ip_vs_origin_name(cp->flags),
1193 (cp->timer.expires-jiffies)/HZ);
1194 else
1195#endif
1196 seq_printf(seq,
1197 "%-3s %08X %04X %08X %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001198 "%s %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001199 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001200 ntohl(cp->caddr.ip), ntohs(cp->cport),
1201 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001202 dbuf, ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001203 ip_vs_state_name(cp->protocol, cp->state),
1204 ip_vs_origin_name(cp->flags),
1205 (cp->timer.expires-jiffies)/HZ);
1206 }
1207 return 0;
1208}
1209
1210static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1211 .start = ip_vs_conn_seq_start,
1212 .next = ip_vs_conn_seq_next,
1213 .stop = ip_vs_conn_seq_stop,
1214 .show = ip_vs_conn_sync_seq_show,
1215};
1216
1217static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1218{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001219 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1220 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001221}
1222
1223static const struct file_operations ip_vs_conn_sync_fops = {
1224 .owner = THIS_MODULE,
1225 .open = ip_vs_conn_sync_open,
1226 .read = seq_read,
1227 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001228 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001229};
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231#endif
1232
1233
1234/*
1235 * Randomly drop connection entries before running out of memory
1236 */
1237static inline int todrop_entry(struct ip_vs_conn *cp)
1238{
1239 /*
1240 * The drop rate array needs tuning for real environments.
1241 * Called from timer bh only => no locking
1242 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001243 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 static char todrop_counter[9] = {0};
1245 int i;
1246
1247 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1248 This will leave enough time for normal connection to get
1249 through. */
1250 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1251 return 0;
1252
1253 /* Don't drop the entry if its number of incoming packets is not
1254 located in [0, 8] */
1255 i = atomic_read(&cp->in_pkts);
1256 if (i > 8 || i < 0) return 0;
1257
1258 if (!todrop_rate[i]) return 0;
1259 if (--todrop_counter[i] > 0) return 0;
1260
1261 todrop_counter[i] = todrop_rate[i];
1262 return 1;
1263}
1264
Marco Angaroni698e2a82016-04-26 21:20:22 +02001265static inline bool ip_vs_conn_ops_mode(struct ip_vs_conn *cp)
1266{
1267 struct ip_vs_service *svc;
1268
1269 if (!cp->dest)
1270 return false;
1271 svc = rcu_dereference(cp->dest->svc);
1272 return svc && (svc->flags & IP_VS_SVC_F_ONEPACKET);
1273}
1274
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001275/* Called from keventd and must protect itself from softirqs */
Eric W. Biederman423b5592015-09-21 13:02:24 -05001276void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001279 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Simon Hormana38e5e22013-05-22 14:50:32 +09001281 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * Randomly scan 1/32 of the whole table every second
1284 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001285 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001286 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Julian Anastasov088339a2013-03-21 11:58:10 +02001288 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Eric W. Biederman423b5592015-09-21 13:02:24 -05001289 if (cp->ipvs != ipvs)
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001290 continue;
Marco Angaroni698e2a82016-04-26 21:20:22 +02001291 if (cp->flags & IP_VS_CONN_F_TEMPLATE) {
1292 if (atomic_read(&cp->n_control) ||
1293 !ip_vs_conn_ops_mode(cp))
1294 continue;
1295 else
1296 /* connection template of OPS */
1297 goto try_drop;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (cp->protocol == IPPROTO_TCP) {
1300 switch(cp->state) {
1301 case IP_VS_TCP_S_SYN_RECV:
1302 case IP_VS_TCP_S_SYNACK:
1303 break;
1304
1305 case IP_VS_TCP_S_ESTABLISHED:
1306 if (todrop_entry(cp))
1307 break;
1308 continue;
1309
1310 default:
1311 continue;
1312 }
Julian Anastasovacaac5d2013-06-18 10:08:08 +03001313 } else if (cp->protocol == IPPROTO_SCTP) {
1314 switch (cp->state) {
1315 case IP_VS_SCTP_S_INIT1:
1316 case IP_VS_SCTP_S_INIT:
1317 break;
1318 case IP_VS_SCTP_S_ESTABLISHED:
1319 if (todrop_entry(cp))
1320 break;
1321 continue;
1322 default:
1323 continue;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else {
Marco Angaroni698e2a82016-04-26 21:20:22 +02001326try_drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (!todrop_entry(cp))
1328 continue;
1329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 IP_VS_DBG(4, "del connection\n");
1332 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001333 cp_c = cp->control;
1334 /* cp->control is valid only with reference to cp */
1335 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001337 ip_vs_conn_expire_now(cp_c);
1338 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001341 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001343 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346
1347/*
1348 * Flush all the connection entries in the ip_vs_conn_tab
1349 */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001350static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001353 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Hans Schillstroma0840e22011-01-03 14:44:58 +01001355flush_again:
Simon Hormana38e5e22013-05-22 14:50:32 +09001356 rcu_read_lock();
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001357 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Julian Anastasov088339a2013-03-21 11:58:10 +02001359 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001360 if (cp->ipvs != ipvs)
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001361 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 IP_VS_DBG(4, "del connection\n");
1363 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001364 cp_c = cp->control;
1365 /* cp->control is valid only with reference to cp */
1366 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001368 ip_vs_conn_expire_now(cp_c);
1369 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001372 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001374 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* the counter may be not NULL, because maybe some conn entries
1377 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001378 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 schedule();
1380 goto flush_again;
1381 }
1382}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001383/*
1384 * per netns init and exit
1385 */
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05001386int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001387{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001388 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Simon Horman92240e82015-10-06 17:09:52 +09001390 proc_create("ip_vs_conn", 0, ipvs->net->proc_net, &ip_vs_conn_fops);
1391 proc_create("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1392 &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001393 return 0;
1394}
1395
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05001396void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001397{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001398 /* flush all the connection entries first */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001399 ip_vs_conn_flush(ipvs);
Simon Horman92240e82015-10-06 17:09:52 +09001400 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1401 remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Sven Wegener048cf482008-08-10 18:24:35 +00001404int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 int idx;
1407
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001408 /* Compute size and mask */
1409 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1410 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
1413 * Allocate the connection hash table and initialize its list heads
1414 */
Changli Gao731109e2011-02-19 18:05:08 +08001415 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!ip_vs_conn_tab)
1417 return -ENOMEM;
1418
1419 /* Allocate ip_vs_conn slab cache */
1420 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1421 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001422 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!ip_vs_conn_cachep) {
1424 vfree(ip_vs_conn_tab);
1425 return -ENOMEM;
1426 }
1427
Hannes Eder1e3e2382009-08-02 11:05:41 +00001428 pr_info("Connection hash table configured "
1429 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001430 ip_vs_conn_tab_size,
1431 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1433 sizeof(struct ip_vs_conn));
1434
Changli Gao731109e2011-02-19 18:05:08 +08001435 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1436 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001439 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* calculate the random value for connection hash */
1443 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1444
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448void ip_vs_conn_cleanup(void)
1449{
Julian Anastasov088339a2013-03-21 11:58:10 +02001450 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1451 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* Release the empty cache */
1453 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 vfree(ip_vs_conn_tab);
1455}