blob: 993de2ba89d33bb3fa532ded0952bfb83de63836 [file] [log] [blame]
Adam Nielsen268cb382009-02-20 10:55:14 +01001/*
2 * xt_LED.c - netfilter target to make LEDs blink upon packet matches
3 *
4 * Copyright (C) 2008 Adam Nielsen <a.nielsen@shikadi.net>
5 *
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.
9 *
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.
14 *
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
18 * 02110-1301 USA.
19 *
20 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Adam Nielsen268cb382009-02-20 10:55:14 +010022#include <linux/module.h>
23#include <linux/skbuff.h>
24#include <linux/netfilter/x_tables.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Adam Nielsen268cb382009-02-20 10:55:14 +010026#include <linux/leds.h>
27#include <linux/mutex.h>
28
29#include <linux/netfilter/xt_LED.h>
30
31MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>");
33MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match");
Jan Engelhardtf1e231a2011-01-18 06:30:13 +010034MODULE_ALIAS("ipt_LED");
35MODULE_ALIAS("ip6t_LED");
Adam Nielsen268cb382009-02-20 10:55:14 +010036
Adam Nielsenb660d042010-04-09 16:51:40 +020037static LIST_HEAD(xt_led_triggers);
38static DEFINE_MUTEX(xt_led_mutex);
39
Adam Nielsen268cb382009-02-20 10:55:14 +010040/*
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
43 * points to.
44 */
45struct xt_led_info_internal {
Adam Nielsenb660d042010-04-09 16:51:40 +020046 struct list_head list;
47 int refcnt;
48 char *trigger_id;
Adam Nielsen268cb382009-02-20 10:55:14 +010049 struct led_trigger netfilter_led_trigger;
50 struct timer_list timer;
51};
52
53static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020054led_tg(struct sk_buff *skb, const struct xt_action_param *par)
Adam Nielsen268cb382009-02-20 10:55:14 +010055{
56 const struct xt_led_info *ledinfo = par->targinfo;
57 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
58
59 /*
60 * If "always blink" is enabled, and there's still some time until the
61 * LED will switch off, briefly switch it off now.
62 */
63 if ((ledinfo->delay > 0) && ledinfo->always_blink &&
64 timer_pending(&ledinternal->timer))
Adam Nielsenb660d042010-04-09 16:51:40 +020065 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
Adam Nielsen268cb382009-02-20 10:55:14 +010066
67 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL);
68
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));
73
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);
77 }
78
79 /* else the delay is negative, which means switch on and stay on */
80
81 return XT_CONTINUE;
82}
83
84static void led_timeout_callback(unsigned long data)
85{
Adam Nielsenb660d042010-04-09 16:51:40 +020086 struct xt_led_info_internal *ledinternal = (struct xt_led_info_internal *)data;
Adam Nielsen268cb382009-02-20 10:55:14 +010087
88 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
89}
90
Adam Nielsenb660d042010-04-09 16:51:40 +020091static struct xt_led_info_internal *led_trigger_lookup(const char *name)
92{
93 struct xt_led_info_internal *ledinternal;
94
95 list_for_each_entry(ledinternal, &xt_led_triggers, list) {
96 if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) {
97 return ledinternal;
98 }
99 }
100 return NULL;
101}
102
Jan Engelhardt135367b2010-03-19 17:16:42 +0100103static int led_tg_check(const struct xt_tgchk_param *par)
Adam Nielsen268cb382009-02-20 10:55:14 +0100104{
105 struct xt_led_info *ledinfo = par->targinfo;
106 struct xt_led_info_internal *ledinternal;
107 int err;
108
109 if (ledinfo->id[0] == '\0') {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100110 pr_info("No 'id' parameter given.\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100111 return -EINVAL;
Adam Nielsen268cb382009-02-20 10:55:14 +0100112 }
113
Adam Nielsenb660d042010-04-09 16:51:40 +0200114 mutex_lock(&xt_led_mutex);
115
116 ledinternal = led_trigger_lookup(ledinfo->id);
117 if (ledinternal) {
118 ledinternal->refcnt++;
119 goto out;
120 }
121
122 err = -ENOMEM;
Adam Nielsen268cb382009-02-20 10:55:14 +0100123 ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
Jan Engelhardt85bc3f32010-03-18 00:27:03 +0100124 if (!ledinternal)
Adam Nielsenb660d042010-04-09 16:51:40 +0200125 goto exit_mutex_only;
Adam Nielsen268cb382009-02-20 10:55:14 +0100126
Adam Nielsenb660d042010-04-09 16:51:40 +0200127 ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
128 if (!ledinternal->trigger_id)
129 goto exit_internal_alloc;
130
131 ledinternal->refcnt = 1;
132 ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
Adam Nielsen268cb382009-02-20 10:55:14 +0100133
134 err = led_trigger_register(&ledinternal->netfilter_led_trigger);
135 if (err) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100136 pr_warning("led_trigger_register() failed\n");
Adam Nielsen268cb382009-02-20 10:55:14 +0100137 if (err == -EEXIST)
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100138 pr_warning("Trigger name is already in use.\n");
Adam Nielsen268cb382009-02-20 10:55:14 +0100139 goto exit_alloc;
140 }
141
142 /* See if we need to set up a timer */
143 if (ledinfo->delay > 0)
144 setup_timer(&ledinternal->timer, led_timeout_callback,
Adam Nielsenb660d042010-04-09 16:51:40 +0200145 (unsigned long)ledinternal);
146
147 list_add_tail(&ledinternal->list, &xt_led_triggers);
148
149out:
150 mutex_unlock(&xt_led_mutex);
Adam Nielsen268cb382009-02-20 10:55:14 +0100151
152 ledinfo->internal_data = ledinternal;
Adam Nielsenb660d042010-04-09 16:51:40 +0200153
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100154 return 0;
Adam Nielsen268cb382009-02-20 10:55:14 +0100155
156exit_alloc:
Adam Nielsenb660d042010-04-09 16:51:40 +0200157 kfree(ledinternal->trigger_id);
158
159exit_internal_alloc:
Adam Nielsen268cb382009-02-20 10:55:14 +0100160 kfree(ledinternal);
Adam Nielsenb660d042010-04-09 16:51:40 +0200161
162exit_mutex_only:
163 mutex_unlock(&xt_led_mutex);
164
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100165 return err;
Adam Nielsen268cb382009-02-20 10:55:14 +0100166}
167
168static void led_tg_destroy(const struct xt_tgdtor_param *par)
169{
170 const struct xt_led_info *ledinfo = par->targinfo;
171 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
172
Adam Nielsenb660d042010-04-09 16:51:40 +0200173 mutex_lock(&xt_led_mutex);
174
175 if (--ledinternal->refcnt) {
176 mutex_unlock(&xt_led_mutex);
177 return;
178 }
179
180 list_del(&ledinternal->list);
181
Adam Nielsen268cb382009-02-20 10:55:14 +0100182 if (ledinfo->delay > 0)
183 del_timer_sync(&ledinternal->timer);
184
185 led_trigger_unregister(&ledinternal->netfilter_led_trigger);
Adam Nielsenb660d042010-04-09 16:51:40 +0200186
187 mutex_unlock(&xt_led_mutex);
188
189 kfree(ledinternal->trigger_id);
Adam Nielsen268cb382009-02-20 10:55:14 +0100190 kfree(ledinternal);
191}
192
193static struct xt_target led_tg_reg __read_mostly = {
194 .name = "LED",
195 .revision = 0,
196 .family = NFPROTO_UNSPEC,
197 .target = led_tg,
Jan Engelhardt7d5f7ed2010-03-09 23:27:24 +0100198 .targetsize = sizeof(struct xt_led_info),
Adam Nielsen268cb382009-02-20 10:55:14 +0100199 .checkentry = led_tg_check,
200 .destroy = led_tg_destroy,
201 .me = THIS_MODULE,
202};
203
204static int __init led_tg_init(void)
205{
206 return xt_register_target(&led_tg_reg);
207}
208
209static void __exit led_tg_exit(void)
210{
211 xt_unregister_target(&led_tg_reg);
212}
213
214module_init(led_tg_init);
215module_exit(led_tg_exit);