2 * xt_LED.c - netfilter target to make LEDs blink upon packet matches
4 * Copyright (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/slab.h>
26 #include <linux/leds.h>
27 #include <linux/mutex.h>
29 #include <linux/netfilter/xt_LED.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>");
33 MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match");
34 MODULE_ALIAS("ipt_LED");
35 MODULE_ALIAS("ip6t_LED");
37 static LIST_HEAD(xt_led_triggers
);
38 static DEFINE_MUTEX(xt_led_mutex
);
41 * This is declared in here (the kernel module) only, to avoid having these
42 * dependencies in userspace code. This is what xt_led_info.internal_data
45 struct xt_led_info_internal
{
46 struct list_head list
;
49 struct led_trigger netfilter_led_trigger
;
50 struct timer_list timer
;
54 led_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
56 const struct xt_led_info
*ledinfo
= par
->targinfo
;
57 struct xt_led_info_internal
*ledinternal
= ledinfo
->internal_data
;
60 * If "always blink" is enabled, and there's still some time until the
61 * LED will switch off, briefly switch it off now.
63 if ((ledinfo
->delay
> 0) && ledinfo
->always_blink
&&
64 timer_pending(&ledinternal
->timer
))
65 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_OFF
);
67 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_FULL
);
69 /* If there's a positive delay, start/update the timer */
70 if (ledinfo
->delay
> 0) {
71 mod_timer(&ledinternal
->timer
,
72 jiffies
+ msecs_to_jiffies(ledinfo
->delay
));
74 /* Otherwise if there was no delay given, blink as fast as possible */
75 } else if (ledinfo
->delay
== 0) {
76 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_OFF
);
79 /* else the delay is negative, which means switch on and stay on */
84 static void led_timeout_callback(unsigned long data
)
86 struct xt_led_info_internal
*ledinternal
= (struct xt_led_info_internal
*)data
;
88 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_OFF
);
91 static struct xt_led_info_internal
*led_trigger_lookup(const char *name
)
93 struct xt_led_info_internal
*ledinternal
;
95 list_for_each_entry(ledinternal
, &xt_led_triggers
, list
) {
96 if (!strcmp(name
, ledinternal
->netfilter_led_trigger
.name
)) {
103 static int led_tg_check(const struct xt_tgchk_param
*par
)
105 struct xt_led_info
*ledinfo
= par
->targinfo
;
106 struct xt_led_info_internal
*ledinternal
;
109 if (ledinfo
->id
[0] == '\0') {
110 pr_info("No 'id' parameter given.\n");
114 mutex_lock(&xt_led_mutex
);
116 ledinternal
= led_trigger_lookup(ledinfo
->id
);
118 ledinternal
->refcnt
++;
123 ledinternal
= kzalloc(sizeof(struct xt_led_info_internal
), GFP_KERNEL
);
125 goto exit_mutex_only
;
127 ledinternal
->trigger_id
= kstrdup(ledinfo
->id
, GFP_KERNEL
);
128 if (!ledinternal
->trigger_id
)
129 goto exit_internal_alloc
;
131 ledinternal
->refcnt
= 1;
132 ledinternal
->netfilter_led_trigger
.name
= ledinternal
->trigger_id
;
134 err
= led_trigger_register(&ledinternal
->netfilter_led_trigger
);
136 pr_warning("led_trigger_register() failed\n");
138 pr_warning("Trigger name is already in use.\n");
142 /* See if we need to set up a timer */
143 if (ledinfo
->delay
> 0)
144 setup_timer(&ledinternal
->timer
, led_timeout_callback
,
145 (unsigned long)ledinternal
);
147 list_add_tail(&ledinternal
->list
, &xt_led_triggers
);
150 mutex_unlock(&xt_led_mutex
);
152 ledinfo
->internal_data
= ledinternal
;
157 kfree(ledinternal
->trigger_id
);
163 mutex_unlock(&xt_led_mutex
);
168 static void led_tg_destroy(const struct xt_tgdtor_param
*par
)
170 const struct xt_led_info
*ledinfo
= par
->targinfo
;
171 struct xt_led_info_internal
*ledinternal
= ledinfo
->internal_data
;
173 mutex_lock(&xt_led_mutex
);
175 if (--ledinternal
->refcnt
) {
176 mutex_unlock(&xt_led_mutex
);
180 list_del(&ledinternal
->list
);
182 if (ledinfo
->delay
> 0)
183 del_timer_sync(&ledinternal
->timer
);
185 led_trigger_unregister(&ledinternal
->netfilter_led_trigger
);
187 mutex_unlock(&xt_led_mutex
);
189 kfree(ledinternal
->trigger_id
);
193 static struct xt_target led_tg_reg __read_mostly
= {
196 .family
= NFPROTO_UNSPEC
,
198 .targetsize
= sizeof(struct xt_led_info
),
199 .checkentry
= led_tg_check
,
200 .destroy
= led_tg_destroy
,
204 static int __init
led_tg_init(void)
206 return xt_register_target(&led_tg_reg
);
209 static void __exit
led_tg_exit(void)
211 xt_unregister_target(&led_tg_reg
);
214 module_init(led_tg_init
);
215 module_exit(led_tg_exit
);