Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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