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 #include <net/inet_sock.h>
54 struct idletimer_tg_attr {
55 struct attribute attr;
56 ssize_t (*show)(struct kobject *kobj,
57 struct attribute *attr, char *buf);
61 struct list_head entry;
62 struct timer_list timer;
63 struct work_struct work;
66 struct idletimer_tg_attr attr;
68 struct timespec delayed_timer_trigger;
69 struct timespec last_modified_timer;
70 struct timespec last_suspend_time;
71 struct notifier_block pm_nb;
81 static LIST_HEAD(idletimer_tg_list);
82 static DEFINE_MUTEX(list_mutex);
83 static DEFINE_SPINLOCK(timestamp_lock);
85 static struct kobject *idletimer_tg_kobj;
87 static bool check_for_delayed_trigger(struct idletimer_tg *timer,
92 spin_lock_bh(×tamp_lock);
93 timer->work_pending = false;
94 if ((ts->tv_sec - timer->last_modified_timer.tv_sec) > timer->timeout ||
95 timer->delayed_timer_trigger.tv_sec != 0) {
97 temp.tv_sec = timer->timeout;
99 if (timer->delayed_timer_trigger.tv_sec != 0) {
100 temp = timespec_add(timer->delayed_timer_trigger, temp);
101 ts->tv_sec = temp.tv_sec;
102 ts->tv_nsec = temp.tv_nsec;
103 timer->delayed_timer_trigger.tv_sec = 0;
104 timer->work_pending = true;
105 schedule_work(&timer->work);
107 temp = timespec_add(timer->last_modified_timer, temp);
108 ts->tv_sec = temp.tv_sec;
109 ts->tv_nsec = temp.tv_nsec;
112 state = timer->active;
114 spin_unlock_bh(×tamp_lock);
118 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
120 char iface_msg[NLMSG_MAX_SIZE];
121 char state_msg[NLMSG_MAX_SIZE];
122 char timestamp_msg[NLMSG_MAX_SIZE];
123 char uid_msg[NLMSG_MAX_SIZE];
124 char *envp[] = { iface_msg, state_msg, timestamp_msg, uid_msg, NULL };
130 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
132 if (NLMSG_MAX_SIZE <= res) {
133 pr_err("message too long (%d)", res);
137 get_monotonic_boottime(&ts);
138 state = check_for_delayed_trigger(timer, &ts);
139 res = snprintf(state_msg, NLMSG_MAX_SIZE, "STATE=%s",
140 state ? "active" : "inactive");
142 if (NLMSG_MAX_SIZE <= res) {
143 pr_err("message too long (%d)", res);
148 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=%u", timer->uid);
149 if (NLMSG_MAX_SIZE <= res)
150 pr_err("message too long (%d)", res);
152 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=");
153 if (NLMSG_MAX_SIZE <= res)
154 pr_err("message too long (%d)", res);
157 time_ns = timespec_to_ns(&ts);
158 res = snprintf(timestamp_msg, NLMSG_MAX_SIZE, "TIME_NS=%llu", time_ns);
159 if (NLMSG_MAX_SIZE <= res) {
160 timestamp_msg[0] = '\0';
161 pr_err("message too long (%d)", res);
164 pr_debug("putting nlmsg: <%s> <%s> <%s> <%s>\n", iface_msg, state_msg,
165 timestamp_msg, uid_msg);
166 kobject_uevent_env(idletimer_tg_kobj, KOBJ_CHANGE, envp);
173 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
175 struct idletimer_tg *entry;
179 list_for_each_entry(entry, &idletimer_tg_list, entry) {
180 if (!strcmp(label, entry->attr.attr.name))
187 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
190 struct idletimer_tg *timer;
191 unsigned long expires = 0;
192 unsigned long now = jiffies;
194 mutex_lock(&list_mutex);
196 timer = __idletimer_tg_find_by_label(attr->name);
198 expires = timer->timer.expires;
200 mutex_unlock(&list_mutex);
202 if (time_after(expires, now))
203 return sprintf(buf, "%u\n",
204 jiffies_to_msecs(expires - now) / 1000);
206 if (timer->send_nl_msg)
207 return sprintf(buf, "0 %d\n",
208 jiffies_to_msecs(now - expires) / 1000);
210 return sprintf(buf, "0\n");
213 static void idletimer_tg_work(struct work_struct *work)
215 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
218 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
220 if (timer->send_nl_msg)
221 notify_netlink_uevent(timer->attr.attr.name, timer);
224 static void idletimer_tg_expired(unsigned long data)
226 struct idletimer_tg *timer = (struct idletimer_tg *) data;
228 pr_debug("timer %s expired\n", timer->attr.attr.name);
229 spin_lock_bh(×tamp_lock);
230 timer->active = false;
231 timer->work_pending = true;
232 schedule_work(&timer->work);
233 spin_unlock_bh(×tamp_lock);
236 static int idletimer_resume(struct notifier_block *notifier,
237 unsigned long pm_event, void *unused)
240 unsigned long time_diff, now = jiffies;
241 struct idletimer_tg *timer = container_of(notifier,
242 struct idletimer_tg, pm_nb);
246 case PM_SUSPEND_PREPARE:
247 get_monotonic_boottime(&timer->last_suspend_time);
249 case PM_POST_SUSPEND:
250 spin_lock_bh(×tamp_lock);
251 if (!timer->active) {
252 spin_unlock_bh(×tamp_lock);
255 /* since jiffies are not updated when suspended now represents
256 * the time it would have suspended */
257 if (time_after(timer->timer.expires, now)) {
258 get_monotonic_boottime(&ts);
259 ts = timespec_sub(ts, timer->last_suspend_time);
260 time_diff = timespec_to_jiffies(&ts);
261 if (timer->timer.expires > (time_diff + now)) {
262 mod_timer_pending(&timer->timer,
263 (timer->timer.expires - time_diff));
265 del_timer(&timer->timer);
266 timer->timer.expires = 0;
267 timer->active = false;
268 timer->work_pending = true;
269 schedule_work(&timer->work);
272 spin_unlock_bh(×tamp_lock);
280 static int idletimer_tg_create(struct idletimer_tg_info *info)
284 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
290 sysfs_attr_init(&info->timer->attr.attr);
291 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
292 if (!info->timer->attr.attr.name) {
296 info->timer->attr.attr.mode = S_IRUGO;
297 info->timer->attr.show = idletimer_tg_show;
299 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
301 pr_debug("couldn't add file to sysfs");
305 list_add(&info->timer->entry, &idletimer_tg_list);
307 setup_timer(&info->timer->timer, idletimer_tg_expired,
308 (unsigned long) info->timer);
309 info->timer->refcnt = 1;
310 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
311 info->timer->active = true;
312 info->timer->timeout = info->timeout;
314 info->timer->delayed_timer_trigger.tv_sec = 0;
315 info->timer->delayed_timer_trigger.tv_nsec = 0;
316 info->timer->work_pending = false;
317 info->timer->uid = 0;
318 get_monotonic_boottime(&info->timer->last_modified_timer);
320 info->timer->pm_nb.notifier_call = idletimer_resume;
321 ret = register_pm_notifier(&info->timer->pm_nb);
323 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
326 mod_timer(&info->timer->timer,
327 msecs_to_jiffies(info->timeout * 1000) + jiffies);
329 INIT_WORK(&info->timer->work, idletimer_tg_work);
334 kfree(info->timer->attr.attr.name);
341 static void reset_timer(const struct idletimer_tg_info *info,
344 unsigned long now = jiffies;
345 struct idletimer_tg *timer = info->timer;
348 spin_lock_bh(×tamp_lock);
349 timer_prev = timer->active;
350 timer->active = true;
351 /* timer_prev is used to guard overflow problem in time_before*/
352 if (!timer_prev || time_before(timer->timer.expires, now)) {
353 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
354 timer->timer.expires, now);
356 /* Stores the uid resposible for waking up the radio */
357 if (skb && (skb->sk)) {
358 timer->uid = from_kuid_munged(current_user_ns(),
359 sock_i_uid(skb_to_full_sk(skb)));
362 /* checks if there is a pending inactive notification*/
363 if (timer->work_pending)
364 timer->delayed_timer_trigger = timer->last_modified_timer;
366 timer->work_pending = true;
367 schedule_work(&timer->work);
371 get_monotonic_boottime(&timer->last_modified_timer);
372 mod_timer(&timer->timer,
373 msecs_to_jiffies(info->timeout * 1000) + now);
374 spin_unlock_bh(×tamp_lock);
378 * The actual xt_tables plugin.
380 static unsigned int idletimer_tg_target(struct sk_buff *skb,
381 const struct xt_action_param *par)
383 const struct idletimer_tg_info *info = par->targinfo;
384 unsigned long now = jiffies;
386 pr_debug("resetting timer %s, timeout period %u\n",
387 info->label, info->timeout);
389 BUG_ON(!info->timer);
391 info->timer->active = true;
393 if (time_before(info->timer->timer.expires, now)) {
394 schedule_work(&info->timer->work);
395 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
396 info->label, info->timer->timer.expires, now);
399 /* TODO: Avoid modifying timers on each packet */
400 reset_timer(info, skb);
404 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
406 struct idletimer_tg_info *info = par->targinfo;
409 pr_debug("checkentry targinfo %s\n", info->label);
411 if (info->timeout == 0) {
412 pr_debug("timeout value is zero\n");
416 if (info->label[0] == '\0' ||
418 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
419 pr_debug("label is empty or not nul-terminated\n");
423 mutex_lock(&list_mutex);
425 info->timer = __idletimer_tg_find_by_label(info->label);
427 info->timer->refcnt++;
428 reset_timer(info, NULL);
429 pr_debug("increased refcnt of timer %s to %u\n",
430 info->label, info->timer->refcnt);
432 ret = idletimer_tg_create(info);
434 pr_debug("failed to create timer\n");
435 mutex_unlock(&list_mutex);
440 mutex_unlock(&list_mutex);
445 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
447 const struct idletimer_tg_info *info = par->targinfo;
449 pr_debug("destroy targinfo %s\n", info->label);
451 mutex_lock(&list_mutex);
453 if (--info->timer->refcnt == 0) {
454 pr_debug("deleting timer %s\n", info->label);
456 list_del(&info->timer->entry);
457 del_timer_sync(&info->timer->timer);
458 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
459 unregister_pm_notifier(&info->timer->pm_nb);
460 cancel_work_sync(&info->timer->work);
461 kfree(info->timer->attr.attr.name);
464 pr_debug("decreased refcnt of timer %s to %u\n",
465 info->label, info->timer->refcnt);
468 mutex_unlock(&list_mutex);
471 static struct xt_target idletimer_tg __read_mostly = {
474 .family = NFPROTO_UNSPEC,
475 .target = idletimer_tg_target,
476 .targetsize = sizeof(struct idletimer_tg_info),
477 .checkentry = idletimer_tg_checkentry,
478 .destroy = idletimer_tg_destroy,
482 static struct class *idletimer_tg_class;
484 static struct device *idletimer_tg_device;
486 static int __init idletimer_tg_init(void)
490 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
491 err = PTR_ERR(idletimer_tg_class);
492 if (IS_ERR(idletimer_tg_class)) {
493 pr_debug("couldn't register device class\n");
497 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
498 MKDEV(0, 0), NULL, "timers");
499 err = PTR_ERR(idletimer_tg_device);
500 if (IS_ERR(idletimer_tg_device)) {
501 pr_debug("couldn't register system device\n");
505 idletimer_tg_kobj = &idletimer_tg_device->kobj;
507 err = xt_register_target(&idletimer_tg);
509 pr_debug("couldn't register xt target\n");
515 device_destroy(idletimer_tg_class, MKDEV(0, 0));
517 class_destroy(idletimer_tg_class);
522 static void __exit idletimer_tg_exit(void)
524 xt_unregister_target(&idletimer_tg);
526 device_destroy(idletimer_tg_class, MKDEV(0, 0));
527 class_destroy(idletimer_tg_class);
530 module_init(idletimer_tg_init);
531 module_exit(idletimer_tg_exit);
533 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
534 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
535 MODULE_DESCRIPTION("Xtables: idle time monitor");
536 MODULE_LICENSE("GPL v2");
537 MODULE_ALIAS("ipt_IDLETIMER");
538 MODULE_ALIAS("ip6t_IDLETIMER");
539 MODULE_ALIAS("arpt_IDLETIMER");