11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INETPEER - A storage for permanent information about peers 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This source is covered by the GNU GPL, the same as all kernel sources. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: Andrey V. Savochkin <saw@msu.ru> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/module.h> 101da177e4SLinus Torvalds #include <linux/types.h> 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/interrupt.h> 131da177e4SLinus Torvalds #include <linux/spinlock.h> 141da177e4SLinus Torvalds #include <linux/random.h> 151da177e4SLinus Torvalds #include <linux/timer.h> 161da177e4SLinus Torvalds #include <linux/time.h> 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/mm.h> 191da177e4SLinus Torvalds #include <linux/net.h> 205faa5df1SSteffen Klassert #include <linux/workqueue.h> 2120380731SArnaldo Carvalho de Melo #include <net/ip.h> 221da177e4SLinus Torvalds #include <net/inetpeer.h> 236e5714eaSDavid S. Miller #include <net/secure_seq.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds /* 261da177e4SLinus Torvalds * Theory of operations. 271da177e4SLinus Torvalds * We keep one entry for each peer IP address. The nodes contains long-living 281da177e4SLinus Torvalds * information about the peer which doesn't depend on routes. 291da177e4SLinus Torvalds * At this moment this information consists only of ID field for the next 301da177e4SLinus Torvalds * outgoing IP packet. This field is incremented with each packet as encoded 311da177e4SLinus Torvalds * in inet_getid() function (include/net/inetpeer.h). 321da177e4SLinus Torvalds * At the moment of writing this notes identifier of IP packets is generated 331da177e4SLinus Torvalds * to be unpredictable using this code only for packets subjected 341da177e4SLinus Torvalds * (actually or potentially) to defragmentation. I.e. DF packets less than 351da177e4SLinus Torvalds * PMTU in size uses a constant ID and do not use this code (see 361da177e4SLinus Torvalds * ip_select_ident() in include/net/ip.h). 371da177e4SLinus Torvalds * 381da177e4SLinus Torvalds * Route cache entries hold references to our nodes. 391da177e4SLinus Torvalds * New cache entries get references via lookup by destination IP address in 401da177e4SLinus Torvalds * the avl tree. The reference is grabbed only when it's needed i.e. only 411da177e4SLinus Torvalds * when we try to output IP packet which needs an unpredictable ID (see 421da177e4SLinus Torvalds * __ip_select_ident() in net/ipv4/route.c). 431da177e4SLinus Torvalds * Nodes are removed only when reference counter goes to 0. 441da177e4SLinus Torvalds * When it's happened the node may be removed when a sufficient amount of 451da177e4SLinus Torvalds * time has been passed since its last use. The less-recently-used entry can 461da177e4SLinus Torvalds * also be removed if the pool is overloaded i.e. if the total amount of 471da177e4SLinus Torvalds * entries is greater-or-equal than the threshold. 481da177e4SLinus Torvalds * 491da177e4SLinus Torvalds * Node pool is organised as an AVL tree. 501da177e4SLinus Torvalds * Such an implementation has been chosen not just for fun. It's a way to 511da177e4SLinus Torvalds * prevent easy and efficient DoS attacks by creating hash collisions. A huge 521da177e4SLinus Torvalds * amount of long living nodes in a single hash slot would significantly delay 531da177e4SLinus Torvalds * lookups performed with disabled BHs. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * Serialisation issues. 56aa1039e7SEric Dumazet * 1. Nodes may appear in the tree only with the pool lock held. 57aa1039e7SEric Dumazet * 2. Nodes may disappear from the tree only with the pool lock held 581da177e4SLinus Torvalds * AND reference count being 0. 594b9d9be8SEric Dumazet * 3. Global variable peer_total is modified under the pool lock. 604b9d9be8SEric Dumazet * 4. struct inet_peer fields modification: 611da177e4SLinus Torvalds * avl_left, avl_right, avl_parent, avl_height: pool lock 621da177e4SLinus Torvalds * refcnt: atomically against modifications on other CPU; 631da177e4SLinus Torvalds * usually under some other lock to prevent node disappearing 64582a72daSDavid S. Miller * daddr: unchangeable 65317fe0e6SEric Dumazet * ip_id_count: atomic value (no lock needed) 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 68e18b890bSChristoph Lameter static struct kmem_cache *peer_cachep __read_mostly; 691da177e4SLinus Torvalds 705faa5df1SSteffen Klassert static LIST_HEAD(gc_list); 715faa5df1SSteffen Klassert static const int gc_delay = 60 * HZ; 725faa5df1SSteffen Klassert static struct delayed_work gc_work; 735faa5df1SSteffen Klassert static DEFINE_SPINLOCK(gc_lock); 745faa5df1SSteffen Klassert 751da177e4SLinus Torvalds #define node_height(x) x->avl_height 76d6cc1d64SEric Dumazet 77d6cc1d64SEric Dumazet #define peer_avl_empty ((struct inet_peer *)&peer_fake_node) 78b914c4eaSEric Dumazet #define peer_avl_empty_rcu ((struct inet_peer __rcu __force *)&peer_fake_node) 79d6cc1d64SEric Dumazet static const struct inet_peer peer_fake_node = { 80b914c4eaSEric Dumazet .avl_left = peer_avl_empty_rcu, 81b914c4eaSEric Dumazet .avl_right = peer_avl_empty_rcu, 821da177e4SLinus Torvalds .avl_height = 0 831da177e4SLinus Torvalds }; 84d6cc1d64SEric Dumazet 85021e9299SDavid S. Miller struct inet_peer_base { 86b914c4eaSEric Dumazet struct inet_peer __rcu *root; 8765e8354eSEric Dumazet seqlock_t lock; 88d6cc1d64SEric Dumazet int total; 89021e9299SDavid S. Miller }; 90021e9299SDavid S. Miller 911da177e4SLinus Torvalds #define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4. */ 94243bbcaaSEric Dumazet int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more 951da177e4SLinus Torvalds * aggressively at this stage */ 96243bbcaaSEric Dumazet int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */ 97243bbcaaSEric Dumazet int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */ 981da177e4SLinus Torvalds 995faa5df1SSteffen Klassert static void inetpeer_gc_worker(struct work_struct *work) 1005faa5df1SSteffen Klassert { 1015faa5df1SSteffen Klassert struct inet_peer *p, *n; 1025faa5df1SSteffen Klassert LIST_HEAD(list); 1035faa5df1SSteffen Klassert 1045faa5df1SSteffen Klassert spin_lock_bh(&gc_lock); 1055faa5df1SSteffen Klassert list_replace_init(&gc_list, &list); 1065faa5df1SSteffen Klassert spin_unlock_bh(&gc_lock); 1075faa5df1SSteffen Klassert 1085faa5df1SSteffen Klassert if (list_empty(&list)) 1095faa5df1SSteffen Klassert return; 1105faa5df1SSteffen Klassert 1115faa5df1SSteffen Klassert list_for_each_entry_safe(p, n, &list, gc_list) { 1125faa5df1SSteffen Klassert 1135faa5df1SSteffen Klassert if(need_resched()) 1145faa5df1SSteffen Klassert cond_resched(); 1155faa5df1SSteffen Klassert 1165faa5df1SSteffen Klassert if (p->avl_left != peer_avl_empty) { 1175faa5df1SSteffen Klassert list_add_tail(&p->avl_left->gc_list, &list); 1185faa5df1SSteffen Klassert p->avl_left = peer_avl_empty; 1195faa5df1SSteffen Klassert } 1205faa5df1SSteffen Klassert 1215faa5df1SSteffen Klassert if (p->avl_right != peer_avl_empty) { 1225faa5df1SSteffen Klassert list_add_tail(&p->avl_right->gc_list, &list); 1235faa5df1SSteffen Klassert p->avl_right = peer_avl_empty; 1245faa5df1SSteffen Klassert } 1255faa5df1SSteffen Klassert 1265faa5df1SSteffen Klassert n = list_entry(p->gc_list.next, struct inet_peer, gc_list); 1275faa5df1SSteffen Klassert 1285faa5df1SSteffen Klassert if (!atomic_read(&p->refcnt)) { 1295faa5df1SSteffen Klassert list_del(&p->gc_list); 1305faa5df1SSteffen Klassert kmem_cache_free(peer_cachep, p); 1315faa5df1SSteffen Klassert } 1325faa5df1SSteffen Klassert } 1335faa5df1SSteffen Klassert 1345faa5df1SSteffen Klassert if (list_empty(&list)) 1355faa5df1SSteffen Klassert return; 1365faa5df1SSteffen Klassert 1375faa5df1SSteffen Klassert spin_lock_bh(&gc_lock); 1385faa5df1SSteffen Klassert list_splice(&list, &gc_list); 1395faa5df1SSteffen Klassert spin_unlock_bh(&gc_lock); 1405faa5df1SSteffen Klassert 1415faa5df1SSteffen Klassert schedule_delayed_work(&gc_work, gc_delay); 1425faa5df1SSteffen Klassert } 1431da177e4SLinus Torvalds 144*c8a627edSGao feng static int __net_init inetpeer_net_init(struct net *net) 145*c8a627edSGao feng { 146*c8a627edSGao feng net->ipv4.peers = kzalloc(sizeof(struct inet_peer_base), 147*c8a627edSGao feng GFP_KERNEL); 148*c8a627edSGao feng if (net->ipv4.peers == NULL) 149*c8a627edSGao feng return -ENOMEM; 150*c8a627edSGao feng 151*c8a627edSGao feng net->ipv4.peers->root = peer_avl_empty_rcu; 152*c8a627edSGao feng seqlock_init(&net->ipv4.peers->lock); 153*c8a627edSGao feng 154*c8a627edSGao feng net->ipv6.peers = kzalloc(sizeof(struct inet_peer_base), 155*c8a627edSGao feng GFP_KERNEL); 156*c8a627edSGao feng if (net->ipv6.peers == NULL) 157*c8a627edSGao feng goto out_ipv6; 158*c8a627edSGao feng 159*c8a627edSGao feng net->ipv6.peers->root = peer_avl_empty_rcu; 160*c8a627edSGao feng seqlock_init(&net->ipv6.peers->lock); 161*c8a627edSGao feng 162*c8a627edSGao feng return 0; 163*c8a627edSGao feng out_ipv6: 164*c8a627edSGao feng kfree(net->ipv4.peers); 165*c8a627edSGao feng return -ENOMEM; 166*c8a627edSGao feng } 167*c8a627edSGao feng 168*c8a627edSGao feng static void __net_exit inetpeer_net_exit(struct net *net) 169*c8a627edSGao feng { 170*c8a627edSGao feng inetpeer_invalidate_tree(net, AF_INET); 171*c8a627edSGao feng kfree(net->ipv4.peers); 172*c8a627edSGao feng net->ipv4.peers = NULL; 173*c8a627edSGao feng 174*c8a627edSGao feng inetpeer_invalidate_tree(net, AF_INET6); 175*c8a627edSGao feng kfree(net->ipv6.peers); 176*c8a627edSGao feng net->ipv6.peers = NULL; 177*c8a627edSGao feng } 178*c8a627edSGao feng 179*c8a627edSGao feng static struct pernet_operations inetpeer_ops = { 180*c8a627edSGao feng .init = inetpeer_net_init, 181*c8a627edSGao feng .exit = inetpeer_net_exit, 182*c8a627edSGao feng }; 183*c8a627edSGao feng 1841da177e4SLinus Torvalds /* Called from ip_output.c:ip_init */ 1851da177e4SLinus Torvalds void __init inet_initpeers(void) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds struct sysinfo si; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* Use the straight interface to information about memory. */ 1901da177e4SLinus Torvalds si_meminfo(&si); 1911da177e4SLinus Torvalds /* The values below were suggested by Alexey Kuznetsov 1921da177e4SLinus Torvalds * <kuznet@ms2.inr.ac.ru>. I don't have any opinion about the values 1931da177e4SLinus Torvalds * myself. --SAW 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds if (si.totalram <= (32768*1024)/PAGE_SIZE) 1961da177e4SLinus Torvalds inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */ 1971da177e4SLinus Torvalds if (si.totalram <= (16384*1024)/PAGE_SIZE) 1981da177e4SLinus Torvalds inet_peer_threshold >>= 1; /* about 512KB */ 1991da177e4SLinus Torvalds if (si.totalram <= (8192*1024)/PAGE_SIZE) 2001da177e4SLinus Torvalds inet_peer_threshold >>= 2; /* about 128KB */ 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds peer_cachep = kmem_cache_create("inet_peer_cache", 2031da177e4SLinus Torvalds sizeof(struct inet_peer), 204317fe0e6SEric Dumazet 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, 20520c2df83SPaul Mundt NULL); 2061da177e4SLinus Torvalds 2075faa5df1SSteffen Klassert INIT_DELAYED_WORK_DEFERRABLE(&gc_work, inetpeer_gc_worker); 208*c8a627edSGao feng register_pernet_subsys(&inetpeer_ops); 209d6cc1d64SEric Dumazet } 2101da177e4SLinus Torvalds 2118790ca17SDavid S. Miller static int addr_compare(const struct inetpeer_addr *a, 2128790ca17SDavid S. Miller const struct inetpeer_addr *b) 21302663045SDavid S. Miller { 21402663045SDavid S. Miller int i, n = (a->family == AF_INET ? 1 : 4); 21502663045SDavid S. Miller 21602663045SDavid S. Miller for (i = 0; i < n; i++) { 2177a71ed89SDavid S. Miller if (a->addr.a6[i] == b->addr.a6[i]) 21802663045SDavid S. Miller continue; 219747465efSEric Dumazet if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i]) 22002663045SDavid S. Miller return -1; 22102663045SDavid S. Miller return 1; 22202663045SDavid S. Miller } 22302663045SDavid S. Miller 22402663045SDavid S. Miller return 0; 22502663045SDavid S. Miller } 22602663045SDavid S. Miller 22765e8354eSEric Dumazet #define rcu_deref_locked(X, BASE) \ 22865e8354eSEric Dumazet rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock)) 22965e8354eSEric Dumazet 230243bbcaaSEric Dumazet /* 231243bbcaaSEric Dumazet * Called with local BH disabled and the pool lock held. 232243bbcaaSEric Dumazet */ 23398158f5aSDavid S. Miller #define lookup(_daddr, _stack, _base) \ 2341da177e4SLinus Torvalds ({ \ 235b914c4eaSEric Dumazet struct inet_peer *u; \ 236b914c4eaSEric Dumazet struct inet_peer __rcu **v; \ 237aa1039e7SEric Dumazet \ 238243bbcaaSEric Dumazet stackptr = _stack; \ 23998158f5aSDavid S. Miller *stackptr++ = &_base->root; \ 24065e8354eSEric Dumazet for (u = rcu_deref_locked(_base->root, _base); \ 241b914c4eaSEric Dumazet u != peer_avl_empty; ) { \ 24202663045SDavid S. Miller int cmp = addr_compare(_daddr, &u->daddr); \ 24302663045SDavid S. Miller if (cmp == 0) \ 2441da177e4SLinus Torvalds break; \ 24502663045SDavid S. Miller if (cmp == -1) \ 2461da177e4SLinus Torvalds v = &u->avl_left; \ 2471da177e4SLinus Torvalds else \ 2481da177e4SLinus Torvalds v = &u->avl_right; \ 2491da177e4SLinus Torvalds *stackptr++ = v; \ 25065e8354eSEric Dumazet u = rcu_deref_locked(*v, _base); \ 2511da177e4SLinus Torvalds } \ 2521da177e4SLinus Torvalds u; \ 2531da177e4SLinus Torvalds }) 2541da177e4SLinus Torvalds 255aa1039e7SEric Dumazet /* 2567b46ac4eSDavid S. Miller * Called with rcu_read_lock() 257aa1039e7SEric Dumazet * Because we hold no lock against a writer, its quite possible we fall 258aa1039e7SEric Dumazet * in an endless loop. 259aa1039e7SEric Dumazet * But every pointer we follow is guaranteed to be valid thanks to RCU. 260aa1039e7SEric Dumazet * We exit from this function if number of links exceeds PEER_MAXDEPTH 261aa1039e7SEric Dumazet */ 2627b46ac4eSDavid S. Miller static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr, 2634b9d9be8SEric Dumazet struct inet_peer_base *base) 264aa1039e7SEric Dumazet { 2657b46ac4eSDavid S. Miller struct inet_peer *u = rcu_dereference(base->root); 266aa1039e7SEric Dumazet int count = 0; 267aa1039e7SEric Dumazet 268aa1039e7SEric Dumazet while (u != peer_avl_empty) { 26902663045SDavid S. Miller int cmp = addr_compare(daddr, &u->daddr); 27002663045SDavid S. Miller if (cmp == 0) { 2715f2f8920SEric Dumazet /* Before taking a reference, check if this entry was 2724b9d9be8SEric Dumazet * deleted (refcnt=-1) 2735f2f8920SEric Dumazet */ 2744b9d9be8SEric Dumazet if (!atomic_add_unless(&u->refcnt, 1, -1)) 275aa1039e7SEric Dumazet u = NULL; 276aa1039e7SEric Dumazet return u; 277aa1039e7SEric Dumazet } 27802663045SDavid S. Miller if (cmp == -1) 2797b46ac4eSDavid S. Miller u = rcu_dereference(u->avl_left); 280aa1039e7SEric Dumazet else 2817b46ac4eSDavid S. Miller u = rcu_dereference(u->avl_right); 282aa1039e7SEric Dumazet if (unlikely(++count == PEER_MAXDEPTH)) 283aa1039e7SEric Dumazet break; 284aa1039e7SEric Dumazet } 285aa1039e7SEric Dumazet return NULL; 286aa1039e7SEric Dumazet } 287aa1039e7SEric Dumazet 288aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */ 28998158f5aSDavid S. Miller #define lookup_rightempty(start, base) \ 2901da177e4SLinus Torvalds ({ \ 291b914c4eaSEric Dumazet struct inet_peer *u; \ 292b914c4eaSEric Dumazet struct inet_peer __rcu **v; \ 2931da177e4SLinus Torvalds *stackptr++ = &start->avl_left; \ 2941da177e4SLinus Torvalds v = &start->avl_left; \ 29565e8354eSEric Dumazet for (u = rcu_deref_locked(*v, base); \ 296b914c4eaSEric Dumazet u->avl_right != peer_avl_empty_rcu; ) { \ 2971da177e4SLinus Torvalds v = &u->avl_right; \ 2981da177e4SLinus Torvalds *stackptr++ = v; \ 29965e8354eSEric Dumazet u = rcu_deref_locked(*v, base); \ 3001da177e4SLinus Torvalds } \ 3011da177e4SLinus Torvalds u; \ 3021da177e4SLinus Torvalds }) 3031da177e4SLinus Torvalds 304aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. 3051da177e4SLinus Torvalds * Variable names are the proof of operation correctness. 306aa1039e7SEric Dumazet * Look into mm/map_avl.c for more detail description of the ideas. 307aa1039e7SEric Dumazet */ 308b914c4eaSEric Dumazet static void peer_avl_rebalance(struct inet_peer __rcu **stack[], 30998158f5aSDavid S. Miller struct inet_peer __rcu ***stackend, 31098158f5aSDavid S. Miller struct inet_peer_base *base) 3111da177e4SLinus Torvalds { 312b914c4eaSEric Dumazet struct inet_peer __rcu **nodep; 313b914c4eaSEric Dumazet struct inet_peer *node, *l, *r; 3141da177e4SLinus Torvalds int lh, rh; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds while (stackend > stack) { 3171da177e4SLinus Torvalds nodep = *--stackend; 31865e8354eSEric Dumazet node = rcu_deref_locked(*nodep, base); 31965e8354eSEric Dumazet l = rcu_deref_locked(node->avl_left, base); 32065e8354eSEric Dumazet r = rcu_deref_locked(node->avl_right, base); 3211da177e4SLinus Torvalds lh = node_height(l); 3221da177e4SLinus Torvalds rh = node_height(r); 3231da177e4SLinus Torvalds if (lh > rh + 1) { /* l: RH+2 */ 3241da177e4SLinus Torvalds struct inet_peer *ll, *lr, *lrl, *lrr; 3251da177e4SLinus Torvalds int lrh; 32665e8354eSEric Dumazet ll = rcu_deref_locked(l->avl_left, base); 32765e8354eSEric Dumazet lr = rcu_deref_locked(l->avl_right, base); 3281da177e4SLinus Torvalds lrh = node_height(lr); 3291da177e4SLinus Torvalds if (lrh <= node_height(ll)) { /* ll: RH+1 */ 330b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_left, lr); /* lr: RH or RH+1 */ 331b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_right, r); /* r: RH */ 3321da177e4SLinus Torvalds node->avl_height = lrh + 1; /* RH+1 or RH+2 */ 333b914c4eaSEric Dumazet RCU_INIT_POINTER(l->avl_left, ll); /* ll: RH+1 */ 334b914c4eaSEric Dumazet RCU_INIT_POINTER(l->avl_right, node); /* node: RH+1 or RH+2 */ 3351da177e4SLinus Torvalds l->avl_height = node->avl_height + 1; 336b914c4eaSEric Dumazet RCU_INIT_POINTER(*nodep, l); 3371da177e4SLinus Torvalds } else { /* ll: RH, lr: RH+1 */ 33865e8354eSEric Dumazet lrl = rcu_deref_locked(lr->avl_left, base);/* lrl: RH or RH-1 */ 33965e8354eSEric Dumazet lrr = rcu_deref_locked(lr->avl_right, base);/* lrr: RH or RH-1 */ 340b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_left, lrr); /* lrr: RH or RH-1 */ 341b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_right, r); /* r: RH */ 3421da177e4SLinus Torvalds node->avl_height = rh + 1; /* node: RH+1 */ 343b914c4eaSEric Dumazet RCU_INIT_POINTER(l->avl_left, ll); /* ll: RH */ 344b914c4eaSEric Dumazet RCU_INIT_POINTER(l->avl_right, lrl); /* lrl: RH or RH-1 */ 3451da177e4SLinus Torvalds l->avl_height = rh + 1; /* l: RH+1 */ 346b914c4eaSEric Dumazet RCU_INIT_POINTER(lr->avl_left, l); /* l: RH+1 */ 347b914c4eaSEric Dumazet RCU_INIT_POINTER(lr->avl_right, node); /* node: RH+1 */ 3481da177e4SLinus Torvalds lr->avl_height = rh + 2; 349b914c4eaSEric Dumazet RCU_INIT_POINTER(*nodep, lr); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds } else if (rh > lh + 1) { /* r: LH+2 */ 3521da177e4SLinus Torvalds struct inet_peer *rr, *rl, *rlr, *rll; 3531da177e4SLinus Torvalds int rlh; 35465e8354eSEric Dumazet rr = rcu_deref_locked(r->avl_right, base); 35565e8354eSEric Dumazet rl = rcu_deref_locked(r->avl_left, base); 3561da177e4SLinus Torvalds rlh = node_height(rl); 3571da177e4SLinus Torvalds if (rlh <= node_height(rr)) { /* rr: LH+1 */ 358b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_right, rl); /* rl: LH or LH+1 */ 359b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_left, l); /* l: LH */ 3601da177e4SLinus Torvalds node->avl_height = rlh + 1; /* LH+1 or LH+2 */ 361b914c4eaSEric Dumazet RCU_INIT_POINTER(r->avl_right, rr); /* rr: LH+1 */ 362b914c4eaSEric Dumazet RCU_INIT_POINTER(r->avl_left, node); /* node: LH+1 or LH+2 */ 3631da177e4SLinus Torvalds r->avl_height = node->avl_height + 1; 364b914c4eaSEric Dumazet RCU_INIT_POINTER(*nodep, r); 3651da177e4SLinus Torvalds } else { /* rr: RH, rl: RH+1 */ 36665e8354eSEric Dumazet rlr = rcu_deref_locked(rl->avl_right, base);/* rlr: LH or LH-1 */ 36765e8354eSEric Dumazet rll = rcu_deref_locked(rl->avl_left, base);/* rll: LH or LH-1 */ 368b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_right, rll); /* rll: LH or LH-1 */ 369b914c4eaSEric Dumazet RCU_INIT_POINTER(node->avl_left, l); /* l: LH */ 3701da177e4SLinus Torvalds node->avl_height = lh + 1; /* node: LH+1 */ 371b914c4eaSEric Dumazet RCU_INIT_POINTER(r->avl_right, rr); /* rr: LH */ 372b914c4eaSEric Dumazet RCU_INIT_POINTER(r->avl_left, rlr); /* rlr: LH or LH-1 */ 3731da177e4SLinus Torvalds r->avl_height = lh + 1; /* r: LH+1 */ 374b914c4eaSEric Dumazet RCU_INIT_POINTER(rl->avl_right, r); /* r: LH+1 */ 375b914c4eaSEric Dumazet RCU_INIT_POINTER(rl->avl_left, node); /* node: LH+1 */ 3761da177e4SLinus Torvalds rl->avl_height = lh + 2; 377b914c4eaSEric Dumazet RCU_INIT_POINTER(*nodep, rl); 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds } else { 3801da177e4SLinus Torvalds node->avl_height = (lh > rh ? lh : rh) + 1; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 385aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */ 38698158f5aSDavid S. Miller #define link_to_pool(n, base) \ 3871da177e4SLinus Torvalds do { \ 3881da177e4SLinus Torvalds n->avl_height = 1; \ 389b914c4eaSEric Dumazet n->avl_left = peer_avl_empty_rcu; \ 390b914c4eaSEric Dumazet n->avl_right = peer_avl_empty_rcu; \ 391b914c4eaSEric Dumazet /* lockless readers can catch us now */ \ 392b914c4eaSEric Dumazet rcu_assign_pointer(**--stackptr, n); \ 39398158f5aSDavid S. Miller peer_avl_rebalance(stack, stackptr, base); \ 3941da177e4SLinus Torvalds } while (0) 3951da177e4SLinus Torvalds 396aa1039e7SEric Dumazet static void inetpeer_free_rcu(struct rcu_head *head) 397aa1039e7SEric Dumazet { 398aa1039e7SEric Dumazet kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu)); 399aa1039e7SEric Dumazet } 400aa1039e7SEric Dumazet 40166944e1cSEric Dumazet static void unlink_from_pool(struct inet_peer *p, struct inet_peer_base *base, 40266944e1cSEric Dumazet struct inet_peer __rcu **stack[PEER_MAXDEPTH]) 4031da177e4SLinus Torvalds { 404b914c4eaSEric Dumazet struct inet_peer __rcu ***stackptr, ***delp; 4054b9d9be8SEric Dumazet 40602663045SDavid S. Miller if (lookup(&p->daddr, stack, base) != p) 4071da177e4SLinus Torvalds BUG(); 4081da177e4SLinus Torvalds delp = stackptr - 1; /* *delp[0] == p */ 409b914c4eaSEric Dumazet if (p->avl_left == peer_avl_empty_rcu) { 4101da177e4SLinus Torvalds *delp[0] = p->avl_right; 4111da177e4SLinus Torvalds --stackptr; 4121da177e4SLinus Torvalds } else { 4131da177e4SLinus Torvalds /* look for a node to insert instead of p */ 4141da177e4SLinus Torvalds struct inet_peer *t; 41598158f5aSDavid S. Miller t = lookup_rightempty(p, base); 41665e8354eSEric Dumazet BUG_ON(rcu_deref_locked(*stackptr[-1], base) != t); 4171da177e4SLinus Torvalds **--stackptr = t->avl_left; 418582a72daSDavid S. Miller /* t is removed, t->daddr > x->daddr for any 4191da177e4SLinus Torvalds * x in p->avl_left subtree. 4201da177e4SLinus Torvalds * Put t in the old place of p. */ 421b914c4eaSEric Dumazet RCU_INIT_POINTER(*delp[0], t); 4221da177e4SLinus Torvalds t->avl_left = p->avl_left; 4231da177e4SLinus Torvalds t->avl_right = p->avl_right; 4241da177e4SLinus Torvalds t->avl_height = p->avl_height; 42509a62660SKris Katterjohn BUG_ON(delp[1] != &p->avl_left); 4261da177e4SLinus Torvalds delp[1] = &t->avl_left; /* was &p->avl_left */ 4271da177e4SLinus Torvalds } 42898158f5aSDavid S. Miller peer_avl_rebalance(stack, stackptr, base); 42998158f5aSDavid S. Miller base->total--; 4304e75db2eSEric Dumazet call_rcu(&p->rcu, inetpeer_free_rcu); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 433*c8a627edSGao feng static struct inet_peer_base *family_to_base(struct net *net, 434*c8a627edSGao feng int family) 435021e9299SDavid S. Miller { 436*c8a627edSGao feng return family == AF_INET ? net->ipv4.peers : net->ipv6.peers; 437021e9299SDavid S. Miller } 438021e9299SDavid S. Miller 4394b9d9be8SEric Dumazet /* perform garbage collect on all items stacked during a lookup */ 4404b9d9be8SEric Dumazet static int inet_peer_gc(struct inet_peer_base *base, 4414b9d9be8SEric Dumazet struct inet_peer __rcu **stack[PEER_MAXDEPTH], 4424b9d9be8SEric Dumazet struct inet_peer __rcu ***stackptr) 44398158f5aSDavid S. Miller { 4444b9d9be8SEric Dumazet struct inet_peer *p, *gchead = NULL; 4454b9d9be8SEric Dumazet __u32 delta, ttl; 4464b9d9be8SEric Dumazet int cnt = 0; 44798158f5aSDavid S. Miller 4484b9d9be8SEric Dumazet if (base->total >= inet_peer_threshold) 4494b9d9be8SEric Dumazet ttl = 0; /* be aggressive */ 4504b9d9be8SEric Dumazet else 4514b9d9be8SEric Dumazet ttl = inet_peer_maxttl 4524b9d9be8SEric Dumazet - (inet_peer_maxttl - inet_peer_minttl) / HZ * 4534b9d9be8SEric Dumazet base->total / inet_peer_threshold * HZ; 4544b9d9be8SEric Dumazet stackptr--; /* last stack slot is peer_avl_empty */ 4554b9d9be8SEric Dumazet while (stackptr > stack) { 4564b9d9be8SEric Dumazet stackptr--; 4574b9d9be8SEric Dumazet p = rcu_deref_locked(**stackptr, base); 4586d1a3e04SEric Dumazet if (atomic_read(&p->refcnt) == 0) { 4596d1a3e04SEric Dumazet smp_rmb(); 460d71209deSPavel Emelyanov delta = (__u32)jiffies - p->dtime; 4616d1a3e04SEric Dumazet if (delta >= ttl && 4624b9d9be8SEric Dumazet atomic_cmpxchg(&p->refcnt, 0, -1) == 0) { 4634b9d9be8SEric Dumazet p->gc_next = gchead; 4644b9d9be8SEric Dumazet gchead = p; 4654b9d9be8SEric Dumazet } 4664b9d9be8SEric Dumazet } 4676d1a3e04SEric Dumazet } 4684b9d9be8SEric Dumazet while ((p = gchead) != NULL) { 4694b9d9be8SEric Dumazet gchead = p->gc_next; 4704b9d9be8SEric Dumazet cnt++; 4714b9d9be8SEric Dumazet unlink_from_pool(p, base, stack); 4724b9d9be8SEric Dumazet } 4734b9d9be8SEric Dumazet return cnt; 4741da177e4SLinus Torvalds } 475d71209deSPavel Emelyanov 476*c8a627edSGao feng struct inet_peer *inet_getpeer(struct net *net, 477*c8a627edSGao feng const struct inetpeer_addr *daddr, 478*c8a627edSGao feng int create) 4791da177e4SLinus Torvalds { 480b914c4eaSEric Dumazet struct inet_peer __rcu **stack[PEER_MAXDEPTH], ***stackptr; 481*c8a627edSGao feng struct inet_peer_base *base = family_to_base(net, daddr->family); 48298158f5aSDavid S. Miller struct inet_peer *p; 48365e8354eSEric Dumazet unsigned int sequence; 4844b9d9be8SEric Dumazet int invalidated, gccnt = 0; 4851da177e4SLinus Torvalds 4864b9d9be8SEric Dumazet /* Attempt a lockless lookup first. 487aa1039e7SEric Dumazet * Because of a concurrent writer, we might not find an existing entry. 488aa1039e7SEric Dumazet */ 4897b46ac4eSDavid S. Miller rcu_read_lock(); 49065e8354eSEric Dumazet sequence = read_seqbegin(&base->lock); 4914b9d9be8SEric Dumazet p = lookup_rcu(daddr, base); 49265e8354eSEric Dumazet invalidated = read_seqretry(&base->lock, sequence); 4937b46ac4eSDavid S. Miller rcu_read_unlock(); 4941da177e4SLinus Torvalds 4954b9d9be8SEric Dumazet if (p) 4961da177e4SLinus Torvalds return p; 4971da177e4SLinus Torvalds 49865e8354eSEric Dumazet /* If no writer did a change during our lookup, we can return early. */ 49965e8354eSEric Dumazet if (!create && !invalidated) 50065e8354eSEric Dumazet return NULL; 50165e8354eSEric Dumazet 502aa1039e7SEric Dumazet /* retry an exact lookup, taking the lock before. 503aa1039e7SEric Dumazet * At least, nodes should be hot in our cache. 504aa1039e7SEric Dumazet */ 50565e8354eSEric Dumazet write_seqlock_bh(&base->lock); 5064b9d9be8SEric Dumazet relookup: 50702663045SDavid S. Miller p = lookup(daddr, stack, base); 508aa1039e7SEric Dumazet if (p != peer_avl_empty) { 5094b9d9be8SEric Dumazet atomic_inc(&p->refcnt); 51065e8354eSEric Dumazet write_sequnlock_bh(&base->lock); 5114b9d9be8SEric Dumazet return p; 5124b9d9be8SEric Dumazet } 5134b9d9be8SEric Dumazet if (!gccnt) { 5144b9d9be8SEric Dumazet gccnt = inet_peer_gc(base, stack, stackptr); 5154b9d9be8SEric Dumazet if (gccnt && create) 5164b9d9be8SEric Dumazet goto relookup; 517aa1039e7SEric Dumazet } 518aa1039e7SEric Dumazet p = create ? kmem_cache_alloc(peer_cachep, GFP_ATOMIC) : NULL; 519aa1039e7SEric Dumazet if (p) { 520b534ecf1SDavid S. Miller p->daddr = *daddr; 521aa1039e7SEric Dumazet atomic_set(&p->refcnt, 1); 522aa1039e7SEric Dumazet atomic_set(&p->rid, 0); 52387c48fa3SEric Dumazet atomic_set(&p->ip_id_count, 52487c48fa3SEric Dumazet (daddr->family == AF_INET) ? 52587c48fa3SEric Dumazet secure_ip_id(daddr->addr.a4) : 52687c48fa3SEric Dumazet secure_ipv6_id(daddr->addr.a6)); 527aa1039e7SEric Dumazet p->tcp_ts_stamp = 0; 528144001bdSDavid S. Miller p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW; 52992d86829SDavid S. Miller p->rate_tokens = 0; 53092d86829SDavid S. Miller p->rate_last = 0; 531ddd4aa42SDavid S. Miller p->pmtu_expires = 0; 53246af3180SHiroaki SHIMODA p->pmtu_orig = 0; 533ddd4aa42SDavid S. Miller memset(&p->redirect_learned, 0, sizeof(p->redirect_learned)); 5345faa5df1SSteffen Klassert INIT_LIST_HEAD(&p->gc_list); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* Link the node. */ 53798158f5aSDavid S. Miller link_to_pool(p, base); 53898158f5aSDavid S. Miller base->total++; 539aa1039e7SEric Dumazet } 54065e8354eSEric Dumazet write_sequnlock_bh(&base->lock); 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds return p; 5431da177e4SLinus Torvalds } 544b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_getpeer); 54598158f5aSDavid S. Miller 5464663afe2SEric Dumazet void inet_putpeer(struct inet_peer *p) 5474663afe2SEric Dumazet { 5484663afe2SEric Dumazet p->dtime = (__u32)jiffies; 5496d1a3e04SEric Dumazet smp_mb__before_atomic_dec(); 5504b9d9be8SEric Dumazet atomic_dec(&p->refcnt); 5514663afe2SEric Dumazet } 552b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_putpeer); 55392d86829SDavid S. Miller 55492d86829SDavid S. Miller /* 55592d86829SDavid S. Miller * Check transmit rate limitation for given message. 55692d86829SDavid S. Miller * The rate information is held in the inet_peer entries now. 55792d86829SDavid S. Miller * This function is generic and could be used for other purposes 55892d86829SDavid S. Miller * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov. 55992d86829SDavid S. Miller * 56092d86829SDavid S. Miller * Note that the same inet_peer fields are modified by functions in 56192d86829SDavid S. Miller * route.c too, but these work for packet destinations while xrlim_allow 56292d86829SDavid S. Miller * works for icmp destinations. This means the rate limiting information 56392d86829SDavid S. Miller * for one "ip object" is shared - and these ICMPs are twice limited: 56492d86829SDavid S. Miller * by source and by destination. 56592d86829SDavid S. Miller * 56692d86829SDavid S. Miller * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate 56792d86829SDavid S. Miller * SHOULD allow setting of rate limits 56892d86829SDavid S. Miller * 56992d86829SDavid S. Miller * Shared between ICMPv4 and ICMPv6. 57092d86829SDavid S. Miller */ 57192d86829SDavid S. Miller #define XRLIM_BURST_FACTOR 6 57292d86829SDavid S. Miller bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout) 57392d86829SDavid S. Miller { 57492d86829SDavid S. Miller unsigned long now, token; 57592d86829SDavid S. Miller bool rc = false; 57692d86829SDavid S. Miller 57792d86829SDavid S. Miller if (!peer) 57892d86829SDavid S. Miller return true; 57992d86829SDavid S. Miller 58092d86829SDavid S. Miller token = peer->rate_tokens; 58192d86829SDavid S. Miller now = jiffies; 58292d86829SDavid S. Miller token += now - peer->rate_last; 58392d86829SDavid S. Miller peer->rate_last = now; 58492d86829SDavid S. Miller if (token > XRLIM_BURST_FACTOR * timeout) 58592d86829SDavid S. Miller token = XRLIM_BURST_FACTOR * timeout; 58692d86829SDavid S. Miller if (token >= timeout) { 58792d86829SDavid S. Miller token -= timeout; 58892d86829SDavid S. Miller rc = true; 58992d86829SDavid S. Miller } 59092d86829SDavid S. Miller peer->rate_tokens = token; 59192d86829SDavid S. Miller return rc; 59292d86829SDavid S. Miller } 59392d86829SDavid S. Miller EXPORT_SYMBOL(inet_peer_xrlim_allow); 5945faa5df1SSteffen Klassert 59555432d2bSEric Dumazet static void inetpeer_inval_rcu(struct rcu_head *head) 59655432d2bSEric Dumazet { 59755432d2bSEric Dumazet struct inet_peer *p = container_of(head, struct inet_peer, gc_rcu); 59855432d2bSEric Dumazet 59955432d2bSEric Dumazet spin_lock_bh(&gc_lock); 60055432d2bSEric Dumazet list_add_tail(&p->gc_list, &gc_list); 60155432d2bSEric Dumazet spin_unlock_bh(&gc_lock); 60255432d2bSEric Dumazet 60355432d2bSEric Dumazet schedule_delayed_work(&gc_work, gc_delay); 60455432d2bSEric Dumazet } 60555432d2bSEric Dumazet 606*c8a627edSGao feng void inetpeer_invalidate_tree(struct net *net, int family) 6075faa5df1SSteffen Klassert { 6085faa5df1SSteffen Klassert struct inet_peer *old, *new, *prev; 609*c8a627edSGao feng struct inet_peer_base *base = family_to_base(net, family); 6105faa5df1SSteffen Klassert 6115faa5df1SSteffen Klassert write_seqlock_bh(&base->lock); 6125faa5df1SSteffen Klassert 6135faa5df1SSteffen Klassert old = base->root; 6145faa5df1SSteffen Klassert if (old == peer_avl_empty_rcu) 6155faa5df1SSteffen Klassert goto out; 6165faa5df1SSteffen Klassert 6175faa5df1SSteffen Klassert new = peer_avl_empty_rcu; 6185faa5df1SSteffen Klassert 6195faa5df1SSteffen Klassert prev = cmpxchg(&base->root, old, new); 6205faa5df1SSteffen Klassert if (prev == old) { 6215faa5df1SSteffen Klassert base->total = 0; 62255432d2bSEric Dumazet call_rcu(&prev->gc_rcu, inetpeer_inval_rcu); 6235faa5df1SSteffen Klassert } 6245faa5df1SSteffen Klassert 6255faa5df1SSteffen Klassert out: 6265faa5df1SSteffen Klassert write_sequnlock_bh(&base->lock); 6275faa5df1SSteffen Klassert } 6285faa5df1SSteffen Klassert EXPORT_SYMBOL(inetpeer_invalidate_tree); 629