xt_recent.c 19 KB

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