Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
0004  * Copyright © CC Computer Consultants GmbH, 2007 - 2008
0005  *
0006  * This is a replacement of the old ipt_recent module, which carried the
0007  * following copyright notice:
0008  *
0009  * Author: Stephen Frost <sfrost@snowman.net>
0010  * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
0011  */
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 #include <linux/init.h>
0014 #include <linux/ip.h>
0015 #include <linux/ipv6.h>
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/proc_fs.h>
0019 #include <linux/seq_file.h>
0020 #include <linux/string.h>
0021 #include <linux/ctype.h>
0022 #include <linux/list.h>
0023 #include <linux/random.h>
0024 #include <linux/jhash.h>
0025 #include <linux/bitops.h>
0026 #include <linux/skbuff.h>
0027 #include <linux/inet.h>
0028 #include <linux/slab.h>
0029 #include <linux/vmalloc.h>
0030 #include <net/net_namespace.h>
0031 #include <net/netns/generic.h>
0032 
0033 #include <linux/netfilter/x_tables.h>
0034 #include <linux/netfilter/xt_recent.h>
0035 
0036 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
0037 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
0038 MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching");
0039 MODULE_LICENSE("GPL");
0040 MODULE_ALIAS("ipt_recent");
0041 MODULE_ALIAS("ip6t_recent");
0042 
0043 static unsigned int ip_list_tot __read_mostly = 100;
0044 static unsigned int ip_list_hash_size __read_mostly;
0045 static unsigned int ip_list_perms __read_mostly = 0644;
0046 static unsigned int ip_list_uid __read_mostly;
0047 static unsigned int ip_list_gid __read_mostly;
0048 module_param(ip_list_tot, uint, 0400);
0049 module_param(ip_list_hash_size, uint, 0400);
0050 module_param(ip_list_perms, uint, 0400);
0051 module_param(ip_list_uid, uint, 0644);
0052 module_param(ip_list_gid, uint, 0644);
0053 MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
0054 MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
0055 MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
0056 MODULE_PARM_DESC(ip_list_uid, "default owner of /proc/net/xt_recent/* files");
0057 MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* files");
0058 
0059 /* retained for backwards compatibility */
0060 static unsigned int ip_pkt_list_tot __read_mostly;
0061 module_param(ip_pkt_list_tot, uint, 0400);
0062 MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
0063 
0064 #define XT_RECENT_MAX_NSTAMPS   256
0065 
0066 struct recent_entry {
0067     struct list_head    list;
0068     struct list_head    lru_list;
0069     union nf_inet_addr  addr;
0070     u_int16_t       family;
0071     u_int8_t        ttl;
0072     u_int8_t        index;
0073     u_int16_t       nstamps;
0074     unsigned long       stamps[];
0075 };
0076 
0077 struct recent_table {
0078     struct list_head    list;
0079     char            name[XT_RECENT_NAME_LEN];
0080     union nf_inet_addr  mask;
0081     unsigned int        refcnt;
0082     unsigned int        entries;
0083     u8          nstamps_max_mask;
0084     struct list_head    lru_list;
0085     struct list_head    iphash[];
0086 };
0087 
0088 struct recent_net {
0089     struct list_head    tables;
0090 #ifdef CONFIG_PROC_FS
0091     struct proc_dir_entry   *xt_recent;
0092 #endif
0093 };
0094 
0095 static unsigned int recent_net_id __read_mostly;
0096 
0097 static inline struct recent_net *recent_pernet(struct net *net)
0098 {
0099     return net_generic(net, recent_net_id);
0100 }
0101 
0102 static DEFINE_SPINLOCK(recent_lock);
0103 static DEFINE_MUTEX(recent_mutex);
0104 
0105 #ifdef CONFIG_PROC_FS
0106 static const struct proc_ops recent_mt_proc_ops;
0107 #endif
0108 
0109 static u_int32_t hash_rnd __read_mostly;
0110 
0111 static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
0112 {
0113     return jhash_1word((__force u32)addr->ip, hash_rnd) &
0114            (ip_list_hash_size - 1);
0115 }
0116 
0117 static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
0118 {
0119     return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
0120            (ip_list_hash_size - 1);
0121 }
0122 
0123 static struct recent_entry *
0124 recent_entry_lookup(const struct recent_table *table,
0125             const union nf_inet_addr *addrp, u_int16_t family,
0126             u_int8_t ttl)
0127 {
0128     struct recent_entry *e;
0129     unsigned int h;
0130 
0131     if (family == NFPROTO_IPV4)
0132         h = recent_entry_hash4(addrp);
0133     else
0134         h = recent_entry_hash6(addrp);
0135 
0136     list_for_each_entry(e, &table->iphash[h], list)
0137         if (e->family == family &&
0138             memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
0139             (ttl == e->ttl || ttl == 0 || e->ttl == 0))
0140             return e;
0141     return NULL;
0142 }
0143 
0144 static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
0145 {
0146     list_del(&e->list);
0147     list_del(&e->lru_list);
0148     kfree(e);
0149     t->entries--;
0150 }
0151 
0152 /*
0153  * Drop entries with timestamps older then 'time'.
0154  */
0155 static void recent_entry_reap(struct recent_table *t, unsigned long time,
0156                   struct recent_entry *working, bool update)
0157 {
0158     struct recent_entry *e;
0159 
0160     /*
0161      * The head of the LRU list is always the oldest entry.
0162      */
0163     e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
0164 
0165     /*
0166      * Do not reap the entry which are going to be updated.
0167      */
0168     if (e == working && update)
0169         return;
0170 
0171     /*
0172      * The last time stamp is the most recent.
0173      */
0174     if (time_after(time, e->stamps[e->index-1]))
0175         recent_entry_remove(t, e);
0176 }
0177 
0178 static struct recent_entry *
0179 recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
0180           u_int16_t family, u_int8_t ttl)
0181 {
0182     struct recent_entry *e;
0183     unsigned int nstamps_max = t->nstamps_max_mask;
0184 
0185     if (t->entries >= ip_list_tot) {
0186         e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
0187         recent_entry_remove(t, e);
0188     }
0189 
0190     nstamps_max += 1;
0191     e = kmalloc(struct_size(e, stamps, nstamps_max), GFP_ATOMIC);
0192     if (e == NULL)
0193         return NULL;
0194     memcpy(&e->addr, addr, sizeof(e->addr));
0195     e->ttl       = ttl;
0196     e->stamps[0] = jiffies;
0197     e->nstamps   = 1;
0198     e->index     = 1;
0199     e->family    = family;
0200     if (family == NFPROTO_IPV4)
0201         list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
0202     else
0203         list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
0204     list_add_tail(&e->lru_list, &t->lru_list);
0205     t->entries++;
0206     return e;
0207 }
0208 
0209 static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
0210 {
0211     e->index &= t->nstamps_max_mask;
0212     e->stamps[e->index++] = jiffies;
0213     if (e->index > e->nstamps)
0214         e->nstamps = e->index;
0215     list_move_tail(&e->lru_list, &t->lru_list);
0216 }
0217 
0218 static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
0219                         const char *name)
0220 {
0221     struct recent_table *t;
0222 
0223     list_for_each_entry(t, &recent_net->tables, list)
0224         if (!strcmp(t->name, name))
0225             return t;
0226     return NULL;
0227 }
0228 
0229 static void recent_table_flush(struct recent_table *t)
0230 {
0231     struct recent_entry *e, *next;
0232     unsigned int i;
0233 
0234     for (i = 0; i < ip_list_hash_size; i++)
0235         list_for_each_entry_safe(e, next, &t->iphash[i], list)
0236             recent_entry_remove(t, e);
0237 }
0238 
0239 static bool
0240 recent_mt(const struct sk_buff *skb, struct xt_action_param *par)
0241 {
0242     struct net *net = xt_net(par);
0243     struct recent_net *recent_net = recent_pernet(net);
0244     const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
0245     struct recent_table *t;
0246     struct recent_entry *e;
0247     union nf_inet_addr addr = {}, addr_mask;
0248     u_int8_t ttl;
0249     bool ret = info->invert;
0250 
0251     if (xt_family(par) == NFPROTO_IPV4) {
0252         const struct iphdr *iph = ip_hdr(skb);
0253 
0254         if (info->side == XT_RECENT_DEST)
0255             addr.ip = iph->daddr;
0256         else
0257             addr.ip = iph->saddr;
0258 
0259         ttl = iph->ttl;
0260     } else {
0261         const struct ipv6hdr *iph = ipv6_hdr(skb);
0262 
0263         if (info->side == XT_RECENT_DEST)
0264             memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
0265         else
0266             memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
0267 
0268         ttl = iph->hop_limit;
0269     }
0270 
0271     /* use TTL as seen before forwarding */
0272     if (xt_out(par) != NULL &&
0273         (!skb->sk || !net_eq(net, sock_net(skb->sk))))
0274         ttl++;
0275 
0276     spin_lock_bh(&recent_lock);
0277     t = recent_table_lookup(recent_net, info->name);
0278 
0279     nf_inet_addr_mask(&addr, &addr_mask, &t->mask);
0280 
0281     e = recent_entry_lookup(t, &addr_mask, xt_family(par),
0282                 (info->check_set & XT_RECENT_TTL) ? ttl : 0);
0283     if (e == NULL) {
0284         if (!(info->check_set & XT_RECENT_SET))
0285             goto out;
0286         e = recent_entry_init(t, &addr_mask, xt_family(par), ttl);
0287         if (e == NULL)
0288             par->hotdrop = true;
0289         ret = !ret;
0290         goto out;
0291     }
0292 
0293     if (info->check_set & XT_RECENT_SET)
0294         ret = !ret;
0295     else if (info->check_set & XT_RECENT_REMOVE) {
0296         recent_entry_remove(t, e);
0297         ret = !ret;
0298     } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
0299         unsigned long time = jiffies - info->seconds * HZ;
0300         unsigned int i, hits = 0;
0301 
0302         for (i = 0; i < e->nstamps; i++) {
0303             if (info->seconds && time_after(time, e->stamps[i]))
0304                 continue;
0305             if (!info->hit_count || ++hits >= info->hit_count) {
0306                 ret = !ret;
0307                 break;
0308             }
0309         }
0310 
0311         /* info->seconds must be non-zero */
0312         if (info->check_set & XT_RECENT_REAP)
0313             recent_entry_reap(t, time, e,
0314                 info->check_set & XT_RECENT_UPDATE && ret);
0315     }
0316 
0317     if (info->check_set & XT_RECENT_SET ||
0318         (info->check_set & XT_RECENT_UPDATE && ret)) {
0319         recent_entry_update(t, e);
0320         e->ttl = ttl;
0321     }
0322 out:
0323     spin_unlock_bh(&recent_lock);
0324     return ret;
0325 }
0326 
0327 static void recent_table_free(void *addr)
0328 {
0329     kvfree(addr);
0330 }
0331 
0332 static int recent_mt_check(const struct xt_mtchk_param *par,
0333                const struct xt_recent_mtinfo_v1 *info)
0334 {
0335     struct recent_net *recent_net = recent_pernet(par->net);
0336     struct recent_table *t;
0337 #ifdef CONFIG_PROC_FS
0338     struct proc_dir_entry *pde;
0339     kuid_t uid;
0340     kgid_t gid;
0341 #endif
0342     unsigned int nstamp_mask;
0343     unsigned int i;
0344     int ret = -EINVAL;
0345 
0346     net_get_random_once(&hash_rnd, sizeof(hash_rnd));
0347 
0348     if (info->check_set & ~XT_RECENT_VALID_FLAGS) {
0349         pr_info_ratelimited("Unsupported userspace flags (%08x)\n",
0350                     info->check_set);
0351         return -EINVAL;
0352     }
0353     if (hweight8(info->check_set &
0354              (XT_RECENT_SET | XT_RECENT_REMOVE |
0355               XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
0356         return -EINVAL;
0357     if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
0358         (info->seconds || info->hit_count ||
0359         (info->check_set & XT_RECENT_MODIFIERS)))
0360         return -EINVAL;
0361     if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
0362         return -EINVAL;
0363     if (info->hit_count >= XT_RECENT_MAX_NSTAMPS) {
0364         pr_info_ratelimited("hitcount (%u) is larger than allowed maximum (%u)\n",
0365                     info->hit_count, XT_RECENT_MAX_NSTAMPS - 1);
0366         return -EINVAL;
0367     }
0368     ret = xt_check_proc_name(info->name, sizeof(info->name));
0369     if (ret)
0370         return ret;
0371 
0372     if (ip_pkt_list_tot && info->hit_count < ip_pkt_list_tot)
0373         nstamp_mask = roundup_pow_of_two(ip_pkt_list_tot) - 1;
0374     else if (info->hit_count)
0375         nstamp_mask = roundup_pow_of_two(info->hit_count) - 1;
0376     else
0377         nstamp_mask = 32 - 1;
0378 
0379     mutex_lock(&recent_mutex);
0380     t = recent_table_lookup(recent_net, info->name);
0381     if (t != NULL) {
0382         if (nstamp_mask > t->nstamps_max_mask) {
0383             spin_lock_bh(&recent_lock);
0384             recent_table_flush(t);
0385             t->nstamps_max_mask = nstamp_mask;
0386             spin_unlock_bh(&recent_lock);
0387         }
0388 
0389         t->refcnt++;
0390         ret = 0;
0391         goto out;
0392     }
0393 
0394     t = kvzalloc(struct_size(t, iphash, ip_list_hash_size), GFP_KERNEL);
0395     if (t == NULL) {
0396         ret = -ENOMEM;
0397         goto out;
0398     }
0399     t->refcnt = 1;
0400     t->nstamps_max_mask = nstamp_mask;
0401 
0402     memcpy(&t->mask, &info->mask, sizeof(t->mask));
0403     strcpy(t->name, info->name);
0404     INIT_LIST_HEAD(&t->lru_list);
0405     for (i = 0; i < ip_list_hash_size; i++)
0406         INIT_LIST_HEAD(&t->iphash[i]);
0407 #ifdef CONFIG_PROC_FS
0408     uid = make_kuid(&init_user_ns, ip_list_uid);
0409     gid = make_kgid(&init_user_ns, ip_list_gid);
0410     if (!uid_valid(uid) || !gid_valid(gid)) {
0411         recent_table_free(t);
0412         ret = -EINVAL;
0413         goto out;
0414     }
0415     pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
0416                    &recent_mt_proc_ops, t);
0417     if (pde == NULL) {
0418         recent_table_free(t);
0419         ret = -ENOMEM;
0420         goto out;
0421     }
0422     proc_set_user(pde, uid, gid);
0423 #endif
0424     spin_lock_bh(&recent_lock);
0425     list_add_tail(&t->list, &recent_net->tables);
0426     spin_unlock_bh(&recent_lock);
0427     ret = 0;
0428 out:
0429     mutex_unlock(&recent_mutex);
0430     return ret;
0431 }
0432 
0433 static int recent_mt_check_v0(const struct xt_mtchk_param *par)
0434 {
0435     const struct xt_recent_mtinfo_v0 *info_v0 = par->matchinfo;
0436     struct xt_recent_mtinfo_v1 info_v1;
0437 
0438     /* Copy revision 0 structure to revision 1 */
0439     memcpy(&info_v1, info_v0, sizeof(struct xt_recent_mtinfo));
0440     /* Set default mask to ensure backward compatible behaviour */
0441     memset(info_v1.mask.all, 0xFF, sizeof(info_v1.mask.all));
0442 
0443     return recent_mt_check(par, &info_v1);
0444 }
0445 
0446 static int recent_mt_check_v1(const struct xt_mtchk_param *par)
0447 {
0448     return recent_mt_check(par, par->matchinfo);
0449 }
0450 
0451 static void recent_mt_destroy(const struct xt_mtdtor_param *par)
0452 {
0453     struct recent_net *recent_net = recent_pernet(par->net);
0454     const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
0455     struct recent_table *t;
0456 
0457     mutex_lock(&recent_mutex);
0458     t = recent_table_lookup(recent_net, info->name);
0459     if (--t->refcnt == 0) {
0460         spin_lock_bh(&recent_lock);
0461         list_del(&t->list);
0462         spin_unlock_bh(&recent_lock);
0463 #ifdef CONFIG_PROC_FS
0464         if (recent_net->xt_recent != NULL)
0465             remove_proc_entry(t->name, recent_net->xt_recent);
0466 #endif
0467         recent_table_flush(t);
0468         recent_table_free(t);
0469     }
0470     mutex_unlock(&recent_mutex);
0471 }
0472 
0473 #ifdef CONFIG_PROC_FS
0474 struct recent_iter_state {
0475     const struct recent_table *table;
0476     unsigned int        bucket;
0477 };
0478 
0479 static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
0480     __acquires(recent_lock)
0481 {
0482     struct recent_iter_state *st = seq->private;
0483     const struct recent_table *t = st->table;
0484     struct recent_entry *e;
0485     loff_t p = *pos;
0486 
0487     spin_lock_bh(&recent_lock);
0488 
0489     for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
0490         list_for_each_entry(e, &t->iphash[st->bucket], list)
0491             if (p-- == 0)
0492                 return e;
0493     return NULL;
0494 }
0495 
0496 static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
0497 {
0498     struct recent_iter_state *st = seq->private;
0499     const struct recent_table *t = st->table;
0500     const struct recent_entry *e = v;
0501     const struct list_head *head = e->list.next;
0502 
0503     (*pos)++;
0504     while (head == &t->iphash[st->bucket]) {
0505         if (++st->bucket >= ip_list_hash_size)
0506             return NULL;
0507         head = t->iphash[st->bucket].next;
0508     }
0509     return list_entry(head, struct recent_entry, list);
0510 }
0511 
0512 static void recent_seq_stop(struct seq_file *s, void *v)
0513     __releases(recent_lock)
0514 {
0515     spin_unlock_bh(&recent_lock);
0516 }
0517 
0518 static int recent_seq_show(struct seq_file *seq, void *v)
0519 {
0520     const struct recent_entry *e = v;
0521     struct recent_iter_state *st = seq->private;
0522     const struct recent_table *t = st->table;
0523     unsigned int i;
0524 
0525     i = (e->index - 1) & t->nstamps_max_mask;
0526 
0527     if (e->family == NFPROTO_IPV4)
0528         seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
0529                &e->addr.ip, e->ttl, e->stamps[i], e->index);
0530     else
0531         seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
0532                &e->addr.in6, e->ttl, e->stamps[i], e->index);
0533     for (i = 0; i < e->nstamps; i++)
0534         seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
0535     seq_putc(seq, '\n');
0536     return 0;
0537 }
0538 
0539 static const struct seq_operations recent_seq_ops = {
0540     .start      = recent_seq_start,
0541     .next       = recent_seq_next,
0542     .stop       = recent_seq_stop,
0543     .show       = recent_seq_show,
0544 };
0545 
0546 static int recent_seq_open(struct inode *inode, struct file *file)
0547 {
0548     struct recent_iter_state *st;
0549 
0550     st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
0551     if (st == NULL)
0552         return -ENOMEM;
0553 
0554     st->table    = pde_data(inode);
0555     return 0;
0556 }
0557 
0558 static ssize_t
0559 recent_mt_proc_write(struct file *file, const char __user *input,
0560              size_t size, loff_t *loff)
0561 {
0562     struct recent_table *t = pde_data(file_inode(file));
0563     struct recent_entry *e;
0564     char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
0565     const char *c = buf;
0566     union nf_inet_addr addr = {};
0567     u_int16_t family;
0568     bool add, succ;
0569 
0570     if (size == 0)
0571         return 0;
0572     if (size > sizeof(buf))
0573         size = sizeof(buf);
0574     if (copy_from_user(buf, input, size) != 0)
0575         return -EFAULT;
0576 
0577     /* Strict protocol! */
0578     if (*loff != 0)
0579         return -ESPIPE;
0580     switch (*c) {
0581     case '/': /* flush table */
0582         spin_lock_bh(&recent_lock);
0583         recent_table_flush(t);
0584         spin_unlock_bh(&recent_lock);
0585         return size;
0586     case '-': /* remove address */
0587         add = false;
0588         break;
0589     case '+': /* add address */
0590         add = true;
0591         break;
0592     default:
0593         pr_info_ratelimited("Need \"+ip\", \"-ip\" or \"/\"\n");
0594         return -EINVAL;
0595     }
0596 
0597     ++c;
0598     --size;
0599     if (strnchr(c, size, ':') != NULL) {
0600         family = NFPROTO_IPV6;
0601         succ   = in6_pton(c, size, (void *)&addr, '\n', NULL);
0602     } else {
0603         family = NFPROTO_IPV4;
0604         succ   = in4_pton(c, size, (void *)&addr, '\n', NULL);
0605     }
0606 
0607     if (!succ)
0608         return -EINVAL;
0609 
0610     spin_lock_bh(&recent_lock);
0611     e = recent_entry_lookup(t, &addr, family, 0);
0612     if (e == NULL) {
0613         if (add)
0614             recent_entry_init(t, &addr, family, 0);
0615     } else {
0616         if (add)
0617             recent_entry_update(t, e);
0618         else
0619             recent_entry_remove(t, e);
0620     }
0621     spin_unlock_bh(&recent_lock);
0622     /* Note we removed one above */
0623     *loff += size + 1;
0624     return size + 1;
0625 }
0626 
0627 static const struct proc_ops recent_mt_proc_ops = {
0628     .proc_open  = recent_seq_open,
0629     .proc_read  = seq_read,
0630     .proc_write = recent_mt_proc_write,
0631     .proc_release   = seq_release_private,
0632     .proc_lseek = seq_lseek,
0633 };
0634 
0635 static int __net_init recent_proc_net_init(struct net *net)
0636 {
0637     struct recent_net *recent_net = recent_pernet(net);
0638 
0639     recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
0640     if (!recent_net->xt_recent)
0641         return -ENOMEM;
0642     return 0;
0643 }
0644 
0645 static void __net_exit recent_proc_net_exit(struct net *net)
0646 {
0647     struct recent_net *recent_net = recent_pernet(net);
0648     struct recent_table *t;
0649 
0650     /* recent_net_exit() is called before recent_mt_destroy(). Make sure
0651      * that the parent xt_recent proc entry is empty before trying to
0652      * remove it.
0653      */
0654     spin_lock_bh(&recent_lock);
0655     list_for_each_entry(t, &recent_net->tables, list)
0656             remove_proc_entry(t->name, recent_net->xt_recent);
0657 
0658     recent_net->xt_recent = NULL;
0659     spin_unlock_bh(&recent_lock);
0660 
0661     remove_proc_entry("xt_recent", net->proc_net);
0662 }
0663 #else
0664 static inline int recent_proc_net_init(struct net *net)
0665 {
0666     return 0;
0667 }
0668 
0669 static inline void recent_proc_net_exit(struct net *net)
0670 {
0671 }
0672 #endif /* CONFIG_PROC_FS */
0673 
0674 static int __net_init recent_net_init(struct net *net)
0675 {
0676     struct recent_net *recent_net = recent_pernet(net);
0677 
0678     INIT_LIST_HEAD(&recent_net->tables);
0679     return recent_proc_net_init(net);
0680 }
0681 
0682 static void __net_exit recent_net_exit(struct net *net)
0683 {
0684     recent_proc_net_exit(net);
0685 }
0686 
0687 static struct pernet_operations recent_net_ops = {
0688     .init   = recent_net_init,
0689     .exit   = recent_net_exit,
0690     .id = &recent_net_id,
0691     .size   = sizeof(struct recent_net),
0692 };
0693 
0694 static struct xt_match recent_mt_reg[] __read_mostly = {
0695     {
0696         .name       = "recent",
0697         .revision   = 0,
0698         .family     = NFPROTO_IPV4,
0699         .match      = recent_mt,
0700         .matchsize  = sizeof(struct xt_recent_mtinfo),
0701         .checkentry = recent_mt_check_v0,
0702         .destroy    = recent_mt_destroy,
0703         .me         = THIS_MODULE,
0704     },
0705     {
0706         .name       = "recent",
0707         .revision   = 0,
0708         .family     = NFPROTO_IPV6,
0709         .match      = recent_mt,
0710         .matchsize  = sizeof(struct xt_recent_mtinfo),
0711         .checkentry = recent_mt_check_v0,
0712         .destroy    = recent_mt_destroy,
0713         .me         = THIS_MODULE,
0714     },
0715     {
0716         .name       = "recent",
0717         .revision   = 1,
0718         .family     = NFPROTO_IPV4,
0719         .match      = recent_mt,
0720         .matchsize  = sizeof(struct xt_recent_mtinfo_v1),
0721         .checkentry = recent_mt_check_v1,
0722         .destroy    = recent_mt_destroy,
0723         .me         = THIS_MODULE,
0724     },
0725     {
0726         .name       = "recent",
0727         .revision   = 1,
0728         .family     = NFPROTO_IPV6,
0729         .match      = recent_mt,
0730         .matchsize  = sizeof(struct xt_recent_mtinfo_v1),
0731         .checkentry = recent_mt_check_v1,
0732         .destroy    = recent_mt_destroy,
0733         .me         = THIS_MODULE,
0734     }
0735 };
0736 
0737 static int __init recent_mt_init(void)
0738 {
0739     int err;
0740 
0741     BUILD_BUG_ON_NOT_POWER_OF_2(XT_RECENT_MAX_NSTAMPS);
0742 
0743     if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS)
0744         return -EINVAL;
0745     ip_list_hash_size = 1 << fls(ip_list_tot);
0746 
0747     err = register_pernet_subsys(&recent_net_ops);
0748     if (err)
0749         return err;
0750     err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
0751     if (err)
0752         unregister_pernet_subsys(&recent_net_ops);
0753     return err;
0754 }
0755 
0756 static void __exit recent_mt_exit(void)
0757 {
0758     xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
0759     unregister_pernet_subsys(&recent_net_ops);
0760 }
0761 
0762 module_init(recent_mt_init);
0763 module_exit(recent_mt_exit);