]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_LED.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.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 */
8bee4bad 21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
268cb38e
AN
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");
f1e231a3
JE
34MODULE_ALIAS("ipt_LED");
35MODULE_ALIAS("ip6t_LED");
268cb38e 36
b660d048
AN
37static LIST_HEAD(xt_led_triggers);
38static DEFINE_MUTEX(xt_led_mutex);
39
268cb38e
AN
40/*
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 {
b660d048
AN
46 struct list_head list;
47 int refcnt;
48 char *trigger_id;
268cb38e
AN
49 struct led_trigger netfilter_led_trigger;
50 struct timer_list timer;
51};
52
8452e6ff
JP
53#define XT_LED_BLINK_DELAY 50 /* ms */
54
268cb38e 55static unsigned int
4b560b44 56led_tg(struct sk_buff *skb, const struct xt_action_param *par)
268cb38e
AN
57{
58 const struct xt_led_info *ledinfo = par->targinfo;
59 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
8452e6ff 60 unsigned long led_delay = XT_LED_BLINK_DELAY;
268cb38e
AN
61
62 /*
63 * If "always blink" is enabled, and there's still some time until the
64 * LED will switch off, briefly switch it off now.
65 */
66 if ((ledinfo->delay > 0) && ledinfo->always_blink &&
67 timer_pending(&ledinternal->timer))
8452e6ff
JP
68 led_trigger_blink_oneshot(&ledinternal->netfilter_led_trigger,
69 &led_delay, &led_delay, 1);
70 else
71 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL);
268cb38e
AN
72
73 /* If there's a positive delay, start/update the timer */
74 if (ledinfo->delay > 0) {
75 mod_timer(&ledinternal->timer,
76 jiffies + msecs_to_jiffies(ledinfo->delay));
77
78 /* Otherwise if there was no delay given, blink as fast as possible */
79 } else if (ledinfo->delay == 0) {
80 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
81 }
82
83 /* else the delay is negative, which means switch on and stay on */
84
85 return XT_CONTINUE;
86}
87
e99e88a9 88static void led_timeout_callback(struct timer_list *t)
268cb38e 89{
e99e88a9
KC
90 struct xt_led_info_internal *ledinternal = from_timer(ledinternal, t,
91 timer);
268cb38e
AN
92
93 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
94}
95
b660d048
AN
96static struct xt_led_info_internal *led_trigger_lookup(const char *name)
97{
98 struct xt_led_info_internal *ledinternal;
99
100 list_for_each_entry(ledinternal, &xt_led_triggers, list) {
101 if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) {
102 return ledinternal;
103 }
104 }
105 return NULL;
106}
107
135367b8 108static int led_tg_check(const struct xt_tgchk_param *par)
268cb38e
AN
109{
110 struct xt_led_info *ledinfo = par->targinfo;
111 struct xt_led_info_internal *ledinternal;
112 int err;
113
114 if (ledinfo->id[0] == '\0') {
8bee4bad 115 pr_info("No 'id' parameter given.\n");
d6b00a53 116 return -EINVAL;
268cb38e
AN
117 }
118
b660d048
AN
119 mutex_lock(&xt_led_mutex);
120
121 ledinternal = led_trigger_lookup(ledinfo->id);
122 if (ledinternal) {
123 ledinternal->refcnt++;
124 goto out;
125 }
126
127 err = -ENOMEM;
268cb38e 128 ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
85bc3f38 129 if (!ledinternal)
b660d048
AN
130 goto exit_mutex_only;
131
132 ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
133 if (!ledinternal->trigger_id)
134 goto exit_internal_alloc;
268cb38e 135
b660d048
AN
136 ledinternal->refcnt = 1;
137 ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
268cb38e
AN
138
139 err = led_trigger_register(&ledinternal->netfilter_led_trigger);
140 if (err) {
a2b60c75 141 pr_err("Trigger name is already in use.\n");
268cb38e
AN
142 goto exit_alloc;
143 }
144
4f4c1eae
PA
145 /* Since the letinternal timer can be shared between multiple targets,
146 * always set it up, even if the current target does not need it
147 */
148 timer_setup(&ledinternal->timer, led_timeout_callback, 0);
b660d048
AN
149
150 list_add_tail(&ledinternal->list, &xt_led_triggers);
151
152out:
153 mutex_unlock(&xt_led_mutex);
268cb38e
AN
154
155 ledinfo->internal_data = ledinternal;
b660d048 156
d6b00a53 157 return 0;
268cb38e
AN
158
159exit_alloc:
b660d048
AN
160 kfree(ledinternal->trigger_id);
161
162exit_internal_alloc:
268cb38e 163 kfree(ledinternal);
b660d048
AN
164
165exit_mutex_only:
166 mutex_unlock(&xt_led_mutex);
167
4a5a5c73 168 return err;
268cb38e
AN
169}
170
171static void led_tg_destroy(const struct xt_tgdtor_param *par)
172{
173 const struct xt_led_info *ledinfo = par->targinfo;
174 struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
175
b660d048
AN
176 mutex_lock(&xt_led_mutex);
177
178 if (--ledinternal->refcnt) {
179 mutex_unlock(&xt_led_mutex);
180 return;
181 }
182
183 list_del(&ledinternal->list);
184
4f4c1eae 185 del_timer_sync(&ledinternal->timer);
268cb38e
AN
186
187 led_trigger_unregister(&ledinternal->netfilter_led_trigger);
b660d048
AN
188
189 mutex_unlock(&xt_led_mutex);
190
191 kfree(ledinternal->trigger_id);
268cb38e
AN
192 kfree(ledinternal);
193}
194
195static struct xt_target led_tg_reg __read_mostly = {
196 .name = "LED",
197 .revision = 0,
198 .family = NFPROTO_UNSPEC,
199 .target = led_tg,
7d5f7ed8 200 .targetsize = sizeof(struct xt_led_info),
9a0e1295 201 .usersize = offsetof(struct xt_led_info, internal_data),
268cb38e
AN
202 .checkentry = led_tg_check,
203 .destroy = led_tg_destroy,
204 .me = THIS_MODULE,
205};
206
207static int __init led_tg_init(void)
208{
209 return xt_register_target(&led_tg_reg);
210}
211
212static void __exit led_tg_exit(void)
213{
214 xt_unregister_target(&led_tg_reg);
215}
216
217module_init(led_tg_init);
218module_exit(led_tg_exit);