JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xt_quota2 - enhanced xt_quota that can count upwards and in packets |
| 3 | * as a minimal accounting match. |
| 4 | * by Jan Engelhardt <jengelh@medozas.de>, 2008 |
| 5 | * |
| 6 | * Originally based on xt_quota.c: |
| 7 | * netfilter module to enforce network quotas |
| 8 | * Sam Johnston <samj@samj.net> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License; either |
| 12 | * version 2 of the License, as published by the Free Software Foundation. |
| 13 | */ |
| 14 | #include <linux/list.h> |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 16 | #include <linux/proc_fs.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/spinlock.h> |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 19 | #include <linux/workqueue.h> |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 20 | #include <asm/atomic.h> |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 21 | #include <net/netlink.h> |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 22 | |
| 23 | #include <linux/netfilter/x_tables.h> |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 24 | #include <linux/netfilter/xt_quota2.h> |
Liping Zhang | dd11131 | 2016-06-22 16:49:48 +0800 | [diff] [blame] | 25 | |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 26 | #ifdef CONFIG_NETFILTER_XT_MATCH_QUOTA2_LOG |
Liping Zhang | dd11131 | 2016-06-22 16:49:48 +0800 | [diff] [blame] | 27 | /* For compatibility, these definitions are copied from the |
| 28 | * deprecated header file <linux/netfilter_ipv4/ipt_ULOG.h> */ |
| 29 | #define ULOG_MAC_LEN 80 |
| 30 | #define ULOG_PREFIX_LEN 32 |
| 31 | |
| 32 | /* Format of the ULOG packets passed through netlink */ |
| 33 | typedef struct ulog_packet_msg { |
| 34 | unsigned long mark; |
| 35 | long timestamp_sec; |
| 36 | long timestamp_usec; |
| 37 | unsigned int hook; |
| 38 | char indev_name[IFNAMSIZ]; |
| 39 | char outdev_name[IFNAMSIZ]; |
| 40 | size_t data_len; |
| 41 | char prefix[ULOG_PREFIX_LEN]; |
| 42 | unsigned char mac_len; |
| 43 | unsigned char mac[ULOG_MAC_LEN]; |
| 44 | unsigned char payload[0]; |
| 45 | } ulog_packet_msg_t; |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 46 | #endif |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 47 | #define QUOTA2_SYSFS_WORK_MAX_SIZE 64 |
| 48 | #define QUOTA2_SYSFS_NUM_ENVP 3 |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @lock: lock to protect quota writers from each other |
| 52 | */ |
| 53 | struct xt_quota_counter { |
| 54 | u_int64_t quota; |
| 55 | spinlock_t lock; |
| 56 | struct list_head list; |
| 57 | atomic_t ref; |
| 58 | char name[sizeof(((struct xt_quota_mtinfo2 *)NULL)->name)]; |
| 59 | struct proc_dir_entry *procfs_entry; |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 60 | char last_iface[QUOTA2_SYSFS_WORK_MAX_SIZE]; |
| 61 | char last_prefix[QUOTA2_SYSFS_WORK_MAX_SIZE]; |
| 62 | struct work_struct work; |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 65 | #define to_quota_counter(x) container_of(x, struct xt_quota_counter, work) |
| 66 | |
| 67 | static struct class *quota_class; |
| 68 | static struct device *quota_device; |
| 69 | static struct kobject *quota_kobj; |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 70 | |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 71 | static LIST_HEAD(counter_list); |
| 72 | static DEFINE_SPINLOCK(counter_list_lock); |
| 73 | |
| 74 | static struct proc_dir_entry *proc_xt_quota; |
| 75 | static unsigned int quota_list_perms = S_IRUGO | S_IWUSR; |
John Stultz | bc1bb86 | 2014-03-28 12:19:27 -0700 | [diff] [blame] | 76 | static kuid_t quota_list_uid = KUIDT_INIT(0); |
| 77 | static kgid_t quota_list_gid = KGIDT_INIT(0); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 78 | module_param_named(perms, quota_list_perms, uint, S_IRUGO | S_IWUSR); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 79 | |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 80 | static void quota2_work(struct work_struct *work) |
| 81 | { |
| 82 | char alert_msg[QUOTA2_SYSFS_WORK_MAX_SIZE]; |
| 83 | char iface_name[QUOTA2_SYSFS_WORK_MAX_SIZE]; |
| 84 | char *envp[QUOTA2_SYSFS_NUM_ENVP] = {alert_msg, iface_name, NULL}; |
| 85 | struct xt_quota_counter *counter = to_quota_counter(work); |
| 86 | |
| 87 | snprintf(alert_msg, sizeof(alert_msg), "ALERT_NAME=%s", counter->name); |
| 88 | snprintf(iface_name, sizeof(iface_name), "INTERFACE=%s", |
| 89 | counter->last_iface); |
| 90 | |
| 91 | kobject_uevent_env(quota_kobj, KOBJ_CHANGE, envp); |
| 92 | } |
| 93 | |
| 94 | static void quota2_log(const struct net_device *in, |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 95 | const struct net_device *out, |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 96 | struct xt_quota_counter *q, |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 97 | const char *prefix) |
| 98 | { |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 99 | if (!prefix) |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 100 | return; |
| 101 | |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 102 | strlcpy(q->last_prefix, prefix, QUOTA2_SYSFS_WORK_MAX_SIZE); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 103 | |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 104 | if (in) |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 105 | strlcpy(q->last_iface, in->name, QUOTA2_SYSFS_WORK_MAX_SIZE); |
| 106 | else if (out) |
| 107 | strlcpy(q->last_iface, out->name, QUOTA2_SYSFS_WORK_MAX_SIZE); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 108 | else |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 109 | strlcpy(q->last_iface, "UNKNOWN", QUOTA2_SYSFS_WORK_MAX_SIZE); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 110 | |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 111 | schedule_work(&q->work); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 112 | } |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 113 | |
Greg Hackmann | 81667c5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 114 | static ssize_t quota_proc_read(struct file *file, char __user *buf, |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 115 | size_t size, loff_t *ppos) |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 116 | { |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 117 | struct xt_quota_counter *e = PDE_DATA(file_inode(file)); |
| 118 | char tmp[24]; |
| 119 | size_t tmp_size; |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 120 | |
| 121 | spin_lock_bh(&e->lock); |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 122 | tmp_size = scnprintf(tmp, sizeof(tmp), "%llu\n", e->quota); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 123 | spin_unlock_bh(&e->lock); |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 124 | return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Greg Hackmann | 81667c5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 127 | static ssize_t quota_proc_write(struct file *file, const char __user *input, |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 128 | size_t size, loff_t *ppos) |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 129 | { |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 130 | struct xt_quota_counter *e = PDE_DATA(file_inode(file)); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 131 | char buf[sizeof("18446744073709551616")]; |
| 132 | |
| 133 | if (size > sizeof(buf)) |
| 134 | size = sizeof(buf); |
| 135 | if (copy_from_user(buf, input, size) != 0) |
| 136 | return -EFAULT; |
| 137 | buf[sizeof(buf)-1] = '\0'; |
| 138 | |
| 139 | spin_lock_bh(&e->lock); |
| 140 | e->quota = simple_strtoull(buf, NULL, 0); |
| 141 | spin_unlock_bh(&e->lock); |
| 142 | return size; |
| 143 | } |
| 144 | |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 145 | static const struct file_operations q2_counter_fops = { |
| 146 | .read = quota_proc_read, |
| 147 | .write = quota_proc_write, |
| 148 | .llseek = default_llseek, |
| 149 | }; |
| 150 | |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 151 | static struct xt_quota_counter * |
| 152 | q2_new_counter(const struct xt_quota_mtinfo2 *q, bool anon) |
| 153 | { |
| 154 | struct xt_quota_counter *e; |
| 155 | unsigned int size; |
| 156 | |
| 157 | /* Do not need all the procfs things for anonymous counters. */ |
| 158 | size = anon ? offsetof(typeof(*e), list) : sizeof(*e); |
| 159 | e = kmalloc(size, GFP_KERNEL); |
| 160 | if (e == NULL) |
| 161 | return NULL; |
| 162 | |
| 163 | e->quota = q->quota; |
| 164 | spin_lock_init(&e->lock); |
| 165 | if (!anon) { |
| 166 | INIT_LIST_HEAD(&e->list); |
| 167 | atomic_set(&e->ref, 1); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 168 | strlcpy(e->name, q->name, sizeof(e->name)); |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 169 | strlcpy(e->last_prefix, "UNSET", sizeof(e->last_prefix)); |
| 170 | strlcpy(e->last_iface, "UNSET", sizeof(e->last_iface)); |
| 171 | INIT_WORK(&e->work, quota2_work); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 172 | } |
| 173 | return e; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * q2_get_counter - get ref to counter or create new |
| 178 | * @name: name of counter |
| 179 | */ |
| 180 | static struct xt_quota_counter * |
| 181 | q2_get_counter(const struct xt_quota_mtinfo2 *q) |
| 182 | { |
| 183 | struct proc_dir_entry *p; |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 184 | struct xt_quota_counter *e = NULL; |
| 185 | struct xt_quota_counter *new_e; |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 186 | |
| 187 | if (*q->name == '\0') |
| 188 | return q2_new_counter(q, true); |
| 189 | |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 190 | /* No need to hold a lock while getting a new counter */ |
| 191 | new_e = q2_new_counter(q, false); |
| 192 | if (new_e == NULL) |
| 193 | goto out; |
| 194 | |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 195 | spin_lock_bh(&counter_list_lock); |
| 196 | list_for_each_entry(e, &counter_list, list) |
| 197 | if (strcmp(e->name, q->name) == 0) { |
| 198 | atomic_inc(&e->ref); |
| 199 | spin_unlock_bh(&counter_list_lock); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 200 | kfree(new_e); |
| 201 | pr_debug("xt_quota2: old counter name=%s", e->name); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 202 | return e; |
| 203 | } |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 204 | e = new_e; |
| 205 | pr_debug("xt_quota2: new_counter name=%s", e->name); |
| 206 | list_add_tail(&e->list, &counter_list); |
| 207 | /* The entry having a refcount of 1 is not directly destructible. |
| 208 | * This func has not yet returned the new entry, thus iptables |
| 209 | * has not references for destroying this entry. |
| 210 | * For another rule to try to destroy it, it would 1st need for this |
| 211 | * func* to be re-invoked, acquire a new ref for the same named quota. |
| 212 | * Nobody will access the e->procfs_entry either. |
| 213 | * So release the lock. */ |
| 214 | spin_unlock_bh(&counter_list_lock); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 215 | |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 216 | /* create_proc_entry() is not spin_lock happy */ |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 217 | p = e->procfs_entry = proc_create_data(e->name, quota_list_perms, |
| 218 | proc_xt_quota, &q2_counter_fops, e); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 219 | |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 220 | if (IS_ERR_OR_NULL(p)) { |
| 221 | spin_lock_bh(&counter_list_lock); |
| 222 | list_del(&e->list); |
| 223 | spin_unlock_bh(&counter_list_lock); |
| 224 | goto out; |
| 225 | } |
Arve Hjønnevåg | f1075f7 | 2013-05-13 20:42:46 -0700 | [diff] [blame] | 226 | proc_set_user(p, quota_list_uid, quota_list_gid); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 227 | return e; |
| 228 | |
| 229 | out: |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 230 | kfree(e); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | static int quota_mt2_check(const struct xt_mtchk_param *par) |
| 235 | { |
| 236 | struct xt_quota_mtinfo2 *q = par->matchinfo; |
| 237 | |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 238 | pr_debug("xt_quota2: check() flags=0x%04x", q->flags); |
| 239 | |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 240 | if (q->flags & ~XT_QUOTA_MASK) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | q->name[sizeof(q->name)-1] = '\0'; |
| 244 | if (*q->name == '.' || strchr(q->name, '/') != NULL) { |
| 245 | printk(KERN_ERR "xt_quota.3: illegal name\n"); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | q->master = q2_get_counter(q); |
| 250 | if (q->master == NULL) { |
| 251 | printk(KERN_ERR "xt_quota.3: memory alloc failure\n"); |
| 252 | return -ENOMEM; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static void quota_mt2_destroy(const struct xt_mtdtor_param *par) |
| 259 | { |
| 260 | struct xt_quota_mtinfo2 *q = par->matchinfo; |
| 261 | struct xt_quota_counter *e = q->master; |
| 262 | |
| 263 | if (*q->name == '\0') { |
| 264 | kfree(e); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | spin_lock_bh(&counter_list_lock); |
| 269 | if (!atomic_dec_and_test(&e->ref)) { |
| 270 | spin_unlock_bh(&counter_list_lock); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | list_del(&e->list); |
| 275 | remove_proc_entry(e->name, proc_xt_quota); |
| 276 | spin_unlock_bh(&counter_list_lock); |
| 277 | kfree(e); |
| 278 | } |
| 279 | |
| 280 | static bool |
| 281 | quota_mt2(const struct sk_buff *skb, struct xt_action_param *par) |
| 282 | { |
| 283 | struct xt_quota_mtinfo2 *q = (void *)par->matchinfo; |
| 284 | struct xt_quota_counter *e = q->master; |
| 285 | bool ret = q->flags & XT_QUOTA_INVERT; |
| 286 | |
| 287 | spin_lock_bh(&e->lock); |
| 288 | if (q->flags & XT_QUOTA_GROW) { |
| 289 | /* |
| 290 | * While no_change is pointless in "grow" mode, we will |
| 291 | * implement it here simply to have a consistent behavior. |
| 292 | */ |
| 293 | if (!(q->flags & XT_QUOTA_NO_CHANGE)) { |
| 294 | e->quota += (q->flags & XT_QUOTA_PACKET) ? 1 : skb->len; |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 295 | } |
| 296 | ret = true; |
| 297 | } else { |
| 298 | if (e->quota >= skb->len) { |
| 299 | if (!(q->flags & XT_QUOTA_NO_CHANGE)) |
| 300 | e->quota -= (q->flags & XT_QUOTA_PACKET) ? 1 : skb->len; |
| 301 | ret = !ret; |
| 302 | } else { |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 303 | /* We are transitioning, log that fact. */ |
| 304 | if (e->quota) { |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 305 | quota2_log(par->in, par->out, e, q->name); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 306 | } |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 307 | /* we do not allow even small packets from now on */ |
| 308 | e->quota = 0; |
| 309 | } |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 310 | } |
| 311 | spin_unlock_bh(&e->lock); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static struct xt_match quota_mt2_reg[] __read_mostly = { |
| 316 | { |
| 317 | .name = "quota2", |
| 318 | .revision = 3, |
| 319 | .family = NFPROTO_IPV4, |
| 320 | .checkentry = quota_mt2_check, |
| 321 | .match = quota_mt2, |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 322 | .destroy = quota_mt2_destroy, |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 323 | .matchsize = sizeof(struct xt_quota_mtinfo2), |
| 324 | .me = THIS_MODULE, |
| 325 | }, |
| 326 | { |
| 327 | .name = "quota2", |
| 328 | .revision = 3, |
| 329 | .family = NFPROTO_IPV6, |
| 330 | .checkentry = quota_mt2_check, |
| 331 | .match = quota_mt2, |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 332 | .destroy = quota_mt2_destroy, |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 333 | .matchsize = sizeof(struct xt_quota_mtinfo2), |
| 334 | .me = THIS_MODULE, |
| 335 | }, |
| 336 | }; |
| 337 | |
| 338 | static int __init quota_mt2_init(void) |
| 339 | { |
| 340 | int ret; |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 341 | pr_debug("xt_quota2: init()"); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 342 | |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 343 | quota_class = class_create(THIS_MODULE, "xt_quota2"); |
| 344 | ret = PTR_ERR(quota_class); |
| 345 | if (IS_ERR(quota_class)) { |
| 346 | pr_err("xt_quota2: couldn't create class"); |
| 347 | class_destroy(quota_class); |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | quota_device = device_create(quota_class, NULL, MKDEV(0, 0), NULL, |
| 352 | "counters"); |
| 353 | ret = PTR_ERR(quota_device); |
| 354 | if (IS_ERR(quota_device)) { |
| 355 | pr_err("xt_quota2: couldn't create device"); |
| 356 | device_destroy(quota_class, MKDEV(0, 0)); |
| 357 | class_destroy(quota_class); |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | quota_kobj = "a_device->kobj; |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 362 | |
| 363 | proc_xt_quota = proc_mkdir("xt_quota", init_net.proc_net); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 364 | if (proc_xt_quota == NULL) |
| 365 | return -EACCES; |
| 366 | |
| 367 | ret = xt_register_matches(quota_mt2_reg, ARRAY_SIZE(quota_mt2_reg)); |
| 368 | if (ret < 0) |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 369 | remove_proc_entry("xt_quota", init_net.proc_net); |
| 370 | pr_debug("xt_quota2: init() %d", ret); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static void __exit quota_mt2_exit(void) |
| 375 | { |
| 376 | xt_unregister_matches(quota_mt2_reg, ARRAY_SIZE(quota_mt2_reg)); |
JP Abgrall | 3fd8238 | 2011-07-12 12:02:59 -0700 | [diff] [blame] | 377 | remove_proc_entry("xt_quota", init_net.proc_net); |
Tyler Wear | e8a34e5 | 2016-05-27 15:48:38 -0700 | [diff] [blame^] | 378 | device_destroy(quota_class, MKDEV(0, 0)); |
| 379 | class_destroy(quota_class); |
JP Abgrall | dc5ce6e | 2011-06-21 11:14:49 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | module_init(quota_mt2_init); |
| 383 | module_exit(quota_mt2_exit); |
| 384 | MODULE_DESCRIPTION("Xtables: countdown quota match; up counter"); |
| 385 | MODULE_AUTHOR("Sam Johnston <samj@samj.net>"); |
| 386 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
| 387 | MODULE_LICENSE("GPL"); |
| 388 | MODULE_ALIAS("ipt_quota2"); |
| 389 | MODULE_ALIAS("ip6t_quota2"); |