xt_recent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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, S_IRUGO | S_IWUSR);
  52. module_param(ip_list_gid, uint, S_IRUGO | S_IWUSR);
  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 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_old_fops, 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(sizeof(*e) + sizeof(e->stamps[0]) * nstamps_max,
  163. GFP_ATOMIC);
  164. if (e == NULL)
  165. return NULL;
  166. memcpy(&e->addr, addr, sizeof(e->addr));
  167. e->ttl = ttl;
  168. e->stamps[0] = jiffies;
  169. e->nstamps = 1;
  170. e->index = 1;
  171. e->family = family;
  172. if (family == NFPROTO_IPV4)
  173. list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]);
  174. else
  175. list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]);
  176. list_add_tail(&e->lru_list, &t->lru_list);
  177. t->entries++;
  178. return e;
  179. }
  180. static void recent_entry_update(struct recent_table *t, struct recent_entry *e)
  181. {
  182. e->index &= t->nstamps_max_mask;
  183. e->stamps[e->index++] = jiffies;
  184. if (e->index > e->nstamps)
  185. e->nstamps = e->index;
  186. list_move_tail(&e->lru_list, &t->lru_list);
  187. }
  188. static struct recent_table *recent_table_lookup(struct recent_net *recent_net,
  189. const char *name)
  190. {
  191. struct recent_table *t;
  192. list_for_each_entry(t, &recent_net->tables, list)
  193. if (!strcmp(t->name, name))
  194. return t;
  195. return NULL;
  196. }
  197. static void recent_table_flush(struct recent_table *t)
  198. {
  199. struct recent_entry *e, *next;
  200. unsigned int i;
  201. for (i = 0; i < ip_list_hash_size; i++)
  202. list_for_each_entry_safe(e, next, &t->iphash[i], list)
  203. recent_entry_remove(t, e);
  204. }
  205. static bool
  206. recent_mt(const struct sk_buff *skb, struct xt_action_param *par)
  207. {
  208. struct net *net = par->net;
  209. struct recent_net *recent_net = recent_pernet(net);
  210. const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
  211. struct recent_table *t;
  212. struct recent_entry *e;
  213. union nf_inet_addr addr = {}, addr_mask;
  214. u_int8_t ttl;
  215. bool ret = info->invert;
  216. if (par->family == NFPROTO_IPV4) {
  217. const struct iphdr *iph = ip_hdr(skb);
  218. if (info->side == XT_RECENT_DEST)
  219. addr.ip = iph->daddr;
  220. else
  221. addr.ip = iph->saddr;
  222. ttl = iph->ttl;
  223. } else {
  224. const struct ipv6hdr *iph = ipv6_hdr(skb);
  225. if (info->side == XT_RECENT_DEST)
  226. memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6));
  227. else
  228. memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6));
  229. ttl = iph->hop_limit;
  230. }
  231. /* use TTL as seen before forwarding */
  232. if (par->out != NULL && skb->sk == NULL)
  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, par->family,
  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, par->family, 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("Unsupported user space 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("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. if (sz <= PAGE_SIZE)
  340. t = kzalloc(sz, GFP_KERNEL);
  341. else
  342. t = vzalloc(sz);
  343. if (t == NULL) {
  344. ret = -ENOMEM;
  345. goto out;
  346. }
  347. t->refcnt = 1;
  348. t->nstamps_max_mask = nstamp_mask;
  349. memcpy(&t->mask, &info->mask, sizeof(t->mask));
  350. strcpy(t->name, info->name);
  351. INIT_LIST_HEAD(&t->lru_list);
  352. for (i = 0; i < ip_list_hash_size; i++)
  353. INIT_LIST_HEAD(&t->iphash[i]);
  354. #ifdef CONFIG_PROC_FS
  355. uid = make_kuid(&init_user_ns, ip_list_uid);
  356. gid = make_kgid(&init_user_ns, ip_list_gid);
  357. if (!uid_valid(uid) || !gid_valid(gid)) {
  358. recent_table_free(t);
  359. ret = -EINVAL;
  360. goto out;
  361. }
  362. pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent,
  363. &recent_mt_fops, t);
  364. if (pde == NULL) {
  365. recent_table_free(t);
  366. ret = -ENOMEM;
  367. goto out;
  368. }
  369. proc_set_user(pde, uid, gid);
  370. #endif
  371. spin_lock_bh(&recent_lock);
  372. list_add_tail(&t->list, &recent_net->tables);
  373. spin_unlock_bh(&recent_lock);
  374. ret = 0;
  375. out:
  376. mutex_unlock(&recent_mutex);
  377. return ret;
  378. }
  379. static int recent_mt_check_v0(const struct xt_mtchk_param *par)
  380. {
  381. const struct xt_recent_mtinfo_v0 *info_v0 = par->matchinfo;
  382. struct xt_recent_mtinfo_v1 info_v1;
  383. /* Copy revision 0 structure to revision 1 */
  384. memcpy(&info_v1, info_v0, sizeof(struct xt_recent_mtinfo));
  385. /* Set default mask to ensure backward compatible behaviour */
  386. memset(info_v1.mask.all, 0xFF, sizeof(info_v1.mask.all));
  387. return recent_mt_check(par, &info_v1);
  388. }
  389. static int recent_mt_check_v1(const struct xt_mtchk_param *par)
  390. {
  391. return recent_mt_check(par, par->matchinfo);
  392. }
  393. static void recent_mt_destroy(const struct xt_mtdtor_param *par)
  394. {
  395. struct recent_net *recent_net = recent_pernet(par->net);
  396. const struct xt_recent_mtinfo_v1 *info = par->matchinfo;
  397. struct recent_table *t;
  398. mutex_lock(&recent_mutex);
  399. t = recent_table_lookup(recent_net, info->name);
  400. if (--t->refcnt == 0) {
  401. spin_lock_bh(&recent_lock);
  402. list_del(&t->list);
  403. spin_unlock_bh(&recent_lock);
  404. #ifdef CONFIG_PROC_FS
  405. if (recent_net->xt_recent != NULL)
  406. remove_proc_entry(t->name, recent_net->xt_recent);
  407. #endif
  408. recent_table_flush(t);
  409. recent_table_free(t);
  410. }
  411. mutex_unlock(&recent_mutex);
  412. }
  413. #ifdef CONFIG_PROC_FS
  414. struct recent_iter_state {
  415. const struct recent_table *table;
  416. unsigned int bucket;
  417. };
  418. static void *recent_seq_start(struct seq_file *seq, loff_t *pos)
  419. __acquires(recent_lock)
  420. {
  421. struct recent_iter_state *st = seq->private;
  422. const struct recent_table *t = st->table;
  423. struct recent_entry *e;
  424. loff_t p = *pos;
  425. spin_lock_bh(&recent_lock);
  426. for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++)
  427. list_for_each_entry(e, &t->iphash[st->bucket], list)
  428. if (p-- == 0)
  429. return e;
  430. return NULL;
  431. }
  432. static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  433. {
  434. struct recent_iter_state *st = seq->private;
  435. const struct recent_table *t = st->table;
  436. const struct recent_entry *e = v;
  437. const struct list_head *head = e->list.next;
  438. while (head == &t->iphash[st->bucket]) {
  439. if (++st->bucket >= ip_list_hash_size)
  440. return NULL;
  441. head = t->iphash[st->bucket].next;
  442. }
  443. (*pos)++;
  444. return list_entry(head, struct recent_entry, list);
  445. }
  446. static void recent_seq_stop(struct seq_file *s, void *v)
  447. __releases(recent_lock)
  448. {
  449. spin_unlock_bh(&recent_lock);
  450. }
  451. static int recent_seq_show(struct seq_file *seq, void *v)
  452. {
  453. const struct recent_entry *e = v;
  454. struct recent_iter_state *st = seq->private;
  455. const struct recent_table *t = st->table;
  456. unsigned int i;
  457. i = (e->index - 1) & t->nstamps_max_mask;
  458. if (e->family == NFPROTO_IPV4)
  459. seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u",
  460. &e->addr.ip, e->ttl, e->stamps[i], e->index);
  461. else
  462. seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u",
  463. &e->addr.in6, e->ttl, e->stamps[i], e->index);
  464. for (i = 0; i < e->nstamps; i++)
  465. seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]);
  466. seq_printf(seq, "\n");
  467. return 0;
  468. }
  469. static const struct seq_operations recent_seq_ops = {
  470. .start = recent_seq_start,
  471. .next = recent_seq_next,
  472. .stop = recent_seq_stop,
  473. .show = recent_seq_show,
  474. };
  475. static int recent_seq_open(struct inode *inode, struct file *file)
  476. {
  477. struct recent_iter_state *st;
  478. st = __seq_open_private(file, &recent_seq_ops, sizeof(*st));
  479. if (st == NULL)
  480. return -ENOMEM;
  481. st->table = PDE_DATA(inode);
  482. return 0;
  483. }
  484. static ssize_t
  485. recent_mt_proc_write(struct file *file, const char __user *input,
  486. size_t size, loff_t *loff)
  487. {
  488. struct recent_table *t = PDE_DATA(file_inode(file));
  489. struct recent_entry *e;
  490. char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")];
  491. const char *c = buf;
  492. union nf_inet_addr addr = {};
  493. u_int16_t family;
  494. bool add, succ;
  495. if (size == 0)
  496. return 0;
  497. if (size > sizeof(buf))
  498. size = sizeof(buf);
  499. if (copy_from_user(buf, input, size) != 0)
  500. return -EFAULT;
  501. /* Strict protocol! */
  502. if (*loff != 0)
  503. return -ESPIPE;
  504. switch (*c) {
  505. case '/': /* flush table */
  506. spin_lock_bh(&recent_lock);
  507. recent_table_flush(t);
  508. spin_unlock_bh(&recent_lock);
  509. return size;
  510. case '-': /* remove address */
  511. add = false;
  512. break;
  513. case '+': /* add address */
  514. add = true;
  515. break;
  516. default:
  517. pr_info("Need \"+ip\", \"-ip\" or \"/\"\n");
  518. return -EINVAL;
  519. }
  520. ++c;
  521. --size;
  522. if (strnchr(c, size, ':') != NULL) {
  523. family = NFPROTO_IPV6;
  524. succ = in6_pton(c, size, (void *)&addr, '\n', NULL);
  525. } else {
  526. family = NFPROTO_IPV4;
  527. succ = in4_pton(c, size, (void *)&addr, '\n', NULL);
  528. }
  529. if (!succ) {
  530. pr_info("illegal address written to procfs\n");
  531. return -EINVAL;
  532. }
  533. spin_lock_bh(&recent_lock);
  534. e = recent_entry_lookup(t, &addr, family, 0);
  535. if (e == NULL) {
  536. if (add)
  537. recent_entry_init(t, &addr, family, 0);
  538. } else {
  539. if (add)
  540. recent_entry_update(t, e);
  541. else
  542. recent_entry_remove(t, e);
  543. }
  544. spin_unlock_bh(&recent_lock);
  545. /* Note we removed one above */
  546. *loff += size + 1;
  547. return size + 1;
  548. }
  549. static const struct file_operations recent_mt_fops = {
  550. .open = recent_seq_open,
  551. .read = seq_read,
  552. .write = recent_mt_proc_write,
  553. .release = seq_release_private,
  554. .owner = THIS_MODULE,
  555. .llseek = seq_lseek,
  556. };
  557. static int __net_init recent_proc_net_init(struct net *net)
  558. {
  559. struct recent_net *recent_net = recent_pernet(net);
  560. recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net);
  561. if (!recent_net->xt_recent)
  562. return -ENOMEM;
  563. return 0;
  564. }
  565. static void __net_exit recent_proc_net_exit(struct net *net)
  566. {
  567. struct recent_net *recent_net = recent_pernet(net);
  568. struct recent_table *t;
  569. /* recent_net_exit() is called before recent_mt_destroy(). Make sure
  570. * that the parent xt_recent proc entry is is empty before trying to
  571. * remove it.
  572. */
  573. spin_lock_bh(&recent_lock);
  574. list_for_each_entry(t, &recent_net->tables, list)
  575. remove_proc_entry(t->name, recent_net->xt_recent);
  576. recent_net->xt_recent = NULL;
  577. spin_unlock_bh(&recent_lock);
  578. remove_proc_entry("xt_recent", net->proc_net);
  579. }
  580. #else
  581. static inline int recent_proc_net_init(struct net *net)
  582. {
  583. return 0;
  584. }
  585. static inline void recent_proc_net_exit(struct net *net)
  586. {
  587. }
  588. #endif /* CONFIG_PROC_FS */
  589. static int __net_init recent_net_init(struct net *net)
  590. {
  591. struct recent_net *recent_net = recent_pernet(net);
  592. INIT_LIST_HEAD(&recent_net->tables);
  593. return recent_proc_net_init(net);
  594. }
  595. static void __net_exit recent_net_exit(struct net *net)
  596. {
  597. recent_proc_net_exit(net);
  598. }
  599. static struct pernet_operations recent_net_ops = {
  600. .init = recent_net_init,
  601. .exit = recent_net_exit,
  602. .id = &recent_net_id,
  603. .size = sizeof(struct recent_net),
  604. };
  605. static struct xt_match recent_mt_reg[] __read_mostly = {
  606. {
  607. .name = "recent",
  608. .revision = 0,
  609. .family = NFPROTO_IPV4,
  610. .match = recent_mt,
  611. .matchsize = sizeof(struct xt_recent_mtinfo),
  612. .checkentry = recent_mt_check_v0,
  613. .destroy = recent_mt_destroy,
  614. .me = THIS_MODULE,
  615. },
  616. {
  617. .name = "recent",
  618. .revision = 0,
  619. .family = NFPROTO_IPV6,
  620. .match = recent_mt,
  621. .matchsize = sizeof(struct xt_recent_mtinfo),
  622. .checkentry = recent_mt_check_v0,
  623. .destroy = recent_mt_destroy,
  624. .me = THIS_MODULE,
  625. },
  626. {
  627. .name = "recent",
  628. .revision = 1,
  629. .family = NFPROTO_IPV4,
  630. .match = recent_mt,
  631. .matchsize = sizeof(struct xt_recent_mtinfo_v1),
  632. .checkentry = recent_mt_check_v1,
  633. .destroy = recent_mt_destroy,
  634. .me = THIS_MODULE,
  635. },
  636. {
  637. .name = "recent",
  638. .revision = 1,
  639. .family = NFPROTO_IPV6,
  640. .match = recent_mt,
  641. .matchsize = sizeof(struct xt_recent_mtinfo_v1),
  642. .checkentry = recent_mt_check_v1,
  643. .destroy = recent_mt_destroy,
  644. .me = THIS_MODULE,
  645. }
  646. };
  647. static int __init recent_mt_init(void)
  648. {
  649. int err;
  650. BUILD_BUG_ON_NOT_POWER_OF_2(XT_RECENT_MAX_NSTAMPS);
  651. if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS)
  652. return -EINVAL;
  653. ip_list_hash_size = 1 << fls(ip_list_tot);
  654. err = register_pernet_subsys(&recent_net_ops);
  655. if (err)
  656. return err;
  657. err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
  658. if (err)
  659. unregister_pernet_subsys(&recent_net_ops);
  660. return err;
  661. }
  662. static void __exit recent_mt_exit(void)
  663. {
  664. xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg));
  665. unregister_pernet_subsys(&recent_net_ops);
  666. }
  667. module_init(recent_mt_init);
  668. module_exit(recent_mt_exit);