xt_recent.c 19 KB

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