Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/netfilter/xt_IDLETIMER.c |
| 3 | * |
| 4 | * Netfilter module to trigger a timer when packet matches. |
| 5 | * After timer expires a kevent will be sent. |
| 6 | * |
| 7 | * Copyright (C) 2004, 2010 Nokia Corporation |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 8 | * |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 9 | * Written by Timo Teras <ext-timo.teras@nokia.com> |
| 10 | * |
| 11 | * Converted to x_tables and reworked for upstream inclusion |
| 12 | * by Luciano Coelho <luciano.coelho@nokia.com> |
| 13 | * |
| 14 | * Contact: Luciano Coelho <luciano.coelho@nokia.com> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * version 2 as published by the Free Software Foundation. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, but |
| 21 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | * General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 28 | * 02110-1301 USA |
| 29 | */ |
| 30 | |
| 31 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 32 | |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/timer.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mutex.h> |
| 37 | #include <linux/netfilter.h> |
| 38 | #include <linux/netfilter/x_tables.h> |
| 39 | #include <linux/netfilter/xt_IDLETIMER.h> |
Randy Dunlap | 600069d | 2010-06-22 08:13:31 +0200 | [diff] [blame] | 40 | #include <linux/kdev_t.h> |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 41 | #include <linux/kobject.h> |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 42 | #include <linux/skbuff.h> |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 43 | #include <linux/workqueue.h> |
| 44 | #include <linux/sysfs.h> |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 45 | #include <linux/rtc.h> |
| 46 | #include <linux/time.h> |
| 47 | #include <linux/math64.h> |
| 48 | #include <linux/suspend.h> |
| 49 | #include <linux/notifier.h> |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 50 | #include <net/net_namespace.h> |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 51 | #include <net/sock.h> |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 52 | |
| 53 | struct idletimer_tg_attr { |
| 54 | struct attribute attr; |
| 55 | ssize_t (*show)(struct kobject *kobj, |
| 56 | struct attribute *attr, char *buf); |
| 57 | }; |
| 58 | |
| 59 | struct idletimer_tg { |
| 60 | struct list_head entry; |
| 61 | struct timer_list timer; |
| 62 | struct work_struct work; |
| 63 | |
| 64 | struct kobject *kobj; |
| 65 | struct idletimer_tg_attr attr; |
| 66 | |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 67 | struct timespec delayed_timer_trigger; |
| 68 | struct timespec last_modified_timer; |
| 69 | struct timespec last_suspend_time; |
| 70 | struct notifier_block pm_nb; |
| 71 | |
| 72 | int timeout; |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 73 | unsigned int refcnt; |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 74 | bool work_pending; |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 75 | bool send_nl_msg; |
| 76 | bool active; |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 77 | uid_t uid; |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static LIST_HEAD(idletimer_tg_list); |
| 81 | static DEFINE_MUTEX(list_mutex); |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 82 | static DEFINE_SPINLOCK(timestamp_lock); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 83 | |
| 84 | static struct kobject *idletimer_tg_kobj; |
| 85 | |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 86 | static bool check_for_delayed_trigger(struct idletimer_tg *timer, |
| 87 | struct timespec *ts) |
| 88 | { |
| 89 | bool state; |
| 90 | struct timespec temp; |
| 91 | spin_lock_bh(×tamp_lock); |
| 92 | timer->work_pending = false; |
| 93 | if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout || |
| 94 | timer->delayed_timer_trigger.tv_sec != 0) { |
| 95 | state = false; |
| 96 | temp.tv_sec = timer->timeout; |
| 97 | temp.tv_nsec = 0; |
| 98 | if (timer->delayed_timer_trigger.tv_sec != 0) { |
| 99 | temp = timespec_add(timer->delayed_timer_trigger, temp); |
| 100 | ts->tv_sec = temp.tv_sec; |
| 101 | ts->tv_nsec = temp.tv_nsec; |
| 102 | timer->delayed_timer_trigger.tv_sec = 0; |
| 103 | timer->work_pending = true; |
| 104 | schedule_work(&timer->work); |
| 105 | } else { |
| 106 | temp = timespec_add(timer->last_modified_timer, temp); |
| 107 | ts->tv_sec = temp.tv_sec; |
| 108 | ts->tv_nsec = temp.tv_nsec; |
| 109 | } |
| 110 | } else { |
| 111 | state = timer->active; |
| 112 | } |
| 113 | spin_unlock_bh(×tamp_lock); |
| 114 | return state; |
| 115 | } |
| 116 | |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 117 | static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer) |
| 118 | { |
| 119 | char iface_msg[NLMSG_MAX_SIZE]; |
| 120 | char state_msg[NLMSG_MAX_SIZE]; |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 121 | char timestamp_msg[NLMSG_MAX_SIZE]; |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 122 | char uid_msg[NLMSG_MAX_SIZE]; |
| 123 | char *envp[] = { iface_msg, state_msg, timestamp_msg, uid_msg, NULL }; |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 124 | int res; |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 125 | struct timespec ts; |
| 126 | uint64_t time_ns; |
| 127 | bool state; |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 128 | |
| 129 | res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s", |
| 130 | iface); |
| 131 | if (NLMSG_MAX_SIZE <= res) { |
| 132 | pr_err("message too long (%d)", res); |
| 133 | return; |
| 134 | } |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 135 | |
| 136 | get_monotonic_boottime(&ts); |
| 137 | state = check_for_delayed_trigger(timer, &ts); |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 138 | res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s", |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 139 | state ? "active" : "inactive"); |
| 140 | |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 141 | if (NLMSG_MAX_SIZE <= res) { |
| 142 | pr_err("message too long (%d)", res); |
| 143 | return; |
| 144 | } |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 145 | |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 146 | if (state) { |
| 147 | res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=%u", timer->uid); |
| 148 | if (NLMSG_MAX_SIZE <= res) |
| 149 | pr_err("message too long (%d)", res); |
| 150 | } else { |
| 151 | res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID="); |
| 152 | if (NLMSG_MAX_SIZE <= res) |
| 153 | pr_err("message too long (%d)", res); |
| 154 | } |
| 155 | |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 156 | time_ns = timespec_to_ns(&ts); |
| 157 | res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns); |
| 158 | if (NLMSG_MAX_SIZE <= res) { |
| 159 | timestamp_msg[0] = '\0'; |
| 160 | pr_err("message too long (%d)", res); |
| 161 | } |
| 162 | |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 163 | pr_debug("putting nlmsg: <%s> <%s> <%s> <%s>\n", iface_msg, state_msg, |
| 164 | timestamp_msg, uid_msg); |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 165 | kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp); |
| 166 | return; |
| 167 | |
| 168 | |
| 169 | } |
| 170 | |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 171 | static |
| 172 | struct idletimer_tg *__idletimer_tg_find_by_label(const char *label) |
| 173 | { |
| 174 | struct idletimer_tg *entry; |
| 175 | |
| 176 | BUG_ON(!label); |
| 177 | |
| 178 | list_for_each_entry(entry, &idletimer_tg_list, entry) { |
| 179 | if (!strcmp(label, entry->attr.attr.name)) |
| 180 | return entry; |
| 181 | } |
| 182 | |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr, |
| 187 | char *buf) |
| 188 | { |
| 189 | struct idletimer_tg *timer; |
| 190 | unsigned long expires = 0; |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 191 | unsigned long now = jiffies; |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 192 | |
| 193 | mutex_lock(&list_mutex); |
| 194 | |
| 195 | timer = __idletimer_tg_find_by_label(attr->name); |
| 196 | if (timer) |
| 197 | expires = timer->timer.expires; |
| 198 | |
| 199 | mutex_unlock(&list_mutex); |
| 200 | |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 201 | if (time_after(expires, now)) |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 202 | return sprintf(buf, "%u\n", |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 203 | jiffies_to_msecs(expires - now) / 1000); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 204 | |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 205 | if (timer->send_nl_msg) |
| 206 | return sprintf(buf, "0 %d\n", |
| 207 | jiffies_to_msecs(now - expires) / 1000); |
| 208 | else |
| 209 | return sprintf(buf, "0\n"); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void idletimer_tg_work(struct work_struct *work) |
| 213 | { |
| 214 | struct idletimer_tg *timer = container_of(work, struct idletimer_tg, |
| 215 | work); |
| 216 | |
| 217 | sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name); |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 218 | |
| 219 | if (timer->send_nl_msg) |
| 220 | notify_netlink_uevent(timer->attr.attr.name, timer); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void idletimer_tg_expired(unsigned long data) |
| 224 | { |
| 225 | struct idletimer_tg *timer = (struct idletimer_tg *) data; |
| 226 | |
| 227 | pr_debug("timer %s expired\n", timer->attr.attr.name); |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 228 | spin_lock_bh(×tamp_lock); |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 229 | timer->active = false; |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 230 | timer->work_pending = true; |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 231 | schedule_work(&timer->work); |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 232 | spin_unlock_bh(×tamp_lock); |
| 233 | } |
| 234 | |
| 235 | static int idletimer_resume(struct notifier_block *notifier, |
| 236 | unsigned long pm_event, void *unused) |
| 237 | { |
| 238 | struct timespec ts; |
| 239 | unsigned long time_diff, now = jiffies; |
| 240 | struct idletimer_tg *timer = container_of(notifier, |
| 241 | struct idletimer_tg, pm_nb); |
| 242 | if (!timer) |
| 243 | return NOTIFY_DONE; |
| 244 | switch (pm_event) { |
| 245 | case PM_SUSPEND_PREPARE: |
| 246 | get_monotonic_boottime(&timer->last_suspend_time); |
| 247 | break; |
| 248 | case PM_POST_SUSPEND: |
| 249 | spin_lock_bh(×tamp_lock); |
| 250 | if (!timer->active) { |
| 251 | spin_unlock_bh(×tamp_lock); |
| 252 | break; |
| 253 | } |
| 254 | /* since jiffies are not updated when suspended now represents |
| 255 | * the time it would have suspended */ |
| 256 | if (time_after(timer->timer.expires, now)) { |
| 257 | get_monotonic_boottime(&ts); |
| 258 | ts = timespec_sub(ts, timer->last_suspend_time); |
| 259 | time_diff = timespec_to_jiffies(&ts); |
| 260 | if (timer->timer.expires > (time_diff + now)) { |
| 261 | mod_timer_pending(&timer->timer, |
| 262 | (timer->timer.expires - time_diff)); |
| 263 | } else { |
| 264 | del_timer(&timer->timer); |
| 265 | timer->timer.expires = 0; |
| 266 | timer->active = false; |
| 267 | timer->work_pending = true; |
| 268 | schedule_work(&timer->work); |
| 269 | } |
| 270 | } |
| 271 | spin_unlock_bh(×tamp_lock); |
| 272 | break; |
| 273 | default: |
| 274 | break; |
| 275 | } |
| 276 | return NOTIFY_DONE; |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int idletimer_tg_create(struct idletimer_tg_info *info) |
| 280 | { |
| 281 | int ret; |
| 282 | |
| 283 | info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL); |
| 284 | if (!info->timer) { |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 285 | ret = -ENOMEM; |
| 286 | goto out; |
| 287 | } |
| 288 | |
Dmitry Torokhov | 484836e | 2015-07-09 17:15:01 -0700 | [diff] [blame] | 289 | sysfs_attr_init(&info->timer->attr.attr); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 290 | info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL); |
| 291 | if (!info->timer->attr.attr.name) { |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 292 | ret = -ENOMEM; |
| 293 | goto out_free_timer; |
| 294 | } |
| 295 | info->timer->attr.attr.mode = S_IRUGO; |
| 296 | info->timer->attr.show = idletimer_tg_show; |
| 297 | |
| 298 | ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr); |
| 299 | if (ret < 0) { |
| 300 | pr_debug("couldn't add file to sysfs"); |
| 301 | goto out_free_attr; |
| 302 | } |
| 303 | |
| 304 | list_add(&info->timer->entry, &idletimer_tg_list); |
| 305 | |
| 306 | setup_timer(&info->timer->timer, idletimer_tg_expired, |
| 307 | (unsigned long) info->timer); |
| 308 | info->timer->refcnt = 1; |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 309 | info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true; |
| 310 | info->timer->active = true; |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 311 | info->timer->timeout = info->timeout; |
| 312 | |
| 313 | info->timer->delayed_timer_trigger.tv_sec = 0; |
| 314 | info->timer->delayed_timer_trigger.tv_nsec = 0; |
| 315 | info->timer->work_pending = false; |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 316 | info->timer->uid = 0; |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 317 | get_monotonic_boottime(&info->timer->last_modified_timer); |
| 318 | |
| 319 | info->timer->pm_nb.notifier_call = idletimer_resume; |
| 320 | ret = register_pm_notifier(&info->timer->pm_nb); |
| 321 | if (ret) |
| 322 | printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n", |
| 323 | __func__, ret); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 324 | |
| 325 | mod_timer(&info->timer->timer, |
| 326 | msecs_to_jiffies(info->timeout * 1000) + jiffies); |
| 327 | |
| 328 | INIT_WORK(&info->timer->work, idletimer_tg_work); |
| 329 | |
| 330 | return 0; |
| 331 | |
| 332 | out_free_attr: |
| 333 | kfree(info->timer->attr.attr.name); |
| 334 | out_free_timer: |
| 335 | kfree(info->timer); |
| 336 | out: |
| 337 | return ret; |
| 338 | } |
| 339 | |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 340 | static void reset_timer(const struct idletimer_tg_info *info, |
| 341 | struct sk_buff *skb) |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 342 | { |
| 343 | unsigned long now = jiffies; |
| 344 | struct idletimer_tg *timer = info->timer; |
| 345 | bool timer_prev; |
| 346 | |
| 347 | spin_lock_bh(×tamp_lock); |
| 348 | timer_prev = timer->active; |
| 349 | timer->active = true; |
| 350 | /* timer_prev is used to guard overflow problem in time_before*/ |
| 351 | if (!timer_prev || time_before(timer->timer.expires, now)) { |
| 352 | pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n", |
| 353 | timer->timer.expires, now); |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 354 | |
| 355 | /* Stores the uid resposible for waking up the radio */ |
| 356 | if (skb && (skb->sk)) { |
Amit Pundir | 22ea73de | 2015-05-11 14:39:59 +0530 | [diff] [blame] | 357 | timer->uid = from_kuid_munged(current_user_ns(), |
| 358 | sock_i_uid(skb->sk)); |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 361 | /* checks if there is a pending inactive notification*/ |
| 362 | if (timer->work_pending) |
| 363 | timer->delayed_timer_trigger = timer->last_modified_timer; |
| 364 | else { |
| 365 | timer->work_pending = true; |
| 366 | schedule_work(&timer->work); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | get_monotonic_boottime(&timer->last_modified_timer); |
| 371 | mod_timer(&timer->timer, |
| 372 | msecs_to_jiffies(info->timeout * 1000) + now); |
| 373 | spin_unlock_bh(×tamp_lock); |
| 374 | } |
| 375 | |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 376 | /* |
| 377 | * The actual xt_tables plugin. |
| 378 | */ |
| 379 | static unsigned int idletimer_tg_target(struct sk_buff *skb, |
| 380 | const struct xt_action_param *par) |
| 381 | { |
| 382 | const struct idletimer_tg_info *info = par->targinfo; |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 383 | unsigned long now = jiffies; |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 384 | |
| 385 | pr_debug("resetting timer %s, timeout period %u\n", |
| 386 | info->label, info->timeout); |
| 387 | |
| 388 | BUG_ON(!info->timer); |
| 389 | |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 390 | info->timer->active = true; |
| 391 | |
| 392 | if (time_before(info->timer->timer.expires, now)) { |
| 393 | schedule_work(&info->timer->work); |
| 394 | pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n", |
| 395 | info->label, info->timer->timer.expires, now); |
| 396 | } |
| 397 | |
| 398 | /* TODO: Avoid modifying timers on each packet */ |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 399 | reset_timer(info, skb); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 400 | return XT_CONTINUE; |
| 401 | } |
| 402 | |
| 403 | static int idletimer_tg_checkentry(const struct xt_tgchk_param *par) |
| 404 | { |
| 405 | struct idletimer_tg_info *info = par->targinfo; |
| 406 | int ret; |
| 407 | |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 408 | pr_debug("checkentry targinfo %s\n", info->label); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 409 | |
| 410 | if (info->timeout == 0) { |
| 411 | pr_debug("timeout value is zero\n"); |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | |
| 415 | if (info->label[0] == '\0' || |
| 416 | strnlen(info->label, |
| 417 | MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) { |
| 418 | pr_debug("label is empty or not nul-terminated\n"); |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | mutex_lock(&list_mutex); |
| 423 | |
| 424 | info->timer = __idletimer_tg_find_by_label(info->label); |
| 425 | if (info->timer) { |
| 426 | info->timer->refcnt++; |
Ruchi Kandoi | 5ecc807 | 2015-04-23 12:09:09 -0700 | [diff] [blame] | 427 | reset_timer(info, NULL); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 428 | pr_debug("increased refcnt of timer %s to %u\n", |
| 429 | info->label, info->timer->refcnt); |
| 430 | } else { |
| 431 | ret = idletimer_tg_create(info); |
| 432 | if (ret < 0) { |
| 433 | pr_debug("failed to create timer\n"); |
| 434 | mutex_unlock(&list_mutex); |
| 435 | return ret; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | mutex_unlock(&list_mutex); |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 440 | |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static void idletimer_tg_destroy(const struct xt_tgdtor_param *par) |
| 445 | { |
| 446 | const struct idletimer_tg_info *info = par->targinfo; |
| 447 | |
| 448 | pr_debug("destroy targinfo %s\n", info->label); |
| 449 | |
| 450 | mutex_lock(&list_mutex); |
| 451 | |
| 452 | if (--info->timer->refcnt == 0) { |
| 453 | pr_debug("deleting timer %s\n", info->label); |
| 454 | |
| 455 | list_del(&info->timer->entry); |
| 456 | del_timer_sync(&info->timer->timer); |
| 457 | sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr); |
Ruchi Kandoi | eb16843 | 2014-03-25 16:43:28 -0700 | [diff] [blame] | 458 | unregister_pm_notifier(&info->timer->pm_nb); |
Subash Abhinov Kasiviswanathan | 7b112430 | 2016-11-10 19:36:15 -0700 | [diff] [blame^] | 459 | cancel_work_sync(&info->timer->work); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 460 | kfree(info->timer->attr.attr.name); |
| 461 | kfree(info->timer); |
| 462 | } else { |
| 463 | pr_debug("decreased refcnt of timer %s to %u\n", |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 464 | info->label, info->timer->refcnt); |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | mutex_unlock(&list_mutex); |
| 468 | } |
| 469 | |
| 470 | static struct xt_target idletimer_tg __read_mostly = { |
| 471 | .name = "IDLETIMER", |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 472 | .revision = 1, |
Luciano Coelho | 0902b46 | 2010-06-15 15:04:00 +0200 | [diff] [blame] | 473 | .family = NFPROTO_UNSPEC, |
| 474 | .target = idletimer_tg_target, |
| 475 | .targetsize = sizeof(struct idletimer_tg_info), |
| 476 | .checkentry = idletimer_tg_checkentry, |
| 477 | .destroy = idletimer_tg_destroy, |
| 478 | .me = THIS_MODULE, |
| 479 | }; |
| 480 | |
| 481 | static struct class *idletimer_tg_class; |
| 482 | |
| 483 | static struct device *idletimer_tg_device; |
| 484 | |
| 485 | static int __init idletimer_tg_init(void) |
| 486 | { |
| 487 | int err; |
| 488 | |
| 489 | idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer"); |
| 490 | err = PTR_ERR(idletimer_tg_class); |
| 491 | if (IS_ERR(idletimer_tg_class)) { |
| 492 | pr_debug("couldn't register device class\n"); |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | idletimer_tg_device = device_create(idletimer_tg_class, NULL, |
| 497 | MKDEV(0, 0), NULL, "timers"); |
| 498 | err = PTR_ERR(idletimer_tg_device); |
| 499 | if (IS_ERR(idletimer_tg_device)) { |
| 500 | pr_debug("couldn't register system device\n"); |
| 501 | goto out_class; |
| 502 | } |
| 503 | |
| 504 | idletimer_tg_kobj = &idletimer_tg_device->kobj; |
| 505 | |
| 506 | err = xt_register_target(&idletimer_tg); |
| 507 | if (err < 0) { |
| 508 | pr_debug("couldn't register xt target\n"); |
| 509 | goto out_dev; |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | out_dev: |
| 514 | device_destroy(idletimer_tg_class, MKDEV(0, 0)); |
| 515 | out_class: |
| 516 | class_destroy(idletimer_tg_class); |
| 517 | out: |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | static void __exit idletimer_tg_exit(void) |
| 522 | { |
| 523 | xt_unregister_target(&idletimer_tg); |
| 524 | |
| 525 | device_destroy(idletimer_tg_class, MKDEV(0, 0)); |
| 526 | class_destroy(idletimer_tg_class); |
| 527 | } |
| 528 | |
| 529 | module_init(idletimer_tg_init); |
| 530 | module_exit(idletimer_tg_exit); |
| 531 | |
| 532 | MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>"); |
| 533 | MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>"); |
| 534 | MODULE_DESCRIPTION("Xtables: idle time monitor"); |
| 535 | MODULE_LICENSE("GPL v2"); |
Jan Engelhardt | f1e231a | 2011-01-18 06:30:13 +0100 | [diff] [blame] | 536 | MODULE_ALIAS("ipt_IDLETIMER"); |
| 537 | MODULE_ALIAS("ip6t_IDLETIMER"); |
JP Abgrall | cf7ad6a | 2012-04-26 23:28:35 -0700 | [diff] [blame] | 538 | MODULE_ALIAS("arpt_IDLETIMER"); |