inetpeer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * INETPEER - A storage for permanent information about peers
  3. *
  4. * This source is covered by the GNU GPL, the same as all kernel sources.
  5. *
  6. * Authors: Andrey V. Savochkin <saw@msu.ru>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/random.h>
  14. #include <linux/timer.h>
  15. #include <linux/time.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/net.h>
  19. #include <linux/workqueue.h>
  20. #include <net/ip.h>
  21. #include <net/inetpeer.h>
  22. #include <net/secure_seq.h>
  23. /*
  24. * Theory of operations.
  25. * We keep one entry for each peer IP address. The nodes contains long-living
  26. * information about the peer which doesn't depend on routes.
  27. *
  28. * Nodes are removed only when reference counter goes to 0.
  29. * When it's happened the node may be removed when a sufficient amount of
  30. * time has been passed since its last use. The less-recently-used entry can
  31. * also be removed if the pool is overloaded i.e. if the total amount of
  32. * entries is greater-or-equal than the threshold.
  33. *
  34. * Node pool is organised as an RB tree.
  35. * Such an implementation has been chosen not just for fun. It's a way to
  36. * prevent easy and efficient DoS attacks by creating hash collisions. A huge
  37. * amount of long living nodes in a single hash slot would significantly delay
  38. * lookups performed with disabled BHs.
  39. *
  40. * Serialisation issues.
  41. * 1. Nodes may appear in the tree only with the pool lock held.
  42. * 2. Nodes may disappear from the tree only with the pool lock held
  43. * AND reference count being 0.
  44. * 3. Global variable peer_total is modified under the pool lock.
  45. * 4. struct inet_peer fields modification:
  46. * rb_node: pool lock
  47. * refcnt: atomically against modifications on other CPU;
  48. * usually under some other lock to prevent node disappearing
  49. * daddr: unchangeable
  50. */
  51. static struct kmem_cache *peer_cachep __read_mostly;
  52. void inet_peer_base_init(struct inet_peer_base *bp)
  53. {
  54. bp->rb_root = RB_ROOT;
  55. seqlock_init(&bp->lock);
  56. bp->total = 0;
  57. }
  58. EXPORT_SYMBOL_GPL(inet_peer_base_init);
  59. #define PEER_MAX_GC 32
  60. /* Exported for sysctl_net_ipv4. */
  61. int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more
  62. * aggressively at this stage */
  63. int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
  64. int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */
  65. /* Called from ip_output.c:ip_init */
  66. void __init inet_initpeers(void)
  67. {
  68. struct sysinfo si;
  69. /* Use the straight interface to information about memory. */
  70. si_meminfo(&si);
  71. /* The values below were suggested by Alexey Kuznetsov
  72. * <kuznet@ms2.inr.ac.ru>. I don't have any opinion about the values
  73. * myself. --SAW
  74. */
  75. if (si.totalram <= (32768*1024)/PAGE_SIZE)
  76. inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
  77. if (si.totalram <= (16384*1024)/PAGE_SIZE)
  78. inet_peer_threshold >>= 1; /* about 512KB */
  79. if (si.totalram <= (8192*1024)/PAGE_SIZE)
  80. inet_peer_threshold >>= 2; /* about 128KB */
  81. peer_cachep = kmem_cache_create("inet_peer_cache",
  82. sizeof(struct inet_peer),
  83. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
  84. NULL);
  85. }
  86. /* Called with rcu_read_lock() or base->lock held */
  87. static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
  88. struct inet_peer_base *base,
  89. unsigned int seq,
  90. struct inet_peer *gc_stack[],
  91. unsigned int *gc_cnt,
  92. struct rb_node **parent_p,
  93. struct rb_node ***pp_p)
  94. {
  95. struct rb_node **pp, *parent;
  96. struct inet_peer *p;
  97. pp = &base->rb_root.rb_node;
  98. parent = NULL;
  99. while (*pp) {
  100. int cmp;
  101. parent = rcu_dereference_raw(*pp);
  102. p = rb_entry(parent, struct inet_peer, rb_node);
  103. cmp = inetpeer_addr_cmp(daddr, &p->daddr);
  104. if (cmp == 0) {
  105. if (!refcount_inc_not_zero(&p->refcnt))
  106. break;
  107. return p;
  108. }
  109. if (gc_stack) {
  110. if (*gc_cnt < PEER_MAX_GC)
  111. gc_stack[(*gc_cnt)++] = p;
  112. } else if (unlikely(read_seqretry(&base->lock, seq))) {
  113. break;
  114. }
  115. if (cmp == -1)
  116. pp = &(*pp)->rb_left;
  117. else
  118. pp = &(*pp)->rb_right;
  119. }
  120. *parent_p = parent;
  121. *pp_p = pp;
  122. return NULL;
  123. }
  124. static void inetpeer_free_rcu(struct rcu_head *head)
  125. {
  126. kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
  127. }
  128. /* perform garbage collect on all items stacked during a lookup */
  129. static void inet_peer_gc(struct inet_peer_base *base,
  130. struct inet_peer *gc_stack[],
  131. unsigned int gc_cnt)
  132. {
  133. struct inet_peer *p;
  134. __u32 delta, ttl;
  135. int i;
  136. if (base->total >= inet_peer_threshold)
  137. ttl = 0; /* be aggressive */
  138. else
  139. ttl = inet_peer_maxttl
  140. - (inet_peer_maxttl - inet_peer_minttl) / HZ *
  141. base->total / inet_peer_threshold * HZ;
  142. for (i = 0; i < gc_cnt; i++) {
  143. p = gc_stack[i];
  144. delta = (__u32)jiffies - p->dtime;
  145. if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
  146. gc_stack[i] = NULL;
  147. }
  148. for (i = 0; i < gc_cnt; i++) {
  149. p = gc_stack[i];
  150. if (p) {
  151. rb_erase(&p->rb_node, &base->rb_root);
  152. base->total--;
  153. call_rcu(&p->rcu, inetpeer_free_rcu);
  154. }
  155. }
  156. }
  157. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  158. const struct inetpeer_addr *daddr,
  159. int create)
  160. {
  161. struct inet_peer *p, *gc_stack[PEER_MAX_GC];
  162. struct rb_node **pp, *parent;
  163. unsigned int gc_cnt, seq;
  164. int invalidated;
  165. /* Attempt a lockless lookup first.
  166. * Because of a concurrent writer, we might not find an existing entry.
  167. */
  168. rcu_read_lock();
  169. seq = read_seqbegin(&base->lock);
  170. p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
  171. invalidated = read_seqretry(&base->lock, seq);
  172. rcu_read_unlock();
  173. if (p)
  174. return p;
  175. /* If no writer did a change during our lookup, we can return early. */
  176. if (!create && !invalidated)
  177. return NULL;
  178. /* retry an exact lookup, taking the lock before.
  179. * At least, nodes should be hot in our cache.
  180. */
  181. parent = NULL;
  182. write_seqlock_bh(&base->lock);
  183. gc_cnt = 0;
  184. p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
  185. if (!p && create) {
  186. p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
  187. if (p) {
  188. p->daddr = *daddr;
  189. refcount_set(&p->refcnt, 2);
  190. atomic_set(&p->rid, 0);
  191. p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
  192. p->rate_tokens = 0;
  193. /* 60*HZ is arbitrary, but chosen enough high so that the first
  194. * calculation of tokens is at its maximum.
  195. */
  196. p->rate_last = jiffies - 60*HZ;
  197. rb_link_node(&p->rb_node, parent, pp);
  198. rb_insert_color(&p->rb_node, &base->rb_root);
  199. base->total++;
  200. }
  201. }
  202. if (gc_cnt)
  203. inet_peer_gc(base, gc_stack, gc_cnt);
  204. write_sequnlock_bh(&base->lock);
  205. return p;
  206. }
  207. EXPORT_SYMBOL_GPL(inet_getpeer);
  208. void inet_putpeer(struct inet_peer *p)
  209. {
  210. p->dtime = (__u32)jiffies;
  211. if (refcount_dec_and_test(&p->refcnt))
  212. call_rcu(&p->rcu, inetpeer_free_rcu);
  213. }
  214. EXPORT_SYMBOL_GPL(inet_putpeer);
  215. /*
  216. * Check transmit rate limitation for given message.
  217. * The rate information is held in the inet_peer entries now.
  218. * This function is generic and could be used for other purposes
  219. * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
  220. *
  221. * Note that the same inet_peer fields are modified by functions in
  222. * route.c too, but these work for packet destinations while xrlim_allow
  223. * works for icmp destinations. This means the rate limiting information
  224. * for one "ip object" is shared - and these ICMPs are twice limited:
  225. * by source and by destination.
  226. *
  227. * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
  228. * SHOULD allow setting of rate limits
  229. *
  230. * Shared between ICMPv4 and ICMPv6.
  231. */
  232. #define XRLIM_BURST_FACTOR 6
  233. bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
  234. {
  235. unsigned long now, token;
  236. bool rc = false;
  237. if (!peer)
  238. return true;
  239. token = peer->rate_tokens;
  240. now = jiffies;
  241. token += now - peer->rate_last;
  242. peer->rate_last = now;
  243. if (token > XRLIM_BURST_FACTOR * timeout)
  244. token = XRLIM_BURST_FACTOR * timeout;
  245. if (token >= timeout) {
  246. token -= timeout;
  247. rc = true;
  248. }
  249. peer->rate_tokens = token;
  250. return rc;
  251. }
  252. EXPORT_SYMBOL(inet_peer_xrlim_allow);
  253. void inetpeer_invalidate_tree(struct inet_peer_base *base)
  254. {
  255. struct inet_peer *p, *n;
  256. rbtree_postorder_for_each_entry_safe(p, n, &base->rb_root, rb_node) {
  257. inet_putpeer(p);
  258. cond_resched();
  259. }
  260. base->rb_root = RB_ROOT;
  261. base->total = 0;
  262. }
  263. EXPORT_SYMBOL(inetpeer_invalidate_tree);