include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / net / netfilter / xt_LED.c
CommitLineData
268cb38e
AN
1/*
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 */
21
22#include <linux/module.h>
23#include <linux/skbuff.h>
24#include <linux/netfilter/x_tables.h>
5a0e3ad6 25#include <linux/slab.h>
268cb38e
AN
26#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
35/*
36 * This is declared in here (the kernel module) only, to avoid having these
37 * dependencies in userspace code. This is what xt_led_info.internal_data
38 * points to.
39 */
40struct xt_led_info_internal {
41 struct led_trigger netfilter_led_trigger;
42 struct timer_list timer;
43};
44
45static unsigned int
46led_tg(struct sk_buff *skb, const struct xt_target_param *par)
47{
48 const struct xt_led_info *ledinfo = par->targinfo;
49 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
50
51 /*
52 * If "always blink" is enabled, and there's still some time until the
53 * LED will switch off, briefly switch it off now.
54 */
55 if ((ledinfo->delay > 0) && ledinfo->always_blink &&
56 timer_pending(&ledinternal->timer))
57 led_trigger_event(&ledinternal->netfilter_led_trigger,LED_OFF);
58
59 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL);
60
61 /* If there's a positive delay, start/update the timer */
62 if (ledinfo->delay > 0) {
63 mod_timer(&ledinternal->timer,
64 jiffies + msecs_to_jiffies(ledinfo->delay));
65
66 /* Otherwise if there was no delay given, blink as fast as possible */
67 } else if (ledinfo->delay == 0) {
68 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
69 }
70
71 /* else the delay is negative, which means switch on and stay on */
72
73 return XT_CONTINUE;
74}
75
76static void led_timeout_callback(unsigned long data)
77{
78 struct xt_led_info *ledinfo = (struct xt_led_info *)data;
79 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
80
81 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
82}
83
84static bool led_tg_check(const struct xt_tgchk_param *par)
85{
86 struct xt_led_info *ledinfo = par->targinfo;
87 struct xt_led_info_internal *ledinternal;
88 int err;
89
90 if (ledinfo->id[0] == '\0') {
91 printk(KERN_ERR KBUILD_MODNAME ": No 'id' parameter given.\n");
92 return false;
93 }
94
95 ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
96 if (!ledinternal) {
97 printk(KERN_CRIT KBUILD_MODNAME ": out of memory\n");
98 return false;
99 }
100
101 ledinternal->netfilter_led_trigger.name = ledinfo->id;
102
103 err = led_trigger_register(&ledinternal->netfilter_led_trigger);
104 if (err) {
105 printk(KERN_CRIT KBUILD_MODNAME
106 ": led_trigger_register() failed\n");
107 if (err == -EEXIST)
108 printk(KERN_ERR KBUILD_MODNAME
109 ": Trigger name is already in use.\n");
110 goto exit_alloc;
111 }
112
113 /* See if we need to set up a timer */
114 if (ledinfo->delay > 0)
115 setup_timer(&ledinternal->timer, led_timeout_callback,
116 (unsigned long)ledinfo);
117
118 ledinfo->internal_data = ledinternal;
119
120 return true;
121
122exit_alloc:
123 kfree(ledinternal);
124
125 return false;
126}
127
128static void led_tg_destroy(const struct xt_tgdtor_param *par)
129{
130 const struct xt_led_info *ledinfo = par->targinfo;
131 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
132
133 if (ledinfo->delay > 0)
134 del_timer_sync(&ledinternal->timer);
135
136 led_trigger_unregister(&ledinternal->netfilter_led_trigger);
137 kfree(ledinternal);
138}
139
140static struct xt_target led_tg_reg __read_mostly = {
141 .name = "LED",
142 .revision = 0,
143 .family = NFPROTO_UNSPEC,
144 .target = led_tg,
145 .targetsize = XT_ALIGN(sizeof(struct xt_led_info)),
146 .checkentry = led_tg_check,
147 .destroy = led_tg_destroy,
148 .me = THIS_MODULE,
149};
150
151static int __init led_tg_init(void)
152{
153 return xt_register_target(&led_tg_reg);
154}
155
156static void __exit led_tg_exit(void)
157{
158 xt_unregister_target(&led_tg_reg);
159}
160
161module_init(led_tg_init);
162module_exit(led_tg_exit);