11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Generic address resolution entity 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: 51da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 61da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Fixes: 141da177e4SLinus Torvalds * Vitaly E. Lavrov releasing NULL neighbor in neigh_add. 151da177e4SLinus Torvalds * Harald Welte Add neighbour cache statistics like rtstat 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 18e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19e005d193SJoe Perches 205a0e3ad6STejun Heo #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/kernel.h> 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/socket.h> 251da177e4SLinus Torvalds #include <linux/netdevice.h> 261da177e4SLinus Torvalds #include <linux/proc_fs.h> 271da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 281da177e4SLinus Torvalds #include <linux/sysctl.h> 291da177e4SLinus Torvalds #endif 301da177e4SLinus Torvalds #include <linux/times.h> 31457c4cbcSEric W. Biederman #include <net/net_namespace.h> 321da177e4SLinus Torvalds #include <net/neighbour.h> 331da177e4SLinus Torvalds #include <net/dst.h> 341da177e4SLinus Torvalds #include <net/sock.h> 358d71740cSTom Tucker #include <net/netevent.h> 36a14a49d2SThomas Graf #include <net/netlink.h> 371da177e4SLinus Torvalds #include <linux/rtnetlink.h> 381da177e4SLinus Torvalds #include <linux/random.h> 39543537bdSPaulo Marques #include <linux/string.h> 40c3609d51Svignesh babu #include <linux/log2.h> 411d4c8c29SJiri Pirko #include <linux/inetdevice.h> 42bba24896SJiri Pirko #include <net/addrconf.h> 431da177e4SLinus Torvalds 44d5d427cdSJoe Perches #define DEBUG 451da177e4SLinus Torvalds #define NEIGH_DEBUG 1 46d5d427cdSJoe Perches #define neigh_dbg(level, fmt, ...) \ 47d5d427cdSJoe Perches do { \ 48d5d427cdSJoe Perches if (level <= NEIGH_DEBUG) \ 49d5d427cdSJoe Perches pr_debug(fmt, ##__VA_ARGS__); \ 50d5d427cdSJoe Perches } while (0) 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #define PNEIGH_HASHMASK 0xF 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg); 55d961db35SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags); 56d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh); 571da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 581da177e4SLinus Torvalds 5945fc3b11SAmos Waterland #ifdef CONFIG_PROC_FS 609a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops; 6145fc3b11SAmos Waterland #endif 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* 641da177e4SLinus Torvalds Neighbour hash table buckets are protected with rwlock tbl->lock. 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds - All the scans/updates to hash buckets MUST be made under this lock. 671da177e4SLinus Torvalds - NOTHING clever should be made under this lock: no callbacks 681da177e4SLinus Torvalds to protocol backends, no attempts to send something to network. 691da177e4SLinus Torvalds It will result in deadlocks, if backend/driver wants to use neighbour 701da177e4SLinus Torvalds cache. 711da177e4SLinus Torvalds - If the entry requires some non-trivial actions, increase 721da177e4SLinus Torvalds its reference count and release table lock. 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds Neighbour entries are protected: 751da177e4SLinus Torvalds - with reference count. 761da177e4SLinus Torvalds - with rwlock neigh->lock 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds Reference count prevents destruction. 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds neigh->lock mainly serializes ll address data and its validity state. 811da177e4SLinus Torvalds However, the same lock is used to protect another entry fields: 821da177e4SLinus Torvalds - timer 831da177e4SLinus Torvalds - resolution queue 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds Again, nothing clever shall be made under neigh->lock, 861da177e4SLinus Torvalds the most complicated procedure, which we allow is dev->hard_header. 871da177e4SLinus Torvalds It is supposed, that dev->hard_header is simplistic and does 881da177e4SLinus Torvalds not make callbacks to neighbour tables. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 918f40b161SDavid S. Miller static int neigh_blackhole(struct neighbour *neigh, struct sk_buff *skb) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds kfree_skb(skb); 941da177e4SLinus Torvalds return -ENETDOWN; 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 974f494554SThomas Graf static void neigh_cleanup_and_release(struct neighbour *neigh) 984f494554SThomas Graf { 994f494554SThomas Graf if (neigh->parms->neigh_cleanup) 1004f494554SThomas Graf neigh->parms->neigh_cleanup(neigh); 1014f494554SThomas Graf 102d961db35SThomas Graf __neigh_notify(neigh, RTM_DELNEIGH, 0); 1034f494554SThomas Graf neigh_release(neigh); 1044f494554SThomas Graf } 1054f494554SThomas Graf 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * It is random distribution in the interval (1/2)*base...(3/2)*base. 1081da177e4SLinus Torvalds * It corresponds to default IPv6 settings and is not overridable, 1091da177e4SLinus Torvalds * because it is really reasonable choice. 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds unsigned long neigh_rand_reach_time(unsigned long base) 1131da177e4SLinus Torvalds { 11463862b5bSAruna-Hewapathirane return base ? (prandom_u32() % base) + (base >> 1) : 0; 1151da177e4SLinus Torvalds } 1160a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_rand_reach_time); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds static int neigh_forced_gc(struct neigh_table *tbl) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds int shrunk = 0; 1221da177e4SLinus Torvalds int i; 123d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 128d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 129d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 130cd089336SDavid S. Miller for (i = 0; i < (1 << nht->hash_shift); i++) { 131767e97e1SEric Dumazet struct neighbour *n; 132767e97e1SEric Dumazet struct neighbour __rcu **np; 1331da177e4SLinus Torvalds 134d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 135767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 136767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 1371da177e4SLinus Torvalds /* Neighbour record may be discarded if: 1381da177e4SLinus Torvalds * - nobody refers to it. 1391da177e4SLinus Torvalds * - it is not permanent 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds write_lock(&n->lock); 1421da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 1431da177e4SLinus Torvalds !(n->nud_state & NUD_PERMANENT)) { 144767e97e1SEric Dumazet rcu_assign_pointer(*np, 145767e97e1SEric Dumazet rcu_dereference_protected(n->next, 146767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 1471da177e4SLinus Torvalds n->dead = 1; 1481da177e4SLinus Torvalds shrunk = 1; 1491da177e4SLinus Torvalds write_unlock(&n->lock); 1504f494554SThomas Graf neigh_cleanup_and_release(n); 1511da177e4SLinus Torvalds continue; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds write_unlock(&n->lock); 1541da177e4SLinus Torvalds np = &n->next; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds tbl->last_flush = jiffies; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds return shrunk; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 165a43d8994SPavel Emelyanov static void neigh_add_timer(struct neighbour *n, unsigned long when) 166a43d8994SPavel Emelyanov { 167a43d8994SPavel Emelyanov neigh_hold(n); 168a43d8994SPavel Emelyanov if (unlikely(mod_timer(&n->timer, when))) { 169a43d8994SPavel Emelyanov printk("NEIGH: BUG, double timer add, state is %x\n", 170a43d8994SPavel Emelyanov n->nud_state); 171a43d8994SPavel Emelyanov dump_stack(); 172a43d8994SPavel Emelyanov } 173a43d8994SPavel Emelyanov } 174a43d8994SPavel Emelyanov 1751da177e4SLinus Torvalds static int neigh_del_timer(struct neighbour *n) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds if ((n->nud_state & NUD_IN_TIMER) && 1781da177e4SLinus Torvalds del_timer(&n->timer)) { 1791da177e4SLinus Torvalds neigh_release(n); 1801da177e4SLinus Torvalds return 1; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds return 0; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds static void pneigh_queue_purge(struct sk_buff_head *list) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds struct sk_buff *skb; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) { 1901da177e4SLinus Torvalds dev_put(skb->dev); 1911da177e4SLinus Torvalds kfree_skb(skb); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 19549636bb1SHerbert Xu static void neigh_flush_dev(struct neigh_table *tbl, struct net_device *dev) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds int i; 198d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1991da177e4SLinus Torvalds 200d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 201d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 202d6bf7817SEric Dumazet 203cd089336SDavid S. Miller for (i = 0; i < (1 << nht->hash_shift); i++) { 204767e97e1SEric Dumazet struct neighbour *n; 205767e97e1SEric Dumazet struct neighbour __rcu **np = &nht->hash_buckets[i]; 2061da177e4SLinus Torvalds 207767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 208767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 2091da177e4SLinus Torvalds if (dev && n->dev != dev) { 2101da177e4SLinus Torvalds np = &n->next; 2111da177e4SLinus Torvalds continue; 2121da177e4SLinus Torvalds } 213767e97e1SEric Dumazet rcu_assign_pointer(*np, 214767e97e1SEric Dumazet rcu_dereference_protected(n->next, 215767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 2161da177e4SLinus Torvalds write_lock(&n->lock); 2171da177e4SLinus Torvalds neigh_del_timer(n); 2181da177e4SLinus Torvalds n->dead = 1; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds if (atomic_read(&n->refcnt) != 1) { 2211da177e4SLinus Torvalds /* The most unpleasant situation. 2221da177e4SLinus Torvalds We must destroy neighbour entry, 2231da177e4SLinus Torvalds but someone still uses it. 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds The destroy will be delayed until 2261da177e4SLinus Torvalds the last user releases us, but 2271da177e4SLinus Torvalds we must kill timers etc. and move 2281da177e4SLinus Torvalds it to safe state. 2291da177e4SLinus Torvalds */ 230c9ab4d85SEric Dumazet __skb_queue_purge(&n->arp_queue); 2318b5c171bSEric Dumazet n->arp_queue_len_bytes = 0; 2321da177e4SLinus Torvalds n->output = neigh_blackhole; 2331da177e4SLinus Torvalds if (n->nud_state & NUD_VALID) 2341da177e4SLinus Torvalds n->nud_state = NUD_NOARP; 2351da177e4SLinus Torvalds else 2361da177e4SLinus Torvalds n->nud_state = NUD_NONE; 237d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is stray\n", n); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds write_unlock(&n->lock); 2404f494554SThomas Graf neigh_cleanup_and_release(n); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds } 24349636bb1SHerbert Xu } 2441da177e4SLinus Torvalds 24549636bb1SHerbert Xu void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev) 24649636bb1SHerbert Xu { 24749636bb1SHerbert Xu write_lock_bh(&tbl->lock); 24849636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 24949636bb1SHerbert Xu write_unlock_bh(&tbl->lock); 25049636bb1SHerbert Xu } 2510a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_changeaddr); 25249636bb1SHerbert Xu 25349636bb1SHerbert Xu int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 25449636bb1SHerbert Xu { 25549636bb1SHerbert Xu write_lock_bh(&tbl->lock); 25649636bb1SHerbert Xu neigh_flush_dev(tbl, dev); 2571da177e4SLinus Torvalds pneigh_ifdown(tbl, dev); 2581da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 2611da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds } 2640a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_ifdown); 2651da177e4SLinus Torvalds 266596b9b68SDavid Miller static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device *dev) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds struct neighbour *n = NULL; 2691da177e4SLinus Torvalds unsigned long now = jiffies; 2701da177e4SLinus Torvalds int entries; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds entries = atomic_inc_return(&tbl->entries) - 1; 2731da177e4SLinus Torvalds if (entries >= tbl->gc_thresh3 || 2741da177e4SLinus Torvalds (entries >= tbl->gc_thresh2 && 2751da177e4SLinus Torvalds time_after(now, tbl->last_flush + 5 * HZ))) { 2761da177e4SLinus Torvalds if (!neigh_forced_gc(tbl) && 2771da177e4SLinus Torvalds entries >= tbl->gc_thresh3) 2781da177e4SLinus Torvalds goto out_entries; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 28108433effSYOSHIFUJI Hideaki / 吉藤英明 n = kzalloc(tbl->entry_size + dev->neigh_priv_len, GFP_ATOMIC); 2821da177e4SLinus Torvalds if (!n) 2831da177e4SLinus Torvalds goto out_entries; 2841da177e4SLinus Torvalds 285c9ab4d85SEric Dumazet __skb_queue_head_init(&n->arp_queue); 2861da177e4SLinus Torvalds rwlock_init(&n->lock); 2870ed8ddf4SEric Dumazet seqlock_init(&n->ha_lock); 2881da177e4SLinus Torvalds n->updated = n->used = now; 2891da177e4SLinus Torvalds n->nud_state = NUD_NONE; 2901da177e4SLinus Torvalds n->output = neigh_blackhole; 291f6b72b62SDavid S. Miller seqlock_init(&n->hh.hh_lock); 2921da177e4SLinus Torvalds n->parms = neigh_parms_clone(&tbl->parms); 293b24b8a24SPavel Emelyanov setup_timer(&n->timer, neigh_timer_handler, (unsigned long)n); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, allocs); 2961da177e4SLinus Torvalds n->tbl = tbl; 2971da177e4SLinus Torvalds atomic_set(&n->refcnt, 1); 2981da177e4SLinus Torvalds n->dead = 1; 2991da177e4SLinus Torvalds out: 3001da177e4SLinus Torvalds return n; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds out_entries: 3031da177e4SLinus Torvalds atomic_dec(&tbl->entries); 3041da177e4SLinus Torvalds goto out; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3072c2aba6cSDavid S. Miller static void neigh_get_hash_rnd(u32 *x) 3082c2aba6cSDavid S. Miller { 3092c2aba6cSDavid S. Miller get_random_bytes(x, sizeof(*x)); 3102c2aba6cSDavid S. Miller *x |= 1; 3112c2aba6cSDavid S. Miller } 3122c2aba6cSDavid S. Miller 313cd089336SDavid S. Miller static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift) 3141da177e4SLinus Torvalds { 315cd089336SDavid S. Miller size_t size = (1 << shift) * sizeof(struct neighbour *); 316d6bf7817SEric Dumazet struct neigh_hash_table *ret; 3176193d2beSEric Dumazet struct neighbour __rcu **buckets; 3182c2aba6cSDavid S. Miller int i; 3191da177e4SLinus Torvalds 320d6bf7817SEric Dumazet ret = kmalloc(sizeof(*ret), GFP_ATOMIC); 321d6bf7817SEric Dumazet if (!ret) 322d6bf7817SEric Dumazet return NULL; 323d6bf7817SEric Dumazet if (size <= PAGE_SIZE) 324d6bf7817SEric Dumazet buckets = kzalloc(size, GFP_ATOMIC); 325d6bf7817SEric Dumazet else 3266193d2beSEric Dumazet buckets = (struct neighbour __rcu **) 327d6bf7817SEric Dumazet __get_free_pages(GFP_ATOMIC | __GFP_ZERO, 328d6bf7817SEric Dumazet get_order(size)); 329d6bf7817SEric Dumazet if (!buckets) { 330d6bf7817SEric Dumazet kfree(ret); 331d6bf7817SEric Dumazet return NULL; 3321da177e4SLinus Torvalds } 3336193d2beSEric Dumazet ret->hash_buckets = buckets; 334cd089336SDavid S. Miller ret->hash_shift = shift; 3352c2aba6cSDavid S. Miller for (i = 0; i < NEIGH_NUM_HASH_RND; i++) 3362c2aba6cSDavid S. Miller neigh_get_hash_rnd(&ret->hash_rnd[i]); 3371da177e4SLinus Torvalds return ret; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 340d6bf7817SEric Dumazet static void neigh_hash_free_rcu(struct rcu_head *head) 3411da177e4SLinus Torvalds { 342d6bf7817SEric Dumazet struct neigh_hash_table *nht = container_of(head, 343d6bf7817SEric Dumazet struct neigh_hash_table, 344d6bf7817SEric Dumazet rcu); 345cd089336SDavid S. Miller size_t size = (1 << nht->hash_shift) * sizeof(struct neighbour *); 3466193d2beSEric Dumazet struct neighbour __rcu **buckets = nht->hash_buckets; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds if (size <= PAGE_SIZE) 349d6bf7817SEric Dumazet kfree(buckets); 3501da177e4SLinus Torvalds else 351d6bf7817SEric Dumazet free_pages((unsigned long)buckets, get_order(size)); 352d6bf7817SEric Dumazet kfree(nht); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 355d6bf7817SEric Dumazet static struct neigh_hash_table *neigh_hash_grow(struct neigh_table *tbl, 356cd089336SDavid S. Miller unsigned long new_shift) 3571da177e4SLinus Torvalds { 358d6bf7817SEric Dumazet unsigned int i, hash; 359d6bf7817SEric Dumazet struct neigh_hash_table *new_nht, *old_nht; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hash_grows); 3621da177e4SLinus Torvalds 363d6bf7817SEric Dumazet old_nht = rcu_dereference_protected(tbl->nht, 364d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 365cd089336SDavid S. Miller new_nht = neigh_hash_alloc(new_shift); 366d6bf7817SEric Dumazet if (!new_nht) 367d6bf7817SEric Dumazet return old_nht; 3681da177e4SLinus Torvalds 369cd089336SDavid S. Miller for (i = 0; i < (1 << old_nht->hash_shift); i++) { 3701da177e4SLinus Torvalds struct neighbour *n, *next; 3711da177e4SLinus Torvalds 372767e97e1SEric Dumazet for (n = rcu_dereference_protected(old_nht->hash_buckets[i], 373767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 374d6bf7817SEric Dumazet n != NULL; 375d6bf7817SEric Dumazet n = next) { 376d6bf7817SEric Dumazet hash = tbl->hash(n->primary_key, n->dev, 377d6bf7817SEric Dumazet new_nht->hash_rnd); 3781da177e4SLinus Torvalds 379cd089336SDavid S. Miller hash >>= (32 - new_nht->hash_shift); 380767e97e1SEric Dumazet next = rcu_dereference_protected(n->next, 381767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 3821da177e4SLinus Torvalds 383767e97e1SEric Dumazet rcu_assign_pointer(n->next, 384767e97e1SEric Dumazet rcu_dereference_protected( 385767e97e1SEric Dumazet new_nht->hash_buckets[hash], 386767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 387767e97e1SEric Dumazet rcu_assign_pointer(new_nht->hash_buckets[hash], n); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 391d6bf7817SEric Dumazet rcu_assign_pointer(tbl->nht, new_nht); 392d6bf7817SEric Dumazet call_rcu(&old_nht->rcu, neigh_hash_free_rcu); 393d6bf7817SEric Dumazet return new_nht; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 3971da177e4SLinus Torvalds struct net_device *dev) 3981da177e4SLinus Torvalds { 3991da177e4SLinus Torvalds struct neighbour *n; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4021da177e4SLinus Torvalds 403d6bf7817SEric Dumazet rcu_read_lock_bh(); 40460395a20SEric W. Biederman n = __neigh_lookup_noref(tbl, pkey, dev); 40560395a20SEric W. Biederman if (n) { 406767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 407767e97e1SEric Dumazet n = NULL; 4081da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4091da177e4SLinus Torvalds } 410767e97e1SEric Dumazet 411d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4121da177e4SLinus Torvalds return n; 4131da177e4SLinus Torvalds } 4140a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup); 4151da177e4SLinus Torvalds 416426b5303SEric W. Biederman struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net, 417426b5303SEric W. Biederman const void *pkey) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds struct neighbour *n; 4201da177e4SLinus Torvalds int key_len = tbl->key_len; 421bc4bf5f3SPavel Emelyanov u32 hash_val; 422d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, lookups); 4251da177e4SLinus Torvalds 426d6bf7817SEric Dumazet rcu_read_lock_bh(); 427d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 428cd089336SDavid S. Miller hash_val = tbl->hash(pkey, NULL, nht->hash_rnd) >> (32 - nht->hash_shift); 429767e97e1SEric Dumazet 430767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 431767e97e1SEric Dumazet n != NULL; 432767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 433426b5303SEric W. Biederman if (!memcmp(n->primary_key, pkey, key_len) && 434878628fbSYOSHIFUJI Hideaki net_eq(dev_net(n->dev), net)) { 435767e97e1SEric Dumazet if (!atomic_inc_not_zero(&n->refcnt)) 436767e97e1SEric Dumazet n = NULL; 4371da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, hits); 4381da177e4SLinus Torvalds break; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds } 441767e97e1SEric Dumazet 442d6bf7817SEric Dumazet rcu_read_unlock_bh(); 4431da177e4SLinus Torvalds return n; 4441da177e4SLinus Torvalds } 4450a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_lookup_nodev); 4461da177e4SLinus Torvalds 447a263b309SDavid S. Miller struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey, 448a263b309SDavid S. Miller struct net_device *dev, bool want_ref) 4491da177e4SLinus Torvalds { 4501da177e4SLinus Torvalds u32 hash_val; 4511da177e4SLinus Torvalds int key_len = tbl->key_len; 4521da177e4SLinus Torvalds int error; 453596b9b68SDavid Miller struct neighbour *n1, *rc, *n = neigh_alloc(tbl, dev); 454d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds if (!n) { 4571da177e4SLinus Torvalds rc = ERR_PTR(-ENOBUFS); 4581da177e4SLinus Torvalds goto out; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds memcpy(n->primary_key, pkey, key_len); 4621da177e4SLinus Torvalds n->dev = dev; 4631da177e4SLinus Torvalds dev_hold(dev); 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* Protocol specific setup. */ 4661da177e4SLinus Torvalds if (tbl->constructor && (error = tbl->constructor(n)) < 0) { 4671da177e4SLinus Torvalds rc = ERR_PTR(error); 4681da177e4SLinus Torvalds goto out_neigh_release; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 471da6a8fa0SDavid Miller if (dev->netdev_ops->ndo_neigh_construct) { 472da6a8fa0SDavid Miller error = dev->netdev_ops->ndo_neigh_construct(n); 473da6a8fa0SDavid Miller if (error < 0) { 474da6a8fa0SDavid Miller rc = ERR_PTR(error); 475da6a8fa0SDavid Miller goto out_neigh_release; 476da6a8fa0SDavid Miller } 477da6a8fa0SDavid Miller } 478da6a8fa0SDavid Miller 479447f2191SDavid S. Miller /* Device specific setup. */ 480447f2191SDavid S. Miller if (n->parms->neigh_setup && 481447f2191SDavid S. Miller (error = n->parms->neigh_setup(n)) < 0) { 482447f2191SDavid S. Miller rc = ERR_PTR(error); 483447f2191SDavid S. Miller goto out_neigh_release; 484447f2191SDavid S. Miller } 485447f2191SDavid S. Miller 4861f9248e5SJiri Pirko n->confirmed = jiffies - (NEIGH_VAR(n->parms, BASE_REACHABLE_TIME) << 1); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 489d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 490d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 4911da177e4SLinus Torvalds 492cd089336SDavid S. Miller if (atomic_read(&tbl->entries) > (1 << nht->hash_shift)) 493cd089336SDavid S. Miller nht = neigh_hash_grow(tbl, nht->hash_shift + 1); 4941da177e4SLinus Torvalds 495cd089336SDavid S. Miller hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds if (n->parms->dead) { 4981da177e4SLinus Torvalds rc = ERR_PTR(-EINVAL); 4991da177e4SLinus Torvalds goto out_tbl_unlock; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 502767e97e1SEric Dumazet for (n1 = rcu_dereference_protected(nht->hash_buckets[hash_val], 503767e97e1SEric Dumazet lockdep_is_held(&tbl->lock)); 504767e97e1SEric Dumazet n1 != NULL; 505767e97e1SEric Dumazet n1 = rcu_dereference_protected(n1->next, 506767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) { 5071da177e4SLinus Torvalds if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) { 508a263b309SDavid S. Miller if (want_ref) 5091da177e4SLinus Torvalds neigh_hold(n1); 5101da177e4SLinus Torvalds rc = n1; 5111da177e4SLinus Torvalds goto out_tbl_unlock; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds n->dead = 0; 516a263b309SDavid S. Miller if (want_ref) 5171da177e4SLinus Torvalds neigh_hold(n); 518767e97e1SEric Dumazet rcu_assign_pointer(n->next, 519767e97e1SEric Dumazet rcu_dereference_protected(nht->hash_buckets[hash_val], 520767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 521767e97e1SEric Dumazet rcu_assign_pointer(nht->hash_buckets[hash_val], n); 5221da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 523d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is created\n", n); 5241da177e4SLinus Torvalds rc = n; 5251da177e4SLinus Torvalds out: 5261da177e4SLinus Torvalds return rc; 5271da177e4SLinus Torvalds out_tbl_unlock: 5281da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 5291da177e4SLinus Torvalds out_neigh_release: 5301da177e4SLinus Torvalds neigh_release(n); 5311da177e4SLinus Torvalds goto out; 5321da177e4SLinus Torvalds } 533a263b309SDavid S. Miller EXPORT_SYMBOL(__neigh_create); 5341da177e4SLinus Torvalds 535be01d655SYOSHIFUJI Hideaki static u32 pneigh_hash(const void *pkey, int key_len) 536fa86d322SPavel Emelyanov { 537fa86d322SPavel Emelyanov u32 hash_val = *(u32 *)(pkey + key_len - 4); 538fa86d322SPavel Emelyanov hash_val ^= (hash_val >> 16); 539fa86d322SPavel Emelyanov hash_val ^= hash_val >> 8; 540fa86d322SPavel Emelyanov hash_val ^= hash_val >> 4; 541fa86d322SPavel Emelyanov hash_val &= PNEIGH_HASHMASK; 542be01d655SYOSHIFUJI Hideaki return hash_val; 543fa86d322SPavel Emelyanov } 544fa86d322SPavel Emelyanov 545be01d655SYOSHIFUJI Hideaki static struct pneigh_entry *__pneigh_lookup_1(struct pneigh_entry *n, 546be01d655SYOSHIFUJI Hideaki struct net *net, 547be01d655SYOSHIFUJI Hideaki const void *pkey, 548be01d655SYOSHIFUJI Hideaki int key_len, 549be01d655SYOSHIFUJI Hideaki struct net_device *dev) 550be01d655SYOSHIFUJI Hideaki { 551be01d655SYOSHIFUJI Hideaki while (n) { 552be01d655SYOSHIFUJI Hideaki if (!memcmp(n->key, pkey, key_len) && 553be01d655SYOSHIFUJI Hideaki net_eq(pneigh_net(n), net) && 554be01d655SYOSHIFUJI Hideaki (n->dev == dev || !n->dev)) 555fa86d322SPavel Emelyanov return n; 556be01d655SYOSHIFUJI Hideaki n = n->next; 557be01d655SYOSHIFUJI Hideaki } 558be01d655SYOSHIFUJI Hideaki return NULL; 559be01d655SYOSHIFUJI Hideaki } 560be01d655SYOSHIFUJI Hideaki 561be01d655SYOSHIFUJI Hideaki struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, 562be01d655SYOSHIFUJI Hideaki struct net *net, const void *pkey, struct net_device *dev) 563be01d655SYOSHIFUJI Hideaki { 564be01d655SYOSHIFUJI Hideaki int key_len = tbl->key_len; 565be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 566be01d655SYOSHIFUJI Hideaki 567be01d655SYOSHIFUJI Hideaki return __pneigh_lookup_1(tbl->phash_buckets[hash_val], 568be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 569fa86d322SPavel Emelyanov } 5700a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL_GPL(__pneigh_lookup); 571fa86d322SPavel Emelyanov 572426b5303SEric W. Biederman struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, 573426b5303SEric W. Biederman struct net *net, const void *pkey, 5741da177e4SLinus Torvalds struct net_device *dev, int creat) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct pneigh_entry *n; 5771da177e4SLinus Torvalds int key_len = tbl->key_len; 578be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds read_lock_bh(&tbl->lock); 581be01d655SYOSHIFUJI Hideaki n = __pneigh_lookup_1(tbl->phash_buckets[hash_val], 582be01d655SYOSHIFUJI Hideaki net, pkey, key_len, dev); 583be01d655SYOSHIFUJI Hideaki read_unlock_bh(&tbl->lock); 5841da177e4SLinus Torvalds 585be01d655SYOSHIFUJI Hideaki if (n || !creat) 5861da177e4SLinus Torvalds goto out; 5871da177e4SLinus Torvalds 5884ae28944SPavel Emelyanov ASSERT_RTNL(); 5894ae28944SPavel Emelyanov 5901da177e4SLinus Torvalds n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); 5911da177e4SLinus Torvalds if (!n) 5921da177e4SLinus Torvalds goto out; 5931da177e4SLinus Torvalds 594efd7ef1cSEric W. Biederman write_pnet(&n->net, net); 5951da177e4SLinus Torvalds memcpy(n->key, pkey, key_len); 5961da177e4SLinus Torvalds n->dev = dev; 5971da177e4SLinus Torvalds if (dev) 5981da177e4SLinus Torvalds dev_hold(dev); 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds if (tbl->pconstructor && tbl->pconstructor(n)) { 6011da177e4SLinus Torvalds if (dev) 6021da177e4SLinus Torvalds dev_put(dev); 6031da177e4SLinus Torvalds kfree(n); 6041da177e4SLinus Torvalds n = NULL; 6051da177e4SLinus Torvalds goto out; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6091da177e4SLinus Torvalds n->next = tbl->phash_buckets[hash_val]; 6101da177e4SLinus Torvalds tbl->phash_buckets[hash_val] = n; 6111da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6121da177e4SLinus Torvalds out: 6131da177e4SLinus Torvalds return n; 6141da177e4SLinus Torvalds } 6150a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_lookup); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds 618426b5303SEric W. Biederman int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey, 6191da177e4SLinus Torvalds struct net_device *dev) 6201da177e4SLinus Torvalds { 6211da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6221da177e4SLinus Torvalds int key_len = tbl->key_len; 623be01d655SYOSHIFUJI Hideaki u32 hash_val = pneigh_hash(pkey, key_len); 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 6261da177e4SLinus Torvalds for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL; 6271da177e4SLinus Torvalds np = &n->next) { 628426b5303SEric W. Biederman if (!memcmp(n->key, pkey, key_len) && n->dev == dev && 629878628fbSYOSHIFUJI Hideaki net_eq(pneigh_net(n), net)) { 6301da177e4SLinus Torvalds *np = n->next; 6311da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6321da177e4SLinus Torvalds if (tbl->pdestructor) 6331da177e4SLinus Torvalds tbl->pdestructor(n); 6341da177e4SLinus Torvalds if (n->dev) 6351da177e4SLinus Torvalds dev_put(n->dev); 6361da177e4SLinus Torvalds kfree(n); 6371da177e4SLinus Torvalds return 0; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 6411da177e4SLinus Torvalds return -ENOENT; 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev) 6451da177e4SLinus Torvalds { 6461da177e4SLinus Torvalds struct pneigh_entry *n, **np; 6471da177e4SLinus Torvalds u32 h; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds for (h = 0; h <= PNEIGH_HASHMASK; h++) { 6501da177e4SLinus Torvalds np = &tbl->phash_buckets[h]; 6511da177e4SLinus Torvalds while ((n = *np) != NULL) { 6521da177e4SLinus Torvalds if (!dev || n->dev == dev) { 6531da177e4SLinus Torvalds *np = n->next; 6541da177e4SLinus Torvalds if (tbl->pdestructor) 6551da177e4SLinus Torvalds tbl->pdestructor(n); 6561da177e4SLinus Torvalds if (n->dev) 6571da177e4SLinus Torvalds dev_put(n->dev); 6581da177e4SLinus Torvalds kfree(n); 6591da177e4SLinus Torvalds continue; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds np = &n->next; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds return -ENOENT; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 66706f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms); 66806f0511dSDenis V. Lunev 66906f0511dSDenis V. Lunev static inline void neigh_parms_put(struct neigh_parms *parms) 67006f0511dSDenis V. Lunev { 67106f0511dSDenis V. Lunev if (atomic_dec_and_test(&parms->refcnt)) 67206f0511dSDenis V. Lunev neigh_parms_destroy(parms); 67306f0511dSDenis V. Lunev } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* 6761da177e4SLinus Torvalds * neighbour must already be out of the table; 6771da177e4SLinus Torvalds * 6781da177e4SLinus Torvalds */ 6791da177e4SLinus Torvalds void neigh_destroy(struct neighbour *neigh) 6801da177e4SLinus Torvalds { 681da6a8fa0SDavid Miller struct net_device *dev = neigh->dev; 682da6a8fa0SDavid Miller 6831da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(neigh->tbl, destroys); 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds if (!neigh->dead) { 686e005d193SJoe Perches pr_warn("Destroying alive neighbour %p\n", neigh); 6871da177e4SLinus Torvalds dump_stack(); 6881da177e4SLinus Torvalds return; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds if (neigh_del_timer(neigh)) 692e005d193SJoe Perches pr_warn("Impossible event\n"); 6931da177e4SLinus Torvalds 694c9ab4d85SEric Dumazet write_lock_bh(&neigh->lock); 695c9ab4d85SEric Dumazet __skb_queue_purge(&neigh->arp_queue); 696c9ab4d85SEric Dumazet write_unlock_bh(&neigh->lock); 6978b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 6981da177e4SLinus Torvalds 699447f2191SDavid S. Miller if (dev->netdev_ops->ndo_neigh_destroy) 700447f2191SDavid S. Miller dev->netdev_ops->ndo_neigh_destroy(neigh); 701447f2191SDavid S. Miller 702da6a8fa0SDavid Miller dev_put(dev); 7031da177e4SLinus Torvalds neigh_parms_put(neigh->parms); 7041da177e4SLinus Torvalds 705d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is destroyed\n", neigh); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds atomic_dec(&neigh->tbl->entries); 7085b8b0060SDavid Miller kfree_rcu(neigh, rcu); 7091da177e4SLinus Torvalds } 7100a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_destroy); 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* Neighbour state is suspicious; 7131da177e4SLinus Torvalds disable fast path. 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds Called with write_locked neigh. 7161da177e4SLinus Torvalds */ 7171da177e4SLinus Torvalds static void neigh_suspect(struct neighbour *neigh) 7181da177e4SLinus Torvalds { 719d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is suspected\n", neigh); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds neigh->output = neigh->ops->output; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds /* Neighbour state is OK; 7251da177e4SLinus Torvalds enable fast path. 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds Called with write_locked neigh. 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds static void neigh_connect(struct neighbour *neigh) 7301da177e4SLinus Torvalds { 731d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is connected\n", neigh); 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds neigh->output = neigh->ops->connected_output; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736e4c4e448SEric Dumazet static void neigh_periodic_work(struct work_struct *work) 7371da177e4SLinus Torvalds { 738e4c4e448SEric Dumazet struct neigh_table *tbl = container_of(work, struct neigh_table, gc_work.work); 739767e97e1SEric Dumazet struct neighbour *n; 740767e97e1SEric Dumazet struct neighbour __rcu **np; 741e4c4e448SEric Dumazet unsigned int i; 742d6bf7817SEric Dumazet struct neigh_hash_table *nht; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs); 7451da177e4SLinus Torvalds 746e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 747d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 748d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds /* 7511da177e4SLinus Torvalds * periodically recompute ReachableTime from random function 7521da177e4SLinus Torvalds */ 7531da177e4SLinus Torvalds 754e4c4e448SEric Dumazet if (time_after(jiffies, tbl->last_rand + 300 * HZ)) { 7551da177e4SLinus Torvalds struct neigh_parms *p; 756e4c4e448SEric Dumazet tbl->last_rand = jiffies; 75775fbfd33SNicolas Dichtel list_for_each_entry(p, &tbl->parms_list, list) 7581da177e4SLinus Torvalds p->reachable_time = 7591f9248e5SJiri Pirko neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 762feff9ab2SDuan Jiong if (atomic_read(&tbl->entries) < tbl->gc_thresh1) 763feff9ab2SDuan Jiong goto out; 764feff9ab2SDuan Jiong 765cd089336SDavid S. Miller for (i = 0 ; i < (1 << nht->hash_shift); i++) { 766d6bf7817SEric Dumazet np = &nht->hash_buckets[i]; 7671da177e4SLinus Torvalds 768767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 769767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 7701da177e4SLinus Torvalds unsigned int state; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds write_lock(&n->lock); 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds state = n->nud_state; 7751da177e4SLinus Torvalds if (state & (NUD_PERMANENT | NUD_IN_TIMER)) { 7761da177e4SLinus Torvalds write_unlock(&n->lock); 7771da177e4SLinus Torvalds goto next_elt; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (time_before(n->used, n->confirmed)) 7811da177e4SLinus Torvalds n->used = n->confirmed; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds if (atomic_read(&n->refcnt) == 1 && 7841da177e4SLinus Torvalds (state == NUD_FAILED || 7851f9248e5SJiri Pirko time_after(jiffies, n->used + NEIGH_VAR(n->parms, GC_STALETIME)))) { 7861da177e4SLinus Torvalds *np = n->next; 7871da177e4SLinus Torvalds n->dead = 1; 7881da177e4SLinus Torvalds write_unlock(&n->lock); 7894f494554SThomas Graf neigh_cleanup_and_release(n); 7901da177e4SLinus Torvalds continue; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds write_unlock(&n->lock); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds next_elt: 7951da177e4SLinus Torvalds np = &n->next; 7961da177e4SLinus Torvalds } 797e4c4e448SEric Dumazet /* 798e4c4e448SEric Dumazet * It's fine to release lock here, even if hash table 799e4c4e448SEric Dumazet * grows while we are preempted. 800e4c4e448SEric Dumazet */ 801e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 802e4c4e448SEric Dumazet cond_resched(); 803e4c4e448SEric Dumazet write_lock_bh(&tbl->lock); 80484338a6cSMichel Machado nht = rcu_dereference_protected(tbl->nht, 80584338a6cSMichel Machado lockdep_is_held(&tbl->lock)); 806e4c4e448SEric Dumazet } 8072724680bSYOSHIFUJI Hideaki / 吉藤英明 out: 8081f9248e5SJiri Pirko /* Cycle through all hash buckets every BASE_REACHABLE_TIME/2 ticks. 8091f9248e5SJiri Pirko * ARP entry timeouts range from 1/2 BASE_REACHABLE_TIME to 3/2 8101f9248e5SJiri Pirko * BASE_REACHABLE_TIME. 8111da177e4SLinus Torvalds */ 812f618002bSviresh kumar queue_delayed_work(system_power_efficient_wq, &tbl->gc_work, 8131f9248e5SJiri Pirko NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME) >> 1); 814e4c4e448SEric Dumazet write_unlock_bh(&tbl->lock); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds static __inline__ int neigh_max_probes(struct neighbour *n) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds struct neigh_parms *p = n->parms; 820a960ff81STimo Teräs int max_probes = NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES); 821a960ff81STimo Teräs if (!(n->nud_state & NUD_PROBE)) 822a960ff81STimo Teräs max_probes += NEIGH_VAR(p, MCAST_PROBES); 823a960ff81STimo Teräs return max_probes; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8265ef12d98STimo Teras static void neigh_invalidate(struct neighbour *neigh) 8270a141509SEric Dumazet __releases(neigh->lock) 8280a141509SEric Dumazet __acquires(neigh->lock) 8295ef12d98STimo Teras { 8305ef12d98STimo Teras struct sk_buff *skb; 8315ef12d98STimo Teras 8325ef12d98STimo Teras NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); 833d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is failed\n", neigh); 8345ef12d98STimo Teras neigh->updated = jiffies; 8355ef12d98STimo Teras 8365ef12d98STimo Teras /* It is very thin place. report_unreachable is very complicated 8375ef12d98STimo Teras routine. Particularly, it can hit the same neighbour entry! 8385ef12d98STimo Teras 8395ef12d98STimo Teras So that, we try to be accurate and avoid dead loop. --ANK 8405ef12d98STimo Teras */ 8415ef12d98STimo Teras while (neigh->nud_state == NUD_FAILED && 8425ef12d98STimo Teras (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 8435ef12d98STimo Teras write_unlock(&neigh->lock); 8445ef12d98STimo Teras neigh->ops->error_report(neigh, skb); 8455ef12d98STimo Teras write_lock(&neigh->lock); 8465ef12d98STimo Teras } 847c9ab4d85SEric Dumazet __skb_queue_purge(&neigh->arp_queue); 8488b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 8495ef12d98STimo Teras } 8505ef12d98STimo Teras 851cd28ca0aSEric Dumazet static void neigh_probe(struct neighbour *neigh) 852cd28ca0aSEric Dumazet __releases(neigh->lock) 853cd28ca0aSEric Dumazet { 8544ed377e3SHannes Frederic Sowa struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue); 855cd28ca0aSEric Dumazet /* keep skb alive even if arp_queue overflows */ 856cd28ca0aSEric Dumazet if (skb) 857cd28ca0aSEric Dumazet skb = skb_copy(skb, GFP_ATOMIC); 858cd28ca0aSEric Dumazet write_unlock(&neigh->lock); 859cd28ca0aSEric Dumazet neigh->ops->solicit(neigh, skb); 860cd28ca0aSEric Dumazet atomic_inc(&neigh->probes); 861cd28ca0aSEric Dumazet kfree_skb(skb); 862cd28ca0aSEric Dumazet } 863cd28ca0aSEric Dumazet 8641da177e4SLinus Torvalds /* Called when a timer expires for a neighbour entry. */ 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds static void neigh_timer_handler(unsigned long arg) 8671da177e4SLinus Torvalds { 8681da177e4SLinus Torvalds unsigned long now, next; 8691da177e4SLinus Torvalds struct neighbour *neigh = (struct neighbour *)arg; 87095c96174SEric Dumazet unsigned int state; 8711da177e4SLinus Torvalds int notify = 0; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds write_lock(&neigh->lock); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds state = neigh->nud_state; 8761da177e4SLinus Torvalds now = jiffies; 8771da177e4SLinus Torvalds next = now + HZ; 8781da177e4SLinus Torvalds 879045f7b3bSDavid S. Miller if (!(state & NUD_IN_TIMER)) 8801da177e4SLinus Torvalds goto out; 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds if (state & NUD_REACHABLE) { 8831da177e4SLinus Torvalds if (time_before_eq(now, 8841da177e4SLinus Torvalds neigh->confirmed + neigh->parms->reachable_time)) { 885d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is still alive\n", neigh); 8861da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 8871da177e4SLinus Torvalds } else if (time_before_eq(now, 8881f9248e5SJiri Pirko neigh->used + 8891f9248e5SJiri Pirko NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) { 890d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is delayed\n", neigh); 8911da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 892955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8931da177e4SLinus Torvalds neigh_suspect(neigh); 8941f9248e5SJiri Pirko next = now + NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME); 8951da177e4SLinus Torvalds } else { 896d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is suspected\n", neigh); 8971da177e4SLinus Torvalds neigh->nud_state = NUD_STALE; 898955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 8991da177e4SLinus Torvalds neigh_suspect(neigh); 9008d71740cSTom Tucker notify = 1; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds } else if (state & NUD_DELAY) { 9031da177e4SLinus Torvalds if (time_before_eq(now, 9041f9248e5SJiri Pirko neigh->confirmed + 9051f9248e5SJiri Pirko NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME))) { 906d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is now reachable\n", neigh); 9071da177e4SLinus Torvalds neigh->nud_state = NUD_REACHABLE; 908955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9091da177e4SLinus Torvalds neigh_connect(neigh); 9108d71740cSTom Tucker notify = 1; 9111da177e4SLinus Torvalds next = neigh->confirmed + neigh->parms->reachable_time; 9121da177e4SLinus Torvalds } else { 913d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is probed\n", neigh); 9141da177e4SLinus Torvalds neigh->nud_state = NUD_PROBE; 915955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9161da177e4SLinus Torvalds atomic_set(&neigh->probes, 0); 9171f9248e5SJiri Pirko next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME); 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds } else { 9201da177e4SLinus Torvalds /* NUD_PROBE|NUD_INCOMPLETE */ 9211f9248e5SJiri Pirko next = now + NEIGH_VAR(neigh->parms, RETRANS_TIME); 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && 9251da177e4SLinus Torvalds atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { 9261da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 9271da177e4SLinus Torvalds notify = 1; 9285ef12d98STimo Teras neigh_invalidate(neigh); 9295e2c21dcSDuan Jiong goto out; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds if (neigh->nud_state & NUD_IN_TIMER) { 9331da177e4SLinus Torvalds if (time_before(next, jiffies + HZ/2)) 9341da177e4SLinus Torvalds next = jiffies + HZ/2; 9356fb9974fSHerbert Xu if (!mod_timer(&neigh->timer, next)) 9366fb9974fSHerbert Xu neigh_hold(neigh); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { 939cd28ca0aSEric Dumazet neigh_probe(neigh); 9409ff56607SDavid S. Miller } else { 9411da177e4SLinus Torvalds out: 9421da177e4SLinus Torvalds write_unlock(&neigh->lock); 9439ff56607SDavid S. Miller } 9441da177e4SLinus Torvalds 945d961db35SThomas Graf if (notify) 946d961db35SThomas Graf neigh_update_notify(neigh); 947d961db35SThomas Graf 9481da177e4SLinus Torvalds neigh_release(neigh); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds int rc; 954cd28ca0aSEric Dumazet bool immediate_probe = false; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds rc = 0; 9591da177e4SLinus Torvalds if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) 9601da177e4SLinus Torvalds goto out_unlock_bh; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { 9631f9248e5SJiri Pirko if (NEIGH_VAR(neigh->parms, MCAST_PROBES) + 9641f9248e5SJiri Pirko NEIGH_VAR(neigh->parms, APP_PROBES)) { 965cd28ca0aSEric Dumazet unsigned long next, now = jiffies; 966cd28ca0aSEric Dumazet 9671f9248e5SJiri Pirko atomic_set(&neigh->probes, 9681f9248e5SJiri Pirko NEIGH_VAR(neigh->parms, UCAST_PROBES)); 9691da177e4SLinus Torvalds neigh->nud_state = NUD_INCOMPLETE; 970cd28ca0aSEric Dumazet neigh->updated = now; 9711f9248e5SJiri Pirko next = now + max(NEIGH_VAR(neigh->parms, RETRANS_TIME), 9721f9248e5SJiri Pirko HZ/2); 973cd28ca0aSEric Dumazet neigh_add_timer(neigh, next); 974cd28ca0aSEric Dumazet immediate_probe = true; 9751da177e4SLinus Torvalds } else { 9761da177e4SLinus Torvalds neigh->nud_state = NUD_FAILED; 977955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9781da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds kfree_skb(skb); 9811da177e4SLinus Torvalds return 1; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds } else if (neigh->nud_state & NUD_STALE) { 984d5d427cdSJoe Perches neigh_dbg(2, "neigh %p is delayed\n", neigh); 9851da177e4SLinus Torvalds neigh->nud_state = NUD_DELAY; 986955aaa2fSYOSHIFUJI Hideaki neigh->updated = jiffies; 9871f9248e5SJiri Pirko neigh_add_timer(neigh, jiffies + 9881f9248e5SJiri Pirko NEIGH_VAR(neigh->parms, DELAY_PROBE_TIME)); 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds if (neigh->nud_state == NUD_INCOMPLETE) { 9921da177e4SLinus Torvalds if (skb) { 9938b5c171bSEric Dumazet while (neigh->arp_queue_len_bytes + skb->truesize > 9941f9248e5SJiri Pirko NEIGH_VAR(neigh->parms, QUEUE_LEN_BYTES)) { 9951da177e4SLinus Torvalds struct sk_buff *buff; 9968b5c171bSEric Dumazet 997f72051b0SDavid S. Miller buff = __skb_dequeue(&neigh->arp_queue); 9988b5c171bSEric Dumazet if (!buff) 9998b5c171bSEric Dumazet break; 10008b5c171bSEric Dumazet neigh->arp_queue_len_bytes -= buff->truesize; 10011da177e4SLinus Torvalds kfree_skb(buff); 10029a6d276eSNeil Horman NEIGH_CACHE_STAT_INC(neigh->tbl, unres_discards); 10031da177e4SLinus Torvalds } 1004a4731138SEric Dumazet skb_dst_force(skb); 10051da177e4SLinus Torvalds __skb_queue_tail(&neigh->arp_queue, skb); 10068b5c171bSEric Dumazet neigh->arp_queue_len_bytes += skb->truesize; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds rc = 1; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds out_unlock_bh: 1011cd28ca0aSEric Dumazet if (immediate_probe) 1012cd28ca0aSEric Dumazet neigh_probe(neigh); 1013cd28ca0aSEric Dumazet else 1014cd28ca0aSEric Dumazet write_unlock(&neigh->lock); 1015cd28ca0aSEric Dumazet local_bh_enable(); 10161da177e4SLinus Torvalds return rc; 10171da177e4SLinus Torvalds } 10180a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(__neigh_event_send); 10191da177e4SLinus Torvalds 1020f6b72b62SDavid S. Miller static void neigh_update_hhs(struct neighbour *neigh) 10211da177e4SLinus Torvalds { 10221da177e4SLinus Torvalds struct hh_cache *hh; 10233b04dddeSStephen Hemminger void (*update)(struct hh_cache*, const struct net_device*, const unsigned char *) 102491a72a70SDoug Kehn = NULL; 102591a72a70SDoug Kehn 102691a72a70SDoug Kehn if (neigh->dev->header_ops) 102791a72a70SDoug Kehn update = neigh->dev->header_ops->cache_update; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (update) { 1030f6b72b62SDavid S. Miller hh = &neigh->hh; 1031f6b72b62SDavid S. Miller if (hh->hh_len) { 10323644f0ceSStephen Hemminger write_seqlock_bh(&hh->hh_lock); 10331da177e4SLinus Torvalds update(hh, neigh->dev, neigh->ha); 10343644f0ceSStephen Hemminger write_sequnlock_bh(&hh->hh_lock); 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds /* Generic update routine. 10421da177e4SLinus Torvalds -- lladdr is new lladdr or NULL, if it is not supplied. 10431da177e4SLinus Torvalds -- new is new state. 10441da177e4SLinus Torvalds -- flags 10451da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, 10461da177e4SLinus Torvalds if it is different. 10471da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" 10481da177e4SLinus Torvalds lladdr instead of overriding it 10491da177e4SLinus Torvalds if it is different. 10501da177e4SLinus Torvalds It also allows to retain current state 10511da177e4SLinus Torvalds if lladdr is unchanged. 10521da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN means that the change is administrative. 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing 10551da177e4SLinus Torvalds NTF_ROUTER flag. 10561da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as 10571da177e4SLinus Torvalds a router. 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds Caller MUST hold reference count on the entry. 10601da177e4SLinus Torvalds */ 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, 10631da177e4SLinus Torvalds u32 flags) 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds u8 old; 10661da177e4SLinus Torvalds int err; 10671da177e4SLinus Torvalds int notify = 0; 10681da177e4SLinus Torvalds struct net_device *dev; 10691da177e4SLinus Torvalds int update_isrouter = 0; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds dev = neigh->dev; 10741da177e4SLinus Torvalds old = neigh->nud_state; 10751da177e4SLinus Torvalds err = -EPERM; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds if (!(flags & NEIGH_UPDATE_F_ADMIN) && 10781da177e4SLinus Torvalds (old & (NUD_NOARP | NUD_PERMANENT))) 10791da177e4SLinus Torvalds goto out; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds if (!(new & NUD_VALID)) { 10821da177e4SLinus Torvalds neigh_del_timer(neigh); 10831da177e4SLinus Torvalds if (old & NUD_CONNECTED) 10841da177e4SLinus Torvalds neigh_suspect(neigh); 10851da177e4SLinus Torvalds neigh->nud_state = new; 10861da177e4SLinus Torvalds err = 0; 10871da177e4SLinus Torvalds notify = old & NUD_VALID; 10885ef12d98STimo Teras if ((old & (NUD_INCOMPLETE | NUD_PROBE)) && 10895ef12d98STimo Teras (new & NUD_FAILED)) { 10905ef12d98STimo Teras neigh_invalidate(neigh); 10915ef12d98STimo Teras notify = 1; 10925ef12d98STimo Teras } 10931da177e4SLinus Torvalds goto out; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds /* Compare new lladdr with cached one */ 10971da177e4SLinus Torvalds if (!dev->addr_len) { 10981da177e4SLinus Torvalds /* First case: device needs no address. */ 10991da177e4SLinus Torvalds lladdr = neigh->ha; 11001da177e4SLinus Torvalds } else if (lladdr) { 11011da177e4SLinus Torvalds /* The second case: if something is already cached 11021da177e4SLinus Torvalds and a new address is proposed: 11031da177e4SLinus Torvalds - compare new & old 11041da177e4SLinus Torvalds - if they are different, check override flag 11051da177e4SLinus Torvalds */ 11061da177e4SLinus Torvalds if ((old & NUD_VALID) && 11071da177e4SLinus Torvalds !memcmp(lladdr, neigh->ha, dev->addr_len)) 11081da177e4SLinus Torvalds lladdr = neigh->ha; 11091da177e4SLinus Torvalds } else { 11101da177e4SLinus Torvalds /* No address is supplied; if we know something, 11111da177e4SLinus Torvalds use it, otherwise discard the request. 11121da177e4SLinus Torvalds */ 11131da177e4SLinus Torvalds err = -EINVAL; 11141da177e4SLinus Torvalds if (!(old & NUD_VALID)) 11151da177e4SLinus Torvalds goto out; 11161da177e4SLinus Torvalds lladdr = neigh->ha; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11201da177e4SLinus Torvalds neigh->confirmed = jiffies; 11211da177e4SLinus Torvalds neigh->updated = jiffies; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds /* If entry was valid and address is not changed, 11241da177e4SLinus Torvalds do not change entry state, if new one is STALE. 11251da177e4SLinus Torvalds */ 11261da177e4SLinus Torvalds err = 0; 11271da177e4SLinus Torvalds update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; 11281da177e4SLinus Torvalds if (old & NUD_VALID) { 11291da177e4SLinus Torvalds if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { 11301da177e4SLinus Torvalds update_isrouter = 0; 11311da177e4SLinus Torvalds if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && 11321da177e4SLinus Torvalds (old & NUD_CONNECTED)) { 11331da177e4SLinus Torvalds lladdr = neigh->ha; 11341da177e4SLinus Torvalds new = NUD_STALE; 11351da177e4SLinus Torvalds } else 11361da177e4SLinus Torvalds goto out; 11371da177e4SLinus Torvalds } else { 11381da177e4SLinus Torvalds if (lladdr == neigh->ha && new == NUD_STALE && 11391da177e4SLinus Torvalds ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || 11401da177e4SLinus Torvalds (old & NUD_CONNECTED)) 11411da177e4SLinus Torvalds ) 11421da177e4SLinus Torvalds new = old; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds if (new != old) { 11471da177e4SLinus Torvalds neigh_del_timer(neigh); 1148a43d8994SPavel Emelyanov if (new & NUD_IN_TIMER) 1149667347f1SDavid S. Miller neigh_add_timer(neigh, (jiffies + 11501da177e4SLinus Torvalds ((new & NUD_REACHABLE) ? 1151667347f1SDavid S. Miller neigh->parms->reachable_time : 1152667347f1SDavid S. Miller 0))); 11531da177e4SLinus Torvalds neigh->nud_state = new; 115453385d2dSBob Gilligan notify = 1; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if (lladdr != neigh->ha) { 11580ed8ddf4SEric Dumazet write_seqlock(&neigh->ha_lock); 11591da177e4SLinus Torvalds memcpy(&neigh->ha, lladdr, dev->addr_len); 11600ed8ddf4SEric Dumazet write_sequnlock(&neigh->ha_lock); 11611da177e4SLinus Torvalds neigh_update_hhs(neigh); 11621da177e4SLinus Torvalds if (!(new & NUD_CONNECTED)) 11631da177e4SLinus Torvalds neigh->confirmed = jiffies - 11641f9248e5SJiri Pirko (NEIGH_VAR(neigh->parms, BASE_REACHABLE_TIME) << 1); 11651da177e4SLinus Torvalds notify = 1; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds if (new == old) 11681da177e4SLinus Torvalds goto out; 11691da177e4SLinus Torvalds if (new & NUD_CONNECTED) 11701da177e4SLinus Torvalds neigh_connect(neigh); 11711da177e4SLinus Torvalds else 11721da177e4SLinus Torvalds neigh_suspect(neigh); 11731da177e4SLinus Torvalds if (!(old & NUD_VALID)) { 11741da177e4SLinus Torvalds struct sk_buff *skb; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* Again: avoid dead loop if something went wrong */ 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds while (neigh->nud_state & NUD_VALID && 11791da177e4SLinus Torvalds (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { 118069cce1d1SDavid S. Miller struct dst_entry *dst = skb_dst(skb); 118169cce1d1SDavid S. Miller struct neighbour *n2, *n1 = neigh; 11821da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 1183e049f288Sroy.qing.li@gmail.com 1184e049f288Sroy.qing.li@gmail.com rcu_read_lock(); 118513a43d94SDavid S. Miller 118613a43d94SDavid S. Miller /* Why not just use 'neigh' as-is? The problem is that 118713a43d94SDavid S. Miller * things such as shaper, eql, and sch_teql can end up 118813a43d94SDavid S. Miller * using alternative, different, neigh objects to output 118913a43d94SDavid S. Miller * the packet in the output path. So what we need to do 119013a43d94SDavid S. Miller * here is re-lookup the top-level neigh in the path so 119113a43d94SDavid S. Miller * we can reinject the packet there. 119213a43d94SDavid S. Miller */ 119313a43d94SDavid S. Miller n2 = NULL; 119413a43d94SDavid S. Miller if (dst) { 119513a43d94SDavid S. Miller n2 = dst_neigh_lookup_skb(dst, skb); 119613a43d94SDavid S. Miller if (n2) 119769cce1d1SDavid S. Miller n1 = n2; 119813a43d94SDavid S. Miller } 11998f40b161SDavid S. Miller n1->output(n1, skb); 120013a43d94SDavid S. Miller if (n2) 120113a43d94SDavid S. Miller neigh_release(n2); 1202e049f288Sroy.qing.li@gmail.com rcu_read_unlock(); 1203e049f288Sroy.qing.li@gmail.com 12041da177e4SLinus Torvalds write_lock_bh(&neigh->lock); 12051da177e4SLinus Torvalds } 1206c9ab4d85SEric Dumazet __skb_queue_purge(&neigh->arp_queue); 12078b5c171bSEric Dumazet neigh->arp_queue_len_bytes = 0; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds out: 12101da177e4SLinus Torvalds if (update_isrouter) { 12111da177e4SLinus Torvalds neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? 12121da177e4SLinus Torvalds (neigh->flags | NTF_ROUTER) : 12131da177e4SLinus Torvalds (neigh->flags & ~NTF_ROUTER); 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds write_unlock_bh(&neigh->lock); 12168d71740cSTom Tucker 12178d71740cSTom Tucker if (notify) 1218d961db35SThomas Graf neigh_update_notify(neigh); 1219d961db35SThomas Graf 12201da177e4SLinus Torvalds return err; 12211da177e4SLinus Torvalds } 12220a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_update); 12231da177e4SLinus Torvalds 12247e980569SJiri Benc /* Update the neigh to listen temporarily for probe responses, even if it is 12257e980569SJiri Benc * in a NUD_FAILED state. The caller has to hold neigh->lock for writing. 12267e980569SJiri Benc */ 12277e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh) 12287e980569SJiri Benc { 12297e980569SJiri Benc neigh->updated = jiffies; 12307e980569SJiri Benc if (!(neigh->nud_state & NUD_FAILED)) 12317e980569SJiri Benc return; 12322176d5d4SDuan Jiong neigh->nud_state = NUD_INCOMPLETE; 12332176d5d4SDuan Jiong atomic_set(&neigh->probes, neigh_max_probes(neigh)); 12347e980569SJiri Benc neigh_add_timer(neigh, 12357e980569SJiri Benc jiffies + NEIGH_VAR(neigh->parms, RETRANS_TIME)); 12367e980569SJiri Benc } 12377e980569SJiri Benc EXPORT_SYMBOL(__neigh_set_probe_once); 12387e980569SJiri Benc 12391da177e4SLinus Torvalds struct neighbour *neigh_event_ns(struct neigh_table *tbl, 12401da177e4SLinus Torvalds u8 *lladdr, void *saddr, 12411da177e4SLinus Torvalds struct net_device *dev) 12421da177e4SLinus Torvalds { 12431da177e4SLinus Torvalds struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, 12441da177e4SLinus Torvalds lladdr || !dev->addr_len); 12451da177e4SLinus Torvalds if (neigh) 12461da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 12471da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE); 12481da177e4SLinus Torvalds return neigh; 12491da177e4SLinus Torvalds } 12500a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_event_ns); 12511da177e4SLinus Torvalds 125234d101ddSEric Dumazet /* called with read_lock_bh(&n->lock); */ 1253bdf53c58SEric W. Biederman static void neigh_hh_init(struct neighbour *n) 12541da177e4SLinus Torvalds { 1255bdf53c58SEric W. Biederman struct net_device *dev = n->dev; 1256bdf53c58SEric W. Biederman __be16 prot = n->tbl->protocol; 1257f6b72b62SDavid S. Miller struct hh_cache *hh = &n->hh; 12580ed8ddf4SEric Dumazet 12590ed8ddf4SEric Dumazet write_lock_bh(&n->lock); 126034d101ddSEric Dumazet 1261f6b72b62SDavid S. Miller /* Only one thread can come in here and initialize the 1262f6b72b62SDavid S. Miller * hh_cache entry. 1263f6b72b62SDavid S. Miller */ 1264b23b5455SDavid S. Miller if (!hh->hh_len) 1265b23b5455SDavid S. Miller dev->header_ops->cache(n, hh, prot); 1266f6b72b62SDavid S. Miller 12670ed8ddf4SEric Dumazet write_unlock_bh(&n->lock); 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds /* Slow and careful. */ 12711da177e4SLinus Torvalds 12728f40b161SDavid S. Miller int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds int rc = 0; 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds if (!neigh_event_send(neigh, skb)) { 12771da177e4SLinus Torvalds int err; 12781da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 12790ed8ddf4SEric Dumazet unsigned int seq; 128034d101ddSEric Dumazet 1281f6b72b62SDavid S. Miller if (dev->header_ops->cache && !neigh->hh.hh_len) 1282bdf53c58SEric W. Biederman neigh_hh_init(neigh); 128334d101ddSEric Dumazet 12840ed8ddf4SEric Dumazet do { 1285e1f16503Sramesh.nagappa@gmail.com __skb_pull(skb, skb_network_offset(skb)); 12860ed8ddf4SEric Dumazet seq = read_seqbegin(&neigh->ha_lock); 12870c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 12881da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 12890ed8ddf4SEric Dumazet } while (read_seqretry(&neigh->ha_lock, seq)); 129034d101ddSEric Dumazet 12911da177e4SLinus Torvalds if (err >= 0) 1292542d4d68SDavid S. Miller rc = dev_queue_xmit(skb); 12931da177e4SLinus Torvalds else 12941da177e4SLinus Torvalds goto out_kfree_skb; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds out: 12971da177e4SLinus Torvalds return rc; 12981da177e4SLinus Torvalds out_kfree_skb: 12991da177e4SLinus Torvalds rc = -EINVAL; 13001da177e4SLinus Torvalds kfree_skb(skb); 13011da177e4SLinus Torvalds goto out; 13021da177e4SLinus Torvalds } 13030a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_resolve_output); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* As fast as possible without hh cache */ 13061da177e4SLinus Torvalds 13078f40b161SDavid S. Miller int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb) 13081da177e4SLinus Torvalds { 13091da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 13100ed8ddf4SEric Dumazet unsigned int seq; 13118f40b161SDavid S. Miller int err; 13121da177e4SLinus Torvalds 13130ed8ddf4SEric Dumazet do { 1314e1f16503Sramesh.nagappa@gmail.com __skb_pull(skb, skb_network_offset(skb)); 13150ed8ddf4SEric Dumazet seq = read_seqbegin(&neigh->ha_lock); 13160c4e8581SStephen Hemminger err = dev_hard_header(skb, dev, ntohs(skb->protocol), 13171da177e4SLinus Torvalds neigh->ha, NULL, skb->len); 13180ed8ddf4SEric Dumazet } while (read_seqretry(&neigh->ha_lock, seq)); 13190ed8ddf4SEric Dumazet 13201da177e4SLinus Torvalds if (err >= 0) 1321542d4d68SDavid S. Miller err = dev_queue_xmit(skb); 13221da177e4SLinus Torvalds else { 13231da177e4SLinus Torvalds err = -EINVAL; 13241da177e4SLinus Torvalds kfree_skb(skb); 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds return err; 13271da177e4SLinus Torvalds } 13280a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_connected_output); 13291da177e4SLinus Torvalds 13308f40b161SDavid S. Miller int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb) 13318f40b161SDavid S. Miller { 13328f40b161SDavid S. Miller return dev_queue_xmit(skb); 13338f40b161SDavid S. Miller } 13348f40b161SDavid S. Miller EXPORT_SYMBOL(neigh_direct_output); 13358f40b161SDavid S. Miller 13361da177e4SLinus Torvalds static void neigh_proxy_process(unsigned long arg) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds struct neigh_table *tbl = (struct neigh_table *)arg; 13391da177e4SLinus Torvalds long sched_next = 0; 13401da177e4SLinus Torvalds unsigned long now = jiffies; 1341f72051b0SDavid S. Miller struct sk_buff *skb, *n; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13441da177e4SLinus Torvalds 1345f72051b0SDavid S. Miller skb_queue_walk_safe(&tbl->proxy_queue, skb, n) { 1346f72051b0SDavid S. Miller long tdif = NEIGH_CB(skb)->sched_next - now; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds if (tdif <= 0) { 1349f72051b0SDavid S. Miller struct net_device *dev = skb->dev; 135020e6074eSEric Dumazet 1351f72051b0SDavid S. Miller __skb_unlink(skb, &tbl->proxy_queue); 135220e6074eSEric Dumazet if (tbl->proxy_redo && netif_running(dev)) { 135320e6074eSEric Dumazet rcu_read_lock(); 1354f72051b0SDavid S. Miller tbl->proxy_redo(skb); 135520e6074eSEric Dumazet rcu_read_unlock(); 135620e6074eSEric Dumazet } else { 1357f72051b0SDavid S. Miller kfree_skb(skb); 135820e6074eSEric Dumazet } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds dev_put(dev); 13611da177e4SLinus Torvalds } else if (!sched_next || tdif < sched_next) 13621da177e4SLinus Torvalds sched_next = tdif; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds del_timer(&tbl->proxy_timer); 13651da177e4SLinus Torvalds if (sched_next) 13661da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, jiffies + sched_next); 13671da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 13711da177e4SLinus Torvalds struct sk_buff *skb) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds unsigned long now = jiffies; 137463862b5bSAruna-Hewapathirane 137563862b5bSAruna-Hewapathirane unsigned long sched_next = now + (prandom_u32() % 13761f9248e5SJiri Pirko NEIGH_VAR(p, PROXY_DELAY)); 13771da177e4SLinus Torvalds 13781f9248e5SJiri Pirko if (tbl->proxy_queue.qlen > NEIGH_VAR(p, PROXY_QLEN)) { 13791da177e4SLinus Torvalds kfree_skb(skb); 13801da177e4SLinus Torvalds return; 13811da177e4SLinus Torvalds } 1382a61bbcf2SPatrick McHardy 1383a61bbcf2SPatrick McHardy NEIGH_CB(skb)->sched_next = sched_next; 1384a61bbcf2SPatrick McHardy NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds spin_lock(&tbl->proxy_queue.lock); 13871da177e4SLinus Torvalds if (del_timer(&tbl->proxy_timer)) { 13881da177e4SLinus Torvalds if (time_before(tbl->proxy_timer.expires, sched_next)) 13891da177e4SLinus Torvalds sched_next = tbl->proxy_timer.expires; 13901da177e4SLinus Torvalds } 1391adf30907SEric Dumazet skb_dst_drop(skb); 13921da177e4SLinus Torvalds dev_hold(skb->dev); 13931da177e4SLinus Torvalds __skb_queue_tail(&tbl->proxy_queue, skb); 13941da177e4SLinus Torvalds mod_timer(&tbl->proxy_timer, sched_next); 13951da177e4SLinus Torvalds spin_unlock(&tbl->proxy_queue.lock); 13961da177e4SLinus Torvalds } 13970a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(pneigh_enqueue); 13981da177e4SLinus Torvalds 139997fd5bc7STobias Klauser static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl, 1400426b5303SEric W. Biederman struct net *net, int ifindex) 1401426b5303SEric W. Biederman { 1402426b5303SEric W. Biederman struct neigh_parms *p; 1403426b5303SEric W. Biederman 140475fbfd33SNicolas Dichtel list_for_each_entry(p, &tbl->parms_list, list) { 1405878628fbSYOSHIFUJI Hideaki if ((p->dev && p->dev->ifindex == ifindex && net_eq(neigh_parms_net(p), net)) || 1406170d6f99SGao feng (!p->dev && !ifindex && net_eq(net, &init_net))) 1407426b5303SEric W. Biederman return p; 1408426b5303SEric W. Biederman } 1409426b5303SEric W. Biederman 1410426b5303SEric W. Biederman return NULL; 1411426b5303SEric W. Biederman } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 14141da177e4SLinus Torvalds struct neigh_table *tbl) 14151da177e4SLinus Torvalds { 1416cf89d6b2SGao feng struct neigh_parms *p; 141700829823SStephen Hemminger struct net *net = dev_net(dev); 141800829823SStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 14191da177e4SLinus Torvalds 1420cf89d6b2SGao feng p = kmemdup(&tbl->parms, sizeof(*p), GFP_KERNEL); 14211da177e4SLinus Torvalds if (p) { 14221da177e4SLinus Torvalds p->tbl = tbl; 14231da177e4SLinus Torvalds atomic_set(&p->refcnt, 1); 14241da177e4SLinus Torvalds p->reachable_time = 14251f9248e5SJiri Pirko neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); 1426c7fb64dbSThomas Graf dev_hold(dev); 1427c7fb64dbSThomas Graf p->dev = dev; 1428efd7ef1cSEric W. Biederman write_pnet(&p->net, net); 14291da177e4SLinus Torvalds p->sysctl_table = NULL; 143063134803SVeaceslav Falico 143163134803SVeaceslav Falico if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 143263134803SVeaceslav Falico dev_put(dev); 143363134803SVeaceslav Falico kfree(p); 143463134803SVeaceslav Falico return NULL; 143563134803SVeaceslav Falico } 143663134803SVeaceslav Falico 14371da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 143875fbfd33SNicolas Dichtel list_add(&p->list, &tbl->parms.list); 14391da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 14401d4c8c29SJiri Pirko 14411d4c8c29SJiri Pirko neigh_parms_data_state_cleanall(p); 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds return p; 14441da177e4SLinus Torvalds } 14450a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_alloc); 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds static void neigh_rcu_free_parms(struct rcu_head *head) 14481da177e4SLinus Torvalds { 14491da177e4SLinus Torvalds struct neigh_parms *parms = 14501da177e4SLinus Torvalds container_of(head, struct neigh_parms, rcu_head); 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds neigh_parms_put(parms); 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) 14561da177e4SLinus Torvalds { 14571da177e4SLinus Torvalds if (!parms || parms == &tbl->parms) 14581da177e4SLinus Torvalds return; 14591da177e4SLinus Torvalds write_lock_bh(&tbl->lock); 146075fbfd33SNicolas Dichtel list_del(&parms->list); 14611da177e4SLinus Torvalds parms->dead = 1; 14621da177e4SLinus Torvalds write_unlock_bh(&tbl->lock); 1463cecbb639SDavid S. Miller if (parms->dev) 1464cecbb639SDavid S. Miller dev_put(parms->dev); 14651da177e4SLinus Torvalds call_rcu(&parms->rcu_head, neigh_rcu_free_parms); 14661da177e4SLinus Torvalds } 14670a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_parms_release); 14681da177e4SLinus Torvalds 146906f0511dSDenis V. Lunev static void neigh_parms_destroy(struct neigh_parms *parms) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds kfree(parms); 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 1474c2ecba71SPavel Emelianov static struct lock_class_key neigh_table_proxy_queue_class; 1475c2ecba71SPavel Emelianov 1476d7480fd3SWANG Cong static struct neigh_table *neigh_tables[NEIGH_NR_TABLES] __read_mostly; 1477d7480fd3SWANG Cong 1478d7480fd3SWANG Cong void neigh_table_init(int index, struct neigh_table *tbl) 14791da177e4SLinus Torvalds { 14801da177e4SLinus Torvalds unsigned long now = jiffies; 14811da177e4SLinus Torvalds unsigned long phsize; 14821da177e4SLinus Torvalds 148375fbfd33SNicolas Dichtel INIT_LIST_HEAD(&tbl->parms_list); 148475fbfd33SNicolas Dichtel list_add(&tbl->parms.list, &tbl->parms_list); 1485e42ea986SEric Dumazet write_pnet(&tbl->parms.net, &init_net); 14861da177e4SLinus Torvalds atomic_set(&tbl->parms.refcnt, 1); 14871da177e4SLinus Torvalds tbl->parms.reachable_time = 14881f9248e5SJiri Pirko neigh_rand_reach_time(NEIGH_VAR(&tbl->parms, BASE_REACHABLE_TIME)); 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds tbl->stats = alloc_percpu(struct neigh_statistics); 14911da177e4SLinus Torvalds if (!tbl->stats) 14921da177e4SLinus Torvalds panic("cannot create neighbour cache statistics"); 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 14959b739ba5SAlexey Dobriyan if (!proc_create_data(tbl->id, 0, init_net.proc_net_stat, 14969b739ba5SAlexey Dobriyan &neigh_stat_seq_fops, tbl)) 14971da177e4SLinus Torvalds panic("cannot create neighbour proc dir entry"); 14981da177e4SLinus Torvalds #endif 14991da177e4SLinus Torvalds 1500cd089336SDavid S. Miller RCU_INIT_POINTER(tbl->nht, neigh_hash_alloc(3)); 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); 150377d04bd9SAndrew Morton tbl->phash_buckets = kzalloc(phsize, GFP_KERNEL); 15041da177e4SLinus Torvalds 1505d6bf7817SEric Dumazet if (!tbl->nht || !tbl->phash_buckets) 15061da177e4SLinus Torvalds panic("cannot allocate neighbour cache hashes"); 15071da177e4SLinus Torvalds 150808433effSYOSHIFUJI Hideaki / 吉藤英明 if (!tbl->entry_size) 150908433effSYOSHIFUJI Hideaki / 吉藤英明 tbl->entry_size = ALIGN(offsetof(struct neighbour, primary_key) + 151008433effSYOSHIFUJI Hideaki / 吉藤英明 tbl->key_len, NEIGH_PRIV_ALIGN); 151108433effSYOSHIFUJI Hideaki / 吉藤英明 else 151208433effSYOSHIFUJI Hideaki / 吉藤英明 WARN_ON(tbl->entry_size % NEIGH_PRIV_ALIGN); 151308433effSYOSHIFUJI Hideaki / 吉藤英明 15141da177e4SLinus Torvalds rwlock_init(&tbl->lock); 1515203b42f7STejun Heo INIT_DEFERRABLE_WORK(&tbl->gc_work, neigh_periodic_work); 1516f618002bSviresh kumar queue_delayed_work(system_power_efficient_wq, &tbl->gc_work, 1517f618002bSviresh kumar tbl->parms.reachable_time); 1518b24b8a24SPavel Emelyanov setup_timer(&tbl->proxy_timer, neigh_proxy_process, (unsigned long)tbl); 1519c2ecba71SPavel Emelianov skb_queue_head_init_class(&tbl->proxy_queue, 1520c2ecba71SPavel Emelianov &neigh_table_proxy_queue_class); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds tbl->last_flush = now; 15231da177e4SLinus Torvalds tbl->last_rand = now + tbl->parms.reachable_time * 20; 1524bd89efc5SSimon Kelley 1525d7480fd3SWANG Cong neigh_tables[index] = tbl; 15261da177e4SLinus Torvalds } 15270a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_init); 15281da177e4SLinus Torvalds 1529d7480fd3SWANG Cong int neigh_table_clear(int index, struct neigh_table *tbl) 15301da177e4SLinus Torvalds { 1531d7480fd3SWANG Cong neigh_tables[index] = NULL; 15321da177e4SLinus Torvalds /* It is not clean... Fix it to unload IPv6 module safely */ 1533a5c30b34STejun Heo cancel_delayed_work_sync(&tbl->gc_work); 15341da177e4SLinus Torvalds del_timer_sync(&tbl->proxy_timer); 15351da177e4SLinus Torvalds pneigh_queue_purge(&tbl->proxy_queue); 15361da177e4SLinus Torvalds neigh_ifdown(tbl, NULL); 15371da177e4SLinus Torvalds if (atomic_read(&tbl->entries)) 1538e005d193SJoe Perches pr_crit("neighbour leakage\n"); 15391da177e4SLinus Torvalds 15406193d2beSEric Dumazet call_rcu(&rcu_dereference_protected(tbl->nht, 1)->rcu, 15416193d2beSEric Dumazet neigh_hash_free_rcu); 1542d6bf7817SEric Dumazet tbl->nht = NULL; 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds kfree(tbl->phash_buckets); 15451da177e4SLinus Torvalds tbl->phash_buckets = NULL; 15461da177e4SLinus Torvalds 15473f192b5cSAlexey Dobriyan remove_proc_entry(tbl->id, init_net.proc_net_stat); 15483f192b5cSAlexey Dobriyan 15493fcde74bSKirill Korotaev free_percpu(tbl->stats); 15503fcde74bSKirill Korotaev tbl->stats = NULL; 15513fcde74bSKirill Korotaev 15521da177e4SLinus Torvalds return 0; 15531da177e4SLinus Torvalds } 15540a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_table_clear); 15551da177e4SLinus Torvalds 1556d7480fd3SWANG Cong static struct neigh_table *neigh_find_table(int family) 1557d7480fd3SWANG Cong { 1558d7480fd3SWANG Cong struct neigh_table *tbl = NULL; 1559d7480fd3SWANG Cong 1560d7480fd3SWANG Cong switch (family) { 1561d7480fd3SWANG Cong case AF_INET: 1562d7480fd3SWANG Cong tbl = neigh_tables[NEIGH_ARP_TABLE]; 1563d7480fd3SWANG Cong break; 1564d7480fd3SWANG Cong case AF_INET6: 1565d7480fd3SWANG Cong tbl = neigh_tables[NEIGH_ND_TABLE]; 1566d7480fd3SWANG Cong break; 1567d7480fd3SWANG Cong case AF_DECnet: 1568d7480fd3SWANG Cong tbl = neigh_tables[NEIGH_DN_TABLE]; 1569d7480fd3SWANG Cong break; 1570d7480fd3SWANG Cong } 1571d7480fd3SWANG Cong 1572d7480fd3SWANG Cong return tbl; 1573d7480fd3SWANG Cong } 1574d7480fd3SWANG Cong 1575661d2967SThomas Graf static int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh) 15761da177e4SLinus Torvalds { 15773b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1578a14a49d2SThomas Graf struct ndmsg *ndm; 1579a14a49d2SThomas Graf struct nlattr *dst_attr; 15801da177e4SLinus Torvalds struct neigh_table *tbl; 1581d7480fd3SWANG Cong struct neighbour *neigh; 15821da177e4SLinus Torvalds struct net_device *dev = NULL; 1583a14a49d2SThomas Graf int err = -EINVAL; 15841da177e4SLinus Torvalds 1585110b2499SEric Dumazet ASSERT_RTNL(); 1586a14a49d2SThomas Graf if (nlmsg_len(nlh) < sizeof(*ndm)) 15871da177e4SLinus Torvalds goto out; 15881da177e4SLinus Torvalds 1589a14a49d2SThomas Graf dst_attr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_DST); 1590a14a49d2SThomas Graf if (dst_attr == NULL) 1591a14a49d2SThomas Graf goto out; 1592a14a49d2SThomas Graf 1593a14a49d2SThomas Graf ndm = nlmsg_data(nlh); 1594a14a49d2SThomas Graf if (ndm->ndm_ifindex) { 1595110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 1596a14a49d2SThomas Graf if (dev == NULL) { 1597a14a49d2SThomas Graf err = -ENODEV; 1598a14a49d2SThomas Graf goto out; 1599a14a49d2SThomas Graf } 1600a14a49d2SThomas Graf } 1601a14a49d2SThomas Graf 1602d7480fd3SWANG Cong tbl = neigh_find_table(ndm->ndm_family); 1603d7480fd3SWANG Cong if (tbl == NULL) 1604d7480fd3SWANG Cong return -EAFNOSUPPORT; 16051da177e4SLinus Torvalds 1606a14a49d2SThomas Graf if (nla_len(dst_attr) < tbl->key_len) 1607110b2499SEric Dumazet goto out; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 1610426b5303SEric W. Biederman err = pneigh_delete(tbl, net, nla_data(dst_attr), dev); 1611110b2499SEric Dumazet goto out; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 1614a14a49d2SThomas Graf if (dev == NULL) 1615110b2499SEric Dumazet goto out; 16161da177e4SLinus Torvalds 1617a14a49d2SThomas Graf neigh = neigh_lookup(tbl, nla_data(dst_attr), dev); 1618a14a49d2SThomas Graf if (neigh == NULL) { 1619a14a49d2SThomas Graf err = -ENOENT; 1620110b2499SEric Dumazet goto out; 1621a14a49d2SThomas Graf } 1622a14a49d2SThomas Graf 1623a14a49d2SThomas Graf err = neigh_update(neigh, NULL, NUD_FAILED, 16241da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | 16251da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1626a14a49d2SThomas Graf neigh_release(neigh); 1627a14a49d2SThomas Graf 16281da177e4SLinus Torvalds out: 16291da177e4SLinus Torvalds return err; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 1632661d2967SThomas Graf static int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh) 16331da177e4SLinus Torvalds { 1634d7480fd3SWANG Cong int flags = NEIGH_UPDATE_F_ADMIN | NEIGH_UPDATE_F_OVERRIDE; 16353b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 16365208debdSThomas Graf struct ndmsg *ndm; 16375208debdSThomas Graf struct nlattr *tb[NDA_MAX+1]; 16381da177e4SLinus Torvalds struct neigh_table *tbl; 16391da177e4SLinus Torvalds struct net_device *dev = NULL; 1640d7480fd3SWANG Cong struct neighbour *neigh; 1641d7480fd3SWANG Cong void *dst, *lladdr; 16425208debdSThomas Graf int err; 16431da177e4SLinus Torvalds 1644110b2499SEric Dumazet ASSERT_RTNL(); 16455208debdSThomas Graf err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 16465208debdSThomas Graf if (err < 0) 16471da177e4SLinus Torvalds goto out; 16481da177e4SLinus Torvalds 16495208debdSThomas Graf err = -EINVAL; 16505208debdSThomas Graf if (tb[NDA_DST] == NULL) 16515208debdSThomas Graf goto out; 16525208debdSThomas Graf 16535208debdSThomas Graf ndm = nlmsg_data(nlh); 16545208debdSThomas Graf if (ndm->ndm_ifindex) { 1655110b2499SEric Dumazet dev = __dev_get_by_index(net, ndm->ndm_ifindex); 16565208debdSThomas Graf if (dev == NULL) { 16575208debdSThomas Graf err = -ENODEV; 16585208debdSThomas Graf goto out; 16595208debdSThomas Graf } 16605208debdSThomas Graf 16615208debdSThomas Graf if (tb[NDA_LLADDR] && nla_len(tb[NDA_LLADDR]) < dev->addr_len) 1662110b2499SEric Dumazet goto out; 16635208debdSThomas Graf } 16645208debdSThomas Graf 1665d7480fd3SWANG Cong tbl = neigh_find_table(ndm->ndm_family); 1666d7480fd3SWANG Cong if (tbl == NULL) 1667d7480fd3SWANG Cong return -EAFNOSUPPORT; 16681da177e4SLinus Torvalds 16695208debdSThomas Graf if (nla_len(tb[NDA_DST]) < tbl->key_len) 1670110b2499SEric Dumazet goto out; 16715208debdSThomas Graf dst = nla_data(tb[NDA_DST]); 16725208debdSThomas Graf lladdr = tb[NDA_LLADDR] ? nla_data(tb[NDA_LLADDR]) : NULL; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds if (ndm->ndm_flags & NTF_PROXY) { 167562dd9318SVille Nuorvala struct pneigh_entry *pn; 167662dd9318SVille Nuorvala 16775208debdSThomas Graf err = -ENOBUFS; 1678426b5303SEric W. Biederman pn = pneigh_lookup(tbl, net, dst, dev, 1); 167962dd9318SVille Nuorvala if (pn) { 168062dd9318SVille Nuorvala pn->flags = ndm->ndm_flags; 168162dd9318SVille Nuorvala err = 0; 168262dd9318SVille Nuorvala } 1683110b2499SEric Dumazet goto out; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16865208debdSThomas Graf if (dev == NULL) 1687110b2499SEric Dumazet goto out; 16881da177e4SLinus Torvalds 16895208debdSThomas Graf neigh = neigh_lookup(tbl, dst, dev); 16905208debdSThomas Graf if (neigh == NULL) { 16915208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 16921da177e4SLinus Torvalds err = -ENOENT; 1693110b2499SEric Dumazet goto out; 16945208debdSThomas Graf } 16955208debdSThomas Graf 16965208debdSThomas Graf neigh = __neigh_lookup_errno(tbl, dst, dev); 16975208debdSThomas Graf if (IS_ERR(neigh)) { 16985208debdSThomas Graf err = PTR_ERR(neigh); 1699110b2499SEric Dumazet goto out; 17001da177e4SLinus Torvalds } 17015208debdSThomas Graf } else { 17025208debdSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL) { 17035208debdSThomas Graf err = -EEXIST; 17045208debdSThomas Graf neigh_release(neigh); 1705110b2499SEric Dumazet goto out; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 17085208debdSThomas Graf if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) 17095208debdSThomas Graf flags &= ~NEIGH_UPDATE_F_OVERRIDE; 17105208debdSThomas Graf } 17111da177e4SLinus Torvalds 17120c5c2d30SEric Biederman if (ndm->ndm_flags & NTF_USE) { 17130c5c2d30SEric Biederman neigh_event_send(neigh, NULL); 17140c5c2d30SEric Biederman err = 0; 17150c5c2d30SEric Biederman } else 17165208debdSThomas Graf err = neigh_update(neigh, lladdr, ndm->ndm_state, flags); 17175208debdSThomas Graf neigh_release(neigh); 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds out: 17201da177e4SLinus Torvalds return err; 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 1723c7fb64dbSThomas Graf static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) 1724c7fb64dbSThomas Graf { 1725ca860fb3SThomas Graf struct nlattr *nest; 1726e386c6ebSThomas Graf 1727ca860fb3SThomas Graf nest = nla_nest_start(skb, NDTA_PARMS); 1728ca860fb3SThomas Graf if (nest == NULL) 1729ca860fb3SThomas Graf return -ENOBUFS; 1730c7fb64dbSThomas Graf 17319a6308d7SDavid S. Miller if ((parms->dev && 17329a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_IFINDEX, parms->dev->ifindex)) || 17339a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)) || 17341f9248e5SJiri Pirko nla_put_u32(skb, NDTPA_QUEUE_LENBYTES, 17351f9248e5SJiri Pirko NEIGH_VAR(parms, QUEUE_LEN_BYTES)) || 17368b5c171bSEric Dumazet /* approximative value for deprecated QUEUE_LEN (in packets) */ 17379a6308d7SDavid S. Miller nla_put_u32(skb, NDTPA_QUEUE_LEN, 17381f9248e5SJiri Pirko NEIGH_VAR(parms, QUEUE_LEN_BYTES) / SKB_TRUESIZE(ETH_FRAME_LEN)) || 17391f9248e5SJiri Pirko nla_put_u32(skb, NDTPA_PROXY_QLEN, NEIGH_VAR(parms, PROXY_QLEN)) || 17401f9248e5SJiri Pirko nla_put_u32(skb, NDTPA_APP_PROBES, NEIGH_VAR(parms, APP_PROBES)) || 17411f9248e5SJiri Pirko nla_put_u32(skb, NDTPA_UCAST_PROBES, 17421f9248e5SJiri Pirko NEIGH_VAR(parms, UCAST_PROBES)) || 17431f9248e5SJiri Pirko nla_put_u32(skb, NDTPA_MCAST_PROBES, 17441f9248e5SJiri Pirko NEIGH_VAR(parms, MCAST_PROBES)) || 17459a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) || 17469a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME, 17471f9248e5SJiri Pirko NEIGH_VAR(parms, BASE_REACHABLE_TIME)) || 17481f9248e5SJiri Pirko nla_put_msecs(skb, NDTPA_GC_STALETIME, 17491f9248e5SJiri Pirko NEIGH_VAR(parms, GC_STALETIME)) || 17509a6308d7SDavid S. Miller nla_put_msecs(skb, NDTPA_DELAY_PROBE_TIME, 17511f9248e5SJiri Pirko NEIGH_VAR(parms, DELAY_PROBE_TIME)) || 17521f9248e5SJiri Pirko nla_put_msecs(skb, NDTPA_RETRANS_TIME, 17531f9248e5SJiri Pirko NEIGH_VAR(parms, RETRANS_TIME)) || 17541f9248e5SJiri Pirko nla_put_msecs(skb, NDTPA_ANYCAST_DELAY, 17551f9248e5SJiri Pirko NEIGH_VAR(parms, ANYCAST_DELAY)) || 17561f9248e5SJiri Pirko nla_put_msecs(skb, NDTPA_PROXY_DELAY, 17571f9248e5SJiri Pirko NEIGH_VAR(parms, PROXY_DELAY)) || 17581f9248e5SJiri Pirko nla_put_msecs(skb, NDTPA_LOCKTIME, 17591f9248e5SJiri Pirko NEIGH_VAR(parms, LOCKTIME))) 17609a6308d7SDavid S. Miller goto nla_put_failure; 1761ca860fb3SThomas Graf return nla_nest_end(skb, nest); 1762c7fb64dbSThomas Graf 1763ca860fb3SThomas Graf nla_put_failure: 1764bc3ed28cSThomas Graf nla_nest_cancel(skb, nest); 1765bc3ed28cSThomas Graf return -EMSGSIZE; 1766c7fb64dbSThomas Graf } 1767c7fb64dbSThomas Graf 1768ca860fb3SThomas Graf static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl, 1769ca860fb3SThomas Graf u32 pid, u32 seq, int type, int flags) 1770c7fb64dbSThomas Graf { 1771c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1772c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1773c7fb64dbSThomas Graf 1774ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1775ca860fb3SThomas Graf if (nlh == NULL) 177626932566SPatrick McHardy return -EMSGSIZE; 1777c7fb64dbSThomas Graf 1778ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1779c7fb64dbSThomas Graf 1780c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1781c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 17829ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 17839ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1784c7fb64dbSThomas Graf 17859a6308d7SDavid S. Miller if (nla_put_string(skb, NDTA_NAME, tbl->id) || 17869a6308d7SDavid S. Miller nla_put_msecs(skb, NDTA_GC_INTERVAL, tbl->gc_interval) || 17879a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH1, tbl->gc_thresh1) || 17889a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH2, tbl->gc_thresh2) || 17899a6308d7SDavid S. Miller nla_put_u32(skb, NDTA_THRESH3, tbl->gc_thresh3)) 17909a6308d7SDavid S. Miller goto nla_put_failure; 1791c7fb64dbSThomas Graf { 1792c7fb64dbSThomas Graf unsigned long now = jiffies; 1793c7fb64dbSThomas Graf unsigned int flush_delta = now - tbl->last_flush; 1794c7fb64dbSThomas Graf unsigned int rand_delta = now - tbl->last_rand; 1795d6bf7817SEric Dumazet struct neigh_hash_table *nht; 1796c7fb64dbSThomas Graf struct ndt_config ndc = { 1797c7fb64dbSThomas Graf .ndtc_key_len = tbl->key_len, 1798c7fb64dbSThomas Graf .ndtc_entry_size = tbl->entry_size, 1799c7fb64dbSThomas Graf .ndtc_entries = atomic_read(&tbl->entries), 1800c7fb64dbSThomas Graf .ndtc_last_flush = jiffies_to_msecs(flush_delta), 1801c7fb64dbSThomas Graf .ndtc_last_rand = jiffies_to_msecs(rand_delta), 1802c7fb64dbSThomas Graf .ndtc_proxy_qlen = tbl->proxy_queue.qlen, 1803c7fb64dbSThomas Graf }; 1804c7fb64dbSThomas Graf 1805d6bf7817SEric Dumazet rcu_read_lock_bh(); 1806d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 18072c2aba6cSDavid S. Miller ndc.ndtc_hash_rnd = nht->hash_rnd[0]; 1808cd089336SDavid S. Miller ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1); 1809d6bf7817SEric Dumazet rcu_read_unlock_bh(); 1810d6bf7817SEric Dumazet 18119a6308d7SDavid S. Miller if (nla_put(skb, NDTA_CONFIG, sizeof(ndc), &ndc)) 18129a6308d7SDavid S. Miller goto nla_put_failure; 1813c7fb64dbSThomas Graf } 1814c7fb64dbSThomas Graf 1815c7fb64dbSThomas Graf { 1816c7fb64dbSThomas Graf int cpu; 1817c7fb64dbSThomas Graf struct ndt_stats ndst; 1818c7fb64dbSThomas Graf 1819c7fb64dbSThomas Graf memset(&ndst, 0, sizeof(ndst)); 1820c7fb64dbSThomas Graf 18216f912042SKAMEZAWA Hiroyuki for_each_possible_cpu(cpu) { 1822c7fb64dbSThomas Graf struct neigh_statistics *st; 1823c7fb64dbSThomas Graf 1824c7fb64dbSThomas Graf st = per_cpu_ptr(tbl->stats, cpu); 1825c7fb64dbSThomas Graf ndst.ndts_allocs += st->allocs; 1826c7fb64dbSThomas Graf ndst.ndts_destroys += st->destroys; 1827c7fb64dbSThomas Graf ndst.ndts_hash_grows += st->hash_grows; 1828c7fb64dbSThomas Graf ndst.ndts_res_failed += st->res_failed; 1829c7fb64dbSThomas Graf ndst.ndts_lookups += st->lookups; 1830c7fb64dbSThomas Graf ndst.ndts_hits += st->hits; 1831c7fb64dbSThomas Graf ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; 1832c7fb64dbSThomas Graf ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; 1833c7fb64dbSThomas Graf ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; 1834c7fb64dbSThomas Graf ndst.ndts_forced_gc_runs += st->forced_gc_runs; 1835c7fb64dbSThomas Graf } 1836c7fb64dbSThomas Graf 18379a6308d7SDavid S. Miller if (nla_put(skb, NDTA_STATS, sizeof(ndst), &ndst)) 18389a6308d7SDavid S. Miller goto nla_put_failure; 1839c7fb64dbSThomas Graf } 1840c7fb64dbSThomas Graf 1841c7fb64dbSThomas Graf BUG_ON(tbl->parms.dev); 1842c7fb64dbSThomas Graf if (neightbl_fill_parms(skb, &tbl->parms) < 0) 1843ca860fb3SThomas Graf goto nla_put_failure; 1844c7fb64dbSThomas Graf 1845c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1846053c095aSJohannes Berg nlmsg_end(skb, nlh); 1847053c095aSJohannes Berg return 0; 1848c7fb64dbSThomas Graf 1849ca860fb3SThomas Graf nla_put_failure: 1850c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 185126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 185226932566SPatrick McHardy return -EMSGSIZE; 1853c7fb64dbSThomas Graf } 1854c7fb64dbSThomas Graf 1855ca860fb3SThomas Graf static int neightbl_fill_param_info(struct sk_buff *skb, 1856ca860fb3SThomas Graf struct neigh_table *tbl, 1857c7fb64dbSThomas Graf struct neigh_parms *parms, 1858ca860fb3SThomas Graf u32 pid, u32 seq, int type, 1859ca860fb3SThomas Graf unsigned int flags) 1860c7fb64dbSThomas Graf { 1861c7fb64dbSThomas Graf struct ndtmsg *ndtmsg; 1862c7fb64dbSThomas Graf struct nlmsghdr *nlh; 1863c7fb64dbSThomas Graf 1864ca860fb3SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndtmsg), flags); 1865ca860fb3SThomas Graf if (nlh == NULL) 186626932566SPatrick McHardy return -EMSGSIZE; 1867c7fb64dbSThomas Graf 1868ca860fb3SThomas Graf ndtmsg = nlmsg_data(nlh); 1869c7fb64dbSThomas Graf 1870c7fb64dbSThomas Graf read_lock_bh(&tbl->lock); 1871c7fb64dbSThomas Graf ndtmsg->ndtm_family = tbl->family; 18729ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad1 = 0; 18739ef1d4c7SPatrick McHardy ndtmsg->ndtm_pad2 = 0; 1874c7fb64dbSThomas Graf 1875ca860fb3SThomas Graf if (nla_put_string(skb, NDTA_NAME, tbl->id) < 0 || 1876ca860fb3SThomas Graf neightbl_fill_parms(skb, parms) < 0) 1877ca860fb3SThomas Graf goto errout; 1878c7fb64dbSThomas Graf 1879c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 1880053c095aSJohannes Berg nlmsg_end(skb, nlh); 1881053c095aSJohannes Berg return 0; 1882ca860fb3SThomas Graf errout: 1883c7fb64dbSThomas Graf read_unlock_bh(&tbl->lock); 188426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 188526932566SPatrick McHardy return -EMSGSIZE; 1886c7fb64dbSThomas Graf } 1887c7fb64dbSThomas Graf 1888ef7c79edSPatrick McHardy static const struct nla_policy nl_neightbl_policy[NDTA_MAX+1] = { 18896b3f8674SThomas Graf [NDTA_NAME] = { .type = NLA_STRING }, 18906b3f8674SThomas Graf [NDTA_THRESH1] = { .type = NLA_U32 }, 18916b3f8674SThomas Graf [NDTA_THRESH2] = { .type = NLA_U32 }, 18926b3f8674SThomas Graf [NDTA_THRESH3] = { .type = NLA_U32 }, 18936b3f8674SThomas Graf [NDTA_GC_INTERVAL] = { .type = NLA_U64 }, 18946b3f8674SThomas Graf [NDTA_PARMS] = { .type = NLA_NESTED }, 18956b3f8674SThomas Graf }; 18966b3f8674SThomas Graf 1897ef7c79edSPatrick McHardy static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = { 18986b3f8674SThomas Graf [NDTPA_IFINDEX] = { .type = NLA_U32 }, 18996b3f8674SThomas Graf [NDTPA_QUEUE_LEN] = { .type = NLA_U32 }, 19006b3f8674SThomas Graf [NDTPA_PROXY_QLEN] = { .type = NLA_U32 }, 19016b3f8674SThomas Graf [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 19026b3f8674SThomas Graf [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 19036b3f8674SThomas Graf [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 19046b3f8674SThomas Graf [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 19056b3f8674SThomas Graf [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 19066b3f8674SThomas Graf [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 19076b3f8674SThomas Graf [NDTPA_RETRANS_TIME] = { .type = NLA_U64 }, 19086b3f8674SThomas Graf [NDTPA_ANYCAST_DELAY] = { .type = NLA_U64 }, 19096b3f8674SThomas Graf [NDTPA_PROXY_DELAY] = { .type = NLA_U64 }, 19106b3f8674SThomas Graf [NDTPA_LOCKTIME] = { .type = NLA_U64 }, 19116b3f8674SThomas Graf }; 19126b3f8674SThomas Graf 1913661d2967SThomas Graf static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh) 1914c7fb64dbSThomas Graf { 19153b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1916c7fb64dbSThomas Graf struct neigh_table *tbl; 19176b3f8674SThomas Graf struct ndtmsg *ndtmsg; 19186b3f8674SThomas Graf struct nlattr *tb[NDTA_MAX+1]; 1919d7480fd3SWANG Cong bool found = false; 1920d7480fd3SWANG Cong int err, tidx; 1921c7fb64dbSThomas Graf 19226b3f8674SThomas Graf err = nlmsg_parse(nlh, sizeof(*ndtmsg), tb, NDTA_MAX, 19236b3f8674SThomas Graf nl_neightbl_policy); 19246b3f8674SThomas Graf if (err < 0) 19256b3f8674SThomas Graf goto errout; 1926c7fb64dbSThomas Graf 19276b3f8674SThomas Graf if (tb[NDTA_NAME] == NULL) { 19286b3f8674SThomas Graf err = -EINVAL; 19296b3f8674SThomas Graf goto errout; 19306b3f8674SThomas Graf } 19316b3f8674SThomas Graf 19326b3f8674SThomas Graf ndtmsg = nlmsg_data(nlh); 1933d7480fd3SWANG Cong 1934d7480fd3SWANG Cong for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) { 1935d7480fd3SWANG Cong tbl = neigh_tables[tidx]; 1936d7480fd3SWANG Cong if (!tbl) 1937d7480fd3SWANG Cong continue; 1938c7fb64dbSThomas Graf if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) 1939c7fb64dbSThomas Graf continue; 1940d7480fd3SWANG Cong if (nla_strcmp(tb[NDTA_NAME], tbl->id) == 0) { 1941d7480fd3SWANG Cong found = true; 1942c7fb64dbSThomas Graf break; 1943c7fb64dbSThomas Graf } 1944c7fb64dbSThomas Graf } 1945c7fb64dbSThomas Graf 1946d7480fd3SWANG Cong if (!found) 1947d7480fd3SWANG Cong return -ENOENT; 1948d7480fd3SWANG Cong 1949c7fb64dbSThomas Graf /* 1950c7fb64dbSThomas Graf * We acquire tbl->lock to be nice to the periodic timers and 1951c7fb64dbSThomas Graf * make sure they always see a consistent set of values. 1952c7fb64dbSThomas Graf */ 1953c7fb64dbSThomas Graf write_lock_bh(&tbl->lock); 1954c7fb64dbSThomas Graf 19556b3f8674SThomas Graf if (tb[NDTA_PARMS]) { 19566b3f8674SThomas Graf struct nlattr *tbp[NDTPA_MAX+1]; 1957c7fb64dbSThomas Graf struct neigh_parms *p; 19586b3f8674SThomas Graf int i, ifindex = 0; 1959c7fb64dbSThomas Graf 19606b3f8674SThomas Graf err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], 19616b3f8674SThomas Graf nl_ntbl_parm_policy); 19626b3f8674SThomas Graf if (err < 0) 19636b3f8674SThomas Graf goto errout_tbl_lock; 1964c7fb64dbSThomas Graf 19656b3f8674SThomas Graf if (tbp[NDTPA_IFINDEX]) 19666b3f8674SThomas Graf ifindex = nla_get_u32(tbp[NDTPA_IFINDEX]); 1967c7fb64dbSThomas Graf 196897fd5bc7STobias Klauser p = lookup_neigh_parms(tbl, net, ifindex); 1969c7fb64dbSThomas Graf if (p == NULL) { 1970c7fb64dbSThomas Graf err = -ENOENT; 19716b3f8674SThomas Graf goto errout_tbl_lock; 1972c7fb64dbSThomas Graf } 1973c7fb64dbSThomas Graf 19746b3f8674SThomas Graf for (i = 1; i <= NDTPA_MAX; i++) { 19756b3f8674SThomas Graf if (tbp[i] == NULL) 19766b3f8674SThomas Graf continue; 1977c7fb64dbSThomas Graf 19786b3f8674SThomas Graf switch (i) { 19796b3f8674SThomas Graf case NDTPA_QUEUE_LEN: 19801f9248e5SJiri Pirko NEIGH_VAR_SET(p, QUEUE_LEN_BYTES, 19811f9248e5SJiri Pirko nla_get_u32(tbp[i]) * 19821f9248e5SJiri Pirko SKB_TRUESIZE(ETH_FRAME_LEN)); 19838b5c171bSEric Dumazet break; 19848b5c171bSEric Dumazet case NDTPA_QUEUE_LENBYTES: 19851f9248e5SJiri Pirko NEIGH_VAR_SET(p, QUEUE_LEN_BYTES, 19861f9248e5SJiri Pirko nla_get_u32(tbp[i])); 19876b3f8674SThomas Graf break; 19886b3f8674SThomas Graf case NDTPA_PROXY_QLEN: 19891f9248e5SJiri Pirko NEIGH_VAR_SET(p, PROXY_QLEN, 19901f9248e5SJiri Pirko nla_get_u32(tbp[i])); 19916b3f8674SThomas Graf break; 19926b3f8674SThomas Graf case NDTPA_APP_PROBES: 19931f9248e5SJiri Pirko NEIGH_VAR_SET(p, APP_PROBES, 19941f9248e5SJiri Pirko nla_get_u32(tbp[i])); 19956b3f8674SThomas Graf break; 19966b3f8674SThomas Graf case NDTPA_UCAST_PROBES: 19971f9248e5SJiri Pirko NEIGH_VAR_SET(p, UCAST_PROBES, 19981f9248e5SJiri Pirko nla_get_u32(tbp[i])); 19996b3f8674SThomas Graf break; 20006b3f8674SThomas Graf case NDTPA_MCAST_PROBES: 20011f9248e5SJiri Pirko NEIGH_VAR_SET(p, MCAST_PROBES, 20021f9248e5SJiri Pirko nla_get_u32(tbp[i])); 20036b3f8674SThomas Graf break; 20046b3f8674SThomas Graf case NDTPA_BASE_REACHABLE_TIME: 20051f9248e5SJiri Pirko NEIGH_VAR_SET(p, BASE_REACHABLE_TIME, 20061f9248e5SJiri Pirko nla_get_msecs(tbp[i])); 20074bf6980dSJean-Francois Remy /* update reachable_time as well, otherwise, the change will 20084bf6980dSJean-Francois Remy * only be effective after the next time neigh_periodic_work 20094bf6980dSJean-Francois Remy * decides to recompute it (can be multiple minutes) 20104bf6980dSJean-Francois Remy */ 20114bf6980dSJean-Francois Remy p->reachable_time = 20124bf6980dSJean-Francois Remy neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); 20136b3f8674SThomas Graf break; 20146b3f8674SThomas Graf case NDTPA_GC_STALETIME: 20151f9248e5SJiri Pirko NEIGH_VAR_SET(p, GC_STALETIME, 20161f9248e5SJiri Pirko nla_get_msecs(tbp[i])); 20176b3f8674SThomas Graf break; 20186b3f8674SThomas Graf case NDTPA_DELAY_PROBE_TIME: 20191f9248e5SJiri Pirko NEIGH_VAR_SET(p, DELAY_PROBE_TIME, 20201f9248e5SJiri Pirko nla_get_msecs(tbp[i])); 20216b3f8674SThomas Graf break; 20226b3f8674SThomas Graf case NDTPA_RETRANS_TIME: 20231f9248e5SJiri Pirko NEIGH_VAR_SET(p, RETRANS_TIME, 20241f9248e5SJiri Pirko nla_get_msecs(tbp[i])); 20256b3f8674SThomas Graf break; 20266b3f8674SThomas Graf case NDTPA_ANYCAST_DELAY: 20273977458cSJiri Pirko NEIGH_VAR_SET(p, ANYCAST_DELAY, 20283977458cSJiri Pirko nla_get_msecs(tbp[i])); 20296b3f8674SThomas Graf break; 20306b3f8674SThomas Graf case NDTPA_PROXY_DELAY: 20313977458cSJiri Pirko NEIGH_VAR_SET(p, PROXY_DELAY, 20323977458cSJiri Pirko nla_get_msecs(tbp[i])); 20336b3f8674SThomas Graf break; 20346b3f8674SThomas Graf case NDTPA_LOCKTIME: 20353977458cSJiri Pirko NEIGH_VAR_SET(p, LOCKTIME, 20363977458cSJiri Pirko nla_get_msecs(tbp[i])); 20376b3f8674SThomas Graf break; 2038c7fb64dbSThomas Graf } 20396b3f8674SThomas Graf } 20406b3f8674SThomas Graf } 20416b3f8674SThomas Graf 2042dc25c676SGao feng err = -ENOENT; 2043dc25c676SGao feng if ((tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || 2044dc25c676SGao feng tb[NDTA_THRESH3] || tb[NDTA_GC_INTERVAL]) && 2045dc25c676SGao feng !net_eq(net, &init_net)) 2046dc25c676SGao feng goto errout_tbl_lock; 2047dc25c676SGao feng 20486b3f8674SThomas Graf if (tb[NDTA_THRESH1]) 20496b3f8674SThomas Graf tbl->gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]); 20506b3f8674SThomas Graf 20516b3f8674SThomas Graf if (tb[NDTA_THRESH2]) 20526b3f8674SThomas Graf tbl->gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]); 20536b3f8674SThomas Graf 20546b3f8674SThomas Graf if (tb[NDTA_THRESH3]) 20556b3f8674SThomas Graf tbl->gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]); 20566b3f8674SThomas Graf 20576b3f8674SThomas Graf if (tb[NDTA_GC_INTERVAL]) 20586b3f8674SThomas Graf tbl->gc_interval = nla_get_msecs(tb[NDTA_GC_INTERVAL]); 2059c7fb64dbSThomas Graf 2060c7fb64dbSThomas Graf err = 0; 2061c7fb64dbSThomas Graf 20626b3f8674SThomas Graf errout_tbl_lock: 2063c7fb64dbSThomas Graf write_unlock_bh(&tbl->lock); 20646b3f8674SThomas Graf errout: 2065c7fb64dbSThomas Graf return err; 2066c7fb64dbSThomas Graf } 2067c7fb64dbSThomas Graf 2068c8822a4eSThomas Graf static int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 2069c7fb64dbSThomas Graf { 20703b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2071ca860fb3SThomas Graf int family, tidx, nidx = 0; 2072ca860fb3SThomas Graf int tbl_skip = cb->args[0]; 2073ca860fb3SThomas Graf int neigh_skip = cb->args[1]; 2074c7fb64dbSThomas Graf struct neigh_table *tbl; 2075c7fb64dbSThomas Graf 2076ca860fb3SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 2077c7fb64dbSThomas Graf 2078d7480fd3SWANG Cong for (tidx = 0; tidx < NEIGH_NR_TABLES; tidx++) { 2079c7fb64dbSThomas Graf struct neigh_parms *p; 2080c7fb64dbSThomas Graf 2081d7480fd3SWANG Cong tbl = neigh_tables[tidx]; 2082d7480fd3SWANG Cong if (!tbl) 2083d7480fd3SWANG Cong continue; 2084d7480fd3SWANG Cong 2085ca860fb3SThomas Graf if (tidx < tbl_skip || (family && tbl->family != family)) 2086c7fb64dbSThomas Graf continue; 2087c7fb64dbSThomas Graf 208815e47304SEric W. Biederman if (neightbl_fill_info(skb, tbl, NETLINK_CB(cb->skb).portid, 2089ca860fb3SThomas Graf cb->nlh->nlmsg_seq, RTM_NEWNEIGHTBL, 20907b46a644SDavid S. Miller NLM_F_MULTI) < 0) 2091c7fb64dbSThomas Graf break; 2092c7fb64dbSThomas Graf 209375fbfd33SNicolas Dichtel nidx = 0; 209475fbfd33SNicolas Dichtel p = list_next_entry(&tbl->parms, list); 209575fbfd33SNicolas Dichtel list_for_each_entry_from(p, &tbl->parms_list, list) { 2096878628fbSYOSHIFUJI Hideaki if (!net_eq(neigh_parms_net(p), net)) 2097426b5303SEric W. Biederman continue; 2098426b5303SEric W. Biederman 2099efc683fcSGautam Kachroo if (nidx < neigh_skip) 2100efc683fcSGautam Kachroo goto next; 2101c7fb64dbSThomas Graf 2102ca860fb3SThomas Graf if (neightbl_fill_param_info(skb, tbl, p, 210315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2104ca860fb3SThomas Graf cb->nlh->nlmsg_seq, 2105ca860fb3SThomas Graf RTM_NEWNEIGHTBL, 21067b46a644SDavid S. Miller NLM_F_MULTI) < 0) 2107c7fb64dbSThomas Graf goto out; 2108efc683fcSGautam Kachroo next: 2109efc683fcSGautam Kachroo nidx++; 2110c7fb64dbSThomas Graf } 2111c7fb64dbSThomas Graf 2112ca860fb3SThomas Graf neigh_skip = 0; 2113c7fb64dbSThomas Graf } 2114c7fb64dbSThomas Graf out: 2115ca860fb3SThomas Graf cb->args[0] = tidx; 2116ca860fb3SThomas Graf cb->args[1] = nidx; 2117c7fb64dbSThomas Graf 2118c7fb64dbSThomas Graf return skb->len; 2119c7fb64dbSThomas Graf } 21201da177e4SLinus Torvalds 21218b8aec50SThomas Graf static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh, 21228b8aec50SThomas Graf u32 pid, u32 seq, int type, unsigned int flags) 21231da177e4SLinus Torvalds { 21241da177e4SLinus Torvalds unsigned long now = jiffies; 21251da177e4SLinus Torvalds struct nda_cacheinfo ci; 21268b8aec50SThomas Graf struct nlmsghdr *nlh; 21278b8aec50SThomas Graf struct ndmsg *ndm; 21281da177e4SLinus Torvalds 21298b8aec50SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 21308b8aec50SThomas Graf if (nlh == NULL) 213126932566SPatrick McHardy return -EMSGSIZE; 21328b8aec50SThomas Graf 21338b8aec50SThomas Graf ndm = nlmsg_data(nlh); 21348b8aec50SThomas Graf ndm->ndm_family = neigh->ops->family; 21359ef1d4c7SPatrick McHardy ndm->ndm_pad1 = 0; 21369ef1d4c7SPatrick McHardy ndm->ndm_pad2 = 0; 21378b8aec50SThomas Graf ndm->ndm_flags = neigh->flags; 21388b8aec50SThomas Graf ndm->ndm_type = neigh->type; 21398b8aec50SThomas Graf ndm->ndm_ifindex = neigh->dev->ifindex; 21401da177e4SLinus Torvalds 21419a6308d7SDavid S. Miller if (nla_put(skb, NDA_DST, neigh->tbl->key_len, neigh->primary_key)) 21429a6308d7SDavid S. Miller goto nla_put_failure; 21438b8aec50SThomas Graf 21448b8aec50SThomas Graf read_lock_bh(&neigh->lock); 21458b8aec50SThomas Graf ndm->ndm_state = neigh->nud_state; 21460ed8ddf4SEric Dumazet if (neigh->nud_state & NUD_VALID) { 21470ed8ddf4SEric Dumazet char haddr[MAX_ADDR_LEN]; 21480ed8ddf4SEric Dumazet 21490ed8ddf4SEric Dumazet neigh_ha_snapshot(haddr, neigh, neigh->dev); 21500ed8ddf4SEric Dumazet if (nla_put(skb, NDA_LLADDR, neigh->dev->addr_len, haddr) < 0) { 21518b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21528b8aec50SThomas Graf goto nla_put_failure; 21538b8aec50SThomas Graf } 21540ed8ddf4SEric Dumazet } 21558b8aec50SThomas Graf 2156b9f5f52cSStephen Hemminger ci.ndm_used = jiffies_to_clock_t(now - neigh->used); 2157b9f5f52cSStephen Hemminger ci.ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); 2158b9f5f52cSStephen Hemminger ci.ndm_updated = jiffies_to_clock_t(now - neigh->updated); 21598b8aec50SThomas Graf ci.ndm_refcnt = atomic_read(&neigh->refcnt) - 1; 21608b8aec50SThomas Graf read_unlock_bh(&neigh->lock); 21618b8aec50SThomas Graf 21629a6308d7SDavid S. Miller if (nla_put_u32(skb, NDA_PROBES, atomic_read(&neigh->probes)) || 21639a6308d7SDavid S. Miller nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) 21649a6308d7SDavid S. Miller goto nla_put_failure; 21658b8aec50SThomas Graf 2166053c095aSJohannes Berg nlmsg_end(skb, nlh); 2167053c095aSJohannes Berg return 0; 21688b8aec50SThomas Graf 21698b8aec50SThomas Graf nla_put_failure: 217026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 217126932566SPatrick McHardy return -EMSGSIZE; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 217484920c14STony Zelenoff static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn, 217584920c14STony Zelenoff u32 pid, u32 seq, int type, unsigned int flags, 217684920c14STony Zelenoff struct neigh_table *tbl) 217784920c14STony Zelenoff { 217884920c14STony Zelenoff struct nlmsghdr *nlh; 217984920c14STony Zelenoff struct ndmsg *ndm; 218084920c14STony Zelenoff 218184920c14STony Zelenoff nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags); 218284920c14STony Zelenoff if (nlh == NULL) 218384920c14STony Zelenoff return -EMSGSIZE; 218484920c14STony Zelenoff 218584920c14STony Zelenoff ndm = nlmsg_data(nlh); 218684920c14STony Zelenoff ndm->ndm_family = tbl->family; 218784920c14STony Zelenoff ndm->ndm_pad1 = 0; 218884920c14STony Zelenoff ndm->ndm_pad2 = 0; 218984920c14STony Zelenoff ndm->ndm_flags = pn->flags | NTF_PROXY; 2190545469f7SJun Zhao ndm->ndm_type = RTN_UNICAST; 219184920c14STony Zelenoff ndm->ndm_ifindex = pn->dev->ifindex; 219284920c14STony Zelenoff ndm->ndm_state = NUD_NONE; 219384920c14STony Zelenoff 21949a6308d7SDavid S. Miller if (nla_put(skb, NDA_DST, tbl->key_len, pn->key)) 21959a6308d7SDavid S. Miller goto nla_put_failure; 219684920c14STony Zelenoff 2197053c095aSJohannes Berg nlmsg_end(skb, nlh); 2198053c095aSJohannes Berg return 0; 219984920c14STony Zelenoff 220084920c14STony Zelenoff nla_put_failure: 220184920c14STony Zelenoff nlmsg_cancel(skb, nlh); 220284920c14STony Zelenoff return -EMSGSIZE; 220384920c14STony Zelenoff } 220484920c14STony Zelenoff 2205d961db35SThomas Graf static void neigh_update_notify(struct neighbour *neigh) 2206d961db35SThomas Graf { 2207d961db35SThomas Graf call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, neigh); 2208d961db35SThomas Graf __neigh_notify(neigh, RTM_NEWNEIGH, 0); 2209d961db35SThomas Graf } 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 22121da177e4SLinus Torvalds struct netlink_callback *cb) 22131da177e4SLinus Torvalds { 22143b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 22151da177e4SLinus Torvalds struct neighbour *n; 22161da177e4SLinus Torvalds int rc, h, s_h = cb->args[1]; 22171da177e4SLinus Torvalds int idx, s_idx = idx = cb->args[2]; 2218d6bf7817SEric Dumazet struct neigh_hash_table *nht; 22191da177e4SLinus Torvalds 2220d6bf7817SEric Dumazet rcu_read_lock_bh(); 2221d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2222d6bf7817SEric Dumazet 22234bd6683bSEric Dumazet for (h = s_h; h < (1 << nht->hash_shift); h++) { 22241da177e4SLinus Torvalds if (h > s_h) 22251da177e4SLinus Torvalds s_idx = 0; 2226767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[h]), idx = 0; 2227767e97e1SEric Dumazet n != NULL; 2228767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) { 222909ad9bc7SOctavian Purdila if (!net_eq(dev_net(n->dev), net)) 2230426b5303SEric W. Biederman continue; 2231efc683fcSGautam Kachroo if (idx < s_idx) 2232efc683fcSGautam Kachroo goto next; 223315e47304SEric W. Biederman if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, 22341da177e4SLinus Torvalds cb->nlh->nlmsg_seq, 2235b6544c0bSJamal Hadi Salim RTM_NEWNEIGH, 22367b46a644SDavid S. Miller NLM_F_MULTI) < 0) { 22371da177e4SLinus Torvalds rc = -1; 22381da177e4SLinus Torvalds goto out; 22391da177e4SLinus Torvalds } 2240efc683fcSGautam Kachroo next: 2241efc683fcSGautam Kachroo idx++; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds rc = skb->len; 22451da177e4SLinus Torvalds out: 2246d6bf7817SEric Dumazet rcu_read_unlock_bh(); 22471da177e4SLinus Torvalds cb->args[1] = h; 22481da177e4SLinus Torvalds cb->args[2] = idx; 22491da177e4SLinus Torvalds return rc; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds 225284920c14STony Zelenoff static int pneigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, 225384920c14STony Zelenoff struct netlink_callback *cb) 225484920c14STony Zelenoff { 225584920c14STony Zelenoff struct pneigh_entry *n; 225684920c14STony Zelenoff struct net *net = sock_net(skb->sk); 225784920c14STony Zelenoff int rc, h, s_h = cb->args[3]; 225884920c14STony Zelenoff int idx, s_idx = idx = cb->args[4]; 225984920c14STony Zelenoff 226084920c14STony Zelenoff read_lock_bh(&tbl->lock); 226184920c14STony Zelenoff 22624bd6683bSEric Dumazet for (h = s_h; h <= PNEIGH_HASHMASK; h++) { 226384920c14STony Zelenoff if (h > s_h) 226484920c14STony Zelenoff s_idx = 0; 226584920c14STony Zelenoff for (n = tbl->phash_buckets[h], idx = 0; n; n = n->next) { 226684920c14STony Zelenoff if (dev_net(n->dev) != net) 226784920c14STony Zelenoff continue; 226884920c14STony Zelenoff if (idx < s_idx) 226984920c14STony Zelenoff goto next; 227015e47304SEric W. Biederman if (pneigh_fill_info(skb, n, NETLINK_CB(cb->skb).portid, 227184920c14STony Zelenoff cb->nlh->nlmsg_seq, 227284920c14STony Zelenoff RTM_NEWNEIGH, 22737b46a644SDavid S. Miller NLM_F_MULTI, tbl) < 0) { 227484920c14STony Zelenoff read_unlock_bh(&tbl->lock); 227584920c14STony Zelenoff rc = -1; 227684920c14STony Zelenoff goto out; 227784920c14STony Zelenoff } 227884920c14STony Zelenoff next: 227984920c14STony Zelenoff idx++; 228084920c14STony Zelenoff } 228184920c14STony Zelenoff } 228284920c14STony Zelenoff 228384920c14STony Zelenoff read_unlock_bh(&tbl->lock); 228484920c14STony Zelenoff rc = skb->len; 228584920c14STony Zelenoff out: 228684920c14STony Zelenoff cb->args[3] = h; 228784920c14STony Zelenoff cb->args[4] = idx; 228884920c14STony Zelenoff return rc; 228984920c14STony Zelenoff 229084920c14STony Zelenoff } 229184920c14STony Zelenoff 2292c8822a4eSThomas Graf static int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) 22931da177e4SLinus Torvalds { 22941da177e4SLinus Torvalds struct neigh_table *tbl; 22951da177e4SLinus Torvalds int t, family, s_t; 229684920c14STony Zelenoff int proxy = 0; 22974bd6683bSEric Dumazet int err; 22981da177e4SLinus Torvalds 22998b8aec50SThomas Graf family = ((struct rtgenmsg *) nlmsg_data(cb->nlh))->rtgen_family; 230084920c14STony Zelenoff 230184920c14STony Zelenoff /* check for full ndmsg structure presence, family member is 230284920c14STony Zelenoff * the same for both structures 230384920c14STony Zelenoff */ 230484920c14STony Zelenoff if (nlmsg_len(cb->nlh) >= sizeof(struct ndmsg) && 230584920c14STony Zelenoff ((struct ndmsg *) nlmsg_data(cb->nlh))->ndm_flags == NTF_PROXY) 230684920c14STony Zelenoff proxy = 1; 230784920c14STony Zelenoff 23081da177e4SLinus Torvalds s_t = cb->args[0]; 23091da177e4SLinus Torvalds 2310d7480fd3SWANG Cong for (t = 0; t < NEIGH_NR_TABLES; t++) { 2311d7480fd3SWANG Cong tbl = neigh_tables[t]; 2312d7480fd3SWANG Cong 2313d7480fd3SWANG Cong if (!tbl) 2314d7480fd3SWANG Cong continue; 23151da177e4SLinus Torvalds if (t < s_t || (family && tbl->family != family)) 23161da177e4SLinus Torvalds continue; 23171da177e4SLinus Torvalds if (t > s_t) 23181da177e4SLinus Torvalds memset(&cb->args[1], 0, sizeof(cb->args) - 23191da177e4SLinus Torvalds sizeof(cb->args[0])); 232084920c14STony Zelenoff if (proxy) 232184920c14STony Zelenoff err = pneigh_dump_table(tbl, skb, cb); 232284920c14STony Zelenoff else 232384920c14STony Zelenoff err = neigh_dump_table(tbl, skb, cb); 23244bd6683bSEric Dumazet if (err < 0) 23254bd6683bSEric Dumazet break; 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds cb->args[0] = t; 23291da177e4SLinus Torvalds return skb->len; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) 23331da177e4SLinus Torvalds { 23341da177e4SLinus Torvalds int chain; 2335d6bf7817SEric Dumazet struct neigh_hash_table *nht; 23361da177e4SLinus Torvalds 2337d6bf7817SEric Dumazet rcu_read_lock_bh(); 2338d6bf7817SEric Dumazet nht = rcu_dereference_bh(tbl->nht); 2339d6bf7817SEric Dumazet 2340767e97e1SEric Dumazet read_lock(&tbl->lock); /* avoid resizes */ 2341cd089336SDavid S. Miller for (chain = 0; chain < (1 << nht->hash_shift); chain++) { 23421da177e4SLinus Torvalds struct neighbour *n; 23431da177e4SLinus Torvalds 2344767e97e1SEric Dumazet for (n = rcu_dereference_bh(nht->hash_buckets[chain]); 2345767e97e1SEric Dumazet n != NULL; 2346767e97e1SEric Dumazet n = rcu_dereference_bh(n->next)) 23471da177e4SLinus Torvalds cb(n, cookie); 23481da177e4SLinus Torvalds } 2349d6bf7817SEric Dumazet read_unlock(&tbl->lock); 2350d6bf7817SEric Dumazet rcu_read_unlock_bh(); 23511da177e4SLinus Torvalds } 23521da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_for_each); 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds /* The tbl->lock must be held as a writer and BH disabled. */ 23551da177e4SLinus Torvalds void __neigh_for_each_release(struct neigh_table *tbl, 23561da177e4SLinus Torvalds int (*cb)(struct neighbour *)) 23571da177e4SLinus Torvalds { 23581da177e4SLinus Torvalds int chain; 2359d6bf7817SEric Dumazet struct neigh_hash_table *nht; 23601da177e4SLinus Torvalds 2361d6bf7817SEric Dumazet nht = rcu_dereference_protected(tbl->nht, 2362d6bf7817SEric Dumazet lockdep_is_held(&tbl->lock)); 2363cd089336SDavid S. Miller for (chain = 0; chain < (1 << nht->hash_shift); chain++) { 2364767e97e1SEric Dumazet struct neighbour *n; 2365767e97e1SEric Dumazet struct neighbour __rcu **np; 23661da177e4SLinus Torvalds 2367d6bf7817SEric Dumazet np = &nht->hash_buckets[chain]; 2368767e97e1SEric Dumazet while ((n = rcu_dereference_protected(*np, 2369767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))) != NULL) { 23701da177e4SLinus Torvalds int release; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds write_lock(&n->lock); 23731da177e4SLinus Torvalds release = cb(n); 23741da177e4SLinus Torvalds if (release) { 2375767e97e1SEric Dumazet rcu_assign_pointer(*np, 2376767e97e1SEric Dumazet rcu_dereference_protected(n->next, 2377767e97e1SEric Dumazet lockdep_is_held(&tbl->lock))); 23781da177e4SLinus Torvalds n->dead = 1; 23791da177e4SLinus Torvalds } else 23801da177e4SLinus Torvalds np = &n->next; 23811da177e4SLinus Torvalds write_unlock(&n->lock); 23824f494554SThomas Graf if (release) 23834f494554SThomas Graf neigh_cleanup_and_release(n); 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds } 2386ecbb4169SAlexey Kuznetsov } 23871da177e4SLinus Torvalds EXPORT_SYMBOL(__neigh_for_each_release); 23881da177e4SLinus Torvalds 2389b79bda3dSEric W. Biederman int neigh_xmit(int index, struct net_device *dev, 23904fd3d7d9SEric W. Biederman const void *addr, struct sk_buff *skb) 23914fd3d7d9SEric W. Biederman { 2392b79bda3dSEric W. Biederman int err = -EAFNOSUPPORT; 2393b79bda3dSEric W. Biederman if (likely(index < NEIGH_NR_TABLES)) { 23944fd3d7d9SEric W. Biederman struct neigh_table *tbl; 23954fd3d7d9SEric W. Biederman struct neighbour *neigh; 23964fd3d7d9SEric W. Biederman 2397b79bda3dSEric W. Biederman tbl = neigh_tables[index]; 23984fd3d7d9SEric W. Biederman if (!tbl) 23994fd3d7d9SEric W. Biederman goto out; 24004fd3d7d9SEric W. Biederman neigh = __neigh_lookup_noref(tbl, addr, dev); 24014fd3d7d9SEric W. Biederman if (!neigh) 24024fd3d7d9SEric W. Biederman neigh = __neigh_create(tbl, addr, dev, false); 24034fd3d7d9SEric W. Biederman err = PTR_ERR(neigh); 24044fd3d7d9SEric W. Biederman if (IS_ERR(neigh)) 24054fd3d7d9SEric W. Biederman goto out_kfree_skb; 24064fd3d7d9SEric W. Biederman err = neigh->output(neigh, skb); 24074fd3d7d9SEric W. Biederman } 2408b79bda3dSEric W. Biederman else if (index == NEIGH_LINK_TABLE) { 2409b79bda3dSEric W. Biederman err = dev_hard_header(skb, dev, ntohs(skb->protocol), 2410b79bda3dSEric W. Biederman addr, NULL, skb->len); 2411b79bda3dSEric W. Biederman if (err < 0) 2412b79bda3dSEric W. Biederman goto out_kfree_skb; 2413b79bda3dSEric W. Biederman err = dev_queue_xmit(skb); 2414b79bda3dSEric W. Biederman } 24154fd3d7d9SEric W. Biederman out: 24164fd3d7d9SEric W. Biederman return err; 24174fd3d7d9SEric W. Biederman out_kfree_skb: 24184fd3d7d9SEric W. Biederman kfree_skb(skb); 24194fd3d7d9SEric W. Biederman goto out; 24204fd3d7d9SEric W. Biederman } 24214fd3d7d9SEric W. Biederman EXPORT_SYMBOL(neigh_xmit); 24224fd3d7d9SEric W. Biederman 24231da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds static struct neighbour *neigh_get_first(struct seq_file *seq) 24261da177e4SLinus Torvalds { 24271da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24281218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2429d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 24301da177e4SLinus Torvalds struct neighbour *n = NULL; 24311da177e4SLinus Torvalds int bucket = state->bucket; 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds state->flags &= ~NEIGH_SEQ_IS_PNEIGH; 2434cd089336SDavid S. Miller for (bucket = 0; bucket < (1 << nht->hash_shift); bucket++) { 2435767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[bucket]); 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds while (n) { 2438878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2439426b5303SEric W. Biederman goto next; 24401da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24411da177e4SLinus Torvalds loff_t fakep = 0; 24421da177e4SLinus Torvalds void *v; 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds v = state->neigh_sub_iter(state, n, &fakep); 24451da177e4SLinus Torvalds if (!v) 24461da177e4SLinus Torvalds goto next; 24471da177e4SLinus Torvalds } 24481da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 24491da177e4SLinus Torvalds break; 24501da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 24511da177e4SLinus Torvalds break; 24521da177e4SLinus Torvalds next: 2453767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds if (n) 24571da177e4SLinus Torvalds break; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds state->bucket = bucket; 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds return n; 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds static struct neighbour *neigh_get_next(struct seq_file *seq, 24651da177e4SLinus Torvalds struct neighbour *n, 24661da177e4SLinus Torvalds loff_t *pos) 24671da177e4SLinus Torvalds { 24681da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 24691218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 2470d6bf7817SEric Dumazet struct neigh_hash_table *nht = state->nht; 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24731da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 24741da177e4SLinus Torvalds if (v) 24751da177e4SLinus Torvalds return n; 24761da177e4SLinus Torvalds } 2477767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds while (1) { 24801da177e4SLinus Torvalds while (n) { 2481878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(n->dev), net)) 2482426b5303SEric W. Biederman goto next; 24831da177e4SLinus Torvalds if (state->neigh_sub_iter) { 24841da177e4SLinus Torvalds void *v = state->neigh_sub_iter(state, n, pos); 24851da177e4SLinus Torvalds if (v) 24861da177e4SLinus Torvalds return n; 24871da177e4SLinus Torvalds goto next; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) 24901da177e4SLinus Torvalds break; 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds if (n->nud_state & ~NUD_NOARP) 24931da177e4SLinus Torvalds break; 24941da177e4SLinus Torvalds next: 2495767e97e1SEric Dumazet n = rcu_dereference_bh(n->next); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds if (n) 24991da177e4SLinus Torvalds break; 25001da177e4SLinus Torvalds 2501cd089336SDavid S. Miller if (++state->bucket >= (1 << nht->hash_shift)) 25021da177e4SLinus Torvalds break; 25031da177e4SLinus Torvalds 2504767e97e1SEric Dumazet n = rcu_dereference_bh(nht->hash_buckets[state->bucket]); 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds if (n && pos) 25081da177e4SLinus Torvalds --(*pos); 25091da177e4SLinus Torvalds return n; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 25121da177e4SLinus Torvalds static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) 25131da177e4SLinus Torvalds { 25141da177e4SLinus Torvalds struct neighbour *n = neigh_get_first(seq); 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds if (n) { 2517745e2031SChris Larson --(*pos); 25181da177e4SLinus Torvalds while (*pos) { 25191da177e4SLinus Torvalds n = neigh_get_next(seq, n, pos); 25201da177e4SLinus Torvalds if (!n) 25211da177e4SLinus Torvalds break; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds return *pos ? NULL : n; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) 25281da177e4SLinus Torvalds { 25291da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25301218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 25311da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 25321da177e4SLinus Torvalds struct pneigh_entry *pn = NULL; 25331da177e4SLinus Torvalds int bucket = state->bucket; 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds state->flags |= NEIGH_SEQ_IS_PNEIGH; 25361da177e4SLinus Torvalds for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { 25371da177e4SLinus Torvalds pn = tbl->phash_buckets[bucket]; 2538878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2539426b5303SEric W. Biederman pn = pn->next; 25401da177e4SLinus Torvalds if (pn) 25411da177e4SLinus Torvalds break; 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds state->bucket = bucket; 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds return pn; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, 25491da177e4SLinus Torvalds struct pneigh_entry *pn, 25501da177e4SLinus Torvalds loff_t *pos) 25511da177e4SLinus Torvalds { 25521da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25531218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 25541da177e4SLinus Torvalds struct neigh_table *tbl = state->tbl; 25551da177e4SLinus Torvalds 2556df07a94cSJorge Boncompte [DTI2] do { 25571da177e4SLinus Torvalds pn = pn->next; 2558df07a94cSJorge Boncompte [DTI2] } while (pn && !net_eq(pneigh_net(pn), net)); 2559df07a94cSJorge Boncompte [DTI2] 25601da177e4SLinus Torvalds while (!pn) { 25611da177e4SLinus Torvalds if (++state->bucket > PNEIGH_HASHMASK) 25621da177e4SLinus Torvalds break; 25631da177e4SLinus Torvalds pn = tbl->phash_buckets[state->bucket]; 2564878628fbSYOSHIFUJI Hideaki while (pn && !net_eq(pneigh_net(pn), net)) 2565426b5303SEric W. Biederman pn = pn->next; 25661da177e4SLinus Torvalds if (pn) 25671da177e4SLinus Torvalds break; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds if (pn && pos) 25711da177e4SLinus Torvalds --(*pos); 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds return pn; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) 25771da177e4SLinus Torvalds { 25781da177e4SLinus Torvalds struct pneigh_entry *pn = pneigh_get_first(seq); 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds if (pn) { 2581745e2031SChris Larson --(*pos); 25821da177e4SLinus Torvalds while (*pos) { 25831da177e4SLinus Torvalds pn = pneigh_get_next(seq, pn, pos); 25841da177e4SLinus Torvalds if (!pn) 25851da177e4SLinus Torvalds break; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds return *pos ? NULL : pn; 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) 25921da177e4SLinus Torvalds { 25931da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 25941da177e4SLinus Torvalds void *rc; 2595745e2031SChris Larson loff_t idxpos = *pos; 25961da177e4SLinus Torvalds 2597745e2031SChris Larson rc = neigh_get_idx(seq, &idxpos); 25981da177e4SLinus Torvalds if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 2599745e2031SChris Larson rc = pneigh_get_idx(seq, &idxpos); 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds return rc; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) 2605d6bf7817SEric Dumazet __acquires(rcu_bh) 26061da177e4SLinus Torvalds { 26071da177e4SLinus Torvalds struct neigh_seq_state *state = seq->private; 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds state->tbl = tbl; 26101da177e4SLinus Torvalds state->bucket = 0; 26111da177e4SLinus Torvalds state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); 26121da177e4SLinus Torvalds 2613d6bf7817SEric Dumazet rcu_read_lock_bh(); 2614d6bf7817SEric Dumazet state->nht = rcu_dereference_bh(tbl->nht); 2615767e97e1SEric Dumazet 2616745e2031SChris Larson return *pos ? neigh_get_idx_any(seq, pos) : SEQ_START_TOKEN; 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_start); 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26211da177e4SLinus Torvalds { 26221da177e4SLinus Torvalds struct neigh_seq_state *state; 26231da177e4SLinus Torvalds void *rc; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 2626bff69732SChris Larson rc = neigh_get_first(seq); 26271da177e4SLinus Torvalds goto out; 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds state = seq->private; 26311da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { 26321da177e4SLinus Torvalds rc = neigh_get_next(seq, v, NULL); 26331da177e4SLinus Torvalds if (rc) 26341da177e4SLinus Torvalds goto out; 26351da177e4SLinus Torvalds if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) 26361da177e4SLinus Torvalds rc = pneigh_get_first(seq); 26371da177e4SLinus Torvalds } else { 26381da177e4SLinus Torvalds BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); 26391da177e4SLinus Torvalds rc = pneigh_get_next(seq, v, NULL); 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds out: 26421da177e4SLinus Torvalds ++(*pos); 26431da177e4SLinus Torvalds return rc; 26441da177e4SLinus Torvalds } 26451da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_next); 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds void neigh_seq_stop(struct seq_file *seq, void *v) 2648d6bf7817SEric Dumazet __releases(rcu_bh) 26491da177e4SLinus Torvalds { 2650d6bf7817SEric Dumazet rcu_read_unlock_bh(); 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds EXPORT_SYMBOL(neigh_seq_stop); 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds /* statistics via seq_file */ 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) 26571da177e4SLinus Torvalds { 265881c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26591da177e4SLinus Torvalds int cpu; 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds if (*pos == 0) 26621da177e4SLinus Torvalds return SEQ_START_TOKEN; 26631da177e4SLinus Torvalds 26640f23174aSRusty Russell for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 26651da177e4SLinus Torvalds if (!cpu_possible(cpu)) 26661da177e4SLinus Torvalds continue; 26671da177e4SLinus Torvalds *pos = cpu+1; 26681da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds return NULL; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) 26741da177e4SLinus Torvalds { 267581c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26761da177e4SLinus Torvalds int cpu; 26771da177e4SLinus Torvalds 26780f23174aSRusty Russell for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 26791da177e4SLinus Torvalds if (!cpu_possible(cpu)) 26801da177e4SLinus Torvalds continue; 26811da177e4SLinus Torvalds *pos = cpu+1; 26821da177e4SLinus Torvalds return per_cpu_ptr(tbl->stats, cpu); 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds return NULL; 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds static void neigh_stat_seq_stop(struct seq_file *seq, void *v) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds static int neigh_stat_seq_show(struct seq_file *seq, void *v) 26931da177e4SLinus Torvalds { 269481c1ebfcSAlexey Dobriyan struct neigh_table *tbl = seq->private; 26951da177e4SLinus Torvalds struct neigh_statistics *st = v; 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 26989a6d276eSNeil Horman seq_printf(seq, "entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards\n"); 26991da177e4SLinus Torvalds return 0; 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " 27039a6d276eSNeil Horman "%08lx %08lx %08lx %08lx %08lx\n", 27041da177e4SLinus Torvalds atomic_read(&tbl->entries), 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds st->allocs, 27071da177e4SLinus Torvalds st->destroys, 27081da177e4SLinus Torvalds st->hash_grows, 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds st->lookups, 27111da177e4SLinus Torvalds st->hits, 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds st->res_failed, 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds st->rcv_probes_mcast, 27161da177e4SLinus Torvalds st->rcv_probes_ucast, 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds st->periodic_gc_runs, 27199a6d276eSNeil Horman st->forced_gc_runs, 27209a6d276eSNeil Horman st->unres_discards 27211da177e4SLinus Torvalds ); 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds return 0; 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds 2726f690808eSStephen Hemminger static const struct seq_operations neigh_stat_seq_ops = { 27271da177e4SLinus Torvalds .start = neigh_stat_seq_start, 27281da177e4SLinus Torvalds .next = neigh_stat_seq_next, 27291da177e4SLinus Torvalds .stop = neigh_stat_seq_stop, 27301da177e4SLinus Torvalds .show = neigh_stat_seq_show, 27311da177e4SLinus Torvalds }; 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds static int neigh_stat_seq_open(struct inode *inode, struct file *file) 27341da177e4SLinus Torvalds { 27351da177e4SLinus Torvalds int ret = seq_open(file, &neigh_stat_seq_ops); 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds if (!ret) { 27381da177e4SLinus Torvalds struct seq_file *sf = file->private_data; 2739d9dda78bSAl Viro sf->private = PDE_DATA(inode); 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds return ret; 27421da177e4SLinus Torvalds }; 27431da177e4SLinus Torvalds 27449a32144eSArjan van de Ven static const struct file_operations neigh_stat_seq_fops = { 27451da177e4SLinus Torvalds .owner = THIS_MODULE, 27461da177e4SLinus Torvalds .open = neigh_stat_seq_open, 27471da177e4SLinus Torvalds .read = seq_read, 27481da177e4SLinus Torvalds .llseek = seq_lseek, 27491da177e4SLinus Torvalds .release = seq_release, 27501da177e4SLinus Torvalds }; 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27531da177e4SLinus Torvalds 2754339bf98fSThomas Graf static inline size_t neigh_nlmsg_size(void) 2755339bf98fSThomas Graf { 2756339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ndmsg)) 2757339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_DST */ 2758339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* NDA_LLADDR */ 2759339bf98fSThomas Graf + nla_total_size(sizeof(struct nda_cacheinfo)) 2760339bf98fSThomas Graf + nla_total_size(4); /* NDA_PROBES */ 2761339bf98fSThomas Graf } 2762339bf98fSThomas Graf 2763b8673311SThomas Graf static void __neigh_notify(struct neighbour *n, int type, int flags) 27641da177e4SLinus Torvalds { 2765c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(n->dev); 27668b8aec50SThomas Graf struct sk_buff *skb; 2767b8673311SThomas Graf int err = -ENOBUFS; 27681da177e4SLinus Torvalds 2769339bf98fSThomas Graf skb = nlmsg_new(neigh_nlmsg_size(), GFP_ATOMIC); 27708b8aec50SThomas Graf if (skb == NULL) 2771b8673311SThomas Graf goto errout; 27721da177e4SLinus Torvalds 2773b8673311SThomas Graf err = neigh_fill_info(skb, n, 0, 0, type, flags); 277426932566SPatrick McHardy if (err < 0) { 277526932566SPatrick McHardy /* -EMSGSIZE implies BUG in neigh_nlmsg_size() */ 277626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 277726932566SPatrick McHardy kfree_skb(skb); 277826932566SPatrick McHardy goto errout; 277926932566SPatrick McHardy } 27801ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 27811ce85fe4SPablo Neira Ayuso return; 2782b8673311SThomas Graf errout: 2783b8673311SThomas Graf if (err < 0) 2784426b5303SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2785b8673311SThomas Graf } 2786b8673311SThomas Graf 2787b8673311SThomas Graf void neigh_app_ns(struct neighbour *n) 2788b8673311SThomas Graf { 2789b8673311SThomas Graf __neigh_notify(n, RTM_GETNEIGH, NLM_F_REQUEST); 27908b8aec50SThomas Graf } 27910a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_app_ns); 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 2794b93196dcSCong Wang static int zero; 2795555445cdSFrancesco Fusco static int int_max = INT_MAX; 2796b93196dcSCong Wang static int unres_qlen_max = INT_MAX / SKB_TRUESIZE(ETH_FRAME_LEN); 27971da177e4SLinus Torvalds 2798fe2c6338SJoe Perches static int proc_unres_qlen(struct ctl_table *ctl, int write, 2799fe2c6338SJoe Perches void __user *buffer, size_t *lenp, loff_t *ppos) 28008b5c171bSEric Dumazet { 28018b5c171bSEric Dumazet int size, ret; 2802fe2c6338SJoe Perches struct ctl_table tmp = *ctl; 28038b5c171bSEric Dumazet 2804ce46cc64SShan Wei tmp.extra1 = &zero; 2805ce46cc64SShan Wei tmp.extra2 = &unres_qlen_max; 28068b5c171bSEric Dumazet tmp.data = &size; 2807ce46cc64SShan Wei 2808ce46cc64SShan Wei size = *(int *)ctl->data / SKB_TRUESIZE(ETH_FRAME_LEN); 2809ce46cc64SShan Wei ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 2810ce46cc64SShan Wei 28118b5c171bSEric Dumazet if (write && !ret) 28128b5c171bSEric Dumazet *(int *)ctl->data = size * SKB_TRUESIZE(ETH_FRAME_LEN); 28138b5c171bSEric Dumazet return ret; 28148b5c171bSEric Dumazet } 28158b5c171bSEric Dumazet 28161d4c8c29SJiri Pirko static struct neigh_parms *neigh_get_dev_parms_rcu(struct net_device *dev, 28171d4c8c29SJiri Pirko int family) 28181d4c8c29SJiri Pirko { 2819bba24896SJiri Pirko switch (family) { 2820bba24896SJiri Pirko case AF_INET: 28211d4c8c29SJiri Pirko return __in_dev_arp_parms_get_rcu(dev); 2822bba24896SJiri Pirko case AF_INET6: 2823bba24896SJiri Pirko return __in6_dev_nd_parms_get_rcu(dev); 2824bba24896SJiri Pirko } 28251d4c8c29SJiri Pirko return NULL; 28261d4c8c29SJiri Pirko } 28271d4c8c29SJiri Pirko 28281d4c8c29SJiri Pirko static void neigh_copy_dflt_parms(struct net *net, struct neigh_parms *p, 28291d4c8c29SJiri Pirko int index) 28301d4c8c29SJiri Pirko { 28311d4c8c29SJiri Pirko struct net_device *dev; 28321d4c8c29SJiri Pirko int family = neigh_parms_family(p); 28331d4c8c29SJiri Pirko 28341d4c8c29SJiri Pirko rcu_read_lock(); 28351d4c8c29SJiri Pirko for_each_netdev_rcu(net, dev) { 28361d4c8c29SJiri Pirko struct neigh_parms *dst_p = 28371d4c8c29SJiri Pirko neigh_get_dev_parms_rcu(dev, family); 28381d4c8c29SJiri Pirko 28391d4c8c29SJiri Pirko if (dst_p && !test_bit(index, dst_p->data_state)) 28401d4c8c29SJiri Pirko dst_p->data[index] = p->data[index]; 28411d4c8c29SJiri Pirko } 28421d4c8c29SJiri Pirko rcu_read_unlock(); 28431d4c8c29SJiri Pirko } 28441d4c8c29SJiri Pirko 28451d4c8c29SJiri Pirko static void neigh_proc_update(struct ctl_table *ctl, int write) 28461d4c8c29SJiri Pirko { 28471d4c8c29SJiri Pirko struct net_device *dev = ctl->extra1; 28481d4c8c29SJiri Pirko struct neigh_parms *p = ctl->extra2; 284977d47afbSJiri Pirko struct net *net = neigh_parms_net(p); 28501d4c8c29SJiri Pirko int index = (int *) ctl->data - p->data; 28511d4c8c29SJiri Pirko 28521d4c8c29SJiri Pirko if (!write) 28531d4c8c29SJiri Pirko return; 28541d4c8c29SJiri Pirko 28551d4c8c29SJiri Pirko set_bit(index, p->data_state); 28561d4c8c29SJiri Pirko if (!dev) /* NULL dev means this is default value */ 28571d4c8c29SJiri Pirko neigh_copy_dflt_parms(net, p, index); 28581d4c8c29SJiri Pirko } 28591d4c8c29SJiri Pirko 28601f9248e5SJiri Pirko static int neigh_proc_dointvec_zero_intmax(struct ctl_table *ctl, int write, 28611f9248e5SJiri Pirko void __user *buffer, 28621f9248e5SJiri Pirko size_t *lenp, loff_t *ppos) 28631f9248e5SJiri Pirko { 28641f9248e5SJiri Pirko struct ctl_table tmp = *ctl; 28651d4c8c29SJiri Pirko int ret; 28661f9248e5SJiri Pirko 28671f9248e5SJiri Pirko tmp.extra1 = &zero; 28681f9248e5SJiri Pirko tmp.extra2 = &int_max; 28691f9248e5SJiri Pirko 28701d4c8c29SJiri Pirko ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 28711d4c8c29SJiri Pirko neigh_proc_update(ctl, write); 28721d4c8c29SJiri Pirko return ret; 28731f9248e5SJiri Pirko } 28741f9248e5SJiri Pirko 2875cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write, 2876cb5b09c1SJiri Pirko void __user *buffer, size_t *lenp, loff_t *ppos) 2877cb5b09c1SJiri Pirko { 28781d4c8c29SJiri Pirko int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 28791d4c8c29SJiri Pirko 28801d4c8c29SJiri Pirko neigh_proc_update(ctl, write); 28811d4c8c29SJiri Pirko return ret; 2882cb5b09c1SJiri Pirko } 2883cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec); 2884cb5b09c1SJiri Pirko 2885cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write, 2886cb5b09c1SJiri Pirko void __user *buffer, 2887cb5b09c1SJiri Pirko size_t *lenp, loff_t *ppos) 2888cb5b09c1SJiri Pirko { 28891d4c8c29SJiri Pirko int ret = proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos); 28901d4c8c29SJiri Pirko 28911d4c8c29SJiri Pirko neigh_proc_update(ctl, write); 28921d4c8c29SJiri Pirko return ret; 2893cb5b09c1SJiri Pirko } 2894cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_jiffies); 2895cb5b09c1SJiri Pirko 2896cb5b09c1SJiri Pirko static int neigh_proc_dointvec_userhz_jiffies(struct ctl_table *ctl, int write, 2897cb5b09c1SJiri Pirko void __user *buffer, 2898cb5b09c1SJiri Pirko size_t *lenp, loff_t *ppos) 2899cb5b09c1SJiri Pirko { 29001d4c8c29SJiri Pirko int ret = proc_dointvec_userhz_jiffies(ctl, write, buffer, lenp, ppos); 29011d4c8c29SJiri Pirko 29021d4c8c29SJiri Pirko neigh_proc_update(ctl, write); 29031d4c8c29SJiri Pirko return ret; 2904cb5b09c1SJiri Pirko } 2905cb5b09c1SJiri Pirko 2906cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write, 2907cb5b09c1SJiri Pirko void __user *buffer, 2908cb5b09c1SJiri Pirko size_t *lenp, loff_t *ppos) 2909cb5b09c1SJiri Pirko { 29101d4c8c29SJiri Pirko int ret = proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos); 29111d4c8c29SJiri Pirko 29121d4c8c29SJiri Pirko neigh_proc_update(ctl, write); 29131d4c8c29SJiri Pirko return ret; 2914cb5b09c1SJiri Pirko } 2915cb5b09c1SJiri Pirko EXPORT_SYMBOL(neigh_proc_dointvec_ms_jiffies); 2916cb5b09c1SJiri Pirko 2917cb5b09c1SJiri Pirko static int neigh_proc_dointvec_unres_qlen(struct ctl_table *ctl, int write, 2918cb5b09c1SJiri Pirko void __user *buffer, 2919cb5b09c1SJiri Pirko size_t *lenp, loff_t *ppos) 2920cb5b09c1SJiri Pirko { 29211d4c8c29SJiri Pirko int ret = proc_unres_qlen(ctl, write, buffer, lenp, ppos); 29221d4c8c29SJiri Pirko 29231d4c8c29SJiri Pirko neigh_proc_update(ctl, write); 29241d4c8c29SJiri Pirko return ret; 2925cb5b09c1SJiri Pirko } 2926cb5b09c1SJiri Pirko 29274bf6980dSJean-Francois Remy static int neigh_proc_base_reachable_time(struct ctl_table *ctl, int write, 29284bf6980dSJean-Francois Remy void __user *buffer, 29294bf6980dSJean-Francois Remy size_t *lenp, loff_t *ppos) 29304bf6980dSJean-Francois Remy { 29314bf6980dSJean-Francois Remy struct neigh_parms *p = ctl->extra2; 29324bf6980dSJean-Francois Remy int ret; 29334bf6980dSJean-Francois Remy 29344bf6980dSJean-Francois Remy if (strcmp(ctl->procname, "base_reachable_time") == 0) 29354bf6980dSJean-Francois Remy ret = neigh_proc_dointvec_jiffies(ctl, write, buffer, lenp, ppos); 29364bf6980dSJean-Francois Remy else if (strcmp(ctl->procname, "base_reachable_time_ms") == 0) 29374bf6980dSJean-Francois Remy ret = neigh_proc_dointvec_ms_jiffies(ctl, write, buffer, lenp, ppos); 29384bf6980dSJean-Francois Remy else 29394bf6980dSJean-Francois Remy ret = -1; 29404bf6980dSJean-Francois Remy 29414bf6980dSJean-Francois Remy if (write && ret == 0) { 29424bf6980dSJean-Francois Remy /* update reachable_time as well, otherwise, the change will 29434bf6980dSJean-Francois Remy * only be effective after the next time neigh_periodic_work 29444bf6980dSJean-Francois Remy * decides to recompute it 29454bf6980dSJean-Francois Remy */ 29464bf6980dSJean-Francois Remy p->reachable_time = 29474bf6980dSJean-Francois Remy neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); 29484bf6980dSJean-Francois Remy } 29494bf6980dSJean-Francois Remy return ret; 29504bf6980dSJean-Francois Remy } 29514bf6980dSJean-Francois Remy 29521f9248e5SJiri Pirko #define NEIGH_PARMS_DATA_OFFSET(index) \ 29531f9248e5SJiri Pirko (&((struct neigh_parms *) 0)->data[index]) 29541f9248e5SJiri Pirko 29551f9248e5SJiri Pirko #define NEIGH_SYSCTL_ENTRY(attr, data_attr, name, mval, proc) \ 29561f9248e5SJiri Pirko [NEIGH_VAR_ ## attr] = { \ 29571f9248e5SJiri Pirko .procname = name, \ 29581f9248e5SJiri Pirko .data = NEIGH_PARMS_DATA_OFFSET(NEIGH_VAR_ ## data_attr), \ 29591f9248e5SJiri Pirko .maxlen = sizeof(int), \ 29601f9248e5SJiri Pirko .mode = mval, \ 29611f9248e5SJiri Pirko .proc_handler = proc, \ 29621f9248e5SJiri Pirko } 29631f9248e5SJiri Pirko 29641f9248e5SJiri Pirko #define NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(attr, name) \ 29651f9248e5SJiri Pirko NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_zero_intmax) 29661f9248e5SJiri Pirko 29671f9248e5SJiri Pirko #define NEIGH_SYSCTL_JIFFIES_ENTRY(attr, name) \ 2968cb5b09c1SJiri Pirko NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_jiffies) 29691f9248e5SJiri Pirko 29701f9248e5SJiri Pirko #define NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(attr, name) \ 2971cb5b09c1SJiri Pirko NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_userhz_jiffies) 29721f9248e5SJiri Pirko 29731f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_ENTRY(attr, name) \ 2974cb5b09c1SJiri Pirko NEIGH_SYSCTL_ENTRY(attr, attr, name, 0644, neigh_proc_dointvec_ms_jiffies) 29751f9248e5SJiri Pirko 29761f9248e5SJiri Pirko #define NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(attr, data_attr, name) \ 2977cb5b09c1SJiri Pirko NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_ms_jiffies) 29781f9248e5SJiri Pirko 29791f9248e5SJiri Pirko #define NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(attr, data_attr, name) \ 2980cb5b09c1SJiri Pirko NEIGH_SYSCTL_ENTRY(attr, data_attr, name, 0644, neigh_proc_dointvec_unres_qlen) 298154716e3bSEric W. Biederman 29821da177e4SLinus Torvalds static struct neigh_sysctl_table { 29831da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 29848b5c171bSEric Dumazet struct ctl_table neigh_vars[NEIGH_VAR_MAX + 1]; 2985ab32ea5dSBrian Haley } neigh_sysctl_template __read_mostly = { 29861da177e4SLinus Torvalds .neigh_vars = { 29871f9248e5SJiri Pirko NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"), 29881f9248e5SJiri Pirko NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"), 29891f9248e5SJiri Pirko NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"), 29901f9248e5SJiri Pirko NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"), 29911f9248e5SJiri Pirko NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"), 29921f9248e5SJiri Pirko NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"), 29931f9248e5SJiri Pirko NEIGH_SYSCTL_JIFFIES_ENTRY(GC_STALETIME, "gc_stale_time"), 29941f9248e5SJiri Pirko NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(QUEUE_LEN_BYTES, "unres_qlen_bytes"), 29951f9248e5SJiri Pirko NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(PROXY_QLEN, "proxy_qlen"), 29961f9248e5SJiri Pirko NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(ANYCAST_DELAY, "anycast_delay"), 29971f9248e5SJiri Pirko NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(PROXY_DELAY, "proxy_delay"), 29981f9248e5SJiri Pirko NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(LOCKTIME, "locktime"), 29991f9248e5SJiri Pirko NEIGH_SYSCTL_UNRES_QLEN_REUSED_ENTRY(QUEUE_LEN, QUEUE_LEN_BYTES, "unres_qlen"), 30001f9248e5SJiri Pirko NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(RETRANS_TIME_MS, RETRANS_TIME, "retrans_time_ms"), 30011f9248e5SJiri Pirko NEIGH_SYSCTL_MS_JIFFIES_REUSED_ENTRY(BASE_REACHABLE_TIME_MS, BASE_REACHABLE_TIME, "base_reachable_time_ms"), 30028b5c171bSEric Dumazet [NEIGH_VAR_GC_INTERVAL] = { 30031da177e4SLinus Torvalds .procname = "gc_interval", 30041da177e4SLinus Torvalds .maxlen = sizeof(int), 30051da177e4SLinus Torvalds .mode = 0644, 30066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 30071da177e4SLinus Torvalds }, 30088b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH1] = { 30091da177e4SLinus Torvalds .procname = "gc_thresh1", 30101da177e4SLinus Torvalds .maxlen = sizeof(int), 30111da177e4SLinus Torvalds .mode = 0644, 3012555445cdSFrancesco Fusco .extra1 = &zero, 3013555445cdSFrancesco Fusco .extra2 = &int_max, 3014555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 30151da177e4SLinus Torvalds }, 30168b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH2] = { 30171da177e4SLinus Torvalds .procname = "gc_thresh2", 30181da177e4SLinus Torvalds .maxlen = sizeof(int), 30191da177e4SLinus Torvalds .mode = 0644, 3020555445cdSFrancesco Fusco .extra1 = &zero, 3021555445cdSFrancesco Fusco .extra2 = &int_max, 3022555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 30231da177e4SLinus Torvalds }, 30248b5c171bSEric Dumazet [NEIGH_VAR_GC_THRESH3] = { 30251da177e4SLinus Torvalds .procname = "gc_thresh3", 30261da177e4SLinus Torvalds .maxlen = sizeof(int), 30271da177e4SLinus Torvalds .mode = 0644, 3028555445cdSFrancesco Fusco .extra1 = &zero, 3029555445cdSFrancesco Fusco .extra2 = &int_max, 3030555445cdSFrancesco Fusco .proc_handler = proc_dointvec_minmax, 30311da177e4SLinus Torvalds }, 3032c3bac5a7SPavel Emelyanov {}, 30331da177e4SLinus Torvalds }, 30341da177e4SLinus Torvalds }; 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 303773af614aSJiri Pirko proc_handler *handler) 30381da177e4SLinus Torvalds { 30391f9248e5SJiri Pirko int i; 30403c607bbbSPavel Emelyanov struct neigh_sysctl_table *t; 30411f9248e5SJiri Pirko const char *dev_name_source; 30428f40a1f9SEric W. Biederman char neigh_path[ sizeof("net//neigh/") + IFNAMSIZ + IFNAMSIZ ]; 304373af614aSJiri Pirko char *p_name; 30441da177e4SLinus Torvalds 30453c607bbbSPavel Emelyanov t = kmemdup(&neigh_sysctl_template, sizeof(*t), GFP_KERNEL); 30461da177e4SLinus Torvalds if (!t) 30473c607bbbSPavel Emelyanov goto err; 30483c607bbbSPavel Emelyanov 3049b194c1f1SJiri Pirko for (i = 0; i < NEIGH_VAR_GC_INTERVAL; i++) { 30501f9248e5SJiri Pirko t->neigh_vars[i].data += (long) p; 3051cb5b09c1SJiri Pirko t->neigh_vars[i].extra1 = dev; 30521d4c8c29SJiri Pirko t->neigh_vars[i].extra2 = p; 3053cb5b09c1SJiri Pirko } 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds if (dev) { 30561da177e4SLinus Torvalds dev_name_source = dev->name; 3057d12af679SEric W. Biederman /* Terminate the table early */ 30588b5c171bSEric Dumazet memset(&t->neigh_vars[NEIGH_VAR_GC_INTERVAL], 0, 30598b5c171bSEric Dumazet sizeof(t->neigh_vars[NEIGH_VAR_GC_INTERVAL])); 30601da177e4SLinus Torvalds } else { 30619ecf07a1SMathias Krause struct neigh_table *tbl = p->tbl; 30628f40a1f9SEric W. Biederman dev_name_source = "default"; 30639ecf07a1SMathias Krause t->neigh_vars[NEIGH_VAR_GC_INTERVAL].data = &tbl->gc_interval; 30649ecf07a1SMathias Krause t->neigh_vars[NEIGH_VAR_GC_THRESH1].data = &tbl->gc_thresh1; 30659ecf07a1SMathias Krause t->neigh_vars[NEIGH_VAR_GC_THRESH2].data = &tbl->gc_thresh2; 30669ecf07a1SMathias Krause t->neigh_vars[NEIGH_VAR_GC_THRESH3].data = &tbl->gc_thresh3; 30671da177e4SLinus Torvalds } 30681da177e4SLinus Torvalds 3069f8572d8fSEric W. Biederman if (handler) { 30701da177e4SLinus Torvalds /* RetransTime */ 30718b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME].proc_handler = handler; 30721da177e4SLinus Torvalds /* ReachableTime */ 30738b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = handler; 30741da177e4SLinus Torvalds /* RetransTime (in milliseconds)*/ 30758b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_RETRANS_TIME_MS].proc_handler = handler; 30761da177e4SLinus Torvalds /* ReachableTime (in milliseconds) */ 30778b5c171bSEric Dumazet t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = handler; 30784bf6980dSJean-Francois Remy } else { 30794bf6980dSJean-Francois Remy /* Those handlers will update p->reachable_time after 30804bf6980dSJean-Francois Remy * base_reachable_time(_ms) is set to ensure the new timer starts being 30814bf6980dSJean-Francois Remy * applied after the next neighbour update instead of waiting for 30824bf6980dSJean-Francois Remy * neigh_periodic_work to update its value (can be multiple minutes) 30834bf6980dSJean-Francois Remy * So any handler that replaces them should do this as well 30844bf6980dSJean-Francois Remy */ 30854bf6980dSJean-Francois Remy /* ReachableTime */ 30864bf6980dSJean-Francois Remy t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME].proc_handler = 30874bf6980dSJean-Francois Remy neigh_proc_base_reachable_time; 30884bf6980dSJean-Francois Remy /* ReachableTime (in milliseconds) */ 30894bf6980dSJean-Francois Remy t->neigh_vars[NEIGH_VAR_BASE_REACHABLE_TIME_MS].proc_handler = 30904bf6980dSJean-Francois Remy neigh_proc_base_reachable_time; 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds 3093464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3094464dc801SEric W. Biederman if (neigh_parms_net(p)->user_ns != &init_user_ns) 3095464dc801SEric W. Biederman t->neigh_vars[0].procname = NULL; 3096464dc801SEric W. Biederman 309773af614aSJiri Pirko switch (neigh_parms_family(p)) { 309873af614aSJiri Pirko case AF_INET: 309973af614aSJiri Pirko p_name = "ipv4"; 310073af614aSJiri Pirko break; 310173af614aSJiri Pirko case AF_INET6: 310273af614aSJiri Pirko p_name = "ipv6"; 310373af614aSJiri Pirko break; 310473af614aSJiri Pirko default: 310573af614aSJiri Pirko BUG(); 310673af614aSJiri Pirko } 310773af614aSJiri Pirko 31088f40a1f9SEric W. Biederman snprintf(neigh_path, sizeof(neigh_path), "net/%s/neigh/%s", 31098f40a1f9SEric W. Biederman p_name, dev_name_source); 31104ab438fcSDenis V. Lunev t->sysctl_header = 31118f40a1f9SEric W. Biederman register_net_sysctl(neigh_parms_net(p), neigh_path, t->neigh_vars); 31123c607bbbSPavel Emelyanov if (!t->sysctl_header) 31138f40a1f9SEric W. Biederman goto free; 31143c607bbbSPavel Emelyanov 31151da177e4SLinus Torvalds p->sysctl_table = t; 31161da177e4SLinus Torvalds return 0; 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds free: 31191da177e4SLinus Torvalds kfree(t); 31203c607bbbSPavel Emelyanov err: 31213c607bbbSPavel Emelyanov return -ENOBUFS; 31221da177e4SLinus Torvalds } 31230a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_register); 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds void neigh_sysctl_unregister(struct neigh_parms *p) 31261da177e4SLinus Torvalds { 31271da177e4SLinus Torvalds if (p->sysctl_table) { 31281da177e4SLinus Torvalds struct neigh_sysctl_table *t = p->sysctl_table; 31291da177e4SLinus Torvalds p->sysctl_table = NULL; 31305dd3df10SEric W. Biederman unregister_net_sysctl_table(t->sysctl_header); 31311da177e4SLinus Torvalds kfree(t); 31321da177e4SLinus Torvalds } 31331da177e4SLinus Torvalds } 31340a204500SYOSHIFUJI Hideaki EXPORT_SYMBOL(neigh_sysctl_unregister); 31351da177e4SLinus Torvalds 31361da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */ 31371da177e4SLinus Torvalds 3138c8822a4eSThomas Graf static int __init neigh_init(void) 3139c8822a4eSThomas Graf { 3140c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_NEWNEIGH, neigh_add, NULL, NULL); 3141c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_DELNEIGH, neigh_delete, NULL, NULL); 3142c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETNEIGH, NULL, neigh_dump_info, NULL); 3143c8822a4eSThomas Graf 3144c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETNEIGHTBL, NULL, neightbl_dump_info, 3145c7ac8679SGreg Rose NULL); 3146c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_SETNEIGHTBL, neightbl_set, NULL, NULL); 3147c8822a4eSThomas Graf 3148c8822a4eSThomas Graf return 0; 3149c8822a4eSThomas Graf } 3150c8822a4eSThomas Graf 3151c8822a4eSThomas Graf subsys_initcall(neigh_init); 3152c8822a4eSThomas Graf 3153