blob: 06809db5eef61987273c6aa314a0b30e29537b09 [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
107
108/*
109 * Returns hash value for IPVS connection entry
110 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000111static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned int proto,
Julius Volz28364a52008-09-02 15:55:43 +0200112 const union nf_inet_addr *addr,
113 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Julius Volz28364a52008-09-02 15:55:43 +0200115#ifdef CONFIG_IP_VS_IPV6
116 if (af == AF_INET6)
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100117 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
118 (__force u32)port, proto, ip_vs_conn_rnd) ^
119 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
Julius Volz28364a52008-09-02 15:55:43 +0200120#endif
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100121 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
122 ip_vs_conn_rnd) ^
123 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Simon Horman85999282010-08-22 21:37:53 +0900126static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
127 bool inverse)
128{
129 const union nf_inet_addr *addr;
130 __be16 port;
131
Simon Hormanf71499a2010-08-22 21:37:54 +0900132 if (p->pe_data && p->pe->hashkey_raw)
Simon Horman85999282010-08-22 21:37:53 +0900133 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
134 ip_vs_conn_tab_mask;
135
136 if (likely(!inverse)) {
137 addr = p->caddr;
138 port = p->cport;
139 } else {
140 addr = p->vaddr;
141 port = p->vport;
142 }
143
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500144 return ip_vs_conn_hashkey(p->ipvs->net, p->af, p->protocol, addr, port);
Simon Horman85999282010-08-22 21:37:53 +0900145}
146
147static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
148{
149 struct ip_vs_conn_param p;
150
Eric W. Biederman19913de2015-09-21 13:01:43 -0500151 ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100152 &cp->caddr, cp->cport, NULL, 0, &p);
Simon Horman85999282010-08-22 21:37:53 +0900153
Simon Hormane9e5eee2010-11-08 20:05:57 +0900154 if (cp->pe) {
155 p.pe = cp->pe;
Simon Horman85999282010-08-22 21:37:53 +0900156 p.pe_data = cp->pe_data;
157 p.pe_data_len = cp->pe_data_len;
158 }
159
160 return ip_vs_conn_hashkey_param(&p, false);
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/*
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100164 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * returns bool success.
166 */
167static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
168{
Eric Dumazet95c96172012-04-15 05:58:06 +0000169 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int ret;
171
Nick Chalk26ec0372010-06-22 08:07:01 +0200172 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
173 return 0;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Hash by protocol, client address and port */
Simon Horman85999282010-08-22 21:37:53 +0900176 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Julian Anastasovac692692013-03-22 11:46:54 +0200178 ct_write_lock_bh(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200179 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 cp->flags |= IP_VS_CONN_F_HASHED;
183 atomic_inc(&cp->refcnt);
Julian Anastasov088339a2013-03-21 11:58:10 +0200184 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ret = 1;
186 } else {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000187 pr_err("%s(): request for already hashed, called from %pF\n",
188 __func__, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ret = 0;
190 }
191
Sven Wegeneraea9d712010-06-09 16:10:57 +0200192 spin_unlock(&cp->lock);
Julian Anastasovac692692013-03-22 11:46:54 +0200193 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 return ret;
196}
197
198
199/*
200 * UNhashes ip_vs_conn from ip_vs_conn_tab.
Julian Anastasov088339a2013-03-21 11:58:10 +0200201 * returns bool success. Caller should hold conn reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
203static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
204{
Eric Dumazet95c96172012-04-15 05:58:06 +0000205 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int ret;
207
208 /* unhash it and decrease its reference counter */
Simon Horman85999282010-08-22 21:37:53 +0900209 hash = ip_vs_conn_hashkey_conn(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Julian Anastasovac692692013-03-22 11:46:54 +0200211 ct_write_lock_bh(hash);
Sven Wegeneraea9d712010-06-09 16:10:57 +0200212 spin_lock(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (cp->flags & IP_VS_CONN_F_HASHED) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200215 hlist_del_rcu(&cp->c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 cp->flags &= ~IP_VS_CONN_F_HASHED;
217 atomic_dec(&cp->refcnt);
218 ret = 1;
219 } else
220 ret = 0;
221
Sven Wegeneraea9d712010-06-09 16:10:57 +0200222 spin_unlock(&cp->lock);
Julian Anastasovac692692013-03-22 11:46:54 +0200223 ct_write_unlock_bh(hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 return ret;
226}
227
Julian Anastasov088339a2013-03-21 11:58:10 +0200228/* Try to unlink ip_vs_conn from ip_vs_conn_tab.
229 * returns bool success.
230 */
231static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
232{
233 unsigned int hash;
234 bool ret;
235
236 hash = ip_vs_conn_hashkey_conn(cp);
237
Julian Anastasovac692692013-03-22 11:46:54 +0200238 ct_write_lock_bh(hash);
Julian Anastasov088339a2013-03-21 11:58:10 +0200239 spin_lock(&cp->lock);
240
241 if (cp->flags & IP_VS_CONN_F_HASHED) {
242 ret = false;
243 /* Decrease refcnt and unlink conn only if we are last user */
244 if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) {
245 hlist_del_rcu(&cp->c_list);
246 cp->flags &= ~IP_VS_CONN_F_HASHED;
247 ret = true;
248 }
249 } else
250 ret = atomic_read(&cp->refcnt) ? false : true;
251
252 spin_unlock(&cp->lock);
Julian Anastasovac692692013-03-22 11:46:54 +0200253 ct_write_unlock_bh(hash);
Julian Anastasov088339a2013-03-21 11:58:10 +0200254
255 return ret;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/*
260 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
261 * Called for pkts coming from OUTside-to-INside.
Simon Hormanf11017e2010-08-22 21:37:52 +0900262 * p->caddr, p->cport: pkt source address (foreign host)
263 * p->vaddr, p->vport: pkt dest address (load balancer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900265static inline struct ip_vs_conn *
266__ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Eric Dumazet95c96172012-04-15 05:58:06 +0000268 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct ip_vs_conn *cp;
270
Simon Horman85999282010-08-22 21:37:53 +0900271 hash = ip_vs_conn_hashkey_param(p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Julian Anastasov088339a2013-03-21 11:58:10 +0200273 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Julian Anastasov088339a2013-03-21 11:58:10 +0200275 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200276 if (p->cport == cp->cport && p->vport == cp->vport &&
277 cp->af == p->af &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900278 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
279 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900280 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100281 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500282 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200283 if (!__ip_vs_conn_get(cp))
284 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* HIT */
Julian Anastasov088339a2013-03-21 11:58:10 +0200286 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return cp;
288 }
289 }
290
Julian Anastasov088339a2013-03-21 11:58:10 +0200291 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return NULL;
294}
295
Simon Hormanf11017e2010-08-22 21:37:52 +0900296struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct ip_vs_conn *cp;
299
Simon Hormanf11017e2010-08-22 21:37:52 +0900300 cp = __ip_vs_conn_in_get(p);
301 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
302 struct ip_vs_conn_param cport_zero_p = *p;
303 cport_zero_p.cport = 0;
304 cp = __ip_vs_conn_in_get(&cport_zero_p);
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Julius Volz28364a52008-09-02 15:55:43 +0200307 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900308 ip_vs_proto_name(p->protocol),
309 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
310 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200311 cp ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return cp;
314}
315
Simon Hormanf11017e2010-08-22 21:37:52 +0900316static int
317ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
318 const struct ip_vs_iphdr *iph,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700319 struct ip_vs_conn_param *p)
Simon Hormanf11017e2010-08-22 21:37:52 +0900320{
321 __be16 _ports[2], *pptr;
Eric W. Biederman19913de2015-09-21 13:01:43 -0500322 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
Simon Hormanf11017e2010-08-22 21:37:52 +0900323
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200324 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Simon Hormanf11017e2010-08-22 21:37:52 +0900325 if (pptr == NULL)
326 return 1;
327
Alex Gartrell802c41a2015-08-26 09:40:32 -0700328 if (likely(!ip_vs_iph_inverse(iph)))
Eric W. Biederman19913de2015-09-21 13:01:43 -0500329 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100330 pptr[0], &iph->daddr, pptr[1], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900331 else
Eric W. Biederman19913de2015-09-21 13:01:43 -0500332 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100333 pptr[1], &iph->saddr, pptr[0], p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900334 return 0;
335}
336
Simon Horman5c0d2372010-08-02 17:12:44 +0200337struct ip_vs_conn *
338ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700339 const struct ip_vs_iphdr *iph)
Simon Horman5c0d2372010-08-02 17:12:44 +0200340{
Simon Hormanf11017e2010-08-22 21:37:52 +0900341 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200342
Alex Gartrell802c41a2015-08-26 09:40:32 -0700343 if (ip_vs_conn_fill_param_proto(af, skb, iph, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200344 return NULL;
345
Simon Hormanf11017e2010-08-22 21:37:52 +0900346 return ip_vs_conn_in_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200347}
348EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
349
Julian Anastasov87375ab2005-09-14 21:08:51 -0700350/* Get reference to connection template */
Simon Hormanf11017e2010-08-22 21:37:52 +0900351struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700352{
Eric Dumazet95c96172012-04-15 05:58:06 +0000353 unsigned int hash;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700354 struct ip_vs_conn *cp;
355
Simon Horman85999282010-08-22 21:37:53 +0900356 hash = ip_vs_conn_hashkey_param(p, false);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700357
Julian Anastasov088339a2013-03-21 11:58:10 +0200358 rcu_read_lock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700359
Julian Anastasov088339a2013-03-21 11:58:10 +0200360 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200361 if (unlikely(p->pe_data && p->pe->ct_match)) {
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500362 if (cp->ipvs != p->ipvs)
Julian Anastasov1845ed02013-03-21 11:58:11 +0200363 continue;
Julian Anastasov088339a2013-03-21 11:58:10 +0200364 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
365 if (__ip_vs_conn_get(cp))
366 goto out;
367 }
Simon Horman85999282010-08-22 21:37:53 +0900368 continue;
369 }
370
Simon Hormanf11017e2010-08-22 21:37:52 +0900371 if (cp->af == p->af &&
372 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000373 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900374 * p->vaddr is a fwmark */
375 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
376 p->af, p->vaddr, &cp->vaddr) &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200377 p->vport == cp->vport && p->cport == cp->cport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700378 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200379 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500380 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200381 if (__ip_vs_conn_get(cp))
382 goto out;
383 }
Julian Anastasov87375ab2005-09-14 21:08:51 -0700384 }
385 cp = NULL;
386
387 out:
Julian Anastasov088339a2013-03-21 11:58:10 +0200388 rcu_read_unlock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700389
Julius Volz28364a52008-09-02 15:55:43 +0200390 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900391 ip_vs_proto_name(p->protocol),
392 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
393 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200394 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700395
396 return cp;
397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Simon Hormanf11017e2010-08-22 21:37:52 +0900399/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
400 * Called for pkts coming from inside-to-OUTside.
401 * p->caddr, p->cport: pkt source address (inside host)
402 * p->vaddr, p->vport: pkt dest address (foreign host) */
403struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Eric Dumazet95c96172012-04-15 05:58:06 +0000405 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct ip_vs_conn *cp, *ret=NULL;
407
408 /*
409 * Check for "full" addressed entries
410 */
Simon Horman85999282010-08-22 21:37:53 +0900411 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Julian Anastasov088339a2013-03-21 11:58:10 +0200413 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Julian Anastasov088339a2013-03-21 11:58:10 +0200415 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200416 if (p->vport == cp->cport && p->cport == cp->dport &&
417 cp->af == p->af &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900418 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
419 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100420 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500421 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200422 if (!__ip_vs_conn_get(cp))
423 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ret = cp;
426 break;
427 }
428 }
429
Julian Anastasov088339a2013-03-21 11:58:10 +0200430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Julius Volz28364a52008-09-02 15:55:43 +0200432 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900433 ip_vs_proto_name(p->protocol),
434 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
435 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200436 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 return ret;
439}
440
Simon Horman5c0d2372010-08-02 17:12:44 +0200441struct ip_vs_conn *
442ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700443 const struct ip_vs_iphdr *iph)
Simon Horman5c0d2372010-08-02 17:12:44 +0200444{
Simon Hormanf11017e2010-08-22 21:37:52 +0900445 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200446
Alex Gartrell802c41a2015-08-26 09:40:32 -0700447 if (ip_vs_conn_fill_param_proto(af, skb, iph, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200448 return NULL;
449
Simon Hormanf11017e2010-08-22 21:37:52 +0900450 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200451}
452EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/*
455 * Put back the conn and restart its timer with its timeout
456 */
457void ip_vs_conn_put(struct ip_vs_conn *cp)
458{
Nick Chalk26ec0372010-06-22 08:07:01 +0200459 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
460 0 : cp->timeout;
461 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 __ip_vs_conn_put(cp);
464}
465
466
467/*
468 * Fill a no_client_port connection with a client port number
469 */
Al Viro014d7302006-09-28 14:29:52 -0700470void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 if (ip_vs_conn_unhash(cp)) {
Julian Anastasovac692692013-03-22 11:46:54 +0200473 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
475 atomic_dec(&ip_vs_conn_no_cport_cnt);
476 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
477 cp->cport = cport;
478 }
Julian Anastasovac692692013-03-22 11:46:54 +0200479 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* hash on new dport */
482 ip_vs_conn_hash(cp);
483 }
484}
485
486
487/*
488 * Bind a connection entry with the corresponding packet_xmit.
489 * Called by ip_vs_conn_new.
490 */
491static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
492{
493 switch (IP_VS_FWD_METHOD(cp)) {
494 case IP_VS_CONN_F_MASQ:
495 cp->packet_xmit = ip_vs_nat_xmit;
496 break;
497
498 case IP_VS_CONN_F_TUNNEL:
Alex Gartrell8052ba22014-09-09 16:40:28 -0700499#ifdef CONFIG_IP_VS_IPV6
500 if (cp->daf == AF_INET6)
501 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
502 else
503#endif
504 cp->packet_xmit = ip_vs_tunnel_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506
507 case IP_VS_CONN_F_DROUTE:
508 cp->packet_xmit = ip_vs_dr_xmit;
509 break;
510
511 case IP_VS_CONN_F_LOCALNODE:
512 cp->packet_xmit = ip_vs_null_xmit;
513 break;
514
515 case IP_VS_CONN_F_BYPASS:
516 cp->packet_xmit = ip_vs_bypass_xmit;
517 break;
518 }
519}
520
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200521#ifdef CONFIG_IP_VS_IPV6
522static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
523{
524 switch (IP_VS_FWD_METHOD(cp)) {
525 case IP_VS_CONN_F_MASQ:
526 cp->packet_xmit = ip_vs_nat_xmit_v6;
527 break;
528
529 case IP_VS_CONN_F_TUNNEL:
Alex Gartrell8052ba22014-09-09 16:40:28 -0700530 if (cp->daf == AF_INET6)
531 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
532 else
533 cp->packet_xmit = ip_vs_tunnel_xmit;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200534 break;
535
536 case IP_VS_CONN_F_DROUTE:
537 cp->packet_xmit = ip_vs_dr_xmit_v6;
538 break;
539
540 case IP_VS_CONN_F_LOCALNODE:
541 cp->packet_xmit = ip_vs_null_xmit;
542 break;
543
544 case IP_VS_CONN_F_BYPASS:
545 cp->packet_xmit = ip_vs_bypass_xmit_v6;
546 break;
547 }
548}
549#endif
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
553{
554 return atomic_read(&dest->activeconns)
555 + atomic_read(&dest->inactconns);
556}
557
558/*
559 * Bind a connection entry with a virtual service destination
560 * Called just after a new connection entry is created.
561 */
562static inline void
563ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
564{
Julian Anastasov35757922010-09-17 14:18:16 +0200565 unsigned int conn_flags;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200566 __u32 flags;
Julian Anastasov35757922010-09-17 14:18:16 +0200567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* if dest is NULL, then return directly */
569 if (!dest)
570 return;
571
572 /* Increase the refcnt counter of the dest */
Julian Anastasovfca9c202013-03-22 11:46:38 +0200573 ip_vs_dest_hold(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Julian Anastasov35757922010-09-17 14:18:16 +0200575 conn_flags = atomic_read(&dest->conn_flags);
576 if (cp->protocol != IPPROTO_UDP)
577 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200578 flags = cp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Bind with the destination and its corresponding transmitter */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200580 if (flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800581 /* if the connection is not template and is created
582 * by sync, preserve the activity flag.
583 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200584 if (!(flags & IP_VS_CONN_F_TEMPLATE))
Julian Anastasov35757922010-09-17 14:18:16 +0200585 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300586 /* connections inherit forwarding method from dest */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200587 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
Julian Anastasov35757922010-09-17 14:18:16 +0200588 }
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200589 flags |= conn_flags;
590 cp->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 cp->dest = dest;
592
Julius Volzcfc78c52008-09-02 15:55:53 +0200593 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
594 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
595 "dest->refcnt:%d\n",
596 ip_vs_proto_name(cp->protocol),
597 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
598 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700599 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcfc78c52008-09-02 15:55:53 +0200600 ip_vs_fwd_tag(cp), cp->state,
601 cp->flags, atomic_read(&cp->refcnt),
602 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Update the connection counters */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200605 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
Julian Anastasov06611f82012-04-24 23:46:36 +0300606 /* It is a normal connection, so modify the counters
607 * according to the flags, later the protocol can
608 * update them on state change
609 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200610 if (!(flags & IP_VS_CONN_F_INACTIVE))
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800611 atomic_inc(&dest->activeconns);
612 else
613 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } else {
615 /* It is a persistent connection/template, so increase
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300616 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 atomic_inc(&dest->persistconns);
618 }
619
620 if (dest->u_threshold != 0 &&
621 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
622 dest->flags |= IP_VS_DEST_F_OVERLOAD;
623}
624
625
626/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800627 * Check if there is a destination for the connection, if so
628 * bind the connection to the destination.
629 */
Julian Anastasov413c2d042013-03-22 11:46:52 +0200630void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800631{
632 struct ip_vs_dest *dest;
633
Julian Anastasov413c2d042013-03-22 11:46:52 +0200634 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -0700635
636 /* This function is only invoked by the synchronization code. We do
637 * not currently support heterogeneous pools with synchronization,
638 * so we can make the assumption that the svc_af is the same as the
639 * dest_af
640 */
Eric W. Biedermandc2add62015-09-21 13:01:51 -0500641 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
Julian Anastasov882a8442012-04-24 23:46:37 +0300642 cp->dport, &cp->vaddr, cp->vport,
643 cp->protocol, cp->fwmark, cp->flags);
644 if (dest) {
645 struct ip_vs_proto_data *pd;
646
Julian Anastasovac692692013-03-22 11:46:54 +0200647 spin_lock_bh(&cp->lock);
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200648 if (cp->dest) {
Julian Anastasovac692692013-03-22 11:46:54 +0200649 spin_unlock_bh(&cp->lock);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200650 rcu_read_unlock();
651 return;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200652 }
653
Julian Anastasov882a8442012-04-24 23:46:37 +0300654 /* Applications work depending on the forwarding method
655 * but better to reassign them always when binding dest */
656 if (cp->app)
657 ip_vs_unbind_app(cp);
658
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800659 ip_vs_bind_dest(cp, dest);
Julian Anastasovac692692013-03-22 11:46:54 +0200660 spin_unlock_bh(&cp->lock);
Julian Anastasov882a8442012-04-24 23:46:37 +0300661
662 /* Update its packet transmitter */
663 cp->packet_xmit = NULL;
664#ifdef CONFIG_IP_VS_IPV6
665 if (cp->af == AF_INET6)
666 ip_vs_bind_xmit_v6(cp);
667 else
668#endif
669 ip_vs_bind_xmit(cp);
670
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500671 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
Julian Anastasov882a8442012-04-24 23:46:37 +0300672 if (pd && atomic_read(&pd->appcnt))
673 ip_vs_bind_app(cp, pd->pp);
674 }
Julian Anastasov413c2d042013-03-22 11:46:52 +0200675 rcu_read_unlock();
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800676}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800677
678
679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * Unbind a connection entry with its VS destination
681 * Called by the ip_vs_conn_expire function.
682 */
683static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
684{
685 struct ip_vs_dest *dest = cp->dest;
686
687 if (!dest)
688 return;
689
Julius Volzcfc78c52008-09-02 15:55:53 +0200690 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
691 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
692 "dest->refcnt:%d\n",
693 ip_vs_proto_name(cp->protocol),
694 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
695 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700696 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcfc78c52008-09-02 15:55:53 +0200697 ip_vs_fwd_tag(cp), cp->state,
698 cp->flags, atomic_read(&cp->refcnt),
699 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700702 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* It is a normal connection, so decrease the inactconns
704 or activeconns counter */
705 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
706 atomic_dec(&dest->inactconns);
707 } else {
708 atomic_dec(&dest->activeconns);
709 }
710 } else {
711 /* It is a persistent connection/template, so decrease
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300712 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 atomic_dec(&dest->persistconns);
714 }
715
716 if (dest->l_threshold != 0) {
717 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
718 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
719 } else if (dest->u_threshold != 0) {
720 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
721 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
722 } else {
723 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
724 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
725 }
726
Julian Anastasovfca9c202013-03-22 11:46:38 +0200727 ip_vs_dest_put(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Simon Horman8e1b0b12011-02-04 18:33:01 +0900730static int expire_quiescent_template(struct netns_ipvs *ipvs,
731 struct ip_vs_dest *dest)
732{
733#ifdef CONFIG_SYSCTL
734 return ipvs->sysctl_expire_quiescent_template &&
735 (atomic_read(&dest->weight) == 0);
736#else
737 return 0;
738#endif
739}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741/*
742 * Checking if the destination of a connection template is available.
743 * If available, return 1, otherwise invalidate this connection
744 * template and return 0.
745 */
746int ip_vs_check_template(struct ip_vs_conn *ct)
747{
748 struct ip_vs_dest *dest = ct->dest;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500749 struct netns_ipvs *ipvs = ct->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * Checking the dest server status.
753 */
754 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900755 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
Simon Horman8e1b0b12011-02-04 18:33:01 +0900756 expire_quiescent_template(ipvs, dest)) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200757 IP_VS_DBG_BUF(9, "check_template: dest not available for "
758 "protocol %s s:%s:%d v:%s:%d "
759 "-> d:%s:%d\n",
760 ip_vs_proto_name(ct->protocol),
761 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
762 ntohs(ct->cport),
763 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
764 ntohs(ct->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700765 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
Julius Volzcfc78c52008-09-02 15:55:53 +0200766 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * Invalidate the connection template
770 */
Al Viro014d7302006-09-28 14:29:52 -0700771 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700773 ct->dport = htons(0xffff);
774 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ct->cport = 0;
776 ip_vs_conn_hash(ct);
777 }
778 }
779
780 /*
781 * Simply decrease the refcnt of the template,
782 * don't restart its timer.
783 */
Julian Anastasov088339a2013-03-21 11:58:10 +0200784 __ip_vs_conn_put(ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786 }
787 return 1;
788}
789
Julian Anastasov088339a2013-03-21 11:58:10 +0200790static void ip_vs_conn_rcu_free(struct rcu_head *head)
791{
792 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
793 rcu_head);
794
795 ip_vs_pe_put(cp->pe);
796 kfree(cp->pe_data);
797 kmem_cache_free(ip_vs_conn_cachep, cp);
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static void ip_vs_conn_expire(unsigned long data)
801{
802 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500803 struct netns_ipvs *ipvs = cp->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * do I control anybody?
807 */
808 if (atomic_read(&cp->n_control))
809 goto expire_later;
810
Julian Anastasov088339a2013-03-21 11:58:10 +0200811 /* Unlink conn if not referenced anymore */
812 if (likely(ip_vs_conn_unlink(cp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* delete the timer if it is activated by other users */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000814 del_timer(&cp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* does anybody control me? */
817 if (cp->control)
818 ip_vs_control_del(cp);
819
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200820 if (cp->flags & IP_VS_CONN_F_NFCT) {
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200821 /* Do not access conntracks during subsys cleanup
822 * because nf_conntrack_find_get can not be used after
823 * conntrack cleanup for the net.
824 */
825 smp_rmb();
826 if (ipvs->enable)
827 ip_vs_conn_drop_conntrack(cp);
828 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (unlikely(cp->app != NULL))
831 ip_vs_unbind_app(cp);
832 ip_vs_unbind_dest(cp);
833 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
834 atomic_dec(&ip_vs_conn_no_cport_cnt);
Julian Anastasov088339a2013-03-21 11:58:10 +0200835 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100836 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838 }
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200841 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
842 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 atomic_read(&cp->n_control));
844
Julian Anastasov088339a2013-03-21 11:58:10 +0200845 atomic_inc(&cp->refcnt);
846 cp->timeout = 60*HZ;
847
Julian Anastasov749c42b2012-04-24 23:46:40 +0300848 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -0500849 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
Julian Anastasov749c42b2012-04-24 23:46:40 +0300850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ip_vs_conn_put(cp);
852}
853
Julian Anastasov088339a2013-03-21 11:58:10 +0200854/* Modify timer, so that it expires as soon as possible.
855 * Can be called without reference only if under RCU lock.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
858{
Julian Anastasov088339a2013-03-21 11:58:10 +0200859 /* Using mod_timer_pending will ensure the timer is not
860 * modified after the final del_timer in ip_vs_conn_expire.
861 */
862 if (timer_pending(&cp->timer) &&
863 time_after(cp->timer.expires, jiffies))
864 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867
868/*
869 * Create a new connection entry and hash it into the ip_vs_conn_tab
870 */
871struct ip_vs_conn *
Alex Gartrellba385282014-09-09 16:40:23 -0700872ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
Eric Dumazet95c96172012-04-15 05:58:06 +0000873 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100874 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct ip_vs_conn *cp;
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500877 struct netns_ipvs *ipvs = p->ipvs;
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500878 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100879 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200881 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000883 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return NULL;
885 }
886
Changli Gao731109e2011-02-19 18:05:08 +0800887 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800888 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500889 cp->ipvs = ipvs;
Simon Hormanf11017e2010-08-22 21:37:52 +0900890 cp->af = p->af;
Alex Gartrellba385282014-09-09 16:40:23 -0700891 cp->daf = dest_af;
Simon Hormanf11017e2010-08-22 21:37:52 +0900892 cp->protocol = p->protocol;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200893 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
Simon Hormanf11017e2010-08-22 21:37:52 +0900894 cp->cport = p->cport;
Michal Kubecek2a971352014-01-30 08:50:20 +0100895 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200896 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Michal Kubecek2a971352014-01-30 08:50:20 +0100897 &cp->vaddr, p->vaddr);
898 cp->vport = p->vport;
Alex Gartrellba385282014-09-09 16:40:23 -0700899 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 cp->dport = dport;
901 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100902 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900903 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
904 ip_vs_pe_get(p->pe);
905 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900906 cp->pe_data = p->pe_data;
907 cp->pe_data_len = p->pe_data_len;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200908 } else {
909 cp->pe = NULL;
910 cp->pe_data = NULL;
911 cp->pe_data_len = 0;
Simon Horman85999282010-08-22 21:37:53 +0900912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 spin_lock_init(&cp->lock);
914
915 /*
916 * Set the entry is referenced by the current thread before hashing
917 * it in the table, so that other thread run ip_vs_random_dropentry
918 * but cannot drop this entry.
919 */
920 atomic_set(&cp->refcnt, 1);
921
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200922 cp->control = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 atomic_set(&cp->n_control, 0);
924 atomic_set(&cp->in_pkts, 0);
925
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200926 cp->packet_xmit = NULL;
927 cp->app = NULL;
928 cp->app_data = NULL;
929 /* reset struct ip_vs_seq */
930 cp->in_seq.delta = 0;
931 cp->out_seq.delta = 0;
932
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100933 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (flags & IP_VS_CONN_F_NO_CPORT)
935 atomic_inc(&ip_vs_conn_no_cport_cnt);
936
937 /* Bind the connection with a destination server */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200938 cp->dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ip_vs_bind_dest(cp, dest);
940
941 /* Set its state and timeout */
942 cp->state = 0;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200943 cp->old_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300945 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200948#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900949 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200950 ip_vs_bind_xmit_v6(cp);
951 else
952#endif
953 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100955 if (unlikely(pd && atomic_read(&pd->appcnt)))
956 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200958 /*
959 * Allow conntrack to be preserved. By default, conntrack
960 * is created and destroyed for every packet.
961 * Sometimes keeping conntrack can be useful for
962 * IP_VS_CONN_F_ONE_PACKET too.
963 */
964
Hans Schillstroma0840e22011-01-03 14:44:58 +0100965 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200966 cp->flags |= IP_VS_CONN_F_NFCT;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* Hash it in the ip_vs_conn_tab finally */
969 ip_vs_conn_hash(cp);
970
971 return cp;
972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/*
975 * /proc/net/ip_vs_conn entries
976 */
977#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100978struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +0800979 struct seq_net_private p;
980 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100981};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
984{
985 int idx;
986 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100987 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900988
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100989 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200990 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
991 /* __ip_vs_conn_get() is not needed by
992 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100995 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +0800996 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 }
Simon Hormana38e5e22013-05-22 14:50:32 +0900999 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 return NULL;
1003}
1004
1005static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001006 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001008 struct ip_vs_iter_state *iter = seq->private;
1009
1010 iter->l = NULL;
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001011 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1013}
1014
1015static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1016{
1017 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001018 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +02001019 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +08001020 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int idx;
1022
1023 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001024 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return ip_vs_conn_array(seq, 0);
1026
1027 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +02001028 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1029 if (e)
1030 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001033 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001034 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001035 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001037 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001038 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001040 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return NULL;
1042}
1043
1044static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001045 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001047 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1051{
1052
1053 if (v == SEQ_START_TOKEN)
1054 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001055 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 else {
1057 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001058 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001059 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1060 size_t len = 0;
Julian Anastasovf18ae722014-09-09 16:40:38 -07001061 char dbuf[IP_VS_ADDRSTRLEN];
Simon Hormana3c918a2010-08-22 21:37:53 +09001062
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001063 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001064 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001065 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001066 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001067 len = strlen(cp->pe->name);
1068 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001069 pe_data[len + 1] = ' ';
1070 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001071 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001072 }
1073 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Vince Busam667a5f12008-09-02 15:55:49 +02001075#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001076 if (cp->daf == AF_INET6)
1077 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1078 else
1079#endif
1080 snprintf(dbuf, sizeof(dbuf), "%08X",
1081 ntohl(cp->daddr.ip));
1082
1083#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001084 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001085 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001086 "%s %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001087 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001088 &cp->caddr.in6, ntohs(cp->cport),
1089 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001090 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001091 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001092 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001093 else
1094#endif
1095 seq_printf(seq,
1096 "%-3s %08X %04X %08X %04X"
Julian Anastasovf18ae722014-09-09 16:40:38 -07001097 " %s %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001099 ntohl(cp->caddr.ip), ntohs(cp->cport),
1100 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001101 dbuf, ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001103 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 return 0;
1106}
1107
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001108static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 .start = ip_vs_conn_seq_start,
1110 .next = ip_vs_conn_seq_next,
1111 .stop = ip_vs_conn_seq_stop,
1112 .show = ip_vs_conn_seq_show,
1113};
1114
1115static int ip_vs_conn_open(struct inode *inode, struct file *file)
1116{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001117 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1118 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Arjan van de Ven9a321442007-02-12 00:55:35 -08001121static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 .owner = THIS_MODULE,
1123 .open = ip_vs_conn_open,
1124 .read = seq_read,
1125 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001126 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001128
Eric Dumazet95c96172012-04-15 05:58:06 +00001129static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001130{
1131 if (flags & IP_VS_CONN_F_SYNC)
1132 return "SYNC";
1133 else
1134 return "LOCAL";
1135}
1136
1137static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1138{
Julian Anastasovf18ae722014-09-09 16:40:38 -07001139 char dbuf[IP_VS_ADDRSTRLEN];
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001140
1141 if (v == SEQ_START_TOKEN)
1142 seq_puts(seq,
1143 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1144 else {
1145 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001146 struct net *net = seq_file_net(seq);
1147
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001148 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001149 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001150
Vince Busam667a5f12008-09-02 15:55:49 +02001151#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001152 if (cp->daf == AF_INET6)
1153 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1154 else
1155#endif
1156 snprintf(dbuf, sizeof(dbuf), "%08X",
1157 ntohl(cp->daddr.ip));
1158
1159#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001160 if (cp->af == AF_INET6)
Julian Anastasovf18ae722014-09-09 16:40:38 -07001161 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1162 "%s %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001163 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001164 &cp->caddr.in6, ntohs(cp->cport),
1165 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001166 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001167 ip_vs_state_name(cp->protocol, cp->state),
1168 ip_vs_origin_name(cp->flags),
1169 (cp->timer.expires-jiffies)/HZ);
1170 else
1171#endif
1172 seq_printf(seq,
1173 "%-3s %08X %04X %08X %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001174 "%s %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001175 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001176 ntohl(cp->caddr.ip), ntohs(cp->cport),
1177 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001178 dbuf, ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001179 ip_vs_state_name(cp->protocol, cp->state),
1180 ip_vs_origin_name(cp->flags),
1181 (cp->timer.expires-jiffies)/HZ);
1182 }
1183 return 0;
1184}
1185
1186static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1187 .start = ip_vs_conn_seq_start,
1188 .next = ip_vs_conn_seq_next,
1189 .stop = ip_vs_conn_seq_stop,
1190 .show = ip_vs_conn_sync_seq_show,
1191};
1192
1193static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1194{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001195 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1196 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001197}
1198
1199static const struct file_operations ip_vs_conn_sync_fops = {
1200 .owner = THIS_MODULE,
1201 .open = ip_vs_conn_sync_open,
1202 .read = seq_read,
1203 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001204 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001205};
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207#endif
1208
1209
1210/*
1211 * Randomly drop connection entries before running out of memory
1212 */
1213static inline int todrop_entry(struct ip_vs_conn *cp)
1214{
1215 /*
1216 * The drop rate array needs tuning for real environments.
1217 * Called from timer bh only => no locking
1218 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001219 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 static char todrop_counter[9] = {0};
1221 int i;
1222
1223 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1224 This will leave enough time for normal connection to get
1225 through. */
1226 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1227 return 0;
1228
1229 /* Don't drop the entry if its number of incoming packets is not
1230 located in [0, 8] */
1231 i = atomic_read(&cp->in_pkts);
1232 if (i > 8 || i < 0) return 0;
1233
1234 if (!todrop_rate[i]) return 0;
1235 if (--todrop_counter[i] > 0) return 0;
1236
1237 todrop_counter[i] = todrop_rate[i];
1238 return 1;
1239}
1240
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001241/* Called from keventd and must protect itself from softirqs */
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001242void ip_vs_random_dropentry(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001245 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Simon Hormana38e5e22013-05-22 14:50:32 +09001247 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Randomly scan 1/32 of the whole table every second
1250 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001251 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001252 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Julian Anastasov088339a2013-03-21 11:58:10 +02001254 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001255 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* connection template */
1257 continue;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001258 if (!net_eq(cp->ipvs->net, net))
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001259 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (cp->protocol == IPPROTO_TCP) {
1261 switch(cp->state) {
1262 case IP_VS_TCP_S_SYN_RECV:
1263 case IP_VS_TCP_S_SYNACK:
1264 break;
1265
1266 case IP_VS_TCP_S_ESTABLISHED:
1267 if (todrop_entry(cp))
1268 break;
1269 continue;
1270
1271 default:
1272 continue;
1273 }
Julian Anastasovacaac5d2013-06-18 10:08:08 +03001274 } else if (cp->protocol == IPPROTO_SCTP) {
1275 switch (cp->state) {
1276 case IP_VS_SCTP_S_INIT1:
1277 case IP_VS_SCTP_S_INIT:
1278 break;
1279 case IP_VS_SCTP_S_ESTABLISHED:
1280 if (todrop_entry(cp))
1281 break;
1282 continue;
1283 default:
1284 continue;
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } else {
1287 if (!todrop_entry(cp))
1288 continue;
1289 }
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 IP_VS_DBG(4, "del connection\n");
1292 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001293 cp_c = cp->control;
1294 /* cp->control is valid only with reference to cp */
1295 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001297 ip_vs_conn_expire_now(cp_c);
1298 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001301 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001303 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306
1307/*
1308 * Flush all the connection entries in the ip_vs_conn_tab
1309 */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001310static void ip_vs_conn_flush(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001313 struct ip_vs_conn *cp, *cp_c;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001314 struct netns_ipvs *ipvs = net_ipvs(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Hans Schillstroma0840e22011-01-03 14:44:58 +01001316flush_again:
Simon Hormana38e5e22013-05-22 14:50:32 +09001317 rcu_read_lock();
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001318 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Julian Anastasov088339a2013-03-21 11:58:10 +02001320 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001321 if (cp->ipvs != ipvs)
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001322 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 IP_VS_DBG(4, "del connection\n");
1324 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001325 cp_c = cp->control;
1326 /* cp->control is valid only with reference to cp */
1327 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001329 ip_vs_conn_expire_now(cp_c);
1330 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001333 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001335 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* the counter may be not NULL, because maybe some conn entries
1338 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001339 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 schedule();
1341 goto flush_again;
1342 }
1343}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001344/*
1345 * per netns init and exit
1346 */
Hans Schillstrom503cf152011-05-01 18:50:16 +02001347int __net_init ip_vs_conn_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001348{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001349 struct netns_ipvs *ipvs = net_ipvs(net);
1350
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001351 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Gao fengd4beaa62013-02-18 01:34:54 +00001353 proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops);
1354 proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001355 return 0;
1356}
1357
Hans Schillstrom503cf152011-05-01 18:50:16 +02001358void __net_exit ip_vs_conn_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001359{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001360 /* flush all the connection entries first */
1361 ip_vs_conn_flush(net);
Gao fengece31ff2013-02-18 01:34:56 +00001362 remove_proc_entry("ip_vs_conn", net->proc_net);
1363 remove_proc_entry("ip_vs_conn_sync", net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Sven Wegener048cf482008-08-10 18:24:35 +00001366int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 int idx;
1369
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001370 /* Compute size and mask */
1371 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1372 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
1375 * Allocate the connection hash table and initialize its list heads
1376 */
Changli Gao731109e2011-02-19 18:05:08 +08001377 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!ip_vs_conn_tab)
1379 return -ENOMEM;
1380
1381 /* Allocate ip_vs_conn slab cache */
1382 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1383 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001384 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!ip_vs_conn_cachep) {
1386 vfree(ip_vs_conn_tab);
1387 return -ENOMEM;
1388 }
1389
Hannes Eder1e3e2382009-08-02 11:05:41 +00001390 pr_info("Connection hash table configured "
1391 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001392 ip_vs_conn_tab_size,
1393 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1395 sizeof(struct ip_vs_conn));
1396
Changli Gao731109e2011-02-19 18:05:08 +08001397 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1398 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001401 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* calculate the random value for connection hash */
1405 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1406
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410void ip_vs_conn_cleanup(void)
1411{
Julian Anastasov088339a2013-03-21 11:58:10 +02001412 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1413 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* Release the empty cache */
1415 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 vfree(ip_vs_conn_tab);
1417}