xt_recent.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright (c) 2006 Patrick McHardy <kaber@trash.net>
  3. * Copyright © CC Computer Consultants GmbH, 2007 - 2008
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This is a replacement of the old ipt_recent module, which carried the
  10. * following copyright notice:
  11. *
  12. * Author: Stephen Frost <sfrost@snowman.net>
  13. * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/init.h>
  17. #include <linux/ip.h>
  18. #include <linux/ipv6.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/string.h>
  24. #include <linux/ctype.h>
  25. #include <linux/list.h>
  26. #include <linux/random.h>
  27. #include <linux/jhash.h>
  28. #include <linux/bitops.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/inet.h>
  31. #include <linux/slab.h>
  32. #include <net/net_namespace.h>
  33. #include <net/netns/generic.h>
  34. #include <linux/netfilter/x_tables.h>
  35. #include <linux/netfilter/xt_recent.h>
  36. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  37. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  38. MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching");
  39. MODULE_LICENSE("GPL");
  40. MODULE_ALIAS("ipt_recent");
  41. MODULE_ALIAS("ip6t_recent");
  42. static unsigned int ip_list_tot = 100;
  43. static unsigned int ip_pkt_list_tot = 20;
  44. static unsigned int ip_list_hash_size = 0;
  45. static unsigned int ip_list_perms = 0644;
  46. static unsigned int ip_list_uid = 0;
  47. static unsigned int ip_list_gid = 0;
  48. module_param(ip_list_tot, uint, 0400);
  49. module_param(ip_pkt_list_tot, uint, 0400);
  50. module_param(ip_list_hash_size, uint, 0400);
  51. module_param(ip_list_perms, uint, 0400);
  52. module_param(ip_list_uid, uint, S_IRUGO | S_IWUSR);
  53. module_param(ip_list_gid, uint, S_IRUGO | S_IWUSR);
  54. MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list");
  55. MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)");
  56. MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs");
  57. MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files");
  58. MODULE_PARM_DESC(ip_list_uid, "default owner of /proc/net/xt_recent/* files");
  59. MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* files");
  60. struct recent_entry {
  61. struct list_head list;
  62. struct list_head lru_list;
  63. union nf_inet_addr addr;
  64. u_int16_t family;
  65. u_int8_t ttl;
  66. u_int8_t index;
  67. u_int16_t nstamps;
  68. unsigned long stamps[0];
  69. };
  70. struct recent_table {
  71. struct list_head list;
  72. char name[XT_RECENT_NAME_LEN];
  73. unsigned int refcnt;
  74. unsigned int entries;
  75. struct list_head lru_list;
  76. struct list_head iphash[0];
  77. };
  78. struct recent_net {
  79. struct list_head tables;
  80. #ifdef CONFIG_PROC_FS
  81. struct proc_dir_entry *xt_recent;
  82. #endif
  83. };
  84. static int recent_net_id;
  85. static inline struct recent_net *recent_pernet(struct net *net)
  86. {
  87. return net_generic(net, recent_net_id);
  88. }
  89. static DEFINE_SPINLOCK(recent_lock);
  90. static DEFINE_MUTEX(recent_mutex);
  91. #ifdef CONFIG_PROC_FS
  92. static const struct file_operations recent_old_fops, recent_mt_fops;
  93. #endif
  94. static u_int32_t hash_rnd __read_mostly;
  95. static bool hash_rnd_inited __read_mostly;
  96. static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr)
  97. {
  98. return jhash_1word((__force u32)addr->ip, hash_rnd) &
  99. (ip_list_hash_size - 1);
  100. }
  101. static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr)
  102. {
  103. return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) &
  104. (ip_list_hash_size - 1);
  105. }
  106. static struct recent_entry *
  107. recent_entry_lookup(const struct recent_table *table,
  108. const union nf_inet_addr *addrp, u_int16_t family,
  109. u_int8_t ttl)
  110. {
  111. struct recent_entry *e;
  112. unsigned int h;
  113. if (family == NFPROTO_IPV4)
  114. h = recent_entry_hash4(addrp);
  115. else
  116. h = recent_entry_hash6(addrp);
  117. list_for_each_entry(e, &table->iphash[h], list)
  118. if (e->family == family &&
  119. memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 &&
  120. (ttl == e->ttl || ttl == 0 || e->ttl == 0))
  121. return e;
  122. return NULL;
  123. }
  124. static void recent_entry_remove(struct recent_table *t, struct recent_entry *e)
  125. {
  126. list_del(&e->list);
  127. list_del(&e->lru_list);
  128. kfree(e);
  129. t->entries--;
  130. }
  131. /*
  132. * Drop entries with timestamps older then 'time'.
  133. */
  134. static void recent_entry_reap(struct recent_table *t, unsigned long time)
  135. {
  136. struct recent_entry *e;
  137. /*
  138. * The head of the LRU list is always the oldest entry.
  139. */
  140. e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
  141. /*
  142. * The last time stamp is the most recent.
  143. */
  144. if (time_after(time, e->stamps[e->index-1]))
  145. recent_entry_remove(t, e);
  146. }
  147. static struct recent_entry *
  148. recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr,
  149. u_int16_t family, u_int8_t ttl)
  150. {
  151. struct recent_entry *e;
  152. if (t->entries >= ip_list_tot) {
  153. e = list_entry(t->lru_list.next, struct recent_entry, lru_list);
  154. recent_entry_remove(t, e);
  155. }
  156. e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot,
  157. GFP_ATOMIC);
  158. if (e == NULL)
  159. return NULL;
  160. memcpy(&e->addr, addr, sizeof(e->addr));
  161. e->ttl = ttl;
  162. e->stamps[0] = jiffies;
  163. e->nstamps = 1;
  164. e->index = 1;
  165. e->family = family;
  166. if (family == NFPROTO_IPV4)
  167. list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
  168. else
  169. list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
  170. list_add_tail(&e->lru_list, &t->lru_list);
  171. t->entries++;
  172. return e;
  173. }
  174. static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
  175. {
  176. e->index %= ip_pkt_list_tot;
  177. e->stamps[e->index++] = jiffies;
  178. if (e->index > e->nstamps)
  179. e->nstamps = e->index;
  180. list_move_tail(&e->lru_list, &t->lru_list);
  181. }
  182. static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
  183. const char *name)
  184. {
  185. struct recent_table *t;
  186. list_for_each_entry(t, &recent_net->tables, list)
  187. if (!strcmp(t->name, name))
  188. return t;
  189. return NULL;
  190. }
  191. static void recent_table_flush(struct recent_table *t)
  192. {
  193. struct recent_entry *e, *next;
  194. unsigned int i;
  195. for (i = 0; i < ip_list_hash_size; i++)
  196. list_for_each_entry_safe(e, next, &t->iphash[i], list)
  197. recent_entry_remove(t, e);
  198. }
  199. static bool
  200. recent_mt(const struct sk_buff *skb, struct xt_action_param *par)
  201. {
  202. struct net *net = dev_net(par->in ? par->in : par->out);
  203. struct recent_net *recent_net = recent_pernet(net);
  204. const struct xt_recent_mtinfo *info = par->matchinfo;
  205. struct recent_table *t;
  206. struct recent_entry *e;
  207. union nf_inet_addr addr = {};
  208. u_int8_t ttl;
  209. bool ret = info->invert;
  210. if (par->family == NFPROTO_IPV4) {
  211. const struct iphdr *iph = ip_hdr(skb);
  212. if (info->side == XT_RECENT_DEST)
  213. addr.ip = iph->daddr;
  214. else
  215. addr.ip = iph->saddr;
  216. ttl = iph->ttl;
  217. } else {
  218. const struct ipv6hdr *iph = ipv6_hdr(skb);
  219. if (info->side == XT_RECENT_DEST)
  220. memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
  221. else
  222. memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
  223. ttl = iph->hop_limit;
  224. }
  225. /* use TTL as seen before forwarding */
  226. if (par->out != NULL && skb->sk == NULL)
  227. ttl++;
  228. spin_lock_bh(&recent_lock);
  229. t = recent_table_lookup(recent_net, info->name);
  230. e = recent_entry_lookup(t, &addr, par->family,
  231. (info->check_set & XT_RECENT_TTL) ? ttl : 0);
  232. if (e == NULL) {
  233. if (!(info->check_set & XT_RECENT_SET))
  234. goto out;
  235. e = recent_entry_init(t, &addr, par->family, ttl);
  236. if (e == NULL)
  237. par->hotdrop = true;
  238. ret = !ret;
  239. goto out;
  240. }
  241. if (info->check_set & XT_RECENT_SET)
  242. ret = !ret;
  243. else if (info->check_set & XT_RECENT_REMOVE) {
  244. recent_entry_remove(t, e);
  245. ret = !ret;
  246. } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) {
  247. unsigned long time = jiffies - info->seconds * HZ;
  248. unsigned int i, hits = 0;
  249. for (i = 0; i < e->nstamps; i++) {
  250. if (info->seconds && time_after(time, e->stamps[i]))
  251. continue;
  252. if (!info->hit_count || ++hits >= info->hit_count) {
  253. ret = !ret;
  254. break;
  255. }
  256. }
  257. /* info->seconds must be non-zero */
  258. if (info->check_set & XT_RECENT_REAP)
  259. recent_entry_reap(t, time);
  260. }
  261. if (info->check_set & XT_RECENT_SET ||
  262. (info->check_set & XT_RECENT_UPDATE && ret)) {
  263. recent_entry_update(t, e);
  264. e->ttl = ttl;
  265. }
  266. out:
  267. spin_unlock_bh(&recent_lock);
  268. return ret;
  269. }
  270. static int recent_mt_check(const struct xt_mtchk_param *par)
  271. {
  272. struct recent_net *recent_net = recent_pernet(par->net);
  273. const struct xt_recent_mtinfo *info = par->matchinfo;
  274. struct recent_table *t;
  275. #ifdef CONFIG_PROC_FS
  276. struct proc_dir_entry *pde;
  277. #endif
  278. unsigned i;
  279. int ret = -EINVAL;
  280. if (unlikely(!hash_rnd_inited)) {
  281. get_random_bytes(&hash_rnd, sizeof(hash_rnd));
  282. hash_rnd_inited = true;
  283. }
  284. if (info->check_set & ~XT_RECENT_VALID_FLAGS) {
  285. pr_info("Unsupported user space flags (%08x)\n",
  286. info->check_set);
  287. return -EINVAL;
  288. }
  289. if (hweight8(info->check_set &
  290. (XT_RECENT_SET | XT_RECENT_REMOVE |
  291. XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1)
  292. return -EINVAL;
  293. if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) &&
  294. (info->seconds || info->hit_count ||
  295. (info->check_set & XT_RECENT_MODIFIERS)))
  296. return -EINVAL;
  297. if ((info->check_set & XT_RECENT_REAP) && !info->seconds)
  298. return -EINVAL;
  299. if (info->hit_count > ip_pkt_list_tot) {
  300. pr_info("hitcount (%u) is larger than "
  301. "packets to be remembered (%u)\n",
  302. info->hit_count, ip_pkt_list_tot);
  303. return -EINVAL;
  304. }
  305. if (info->name[0] == '\0' ||
  306. strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN)
  307. return -EINVAL;
  308. mutex_lock(&recent_mutex);
  309. t = recent_table_lookup(recent_net, info->name);
  310. if (t != NULL) {
  311. t->refcnt++;
  312. ret = 0;
  313. goto out;
  314. }
  315. t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size,
  316. GFP_KERNEL);
  317. if (t == NULL) {
  318. ret = -ENOMEM;
  319. goto out;
  320. }
  321. t->refcnt = 1;
  322. strcpy(t->name, info->name);
  323. INIT_LIST_HEAD(&t->lru_list);
  324. for (i = 0; i < ip_list_hash_size; i++)
  325. INIT_LIST_HEAD(&t->iphash[i]);
  326. #ifdef CONFIG_PROC_FS
  327. pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
  328. &recent_mt_fops, t);
  329. if (pde == NULL) {
  330. kfree(t);
  331. ret = -ENOMEM;
  332. goto out;
  333. }
  334. pde->uid = ip_list_uid;
  335. pde->gid = ip_list_gid;
  336. #endif
  337. spin_lock_bh(&recent_lock);
  338. list_add_tail(&t->list, &recent_net->tables);
  339. spin_unlock_bh(&recent_lock);
  340. ret = 0;
  341. out:
  342. mutex_unlock(&recent_mutex);
  343. return ret;
  344. }
  345. static void recent_mt_destroy(const struct xt_mtdtor_param *par)
  346. {
  347. struct recent_net *recent_net = recent_pernet(par->net);
  348. const struct xt_recent_mtinfo *info = par->matchinfo;
  349. struct recent_table *t;
  350. mutex_lock(&recent_mutex);
  351. t = recent_table_lookup(recent_net, info->name);
  352. if (--t->refcnt == 0) {
  353. spin_lock_bh(&recent_lock);
  354. list_del(&t->list);
  355. spin_unlock_bh(&recent_lock);
  356. #ifdef CONFIG_PROC_FS
  357. remove_proc_entry(t->name, recent_net->xt_recent);
  358. #endif
  359. recent_table_flush(t);
  360. kfree(t);
  361. }
  362. mutex_unlock(&recent_mutex);
  363. }
  364. #ifdef CONFIG_PROC_FS
  365. struct recent_iter_state {
  366. const struct recent_table *table;
  367. unsigned int bucket;
  368. };
  369. static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
  370. __acquires(recent_lock)
  371. {
  372. struct recent_iter_state *st = seq->private;
  373. const struct recent_table *t = st->table;
  374. struct recent_entry *e;
  375. loff_t p = *pos;
  376. spin_lock_bh(&recent_lock);
  377. for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
  378. list_for_each_entry(e, &t->iphash[st->bucket], list)
  379. if (p-- == 0)
  380. return e;
  381. return NULL;
  382. }
  383. static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  384. {
  385. struct recent_iter_state *st = seq->private;
  386. const struct recent_table *t = st->table;
  387. const struct recent_entry *e = v;
  388. const struct list_head *head = e->list.next;
  389. while (head == &t->iphash[st->bucket]) {
  390. if (++st->bucket >= ip_list_hash_size)
  391. return NULL;
  392. head = t->iphash[st->bucket].next;
  393. }
  394. (*pos)++;
  395. return list_entry(head, struct recent_entry, list);
  396. }
  397. static void recent_seq_stop(struct seq_file *s, void *v)
  398. __releases(recent_lock)
  399. {
  400. spin_unlock_bh(&recent_lock);
  401. }
  402. static int recent_seq_show(struct seq_file *seq, void *v)
  403. {
  404. const struct recent_entry *e = v;
  405. unsigned int i;
  406. i = (e->index - 1) % ip_pkt_list_tot;
  407. if (e->family == NFPROTO_IPV4)
  408. seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
  409. &e->addr.ip, e->ttl, e->stamps[i], e->index);
  410. else
  411. seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
  412. &e->addr.in6, e->ttl, e->stamps[i], e->index);
  413. for (i = 0; i < e->nstamps; i++)
  414. seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
  415. seq_printf(seq, "\n");
  416. return 0;
  417. }
  418. static const struct seq_operations recent_seq_ops = {
  419. .start = recent_seq_start,
  420. .next = recent_seq_next,
  421. .stop = recent_seq_stop,
  422. .show = recent_seq_show,
  423. };
  424. static int recent_seq_open(struct inode *inode, struct file *file)
  425. {
  426. struct proc_dir_entry *pde = PDE(inode);
  427. struct recent_iter_state *st;
  428. st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
  429. if (st == NULL)
  430. return -ENOMEM;
  431. st->table = pde->data;
  432. return 0;
  433. }
  434. static ssize_t
  435. recent_mt_proc_write(struct file *file, const char __user *input,
  436. size_t size, loff_t *loff)
  437. {
  438. const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  439. struct recent_table *t = pde->data;
  440. struct recent_entry *e;
  441. char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
  442. const char *c = buf;
  443. union nf_inet_addr addr = {};
  444. u_int16_t family;
  445. bool add, succ;
  446. if (size == 0)
  447. return 0;
  448. if (size > sizeof(buf))
  449. size = sizeof(buf);
  450. if (copy_from_user(buf, input, size) != 0)
  451. return -EFAULT;
  452. /* Strict protocol! */
  453. if (*loff != 0)
  454. return -ESPIPE;
  455. switch (*c) {
  456. case '/': /* flush table */
  457. spin_lock_bh(&recent_lock);
  458. recent_table_flush(t);
  459. spin_unlock_bh(&recent_lock);
  460. return size;
  461. case '-': /* remove address */
  462. add = false;
  463. break;
  464. case '+': /* add address */
  465. add = true;
  466. break;
  467. default:
  468. pr_info("Need \"+ip\", \"-ip\" or \"/\"\n");
  469. return -EINVAL;
  470. }
  471. ++c;
  472. --size;
  473. if (strnchr(c, size, ':') != NULL) {
  474. family = NFPROTO_IPV6;
  475. succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
  476. } else {
  477. family = NFPROTO_IPV4;
  478. succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
  479. }
  480. if (!succ) {
  481. pr_info("illegal address written to procfs\n");
  482. return -EINVAL;
  483. }
  484. spin_lock_bh(&recent_lock);
  485. e = recent_entry_lookup(t, &addr, family, 0);
  486. if (e == NULL) {
  487. if (add)
  488. recent_entry_init(t, &addr, family, 0);
  489. } else {
  490. if (add)
  491. recent_entry_update(t, e);
  492. else
  493. recent_entry_remove(t, e);
  494. }
  495. spin_unlock_bh(&recent_lock);
  496. /* Note we removed one above */
  497. *loff += size + 1;
  498. return size + 1;
  499. }
  500. static const struct file_operations recent_mt_fops = {
  501. .open = recent_seq_open,
  502. .read = seq_read,
  503. .write = recent_mt_proc_write,
  504. .release = seq_release_private,
  505. .owner = THIS_MODULE,
  506. .llseek = seq_lseek,
  507. };
  508. static int __net_init recent_proc_net_init(struct net *net)
  509. {
  510. struct recent_net *recent_net = recent_pernet(net);
  511. recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
  512. if (!recent_net->xt_recent)
  513. return -ENOMEM;
  514. return 0;
  515. }
  516. static void __net_exit recent_proc_net_exit(struct net *net)
  517. {
  518. proc_net_remove(net, "xt_recent");
  519. }
  520. #else
  521. static inline int recent_proc_net_init(struct net *net)
  522. {
  523. return 0;
  524. }
  525. static inline void recent_proc_net_exit(struct net *net)
  526. {
  527. }
  528. #endif /* CONFIG_PROC_FS */
  529. static int __net_init recent_net_init(struct net *net)
  530. {
  531. struct recent_net *recent_net = recent_pernet(net);
  532. INIT_LIST_HEAD(&recent_net->tables);
  533. return recent_proc_net_init(net);
  534. }
  535. static void __net_exit recent_net_exit(struct net *net)
  536. {
  537. struct recent_net *recent_net = recent_pernet(net);
  538. BUG_ON(!list_empty(&recent_net->tables));
  539. recent_proc_net_exit(net);
  540. }
  541. static struct pernet_operations recent_net_ops = {
  542. .init = recent_net_init,
  543. .exit = recent_net_exit,
  544. .id = &recent_net_id,
  545. .size = sizeof(struct recent_net),
  546. };
  547. static struct xt_match recent_mt_reg[] __read_mostly = {
  548. {
  549. .name = "recent",
  550. .revision = 0,
  551. .family = NFPROTO_IPV4,
  552. .match = recent_mt,
  553. .matchsize = sizeof(struct xt_recent_mtinfo),
  554. .checkentry = recent_mt_check,
  555. .destroy = recent_mt_destroy,
  556. .me = THIS_MODULE,
  557. },
  558. {
  559. .name = "recent",
  560. .revision = 0,
  561. .family = NFPROTO_IPV6,
  562. .match = recent_mt,
  563. .matchsize = sizeof(struct xt_recent_mtinfo),
  564. .checkentry = recent_mt_check,
  565. .destroy = recent_mt_destroy,
  566. .me = THIS_MODULE,
  567. },
  568. };
  569. static int __init recent_mt_init(void)
  570. {
  571. int err;
  572. if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255)
  573. return -EINVAL;
  574. ip_list_hash_size = 1 << fls(ip_list_tot);
  575. err = register_pernet_subsys(&recent_net_ops);
  576. if (err)
  577. return err;
  578. err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
  579. if (err)
  580. unregister_pernet_subsys(&recent_net_ops);
  581. return err;
  582. }
  583. static void __exit recent_mt_exit(void)
  584. {
  585. xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
  586. unregister_pernet_subsys(&recent_net_ops);
  587. }
  588. module_init(recent_mt_init);
  589. module_exit(recent_mt_exit);