1 #ifndef _IP_SET_TIMEOUT_H
2 #define _IP_SET_TIMEOUT_H
3 
4 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #ifdef __KERNEL__
12 
13 /* How often should the gc be run by default */
14 #define IPSET_GC_TIME			(3 * 60)
15 
16 /* Timeout period depending on the timeout value of the given set */
17 #define IPSET_GC_PERIOD(timeout) \
18 	((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
19 
20 /* Entry is set with no timeout value */
21 #define IPSET_ELEM_PERMANENT	0
22 
23 /* Set is defined with timeout support: timeout value may be 0 */
24 #define IPSET_NO_TIMEOUT	UINT_MAX
25 
26 /* Max timeout value, see msecs_to_jiffies() in jiffies.h */
27 #define IPSET_MAX_TIMEOUT	(UINT_MAX >> 1)/MSEC_PER_SEC
28 
29 #define ip_set_adt_opt_timeout(opt, set)	\
30 ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
31 
32 static inline unsigned int
ip_set_timeout_uget(struct nlattr * tb)33 ip_set_timeout_uget(struct nlattr *tb)
34 {
35 	unsigned int timeout = ip_set_get_h32(tb);
36 
37 	/* Normalize to fit into jiffies */
38 	if (timeout > IPSET_MAX_TIMEOUT)
39 		timeout = IPSET_MAX_TIMEOUT;
40 
41 	return timeout;
42 }
43 
44 static inline bool
ip_set_timeout_expired(const unsigned long * t)45 ip_set_timeout_expired(const unsigned long *t)
46 {
47 	return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
48 }
49 
50 static inline void
ip_set_timeout_set(unsigned long * timeout,u32 value)51 ip_set_timeout_set(unsigned long *timeout, u32 value)
52 {
53 	unsigned long t;
54 
55 	if (!value) {
56 		*timeout = IPSET_ELEM_PERMANENT;
57 		return;
58 	}
59 
60 	t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
61 	if (t == IPSET_ELEM_PERMANENT)
62 		/* Bingo! :-) */
63 		t--;
64 	*timeout = t;
65 }
66 
67 static inline u32
ip_set_timeout_get(const unsigned long * timeout)68 ip_set_timeout_get(const unsigned long *timeout)
69 {
70 	u32 t;
71 
72 	if (*timeout == IPSET_ELEM_PERMANENT)
73 		return 0;
74 
75 	t = jiffies_to_msecs(*timeout - jiffies)/MSEC_PER_SEC;
76 	/* Zero value in userspace means no timeout */
77 	return t == 0 ? 1 : t;
78 }
79 
80 #endif	/* __KERNEL__ */
81 #endif /* _IP_SET_TIMEOUT_H */
82