Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 1 | #ifndef _IP_SET_TIMEOUT_H |
| 2 | #define _IP_SET_TIMEOUT_H |
| 3 | |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame] | 4 | /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 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 | |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame] | 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 */ |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 24 | #define IPSET_NO_TIMEOUT UINT_MAX |
| 25 | |
Jozsef Kadlecsik | ca134ce | 2013-09-07 00:10:07 +0200 | [diff] [blame] | 26 | #define ip_set_adt_opt_timeout(opt, set) \ |
| 27 | ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout) |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 28 | |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 29 | static inline unsigned int |
| 30 | ip_set_timeout_uget(struct nlattr *tb) |
| 31 | { |
| 32 | unsigned int timeout = ip_set_get_h32(tb); |
| 33 | |
Jozsef Kadlecsik | 127f559 | 2012-05-07 02:35:44 +0000 | [diff] [blame] | 34 | /* Normalize to fit into jiffies */ |
| 35 | if (timeout > UINT_MAX/MSEC_PER_SEC) |
| 36 | timeout = UINT_MAX/MSEC_PER_SEC; |
| 37 | |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 38 | /* Userspace supplied TIMEOUT parameter: adjust crazy size */ |
| 39 | return timeout == IPSET_NO_TIMEOUT ? IPSET_NO_TIMEOUT - 1 : timeout; |
| 40 | } |
| 41 | |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 42 | static inline bool |
Jozsef Kadlecsik | b57b2d1 | 2015-06-13 14:22:25 +0200 | [diff] [blame] | 43 | ip_set_timeout_expired(unsigned long *t) |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 44 | { |
Jozsef Kadlecsik | b57b2d1 | 2015-06-13 14:22:25 +0200 | [diff] [blame] | 45 | return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t); |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame] | 48 | static inline void |
Jozsef Kadlecsik | b57b2d1 | 2015-06-13 14:22:25 +0200 | [diff] [blame] | 49 | ip_set_timeout_set(unsigned long *timeout, u32 value) |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 50 | { |
Jozsef Kadlecsik | b57b2d1 | 2015-06-13 14:22:25 +0200 | [diff] [blame] | 51 | unsigned long t; |
| 52 | |
| 53 | if (!value) { |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame] | 54 | *timeout = IPSET_ELEM_PERMANENT; |
| 55 | return; |
| 56 | } |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 57 | |
Jozsef Kadlecsik | b57b2d1 | 2015-06-13 14:22:25 +0200 | [diff] [blame] | 58 | t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies; |
| 59 | if (t == IPSET_ELEM_PERMANENT) |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 60 | /* Bingo! :-) */ |
Jozsef Kadlecsik | b57b2d1 | 2015-06-13 14:22:25 +0200 | [diff] [blame] | 61 | t--; |
| 62 | *timeout = t; |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static inline u32 |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame] | 66 | ip_set_timeout_get(unsigned long *timeout) |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 67 | { |
Jozsef Kadlecsik | 6e02c06 | 2018-05-31 18:45:21 +0200 | [diff] [blame] | 68 | u32 t; |
| 69 | |
| 70 | if (*timeout == IPSET_ELEM_PERMANENT) |
| 71 | return 0; |
| 72 | |
| 73 | t = jiffies_to_msecs(*timeout - jiffies)/MSEC_PER_SEC; |
| 74 | /* Zero value in userspace means no timeout */ |
| 75 | return t == 0 ? 1 : t; |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 76 | } |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 77 | |
| 78 | #endif /* __KERNEL__ */ |
Jozsef Kadlecsik | 72205fc | 2011-02-01 15:33:17 +0100 | [diff] [blame] | 79 | #endif /* _IP_SET_TIMEOUT_H */ |