Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 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 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 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 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 34 | static LIST_HEAD(xt_led_triggers); |
| 35 | static DEFINE_MUTEX(xt_led_mutex); |
| 36 | |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 37 | /* |
| 38 | * This is declared in here (the kernel module) only, to avoid having these |
| 39 | * dependencies in userspace code. This is what xt_led_info.internal_data |
| 40 | * points to. |
| 41 | */ |
| 42 | struct xt_led_info_internal { |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 43 | struct list_head list; |
| 44 | int refcnt; |
| 45 | char *trigger_id; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 46 | struct led_trigger netfilter_led_trigger; |
| 47 | struct timer_list timer; |
| 48 | }; |
| 49 | |
| 50 | static unsigned int |
| 51 | led_tg(struct sk_buff *skb, const struct xt_target_param *par) |
| 52 | { |
| 53 | const struct xt_led_info *ledinfo = par->targinfo; |
| 54 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; |
| 55 | |
| 56 | /* |
| 57 | * If "always blink" is enabled, and there's still some time until the |
| 58 | * LED will switch off, briefly switch it off now. |
| 59 | */ |
| 60 | if ((ledinfo->delay > 0) && ledinfo->always_blink && |
| 61 | timer_pending(&ledinternal->timer)) |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 62 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 63 | |
| 64 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL); |
| 65 | |
| 66 | /* If there's a positive delay, start/update the timer */ |
| 67 | if (ledinfo->delay > 0) { |
| 68 | mod_timer(&ledinternal->timer, |
| 69 | jiffies + msecs_to_jiffies(ledinfo->delay)); |
| 70 | |
| 71 | /* Otherwise if there was no delay given, blink as fast as possible */ |
| 72 | } else if (ledinfo->delay == 0) { |
| 73 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); |
| 74 | } |
| 75 | |
| 76 | /* else the delay is negative, which means switch on and stay on */ |
| 77 | |
| 78 | return XT_CONTINUE; |
| 79 | } |
| 80 | |
| 81 | static void led_timeout_callback(unsigned long data) |
| 82 | { |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 83 | struct xt_led_info_internal *ledinternal = (struct xt_led_info_internal *)data; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 84 | |
| 85 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); |
| 86 | } |
| 87 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 88 | static struct xt_led_info_internal *led_trigger_lookup(const char *name) |
| 89 | { |
| 90 | struct xt_led_info_internal *ledinternal; |
| 91 | |
| 92 | list_for_each_entry(ledinternal, &xt_led_triggers, list) { |
| 93 | if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) { |
| 94 | return ledinternal; |
| 95 | } |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 100 | static int led_tg_check(const struct xt_tgchk_param *par) |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 101 | { |
| 102 | struct xt_led_info *ledinfo = par->targinfo; |
| 103 | struct xt_led_info_internal *ledinternal; |
| 104 | int err; |
| 105 | |
| 106 | if (ledinfo->id[0] == '\0') { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 107 | pr_info("No 'id' parameter given.\n"); |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 108 | return -EINVAL; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 111 | mutex_lock(&xt_led_mutex); |
| 112 | |
| 113 | ledinternal = led_trigger_lookup(ledinfo->id); |
| 114 | if (ledinternal) { |
| 115 | ledinternal->refcnt++; |
| 116 | goto out; |
| 117 | } |
| 118 | |
| 119 | err = -ENOMEM; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 120 | ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL); |
Jan Engelhardt | 85bc3f3 | 2010-03-18 00:27:03 +0100 | [diff] [blame] | 121 | if (!ledinternal) |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 122 | goto exit_mutex_only; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 123 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 124 | ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL); |
| 125 | if (!ledinternal->trigger_id) |
| 126 | goto exit_internal_alloc; |
| 127 | |
| 128 | ledinternal->refcnt = 1; |
| 129 | ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 130 | |
| 131 | err = led_trigger_register(&ledinternal->netfilter_led_trigger); |
| 132 | if (err) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 133 | pr_warning("led_trigger_register() failed\n"); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 134 | if (err == -EEXIST) |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 135 | pr_warning("Trigger name is already in use.\n"); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 136 | goto exit_alloc; |
| 137 | } |
| 138 | |
| 139 | /* See if we need to set up a timer */ |
| 140 | if (ledinfo->delay > 0) |
| 141 | setup_timer(&ledinternal->timer, led_timeout_callback, |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 142 | (unsigned long)ledinternal); |
| 143 | |
| 144 | list_add_tail(&ledinternal->list, &xt_led_triggers); |
| 145 | |
| 146 | out: |
| 147 | mutex_unlock(&xt_led_mutex); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 148 | |
| 149 | ledinfo->internal_data = ledinternal; |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 150 | |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 151 | return 0; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 152 | |
| 153 | exit_alloc: |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 154 | kfree(ledinternal->trigger_id); |
| 155 | |
| 156 | exit_internal_alloc: |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 157 | kfree(ledinternal); |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 158 | |
| 159 | exit_mutex_only: |
| 160 | mutex_unlock(&xt_led_mutex); |
| 161 | |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 162 | return err; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void led_tg_destroy(const struct xt_tgdtor_param *par) |
| 166 | { |
| 167 | const struct xt_led_info *ledinfo = par->targinfo; |
| 168 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; |
| 169 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 170 | mutex_lock(&xt_led_mutex); |
| 171 | |
| 172 | if (--ledinternal->refcnt) { |
| 173 | mutex_unlock(&xt_led_mutex); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | list_del(&ledinternal->list); |
| 178 | |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 179 | if (ledinfo->delay > 0) |
| 180 | del_timer_sync(&ledinternal->timer); |
| 181 | |
| 182 | led_trigger_unregister(&ledinternal->netfilter_led_trigger); |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame^] | 183 | |
| 184 | mutex_unlock(&xt_led_mutex); |
| 185 | |
| 186 | kfree(ledinternal->trigger_id); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 187 | kfree(ledinternal); |
| 188 | } |
| 189 | |
| 190 | static struct xt_target led_tg_reg __read_mostly = { |
| 191 | .name = "LED", |
| 192 | .revision = 0, |
| 193 | .family = NFPROTO_UNSPEC, |
| 194 | .target = led_tg, |
Jan Engelhardt | 7d5f7ed | 2010-03-09 23:27:24 +0100 | [diff] [blame] | 195 | .targetsize = sizeof(struct xt_led_info), |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 196 | .checkentry = led_tg_check, |
| 197 | .destroy = led_tg_destroy, |
| 198 | .me = THIS_MODULE, |
| 199 | }; |
| 200 | |
| 201 | static int __init led_tg_init(void) |
| 202 | { |
| 203 | return xt_register_target(&led_tg_reg); |
| 204 | } |
| 205 | |
| 206 | static void __exit led_tg_exit(void) |
| 207 | { |
| 208 | xt_unregister_target(&led_tg_reg); |
| 209 | } |
| 210 | |
| 211 | module_init(led_tg_init); |
| 212 | module_exit(led_tg_exit); |