Loading...
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 * 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>
39#include <linux/kdev_t.h>
40#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
119static int idletimer_tg_create(struct idletimer_tg_info *info)
120{
121 int ret;
122
123 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
124 if (!info->timer) {
125 ret = -ENOMEM;
126 goto out;
127 }
128
129 sysfs_attr_init(&info->timer->attr.attr);
130 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
131 if (!info->timer->attr.attr.name) {
132 ret = -ENOMEM;
133 goto out_free_timer;
134 }
135 info->timer->attr.attr.mode = S_IRUGO;
136 info->timer->attr.show = idletimer_tg_show;
137
138 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
139 if (ret < 0) {
140 pr_debug("couldn't add file to sysfs");
141 goto out_free_attr;
142 }
143
144 list_add(&info->timer->entry, &idletimer_tg_list);
145
146 setup_timer(&info->timer->timer, idletimer_tg_expired,
147 (unsigned long) info->timer);
148 info->timer->refcnt = 1;
149
150 mod_timer(&info->timer->timer,
151 msecs_to_jiffies(info->timeout * 1000) + jiffies);
152
153 INIT_WORK(&info->timer->work, idletimer_tg_work);
154
155 return 0;
156
157out_free_attr:
158 kfree(info->timer->attr.attr.name);
159out_free_timer:
160 kfree(info->timer);
161out:
162 return ret;
163}
164
165/*
166 * The actual xt_tables plugin.
167 */
168static unsigned int idletimer_tg_target(struct sk_buff *skb,
169 const struct xt_action_param *par)
170{
171 const struct idletimer_tg_info *info = par->targinfo;
172
173 pr_debug("resetting timer %s, timeout period %u\n",
174 info->label, info->timeout);
175
176 BUG_ON(!info->timer);
177
178 mod_timer(&info->timer->timer,
179 msecs_to_jiffies(info->timeout * 1000) + jiffies);
180
181 return XT_CONTINUE;
182}
183
184static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
185{
186 struct idletimer_tg_info *info = par->targinfo;
187 int ret;
188
189 pr_debug("checkentry targinfo%s\n", info->label);
190
191 if (info->timeout == 0) {
192 pr_debug("timeout value is zero\n");
193 return -EINVAL;
194 }
195
196 if (info->label[0] == '\0' ||
197 strnlen(info->label,
198 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
199 pr_debug("label is empty or not nul-terminated\n");
200 return -EINVAL;
201 }
202
203 mutex_lock(&list_mutex);
204
205 info->timer = __idletimer_tg_find_by_label(info->label);
206 if (info->timer) {
207 info->timer->refcnt++;
208 mod_timer(&info->timer->timer,
209 msecs_to_jiffies(info->timeout * 1000) + jiffies);
210
211 pr_debug("increased refcnt of timer %s to %u\n",
212 info->label, info->timer->refcnt);
213 } else {
214 ret = idletimer_tg_create(info);
215 if (ret < 0) {
216 pr_debug("failed to create timer\n");
217 mutex_unlock(&list_mutex);
218 return ret;
219 }
220 }
221
222 mutex_unlock(&list_mutex);
223 return 0;
224}
225
226static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
227{
228 const struct idletimer_tg_info *info = par->targinfo;
229
230 pr_debug("destroy targinfo %s\n", info->label);
231
232 mutex_lock(&list_mutex);
233
234 if (--info->timer->refcnt == 0) {
235 pr_debug("deleting timer %s\n", info->label);
236
237 list_del(&info->timer->entry);
238 del_timer_sync(&info->timer->timer);
239 cancel_work_sync(&info->timer->work);
240 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
241 kfree(info->timer->attr.attr.name);
242 kfree(info->timer);
243 } else {
244 pr_debug("decreased refcnt of timer %s to %u\n",
245 info->label, info->timer->refcnt);
246 }
247
248 mutex_unlock(&list_mutex);
249}
250
251static struct xt_target idletimer_tg __read_mostly = {
252 .name = "IDLETIMER",
253 .family = NFPROTO_UNSPEC,
254 .target = idletimer_tg_target,
255 .targetsize = sizeof(struct idletimer_tg_info),
256 .checkentry = idletimer_tg_checkentry,
257 .destroy = idletimer_tg_destroy,
258 .me = THIS_MODULE,
259};
260
261static struct class *idletimer_tg_class;
262
263static struct device *idletimer_tg_device;
264
265static int __init idletimer_tg_init(void)
266{
267 int err;
268
269 idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
270 err = PTR_ERR(idletimer_tg_class);
271 if (IS_ERR(idletimer_tg_class)) {
272 pr_debug("couldn't register device class\n");
273 goto out;
274 }
275
276 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
277 MKDEV(0, 0), NULL, "timers");
278 err = PTR_ERR(idletimer_tg_device);
279 if (IS_ERR(idletimer_tg_device)) {
280 pr_debug("couldn't register system device\n");
281 goto out_class;
282 }
283
284 idletimer_tg_kobj = &idletimer_tg_device->kobj;
285
286 err = xt_register_target(&idletimer_tg);
287 if (err < 0) {
288 pr_debug("couldn't register xt target\n");
289 goto out_dev;
290 }
291
292 return 0;
293out_dev:
294 device_destroy(idletimer_tg_class, MKDEV(0, 0));
295out_class:
296 class_destroy(idletimer_tg_class);
297out:
298 return err;
299}
300
301static void __exit idletimer_tg_exit(void)
302{
303 xt_unregister_target(&idletimer_tg);
304
305 device_destroy(idletimer_tg_class, MKDEV(0, 0));
306 class_destroy(idletimer_tg_class);
307}
308
309module_init(idletimer_tg_init);
310module_exit(idletimer_tg_exit);
311
312MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
313MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
314MODULE_DESCRIPTION("Xtables: idle time monitor");
315MODULE_LICENSE("GPL v2");
316MODULE_ALIAS("ipt_IDLETIMER");
317MODULE_ALIAS("ip6t_IDLETIMER");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/net/netfilter/xt_IDLETIMER.c
4 *
5 * Netfilter module to trigger a timer when packet matches.
6 * After timer expires a kevent will be sent.
7 *
8 * Copyright (C) 2004, 2010 Nokia Corporation
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
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/module.h>
20#include <linux/timer.h>
21#include <linux/alarmtimer.h>
22#include <linux/list.h>
23#include <linux/mutex.h>
24#include <linux/netfilter.h>
25#include <linux/netfilter/x_tables.h>
26#include <linux/netfilter/xt_IDLETIMER.h>
27#include <linux/kdev_t.h>
28#include <linux/kobject.h>
29#include <linux/workqueue.h>
30#include <linux/sysfs.h>
31
32struct idletimer_tg {
33 struct list_head entry;
34 struct alarm alarm;
35 struct timer_list timer;
36 struct work_struct work;
37
38 struct kobject *kobj;
39 struct device_attribute attr;
40
41 unsigned int refcnt;
42 u8 timer_type;
43};
44
45static LIST_HEAD(idletimer_tg_list);
46static DEFINE_MUTEX(list_mutex);
47
48static struct kobject *idletimer_tg_kobj;
49
50static
51struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
52{
53 struct idletimer_tg *entry;
54
55 list_for_each_entry(entry, &idletimer_tg_list, entry) {
56 if (!strcmp(label, entry->attr.attr.name))
57 return entry;
58 }
59
60 return NULL;
61}
62
63static ssize_t idletimer_tg_show(struct device *dev,
64 struct device_attribute *attr, char *buf)
65{
66 struct idletimer_tg *timer;
67 unsigned long expires = 0;
68 struct timespec64 ktimespec = {};
69 long time_diff = 0;
70
71 mutex_lock(&list_mutex);
72
73 timer = __idletimer_tg_find_by_label(attr->attr.name);
74 if (timer) {
75 if (timer->timer_type & XT_IDLETIMER_ALARM) {
76 ktime_t expires_alarm = alarm_expires_remaining(&timer->alarm);
77 ktimespec = ktime_to_timespec64(expires_alarm);
78 time_diff = ktimespec.tv_sec;
79 } else {
80 expires = timer->timer.expires;
81 time_diff = jiffies_to_msecs(expires - jiffies) / 1000;
82 }
83 }
84
85 mutex_unlock(&list_mutex);
86
87 if (time_after(expires, jiffies) || ktimespec.tv_sec > 0)
88 return sysfs_emit(buf, "%ld\n", time_diff);
89
90 return sysfs_emit(buf, "0\n");
91}
92
93static void idletimer_tg_work(struct work_struct *work)
94{
95 struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
96 work);
97
98 sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
99}
100
101static void idletimer_tg_expired(struct timer_list *t)
102{
103 struct idletimer_tg *timer = from_timer(timer, t, timer);
104
105 pr_debug("timer %s expired\n", timer->attr.attr.name);
106
107 schedule_work(&timer->work);
108}
109
110static void idletimer_tg_alarmproc(struct alarm *alarm, ktime_t now)
111{
112 struct idletimer_tg *timer = alarm->data;
113
114 pr_debug("alarm %s expired\n", timer->attr.attr.name);
115 schedule_work(&timer->work);
116}
117
118static int idletimer_check_sysfs_name(const char *name, unsigned int size)
119{
120 int ret;
121
122 ret = xt_check_proc_name(name, size);
123 if (ret < 0)
124 return ret;
125
126 if (!strcmp(name, "power") ||
127 !strcmp(name, "subsystem") ||
128 !strcmp(name, "uevent"))
129 return -EINVAL;
130
131 return 0;
132}
133
134static int idletimer_tg_create(struct idletimer_tg_info *info)
135{
136 int ret;
137
138 info->timer = kzalloc(sizeof(*info->timer), GFP_KERNEL);
139 if (!info->timer) {
140 ret = -ENOMEM;
141 goto out;
142 }
143
144 ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
145 if (ret < 0)
146 goto out_free_timer;
147
148 sysfs_attr_init(&info->timer->attr.attr);
149 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
150 if (!info->timer->attr.attr.name) {
151 ret = -ENOMEM;
152 goto out_free_timer;
153 }
154 info->timer->attr.attr.mode = 0444;
155 info->timer->attr.show = idletimer_tg_show;
156
157 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
158 if (ret < 0) {
159 pr_debug("couldn't add file to sysfs");
160 goto out_free_attr;
161 }
162
163 list_add(&info->timer->entry, &idletimer_tg_list);
164
165 timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
166 info->timer->refcnt = 1;
167
168 INIT_WORK(&info->timer->work, idletimer_tg_work);
169
170 mod_timer(&info->timer->timer,
171 msecs_to_jiffies(info->timeout * 1000) + jiffies);
172
173 return 0;
174
175out_free_attr:
176 kfree(info->timer->attr.attr.name);
177out_free_timer:
178 kfree(info->timer);
179out:
180 return ret;
181}
182
183static int idletimer_tg_create_v1(struct idletimer_tg_info_v1 *info)
184{
185 int ret;
186
187 info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
188 if (!info->timer) {
189 ret = -ENOMEM;
190 goto out;
191 }
192
193 ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
194 if (ret < 0)
195 goto out_free_timer;
196
197 sysfs_attr_init(&info->timer->attr.attr);
198 info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
199 if (!info->timer->attr.attr.name) {
200 ret = -ENOMEM;
201 goto out_free_timer;
202 }
203 info->timer->attr.attr.mode = 0444;
204 info->timer->attr.show = idletimer_tg_show;
205
206 ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
207 if (ret < 0) {
208 pr_debug("couldn't add file to sysfs");
209 goto out_free_attr;
210 }
211
212 /* notify userspace */
213 kobject_uevent(idletimer_tg_kobj,KOBJ_ADD);
214
215 list_add(&info->timer->entry, &idletimer_tg_list);
216 pr_debug("timer type value is %u", info->timer_type);
217 info->timer->timer_type = info->timer_type;
218 info->timer->refcnt = 1;
219
220 INIT_WORK(&info->timer->work, idletimer_tg_work);
221
222 if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
223 ktime_t tout;
224 alarm_init(&info->timer->alarm, ALARM_BOOTTIME,
225 idletimer_tg_alarmproc);
226 info->timer->alarm.data = info->timer;
227 tout = ktime_set(info->timeout, 0);
228 alarm_start_relative(&info->timer->alarm, tout);
229 } else {
230 timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
231 mod_timer(&info->timer->timer,
232 msecs_to_jiffies(info->timeout * 1000) + jiffies);
233 }
234
235 return 0;
236
237out_free_attr:
238 kfree(info->timer->attr.attr.name);
239out_free_timer:
240 kfree(info->timer);
241out:
242 return ret;
243}
244
245/*
246 * The actual xt_tables plugin.
247 */
248static unsigned int idletimer_tg_target(struct sk_buff *skb,
249 const struct xt_action_param *par)
250{
251 const struct idletimer_tg_info *info = par->targinfo;
252
253 pr_debug("resetting timer %s, timeout period %u\n",
254 info->label, info->timeout);
255
256 mod_timer(&info->timer->timer,
257 msecs_to_jiffies(info->timeout * 1000) + jiffies);
258
259 return XT_CONTINUE;
260}
261
262/*
263 * The actual xt_tables plugin.
264 */
265static unsigned int idletimer_tg_target_v1(struct sk_buff *skb,
266 const struct xt_action_param *par)
267{
268 const struct idletimer_tg_info_v1 *info = par->targinfo;
269
270 pr_debug("resetting timer %s, timeout period %u\n",
271 info->label, info->timeout);
272
273 if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
274 ktime_t tout = ktime_set(info->timeout, 0);
275 alarm_start_relative(&info->timer->alarm, tout);
276 } else {
277 mod_timer(&info->timer->timer,
278 msecs_to_jiffies(info->timeout * 1000) + jiffies);
279 }
280
281 return XT_CONTINUE;
282}
283
284static int idletimer_tg_helper(struct idletimer_tg_info *info)
285{
286 if (info->timeout == 0) {
287 pr_debug("timeout value is zero\n");
288 return -EINVAL;
289 }
290 if (info->timeout >= INT_MAX / 1000) {
291 pr_debug("timeout value is too big\n");
292 return -EINVAL;
293 }
294 if (info->label[0] == '\0' ||
295 strnlen(info->label,
296 MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
297 pr_debug("label is empty or not nul-terminated\n");
298 return -EINVAL;
299 }
300 return 0;
301}
302
303
304static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
305{
306 struct idletimer_tg_info *info = par->targinfo;
307 int ret;
308
309 pr_debug("checkentry targinfo%s\n", info->label);
310
311 ret = idletimer_tg_helper(info);
312 if(ret < 0)
313 {
314 pr_debug("checkentry helper return invalid\n");
315 return -EINVAL;
316 }
317 mutex_lock(&list_mutex);
318
319 info->timer = __idletimer_tg_find_by_label(info->label);
320 if (info->timer) {
321 info->timer->refcnt++;
322 mod_timer(&info->timer->timer,
323 msecs_to_jiffies(info->timeout * 1000) + jiffies);
324
325 pr_debug("increased refcnt of timer %s to %u\n",
326 info->label, info->timer->refcnt);
327 } else {
328 ret = idletimer_tg_create(info);
329 if (ret < 0) {
330 pr_debug("failed to create timer\n");
331 mutex_unlock(&list_mutex);
332 return ret;
333 }
334 }
335
336 mutex_unlock(&list_mutex);
337 return 0;
338}
339
340static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param *par)
341{
342 struct idletimer_tg_info_v1 *info = par->targinfo;
343 int ret;
344
345 pr_debug("checkentry targinfo%s\n", info->label);
346
347 if (info->send_nl_msg)
348 return -EOPNOTSUPP;
349
350 ret = idletimer_tg_helper((struct idletimer_tg_info *)info);
351 if(ret < 0)
352 {
353 pr_debug("checkentry helper return invalid\n");
354 return -EINVAL;
355 }
356
357 if (info->timer_type > XT_IDLETIMER_ALARM) {
358 pr_debug("invalid value for timer type\n");
359 return -EINVAL;
360 }
361
362 mutex_lock(&list_mutex);
363
364 info->timer = __idletimer_tg_find_by_label(info->label);
365 if (info->timer) {
366 if (info->timer->timer_type != info->timer_type) {
367 pr_debug("Adding/Replacing rule with same label and different timer type is not allowed\n");
368 mutex_unlock(&list_mutex);
369 return -EINVAL;
370 }
371
372 info->timer->refcnt++;
373 if (info->timer_type & XT_IDLETIMER_ALARM) {
374 /* calculate remaining expiry time */
375 ktime_t tout = alarm_expires_remaining(&info->timer->alarm);
376 struct timespec64 ktimespec = ktime_to_timespec64(tout);
377
378 if (ktimespec.tv_sec > 0) {
379 pr_debug("time_expiry_remaining %lld\n",
380 ktimespec.tv_sec);
381 alarm_start_relative(&info->timer->alarm, tout);
382 }
383 } else {
384 mod_timer(&info->timer->timer,
385 msecs_to_jiffies(info->timeout * 1000) + jiffies);
386 }
387 pr_debug("increased refcnt of timer %s to %u\n",
388 info->label, info->timer->refcnt);
389 } else {
390 ret = idletimer_tg_create_v1(info);
391 if (ret < 0) {
392 pr_debug("failed to create timer\n");
393 mutex_unlock(&list_mutex);
394 return ret;
395 }
396 }
397
398 mutex_unlock(&list_mutex);
399 return 0;
400}
401
402static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
403{
404 const struct idletimer_tg_info *info = par->targinfo;
405
406 pr_debug("destroy targinfo %s\n", info->label);
407
408 mutex_lock(&list_mutex);
409
410 if (--info->timer->refcnt > 0) {
411 pr_debug("decreased refcnt of timer %s to %u\n",
412 info->label, info->timer->refcnt);
413 mutex_unlock(&list_mutex);
414 return;
415 }
416
417 pr_debug("deleting timer %s\n", info->label);
418
419 list_del(&info->timer->entry);
420 mutex_unlock(&list_mutex);
421
422 timer_shutdown_sync(&info->timer->timer);
423 cancel_work_sync(&info->timer->work);
424 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
425 kfree(info->timer->attr.attr.name);
426 kfree(info->timer);
427}
428
429static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param *par)
430{
431 const struct idletimer_tg_info_v1 *info = par->targinfo;
432
433 pr_debug("destroy targinfo %s\n", info->label);
434
435 mutex_lock(&list_mutex);
436
437 if (--info->timer->refcnt > 0) {
438 pr_debug("decreased refcnt of timer %s to %u\n",
439 info->label, info->timer->refcnt);
440 mutex_unlock(&list_mutex);
441 return;
442 }
443
444 pr_debug("deleting timer %s\n", info->label);
445
446 list_del(&info->timer->entry);
447 mutex_unlock(&list_mutex);
448
449 if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
450 alarm_cancel(&info->timer->alarm);
451 } else {
452 timer_shutdown_sync(&info->timer->timer);
453 }
454 cancel_work_sync(&info->timer->work);
455 sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
456 kfree(info->timer->attr.attr.name);
457 kfree(info->timer);
458}
459
460
461static struct xt_target idletimer_tg[] __read_mostly = {
462 {
463 .name = "IDLETIMER",
464 .family = NFPROTO_IPV4,
465 .target = idletimer_tg_target,
466 .targetsize = sizeof(struct idletimer_tg_info),
467 .usersize = offsetof(struct idletimer_tg_info, timer),
468 .checkentry = idletimer_tg_checkentry,
469 .destroy = idletimer_tg_destroy,
470 .me = THIS_MODULE,
471 },
472 {
473 .name = "IDLETIMER",
474 .family = NFPROTO_IPV4,
475 .revision = 1,
476 .target = idletimer_tg_target_v1,
477 .targetsize = sizeof(struct idletimer_tg_info_v1),
478 .usersize = offsetof(struct idletimer_tg_info_v1, timer),
479 .checkentry = idletimer_tg_checkentry_v1,
480 .destroy = idletimer_tg_destroy_v1,
481 .me = THIS_MODULE,
482 },
483#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
484 {
485 .name = "IDLETIMER",
486 .family = NFPROTO_IPV6,
487 .target = idletimer_tg_target,
488 .targetsize = sizeof(struct idletimer_tg_info),
489 .usersize = offsetof(struct idletimer_tg_info, timer),
490 .checkentry = idletimer_tg_checkentry,
491 .destroy = idletimer_tg_destroy,
492 .me = THIS_MODULE,
493 },
494 {
495 .name = "IDLETIMER",
496 .family = NFPROTO_IPV6,
497 .revision = 1,
498 .target = idletimer_tg_target_v1,
499 .targetsize = sizeof(struct idletimer_tg_info_v1),
500 .usersize = offsetof(struct idletimer_tg_info_v1, timer),
501 .checkentry = idletimer_tg_checkentry_v1,
502 .destroy = idletimer_tg_destroy_v1,
503 .me = THIS_MODULE,
504 },
505#endif
506};
507
508static struct class *idletimer_tg_class;
509
510static struct device *idletimer_tg_device;
511
512static int __init idletimer_tg_init(void)
513{
514 int err;
515
516 idletimer_tg_class = class_create("xt_idletimer");
517 err = PTR_ERR(idletimer_tg_class);
518 if (IS_ERR(idletimer_tg_class)) {
519 pr_debug("couldn't register device class\n");
520 goto out;
521 }
522
523 idletimer_tg_device = device_create(idletimer_tg_class, NULL,
524 MKDEV(0, 0), NULL, "timers");
525 err = PTR_ERR(idletimer_tg_device);
526 if (IS_ERR(idletimer_tg_device)) {
527 pr_debug("couldn't register system device\n");
528 goto out_class;
529 }
530
531 idletimer_tg_kobj = &idletimer_tg_device->kobj;
532
533 err = xt_register_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
534
535 if (err < 0) {
536 pr_debug("couldn't register xt target\n");
537 goto out_dev;
538 }
539
540 return 0;
541out_dev:
542 device_destroy(idletimer_tg_class, MKDEV(0, 0));
543out_class:
544 class_destroy(idletimer_tg_class);
545out:
546 return err;
547}
548
549static void __exit idletimer_tg_exit(void)
550{
551 xt_unregister_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
552
553 device_destroy(idletimer_tg_class, MKDEV(0, 0));
554 class_destroy(idletimer_tg_class);
555}
556
557module_init(idletimer_tg_init);
558module_exit(idletimer_tg_exit);
559
560MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
561MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
562MODULE_DESCRIPTION("Xtables: idle time monitor");
563MODULE_LICENSE("GPL v2");
564MODULE_ALIAS("ipt_IDLETIMER");
565MODULE_ALIAS("ip6t_IDLETIMER");