blob: 4bb1b7d1286d48dabba07469ae348d245ca31113 [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 W. Biederman754b81a2015-09-21 13:02:40 -0500111static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, 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) ^
Eric W. Biederman754b81a2015-09-21 13:02:40 -0500119 ((size_t)ipvs>>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) ^
Eric W. Biederman754b81a2015-09-21 13:02:40 -0500123 ((size_t)ipvs>>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. Biederman754b81a2015-09-21 13:02:40 -0500144 return ip_vs_conn_hashkey(p->ipvs, 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
Eric W. Biedermanf5099dd2015-09-21 13:02:37 -0500317ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
318 int af, const struct sk_buff *skb,
Simon Hormanf11017e2010-08-22 21:37:52 +0900319 const struct ip_vs_iphdr *iph,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700320 struct ip_vs_conn_param *p)
Simon Hormanf11017e2010-08-22 21:37:52 +0900321{
322 __be16 _ports[2], *pptr;
323
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 *
Eric W. Biedermanab161972015-09-21 13:02:38 -0500338ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
339 const struct sk_buff *skb,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700340 const struct ip_vs_iphdr *iph)
Simon Horman5c0d2372010-08-02 17:12:44 +0200341{
Simon Hormanf11017e2010-08-22 21:37:52 +0900342 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200343
Eric W. Biedermanf5099dd2015-09-21 13:02:37 -0500344 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200345 return NULL;
346
Simon Hormanf11017e2010-08-22 21:37:52 +0900347 return ip_vs_conn_in_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200348}
349EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
350
Julian Anastasov87375ab2005-09-14 21:08:51 -0700351/* Get reference to connection template */
Simon Hormanf11017e2010-08-22 21:37:52 +0900352struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
Julian Anastasov87375ab2005-09-14 21:08:51 -0700353{
Eric Dumazet95c96172012-04-15 05:58:06 +0000354 unsigned int hash;
Julian Anastasov87375ab2005-09-14 21:08:51 -0700355 struct ip_vs_conn *cp;
356
Simon Horman85999282010-08-22 21:37:53 +0900357 hash = ip_vs_conn_hashkey_param(p, false);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700358
Julian Anastasov088339a2013-03-21 11:58:10 +0200359 rcu_read_lock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700360
Julian Anastasov088339a2013-03-21 11:58:10 +0200361 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200362 if (unlikely(p->pe_data && p->pe->ct_match)) {
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500363 if (cp->ipvs != p->ipvs)
Julian Anastasov1845ed02013-03-21 11:58:11 +0200364 continue;
Julian Anastasov088339a2013-03-21 11:58:10 +0200365 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
366 if (__ip_vs_conn_get(cp))
367 goto out;
368 }
Simon Horman85999282010-08-22 21:37:53 +0900369 continue;
370 }
371
Simon Hormanf11017e2010-08-22 21:37:52 +0900372 if (cp->af == p->af &&
373 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000374 /* protocol should only be IPPROTO_IP if
Simon Hormanf11017e2010-08-22 21:37:52 +0900375 * p->vaddr is a fwmark */
376 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
377 p->af, p->vaddr, &cp->vaddr) &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200378 p->vport == cp->vport && p->cport == cp->cport &&
Julian Anastasov87375ab2005-09-14 21:08:51 -0700379 cp->flags & IP_VS_CONN_F_TEMPLATE &&
Julian Anastasov1845ed02013-03-21 11:58:11 +0200380 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500381 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200382 if (__ip_vs_conn_get(cp))
383 goto out;
384 }
Julian Anastasov87375ab2005-09-14 21:08:51 -0700385 }
386 cp = NULL;
387
388 out:
Julian Anastasov088339a2013-03-21 11:58:10 +0200389 rcu_read_unlock();
Julian Anastasov87375ab2005-09-14 21:08:51 -0700390
Julius Volz28364a52008-09-02 15:55:43 +0200391 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900392 ip_vs_proto_name(p->protocol),
393 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
394 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200395 cp ? "hit" : "not hit");
Julian Anastasov87375ab2005-09-14 21:08:51 -0700396
397 return cp;
398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Simon Hormanf11017e2010-08-22 21:37:52 +0900400/* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
401 * Called for pkts coming from inside-to-OUTside.
402 * p->caddr, p->cport: pkt source address (inside host)
403 * p->vaddr, p->vport: pkt dest address (foreign host) */
404struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Eric Dumazet95c96172012-04-15 05:58:06 +0000406 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct ip_vs_conn *cp, *ret=NULL;
408
409 /*
410 * Check for "full" addressed entries
411 */
Simon Horman85999282010-08-22 21:37:53 +0900412 hash = ip_vs_conn_hashkey_param(p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Julian Anastasov088339a2013-03-21 11:58:10 +0200414 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Julian Anastasov088339a2013-03-21 11:58:10 +0200416 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov1845ed02013-03-21 11:58:11 +0200417 if (p->vport == cp->cport && p->cport == cp->dport &&
418 cp->af == p->af &&
Simon Hormanf11017e2010-08-22 21:37:52 +0900419 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
420 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100421 p->protocol == cp->protocol &&
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500422 cp->ipvs == p->ipvs) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200423 if (!__ip_vs_conn_get(cp))
424 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* HIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ret = cp;
427 break;
428 }
429 }
430
Julian Anastasov088339a2013-03-21 11:58:10 +0200431 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Julius Volz28364a52008-09-02 15:55:43 +0200433 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
Simon Hormanf11017e2010-08-22 21:37:52 +0900434 ip_vs_proto_name(p->protocol),
435 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
436 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
Julius Volz28364a52008-09-02 15:55:43 +0200437 ret ? "hit" : "not hit");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return ret;
440}
441
Simon Horman5c0d2372010-08-02 17:12:44 +0200442struct ip_vs_conn *
Eric W. Biederman0cf705c8c2015-09-21 13:02:39 -0500443ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
444 const struct sk_buff *skb,
Alex Gartrell802c41a2015-08-26 09:40:32 -0700445 const struct ip_vs_iphdr *iph)
Simon Horman5c0d2372010-08-02 17:12:44 +0200446{
Simon Hormanf11017e2010-08-22 21:37:52 +0900447 struct ip_vs_conn_param p;
Simon Horman5c0d2372010-08-02 17:12:44 +0200448
Eric W. Biedermanf5099dd2015-09-21 13:02:37 -0500449 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
Simon Horman5c0d2372010-08-02 17:12:44 +0200450 return NULL;
451
Simon Hormanf11017e2010-08-22 21:37:52 +0900452 return ip_vs_conn_out_get(&p);
Simon Horman5c0d2372010-08-02 17:12:44 +0200453}
454EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
457 * Put back the conn and restart its timer with its timeout
458 */
459void ip_vs_conn_put(struct ip_vs_conn *cp)
460{
Nick Chalk26ec0372010-06-22 08:07:01 +0200461 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
462 0 : cp->timeout;
463 mod_timer(&cp->timer, jiffies+t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 __ip_vs_conn_put(cp);
466}
467
468
469/*
470 * Fill a no_client_port connection with a client port number
471 */
Al Viro014d7302006-09-28 14:29:52 -0700472void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 if (ip_vs_conn_unhash(cp)) {
Julian Anastasovac692692013-03-22 11:46:54 +0200475 spin_lock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
477 atomic_dec(&ip_vs_conn_no_cport_cnt);
478 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
479 cp->cport = cport;
480 }
Julian Anastasovac692692013-03-22 11:46:54 +0200481 spin_unlock_bh(&cp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* hash on new dport */
484 ip_vs_conn_hash(cp);
485 }
486}
487
488
489/*
490 * Bind a connection entry with the corresponding packet_xmit.
491 * Called by ip_vs_conn_new.
492 */
493static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
494{
495 switch (IP_VS_FWD_METHOD(cp)) {
496 case IP_VS_CONN_F_MASQ:
497 cp->packet_xmit = ip_vs_nat_xmit;
498 break;
499
500 case IP_VS_CONN_F_TUNNEL:
Alex Gartrell8052ba22014-09-09 16:40:28 -0700501#ifdef CONFIG_IP_VS_IPV6
502 if (cp->daf == AF_INET6)
503 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
504 else
505#endif
506 cp->packet_xmit = ip_vs_tunnel_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508
509 case IP_VS_CONN_F_DROUTE:
510 cp->packet_xmit = ip_vs_dr_xmit;
511 break;
512
513 case IP_VS_CONN_F_LOCALNODE:
514 cp->packet_xmit = ip_vs_null_xmit;
515 break;
516
517 case IP_VS_CONN_F_BYPASS:
518 cp->packet_xmit = ip_vs_bypass_xmit;
519 break;
520 }
521}
522
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200523#ifdef CONFIG_IP_VS_IPV6
524static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
525{
526 switch (IP_VS_FWD_METHOD(cp)) {
527 case IP_VS_CONN_F_MASQ:
528 cp->packet_xmit = ip_vs_nat_xmit_v6;
529 break;
530
531 case IP_VS_CONN_F_TUNNEL:
Alex Gartrell8052ba22014-09-09 16:40:28 -0700532 if (cp->daf == AF_INET6)
533 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
534 else
535 cp->packet_xmit = ip_vs_tunnel_xmit;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200536 break;
537
538 case IP_VS_CONN_F_DROUTE:
539 cp->packet_xmit = ip_vs_dr_xmit_v6;
540 break;
541
542 case IP_VS_CONN_F_LOCALNODE:
543 cp->packet_xmit = ip_vs_null_xmit;
544 break;
545
546 case IP_VS_CONN_F_BYPASS:
547 cp->packet_xmit = ip_vs_bypass_xmit_v6;
548 break;
549 }
550}
551#endif
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
555{
556 return atomic_read(&dest->activeconns)
557 + atomic_read(&dest->inactconns);
558}
559
560/*
561 * Bind a connection entry with a virtual service destination
562 * Called just after a new connection entry is created.
563 */
564static inline void
565ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
566{
Julian Anastasov35757922010-09-17 14:18:16 +0200567 unsigned int conn_flags;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200568 __u32 flags;
Julian Anastasov35757922010-09-17 14:18:16 +0200569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* if dest is NULL, then return directly */
571 if (!dest)
572 return;
573
574 /* Increase the refcnt counter of the dest */
Julian Anastasovfca9c202013-03-22 11:46:38 +0200575 ip_vs_dest_hold(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Julian Anastasov35757922010-09-17 14:18:16 +0200577 conn_flags = atomic_read(&dest->conn_flags);
578 if (cp->protocol != IPPROTO_UDP)
579 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200580 flags = cp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Bind with the destination and its corresponding transmitter */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200582 if (flags & IP_VS_CONN_F_SYNC) {
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800583 /* if the connection is not template and is created
584 * by sync, preserve the activity flag.
585 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200586 if (!(flags & IP_VS_CONN_F_TEMPLATE))
Julian Anastasov35757922010-09-17 14:18:16 +0200587 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
Julian Anastasov32337592010-10-17 16:43:36 +0300588 /* connections inherit forwarding method from dest */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200589 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
Julian Anastasov35757922010-09-17 14:18:16 +0200590 }
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200591 flags |= conn_flags;
592 cp->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 cp->dest = dest;
594
Julius Volzcfc78c52008-09-02 15:55:53 +0200595 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
596 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
597 "dest->refcnt:%d\n",
598 ip_vs_proto_name(cp->protocol),
599 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
600 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700601 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcfc78c52008-09-02 15:55:53 +0200602 ip_vs_fwd_tag(cp), cp->state,
603 cp->flags, atomic_read(&cp->refcnt),
604 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Update the connection counters */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200607 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
Julian Anastasov06611f82012-04-24 23:46:36 +0300608 /* It is a normal connection, so modify the counters
609 * according to the flags, later the protocol can
610 * update them on state change
611 */
Pablo Neira Ayuso6b324db2012-05-08 09:28:19 +0200612 if (!(flags & IP_VS_CONN_F_INACTIVE))
Rumen G. Bogdanovskib2096392007-11-19 21:53:27 -0800613 atomic_inc(&dest->activeconns);
614 else
615 atomic_inc(&dest->inactconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 } else {
617 /* It is a persistent connection/template, so increase
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300618 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 atomic_inc(&dest->persistconns);
620 }
621
622 if (dest->u_threshold != 0 &&
623 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
624 dest->flags |= IP_VS_DEST_F_OVERLOAD;
625}
626
627
628/*
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800629 * Check if there is a destination for the connection, if so
630 * bind the connection to the destination.
631 */
Julian Anastasov413c2d042013-03-22 11:46:52 +0200632void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800633{
634 struct ip_vs_dest *dest;
635
Julian Anastasov413c2d042013-03-22 11:46:52 +0200636 rcu_read_lock();
Alex Gartrell655eef12014-09-09 16:40:21 -0700637
638 /* This function is only invoked by the synchronization code. We do
639 * not currently support heterogeneous pools with synchronization,
640 * so we can make the assumption that the svc_af is the same as the
641 * dest_af
642 */
Eric W. Biedermandc2add62015-09-21 13:01:51 -0500643 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
Julian Anastasov882a8442012-04-24 23:46:37 +0300644 cp->dport, &cp->vaddr, cp->vport,
645 cp->protocol, cp->fwmark, cp->flags);
646 if (dest) {
647 struct ip_vs_proto_data *pd;
648
Julian Anastasovac692692013-03-22 11:46:54 +0200649 spin_lock_bh(&cp->lock);
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200650 if (cp->dest) {
Julian Anastasovac692692013-03-22 11:46:54 +0200651 spin_unlock_bh(&cp->lock);
Julian Anastasov413c2d042013-03-22 11:46:52 +0200652 rcu_read_unlock();
653 return;
Pablo Neira Ayusof73181c2012-05-08 19:40:30 +0200654 }
655
Julian Anastasov882a8442012-04-24 23:46:37 +0300656 /* Applications work depending on the forwarding method
657 * but better to reassign them always when binding dest */
658 if (cp->app)
659 ip_vs_unbind_app(cp);
660
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800661 ip_vs_bind_dest(cp, dest);
Julian Anastasovac692692013-03-22 11:46:54 +0200662 spin_unlock_bh(&cp->lock);
Julian Anastasov882a8442012-04-24 23:46:37 +0300663
664 /* Update its packet transmitter */
665 cp->packet_xmit = NULL;
666#ifdef CONFIG_IP_VS_IPV6
667 if (cp->af == AF_INET6)
668 ip_vs_bind_xmit_v6(cp);
669 else
670#endif
671 ip_vs_bind_xmit(cp);
672
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500673 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
Julian Anastasov882a8442012-04-24 23:46:37 +0300674 if (pd && atomic_read(&pd->appcnt))
675 ip_vs_bind_app(cp, pd->pp);
676 }
Julian Anastasov413c2d042013-03-22 11:46:52 +0200677 rcu_read_unlock();
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800678}
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800679
680
681/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * Unbind a connection entry with its VS destination
683 * Called by the ip_vs_conn_expire function.
684 */
685static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
686{
687 struct ip_vs_dest *dest = cp->dest;
688
689 if (!dest)
690 return;
691
Julius Volzcfc78c52008-09-02 15:55:53 +0200692 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
693 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
694 "dest->refcnt:%d\n",
695 ip_vs_proto_name(cp->protocol),
696 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
697 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700698 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcfc78c52008-09-02 15:55:53 +0200699 ip_vs_fwd_tag(cp), cp->state,
700 cp->flags, atomic_read(&cp->refcnt),
701 atomic_read(&dest->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* Update the connection counters */
Julian Anastasov87375ab2005-09-14 21:08:51 -0700704 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* It is a normal connection, so decrease the inactconns
706 or activeconns counter */
707 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
708 atomic_dec(&dest->inactconns);
709 } else {
710 atomic_dec(&dest->activeconns);
711 }
712 } else {
713 /* It is a persistent connection/template, so decrease
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300714 the persistent connection counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 atomic_dec(&dest->persistconns);
716 }
717
718 if (dest->l_threshold != 0) {
719 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
720 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
721 } else if (dest->u_threshold != 0) {
722 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
723 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
724 } else {
725 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
726 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
727 }
728
Julian Anastasovfca9c202013-03-22 11:46:38 +0200729 ip_vs_dest_put(dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Simon Horman8e1b0b12011-02-04 18:33:01 +0900732static int expire_quiescent_template(struct netns_ipvs *ipvs,
733 struct ip_vs_dest *dest)
734{
735#ifdef CONFIG_SYSCTL
736 return ipvs->sysctl_expire_quiescent_template &&
737 (atomic_read(&dest->weight) == 0);
738#else
739 return 0;
740#endif
741}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/*
744 * Checking if the destination of a connection template is available.
745 * If available, return 1, otherwise invalidate this connection
746 * template and return 0.
747 */
748int ip_vs_check_template(struct ip_vs_conn *ct)
749{
750 struct ip_vs_dest *dest = ct->dest;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500751 struct netns_ipvs *ipvs = ct->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * Checking the dest server status.
755 */
756 if ((dest == NULL) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900757 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
Simon Horman8e1b0b12011-02-04 18:33:01 +0900758 expire_quiescent_template(ipvs, dest)) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200759 IP_VS_DBG_BUF(9, "check_template: dest not available for "
760 "protocol %s s:%s:%d v:%s:%d "
761 "-> d:%s:%d\n",
762 ip_vs_proto_name(ct->protocol),
763 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
764 ntohs(ct->cport),
765 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
766 ntohs(ct->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700767 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
Julius Volzcfc78c52008-09-02 15:55:53 +0200768 ntohs(ct->dport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /*
771 * Invalidate the connection template
772 */
Al Viro014d7302006-09-28 14:29:52 -0700773 if (ct->vport != htons(0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (ip_vs_conn_unhash(ct)) {
Al Viro014d7302006-09-28 14:29:52 -0700775 ct->dport = htons(0xffff);
776 ct->vport = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ct->cport = 0;
778 ip_vs_conn_hash(ct);
779 }
780 }
781
782 /*
783 * Simply decrease the refcnt of the template,
784 * don't restart its timer.
785 */
Julian Anastasov088339a2013-03-21 11:58:10 +0200786 __ip_vs_conn_put(ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 0;
788 }
789 return 1;
790}
791
Julian Anastasov088339a2013-03-21 11:58:10 +0200792static void ip_vs_conn_rcu_free(struct rcu_head *head)
793{
794 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
795 rcu_head);
796
797 ip_vs_pe_put(cp->pe);
798 kfree(cp->pe_data);
799 kmem_cache_free(ip_vs_conn_cachep, cp);
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static void ip_vs_conn_expire(unsigned long data)
803{
804 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500805 struct netns_ipvs *ipvs = cp->ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /*
808 * do I control anybody?
809 */
810 if (atomic_read(&cp->n_control))
811 goto expire_later;
812
Julian Anastasov088339a2013-03-21 11:58:10 +0200813 /* Unlink conn if not referenced anymore */
814 if (likely(ip_vs_conn_unlink(cp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* delete the timer if it is activated by other users */
Ying Xue25cc4ae2013-02-03 20:32:57 +0000816 del_timer(&cp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* does anybody control me? */
819 if (cp->control)
820 ip_vs_control_del(cp);
821
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200822 if (cp->flags & IP_VS_CONN_F_NFCT) {
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +0200823 /* Do not access conntracks during subsys cleanup
824 * because nf_conntrack_find_get can not be used after
825 * conntrack cleanup for the net.
826 */
827 smp_rmb();
828 if (ipvs->enable)
829 ip_vs_conn_drop_conntrack(cp);
830 }
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (unlikely(cp->app != NULL))
833 ip_vs_unbind_app(cp);
834 ip_vs_unbind_dest(cp);
835 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
836 atomic_dec(&ip_vs_conn_no_cport_cnt);
Julian Anastasov088339a2013-03-21 11:58:10 +0200837 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100838 atomic_dec(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return;
840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 expire_later:
Julian Anastasov088339a2013-03-21 11:58:10 +0200843 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
844 atomic_read(&cp->refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 atomic_read(&cp->n_control));
846
Julian Anastasov088339a2013-03-21 11:58:10 +0200847 atomic_inc(&cp->refcnt);
848 cp->timeout = 60*HZ;
849
Julian Anastasov749c42b2012-04-24 23:46:40 +0300850 if (ipvs->sync_state & IP_VS_STATE_MASTER)
Eric W. Biedermanb61a8c12015-09-21 13:02:17 -0500851 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
Julian Anastasov749c42b2012-04-24 23:46:40 +0300852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ip_vs_conn_put(cp);
854}
855
Julian Anastasov088339a2013-03-21 11:58:10 +0200856/* Modify timer, so that it expires as soon as possible.
857 * Can be called without reference only if under RCU lock.
858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
860{
Julian Anastasov088339a2013-03-21 11:58:10 +0200861 /* Using mod_timer_pending will ensure the timer is not
862 * modified after the final del_timer in ip_vs_conn_expire.
863 */
864 if (timer_pending(&cp->timer) &&
865 time_after(cp->timer.expires, jiffies))
866 mod_timer_pending(&cp->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869
870/*
871 * Create a new connection entry and hash it into the ip_vs_conn_tab
872 */
873struct ip_vs_conn *
Alex Gartrellba385282014-09-09 16:40:23 -0700874ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
Eric Dumazet95c96172012-04-15 05:58:06 +0000875 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100876 struct ip_vs_dest *dest, __u32 fwmark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct ip_vs_conn *cp;
Eric W. Biedermane64e2b42015-09-21 13:01:42 -0500879 struct netns_ipvs *ipvs = p->ipvs;
Eric W. Biederman18d6ade2015-09-21 13:02:01 -0500880 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100881 p->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200883 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (cp == NULL) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000885 IP_VS_ERR_RL("%s(): no memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return NULL;
887 }
888
Changli Gao731109e2011-02-19 18:05:08 +0800889 INIT_HLIST_NODE(&cp->c_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800890 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -0500891 cp->ipvs = ipvs;
Simon Hormanf11017e2010-08-22 21:37:52 +0900892 cp->af = p->af;
Alex Gartrellba385282014-09-09 16:40:23 -0700893 cp->daf = dest_af;
Simon Hormanf11017e2010-08-22 21:37:52 +0900894 cp->protocol = p->protocol;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200895 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
Simon Hormanf11017e2010-08-22 21:37:52 +0900896 cp->cport = p->cport;
Michal Kubecek2a971352014-01-30 08:50:20 +0100897 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200898 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
Michal Kubecek2a971352014-01-30 08:50:20 +0100899 &cp->vaddr, p->vaddr);
900 cp->vport = p->vport;
Alex Gartrellba385282014-09-09 16:40:23 -0700901 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 cp->dport = dport;
903 cp->flags = flags;
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100904 cp->fwmark = fwmark;
Simon Hormane9e5eee2010-11-08 20:05:57 +0900905 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
906 ip_vs_pe_get(p->pe);
907 cp->pe = p->pe;
Simon Horman85999282010-08-22 21:37:53 +0900908 cp->pe_data = p->pe_data;
909 cp->pe_data_len = p->pe_data_len;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200910 } else {
911 cp->pe = NULL;
912 cp->pe_data = NULL;
913 cp->pe_data_len = 0;
Simon Horman85999282010-08-22 21:37:53 +0900914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 spin_lock_init(&cp->lock);
916
917 /*
918 * Set the entry is referenced by the current thread before hashing
919 * it in the table, so that other thread run ip_vs_random_dropentry
920 * but cannot drop this entry.
921 */
922 atomic_set(&cp->refcnt, 1);
923
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200924 cp->control = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 atomic_set(&cp->n_control, 0);
926 atomic_set(&cp->in_pkts, 0);
927
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200928 cp->packet_xmit = NULL;
929 cp->app = NULL;
930 cp->app_data = NULL;
931 /* reset struct ip_vs_seq */
932 cp->in_seq.delta = 0;
933 cp->out_seq.delta = 0;
934
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100935 atomic_inc(&ipvs->conn_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (flags & IP_VS_CONN_F_NO_CPORT)
937 atomic_inc(&ip_vs_conn_no_cport_cnt);
938
939 /* Bind the connection with a destination server */
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200940 cp->dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ip_vs_bind_dest(cp, dest);
942
943 /* Set its state and timeout */
944 cp->state = 0;
Julian Anastasov9a05475c2013-03-21 11:58:12 +0200945 cp->old_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 cp->timeout = 3*HZ;
Julian Anastasov749c42b2012-04-24 23:46:40 +0300947 cp->sync_endtime = jiffies & ~3UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* Bind its packet transmitter */
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200950#ifdef CONFIG_IP_VS_IPV6
Simon Hormanf11017e2010-08-22 21:37:52 +0900951 if (p->af == AF_INET6)
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200952 ip_vs_bind_xmit_v6(cp);
953 else
954#endif
955 ip_vs_bind_xmit(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Hans Schillstrom9bbac6a2011-01-03 14:44:52 +0100957 if (unlikely(pd && atomic_read(&pd->appcnt)))
958 ip_vs_bind_app(cp, pd->pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200960 /*
961 * Allow conntrack to be preserved. By default, conntrack
962 * is created and destroyed for every packet.
963 * Sometimes keeping conntrack can be useful for
964 * IP_VS_CONN_F_ONE_PACKET too.
965 */
966
Hans Schillstroma0840e22011-01-03 14:44:58 +0100967 if (ip_vs_conntrack_enabled(ipvs))
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200968 cp->flags |= IP_VS_CONN_F_NFCT;
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Hash it in the ip_vs_conn_tab finally */
971 ip_vs_conn_hash(cp);
972
973 return cp;
974}
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/*
977 * /proc/net/ip_vs_conn entries
978 */
979#ifdef CONFIG_PROC_FS
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100980struct ip_vs_iter_state {
Changli Gao731109e2011-02-19 18:05:08 +0800981 struct seq_net_private p;
982 struct hlist_head *l;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100983};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
986{
987 int idx;
988 struct ip_vs_conn *cp;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100989 struct ip_vs_iter_state *iter = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900990
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +0100991 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +0200992 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
993 /* __ip_vs_conn_get() is not needed by
994 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (pos-- == 0) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100997 iter->l = &ip_vs_conn_tab[idx];
Changli Gao731109e2011-02-19 18:05:08 +0800998 return cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001001 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
1004 return NULL;
1005}
1006
1007static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001008 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001010 struct ip_vs_iter_state *iter = seq->private;
1011
1012 iter->l = NULL;
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001013 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1015}
1016
1017static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1018{
1019 struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001020 struct ip_vs_iter_state *iter = seq->private;
Julian Anastasov088339a2013-03-21 11:58:10 +02001021 struct hlist_node *e;
Changli Gao731109e2011-02-19 18:05:08 +08001022 struct hlist_head *l = iter->l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int idx;
1024
1025 ++*pos;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001026 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return ip_vs_conn_array(seq, 0);
1028
1029 /* more on same hash chain? */
Julian Anastasov088339a2013-03-21 11:58:10 +02001030 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1031 if (e)
1032 return hlist_entry(e, struct ip_vs_conn, c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 idx = l - ip_vs_conn_tab;
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001035 while (++idx < ip_vs_conn_tab_size) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001036 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001037 iter->l = &ip_vs_conn_tab[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return cp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001039 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001040 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001042 iter->l = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return NULL;
1044}
1045
1046static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001047 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Julian Anastasov7cf2eb72013-04-17 23:50:46 +03001049 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1053{
1054
1055 if (v == SEQ_START_TOKEN)
1056 seq_puts(seq,
Simon Hormana3c918a2010-08-22 21:37:53 +09001057 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 else {
1059 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001060 struct net *net = seq_file_net(seq);
Simon Hormana3c918a2010-08-22 21:37:53 +09001061 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1062 size_t len = 0;
Julian Anastasovf18ae722014-09-09 16:40:38 -07001063 char dbuf[IP_VS_ADDRSTRLEN];
Simon Hormana3c918a2010-08-22 21:37:53 +09001064
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001065 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001066 return 0;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001067 if (cp->pe_data) {
Simon Hormana3c918a2010-08-22 21:37:53 +09001068 pe_data[0] = ' ';
Simon Hormane9e5eee2010-11-08 20:05:57 +09001069 len = strlen(cp->pe->name);
1070 memcpy(pe_data + 1, cp->pe->name, len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001071 pe_data[len + 1] = ' ';
1072 len += 2;
Simon Hormane9e5eee2010-11-08 20:05:57 +09001073 len += cp->pe->show_pe_data(cp, pe_data + len);
Simon Hormana3c918a2010-08-22 21:37:53 +09001074 }
1075 pe_data[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Vince Busam667a5f12008-09-02 15:55:49 +02001077#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001078 if (cp->daf == AF_INET6)
1079 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1080 else
1081#endif
1082 snprintf(dbuf, sizeof(dbuf), "%08X",
1083 ntohl(cp->daddr.ip));
1084
1085#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001086 if (cp->af == AF_INET6)
Simon Hormana3c918a2010-08-22 21:37:53 +09001087 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001088 "%s %04X %-11s %7lu%s\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001089 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001090 &cp->caddr.in6, ntohs(cp->cport),
1091 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001092 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001093 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001094 (cp->timer.expires-jiffies)/HZ, pe_data);
Vince Busam667a5f12008-09-02 15:55:49 +02001095 else
1096#endif
1097 seq_printf(seq,
1098 "%-3s %08X %04X %08X %04X"
Julian Anastasovf18ae722014-09-09 16:40:38 -07001099 " %s %04X %-11s %7lu%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001101 ntohl(cp->caddr.ip), ntohs(cp->cport),
1102 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001103 dbuf, ntohs(cp->dport),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ip_vs_state_name(cp->protocol, cp->state),
Simon Hormana3c918a2010-08-22 21:37:53 +09001105 (cp->timer.expires-jiffies)/HZ, pe_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 return 0;
1108}
1109
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001110static const struct seq_operations ip_vs_conn_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 .start = ip_vs_conn_seq_start,
1112 .next = ip_vs_conn_seq_next,
1113 .stop = ip_vs_conn_seq_stop,
1114 .show = ip_vs_conn_seq_show,
1115};
1116
1117static int ip_vs_conn_open(struct inode *inode, struct file *file)
1118{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001119 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1120 sizeof(struct ip_vs_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Arjan van de Ven9a321442007-02-12 00:55:35 -08001123static const struct file_operations ip_vs_conn_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 .owner = THIS_MODULE,
1125 .open = ip_vs_conn_open,
1126 .read = seq_read,
1127 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001128 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129};
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001130
Eric Dumazet95c96172012-04-15 05:58:06 +00001131static const char *ip_vs_origin_name(unsigned int flags)
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001132{
1133 if (flags & IP_VS_CONN_F_SYNC)
1134 return "SYNC";
1135 else
1136 return "LOCAL";
1137}
1138
1139static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1140{
Julian Anastasovf18ae722014-09-09 16:40:38 -07001141 char dbuf[IP_VS_ADDRSTRLEN];
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001142
1143 if (v == SEQ_START_TOKEN)
1144 seq_puts(seq,
1145 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1146 else {
1147 const struct ip_vs_conn *cp = v;
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001148 struct net *net = seq_file_net(seq);
1149
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001150 if (!net_eq(cp->ipvs->net, net))
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001151 return 0;
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001152
Vince Busam667a5f12008-09-02 15:55:49 +02001153#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovf18ae722014-09-09 16:40:38 -07001154 if (cp->daf == AF_INET6)
1155 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1156 else
1157#endif
1158 snprintf(dbuf, sizeof(dbuf), "%08X",
1159 ntohl(cp->daddr.ip));
1160
1161#ifdef CONFIG_IP_VS_IPV6
Vince Busam667a5f12008-09-02 15:55:49 +02001162 if (cp->af == AF_INET6)
Julian Anastasovf18ae722014-09-09 16:40:38 -07001163 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1164 "%s %04X %-11s %-6s %7lu\n",
Vince Busam667a5f12008-09-02 15:55:49 +02001165 ip_vs_proto_name(cp->protocol),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001166 &cp->caddr.in6, ntohs(cp->cport),
1167 &cp->vaddr.in6, ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001168 dbuf, ntohs(cp->dport),
Vince Busam667a5f12008-09-02 15:55:49 +02001169 ip_vs_state_name(cp->protocol, cp->state),
1170 ip_vs_origin_name(cp->flags),
1171 (cp->timer.expires-jiffies)/HZ);
1172 else
1173#endif
1174 seq_printf(seq,
1175 "%-3s %08X %04X %08X %04X "
Julian Anastasovf18ae722014-09-09 16:40:38 -07001176 "%s %04X %-11s %-6s %7lu\n",
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001177 ip_vs_proto_name(cp->protocol),
Julius Volze7ade462008-09-02 15:55:33 +02001178 ntohl(cp->caddr.ip), ntohs(cp->cport),
1179 ntohl(cp->vaddr.ip), ntohs(cp->vport),
Julian Anastasovf18ae722014-09-09 16:40:38 -07001180 dbuf, ntohs(cp->dport),
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001181 ip_vs_state_name(cp->protocol, cp->state),
1182 ip_vs_origin_name(cp->flags),
1183 (cp->timer.expires-jiffies)/HZ);
1184 }
1185 return 0;
1186}
1187
1188static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1189 .start = ip_vs_conn_seq_start,
1190 .next = ip_vs_conn_seq_next,
1191 .stop = ip_vs_conn_seq_stop,
1192 .show = ip_vs_conn_sync_seq_show,
1193};
1194
1195static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1196{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001197 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1198 sizeof(struct ip_vs_iter_state));
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001199}
1200
1201static const struct file_operations ip_vs_conn_sync_fops = {
1202 .owner = THIS_MODULE,
1203 .open = ip_vs_conn_sync_open,
1204 .read = seq_read,
1205 .llseek = seq_lseek,
Hans Schillstrom0f081902011-05-15 17:20:29 +02001206 .release = seq_release_net,
Rumen G. Bogdanovski7a4fbb12007-11-19 21:52:42 -08001207};
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209#endif
1210
1211
1212/*
1213 * Randomly drop connection entries before running out of memory
1214 */
1215static inline int todrop_entry(struct ip_vs_conn *cp)
1216{
1217 /*
1218 * The drop rate array needs tuning for real environments.
1219 * Called from timer bh only => no locking
1220 */
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001221 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 static char todrop_counter[9] = {0};
1223 int i;
1224
1225 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1226 This will leave enough time for normal connection to get
1227 through. */
1228 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1229 return 0;
1230
1231 /* Don't drop the entry if its number of incoming packets is not
1232 located in [0, 8] */
1233 i = atomic_read(&cp->in_pkts);
1234 if (i > 8 || i < 0) return 0;
1235
1236 if (!todrop_rate[i]) return 0;
1237 if (--todrop_counter[i] > 0) return 0;
1238
1239 todrop_counter[i] = todrop_rate[i];
1240 return 1;
1241}
1242
Julian Anastasovaf9debd2005-07-11 20:59:57 -07001243/* Called from keventd and must protect itself from softirqs */
Eric W. Biederman423b5592015-09-21 13:02:24 -05001244void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001247 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Simon Hormana38e5e22013-05-22 14:50:32 +09001249 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Randomly scan 1/32 of the whole table every second
1252 */
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001253 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001254 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Julian Anastasov088339a2013-03-21 11:58:10 +02001256 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
Julian Anastasov87375ab2005-09-14 21:08:51 -07001257 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* connection template */
1259 continue;
Eric W. Biederman423b5592015-09-21 13:02:24 -05001260 if (cp->ipvs != ipvs)
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001261 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (cp->protocol == IPPROTO_TCP) {
1263 switch(cp->state) {
1264 case IP_VS_TCP_S_SYN_RECV:
1265 case IP_VS_TCP_S_SYNACK:
1266 break;
1267
1268 case IP_VS_TCP_S_ESTABLISHED:
1269 if (todrop_entry(cp))
1270 break;
1271 continue;
1272
1273 default:
1274 continue;
1275 }
Julian Anastasovacaac5d2013-06-18 10:08:08 +03001276 } else if (cp->protocol == IPPROTO_SCTP) {
1277 switch (cp->state) {
1278 case IP_VS_SCTP_S_INIT1:
1279 case IP_VS_SCTP_S_INIT:
1280 break;
1281 case IP_VS_SCTP_S_ESTABLISHED:
1282 if (todrop_entry(cp))
1283 break;
1284 continue;
1285 default:
1286 continue;
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 } else {
1289 if (!todrop_entry(cp))
1290 continue;
1291 }
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 IP_VS_DBG(4, "del connection\n");
1294 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001295 cp_c = cp->control;
1296 /* cp->control is valid only with reference to cp */
1297 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001299 ip_vs_conn_expire_now(cp_c);
1300 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001303 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001305 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308
1309/*
1310 * Flush all the connection entries in the ip_vs_conn_tab
1311 */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001312static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 int idx;
Julian Anastasov088339a2013-03-21 11:58:10 +02001315 struct ip_vs_conn *cp, *cp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Hans Schillstroma0840e22011-01-03 14:44:58 +01001317flush_again:
Simon Hormana38e5e22013-05-22 14:50:32 +09001318 rcu_read_lock();
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001319 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Julian Anastasov088339a2013-03-21 11:58:10 +02001321 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
Eric W. Biederman58dbc6f2015-09-21 13:01:41 -05001322 if (cp->ipvs != ipvs)
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 IP_VS_DBG(4, "del connection\n");
1325 ip_vs_conn_expire_now(cp);
Julian Anastasov088339a2013-03-21 11:58:10 +02001326 cp_c = cp->control;
1327 /* cp->control is valid only with reference to cp */
1328 if (cp_c && __ip_vs_conn_get(cp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 IP_VS_DBG(4, "del conn template\n");
Julian Anastasov088339a2013-03-21 11:58:10 +02001330 ip_vs_conn_expire_now(cp_c);
1331 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001334 cond_resched_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
Simon Hormana38e5e22013-05-22 14:50:32 +09001336 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* the counter may be not NULL, because maybe some conn entries
1339 are run by slow timer handler or unhashed but still referred */
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001340 if (atomic_read(&ipvs->conn_count) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 schedule();
1342 goto flush_again;
1343 }
1344}
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001345/*
1346 * per netns init and exit
1347 */
Hans Schillstrom503cf152011-05-01 18:50:16 +02001348int __net_init ip_vs_conn_net_init(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001349{
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001350 struct netns_ipvs *ipvs = net_ipvs(net);
1351
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001352 atomic_set(&ipvs->conn_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Gao fengd4beaa62013-02-18 01:34:54 +00001354 proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops);
1355 proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001356 return 0;
1357}
1358
Hans Schillstrom503cf152011-05-01 18:50:16 +02001359void __net_exit ip_vs_conn_net_cleanup(struct net *net)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001360{
Eric W. Biedermand8897172015-09-21 13:02:41 -05001361 struct netns_ipvs *ipvs = net_ipvs(net);
1362
Hans Schillstrom6e67e582011-01-03 14:44:57 +01001363 /* flush all the connection entries first */
Eric W. Biedermand8897172015-09-21 13:02:41 -05001364 ip_vs_conn_flush(ipvs);
Gao fengece31ff2013-02-18 01:34:56 +00001365 remove_proc_entry("ip_vs_conn", net->proc_net);
1366 remove_proc_entry("ip_vs_conn_sync", net->proc_net);
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Sven Wegener048cf482008-08-10 18:24:35 +00001369int __init ip_vs_conn_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 int idx;
1372
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001373 /* Compute size and mask */
1374 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1375 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /*
1378 * Allocate the connection hash table and initialize its list heads
1379 */
Changli Gao731109e2011-02-19 18:05:08 +08001380 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!ip_vs_conn_tab)
1382 return -ENOMEM;
1383
1384 /* Allocate ip_vs_conn slab cache */
1385 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1386 sizeof(struct ip_vs_conn), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001387 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!ip_vs_conn_cachep) {
1389 vfree(ip_vs_conn_tab);
1390 return -ENOMEM;
1391 }
1392
Hannes Eder1e3e2382009-08-02 11:05:41 +00001393 pr_info("Connection hash table configured "
1394 "(size=%d, memory=%ldKbytes)\n",
Catalin(ux) M. BOIE6f7edb42010-01-05 05:50:24 +01001395 ip_vs_conn_tab_size,
1396 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1398 sizeof(struct ip_vs_conn));
1399
Changli Gao731109e2011-02-19 18:05:08 +08001400 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1401 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
Julian Anastasov088339a2013-03-21 11:58:10 +02001404 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* calculate the random value for connection hash */
1408 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1409
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413void ip_vs_conn_cleanup(void)
1414{
Julian Anastasov088339a2013-03-21 11:58:10 +02001415 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1416 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Release the empty cache */
1418 kmem_cache_destroy(ip_vs_conn_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 vfree(ip_vs_conn_tab);
1420}