1 // SPDX-License-Identifier: GPL-2.0-only
3 * xt_LED.c - netfilter target to make LEDs blink upon packet matches
5 * Copyright (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <linux/netfilter/x_tables.h>
11 #include <linux/slab.h>
12 #include <linux/leds.h>
13 #include <linux/mutex.h>
15 #include <linux/netfilter/xt_LED.h>
17 MODULE_LICENSE("GPL");
18 MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>");
19 MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match");
20 MODULE_ALIAS("ipt_LED");
21 MODULE_ALIAS("ip6t_LED");
23 static LIST_HEAD(xt_led_triggers
);
24 static DEFINE_MUTEX(xt_led_mutex
);
27 * This is declared in here (the kernel module) only, to avoid having these
28 * dependencies in userspace code. This is what xt_led_info.internal_data
31 struct xt_led_info_internal
{
32 struct list_head list
;
35 struct led_trigger netfilter_led_trigger
;
36 struct timer_list timer
;
39 #define XT_LED_BLINK_DELAY 50 /* ms */
42 led_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
44 const struct xt_led_info
*ledinfo
= par
->targinfo
;
45 struct xt_led_info_internal
*ledinternal
= ledinfo
->internal_data
;
46 unsigned long led_delay
= XT_LED_BLINK_DELAY
;
49 * If "always blink" is enabled, and there's still some time until the
50 * LED will switch off, briefly switch it off now.
52 if ((ledinfo
->delay
> 0) && ledinfo
->always_blink
&&
53 timer_pending(&ledinternal
->timer
))
54 led_trigger_blink_oneshot(&ledinternal
->netfilter_led_trigger
,
55 &led_delay
, &led_delay
, 1);
57 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_FULL
);
59 /* If there's a positive delay, start/update the timer */
60 if (ledinfo
->delay
> 0) {
61 mod_timer(&ledinternal
->timer
,
62 jiffies
+ msecs_to_jiffies(ledinfo
->delay
));
64 /* Otherwise if there was no delay given, blink as fast as possible */
65 } else if (ledinfo
->delay
== 0) {
66 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_OFF
);
69 /* else the delay is negative, which means switch on and stay on */
74 static void led_timeout_callback(struct timer_list
*t
)
76 struct xt_led_info_internal
*ledinternal
= from_timer(ledinternal
, t
,
79 led_trigger_event(&ledinternal
->netfilter_led_trigger
, LED_OFF
);
82 static struct xt_led_info_internal
*led_trigger_lookup(const char *name
)
84 struct xt_led_info_internal
*ledinternal
;
86 list_for_each_entry(ledinternal
, &xt_led_triggers
, list
) {
87 if (!strcmp(name
, ledinternal
->netfilter_led_trigger
.name
)) {
94 static int led_tg_check(const struct xt_tgchk_param
*par
)
96 struct xt_led_info
*ledinfo
= par
->targinfo
;
97 struct xt_led_info_internal
*ledinternal
;
100 if (ledinfo
->id
[0] == '\0')
103 mutex_lock(&xt_led_mutex
);
105 ledinternal
= led_trigger_lookup(ledinfo
->id
);
107 ledinternal
->refcnt
++;
112 ledinternal
= kzalloc(sizeof(struct xt_led_info_internal
), GFP_KERNEL
);
114 goto exit_mutex_only
;
116 ledinternal
->trigger_id
= kstrdup(ledinfo
->id
, GFP_KERNEL
);
117 if (!ledinternal
->trigger_id
)
118 goto exit_internal_alloc
;
120 ledinternal
->refcnt
= 1;
121 ledinternal
->netfilter_led_trigger
.name
= ledinternal
->trigger_id
;
123 err
= led_trigger_register(&ledinternal
->netfilter_led_trigger
);
125 pr_info_ratelimited("Trigger name is already in use.\n");
129 /* Since the letinternal timer can be shared between multiple targets,
130 * always set it up, even if the current target does not need it
132 timer_setup(&ledinternal
->timer
, led_timeout_callback
, 0);
134 list_add_tail(&ledinternal
->list
, &xt_led_triggers
);
137 mutex_unlock(&xt_led_mutex
);
139 ledinfo
->internal_data
= ledinternal
;
144 kfree(ledinternal
->trigger_id
);
150 mutex_unlock(&xt_led_mutex
);
155 static void led_tg_destroy(const struct xt_tgdtor_param
*par
)
157 const struct xt_led_info
*ledinfo
= par
->targinfo
;
158 struct xt_led_info_internal
*ledinternal
= ledinfo
->internal_data
;
160 mutex_lock(&xt_led_mutex
);
162 if (--ledinternal
->refcnt
) {
163 mutex_unlock(&xt_led_mutex
);
167 list_del(&ledinternal
->list
);
169 del_timer_sync(&ledinternal
->timer
);
171 led_trigger_unregister(&ledinternal
->netfilter_led_trigger
);
173 mutex_unlock(&xt_led_mutex
);
175 kfree(ledinternal
->trigger_id
);
179 static struct xt_target led_tg_reg __read_mostly
= {
182 .family
= NFPROTO_UNSPEC
,
184 .targetsize
= sizeof(struct xt_led_info
),
185 .usersize
= offsetof(struct xt_led_info
, internal_data
),
186 .checkentry
= led_tg_check
,
187 .destroy
= led_tg_destroy
,
191 static int __init
led_tg_init(void)
193 return xt_register_target(&led_tg_reg
);
196 static void __exit
led_tg_exit(void)
198 xt_unregister_target(&led_tg_reg
);
201 module_init(led_tg_init
);
202 module_exit(led_tg_exit
);