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 18e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19e005d193SJoe Perches 205a0e3ad6STejun Heo #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/kernel.h> 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/socket.h> 251da177e4SLinus Torvalds #include <linux/netdevice.h> 261da177e4SLinus Torvalds #include <linux/proc_fs.h> 271da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 281da177e4SLinus Torvalds #include <linux/sysctl.h> 291da177e4SLinus Torvalds #endif 301da177e4SLinus Torvalds #include <linux/times.h> 31457c4cbcSEric W. Biederman #include <net/net_namespace.h> 321da177e4SLinus Torvalds #include <net/neighbour.h> 331da177e4SLinus Torvalds #include <net/dst.h> 341da177e4SLinus Torvalds #include <net/sock.h> 358d71740cSTom Tucker #include <net/netevent.h> 36a14a49d2SThomas Graf #include <net/netlink.h> 371da177e4SLinus Torvalds #include <linux/rtnetlink.h> 381da177e4SLinus Torvalds #include <linux/random.h> 39543537bdSPaulo Marques #include <linux/string.h> 40c3609d51Svignesh babu #include <linux/log2.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define NEIGH_DEBUG 1 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define NEIGH_PRINTK(x...) printk(x) 451da177e4SLinus Torvalds #define NEIGH_NOPRINTK(x...) do { ; } while(0) 461da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_NOPRINTK 471da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_NOPRINTK 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #if NEIGH_DEBUG >= 1 501da177e4SLinus Torvalds #undef NEIGH_PRINTK1 511da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_PRINTK 521da177e4SLinus Torvalds #endif 531da177e4SLinus Torvalds #if NEIGH_DEBUG >= 2 541da177e4SLinus Torvalds #undef NEIGH_PRINTK2 551da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_PRINTK 561da177e4SLinus Torvalds #endif 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #define PNEIGH_HASHMASK 0xF 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg); 61d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags); 62d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh); 631da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds static struct neigh_table *neigh_tables; 6645fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS 679a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops; 6845fc3b11SAmos Waterland #endif 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* 711da177e4SLinus Torvalds Neighbour hash table buckets are protected with rwlock tbl->lock. 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds - All the scans/updates to hash buckets MUST be made under this lock. 741da177e4SLinus Torvalds - NOTHING clever should be made under this lock: no callbacks 751da177e4SLinus Torvalds to protocol backends, no attempts to send something to network. 761da177e4SLinus Torvalds It will result in deadlocks, if backend/driver wants to use neighbour 771da177e4SLinus Torvalds cache. 781da177e4SLinus Torvalds - If the entry requires some non-trivial actions, increase 791da177e4SLinus Torvalds its reference count and release table lock. 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds Neighbour entries are protected: 821da177e4SLinus Torvalds - with reference count. 831da177e4SLinus Torvalds - with rwlock neigh->lock 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds Reference count prevents destruction. 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds neigh->lock mainly serializes ll address data and its validity state. 881da177e4SLinus Torvalds However, the same lock is used to protect another entry fields: 891da177e4SLinus Torvalds - timer 901da177e4SLinus Torvalds - resolution queue 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds Again, nothing clever shall be made under neigh->lock, 931da177e4SLinus Torvalds the most complicated procedure, which we allow is dev->hard_header. 941da177e4SLinus Torvalds It is supposed, that dev->hard_header is simplistic and does 951da177e4SLinus Torvalds not make callbacks to neighbour tables. 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds The last lock is neigh_tbl_lock. It is pure SMP lock, protecting 981da177e4SLinus Torvalds list of neighbour tables. This list is used only in process context, 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock); 1021da177e4SLinus Torvalds 1038f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds kfree_skb(skb); 1061da177e4SLinus Torvalds return -ENETDOWN; 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1094f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh) 1104f494554SThomas Graf { 1114f494554SThomas Graf if (neigh->parms->neigh_cleanup) 1124f494554SThomas Graf neigh->parms->neigh_cleanup(neigh); 1134f494554SThomas Graf 114d961db35SThomas Graf __neigh_notify(neigh, RTM_DELNEIGH, 0); 1154f494554SThomas Graf neigh_release(neigh); 1164f494554SThomas Graf } 1174f494554SThomas Graf 1181da177e4SLinus Torvalds /* 1191da177e4SLinus Torvalds * It is random distribution in the interval (1/2)*base...(3/2)*base. 1201da177e4SLinus Torvalds * It corresponds to default IPv6 settings and is not overridable, 1211da177e4SLinus Torvalds * because it is really reasonable choice. 1221da177e4SLinus Torvalds */ 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base) 1251da177e4SLinus Torvalds { 126a02cec21SEric Dumazet return base ? (net_random() % base) + (base >> 1) : 0; 1271da177e4SLinus Torvalds } 1280a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time); 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds int shrunk = 0; 1341da177e4SLinus Torvalds int i; 135d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 140d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 141d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 142cd089336SDavid S. Miller for (i = 0; i < (1 << nht->hash_shift); i++) { 143767e97e1SEric Dumazet struct neighbour *n; 144767e97e1SEric Dumazet struct neighbour __rcu **np; 1451da177e4SLinus Torvalds 146d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 147767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 148767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 1491da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1501da177e4SLinus Torvalds * - nobody refers to it. 1511da177e4SLinus Torvalds * - it is not permanent 1521da177e4SLinus Torvalds */ 1531da177e4SLinus Torvalds write_lock(&n->lock); 1541da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1551da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 156767e97e1SEric Dumazet rcu_assign_pointer(*np, 157767e97e1SEric Dumazet rcu_dereference_protected(n->next, 158767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 1591da177e4SLinus Torvalds n->dead = 1; 1601da177e4SLinus Torvalds shrunk = 1; 1611da177e4SLinus Torvalds write_unlock(&n->lock); 1624f494554SThomas Graf neigh_cleanup_and_release(n); 1631da177e4SLinus Torvalds continue; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds write_unlock(&n->lock); 1661da177e4SLinus Torvalds np = &n->next; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds tbl->last_flush = jiffies; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds return shrunk; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 177a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 178a43d8994SPavel Emelyanov { 179a43d8994SPavel Emelyanov neigh_hold(n); 180a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 181a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 182a43d8994SPavel Emelyanov n->nud_state); 183a43d8994SPavel Emelyanov dump_stack(); 184a43d8994SPavel Emelyanov } 185a43d8994SPavel Emelyanov } 186a43d8994SPavel Emelyanov 1871da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1901da177e4SLinus Torvalds del_timer(&n->timer)) { 1911da177e4SLinus Torvalds neigh_release(n); 1921da177e4SLinus Torvalds return 1; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds return 0; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds struct sk_buff *skb; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 2021da177e4SLinus Torvalds dev_put(skb->dev); 2031da177e4SLinus Torvalds kfree_skb(skb); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 20749636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds int i; 210d6bf7817SEric Dumazet struct neigh_hash_table *nht; 2111da177e4SLinus Torvalds 212d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 213d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 214d6bf7817SEric Dumazet 215cd089336SDavid S. Miller for (i = 0; i < (1 << nht->hash_shift); i++) { 216767e97e1SEric Dumazet struct neighbour *n; 217767e97e1SEric Dumazet struct neighbour __rcu **np = &nht->hash_buckets[i]; 2181da177e4SLinus Torvalds 219767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 220767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 2211da177e4SLinus Torvalds if (dev && n->dev != dev) { 2221da177e4SLinus Torvalds np = &n->next; 2231da177e4SLinus Torvalds continue; 2241da177e4SLinus Torvalds } 225767e97e1SEric Dumazet rcu_assign_pointer(*np, 226767e97e1SEric Dumazet rcu_dereference_protected(n->next, 227767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 2281da177e4SLinus Torvalds write_lock(&n->lock); 2291da177e4SLinus Torvalds neigh_del_timer(n); 2301da177e4SLinus Torvalds n->dead = 1; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2331da177e4SLinus Torvalds /* The most unpleasant situation. 2341da177e4SLinus Torvalds We must destroy neighbour entry, 2351da177e4SLinus Torvalds but someone still uses it. 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds The destroy will be delayed until 2381da177e4SLinus Torvalds the last user releases us, but 2391da177e4SLinus Torvalds we must kill timers etc. and move 2401da177e4SLinus Torvalds it to safe state. 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds skb_queue_purge(&n->arp_queue); 2438b5c171bSEric Dumazet n->arp_queue_len_bytes = 0; 2441da177e4SLinus Torvalds n->output = neigh_blackhole; 2451da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2461da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2471da177e4SLinus Torvalds else 2481da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2491da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is stray.\n", n); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds write_unlock(&n->lock); 2524f494554SThomas Graf neigh_cleanup_and_release(n); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds } 25549636bb1SHerbert Xu } 2561da177e4SLinus Torvalds 25749636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 25849636bb1SHerbert Xu { 25949636bb1SHerbert Xu write_lock_bh(&tbl->lock); 26049636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 26149636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 26249636bb1SHerbert Xu } 2630a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr); 26449636bb1SHerbert Xu 26549636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 26649636bb1SHerbert Xu { 26749636bb1SHerbert Xu write_lock_bh(&tbl->lock); 26849636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2691da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2701da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2731da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2741da177e4SLinus Torvalds return 0; 2751da177e4SLinus Torvalds } 2760a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown); 2771da177e4SLinus Torvalds 278596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds struct neighbour *n = NULL; 2811da177e4SLinus Torvalds unsigned long now = jiffies; 2821da177e4SLinus Torvalds int entries; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2851da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2861da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2871da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2881da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2891da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2901da177e4SLinus Torvalds goto out_entries; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 293596b9b68SDavid Miller if (tbl->entry_size) 2945b8b0060SDavid Miller n = kzalloc(tbl->entry_size, GFP_ATOMIC); 295596b9b68SDavid Miller else { 296596b9b68SDavid Miller int sz = sizeof(*n) + tbl->key_len; 297596b9b68SDavid Miller 298596b9b68SDavid Miller sz = ALIGN(sz, NEIGH_PRIV_ALIGN); 299596b9b68SDavid Miller sz += dev->neigh_priv_len; 300596b9b68SDavid Miller n = kzalloc(sz, GFP_ATOMIC); 301596b9b68SDavid Miller } 3021da177e4SLinus Torvalds if (!n) 3031da177e4SLinus Torvalds goto out_entries; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds skb_queue_head_init(&n->arp_queue); 3061da177e4SLinus Torvalds rwlock_init(&n->lock); 3070ed8ddf4SEric Dumazet seqlock_init(&n->ha_lock); 3081da177e4SLinus Torvalds n->updated = n->used = now; 3091da177e4SLinus Torvalds n->nud_state = NUD_NONE; 3101da177e4SLinus Torvalds n->output = neigh_blackhole; 311f6b72b62SDavid S. Miller seqlock_init(&n->hh.hh_lock); 3121da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 313b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 3161da177e4SLinus Torvalds n->tbl = tbl; 3171da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 3181da177e4SLinus Torvalds n->dead = 1; 3191da177e4SLinus Torvalds out: 3201da177e4SLinus Torvalds return n; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds out_entries: 3231da177e4SLinus Torvalds atomic_dec(&tbl->entries); 3241da177e4SLinus Torvalds goto out; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3272c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x) 3282c2aba6cSDavid S. Miller { 3292c2aba6cSDavid S. Miller get_random_bytes(x, sizeof(*x)); 3302c2aba6cSDavid S. Miller *x |= 1; 3312c2aba6cSDavid S. Miller } 3322c2aba6cSDavid S. Miller 333cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift) 3341da177e4SLinus Torvalds { 335cd089336SDavid S. Miller size_t size = (1 << shift) * sizeof(struct neighbour *); 336d6bf7817SEric Dumazet struct neigh_hash_table *ret; 3376193d2beSEric Dumazet struct neighbour __rcu **buckets; 3382c2aba6cSDavid S. Miller int i; 3391da177e4SLinus Torvalds 340d6bf7817SEric Dumazet ret = kmalloc(sizeof(*ret), GFP_ATOMIC); 341d6bf7817SEric Dumazet if (!ret) 342d6bf7817SEric Dumazet return NULL; 343d6bf7817SEric Dumazet if (size <= PAGE_SIZE) 344d6bf7817SEric Dumazet buckets = kzalloc(size, GFP_ATOMIC); 345d6bf7817SEric Dumazet else 3466193d2beSEric Dumazet buckets = (struct neighbour __rcu **) 347d6bf7817SEric Dumazet __get_free_pages(GFP_ATOMIC | __GFP_ZERO, 348d6bf7817SEric Dumazet get_order(size)); 349d6bf7817SEric Dumazet if (!buckets) { 350d6bf7817SEric Dumazet kfree(ret); 351d6bf7817SEric Dumazet return NULL; 3521da177e4SLinus Torvalds } 3536193d2beSEric Dumazet ret->hash_buckets = buckets; 354cd089336SDavid S. Miller ret->hash_shift = shift; 3552c2aba6cSDavid S. Miller for (i = 0; i < NEIGH_NUM_HASH_RND; i++) 3562c2aba6cSDavid S. Miller neigh_get_hash_rnd(&ret->hash_rnd[i]); 3571da177e4SLinus Torvalds return ret; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head) 3611da177e4SLinus Torvalds { 362d6bf7817SEric Dumazet struct neigh_hash_table *nht = container_of(head, 363d6bf7817SEric Dumazet struct neigh_hash_table, 364d6bf7817SEric Dumazet rcu); 365cd089336SDavid S. Miller size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *); 3666193d2beSEric Dumazet struct neighbour __rcu **buckets = nht->hash_buckets; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds if (size <= PAGE_SIZE) 369d6bf7817SEric Dumazet kfree(buckets); 3701da177e4SLinus Torvalds else 371d6bf7817SEric Dumazet free_pages((unsigned long)buckets, get_order(size)); 372d6bf7817SEric Dumazet kfree(nht); 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 375d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, 376cd089336SDavid S. Miller unsigned long new_shift) 3771da177e4SLinus Torvalds { 378d6bf7817SEric Dumazet unsigned int i, hash; 379d6bf7817SEric Dumazet struct neigh_hash_table *new_nht, *old_nht; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3821da177e4SLinus Torvalds 383d6bf7817SEric Dumazet old_nht = rcu_dereference_protected(tbl->nht, 384d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 385cd089336SDavid S. Miller new_nht = neigh_hash_alloc(new_shift); 386d6bf7817SEric Dumazet if (!new_nht) 387d6bf7817SEric Dumazet return old_nht; 3881da177e4SLinus Torvalds 389cd089336SDavid S. Miller for (i = 0; i < (1 << old_nht->hash_shift); i++) { 3901da177e4SLinus Torvalds struct neighbour *n, *next; 3911da177e4SLinus Torvalds 392767e97e1SEric Dumazet for (n = rcu_dereference_protected(old_nht->hash_buckets[i], 393767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 394d6bf7817SEric Dumazet n != NULL; 395d6bf7817SEric Dumazet n = next) { 396d6bf7817SEric Dumazet hash = tbl->hash(n->primary_key, n->dev, 397d6bf7817SEric Dumazet new_nht->hash_rnd); 3981da177e4SLinus Torvalds 399cd089336SDavid S. Miller hash >>= (32 - new_nht->hash_shift); 400767e97e1SEric Dumazet next = rcu_dereference_protected(n->next, 401767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 4021da177e4SLinus Torvalds 403767e97e1SEric Dumazet rcu_assign_pointer(n->next, 404767e97e1SEric Dumazet rcu_dereference_protected( 405767e97e1SEric Dumazet new_nht->hash_buckets[hash], 406767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 407767e97e1SEric Dumazet rcu_assign_pointer(new_nht->hash_buckets[hash], n); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 411d6bf7817SEric Dumazet rcu_assign_pointer(tbl->nht, new_nht); 412d6bf7817SEric Dumazet call_rcu(&old_nht->rcu, neigh_hash_free_rcu); 413d6bf7817SEric Dumazet return new_nht; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 4171da177e4SLinus Torvalds struct net_device *dev) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds struct neighbour *n; 4201da177e4SLinus Torvalds int key_len = tbl->key_len; 421bc4bf5f3SPavel Emelyanov u32 hash_val; 422d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4251da177e4SLinus Torvalds 426d6bf7817SEric Dumazet rcu_read_lock_bh(); 427d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 428cd089336SDavid S. Miller hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 429767e97e1SEric Dumazet 430767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 431767e97e1SEric Dumazet n != NULL; 432767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 4331da177e4SLinus Torvalds if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) { 434767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 435767e97e1SEric Dumazet n = NULL; 4361da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4371da177e4SLinus Torvalds break; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds } 440767e97e1SEric Dumazet 441d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4421da177e4SLinus Torvalds return n; 4431da177e4SLinus Torvalds } 4440a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup); 4451da177e4SLinus Torvalds 446426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 447426b5303SEric W. Biederman const void *pkey) 4481da177e4SLinus Torvalds { 4491da177e4SLinus Torvalds struct neighbour *n; 4501da177e4SLinus Torvalds int key_len = tbl->key_len; 451bc4bf5f3SPavel Emelyanov u32 hash_val; 452d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4551da177e4SLinus Torvalds 456d6bf7817SEric Dumazet rcu_read_lock_bh(); 457d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 458cd089336SDavid S. Miller hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift); 459767e97e1SEric Dumazet 460767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 461767e97e1SEric Dumazet n != NULL; 462767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 463426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 464878628fbSYOSHIFUJI Hideaki net_eq(dev_net(n->dev), net)) { 465767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 466767e97e1SEric Dumazet n = NULL; 4671da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4681da177e4SLinus Torvalds break; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds } 471767e97e1SEric Dumazet 472d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4731da177e4SLinus Torvalds return n; 4741da177e4SLinus Torvalds } 4750a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev); 4761da177e4SLinus Torvalds 477a263b309SDavid S. Miller struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey, 478a263b309SDavid S. Miller struct net_device *dev, bool want_ref) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds u32 hash_val; 4811da177e4SLinus Torvalds int key_len = tbl->key_len; 4821da177e4SLinus Torvalds int error; 483596b9b68SDavid Miller struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev); 484d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds if (!n) { 4871da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4881da177e4SLinus Torvalds goto out; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4921da177e4SLinus Torvalds n->dev = dev; 4931da177e4SLinus Torvalds dev_hold(dev); 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* Protocol specific setup. */ 4961da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4971da177e4SLinus Torvalds rc = ERR_PTR(error); 4981da177e4SLinus Torvalds goto out_neigh_release; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 501da6a8fa0SDavid Miller if (dev->netdev_ops->ndo_neigh_construct) { 502da6a8fa0SDavid Miller error = dev->netdev_ops->ndo_neigh_construct(n); 503da6a8fa0SDavid Miller if (error < 0) { 504da6a8fa0SDavid Miller rc = ERR_PTR(error); 505da6a8fa0SDavid Miller goto out_neigh_release; 506da6a8fa0SDavid Miller } 507da6a8fa0SDavid Miller } 508da6a8fa0SDavid Miller 509447f2191SDavid S. Miller /* Device specific setup. */ 510447f2191SDavid S. Miller if (n->parms->neigh_setup && 511447f2191SDavid S. Miller (error = n->parms->neigh_setup(n)) < 0) { 512447f2191SDavid S. Miller rc = ERR_PTR(error); 513447f2191SDavid S. Miller goto out_neigh_release; 514447f2191SDavid S. Miller } 515447f2191SDavid S. Miller 5161da177e4SLinus Torvalds n->confirmed = jiffies - (n->parms->base_reachable_time << 1); 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 519d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 520d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 5211da177e4SLinus Torvalds 522cd089336SDavid S. Miller if (atomic_read(&tbl->entries) > (1 << nht->hash_shift)) 523cd089336SDavid S. Miller nht = neigh_hash_grow(tbl, nht->hash_shift + 1); 5241da177e4SLinus Torvalds 525cd089336SDavid S. Miller hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds if (n->parms->dead) { 5281da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 5291da177e4SLinus Torvalds goto out_tbl_unlock; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 532767e97e1SEric Dumazet for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val], 533767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 534767e97e1SEric Dumazet n1 != NULL; 535767e97e1SEric Dumazet n1 = rcu_dereference_protected(n1->next, 536767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) { 5371da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 538a263b309SDavid S. Miller if (want_ref) 5391da177e4SLinus Torvalds neigh_hold(n1); 5401da177e4SLinus Torvalds rc = n1; 5411da177e4SLinus Torvalds goto out_tbl_unlock; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds n->dead = 0; 546a263b309SDavid S. Miller if (want_ref) 5471da177e4SLinus Torvalds neigh_hold(n); 548767e97e1SEric Dumazet rcu_assign_pointer(n->next, 549767e97e1SEric Dumazet rcu_dereference_protected(nht->hash_buckets[hash_val], 550767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 551767e97e1SEric Dumazet rcu_assign_pointer(nht->hash_buckets[hash_val], n); 5521da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5531da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is created.\n", n); 5541da177e4SLinus Torvalds rc = n; 5551da177e4SLinus Torvalds out: 5561da177e4SLinus Torvalds return rc; 5571da177e4SLinus Torvalds out_tbl_unlock: 5581da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5591da177e4SLinus Torvalds out_neigh_release: 5601da177e4SLinus Torvalds neigh_release(n); 5611da177e4SLinus Torvalds goto out; 5621da177e4SLinus Torvalds } 563a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create); 5641da177e4SLinus Torvalds 565be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len) 566fa86d322SPavel Emelyanov { 567fa86d322SPavel Emelyanov u32 hash_val = *(u32 *)(pkey + key_len - 4); 568fa86d322SPavel Emelyanov hash_val ^= (hash_val >> 16); 569fa86d322SPavel Emelyanov hash_val ^= hash_val >> 8; 570fa86d322SPavel Emelyanov hash_val ^= hash_val >> 4; 571fa86d322SPavel Emelyanov hash_val &= PNEIGH_HASHMASK; 572be01d655SYOSHIFUJI Hideaki return hash_val; 573fa86d322SPavel Emelyanov } 574fa86d322SPavel Emelyanov 575be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, 576be01d655SYOSHIFUJI Hideaki struct net *net, 577be01d655SYOSHIFUJI Hideaki const void *pkey, 578be01d655SYOSHIFUJI Hideaki int key_len, 579be01d655SYOSHIFUJI Hideaki struct net_device *dev) 580be01d655SYOSHIFUJI Hideaki { 581be01d655SYOSHIFUJI Hideaki while (n) { 582be01d655SYOSHIFUJI Hideaki if (!memcmp(n->key, pkey, key_len) && 583be01d655SYOSHIFUJI Hideaki net_eq(pneigh_net(n), net) && 584be01d655SYOSHIFUJI Hideaki (n->dev == dev || !n->dev)) 585fa86d322SPavel Emelyanov return n; 586be01d655SYOSHIFUJI Hideaki n = n->next; 587be01d655SYOSHIFUJI Hideaki } 588be01d655SYOSHIFUJI Hideaki return NULL; 589be01d655SYOSHIFUJI Hideaki } 590be01d655SYOSHIFUJI Hideaki 591be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, 592be01d655SYOSHIFUJI Hideaki struct net *net, const void *pkey, struct net_device *dev) 593be01d655SYOSHIFUJI Hideaki { 594be01d655SYOSHIFUJI Hideaki int key_len = tbl->key_len; 595be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 596be01d655SYOSHIFUJI Hideaki 597be01d655SYOSHIFUJI Hideaki return __pneigh_lookup_1(tbl->phash_buckets[hash_val], 598be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 599fa86d322SPavel Emelyanov } 6000a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup); 601fa86d322SPavel Emelyanov 602426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 603426b5303SEric W. Biederman struct net *net, const void *pkey, 6041da177e4SLinus Torvalds struct net_device *dev, int creat) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds struct pneigh_entry *n; 6071da177e4SLinus Torvalds int key_len = tbl->key_len; 608be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 611be01d655SYOSHIFUJI Hideaki n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], 612be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 613be01d655SYOSHIFUJI Hideaki read_unlock_bh(&tbl->lock); 6141da177e4SLinus Torvalds 615be01d655SYOSHIFUJI Hideaki if (n || !creat) 6161da177e4SLinus Torvalds goto out; 6171da177e4SLinus Torvalds 6184ae28944SPavel Emelyanov ASSERT_RTNL(); 6194ae28944SPavel Emelyanov 6201da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 6211da177e4SLinus Torvalds if (!n) 6221da177e4SLinus Torvalds goto out; 6231da177e4SLinus Torvalds 624e42ea986SEric Dumazet write_pnet(&n->net, hold_net(net)); 6251da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 6261da177e4SLinus Torvalds n->dev = dev; 6271da177e4SLinus Torvalds if (dev) 6281da177e4SLinus Torvalds dev_hold(dev); 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 6311da177e4SLinus Torvalds if (dev) 6321da177e4SLinus Torvalds dev_put(dev); 633da12f735SDenis V. Lunev release_net(net); 6341da177e4SLinus Torvalds kfree(n); 6351da177e4SLinus Torvalds n = NULL; 6361da177e4SLinus Torvalds goto out; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6401da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 6411da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 6421da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6431da177e4SLinus Torvalds out: 6441da177e4SLinus Torvalds return n; 6451da177e4SLinus Torvalds } 6460a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup); 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds 649426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 6501da177e4SLinus Torvalds struct net_device *dev) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6531da177e4SLinus Torvalds int key_len = tbl->key_len; 654be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6571da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 6581da177e4SLinus Torvalds np = &n->next) { 659426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 660878628fbSYOSHIFUJI Hideaki net_eq(pneigh_net(n), net)) { 6611da177e4SLinus Torvalds *np = n->next; 6621da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6631da177e4SLinus Torvalds if (tbl->pdestructor) 6641da177e4SLinus Torvalds tbl->pdestructor(n); 6651da177e4SLinus Torvalds if (n->dev) 6661da177e4SLinus Torvalds dev_put(n->dev); 66757da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6681da177e4SLinus Torvalds kfree(n); 6691da177e4SLinus Torvalds return 0; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6731da177e4SLinus Torvalds return -ENOENT; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6791da177e4SLinus Torvalds u32 h; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 6821da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 6831da177e4SLinus Torvalds while ((n = *np) != NULL) { 6841da177e4SLinus Torvalds if (!dev || n->dev == dev) { 6851da177e4SLinus Torvalds *np = n->next; 6861da177e4SLinus Torvalds if (tbl->pdestructor) 6871da177e4SLinus Torvalds tbl->pdestructor(n); 6881da177e4SLinus Torvalds if (n->dev) 6891da177e4SLinus Torvalds dev_put(n->dev); 69057da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6911da177e4SLinus Torvalds kfree(n); 6921da177e4SLinus Torvalds continue; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds np = &n->next; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds return -ENOENT; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 70006f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 70106f0511dSDenis V. Lunev 70206f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 70306f0511dSDenis V. Lunev { 70406f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 70506f0511dSDenis V. Lunev neigh_parms_destroy(parms); 70606f0511dSDenis V. Lunev } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds /* 7091da177e4SLinus Torvalds * neighbour must already be out of the table; 7101da177e4SLinus Torvalds * 7111da177e4SLinus Torvalds */ 7121da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 7131da177e4SLinus Torvalds { 714da6a8fa0SDavid Miller struct net_device *dev = neigh->dev; 715da6a8fa0SDavid Miller 7161da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds if (!neigh->dead) { 719e005d193SJoe Perches pr_warn("Destroying alive neighbour %p\n", neigh); 7201da177e4SLinus Torvalds dump_stack(); 7211da177e4SLinus Torvalds return; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 725e005d193SJoe Perches pr_warn("Impossible event\n"); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 7288b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 7291da177e4SLinus Torvalds 730447f2191SDavid S. Miller if (dev->netdev_ops->ndo_neigh_destroy) 731447f2191SDavid S. Miller dev->netdev_ops->ndo_neigh_destroy(neigh); 732447f2191SDavid S. Miller 733da6a8fa0SDavid Miller dev_put(dev); 7341da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 7395b8b0060SDavid Miller kfree_rcu(neigh, rcu); 7401da177e4SLinus Torvalds } 7410a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds /* Neighbour state is suspicious; 7441da177e4SLinus Torvalds disable fast path. 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds Called with write_locked neigh. 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds neigh->output = neigh->ops->output; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds /* Neighbour state is OK; 7561da177e4SLinus Torvalds enable fast path. 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds Called with write_locked neigh. 7591da177e4SLinus Torvalds */ 7601da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 7611da177e4SLinus Torvalds { 7621da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is connected.\n", neigh); 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 767e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work) 7681da177e4SLinus Torvalds { 769e4c4e448SEric Dumazet struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); 770767e97e1SEric Dumazet struct neighbour *n; 771767e97e1SEric Dumazet struct neighbour __rcu **np; 772e4c4e448SEric Dumazet unsigned int i; 773d6bf7817SEric Dumazet struct neigh_hash_table *nht; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 7761da177e4SLinus Torvalds 777e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 778d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 779d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds /* 7821da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 7831da177e4SLinus Torvalds */ 7841da177e4SLinus Torvalds 785e4c4e448SEric Dumazet if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { 7861da177e4SLinus Torvalds struct neigh_parms *p; 787e4c4e448SEric Dumazet tbl->last_rand = jiffies; 7881da177e4SLinus Torvalds for (p = &tbl->parms; p; p = p->next) 7891da177e4SLinus Torvalds p->reachable_time = 7901da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 793cd089336SDavid S. Miller for (i = 0 ; i < (1 << nht->hash_shift); i++) { 794d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 7951da177e4SLinus Torvalds 796767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 797767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 7981da177e4SLinus Torvalds unsigned int state; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds write_lock(&n->lock); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds state = n->nud_state; 8031da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 8041da177e4SLinus Torvalds write_unlock(&n->lock); 8051da177e4SLinus Torvalds goto next_elt; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 8091da177e4SLinus Torvalds n->used = n->confirmed; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 8121da177e4SLinus Torvalds (state == NUD_FAILED || 813e4c4e448SEric Dumazet time_after(jiffies, n->used + n->parms->gc_staletime))) { 8141da177e4SLinus Torvalds *np = n->next; 8151da177e4SLinus Torvalds n->dead = 1; 8161da177e4SLinus Torvalds write_unlock(&n->lock); 8174f494554SThomas Graf neigh_cleanup_and_release(n); 8181da177e4SLinus Torvalds continue; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds write_unlock(&n->lock); 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds next_elt: 8231da177e4SLinus Torvalds np = &n->next; 8241da177e4SLinus Torvalds } 825e4c4e448SEric Dumazet /* 826e4c4e448SEric Dumazet * It's fine to release lock here, even if hash table 827e4c4e448SEric Dumazet * grows while we are preempted. 828e4c4e448SEric Dumazet */ 829e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 830e4c4e448SEric Dumazet cond_resched(); 831e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 83284338a6cSMichel Machado nht = rcu_dereference_protected(tbl->nht, 83384338a6cSMichel Machado lockdep_is_held(&tbl->lock)); 834e4c4e448SEric Dumazet } 8351da177e4SLinus Torvalds /* Cycle through all hash buckets every base_reachable_time/2 ticks. 8361da177e4SLinus Torvalds * ARP entry timeouts range from 1/2 base_reachable_time to 3/2 8371da177e4SLinus Torvalds * base_reachable_time. 8381da177e4SLinus Torvalds */ 839e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, 840e4c4e448SEric Dumazet tbl->parms.base_reachable_time >> 1); 841e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 847a02cec21SEric Dumazet return (n->nud_state & NUD_PROBE) ? 8481da177e4SLinus Torvalds p->ucast_probes : 849a02cec21SEric Dumazet p->ucast_probes + p->app_probes + p->mcast_probes; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8525ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh) 8530a141509SEric Dumazet __releases(neigh->lock) 8540a141509SEric Dumazet __acquires(neigh->lock) 8555ef12d98STimo Teras { 8565ef12d98STimo Teras struct sk_buff *skb; 8575ef12d98STimo Teras 8585ef12d98STimo Teras NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 8595ef12d98STimo Teras NEIGH_PRINTK2("neigh %p is failed.\n", neigh); 8605ef12d98STimo Teras neigh->updated = jiffies; 8615ef12d98STimo Teras 8625ef12d98STimo Teras /* It is very thin place. report_unreachable is very complicated 8635ef12d98STimo Teras routine. Particularly, it can hit the same neighbour entry! 8645ef12d98STimo Teras 8655ef12d98STimo Teras So that, we try to be accurate and avoid dead loop. --ANK 8665ef12d98STimo Teras */ 8675ef12d98STimo Teras while (neigh->nud_state == NUD_FAILED && 8685ef12d98STimo Teras (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 8695ef12d98STimo Teras write_unlock(&neigh->lock); 8705ef12d98STimo Teras neigh->ops->error_report(neigh, skb); 8715ef12d98STimo Teras write_lock(&neigh->lock); 8725ef12d98STimo Teras } 8735ef12d98STimo Teras skb_queue_purge(&neigh->arp_queue); 8748b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 8755ef12d98STimo Teras } 8765ef12d98STimo Teras 877cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh) 878cd28ca0aSEric Dumazet __releases(neigh->lock) 879cd28ca0aSEric Dumazet { 880cd28ca0aSEric Dumazet struct sk_buff *skb = skb_peek(&neigh->arp_queue); 881cd28ca0aSEric Dumazet /* keep skb alive even if arp_queue overflows */ 882cd28ca0aSEric Dumazet if (skb) 883cd28ca0aSEric Dumazet skb = skb_copy(skb, GFP_ATOMIC); 884cd28ca0aSEric Dumazet write_unlock(&neigh->lock); 885cd28ca0aSEric Dumazet neigh->ops->solicit(neigh, skb); 886cd28ca0aSEric Dumazet atomic_inc(&neigh->probes); 887cd28ca0aSEric Dumazet kfree_skb(skb); 888cd28ca0aSEric Dumazet } 889cd28ca0aSEric Dumazet 8901da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds unsigned long now, next; 8951da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 89695c96174SEric Dumazet unsigned int state; 8971da177e4SLinus Torvalds int notify = 0; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds write_lock(&neigh->lock); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds state = neigh->nud_state; 9021da177e4SLinus Torvalds now = jiffies; 9031da177e4SLinus Torvalds next = now + HZ; 9041da177e4SLinus Torvalds 905045f7b3bSDavid S. Miller if (!(state & NUD_IN_TIMER)) 9061da177e4SLinus Torvalds goto out; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 9091da177e4SLinus Torvalds if (time_before_eq(now, 9101da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 9111da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is still alive.\n", neigh); 9121da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 9131da177e4SLinus Torvalds } else if (time_before_eq(now, 9141da177e4SLinus Torvalds neigh->used + neigh->parms->delay_probe_time)) { 9151da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 9161da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 917955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9181da177e4SLinus Torvalds neigh_suspect(neigh); 9191da177e4SLinus Torvalds next = now + neigh->parms->delay_probe_time; 9201da177e4SLinus Torvalds } else { 9211da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 9221da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 923955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9241da177e4SLinus Torvalds neigh_suspect(neigh); 9258d71740cSTom Tucker notify = 1; 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 9281da177e4SLinus Torvalds if (time_before_eq(now, 9291da177e4SLinus Torvalds neigh->confirmed + neigh->parms->delay_probe_time)) { 9301da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh); 9311da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 932955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9331da177e4SLinus Torvalds neigh_connect(neigh); 9348d71740cSTom Tucker notify = 1; 9351da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 9361da177e4SLinus Torvalds } else { 9371da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is probed.\n", neigh); 9381da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 939955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9401da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 9411da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds } else { 9441da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 9451da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 9491da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 9501da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 9511da177e4SLinus Torvalds notify = 1; 9525ef12d98STimo Teras neigh_invalidate(neigh); 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 9561da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 9571da177e4SLinus Torvalds next = jiffies + HZ/2; 9586fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 9596fb9974fSHerbert Xu neigh_hold(neigh); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 962cd28ca0aSEric Dumazet neigh_probe(neigh); 9639ff56607SDavid S. Miller } else { 9641da177e4SLinus Torvalds out: 9651da177e4SLinus Torvalds write_unlock(&neigh->lock); 9669ff56607SDavid S. Miller } 9671da177e4SLinus Torvalds 968d961db35SThomas Graf if (notify) 969d961db35SThomas Graf neigh_update_notify(neigh); 970d961db35SThomas Graf 9711da177e4SLinus Torvalds neigh_release(neigh); 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 9751da177e4SLinus Torvalds { 9761da177e4SLinus Torvalds int rc; 977cd28ca0aSEric Dumazet bool immediate_probe = false; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds rc = 0; 9821da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 9831da177e4SLinus Torvalds goto out_unlock_bh; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 9861da177e4SLinus Torvalds if (neigh->parms->mcast_probes + neigh->parms->app_probes) { 987cd28ca0aSEric Dumazet unsigned long next, now = jiffies; 988cd28ca0aSEric Dumazet 9891da177e4SLinus Torvalds atomic_set(&neigh->probes, neigh->parms->ucast_probes); 9901da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 991cd28ca0aSEric Dumazet neigh->updated = now; 992cd28ca0aSEric Dumazet next = now + max(neigh->parms->retrans_time, HZ/2); 993cd28ca0aSEric Dumazet neigh_add_timer(neigh, next); 994cd28ca0aSEric Dumazet immediate_probe = true; 9951da177e4SLinus Torvalds } else { 9961da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 997955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9981da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds kfree_skb(skb); 10011da177e4SLinus Torvalds return 1; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 10041da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 10051da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 1006955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 1007667347f1SDavid S. Miller neigh_add_timer(neigh, 1008667347f1SDavid S. Miller jiffies + neigh->parms->delay_probe_time); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 10121da177e4SLinus Torvalds if (skb) { 10138b5c171bSEric Dumazet while (neigh->arp_queue_len_bytes + skb->truesize > 10148b5c171bSEric Dumazet neigh->parms->queue_len_bytes) { 10151da177e4SLinus Torvalds struct sk_buff *buff; 10168b5c171bSEric Dumazet 1017f72051b0SDavid S. Miller buff = __skb_dequeue(&neigh->arp_queue); 10188b5c171bSEric Dumazet if (!buff) 10198b5c171bSEric Dumazet break; 10208b5c171bSEric Dumazet neigh->arp_queue_len_bytes -= buff->truesize; 10211da177e4SLinus Torvalds kfree_skb(buff); 10229a6d276eSNeil Horman NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); 10231da177e4SLinus Torvalds } 1024a4731138SEric Dumazet skb_dst_force(skb); 10251da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 10268b5c171bSEric Dumazet neigh->arp_queue_len_bytes += skb->truesize; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds rc = 1; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds out_unlock_bh: 1031cd28ca0aSEric Dumazet if (immediate_probe) 1032cd28ca0aSEric Dumazet neigh_probe(neigh); 1033cd28ca0aSEric Dumazet else 1034cd28ca0aSEric Dumazet write_unlock(&neigh->lock); 1035cd28ca0aSEric Dumazet local_bh_enable(); 10361da177e4SLinus Torvalds return rc; 10371da177e4SLinus Torvalds } 10380a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send); 10391da177e4SLinus Torvalds 1040f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh) 10411da177e4SLinus Torvalds { 10421da177e4SLinus Torvalds struct hh_cache *hh; 10433b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 104491a72a70SDoug Kehn = NULL; 104591a72a70SDoug Kehn 104691a72a70SDoug Kehn if (neigh->dev->header_ops) 104791a72a70SDoug Kehn update = neigh->dev->header_ops->cache_update; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds if (update) { 1050f6b72b62SDavid S. Miller hh = &neigh->hh; 1051f6b72b62SDavid S. Miller if (hh->hh_len) { 10523644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 10531da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 10543644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* Generic update routine. 10621da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 10631da177e4SLinus Torvalds -- new is new state. 10641da177e4SLinus Torvalds -- flags 10651da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 10661da177e4SLinus Torvalds if it is different. 10671da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 10681da177e4SLinus Torvalds lladdr instead of overriding it 10691da177e4SLinus Torvalds if it is different. 10701da177e4SLinus Torvalds It also allows to retain current state 10711da177e4SLinus Torvalds if lladdr is unchanged. 10721da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 10751da177e4SLinus Torvalds NTF_ROUTER flag. 10761da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 10771da177e4SLinus Torvalds a router. 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 10801da177e4SLinus Torvalds */ 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 10831da177e4SLinus Torvalds u32 flags) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds u8 old; 10861da177e4SLinus Torvalds int err; 10871da177e4SLinus Torvalds int notify = 0; 10881da177e4SLinus Torvalds struct net_device *dev; 10891da177e4SLinus Torvalds int update_isrouter = 0; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds dev = neigh->dev; 10941da177e4SLinus Torvalds old = neigh->nud_state; 10951da177e4SLinus Torvalds err = -EPERM; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 10981da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 10991da177e4SLinus Torvalds goto out; 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 11021da177e4SLinus Torvalds neigh_del_timer(neigh); 11031da177e4SLinus Torvalds if (old & NUD_CONNECTED) 11041da177e4SLinus Torvalds neigh_suspect(neigh); 11051da177e4SLinus Torvalds neigh->nud_state = new; 11061da177e4SLinus Torvalds err = 0; 11071da177e4SLinus Torvalds notify = old & NUD_VALID; 11085ef12d98STimo Teras if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && 11095ef12d98STimo Teras (new & NUD_FAILED)) { 11105ef12d98STimo Teras neigh_invalidate(neigh); 11115ef12d98STimo Teras notify = 1; 11125ef12d98STimo Teras } 11131da177e4SLinus Torvalds goto out; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 11171da177e4SLinus Torvalds if (!dev->addr_len) { 11181da177e4SLinus Torvalds /* First case: device needs no address. */ 11191da177e4SLinus Torvalds lladdr = neigh->ha; 11201da177e4SLinus Torvalds } else if (lladdr) { 11211da177e4SLinus Torvalds /* The second case: if something is already cached 11221da177e4SLinus Torvalds and a new address is proposed: 11231da177e4SLinus Torvalds - compare new & old 11241da177e4SLinus Torvalds - if they are different, check override flag 11251da177e4SLinus Torvalds */ 11261da177e4SLinus Torvalds if ((old & NUD_VALID) && 11271da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 11281da177e4SLinus Torvalds lladdr = neigh->ha; 11291da177e4SLinus Torvalds } else { 11301da177e4SLinus Torvalds /* No address is supplied; if we know something, 11311da177e4SLinus Torvalds use it, otherwise discard the request. 11321da177e4SLinus Torvalds */ 11331da177e4SLinus Torvalds err = -EINVAL; 11341da177e4SLinus Torvalds if (!(old & NUD_VALID)) 11351da177e4SLinus Torvalds goto out; 11361da177e4SLinus Torvalds lladdr = neigh->ha; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11401da177e4SLinus Torvalds neigh->confirmed = jiffies; 11411da177e4SLinus Torvalds neigh->updated = jiffies; 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /* If entry was valid and address is not changed, 11441da177e4SLinus Torvalds do not change entry state, if new one is STALE. 11451da177e4SLinus Torvalds */ 11461da177e4SLinus Torvalds err = 0; 11471da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 11481da177e4SLinus Torvalds if (old & NUD_VALID) { 11491da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 11501da177e4SLinus Torvalds update_isrouter = 0; 11511da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 11521da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 11531da177e4SLinus Torvalds lladdr = neigh->ha; 11541da177e4SLinus Torvalds new = NUD_STALE; 11551da177e4SLinus Torvalds } else 11561da177e4SLinus Torvalds goto out; 11571da177e4SLinus Torvalds } else { 11581da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 11591da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 11601da177e4SLinus Torvalds (old & NUD_CONNECTED)) 11611da177e4SLinus Torvalds ) 11621da177e4SLinus Torvalds new = old; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds if (new != old) { 11671da177e4SLinus Torvalds neigh_del_timer(neigh); 1168a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1169667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 11701da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1171667347f1SDavid S. Miller neigh->parms->reachable_time : 1172667347f1SDavid S. Miller 0))); 11731da177e4SLinus Torvalds neigh->nud_state = new; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds if (lladdr != neigh->ha) { 11770ed8ddf4SEric Dumazet write_seqlock(&neigh->ha_lock); 11781da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 11790ed8ddf4SEric Dumazet write_sequnlock(&neigh->ha_lock); 11801da177e4SLinus Torvalds neigh_update_hhs(neigh); 11811da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 11821da177e4SLinus Torvalds neigh->confirmed = jiffies - 11831da177e4SLinus Torvalds (neigh->parms->base_reachable_time << 1); 11841da177e4SLinus Torvalds notify = 1; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds if (new == old) 11871da177e4SLinus Torvalds goto out; 11881da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11891da177e4SLinus Torvalds neigh_connect(neigh); 11901da177e4SLinus Torvalds else 11911da177e4SLinus Torvalds neigh_suspect(neigh); 11921da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 11931da177e4SLinus Torvalds struct sk_buff *skb; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 11981da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 119969cce1d1SDavid S. Miller struct dst_entry *dst = skb_dst(skb); 120069cce1d1SDavid S. Miller struct neighbour *n2, *n1 = neigh; 12011da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 1202e049f288Sroy.qing.li@gmail.com 1203e049f288Sroy.qing.li@gmail.com rcu_read_lock(); 120413a43d94SDavid S. Miller 120513a43d94SDavid S. Miller /* Why not just use 'neigh' as-is? The problem is that 120613a43d94SDavid S. Miller * things such as shaper, eql, and sch_teql can end up 120713a43d94SDavid S. Miller * using alternative, different, neigh objects to output 120813a43d94SDavid S. Miller * the packet in the output path. So what we need to do 120913a43d94SDavid S. Miller * here is re-lookup the top-level neigh in the path so 121013a43d94SDavid S. Miller * we can reinject the packet there. 121113a43d94SDavid S. Miller */ 121213a43d94SDavid S. Miller n2 = NULL; 121313a43d94SDavid S. Miller if (dst) { 121413a43d94SDavid S. Miller n2 = dst_neigh_lookup_skb(dst, skb); 121513a43d94SDavid S. Miller if (n2) 121669cce1d1SDavid S. Miller n1 = n2; 121713a43d94SDavid S. Miller } 12188f40b161SDavid S. Miller n1->output(n1, skb); 121913a43d94SDavid S. Miller if (n2) 122013a43d94SDavid S. Miller neigh_release(n2); 1221e049f288Sroy.qing.li@gmail.com rcu_read_unlock(); 1222e049f288Sroy.qing.li@gmail.com 12231da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 12268b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds out: 12291da177e4SLinus Torvalds if (update_isrouter) { 12301da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 12311da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 12321da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 12358d71740cSTom Tucker 12368d71740cSTom Tucker if (notify) 1237d961db35SThomas Graf neigh_update_notify(neigh); 1238d961db35SThomas Graf 12391da177e4SLinus Torvalds return err; 12401da177e4SLinus Torvalds } 12410a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update); 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 12441da177e4SLinus Torvalds u8 *lladdr, void *saddr, 12451da177e4SLinus Torvalds struct net_device *dev) 12461da177e4SLinus Torvalds { 12471da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 12481da177e4SLinus Torvalds lladdr || !dev->addr_len); 12491da177e4SLinus Torvalds if (neigh) 12501da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 12511da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 12521da177e4SLinus Torvalds return neigh; 12531da177e4SLinus Torvalds } 12540a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns); 12551da177e4SLinus Torvalds 125634d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */ 1257f6b72b62SDavid S. Miller static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct net_device *dev = dst->dev; 1260f6b72b62SDavid S. Miller __be16 prot = dst->ops->protocol; 1261f6b72b62SDavid S. Miller struct hh_cache *hh = &n->hh; 12620ed8ddf4SEric Dumazet 12630ed8ddf4SEric Dumazet write_lock_bh(&n->lock); 126434d101ddSEric Dumazet 1265f6b72b62SDavid S. Miller /* Only one thread can come in here and initialize the 1266f6b72b62SDavid S. Miller * hh_cache entry. 1267f6b72b62SDavid S. Miller */ 1268b23b5455SDavid S. Miller if (!hh->hh_len) 1269b23b5455SDavid S. Miller dev->header_ops->cache(n, hh, prot); 1270f6b72b62SDavid S. Miller 12710ed8ddf4SEric Dumazet write_unlock_bh(&n->lock); 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit 1275767e97e1SEric Dumazet * worked, f.e. if you want to override normal output path (eql, shaper), 1276767e97e1SEric Dumazet * but resolution is not made yet. 12771da177e4SLinus Torvalds */ 12781da177e4SLinus Torvalds 12798f40b161SDavid S. Miller int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb) 12801da177e4SLinus Torvalds { 12811da177e4SLinus Torvalds struct net_device *dev = skb->dev; 12821da177e4SLinus Torvalds 1283bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12841da177e4SLinus Torvalds 12850c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 12861da177e4SLinus Torvalds skb->len) < 0 && 12873b04dddeSStephen Hemminger dev->header_ops->rebuild(skb)) 12881da177e4SLinus Torvalds return 0; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds return dev_queue_xmit(skb); 12911da177e4SLinus Torvalds } 12920a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output); 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds /* Slow and careful. */ 12951da177e4SLinus Torvalds 12968f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb) 12971da177e4SLinus Torvalds { 1298adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 12991da177e4SLinus Torvalds int rc = 0; 13001da177e4SLinus Torvalds 13018f40b161SDavid S. Miller if (!dst) 13021da177e4SLinus Torvalds goto discard; 13031da177e4SLinus Torvalds 1304bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 13071da177e4SLinus Torvalds int err; 13081da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 13090ed8ddf4SEric Dumazet unsigned int seq; 131034d101ddSEric Dumazet 1311f6b72b62SDavid S. Miller if (dev->header_ops->cache && !neigh->hh.hh_len) 1312f6b72b62SDavid S. Miller neigh_hh_init(neigh, dst); 131334d101ddSEric Dumazet 13140ed8ddf4SEric Dumazet do { 13150ed8ddf4SEric Dumazet seq = read_seqbegin(&neigh->ha_lock); 13160c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13171da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13180ed8ddf4SEric Dumazet } while (read_seqretry(&neigh->ha_lock, seq)); 131934d101ddSEric Dumazet 13201da177e4SLinus Torvalds if (err >= 0) 1321542d4d68SDavid S. Miller rc = dev_queue_xmit(skb); 13221da177e4SLinus Torvalds else 13231da177e4SLinus Torvalds goto out_kfree_skb; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds out: 13261da177e4SLinus Torvalds return rc; 13271da177e4SLinus Torvalds discard: 13281da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", 13298f40b161SDavid S. Miller dst, neigh); 13301da177e4SLinus Torvalds out_kfree_skb: 13311da177e4SLinus Torvalds rc = -EINVAL; 13321da177e4SLinus Torvalds kfree_skb(skb); 13331da177e4SLinus Torvalds goto out; 13341da177e4SLinus Torvalds } 13350a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output); 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds /* As fast as possible without hh cache */ 13381da177e4SLinus Torvalds 13398f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb) 13401da177e4SLinus Torvalds { 13411da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 13420ed8ddf4SEric Dumazet unsigned int seq; 13438f40b161SDavid S. Miller int err; 13441da177e4SLinus Torvalds 1345bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 13461da177e4SLinus Torvalds 13470ed8ddf4SEric Dumazet do { 13480ed8ddf4SEric Dumazet seq = read_seqbegin(&neigh->ha_lock); 13490c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13501da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13510ed8ddf4SEric Dumazet } while (read_seqretry(&neigh->ha_lock, seq)); 13520ed8ddf4SEric Dumazet 13531da177e4SLinus Torvalds if (err >= 0) 1354542d4d68SDavid S. Miller err = dev_queue_xmit(skb); 13551da177e4SLinus Torvalds else { 13561da177e4SLinus Torvalds err = -EINVAL; 13571da177e4SLinus Torvalds kfree_skb(skb); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds return err; 13601da177e4SLinus Torvalds } 13610a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output); 13621da177e4SLinus Torvalds 13638f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb) 13648f40b161SDavid S. Miller { 13658f40b161SDavid S. Miller return dev_queue_xmit(skb); 13668f40b161SDavid S. Miller } 13678f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output); 13688f40b161SDavid S. Miller 13691da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 13701da177e4SLinus Torvalds { 13711da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 13721da177e4SLinus Torvalds long sched_next = 0; 13731da177e4SLinus Torvalds unsigned long now = jiffies; 1374f72051b0SDavid S. Miller struct sk_buff *skb, *n; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13771da177e4SLinus Torvalds 1378f72051b0SDavid S. Miller skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { 1379f72051b0SDavid S. Miller long tdif = NEIGH_CB(skb)->sched_next - now; 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds if (tdif <= 0) { 1382f72051b0SDavid S. Miller struct net_device *dev = skb->dev; 138320e6074eSEric Dumazet 1384f72051b0SDavid S. Miller __skb_unlink(skb, &tbl->proxy_queue); 138520e6074eSEric Dumazet if (tbl->proxy_redo && netif_running(dev)) { 138620e6074eSEric Dumazet rcu_read_lock(); 1387f72051b0SDavid S. Miller tbl->proxy_redo(skb); 138820e6074eSEric Dumazet rcu_read_unlock(); 138920e6074eSEric Dumazet } else { 1390f72051b0SDavid S. Miller kfree_skb(skb); 139120e6074eSEric Dumazet } 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds dev_put(dev); 13941da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 13951da177e4SLinus Torvalds sched_next = tdif; 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 13981da177e4SLinus Torvalds if (sched_next) 13991da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 14001da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 14041da177e4SLinus Torvalds struct sk_buff *skb) 14051da177e4SLinus Torvalds { 14061da177e4SLinus Torvalds unsigned long now = jiffies; 14071da177e4SLinus Torvalds unsigned long sched_next = now + (net_random() % p->proxy_delay); 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds if (tbl->proxy_queue.qlen > p->proxy_qlen) { 14101da177e4SLinus Torvalds kfree_skb(skb); 14111da177e4SLinus Torvalds return; 14121da177e4SLinus Torvalds } 1413a61bbcf2SPatrick McHardy 1414a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1415a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 14181da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 14191da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 14201da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 14211da177e4SLinus Torvalds } 1422adf30907SEric Dumazet skb_dst_drop(skb); 14231da177e4SLinus Torvalds dev_hold(skb->dev); 14241da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 14251da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 14261da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 14271da177e4SLinus Torvalds } 14280a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue); 14291da177e4SLinus Torvalds 143097fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, 1431426b5303SEric W. Biederman struct net *net, int ifindex) 1432426b5303SEric W. Biederman { 1433426b5303SEric W. Biederman struct neigh_parms *p; 1434426b5303SEric W. Biederman 1435426b5303SEric W. Biederman for (p = &tbl->parms; p; p = p->next) { 1436878628fbSYOSHIFUJI Hideaki if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || 1437426b5303SEric W. Biederman (!p->dev && !ifindex)) 1438426b5303SEric W. Biederman return p; 1439426b5303SEric W. Biederman } 1440426b5303SEric W. Biederman 1441426b5303SEric W. Biederman return NULL; 1442426b5303SEric W. Biederman } 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 14451da177e4SLinus Torvalds struct neigh_table *tbl) 14461da177e4SLinus Torvalds { 1447426b5303SEric W. Biederman struct neigh_parms *p, *ref; 144800829823SStephen Hemminger struct net *net = dev_net(dev); 144900829823SStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 14501da177e4SLinus Torvalds 145197fd5bc7STobias Klauser ref = lookup_neigh_parms(tbl, net, 0); 1452426b5303SEric W. Biederman if (!ref) 1453426b5303SEric W. Biederman return NULL; 1454426b5303SEric W. Biederman 1455426b5303SEric W. Biederman p = kmemdup(ref, sizeof(*p), GFP_KERNEL); 14561da177e4SLinus Torvalds if (p) { 14571da177e4SLinus Torvalds p->tbl = tbl; 14581da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 14591da177e4SLinus Torvalds p->reachable_time = 14601da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 1461486b51d3SDenis V. Lunev 146200829823SStephen Hemminger if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 14631da177e4SLinus Torvalds kfree(p); 14641da177e4SLinus Torvalds return NULL; 14651da177e4SLinus Torvalds } 1466c7fb64dbSThomas Graf 1467c7fb64dbSThomas Graf dev_hold(dev); 1468c7fb64dbSThomas Graf p->dev = dev; 1469e42ea986SEric Dumazet write_pnet(&p->net, hold_net(net)); 14701da177e4SLinus Torvalds p->sysctl_table = NULL; 14711da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14721da177e4SLinus Torvalds p->next = tbl->parms.next; 14731da177e4SLinus Torvalds tbl->parms.next = p; 14741da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds return p; 14771da177e4SLinus Torvalds } 14780a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc); 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 14811da177e4SLinus Torvalds { 14821da177e4SLinus Torvalds struct neigh_parms *parms = 14831da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds neigh_parms_put(parms); 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 14891da177e4SLinus Torvalds { 14901da177e4SLinus Torvalds struct neigh_parms **p; 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 14931da177e4SLinus Torvalds return; 14941da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14951da177e4SLinus Torvalds for (p = &tbl->parms.next; *p; p = &(*p)->next) { 14961da177e4SLinus Torvalds if (*p == parms) { 14971da177e4SLinus Torvalds *p = parms->next; 14981da177e4SLinus Torvalds parms->dead = 1; 14991da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1500cecbb639SDavid S. Miller if (parms->dev) 1501cecbb639SDavid S. Miller dev_put(parms->dev); 15021da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 15031da177e4SLinus Torvalds return; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 15071da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_parms_release: not found\n"); 15081da177e4SLinus Torvalds } 15090a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release); 15101da177e4SLinus Torvalds 151106f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 15121da177e4SLinus Torvalds { 151357da52c1SYOSHIFUJI Hideaki release_net(neigh_parms_net(parms)); 15141da177e4SLinus Torvalds kfree(parms); 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 1517c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1518c2ecba71SPavel Emelianov 1519dcd2ba92SHiroaki SHIMODA static void neigh_table_init_no_netlink(struct neigh_table *tbl) 15201da177e4SLinus Torvalds { 15211da177e4SLinus Torvalds unsigned long now = jiffies; 15221da177e4SLinus Torvalds unsigned long phsize; 15231da177e4SLinus Torvalds 1524e42ea986SEric Dumazet write_pnet(&tbl->parms.net, &init_net); 15251da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 15261da177e4SLinus Torvalds tbl->parms.reachable_time = 15271da177e4SLinus Torvalds neigh_rand_reach_time(tbl->parms.base_reachable_time); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 15301da177e4SLinus Torvalds if (!tbl->stats) 15311da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15349b739ba5SAlexey Dobriyan if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat, 15359b739ba5SAlexey Dobriyan &neigh_stat_seq_fops, tbl)) 15361da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 15371da177e4SLinus Torvalds #endif 15381da177e4SLinus Torvalds 1539cd089336SDavid S. Miller RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3)); 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 154277d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 15431da177e4SLinus Torvalds 1544d6bf7817SEric Dumazet if (!tbl->nht || !tbl->phash_buckets) 15451da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1548e4c4e448SEric Dumazet INIT_DELAYED_WORK_DEFERRABLE(&tbl->gc_work, neigh_periodic_work); 1549e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time); 1550b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1551c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1552c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds tbl->last_flush = now; 15551da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1556bd89efc5SSimon Kelley } 1557bd89efc5SSimon Kelley 1558bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl) 1559bd89efc5SSimon Kelley { 1560bd89efc5SSimon Kelley struct neigh_table *tmp; 1561bd89efc5SSimon Kelley 1562bd89efc5SSimon Kelley neigh_table_init_no_netlink(tbl); 15631da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 1564bd89efc5SSimon Kelley for (tmp = neigh_tables; tmp; tmp = tmp->next) { 1565bd89efc5SSimon Kelley if (tmp->family == tbl->family) 1566bd89efc5SSimon Kelley break; 1567bd89efc5SSimon Kelley } 15681da177e4SLinus Torvalds tbl->next = neigh_tables; 15691da177e4SLinus Torvalds neigh_tables = tbl; 15701da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 1571bd89efc5SSimon Kelley 1572bd89efc5SSimon Kelley if (unlikely(tmp)) { 1573e005d193SJoe Perches pr_err("Registering multiple tables for family %d\n", 1574e005d193SJoe Perches tbl->family); 1575bd89efc5SSimon Kelley dump_stack(); 1576bd89efc5SSimon Kelley } 15771da177e4SLinus Torvalds } 15780a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init); 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl) 15811da177e4SLinus Torvalds { 15821da177e4SLinus Torvalds struct neigh_table **tp; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 1585a5c30b34STejun Heo cancel_delayed_work_sync(&tbl->gc_work); 15861da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 15871da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 15881da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 15891da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 1590e005d193SJoe Perches pr_crit("neighbour leakage\n"); 15911da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 15921da177e4SLinus Torvalds for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { 15931da177e4SLinus Torvalds if (*tp == tbl) { 15941da177e4SLinus Torvalds *tp = tbl->next; 15951da177e4SLinus Torvalds break; 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 15991da177e4SLinus Torvalds 16006193d2beSEric Dumazet call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu, 16016193d2beSEric Dumazet neigh_hash_free_rcu); 1602d6bf7817SEric Dumazet tbl->nht = NULL; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds kfree(tbl->phash_buckets); 16051da177e4SLinus Torvalds tbl->phash_buckets = NULL; 16061da177e4SLinus Torvalds 16073f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 16083f192b5cSAlexey Dobriyan 16093fcde74bSKirill Korotaev free_percpu(tbl->stats); 16103fcde74bSKirill Korotaev tbl->stats = NULL; 16113fcde74bSKirill Korotaev 16121da177e4SLinus Torvalds return 0; 16131da177e4SLinus Torvalds } 16140a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear); 16151da177e4SLinus Torvalds 1616c8822a4eSThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 16171da177e4SLinus Torvalds { 16183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1619a14a49d2SThomas Graf struct ndmsg *ndm; 1620a14a49d2SThomas Graf struct nlattr *dst_attr; 16211da177e4SLinus Torvalds struct neigh_table *tbl; 16221da177e4SLinus Torvalds struct net_device *dev = NULL; 1623a14a49d2SThomas Graf int err = -EINVAL; 16241da177e4SLinus Torvalds 1625110b2499SEric Dumazet ASSERT_RTNL(); 1626a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 16271da177e4SLinus Torvalds goto out; 16281da177e4SLinus Torvalds 1629a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1630a14a49d2SThomas Graf if (dst_attr == NULL) 1631a14a49d2SThomas Graf goto out; 1632a14a49d2SThomas Graf 1633a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1634a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1635110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 1636a14a49d2SThomas Graf if (dev == NULL) { 1637a14a49d2SThomas Graf err = -ENODEV; 1638a14a49d2SThomas Graf goto out; 1639a14a49d2SThomas Graf } 1640a14a49d2SThomas Graf } 1641a14a49d2SThomas Graf 16421da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 16431da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1644a14a49d2SThomas Graf struct neighbour *neigh; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 16471da177e4SLinus Torvalds continue; 16481da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16491da177e4SLinus Torvalds 1650a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 1651110b2499SEric Dumazet goto out; 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1654426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 1655110b2499SEric Dumazet goto out; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 1658a14a49d2SThomas Graf if (dev == NULL) 1659110b2499SEric Dumazet goto out; 16601da177e4SLinus Torvalds 1661a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1662a14a49d2SThomas Graf if (neigh == NULL) { 1663a14a49d2SThomas Graf err = -ENOENT; 1664110b2499SEric Dumazet goto out; 1665a14a49d2SThomas Graf } 1666a14a49d2SThomas Graf 1667a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 16681da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 16691da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1670a14a49d2SThomas Graf neigh_release(neigh); 1671110b2499SEric Dumazet goto out; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 1674a14a49d2SThomas Graf err = -EAFNOSUPPORT; 1675a14a49d2SThomas Graf 16761da177e4SLinus Torvalds out: 16771da177e4SLinus Torvalds return err; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 1680c8822a4eSThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 16811da177e4SLinus Torvalds { 16823b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 16835208debdSThomas Graf struct ndmsg *ndm; 16845208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 16851da177e4SLinus Torvalds struct neigh_table *tbl; 16861da177e4SLinus Torvalds struct net_device *dev = NULL; 16875208debdSThomas Graf int err; 16881da177e4SLinus Torvalds 1689110b2499SEric Dumazet ASSERT_RTNL(); 16905208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 16915208debdSThomas Graf if (err < 0) 16921da177e4SLinus Torvalds goto out; 16931da177e4SLinus Torvalds 16945208debdSThomas Graf err = -EINVAL; 16955208debdSThomas Graf if (tb[NDA_DST] == NULL) 16965208debdSThomas Graf goto out; 16975208debdSThomas Graf 16985208debdSThomas Graf ndm = nlmsg_data(nlh); 16995208debdSThomas Graf if (ndm->ndm_ifindex) { 1700110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 17015208debdSThomas Graf if (dev == NULL) { 17025208debdSThomas Graf err = -ENODEV; 17035208debdSThomas Graf goto out; 17045208debdSThomas Graf } 17055208debdSThomas Graf 17065208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 1707110b2499SEric Dumazet goto out; 17085208debdSThomas Graf } 17095208debdSThomas Graf 17101da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 17111da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 17125208debdSThomas Graf int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 17135208debdSThomas Graf struct neighbour *neigh; 17145208debdSThomas Graf void *dst, *lladdr; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 17171da177e4SLinus Torvalds continue; 17181da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17191da177e4SLinus Torvalds 17205208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 1721110b2499SEric Dumazet goto out; 17225208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 17235208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 172662dd9318SVille Nuorvala struct pneigh_entry *pn; 172762dd9318SVille Nuorvala 17285208debdSThomas Graf err = -ENOBUFS; 1729426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 173062dd9318SVille Nuorvala if (pn) { 173162dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 173262dd9318SVille Nuorvala err = 0; 173362dd9318SVille Nuorvala } 1734110b2499SEric Dumazet goto out; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17375208debdSThomas Graf if (dev == NULL) 1738110b2499SEric Dumazet goto out; 17391da177e4SLinus Torvalds 17405208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 17415208debdSThomas Graf if (neigh == NULL) { 17425208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 17431da177e4SLinus Torvalds err = -ENOENT; 1744110b2499SEric Dumazet goto out; 17455208debdSThomas Graf } 17465208debdSThomas Graf 17475208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 17485208debdSThomas Graf if (IS_ERR(neigh)) { 17495208debdSThomas Graf err = PTR_ERR(neigh); 1750110b2499SEric Dumazet goto out; 17511da177e4SLinus Torvalds } 17525208debdSThomas Graf } else { 17535208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 17545208debdSThomas Graf err = -EEXIST; 17555208debdSThomas Graf neigh_release(neigh); 1756110b2499SEric Dumazet goto out; 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds 17595208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 17605208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 17615208debdSThomas Graf } 17621da177e4SLinus Torvalds 17630c5c2d30SEric Biederman if (ndm->ndm_flags & NTF_USE) { 17640c5c2d30SEric Biederman neigh_event_send(neigh, NULL); 17650c5c2d30SEric Biederman err = 0; 17660c5c2d30SEric Biederman } else 17675208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 17685208debdSThomas Graf neigh_release(neigh); 1769110b2499SEric Dumazet goto out; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17735208debdSThomas Graf err = -EAFNOSUPPORT; 17741da177e4SLinus Torvalds out: 17751da177e4SLinus Torvalds return err; 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 1778c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1779c7fb64dbSThomas Graf { 1780ca860fb3SThomas Graf struct nlattr *nest; 1781e386c6ebSThomas Graf 1782ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1783ca860fb3SThomas Graf if (nest == NULL) 1784ca860fb3SThomas Graf return -ENOBUFS; 1785c7fb64dbSThomas Graf 17869a6308d7SDavid S. Miller if ((parms->dev && 17879a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) || 17889a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) || 17899a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_QUEUE_LENBYTES, parms->queue_len_bytes) || 17908b5c171bSEric Dumazet /* approximative value for deprecated QUEUE_LEN (in packets) */ 17919a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_QUEUE_LEN, 17928b5c171bSEric Dumazet DIV_ROUND_UP(parms->queue_len_bytes, 17939a6308d7SDavid S. Miller SKB_TRUESIZE(ETH_FRAME_LEN))) || 17949a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen) || 17959a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_APP_PROBES, parms->app_probes) || 17969a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes) || 17979a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes) || 17989a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) || 17999a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME, 18009a6308d7SDavid S. Miller parms->base_reachable_time) || 18019a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_GC_STALETIME, parms->gc_staletime) || 18029a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME, 18039a6308d7SDavid S. Miller parms->delay_probe_time) || 18049a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_RETRANS_TIME, parms->retrans_time) || 18059a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay) || 18069a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_PROXY_DELAY, parms->proxy_delay) || 18079a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_LOCKTIME, parms->locktime)) 18089a6308d7SDavid S. Miller goto nla_put_failure; 1809ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1810c7fb64dbSThomas Graf 1811ca860fb3SThomas Graf nla_put_failure: 1812bc3ed28cSThomas Graf nla_nest_cancel(skb, nest); 1813bc3ed28cSThomas Graf return -EMSGSIZE; 1814c7fb64dbSThomas Graf } 1815c7fb64dbSThomas Graf 1816ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1817ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1818c7fb64dbSThomas Graf { 1819c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1820c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1821c7fb64dbSThomas Graf 1822ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1823ca860fb3SThomas Graf if (nlh == NULL) 182426932566SPatrick McHardy return -EMSGSIZE; 1825c7fb64dbSThomas Graf 1826ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1827c7fb64dbSThomas Graf 1828c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1829c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 18309ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 18319ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1832c7fb64dbSThomas Graf 18339a6308d7SDavid S. Miller if (nla_put_string(skb, NDTA_NAME, tbl->id) || 18349a6308d7SDavid S. Miller nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval) || 18359a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) || 18369a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) || 18379a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3)) 18389a6308d7SDavid S. Miller goto nla_put_failure; 1839c7fb64dbSThomas Graf { 1840c7fb64dbSThomas Graf unsigned long now = jiffies; 1841c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1842c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1843d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1844c7fb64dbSThomas Graf struct ndt_config ndc = { 1845c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1846c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1847c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1848c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1849c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1850c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1851c7fb64dbSThomas Graf }; 1852c7fb64dbSThomas Graf 1853d6bf7817SEric Dumazet rcu_read_lock_bh(); 1854d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 18552c2aba6cSDavid S. Miller ndc.ndtc_hash_rnd = nht->hash_rnd[0]; 1856cd089336SDavid S. Miller ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1); 1857d6bf7817SEric Dumazet rcu_read_unlock_bh(); 1858d6bf7817SEric Dumazet 18599a6308d7SDavid S. Miller if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc)) 18609a6308d7SDavid S. Miller goto nla_put_failure; 1861c7fb64dbSThomas Graf } 1862c7fb64dbSThomas Graf 1863c7fb64dbSThomas Graf { 1864c7fb64dbSThomas Graf int cpu; 1865c7fb64dbSThomas Graf struct ndt_stats ndst; 1866c7fb64dbSThomas Graf 1867c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1868c7fb64dbSThomas Graf 18696f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1870c7fb64dbSThomas Graf struct neigh_statistics *st; 1871c7fb64dbSThomas Graf 1872c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1873c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1874c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1875c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1876c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1877c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1878c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1879c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1880c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1881c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1882c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1883c7fb64dbSThomas Graf } 1884c7fb64dbSThomas Graf 18859a6308d7SDavid S. Miller if (nla_put(skb, NDTA_STATS, sizeof(ndst), &ndst)) 18869a6308d7SDavid S. Miller goto nla_put_failure; 1887c7fb64dbSThomas Graf } 1888c7fb64dbSThomas Graf 1889c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1890c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1891ca860fb3SThomas Graf goto nla_put_failure; 1892c7fb64dbSThomas Graf 1893c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1894ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1895c7fb64dbSThomas Graf 1896ca860fb3SThomas Graf nla_put_failure: 1897c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 189826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 189926932566SPatrick McHardy return -EMSGSIZE; 1900c7fb64dbSThomas Graf } 1901c7fb64dbSThomas Graf 1902ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1903ca860fb3SThomas Graf struct neigh_table *tbl, 1904c7fb64dbSThomas Graf struct neigh_parms *parms, 1905ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1906ca860fb3SThomas Graf unsigned int flags) 1907c7fb64dbSThomas Graf { 1908c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1909c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1910c7fb64dbSThomas Graf 1911ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1912ca860fb3SThomas Graf if (nlh == NULL) 191326932566SPatrick McHardy return -EMSGSIZE; 1914c7fb64dbSThomas Graf 1915ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1916c7fb64dbSThomas Graf 1917c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1918c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 19199ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 19209ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1921c7fb64dbSThomas Graf 1922ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1923ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1924ca860fb3SThomas Graf goto errout; 1925c7fb64dbSThomas Graf 1926c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1927ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1928ca860fb3SThomas Graf errout: 1929c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 193026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 193126932566SPatrick McHardy return -EMSGSIZE; 1932c7fb64dbSThomas Graf } 1933c7fb64dbSThomas Graf 1934ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 19356b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 19366b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 19376b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 19386b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 19396b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 19406b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 19416b3f8674SThomas Graf }; 19426b3f8674SThomas Graf 1943ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 19446b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 19456b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 19466b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 19476b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 19486b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 19496b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 19506b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 19516b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 19526b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 19536b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 19546b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 19556b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 19566b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 19576b3f8674SThomas Graf }; 19586b3f8674SThomas Graf 1959c8822a4eSThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1960c7fb64dbSThomas Graf { 19613b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1962c7fb64dbSThomas Graf struct neigh_table *tbl; 19636b3f8674SThomas Graf struct ndtmsg *ndtmsg; 19646b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 19656b3f8674SThomas Graf int err; 1966c7fb64dbSThomas Graf 19676b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 19686b3f8674SThomas Graf nl_neightbl_policy); 19696b3f8674SThomas Graf if (err < 0) 19706b3f8674SThomas Graf goto errout; 1971c7fb64dbSThomas Graf 19726b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 19736b3f8674SThomas Graf err = -EINVAL; 19746b3f8674SThomas Graf goto errout; 19756b3f8674SThomas Graf } 19766b3f8674SThomas Graf 19776b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1978c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1979c7fb64dbSThomas Graf for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1980c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1981c7fb64dbSThomas Graf continue; 1982c7fb64dbSThomas Graf 19836b3f8674SThomas Graf if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) 1984c7fb64dbSThomas Graf break; 1985c7fb64dbSThomas Graf } 1986c7fb64dbSThomas Graf 1987c7fb64dbSThomas Graf if (tbl == NULL) { 1988c7fb64dbSThomas Graf err = -ENOENT; 19896b3f8674SThomas Graf goto errout_locked; 1990c7fb64dbSThomas Graf } 1991c7fb64dbSThomas Graf 1992c7fb64dbSThomas Graf /* 1993c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1994c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1995c7fb64dbSThomas Graf */ 1996c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1997c7fb64dbSThomas Graf 19986b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 19996b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 2000c7fb64dbSThomas Graf struct neigh_parms *p; 20016b3f8674SThomas Graf int i, ifindex = 0; 2002c7fb64dbSThomas Graf 20036b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 20046b3f8674SThomas Graf nl_ntbl_parm_policy); 20056b3f8674SThomas Graf if (err < 0) 20066b3f8674SThomas Graf goto errout_tbl_lock; 2007c7fb64dbSThomas Graf 20086b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 20096b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 2010c7fb64dbSThomas Graf 201197fd5bc7STobias Klauser p = lookup_neigh_parms(tbl, net, ifindex); 2012c7fb64dbSThomas Graf if (p == NULL) { 2013c7fb64dbSThomas Graf err = -ENOENT; 20146b3f8674SThomas Graf goto errout_tbl_lock; 2015c7fb64dbSThomas Graf } 2016c7fb64dbSThomas Graf 20176b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 20186b3f8674SThomas Graf if (tbp[i] == NULL) 20196b3f8674SThomas Graf continue; 2020c7fb64dbSThomas Graf 20216b3f8674SThomas Graf switch (i) { 20226b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 20238b5c171bSEric Dumazet p->queue_len_bytes = nla_get_u32(tbp[i]) * 20248b5c171bSEric Dumazet SKB_TRUESIZE(ETH_FRAME_LEN); 20258b5c171bSEric Dumazet break; 20268b5c171bSEric Dumazet case NDTPA_QUEUE_LENBYTES: 20278b5c171bSEric Dumazet p->queue_len_bytes = nla_get_u32(tbp[i]); 20286b3f8674SThomas Graf break; 20296b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 20306b3f8674SThomas Graf p->proxy_qlen = nla_get_u32(tbp[i]); 20316b3f8674SThomas Graf break; 20326b3f8674SThomas Graf case NDTPA_APP_PROBES: 20336b3f8674SThomas Graf p->app_probes = nla_get_u32(tbp[i]); 20346b3f8674SThomas Graf break; 20356b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 20366b3f8674SThomas Graf p->ucast_probes = nla_get_u32(tbp[i]); 20376b3f8674SThomas Graf break; 20386b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 20396b3f8674SThomas Graf p->mcast_probes = nla_get_u32(tbp[i]); 20406b3f8674SThomas Graf break; 20416b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 20426b3f8674SThomas Graf p->base_reachable_time = nla_get_msecs(tbp[i]); 20436b3f8674SThomas Graf break; 20446b3f8674SThomas Graf case NDTPA_GC_STALETIME: 20456b3f8674SThomas Graf p->gc_staletime = nla_get_msecs(tbp[i]); 20466b3f8674SThomas Graf break; 20476b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 20486b3f8674SThomas Graf p->delay_probe_time = nla_get_msecs(tbp[i]); 20496b3f8674SThomas Graf break; 20506b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 20516b3f8674SThomas Graf p->retrans_time = nla_get_msecs(tbp[i]); 20526b3f8674SThomas Graf break; 20536b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 20546b3f8674SThomas Graf p->anycast_delay = nla_get_msecs(tbp[i]); 20556b3f8674SThomas Graf break; 20566b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 20576b3f8674SThomas Graf p->proxy_delay = nla_get_msecs(tbp[i]); 20586b3f8674SThomas Graf break; 20596b3f8674SThomas Graf case NDTPA_LOCKTIME: 20606b3f8674SThomas Graf p->locktime = nla_get_msecs(tbp[i]); 20616b3f8674SThomas Graf break; 2062c7fb64dbSThomas Graf } 20636b3f8674SThomas Graf } 20646b3f8674SThomas Graf } 20656b3f8674SThomas Graf 20666b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 20676b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 20686b3f8674SThomas Graf 20696b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 20706b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 20716b3f8674SThomas Graf 20726b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 20736b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 20746b3f8674SThomas Graf 20756b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 20766b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 2077c7fb64dbSThomas Graf 2078c7fb64dbSThomas Graf err = 0; 2079c7fb64dbSThomas Graf 20806b3f8674SThomas Graf errout_tbl_lock: 2081c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 20826b3f8674SThomas Graf errout_locked: 2083c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 20846b3f8674SThomas Graf errout: 2085c7fb64dbSThomas Graf return err; 2086c7fb64dbSThomas Graf } 2087c7fb64dbSThomas Graf 2088c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 2089c7fb64dbSThomas Graf { 20903b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2091ca860fb3SThomas Graf int family, tidx, nidx = 0; 2092ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 2093ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 2094c7fb64dbSThomas Graf struct neigh_table *tbl; 2095c7fb64dbSThomas Graf 2096ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 2097c7fb64dbSThomas Graf 2098c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 2099ca860fb3SThomas Graf for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) { 2100c7fb64dbSThomas Graf struct neigh_parms *p; 2101c7fb64dbSThomas Graf 2102ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 2103c7fb64dbSThomas Graf continue; 2104c7fb64dbSThomas Graf 210515e47304SEric W. Biederman if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid, 2106ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 2107ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2108c7fb64dbSThomas Graf break; 2109c7fb64dbSThomas Graf 2110426b5303SEric W. Biederman for (nidx = 0, p = tbl->parms.next; p; p = p->next) { 2111878628fbSYOSHIFUJI Hideaki if (!net_eq(neigh_parms_net(p), net)) 2112426b5303SEric W. Biederman continue; 2113426b5303SEric W. Biederman 2114efc683fcSGautam Kachroo if (nidx < neigh_skip) 2115efc683fcSGautam Kachroo goto next; 2116c7fb64dbSThomas Graf 2117ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 211815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2119ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 2120ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 2121ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2122c7fb64dbSThomas Graf goto out; 2123efc683fcSGautam Kachroo next: 2124efc683fcSGautam Kachroo nidx++; 2125c7fb64dbSThomas Graf } 2126c7fb64dbSThomas Graf 2127ca860fb3SThomas Graf neigh_skip = 0; 2128c7fb64dbSThomas Graf } 2129c7fb64dbSThomas Graf out: 2130c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 2131ca860fb3SThomas Graf cb->args[0] = tidx; 2132ca860fb3SThomas Graf cb->args[1] = nidx; 2133c7fb64dbSThomas Graf 2134c7fb64dbSThomas Graf return skb->len; 2135c7fb64dbSThomas Graf } 21361da177e4SLinus Torvalds 21378b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 21388b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 21391da177e4SLinus Torvalds { 21401da177e4SLinus Torvalds unsigned long now = jiffies; 21411da177e4SLinus Torvalds struct nda_cacheinfo ci; 21428b8aec50SThomas Graf struct nlmsghdr *nlh; 21438b8aec50SThomas Graf struct ndmsg *ndm; 21441da177e4SLinus Torvalds 21458b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 21468b8aec50SThomas Graf if (nlh == NULL) 214726932566SPatrick McHardy return -EMSGSIZE; 21488b8aec50SThomas Graf 21498b8aec50SThomas Graf ndm = nlmsg_data(nlh); 21508b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 21519ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 21529ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 21538b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 21548b8aec50SThomas Graf ndm->ndm_type = neigh->type; 21558b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 21561da177e4SLinus Torvalds 21579a6308d7SDavid S. Miller if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key)) 21589a6308d7SDavid S. Miller goto nla_put_failure; 21598b8aec50SThomas Graf 21608b8aec50SThomas Graf read_lock_bh(&neigh->lock); 21618b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 21620ed8ddf4SEric Dumazet if (neigh->nud_state & NUD_VALID) { 21630ed8ddf4SEric Dumazet char haddr[MAX_ADDR_LEN]; 21640ed8ddf4SEric Dumazet 21650ed8ddf4SEric Dumazet neigh_ha_snapshot(haddr, neigh, neigh->dev); 21660ed8ddf4SEric Dumazet if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) { 21678b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21688b8aec50SThomas Graf goto nla_put_failure; 21698b8aec50SThomas Graf } 21700ed8ddf4SEric Dumazet } 21718b8aec50SThomas Graf 2172b9f5f52cSStephen Hemminger ci.ndm_used = jiffies_to_clock_t(now - neigh->used); 2173b9f5f52cSStephen Hemminger ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); 2174b9f5f52cSStephen Hemminger ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); 21758b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 21768b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21778b8aec50SThomas Graf 21789a6308d7SDavid S. Miller if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) || 21799a6308d7SDavid S. Miller nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) 21809a6308d7SDavid S. Miller goto nla_put_failure; 21818b8aec50SThomas Graf 21828b8aec50SThomas Graf return nlmsg_end(skb, nlh); 21838b8aec50SThomas Graf 21848b8aec50SThomas Graf nla_put_failure: 218526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 218626932566SPatrick McHardy return -EMSGSIZE; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 218984920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn, 219084920c14STony Zelenoff u32 pid, u32 seq, int type, unsigned int flags, 219184920c14STony Zelenoff struct neigh_table *tbl) 219284920c14STony Zelenoff { 219384920c14STony Zelenoff struct nlmsghdr *nlh; 219484920c14STony Zelenoff struct ndmsg *ndm; 219584920c14STony Zelenoff 219684920c14STony Zelenoff nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 219784920c14STony Zelenoff if (nlh == NULL) 219884920c14STony Zelenoff return -EMSGSIZE; 219984920c14STony Zelenoff 220084920c14STony Zelenoff ndm = nlmsg_data(nlh); 220184920c14STony Zelenoff ndm->ndm_family = tbl->family; 220284920c14STony Zelenoff ndm->ndm_pad1 = 0; 220384920c14STony Zelenoff ndm->ndm_pad2 = 0; 220484920c14STony Zelenoff ndm->ndm_flags = pn->flags | NTF_PROXY; 220584920c14STony Zelenoff ndm->ndm_type = NDA_DST; 220684920c14STony Zelenoff ndm->ndm_ifindex = pn->dev->ifindex; 220784920c14STony Zelenoff ndm->ndm_state = NUD_NONE; 220884920c14STony Zelenoff 22099a6308d7SDavid S. Miller if (nla_put(skb, NDA_DST, tbl->key_len, pn->key)) 22109a6308d7SDavid S. Miller goto nla_put_failure; 221184920c14STony Zelenoff 221284920c14STony Zelenoff return nlmsg_end(skb, nlh); 221384920c14STony Zelenoff 221484920c14STony Zelenoff nla_put_failure: 221584920c14STony Zelenoff nlmsg_cancel(skb, nlh); 221684920c14STony Zelenoff return -EMSGSIZE; 221784920c14STony Zelenoff } 221884920c14STony Zelenoff 2219d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2220d961db35SThomas Graf { 2221d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2222d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2223d961db35SThomas Graf } 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 22261da177e4SLinus Torvalds struct netlink_callback *cb) 22271da177e4SLinus Torvalds { 22283b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 22291da177e4SLinus Torvalds struct neighbour *n; 22301da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 22311da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 2232d6bf7817SEric Dumazet struct neigh_hash_table *nht; 22331da177e4SLinus Torvalds 2234d6bf7817SEric Dumazet rcu_read_lock_bh(); 2235d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2236d6bf7817SEric Dumazet 22374bd6683bSEric Dumazet for (h = s_h; h < (1 << nht->hash_shift); h++) { 22381da177e4SLinus Torvalds if (h > s_h) 22391da177e4SLinus Torvalds s_idx = 0; 2240767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0; 2241767e97e1SEric Dumazet n != NULL; 2242767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 224309ad9bc7SOctavian Purdila if (!net_eq(dev_net(n->dev), net)) 2244426b5303SEric W. Biederman continue; 2245efc683fcSGautam Kachroo if (idx < s_idx) 2246efc683fcSGautam Kachroo goto next; 224715e47304SEric W. Biederman if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, 22481da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2249b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 2250b6544c0bSJamal Hadi Salim NLM_F_MULTI) <= 0) { 22511da177e4SLinus Torvalds rc = -1; 22521da177e4SLinus Torvalds goto out; 22531da177e4SLinus Torvalds } 2254efc683fcSGautam Kachroo next: 2255efc683fcSGautam Kachroo idx++; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds } 22581da177e4SLinus Torvalds rc = skb->len; 22591da177e4SLinus Torvalds out: 2260d6bf7817SEric Dumazet rcu_read_unlock_bh(); 22611da177e4SLinus Torvalds cb->args[1] = h; 22621da177e4SLinus Torvalds cb->args[2] = idx; 22631da177e4SLinus Torvalds return rc; 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds 226684920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 226784920c14STony Zelenoff struct netlink_callback *cb) 226884920c14STony Zelenoff { 226984920c14STony Zelenoff struct pneigh_entry *n; 227084920c14STony Zelenoff struct net *net = sock_net(skb->sk); 227184920c14STony Zelenoff int rc, h, s_h = cb->args[3]; 227284920c14STony Zelenoff int idx, s_idx = idx = cb->args[4]; 227384920c14STony Zelenoff 227484920c14STony Zelenoff read_lock_bh(&tbl->lock); 227584920c14STony Zelenoff 22764bd6683bSEric Dumazet for (h = s_h; h <= PNEIGH_HASHMASK; h++) { 227784920c14STony Zelenoff if (h > s_h) 227884920c14STony Zelenoff s_idx = 0; 227984920c14STony Zelenoff for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) { 228084920c14STony Zelenoff if (dev_net(n->dev) != net) 228184920c14STony Zelenoff continue; 228284920c14STony Zelenoff if (idx < s_idx) 228384920c14STony Zelenoff goto next; 228415e47304SEric W. Biederman if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, 228584920c14STony Zelenoff cb->nlh->nlmsg_seq, 228684920c14STony Zelenoff RTM_NEWNEIGH, 228784920c14STony Zelenoff NLM_F_MULTI, tbl) <= 0) { 228884920c14STony Zelenoff read_unlock_bh(&tbl->lock); 228984920c14STony Zelenoff rc = -1; 229084920c14STony Zelenoff goto out; 229184920c14STony Zelenoff } 229284920c14STony Zelenoff next: 229384920c14STony Zelenoff idx++; 229484920c14STony Zelenoff } 229584920c14STony Zelenoff } 229684920c14STony Zelenoff 229784920c14STony Zelenoff read_unlock_bh(&tbl->lock); 229884920c14STony Zelenoff rc = skb->len; 229984920c14STony Zelenoff out: 230084920c14STony Zelenoff cb->args[3] = h; 230184920c14STony Zelenoff cb->args[4] = idx; 230284920c14STony Zelenoff return rc; 230384920c14STony Zelenoff 230484920c14STony Zelenoff } 230584920c14STony Zelenoff 2306c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 23071da177e4SLinus Torvalds { 23081da177e4SLinus Torvalds struct neigh_table *tbl; 23091da177e4SLinus Torvalds int t, family, s_t; 231084920c14STony Zelenoff int proxy = 0; 23114bd6683bSEric Dumazet int err; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 23148b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 231584920c14STony Zelenoff 231684920c14STony Zelenoff /* check for full ndmsg structure presence, family member is 231784920c14STony Zelenoff * the same for both structures 231884920c14STony Zelenoff */ 231984920c14STony Zelenoff if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) && 232084920c14STony Zelenoff ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY) 232184920c14STony Zelenoff proxy = 1; 232284920c14STony Zelenoff 23231da177e4SLinus Torvalds s_t = cb->args[0]; 23241da177e4SLinus Torvalds 23254bd6683bSEric Dumazet for (tbl = neigh_tables, t = 0; tbl; 232684920c14STony Zelenoff tbl = tbl->next, t++) { 23271da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 23281da177e4SLinus Torvalds continue; 23291da177e4SLinus Torvalds if (t > s_t) 23301da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 23311da177e4SLinus Torvalds sizeof(cb->args[0])); 233284920c14STony Zelenoff if (proxy) 233384920c14STony Zelenoff err = pneigh_dump_table(tbl, skb, cb); 233484920c14STony Zelenoff else 233584920c14STony Zelenoff err = neigh_dump_table(tbl, skb, cb); 23364bd6683bSEric Dumazet if (err < 0) 23374bd6683bSEric Dumazet break; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds cb->args[0] = t; 23421da177e4SLinus Torvalds return skb->len; 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 23461da177e4SLinus Torvalds { 23471da177e4SLinus Torvalds int chain; 2348d6bf7817SEric Dumazet struct neigh_hash_table *nht; 23491da177e4SLinus Torvalds 2350d6bf7817SEric Dumazet rcu_read_lock_bh(); 2351d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2352d6bf7817SEric Dumazet 2353767e97e1SEric Dumazet read_lock(&tbl->lock); /* avoid resizes */ 2354cd089336SDavid S. Miller for (chain = 0; chain < (1 << nht->hash_shift); chain++) { 23551da177e4SLinus Torvalds struct neighbour *n; 23561da177e4SLinus Torvalds 2357767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[chain]); 2358767e97e1SEric Dumazet n != NULL; 2359767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) 23601da177e4SLinus Torvalds cb(n, cookie); 23611da177e4SLinus Torvalds } 2362d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2363d6bf7817SEric Dumazet rcu_read_unlock_bh(); 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 23681da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 23691da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 23701da177e4SLinus Torvalds { 23711da177e4SLinus Torvalds int chain; 2372d6bf7817SEric Dumazet struct neigh_hash_table *nht; 23731da177e4SLinus Torvalds 2374d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 2375d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 2376cd089336SDavid S. Miller for (chain = 0; chain < (1 << nht->hash_shift); chain++) { 2377767e97e1SEric Dumazet struct neighbour *n; 2378767e97e1SEric Dumazet struct neighbour __rcu **np; 23791da177e4SLinus Torvalds 2380d6bf7817SEric Dumazet np = &nht->hash_buckets[chain]; 2381767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 2382767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 23831da177e4SLinus Torvalds int release; 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds write_lock(&n->lock); 23861da177e4SLinus Torvalds release = cb(n); 23871da177e4SLinus Torvalds if (release) { 2388767e97e1SEric Dumazet rcu_assign_pointer(*np, 2389767e97e1SEric Dumazet rcu_dereference_protected(n->next, 2390767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 23911da177e4SLinus Torvalds n->dead = 1; 23921da177e4SLinus Torvalds } else 23931da177e4SLinus Torvalds np = &n->next; 23941da177e4SLinus Torvalds write_unlock(&n->lock); 23954f494554SThomas Graf if (release) 23964f494554SThomas Graf neigh_cleanup_and_release(n); 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds } 2399ecbb4169SAlexey Kuznetsov } 24001da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 24051da177e4SLinus Torvalds { 24061da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24071218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2408d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 24091da177e4SLinus Torvalds struct neighbour *n = NULL; 24101da177e4SLinus Torvalds int bucket = state->bucket; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 2413cd089336SDavid S. Miller for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) { 2414767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[bucket]); 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds while (n) { 2417878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2418426b5303SEric W. Biederman goto next; 24191da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24201da177e4SLinus Torvalds loff_t fakep = 0; 24211da177e4SLinus Torvalds void *v; 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 24241da177e4SLinus Torvalds if (!v) 24251da177e4SLinus Torvalds goto next; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 24281da177e4SLinus Torvalds break; 24291da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 24301da177e4SLinus Torvalds break; 24311da177e4SLinus Torvalds next: 2432767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds if (n) 24361da177e4SLinus Torvalds break; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds state->bucket = bucket; 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds return n; 24411da177e4SLinus Torvalds } 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 24441da177e4SLinus Torvalds struct neighbour *n, 24451da177e4SLinus Torvalds loff_t *pos) 24461da177e4SLinus Torvalds { 24471da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24481218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2449d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24521da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 24531da177e4SLinus Torvalds if (v) 24541da177e4SLinus Torvalds return n; 24551da177e4SLinus Torvalds } 2456767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds while (1) { 24591da177e4SLinus Torvalds while (n) { 2460878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2461426b5303SEric W. Biederman goto next; 24621da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24631da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 24641da177e4SLinus Torvalds if (v) 24651da177e4SLinus Torvalds return n; 24661da177e4SLinus Torvalds goto next; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 24691da177e4SLinus Torvalds break; 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 24721da177e4SLinus Torvalds break; 24731da177e4SLinus Torvalds next: 2474767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds if (n) 24781da177e4SLinus Torvalds break; 24791da177e4SLinus Torvalds 2480cd089336SDavid S. Miller if (++state->bucket >= (1 << nht->hash_shift)) 24811da177e4SLinus Torvalds break; 24821da177e4SLinus Torvalds 2483767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[state->bucket]); 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds if (n && pos) 24871da177e4SLinus Torvalds --(*pos); 24881da177e4SLinus Torvalds return n; 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 24921da177e4SLinus Torvalds { 24931da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds if (n) { 2496745e2031SChris Larson --(*pos); 24971da177e4SLinus Torvalds while (*pos) { 24981da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 24991da177e4SLinus Torvalds if (!n) 25001da177e4SLinus Torvalds break; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds } 25031da177e4SLinus Torvalds return *pos ? NULL : n; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25091218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 25101da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 25111da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 25121da177e4SLinus Torvalds int bucket = state->bucket; 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 25151da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 25161da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2517878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2518426b5303SEric W. Biederman pn = pn->next; 25191da177e4SLinus Torvalds if (pn) 25201da177e4SLinus Torvalds break; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds state->bucket = bucket; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds return pn; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 25281da177e4SLinus Torvalds struct pneigh_entry *pn, 25291da177e4SLinus Torvalds loff_t *pos) 25301da177e4SLinus Torvalds { 25311da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25321218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 25331da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 25341da177e4SLinus Torvalds 2535df07a94cSJorge Boncompte [DTI2] do { 25361da177e4SLinus Torvalds pn = pn->next; 2537df07a94cSJorge Boncompte [DTI2] } while (pn && !net_eq(pneigh_net(pn), net)); 2538df07a94cSJorge Boncompte [DTI2] 25391da177e4SLinus Torvalds while (!pn) { 25401da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 25411da177e4SLinus Torvalds break; 25421da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2543878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2544426b5303SEric W. Biederman pn = pn->next; 25451da177e4SLinus Torvalds if (pn) 25461da177e4SLinus Torvalds break; 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds if (pn && pos) 25501da177e4SLinus Torvalds --(*pos); 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds return pn; 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 25561da177e4SLinus Torvalds { 25571da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds if (pn) { 2560745e2031SChris Larson --(*pos); 25611da177e4SLinus Torvalds while (*pos) { 25621da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 25631da177e4SLinus Torvalds if (!pn) 25641da177e4SLinus Torvalds break; 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds return *pos ? NULL : pn; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 25711da177e4SLinus Torvalds { 25721da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25731da177e4SLinus Torvalds void *rc; 2574745e2031SChris Larson loff_t idxpos = *pos; 25751da177e4SLinus Torvalds 2576745e2031SChris Larson rc = neigh_get_idx(seq, &idxpos); 25771da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 2578745e2031SChris Larson rc = pneigh_get_idx(seq, &idxpos); 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds return rc; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 2584d6bf7817SEric Dumazet __acquires(rcu_bh) 25851da177e4SLinus Torvalds { 25861da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds state->tbl = tbl; 25891da177e4SLinus Torvalds state->bucket = 0; 25901da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 25911da177e4SLinus Torvalds 2592d6bf7817SEric Dumazet rcu_read_lock_bh(); 2593d6bf7817SEric Dumazet state->nht = rcu_dereference_bh(tbl->nht); 2594767e97e1SEric Dumazet 2595745e2031SChris Larson return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; 25961da177e4SLinus Torvalds } 25971da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 25981da177e4SLinus Torvalds 25991da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds struct neigh_seq_state *state; 26021da177e4SLinus Torvalds void *rc; 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2605bff69732SChris Larson rc = neigh_get_first(seq); 26061da177e4SLinus Torvalds goto out; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds state = seq->private; 26101da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 26111da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 26121da177e4SLinus Torvalds if (rc) 26131da177e4SLinus Torvalds goto out; 26141da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 26151da177e4SLinus Torvalds rc = pneigh_get_first(seq); 26161da177e4SLinus Torvalds } else { 26171da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 26181da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds out: 26211da177e4SLinus Torvalds ++(*pos); 26221da177e4SLinus Torvalds return rc; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 2627d6bf7817SEric Dumazet __releases(rcu_bh) 26281da177e4SLinus Torvalds { 2629d6bf7817SEric Dumazet rcu_read_unlock_bh(); 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds /* statistics via seq_file */ 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 26361da177e4SLinus Torvalds { 263781c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26381da177e4SLinus Torvalds int cpu; 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds if (*pos == 0) 26411da177e4SLinus Torvalds return SEQ_START_TOKEN; 26421da177e4SLinus Torvalds 26430f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 26441da177e4SLinus Torvalds if (!cpu_possible(cpu)) 26451da177e4SLinus Torvalds continue; 26461da177e4SLinus Torvalds *pos = cpu+1; 26471da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds return NULL; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26531da177e4SLinus Torvalds { 265481c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26551da177e4SLinus Torvalds int cpu; 26561da177e4SLinus Torvalds 26570f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 26581da177e4SLinus Torvalds if (!cpu_possible(cpu)) 26591da177e4SLinus Torvalds continue; 26601da177e4SLinus Torvalds *pos = cpu+1; 26611da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds return NULL; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 26671da177e4SLinus Torvalds { 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 26721da177e4SLinus Torvalds { 267381c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26741da177e4SLinus Torvalds struct neigh_statistics *st = v; 26751da177e4SLinus Torvalds 26761da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26779a6d276eSNeil 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"); 26781da177e4SLinus Torvalds return 0; 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 26829a6d276eSNeil Horman "%08lx %08lx %08lx %08lx %08lx\n", 26831da177e4SLinus Torvalds atomic_read(&tbl->entries), 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds st->allocs, 26861da177e4SLinus Torvalds st->destroys, 26871da177e4SLinus Torvalds st->hash_grows, 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds st->lookups, 26901da177e4SLinus Torvalds st->hits, 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds st->res_failed, 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds st->rcv_probes_mcast, 26951da177e4SLinus Torvalds st->rcv_probes_ucast, 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds st->periodic_gc_runs, 26989a6d276eSNeil Horman st->forced_gc_runs, 26999a6d276eSNeil Horman st->unres_discards 27001da177e4SLinus Torvalds ); 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds return 0; 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 2705f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 27061da177e4SLinus Torvalds .start = neigh_stat_seq_start, 27071da177e4SLinus Torvalds .next = neigh_stat_seq_next, 27081da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 27091da177e4SLinus Torvalds .show = neigh_stat_seq_show, 27101da177e4SLinus Torvalds }; 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 27131da177e4SLinus Torvalds { 27141da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds if (!ret) { 27171da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 271881c1ebfcSAlexey Dobriyan sf->private = PDE(inode)->data; 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds return ret; 27211da177e4SLinus Torvalds }; 27221da177e4SLinus Torvalds 27239a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 27241da177e4SLinus Torvalds .owner = THIS_MODULE, 27251da177e4SLinus Torvalds .open = neigh_stat_seq_open, 27261da177e4SLinus Torvalds .read = seq_read, 27271da177e4SLinus Torvalds .llseek = seq_lseek, 27281da177e4SLinus Torvalds .release = seq_release, 27291da177e4SLinus Torvalds }; 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27321da177e4SLinus Torvalds 2733339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2734339bf98fSThomas Graf { 2735339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2736339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2737339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2738339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2739339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2740339bf98fSThomas Graf } 2741339bf98fSThomas Graf 2742b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 27431da177e4SLinus Torvalds { 2744c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(n->dev); 27458b8aec50SThomas Graf struct sk_buff *skb; 2746b8673311SThomas Graf int err = -ENOBUFS; 27471da177e4SLinus Torvalds 2748339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 27498b8aec50SThomas Graf if (skb == NULL) 2750b8673311SThomas Graf goto errout; 27511da177e4SLinus Torvalds 2752b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 275326932566SPatrick McHardy if (err < 0) { 275426932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 275526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 275626932566SPatrick McHardy kfree_skb(skb); 275726932566SPatrick McHardy goto errout; 275826932566SPatrick McHardy } 27591ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 27601ce85fe4SPablo Neira Ayuso return; 2761b8673311SThomas Graf errout: 2762b8673311SThomas Graf if (err < 0) 2763426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2764b8673311SThomas Graf } 2765b8673311SThomas Graf 2766d961db35SThomas Graf #ifdef CONFIG_ARPD 2767b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2768b8673311SThomas Graf { 2769b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 27708b8aec50SThomas Graf } 27710a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns); 27721da177e4SLinus Torvalds #endif /* CONFIG_ARPD */ 27731da177e4SLinus Torvalds 27741da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27751da177e4SLinus Torvalds 27768b5c171bSEric Dumazet static int proc_unres_qlen(ctl_table *ctl, int write, void __user *buffer, 27778b5c171bSEric Dumazet size_t *lenp, loff_t *ppos) 27788b5c171bSEric Dumazet { 27798b5c171bSEric Dumazet int size, ret; 27808b5c171bSEric Dumazet ctl_table tmp = *ctl; 27818b5c171bSEric Dumazet 27828b5c171bSEric Dumazet tmp.data = &size; 27838b5c171bSEric Dumazet size = DIV_ROUND_UP(*(int *)ctl->data, SKB_TRUESIZE(ETH_FRAME_LEN)); 27848b5c171bSEric Dumazet ret = proc_dointvec(&tmp, write, buffer, lenp, ppos); 27858b5c171bSEric Dumazet if (write && !ret) 27868b5c171bSEric Dumazet *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN); 27878b5c171bSEric Dumazet return ret; 27888b5c171bSEric Dumazet } 27898b5c171bSEric Dumazet 27908b5c171bSEric Dumazet enum { 27918b5c171bSEric Dumazet NEIGH_VAR_MCAST_PROBE, 27928b5c171bSEric Dumazet NEIGH_VAR_UCAST_PROBE, 27938b5c171bSEric Dumazet NEIGH_VAR_APP_PROBE, 27948b5c171bSEric Dumazet NEIGH_VAR_RETRANS_TIME, 27958b5c171bSEric Dumazet NEIGH_VAR_BASE_REACHABLE_TIME, 27968b5c171bSEric Dumazet NEIGH_VAR_DELAY_PROBE_TIME, 27978b5c171bSEric Dumazet NEIGH_VAR_GC_STALETIME, 27988b5c171bSEric Dumazet NEIGH_VAR_QUEUE_LEN, 27998b5c171bSEric Dumazet NEIGH_VAR_QUEUE_LEN_BYTES, 28008b5c171bSEric Dumazet NEIGH_VAR_PROXY_QLEN, 28018b5c171bSEric Dumazet NEIGH_VAR_ANYCAST_DELAY, 28028b5c171bSEric Dumazet NEIGH_VAR_PROXY_DELAY, 28038b5c171bSEric Dumazet NEIGH_VAR_LOCKTIME, 28048b5c171bSEric Dumazet NEIGH_VAR_RETRANS_TIME_MS, 28058b5c171bSEric Dumazet NEIGH_VAR_BASE_REACHABLE_TIME_MS, 28068b5c171bSEric Dumazet NEIGH_VAR_GC_INTERVAL, 28078b5c171bSEric Dumazet NEIGH_VAR_GC_THRESH1, 28088b5c171bSEric Dumazet NEIGH_VAR_GC_THRESH2, 28098b5c171bSEric Dumazet NEIGH_VAR_GC_THRESH3, 28108b5c171bSEric Dumazet NEIGH_VAR_MAX 28118b5c171bSEric Dumazet }; 281254716e3bSEric W. Biederman 28131da177e4SLinus Torvalds static struct neigh_sysctl_table { 28141da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 28158b5c171bSEric Dumazet struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1]; 2816ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 28171da177e4SLinus Torvalds .neigh_vars = { 28188b5c171bSEric Dumazet [NEIGH_VAR_MCAST_PROBE] = { 28191da177e4SLinus Torvalds .procname = "mcast_solicit", 28201da177e4SLinus Torvalds .maxlen = sizeof(int), 28211da177e4SLinus Torvalds .mode = 0644, 28226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28231da177e4SLinus Torvalds }, 28248b5c171bSEric Dumazet [NEIGH_VAR_UCAST_PROBE] = { 28251da177e4SLinus Torvalds .procname = "ucast_solicit", 28261da177e4SLinus Torvalds .maxlen = sizeof(int), 28271da177e4SLinus Torvalds .mode = 0644, 28286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28291da177e4SLinus Torvalds }, 28308b5c171bSEric Dumazet [NEIGH_VAR_APP_PROBE] = { 28311da177e4SLinus Torvalds .procname = "app_solicit", 28321da177e4SLinus Torvalds .maxlen = sizeof(int), 28331da177e4SLinus Torvalds .mode = 0644, 28346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28351da177e4SLinus Torvalds }, 28368b5c171bSEric Dumazet [NEIGH_VAR_RETRANS_TIME] = { 28371da177e4SLinus Torvalds .procname = "retrans_time", 28381da177e4SLinus Torvalds .maxlen = sizeof(int), 28391da177e4SLinus Torvalds .mode = 0644, 28406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 28411da177e4SLinus Torvalds }, 28428b5c171bSEric Dumazet [NEIGH_VAR_BASE_REACHABLE_TIME] = { 28431da177e4SLinus Torvalds .procname = "base_reachable_time", 28441da177e4SLinus Torvalds .maxlen = sizeof(int), 28451da177e4SLinus Torvalds .mode = 0644, 28466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28471da177e4SLinus Torvalds }, 28488b5c171bSEric Dumazet [NEIGH_VAR_DELAY_PROBE_TIME] = { 28491da177e4SLinus Torvalds .procname = "delay_first_probe_time", 28501da177e4SLinus Torvalds .maxlen = sizeof(int), 28511da177e4SLinus Torvalds .mode = 0644, 28526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28531da177e4SLinus Torvalds }, 28548b5c171bSEric Dumazet [NEIGH_VAR_GC_STALETIME] = { 28551da177e4SLinus Torvalds .procname = "gc_stale_time", 28561da177e4SLinus Torvalds .maxlen = sizeof(int), 28571da177e4SLinus Torvalds .mode = 0644, 28586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28591da177e4SLinus Torvalds }, 28608b5c171bSEric Dumazet [NEIGH_VAR_QUEUE_LEN] = { 28611da177e4SLinus Torvalds .procname = "unres_qlen", 28621da177e4SLinus Torvalds .maxlen = sizeof(int), 28631da177e4SLinus Torvalds .mode = 0644, 28648b5c171bSEric Dumazet .proc_handler = proc_unres_qlen, 28658b5c171bSEric Dumazet }, 28668b5c171bSEric Dumazet [NEIGH_VAR_QUEUE_LEN_BYTES] = { 28678b5c171bSEric Dumazet .procname = "unres_qlen_bytes", 28688b5c171bSEric Dumazet .maxlen = sizeof(int), 28698b5c171bSEric Dumazet .mode = 0644, 28706d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28711da177e4SLinus Torvalds }, 28728b5c171bSEric Dumazet [NEIGH_VAR_PROXY_QLEN] = { 28731da177e4SLinus Torvalds .procname = "proxy_qlen", 28741da177e4SLinus Torvalds .maxlen = sizeof(int), 28751da177e4SLinus Torvalds .mode = 0644, 28766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28771da177e4SLinus Torvalds }, 28788b5c171bSEric Dumazet [NEIGH_VAR_ANYCAST_DELAY] = { 28791da177e4SLinus Torvalds .procname = "anycast_delay", 28801da177e4SLinus Torvalds .maxlen = sizeof(int), 28811da177e4SLinus Torvalds .mode = 0644, 28826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 28831da177e4SLinus Torvalds }, 28848b5c171bSEric Dumazet [NEIGH_VAR_PROXY_DELAY] = { 28851da177e4SLinus Torvalds .procname = "proxy_delay", 28861da177e4SLinus Torvalds .maxlen = sizeof(int), 28871da177e4SLinus Torvalds .mode = 0644, 28886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 28891da177e4SLinus Torvalds }, 28908b5c171bSEric Dumazet [NEIGH_VAR_LOCKTIME] = { 28911da177e4SLinus Torvalds .procname = "locktime", 28921da177e4SLinus Torvalds .maxlen = sizeof(int), 28931da177e4SLinus Torvalds .mode = 0644, 28946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 28951da177e4SLinus Torvalds }, 28968b5c171bSEric Dumazet [NEIGH_VAR_RETRANS_TIME_MS] = { 2897d12af679SEric W. Biederman .procname = "retrans_time_ms", 2898d12af679SEric W. Biederman .maxlen = sizeof(int), 2899d12af679SEric W. Biederman .mode = 0644, 29006d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2901d12af679SEric W. Biederman }, 29028b5c171bSEric Dumazet [NEIGH_VAR_BASE_REACHABLE_TIME_MS] = { 2903d12af679SEric W. Biederman .procname = "base_reachable_time_ms", 2904d12af679SEric W. Biederman .maxlen = sizeof(int), 2905d12af679SEric W. Biederman .mode = 0644, 29066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2907d12af679SEric W. Biederman }, 29088b5c171bSEric Dumazet [NEIGH_VAR_GC_INTERVAL] = { 29091da177e4SLinus Torvalds .procname = "gc_interval", 29101da177e4SLinus Torvalds .maxlen = sizeof(int), 29111da177e4SLinus Torvalds .mode = 0644, 29126d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 29131da177e4SLinus Torvalds }, 29148b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH1] = { 29151da177e4SLinus Torvalds .procname = "gc_thresh1", 29161da177e4SLinus Torvalds .maxlen = sizeof(int), 29171da177e4SLinus Torvalds .mode = 0644, 29186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 29191da177e4SLinus Torvalds }, 29208b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH2] = { 29211da177e4SLinus Torvalds .procname = "gc_thresh2", 29221da177e4SLinus Torvalds .maxlen = sizeof(int), 29231da177e4SLinus Torvalds .mode = 0644, 29246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 29251da177e4SLinus Torvalds }, 29268b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH3] = { 29271da177e4SLinus Torvalds .procname = "gc_thresh3", 29281da177e4SLinus Torvalds .maxlen = sizeof(int), 29291da177e4SLinus Torvalds .mode = 0644, 29306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 29311da177e4SLinus Torvalds }, 2932c3bac5a7SPavel Emelyanov {}, 29331da177e4SLinus Torvalds }, 29341da177e4SLinus Torvalds }; 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 293754716e3bSEric W. Biederman char *p_name, proc_handler *handler) 29381da177e4SLinus Torvalds { 29393c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 29401da177e4SLinus Torvalds const char *dev_name_source = NULL; 29418f40a1f9SEric W. Biederman char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ]; 29421da177e4SLinus Torvalds 29433c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 29441da177e4SLinus Torvalds if (!t) 29453c607bbbSPavel Emelyanov goto err; 29463c607bbbSPavel Emelyanov 29478b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_MCAST_PROBE].data = &p->mcast_probes; 29488b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_UCAST_PROBE].data = &p->ucast_probes; 29498b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_APP_PROBE].data = &p->app_probes; 29508b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].data = &p->retrans_time; 29518b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].data = &p->base_reachable_time; 29528b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_DELAY_PROBE_TIME].data = &p->delay_probe_time; 29538b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_STALETIME].data = &p->gc_staletime; 29548b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_QUEUE_LEN].data = &p->queue_len_bytes; 29558b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_QUEUE_LEN_BYTES].data = &p->queue_len_bytes; 29568b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_PROXY_QLEN].data = &p->proxy_qlen; 29578b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_ANYCAST_DELAY].data = &p->anycast_delay; 29588b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_PROXY_DELAY].data = &p->proxy_delay; 29598b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_LOCKTIME].data = &p->locktime; 29608b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].data = &p->retrans_time; 29618b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].data = &p->base_reachable_time; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds if (dev) { 29641da177e4SLinus Torvalds dev_name_source = dev->name; 2965d12af679SEric W. Biederman /* Terminate the table early */ 29668b5c171bSEric Dumazet memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0, 29678b5c171bSEric Dumazet sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL])); 29681da177e4SLinus Torvalds } else { 29698f40a1f9SEric W. Biederman dev_name_source = "default"; 29708b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = (int *)(p + 1); 29718b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = (int *)(p + 1) + 1; 29728b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = (int *)(p + 1) + 2; 29738b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = (int *)(p + 1) + 3; 29741da177e4SLinus Torvalds } 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds 2977f8572d8fSEric W. Biederman if (handler) { 29781da177e4SLinus Torvalds /* RetransTime */ 29798b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler; 29808b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].extra1 = dev; 29811da177e4SLinus Torvalds /* ReachableTime */ 29828b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler; 29838b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].extra1 = dev; 29841da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 29858b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler; 29868b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].extra1 = dev; 29871da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 29888b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler; 29898b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].extra1 = dev; 29901da177e4SLinus Torvalds } 29911da177e4SLinus Torvalds 29928f40a1f9SEric W. Biederman snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s", 29938f40a1f9SEric W. Biederman p_name, dev_name_source); 29944ab438fcSDenis V. Lunev t->sysctl_header = 29958f40a1f9SEric W. Biederman register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars); 29963c607bbbSPavel Emelyanov if (!t->sysctl_header) 29978f40a1f9SEric W. Biederman goto free; 29983c607bbbSPavel Emelyanov 29991da177e4SLinus Torvalds p->sysctl_table = t; 30001da177e4SLinus Torvalds return 0; 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds free: 30031da177e4SLinus Torvalds kfree(t); 30043c607bbbSPavel Emelyanov err: 30053c607bbbSPavel Emelyanov return -ENOBUFS; 30061da177e4SLinus Torvalds } 30070a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register); 30081da177e4SLinus Torvalds 30091da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 30101da177e4SLinus Torvalds { 30111da177e4SLinus Torvalds if (p->sysctl_table) { 30121da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 30131da177e4SLinus Torvalds p->sysctl_table = NULL; 30145dd3df10SEric W. Biederman unregister_net_sysctl_table(t->sysctl_header); 30151da177e4SLinus Torvalds kfree(t); 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds } 30180a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister); 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 30211da177e4SLinus Torvalds 3022c8822a4eSThomas Graf static int __init neigh_init(void) 3023c8822a4eSThomas Graf { 3024c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL); 3025c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL); 3026c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL); 3027c8822a4eSThomas Graf 3028c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info, 3029c7ac8679SGreg Rose NULL); 3030c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL); 3031c8822a4eSThomas Graf 3032c8822a4eSThomas Graf return 0; 3033c8822a4eSThomas Graf } 3034c8822a4eSThomas Graf 3035c8822a4eSThomas Graf subsys_initcall(neigh_init); 3036c8822a4eSThomas Graf 3037