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 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/socket.h> 221da177e4SLinus Torvalds #include <linux/netdevice.h> 231da177e4SLinus Torvalds #include <linux/proc_fs.h> 241da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 251da177e4SLinus Torvalds #include <linux/sysctl.h> 261da177e4SLinus Torvalds #endif 271da177e4SLinus Torvalds #include <linux/times.h> 28457c4cbcSEric W. Biederman #include <net/net_namespace.h> 291da177e4SLinus Torvalds #include <net/neighbour.h> 301da177e4SLinus Torvalds #include <net/dst.h> 311da177e4SLinus Torvalds #include <net/sock.h> 328d71740cSTom Tucker #include <net/netevent.h> 33a14a49d2SThomas Graf #include <net/netlink.h> 341da177e4SLinus Torvalds #include <linux/rtnetlink.h> 351da177e4SLinus Torvalds #include <linux/random.h> 36543537bdSPaulo Marques #include <linux/string.h> 37c3609d51Svignesh babu #include <linux/log2.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define NEIGH_DEBUG 1 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #define NEIGH_PRINTK(x...) printk(x) 421da177e4SLinus Torvalds #define NEIGH_NOPRINTK(x...) do { ; } while(0) 431da177e4SLinus Torvalds #define NEIGH_PRINTK0 NEIGH_PRINTK 441da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_NOPRINTK 451da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_NOPRINTK 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #if NEIGH_DEBUG >= 1 481da177e4SLinus Torvalds #undef NEIGH_PRINTK1 491da177e4SLinus Torvalds #define NEIGH_PRINTK1 NEIGH_PRINTK 501da177e4SLinus Torvalds #endif 511da177e4SLinus Torvalds #if NEIGH_DEBUG >= 2 521da177e4SLinus Torvalds #undef NEIGH_PRINTK2 531da177e4SLinus Torvalds #define NEIGH_PRINTK2 NEIGH_PRINTK 541da177e4SLinus Torvalds #endif 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #define PNEIGH_HASHMASK 0xF 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg); 59d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags); 60d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh); 611da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds static struct neigh_table *neigh_tables; 6445fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS 659a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops; 6645fc3b11SAmos Waterland #endif 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* 691da177e4SLinus Torvalds Neighbour hash table buckets are protected with rwlock tbl->lock. 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds - All the scans/updates to hash buckets MUST be made under this lock. 721da177e4SLinus Torvalds - NOTHING clever should be made under this lock: no callbacks 731da177e4SLinus Torvalds to protocol backends, no attempts to send something to network. 741da177e4SLinus Torvalds It will result in deadlocks, if backend/driver wants to use neighbour 751da177e4SLinus Torvalds cache. 761da177e4SLinus Torvalds - If the entry requires some non-trivial actions, increase 771da177e4SLinus Torvalds its reference count and release table lock. 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds Neighbour entries are protected: 801da177e4SLinus Torvalds - with reference count. 811da177e4SLinus Torvalds - with rwlock neigh->lock 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds Reference count prevents destruction. 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds neigh->lock mainly serializes ll address data and its validity state. 861da177e4SLinus Torvalds However, the same lock is used to protect another entry fields: 871da177e4SLinus Torvalds - timer 881da177e4SLinus Torvalds - resolution queue 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds Again, nothing clever shall be made under neigh->lock, 911da177e4SLinus Torvalds the most complicated procedure, which we allow is dev->hard_header. 921da177e4SLinus Torvalds It is supposed, that dev->hard_header is simplistic and does 931da177e4SLinus Torvalds not make callbacks to neighbour tables. 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds The last lock is neigh_tbl_lock. It is pure SMP lock, protecting 961da177e4SLinus Torvalds list of neighbour tables. This list is used only in process context, 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds static DEFINE_RWLOCK(neigh_tbl_lock); 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static int neigh_blackhole(struct sk_buff *skb) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds kfree_skb(skb); 1041da177e4SLinus Torvalds return -ENETDOWN; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1074f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh) 1084f494554SThomas Graf { 1094f494554SThomas Graf if (neigh->parms->neigh_cleanup) 1104f494554SThomas Graf neigh->parms->neigh_cleanup(neigh); 1114f494554SThomas Graf 112d961db35SThomas Graf __neigh_notify(neigh, RTM_DELNEIGH, 0); 1134f494554SThomas Graf neigh_release(neigh); 1144f494554SThomas Graf } 1154f494554SThomas Graf 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * It is random distribution in the interval (1/2)*base...(3/2)*base. 1181da177e4SLinus Torvalds * It corresponds to default IPv6 settings and is not overridable, 1191da177e4SLinus Torvalds * because it is really reasonable choice. 1201da177e4SLinus Torvalds */ 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds return (base ? (net_random() % base) + (base >> 1) : 0); 1251da177e4SLinus Torvalds } 1260a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time); 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds int shrunk = 0; 1321da177e4SLinus Torvalds int i; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 1371da177e4SLinus Torvalds for (i = 0; i <= tbl->hash_mask; i++) { 1381da177e4SLinus Torvalds struct neighbour *n, **np; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds np = &tbl->hash_buckets[i]; 1411da177e4SLinus Torvalds while ((n = *np) != NULL) { 1421da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1431da177e4SLinus Torvalds * - nobody refers to it. 1441da177e4SLinus Torvalds * - it is not permanent 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds write_lock(&n->lock); 1471da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1481da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 1491da177e4SLinus Torvalds *np = n->next; 1501da177e4SLinus Torvalds n->dead = 1; 1511da177e4SLinus Torvalds shrunk = 1; 1521da177e4SLinus Torvalds write_unlock(&n->lock); 1534f494554SThomas Graf neigh_cleanup_and_release(n); 1541da177e4SLinus Torvalds continue; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds write_unlock(&n->lock); 1571da177e4SLinus Torvalds np = &n->next; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds tbl->last_flush = jiffies; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds return shrunk; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 168a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 169a43d8994SPavel Emelyanov { 170a43d8994SPavel Emelyanov neigh_hold(n); 171a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 172a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 173a43d8994SPavel Emelyanov n->nud_state); 174a43d8994SPavel Emelyanov dump_stack(); 175a43d8994SPavel Emelyanov } 176a43d8994SPavel Emelyanov } 177a43d8994SPavel Emelyanov 1781da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1811da177e4SLinus Torvalds del_timer(&n->timer)) { 1821da177e4SLinus Torvalds neigh_release(n); 1831da177e4SLinus Torvalds return 1; 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds return 0; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds struct sk_buff *skb; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 1931da177e4SLinus Torvalds dev_put(skb->dev); 1941da177e4SLinus Torvalds kfree_skb(skb); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 19849636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds int i; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds for (i = 0; i <= tbl->hash_mask; i++) { 2031da177e4SLinus Torvalds struct neighbour *n, **np = &tbl->hash_buckets[i]; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds while ((n = *np) != NULL) { 2061da177e4SLinus Torvalds if (dev && n->dev != dev) { 2071da177e4SLinus Torvalds np = &n->next; 2081da177e4SLinus Torvalds continue; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds *np = n->next; 2111da177e4SLinus Torvalds write_lock(&n->lock); 2121da177e4SLinus Torvalds neigh_del_timer(n); 2131da177e4SLinus Torvalds n->dead = 1; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2161da177e4SLinus Torvalds /* The most unpleasant situation. 2171da177e4SLinus Torvalds We must destroy neighbour entry, 2181da177e4SLinus Torvalds but someone still uses it. 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds The destroy will be delayed until 2211da177e4SLinus Torvalds the last user releases us, but 2221da177e4SLinus Torvalds we must kill timers etc. and move 2231da177e4SLinus Torvalds it to safe state. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds skb_queue_purge(&n->arp_queue); 2261da177e4SLinus Torvalds n->output = neigh_blackhole; 2271da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2281da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2291da177e4SLinus Torvalds else 2301da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2311da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is stray.\n", n); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds write_unlock(&n->lock); 2344f494554SThomas Graf neigh_cleanup_and_release(n); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } 23749636bb1SHerbert Xu } 2381da177e4SLinus Torvalds 23949636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 24049636bb1SHerbert Xu { 24149636bb1SHerbert Xu write_lock_bh(&tbl->lock); 24249636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 24349636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 24449636bb1SHerbert Xu } 2450a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr); 24649636bb1SHerbert Xu 24749636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 24849636bb1SHerbert Xu { 24949636bb1SHerbert Xu write_lock_bh(&tbl->lock); 25049636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2511da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2521da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2551da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2561da177e4SLinus Torvalds return 0; 2571da177e4SLinus Torvalds } 2580a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static struct neighbour *neigh_alloc(struct neigh_table *tbl) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds struct neighbour *n = NULL; 2631da177e4SLinus Torvalds unsigned long now = jiffies; 2641da177e4SLinus Torvalds int entries; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2671da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2681da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2691da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2701da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2711da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2721da177e4SLinus Torvalds goto out_entries; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 275c3762229SRobert P. J. Day n = kmem_cache_zalloc(tbl->kmem_cachep, GFP_ATOMIC); 2761da177e4SLinus Torvalds if (!n) 2771da177e4SLinus Torvalds goto out_entries; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds skb_queue_head_init(&n->arp_queue); 2801da177e4SLinus Torvalds rwlock_init(&n->lock); 2811da177e4SLinus Torvalds n->updated = n->used = now; 2821da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2831da177e4SLinus Torvalds n->output = neigh_blackhole; 2841da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 285b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 2881da177e4SLinus Torvalds n->tbl = tbl; 2891da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 2901da177e4SLinus Torvalds n->dead = 1; 2911da177e4SLinus Torvalds out: 2921da177e4SLinus Torvalds return n; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds out_entries: 2951da177e4SLinus Torvalds atomic_dec(&tbl->entries); 2961da177e4SLinus Torvalds goto out; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds static struct neighbour **neigh_hash_alloc(unsigned int entries) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds unsigned long size = entries * sizeof(struct neighbour *); 3021da177e4SLinus Torvalds struct neighbour **ret; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds if (size <= PAGE_SIZE) { 30577d04bd9SAndrew Morton ret = kzalloc(size, GFP_ATOMIC); 3061da177e4SLinus Torvalds } else { 3071da177e4SLinus Torvalds ret = (struct neighbour **) 30877d04bd9SAndrew Morton __get_free_pages(GFP_ATOMIC|__GFP_ZERO, get_order(size)); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds return ret; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds static void neigh_hash_free(struct neighbour **hash, unsigned int entries) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds unsigned long size = entries * sizeof(struct neighbour *); 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds if (size <= PAGE_SIZE) 3181da177e4SLinus Torvalds kfree(hash); 3191da177e4SLinus Torvalds else 3201da177e4SLinus Torvalds free_pages((unsigned long)hash, get_order(size)); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds struct neighbour **new_hash, **old_hash; 3261da177e4SLinus Torvalds unsigned int i, new_hash_mask, old_entries; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3291da177e4SLinus Torvalds 330c3609d51Svignesh babu BUG_ON(!is_power_of_2(new_entries)); 3311da177e4SLinus Torvalds new_hash = neigh_hash_alloc(new_entries); 3321da177e4SLinus Torvalds if (!new_hash) 3331da177e4SLinus Torvalds return; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds old_entries = tbl->hash_mask + 1; 3361da177e4SLinus Torvalds new_hash_mask = new_entries - 1; 3371da177e4SLinus Torvalds old_hash = tbl->hash_buckets; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); 3401da177e4SLinus Torvalds for (i = 0; i < old_entries; i++) { 3411da177e4SLinus Torvalds struct neighbour *n, *next; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds for (n = old_hash[i]; n; n = next) { 3441da177e4SLinus Torvalds unsigned int hash_val = tbl->hash(n->primary_key, n->dev); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds hash_val &= new_hash_mask; 3471da177e4SLinus Torvalds next = n->next; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds n->next = new_hash[hash_val]; 3501da177e4SLinus Torvalds new_hash[hash_val] = n; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds tbl->hash_buckets = new_hash; 3541da177e4SLinus Torvalds tbl->hash_mask = new_hash_mask; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds neigh_hash_free(old_hash, old_entries); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 3601da177e4SLinus Torvalds struct net_device *dev) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds struct neighbour *n; 3631da177e4SLinus Torvalds int key_len = tbl->key_len; 364bc4bf5f3SPavel Emelyanov u32 hash_val; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 369bc4bf5f3SPavel Emelyanov hash_val = tbl->hash(pkey, dev); 370c5e29460SJulian Anastasov for (n = tbl->hash_buckets[hash_val & tbl->hash_mask]; n; n = n->next) { 3711da177e4SLinus Torvalds if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) { 3721da177e4SLinus Torvalds neigh_hold(n); 3731da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 3741da177e4SLinus Torvalds break; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 3781da177e4SLinus Torvalds return n; 3791da177e4SLinus Torvalds } 3800a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup); 3811da177e4SLinus Torvalds 382426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 383426b5303SEric W. Biederman const void *pkey) 3841da177e4SLinus Torvalds { 3851da177e4SLinus Torvalds struct neighbour *n; 3861da177e4SLinus Torvalds int key_len = tbl->key_len; 387bc4bf5f3SPavel Emelyanov u32 hash_val; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 392bc4bf5f3SPavel Emelyanov hash_val = tbl->hash(pkey, NULL); 393c5e29460SJulian Anastasov for (n = tbl->hash_buckets[hash_val & tbl->hash_mask]; n; n = n->next) { 394426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 395878628fbSYOSHIFUJI Hideaki net_eq(dev_net(n->dev), net)) { 3961da177e4SLinus Torvalds neigh_hold(n); 3971da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 3981da177e4SLinus Torvalds break; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 4021da177e4SLinus Torvalds return n; 4031da177e4SLinus Torvalds } 4040a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey, 4071da177e4SLinus Torvalds struct net_device *dev) 4081da177e4SLinus Torvalds { 4091da177e4SLinus Torvalds u32 hash_val; 4101da177e4SLinus Torvalds int key_len = tbl->key_len; 4111da177e4SLinus Torvalds int error; 4121da177e4SLinus Torvalds struct neighbour *n1, *rc, *n = neigh_alloc(tbl); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds if (!n) { 4151da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4161da177e4SLinus Torvalds goto out; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4201da177e4SLinus Torvalds n->dev = dev; 4211da177e4SLinus Torvalds dev_hold(dev); 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds /* Protocol specific setup. */ 4241da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4251da177e4SLinus Torvalds rc = ERR_PTR(error); 4261da177e4SLinus Torvalds goto out_neigh_release; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* Device specific setup. */ 4301da177e4SLinus Torvalds if (n->parms->neigh_setup && 4311da177e4SLinus Torvalds (error = n->parms->neigh_setup(n)) < 0) { 4321da177e4SLinus Torvalds rc = ERR_PTR(error); 4331da177e4SLinus Torvalds goto out_neigh_release; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds n->confirmed = jiffies - (n->parms->base_reachable_time << 1); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds if (atomic_read(&tbl->entries) > (tbl->hash_mask + 1)) 4411da177e4SLinus Torvalds neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1); 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds hash_val = tbl->hash(pkey, dev) & tbl->hash_mask; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds if (n->parms->dead) { 4461da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 4471da177e4SLinus Torvalds goto out_tbl_unlock; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) { 4511da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 4521da177e4SLinus Torvalds neigh_hold(n1); 4531da177e4SLinus Torvalds rc = n1; 4541da177e4SLinus Torvalds goto out_tbl_unlock; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds n->next = tbl->hash_buckets[hash_val]; 4591da177e4SLinus Torvalds tbl->hash_buckets[hash_val] = n; 4601da177e4SLinus Torvalds n->dead = 0; 4611da177e4SLinus Torvalds neigh_hold(n); 4621da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 4631da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is created.\n", n); 4641da177e4SLinus Torvalds rc = n; 4651da177e4SLinus Torvalds out: 4661da177e4SLinus Torvalds return rc; 4671da177e4SLinus Torvalds out_tbl_unlock: 4681da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 4691da177e4SLinus Torvalds out_neigh_release: 4701da177e4SLinus Torvalds neigh_release(n); 4711da177e4SLinus Torvalds goto out; 4721da177e4SLinus Torvalds } 4730a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_create); 4741da177e4SLinus Torvalds 475be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len) 476fa86d322SPavel Emelyanov { 477fa86d322SPavel Emelyanov u32 hash_val = *(u32 *)(pkey + key_len - 4); 478fa86d322SPavel Emelyanov hash_val ^= (hash_val >> 16); 479fa86d322SPavel Emelyanov hash_val ^= hash_val >> 8; 480fa86d322SPavel Emelyanov hash_val ^= hash_val >> 4; 481fa86d322SPavel Emelyanov hash_val &= PNEIGH_HASHMASK; 482be01d655SYOSHIFUJI Hideaki return hash_val; 483fa86d322SPavel Emelyanov } 484fa86d322SPavel Emelyanov 485be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, 486be01d655SYOSHIFUJI Hideaki struct net *net, 487be01d655SYOSHIFUJI Hideaki const void *pkey, 488be01d655SYOSHIFUJI Hideaki int key_len, 489be01d655SYOSHIFUJI Hideaki struct net_device *dev) 490be01d655SYOSHIFUJI Hideaki { 491be01d655SYOSHIFUJI Hideaki while (n) { 492be01d655SYOSHIFUJI Hideaki if (!memcmp(n->key, pkey, key_len) && 493be01d655SYOSHIFUJI Hideaki net_eq(pneigh_net(n), net) && 494be01d655SYOSHIFUJI Hideaki (n->dev == dev || !n->dev)) 495fa86d322SPavel Emelyanov return n; 496be01d655SYOSHIFUJI Hideaki n = n->next; 497be01d655SYOSHIFUJI Hideaki } 498be01d655SYOSHIFUJI Hideaki return NULL; 499be01d655SYOSHIFUJI Hideaki } 500be01d655SYOSHIFUJI Hideaki 501be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, 502be01d655SYOSHIFUJI Hideaki struct net *net, const void *pkey, struct net_device *dev) 503be01d655SYOSHIFUJI Hideaki { 504be01d655SYOSHIFUJI Hideaki int key_len = tbl->key_len; 505be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 506be01d655SYOSHIFUJI Hideaki 507be01d655SYOSHIFUJI Hideaki return __pneigh_lookup_1(tbl->phash_buckets[hash_val], 508be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 509fa86d322SPavel Emelyanov } 5100a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup); 511fa86d322SPavel Emelyanov 512426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 513426b5303SEric W. Biederman struct net *net, const void *pkey, 5141da177e4SLinus Torvalds struct net_device *dev, int creat) 5151da177e4SLinus Torvalds { 5161da177e4SLinus Torvalds struct pneigh_entry *n; 5171da177e4SLinus Torvalds int key_len = tbl->key_len; 518be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 521be01d655SYOSHIFUJI Hideaki n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], 522be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 523be01d655SYOSHIFUJI Hideaki read_unlock_bh(&tbl->lock); 5241da177e4SLinus Torvalds 525be01d655SYOSHIFUJI Hideaki if (n || !creat) 5261da177e4SLinus Torvalds goto out; 5271da177e4SLinus Torvalds 5284ae28944SPavel Emelyanov ASSERT_RTNL(); 5294ae28944SPavel Emelyanov 5301da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 5311da177e4SLinus Torvalds if (!n) 5321da177e4SLinus Torvalds goto out; 5331da177e4SLinus Torvalds 534e42ea986SEric Dumazet write_pnet(&n->net, hold_net(net)); 5351da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 5361da177e4SLinus Torvalds n->dev = dev; 5371da177e4SLinus Torvalds if (dev) 5381da177e4SLinus Torvalds dev_hold(dev); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 5411da177e4SLinus Torvalds if (dev) 5421da177e4SLinus Torvalds dev_put(dev); 543da12f735SDenis V. Lunev release_net(net); 5441da177e4SLinus Torvalds kfree(n); 5451da177e4SLinus Torvalds n = NULL; 5461da177e4SLinus Torvalds goto out; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 5501da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 5511da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 5521da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5531da177e4SLinus Torvalds out: 5541da177e4SLinus Torvalds return n; 5551da177e4SLinus Torvalds } 5560a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds 559426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 5601da177e4SLinus Torvalds struct net_device *dev) 5611da177e4SLinus Torvalds { 5621da177e4SLinus Torvalds struct pneigh_entry *n, **np; 5631da177e4SLinus Torvalds int key_len = tbl->key_len; 564be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 5671da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 5681da177e4SLinus Torvalds np = &n->next) { 569426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 570878628fbSYOSHIFUJI Hideaki net_eq(pneigh_net(n), net)) { 5711da177e4SLinus Torvalds *np = n->next; 5721da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5731da177e4SLinus Torvalds if (tbl->pdestructor) 5741da177e4SLinus Torvalds tbl->pdestructor(n); 5751da177e4SLinus Torvalds if (n->dev) 5761da177e4SLinus Torvalds dev_put(n->dev); 57757da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 5781da177e4SLinus Torvalds kfree(n); 5791da177e4SLinus Torvalds return 0; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5831da177e4SLinus Torvalds return -ENOENT; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds struct pneigh_entry *n, **np; 5891da177e4SLinus Torvalds u32 h; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 5921da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 5931da177e4SLinus Torvalds while ((n = *np) != NULL) { 5941da177e4SLinus Torvalds if (!dev || n->dev == dev) { 5951da177e4SLinus Torvalds *np = n->next; 5961da177e4SLinus Torvalds if (tbl->pdestructor) 5971da177e4SLinus Torvalds tbl->pdestructor(n); 5981da177e4SLinus Torvalds if (n->dev) 5991da177e4SLinus Torvalds dev_put(n->dev); 60057da52c1SYOSHIFUJI Hideaki release_net(pneigh_net(n)); 6011da177e4SLinus Torvalds kfree(n); 6021da177e4SLinus Torvalds continue; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds np = &n->next; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds return -ENOENT; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 61006f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 61106f0511dSDenis V. Lunev 61206f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 61306f0511dSDenis V. Lunev { 61406f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 61506f0511dSDenis V. Lunev neigh_parms_destroy(parms); 61606f0511dSDenis V. Lunev } 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* 6191da177e4SLinus Torvalds * neighbour must already be out of the table; 6201da177e4SLinus Torvalds * 6211da177e4SLinus Torvalds */ 6221da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 6231da177e4SLinus Torvalds { 6241da177e4SLinus Torvalds struct hh_cache *hh; 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds if (!neigh->dead) { 6291da177e4SLinus Torvalds printk(KERN_WARNING 6301da177e4SLinus Torvalds "Destroying alive neighbour %p\n", neigh); 6311da177e4SLinus Torvalds dump_stack(); 6321da177e4SLinus Torvalds return; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 6361da177e4SLinus Torvalds printk(KERN_WARNING "Impossible event.\n"); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds while ((hh = neigh->hh) != NULL) { 6391da177e4SLinus Torvalds neigh->hh = hh->hh_next; 6401da177e4SLinus Torvalds hh->hh_next = NULL; 6413644f0ceSStephen Hemminger 6423644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 6431da177e4SLinus Torvalds hh->hh_output = neigh_blackhole; 6443644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 6451da177e4SLinus Torvalds if (atomic_dec_and_test(&hh->hh_refcnt)) 6461da177e4SLinus Torvalds kfree(hh); 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds dev_put(neigh->dev); 6521da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 6571da177e4SLinus Torvalds kmem_cache_free(neigh->tbl->kmem_cachep, neigh); 6581da177e4SLinus Torvalds } 6590a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy); 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds /* Neighbour state is suspicious; 6621da177e4SLinus Torvalds disable fast path. 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds Called with write_locked neigh. 6651da177e4SLinus Torvalds */ 6661da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds struct hh_cache *hh; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds neigh->output = neigh->ops->output; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 6751da177e4SLinus Torvalds hh->hh_output = neigh->ops->output; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* Neighbour state is OK; 6791da177e4SLinus Torvalds enable fast path. 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds Called with write_locked neigh. 6821da177e4SLinus Torvalds */ 6831da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds struct hh_cache *hh; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is connected.\n", neigh); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 6921da177e4SLinus Torvalds hh->hh_output = neigh->ops->hh_output; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 695e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work) 6961da177e4SLinus Torvalds { 697e4c4e448SEric Dumazet struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); 6981da177e4SLinus Torvalds struct neighbour *n, **np; 699e4c4e448SEric Dumazet unsigned int i; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 7021da177e4SLinus Torvalds 703e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds /* 7061da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 7071da177e4SLinus Torvalds */ 7081da177e4SLinus Torvalds 709e4c4e448SEric Dumazet if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { 7101da177e4SLinus Torvalds struct neigh_parms *p; 711e4c4e448SEric Dumazet tbl->last_rand = jiffies; 7121da177e4SLinus Torvalds for (p = &tbl->parms; p; p = p->next) 7131da177e4SLinus Torvalds p->reachable_time = 7141da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 717e4c4e448SEric Dumazet for (i = 0 ; i <= tbl->hash_mask; i++) { 718e4c4e448SEric Dumazet np = &tbl->hash_buckets[i]; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds while ((n = *np) != NULL) { 7211da177e4SLinus Torvalds unsigned int state; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds write_lock(&n->lock); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds state = n->nud_state; 7261da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 7271da177e4SLinus Torvalds write_unlock(&n->lock); 7281da177e4SLinus Torvalds goto next_elt; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 7321da177e4SLinus Torvalds n->used = n->confirmed; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 7351da177e4SLinus Torvalds (state == NUD_FAILED || 736e4c4e448SEric Dumazet time_after(jiffies, n->used + n->parms->gc_staletime))) { 7371da177e4SLinus Torvalds *np = n->next; 7381da177e4SLinus Torvalds n->dead = 1; 7391da177e4SLinus Torvalds write_unlock(&n->lock); 7404f494554SThomas Graf neigh_cleanup_and_release(n); 7411da177e4SLinus Torvalds continue; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds write_unlock(&n->lock); 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds next_elt: 7461da177e4SLinus Torvalds np = &n->next; 7471da177e4SLinus Torvalds } 748e4c4e448SEric Dumazet /* 749e4c4e448SEric Dumazet * It's fine to release lock here, even if hash table 750e4c4e448SEric Dumazet * grows while we are preempted. 751e4c4e448SEric Dumazet */ 752e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 753e4c4e448SEric Dumazet cond_resched(); 754e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 755e4c4e448SEric Dumazet } 7561da177e4SLinus Torvalds /* Cycle through all hash buckets every base_reachable_time/2 ticks. 7571da177e4SLinus Torvalds * ARP entry timeouts range from 1/2 base_reachable_time to 3/2 7581da177e4SLinus Torvalds * base_reachable_time. 7591da177e4SLinus Torvalds */ 760e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, 761e4c4e448SEric Dumazet tbl->parms.base_reachable_time >> 1); 762e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 7681da177e4SLinus Torvalds return (n->nud_state & NUD_PROBE ? 7691da177e4SLinus Torvalds p->ucast_probes : 7701da177e4SLinus Torvalds p->ucast_probes + p->app_probes + p->mcast_probes); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7735ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh) 7745ef12d98STimo Teras { 7755ef12d98STimo Teras struct sk_buff *skb; 7765ef12d98STimo Teras 7775ef12d98STimo Teras NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 7785ef12d98STimo Teras NEIGH_PRINTK2("neigh %p is failed.\n", neigh); 7795ef12d98STimo Teras neigh->updated = jiffies; 7805ef12d98STimo Teras 7815ef12d98STimo Teras /* It is very thin place. report_unreachable is very complicated 7825ef12d98STimo Teras routine. Particularly, it can hit the same neighbour entry! 7835ef12d98STimo Teras 7845ef12d98STimo Teras So that, we try to be accurate and avoid dead loop. --ANK 7855ef12d98STimo Teras */ 7865ef12d98STimo Teras while (neigh->nud_state == NUD_FAILED && 7875ef12d98STimo Teras (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 7885ef12d98STimo Teras write_unlock(&neigh->lock); 7895ef12d98STimo Teras neigh->ops->error_report(neigh, skb); 7905ef12d98STimo Teras write_lock(&neigh->lock); 7915ef12d98STimo Teras } 7925ef12d98STimo Teras skb_queue_purge(&neigh->arp_queue); 7935ef12d98STimo Teras } 7945ef12d98STimo Teras 7951da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 7981da177e4SLinus Torvalds { 7991da177e4SLinus Torvalds unsigned long now, next; 8001da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 8011da177e4SLinus Torvalds unsigned state; 8021da177e4SLinus Torvalds int notify = 0; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds write_lock(&neigh->lock); 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds state = neigh->nud_state; 8071da177e4SLinus Torvalds now = jiffies; 8081da177e4SLinus Torvalds next = now + HZ; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds if (!(state & NUD_IN_TIMER)) { 8111da177e4SLinus Torvalds #ifndef CONFIG_SMP 8121da177e4SLinus Torvalds printk(KERN_WARNING "neigh: timer & !nud_in_timer\n"); 8131da177e4SLinus Torvalds #endif 8141da177e4SLinus Torvalds goto out; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 8181da177e4SLinus Torvalds if (time_before_eq(now, 8191da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 8201da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is still alive.\n", neigh); 8211da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 8221da177e4SLinus Torvalds } else if (time_before_eq(now, 8231da177e4SLinus Torvalds neigh->used + neigh->parms->delay_probe_time)) { 8241da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 8251da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 826955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8271da177e4SLinus Torvalds neigh_suspect(neigh); 8281da177e4SLinus Torvalds next = now + neigh->parms->delay_probe_time; 8291da177e4SLinus Torvalds } else { 8301da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 8311da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 832955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8331da177e4SLinus Torvalds neigh_suspect(neigh); 8348d71740cSTom Tucker notify = 1; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 8371da177e4SLinus Torvalds if (time_before_eq(now, 8381da177e4SLinus Torvalds neigh->confirmed + neigh->parms->delay_probe_time)) { 8391da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh); 8401da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 841955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8421da177e4SLinus Torvalds neigh_connect(neigh); 8438d71740cSTom Tucker notify = 1; 8441da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 8451da177e4SLinus Torvalds } else { 8461da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is probed.\n", neigh); 8471da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 848955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8491da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 8501da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds } else { 8531da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 8541da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 8581da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 8591da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 8601da177e4SLinus Torvalds notify = 1; 8615ef12d98STimo Teras neigh_invalidate(neigh); 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 8651da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 8661da177e4SLinus Torvalds next = jiffies + HZ/2; 8676fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 8686fb9974fSHerbert Xu neigh_hold(neigh); 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 8711da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&neigh->arp_queue); 8729ff56607SDavid S. Miller /* keep skb alive even if arp_queue overflows */ 8739ff56607SDavid S. Miller if (skb) 8747e36763bSFrank Blaschka skb = skb_copy(skb, GFP_ATOMIC); 8759ff56607SDavid S. Miller write_unlock(&neigh->lock); 8761da177e4SLinus Torvalds neigh->ops->solicit(neigh, skb); 8771da177e4SLinus Torvalds atomic_inc(&neigh->probes); 8789ff56607SDavid S. Miller kfree_skb(skb); 8799ff56607SDavid S. Miller } else { 8801da177e4SLinus Torvalds out: 8811da177e4SLinus Torvalds write_unlock(&neigh->lock); 8829ff56607SDavid S. Miller } 8831da177e4SLinus Torvalds 884d961db35SThomas Graf if (notify) 885d961db35SThomas Graf neigh_update_notify(neigh); 886d961db35SThomas Graf 8871da177e4SLinus Torvalds neigh_release(neigh); 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 8911da177e4SLinus Torvalds { 8921da177e4SLinus Torvalds int rc; 8931da177e4SLinus Torvalds unsigned long now; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds rc = 0; 8981da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 8991da177e4SLinus Torvalds goto out_unlock_bh; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds now = jiffies; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 9041da177e4SLinus Torvalds if (neigh->parms->mcast_probes + neigh->parms->app_probes) { 9051da177e4SLinus Torvalds atomic_set(&neigh->probes, neigh->parms->ucast_probes); 9061da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 907955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 908667347f1SDavid S. Miller neigh_add_timer(neigh, now + 1); 9091da177e4SLinus Torvalds } else { 9101da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 911955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9121da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds kfree_skb(skb); 9151da177e4SLinus Torvalds return 1; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 9181da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 9191da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 920955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 921667347f1SDavid S. Miller neigh_add_timer(neigh, 922667347f1SDavid S. Miller jiffies + neigh->parms->delay_probe_time); 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 9261da177e4SLinus Torvalds if (skb) { 9271da177e4SLinus Torvalds if (skb_queue_len(&neigh->arp_queue) >= 9281da177e4SLinus Torvalds neigh->parms->queue_len) { 9291da177e4SLinus Torvalds struct sk_buff *buff; 930f72051b0SDavid S. Miller buff = __skb_dequeue(&neigh->arp_queue); 9311da177e4SLinus Torvalds kfree_skb(buff); 9329a6d276eSNeil Horman NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds rc = 1; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds out_unlock_bh: 9391da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9401da177e4SLinus Torvalds return rc; 9411da177e4SLinus Torvalds } 9420a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send); 9431da177e4SLinus Torvalds 944e92b43a3SStephen Hemminger static void neigh_update_hhs(struct neighbour *neigh) 9451da177e4SLinus Torvalds { 9461da177e4SLinus Torvalds struct hh_cache *hh; 9473b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 9483b04dddeSStephen Hemminger = neigh->dev->header_ops->cache_update; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds if (update) { 9511da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) { 9523644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 9531da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 9543644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* Generic update routine. 9621da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 9631da177e4SLinus Torvalds -- new is new state. 9641da177e4SLinus Torvalds -- flags 9651da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 9661da177e4SLinus Torvalds if it is different. 9671da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 9681da177e4SLinus Torvalds lladdr instead of overriding it 9691da177e4SLinus Torvalds if it is different. 9701da177e4SLinus Torvalds It also allows to retain current state 9711da177e4SLinus Torvalds if lladdr is unchanged. 9721da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 9751da177e4SLinus Torvalds NTF_ROUTER flag. 9761da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 9771da177e4SLinus Torvalds a router. 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 9801da177e4SLinus Torvalds */ 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 9831da177e4SLinus Torvalds u32 flags) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds u8 old; 9861da177e4SLinus Torvalds int err; 9871da177e4SLinus Torvalds int notify = 0; 9881da177e4SLinus Torvalds struct net_device *dev; 9891da177e4SLinus Torvalds int update_isrouter = 0; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds dev = neigh->dev; 9941da177e4SLinus Torvalds old = neigh->nud_state; 9951da177e4SLinus Torvalds err = -EPERM; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 9981da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 9991da177e4SLinus Torvalds goto out; 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 10021da177e4SLinus Torvalds neigh_del_timer(neigh); 10031da177e4SLinus Torvalds if (old & NUD_CONNECTED) 10041da177e4SLinus Torvalds neigh_suspect(neigh); 10051da177e4SLinus Torvalds neigh->nud_state = new; 10061da177e4SLinus Torvalds err = 0; 10071da177e4SLinus Torvalds notify = old & NUD_VALID; 10085ef12d98STimo Teras if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && 10095ef12d98STimo Teras (new & NUD_FAILED)) { 10105ef12d98STimo Teras neigh_invalidate(neigh); 10115ef12d98STimo Teras notify = 1; 10125ef12d98STimo Teras } 10131da177e4SLinus Torvalds goto out; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 10171da177e4SLinus Torvalds if (!dev->addr_len) { 10181da177e4SLinus Torvalds /* First case: device needs no address. */ 10191da177e4SLinus Torvalds lladdr = neigh->ha; 10201da177e4SLinus Torvalds } else if (lladdr) { 10211da177e4SLinus Torvalds /* The second case: if something is already cached 10221da177e4SLinus Torvalds and a new address is proposed: 10231da177e4SLinus Torvalds - compare new & old 10241da177e4SLinus Torvalds - if they are different, check override flag 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds if ((old & NUD_VALID) && 10271da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 10281da177e4SLinus Torvalds lladdr = neigh->ha; 10291da177e4SLinus Torvalds } else { 10301da177e4SLinus Torvalds /* No address is supplied; if we know something, 10311da177e4SLinus Torvalds use it, otherwise discard the request. 10321da177e4SLinus Torvalds */ 10331da177e4SLinus Torvalds err = -EINVAL; 10341da177e4SLinus Torvalds if (!(old & NUD_VALID)) 10351da177e4SLinus Torvalds goto out; 10361da177e4SLinus Torvalds lladdr = neigh->ha; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds if (new & NUD_CONNECTED) 10401da177e4SLinus Torvalds neigh->confirmed = jiffies; 10411da177e4SLinus Torvalds neigh->updated = jiffies; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /* If entry was valid and address is not changed, 10441da177e4SLinus Torvalds do not change entry state, if new one is STALE. 10451da177e4SLinus Torvalds */ 10461da177e4SLinus Torvalds err = 0; 10471da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 10481da177e4SLinus Torvalds if (old & NUD_VALID) { 10491da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 10501da177e4SLinus Torvalds update_isrouter = 0; 10511da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 10521da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 10531da177e4SLinus Torvalds lladdr = neigh->ha; 10541da177e4SLinus Torvalds new = NUD_STALE; 10551da177e4SLinus Torvalds } else 10561da177e4SLinus Torvalds goto out; 10571da177e4SLinus Torvalds } else { 10581da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 10591da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 10601da177e4SLinus Torvalds (old & NUD_CONNECTED)) 10611da177e4SLinus Torvalds ) 10621da177e4SLinus Torvalds new = old; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds if (new != old) { 10671da177e4SLinus Torvalds neigh_del_timer(neigh); 1068a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1069667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 10701da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1071667347f1SDavid S. Miller neigh->parms->reachable_time : 1072667347f1SDavid S. Miller 0))); 10731da177e4SLinus Torvalds neigh->nud_state = new; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds if (lladdr != neigh->ha) { 10771da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 10781da177e4SLinus Torvalds neigh_update_hhs(neigh); 10791da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 10801da177e4SLinus Torvalds neigh->confirmed = jiffies - 10811da177e4SLinus Torvalds (neigh->parms->base_reachable_time << 1); 10821da177e4SLinus Torvalds notify = 1; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds if (new == old) 10851da177e4SLinus Torvalds goto out; 10861da177e4SLinus Torvalds if (new & NUD_CONNECTED) 10871da177e4SLinus Torvalds neigh_connect(neigh); 10881da177e4SLinus Torvalds else 10891da177e4SLinus Torvalds neigh_suspect(neigh); 10901da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 10911da177e4SLinus Torvalds struct sk_buff *skb; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 10961da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 10971da177e4SLinus Torvalds struct neighbour *n1 = neigh; 10981da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 10991da177e4SLinus Torvalds /* On shaper/eql skb->dst->neighbour != neigh :( */ 1100adf30907SEric Dumazet if (skb_dst(skb) && skb_dst(skb)->neighbour) 1101adf30907SEric Dumazet n1 = skb_dst(skb)->neighbour; 11021da177e4SLinus Torvalds n1->output(skb); 11031da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds out: 11081da177e4SLinus Torvalds if (update_isrouter) { 11091da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 11101da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 11111da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 11148d71740cSTom Tucker 11158d71740cSTom Tucker if (notify) 1116d961db35SThomas Graf neigh_update_notify(neigh); 1117d961db35SThomas Graf 11181da177e4SLinus Torvalds return err; 11191da177e4SLinus Torvalds } 11200a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update); 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 11231da177e4SLinus Torvalds u8 *lladdr, void *saddr, 11241da177e4SLinus Torvalds struct net_device *dev) 11251da177e4SLinus Torvalds { 11261da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 11271da177e4SLinus Torvalds lladdr || !dev->addr_len); 11281da177e4SLinus Torvalds if (neigh) 11291da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 11301da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 11311da177e4SLinus Torvalds return neigh; 11321da177e4SLinus Torvalds } 11330a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, 1136d77072ecSAl Viro __be16 protocol) 11371da177e4SLinus Torvalds { 11381da177e4SLinus Torvalds struct hh_cache *hh; 11391da177e4SLinus Torvalds struct net_device *dev = dst->dev; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds for (hh = n->hh; hh; hh = hh->hh_next) 11421da177e4SLinus Torvalds if (hh->hh_type == protocol) 11431da177e4SLinus Torvalds break; 11441da177e4SLinus Torvalds 114577d04bd9SAndrew Morton if (!hh && (hh = kzalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) { 11463644f0ceSStephen Hemminger seqlock_init(&hh->hh_lock); 11471da177e4SLinus Torvalds hh->hh_type = protocol; 11481da177e4SLinus Torvalds atomic_set(&hh->hh_refcnt, 0); 11491da177e4SLinus Torvalds hh->hh_next = NULL; 11503b04dddeSStephen Hemminger 11513b04dddeSStephen Hemminger if (dev->header_ops->cache(n, hh)) { 11521da177e4SLinus Torvalds kfree(hh); 11531da177e4SLinus Torvalds hh = NULL; 11541da177e4SLinus Torvalds } else { 11551da177e4SLinus Torvalds atomic_inc(&hh->hh_refcnt); 11561da177e4SLinus Torvalds hh->hh_next = n->hh; 11571da177e4SLinus Torvalds n->hh = hh; 11581da177e4SLinus Torvalds if (n->nud_state & NUD_CONNECTED) 11591da177e4SLinus Torvalds hh->hh_output = n->ops->hh_output; 11601da177e4SLinus Torvalds else 11611da177e4SLinus Torvalds hh->hh_output = n->ops->output; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds if (hh) { 11651da177e4SLinus Torvalds atomic_inc(&hh->hh_refcnt); 11661da177e4SLinus Torvalds dst->hh = hh; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit 11711da177e4SLinus Torvalds worked, f.e. if you want to override normal output path (eql, shaper), 11721da177e4SLinus Torvalds but resolution is not made yet. 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds int neigh_compat_output(struct sk_buff *skb) 11761da177e4SLinus Torvalds { 11771da177e4SLinus Torvalds struct net_device *dev = skb->dev; 11781da177e4SLinus Torvalds 1179bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 11801da177e4SLinus Torvalds 11810c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 11821da177e4SLinus Torvalds skb->len) < 0 && 11833b04dddeSStephen Hemminger dev->header_ops->rebuild(skb)) 11841da177e4SLinus Torvalds return 0; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds return dev_queue_xmit(skb); 11871da177e4SLinus Torvalds } 11880a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_compat_output); 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds /* Slow and careful. */ 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds int neigh_resolve_output(struct sk_buff *skb) 11931da177e4SLinus Torvalds { 1194adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 11951da177e4SLinus Torvalds struct neighbour *neigh; 11961da177e4SLinus Torvalds int rc = 0; 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds if (!dst || !(neigh = dst->neighbour)) 11991da177e4SLinus Torvalds goto discard; 12001da177e4SLinus Torvalds 1201bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 12041da177e4SLinus Torvalds int err; 12051da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 12063b04dddeSStephen Hemminger if (dev->header_ops->cache && !dst->hh) { 12071da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 12081da177e4SLinus Torvalds if (!dst->hh) 12091da177e4SLinus Torvalds neigh_hh_init(neigh, dst, dst->ops->protocol); 12100c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12111da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12121da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 12131da177e4SLinus Torvalds } else { 12141da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 12150c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12161da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12171da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds if (err >= 0) 12201da177e4SLinus Torvalds rc = neigh->ops->queue_xmit(skb); 12211da177e4SLinus Torvalds else 12221da177e4SLinus Torvalds goto out_kfree_skb; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds out: 12251da177e4SLinus Torvalds return rc; 12261da177e4SLinus Torvalds discard: 12271da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", 12281da177e4SLinus Torvalds dst, dst ? dst->neighbour : NULL); 12291da177e4SLinus Torvalds out_kfree_skb: 12301da177e4SLinus Torvalds rc = -EINVAL; 12311da177e4SLinus Torvalds kfree_skb(skb); 12321da177e4SLinus Torvalds goto out; 12331da177e4SLinus Torvalds } 12340a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /* As fast as possible without hh cache */ 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds int neigh_connected_output(struct sk_buff *skb) 12391da177e4SLinus Torvalds { 12401da177e4SLinus Torvalds int err; 1241adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 12421da177e4SLinus Torvalds struct neighbour *neigh = dst->neighbour; 12431da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 12441da177e4SLinus Torvalds 1245bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 12480c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12491da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12501da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 12511da177e4SLinus Torvalds if (err >= 0) 12521da177e4SLinus Torvalds err = neigh->ops->queue_xmit(skb); 12531da177e4SLinus Torvalds else { 12541da177e4SLinus Torvalds err = -EINVAL; 12551da177e4SLinus Torvalds kfree_skb(skb); 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds return err; 12581da177e4SLinus Torvalds } 12590a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 12621da177e4SLinus Torvalds { 12631da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 12641da177e4SLinus Torvalds long sched_next = 0; 12651da177e4SLinus Torvalds unsigned long now = jiffies; 1266f72051b0SDavid S. Miller struct sk_buff *skb, *n; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 12691da177e4SLinus Torvalds 1270f72051b0SDavid S. Miller skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { 1271f72051b0SDavid S. Miller long tdif = NEIGH_CB(skb)->sched_next - now; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds if (tdif <= 0) { 1274f72051b0SDavid S. Miller struct net_device *dev = skb->dev; 1275f72051b0SDavid S. Miller __skb_unlink(skb, &tbl->proxy_queue); 12761da177e4SLinus Torvalds if (tbl->proxy_redo && netif_running(dev)) 1277f72051b0SDavid S. Miller tbl->proxy_redo(skb); 12781da177e4SLinus Torvalds else 1279f72051b0SDavid S. Miller kfree_skb(skb); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds dev_put(dev); 12821da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 12831da177e4SLinus Torvalds sched_next = tdif; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 12861da177e4SLinus Torvalds if (sched_next) 12871da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 12881da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 12921da177e4SLinus Torvalds struct sk_buff *skb) 12931da177e4SLinus Torvalds { 12941da177e4SLinus Torvalds unsigned long now = jiffies; 12951da177e4SLinus Torvalds unsigned long sched_next = now + (net_random() % p->proxy_delay); 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds if (tbl->proxy_queue.qlen > p->proxy_qlen) { 12981da177e4SLinus Torvalds kfree_skb(skb); 12991da177e4SLinus Torvalds return; 13001da177e4SLinus Torvalds } 1301a61bbcf2SPatrick McHardy 1302a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1303a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13061da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 13071da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 13081da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 13091da177e4SLinus Torvalds } 1310adf30907SEric Dumazet skb_dst_drop(skb); 13111da177e4SLinus Torvalds dev_hold(skb->dev); 13121da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 13131da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 13141da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13151da177e4SLinus Torvalds } 13160a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue); 13171da177e4SLinus Torvalds 131897fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, 1319426b5303SEric W. Biederman struct net *net, int ifindex) 1320426b5303SEric W. Biederman { 1321426b5303SEric W. Biederman struct neigh_parms *p; 1322426b5303SEric W. Biederman 1323426b5303SEric W. Biederman for (p = &tbl->parms; p; p = p->next) { 1324878628fbSYOSHIFUJI Hideaki if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || 1325426b5303SEric W. Biederman (!p->dev && !ifindex)) 1326426b5303SEric W. Biederman return p; 1327426b5303SEric W. Biederman } 1328426b5303SEric W. Biederman 1329426b5303SEric W. Biederman return NULL; 1330426b5303SEric W. Biederman } 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 13331da177e4SLinus Torvalds struct neigh_table *tbl) 13341da177e4SLinus Torvalds { 1335426b5303SEric W. Biederman struct neigh_parms *p, *ref; 133600829823SStephen Hemminger struct net *net = dev_net(dev); 133700829823SStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 13381da177e4SLinus Torvalds 133997fd5bc7STobias Klauser ref = lookup_neigh_parms(tbl, net, 0); 1340426b5303SEric W. Biederman if (!ref) 1341426b5303SEric W. Biederman return NULL; 1342426b5303SEric W. Biederman 1343426b5303SEric W. Biederman p = kmemdup(ref, sizeof(*p), GFP_KERNEL); 13441da177e4SLinus Torvalds if (p) { 13451da177e4SLinus Torvalds p->tbl = tbl; 13461da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 13471da177e4SLinus Torvalds p->reachable_time = 13481da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 1349486b51d3SDenis V. Lunev 135000829823SStephen Hemminger if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 13511da177e4SLinus Torvalds kfree(p); 13521da177e4SLinus Torvalds return NULL; 13531da177e4SLinus Torvalds } 1354c7fb64dbSThomas Graf 1355c7fb64dbSThomas Graf dev_hold(dev); 1356c7fb64dbSThomas Graf p->dev = dev; 1357e42ea986SEric Dumazet write_pnet(&p->net, hold_net(net)); 13581da177e4SLinus Torvalds p->sysctl_table = NULL; 13591da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 13601da177e4SLinus Torvalds p->next = tbl->parms.next; 13611da177e4SLinus Torvalds tbl->parms.next = p; 13621da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds return p; 13651da177e4SLinus Torvalds } 13660a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc); 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct neigh_parms *parms = 13711da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds neigh_parms_put(parms); 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 13771da177e4SLinus Torvalds { 13781da177e4SLinus Torvalds struct neigh_parms **p; 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 13811da177e4SLinus Torvalds return; 13821da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 13831da177e4SLinus Torvalds for (p = &tbl->parms.next; *p; p = &(*p)->next) { 13841da177e4SLinus Torvalds if (*p == parms) { 13851da177e4SLinus Torvalds *p = parms->next; 13861da177e4SLinus Torvalds parms->dead = 1; 13871da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1388cecbb639SDavid S. Miller if (parms->dev) 1389cecbb639SDavid S. Miller dev_put(parms->dev); 13901da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 13911da177e4SLinus Torvalds return; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 13951da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_parms_release: not found\n"); 13961da177e4SLinus Torvalds } 13970a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release); 13981da177e4SLinus Torvalds 139906f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 14001da177e4SLinus Torvalds { 140157da52c1SYOSHIFUJI Hideaki release_net(neigh_parms_net(parms)); 14021da177e4SLinus Torvalds kfree(parms); 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 1405c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1406c2ecba71SPavel Emelianov 1407bd89efc5SSimon Kelley void neigh_table_init_no_netlink(struct neigh_table *tbl) 14081da177e4SLinus Torvalds { 14091da177e4SLinus Torvalds unsigned long now = jiffies; 14101da177e4SLinus Torvalds unsigned long phsize; 14111da177e4SLinus Torvalds 1412e42ea986SEric Dumazet write_pnet(&tbl->parms.net, &init_net); 14131da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 14141da177e4SLinus Torvalds tbl->parms.reachable_time = 14151da177e4SLinus Torvalds neigh_rand_reach_time(tbl->parms.base_reachable_time); 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds if (!tbl->kmem_cachep) 1418e5d679f3SAlexey Dobriyan tbl->kmem_cachep = 1419e5d679f3SAlexey Dobriyan kmem_cache_create(tbl->id, tbl->entry_size, 0, 1420e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 142120c2df83SPaul Mundt NULL); 14221da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 14231da177e4SLinus Torvalds if (!tbl->stats) 14241da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 14279b739ba5SAlexey Dobriyan if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat, 14289b739ba5SAlexey Dobriyan &neigh_stat_seq_fops, tbl)) 14291da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 14301da177e4SLinus Torvalds #endif 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds tbl->hash_mask = 1; 14331da177e4SLinus Torvalds tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1); 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 143677d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds if (!tbl->hash_buckets || !tbl->phash_buckets) 14391da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1444e4c4e448SEric Dumazet INIT_DELAYED_WORK_DEFERRABLE(&tbl->gc_work, neigh_periodic_work); 1445e4c4e448SEric Dumazet schedule_delayed_work(&tbl->gc_work, tbl->parms.reachable_time); 1446b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1447c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1448c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds tbl->last_flush = now; 14511da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1452bd89efc5SSimon Kelley } 14530a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init_no_netlink); 1454bd89efc5SSimon Kelley 1455bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl) 1456bd89efc5SSimon Kelley { 1457bd89efc5SSimon Kelley struct neigh_table *tmp; 1458bd89efc5SSimon Kelley 1459bd89efc5SSimon Kelley neigh_table_init_no_netlink(tbl); 14601da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 1461bd89efc5SSimon Kelley for (tmp = neigh_tables; tmp; tmp = tmp->next) { 1462bd89efc5SSimon Kelley if (tmp->family == tbl->family) 1463bd89efc5SSimon Kelley break; 1464bd89efc5SSimon Kelley } 14651da177e4SLinus Torvalds tbl->next = neigh_tables; 14661da177e4SLinus Torvalds neigh_tables = tbl; 14671da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 1468bd89efc5SSimon Kelley 1469bd89efc5SSimon Kelley if (unlikely(tmp)) { 1470bd89efc5SSimon Kelley printk(KERN_ERR "NEIGH: Registering multiple tables for " 1471bd89efc5SSimon Kelley "family %d\n", tbl->family); 1472bd89efc5SSimon Kelley dump_stack(); 1473bd89efc5SSimon Kelley } 14741da177e4SLinus Torvalds } 14750a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init); 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl) 14781da177e4SLinus Torvalds { 14791da177e4SLinus Torvalds struct neigh_table **tp; 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 1482e4c4e448SEric Dumazet cancel_delayed_work(&tbl->gc_work); 1483e4c4e448SEric Dumazet flush_scheduled_work(); 14841da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 14851da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 14861da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 14871da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 14881da177e4SLinus Torvalds printk(KERN_CRIT "neighbour leakage\n"); 14891da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 14901da177e4SLinus Torvalds for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { 14911da177e4SLinus Torvalds if (*tp == tbl) { 14921da177e4SLinus Torvalds *tp = tbl->next; 14931da177e4SLinus Torvalds break; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1); 14991da177e4SLinus Torvalds tbl->hash_buckets = NULL; 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds kfree(tbl->phash_buckets); 15021da177e4SLinus Torvalds tbl->phash_buckets = NULL; 15031da177e4SLinus Torvalds 15043f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 15053f192b5cSAlexey Dobriyan 15063fcde74bSKirill Korotaev free_percpu(tbl->stats); 15073fcde74bSKirill Korotaev tbl->stats = NULL; 15083fcde74bSKirill Korotaev 1509bfb85c9fSRandy Dunlap kmem_cache_destroy(tbl->kmem_cachep); 1510bfb85c9fSRandy Dunlap tbl->kmem_cachep = NULL; 1511bfb85c9fSRandy Dunlap 15121da177e4SLinus Torvalds return 0; 15131da177e4SLinus Torvalds } 15140a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear); 15151da177e4SLinus Torvalds 1516c8822a4eSThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 15171da177e4SLinus Torvalds { 15183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1519a14a49d2SThomas Graf struct ndmsg *ndm; 1520a14a49d2SThomas Graf struct nlattr *dst_attr; 15211da177e4SLinus Torvalds struct neigh_table *tbl; 15221da177e4SLinus Torvalds struct net_device *dev = NULL; 1523a14a49d2SThomas Graf int err = -EINVAL; 15241da177e4SLinus Torvalds 1525a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 15261da177e4SLinus Torvalds goto out; 15271da177e4SLinus Torvalds 1528a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1529a14a49d2SThomas Graf if (dst_attr == NULL) 1530a14a49d2SThomas Graf goto out; 1531a14a49d2SThomas Graf 1532a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1533a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1534881d966bSEric W. Biederman dev = dev_get_by_index(net, ndm->ndm_ifindex); 1535a14a49d2SThomas Graf if (dev == NULL) { 1536a14a49d2SThomas Graf err = -ENODEV; 1537a14a49d2SThomas Graf goto out; 1538a14a49d2SThomas Graf } 1539a14a49d2SThomas Graf } 1540a14a49d2SThomas Graf 15411da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 15421da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1543a14a49d2SThomas Graf struct neighbour *neigh; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 15461da177e4SLinus Torvalds continue; 15471da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 15481da177e4SLinus Torvalds 1549a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 15501da177e4SLinus Torvalds goto out_dev_put; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1553426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 15541da177e4SLinus Torvalds goto out_dev_put; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 1557a14a49d2SThomas Graf if (dev == NULL) 1558a14a49d2SThomas Graf goto out_dev_put; 15591da177e4SLinus Torvalds 1560a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1561a14a49d2SThomas Graf if (neigh == NULL) { 1562a14a49d2SThomas Graf err = -ENOENT; 1563a14a49d2SThomas Graf goto out_dev_put; 1564a14a49d2SThomas Graf } 1565a14a49d2SThomas Graf 1566a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 15671da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 15681da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1569a14a49d2SThomas Graf neigh_release(neigh); 15701da177e4SLinus Torvalds goto out_dev_put; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 1573a14a49d2SThomas Graf err = -EAFNOSUPPORT; 1574a14a49d2SThomas Graf 15751da177e4SLinus Torvalds out_dev_put: 15761da177e4SLinus Torvalds if (dev) 15771da177e4SLinus Torvalds dev_put(dev); 15781da177e4SLinus Torvalds out: 15791da177e4SLinus Torvalds return err; 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 1582c8822a4eSThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 15831da177e4SLinus Torvalds { 15843b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 15855208debdSThomas Graf struct ndmsg *ndm; 15865208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 15871da177e4SLinus Torvalds struct neigh_table *tbl; 15881da177e4SLinus Torvalds struct net_device *dev = NULL; 15895208debdSThomas Graf int err; 15901da177e4SLinus Torvalds 15915208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 15925208debdSThomas Graf if (err < 0) 15931da177e4SLinus Torvalds goto out; 15941da177e4SLinus Torvalds 15955208debdSThomas Graf err = -EINVAL; 15965208debdSThomas Graf if (tb[NDA_DST] == NULL) 15975208debdSThomas Graf goto out; 15985208debdSThomas Graf 15995208debdSThomas Graf ndm = nlmsg_data(nlh); 16005208debdSThomas Graf if (ndm->ndm_ifindex) { 1601881d966bSEric W. Biederman dev = dev_get_by_index(net, ndm->ndm_ifindex); 16025208debdSThomas Graf if (dev == NULL) { 16035208debdSThomas Graf err = -ENODEV; 16045208debdSThomas Graf goto out; 16055208debdSThomas Graf } 16065208debdSThomas Graf 16075208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 16085208debdSThomas Graf goto out_dev_put; 16095208debdSThomas Graf } 16105208debdSThomas Graf 16111da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 16121da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 16135208debdSThomas Graf int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 16145208debdSThomas Graf struct neighbour *neigh; 16155208debdSThomas Graf void *dst, *lladdr; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 16181da177e4SLinus Torvalds continue; 16191da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16201da177e4SLinus Torvalds 16215208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 16221da177e4SLinus Torvalds goto out_dev_put; 16235208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 16245208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 162762dd9318SVille Nuorvala struct pneigh_entry *pn; 162862dd9318SVille Nuorvala 16295208debdSThomas Graf err = -ENOBUFS; 1630426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 163162dd9318SVille Nuorvala if (pn) { 163262dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 163362dd9318SVille Nuorvala err = 0; 163462dd9318SVille Nuorvala } 16351da177e4SLinus Torvalds goto out_dev_put; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16385208debdSThomas Graf if (dev == NULL) 16391da177e4SLinus Torvalds goto out_dev_put; 16401da177e4SLinus Torvalds 16415208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 16425208debdSThomas Graf if (neigh == NULL) { 16435208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 16441da177e4SLinus Torvalds err = -ENOENT; 16451da177e4SLinus Torvalds goto out_dev_put; 16465208debdSThomas Graf } 16475208debdSThomas Graf 16485208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 16495208debdSThomas Graf if (IS_ERR(neigh)) { 16505208debdSThomas Graf err = PTR_ERR(neigh); 16511da177e4SLinus Torvalds goto out_dev_put; 16521da177e4SLinus Torvalds } 16535208debdSThomas Graf } else { 16545208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 16555208debdSThomas Graf err = -EEXIST; 16565208debdSThomas Graf neigh_release(neigh); 16575208debdSThomas Graf goto out_dev_put; 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16605208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 16615208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 16625208debdSThomas Graf } 16631da177e4SLinus Torvalds 16640c5c2d30SEric Biederman if (ndm->ndm_flags & NTF_USE) { 16650c5c2d30SEric Biederman neigh_event_send(neigh, NULL); 16660c5c2d30SEric Biederman err = 0; 16670c5c2d30SEric Biederman } else 16685208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 16695208debdSThomas Graf neigh_release(neigh); 16701da177e4SLinus Torvalds goto out_dev_put; 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16745208debdSThomas Graf err = -EAFNOSUPPORT; 16755208debdSThomas Graf 16761da177e4SLinus Torvalds out_dev_put: 16771da177e4SLinus Torvalds if (dev) 16781da177e4SLinus Torvalds dev_put(dev); 16791da177e4SLinus Torvalds out: 16801da177e4SLinus Torvalds return err; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 1683c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1684c7fb64dbSThomas Graf { 1685ca860fb3SThomas Graf struct nlattr *nest; 1686e386c6ebSThomas Graf 1687ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1688ca860fb3SThomas Graf if (nest == NULL) 1689ca860fb3SThomas Graf return -ENOBUFS; 1690c7fb64dbSThomas Graf 1691c7fb64dbSThomas Graf if (parms->dev) 1692ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex); 1693c7fb64dbSThomas Graf 1694ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)); 1695ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len); 1696ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen); 1697ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes); 1698ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes); 1699ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes); 1700ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time); 1701ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME, 1702c7fb64dbSThomas Graf parms->base_reachable_time); 1703ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime); 1704ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time); 1705ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time); 1706ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay); 1707ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay); 1708ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime); 1709c7fb64dbSThomas Graf 1710ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1711c7fb64dbSThomas Graf 1712ca860fb3SThomas Graf nla_put_failure: 1713bc3ed28cSThomas Graf nla_nest_cancel(skb, nest); 1714bc3ed28cSThomas Graf return -EMSGSIZE; 1715c7fb64dbSThomas Graf } 1716c7fb64dbSThomas Graf 1717ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1718ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1719c7fb64dbSThomas Graf { 1720c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1721c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1722c7fb64dbSThomas Graf 1723ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1724ca860fb3SThomas Graf if (nlh == NULL) 172526932566SPatrick McHardy return -EMSGSIZE; 1726c7fb64dbSThomas Graf 1727ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1728c7fb64dbSThomas Graf 1729c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1730c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 17319ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 17329ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1733c7fb64dbSThomas Graf 1734ca860fb3SThomas Graf NLA_PUT_STRING(skb, NDTA_NAME, tbl->id); 1735ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval); 1736ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1); 1737ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2); 1738ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3); 1739c7fb64dbSThomas Graf 1740c7fb64dbSThomas Graf { 1741c7fb64dbSThomas Graf unsigned long now = jiffies; 1742c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1743c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1744c7fb64dbSThomas Graf 1745c7fb64dbSThomas Graf struct ndt_config ndc = { 1746c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1747c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1748c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1749c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1750c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1751c7fb64dbSThomas Graf .ndtc_hash_rnd = tbl->hash_rnd, 1752c7fb64dbSThomas Graf .ndtc_hash_mask = tbl->hash_mask, 1753c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1754c7fb64dbSThomas Graf }; 1755c7fb64dbSThomas Graf 1756ca860fb3SThomas Graf NLA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc); 1757c7fb64dbSThomas Graf } 1758c7fb64dbSThomas Graf 1759c7fb64dbSThomas Graf { 1760c7fb64dbSThomas Graf int cpu; 1761c7fb64dbSThomas Graf struct ndt_stats ndst; 1762c7fb64dbSThomas Graf 1763c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1764c7fb64dbSThomas Graf 17656f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1766c7fb64dbSThomas Graf struct neigh_statistics *st; 1767c7fb64dbSThomas Graf 1768c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1769c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1770c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1771c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1772c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1773c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1774c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1775c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1776c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1777c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1778c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1779c7fb64dbSThomas Graf } 1780c7fb64dbSThomas Graf 1781ca860fb3SThomas Graf NLA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst); 1782c7fb64dbSThomas Graf } 1783c7fb64dbSThomas Graf 1784c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1785c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1786ca860fb3SThomas Graf goto nla_put_failure; 1787c7fb64dbSThomas Graf 1788c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1789ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1790c7fb64dbSThomas Graf 1791ca860fb3SThomas Graf nla_put_failure: 1792c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 179326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 179426932566SPatrick McHardy return -EMSGSIZE; 1795c7fb64dbSThomas Graf } 1796c7fb64dbSThomas Graf 1797ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1798ca860fb3SThomas Graf struct neigh_table *tbl, 1799c7fb64dbSThomas Graf struct neigh_parms *parms, 1800ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1801ca860fb3SThomas Graf unsigned int flags) 1802c7fb64dbSThomas Graf { 1803c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1804c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1805c7fb64dbSThomas Graf 1806ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1807ca860fb3SThomas Graf if (nlh == NULL) 180826932566SPatrick McHardy return -EMSGSIZE; 1809c7fb64dbSThomas Graf 1810ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1811c7fb64dbSThomas Graf 1812c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1813c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 18149ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 18159ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1816c7fb64dbSThomas Graf 1817ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1818ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1819ca860fb3SThomas Graf goto errout; 1820c7fb64dbSThomas Graf 1821c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1822ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1823ca860fb3SThomas Graf errout: 1824c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 182526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 182626932566SPatrick McHardy return -EMSGSIZE; 1827c7fb64dbSThomas Graf } 1828c7fb64dbSThomas Graf 1829ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 18306b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 18316b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 18326b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 18336b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 18346b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 18356b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 18366b3f8674SThomas Graf }; 18376b3f8674SThomas Graf 1838ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 18396b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 18406b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 18416b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 18426b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 18436b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 18446b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 18456b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 18466b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 18476b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 18486b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 18496b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 18506b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 18516b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 18526b3f8674SThomas Graf }; 18536b3f8674SThomas Graf 1854c8822a4eSThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1855c7fb64dbSThomas Graf { 18563b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1857c7fb64dbSThomas Graf struct neigh_table *tbl; 18586b3f8674SThomas Graf struct ndtmsg *ndtmsg; 18596b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 18606b3f8674SThomas Graf int err; 1861c7fb64dbSThomas Graf 18626b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 18636b3f8674SThomas Graf nl_neightbl_policy); 18646b3f8674SThomas Graf if (err < 0) 18656b3f8674SThomas Graf goto errout; 1866c7fb64dbSThomas Graf 18676b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 18686b3f8674SThomas Graf err = -EINVAL; 18696b3f8674SThomas Graf goto errout; 18706b3f8674SThomas Graf } 18716b3f8674SThomas Graf 18726b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1873c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1874c7fb64dbSThomas Graf for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1875c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1876c7fb64dbSThomas Graf continue; 1877c7fb64dbSThomas Graf 18786b3f8674SThomas Graf if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) 1879c7fb64dbSThomas Graf break; 1880c7fb64dbSThomas Graf } 1881c7fb64dbSThomas Graf 1882c7fb64dbSThomas Graf if (tbl == NULL) { 1883c7fb64dbSThomas Graf err = -ENOENT; 18846b3f8674SThomas Graf goto errout_locked; 1885c7fb64dbSThomas Graf } 1886c7fb64dbSThomas Graf 1887c7fb64dbSThomas Graf /* 1888c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1889c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1890c7fb64dbSThomas Graf */ 1891c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1892c7fb64dbSThomas Graf 18936b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 18946b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 1895c7fb64dbSThomas Graf struct neigh_parms *p; 18966b3f8674SThomas Graf int i, ifindex = 0; 1897c7fb64dbSThomas Graf 18986b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 18996b3f8674SThomas Graf nl_ntbl_parm_policy); 19006b3f8674SThomas Graf if (err < 0) 19016b3f8674SThomas Graf goto errout_tbl_lock; 1902c7fb64dbSThomas Graf 19036b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 19046b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 1905c7fb64dbSThomas Graf 190697fd5bc7STobias Klauser p = lookup_neigh_parms(tbl, net, ifindex); 1907c7fb64dbSThomas Graf if (p == NULL) { 1908c7fb64dbSThomas Graf err = -ENOENT; 19096b3f8674SThomas Graf goto errout_tbl_lock; 1910c7fb64dbSThomas Graf } 1911c7fb64dbSThomas Graf 19126b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 19136b3f8674SThomas Graf if (tbp[i] == NULL) 19146b3f8674SThomas Graf continue; 1915c7fb64dbSThomas Graf 19166b3f8674SThomas Graf switch (i) { 19176b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 19186b3f8674SThomas Graf p->queue_len = nla_get_u32(tbp[i]); 19196b3f8674SThomas Graf break; 19206b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 19216b3f8674SThomas Graf p->proxy_qlen = nla_get_u32(tbp[i]); 19226b3f8674SThomas Graf break; 19236b3f8674SThomas Graf case NDTPA_APP_PROBES: 19246b3f8674SThomas Graf p->app_probes = nla_get_u32(tbp[i]); 19256b3f8674SThomas Graf break; 19266b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 19276b3f8674SThomas Graf p->ucast_probes = nla_get_u32(tbp[i]); 19286b3f8674SThomas Graf break; 19296b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 19306b3f8674SThomas Graf p->mcast_probes = nla_get_u32(tbp[i]); 19316b3f8674SThomas Graf break; 19326b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 19336b3f8674SThomas Graf p->base_reachable_time = nla_get_msecs(tbp[i]); 19346b3f8674SThomas Graf break; 19356b3f8674SThomas Graf case NDTPA_GC_STALETIME: 19366b3f8674SThomas Graf p->gc_staletime = nla_get_msecs(tbp[i]); 19376b3f8674SThomas Graf break; 19386b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 19396b3f8674SThomas Graf p->delay_probe_time = nla_get_msecs(tbp[i]); 19406b3f8674SThomas Graf break; 19416b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 19426b3f8674SThomas Graf p->retrans_time = nla_get_msecs(tbp[i]); 19436b3f8674SThomas Graf break; 19446b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 19456b3f8674SThomas Graf p->anycast_delay = nla_get_msecs(tbp[i]); 19466b3f8674SThomas Graf break; 19476b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 19486b3f8674SThomas Graf p->proxy_delay = nla_get_msecs(tbp[i]); 19496b3f8674SThomas Graf break; 19506b3f8674SThomas Graf case NDTPA_LOCKTIME: 19516b3f8674SThomas Graf p->locktime = nla_get_msecs(tbp[i]); 19526b3f8674SThomas Graf break; 1953c7fb64dbSThomas Graf } 19546b3f8674SThomas Graf } 19556b3f8674SThomas Graf } 19566b3f8674SThomas Graf 19576b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 19586b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 19596b3f8674SThomas Graf 19606b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 19616b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 19626b3f8674SThomas Graf 19636b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 19646b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 19656b3f8674SThomas Graf 19666b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 19676b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 1968c7fb64dbSThomas Graf 1969c7fb64dbSThomas Graf err = 0; 1970c7fb64dbSThomas Graf 19716b3f8674SThomas Graf errout_tbl_lock: 1972c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 19736b3f8674SThomas Graf errout_locked: 1974c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 19756b3f8674SThomas Graf errout: 1976c7fb64dbSThomas Graf return err; 1977c7fb64dbSThomas Graf } 1978c7fb64dbSThomas Graf 1979c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 1980c7fb64dbSThomas Graf { 19813b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1982ca860fb3SThomas Graf int family, tidx, nidx = 0; 1983ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 1984ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 1985c7fb64dbSThomas Graf struct neigh_table *tbl; 1986c7fb64dbSThomas Graf 1987ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 1988c7fb64dbSThomas Graf 1989c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1990ca860fb3SThomas Graf for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) { 1991c7fb64dbSThomas Graf struct neigh_parms *p; 1992c7fb64dbSThomas Graf 1993ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 1994c7fb64dbSThomas Graf continue; 1995c7fb64dbSThomas Graf 1996ca860fb3SThomas Graf if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid, 1997ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 1998ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 1999c7fb64dbSThomas Graf break; 2000c7fb64dbSThomas Graf 2001426b5303SEric W. Biederman for (nidx = 0, p = tbl->parms.next; p; p = p->next) { 2002878628fbSYOSHIFUJI Hideaki if (!net_eq(neigh_parms_net(p), net)) 2003426b5303SEric W. Biederman continue; 2004426b5303SEric W. Biederman 2005efc683fcSGautam Kachroo if (nidx < neigh_skip) 2006efc683fcSGautam Kachroo goto next; 2007c7fb64dbSThomas Graf 2008ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 2009ca860fb3SThomas Graf NETLINK_CB(cb->skb).pid, 2010ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 2011ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 2012ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 2013c7fb64dbSThomas Graf goto out; 2014efc683fcSGautam Kachroo next: 2015efc683fcSGautam Kachroo nidx++; 2016c7fb64dbSThomas Graf } 2017c7fb64dbSThomas Graf 2018ca860fb3SThomas Graf neigh_skip = 0; 2019c7fb64dbSThomas Graf } 2020c7fb64dbSThomas Graf out: 2021c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 2022ca860fb3SThomas Graf cb->args[0] = tidx; 2023ca860fb3SThomas Graf cb->args[1] = nidx; 2024c7fb64dbSThomas Graf 2025c7fb64dbSThomas Graf return skb->len; 2026c7fb64dbSThomas Graf } 20271da177e4SLinus Torvalds 20288b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 20298b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 20301da177e4SLinus Torvalds { 20311da177e4SLinus Torvalds unsigned long now = jiffies; 20321da177e4SLinus Torvalds struct nda_cacheinfo ci; 20338b8aec50SThomas Graf struct nlmsghdr *nlh; 20348b8aec50SThomas Graf struct ndmsg *ndm; 20351da177e4SLinus Torvalds 20368b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 20378b8aec50SThomas Graf if (nlh == NULL) 203826932566SPatrick McHardy return -EMSGSIZE; 20398b8aec50SThomas Graf 20408b8aec50SThomas Graf ndm = nlmsg_data(nlh); 20418b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 20429ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 20439ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 20448b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 20458b8aec50SThomas Graf ndm->ndm_type = neigh->type; 20468b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 20471da177e4SLinus Torvalds 20488b8aec50SThomas Graf NLA_PUT(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key); 20498b8aec50SThomas Graf 20508b8aec50SThomas Graf read_lock_bh(&neigh->lock); 20518b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 20528b8aec50SThomas Graf if ((neigh->nud_state & NUD_VALID) && 20538b8aec50SThomas Graf nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, neigh->ha) < 0) { 20548b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 20558b8aec50SThomas Graf goto nla_put_failure; 20568b8aec50SThomas Graf } 20578b8aec50SThomas Graf 2058b9f5f52cSStephen Hemminger ci.ndm_used = jiffies_to_clock_t(now - neigh->used); 2059b9f5f52cSStephen Hemminger ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); 2060b9f5f52cSStephen Hemminger ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); 20618b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 20628b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 20638b8aec50SThomas Graf 20648b8aec50SThomas Graf NLA_PUT_U32(skb, NDA_PROBES, atomic_read(&neigh->probes)); 20658b8aec50SThomas Graf NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); 20668b8aec50SThomas Graf 20678b8aec50SThomas Graf return nlmsg_end(skb, nlh); 20688b8aec50SThomas Graf 20698b8aec50SThomas Graf nla_put_failure: 207026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 207126932566SPatrick McHardy return -EMSGSIZE; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 2074d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2075d961db35SThomas Graf { 2076d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2077d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2078d961db35SThomas Graf } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 20811da177e4SLinus Torvalds struct netlink_callback *cb) 20821da177e4SLinus Torvalds { 20833b1e0a65SYOSHIFUJI Hideaki struct net * net = sock_net(skb->sk); 20841da177e4SLinus Torvalds struct neighbour *n; 20851da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 20861da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 20871da177e4SLinus Torvalds 2088c5e29460SJulian Anastasov read_lock_bh(&tbl->lock); 20891da177e4SLinus Torvalds for (h = 0; h <= tbl->hash_mask; h++) { 20901da177e4SLinus Torvalds if (h < s_h) 20911da177e4SLinus Torvalds continue; 20921da177e4SLinus Torvalds if (h > s_h) 20931da177e4SLinus Torvalds s_idx = 0; 2094426b5303SEric W. Biederman for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next) { 2095c346dca1SYOSHIFUJI Hideaki if (dev_net(n->dev) != net) 2096426b5303SEric W. Biederman continue; 2097efc683fcSGautam Kachroo if (idx < s_idx) 2098efc683fcSGautam Kachroo goto next; 20991da177e4SLinus Torvalds if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid, 21001da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2101b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 2102b6544c0bSJamal Hadi Salim NLM_F_MULTI) <= 0) { 21031da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 21041da177e4SLinus Torvalds rc = -1; 21051da177e4SLinus Torvalds goto out; 21061da177e4SLinus Torvalds } 2107efc683fcSGautam Kachroo next: 2108efc683fcSGautam Kachroo idx++; 21091da177e4SLinus Torvalds } 21101da177e4SLinus Torvalds } 2111c5e29460SJulian Anastasov read_unlock_bh(&tbl->lock); 21121da177e4SLinus Torvalds rc = skb->len; 21131da177e4SLinus Torvalds out: 21141da177e4SLinus Torvalds cb->args[1] = h; 21151da177e4SLinus Torvalds cb->args[2] = idx; 21161da177e4SLinus Torvalds return rc; 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 2119c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 21201da177e4SLinus Torvalds { 21211da177e4SLinus Torvalds struct neigh_table *tbl; 21221da177e4SLinus Torvalds int t, family, s_t; 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 21258b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 21261da177e4SLinus Torvalds s_t = cb->args[0]; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) { 21291da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 21301da177e4SLinus Torvalds continue; 21311da177e4SLinus Torvalds if (t > s_t) 21321da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 21331da177e4SLinus Torvalds sizeof(cb->args[0])); 21341da177e4SLinus Torvalds if (neigh_dump_table(tbl, skb, cb) < 0) 21351da177e4SLinus Torvalds break; 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds cb->args[0] = t; 21401da177e4SLinus Torvalds return skb->len; 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds int chain; 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 21481da177e4SLinus Torvalds for (chain = 0; chain <= tbl->hash_mask; chain++) { 21491da177e4SLinus Torvalds struct neighbour *n; 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds for (n = tbl->hash_buckets[chain]; n; n = n->next) 21521da177e4SLinus Torvalds cb(n, cookie); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 21571da177e4SLinus Torvalds 21581da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 21591da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 21601da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 21611da177e4SLinus Torvalds { 21621da177e4SLinus Torvalds int chain; 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds for (chain = 0; chain <= tbl->hash_mask; chain++) { 21651da177e4SLinus Torvalds struct neighbour *n, **np; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds np = &tbl->hash_buckets[chain]; 21681da177e4SLinus Torvalds while ((n = *np) != NULL) { 21691da177e4SLinus Torvalds int release; 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds write_lock(&n->lock); 21721da177e4SLinus Torvalds release = cb(n); 21731da177e4SLinus Torvalds if (release) { 21741da177e4SLinus Torvalds *np = n->next; 21751da177e4SLinus Torvalds n->dead = 1; 21761da177e4SLinus Torvalds } else 21771da177e4SLinus Torvalds np = &n->next; 21781da177e4SLinus Torvalds write_unlock(&n->lock); 21794f494554SThomas Graf if (release) 21804f494554SThomas Graf neigh_cleanup_and_release(n); 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds } 2183ecbb4169SAlexey Kuznetsov } 21841da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 21891da177e4SLinus Torvalds { 21901da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 21911218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 21921da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 21931da177e4SLinus Torvalds struct neighbour *n = NULL; 21941da177e4SLinus Torvalds int bucket = state->bucket; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 21971da177e4SLinus Torvalds for (bucket = 0; bucket <= tbl->hash_mask; bucket++) { 21981da177e4SLinus Torvalds n = tbl->hash_buckets[bucket]; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds while (n) { 2201878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2202426b5303SEric W. Biederman goto next; 22031da177e4SLinus Torvalds if (state->neigh_sub_iter) { 22041da177e4SLinus Torvalds loff_t fakep = 0; 22051da177e4SLinus Torvalds void *v; 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 22081da177e4SLinus Torvalds if (!v) 22091da177e4SLinus Torvalds goto next; 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 22121da177e4SLinus Torvalds break; 22131da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 22141da177e4SLinus Torvalds break; 22151da177e4SLinus Torvalds next: 22161da177e4SLinus Torvalds n = n->next; 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds if (n) 22201da177e4SLinus Torvalds break; 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds state->bucket = bucket; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds return n; 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 22281da177e4SLinus Torvalds struct neighbour *n, 22291da177e4SLinus Torvalds loff_t *pos) 22301da177e4SLinus Torvalds { 22311da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 22321218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 22331da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds if (state->neigh_sub_iter) { 22361da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 22371da177e4SLinus Torvalds if (v) 22381da177e4SLinus Torvalds return n; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds n = n->next; 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds while (1) { 22431da177e4SLinus Torvalds while (n) { 2244878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2245426b5303SEric W. Biederman goto next; 22461da177e4SLinus Torvalds if (state->neigh_sub_iter) { 22471da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 22481da177e4SLinus Torvalds if (v) 22491da177e4SLinus Torvalds return n; 22501da177e4SLinus Torvalds goto next; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 22531da177e4SLinus Torvalds break; 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 22561da177e4SLinus Torvalds break; 22571da177e4SLinus Torvalds next: 22581da177e4SLinus Torvalds n = n->next; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds if (n) 22621da177e4SLinus Torvalds break; 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds if (++state->bucket > tbl->hash_mask) 22651da177e4SLinus Torvalds break; 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds n = tbl->hash_buckets[state->bucket]; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds if (n && pos) 22711da177e4SLinus Torvalds --(*pos); 22721da177e4SLinus Torvalds return n; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds if (n) { 2280745e2031SChris Larson --(*pos); 22811da177e4SLinus Torvalds while (*pos) { 22821da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 22831da177e4SLinus Torvalds if (!n) 22841da177e4SLinus Torvalds break; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds return *pos ? NULL : n; 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 22911da177e4SLinus Torvalds { 22921da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 22931218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 22941da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 22951da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 22961da177e4SLinus Torvalds int bucket = state->bucket; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 22991da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 23001da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2301878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2302426b5303SEric W. Biederman pn = pn->next; 23031da177e4SLinus Torvalds if (pn) 23041da177e4SLinus Torvalds break; 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds state->bucket = bucket; 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds return pn; 23091da177e4SLinus Torvalds } 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 23121da177e4SLinus Torvalds struct pneigh_entry *pn, 23131da177e4SLinus Torvalds loff_t *pos) 23141da177e4SLinus Torvalds { 23151da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23161218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 23171da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds pn = pn->next; 23201da177e4SLinus Torvalds while (!pn) { 23211da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 23221da177e4SLinus Torvalds break; 23231da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2324878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2325426b5303SEric W. Biederman pn = pn->next; 23261da177e4SLinus Torvalds if (pn) 23271da177e4SLinus Torvalds break; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds if (pn && pos) 23311da177e4SLinus Torvalds --(*pos); 23321da177e4SLinus Torvalds 23331da177e4SLinus Torvalds return pn; 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 23371da177e4SLinus Torvalds { 23381da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds if (pn) { 2341745e2031SChris Larson --(*pos); 23421da177e4SLinus Torvalds while (*pos) { 23431da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 23441da177e4SLinus Torvalds if (!pn) 23451da177e4SLinus Torvalds break; 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds return *pos ? NULL : pn; 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 23521da177e4SLinus Torvalds { 23531da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23541da177e4SLinus Torvalds void *rc; 2355745e2031SChris Larson loff_t idxpos = *pos; 23561da177e4SLinus Torvalds 2357745e2031SChris Larson rc = neigh_get_idx(seq, &idxpos); 23581da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 2359745e2031SChris Larson rc = pneigh_get_idx(seq, &idxpos); 23601da177e4SLinus Torvalds 23611da177e4SLinus Torvalds return rc; 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 23659a429c49SEric Dumazet __acquires(tbl->lock) 23661da177e4SLinus Torvalds { 23671da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds state->tbl = tbl; 23701da177e4SLinus Torvalds state->bucket = 0; 23711da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 23741da177e4SLinus Torvalds 2375745e2031SChris Larson return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 23801da177e4SLinus Torvalds { 23811da177e4SLinus Torvalds struct neigh_seq_state *state; 23821da177e4SLinus Torvalds void *rc; 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2385bff69732SChris Larson rc = neigh_get_first(seq); 23861da177e4SLinus Torvalds goto out; 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds state = seq->private; 23901da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 23911da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 23921da177e4SLinus Torvalds if (rc) 23931da177e4SLinus Torvalds goto out; 23941da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 23951da177e4SLinus Torvalds rc = pneigh_get_first(seq); 23961da177e4SLinus Torvalds } else { 23971da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 23981da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds out: 24011da177e4SLinus Torvalds ++(*pos); 24021da177e4SLinus Torvalds return rc; 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 24079a429c49SEric Dumazet __releases(tbl->lock) 24081da177e4SLinus Torvalds { 24091da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24101da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /* statistics via seq_file */ 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 24191da177e4SLinus Torvalds { 24201da177e4SLinus Torvalds struct proc_dir_entry *pde = seq->private; 24211da177e4SLinus Torvalds struct neigh_table *tbl = pde->data; 24221da177e4SLinus Torvalds int cpu; 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds if (*pos == 0) 24251da177e4SLinus Torvalds return SEQ_START_TOKEN; 24261da177e4SLinus Torvalds 24270f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 24281da177e4SLinus Torvalds if (!cpu_possible(cpu)) 24291da177e4SLinus Torvalds continue; 24301da177e4SLinus Torvalds *pos = cpu+1; 24311da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds return NULL; 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 24371da177e4SLinus Torvalds { 24381da177e4SLinus Torvalds struct proc_dir_entry *pde = seq->private; 24391da177e4SLinus Torvalds struct neigh_table *tbl = pde->data; 24401da177e4SLinus Torvalds int cpu; 24411da177e4SLinus Torvalds 24420f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 24431da177e4SLinus Torvalds if (!cpu_possible(cpu)) 24441da177e4SLinus Torvalds continue; 24451da177e4SLinus Torvalds *pos = cpu+1; 24461da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 24471da177e4SLinus Torvalds } 24481da177e4SLinus Torvalds return NULL; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 24521da177e4SLinus Torvalds { 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 24571da177e4SLinus Torvalds { 24581da177e4SLinus Torvalds struct proc_dir_entry *pde = seq->private; 24591da177e4SLinus Torvalds struct neigh_table *tbl = pde->data; 24601da177e4SLinus Torvalds struct neigh_statistics *st = v; 24611da177e4SLinus Torvalds 24621da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 24639a6d276eSNeil 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"); 24641da177e4SLinus Torvalds return 0; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 24689a6d276eSNeil Horman "%08lx %08lx %08lx %08lx %08lx\n", 24691da177e4SLinus Torvalds atomic_read(&tbl->entries), 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds st->allocs, 24721da177e4SLinus Torvalds st->destroys, 24731da177e4SLinus Torvalds st->hash_grows, 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds st->lookups, 24761da177e4SLinus Torvalds st->hits, 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds st->res_failed, 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds st->rcv_probes_mcast, 24811da177e4SLinus Torvalds st->rcv_probes_ucast, 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds st->periodic_gc_runs, 24849a6d276eSNeil Horman st->forced_gc_runs, 24859a6d276eSNeil Horman st->unres_discards 24861da177e4SLinus Torvalds ); 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds return 0; 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds 2491f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 24921da177e4SLinus Torvalds .start = neigh_stat_seq_start, 24931da177e4SLinus Torvalds .next = neigh_stat_seq_next, 24941da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 24951da177e4SLinus Torvalds .show = neigh_stat_seq_show, 24961da177e4SLinus Torvalds }; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 24991da177e4SLinus Torvalds { 25001da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds if (!ret) { 25031da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 25041da177e4SLinus Torvalds sf->private = PDE(inode); 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds return ret; 25071da177e4SLinus Torvalds }; 25081da177e4SLinus Torvalds 25099a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 25101da177e4SLinus Torvalds .owner = THIS_MODULE, 25111da177e4SLinus Torvalds .open = neigh_stat_seq_open, 25121da177e4SLinus Torvalds .read = seq_read, 25131da177e4SLinus Torvalds .llseek = seq_lseek, 25141da177e4SLinus Torvalds .release = seq_release, 25151da177e4SLinus Torvalds }; 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 25181da177e4SLinus Torvalds 2519339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2520339bf98fSThomas Graf { 2521339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2522339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2523339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2524339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2525339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2526339bf98fSThomas Graf } 2527339bf98fSThomas Graf 2528b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 25291da177e4SLinus Torvalds { 2530c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(n->dev); 25318b8aec50SThomas Graf struct sk_buff *skb; 2532b8673311SThomas Graf int err = -ENOBUFS; 25331da177e4SLinus Torvalds 2534339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 25358b8aec50SThomas Graf if (skb == NULL) 2536b8673311SThomas Graf goto errout; 25371da177e4SLinus Torvalds 2538b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 253926932566SPatrick McHardy if (err < 0) { 254026932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 254126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 254226932566SPatrick McHardy kfree_skb(skb); 254326932566SPatrick McHardy goto errout; 254426932566SPatrick McHardy } 25451ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 25461ce85fe4SPablo Neira Ayuso return; 2547b8673311SThomas Graf errout: 2548b8673311SThomas Graf if (err < 0) 2549426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2550b8673311SThomas Graf } 2551b8673311SThomas Graf 2552d961db35SThomas Graf #ifdef CONFIG_ARPD 2553b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2554b8673311SThomas Graf { 2555b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 25568b8aec50SThomas Graf } 25570a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns); 25581da177e4SLinus Torvalds #endif /* CONFIG_ARPD */ 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds static struct neigh_sysctl_table { 25631da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 2564c3bac5a7SPavel Emelyanov struct ctl_table neigh_vars[__NET_NEIGH_MAX]; 2565c3bac5a7SPavel Emelyanov char *dev_name; 2566ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 25671da177e4SLinus Torvalds .neigh_vars = { 25681da177e4SLinus Torvalds { 25691da177e4SLinus Torvalds .procname = "mcast_solicit", 25701da177e4SLinus Torvalds .maxlen = sizeof(int), 25711da177e4SLinus Torvalds .mode = 0644, 25726d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 25731da177e4SLinus Torvalds }, 25741da177e4SLinus Torvalds { 25751da177e4SLinus Torvalds .procname = "ucast_solicit", 25761da177e4SLinus Torvalds .maxlen = sizeof(int), 25771da177e4SLinus Torvalds .mode = 0644, 25786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 25791da177e4SLinus Torvalds }, 25801da177e4SLinus Torvalds { 25811da177e4SLinus Torvalds .procname = "app_solicit", 25821da177e4SLinus Torvalds .maxlen = sizeof(int), 25831da177e4SLinus Torvalds .mode = 0644, 25846d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 25851da177e4SLinus Torvalds }, 25861da177e4SLinus Torvalds { 25871da177e4SLinus Torvalds .procname = "retrans_time", 25881da177e4SLinus Torvalds .maxlen = sizeof(int), 25891da177e4SLinus Torvalds .mode = 0644, 25906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 25911da177e4SLinus Torvalds }, 25921da177e4SLinus Torvalds { 25931da177e4SLinus Torvalds .procname = "base_reachable_time", 25941da177e4SLinus Torvalds .maxlen = sizeof(int), 25951da177e4SLinus Torvalds .mode = 0644, 25966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 25971da177e4SLinus Torvalds }, 25981da177e4SLinus Torvalds { 25991da177e4SLinus Torvalds .procname = "delay_first_probe_time", 26001da177e4SLinus Torvalds .maxlen = sizeof(int), 26011da177e4SLinus Torvalds .mode = 0644, 26026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26031da177e4SLinus Torvalds }, 26041da177e4SLinus Torvalds { 26051da177e4SLinus Torvalds .procname = "gc_stale_time", 26061da177e4SLinus Torvalds .maxlen = sizeof(int), 26071da177e4SLinus Torvalds .mode = 0644, 26086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26091da177e4SLinus Torvalds }, 26101da177e4SLinus Torvalds { 26111da177e4SLinus Torvalds .procname = "unres_qlen", 26121da177e4SLinus Torvalds .maxlen = sizeof(int), 26131da177e4SLinus Torvalds .mode = 0644, 26146d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26151da177e4SLinus Torvalds }, 26161da177e4SLinus Torvalds { 26171da177e4SLinus Torvalds .procname = "proxy_qlen", 26181da177e4SLinus Torvalds .maxlen = sizeof(int), 26191da177e4SLinus Torvalds .mode = 0644, 26206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26211da177e4SLinus Torvalds }, 26221da177e4SLinus Torvalds { 26231da177e4SLinus Torvalds .procname = "anycast_delay", 26241da177e4SLinus Torvalds .maxlen = sizeof(int), 26251da177e4SLinus Torvalds .mode = 0644, 26266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26271da177e4SLinus Torvalds }, 26281da177e4SLinus Torvalds { 26291da177e4SLinus Torvalds .procname = "proxy_delay", 26301da177e4SLinus Torvalds .maxlen = sizeof(int), 26311da177e4SLinus Torvalds .mode = 0644, 26326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26331da177e4SLinus Torvalds }, 26341da177e4SLinus Torvalds { 26351da177e4SLinus Torvalds .procname = "locktime", 26361da177e4SLinus Torvalds .maxlen = sizeof(int), 26371da177e4SLinus Torvalds .mode = 0644, 26386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_userhz_jiffies, 26391da177e4SLinus Torvalds }, 26401da177e4SLinus Torvalds { 2641d12af679SEric W. Biederman .procname = "retrans_time_ms", 2642d12af679SEric W. Biederman .maxlen = sizeof(int), 2643d12af679SEric W. Biederman .mode = 0644, 26446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2645d12af679SEric W. Biederman }, 2646d12af679SEric W. Biederman { 2647d12af679SEric W. Biederman .procname = "base_reachable_time_ms", 2648d12af679SEric W. Biederman .maxlen = sizeof(int), 2649d12af679SEric W. Biederman .mode = 0644, 26506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 2651d12af679SEric W. Biederman }, 2652d12af679SEric W. Biederman { 26531da177e4SLinus Torvalds .procname = "gc_interval", 26541da177e4SLinus Torvalds .maxlen = sizeof(int), 26551da177e4SLinus Torvalds .mode = 0644, 26566d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26571da177e4SLinus Torvalds }, 26581da177e4SLinus Torvalds { 26591da177e4SLinus Torvalds .procname = "gc_thresh1", 26601da177e4SLinus Torvalds .maxlen = sizeof(int), 26611da177e4SLinus Torvalds .mode = 0644, 26626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26631da177e4SLinus Torvalds }, 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds .procname = "gc_thresh2", 26661da177e4SLinus Torvalds .maxlen = sizeof(int), 26671da177e4SLinus Torvalds .mode = 0644, 26686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26691da177e4SLinus Torvalds }, 26701da177e4SLinus Torvalds { 26711da177e4SLinus Torvalds .procname = "gc_thresh3", 26721da177e4SLinus Torvalds .maxlen = sizeof(int), 26731da177e4SLinus Torvalds .mode = 0644, 26746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 26751da177e4SLinus Torvalds }, 2676c3bac5a7SPavel Emelyanov {}, 26771da177e4SLinus Torvalds }, 26781da177e4SLinus Torvalds }; 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 26811da177e4SLinus Torvalds int p_id, int pdev_id, char *p_name, 2682f8572d8fSEric W. Biederman proc_handler *handler) 26831da177e4SLinus Torvalds { 26843c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 26851da177e4SLinus Torvalds const char *dev_name_source = NULL; 2686c3bac5a7SPavel Emelyanov 2687c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_ROOT 0 2688c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_PROTO 1 2689c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_NEIGH 2 2690c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_DEV 3 2691c3bac5a7SPavel Emelyanov 2692c3bac5a7SPavel Emelyanov struct ctl_path neigh_path[] = { 2693f8572d8fSEric W. Biederman { .procname = "net", }, 2694f8572d8fSEric W. Biederman { .procname = "proto", }, 2695f8572d8fSEric W. Biederman { .procname = "neigh", }, 2696f8572d8fSEric W. Biederman { .procname = "default", }, 2697c3bac5a7SPavel Emelyanov { }, 2698c3bac5a7SPavel Emelyanov }; 26991da177e4SLinus Torvalds 27003c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 27011da177e4SLinus Torvalds if (!t) 27023c607bbbSPavel Emelyanov goto err; 27033c607bbbSPavel Emelyanov 27041da177e4SLinus Torvalds t->neigh_vars[0].data = &p->mcast_probes; 27051da177e4SLinus Torvalds t->neigh_vars[1].data = &p->ucast_probes; 27061da177e4SLinus Torvalds t->neigh_vars[2].data = &p->app_probes; 27071da177e4SLinus Torvalds t->neigh_vars[3].data = &p->retrans_time; 27081da177e4SLinus Torvalds t->neigh_vars[4].data = &p->base_reachable_time; 27091da177e4SLinus Torvalds t->neigh_vars[5].data = &p->delay_probe_time; 27101da177e4SLinus Torvalds t->neigh_vars[6].data = &p->gc_staletime; 27111da177e4SLinus Torvalds t->neigh_vars[7].data = &p->queue_len; 27121da177e4SLinus Torvalds t->neigh_vars[8].data = &p->proxy_qlen; 27131da177e4SLinus Torvalds t->neigh_vars[9].data = &p->anycast_delay; 27141da177e4SLinus Torvalds t->neigh_vars[10].data = &p->proxy_delay; 27151da177e4SLinus Torvalds t->neigh_vars[11].data = &p->locktime; 2716d12af679SEric W. Biederman t->neigh_vars[12].data = &p->retrans_time; 2717d12af679SEric W. Biederman t->neigh_vars[13].data = &p->base_reachable_time; 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds if (dev) { 27201da177e4SLinus Torvalds dev_name_source = dev->name; 2721d12af679SEric W. Biederman /* Terminate the table early */ 2722d12af679SEric W. Biederman memset(&t->neigh_vars[14], 0, sizeof(t->neigh_vars[14])); 27231da177e4SLinus Torvalds } else { 2724c3bac5a7SPavel Emelyanov dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname; 2725d12af679SEric W. Biederman t->neigh_vars[14].data = (int *)(p + 1); 2726d12af679SEric W. Biederman t->neigh_vars[15].data = (int *)(p + 1) + 1; 2727d12af679SEric W. Biederman t->neigh_vars[16].data = (int *)(p + 1) + 2; 2728d12af679SEric W. Biederman t->neigh_vars[17].data = (int *)(p + 1) + 3; 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds 2732f8572d8fSEric W. Biederman if (handler) { 27331da177e4SLinus Torvalds /* RetransTime */ 27341da177e4SLinus Torvalds t->neigh_vars[3].proc_handler = handler; 27351da177e4SLinus Torvalds t->neigh_vars[3].extra1 = dev; 27361da177e4SLinus Torvalds /* ReachableTime */ 27371da177e4SLinus Torvalds t->neigh_vars[4].proc_handler = handler; 27381da177e4SLinus Torvalds t->neigh_vars[4].extra1 = dev; 27391da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 2740d12af679SEric W. Biederman t->neigh_vars[12].proc_handler = handler; 2741d12af679SEric W. Biederman t->neigh_vars[12].extra1 = dev; 27421da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 2743d12af679SEric W. Biederman t->neigh_vars[13].proc_handler = handler; 2744d12af679SEric W. Biederman t->neigh_vars[13].extra1 = dev; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 2747c3bac5a7SPavel Emelyanov t->dev_name = kstrdup(dev_name_source, GFP_KERNEL); 2748c3bac5a7SPavel Emelyanov if (!t->dev_name) 27491da177e4SLinus Torvalds goto free; 27501da177e4SLinus Torvalds 2751c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name; 2752c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name; 27531da177e4SLinus Torvalds 27544ab438fcSDenis V. Lunev t->sysctl_header = 275557da52c1SYOSHIFUJI Hideaki register_net_sysctl_table(neigh_parms_net(p), neigh_path, t->neigh_vars); 27563c607bbbSPavel Emelyanov if (!t->sysctl_header) 27571da177e4SLinus Torvalds goto free_procname; 27583c607bbbSPavel Emelyanov 27591da177e4SLinus Torvalds p->sysctl_table = t; 27601da177e4SLinus Torvalds return 0; 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds free_procname: 2763c3bac5a7SPavel Emelyanov kfree(t->dev_name); 27641da177e4SLinus Torvalds free: 27651da177e4SLinus Torvalds kfree(t); 27663c607bbbSPavel Emelyanov err: 27673c607bbbSPavel Emelyanov return -ENOBUFS; 27681da177e4SLinus Torvalds } 27690a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register); 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 27721da177e4SLinus Torvalds { 27731da177e4SLinus Torvalds if (p->sysctl_table) { 27741da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 27751da177e4SLinus Torvalds p->sysctl_table = NULL; 27761da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 2777c3bac5a7SPavel Emelyanov kfree(t->dev_name); 27781da177e4SLinus Torvalds kfree(t); 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds } 27810a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister); 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 27841da177e4SLinus Torvalds 2785c8822a4eSThomas Graf static int __init neigh_init(void) 2786c8822a4eSThomas Graf { 2787c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL); 2788c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL); 2789c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info); 2790c8822a4eSThomas Graf 2791c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info); 2792c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL); 2793c8822a4eSThomas Graf 2794c8822a4eSThomas Graf return 0; 2795c8822a4eSThomas Graf } 2796c8822a4eSThomas Graf 2797c8822a4eSThomas Graf subsys_initcall(neigh_init); 2798c8822a4eSThomas Graf 2799