11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Generic address resolution entity 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: 51da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 61da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Fixes: 141da177e4SLinus Torvalds * Vitaly E. Lavrov releasing NULL neighbor in neigh_add. 151da177e4SLinus Torvalds * Harald Welte Add neighbour cache statistics like rtstat 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 185a0e3ad6STejun Heo #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/types.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/socket.h> 231da177e4SLinus Torvalds #include <linux/netdevice.h> 241da177e4SLinus Torvalds #include <linux/proc_fs.h> 251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 261da177e4SLinus Torvalds #include <linux/sysctl.h> 271da177e4SLinus Torvalds #endif 281da177e4SLinus Torvalds #include <linux/times.h> 29457c4cbcSEric W. Biederman #include <net/net_namespace.h> 301da177e4SLinus Torvalds #include <net/neighbour.h> 311da177e4SLinus Torvalds #include <net/dst.h> 321da177e4SLinus Torvalds #include <net/sock.h> 338d71740cSTom Tucker #include <net/netevent.h> 34a14a49d2SThomas Graf #include <net/netlink.h> 351da177e4SLinus Torvalds #include <linux/rtnetlink.h> 361da177e4SLinus Torvalds #include <linux/random.h> 37543537bdSPaulo Marques #include <linux/string.h> 38c3609d51Svignesh babu #include <linux/log2.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define NEIGH_DEBUG 1 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define NEIGH_PRINTK(x...) printk(x) 431da177e4SLinus Torvalds #define NEIGH_NOPRINTK(x...) do { ; } while(0) 441da177e4SLinus Torvalds #define NEIGH_PRINTK0 NEIGH_PRINTK 451da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_NOPRINTK 461da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_NOPRINTK 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #if NEIGH_DEBUG >= 1 491da177e4SLinus Torvalds #undef NEIGH_PRINTK1 501da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_PRINTK 511da177e4SLinus Torvalds #endif 521da177e4SLinus Torvalds #if NEIGH_DEBUG >= 2 531da177e4SLinus Torvalds #undef NEIGH_PRINTK2 541da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_PRINTK 551da177e4SLinus Torvalds #endif 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds #define PNEIGH_HASHMASK 0xF 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg); 60d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags); 61d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh); 621da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds static struct neigh_table *neigh_tables; 6545fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS 669a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops; 6745fc3b11SAmos Waterland #endif 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* 701da177e4SLinus Torvalds Neighbour hash table buckets are protected with rwlock tbl->lock. 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds - All the scans/updates to hash buckets MUST be made under this lock. 731da177e4SLinus Torvalds - NOTHING clever should be made under this lock: no callbacks 741da177e4SLinus Torvalds to protocol backends, no attempts to send something to network. 751da177e4SLinus Torvalds It will result in deadlocks, if backend/driver wants to use neighbour 761da177e4SLinus Torvalds cache. 771da177e4SLinus Torvalds - If the entry requires some non-trivial actions, increase 781da177e4SLinus Torvalds its reference count and release table lock. 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds Neighbour entries are protected: 811da177e4SLinus Torvalds - with reference count. 821da177e4SLinus Torvalds - with rwlock neigh->lock 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds Reference count prevents destruction. 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds neigh->lock mainly serializes ll address data and its validity state. 871da177e4SLinus Torvalds However, the same lock is used to protect another entry fields: 881da177e4SLinus Torvalds - timer 891da177e4SLinus Torvalds - resolution queue 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds Again, nothing clever shall be made under neigh->lock, 921da177e4SLinus Torvalds the most complicated procedure, which we allow is dev->hard_header. 931da177e4SLinus Torvalds It is supposed, that dev->hard_header is simplistic and does 941da177e4SLinus Torvalds not make callbacks to neighbour tables. 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds The last lock is neigh_tbl_lock. It is pure SMP lock, protecting 971da177e4SLinus Torvalds list of neighbour tables. This list is used only in process context, 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static int neigh_blackhole(struct sk_buff *skb) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds kfree_skb(skb); 1051da177e4SLinus Torvalds return -ENETDOWN; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1084f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh) 1094f494554SThomas Graf { 1104f494554SThomas Graf if (neigh->parms->neigh_cleanup) 1114f494554SThomas Graf neigh->parms->neigh_cleanup(neigh); 1124f494554SThomas Graf 113d961db35SThomas Graf __neigh_notify(neigh, RTM_DELNEIGH, 0); 1144f494554SThomas Graf neigh_release(neigh); 1154f494554SThomas Graf } 1164f494554SThomas Graf 1171da177e4SLinus Torvalds /* 1181da177e4SLinus Torvalds * It is random distribution in the interval (1/2)*base...(3/2)*base. 1191da177e4SLinus Torvalds * It corresponds to default IPv6 settings and is not overridable, 1201da177e4SLinus Torvalds * because it is really reasonable choice. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base) 1241da177e4SLinus Torvalds { 125a02cec21SEric Dumazet return base ? (net_random() % base) + (base >> 1) : 0; 1261da177e4SLinus Torvalds } 1270a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds int shrunk = 0; 1331da177e4SLinus Torvalds int i; 134d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 139d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 140d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 141d6bf7817SEric Dumazet for (i = 0; i <= nht->hash_mask; i++) { 1421da177e4SLinus Torvalds struct neighbour *n, **np; 1431da177e4SLinus Torvalds 144d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 1451da177e4SLinus Torvalds while ((n = *np) != NULL) { 1461da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1471da177e4SLinus Torvalds * - nobody refers to it. 1481da177e4SLinus Torvalds * - it is not permanent 1491da177e4SLinus Torvalds */ 1501da177e4SLinus Torvalds write_lock(&n->lock); 1511da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1521da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 1531da177e4SLinus Torvalds *np = n->next; 1541da177e4SLinus Torvalds n->dead = 1; 1551da177e4SLinus Torvalds shrunk = 1; 1561da177e4SLinus Torvalds write_unlock(&n->lock); 1574f494554SThomas Graf neigh_cleanup_and_release(n); 1581da177e4SLinus Torvalds continue; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds write_unlock(&n->lock); 1611da177e4SLinus Torvalds np = &n->next; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds tbl->last_flush = jiffies; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds return shrunk; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 172a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 173a43d8994SPavel Emelyanov { 174a43d8994SPavel Emelyanov neigh_hold(n); 175a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 176a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 177a43d8994SPavel Emelyanov n->nud_state); 178a43d8994SPavel Emelyanov dump_stack(); 179a43d8994SPavel Emelyanov } 180a43d8994SPavel Emelyanov } 181a43d8994SPavel Emelyanov 1821da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1851da177e4SLinus Torvalds del_timer(&n->timer)) { 1861da177e4SLinus Torvalds neigh_release(n); 1871da177e4SLinus Torvalds return 1; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds return 0; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct sk_buff *skb; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 1971da177e4SLinus Torvalds dev_put(skb->dev); 1981da177e4SLinus Torvalds kfree_skb(skb); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 20249636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds int i; 205d6bf7817SEric Dumazet struct neigh_hash_table *nht; 2061da177e4SLinus Torvalds 207d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 208d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 209d6bf7817SEric Dumazet 210d6bf7817SEric Dumazet for (i = 0; i <= nht->hash_mask; i++) { 211d6bf7817SEric Dumazet struct neighbour *n, **np = &nht->hash_buckets[i]; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds while ((n = *np) != NULL) { 2141da177e4SLinus Torvalds if (dev && n->dev != dev) { 2151da177e4SLinus Torvalds np = &n->next; 2161da177e4SLinus Torvalds continue; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds *np = n->next; 2191da177e4SLinus Torvalds write_lock(&n->lock); 2201da177e4SLinus Torvalds neigh_del_timer(n); 2211da177e4SLinus Torvalds n->dead = 1; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2241da177e4SLinus Torvalds /* The most unpleasant situation. 2251da177e4SLinus Torvalds We must destroy neighbour entry, 2261da177e4SLinus Torvalds but someone still uses it. 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds The destroy will be delayed until 2291da177e4SLinus Torvalds the last user releases us, but 2301da177e4SLinus Torvalds we must kill timers etc. and move 2311da177e4SLinus Torvalds it to safe state. 2321da177e4SLinus Torvalds */ 2331da177e4SLinus Torvalds skb_queue_purge(&n->arp_queue); 2341da177e4SLinus Torvalds n->output = neigh_blackhole; 2351da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2361da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2371da177e4SLinus Torvalds else 2381da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2391da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is stray.\n", n); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds write_unlock(&n->lock); 2424f494554SThomas Graf neigh_cleanup_and_release(n); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds } 24549636bb1SHerbert Xu } 2461da177e4SLinus Torvalds 24749636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 24849636bb1SHerbert Xu { 24949636bb1SHerbert Xu write_lock_bh(&tbl->lock); 25049636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 25149636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 25249636bb1SHerbert Xu } 2530a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr); 25449636bb1SHerbert Xu 25549636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 25649636bb1SHerbert Xu { 25749636bb1SHerbert Xu write_lock_bh(&tbl->lock); 25849636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2591da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2601da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2631da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2641da177e4SLinus Torvalds return 0; 2651da177e4SLinus Torvalds } 2660a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds static struct neighbour *neigh_alloc(struct neigh_table *tbl) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds struct neighbour *n = NULL; 2711da177e4SLinus Torvalds unsigned long now = jiffies; 2721da177e4SLinus Torvalds int entries; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2751da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2761da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2771da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2781da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2791da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2801da177e4SLinus Torvalds goto out_entries; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283c3762229SRobert P. J. Day n = kmem_cache_zalloc(tbl->kmem_cachep, GFP_ATOMIC); 2841da177e4SLinus Torvalds if (!n) 2851da177e4SLinus Torvalds goto out_entries; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds skb_queue_head_init(&n->arp_queue); 2881da177e4SLinus Torvalds rwlock_init(&n->lock); 2891da177e4SLinus Torvalds n->updated = n->used = now; 2901da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2911da177e4SLinus Torvalds n->output = neigh_blackhole; 2921da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 293b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 2961da177e4SLinus Torvalds n->tbl = tbl; 2971da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 2981da177e4SLinus Torvalds n->dead = 1; 2991da177e4SLinus Torvalds out: 3001da177e4SLinus Torvalds return n; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds out_entries: 3031da177e4SLinus Torvalds atomic_dec(&tbl->entries); 3041da177e4SLinus Torvalds goto out; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 307d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_alloc(unsigned int entries) 3081da177e4SLinus Torvalds { 309d6bf7817SEric Dumazet size_t size = entries * sizeof(struct neighbour *); 310d6bf7817SEric Dumazet struct neigh_hash_table *ret; 311d6bf7817SEric Dumazet struct neighbour **buckets; 3121da177e4SLinus Torvalds 313d6bf7817SEric Dumazet ret = kmalloc(sizeof(*ret), GFP_ATOMIC); 314d6bf7817SEric Dumazet if (!ret) 315d6bf7817SEric Dumazet return NULL; 316d6bf7817SEric Dumazet if (size <= PAGE_SIZE) 317d6bf7817SEric Dumazet buckets = kzalloc(size, GFP_ATOMIC); 318d6bf7817SEric Dumazet else 319d6bf7817SEric Dumazet buckets = (struct neighbour **) 320d6bf7817SEric Dumazet __get_free_pages(GFP_ATOMIC | __GFP_ZERO, 321d6bf7817SEric Dumazet get_order(size)); 322d6bf7817SEric Dumazet if (!buckets) { 323d6bf7817SEric Dumazet kfree(ret); 324d6bf7817SEric Dumazet return NULL; 3251da177e4SLinus Torvalds } 326d6bf7817SEric Dumazet ret->hash_buckets = buckets; 327d6bf7817SEric Dumazet ret->hash_mask = entries - 1; 328d6bf7817SEric Dumazet get_random_bytes(&ret->hash_rnd, sizeof(ret->hash_rnd)); 3291da177e4SLinus Torvalds return ret; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 332d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head) 3331da177e4SLinus Torvalds { 334d6bf7817SEric Dumazet struct neigh_hash_table *nht = container_of(head, 335d6bf7817SEric Dumazet struct neigh_hash_table, 336d6bf7817SEric Dumazet rcu); 337d6bf7817SEric Dumazet size_t size = (nht->hash_mask + 1) * sizeof(struct neighbour *); 338d6bf7817SEric Dumazet struct neighbour **buckets = nht->hash_buckets; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds if (size <= PAGE_SIZE) 341d6bf7817SEric Dumazet kfree(buckets); 3421da177e4SLinus Torvalds else 343d6bf7817SEric Dumazet free_pages((unsigned long)buckets, get_order(size)); 344d6bf7817SEric Dumazet kfree(nht); 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 347d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, 348d6bf7817SEric Dumazet unsigned long new_entries) 3491da177e4SLinus Torvalds { 350d6bf7817SEric Dumazet unsigned int i, hash; 351d6bf7817SEric Dumazet struct neigh_hash_table *new_nht, *old_nht; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3541da177e4SLinus Torvalds 355c3609d51Svignesh babu BUG_ON(!is_power_of_2(new_entries)); 356d6bf7817SEric Dumazet old_nht = rcu_dereference_protected(tbl->nht, 357d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 358d6bf7817SEric Dumazet new_nht = neigh_hash_alloc(new_entries); 359d6bf7817SEric Dumazet if (!new_nht) 360d6bf7817SEric Dumazet return old_nht; 3611da177e4SLinus Torvalds 362d6bf7817SEric Dumazet for (i = 0; i <= old_nht->hash_mask; i++) { 3631da177e4SLinus Torvalds struct neighbour *n, *next; 3641da177e4SLinus Torvalds 365d6bf7817SEric Dumazet for (n = old_nht->hash_buckets[i]; 366d6bf7817SEric Dumazet n != NULL; 367d6bf7817SEric Dumazet n = next) { 368d6bf7817SEric Dumazet hash = tbl->hash(n->primary_key, n->dev, 369d6bf7817SEric Dumazet new_nht->hash_rnd); 3701da177e4SLinus Torvalds 371d6bf7817SEric Dumazet hash &= new_nht->hash_mask; 3721da177e4SLinus Torvalds next = n->next; 3731da177e4SLinus Torvalds 374d6bf7817SEric Dumazet n->next = new_nht->hash_buckets[hash]; 375d6bf7817SEric Dumazet new_nht->hash_buckets[hash] = n; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 379d6bf7817SEric Dumazet rcu_assign_pointer(tbl->nht, new_nht); 380d6bf7817SEric Dumazet call_rcu(&old_nht->rcu, neigh_hash_free_rcu); 381d6bf7817SEric Dumazet return new_nht; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 3851da177e4SLinus Torvalds struct net_device *dev) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds struct neighbour *n; 3881da177e4SLinus Torvalds int key_len = tbl->key_len; 389bc4bf5f3SPavel Emelyanov u32 hash_val; 390d6bf7817SEric Dumazet struct neigh_hash_table *nht; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 3931da177e4SLinus Torvalds 394d6bf7817SEric Dumazet rcu_read_lock_bh(); 395d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 396d6bf7817SEric Dumazet hash_val = tbl->hash(pkey, dev, nht->hash_rnd) & nht->hash_mask; 397d6bf7817SEric Dumazet read_lock(&tbl->lock); 398d6bf7817SEric Dumazet for (n = nht->hash_buckets[hash_val]; n; n = n->next) { 3991da177e4SLinus Torvalds if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) { 4001da177e4SLinus Torvalds neigh_hold(n); 4011da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4021da177e4SLinus Torvalds break; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds } 405d6bf7817SEric Dumazet read_unlock(&tbl->lock); 406d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4071da177e4SLinus Torvalds return n; 4081da177e4SLinus Torvalds } 4090a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup); 4101da177e4SLinus Torvalds 411426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 412426b5303SEric W. Biederman const void *pkey) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds struct neighbour *n; 4151da177e4SLinus Torvalds int key_len = tbl->key_len; 416bc4bf5f3SPavel Emelyanov u32 hash_val; 417d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4201da177e4SLinus Torvalds 421d6bf7817SEric Dumazet rcu_read_lock_bh(); 422d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 423d6bf7817SEric Dumazet hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) & nht->hash_mask; 424d6bf7817SEric Dumazet read_lock(&tbl->lock); 425d6bf7817SEric Dumazet for (n = nht->hash_buckets[hash_val]; n; n = n->next) { 426426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 427878628fbSYOSHIFUJI Hideaki net_eq(dev_net(n->dev), net)) { 4281da177e4SLinus Torvalds neigh_hold(n); 4291da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4301da177e4SLinus Torvalds break; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds } 433d6bf7817SEric Dumazet read_unlock(&tbl->lock); 434d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4351da177e4SLinus Torvalds return n; 4361da177e4SLinus Torvalds } 4370a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey, 4401da177e4SLinus Torvalds struct net_device *dev) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds u32 hash_val; 4431da177e4SLinus Torvalds int key_len = tbl->key_len; 4441da177e4SLinus Torvalds int error; 4451da177e4SLinus Torvalds struct neighbour *n1, *rc, *n = neigh_alloc(tbl); 446d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (!n) { 4491da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4501da177e4SLinus Torvalds goto out; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4541da177e4SLinus Torvalds n->dev = dev; 4551da177e4SLinus Torvalds dev_hold(dev); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* Protocol specific setup. */ 4581da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4591da177e4SLinus Torvalds rc = ERR_PTR(error); 4601da177e4SLinus Torvalds goto out_neigh_release; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /* Device specific setup. */ 4641da177e4SLinus Torvalds if (n->parms->neigh_setup && 4651da177e4SLinus Torvalds (error = n->parms->neigh_setup(n)) < 0) { 4661da177e4SLinus Torvalds rc = ERR_PTR(error); 4671da177e4SLinus Torvalds goto out_neigh_release; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds n->confirmed = jiffies - (n->parms->base_reachable_time << 1); 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 473d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 474d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 4751da177e4SLinus Torvalds 476d6bf7817SEric Dumazet if (atomic_read(&tbl->entries) > (nht->hash_mask + 1)) 477d6bf7817SEric Dumazet nht = neigh_hash_grow(tbl, (nht->hash_mask + 1) << 1); 4781da177e4SLinus Torvalds 479d6bf7817SEric Dumazet hash_val = tbl->hash(pkey, dev, nht->hash_rnd) & nht->hash_mask; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds if (n->parms->dead) { 4821da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 4831da177e4SLinus Torvalds goto out_tbl_unlock; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 486d6bf7817SEric Dumazet for (n1 = nht->hash_buckets[hash_val]; n1; n1 = n1->next) { 4871da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 4881da177e4SLinus Torvalds neigh_hold(n1); 4891da177e4SLinus Torvalds rc = n1; 4901da177e4SLinus Torvalds goto out_tbl_unlock; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494d6bf7817SEric Dumazet n->next = nht->hash_buckets[hash_val]; 495d6bf7817SEric Dumazet nht->hash_buckets[hash_val] = n; 4961da177e4SLinus Torvalds n->dead = 0; 4971da177e4SLinus Torvalds neigh_hold(n); 4981da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 4991da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is created.\n", n); 5001da177e4SLinus Torvalds rc = n; 5011da177e4SLinus Torvalds out: 5021da177e4SLinus Torvalds return rc; 5031da177e4SLinus Torvalds out_tbl_unlock: 5041da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5051da177e4SLinus Torvalds out_neigh_release: 5061da177e4SLinus Torvalds neigh_release(n); 5071da177e4SLinus Torvalds goto out; 5081da177e4SLinus Torvalds } 5090a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_create); 5101da177e4SLinus Torvalds 511be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len) 512fa86d322SPavel Emelyanov { 513fa86d322SPavel Emelyanov u32 hash_val = *(u32 *)(pkey + key_len - 4); 514fa86d322SPavel Emelyanov hash_val ^= (hash_val >> 16); 515fa86d322SPavel Emelyanov hash_val ^= hash_val >> 8; 516fa86d322SPavel Emelyanov hash_val ^= hash_val >> 4; 517fa86d322SPavel Emelyanov hash_val &= PNEIGH_HASHMASK; 518be01d655SYOSHIFUJI Hideaki return hash_val; 519fa86d322SPavel Emelyanov } 520fa86d322SPavel Emelyanov 521be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, 522be01d655SYOSHIFUJI Hideaki struct net *net, 523be01d655SYOSHIFUJI Hideaki const void *pkey, 524be01d655SYOSHIFUJI Hideaki int key_len, 525be01d655SYOSHIFUJI Hideaki struct net_device *dev) 526be01d655SYOSHIFUJI Hideaki { 527be01d655SYOSHIFUJI Hideaki while (n) { 528be01d655SYOSHIFUJI Hideaki if (!memcmp(n->key, pkey, key_len) && 529be01d655SYOSHIFUJI Hideaki net_eq(pneigh_net(n), net) && 530be01d655SYOSHIFUJI Hideaki (n->dev == dev || !n->dev)) 531fa86d322SPavel Emelyanov return n; 532be01d655SYOSHIFUJI Hideaki n = n->next; 533be01d655SYOSHIFUJI Hideaki } 534be01d655SYOSHIFUJI Hideaki return NULL; 535be01d655SYOSHIFUJI Hideaki } 536be01d655SYOSHIFUJI Hideaki 537be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, 538be01d655SYOSHIFUJI Hideaki struct net *net, const void *pkey, struct net_device *dev) 539be01d655SYOSHIFUJI Hideaki { 540be01d655SYOSHIFUJI Hideaki int key_len = tbl->key_len; 541be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 542be01d655SYOSHIFUJI Hideaki 543be01d655SYOSHIFUJI Hideaki return __pneigh_lookup_1(tbl->phash_buckets[hash_val], 544be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 545fa86d322SPavel Emelyanov } 5460a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup); 547fa86d322SPavel Emelyanov 548426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 549426b5303SEric W. Biederman struct net *net, const void *pkey, 5501da177e4SLinus Torvalds struct net_device *dev, int creat) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds struct pneigh_entry *n; 5531da177e4SLinus Torvalds int key_len = tbl->key_len; 554be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 557be01d655SYOSHIFUJI Hideaki n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], 558be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 559be01d655SYOSHIFUJI Hideaki read_unlock_bh(&tbl->lock); 5601da177e4SLinus Torvalds 561be01d655SYOSHIFUJI Hideaki if (n || !creat) 5621da177e4SLinus Torvalds goto out; 5631da177e4SLinus Torvalds 5644ae28944SPavel Emelyanov ASSERT_RTNL(); 5654ae28944SPavel Emelyanov 5661da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 5671da177e4SLinus Torvalds if (!n) 5681da177e4SLinus Torvalds goto out; 5691da177e4SLinus Torvalds 570e42ea986SEric Dumazet write_pnet(&n->net, hold_net(net)); 5711da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 5721da177e4SLinus Torvalds n->dev = dev; 5731da177e4SLinus Torvalds if (dev) 5741da177e4SLinus Torvalds dev_hold(dev); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 5771da177e4SLinus Torvalds if (dev) 5781da177e4SLinus Torvalds dev_put(dev); 579da12f735SDenis V. Lunev release_net(net); 5801da177e4SLinus Torvalds kfree(n); 5811da177e4SLinus Torvalds n = NULL; 5821da177e4SLinus Torvalds goto out; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 5861da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 5871da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 5881da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5891da177e4SLinus Torvalds out: 5901da177e4SLinus Torvalds return n; 5911da177e4SLinus Torvalds } 5920a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup); 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds 595426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 5961da177e4SLinus Torvalds struct net_device *dev) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds struct pneigh_entry *n, **np; 5991da177e4SLinus Torvalds int key_len = tbl->key_len; 600be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6031da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 6041da177e4SLinus Torvalds np = &n->next) { 605426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 606878628fbSYOSHIFUJI Hideaki net_eq(pneigh_net(n), net)) { 6071da177e4SLinus Torvalds *np = n->next; 6081da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6091da177e4SLinus Torvalds if (tbl->pdestructor) 6101da177e4SLinus Torvalds tbl->pdestructor(n); 6111da177e4SLinus Torvalds if (n->dev) 6121da177e4SLinus Torvalds dev_put(n->dev); 61357da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6141da177e4SLinus Torvalds kfree(n); 6151da177e4SLinus Torvalds return 0; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6191da177e4SLinus Torvalds return -ENOENT; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 6231da177e4SLinus Torvalds { 6241da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6251da177e4SLinus Torvalds u32 h; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 6281da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 6291da177e4SLinus Torvalds while ((n = *np) != NULL) { 6301da177e4SLinus Torvalds if (!dev || n->dev == dev) { 6311da177e4SLinus Torvalds *np = n->next; 6321da177e4SLinus Torvalds if (tbl->pdestructor) 6331da177e4SLinus Torvalds tbl->pdestructor(n); 6341da177e4SLinus Torvalds if (n->dev) 6351da177e4SLinus Torvalds dev_put(n->dev); 63657da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6371da177e4SLinus Torvalds kfree(n); 6381da177e4SLinus Torvalds continue; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds np = &n->next; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds return -ENOENT; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 64606f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 64706f0511dSDenis V. Lunev 64806f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 64906f0511dSDenis V. Lunev { 65006f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 65106f0511dSDenis V. Lunev neigh_parms_destroy(parms); 65206f0511dSDenis V. Lunev } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds /* 6551da177e4SLinus Torvalds * neighbour must already be out of the table; 6561da177e4SLinus Torvalds * 6571da177e4SLinus Torvalds */ 6581da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds struct hh_cache *hh; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds if (!neigh->dead) { 6651da177e4SLinus Torvalds printk(KERN_WARNING 6661da177e4SLinus Torvalds "Destroying alive neighbour %p\n", neigh); 6671da177e4SLinus Torvalds dump_stack(); 6681da177e4SLinus Torvalds return; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 6721da177e4SLinus Torvalds printk(KERN_WARNING "Impossible event.\n"); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds while ((hh = neigh->hh) != NULL) { 6751da177e4SLinus Torvalds neigh->hh = hh->hh_next; 6761da177e4SLinus Torvalds hh->hh_next = NULL; 6773644f0ceSStephen Hemminger 6783644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 6791da177e4SLinus Torvalds hh->hh_output = neigh_blackhole; 6803644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 6811da177e4SLinus Torvalds if (atomic_dec_and_test(&hh->hh_refcnt)) 6821da177e4SLinus Torvalds kfree(hh); 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds dev_put(neigh->dev); 6881da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 6931da177e4SLinus Torvalds kmem_cache_free(neigh->tbl->kmem_cachep, neigh); 6941da177e4SLinus Torvalds } 6950a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* Neighbour state is suspicious; 6981da177e4SLinus Torvalds disable fast path. 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds Called with write_locked neigh. 7011da177e4SLinus Torvalds */ 7021da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 7031da177e4SLinus Torvalds { 7041da177e4SLinus Torvalds struct hh_cache *hh; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds neigh->output = neigh->ops->output; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 7111da177e4SLinus Torvalds hh->hh_output = neigh->ops->output; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* Neighbour state is OK; 7151da177e4SLinus Torvalds enable fast path. 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds Called with write_locked neigh. 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds struct hh_cache *hh; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is connected.\n", neigh); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 7281da177e4SLinus Torvalds hh->hh_output = neigh->ops->hh_output; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 731e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work) 7321da177e4SLinus Torvalds { 733e4c4e448SEric Dumazet struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); 7341da177e4SLinus Torvalds struct neighbour *n, **np; 735e4c4e448SEric Dumazet unsigned int i; 736d6bf7817SEric Dumazet struct neigh_hash_table *nht; 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 7391da177e4SLinus Torvalds 740e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 741d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 742d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* 7451da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 7461da177e4SLinus Torvalds */ 7471da177e4SLinus Torvalds 748e4c4e448SEric Dumazet if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { 7491da177e4SLinus Torvalds struct neigh_parms *p; 750e4c4e448SEric Dumazet tbl->last_rand = jiffies; 7511da177e4SLinus Torvalds for (p = &tbl->parms; p; p = p->next) 7521da177e4SLinus Torvalds p->reachable_time = 7531da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 756d6bf7817SEric Dumazet for (i = 0 ; i <= nht->hash_mask; i++) { 757d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds while ((n = *np) != NULL) { 7601da177e4SLinus Torvalds unsigned int state; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds write_lock(&n->lock); 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds state = n->nud_state; 7651da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 7661da177e4SLinus Torvalds write_unlock(&n->lock); 7671da177e4SLinus Torvalds goto next_elt; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 7711da177e4SLinus Torvalds n->used = n->confirmed; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 7741da177e4SLinus Torvalds (state == NUD_FAILED || 775e4c4e448SEric Dumazet time_after(jiffies, n->used + n->parms->gc_staletime))) { 7761da177e4SLinus Torvalds *np = n->next; 7771da177e4SLinus Torvalds n->dead = 1; 7781da177e4SLinus Torvalds write_unlock(&n->lock); 7794f494554SThomas Graf neigh_cleanup_and_release(n); 7801da177e4SLinus Torvalds continue; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds write_unlock(&n->lock); 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds next_elt: 7851da177e4SLinus Torvalds np = &n->next; 7861da177e4SLinus Torvalds } 787e4c4e448SEric Dumazet /* 788e4c4e448SEric Dumazet * It's fine to release lock here, even if hash table 789e4c4e448SEric Dumazet * grows while we are preempted. 790e4c4e448SEric Dumazet */ 791e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 792e4c4e448SEric Dumazet cond_resched(); 793e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 794e4c4e448SEric Dumazet } 7951da177e4SLinus Torvalds /* Cycle through all hash buckets every base_reachable_time/2 ticks. 7961da177e4SLinus Torvalds * ARP entry timeouts range from 1/2 base_reachable_time to 3/2 7971da177e4SLinus Torvalds * base_reachable_time. 7981da177e4SLinus Torvalds */ 799e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, 800e4c4e448SEric Dumazet tbl->parms.base_reachable_time >> 1); 801e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 807a02cec21SEric Dumazet return (n->nud_state & NUD_PROBE) ? 8081da177e4SLinus Torvalds p->ucast_probes : 809a02cec21SEric Dumazet p->ucast_probes + p->app_probes + p->mcast_probes; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8125ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh) 8130a141509SEric Dumazet __releases(neigh->lock) 8140a141509SEric Dumazet __acquires(neigh->lock) 8155ef12d98STimo Teras { 8165ef12d98STimo Teras struct sk_buff *skb; 8175ef12d98STimo Teras 8185ef12d98STimo Teras NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 8195ef12d98STimo Teras NEIGH_PRINTK2("neigh %p is failed.\n", neigh); 8205ef12d98STimo Teras neigh->updated = jiffies; 8215ef12d98STimo Teras 8225ef12d98STimo Teras /* It is very thin place. report_unreachable is very complicated 8235ef12d98STimo Teras routine. Particularly, it can hit the same neighbour entry! 8245ef12d98STimo Teras 8255ef12d98STimo Teras So that, we try to be accurate and avoid dead loop. --ANK 8265ef12d98STimo Teras */ 8275ef12d98STimo Teras while (neigh->nud_state == NUD_FAILED && 8285ef12d98STimo Teras (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 8295ef12d98STimo Teras write_unlock(&neigh->lock); 8305ef12d98STimo Teras neigh->ops->error_report(neigh, skb); 8315ef12d98STimo Teras write_lock(&neigh->lock); 8325ef12d98STimo Teras } 8335ef12d98STimo Teras skb_queue_purge(&neigh->arp_queue); 8345ef12d98STimo Teras } 8355ef12d98STimo Teras 8361da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds unsigned long now, next; 8411da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 8421da177e4SLinus Torvalds unsigned state; 8431da177e4SLinus Torvalds int notify = 0; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds write_lock(&neigh->lock); 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds state = neigh->nud_state; 8481da177e4SLinus Torvalds now = jiffies; 8491da177e4SLinus Torvalds next = now + HZ; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds if (!(state & NUD_IN_TIMER)) { 8521da177e4SLinus Torvalds #ifndef CONFIG_SMP 8531da177e4SLinus Torvalds printk(KERN_WARNING "neigh: timer & !nud_in_timer\n"); 8541da177e4SLinus Torvalds #endif 8551da177e4SLinus Torvalds goto out; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 8591da177e4SLinus Torvalds if (time_before_eq(now, 8601da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 8611da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is still alive.\n", neigh); 8621da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 8631da177e4SLinus Torvalds } else if (time_before_eq(now, 8641da177e4SLinus Torvalds neigh->used + neigh->parms->delay_probe_time)) { 8651da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 8661da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 867955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8681da177e4SLinus Torvalds neigh_suspect(neigh); 8691da177e4SLinus Torvalds next = now + neigh->parms->delay_probe_time; 8701da177e4SLinus Torvalds } else { 8711da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 8721da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 873955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8741da177e4SLinus Torvalds neigh_suspect(neigh); 8758d71740cSTom Tucker notify = 1; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 8781da177e4SLinus Torvalds if (time_before_eq(now, 8791da177e4SLinus Torvalds neigh->confirmed + neigh->parms->delay_probe_time)) { 8801da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh); 8811da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 882955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8831da177e4SLinus Torvalds neigh_connect(neigh); 8848d71740cSTom Tucker notify = 1; 8851da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 8861da177e4SLinus Torvalds } else { 8871da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is probed.\n", neigh); 8881da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 889955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8901da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 8911da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds } else { 8941da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 8951da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 8991da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 9001da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 9011da177e4SLinus Torvalds notify = 1; 9025ef12d98STimo Teras neigh_invalidate(neigh); 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 9061da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 9071da177e4SLinus Torvalds next = jiffies + HZ/2; 9086fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 9096fb9974fSHerbert Xu neigh_hold(neigh); 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 9121da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&neigh->arp_queue); 9139ff56607SDavid S. Miller /* keep skb alive even if arp_queue overflows */ 9149ff56607SDavid S. Miller if (skb) 9157e36763bSFrank Blaschka skb = skb_copy(skb, GFP_ATOMIC); 9169ff56607SDavid S. Miller write_unlock(&neigh->lock); 9171da177e4SLinus Torvalds neigh->ops->solicit(neigh, skb); 9181da177e4SLinus Torvalds atomic_inc(&neigh->probes); 9199ff56607SDavid S. Miller kfree_skb(skb); 9209ff56607SDavid S. Miller } else { 9211da177e4SLinus Torvalds out: 9221da177e4SLinus Torvalds write_unlock(&neigh->lock); 9239ff56607SDavid S. Miller } 9241da177e4SLinus Torvalds 925d961db35SThomas Graf if (notify) 926d961db35SThomas Graf neigh_update_notify(neigh); 927d961db35SThomas Graf 9281da177e4SLinus Torvalds neigh_release(neigh); 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 9321da177e4SLinus Torvalds { 9331da177e4SLinus Torvalds int rc; 9341da177e4SLinus Torvalds unsigned long now; 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds rc = 0; 9391da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 9401da177e4SLinus Torvalds goto out_unlock_bh; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds now = jiffies; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 9451da177e4SLinus Torvalds if (neigh->parms->mcast_probes + neigh->parms->app_probes) { 9461da177e4SLinus Torvalds atomic_set(&neigh->probes, neigh->parms->ucast_probes); 9471da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 948955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 949667347f1SDavid S. Miller neigh_add_timer(neigh, now + 1); 9501da177e4SLinus Torvalds } else { 9511da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 952955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9531da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds kfree_skb(skb); 9561da177e4SLinus Torvalds return 1; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 9591da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 9601da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 961955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 962667347f1SDavid S. Miller neigh_add_timer(neigh, 963667347f1SDavid S. Miller jiffies + neigh->parms->delay_probe_time); 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 9671da177e4SLinus Torvalds if (skb) { 9681da177e4SLinus Torvalds if (skb_queue_len(&neigh->arp_queue) >= 9691da177e4SLinus Torvalds neigh->parms->queue_len) { 9701da177e4SLinus Torvalds struct sk_buff *buff; 971f72051b0SDavid S. Miller buff = __skb_dequeue(&neigh->arp_queue); 9721da177e4SLinus Torvalds kfree_skb(buff); 9739a6d276eSNeil Horman NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); 9741da177e4SLinus Torvalds } 975a4731138SEric Dumazet skb_dst_force(skb); 9761da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds rc = 1; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds out_unlock_bh: 9811da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9821da177e4SLinus Torvalds return rc; 9831da177e4SLinus Torvalds } 9840a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send); 9851da177e4SLinus Torvalds 986e92b43a3SStephen Hemminger static void neigh_update_hhs(struct neighbour *neigh) 9871da177e4SLinus Torvalds { 9881da177e4SLinus Torvalds struct hh_cache *hh; 9893b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 99091a72a70SDoug Kehn = NULL; 99191a72a70SDoug Kehn 99291a72a70SDoug Kehn if (neigh->dev->header_ops) 99391a72a70SDoug Kehn update = neigh->dev->header_ops->cache_update; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds if (update) { 9961da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) { 9973644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 9981da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 9993644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* Generic update routine. 10071da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 10081da177e4SLinus Torvalds -- new is new state. 10091da177e4SLinus Torvalds -- flags 10101da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 10111da177e4SLinus Torvalds if it is different. 10121da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 10131da177e4SLinus Torvalds lladdr instead of overriding it 10141da177e4SLinus Torvalds if it is different. 10151da177e4SLinus Torvalds It also allows to retain current state 10161da177e4SLinus Torvalds if lladdr is unchanged. 10171da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 10201da177e4SLinus Torvalds NTF_ROUTER flag. 10211da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 10221da177e4SLinus Torvalds a router. 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 10281da177e4SLinus Torvalds u32 flags) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds u8 old; 10311da177e4SLinus Torvalds int err; 10321da177e4SLinus Torvalds int notify = 0; 10331da177e4SLinus Torvalds struct net_device *dev; 10341da177e4SLinus Torvalds int update_isrouter = 0; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds dev = neigh->dev; 10391da177e4SLinus Torvalds old = neigh->nud_state; 10401da177e4SLinus Torvalds err = -EPERM; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 10431da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 10441da177e4SLinus Torvalds goto out; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 10471da177e4SLinus Torvalds neigh_del_timer(neigh); 10481da177e4SLinus Torvalds if (old & NUD_CONNECTED) 10491da177e4SLinus Torvalds neigh_suspect(neigh); 10501da177e4SLinus Torvalds neigh->nud_state = new; 10511da177e4SLinus Torvalds err = 0; 10521da177e4SLinus Torvalds notify = old & NUD_VALID; 10535ef12d98STimo Teras if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && 10545ef12d98STimo Teras (new & NUD_FAILED)) { 10555ef12d98STimo Teras neigh_invalidate(neigh); 10565ef12d98STimo Teras notify = 1; 10575ef12d98STimo Teras } 10581da177e4SLinus Torvalds goto out; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 10621da177e4SLinus Torvalds if (!dev->addr_len) { 10631da177e4SLinus Torvalds /* First case: device needs no address. */ 10641da177e4SLinus Torvalds lladdr = neigh->ha; 10651da177e4SLinus Torvalds } else if (lladdr) { 10661da177e4SLinus Torvalds /* The second case: if something is already cached 10671da177e4SLinus Torvalds and a new address is proposed: 10681da177e4SLinus Torvalds - compare new & old 10691da177e4SLinus Torvalds - if they are different, check override flag 10701da177e4SLinus Torvalds */ 10711da177e4SLinus Torvalds if ((old & NUD_VALID) && 10721da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 10731da177e4SLinus Torvalds lladdr = neigh->ha; 10741da177e4SLinus Torvalds } else { 10751da177e4SLinus Torvalds /* No address is supplied; if we know something, 10761da177e4SLinus Torvalds use it, otherwise discard the request. 10771da177e4SLinus Torvalds */ 10781da177e4SLinus Torvalds err = -EINVAL; 10791da177e4SLinus Torvalds if (!(old & NUD_VALID)) 10801da177e4SLinus Torvalds goto out; 10811da177e4SLinus Torvalds lladdr = neigh->ha; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds if (new & NUD_CONNECTED) 10851da177e4SLinus Torvalds neigh->confirmed = jiffies; 10861da177e4SLinus Torvalds neigh->updated = jiffies; 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds /* If entry was valid and address is not changed, 10891da177e4SLinus Torvalds do not change entry state, if new one is STALE. 10901da177e4SLinus Torvalds */ 10911da177e4SLinus Torvalds err = 0; 10921da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 10931da177e4SLinus Torvalds if (old & NUD_VALID) { 10941da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 10951da177e4SLinus Torvalds update_isrouter = 0; 10961da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 10971da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 10981da177e4SLinus Torvalds lladdr = neigh->ha; 10991da177e4SLinus Torvalds new = NUD_STALE; 11001da177e4SLinus Torvalds } else 11011da177e4SLinus Torvalds goto out; 11021da177e4SLinus Torvalds } else { 11031da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 11041da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 11051da177e4SLinus Torvalds (old & NUD_CONNECTED)) 11061da177e4SLinus Torvalds ) 11071da177e4SLinus Torvalds new = old; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds if (new != old) { 11121da177e4SLinus Torvalds neigh_del_timer(neigh); 1113a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1114667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 11151da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1116667347f1SDavid S. Miller neigh->parms->reachable_time : 1117667347f1SDavid S. Miller 0))); 11181da177e4SLinus Torvalds neigh->nud_state = new; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds if (lladdr != neigh->ha) { 11221da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 11231da177e4SLinus Torvalds neigh_update_hhs(neigh); 11241da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 11251da177e4SLinus Torvalds neigh->confirmed = jiffies - 11261da177e4SLinus Torvalds (neigh->parms->base_reachable_time << 1); 11271da177e4SLinus Torvalds notify = 1; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds if (new == old) 11301da177e4SLinus Torvalds goto out; 11311da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11321da177e4SLinus Torvalds neigh_connect(neigh); 11331da177e4SLinus Torvalds else 11341da177e4SLinus Torvalds neigh_suspect(neigh); 11351da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 11361da177e4SLinus Torvalds struct sk_buff *skb; 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 11411da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 11421da177e4SLinus Torvalds struct neighbour *n1 = neigh; 11431da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 11441da177e4SLinus Torvalds /* On shaper/eql skb->dst->neighbour != neigh :( */ 1145adf30907SEric Dumazet if (skb_dst(skb) && skb_dst(skb)->neighbour) 1146adf30907SEric Dumazet n1 = skb_dst(skb)->neighbour; 11471da177e4SLinus Torvalds n1->output(skb); 11481da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds out: 11531da177e4SLinus Torvalds if (update_isrouter) { 11541da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 11551da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 11561da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 11598d71740cSTom Tucker 11608d71740cSTom Tucker if (notify) 1161d961db35SThomas Graf neigh_update_notify(neigh); 1162d961db35SThomas Graf 11631da177e4SLinus Torvalds return err; 11641da177e4SLinus Torvalds } 11650a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update); 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 11681da177e4SLinus Torvalds u8 *lladdr, void *saddr, 11691da177e4SLinus Torvalds struct net_device *dev) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 11721da177e4SLinus Torvalds lladdr || !dev->addr_len); 11731da177e4SLinus Torvalds if (neigh) 11741da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 11751da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 11761da177e4SLinus Torvalds return neigh; 11771da177e4SLinus Torvalds } 11780a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns); 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, 1181d77072ecSAl Viro __be16 protocol) 11821da177e4SLinus Torvalds { 11831da177e4SLinus Torvalds struct hh_cache *hh; 11841da177e4SLinus Torvalds struct net_device *dev = dst->dev; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds for (hh = n->hh; hh; hh = hh->hh_next) 11871da177e4SLinus Torvalds if (hh->hh_type == protocol) 11881da177e4SLinus Torvalds break; 11891da177e4SLinus Torvalds 119077d04bd9SAndrew Morton if (!hh && (hh = kzalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) { 11913644f0ceSStephen Hemminger seqlock_init(&hh->hh_lock); 11921da177e4SLinus Torvalds hh->hh_type = protocol; 11931da177e4SLinus Torvalds atomic_set(&hh->hh_refcnt, 0); 11941da177e4SLinus Torvalds hh->hh_next = NULL; 11953b04dddeSStephen Hemminger 11963b04dddeSStephen Hemminger if (dev->header_ops->cache(n, hh)) { 11971da177e4SLinus Torvalds kfree(hh); 11981da177e4SLinus Torvalds hh = NULL; 11991da177e4SLinus Torvalds } else { 12001da177e4SLinus Torvalds atomic_inc(&hh->hh_refcnt); 12011da177e4SLinus Torvalds hh->hh_next = n->hh; 12021da177e4SLinus Torvalds n->hh = hh; 12031da177e4SLinus Torvalds if (n->nud_state & NUD_CONNECTED) 12041da177e4SLinus Torvalds hh->hh_output = n->ops->hh_output; 12051da177e4SLinus Torvalds else 12061da177e4SLinus Torvalds hh->hh_output = n->ops->output; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds if (hh) { 12101da177e4SLinus Torvalds atomic_inc(&hh->hh_refcnt); 12111da177e4SLinus Torvalds dst->hh = hh; 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit 12161da177e4SLinus Torvalds worked, f.e. if you want to override normal output path (eql, shaper), 12171da177e4SLinus Torvalds but resolution is not made yet. 12181da177e4SLinus Torvalds */ 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds int neigh_compat_output(struct sk_buff *skb) 12211da177e4SLinus Torvalds { 12221da177e4SLinus Torvalds struct net_device *dev = skb->dev; 12231da177e4SLinus Torvalds 1224bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12251da177e4SLinus Torvalds 12260c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 12271da177e4SLinus Torvalds skb->len) < 0 && 12283b04dddeSStephen Hemminger dev->header_ops->rebuild(skb)) 12291da177e4SLinus Torvalds return 0; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds return dev_queue_xmit(skb); 12321da177e4SLinus Torvalds } 12330a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output); 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds /* Slow and careful. */ 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds int neigh_resolve_output(struct sk_buff *skb) 12381da177e4SLinus Torvalds { 1239adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 12401da177e4SLinus Torvalds struct neighbour *neigh; 12411da177e4SLinus Torvalds int rc = 0; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds if (!dst || !(neigh = dst->neighbour)) 12441da177e4SLinus Torvalds goto discard; 12451da177e4SLinus Torvalds 1246bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 12491da177e4SLinus Torvalds int err; 12501da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 1251c7d4426aSEric Dumazet if (dev->header_ops->cache && 1252c7d4426aSEric Dumazet !dst->hh && 1253c7d4426aSEric Dumazet !(dst->flags & DST_NOCACHE)) { 12541da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 12551da177e4SLinus Torvalds if (!dst->hh) 12561da177e4SLinus Torvalds neigh_hh_init(neigh, dst, dst->ops->protocol); 12570c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12581da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12591da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 12601da177e4SLinus Torvalds } else { 12611da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 12620c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12631da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12641da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds if (err >= 0) 12671da177e4SLinus Torvalds rc = neigh->ops->queue_xmit(skb); 12681da177e4SLinus Torvalds else 12691da177e4SLinus Torvalds goto out_kfree_skb; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds out: 12721da177e4SLinus Torvalds return rc; 12731da177e4SLinus Torvalds discard: 12741da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", 12751da177e4SLinus Torvalds dst, dst ? dst->neighbour : NULL); 12761da177e4SLinus Torvalds out_kfree_skb: 12771da177e4SLinus Torvalds rc = -EINVAL; 12781da177e4SLinus Torvalds kfree_skb(skb); 12791da177e4SLinus Torvalds goto out; 12801da177e4SLinus Torvalds } 12810a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output); 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds /* As fast as possible without hh cache */ 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds int neigh_connected_output(struct sk_buff *skb) 12861da177e4SLinus Torvalds { 12871da177e4SLinus Torvalds int err; 1288adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 12891da177e4SLinus Torvalds struct neighbour *neigh = dst->neighbour; 12901da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 12911da177e4SLinus Torvalds 1292bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 12950c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12961da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12971da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 12981da177e4SLinus Torvalds if (err >= 0) 12991da177e4SLinus Torvalds err = neigh->ops->queue_xmit(skb); 13001da177e4SLinus Torvalds else { 13011da177e4SLinus Torvalds err = -EINVAL; 13021da177e4SLinus Torvalds kfree_skb(skb); 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds return err; 13051da177e4SLinus Torvalds } 13060a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 13091da177e4SLinus Torvalds { 13101da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 13111da177e4SLinus Torvalds long sched_next = 0; 13121da177e4SLinus Torvalds unsigned long now = jiffies; 1313f72051b0SDavid S. Miller struct sk_buff *skb, *n; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13161da177e4SLinus Torvalds 1317f72051b0SDavid S. Miller skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { 1318f72051b0SDavid S. Miller long tdif = NEIGH_CB(skb)->sched_next - now; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds if (tdif <= 0) { 1321f72051b0SDavid S. Miller struct net_device *dev = skb->dev; 1322f72051b0SDavid S. Miller __skb_unlink(skb, &tbl->proxy_queue); 13231da177e4SLinus Torvalds if (tbl->proxy_redo && netif_running(dev)) 1324f72051b0SDavid S. Miller tbl->proxy_redo(skb); 13251da177e4SLinus Torvalds else 1326f72051b0SDavid S. Miller kfree_skb(skb); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds dev_put(dev); 13291da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 13301da177e4SLinus Torvalds sched_next = tdif; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 13331da177e4SLinus Torvalds if (sched_next) 13341da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 13351da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 13391da177e4SLinus Torvalds struct sk_buff *skb) 13401da177e4SLinus Torvalds { 13411da177e4SLinus Torvalds unsigned long now = jiffies; 13421da177e4SLinus Torvalds unsigned long sched_next = now + (net_random() % p->proxy_delay); 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (tbl->proxy_queue.qlen > p->proxy_qlen) { 13451da177e4SLinus Torvalds kfree_skb(skb); 13461da177e4SLinus Torvalds return; 13471da177e4SLinus Torvalds } 1348a61bbcf2SPatrick McHardy 1349a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1350a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13531da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 13541da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 13551da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 13561da177e4SLinus Torvalds } 1357adf30907SEric Dumazet skb_dst_drop(skb); 13581da177e4SLinus Torvalds dev_hold(skb->dev); 13591da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 13601da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 13611da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13621da177e4SLinus Torvalds } 13630a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue); 13641da177e4SLinus Torvalds 136597fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, 1366426b5303SEric W. Biederman struct net *net, int ifindex) 1367426b5303SEric W. Biederman { 1368426b5303SEric W. Biederman struct neigh_parms *p; 1369426b5303SEric W. Biederman 1370426b5303SEric W. Biederman for (p = &tbl->parms; p; p = p->next) { 1371878628fbSYOSHIFUJI Hideaki if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || 1372426b5303SEric W. Biederman (!p->dev && !ifindex)) 1373426b5303SEric W. Biederman return p; 1374426b5303SEric W. Biederman } 1375426b5303SEric W. Biederman 1376426b5303SEric W. Biederman return NULL; 1377426b5303SEric W. Biederman } 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 13801da177e4SLinus Torvalds struct neigh_table *tbl) 13811da177e4SLinus Torvalds { 1382426b5303SEric W. Biederman struct neigh_parms *p, *ref; 138300829823SStephen Hemminger struct net *net = dev_net(dev); 138400829823SStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 13851da177e4SLinus Torvalds 138697fd5bc7STobias Klauser ref = lookup_neigh_parms(tbl, net, 0); 1387426b5303SEric W. Biederman if (!ref) 1388426b5303SEric W. Biederman return NULL; 1389426b5303SEric W. Biederman 1390426b5303SEric W. Biederman p = kmemdup(ref, sizeof(*p), GFP_KERNEL); 13911da177e4SLinus Torvalds if (p) { 13921da177e4SLinus Torvalds p->tbl = tbl; 13931da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 13941da177e4SLinus Torvalds p->reachable_time = 13951da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 1396486b51d3SDenis V. Lunev 139700829823SStephen Hemminger if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 13981da177e4SLinus Torvalds kfree(p); 13991da177e4SLinus Torvalds return NULL; 14001da177e4SLinus Torvalds } 1401c7fb64dbSThomas Graf 1402c7fb64dbSThomas Graf dev_hold(dev); 1403c7fb64dbSThomas Graf p->dev = dev; 1404e42ea986SEric Dumazet write_pnet(&p->net, hold_net(net)); 14051da177e4SLinus Torvalds p->sysctl_table = NULL; 14061da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14071da177e4SLinus Torvalds p->next = tbl->parms.next; 14081da177e4SLinus Torvalds tbl->parms.next = p; 14091da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds return p; 14121da177e4SLinus Torvalds } 14130a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc); 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds struct neigh_parms *parms = 14181da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds neigh_parms_put(parms); 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 14241da177e4SLinus Torvalds { 14251da177e4SLinus Torvalds struct neigh_parms **p; 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 14281da177e4SLinus Torvalds return; 14291da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14301da177e4SLinus Torvalds for (p = &tbl->parms.next; *p; p = &(*p)->next) { 14311da177e4SLinus Torvalds if (*p == parms) { 14321da177e4SLinus Torvalds *p = parms->next; 14331da177e4SLinus Torvalds parms->dead = 1; 14341da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1435cecbb639SDavid S. Miller if (parms->dev) 1436cecbb639SDavid S. Miller dev_put(parms->dev); 14371da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 14381da177e4SLinus Torvalds return; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14421da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_parms_release: not found\n"); 14431da177e4SLinus Torvalds } 14440a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release); 14451da177e4SLinus Torvalds 144606f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 14471da177e4SLinus Torvalds { 144857da52c1SYOSHIFUJI Hideaki release_net(neigh_parms_net(parms)); 14491da177e4SLinus Torvalds kfree(parms); 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 1452c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1453c2ecba71SPavel Emelianov 1454bd89efc5SSimon Kelley void neigh_table_init_no_netlink(struct neigh_table *tbl) 14551da177e4SLinus Torvalds { 14561da177e4SLinus Torvalds unsigned long now = jiffies; 14571da177e4SLinus Torvalds unsigned long phsize; 14581da177e4SLinus Torvalds 1459e42ea986SEric Dumazet write_pnet(&tbl->parms.net, &init_net); 14601da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 14611da177e4SLinus Torvalds tbl->parms.reachable_time = 14621da177e4SLinus Torvalds neigh_rand_reach_time(tbl->parms.base_reachable_time); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds if (!tbl->kmem_cachep) 1465e5d679f3SAlexey Dobriyan tbl->kmem_cachep = 1466e5d679f3SAlexey Dobriyan kmem_cache_create(tbl->id, tbl->entry_size, 0, 1467e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 146820c2df83SPaul Mundt NULL); 14691da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 14701da177e4SLinus Torvalds if (!tbl->stats) 14711da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 14749b739ba5SAlexey Dobriyan if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat, 14759b739ba5SAlexey Dobriyan &neigh_stat_seq_fops, tbl)) 14761da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 14771da177e4SLinus Torvalds #endif 14781da177e4SLinus Torvalds 1479d6bf7817SEric Dumazet tbl->nht = neigh_hash_alloc(8); 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 148277d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 14831da177e4SLinus Torvalds 1484d6bf7817SEric Dumazet if (!tbl->nht || !tbl->phash_buckets) 14851da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1488e4c4e448SEric Dumazet INIT_DELAYED_WORK_DEFERRABLE(&tbl->gc_work, neigh_periodic_work); 1489e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time); 1490b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1491c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1492c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds tbl->last_flush = now; 14951da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1496bd89efc5SSimon Kelley } 14970a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init_no_netlink); 1498bd89efc5SSimon Kelley 1499bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl) 1500bd89efc5SSimon Kelley { 1501bd89efc5SSimon Kelley struct neigh_table *tmp; 1502bd89efc5SSimon Kelley 1503bd89efc5SSimon Kelley neigh_table_init_no_netlink(tbl); 15041da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 1505bd89efc5SSimon Kelley for (tmp = neigh_tables; tmp; tmp = tmp->next) { 1506bd89efc5SSimon Kelley if (tmp->family == tbl->family) 1507bd89efc5SSimon Kelley break; 1508bd89efc5SSimon Kelley } 15091da177e4SLinus Torvalds tbl->next = neigh_tables; 15101da177e4SLinus Torvalds neigh_tables = tbl; 15111da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 1512bd89efc5SSimon Kelley 1513bd89efc5SSimon Kelley if (unlikely(tmp)) { 1514bd89efc5SSimon Kelley printk(KERN_ERR "NEIGH: Registering multiple tables for " 1515bd89efc5SSimon Kelley "family %d\n", tbl->family); 1516bd89efc5SSimon Kelley dump_stack(); 1517bd89efc5SSimon Kelley } 15181da177e4SLinus Torvalds } 15190a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init); 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl) 15221da177e4SLinus Torvalds { 15231da177e4SLinus Torvalds struct neigh_table **tp; 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 1526e4c4e448SEric Dumazet cancel_delayed_work(&tbl->gc_work); 1527e4c4e448SEric Dumazet flush_scheduled_work(); 15281da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 15291da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 15301da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 15311da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 15321da177e4SLinus Torvalds printk(KERN_CRIT "neighbour leakage\n"); 15331da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 15341da177e4SLinus Torvalds for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { 15351da177e4SLinus Torvalds if (*tp == tbl) { 15361da177e4SLinus Torvalds *tp = tbl->next; 15371da177e4SLinus Torvalds break; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 15411da177e4SLinus Torvalds 1542d6bf7817SEric Dumazet call_rcu(&tbl->nht->rcu, neigh_hash_free_rcu); 1543d6bf7817SEric Dumazet tbl->nht = NULL; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds kfree(tbl->phash_buckets); 15461da177e4SLinus Torvalds tbl->phash_buckets = NULL; 15471da177e4SLinus Torvalds 15483f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 15493f192b5cSAlexey Dobriyan 15503fcde74bSKirill Korotaev free_percpu(tbl->stats); 15513fcde74bSKirill Korotaev tbl->stats = NULL; 15523fcde74bSKirill Korotaev 1553bfb85c9fSRandy Dunlap kmem_cache_destroy(tbl->kmem_cachep); 1554bfb85c9fSRandy Dunlap tbl->kmem_cachep = NULL; 1555bfb85c9fSRandy Dunlap 15561da177e4SLinus Torvalds return 0; 15571da177e4SLinus Torvalds } 15580a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear); 15591da177e4SLinus Torvalds 1560c8822a4eSThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 15611da177e4SLinus Torvalds { 15623b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1563a14a49d2SThomas Graf struct ndmsg *ndm; 1564a14a49d2SThomas Graf struct nlattr *dst_attr; 15651da177e4SLinus Torvalds struct neigh_table *tbl; 15661da177e4SLinus Torvalds struct net_device *dev = NULL; 1567a14a49d2SThomas Graf int err = -EINVAL; 15681da177e4SLinus Torvalds 1569110b2499SEric Dumazet ASSERT_RTNL(); 1570a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 15711da177e4SLinus Torvalds goto out; 15721da177e4SLinus Torvalds 1573a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1574a14a49d2SThomas Graf if (dst_attr == NULL) 1575a14a49d2SThomas Graf goto out; 1576a14a49d2SThomas Graf 1577a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1578a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1579110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 1580a14a49d2SThomas Graf if (dev == NULL) { 1581a14a49d2SThomas Graf err = -ENODEV; 1582a14a49d2SThomas Graf goto out; 1583a14a49d2SThomas Graf } 1584a14a49d2SThomas Graf } 1585a14a49d2SThomas Graf 15861da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 15871da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1588a14a49d2SThomas Graf struct neighbour *neigh; 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 15911da177e4SLinus Torvalds continue; 15921da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 15931da177e4SLinus Torvalds 1594a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 1595110b2499SEric Dumazet goto out; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1598426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 1599110b2499SEric Dumazet goto out; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 1602a14a49d2SThomas Graf if (dev == NULL) 1603110b2499SEric Dumazet goto out; 16041da177e4SLinus Torvalds 1605a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1606a14a49d2SThomas Graf if (neigh == NULL) { 1607a14a49d2SThomas Graf err = -ENOENT; 1608110b2499SEric Dumazet goto out; 1609a14a49d2SThomas Graf } 1610a14a49d2SThomas Graf 1611a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 16121da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 16131da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1614a14a49d2SThomas Graf neigh_release(neigh); 1615110b2499SEric Dumazet goto out; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 1618a14a49d2SThomas Graf err = -EAFNOSUPPORT; 1619a14a49d2SThomas Graf 16201da177e4SLinus Torvalds out: 16211da177e4SLinus Torvalds return err; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 1624c8822a4eSThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 16251da177e4SLinus Torvalds { 16263b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 16275208debdSThomas Graf struct ndmsg *ndm; 16285208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 16291da177e4SLinus Torvalds struct neigh_table *tbl; 16301da177e4SLinus Torvalds struct net_device *dev = NULL; 16315208debdSThomas Graf int err; 16321da177e4SLinus Torvalds 1633110b2499SEric Dumazet ASSERT_RTNL(); 16345208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 16355208debdSThomas Graf if (err < 0) 16361da177e4SLinus Torvalds goto out; 16371da177e4SLinus Torvalds 16385208debdSThomas Graf err = -EINVAL; 16395208debdSThomas Graf if (tb[NDA_DST] == NULL) 16405208debdSThomas Graf goto out; 16415208debdSThomas Graf 16425208debdSThomas Graf ndm = nlmsg_data(nlh); 16435208debdSThomas Graf if (ndm->ndm_ifindex) { 1644110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 16455208debdSThomas Graf if (dev == NULL) { 16465208debdSThomas Graf err = -ENODEV; 16475208debdSThomas Graf goto out; 16485208debdSThomas Graf } 16495208debdSThomas Graf 16505208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 1651110b2499SEric Dumazet goto out; 16525208debdSThomas Graf } 16535208debdSThomas Graf 16541da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 16551da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 16565208debdSThomas Graf int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 16575208debdSThomas Graf struct neighbour *neigh; 16585208debdSThomas Graf void *dst, *lladdr; 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 16611da177e4SLinus Torvalds continue; 16621da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16631da177e4SLinus Torvalds 16645208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 1665110b2499SEric Dumazet goto out; 16665208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 16675208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 167062dd9318SVille Nuorvala struct pneigh_entry *pn; 167162dd9318SVille Nuorvala 16725208debdSThomas Graf err = -ENOBUFS; 1673426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 167462dd9318SVille Nuorvala if (pn) { 167562dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 167662dd9318SVille Nuorvala err = 0; 167762dd9318SVille Nuorvala } 1678110b2499SEric Dumazet goto out; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 16815208debdSThomas Graf if (dev == NULL) 1682110b2499SEric Dumazet goto out; 16831da177e4SLinus Torvalds 16845208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 16855208debdSThomas Graf if (neigh == NULL) { 16865208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 16871da177e4SLinus Torvalds err = -ENOENT; 1688110b2499SEric Dumazet goto out; 16895208debdSThomas Graf } 16905208debdSThomas Graf 16915208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 16925208debdSThomas Graf if (IS_ERR(neigh)) { 16935208debdSThomas Graf err = PTR_ERR(neigh); 1694110b2499SEric Dumazet goto out; 16951da177e4SLinus Torvalds } 16965208debdSThomas Graf } else { 16975208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 16985208debdSThomas Graf err = -EEXIST; 16995208debdSThomas Graf neigh_release(neigh); 1700110b2499SEric Dumazet goto out; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17035208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 17045208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 17055208debdSThomas Graf } 17061da177e4SLinus Torvalds 17070c5c2d30SEric Biederman if (ndm->ndm_flags & NTF_USE) { 17080c5c2d30SEric Biederman neigh_event_send(neigh, NULL); 17090c5c2d30SEric Biederman err = 0; 17100c5c2d30SEric Biederman } else 17115208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 17125208debdSThomas Graf neigh_release(neigh); 1713110b2499SEric Dumazet goto out; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17175208debdSThomas Graf err = -EAFNOSUPPORT; 17181da177e4SLinus Torvalds out: 17191da177e4SLinus Torvalds return err; 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 1722c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1723c7fb64dbSThomas Graf { 1724ca860fb3SThomas Graf struct nlattr *nest; 1725e386c6ebSThomas Graf 1726ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1727ca860fb3SThomas Graf if (nest == NULL) 1728ca860fb3SThomas Graf return -ENOBUFS; 1729c7fb64dbSThomas Graf 1730c7fb64dbSThomas Graf if (parms->dev) 1731ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex); 1732c7fb64dbSThomas Graf 1733ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)); 1734ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len); 1735ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen); 1736ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes); 1737ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes); 1738ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes); 1739ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time); 1740ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME, 1741c7fb64dbSThomas Graf parms->base_reachable_time); 1742ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime); 1743ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time); 1744ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time); 1745ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay); 1746ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay); 1747ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime); 1748c7fb64dbSThomas Graf 1749ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1750c7fb64dbSThomas Graf 1751ca860fb3SThomas Graf nla_put_failure: 1752bc3ed28cSThomas Graf nla_nest_cancel(skb, nest); 1753bc3ed28cSThomas Graf return -EMSGSIZE; 1754c7fb64dbSThomas Graf } 1755c7fb64dbSThomas Graf 1756ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1757ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1758c7fb64dbSThomas Graf { 1759c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1760c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1761c7fb64dbSThomas Graf 1762ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1763ca860fb3SThomas Graf if (nlh == NULL) 176426932566SPatrick McHardy return -EMSGSIZE; 1765c7fb64dbSThomas Graf 1766ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1767c7fb64dbSThomas Graf 1768c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1769c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 17709ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 17719ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1772c7fb64dbSThomas Graf 1773ca860fb3SThomas Graf NLA_PUT_STRING(skb, NDTA_NAME, tbl->id); 1774ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval); 1775ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1); 1776ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2); 1777ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3); 1778c7fb64dbSThomas Graf 1779c7fb64dbSThomas Graf { 1780c7fb64dbSThomas Graf unsigned long now = jiffies; 1781c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1782c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1783d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1784c7fb64dbSThomas Graf struct ndt_config ndc = { 1785c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1786c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1787c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1788c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1789c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1790c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1791c7fb64dbSThomas Graf }; 1792c7fb64dbSThomas Graf 1793d6bf7817SEric Dumazet rcu_read_lock_bh(); 1794d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 1795d6bf7817SEric Dumazet ndc.ndtc_hash_rnd = nht->hash_rnd; 1796d6bf7817SEric Dumazet ndc.ndtc_hash_mask = nht->hash_mask; 1797d6bf7817SEric Dumazet rcu_read_unlock_bh(); 1798d6bf7817SEric Dumazet 1799ca860fb3SThomas Graf NLA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc); 1800c7fb64dbSThomas Graf } 1801c7fb64dbSThomas Graf 1802c7fb64dbSThomas Graf { 1803c7fb64dbSThomas Graf int cpu; 1804c7fb64dbSThomas Graf struct ndt_stats ndst; 1805c7fb64dbSThomas Graf 1806c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1807c7fb64dbSThomas Graf 18086f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1809c7fb64dbSThomas Graf struct neigh_statistics *st; 1810c7fb64dbSThomas Graf 1811c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1812c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1813c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1814c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1815c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1816c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1817c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1818c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1819c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1820c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1821c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1822c7fb64dbSThomas Graf } 1823c7fb64dbSThomas Graf 1824ca860fb3SThomas Graf NLA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst); 1825c7fb64dbSThomas Graf } 1826c7fb64dbSThomas Graf 1827c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1828c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1829ca860fb3SThomas Graf goto nla_put_failure; 1830c7fb64dbSThomas Graf 1831c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1832ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1833c7fb64dbSThomas Graf 1834ca860fb3SThomas Graf nla_put_failure: 1835c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 183626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 183726932566SPatrick McHardy return -EMSGSIZE; 1838c7fb64dbSThomas Graf } 1839c7fb64dbSThomas Graf 1840ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1841ca860fb3SThomas Graf struct neigh_table *tbl, 1842c7fb64dbSThomas Graf struct neigh_parms *parms, 1843ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1844ca860fb3SThomas Graf unsigned int flags) 1845c7fb64dbSThomas Graf { 1846c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1847c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1848c7fb64dbSThomas Graf 1849ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1850ca860fb3SThomas Graf if (nlh == NULL) 185126932566SPatrick McHardy return -EMSGSIZE; 1852c7fb64dbSThomas Graf 1853ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1854c7fb64dbSThomas Graf 1855c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1856c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 18579ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 18589ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1859c7fb64dbSThomas Graf 1860ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1861ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1862ca860fb3SThomas Graf goto errout; 1863c7fb64dbSThomas Graf 1864c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1865ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1866ca860fb3SThomas Graf errout: 1867c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 186826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 186926932566SPatrick McHardy return -EMSGSIZE; 1870c7fb64dbSThomas Graf } 1871c7fb64dbSThomas Graf 1872ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 18736b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 18746b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 18756b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 18766b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 18776b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 18786b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 18796b3f8674SThomas Graf }; 18806b3f8674SThomas Graf 1881ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 18826b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 18836b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 18846b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 18856b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 18866b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 18876b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 18886b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 18896b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 18906b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 18916b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 18926b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 18936b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 18946b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 18956b3f8674SThomas Graf }; 18966b3f8674SThomas Graf 1897c8822a4eSThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1898c7fb64dbSThomas Graf { 18993b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1900c7fb64dbSThomas Graf struct neigh_table *tbl; 19016b3f8674SThomas Graf struct ndtmsg *ndtmsg; 19026b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 19036b3f8674SThomas Graf int err; 1904c7fb64dbSThomas Graf 19056b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 19066b3f8674SThomas Graf nl_neightbl_policy); 19076b3f8674SThomas Graf if (err < 0) 19086b3f8674SThomas Graf goto errout; 1909c7fb64dbSThomas Graf 19106b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 19116b3f8674SThomas Graf err = -EINVAL; 19126b3f8674SThomas Graf goto errout; 19136b3f8674SThomas Graf } 19146b3f8674SThomas Graf 19156b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1916c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1917c7fb64dbSThomas Graf for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1918c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1919c7fb64dbSThomas Graf continue; 1920c7fb64dbSThomas Graf 19216b3f8674SThomas Graf if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) 1922c7fb64dbSThomas Graf break; 1923c7fb64dbSThomas Graf } 1924c7fb64dbSThomas Graf 1925c7fb64dbSThomas Graf if (tbl == NULL) { 1926c7fb64dbSThomas Graf err = -ENOENT; 19276b3f8674SThomas Graf goto errout_locked; 1928c7fb64dbSThomas Graf } 1929c7fb64dbSThomas Graf 1930c7fb64dbSThomas Graf /* 1931c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1932c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1933c7fb64dbSThomas Graf */ 1934c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1935c7fb64dbSThomas Graf 19366b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 19376b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 1938c7fb64dbSThomas Graf struct neigh_parms *p; 19396b3f8674SThomas Graf int i, ifindex = 0; 1940c7fb64dbSThomas Graf 19416b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 19426b3f8674SThomas Graf nl_ntbl_parm_policy); 19436b3f8674SThomas Graf if (err < 0) 19446b3f8674SThomas Graf goto errout_tbl_lock; 1945c7fb64dbSThomas Graf 19466b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 19476b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 1948c7fb64dbSThomas Graf 194997fd5bc7STobias Klauser p = lookup_neigh_parms(tbl, net, ifindex); 1950c7fb64dbSThomas Graf if (p == NULL) { 1951c7fb64dbSThomas Graf err = -ENOENT; 19526b3f8674SThomas Graf goto errout_tbl_lock; 1953c7fb64dbSThomas Graf } 1954c7fb64dbSThomas Graf 19556b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 19566b3f8674SThomas Graf if (tbp[i] == NULL) 19576b3f8674SThomas Graf continue; 1958c7fb64dbSThomas Graf 19596b3f8674SThomas Graf switch (i) { 19606b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 19616b3f8674SThomas Graf p->queue_len = nla_get_u32(tbp[i]); 19626b3f8674SThomas Graf break; 19636b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 19646b3f8674SThomas Graf p->proxy_qlen = nla_get_u32(tbp[i]); 19656b3f8674SThomas Graf break; 19666b3f8674SThomas Graf case NDTPA_APP_PROBES: 19676b3f8674SThomas Graf p->app_probes = nla_get_u32(tbp[i]); 19686b3f8674SThomas Graf break; 19696b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 19706b3f8674SThomas Graf p->ucast_probes = nla_get_u32(tbp[i]); 19716b3f8674SThomas Graf break; 19726b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 19736b3f8674SThomas Graf p->mcast_probes = nla_get_u32(tbp[i]); 19746b3f8674SThomas Graf break; 19756b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 19766b3f8674SThomas Graf p->base_reachable_time = nla_get_msecs(tbp[i]); 19776b3f8674SThomas Graf break; 19786b3f8674SThomas Graf case NDTPA_GC_STALETIME: 19796b3f8674SThomas Graf p->gc_staletime = nla_get_msecs(tbp[i]); 19806b3f8674SThomas Graf break; 19816b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 19826b3f8674SThomas Graf p->delay_probe_time = nla_get_msecs(tbp[i]); 19836b3f8674SThomas Graf break; 19846b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 19856b3f8674SThomas Graf p->retrans_time = nla_get_msecs(tbp[i]); 19866b3f8674SThomas Graf break; 19876b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 19886b3f8674SThomas Graf p->anycast_delay = nla_get_msecs(tbp[i]); 19896b3f8674SThomas Graf break; 19906b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 19916b3f8674SThomas Graf p->proxy_delay = nla_get_msecs(tbp[i]); 19926b3f8674SThomas Graf break; 19936b3f8674SThomas Graf case NDTPA_LOCKTIME: 19946b3f8674SThomas Graf p->locktime = nla_get_msecs(tbp[i]); 19956b3f8674SThomas Graf break; 1996c7fb64dbSThomas Graf } 19976b3f8674SThomas Graf } 19986b3f8674SThomas Graf } 19996b3f8674SThomas Graf 20006b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 20016b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 20026b3f8674SThomas Graf 20036b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 20046b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 20056b3f8674SThomas Graf 20066b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 20076b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 20086b3f8674SThomas Graf 20096b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 20106b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 2011c7fb64dbSThomas Graf 2012c7fb64dbSThomas Graf err = 0; 2013c7fb64dbSThomas Graf 20146b3f8674SThomas Graf errout_tbl_lock: 2015c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 20166b3f8674SThomas Graf errout_locked: 2017c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 20186b3f8674SThomas Graf errout: 2019c7fb64dbSThomas Graf return err; 2020c7fb64dbSThomas Graf } 2021c7fb64dbSThomas Graf 2022c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 2023c7fb64dbSThomas Graf { 20243b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2025ca860fb3SThomas Graf int family, tidx, nidx = 0; 2026ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 2027ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 2028c7fb64dbSThomas Graf struct neigh_table *tbl; 2029c7fb64dbSThomas Graf 2030ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 2031c7fb64dbSThomas Graf 2032c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 2033ca860fb3SThomas Graf for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) { 2034c7fb64dbSThomas Graf struct neigh_parms *p; 2035c7fb64dbSThomas Graf 2036ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 2037c7fb64dbSThomas Graf continue; 2038c7fb64dbSThomas Graf 2039ca860fb3SThomas Graf if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid, 2040ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 2041ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2042c7fb64dbSThomas Graf break; 2043c7fb64dbSThomas Graf 2044426b5303SEric W. Biederman for (nidx = 0, p = tbl->parms.next; p; p = p->next) { 2045878628fbSYOSHIFUJI Hideaki if (!net_eq(neigh_parms_net(p), net)) 2046426b5303SEric W. Biederman continue; 2047426b5303SEric W. Biederman 2048efc683fcSGautam Kachroo if (nidx < neigh_skip) 2049efc683fcSGautam Kachroo goto next; 2050c7fb64dbSThomas Graf 2051ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 2052ca860fb3SThomas Graf NETLINK_CB(cb->skb).pid, 2053ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 2054ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 2055ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2056c7fb64dbSThomas Graf goto out; 2057efc683fcSGautam Kachroo next: 2058efc683fcSGautam Kachroo nidx++; 2059c7fb64dbSThomas Graf } 2060c7fb64dbSThomas Graf 2061ca860fb3SThomas Graf neigh_skip = 0; 2062c7fb64dbSThomas Graf } 2063c7fb64dbSThomas Graf out: 2064c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 2065ca860fb3SThomas Graf cb->args[0] = tidx; 2066ca860fb3SThomas Graf cb->args[1] = nidx; 2067c7fb64dbSThomas Graf 2068c7fb64dbSThomas Graf return skb->len; 2069c7fb64dbSThomas Graf } 20701da177e4SLinus Torvalds 20718b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 20728b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 20731da177e4SLinus Torvalds { 20741da177e4SLinus Torvalds unsigned long now = jiffies; 20751da177e4SLinus Torvalds struct nda_cacheinfo ci; 20768b8aec50SThomas Graf struct nlmsghdr *nlh; 20778b8aec50SThomas Graf struct ndmsg *ndm; 20781da177e4SLinus Torvalds 20798b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 20808b8aec50SThomas Graf if (nlh == NULL) 208126932566SPatrick McHardy return -EMSGSIZE; 20828b8aec50SThomas Graf 20838b8aec50SThomas Graf ndm = nlmsg_data(nlh); 20848b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 20859ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 20869ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 20878b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 20888b8aec50SThomas Graf ndm->ndm_type = neigh->type; 20898b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 20901da177e4SLinus Torvalds 20918b8aec50SThomas Graf NLA_PUT(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key); 20928b8aec50SThomas Graf 20938b8aec50SThomas Graf read_lock_bh(&neigh->lock); 20948b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 20958b8aec50SThomas Graf if ((neigh->nud_state & NUD_VALID) && 20968b8aec50SThomas Graf nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, neigh->ha) < 0) { 20978b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 20988b8aec50SThomas Graf goto nla_put_failure; 20998b8aec50SThomas Graf } 21008b8aec50SThomas Graf 2101b9f5f52cSStephen Hemminger ci.ndm_used = jiffies_to_clock_t(now - neigh->used); 2102b9f5f52cSStephen Hemminger ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); 2103b9f5f52cSStephen Hemminger ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); 21048b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 21058b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21068b8aec50SThomas Graf 21078b8aec50SThomas Graf NLA_PUT_U32(skb, NDA_PROBES, atomic_read(&neigh->probes)); 21088b8aec50SThomas Graf NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); 21098b8aec50SThomas Graf 21108b8aec50SThomas Graf return nlmsg_end(skb, nlh); 21118b8aec50SThomas Graf 21128b8aec50SThomas Graf nla_put_failure: 211326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 211426932566SPatrick McHardy return -EMSGSIZE; 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds 2117d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2118d961db35SThomas Graf { 2119d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2120d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2121d961db35SThomas Graf } 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 21241da177e4SLinus Torvalds struct netlink_callback *cb) 21251da177e4SLinus Torvalds { 21263b1e0a65SYOSHIFUJI Hideaki struct net * net = sock_net(skb->sk); 21271da177e4SLinus Torvalds struct neighbour *n; 21281da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 21291da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 2130d6bf7817SEric Dumazet struct neigh_hash_table *nht; 21311da177e4SLinus Torvalds 2132d6bf7817SEric Dumazet rcu_read_lock_bh(); 2133d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2134d6bf7817SEric Dumazet 2135d6bf7817SEric Dumazet read_lock(&tbl->lock); 2136d6bf7817SEric Dumazet for (h = 0; h <= nht->hash_mask; h++) { 21371da177e4SLinus Torvalds if (h < s_h) 21381da177e4SLinus Torvalds continue; 21391da177e4SLinus Torvalds if (h > s_h) 21401da177e4SLinus Torvalds s_idx = 0; 2141d6bf7817SEric Dumazet for (n = nht->hash_buckets[h], idx = 0; n; n = n->next) { 214209ad9bc7SOctavian Purdila if (!net_eq(dev_net(n->dev), net)) 2143426b5303SEric W. Biederman continue; 2144efc683fcSGautam Kachroo if (idx < s_idx) 2145efc683fcSGautam Kachroo goto next; 21461da177e4SLinus Torvalds if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid, 21471da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2148b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 2149b6544c0bSJamal Hadi Salim NLM_F_MULTI) <= 0) { 21501da177e4SLinus Torvalds rc = -1; 21511da177e4SLinus Torvalds goto out; 21521da177e4SLinus Torvalds } 2153efc683fcSGautam Kachroo next: 2154efc683fcSGautam Kachroo idx++; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds rc = skb->len; 21581da177e4SLinus Torvalds out: 2159d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2160d6bf7817SEric Dumazet rcu_read_unlock_bh(); 21611da177e4SLinus Torvalds cb->args[1] = h; 21621da177e4SLinus Torvalds cb->args[2] = idx; 21631da177e4SLinus Torvalds return rc; 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds 2166c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 21671da177e4SLinus Torvalds { 21681da177e4SLinus Torvalds struct neigh_table *tbl; 21691da177e4SLinus Torvalds int t, family, s_t; 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 21728b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 21731da177e4SLinus Torvalds s_t = cb->args[0]; 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) { 21761da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 21771da177e4SLinus Torvalds continue; 21781da177e4SLinus Torvalds if (t > s_t) 21791da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 21801da177e4SLinus Torvalds sizeof(cb->args[0])); 21811da177e4SLinus Torvalds if (neigh_dump_table(tbl, skb, cb) < 0) 21821da177e4SLinus Torvalds break; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds cb->args[0] = t; 21871da177e4SLinus Torvalds return skb->len; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds int chain; 2193d6bf7817SEric Dumazet struct neigh_hash_table *nht; 21941da177e4SLinus Torvalds 2195d6bf7817SEric Dumazet rcu_read_lock_bh(); 2196d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2197d6bf7817SEric Dumazet 2198d6bf7817SEric Dumazet read_lock(&tbl->lock); 2199d6bf7817SEric Dumazet for (chain = 0; chain <= nht->hash_mask; chain++) { 22001da177e4SLinus Torvalds struct neighbour *n; 22011da177e4SLinus Torvalds 2202d6bf7817SEric Dumazet for (n = nht->hash_buckets[chain]; n; n = n->next) 22031da177e4SLinus Torvalds cb(n, cookie); 22041da177e4SLinus Torvalds } 2205d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2206d6bf7817SEric Dumazet rcu_read_unlock_bh(); 22071da177e4SLinus Torvalds } 22081da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 22111da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 22121da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 22131da177e4SLinus Torvalds { 22141da177e4SLinus Torvalds int chain; 2215d6bf7817SEric Dumazet struct neigh_hash_table *nht; 22161da177e4SLinus Torvalds 2217d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 2218d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 2219d6bf7817SEric Dumazet for (chain = 0; chain <= nht->hash_mask; chain++) { 22201da177e4SLinus Torvalds struct neighbour *n, **np; 22211da177e4SLinus Torvalds 2222d6bf7817SEric Dumazet np = &nht->hash_buckets[chain]; 22231da177e4SLinus Torvalds while ((n = *np) != NULL) { 22241da177e4SLinus Torvalds int release; 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds write_lock(&n->lock); 22271da177e4SLinus Torvalds release = cb(n); 22281da177e4SLinus Torvalds if (release) { 22291da177e4SLinus Torvalds *np = n->next; 22301da177e4SLinus Torvalds n->dead = 1; 22311da177e4SLinus Torvalds } else 22321da177e4SLinus Torvalds np = &n->next; 22331da177e4SLinus Torvalds write_unlock(&n->lock); 22344f494554SThomas Graf if (release) 22354f494554SThomas Graf neigh_cleanup_and_release(n); 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds } 2238ecbb4169SAlexey Kuznetsov } 22391da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 22441da177e4SLinus Torvalds { 22451da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 22461218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2247d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 22481da177e4SLinus Torvalds struct neighbour *n = NULL; 22491da177e4SLinus Torvalds int bucket = state->bucket; 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 2252d6bf7817SEric Dumazet for (bucket = 0; bucket <= nht->hash_mask; bucket++) { 2253d6bf7817SEric Dumazet n = nht->hash_buckets[bucket]; 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds while (n) { 2256878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2257426b5303SEric W. Biederman goto next; 22581da177e4SLinus Torvalds if (state->neigh_sub_iter) { 22591da177e4SLinus Torvalds loff_t fakep = 0; 22601da177e4SLinus Torvalds void *v; 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 22631da177e4SLinus Torvalds if (!v) 22641da177e4SLinus Torvalds goto next; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 22671da177e4SLinus Torvalds break; 22681da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 22691da177e4SLinus Torvalds break; 22701da177e4SLinus Torvalds next: 22711da177e4SLinus Torvalds n = n->next; 22721da177e4SLinus Torvalds } 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds if (n) 22751da177e4SLinus Torvalds break; 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds state->bucket = bucket; 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds return n; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 22831da177e4SLinus Torvalds struct neighbour *n, 22841da177e4SLinus Torvalds loff_t *pos) 22851da177e4SLinus Torvalds { 22861da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 22871218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2288d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds if (state->neigh_sub_iter) { 22911da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 22921da177e4SLinus Torvalds if (v) 22931da177e4SLinus Torvalds return n; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds n = n->next; 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds while (1) { 22981da177e4SLinus Torvalds while (n) { 2299878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2300426b5303SEric W. Biederman goto next; 23011da177e4SLinus Torvalds if (state->neigh_sub_iter) { 23021da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 23031da177e4SLinus Torvalds if (v) 23041da177e4SLinus Torvalds return n; 23051da177e4SLinus Torvalds goto next; 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 23081da177e4SLinus Torvalds break; 23091da177e4SLinus Torvalds 23101da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 23111da177e4SLinus Torvalds break; 23121da177e4SLinus Torvalds next: 23131da177e4SLinus Torvalds n = n->next; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds if (n) 23171da177e4SLinus Torvalds break; 23181da177e4SLinus Torvalds 2319d6bf7817SEric Dumazet if (++state->bucket > nht->hash_mask) 23201da177e4SLinus Torvalds break; 23211da177e4SLinus Torvalds 2322d6bf7817SEric Dumazet n = nht->hash_buckets[state->bucket]; 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds 23251da177e4SLinus Torvalds if (n && pos) 23261da177e4SLinus Torvalds --(*pos); 23271da177e4SLinus Torvalds return n; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 23311da177e4SLinus Torvalds { 23321da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds if (n) { 2335745e2031SChris Larson --(*pos); 23361da177e4SLinus Torvalds while (*pos) { 23371da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 23381da177e4SLinus Torvalds if (!n) 23391da177e4SLinus Torvalds break; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds return *pos ? NULL : n; 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 23461da177e4SLinus Torvalds { 23471da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23481218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 23491da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 23501da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 23511da177e4SLinus Torvalds int bucket = state->bucket; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 23541da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 23551da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2356878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2357426b5303SEric W. Biederman pn = pn->next; 23581da177e4SLinus Torvalds if (pn) 23591da177e4SLinus Torvalds break; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds state->bucket = bucket; 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds return pn; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 23671da177e4SLinus Torvalds struct pneigh_entry *pn, 23681da177e4SLinus Torvalds loff_t *pos) 23691da177e4SLinus Torvalds { 23701da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23711218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 23721da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds pn = pn->next; 23751da177e4SLinus Torvalds while (!pn) { 23761da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 23771da177e4SLinus Torvalds break; 23781da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2379878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2380426b5303SEric W. Biederman pn = pn->next; 23811da177e4SLinus Torvalds if (pn) 23821da177e4SLinus Torvalds break; 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds if (pn && pos) 23861da177e4SLinus Torvalds --(*pos); 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds return pn; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 23921da177e4SLinus Torvalds { 23931da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds if (pn) { 2396745e2031SChris Larson --(*pos); 23971da177e4SLinus Torvalds while (*pos) { 23981da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 23991da177e4SLinus Torvalds if (!pn) 24001da177e4SLinus Torvalds break; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds return *pos ? NULL : pn; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 24071da177e4SLinus Torvalds { 24081da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24091da177e4SLinus Torvalds void *rc; 2410745e2031SChris Larson loff_t idxpos = *pos; 24111da177e4SLinus Torvalds 2412745e2031SChris Larson rc = neigh_get_idx(seq, &idxpos); 24131da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 2414745e2031SChris Larson rc = pneigh_get_idx(seq, &idxpos); 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds return rc; 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 24209a429c49SEric Dumazet __acquires(tbl->lock) 2421d6bf7817SEric Dumazet __acquires(rcu_bh) 24221da177e4SLinus Torvalds { 24231da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds state->tbl = tbl; 24261da177e4SLinus Torvalds state->bucket = 0; 24271da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 24281da177e4SLinus Torvalds 2429d6bf7817SEric Dumazet rcu_read_lock_bh(); 2430d6bf7817SEric Dumazet state->nht = rcu_dereference_bh(tbl->nht); 2431d6bf7817SEric Dumazet read_lock(&tbl->lock); 2432745e2031SChris Larson return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 24371da177e4SLinus Torvalds { 24381da177e4SLinus Torvalds struct neigh_seq_state *state; 24391da177e4SLinus Torvalds void *rc; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2442bff69732SChris Larson rc = neigh_get_first(seq); 24431da177e4SLinus Torvalds goto out; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds state = seq->private; 24471da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 24481da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 24491da177e4SLinus Torvalds if (rc) 24501da177e4SLinus Torvalds goto out; 24511da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 24521da177e4SLinus Torvalds rc = pneigh_get_first(seq); 24531da177e4SLinus Torvalds } else { 24541da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 24551da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 24561da177e4SLinus Torvalds } 24571da177e4SLinus Torvalds out: 24581da177e4SLinus Torvalds ++(*pos); 24591da177e4SLinus Torvalds return rc; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 24621da177e4SLinus Torvalds 24631da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 24649a429c49SEric Dumazet __releases(tbl->lock) 2465d6bf7817SEric Dumazet __releases(rcu_bh) 24661da177e4SLinus Torvalds { 24671da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24681da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 24691da177e4SLinus Torvalds 2470d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2471d6bf7817SEric Dumazet rcu_read_unlock_bh(); 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds /* statistics via seq_file */ 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 24781da177e4SLinus Torvalds { 247981c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 24801da177e4SLinus Torvalds int cpu; 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds if (*pos == 0) 24831da177e4SLinus Torvalds return SEQ_START_TOKEN; 24841da177e4SLinus Torvalds 24850f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 24861da177e4SLinus Torvalds if (!cpu_possible(cpu)) 24871da177e4SLinus Torvalds continue; 24881da177e4SLinus Torvalds *pos = cpu+1; 24891da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds return NULL; 24921da177e4SLinus Torvalds } 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 24951da177e4SLinus Torvalds { 249681c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 24971da177e4SLinus Torvalds int cpu; 24981da177e4SLinus Torvalds 24990f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 25001da177e4SLinus Torvalds if (!cpu_possible(cpu)) 25011da177e4SLinus Torvalds continue; 25021da177e4SLinus Torvalds *pos = cpu+1; 25031da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds return NULL; 25061da177e4SLinus Torvalds } 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 25091da177e4SLinus Torvalds { 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 25141da177e4SLinus Torvalds { 251581c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 25161da177e4SLinus Torvalds struct neigh_statistics *st = v; 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 25199a6d276eSNeil Horman seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards\n"); 25201da177e4SLinus Torvalds return 0; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 25249a6d276eSNeil Horman "%08lx %08lx %08lx %08lx %08lx\n", 25251da177e4SLinus Torvalds atomic_read(&tbl->entries), 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds st->allocs, 25281da177e4SLinus Torvalds st->destroys, 25291da177e4SLinus Torvalds st->hash_grows, 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds st->lookups, 25321da177e4SLinus Torvalds st->hits, 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds st->res_failed, 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds st->rcv_probes_mcast, 25371da177e4SLinus Torvalds st->rcv_probes_ucast, 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds st->periodic_gc_runs, 25409a6d276eSNeil Horman st->forced_gc_runs, 25419a6d276eSNeil Horman st->unres_discards 25421da177e4SLinus Torvalds ); 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds return 0; 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds 2547f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 25481da177e4SLinus Torvalds .start = neigh_stat_seq_start, 25491da177e4SLinus Torvalds .next = neigh_stat_seq_next, 25501da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 25511da177e4SLinus Torvalds .show = neigh_stat_seq_show, 25521da177e4SLinus Torvalds }; 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 25551da177e4SLinus Torvalds { 25561da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds if (!ret) { 25591da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 256081c1ebfcSAlexey Dobriyan sf->private = PDE(inode)->data; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds return ret; 25631da177e4SLinus Torvalds }; 25641da177e4SLinus Torvalds 25659a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 25661da177e4SLinus Torvalds .owner = THIS_MODULE, 25671da177e4SLinus Torvalds .open = neigh_stat_seq_open, 25681da177e4SLinus Torvalds .read = seq_read, 25691da177e4SLinus Torvalds .llseek = seq_lseek, 25701da177e4SLinus Torvalds .release = seq_release, 25711da177e4SLinus Torvalds }; 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 25741da177e4SLinus Torvalds 2575339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2576339bf98fSThomas Graf { 2577339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2578339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2579339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2580339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2581339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2582339bf98fSThomas Graf } 2583339bf98fSThomas Graf 2584b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 25851da177e4SLinus Torvalds { 2586c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(n->dev); 25878b8aec50SThomas Graf struct sk_buff *skb; 2588b8673311SThomas Graf int err = -ENOBUFS; 25891da177e4SLinus Torvalds 2590339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 25918b8aec50SThomas Graf if (skb == NULL) 2592b8673311SThomas Graf goto errout; 25931da177e4SLinus Torvalds 2594b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 259526932566SPatrick McHardy if (err < 0) { 259626932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 259726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 259826932566SPatrick McHardy kfree_skb(skb); 259926932566SPatrick McHardy goto errout; 260026932566SPatrick McHardy } 26011ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 26021ce85fe4SPablo Neira Ayuso return; 2603b8673311SThomas Graf errout: 2604b8673311SThomas Graf if (err < 0) 2605426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2606b8673311SThomas Graf } 2607b8673311SThomas Graf 2608d961db35SThomas Graf #ifdef CONFIG_ARPD 2609b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2610b8673311SThomas Graf { 2611b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 26128b8aec50SThomas Graf } 26130a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns); 26141da177e4SLinus Torvalds #endif /* CONFIG_ARPD */ 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 26171da177e4SLinus Torvalds 261854716e3bSEric W. Biederman #define NEIGH_VARS_MAX 19 261954716e3bSEric W. Biederman 26201da177e4SLinus Torvalds static struct neigh_sysctl_table { 26211da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 262254716e3bSEric W. Biederman struct ctl_table neigh_vars[NEIGH_VARS_MAX]; 2623c3bac5a7SPavel Emelyanov char *dev_name; 2624ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 26251da177e4SLinus Torvalds .neigh_vars = { 26261da177e4SLinus Torvalds { 26271da177e4SLinus Torvalds .procname = "mcast_solicit", 26281da177e4SLinus Torvalds .maxlen = sizeof(int), 26291da177e4SLinus Torvalds .mode = 0644, 26306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26311da177e4SLinus Torvalds }, 26321da177e4SLinus Torvalds { 26331da177e4SLinus Torvalds .procname = "ucast_solicit", 26341da177e4SLinus Torvalds .maxlen = sizeof(int), 26351da177e4SLinus Torvalds .mode = 0644, 26366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26371da177e4SLinus Torvalds }, 26381da177e4SLinus Torvalds { 26391da177e4SLinus Torvalds .procname = "app_solicit", 26401da177e4SLinus Torvalds .maxlen = sizeof(int), 26411da177e4SLinus Torvalds .mode = 0644, 26426d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26431da177e4SLinus Torvalds }, 26441da177e4SLinus Torvalds { 26451da177e4SLinus Torvalds .procname = "retrans_time", 26461da177e4SLinus Torvalds .maxlen = sizeof(int), 26471da177e4SLinus Torvalds .mode = 0644, 26486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26491da177e4SLinus Torvalds }, 26501da177e4SLinus Torvalds { 26511da177e4SLinus Torvalds .procname = "base_reachable_time", 26521da177e4SLinus Torvalds .maxlen = sizeof(int), 26531da177e4SLinus Torvalds .mode = 0644, 26546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26551da177e4SLinus Torvalds }, 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds .procname = "delay_first_probe_time", 26581da177e4SLinus Torvalds .maxlen = sizeof(int), 26591da177e4SLinus Torvalds .mode = 0644, 26606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26611da177e4SLinus Torvalds }, 26621da177e4SLinus Torvalds { 26631da177e4SLinus Torvalds .procname = "gc_stale_time", 26641da177e4SLinus Torvalds .maxlen = sizeof(int), 26651da177e4SLinus Torvalds .mode = 0644, 26666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26671da177e4SLinus Torvalds }, 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds .procname = "unres_qlen", 26701da177e4SLinus Torvalds .maxlen = sizeof(int), 26711da177e4SLinus Torvalds .mode = 0644, 26726d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26731da177e4SLinus Torvalds }, 26741da177e4SLinus Torvalds { 26751da177e4SLinus Torvalds .procname = "proxy_qlen", 26761da177e4SLinus Torvalds .maxlen = sizeof(int), 26771da177e4SLinus Torvalds .mode = 0644, 26786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26791da177e4SLinus Torvalds }, 26801da177e4SLinus Torvalds { 26811da177e4SLinus Torvalds .procname = "anycast_delay", 26821da177e4SLinus Torvalds .maxlen = sizeof(int), 26831da177e4SLinus Torvalds .mode = 0644, 26846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26851da177e4SLinus Torvalds }, 26861da177e4SLinus Torvalds { 26871da177e4SLinus Torvalds .procname = "proxy_delay", 26881da177e4SLinus Torvalds .maxlen = sizeof(int), 26891da177e4SLinus Torvalds .mode = 0644, 26906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26911da177e4SLinus Torvalds }, 26921da177e4SLinus Torvalds { 26931da177e4SLinus Torvalds .procname = "locktime", 26941da177e4SLinus Torvalds .maxlen = sizeof(int), 26951da177e4SLinus Torvalds .mode = 0644, 26966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26971da177e4SLinus Torvalds }, 26981da177e4SLinus Torvalds { 2699d12af679SEric W. Biederman .procname = "retrans_time_ms", 2700d12af679SEric W. Biederman .maxlen = sizeof(int), 2701d12af679SEric W. Biederman .mode = 0644, 27026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2703d12af679SEric W. Biederman }, 2704d12af679SEric W. Biederman { 2705d12af679SEric W. Biederman .procname = "base_reachable_time_ms", 2706d12af679SEric W. Biederman .maxlen = sizeof(int), 2707d12af679SEric W. Biederman .mode = 0644, 27086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2709d12af679SEric W. Biederman }, 2710d12af679SEric W. Biederman { 27111da177e4SLinus Torvalds .procname = "gc_interval", 27121da177e4SLinus Torvalds .maxlen = sizeof(int), 27131da177e4SLinus Torvalds .mode = 0644, 27146d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27151da177e4SLinus Torvalds }, 27161da177e4SLinus Torvalds { 27171da177e4SLinus Torvalds .procname = "gc_thresh1", 27181da177e4SLinus Torvalds .maxlen = sizeof(int), 27191da177e4SLinus Torvalds .mode = 0644, 27206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27211da177e4SLinus Torvalds }, 27221da177e4SLinus Torvalds { 27231da177e4SLinus Torvalds .procname = "gc_thresh2", 27241da177e4SLinus Torvalds .maxlen = sizeof(int), 27251da177e4SLinus Torvalds .mode = 0644, 27266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27271da177e4SLinus Torvalds }, 27281da177e4SLinus Torvalds { 27291da177e4SLinus Torvalds .procname = "gc_thresh3", 27301da177e4SLinus Torvalds .maxlen = sizeof(int), 27311da177e4SLinus Torvalds .mode = 0644, 27326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27331da177e4SLinus Torvalds }, 2734c3bac5a7SPavel Emelyanov {}, 27351da177e4SLinus Torvalds }, 27361da177e4SLinus Torvalds }; 27371da177e4SLinus Torvalds 27381da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 273954716e3bSEric W. Biederman char *p_name, proc_handler *handler) 27401da177e4SLinus Torvalds { 27413c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 27421da177e4SLinus Torvalds const char *dev_name_source = NULL; 2743c3bac5a7SPavel Emelyanov 2744c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_ROOT 0 2745c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_PROTO 1 2746c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_NEIGH 2 2747c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_DEV 3 2748c3bac5a7SPavel Emelyanov 2749c3bac5a7SPavel Emelyanov struct ctl_path neigh_path[] = { 2750f8572d8fSEric W. Biederman { .procname = "net", }, 2751f8572d8fSEric W. Biederman { .procname = "proto", }, 2752f8572d8fSEric W. Biederman { .procname = "neigh", }, 2753f8572d8fSEric W. Biederman { .procname = "default", }, 2754c3bac5a7SPavel Emelyanov { }, 2755c3bac5a7SPavel Emelyanov }; 27561da177e4SLinus Torvalds 27573c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 27581da177e4SLinus Torvalds if (!t) 27593c607bbbSPavel Emelyanov goto err; 27603c607bbbSPavel Emelyanov 27611da177e4SLinus Torvalds t->neigh_vars[0].data = &p->mcast_probes; 27621da177e4SLinus Torvalds t->neigh_vars[1].data = &p->ucast_probes; 27631da177e4SLinus Torvalds t->neigh_vars[2].data = &p->app_probes; 27641da177e4SLinus Torvalds t->neigh_vars[3].data = &p->retrans_time; 27651da177e4SLinus Torvalds t->neigh_vars[4].data = &p->base_reachable_time; 27661da177e4SLinus Torvalds t->neigh_vars[5].data = &p->delay_probe_time; 27671da177e4SLinus Torvalds t->neigh_vars[6].data = &p->gc_staletime; 27681da177e4SLinus Torvalds t->neigh_vars[7].data = &p->queue_len; 27691da177e4SLinus Torvalds t->neigh_vars[8].data = &p->proxy_qlen; 27701da177e4SLinus Torvalds t->neigh_vars[9].data = &p->anycast_delay; 27711da177e4SLinus Torvalds t->neigh_vars[10].data = &p->proxy_delay; 27721da177e4SLinus Torvalds t->neigh_vars[11].data = &p->locktime; 2773d12af679SEric W. Biederman t->neigh_vars[12].data = &p->retrans_time; 2774d12af679SEric W. Biederman t->neigh_vars[13].data = &p->base_reachable_time; 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds if (dev) { 27771da177e4SLinus Torvalds dev_name_source = dev->name; 2778d12af679SEric W. Biederman /* Terminate the table early */ 2779d12af679SEric W. Biederman memset(&t->neigh_vars[14], 0, sizeof(t->neigh_vars[14])); 27801da177e4SLinus Torvalds } else { 2781c3bac5a7SPavel Emelyanov dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname; 2782d12af679SEric W. Biederman t->neigh_vars[14].data = (int *)(p + 1); 2783d12af679SEric W. Biederman t->neigh_vars[15].data = (int *)(p + 1) + 1; 2784d12af679SEric W. Biederman t->neigh_vars[16].data = (int *)(p + 1) + 2; 2785d12af679SEric W. Biederman t->neigh_vars[17].data = (int *)(p + 1) + 3; 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds 2789f8572d8fSEric W. Biederman if (handler) { 27901da177e4SLinus Torvalds /* RetransTime */ 27911da177e4SLinus Torvalds t->neigh_vars[3].proc_handler = handler; 27921da177e4SLinus Torvalds t->neigh_vars[3].extra1 = dev; 27931da177e4SLinus Torvalds /* ReachableTime */ 27941da177e4SLinus Torvalds t->neigh_vars[4].proc_handler = handler; 27951da177e4SLinus Torvalds t->neigh_vars[4].extra1 = dev; 27961da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 2797d12af679SEric W. Biederman t->neigh_vars[12].proc_handler = handler; 2798d12af679SEric W. Biederman t->neigh_vars[12].extra1 = dev; 27991da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 2800d12af679SEric W. Biederman t->neigh_vars[13].proc_handler = handler; 2801d12af679SEric W. Biederman t->neigh_vars[13].extra1 = dev; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 2804c3bac5a7SPavel Emelyanov t->dev_name = kstrdup(dev_name_source, GFP_KERNEL); 2805c3bac5a7SPavel Emelyanov if (!t->dev_name) 28061da177e4SLinus Torvalds goto free; 28071da177e4SLinus Torvalds 2808c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name; 2809c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name; 28101da177e4SLinus Torvalds 28114ab438fcSDenis V. Lunev t->sysctl_header = 281257da52c1SYOSHIFUJI Hideaki register_net_sysctl_table(neigh_parms_net(p), neigh_path, t->neigh_vars); 28133c607bbbSPavel Emelyanov if (!t->sysctl_header) 28141da177e4SLinus Torvalds goto free_procname; 28153c607bbbSPavel Emelyanov 28161da177e4SLinus Torvalds p->sysctl_table = t; 28171da177e4SLinus Torvalds return 0; 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds free_procname: 2820c3bac5a7SPavel Emelyanov kfree(t->dev_name); 28211da177e4SLinus Torvalds free: 28221da177e4SLinus Torvalds kfree(t); 28233c607bbbSPavel Emelyanov err: 28243c607bbbSPavel Emelyanov return -ENOBUFS; 28251da177e4SLinus Torvalds } 28260a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register); 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 28291da177e4SLinus Torvalds { 28301da177e4SLinus Torvalds if (p->sysctl_table) { 28311da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 28321da177e4SLinus Torvalds p->sysctl_table = NULL; 28331da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 2834c3bac5a7SPavel Emelyanov kfree(t->dev_name); 28351da177e4SLinus Torvalds kfree(t); 28361da177e4SLinus Torvalds } 28371da177e4SLinus Torvalds } 28380a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister); 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 28411da177e4SLinus Torvalds 2842c8822a4eSThomas Graf static int __init neigh_init(void) 2843c8822a4eSThomas Graf { 2844c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL); 2845c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL); 2846c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info); 2847c8822a4eSThomas Graf 2848c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info); 2849c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL); 2850c8822a4eSThomas Graf 2851c8822a4eSThomas Graf return 0; 2852c8822a4eSThomas Graf } 2853c8822a4eSThomas Graf 2854c8822a4eSThomas Graf subsys_initcall(neigh_init); 2855c8822a4eSThomas Graf 2856