Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/net/netfilter/xt_IDLETIMER.c
0004  *
0005  * Netfilter module to trigger a timer when packet matches.
0006  * After timer expires a kevent will be sent.
0007  *
0008  * Copyright (C) 2004, 2010 Nokia Corporation
0009  * Written by Timo Teras <ext-timo.teras@nokia.com>
0010  *
0011  * Converted to x_tables and reworked for upstream inclusion
0012  * by Luciano Coelho <luciano.coelho@nokia.com>
0013  *
0014  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
0015  */
0016 
0017 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0018 
0019 #include <linux/module.h>
0020 #include <linux/timer.h>
0021 #include <linux/alarmtimer.h>
0022 #include <linux/list.h>
0023 #include <linux/mutex.h>
0024 #include <linux/netfilter.h>
0025 #include <linux/netfilter/x_tables.h>
0026 #include <linux/netfilter/xt_IDLETIMER.h>
0027 #include <linux/kdev_t.h>
0028 #include <linux/kobject.h>
0029 #include <linux/workqueue.h>
0030 #include <linux/sysfs.h>
0031 
0032 struct idletimer_tg {
0033     struct list_head entry;
0034     struct alarm alarm;
0035     struct timer_list timer;
0036     struct work_struct work;
0037 
0038     struct kobject *kobj;
0039     struct device_attribute attr;
0040 
0041     unsigned int refcnt;
0042     u8 timer_type;
0043 };
0044 
0045 static LIST_HEAD(idletimer_tg_list);
0046 static DEFINE_MUTEX(list_mutex);
0047 
0048 static struct kobject *idletimer_tg_kobj;
0049 
0050 static
0051 struct idletimer_tg *__idletimer_tg_find_by_label(const char *label)
0052 {
0053     struct idletimer_tg *entry;
0054 
0055     list_for_each_entry(entry, &idletimer_tg_list, entry) {
0056         if (!strcmp(label, entry->attr.attr.name))
0057             return entry;
0058     }
0059 
0060     return NULL;
0061 }
0062 
0063 static ssize_t idletimer_tg_show(struct device *dev,
0064                  struct device_attribute *attr, char *buf)
0065 {
0066     struct idletimer_tg *timer;
0067     unsigned long expires = 0;
0068     struct timespec64 ktimespec = {};
0069     long time_diff = 0;
0070 
0071     mutex_lock(&list_mutex);
0072 
0073     timer = __idletimer_tg_find_by_label(attr->attr.name);
0074     if (timer) {
0075         if (timer->timer_type & XT_IDLETIMER_ALARM) {
0076             ktime_t expires_alarm = alarm_expires_remaining(&timer->alarm);
0077             ktimespec = ktime_to_timespec64(expires_alarm);
0078             time_diff = ktimespec.tv_sec;
0079         } else {
0080             expires = timer->timer.expires;
0081             time_diff = jiffies_to_msecs(expires - jiffies) / 1000;
0082         }
0083     }
0084 
0085     mutex_unlock(&list_mutex);
0086 
0087     if (time_after(expires, jiffies) || ktimespec.tv_sec > 0)
0088         return sysfs_emit(buf, "%ld\n", time_diff);
0089 
0090     return sysfs_emit(buf, "0\n");
0091 }
0092 
0093 static void idletimer_tg_work(struct work_struct *work)
0094 {
0095     struct idletimer_tg *timer = container_of(work, struct idletimer_tg,
0096                           work);
0097 
0098     sysfs_notify(idletimer_tg_kobj, NULL, timer->attr.attr.name);
0099 }
0100 
0101 static void idletimer_tg_expired(struct timer_list *t)
0102 {
0103     struct idletimer_tg *timer = from_timer(timer, t, timer);
0104 
0105     pr_debug("timer %s expired\n", timer->attr.attr.name);
0106 
0107     schedule_work(&timer->work);
0108 }
0109 
0110 static enum alarmtimer_restart idletimer_tg_alarmproc(struct alarm *alarm,
0111                               ktime_t now)
0112 {
0113     struct idletimer_tg *timer = alarm->data;
0114 
0115     pr_debug("alarm %s expired\n", timer->attr.attr.name);
0116     schedule_work(&timer->work);
0117     return ALARMTIMER_NORESTART;
0118 }
0119 
0120 static int idletimer_check_sysfs_name(const char *name, unsigned int size)
0121 {
0122     int ret;
0123 
0124     ret = xt_check_proc_name(name, size);
0125     if (ret < 0)
0126         return ret;
0127 
0128     if (!strcmp(name, "power") ||
0129         !strcmp(name, "subsystem") ||
0130         !strcmp(name, "uevent"))
0131         return -EINVAL;
0132 
0133     return 0;
0134 }
0135 
0136 static int idletimer_tg_create(struct idletimer_tg_info *info)
0137 {
0138     int ret;
0139 
0140     info->timer = kzalloc(sizeof(*info->timer), GFP_KERNEL);
0141     if (!info->timer) {
0142         ret = -ENOMEM;
0143         goto out;
0144     }
0145 
0146     ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
0147     if (ret < 0)
0148         goto out_free_timer;
0149 
0150     sysfs_attr_init(&info->timer->attr.attr);
0151     info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
0152     if (!info->timer->attr.attr.name) {
0153         ret = -ENOMEM;
0154         goto out_free_timer;
0155     }
0156     info->timer->attr.attr.mode = 0444;
0157     info->timer->attr.show = idletimer_tg_show;
0158 
0159     ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
0160     if (ret < 0) {
0161         pr_debug("couldn't add file to sysfs");
0162         goto out_free_attr;
0163     }
0164 
0165     list_add(&info->timer->entry, &idletimer_tg_list);
0166 
0167     timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
0168     info->timer->refcnt = 1;
0169 
0170     INIT_WORK(&info->timer->work, idletimer_tg_work);
0171 
0172     mod_timer(&info->timer->timer,
0173           msecs_to_jiffies(info->timeout * 1000) + jiffies);
0174 
0175     return 0;
0176 
0177 out_free_attr:
0178     kfree(info->timer->attr.attr.name);
0179 out_free_timer:
0180     kfree(info->timer);
0181 out:
0182     return ret;
0183 }
0184 
0185 static int idletimer_tg_create_v1(struct idletimer_tg_info_v1 *info)
0186 {
0187     int ret;
0188 
0189     info->timer = kmalloc(sizeof(*info->timer), GFP_KERNEL);
0190     if (!info->timer) {
0191         ret = -ENOMEM;
0192         goto out;
0193     }
0194 
0195     ret = idletimer_check_sysfs_name(info->label, sizeof(info->label));
0196     if (ret < 0)
0197         goto out_free_timer;
0198 
0199     sysfs_attr_init(&info->timer->attr.attr);
0200     info->timer->attr.attr.name = kstrdup(info->label, GFP_KERNEL);
0201     if (!info->timer->attr.attr.name) {
0202         ret = -ENOMEM;
0203         goto out_free_timer;
0204     }
0205     info->timer->attr.attr.mode = 0444;
0206     info->timer->attr.show = idletimer_tg_show;
0207 
0208     ret = sysfs_create_file(idletimer_tg_kobj, &info->timer->attr.attr);
0209     if (ret < 0) {
0210         pr_debug("couldn't add file to sysfs");
0211         goto out_free_attr;
0212     }
0213 
0214     /*  notify userspace  */
0215     kobject_uevent(idletimer_tg_kobj,KOBJ_ADD);
0216 
0217     list_add(&info->timer->entry, &idletimer_tg_list);
0218     pr_debug("timer type value is %u", info->timer_type);
0219     info->timer->timer_type = info->timer_type;
0220     info->timer->refcnt = 1;
0221 
0222     INIT_WORK(&info->timer->work, idletimer_tg_work);
0223 
0224     if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
0225         ktime_t tout;
0226         alarm_init(&info->timer->alarm, ALARM_BOOTTIME,
0227                idletimer_tg_alarmproc);
0228         info->timer->alarm.data = info->timer;
0229         tout = ktime_set(info->timeout, 0);
0230         alarm_start_relative(&info->timer->alarm, tout);
0231     } else {
0232         timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
0233         mod_timer(&info->timer->timer,
0234                 msecs_to_jiffies(info->timeout * 1000) + jiffies);
0235     }
0236 
0237     return 0;
0238 
0239 out_free_attr:
0240     kfree(info->timer->attr.attr.name);
0241 out_free_timer:
0242     kfree(info->timer);
0243 out:
0244     return ret;
0245 }
0246 
0247 /*
0248  * The actual xt_tables plugin.
0249  */
0250 static unsigned int idletimer_tg_target(struct sk_buff *skb,
0251                      const struct xt_action_param *par)
0252 {
0253     const struct idletimer_tg_info *info = par->targinfo;
0254 
0255     pr_debug("resetting timer %s, timeout period %u\n",
0256          info->label, info->timeout);
0257 
0258     mod_timer(&info->timer->timer,
0259           msecs_to_jiffies(info->timeout * 1000) + jiffies);
0260 
0261     return XT_CONTINUE;
0262 }
0263 
0264 /*
0265  * The actual xt_tables plugin.
0266  */
0267 static unsigned int idletimer_tg_target_v1(struct sk_buff *skb,
0268                      const struct xt_action_param *par)
0269 {
0270     const struct idletimer_tg_info_v1 *info = par->targinfo;
0271 
0272     pr_debug("resetting timer %s, timeout period %u\n",
0273          info->label, info->timeout);
0274 
0275     if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
0276         ktime_t tout = ktime_set(info->timeout, 0);
0277         alarm_start_relative(&info->timer->alarm, tout);
0278     } else {
0279         mod_timer(&info->timer->timer,
0280                 msecs_to_jiffies(info->timeout * 1000) + jiffies);
0281     }
0282 
0283     return XT_CONTINUE;
0284 }
0285 
0286 static int idletimer_tg_helper(struct idletimer_tg_info *info)
0287 {
0288     if (info->timeout == 0) {
0289         pr_debug("timeout value is zero\n");
0290         return -EINVAL;
0291     }
0292     if (info->timeout >= INT_MAX / 1000) {
0293         pr_debug("timeout value is too big\n");
0294         return -EINVAL;
0295     }
0296     if (info->label[0] == '\0' ||
0297         strnlen(info->label,
0298             MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
0299         pr_debug("label is empty or not nul-terminated\n");
0300         return -EINVAL;
0301     }
0302     return 0;
0303 }
0304 
0305 
0306 static int idletimer_tg_checkentry(const struct xt_tgchk_param *par)
0307 {
0308     struct idletimer_tg_info *info = par->targinfo;
0309     int ret;
0310 
0311     pr_debug("checkentry targinfo%s\n", info->label);
0312 
0313     ret = idletimer_tg_helper(info);
0314     if(ret < 0)
0315     {
0316         pr_debug("checkentry helper return invalid\n");
0317         return -EINVAL;
0318     }
0319     mutex_lock(&list_mutex);
0320 
0321     info->timer = __idletimer_tg_find_by_label(info->label);
0322     if (info->timer) {
0323         info->timer->refcnt++;
0324         mod_timer(&info->timer->timer,
0325               msecs_to_jiffies(info->timeout * 1000) + jiffies);
0326 
0327         pr_debug("increased refcnt of timer %s to %u\n",
0328              info->label, info->timer->refcnt);
0329     } else {
0330         ret = idletimer_tg_create(info);
0331         if (ret < 0) {
0332             pr_debug("failed to create timer\n");
0333             mutex_unlock(&list_mutex);
0334             return ret;
0335         }
0336     }
0337 
0338     mutex_unlock(&list_mutex);
0339     return 0;
0340 }
0341 
0342 static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param *par)
0343 {
0344     struct idletimer_tg_info_v1 *info = par->targinfo;
0345     int ret;
0346 
0347     pr_debug("checkentry targinfo%s\n", info->label);
0348 
0349     if (info->send_nl_msg)
0350         return -EOPNOTSUPP;
0351 
0352     ret = idletimer_tg_helper((struct idletimer_tg_info *)info);
0353     if(ret < 0)
0354     {
0355         pr_debug("checkentry helper return invalid\n");
0356         return -EINVAL;
0357     }
0358 
0359     if (info->timer_type > XT_IDLETIMER_ALARM) {
0360         pr_debug("invalid value for timer type\n");
0361         return -EINVAL;
0362     }
0363 
0364     mutex_lock(&list_mutex);
0365 
0366     info->timer = __idletimer_tg_find_by_label(info->label);
0367     if (info->timer) {
0368         if (info->timer->timer_type != info->timer_type) {
0369             pr_debug("Adding/Replacing rule with same label and different timer type is not allowed\n");
0370             mutex_unlock(&list_mutex);
0371             return -EINVAL;
0372         }
0373 
0374         info->timer->refcnt++;
0375         if (info->timer_type & XT_IDLETIMER_ALARM) {
0376             /* calculate remaining expiry time */
0377             ktime_t tout = alarm_expires_remaining(&info->timer->alarm);
0378             struct timespec64 ktimespec = ktime_to_timespec64(tout);
0379 
0380             if (ktimespec.tv_sec > 0) {
0381                 pr_debug("time_expiry_remaining %lld\n",
0382                      ktimespec.tv_sec);
0383                 alarm_start_relative(&info->timer->alarm, tout);
0384             }
0385         } else {
0386                 mod_timer(&info->timer->timer,
0387                     msecs_to_jiffies(info->timeout * 1000) + jiffies);
0388         }
0389         pr_debug("increased refcnt of timer %s to %u\n",
0390              info->label, info->timer->refcnt);
0391     } else {
0392         ret = idletimer_tg_create_v1(info);
0393         if (ret < 0) {
0394             pr_debug("failed to create timer\n");
0395             mutex_unlock(&list_mutex);
0396             return ret;
0397         }
0398     }
0399 
0400     mutex_unlock(&list_mutex);
0401     return 0;
0402 }
0403 
0404 static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
0405 {
0406     const struct idletimer_tg_info *info = par->targinfo;
0407 
0408     pr_debug("destroy targinfo %s\n", info->label);
0409 
0410     mutex_lock(&list_mutex);
0411 
0412     if (--info->timer->refcnt == 0) {
0413         pr_debug("deleting timer %s\n", info->label);
0414 
0415         list_del(&info->timer->entry);
0416         del_timer_sync(&info->timer->timer);
0417         cancel_work_sync(&info->timer->work);
0418         sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
0419         kfree(info->timer->attr.attr.name);
0420         kfree(info->timer);
0421     } else {
0422         pr_debug("decreased refcnt of timer %s to %u\n",
0423              info->label, info->timer->refcnt);
0424     }
0425 
0426     mutex_unlock(&list_mutex);
0427 }
0428 
0429 static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param *par)
0430 {
0431     const struct idletimer_tg_info_v1 *info = par->targinfo;
0432 
0433     pr_debug("destroy targinfo %s\n", info->label);
0434 
0435     mutex_lock(&list_mutex);
0436 
0437     if (--info->timer->refcnt == 0) {
0438         pr_debug("deleting timer %s\n", info->label);
0439 
0440         list_del(&info->timer->entry);
0441         if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
0442             alarm_cancel(&info->timer->alarm);
0443         } else {
0444             del_timer_sync(&info->timer->timer);
0445         }
0446         cancel_work_sync(&info->timer->work);
0447         sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
0448         kfree(info->timer->attr.attr.name);
0449         kfree(info->timer);
0450     } else {
0451         pr_debug("decreased refcnt of timer %s to %u\n",
0452              info->label, info->timer->refcnt);
0453     }
0454 
0455     mutex_unlock(&list_mutex);
0456 }
0457 
0458 
0459 static struct xt_target idletimer_tg[] __read_mostly = {
0460     {
0461     .name       = "IDLETIMER",
0462     .family     = NFPROTO_UNSPEC,
0463     .target     = idletimer_tg_target,
0464     .targetsize     = sizeof(struct idletimer_tg_info),
0465     .usersize   = offsetof(struct idletimer_tg_info, timer),
0466     .checkentry = idletimer_tg_checkentry,
0467     .destroy        = idletimer_tg_destroy,
0468     .me     = THIS_MODULE,
0469     },
0470     {
0471     .name       = "IDLETIMER",
0472     .family     = NFPROTO_UNSPEC,
0473     .revision   = 1,
0474     .target     = idletimer_tg_target_v1,
0475     .targetsize     = sizeof(struct idletimer_tg_info_v1),
0476     .usersize   = offsetof(struct idletimer_tg_info_v1, timer),
0477     .checkentry = idletimer_tg_checkentry_v1,
0478     .destroy        = idletimer_tg_destroy_v1,
0479     .me     = THIS_MODULE,
0480     },
0481 
0482 
0483 };
0484 
0485 static struct class *idletimer_tg_class;
0486 
0487 static struct device *idletimer_tg_device;
0488 
0489 static int __init idletimer_tg_init(void)
0490 {
0491     int err;
0492 
0493     idletimer_tg_class = class_create(THIS_MODULE, "xt_idletimer");
0494     err = PTR_ERR(idletimer_tg_class);
0495     if (IS_ERR(idletimer_tg_class)) {
0496         pr_debug("couldn't register device class\n");
0497         goto out;
0498     }
0499 
0500     idletimer_tg_device = device_create(idletimer_tg_class, NULL,
0501                         MKDEV(0, 0), NULL, "timers");
0502     err = PTR_ERR(idletimer_tg_device);
0503     if (IS_ERR(idletimer_tg_device)) {
0504         pr_debug("couldn't register system device\n");
0505         goto out_class;
0506     }
0507 
0508     idletimer_tg_kobj = &idletimer_tg_device->kobj;
0509 
0510     err = xt_register_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
0511 
0512     if (err < 0) {
0513         pr_debug("couldn't register xt target\n");
0514         goto out_dev;
0515     }
0516 
0517     return 0;
0518 out_dev:
0519     device_destroy(idletimer_tg_class, MKDEV(0, 0));
0520 out_class:
0521     class_destroy(idletimer_tg_class);
0522 out:
0523     return err;
0524 }
0525 
0526 static void __exit idletimer_tg_exit(void)
0527 {
0528     xt_unregister_targets(idletimer_tg, ARRAY_SIZE(idletimer_tg));
0529 
0530     device_destroy(idletimer_tg_class, MKDEV(0, 0));
0531     class_destroy(idletimer_tg_class);
0532 }
0533 
0534 module_init(idletimer_tg_init);
0535 module_exit(idletimer_tg_exit);
0536 
0537 MODULE_AUTHOR("Timo Teras <ext-timo.teras@nokia.com>");
0538 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
0539 MODULE_DESCRIPTION("Xtables: idle time monitor");
0540 MODULE_LICENSE("GPL v2");
0541 MODULE_ALIAS("ipt_IDLETIMER");
0542 MODULE_ALIAS("ip6t_IDLETIMER");