blob: a4140509eea1f3e821bd6ffb56b5b747dfd8c9fc [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");
34
Adam Nielsenb660d042010-04-09 16:51:40 +020035static LIST_HEAD(xt_led_triggers);
36static DEFINE_MUTEX(xt_led_mutex);
37
Adam Nielsen268cb382009-02-20 10:55:14 +010038/*
39 * This is declared in here (the kernel module) only, to avoid having these
40 * dependencies in userspace code. This is what xt_led_info.internal_data
41 * points to.
42 */
43struct xt_led_info_internal {
Adam Nielsenb660d042010-04-09 16:51:40 +020044 struct list_head list;
45 int refcnt;
46 char *trigger_id;
Adam Nielsen268cb382009-02-20 10:55:14 +010047 struct led_trigger netfilter_led_trigger;
48 struct timer_list timer;
49};
50
51static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020052led_tg(struct sk_buff *skb, const struct xt_action_param *par)
Adam Nielsen268cb382009-02-20 10:55:14 +010053{
54 const struct xt_led_info *ledinfo = par->targinfo;
55 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
56
57 /*
58 * If "always blink" is enabled, and there's still some time until the
59 * LED will switch off, briefly switch it off now.
60 */
61 if ((ledinfo->delay > 0) && ledinfo->always_blink &&
62 timer_pending(&ledinternal->timer))
Adam Nielsenb660d042010-04-09 16:51:40 +020063 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
Adam Nielsen268cb382009-02-20 10:55:14 +010064
65 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL);
66
67 /* If there's a positive delay, start/update the timer */
68 if (ledinfo->delay > 0) {
69 mod_timer(&ledinternal->timer,
70 jiffies + msecs_to_jiffies(ledinfo->delay));
71
72 /* Otherwise if there was no delay given, blink as fast as possible */
73 } else if (ledinfo->delay == 0) {
74 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
75 }
76
77 /* else the delay is negative, which means switch on and stay on */
78
79 return XT_CONTINUE;
80}
81
82static void led_timeout_callback(unsigned long data)
83{
Adam Nielsenb660d042010-04-09 16:51:40 +020084 struct xt_led_info_internal *ledinternal = (struct xt_led_info_internal *)data;
Adam Nielsen268cb382009-02-20 10:55:14 +010085
86 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
87}
88
Adam Nielsenb660d042010-04-09 16:51:40 +020089static struct xt_led_info_internal *led_trigger_lookup(const char *name)
90{
91 struct xt_led_info_internal *ledinternal;
92
93 list_for_each_entry(ledinternal, &xt_led_triggers, list) {
94 if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) {
95 return ledinternal;
96 }
97 }
98 return NULL;
99}
100
Jan Engelhardt135367b2010-03-19 17:16:42 +0100101static int led_tg_check(const struct xt_tgchk_param *par)
Adam Nielsen268cb382009-02-20 10:55:14 +0100102{
103 struct xt_led_info *ledinfo = par->targinfo;
104 struct xt_led_info_internal *ledinternal;
105 int err;
106
107 if (ledinfo->id[0] == '\0') {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100108 pr_info("No 'id' parameter given.\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100109 return -EINVAL;
Adam Nielsen268cb382009-02-20 10:55:14 +0100110 }
111
Adam Nielsenb660d042010-04-09 16:51:40 +0200112 mutex_lock(&xt_led_mutex);
113
114 ledinternal = led_trigger_lookup(ledinfo->id);
115 if (ledinternal) {
116 ledinternal->refcnt++;
117 goto out;
118 }
119
120 err = -ENOMEM;
Adam Nielsen268cb382009-02-20 10:55:14 +0100121 ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
Jan Engelhardt85bc3f32010-03-18 00:27:03 +0100122 if (!ledinternal)
Adam Nielsenb660d042010-04-09 16:51:40 +0200123 goto exit_mutex_only;
Adam Nielsen268cb382009-02-20 10:55:14 +0100124
Adam Nielsenb660d042010-04-09 16:51:40 +0200125 ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
126 if (!ledinternal->trigger_id)
127 goto exit_internal_alloc;
128
129 ledinternal->refcnt = 1;
130 ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
Adam Nielsen268cb382009-02-20 10:55:14 +0100131
132 err = led_trigger_register(&ledinternal->netfilter_led_trigger);
133 if (err) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100134 pr_warning("led_trigger_register() failed\n");
Adam Nielsen268cb382009-02-20 10:55:14 +0100135 if (err == -EEXIST)
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100136 pr_warning("Trigger name is already in use.\n");
Adam Nielsen268cb382009-02-20 10:55:14 +0100137 goto exit_alloc;
138 }
139
140 /* See if we need to set up a timer */
141 if (ledinfo->delay > 0)
142 setup_timer(&ledinternal->timer, led_timeout_callback,
Adam Nielsenb660d042010-04-09 16:51:40 +0200143 (unsigned long)ledinternal);
144
145 list_add_tail(&ledinternal->list, &xt_led_triggers);
146
147out:
148 mutex_unlock(&xt_led_mutex);
Adam Nielsen268cb382009-02-20 10:55:14 +0100149
150 ledinfo->internal_data = ledinternal;
Adam Nielsenb660d042010-04-09 16:51:40 +0200151
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100152 return 0;
Adam Nielsen268cb382009-02-20 10:55:14 +0100153
154exit_alloc:
Adam Nielsenb660d042010-04-09 16:51:40 +0200155 kfree(ledinternal->trigger_id);
156
157exit_internal_alloc:
Adam Nielsen268cb382009-02-20 10:55:14 +0100158 kfree(ledinternal);
Adam Nielsenb660d042010-04-09 16:51:40 +0200159
160exit_mutex_only:
161 mutex_unlock(&xt_led_mutex);
162
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100163 return err;
Adam Nielsen268cb382009-02-20 10:55:14 +0100164}
165
166static void led_tg_destroy(const struct xt_tgdtor_param *par)
167{
168 const struct xt_led_info *ledinfo = par->targinfo;
169 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
170
Adam Nielsenb660d042010-04-09 16:51:40 +0200171 mutex_lock(&xt_led_mutex);
172
173 if (--ledinternal->refcnt) {
174 mutex_unlock(&xt_led_mutex);
175 return;
176 }
177
178 list_del(&ledinternal->list);
179
Adam Nielsen268cb382009-02-20 10:55:14 +0100180 if (ledinfo->delay > 0)
181 del_timer_sync(&ledinternal->timer);
182
183 led_trigger_unregister(&ledinternal->netfilter_led_trigger);
Adam Nielsenb660d042010-04-09 16:51:40 +0200184
185 mutex_unlock(&xt_led_mutex);
186
187 kfree(ledinternal->trigger_id);
Adam Nielsen268cb382009-02-20 10:55:14 +0100188 kfree(ledinternal);
189}
190
191static struct xt_target led_tg_reg __read_mostly = {
192 .name = "LED",
193 .revision = 0,
194 .family = NFPROTO_UNSPEC,
195 .target = led_tg,
Jan Engelhardt7d5f7ed2010-03-09 23:27:24 +0100196 .targetsize = sizeof(struct xt_led_info),
Adam Nielsen268cb382009-02-20 10:55:14 +0100197 .checkentry = led_tg_check,
198 .destroy = led_tg_destroy,
199 .me = THIS_MODULE,
200};
201
202static int __init led_tg_init(void)
203{
204 return xt_register_target(&led_tg_reg);
205}
206
207static void __exit led_tg_exit(void)
208{
209 xt_unregister_target(&led_tg_reg);
210}
211
212module_init(led_tg_init);
213module_exit(led_tg_exit);