xref: /openbmc/linux/include/net/neighbour.h (revision 89740ca7)
11da177e4SLinus Torvalds #ifndef _NET_NEIGHBOUR_H
21da177e4SLinus Torvalds #define _NET_NEIGHBOUR_H
31da177e4SLinus Torvalds 
49067c722SThomas Graf #include <linux/neighbour.h>
59067c722SThomas Graf 
61da177e4SLinus Torvalds /*
71da177e4SLinus Torvalds  *	Generic neighbour manipulation
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Authors:
101da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
111da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * 	Changes:
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Harald Welte:		<laforge@gnumonks.org>
161da177e4SLinus Torvalds  *		- Add neighbour cache statistics like rtstat
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
1960063497SArun Sharma #include <linux/atomic.h>
201da177e4SLinus Torvalds #include <linux/netdevice.h>
2114c85021SArnaldo Carvalho de Melo #include <linux/skbuff.h>
221da177e4SLinus Torvalds #include <linux/rcupdate.h>
231da177e4SLinus Torvalds #include <linux/seq_file.h>
241d4c8c29SJiri Pirko #include <linux/bitmap.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/err.h>
271da177e4SLinus Torvalds #include <linux/sysctl.h>
28e4c4e448SEric Dumazet #include <linux/workqueue.h>
29c8822a4eSThomas Graf #include <net/rtnetlink.h>
301da177e4SLinus Torvalds 
3139971554SPavel Emelyanov /*
3239971554SPavel Emelyanov  * NUD stands for "neighbor unreachability detection"
3339971554SPavel Emelyanov  */
3439971554SPavel Emelyanov 
351da177e4SLinus Torvalds #define NUD_IN_TIMER	(NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
361da177e4SLinus Torvalds #define NUD_VALID	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
371da177e4SLinus Torvalds #define NUD_CONNECTED	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds struct neighbour;
401da177e4SLinus Torvalds 
411f9248e5SJiri Pirko enum {
421f9248e5SJiri Pirko 	NEIGH_VAR_MCAST_PROBES,
431f9248e5SJiri Pirko 	NEIGH_VAR_UCAST_PROBES,
441f9248e5SJiri Pirko 	NEIGH_VAR_APP_PROBES,
451f9248e5SJiri Pirko 	NEIGH_VAR_RETRANS_TIME,
461f9248e5SJiri Pirko 	NEIGH_VAR_BASE_REACHABLE_TIME,
471f9248e5SJiri Pirko 	NEIGH_VAR_DELAY_PROBE_TIME,
481f9248e5SJiri Pirko 	NEIGH_VAR_GC_STALETIME,
491f9248e5SJiri Pirko 	NEIGH_VAR_QUEUE_LEN_BYTES,
501f9248e5SJiri Pirko 	NEIGH_VAR_PROXY_QLEN,
511f9248e5SJiri Pirko 	NEIGH_VAR_ANYCAST_DELAY,
521f9248e5SJiri Pirko 	NEIGH_VAR_PROXY_DELAY,
531f9248e5SJiri Pirko 	NEIGH_VAR_LOCKTIME,
541f9248e5SJiri Pirko #define NEIGH_VAR_DATA_MAX (NEIGH_VAR_LOCKTIME + 1)
551f9248e5SJiri Pirko 	/* Following are used as a second way to access one of the above */
561f9248e5SJiri Pirko 	NEIGH_VAR_QUEUE_LEN, /* same data as NEIGH_VAR_QUEUE_LEN_BYTES */
571f9248e5SJiri Pirko 	NEIGH_VAR_RETRANS_TIME_MS, /* same data as NEIGH_VAR_RETRANS_TIME */
581f9248e5SJiri Pirko 	NEIGH_VAR_BASE_REACHABLE_TIME_MS, /* same data as NEIGH_VAR_BASE_REACHABLE_TIME */
591f9248e5SJiri Pirko 	/* Following are used by "default" only */
601f9248e5SJiri Pirko 	NEIGH_VAR_GC_INTERVAL,
611f9248e5SJiri Pirko 	NEIGH_VAR_GC_THRESH1,
621f9248e5SJiri Pirko 	NEIGH_VAR_GC_THRESH2,
631f9248e5SJiri Pirko 	NEIGH_VAR_GC_THRESH3,
641f9248e5SJiri Pirko 	NEIGH_VAR_MAX
651f9248e5SJiri Pirko };
661f9248e5SJiri Pirko 
67fd2c3ef7SEric Dumazet struct neigh_parms {
6857da52c1SYOSHIFUJI Hideaki #ifdef CONFIG_NET_NS
69426b5303SEric W. Biederman 	struct net *net;
7057da52c1SYOSHIFUJI Hideaki #endif
71c7fb64dbSThomas Graf 	struct net_device *dev;
721da177e4SLinus Torvalds 	struct neigh_parms *next;
73447f2191SDavid S. Miller 	int	(*neigh_setup)(struct neighbour *);
74ecbb4169SAlexey Kuznetsov 	void	(*neigh_cleanup)(struct neighbour *);
751da177e4SLinus Torvalds 	struct neigh_table *tbl;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	void	*sysctl_table;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	int dead;
801da177e4SLinus Torvalds 	atomic_t refcnt;
811da177e4SLinus Torvalds 	struct rcu_head rcu_head;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	int	reachable_time;
841f9248e5SJiri Pirko 	int	data[NEIGH_VAR_DATA_MAX];
851d4c8c29SJiri Pirko 	DECLARE_BITMAP(data_state, NEIGH_VAR_DATA_MAX);
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
881f9248e5SJiri Pirko static inline void neigh_var_set(struct neigh_parms *p, int index, int val)
891f9248e5SJiri Pirko {
901d4c8c29SJiri Pirko 	set_bit(index, p->data_state);
911f9248e5SJiri Pirko 	p->data[index] = val;
921f9248e5SJiri Pirko }
931f9248e5SJiri Pirko 
941f9248e5SJiri Pirko #define NEIGH_VAR(p, attr) ((p)->data[NEIGH_VAR_ ## attr])
9589740ca7SJiri Pirko 
9689740ca7SJiri Pirko /* In ndo_neigh_setup, NEIGH_VAR_INIT should be used.
9789740ca7SJiri Pirko  * In other cases, NEIGH_VAR_SET should be used.
9889740ca7SJiri Pirko  */
9989740ca7SJiri Pirko #define NEIGH_VAR_INIT(p, attr, val) (NEIGH_VAR(p, attr) = val)
1001f9248e5SJiri Pirko #define NEIGH_VAR_SET(p, attr, val) neigh_var_set(p, NEIGH_VAR_ ## attr, val)
1011f9248e5SJiri Pirko 
1021d4c8c29SJiri Pirko static inline void neigh_parms_data_state_setall(struct neigh_parms *p)
1031d4c8c29SJiri Pirko {
1041d4c8c29SJiri Pirko 	bitmap_fill(p->data_state, NEIGH_VAR_DATA_MAX);
1051d4c8c29SJiri Pirko }
1061d4c8c29SJiri Pirko 
1071d4c8c29SJiri Pirko static inline void neigh_parms_data_state_cleanall(struct neigh_parms *p)
1081d4c8c29SJiri Pirko {
1091d4c8c29SJiri Pirko 	bitmap_zero(p->data_state, NEIGH_VAR_DATA_MAX);
1101d4c8c29SJiri Pirko }
1111d4c8c29SJiri Pirko 
112fd2c3ef7SEric Dumazet struct neigh_statistics {
1131da177e4SLinus Torvalds 	unsigned long allocs;		/* number of allocated neighs */
1141da177e4SLinus Torvalds 	unsigned long destroys;		/* number of destroyed neighs */
1151da177e4SLinus Torvalds 	unsigned long hash_grows;	/* number of hash resizes */
1161da177e4SLinus Torvalds 
1179a6d276eSNeil Horman 	unsigned long res_failed;	/* number of failed resolutions */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	unsigned long lookups;		/* number of lookups */
1201da177e4SLinus Torvalds 	unsigned long hits;		/* number of hits (among lookups) */
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	unsigned long rcv_probes_mcast;	/* number of received mcast ipv6 */
1231da177e4SLinus Torvalds 	unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	unsigned long periodic_gc_runs;	/* number of periodic GC runs */
1261da177e4SLinus Torvalds 	unsigned long forced_gc_runs;	/* number of forced GC runs */
1279a6d276eSNeil Horman 
1289a6d276eSNeil Horman 	unsigned long unres_discards;	/* number of unresolved drops */
1291da177e4SLinus Torvalds };
1301da177e4SLinus Torvalds 
1314ea7334bSChristoph Lameter #define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
1321da177e4SLinus Torvalds 
133fd2c3ef7SEric Dumazet struct neighbour {
134767e97e1SEric Dumazet 	struct neighbour __rcu	*next;
1351da177e4SLinus Torvalds 	struct neigh_table	*tbl;
1361da177e4SLinus Torvalds 	struct neigh_parms	*parms;
1371da177e4SLinus Torvalds 	unsigned long		confirmed;
1381da177e4SLinus Torvalds 	unsigned long		updated;
13946b13fc5SEric Dumazet 	rwlock_t		lock;
140367e5e37SEric Dumazet 	atomic_t		refcnt;
141e37ef961SEric Dumazet 	struct sk_buff_head	arp_queue;
1428b5c171bSEric Dumazet 	unsigned int		arp_queue_len_bytes;
143e37ef961SEric Dumazet 	struct timer_list	timer;
144e37ef961SEric Dumazet 	unsigned long		used;
1451da177e4SLinus Torvalds 	atomic_t		probes;
14646b13fc5SEric Dumazet 	__u8			flags;
14746b13fc5SEric Dumazet 	__u8			nud_state;
14846b13fc5SEric Dumazet 	__u8			type;
14946b13fc5SEric Dumazet 	__u8			dead;
1500ed8ddf4SEric Dumazet 	seqlock_t		ha_lock;
151d924424aSStephen Hemminger 	unsigned char		ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
152f6b72b62SDavid S. Miller 	struct hh_cache		hh;
1538f40b161SDavid S. Miller 	int			(*output)(struct neighbour *, struct sk_buff *);
15489d69d2bSStephen Hemminger 	const struct neigh_ops	*ops;
155767e97e1SEric Dumazet 	struct rcu_head		rcu;
156e37ef961SEric Dumazet 	struct net_device	*dev;
1571da177e4SLinus Torvalds 	u8			primary_key[0];
1581da177e4SLinus Torvalds };
1591da177e4SLinus Torvalds 
160fd2c3ef7SEric Dumazet struct neigh_ops {
1611da177e4SLinus Torvalds 	int			family;
1621da177e4SLinus Torvalds 	void			(*solicit)(struct neighbour *, struct sk_buff *);
1631da177e4SLinus Torvalds 	void			(*error_report)(struct neighbour *, struct sk_buff *);
1648f40b161SDavid S. Miller 	int			(*output)(struct neighbour *, struct sk_buff *);
1658f40b161SDavid S. Miller 	int			(*connected_output)(struct neighbour *, struct sk_buff *);
1661da177e4SLinus Torvalds };
1671da177e4SLinus Torvalds 
168fd2c3ef7SEric Dumazet struct pneigh_entry {
1691da177e4SLinus Torvalds 	struct pneigh_entry	*next;
17057da52c1SYOSHIFUJI Hideaki #ifdef CONFIG_NET_NS
171426b5303SEric W. Biederman 	struct net		*net;
17257da52c1SYOSHIFUJI Hideaki #endif
1731da177e4SLinus Torvalds 	struct net_device	*dev;
17462dd9318SVille Nuorvala 	u8			flags;
1751da177e4SLinus Torvalds 	u8			key[0];
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /*
1791da177e4SLinus Torvalds  *	neighbour table manipulation
1801da177e4SLinus Torvalds  */
1811da177e4SLinus Torvalds 
1822c2aba6cSDavid S. Miller #define NEIGH_NUM_HASH_RND	4
1832c2aba6cSDavid S. Miller 
184d6bf7817SEric Dumazet struct neigh_hash_table {
185767e97e1SEric Dumazet 	struct neighbour __rcu	**hash_buckets;
186cd089336SDavid S. Miller 	unsigned int		hash_shift;
1872c2aba6cSDavid S. Miller 	__u32			hash_rnd[NEIGH_NUM_HASH_RND];
188d6bf7817SEric Dumazet 	struct rcu_head		rcu;
189d6bf7817SEric Dumazet };
190d6bf7817SEric Dumazet 
1911da177e4SLinus Torvalds 
192fd2c3ef7SEric Dumazet struct neigh_table {
1931da177e4SLinus Torvalds 	struct neigh_table	*next;
1941da177e4SLinus Torvalds 	int			family;
1951da177e4SLinus Torvalds 	int			entry_size;
1961da177e4SLinus Torvalds 	int			key_len;
197d6bf7817SEric Dumazet 	__u32			(*hash)(const void *pkey,
198d6bf7817SEric Dumazet 					const struct net_device *dev,
1992c2aba6cSDavid S. Miller 					__u32 *hash_rnd);
2001da177e4SLinus Torvalds 	int			(*constructor)(struct neighbour *);
2011da177e4SLinus Torvalds 	int			(*pconstructor)(struct pneigh_entry *);
2021da177e4SLinus Torvalds 	void			(*pdestructor)(struct pneigh_entry *);
2031da177e4SLinus Torvalds 	void			(*proxy_redo)(struct sk_buff *skb);
2041da177e4SLinus Torvalds 	char			*id;
2051da177e4SLinus Torvalds 	struct neigh_parms	parms;
206787a3445SKulikov Vasiliy 	/* HACK. gc_* should follow parms without a gap! */
2071da177e4SLinus Torvalds 	int			gc_interval;
2081da177e4SLinus Torvalds 	int			gc_thresh1;
2091da177e4SLinus Torvalds 	int			gc_thresh2;
2101da177e4SLinus Torvalds 	int			gc_thresh3;
2111da177e4SLinus Torvalds 	unsigned long		last_flush;
212e4c4e448SEric Dumazet 	struct delayed_work	gc_work;
2131da177e4SLinus Torvalds 	struct timer_list 	proxy_timer;
2141da177e4SLinus Torvalds 	struct sk_buff_head	proxy_queue;
2151da177e4SLinus Torvalds 	atomic_t		entries;
2161da177e4SLinus Torvalds 	rwlock_t		lock;
2171da177e4SLinus Torvalds 	unsigned long		last_rand;
2187d720c3eSTejun Heo 	struct neigh_statistics	__percpu *stats;
219d6bf7817SEric Dumazet 	struct neigh_hash_table __rcu *nht;
2201da177e4SLinus Torvalds 	struct pneigh_entry	**phash_buckets;
2211da177e4SLinus Torvalds };
2221da177e4SLinus Torvalds 
22373af614aSJiri Pirko static inline int neigh_parms_family(struct neigh_parms *p)
22473af614aSJiri Pirko {
22573af614aSJiri Pirko 	return p->tbl->family;
22673af614aSJiri Pirko }
22773af614aSJiri Pirko 
2281026fec8SDavid Miller #define NEIGH_PRIV_ALIGN	sizeof(long long)
229daaba4faSYOSHIFUJI Hideaki / 吉藤英明 #define NEIGH_ENTRY_SIZE(size)	ALIGN((size), NEIGH_PRIV_ALIGN)
2301026fec8SDavid Miller 
2311026fec8SDavid Miller static inline void *neighbour_priv(const struct neighbour *n)
2321026fec8SDavid Miller {
23308433effSYOSHIFUJI Hideaki / 吉藤英明 	return (char *)n + n->tbl->entry_size;
2341026fec8SDavid Miller }
2351026fec8SDavid Miller 
2361da177e4SLinus Torvalds /* flags for neigh_update() */
2371da177e4SLinus Torvalds #define NEIGH_UPDATE_F_OVERRIDE			0x00000001
2381da177e4SLinus Torvalds #define NEIGH_UPDATE_F_WEAK_OVERRIDE		0x00000002
2391da177e4SLinus Torvalds #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER	0x00000004
2401da177e4SLinus Torvalds #define NEIGH_UPDATE_F_ISROUTER			0x40000000
2411da177e4SLinus Torvalds #define NEIGH_UPDATE_F_ADMIN			0x80000000
2421da177e4SLinus Torvalds 
24390972b22SJoe Perches void neigh_table_init(struct neigh_table *tbl);
24490972b22SJoe Perches int neigh_table_clear(struct neigh_table *tbl);
24590972b22SJoe Perches struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
2461da177e4SLinus Torvalds 			       struct net_device *dev);
24790972b22SJoe Perches struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
2481da177e4SLinus Torvalds 				     const void *pkey);
24990972b22SJoe Perches struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
25090972b22SJoe Perches 				 struct net_device *dev, bool want_ref);
251a263b309SDavid S. Miller static inline struct neighbour *neigh_create(struct neigh_table *tbl,
252a263b309SDavid S. Miller 					     const void *pkey,
253a263b309SDavid S. Miller 					     struct net_device *dev)
254a263b309SDavid S. Miller {
255a263b309SDavid S. Miller 	return __neigh_create(tbl, pkey, dev, true);
256a263b309SDavid S. Miller }
25790972b22SJoe Perches void neigh_destroy(struct neighbour *neigh);
25890972b22SJoe Perches int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
25990972b22SJoe Perches int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags);
2607e980569SJiri Benc void __neigh_set_probe_once(struct neighbour *neigh);
26190972b22SJoe Perches void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
26290972b22SJoe Perches int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
26390972b22SJoe Perches int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb);
26490972b22SJoe Perches int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb);
26590972b22SJoe Perches int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb);
26690972b22SJoe Perches int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb);
26790972b22SJoe Perches struct neighbour *neigh_event_ns(struct neigh_table *tbl,
2681da177e4SLinus Torvalds 						u8 *lladdr, void *saddr,
2691da177e4SLinus Torvalds 						struct net_device *dev);
2701da177e4SLinus Torvalds 
27190972b22SJoe Perches struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
27290972b22SJoe Perches 				      struct neigh_table *tbl);
27390972b22SJoe Perches void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
27457da52c1SYOSHIFUJI Hideaki 
27557da52c1SYOSHIFUJI Hideaki static inline
27657da52c1SYOSHIFUJI Hideaki struct net *neigh_parms_net(const struct neigh_parms *parms)
27757da52c1SYOSHIFUJI Hideaki {
278e42ea986SEric Dumazet 	return read_pnet(&parms->net);
27957da52c1SYOSHIFUJI Hideaki }
28057da52c1SYOSHIFUJI Hideaki 
28190972b22SJoe Perches unsigned long neigh_rand_reach_time(unsigned long base);
2821da177e4SLinus Torvalds 
28390972b22SJoe Perches void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
2841da177e4SLinus Torvalds 		    struct sk_buff *skb);
28590972b22SJoe Perches struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net,
28690972b22SJoe Perches 				   const void *key, struct net_device *dev,
28790972b22SJoe Perches 				   int creat);
28890972b22SJoe Perches struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, struct net *net,
28990972b22SJoe Perches 				     const void *key, struct net_device *dev);
29090972b22SJoe Perches int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key,
291fa86d322SPavel Emelyanov 		  struct net_device *dev);
2921da177e4SLinus Torvalds 
29390972b22SJoe Perches static inline struct net *pneigh_net(const struct pneigh_entry *pneigh)
29457da52c1SYOSHIFUJI Hideaki {
295e42ea986SEric Dumazet 	return read_pnet(&pneigh->net);
29657da52c1SYOSHIFUJI Hideaki }
29757da52c1SYOSHIFUJI Hideaki 
29890972b22SJoe Perches void neigh_app_ns(struct neighbour *n);
29990972b22SJoe Perches void neigh_for_each(struct neigh_table *tbl,
30090972b22SJoe Perches 		    void (*cb)(struct neighbour *, void *), void *cookie);
30190972b22SJoe Perches void __neigh_for_each_release(struct neigh_table *tbl,
30290972b22SJoe Perches 			      int (*cb)(struct neighbour *));
30390972b22SJoe Perches void pneigh_for_each(struct neigh_table *tbl,
30490972b22SJoe Perches 		     void (*cb)(struct pneigh_entry *));
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds struct neigh_seq_state {
30742508461SDenis V. Lunev 	struct seq_net_private p;
3081da177e4SLinus Torvalds 	struct neigh_table *tbl;
309d6bf7817SEric Dumazet 	struct neigh_hash_table *nht;
3101da177e4SLinus Torvalds 	void *(*neigh_sub_iter)(struct neigh_seq_state *state,
3111da177e4SLinus Torvalds 				struct neighbour *n, loff_t *pos);
3121da177e4SLinus Torvalds 	unsigned int bucket;
3131da177e4SLinus Torvalds 	unsigned int flags;
3141da177e4SLinus Torvalds #define NEIGH_SEQ_NEIGH_ONLY	0x00000001
3151da177e4SLinus Torvalds #define NEIGH_SEQ_IS_PNEIGH	0x00000002
3161da177e4SLinus Torvalds #define NEIGH_SEQ_SKIP_NOARP	0x00000004
3171da177e4SLinus Torvalds };
31890972b22SJoe Perches void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *,
31990972b22SJoe Perches 		      unsigned int);
32090972b22SJoe Perches void *neigh_seq_next(struct seq_file *, void *, loff_t *);
32190972b22SJoe Perches void neigh_seq_stop(struct seq_file *, void *);
3221da177e4SLinus Torvalds 
323cb5b09c1SJiri Pirko int neigh_proc_dointvec(struct ctl_table *ctl, int write,
324cb5b09c1SJiri Pirko 			void __user *buffer, size_t *lenp, loff_t *ppos);
325cb5b09c1SJiri Pirko int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
326cb5b09c1SJiri Pirko 				void __user *buffer,
327cb5b09c1SJiri Pirko 				size_t *lenp, loff_t *ppos);
328cb5b09c1SJiri Pirko int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
329cb5b09c1SJiri Pirko 				   void __user *buffer,
330cb5b09c1SJiri Pirko 				   size_t *lenp, loff_t *ppos);
331cb5b09c1SJiri Pirko 
33290972b22SJoe Perches int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
33373af614aSJiri Pirko 			  proc_handler *proc_handler);
33490972b22SJoe Perches void neigh_sysctl_unregister(struct neigh_parms *p);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static inline void __neigh_parms_put(struct neigh_parms *parms)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	atomic_dec(&parms->refcnt);
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	atomic_inc(&parms->refcnt);
3441da177e4SLinus Torvalds 	return parms;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*
3481da177e4SLinus Torvalds  *	Neighbour references
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static inline void neigh_release(struct neighbour *neigh)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	if (atomic_dec_and_test(&neigh->refcnt))
3541da177e4SLinus Torvalds 		neigh_destroy(neigh);
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds static inline struct neighbour * neigh_clone(struct neighbour *neigh)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	if (neigh)
3601da177e4SLinus Torvalds 		atomic_inc(&neigh->refcnt);
3611da177e4SLinus Torvalds 	return neigh;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds #define neigh_hold(n)	atomic_inc(&(n)->refcnt)
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
3671da177e4SLinus Torvalds {
368ef22b7b6STetsuo Handa 	unsigned long now = jiffies;
3690ed8ddf4SEric Dumazet 
3700ed8ddf4SEric Dumazet 	if (neigh->used != now)
3710ed8ddf4SEric Dumazet 		neigh->used = now;
3721da177e4SLinus Torvalds 	if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
3731da177e4SLinus Torvalds 		return __neigh_event_send(neigh, skb);
3741da177e4SLinus Torvalds 	return 0;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
377e179e632SBart De Schuymer #ifdef CONFIG_BRIDGE_NETFILTER
378e179e632SBart De Schuymer static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
379e179e632SBart De Schuymer {
38095c96174SEric Dumazet 	unsigned int seq, hh_alen;
381e179e632SBart De Schuymer 
382e179e632SBart De Schuymer 	do {
383e179e632SBart De Schuymer 		seq = read_seqbegin(&hh->hh_lock);
384e179e632SBart De Schuymer 		hh_alen = HH_DATA_ALIGN(ETH_HLEN);
385e179e632SBart De Schuymer 		memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN);
386e179e632SBart De Schuymer 	} while (read_seqretry(&hh->hh_lock, seq));
387e179e632SBart De Schuymer 	return 0;
388e179e632SBart De Schuymer }
389e179e632SBart De Schuymer #endif
390e179e632SBart De Schuymer 
391425f09abSEric Dumazet static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
3923644f0ceSStephen Hemminger {
39395c96174SEric Dumazet 	unsigned int seq;
3943644f0ceSStephen Hemminger 	int hh_len;
3953644f0ceSStephen Hemminger 
3963644f0ceSStephen Hemminger 	do {
3973644f0ceSStephen Hemminger 		seq = read_seqbegin(&hh->hh_lock);
3983644f0ceSStephen Hemminger 		hh_len = hh->hh_len;
399425f09abSEric Dumazet 		if (likely(hh_len <= HH_DATA_MOD)) {
400425f09abSEric Dumazet 			/* this is inlined by gcc */
401425f09abSEric Dumazet 			memcpy(skb->data - HH_DATA_MOD, hh->hh_data, HH_DATA_MOD);
402425f09abSEric Dumazet 		} else {
403425f09abSEric Dumazet 			int hh_alen = HH_DATA_ALIGN(hh_len);
404425f09abSEric Dumazet 
4053644f0ceSStephen Hemminger 			memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
406425f09abSEric Dumazet 		}
4073644f0ceSStephen Hemminger 	} while (read_seqretry(&hh->hh_lock, seq));
4083644f0ceSStephen Hemminger 
4093644f0ceSStephen Hemminger 	skb_push(skb, hh_len);
410b23b5455SDavid S. Miller 	return dev_queue_xmit(skb);
4113644f0ceSStephen Hemminger }
4123644f0ceSStephen Hemminger 
4131da177e4SLinus Torvalds static inline struct neighbour *
4141da177e4SLinus Torvalds __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct neighbour *n = neigh_lookup(tbl, pkey, dev);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (n || !creat)
4191da177e4SLinus Torvalds 		return n;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	n = neigh_create(tbl, pkey, dev);
4221da177e4SLinus Torvalds 	return IS_ERR(n) ? NULL : n;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds static inline struct neighbour *
4261da177e4SLinus Torvalds __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
4271da177e4SLinus Torvalds   struct net_device *dev)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	struct neighbour *n = neigh_lookup(tbl, pkey, dev);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	if (n)
4321da177e4SLinus Torvalds 		return n;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	return neigh_create(tbl, pkey, dev);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
437a61bbcf2SPatrick McHardy struct neighbour_cb {
438a61bbcf2SPatrick McHardy 	unsigned long sched_next;
439a61bbcf2SPatrick McHardy 	unsigned int flags;
440a61bbcf2SPatrick McHardy };
441a61bbcf2SPatrick McHardy 
442a61bbcf2SPatrick McHardy #define LOCALLY_ENQUEUED 0x1
443a61bbcf2SPatrick McHardy 
444a61bbcf2SPatrick McHardy #define NEIGH_CB(skb)	((struct neighbour_cb *)(skb)->cb)
4451da177e4SLinus Torvalds 
4460ed8ddf4SEric Dumazet static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
4470ed8ddf4SEric Dumazet 				     const struct net_device *dev)
4480ed8ddf4SEric Dumazet {
4490ed8ddf4SEric Dumazet 	unsigned int seq;
4500ed8ddf4SEric Dumazet 
4510ed8ddf4SEric Dumazet 	do {
4520ed8ddf4SEric Dumazet 		seq = read_seqbegin(&n->ha_lock);
4530ed8ddf4SEric Dumazet 		memcpy(dst, n->ha, dev->addr_len);
4540ed8ddf4SEric Dumazet 	} while (read_seqretry(&n->ha_lock, seq));
4550ed8ddf4SEric Dumazet }
4561da177e4SLinus Torvalds #endif
457