1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef _NET_NEIGHBOUR_H 31da177e4SLinus Torvalds #define _NET_NEIGHBOUR_H 41da177e4SLinus Torvalds 59067c722SThomas Graf #include <linux/neighbour.h> 69067c722SThomas Graf 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Generic neighbour manipulation 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Authors: 111da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 121da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Harald Welte: <laforge@gnumonks.org> 171da177e4SLinus Torvalds * - Add neighbour cache statistics like rtstat 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 2060063497SArun Sharma #include <linux/atomic.h> 219f237430SReshetova, Elena #include <linux/refcount.h> 221da177e4SLinus Torvalds #include <linux/netdevice.h> 2314c85021SArnaldo Carvalho de Melo #include <linux/skbuff.h> 241da177e4SLinus Torvalds #include <linux/rcupdate.h> 251da177e4SLinus Torvalds #include <linux/seq_file.h> 261d4c8c29SJiri Pirko #include <linux/bitmap.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <linux/err.h> 291da177e4SLinus Torvalds #include <linux/sysctl.h> 30e4c4e448SEric Dumazet #include <linux/workqueue.h> 31c8822a4eSThomas Graf #include <net/rtnetlink.h> 321da177e4SLinus Torvalds 3339971554SPavel Emelyanov /* 3439971554SPavel Emelyanov * NUD stands for "neighbor unreachability detection" 3539971554SPavel Emelyanov */ 3639971554SPavel Emelyanov 371da177e4SLinus Torvalds #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE) 381da177e4SLinus Torvalds #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY) 391da177e4SLinus Torvalds #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE) 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds struct neighbour; 421da177e4SLinus Torvalds 431f9248e5SJiri Pirko enum { 441f9248e5SJiri Pirko NEIGH_VAR_MCAST_PROBES, 451f9248e5SJiri Pirko NEIGH_VAR_UCAST_PROBES, 461f9248e5SJiri Pirko NEIGH_VAR_APP_PROBES, 478da86466SYOSHIFUJI Hideaki/吉藤英明 NEIGH_VAR_MCAST_REPROBES, 481f9248e5SJiri Pirko NEIGH_VAR_RETRANS_TIME, 491f9248e5SJiri Pirko NEIGH_VAR_BASE_REACHABLE_TIME, 501f9248e5SJiri Pirko NEIGH_VAR_DELAY_PROBE_TIME, 51211da42eSYuwei Wang NEIGH_VAR_INTERVAL_PROBE_TIME_MS, 521f9248e5SJiri Pirko NEIGH_VAR_GC_STALETIME, 531f9248e5SJiri Pirko NEIGH_VAR_QUEUE_LEN_BYTES, 541f9248e5SJiri Pirko NEIGH_VAR_PROXY_QLEN, 551f9248e5SJiri Pirko NEIGH_VAR_ANYCAST_DELAY, 561f9248e5SJiri Pirko NEIGH_VAR_PROXY_DELAY, 571f9248e5SJiri Pirko NEIGH_VAR_LOCKTIME, 581f9248e5SJiri Pirko #define NEIGH_VAR_DATA_MAX (NEIGH_VAR_LOCKTIME + 1) 591f9248e5SJiri Pirko /* Following are used as a second way to access one of the above */ 601f9248e5SJiri Pirko NEIGH_VAR_QUEUE_LEN, /* same data as NEIGH_VAR_QUEUE_LEN_BYTES */ 611f9248e5SJiri Pirko NEIGH_VAR_RETRANS_TIME_MS, /* same data as NEIGH_VAR_RETRANS_TIME */ 621f9248e5SJiri Pirko NEIGH_VAR_BASE_REACHABLE_TIME_MS, /* same data as NEIGH_VAR_BASE_REACHABLE_TIME */ 631f9248e5SJiri Pirko /* Following are used by "default" only */ 641f9248e5SJiri Pirko NEIGH_VAR_GC_INTERVAL, 651f9248e5SJiri Pirko NEIGH_VAR_GC_THRESH1, 661f9248e5SJiri Pirko NEIGH_VAR_GC_THRESH2, 671f9248e5SJiri Pirko NEIGH_VAR_GC_THRESH3, 681f9248e5SJiri Pirko NEIGH_VAR_MAX 691f9248e5SJiri Pirko }; 701f9248e5SJiri Pirko 71fd2c3ef7SEric Dumazet struct neigh_parms { 720c5c9fb5SEric W. Biederman possible_net_t net; 73c7fb64dbSThomas Graf struct net_device *dev; 7408d62256SEric Dumazet netdevice_tracker dev_tracker; 7575fbfd33SNicolas Dichtel struct list_head list; 76447f2191SDavid S. Miller int (*neigh_setup)(struct neighbour *); 771da177e4SLinus Torvalds struct neigh_table *tbl; 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds void *sysctl_table; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds int dead; 826343944bSReshetova, Elena refcount_t refcnt; 831da177e4SLinus Torvalds struct rcu_head rcu_head; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds int reachable_time; 868207f253SThomas Zeitlhofer u32 qlen; 871f9248e5SJiri Pirko int data[NEIGH_VAR_DATA_MAX]; 881d4c8c29SJiri Pirko DECLARE_BITMAP(data_state, NEIGH_VAR_DATA_MAX); 891da177e4SLinus Torvalds }; 901da177e4SLinus Torvalds 911f9248e5SJiri Pirko static inline void neigh_var_set(struct neigh_parms *p, int index, int val) 921f9248e5SJiri Pirko { 931d4c8c29SJiri Pirko set_bit(index, p->data_state); 941f9248e5SJiri Pirko p->data[index] = val; 951f9248e5SJiri Pirko } 961f9248e5SJiri Pirko 971f9248e5SJiri Pirko #define NEIGH_VAR(p, attr) ((p)->data[NEIGH_VAR_ ## attr]) 9889740ca7SJiri Pirko 9989740ca7SJiri Pirko /* In ndo_neigh_setup, NEIGH_VAR_INIT should be used. 10089740ca7SJiri Pirko * In other cases, NEIGH_VAR_SET should be used. 10189740ca7SJiri Pirko */ 10289740ca7SJiri Pirko #define NEIGH_VAR_INIT(p, attr, val) (NEIGH_VAR(p, attr) = val) 1031f9248e5SJiri Pirko #define NEIGH_VAR_SET(p, attr, val) neigh_var_set(p, NEIGH_VAR_ ## attr, val) 1041f9248e5SJiri Pirko 1051d4c8c29SJiri Pirko static inline void neigh_parms_data_state_setall(struct neigh_parms *p) 1061d4c8c29SJiri Pirko { 1071d4c8c29SJiri Pirko bitmap_fill(p->data_state, NEIGH_VAR_DATA_MAX); 1081d4c8c29SJiri Pirko } 1091d4c8c29SJiri Pirko 1101d4c8c29SJiri Pirko static inline void neigh_parms_data_state_cleanall(struct neigh_parms *p) 1111d4c8c29SJiri Pirko { 1121d4c8c29SJiri Pirko bitmap_zero(p->data_state, NEIGH_VAR_DATA_MAX); 1131d4c8c29SJiri Pirko } 1141d4c8c29SJiri Pirko 115fd2c3ef7SEric Dumazet struct neigh_statistics { 1161da177e4SLinus Torvalds unsigned long allocs; /* number of allocated neighs */ 1171da177e4SLinus Torvalds unsigned long destroys; /* number of destroyed neighs */ 1181da177e4SLinus Torvalds unsigned long hash_grows; /* number of hash resizes */ 1191da177e4SLinus Torvalds 1209a6d276eSNeil Horman unsigned long res_failed; /* number of failed resolutions */ 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds unsigned long lookups; /* number of lookups */ 1231da177e4SLinus Torvalds unsigned long hits; /* number of hits (among lookups) */ 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */ 1261da177e4SLinus Torvalds unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */ 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds unsigned long periodic_gc_runs; /* number of periodic GC runs */ 1291da177e4SLinus Torvalds unsigned long forced_gc_runs; /* number of forced GC runs */ 1309a6d276eSNeil Horman 1319a6d276eSNeil Horman unsigned long unres_discards; /* number of unresolved drops */ 132fb811395SRick Jones unsigned long table_fulls; /* times even gc couldn't help */ 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 1354ea7334bSChristoph Lameter #define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field) 1361da177e4SLinus Torvalds 137fd2c3ef7SEric Dumazet struct neighbour { 138767e97e1SEric Dumazet struct neighbour __rcu *next; 1391da177e4SLinus Torvalds struct neigh_table *tbl; 1401da177e4SLinus Torvalds struct neigh_parms *parms; 1411da177e4SLinus Torvalds unsigned long confirmed; 1421da177e4SLinus Torvalds unsigned long updated; 14346b13fc5SEric Dumazet rwlock_t lock; 1449f237430SReshetova, Elena refcount_t refcnt; 1458b5c171bSEric Dumazet unsigned int arp_queue_len_bytes; 1464b7cd11fSDavid Ahern struct sk_buff_head arp_queue; 147e37ef961SEric Dumazet struct timer_list timer; 148e37ef961SEric Dumazet unsigned long used; 1491da177e4SLinus Torvalds atomic_t probes; 1502c611ad9SRoopa Prabhu u8 nud_state; 1512c611ad9SRoopa Prabhu u8 type; 1522c611ad9SRoopa Prabhu u8 dead; 153df9b0e30SDavid Ahern u8 protocol; 1542c611ad9SRoopa Prabhu u32 flags; 1550ed8ddf4SEric Dumazet seqlock_t ha_lock; 1564b7cd11fSDavid Ahern unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))] __aligned(8); 157f6b72b62SDavid S. Miller struct hh_cache hh; 1588f40b161SDavid S. Miller int (*output)(struct neighbour *, struct sk_buff *); 15989d69d2bSStephen Hemminger const struct neigh_ops *ops; 16058956317SDavid Ahern struct list_head gc_list; 1617482e384SDaniel Borkmann struct list_head managed_list; 162767e97e1SEric Dumazet struct rcu_head rcu; 163e37ef961SEric Dumazet struct net_device *dev; 16485662c9fSEric Dumazet netdevice_tracker dev_tracker; 1651da177e4SLinus Torvalds u8 primary_key[0]; 1663859a271SKees Cook } __randomize_layout; 1671da177e4SLinus Torvalds 168fd2c3ef7SEric Dumazet struct neigh_ops { 1691da177e4SLinus Torvalds int family; 1701da177e4SLinus Torvalds void (*solicit)(struct neighbour *, struct sk_buff *); 1711da177e4SLinus Torvalds void (*error_report)(struct neighbour *, struct sk_buff *); 1728f40b161SDavid S. Miller int (*output)(struct neighbour *, struct sk_buff *); 1738f40b161SDavid S. Miller int (*connected_output)(struct neighbour *, struct sk_buff *); 1741da177e4SLinus Torvalds }; 1751da177e4SLinus Torvalds 176fd2c3ef7SEric Dumazet struct pneigh_entry { 1771da177e4SLinus Torvalds struct pneigh_entry *next; 1780c5c9fb5SEric W. Biederman possible_net_t net; 1791da177e4SLinus Torvalds struct net_device *dev; 18077a23b1fSEric Dumazet netdevice_tracker dev_tracker; 1812c611ad9SRoopa Prabhu u32 flags; 182df9b0e30SDavid Ahern u8 protocol; 183*ed779fe4SQingfang DENG u32 key[]; 1841da177e4SLinus Torvalds }; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* 1871da177e4SLinus Torvalds * neighbour table manipulation 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds 1902c2aba6cSDavid S. Miller #define NEIGH_NUM_HASH_RND 4 1912c2aba6cSDavid S. Miller 192d6bf7817SEric Dumazet struct neigh_hash_table { 193767e97e1SEric Dumazet struct neighbour __rcu **hash_buckets; 194cd089336SDavid S. Miller unsigned int hash_shift; 1952c2aba6cSDavid S. Miller __u32 hash_rnd[NEIGH_NUM_HASH_RND]; 196d6bf7817SEric Dumazet struct rcu_head rcu; 197d6bf7817SEric Dumazet }; 198d6bf7817SEric Dumazet 1991da177e4SLinus Torvalds 200fd2c3ef7SEric Dumazet struct neigh_table { 2011da177e4SLinus Torvalds int family; 202e451ae8eSAlexey Dobriyan unsigned int entry_size; 20301ccdf12SAlexey Dobriyan unsigned int key_len; 204bdf53c58SEric W. Biederman __be16 protocol; 205d6bf7817SEric Dumazet __u32 (*hash)(const void *pkey, 206d6bf7817SEric Dumazet const struct net_device *dev, 2072c2aba6cSDavid S. Miller __u32 *hash_rnd); 20860395a20SEric W. Biederman bool (*key_eq)(const struct neighbour *, const void *pkey); 2091da177e4SLinus Torvalds int (*constructor)(struct neighbour *); 2101da177e4SLinus Torvalds int (*pconstructor)(struct pneigh_entry *); 2111da177e4SLinus Torvalds void (*pdestructor)(struct pneigh_entry *); 2121da177e4SLinus Torvalds void (*proxy_redo)(struct sk_buff *skb); 2138cf8821eSJeff Dike int (*is_multicast)(const void *pkey); 214b8fb1ab4SDavid Ahern bool (*allow_add)(const struct net_device *dev, 215b8fb1ab4SDavid Ahern struct netlink_ext_ack *extack); 2161da177e4SLinus Torvalds char *id; 2171da177e4SLinus Torvalds struct neigh_parms parms; 21875fbfd33SNicolas Dichtel struct list_head parms_list; 2191da177e4SLinus Torvalds int gc_interval; 2201da177e4SLinus Torvalds int gc_thresh1; 2211da177e4SLinus Torvalds int gc_thresh2; 2221da177e4SLinus Torvalds int gc_thresh3; 2231da177e4SLinus Torvalds unsigned long last_flush; 224e4c4e448SEric Dumazet struct delayed_work gc_work; 2257482e384SDaniel Borkmann struct delayed_work managed_work; 2261da177e4SLinus Torvalds struct timer_list proxy_timer; 2271da177e4SLinus Torvalds struct sk_buff_head proxy_queue; 2281da177e4SLinus Torvalds atomic_t entries; 22958956317SDavid Ahern atomic_t gc_entries; 23058956317SDavid Ahern struct list_head gc_list; 2317482e384SDaniel Borkmann struct list_head managed_list; 2321da177e4SLinus Torvalds rwlock_t lock; 2331da177e4SLinus Torvalds unsigned long last_rand; 2347d720c3eSTejun Heo struct neigh_statistics __percpu *stats; 235d6bf7817SEric Dumazet struct neigh_hash_table __rcu *nht; 2361da177e4SLinus Torvalds struct pneigh_entry **phash_buckets; 2371da177e4SLinus Torvalds }; 2381da177e4SLinus Torvalds 239d7480fd3SWANG Cong enum { 240d7480fd3SWANG Cong NEIGH_ARP_TABLE = 0, 241d7480fd3SWANG Cong NEIGH_ND_TABLE = 1, 242d7480fd3SWANG Cong NEIGH_DN_TABLE = 2, 243d7480fd3SWANG Cong NEIGH_NR_TABLES, 244b79bda3dSEric W. Biederman NEIGH_LINK_TABLE = NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */ 245d7480fd3SWANG Cong }; 246d7480fd3SWANG Cong 24773af614aSJiri Pirko static inline int neigh_parms_family(struct neigh_parms *p) 24873af614aSJiri Pirko { 24973af614aSJiri Pirko return p->tbl->family; 25073af614aSJiri Pirko } 25173af614aSJiri Pirko 2521026fec8SDavid Miller #define NEIGH_PRIV_ALIGN sizeof(long long) 253daaba4faSYOSHIFUJI Hideaki / 吉藤英明 #define NEIGH_ENTRY_SIZE(size) ALIGN((size), NEIGH_PRIV_ALIGN) 2541026fec8SDavid Miller 2551026fec8SDavid Miller static inline void *neighbour_priv(const struct neighbour *n) 2561026fec8SDavid Miller { 25708433effSYOSHIFUJI Hideaki / 吉藤英明 return (char *)n + n->tbl->entry_size; 2581026fec8SDavid Miller } 2591026fec8SDavid Miller 2601da177e4SLinus Torvalds /* flags for neigh_update() */ 2617482e384SDaniel Borkmann #define NEIGH_UPDATE_F_OVERRIDE BIT(0) 2627482e384SDaniel Borkmann #define NEIGH_UPDATE_F_WEAK_OVERRIDE BIT(1) 2637482e384SDaniel Borkmann #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER BIT(2) 2647482e384SDaniel Borkmann #define NEIGH_UPDATE_F_USE BIT(3) 2657482e384SDaniel Borkmann #define NEIGH_UPDATE_F_MANAGED BIT(4) 2667482e384SDaniel Borkmann #define NEIGH_UPDATE_F_EXT_LEARNED BIT(5) 2677482e384SDaniel Borkmann #define NEIGH_UPDATE_F_ISROUTER BIT(6) 2687482e384SDaniel Borkmann #define NEIGH_UPDATE_F_ADMIN BIT(7) 2691da177e4SLinus Torvalds 2702c611ad9SRoopa Prabhu /* In-kernel representation for NDA_FLAGS_EXT flags: */ 2712c611ad9SRoopa Prabhu #define NTF_OLD_MASK 0xff 2722c611ad9SRoopa Prabhu #define NTF_EXT_SHIFT 8 2737482e384SDaniel Borkmann #define NTF_EXT_MASK (NTF_EXT_MANAGED) 2747482e384SDaniel Borkmann 2757482e384SDaniel Borkmann #define NTF_MANAGED (NTF_EXT_MANAGED << NTF_EXT_SHIFT) 2762c611ad9SRoopa Prabhu 27782cbb5c6SRoopa Prabhu extern const struct nla_policy nda_policy[]; 27860395a20SEric W. Biederman 27960395a20SEric W. Biederman static inline bool neigh_key_eq32(const struct neighbour *n, const void *pkey) 28060395a20SEric W. Biederman { 28160395a20SEric W. Biederman return *(const u32 *)n->primary_key == *(const u32 *)pkey; 28260395a20SEric W. Biederman } 28360395a20SEric W. Biederman 28460395a20SEric W. Biederman static inline bool neigh_key_eq128(const struct neighbour *n, const void *pkey) 28560395a20SEric W. Biederman { 28660395a20SEric W. Biederman const u32 *n32 = (const u32 *)n->primary_key; 28760395a20SEric W. Biederman const u32 *p32 = pkey; 28860395a20SEric W. Biederman 28960395a20SEric W. Biederman return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) | 29060395a20SEric W. Biederman (n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0; 29160395a20SEric W. Biederman } 29260395a20SEric W. Biederman 29360395a20SEric W. Biederman static inline struct neighbour *___neigh_lookup_noref( 29460395a20SEric W. Biederman struct neigh_table *tbl, 29560395a20SEric W. Biederman bool (*key_eq)(const struct neighbour *n, const void *pkey), 29660395a20SEric W. Biederman __u32 (*hash)(const void *pkey, 29760395a20SEric W. Biederman const struct net_device *dev, 29860395a20SEric W. Biederman __u32 *hash_rnd), 29960395a20SEric W. Biederman const void *pkey, 30060395a20SEric W. Biederman struct net_device *dev) 30160395a20SEric W. Biederman { 30209eed119SEric Dumazet struct neigh_hash_table *nht = rcu_dereference(tbl->nht); 30360395a20SEric W. Biederman struct neighbour *n; 30460395a20SEric W. Biederman u32 hash_val; 30560395a20SEric W. Biederman 30660395a20SEric W. Biederman hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 30709eed119SEric Dumazet for (n = rcu_dereference(nht->hash_buckets[hash_val]); 30860395a20SEric W. Biederman n != NULL; 30909eed119SEric Dumazet n = rcu_dereference(n->next)) { 31060395a20SEric W. Biederman if (n->dev == dev && key_eq(n, pkey)) 31160395a20SEric W. Biederman return n; 31260395a20SEric W. Biederman } 31360395a20SEric W. Biederman 31460395a20SEric W. Biederman return NULL; 31560395a20SEric W. Biederman } 31660395a20SEric W. Biederman 31760395a20SEric W. Biederman static inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl, 31860395a20SEric W. Biederman const void *pkey, 31960395a20SEric W. Biederman struct net_device *dev) 32060395a20SEric W. Biederman { 32160395a20SEric W. Biederman return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev); 32260395a20SEric W. Biederman } 32360395a20SEric W. Biederman 3241e84dc6bSYajun Deng static inline void neigh_confirm(struct neighbour *n) 3251e84dc6bSYajun Deng { 3261e84dc6bSYajun Deng if (n) { 3271e84dc6bSYajun Deng unsigned long now = jiffies; 3281e84dc6bSYajun Deng 3291e84dc6bSYajun Deng /* avoid dirtying neighbour */ 3301e84dc6bSYajun Deng if (READ_ONCE(n->confirmed) != now) 3311e84dc6bSYajun Deng WRITE_ONCE(n->confirmed, now); 3321e84dc6bSYajun Deng } 3331e84dc6bSYajun Deng } 3341e84dc6bSYajun Deng 335d7480fd3SWANG Cong void neigh_table_init(int index, struct neigh_table *tbl); 336d7480fd3SWANG Cong int neigh_table_clear(int index, struct neigh_table *tbl); 33790972b22SJoe Perches struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, 3381da177e4SLinus Torvalds struct net_device *dev); 33990972b22SJoe Perches struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey, 34090972b22SJoe Perches struct net_device *dev, bool want_ref); 341a263b309SDavid S. Miller static inline struct neighbour *neigh_create(struct neigh_table *tbl, 342a263b309SDavid S. Miller const void *pkey, 343a263b309SDavid S. Miller struct net_device *dev) 344a263b309SDavid S. Miller { 345a263b309SDavid S. Miller return __neigh_create(tbl, pkey, dev, true); 346a263b309SDavid S. Miller } 34790972b22SJoe Perches void neigh_destroy(struct neighbour *neigh); 3484a81f6daSDaniel Borkmann int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb, 3494a81f6daSDaniel Borkmann const bool immediate_ok); 3507b8f7a40SRoopa Prabhu int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags, 3517b8f7a40SRoopa Prabhu u32 nlmsg_pid); 3527e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh); 3535071034eSSowmini Varadhan bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl); 35490972b22SJoe Perches void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev); 35590972b22SJoe Perches int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev); 356859bd2efSDavid Ahern int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev); 35790972b22SJoe Perches int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb); 35890972b22SJoe Perches int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb); 35990972b22SJoe Perches int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb); 36090972b22SJoe Perches struct neighbour *neigh_event_ns(struct neigh_table *tbl, 3611da177e4SLinus Torvalds u8 *lladdr, void *saddr, 3621da177e4SLinus Torvalds struct net_device *dev); 3631da177e4SLinus Torvalds 36490972b22SJoe Perches struct neigh_parms *neigh_parms_alloc(struct net_device *dev, 36590972b22SJoe Perches struct neigh_table *tbl); 36690972b22SJoe Perches void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms); 36757da52c1SYOSHIFUJI Hideaki 36857da52c1SYOSHIFUJI Hideaki static inline 36957da52c1SYOSHIFUJI Hideaki struct net *neigh_parms_net(const struct neigh_parms *parms) 37057da52c1SYOSHIFUJI Hideaki { 371e42ea986SEric Dumazet return read_pnet(&parms->net); 37257da52c1SYOSHIFUJI Hideaki } 37357da52c1SYOSHIFUJI Hideaki 37490972b22SJoe Perches unsigned long neigh_rand_reach_time(unsigned long base); 3751da177e4SLinus Torvalds 37690972b22SJoe Perches void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, 3771da177e4SLinus Torvalds struct sk_buff *skb); 37890972b22SJoe Perches struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net, 37990972b22SJoe Perches const void *key, struct net_device *dev, 38090972b22SJoe Perches int creat); 38190972b22SJoe Perches struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, struct net *net, 38290972b22SJoe Perches const void *key, struct net_device *dev); 38390972b22SJoe Perches int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key, 384fa86d322SPavel Emelyanov struct net_device *dev); 3851da177e4SLinus Torvalds 38690972b22SJoe Perches static inline struct net *pneigh_net(const struct pneigh_entry *pneigh) 38757da52c1SYOSHIFUJI Hideaki { 388e42ea986SEric Dumazet return read_pnet(&pneigh->net); 38957da52c1SYOSHIFUJI Hideaki } 39057da52c1SYOSHIFUJI Hideaki 39190972b22SJoe Perches void neigh_app_ns(struct neighbour *n); 39290972b22SJoe Perches void neigh_for_each(struct neigh_table *tbl, 39390972b22SJoe Perches void (*cb)(struct neighbour *, void *), void *cookie); 39490972b22SJoe Perches void __neigh_for_each_release(struct neigh_table *tbl, 39590972b22SJoe Perches int (*cb)(struct neighbour *)); 3964fd3d7d9SEric W. Biederman int neigh_xmit(int fam, struct net_device *, const void *, struct sk_buff *); 39790972b22SJoe Perches void pneigh_for_each(struct neigh_table *tbl, 39890972b22SJoe Perches void (*cb)(struct pneigh_entry *)); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds struct neigh_seq_state { 40142508461SDenis V. Lunev struct seq_net_private p; 4021da177e4SLinus Torvalds struct neigh_table *tbl; 403d6bf7817SEric Dumazet struct neigh_hash_table *nht; 4041da177e4SLinus Torvalds void *(*neigh_sub_iter)(struct neigh_seq_state *state, 4051da177e4SLinus Torvalds struct neighbour *n, loff_t *pos); 4061da177e4SLinus Torvalds unsigned int bucket; 4071da177e4SLinus Torvalds unsigned int flags; 4081da177e4SLinus Torvalds #define NEIGH_SEQ_NEIGH_ONLY 0x00000001 4091da177e4SLinus Torvalds #define NEIGH_SEQ_IS_PNEIGH 0x00000002 4101da177e4SLinus Torvalds #define NEIGH_SEQ_SKIP_NOARP 0x00000004 4111da177e4SLinus Torvalds }; 41290972b22SJoe Perches void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, 41390972b22SJoe Perches unsigned int); 41490972b22SJoe Perches void *neigh_seq_next(struct seq_file *, void *, loff_t *); 41590972b22SJoe Perches void neigh_seq_stop(struct seq_file *, void *); 4161da177e4SLinus Torvalds 417cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write, 418c7388c1fSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos); 419cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write, 420c7388c1fSChristoph Hellwig void *buffer, 421cb5b09c1SJiri Pirko size_t *lenp, loff_t *ppos); 422cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write, 423c7388c1fSChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos); 424cb5b09c1SJiri Pirko 42590972b22SJoe Perches int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, 42673af614aSJiri Pirko proc_handler *proc_handler); 42790972b22SJoe Perches void neigh_sysctl_unregister(struct neigh_parms *p); 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds static inline void __neigh_parms_put(struct neigh_parms *parms) 4301da177e4SLinus Torvalds { 4316343944bSReshetova, Elena refcount_dec(&parms->refcnt); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms) 4351da177e4SLinus Torvalds { 4366343944bSReshetova, Elena refcount_inc(&parms->refcnt); 4371da177e4SLinus Torvalds return parms; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* 4411da177e4SLinus Torvalds * Neighbour references 4421da177e4SLinus Torvalds */ 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds static inline void neigh_release(struct neighbour *neigh) 4451da177e4SLinus Torvalds { 4469f237430SReshetova, Elena if (refcount_dec_and_test(&neigh->refcnt)) 4471da177e4SLinus Torvalds neigh_destroy(neigh); 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds static inline struct neighbour * neigh_clone(struct neighbour *neigh) 4511da177e4SLinus Torvalds { 4521da177e4SLinus Torvalds if (neigh) 4539f237430SReshetova, Elena refcount_inc(&neigh->refcnt); 4541da177e4SLinus Torvalds return neigh; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4579f237430SReshetova, Elena #define neigh_hold(n) refcount_inc(&(n)->refcnt) 4581da177e4SLinus Torvalds 4594a81f6daSDaniel Borkmann static __always_inline int neigh_event_send_probe(struct neighbour *neigh, 4604a81f6daSDaniel Borkmann struct sk_buff *skb, 4614a81f6daSDaniel Borkmann const bool immediate_ok) 4621da177e4SLinus Torvalds { 463ef22b7b6STetsuo Handa unsigned long now = jiffies; 4640ed8ddf4SEric Dumazet 4651b53d644SEric Dumazet if (READ_ONCE(neigh->used) != now) 4661b53d644SEric Dumazet WRITE_ONCE(neigh->used, now); 467b071af52SEric Dumazet if (!(READ_ONCE(neigh->nud_state) & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))) 4684a81f6daSDaniel Borkmann return __neigh_event_send(neigh, skb, immediate_ok); 4691da177e4SLinus Torvalds return 0; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4724a81f6daSDaniel Borkmann static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) 4734a81f6daSDaniel Borkmann { 4744a81f6daSDaniel Borkmann return neigh_event_send_probe(neigh, skb, true); 4754a81f6daSDaniel Borkmann } 4764a81f6daSDaniel Borkmann 47734666d46SPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 478e179e632SBart De Schuymer static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb) 479e179e632SBart De Schuymer { 48095c96174SEric Dumazet unsigned int seq, hh_alen; 481e179e632SBart De Schuymer 482e179e632SBart De Schuymer do { 483e179e632SBart De Schuymer seq = read_seqbegin(&hh->hh_lock); 484e179e632SBart De Schuymer hh_alen = HH_DATA_ALIGN(ETH_HLEN); 485e179e632SBart De Schuymer memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN); 486e179e632SBart De Schuymer } while (read_seqretry(&hh->hh_lock, seq)); 487e179e632SBart De Schuymer return 0; 488e179e632SBart De Schuymer } 489e179e632SBart De Schuymer #endif 490e179e632SBart De Schuymer 491425f09abSEric Dumazet static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb) 4923644f0ceSStephen Hemminger { 493e6ac64d4SStefano Brivio unsigned int hh_alen = 0; 49495c96174SEric Dumazet unsigned int seq; 4955b3dc2f3SAlexey Dobriyan unsigned int hh_len; 4963644f0ceSStephen Hemminger 4973644f0ceSStephen Hemminger do { 4983644f0ceSStephen Hemminger seq = read_seqbegin(&hh->hh_lock); 499c305c6aeSEric Dumazet hh_len = READ_ONCE(hh->hh_len); 500425f09abSEric Dumazet if (likely(hh_len <= HH_DATA_MOD)) { 501e6ac64d4SStefano Brivio hh_alen = HH_DATA_MOD; 502425f09abSEric Dumazet 503e6ac64d4SStefano Brivio /* skb_push() would proceed silently if we have room for 504e6ac64d4SStefano Brivio * the unaligned size but not for the aligned size: 505e6ac64d4SStefano Brivio * check headroom explicitly. 506e6ac64d4SStefano Brivio */ 507e6ac64d4SStefano Brivio if (likely(skb_headroom(skb) >= HH_DATA_MOD)) { 508e6ac64d4SStefano Brivio /* this is inlined by gcc */ 509e6ac64d4SStefano Brivio memcpy(skb->data - HH_DATA_MOD, hh->hh_data, 510e6ac64d4SStefano Brivio HH_DATA_MOD); 511e6ac64d4SStefano Brivio } 512e6ac64d4SStefano Brivio } else { 513e6ac64d4SStefano Brivio hh_alen = HH_DATA_ALIGN(hh_len); 514e6ac64d4SStefano Brivio 515e6ac64d4SStefano Brivio if (likely(skb_headroom(skb) >= hh_alen)) { 516e6ac64d4SStefano Brivio memcpy(skb->data - hh_alen, hh->hh_data, 517e6ac64d4SStefano Brivio hh_alen); 518e6ac64d4SStefano Brivio } 519425f09abSEric Dumazet } 5203644f0ceSStephen Hemminger } while (read_seqretry(&hh->hh_lock, seq)); 5213644f0ceSStephen Hemminger 522e6ac64d4SStefano Brivio if (WARN_ON_ONCE(skb_headroom(skb) < hh_alen)) { 523e6ac64d4SStefano Brivio kfree_skb(skb); 524e6ac64d4SStefano Brivio return NET_XMIT_DROP; 525e6ac64d4SStefano Brivio } 526e6ac64d4SStefano Brivio 527e6ac64d4SStefano Brivio __skb_push(skb, hh_len); 528b23b5455SDavid S. Miller return dev_queue_xmit(skb); 5293644f0ceSStephen Hemminger } 5303644f0ceSStephen Hemminger 5310353f282SDavid Ahern static inline int neigh_output(struct neighbour *n, struct sk_buff *skb, 5320353f282SDavid Ahern bool skip_cache) 533c16ec185SJulian Anastasov { 534c16ec185SJulian Anastasov const struct hh_cache *hh = &n->hh; 535c16ec185SJulian Anastasov 536d18785e2SEric Dumazet /* n->nud_state and hh->hh_len could be changed under us. 537d18785e2SEric Dumazet * neigh_hh_output() is taking care of the race later. 538d18785e2SEric Dumazet */ 539d18785e2SEric Dumazet if (!skip_cache && 540d18785e2SEric Dumazet (READ_ONCE(n->nud_state) & NUD_CONNECTED) && 541d18785e2SEric Dumazet READ_ONCE(hh->hh_len)) 542c16ec185SJulian Anastasov return neigh_hh_output(hh, skb); 543d18785e2SEric Dumazet 544c16ec185SJulian Anastasov return n->output(n, skb); 545c16ec185SJulian Anastasov } 546c16ec185SJulian Anastasov 5471da177e4SLinus Torvalds static inline struct neighbour * 5481da177e4SLinus Torvalds __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds struct neighbour *n = neigh_lookup(tbl, pkey, dev); 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds if (n || !creat) 5531da177e4SLinus Torvalds return n; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds n = neigh_create(tbl, pkey, dev); 5561da177e4SLinus Torvalds return IS_ERR(n) ? NULL : n; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds static inline struct neighbour * 5601da177e4SLinus Torvalds __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey, 5611da177e4SLinus Torvalds struct net_device *dev) 5621da177e4SLinus Torvalds { 5631da177e4SLinus Torvalds struct neighbour *n = neigh_lookup(tbl, pkey, dev); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds if (n) 5661da177e4SLinus Torvalds return n; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds return neigh_create(tbl, pkey, dev); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 571a61bbcf2SPatrick McHardy struct neighbour_cb { 572a61bbcf2SPatrick McHardy unsigned long sched_next; 573a61bbcf2SPatrick McHardy unsigned int flags; 574a61bbcf2SPatrick McHardy }; 575a61bbcf2SPatrick McHardy 576a61bbcf2SPatrick McHardy #define LOCALLY_ENQUEUED 0x1 577a61bbcf2SPatrick McHardy 578a61bbcf2SPatrick McHardy #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb) 5791da177e4SLinus Torvalds 5800ed8ddf4SEric Dumazet static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n, 5810ed8ddf4SEric Dumazet const struct net_device *dev) 5820ed8ddf4SEric Dumazet { 5830ed8ddf4SEric Dumazet unsigned int seq; 5840ed8ddf4SEric Dumazet 5850ed8ddf4SEric Dumazet do { 5860ed8ddf4SEric Dumazet seq = read_seqbegin(&n->ha_lock); 5870ed8ddf4SEric Dumazet memcpy(dst, n->ha, dev->addr_len); 5880ed8ddf4SEric Dumazet } while (read_seqretry(&n->ha_lock, seq)); 5890ed8ddf4SEric Dumazet } 5904fd3d7d9SEric W. Biederman 591fc6e8073SRoopa Prabhu static inline void neigh_update_is_router(struct neighbour *neigh, u32 flags, 592fc6e8073SRoopa Prabhu int *notify) 593fc6e8073SRoopa Prabhu { 594fc6e8073SRoopa Prabhu u8 ndm_flags = 0; 595fc6e8073SRoopa Prabhu 596fc6e8073SRoopa Prabhu ndm_flags |= (flags & NEIGH_UPDATE_F_ISROUTER) ? NTF_ROUTER : 0; 597fc6e8073SRoopa Prabhu if ((neigh->flags ^ ndm_flags) & NTF_ROUTER) { 598fc6e8073SRoopa Prabhu if (ndm_flags & NTF_ROUTER) 599fc6e8073SRoopa Prabhu neigh->flags |= NTF_ROUTER; 600fc6e8073SRoopa Prabhu else 601fc6e8073SRoopa Prabhu neigh->flags &= ~NTF_ROUTER; 602fc6e8073SRoopa Prabhu *notify = 1; 603fc6e8073SRoopa Prabhu } 604fc6e8073SRoopa Prabhu } 6051da177e4SLinus Torvalds #endif 606