blob: dd75d4120099b9fab7b7bb1b8da94109bdfcde69 [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
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200839 if (cp->flags & IP_VS_CONN_F_NFCT) {
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200840 /* Do not access conntracks during subsys cleanup
841 * because nf_conntrack_find_get can not be used after
842 * conntrack cleanup for the net.
843 */
844 smp_rmb();
845 if (ipvs->enable)
846 ip_vs_conn_drop_conntrack(cp);
847 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (unlikely(cp->app != NULL))
850 ip_vs_unbind_app(cp);
851 ip_vs_unbind_dest(cp);
852 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
853 atomic_dec(&ip_vs_conn_no_cport_cnt);
Marco Angaroni013b0422016-04-05 18:26:52 +0200854 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
855 ip_vs_conn_rcu_free(&cp->rcu_head);
856 else
857 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100858 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200863 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
864 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 atomic_read(&cp->n_control));
866
Julian Anastasov088339a2013-03-21 11:58:10 +0200867 atomic_inc(&cp->refcnt);
868 cp->timeout = 60*HZ;
869
Julian Anastasov749c42b2012-04-24 23:46:40 +0300870 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -0500871 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
Julian Anastasov749c42b2012-04-24 23:46:40 +0300872
Marco Angaroni013b0422016-04-05 18:26:52 +0200873 __ip_vs_conn_put_timer(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Julian Anastasov088339a2013-03-21 11:58:10 +0200876/* Modify timer, so that it expires as soon as possible.
877 * Can be called without reference only if under RCU lock.
878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
880{
Julian Anastasov088339a2013-03-21 11:58:10 +0200881 /* Using mod_timer_pending will ensure the timer is not
882 * modified after the final del_timer in ip_vs_conn_expire.
883 */
884 if (timer_pending(&cp->timer) &&
885 time_after(cp->timer.expires, jiffies))
886 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889
890/*
891 * Create a new connection entry and hash it into the ip_vs_conn_tab
892 */
893struct ip_vs_conn *
Alex Gartrellba385282014-09-09 16:40:23 -0700894ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
Eric Dumazet95c96172012-04-15 05:58:06 +0000895 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100896 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 struct ip_vs_conn *cp;
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500899 struct netns_ipvs *ipvs = p->ipvs;
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500900 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100901 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200903 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000905 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return NULL;
907 }
908
Changli Gao731109e2011-02-19 18:05:08 +0800909 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800910 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500911 cp->ipvs = ipvs;
Simon Hormanf11017e2010-08-22 21:37:52 +0900912 cp->af = p->af;
Alex Gartrellba385282014-09-09 16:40:23 -0700913 cp->daf = dest_af;
Simon Hormanf11017e2010-08-22 21:37:52 +0900914 cp->protocol = p->protocol;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200915 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
Simon Hormanf11017e2010-08-22 21:37:52 +0900916 cp->cport = p->cport;
Michal Kubecek2a971352014-01-30 08:50:20 +0100917 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200918 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Michal Kubecek2a971352014-01-30 08:50:20 +0100919 &cp->vaddr, p->vaddr);
920 cp->vport = p->vport;
Alex Gartrellba385282014-09-09 16:40:23 -0700921 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 cp->dport = dport;
923 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100924 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900925 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
926 ip_vs_pe_get(p->pe);
927 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900928 cp->pe_data = p->pe_data;
929 cp->pe_data_len = p->pe_data_len;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200930 } else {
931 cp->pe = NULL;
932 cp->pe_data = NULL;
933 cp->pe_data_len = 0;
Simon Horman85999282010-08-22 21:37:53 +0900934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 spin_lock_init(&cp->lock);
936
937 /*
938 * Set the entry is referenced by the current thread before hashing
939 * it in the table, so that other thread run ip_vs_random_dropentry
940 * but cannot drop this entry.
941 */
942 atomic_set(&cp->refcnt, 1);
943
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200944 cp->control = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 atomic_set(&cp->n_control, 0);
946 atomic_set(&cp->in_pkts, 0);
947
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200948 cp->packet_xmit = NULL;
949 cp->app = NULL;
950 cp->app_data = NULL;
951 /* reset struct ip_vs_seq */
952 cp->in_seq.delta = 0;
953 cp->out_seq.delta = 0;
954
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100955 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (flags & IP_VS_CONN_F_NO_CPORT)
957 atomic_inc(&ip_vs_conn_no_cport_cnt);
958
959 /* Bind the connection with a destination server */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200960 cp->dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 ip_vs_bind_dest(cp, dest);
962
963 /* Set its state and timeout */
964 cp->state = 0;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200965 cp->old_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300967 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200970#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900971 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200972 ip_vs_bind_xmit_v6(cp);
973 else
974#endif
975 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100977 if (unlikely(pd && atomic_read(&pd->appcnt)))
978 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200980 /*
981 * Allow conntrack to be preserved. By default, conntrack
982 * is created and destroyed for every packet.
983 * Sometimes keeping conntrack can be useful for
984 * IP_VS_CONN_F_ONE_PACKET too.
985 */
986
Hans Schillstroma0840e22011-01-03 14:44:58 +0100987 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200988 cp->flags |= IP_VS_CONN_F_NFCT;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Hash it in the ip_vs_conn_tab finally */
991 ip_vs_conn_hash(cp);
992
993 return cp;
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
997 * /proc/net/ip_vs_conn entries
998 */
999#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001000struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +08001001 struct seq_net_private p;
1002 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001003};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
1006{
1007 int idx;
1008 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001009 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001010
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001011 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001012 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1013 /* __ip_vs_conn_get() is not needed by
1014 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
1015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001017 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +08001018 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001021 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
1024 return NULL;
1025}
1026
1027static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001028 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001030 struct ip_vs_iter_state *iter = seq->private;
1031
1032 iter->l = NULL;
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001033 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1035}
1036
1037static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1038{
1039 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001040 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +02001041 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +08001042 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int idx;
1044
1045 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001046 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return ip_vs_conn_array(seq, 0);
1048
1049 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +02001050 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1051 if (e)
1052 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001055 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001056 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001057 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001059 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001060 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001062 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return NULL;
1064}
1065
1066static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001067 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001069 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1073{
1074
1075 if (v == SEQ_START_TOKEN)
1076 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001077 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 else {
1079 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001080 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001081 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1082 size_t len = 0;
Julian Anastasovf18ae722014-09-09 16:40:38 -07001083 char dbuf[IP_VS_ADDRSTRLEN];
Simon Hormana3c918a2010-08-22 21:37:53 +09001084
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001085 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001086 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001087 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001088 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001089 len = strlen(cp->pe->name);
1090 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001091 pe_data[len + 1] = ' ';
1092 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001093 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001094 }
1095 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Vince Busam667a5f12008-09-02 15:55:49 +02001097#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001098 if (cp->daf == AF_INET6)
1099 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1100 else
1101#endif
1102 snprintf(dbuf, sizeof(dbuf), "%08X",
1103 ntohl(cp->daddr.ip));
1104
1105#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001106 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001107 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001108 "%s %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001109 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001110 &cp->caddr.in6, ntohs(cp->cport),
1111 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001112 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001113 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001114 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001115 else
1116#endif
1117 seq_printf(seq,
1118 "%-3s %08X %04X %08X %04X"
Julian Anastasovf18ae722014-09-09 16:40:38 -07001119 " %s %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001121 ntohl(cp->caddr.ip), ntohs(cp->cport),
1122 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001123 dbuf, ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001125 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 return 0;
1128}
1129
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001130static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 .start = ip_vs_conn_seq_start,
1132 .next = ip_vs_conn_seq_next,
1133 .stop = ip_vs_conn_seq_stop,
1134 .show = ip_vs_conn_seq_show,
1135};
1136
1137static int ip_vs_conn_open(struct inode *inode, struct file *file)
1138{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001139 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1140 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Arjan van de Ven9a321442007-02-12 00:55:35 -08001143static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 .owner = THIS_MODULE,
1145 .open = ip_vs_conn_open,
1146 .read = seq_read,
1147 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001148 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001150
Eric Dumazet95c96172012-04-15 05:58:06 +00001151static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001152{
1153 if (flags & IP_VS_CONN_F_SYNC)
1154 return "SYNC";
1155 else
1156 return "LOCAL";
1157}
1158
1159static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1160{
Julian Anastasovf18ae722014-09-09 16:40:38 -07001161 char dbuf[IP_VS_ADDRSTRLEN];
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001162
1163 if (v == SEQ_START_TOKEN)
1164 seq_puts(seq,
1165 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1166 else {
1167 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001168 struct net *net = seq_file_net(seq);
1169
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001170 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001171 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001172
Vince Busam667a5f12008-09-02 15:55:49 +02001173#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001174 if (cp->daf == AF_INET6)
1175 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1176 else
1177#endif
1178 snprintf(dbuf, sizeof(dbuf), "%08X",
1179 ntohl(cp->daddr.ip));
1180
1181#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001182 if (cp->af == AF_INET6)
Julian Anastasovf18ae722014-09-09 16:40:38 -07001183 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1184 "%s %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001185 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001186 &cp->caddr.in6, ntohs(cp->cport),
1187 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001188 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001189 ip_vs_state_name(cp->protocol, cp->state),
1190 ip_vs_origin_name(cp->flags),
1191 (cp->timer.expires-jiffies)/HZ);
1192 else
1193#endif
1194 seq_printf(seq,
1195 "%-3s %08X %04X %08X %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001196 "%s %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001197 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001198 ntohl(cp->caddr.ip), ntohs(cp->cport),
1199 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001200 dbuf, ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001201 ip_vs_state_name(cp->protocol, cp->state),
1202 ip_vs_origin_name(cp->flags),
1203 (cp->timer.expires-jiffies)/HZ);
1204 }
1205 return 0;
1206}
1207
1208static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1209 .start = ip_vs_conn_seq_start,
1210 .next = ip_vs_conn_seq_next,
1211 .stop = ip_vs_conn_seq_stop,
1212 .show = ip_vs_conn_sync_seq_show,
1213};
1214
1215static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1216{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001217 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1218 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001219}
1220
1221static const struct file_operations ip_vs_conn_sync_fops = {
1222 .owner = THIS_MODULE,
1223 .open = ip_vs_conn_sync_open,
1224 .read = seq_read,
1225 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001226 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001227};
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229#endif
1230
1231
1232/*
1233 * Randomly drop connection entries before running out of memory
1234 */
1235static inline int todrop_entry(struct ip_vs_conn *cp)
1236{
1237 /*
1238 * The drop rate array needs tuning for real environments.
1239 * Called from timer bh only => no locking
1240 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001241 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 static char todrop_counter[9] = {0};
1243 int i;
1244
1245 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1246 This will leave enough time for normal connection to get
1247 through. */
1248 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1249 return 0;
1250
1251 /* Don't drop the entry if its number of incoming packets is not
1252 located in [0, 8] */
1253 i = atomic_read(&cp->in_pkts);
1254 if (i > 8 || i < 0) return 0;
1255
1256 if (!todrop_rate[i]) return 0;
1257 if (--todrop_counter[i] > 0) return 0;
1258
1259 todrop_counter[i] = todrop_rate[i];
1260 return 1;
1261}
1262
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001263/* Called from keventd and must protect itself from softirqs */
Eric W. Biederman423b5592015-09-21 13:02:24 -05001264void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001267 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Simon Hormana38e5e22013-05-22 14:50:32 +09001269 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Randomly scan 1/32 of the whole table every second
1272 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001273 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001274 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Julian Anastasov088339a2013-03-21 11:58:10 +02001276 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001277 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* connection template */
1279 continue;
Eric W. Biederman423b5592015-09-21 13:02:24 -05001280 if (cp->ipvs != ipvs)
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001281 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (cp->protocol == IPPROTO_TCP) {
1283 switch(cp->state) {
1284 case IP_VS_TCP_S_SYN_RECV:
1285 case IP_VS_TCP_S_SYNACK:
1286 break;
1287
1288 case IP_VS_TCP_S_ESTABLISHED:
1289 if (todrop_entry(cp))
1290 break;
1291 continue;
1292
1293 default:
1294 continue;
1295 }
Julian Anastasovacaac5d2013-06-18 10:08:08 +03001296 } else if (cp->protocol == IPPROTO_SCTP) {
1297 switch (cp->state) {
1298 case IP_VS_SCTP_S_INIT1:
1299 case IP_VS_SCTP_S_INIT:
1300 break;
1301 case IP_VS_SCTP_S_ESTABLISHED:
1302 if (todrop_entry(cp))
1303 break;
1304 continue;
1305 default:
1306 continue;
1307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 } else {
1309 if (!todrop_entry(cp))
1310 continue;
1311 }
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 IP_VS_DBG(4, "del connection\n");
1314 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001315 cp_c = cp->control;
1316 /* cp->control is valid only with reference to cp */
1317 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001319 ip_vs_conn_expire_now(cp_c);
1320 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001323 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001325 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328
1329/*
1330 * Flush all the connection entries in the ip_vs_conn_tab
1331 */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001332static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001335 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Hans Schillstroma0840e22011-01-03 14:44:58 +01001337flush_again:
Simon Hormana38e5e22013-05-22 14:50:32 +09001338 rcu_read_lock();
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001339 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Julian Anastasov088339a2013-03-21 11:58:10 +02001341 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001342 if (cp->ipvs != ipvs)
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001343 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 IP_VS_DBG(4, "del connection\n");
1345 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001346 cp_c = cp->control;
1347 /* cp->control is valid only with reference to cp */
1348 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001350 ip_vs_conn_expire_now(cp_c);
1351 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001354 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001356 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /* the counter may be not NULL, because maybe some conn entries
1359 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001360 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 schedule();
1362 goto flush_again;
1363 }
1364}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001365/*
1366 * per netns init and exit
1367 */
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05001368int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001369{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001370 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Simon Horman92240e82015-10-06 17:09:52 +09001372 proc_create("ip_vs_conn", 0, ipvs->net->proc_net, &ip_vs_conn_fops);
1373 proc_create("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1374 &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001375 return 0;
1376}
1377
Eric W. Biederman2f3edc6a2015-09-21 13:02:42 -05001378void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001379{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001380 /* flush all the connection entries first */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001381 ip_vs_conn_flush(ipvs);
Simon Horman92240e82015-10-06 17:09:52 +09001382 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1383 remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Sven Wegener048cf482008-08-10 18:24:35 +00001386int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int idx;
1389
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001390 /* Compute size and mask */
1391 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1392 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /*
1395 * Allocate the connection hash table and initialize its list heads
1396 */
Changli Gao731109e2011-02-19 18:05:08 +08001397 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!ip_vs_conn_tab)
1399 return -ENOMEM;
1400
1401 /* Allocate ip_vs_conn slab cache */
1402 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1403 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001404 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (!ip_vs_conn_cachep) {
1406 vfree(ip_vs_conn_tab);
1407 return -ENOMEM;
1408 }
1409
Hannes Eder1e3e2382009-08-02 11:05:41 +00001410 pr_info("Connection hash table configured "
1411 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001412 ip_vs_conn_tab_size,
1413 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1415 sizeof(struct ip_vs_conn));
1416
Changli Gao731109e2011-02-19 18:05:08 +08001417 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1418 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001421 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* calculate the random value for connection hash */
1425 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1426
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430void ip_vs_conn_cleanup(void)
1431{
Julian Anastasov088339a2013-03-21 11:58:10 +02001432 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1433 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* Release the empty cache */
1435 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 vfree(ip_vs_conn_tab);
1437}