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 } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds int shrunk = 0; 1311da177e4SLinus Torvalds int i; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 1361da177e4SLinus Torvalds for (i = 0; i <= tbl->hash_mask; i++) { 1371da177e4SLinus Torvalds struct neighbour *n, **np; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds np = &tbl->hash_buckets[i]; 1401da177e4SLinus Torvalds while ((n = *np) != NULL) { 1411da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1421da177e4SLinus Torvalds * - nobody refers to it. 1431da177e4SLinus Torvalds * - it is not permanent 1441da177e4SLinus Torvalds */ 1451da177e4SLinus Torvalds write_lock(&n->lock); 1461da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1471da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 1481da177e4SLinus Torvalds *np = n->next; 1491da177e4SLinus Torvalds n->dead = 1; 1501da177e4SLinus Torvalds shrunk = 1; 1511da177e4SLinus Torvalds write_unlock(&n->lock); 1524f494554SThomas Graf neigh_cleanup_and_release(n); 1531da177e4SLinus Torvalds continue; 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds write_unlock(&n->lock); 1561da177e4SLinus Torvalds np = &n->next; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds tbl->last_flush = jiffies; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds return shrunk; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 167a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 168a43d8994SPavel Emelyanov { 169a43d8994SPavel Emelyanov neigh_hold(n); 170a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 171a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 172a43d8994SPavel Emelyanov n->nud_state); 173a43d8994SPavel Emelyanov dump_stack(); 174a43d8994SPavel Emelyanov } 175a43d8994SPavel Emelyanov } 176a43d8994SPavel Emelyanov 1771da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1801da177e4SLinus Torvalds del_timer(&n->timer)) { 1811da177e4SLinus Torvalds neigh_release(n); 1821da177e4SLinus Torvalds return 1; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds return 0; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds struct sk_buff *skb; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 1921da177e4SLinus Torvalds dev_put(skb->dev); 1931da177e4SLinus Torvalds kfree_skb(skb); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 19749636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds int i; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds for (i = 0; i <= tbl->hash_mask; i++) { 2021da177e4SLinus Torvalds struct neighbour *n, **np = &tbl->hash_buckets[i]; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds while ((n = *np) != NULL) { 2051da177e4SLinus Torvalds if (dev && n->dev != dev) { 2061da177e4SLinus Torvalds np = &n->next; 2071da177e4SLinus Torvalds continue; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds *np = n->next; 2101da177e4SLinus Torvalds write_lock(&n->lock); 2111da177e4SLinus Torvalds neigh_del_timer(n); 2121da177e4SLinus Torvalds n->dead = 1; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2151da177e4SLinus Torvalds /* The most unpleasant situation. 2161da177e4SLinus Torvalds We must destroy neighbour entry, 2171da177e4SLinus Torvalds but someone still uses it. 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds The destroy will be delayed until 2201da177e4SLinus Torvalds the last user releases us, but 2211da177e4SLinus Torvalds we must kill timers etc. and move 2221da177e4SLinus Torvalds it to safe state. 2231da177e4SLinus Torvalds */ 2241da177e4SLinus Torvalds skb_queue_purge(&n->arp_queue); 2251da177e4SLinus Torvalds n->output = neigh_blackhole; 2261da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2271da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2281da177e4SLinus Torvalds else 2291da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2301da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is stray.\n", n); 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds write_unlock(&n->lock); 2334f494554SThomas Graf neigh_cleanup_and_release(n); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds } 23649636bb1SHerbert Xu } 2371da177e4SLinus Torvalds 23849636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 23949636bb1SHerbert Xu { 24049636bb1SHerbert Xu write_lock_bh(&tbl->lock); 24149636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 24249636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 24349636bb1SHerbert Xu } 24449636bb1SHerbert Xu 24549636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 24649636bb1SHerbert Xu { 24749636bb1SHerbert Xu write_lock_bh(&tbl->lock); 24849636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2491da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2501da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2531da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2541da177e4SLinus Torvalds return 0; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static struct neighbour *neigh_alloc(struct neigh_table *tbl) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds struct neighbour *n = NULL; 2601da177e4SLinus Torvalds unsigned long now = jiffies; 2611da177e4SLinus Torvalds int entries; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2641da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2651da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2661da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2671da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2681da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2691da177e4SLinus Torvalds goto out_entries; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 272c3762229SRobert P. J. Day n = kmem_cache_zalloc(tbl->kmem_cachep, GFP_ATOMIC); 2731da177e4SLinus Torvalds if (!n) 2741da177e4SLinus Torvalds goto out_entries; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds skb_queue_head_init(&n->arp_queue); 2771da177e4SLinus Torvalds rwlock_init(&n->lock); 2781da177e4SLinus Torvalds n->updated = n->used = now; 2791da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2801da177e4SLinus Torvalds n->output = neigh_blackhole; 2811da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 282b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 2851da177e4SLinus Torvalds n->tbl = tbl; 2861da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 2871da177e4SLinus Torvalds n->dead = 1; 2881da177e4SLinus Torvalds out: 2891da177e4SLinus Torvalds return n; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds out_entries: 2921da177e4SLinus Torvalds atomic_dec(&tbl->entries); 2931da177e4SLinus Torvalds goto out; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds static struct neighbour **neigh_hash_alloc(unsigned int entries) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds unsigned long size = entries * sizeof(struct neighbour *); 2991da177e4SLinus Torvalds struct neighbour **ret; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds if (size <= PAGE_SIZE) { 30277d04bd9SAndrew Morton ret = kzalloc(size, GFP_ATOMIC); 3031da177e4SLinus Torvalds } else { 3041da177e4SLinus Torvalds ret = (struct neighbour **) 30577d04bd9SAndrew Morton __get_free_pages(GFP_ATOMIC|__GFP_ZERO, get_order(size)); 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds return ret; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static void neigh_hash_free(struct neighbour **hash, unsigned int entries) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds unsigned long size = entries * sizeof(struct neighbour *); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (size <= PAGE_SIZE) 3151da177e4SLinus Torvalds kfree(hash); 3161da177e4SLinus Torvalds else 3171da177e4SLinus Torvalds free_pages((unsigned long)hash, get_order(size)); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds struct neighbour **new_hash, **old_hash; 3231da177e4SLinus Torvalds unsigned int i, new_hash_mask, old_entries; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3261da177e4SLinus Torvalds 327c3609d51Svignesh babu BUG_ON(!is_power_of_2(new_entries)); 3281da177e4SLinus Torvalds new_hash = neigh_hash_alloc(new_entries); 3291da177e4SLinus Torvalds if (!new_hash) 3301da177e4SLinus Torvalds return; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds old_entries = tbl->hash_mask + 1; 3331da177e4SLinus Torvalds new_hash_mask = new_entries - 1; 3341da177e4SLinus Torvalds old_hash = tbl->hash_buckets; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); 3371da177e4SLinus Torvalds for (i = 0; i < old_entries; i++) { 3381da177e4SLinus Torvalds struct neighbour *n, *next; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds for (n = old_hash[i]; n; n = next) { 3411da177e4SLinus Torvalds unsigned int hash_val = tbl->hash(n->primary_key, n->dev); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds hash_val &= new_hash_mask; 3441da177e4SLinus Torvalds next = n->next; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds n->next = new_hash[hash_val]; 3471da177e4SLinus Torvalds new_hash[hash_val] = n; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds tbl->hash_buckets = new_hash; 3511da177e4SLinus Torvalds tbl->hash_mask = new_hash_mask; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds neigh_hash_free(old_hash, old_entries); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 3571da177e4SLinus Torvalds struct net_device *dev) 3581da177e4SLinus Torvalds { 3591da177e4SLinus Torvalds struct neighbour *n; 3601da177e4SLinus Torvalds int key_len = tbl->key_len; 361bc4bf5f3SPavel Emelyanov u32 hash_val; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 366bc4bf5f3SPavel Emelyanov hash_val = tbl->hash(pkey, dev); 367c5e29460SJulian Anastasov for (n = tbl->hash_buckets[hash_val & tbl->hash_mask]; n; n = n->next) { 3681da177e4SLinus Torvalds if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) { 3691da177e4SLinus Torvalds neigh_hold(n); 3701da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 3711da177e4SLinus Torvalds break; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 3751da177e4SLinus Torvalds return n; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 378426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 379426b5303SEric W. Biederman const void *pkey) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds struct neighbour *n; 3821da177e4SLinus Torvalds int key_len = tbl->key_len; 383bc4bf5f3SPavel Emelyanov u32 hash_val; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 388bc4bf5f3SPavel Emelyanov hash_val = tbl->hash(pkey, NULL); 389c5e29460SJulian Anastasov for (n = tbl->hash_buckets[hash_val & tbl->hash_mask]; n; n = n->next) { 390426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 391426b5303SEric W. Biederman (net == n->dev->nd_net)) { 3921da177e4SLinus Torvalds neigh_hold(n); 3931da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 3941da177e4SLinus Torvalds break; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 3981da177e4SLinus Torvalds return n; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey, 4021da177e4SLinus Torvalds struct net_device *dev) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds u32 hash_val; 4051da177e4SLinus Torvalds int key_len = tbl->key_len; 4061da177e4SLinus Torvalds int error; 4071da177e4SLinus Torvalds struct neighbour *n1, *rc, *n = neigh_alloc(tbl); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (!n) { 4101da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4111da177e4SLinus Torvalds goto out; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4151da177e4SLinus Torvalds n->dev = dev; 4161da177e4SLinus Torvalds dev_hold(dev); 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds /* Protocol specific setup. */ 4191da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4201da177e4SLinus Torvalds rc = ERR_PTR(error); 4211da177e4SLinus Torvalds goto out_neigh_release; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* Device specific setup. */ 4251da177e4SLinus Torvalds if (n->parms->neigh_setup && 4261da177e4SLinus Torvalds (error = n->parms->neigh_setup(n)) < 0) { 4271da177e4SLinus Torvalds rc = ERR_PTR(error); 4281da177e4SLinus Torvalds goto out_neigh_release; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds n->confirmed = jiffies - (n->parms->base_reachable_time << 1); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds if (atomic_read(&tbl->entries) > (tbl->hash_mask + 1)) 4361da177e4SLinus Torvalds neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds hash_val = tbl->hash(pkey, dev) & tbl->hash_mask; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds if (n->parms->dead) { 4411da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 4421da177e4SLinus Torvalds goto out_tbl_unlock; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) { 4461da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 4471da177e4SLinus Torvalds neigh_hold(n1); 4481da177e4SLinus Torvalds rc = n1; 4491da177e4SLinus Torvalds goto out_tbl_unlock; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds n->next = tbl->hash_buckets[hash_val]; 4541da177e4SLinus Torvalds tbl->hash_buckets[hash_val] = n; 4551da177e4SLinus Torvalds n->dead = 0; 4561da177e4SLinus Torvalds neigh_hold(n); 4571da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 4581da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is created.\n", n); 4591da177e4SLinus Torvalds rc = n; 4601da177e4SLinus Torvalds out: 4611da177e4SLinus Torvalds return rc; 4621da177e4SLinus Torvalds out_tbl_unlock: 4631da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 4641da177e4SLinus Torvalds out_neigh_release: 4651da177e4SLinus Torvalds neigh_release(n); 4661da177e4SLinus Torvalds goto out; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 469426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 470426b5303SEric W. Biederman struct net *net, const void *pkey, 4711da177e4SLinus Torvalds struct net_device *dev, int creat) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct pneigh_entry *n; 4741da177e4SLinus Torvalds int key_len = tbl->key_len; 4751da177e4SLinus Torvalds u32 hash_val = *(u32 *)(pkey + key_len - 4); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds hash_val ^= (hash_val >> 16); 4781da177e4SLinus Torvalds hash_val ^= hash_val >> 8; 4791da177e4SLinus Torvalds hash_val ^= hash_val >> 4; 4801da177e4SLinus Torvalds hash_val &= PNEIGH_HASHMASK; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds for (n = tbl->phash_buckets[hash_val]; n; n = n->next) { 4851da177e4SLinus Torvalds if (!memcmp(n->key, pkey, key_len) && 486426b5303SEric W. Biederman (n->net == net) && 4871da177e4SLinus Torvalds (n->dev == dev || !n->dev)) { 4881da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 4891da177e4SLinus Torvalds goto out; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 4931da177e4SLinus Torvalds n = NULL; 4941da177e4SLinus Torvalds if (!creat) 4951da177e4SLinus Torvalds goto out; 4961da177e4SLinus Torvalds 4974ae28944SPavel Emelyanov ASSERT_RTNL(); 4984ae28944SPavel Emelyanov 4991da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 5001da177e4SLinus Torvalds if (!n) 5011da177e4SLinus Torvalds goto out; 5021da177e4SLinus Torvalds 503426b5303SEric W. Biederman n->net = hold_net(net); 5041da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 5051da177e4SLinus Torvalds n->dev = dev; 5061da177e4SLinus Torvalds if (dev) 5071da177e4SLinus Torvalds dev_hold(dev); 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 5101da177e4SLinus Torvalds if (dev) 5111da177e4SLinus Torvalds dev_put(dev); 512da12f735SDenis V. Lunev release_net(net); 5131da177e4SLinus Torvalds kfree(n); 5141da177e4SLinus Torvalds n = NULL; 5151da177e4SLinus Torvalds goto out; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 5191da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 5201da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 5211da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5221da177e4SLinus Torvalds out: 5231da177e4SLinus Torvalds return n; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds 527426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 5281da177e4SLinus Torvalds struct net_device *dev) 5291da177e4SLinus Torvalds { 5301da177e4SLinus Torvalds struct pneigh_entry *n, **np; 5311da177e4SLinus Torvalds int key_len = tbl->key_len; 5321da177e4SLinus Torvalds u32 hash_val = *(u32 *)(pkey + key_len - 4); 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds hash_val ^= (hash_val >> 16); 5351da177e4SLinus Torvalds hash_val ^= hash_val >> 8; 5361da177e4SLinus Torvalds hash_val ^= hash_val >> 4; 5371da177e4SLinus Torvalds hash_val &= PNEIGH_HASHMASK; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 5401da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 5411da177e4SLinus Torvalds np = &n->next) { 542426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 543426b5303SEric W. Biederman (n->net == net)) { 5441da177e4SLinus Torvalds *np = n->next; 5451da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5461da177e4SLinus Torvalds if (tbl->pdestructor) 5471da177e4SLinus Torvalds tbl->pdestructor(n); 5481da177e4SLinus Torvalds if (n->dev) 5491da177e4SLinus Torvalds dev_put(n->dev); 550426b5303SEric W. Biederman release_net(n->net); 5511da177e4SLinus Torvalds kfree(n); 5521da177e4SLinus Torvalds return 0; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5561da177e4SLinus Torvalds return -ENOENT; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds struct pneigh_entry *n, **np; 5621da177e4SLinus Torvalds u32 h; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 5651da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 5661da177e4SLinus Torvalds while ((n = *np) != NULL) { 5671da177e4SLinus Torvalds if (!dev || n->dev == dev) { 5681da177e4SLinus Torvalds *np = n->next; 5691da177e4SLinus Torvalds if (tbl->pdestructor) 5701da177e4SLinus Torvalds tbl->pdestructor(n); 5711da177e4SLinus Torvalds if (n->dev) 5721da177e4SLinus Torvalds dev_put(n->dev); 573426b5303SEric W. Biederman release_net(n->net); 5741da177e4SLinus Torvalds kfree(n); 5751da177e4SLinus Torvalds continue; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds np = &n->next; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds return -ENOENT; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 58306f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 58406f0511dSDenis V. Lunev 58506f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 58606f0511dSDenis V. Lunev { 58706f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 58806f0511dSDenis V. Lunev neigh_parms_destroy(parms); 58906f0511dSDenis V. Lunev } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* 5921da177e4SLinus Torvalds * neighbour must already be out of the table; 5931da177e4SLinus Torvalds * 5941da177e4SLinus Torvalds */ 5951da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 5961da177e4SLinus Torvalds { 5971da177e4SLinus Torvalds struct hh_cache *hh; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (!neigh->dead) { 6021da177e4SLinus Torvalds printk(KERN_WARNING 6031da177e4SLinus Torvalds "Destroying alive neighbour %p\n", neigh); 6041da177e4SLinus Torvalds dump_stack(); 6051da177e4SLinus Torvalds return; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 6091da177e4SLinus Torvalds printk(KERN_WARNING "Impossible event.\n"); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds while ((hh = neigh->hh) != NULL) { 6121da177e4SLinus Torvalds neigh->hh = hh->hh_next; 6131da177e4SLinus Torvalds hh->hh_next = NULL; 6143644f0ceSStephen Hemminger 6153644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 6161da177e4SLinus Torvalds hh->hh_output = neigh_blackhole; 6173644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 6181da177e4SLinus Torvalds if (atomic_dec_and_test(&hh->hh_refcnt)) 6191da177e4SLinus Torvalds kfree(hh); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds dev_put(neigh->dev); 6251da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 6301da177e4SLinus Torvalds kmem_cache_free(neigh->tbl->kmem_cachep, neigh); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* Neighbour state is suspicious; 6341da177e4SLinus Torvalds disable fast path. 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds Called with write_locked neigh. 6371da177e4SLinus Torvalds */ 6381da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds struct hh_cache *hh; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds neigh->output = neigh->ops->output; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 6471da177e4SLinus Torvalds hh->hh_output = neigh->ops->output; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* Neighbour state is OK; 6511da177e4SLinus Torvalds enable fast path. 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds Called with write_locked neigh. 6541da177e4SLinus Torvalds */ 6551da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 6561da177e4SLinus Torvalds { 6571da177e4SLinus Torvalds struct hh_cache *hh; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is connected.\n", neigh); 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) 6641da177e4SLinus Torvalds hh->hh_output = neigh->ops->hh_output; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds static void neigh_periodic_timer(unsigned long arg) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 6701da177e4SLinus Torvalds struct neighbour *n, **np; 6711da177e4SLinus Torvalds unsigned long expire, now = jiffies; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds write_lock(&tbl->lock); 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /* 6781da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 6791da177e4SLinus Torvalds */ 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (time_after(now, tbl->last_rand + 300 * HZ)) { 6821da177e4SLinus Torvalds struct neigh_parms *p; 6831da177e4SLinus Torvalds tbl->last_rand = now; 6841da177e4SLinus Torvalds for (p = &tbl->parms; p; p = p->next) 6851da177e4SLinus Torvalds p->reachable_time = 6861da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds np = &tbl->hash_buckets[tbl->hash_chain_gc]; 6901da177e4SLinus Torvalds tbl->hash_chain_gc = ((tbl->hash_chain_gc + 1) & tbl->hash_mask); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds while ((n = *np) != NULL) { 6931da177e4SLinus Torvalds unsigned int state; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds write_lock(&n->lock); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds state = n->nud_state; 6981da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 6991da177e4SLinus Torvalds write_unlock(&n->lock); 7001da177e4SLinus Torvalds goto next_elt; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 7041da177e4SLinus Torvalds n->used = n->confirmed; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 7071da177e4SLinus Torvalds (state == NUD_FAILED || 7081da177e4SLinus Torvalds time_after(now, n->used + n->parms->gc_staletime))) { 7091da177e4SLinus Torvalds *np = n->next; 7101da177e4SLinus Torvalds n->dead = 1; 7111da177e4SLinus Torvalds write_unlock(&n->lock); 7124f494554SThomas Graf neigh_cleanup_and_release(n); 7131da177e4SLinus Torvalds continue; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds write_unlock(&n->lock); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds next_elt: 7181da177e4SLinus Torvalds np = &n->next; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* Cycle through all hash buckets every base_reachable_time/2 ticks. 7221da177e4SLinus Torvalds * ARP entry timeouts range from 1/2 base_reachable_time to 3/2 7231da177e4SLinus Torvalds * base_reachable_time. 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds expire = tbl->parms.base_reachable_time >> 1; 7261da177e4SLinus Torvalds expire /= (tbl->hash_mask + 1); 7271da177e4SLinus Torvalds if (!expire) 7281da177e4SLinus Torvalds expire = 1; 7291da177e4SLinus Torvalds 730f5a6e01cSArjan van de Ven if (expire>HZ) 731f5a6e01cSArjan van de Ven mod_timer(&tbl->gc_timer, round_jiffies(now + expire)); 732f5a6e01cSArjan van de Ven else 7331da177e4SLinus Torvalds mod_timer(&tbl->gc_timer, now + expire); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds write_unlock(&tbl->lock); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 7391da177e4SLinus Torvalds { 7401da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 7411da177e4SLinus Torvalds return (n->nud_state & NUD_PROBE ? 7421da177e4SLinus Torvalds p->ucast_probes : 7431da177e4SLinus Torvalds p->ucast_probes + p->app_probes + p->mcast_probes); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds unsigned long now, next; 7511da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 7521da177e4SLinus Torvalds unsigned state; 7531da177e4SLinus Torvalds int notify = 0; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds write_lock(&neigh->lock); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds state = neigh->nud_state; 7581da177e4SLinus Torvalds now = jiffies; 7591da177e4SLinus Torvalds next = now + HZ; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds if (!(state & NUD_IN_TIMER)) { 7621da177e4SLinus Torvalds #ifndef CONFIG_SMP 7631da177e4SLinus Torvalds printk(KERN_WARNING "neigh: timer & !nud_in_timer\n"); 7641da177e4SLinus Torvalds #endif 7651da177e4SLinus Torvalds goto out; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 7691da177e4SLinus Torvalds if (time_before_eq(now, 7701da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 7711da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is still alive.\n", neigh); 7721da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 7731da177e4SLinus Torvalds } else if (time_before_eq(now, 7741da177e4SLinus Torvalds neigh->used + neigh->parms->delay_probe_time)) { 7751da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 7761da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 777955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 7781da177e4SLinus Torvalds neigh_suspect(neigh); 7791da177e4SLinus Torvalds next = now + neigh->parms->delay_probe_time; 7801da177e4SLinus Torvalds } else { 7811da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); 7821da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 783955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 7841da177e4SLinus Torvalds neigh_suspect(neigh); 7858d71740cSTom Tucker notify = 1; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 7881da177e4SLinus Torvalds if (time_before_eq(now, 7891da177e4SLinus Torvalds neigh->confirmed + neigh->parms->delay_probe_time)) { 7901da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh); 7911da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 792955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 7931da177e4SLinus Torvalds neigh_connect(neigh); 7948d71740cSTom Tucker notify = 1; 7951da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 7961da177e4SLinus Torvalds } else { 7971da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is probed.\n", neigh); 7981da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 799955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8001da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 8011da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds } else { 8041da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 8051da177e4SLinus Torvalds next = now + neigh->parms->retrans_time; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 8091da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 8101da177e4SLinus Torvalds struct sk_buff *skb; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 813955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8141da177e4SLinus Torvalds notify = 1; 8151da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 8161da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is failed.\n", neigh); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* It is very thin place. report_unreachable is very complicated 8191da177e4SLinus Torvalds routine. Particularly, it can hit the same neighbour entry! 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds So that, we try to be accurate and avoid dead loop. --ANK 8221da177e4SLinus Torvalds */ 8231da177e4SLinus Torvalds while (neigh->nud_state == NUD_FAILED && 8241da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 8251da177e4SLinus Torvalds write_unlock(&neigh->lock); 8261da177e4SLinus Torvalds neigh->ops->error_report(neigh, skb); 8271da177e4SLinus Torvalds write_lock(&neigh->lock); 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 8331da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 8341da177e4SLinus Torvalds next = jiffies + HZ/2; 8356fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 8366fb9974fSHerbert Xu neigh_hold(neigh); 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 8391da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&neigh->arp_queue); 8409ff56607SDavid S. Miller /* keep skb alive even if arp_queue overflows */ 8419ff56607SDavid S. Miller if (skb) 8429ff56607SDavid S. Miller skb_get(skb); 8439ff56607SDavid S. Miller write_unlock(&neigh->lock); 8441da177e4SLinus Torvalds neigh->ops->solicit(neigh, skb); 8451da177e4SLinus Torvalds atomic_inc(&neigh->probes); 8469ff56607SDavid S. Miller if (skb) 8479ff56607SDavid S. Miller kfree_skb(skb); 8489ff56607SDavid S. Miller } else { 8491da177e4SLinus Torvalds out: 8501da177e4SLinus Torvalds write_unlock(&neigh->lock); 8519ff56607SDavid S. Miller } 8521da177e4SLinus Torvalds 853d961db35SThomas Graf if (notify) 854d961db35SThomas Graf neigh_update_notify(neigh); 855d961db35SThomas Graf 8561da177e4SLinus Torvalds neigh_release(neigh); 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 8601da177e4SLinus Torvalds { 8611da177e4SLinus Torvalds int rc; 8621da177e4SLinus Torvalds unsigned long now; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds rc = 0; 8671da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 8681da177e4SLinus Torvalds goto out_unlock_bh; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds now = jiffies; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 8731da177e4SLinus Torvalds if (neigh->parms->mcast_probes + neigh->parms->app_probes) { 8741da177e4SLinus Torvalds atomic_set(&neigh->probes, neigh->parms->ucast_probes); 8751da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 876955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 877667347f1SDavid S. Miller neigh_add_timer(neigh, now + 1); 8781da177e4SLinus Torvalds } else { 8791da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 880955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8811da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds if (skb) 8841da177e4SLinus Torvalds kfree_skb(skb); 8851da177e4SLinus Torvalds return 1; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 8881da177e4SLinus Torvalds NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); 8891da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 890955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 891667347f1SDavid S. Miller neigh_add_timer(neigh, 892667347f1SDavid S. Miller jiffies + neigh->parms->delay_probe_time); 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 8961da177e4SLinus Torvalds if (skb) { 8971da177e4SLinus Torvalds if (skb_queue_len(&neigh->arp_queue) >= 8981da177e4SLinus Torvalds neigh->parms->queue_len) { 8991da177e4SLinus Torvalds struct sk_buff *buff; 9001da177e4SLinus Torvalds buff = neigh->arp_queue.next; 9011da177e4SLinus Torvalds __skb_unlink(buff, &neigh->arp_queue); 9021da177e4SLinus Torvalds kfree_skb(buff); 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds rc = 1; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds out_unlock_bh: 9091da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9101da177e4SLinus Torvalds return rc; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 913e92b43a3SStephen Hemminger static void neigh_update_hhs(struct neighbour *neigh) 9141da177e4SLinus Torvalds { 9151da177e4SLinus Torvalds struct hh_cache *hh; 9163b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 9173b04dddeSStephen Hemminger = neigh->dev->header_ops->cache_update; 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds if (update) { 9201da177e4SLinus Torvalds for (hh = neigh->hh; hh; hh = hh->hh_next) { 9213644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 9221da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 9233644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds /* Generic update routine. 9311da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 9321da177e4SLinus Torvalds -- new is new state. 9331da177e4SLinus Torvalds -- flags 9341da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 9351da177e4SLinus Torvalds if it is different. 9361da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 9371da177e4SLinus Torvalds lladdr instead of overriding it 9381da177e4SLinus Torvalds if it is different. 9391da177e4SLinus Torvalds It also allows to retain current state 9401da177e4SLinus Torvalds if lladdr is unchanged. 9411da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 9441da177e4SLinus Torvalds NTF_ROUTER flag. 9451da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 9461da177e4SLinus Torvalds a router. 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 9491da177e4SLinus Torvalds */ 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 9521da177e4SLinus Torvalds u32 flags) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds u8 old; 9551da177e4SLinus Torvalds int err; 9561da177e4SLinus Torvalds int notify = 0; 9571da177e4SLinus Torvalds struct net_device *dev; 9581da177e4SLinus Torvalds int update_isrouter = 0; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds dev = neigh->dev; 9631da177e4SLinus Torvalds old = neigh->nud_state; 9641da177e4SLinus Torvalds err = -EPERM; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 9671da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 9681da177e4SLinus Torvalds goto out; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 9711da177e4SLinus Torvalds neigh_del_timer(neigh); 9721da177e4SLinus Torvalds if (old & NUD_CONNECTED) 9731da177e4SLinus Torvalds neigh_suspect(neigh); 9741da177e4SLinus Torvalds neigh->nud_state = new; 9751da177e4SLinus Torvalds err = 0; 9761da177e4SLinus Torvalds notify = old & NUD_VALID; 9771da177e4SLinus Torvalds goto out; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 9811da177e4SLinus Torvalds if (!dev->addr_len) { 9821da177e4SLinus Torvalds /* First case: device needs no address. */ 9831da177e4SLinus Torvalds lladdr = neigh->ha; 9841da177e4SLinus Torvalds } else if (lladdr) { 9851da177e4SLinus Torvalds /* The second case: if something is already cached 9861da177e4SLinus Torvalds and a new address is proposed: 9871da177e4SLinus Torvalds - compare new & old 9881da177e4SLinus Torvalds - if they are different, check override flag 9891da177e4SLinus Torvalds */ 9901da177e4SLinus Torvalds if ((old & NUD_VALID) && 9911da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 9921da177e4SLinus Torvalds lladdr = neigh->ha; 9931da177e4SLinus Torvalds } else { 9941da177e4SLinus Torvalds /* No address is supplied; if we know something, 9951da177e4SLinus Torvalds use it, otherwise discard the request. 9961da177e4SLinus Torvalds */ 9971da177e4SLinus Torvalds err = -EINVAL; 9981da177e4SLinus Torvalds if (!(old & NUD_VALID)) 9991da177e4SLinus Torvalds goto out; 10001da177e4SLinus Torvalds lladdr = neigh->ha; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds if (new & NUD_CONNECTED) 10041da177e4SLinus Torvalds neigh->confirmed = jiffies; 10051da177e4SLinus Torvalds neigh->updated = jiffies; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds /* If entry was valid and address is not changed, 10081da177e4SLinus Torvalds do not change entry state, if new one is STALE. 10091da177e4SLinus Torvalds */ 10101da177e4SLinus Torvalds err = 0; 10111da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 10121da177e4SLinus Torvalds if (old & NUD_VALID) { 10131da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 10141da177e4SLinus Torvalds update_isrouter = 0; 10151da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 10161da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 10171da177e4SLinus Torvalds lladdr = neigh->ha; 10181da177e4SLinus Torvalds new = NUD_STALE; 10191da177e4SLinus Torvalds } else 10201da177e4SLinus Torvalds goto out; 10211da177e4SLinus Torvalds } else { 10221da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 10231da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 10241da177e4SLinus Torvalds (old & NUD_CONNECTED)) 10251da177e4SLinus Torvalds ) 10261da177e4SLinus Torvalds new = old; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds if (new != old) { 10311da177e4SLinus Torvalds neigh_del_timer(neigh); 1032a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1033667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 10341da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1035667347f1SDavid S. Miller neigh->parms->reachable_time : 1036667347f1SDavid S. Miller 0))); 10371da177e4SLinus Torvalds neigh->nud_state = new; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds if (lladdr != neigh->ha) { 10411da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 10421da177e4SLinus Torvalds neigh_update_hhs(neigh); 10431da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 10441da177e4SLinus Torvalds neigh->confirmed = jiffies - 10451da177e4SLinus Torvalds (neigh->parms->base_reachable_time << 1); 10461da177e4SLinus Torvalds notify = 1; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds if (new == old) 10491da177e4SLinus Torvalds goto out; 10501da177e4SLinus Torvalds if (new & NUD_CONNECTED) 10511da177e4SLinus Torvalds neigh_connect(neigh); 10521da177e4SLinus Torvalds else 10531da177e4SLinus Torvalds neigh_suspect(neigh); 10541da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 10551da177e4SLinus Torvalds struct sk_buff *skb; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 10601da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 10611da177e4SLinus Torvalds struct neighbour *n1 = neigh; 10621da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 10631da177e4SLinus Torvalds /* On shaper/eql skb->dst->neighbour != neigh :( */ 10641da177e4SLinus Torvalds if (skb->dst && skb->dst->neighbour) 10651da177e4SLinus Torvalds n1 = skb->dst->neighbour; 10661da177e4SLinus Torvalds n1->output(skb); 10671da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds skb_queue_purge(&neigh->arp_queue); 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds out: 10721da177e4SLinus Torvalds if (update_isrouter) { 10731da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 10741da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 10751da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 10788d71740cSTom Tucker 10798d71740cSTom Tucker if (notify) 1080d961db35SThomas Graf neigh_update_notify(neigh); 1081d961db35SThomas Graf 10821da177e4SLinus Torvalds return err; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 10861da177e4SLinus Torvalds u8 *lladdr, void *saddr, 10871da177e4SLinus Torvalds struct net_device *dev) 10881da177e4SLinus Torvalds { 10891da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 10901da177e4SLinus Torvalds lladdr || !dev->addr_len); 10911da177e4SLinus Torvalds if (neigh) 10921da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 10931da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 10941da177e4SLinus Torvalds return neigh; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, 1098d77072ecSAl Viro __be16 protocol) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct hh_cache *hh; 11011da177e4SLinus Torvalds struct net_device *dev = dst->dev; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds for (hh = n->hh; hh; hh = hh->hh_next) 11041da177e4SLinus Torvalds if (hh->hh_type == protocol) 11051da177e4SLinus Torvalds break; 11061da177e4SLinus Torvalds 110777d04bd9SAndrew Morton if (!hh && (hh = kzalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) { 11083644f0ceSStephen Hemminger seqlock_init(&hh->hh_lock); 11091da177e4SLinus Torvalds hh->hh_type = protocol; 11101da177e4SLinus Torvalds atomic_set(&hh->hh_refcnt, 0); 11111da177e4SLinus Torvalds hh->hh_next = NULL; 11123b04dddeSStephen Hemminger 11133b04dddeSStephen Hemminger if (dev->header_ops->cache(n, hh)) { 11141da177e4SLinus Torvalds kfree(hh); 11151da177e4SLinus Torvalds hh = NULL; 11161da177e4SLinus Torvalds } else { 11171da177e4SLinus Torvalds atomic_inc(&hh->hh_refcnt); 11181da177e4SLinus Torvalds hh->hh_next = n->hh; 11191da177e4SLinus Torvalds n->hh = hh; 11201da177e4SLinus Torvalds if (n->nud_state & NUD_CONNECTED) 11211da177e4SLinus Torvalds hh->hh_output = n->ops->hh_output; 11221da177e4SLinus Torvalds else 11231da177e4SLinus Torvalds hh->hh_output = n->ops->output; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds if (hh) { 11271da177e4SLinus Torvalds atomic_inc(&hh->hh_refcnt); 11281da177e4SLinus Torvalds dst->hh = hh; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* This function can be used in contexts, where only old dev_queue_xmit 11331da177e4SLinus Torvalds worked, f.e. if you want to override normal output path (eql, shaper), 11341da177e4SLinus Torvalds but resolution is not made yet. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds int neigh_compat_output(struct sk_buff *skb) 11381da177e4SLinus Torvalds { 11391da177e4SLinus Torvalds struct net_device *dev = skb->dev; 11401da177e4SLinus Torvalds 1141bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 11421da177e4SLinus Torvalds 11430c4e8581SStephen Hemminger if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 11441da177e4SLinus Torvalds skb->len) < 0 && 11453b04dddeSStephen Hemminger dev->header_ops->rebuild(skb)) 11461da177e4SLinus Torvalds return 0; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds return dev_queue_xmit(skb); 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* Slow and careful. */ 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds int neigh_resolve_output(struct sk_buff *skb) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct dst_entry *dst = skb->dst; 11561da177e4SLinus Torvalds struct neighbour *neigh; 11571da177e4SLinus Torvalds int rc = 0; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds if (!dst || !(neigh = dst->neighbour)) 11601da177e4SLinus Torvalds goto discard; 11611da177e4SLinus Torvalds 1162bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 11651da177e4SLinus Torvalds int err; 11661da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 11673b04dddeSStephen Hemminger if (dev->header_ops->cache && !dst->hh) { 11681da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 11691da177e4SLinus Torvalds if (!dst->hh) 11701da177e4SLinus Torvalds neigh_hh_init(neigh, dst, dst->ops->protocol); 11710c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 11721da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 11731da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 11741da177e4SLinus Torvalds } else { 11751da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 11760c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 11771da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 11781da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds if (err >= 0) 11811da177e4SLinus Torvalds rc = neigh->ops->queue_xmit(skb); 11821da177e4SLinus Torvalds else 11831da177e4SLinus Torvalds goto out_kfree_skb; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds out: 11861da177e4SLinus Torvalds return rc; 11871da177e4SLinus Torvalds discard: 11881da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", 11891da177e4SLinus Torvalds dst, dst ? dst->neighbour : NULL); 11901da177e4SLinus Torvalds out_kfree_skb: 11911da177e4SLinus Torvalds rc = -EINVAL; 11921da177e4SLinus Torvalds kfree_skb(skb); 11931da177e4SLinus Torvalds goto out; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /* As fast as possible without hh cache */ 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds int neigh_connected_output(struct sk_buff *skb) 11991da177e4SLinus Torvalds { 12001da177e4SLinus Torvalds int err; 12011da177e4SLinus Torvalds struct dst_entry *dst = skb->dst; 12021da177e4SLinus Torvalds struct neighbour *neigh = dst->neighbour; 12031da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 12041da177e4SLinus Torvalds 1205bbe735e4SArnaldo Carvalho de Melo __skb_pull(skb, skb_network_offset(skb)); 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 12080c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12091da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12101da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 12111da177e4SLinus Torvalds if (err >= 0) 12121da177e4SLinus Torvalds err = neigh->ops->queue_xmit(skb); 12131da177e4SLinus Torvalds else { 12141da177e4SLinus Torvalds err = -EINVAL; 12151da177e4SLinus Torvalds kfree_skb(skb); 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds return err; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 12211da177e4SLinus Torvalds { 12221da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 12231da177e4SLinus Torvalds long sched_next = 0; 12241da177e4SLinus Torvalds unsigned long now = jiffies; 12251da177e4SLinus Torvalds struct sk_buff *skb; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds skb = tbl->proxy_queue.next; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds while (skb != (struct sk_buff *)&tbl->proxy_queue) { 12321da177e4SLinus Torvalds struct sk_buff *back = skb; 1233a61bbcf2SPatrick McHardy long tdif = NEIGH_CB(back)->sched_next - now; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds skb = skb->next; 12361da177e4SLinus Torvalds if (tdif <= 0) { 12371da177e4SLinus Torvalds struct net_device *dev = back->dev; 12381da177e4SLinus Torvalds __skb_unlink(back, &tbl->proxy_queue); 12391da177e4SLinus Torvalds if (tbl->proxy_redo && netif_running(dev)) 12401da177e4SLinus Torvalds tbl->proxy_redo(back); 12411da177e4SLinus Torvalds else 12421da177e4SLinus Torvalds kfree_skb(back); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds dev_put(dev); 12451da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 12461da177e4SLinus Torvalds sched_next = tdif; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 12491da177e4SLinus Torvalds if (sched_next) 12501da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 12511da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 12551da177e4SLinus Torvalds struct sk_buff *skb) 12561da177e4SLinus Torvalds { 12571da177e4SLinus Torvalds unsigned long now = jiffies; 12581da177e4SLinus Torvalds unsigned long sched_next = now + (net_random() % p->proxy_delay); 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds if (tbl->proxy_queue.qlen > p->proxy_qlen) { 12611da177e4SLinus Torvalds kfree_skb(skb); 12621da177e4SLinus Torvalds return; 12631da177e4SLinus Torvalds } 1264a61bbcf2SPatrick McHardy 1265a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1266a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 12691da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 12701da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 12711da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds dst_release(skb->dst); 12741da177e4SLinus Torvalds skb->dst = NULL; 12751da177e4SLinus Torvalds dev_hold(skb->dev); 12761da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 12771da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 12781da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 1281426b5303SEric W. Biederman static inline struct neigh_parms *lookup_neigh_params(struct neigh_table *tbl, 1282426b5303SEric W. Biederman struct net *net, int ifindex) 1283426b5303SEric W. Biederman { 1284426b5303SEric W. Biederman struct neigh_parms *p; 1285426b5303SEric W. Biederman 1286426b5303SEric W. Biederman for (p = &tbl->parms; p; p = p->next) { 1287426b5303SEric W. Biederman if (p->net != net) 1288426b5303SEric W. Biederman continue; 1289426b5303SEric W. Biederman if ((p->dev && p->dev->ifindex == ifindex) || 1290426b5303SEric W. Biederman (!p->dev && !ifindex)) 1291426b5303SEric W. Biederman return p; 1292426b5303SEric W. Biederman } 1293426b5303SEric W. Biederman 1294426b5303SEric W. Biederman return NULL; 1295426b5303SEric W. Biederman } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 12981da177e4SLinus Torvalds struct neigh_table *tbl) 12991da177e4SLinus Torvalds { 1300426b5303SEric W. Biederman struct neigh_parms *p, *ref; 1301426b5303SEric W. Biederman struct net *net; 13021da177e4SLinus Torvalds 1303426b5303SEric W. Biederman net = dev->nd_net; 1304426b5303SEric W. Biederman ref = lookup_neigh_params(tbl, net, 0); 1305426b5303SEric W. Biederman if (!ref) 1306426b5303SEric W. Biederman return NULL; 1307426b5303SEric W. Biederman 1308426b5303SEric W. Biederman p = kmemdup(ref, sizeof(*p), GFP_KERNEL); 13091da177e4SLinus Torvalds if (p) { 13101da177e4SLinus Torvalds p->tbl = tbl; 13111da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 13121da177e4SLinus Torvalds INIT_RCU_HEAD(&p->rcu_head); 13131da177e4SLinus Torvalds p->reachable_time = 13141da177e4SLinus Torvalds neigh_rand_reach_time(p->base_reachable_time); 1315486b51d3SDenis V. Lunev 1316c7fb64dbSThomas Graf if (dev->neigh_setup && dev->neigh_setup(dev, p)) { 13171da177e4SLinus Torvalds kfree(p); 13181da177e4SLinus Torvalds return NULL; 13191da177e4SLinus Torvalds } 1320c7fb64dbSThomas Graf 1321c7fb64dbSThomas Graf dev_hold(dev); 1322c7fb64dbSThomas Graf p->dev = dev; 1323426b5303SEric W. Biederman p->net = hold_net(net); 13241da177e4SLinus Torvalds p->sysctl_table = NULL; 13251da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 13261da177e4SLinus Torvalds p->next = tbl->parms.next; 13271da177e4SLinus Torvalds tbl->parms.next = p; 13281da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds return p; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds struct neigh_parms *parms = 13361da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds neigh_parms_put(parms); 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 13421da177e4SLinus Torvalds { 13431da177e4SLinus Torvalds struct neigh_parms **p; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 13461da177e4SLinus Torvalds return; 13471da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 13481da177e4SLinus Torvalds for (p = &tbl->parms.next; *p; p = &(*p)->next) { 13491da177e4SLinus Torvalds if (*p == parms) { 13501da177e4SLinus Torvalds *p = parms->next; 13511da177e4SLinus Torvalds parms->dead = 1; 13521da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1353cecbb639SDavid S. Miller if (parms->dev) 1354cecbb639SDavid S. Miller dev_put(parms->dev); 13551da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 13561da177e4SLinus Torvalds return; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 13601da177e4SLinus Torvalds NEIGH_PRINTK1("neigh_parms_release: not found\n"); 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 136306f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 13641da177e4SLinus Torvalds { 1365426b5303SEric W. Biederman release_net(parms->net); 13661da177e4SLinus Torvalds kfree(parms); 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 1369c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1370c2ecba71SPavel Emelianov 1371bd89efc5SSimon Kelley void neigh_table_init_no_netlink(struct neigh_table *tbl) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds unsigned long now = jiffies; 13741da177e4SLinus Torvalds unsigned long phsize; 13751da177e4SLinus Torvalds 1376426b5303SEric W. Biederman tbl->parms.net = &init_net; 13771da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 13781da177e4SLinus Torvalds INIT_RCU_HEAD(&tbl->parms.rcu_head); 13791da177e4SLinus Torvalds tbl->parms.reachable_time = 13801da177e4SLinus Torvalds neigh_rand_reach_time(tbl->parms.base_reachable_time); 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds if (!tbl->kmem_cachep) 1383e5d679f3SAlexey Dobriyan tbl->kmem_cachep = 1384e5d679f3SAlexey Dobriyan kmem_cache_create(tbl->id, tbl->entry_size, 0, 1385e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 138620c2df83SPaul Mundt NULL); 13871da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 13881da177e4SLinus Torvalds if (!tbl->stats) 13891da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 139246ecf0b9SWang Chen tbl->pde = proc_create(tbl->id, 0, init_net.proc_net_stat, 139346ecf0b9SWang Chen &neigh_stat_seq_fops); 13941da177e4SLinus Torvalds if (!tbl->pde) 13951da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 13961da177e4SLinus Torvalds tbl->pde->data = tbl; 13971da177e4SLinus Torvalds #endif 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds tbl->hash_mask = 1; 14001da177e4SLinus Torvalds tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1); 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 140377d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds if (!tbl->hash_buckets || !tbl->phash_buckets) 14061da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1411b24b8a24SPavel Emelyanov setup_timer(&tbl->gc_timer, neigh_periodic_timer, (unsigned long)tbl); 14121da177e4SLinus Torvalds tbl->gc_timer.expires = now + 1; 14131da177e4SLinus Torvalds add_timer(&tbl->gc_timer); 14141da177e4SLinus Torvalds 1415b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1416c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1417c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds tbl->last_flush = now; 14201da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1421bd89efc5SSimon Kelley } 1422bd89efc5SSimon Kelley 1423bd89efc5SSimon Kelley void neigh_table_init(struct neigh_table *tbl) 1424bd89efc5SSimon Kelley { 1425bd89efc5SSimon Kelley struct neigh_table *tmp; 1426bd89efc5SSimon Kelley 1427bd89efc5SSimon Kelley neigh_table_init_no_netlink(tbl); 14281da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 1429bd89efc5SSimon Kelley for (tmp = neigh_tables; tmp; tmp = tmp->next) { 1430bd89efc5SSimon Kelley if (tmp->family == tbl->family) 1431bd89efc5SSimon Kelley break; 1432bd89efc5SSimon Kelley } 14331da177e4SLinus Torvalds tbl->next = neigh_tables; 14341da177e4SLinus Torvalds neigh_tables = tbl; 14351da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 1436bd89efc5SSimon Kelley 1437bd89efc5SSimon Kelley if (unlikely(tmp)) { 1438bd89efc5SSimon Kelley printk(KERN_ERR "NEIGH: Registering multiple tables for " 1439bd89efc5SSimon Kelley "family %d\n", tbl->family); 1440bd89efc5SSimon Kelley dump_stack(); 1441bd89efc5SSimon Kelley } 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds int neigh_table_clear(struct neigh_table *tbl) 14451da177e4SLinus Torvalds { 14461da177e4SLinus Torvalds struct neigh_table **tp; 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 14491da177e4SLinus Torvalds del_timer_sync(&tbl->gc_timer); 14501da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 14511da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 14521da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 14531da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 14541da177e4SLinus Torvalds printk(KERN_CRIT "neighbour leakage\n"); 14551da177e4SLinus Torvalds write_lock(&neigh_tbl_lock); 14561da177e4SLinus Torvalds for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { 14571da177e4SLinus Torvalds if (*tp == tbl) { 14581da177e4SLinus Torvalds *tp = tbl->next; 14591da177e4SLinus Torvalds break; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds write_unlock(&neigh_tbl_lock); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1); 14651da177e4SLinus Torvalds tbl->hash_buckets = NULL; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds kfree(tbl->phash_buckets); 14681da177e4SLinus Torvalds tbl->phash_buckets = NULL; 14691da177e4SLinus Torvalds 14703f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 14713f192b5cSAlexey Dobriyan 14723fcde74bSKirill Korotaev free_percpu(tbl->stats); 14733fcde74bSKirill Korotaev tbl->stats = NULL; 14743fcde74bSKirill Korotaev 1475bfb85c9fSRandy Dunlap kmem_cache_destroy(tbl->kmem_cachep); 1476bfb85c9fSRandy Dunlap tbl->kmem_cachep = NULL; 1477bfb85c9fSRandy Dunlap 14781da177e4SLinus Torvalds return 0; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 1481c8822a4eSThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 14821da177e4SLinus Torvalds { 1483881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 1484a14a49d2SThomas Graf struct ndmsg *ndm; 1485a14a49d2SThomas Graf struct nlattr *dst_attr; 14861da177e4SLinus Torvalds struct neigh_table *tbl; 14871da177e4SLinus Torvalds struct net_device *dev = NULL; 1488a14a49d2SThomas Graf int err = -EINVAL; 14891da177e4SLinus Torvalds 1490a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 14911da177e4SLinus Torvalds goto out; 14921da177e4SLinus Torvalds 1493a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1494a14a49d2SThomas Graf if (dst_attr == NULL) 1495a14a49d2SThomas Graf goto out; 1496a14a49d2SThomas Graf 1497a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1498a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1499881d966bSEric W. Biederman dev = dev_get_by_index(net, ndm->ndm_ifindex); 1500a14a49d2SThomas Graf if (dev == NULL) { 1501a14a49d2SThomas Graf err = -ENODEV; 1502a14a49d2SThomas Graf goto out; 1503a14a49d2SThomas Graf } 1504a14a49d2SThomas Graf } 1505a14a49d2SThomas Graf 15061da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 15071da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1508a14a49d2SThomas Graf struct neighbour *neigh; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 15111da177e4SLinus Torvalds continue; 15121da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 15131da177e4SLinus Torvalds 1514a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 15151da177e4SLinus Torvalds goto out_dev_put; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1518426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 15191da177e4SLinus Torvalds goto out_dev_put; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 1522a14a49d2SThomas Graf if (dev == NULL) 1523a14a49d2SThomas Graf goto out_dev_put; 15241da177e4SLinus Torvalds 1525a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1526a14a49d2SThomas Graf if (neigh == NULL) { 1527a14a49d2SThomas Graf err = -ENOENT; 1528a14a49d2SThomas Graf goto out_dev_put; 1529a14a49d2SThomas Graf } 1530a14a49d2SThomas Graf 1531a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 15321da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 15331da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1534a14a49d2SThomas Graf neigh_release(neigh); 15351da177e4SLinus Torvalds goto out_dev_put; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 1538a14a49d2SThomas Graf err = -EAFNOSUPPORT; 1539a14a49d2SThomas Graf 15401da177e4SLinus Torvalds out_dev_put: 15411da177e4SLinus Torvalds if (dev) 15421da177e4SLinus Torvalds dev_put(dev); 15431da177e4SLinus Torvalds out: 15441da177e4SLinus Torvalds return err; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 1547c8822a4eSThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 15481da177e4SLinus Torvalds { 1549881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 15505208debdSThomas Graf struct ndmsg *ndm; 15515208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 15521da177e4SLinus Torvalds struct neigh_table *tbl; 15531da177e4SLinus Torvalds struct net_device *dev = NULL; 15545208debdSThomas Graf int err; 15551da177e4SLinus Torvalds 15565208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 15575208debdSThomas Graf if (err < 0) 15581da177e4SLinus Torvalds goto out; 15591da177e4SLinus Torvalds 15605208debdSThomas Graf err = -EINVAL; 15615208debdSThomas Graf if (tb[NDA_DST] == NULL) 15625208debdSThomas Graf goto out; 15635208debdSThomas Graf 15645208debdSThomas Graf ndm = nlmsg_data(nlh); 15655208debdSThomas Graf if (ndm->ndm_ifindex) { 1566881d966bSEric W. Biederman dev = dev_get_by_index(net, ndm->ndm_ifindex); 15675208debdSThomas Graf if (dev == NULL) { 15685208debdSThomas Graf err = -ENODEV; 15695208debdSThomas Graf goto out; 15705208debdSThomas Graf } 15715208debdSThomas Graf 15725208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 15735208debdSThomas Graf goto out_dev_put; 15745208debdSThomas Graf } 15755208debdSThomas Graf 15761da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 15771da177e4SLinus Torvalds for (tbl = neigh_tables; tbl; tbl = tbl->next) { 15785208debdSThomas Graf int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 15795208debdSThomas Graf struct neighbour *neigh; 15805208debdSThomas Graf void *dst, *lladdr; 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds if (tbl->family != ndm->ndm_family) 15831da177e4SLinus Torvalds continue; 15841da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 15851da177e4SLinus Torvalds 15865208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 15871da177e4SLinus Torvalds goto out_dev_put; 15885208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 15895208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 159262dd9318SVille Nuorvala struct pneigh_entry *pn; 159362dd9318SVille Nuorvala 15945208debdSThomas Graf err = -ENOBUFS; 1595426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 159662dd9318SVille Nuorvala if (pn) { 159762dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 159862dd9318SVille Nuorvala err = 0; 159962dd9318SVille Nuorvala } 16001da177e4SLinus Torvalds goto out_dev_put; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 16035208debdSThomas Graf if (dev == NULL) 16041da177e4SLinus Torvalds goto out_dev_put; 16051da177e4SLinus Torvalds 16065208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 16075208debdSThomas Graf if (neigh == NULL) { 16085208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 16091da177e4SLinus Torvalds err = -ENOENT; 16101da177e4SLinus Torvalds goto out_dev_put; 16115208debdSThomas Graf } 16125208debdSThomas Graf 16135208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 16145208debdSThomas Graf if (IS_ERR(neigh)) { 16155208debdSThomas Graf err = PTR_ERR(neigh); 16161da177e4SLinus Torvalds goto out_dev_put; 16171da177e4SLinus Torvalds } 16185208debdSThomas Graf } else { 16195208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 16205208debdSThomas Graf err = -EEXIST; 16215208debdSThomas Graf neigh_release(neigh); 16225208debdSThomas Graf goto out_dev_put; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16255208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 16265208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 16275208debdSThomas Graf } 16281da177e4SLinus Torvalds 16295208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 16305208debdSThomas Graf neigh_release(neigh); 16311da177e4SLinus Torvalds goto out_dev_put; 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 16355208debdSThomas Graf err = -EAFNOSUPPORT; 16365208debdSThomas Graf 16371da177e4SLinus Torvalds out_dev_put: 16381da177e4SLinus Torvalds if (dev) 16391da177e4SLinus Torvalds dev_put(dev); 16401da177e4SLinus Torvalds out: 16411da177e4SLinus Torvalds return err; 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 1644c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1645c7fb64dbSThomas Graf { 1646ca860fb3SThomas Graf struct nlattr *nest; 1647e386c6ebSThomas Graf 1648ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1649ca860fb3SThomas Graf if (nest == NULL) 1650ca860fb3SThomas Graf return -ENOBUFS; 1651c7fb64dbSThomas Graf 1652c7fb64dbSThomas Graf if (parms->dev) 1653ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex); 1654c7fb64dbSThomas Graf 1655ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)); 1656ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len); 1657ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen); 1658ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes); 1659ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes); 1660ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes); 1661ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time); 1662ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME, 1663c7fb64dbSThomas Graf parms->base_reachable_time); 1664ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime); 1665ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time); 1666ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time); 1667ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay); 1668ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay); 1669ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime); 1670c7fb64dbSThomas Graf 1671ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1672c7fb64dbSThomas Graf 1673ca860fb3SThomas Graf nla_put_failure: 1674ca860fb3SThomas Graf return nla_nest_cancel(skb, nest); 1675c7fb64dbSThomas Graf } 1676c7fb64dbSThomas Graf 1677ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1678ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1679c7fb64dbSThomas Graf { 1680c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1681c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1682c7fb64dbSThomas Graf 1683ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1684ca860fb3SThomas Graf if (nlh == NULL) 168526932566SPatrick McHardy return -EMSGSIZE; 1686c7fb64dbSThomas Graf 1687ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1688c7fb64dbSThomas Graf 1689c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1690c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 16919ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 16929ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1693c7fb64dbSThomas Graf 1694ca860fb3SThomas Graf NLA_PUT_STRING(skb, NDTA_NAME, tbl->id); 1695ca860fb3SThomas Graf NLA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval); 1696ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1); 1697ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2); 1698ca860fb3SThomas Graf NLA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3); 1699c7fb64dbSThomas Graf 1700c7fb64dbSThomas Graf { 1701c7fb64dbSThomas Graf unsigned long now = jiffies; 1702c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1703c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1704c7fb64dbSThomas Graf 1705c7fb64dbSThomas Graf struct ndt_config ndc = { 1706c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1707c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1708c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1709c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1710c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1711c7fb64dbSThomas Graf .ndtc_hash_rnd = tbl->hash_rnd, 1712c7fb64dbSThomas Graf .ndtc_hash_mask = tbl->hash_mask, 1713c7fb64dbSThomas Graf .ndtc_hash_chain_gc = tbl->hash_chain_gc, 1714c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1715c7fb64dbSThomas Graf }; 1716c7fb64dbSThomas Graf 1717ca860fb3SThomas Graf NLA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc); 1718c7fb64dbSThomas Graf } 1719c7fb64dbSThomas Graf 1720c7fb64dbSThomas Graf { 1721c7fb64dbSThomas Graf int cpu; 1722c7fb64dbSThomas Graf struct ndt_stats ndst; 1723c7fb64dbSThomas Graf 1724c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1725c7fb64dbSThomas Graf 17266f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1727c7fb64dbSThomas Graf struct neigh_statistics *st; 1728c7fb64dbSThomas Graf 1729c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1730c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1731c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1732c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1733c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1734c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1735c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1736c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1737c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1738c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1739c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1740c7fb64dbSThomas Graf } 1741c7fb64dbSThomas Graf 1742ca860fb3SThomas Graf NLA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst); 1743c7fb64dbSThomas Graf } 1744c7fb64dbSThomas Graf 1745c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1746c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1747ca860fb3SThomas Graf goto nla_put_failure; 1748c7fb64dbSThomas Graf 1749c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1750ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1751c7fb64dbSThomas Graf 1752ca860fb3SThomas Graf nla_put_failure: 1753c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 175426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 175526932566SPatrick McHardy return -EMSGSIZE; 1756c7fb64dbSThomas Graf } 1757c7fb64dbSThomas Graf 1758ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1759ca860fb3SThomas Graf struct neigh_table *tbl, 1760c7fb64dbSThomas Graf struct neigh_parms *parms, 1761ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1762ca860fb3SThomas Graf unsigned int flags) 1763c7fb64dbSThomas Graf { 1764c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1765c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1766c7fb64dbSThomas Graf 1767ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1768ca860fb3SThomas Graf if (nlh == NULL) 176926932566SPatrick McHardy return -EMSGSIZE; 1770c7fb64dbSThomas Graf 1771ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1772c7fb64dbSThomas Graf 1773c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1774c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 17759ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 17769ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1777c7fb64dbSThomas Graf 1778ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1779ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1780ca860fb3SThomas Graf goto errout; 1781c7fb64dbSThomas Graf 1782c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1783ca860fb3SThomas Graf return nlmsg_end(skb, nlh); 1784ca860fb3SThomas Graf errout: 1785c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 178626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 178726932566SPatrick McHardy return -EMSGSIZE; 1788c7fb64dbSThomas Graf } 1789c7fb64dbSThomas Graf 1790ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 17916b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 17926b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 17936b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 17946b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 17956b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 17966b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 17976b3f8674SThomas Graf }; 17986b3f8674SThomas Graf 1799ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 18006b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 18016b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 18026b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 18036b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 18046b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 18056b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 18066b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 18076b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 18086b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 18096b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 18106b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 18116b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 18126b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 18136b3f8674SThomas Graf }; 18146b3f8674SThomas Graf 1815c8822a4eSThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1816c7fb64dbSThomas Graf { 1817b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 1818c7fb64dbSThomas Graf struct neigh_table *tbl; 18196b3f8674SThomas Graf struct ndtmsg *ndtmsg; 18206b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 18216b3f8674SThomas Graf int err; 1822c7fb64dbSThomas Graf 18236b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 18246b3f8674SThomas Graf nl_neightbl_policy); 18256b3f8674SThomas Graf if (err < 0) 18266b3f8674SThomas Graf goto errout; 1827c7fb64dbSThomas Graf 18286b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 18296b3f8674SThomas Graf err = -EINVAL; 18306b3f8674SThomas Graf goto errout; 18316b3f8674SThomas Graf } 18326b3f8674SThomas Graf 18336b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1834c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1835c7fb64dbSThomas Graf for (tbl = neigh_tables; tbl; tbl = tbl->next) { 1836c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1837c7fb64dbSThomas Graf continue; 1838c7fb64dbSThomas Graf 18396b3f8674SThomas Graf if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) 1840c7fb64dbSThomas Graf break; 1841c7fb64dbSThomas Graf } 1842c7fb64dbSThomas Graf 1843c7fb64dbSThomas Graf if (tbl == NULL) { 1844c7fb64dbSThomas Graf err = -ENOENT; 18456b3f8674SThomas Graf goto errout_locked; 1846c7fb64dbSThomas Graf } 1847c7fb64dbSThomas Graf 1848c7fb64dbSThomas Graf /* 1849c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1850c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1851c7fb64dbSThomas Graf */ 1852c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1853c7fb64dbSThomas Graf 18546b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 18556b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 1856c7fb64dbSThomas Graf struct neigh_parms *p; 18576b3f8674SThomas Graf int i, ifindex = 0; 1858c7fb64dbSThomas Graf 18596b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 18606b3f8674SThomas Graf nl_ntbl_parm_policy); 18616b3f8674SThomas Graf if (err < 0) 18626b3f8674SThomas Graf goto errout_tbl_lock; 1863c7fb64dbSThomas Graf 18646b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 18656b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 1866c7fb64dbSThomas Graf 1867426b5303SEric W. Biederman p = lookup_neigh_params(tbl, net, ifindex); 1868c7fb64dbSThomas Graf if (p == NULL) { 1869c7fb64dbSThomas Graf err = -ENOENT; 18706b3f8674SThomas Graf goto errout_tbl_lock; 1871c7fb64dbSThomas Graf } 1872c7fb64dbSThomas Graf 18736b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 18746b3f8674SThomas Graf if (tbp[i] == NULL) 18756b3f8674SThomas Graf continue; 1876c7fb64dbSThomas Graf 18776b3f8674SThomas Graf switch (i) { 18786b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 18796b3f8674SThomas Graf p->queue_len = nla_get_u32(tbp[i]); 18806b3f8674SThomas Graf break; 18816b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 18826b3f8674SThomas Graf p->proxy_qlen = nla_get_u32(tbp[i]); 18836b3f8674SThomas Graf break; 18846b3f8674SThomas Graf case NDTPA_APP_PROBES: 18856b3f8674SThomas Graf p->app_probes = nla_get_u32(tbp[i]); 18866b3f8674SThomas Graf break; 18876b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 18886b3f8674SThomas Graf p->ucast_probes = nla_get_u32(tbp[i]); 18896b3f8674SThomas Graf break; 18906b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 18916b3f8674SThomas Graf p->mcast_probes = nla_get_u32(tbp[i]); 18926b3f8674SThomas Graf break; 18936b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 18946b3f8674SThomas Graf p->base_reachable_time = nla_get_msecs(tbp[i]); 18956b3f8674SThomas Graf break; 18966b3f8674SThomas Graf case NDTPA_GC_STALETIME: 18976b3f8674SThomas Graf p->gc_staletime = nla_get_msecs(tbp[i]); 18986b3f8674SThomas Graf break; 18996b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 19006b3f8674SThomas Graf p->delay_probe_time = nla_get_msecs(tbp[i]); 19016b3f8674SThomas Graf break; 19026b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 19036b3f8674SThomas Graf p->retrans_time = nla_get_msecs(tbp[i]); 19046b3f8674SThomas Graf break; 19056b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 19066b3f8674SThomas Graf p->anycast_delay = nla_get_msecs(tbp[i]); 19076b3f8674SThomas Graf break; 19086b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 19096b3f8674SThomas Graf p->proxy_delay = nla_get_msecs(tbp[i]); 19106b3f8674SThomas Graf break; 19116b3f8674SThomas Graf case NDTPA_LOCKTIME: 19126b3f8674SThomas Graf p->locktime = nla_get_msecs(tbp[i]); 19136b3f8674SThomas Graf break; 1914c7fb64dbSThomas Graf } 19156b3f8674SThomas Graf } 19166b3f8674SThomas Graf } 19176b3f8674SThomas Graf 19186b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 19196b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 19206b3f8674SThomas Graf 19216b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 19226b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 19236b3f8674SThomas Graf 19246b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 19256b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 19266b3f8674SThomas Graf 19276b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 19286b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 1929c7fb64dbSThomas Graf 1930c7fb64dbSThomas Graf err = 0; 1931c7fb64dbSThomas Graf 19326b3f8674SThomas Graf errout_tbl_lock: 1933c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 19346b3f8674SThomas Graf errout_locked: 1935c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 19366b3f8674SThomas Graf errout: 1937c7fb64dbSThomas Graf return err; 1938c7fb64dbSThomas Graf } 1939c7fb64dbSThomas Graf 1940c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 1941c7fb64dbSThomas Graf { 1942b854272bSDenis V. Lunev struct net *net = skb->sk->sk_net; 1943ca860fb3SThomas Graf int family, tidx, nidx = 0; 1944ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 1945ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 1946c7fb64dbSThomas Graf struct neigh_table *tbl; 1947c7fb64dbSThomas Graf 1948ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 1949c7fb64dbSThomas Graf 1950c7fb64dbSThomas Graf read_lock(&neigh_tbl_lock); 1951ca860fb3SThomas Graf for (tbl = neigh_tables, tidx = 0; tbl; tbl = tbl->next, tidx++) { 1952c7fb64dbSThomas Graf struct neigh_parms *p; 1953c7fb64dbSThomas Graf 1954ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 1955c7fb64dbSThomas Graf continue; 1956c7fb64dbSThomas Graf 1957ca860fb3SThomas Graf if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).pid, 1958ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 1959ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 1960c7fb64dbSThomas Graf break; 1961c7fb64dbSThomas Graf 1962426b5303SEric W. Biederman for (nidx = 0, p = tbl->parms.next; p; p = p->next) { 1963426b5303SEric W. Biederman if (net != p->net) 1964426b5303SEric W. Biederman continue; 1965426b5303SEric W. Biederman 1966426b5303SEric W. Biederman if (nidx++ < neigh_skip) 1967c7fb64dbSThomas Graf continue; 1968c7fb64dbSThomas Graf 1969ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 1970ca860fb3SThomas Graf NETLINK_CB(cb->skb).pid, 1971ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 1972ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 1973ca860fb3SThomas Graf NLM_F_MULTI) <= 0) 1974c7fb64dbSThomas Graf goto out; 1975c7fb64dbSThomas Graf } 1976c7fb64dbSThomas Graf 1977ca860fb3SThomas Graf neigh_skip = 0; 1978c7fb64dbSThomas Graf } 1979c7fb64dbSThomas Graf out: 1980c7fb64dbSThomas Graf read_unlock(&neigh_tbl_lock); 1981ca860fb3SThomas Graf cb->args[0] = tidx; 1982ca860fb3SThomas Graf cb->args[1] = nidx; 1983c7fb64dbSThomas Graf 1984c7fb64dbSThomas Graf return skb->len; 1985c7fb64dbSThomas Graf } 19861da177e4SLinus Torvalds 19878b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 19888b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 19891da177e4SLinus Torvalds { 19901da177e4SLinus Torvalds unsigned long now = jiffies; 19911da177e4SLinus Torvalds struct nda_cacheinfo ci; 19928b8aec50SThomas Graf struct nlmsghdr *nlh; 19938b8aec50SThomas Graf struct ndmsg *ndm; 19941da177e4SLinus Torvalds 19958b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 19968b8aec50SThomas Graf if (nlh == NULL) 199726932566SPatrick McHardy return -EMSGSIZE; 19988b8aec50SThomas Graf 19998b8aec50SThomas Graf ndm = nlmsg_data(nlh); 20008b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 20019ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 20029ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 20038b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 20048b8aec50SThomas Graf ndm->ndm_type = neigh->type; 20058b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 20061da177e4SLinus Torvalds 20078b8aec50SThomas Graf NLA_PUT(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key); 20088b8aec50SThomas Graf 20098b8aec50SThomas Graf read_lock_bh(&neigh->lock); 20108b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 20118b8aec50SThomas Graf if ((neigh->nud_state & NUD_VALID) && 20128b8aec50SThomas Graf nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, neigh->ha) < 0) { 20138b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 20148b8aec50SThomas Graf goto nla_put_failure; 20158b8aec50SThomas Graf } 20168b8aec50SThomas Graf 20178b8aec50SThomas Graf ci.ndm_used = now - neigh->used; 20188b8aec50SThomas Graf ci.ndm_confirmed = now - neigh->confirmed; 20198b8aec50SThomas Graf ci.ndm_updated = now - neigh->updated; 20208b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 20218b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 20228b8aec50SThomas Graf 20238b8aec50SThomas Graf NLA_PUT_U32(skb, NDA_PROBES, atomic_read(&neigh->probes)); 20248b8aec50SThomas Graf NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); 20258b8aec50SThomas Graf 20268b8aec50SThomas Graf return nlmsg_end(skb, nlh); 20278b8aec50SThomas Graf 20288b8aec50SThomas Graf nla_put_failure: 202926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 203026932566SPatrick McHardy return -EMSGSIZE; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 2033d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2034d961db35SThomas Graf { 2035d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2036d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2037d961db35SThomas Graf } 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 20401da177e4SLinus Torvalds struct netlink_callback *cb) 20411da177e4SLinus Torvalds { 2042426b5303SEric W. Biederman struct net * net = skb->sk->sk_net; 20431da177e4SLinus Torvalds struct neighbour *n; 20441da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 20451da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 20461da177e4SLinus Torvalds 2047c5e29460SJulian Anastasov read_lock_bh(&tbl->lock); 20481da177e4SLinus Torvalds for (h = 0; h <= tbl->hash_mask; h++) { 20491da177e4SLinus Torvalds if (h < s_h) 20501da177e4SLinus Torvalds continue; 20511da177e4SLinus Torvalds if (h > s_h) 20521da177e4SLinus Torvalds s_idx = 0; 2053426b5303SEric W. Biederman for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next) { 2054426b5303SEric W. Biederman int lidx; 2055426b5303SEric W. Biederman if (n->dev->nd_net != net) 2056426b5303SEric W. Biederman continue; 2057426b5303SEric W. Biederman lidx = idx++; 2058426b5303SEric W. Biederman if (lidx < s_idx) 20591da177e4SLinus Torvalds continue; 20601da177e4SLinus Torvalds if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid, 20611da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2062b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 2063b6544c0bSJamal Hadi Salim NLM_F_MULTI) <= 0) { 20641da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 20651da177e4SLinus Torvalds rc = -1; 20661da177e4SLinus Torvalds goto out; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds } 2070c5e29460SJulian Anastasov read_unlock_bh(&tbl->lock); 20711da177e4SLinus Torvalds rc = skb->len; 20721da177e4SLinus Torvalds out: 20731da177e4SLinus Torvalds cb->args[1] = h; 20741da177e4SLinus Torvalds cb->args[2] = idx; 20751da177e4SLinus Torvalds return rc; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 2078c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 20791da177e4SLinus Torvalds { 20801da177e4SLinus Torvalds struct neigh_table *tbl; 20811da177e4SLinus Torvalds int t, family, s_t; 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds read_lock(&neigh_tbl_lock); 20848b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 20851da177e4SLinus Torvalds s_t = cb->args[0]; 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) { 20881da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 20891da177e4SLinus Torvalds continue; 20901da177e4SLinus Torvalds if (t > s_t) 20911da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 20921da177e4SLinus Torvalds sizeof(cb->args[0])); 20931da177e4SLinus Torvalds if (neigh_dump_table(tbl, skb, cb) < 0) 20941da177e4SLinus Torvalds break; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds read_unlock(&neigh_tbl_lock); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds cb->args[0] = t; 20991da177e4SLinus Torvalds return skb->len; 21001da177e4SLinus Torvalds } 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 21031da177e4SLinus Torvalds { 21041da177e4SLinus Torvalds int chain; 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 21071da177e4SLinus Torvalds for (chain = 0; chain <= tbl->hash_mask; chain++) { 21081da177e4SLinus Torvalds struct neighbour *n; 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds for (n = tbl->hash_buckets[chain]; n; n = n->next) 21111da177e4SLinus Torvalds cb(n, cookie); 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 21181da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 21191da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 21201da177e4SLinus Torvalds { 21211da177e4SLinus Torvalds int chain; 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds for (chain = 0; chain <= tbl->hash_mask; chain++) { 21241da177e4SLinus Torvalds struct neighbour *n, **np; 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds np = &tbl->hash_buckets[chain]; 21271da177e4SLinus Torvalds while ((n = *np) != NULL) { 21281da177e4SLinus Torvalds int release; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds write_lock(&n->lock); 21311da177e4SLinus Torvalds release = cb(n); 21321da177e4SLinus Torvalds if (release) { 21331da177e4SLinus Torvalds *np = n->next; 21341da177e4SLinus Torvalds n->dead = 1; 21351da177e4SLinus Torvalds } else 21361da177e4SLinus Torvalds np = &n->next; 21371da177e4SLinus Torvalds write_unlock(&n->lock); 21384f494554SThomas Graf if (release) 21394f494554SThomas Graf neigh_cleanup_and_release(n); 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds } 2142ecbb4169SAlexey Kuznetsov } 21431da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 21481da177e4SLinus Torvalds { 21491da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 215042508461SDenis V. Lunev struct net *net = state->p.net; 21511da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 21521da177e4SLinus Torvalds struct neighbour *n = NULL; 21531da177e4SLinus Torvalds int bucket = state->bucket; 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 21561da177e4SLinus Torvalds for (bucket = 0; bucket <= tbl->hash_mask; bucket++) { 21571da177e4SLinus Torvalds n = tbl->hash_buckets[bucket]; 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds while (n) { 2160426b5303SEric W. Biederman if (n->dev->nd_net != net) 2161426b5303SEric W. Biederman goto next; 21621da177e4SLinus Torvalds if (state->neigh_sub_iter) { 21631da177e4SLinus Torvalds loff_t fakep = 0; 21641da177e4SLinus Torvalds void *v; 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 21671da177e4SLinus Torvalds if (!v) 21681da177e4SLinus Torvalds goto next; 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 21711da177e4SLinus Torvalds break; 21721da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 21731da177e4SLinus Torvalds break; 21741da177e4SLinus Torvalds next: 21751da177e4SLinus Torvalds n = n->next; 21761da177e4SLinus Torvalds } 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (n) 21791da177e4SLinus Torvalds break; 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds state->bucket = bucket; 21821da177e4SLinus Torvalds 21831da177e4SLinus Torvalds return n; 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 21871da177e4SLinus Torvalds struct neighbour *n, 21881da177e4SLinus Torvalds loff_t *pos) 21891da177e4SLinus Torvalds { 21901da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 219142508461SDenis V. Lunev struct net *net = state->p.net; 21921da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds if (state->neigh_sub_iter) { 21951da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 21961da177e4SLinus Torvalds if (v) 21971da177e4SLinus Torvalds return n; 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds n = n->next; 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds while (1) { 22021da177e4SLinus Torvalds while (n) { 2203426b5303SEric W. Biederman if (n->dev->nd_net != net) 2204426b5303SEric W. Biederman goto next; 22051da177e4SLinus Torvalds if (state->neigh_sub_iter) { 22061da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 22071da177e4SLinus Torvalds if (v) 22081da177e4SLinus Torvalds return n; 22091da177e4SLinus Torvalds goto next; 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 22121da177e4SLinus Torvalds break; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 22151da177e4SLinus Torvalds break; 22161da177e4SLinus Torvalds next: 22171da177e4SLinus Torvalds n = n->next; 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds if (n) 22211da177e4SLinus Torvalds break; 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds if (++state->bucket > tbl->hash_mask) 22241da177e4SLinus Torvalds break; 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds n = tbl->hash_buckets[state->bucket]; 22271da177e4SLinus Torvalds } 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds if (n && pos) 22301da177e4SLinus Torvalds --(*pos); 22311da177e4SLinus Torvalds return n; 22321da177e4SLinus Torvalds } 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 22351da177e4SLinus Torvalds { 22361da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds if (n) { 22391da177e4SLinus Torvalds while (*pos) { 22401da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 22411da177e4SLinus Torvalds if (!n) 22421da177e4SLinus Torvalds break; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds return *pos ? NULL : n; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 225142508461SDenis V. Lunev struct net * net = state->p.net; 22521da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 22531da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 22541da177e4SLinus Torvalds int bucket = state->bucket; 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 22571da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 22581da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2259426b5303SEric W. Biederman while (pn && (pn->net != net)) 2260426b5303SEric W. Biederman pn = pn->next; 22611da177e4SLinus Torvalds if (pn) 22621da177e4SLinus Torvalds break; 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds state->bucket = bucket; 22651da177e4SLinus Torvalds 22661da177e4SLinus Torvalds return pn; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 22701da177e4SLinus Torvalds struct pneigh_entry *pn, 22711da177e4SLinus Torvalds loff_t *pos) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 227442508461SDenis V. Lunev struct net * net = state->p.net; 22751da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds pn = pn->next; 22781da177e4SLinus Torvalds while (!pn) { 22791da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 22801da177e4SLinus Torvalds break; 22811da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2282426b5303SEric W. Biederman while (pn && (pn->net != net)) 2283426b5303SEric W. Biederman pn = pn->next; 22841da177e4SLinus Torvalds if (pn) 22851da177e4SLinus Torvalds break; 22861da177e4SLinus Torvalds } 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds if (pn && pos) 22891da177e4SLinus Torvalds --(*pos); 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds return pn; 22921da177e4SLinus Torvalds } 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 22951da177e4SLinus Torvalds { 22961da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds if (pn) { 22991da177e4SLinus Torvalds while (*pos) { 23001da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 23011da177e4SLinus Torvalds if (!pn) 23021da177e4SLinus Torvalds break; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds return *pos ? NULL : pn; 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 23091da177e4SLinus Torvalds { 23101da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23111da177e4SLinus Torvalds void *rc; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds rc = neigh_get_idx(seq, pos); 23141da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 23151da177e4SLinus Torvalds rc = pneigh_get_idx(seq, pos); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds return rc; 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 23219a429c49SEric Dumazet __acquires(tbl->lock) 23221da177e4SLinus Torvalds { 23231da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23241da177e4SLinus Torvalds loff_t pos_minus_one; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds state->tbl = tbl; 23271da177e4SLinus Torvalds state->bucket = 0; 23281da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds pos_minus_one = *pos - 1; 23331da177e4SLinus Torvalds return *pos ? neigh_get_idx_any(seq, &pos_minus_one) : SEQ_START_TOKEN; 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 23381da177e4SLinus Torvalds { 23391da177e4SLinus Torvalds struct neigh_seq_state *state; 23401da177e4SLinus Torvalds void *rc; 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 23431da177e4SLinus Torvalds rc = neigh_get_idx(seq, pos); 23441da177e4SLinus Torvalds goto out; 23451da177e4SLinus Torvalds } 23461da177e4SLinus Torvalds 23471da177e4SLinus Torvalds state = seq->private; 23481da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 23491da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 23501da177e4SLinus Torvalds if (rc) 23511da177e4SLinus Torvalds goto out; 23521da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 23531da177e4SLinus Torvalds rc = pneigh_get_first(seq); 23541da177e4SLinus Torvalds } else { 23551da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 23561da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds out: 23591da177e4SLinus Torvalds ++(*pos); 23601da177e4SLinus Torvalds return rc; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 23659a429c49SEric Dumazet __releases(tbl->lock) 23661da177e4SLinus Torvalds { 23671da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 23681da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds read_unlock_bh(&tbl->lock); 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds /* statistics via seq_file */ 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 23771da177e4SLinus Torvalds { 23781da177e4SLinus Torvalds struct proc_dir_entry *pde = seq->private; 23791da177e4SLinus Torvalds struct neigh_table *tbl = pde->data; 23801da177e4SLinus Torvalds int cpu; 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds if (*pos == 0) 23831da177e4SLinus Torvalds return SEQ_START_TOKEN; 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) { 23861da177e4SLinus Torvalds if (!cpu_possible(cpu)) 23871da177e4SLinus Torvalds continue; 23881da177e4SLinus Torvalds *pos = cpu+1; 23891da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds return NULL; 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 23941da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 23951da177e4SLinus Torvalds { 23961da177e4SLinus Torvalds struct proc_dir_entry *pde = seq->private; 23971da177e4SLinus Torvalds struct neigh_table *tbl = pde->data; 23981da177e4SLinus Torvalds int cpu; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds for (cpu = *pos; cpu < NR_CPUS; ++cpu) { 24011da177e4SLinus Torvalds if (!cpu_possible(cpu)) 24021da177e4SLinus Torvalds continue; 24031da177e4SLinus Torvalds *pos = cpu+1; 24041da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 24051da177e4SLinus Torvalds } 24061da177e4SLinus Torvalds return NULL; 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 24101da177e4SLinus Torvalds { 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 24151da177e4SLinus Torvalds { 24161da177e4SLinus Torvalds struct proc_dir_entry *pde = seq->private; 24171da177e4SLinus Torvalds struct neigh_table *tbl = pde->data; 24181da177e4SLinus Torvalds struct neigh_statistics *st = v; 24191da177e4SLinus Torvalds 24201da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 24215bec0039SOlaf Rempel seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs\n"); 24221da177e4SLinus Torvalds return 0; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 24261da177e4SLinus Torvalds "%08lx %08lx %08lx %08lx\n", 24271da177e4SLinus Torvalds atomic_read(&tbl->entries), 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds st->allocs, 24301da177e4SLinus Torvalds st->destroys, 24311da177e4SLinus Torvalds st->hash_grows, 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds st->lookups, 24341da177e4SLinus Torvalds st->hits, 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds st->res_failed, 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds st->rcv_probes_mcast, 24391da177e4SLinus Torvalds st->rcv_probes_ucast, 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds st->periodic_gc_runs, 24421da177e4SLinus Torvalds st->forced_gc_runs 24431da177e4SLinus Torvalds ); 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds return 0; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 2448f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 24491da177e4SLinus Torvalds .start = neigh_stat_seq_start, 24501da177e4SLinus Torvalds .next = neigh_stat_seq_next, 24511da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 24521da177e4SLinus Torvalds .show = neigh_stat_seq_show, 24531da177e4SLinus Torvalds }; 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 24561da177e4SLinus Torvalds { 24571da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds if (!ret) { 24601da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 24611da177e4SLinus Torvalds sf->private = PDE(inode); 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds return ret; 24641da177e4SLinus Torvalds }; 24651da177e4SLinus Torvalds 24669a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 24671da177e4SLinus Torvalds .owner = THIS_MODULE, 24681da177e4SLinus Torvalds .open = neigh_stat_seq_open, 24691da177e4SLinus Torvalds .read = seq_read, 24701da177e4SLinus Torvalds .llseek = seq_lseek, 24711da177e4SLinus Torvalds .release = seq_release, 24721da177e4SLinus Torvalds }; 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 24751da177e4SLinus Torvalds 2476339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2477339bf98fSThomas Graf { 2478339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2479339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2480339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2481339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2482339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2483339bf98fSThomas Graf } 2484339bf98fSThomas Graf 2485b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 24861da177e4SLinus Torvalds { 2487426b5303SEric W. Biederman struct net *net = n->dev->nd_net; 24888b8aec50SThomas Graf struct sk_buff *skb; 2489b8673311SThomas Graf int err = -ENOBUFS; 24901da177e4SLinus Torvalds 2491339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 24928b8aec50SThomas Graf if (skb == NULL) 2493b8673311SThomas Graf goto errout; 24941da177e4SLinus Torvalds 2495b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 249626932566SPatrick McHardy if (err < 0) { 249726932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 249826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 249926932566SPatrick McHardy kfree_skb(skb); 250026932566SPatrick McHardy goto errout; 250126932566SPatrick McHardy } 2502426b5303SEric W. Biederman err = rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 2503b8673311SThomas Graf errout: 2504b8673311SThomas Graf if (err < 0) 2505426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2506b8673311SThomas Graf } 2507b8673311SThomas Graf 2508d961db35SThomas Graf #ifdef CONFIG_ARPD 2509b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2510b8673311SThomas Graf { 2511b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 25128b8aec50SThomas Graf } 25131da177e4SLinus Torvalds #endif /* CONFIG_ARPD */ 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds static struct neigh_sysctl_table { 25181da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 2519c3bac5a7SPavel Emelyanov struct ctl_table neigh_vars[__NET_NEIGH_MAX]; 2520c3bac5a7SPavel Emelyanov char *dev_name; 2521ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 25221da177e4SLinus Torvalds .neigh_vars = { 25231da177e4SLinus Torvalds { 25241da177e4SLinus Torvalds .ctl_name = NET_NEIGH_MCAST_SOLICIT, 25251da177e4SLinus Torvalds .procname = "mcast_solicit", 25261da177e4SLinus Torvalds .maxlen = sizeof(int), 25271da177e4SLinus Torvalds .mode = 0644, 25281da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 25291da177e4SLinus Torvalds }, 25301da177e4SLinus Torvalds { 25311da177e4SLinus Torvalds .ctl_name = NET_NEIGH_UCAST_SOLICIT, 25321da177e4SLinus Torvalds .procname = "ucast_solicit", 25331da177e4SLinus Torvalds .maxlen = sizeof(int), 25341da177e4SLinus Torvalds .mode = 0644, 25351da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 25361da177e4SLinus Torvalds }, 25371da177e4SLinus Torvalds { 25381da177e4SLinus Torvalds .ctl_name = NET_NEIGH_APP_SOLICIT, 25391da177e4SLinus Torvalds .procname = "app_solicit", 25401da177e4SLinus Torvalds .maxlen = sizeof(int), 25411da177e4SLinus Torvalds .mode = 0644, 25421da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 25431da177e4SLinus Torvalds }, 25441da177e4SLinus Torvalds { 25451da177e4SLinus Torvalds .procname = "retrans_time", 25461da177e4SLinus Torvalds .maxlen = sizeof(int), 25471da177e4SLinus Torvalds .mode = 0644, 25481da177e4SLinus Torvalds .proc_handler = &proc_dointvec_userhz_jiffies, 25491da177e4SLinus Torvalds }, 25501da177e4SLinus Torvalds { 25511da177e4SLinus Torvalds .ctl_name = NET_NEIGH_REACHABLE_TIME, 25521da177e4SLinus Torvalds .procname = "base_reachable_time", 25531da177e4SLinus Torvalds .maxlen = sizeof(int), 25541da177e4SLinus Torvalds .mode = 0644, 25551da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 25561da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 25571da177e4SLinus Torvalds }, 25581da177e4SLinus Torvalds { 25591da177e4SLinus Torvalds .ctl_name = NET_NEIGH_DELAY_PROBE_TIME, 25601da177e4SLinus Torvalds .procname = "delay_first_probe_time", 25611da177e4SLinus Torvalds .maxlen = sizeof(int), 25621da177e4SLinus Torvalds .mode = 0644, 25631da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 25641da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 25651da177e4SLinus Torvalds }, 25661da177e4SLinus Torvalds { 25671da177e4SLinus Torvalds .ctl_name = NET_NEIGH_GC_STALE_TIME, 25681da177e4SLinus Torvalds .procname = "gc_stale_time", 25691da177e4SLinus Torvalds .maxlen = sizeof(int), 25701da177e4SLinus Torvalds .mode = 0644, 25711da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 25721da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 25731da177e4SLinus Torvalds }, 25741da177e4SLinus Torvalds { 25751da177e4SLinus Torvalds .ctl_name = NET_NEIGH_UNRES_QLEN, 25761da177e4SLinus Torvalds .procname = "unres_qlen", 25771da177e4SLinus Torvalds .maxlen = sizeof(int), 25781da177e4SLinus Torvalds .mode = 0644, 25791da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 25801da177e4SLinus Torvalds }, 25811da177e4SLinus Torvalds { 25821da177e4SLinus Torvalds .ctl_name = NET_NEIGH_PROXY_QLEN, 25831da177e4SLinus Torvalds .procname = "proxy_qlen", 25841da177e4SLinus Torvalds .maxlen = sizeof(int), 25851da177e4SLinus Torvalds .mode = 0644, 25861da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 25871da177e4SLinus Torvalds }, 25881da177e4SLinus Torvalds { 25891da177e4SLinus Torvalds .procname = "anycast_delay", 25901da177e4SLinus Torvalds .maxlen = sizeof(int), 25911da177e4SLinus Torvalds .mode = 0644, 25921da177e4SLinus Torvalds .proc_handler = &proc_dointvec_userhz_jiffies, 25931da177e4SLinus Torvalds }, 25941da177e4SLinus Torvalds { 25951da177e4SLinus Torvalds .procname = "proxy_delay", 25961da177e4SLinus Torvalds .maxlen = sizeof(int), 25971da177e4SLinus Torvalds .mode = 0644, 25981da177e4SLinus Torvalds .proc_handler = &proc_dointvec_userhz_jiffies, 25991da177e4SLinus Torvalds }, 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds .procname = "locktime", 26021da177e4SLinus Torvalds .maxlen = sizeof(int), 26031da177e4SLinus Torvalds .mode = 0644, 26041da177e4SLinus Torvalds .proc_handler = &proc_dointvec_userhz_jiffies, 26051da177e4SLinus Torvalds }, 26061da177e4SLinus Torvalds { 2607d12af679SEric W. Biederman .ctl_name = NET_NEIGH_RETRANS_TIME_MS, 2608d12af679SEric W. Biederman .procname = "retrans_time_ms", 2609d12af679SEric W. Biederman .maxlen = sizeof(int), 2610d12af679SEric W. Biederman .mode = 0644, 2611d12af679SEric W. Biederman .proc_handler = &proc_dointvec_ms_jiffies, 2612d12af679SEric W. Biederman .strategy = &sysctl_ms_jiffies, 2613d12af679SEric W. Biederman }, 2614d12af679SEric W. Biederman { 2615d12af679SEric W. Biederman .ctl_name = NET_NEIGH_REACHABLE_TIME_MS, 2616d12af679SEric W. Biederman .procname = "base_reachable_time_ms", 2617d12af679SEric W. Biederman .maxlen = sizeof(int), 2618d12af679SEric W. Biederman .mode = 0644, 2619d12af679SEric W. Biederman .proc_handler = &proc_dointvec_ms_jiffies, 2620d12af679SEric W. Biederman .strategy = &sysctl_ms_jiffies, 2621d12af679SEric W. Biederman }, 2622d12af679SEric W. Biederman { 26231da177e4SLinus Torvalds .ctl_name = NET_NEIGH_GC_INTERVAL, 26241da177e4SLinus Torvalds .procname = "gc_interval", 26251da177e4SLinus Torvalds .maxlen = sizeof(int), 26261da177e4SLinus Torvalds .mode = 0644, 26271da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 26281da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 26291da177e4SLinus Torvalds }, 26301da177e4SLinus Torvalds { 26311da177e4SLinus Torvalds .ctl_name = NET_NEIGH_GC_THRESH1, 26321da177e4SLinus Torvalds .procname = "gc_thresh1", 26331da177e4SLinus Torvalds .maxlen = sizeof(int), 26341da177e4SLinus Torvalds .mode = 0644, 26351da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 26361da177e4SLinus Torvalds }, 26371da177e4SLinus Torvalds { 26381da177e4SLinus Torvalds .ctl_name = NET_NEIGH_GC_THRESH2, 26391da177e4SLinus Torvalds .procname = "gc_thresh2", 26401da177e4SLinus Torvalds .maxlen = sizeof(int), 26411da177e4SLinus Torvalds .mode = 0644, 26421da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 26431da177e4SLinus Torvalds }, 26441da177e4SLinus Torvalds { 26451da177e4SLinus Torvalds .ctl_name = NET_NEIGH_GC_THRESH3, 26461da177e4SLinus Torvalds .procname = "gc_thresh3", 26471da177e4SLinus Torvalds .maxlen = sizeof(int), 26481da177e4SLinus Torvalds .mode = 0644, 26491da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 26501da177e4SLinus Torvalds }, 2651c3bac5a7SPavel Emelyanov {}, 26521da177e4SLinus Torvalds }, 26531da177e4SLinus Torvalds }; 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 26561da177e4SLinus Torvalds int p_id, int pdev_id, char *p_name, 26571da177e4SLinus Torvalds proc_handler *handler, ctl_handler *strategy) 26581da177e4SLinus Torvalds { 26593c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 26601da177e4SLinus Torvalds const char *dev_name_source = NULL; 2661c3bac5a7SPavel Emelyanov 2662c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_ROOT 0 2663c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_PROTO 1 2664c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_NEIGH 2 2665c3bac5a7SPavel Emelyanov #define NEIGH_CTL_PATH_DEV 3 2666c3bac5a7SPavel Emelyanov 2667c3bac5a7SPavel Emelyanov struct ctl_path neigh_path[] = { 2668c3bac5a7SPavel Emelyanov { .procname = "net", .ctl_name = CTL_NET, }, 2669c3bac5a7SPavel Emelyanov { .procname = "proto", .ctl_name = 0, }, 2670c3bac5a7SPavel Emelyanov { .procname = "neigh", .ctl_name = 0, }, 2671c3bac5a7SPavel Emelyanov { .procname = "default", .ctl_name = NET_PROTO_CONF_DEFAULT, }, 2672c3bac5a7SPavel Emelyanov { }, 2673c3bac5a7SPavel Emelyanov }; 26741da177e4SLinus Torvalds 26753c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 26761da177e4SLinus Torvalds if (!t) 26773c607bbbSPavel Emelyanov goto err; 26783c607bbbSPavel Emelyanov 26791da177e4SLinus Torvalds t->neigh_vars[0].data = &p->mcast_probes; 26801da177e4SLinus Torvalds t->neigh_vars[1].data = &p->ucast_probes; 26811da177e4SLinus Torvalds t->neigh_vars[2].data = &p->app_probes; 26821da177e4SLinus Torvalds t->neigh_vars[3].data = &p->retrans_time; 26831da177e4SLinus Torvalds t->neigh_vars[4].data = &p->base_reachable_time; 26841da177e4SLinus Torvalds t->neigh_vars[5].data = &p->delay_probe_time; 26851da177e4SLinus Torvalds t->neigh_vars[6].data = &p->gc_staletime; 26861da177e4SLinus Torvalds t->neigh_vars[7].data = &p->queue_len; 26871da177e4SLinus Torvalds t->neigh_vars[8].data = &p->proxy_qlen; 26881da177e4SLinus Torvalds t->neigh_vars[9].data = &p->anycast_delay; 26891da177e4SLinus Torvalds t->neigh_vars[10].data = &p->proxy_delay; 26901da177e4SLinus Torvalds t->neigh_vars[11].data = &p->locktime; 2691d12af679SEric W. Biederman t->neigh_vars[12].data = &p->retrans_time; 2692d12af679SEric W. Biederman t->neigh_vars[13].data = &p->base_reachable_time; 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds if (dev) { 26951da177e4SLinus Torvalds dev_name_source = dev->name; 2696c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_DEV].ctl_name = dev->ifindex; 2697d12af679SEric W. Biederman /* Terminate the table early */ 2698d12af679SEric W. Biederman memset(&t->neigh_vars[14], 0, sizeof(t->neigh_vars[14])); 26991da177e4SLinus Torvalds } else { 2700c3bac5a7SPavel Emelyanov dev_name_source = neigh_path[NEIGH_CTL_PATH_DEV].procname; 2701d12af679SEric W. Biederman t->neigh_vars[14].data = (int *)(p + 1); 2702d12af679SEric W. Biederman t->neigh_vars[15].data = (int *)(p + 1) + 1; 2703d12af679SEric W. Biederman t->neigh_vars[16].data = (int *)(p + 1) + 2; 2704d12af679SEric W. Biederman t->neigh_vars[17].data = (int *)(p + 1) + 3; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds if (handler || strategy) { 27091da177e4SLinus Torvalds /* RetransTime */ 27101da177e4SLinus Torvalds t->neigh_vars[3].proc_handler = handler; 27111da177e4SLinus Torvalds t->neigh_vars[3].strategy = strategy; 27121da177e4SLinus Torvalds t->neigh_vars[3].extra1 = dev; 2713d12af679SEric W. Biederman if (!strategy) 2714d12af679SEric W. Biederman t->neigh_vars[3].ctl_name = CTL_UNNUMBERED; 27151da177e4SLinus Torvalds /* ReachableTime */ 27161da177e4SLinus Torvalds t->neigh_vars[4].proc_handler = handler; 27171da177e4SLinus Torvalds t->neigh_vars[4].strategy = strategy; 27181da177e4SLinus Torvalds t->neigh_vars[4].extra1 = dev; 2719d12af679SEric W. Biederman if (!strategy) 2720d12af679SEric W. Biederman t->neigh_vars[4].ctl_name = CTL_UNNUMBERED; 27211da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 2722d12af679SEric W. Biederman t->neigh_vars[12].proc_handler = handler; 2723d12af679SEric W. Biederman t->neigh_vars[12].strategy = strategy; 2724d12af679SEric W. Biederman t->neigh_vars[12].extra1 = dev; 2725d12af679SEric W. Biederman if (!strategy) 2726d12af679SEric W. Biederman t->neigh_vars[12].ctl_name = CTL_UNNUMBERED; 27271da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 2728d12af679SEric W. Biederman t->neigh_vars[13].proc_handler = handler; 2729d12af679SEric W. Biederman t->neigh_vars[13].strategy = strategy; 2730d12af679SEric W. Biederman t->neigh_vars[13].extra1 = dev; 2731d12af679SEric W. Biederman if (!strategy) 2732d12af679SEric W. Biederman t->neigh_vars[13].ctl_name = CTL_UNNUMBERED; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 2735c3bac5a7SPavel Emelyanov t->dev_name = kstrdup(dev_name_source, GFP_KERNEL); 2736c3bac5a7SPavel Emelyanov if (!t->dev_name) 27371da177e4SLinus Torvalds goto free; 27381da177e4SLinus Torvalds 2739c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_DEV].procname = t->dev_name; 2740c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_NEIGH].ctl_name = pdev_id; 2741c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_PROTO].procname = p_name; 2742c3bac5a7SPavel Emelyanov neigh_path[NEIGH_CTL_PATH_PROTO].ctl_name = p_id; 27431da177e4SLinus Torvalds 27444ab438fcSDenis V. Lunev t->sysctl_header = 27454ab438fcSDenis V. Lunev register_net_sysctl_table(p->net, neigh_path, t->neigh_vars); 27463c607bbbSPavel Emelyanov if (!t->sysctl_header) 27471da177e4SLinus Torvalds goto free_procname; 27483c607bbbSPavel Emelyanov 27491da177e4SLinus Torvalds p->sysctl_table = t; 27501da177e4SLinus Torvalds return 0; 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds free_procname: 2753c3bac5a7SPavel Emelyanov kfree(t->dev_name); 27541da177e4SLinus Torvalds free: 27551da177e4SLinus Torvalds kfree(t); 27563c607bbbSPavel Emelyanov err: 27573c607bbbSPavel Emelyanov return -ENOBUFS; 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds 27601da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 27611da177e4SLinus Torvalds { 27621da177e4SLinus Torvalds if (p->sysctl_table) { 27631da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 27641da177e4SLinus Torvalds p->sysctl_table = NULL; 27651da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 2766c3bac5a7SPavel Emelyanov kfree(t->dev_name); 27671da177e4SLinus Torvalds kfree(t); 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 27721da177e4SLinus Torvalds 2773c8822a4eSThomas Graf static int __init neigh_init(void) 2774c8822a4eSThomas Graf { 2775c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL); 2776c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL); 2777c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info); 2778c8822a4eSThomas Graf 2779c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info); 2780c8822a4eSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL); 2781c8822a4eSThomas Graf 2782c8822a4eSThomas Graf return 0; 2783c8822a4eSThomas Graf } 2784c8822a4eSThomas Graf 2785c8822a4eSThomas Graf subsys_initcall(neigh_init); 2786c8822a4eSThomas Graf 27871da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_event_send); 27881da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_changeaddr); 27891da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_compat_output); 27901da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_connected_output); 27911da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_create); 27921da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_destroy); 27931da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_event_ns); 27941da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_ifdown); 27951da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_lookup); 27961da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_lookup_nodev); 27971da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_parms_alloc); 27981da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_parms_release); 27991da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_rand_reach_time); 28001da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_resolve_output); 28011da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_table_clear); 28021da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_table_init); 2803bd89efc5SSimon Kelley EXPORT_SYMBOL(neigh_table_init_no_netlink); 28041da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_update); 28051da177e4SLinus Torvalds EXPORT_SYMBOL(pneigh_enqueue); 28061da177e4SLinus Torvalds EXPORT_SYMBOL(pneigh_lookup); 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds #ifdef CONFIG_ARPD 28091da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_app_ns); 28101da177e4SLinus Torvalds #endif 28111da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 28121da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_sysctl_register); 28131da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_sysctl_unregister); 28141da177e4SLinus Torvalds #endif 2815