2 * linux/net/netfilter/xt_IDLETIMER.c
4 * Netfilter module to trigger a timer when packet matches.
5 * After timer expires a kevent will be sent.
7 * Copyright (C) 2004, 2010 Nokia Corporation
9 * Written by Timo Teras <ext-timo.teras@nokia.com>
11 * Converted to x_tables and reworked for upstream inclusion
12 * by Luciano Coelho <luciano.coelho@nokia.com>
14 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
40 #include <linux/kdev_t.h>
41 #include <linux/kobject.h>
42 #include <linux/skbuff.h>
43 #include <linux/workqueue.h>
44 #include <linux/sysfs.h>
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>
50 #include <net/net_namespace.h>
52 struct idletimer_tg_attr {
53 struct attribute attr;
54 ssize_t (*show)(struct kobject *kobj,
55 struct attribute *attr, char *buf);
59 struct list_head entry;
60 struct timer_list timer;
61 struct work_struct work;
64 struct idletimer_tg_attr attr;
66 struct timespec delayed_timer_trigger;
67 struct timespec last_modified_timer;
68 struct timespec last_suspend_time;
69 struct notifier_block pm_nb;
78 static LIST_HEAD(idletimer_tg_list);
79 static DEFINE_MUTEX(list_mutex);
80 static DEFINE_SPINLOCK(timestamp_lock);
82 static struct kobject *idletimer_tg_kobj;
84 static bool check_for_delayed_trigger(struct idletimer_tg *timer,
89 spin_lock_bh(×tamp_lock);
90 timer->work_pending = false;
91 if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
92 timer->delayed_timer_trigger.tv_sec != 0) {
94 temp.tv_sec = timer->timeout;
96 if (timer->delayed_timer_trigger.tv_sec != 0) {
97 temp = timespec_add(timer->delayed_timer_trigger, temp);
98 ts->tv_sec = temp.tv_sec;
99 ts->tv_nsec = temp.tv_nsec;
100 timer->delayed_timer_trigger.tv_sec = 0;
101 timer->work_pending = true;
102 schedule_work(&timer->work);
104 temp = timespec_add(timer->last_modified_timer, temp);
105 ts->tv_sec = temp.tv_sec;
106 ts->tv_nsec = temp.tv_nsec;
109 state = timer->active;
111 spin_unlock_bh(×tamp_lock);
115 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
117 char iface_msg[NLMSG_MAX_SIZE];
118 char state_msg[NLMSG_MAX_SIZE];
119 char timestamp_msg[NLMSG_MAX_SIZE];
120 char *envp[] = { iface_msg, state_msg, timestamp_msg, NULL };
126 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
128 if (NLMSG_MAX_SIZE <= res) {
129 pr_err("message too long (%d)", res);
133 get_monotonic_boottime(&ts);
134 state = check_for_delayed_trigger(timer, &ts);
135 res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
136 state ? "active" : "inactive");
138 if (NLMSG_MAX_SIZE <= res) {
139 pr_err("message too long (%d)", res);
143 time_ns = timespec_to_ns(&ts);
144 res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
145 if (NLMSG_MAX_SIZE <= res) {
146 timestamp_msg[0] = '\0';
147 pr_err("message too long (%d)", res);
150 pr_debug("putting nlmsg: <%s> <%s>\n", iface_msg, state_msg);
151 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
158 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
160 struct idletimer_tg *entry;
164 list_for_each_entry(entry, &idletimer_tg_list, entry) {
165 if (!strcmp(label, entry->attr.attr.name))
172 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
175 struct idletimer_tg *timer;
176 unsigned long expires = 0;
177 unsigned long now = jiffies;
179 mutex_lock(&list_mutex);
181 timer = __idletimer_tg_find_by_label(attr->name);
183 expires = timer->timer.expires;
185 mutex_unlock(&list_mutex);
187 if (time_after(expires, now))
188 return sprintf(buf, "%u\n",
189 jiffies_to_msecs(expires - now) / 1000);
191 if (timer->send_nl_msg)
192 return sprintf(buf, "0 %d\n",
193 jiffies_to_msecs(now - expires) / 1000);
195 return sprintf(buf, "0\n");
198 static void idletimer_tg_work(struct work_struct *work)
200 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
203 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
205 if (timer->send_nl_msg)
206 notify_netlink_uevent(timer->attr.attr.name, timer);
209 static void idletimer_tg_expired(unsigned long data)
211 struct idletimer_tg *timer = (struct idletimer_tg *) data;
213 pr_debug("timer %s expired\n", timer->attr.attr.name);
214 spin_lock_bh(×tamp_lock);
215 timer->active = false;
216 timer->work_pending = true;
217 schedule_work(&timer->work);
218 spin_unlock_bh(×tamp_lock);
221 static int idletimer_resume(struct notifier_block *notifier,
222 unsigned long pm_event, void *unused)
225 unsigned long time_diff, now = jiffies;
226 struct idletimer_tg *timer = container_of(notifier,
227 struct idletimer_tg, pm_nb);
231 case PM_SUSPEND_PREPARE:
232 get_monotonic_boottime(&timer->last_suspend_time);
234 case PM_POST_SUSPEND:
235 spin_lock_bh(×tamp_lock);
236 if (!timer->active) {
237 spin_unlock_bh(×tamp_lock);
240 /* since jiffies are not updated when suspended now represents
241 * the time it would have suspended */
242 if (time_after(timer->timer.expires, now)) {
243 get_monotonic_boottime(&ts);
244 ts = timespec_sub(ts, timer->last_suspend_time);
245 time_diff = timespec_to_jiffies(&ts);
246 if (timer->timer.expires > (time_diff + now)) {
247 mod_timer_pending(&timer->timer,
248 (timer->timer.expires - time_diff));
250 del_timer(&timer->timer);
251 timer->timer.expires = 0;
252 timer->active = false;
253 timer->work_pending = true;
254 schedule_work(&timer->work);
257 spin_unlock_bh(×tamp_lock);
265 static int idletimer_tg_create(struct idletimer_tg_info *info)
269 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
275 sysfs_attr_init(&info->timer->attr.attr);
276 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
277 if (!info->timer->attr.attr.name) {
281 info->timer->attr.attr.mode = S_IRUGO;
282 info->timer->attr.show = idletimer_tg_show;
284 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
286 pr_debug("couldn't add file to sysfs");
290 list_add(&info->timer->entry, &idletimer_tg_list);
292 setup_timer(&info->timer->timer, idletimer_tg_expired,
293 (unsigned long) info->timer);
294 info->timer->refcnt = 1;
295 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
296 info->timer->active = true;
297 info->timer->timeout = info->timeout;
299 info->timer->delayed_timer_trigger.tv_sec = 0;
300 info->timer->delayed_timer_trigger.tv_nsec = 0;
301 info->timer->work_pending = false;
302 get_monotonic_boottime(&info->timer->last_modified_timer);
304 info->timer->pm_nb.notifier_call = idletimer_resume;
305 ret = register_pm_notifier(&info->timer->pm_nb);
307 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
310 mod_timer(&info->timer->timer,
311 msecs_to_jiffies(info->timeout * 1000) + jiffies);
313 INIT_WORK(&info->timer->work, idletimer_tg_work);
318 kfree(info->timer->attr.attr.name);
325 static void reset_timer(const struct idletimer_tg_info *info)
327 unsigned long now = jiffies;
328 struct idletimer_tg *timer = info->timer;
331 spin_lock_bh(×tamp_lock);
332 timer_prev = timer->active;
333 timer->active = true;
334 /* timer_prev is used to guard overflow problem in time_before*/
335 if (!timer_prev || time_before(timer->timer.expires, now)) {
336 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
337 timer->timer.expires, now);
338 /* checks if there is a pending inactive notification*/
339 if (timer->work_pending)
340 timer->delayed_timer_trigger = timer->last_modified_timer;
342 timer->work_pending = true;
343 schedule_work(&timer->work);
347 get_monotonic_boottime(&timer->last_modified_timer);
348 mod_timer(&timer->timer,
349 msecs_to_jiffies(info->timeout * 1000) + now);
350 spin_unlock_bh(×tamp_lock);
354 * The actual xt_tables plugin.
356 static unsigned int idletimer_tg_target(struct sk_buff *skb,
357 const struct xt_action_param *par)
359 const struct idletimer_tg_info *info = par->targinfo;
360 unsigned long now = jiffies;
362 pr_debug("resetting timer %s, timeout period %u\n",
363 info->label, info->timeout);
365 BUG_ON(!info->timer);
367 info->timer->active = true;
369 if (time_before(info->timer->timer.expires, now)) {
370 schedule_work(&info->timer->work);
371 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
372 info->label, info->timer->timer.expires, now);
375 /* TODO: Avoid modifying timers on each packet */
380 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
382 struct idletimer_tg_info *info = par->targinfo;
385 pr_debug("checkentry targinfo %s\n", info->label);
387 if (info->timeout == 0) {
388 pr_debug("timeout value is zero\n");
392 if (info->label[0] == '\0' ||
394 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
395 pr_debug("label is empty or not nul-terminated\n");
399 mutex_lock(&list_mutex);
401 info->timer = __idletimer_tg_find_by_label(info->label);
403 info->timer->refcnt++;
405 pr_debug("increased refcnt of timer %s to %u\n",
406 info->label, info->timer->refcnt);
408 ret = idletimer_tg_create(info);
410 pr_debug("failed to create timer\n");
411 mutex_unlock(&list_mutex);
416 mutex_unlock(&list_mutex);
421 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
423 const struct idletimer_tg_info *info = par->targinfo;
425 pr_debug("destroy targinfo %s\n", info->label);
427 mutex_lock(&list_mutex);
429 if (--info->timer->refcnt == 0) {
430 pr_debug("deleting timer %s\n", info->label);
432 list_del(&info->timer->entry);
433 del_timer_sync(&info->timer->timer);
434 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
435 unregister_pm_notifier(&info->timer->pm_nb);
436 kfree(info->timer->attr.attr.name);
439 pr_debug("decreased refcnt of timer %s to %u\n",
440 info->label, info->timer->refcnt);
443 mutex_unlock(&list_mutex);
446 static struct xt_target idletimer_tg __read_mostly = {
449 .family = NFPROTO_UNSPEC,
450 .target = idletimer_tg_target,
451 .targetsize = sizeof(struct idletimer_tg_info),
452 .checkentry = idletimer_tg_checkentry,
453 .destroy = idletimer_tg_destroy,
457 static struct class *idletimer_tg_class;
459 static struct device *idletimer_tg_device;
461 static int __init idletimer_tg_init(void)
465 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
466 err = PTR_ERR(idletimer_tg_class);
467 if (IS_ERR(idletimer_tg_class)) {
468 pr_debug("couldn't register device class\n");
472 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
473 MKDEV(0, 0), NULL, "timers");
474 err = PTR_ERR(idletimer_tg_device);
475 if (IS_ERR(idletimer_tg_device)) {
476 pr_debug("couldn't register system device\n");
480 idletimer_tg_kobj = &idletimer_tg_device->kobj;
482 err = xt_register_target(&idletimer_tg);
484 pr_debug("couldn't register xt target\n");
490 device_destroy(idletimer_tg_class, MKDEV(0, 0));
492 class_destroy(idletimer_tg_class);
497 static void __exit idletimer_tg_exit(void)
499 xt_unregister_target(&idletimer_tg);
501 device_destroy(idletimer_tg_class, MKDEV(0, 0));
502 class_destroy(idletimer_tg_class);
505 module_init(idletimer_tg_init);
506 module_exit(idletimer_tg_exit);
508 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
509 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
510 MODULE_DESCRIPTION("Xtables: idle time monitor");
511 MODULE_LICENSE("GPL v2");
512 MODULE_ALIAS("ipt_IDLETIMER");
513 MODULE_ALIAS("ip6t_IDLETIMER");
514 MODULE_ALIAS("arpt_IDLETIMER");