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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/slab.h>
26 #include <linux/leds.h>
27 #include <linux/mutex.h>
28 
29 #include <linux/netfilter/xt_LED.h>
30 
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>");
33 MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match");
34 MODULE_ALIAS("ipt_LED");
35 MODULE_ALIAS("ip6t_LED");
36 
37 static LIST_HEAD(xt_led_triggers);
38 static DEFINE_MUTEX(xt_led_mutex);
39 
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  */
45 struct xt_led_info_internal {
46 	struct list_head list;
47 	int refcnt;
48 	char *trigger_id;
49 	struct led_trigger netfilter_led_trigger;
50 	struct timer_list timer;
51 };
52 
53 #define XT_LED_BLINK_DELAY 50 /* ms */
54 
55 static unsigned int
led_tg(struct sk_buff * skb,const struct xt_action_param * par)56 led_tg(struct sk_buff *skb, const struct xt_action_param *par)
57 {
58 	const struct xt_led_info *ledinfo = par->targinfo;
59 	struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
60 	unsigned long led_delay = XT_LED_BLINK_DELAY;
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))
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);
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 
led_timeout_callback(struct timer_list * t)88 static void led_timeout_callback(struct timer_list *t)
89 {
90 	struct xt_led_info_internal *ledinternal = from_timer(ledinternal, t,
91 							      timer);
92 
93 	led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF);
94 }
95 
led_trigger_lookup(const char * name)96 static 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 
led_tg_check(const struct xt_tgchk_param * par)108 static int led_tg_check(const struct xt_tgchk_param *par)
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')
115 		return -EINVAL;
116 
117 	mutex_lock(&xt_led_mutex);
118 
119 	ledinternal = led_trigger_lookup(ledinfo->id);
120 	if (ledinternal) {
121 		ledinternal->refcnt++;
122 		goto out;
123 	}
124 
125 	err = -ENOMEM;
126 	ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL);
127 	if (!ledinternal)
128 		goto exit_mutex_only;
129 
130 	ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL);
131 	if (!ledinternal->trigger_id)
132 		goto exit_internal_alloc;
133 
134 	ledinternal->refcnt = 1;
135 	ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id;
136 
137 	err = led_trigger_register(&ledinternal->netfilter_led_trigger);
138 	if (err) {
139 		pr_info_ratelimited("Trigger name is already in use.\n");
140 		goto exit_alloc;
141 	}
142 
143 	/* Since the letinternal timer can be shared between multiple targets,
144 	 * always set it up, even if the current target does not need it
145 	 */
146 	timer_setup(&ledinternal->timer, led_timeout_callback, 0);
147 
148 	list_add_tail(&ledinternal->list, &xt_led_triggers);
149 
150 out:
151 	mutex_unlock(&xt_led_mutex);
152 
153 	ledinfo->internal_data = ledinternal;
154 
155 	return 0;
156 
157 exit_alloc:
158 	kfree(ledinternal->trigger_id);
159 
160 exit_internal_alloc:
161 	kfree(ledinternal);
162 
163 exit_mutex_only:
164 	mutex_unlock(&xt_led_mutex);
165 
166 	return err;
167 }
168 
led_tg_destroy(const struct xt_tgdtor_param * par)169 static void led_tg_destroy(const struct xt_tgdtor_param *par)
170 {
171 	const struct xt_led_info *ledinfo = par->targinfo;
172 	struct xt_led_info_internal *ledinternal = ledinfo->internal_data;
173 
174 	mutex_lock(&xt_led_mutex);
175 
176 	if (--ledinternal->refcnt) {
177 		mutex_unlock(&xt_led_mutex);
178 		return;
179 	}
180 
181 	list_del(&ledinternal->list);
182 
183 	del_timer_sync(&ledinternal->timer);
184 
185 	led_trigger_unregister(&ledinternal->netfilter_led_trigger);
186 
187 	mutex_unlock(&xt_led_mutex);
188 
189 	kfree(ledinternal->trigger_id);
190 	kfree(ledinternal);
191 }
192 
193 static struct xt_target led_tg_reg __read_mostly = {
194 	.name		= "LED",
195 	.revision	= 0,
196 	.family		= NFPROTO_UNSPEC,
197 	.target		= led_tg,
198 	.targetsize	= sizeof(struct xt_led_info),
199 	.usersize	= offsetof(struct xt_led_info, internal_data),
200 	.checkentry	= led_tg_check,
201 	.destroy	= led_tg_destroy,
202 	.me		= THIS_MODULE,
203 };
204 
led_tg_init(void)205 static int __init led_tg_init(void)
206 {
207 	return xt_register_target(&led_tg_reg);
208 }
209 
led_tg_exit(void)210 static void __exit led_tg_exit(void)
211 {
212 	xt_unregister_target(&led_tg_reg);
213 }
214 
215 module_init(led_tg_init);
216 module_exit(led_tg_exit);
217