]>
Commit | Line | Data |
---|---|---|
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> | |
25 | #include <linux/leds.h> | |
26 | #include <linux/mutex.h> | |
27 | ||
28 | #include <linux/netfilter/xt_LED.h> | |
29 | ||
30 | MODULE_LICENSE("GPL"); | |
31 | MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>"); | |
32 | MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match"); | |
33 | ||
34 | /* | |
35 | * This is declared in here (the kernel module) only, to avoid having these | |
36 | * dependencies in userspace code. This is what xt_led_info.internal_data | |
37 | * points to. | |
38 | */ | |
39 | struct xt_led_info_internal { | |
40 | struct led_trigger netfilter_led_trigger; | |
41 | struct timer_list timer; | |
42 | }; | |
43 | ||
44 | static unsigned int | |
45 | led_tg(struct sk_buff *skb, const struct xt_target_param *par) | |
46 | { | |
47 | const struct xt_led_info *ledinfo = par->targinfo; | |
48 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; | |
49 | ||
50 | /* | |
51 | * If "always blink" is enabled, and there's still some time until the | |
52 | * LED will switch off, briefly switch it off now. | |
53 | */ | |
54 | if ((ledinfo->delay > 0) && ledinfo->always_blink && | |
55 | timer_pending(&ledinternal->timer)) | |
56 | led_trigger_event(&ledinternal->netfilter_led_trigger,LED_OFF); | |
57 | ||
58 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL); | |
59 | ||
60 | /* If there's a positive delay, start/update the timer */ | |
61 | if (ledinfo->delay > 0) { | |
62 | mod_timer(&ledinternal->timer, | |
63 | jiffies + msecs_to_jiffies(ledinfo->delay)); | |
64 | ||
65 | /* Otherwise if there was no delay given, blink as fast as possible */ | |
66 | } else if (ledinfo->delay == 0) { | |
67 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); | |
68 | } | |
69 | ||
70 | /* else the delay is negative, which means switch on and stay on */ | |
71 | ||
72 | return XT_CONTINUE; | |
73 | } | |
74 | ||
75 | static void led_timeout_callback(unsigned long data) | |
76 | { | |
77 | struct xt_led_info *ledinfo = (struct xt_led_info *)data; | |
78 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; | |
79 | ||
80 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); | |
81 | } | |
82 | ||
135367b8 | 83 | static int led_tg_check(const struct xt_tgchk_param *par) |
268cb38e AN |
84 | { |
85 | struct xt_led_info *ledinfo = par->targinfo; | |
86 | struct xt_led_info_internal *ledinternal; | |
87 | int err; | |
88 | ||
89 | if (ledinfo->id[0] == '\0') { | |
8bee4bad | 90 | pr_info("No 'id' parameter given.\n"); |
d6b00a53 | 91 | return -EINVAL; |
268cb38e AN |
92 | } |
93 | ||
94 | ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL); | |
85bc3f38 | 95 | if (!ledinternal) |
4a5a5c73 | 96 | return -ENOMEM; |
268cb38e AN |
97 | |
98 | ledinternal->netfilter_led_trigger.name = ledinfo->id; | |
99 | ||
100 | err = led_trigger_register(&ledinternal->netfilter_led_trigger); | |
101 | if (err) { | |
8bee4bad | 102 | pr_warning("led_trigger_register() failed\n"); |
268cb38e | 103 | if (err == -EEXIST) |
8bee4bad | 104 | pr_warning("Trigger name is already in use.\n"); |
268cb38e AN |
105 | goto exit_alloc; |
106 | } | |
107 | ||
108 | /* See if we need to set up a timer */ | |
109 | if (ledinfo->delay > 0) | |
110 | setup_timer(&ledinternal->timer, led_timeout_callback, | |
111 | (unsigned long)ledinfo); | |
112 | ||
113 | ledinfo->internal_data = ledinternal; | |
d6b00a53 | 114 | return 0; |
268cb38e AN |
115 | |
116 | exit_alloc: | |
117 | kfree(ledinternal); | |
4a5a5c73 | 118 | return err; |
268cb38e AN |
119 | } |
120 | ||
121 | static void led_tg_destroy(const struct xt_tgdtor_param *par) | |
122 | { | |
123 | const struct xt_led_info *ledinfo = par->targinfo; | |
124 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; | |
125 | ||
126 | if (ledinfo->delay > 0) | |
127 | del_timer_sync(&ledinternal->timer); | |
128 | ||
129 | led_trigger_unregister(&ledinternal->netfilter_led_trigger); | |
130 | kfree(ledinternal); | |
131 | } | |
132 | ||
133 | static struct xt_target led_tg_reg __read_mostly = { | |
134 | .name = "LED", | |
135 | .revision = 0, | |
136 | .family = NFPROTO_UNSPEC, | |
137 | .target = led_tg, | |
7d5f7ed8 | 138 | .targetsize = sizeof(struct xt_led_info), |
268cb38e AN |
139 | .checkentry = led_tg_check, |
140 | .destroy = led_tg_destroy, | |
141 | .me = THIS_MODULE, | |
142 | }; | |
143 | ||
144 | static int __init led_tg_init(void) | |
145 | { | |
146 | return xt_register_target(&led_tg_reg); | |
147 | } | |
148 | ||
149 | static void __exit led_tg_exit(void) | |
150 | { | |
151 | xt_unregister_target(&led_tg_reg); | |
152 | } | |
153 | ||
154 | module_init(led_tg_init); | |
155 | module_exit(led_tg_exit); |