xt_recent.c 19 KB

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