2019-05-29 23:57:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2010-06-15 13:04:00 +00:00
|
|
|
/*
|
|
|
|
* linux/net/netfilter/xt_IDLETIMER.c
|
|
|
|
*
|
|
|
|
* Netfilter module to trigger a timer when packet matches.
|
|
|
|
* After timer expires a kevent will be sent.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004, 2010 Nokia Corporation
|
|
|
|
* Written by Timo Teras <ext-timo.teras@nokia.com>
|
|
|
|
*
|
|
|
|
* Converted to x_tables and reworked for upstream inclusion
|
|
|
|
* by Luciano Coelho <luciano.coelho@nokia.com>
|
|
|
|
*
|
|
|
|
* Contact: Luciano Coelho <luciano.coelho@nokia.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/timer.h>
|
2020-02-06 11:07:29 +00:00
|
|
|
#include <linux/alarmtimer.h>
|
2010-06-15 13:04:00 +00:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_IDLETIMER.h>
|
2010-06-22 06:13:31 +00:00
|
|
|
#include <linux/kdev_t.h>
|
2010-06-15 13:04:00 +00:00
|
|
|
#include <linux/kobject.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/sysfs.h>
|
|
|
|
|
|
|
|
struct idletimer_tg {
|
|
|
|
struct list_head entry;
|
2020-02-06 11:07:29 +00:00
|
|
|
struct alarm alarm;
|
2010-06-15 13:04:00 +00:00
|
|
|
struct timer_list timer;
|
|
|
|
struct work_struct work;
|
|
|
|
|
|
|
|
struct kobject *kobj;
|
2019-02-27 18:19:10 +00:00
|
|
|
struct device_attribute attr;
|
2010-06-15 13:04:00 +00:00
|
|
|
|
|
|
|
unsigned int refcnt;
|
2020-02-06 11:07:29 +00:00
|
|
|
u8 timer_type;
|
2010-06-15 13:04:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(idletimer_tg_list);
|
|
|
|
static DEFINE_MUTEX(list_mutex);
|
|
|
|
|
|
|
|
static struct kobject *idletimer_tg_kobj;
|
|
|
|
|
|
|
|
static
|
|
|
|
struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
|
|
|
|
{
|
|
|
|
struct idletimer_tg *entry;
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &idletimer_tg_list, entry) {
|
|
|
|
if (!strcmp(label, entry->attr.attr.name))
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-02-27 18:19:10 +00:00
|
|
|
static ssize_t idletimer_tg_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2010-06-15 13:04:00 +00:00
|
|
|
{
|
|
|
|
struct idletimer_tg *timer;
|
|
|
|
unsigned long expires = 0;
|
2020-02-06 11:07:29 +00:00
|
|
|
struct timespec64 ktimespec = {};
|
|
|
|
long time_diff = 0;
|
2010-06-15 13:04:00 +00:00
|
|
|
|
|
|
|
mutex_lock(&list_mutex);
|
|
|
|
|
2019-02-27 18:19:10 +00:00
|
|
|
timer = __idletimer_tg_find_by_label(attr->attr.name);
|
2020-02-06 11:07:29 +00:00
|
|
|
if (timer) {
|
|
|
|
if (timer->timer_type & XT_IDLETIMER_ALARM) {
|
|
|
|
ktime_t expires_alarm = alarm_expires_remaining(&timer->alarm);
|
|
|
|
ktimespec = ktime_to_timespec64(expires_alarm);
|
|
|
|
time_diff = ktimespec.tv_sec;
|
|
|
|
} else {
|
2010-06-15 13:04:00 +00:00
|
|
|
expires = timer->timer.expires;
|
2020-02-06 11:07:29 +00:00
|
|
|
time_diff = jiffies_to_msecs(
|
|
|
|
expires - jiffies) / 1000;
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 13:04:00 +00:00
|
|
|
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
if (time_after(expires, jiffies) || ktimespec.tv_sec > 0)
|
|
|
|
return snprintf(buf, PAGE_SIZE, "%ld\n", time_diff);
|
2010-06-15 13:04:00 +00:00
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
return snprintf(buf, PAGE_SIZE, "0\n");
|
2010-06-15 13:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void idletimer_tg_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
|
|
|
|
work);
|
|
|
|
|
|
|
|
sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
|
|
|
|
}
|
|
|
|
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-16 21:43:17 +00:00
|
|
|
static void idletimer_tg_expired(struct timer_list *t)
|
2010-06-15 13:04:00 +00:00
|
|
|
{
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-16 21:43:17 +00:00
|
|
|
struct idletimer_tg *timer = from_timer(timer, t, timer);
|
2010-06-15 13:04:00 +00:00
|
|
|
|
|
|
|
pr_debug("timer %s expired\n", timer->attr.attr.name);
|
|
|
|
|
|
|
|
schedule_work(&timer->work);
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
static enum alarmtimer_restart idletimer_tg_alarmproc(struct alarm *alarm,
|
|
|
|
ktime_t now)
|
|
|
|
{
|
|
|
|
struct idletimer_tg *timer = alarm->data;
|
|
|
|
|
|
|
|
pr_debug("alarm %s expired\n", timer->attr.attr.name);
|
|
|
|
schedule_work(&timer->work);
|
|
|
|
return ALARMTIMER_NORESTART;
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:00:08 +00:00
|
|
|
static int idletimer_check_sysfs_name(const char *name, unsigned int size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = xt_check_proc_name(name, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!strcmp(name, "power") ||
|
|
|
|
!strcmp(name, "subsystem") ||
|
|
|
|
!strcmp(name, "uevent"))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-15 13:04:00 +00:00
|
|
|
static int idletimer_tg_create(struct idletimer_tg_info *info)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
|
|
|
|
if (!info->timer) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:00:08 +00:00
|
|
|
ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_free_timer;
|
|
|
|
|
2015-07-10 00:15:01 +00:00
|
|
|
sysfs_attr_init(&info->timer->attr.attr);
|
2010-06-15 13:04:00 +00:00
|
|
|
info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
|
|
|
|
if (!info->timer->attr.attr.name) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_free_timer;
|
|
|
|
}
|
2018-03-23 22:54:38 +00:00
|
|
|
info->timer->attr.attr.mode = 0444;
|
2010-06-15 13:04:00 +00:00
|
|
|
info->timer->attr.show = idletimer_tg_show;
|
|
|
|
|
|
|
|
ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_debug("couldn't add file to sysfs");
|
|
|
|
goto out_free_attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&info->timer->entry, &idletimer_tg_list);
|
|
|
|
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-16 21:43:17 +00:00
|
|
|
timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
|
2010-06-15 13:04:00 +00:00
|
|
|
info->timer->refcnt = 1;
|
|
|
|
|
2018-02-17 03:36:28 +00:00
|
|
|
INIT_WORK(&info->timer->work, idletimer_tg_work);
|
|
|
|
|
2010-06-15 13:04:00 +00:00
|
|
|
mod_timer(&info->timer->timer,
|
|
|
|
msecs_to_jiffies(info->timeout * 1000) + jiffies);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_free_attr:
|
|
|
|
kfree(info->timer->attr.attr.name);
|
|
|
|
out_free_timer:
|
|
|
|
kfree(info->timer);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
static int idletimer_tg_create_v1(struct idletimer_tg_info_v1 *info)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
|
|
|
|
if (!info->timer) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_free_timer;
|
|
|
|
|
|
|
|
sysfs_attr_init(&info->timer->attr.attr);
|
|
|
|
info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
|
|
|
|
if (!info->timer->attr.attr.name) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_free_timer;
|
|
|
|
}
|
|
|
|
info->timer->attr.attr.mode = 0444;
|
|
|
|
info->timer->attr.show = idletimer_tg_show;
|
|
|
|
|
|
|
|
ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_debug("couldn't add file to sysfs");
|
|
|
|
goto out_free_attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* notify userspace */
|
|
|
|
kobject_uevent(idletimer_tg_kobj,KOBJ_ADD);
|
|
|
|
|
|
|
|
list_add(&info->timer->entry, &idletimer_tg_list);
|
|
|
|
pr_debug("timer type value is %u", info->timer_type);
|
|
|
|
info->timer->timer_type = info->timer_type;
|
|
|
|
info->timer->refcnt = 1;
|
|
|
|
|
|
|
|
INIT_WORK(&info->timer->work, idletimer_tg_work);
|
|
|
|
|
|
|
|
if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
|
|
|
|
ktime_t tout;
|
|
|
|
alarm_init(&info->timer->alarm, ALARM_BOOTTIME,
|
|
|
|
idletimer_tg_alarmproc);
|
|
|
|
info->timer->alarm.data = info->timer;
|
|
|
|
tout = ktime_set(info->timeout, 0);
|
|
|
|
alarm_start_relative(&info->timer->alarm, tout);
|
|
|
|
} else {
|
|
|
|
timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
|
|
|
|
mod_timer(&info->timer->timer,
|
|
|
|
msecs_to_jiffies(info->timeout * 1000) + jiffies);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_free_attr:
|
|
|
|
kfree(info->timer->attr.attr.name);
|
|
|
|
out_free_timer:
|
|
|
|
kfree(info->timer);
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-15 13:04:00 +00:00
|
|
|
/*
|
|
|
|
* The actual xt_tables plugin.
|
|
|
|
*/
|
|
|
|
static unsigned int idletimer_tg_target(struct sk_buff *skb,
|
|
|
|
const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct idletimer_tg_info *info = par->targinfo;
|
|
|
|
|
|
|
|
pr_debug("resetting timer %s, timeout period %u\n",
|
|
|
|
info->label, info->timeout);
|
|
|
|
|
|
|
|
mod_timer(&info->timer->timer,
|
|
|
|
msecs_to_jiffies(info->timeout * 1000) + jiffies);
|
|
|
|
|
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
/*
|
|
|
|
* The actual xt_tables plugin.
|
|
|
|
*/
|
|
|
|
static unsigned int idletimer_tg_target_v1(struct sk_buff *skb,
|
|
|
|
const struct xt_action_param *par)
|
2010-06-15 13:04:00 +00:00
|
|
|
{
|
2020-02-06 11:07:29 +00:00
|
|
|
const struct idletimer_tg_info_v1 *info = par->targinfo;
|
2010-06-15 13:04:00 +00:00
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
pr_debug("resetting timer %s, timeout period %u\n",
|
|
|
|
info->label, info->timeout);
|
|
|
|
|
|
|
|
if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
|
|
|
|
ktime_t tout = ktime_set(info->timeout, 0);
|
|
|
|
alarm_start_relative(&info->timer->alarm, tout);
|
|
|
|
} else {
|
|
|
|
mod_timer(&info->timer->timer,
|
|
|
|
msecs_to_jiffies(info->timeout * 1000) + jiffies);
|
|
|
|
}
|
2010-06-15 13:04:00 +00:00
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int idletimer_tg_helper(struct idletimer_tg_info *info)
|
|
|
|
{
|
2010-06-15 13:04:00 +00:00
|
|
|
if (info->timeout == 0) {
|
|
|
|
pr_debug("timeout value is zero\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-02-17 03:36:28 +00:00
|
|
|
if (info->timeout >= INT_MAX / 1000) {
|
|
|
|
pr_debug("timeout value is too big\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-06-15 13:04:00 +00:00
|
|
|
if (info->label[0] == '\0' ||
|
|
|
|
strnlen(info->label,
|
|
|
|
MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
|
|
|
|
pr_debug("label is empty or not nul-terminated\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-02-06 11:07:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-06-15 13:04:00 +00:00
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
|
|
|
|
static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
struct idletimer_tg_info *info = par->targinfo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pr_debug("checkentry targinfo%s\n", info->label);
|
|
|
|
|
|
|
|
ret = idletimer_tg_helper(info);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
pr_debug("checkentry helper return invalid\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-06-15 13:04:00 +00:00
|
|
|
mutex_lock(&list_mutex);
|
|
|
|
|
|
|
|
info->timer = __idletimer_tg_find_by_label(info->label);
|
|
|
|
if (info->timer) {
|
|
|
|
info->timer->refcnt++;
|
|
|
|
mod_timer(&info->timer->timer,
|
|
|
|
msecs_to_jiffies(info->timeout * 1000) + jiffies);
|
|
|
|
|
|
|
|
pr_debug("increased refcnt of timer %s to %u\n",
|
|
|
|
info->label, info->timer->refcnt);
|
|
|
|
} else {
|
|
|
|
ret = idletimer_tg_create(info);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_debug("failed to create timer\n");
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
struct idletimer_tg_info_v1 *info = par->targinfo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pr_debug("checkentry targinfo%s\n", info->label);
|
|
|
|
|
|
|
|
ret = idletimer_tg_helper((struct idletimer_tg_info *)info);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
|
|
|
pr_debug("checkentry helper return invalid\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->timer_type > XT_IDLETIMER_ALARM) {
|
|
|
|
pr_debug("invalid value for timer type\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&list_mutex);
|
|
|
|
|
|
|
|
info->timer = __idletimer_tg_find_by_label(info->label);
|
|
|
|
if (info->timer) {
|
|
|
|
if (info->timer->timer_type != info->timer_type) {
|
|
|
|
pr_debug("Adding/Replacing rule with same label and different timer type is not allowed\n");
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->timer->refcnt++;
|
|
|
|
if (info->timer_type & XT_IDLETIMER_ALARM) {
|
|
|
|
/* calculate remaining expiry time */
|
|
|
|
ktime_t tout = alarm_expires_remaining(&info->timer->alarm);
|
|
|
|
struct timespec64 ktimespec = ktime_to_timespec64(tout);
|
|
|
|
|
|
|
|
if (ktimespec.tv_sec > 0) {
|
|
|
|
pr_debug("time_expiry_remaining %lld\n",
|
|
|
|
ktimespec.tv_sec);
|
|
|
|
alarm_start_relative(&info->timer->alarm, tout);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mod_timer(&info->timer->timer,
|
|
|
|
msecs_to_jiffies(info->timeout * 1000) + jiffies);
|
|
|
|
}
|
|
|
|
pr_debug("increased refcnt of timer %s to %u\n",
|
|
|
|
info->label, info->timer->refcnt);
|
|
|
|
} else {
|
|
|
|
ret = idletimer_tg_create_v1(info);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_debug("failed to create timer\n");
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-15 13:04:00 +00:00
|
|
|
static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct idletimer_tg_info *info = par->targinfo;
|
|
|
|
|
|
|
|
pr_debug("destroy targinfo %s\n", info->label);
|
|
|
|
|
|
|
|
mutex_lock(&list_mutex);
|
|
|
|
|
|
|
|
if (--info->timer->refcnt == 0) {
|
|
|
|
pr_debug("deleting timer %s\n", info->label);
|
|
|
|
|
|
|
|
list_del(&info->timer->entry);
|
|
|
|
del_timer_sync(&info->timer->timer);
|
2016-04-21 07:47:08 +00:00
|
|
|
cancel_work_sync(&info->timer->work);
|
2010-06-15 13:04:00 +00:00
|
|
|
sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
|
|
|
|
kfree(info->timer->attr.attr.name);
|
|
|
|
kfree(info->timer);
|
|
|
|
} else {
|
|
|
|
pr_debug("decreased refcnt of timer %s to %u\n",
|
|
|
|
info->label, info->timer->refcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct idletimer_tg_info_v1 *info = par->targinfo;
|
|
|
|
|
|
|
|
pr_debug("destroy targinfo %s\n", info->label);
|
|
|
|
|
|
|
|
mutex_lock(&list_mutex);
|
|
|
|
|
|
|
|
if (--info->timer->refcnt == 0) {
|
|
|
|
pr_debug("deleting timer %s\n", info->label);
|
|
|
|
|
|
|
|
list_del(&info->timer->entry);
|
|
|
|
if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
|
|
|
|
alarm_cancel(&info->timer->alarm);
|
|
|
|
} else {
|
|
|
|
del_timer_sync(&info->timer->timer);
|
|
|
|
}
|
|
|
|
cancel_work_sync(&info->timer->work);
|
|
|
|
sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
|
|
|
|
kfree(info->timer->attr.attr.name);
|
|
|
|
kfree(info->timer);
|
|
|
|
} else {
|
|
|
|
pr_debug("decreased refcnt of timer %s to %u\n",
|
|
|
|
info->label, info->timer->refcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&list_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct xt_target idletimer_tg[] __read_mostly = {
|
|
|
|
{
|
2010-06-15 13:04:00 +00:00
|
|
|
.name = "IDLETIMER",
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.target = idletimer_tg_target,
|
|
|
|
.targetsize = sizeof(struct idletimer_tg_info),
|
2018-01-29 12:21:20 +00:00
|
|
|
.usersize = offsetof(struct idletimer_tg_info, timer),
|
2010-06-15 13:04:00 +00:00
|
|
|
.checkentry = idletimer_tg_checkentry,
|
|
|
|
.destroy = idletimer_tg_destroy,
|
|
|
|
.me = THIS_MODULE,
|
2020-02-06 11:07:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "IDLETIMER",
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.revision = 1,
|
|
|
|
.target = idletimer_tg_target_v1,
|
|
|
|
.targetsize = sizeof(struct idletimer_tg_info_v1),
|
|
|
|
.usersize = offsetof(struct idletimer_tg_info_v1, timer),
|
|
|
|
.checkentry = idletimer_tg_checkentry_v1,
|
|
|
|
.destroy = idletimer_tg_destroy_v1,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2010-06-15 13:04:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct class *idletimer_tg_class;
|
|
|
|
|
|
|
|
static struct device *idletimer_tg_device;
|
|
|
|
|
|
|
|
static int __init idletimer_tg_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
|
|
|
|
err = PTR_ERR(idletimer_tg_class);
|
|
|
|
if (IS_ERR(idletimer_tg_class)) {
|
|
|
|
pr_debug("couldn't register device class\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
idletimer_tg_device = device_create(idletimer_tg_class, NULL,
|
|
|
|
MKDEV(0, 0), NULL, "timers");
|
|
|
|
err = PTR_ERR(idletimer_tg_device);
|
|
|
|
if (IS_ERR(idletimer_tg_device)) {
|
|
|
|
pr_debug("couldn't register system device\n");
|
|
|
|
goto out_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
idletimer_tg_kobj = &idletimer_tg_device->kobj;
|
|
|
|
|
2020-02-06 11:07:29 +00:00
|
|
|
err = xt_register_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
|
|
|
|
|
2010-06-15 13:04:00 +00:00
|
|
|
if (err < 0) {
|
|
|
|
pr_debug("couldn't register xt target\n");
|
|
|
|
goto out_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
out_dev:
|
|
|
|
device_destroy(idletimer_tg_class, MKDEV(0, 0));
|
|
|
|
out_class:
|
|
|
|
class_destroy(idletimer_tg_class);
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit idletimer_tg_exit(void)
|
|
|
|
{
|
2020-02-06 11:07:29 +00:00
|
|
|
xt_unregister_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
|
2010-06-15 13:04:00 +00:00
|
|
|
|
|
|
|
device_destroy(idletimer_tg_class, MKDEV(0, 0));
|
|
|
|
class_destroy(idletimer_tg_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(idletimer_tg_init);
|
|
|
|
module_exit(idletimer_tg_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
|
|
|
|
MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
|
|
|
|
MODULE_DESCRIPTION("Xtables: idle time monitor");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
2011-01-18 05:30:13 +00:00
|
|
|
MODULE_ALIAS("ipt_IDLETIMER");
|
|
|
|
MODULE_ALIAS("ip6t_IDLETIMER");
|