blob: 921c9bd7e1e74f14b956794cb8b6c69dde8988c1 [file] [log] [blame]
Luciano Coelho0902b462010-06-15 15:04:00 +02001/*
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 * Written by Timo Teras <ext-timo.teras@nokia.com>
9 *
10 * Converted to x_tables and reworked for upstream inclusion
11 * by Luciano Coelho <luciano.coelho@nokia.com>
12 *
13 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 * 02110-1301 USA
28 */
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <linux/module.h>
33#include <linux/timer.h>
34#include <linux/list.h>
35#include <linux/mutex.h>
36#include <linux/netfilter.h>
37#include <linux/netfilter/x_tables.h>
38#include <linux/netfilter/xt_IDLETIMER.h>
Randy Dunlap600069d2010-06-22 08:13:31 +020039#include <linux/kdev_t.h>
Luciano Coelho0902b462010-06-15 15:04:00 +020040#include <linux/kobject.h>
41#include <linux/workqueue.h>
42#include <linux/sysfs.h>
43
44struct idletimer_tg_attr {
45 struct attribute attr;
46 ssize_t (*show)(struct kobject *kobj,
47 struct attribute *attr, char *buf);
48};
49
50struct idletimer_tg {
51 struct list_head entry;
52 struct timer_list timer;
53 struct work_struct work;
54
55 struct kobject *kobj;
56 struct idletimer_tg_attr attr;
57
58 unsigned int refcnt;
59};
60
61static LIST_HEAD(idletimer_tg_list);
62static DEFINE_MUTEX(list_mutex);
63
64static struct kobject *idletimer_tg_kobj;
65
66static
67struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
68{
69 struct idletimer_tg *entry;
70
71 BUG_ON(!label);
72
73 list_for_each_entry(entry, &idletimer_tg_list, entry) {
74 if (!strcmp(label, entry->attr.attr.name))
75 return entry;
76 }
77
78 return NULL;
79}
80
81static ssize_t idletimer_tg_show(struct kobject *kobj, struct attribute *attr,
82 char *buf)
83{
84 struct idletimer_tg *timer;
85 unsigned long expires = 0;
86
87 mutex_lock(&list_mutex);
88
89 timer = __idletimer_tg_find_by_label(attr->name);
90 if (timer)
91 expires = timer->timer.expires;
92
93 mutex_unlock(&list_mutex);
94
95 if (time_after(expires, jiffies))
96 return sprintf(buf, "%u\n",
97 jiffies_to_msecs(expires - jiffies) / 1000);
98
99 return sprintf(buf, "0\n");
100}
101
102static void idletimer_tg_work(struct work_struct *work)
103{
104 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
105 work);
106
107 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
108}
109
110static void idletimer_tg_expired(unsigned long data)
111{
112 struct idletimer_tg *timer = (struct idletimer_tg *) data;
113
114 pr_debug("timer %s expired\n", timer->attr.attr.name);
115
116 schedule_work(&timer->work);
117}
118
Taehee Yoof184d302018-10-21 00:00:08 +0900119static int idletimer_check_sysfs_name(const char *name, unsigned int size)
120{
121 int ret;
122
123 ret = xt_check_proc_name(name, size);
124 if (ret < 0)
125 return ret;
126
127 if (!strcmp(name, "power") ||
128 !strcmp(name, "subsystem") ||
129 !strcmp(name, "uevent"))
130 return -EINVAL;
131
132 return 0;
133}
134
Luciano Coelho0902b462010-06-15 15:04:00 +0200135static int idletimer_tg_create(struct idletimer_tg_info *info)
136{
137 int ret;
138
139 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
140 if (!info->timer) {
Luciano Coelho0902b462010-06-15 15:04:00 +0200141 ret = -ENOMEM;
142 goto out;
143 }
144
Taehee Yoof184d302018-10-21 00:00:08 +0900145 ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
146 if (ret < 0)
147 goto out_free_timer;
148
Dmitry Torokhov484836e2015-07-09 17:15:01 -0700149 sysfs_attr_init(&info->timer->attr.attr);
Luciano Coelho0902b462010-06-15 15:04:00 +0200150 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
151 if (!info->timer->attr.attr.name) {
Luciano Coelho0902b462010-06-15 15:04:00 +0200152 ret = -ENOMEM;
153 goto out_free_timer;
154 }
155 info->timer->attr.attr.mode = S_IRUGO;
156 info->timer->attr.show = idletimer_tg_show;
157
158 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
159 if (ret < 0) {
160 pr_debug("couldn't add file to sysfs");
161 goto out_free_attr;
162 }
163
164 list_add(&info->timer->entry, &idletimer_tg_list);
165
166 setup_timer(&info->timer->timer, idletimer_tg_expired,
167 (unsigned long) info->timer);
168 info->timer->refcnt = 1;
169
Eric Dumazet034ad9d2018-02-16 19:36:28 -0800170 INIT_WORK(&info->timer->work, idletimer_tg_work);
171
Luciano Coelho0902b462010-06-15 15:04:00 +0200172 mod_timer(&info->timer->timer,
173 msecs_to_jiffies(info->timeout * 1000) + jiffies);
174
Luciano Coelho0902b462010-06-15 15:04:00 +0200175 return 0;
176
177out_free_attr:
178 kfree(info->timer->attr.attr.name);
179out_free_timer:
180 kfree(info->timer);
181out:
182 return ret;
183}
184
185/*
186 * The actual xt_tables plugin.
187 */
188static unsigned int idletimer_tg_target(struct sk_buff *skb,
189 const struct xt_action_param *par)
190{
191 const struct idletimer_tg_info *info = par->targinfo;
192
193 pr_debug("resetting timer %s, timeout period %u\n",
194 info->label, info->timeout);
195
196 BUG_ON(!info->timer);
197
198 mod_timer(&info->timer->timer,
199 msecs_to_jiffies(info->timeout * 1000) + jiffies);
200
201 return XT_CONTINUE;
202}
203
204static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
205{
206 struct idletimer_tg_info *info = par->targinfo;
207 int ret;
208
209 pr_debug("checkentry targinfo%s\n", info->label);
210
211 if (info->timeout == 0) {
212 pr_debug("timeout value is zero\n");
213 return -EINVAL;
214 }
Eric Dumazet034ad9d2018-02-16 19:36:28 -0800215 if (info->timeout >= INT_MAX / 1000) {
216 pr_debug("timeout value is too big\n");
217 return -EINVAL;
218 }
Luciano Coelho0902b462010-06-15 15:04:00 +0200219 if (info->label[0] == '\0' ||
220 strnlen(info->label,
221 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
222 pr_debug("label is empty or not nul-terminated\n");
223 return -EINVAL;
224 }
225
226 mutex_lock(&list_mutex);
227
228 info->timer = __idletimer_tg_find_by_label(info->label);
229 if (info->timer) {
230 info->timer->refcnt++;
231 mod_timer(&info->timer->timer,
232 msecs_to_jiffies(info->timeout * 1000) + jiffies);
233
234 pr_debug("increased refcnt of timer %s to %u\n",
235 info->label, info->timer->refcnt);
236 } else {
237 ret = idletimer_tg_create(info);
238 if (ret < 0) {
239 pr_debug("failed to create timer\n");
240 mutex_unlock(&list_mutex);
241 return ret;
242 }
243 }
244
245 mutex_unlock(&list_mutex);
246 return 0;
247}
248
249static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
250{
251 const struct idletimer_tg_info *info = par->targinfo;
252
253 pr_debug("destroy targinfo %s\n", info->label);
254
255 mutex_lock(&list_mutex);
256
257 if (--info->timer->refcnt == 0) {
258 pr_debug("deleting timer %s\n", info->label);
259
260 list_del(&info->timer->entry);
261 del_timer_sync(&info->timer->timer);
Liping Zhangcec59132016-04-21 00:47:08 -0700262 cancel_work_sync(&info->timer->work);
Luciano Coelho0902b462010-06-15 15:04:00 +0200263 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
264 kfree(info->timer->attr.attr.name);
265 kfree(info->timer);
266 } else {
267 pr_debug("decreased refcnt of timer %s to %u\n",
268 info->label, info->timer->refcnt);
269 }
270
271 mutex_unlock(&list_mutex);
272}
273
274static struct xt_target idletimer_tg __read_mostly = {
275 .name = "IDLETIMER",
276 .family = NFPROTO_UNSPEC,
277 .target = idletimer_tg_target,
278 .targetsize = sizeof(struct idletimer_tg_info),
279 .checkentry = idletimer_tg_checkentry,
280 .destroy = idletimer_tg_destroy,
281 .me = THIS_MODULE,
282};
283
284static struct class *idletimer_tg_class;
285
286static struct device *idletimer_tg_device;
287
288static int __init idletimer_tg_init(void)
289{
290 int err;
291
292 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
293 err = PTR_ERR(idletimer_tg_class);
294 if (IS_ERR(idletimer_tg_class)) {
295 pr_debug("couldn't register device class\n");
296 goto out;
297 }
298
299 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
300 MKDEV(0, 0), NULL, "timers");
301 err = PTR_ERR(idletimer_tg_device);
302 if (IS_ERR(idletimer_tg_device)) {
303 pr_debug("couldn't register system device\n");
304 goto out_class;
305 }
306
307 idletimer_tg_kobj = &idletimer_tg_device->kobj;
308
309 err = xt_register_target(&idletimer_tg);
310 if (err < 0) {
311 pr_debug("couldn't register xt target\n");
312 goto out_dev;
313 }
314
315 return 0;
316out_dev:
317 device_destroy(idletimer_tg_class, MKDEV(0, 0));
318out_class:
319 class_destroy(idletimer_tg_class);
320out:
321 return err;
322}
323
324static void __exit idletimer_tg_exit(void)
325{
326 xt_unregister_target(&idletimer_tg);
327
328 device_destroy(idletimer_tg_class, MKDEV(0, 0));
329 class_destroy(idletimer_tg_class);
330}
331
332module_init(idletimer_tg_init);
333module_exit(idletimer_tg_exit);
334
335MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
336MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
337MODULE_DESCRIPTION("Xtables: idle time monitor");
338MODULE_LICENSE("GPL v2");
Jan Engelhardtf1e231a2011-01-18 06:30:13 +0100339MODULE_ALIAS("ipt_IDLETIMER");
340MODULE_ALIAS("ip6t_IDLETIMER");