blob: 292365ffa4f029a8a94e3e9ed80c1fc028e74059 [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 */
765int ip_vs_check_template(struct ip_vs_conn *ct)
766{
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) ||
Simon Horman8e1b0b12011-02-04 18:33:01 +0900775 expire_quiescent_template(ipvs, dest)) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200776 IP_VS_DBG_BUF(9, "check_template: dest not available for "
777 "protocol %s s:%s:%d v:%s:%d "
778 "-> d:%s:%d\n",
779 ip_vs_proto_name(ct->protocol),
780 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
781 ntohs(ct->cport),
782 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
783 ntohs(ct->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700784 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
Julius Volzcfc78c52008-09-02 15:55:53 +0200785 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /*
788 * Invalidate the connection template
789 */
Al Viro014d7302006-09-28 14:29:52 -0700790 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700792 ct->dport = htons(0xffff);
793 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ct->cport = 0;
795 ip_vs_conn_hash(ct);
796 }
797 }
798
799 /*
800 * Simply decrease the refcnt of the template,
801 * don't restart its timer.
802 */
Julian Anastasov088339a2013-03-21 11:58:10 +0200803 __ip_vs_conn_put(ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805 }
806 return 1;
807}
808
Julian Anastasov088339a2013-03-21 11:58:10 +0200809static void ip_vs_conn_rcu_free(struct rcu_head *head)
810{
811 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
812 rcu_head);
813
814 ip_vs_pe_put(cp->pe);
815 kfree(cp->pe_data);
816 kmem_cache_free(ip_vs_conn_cachep, cp);
817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819static void ip_vs_conn_expire(unsigned long data)
820{
821 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500822 struct netns_ipvs *ipvs = cp->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * do I control anybody?
826 */
827 if (atomic_read(&cp->n_control))
828 goto expire_later;
829
Julian Anastasov088339a2013-03-21 11:58:10 +0200830 /* Unlink conn if not referenced anymore */
831 if (likely(ip_vs_conn_unlink(cp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* delete the timer if it is activated by other users */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000833 del_timer(&cp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* does anybody control me? */
836 if (cp->control)
837 ip_vs_control_del(cp);
838
Marco Angaroni8fb04d92016-04-09 14:14:23 +0200839 if ((cp->flags & IP_VS_CONN_F_NFCT) &&
840 !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) {
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200841 /* Do not access conntracks during subsys cleanup
842 * because nf_conntrack_find_get can not be used after
843 * conntrack cleanup for the net.
844 */
845 smp_rmb();
846 if (ipvs->enable)
847 ip_vs_conn_drop_conntrack(cp);
848 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (unlikely(cp->app != NULL))
851 ip_vs_unbind_app(cp);
852 ip_vs_unbind_dest(cp);
853 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
854 atomic_dec(&ip_vs_conn_no_cport_cnt);
Marco Angaroni013b0422016-04-05 18:26:52 +0200855 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
856 ip_vs_conn_rcu_free(&cp->rcu_head);
857 else
858 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100859 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return;
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200864 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
865 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 atomic_read(&cp->n_control));
867
Julian Anastasov088339a2013-03-21 11:58:10 +0200868 atomic_inc(&cp->refcnt);
869 cp->timeout = 60*HZ;
870
Julian Anastasov749c42b2012-04-24 23:46:40 +0300871 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -0500872 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
Julian Anastasov749c42b2012-04-24 23:46:40 +0300873
Marco Angaroni013b0422016-04-05 18:26:52 +0200874 __ip_vs_conn_put_timer(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Julian Anastasov088339a2013-03-21 11:58:10 +0200877/* Modify timer, so that it expires as soon as possible.
878 * Can be called without reference only if under RCU lock.
879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
881{
Julian Anastasov088339a2013-03-21 11:58:10 +0200882 /* Using mod_timer_pending will ensure the timer is not
883 * modified after the final del_timer in ip_vs_conn_expire.
884 */
885 if (timer_pending(&cp->timer) &&
886 time_after(cp->timer.expires, jiffies))
887 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890
891/*
892 * Create a new connection entry and hash it into the ip_vs_conn_tab
893 */
894struct ip_vs_conn *
Alex Gartrellba385282014-09-09 16:40:23 -0700895ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
Eric Dumazet95c96172012-04-15 05:58:06 +0000896 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100897 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct ip_vs_conn *cp;
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500900 struct netns_ipvs *ipvs = p->ipvs;
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500901 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100902 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200904 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000906 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return NULL;
908 }
909
Changli Gao731109e2011-02-19 18:05:08 +0800910 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800911 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500912 cp->ipvs = ipvs;
Simon Hormanf11017e2010-08-22 21:37:52 +0900913 cp->af = p->af;
Alex Gartrellba385282014-09-09 16:40:23 -0700914 cp->daf = dest_af;
Simon Hormanf11017e2010-08-22 21:37:52 +0900915 cp->protocol = p->protocol;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200916 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
Simon Hormanf11017e2010-08-22 21:37:52 +0900917 cp->cport = p->cport;
Michal Kubecek2a971352014-01-30 08:50:20 +0100918 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200919 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Michal Kubecek2a971352014-01-30 08:50:20 +0100920 &cp->vaddr, p->vaddr);
921 cp->vport = p->vport;
Alex Gartrellba385282014-09-09 16:40:23 -0700922 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 cp->dport = dport;
924 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100925 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900926 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
927 ip_vs_pe_get(p->pe);
928 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900929 cp->pe_data = p->pe_data;
930 cp->pe_data_len = p->pe_data_len;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200931 } else {
932 cp->pe = NULL;
933 cp->pe_data = NULL;
934 cp->pe_data_len = 0;
Simon Horman85999282010-08-22 21:37:53 +0900935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 spin_lock_init(&cp->lock);
937
938 /*
939 * Set the entry is referenced by the current thread before hashing
940 * it in the table, so that other thread run ip_vs_random_dropentry
941 * but cannot drop this entry.
942 */
943 atomic_set(&cp->refcnt, 1);
944
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200945 cp->control = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 atomic_set(&cp->n_control, 0);
947 atomic_set(&cp->in_pkts, 0);
948
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200949 cp->packet_xmit = NULL;
950 cp->app = NULL;
951 cp->app_data = NULL;
952 /* reset struct ip_vs_seq */
953 cp->in_seq.delta = 0;
954 cp->out_seq.delta = 0;
955
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100956 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (flags & IP_VS_CONN_F_NO_CPORT)
958 atomic_inc(&ip_vs_conn_no_cport_cnt);
959
960 /* Bind the connection with a destination server */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200961 cp->dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 ip_vs_bind_dest(cp, dest);
963
964 /* Set its state and timeout */
965 cp->state = 0;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200966 cp->old_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300968 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200971#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900972 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200973 ip_vs_bind_xmit_v6(cp);
974 else
975#endif
976 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100978 if (unlikely(pd && atomic_read(&pd->appcnt)))
979 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200981 /*
982 * Allow conntrack to be preserved. By default, conntrack
983 * is created and destroyed for every packet.
984 * Sometimes keeping conntrack can be useful for
985 * IP_VS_CONN_F_ONE_PACKET too.
986 */
987
Hans Schillstroma0840e22011-01-03 14:44:58 +0100988 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200989 cp->flags |= IP_VS_CONN_F_NFCT;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* Hash it in the ip_vs_conn_tab finally */
992 ip_vs_conn_hash(cp);
993
994 return cp;
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/*
998 * /proc/net/ip_vs_conn entries
999 */
1000#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001001struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +08001002 struct seq_net_private p;
1003 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001004};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
1007{
1008 int idx;
1009 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001010 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001011
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001012 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001013 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1014 /* __ip_vs_conn_get() is not needed by
1015 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
1016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001018 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +08001019 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001022 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
1025 return NULL;
1026}
1027
1028static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001029 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001031 struct ip_vs_iter_state *iter = seq->private;
1032
1033 iter->l = NULL;
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001034 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1036}
1037
1038static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1039{
1040 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001041 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +02001042 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +08001043 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int idx;
1045
1046 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001047 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return ip_vs_conn_array(seq, 0);
1049
1050 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +02001051 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1052 if (e)
1053 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001056 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001057 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001058 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001060 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001061 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001063 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return NULL;
1065}
1066
1067static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001068 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001070 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1074{
1075
1076 if (v == SEQ_START_TOKEN)
1077 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001078 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 else {
1080 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001081 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001082 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1083 size_t len = 0;
Julian Anastasovf18ae722014-09-09 16:40:38 -07001084 char dbuf[IP_VS_ADDRSTRLEN];
Simon Hormana3c918a2010-08-22 21:37:53 +09001085
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001086 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001087 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001088 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001089 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001090 len = strlen(cp->pe->name);
1091 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001092 pe_data[len + 1] = ' ';
1093 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001094 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001095 }
1096 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Vince Busam667a5f12008-09-02 15:55:49 +02001098#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001099 if (cp->daf == AF_INET6)
1100 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1101 else
1102#endif
1103 snprintf(dbuf, sizeof(dbuf), "%08X",
1104 ntohl(cp->daddr.ip));
1105
1106#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001107 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001108 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001109 "%s %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001110 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001111 &cp->caddr.in6, ntohs(cp->cport),
1112 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001113 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001114 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001115 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001116 else
1117#endif
1118 seq_printf(seq,
1119 "%-3s %08X %04X %08X %04X"
Julian Anastasovf18ae722014-09-09 16:40:38 -07001120 " %s %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001122 ntohl(cp->caddr.ip), ntohs(cp->cport),
1123 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001124 dbuf, ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001126 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 return 0;
1129}
1130
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001131static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 .start = ip_vs_conn_seq_start,
1133 .next = ip_vs_conn_seq_next,
1134 .stop = ip_vs_conn_seq_stop,
1135 .show = ip_vs_conn_seq_show,
1136};
1137
1138static int ip_vs_conn_open(struct inode *inode, struct file *file)
1139{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001140 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1141 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Arjan van de Ven9a321442007-02-12 00:55:35 -08001144static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 .owner = THIS_MODULE,
1146 .open = ip_vs_conn_open,
1147 .read = seq_read,
1148 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001149 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001151
Eric Dumazet95c96172012-04-15 05:58:06 +00001152static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001153{
1154 if (flags & IP_VS_CONN_F_SYNC)
1155 return "SYNC";
1156 else
1157 return "LOCAL";
1158}
1159
1160static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1161{
Julian Anastasovf18ae722014-09-09 16:40:38 -07001162 char dbuf[IP_VS_ADDRSTRLEN];
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001163
1164 if (v == SEQ_START_TOKEN)
1165 seq_puts(seq,
1166 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1167 else {
1168 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001169 struct net *net = seq_file_net(seq);
1170
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001171 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001172 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001173
Vince Busam667a5f12008-09-02 15:55:49 +02001174#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001175 if (cp->daf == AF_INET6)
1176 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1177 else
1178#endif
1179 snprintf(dbuf, sizeof(dbuf), "%08X",
1180 ntohl(cp->daddr.ip));
1181
1182#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001183 if (cp->af == AF_INET6)
Julian Anastasovf18ae722014-09-09 16:40:38 -07001184 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1185 "%s %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001186 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001187 &cp->caddr.in6, ntohs(cp->cport),
1188 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001189 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001190 ip_vs_state_name(cp->protocol, cp->state),
1191 ip_vs_origin_name(cp->flags),
1192 (cp->timer.expires-jiffies)/HZ);
1193 else
1194#endif
1195 seq_printf(seq,
1196 "%-3s %08X %04X %08X %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001197 "%s %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001198 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001199 ntohl(cp->caddr.ip), ntohs(cp->cport),
1200 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001201 dbuf, ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001202 ip_vs_state_name(cp->protocol, cp->state),
1203 ip_vs_origin_name(cp->flags),
1204 (cp->timer.expires-jiffies)/HZ);
1205 }
1206 return 0;
1207}
1208
1209static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1210 .start = ip_vs_conn_seq_start,
1211 .next = ip_vs_conn_seq_next,
1212 .stop = ip_vs_conn_seq_stop,
1213 .show = ip_vs_conn_sync_seq_show,
1214};
1215
1216static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1217{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001218 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1219 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001220}
1221
1222static const struct file_operations ip_vs_conn_sync_fops = {
1223 .owner = THIS_MODULE,
1224 .open = ip_vs_conn_sync_open,
1225 .read = seq_read,
1226 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001227 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001228};
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230#endif
1231
1232
1233/*
1234 * Randomly drop connection entries before running out of memory
1235 */
1236static inline int todrop_entry(struct ip_vs_conn *cp)
1237{
1238 /*
1239 * The drop rate array needs tuning for real environments.
1240 * Called from timer bh only => no locking
1241 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001242 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 static char todrop_counter[9] = {0};
1244 int i;
1245
1246 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1247 This will leave enough time for normal connection to get
1248 through. */
1249 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1250 return 0;
1251
1252 /* Don't drop the entry if its number of incoming packets is not
1253 located in [0, 8] */
1254 i = atomic_read(&cp->in_pkts);
1255 if (i > 8 || i < 0) return 0;
1256
1257 if (!todrop_rate[i]) return 0;
1258 if (--todrop_counter[i] > 0) return 0;
1259
1260 todrop_counter[i] = todrop_rate[i];
1261 return 1;
1262}
1263
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001264/* Called from keventd and must protect itself from softirqs */
Eric W. Biederman423b5592015-09-21 13:02:24 -05001265void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001268 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Simon Hormana38e5e22013-05-22 14:50:32 +09001270 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Randomly scan 1/32 of the whole table every second
1273 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001274 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001275 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Julian Anastasov088339a2013-03-21 11:58:10 +02001277 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001278 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* connection template */
1280 continue;
Eric W. Biederman423b5592015-09-21 13:02:24 -05001281 if (cp->ipvs != ipvs)
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001282 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (cp->protocol == IPPROTO_TCP) {
1284 switch(cp->state) {
1285 case IP_VS_TCP_S_SYN_RECV:
1286 case IP_VS_TCP_S_SYNACK:
1287 break;
1288
1289 case IP_VS_TCP_S_ESTABLISHED:
1290 if (todrop_entry(cp))
1291 break;
1292 continue;
1293
1294 default:
1295 continue;
1296 }
Julian Anastasovacaac5d2013-06-18 10:08:08 +03001297 } else if (cp->protocol == IPPROTO_SCTP) {
1298 switch (cp->state) {
1299 case IP_VS_SCTP_S_INIT1:
1300 case IP_VS_SCTP_S_INIT:
1301 break;
1302 case IP_VS_SCTP_S_ESTABLISHED:
1303 if (todrop_entry(cp))
1304 break;
1305 continue;
1306 default:
1307 continue;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 } else {
1310 if (!todrop_entry(cp))
1311 continue;
1312 }
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 IP_VS_DBG(4, "del connection\n");
1315 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001316 cp_c = cp->control;
1317 /* cp->control is valid only with reference to cp */
1318 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001320 ip_vs_conn_expire_now(cp_c);
1321 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001324 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329
1330/*
1331 * Flush all the connection entries in the ip_vs_conn_tab
1332 */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001333static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001336 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Hans Schillstroma0840e22011-01-03 14:44:58 +01001338flush_again:
Simon Hormana38e5e22013-05-22 14:50:32 +09001339 rcu_read_lock();
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001340 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Julian Anastasov088339a2013-03-21 11:58:10 +02001342 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001343 if (cp->ipvs != ipvs)
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001344 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 IP_VS_DBG(4, "del connection\n");
1346 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001347 cp_c = cp->control;
1348 /* cp->control is valid only with reference to cp */
1349 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001351 ip_vs_conn_expire_now(cp_c);
1352 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001355 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001357 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* the counter may be not NULL, because maybe some conn entries
1360 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001361 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 schedule();
1363 goto flush_again;
1364 }
1365}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001366/*
1367 * per netns init and exit
1368 */
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05001369int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001370{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001371 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Simon Horman92240e82015-10-06 17:09:52 +09001373 proc_create("ip_vs_conn", 0, ipvs->net->proc_net, &ip_vs_conn_fops);
1374 proc_create("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1375 &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001376 return 0;
1377}
1378
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05001379void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001380{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001381 /* flush all the connection entries first */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001382 ip_vs_conn_flush(ipvs);
Simon Horman92240e82015-10-06 17:09:52 +09001383 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1384 remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Sven Wegener048cf482008-08-10 18:24:35 +00001387int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 int idx;
1390
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001391 /* Compute size and mask */
1392 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1393 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * Allocate the connection hash table and initialize its list heads
1397 */
Changli Gao731109e2011-02-19 18:05:08 +08001398 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!ip_vs_conn_tab)
1400 return -ENOMEM;
1401
1402 /* Allocate ip_vs_conn slab cache */
1403 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1404 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001405 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!ip_vs_conn_cachep) {
1407 vfree(ip_vs_conn_tab);
1408 return -ENOMEM;
1409 }
1410
Hannes Eder1e3e2382009-08-02 11:05:41 +00001411 pr_info("Connection hash table configured "
1412 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001413 ip_vs_conn_tab_size,
1414 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1416 sizeof(struct ip_vs_conn));
1417
Changli Gao731109e2011-02-19 18:05:08 +08001418 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1419 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001422 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* calculate the random value for connection hash */
1426 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1427
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431void ip_vs_conn_cleanup(void)
1432{
Julian Anastasov088339a2013-03-21 11:58:10 +02001433 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1434 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* Release the empty cache */
1436 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 vfree(ip_vs_conn_tab);
1438}