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