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 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
276 if (!info->timer->attr.attr.name) {
280 info->timer->attr.attr.mode = S_IRUGO;
281 info->timer->attr.show = idletimer_tg_show;
283 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
285 pr_debug("couldn't add file to sysfs");
289 list_add(&info->timer->entry, &idletimer_tg_list);
291 setup_timer(&info->timer->timer, idletimer_tg_expired,
292 (unsigned long) info->timer);
293 info->timer->refcnt = 1;
294 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
295 info->timer->active = true;
296 info->timer->timeout = info->timeout;
298 info->timer->delayed_timer_trigger.tv_sec = 0;
299 info->timer->delayed_timer_trigger.tv_nsec = 0;
300 info->timer->work_pending = false;
301 get_monotonic_boottime(&info->timer->last_modified_timer);
303 info->timer->pm_nb.notifier_call = idletimer_resume;
304 ret = register_pm_notifier(&info->timer->pm_nb);
306 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
309 mod_timer(&info->timer->timer,
310 msecs_to_jiffies(info->timeout * 1000) + jiffies);
312 INIT_WORK(&info->timer->work, idletimer_tg_work);
317 kfree(info->timer->attr.attr.name);
324 static void reset_timer(const struct idletimer_tg_info *info)
326 unsigned long now = jiffies;
327 struct idletimer_tg *timer = info->timer;
330 spin_lock_bh(×tamp_lock);
331 timer_prev = timer->active;
332 timer->active = true;
333 /* timer_prev is used to guard overflow problem in time_before*/
334 if (!timer_prev || time_before(timer->timer.expires, now)) {
335 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
336 timer->timer.expires, now);
337 /* checks if there is a pending inactive notification*/
338 if (timer->work_pending)
339 timer->delayed_timer_trigger = timer->last_modified_timer;
341 timer->work_pending = true;
342 schedule_work(&timer->work);
346 get_monotonic_boottime(&timer->last_modified_timer);
347 mod_timer(&timer->timer,
348 msecs_to_jiffies(info->timeout * 1000) + now);
349 spin_unlock_bh(×tamp_lock);
353 * The actual xt_tables plugin.
355 static unsigned int idletimer_tg_target(struct sk_buff *skb,
356 const struct xt_action_param *par)
358 const struct idletimer_tg_info *info = par->targinfo;
359 unsigned long now = jiffies;
361 pr_debug("resetting timer %s, timeout period %u\n",
362 info->label, info->timeout);
364 BUG_ON(!info->timer);
366 info->timer->active = true;
368 if (time_before(info->timer->timer.expires, now)) {
369 schedule_work(&info->timer->work);
370 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
371 info->label, info->timer->timer.expires, now);
374 /* TODO: Avoid modifying timers on each packet */
379 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
381 struct idletimer_tg_info *info = par->targinfo;
384 pr_debug("checkentry targinfo %s\n", info->label);
386 if (info->timeout == 0) {
387 pr_debug("timeout value is zero\n");
391 if (info->label[0] == '\0' ||
393 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
394 pr_debug("label is empty or not nul-terminated\n");
398 mutex_lock(&list_mutex);
400 info->timer = __idletimer_tg_find_by_label(info->label);
402 info->timer->refcnt++;
404 pr_debug("increased refcnt of timer %s to %u\n",
405 info->label, info->timer->refcnt);
407 ret = idletimer_tg_create(info);
409 pr_debug("failed to create timer\n");
410 mutex_unlock(&list_mutex);
415 mutex_unlock(&list_mutex);
420 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
422 const struct idletimer_tg_info *info = par->targinfo;
424 pr_debug("destroy targinfo %s\n", info->label);
426 mutex_lock(&list_mutex);
428 if (--info->timer->refcnt == 0) {
429 pr_debug("deleting timer %s\n", info->label);
431 list_del(&info->timer->entry);
432 del_timer_sync(&info->timer->timer);
433 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
434 unregister_pm_notifier(&info->timer->pm_nb);
435 kfree(info->timer->attr.attr.name);
438 pr_debug("decreased refcnt of timer %s to %u\n",
439 info->label, info->timer->refcnt);
442 mutex_unlock(&list_mutex);
445 static struct xt_target idletimer_tg __read_mostly = {
448 .family = NFPROTO_UNSPEC,
449 .target = idletimer_tg_target,
450 .targetsize = sizeof(struct idletimer_tg_info),
451 .checkentry = idletimer_tg_checkentry,
452 .destroy = idletimer_tg_destroy,
456 static struct class *idletimer_tg_class;
458 static struct device *idletimer_tg_device;
460 static int __init idletimer_tg_init(void)
464 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
465 err = PTR_ERR(idletimer_tg_class);
466 if (IS_ERR(idletimer_tg_class)) {
467 pr_debug("couldn't register device class\n");
471 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
472 MKDEV(0, 0), NULL, "timers");
473 err = PTR_ERR(idletimer_tg_device);
474 if (IS_ERR(idletimer_tg_device)) {
475 pr_debug("couldn't register system device\n");
479 idletimer_tg_kobj = &idletimer_tg_device->kobj;
481 err = xt_register_target(&idletimer_tg);
483 pr_debug("couldn't register xt target\n");
489 device_destroy(idletimer_tg_class, MKDEV(0, 0));
491 class_destroy(idletimer_tg_class);
496 static void __exit idletimer_tg_exit(void)
498 xt_unregister_target(&idletimer_tg);
500 device_destroy(idletimer_tg_class, MKDEV(0, 0));
501 class_destroy(idletimer_tg_class);
504 module_init(idletimer_tg_init);
505 module_exit(idletimer_tg_exit);
507 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
508 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
509 MODULE_DESCRIPTION("Xtables: idle time monitor");
510 MODULE_LICENSE("GPL v2");
511 MODULE_ALIAS("ipt_IDLETIMER");
512 MODULE_ALIAS("ip6t_IDLETIMER");
513 MODULE_ALIAS("arpt_IDLETIMER");