11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Generic address resolution entity 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: 51da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 61da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Fixes: 141da177e4SLinus Torvalds * Vitaly E. Lavrov releasing NULL neighbor in neigh_add. 151da177e4SLinus Torvalds * Harald Welte Add neighbour cache statistics like rtstat 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 185a0e3ad6STejun Heo #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/types.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/socket.h> 231da177e4SLinus Torvalds #include <linux/netdevice.h> 241da177e4SLinus Torvalds #include <linux/proc_fs.h> 251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 261da177e4SLinus Torvalds #include <linux/sysctl.h> 271da177e4SLinus Torvalds #endif 281da177e4SLinus Torvalds #include <linux/times.h> 29457c4cbcSEric W. Biederman #include <net/net_namespace.h> 301da177e4SLinus Torvalds #include <net/neighbour.h> 311da177e4SLinus Torvalds #include <net/dst.h> 321da177e4SLinus Torvalds #include <net/sock.h> 338d71740cSTom Tucker #include <net/netevent.h> 34a14a49d2SThomas Graf #include <net/netlink.h> 351da177e4SLinus Torvalds #include <linux/rtnetlink.h> 361da177e4SLinus Torvalds #include <linux/random.h> 37543537bdSPaulo Marques #include <linux/string.h> 38c3609d51Svignesh babu #include <linux/log2.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define NEIGH_DEBUG 1 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define NEIGH_PRINTK(x...) printk(x) 431da177e4SLinus Torvalds #define NEIGH_NOPRINTK(x...) do { ; } while(0) 441da177e4SLinus Torvalds #define NEIGH_PRINTK0 NEIGH_PRINTK 451da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_NOPRINTK 461da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_NOPRINTK 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #if NEIGH_DEBUG >= 1 491da177e4SLinus Torvalds #undef NEIGH_PRINTK1 501da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_PRINTK 511da177e4SLinus Torvalds #endif 521da177e4SLinus Torvalds #if NEIGH_DEBUG >= 2 531da177e4SLinus Torvalds #undef NEIGH_PRINTK2 541da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_PRINTK 551da177e4SLinus Torvalds #endif 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds #define PNEIGH_HASHMASK 0xF 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg); 60d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags); 61d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh); 621da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds static struct neigh_table *neigh_tables; 6545fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS 669a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops; 6745fc3b11SAmos Waterland #endif 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* 701da177e4SLinus Torvalds Neighbour hash table buckets are protected with rwlock tbl->lock. 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds - All the scans/updates to hash buckets MUST be made under this lock. 731da177e4SLinus Torvalds - NOTHING clever should be made under this lock: no callbacks 741da177e4SLinus Torvalds to protocol backends, no attempts to send something to network. 751da177e4SLinus Torvalds It will result in deadlocks, if backend/driver wants to use neighbour 761da177e4SLinus Torvalds cache. 771da177e4SLinus Torvalds - If the entry requires some non-trivial actions, increase 781da177e4SLinus Torvalds its reference count and release table lock. 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds Neighbour entries are protected: 811da177e4SLinus Torvalds - with reference count. 821da177e4SLinus Torvalds - with rwlock neigh->lock 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds Reference count prevents destruction. 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds neigh->lock mainly serializes ll address data and its validity state. 871da177e4SLinus Torvalds However, the same lock is used to protect another entry fields: 881da177e4SLinus Torvalds - timer 891da177e4SLinus Torvalds - resolution queue 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds Again, nothing clever shall be made under neigh->lock, 921da177e4SLinus Torvalds the most complicated procedure, which we allow is dev->hard_header. 931da177e4SLinus Torvalds It is supposed, that dev->hard_header is simplistic and does 941da177e4SLinus Torvalds not make callbacks to neighbour tables. 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds The last lock is neigh_tbl_lock. It is pure SMP lock, protecting 971da177e4SLinus Torvalds list of neighbour tables. This list is used only in process context, 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static int neigh_blackhole(struct sk_buff *skb) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds kfree_skb(skb); 1051da177e4SLinus Torvalds return -ENETDOWN; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1084f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh) 1094f494554SThomas Graf { 1104f494554SThomas Graf if (neigh->parms->neigh_cleanup) 1114f494554SThomas Graf neigh->parms->neigh_cleanup(neigh); 1124f494554SThomas Graf 113d961db35SThomas Graf __neigh_notify(neigh, RTM_DELNEIGH, 0); 1144f494554SThomas Graf neigh_release(neigh); 1154f494554SThomas Graf } 1164f494554SThomas Graf 1171da177e4SLinus Torvalds /* 1181da177e4SLinus Torvalds * It is random distribution in the interval (1/2)*base...(3/2)*base. 1191da177e4SLinus Torvalds * It corresponds to default IPv6 settings and is not overridable, 1201da177e4SLinus Torvalds * because it is really reasonable choice. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base) 1241da177e4SLinus Torvalds { 125a02cec21SEric Dumazet return base ? (net_random() % base) + (base >> 1) : 0; 1261da177e4SLinus Torvalds } 1270a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds int shrunk = 0; 1331da177e4SLinus Torvalds int i; 134d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 139d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 140d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 141d6bf7817SEric Dumazet for (i = 0; i <= nht->hash_mask; i++) { 142767e97e1SEric Dumazet struct neighbour *n; 143767e97e1SEric Dumazet struct neighbour __rcu **np; 1441da177e4SLinus Torvalds 145d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 146767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 147767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 1481da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1491da177e4SLinus Torvalds * - nobody refers to it. 1501da177e4SLinus Torvalds * - it is not permanent 1511da177e4SLinus Torvalds */ 1521da177e4SLinus Torvalds write_lock(&n->lock); 1531da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1541da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 155767e97e1SEric Dumazet rcu_assign_pointer(*np, 156767e97e1SEric Dumazet rcu_dereference_protected(n->next, 157767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 1581da177e4SLinus Torvalds n->dead = 1; 1591da177e4SLinus Torvalds shrunk = 1; 1601da177e4SLinus Torvalds write_unlock(&n->lock); 1614f494554SThomas Graf neigh_cleanup_and_release(n); 1621da177e4SLinus Torvalds continue; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds write_unlock(&n->lock); 1651da177e4SLinus Torvalds np = &n->next; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds tbl->last_flush = jiffies; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds return shrunk; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 176a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 177a43d8994SPavel Emelyanov { 178a43d8994SPavel Emelyanov neigh_hold(n); 179a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 180a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 181a43d8994SPavel Emelyanov n->nud_state); 182a43d8994SPavel Emelyanov dump_stack(); 183a43d8994SPavel Emelyanov } 184a43d8994SPavel Emelyanov } 185a43d8994SPavel Emelyanov 1861da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1891da177e4SLinus Torvalds del_timer(&n->timer)) { 1901da177e4SLinus Torvalds neigh_release(n); 1911da177e4SLinus Torvalds return 1; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds return 0; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds struct sk_buff *skb; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 2011da177e4SLinus Torvalds dev_put(skb->dev); 2021da177e4SLinus Torvalds kfree_skb(skb); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 20649636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds int i; 209d6bf7817SEric Dumazet struct neigh_hash_table *nht; 2101da177e4SLinus Torvalds 211d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 212d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 213d6bf7817SEric Dumazet 214d6bf7817SEric Dumazet for (i = 0; i <= nht->hash_mask; i++) { 215767e97e1SEric Dumazet struct neighbour *n; 216767e97e1SEric Dumazet struct neighbour __rcu **np = &nht->hash_buckets[i]; 2171da177e4SLinus Torvalds 218767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 219767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 2201da177e4SLinus Torvalds if (dev && n->dev != dev) { 2211da177e4SLinus Torvalds np = &n->next; 2221da177e4SLinus Torvalds continue; 2231da177e4SLinus Torvalds } 224767e97e1SEric Dumazet rcu_assign_pointer(*np, 225767e97e1SEric Dumazet rcu_dereference_protected(n->next, 226767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 2271da177e4SLinus Torvalds write_lock(&n->lock); 2281da177e4SLinus Torvalds neigh_del_timer(n); 2291da177e4SLinus Torvalds n->dead = 1; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2321da177e4SLinus Torvalds /* The most unpleasant situation. 2331da177e4SLinus Torvalds We must destroy neighbour entry, 2341da177e4SLinus Torvalds but someone still uses it. 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds The destroy will be delayed until 2371da177e4SLinus Torvalds the last user releases us, but 2381da177e4SLinus Torvalds we must kill timers etc. and move 2391da177e4SLinus Torvalds it to safe state. 2401da177e4SLinus Torvalds */ 2411da177e4SLinus Torvalds skb_queue_purge(&n->arp_queue); 2421da177e4SLinus Torvalds n->output = neigh_blackhole; 2431da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2441da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2451da177e4SLinus Torvalds else 2461da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2471da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is stray.\n", n); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds write_unlock(&n->lock); 2504f494554SThomas Graf neigh_cleanup_and_release(n); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds } 25349636bb1SHerbert Xu } 2541da177e4SLinus Torvalds 25549636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 25649636bb1SHerbert Xu { 25749636bb1SHerbert Xu write_lock_bh(&tbl->lock); 25849636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 25949636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 26049636bb1SHerbert Xu } 2610a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr); 26249636bb1SHerbert Xu 26349636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 26449636bb1SHerbert Xu { 26549636bb1SHerbert Xu write_lock_bh(&tbl->lock); 26649636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2671da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2681da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2711da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2721da177e4SLinus Torvalds return 0; 2731da177e4SLinus Torvalds } 2740a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static struct neighbour *neigh_alloc(struct neigh_table *tbl) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds struct neighbour *n = NULL; 2791da177e4SLinus Torvalds unsigned long now = jiffies; 2801da177e4SLinus Torvalds int entries; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2831da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2841da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2851da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2861da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2871da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2881da177e4SLinus Torvalds goto out_entries; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 291c3762229SRobert P. J. Day n = kmem_cache_zalloc(tbl->kmem_cachep, GFP_ATOMIC); 2921da177e4SLinus Torvalds if (!n) 2931da177e4SLinus Torvalds goto out_entries; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds skb_queue_head_init(&n->arp_queue); 2961da177e4SLinus Torvalds rwlock_init(&n->lock); 2971da177e4SLinus Torvalds n->updated = n->used = now; 2981da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2991da177e4SLinus Torvalds n->output = neigh_blackhole; 3001da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 301b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 3041da177e4SLinus Torvalds n->tbl = tbl; 3051da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 3061da177e4SLinus Torvalds n->dead = 1; 3071da177e4SLinus Torvalds out: 3081da177e4SLinus Torvalds return n; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds out_entries: 3111da177e4SLinus Torvalds atomic_dec(&tbl->entries); 3121da177e4SLinus Torvalds goto out; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 315d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_alloc(unsigned int entries) 3161da177e4SLinus Torvalds { 317d6bf7817SEric Dumazet size_t size = entries * sizeof(struct neighbour *); 318d6bf7817SEric Dumazet struct neigh_hash_table *ret; 319d6bf7817SEric Dumazet struct neighbour **buckets; 3201da177e4SLinus Torvalds 321d6bf7817SEric Dumazet ret = kmalloc(sizeof(*ret), GFP_ATOMIC); 322d6bf7817SEric Dumazet if (!ret) 323d6bf7817SEric Dumazet return NULL; 324d6bf7817SEric Dumazet if (size <= PAGE_SIZE) 325d6bf7817SEric Dumazet buckets = kzalloc(size, GFP_ATOMIC); 326d6bf7817SEric Dumazet else 327d6bf7817SEric Dumazet buckets = (struct neighbour **) 328d6bf7817SEric Dumazet __get_free_pages(GFP_ATOMIC | __GFP_ZERO, 329d6bf7817SEric Dumazet get_order(size)); 330d6bf7817SEric Dumazet if (!buckets) { 331d6bf7817SEric Dumazet kfree(ret); 332d6bf7817SEric Dumazet return NULL; 3331da177e4SLinus Torvalds } 334767e97e1SEric Dumazet rcu_assign_pointer(ret->hash_buckets, buckets); 335d6bf7817SEric Dumazet ret->hash_mask = entries - 1; 336d6bf7817SEric Dumazet get_random_bytes(&ret->hash_rnd, sizeof(ret->hash_rnd)); 3371da177e4SLinus Torvalds return ret; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 340d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head) 3411da177e4SLinus Torvalds { 342d6bf7817SEric Dumazet struct neigh_hash_table *nht = container_of(head, 343d6bf7817SEric Dumazet struct neigh_hash_table, 344d6bf7817SEric Dumazet rcu); 345d6bf7817SEric Dumazet size_t size = (nht->hash_mask + 1) * sizeof(struct neighbour *); 346d6bf7817SEric Dumazet struct neighbour **buckets = nht->hash_buckets; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds if (size <= PAGE_SIZE) 349d6bf7817SEric Dumazet kfree(buckets); 3501da177e4SLinus Torvalds else 351d6bf7817SEric Dumazet free_pages((unsigned long)buckets, get_order(size)); 352d6bf7817SEric Dumazet kfree(nht); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 355d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, 356d6bf7817SEric Dumazet unsigned long new_entries) 3571da177e4SLinus Torvalds { 358d6bf7817SEric Dumazet unsigned int i, hash; 359d6bf7817SEric Dumazet struct neigh_hash_table *new_nht, *old_nht; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3621da177e4SLinus Torvalds 363c3609d51Svignesh babu BUG_ON(!is_power_of_2(new_entries)); 364d6bf7817SEric Dumazet old_nht = rcu_dereference_protected(tbl->nht, 365d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 366d6bf7817SEric Dumazet new_nht = neigh_hash_alloc(new_entries); 367d6bf7817SEric Dumazet if (!new_nht) 368d6bf7817SEric Dumazet return old_nht; 3691da177e4SLinus Torvalds 370d6bf7817SEric Dumazet for (i = 0; i <= old_nht->hash_mask; i++) { 3711da177e4SLinus Torvalds struct neighbour *n, *next; 3721da177e4SLinus Torvalds 373767e97e1SEric Dumazet for (n = rcu_dereference_protected(old_nht->hash_buckets[i], 374767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 375d6bf7817SEric Dumazet n != NULL; 376d6bf7817SEric Dumazet n = next) { 377d6bf7817SEric Dumazet hash = tbl->hash(n->primary_key, n->dev, 378d6bf7817SEric Dumazet new_nht->hash_rnd); 3791da177e4SLinus Torvalds 380d6bf7817SEric Dumazet hash &= new_nht->hash_mask; 381767e97e1SEric Dumazet next = rcu_dereference_protected(n->next, 382767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 3831da177e4SLinus Torvalds 384767e97e1SEric Dumazet rcu_assign_pointer(n->next, 385767e97e1SEric Dumazet rcu_dereference_protected( 386767e97e1SEric Dumazet new_nht->hash_buckets[hash], 387767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 388767e97e1SEric Dumazet rcu_assign_pointer(new_nht->hash_buckets[hash], n); 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 392d6bf7817SEric Dumazet rcu_assign_pointer(tbl->nht, new_nht); 393d6bf7817SEric Dumazet call_rcu(&old_nht->rcu, neigh_hash_free_rcu); 394d6bf7817SEric Dumazet return new_nht; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 3981da177e4SLinus Torvalds struct net_device *dev) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds struct neighbour *n; 4011da177e4SLinus Torvalds int key_len = tbl->key_len; 402bc4bf5f3SPavel Emelyanov u32 hash_val; 403d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4061da177e4SLinus Torvalds 407d6bf7817SEric Dumazet rcu_read_lock_bh(); 408d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 409d6bf7817SEric Dumazet hash_val = tbl->hash(pkey, dev, nht->hash_rnd) & nht->hash_mask; 410767e97e1SEric Dumazet 411767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 412767e97e1SEric Dumazet n != NULL; 413767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 4141da177e4SLinus Torvalds if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) { 415767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 416767e97e1SEric Dumazet n = NULL; 4171da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4181da177e4SLinus Torvalds break; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds } 421767e97e1SEric Dumazet 422d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4231da177e4SLinus Torvalds return n; 4241da177e4SLinus Torvalds } 4250a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup); 4261da177e4SLinus Torvalds 427426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 428426b5303SEric W. Biederman const void *pkey) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds struct neighbour *n; 4311da177e4SLinus Torvalds int key_len = tbl->key_len; 432bc4bf5f3SPavel Emelyanov u32 hash_val; 433d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4361da177e4SLinus Torvalds 437d6bf7817SEric Dumazet rcu_read_lock_bh(); 438d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 439d6bf7817SEric Dumazet hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) & nht->hash_mask; 440767e97e1SEric Dumazet 441767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 442767e97e1SEric Dumazet n != NULL; 443767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 444426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 445878628fbSYOSHIFUJI Hideaki net_eq(dev_net(n->dev), net)) { 446767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 447767e97e1SEric Dumazet n = NULL; 4481da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4491da177e4SLinus Torvalds break; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds } 452767e97e1SEric Dumazet 453d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4541da177e4SLinus Torvalds return n; 4551da177e4SLinus Torvalds } 4560a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev); 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey, 4591da177e4SLinus Torvalds struct net_device *dev) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds u32 hash_val; 4621da177e4SLinus Torvalds int key_len = tbl->key_len; 4631da177e4SLinus Torvalds int error; 4641da177e4SLinus Torvalds struct neighbour *n1, *rc, *n = neigh_alloc(tbl); 465d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds if (!n) { 4681da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4691da177e4SLinus Torvalds goto out; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4731da177e4SLinus Torvalds n->dev = dev; 4741da177e4SLinus Torvalds dev_hold(dev); 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* Protocol specific setup. */ 4771da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4781da177e4SLinus Torvalds rc = ERR_PTR(error); 4791da177e4SLinus Torvalds goto out_neigh_release; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* Device specific setup. */ 4831da177e4SLinus Torvalds if (n->parms->neigh_setup && 4841da177e4SLinus Torvalds (error = n->parms->neigh_setup(n)) < 0) { 4851da177e4SLinus Torvalds rc = ERR_PTR(error); 4861da177e4SLinus Torvalds goto out_neigh_release; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds n->confirmed = jiffies - (n->parms->base_reachable_time << 1); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 492d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 493d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 4941da177e4SLinus Torvalds 495d6bf7817SEric Dumazet if (atomic_read(&tbl->entries) > (nht->hash_mask + 1)) 496d6bf7817SEric Dumazet nht = neigh_hash_grow(tbl, (nht->hash_mask + 1) << 1); 4971da177e4SLinus Torvalds 498d6bf7817SEric Dumazet hash_val = tbl->hash(pkey, dev, nht->hash_rnd) & nht->hash_mask; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (n->parms->dead) { 5011da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 5021da177e4SLinus Torvalds goto out_tbl_unlock; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 505767e97e1SEric Dumazet for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val], 506767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 507767e97e1SEric Dumazet n1 != NULL; 508767e97e1SEric Dumazet n1 = rcu_dereference_protected(n1->next, 509767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) { 5101da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 5111da177e4SLinus Torvalds neigh_hold(n1); 5121da177e4SLinus Torvalds rc = n1; 5131da177e4SLinus Torvalds goto out_tbl_unlock; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds n->dead = 0; 5181da177e4SLinus Torvalds neigh_hold(n); 519767e97e1SEric Dumazet rcu_assign_pointer(n->next, 520767e97e1SEric Dumazet rcu_dereference_protected(nht->hash_buckets[hash_val], 521767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 522767e97e1SEric Dumazet rcu_assign_pointer(nht->hash_buckets[hash_val], n); 5231da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5241da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is created.\n", n); 5251da177e4SLinus Torvalds rc = n; 5261da177e4SLinus Torvalds out: 5271da177e4SLinus Torvalds return rc; 5281da177e4SLinus Torvalds out_tbl_unlock: 5291da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5301da177e4SLinus Torvalds out_neigh_release: 5311da177e4SLinus Torvalds neigh_release(n); 5321da177e4SLinus Torvalds goto out; 5331da177e4SLinus Torvalds } 5340a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_create); 5351da177e4SLinus Torvalds 536be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len) 537fa86d322SPavel Emelyanov { 538fa86d322SPavel Emelyanov u32 hash_val = *(u32 *)(pkey + key_len - 4); 539fa86d322SPavel Emelyanov hash_val ^= (hash_val >> 16); 540fa86d322SPavel Emelyanov hash_val ^= hash_val >> 8; 541fa86d322SPavel Emelyanov hash_val ^= hash_val >> 4; 542fa86d322SPavel Emelyanov hash_val &= PNEIGH_HASHMASK; 543be01d655SYOSHIFUJI Hideaki return hash_val; 544fa86d322SPavel Emelyanov } 545fa86d322SPavel Emelyanov 546be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, 547be01d655SYOSHIFUJI Hideaki struct net *net, 548be01d655SYOSHIFUJI Hideaki const void *pkey, 549be01d655SYOSHIFUJI Hideaki int key_len, 550be01d655SYOSHIFUJI Hideaki struct net_device *dev) 551be01d655SYOSHIFUJI Hideaki { 552be01d655SYOSHIFUJI Hideaki while (n) { 553be01d655SYOSHIFUJI Hideaki if (!memcmp(n->key, pkey, key_len) && 554be01d655SYOSHIFUJI Hideaki net_eq(pneigh_net(n), net) && 555be01d655SYOSHIFUJI Hideaki (n->dev == dev || !n->dev)) 556fa86d322SPavel Emelyanov return n; 557be01d655SYOSHIFUJI Hideaki n = n->next; 558be01d655SYOSHIFUJI Hideaki } 559be01d655SYOSHIFUJI Hideaki return NULL; 560be01d655SYOSHIFUJI Hideaki } 561be01d655SYOSHIFUJI Hideaki 562be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, 563be01d655SYOSHIFUJI Hideaki struct net *net, const void *pkey, struct net_device *dev) 564be01d655SYOSHIFUJI Hideaki { 565be01d655SYOSHIFUJI Hideaki int key_len = tbl->key_len; 566be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 567be01d655SYOSHIFUJI Hideaki 568be01d655SYOSHIFUJI Hideaki return __pneigh_lookup_1(tbl->phash_buckets[hash_val], 569be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 570fa86d322SPavel Emelyanov } 5710a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup); 572fa86d322SPavel Emelyanov 573426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 574426b5303SEric W. Biederman struct net *net, const void *pkey, 5751da177e4SLinus Torvalds struct net_device *dev, int creat) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds struct pneigh_entry *n; 5781da177e4SLinus Torvalds int key_len = tbl->key_len; 579be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 582be01d655SYOSHIFUJI Hideaki n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], 583be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 584be01d655SYOSHIFUJI Hideaki read_unlock_bh(&tbl->lock); 5851da177e4SLinus Torvalds 586be01d655SYOSHIFUJI Hideaki if (n || !creat) 5871da177e4SLinus Torvalds goto out; 5881da177e4SLinus Torvalds 5894ae28944SPavel Emelyanov ASSERT_RTNL(); 5904ae28944SPavel Emelyanov 5911da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 5921da177e4SLinus Torvalds if (!n) 5931da177e4SLinus Torvalds goto out; 5941da177e4SLinus Torvalds 595e42ea986SEric Dumazet write_pnet(&n->net, hold_net(net)); 5961da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 5971da177e4SLinus Torvalds n->dev = dev; 5981da177e4SLinus Torvalds if (dev) 5991da177e4SLinus Torvalds dev_hold(dev); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 6021da177e4SLinus Torvalds if (dev) 6031da177e4SLinus Torvalds dev_put(dev); 604da12f735SDenis V. Lunev release_net(net); 6051da177e4SLinus Torvalds kfree(n); 6061da177e4SLinus Torvalds n = NULL; 6071da177e4SLinus Torvalds goto out; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6111da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 6121da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 6131da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6141da177e4SLinus Torvalds out: 6151da177e4SLinus Torvalds return n; 6161da177e4SLinus Torvalds } 6170a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds 620426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 6211da177e4SLinus Torvalds struct net_device *dev) 6221da177e4SLinus Torvalds { 6231da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6241da177e4SLinus Torvalds int key_len = tbl->key_len; 625be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6281da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 6291da177e4SLinus Torvalds np = &n->next) { 630426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 631878628fbSYOSHIFUJI Hideaki net_eq(pneigh_net(n), net)) { 6321da177e4SLinus Torvalds *np = n->next; 6331da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6341da177e4SLinus Torvalds if (tbl->pdestructor) 6351da177e4SLinus Torvalds tbl->pdestructor(n); 6361da177e4SLinus Torvalds if (n->dev) 6371da177e4SLinus Torvalds dev_put(n->dev); 63857da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6391da177e4SLinus Torvalds kfree(n); 6401da177e4SLinus Torvalds return 0; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6441da177e4SLinus Torvalds return -ENOENT; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 6481da177e4SLinus Torvalds { 6491da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6501da177e4SLinus Torvalds u32 h; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 6531da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 6541da177e4SLinus Torvalds while ((n = *np) != NULL) { 6551da177e4SLinus Torvalds if (!dev || n->dev == dev) { 6561da177e4SLinus Torvalds *np = n->next; 6571da177e4SLinus Torvalds if (tbl->pdestructor) 6581da177e4SLinus Torvalds tbl->pdestructor(n); 6591da177e4SLinus Torvalds if (n->dev) 6601da177e4SLinus Torvalds dev_put(n->dev); 66157da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6621da177e4SLinus Torvalds kfree(n); 6631da177e4SLinus Torvalds continue; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds np = &n->next; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds return -ENOENT; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 67106f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 67206f0511dSDenis V. Lunev 67306f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 67406f0511dSDenis V. Lunev { 67506f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 67606f0511dSDenis V. Lunev neigh_parms_destroy(parms); 67706f0511dSDenis V. Lunev } 6781da177e4SLinus Torvalds 679767e97e1SEric Dumazet static void neigh_destroy_rcu(struct rcu_head *head) 680767e97e1SEric Dumazet { 681767e97e1SEric Dumazet struct neighbour *neigh = container_of(head, struct neighbour, rcu); 682767e97e1SEric Dumazet 683767e97e1SEric Dumazet kmem_cache_free(neigh->tbl->kmem_cachep, neigh); 684767e97e1SEric Dumazet } 6851da177e4SLinus Torvalds /* 6861da177e4SLinus Torvalds * neighbour must already be out of the table; 6871da177e4SLinus Torvalds * 6881da177e4SLinus Torvalds */ 6891da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds struct hh_cache *hh; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds if (!neigh->dead) { 6961da177e4SLinus Torvalds printk(KERN_WARNING 6971da177e4SLinus Torvalds "Destroying alive neighbour %p\n", neigh); 6981da177e4SLinus Torvalds dump_stack(); 6991da177e4SLinus Torvalds return; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 7031da177e4SLinus Torvalds printk(KERN_WARNING "Impossible event.\n"); 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds while ((hh = neigh->hh) != NULL) { 7061da177e4SLinus Torvalds neigh->hh = hh->hh_next; 7071da177e4SLinus Torvalds hh->hh_next = NULL; 7083644f0ceSStephen Hemminger 7093644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 7101da177e4SLinus Torvalds hh->hh_output = neigh_blackhole; 7113644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 71234d101ddSEric Dumazet hh_cache_put(hh); 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds dev_put(neigh->dev); 7181da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 723767e97e1SEric Dumazet call_rcu(&neigh->rcu, neigh_destroy_rcu); 7241da177e4SLinus Torvalds } 7250a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* Neighbour state is suspicious; 7281da177e4SLinus Torvalds disable fast path. 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds Called with write_locked neigh. 7311da177e4SLinus Torvalds */ 7321da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds struct hh_cache *hh; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds neigh->output = neigh->ops->output; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 7411da177e4SLinus Torvalds hh->hh_output = neigh->ops->output; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* Neighbour state is OK; 7451da177e4SLinus Torvalds enable fast path. 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds Called with write_locked neigh. 7481da177e4SLinus Torvalds */ 7491da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 7501da177e4SLinus Torvalds { 7511da177e4SLinus Torvalds struct hh_cache *hh; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is connected.\n", neigh); 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 7581da177e4SLinus Torvalds hh->hh_output = neigh->ops->hh_output; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 761e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work) 7621da177e4SLinus Torvalds { 763e4c4e448SEric Dumazet struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); 764767e97e1SEric Dumazet struct neighbour *n; 765767e97e1SEric Dumazet struct neighbour __rcu **np; 766e4c4e448SEric Dumazet unsigned int i; 767d6bf7817SEric Dumazet struct neigh_hash_table *nht; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 7701da177e4SLinus Torvalds 771e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 772d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 773d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 7771da177e4SLinus Torvalds */ 7781da177e4SLinus Torvalds 779e4c4e448SEric Dumazet if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { 7801da177e4SLinus Torvalds struct neigh_parms *p; 781e4c4e448SEric Dumazet tbl->last_rand = jiffies; 7821da177e4SLinus Torvalds for (p = &tbl->parms; p; p = p->next) 7831da177e4SLinus Torvalds p->reachable_time = 7841da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 787d6bf7817SEric Dumazet for (i = 0 ; i <= nht->hash_mask; i++) { 788d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 7891da177e4SLinus Torvalds 790767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 791767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 7921da177e4SLinus Torvalds unsigned int state; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds write_lock(&n->lock); 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds state = n->nud_state; 7971da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 7981da177e4SLinus Torvalds write_unlock(&n->lock); 7991da177e4SLinus Torvalds goto next_elt; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 8031da177e4SLinus Torvalds n->used = n->confirmed; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 8061da177e4SLinus Torvalds (state == NUD_FAILED || 807e4c4e448SEric Dumazet time_after(jiffies, n->used + n->parms->gc_staletime))) { 8081da177e4SLinus Torvalds *np = n->next; 8091da177e4SLinus Torvalds n->dead = 1; 8101da177e4SLinus Torvalds write_unlock(&n->lock); 8114f494554SThomas Graf neigh_cleanup_and_release(n); 8121da177e4SLinus Torvalds continue; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds write_unlock(&n->lock); 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds next_elt: 8171da177e4SLinus Torvalds np = &n->next; 8181da177e4SLinus Torvalds } 819e4c4e448SEric Dumazet /* 820e4c4e448SEric Dumazet * It's fine to release lock here, even if hash table 821e4c4e448SEric Dumazet * grows while we are preempted. 822e4c4e448SEric Dumazet */ 823e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 824e4c4e448SEric Dumazet cond_resched(); 825e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 826e4c4e448SEric Dumazet } 8271da177e4SLinus Torvalds /* Cycle through all hash buckets every base_reachable_time/2 ticks. 8281da177e4SLinus Torvalds * ARP entry timeouts range from 1/2 base_reachable_time to 3/2 8291da177e4SLinus Torvalds * base_reachable_time. 8301da177e4SLinus Torvalds */ 831e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, 832e4c4e448SEric Dumazet tbl->parms.base_reachable_time >> 1); 833e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 8371da177e4SLinus Torvalds { 8381da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 839a02cec21SEric Dumazet return (n->nud_state & NUD_PROBE) ? 8401da177e4SLinus Torvalds p->ucast_probes : 841a02cec21SEric Dumazet p->ucast_probes + p->app_probes + p->mcast_probes; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8445ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh) 8450a141509SEric Dumazet __releases(neigh->lock) 8460a141509SEric Dumazet __acquires(neigh->lock) 8475ef12d98STimo Teras { 8485ef12d98STimo Teras struct sk_buff *skb; 8495ef12d98STimo Teras 8505ef12d98STimo Teras NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 8515ef12d98STimo Teras NEIGH_PRINTK2("neigh %p is failed.\n", neigh); 8525ef12d98STimo Teras neigh->updated = jiffies; 8535ef12d98STimo Teras 8545ef12d98STimo Teras /* It is very thin place. report_unreachable is very complicated 8555ef12d98STimo Teras routine. Particularly, it can hit the same neighbour entry! 8565ef12d98STimo Teras 8575ef12d98STimo Teras So that, we try to be accurate and avoid dead loop. --ANK 8585ef12d98STimo Teras */ 8595ef12d98STimo Teras while (neigh->nud_state == NUD_FAILED && 8605ef12d98STimo Teras (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 8615ef12d98STimo Teras write_unlock(&neigh->lock); 8625ef12d98STimo Teras neigh->ops->error_report(neigh, skb); 8635ef12d98STimo Teras write_lock(&neigh->lock); 8645ef12d98STimo Teras } 8655ef12d98STimo Teras skb_queue_purge(&neigh->arp_queue); 8665ef12d98STimo Teras } 8675ef12d98STimo Teras 8681da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 8711da177e4SLinus Torvalds { 8721da177e4SLinus Torvalds unsigned long now, next; 8731da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 8741da177e4SLinus Torvalds unsigned state; 8751da177e4SLinus Torvalds int notify = 0; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds write_lock(&neigh->lock); 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds state = neigh->nud_state; 8801da177e4SLinus Torvalds now = jiffies; 8811da177e4SLinus Torvalds next = now + HZ; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds if (!(state & NUD_IN_TIMER)) { 8841da177e4SLinus Torvalds #ifndef CONFIG_SMP 8851da177e4SLinus Torvalds printk(KERN_WARNING "neigh: timer & !nud_in_timer\n"); 8861da177e4SLinus Torvalds #endif 8871da177e4SLinus Torvalds goto out; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 8911da177e4SLinus Torvalds if (time_before_eq(now, 8921da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 8931da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is still alive.\n", neigh); 8941da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 8951da177e4SLinus Torvalds } else if (time_before_eq(now, 8961da177e4SLinus Torvalds neigh->used + neigh->parms->delay_probe_time)) { 8971da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 8981da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 899955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9001da177e4SLinus Torvalds neigh_suspect(neigh); 9011da177e4SLinus Torvalds next = now + neigh->parms->delay_probe_time; 9021da177e4SLinus Torvalds } else { 9031da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 9041da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 905955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9061da177e4SLinus Torvalds neigh_suspect(neigh); 9078d71740cSTom Tucker notify = 1; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 9101da177e4SLinus Torvalds if (time_before_eq(now, 9111da177e4SLinus Torvalds neigh->confirmed + neigh->parms->delay_probe_time)) { 9121da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh); 9131da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 914955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9151da177e4SLinus Torvalds neigh_connect(neigh); 9168d71740cSTom Tucker notify = 1; 9171da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 9181da177e4SLinus Torvalds } else { 9191da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is probed.\n", neigh); 9201da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 921955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9221da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 9231da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds } else { 9261da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 9271da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 9311da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 9321da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 9331da177e4SLinus Torvalds notify = 1; 9345ef12d98STimo Teras neigh_invalidate(neigh); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 9381da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 9391da177e4SLinus Torvalds next = jiffies + HZ/2; 9406fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 9416fb9974fSHerbert Xu neigh_hold(neigh); 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 9441da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&neigh->arp_queue); 9459ff56607SDavid S. Miller /* keep skb alive even if arp_queue overflows */ 9469ff56607SDavid S. Miller if (skb) 9477e36763bSFrank Blaschka skb = skb_copy(skb, GFP_ATOMIC); 9489ff56607SDavid S. Miller write_unlock(&neigh->lock); 9491da177e4SLinus Torvalds neigh->ops->solicit(neigh, skb); 9501da177e4SLinus Torvalds atomic_inc(&neigh->probes); 9519ff56607SDavid S. Miller kfree_skb(skb); 9529ff56607SDavid S. Miller } else { 9531da177e4SLinus Torvalds out: 9541da177e4SLinus Torvalds write_unlock(&neigh->lock); 9559ff56607SDavid S. Miller } 9561da177e4SLinus Torvalds 957d961db35SThomas Graf if (notify) 958d961db35SThomas Graf neigh_update_notify(neigh); 959d961db35SThomas Graf 9601da177e4SLinus Torvalds neigh_release(neigh); 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds int rc; 9661da177e4SLinus Torvalds unsigned long now; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds rc = 0; 9711da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 9721da177e4SLinus Torvalds goto out_unlock_bh; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds now = jiffies; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 9771da177e4SLinus Torvalds if (neigh->parms->mcast_probes + neigh->parms->app_probes) { 9781da177e4SLinus Torvalds atomic_set(&neigh->probes, neigh->parms->ucast_probes); 9791da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 980955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 981667347f1SDavid S. Miller neigh_add_timer(neigh, now + 1); 9821da177e4SLinus Torvalds } else { 9831da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 984955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9851da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds kfree_skb(skb); 9881da177e4SLinus Torvalds return 1; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 9911da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 9921da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 993955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 994667347f1SDavid S. Miller neigh_add_timer(neigh, 995667347f1SDavid S. Miller jiffies + neigh->parms->delay_probe_time); 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 9991da177e4SLinus Torvalds if (skb) { 10001da177e4SLinus Torvalds if (skb_queue_len(&neigh->arp_queue) >= 10011da177e4SLinus Torvalds neigh->parms->queue_len) { 10021da177e4SLinus Torvalds struct sk_buff *buff; 1003f72051b0SDavid S. Miller buff = __skb_dequeue(&neigh->arp_queue); 10041da177e4SLinus Torvalds kfree_skb(buff); 10059a6d276eSNeil Horman NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); 10061da177e4SLinus Torvalds } 1007a4731138SEric Dumazet skb_dst_force(skb); 10081da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds rc = 1; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds out_unlock_bh: 10131da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 10141da177e4SLinus Torvalds return rc; 10151da177e4SLinus Torvalds } 10160a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send); 10171da177e4SLinus Torvalds 1018e92b43a3SStephen Hemminger static void neigh_update_hhs(struct neighbour *neigh) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds struct hh_cache *hh; 10213b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 102291a72a70SDoug Kehn = NULL; 102391a72a70SDoug Kehn 102491a72a70SDoug Kehn if (neigh->dev->header_ops) 102591a72a70SDoug Kehn update = neigh->dev->header_ops->cache_update; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds if (update) { 10281da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) { 10293644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 10301da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 10313644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /* Generic update routine. 10391da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 10401da177e4SLinus Torvalds -- new is new state. 10411da177e4SLinus Torvalds -- flags 10421da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 10431da177e4SLinus Torvalds if it is different. 10441da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 10451da177e4SLinus Torvalds lladdr instead of overriding it 10461da177e4SLinus Torvalds if it is different. 10471da177e4SLinus Torvalds It also allows to retain current state 10481da177e4SLinus Torvalds if lladdr is unchanged. 10491da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 10521da177e4SLinus Torvalds NTF_ROUTER flag. 10531da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 10541da177e4SLinus Torvalds a router. 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 10571da177e4SLinus Torvalds */ 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 10601da177e4SLinus Torvalds u32 flags) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds u8 old; 10631da177e4SLinus Torvalds int err; 10641da177e4SLinus Torvalds int notify = 0; 10651da177e4SLinus Torvalds struct net_device *dev; 10661da177e4SLinus Torvalds int update_isrouter = 0; 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds dev = neigh->dev; 10711da177e4SLinus Torvalds old = neigh->nud_state; 10721da177e4SLinus Torvalds err = -EPERM; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 10751da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 10761da177e4SLinus Torvalds goto out; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 10791da177e4SLinus Torvalds neigh_del_timer(neigh); 10801da177e4SLinus Torvalds if (old & NUD_CONNECTED) 10811da177e4SLinus Torvalds neigh_suspect(neigh); 10821da177e4SLinus Torvalds neigh->nud_state = new; 10831da177e4SLinus Torvalds err = 0; 10841da177e4SLinus Torvalds notify = old & NUD_VALID; 10855ef12d98STimo Teras if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && 10865ef12d98STimo Teras (new & NUD_FAILED)) { 10875ef12d98STimo Teras neigh_invalidate(neigh); 10885ef12d98STimo Teras notify = 1; 10895ef12d98STimo Teras } 10901da177e4SLinus Torvalds goto out; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 10941da177e4SLinus Torvalds if (!dev->addr_len) { 10951da177e4SLinus Torvalds /* First case: device needs no address. */ 10961da177e4SLinus Torvalds lladdr = neigh->ha; 10971da177e4SLinus Torvalds } else if (lladdr) { 10981da177e4SLinus Torvalds /* The second case: if something is already cached 10991da177e4SLinus Torvalds and a new address is proposed: 11001da177e4SLinus Torvalds - compare new & old 11011da177e4SLinus Torvalds - if they are different, check override flag 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds if ((old & NUD_VALID) && 11041da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 11051da177e4SLinus Torvalds lladdr = neigh->ha; 11061da177e4SLinus Torvalds } else { 11071da177e4SLinus Torvalds /* No address is supplied; if we know something, 11081da177e4SLinus Torvalds use it, otherwise discard the request. 11091da177e4SLinus Torvalds */ 11101da177e4SLinus Torvalds err = -EINVAL; 11111da177e4SLinus Torvalds if (!(old & NUD_VALID)) 11121da177e4SLinus Torvalds goto out; 11131da177e4SLinus Torvalds lladdr = neigh->ha; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11171da177e4SLinus Torvalds neigh->confirmed = jiffies; 11181da177e4SLinus Torvalds neigh->updated = jiffies; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /* If entry was valid and address is not changed, 11211da177e4SLinus Torvalds do not change entry state, if new one is STALE. 11221da177e4SLinus Torvalds */ 11231da177e4SLinus Torvalds err = 0; 11241da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 11251da177e4SLinus Torvalds if (old & NUD_VALID) { 11261da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 11271da177e4SLinus Torvalds update_isrouter = 0; 11281da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 11291da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 11301da177e4SLinus Torvalds lladdr = neigh->ha; 11311da177e4SLinus Torvalds new = NUD_STALE; 11321da177e4SLinus Torvalds } else 11331da177e4SLinus Torvalds goto out; 11341da177e4SLinus Torvalds } else { 11351da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 11361da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 11371da177e4SLinus Torvalds (old & NUD_CONNECTED)) 11381da177e4SLinus Torvalds ) 11391da177e4SLinus Torvalds new = old; 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds if (new != old) { 11441da177e4SLinus Torvalds neigh_del_timer(neigh); 1145a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1146667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 11471da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1148667347f1SDavid S. Miller neigh->parms->reachable_time : 1149667347f1SDavid S. Miller 0))); 11501da177e4SLinus Torvalds neigh->nud_state = new; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds if (lladdr != neigh->ha) { 11541da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 11551da177e4SLinus Torvalds neigh_update_hhs(neigh); 11561da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 11571da177e4SLinus Torvalds neigh->confirmed = jiffies - 11581da177e4SLinus Torvalds (neigh->parms->base_reachable_time << 1); 11591da177e4SLinus Torvalds notify = 1; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds if (new == old) 11621da177e4SLinus Torvalds goto out; 11631da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11641da177e4SLinus Torvalds neigh_connect(neigh); 11651da177e4SLinus Torvalds else 11661da177e4SLinus Torvalds neigh_suspect(neigh); 11671da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 11681da177e4SLinus Torvalds struct sk_buff *skb; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 11731da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 11741da177e4SLinus Torvalds struct neighbour *n1 = neigh; 11751da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 11761da177e4SLinus Torvalds /* On shaper/eql skb->dst->neighbour != neigh :( */ 1177adf30907SEric Dumazet if (skb_dst(skb) && skb_dst(skb)->neighbour) 1178adf30907SEric Dumazet n1 = skb_dst(skb)->neighbour; 11791da177e4SLinus Torvalds n1->output(skb); 11801da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds out: 11851da177e4SLinus Torvalds if (update_isrouter) { 11861da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 11871da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 11881da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 11918d71740cSTom Tucker 11928d71740cSTom Tucker if (notify) 1193d961db35SThomas Graf neigh_update_notify(neigh); 1194d961db35SThomas Graf 11951da177e4SLinus Torvalds return err; 11961da177e4SLinus Torvalds } 11970a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update); 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 12001da177e4SLinus Torvalds u8 *lladdr, void *saddr, 12011da177e4SLinus Torvalds struct net_device *dev) 12021da177e4SLinus Torvalds { 12031da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 12041da177e4SLinus Torvalds lladdr || !dev->addr_len); 12051da177e4SLinus Torvalds if (neigh) 12061da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 12071da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 12081da177e4SLinus Torvalds return neigh; 12091da177e4SLinus Torvalds } 12100a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns); 12111da177e4SLinus Torvalds 121234d101ddSEric Dumazet static inline bool neigh_hh_lookup(struct neighbour *n, struct dst_entry *dst, 121334d101ddSEric Dumazet __be16 protocol) 121434d101ddSEric Dumazet { 121534d101ddSEric Dumazet struct hh_cache *hh; 121634d101ddSEric Dumazet 121734d101ddSEric Dumazet for (hh = n->hh; hh; hh = hh->hh_next) { 121834d101ddSEric Dumazet if (hh->hh_type == protocol) { 121934d101ddSEric Dumazet atomic_inc(&hh->hh_refcnt); 122034d101ddSEric Dumazet if (unlikely(cmpxchg(&dst->hh, NULL, hh) != NULL)) 122134d101ddSEric Dumazet hh_cache_put(hh); 122234d101ddSEric Dumazet return true; 122334d101ddSEric Dumazet } 122434d101ddSEric Dumazet } 122534d101ddSEric Dumazet return false; 122634d101ddSEric Dumazet } 122734d101ddSEric Dumazet 122834d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */ 12291da177e4SLinus Torvalds static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, 1230d77072ecSAl Viro __be16 protocol) 12311da177e4SLinus Torvalds { 12321da177e4SLinus Torvalds struct hh_cache *hh; 12331da177e4SLinus Torvalds struct net_device *dev = dst->dev; 12341da177e4SLinus Torvalds 123534d101ddSEric Dumazet if (likely(neigh_hh_lookup(n, dst, protocol))) 123634d101ddSEric Dumazet return; 12371da177e4SLinus Torvalds 123834d101ddSEric Dumazet /* slow path */ 123934d101ddSEric Dumazet hh = kzalloc(sizeof(*hh), GFP_ATOMIC); 124034d101ddSEric Dumazet if (!hh) 124134d101ddSEric Dumazet return; 124234d101ddSEric Dumazet 12433644f0ceSStephen Hemminger seqlock_init(&hh->hh_lock); 12441da177e4SLinus Torvalds hh->hh_type = protocol; 124534d101ddSEric Dumazet atomic_set(&hh->hh_refcnt, 2); 12463b04dddeSStephen Hemminger 12473b04dddeSStephen Hemminger if (dev->header_ops->cache(n, hh)) { 12481da177e4SLinus Torvalds kfree(hh); 124934d101ddSEric Dumazet return; 125034d101ddSEric Dumazet } 125134d101ddSEric Dumazet read_unlock(&n->lock); 125234d101ddSEric Dumazet write_lock(&n->lock); 125334d101ddSEric Dumazet 125434d101ddSEric Dumazet /* must check if another thread already did the insert */ 125534d101ddSEric Dumazet if (neigh_hh_lookup(n, dst, protocol)) { 125634d101ddSEric Dumazet kfree(hh); 125734d101ddSEric Dumazet goto end; 125834d101ddSEric Dumazet } 125934d101ddSEric Dumazet 12601da177e4SLinus Torvalds if (n->nud_state & NUD_CONNECTED) 12611da177e4SLinus Torvalds hh->hh_output = n->ops->hh_output; 12621da177e4SLinus Torvalds else 12631da177e4SLinus Torvalds hh->hh_output = n->ops->output; 126434d101ddSEric Dumazet 126534d101ddSEric Dumazet hh->hh_next = n->hh; 126634d101ddSEric Dumazet n->hh = hh; 126734d101ddSEric Dumazet 126834d101ddSEric Dumazet if (unlikely(cmpxchg(&dst->hh, NULL, hh) != NULL)) 126934d101ddSEric Dumazet hh_cache_put(hh); 127034d101ddSEric Dumazet end: 127134d101ddSEric Dumazet write_unlock(&n->lock); 127234d101ddSEric Dumazet read_lock(&n->lock); 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit 1276767e97e1SEric Dumazet * worked, f.e. if you want to override normal output path (eql, shaper), 1277767e97e1SEric Dumazet * but resolution is not made yet. 12781da177e4SLinus Torvalds */ 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds int neigh_compat_output(struct sk_buff *skb) 12811da177e4SLinus Torvalds { 12821da177e4SLinus Torvalds struct net_device *dev = skb->dev; 12831da177e4SLinus Torvalds 1284bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12851da177e4SLinus Torvalds 12860c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 12871da177e4SLinus Torvalds skb->len) < 0 && 12883b04dddeSStephen Hemminger dev->header_ops->rebuild(skb)) 12891da177e4SLinus Torvalds return 0; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds return dev_queue_xmit(skb); 12921da177e4SLinus Torvalds } 12930a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output); 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds /* Slow and careful. */ 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds int neigh_resolve_output(struct sk_buff *skb) 12981da177e4SLinus Torvalds { 1299adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 13001da177e4SLinus Torvalds struct neighbour *neigh; 13011da177e4SLinus Torvalds int rc = 0; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds if (!dst || !(neigh = dst->neighbour)) 13041da177e4SLinus Torvalds goto discard; 13051da177e4SLinus Torvalds 1306bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 13091da177e4SLinus Torvalds int err; 13101da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 131134d101ddSEric Dumazet 131234d101ddSEric Dumazet read_lock_bh(&neigh->lock); 1313c7d4426aSEric Dumazet if (dev->header_ops->cache && 1314c7d4426aSEric Dumazet !dst->hh && 131534d101ddSEric Dumazet !(dst->flags & DST_NOCACHE)) 13161da177e4SLinus Torvalds neigh_hh_init(neigh, dst, dst->ops->protocol); 131734d101ddSEric Dumazet 13180c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13191da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13201da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 132134d101ddSEric Dumazet 13221da177e4SLinus Torvalds if (err >= 0) 13231da177e4SLinus Torvalds rc = neigh->ops->queue_xmit(skb); 13241da177e4SLinus Torvalds else 13251da177e4SLinus Torvalds goto out_kfree_skb; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds out: 13281da177e4SLinus Torvalds return rc; 13291da177e4SLinus Torvalds discard: 13301da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", 13311da177e4SLinus Torvalds dst, dst ? dst->neighbour : NULL); 13321da177e4SLinus Torvalds out_kfree_skb: 13331da177e4SLinus Torvalds rc = -EINVAL; 13341da177e4SLinus Torvalds kfree_skb(skb); 13351da177e4SLinus Torvalds goto out; 13361da177e4SLinus Torvalds } 13370a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output); 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds /* As fast as possible without hh cache */ 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds int neigh_connected_output(struct sk_buff *skb) 13421da177e4SLinus Torvalds { 13431da177e4SLinus Torvalds int err; 1344adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 13451da177e4SLinus Torvalds struct neighbour *neigh = dst->neighbour; 13461da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 13471da177e4SLinus Torvalds 1348bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 13510c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13521da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13531da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 13541da177e4SLinus Torvalds if (err >= 0) 13551da177e4SLinus Torvalds err = neigh->ops->queue_xmit(skb); 13561da177e4SLinus Torvalds else { 13571da177e4SLinus Torvalds err = -EINVAL; 13581da177e4SLinus Torvalds kfree_skb(skb); 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds return err; 13611da177e4SLinus Torvalds } 13620a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output); 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 13651da177e4SLinus Torvalds { 13661da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 13671da177e4SLinus Torvalds long sched_next = 0; 13681da177e4SLinus Torvalds unsigned long now = jiffies; 1369f72051b0SDavid S. Miller struct sk_buff *skb, *n; 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13721da177e4SLinus Torvalds 1373f72051b0SDavid S. Miller skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { 1374f72051b0SDavid S. Miller long tdif = NEIGH_CB(skb)->sched_next - now; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds if (tdif <= 0) { 1377f72051b0SDavid S. Miller struct net_device *dev = skb->dev; 1378f72051b0SDavid S. Miller __skb_unlink(skb, &tbl->proxy_queue); 13791da177e4SLinus Torvalds if (tbl->proxy_redo && netif_running(dev)) 1380f72051b0SDavid S. Miller tbl->proxy_redo(skb); 13811da177e4SLinus Torvalds else 1382f72051b0SDavid S. Miller kfree_skb(skb); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds dev_put(dev); 13851da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 13861da177e4SLinus Torvalds sched_next = tdif; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 13891da177e4SLinus Torvalds if (sched_next) 13901da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 13911da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 13951da177e4SLinus Torvalds struct sk_buff *skb) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds unsigned long now = jiffies; 13981da177e4SLinus Torvalds unsigned long sched_next = now + (net_random() % p->proxy_delay); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds if (tbl->proxy_queue.qlen > p->proxy_qlen) { 14011da177e4SLinus Torvalds kfree_skb(skb); 14021da177e4SLinus Torvalds return; 14031da177e4SLinus Torvalds } 1404a61bbcf2SPatrick McHardy 1405a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1406a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 14091da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 14101da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 14111da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 14121da177e4SLinus Torvalds } 1413adf30907SEric Dumazet skb_dst_drop(skb); 14141da177e4SLinus Torvalds dev_hold(skb->dev); 14151da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 14161da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 14171da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 14181da177e4SLinus Torvalds } 14190a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue); 14201da177e4SLinus Torvalds 142197fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, 1422426b5303SEric W. Biederman struct net *net, int ifindex) 1423426b5303SEric W. Biederman { 1424426b5303SEric W. Biederman struct neigh_parms *p; 1425426b5303SEric W. Biederman 1426426b5303SEric W. Biederman for (p = &tbl->parms; p; p = p->next) { 1427878628fbSYOSHIFUJI Hideaki if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || 1428426b5303SEric W. Biederman (!p->dev && !ifindex)) 1429426b5303SEric W. Biederman return p; 1430426b5303SEric W. Biederman } 1431426b5303SEric W. Biederman 1432426b5303SEric W. Biederman return NULL; 1433426b5303SEric W. Biederman } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 14361da177e4SLinus Torvalds struct neigh_table *tbl) 14371da177e4SLinus Torvalds { 1438426b5303SEric W. Biederman struct neigh_parms *p, *ref; 143900829823SStephen Hemminger struct net *net = dev_net(dev); 144000829823SStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 14411da177e4SLinus Torvalds 144297fd5bc7STobias Klauser ref = lookup_neigh_parms(tbl, net, 0); 1443426b5303SEric W. Biederman if (!ref) 1444426b5303SEric W. Biederman return NULL; 1445426b5303SEric W. Biederman 1446426b5303SEric W. Biederman p = kmemdup(ref, sizeof(*p), GFP_KERNEL); 14471da177e4SLinus Torvalds if (p) { 14481da177e4SLinus Torvalds p->tbl = tbl; 14491da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 14501da177e4SLinus Torvalds p->reachable_time = 14511da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 1452486b51d3SDenis V. Lunev 145300829823SStephen Hemminger if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 14541da177e4SLinus Torvalds kfree(p); 14551da177e4SLinus Torvalds return NULL; 14561da177e4SLinus Torvalds } 1457c7fb64dbSThomas Graf 1458c7fb64dbSThomas Graf dev_hold(dev); 1459c7fb64dbSThomas Graf p->dev = dev; 1460e42ea986SEric Dumazet write_pnet(&p->net, hold_net(net)); 14611da177e4SLinus Torvalds p->sysctl_table = NULL; 14621da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14631da177e4SLinus Torvalds p->next = tbl->parms.next; 14641da177e4SLinus Torvalds tbl->parms.next = p; 14651da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds return p; 14681da177e4SLinus Torvalds } 14690a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc); 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 14721da177e4SLinus Torvalds { 14731da177e4SLinus Torvalds struct neigh_parms *parms = 14741da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds neigh_parms_put(parms); 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 14801da177e4SLinus Torvalds { 14811da177e4SLinus Torvalds struct neigh_parms **p; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 14841da177e4SLinus Torvalds return; 14851da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 14861da177e4SLinus Torvalds for (p = &tbl->parms.next; *p; p = &(*p)->next) { 14871da177e4SLinus Torvalds if (*p == parms) { 14881da177e4SLinus Torvalds *p = parms->next; 14891da177e4SLinus Torvalds parms->dead = 1; 14901da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1491cecbb639SDavid S. Miller if (parms->dev) 1492cecbb639SDavid S. Miller dev_put(parms->dev); 14931da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 14941da177e4SLinus Torvalds return; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14981da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_parms_release: not found\n"); 14991da177e4SLinus Torvalds } 15000a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release); 15011da177e4SLinus Torvalds 150206f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 15031da177e4SLinus Torvalds { 150457da52c1SYOSHIFUJI Hideaki release_net(neigh_parms_net(parms)); 15051da177e4SLinus Torvalds kfree(parms); 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds 1508c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1509c2ecba71SPavel Emelianov 1510bd89efc5SSimon Kelley void neigh_table_init_no_netlink(struct neigh_table *tbl) 15111da177e4SLinus Torvalds { 15121da177e4SLinus Torvalds unsigned long now = jiffies; 15131da177e4SLinus Torvalds unsigned long phsize; 15141da177e4SLinus Torvalds 1515e42ea986SEric Dumazet write_pnet(&tbl->parms.net, &init_net); 15161da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 15171da177e4SLinus Torvalds tbl->parms.reachable_time = 15181da177e4SLinus Torvalds neigh_rand_reach_time(tbl->parms.base_reachable_time); 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds if (!tbl->kmem_cachep) 1521e5d679f3SAlexey Dobriyan tbl->kmem_cachep = 1522e5d679f3SAlexey Dobriyan kmem_cache_create(tbl->id, tbl->entry_size, 0, 1523e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 152420c2df83SPaul Mundt NULL); 15251da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 15261da177e4SLinus Torvalds if (!tbl->stats) 15271da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15309b739ba5SAlexey Dobriyan if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat, 15319b739ba5SAlexey Dobriyan &neigh_stat_seq_fops, tbl)) 15321da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 15331da177e4SLinus Torvalds #endif 15341da177e4SLinus Torvalds 1535d6bf7817SEric Dumazet tbl->nht = neigh_hash_alloc(8); 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 153877d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 15391da177e4SLinus Torvalds 1540d6bf7817SEric Dumazet if (!tbl->nht || !tbl->phash_buckets) 15411da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1544e4c4e448SEric Dumazet INIT_DELAYED_WORK_DEFERRABLE(&tbl->gc_work, neigh_periodic_work); 1545e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time); 1546b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1547c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1548c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds tbl->last_flush = now; 15511da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1552bd89efc5SSimon Kelley } 15530a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init_no_netlink); 1554bd89efc5SSimon Kelley 1555bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl) 1556bd89efc5SSimon Kelley { 1557bd89efc5SSimon Kelley struct neigh_table *tmp; 1558bd89efc5SSimon Kelley 1559bd89efc5SSimon Kelley neigh_table_init_no_netlink(tbl); 15601da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 1561bd89efc5SSimon Kelley for (tmp = neigh_tables; tmp; tmp = tmp->next) { 1562bd89efc5SSimon Kelley if (tmp->family == tbl->family) 1563bd89efc5SSimon Kelley break; 1564bd89efc5SSimon Kelley } 15651da177e4SLinus Torvalds tbl->next = neigh_tables; 15661da177e4SLinus Torvalds neigh_tables = tbl; 15671da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 1568bd89efc5SSimon Kelley 1569bd89efc5SSimon Kelley if (unlikely(tmp)) { 1570bd89efc5SSimon Kelley printk(KERN_ERR "NEIGH: Registering multiple tables for " 1571bd89efc5SSimon Kelley "family %d\n", tbl->family); 1572bd89efc5SSimon Kelley dump_stack(); 1573bd89efc5SSimon Kelley } 15741da177e4SLinus Torvalds } 15750a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init); 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl) 15781da177e4SLinus Torvalds { 15791da177e4SLinus Torvalds struct neigh_table **tp; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 1582e4c4e448SEric Dumazet cancel_delayed_work(&tbl->gc_work); 1583e4c4e448SEric Dumazet flush_scheduled_work(); 15841da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 15851da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 15861da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 15871da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 15881da177e4SLinus Torvalds printk(KERN_CRIT "neighbour leakage\n"); 15891da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 15901da177e4SLinus Torvalds for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { 15911da177e4SLinus Torvalds if (*tp == tbl) { 15921da177e4SLinus Torvalds *tp = tbl->next; 15931da177e4SLinus Torvalds break; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 15971da177e4SLinus Torvalds 1598d6bf7817SEric Dumazet call_rcu(&tbl->nht->rcu, neigh_hash_free_rcu); 1599d6bf7817SEric Dumazet tbl->nht = NULL; 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds kfree(tbl->phash_buckets); 16021da177e4SLinus Torvalds tbl->phash_buckets = NULL; 16031da177e4SLinus Torvalds 16043f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 16053f192b5cSAlexey Dobriyan 16063fcde74bSKirill Korotaev free_percpu(tbl->stats); 16073fcde74bSKirill Korotaev tbl->stats = NULL; 16083fcde74bSKirill Korotaev 1609bfb85c9fSRandy Dunlap kmem_cache_destroy(tbl->kmem_cachep); 1610bfb85c9fSRandy Dunlap tbl->kmem_cachep = NULL; 1611bfb85c9fSRandy Dunlap 16121da177e4SLinus Torvalds return 0; 16131da177e4SLinus Torvalds } 16140a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear); 16151da177e4SLinus Torvalds 1616c8822a4eSThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 16171da177e4SLinus Torvalds { 16183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1619a14a49d2SThomas Graf struct ndmsg *ndm; 1620a14a49d2SThomas Graf struct nlattr *dst_attr; 16211da177e4SLinus Torvalds struct neigh_table *tbl; 16221da177e4SLinus Torvalds struct net_device *dev = NULL; 1623a14a49d2SThomas Graf int err = -EINVAL; 16241da177e4SLinus Torvalds 1625110b2499SEric Dumazet ASSERT_RTNL(); 1626a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 16271da177e4SLinus Torvalds goto out; 16281da177e4SLinus Torvalds 1629a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1630a14a49d2SThomas Graf if (dst_attr == NULL) 1631a14a49d2SThomas Graf goto out; 1632a14a49d2SThomas Graf 1633a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1634a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1635110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 1636a14a49d2SThomas Graf if (dev == NULL) { 1637a14a49d2SThomas Graf err = -ENODEV; 1638a14a49d2SThomas Graf goto out; 1639a14a49d2SThomas Graf } 1640a14a49d2SThomas Graf } 1641a14a49d2SThomas Graf 16421da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 16431da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1644a14a49d2SThomas Graf struct neighbour *neigh; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 16471da177e4SLinus Torvalds continue; 16481da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16491da177e4SLinus Torvalds 1650a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 1651110b2499SEric Dumazet goto out; 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1654426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 1655110b2499SEric Dumazet goto out; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 1658a14a49d2SThomas Graf if (dev == NULL) 1659110b2499SEric Dumazet goto out; 16601da177e4SLinus Torvalds 1661a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1662a14a49d2SThomas Graf if (neigh == NULL) { 1663a14a49d2SThomas Graf err = -ENOENT; 1664110b2499SEric Dumazet goto out; 1665a14a49d2SThomas Graf } 1666a14a49d2SThomas Graf 1667a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 16681da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 16691da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1670a14a49d2SThomas Graf neigh_release(neigh); 1671110b2499SEric Dumazet goto out; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 1674a14a49d2SThomas Graf err = -EAFNOSUPPORT; 1675a14a49d2SThomas Graf 16761da177e4SLinus Torvalds out: 16771da177e4SLinus Torvalds return err; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 1680c8822a4eSThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 16811da177e4SLinus Torvalds { 16823b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 16835208debdSThomas Graf struct ndmsg *ndm; 16845208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 16851da177e4SLinus Torvalds struct neigh_table *tbl; 16861da177e4SLinus Torvalds struct net_device *dev = NULL; 16875208debdSThomas Graf int err; 16881da177e4SLinus Torvalds 1689110b2499SEric Dumazet ASSERT_RTNL(); 16905208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 16915208debdSThomas Graf if (err < 0) 16921da177e4SLinus Torvalds goto out; 16931da177e4SLinus Torvalds 16945208debdSThomas Graf err = -EINVAL; 16955208debdSThomas Graf if (tb[NDA_DST] == NULL) 16965208debdSThomas Graf goto out; 16975208debdSThomas Graf 16985208debdSThomas Graf ndm = nlmsg_data(nlh); 16995208debdSThomas Graf if (ndm->ndm_ifindex) { 1700110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 17015208debdSThomas Graf if (dev == NULL) { 17025208debdSThomas Graf err = -ENODEV; 17035208debdSThomas Graf goto out; 17045208debdSThomas Graf } 17055208debdSThomas Graf 17065208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 1707110b2499SEric Dumazet goto out; 17085208debdSThomas Graf } 17095208debdSThomas Graf 17101da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 17111da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 17125208debdSThomas Graf int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 17135208debdSThomas Graf struct neighbour *neigh; 17145208debdSThomas Graf void *dst, *lladdr; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 17171da177e4SLinus Torvalds continue; 17181da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17191da177e4SLinus Torvalds 17205208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 1721110b2499SEric Dumazet goto out; 17225208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 17235208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 172662dd9318SVille Nuorvala struct pneigh_entry *pn; 172762dd9318SVille Nuorvala 17285208debdSThomas Graf err = -ENOBUFS; 1729426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 173062dd9318SVille Nuorvala if (pn) { 173162dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 173262dd9318SVille Nuorvala err = 0; 173362dd9318SVille Nuorvala } 1734110b2499SEric Dumazet goto out; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17375208debdSThomas Graf if (dev == NULL) 1738110b2499SEric Dumazet goto out; 17391da177e4SLinus Torvalds 17405208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 17415208debdSThomas Graf if (neigh == NULL) { 17425208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 17431da177e4SLinus Torvalds err = -ENOENT; 1744110b2499SEric Dumazet goto out; 17455208debdSThomas Graf } 17465208debdSThomas Graf 17475208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 17485208debdSThomas Graf if (IS_ERR(neigh)) { 17495208debdSThomas Graf err = PTR_ERR(neigh); 1750110b2499SEric Dumazet goto out; 17511da177e4SLinus Torvalds } 17525208debdSThomas Graf } else { 17535208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 17545208debdSThomas Graf err = -EEXIST; 17555208debdSThomas Graf neigh_release(neigh); 1756110b2499SEric Dumazet goto out; 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds 17595208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 17605208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 17615208debdSThomas Graf } 17621da177e4SLinus Torvalds 17630c5c2d30SEric Biederman if (ndm->ndm_flags & NTF_USE) { 17640c5c2d30SEric Biederman neigh_event_send(neigh, NULL); 17650c5c2d30SEric Biederman err = 0; 17660c5c2d30SEric Biederman } else 17675208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 17685208debdSThomas Graf neigh_release(neigh); 1769110b2499SEric Dumazet goto out; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 17735208debdSThomas Graf err = -EAFNOSUPPORT; 17741da177e4SLinus Torvalds out: 17751da177e4SLinus Torvalds return err; 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 1778c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1779c7fb64dbSThomas Graf { 1780ca860fb3SThomas Graf struct nlattr *nest; 1781e386c6ebSThomas Graf 1782ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1783ca860fb3SThomas Graf if (nest == NULL) 1784ca860fb3SThomas Graf return -ENOBUFS; 1785c7fb64dbSThomas Graf 1786c7fb64dbSThomas Graf if (parms->dev) 1787ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex); 1788c7fb64dbSThomas Graf 1789ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)); 1790ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len); 1791ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen); 1792ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes); 1793ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes); 1794ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes); 1795ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time); 1796ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME, 1797c7fb64dbSThomas Graf parms->base_reachable_time); 1798ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime); 1799ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time); 1800ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time); 1801ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay); 1802ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay); 1803ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime); 1804c7fb64dbSThomas Graf 1805ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1806c7fb64dbSThomas Graf 1807ca860fb3SThomas Graf nla_put_failure: 1808bc3ed28cSThomas Graf nla_nest_cancel(skb, nest); 1809bc3ed28cSThomas Graf return -EMSGSIZE; 1810c7fb64dbSThomas Graf } 1811c7fb64dbSThomas Graf 1812ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1813ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1814c7fb64dbSThomas Graf { 1815c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1816c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1817c7fb64dbSThomas Graf 1818ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1819ca860fb3SThomas Graf if (nlh == NULL) 182026932566SPatrick McHardy return -EMSGSIZE; 1821c7fb64dbSThomas Graf 1822ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1823c7fb64dbSThomas Graf 1824c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1825c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 18269ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 18279ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1828c7fb64dbSThomas Graf 1829ca860fb3SThomas Graf NLA_PUT_STRING(skb, NDTA_NAME, tbl->id); 1830ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval); 1831ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1); 1832ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2); 1833ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3); 1834c7fb64dbSThomas Graf 1835c7fb64dbSThomas Graf { 1836c7fb64dbSThomas Graf unsigned long now = jiffies; 1837c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1838c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1839d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1840c7fb64dbSThomas Graf struct ndt_config ndc = { 1841c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1842c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1843c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1844c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1845c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1846c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1847c7fb64dbSThomas Graf }; 1848c7fb64dbSThomas Graf 1849d6bf7817SEric Dumazet rcu_read_lock_bh(); 1850d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 1851d6bf7817SEric Dumazet ndc.ndtc_hash_rnd = nht->hash_rnd; 1852d6bf7817SEric Dumazet ndc.ndtc_hash_mask = nht->hash_mask; 1853d6bf7817SEric Dumazet rcu_read_unlock_bh(); 1854d6bf7817SEric Dumazet 1855ca860fb3SThomas Graf NLA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc); 1856c7fb64dbSThomas Graf } 1857c7fb64dbSThomas Graf 1858c7fb64dbSThomas Graf { 1859c7fb64dbSThomas Graf int cpu; 1860c7fb64dbSThomas Graf struct ndt_stats ndst; 1861c7fb64dbSThomas Graf 1862c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1863c7fb64dbSThomas Graf 18646f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1865c7fb64dbSThomas Graf struct neigh_statistics *st; 1866c7fb64dbSThomas Graf 1867c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1868c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1869c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1870c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1871c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1872c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1873c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1874c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1875c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1876c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1877c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1878c7fb64dbSThomas Graf } 1879c7fb64dbSThomas Graf 1880ca860fb3SThomas Graf NLA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst); 1881c7fb64dbSThomas Graf } 1882c7fb64dbSThomas Graf 1883c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1884c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1885ca860fb3SThomas Graf goto nla_put_failure; 1886c7fb64dbSThomas Graf 1887c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1888ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1889c7fb64dbSThomas Graf 1890ca860fb3SThomas Graf nla_put_failure: 1891c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 189226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 189326932566SPatrick McHardy return -EMSGSIZE; 1894c7fb64dbSThomas Graf } 1895c7fb64dbSThomas Graf 1896ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1897ca860fb3SThomas Graf struct neigh_table *tbl, 1898c7fb64dbSThomas Graf struct neigh_parms *parms, 1899ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1900ca860fb3SThomas Graf unsigned int flags) 1901c7fb64dbSThomas Graf { 1902c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1903c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1904c7fb64dbSThomas Graf 1905ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1906ca860fb3SThomas Graf if (nlh == NULL) 190726932566SPatrick McHardy return -EMSGSIZE; 1908c7fb64dbSThomas Graf 1909ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1910c7fb64dbSThomas Graf 1911c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1912c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 19139ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 19149ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1915c7fb64dbSThomas Graf 1916ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1917ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1918ca860fb3SThomas Graf goto errout; 1919c7fb64dbSThomas Graf 1920c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1921ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1922ca860fb3SThomas Graf errout: 1923c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 192426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 192526932566SPatrick McHardy return -EMSGSIZE; 1926c7fb64dbSThomas Graf } 1927c7fb64dbSThomas Graf 1928ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 19296b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 19306b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 19316b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 19326b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 19336b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 19346b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 19356b3f8674SThomas Graf }; 19366b3f8674SThomas Graf 1937ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 19386b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 19396b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 19406b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 19416b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 19426b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 19436b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 19446b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 19456b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 19466b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 19476b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 19486b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 19496b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 19506b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 19516b3f8674SThomas Graf }; 19526b3f8674SThomas Graf 1953c8822a4eSThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1954c7fb64dbSThomas Graf { 19553b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1956c7fb64dbSThomas Graf struct neigh_table *tbl; 19576b3f8674SThomas Graf struct ndtmsg *ndtmsg; 19586b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 19596b3f8674SThomas Graf int err; 1960c7fb64dbSThomas Graf 19616b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 19626b3f8674SThomas Graf nl_neightbl_policy); 19636b3f8674SThomas Graf if (err < 0) 19646b3f8674SThomas Graf goto errout; 1965c7fb64dbSThomas Graf 19666b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 19676b3f8674SThomas Graf err = -EINVAL; 19686b3f8674SThomas Graf goto errout; 19696b3f8674SThomas Graf } 19706b3f8674SThomas Graf 19716b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1972c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1973c7fb64dbSThomas Graf for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1974c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1975c7fb64dbSThomas Graf continue; 1976c7fb64dbSThomas Graf 19776b3f8674SThomas Graf if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) 1978c7fb64dbSThomas Graf break; 1979c7fb64dbSThomas Graf } 1980c7fb64dbSThomas Graf 1981c7fb64dbSThomas Graf if (tbl == NULL) { 1982c7fb64dbSThomas Graf err = -ENOENT; 19836b3f8674SThomas Graf goto errout_locked; 1984c7fb64dbSThomas Graf } 1985c7fb64dbSThomas Graf 1986c7fb64dbSThomas Graf /* 1987c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1988c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1989c7fb64dbSThomas Graf */ 1990c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1991c7fb64dbSThomas Graf 19926b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 19936b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 1994c7fb64dbSThomas Graf struct neigh_parms *p; 19956b3f8674SThomas Graf int i, ifindex = 0; 1996c7fb64dbSThomas Graf 19976b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 19986b3f8674SThomas Graf nl_ntbl_parm_policy); 19996b3f8674SThomas Graf if (err < 0) 20006b3f8674SThomas Graf goto errout_tbl_lock; 2001c7fb64dbSThomas Graf 20026b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 20036b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 2004c7fb64dbSThomas Graf 200597fd5bc7STobias Klauser p = lookup_neigh_parms(tbl, net, ifindex); 2006c7fb64dbSThomas Graf if (p == NULL) { 2007c7fb64dbSThomas Graf err = -ENOENT; 20086b3f8674SThomas Graf goto errout_tbl_lock; 2009c7fb64dbSThomas Graf } 2010c7fb64dbSThomas Graf 20116b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 20126b3f8674SThomas Graf if (tbp[i] == NULL) 20136b3f8674SThomas Graf continue; 2014c7fb64dbSThomas Graf 20156b3f8674SThomas Graf switch (i) { 20166b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 20176b3f8674SThomas Graf p->queue_len = nla_get_u32(tbp[i]); 20186b3f8674SThomas Graf break; 20196b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 20206b3f8674SThomas Graf p->proxy_qlen = nla_get_u32(tbp[i]); 20216b3f8674SThomas Graf break; 20226b3f8674SThomas Graf case NDTPA_APP_PROBES: 20236b3f8674SThomas Graf p->app_probes = nla_get_u32(tbp[i]); 20246b3f8674SThomas Graf break; 20256b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 20266b3f8674SThomas Graf p->ucast_probes = nla_get_u32(tbp[i]); 20276b3f8674SThomas Graf break; 20286b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 20296b3f8674SThomas Graf p->mcast_probes = nla_get_u32(tbp[i]); 20306b3f8674SThomas Graf break; 20316b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 20326b3f8674SThomas Graf p->base_reachable_time = nla_get_msecs(tbp[i]); 20336b3f8674SThomas Graf break; 20346b3f8674SThomas Graf case NDTPA_GC_STALETIME: 20356b3f8674SThomas Graf p->gc_staletime = nla_get_msecs(tbp[i]); 20366b3f8674SThomas Graf break; 20376b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 20386b3f8674SThomas Graf p->delay_probe_time = nla_get_msecs(tbp[i]); 20396b3f8674SThomas Graf break; 20406b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 20416b3f8674SThomas Graf p->retrans_time = nla_get_msecs(tbp[i]); 20426b3f8674SThomas Graf break; 20436b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 20446b3f8674SThomas Graf p->anycast_delay = nla_get_msecs(tbp[i]); 20456b3f8674SThomas Graf break; 20466b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 20476b3f8674SThomas Graf p->proxy_delay = nla_get_msecs(tbp[i]); 20486b3f8674SThomas Graf break; 20496b3f8674SThomas Graf case NDTPA_LOCKTIME: 20506b3f8674SThomas Graf p->locktime = nla_get_msecs(tbp[i]); 20516b3f8674SThomas Graf break; 2052c7fb64dbSThomas Graf } 20536b3f8674SThomas Graf } 20546b3f8674SThomas Graf } 20556b3f8674SThomas Graf 20566b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 20576b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 20586b3f8674SThomas Graf 20596b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 20606b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 20616b3f8674SThomas Graf 20626b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 20636b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 20646b3f8674SThomas Graf 20656b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 20666b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 2067c7fb64dbSThomas Graf 2068c7fb64dbSThomas Graf err = 0; 2069c7fb64dbSThomas Graf 20706b3f8674SThomas Graf errout_tbl_lock: 2071c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 20726b3f8674SThomas Graf errout_locked: 2073c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 20746b3f8674SThomas Graf errout: 2075c7fb64dbSThomas Graf return err; 2076c7fb64dbSThomas Graf } 2077c7fb64dbSThomas Graf 2078c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 2079c7fb64dbSThomas Graf { 20803b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2081ca860fb3SThomas Graf int family, tidx, nidx = 0; 2082ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 2083ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 2084c7fb64dbSThomas Graf struct neigh_table *tbl; 2085c7fb64dbSThomas Graf 2086ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 2087c7fb64dbSThomas Graf 2088c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 2089ca860fb3SThomas Graf for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) { 2090c7fb64dbSThomas Graf struct neigh_parms *p; 2091c7fb64dbSThomas Graf 2092ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 2093c7fb64dbSThomas Graf continue; 2094c7fb64dbSThomas Graf 2095ca860fb3SThomas Graf if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid, 2096ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 2097ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2098c7fb64dbSThomas Graf break; 2099c7fb64dbSThomas Graf 2100426b5303SEric W. Biederman for (nidx = 0, p = tbl->parms.next; p; p = p->next) { 2101878628fbSYOSHIFUJI Hideaki if (!net_eq(neigh_parms_net(p), net)) 2102426b5303SEric W. Biederman continue; 2103426b5303SEric W. Biederman 2104efc683fcSGautam Kachroo if (nidx < neigh_skip) 2105efc683fcSGautam Kachroo goto next; 2106c7fb64dbSThomas Graf 2107ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 2108ca860fb3SThomas Graf NETLINK_CB(cb->skb).pid, 2109ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 2110ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 2111ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2112c7fb64dbSThomas Graf goto out; 2113efc683fcSGautam Kachroo next: 2114efc683fcSGautam Kachroo nidx++; 2115c7fb64dbSThomas Graf } 2116c7fb64dbSThomas Graf 2117ca860fb3SThomas Graf neigh_skip = 0; 2118c7fb64dbSThomas Graf } 2119c7fb64dbSThomas Graf out: 2120c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 2121ca860fb3SThomas Graf cb->args[0] = tidx; 2122ca860fb3SThomas Graf cb->args[1] = nidx; 2123c7fb64dbSThomas Graf 2124c7fb64dbSThomas Graf return skb->len; 2125c7fb64dbSThomas Graf } 21261da177e4SLinus Torvalds 21278b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 21288b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 21291da177e4SLinus Torvalds { 21301da177e4SLinus Torvalds unsigned long now = jiffies; 21311da177e4SLinus Torvalds struct nda_cacheinfo ci; 21328b8aec50SThomas Graf struct nlmsghdr *nlh; 21338b8aec50SThomas Graf struct ndmsg *ndm; 21341da177e4SLinus Torvalds 21358b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 21368b8aec50SThomas Graf if (nlh == NULL) 213726932566SPatrick McHardy return -EMSGSIZE; 21388b8aec50SThomas Graf 21398b8aec50SThomas Graf ndm = nlmsg_data(nlh); 21408b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 21419ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 21429ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 21438b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 21448b8aec50SThomas Graf ndm->ndm_type = neigh->type; 21458b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 21461da177e4SLinus Torvalds 21478b8aec50SThomas Graf NLA_PUT(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key); 21488b8aec50SThomas Graf 21498b8aec50SThomas Graf read_lock_bh(&neigh->lock); 21508b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 21518b8aec50SThomas Graf if ((neigh->nud_state & NUD_VALID) && 21528b8aec50SThomas Graf nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, neigh->ha) < 0) { 21538b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21548b8aec50SThomas Graf goto nla_put_failure; 21558b8aec50SThomas Graf } 21568b8aec50SThomas Graf 2157b9f5f52cSStephen Hemminger ci.ndm_used = jiffies_to_clock_t(now - neigh->used); 2158b9f5f52cSStephen Hemminger ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); 2159b9f5f52cSStephen Hemminger ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); 21608b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 21618b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21628b8aec50SThomas Graf 21638b8aec50SThomas Graf NLA_PUT_U32(skb, NDA_PROBES, atomic_read(&neigh->probes)); 21648b8aec50SThomas Graf NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); 21658b8aec50SThomas Graf 21668b8aec50SThomas Graf return nlmsg_end(skb, nlh); 21678b8aec50SThomas Graf 21688b8aec50SThomas Graf nla_put_failure: 216926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 217026932566SPatrick McHardy return -EMSGSIZE; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 2173d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2174d961db35SThomas Graf { 2175d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2176d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2177d961db35SThomas Graf } 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 21801da177e4SLinus Torvalds struct netlink_callback *cb) 21811da177e4SLinus Torvalds { 21823b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 21831da177e4SLinus Torvalds struct neighbour *n; 21841da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 21851da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 2186d6bf7817SEric Dumazet struct neigh_hash_table *nht; 21871da177e4SLinus Torvalds 2188d6bf7817SEric Dumazet rcu_read_lock_bh(); 2189d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2190d6bf7817SEric Dumazet 2191d6bf7817SEric Dumazet for (h = 0; h <= nht->hash_mask; h++) { 21921da177e4SLinus Torvalds if (h < s_h) 21931da177e4SLinus Torvalds continue; 21941da177e4SLinus Torvalds if (h > s_h) 21951da177e4SLinus Torvalds s_idx = 0; 2196767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0; 2197767e97e1SEric Dumazet n != NULL; 2198767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 219909ad9bc7SOctavian Purdila if (!net_eq(dev_net(n->dev), net)) 2200426b5303SEric W. Biederman continue; 2201efc683fcSGautam Kachroo if (idx < s_idx) 2202efc683fcSGautam Kachroo goto next; 22031da177e4SLinus Torvalds if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid, 22041da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2205b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 2206b6544c0bSJamal Hadi Salim NLM_F_MULTI) <= 0) { 22071da177e4SLinus Torvalds rc = -1; 22081da177e4SLinus Torvalds goto out; 22091da177e4SLinus Torvalds } 2210efc683fcSGautam Kachroo next: 2211efc683fcSGautam Kachroo idx++; 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds rc = skb->len; 22151da177e4SLinus Torvalds out: 2216d6bf7817SEric Dumazet rcu_read_unlock_bh(); 22171da177e4SLinus Torvalds cb->args[1] = h; 22181da177e4SLinus Torvalds cb->args[2] = idx; 22191da177e4SLinus Torvalds return rc; 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 2222c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 22231da177e4SLinus Torvalds { 22241da177e4SLinus Torvalds struct neigh_table *tbl; 22251da177e4SLinus Torvalds int t, family, s_t; 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 22288b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 22291da177e4SLinus Torvalds s_t = cb->args[0]; 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) { 22321da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 22331da177e4SLinus Torvalds continue; 22341da177e4SLinus Torvalds if (t > s_t) 22351da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 22361da177e4SLinus Torvalds sizeof(cb->args[0])); 22371da177e4SLinus Torvalds if (neigh_dump_table(tbl, skb, cb) < 0) 22381da177e4SLinus Torvalds break; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds cb->args[0] = t; 22431da177e4SLinus Torvalds return skb->len; 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 22471da177e4SLinus Torvalds { 22481da177e4SLinus Torvalds int chain; 2249d6bf7817SEric Dumazet struct neigh_hash_table *nht; 22501da177e4SLinus Torvalds 2251d6bf7817SEric Dumazet rcu_read_lock_bh(); 2252d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2253d6bf7817SEric Dumazet 2254767e97e1SEric Dumazet read_lock(&tbl->lock); /* avoid resizes */ 2255d6bf7817SEric Dumazet for (chain = 0; chain <= nht->hash_mask; chain++) { 22561da177e4SLinus Torvalds struct neighbour *n; 22571da177e4SLinus Torvalds 2258767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[chain]); 2259767e97e1SEric Dumazet n != NULL; 2260767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) 22611da177e4SLinus Torvalds cb(n, cookie); 22621da177e4SLinus Torvalds } 2263d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2264d6bf7817SEric Dumazet rcu_read_unlock_bh(); 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 22691da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 22701da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 22711da177e4SLinus Torvalds { 22721da177e4SLinus Torvalds int chain; 2273d6bf7817SEric Dumazet struct neigh_hash_table *nht; 22741da177e4SLinus Torvalds 2275d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 2276d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 2277d6bf7817SEric Dumazet for (chain = 0; chain <= nht->hash_mask; chain++) { 2278767e97e1SEric Dumazet struct neighbour *n; 2279767e97e1SEric Dumazet struct neighbour __rcu **np; 22801da177e4SLinus Torvalds 2281d6bf7817SEric Dumazet np = &nht->hash_buckets[chain]; 2282767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 2283767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 22841da177e4SLinus Torvalds int release; 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds write_lock(&n->lock); 22871da177e4SLinus Torvalds release = cb(n); 22881da177e4SLinus Torvalds if (release) { 2289767e97e1SEric Dumazet rcu_assign_pointer(*np, 2290767e97e1SEric Dumazet rcu_dereference_protected(n->next, 2291767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 22921da177e4SLinus Torvalds n->dead = 1; 22931da177e4SLinus Torvalds } else 22941da177e4SLinus Torvalds np = &n->next; 22951da177e4SLinus Torvalds write_unlock(&n->lock); 22964f494554SThomas Graf if (release) 22974f494554SThomas Graf neigh_cleanup_and_release(n); 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds } 2300ecbb4169SAlexey Kuznetsov } 23011da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 23061da177e4SLinus Torvalds { 23071da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23081218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2309d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 23101da177e4SLinus Torvalds struct neighbour *n = NULL; 23111da177e4SLinus Torvalds int bucket = state->bucket; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 2314d6bf7817SEric Dumazet for (bucket = 0; bucket <= nht->hash_mask; bucket++) { 2315767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[bucket]); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds while (n) { 2318878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2319426b5303SEric W. Biederman goto next; 23201da177e4SLinus Torvalds if (state->neigh_sub_iter) { 23211da177e4SLinus Torvalds loff_t fakep = 0; 23221da177e4SLinus Torvalds void *v; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 23251da177e4SLinus Torvalds if (!v) 23261da177e4SLinus Torvalds goto next; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 23291da177e4SLinus Torvalds break; 23301da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 23311da177e4SLinus Torvalds break; 23321da177e4SLinus Torvalds next: 2333767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds if (n) 23371da177e4SLinus Torvalds break; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds state->bucket = bucket; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds return n; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 23451da177e4SLinus Torvalds struct neighbour *n, 23461da177e4SLinus Torvalds loff_t *pos) 23471da177e4SLinus Torvalds { 23481da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23491218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2350d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds if (state->neigh_sub_iter) { 23531da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 23541da177e4SLinus Torvalds if (v) 23551da177e4SLinus Torvalds return n; 23561da177e4SLinus Torvalds } 2357767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 23581da177e4SLinus Torvalds 23591da177e4SLinus Torvalds while (1) { 23601da177e4SLinus Torvalds while (n) { 2361878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2362426b5303SEric W. Biederman goto next; 23631da177e4SLinus Torvalds if (state->neigh_sub_iter) { 23641da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 23651da177e4SLinus Torvalds if (v) 23661da177e4SLinus Torvalds return n; 23671da177e4SLinus Torvalds goto next; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 23701da177e4SLinus Torvalds break; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 23731da177e4SLinus Torvalds break; 23741da177e4SLinus Torvalds next: 2375767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds if (n) 23791da177e4SLinus Torvalds break; 23801da177e4SLinus Torvalds 2381d6bf7817SEric Dumazet if (++state->bucket > nht->hash_mask) 23821da177e4SLinus Torvalds break; 23831da177e4SLinus Torvalds 2384767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[state->bucket]); 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds if (n && pos) 23881da177e4SLinus Torvalds --(*pos); 23891da177e4SLinus Torvalds return n; 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 23931da177e4SLinus Torvalds { 23941da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds if (n) { 2397745e2031SChris Larson --(*pos); 23981da177e4SLinus Torvalds while (*pos) { 23991da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 24001da177e4SLinus Torvalds if (!n) 24011da177e4SLinus Torvalds break; 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds return *pos ? NULL : n; 24051da177e4SLinus Torvalds } 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 24081da177e4SLinus Torvalds { 24091da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24101218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 24111da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 24121da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 24131da177e4SLinus Torvalds int bucket = state->bucket; 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 24161da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 24171da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2418878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2419426b5303SEric W. Biederman pn = pn->next; 24201da177e4SLinus Torvalds if (pn) 24211da177e4SLinus Torvalds break; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds state->bucket = bucket; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds return pn; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 24291da177e4SLinus Torvalds struct pneigh_entry *pn, 24301da177e4SLinus Torvalds loff_t *pos) 24311da177e4SLinus Torvalds { 24321da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24331218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 24341da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds pn = pn->next; 24371da177e4SLinus Torvalds while (!pn) { 24381da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 24391da177e4SLinus Torvalds break; 24401da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2441878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2442426b5303SEric W. Biederman pn = pn->next; 24431da177e4SLinus Torvalds if (pn) 24441da177e4SLinus Torvalds break; 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds if (pn && pos) 24481da177e4SLinus Torvalds --(*pos); 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds return pn; 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 24541da177e4SLinus Torvalds { 24551da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds if (pn) { 2458745e2031SChris Larson --(*pos); 24591da177e4SLinus Torvalds while (*pos) { 24601da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 24611da177e4SLinus Torvalds if (!pn) 24621da177e4SLinus Torvalds break; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds return *pos ? NULL : pn; 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 24691da177e4SLinus Torvalds { 24701da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24711da177e4SLinus Torvalds void *rc; 2472745e2031SChris Larson loff_t idxpos = *pos; 24731da177e4SLinus Torvalds 2474745e2031SChris Larson rc = neigh_get_idx(seq, &idxpos); 24751da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 2476745e2031SChris Larson rc = pneigh_get_idx(seq, &idxpos); 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds return rc; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 2482d6bf7817SEric Dumazet __acquires(rcu_bh) 24831da177e4SLinus Torvalds { 24841da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds state->tbl = tbl; 24871da177e4SLinus Torvalds state->bucket = 0; 24881da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 24891da177e4SLinus Torvalds 2490d6bf7817SEric Dumazet rcu_read_lock_bh(); 2491d6bf7817SEric Dumazet state->nht = rcu_dereference_bh(tbl->nht); 2492767e97e1SEric Dumazet 2493745e2031SChris Larson return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 24981da177e4SLinus Torvalds { 24991da177e4SLinus Torvalds struct neigh_seq_state *state; 25001da177e4SLinus Torvalds void *rc; 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2503bff69732SChris Larson rc = neigh_get_first(seq); 25041da177e4SLinus Torvalds goto out; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds state = seq->private; 25081da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 25091da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 25101da177e4SLinus Torvalds if (rc) 25111da177e4SLinus Torvalds goto out; 25121da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 25131da177e4SLinus Torvalds rc = pneigh_get_first(seq); 25141da177e4SLinus Torvalds } else { 25151da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 25161da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 25171da177e4SLinus Torvalds } 25181da177e4SLinus Torvalds out: 25191da177e4SLinus Torvalds ++(*pos); 25201da177e4SLinus Torvalds return rc; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 2525d6bf7817SEric Dumazet __releases(rcu_bh) 25261da177e4SLinus Torvalds { 2527d6bf7817SEric Dumazet rcu_read_unlock_bh(); 25281da177e4SLinus Torvalds } 25291da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds /* statistics via seq_file */ 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 25341da177e4SLinus Torvalds { 253581c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 25361da177e4SLinus Torvalds int cpu; 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds if (*pos == 0) 25391da177e4SLinus Torvalds return SEQ_START_TOKEN; 25401da177e4SLinus Torvalds 25410f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 25421da177e4SLinus Torvalds if (!cpu_possible(cpu)) 25431da177e4SLinus Torvalds continue; 25441da177e4SLinus Torvalds *pos = cpu+1; 25451da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds return NULL; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 25511da177e4SLinus Torvalds { 255281c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 25531da177e4SLinus Torvalds int cpu; 25541da177e4SLinus Torvalds 25550f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 25561da177e4SLinus Torvalds if (!cpu_possible(cpu)) 25571da177e4SLinus Torvalds continue; 25581da177e4SLinus Torvalds *pos = cpu+1; 25591da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds return NULL; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 25651da177e4SLinus Torvalds { 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 25701da177e4SLinus Torvalds { 257181c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 25721da177e4SLinus Torvalds struct neigh_statistics *st = v; 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 25759a6d276eSNeil 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"); 25761da177e4SLinus Torvalds return 0; 25771da177e4SLinus Torvalds } 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 25809a6d276eSNeil Horman "%08lx %08lx %08lx %08lx %08lx\n", 25811da177e4SLinus Torvalds atomic_read(&tbl->entries), 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds st->allocs, 25841da177e4SLinus Torvalds st->destroys, 25851da177e4SLinus Torvalds st->hash_grows, 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds st->lookups, 25881da177e4SLinus Torvalds st->hits, 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds st->res_failed, 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds st->rcv_probes_mcast, 25931da177e4SLinus Torvalds st->rcv_probes_ucast, 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds st->periodic_gc_runs, 25969a6d276eSNeil Horman st->forced_gc_runs, 25979a6d276eSNeil Horman st->unres_discards 25981da177e4SLinus Torvalds ); 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds return 0; 26011da177e4SLinus Torvalds } 26021da177e4SLinus Torvalds 2603f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 26041da177e4SLinus Torvalds .start = neigh_stat_seq_start, 26051da177e4SLinus Torvalds .next = neigh_stat_seq_next, 26061da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 26071da177e4SLinus Torvalds .show = neigh_stat_seq_show, 26081da177e4SLinus Torvalds }; 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 26111da177e4SLinus Torvalds { 26121da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds if (!ret) { 26151da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 261681c1ebfcSAlexey Dobriyan sf->private = PDE(inode)->data; 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds return ret; 26191da177e4SLinus Torvalds }; 26201da177e4SLinus Torvalds 26219a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 26221da177e4SLinus Torvalds .owner = THIS_MODULE, 26231da177e4SLinus Torvalds .open = neigh_stat_seq_open, 26241da177e4SLinus Torvalds .read = seq_read, 26251da177e4SLinus Torvalds .llseek = seq_lseek, 26261da177e4SLinus Torvalds .release = seq_release, 26271da177e4SLinus Torvalds }; 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 26301da177e4SLinus Torvalds 2631339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2632339bf98fSThomas Graf { 2633339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2634339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2635339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2636339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2637339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2638339bf98fSThomas Graf } 2639339bf98fSThomas Graf 2640b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 26411da177e4SLinus Torvalds { 2642c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(n->dev); 26438b8aec50SThomas Graf struct sk_buff *skb; 2644b8673311SThomas Graf int err = -ENOBUFS; 26451da177e4SLinus Torvalds 2646339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 26478b8aec50SThomas Graf if (skb == NULL) 2648b8673311SThomas Graf goto errout; 26491da177e4SLinus Torvalds 2650b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 265126932566SPatrick McHardy if (err < 0) { 265226932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 265326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 265426932566SPatrick McHardy kfree_skb(skb); 265526932566SPatrick McHardy goto errout; 265626932566SPatrick McHardy } 26571ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 26581ce85fe4SPablo Neira Ayuso return; 2659b8673311SThomas Graf errout: 2660b8673311SThomas Graf if (err < 0) 2661426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2662b8673311SThomas Graf } 2663b8673311SThomas Graf 2664d961db35SThomas Graf #ifdef CONFIG_ARPD 2665b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2666b8673311SThomas Graf { 2667b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 26688b8aec50SThomas Graf } 26690a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns); 26701da177e4SLinus Torvalds #endif /* CONFIG_ARPD */ 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 26731da177e4SLinus Torvalds 267454716e3bSEric W. Biederman #define NEIGH_VARS_MAX 19 267554716e3bSEric W. Biederman 26761da177e4SLinus Torvalds static struct neigh_sysctl_table { 26771da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 267854716e3bSEric W. Biederman struct ctl_table neigh_vars[NEIGH_VARS_MAX]; 2679c3bac5a7SPavel Emelyanov char *dev_name; 2680ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 26811da177e4SLinus Torvalds .neigh_vars = { 26821da177e4SLinus Torvalds { 26831da177e4SLinus Torvalds .procname = "mcast_solicit", 26841da177e4SLinus Torvalds .maxlen = sizeof(int), 26851da177e4SLinus Torvalds .mode = 0644, 26866d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26871da177e4SLinus Torvalds }, 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds .procname = "ucast_solicit", 26901da177e4SLinus Torvalds .maxlen = sizeof(int), 26911da177e4SLinus Torvalds .mode = 0644, 26926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26931da177e4SLinus Torvalds }, 26941da177e4SLinus Torvalds { 26951da177e4SLinus Torvalds .procname = "app_solicit", 26961da177e4SLinus Torvalds .maxlen = sizeof(int), 26971da177e4SLinus Torvalds .mode = 0644, 26986d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26991da177e4SLinus Torvalds }, 27001da177e4SLinus Torvalds { 27011da177e4SLinus Torvalds .procname = "retrans_time", 27021da177e4SLinus Torvalds .maxlen = sizeof(int), 27031da177e4SLinus Torvalds .mode = 0644, 27046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 27051da177e4SLinus Torvalds }, 27061da177e4SLinus Torvalds { 27071da177e4SLinus Torvalds .procname = "base_reachable_time", 27081da177e4SLinus Torvalds .maxlen = sizeof(int), 27091da177e4SLinus Torvalds .mode = 0644, 27106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27111da177e4SLinus Torvalds }, 27121da177e4SLinus Torvalds { 27131da177e4SLinus Torvalds .procname = "delay_first_probe_time", 27141da177e4SLinus Torvalds .maxlen = sizeof(int), 27151da177e4SLinus Torvalds .mode = 0644, 27166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27171da177e4SLinus Torvalds }, 27181da177e4SLinus Torvalds { 27191da177e4SLinus Torvalds .procname = "gc_stale_time", 27201da177e4SLinus Torvalds .maxlen = sizeof(int), 27211da177e4SLinus Torvalds .mode = 0644, 27226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27231da177e4SLinus Torvalds }, 27241da177e4SLinus Torvalds { 27251da177e4SLinus Torvalds .procname = "unres_qlen", 27261da177e4SLinus Torvalds .maxlen = sizeof(int), 27271da177e4SLinus Torvalds .mode = 0644, 27286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27291da177e4SLinus Torvalds }, 27301da177e4SLinus Torvalds { 27311da177e4SLinus Torvalds .procname = "proxy_qlen", 27321da177e4SLinus Torvalds .maxlen = sizeof(int), 27331da177e4SLinus Torvalds .mode = 0644, 27346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27351da177e4SLinus Torvalds }, 27361da177e4SLinus Torvalds { 27371da177e4SLinus Torvalds .procname = "anycast_delay", 27381da177e4SLinus Torvalds .maxlen = sizeof(int), 27391da177e4SLinus Torvalds .mode = 0644, 27406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 27411da177e4SLinus Torvalds }, 27421da177e4SLinus Torvalds { 27431da177e4SLinus Torvalds .procname = "proxy_delay", 27441da177e4SLinus Torvalds .maxlen = sizeof(int), 27451da177e4SLinus Torvalds .mode = 0644, 27466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 27471da177e4SLinus Torvalds }, 27481da177e4SLinus Torvalds { 27491da177e4SLinus Torvalds .procname = "locktime", 27501da177e4SLinus Torvalds .maxlen = sizeof(int), 27511da177e4SLinus Torvalds .mode = 0644, 27526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 27531da177e4SLinus Torvalds }, 27541da177e4SLinus Torvalds { 2755d12af679SEric W. Biederman .procname = "retrans_time_ms", 2756d12af679SEric W. Biederman .maxlen = sizeof(int), 2757d12af679SEric W. Biederman .mode = 0644, 27586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2759d12af679SEric W. Biederman }, 2760d12af679SEric W. Biederman { 2761d12af679SEric W. Biederman .procname = "base_reachable_time_ms", 2762d12af679SEric W. Biederman .maxlen = sizeof(int), 2763d12af679SEric W. Biederman .mode = 0644, 27646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2765d12af679SEric W. Biederman }, 2766d12af679SEric W. Biederman { 27671da177e4SLinus Torvalds .procname = "gc_interval", 27681da177e4SLinus Torvalds .maxlen = sizeof(int), 27691da177e4SLinus Torvalds .mode = 0644, 27706d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27711da177e4SLinus Torvalds }, 27721da177e4SLinus Torvalds { 27731da177e4SLinus Torvalds .procname = "gc_thresh1", 27741da177e4SLinus Torvalds .maxlen = sizeof(int), 27751da177e4SLinus Torvalds .mode = 0644, 27766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27771da177e4SLinus Torvalds }, 27781da177e4SLinus Torvalds { 27791da177e4SLinus Torvalds .procname = "gc_thresh2", 27801da177e4SLinus Torvalds .maxlen = sizeof(int), 27811da177e4SLinus Torvalds .mode = 0644, 27826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27831da177e4SLinus Torvalds }, 27841da177e4SLinus Torvalds { 27851da177e4SLinus Torvalds .procname = "gc_thresh3", 27861da177e4SLinus Torvalds .maxlen = sizeof(int), 27871da177e4SLinus Torvalds .mode = 0644, 27886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27891da177e4SLinus Torvalds }, 2790c3bac5a7SPavel Emelyanov {}, 27911da177e4SLinus Torvalds }, 27921da177e4SLinus Torvalds }; 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 279554716e3bSEric W. Biederman char *p_name, proc_handler *handler) 27961da177e4SLinus Torvalds { 27973c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 27981da177e4SLinus Torvalds const char *dev_name_source = NULL; 2799c3bac5a7SPavel Emelyanov 2800c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_ROOT 0 2801c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_PROTO 1 2802c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_NEIGH 2 2803c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_DEV 3 2804c3bac5a7SPavel Emelyanov 2805c3bac5a7SPavel Emelyanov struct ctl_path neigh_path[] = { 2806f8572d8fSEric W. Biederman { .procname = "net", }, 2807f8572d8fSEric W. Biederman { .procname = "proto", }, 2808f8572d8fSEric W. Biederman { .procname = "neigh", }, 2809f8572d8fSEric W. Biederman { .procname = "default", }, 2810c3bac5a7SPavel Emelyanov { }, 2811c3bac5a7SPavel Emelyanov }; 28121da177e4SLinus Torvalds 28133c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 28141da177e4SLinus Torvalds if (!t) 28153c607bbbSPavel Emelyanov goto err; 28163c607bbbSPavel Emelyanov 28171da177e4SLinus Torvalds t->neigh_vars[0].data = &p->mcast_probes; 28181da177e4SLinus Torvalds t->neigh_vars[1].data = &p->ucast_probes; 28191da177e4SLinus Torvalds t->neigh_vars[2].data = &p->app_probes; 28201da177e4SLinus Torvalds t->neigh_vars[3].data = &p->retrans_time; 28211da177e4SLinus Torvalds t->neigh_vars[4].data = &p->base_reachable_time; 28221da177e4SLinus Torvalds t->neigh_vars[5].data = &p->delay_probe_time; 28231da177e4SLinus Torvalds t->neigh_vars[6].data = &p->gc_staletime; 28241da177e4SLinus Torvalds t->neigh_vars[7].data = &p->queue_len; 28251da177e4SLinus Torvalds t->neigh_vars[8].data = &p->proxy_qlen; 28261da177e4SLinus Torvalds t->neigh_vars[9].data = &p->anycast_delay; 28271da177e4SLinus Torvalds t->neigh_vars[10].data = &p->proxy_delay; 28281da177e4SLinus Torvalds t->neigh_vars[11].data = &p->locktime; 2829d12af679SEric W. Biederman t->neigh_vars[12].data = &p->retrans_time; 2830d12af679SEric W. Biederman t->neigh_vars[13].data = &p->base_reachable_time; 28311da177e4SLinus Torvalds 28321da177e4SLinus Torvalds if (dev) { 28331da177e4SLinus Torvalds dev_name_source = dev->name; 2834d12af679SEric W. Biederman /* Terminate the table early */ 2835d12af679SEric W. Biederman memset(&t->neigh_vars[14], 0, sizeof(t->neigh_vars[14])); 28361da177e4SLinus Torvalds } else { 2837c3bac5a7SPavel Emelyanov dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname; 2838d12af679SEric W. Biederman t->neigh_vars[14].data = (int *)(p + 1); 2839d12af679SEric W. Biederman t->neigh_vars[15].data = (int *)(p + 1) + 1; 2840d12af679SEric W. Biederman t->neigh_vars[16].data = (int *)(p + 1) + 2; 2841d12af679SEric W. Biederman t->neigh_vars[17].data = (int *)(p + 1) + 3; 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds 2845f8572d8fSEric W. Biederman if (handler) { 28461da177e4SLinus Torvalds /* RetransTime */ 28471da177e4SLinus Torvalds t->neigh_vars[3].proc_handler = handler; 28481da177e4SLinus Torvalds t->neigh_vars[3].extra1 = dev; 28491da177e4SLinus Torvalds /* ReachableTime */ 28501da177e4SLinus Torvalds t->neigh_vars[4].proc_handler = handler; 28511da177e4SLinus Torvalds t->neigh_vars[4].extra1 = dev; 28521da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 2853d12af679SEric W. Biederman t->neigh_vars[12].proc_handler = handler; 2854d12af679SEric W. Biederman t->neigh_vars[12].extra1 = dev; 28551da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 2856d12af679SEric W. Biederman t->neigh_vars[13].proc_handler = handler; 2857d12af679SEric W. Biederman t->neigh_vars[13].extra1 = dev; 28581da177e4SLinus Torvalds } 28591da177e4SLinus Torvalds 2860c3bac5a7SPavel Emelyanov t->dev_name = kstrdup(dev_name_source, GFP_KERNEL); 2861c3bac5a7SPavel Emelyanov if (!t->dev_name) 28621da177e4SLinus Torvalds goto free; 28631da177e4SLinus Torvalds 2864c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name; 2865c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name; 28661da177e4SLinus Torvalds 28674ab438fcSDenis V. Lunev t->sysctl_header = 286857da52c1SYOSHIFUJI Hideaki register_net_sysctl_table(neigh_parms_net(p), neigh_path, t->neigh_vars); 28693c607bbbSPavel Emelyanov if (!t->sysctl_header) 28701da177e4SLinus Torvalds goto free_procname; 28713c607bbbSPavel Emelyanov 28721da177e4SLinus Torvalds p->sysctl_table = t; 28731da177e4SLinus Torvalds return 0; 28741da177e4SLinus Torvalds 28751da177e4SLinus Torvalds free_procname: 2876c3bac5a7SPavel Emelyanov kfree(t->dev_name); 28771da177e4SLinus Torvalds free: 28781da177e4SLinus Torvalds kfree(t); 28793c607bbbSPavel Emelyanov err: 28803c607bbbSPavel Emelyanov return -ENOBUFS; 28811da177e4SLinus Torvalds } 28820a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register); 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 28851da177e4SLinus Torvalds { 28861da177e4SLinus Torvalds if (p->sysctl_table) { 28871da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 28881da177e4SLinus Torvalds p->sysctl_table = NULL; 28891da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 2890c3bac5a7SPavel Emelyanov kfree(t->dev_name); 28911da177e4SLinus Torvalds kfree(t); 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds } 28940a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister); 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 28971da177e4SLinus Torvalds 2898c8822a4eSThomas Graf static int __init neigh_init(void) 2899c8822a4eSThomas Graf { 2900c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL); 2901c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL); 2902c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info); 2903c8822a4eSThomas Graf 2904c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info); 2905c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL); 2906c8822a4eSThomas Graf 2907c8822a4eSThomas Graf return 0; 2908c8822a4eSThomas Graf } 2909c8822a4eSThomas Graf 2910c8822a4eSThomas Graf subsys_initcall(neigh_init); 2911c8822a4eSThomas Graf 2912