• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(&timestamp_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(&timestamp_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(&timestamp_lock);
230 	timer->active = false;
231 	timer->work_pending = true;
232 	schedule_work(&timer->work);
233 	spin_unlock_bh(&timestamp_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(&timestamp_lock);
251 		if (!timer->active) {
252 			spin_unlock_bh(&timestamp_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(&timestamp_lock);
273 		break;
274 	default:
275 		break;
276 	}
277 	return NOTIFY_DONE;
278 }
279 
idletimer_tg_create(struct idletimer_tg_info * info)280 static int idletimer_tg_create(struct idletimer_tg_info *info)
281 {
282 	int ret;
283 
284 	info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
285 	if (!info->timer) {
286 		ret = -ENOMEM;
287 		goto out;
288 	}
289 
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) {
293 		ret = -ENOMEM;
294 		goto out_free_timer;
295 	}
296 	info->timer->attr.attr.mode = S_IRUGO;
297 	info->timer->attr.show = idletimer_tg_show;
298 
299 	ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
300 	if (ret < 0) {
301 		pr_debug("couldn't add file to sysfs");
302 		goto out_free_attr;
303 	}
304 
305 	list_add(&info->timer->entry, &idletimer_tg_list);
306 
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;
313 
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);
319 
320 	info->timer->pm_nb.notifier_call = idletimer_resume;
321 	ret = register_pm_notifier(&info->timer->pm_nb);
322 	if (ret)
323 		printk(KERN_WARNING "[%s] Failed to register pm notifier %d\n",
324 				__func__, ret);
325 
326 	INIT_WORK(&info->timer->work, idletimer_tg_work);
327 
328 	mod_timer(&info->timer->timer,
329 		  msecs_to_jiffies(info->timeout * 1000) + jiffies);
330 
331 	return 0;
332 
333 out_free_attr:
334 	kfree(info->timer->attr.attr.name);
335 out_free_timer:
336 	kfree(info->timer);
337 out:
338 	return ret;
339 }
340 
reset_timer(const struct idletimer_tg_info * info,struct sk_buff * skb)341 static void reset_timer(const struct idletimer_tg_info *info,
342 			struct sk_buff *skb)
343 {
344 	unsigned long now = jiffies;
345 	struct idletimer_tg *timer = info->timer;
346 	bool timer_prev;
347 
348 	spin_lock_bh(&timestamp_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);
355 
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)));
360 		}
361 
362 		/* checks if there is a pending inactive notification*/
363 		if (timer->work_pending)
364 			timer->delayed_timer_trigger = timer->last_modified_timer;
365 		else {
366 			timer->work_pending = true;
367 			schedule_work(&timer->work);
368 		}
369 	}
370 
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(&timestamp_lock);
375 }
376 
377 /*
378  * The actual xt_tables plugin.
379  */
idletimer_tg_target(struct sk_buff * skb,const struct xt_action_param * par)380 static unsigned int idletimer_tg_target(struct sk_buff *skb,
381 					 const struct xt_action_param *par)
382 {
383 	const struct idletimer_tg_info *info = par->targinfo;
384 	unsigned long now = jiffies;
385 
386 	pr_debug("resetting timer %s, timeout period %u\n",
387 		 info->label, info->timeout);
388 
389 	BUG_ON(!info->timer);
390 
391 	info->timer->active = true;
392 
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);
397 	}
398 
399 	/* TODO: Avoid modifying timers on each packet */
400 	reset_timer(info, skb);
401 	return XT_CONTINUE;
402 }
403 
idletimer_tg_checkentry(const struct xt_tgchk_param * par)404 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
405 {
406 	struct idletimer_tg_info *info = par->targinfo;
407 	int ret;
408 
409 	pr_debug("checkentry targinfo %s\n", info->label);
410 
411 	if (info->timeout == 0) {
412 		pr_debug("timeout value is zero\n");
413 		return -EINVAL;
414 	}
415 	if (info->timeout >= INT_MAX / 1000) {
416 		pr_debug("timeout value is too big\n");
417 		return -EINVAL;
418 	}
419 	if (info->label[0] == '\0' ||
420 	    strnlen(info->label,
421 		    MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
422 		pr_debug("label is empty or not nul-terminated\n");
423 		return -EINVAL;
424 	}
425 
426 	mutex_lock(&list_mutex);
427 
428 	info->timer = __idletimer_tg_find_by_label(info->label);
429 	if (info->timer) {
430 		info->timer->refcnt++;
431 		reset_timer(info, NULL);
432 		pr_debug("increased refcnt of timer %s to %u\n",
433 			 info->label, info->timer->refcnt);
434 	} else {
435 		ret = idletimer_tg_create(info);
436 		if (ret < 0) {
437 			pr_debug("failed to create timer\n");
438 			mutex_unlock(&list_mutex);
439 			return ret;
440 		}
441 	}
442 
443 	mutex_unlock(&list_mutex);
444 
445 	return 0;
446 }
447 
idletimer_tg_destroy(const struct xt_tgdtor_param * par)448 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
449 {
450 	const struct idletimer_tg_info *info = par->targinfo;
451 
452 	pr_debug("destroy targinfo %s\n", info->label);
453 
454 	mutex_lock(&list_mutex);
455 
456 	if (--info->timer->refcnt == 0) {
457 		pr_debug("deleting timer %s\n", info->label);
458 
459 		list_del(&info->timer->entry);
460 		del_timer_sync(&info->timer->timer);
461 		sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
462 		unregister_pm_notifier(&info->timer->pm_nb);
463 		cancel_work_sync(&info->timer->work);
464 		kfree(info->timer->attr.attr.name);
465 		kfree(info->timer);
466 	} else {
467 		pr_debug("decreased refcnt of timer %s to %u\n",
468 		info->label, info->timer->refcnt);
469 	}
470 
471 	mutex_unlock(&list_mutex);
472 }
473 
474 static struct xt_target idletimer_tg __read_mostly = {
475 	.name		= "IDLETIMER",
476 	.revision	= 1,
477 	.family		= NFPROTO_UNSPEC,
478 	.target		= idletimer_tg_target,
479 	.targetsize     = sizeof(struct idletimer_tg_info),
480 	.checkentry	= idletimer_tg_checkentry,
481 	.destroy        = idletimer_tg_destroy,
482 	.me		= THIS_MODULE,
483 };
484 
485 static struct class *idletimer_tg_class;
486 
487 static struct device *idletimer_tg_device;
488 
idletimer_tg_init(void)489 static int __init idletimer_tg_init(void)
490 {
491 	int err;
492 
493 	idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
494 	err = PTR_ERR(idletimer_tg_class);
495 	if (IS_ERR(idletimer_tg_class)) {
496 		pr_debug("couldn't register device class\n");
497 		goto out;
498 	}
499 
500 	idletimer_tg_device = device_create(idletimer_tg_class, NULL,
501 					    MKDEV(0, 0), NULL, "timers");
502 	err = PTR_ERR(idletimer_tg_device);
503 	if (IS_ERR(idletimer_tg_device)) {
504 		pr_debug("couldn't register system device\n");
505 		goto out_class;
506 	}
507 
508 	idletimer_tg_kobj = &idletimer_tg_device->kobj;
509 
510 	err =  xt_register_target(&idletimer_tg);
511 	if (err < 0) {
512 		pr_debug("couldn't register xt target\n");
513 		goto out_dev;
514 	}
515 
516 	return 0;
517 out_dev:
518 	device_destroy(idletimer_tg_class, MKDEV(0, 0));
519 out_class:
520 	class_destroy(idletimer_tg_class);
521 out:
522 	return err;
523 }
524 
idletimer_tg_exit(void)525 static void __exit idletimer_tg_exit(void)
526 {
527 	xt_unregister_target(&idletimer_tg);
528 
529 	device_destroy(idletimer_tg_class, MKDEV(0, 0));
530 	class_destroy(idletimer_tg_class);
531 }
532 
533 module_init(idletimer_tg_init);
534 module_exit(idletimer_tg_exit);
535 
536 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
537 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
538 MODULE_DESCRIPTION("Xtables: idle time monitor");
539 MODULE_LICENSE("GPL v2");
540 MODULE_ALIAS("ipt_IDLETIMER");
541 MODULE_ALIAS("ip6t_IDLETIMER");
542 MODULE_ALIAS("arpt_IDLETIMER");
543