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
8 *
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>
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>
51 #include <net/sock.h>
52 #include <net/inet_sock.h>
53
54 struct idletimer_tg_attr {
55 struct attribute attr;
56 ssize_t (*show)(struct kobject *kobj,
57 struct attribute *attr, char *buf);
58 };
59
60 struct idletimer_tg {
61 struct list_head entry;
62 struct timer_list timer;
63 struct work_struct work;
64
65 struct kobject *kobj;
66 struct idletimer_tg_attr attr;
67
68 struct timespec delayed_timer_trigger;
69 struct timespec last_modified_timer;
70 struct timespec last_suspend_time;
71 struct notifier_block pm_nb;
72
73 int timeout;
74 unsigned int refcnt;
75 bool work_pending;
76 bool send_nl_msg;
77 bool active;
78 uid_t uid;
79 };
80
81 static LIST_HEAD(idletimer_tg_list);
82 static DEFINE_MUTEX(list_mutex);
83 static DEFINE_SPINLOCK(timestamp_lock);
84
85 static struct kobject *idletimer_tg_kobj;
86
check_for_delayed_trigger(struct idletimer_tg * timer,struct timespec * ts)87 static bool check_for_delayed_trigger(struct idletimer_tg *timer,
88 struct timespec *ts)
89 {
90 bool state;
91 struct timespec temp;
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) {
96 state = false;
97 temp.tv_sec = timer->timeout;
98 temp.tv_nsec = 0;
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);
106 } else {
107 temp = timespec_add(timer->last_modified_timer, temp);
108 ts->tv_sec = temp.tv_sec;
109 ts->tv_nsec = temp.tv_nsec;
110 }
111 } else {
112 state = timer->active;
113 }
114 spin_unlock_bh(×tamp_lock);
115 return state;
116 }
117
notify_netlink_uevent(const char * iface,struct idletimer_tg * timer)118 static void notify_netlink_uevent(const char *iface, struct idletimer_tg *timer)
119 {
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 };
125 int res;
126 struct timespec ts;
127 uint64_t time_ns;
128 bool state;
129
130 res = snprintf(iface_msg, NLMSG_MAX_SIZE, "INTERFACE=%s",
131 iface);
132 if (NLMSG_MAX_SIZE <= res) {
133 pr_err("message too long (%d)", res);
134 return;
135 }
136
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");
141
142 if (NLMSG_MAX_SIZE <= res) {
143 pr_err("message too long (%d)", res);
144 return;
145 }
146
147 if (state) {
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);
151 } else {
152 res = snprintf(uid_msg, NLMSG_MAX_SIZE, "UID=");
153 if (NLMSG_MAX_SIZE <= res)
154 pr_err("message too long (%d)", res);
155 }
156
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);
162 }
163
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);
167 return;
168
169
170 }
171
172 static
__idletimer_tg_find_by_label(const char * label)173 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
174 {
175 struct idletimer_tg *entry;
176
177 BUG_ON(!label);
178
179 list_for_each_entry(entry, &idletimer_tg_list, entry) {
180 if (!strcmp(label, entry->attr.attr.name))
181 return entry;
182 }
183
184 return NULL;
185 }
186
idletimer_tg_show(struct kobject * kobj,struct attribute * attr,char * buf)187 static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
188 char *buf)
189 {
190 struct idletimer_tg *timer;
191 unsigned long expires = 0;
192 unsigned long now = jiffies;
193
194 mutex_lock(&list_mutex);
195
196 timer = __idletimer_tg_find_by_label(attr->name);
197 if (timer)
198 expires = timer->timer.expires;
199
200 mutex_unlock(&list_mutex);
201
202 if (time_after(expires, now))
203 return sprintf(buf, "%u\n",
204 jiffies_to_msecs(expires - now) / 1000);
205
206 if (timer->send_nl_msg)
207 return sprintf(buf, "0 %d\n",
208 jiffies_to_msecs(now - expires) / 1000);
209 else
210 return sprintf(buf, "0\n");
211 }
212
idletimer_tg_work(struct work_struct * work)213 static void idletimer_tg_work(struct work_struct *work)
214 {
215 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
216 work);
217
218 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
219
220 if (timer->send_nl_msg)
221 notify_netlink_uevent(timer->attr.attr.name, timer);
222 }
223
idletimer_tg_expired(unsigned long data)224 static void idletimer_tg_expired(unsigned long data)
225 {
226 struct idletimer_tg *timer = (struct idletimer_tg *) data;
227
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);
234 }
235
idletimer_resume(struct notifier_block * notifier,unsigned long pm_event,void * unused)236 static int idletimer_resume(struct notifier_block *notifier,
237 unsigned long pm_event, void *unused)
238 {
239 struct timespec ts;
240 unsigned long time_diff, now = jiffies;
241 struct idletimer_tg *timer = container_of(notifier,
242 struct idletimer_tg, pm_nb);
243 if (!timer)
244 return NOTIFY_DONE;
245 switch (pm_event) {
246 case PM_SUSPEND_PREPARE:
247 get_monotonic_boottime(&timer->last_suspend_time);
248 break;
249 case PM_POST_SUSPEND:
250 spin_lock_bh(×tamp_lock);
251 if (!timer->active) {
252 spin_unlock_bh(×tamp_lock);
253 break;
254 }
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));
264 } else {
265 del_timer(&timer->timer);
266 timer->timer.expires = 0;
267 timer->active = false;
268 timer->work_pending = true;
269 schedule_work(&timer->work);
270 }
271 }
272 spin_unlock_bh(×tamp_lock);
273 break;
274 default:
275 break;
276 }
277 return NOTIFY_DONE;
278 }
279
idletimer_check_sysfs_name(const char * name,unsigned int size)280 static int idletimer_check_sysfs_name(const char *name, unsigned int size)
281 {
282 int ret;
283
284 ret = xt_check_proc_name(name, size);
285 if (ret < 0)
286 return ret;
287
288 if (!strcmp(name, "power") ||
289 !strcmp(name, "subsystem") ||
290 !strcmp(name, "uevent"))
291 return -EINVAL;
292
293 return 0;
294 }
295
idletimer_tg_create(struct idletimer_tg_info * info)296 static int idletimer_tg_create(struct idletimer_tg_info *info)
297 {
298 int ret;
299
300 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
301 if (!info->timer) {
302 ret = -ENOMEM;
303 goto out;
304 }
305
306 ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
307 if (ret < 0)
308 goto out_free_timer;
309
310 sysfs_attr_init(&info->timer->attr.attr);
311 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
312 if (!info->timer->attr.attr.name) {
313 ret = -ENOMEM;
314 goto out_free_timer;
315 }
316 info->timer->attr.attr.mode = S_IRUGO;
317 info->timer->attr.show = idletimer_tg_show;
318
319 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
320 if (ret < 0) {
321 pr_debug("couldn't add file to sysfs");
322 goto out_free_attr;
323 }
324
325 list_add(&info->timer->entry, &idletimer_tg_list);
326
327 setup_timer(&info->timer->timer, idletimer_tg_expired,
328 (unsigned long) info->timer);
329 info->timer->refcnt = 1;
330 info->timer->send_nl_msg = (info->send_nl_msg == 0) ? false : true;
331 info->timer->active = true;
332 info->timer->timeout = info->timeout;
333
334 info->timer->delayed_timer_trigger.tv_sec = 0;
335 info->timer->delayed_timer_trigger.tv_nsec = 0;
336 info->timer->work_pending = false;
337 info->timer->uid = 0;
338 get_monotonic_boottime(&info->timer->last_modified_timer);
339
340 info->timer->pm_nb.notifier_call = idletimer_resume;
341 ret = register_pm_notifier(&info->timer->pm_nb);
342 if (ret)
343 printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
344 __func__, ret);
345
346 INIT_WORK(&info->timer->work, idletimer_tg_work);
347
348 mod_timer(&info->timer->timer,
349 msecs_to_jiffies(info->timeout * 1000) + jiffies);
350
351 return 0;
352
353 out_free_attr:
354 kfree(info->timer->attr.attr.name);
355 out_free_timer:
356 kfree(info->timer);
357 out:
358 return ret;
359 }
360
reset_timer(const struct idletimer_tg_info * info,struct sk_buff * skb)361 static void reset_timer(const struct idletimer_tg_info *info,
362 struct sk_buff *skb)
363 {
364 unsigned long now = jiffies;
365 struct idletimer_tg *timer = info->timer;
366 bool timer_prev;
367
368 spin_lock_bh(×tamp_lock);
369 timer_prev = timer->active;
370 timer->active = true;
371 /* timer_prev is used to guard overflow problem in time_before*/
372 if (!timer_prev || time_before(timer->timer.expires, now)) {
373 pr_debug("Starting Checkentry timer (Expired, Jiffies): %lu, %lu\n",
374 timer->timer.expires, now);
375
376 /* Stores the uid resposible for waking up the radio */
377 if (skb && (skb->sk)) {
378 timer->uid = from_kuid_munged(current_user_ns(),
379 sock_i_uid(skb_to_full_sk(skb)));
380 }
381
382 /* checks if there is a pending inactive notification*/
383 if (timer->work_pending)
384 timer->delayed_timer_trigger = timer->last_modified_timer;
385 else {
386 timer->work_pending = true;
387 schedule_work(&timer->work);
388 }
389 }
390
391 get_monotonic_boottime(&timer->last_modified_timer);
392 mod_timer(&timer->timer,
393 msecs_to_jiffies(info->timeout * 1000) + now);
394 spin_unlock_bh(×tamp_lock);
395 }
396
397 /*
398 * The actual xt_tables plugin.
399 */
idletimer_tg_target(struct sk_buff * skb,const struct xt_action_param * par)400 static unsigned int idletimer_tg_target(struct sk_buff *skb,
401 const struct xt_action_param *par)
402 {
403 const struct idletimer_tg_info *info = par->targinfo;
404 unsigned long now = jiffies;
405
406 pr_debug("resetting timer %s, timeout period %u\n",
407 info->label, info->timeout);
408
409 BUG_ON(!info->timer);
410
411 info->timer->active = true;
412
413 if (time_before(info->timer->timer.expires, now)) {
414 schedule_work(&info->timer->work);
415 pr_debug("Starting timer %s (Expired, Jiffies): %lu, %lu\n",
416 info->label, info->timer->timer.expires, now);
417 }
418
419 /* TODO: Avoid modifying timers on each packet */
420 reset_timer(info, skb);
421 return XT_CONTINUE;
422 }
423
idletimer_tg_checkentry(const struct xt_tgchk_param * par)424 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
425 {
426 struct idletimer_tg_info *info = par->targinfo;
427 int ret;
428
429 pr_debug("checkentry targinfo %s\n", info->label);
430
431 if (info->timeout == 0) {
432 pr_debug("timeout value is zero\n");
433 return -EINVAL;
434 }
435 if (info->timeout >= INT_MAX / 1000) {
436 pr_debug("timeout value is too big\n");
437 return -EINVAL;
438 }
439 if (info->label[0] == '\0' ||
440 strnlen(info->label,
441 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
442 pr_debug("label is empty or not nul-terminated\n");
443 return -EINVAL;
444 }
445
446 mutex_lock(&list_mutex);
447
448 info->timer = __idletimer_tg_find_by_label(info->label);
449 if (info->timer) {
450 info->timer->refcnt++;
451 reset_timer(info, NULL);
452 pr_debug("increased refcnt of timer %s to %u\n",
453 info->label, info->timer->refcnt);
454 } else {
455 ret = idletimer_tg_create(info);
456 if (ret < 0) {
457 pr_debug("failed to create timer\n");
458 mutex_unlock(&list_mutex);
459 return ret;
460 }
461 }
462
463 mutex_unlock(&list_mutex);
464
465 return 0;
466 }
467
idletimer_tg_destroy(const struct xt_tgdtor_param * par)468 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
469 {
470 const struct idletimer_tg_info *info = par->targinfo;
471
472 pr_debug("destroy targinfo %s\n", info->label);
473
474 mutex_lock(&list_mutex);
475
476 if (--info->timer->refcnt == 0) {
477 pr_debug("deleting timer %s\n", info->label);
478
479 list_del(&info->timer->entry);
480 del_timer_sync(&info->timer->timer);
481 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
482 unregister_pm_notifier(&info->timer->pm_nb);
483 cancel_work_sync(&info->timer->work);
484 kfree(info->timer->attr.attr.name);
485 kfree(info->timer);
486 } else {
487 pr_debug("decreased refcnt of timer %s to %u\n",
488 info->label, info->timer->refcnt);
489 }
490
491 mutex_unlock(&list_mutex);
492 }
493
494 static struct xt_target idletimer_tg __read_mostly = {
495 .name = "IDLETIMER",
496 .revision = 1,
497 .family = NFPROTO_UNSPEC,
498 .target = idletimer_tg_target,
499 .targetsize = sizeof(struct idletimer_tg_info),
500 .usersize = offsetof(struct idletimer_tg_info, timer),
501 .checkentry = idletimer_tg_checkentry,
502 .destroy = idletimer_tg_destroy,
503 .me = THIS_MODULE,
504 };
505
506 static struct class *idletimer_tg_class;
507
508 static struct device *idletimer_tg_device;
509
idletimer_tg_init(void)510 static int __init idletimer_tg_init(void)
511 {
512 int err;
513
514 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
515 err = PTR_ERR(idletimer_tg_class);
516 if (IS_ERR(idletimer_tg_class)) {
517 pr_debug("couldn't register device class\n");
518 goto out;
519 }
520
521 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
522 MKDEV(0, 0), NULL, "timers");
523 err = PTR_ERR(idletimer_tg_device);
524 if (IS_ERR(idletimer_tg_device)) {
525 pr_debug("couldn't register system device\n");
526 goto out_class;
527 }
528
529 idletimer_tg_kobj = &idletimer_tg_device->kobj;
530
531 err = xt_register_target(&idletimer_tg);
532 if (err < 0) {
533 pr_debug("couldn't register xt target\n");
534 goto out_dev;
535 }
536
537 return 0;
538 out_dev:
539 device_destroy(idletimer_tg_class, MKDEV(0, 0));
540 out_class:
541 class_destroy(idletimer_tg_class);
542 out:
543 return err;
544 }
545
idletimer_tg_exit(void)546 static void __exit idletimer_tg_exit(void)
547 {
548 xt_unregister_target(&idletimer_tg);
549
550 device_destroy(idletimer_tg_class, MKDEV(0, 0));
551 class_destroy(idletimer_tg_class);
552 }
553
554 module_init(idletimer_tg_init);
555 module_exit(idletimer_tg_exit);
556
557 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
558 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
559 MODULE_DESCRIPTION("Xtables: idle time monitor");
560 MODULE_LICENSE("GPL v2");
561 MODULE_ALIAS("ipt_IDLETIMER");
562 MODULE_ALIAS("ip6t_IDLETIMER");
563 MODULE_ALIAS("arpt_IDLETIMER");
564