Thomas Gleixner | 935912c | 2019-05-27 08:55:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 2 | /* |
| 3 | * xt_LED.c - netfilter target to make LEDs blink upon packet matches |
| 4 | * |
| 5 | * Copyright (C) 2008 Adam Nielsen <a.nielsen@shikadi.net> |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 6 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/skbuff.h> |
| 10 | #include <linux/netfilter/x_tables.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 12 | #include <linux/leds.h> |
| 13 | #include <linux/mutex.h> |
| 14 | |
| 15 | #include <linux/netfilter/xt_LED.h> |
| 16 | |
| 17 | MODULE_LICENSE("GPL"); |
| 18 | MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>"); |
| 19 | MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match"); |
Jan Engelhardt | f1e231a | 2011-01-18 06:30:13 +0100 | [diff] [blame] | 20 | MODULE_ALIAS("ipt_LED"); |
| 21 | MODULE_ALIAS("ip6t_LED"); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 22 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 23 | static LIST_HEAD(xt_led_triggers); |
| 24 | static DEFINE_MUTEX(xt_led_mutex); |
| 25 | |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 26 | /* |
| 27 | * This is declared in here (the kernel module) only, to avoid having these |
| 28 | * dependencies in userspace code. This is what xt_led_info.internal_data |
| 29 | * points to. |
| 30 | */ |
| 31 | struct xt_led_info_internal { |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 32 | struct list_head list; |
| 33 | int refcnt; |
| 34 | char *trigger_id; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 35 | struct led_trigger netfilter_led_trigger; |
| 36 | struct timer_list timer; |
| 37 | }; |
| 38 | |
Jiri Prchal | 8452e6f | 2014-07-25 15:58:33 +0200 | [diff] [blame] | 39 | #define XT_LED_BLINK_DELAY 50 /* ms */ |
| 40 | |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 41 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 42 | led_tg(struct sk_buff *skb, const struct xt_action_param *par) |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 43 | { |
| 44 | const struct xt_led_info *ledinfo = par->targinfo; |
| 45 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; |
Jiri Prchal | 8452e6f | 2014-07-25 15:58:33 +0200 | [diff] [blame] | 46 | unsigned long led_delay = XT_LED_BLINK_DELAY; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * If "always blink" is enabled, and there's still some time until the |
| 50 | * LED will switch off, briefly switch it off now. |
| 51 | */ |
| 52 | if ((ledinfo->delay > 0) && ledinfo->always_blink && |
| 53 | timer_pending(&ledinternal->timer)) |
Jiri Prchal | 8452e6f | 2014-07-25 15:58:33 +0200 | [diff] [blame] | 54 | led_trigger_blink_oneshot(&ledinternal->netfilter_led_trigger, |
| 55 | &led_delay, &led_delay, 1); |
| 56 | else |
| 57 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 58 | |
| 59 | /* If there's a positive delay, start/update the timer */ |
| 60 | if (ledinfo->delay > 0) { |
| 61 | mod_timer(&ledinternal->timer, |
| 62 | jiffies + msecs_to_jiffies(ledinfo->delay)); |
| 63 | |
| 64 | /* Otherwise if there was no delay given, blink as fast as possible */ |
| 65 | } else if (ledinfo->delay == 0) { |
| 66 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); |
| 67 | } |
| 68 | |
| 69 | /* else the delay is negative, which means switch on and stay on */ |
| 70 | |
| 71 | return XT_CONTINUE; |
| 72 | } |
| 73 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 74 | static void led_timeout_callback(struct timer_list *t) |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 75 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 76 | struct xt_led_info_internal *ledinternal = from_timer(ledinternal, t, |
| 77 | timer); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 78 | |
| 79 | led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); |
| 80 | } |
| 81 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 82 | static struct xt_led_info_internal *led_trigger_lookup(const char *name) |
| 83 | { |
| 84 | struct xt_led_info_internal *ledinternal; |
| 85 | |
| 86 | list_for_each_entry(ledinternal, &xt_led_triggers, list) { |
| 87 | if (!strcmp(name, ledinternal->netfilter_led_trigger.name)) { |
| 88 | return ledinternal; |
| 89 | } |
| 90 | } |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 94 | static int led_tg_check(const struct xt_tgchk_param *par) |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 95 | { |
| 96 | struct xt_led_info *ledinfo = par->targinfo; |
| 97 | struct xt_led_info_internal *ledinternal; |
| 98 | int err; |
| 99 | |
Florian Westphal | 0cc9501 | 2018-02-09 15:51:59 +0100 | [diff] [blame] | 100 | if (ledinfo->id[0] == '\0') |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 101 | return -EINVAL; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 102 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 103 | mutex_lock(&xt_led_mutex); |
| 104 | |
| 105 | ledinternal = led_trigger_lookup(ledinfo->id); |
| 106 | if (ledinternal) { |
| 107 | ledinternal->refcnt++; |
| 108 | goto out; |
| 109 | } |
| 110 | |
| 111 | err = -ENOMEM; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 112 | ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL); |
Jan Engelhardt | 85bc3f3 | 2010-03-18 00:27:03 +0100 | [diff] [blame] | 113 | if (!ledinternal) |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 114 | goto exit_mutex_only; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 115 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 116 | ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL); |
| 117 | if (!ledinternal->trigger_id) |
| 118 | goto exit_internal_alloc; |
| 119 | |
| 120 | ledinternal->refcnt = 1; |
| 121 | ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 122 | |
| 123 | err = led_trigger_register(&ledinternal->netfilter_led_trigger); |
| 124 | if (err) { |
Florian Westphal | b260664 | 2018-02-09 15:52:07 +0100 | [diff] [blame] | 125 | pr_info_ratelimited("Trigger name is already in use.\n"); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 126 | goto exit_alloc; |
| 127 | } |
| 128 | |
Paolo Abeni | 1041401 | 2018-02-12 18:49:39 +0100 | [diff] [blame] | 129 | /* Since the letinternal timer can be shared between multiple targets, |
| 130 | * always set it up, even if the current target does not need it |
| 131 | */ |
| 132 | timer_setup(&ledinternal->timer, led_timeout_callback, 0); |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 133 | |
| 134 | list_add_tail(&ledinternal->list, &xt_led_triggers); |
| 135 | |
| 136 | out: |
| 137 | mutex_unlock(&xt_led_mutex); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 138 | |
| 139 | ledinfo->internal_data = ledinternal; |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 140 | |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 141 | return 0; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 142 | |
| 143 | exit_alloc: |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 144 | kfree(ledinternal->trigger_id); |
| 145 | |
| 146 | exit_internal_alloc: |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 147 | kfree(ledinternal); |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 148 | |
| 149 | exit_mutex_only: |
| 150 | mutex_unlock(&xt_led_mutex); |
| 151 | |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 152 | return err; |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void led_tg_destroy(const struct xt_tgdtor_param *par) |
| 156 | { |
| 157 | const struct xt_led_info *ledinfo = par->targinfo; |
| 158 | struct xt_led_info_internal *ledinternal = ledinfo->internal_data; |
| 159 | |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 160 | mutex_lock(&xt_led_mutex); |
| 161 | |
| 162 | if (--ledinternal->refcnt) { |
| 163 | mutex_unlock(&xt_led_mutex); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | list_del(&ledinternal->list); |
| 168 | |
Paolo Abeni | 1041401 | 2018-02-12 18:49:39 +0100 | [diff] [blame] | 169 | del_timer_sync(&ledinternal->timer); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 170 | |
| 171 | led_trigger_unregister(&ledinternal->netfilter_led_trigger); |
Adam Nielsen | b660d04 | 2010-04-09 16:51:40 +0200 | [diff] [blame] | 172 | |
| 173 | mutex_unlock(&xt_led_mutex); |
| 174 | |
| 175 | kfree(ledinternal->trigger_id); |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 176 | kfree(ledinternal); |
| 177 | } |
| 178 | |
| 179 | static struct xt_target led_tg_reg __read_mostly = { |
| 180 | .name = "LED", |
| 181 | .revision = 0, |
| 182 | .family = NFPROTO_UNSPEC, |
| 183 | .target = led_tg, |
Jan Engelhardt | 7d5f7ed | 2010-03-09 23:27:24 +0100 | [diff] [blame] | 184 | .targetsize = sizeof(struct xt_led_info), |
Dmitry Vyukov | 1e98ffe | 2018-01-29 13:21:20 +0100 | [diff] [blame] | 185 | .usersize = offsetof(struct xt_led_info, internal_data), |
Adam Nielsen | 268cb38 | 2009-02-20 10:55:14 +0100 | [diff] [blame] | 186 | .checkentry = led_tg_check, |
| 187 | .destroy = led_tg_destroy, |
| 188 | .me = THIS_MODULE, |
| 189 | }; |
| 190 | |
| 191 | static int __init led_tg_init(void) |
| 192 | { |
| 193 | return xt_register_target(&led_tg_reg); |
| 194 | } |
| 195 | |
| 196 | static void __exit led_tg_exit(void) |
| 197 | { |
| 198 | xt_unregister_target(&led_tg_reg); |
| 199 | } |
| 200 | |
| 201 | module_init(led_tg_init); |
| 202 | module_exit(led_tg_exit); |