at master 5.6 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 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> 6 */ 7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8#include <linux/module.h> 9#include <linux/skbuff.h> 10#include <linux/netfilter/x_tables.h> 11#include <linux/slab.h> 12#include <linux/leds.h> 13#include <linux/mutex.h> 14 15#include <linux/netfilter/xt_LED.h> 16 17MODULE_LICENSE("GPL"); 18MODULE_AUTHOR("Adam Nielsen <a.nielsen@shikadi.net>"); 19MODULE_DESCRIPTION("Xtables: trigger LED devices on packet match"); 20MODULE_ALIAS("ipt_LED"); 21MODULE_ALIAS("ip6t_LED"); 22 23static LIST_HEAD(xt_led_triggers); 24static DEFINE_MUTEX(xt_led_mutex); 25 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 */ 31struct xt_led_info_internal { 32 struct list_head list; 33 int refcnt; 34 char *trigger_id; 35 struct led_trigger netfilter_led_trigger; 36 struct timer_list timer; 37}; 38 39#define XT_LED_BLINK_DELAY 50 /* ms */ 40 41static unsigned int 42led_tg(struct sk_buff *skb, const struct xt_action_param *par) 43{ 44 const struct xt_led_info *ledinfo = par->targinfo; 45 struct xt_led_info_internal *ledinternal = ledinfo->internal_data; 46 47 /* 48 * If "always blink" is enabled, and there's still some time until the 49 * LED will switch off, briefly switch it off now. 50 */ 51 if ((ledinfo->delay > 0) && ledinfo->always_blink && 52 timer_pending(&ledinternal->timer)) 53 led_trigger_blink_oneshot(&ledinternal->netfilter_led_trigger, 54 XT_LED_BLINK_DELAY, XT_LED_BLINK_DELAY, 1); 55 else 56 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_FULL); 57 58 /* If there's a positive delay, start/update the timer */ 59 if (ledinfo->delay > 0) { 60 mod_timer(&ledinternal->timer, 61 jiffies + msecs_to_jiffies(ledinfo->delay)); 62 63 /* Otherwise if there was no delay given, blink as fast as possible */ 64 } else if (ledinfo->delay == 0) { 65 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); 66 } 67 68 /* else the delay is negative, which means switch on and stay on */ 69 70 return XT_CONTINUE; 71} 72 73static void led_timeout_callback(struct timer_list *t) 74{ 75 struct xt_led_info_internal *ledinternal = timer_container_of(ledinternal, 76 t, 77 timer); 78 79 led_trigger_event(&ledinternal->netfilter_led_trigger, LED_OFF); 80} 81 82static 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 94static int led_tg_check(const struct xt_tgchk_param *par) 95{ 96 struct xt_led_info *ledinfo = par->targinfo; 97 struct xt_led_info_internal *ledinternal; 98 int err; 99 100 /* Bail out if empty string or not a string at all. */ 101 if (ledinfo->id[0] == '\0' || 102 !memchr(ledinfo->id, '\0', sizeof(ledinfo->id))) 103 return -EINVAL; 104 105 mutex_lock(&xt_led_mutex); 106 107 ledinternal = led_trigger_lookup(ledinfo->id); 108 if (ledinternal) { 109 ledinternal->refcnt++; 110 goto out; 111 } 112 113 err = -ENOMEM; 114 ledinternal = kzalloc(sizeof(struct xt_led_info_internal), GFP_KERNEL); 115 if (!ledinternal) 116 goto exit_mutex_only; 117 118 ledinternal->trigger_id = kstrdup(ledinfo->id, GFP_KERNEL); 119 if (!ledinternal->trigger_id) 120 goto exit_internal_alloc; 121 122 ledinternal->refcnt = 1; 123 ledinternal->netfilter_led_trigger.name = ledinternal->trigger_id; 124 125 err = led_trigger_register(&ledinternal->netfilter_led_trigger); 126 if (err) { 127 pr_info_ratelimited("Trigger name is already in use.\n"); 128 goto exit_alloc; 129 } 130 131 /* Since the letinternal timer can be shared between multiple targets, 132 * always set it up, even if the current target does not need it 133 */ 134 timer_setup(&ledinternal->timer, led_timeout_callback, 0); 135 136 list_add_tail(&ledinternal->list, &xt_led_triggers); 137 138out: 139 mutex_unlock(&xt_led_mutex); 140 141 ledinfo->internal_data = ledinternal; 142 143 return 0; 144 145exit_alloc: 146 kfree(ledinternal->trigger_id); 147 148exit_internal_alloc: 149 kfree(ledinternal); 150 151exit_mutex_only: 152 mutex_unlock(&xt_led_mutex); 153 154 return err; 155} 156 157static void led_tg_destroy(const struct xt_tgdtor_param *par) 158{ 159 const struct xt_led_info *ledinfo = par->targinfo; 160 struct xt_led_info_internal *ledinternal = ledinfo->internal_data; 161 162 mutex_lock(&xt_led_mutex); 163 164 if (--ledinternal->refcnt) { 165 mutex_unlock(&xt_led_mutex); 166 return; 167 } 168 169 list_del(&ledinternal->list); 170 171 timer_shutdown_sync(&ledinternal->timer); 172 173 led_trigger_unregister(&ledinternal->netfilter_led_trigger); 174 175 mutex_unlock(&xt_led_mutex); 176 177 kfree(ledinternal->trigger_id); 178 kfree(ledinternal); 179} 180 181static struct xt_target led_tg_reg[] __read_mostly = { 182 { 183 .name = "LED", 184 .revision = 0, 185 .family = NFPROTO_IPV4, 186 .target = led_tg, 187 .targetsize = sizeof(struct xt_led_info), 188 .usersize = offsetof(struct xt_led_info, internal_data), 189 .checkentry = led_tg_check, 190 .destroy = led_tg_destroy, 191 .me = THIS_MODULE, 192 }, 193#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 194 { 195 .name = "LED", 196 .revision = 0, 197 .family = NFPROTO_IPV6, 198 .target = led_tg, 199 .targetsize = sizeof(struct xt_led_info), 200 .usersize = offsetof(struct xt_led_info, internal_data), 201 .checkentry = led_tg_check, 202 .destroy = led_tg_destroy, 203 .me = THIS_MODULE, 204 }, 205#endif 206}; 207 208static int __init led_tg_init(void) 209{ 210 return xt_register_targets(led_tg_reg, ARRAY_SIZE(led_tg_reg)); 211} 212 213static void __exit led_tg_exit(void) 214{ 215 xt_unregister_targets(led_tg_reg, ARRAY_SIZE(led_tg_reg)); 216} 217 218module_init(led_tg_init); 219module_exit(led_tg_exit);