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 42d5d427cdSJoe Perches #define DEBUG 431da177e4SLinus Torvalds #define NEIGH_DEBUG 1 44d5d427cdSJoe Perches #define neigh_dbg(level, fmt, ...) \ 45d5d427cdSJoe Perches do { \ 46d5d427cdSJoe Perches if (level <= NEIGH_DEBUG) \ 47d5d427cdSJoe Perches pr_debug(fmt, ##__VA_ARGS__); \ 48d5d427cdSJoe Perches } while (0) 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define PNEIGH_HASHMASK 0xF 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg); 53d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags); 54d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh); 551da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds static struct neigh_table *neigh_tables; 5845fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS 599a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops; 6045fc3b11SAmos Waterland #endif 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* 631da177e4SLinus Torvalds Neighbour hash table buckets are protected with rwlock tbl->lock. 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds - All the scans/updates to hash buckets MUST be made under this lock. 661da177e4SLinus Torvalds - NOTHING clever should be made under this lock: no callbacks 671da177e4SLinus Torvalds to protocol backends, no attempts to send something to network. 681da177e4SLinus Torvalds It will result in deadlocks, if backend/driver wants to use neighbour 691da177e4SLinus Torvalds cache. 701da177e4SLinus Torvalds - If the entry requires some non-trivial actions, increase 711da177e4SLinus Torvalds its reference count and release table lock. 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds Neighbour entries are protected: 741da177e4SLinus Torvalds - with reference count. 751da177e4SLinus Torvalds - with rwlock neigh->lock 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds Reference count prevents destruction. 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds neigh->lock mainly serializes ll address data and its validity state. 801da177e4SLinus Torvalds However, the same lock is used to protect another entry fields: 811da177e4SLinus Torvalds - timer 821da177e4SLinus Torvalds - resolution queue 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds Again, nothing clever shall be made under neigh->lock, 851da177e4SLinus Torvalds the most complicated procedure, which we allow is dev->hard_header. 861da177e4SLinus Torvalds It is supposed, that dev->hard_header is simplistic and does 871da177e4SLinus Torvalds not make callbacks to neighbour tables. 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds The last lock is neigh_tbl_lock. It is pure SMP lock, protecting 901da177e4SLinus Torvalds list of neighbour tables. This list is used only in process context, 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock); 941da177e4SLinus Torvalds 958f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb) 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds kfree_skb(skb); 981da177e4SLinus Torvalds return -ENETDOWN; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 1014f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh) 1024f494554SThomas Graf { 1034f494554SThomas Graf if (neigh->parms->neigh_cleanup) 1044f494554SThomas Graf neigh->parms->neigh_cleanup(neigh); 1054f494554SThomas Graf 106d961db35SThomas Graf __neigh_notify(neigh, RTM_DELNEIGH, 0); 1074f494554SThomas Graf neigh_release(neigh); 1084f494554SThomas Graf } 1094f494554SThomas Graf 1101da177e4SLinus Torvalds /* 1111da177e4SLinus Torvalds * It is random distribution in the interval (1/2)*base...(3/2)*base. 1121da177e4SLinus Torvalds * It corresponds to default IPv6 settings and is not overridable, 1131da177e4SLinus Torvalds * because it is really reasonable choice. 1141da177e4SLinus Torvalds */ 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base) 1171da177e4SLinus Torvalds { 118a02cec21SEric Dumazet return base ? (net_random() % base) + (base >> 1) : 0; 1191da177e4SLinus Torvalds } 1200a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds int shrunk = 0; 1261da177e4SLinus Torvalds int i; 127d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 132d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 133d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 134cd089336SDavid S. Miller for (i = 0; i < (1 << nht->hash_shift); i++) { 135767e97e1SEric Dumazet struct neighbour *n; 136767e97e1SEric Dumazet struct neighbour __rcu **np; 1371da177e4SLinus Torvalds 138d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 139767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 140767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 1411da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1421da177e4SLinus Torvalds * - nobody refers to it. 1431da177e4SLinus Torvalds * - it is not permanent 1441da177e4SLinus Torvalds */ 1451da177e4SLinus Torvalds write_lock(&n->lock); 1461da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1471da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 148767e97e1SEric Dumazet rcu_assign_pointer(*np, 149767e97e1SEric Dumazet rcu_dereference_protected(n->next, 150767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 1511da177e4SLinus Torvalds n->dead = 1; 1521da177e4SLinus Torvalds shrunk = 1; 1531da177e4SLinus Torvalds write_unlock(&n->lock); 1544f494554SThomas Graf neigh_cleanup_and_release(n); 1551da177e4SLinus Torvalds continue; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds write_unlock(&n->lock); 1581da177e4SLinus Torvalds np = &n->next; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds tbl->last_flush = jiffies; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds return shrunk; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 169a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 170a43d8994SPavel Emelyanov { 171a43d8994SPavel Emelyanov neigh_hold(n); 172a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 173a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 174a43d8994SPavel Emelyanov n->nud_state); 175a43d8994SPavel Emelyanov dump_stack(); 176a43d8994SPavel Emelyanov } 177a43d8994SPavel Emelyanov } 178a43d8994SPavel Emelyanov 1791da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1821da177e4SLinus Torvalds del_timer(&n->timer)) { 1831da177e4SLinus Torvalds neigh_release(n); 1841da177e4SLinus Torvalds return 1; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds return 0; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds struct sk_buff *skb; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 1941da177e4SLinus Torvalds dev_put(skb->dev); 1951da177e4SLinus Torvalds kfree_skb(skb); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 19949636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds int i; 202d6bf7817SEric Dumazet struct neigh_hash_table *nht; 2031da177e4SLinus Torvalds 204d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 205d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 206d6bf7817SEric Dumazet 207cd089336SDavid S. Miller for (i = 0; i < (1 << nht->hash_shift); i++) { 208767e97e1SEric Dumazet struct neighbour *n; 209767e97e1SEric Dumazet struct neighbour __rcu **np = &nht->hash_buckets[i]; 2101da177e4SLinus Torvalds 211767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 212767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 2131da177e4SLinus Torvalds if (dev && n->dev != dev) { 2141da177e4SLinus Torvalds np = &n->next; 2151da177e4SLinus Torvalds continue; 2161da177e4SLinus Torvalds } 217767e97e1SEric Dumazet rcu_assign_pointer(*np, 218767e97e1SEric Dumazet rcu_dereference_protected(n->next, 219767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 2201da177e4SLinus Torvalds write_lock(&n->lock); 2211da177e4SLinus Torvalds neigh_del_timer(n); 2221da177e4SLinus Torvalds n->dead = 1; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2251da177e4SLinus Torvalds /* The most unpleasant situation. 2261da177e4SLinus Torvalds We must destroy neighbour entry, 2271da177e4SLinus Torvalds but someone still uses it. 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds The destroy will be delayed until 2301da177e4SLinus Torvalds the last user releases us, but 2311da177e4SLinus Torvalds we must kill timers etc. and move 2321da177e4SLinus Torvalds it to safe state. 2331da177e4SLinus Torvalds */ 234c9ab4d85SEric Dumazet __skb_queue_purge(&n->arp_queue); 2358b5c171bSEric Dumazet n->arp_queue_len_bytes = 0; 2361da177e4SLinus Torvalds n->output = neigh_blackhole; 2371da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2381da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2391da177e4SLinus Torvalds else 2401da177e4SLinus Torvalds n->nud_state = NUD_NONE; 241d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is stray\n", n); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds write_unlock(&n->lock); 2444f494554SThomas Graf neigh_cleanup_and_release(n); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds } 24749636bb1SHerbert Xu } 2481da177e4SLinus Torvalds 24949636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 25049636bb1SHerbert Xu { 25149636bb1SHerbert Xu write_lock_bh(&tbl->lock); 25249636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 25349636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 25449636bb1SHerbert Xu } 2550a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr); 25649636bb1SHerbert Xu 25749636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 25849636bb1SHerbert Xu { 25949636bb1SHerbert Xu write_lock_bh(&tbl->lock); 26049636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2611da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2621da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2651da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2661da177e4SLinus Torvalds return 0; 2671da177e4SLinus Torvalds } 2680a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown); 2691da177e4SLinus Torvalds 270596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds struct neighbour *n = NULL; 2731da177e4SLinus Torvalds unsigned long now = jiffies; 2741da177e4SLinus Torvalds int entries; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2771da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2781da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2791da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2801da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2811da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2821da177e4SLinus Torvalds goto out_entries; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 28508433effSYOSHIFUJI Hideaki / 吉藤英明 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC); 2861da177e4SLinus Torvalds if (!n) 2871da177e4SLinus Torvalds goto out_entries; 2881da177e4SLinus Torvalds 289c9ab4d85SEric Dumazet __skb_queue_head_init(&n->arp_queue); 2901da177e4SLinus Torvalds rwlock_init(&n->lock); 2910ed8ddf4SEric Dumazet seqlock_init(&n->ha_lock); 2921da177e4SLinus Torvalds n->updated = n->used = now; 2931da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2941da177e4SLinus Torvalds n->output = neigh_blackhole; 295f6b72b62SDavid S. Miller seqlock_init(&n->hh.hh_lock); 2961da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 297b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 3001da177e4SLinus Torvalds n->tbl = tbl; 3011da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 3021da177e4SLinus Torvalds n->dead = 1; 3031da177e4SLinus Torvalds out: 3041da177e4SLinus Torvalds return n; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds out_entries: 3071da177e4SLinus Torvalds atomic_dec(&tbl->entries); 3081da177e4SLinus Torvalds goto out; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3112c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x) 3122c2aba6cSDavid S. Miller { 3132c2aba6cSDavid S. Miller get_random_bytes(x, sizeof(*x)); 3142c2aba6cSDavid S. Miller *x |= 1; 3152c2aba6cSDavid S. Miller } 3162c2aba6cSDavid S. Miller 317cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift) 3181da177e4SLinus Torvalds { 319cd089336SDavid S. Miller size_t size = (1 << shift) * sizeof(struct neighbour *); 320d6bf7817SEric Dumazet struct neigh_hash_table *ret; 3216193d2beSEric Dumazet struct neighbour __rcu **buckets; 3222c2aba6cSDavid S. Miller int i; 3231da177e4SLinus Torvalds 324d6bf7817SEric Dumazet ret = kmalloc(sizeof(*ret), GFP_ATOMIC); 325d6bf7817SEric Dumazet if (!ret) 326d6bf7817SEric Dumazet return NULL; 327d6bf7817SEric Dumazet if (size <= PAGE_SIZE) 328d6bf7817SEric Dumazet buckets = kzalloc(size, GFP_ATOMIC); 329d6bf7817SEric Dumazet else 3306193d2beSEric Dumazet buckets = (struct neighbour __rcu **) 331d6bf7817SEric Dumazet __get_free_pages(GFP_ATOMIC | __GFP_ZERO, 332d6bf7817SEric Dumazet get_order(size)); 333d6bf7817SEric Dumazet if (!buckets) { 334d6bf7817SEric Dumazet kfree(ret); 335d6bf7817SEric Dumazet return NULL; 3361da177e4SLinus Torvalds } 3376193d2beSEric Dumazet ret->hash_buckets = buckets; 338cd089336SDavid S. Miller ret->hash_shift = shift; 3392c2aba6cSDavid S. Miller for (i = 0; i < NEIGH_NUM_HASH_RND; i++) 3402c2aba6cSDavid S. Miller neigh_get_hash_rnd(&ret->hash_rnd[i]); 3411da177e4SLinus Torvalds return ret; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 344d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head) 3451da177e4SLinus Torvalds { 346d6bf7817SEric Dumazet struct neigh_hash_table *nht = container_of(head, 347d6bf7817SEric Dumazet struct neigh_hash_table, 348d6bf7817SEric Dumazet rcu); 349cd089336SDavid S. Miller size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *); 3506193d2beSEric Dumazet struct neighbour __rcu **buckets = nht->hash_buckets; 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds if (size <= PAGE_SIZE) 353d6bf7817SEric Dumazet kfree(buckets); 3541da177e4SLinus Torvalds else 355d6bf7817SEric Dumazet free_pages((unsigned long)buckets, get_order(size)); 356d6bf7817SEric Dumazet kfree(nht); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 359d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, 360cd089336SDavid S. Miller unsigned long new_shift) 3611da177e4SLinus Torvalds { 362d6bf7817SEric Dumazet unsigned int i, hash; 363d6bf7817SEric Dumazet struct neigh_hash_table *new_nht, *old_nht; 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3661da177e4SLinus Torvalds 367d6bf7817SEric Dumazet old_nht = rcu_dereference_protected(tbl->nht, 368d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 369cd089336SDavid S. Miller new_nht = neigh_hash_alloc(new_shift); 370d6bf7817SEric Dumazet if (!new_nht) 371d6bf7817SEric Dumazet return old_nht; 3721da177e4SLinus Torvalds 373cd089336SDavid S. Miller for (i = 0; i < (1 << old_nht->hash_shift); i++) { 3741da177e4SLinus Torvalds struct neighbour *n, *next; 3751da177e4SLinus Torvalds 376767e97e1SEric Dumazet for (n = rcu_dereference_protected(old_nht->hash_buckets[i], 377767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 378d6bf7817SEric Dumazet n != NULL; 379d6bf7817SEric Dumazet n = next) { 380d6bf7817SEric Dumazet hash = tbl->hash(n->primary_key, n->dev, 381d6bf7817SEric Dumazet new_nht->hash_rnd); 3821da177e4SLinus Torvalds 383cd089336SDavid S. Miller hash >>= (32 - new_nht->hash_shift); 384767e97e1SEric Dumazet next = rcu_dereference_protected(n->next, 385767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 3861da177e4SLinus Torvalds 387767e97e1SEric Dumazet rcu_assign_pointer(n->next, 388767e97e1SEric Dumazet rcu_dereference_protected( 389767e97e1SEric Dumazet new_nht->hash_buckets[hash], 390767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 391767e97e1SEric Dumazet rcu_assign_pointer(new_nht->hash_buckets[hash], n); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 395d6bf7817SEric Dumazet rcu_assign_pointer(tbl->nht, new_nht); 396d6bf7817SEric Dumazet call_rcu(&old_nht->rcu, neigh_hash_free_rcu); 397d6bf7817SEric Dumazet return new_nht; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 4011da177e4SLinus Torvalds struct net_device *dev) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds struct neighbour *n; 4041da177e4SLinus Torvalds int key_len = tbl->key_len; 405bc4bf5f3SPavel Emelyanov u32 hash_val; 406d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4091da177e4SLinus Torvalds 410d6bf7817SEric Dumazet rcu_read_lock_bh(); 411d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 412cd089336SDavid S. Miller hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 413767e97e1SEric Dumazet 414767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 415767e97e1SEric Dumazet n != NULL; 416767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 4171da177e4SLinus Torvalds if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) { 418767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 419767e97e1SEric Dumazet n = NULL; 4201da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4211da177e4SLinus Torvalds break; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds } 424767e97e1SEric Dumazet 425d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4261da177e4SLinus Torvalds return n; 4271da177e4SLinus Torvalds } 4280a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup); 4291da177e4SLinus Torvalds 430426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 431426b5303SEric W. Biederman const void *pkey) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds struct neighbour *n; 4341da177e4SLinus Torvalds int key_len = tbl->key_len; 435bc4bf5f3SPavel Emelyanov u32 hash_val; 436d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4391da177e4SLinus Torvalds 440d6bf7817SEric Dumazet rcu_read_lock_bh(); 441d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 442cd089336SDavid S. Miller hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift); 443767e97e1SEric Dumazet 444767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 445767e97e1SEric Dumazet n != NULL; 446767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 447426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 448878628fbSYOSHIFUJI Hideaki net_eq(dev_net(n->dev), net)) { 449767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 450767e97e1SEric Dumazet n = NULL; 4511da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4521da177e4SLinus Torvalds break; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds } 455767e97e1SEric Dumazet 456d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4571da177e4SLinus Torvalds return n; 4581da177e4SLinus Torvalds } 4590a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev); 4601da177e4SLinus Torvalds 461a263b309SDavid S. Miller struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey, 462a263b309SDavid S. Miller struct net_device *dev, bool want_ref) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds u32 hash_val; 4651da177e4SLinus Torvalds int key_len = tbl->key_len; 4661da177e4SLinus Torvalds int error; 467596b9b68SDavid Miller struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev); 468d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (!n) { 4711da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4721da177e4SLinus Torvalds goto out; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4761da177e4SLinus Torvalds n->dev = dev; 4771da177e4SLinus Torvalds dev_hold(dev); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* Protocol specific setup. */ 4801da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4811da177e4SLinus Torvalds rc = ERR_PTR(error); 4821da177e4SLinus Torvalds goto out_neigh_release; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 485da6a8fa0SDavid Miller if (dev->netdev_ops->ndo_neigh_construct) { 486da6a8fa0SDavid Miller error = dev->netdev_ops->ndo_neigh_construct(n); 487da6a8fa0SDavid Miller if (error < 0) { 488da6a8fa0SDavid Miller rc = ERR_PTR(error); 489da6a8fa0SDavid Miller goto out_neigh_release; 490da6a8fa0SDavid Miller } 491da6a8fa0SDavid Miller } 492da6a8fa0SDavid Miller 493447f2191SDavid S. Miller /* Device specific setup. */ 494447f2191SDavid S. Miller if (n->parms->neigh_setup && 495447f2191SDavid S. Miller (error = n->parms->neigh_setup(n)) < 0) { 496447f2191SDavid S. Miller rc = ERR_PTR(error); 497447f2191SDavid S. Miller goto out_neigh_release; 498447f2191SDavid S. Miller } 499447f2191SDavid S. Miller 5001da177e4SLinus Torvalds n->confirmed = jiffies - (n->parms->base_reachable_time << 1); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 503d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 504d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 5051da177e4SLinus Torvalds 506cd089336SDavid S. Miller if (atomic_read(&tbl->entries) > (1 << nht->hash_shift)) 507cd089336SDavid S. Miller nht = neigh_hash_grow(tbl, nht->hash_shift + 1); 5081da177e4SLinus Torvalds 509cd089336SDavid S. Miller hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (n->parms->dead) { 5121da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 5131da177e4SLinus Torvalds goto out_tbl_unlock; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 516767e97e1SEric Dumazet for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val], 517767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 518767e97e1SEric Dumazet n1 != NULL; 519767e97e1SEric Dumazet n1 = rcu_dereference_protected(n1->next, 520767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) { 5211da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 522a263b309SDavid S. Miller if (want_ref) 5231da177e4SLinus Torvalds neigh_hold(n1); 5241da177e4SLinus Torvalds rc = n1; 5251da177e4SLinus Torvalds goto out_tbl_unlock; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds n->dead = 0; 530a263b309SDavid S. Miller if (want_ref) 5311da177e4SLinus Torvalds neigh_hold(n); 532767e97e1SEric Dumazet rcu_assign_pointer(n->next, 533767e97e1SEric Dumazet rcu_dereference_protected(nht->hash_buckets[hash_val], 534767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 535767e97e1SEric Dumazet rcu_assign_pointer(nht->hash_buckets[hash_val], n); 5361da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 537d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is created\n", n); 5381da177e4SLinus Torvalds rc = n; 5391da177e4SLinus Torvalds out: 5401da177e4SLinus Torvalds return rc; 5411da177e4SLinus Torvalds out_tbl_unlock: 5421da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5431da177e4SLinus Torvalds out_neigh_release: 5441da177e4SLinus Torvalds neigh_release(n); 5451da177e4SLinus Torvalds goto out; 5461da177e4SLinus Torvalds } 547a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create); 5481da177e4SLinus Torvalds 549be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len) 550fa86d322SPavel Emelyanov { 551fa86d322SPavel Emelyanov u32 hash_val = *(u32 *)(pkey + key_len - 4); 552fa86d322SPavel Emelyanov hash_val ^= (hash_val >> 16); 553fa86d322SPavel Emelyanov hash_val ^= hash_val >> 8; 554fa86d322SPavel Emelyanov hash_val ^= hash_val >> 4; 555fa86d322SPavel Emelyanov hash_val &= PNEIGH_HASHMASK; 556be01d655SYOSHIFUJI Hideaki return hash_val; 557fa86d322SPavel Emelyanov } 558fa86d322SPavel Emelyanov 559be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, 560be01d655SYOSHIFUJI Hideaki struct net *net, 561be01d655SYOSHIFUJI Hideaki const void *pkey, 562be01d655SYOSHIFUJI Hideaki int key_len, 563be01d655SYOSHIFUJI Hideaki struct net_device *dev) 564be01d655SYOSHIFUJI Hideaki { 565be01d655SYOSHIFUJI Hideaki while (n) { 566be01d655SYOSHIFUJI Hideaki if (!memcmp(n->key, pkey, key_len) && 567be01d655SYOSHIFUJI Hideaki net_eq(pneigh_net(n), net) && 568be01d655SYOSHIFUJI Hideaki (n->dev == dev || !n->dev)) 569fa86d322SPavel Emelyanov return n; 570be01d655SYOSHIFUJI Hideaki n = n->next; 571be01d655SYOSHIFUJI Hideaki } 572be01d655SYOSHIFUJI Hideaki return NULL; 573be01d655SYOSHIFUJI Hideaki } 574be01d655SYOSHIFUJI Hideaki 575be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, 576be01d655SYOSHIFUJI Hideaki struct net *net, const void *pkey, struct net_device *dev) 577be01d655SYOSHIFUJI Hideaki { 578be01d655SYOSHIFUJI Hideaki int key_len = tbl->key_len; 579be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 580be01d655SYOSHIFUJI Hideaki 581be01d655SYOSHIFUJI Hideaki return __pneigh_lookup_1(tbl->phash_buckets[hash_val], 582be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 583fa86d322SPavel Emelyanov } 5840a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup); 585fa86d322SPavel Emelyanov 586426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 587426b5303SEric W. Biederman struct net *net, const void *pkey, 5881da177e4SLinus Torvalds struct net_device *dev, int creat) 5891da177e4SLinus Torvalds { 5901da177e4SLinus Torvalds struct pneigh_entry *n; 5911da177e4SLinus Torvalds int key_len = tbl->key_len; 592be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 595be01d655SYOSHIFUJI Hideaki n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], 596be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 597be01d655SYOSHIFUJI Hideaki read_unlock_bh(&tbl->lock); 5981da177e4SLinus Torvalds 599be01d655SYOSHIFUJI Hideaki if (n || !creat) 6001da177e4SLinus Torvalds goto out; 6011da177e4SLinus Torvalds 6024ae28944SPavel Emelyanov ASSERT_RTNL(); 6034ae28944SPavel Emelyanov 6041da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 6051da177e4SLinus Torvalds if (!n) 6061da177e4SLinus Torvalds goto out; 6071da177e4SLinus Torvalds 608e42ea986SEric Dumazet write_pnet(&n->net, hold_net(net)); 6091da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 6101da177e4SLinus Torvalds n->dev = dev; 6111da177e4SLinus Torvalds if (dev) 6121da177e4SLinus Torvalds dev_hold(dev); 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 6151da177e4SLinus Torvalds if (dev) 6161da177e4SLinus Torvalds dev_put(dev); 617da12f735SDenis V. Lunev release_net(net); 6181da177e4SLinus Torvalds kfree(n); 6191da177e4SLinus Torvalds n = NULL; 6201da177e4SLinus Torvalds goto out; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6241da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 6251da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 6261da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6271da177e4SLinus Torvalds out: 6281da177e4SLinus Torvalds return n; 6291da177e4SLinus Torvalds } 6300a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup); 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds 633426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 6341da177e4SLinus Torvalds struct net_device *dev) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6371da177e4SLinus Torvalds int key_len = tbl->key_len; 638be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6411da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 6421da177e4SLinus Torvalds np = &n->next) { 643426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 644878628fbSYOSHIFUJI Hideaki net_eq(pneigh_net(n), net)) { 6451da177e4SLinus Torvalds *np = n->next; 6461da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6471da177e4SLinus Torvalds if (tbl->pdestructor) 6481da177e4SLinus Torvalds tbl->pdestructor(n); 6491da177e4SLinus Torvalds if (n->dev) 6501da177e4SLinus Torvalds dev_put(n->dev); 65157da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6521da177e4SLinus Torvalds kfree(n); 6531da177e4SLinus Torvalds return 0; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6571da177e4SLinus Torvalds return -ENOENT; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6631da177e4SLinus Torvalds u32 h; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 6661da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 6671da177e4SLinus Torvalds while ((n = *np) != NULL) { 6681da177e4SLinus Torvalds if (!dev || n->dev == dev) { 6691da177e4SLinus Torvalds *np = n->next; 6701da177e4SLinus Torvalds if (tbl->pdestructor) 6711da177e4SLinus Torvalds tbl->pdestructor(n); 6721da177e4SLinus Torvalds if (n->dev) 6731da177e4SLinus Torvalds dev_put(n->dev); 67457da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6751da177e4SLinus Torvalds kfree(n); 6761da177e4SLinus Torvalds continue; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds np = &n->next; 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds return -ENOENT; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 68406f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 68506f0511dSDenis V. Lunev 68606f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 68706f0511dSDenis V. Lunev { 68806f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 68906f0511dSDenis V. Lunev neigh_parms_destroy(parms); 69006f0511dSDenis V. Lunev } 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* 6931da177e4SLinus Torvalds * neighbour must already be out of the table; 6941da177e4SLinus Torvalds * 6951da177e4SLinus Torvalds */ 6961da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 6971da177e4SLinus Torvalds { 698da6a8fa0SDavid Miller struct net_device *dev = neigh->dev; 699da6a8fa0SDavid Miller 7001da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds if (!neigh->dead) { 703e005d193SJoe Perches pr_warn("Destroying alive neighbour %p\n", neigh); 7041da177e4SLinus Torvalds dump_stack(); 7051da177e4SLinus Torvalds return; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 709e005d193SJoe Perches pr_warn("Impossible event\n"); 7101da177e4SLinus Torvalds 711c9ab4d85SEric Dumazet write_lock_bh(&neigh->lock); 712c9ab4d85SEric Dumazet __skb_queue_purge(&neigh->arp_queue); 713c9ab4d85SEric Dumazet write_unlock_bh(&neigh->lock); 7148b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 7151da177e4SLinus Torvalds 716447f2191SDavid S. Miller if (dev->netdev_ops->ndo_neigh_destroy) 717447f2191SDavid S. Miller dev->netdev_ops->ndo_neigh_destroy(neigh); 718447f2191SDavid S. Miller 719da6a8fa0SDavid Miller dev_put(dev); 7201da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 7211da177e4SLinus Torvalds 722d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is destroyed\n", neigh); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 7255b8b0060SDavid Miller kfree_rcu(neigh, rcu); 7261da177e4SLinus Torvalds } 7270a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* Neighbour state is suspicious; 7301da177e4SLinus Torvalds disable fast path. 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds Called with write_locked neigh. 7331da177e4SLinus Torvalds */ 7341da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 7351da177e4SLinus Torvalds { 736d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is suspected\n", neigh); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds neigh->output = neigh->ops->output; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /* Neighbour state is OK; 7421da177e4SLinus Torvalds enable fast path. 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds Called with write_locked neigh. 7451da177e4SLinus Torvalds */ 7461da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 7471da177e4SLinus Torvalds { 748d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is connected\n", neigh); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work) 7541da177e4SLinus Torvalds { 755e4c4e448SEric Dumazet struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); 756767e97e1SEric Dumazet struct neighbour *n; 757767e97e1SEric Dumazet struct neighbour __rcu **np; 758e4c4e448SEric Dumazet unsigned int i; 759d6bf7817SEric Dumazet struct neigh_hash_table *nht; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 7621da177e4SLinus Torvalds 763e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 764d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 765d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 7661da177e4SLinus Torvalds 7672724680bSYOSHIFUJI Hideaki / 吉藤英明 if (atomic_read(&tbl->entries) < tbl->gc_thresh1) 7682724680bSYOSHIFUJI Hideaki / 吉藤英明 goto out; 7692724680bSYOSHIFUJI Hideaki / 吉藤英明 7701da177e4SLinus Torvalds /* 7711da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 7721da177e4SLinus Torvalds */ 7731da177e4SLinus Torvalds 774e4c4e448SEric Dumazet if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { 7751da177e4SLinus Torvalds struct neigh_parms *p; 776e4c4e448SEric Dumazet tbl->last_rand = jiffies; 7771da177e4SLinus Torvalds for (p = &tbl->parms; p; p = p->next) 7781da177e4SLinus Torvalds p->reachable_time = 7791da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 782cd089336SDavid S. Miller for (i = 0 ; i < (1 << nht->hash_shift); i++) { 783d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 7841da177e4SLinus Torvalds 785767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 786767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 7871da177e4SLinus Torvalds unsigned int state; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds write_lock(&n->lock); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds state = n->nud_state; 7921da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 7931da177e4SLinus Torvalds write_unlock(&n->lock); 7941da177e4SLinus Torvalds goto next_elt; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 7981da177e4SLinus Torvalds n->used = n->confirmed; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 8011da177e4SLinus Torvalds (state == NUD_FAILED || 802e4c4e448SEric Dumazet time_after(jiffies, n->used + n->parms->gc_staletime))) { 8031da177e4SLinus Torvalds *np = n->next; 8041da177e4SLinus Torvalds n->dead = 1; 8051da177e4SLinus Torvalds write_unlock(&n->lock); 8064f494554SThomas Graf neigh_cleanup_and_release(n); 8071da177e4SLinus Torvalds continue; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds write_unlock(&n->lock); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds next_elt: 8121da177e4SLinus Torvalds np = &n->next; 8131da177e4SLinus Torvalds } 814e4c4e448SEric Dumazet /* 815e4c4e448SEric Dumazet * It's fine to release lock here, even if hash table 816e4c4e448SEric Dumazet * grows while we are preempted. 817e4c4e448SEric Dumazet */ 818e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 819e4c4e448SEric Dumazet cond_resched(); 820e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 82184338a6cSMichel Machado nht = rcu_dereference_protected(tbl->nht, 82284338a6cSMichel Machado lockdep_is_held(&tbl->lock)); 823e4c4e448SEric Dumazet } 8242724680bSYOSHIFUJI Hideaki / 吉藤英明 out: 8251da177e4SLinus Torvalds /* Cycle through all hash buckets every base_reachable_time/2 ticks. 8261da177e4SLinus Torvalds * ARP entry timeouts range from 1/2 base_reachable_time to 3/2 8271da177e4SLinus Torvalds * base_reachable_time. 8281da177e4SLinus Torvalds */ 829e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, 830e4c4e448SEric Dumazet tbl->parms.base_reachable_time >> 1); 831e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 8351da177e4SLinus Torvalds { 8361da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 837a02cec21SEric Dumazet return (n->nud_state & NUD_PROBE) ? 8381da177e4SLinus Torvalds p->ucast_probes : 839a02cec21SEric Dumazet p->ucast_probes + p->app_probes + p->mcast_probes; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8425ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh) 8430a141509SEric Dumazet __releases(neigh->lock) 8440a141509SEric Dumazet __acquires(neigh->lock) 8455ef12d98STimo Teras { 8465ef12d98STimo Teras struct sk_buff *skb; 8475ef12d98STimo Teras 8485ef12d98STimo Teras NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 849d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is failed\n", neigh); 8505ef12d98STimo Teras neigh->updated = jiffies; 8515ef12d98STimo Teras 8525ef12d98STimo Teras /* It is very thin place. report_unreachable is very complicated 8535ef12d98STimo Teras routine. Particularly, it can hit the same neighbour entry! 8545ef12d98STimo Teras 8555ef12d98STimo Teras So that, we try to be accurate and avoid dead loop. --ANK 8565ef12d98STimo Teras */ 8575ef12d98STimo Teras while (neigh->nud_state == NUD_FAILED && 8585ef12d98STimo Teras (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 8595ef12d98STimo Teras write_unlock(&neigh->lock); 8605ef12d98STimo Teras neigh->ops->error_report(neigh, skb); 8615ef12d98STimo Teras write_lock(&neigh->lock); 8625ef12d98STimo Teras } 863c9ab4d85SEric Dumazet __skb_queue_purge(&neigh->arp_queue); 8648b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 8655ef12d98STimo Teras } 8665ef12d98STimo Teras 867cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh) 868cd28ca0aSEric Dumazet __releases(neigh->lock) 869cd28ca0aSEric Dumazet { 8704ed377e3SHannes Frederic Sowa struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue); 871cd28ca0aSEric Dumazet /* keep skb alive even if arp_queue overflows */ 872cd28ca0aSEric Dumazet if (skb) 873cd28ca0aSEric Dumazet skb = skb_copy(skb, GFP_ATOMIC); 874cd28ca0aSEric Dumazet write_unlock(&neigh->lock); 875cd28ca0aSEric Dumazet neigh->ops->solicit(neigh, skb); 876cd28ca0aSEric Dumazet atomic_inc(&neigh->probes); 877cd28ca0aSEric Dumazet kfree_skb(skb); 878cd28ca0aSEric Dumazet } 879cd28ca0aSEric Dumazet 8801da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 8831da177e4SLinus Torvalds { 8841da177e4SLinus Torvalds unsigned long now, next; 8851da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 88695c96174SEric Dumazet unsigned int state; 8871da177e4SLinus Torvalds int notify = 0; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds write_lock(&neigh->lock); 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds state = neigh->nud_state; 8921da177e4SLinus Torvalds now = jiffies; 8931da177e4SLinus Torvalds next = now + HZ; 8941da177e4SLinus Torvalds 895045f7b3bSDavid S. Miller if (!(state & NUD_IN_TIMER)) 8961da177e4SLinus Torvalds goto out; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 8991da177e4SLinus Torvalds if (time_before_eq(now, 9001da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 901d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is still alive\n", neigh); 9021da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 9031da177e4SLinus Torvalds } else if (time_before_eq(now, 9041da177e4SLinus Torvalds neigh->used + neigh->parms->delay_probe_time)) { 905d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is delayed\n", neigh); 9061da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 907955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9081da177e4SLinus Torvalds neigh_suspect(neigh); 9091da177e4SLinus Torvalds next = now + neigh->parms->delay_probe_time; 9101da177e4SLinus Torvalds } else { 911d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is suspected\n", neigh); 9121da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 913955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9141da177e4SLinus Torvalds neigh_suspect(neigh); 9158d71740cSTom Tucker notify = 1; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 9181da177e4SLinus Torvalds if (time_before_eq(now, 9191da177e4SLinus Torvalds neigh->confirmed + neigh->parms->delay_probe_time)) { 920d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is now reachable\n", neigh); 9211da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 922955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9231da177e4SLinus Torvalds neigh_connect(neigh); 9248d71740cSTom Tucker notify = 1; 9251da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 9261da177e4SLinus Torvalds } else { 927d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is probed\n", neigh); 9281da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 929955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9301da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 9311da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds } else { 9341da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 9351da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 9391da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 9401da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 9411da177e4SLinus Torvalds notify = 1; 9425ef12d98STimo Teras neigh_invalidate(neigh); 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 9461da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 9471da177e4SLinus Torvalds next = jiffies + HZ/2; 9486fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 9496fb9974fSHerbert Xu neigh_hold(neigh); 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 952cd28ca0aSEric Dumazet neigh_probe(neigh); 9539ff56607SDavid S. Miller } else { 9541da177e4SLinus Torvalds out: 9551da177e4SLinus Torvalds write_unlock(&neigh->lock); 9569ff56607SDavid S. Miller } 9571da177e4SLinus Torvalds 958d961db35SThomas Graf if (notify) 959d961db35SThomas Graf neigh_update_notify(neigh); 960d961db35SThomas Graf 9611da177e4SLinus Torvalds neigh_release(neigh); 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 9651da177e4SLinus Torvalds { 9661da177e4SLinus Torvalds int rc; 967cd28ca0aSEric Dumazet bool immediate_probe = false; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds rc = 0; 9721da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 9731da177e4SLinus Torvalds goto out_unlock_bh; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 9761da177e4SLinus Torvalds if (neigh->parms->mcast_probes + neigh->parms->app_probes) { 977cd28ca0aSEric Dumazet unsigned long next, now = jiffies; 978cd28ca0aSEric Dumazet 9791da177e4SLinus Torvalds atomic_set(&neigh->probes, neigh->parms->ucast_probes); 9801da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 981cd28ca0aSEric Dumazet neigh->updated = now; 982cd28ca0aSEric Dumazet next = now + max(neigh->parms->retrans_time, HZ/2); 983cd28ca0aSEric Dumazet neigh_add_timer(neigh, next); 984cd28ca0aSEric Dumazet immediate_probe = true; 9851da177e4SLinus Torvalds } else { 9861da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 987955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9881da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds kfree_skb(skb); 9911da177e4SLinus Torvalds return 1; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 994d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is delayed\n", neigh); 9951da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 996955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 997667347f1SDavid S. Miller neigh_add_timer(neigh, 998667347f1SDavid S. Miller jiffies + neigh->parms->delay_probe_time); 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 10021da177e4SLinus Torvalds if (skb) { 10038b5c171bSEric Dumazet while (neigh->arp_queue_len_bytes + skb->truesize > 10048b5c171bSEric Dumazet neigh->parms->queue_len_bytes) { 10051da177e4SLinus Torvalds struct sk_buff *buff; 10068b5c171bSEric Dumazet 1007f72051b0SDavid S. Miller buff = __skb_dequeue(&neigh->arp_queue); 10088b5c171bSEric Dumazet if (!buff) 10098b5c171bSEric Dumazet break; 10108b5c171bSEric Dumazet neigh->arp_queue_len_bytes -= buff->truesize; 10111da177e4SLinus Torvalds kfree_skb(buff); 10129a6d276eSNeil Horman NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); 10131da177e4SLinus Torvalds } 1014a4731138SEric Dumazet skb_dst_force(skb); 10151da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 10168b5c171bSEric Dumazet neigh->arp_queue_len_bytes += skb->truesize; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds rc = 1; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds out_unlock_bh: 1021cd28ca0aSEric Dumazet if (immediate_probe) 1022cd28ca0aSEric Dumazet neigh_probe(neigh); 1023cd28ca0aSEric Dumazet else 1024cd28ca0aSEric Dumazet write_unlock(&neigh->lock); 1025cd28ca0aSEric Dumazet local_bh_enable(); 10261da177e4SLinus Torvalds return rc; 10271da177e4SLinus Torvalds } 10280a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send); 10291da177e4SLinus Torvalds 1030f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh) 10311da177e4SLinus Torvalds { 10321da177e4SLinus Torvalds struct hh_cache *hh; 10333b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 103491a72a70SDoug Kehn = NULL; 103591a72a70SDoug Kehn 103691a72a70SDoug Kehn if (neigh->dev->header_ops) 103791a72a70SDoug Kehn update = neigh->dev->header_ops->cache_update; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds if (update) { 1040f6b72b62SDavid S. Miller hh = &neigh->hh; 1041f6b72b62SDavid S. Miller if (hh->hh_len) { 10423644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 10431da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 10443644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* Generic update routine. 10521da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 10531da177e4SLinus Torvalds -- new is new state. 10541da177e4SLinus Torvalds -- flags 10551da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 10561da177e4SLinus Torvalds if it is different. 10571da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 10581da177e4SLinus Torvalds lladdr instead of overriding it 10591da177e4SLinus Torvalds if it is different. 10601da177e4SLinus Torvalds It also allows to retain current state 10611da177e4SLinus Torvalds if lladdr is unchanged. 10621da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 10651da177e4SLinus Torvalds NTF_ROUTER flag. 10661da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 10671da177e4SLinus Torvalds a router. 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 10701da177e4SLinus Torvalds */ 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 10731da177e4SLinus Torvalds u32 flags) 10741da177e4SLinus Torvalds { 10751da177e4SLinus Torvalds u8 old; 10761da177e4SLinus Torvalds int err; 10771da177e4SLinus Torvalds int notify = 0; 10781da177e4SLinus Torvalds struct net_device *dev; 10791da177e4SLinus Torvalds int update_isrouter = 0; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds dev = neigh->dev; 10841da177e4SLinus Torvalds old = neigh->nud_state; 10851da177e4SLinus Torvalds err = -EPERM; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 10881da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 10891da177e4SLinus Torvalds goto out; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 10921da177e4SLinus Torvalds neigh_del_timer(neigh); 10931da177e4SLinus Torvalds if (old & NUD_CONNECTED) 10941da177e4SLinus Torvalds neigh_suspect(neigh); 10951da177e4SLinus Torvalds neigh->nud_state = new; 10961da177e4SLinus Torvalds err = 0; 10971da177e4SLinus Torvalds notify = old & NUD_VALID; 10985ef12d98STimo Teras if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && 10995ef12d98STimo Teras (new & NUD_FAILED)) { 11005ef12d98STimo Teras neigh_invalidate(neigh); 11015ef12d98STimo Teras notify = 1; 11025ef12d98STimo Teras } 11031da177e4SLinus Torvalds goto out; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 11071da177e4SLinus Torvalds if (!dev->addr_len) { 11081da177e4SLinus Torvalds /* First case: device needs no address. */ 11091da177e4SLinus Torvalds lladdr = neigh->ha; 11101da177e4SLinus Torvalds } else if (lladdr) { 11111da177e4SLinus Torvalds /* The second case: if something is already cached 11121da177e4SLinus Torvalds and a new address is proposed: 11131da177e4SLinus Torvalds - compare new & old 11141da177e4SLinus Torvalds - if they are different, check override flag 11151da177e4SLinus Torvalds */ 11161da177e4SLinus Torvalds if ((old & NUD_VALID) && 11171da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 11181da177e4SLinus Torvalds lladdr = neigh->ha; 11191da177e4SLinus Torvalds } else { 11201da177e4SLinus Torvalds /* No address is supplied; if we know something, 11211da177e4SLinus Torvalds use it, otherwise discard the request. 11221da177e4SLinus Torvalds */ 11231da177e4SLinus Torvalds err = -EINVAL; 11241da177e4SLinus Torvalds if (!(old & NUD_VALID)) 11251da177e4SLinus Torvalds goto out; 11261da177e4SLinus Torvalds lladdr = neigh->ha; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11301da177e4SLinus Torvalds neigh->confirmed = jiffies; 11311da177e4SLinus Torvalds neigh->updated = jiffies; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds /* If entry was valid and address is not changed, 11341da177e4SLinus Torvalds do not change entry state, if new one is STALE. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds err = 0; 11371da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 11381da177e4SLinus Torvalds if (old & NUD_VALID) { 11391da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 11401da177e4SLinus Torvalds update_isrouter = 0; 11411da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 11421da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 11431da177e4SLinus Torvalds lladdr = neigh->ha; 11441da177e4SLinus Torvalds new = NUD_STALE; 11451da177e4SLinus Torvalds } else 11461da177e4SLinus Torvalds goto out; 11471da177e4SLinus Torvalds } else { 11481da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 11491da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 11501da177e4SLinus Torvalds (old & NUD_CONNECTED)) 11511da177e4SLinus Torvalds ) 11521da177e4SLinus Torvalds new = old; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds if (new != old) { 11571da177e4SLinus Torvalds neigh_del_timer(neigh); 1158a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1159667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 11601da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1161667347f1SDavid S. Miller neigh->parms->reachable_time : 1162667347f1SDavid S. Miller 0))); 11631da177e4SLinus Torvalds neigh->nud_state = new; 116453385d2dSBob Gilligan notify = 1; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds if (lladdr != neigh->ha) { 11680ed8ddf4SEric Dumazet write_seqlock(&neigh->ha_lock); 11691da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 11700ed8ddf4SEric Dumazet write_sequnlock(&neigh->ha_lock); 11711da177e4SLinus Torvalds neigh_update_hhs(neigh); 11721da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 11731da177e4SLinus Torvalds neigh->confirmed = jiffies - 11741da177e4SLinus Torvalds (neigh->parms->base_reachable_time << 1); 11751da177e4SLinus Torvalds notify = 1; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds if (new == old) 11781da177e4SLinus Torvalds goto out; 11791da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11801da177e4SLinus Torvalds neigh_connect(neigh); 11811da177e4SLinus Torvalds else 11821da177e4SLinus Torvalds neigh_suspect(neigh); 11831da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 11841da177e4SLinus Torvalds struct sk_buff *skb; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 11891da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 119069cce1d1SDavid S. Miller struct dst_entry *dst = skb_dst(skb); 119169cce1d1SDavid S. Miller struct neighbour *n2, *n1 = neigh; 11921da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 1193e049f288Sroy.qing.li@gmail.com 1194e049f288Sroy.qing.li@gmail.com rcu_read_lock(); 119513a43d94SDavid S. Miller 119613a43d94SDavid S. Miller /* Why not just use 'neigh' as-is? The problem is that 119713a43d94SDavid S. Miller * things such as shaper, eql, and sch_teql can end up 119813a43d94SDavid S. Miller * using alternative, different, neigh objects to output 119913a43d94SDavid S. Miller * the packet in the output path. So what we need to do 120013a43d94SDavid S. Miller * here is re-lookup the top-level neigh in the path so 120113a43d94SDavid S. Miller * we can reinject the packet there. 120213a43d94SDavid S. Miller */ 120313a43d94SDavid S. Miller n2 = NULL; 120413a43d94SDavid S. Miller if (dst) { 120513a43d94SDavid S. Miller n2 = dst_neigh_lookup_skb(dst, skb); 120613a43d94SDavid S. Miller if (n2) 120769cce1d1SDavid S. Miller n1 = n2; 120813a43d94SDavid S. Miller } 12098f40b161SDavid S. Miller n1->output(n1, skb); 121013a43d94SDavid S. Miller if (n2) 121113a43d94SDavid S. Miller neigh_release(n2); 1212e049f288Sroy.qing.li@gmail.com rcu_read_unlock(); 1213e049f288Sroy.qing.li@gmail.com 12141da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 12151da177e4SLinus Torvalds } 1216c9ab4d85SEric Dumazet __skb_queue_purge(&neigh->arp_queue); 12178b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds out: 12201da177e4SLinus Torvalds if (update_isrouter) { 12211da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 12221da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 12231da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 12268d71740cSTom Tucker 12278d71740cSTom Tucker if (notify) 1228d961db35SThomas Graf neigh_update_notify(neigh); 1229d961db35SThomas Graf 12301da177e4SLinus Torvalds return err; 12311da177e4SLinus Torvalds } 12320a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update); 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 12351da177e4SLinus Torvalds u8 *lladdr, void *saddr, 12361da177e4SLinus Torvalds struct net_device *dev) 12371da177e4SLinus Torvalds { 12381da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 12391da177e4SLinus Torvalds lladdr || !dev->addr_len); 12401da177e4SLinus Torvalds if (neigh) 12411da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 12421da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 12431da177e4SLinus Torvalds return neigh; 12441da177e4SLinus Torvalds } 12450a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns); 12461da177e4SLinus Torvalds 124734d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */ 1248f6b72b62SDavid S. Miller static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst) 12491da177e4SLinus Torvalds { 12501da177e4SLinus Torvalds struct net_device *dev = dst->dev; 1251f6b72b62SDavid S. Miller __be16 prot = dst->ops->protocol; 1252f6b72b62SDavid S. Miller struct hh_cache *hh = &n->hh; 12530ed8ddf4SEric Dumazet 12540ed8ddf4SEric Dumazet write_lock_bh(&n->lock); 125534d101ddSEric Dumazet 1256f6b72b62SDavid S. Miller /* Only one thread can come in here and initialize the 1257f6b72b62SDavid S. Miller * hh_cache entry. 1258f6b72b62SDavid S. Miller */ 1259b23b5455SDavid S. Miller if (!hh->hh_len) 1260b23b5455SDavid S. Miller dev->header_ops->cache(n, hh, prot); 1261f6b72b62SDavid S. Miller 12620ed8ddf4SEric Dumazet write_unlock_bh(&n->lock); 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit 1266767e97e1SEric Dumazet * worked, f.e. if you want to override normal output path (eql, shaper), 1267767e97e1SEric Dumazet * but resolution is not made yet. 12681da177e4SLinus Torvalds */ 12691da177e4SLinus Torvalds 12708f40b161SDavid S. Miller int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb) 12711da177e4SLinus Torvalds { 12721da177e4SLinus Torvalds struct net_device *dev = skb->dev; 12731da177e4SLinus Torvalds 1274bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12751da177e4SLinus Torvalds 12760c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 12771da177e4SLinus Torvalds skb->len) < 0 && 12783b04dddeSStephen Hemminger dev->header_ops->rebuild(skb)) 12791da177e4SLinus Torvalds return 0; 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds return dev_queue_xmit(skb); 12821da177e4SLinus Torvalds } 12830a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output); 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* Slow and careful. */ 12861da177e4SLinus Torvalds 12878f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb) 12881da177e4SLinus Torvalds { 1289adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 12901da177e4SLinus Torvalds int rc = 0; 12911da177e4SLinus Torvalds 12928f40b161SDavid S. Miller if (!dst) 12931da177e4SLinus Torvalds goto discard; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 12961da177e4SLinus Torvalds int err; 12971da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 12980ed8ddf4SEric Dumazet unsigned int seq; 129934d101ddSEric Dumazet 1300f6b72b62SDavid S. Miller if (dev->header_ops->cache && !neigh->hh.hh_len) 1301f6b72b62SDavid S. Miller neigh_hh_init(neigh, dst); 130234d101ddSEric Dumazet 13030ed8ddf4SEric Dumazet do { 1304e1f16503Sramesh.nagappa@gmail.com __skb_pull(skb, skb_network_offset(skb)); 13050ed8ddf4SEric Dumazet seq = read_seqbegin(&neigh->ha_lock); 13060c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13071da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13080ed8ddf4SEric Dumazet } while (read_seqretry(&neigh->ha_lock, seq)); 130934d101ddSEric Dumazet 13101da177e4SLinus Torvalds if (err >= 0) 1311542d4d68SDavid S. Miller rc = dev_queue_xmit(skb); 13121da177e4SLinus Torvalds else 13131da177e4SLinus Torvalds goto out_kfree_skb; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds out: 13161da177e4SLinus Torvalds return rc; 13171da177e4SLinus Torvalds discard: 1318d5d427cdSJoe Perches neigh_dbg(1, "%s: dst=%p neigh=%p\n", __func__, dst, neigh); 13191da177e4SLinus Torvalds out_kfree_skb: 13201da177e4SLinus Torvalds rc = -EINVAL; 13211da177e4SLinus Torvalds kfree_skb(skb); 13221da177e4SLinus Torvalds goto out; 13231da177e4SLinus Torvalds } 13240a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output); 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* As fast as possible without hh cache */ 13271da177e4SLinus Torvalds 13288f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb) 13291da177e4SLinus Torvalds { 13301da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 13310ed8ddf4SEric Dumazet unsigned int seq; 13328f40b161SDavid S. Miller int err; 13331da177e4SLinus Torvalds 13340ed8ddf4SEric Dumazet do { 1335e1f16503Sramesh.nagappa@gmail.com __skb_pull(skb, skb_network_offset(skb)); 13360ed8ddf4SEric Dumazet seq = read_seqbegin(&neigh->ha_lock); 13370c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13381da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13390ed8ddf4SEric Dumazet } while (read_seqretry(&neigh->ha_lock, seq)); 13400ed8ddf4SEric Dumazet 13411da177e4SLinus Torvalds if (err >= 0) 1342542d4d68SDavid S. Miller err = dev_queue_xmit(skb); 13431da177e4SLinus Torvalds else { 13441da177e4SLinus Torvalds err = -EINVAL; 13451da177e4SLinus Torvalds kfree_skb(skb); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds return err; 13481da177e4SLinus Torvalds } 13490a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output); 13501da177e4SLinus Torvalds 13518f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb) 13528f40b161SDavid S. Miller { 13538f40b161SDavid S. Miller return dev_queue_xmit(skb); 13548f40b161SDavid S. Miller } 13558f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output); 13568f40b161SDavid S. Miller 13571da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 13581da177e4SLinus Torvalds { 13591da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 13601da177e4SLinus Torvalds long sched_next = 0; 13611da177e4SLinus Torvalds unsigned long now = jiffies; 1362f72051b0SDavid S. Miller struct sk_buff *skb, *n; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13651da177e4SLinus Torvalds 1366f72051b0SDavid S. Miller skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { 1367f72051b0SDavid S. Miller long tdif = NEIGH_CB(skb)->sched_next - now; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds if (tdif <= 0) { 1370f72051b0SDavid S. Miller struct net_device *dev = skb->dev; 137120e6074eSEric Dumazet 1372f72051b0SDavid S. Miller __skb_unlink(skb, &tbl->proxy_queue); 137320e6074eSEric Dumazet if (tbl->proxy_redo && netif_running(dev)) { 137420e6074eSEric Dumazet rcu_read_lock(); 1375f72051b0SDavid S. Miller tbl->proxy_redo(skb); 137620e6074eSEric Dumazet rcu_read_unlock(); 137720e6074eSEric Dumazet } else { 1378f72051b0SDavid S. Miller kfree_skb(skb); 137920e6074eSEric Dumazet } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds dev_put(dev); 13821da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 13831da177e4SLinus Torvalds sched_next = tdif; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 13861da177e4SLinus Torvalds if (sched_next) 13871da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 13881da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 13921da177e4SLinus Torvalds struct sk_buff *skb) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds unsigned long now = jiffies; 13951da177e4SLinus Torvalds unsigned long sched_next = now + (net_random() % p->proxy_delay); 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds if (tbl->proxy_queue.qlen > p->proxy_qlen) { 13981da177e4SLinus Torvalds kfree_skb(skb); 13991da177e4SLinus Torvalds return; 14001da177e4SLinus Torvalds } 1401a61bbcf2SPatrick McHardy 1402a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1403a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 14061da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 14071da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 14081da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 14091da177e4SLinus Torvalds } 1410adf30907SEric Dumazet skb_dst_drop(skb); 14111da177e4SLinus Torvalds dev_hold(skb->dev); 14121da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 14131da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 14141da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 14151da177e4SLinus Torvalds } 14160a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue); 14171da177e4SLinus Torvalds 141897fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, 1419426b5303SEric W. Biederman struct net *net, int ifindex) 1420426b5303SEric W. Biederman { 1421426b5303SEric W. Biederman struct neigh_parms *p; 1422426b5303SEric W. Biederman 1423426b5303SEric W. Biederman for (p = &tbl->parms; p; p = p->next) { 1424878628fbSYOSHIFUJI Hideaki if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || 1425170d6f99SGao feng (!p->dev && !ifindex && net_eq(net, &init_net))) 1426426b5303SEric W. Biederman return p; 1427426b5303SEric W. Biederman } 1428426b5303SEric W. Biederman 1429426b5303SEric W. Biederman return NULL; 1430426b5303SEric W. Biederman } 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 14331da177e4SLinus Torvalds struct neigh_table *tbl) 14341da177e4SLinus Torvalds { 1435cf89d6b2SGao feng struct neigh_parms *p; 143600829823SStephen Hemminger struct net *net = dev_net(dev); 143700829823SStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 14381da177e4SLinus Torvalds 1439cf89d6b2SGao feng p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL); 14401da177e4SLinus Torvalds if (p) { 14411da177e4SLinus Torvalds p->tbl = tbl; 14421da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 14431da177e4SLinus Torvalds p->reachable_time = 14441da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 1445c7fb64dbSThomas Graf dev_hold(dev); 1446c7fb64dbSThomas Graf p->dev = dev; 1447e42ea986SEric Dumazet write_pnet(&p->net, hold_net(net)); 14481da177e4SLinus Torvalds p->sysctl_table = NULL; 144963134803SVeaceslav Falico 145063134803SVeaceslav Falico if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 145163134803SVeaceslav Falico release_net(net); 145263134803SVeaceslav Falico dev_put(dev); 145363134803SVeaceslav Falico kfree(p); 145463134803SVeaceslav Falico return NULL; 145563134803SVeaceslav Falico } 145663134803SVeaceslav Falico 14571da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14581da177e4SLinus Torvalds p->next = tbl->parms.next; 14591da177e4SLinus Torvalds tbl->parms.next = p; 14601da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds return p; 14631da177e4SLinus Torvalds } 14640a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 14671da177e4SLinus Torvalds { 14681da177e4SLinus Torvalds struct neigh_parms *parms = 14691da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds neigh_parms_put(parms); 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 14751da177e4SLinus Torvalds { 14761da177e4SLinus Torvalds struct neigh_parms **p; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 14791da177e4SLinus Torvalds return; 14801da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14811da177e4SLinus Torvalds for (p = &tbl->parms.next; *p; p = &(*p)->next) { 14821da177e4SLinus Torvalds if (*p == parms) { 14831da177e4SLinus Torvalds *p = parms->next; 14841da177e4SLinus Torvalds parms->dead = 1; 14851da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1486cecbb639SDavid S. Miller if (parms->dev) 1487cecbb639SDavid S. Miller dev_put(parms->dev); 14881da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 14891da177e4SLinus Torvalds return; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1493d5d427cdSJoe Perches neigh_dbg(1, "%s: not found\n", __func__); 14941da177e4SLinus Torvalds } 14950a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release); 14961da177e4SLinus Torvalds 149706f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 14981da177e4SLinus Torvalds { 149957da52c1SYOSHIFUJI Hideaki release_net(neigh_parms_net(parms)); 15001da177e4SLinus Torvalds kfree(parms); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 1503c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1504c2ecba71SPavel Emelianov 1505dcd2ba92SHiroaki SHIMODA static void neigh_table_init_no_netlink(struct neigh_table *tbl) 15061da177e4SLinus Torvalds { 15071da177e4SLinus Torvalds unsigned long now = jiffies; 15081da177e4SLinus Torvalds unsigned long phsize; 15091da177e4SLinus Torvalds 1510e42ea986SEric Dumazet write_pnet(&tbl->parms.net, &init_net); 15111da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 15121da177e4SLinus Torvalds tbl->parms.reachable_time = 15131da177e4SLinus Torvalds neigh_rand_reach_time(tbl->parms.base_reachable_time); 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 15161da177e4SLinus Torvalds if (!tbl->stats) 15171da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15209b739ba5SAlexey Dobriyan if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat, 15219b739ba5SAlexey Dobriyan &neigh_stat_seq_fops, tbl)) 15221da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 15231da177e4SLinus Torvalds #endif 15241da177e4SLinus Torvalds 1525cd089336SDavid S. Miller RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3)); 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 152877d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 15291da177e4SLinus Torvalds 1530d6bf7817SEric Dumazet if (!tbl->nht || !tbl->phash_buckets) 15311da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 15321da177e4SLinus Torvalds 153308433effSYOSHIFUJI Hideaki / 吉藤英明 if (!tbl->entry_size) 153408433effSYOSHIFUJI Hideaki / 吉藤英明 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) + 153508433effSYOSHIFUJI Hideaki / 吉藤英明 tbl->key_len, NEIGH_PRIV_ALIGN); 153608433effSYOSHIFUJI Hideaki / 吉藤英明 else 153708433effSYOSHIFUJI Hideaki / 吉藤英明 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN); 153808433effSYOSHIFUJI Hideaki / 吉藤英明 15391da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1540203b42f7STejun Heo INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work); 1541e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time); 1542b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1543c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1544c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds tbl->last_flush = now; 15471da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1548bd89efc5SSimon Kelley } 1549bd89efc5SSimon Kelley 1550bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl) 1551bd89efc5SSimon Kelley { 1552bd89efc5SSimon Kelley struct neigh_table *tmp; 1553bd89efc5SSimon Kelley 1554bd89efc5SSimon Kelley neigh_table_init_no_netlink(tbl); 15551da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 1556bd89efc5SSimon Kelley for (tmp = neigh_tables; tmp; tmp = tmp->next) { 1557bd89efc5SSimon Kelley if (tmp->family == tbl->family) 1558bd89efc5SSimon Kelley break; 1559bd89efc5SSimon Kelley } 15601da177e4SLinus Torvalds tbl->next = neigh_tables; 15611da177e4SLinus Torvalds neigh_tables = tbl; 15621da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 1563bd89efc5SSimon Kelley 1564bd89efc5SSimon Kelley if (unlikely(tmp)) { 1565e005d193SJoe Perches pr_err("Registering multiple tables for family %d\n", 1566e005d193SJoe Perches tbl->family); 1567bd89efc5SSimon Kelley dump_stack(); 1568bd89efc5SSimon Kelley } 15691da177e4SLinus Torvalds } 15700a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init); 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds struct neigh_table **tp; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 1577a5c30b34STejun Heo cancel_delayed_work_sync(&tbl->gc_work); 15781da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 15791da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 15801da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 15811da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 1582e005d193SJoe Perches pr_crit("neighbour leakage\n"); 15831da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 15841da177e4SLinus Torvalds for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { 15851da177e4SLinus Torvalds if (*tp == tbl) { 15861da177e4SLinus Torvalds *tp = tbl->next; 15871da177e4SLinus Torvalds break; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 15911da177e4SLinus Torvalds 15926193d2beSEric Dumazet call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu, 15936193d2beSEric Dumazet neigh_hash_free_rcu); 1594d6bf7817SEric Dumazet tbl->nht = NULL; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds kfree(tbl->phash_buckets); 15971da177e4SLinus Torvalds tbl->phash_buckets = NULL; 15981da177e4SLinus Torvalds 15993f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 16003f192b5cSAlexey Dobriyan 16013fcde74bSKirill Korotaev free_percpu(tbl->stats); 16023fcde74bSKirill Korotaev tbl->stats = NULL; 16033fcde74bSKirill Korotaev 16041da177e4SLinus Torvalds return 0; 16051da177e4SLinus Torvalds } 16060a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear); 16071da177e4SLinus Torvalds 1608661d2967SThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh) 16091da177e4SLinus Torvalds { 16103b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1611a14a49d2SThomas Graf struct ndmsg *ndm; 1612a14a49d2SThomas Graf struct nlattr *dst_attr; 16131da177e4SLinus Torvalds struct neigh_table *tbl; 16141da177e4SLinus Torvalds struct net_device *dev = NULL; 1615a14a49d2SThomas Graf int err = -EINVAL; 16161da177e4SLinus Torvalds 1617110b2499SEric Dumazet ASSERT_RTNL(); 1618a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 16191da177e4SLinus Torvalds goto out; 16201da177e4SLinus Torvalds 1621a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1622a14a49d2SThomas Graf if (dst_attr == NULL) 1623a14a49d2SThomas Graf goto out; 1624a14a49d2SThomas Graf 1625a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1626a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1627110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 1628a14a49d2SThomas Graf if (dev == NULL) { 1629a14a49d2SThomas Graf err = -ENODEV; 1630a14a49d2SThomas Graf goto out; 1631a14a49d2SThomas Graf } 1632a14a49d2SThomas Graf } 1633a14a49d2SThomas Graf 16341da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 16351da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1636a14a49d2SThomas Graf struct neighbour *neigh; 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 16391da177e4SLinus Torvalds continue; 16401da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16411da177e4SLinus Torvalds 1642a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 1643110b2499SEric Dumazet goto out; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1646426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 1647110b2499SEric Dumazet goto out; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 1650a14a49d2SThomas Graf if (dev == NULL) 1651110b2499SEric Dumazet goto out; 16521da177e4SLinus Torvalds 1653a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1654a14a49d2SThomas Graf if (neigh == NULL) { 1655a14a49d2SThomas Graf err = -ENOENT; 1656110b2499SEric Dumazet goto out; 1657a14a49d2SThomas Graf } 1658a14a49d2SThomas Graf 1659a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 16601da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 16611da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1662a14a49d2SThomas Graf neigh_release(neigh); 1663110b2499SEric Dumazet goto out; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 1666a14a49d2SThomas Graf err = -EAFNOSUPPORT; 1667a14a49d2SThomas Graf 16681da177e4SLinus Torvalds out: 16691da177e4SLinus Torvalds return err; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 1672661d2967SThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh) 16731da177e4SLinus Torvalds { 16743b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 16755208debdSThomas Graf struct ndmsg *ndm; 16765208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 16771da177e4SLinus Torvalds struct neigh_table *tbl; 16781da177e4SLinus Torvalds struct net_device *dev = NULL; 16795208debdSThomas Graf int err; 16801da177e4SLinus Torvalds 1681110b2499SEric Dumazet ASSERT_RTNL(); 16825208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 16835208debdSThomas Graf if (err < 0) 16841da177e4SLinus Torvalds goto out; 16851da177e4SLinus Torvalds 16865208debdSThomas Graf err = -EINVAL; 16875208debdSThomas Graf if (tb[NDA_DST] == NULL) 16885208debdSThomas Graf goto out; 16895208debdSThomas Graf 16905208debdSThomas Graf ndm = nlmsg_data(nlh); 16915208debdSThomas Graf if (ndm->ndm_ifindex) { 1692110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 16935208debdSThomas Graf if (dev == NULL) { 16945208debdSThomas Graf err = -ENODEV; 16955208debdSThomas Graf goto out; 16965208debdSThomas Graf } 16975208debdSThomas Graf 16985208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 1699110b2499SEric Dumazet goto out; 17005208debdSThomas Graf } 17015208debdSThomas Graf 17021da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 17031da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 17045208debdSThomas Graf int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 17055208debdSThomas Graf struct neighbour *neigh; 17065208debdSThomas Graf void *dst, *lladdr; 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 17091da177e4SLinus Torvalds continue; 17101da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17111da177e4SLinus Torvalds 17125208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 1713110b2499SEric Dumazet goto out; 17145208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 17155208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 171862dd9318SVille Nuorvala struct pneigh_entry *pn; 171962dd9318SVille Nuorvala 17205208debdSThomas Graf err = -ENOBUFS; 1721426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 172262dd9318SVille Nuorvala if (pn) { 172362dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 172462dd9318SVille Nuorvala err = 0; 172562dd9318SVille Nuorvala } 1726110b2499SEric Dumazet goto out; 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17295208debdSThomas Graf if (dev == NULL) 1730110b2499SEric Dumazet goto out; 17311da177e4SLinus Torvalds 17325208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 17335208debdSThomas Graf if (neigh == NULL) { 17345208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 17351da177e4SLinus Torvalds err = -ENOENT; 1736110b2499SEric Dumazet goto out; 17375208debdSThomas Graf } 17385208debdSThomas Graf 17395208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 17405208debdSThomas Graf if (IS_ERR(neigh)) { 17415208debdSThomas Graf err = PTR_ERR(neigh); 1742110b2499SEric Dumazet goto out; 17431da177e4SLinus Torvalds } 17445208debdSThomas Graf } else { 17455208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 17465208debdSThomas Graf err = -EEXIST; 17475208debdSThomas Graf neigh_release(neigh); 1748110b2499SEric Dumazet goto out; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds 17515208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 17525208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 17535208debdSThomas Graf } 17541da177e4SLinus Torvalds 17550c5c2d30SEric Biederman if (ndm->ndm_flags & NTF_USE) { 17560c5c2d30SEric Biederman neigh_event_send(neigh, NULL); 17570c5c2d30SEric Biederman err = 0; 17580c5c2d30SEric Biederman } else 17595208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 17605208debdSThomas Graf neigh_release(neigh); 1761110b2499SEric Dumazet goto out; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17655208debdSThomas Graf err = -EAFNOSUPPORT; 17661da177e4SLinus Torvalds out: 17671da177e4SLinus Torvalds return err; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 1770c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1771c7fb64dbSThomas Graf { 1772ca860fb3SThomas Graf struct nlattr *nest; 1773e386c6ebSThomas Graf 1774ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1775ca860fb3SThomas Graf if (nest == NULL) 1776ca860fb3SThomas Graf return -ENOBUFS; 1777c7fb64dbSThomas Graf 17789a6308d7SDavid S. Miller if ((parms->dev && 17799a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) || 17809a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) || 17819a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_QUEUE_LENBYTES, parms->queue_len_bytes) || 17828b5c171bSEric Dumazet /* approximative value for deprecated QUEUE_LEN (in packets) */ 17839a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_QUEUE_LEN, 1784ce46cc64SShan Wei parms->queue_len_bytes / SKB_TRUESIZE(ETH_FRAME_LEN)) || 17859a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen) || 17869a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_APP_PROBES, parms->app_probes) || 17879a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes) || 17889a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes) || 17899a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) || 17909a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME, 17919a6308d7SDavid S. Miller parms->base_reachable_time) || 17929a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_GC_STALETIME, parms->gc_staletime) || 17939a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME, 17949a6308d7SDavid S. Miller parms->delay_probe_time) || 17959a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_RETRANS_TIME, parms->retrans_time) || 17969a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay) || 17979a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_PROXY_DELAY, parms->proxy_delay) || 17989a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_LOCKTIME, parms->locktime)) 17999a6308d7SDavid S. Miller goto nla_put_failure; 1800ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1801c7fb64dbSThomas Graf 1802ca860fb3SThomas Graf nla_put_failure: 1803bc3ed28cSThomas Graf nla_nest_cancel(skb, nest); 1804bc3ed28cSThomas Graf return -EMSGSIZE; 1805c7fb64dbSThomas Graf } 1806c7fb64dbSThomas Graf 1807ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1808ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1809c7fb64dbSThomas Graf { 1810c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1811c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1812c7fb64dbSThomas Graf 1813ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1814ca860fb3SThomas Graf if (nlh == NULL) 181526932566SPatrick McHardy return -EMSGSIZE; 1816c7fb64dbSThomas Graf 1817ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1818c7fb64dbSThomas Graf 1819c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1820c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 18219ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 18229ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1823c7fb64dbSThomas Graf 18249a6308d7SDavid S. Miller if (nla_put_string(skb, NDTA_NAME, tbl->id) || 18259a6308d7SDavid S. Miller nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval) || 18269a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) || 18279a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) || 18289a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3)) 18299a6308d7SDavid S. Miller goto nla_put_failure; 1830c7fb64dbSThomas Graf { 1831c7fb64dbSThomas Graf unsigned long now = jiffies; 1832c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1833c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1834d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1835c7fb64dbSThomas Graf struct ndt_config ndc = { 1836c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1837c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1838c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1839c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1840c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1841c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1842c7fb64dbSThomas Graf }; 1843c7fb64dbSThomas Graf 1844d6bf7817SEric Dumazet rcu_read_lock_bh(); 1845d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 18462c2aba6cSDavid S. Miller ndc.ndtc_hash_rnd = nht->hash_rnd[0]; 1847cd089336SDavid S. Miller ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1); 1848d6bf7817SEric Dumazet rcu_read_unlock_bh(); 1849d6bf7817SEric Dumazet 18509a6308d7SDavid S. Miller if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc)) 18519a6308d7SDavid S. Miller goto nla_put_failure; 1852c7fb64dbSThomas Graf } 1853c7fb64dbSThomas Graf 1854c7fb64dbSThomas Graf { 1855c7fb64dbSThomas Graf int cpu; 1856c7fb64dbSThomas Graf struct ndt_stats ndst; 1857c7fb64dbSThomas Graf 1858c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1859c7fb64dbSThomas Graf 18606f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1861c7fb64dbSThomas Graf struct neigh_statistics *st; 1862c7fb64dbSThomas Graf 1863c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1864c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1865c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1866c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1867c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1868c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1869c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1870c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1871c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1872c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1873c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1874c7fb64dbSThomas Graf } 1875c7fb64dbSThomas Graf 18769a6308d7SDavid S. Miller if (nla_put(skb, NDTA_STATS, sizeof(ndst), &ndst)) 18779a6308d7SDavid S. Miller goto nla_put_failure; 1878c7fb64dbSThomas Graf } 1879c7fb64dbSThomas Graf 1880c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1881c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1882ca860fb3SThomas Graf goto nla_put_failure; 1883c7fb64dbSThomas Graf 1884c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1885ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1886c7fb64dbSThomas Graf 1887ca860fb3SThomas Graf nla_put_failure: 1888c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 188926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 189026932566SPatrick McHardy return -EMSGSIZE; 1891c7fb64dbSThomas Graf } 1892c7fb64dbSThomas Graf 1893ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1894ca860fb3SThomas Graf struct neigh_table *tbl, 1895c7fb64dbSThomas Graf struct neigh_parms *parms, 1896ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1897ca860fb3SThomas Graf unsigned int flags) 1898c7fb64dbSThomas Graf { 1899c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1900c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1901c7fb64dbSThomas Graf 1902ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1903ca860fb3SThomas Graf if (nlh == NULL) 190426932566SPatrick McHardy return -EMSGSIZE; 1905c7fb64dbSThomas Graf 1906ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1907c7fb64dbSThomas Graf 1908c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1909c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 19109ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 19119ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1912c7fb64dbSThomas Graf 1913ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1914ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1915ca860fb3SThomas Graf goto errout; 1916c7fb64dbSThomas Graf 1917c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1918ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1919ca860fb3SThomas Graf errout: 1920c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 192126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 192226932566SPatrick McHardy return -EMSGSIZE; 1923c7fb64dbSThomas Graf } 1924c7fb64dbSThomas Graf 1925ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 19266b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 19276b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 19286b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 19296b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 19306b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 19316b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 19326b3f8674SThomas Graf }; 19336b3f8674SThomas Graf 1934ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 19356b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 19366b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 19376b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 19386b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 19396b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 19406b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 19416b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 19426b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 19436b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 19446b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 19456b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 19466b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 19476b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 19486b3f8674SThomas Graf }; 19496b3f8674SThomas Graf 1950661d2967SThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh) 1951c7fb64dbSThomas Graf { 19523b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1953c7fb64dbSThomas Graf struct neigh_table *tbl; 19546b3f8674SThomas Graf struct ndtmsg *ndtmsg; 19556b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 19566b3f8674SThomas Graf int err; 1957c7fb64dbSThomas Graf 19586b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 19596b3f8674SThomas Graf nl_neightbl_policy); 19606b3f8674SThomas Graf if (err < 0) 19616b3f8674SThomas Graf goto errout; 1962c7fb64dbSThomas Graf 19636b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 19646b3f8674SThomas Graf err = -EINVAL; 19656b3f8674SThomas Graf goto errout; 19666b3f8674SThomas Graf } 19676b3f8674SThomas Graf 19686b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1969c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1970c7fb64dbSThomas Graf for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1971c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1972c7fb64dbSThomas Graf continue; 1973c7fb64dbSThomas Graf 19746b3f8674SThomas Graf if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) 1975c7fb64dbSThomas Graf break; 1976c7fb64dbSThomas Graf } 1977c7fb64dbSThomas Graf 1978c7fb64dbSThomas Graf if (tbl == NULL) { 1979c7fb64dbSThomas Graf err = -ENOENT; 19806b3f8674SThomas Graf goto errout_locked; 1981c7fb64dbSThomas Graf } 1982c7fb64dbSThomas Graf 1983c7fb64dbSThomas Graf /* 1984c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1985c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1986c7fb64dbSThomas Graf */ 1987c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1988c7fb64dbSThomas Graf 19896b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 19906b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 1991c7fb64dbSThomas Graf struct neigh_parms *p; 19926b3f8674SThomas Graf int i, ifindex = 0; 1993c7fb64dbSThomas Graf 19946b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 19956b3f8674SThomas Graf nl_ntbl_parm_policy); 19966b3f8674SThomas Graf if (err < 0) 19976b3f8674SThomas Graf goto errout_tbl_lock; 1998c7fb64dbSThomas Graf 19996b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 20006b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 2001c7fb64dbSThomas Graf 200297fd5bc7STobias Klauser p = lookup_neigh_parms(tbl, net, ifindex); 2003c7fb64dbSThomas Graf if (p == NULL) { 2004c7fb64dbSThomas Graf err = -ENOENT; 20056b3f8674SThomas Graf goto errout_tbl_lock; 2006c7fb64dbSThomas Graf } 2007c7fb64dbSThomas Graf 20086b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 20096b3f8674SThomas Graf if (tbp[i] == NULL) 20106b3f8674SThomas Graf continue; 2011c7fb64dbSThomas Graf 20126b3f8674SThomas Graf switch (i) { 20136b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 20148b5c171bSEric Dumazet p->queue_len_bytes = nla_get_u32(tbp[i]) * 20158b5c171bSEric Dumazet SKB_TRUESIZE(ETH_FRAME_LEN); 20168b5c171bSEric Dumazet break; 20178b5c171bSEric Dumazet case NDTPA_QUEUE_LENBYTES: 20188b5c171bSEric Dumazet p->queue_len_bytes = nla_get_u32(tbp[i]); 20196b3f8674SThomas Graf break; 20206b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 20216b3f8674SThomas Graf p->proxy_qlen = nla_get_u32(tbp[i]); 20226b3f8674SThomas Graf break; 20236b3f8674SThomas Graf case NDTPA_APP_PROBES: 20246b3f8674SThomas Graf p->app_probes = nla_get_u32(tbp[i]); 20256b3f8674SThomas Graf break; 20266b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 20276b3f8674SThomas Graf p->ucast_probes = nla_get_u32(tbp[i]); 20286b3f8674SThomas Graf break; 20296b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 20306b3f8674SThomas Graf p->mcast_probes = nla_get_u32(tbp[i]); 20316b3f8674SThomas Graf break; 20326b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 20336b3f8674SThomas Graf p->base_reachable_time = nla_get_msecs(tbp[i]); 20346b3f8674SThomas Graf break; 20356b3f8674SThomas Graf case NDTPA_GC_STALETIME: 20366b3f8674SThomas Graf p->gc_staletime = nla_get_msecs(tbp[i]); 20376b3f8674SThomas Graf break; 20386b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 20396b3f8674SThomas Graf p->delay_probe_time = nla_get_msecs(tbp[i]); 20406b3f8674SThomas Graf break; 20416b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 20426b3f8674SThomas Graf p->retrans_time = nla_get_msecs(tbp[i]); 20436b3f8674SThomas Graf break; 20446b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 20456b3f8674SThomas Graf p->anycast_delay = nla_get_msecs(tbp[i]); 20466b3f8674SThomas Graf break; 20476b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 20486b3f8674SThomas Graf p->proxy_delay = nla_get_msecs(tbp[i]); 20496b3f8674SThomas Graf break; 20506b3f8674SThomas Graf case NDTPA_LOCKTIME: 20516b3f8674SThomas Graf p->locktime = nla_get_msecs(tbp[i]); 20526b3f8674SThomas Graf break; 2053c7fb64dbSThomas Graf } 20546b3f8674SThomas Graf } 20556b3f8674SThomas Graf } 20566b3f8674SThomas Graf 2057dc25c676SGao feng err = -ENOENT; 2058dc25c676SGao feng if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || 2059dc25c676SGao feng tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) && 2060dc25c676SGao feng !net_eq(net, &init_net)) 2061dc25c676SGao feng goto errout_tbl_lock; 2062dc25c676SGao feng 20636b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 20646b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 20656b3f8674SThomas Graf 20666b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 20676b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 20686b3f8674SThomas Graf 20696b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 20706b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 20716b3f8674SThomas Graf 20726b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 20736b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 2074c7fb64dbSThomas Graf 2075c7fb64dbSThomas Graf err = 0; 2076c7fb64dbSThomas Graf 20776b3f8674SThomas Graf errout_tbl_lock: 2078c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 20796b3f8674SThomas Graf errout_locked: 2080c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 20816b3f8674SThomas Graf errout: 2082c7fb64dbSThomas Graf return err; 2083c7fb64dbSThomas Graf } 2084c7fb64dbSThomas Graf 2085c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 2086c7fb64dbSThomas Graf { 20873b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2088ca860fb3SThomas Graf int family, tidx, nidx = 0; 2089ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 2090ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 2091c7fb64dbSThomas Graf struct neigh_table *tbl; 2092c7fb64dbSThomas Graf 2093ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 2094c7fb64dbSThomas Graf 2095c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 2096ca860fb3SThomas Graf for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) { 2097c7fb64dbSThomas Graf struct neigh_parms *p; 2098c7fb64dbSThomas Graf 2099ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 2100c7fb64dbSThomas Graf continue; 2101c7fb64dbSThomas Graf 210215e47304SEric W. Biederman if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid, 2103ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 2104ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2105c7fb64dbSThomas Graf break; 2106c7fb64dbSThomas Graf 2107426b5303SEric W. Biederman for (nidx = 0, p = tbl->parms.next; p; p = p->next) { 2108878628fbSYOSHIFUJI Hideaki if (!net_eq(neigh_parms_net(p), net)) 2109426b5303SEric W. Biederman continue; 2110426b5303SEric W. Biederman 2111efc683fcSGautam Kachroo if (nidx < neigh_skip) 2112efc683fcSGautam Kachroo goto next; 2113c7fb64dbSThomas Graf 2114ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 211515e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2116ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 2117ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 2118ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2119c7fb64dbSThomas Graf goto out; 2120efc683fcSGautam Kachroo next: 2121efc683fcSGautam Kachroo nidx++; 2122c7fb64dbSThomas Graf } 2123c7fb64dbSThomas Graf 2124ca860fb3SThomas Graf neigh_skip = 0; 2125c7fb64dbSThomas Graf } 2126c7fb64dbSThomas Graf out: 2127c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 2128ca860fb3SThomas Graf cb->args[0] = tidx; 2129ca860fb3SThomas Graf cb->args[1] = nidx; 2130c7fb64dbSThomas Graf 2131c7fb64dbSThomas Graf return skb->len; 2132c7fb64dbSThomas Graf } 21331da177e4SLinus Torvalds 21348b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 21358b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds unsigned long now = jiffies; 21381da177e4SLinus Torvalds struct nda_cacheinfo ci; 21398b8aec50SThomas Graf struct nlmsghdr *nlh; 21408b8aec50SThomas Graf struct ndmsg *ndm; 21411da177e4SLinus Torvalds 21428b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 21438b8aec50SThomas Graf if (nlh == NULL) 214426932566SPatrick McHardy return -EMSGSIZE; 21458b8aec50SThomas Graf 21468b8aec50SThomas Graf ndm = nlmsg_data(nlh); 21478b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 21489ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 21499ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 21508b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 21518b8aec50SThomas Graf ndm->ndm_type = neigh->type; 21528b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 21531da177e4SLinus Torvalds 21549a6308d7SDavid S. Miller if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key)) 21559a6308d7SDavid S. Miller goto nla_put_failure; 21568b8aec50SThomas Graf 21578b8aec50SThomas Graf read_lock_bh(&neigh->lock); 21588b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 21590ed8ddf4SEric Dumazet if (neigh->nud_state & NUD_VALID) { 21600ed8ddf4SEric Dumazet char haddr[MAX_ADDR_LEN]; 21610ed8ddf4SEric Dumazet 21620ed8ddf4SEric Dumazet neigh_ha_snapshot(haddr, neigh, neigh->dev); 21630ed8ddf4SEric Dumazet if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) { 21648b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21658b8aec50SThomas Graf goto nla_put_failure; 21668b8aec50SThomas Graf } 21670ed8ddf4SEric Dumazet } 21688b8aec50SThomas Graf 2169b9f5f52cSStephen Hemminger ci.ndm_used = jiffies_to_clock_t(now - neigh->used); 2170b9f5f52cSStephen Hemminger ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); 2171b9f5f52cSStephen Hemminger ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); 21728b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 21738b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21748b8aec50SThomas Graf 21759a6308d7SDavid S. Miller if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) || 21769a6308d7SDavid S. Miller nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) 21779a6308d7SDavid S. Miller goto nla_put_failure; 21788b8aec50SThomas Graf 21798b8aec50SThomas Graf return nlmsg_end(skb, nlh); 21808b8aec50SThomas Graf 21818b8aec50SThomas Graf nla_put_failure: 218226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 218326932566SPatrick McHardy return -EMSGSIZE; 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 218684920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn, 218784920c14STony Zelenoff u32 pid, u32 seq, int type, unsigned int flags, 218884920c14STony Zelenoff struct neigh_table *tbl) 218984920c14STony Zelenoff { 219084920c14STony Zelenoff struct nlmsghdr *nlh; 219184920c14STony Zelenoff struct ndmsg *ndm; 219284920c14STony Zelenoff 219384920c14STony Zelenoff nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 219484920c14STony Zelenoff if (nlh == NULL) 219584920c14STony Zelenoff return -EMSGSIZE; 219684920c14STony Zelenoff 219784920c14STony Zelenoff ndm = nlmsg_data(nlh); 219884920c14STony Zelenoff ndm->ndm_family = tbl->family; 219984920c14STony Zelenoff ndm->ndm_pad1 = 0; 220084920c14STony Zelenoff ndm->ndm_pad2 = 0; 220184920c14STony Zelenoff ndm->ndm_flags = pn->flags | NTF_PROXY; 220284920c14STony Zelenoff ndm->ndm_type = NDA_DST; 220384920c14STony Zelenoff ndm->ndm_ifindex = pn->dev->ifindex; 220484920c14STony Zelenoff ndm->ndm_state = NUD_NONE; 220584920c14STony Zelenoff 22069a6308d7SDavid S. Miller if (nla_put(skb, NDA_DST, tbl->key_len, pn->key)) 22079a6308d7SDavid S. Miller goto nla_put_failure; 220884920c14STony Zelenoff 220984920c14STony Zelenoff return nlmsg_end(skb, nlh); 221084920c14STony Zelenoff 221184920c14STony Zelenoff nla_put_failure: 221284920c14STony Zelenoff nlmsg_cancel(skb, nlh); 221384920c14STony Zelenoff return -EMSGSIZE; 221484920c14STony Zelenoff } 221584920c14STony Zelenoff 2216d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2217d961db35SThomas Graf { 2218d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2219d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2220d961db35SThomas Graf } 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 22231da177e4SLinus Torvalds struct netlink_callback *cb) 22241da177e4SLinus Torvalds { 22253b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 22261da177e4SLinus Torvalds struct neighbour *n; 22271da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 22281da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 2229d6bf7817SEric Dumazet struct neigh_hash_table *nht; 22301da177e4SLinus Torvalds 2231d6bf7817SEric Dumazet rcu_read_lock_bh(); 2232d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2233d6bf7817SEric Dumazet 22344bd6683bSEric Dumazet for (h = s_h; h < (1 << nht->hash_shift); h++) { 22351da177e4SLinus Torvalds if (h > s_h) 22361da177e4SLinus Torvalds s_idx = 0; 2237767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0; 2238767e97e1SEric Dumazet n != NULL; 2239767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 224009ad9bc7SOctavian Purdila if (!net_eq(dev_net(n->dev), net)) 2241426b5303SEric W. Biederman continue; 2242efc683fcSGautam Kachroo if (idx < s_idx) 2243efc683fcSGautam Kachroo goto next; 224415e47304SEric W. Biederman if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, 22451da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2246b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 2247b6544c0bSJamal Hadi Salim NLM_F_MULTI) <= 0) { 22481da177e4SLinus Torvalds rc = -1; 22491da177e4SLinus Torvalds goto out; 22501da177e4SLinus Torvalds } 2251efc683fcSGautam Kachroo next: 2252efc683fcSGautam Kachroo idx++; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds rc = skb->len; 22561da177e4SLinus Torvalds out: 2257d6bf7817SEric Dumazet rcu_read_unlock_bh(); 22581da177e4SLinus Torvalds cb->args[1] = h; 22591da177e4SLinus Torvalds cb->args[2] = idx; 22601da177e4SLinus Torvalds return rc; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 226384920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 226484920c14STony Zelenoff struct netlink_callback *cb) 226584920c14STony Zelenoff { 226684920c14STony Zelenoff struct pneigh_entry *n; 226784920c14STony Zelenoff struct net *net = sock_net(skb->sk); 226884920c14STony Zelenoff int rc, h, s_h = cb->args[3]; 226984920c14STony Zelenoff int idx, s_idx = idx = cb->args[4]; 227084920c14STony Zelenoff 227184920c14STony Zelenoff read_lock_bh(&tbl->lock); 227284920c14STony Zelenoff 22734bd6683bSEric Dumazet for (h = s_h; h <= PNEIGH_HASHMASK; h++) { 227484920c14STony Zelenoff if (h > s_h) 227584920c14STony Zelenoff s_idx = 0; 227684920c14STony Zelenoff for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) { 227784920c14STony Zelenoff if (dev_net(n->dev) != net) 227884920c14STony Zelenoff continue; 227984920c14STony Zelenoff if (idx < s_idx) 228084920c14STony Zelenoff goto next; 228115e47304SEric W. Biederman if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, 228284920c14STony Zelenoff cb->nlh->nlmsg_seq, 228384920c14STony Zelenoff RTM_NEWNEIGH, 228484920c14STony Zelenoff NLM_F_MULTI, tbl) <= 0) { 228584920c14STony Zelenoff read_unlock_bh(&tbl->lock); 228684920c14STony Zelenoff rc = -1; 228784920c14STony Zelenoff goto out; 228884920c14STony Zelenoff } 228984920c14STony Zelenoff next: 229084920c14STony Zelenoff idx++; 229184920c14STony Zelenoff } 229284920c14STony Zelenoff } 229384920c14STony Zelenoff 229484920c14STony Zelenoff read_unlock_bh(&tbl->lock); 229584920c14STony Zelenoff rc = skb->len; 229684920c14STony Zelenoff out: 229784920c14STony Zelenoff cb->args[3] = h; 229884920c14STony Zelenoff cb->args[4] = idx; 229984920c14STony Zelenoff return rc; 230084920c14STony Zelenoff 230184920c14STony Zelenoff } 230284920c14STony Zelenoff 2303c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 23041da177e4SLinus Torvalds { 23051da177e4SLinus Torvalds struct neigh_table *tbl; 23061da177e4SLinus Torvalds int t, family, s_t; 230784920c14STony Zelenoff int proxy = 0; 23084bd6683bSEric Dumazet int err; 23091da177e4SLinus Torvalds 23101da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 23118b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 231284920c14STony Zelenoff 231384920c14STony Zelenoff /* check for full ndmsg structure presence, family member is 231484920c14STony Zelenoff * the same for both structures 231584920c14STony Zelenoff */ 231684920c14STony Zelenoff if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) && 231784920c14STony Zelenoff ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY) 231884920c14STony Zelenoff proxy = 1; 231984920c14STony Zelenoff 23201da177e4SLinus Torvalds s_t = cb->args[0]; 23211da177e4SLinus Torvalds 23224bd6683bSEric Dumazet for (tbl = neigh_tables, t = 0; tbl; 232384920c14STony Zelenoff tbl = tbl->next, t++) { 23241da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 23251da177e4SLinus Torvalds continue; 23261da177e4SLinus Torvalds if (t > s_t) 23271da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 23281da177e4SLinus Torvalds sizeof(cb->args[0])); 232984920c14STony Zelenoff if (proxy) 233084920c14STony Zelenoff err = pneigh_dump_table(tbl, skb, cb); 233184920c14STony Zelenoff else 233284920c14STony Zelenoff err = neigh_dump_table(tbl, skb, cb); 23334bd6683bSEric Dumazet if (err < 0) 23344bd6683bSEric Dumazet break; 23351da177e4SLinus Torvalds } 23361da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds cb->args[0] = t; 23391da177e4SLinus Torvalds return skb->len; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 23431da177e4SLinus Torvalds { 23441da177e4SLinus Torvalds int chain; 2345d6bf7817SEric Dumazet struct neigh_hash_table *nht; 23461da177e4SLinus Torvalds 2347d6bf7817SEric Dumazet rcu_read_lock_bh(); 2348d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2349d6bf7817SEric Dumazet 2350767e97e1SEric Dumazet read_lock(&tbl->lock); /* avoid resizes */ 2351cd089336SDavid S. Miller for (chain = 0; chain < (1 << nht->hash_shift); chain++) { 23521da177e4SLinus Torvalds struct neighbour *n; 23531da177e4SLinus Torvalds 2354767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[chain]); 2355767e97e1SEric Dumazet n != NULL; 2356767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) 23571da177e4SLinus Torvalds cb(n, cookie); 23581da177e4SLinus Torvalds } 2359d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2360d6bf7817SEric Dumazet rcu_read_unlock_bh(); 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 23651da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 23661da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 23671da177e4SLinus Torvalds { 23681da177e4SLinus Torvalds int chain; 2369d6bf7817SEric Dumazet struct neigh_hash_table *nht; 23701da177e4SLinus Torvalds 2371d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 2372d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 2373cd089336SDavid S. Miller for (chain = 0; chain < (1 << nht->hash_shift); chain++) { 2374767e97e1SEric Dumazet struct neighbour *n; 2375767e97e1SEric Dumazet struct neighbour __rcu **np; 23761da177e4SLinus Torvalds 2377d6bf7817SEric Dumazet np = &nht->hash_buckets[chain]; 2378767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 2379767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 23801da177e4SLinus Torvalds int release; 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds write_lock(&n->lock); 23831da177e4SLinus Torvalds release = cb(n); 23841da177e4SLinus Torvalds if (release) { 2385767e97e1SEric Dumazet rcu_assign_pointer(*np, 2386767e97e1SEric Dumazet rcu_dereference_protected(n->next, 2387767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 23881da177e4SLinus Torvalds n->dead = 1; 23891da177e4SLinus Torvalds } else 23901da177e4SLinus Torvalds np = &n->next; 23911da177e4SLinus Torvalds write_unlock(&n->lock); 23924f494554SThomas Graf if (release) 23934f494554SThomas Graf neigh_cleanup_and_release(n); 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds } 2396ecbb4169SAlexey Kuznetsov } 23971da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 24021da177e4SLinus Torvalds { 24031da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24041218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2405d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 24061da177e4SLinus Torvalds struct neighbour *n = NULL; 24071da177e4SLinus Torvalds int bucket = state->bucket; 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 2410cd089336SDavid S. Miller for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) { 2411767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[bucket]); 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds while (n) { 2414878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2415426b5303SEric W. Biederman goto next; 24161da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24171da177e4SLinus Torvalds loff_t fakep = 0; 24181da177e4SLinus Torvalds void *v; 24191da177e4SLinus Torvalds 24201da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 24211da177e4SLinus Torvalds if (!v) 24221da177e4SLinus Torvalds goto next; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 24251da177e4SLinus Torvalds break; 24261da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 24271da177e4SLinus Torvalds break; 24281da177e4SLinus Torvalds next: 2429767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds if (n) 24331da177e4SLinus Torvalds break; 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds state->bucket = bucket; 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds return n; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 24411da177e4SLinus Torvalds struct neighbour *n, 24421da177e4SLinus Torvalds loff_t *pos) 24431da177e4SLinus Torvalds { 24441da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24451218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2446d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24491da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 24501da177e4SLinus Torvalds if (v) 24511da177e4SLinus Torvalds return n; 24521da177e4SLinus Torvalds } 2453767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds while (1) { 24561da177e4SLinus Torvalds while (n) { 2457878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2458426b5303SEric W. Biederman goto next; 24591da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24601da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 24611da177e4SLinus Torvalds if (v) 24621da177e4SLinus Torvalds return n; 24631da177e4SLinus Torvalds goto next; 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 24661da177e4SLinus Torvalds break; 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 24691da177e4SLinus Torvalds break; 24701da177e4SLinus Torvalds next: 2471767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds if (n) 24751da177e4SLinus Torvalds break; 24761da177e4SLinus Torvalds 2477cd089336SDavid S. Miller if (++state->bucket >= (1 << nht->hash_shift)) 24781da177e4SLinus Torvalds break; 24791da177e4SLinus Torvalds 2480767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[state->bucket]); 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds if (n && pos) 24841da177e4SLinus Torvalds --(*pos); 24851da177e4SLinus Torvalds return n; 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 24891da177e4SLinus Torvalds { 24901da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds if (n) { 2493745e2031SChris Larson --(*pos); 24941da177e4SLinus Torvalds while (*pos) { 24951da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 24961da177e4SLinus Torvalds if (!n) 24971da177e4SLinus Torvalds break; 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds return *pos ? NULL : n; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25061218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 25071da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 25081da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 25091da177e4SLinus Torvalds int bucket = state->bucket; 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 25121da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 25131da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2514878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2515426b5303SEric W. Biederman pn = pn->next; 25161da177e4SLinus Torvalds if (pn) 25171da177e4SLinus Torvalds break; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds state->bucket = bucket; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds return pn; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 25251da177e4SLinus Torvalds struct pneigh_entry *pn, 25261da177e4SLinus Torvalds loff_t *pos) 25271da177e4SLinus Torvalds { 25281da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25291218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 25301da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 25311da177e4SLinus Torvalds 2532df07a94cSJorge Boncompte [DTI2] do { 25331da177e4SLinus Torvalds pn = pn->next; 2534df07a94cSJorge Boncompte [DTI2] } while (pn && !net_eq(pneigh_net(pn), net)); 2535df07a94cSJorge Boncompte [DTI2] 25361da177e4SLinus Torvalds while (!pn) { 25371da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 25381da177e4SLinus Torvalds break; 25391da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2540878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2541426b5303SEric W. Biederman pn = pn->next; 25421da177e4SLinus Torvalds if (pn) 25431da177e4SLinus Torvalds break; 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds if (pn && pos) 25471da177e4SLinus Torvalds --(*pos); 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds return pn; 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 25531da177e4SLinus Torvalds { 25541da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds if (pn) { 2557745e2031SChris Larson --(*pos); 25581da177e4SLinus Torvalds while (*pos) { 25591da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 25601da177e4SLinus Torvalds if (!pn) 25611da177e4SLinus Torvalds break; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds return *pos ? NULL : pn; 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 25681da177e4SLinus Torvalds { 25691da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25701da177e4SLinus Torvalds void *rc; 2571745e2031SChris Larson loff_t idxpos = *pos; 25721da177e4SLinus Torvalds 2573745e2031SChris Larson rc = neigh_get_idx(seq, &idxpos); 25741da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 2575745e2031SChris Larson rc = pneigh_get_idx(seq, &idxpos); 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds return rc; 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 2581d6bf7817SEric Dumazet __acquires(rcu_bh) 25821da177e4SLinus Torvalds { 25831da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds state->tbl = tbl; 25861da177e4SLinus Torvalds state->bucket = 0; 25871da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 25881da177e4SLinus Torvalds 2589d6bf7817SEric Dumazet rcu_read_lock_bh(); 2590d6bf7817SEric Dumazet state->nht = rcu_dereference_bh(tbl->nht); 2591767e97e1SEric Dumazet 2592745e2031SChris Larson return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25971da177e4SLinus Torvalds { 25981da177e4SLinus Torvalds struct neigh_seq_state *state; 25991da177e4SLinus Torvalds void *rc; 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2602bff69732SChris Larson rc = neigh_get_first(seq); 26031da177e4SLinus Torvalds goto out; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds state = seq->private; 26071da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 26081da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 26091da177e4SLinus Torvalds if (rc) 26101da177e4SLinus Torvalds goto out; 26111da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 26121da177e4SLinus Torvalds rc = pneigh_get_first(seq); 26131da177e4SLinus Torvalds } else { 26141da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 26151da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 26161da177e4SLinus Torvalds } 26171da177e4SLinus Torvalds out: 26181da177e4SLinus Torvalds ++(*pos); 26191da177e4SLinus Torvalds return rc; 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 2624d6bf7817SEric Dumazet __releases(rcu_bh) 26251da177e4SLinus Torvalds { 2626d6bf7817SEric Dumazet rcu_read_unlock_bh(); 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds /* statistics via seq_file */ 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 26331da177e4SLinus Torvalds { 263481c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26351da177e4SLinus Torvalds int cpu; 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds if (*pos == 0) 26381da177e4SLinus Torvalds return SEQ_START_TOKEN; 26391da177e4SLinus Torvalds 26400f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 26411da177e4SLinus Torvalds if (!cpu_possible(cpu)) 26421da177e4SLinus Torvalds continue; 26431da177e4SLinus Torvalds *pos = cpu+1; 26441da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds return NULL; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26501da177e4SLinus Torvalds { 265181c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26521da177e4SLinus Torvalds int cpu; 26531da177e4SLinus Torvalds 26540f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 26551da177e4SLinus Torvalds if (!cpu_possible(cpu)) 26561da177e4SLinus Torvalds continue; 26571da177e4SLinus Torvalds *pos = cpu+1; 26581da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds return NULL; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 26691da177e4SLinus Torvalds { 267081c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26711da177e4SLinus Torvalds struct neigh_statistics *st = v; 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26749a6d276eSNeil 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"); 26751da177e4SLinus Torvalds return 0; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 26799a6d276eSNeil Horman "%08lx %08lx %08lx %08lx %08lx\n", 26801da177e4SLinus Torvalds atomic_read(&tbl->entries), 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds st->allocs, 26831da177e4SLinus Torvalds st->destroys, 26841da177e4SLinus Torvalds st->hash_grows, 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds st->lookups, 26871da177e4SLinus Torvalds st->hits, 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds st->res_failed, 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds st->rcv_probes_mcast, 26921da177e4SLinus Torvalds st->rcv_probes_ucast, 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds st->periodic_gc_runs, 26959a6d276eSNeil Horman st->forced_gc_runs, 26969a6d276eSNeil Horman st->unres_discards 26971da177e4SLinus Torvalds ); 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds return 0; 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 2702f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 27031da177e4SLinus Torvalds .start = neigh_stat_seq_start, 27041da177e4SLinus Torvalds .next = neigh_stat_seq_next, 27051da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 27061da177e4SLinus Torvalds .show = neigh_stat_seq_show, 27071da177e4SLinus Torvalds }; 27081da177e4SLinus Torvalds 27091da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 27101da177e4SLinus Torvalds { 27111da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds if (!ret) { 27141da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 2715d9dda78bSAl Viro sf->private = PDE_DATA(inode); 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds return ret; 27181da177e4SLinus Torvalds }; 27191da177e4SLinus Torvalds 27209a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 27211da177e4SLinus Torvalds .owner = THIS_MODULE, 27221da177e4SLinus Torvalds .open = neigh_stat_seq_open, 27231da177e4SLinus Torvalds .read = seq_read, 27241da177e4SLinus Torvalds .llseek = seq_lseek, 27251da177e4SLinus Torvalds .release = seq_release, 27261da177e4SLinus Torvalds }; 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27291da177e4SLinus Torvalds 2730339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2731339bf98fSThomas Graf { 2732339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2733339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2734339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2735339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2736339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2737339bf98fSThomas Graf } 2738339bf98fSThomas Graf 2739b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 27401da177e4SLinus Torvalds { 2741c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(n->dev); 27428b8aec50SThomas Graf struct sk_buff *skb; 2743b8673311SThomas Graf int err = -ENOBUFS; 27441da177e4SLinus Torvalds 2745339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 27468b8aec50SThomas Graf if (skb == NULL) 2747b8673311SThomas Graf goto errout; 27481da177e4SLinus Torvalds 2749b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 275026932566SPatrick McHardy if (err < 0) { 275126932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 275226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 275326932566SPatrick McHardy kfree_skb(skb); 275426932566SPatrick McHardy goto errout; 275526932566SPatrick McHardy } 27561ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 27571ce85fe4SPablo Neira Ayuso return; 2758b8673311SThomas Graf errout: 2759b8673311SThomas Graf if (err < 0) 2760426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2761b8673311SThomas Graf } 2762b8673311SThomas Graf 2763b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2764b8673311SThomas Graf { 2765b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 27668b8aec50SThomas Graf } 27670a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns); 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 2770b93196dcSCong Wang static int zero; 2771555445cdSFrancesco Fusco static int int_max = INT_MAX; 2772b93196dcSCong Wang static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN); 27731da177e4SLinus Torvalds 2774fe2c6338SJoe Perches static int proc_unres_qlen(struct ctl_table *ctl, int write, 2775fe2c6338SJoe Perches void __user *buffer, size_t *lenp, loff_t *ppos) 27768b5c171bSEric Dumazet { 27778b5c171bSEric Dumazet int size, ret; 2778fe2c6338SJoe Perches struct ctl_table tmp = *ctl; 27798b5c171bSEric Dumazet 2780ce46cc64SShan Wei tmp.extra1 = &zero; 2781ce46cc64SShan Wei tmp.extra2 = &unres_qlen_max; 27828b5c171bSEric Dumazet tmp.data = &size; 2783ce46cc64SShan Wei 2784ce46cc64SShan Wei size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN); 2785ce46cc64SShan Wei ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 2786ce46cc64SShan Wei 27878b5c171bSEric Dumazet if (write && !ret) 27888b5c171bSEric Dumazet *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN); 27898b5c171bSEric Dumazet return ret; 27908b5c171bSEric Dumazet } 27918b5c171bSEric Dumazet 27928b5c171bSEric Dumazet enum { 27938b5c171bSEric Dumazet NEIGH_VAR_MCAST_PROBE, 27948b5c171bSEric Dumazet NEIGH_VAR_UCAST_PROBE, 27958b5c171bSEric Dumazet NEIGH_VAR_APP_PROBE, 27968b5c171bSEric Dumazet NEIGH_VAR_RETRANS_TIME, 27978b5c171bSEric Dumazet NEIGH_VAR_BASE_REACHABLE_TIME, 27988b5c171bSEric Dumazet NEIGH_VAR_DELAY_PROBE_TIME, 27998b5c171bSEric Dumazet NEIGH_VAR_GC_STALETIME, 28008b5c171bSEric Dumazet NEIGH_VAR_QUEUE_LEN, 28018b5c171bSEric Dumazet NEIGH_VAR_QUEUE_LEN_BYTES, 28028b5c171bSEric Dumazet NEIGH_VAR_PROXY_QLEN, 28038b5c171bSEric Dumazet NEIGH_VAR_ANYCAST_DELAY, 28048b5c171bSEric Dumazet NEIGH_VAR_PROXY_DELAY, 28058b5c171bSEric Dumazet NEIGH_VAR_LOCKTIME, 28068b5c171bSEric Dumazet NEIGH_VAR_RETRANS_TIME_MS, 28078b5c171bSEric Dumazet NEIGH_VAR_BASE_REACHABLE_TIME_MS, 28088b5c171bSEric Dumazet NEIGH_VAR_GC_INTERVAL, 28098b5c171bSEric Dumazet NEIGH_VAR_GC_THRESH1, 28108b5c171bSEric Dumazet NEIGH_VAR_GC_THRESH2, 28118b5c171bSEric Dumazet NEIGH_VAR_GC_THRESH3, 28128b5c171bSEric Dumazet NEIGH_VAR_MAX 28138b5c171bSEric Dumazet }; 281454716e3bSEric W. Biederman 28151da177e4SLinus Torvalds static struct neigh_sysctl_table { 28161da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 28178b5c171bSEric Dumazet struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1]; 2818ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 28191da177e4SLinus Torvalds .neigh_vars = { 28208b5c171bSEric Dumazet [NEIGH_VAR_MCAST_PROBE] = { 28211da177e4SLinus Torvalds .procname = "mcast_solicit", 28221da177e4SLinus Torvalds .maxlen = sizeof(int), 28231da177e4SLinus Torvalds .mode = 0644, 2824555445cdSFrancesco Fusco .extra1 = &zero, 2825555445cdSFrancesco Fusco .extra2 = &int_max, 2826555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 28271da177e4SLinus Torvalds }, 28288b5c171bSEric Dumazet [NEIGH_VAR_UCAST_PROBE] = { 28291da177e4SLinus Torvalds .procname = "ucast_solicit", 28301da177e4SLinus Torvalds .maxlen = sizeof(int), 28311da177e4SLinus Torvalds .mode = 0644, 2832555445cdSFrancesco Fusco .extra1 = &zero, 2833555445cdSFrancesco Fusco .extra2 = &int_max, 2834555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 28351da177e4SLinus Torvalds }, 28368b5c171bSEric Dumazet [NEIGH_VAR_APP_PROBE] = { 28371da177e4SLinus Torvalds .procname = "app_solicit", 28381da177e4SLinus Torvalds .maxlen = sizeof(int), 28391da177e4SLinus Torvalds .mode = 0644, 2840555445cdSFrancesco Fusco .extra1 = &zero, 2841555445cdSFrancesco Fusco .extra2 = &int_max, 2842555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 28431da177e4SLinus Torvalds }, 28448b5c171bSEric Dumazet [NEIGH_VAR_RETRANS_TIME] = { 28451da177e4SLinus Torvalds .procname = "retrans_time", 28461da177e4SLinus Torvalds .maxlen = sizeof(int), 28471da177e4SLinus Torvalds .mode = 0644, 28486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 28491da177e4SLinus Torvalds }, 28508b5c171bSEric Dumazet [NEIGH_VAR_BASE_REACHABLE_TIME] = { 28511da177e4SLinus Torvalds .procname = "base_reachable_time", 28521da177e4SLinus Torvalds .maxlen = sizeof(int), 28531da177e4SLinus Torvalds .mode = 0644, 28546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28551da177e4SLinus Torvalds }, 28568b5c171bSEric Dumazet [NEIGH_VAR_DELAY_PROBE_TIME] = { 28571da177e4SLinus Torvalds .procname = "delay_first_probe_time", 28581da177e4SLinus Torvalds .maxlen = sizeof(int), 28591da177e4SLinus Torvalds .mode = 0644, 28606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28611da177e4SLinus Torvalds }, 28628b5c171bSEric Dumazet [NEIGH_VAR_GC_STALETIME] = { 28631da177e4SLinus Torvalds .procname = "gc_stale_time", 28641da177e4SLinus Torvalds .maxlen = sizeof(int), 28651da177e4SLinus Torvalds .mode = 0644, 28666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28671da177e4SLinus Torvalds }, 28688b5c171bSEric Dumazet [NEIGH_VAR_QUEUE_LEN] = { 28691da177e4SLinus Torvalds .procname = "unres_qlen", 28701da177e4SLinus Torvalds .maxlen = sizeof(int), 28711da177e4SLinus Torvalds .mode = 0644, 28728b5c171bSEric Dumazet .proc_handler = proc_unres_qlen, 28738b5c171bSEric Dumazet }, 28748b5c171bSEric Dumazet [NEIGH_VAR_QUEUE_LEN_BYTES] = { 28758b5c171bSEric Dumazet .procname = "unres_qlen_bytes", 28768b5c171bSEric Dumazet .maxlen = sizeof(int), 28778b5c171bSEric Dumazet .mode = 0644, 2878ce46cc64SShan Wei .extra1 = &zero, 2879ce46cc64SShan Wei .proc_handler = proc_dointvec_minmax, 28801da177e4SLinus Torvalds }, 28818b5c171bSEric Dumazet [NEIGH_VAR_PROXY_QLEN] = { 28821da177e4SLinus Torvalds .procname = "proxy_qlen", 28831da177e4SLinus Torvalds .maxlen = sizeof(int), 28841da177e4SLinus Torvalds .mode = 0644, 2885555445cdSFrancesco Fusco .extra1 = &zero, 2886555445cdSFrancesco Fusco .extra2 = &int_max, 2887555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 28881da177e4SLinus Torvalds }, 28898b5c171bSEric Dumazet [NEIGH_VAR_ANYCAST_DELAY] = { 28901da177e4SLinus Torvalds .procname = "anycast_delay", 28911da177e4SLinus Torvalds .maxlen = sizeof(int), 28921da177e4SLinus Torvalds .mode = 0644, 28936d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 28941da177e4SLinus Torvalds }, 28958b5c171bSEric Dumazet [NEIGH_VAR_PROXY_DELAY] = { 28961da177e4SLinus Torvalds .procname = "proxy_delay", 28971da177e4SLinus Torvalds .maxlen = sizeof(int), 28981da177e4SLinus Torvalds .mode = 0644, 28996d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 29001da177e4SLinus Torvalds }, 29018b5c171bSEric Dumazet [NEIGH_VAR_LOCKTIME] = { 29021da177e4SLinus Torvalds .procname = "locktime", 29031da177e4SLinus Torvalds .maxlen = sizeof(int), 29041da177e4SLinus Torvalds .mode = 0644, 29056d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 29061da177e4SLinus Torvalds }, 29078b5c171bSEric Dumazet [NEIGH_VAR_RETRANS_TIME_MS] = { 2908d12af679SEric W. Biederman .procname = "retrans_time_ms", 2909d12af679SEric W. Biederman .maxlen = sizeof(int), 2910d12af679SEric W. Biederman .mode = 0644, 29116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2912d12af679SEric W. Biederman }, 29138b5c171bSEric Dumazet [NEIGH_VAR_BASE_REACHABLE_TIME_MS] = { 2914d12af679SEric W. Biederman .procname = "base_reachable_time_ms", 2915d12af679SEric W. Biederman .maxlen = sizeof(int), 2916d12af679SEric W. Biederman .mode = 0644, 29176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2918d12af679SEric W. Biederman }, 29198b5c171bSEric Dumazet [NEIGH_VAR_GC_INTERVAL] = { 29201da177e4SLinus Torvalds .procname = "gc_interval", 29211da177e4SLinus Torvalds .maxlen = sizeof(int), 29221da177e4SLinus Torvalds .mode = 0644, 29236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 29241da177e4SLinus Torvalds }, 29258b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH1] = { 29261da177e4SLinus Torvalds .procname = "gc_thresh1", 29271da177e4SLinus Torvalds .maxlen = sizeof(int), 29281da177e4SLinus Torvalds .mode = 0644, 2929555445cdSFrancesco Fusco .extra1 = &zero, 2930555445cdSFrancesco Fusco .extra2 = &int_max, 2931555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 29321da177e4SLinus Torvalds }, 29338b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH2] = { 29341da177e4SLinus Torvalds .procname = "gc_thresh2", 29351da177e4SLinus Torvalds .maxlen = sizeof(int), 29361da177e4SLinus Torvalds .mode = 0644, 2937555445cdSFrancesco Fusco .extra1 = &zero, 2938555445cdSFrancesco Fusco .extra2 = &int_max, 2939555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 29401da177e4SLinus Torvalds }, 29418b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH3] = { 29421da177e4SLinus Torvalds .procname = "gc_thresh3", 29431da177e4SLinus Torvalds .maxlen = sizeof(int), 29441da177e4SLinus Torvalds .mode = 0644, 2945555445cdSFrancesco Fusco .extra1 = &zero, 2946555445cdSFrancesco Fusco .extra2 = &int_max, 2947555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 29481da177e4SLinus Torvalds }, 2949c3bac5a7SPavel Emelyanov {}, 29501da177e4SLinus Torvalds }, 29511da177e4SLinus Torvalds }; 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 295454716e3bSEric W. Biederman char *p_name, proc_handler *handler) 29551da177e4SLinus Torvalds { 29563c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 29571da177e4SLinus Torvalds const char *dev_name_source = NULL; 29588f40a1f9SEric W. Biederman char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ]; 29591da177e4SLinus Torvalds 29603c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 29611da177e4SLinus Torvalds if (!t) 29623c607bbbSPavel Emelyanov goto err; 29633c607bbbSPavel Emelyanov 29648b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_MCAST_PROBE].data = &p->mcast_probes; 29658b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_UCAST_PROBE].data = &p->ucast_probes; 29668b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_APP_PROBE].data = &p->app_probes; 29678b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].data = &p->retrans_time; 29688b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].data = &p->base_reachable_time; 29698b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_DELAY_PROBE_TIME].data = &p->delay_probe_time; 29708b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_STALETIME].data = &p->gc_staletime; 29718b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_QUEUE_LEN].data = &p->queue_len_bytes; 29728b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_QUEUE_LEN_BYTES].data = &p->queue_len_bytes; 29738b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_PROXY_QLEN].data = &p->proxy_qlen; 29748b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_ANYCAST_DELAY].data = &p->anycast_delay; 29758b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_PROXY_DELAY].data = &p->proxy_delay; 29768b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_LOCKTIME].data = &p->locktime; 29778b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].data = &p->retrans_time; 29788b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].data = &p->base_reachable_time; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds if (dev) { 29811da177e4SLinus Torvalds dev_name_source = dev->name; 2982d12af679SEric W. Biederman /* Terminate the table early */ 29838b5c171bSEric Dumazet memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0, 29848b5c171bSEric Dumazet sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL])); 29851da177e4SLinus Torvalds } else { 29868f40a1f9SEric W. Biederman dev_name_source = "default"; 29878b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = (int *)(p + 1); 29888b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = (int *)(p + 1) + 1; 29898b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = (int *)(p + 1) + 2; 29908b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = (int *)(p + 1) + 3; 29911da177e4SLinus Torvalds } 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds 2994f8572d8fSEric W. Biederman if (handler) { 29951da177e4SLinus Torvalds /* RetransTime */ 29968b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler; 29978b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].extra1 = dev; 29981da177e4SLinus Torvalds /* ReachableTime */ 29998b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler; 30008b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].extra1 = dev; 30011da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 30028b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler; 30038b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].extra1 = dev; 30041da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 30058b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler; 30068b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].extra1 = dev; 30071da177e4SLinus Torvalds } 30081da177e4SLinus Torvalds 3009464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3010464dc801SEric W. Biederman if (neigh_parms_net(p)->user_ns != &init_user_ns) 3011464dc801SEric W. Biederman t->neigh_vars[0].procname = NULL; 3012464dc801SEric W. Biederman 30138f40a1f9SEric W. Biederman snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s", 30148f40a1f9SEric W. Biederman p_name, dev_name_source); 30154ab438fcSDenis V. Lunev t->sysctl_header = 30168f40a1f9SEric W. Biederman register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars); 30173c607bbbSPavel Emelyanov if (!t->sysctl_header) 30188f40a1f9SEric W. Biederman goto free; 30193c607bbbSPavel Emelyanov 30201da177e4SLinus Torvalds p->sysctl_table = t; 30211da177e4SLinus Torvalds return 0; 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds free: 30241da177e4SLinus Torvalds kfree(t); 30253c607bbbSPavel Emelyanov err: 30263c607bbbSPavel Emelyanov return -ENOBUFS; 30271da177e4SLinus Torvalds } 30280a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register); 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 30311da177e4SLinus Torvalds { 30321da177e4SLinus Torvalds if (p->sysctl_table) { 30331da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 30341da177e4SLinus Torvalds p->sysctl_table = NULL; 30355dd3df10SEric W. Biederman unregister_net_sysctl_table(t->sysctl_header); 30361da177e4SLinus Torvalds kfree(t); 30371da177e4SLinus Torvalds } 30381da177e4SLinus Torvalds } 30390a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister); 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 30421da177e4SLinus Torvalds 3043c8822a4eSThomas Graf static int __init neigh_init(void) 3044c8822a4eSThomas Graf { 3045c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL); 3046c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL); 3047c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL); 3048c8822a4eSThomas Graf 3049c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info, 3050c7ac8679SGreg Rose NULL); 3051c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL); 3052c8822a4eSThomas Graf 3053c8822a4eSThomas Graf return 0; 3054c8822a4eSThomas Graf } 3055c8822a4eSThomas Graf 3056c8822a4eSThomas Graf subsys_initcall(neigh_init); 3057c8822a4eSThomas Graf 3058