]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | * | |
1da177e4 LT |
8 | * 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 | ||
9aada7ac HE |
25 | #define KMSG_COMPONENT "IPVS" |
26 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
27 | ||
e924283b | 28 | #include <linux/interrupt.h> |
14c85021 | 29 | #include <linux/in.h> |
f190055f | 30 | #include <linux/net.h> |
1da177e4 | 31 | #include <linux/kernel.h> |
14c85021 | 32 | #include <linux/module.h> |
1da177e4 LT |
33 | #include <linux/vmalloc.h> |
34 | #include <linux/proc_fs.h> /* for proc_net_* */ | |
5a0e3ad6 | 35 | #include <linux/slab.h> |
1da177e4 LT |
36 | #include <linux/seq_file.h> |
37 | #include <linux/jhash.h> | |
38 | #include <linux/random.h> | |
39 | ||
457c4cbc | 40 | #include <net/net_namespace.h> |
1da177e4 LT |
41 | #include <net/ip_vs.h> |
42 | ||
43 | ||
6f7edb48 CB |
44 | #ifndef CONFIG_IP_VS_TAB_BITS |
45 | #define CONFIG_IP_VS_TAB_BITS 12 | |
46 | #endif | |
47 | ||
48 | /* | |
49 | * Connection hash size. Default is what was selected at compile time. | |
50 | */ | |
4ecd2944 | 51 | static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS; |
6f7edb48 CB |
52 | module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444); |
53 | MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size"); | |
54 | ||
55 | /* size and mask values */ | |
4ecd2944 ED |
56 | int ip_vs_conn_tab_size __read_mostly; |
57 | static int ip_vs_conn_tab_mask __read_mostly; | |
6f7edb48 | 58 | |
1da177e4 LT |
59 | /* |
60 | * Connection hash table: for input and output packets lookups of IPVS | |
61 | */ | |
731109e7 | 62 | static struct hlist_head *ip_vs_conn_tab __read_mostly; |
1da177e4 LT |
63 | |
64 | /* SLAB cache for IPVS connections */ | |
e18b890b | 65 | static struct kmem_cache *ip_vs_conn_cachep __read_mostly; |
1da177e4 | 66 | |
1da177e4 LT |
67 | /* counter for no client port connections */ |
68 | static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0); | |
69 | ||
70 | /* random value for IPVS connection hash */ | |
4ecd2944 | 71 | static unsigned int ip_vs_conn_rnd __read_mostly; |
1da177e4 LT |
72 | |
73 | /* | |
74 | * Fine locking granularity for big connection hash table | |
75 | */ | |
6e67e586 | 76 | #define CT_LOCKARRAY_BITS 5 |
1da177e4 LT |
77 | #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS) |
78 | #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1) | |
79 | ||
80 | struct ip_vs_aligned_lock | |
81 | { | |
088339a5 | 82 | spinlock_t l; |
1da177e4 LT |
83 | } __attribute__((__aligned__(SMP_CACHE_BYTES))); |
84 | ||
85 | /* lock array for conn table */ | |
86 | static struct ip_vs_aligned_lock | |
87 | __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned; | |
88 | ||
ac69269a | 89 | static inline void ct_write_lock_bh(unsigned int key) |
1da177e4 | 90 | { |
ac69269a | 91 | spin_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
1da177e4 LT |
92 | } |
93 | ||
ac69269a | 94 | static inline void ct_write_unlock_bh(unsigned int key) |
1da177e4 | 95 | { |
ac69269a | 96 | spin_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l); |
1da177e4 LT |
97 | } |
98 | ||
99 | ||
100 | /* | |
101 | * Returns hash value for IPVS connection entry | |
102 | */ | |
95c96174 | 103 | static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned int proto, |
28364a59 JV |
104 | const union nf_inet_addr *addr, |
105 | __be16 port) | |
1da177e4 | 106 | { |
28364a59 JV |
107 | #ifdef CONFIG_IP_VS_IPV6 |
108 | if (af == AF_INET6) | |
6e67e586 HS |
109 | return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd), |
110 | (__force u32)port, proto, ip_vs_conn_rnd) ^ | |
111 | ((size_t)net>>8)) & ip_vs_conn_tab_mask; | |
28364a59 | 112 | #endif |
6e67e586 HS |
113 | return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto, |
114 | ip_vs_conn_rnd) ^ | |
115 | ((size_t)net>>8)) & ip_vs_conn_tab_mask; | |
1da177e4 LT |
116 | } |
117 | ||
85999283 SH |
118 | static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p, |
119 | bool inverse) | |
120 | { | |
121 | const union nf_inet_addr *addr; | |
122 | __be16 port; | |
123 | ||
f71499aa | 124 | if (p->pe_data && p->pe->hashkey_raw) |
85999283 SH |
125 | return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) & |
126 | ip_vs_conn_tab_mask; | |
127 | ||
128 | if (likely(!inverse)) { | |
129 | addr = p->caddr; | |
130 | port = p->cport; | |
131 | } else { | |
132 | addr = p->vaddr; | |
133 | port = p->vport; | |
134 | } | |
135 | ||
6e67e586 | 136 | return ip_vs_conn_hashkey(p->net, p->af, p->protocol, addr, port); |
85999283 SH |
137 | } |
138 | ||
139 | static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp) | |
140 | { | |
141 | struct ip_vs_conn_param p; | |
142 | ||
6e67e586 HS |
143 | ip_vs_conn_fill_param(ip_vs_conn_net(cp), cp->af, cp->protocol, |
144 | &cp->caddr, cp->cport, NULL, 0, &p); | |
85999283 | 145 | |
e9e5eee8 SH |
146 | if (cp->pe) { |
147 | p.pe = cp->pe; | |
85999283 SH |
148 | p.pe_data = cp->pe_data; |
149 | p.pe_data_len = cp->pe_data_len; | |
150 | } | |
151 | ||
152 | return ip_vs_conn_hashkey_param(&p, false); | |
153 | } | |
1da177e4 LT |
154 | |
155 | /* | |
6e67e586 | 156 | * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port. |
1da177e4 LT |
157 | * returns bool success. |
158 | */ | |
159 | static inline int ip_vs_conn_hash(struct ip_vs_conn *cp) | |
160 | { | |
95c96174 | 161 | unsigned int hash; |
1da177e4 LT |
162 | int ret; |
163 | ||
26ec037f NC |
164 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
165 | return 0; | |
166 | ||
1da177e4 | 167 | /* Hash by protocol, client address and port */ |
85999283 | 168 | hash = ip_vs_conn_hashkey_conn(cp); |
1da177e4 | 169 | |
ac69269a | 170 | ct_write_lock_bh(hash); |
aea9d711 | 171 | spin_lock(&cp->lock); |
1da177e4 LT |
172 | |
173 | if (!(cp->flags & IP_VS_CONN_F_HASHED)) { | |
1da177e4 LT |
174 | cp->flags |= IP_VS_CONN_F_HASHED; |
175 | atomic_inc(&cp->refcnt); | |
088339a5 | 176 | hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]); |
1da177e4 LT |
177 | ret = 1; |
178 | } else { | |
1e3e238e HE |
179 | pr_err("%s(): request for already hashed, called from %pF\n", |
180 | __func__, __builtin_return_address(0)); | |
1da177e4 LT |
181 | ret = 0; |
182 | } | |
183 | ||
aea9d711 | 184 | spin_unlock(&cp->lock); |
ac69269a | 185 | ct_write_unlock_bh(hash); |
1da177e4 LT |
186 | |
187 | return ret; | |
188 | } | |
189 | ||
190 | ||
191 | /* | |
192 | * UNhashes ip_vs_conn from ip_vs_conn_tab. | |
088339a5 | 193 | * returns bool success. Caller should hold conn reference. |
1da177e4 LT |
194 | */ |
195 | static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp) | |
196 | { | |
95c96174 | 197 | unsigned int hash; |
1da177e4 LT |
198 | int ret; |
199 | ||
200 | /* unhash it and decrease its reference counter */ | |
85999283 | 201 | hash = ip_vs_conn_hashkey_conn(cp); |
1da177e4 | 202 | |
ac69269a | 203 | ct_write_lock_bh(hash); |
aea9d711 | 204 | spin_lock(&cp->lock); |
1da177e4 LT |
205 | |
206 | if (cp->flags & IP_VS_CONN_F_HASHED) { | |
088339a5 | 207 | hlist_del_rcu(&cp->c_list); |
1da177e4 LT |
208 | cp->flags &= ~IP_VS_CONN_F_HASHED; |
209 | atomic_dec(&cp->refcnt); | |
210 | ret = 1; | |
211 | } else | |
212 | ret = 0; | |
213 | ||
aea9d711 | 214 | spin_unlock(&cp->lock); |
ac69269a | 215 | ct_write_unlock_bh(hash); |
1da177e4 LT |
216 | |
217 | return ret; | |
218 | } | |
219 | ||
088339a5 JA |
220 | /* Try to unlink ip_vs_conn from ip_vs_conn_tab. |
221 | * returns bool success. | |
222 | */ | |
223 | static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp) | |
224 | { | |
225 | unsigned int hash; | |
226 | bool ret; | |
227 | ||
228 | hash = ip_vs_conn_hashkey_conn(cp); | |
229 | ||
ac69269a | 230 | ct_write_lock_bh(hash); |
088339a5 JA |
231 | spin_lock(&cp->lock); |
232 | ||
233 | if (cp->flags & IP_VS_CONN_F_HASHED) { | |
234 | ret = false; | |
235 | /* Decrease refcnt and unlink conn only if we are last user */ | |
236 | if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) { | |
237 | hlist_del_rcu(&cp->c_list); | |
238 | cp->flags &= ~IP_VS_CONN_F_HASHED; | |
239 | ret = true; | |
240 | } | |
241 | } else | |
242 | ret = atomic_read(&cp->refcnt) ? false : true; | |
243 | ||
244 | spin_unlock(&cp->lock); | |
ac69269a | 245 | ct_write_unlock_bh(hash); |
088339a5 JA |
246 | |
247 | return ret; | |
248 | } | |
249 | ||
1da177e4 LT |
250 | |
251 | /* | |
252 | * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. | |
253 | * Called for pkts coming from OUTside-to-INside. | |
f11017ec SH |
254 | * p->caddr, p->cport: pkt source address (foreign host) |
255 | * p->vaddr, p->vport: pkt dest address (load balancer) | |
1da177e4 | 256 | */ |
f11017ec SH |
257 | static inline struct ip_vs_conn * |
258 | __ip_vs_conn_in_get(const struct ip_vs_conn_param *p) | |
1da177e4 | 259 | { |
95c96174 | 260 | unsigned int hash; |
1da177e4 LT |
261 | struct ip_vs_conn *cp; |
262 | ||
85999283 | 263 | hash = ip_vs_conn_hashkey_param(p, false); |
1da177e4 | 264 | |
088339a5 | 265 | rcu_read_lock(); |
1da177e4 | 266 | |
088339a5 | 267 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) { |
1845ed0b JA |
268 | if (p->cport == cp->cport && p->vport == cp->vport && |
269 | cp->af == p->af && | |
f11017ec SH |
270 | ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && |
271 | ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && | |
f11017ec | 272 | ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) && |
6e67e586 HS |
273 | p->protocol == cp->protocol && |
274 | ip_vs_conn_net_eq(cp, p->net)) { | |
088339a5 JA |
275 | if (!__ip_vs_conn_get(cp)) |
276 | continue; | |
1da177e4 | 277 | /* HIT */ |
088339a5 | 278 | rcu_read_unlock(); |
1da177e4 LT |
279 | return cp; |
280 | } | |
281 | } | |
282 | ||
088339a5 | 283 | rcu_read_unlock(); |
1da177e4 LT |
284 | |
285 | return NULL; | |
286 | } | |
287 | ||
f11017ec | 288 | struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p) |
1da177e4 LT |
289 | { |
290 | struct ip_vs_conn *cp; | |
291 | ||
f11017ec SH |
292 | cp = __ip_vs_conn_in_get(p); |
293 | if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) { | |
294 | struct ip_vs_conn_param cport_zero_p = *p; | |
295 | cport_zero_p.cport = 0; | |
296 | cp = __ip_vs_conn_in_get(&cport_zero_p); | |
297 | } | |
1da177e4 | 298 | |
28364a59 | 299 | IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n", |
f11017ec SH |
300 | ip_vs_proto_name(p->protocol), |
301 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), | |
302 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), | |
28364a59 | 303 | cp ? "hit" : "not hit"); |
1da177e4 LT |
304 | |
305 | return cp; | |
306 | } | |
307 | ||
f11017ec SH |
308 | static int |
309 | ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb, | |
310 | const struct ip_vs_iphdr *iph, | |
d4383f04 | 311 | int inverse, struct ip_vs_conn_param *p) |
f11017ec SH |
312 | { |
313 | __be16 _ports[2], *pptr; | |
6e67e586 | 314 | struct net *net = skb_net(skb); |
f11017ec | 315 | |
d4383f04 | 316 | pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph); |
f11017ec SH |
317 | if (pptr == NULL) |
318 | return 1; | |
319 | ||
320 | if (likely(!inverse)) | |
6e67e586 HS |
321 | ip_vs_conn_fill_param(net, af, iph->protocol, &iph->saddr, |
322 | pptr[0], &iph->daddr, pptr[1], p); | |
f11017ec | 323 | else |
6e67e586 HS |
324 | ip_vs_conn_fill_param(net, af, iph->protocol, &iph->daddr, |
325 | pptr[1], &iph->saddr, pptr[0], p); | |
f11017ec SH |
326 | return 0; |
327 | } | |
328 | ||
5c0d2374 SH |
329 | struct ip_vs_conn * |
330 | ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb, | |
d4383f04 | 331 | const struct ip_vs_iphdr *iph, int inverse) |
5c0d2374 | 332 | { |
f11017ec | 333 | struct ip_vs_conn_param p; |
5c0d2374 | 334 | |
d4383f04 | 335 | if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p)) |
5c0d2374 SH |
336 | return NULL; |
337 | ||
f11017ec | 338 | return ip_vs_conn_in_get(&p); |
5c0d2374 SH |
339 | } |
340 | EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto); | |
341 | ||
87375ab4 | 342 | /* Get reference to connection template */ |
f11017ec | 343 | struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p) |
87375ab4 | 344 | { |
95c96174 | 345 | unsigned int hash; |
87375ab4 JA |
346 | struct ip_vs_conn *cp; |
347 | ||
85999283 | 348 | hash = ip_vs_conn_hashkey_param(p, false); |
87375ab4 | 349 | |
088339a5 | 350 | rcu_read_lock(); |
87375ab4 | 351 | |
088339a5 | 352 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) { |
1845ed0b JA |
353 | if (unlikely(p->pe_data && p->pe->ct_match)) { |
354 | if (!ip_vs_conn_net_eq(cp, p->net)) | |
355 | continue; | |
088339a5 JA |
356 | if (p->pe == cp->pe && p->pe->ct_match(p, cp)) { |
357 | if (__ip_vs_conn_get(cp)) | |
358 | goto out; | |
359 | } | |
85999283 SH |
360 | continue; |
361 | } | |
362 | ||
f11017ec SH |
363 | if (cp->af == p->af && |
364 | ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && | |
be8be9ec | 365 | /* protocol should only be IPPROTO_IP if |
f11017ec SH |
366 | * p->vaddr is a fwmark */ |
367 | ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : | |
368 | p->af, p->vaddr, &cp->vaddr) && | |
1845ed0b | 369 | p->vport == cp->vport && p->cport == cp->cport && |
87375ab4 | 370 | cp->flags & IP_VS_CONN_F_TEMPLATE && |
1845ed0b JA |
371 | p->protocol == cp->protocol && |
372 | ip_vs_conn_net_eq(cp, p->net)) { | |
088339a5 JA |
373 | if (__ip_vs_conn_get(cp)) |
374 | goto out; | |
375 | } | |
87375ab4 JA |
376 | } |
377 | cp = NULL; | |
378 | ||
379 | out: | |
088339a5 | 380 | rcu_read_unlock(); |
87375ab4 | 381 | |
28364a59 | 382 | IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n", |
f11017ec SH |
383 | ip_vs_proto_name(p->protocol), |
384 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), | |
385 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), | |
28364a59 | 386 | cp ? "hit" : "not hit"); |
87375ab4 JA |
387 | |
388 | return cp; | |
389 | } | |
1da177e4 | 390 | |
f11017ec SH |
391 | /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab. |
392 | * Called for pkts coming from inside-to-OUTside. | |
393 | * p->caddr, p->cport: pkt source address (inside host) | |
394 | * p->vaddr, p->vport: pkt dest address (foreign host) */ | |
395 | struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p) | |
1da177e4 | 396 | { |
95c96174 | 397 | unsigned int hash; |
1da177e4 LT |
398 | struct ip_vs_conn *cp, *ret=NULL; |
399 | ||
400 | /* | |
401 | * Check for "full" addressed entries | |
402 | */ | |
85999283 | 403 | hash = ip_vs_conn_hashkey_param(p, true); |
1da177e4 | 404 | |
088339a5 | 405 | rcu_read_lock(); |
1da177e4 | 406 | |
088339a5 | 407 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) { |
1845ed0b JA |
408 | if (p->vport == cp->cport && p->cport == cp->dport && |
409 | cp->af == p->af && | |
f11017ec SH |
410 | ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && |
411 | ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && | |
6e67e586 HS |
412 | p->protocol == cp->protocol && |
413 | ip_vs_conn_net_eq(cp, p->net)) { | |
088339a5 JA |
414 | if (!__ip_vs_conn_get(cp)) |
415 | continue; | |
1da177e4 | 416 | /* HIT */ |
1da177e4 LT |
417 | ret = cp; |
418 | break; | |
419 | } | |
420 | } | |
421 | ||
088339a5 | 422 | rcu_read_unlock(); |
1da177e4 | 423 | |
28364a59 | 424 | IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n", |
f11017ec SH |
425 | ip_vs_proto_name(p->protocol), |
426 | IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport), | |
427 | IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport), | |
28364a59 | 428 | ret ? "hit" : "not hit"); |
1da177e4 LT |
429 | |
430 | return ret; | |
431 | } | |
432 | ||
5c0d2374 SH |
433 | struct ip_vs_conn * |
434 | ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb, | |
d4383f04 | 435 | const struct ip_vs_iphdr *iph, int inverse) |
5c0d2374 | 436 | { |
f11017ec | 437 | struct ip_vs_conn_param p; |
5c0d2374 | 438 | |
d4383f04 | 439 | if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p)) |
5c0d2374 SH |
440 | return NULL; |
441 | ||
f11017ec | 442 | return ip_vs_conn_out_get(&p); |
5c0d2374 SH |
443 | } |
444 | EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto); | |
1da177e4 LT |
445 | |
446 | /* | |
447 | * Put back the conn and restart its timer with its timeout | |
448 | */ | |
449 | void ip_vs_conn_put(struct ip_vs_conn *cp) | |
450 | { | |
26ec037f NC |
451 | unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ? |
452 | 0 : cp->timeout; | |
453 | mod_timer(&cp->timer, jiffies+t); | |
1da177e4 LT |
454 | |
455 | __ip_vs_conn_put(cp); | |
456 | } | |
457 | ||
458 | ||
459 | /* | |
460 | * Fill a no_client_port connection with a client port number | |
461 | */ | |
014d730d | 462 | void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport) |
1da177e4 LT |
463 | { |
464 | if (ip_vs_conn_unhash(cp)) { | |
ac69269a | 465 | spin_lock_bh(&cp->lock); |
1da177e4 LT |
466 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) { |
467 | atomic_dec(&ip_vs_conn_no_cport_cnt); | |
468 | cp->flags &= ~IP_VS_CONN_F_NO_CPORT; | |
469 | cp->cport = cport; | |
470 | } | |
ac69269a | 471 | spin_unlock_bh(&cp->lock); |
1da177e4 LT |
472 | |
473 | /* hash on new dport */ | |
474 | ip_vs_conn_hash(cp); | |
475 | } | |
476 | } | |
477 | ||
478 | ||
479 | /* | |
480 | * Bind a connection entry with the corresponding packet_xmit. | |
481 | * Called by ip_vs_conn_new. | |
482 | */ | |
483 | static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp) | |
484 | { | |
485 | switch (IP_VS_FWD_METHOD(cp)) { | |
486 | case IP_VS_CONN_F_MASQ: | |
487 | cp->packet_xmit = ip_vs_nat_xmit; | |
488 | break; | |
489 | ||
490 | case IP_VS_CONN_F_TUNNEL: | |
491 | cp->packet_xmit = ip_vs_tunnel_xmit; | |
492 | break; | |
493 | ||
494 | case IP_VS_CONN_F_DROUTE: | |
495 | cp->packet_xmit = ip_vs_dr_xmit; | |
496 | break; | |
497 | ||
498 | case IP_VS_CONN_F_LOCALNODE: | |
499 | cp->packet_xmit = ip_vs_null_xmit; | |
500 | break; | |
501 | ||
502 | case IP_VS_CONN_F_BYPASS: | |
503 | cp->packet_xmit = ip_vs_bypass_xmit; | |
504 | break; | |
505 | } | |
506 | } | |
507 | ||
b3cdd2a7 JV |
508 | #ifdef CONFIG_IP_VS_IPV6 |
509 | static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp) | |
510 | { | |
511 | switch (IP_VS_FWD_METHOD(cp)) { | |
512 | case IP_VS_CONN_F_MASQ: | |
513 | cp->packet_xmit = ip_vs_nat_xmit_v6; | |
514 | break; | |
515 | ||
516 | case IP_VS_CONN_F_TUNNEL: | |
517 | cp->packet_xmit = ip_vs_tunnel_xmit_v6; | |
518 | break; | |
519 | ||
520 | case IP_VS_CONN_F_DROUTE: | |
521 | cp->packet_xmit = ip_vs_dr_xmit_v6; | |
522 | break; | |
523 | ||
524 | case IP_VS_CONN_F_LOCALNODE: | |
525 | cp->packet_xmit = ip_vs_null_xmit; | |
526 | break; | |
527 | ||
528 | case IP_VS_CONN_F_BYPASS: | |
529 | cp->packet_xmit = ip_vs_bypass_xmit_v6; | |
530 | break; | |
531 | } | |
532 | } | |
533 | #endif | |
534 | ||
1da177e4 LT |
535 | |
536 | static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest) | |
537 | { | |
538 | return atomic_read(&dest->activeconns) | |
539 | + atomic_read(&dest->inactconns); | |
540 | } | |
541 | ||
542 | /* | |
543 | * Bind a connection entry with a virtual service destination | |
544 | * Called just after a new connection entry is created. | |
545 | */ | |
546 | static inline void | |
547 | ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) | |
548 | { | |
3575792e | 549 | unsigned int conn_flags; |
6b324dbf | 550 | __u32 flags; |
3575792e | 551 | |
1da177e4 LT |
552 | /* if dest is NULL, then return directly */ |
553 | if (!dest) | |
554 | return; | |
555 | ||
556 | /* Increase the refcnt counter of the dest */ | |
fca9c20a | 557 | ip_vs_dest_hold(dest); |
1da177e4 | 558 | |
3575792e JA |
559 | conn_flags = atomic_read(&dest->conn_flags); |
560 | if (cp->protocol != IPPROTO_UDP) | |
561 | conn_flags &= ~IP_VS_CONN_F_ONE_PACKET; | |
6b324dbf | 562 | flags = cp->flags; |
1da177e4 | 563 | /* Bind with the destination and its corresponding transmitter */ |
6b324dbf | 564 | if (flags & IP_VS_CONN_F_SYNC) { |
b209639e RB |
565 | /* if the connection is not template and is created |
566 | * by sync, preserve the activity flag. | |
567 | */ | |
6b324dbf | 568 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) |
3575792e | 569 | conn_flags &= ~IP_VS_CONN_F_INACTIVE; |
3233759b | 570 | /* connections inherit forwarding method from dest */ |
6b324dbf | 571 | flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT); |
3575792e | 572 | } |
6b324dbf PNA |
573 | flags |= conn_flags; |
574 | cp->flags = flags; | |
1da177e4 LT |
575 | cp->dest = dest; |
576 | ||
cfc78c5a JV |
577 | IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d " |
578 | "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " | |
579 | "dest->refcnt:%d\n", | |
580 | ip_vs_proto_name(cp->protocol), | |
581 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), | |
582 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), | |
583 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), | |
584 | ip_vs_fwd_tag(cp), cp->state, | |
585 | cp->flags, atomic_read(&cp->refcnt), | |
586 | atomic_read(&dest->refcnt)); | |
1da177e4 LT |
587 | |
588 | /* Update the connection counters */ | |
6b324dbf | 589 | if (!(flags & IP_VS_CONN_F_TEMPLATE)) { |
06611f82 JA |
590 | /* It is a normal connection, so modify the counters |
591 | * according to the flags, later the protocol can | |
592 | * update them on state change | |
593 | */ | |
6b324dbf | 594 | if (!(flags & IP_VS_CONN_F_INACTIVE)) |
b209639e RB |
595 | atomic_inc(&dest->activeconns); |
596 | else | |
597 | atomic_inc(&dest->inactconns); | |
1da177e4 LT |
598 | } else { |
599 | /* It is a persistent connection/template, so increase | |
25985edc | 600 | the persistent connection counter */ |
1da177e4 LT |
601 | atomic_inc(&dest->persistconns); |
602 | } | |
603 | ||
604 | if (dest->u_threshold != 0 && | |
605 | ip_vs_dest_totalconns(dest) >= dest->u_threshold) | |
606 | dest->flags |= IP_VS_DEST_F_OVERLOAD; | |
607 | } | |
608 | ||
609 | ||
1e356f9c RB |
610 | /* |
611 | * Check if there is a destination for the connection, if so | |
612 | * bind the connection to the destination. | |
613 | */ | |
413c2d04 | 614 | void ip_vs_try_bind_dest(struct ip_vs_conn *cp) |
1e356f9c RB |
615 | { |
616 | struct ip_vs_dest *dest; | |
617 | ||
413c2d04 | 618 | rcu_read_lock(); |
655eef10 AG |
619 | |
620 | /* This function is only invoked by the synchronization code. We do | |
621 | * not currently support heterogeneous pools with synchronization, | |
622 | * so we can make the assumption that the svc_af is the same as the | |
623 | * dest_af | |
624 | */ | |
625 | dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, cp->af, &cp->daddr, | |
882a844b JA |
626 | cp->dport, &cp->vaddr, cp->vport, |
627 | cp->protocol, cp->fwmark, cp->flags); | |
628 | if (dest) { | |
629 | struct ip_vs_proto_data *pd; | |
630 | ||
ac69269a | 631 | spin_lock_bh(&cp->lock); |
f73181c8 | 632 | if (cp->dest) { |
ac69269a | 633 | spin_unlock_bh(&cp->lock); |
413c2d04 JA |
634 | rcu_read_unlock(); |
635 | return; | |
f73181c8 PNA |
636 | } |
637 | ||
882a844b JA |
638 | /* Applications work depending on the forwarding method |
639 | * but better to reassign them always when binding dest */ | |
640 | if (cp->app) | |
641 | ip_vs_unbind_app(cp); | |
642 | ||
1e356f9c | 643 | ip_vs_bind_dest(cp, dest); |
ac69269a | 644 | spin_unlock_bh(&cp->lock); |
882a844b JA |
645 | |
646 | /* Update its packet transmitter */ | |
647 | cp->packet_xmit = NULL; | |
648 | #ifdef CONFIG_IP_VS_IPV6 | |
649 | if (cp->af == AF_INET6) | |
650 | ip_vs_bind_xmit_v6(cp); | |
651 | else | |
652 | #endif | |
653 | ip_vs_bind_xmit(cp); | |
654 | ||
655 | pd = ip_vs_proto_data_get(ip_vs_conn_net(cp), cp->protocol); | |
656 | if (pd && atomic_read(&pd->appcnt)) | |
657 | ip_vs_bind_app(cp, pd->pp); | |
658 | } | |
413c2d04 | 659 | rcu_read_unlock(); |
1e356f9c | 660 | } |
1e356f9c RB |
661 | |
662 | ||
1da177e4 LT |
663 | /* |
664 | * Unbind a connection entry with its VS destination | |
665 | * Called by the ip_vs_conn_expire function. | |
666 | */ | |
667 | static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp) | |
668 | { | |
669 | struct ip_vs_dest *dest = cp->dest; | |
670 | ||
671 | if (!dest) | |
672 | return; | |
673 | ||
cfc78c5a JV |
674 | IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d " |
675 | "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d " | |
676 | "dest->refcnt:%d\n", | |
677 | ip_vs_proto_name(cp->protocol), | |
678 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport), | |
679 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport), | |
680 | IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport), | |
681 | ip_vs_fwd_tag(cp), cp->state, | |
682 | cp->flags, atomic_read(&cp->refcnt), | |
683 | atomic_read(&dest->refcnt)); | |
1da177e4 LT |
684 | |
685 | /* Update the connection counters */ | |
87375ab4 | 686 | if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) { |
1da177e4 LT |
687 | /* It is a normal connection, so decrease the inactconns |
688 | or activeconns counter */ | |
689 | if (cp->flags & IP_VS_CONN_F_INACTIVE) { | |
690 | atomic_dec(&dest->inactconns); | |
691 | } else { | |
692 | atomic_dec(&dest->activeconns); | |
693 | } | |
694 | } else { | |
695 | /* It is a persistent connection/template, so decrease | |
25985edc | 696 | the persistent connection counter */ |
1da177e4 LT |
697 | atomic_dec(&dest->persistconns); |
698 | } | |
699 | ||
700 | if (dest->l_threshold != 0) { | |
701 | if (ip_vs_dest_totalconns(dest) < dest->l_threshold) | |
702 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; | |
703 | } else if (dest->u_threshold != 0) { | |
704 | if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3) | |
705 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; | |
706 | } else { | |
707 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) | |
708 | dest->flags &= ~IP_VS_DEST_F_OVERLOAD; | |
709 | } | |
710 | ||
fca9c20a | 711 | ip_vs_dest_put(dest); |
1da177e4 LT |
712 | } |
713 | ||
8e1b0b1b SH |
714 | static int expire_quiescent_template(struct netns_ipvs *ipvs, |
715 | struct ip_vs_dest *dest) | |
716 | { | |
717 | #ifdef CONFIG_SYSCTL | |
718 | return ipvs->sysctl_expire_quiescent_template && | |
719 | (atomic_read(&dest->weight) == 0); | |
720 | #else | |
721 | return 0; | |
722 | #endif | |
723 | } | |
1da177e4 LT |
724 | |
725 | /* | |
726 | * Checking if the destination of a connection template is available. | |
727 | * If available, return 1, otherwise invalidate this connection | |
728 | * template and return 0. | |
729 | */ | |
730 | int ip_vs_check_template(struct ip_vs_conn *ct) | |
731 | { | |
732 | struct ip_vs_dest *dest = ct->dest; | |
a0840e2e | 733 | struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct)); |
1da177e4 LT |
734 | |
735 | /* | |
736 | * Checking the dest server status. | |
737 | */ | |
738 | if ((dest == NULL) || | |
e905a9ed | 739 | !(dest->flags & IP_VS_DEST_F_AVAILABLE) || |
8e1b0b1b | 740 | expire_quiescent_template(ipvs, dest)) { |
cfc78c5a JV |
741 | IP_VS_DBG_BUF(9, "check_template: dest not available for " |
742 | "protocol %s s:%s:%d v:%s:%d " | |
743 | "-> d:%s:%d\n", | |
744 | ip_vs_proto_name(ct->protocol), | |
745 | IP_VS_DBG_ADDR(ct->af, &ct->caddr), | |
746 | ntohs(ct->cport), | |
747 | IP_VS_DBG_ADDR(ct->af, &ct->vaddr), | |
748 | ntohs(ct->vport), | |
749 | IP_VS_DBG_ADDR(ct->af, &ct->daddr), | |
750 | ntohs(ct->dport)); | |
1da177e4 LT |
751 | |
752 | /* | |
753 | * Invalidate the connection template | |
754 | */ | |
014d730d | 755 | if (ct->vport != htons(0xffff)) { |
1da177e4 | 756 | if (ip_vs_conn_unhash(ct)) { |
014d730d AV |
757 | ct->dport = htons(0xffff); |
758 | ct->vport = htons(0xffff); | |
1da177e4 LT |
759 | ct->cport = 0; |
760 | ip_vs_conn_hash(ct); | |
761 | } | |
762 | } | |
763 | ||
764 | /* | |
765 | * Simply decrease the refcnt of the template, | |
766 | * don't restart its timer. | |
767 | */ | |
088339a5 | 768 | __ip_vs_conn_put(ct); |
1da177e4 LT |
769 | return 0; |
770 | } | |
771 | return 1; | |
772 | } | |
773 | ||
088339a5 JA |
774 | static void ip_vs_conn_rcu_free(struct rcu_head *head) |
775 | { | |
776 | struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn, | |
777 | rcu_head); | |
778 | ||
779 | ip_vs_pe_put(cp->pe); | |
780 | kfree(cp->pe_data); | |
781 | kmem_cache_free(ip_vs_conn_cachep, cp); | |
782 | } | |
783 | ||
1da177e4 LT |
784 | static void ip_vs_conn_expire(unsigned long data) |
785 | { | |
786 | struct ip_vs_conn *cp = (struct ip_vs_conn *)data; | |
749c42b6 JA |
787 | struct net *net = ip_vs_conn_net(cp); |
788 | struct netns_ipvs *ipvs = net_ipvs(net); | |
1da177e4 | 789 | |
1da177e4 LT |
790 | /* |
791 | * do I control anybody? | |
792 | */ | |
793 | if (atomic_read(&cp->n_control)) | |
794 | goto expire_later; | |
795 | ||
088339a5 JA |
796 | /* Unlink conn if not referenced anymore */ |
797 | if (likely(ip_vs_conn_unlink(cp))) { | |
1da177e4 | 798 | /* delete the timer if it is activated by other users */ |
25cc4ae9 | 799 | del_timer(&cp->timer); |
1da177e4 LT |
800 | |
801 | /* does anybody control me? */ | |
802 | if (cp->control) | |
803 | ip_vs_control_del(cp); | |
804 | ||
8f4e0a18 | 805 | if (cp->flags & IP_VS_CONN_F_NFCT) { |
8f4e0a18 HS |
806 | /* Do not access conntracks during subsys cleanup |
807 | * because nf_conntrack_find_get can not be used after | |
808 | * conntrack cleanup for the net. | |
809 | */ | |
810 | smp_rmb(); | |
811 | if (ipvs->enable) | |
812 | ip_vs_conn_drop_conntrack(cp); | |
813 | } | |
f4bc17cd | 814 | |
1da177e4 LT |
815 | if (unlikely(cp->app != NULL)) |
816 | ip_vs_unbind_app(cp); | |
817 | ip_vs_unbind_dest(cp); | |
818 | if (cp->flags & IP_VS_CONN_F_NO_CPORT) | |
819 | atomic_dec(&ip_vs_conn_no_cport_cnt); | |
088339a5 | 820 | call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free); |
6e67e586 | 821 | atomic_dec(&ipvs->conn_count); |
1da177e4 LT |
822 | return; |
823 | } | |
824 | ||
1da177e4 | 825 | expire_later: |
088339a5 JA |
826 | IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n", |
827 | atomic_read(&cp->refcnt), | |
1da177e4 LT |
828 | atomic_read(&cp->n_control)); |
829 | ||
088339a5 JA |
830 | atomic_inc(&cp->refcnt); |
831 | cp->timeout = 60*HZ; | |
832 | ||
749c42b6 JA |
833 | if (ipvs->sync_state & IP_VS_STATE_MASTER) |
834 | ip_vs_sync_conn(net, cp, sysctl_sync_threshold(ipvs)); | |
835 | ||
1da177e4 LT |
836 | ip_vs_conn_put(cp); |
837 | } | |
838 | ||
088339a5 JA |
839 | /* Modify timer, so that it expires as soon as possible. |
840 | * Can be called without reference only if under RCU lock. | |
841 | */ | |
1da177e4 LT |
842 | void ip_vs_conn_expire_now(struct ip_vs_conn *cp) |
843 | { | |
088339a5 JA |
844 | /* Using mod_timer_pending will ensure the timer is not |
845 | * modified after the final del_timer in ip_vs_conn_expire. | |
846 | */ | |
847 | if (timer_pending(&cp->timer) && | |
848 | time_after(cp->timer.expires, jiffies)) | |
849 | mod_timer_pending(&cp->timer, jiffies); | |
1da177e4 LT |
850 | } |
851 | ||
852 | ||
853 | /* | |
854 | * Create a new connection entry and hash it into the ip_vs_conn_tab | |
855 | */ | |
856 | struct ip_vs_conn * | |
ba38528a | 857 | ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af, |
95c96174 | 858 | const union nf_inet_addr *daddr, __be16 dport, unsigned int flags, |
0e051e68 | 859 | struct ip_vs_dest *dest, __u32 fwmark) |
1da177e4 LT |
860 | { |
861 | struct ip_vs_conn *cp; | |
6e67e586 HS |
862 | struct netns_ipvs *ipvs = net_ipvs(p->net); |
863 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net, | |
864 | p->protocol); | |
1da177e4 | 865 | |
9a05475c | 866 | cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC); |
1da177e4 | 867 | if (cp == NULL) { |
1e3e238e | 868 | IP_VS_ERR_RL("%s(): no memory\n", __func__); |
1da177e4 LT |
869 | return NULL; |
870 | } | |
871 | ||
731109e7 | 872 | INIT_HLIST_NODE(&cp->c_list); |
b24b8a24 | 873 | setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp); |
6e67e586 | 874 | ip_vs_conn_net_set(cp, p->net); |
f11017ec | 875 | cp->af = p->af; |
ba38528a | 876 | cp->daf = dest_af; |
f11017ec | 877 | cp->protocol = p->protocol; |
9a05475c | 878 | ip_vs_addr_set(p->af, &cp->caddr, p->caddr); |
f11017ec | 879 | cp->cport = p->cport; |
2a971354 | 880 | /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */ |
9a05475c | 881 | ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af, |
2a971354 MK |
882 | &cp->vaddr, p->vaddr); |
883 | cp->vport = p->vport; | |
ba38528a | 884 | ip_vs_addr_set(cp->daf, &cp->daddr, daddr); |
1da177e4 LT |
885 | cp->dport = dport; |
886 | cp->flags = flags; | |
0e051e68 | 887 | cp->fwmark = fwmark; |
e9e5eee8 SH |
888 | if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) { |
889 | ip_vs_pe_get(p->pe); | |
890 | cp->pe = p->pe; | |
85999283 SH |
891 | cp->pe_data = p->pe_data; |
892 | cp->pe_data_len = p->pe_data_len; | |
9a05475c JA |
893 | } else { |
894 | cp->pe = NULL; | |
895 | cp->pe_data = NULL; | |
896 | cp->pe_data_len = 0; | |
85999283 | 897 | } |
1da177e4 LT |
898 | spin_lock_init(&cp->lock); |
899 | ||
900 | /* | |
901 | * Set the entry is referenced by the current thread before hashing | |
902 | * it in the table, so that other thread run ip_vs_random_dropentry | |
903 | * but cannot drop this entry. | |
904 | */ | |
905 | atomic_set(&cp->refcnt, 1); | |
906 | ||
9a05475c | 907 | cp->control = NULL; |
1da177e4 LT |
908 | atomic_set(&cp->n_control, 0); |
909 | atomic_set(&cp->in_pkts, 0); | |
910 | ||
9a05475c JA |
911 | cp->packet_xmit = NULL; |
912 | cp->app = NULL; | |
913 | cp->app_data = NULL; | |
914 | /* reset struct ip_vs_seq */ | |
915 | cp->in_seq.delta = 0; | |
916 | cp->out_seq.delta = 0; | |
917 | ||
6e67e586 | 918 | atomic_inc(&ipvs->conn_count); |
1da177e4 LT |
919 | if (flags & IP_VS_CONN_F_NO_CPORT) |
920 | atomic_inc(&ip_vs_conn_no_cport_cnt); | |
921 | ||
922 | /* Bind the connection with a destination server */ | |
9a05475c | 923 | cp->dest = NULL; |
1da177e4 LT |
924 | ip_vs_bind_dest(cp, dest); |
925 | ||
926 | /* Set its state and timeout */ | |
927 | cp->state = 0; | |
9a05475c | 928 | cp->old_state = 0; |
1da177e4 | 929 | cp->timeout = 3*HZ; |
749c42b6 | 930 | cp->sync_endtime = jiffies & ~3UL; |
1da177e4 LT |
931 | |
932 | /* Bind its packet transmitter */ | |
b3cdd2a7 | 933 | #ifdef CONFIG_IP_VS_IPV6 |
f11017ec | 934 | if (p->af == AF_INET6) |
b3cdd2a7 JV |
935 | ip_vs_bind_xmit_v6(cp); |
936 | else | |
937 | #endif | |
938 | ip_vs_bind_xmit(cp); | |
1da177e4 | 939 | |
9bbac6a9 HS |
940 | if (unlikely(pd && atomic_read(&pd->appcnt))) |
941 | ip_vs_bind_app(cp, pd->pp); | |
1da177e4 | 942 | |
f4bc17cd JA |
943 | /* |
944 | * Allow conntrack to be preserved. By default, conntrack | |
945 | * is created and destroyed for every packet. | |
946 | * Sometimes keeping conntrack can be useful for | |
947 | * IP_VS_CONN_F_ONE_PACKET too. | |
948 | */ | |
949 | ||
a0840e2e | 950 | if (ip_vs_conntrack_enabled(ipvs)) |
f4bc17cd JA |
951 | cp->flags |= IP_VS_CONN_F_NFCT; |
952 | ||
1da177e4 LT |
953 | /* Hash it in the ip_vs_conn_tab finally */ |
954 | ip_vs_conn_hash(cp); | |
955 | ||
956 | return cp; | |
957 | } | |
958 | ||
1da177e4 LT |
959 | /* |
960 | * /proc/net/ip_vs_conn entries | |
961 | */ | |
962 | #ifdef CONFIG_PROC_FS | |
6e67e586 | 963 | struct ip_vs_iter_state { |
731109e7 CG |
964 | struct seq_net_private p; |
965 | struct hlist_head *l; | |
6e67e586 | 966 | }; |
1da177e4 LT |
967 | |
968 | static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos) | |
969 | { | |
970 | int idx; | |
971 | struct ip_vs_conn *cp; | |
6e67e586 | 972 | struct ip_vs_iter_state *iter = seq->private; |
e905a9ed | 973 | |
6f7edb48 | 974 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
088339a5 JA |
975 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) { |
976 | /* __ip_vs_conn_get() is not needed by | |
977 | * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show | |
978 | */ | |
1da177e4 | 979 | if (pos-- == 0) { |
6e67e586 | 980 | iter->l = &ip_vs_conn_tab[idx]; |
731109e7 | 981 | return cp; |
1da177e4 LT |
982 | } |
983 | } | |
a38e5e23 | 984 | cond_resched_rcu(); |
1da177e4 LT |
985 | } |
986 | ||
987 | return NULL; | |
988 | } | |
989 | ||
990 | static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos) | |
7cf2eb7b | 991 | __acquires(RCU) |
1da177e4 | 992 | { |
6e67e586 HS |
993 | struct ip_vs_iter_state *iter = seq->private; |
994 | ||
995 | iter->l = NULL; | |
7cf2eb7b | 996 | rcu_read_lock(); |
1da177e4 LT |
997 | return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN; |
998 | } | |
999 | ||
1000 | static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1001 | { | |
1002 | struct ip_vs_conn *cp = v; | |
6e67e586 | 1003 | struct ip_vs_iter_state *iter = seq->private; |
088339a5 | 1004 | struct hlist_node *e; |
731109e7 | 1005 | struct hlist_head *l = iter->l; |
1da177e4 LT |
1006 | int idx; |
1007 | ||
1008 | ++*pos; | |
e905a9ed | 1009 | if (v == SEQ_START_TOKEN) |
1da177e4 LT |
1010 | return ip_vs_conn_array(seq, 0); |
1011 | ||
1012 | /* more on same hash chain? */ | |
088339a5 JA |
1013 | e = rcu_dereference(hlist_next_rcu(&cp->c_list)); |
1014 | if (e) | |
1015 | return hlist_entry(e, struct ip_vs_conn, c_list); | |
1da177e4 LT |
1016 | |
1017 | idx = l - ip_vs_conn_tab; | |
6f7edb48 | 1018 | while (++idx < ip_vs_conn_tab_size) { |
088339a5 | 1019 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) { |
6e67e586 | 1020 | iter->l = &ip_vs_conn_tab[idx]; |
1da177e4 | 1021 | return cp; |
e905a9ed | 1022 | } |
a38e5e23 | 1023 | cond_resched_rcu(); |
1da177e4 | 1024 | } |
6e67e586 | 1025 | iter->l = NULL; |
1da177e4 LT |
1026 | return NULL; |
1027 | } | |
1028 | ||
1029 | static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v) | |
7cf2eb7b | 1030 | __releases(RCU) |
1da177e4 | 1031 | { |
7cf2eb7b | 1032 | rcu_read_unlock(); |
1da177e4 LT |
1033 | } |
1034 | ||
1035 | static int ip_vs_conn_seq_show(struct seq_file *seq, void *v) | |
1036 | { | |
1037 | ||
1038 | if (v == SEQ_START_TOKEN) | |
1039 | seq_puts(seq, | |
a3c918ac | 1040 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n"); |
1da177e4 LT |
1041 | else { |
1042 | const struct ip_vs_conn *cp = v; | |
6e67e586 | 1043 | struct net *net = seq_file_net(seq); |
a3c918ac SH |
1044 | char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3]; |
1045 | size_t len = 0; | |
1046 | ||
6e67e586 HS |
1047 | if (!ip_vs_conn_net_eq(cp, net)) |
1048 | return 0; | |
e9e5eee8 | 1049 | if (cp->pe_data) { |
a3c918ac | 1050 | pe_data[0] = ' '; |
e9e5eee8 SH |
1051 | len = strlen(cp->pe->name); |
1052 | memcpy(pe_data + 1, cp->pe->name, len); | |
a3c918ac SH |
1053 | pe_data[len + 1] = ' '; |
1054 | len += 2; | |
e9e5eee8 | 1055 | len += cp->pe->show_pe_data(cp, pe_data + len); |
a3c918ac SH |
1056 | } |
1057 | pe_data[len] = '\0'; | |
1da177e4 | 1058 | |
667a5f18 VB |
1059 | #ifdef CONFIG_IP_VS_IPV6 |
1060 | if (cp->af == AF_INET6) | |
a3c918ac SH |
1061 | seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X " |
1062 | "%pI6 %04X %-11s %7lu%s\n", | |
667a5f18 | 1063 | ip_vs_proto_name(cp->protocol), |
38ff4fa4 HH |
1064 | &cp->caddr.in6, ntohs(cp->cport), |
1065 | &cp->vaddr.in6, ntohs(cp->vport), | |
1066 | &cp->daddr.in6, ntohs(cp->dport), | |
667a5f18 | 1067 | ip_vs_state_name(cp->protocol, cp->state), |
a3c918ac | 1068 | (cp->timer.expires-jiffies)/HZ, pe_data); |
667a5f18 VB |
1069 | else |
1070 | #endif | |
1071 | seq_printf(seq, | |
1072 | "%-3s %08X %04X %08X %04X" | |
a3c918ac | 1073 | " %08X %04X %-11s %7lu%s\n", |
1da177e4 | 1074 | ip_vs_proto_name(cp->protocol), |
e7ade46a JV |
1075 | ntohl(cp->caddr.ip), ntohs(cp->cport), |
1076 | ntohl(cp->vaddr.ip), ntohs(cp->vport), | |
1077 | ntohl(cp->daddr.ip), ntohs(cp->dport), | |
1da177e4 | 1078 | ip_vs_state_name(cp->protocol, cp->state), |
a3c918ac | 1079 | (cp->timer.expires-jiffies)/HZ, pe_data); |
1da177e4 LT |
1080 | } |
1081 | return 0; | |
1082 | } | |
1083 | ||
56b3d975 | 1084 | static const struct seq_operations ip_vs_conn_seq_ops = { |
1da177e4 LT |
1085 | .start = ip_vs_conn_seq_start, |
1086 | .next = ip_vs_conn_seq_next, | |
1087 | .stop = ip_vs_conn_seq_stop, | |
1088 | .show = ip_vs_conn_seq_show, | |
1089 | }; | |
1090 | ||
1091 | static int ip_vs_conn_open(struct inode *inode, struct file *file) | |
1092 | { | |
6e67e586 HS |
1093 | return seq_open_net(inode, file, &ip_vs_conn_seq_ops, |
1094 | sizeof(struct ip_vs_iter_state)); | |
1da177e4 LT |
1095 | } |
1096 | ||
9a32144e | 1097 | static const struct file_operations ip_vs_conn_fops = { |
1da177e4 LT |
1098 | .owner = THIS_MODULE, |
1099 | .open = ip_vs_conn_open, | |
1100 | .read = seq_read, | |
1101 | .llseek = seq_lseek, | |
0f08190f | 1102 | .release = seq_release_net, |
1da177e4 | 1103 | }; |
7a4fbb1f | 1104 | |
95c96174 | 1105 | static const char *ip_vs_origin_name(unsigned int flags) |
7a4fbb1f RB |
1106 | { |
1107 | if (flags & IP_VS_CONN_F_SYNC) | |
1108 | return "SYNC"; | |
1109 | else | |
1110 | return "LOCAL"; | |
1111 | } | |
1112 | ||
1113 | static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v) | |
1114 | { | |
1115 | ||
1116 | if (v == SEQ_START_TOKEN) | |
1117 | seq_puts(seq, | |
1118 | "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n"); | |
1119 | else { | |
1120 | const struct ip_vs_conn *cp = v; | |
6e67e586 HS |
1121 | struct net *net = seq_file_net(seq); |
1122 | ||
1123 | if (!ip_vs_conn_net_eq(cp, net)) | |
1124 | return 0; | |
7a4fbb1f | 1125 | |
667a5f18 VB |
1126 | #ifdef CONFIG_IP_VS_IPV6 |
1127 | if (cp->af == AF_INET6) | |
5b095d98 | 1128 | seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n", |
667a5f18 | 1129 | ip_vs_proto_name(cp->protocol), |
38ff4fa4 HH |
1130 | &cp->caddr.in6, ntohs(cp->cport), |
1131 | &cp->vaddr.in6, ntohs(cp->vport), | |
1132 | &cp->daddr.in6, ntohs(cp->dport), | |
667a5f18 VB |
1133 | ip_vs_state_name(cp->protocol, cp->state), |
1134 | ip_vs_origin_name(cp->flags), | |
1135 | (cp->timer.expires-jiffies)/HZ); | |
1136 | else | |
1137 | #endif | |
1138 | seq_printf(seq, | |
1139 | "%-3s %08X %04X %08X %04X " | |
1140 | "%08X %04X %-11s %-6s %7lu\n", | |
7a4fbb1f | 1141 | ip_vs_proto_name(cp->protocol), |
e7ade46a JV |
1142 | ntohl(cp->caddr.ip), ntohs(cp->cport), |
1143 | ntohl(cp->vaddr.ip), ntohs(cp->vport), | |
1144 | ntohl(cp->daddr.ip), ntohs(cp->dport), | |
7a4fbb1f RB |
1145 | ip_vs_state_name(cp->protocol, cp->state), |
1146 | ip_vs_origin_name(cp->flags), | |
1147 | (cp->timer.expires-jiffies)/HZ); | |
1148 | } | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static const struct seq_operations ip_vs_conn_sync_seq_ops = { | |
1153 | .start = ip_vs_conn_seq_start, | |
1154 | .next = ip_vs_conn_seq_next, | |
1155 | .stop = ip_vs_conn_seq_stop, | |
1156 | .show = ip_vs_conn_sync_seq_show, | |
1157 | }; | |
1158 | ||
1159 | static int ip_vs_conn_sync_open(struct inode *inode, struct file *file) | |
1160 | { | |
6e67e586 HS |
1161 | return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops, |
1162 | sizeof(struct ip_vs_iter_state)); | |
7a4fbb1f RB |
1163 | } |
1164 | ||
1165 | static const struct file_operations ip_vs_conn_sync_fops = { | |
1166 | .owner = THIS_MODULE, | |
1167 | .open = ip_vs_conn_sync_open, | |
1168 | .read = seq_read, | |
1169 | .llseek = seq_lseek, | |
0f08190f | 1170 | .release = seq_release_net, |
7a4fbb1f RB |
1171 | }; |
1172 | ||
1da177e4 LT |
1173 | #endif |
1174 | ||
1175 | ||
1176 | /* | |
1177 | * Randomly drop connection entries before running out of memory | |
1178 | */ | |
1179 | static inline int todrop_entry(struct ip_vs_conn *cp) | |
1180 | { | |
1181 | /* | |
1182 | * The drop rate array needs tuning for real environments. | |
1183 | * Called from timer bh only => no locking | |
1184 | */ | |
9b5b5cff | 1185 | static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; |
1da177e4 LT |
1186 | static char todrop_counter[9] = {0}; |
1187 | int i; | |
1188 | ||
1189 | /* if the conn entry hasn't lasted for 60 seconds, don't drop it. | |
1190 | This will leave enough time for normal connection to get | |
1191 | through. */ | |
1192 | if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ)) | |
1193 | return 0; | |
1194 | ||
1195 | /* Don't drop the entry if its number of incoming packets is not | |
1196 | located in [0, 8] */ | |
1197 | i = atomic_read(&cp->in_pkts); | |
1198 | if (i > 8 || i < 0) return 0; | |
1199 | ||
1200 | if (!todrop_rate[i]) return 0; | |
1201 | if (--todrop_counter[i] > 0) return 0; | |
1202 | ||
1203 | todrop_counter[i] = todrop_rate[i]; | |
1204 | return 1; | |
1205 | } | |
1206 | ||
af9debd4 | 1207 | /* Called from keventd and must protect itself from softirqs */ |
f6340ee0 | 1208 | void ip_vs_random_dropentry(struct net *net) |
1da177e4 LT |
1209 | { |
1210 | int idx; | |
088339a5 | 1211 | struct ip_vs_conn *cp, *cp_c; |
1da177e4 | 1212 | |
a38e5e23 | 1213 | rcu_read_lock(); |
1da177e4 LT |
1214 | /* |
1215 | * Randomly scan 1/32 of the whole table every second | |
1216 | */ | |
6f7edb48 | 1217 | for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) { |
63862b5b | 1218 | unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask; |
1da177e4 | 1219 | |
088339a5 | 1220 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) { |
87375ab4 | 1221 | if (cp->flags & IP_VS_CONN_F_TEMPLATE) |
1da177e4 LT |
1222 | /* connection template */ |
1223 | continue; | |
f6340ee0 HS |
1224 | if (!ip_vs_conn_net_eq(cp, net)) |
1225 | continue; | |
1da177e4 LT |
1226 | if (cp->protocol == IPPROTO_TCP) { |
1227 | switch(cp->state) { | |
1228 | case IP_VS_TCP_S_SYN_RECV: | |
1229 | case IP_VS_TCP_S_SYNACK: | |
1230 | break; | |
1231 | ||
1232 | case IP_VS_TCP_S_ESTABLISHED: | |
1233 | if (todrop_entry(cp)) | |
1234 | break; | |
1235 | continue; | |
1236 | ||
1237 | default: | |
1238 | continue; | |
1239 | } | |
acaac5d8 JA |
1240 | } else if (cp->protocol == IPPROTO_SCTP) { |
1241 | switch (cp->state) { | |
1242 | case IP_VS_SCTP_S_INIT1: | |
1243 | case IP_VS_SCTP_S_INIT: | |
1244 | break; | |
1245 | case IP_VS_SCTP_S_ESTABLISHED: | |
1246 | if (todrop_entry(cp)) | |
1247 | break; | |
1248 | continue; | |
1249 | default: | |
1250 | continue; | |
1251 | } | |
1da177e4 LT |
1252 | } else { |
1253 | if (!todrop_entry(cp)) | |
1254 | continue; | |
1255 | } | |
1256 | ||
1da177e4 LT |
1257 | IP_VS_DBG(4, "del connection\n"); |
1258 | ip_vs_conn_expire_now(cp); | |
088339a5 JA |
1259 | cp_c = cp->control; |
1260 | /* cp->control is valid only with reference to cp */ | |
1261 | if (cp_c && __ip_vs_conn_get(cp)) { | |
1da177e4 | 1262 | IP_VS_DBG(4, "del conn template\n"); |
088339a5 JA |
1263 | ip_vs_conn_expire_now(cp_c); |
1264 | __ip_vs_conn_put(cp); | |
1da177e4 | 1265 | } |
1da177e4 | 1266 | } |
a38e5e23 | 1267 | cond_resched_rcu(); |
1da177e4 | 1268 | } |
a38e5e23 | 1269 | rcu_read_unlock(); |
1da177e4 LT |
1270 | } |
1271 | ||
1272 | ||
1273 | /* | |
1274 | * Flush all the connection entries in the ip_vs_conn_tab | |
1275 | */ | |
6e67e586 | 1276 | static void ip_vs_conn_flush(struct net *net) |
1da177e4 LT |
1277 | { |
1278 | int idx; | |
088339a5 | 1279 | struct ip_vs_conn *cp, *cp_c; |
6e67e586 | 1280 | struct netns_ipvs *ipvs = net_ipvs(net); |
1da177e4 | 1281 | |
a0840e2e | 1282 | flush_again: |
a38e5e23 | 1283 | rcu_read_lock(); |
6f7edb48 | 1284 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) { |
1da177e4 | 1285 | |
088339a5 | 1286 | hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) { |
6e67e586 HS |
1287 | if (!ip_vs_conn_net_eq(cp, net)) |
1288 | continue; | |
1da177e4 LT |
1289 | IP_VS_DBG(4, "del connection\n"); |
1290 | ip_vs_conn_expire_now(cp); | |
088339a5 JA |
1291 | cp_c = cp->control; |
1292 | /* cp->control is valid only with reference to cp */ | |
1293 | if (cp_c && __ip_vs_conn_get(cp)) { | |
1da177e4 | 1294 | IP_VS_DBG(4, "del conn template\n"); |
088339a5 JA |
1295 | ip_vs_conn_expire_now(cp_c); |
1296 | __ip_vs_conn_put(cp); | |
1da177e4 | 1297 | } |
1da177e4 | 1298 | } |
a38e5e23 | 1299 | cond_resched_rcu(); |
1da177e4 | 1300 | } |
a38e5e23 | 1301 | rcu_read_unlock(); |
1da177e4 LT |
1302 | |
1303 | /* the counter may be not NULL, because maybe some conn entries | |
1304 | are run by slow timer handler or unhashed but still referred */ | |
6e67e586 | 1305 | if (atomic_read(&ipvs->conn_count) != 0) { |
1da177e4 LT |
1306 | schedule(); |
1307 | goto flush_again; | |
1308 | } | |
1309 | } | |
61b1ab45 HS |
1310 | /* |
1311 | * per netns init and exit | |
1312 | */ | |
503cf15a | 1313 | int __net_init ip_vs_conn_net_init(struct net *net) |
61b1ab45 | 1314 | { |
6e67e586 HS |
1315 | struct netns_ipvs *ipvs = net_ipvs(net); |
1316 | ||
6e67e586 | 1317 | atomic_set(&ipvs->conn_count, 0); |
1da177e4 | 1318 | |
d4beaa66 G |
1319 | proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops); |
1320 | proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops); | |
61b1ab45 HS |
1321 | return 0; |
1322 | } | |
1323 | ||
503cf15a | 1324 | void __net_exit ip_vs_conn_net_cleanup(struct net *net) |
61b1ab45 | 1325 | { |
6e67e586 HS |
1326 | /* flush all the connection entries first */ |
1327 | ip_vs_conn_flush(net); | |
ece31ffd G |
1328 | remove_proc_entry("ip_vs_conn", net->proc_net); |
1329 | remove_proc_entry("ip_vs_conn_sync", net->proc_net); | |
61b1ab45 | 1330 | } |
1da177e4 | 1331 | |
048cf48b | 1332 | int __init ip_vs_conn_init(void) |
1da177e4 LT |
1333 | { |
1334 | int idx; | |
1335 | ||
6f7edb48 CB |
1336 | /* Compute size and mask */ |
1337 | ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits; | |
1338 | ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1; | |
1339 | ||
1da177e4 LT |
1340 | /* |
1341 | * Allocate the connection hash table and initialize its list heads | |
1342 | */ | |
731109e7 | 1343 | ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab)); |
1da177e4 LT |
1344 | if (!ip_vs_conn_tab) |
1345 | return -ENOMEM; | |
1346 | ||
1347 | /* Allocate ip_vs_conn slab cache */ | |
1348 | ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn", | |
1349 | sizeof(struct ip_vs_conn), 0, | |
20c2df83 | 1350 | SLAB_HWCACHE_ALIGN, NULL); |
1da177e4 LT |
1351 | if (!ip_vs_conn_cachep) { |
1352 | vfree(ip_vs_conn_tab); | |
1353 | return -ENOMEM; | |
1354 | } | |
1355 | ||
1e3e238e HE |
1356 | pr_info("Connection hash table configured " |
1357 | "(size=%d, memory=%ldKbytes)\n", | |
6f7edb48 CB |
1358 | ip_vs_conn_tab_size, |
1359 | (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024); | |
1da177e4 LT |
1360 | IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n", |
1361 | sizeof(struct ip_vs_conn)); | |
1362 | ||
731109e7 CG |
1363 | for (idx = 0; idx < ip_vs_conn_tab_size; idx++) |
1364 | INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]); | |
1da177e4 LT |
1365 | |
1366 | for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) { | |
088339a5 | 1367 | spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l); |
1da177e4 LT |
1368 | } |
1369 | ||
1da177e4 LT |
1370 | /* calculate the random value for connection hash */ |
1371 | get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd)); | |
1372 | ||
7a4f0761 | 1373 | return 0; |
1da177e4 LT |
1374 | } |
1375 | ||
1da177e4 LT |
1376 | void ip_vs_conn_cleanup(void) |
1377 | { | |
088339a5 JA |
1378 | /* Wait all ip_vs_conn_rcu_free() callbacks to complete */ |
1379 | rcu_barrier(); | |
1da177e4 LT |
1380 | /* Release the empty cache */ |
1381 | kmem_cache_destroy(ip_vs_conn_cachep); | |
1da177e4 LT |
1382 | vfree(ip_vs_conn_tab); |
1383 | } |